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.
30 #include "proxmark3.h"
35 #include "cmdparser.h"
37 #include "../common/iso15693tools.h"
40 #define FrameSOF Iso15693FrameSOF
41 #define Logic0 Iso15693Logic0
42 #define Logic1 Iso15693Logic1
43 #define FrameEOF Iso15693FrameEOF
45 #define Crc(data,datalen) Iso15693Crc(data,datalen)
46 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
47 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
48 #define TRACE_BUFF_SIZE 12000
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? no 'read', just 'readmulti'" },
138 { 0xE0160c0000000000LL
, 24, "EM-Marin SA; EM4035?" },
139 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
140 { 0xE016940000000000LL
, 24, "EM-Marin SA (Skidata); 51x64bit" },
142 { 0xE017000000000000LL
, 16, "KSW Microtec GmbH Germany" },
143 { 0xE018000000000000LL
, 16, "ZMD AG Germany" },
144 { 0xE019000000000000LL
, 16, "XICOR, Inc. USA" },
145 { 0xE01A000000000000LL
, 16, "Sony Corporation Japan Identifier Company Country" },
146 { 0xE01B000000000000LL
, 16, "Malaysia Microelectronic Solutions Sdn. Bhd Malaysia" },
147 { 0xE01C000000000000LL
, 16, "Emosyn USA" },
148 { 0xE01D000000000000LL
, 16, "Shanghai Fudan Microelectronics Co. Ltd. P.R. China" },
149 { 0xE01E000000000000LL
, 16, "Magellan Technology Pty Limited Australia" },
150 { 0xE01F000000000000LL
, 16, "Melexis NV BO Switzerland" },
151 { 0xE020000000000000LL
, 16, "Renesas Technology Corp. Japan" },
152 { 0xE021000000000000LL
, 16, "TAGSYS France" },
153 { 0xE022000000000000LL
, 16, "Transcore USA" },
154 { 0xE023000000000000LL
, 16, "Shanghai belling corp., ltd. China" },
155 { 0xE024000000000000LL
, 16, "Masktech Germany Gmbh Germany" },
156 { 0xE025000000000000LL
, 16, "Innovision Research and Technology Plc UK" },
157 { 0xE026000000000000LL
, 16, "Hitachi ULSI Systems Co., Ltd. Japan" },
158 { 0xE027000000000000LL
, 16, "Cypak AB Sweden" },
159 { 0xE028000000000000LL
, 16, "Ricoh Japan" },
160 { 0xE029000000000000LL
, 16, "ASK France" },
161 { 0xE02A000000000000LL
, 16, "Unicore Microsystems, LLC Russian Federation" },
162 { 0xE02B000000000000LL
, 16, "Dallas Semiconductor/Maxim USA" },
163 { 0xE02C000000000000LL
, 16, "Impinj, Inc. USA" },
164 { 0xE02D000000000000LL
, 16, "RightPlug Alliance USA" },
165 { 0xE02E000000000000LL
, 16, "Broadcom Corporation USA" },
166 { 0xE02F000000000000LL
, 16, "MStar Semiconductor, Inc Taiwan, ROC" },
167 { 0xE030000000000000LL
, 16, "BeeDar Technology Inc. USA" },
168 { 0xE031000000000000LL
, 16, " RFIDsec Denmark" },
169 { 0xE032000000000000LL
, 16, " Schweizer Electronic AG Germany" },
170 { 0xE033000000000000LL
, 16, " AMIC Technology Corp Taiwan" },
171 { 0xE034000000000000LL
, 16, "Mikron JSC Russia" },
172 { 0xE035000000000000LL
, 16, "Fraunhofer Institute for Photonic Microsystems Germany" },
173 { 0xE036000000000000LL
, 16, "IDS Microchip AG Switzerland" },
174 { 0xE037000000000000LL
, 16, "Kovio USA" },
175 { 0xE038000000000000LL
, 16, "HMT Microelectronic Ltd Switzerland Identifier Company Country" },
176 { 0xE039000000000000LL
, 16, "Silicon Craft Technology Thailand" },
177 { 0xE03A000000000000LL
, 16, "Advanced Film Device Inc. Japan" },
178 { 0xE03B000000000000LL
, 16, "Nitecrest Ltd UK" },
179 { 0xE03C000000000000LL
, 16, "Verayo Inc. USA" },
180 { 0xE03D000000000000LL
, 16, "HID Global USA" },
181 { 0xE03E000000000000LL
, 16, "Productivity Engineering Gmbh Germany" },
182 { 0xE03F000000000000LL
, 16, "Austriamicrosystems AG (reserved) Austria" },
183 { 0xE040000000000000LL
, 16, "Gemalto SA France" },
184 { 0xE041000000000000LL
, 16, "Renesas Electronics Corporation Japan" },
185 { 0xE042000000000000LL
, 16, "3Alogics Inc Korea" },
186 { 0xE043000000000000LL
, 16, "Top TroniQ Asia Limited Hong Kong" },
187 { 0xE044000000000000LL
, 16, "Gentag Inc (USA) USA" },
188 { 0,0,"no tag-info available" } // must be the last entry
192 // fast method to just read the UID of a tag (collission detection not supported)
193 // *buf should be large enough to fit the 64bit uid
194 // returns 1 if suceeded
195 int getUID(uint8_t *buf
)
199 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
200 uint8_t *req
=c
.d
.asBytes
;
203 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
205 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
206 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
207 req
[1]=ISO15_CMD_INVENTORY
;
208 req
[2]=0; // mask length
209 reqlen
=AddCrc(req
,3);
214 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
215 recv
= resp
.d
.asBytes
;
216 if (resp
.arg
[0]>=12 && ISO15_CRC_CHECK
==Crc(recv
,12)) {
217 memcpy(buf
,&recv
[2],8);
227 // get a product description based on the UID
229 // returns description of the best match
230 static char* getTagInfo(uint8_t *uid
) {
233 memcpy(&myuid
,uid
,sizeof(uint64_t));
234 while (uidmapping
[i
].mask
>0) {
235 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
236 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
240 if (uidmapping
[i
].mask
> uidmapping
[best
].mask
) {
248 if (best
>=0) return uidmapping
[best
].desc
;
250 return uidmapping
[i
].desc
;
254 // return a clear-text message to an errorcode
255 static char* TagErrorStr(uint8_t error
) {
257 case 0x01: return "The command is not supported";
258 case 0x02: return "The command is not recognised";
259 case 0x03: return "The option is not supported.";
260 case 0x0f: return "Unknown error.";
261 case 0x10: return "The specified block is not available (doesn’t exist).";
262 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
263 case 0x12: return "The specified block is locked and its content cannot be changed.";
264 case 0x13: return "The specified block was not successfully programmed.";
265 case 0x14: return "The specified block was not successfully locked.";
266 default: return "Reserved for Future Use or Custom command error.";
272 int CmdHF15Demod(const char *Cmd
)
274 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
277 int max
= 0, maxPos
= 0;
281 if (GraphTraceLen
< 1000) return 0;
283 // First, correlate for SOF
284 for (i
= 0; i
< 100; i
++) {
286 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
287 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
294 PrintAndLog("SOF at %d, correlation %d", maxPos
,
295 max
/ (arraylen(FrameSOF
) / skip
));
297 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
300 memset(outBuf
, 0, sizeof(outBuf
));
303 int corr0
= 0, corr1
= 0, corrEOF
= 0;
304 for (j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
305 corr0
+= Logic0
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
307 for (j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
308 corr1
+= Logic1
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
310 for (j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
311 corrEOF
+= FrameEOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
313 // Even things out by the length of the target waveform.
317 if (corrEOF
> corr1
&& corrEOF
> corr0
) {
318 PrintAndLog("EOF at %d", i
);
320 } else if (corr1
> corr0
) {
321 i
+= arraylen(Logic1
) / skip
;
324 i
+= arraylen(Logic0
) / skip
;
331 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
332 PrintAndLog("ran off end!");
337 PrintAndLog("error, uneven octet! (discard extra bits!)");
338 PrintAndLog(" mask=%02x", mask
);
340 PrintAndLog("%d octets", k
);
342 for (i
= 0; i
< k
; i
++) {
343 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
345 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
351 // * Acquire Samples as Reader (enables carrier, sends inquiry)
352 int CmdHF15Read(const char *Cmd
)
354 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
357 uint8_t data
[TRACE_BUFF_SIZE
] = {0x00};
359 GetFromBigBuf(data
,TRACE_BUFF_SIZE
,3560); //3560 -- should be offset..
360 WaitForResponseTimeout(CMD_ACK
,NULL
, 1500);
362 for (int j
= 0; j
< TRACE_BUFF_SIZE
; j
++) {
363 GraphBuffer
[j
] = ((int)data
[j
]) ;
365 GraphTraceLen
= TRACE_BUFF_SIZE
;
366 RepaintGraphWindow();
370 // Record Activity without enabeling carrier
371 int CmdHF15Record(const char *Cmd
)
373 UsbCommand c
= {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693
};
376 uint8_t data
[TRACE_BUFF_SIZE
] = {0x00};
378 GetFromBigBuf(data
,TRACE_BUFF_SIZE
,3560); //3560 -- should be offset..
379 WaitForResponseTimeout(CMD_ACK
,NULL
, 1500);
381 for (int j
= 0; j
< TRACE_BUFF_SIZE
; j
++) {
382 GraphBuffer
[j
] = ((int)data
[j
]) ;
384 GraphTraceLen
= TRACE_BUFF_SIZE
;
385 RepaintGraphWindow();
389 int CmdHF15Reader(const char *Cmd
)
391 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
396 // Simulation is still not working very good
397 int CmdHF15Sim(const char *Cmd
)
399 char cmdp
= param_getchar(Cmd
, 0);
400 uint8_t uid
[8] = {0x00};
402 //E0 16 24 00 00 00 00 00
403 if (cmdp
== 'h' || cmdp
== 'H') {
404 PrintAndLog("Usage: hf 15 sim <UID>");
406 PrintAndLog(" sample: hf 15 sim E016240000000000");
410 if (param_gethex(Cmd
, 0, uid
, 16)) {
411 PrintAndLog("UID must include 16 HEX symbols");
415 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
416 uid
[0],uid
[1],uid
[2],uid
[3],uid
[4], uid
[5], uid
[6], uid
[7]);
418 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {0, 0, 0}};
419 memcpy(c
.d
.asBytes
,uid
,8);
425 // finds the AFI (Application Family Idendifier) of a card, by trying all values
426 // (There is no standard way of reading the AFI, allthough some tags support this)
427 int CmdHF15Afi(const char *Cmd
)
429 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
434 // Reads all memory pages
435 int CmdHF15DumpMem(const char*Cmd
) {
439 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
440 uint8_t *req
=c
.d
.asBytes
;
446 PrintAndLog("No Tag found.");
450 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
451 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
453 for (int retry
=0; retry
<5; retry
++) {
455 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
456 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
457 req
[1]=ISO15_CMD_READ
;
458 memcpy(&req
[2],uid
,8);
460 reqlen
=AddCrc(req
,11);
465 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
466 recv
= resp
.d
.asBytes
;
467 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
468 if (!(recv
[0] & ISO15_RES_ERROR
)) {
470 *output
=0; // reset outputstring
471 sprintf(output
, "Block %02x ",blocknum
);
472 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in hex
473 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
476 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in cleaned ascii
477 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
479 PrintAndLog("%s",output
);
481 // PrintAndLog("bn=%i",blocknum);
483 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
486 } // else PrintAndLog("crc");
487 } // else PrintAndLog("r null");
490 // if (resp.arg[0]<3)
491 // PrintAndLog("Lost Connection");
492 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
493 // PrintAndLog("CRC Failed");
495 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
502 static command_t CommandTable15
[] =
504 {"help", CmdHF15Help
, 1, "This help"},
505 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
506 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
507 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
508 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
509 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
510 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
511 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
512 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
513 {NULL
, NULL
, 0, NULL
}
516 int CmdHF15(const char *Cmd
)
518 CmdsParse(CommandTable15
, Cmd
);
522 int CmdHF15Help(const char *Cmd
)
524 CmdsHelp(CommandTable15
);
529 // "HF 15 Cmd" Interface
530 // Allows direct communication with the tag on command level
532 int CmdHF15CmdInquiry(const char *Cmd
)
536 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
537 uint8_t *req
=c
.d
.asBytes
;
540 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
541 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
542 req
[1]=ISO15_CMD_INVENTORY
;
543 req
[2]=0; // mask length
544 reqlen
=AddCrc(req
,3);
549 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
550 if (resp
.arg
[0]>=12) {
551 recv
= resp
.d
.asBytes
;
552 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
553 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
555 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp
.arg
[0]);
558 PrintAndLog("timeout.");
564 // Turns debugging on(1)/off(0)
565 int CmdHF15CmdDebug( const char *cmd
) {
568 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
569 PrintAndLog(" 0 no debugging");
570 PrintAndLog(" 1 turn debugging on");
574 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
580 int CmdHF15CmdRaw (const char *cmd
) {
583 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
590 unsigned int datalen
=0, temp
;
595 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
596 PrintAndLog(" -r do not read response");
597 PrintAndLog(" -2 use slower '1 out of 256' mode");
598 PrintAndLog(" -c calculate and append CRC");
599 PrintAndLog(" Tip: turn on debugging for verbose output");
604 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
606 while (cmd
[i
]!='\0') {
607 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
622 PrintAndLog("Invalid option");
628 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
629 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
630 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
631 buf
[strlen(buf
)+1]=0;
632 buf
[strlen(buf
)]=cmd
[i
];
635 if (strlen(buf
)>=2) {
636 sscanf(buf
,"%x",&temp
);
637 data
[datalen
]=(uint8_t)(temp
& 0xff);
643 PrintAndLog("Invalid char on input");
646 if (crc
) datalen
=AddCrc(data
,datalen
);
651 memcpy(c
.d
.asBytes
,data
,datalen
);
656 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
657 recv
= resp
.d
.asBytes
;
658 PrintAndLog("received %i octets",resp
.arg
[0]);
659 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
660 if (hexout
!= NULL
) {
661 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
662 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
664 PrintAndLog("%s", hexout
);
668 PrintAndLog("timeout while waiting for reply.");
677 * parses common HF 15 CMD parameters and prepares some data structures
681 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
683 uint8_t *req
= c
->d
.asBytes
;
684 uint8_t uid
[8] = {0x00};
688 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
690 if (strstr(*cmd
,"-2")==*cmd
) {
691 c
->arg
[1]=0; // use 1of256
696 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
698 if (strstr(*cmd
,"-o")==*cmd
) {
699 req
[reqlen
]=ISO15_REQ_OPTION
;
704 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
708 PrintAndLog("missing addr");
713 // you must have selected the tag earlier
714 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
715 ISO15_REQ_NONINVENTORY
| ISO15_REQ_SELECT
;
716 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
721 // unaddressed mode may not be supported by all vendors
722 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
723 ISO15_REQ_NONINVENTORY
;
724 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
728 // we scan for the UID ourself
729 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
730 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
731 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
734 PrintAndLog("No Tag found");
737 memcpy(req
+reqlen
,uid
,8);
738 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
742 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
743 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
744 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
747 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
748 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
749 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
750 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
751 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
752 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
753 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
757 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
758 memcpy(&req
[reqlen
],&uid
[0],8);
761 // skip to next space
762 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
763 // skip over the space
764 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
771 * Commandline handling: HF15 CMD SYSINFO
772 * get system information from tag/VICC
774 int CmdHF15CmdSysinfo(const char *Cmd
) {
777 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
778 uint8_t *req
=c
.d
.asBytes
;
782 char output
[2048]="";
789 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
790 PrintAndLog(" options:");
791 PrintAndLog(" -2 use slower '1 out of 256' mode");
792 PrintAndLog(" uid (either): ");
793 PrintAndLog(" <8B hex> full UID eg E011223344556677");
794 PrintAndLog(" s selected tag");
795 PrintAndLog(" u unaddressed mode");
796 PrintAndLog(" * scan for tag");
797 PrintAndLog(" start#: page number to start 0-255");
798 PrintAndLog(" count#: number of pages");
802 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_SYSINFO
},1);
805 reqlen
=AddCrc(req
,reqlen
);
810 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
811 recv
= resp
.d
.asBytes
;
812 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
813 if (!(recv
[0] & ISO15_RES_ERROR
)) {
814 *output
=0; // reset outputstring
815 for ( i
=1; i
<resp
.arg
[0]-2; i
++) {
816 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
818 strcat(output
,"\n\r");
819 strcat(output
,"UID = ");
820 strcat(output
,sprintUID(NULL
,recv
+2));
821 strcat(output
,"\n\r");
822 strcat(output
,getTagInfo(recv
+2)); //ABC
823 strcat(output
,"\n\r");
826 sprintf(output
+strlen(output
),"DSFID supported, set to %02X\n\r",recv
[i
++]);
828 strcat(output
,"DSFID not supported\n\r");
830 sprintf(output
+strlen(output
),"AFI supported, set to %03X\n\r",recv
[i
++]);
832 strcat(output
,"AFI not supported\n\r");
833 if (recv
[1] & 0x04) {
834 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
835 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
836 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
839 strcat(output
,"Tag does not provide information on memory layout\n\r");
840 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02X\n\r",recv
[i
++]);
841 else strcat(output
,"IC reference not given\n\r");
844 PrintAndLog("%s",output
);
846 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
849 PrintAndLog("CRC failed");
852 PrintAndLog("timeout: no answer");
859 * Commandline handling: HF15 CMD READMULTI
860 * Read multiple blocks at once (not all tags support this)
862 int CmdHF15CmdReadmulti(const char *Cmd
) {
865 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
866 uint8_t *req
=c
.d
.asBytes
;
867 int reqlen
=0, pagenum
,pagecount
;
870 char output
[2048]="";
876 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
877 PrintAndLog(" options:");
878 PrintAndLog(" -2 use slower '1 out of 256' mode");
879 PrintAndLog(" uid (either): ");
880 PrintAndLog(" <8B hex> full UID eg E011223344556677");
881 PrintAndLog(" s selected tag");
882 PrintAndLog(" u unaddressed mode");
883 PrintAndLog(" * scan for tag");
884 PrintAndLog(" start#: page number to start 0-255");
885 PrintAndLog(" count#: number of pages");
889 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READMULTI
},1);
892 pagenum
=strtol(cmd
,NULL
,0);
894 // skip to next space
895 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
896 // skip over the space
897 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
899 pagecount
=strtol(cmd
,NULL
,0);
900 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
902 req
[reqlen
++]=(uint8_t)pagenum
;
903 req
[reqlen
++]=(uint8_t)pagecount
;
905 reqlen
=AddCrc(req
,reqlen
);
911 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
912 recv
= resp
.d
.asBytes
;
913 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
914 if (!(recv
[0] & ISO15_RES_ERROR
)) {
915 *output
=0; // reset outputstring
916 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
917 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
920 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
921 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
923 PrintAndLog("%s",output
);
925 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
928 PrintAndLog("CRC failed");
931 PrintAndLog("no answer");
938 * Commandline handling: HF15 CMD READ
939 * Reads a single Block
941 int CmdHF15CmdRead(const char *Cmd
) {
944 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
945 uint8_t *req
=c
.d
.asBytes
;
946 int reqlen
=0, pagenum
;
955 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
956 PrintAndLog(" options:");
957 PrintAndLog(" -2 use slower '1 out of 256' mode");
958 PrintAndLog(" uid (either): ");
959 PrintAndLog(" <8B hex> full UID eg E011223344556677");
960 PrintAndLog(" s selected tag");
961 PrintAndLog(" u unaddressed mode");
962 PrintAndLog(" * scan for tag");
963 PrintAndLog(" page#: page number 0-255");
967 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READ
},1);
970 pagenum
=strtol(cmd
,NULL
,0);
972 PrintAndLog("invalid pagenum");
976 req
[reqlen
++]=(uint8_t)pagenum
;
978 reqlen
=AddCrc(req
,reqlen
);
984 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
985 recv
= resp
.d
.asBytes
;
986 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
987 if (!(recv
[0] & ISO15_RES_ERROR
)) {
988 *output
=0; // reset outputstring
989 //sprintf(output, "Block %2i ",blocknum);
990 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
991 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
994 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
995 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
997 PrintAndLog("%s",output
);
999 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1002 PrintAndLog("CRC failed");
1005 PrintAndLog("no answer");
1013 * Commandline handling: HF15 CMD WRITE
1014 * Writes a single Block - might run into timeout, even when successful
1016 int CmdHF15CmdWrite(const char *Cmd
) {
1019 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
1020 uint8_t *req
=c
.d
.asBytes
;
1021 int reqlen
=0, pagenum
, temp
;
1026 strncpy(cmd
,Cmd
,99);
1029 if (strlen(cmd
)<3) {
1030 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
1031 PrintAndLog(" options:");
1032 PrintAndLog(" -2 use slower '1 out of 256' mode");
1033 PrintAndLog(" -o set OPTION Flag (needed for TI)");
1034 PrintAndLog(" uid (either): ");
1035 PrintAndLog(" <8B hex> full UID eg E011223344556677");
1036 PrintAndLog(" s selected tag");
1037 PrintAndLog(" u unaddressed mode");
1038 PrintAndLog(" * scan for tag");
1039 PrintAndLog(" page#: page number 0-255");
1040 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
1044 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_WRITE
},1);
1047 // *cmd -> page num ; *cmd2 -> data
1049 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
1053 pagenum
=strtol(cmd
,NULL
,0);
1055 PrintAndLog("invalid pagenum");
1058 req
[reqlen
++]=(uint8_t)pagenum
;
1061 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
1066 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
1067 req
[reqlen
++]=temp
& 0xff;
1071 reqlen
=AddCrc(req
,reqlen
);
1077 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000) && resp
.arg
[0]>2) {
1078 recv
= resp
.d
.asBytes
;
1079 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
1080 if (!(recv
[0] & ISO15_RES_ERROR
)) {
1083 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
1086 PrintAndLog("CRC failed");
1089 PrintAndLog("timeout: no answer - data may be written anyway");
1097 static command_t CommandTable15Cmd
[] =
1099 {"help", CmdHF15CmdHelp
, 1, "This Help"},
1100 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
1102 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
1104 {"read", CmdHF15CmdRead
, 0, "Read a block"},
1105 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
1106 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
1107 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
1108 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
1109 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
1110 {NULL
, NULL
, 0, NULL
}
1113 int CmdHF15Cmd(const char *Cmd
)
1115 CmdsParse(CommandTable15Cmd
, Cmd
);
1119 int CmdHF15CmdHelp(const char *Cmd
)
1121 CmdsHelp(CommandTable15Cmd
);