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.
33 #include "cmdparser.h"
35 #include "iso15693tools.h"
38 #define FrameSOF Iso15693FrameSOF
39 #define Logic0 Iso15693Logic0
40 #define Logic1 Iso15693Logic1
41 #define FrameEOF Iso15693FrameEOF
43 #define Crc(data,datalen) Iso15693Crc(data,datalen)
44 #define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
45 #define sprintUID(target,uid) Iso15693sprintUID(target,uid)
47 static int CmdHelp(const char *Cmd
);
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 r
=WaitForResponseTimeout(CMD_ACK
,1000);
118 if (r
->arg
[0]>=12 && ISO15_CRC_CHECK
==Crc(recv
,12)) {
119 memcpy(buf
,&recv
[2],8);
129 // get a product description based on the UID
131 // returns description of the best match
132 static char* getTagInfo(uint8_t *uid
) {
135 memcpy(&myuid
,uid
,sizeof(uint64_t));
136 while (uidmapping
[i
].mask
>0) {
137 mask
=(~0LL) <<(64-uidmapping
[i
].mask
);
138 if ((myuid
& mask
) == uidmapping
[i
].uid
) {
142 if (uidmapping
[i
].mask
>uidmapping
[best
].mask
) {
150 if (best
>=0) return uidmapping
[best
].desc
;
152 return uidmapping
[i
].desc
;
156 // return a clear-text message to an errorcode
157 static char* TagErrorStr(uint8_t error
) {
159 case 0x01: return "The command is not supported";
160 case 0x02: return "The command is not recognised";
161 case 0x03: return "The option is not supported.";
162 case 0x0f: return "Unknown error.";
163 case 0x10: return "The specified block is not available (doesn’t exist).";
164 case 0x11: return "The specified block is already -locked and thus cannot be locked again";
165 case 0x12: return "The specified block is locked and its content cannot be changed.";
166 case 0x13: return "The specified block was not successfully programmed.";
167 case 0x14: return "The specified block was not successfully locked.";
168 default: return "Reserved for Future Use or Custom command error.";
174 int CmdHF15Demod(const char *Cmd
)
176 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
179 int max
= 0, maxPos
= 0;
183 if (GraphTraceLen
< 1000) return 0;
185 // First, correlate for SOF
186 for (i
= 0; i
< 100; i
++) {
188 for (j
= 0; j
< arraylen(FrameSOF
); j
+= skip
) {
189 corr
+= FrameSOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
196 PrintAndLog("SOF at %d, correlation %d", maxPos
,
197 max
/ (arraylen(FrameSOF
) / skip
));
199 i
= maxPos
+ arraylen(FrameSOF
) / skip
;
202 memset(outBuf
, 0, sizeof(outBuf
));
205 int corr0
= 0, corr1
= 0, corrEOF
= 0;
206 for (j
= 0; j
< arraylen(Logic0
); j
+= skip
) {
207 corr0
+= Logic0
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
209 for (j
= 0; j
< arraylen(Logic1
); j
+= skip
) {
210 corr1
+= Logic1
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
212 for (j
= 0; j
< arraylen(FrameEOF
); j
+= skip
) {
213 corrEOF
+= FrameEOF
[j
] * GraphBuffer
[i
+ (j
/ skip
)];
215 // Even things out by the length of the target waveform.
219 if (corrEOF
> corr1
&& corrEOF
> corr0
) {
220 PrintAndLog("EOF at %d", i
);
222 } else if (corr1
> corr0
) {
223 i
+= arraylen(Logic1
) / skip
;
226 i
+= arraylen(Logic0
) / skip
;
233 if ((i
+ (int)arraylen(FrameEOF
)) >= GraphTraceLen
) {
234 PrintAndLog("ran off end!");
239 PrintAndLog("error, uneven octet! (discard extra bits!)");
240 PrintAndLog(" mask=%02x", mask
);
242 PrintAndLog("%d octets", k
);
244 for (i
= 0; i
< k
; i
++) {
245 PrintAndLog("# %2d: %02x ", i
, outBuf
[i
]);
247 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf
, k
- 2));
253 // * Acquire Samples as Reader (enables carrier, sends inquiry)
254 int CmdHF15Read(const char *Cmd
)
256 UsbCommand c
= {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693
};
261 // Record Activity without enabeling carrier
262 int CmdHF15Record(const char *Cmd
)
264 UsbCommand c
= {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693
};
269 int CmdHF15Reader(const char *Cmd
)
271 UsbCommand c
= {CMD_READER_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
276 // Simulation is still not working very good
277 int CmdHF15Sim(const char *Cmd
)
279 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {strtol(Cmd
, NULL
, 0), 0, 0}};
284 // finds the AFI (Application Family Idendifier) of a card, by trying all values
285 // (There is no standard way of reading the AFI, allthough some tags support this)
286 int CmdHF15Afi(const char *Cmd
)
288 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
293 // Reads all memory pages
294 int CmdHF15DumpMem(const char*Cmd
) {
298 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
299 uint8_t *req
=c
.d
.asBytes
;
305 PrintAndLog("No Tag found.");
309 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
310 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
312 for (int retry
=0; retry
<5; retry
++) {
314 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
315 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
316 req
[1]=ISO15_CMD_READ
;
317 memcpy(&req
[2],uid
,8);
319 reqlen
=AddCrc(req
,11);
324 r
=WaitForResponseTimeout(CMD_ACK
,1000);
328 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
329 if (!(recv
[0] & ISO15_RES_ERROR
)) {
331 *output
=0; // reset outputstring
332 sprintf(output
, "Block %2i ",blocknum
);
333 for ( int i
=1; i
<r
->arg
[0]-2; i
++) { // data in hex
334 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
337 for ( int i
=1; i
<r
->arg
[0]-2; i
++) { // data in cleaned ascii
338 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
340 PrintAndLog("%s",output
);
342 // PrintAndLog("bn=%i",blocknum);
344 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
347 } // else PrintAndLog("crc");
348 } // else PrintAndLog("r null");
351 if (r
&& r
->arg
[0]<3)
352 PrintAndLog("Lost Connection");
353 else if (r
&& ISO15_CRC_CHECK
!=Crc(r
->d
.asBytes
,r
->arg
[0]))
354 PrintAndLog("CRC Failed");
356 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
363 static command_t CommandTable15
[] =
365 {"help", CmdHF15Help
, 1, "This help"},
366 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
367 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
368 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
369 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
370 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
371 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
372 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
373 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
374 {NULL
, NULL
, 0, NULL
}
377 int CmdHF15(const char *Cmd
)
379 CmdsParse(CommandTable15
, Cmd
);
383 int CmdHF15Help(const char *Cmd
)
385 CmdsHelp(CommandTable15
);
390 // "HF 15 Cmd" Interface
391 // Allows direct communication with the tag on command level
393 int CmdHF15CmdInquiry(const char *Cmd
)
397 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
398 uint8_t *req
=c
.d
.asBytes
;
401 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
402 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
403 req
[1]=ISO15_CMD_INVENTORY
;
404 req
[2]=0; // mask length
405 reqlen
=AddCrc(req
,3);
410 r
=WaitForResponseTimeout(CMD_ACK
,1000);
415 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
416 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
418 PrintAndLog("Response to short, just %i bytes. No tag?\n",r
->arg
[0]);
421 PrintAndLog("timeout.");
427 // Turns debugging on(1)/off(0)
428 int CmdHF15CmdDebug( const char *cmd
) {
431 PrintAndLog("Usage: hf 15 cmd debug <0/1>");
432 PrintAndLog(" 0..no debugging output 1..turn debugging on");
436 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
442 int CmdHF15CmdRaw (const char *cmd
) {
445 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
452 unsigned int datalen
=0, temp
;
457 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
458 PrintAndLog(" -r do not read response");
459 PrintAndLog(" -2 use slower '1 out of 256' mode");
460 PrintAndLog(" -c calculate and append CRC");
461 PrintAndLog(" Tip: turn on debugging for verbose output");
466 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
468 while (cmd
[i
]!='\0') {
469 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
484 PrintAndLog("Invalid option");
490 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
491 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
492 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
493 buf
[strlen(buf
)+1]=0;
494 buf
[strlen(buf
)]=cmd
[i
];
497 if (strlen(buf
)>=2) {
498 sscanf(buf
,"%x",&temp
);
499 data
[datalen
]=(uint8_t)(temp
& 0xff);
505 PrintAndLog("Invalid char on input");
508 if (crc
) datalen
=AddCrc(data
,datalen
);
513 memcpy(c
.d
.asBytes
,data
,datalen
);
518 r
=WaitForResponseTimeout(CMD_ACK
,1000);
522 PrintAndLog("received %i octets",r
->arg
[0]);
523 hexout
= (char *)malloc(r
->arg
[0] * 3 + 1);
524 if (hexout
!= NULL
) {
525 for (int i
= 0; i
< r
->arg
[0]; i
++) { // data in hex
526 sprintf(&hexout
[i
* 3], "%02hX ", recv
[i
]);
528 PrintAndLog("%s", hexout
);
532 PrintAndLog("timeout while waiting for reply.");
541 * parses common HF 15 CMD parameters and prepares some data structures
545 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
547 uint8_t *req
=c
->d
.asBytes
, uid
[8];
551 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
553 if (strstr(*cmd
,"-2")==*cmd
) {
554 c
->arg
[1]=0; // use 1of256
559 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
561 if (strstr(*cmd
,"-o")==*cmd
) {
562 req
[reqlen
]=ISO15_REQ_OPTION
;
567 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
571 PrintAndLog("missing addr");
576 // you must have selected the tag earlier
577 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
578 ISO15_REQ_NONINVENTORY
| ISO15_REQ_SELECT
;
579 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
584 // unaddressed mode may not be supported by all vendors
585 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
586 ISO15_REQ_NONINVENTORY
;
587 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
591 // we scan for the UID ourself
592 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
593 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
594 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
597 PrintAndLog("No Tag found");
600 memcpy(req
+reqlen
,uid
,8);
601 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
605 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
606 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
607 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
610 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
611 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
612 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
613 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
614 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
615 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
616 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
620 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
621 memcpy(&req
[reqlen
],&uid
[0],8);
624 // skip to next space
625 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
626 // skip over the space
627 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
634 * Commandline handling: HF15 CMD SYSINFO
635 * get system information from tag/VICC
637 int CmdHF15CmdSysinfo(const char *Cmd
) {
640 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
641 uint8_t *req
=c
.d
.asBytes
;
645 char output
[2048]="";
652 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
653 PrintAndLog(" options:");
654 PrintAndLog(" -2 use slower '1 out of 256' mode");
655 PrintAndLog(" uid (either): ");
656 PrintAndLog(" <8B hex> full UID eg E011223344556677");
657 PrintAndLog(" s selected tag");
658 PrintAndLog(" u unaddressed mode");
659 PrintAndLog(" * scan for tag");
660 PrintAndLog(" start#: page number to start 0-255");
661 PrintAndLog(" count#: number of pages");
665 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_SYSINFO
},1);
668 reqlen
=AddCrc(req
,reqlen
);
673 r
=WaitForResponseTimeout(CMD_ACK
,1000);
675 if (r
!=NULL
&& r
->arg
[0]>2) {
677 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
678 if (!(recv
[0] & ISO15_RES_ERROR
)) {
679 *output
=0; // reset outputstring
680 for ( i
=1; i
<r
->arg
[0]-2; i
++) {
681 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
683 strcat(output
,"\n\r");
684 strcat(output
,"UID = ");
685 strcat(output
,sprintUID(NULL
,recv
+2));
686 strcat(output
,"\n\r");
687 strcat(output
,getTagInfo(recv
+2)); //ABC
688 strcat(output
,"\n\r");
691 sprintf(output
+strlen(output
),"DSFID supported, set to %02hX\n\r",recv
[i
++]);
693 strcat(output
,"DSFID not supported\n\r");
695 sprintf(output
+strlen(output
),"AFI supported, set to %03hX\n\r",recv
[i
++]);
697 strcat(output
,"AFI not supported\n\r");
698 if (recv
[1] & 0x04) {
699 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
700 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
701 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
704 strcat(output
,"Tag does not provide information on memory layout\n\r");
705 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02hX\n\r",recv
[i
++]);
706 else strcat(output
,"IC reference not given\n\r");
709 PrintAndLog("%s",output
);
711 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
714 PrintAndLog("CRC failed");
717 PrintAndLog("timeout: no answer");
724 * Commandline handling: HF15 CMD READMULTI
725 * Read multiple blocks at once (not all tags support this)
727 int CmdHF15CmdReadmulti(const char *Cmd
) {
730 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
731 uint8_t *req
=c
.d
.asBytes
;
732 int reqlen
=0, pagenum
,pagecount
;
735 char output
[2048]="";
741 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
742 PrintAndLog(" options:");
743 PrintAndLog(" -2 use slower '1 out of 256' mode");
744 PrintAndLog(" uid (either): ");
745 PrintAndLog(" <8B hex> full UID eg E011223344556677");
746 PrintAndLog(" s selected tag");
747 PrintAndLog(" u unaddressed mode");
748 PrintAndLog(" * scan for tag");
749 PrintAndLog(" start#: page number to start 0-255");
750 PrintAndLog(" count#: number of pages");
754 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READMULTI
},1);
757 pagenum
=strtol(cmd
,NULL
,0);
759 // skip to next space
760 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
761 // skip over the space
762 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
764 pagecount
=strtol(cmd
,NULL
,0);
765 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
767 req
[reqlen
++]=(uint8_t)pagenum
;
768 req
[reqlen
++]=(uint8_t)pagecount
;
770 reqlen
=AddCrc(req
,reqlen
);
776 r
=WaitForResponseTimeout(CMD_ACK
,1000);
778 if (r
!=NULL
&& r
->arg
[0]>2) {
780 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
781 if (!(recv
[0] & ISO15_RES_ERROR
)) {
782 *output
=0; // reset outputstring
783 for ( int i
=1; i
<r
->arg
[0]-2; i
++) {
784 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
787 for ( int i
=1; i
<r
->arg
[0]-2; i
++) {
788 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
790 PrintAndLog("%s",output
);
792 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
795 PrintAndLog("CRC failed");
798 PrintAndLog("no answer");
805 * Commandline handling: HF15 CMD READ
806 * Reads a single Block
808 int CmdHF15CmdRead(const char *Cmd
) {
811 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
812 uint8_t *req
=c
.d
.asBytes
;
813 int reqlen
=0, pagenum
;
822 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
823 PrintAndLog(" options:");
824 PrintAndLog(" -2 use slower '1 out of 256' mode");
825 PrintAndLog(" uid (either): ");
826 PrintAndLog(" <8B hex> full UID eg E011223344556677");
827 PrintAndLog(" s selected tag");
828 PrintAndLog(" u unaddressed mode");
829 PrintAndLog(" * scan for tag");
830 PrintAndLog(" page#: page number 0-255");
834 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READ
},1);
837 pagenum
=strtol(cmd
,NULL
,0);
839 PrintAndLog("invalid pagenum");
843 req
[reqlen
++]=(uint8_t)pagenum
;
845 reqlen
=AddCrc(req
,reqlen
);
851 r
=WaitForResponseTimeout(CMD_ACK
,1000);
853 if (r
!=NULL
&& r
->arg
[0]>2) {
855 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
856 if (!(recv
[0] & ISO15_RES_ERROR
)) {
857 *output
=0; // reset outputstring
858 //sprintf(output, "Block %2i ",blocknum);
859 for ( int i
=1; i
<r
->arg
[0]-2; i
++) {
860 sprintf(output
+strlen(output
),"%02hX ",recv
[i
]);
863 for ( int i
=1; i
<r
->arg
[0]-2; i
++) {
864 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
866 PrintAndLog("%s",output
);
868 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
871 PrintAndLog("CRC failed");
874 PrintAndLog("no answer");
882 * Commandline handling: HF15 CMD WRITE
883 * Writes a single Block - might run into timeout, even when successful
885 int CmdHF15CmdWrite(const char *Cmd
) {
888 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
889 uint8_t *req
=c
.d
.asBytes
;
890 int reqlen
=0, pagenum
, temp
;
899 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
900 PrintAndLog(" options:");
901 PrintAndLog(" -2 use slower '1 out of 256' mode");
902 PrintAndLog(" -o set OPTION Flag (needed for TI)");
903 PrintAndLog(" uid (either): ");
904 PrintAndLog(" <8B hex> full UID eg E011223344556677");
905 PrintAndLog(" s selected tag");
906 PrintAndLog(" u unaddressed mode");
907 PrintAndLog(" * scan for tag");
908 PrintAndLog(" page#: page number 0-255");
909 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
913 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_WRITE
},1);
916 // *cmd -> page num ; *cmd2 -> data
918 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
922 pagenum
=strtol(cmd
,NULL
,0);
924 PrintAndLog("invalid pagenum");
927 req
[reqlen
++]=(uint8_t)pagenum
;
930 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
935 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
936 req
[reqlen
++]=temp
& 0xff;
940 reqlen
=AddCrc(req
,reqlen
);
946 r
=WaitForResponseTimeout(CMD_ACK
,2000);
948 if (r
!=NULL
&& r
->arg
[0]>2) {
950 if (ISO15_CRC_CHECK
==Crc(recv
,r
->arg
[0])) {
951 if (!(recv
[0] & ISO15_RES_ERROR
)) {
954 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
957 PrintAndLog("CRC failed");
960 PrintAndLog("timeout: no answer - data may be written anyway");
968 static command_t CommandTable15Cmd
[] =
970 {"help", CmdHF15CmdHelp
, 1, "This Help"},
971 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
973 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
975 {"read", CmdHF15CmdRead
, 0, "Read a block"},
976 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
977 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
978 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
979 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
980 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
981 {NULL
, NULL
, 0, NULL
}
984 int CmdHF15Cmd(const char *Cmd
)
986 CmdsParse(CommandTable15Cmd
, Cmd
);
990 int CmdHF15CmdHelp(const char *Cmd
)
992 CmdsHelp(CommandTable15Cmd
);