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 "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)
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, "NXP(Philips)" },
63 { 0xE004010000000000LL
, 24, "NXP(Philips); IC SL2 ICS20/ICS21(SLI) ICS2002/ICS2102(SLIX)" },
64 { 0xE004020000000000LL
, 24, "NXP(Philips); IC SL2 ICS53/ICS54(SLI-S) ICS5302/ICS5402(SLIX-S)" },
65 { 0xE004030000000000LL
, 24, "NXP(Philips); IC SL2 ICS50/ICS51(SLI-L) ICS5002/ICS5102(SLIX-L)" },
66 { 0xE005000000000000LL
, 16, "Infineon" },
67 { 0xE005400000000000LL
, 24, "Infineon; 56x32bit" },
68 { 0xE006000000000000LL
, 16, "Cylinc" },
69 { 0xE007000000000000LL
, 16, "Texas Instrument; " },
70 { 0xE007000000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Inlay; 64x32bit" },
71 { 0xE007100000000000LL
, 20, "Texas Instrument; Tag-it HF-I Plus Chip; 64x32bit" },
72 { 0xE007800000000000LL
, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
73 { 0xE007C00000000000LL
, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
74 { 0xE007C40000000000LL
, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
75 { 0xE008000000000000LL
, 16, "Fujitsu" },
76 { 0xE009000000000000LL
, 16, "Matsushita" },
77 { 0xE00A000000000000LL
, 16, "NEC" },
78 { 0xE00B000000000000LL
, 16, "Oki Electric" },
79 { 0xE00C000000000000LL
, 16, "Toshiba" },
80 { 0xE00D000000000000LL
, 16, "Mitsubishi" },
81 { 0xE00E000000000000LL
, 16, "Samsung" },
82 { 0xE00F000000000000LL
, 16, "Hyundai" },
83 { 0xE010000000000000LL
, 16, "LG-Semiconductors" },
84 { 0xE012000000000000LL
, 16, "HID Corporation" },
85 { 0xE016000000000000LL
, 16, "EM-Marin SA (Skidata)" },
86 { 0xE016040000000000LL
, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034? no 'read', just 'readmulti'" },
87 { 0xE0160c0000000000LL
, 24, "EM-Marin SA; EM4035?" },
88 { 0xE016100000000000LL
, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
89 { 0xE016940000000000LL
, 24, "EM-Marin SA (Skidata); 51x64bit" },
90 { 0,0,"no tag-info available" } // must be the last entry
94 // fast method to just read the UID of a tag (collission detection not supported)
95 // *buf should be large enough to fit the 64bit uid
96 // returns 1 if suceeded
97 int getUID(uint8_t *buf
)
101 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
102 uint8_t *req
=c
.d
.asBytes
;
105 for (int retry
=0;retry
<3; retry
++) { // don't give up the at the first try
107 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
108 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
109 req
[1]=ISO15_CMD_INVENTORY
;
110 req
[2]=0; // mask length
111 reqlen
=AddCrc(req
,3);
116 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
117 recv
= resp
.d
.asBytes
;
118 if (resp
.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 char cmdp
= param_getchar(Cmd
, 0);
280 uint8_t uid
[8] = {0x00};
282 //E0 16 24 00 00 00 00 00
283 if (cmdp
== 'h' || cmdp
== 'H') {
284 PrintAndLog("Usage: hf 15 sim <UID>");
286 PrintAndLog(" sample: hf 15 sim E016240000000000");
290 if (param_gethex(Cmd
, 0, uid
, 16)) {
291 PrintAndLog("UID must include 16 HEX symbols");
295 PrintAndLog("Starting simulating UID %02X %02X %02X %02X %02X %02X %02X %02X",
296 uid
[0],uid
[1],uid
[2],uid
[3],uid
[4], uid
[5], uid
[6], uid
[7]);
298 UsbCommand c
= {CMD_SIMTAG_ISO_15693
, {0, 0, 0}};
299 memcpy(c
.d
.asBytes
,uid
,8);
305 // finds the AFI (Application Family Idendifier) of a card, by trying all values
306 // (There is no standard way of reading the AFI, allthough some tags support this)
307 int CmdHF15Afi(const char *Cmd
)
309 UsbCommand c
= {CMD_ISO_15693_FIND_AFI
, {strtol(Cmd
, NULL
, 0), 0, 0}};
314 // Reads all memory pages
315 int CmdHF15DumpMem(const char*Cmd
) {
319 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
320 uint8_t *req
=c
.d
.asBytes
;
326 PrintAndLog("No Tag found.");
330 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL
,uid
));
331 PrintAndLog("Tag Info: %s",getTagInfo(uid
));
333 for (int retry
=0; retry
<5; retry
++) {
335 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
336 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
337 req
[1]=ISO15_CMD_READ
;
338 memcpy(&req
[2],uid
,8);
340 reqlen
=AddCrc(req
,11);
345 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
346 recv
= resp
.d
.asBytes
;
347 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
348 if (!(recv
[0] & ISO15_RES_ERROR
)) {
350 *output
=0; // reset outputstring
351 sprintf(output
, "Block %02x ",blocknum
);
352 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in hex
353 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
356 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) { // data in cleaned ascii
357 sprintf(output
+strlen(output
),"%c",(recv
[i
]>31 && recv
[i
]<127)?recv
[i
]:'.');
359 PrintAndLog("%s",output
);
361 // PrintAndLog("bn=%i",blocknum);
363 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
366 } // else PrintAndLog("crc");
367 } // else PrintAndLog("r null");
370 // if (resp.arg[0]<3)
371 // PrintAndLog("Lost Connection");
372 // else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
373 // PrintAndLog("CRC Failed");
375 // PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
382 static command_t CommandTable15
[] =
384 {"help", CmdHF15Help
, 1, "This help"},
385 {"demod", CmdHF15Demod
, 1, "Demodulate ISO15693 from tag"},
386 {"read", CmdHF15Read
, 0, "Read HF tag (ISO 15693)"},
387 {"record", CmdHF15Record
, 0, "Record Samples (ISO 15693)"}, // atrox
388 {"reader", CmdHF15Reader
, 0, "Act like an ISO15693 reader"},
389 {"sim", CmdHF15Sim
, 0, "Fake an ISO15693 tag"},
390 {"cmd", CmdHF15Cmd
, 0, "Send direct commands to ISO15693 tag"},
391 {"findafi", CmdHF15Afi
, 0, "Brute force AFI of an ISO15693 tag"},
392 {"dumpmemory", CmdHF15DumpMem
, 0, "Read all memory pages of an ISO15693 tag"},
393 {NULL
, NULL
, 0, NULL
}
396 int CmdHF15(const char *Cmd
)
398 CmdsParse(CommandTable15
, Cmd
);
402 int CmdHF15Help(const char *Cmd
)
404 CmdsHelp(CommandTable15
);
409 // "HF 15 Cmd" Interface
410 // Allows direct communication with the tag on command level
412 int CmdHF15CmdInquiry(const char *Cmd
)
416 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
417 uint8_t *req
=c
.d
.asBytes
;
420 req
[0]= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
421 ISO15_REQ_INVENTORY
| ISO15_REQINV_SLOT1
;
422 req
[1]=ISO15_CMD_INVENTORY
;
423 req
[2]=0; // mask length
424 reqlen
=AddCrc(req
,3);
429 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
430 if (resp
.arg
[0]>=12) {
431 recv
= resp
.d
.asBytes
;
432 PrintAndLog("UID=%s",sprintUID(NULL
,&recv
[2]));
433 PrintAndLog("Tag Info: %s",getTagInfo(&recv
[2]));
435 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp
.arg
[0]);
438 PrintAndLog("timeout.");
444 // Turns debugging on(1)/off(0)
445 int CmdHF15CmdDebug( const char *cmd
) {
448 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
449 PrintAndLog(" 0 no debugging");
450 PrintAndLog(" 1 turn debugging on");
454 UsbCommand c
= {CMD_ISO_15693_DEBUG
, {debug
, 0, 0}};
460 int CmdHF15CmdRaw (const char *cmd
) {
463 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
470 unsigned int datalen
=0, temp
;
475 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
476 PrintAndLog(" -r do not read response");
477 PrintAndLog(" -2 use slower '1 out of 256' mode");
478 PrintAndLog(" -c calculate and append CRC");
479 PrintAndLog(" Tip: turn on debugging for verbose output");
484 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
486 while (cmd
[i
]!='\0') {
487 if (cmd
[i
]==' ' || cmd
[i
]=='\t') { i
++; continue; }
502 PrintAndLog("Invalid option");
508 if ((cmd
[i
]>='0' && cmd
[i
]<='9') ||
509 (cmd
[i
]>='a' && cmd
[i
]<='f') ||
510 (cmd
[i
]>='A' && cmd
[i
]<='F') ) {
511 buf
[strlen(buf
)+1]=0;
512 buf
[strlen(buf
)]=cmd
[i
];
515 if (strlen(buf
)>=2) {
516 sscanf(buf
,"%x",&temp
);
517 data
[datalen
]=(uint8_t)(temp
& 0xff);
523 PrintAndLog("Invalid char on input");
526 if (crc
) datalen
=AddCrc(data
,datalen
);
531 memcpy(c
.d
.asBytes
,data
,datalen
);
536 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000)) {
537 recv
= resp
.d
.asBytes
;
538 PrintAndLog("received %i octets",resp
.arg
[0]);
539 hexout
= (char *)malloc(resp
.arg
[0] * 3 + 1);
540 if (hexout
!= NULL
) {
541 for (int i
= 0; i
< resp
.arg
[0]; i
++) { // data in hex
542 sprintf(&hexout
[i
* 3], "%02X ", recv
[i
]);
544 PrintAndLog("%s", hexout
);
548 PrintAndLog("timeout while waiting for reply.");
557 * parses common HF 15 CMD parameters and prepares some data structures
561 int prepareHF15Cmd(char **cmd
, UsbCommand
*c
, uint8_t iso15cmd
[], int iso15cmdlen
) {
563 uint8_t *req
=c
->d
.asBytes
;
564 uint8_t uid
[8] = {0x00};
568 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
570 if (strstr(*cmd
,"-2")==*cmd
) {
571 c
->arg
[1]=0; // use 1of256
576 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
578 if (strstr(*cmd
,"-o")==*cmd
) {
579 req
[reqlen
]=ISO15_REQ_OPTION
;
584 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
588 PrintAndLog("missing addr");
593 // you must have selected the tag earlier
594 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
595 ISO15_REQ_NONINVENTORY
| ISO15_REQ_SELECT
;
596 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
601 // unaddressed mode may not be supported by all vendors
602 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
603 ISO15_REQ_NONINVENTORY
;
604 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
608 // we scan for the UID ourself
609 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
610 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
611 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
614 PrintAndLog("No Tag found");
617 memcpy(req
+reqlen
,uid
,8);
618 PrintAndLog("Detected UID %s",sprintUID(NULL
,uid
));
622 req
[reqlen
++]|= ISO15_REQ_SUBCARRIER_SINGLE
| ISO15_REQ_DATARATE_HIGH
|
623 ISO15_REQ_NONINVENTORY
| ISO15_REQ_ADDRESS
;
624 memcpy(&req
[reqlen
],&iso15cmd
[0],iso15cmdlen
);
627 /* sscanf(cmd,"%hX%hX%hX%hX%hX%hX%hX%hX",
628 (short unsigned int *)&uid[7],(short unsigned int *)&uid[6],
629 (short unsigned int *)&uid[5],(short unsigned int *)&uid[4],
630 (short unsigned int *)&uid[3],(short unsigned int *)&uid[2],
631 (short unsigned int *)&uid[1],(short unsigned int *)&uid[0]); */
632 for (int i
=0;i
<8 && (*cmd
)[i
*2] && (*cmd
)[i
*2+1];i
++) { // parse UID
633 sscanf((char[]){(*cmd
)[i
*2],(*cmd
)[i
*2+1],0},"%X",&temp
);
637 PrintAndLog("Using UID %s",sprintUID(NULL
,uid
));
638 memcpy(&req
[reqlen
],&uid
[0],8);
641 // skip to next space
642 while (**cmd
!=' ' && **cmd
!='\t') (*cmd
)++;
643 // skip over the space
644 while (**cmd
==' ' || **cmd
=='\t') (*cmd
)++;
651 * Commandline handling: HF15 CMD SYSINFO
652 * get system information from tag/VICC
654 int CmdHF15CmdSysinfo(const char *Cmd
) {
657 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
658 uint8_t *req
=c
.d
.asBytes
;
662 char output
[2048]="";
669 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
670 PrintAndLog(" options:");
671 PrintAndLog(" -2 use slower '1 out of 256' mode");
672 PrintAndLog(" uid (either): ");
673 PrintAndLog(" <8B hex> full UID eg E011223344556677");
674 PrintAndLog(" s selected tag");
675 PrintAndLog(" u unaddressed mode");
676 PrintAndLog(" * scan for tag");
677 PrintAndLog(" start#: page number to start 0-255");
678 PrintAndLog(" count#: number of pages");
682 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_SYSINFO
},1);
685 reqlen
=AddCrc(req
,reqlen
);
690 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
691 recv
= resp
.d
.asBytes
;
692 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
693 if (!(recv
[0] & ISO15_RES_ERROR
)) {
694 *output
=0; // reset outputstring
695 for ( i
=1; i
<resp
.arg
[0]-2; i
++) {
696 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
698 strcat(output
,"\n\r");
699 strcat(output
,"UID = ");
700 strcat(output
,sprintUID(NULL
,recv
+2));
701 strcat(output
,"\n\r");
702 strcat(output
,getTagInfo(recv
+2)); //ABC
703 strcat(output
,"\n\r");
706 sprintf(output
+strlen(output
),"DSFID supported, set to %02X\n\r",recv
[i
++]);
708 strcat(output
,"DSFID not supported\n\r");
710 sprintf(output
+strlen(output
),"AFI supported, set to %03X\n\r",recv
[i
++]);
712 strcat(output
,"AFI not supported\n\r");
713 if (recv
[1] & 0x04) {
714 strcat(output
,"Tag provides info on memory layout (vendor dependent)\n\r");
715 sprintf(output
+strlen(output
)," %i (or %i) bytes/page x %i pages \n\r",
716 (recv
[i
+1]&0x1F)+1, (recv
[i
+1]&0x1F), recv
[i
]+1);
719 strcat(output
,"Tag does not provide information on memory layout\n\r");
720 if (recv
[1] & 0x08) sprintf(output
+strlen(output
),"IC reference given: %02X\n\r",recv
[i
++]);
721 else strcat(output
,"IC reference not given\n\r");
724 PrintAndLog("%s",output
);
726 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
729 PrintAndLog("CRC failed");
732 PrintAndLog("timeout: no answer");
739 * Commandline handling: HF15 CMD READMULTI
740 * Read multiple blocks at once (not all tags support this)
742 int CmdHF15CmdReadmulti(const char *Cmd
) {
745 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
746 uint8_t *req
=c
.d
.asBytes
;
747 int reqlen
=0, pagenum
,pagecount
;
750 char output
[2048]="";
756 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
757 PrintAndLog(" options:");
758 PrintAndLog(" -2 use slower '1 out of 256' mode");
759 PrintAndLog(" uid (either): ");
760 PrintAndLog(" <8B hex> full UID eg E011223344556677");
761 PrintAndLog(" s selected tag");
762 PrintAndLog(" u unaddressed mode");
763 PrintAndLog(" * scan for tag");
764 PrintAndLog(" start#: page number to start 0-255");
765 PrintAndLog(" count#: number of pages");
769 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READMULTI
},1);
772 pagenum
=strtol(cmd
,NULL
,0);
774 // skip to next space
775 while (*cmd
!=' ' && *cmd
!='\t') cmd
++;
776 // skip over the space
777 while (*cmd
==' ' || *cmd
=='\t') cmd
++;
779 pagecount
=strtol(cmd
,NULL
,0);
780 if (pagecount
>0) pagecount
--; // 0 means 1 page, 1 means 2 pages, ...
782 req
[reqlen
++]=(uint8_t)pagenum
;
783 req
[reqlen
++]=(uint8_t)pagecount
;
785 reqlen
=AddCrc(req
,reqlen
);
791 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
792 recv
= resp
.d
.asBytes
;
793 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
794 if (!(recv
[0] & ISO15_RES_ERROR
)) {
795 *output
=0; // reset outputstring
796 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
797 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
800 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
801 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
803 PrintAndLog("%s",output
);
805 PrintAndLog("Tag returned Error %i: %s",recv
[0],TagErrorStr(recv
[0]));
808 PrintAndLog("CRC failed");
811 PrintAndLog("no answer");
818 * Commandline handling: HF15 CMD READ
819 * Reads a single Block
821 int CmdHF15CmdRead(const char *Cmd
) {
824 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
825 uint8_t *req
=c
.d
.asBytes
;
826 int reqlen
=0, pagenum
;
835 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
836 PrintAndLog(" options:");
837 PrintAndLog(" -2 use slower '1 out of 256' mode");
838 PrintAndLog(" uid (either): ");
839 PrintAndLog(" <8B hex> full UID eg E011223344556677");
840 PrintAndLog(" s selected tag");
841 PrintAndLog(" u unaddressed mode");
842 PrintAndLog(" * scan for tag");
843 PrintAndLog(" page#: page number 0-255");
847 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_READ
},1);
850 pagenum
=strtol(cmd
,NULL
,0);
852 PrintAndLog("invalid pagenum");
856 req
[reqlen
++]=(uint8_t)pagenum
;
858 reqlen
=AddCrc(req
,reqlen
);
864 if (WaitForResponseTimeout(CMD_ACK
,&resp
,1000) && resp
.arg
[0]>2) {
865 recv
= resp
.d
.asBytes
;
866 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
867 if (!(recv
[0] & ISO15_RES_ERROR
)) {
868 *output
=0; // reset outputstring
869 //sprintf(output, "Block %2i ",blocknum);
870 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
871 sprintf(output
+strlen(output
),"%02X ",recv
[i
]);
874 for ( int i
=1; i
<resp
.arg
[0]-2; i
++) {
875 sprintf(output
+strlen(output
),"%c",recv
[i
]>31 && recv
[i
]<127?recv
[i
]:'.');
877 PrintAndLog("%s",output
);
879 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
882 PrintAndLog("CRC failed");
885 PrintAndLog("no answer");
893 * Commandline handling: HF15 CMD WRITE
894 * Writes a single Block - might run into timeout, even when successful
896 int CmdHF15CmdWrite(const char *Cmd
) {
899 UsbCommand c
= {CMD_ISO_15693_COMMAND
, {0, 1, 1}}; // len,speed,recv?
900 uint8_t *req
=c
.d
.asBytes
;
901 int reqlen
=0, pagenum
, temp
;
910 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
911 PrintAndLog(" options:");
912 PrintAndLog(" -2 use slower '1 out of 256' mode");
913 PrintAndLog(" -o set OPTION Flag (needed for TI)");
914 PrintAndLog(" uid (either): ");
915 PrintAndLog(" <8B hex> full UID eg E011223344556677");
916 PrintAndLog(" s selected tag");
917 PrintAndLog(" u unaddressed mode");
918 PrintAndLog(" * scan for tag");
919 PrintAndLog(" page#: page number 0-255");
920 PrintAndLog(" hexdata: data to be written eg AA BB CC DD");
924 prepareHF15Cmd(&cmd
, &c
,(uint8_t[]){ISO15_CMD_WRITE
},1);
927 // *cmd -> page num ; *cmd2 -> data
929 while (*cmd2
!=' ' && *cmd2
!='\t' && *cmd2
) cmd2
++;
933 pagenum
=strtol(cmd
,NULL
,0);
935 PrintAndLog("invalid pagenum");
938 req
[reqlen
++]=(uint8_t)pagenum
;
941 while (cmd2
[0] && cmd2
[1]) { // hexdata, read by 2 hexchars
946 sscanf((char[]){cmd2
[0],cmd2
[1],0},"%X",&temp
);
947 req
[reqlen
++]=temp
& 0xff;
951 reqlen
=AddCrc(req
,reqlen
);
957 if (WaitForResponseTimeout(CMD_ACK
,&resp
,2000) && resp
.arg
[0]>2) {
958 recv
= resp
.d
.asBytes
;
959 if (ISO15_CRC_CHECK
==Crc(recv
,resp
.arg
[0])) {
960 if (!(recv
[0] & ISO15_RES_ERROR
)) {
963 PrintAndLog("Tag returned Error %i: %s",recv
[1],TagErrorStr(recv
[1]));
966 PrintAndLog("CRC failed");
969 PrintAndLog("timeout: no answer - data may be written anyway");
977 static command_t CommandTable15Cmd
[] =
979 {"help", CmdHF15CmdHelp
, 1, "This Help"},
980 {"inquiry", CmdHF15CmdInquiry
, 0, "Search for tags in range"},
982 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
984 {"read", CmdHF15CmdRead
, 0, "Read a block"},
985 {"write", CmdHF15CmdWrite
, 0, "Write a block"},
986 {"readmulti",CmdHF15CmdReadmulti
, 0, "Reads multiple Blocks"},
987 {"sysinfo",CmdHF15CmdSysinfo
, 0, "Get Card Information"},
988 {"raw", CmdHF15CmdRaw
, 0, "Send raw hex data to tag"},
989 {"debug", CmdHF15CmdDebug
, 0, "Turn debugging on/off"},
990 {NULL
, NULL
, 0, NULL
}
993 int CmdHF15Cmd(const char *Cmd
)
995 CmdsParse(CommandTable15Cmd
, Cmd
);
999 int CmdHF15CmdHelp(const char *Cmd
)
1001 CmdsHelp(CommandTable15Cmd
);