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.
29 //#include "proxusb.h"
30 #include "proxmark3.h"
34 #include "cmdparser.h"
36 #include "../common/iso15693tools.h"
39 #define FrameSOF Iso15693FrameSOF
40 #define Logic0 Iso15693Logic0
41 #define Logic1 Iso15693Logic1
42 #define FrameEOF Iso15693FrameEOF
44 #define Crc(data,datalen) Iso15693Crc(data,datalen)
45 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
46 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
47 #define TRACE_BUFF_SIZE 12000
49 // structure and database for uid -> tagtype lookups
52 int mask
; // how many MSB bits used
57 const productName uidmapping
[] = {
58 // UID, #significant Bits, "Vendor(+Product)"
59 { 0xE001000000000000LL
, 16, "Motorola" },
60 { 0xE002000000000000LL
, 16, "ST Microelectronics" },
61 { 0xE003000000000000LL
, 16, "Hitachi" },
62 { 0xE004000000000000LL
, 16, "Philips" },
63 { 0xE004010000000000LL
, 24, "Philips; IC SL2 ICS20" },
64 { 0xE005000000000000LL
, 16, "Infineon" },
65 { 0xE005400000000000LL
, 24, "Infineon; 56x32bit" },
66 { 0xE006000000000000LL
, 16, "Cylinc" },
67 { 0xE007000000000000LL
, 16, "Texas Instrument; " },
68 { 0xE007000000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
69 { 0xE007100000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
70 { 0xE007800000000000LL
, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
71 { 0xE007C00000000000LL
, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
72 { 0xE007C40000000000LL
, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
73 { 0xE008000000000000LL
, 16, "Fujitsu" },
74 { 0xE009000000000000LL
, 16, "Matsushita" },
75 { 0xE00A000000000000LL
, 16, "NEC" },
76 { 0xE00B000000000000LL
, 16, "Oki Electric" },
77 { 0xE00C000000000000LL
, 16, "Toshiba" },
78 { 0xE00D000000000000LL
, 16, "Mitsubishi" },
79 { 0xE00E000000000000LL
, 16, "Samsung" },
80 { 0xE00F000000000000LL
, 16, "Hyundai" },
81 { 0xE010000000000000LL
, 16, "LG-Semiconductors" },
82 { 0xE012000000000000LL
, 16, "HID Corporation" },
83 { 0xE016000000000000LL
, 16, "EM-Marin SA (Skidata)" },
84 { 0xE016040000000000LL
, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034? no 'read', just 'readmulti'" },
85 { 0xE0160c0000000000LL
, 24, "EM-Marin SA; EM4035?" },
86 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
87 { 0xE016940000000000LL
, 24, "EM-Marin SA (Skidata); 51x64bit" },
88 { 0,0,"no tag-info available" } // must be the last entry
92 // fast method to just read the UID of a tag (collission detection not supported)
93 // *buf should be large enough to fit the 64bit uid
94 // returns 1 if suceeded
95 int getUID(uint8_t *buf
)
99 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
100 uint8_t *req
=c
.d
.asBytes
;
103 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
105 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
106 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
107 req
[1]=ISO15_CMD_INVENTORY
;
108 req
[2]=0; // mask length
109 reqlen
=AddCrc(req
,3);
114 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
115 recv
= resp
.d
.asBytes
;
116 if (resp
.arg
[0]>=12 && ISO15_CRC_CHECK
==Crc(recv
,12)) {
117 memcpy(buf
,&recv
[2],8);
127 // get a product description based on the UID
129 // returns description of the best match
130 static char* getTagInfo(uint8_t *uid
) {
133 memcpy(&myuid
,uid
,sizeof(uint64_t));
134 while (uidmapping
[i
].mask
>0) {
135 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
136 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
140 if (uidmapping
[i
].mask
>uidmapping
[best
].mask
) {
148 if (best
>=0) return uidmapping
[best
].desc
;
150 return uidmapping
[i
].desc
;
154 // return a clear-text message to an errorcode
155 static char* TagErrorStr(uint8_t error
) {
157 case 0x01: return "The command is not supported";
158 case 0x02: return "The command is not recognised";
159 case 0x03: return "The option is not supported.";
160 case 0x0f: return "Unknown error.";
161 case 0x10: return "The specified block is not available (doesn’t exist).";
162 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
163 case 0x12: return "The specified block is locked and its content cannot be changed.";
164 case 0x13: return "The specified block was not successfully programmed.";
165 case 0x14: return "The specified block was not successfully locked.";
166 default: return "Reserved for Future Use or Custom command error.";
172 int CmdHF15Demod(const char *Cmd
)
174 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
177 int max
= 0, maxPos
= 0;
181 if (GraphTraceLen
< 1000) return 0;
183 // First, correlate for SOF
184 for (i
= 0; i
< 100; i
++) {
186 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
187 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
194 PrintAndLog("SOF at %d, correlation %d", maxPos
,
195 max
/ (arraylen(FrameSOF
) / skip
));
197 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
200 memset(outBuf
, 0, sizeof(outBuf
));
203 int corr0
= 0, corr1
= 0, corrEOF
= 0;
204 for (j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
205 corr0
+= Logic0
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
207 for (j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
208 corr1
+= Logic1
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
210 for (j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
211 corrEOF
+= FrameEOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
213 // Even things out by the length of the target waveform.
217 if (corrEOF
> corr1
&& corrEOF
> corr0
) {
218 PrintAndLog("EOF at %d", i
);
220 } else if (corr1
> corr0
) {
221 i
+= arraylen(Logic1
) / skip
;
224 i
+= arraylen(Logic0
) / skip
;
231 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
232 PrintAndLog("ran off end!");
237 PrintAndLog("error, uneven octet! (discard extra bits!)");
238 PrintAndLog(" mask=%02x", mask
);
240 PrintAndLog("%d octets", k
);
242 for (i
= 0; i
< k
; i
++) {
243 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
245 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
251 // * Acquire Samples as Reader (enables carrier, sends inquiry)
252 int CmdHF15Read(const char *Cmd
)
254 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
257 uint8_t data
[TRACE_BUFF_SIZE
] = {0x00};
259 GetFromBigBuf(data
,TRACE_BUFF_SIZE
,3560); //3560 -- should be offset..
260 WaitForResponseTimeout(CMD_ACK
,NULL
, 1500);
262 for (int j
= 0; j
< TRACE_BUFF_SIZE
; j
++) {
263 GraphBuffer
[j
] = ((int)data
[j
]) ;
265 GraphTraceLen
= TRACE_BUFF_SIZE
;
266 RepaintGraphWindow();
270 // Record Activity without enabeling carrier
271 int CmdHF15Record(const char *Cmd
)
273 UsbCommand c
= {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693
};
276 uint8_t data
[TRACE_BUFF_SIZE
] = {0x00};
278 GetFromBigBuf(data
,TRACE_BUFF_SIZE
,3560); //3560 -- should be offset..
279 WaitForResponseTimeout(CMD_ACK
,NULL
, 1500);
281 for (int j
= 0; j
< TRACE_BUFF_SIZE
; j
++) {
282 GraphBuffer
[j
] = ((int)data
[j
]) ;
284 GraphTraceLen
= TRACE_BUFF_SIZE
;
285 RepaintGraphWindow();
289 int CmdHF15Reader(const char *Cmd
)
291 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
296 // Simulation is still not working very good
297 int CmdHF15Sim(const char *Cmd
)
299 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
304 // finds the AFI (Application Family Idendifier) of a card, by trying all values
305 // (There is no standard way of reading the AFI, allthough some tags support this)
306 int CmdHF15Afi(const char *Cmd
)
308 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
313 // Reads all memory pages
314 int CmdHF15DumpMem(const char*Cmd
) {
318 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
319 uint8_t *req
=c
.d
.asBytes
;
325 PrintAndLog("No Tag found.");
329 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
330 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
332 for (int retry
=0; retry
<5; retry
++) {
334 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
335 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
336 req
[1]=ISO15_CMD_READ
;
337 memcpy(&req
[2],uid
,8);
339 reqlen
=AddCrc(req
,11);
344 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
345 recv
= resp
.d
.asBytes
;
346 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
347 if (!(recv
[0] & ISO15_RES_ERROR
)) {
349 *output
=0; // reset outputstring
350 sprintf(output
, "Block %2i ",blocknum
);
351 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in hex
352 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
355 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in cleaned ascii
356 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
358 PrintAndLog("%s",output
);
360 // PrintAndLog("bn=%i",blocknum);
362 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
365 } // else PrintAndLog("crc");
366 } // else PrintAndLog("r null");
369 // if (resp.arg[0]<3)
370 // PrintAndLog("Lost Connection");
371 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
372 // PrintAndLog("CRC Failed");
374 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
381 static command_t CommandTable15
[] =
383 {"help", CmdHF15Help
, 1, "This help"},
384 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
385 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
386 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
387 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
388 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
389 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
390 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
391 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
392 {NULL
, NULL
, 0, NULL
}
395 int CmdHF15(const char *Cmd
)
397 CmdsParse(CommandTable15
, Cmd
);
401 int CmdHF15Help(const char *Cmd
)
403 CmdsHelp(CommandTable15
);
408 // "HF 15 Cmd" Interface
409 // Allows direct communication with the tag on command level
411 int CmdHF15CmdInquiry(const char *Cmd
)
415 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
416 uint8_t *req
=c
.d
.asBytes
;
419 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
420 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
421 req
[1]=ISO15_CMD_INVENTORY
;
422 req
[2]=0; // mask length
423 reqlen
=AddCrc(req
,3);
428 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
429 if (resp
.arg
[0]>=12) {
430 recv
= resp
.d
.asBytes
;
431 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
432 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
434 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp
.arg
[0]);
437 PrintAndLog("timeout.");
443 // Turns debugging on(1)/off(0)
444 int CmdHF15CmdDebug( const char *cmd
) {
447 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
448 PrintAndLog(" 0 no debugging");
449 PrintAndLog(" 1 turn debugging on");
453 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
459 int CmdHF15CmdRaw (const char *cmd
) {
462 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
469 unsigned int datalen
=0, temp
;
474 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
475 PrintAndLog(" -r do not read response");
476 PrintAndLog(" -2 use slower '1 out of 256' mode");
477 PrintAndLog(" -c calculate and append CRC");
478 PrintAndLog(" Tip: turn on debugging for verbose output");
483 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
485 while (cmd
[i
]!='\0') {
486 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
501 PrintAndLog("Invalid option");
507 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
508 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
509 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
510 buf
[strlen(buf
)+1]=0;
511 buf
[strlen(buf
)]=cmd
[i
];
514 if (strlen(buf
)>=2) {
515 sscanf(buf
,"%x",&temp
);
516 data
[datalen
]=(uint8_t)(temp
& 0xff);
522 PrintAndLog("Invalid char on input");
525 if (crc
) datalen
=AddCrc(data
,datalen
);
530 memcpy(c
.d
.asBytes
,data
,datalen
);
535 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
536 recv
= resp
.d
.asBytes
;
537 PrintAndLog("received %i octets",resp
.arg
[0]);
538 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
539 if (hexout
!= NULL
) {
540 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
541 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
543 PrintAndLog("%s", hexout
);
547 PrintAndLog("timeout while waiting for reply.");
556 * parses common HF 15 CMD parameters and prepares some data structures
560 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
562 uint8_t *req
=c
->d
.asBytes
, uid
[8];
566 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
568 if (strstr(*cmd
,"-2")==*cmd
) {
569 c
->arg
[1]=0; // use 1of256
574 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
576 if (strstr(*cmd
,"-o")==*cmd
) {
577 req
[reqlen
]=ISO15_REQ_OPTION
;
582 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
586 PrintAndLog("missing addr");
591 // you must have selected the tag earlier
592 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
593 ISO15_REQ_NONINVENTORY
| ISO15_REQ_SELECT
;
594 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
599 // unaddressed mode may not be supported by all vendors
600 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
601 ISO15_REQ_NONINVENTORY
;
602 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
606 // we scan for the UID ourself
607 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
608 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
609 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
612 PrintAndLog("No Tag found");
615 memcpy(req
+reqlen
,uid
,8);
616 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
620 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
621 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
622 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
625 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
626 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
627 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
628 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
629 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
630 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
631 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
635 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
636 memcpy(&req
[reqlen
],&uid
[0],8);
639 // skip to next space
640 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
641 // skip over the space
642 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
649 * Commandline handling: HF15 CMD SYSINFO
650 * get system information from tag/VICC
652 int CmdHF15CmdSysinfo(const char *Cmd
) {
655 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
656 uint8_t *req
=c
.d
.asBytes
;
660 char output
[2048]="";
667 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
668 PrintAndLog(" options:");
669 PrintAndLog(" -2 use slower '1 out of 256' mode");
670 PrintAndLog(" uid (either): ");
671 PrintAndLog(" <8B hex> full UID eg E011223344556677");
672 PrintAndLog(" s selected tag");
673 PrintAndLog(" u unaddressed mode");
674 PrintAndLog(" * scan for tag");
675 PrintAndLog(" start#: page number to start 0-255");
676 PrintAndLog(" count#: number of pages");
680 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_SYSINFO
},1);
683 reqlen
=AddCrc(req
,reqlen
);
688 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
689 recv
= resp
.d
.asBytes
;
690 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
691 if (!(recv
[0] & ISO15_RES_ERROR
)) {
692 *output
=0; // reset outputstring
693 for ( i
=1; i
<resp
.arg
[0]-2; i
++) {
694 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
696 strcat(output
,"\n\r");
697 strcat(output
,"UID = ");
698 strcat(output
,sprintUID(NULL
,recv
+2));
699 strcat(output
,"\n\r");
700 strcat(output
,getTagInfo(recv
+2)); //ABC
701 strcat(output
,"\n\r");
704 sprintf(output
+strlen(output
),"DSFID supported, set to %02X\n\r",recv
[i
++]);
706 strcat(output
,"DSFID not supported\n\r");
708 sprintf(output
+strlen(output
),"AFI supported, set to %03X\n\r",recv
[i
++]);
710 strcat(output
,"AFI not supported\n\r");
711 if (recv
[1] & 0x04) {
712 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
713 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
714 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
717 strcat(output
,"Tag does not provide information on memory layout\n\r");
718 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02X\n\r",recv
[i
++]);
719 else strcat(output
,"IC reference not given\n\r");
722 PrintAndLog("%s",output
);
724 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
727 PrintAndLog("CRC failed");
730 PrintAndLog("timeout: no answer");
737 * Commandline handling: HF15 CMD READMULTI
738 * Read multiple blocks at once (not all tags support this)
740 int CmdHF15CmdReadmulti(const char *Cmd
) {
743 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
744 uint8_t *req
=c
.d
.asBytes
;
745 int reqlen
=0, pagenum
,pagecount
;
748 char output
[2048]="";
754 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
755 PrintAndLog(" options:");
756 PrintAndLog(" -2 use slower '1 out of 256' mode");
757 PrintAndLog(" uid (either): ");
758 PrintAndLog(" <8B hex> full UID eg E011223344556677");
759 PrintAndLog(" s selected tag");
760 PrintAndLog(" u unaddressed mode");
761 PrintAndLog(" * scan for tag");
762 PrintAndLog(" start#: page number to start 0-255");
763 PrintAndLog(" count#: number of pages");
767 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READMULTI
},1);
770 pagenum
=strtol(cmd
,NULL
,0);
772 // skip to next space
773 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
774 // skip over the space
775 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
777 pagecount
=strtol(cmd
,NULL
,0);
778 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
780 req
[reqlen
++]=(uint8_t)pagenum
;
781 req
[reqlen
++]=(uint8_t)pagecount
;
783 reqlen
=AddCrc(req
,reqlen
);
789 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
790 recv
= resp
.d
.asBytes
;
791 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
792 if (!(recv
[0] & ISO15_RES_ERROR
)) {
793 *output
=0; // reset outputstring
794 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
795 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
798 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
799 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
801 PrintAndLog("%s",output
);
803 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
806 PrintAndLog("CRC failed");
809 PrintAndLog("no answer");
816 * Commandline handling: HF15 CMD READ
817 * Reads a single Block
819 int CmdHF15CmdRead(const char *Cmd
) {
822 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
823 uint8_t *req
=c
.d
.asBytes
;
824 int reqlen
=0, pagenum
;
833 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
834 PrintAndLog(" options:");
835 PrintAndLog(" -2 use slower '1 out of 256' mode");
836 PrintAndLog(" uid (either): ");
837 PrintAndLog(" <8B hex> full UID eg E011223344556677");
838 PrintAndLog(" s selected tag");
839 PrintAndLog(" u unaddressed mode");
840 PrintAndLog(" * scan for tag");
841 PrintAndLog(" page#: page number 0-255");
845 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READ
},1);
848 pagenum
=strtol(cmd
,NULL
,0);
850 PrintAndLog("invalid pagenum");
854 req
[reqlen
++]=(uint8_t)pagenum
;
856 reqlen
=AddCrc(req
,reqlen
);
862 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
863 recv
= resp
.d
.asBytes
;
864 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
865 if (!(recv
[0] & ISO15_RES_ERROR
)) {
866 *output
=0; // reset outputstring
867 //sprintf(output, "Block %2i ",blocknum);
868 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
869 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
872 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
873 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
875 PrintAndLog("%s",output
);
877 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
880 PrintAndLog("CRC failed");
883 PrintAndLog("no answer");
891 * Commandline handling: HF15 CMD WRITE
892 * Writes a single Block - might run into timeout, even when successful
894 int CmdHF15CmdWrite(const char *Cmd
) {
897 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
898 uint8_t *req
=c
.d
.asBytes
;
899 int reqlen
=0, pagenum
, temp
;
908 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
909 PrintAndLog(" options:");
910 PrintAndLog(" -2 use slower '1 out of 256' mode");
911 PrintAndLog(" -o set OPTION Flag (needed for TI)");
912 PrintAndLog(" uid (either): ");
913 PrintAndLog(" <8B hex> full UID eg E011223344556677");
914 PrintAndLog(" s selected tag");
915 PrintAndLog(" u unaddressed mode");
916 PrintAndLog(" * scan for tag");
917 PrintAndLog(" page#: page number 0-255");
918 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
922 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_WRITE
},1);
925 // *cmd -> page num ; *cmd2 -> data
927 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
931 pagenum
=strtol(cmd
,NULL
,0);
933 PrintAndLog("invalid pagenum");
936 req
[reqlen
++]=(uint8_t)pagenum
;
939 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
944 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
945 req
[reqlen
++]=temp
& 0xff;
949 reqlen
=AddCrc(req
,reqlen
);
955 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000) && resp
.arg
[0]>2) {
956 recv
= resp
.d
.asBytes
;
957 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
958 if (!(recv
[0] & ISO15_RES_ERROR
)) {
961 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
964 PrintAndLog("CRC failed");
967 PrintAndLog("timeout: no answer - data may be written anyway");
975 static command_t CommandTable15Cmd
[] =
977 {"help", CmdHF15CmdHelp
, 1, "This Help"},
978 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
980 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
982 {"read", CmdHF15CmdRead
, 0, "Read a block"},
983 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
984 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
985 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
986 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
987 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
988 {NULL
, NULL
, 0, NULL
}
991 int CmdHF15Cmd(const char *Cmd
)
993 CmdsParse(CommandTable15Cmd
, Cmd
);
997 int CmdHF15CmdHelp(const char *Cmd
)
999 CmdsHelp(CommandTable15Cmd
);