1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3 // Modified 2010-2012 by <adrian -at- atrox.at>
4 // Modified 2012 by <vsza at vsza.hu>
6 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
7 // at your option, any later version. See the LICENSE.txt file for the text of
9 //-----------------------------------------------------------------------------
10 // High frequency ISO15693 commands
11 //-----------------------------------------------------------------------------
12 // There are three basic operation modes, depending on which device (proxmark/pc)
13 // the signal processing, (de)modulation, transmission protocol and logic is done.
15 // All steps are done on the proxmark, the output of the commands is returned via
16 // USB-debug-print commands.
18 // The protocol is done on the PC, passing only Iso15693 data frames via USB. This
19 // allows direct communication with a tag on command level
21 // The proxmark just samples the antenna and passes this "analog" data via USB to
22 // the client. Signal Processing & decoding is done on the pc. This is the slowest
23 // variant, but offers the possibility to analyze the waveforms directly.
36 #include "cmdparser.h"
37 #include "iso15693tools.h"
38 #include "protocols.h"
41 #define Crc(data,datalen) Iso15693Crc(data,datalen)
42 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
43 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
46 // 1) Unmodulated time of 56.64us
47 // 2) 24 pulses of 423.75khz
48 // 3) logic '1' (unmodulated for 18.88us followed by 8 pulses of 423.75khz)
50 static const int Iso15693FrameSOF
[] = {
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
54 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60 static const int Iso15693Logic0
[] = {
66 static const int Iso15693Logic1
[] = {
74 // 1) logic '0' (8 pulses of 423.75khz followed by unmodulated for 18.88us)
75 // 2) 24 pulses of 423.75khz
76 // 3) Unmodulated time of 56.64us
78 static const int Iso15693FrameEOF
[] = {
83 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
84 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
89 // structure and database for uid -> tagtype lookups
92 int mask
; // how many MSB bits used
97 const productName uidmapping
[] = {
99 // UID, #significant Bits, "Vendor(+Product)"
100 { 0xE001000000000000LL
, 16, "Motorola UK" },
103 // 02 = ST Microelectronics
104 // XX = IC id (Chip ID Family)
105 { 0xE002000000000000LL
, 16, "ST Microelectronics SA France" },
106 { 0xE002050000000000LL
, 24, "ST Microelectronics; LRI64 [IC id = 05]"},
107 { 0xE002080000000000LL
, 24, "ST Microelectronics; LRI2K [IC id = 08]"},
108 { 0xE0020A0000000000LL
, 24, "ST Microelectronics; LRIS2K [IC id = 10]"},
109 { 0xE002440000000000LL
, 24, "ST Microelectronics; LRIS64K [IC id = 68]"},
111 { 0xE003000000000000LL
, 16, "Hitachi, Ltd Japan" },
114 // 04 = Manufacturer code (Philips/NXP)
115 // XX = IC id (Chip ID Family)
116 //I-Code SLI SL2 ICS20 [IC id = 01]
117 //I-Code SLI-S [IC id = 02]
118 //I-Code SLI-L [IC id = 03]
119 //I-Code SLIX [IC id = 01 + bit36 set to 1 (starting from bit0 - different from normal SLI)]
120 //I-Code SLIX-S [IC id = 02 + bit36 set to 1]
121 //I-Code SLIX-L [IC id = 03 + bit36 set to 1]
122 { 0xE004000000000000LL
, 16, "NXP Semiconductors Germany (Philips)" },
123 { 0xE004010000000000LL
, 24, "NXP(Philips); IC SL2 ICS20/ICS21(SLI) ICS2002/ICS2102(SLIX)" },
124 { 0xE004020000000000LL
, 24, "NXP(Philips); IC SL2 ICS53/ICS54(SLI-S) ICS5302/ICS5402(SLIX-S)" },
125 { 0xE004030000000000LL
, 24, "NXP(Philips); IC SL2 ICS50/ICS51(SLI-L) ICS5002/ICS5102(SLIX-L)" },
128 // 05 = Manufacturer code (Infineon)
129 // XX = IC id (Chip ID Family)
130 { 0xE005000000000000LL
, 16, "Infineon Technologies AG Germany" },
131 { 0xE005A10000000000LL
, 24, "Infineon; SRF55V01P [IC id = 161] plain mode 1kBit"},
132 { 0xE005A80000000000LL
, 24, "Infineon; SRF55V01P [IC id = 168] pilot series 1kBit"},
133 { 0xE005400000000000LL
, 24, "Infineon; SRF55V02P [IC id = 64] plain mode 2kBit"},
134 { 0xE005000000000000LL
, 24, "Infineon; SRF55V10P [IC id = 00] plain mode 10KBit"},
135 { 0xE005500000000000LL
, 24, "Infineon; SRF55V02S [IC id = 80] secure mode 2kBit"},
136 { 0xE005100000000000LL
, 24, "Infineon; SRF55V10S [IC id = 16] secure mode 10KBit"},
137 { 0xE0051E0000000000LL
, 23, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
138 { 0xE005200000000000LL
, 21, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
140 { 0xE006000000000000LL
, 16, "Cylink USA" },
144 // 07 = Texas Instruments
145 // XX = from bit 41 to bit 43 = product configuration - from bit 44 to bit 47 IC id (Chip ID Family)
146 //Tag IT RFIDType-I Plus, 2kBit, TI Inlay
147 //Tag-it HF-I Plus Inlay [IC id = 00] -> b'0000 000 2kBit
148 //Tag-it HF-I Plus Chip [IC id = 64] -> b'1000 000 2kBit
149 //Tag-it HF-I Standard Chip / Inlays [IC id = 96] -> b'1100 000 256Bit
150 //Tag-it HF-I Pro Chip / Inlays [IC id = 98] -> b'1100 010 256Bit, Password protection
151 { 0xE007000000000000LL
, 16, "Texas Instrument France" },
152 { 0xE007000000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
153 { 0xE007100000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
154 { 0xE007800000000000LL
, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
155 { 0xE007C00000000000LL
, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
156 { 0xE007C40000000000LL
, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
158 { 0xE008000000000000LL
, 16, "Fujitsu Limited Japan" },
159 { 0xE009000000000000LL
, 16, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
160 { 0xE00A000000000000LL
, 16, "NEC Japan" },
161 { 0xE00B000000000000LL
, 16, "Oki Electric Industry Co. Ltd Japan" },
162 { 0xE00C000000000000LL
, 16, "Toshiba Corp. Japan" },
163 { 0xE00D000000000000LL
, 16, "Mitsubishi Electric Corp. Japan" },
164 { 0xE00E000000000000LL
, 16, "Samsung Electronics Co. Ltd Korea" },
165 { 0xE00F000000000000LL
, 16, "Hynix / Hyundai, Korea" },
166 { 0xE010000000000000LL
, 16, "LG-Semiconductors Co. Ltd Korea" },
167 { 0xE011000000000000LL
, 16, "Emosyn-EM Microelectronics USA" },
169 { 0xE012000000000000LL
, 16, "HID Corporation" },
170 { 0xE012000000000000LL
, 16, "INSIDE Technology France" },
171 { 0xE013000000000000LL
, 16, "ORGA Kartensysteme GmbH Germany" },
172 { 0xE014000000000000LL
, 16, "SHARP Corporation Japan" },
173 { 0xE015000000000000LL
, 16, "ATMEL France" },
175 { 0xE016000000000000LL
, 16, "EM Microelectronic-Marin SA Switzerland (Skidata)"},
176 { 0xE016040000000000LL
, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034 [IC id = 01] (Read/Write - no AFI)"},
177 { 0xE0160C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4035 [IC id = 03] (Read/Write - replaced by 4233)"},
178 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135 [IC id = 04] (Read/Write - replaced by 4233) 36x64bit start page 13"},
179 { 0xE016140000000000LL
, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 05] 28pF"},
180 { 0xE016180000000000LL
, 24, "EM-Marin SA (Skidata); EM4006 [IC id = 06] (Read Only)"},
181 { 0xE0161C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 07] 23,5pF (Read/Write)"},
182 { 0xE016200000000000LL
, 24, "EM-Marin SA (Skidata); EM4033 [IC id = 08] 23,5pF (Read Only - no AFI / no DSFID / no security blocks)"},
183 { 0xE016240000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 09] 23,5pF CustomerID-102"},
184 { 0xE016280000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 10] 23,5pF (1Kb flash memory - not provide High Security mode and QuietStorage feature)" },
185 { 0xE0163C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 15] 23,5pF"},
186 { 0xE0167C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 31] 95pF"},
187 { 0xE016940000000000LL
, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 37] 95pF 51x64bit "},
188 { 0xE0169c0000000000LL
, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 39] 95pF (Read/Write)" },
189 { 0xE016A80000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 42] 97pF" },
190 { 0xE016BC0000000000LL
, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 47] 97pF" },
192 { 0xE017000000000000LL
, 16, "KSW Microtec GmbH Germany" },
193 { 0xE018000000000000LL
, 16, "ZMD AG Germany" },
194 { 0xE019000000000000LL
, 16, "XICOR, Inc. USA" },
195 { 0xE01A000000000000LL
, 16, "Sony Corporation Japan Identifier Company Country" },
196 { 0xE01B000000000000LL
, 16, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
197 { 0xE01C000000000000LL
, 16, "Emosyn USA" },
198 { 0xE01D000000000000LL
, 16, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
199 { 0xE01E000000000000LL
, 16, "Magellan Technology Pty Limited Australia" },
200 { 0xE01F000000000000LL
, 16, "Melexis NV BO Switzerland" },
201 { 0xE020000000000000LL
, 16, "Renesas Technology Corp. Japan" },
202 { 0xE021000000000000LL
, 16, "TAGSYS France" },
203 { 0xE022000000000000LL
, 16, "Transcore USA" },
204 { 0xE023000000000000LL
, 16, "Shanghai belling corp., ltd. China" },
205 { 0xE024000000000000LL
, 16, "Masktech Germany Gmbh Germany" },
206 { 0xE025000000000000LL
, 16, "Innovision Research and Technology Plc UK" },
207 { 0xE026000000000000LL
, 16, "Hitachi ULSI Systems Co., Ltd. Japan" },
208 { 0xE027000000000000LL
, 16, "Cypak AB Sweden" },
209 { 0xE028000000000000LL
, 16, "Ricoh Japan" },
210 { 0xE029000000000000LL
, 16, "ASK France" },
211 { 0xE02A000000000000LL
, 16, "Unicore Microsystems, LLC Russian Federation" },
212 { 0xE02B000000000000LL
, 16, "Dallas Semiconductor/Maxim USA" },
213 { 0xE02C000000000000LL
, 16, "Impinj, Inc. USA" },
214 { 0xE02D000000000000LL
, 16, "RightPlug Alliance USA" },
215 { 0xE02E000000000000LL
, 16, "Broadcom Corporation USA" },
216 { 0xE02F000000000000LL
, 16, "MStar Semiconductor, Inc Taiwan, ROC" },
217 { 0xE030000000000000LL
, 16, "BeeDar Technology Inc. USA" },
218 { 0xE031000000000000LL
, 16, " RFIDsec Denmark" },
219 { 0xE032000000000000LL
, 16, " Schweizer Electronic AG Germany" },
220 { 0xE033000000000000LL
, 16, " AMIC Technology Corp Taiwan" },
221 { 0xE034000000000000LL
, 16, "Mikron JSC Russia" },
222 { 0xE035000000000000LL
, 16, "Fraunhofer Institute for Photonic Microsystems Germany" },
223 { 0xE036000000000000LL
, 16, "IDS Microchip AG Switzerland" },
224 { 0xE037000000000000LL
, 16, "Kovio USA" },
225 { 0xE038000000000000LL
, 16, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
226 { 0xE039000000000000LL
, 16, "Silicon Craft Technology Thailand" },
227 { 0xE03A000000000000LL
, 16, "Advanced Film Device Inc. Japan" },
228 { 0xE03B000000000000LL
, 16, "Nitecrest Ltd UK" },
229 { 0xE03C000000000000LL
, 16, "Verayo Inc. USA" },
230 { 0xE03D000000000000LL
, 16, "HID Global USA" },
231 { 0xE03E000000000000LL
, 16, "Productivity Engineering Gmbh Germany" },
232 { 0xE03F000000000000LL
, 16, "Austriamicrosystems AG (reserved) Austria" },
233 { 0xE040000000000000LL
, 16, "Gemalto SA France" },
234 { 0xE041000000000000LL
, 16, "Renesas Electronics Corporation Japan" },
235 { 0xE042000000000000LL
, 16, "3Alogics Inc Korea" },
236 { 0xE043000000000000LL
, 16, "Top TroniQ Asia Limited Hong Kong" },
237 { 0xE044000000000000LL
, 16, "Gentag Inc (USA) USA" },
238 { 0,0,"no tag-info available" } // must be the last entry
242 // fast method to just read the UID of a tag (collission detection not supported)
243 // *buf should be large enough to fit the 64bit uid
244 // returns 1 if suceeded
245 int getUID(uint8_t *buf
)
249 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
250 uint8_t *req
=c
.d
.asBytes
;
253 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
255 req
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
256 req
[1] = ISO15693_INVENTORY
;
257 req
[2] = 0; // mask length
258 reqlen
= AddCrc(req
,3);
263 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
264 recv
= resp
.d
.asBytes
;
265 if (resp
.arg
[0]>=12 && ISO15693_CRC_CHECK
==Crc(recv
,12)) {
266 memcpy(buf
,&recv
[2],8);
276 // get a product description based on the UID
278 // returns description of the best match
279 static char* getTagInfo(uint8_t *uid
) {
282 memcpy(&myuid
,uid
,sizeof(uint64_t));
283 while (uidmapping
[i
].mask
>0) {
284 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
285 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
289 if (uidmapping
[i
].mask
>uidmapping
[best
].mask
) {
297 if (best
>=0) return uidmapping
[best
].desc
;
299 return uidmapping
[i
].desc
;
303 // return a clear-text message to an errorcode
304 static char* TagErrorStr(uint8_t error
) {
306 case 0x01: return "The command is not supported";
307 case 0x02: return "The command is not recognised";
308 case 0x03: return "The option is not supported.";
309 case 0x0f: return "Unknown error.";
310 case 0x10: return "The specified block is not available (doesn't exist).";
311 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
312 case 0x12: return "The specified block is locked and its content cannot be changed.";
313 case 0x13: return "The specified block was not successfully programmed.";
314 case 0x14: return "The specified block was not successfully locked.";
315 default: return "Reserved for Future Use or Custom command error.";
321 int CmdHF15Demod(const char *Cmd
)
323 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
326 int max
= 0, maxPos
= 0;
330 if (GraphTraceLen
< 2000) return 0;
332 // First, correlate for SOF
333 for (i
= 0; i
< 200; i
++) {
335 for (j
= 0; j
< arraylen(Iso15693FrameSOF
); j
+= skip
) {
336 corr
+= Iso15693FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
343 PrintAndLog("SOF at %d, correlation %d", maxPos
,
344 max
/ (arraylen(Iso15693FrameSOF
) / skip
));
346 i
= maxPos
+ arraylen(Iso15693FrameSOF
) / skip
;
349 memset(outBuf
, 0, sizeof(outBuf
));
352 int corr0
= 0, corr00
= 0, corr01
= 0, corr1
= 0, corrEOF
= 0;
353 for(j
= 0; j
< arraylen(Iso15693Logic0
); j
+= skip
) {
354 corr0
+= Iso15693Logic0
[j
]*GraphBuffer
[i
+(j
/skip
)];
356 corr01
= corr00
= corr0
;
357 for(j
= 0; j
< arraylen(Iso15693Logic0
); j
+= skip
) {
358 corr00
+= Iso15693Logic0
[j
]*GraphBuffer
[i
+arraylen(Iso15693Logic0
)/skip
+(j
/skip
)];
359 corr01
+= Iso15693Logic1
[j
]*GraphBuffer
[i
+arraylen(Iso15693Logic0
)/skip
+(j
/skip
)];
361 for(j
= 0; j
< arraylen(Iso15693Logic1
); j
+= skip
) {
362 corr1
+= Iso15693Logic1
[j
]*GraphBuffer
[i
+(j
/skip
)];
364 for(j
= 0; j
< arraylen(Iso15693FrameEOF
); j
+= skip
) {
365 corrEOF
+= Iso15693FrameEOF
[j
]*GraphBuffer
[i
+(j
/skip
)];
367 // Even things out by the length of the target waveform.
373 if(corrEOF
> corr1
&& corrEOF
> corr00
&& corrEOF
> corr01
) {
374 PrintAndLog("EOF at %d", i
);
376 } else if (corr1
> corr0
) {
377 i
+= arraylen(Iso15693Logic1
) / skip
;
380 i
+= arraylen(Iso15693Logic0
) / skip
;
387 if ((i
+ (int)arraylen(Iso15693FrameEOF
)) >= GraphTraceLen
) {
388 PrintAndLog("ran off end!");
393 PrintAndLog("error, uneven octet! (discard extra bits!)");
394 PrintAndLog(" mask=%02x", mask
);
396 PrintAndLog("%d octets", k
);
398 for (i
= 0; i
< k
; i
++) {
399 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
401 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
407 // * Acquire Samples as Reader (enables carrier, sends inquiry)
408 int CmdHF15Read(const char *Cmd
)
410 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
415 // Record Activity without enabling carrier
416 int CmdHF15Snoop(const char *Cmd
)
418 UsbCommand c
= {CMD_SNOOP_ISO_15693
};
423 int HF15Reader(const char *Cmd
, bool verbose
)
428 if (verbose
) PrintAndLog("No Tag found.");
432 PrintAndLog("Tag UID : %s",sprintUID(NULL
,uid
));
433 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
437 int CmdHF15Reader(const char *Cmd
)
439 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
444 // Simulation is still not working very good
445 int CmdHF15Sim(const char *Cmd
)
447 char cmdp
= param_getchar(Cmd
, 0);
448 uint8_t uid
[8] = {0x00};
450 //E0 16 24 00 00 00 00 00
451 if (cmdp
== 'h' || cmdp
== 'H') {
452 PrintAndLog("Usage: hf 15 sim <UID>");
454 PrintAndLog(" sample: hf 15 sim E016240000000000");
458 if (param_gethex(Cmd
, 0, uid
, 16)) {
459 PrintAndLog("UID must include 16 HEX symbols");
463 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
464 uid
[0],uid
[1],uid
[2],uid
[3],uid
[4], uid
[5], uid
[6], uid
[7]);
465 PrintAndLog("Press the button to stop simulation");
467 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {0, 0, 0}};
468 memcpy(c
.d
.asBytes
,uid
,8);
474 // finds the AFI (Application Family Idendifier) of a card, by trying all values
475 // (There is no standard way of reading the AFI, allthough some tags support this)
476 int CmdHF15Afi(const char *Cmd
)
478 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
483 // Reads all memory pages
484 int CmdHF15DumpMem(const char*Cmd
) {
488 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
489 uint8_t *req
=c
.d
.asBytes
;
495 PrintAndLog("No Tag found.");
499 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
500 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
502 for (int retry
=0; retry
<5; retry
++) {
504 req
[0]= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
505 req
[1] = ISO15693_READBLOCK
;
506 memcpy(&req
[2],uid
,8);
508 reqlen
= AddCrc(req
,11);
513 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
514 recv
= resp
.d
.asBytes
;
515 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
516 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
518 *output
=0; // reset outputstring
519 sprintf(output
, "Block %02x ",blocknum
);
520 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in hex
521 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
524 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in cleaned ascii
525 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
527 PrintAndLog("%s",output
);
529 // PrintAndLog("bn=%i",blocknum);
531 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
534 } // else PrintAndLog("crc");
535 } // else PrintAndLog("r null");
538 // if (resp.arg[0]<3)
539 // PrintAndLog("Lost Connection");
540 // else if (ISO15693_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
541 // PrintAndLog("CRC Failed");
543 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
550 static command_t CommandTable15
[] =
552 {"help", CmdHF15Help
, 1, "This help"},
553 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
554 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
555 {"snoop", CmdHF15Snoop
, 0, "Eavesdrop ISO 15693 communications"},
556 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
557 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
558 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
559 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
560 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
561 {NULL
, NULL
, 0, NULL
}
564 int CmdHF15(const char *Cmd
)
566 CmdsParse(CommandTable15
, Cmd
);
570 int CmdHF15Help(const char *Cmd
)
572 CmdsHelp(CommandTable15
);
577 // "HF 15 Cmd" Interface
578 // Allows direct communication with the tag on command level
580 int CmdHF15CmdInquiry(const char *Cmd
)
584 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
585 uint8_t *req
=c
.d
.asBytes
;
588 req
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
589 req
[1] = ISO15693_INVENTORY
;
590 req
[2] = 0; // mask length
591 reqlen
=AddCrc(req
,3);
596 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
597 if (resp
.arg
[0]>=12) {
598 recv
= resp
.d
.asBytes
;
599 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
600 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
602 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp
.arg
[0]);
605 PrintAndLog("timeout.");
611 // Turns debugging on(1)/off(0)
612 int CmdHF15CmdDebug( const char *cmd
) {
613 int debug
= atoi(cmd
);
614 if (strlen(cmd
) < 1) {
615 PrintAndLog("Usage: hf 15 debug <0|1>");
616 PrintAndLog(" 0 no debugging");
617 PrintAndLog(" 1 turn debugging on");
621 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
627 int CmdHF15CmdRaw (const char *cmd
) {
630 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
637 unsigned int datalen
=0, temp
;
642 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
643 PrintAndLog(" -r do not read response");
644 PrintAndLog(" -2 use slower '1 out of 256' mode");
645 PrintAndLog(" -c calculate and append CRC");
646 PrintAndLog(" Tip: turn on debugging for verbose output");
651 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
653 while (cmd
[i
]!='\0') {
654 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
669 PrintAndLog("Invalid option");
675 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
676 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
677 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
678 buf
[strlen(buf
)+1]=0;
679 buf
[strlen(buf
)]=cmd
[i
];
682 if (strlen(buf
)>=2) {
683 sscanf(buf
,"%x",&temp
);
684 data
[datalen
]=(uint8_t)(temp
& 0xff);
690 PrintAndLog("Invalid char on input");
693 if (crc
) datalen
=AddCrc(data
,datalen
);
698 memcpy(c
.d
.asBytes
,data
,datalen
);
703 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
704 recv
= resp
.d
.asBytes
;
705 PrintAndLog("received %i octets",resp
.arg
[0]);
706 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
707 if (hexout
!= NULL
) {
708 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
709 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
711 PrintAndLog("%s", hexout
);
715 PrintAndLog("timeout while waiting for reply.");
724 * parses common HF 15 CMD parameters and prepares some data structures
728 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
730 uint8_t *req
=c
->d
.asBytes
;
731 uint8_t uid
[8] = {0x00};
735 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
737 if (strstr(*cmd
,"-2")==*cmd
) {
738 c
->arg
[1]=0; // use 1of256
743 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
745 if (strstr(*cmd
,"-o")==*cmd
) {
746 req
[reqlen
]=ISO15693_REQ_OPTION
;
751 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
755 PrintAndLog("missing addr");
760 // you must have selected the tag earlier
761 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_SELECT
;
762 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
763 reqlen
+= iso15cmdlen
;
767 // unaddressed mode may not be supported by all vendors
768 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
;
769 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
770 reqlen
+= iso15cmdlen
;
773 // we scan for the UID ourself
774 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
775 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
778 PrintAndLog("No Tag found");
781 memcpy(req
+reqlen
,uid
,8);
782 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
786 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
787 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
790 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
791 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
792 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
793 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
794 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
795 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
796 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
800 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
801 memcpy(&req
[reqlen
],&uid
[0],8);
804 // skip to next space
805 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
806 // skip over the space
807 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
814 * Commandline handling: HF15 CMD SYSINFO
815 * get system information from tag/VICC
817 int CmdHF15CmdSysinfo(const char *Cmd
) {
820 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
821 uint8_t *req
=c
.d
.asBytes
;
825 char output
[2048]="";
832 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
833 PrintAndLog(" options:");
834 PrintAndLog(" -2 use slower '1 out of 256' mode");
835 PrintAndLog(" uid (either): ");
836 PrintAndLog(" <8B hex> full UID eg E011223344556677");
837 PrintAndLog(" s selected tag");
838 PrintAndLog(" u unaddressed mode");
839 PrintAndLog(" * scan for tag");
840 PrintAndLog(" start#: page number to start 0-255");
841 PrintAndLog(" count#: number of pages");
845 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_GET_SYSTEM_INFO
},1);
848 reqlen
=AddCrc(req
,reqlen
);
853 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
854 recv
= resp
.d
.asBytes
;
855 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
856 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
857 *output
=0; // reset outputstring
858 for ( i
=1; i
<resp
.arg
[0]-2; i
++) {
859 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
861 strcat(output
,"\n\r");
862 strcat(output
,"UID = ");
863 strcat(output
,sprintUID(NULL
,recv
+2));
864 strcat(output
,"\n\r");
865 strcat(output
,getTagInfo(recv
+2)); //ABC
866 strcat(output
,"\n\r");
869 sprintf(output
+strlen(output
),"DSFID supported, set to %02X\n\r",recv
[i
++]);
871 strcat(output
,"DSFID not supported\n\r");
873 sprintf(output
+strlen(output
),"AFI supported, set to %03X\n\r",recv
[i
++]);
875 strcat(output
,"AFI not supported\n\r");
876 if (recv
[1] & 0x04) {
877 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
878 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
879 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
882 strcat(output
,"Tag does not provide information on memory layout\n\r");
883 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02X\n\r",recv
[i
++]);
884 else strcat(output
,"IC reference not given\n\r");
887 PrintAndLog("%s",output
);
889 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
892 PrintAndLog("CRC failed");
895 PrintAndLog("timeout: no answer");
902 * Commandline handling: HF15 CMD READMULTI
903 * Read multiple blocks at once (not all tags support this)
905 int CmdHF15CmdReadmulti(const char *Cmd
) {
908 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
909 uint8_t *req
=c
.d
.asBytes
;
910 int reqlen
=0, pagenum
,pagecount
;
913 char output
[2048]="";
919 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
920 PrintAndLog(" options:");
921 PrintAndLog(" -2 use slower '1 out of 256' mode");
922 PrintAndLog(" uid (either): ");
923 PrintAndLog(" <8B hex> full UID eg E011223344556677");
924 PrintAndLog(" s selected tag");
925 PrintAndLog(" u unaddressed mode");
926 PrintAndLog(" * scan for tag");
927 PrintAndLog(" start#: page number to start 0-255");
928 PrintAndLog(" count#: number of pages");
932 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_READ_MULTI_BLOCK
},1);
935 pagenum
=strtol(cmd
,NULL
,0);
937 // skip to next space
938 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
939 // skip over the space
940 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
942 pagecount
=strtol(cmd
,NULL
,0);
943 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
945 req
[reqlen
++]=(uint8_t)pagenum
;
946 req
[reqlen
++]=(uint8_t)pagecount
;
948 reqlen
=AddCrc(req
,reqlen
);
954 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
955 recv
= resp
.d
.asBytes
;
956 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
957 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
958 *output
=0; // reset outputstring
959 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
960 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
963 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
964 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
966 PrintAndLog("%s",output
);
968 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
971 PrintAndLog("CRC failed");
974 PrintAndLog("no answer");
981 * Commandline handling: HF15 CMD READ
982 * Reads a single Block
984 int CmdHF15CmdRead(const char *Cmd
) {
987 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
988 uint8_t *req
=c
.d
.asBytes
;
989 int reqlen
=0, pagenum
;
998 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
999 PrintAndLog(" options:");
1000 PrintAndLog(" -2 use slower '1 out of 256' mode");
1001 PrintAndLog(" uid (either): ");
1002 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1003 PrintAndLog(" s selected tag");
1004 PrintAndLog(" u unaddressed mode");
1005 PrintAndLog(" * scan for tag");
1006 PrintAndLog(" page#: page number 0-255");
1010 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_READBLOCK
},1);
1013 pagenum
=strtol(cmd
,NULL
,0);
1015 PrintAndLog("invalid pagenum");
1019 req
[reqlen
++]=(uint8_t)pagenum
;
1021 reqlen
=AddCrc(req
,reqlen
);
1027 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
1028 recv
= resp
.d
.asBytes
;
1029 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
1030 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
1031 *output
=0; // reset outputstring
1032 //sprintf(output, "Block %2i ",blocknum);
1033 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
1034 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
1037 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
1038 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
1040 PrintAndLog("%s",output
);
1042 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1045 PrintAndLog("CRC failed");
1048 PrintAndLog("no answer");
1056 * Commandline handling: HF15 CMD WRITE
1057 * Writes a single Block - might run into timeout, even when successful
1059 int CmdHF15CmdWrite(const char *Cmd
) {
1062 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
1063 uint8_t *req
=c
.d
.asBytes
;
1064 int reqlen
=0, pagenum
, temp
;
1069 strncpy(cmd
,Cmd
,99);
1072 if (strlen(cmd
)<3) {
1073 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
1074 PrintAndLog(" options:");
1075 PrintAndLog(" -2 use slower '1 out of 256' mode");
1076 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1077 PrintAndLog(" uid (either): ");
1078 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1079 PrintAndLog(" s selected tag");
1080 PrintAndLog(" u unaddressed mode");
1081 PrintAndLog(" * scan for tag");
1082 PrintAndLog(" page#: page number 0-255");
1083 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1087 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_WRITEBLOCK
},1);
1090 // *cmd -> page num ; *cmd2 -> data
1092 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
1096 pagenum
=strtol(cmd
,NULL
,0);
1098 PrintAndLog("invalid pagenum");
1101 req
[reqlen
++]=(uint8_t)pagenum
;
1104 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
1109 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
1110 req
[reqlen
++]=temp
& 0xff;
1114 reqlen
=AddCrc(req
,reqlen
);
1120 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000) && resp
.arg
[0]>2) {
1121 recv
= resp
.d
.asBytes
;
1122 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
1123 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
1126 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1129 PrintAndLog("CRC failed");
1132 PrintAndLog("timeout: no answer - data may be written anyway");
1140 static command_t CommandTable15Cmd
[] =
1142 {"help", CmdHF15CmdHelp
, 1, "This Help"},
1143 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
1145 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1147 {"read", CmdHF15CmdRead
, 0, "Read a block"},
1148 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
1149 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
1150 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
1151 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
1152 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
1153 {NULL
, NULL
, 0, NULL
}
1156 int CmdHF15Cmd(const char *Cmd
)
1158 CmdsParse(CommandTable15Cmd
, Cmd
);
1162 int CmdHF15CmdHelp(const char *Cmd
)
1164 CmdsHelp(CommandTable15Cmd
);