]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf15.c
Documentation to apps.h, documentation/renaming to iclass
[proxmark3-svn] / client / cmdhf15.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
0546b4aa 3// Modified 2010-2012 by <adrian -at- atrox.at>
65a23af2 4// Modified 2012 by <vsza at vsza.hu>
a553f267 5//
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
8// the license.
9//-----------------------------------------------------------------------------
10// High frequency ISO15693 commands
11//-----------------------------------------------------------------------------
9455b51c 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.
14// Mode 1:
15// All steps are done on the proxmark, the output of the commands is returned via
16// USB-debug-print commands.
17// Mode 2:
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
20// Mode 3:
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.
a553f267 24
7fe9b0b7 25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <stdint.h>
3fe4ff4f 29
902cb3c0 30#include "proxmark3.h"
7fe9b0b7 31#include "data.h"
32#include "graph.h"
33#include "ui.h"
3fe4ff4f 34#include "util.h"
7fe9b0b7 35#include "cmdparser.h"
36#include "cmdhf15.h"
9455b51c 37#include "iso15693tools.h"
38#include "cmdmain.h"
39
40#define FrameSOF Iso15693FrameSOF
41#define Logic0 Iso15693Logic0
42#define Logic1 Iso15693Logic1
43#define FrameEOF Iso15693FrameEOF
44
45#define Crc(data,datalen) Iso15693Crc(data,datalen)
46#define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
47#define sprintUID(target,uid) Iso15693sprintUID(target,uid)
7fe9b0b7 48
9455b51c 49// structure and database for uid -> tagtype lookups
50typedef struct {
51 uint64_t uid;
52 int mask; // how many MSB bits used
53 char* desc;
54} productName;
55
56
57const productName uidmapping[] = {
0546b4aa 58 // UID, #significant Bits, "Vendor(+Product)"
c71e7235 59 { 0xE001000000000000LL, 16, "Motorola" },
60 { 0xE002000000000000LL, 16, "ST Microelectronics" },
61 { 0xE003000000000000LL, 16, "Hitachi" },
3fe4ff4f 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)" },
c71e7235 66 { 0xE005000000000000LL, 16, "Infineon" },
67 { 0xE005400000000000LL, 24, "Infineon; 56x32bit" },
68 { 0xE006000000000000LL, 16, "Cylinc" },
9455b51c 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" },
0546b4aa 72 { 0xE007800000000000LL, 23, "Texas Instrument; Tag-it HF-I Plus (RF-HDT-DVBB tag or Third Party Products)" },
9455b51c 73 { 0xE007C00000000000LL, 23, "Texas Instrument; Tag-it HF-I Standard; 8x32bit" },
74 { 0xE007C40000000000LL, 23, "Texas Instrument; Tag-it HF-I Pro; 8x23bit; password" },
c71e7235 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" },
9455b51c 85 { 0xE016000000000000LL, 16, "EM-Marin SA (Skidata)" },
0546b4aa 86 { 0xE016040000000000LL, 24, "EM-Marin SA (Skidata Keycard-eco); EM4034? no 'read', just 'readmulti'" },
87 { 0xE0160c0000000000LL, 24, "EM-Marin SA; EM4035?" },
9455b51c 88 { 0xE016100000000000LL, 24, "EM-Marin SA (Skidata); EM4135; 36x64bit start page 13" },
0546b4aa 89 { 0xE016940000000000LL, 24, "EM-Marin SA (Skidata); 51x64bit" },
9455b51c 90 { 0,0,"no tag-info available" } // must be the last entry
91};
92
93
e4da8ed0 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
9455b51c 96// returns 1 if suceeded
97int getUID(uint8_t *buf)
7fe9b0b7 98{
902cb3c0 99 UsbCommand resp;
9455b51c 100 uint8_t *recv;
101 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
102 uint8_t *req=c.d.asBytes;
103 int reqlen=0;
104
105 for (int retry=0;retry<3; retry++) { // don't give up the at the first try
106
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);
112 c.arg[0]=reqlen;
113
114 SendCommand(&c);
115
902cb3c0 116 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
117 recv = resp.d.asBytes;
118 if (resp.arg[0]>=12 && ISO15_CRC_CHECK==Crc(recv,12)) {
9455b51c 119 memcpy(buf,&recv[2],8);
120 return 1;
121 }
122 }
123 } // retry
124 return 0;
125}
126
127
7fe9b0b7 128
9455b51c 129// get a product description based on the UID
130// uid[8] tag uid
131// returns description of the best match
132static char* getTagInfo(uint8_t *uid) {
31b6e9af 133 uint64_t myuid,mask;
9455b51c 134 int i=0, best=-1;
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) {
139 if (best==-1) {
140 best=i;
141 } else {
142 if (uidmapping[i].mask>uidmapping[best].mask) {
143 best=i;
144 }
145 }
146 }
147 i++;
148 }
7fe9b0b7 149
9455b51c 150 if (best>=0) return uidmapping[best].desc;
151
152 return uidmapping[i].desc;
7fe9b0b7 153}
154
9455b51c 155
b4a9d841 156// return a clear-text message to an errorcode
9455b51c 157static char* TagErrorStr(uint8_t error) {
b4a9d841 158 switch (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.";
169 }
9455b51c 170}
171
172
173// Mode 3
7fe9b0b7 174int CmdHF15Demod(const char *Cmd)
175{
9455b51c 176 // The sampling rate is 106.353 ksps/s, for T = 18.8 us
177
178 int i, j;
179 int max = 0, maxPos = 0;
180
181 int skip = 4;
182
183 if (GraphTraceLen < 1000) return 0;
184
185 // First, correlate for SOF
186 for (i = 0; i < 100; i++) {
187 int corr = 0;
188 for (j = 0; j < arraylen(FrameSOF); j += skip) {
189 corr += FrameSOF[j] * GraphBuffer[i + (j / skip)];
190 }
191 if (corr > max) {
192 max = corr;
193 maxPos = i;
194 }
195 }
196 PrintAndLog("SOF at %d, correlation %d", maxPos,
197 max / (arraylen(FrameSOF) / skip));
198
199 i = maxPos + arraylen(FrameSOF) / skip;
200 int k = 0;
201 uint8_t outBuf[20];
202 memset(outBuf, 0, sizeof(outBuf));
203 uint8_t mask = 0x01;
204 for (;;) {
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)];
208 }
209 for (j = 0; j < arraylen(Logic1); j += skip) {
210 corr1 += Logic1[j] * GraphBuffer[i + (j / skip)];
211 }
212 for (j = 0; j < arraylen(FrameEOF); j += skip) {
213 corrEOF += FrameEOF[j] * GraphBuffer[i + (j / skip)];
214 }
215 // Even things out by the length of the target waveform.
216 corr0 *= 4;
217 corr1 *= 4;
218
219 if (corrEOF > corr1 && corrEOF > corr0) {
220 PrintAndLog("EOF at %d", i);
221 break;
222 } else if (corr1 > corr0) {
223 i += arraylen(Logic1) / skip;
224 outBuf[k] |= mask;
225 } else {
226 i += arraylen(Logic0) / skip;
227 }
228 mask <<= 1;
229 if (mask == 0) {
230 k++;
231 mask = 0x01;
232 }
233 if ((i + (int)arraylen(FrameEOF)) >= GraphTraceLen) {
234 PrintAndLog("ran off end!");
235 break;
236 }
237 }
238 if (mask != 0x01) {
239 PrintAndLog("error, uneven octet! (discard extra bits!)");
240 PrintAndLog(" mask=%02x", mask);
241 }
242 PrintAndLog("%d octets", k);
243
244 for (i = 0; i < k; i++) {
245 PrintAndLog("# %2d: %02x ", i, outBuf[i]);
246 }
247 PrintAndLog("CRC=%04x", Iso15693Crc(outBuf, k - 2));
248 return 0;
7fe9b0b7 249}
250
9455b51c 251
252
253// * Acquire Samples as Reader (enables carrier, sends inquiry)
7fe9b0b7 254int CmdHF15Read(const char *Cmd)
255{
9455b51c 256 UsbCommand c = {CMD_ACQUIRE_RAW_ADC_SAMPLES_ISO_15693};
257 SendCommand(&c);
258 return 0;
259}
260
261// Record Activity without enabeling carrier
262int CmdHF15Record(const char *Cmd)
263{
264 UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693};
265 SendCommand(&c);
266 return 0;
7fe9b0b7 267}
268
269int CmdHF15Reader(const char *Cmd)
270{
9455b51c 271 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
272 SendCommand(&c);
273 return 0;
7fe9b0b7 274}
275
9455b51c 276// Simulation is still not working very good
7fe9b0b7 277int CmdHF15Sim(const char *Cmd)
278{
3fe4ff4f 279 char cmdp = param_getchar(Cmd, 0);
280 uint8_t uid[8] = {0x00};
281
282 //E0 16 24 00 00 00 00 00
283 if (cmdp == 'h' || cmdp == 'H') {
284 PrintAndLog("Usage: hf 15 sim <UID>");
285 PrintAndLog("");
286 PrintAndLog(" sample: hf 15 sim E016240000000000");
287 return 0;
288 }
289
290 if (param_gethex(Cmd, 0, uid, 16)) {
291 PrintAndLog("UID must include 16 HEX symbols");
292 return 0;
293 }
294
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]);
297
298 UsbCommand c = {CMD_SIMTAG_ISO_15693, {0, 0, 0}};
299 memcpy(c.d.asBytes,uid,8);
300
9455b51c 301 SendCommand(&c);
302 return 0;
7fe9b0b7 303}
304
9455b51c 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)
307int CmdHF15Afi(const char *Cmd)
7fe9b0b7 308{
9455b51c 309 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
310 SendCommand(&c);
311 return 0;
312}
313
314// Reads all memory pages
315int CmdHF15DumpMem(const char*Cmd) {
902cb3c0 316 UsbCommand resp;
9455b51c 317 uint8_t uid[8];
318 uint8_t *recv=NULL;
319 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
320 uint8_t *req=c.d.asBytes;
321 int reqlen=0;
322 int blocknum=0;
323 char output[80];
324
325 if (!getUID(uid)) {
326 PrintAndLog("No Tag found.");
327 return 0;
328 }
329
330 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
331 PrintAndLog("Tag Info: %s",getTagInfo(uid));
332
333 for (int retry=0; retry<5; retry++) {
334
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);
339 req[10]=blocknum;
340 reqlen=AddCrc(req,11);
341 c.arg[0]=reqlen;
342
343 SendCommand(&c);
344
902cb3c0 345 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
346 recv = resp.d.asBytes;
347 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
9455b51c 348 if (!(recv[0] & ISO15_RES_ERROR)) {
349 retry=0;
350 *output=0; // reset outputstring
3fe4ff4f 351 sprintf(output, "Block %02x ",blocknum);
902cb3c0 352 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
7bb9d33e 353 sprintf(output+strlen(output),"%02X ",recv[i]);
9455b51c 354 }
355 strcat(output," ");
902cb3c0 356 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
9455b51c 357 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
358 }
359 PrintAndLog("%s",output);
360 blocknum++;
361 // PrintAndLog("bn=%i",blocknum);
362 } else {
b4a9d841 363 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 364 return 0;
365 }
366 } // else PrintAndLog("crc");
367 } // else PrintAndLog("r null");
9455b51c 368 } // retry
902cb3c0 369 // TODO: need fix
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");
374// else
375// PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 376 return 0;
377}
378
379
380// "HF 15" interface
381
382static command_t CommandTable15[] =
383{
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}
7fe9b0b7 394};
395
396int CmdHF15(const char *Cmd)
397{
9455b51c 398 CmdsParse(CommandTable15, Cmd);
399 return 0;
7fe9b0b7 400}
401
9455b51c 402int CmdHF15Help(const char *Cmd)
7fe9b0b7 403{
9455b51c 404 CmdsHelp(CommandTable15);
405 return 0;
7fe9b0b7 406}
9455b51c 407
408
409// "HF 15 Cmd" Interface
410// Allows direct communication with the tag on command level
411
412int CmdHF15CmdInquiry(const char *Cmd)
413{
902cb3c0 414 UsbCommand resp;
9455b51c 415 uint8_t *recv;
416 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
417 uint8_t *req=c.d.asBytes;
418 int reqlen=0;
419
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);
425 c.arg[0]=reqlen;
426
427 SendCommand(&c);
428
902cb3c0 429 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
430 if (resp.arg[0]>=12) {
431 recv = resp.d.asBytes;
9455b51c 432 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
433 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
434 } else {
902cb3c0 435 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
9455b51c 436 }
437 } else {
438 PrintAndLog("timeout.");
439 }
440 return 0;
441}
442
443
444// Turns debugging on(1)/off(0)
445int CmdHF15CmdDebug( const char *cmd) {
446 int debug=atoi(cmd);
447 if (strlen(cmd)<1) {
3fe4ff4f 448 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
449 PrintAndLog(" 0 no debugging");
450 PrintAndLog(" 1 turn debugging on");
9455b51c 451 return 0;
452 }
453
454 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
455 SendCommand(&c);
456 return 0;
457}
458
459
460int CmdHF15CmdRaw (const char *cmd) {
902cb3c0 461 UsbCommand resp;
9455b51c 462 uint8_t *recv;
463 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
464 int reply=1;
465 int fast=1;
466 int crc=0;
467 char buf[5]="";
468 int i=0;
469 uint8_t data[100];
470 unsigned int datalen=0, temp;
fdb67f1c 471 char *hexout;
9455b51c 472
473
474 if (strlen(cmd)<3) {
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");
480 return 0;
481 }
482
483 // strip
484 while (*cmd==' ' || *cmd=='\t') cmd++;
485
486 while (cmd[i]!='\0') {
487 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
488 if (cmd[i]=='-') {
489 switch (cmd[i+1]) {
490 case 'r':
491 case 'R':
492 reply=0;
493 break;
494 case '2':
495 fast=0;
496 break;
497 case 'c':
498 case 'C':
499 crc=1;
500 break;
501 default:
502 PrintAndLog("Invalid option");
503 return 0;
504 }
505 i+=2;
506 continue;
507 }
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];
513 i++;
514
515 if (strlen(buf)>=2) {
516 sscanf(buf,"%x",&temp);
517 data[datalen]=(uint8_t)(temp & 0xff);
518 datalen++;
519 *buf=0;
520 }
521 continue;
522 }
523 PrintAndLog("Invalid char on input");
524 return 0;
525 }
526 if (crc) datalen=AddCrc(data,datalen);
527
528 c.arg[0]=datalen;
529 c.arg[1]=fast;
530 c.arg[2]=reply;
531 memcpy(c.d.asBytes,data,datalen);
532
533 SendCommand(&c);
534
535 if (reply) {
902cb3c0 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);
fdb67f1c 540 if (hexout != NULL) {
902cb3c0 541 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
7bb9d33e 542 sprintf(&hexout[i * 3], "%02X ", recv[i]);
fdb67f1c 543 }
544 PrintAndLog("%s", hexout);
545 free(hexout);
546 }
9455b51c 547 } else {
548 PrintAndLog("timeout while waiting for reply.");
549 }
550
551 } // if reply
552 return 0;
553}
554
555
0546b4aa 556/**
557 * parses common HF 15 CMD parameters and prepares some data structures
558 * Parameters:
559 * **cmd command line
560 */
9455b51c 561int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
562 int temp;
90e278d3 563 uint8_t *req=c->d.asBytes;
3fe4ff4f 564 uint8_t uid[8] = {0x00};
9455b51c 565 uint32_t reqlen=0;
566
567 // strip
568 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
569
570 if (strstr(*cmd,"-2")==*cmd) {
c43897de 571 c->arg[1]=0; // use 1of256
9455b51c 572 (*cmd)+=2;
573 }
574
575 // strip
576 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
577
578 if (strstr(*cmd,"-o")==*cmd) {
579 req[reqlen]=ISO15_REQ_OPTION;
580 (*cmd)+=2;
581 }
582
583 // strip
584 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
585
586 switch (**cmd) {
587 case 0:
588 PrintAndLog("missing addr");
589 return 0;
590 break;
591 case 's':
592 case 'S':
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);
597 reqlen+=iso15cmdlen;
598 break;
599 case 'u':
600 case 'U':
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);
605 reqlen+=iso15cmdlen;
606 break;
607 case '*':
65a23af2 608 // we scan for the UID ourself
9455b51c 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);
612 reqlen+=iso15cmdlen;
613 if (!getUID(uid)) {
614 PrintAndLog("No Tag found");
615 return 0;
616 }
617 memcpy(req+reqlen,uid,8);
618 PrintAndLog("Detected UID %s",sprintUID(NULL,uid));
619 reqlen+=8;
620 break;
621 default:
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);
625 reqlen+=iso15cmdlen;
626
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);
634 uid[7-i]=temp&0xff;
635 }
636
637 PrintAndLog("Using UID %s",sprintUID(NULL,uid));
638 memcpy(&req[reqlen],&uid[0],8);
639 reqlen+=8;
640 }
641 // skip to next space
642 while (**cmd!=' ' && **cmd!='\t') (*cmd)++;
643 // skip over the space
644 while (**cmd==' ' || **cmd=='\t') (*cmd)++;
645
646 c->arg[0]=reqlen;
647 return 1;
648}
649
6d7234cd 650/**
651 * Commandline handling: HF15 CMD SYSINFO
652 * get system information from tag/VICC
653 */
654int CmdHF15CmdSysinfo(const char *Cmd) {
902cb3c0 655 UsbCommand resp;
6d7234cd 656 uint8_t *recv;
657 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
658 uint8_t *req=c.d.asBytes;
659 int reqlen=0;
660 char cmdbuf[100];
661 char *cmd=cmdbuf;
662 char output[2048]="";
663 int i;
664
665 strncpy(cmd,Cmd,99);
666
667 // usage:
668 if (strlen(cmd)<1) {
0546b4aa 669 PrintAndLog("Usage: hf 15 cmd sysinfo [options] <uid|s|u|*>");
6d7234cd 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");
679 return 0;
680 }
681
682 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_SYSINFO},1);
683 reqlen=c.arg[0];
684
685 reqlen=AddCrc(req,reqlen);
686 c.arg[0]=reqlen;
687
688 SendCommand(&c);
689
902cb3c0 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])) {
6d7234cd 693 if (!(recv[0] & ISO15_RES_ERROR)) {
694 *output=0; // reset outputstring
902cb3c0 695 for ( i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 696 sprintf(output+strlen(output),"%02X ",recv[i]);
6d7234cd 697 }
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");
704 i=10;
705 if (recv[1] & 0x01)
7bb9d33e 706 sprintf(output+strlen(output),"DSFID supported, set to %02X\n\r",recv[i++]);
6d7234cd 707 else
708 strcat(output,"DSFID not supported\n\r");
709 if (recv[1] & 0x02)
7bb9d33e 710 sprintf(output+strlen(output),"AFI supported, set to %03X\n\r",recv[i++]);
6d7234cd 711 else
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);
717 i+=2;
718 } else
719 strcat(output,"Tag does not provide information on memory layout\n\r");
7bb9d33e 720 if (recv[1] & 0x08) sprintf(output+strlen(output),"IC reference given: %02X\n\r",recv[i++]);
6d7234cd 721 else strcat(output,"IC reference not given\n\r");
722
723
724 PrintAndLog("%s",output);
725 } else {
726 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
727 }
728 } else {
729 PrintAndLog("CRC failed");
730 }
731 } else {
eba61a56 732 PrintAndLog("timeout: no answer");
6d7234cd 733 }
734
735 return 0;
736}
737
7853775e 738/**
739 * Commandline handling: HF15 CMD READMULTI
740 * Read multiple blocks at once (not all tags support this)
741 */
742int CmdHF15CmdReadmulti(const char *Cmd) {
902cb3c0 743 UsbCommand resp;
7853775e 744 uint8_t *recv;
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;
748 char cmdbuf[100];
749 char *cmd=cmdbuf;
750 char output[2048]="";
751
752 strncpy(cmd,Cmd,99);
753
754 // usage:
755 if (strlen(cmd)<3) {
0546b4aa 756 PrintAndLog("Usage: hf 15 cmd readmulti [options] <uid|s|u|*> <start#> <count#>");
7853775e 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");
766 return 0;
767 }
768
769 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READMULTI},1);
770 reqlen=c.arg[0];
771
772 pagenum=strtol(cmd,NULL,0);
773
774 // skip to next space
775 while (*cmd!=' ' && *cmd!='\t') cmd++;
776 // skip over the space
777 while (*cmd==' ' || *cmd=='\t') cmd++;
778
779 pagecount=strtol(cmd,NULL,0);
780 if (pagecount>0) pagecount--; // 0 means 1 page, 1 means 2 pages, ...
781
782 req[reqlen++]=(uint8_t)pagenum;
783 req[reqlen++]=(uint8_t)pagecount;
784
785 reqlen=AddCrc(req,reqlen);
786
787 c.arg[0]=reqlen;
788
789 SendCommand(&c);
0546b4aa 790
902cb3c0 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])) {
7853775e 794 if (!(recv[0] & ISO15_RES_ERROR)) {
795 *output=0; // reset outputstring
902cb3c0 796 for ( int i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 797 sprintf(output+strlen(output),"%02X ",recv[i]);
7853775e 798 }
799 strcat(output," ");
902cb3c0 800 for ( int i=1; i<resp.arg[0]-2; i++) {
7853775e 801 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
802 }
803 PrintAndLog("%s",output);
804 } else {
805 PrintAndLog("Tag returned Error %i: %s",recv[0],TagErrorStr(recv[0]));
806 }
807 } else {
808 PrintAndLog("CRC failed");
809 }
810 } else {
811 PrintAndLog("no answer");
812 }
813
814 return 0;
815}
816
05151b6f 817/**
818 * Commandline handling: HF15 CMD READ
819 * Reads a single Block
820 */
9455b51c 821int CmdHF15CmdRead(const char *Cmd) {
902cb3c0 822 UsbCommand resp;
9455b51c 823 uint8_t *recv;
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;
827 char cmdbuf[100];
828 char *cmd=cmdbuf;
829 char output[100]="";
830
831 strncpy(cmd,Cmd,99);
832
833 // usage:
834 if (strlen(cmd)<3) {
0546b4aa 835 PrintAndLog("Usage: hf 15 cmd read [options] <uid|s|u|*> <page#>");
9455b51c 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");
844 return 0;
845 }
846
847 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_READ},1);
848 reqlen=c.arg[0];
849
850 pagenum=strtol(cmd,NULL,0);
851 /*if (pagenum<0) {
852 PrintAndLog("invalid pagenum");
853 return 0;
854 } */
855
856 req[reqlen++]=(uint8_t)pagenum;
857
858 reqlen=AddCrc(req,reqlen);
859
860 c.arg[0]=reqlen;
861
862 SendCommand(&c);
863
902cb3c0 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])) {
9455b51c 867 if (!(recv[0] & ISO15_RES_ERROR)) {
868 *output=0; // reset outputstring
869 //sprintf(output, "Block %2i ",blocknum);
902cb3c0 870 for ( int i=1; i<resp.arg[0]-2; i++) {
7bb9d33e 871 sprintf(output+strlen(output),"%02X ",recv[i]);
9455b51c 872 }
873 strcat(output," ");
902cb3c0 874 for ( int i=1; i<resp.arg[0]-2; i++) {
e8da7740 875 sprintf(output+strlen(output),"%c",recv[i]>31 && recv[i]<127?recv[i]:'.');
9455b51c 876 }
877 PrintAndLog("%s",output);
878 } else {
b4a9d841 879 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 880 }
881 } else {
882 PrintAndLog("CRC failed");
883 }
884 } else {
885 PrintAndLog("no answer");
886 }
887
888 return 0;
889}
890
891
05151b6f 892/**
893 * Commandline handling: HF15 CMD WRITE
894 * Writes a single Block - might run into timeout, even when successful
895 */
9455b51c 896int CmdHF15CmdWrite(const char *Cmd) {
902cb3c0 897 UsbCommand resp;
9455b51c 898 uint8_t *recv;
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;
902 char cmdbuf[100];
903 char *cmd=cmdbuf;
904 char *cmd2;
905
906 strncpy(cmd,Cmd,99);
907
908 // usage:
909 if (strlen(cmd)<3) {
0546b4aa 910 PrintAndLog("Usage: hf 15 cmd write [options] <uid|s|u|*> <page#> <hexdata>");
9455b51c 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");
921 return 0;
922 }
923
924 prepareHF15Cmd(&cmd, &c,(uint8_t[]){ISO15_CMD_WRITE},1);
925 reqlen=c.arg[0];
926
927 // *cmd -> page num ; *cmd2 -> data
928 cmd2=cmd;
929 while (*cmd2!=' ' && *cmd2!='\t' && *cmd2) cmd2++;
930 *cmd2=0;
931 cmd2++;
932
933 pagenum=strtol(cmd,NULL,0);
934 /*if (pagenum<0) {
935 PrintAndLog("invalid pagenum");
936 return 0;
937 } */
938 req[reqlen++]=(uint8_t)pagenum;
939
940
941 while (cmd2[0] && cmd2[1]) { // hexdata, read by 2 hexchars
942 if (*cmd2==' ') {
943 cmd2++;
944 continue;
945 }
946 sscanf((char[]){cmd2[0],cmd2[1],0},"%X",&temp);
947 req[reqlen++]=temp & 0xff;
948 cmd2+=2;
949 }
950
951 reqlen=AddCrc(req,reqlen);
952
953 c.arg[0]=reqlen;
954
955 SendCommand(&c);
956
902cb3c0 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])) {
9455b51c 960 if (!(recv[0] & ISO15_RES_ERROR)) {
961 PrintAndLog("OK");
962 } else {
b4a9d841 963 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 964 }
965 } else {
966 PrintAndLog("CRC failed");
967 }
968 } else {
65a23af2 969 PrintAndLog("timeout: no answer - data may be written anyway");
9455b51c 970 }
971
972 return 0;
973}
974
975
976
977static command_t CommandTable15Cmd[] =
978{
979 {"help", CmdHF15CmdHelp, 1, "This Help"},
980 {"inquiry", CmdHF15CmdInquiry, 0, "Search for tags in range"},
981 /*
982 {"select", CmdHF15CmdSelect, 0, "Select an tag with a specific UID for further commands"},
983 */
984 {"read", CmdHF15CmdRead, 0, "Read a block"},
985 {"write", CmdHF15CmdWrite, 0, "Write a block"},
9455b51c 986 {"readmulti",CmdHF15CmdReadmulti, 0, "Reads multiple Blocks"},
0546b4aa 987 {"sysinfo",CmdHF15CmdSysinfo, 0, "Get Card Information"},
9455b51c 988 {"raw", CmdHF15CmdRaw, 0, "Send raw hex data to tag"},
989 {"debug", CmdHF15CmdDebug, 0, "Turn debugging on/off"},
990 {NULL, NULL, 0, NULL}
991};
992
993int CmdHF15Cmd(const char *Cmd)
994{
995 CmdsParse(CommandTable15Cmd, Cmd);
996 return 0;
997}
998
999int CmdHF15CmdHelp(const char *Cmd)
1000{
1001 CmdsHelp(CommandTable15Cmd);
1002 return 0;
1003}
1004
Impressum, Datenschutz