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 FrameSOF Iso15693FrameSOF
42 #define Logic0 Iso15693Logic0
43 #define Logic1 Iso15693Logic1
44 #define FrameEOF Iso15693FrameEOF
46 #define Crc(data,datalen) Iso15693Crc(data,datalen)
47 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
48 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
50 // structure and database for uid -> tagtype lookups
53 int mask
; // how many MSB bits used
58 const productName uidmapping
[] = {
60 // UID, #significant Bits, "Vendor(+Product)"
61 { 0xE001000000000000LL
, 16, "Motorola UK" },
64 // 02 = ST Microelectronics
65 // XX = IC id (Chip ID Family)
66 { 0xE002000000000000LL
, 16, "ST Microelectronics SA France" },
67 { 0xE002050000000000LL
, 24, "ST Microelectronics; LRI64 [IC id = 05]"},
68 { 0xE002080000000000LL
, 24, "ST Microelectronics; LRI2K [IC id = 08]"},
69 { 0xE0020A0000000000LL
, 24, "ST Microelectronics; LRIS2K [IC id = 10]"},
70 { 0xE002440000000000LL
, 24, "ST Microelectronics; LRIS64K [IC id = 68]"},
72 { 0xE003000000000000LL
, 16, "Hitachi, Ltd Japan" },
75 // 04 = Manufacturer code (Philips/NXP)
76 // XX = IC id (Chip ID Family)
77 //I-Code SLI SL2 ICS20 [IC id = 01]
78 //I-Code SLI-S [IC id = 02]
79 //I-Code SLI-L [IC id = 03]
80 //I-Code SLIX [IC id = 01 + bit36 set to 1 (starting from bit0 - different from normal SLI)]
81 //I-Code SLIX-S [IC id = 02 + bit36 set to 1]
82 //I-Code SLIX-L [IC id = 03 + bit36 set to 1]
83 { 0xE004000000000000LL
, 16, "NXP Semiconductors Germany (Philips)" },
84 { 0xE004010000000000LL
, 24, "NXP(Philips); IC SL2 ICS20/ICS21(SLI) ICS2002/ICS2102(SLIX)" },
85 { 0xE004020000000000LL
, 24, "NXP(Philips); IC SL2 ICS53/ICS54(SLI-S) ICS5302/ICS5402(SLIX-S)" },
86 { 0xE004030000000000LL
, 24, "NXP(Philips); IC SL2 ICS50/ICS51(SLI-L) ICS5002/ICS5102(SLIX-L)" },
89 // 05 = Manufacturer code (Infineon)
90 // XX = IC id (Chip ID Family)
91 { 0xE005000000000000LL
, 16, "Infineon Technologies AG Germany" },
92 { 0xE005A10000000000LL
, 24, "Infineon; SRF55V01P [IC id = 161] plain mode 1kBit"},
93 { 0xE005A80000000000LL
, 24, "Infineon; SRF55V01P [IC id = 168] pilot series 1kBit"},
94 { 0xE005400000000000LL
, 24, "Infineon; SRF55V02P [IC id = 64] plain mode 2kBit"},
95 { 0xE005000000000000LL
, 24, "Infineon; SRF55V10P [IC id = 00] plain mode 10KBit"},
96 { 0xE005500000000000LL
, 24, "Infineon; SRF55V02S [IC id = 80] secure mode 2kBit"},
97 { 0xE005100000000000LL
, 24, "Infineon; SRF55V10S [IC id = 16] secure mode 10KBit"},
98 { 0xE0051E0000000000LL
, 23, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
99 { 0xE005200000000000LL
, 21, "Infineon; SLE66r01P [IC id = 3x = My-d Move or My-d move NFC]"},
101 { 0xE006000000000000LL
, 16, "Cylink USA" },
105 // 07 = Texas Instruments
106 // XX = from bit 41 to bit 43 = product configuration - from bit 44 to bit 47 IC id (Chip ID Family)
107 //Tag IT RFIDType-I Plus, 2kBit, TI Inlay
108 //Tag-it HF-I Plus Inlay [IC id = 00] -> b'0000 000 2kBit
109 //Tag-it HF-I Plus Chip [IC id = 64] -> b'1000 000 2kBit
110 //Tag-it HF-I Standard Chip / Inlays [IC id = 96] -> b'1100 000 256Bit
111 //Tag-it HF-I Pro Chip / Inlays [IC id = 98] -> b'1100 010 256Bit, Password protection
112 { 0xE007000000000000LL
, 16, "Texas Instrument France" },
113 { 0xE007000000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
114 { 0xE007100000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
115 { 0xE007800000000000LL
, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
116 { 0xE007C00000000000LL
, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
117 { 0xE007C40000000000LL
, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
119 { 0xE008000000000000LL
, 16, "Fujitsu Limited Japan" },
120 { 0xE009000000000000LL
, 16, "Matsushita Electronics Corporation, Semiconductor Company Japan" },
121 { 0xE00A000000000000LL
, 16, "NEC Japan" },
122 { 0xE00B000000000000LL
, 16, "Oki Electric Industry Co. Ltd Japan" },
123 { 0xE00C000000000000LL
, 16, "Toshiba Corp. Japan" },
124 { 0xE00D000000000000LL
, 16, "Mitsubishi Electric Corp. Japan" },
125 { 0xE00E000000000000LL
, 16, "Samsung Electronics Co. Ltd Korea" },
126 { 0xE00F000000000000LL
, 16, "Hynix / Hyundai, Korea" },
127 { 0xE010000000000000LL
, 16, "LG-Semiconductors Co. Ltd Korea" },
128 { 0xE011000000000000LL
, 16, "Emosyn-EM Microelectronics USA" },
130 { 0xE012000000000000LL
, 16, "HID Corporation" },
131 { 0xE012000000000000LL
, 16, "INSIDE Technology France" },
132 { 0xE013000000000000LL
, 16, "ORGA Kartensysteme GmbH Germany" },
133 { 0xE014000000000000LL
, 16, "SHARP Corporation Japan" },
134 { 0xE015000000000000LL
, 16, "ATMEL France" },
136 { 0xE016000000000000LL
, 16, "EM Microelectronic-Marin SA Switzerland (Skidata)"},
137 { 0xE016040000000000LL
, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034 [IC id = 01] (Read/Write - no AFI)"},
138 { 0xE0160C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4035 [IC id = 03] (Read/Write - replaced by 4233)"},
139 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135 [IC id = 04] (Read/Write - replaced by 4233) 36x64bit start page 13"},
140 { 0xE016140000000000LL
, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 05] 28pF"},
141 { 0xE016180000000000LL
, 24, "EM-Marin SA (Skidata); EM4006 [IC id = 06] (Read Only)"},
142 { 0xE0161C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 07] 23,5pF (Read/Write)"},
143 { 0xE016200000000000LL
, 24, "EM-Marin SA (Skidata); EM4033 [IC id = 08] 23,5pF (Read Only - no AFI / no DSFID / no security blocks)"},
144 { 0xE016240000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 09] 23,5pF CustomerID-102"},
145 { 0xE016280000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 10] 23,5pF (1Kb flash memory - not provide High Security mode and QuietStorage feature)" },
146 { 0xE0163C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 15] 23,5pF"},
147 { 0xE0167C0000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 [IC id = 31] 95pF"},
148 { 0xE016940000000000LL
, 24, "EM-Marin SA (Skidata); EM4036 [IC id = 37] 95pF 51x64bit "},
149 { 0xE0169c0000000000LL
, 24, "EM-Marin SA (Skidata); EM4133 [IC id = 39] 95pF (Read/Write)" },
150 { 0xE016A80000000000LL
, 24, "EM-Marin SA (Skidata); EM4233 SLIC [IC id = 42] 97pF" },
151 { 0xE016BC0000000000LL
, 24, "EM-Marin SA (Skidata); EM4237 [IC id = 47] 97pF" },
153 { 0xE017000000000000LL
, 16, "KSW Microtec GmbH Germany" },
154 { 0xE018000000000000LL
, 16, "ZMD AG Germany" },
155 { 0xE019000000000000LL
, 16, "XICOR, Inc. USA" },
156 { 0xE01A000000000000LL
, 16, "Sony Corporation Japan Identifier Company Country" },
157 { 0xE01B000000000000LL
, 16, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
158 { 0xE01C000000000000LL
, 16, "Emosyn USA" },
159 { 0xE01D000000000000LL
, 16, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
160 { 0xE01E000000000000LL
, 16, "Magellan Technology Pty Limited Australia" },
161 { 0xE01F000000000000LL
, 16, "Melexis NV BO Switzerland" },
162 { 0xE020000000000000LL
, 16, "Renesas Technology Corp. Japan" },
163 { 0xE021000000000000LL
, 16, "TAGSYS France" },
164 { 0xE022000000000000LL
, 16, "Transcore USA" },
165 { 0xE023000000000000LL
, 16, "Shanghai belling corp., ltd. China" },
166 { 0xE024000000000000LL
, 16, "Masktech Germany Gmbh Germany" },
167 { 0xE025000000000000LL
, 16, "Innovision Research and Technology Plc UK" },
168 { 0xE026000000000000LL
, 16, "Hitachi ULSI Systems Co., Ltd. Japan" },
169 { 0xE027000000000000LL
, 16, "Cypak AB Sweden" },
170 { 0xE028000000000000LL
, 16, "Ricoh Japan" },
171 { 0xE029000000000000LL
, 16, "ASK France" },
172 { 0xE02A000000000000LL
, 16, "Unicore Microsystems, LLC Russian Federation" },
173 { 0xE02B000000000000LL
, 16, "Dallas Semiconductor/Maxim USA" },
174 { 0xE02C000000000000LL
, 16, "Impinj, Inc. USA" },
175 { 0xE02D000000000000LL
, 16, "RightPlug Alliance USA" },
176 { 0xE02E000000000000LL
, 16, "Broadcom Corporation USA" },
177 { 0xE02F000000000000LL
, 16, "MStar Semiconductor, Inc Taiwan, ROC" },
178 { 0xE030000000000000LL
, 16, "BeeDar Technology Inc. USA" },
179 { 0xE031000000000000LL
, 16, " RFIDsec Denmark" },
180 { 0xE032000000000000LL
, 16, " Schweizer Electronic AG Germany" },
181 { 0xE033000000000000LL
, 16, " AMIC Technology Corp Taiwan" },
182 { 0xE034000000000000LL
, 16, "Mikron JSC Russia" },
183 { 0xE035000000000000LL
, 16, "Fraunhofer Institute for Photonic Microsystems Germany" },
184 { 0xE036000000000000LL
, 16, "IDS Microchip AG Switzerland" },
185 { 0xE037000000000000LL
, 16, "Kovio USA" },
186 { 0xE038000000000000LL
, 16, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
187 { 0xE039000000000000LL
, 16, "Silicon Craft Technology Thailand" },
188 { 0xE03A000000000000LL
, 16, "Advanced Film Device Inc. Japan" },
189 { 0xE03B000000000000LL
, 16, "Nitecrest Ltd UK" },
190 { 0xE03C000000000000LL
, 16, "Verayo Inc. USA" },
191 { 0xE03D000000000000LL
, 16, "HID Global USA" },
192 { 0xE03E000000000000LL
, 16, "Productivity Engineering Gmbh Germany" },
193 { 0xE03F000000000000LL
, 16, "Austriamicrosystems AG (reserved) Austria" },
194 { 0xE040000000000000LL
, 16, "Gemalto SA France" },
195 { 0xE041000000000000LL
, 16, "Renesas Electronics Corporation Japan" },
196 { 0xE042000000000000LL
, 16, "3Alogics Inc Korea" },
197 { 0xE043000000000000LL
, 16, "Top TroniQ Asia Limited Hong Kong" },
198 { 0xE044000000000000LL
, 16, "Gentag Inc (USA) USA" },
199 { 0,0,"no tag-info available" } // must be the last entry
203 // fast method to just read the UID of a tag (collission detection not supported)
204 // *buf should be large enough to fit the 64bit uid
205 // returns 1 if suceeded
206 int getUID(uint8_t *buf
)
210 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
211 uint8_t *req
=c
.d
.asBytes
;
214 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
216 req
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
217 req
[1] = ISO15693_INVENTORY
;
218 req
[2] = 0; // mask length
219 reqlen
= AddCrc(req
,3);
224 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
225 recv
= resp
.d
.asBytes
;
226 if (resp
.arg
[0]>=12 && ISO15693_CRC_CHECK
==Crc(recv
,12)) {
227 memcpy(buf
,&recv
[2],8);
237 // get a product description based on the UID
239 // returns description of the best match
240 static char* getTagInfo(uint8_t *uid
) {
243 memcpy(&myuid
,uid
,sizeof(uint64_t));
244 while (uidmapping
[i
].mask
>0) {
245 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
246 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
250 if (uidmapping
[i
].mask
>uidmapping
[best
].mask
) {
258 if (best
>=0) return uidmapping
[best
].desc
;
260 return uidmapping
[i
].desc
;
264 // return a clear-text message to an errorcode
265 static char* TagErrorStr(uint8_t error
) {
267 case 0x01: return "The command is not supported";
268 case 0x02: return "The command is not recognised";
269 case 0x03: return "The option is not supported.";
270 case 0x0f: return "Unknown error.";
271 case 0x10: return "The specified block is not available (doesn't exist).";
272 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
273 case 0x12: return "The specified block is locked and its content cannot be changed.";
274 case 0x13: return "The specified block was not successfully programmed.";
275 case 0x14: return "The specified block was not successfully locked.";
276 default: return "Reserved for Future Use or Custom command error.";
282 int CmdHF15Demod(const char *Cmd
)
284 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
287 int max
= 0, maxPos
= 0;
291 if (GraphTraceLen
< 2000) return 0;
293 // First, correlate for SOF
294 for (i
= 0; i
< 200; i
++) {
296 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
297 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
304 PrintAndLog("SOF at %d, correlation %d", maxPos
,
305 max
/ (arraylen(FrameSOF
) / skip
));
307 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
310 memset(outBuf
, 0, sizeof(outBuf
));
313 int corr0
= 0, corr00
= 0, corr01
= 0, corr1
= 0, corrEOF
= 0;
314 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
315 corr0
+= Logic0
[j
]*GraphBuffer
[i
+(j
/skip
)];
317 corr01
= corr00
= corr0
;
318 for(j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
319 corr00
+= Logic0
[j
]*GraphBuffer
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
320 corr01
+= Logic1
[j
]*GraphBuffer
[i
+arraylen(Logic0
)/skip
+(j
/skip
)];
322 for(j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
323 corr1
+= Logic1
[j
]*GraphBuffer
[i
+(j
/skip
)];
325 for(j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
326 corrEOF
+= FrameEOF
[j
]*GraphBuffer
[i
+(j
/skip
)];
328 // Even things out by the length of the target waveform.
334 if(corrEOF
> corr1
&& corrEOF
> corr00
&& corrEOF
> corr01
) {
335 PrintAndLog("EOF at %d", i
);
337 } else if (corr1
> corr0
) {
338 i
+= arraylen(Logic1
) / skip
;
341 i
+= arraylen(Logic0
) / skip
;
348 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
349 PrintAndLog("ran off end!");
354 PrintAndLog("error, uneven octet! (discard extra bits!)");
355 PrintAndLog(" mask=%02x", mask
);
357 PrintAndLog("%d octets", k
);
359 for (i
= 0; i
< k
; i
++) {
360 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
362 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
368 // * Acquire Samples as Reader (enables carrier, sends inquiry)
369 int CmdHF15Read(const char *Cmd
)
371 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
376 // Record Activity without enabling carrier
377 // TODO: currently it DOES enable the carrier
378 int CmdHF15Record(const char *Cmd
)
380 UsbCommand c
= {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693
};
385 int HF15Reader(const char *Cmd
, bool verbose
)
390 if (verbose
) PrintAndLog("No Tag found.");
394 PrintAndLog("Tag UID : %s",sprintUID(NULL
,uid
));
395 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
399 int CmdHF15Reader(const char *Cmd
)
401 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
406 // Simulation is still not working very good
407 int CmdHF15Sim(const char *Cmd
)
409 char cmdp
= param_getchar(Cmd
, 0);
410 uint8_t uid
[8] = {0x00};
412 //E0 16 24 00 00 00 00 00
413 if (cmdp
== 'h' || cmdp
== 'H') {
414 PrintAndLog("Usage: hf 15 sim <UID>");
416 PrintAndLog(" sample: hf 15 sim E016240000000000");
420 if (param_gethex(Cmd
, 0, uid
, 16)) {
421 PrintAndLog("UID must include 16 HEX symbols");
425 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
426 uid
[0],uid
[1],uid
[2],uid
[3],uid
[4], uid
[5], uid
[6], uid
[7]);
427 PrintAndLog("Press the button to stop simulation");
429 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {0, 0, 0}};
430 memcpy(c
.d
.asBytes
,uid
,8);
436 // finds the AFI (Application Family Idendifier) of a card, by trying all values
437 // (There is no standard way of reading the AFI, allthough some tags support this)
438 int CmdHF15Afi(const char *Cmd
)
440 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
445 // Reads all memory pages
446 int CmdHF15DumpMem(const char*Cmd
) {
450 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
451 uint8_t *req
=c
.d
.asBytes
;
457 PrintAndLog("No Tag found.");
461 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
462 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
464 for (int retry
=0; retry
<5; retry
++) {
466 req
[0]= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
467 req
[1] = ISO15693_READBLOCK
;
468 memcpy(&req
[2],uid
,8);
470 reqlen
= AddCrc(req
,11);
475 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
476 recv
= resp
.d
.asBytes
;
477 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
478 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
480 *output
=0; // reset outputstring
481 sprintf(output
, "Block %02x ",blocknum
);
482 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in hex
483 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
486 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in cleaned ascii
487 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
489 PrintAndLog("%s",output
);
491 // PrintAndLog("bn=%i",blocknum);
493 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
496 } // else PrintAndLog("crc");
497 } // else PrintAndLog("r null");
500 // if (resp.arg[0]<3)
501 // PrintAndLog("Lost Connection");
502 // else if (ISO15693_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
503 // PrintAndLog("CRC Failed");
505 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
512 static command_t CommandTable15
[] =
514 {"help", CmdHF15Help
, 1, "This help"},
515 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
516 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
517 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
518 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
519 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
520 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
521 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
522 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
523 {NULL
, NULL
, 0, NULL
}
526 int CmdHF15(const char *Cmd
)
528 CmdsParse(CommandTable15
, Cmd
);
532 int CmdHF15Help(const char *Cmd
)
534 CmdsHelp(CommandTable15
);
539 // "HF 15 Cmd" Interface
540 // Allows direct communication with the tag on command level
542 int CmdHF15CmdInquiry(const char *Cmd
)
546 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
547 uint8_t *req
=c
.d
.asBytes
;
550 req
[0] = ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_INVENTORY
| ISO15693_REQINV_SLOT1
;
551 req
[1] = ISO15693_INVENTORY
;
552 req
[2] = 0; // mask length
553 reqlen
=AddCrc(req
,3);
558 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
559 if (resp
.arg
[0]>=12) {
560 recv
= resp
.d
.asBytes
;
561 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
562 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
564 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp
.arg
[0]);
567 PrintAndLog("timeout.");
573 // Turns debugging on(1)/off(0)
574 int CmdHF15CmdDebug( const char *cmd
) {
575 int debug
= atoi(cmd
);
576 if (strlen(cmd
) < 1) {
577 PrintAndLog("Usage: hf 15 debug <0|1>");
578 PrintAndLog(" 0 no debugging");
579 PrintAndLog(" 1 turn debugging on");
583 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
589 int CmdHF15CmdRaw (const char *cmd
) {
592 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
599 unsigned int datalen
=0, temp
;
604 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
605 PrintAndLog(" -r do not read response");
606 PrintAndLog(" -2 use slower '1 out of 256' mode");
607 PrintAndLog(" -c calculate and append CRC");
608 PrintAndLog(" Tip: turn on debugging for verbose output");
613 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
615 while (cmd
[i
]!='\0') {
616 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
631 PrintAndLog("Invalid option");
637 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
638 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
639 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
640 buf
[strlen(buf
)+1]=0;
641 buf
[strlen(buf
)]=cmd
[i
];
644 if (strlen(buf
)>=2) {
645 sscanf(buf
,"%x",&temp
);
646 data
[datalen
]=(uint8_t)(temp
& 0xff);
652 PrintAndLog("Invalid char on input");
655 if (crc
) datalen
=AddCrc(data
,datalen
);
660 memcpy(c
.d
.asBytes
,data
,datalen
);
665 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
666 recv
= resp
.d
.asBytes
;
667 PrintAndLog("received %i octets",resp
.arg
[0]);
668 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
669 if (hexout
!= NULL
) {
670 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
671 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
673 PrintAndLog("%s", hexout
);
677 PrintAndLog("timeout while waiting for reply.");
686 * parses common HF 15 CMD parameters and prepares some data structures
690 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
692 uint8_t *req
=c
->d
.asBytes
;
693 uint8_t uid
[8] = {0x00};
697 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
699 if (strstr(*cmd
,"-2")==*cmd
) {
700 c
->arg
[1]=0; // use 1of256
705 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
707 if (strstr(*cmd
,"-o")==*cmd
) {
708 req
[reqlen
]=ISO15693_REQ_OPTION
;
713 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
717 PrintAndLog("missing addr");
722 // you must have selected the tag earlier
723 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_SELECT
;
724 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
725 reqlen
+= iso15cmdlen
;
729 // unaddressed mode may not be supported by all vendors
730 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
;
731 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
732 reqlen
+= iso15cmdlen
;
735 // we scan for the UID ourself
736 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
737 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
740 PrintAndLog("No Tag found");
743 memcpy(req
+reqlen
,uid
,8);
744 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
748 req
[reqlen
++] |= ISO15693_REQ_DATARATE_HIGH
| ISO15693_REQ_ADDRESS
;
749 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
752 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
753 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
754 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
755 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
756 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
757 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
758 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
762 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
763 memcpy(&req
[reqlen
],&uid
[0],8);
766 // skip to next space
767 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
768 // skip over the space
769 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
776 * Commandline handling: HF15 CMD SYSINFO
777 * get system information from tag/VICC
779 int CmdHF15CmdSysinfo(const char *Cmd
) {
782 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
783 uint8_t *req
=c
.d
.asBytes
;
787 char output
[2048]="";
794 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
795 PrintAndLog(" options:");
796 PrintAndLog(" -2 use slower '1 out of 256' mode");
797 PrintAndLog(" uid (either): ");
798 PrintAndLog(" <8B hex> full UID eg E011223344556677");
799 PrintAndLog(" s selected tag");
800 PrintAndLog(" u unaddressed mode");
801 PrintAndLog(" * scan for tag");
802 PrintAndLog(" start#: page number to start 0-255");
803 PrintAndLog(" count#: number of pages");
807 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_GET_SYSTEM_INFO
},1);
810 reqlen
=AddCrc(req
,reqlen
);
815 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
816 recv
= resp
.d
.asBytes
;
817 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
818 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
819 *output
=0; // reset outputstring
820 for ( i
=1; i
<resp
.arg
[0]-2; i
++) {
821 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
823 strcat(output
,"\n\r");
824 strcat(output
,"UID = ");
825 strcat(output
,sprintUID(NULL
,recv
+2));
826 strcat(output
,"\n\r");
827 strcat(output
,getTagInfo(recv
+2)); //ABC
828 strcat(output
,"\n\r");
831 sprintf(output
+strlen(output
),"DSFID supported, set to %02X\n\r",recv
[i
++]);
833 strcat(output
,"DSFID not supported\n\r");
835 sprintf(output
+strlen(output
),"AFI supported, set to %03X\n\r",recv
[i
++]);
837 strcat(output
,"AFI not supported\n\r");
838 if (recv
[1] & 0x04) {
839 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
840 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
841 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
844 strcat(output
,"Tag does not provide information on memory layout\n\r");
845 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02X\n\r",recv
[i
++]);
846 else strcat(output
,"IC reference not given\n\r");
849 PrintAndLog("%s",output
);
851 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
854 PrintAndLog("CRC failed");
857 PrintAndLog("timeout: no answer");
864 * Commandline handling: HF15 CMD READMULTI
865 * Read multiple blocks at once (not all tags support this)
867 int CmdHF15CmdReadmulti(const char *Cmd
) {
870 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
871 uint8_t *req
=c
.d
.asBytes
;
872 int reqlen
=0, pagenum
,pagecount
;
875 char output
[2048]="";
881 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
882 PrintAndLog(" options:");
883 PrintAndLog(" -2 use slower '1 out of 256' mode");
884 PrintAndLog(" uid (either): ");
885 PrintAndLog(" <8B hex> full UID eg E011223344556677");
886 PrintAndLog(" s selected tag");
887 PrintAndLog(" u unaddressed mode");
888 PrintAndLog(" * scan for tag");
889 PrintAndLog(" start#: page number to start 0-255");
890 PrintAndLog(" count#: number of pages");
894 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_READ_MULTI_BLOCK
},1);
897 pagenum
=strtol(cmd
,NULL
,0);
899 // skip to next space
900 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
901 // skip over the space
902 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
904 pagecount
=strtol(cmd
,NULL
,0);
905 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
907 req
[reqlen
++]=(uint8_t)pagenum
;
908 req
[reqlen
++]=(uint8_t)pagecount
;
910 reqlen
=AddCrc(req
,reqlen
);
916 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
917 recv
= resp
.d
.asBytes
;
918 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
919 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
920 *output
=0; // reset outputstring
921 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
922 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
925 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
926 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
928 PrintAndLog("%s",output
);
930 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
933 PrintAndLog("CRC failed");
936 PrintAndLog("no answer");
943 * Commandline handling: HF15 CMD READ
944 * Reads a single Block
946 int CmdHF15CmdRead(const char *Cmd
) {
949 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
950 uint8_t *req
=c
.d
.asBytes
;
951 int reqlen
=0, pagenum
;
960 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
961 PrintAndLog(" options:");
962 PrintAndLog(" -2 use slower '1 out of 256' mode");
963 PrintAndLog(" uid (either): ");
964 PrintAndLog(" <8B hex> full UID eg E011223344556677");
965 PrintAndLog(" s selected tag");
966 PrintAndLog(" u unaddressed mode");
967 PrintAndLog(" * scan for tag");
968 PrintAndLog(" page#: page number 0-255");
972 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_READBLOCK
},1);
975 pagenum
=strtol(cmd
,NULL
,0);
977 PrintAndLog("invalid pagenum");
981 req
[reqlen
++]=(uint8_t)pagenum
;
983 reqlen
=AddCrc(req
,reqlen
);
989 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
990 recv
= resp
.d
.asBytes
;
991 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
992 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
993 *output
=0; // reset outputstring
994 //sprintf(output, "Block %2i ",blocknum);
995 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
996 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
999 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
1000 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
1002 PrintAndLog("%s",output
);
1004 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1007 PrintAndLog("CRC failed");
1010 PrintAndLog("no answer");
1018 * Commandline handling: HF15 CMD WRITE
1019 * Writes a single Block - might run into timeout, even when successful
1021 int CmdHF15CmdWrite(const char *Cmd
) {
1024 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
1025 uint8_t *req
=c
.d
.asBytes
;
1026 int reqlen
=0, pagenum
, temp
;
1031 strncpy(cmd
,Cmd
,99);
1034 if (strlen(cmd
)<3) {
1035 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
1036 PrintAndLog(" options:");
1037 PrintAndLog(" -2 use slower '1 out of 256' mode");
1038 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1039 PrintAndLog(" uid (either): ");
1040 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1041 PrintAndLog(" s selected tag");
1042 PrintAndLog(" u unaddressed mode");
1043 PrintAndLog(" * scan for tag");
1044 PrintAndLog(" page#: page number 0-255");
1045 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1049 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15693_WRITEBLOCK
},1);
1052 // *cmd -> page num ; *cmd2 -> data
1054 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
1058 pagenum
=strtol(cmd
,NULL
,0);
1060 PrintAndLog("invalid pagenum");
1063 req
[reqlen
++]=(uint8_t)pagenum
;
1066 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
1071 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
1072 req
[reqlen
++]=temp
& 0xff;
1076 reqlen
=AddCrc(req
,reqlen
);
1082 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000) && resp
.arg
[0]>2) {
1083 recv
= resp
.d
.asBytes
;
1084 if (ISO15693_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
1085 if (!(recv
[0] & ISO15693_RES_ERROR
)) {
1088 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1091 PrintAndLog("CRC failed");
1094 PrintAndLog("timeout: no answer - data may be written anyway");
1102 static command_t CommandTable15Cmd
[] =
1104 {"help", CmdHF15CmdHelp
, 1, "This Help"},
1105 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
1107 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1109 {"read", CmdHF15CmdRead
, 0, "Read a block"},
1110 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
1111 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
1112 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
1113 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
1114 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
1115 {NULL
, NULL
, 0, NULL
}
1118 int CmdHF15Cmd(const char *Cmd
)
1120 CmdsParse(CommandTable15Cmd
, Cmd
);
1124 int CmdHF15CmdHelp(const char *Cmd
)
1126 CmdsHelp(CommandTable15Cmd
);