]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf15.c
FIX: Another try to see if the "lf em4x 410xsim" becomes better, added the clock...
[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>
28fdb04f 29//#include "proxusb.h"
902cb3c0 30#include "proxmark3.h"
7fe9b0b7 31#include "data.h"
32#include "graph.h"
33#include "ui.h"
34#include "cmdparser.h"
35#include "cmdhf15.h"
f38a1528 36#include "../common/iso15693tools.h"
9455b51c 37#include "cmdmain.h"
38
39#define FrameSOF Iso15693FrameSOF
40#define Logic0 Iso15693Logic0
41#define Logic1 Iso15693Logic1
42#define FrameEOF Iso15693FrameEOF
43
44#define Crc(data,datalen) Iso15693Crc(data,datalen)
45#define AddCrc(data,datalen) Iso15693AddCrc(data,datalen)
46#define sprintUID(target,uid) Iso15693sprintUID(target,uid)
7bd30f12 47#define TRACE_BUFF_SIZE 12000
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" },
c2d25819 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);
7bd30f12 258
259 uint8_t data[TRACE_BUFF_SIZE] = {0x00};
260
261 GetFromBigBuf(data,TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
262 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
263
264 for (int j = 0; j < TRACE_BUFF_SIZE; j++) {
265 GraphBuffer[j] = ((int)data[j]) ;
266 }
267 GraphTraceLen = TRACE_BUFF_SIZE;
268 RepaintGraphWindow();
9455b51c 269 return 0;
270}
271
272// Record Activity without enabeling carrier
273int CmdHF15Record(const char *Cmd)
274{
275 UsbCommand c = {CMD_RECORD_RAW_ADC_SAMPLES_ISO_15693};
276 SendCommand(&c);
7bd30f12 277
278 uint8_t data[TRACE_BUFF_SIZE] = {0x00};
279
280 GetFromBigBuf(data,TRACE_BUFF_SIZE,3560); //3560 -- should be offset..
281 WaitForResponseTimeout(CMD_ACK,NULL, 1500);
282
283 for (int j = 0; j < TRACE_BUFF_SIZE; j++) {
284 GraphBuffer[j] = ((int)data[j]) ;
285 }
286 GraphTraceLen = TRACE_BUFF_SIZE;
287 RepaintGraphWindow();
9455b51c 288 return 0;
7fe9b0b7 289}
290
291int CmdHF15Reader(const char *Cmd)
292{
9455b51c 293 UsbCommand c = {CMD_READER_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
294 SendCommand(&c);
295 return 0;
7fe9b0b7 296}
297
9455b51c 298// Simulation is still not working very good
7fe9b0b7 299int CmdHF15Sim(const char *Cmd)
300{
9455b51c 301 UsbCommand c = {CMD_SIMTAG_ISO_15693, {strtol(Cmd, NULL, 0), 0, 0}};
302 SendCommand(&c);
303 return 0;
7fe9b0b7 304}
305
9455b51c 306// finds the AFI (Application Family Idendifier) of a card, by trying all values
307// (There is no standard way of reading the AFI, allthough some tags support this)
308int CmdHF15Afi(const char *Cmd)
7fe9b0b7 309{
9455b51c 310 UsbCommand c = {CMD_ISO_15693_FIND_AFI, {strtol(Cmd, NULL, 0), 0, 0}};
311 SendCommand(&c);
312 return 0;
313}
314
315// Reads all memory pages
316int CmdHF15DumpMem(const char*Cmd) {
902cb3c0 317 UsbCommand resp;
9455b51c 318 uint8_t uid[8];
319 uint8_t *recv=NULL;
320 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
321 uint8_t *req=c.d.asBytes;
322 int reqlen=0;
323 int blocknum=0;
324 char output[80];
325
326 if (!getUID(uid)) {
327 PrintAndLog("No Tag found.");
328 return 0;
329 }
330
331 PrintAndLog("Reading memory from tag UID=%s",sprintUID(NULL,uid));
332 PrintAndLog("Tag Info: %s",getTagInfo(uid));
333
334 for (int retry=0; retry<5; retry++) {
335
336 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
337 ISO15_REQ_NONINVENTORY | ISO15_REQ_ADDRESS;
338 req[1]=ISO15_CMD_READ;
339 memcpy(&req[2],uid,8);
340 req[10]=blocknum;
341 reqlen=AddCrc(req,11);
342 c.arg[0]=reqlen;
343
344 SendCommand(&c);
345
902cb3c0 346 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
347 recv = resp.d.asBytes;
348 if (ISO15_CRC_CHECK==Crc(recv,resp.arg[0])) {
9455b51c 349 if (!(recv[0] & ISO15_RES_ERROR)) {
350 retry=0;
351 *output=0; // reset outputstring
72e930ef 352 sprintf(output, "Block %02x ",blocknum);
902cb3c0 353 for ( int i=1; i<resp.arg[0]-2; i++) { // data in hex
7bb9d33e 354 sprintf(output+strlen(output),"%02X ",recv[i]);
9455b51c 355 }
356 strcat(output," ");
902cb3c0 357 for ( int i=1; i<resp.arg[0]-2; i++) { // data in cleaned ascii
9455b51c 358 sprintf(output+strlen(output),"%c",(recv[i]>31 && recv[i]<127)?recv[i]:'.');
359 }
360 PrintAndLog("%s",output);
361 blocknum++;
362 // PrintAndLog("bn=%i",blocknum);
363 } else {
b4a9d841 364 PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 365 return 0;
366 }
367 } // else PrintAndLog("crc");
368 } // else PrintAndLog("r null");
9455b51c 369 } // retry
902cb3c0 370 // TODO: need fix
371// if (resp.arg[0]<3)
372// PrintAndLog("Lost Connection");
373// else if (ISO15_CRC_CHECK!=Crc(resp.d.asBytes,resp.arg[0]))
374// PrintAndLog("CRC Failed");
375// else
376// PrintAndLog("Tag returned Error %i: %s",recv[1],TagErrorStr(recv[1]));
9455b51c 377 return 0;
378}
379
380
381// "HF 15" interface
382
383static command_t CommandTable15[] =
384{
385 {"help", CmdHF15Help, 1, "This help"},
386 {"demod", CmdHF15Demod, 1, "Demodulate ISO15693 from tag"},
387 {"read", CmdHF15Read, 0, "Read HF tag (ISO 15693)"},
388 {"record", CmdHF15Record, 0, "Record Samples (ISO 15693)"}, // atrox
389 {"reader", CmdHF15Reader, 0, "Act like an ISO15693 reader"},
390 {"sim", CmdHF15Sim, 0, "Fake an ISO15693 tag"},
391 {"cmd", CmdHF15Cmd, 0, "Send direct commands to ISO15693 tag"},
392 {"findafi", CmdHF15Afi, 0, "Brute force AFI of an ISO15693 tag"},
393 {"dumpmemory", CmdHF15DumpMem, 0, "Read all memory pages of an ISO15693 tag"},
394 {NULL, NULL, 0, NULL}
7fe9b0b7 395};
396
397int CmdHF15(const char *Cmd)
398{
9455b51c 399 CmdsParse(CommandTable15, Cmd);
400 return 0;
7fe9b0b7 401}
402
9455b51c 403int CmdHF15Help(const char *Cmd)
7fe9b0b7 404{
9455b51c 405 CmdsHelp(CommandTable15);
406 return 0;
7fe9b0b7 407}
9455b51c 408
409
410// "HF 15 Cmd" Interface
411// Allows direct communication with the tag on command level
412
413int CmdHF15CmdInquiry(const char *Cmd)
414{
902cb3c0 415 UsbCommand resp;
9455b51c 416 uint8_t *recv;
417 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
418 uint8_t *req=c.d.asBytes;
419 int reqlen=0;
420
421 req[0]= ISO15_REQ_SUBCARRIER_SINGLE | ISO15_REQ_DATARATE_HIGH |
422 ISO15_REQ_INVENTORY | ISO15_REQINV_SLOT1;
423 req[1]=ISO15_CMD_INVENTORY;
424 req[2]=0; // mask length
425 reqlen=AddCrc(req,3);
426 c.arg[0]=reqlen;
427
428 SendCommand(&c);
429
902cb3c0 430 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
431 if (resp.arg[0]>=12) {
432 recv = resp.d.asBytes;
9455b51c 433 PrintAndLog("UID=%s",sprintUID(NULL,&recv[2]));
434 PrintAndLog("Tag Info: %s",getTagInfo(&recv[2]));
435 } else {
902cb3c0 436 PrintAndLog("Response to short, just %i bytes. No tag?\n",resp.arg[0]);
9455b51c 437 }
438 } else {
439 PrintAndLog("timeout.");
440 }
441 return 0;
442}
443
444
445// Turns debugging on(1)/off(0)
446int CmdHF15CmdDebug( const char *cmd) {
447 int debug=atoi(cmd);
448 if (strlen(cmd)<1) {
7bd30f12 449 PrintAndLog("Usage: hf 15 cmd debug <0|1>");
450 PrintAndLog(" 0 no debugging");
451 PrintAndLog(" 1 turn debugging on");
9455b51c 452 return 0;
453 }
454
455 UsbCommand c = {CMD_ISO_15693_DEBUG, {debug, 0, 0}};
456 SendCommand(&c);
457 return 0;
458}
459
460
461int CmdHF15CmdRaw (const char *cmd) {
902cb3c0 462 UsbCommand resp;
9455b51c 463 uint8_t *recv;
464 UsbCommand c = {CMD_ISO_15693_COMMAND, {0, 1, 1}}; // len,speed,recv?
465 int reply=1;
466 int fast=1;
467 int crc=0;
468 char buf[5]="";
469 int i=0;
470 uint8_t data[100];
471 unsigned int datalen=0, temp;
fdb67f1c 472 char *hexout;
9455b51c 473
474
475 if (strlen(cmd)<3) {
476 PrintAndLog("Usage: hf 15 cmd raw [-r] [-2] [-c] <0A 0B 0C ... hex>");
477 PrintAndLog(" -r do not read response");
478 PrintAndLog(" -2 use slower '1 out of 256' mode");
479 PrintAndLog(" -c calculate and append CRC");
480 PrintAndLog(" Tip: turn on debugging for verbose output");
481 return 0;
482 }
483
484 // strip
485 while (*cmd==' ' || *cmd=='\t') cmd++;
486
487 while (cmd[i]!='\0') {
488 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
489 if (cmd[i]=='-') {
490 switch (cmd[i+1]) {
491 case 'r':
492 case 'R':
493 reply=0;
494 break;
495 case '2':
496 fast=0;
497 break;
498 case 'c':
499 case 'C':
500 crc=1;
501 break;
502 default:
503 PrintAndLog("Invalid option");
504 return 0;
505 }
506 i+=2;
507 continue;
508 }
509 if ((cmd[i]>='0' && cmd[i]<='9') ||
510 (cmd[i]>='a' && cmd[i]<='f') ||
511 (cmd[i]>='A' && cmd[i]<='F') ) {
512 buf[strlen(buf)+1]=0;
513 buf[strlen(buf)]=cmd[i];
514 i++;
515
516 if (strlen(buf)>=2) {
517 sscanf(buf,"%x",&temp);
518 data[datalen]=(uint8_t)(temp & 0xff);
519 datalen++;
520 *buf=0;
521 }
522 continue;
523 }
524 PrintAndLog("Invalid char on input");
525 return 0;
526 }
527 if (crc) datalen=AddCrc(data,datalen);
528
529 c.arg[0]=datalen;
530 c.arg[1]=fast;
531 c.arg[2]=reply;
532 memcpy(c.d.asBytes,data,datalen);
533
534 SendCommand(&c);
535
536 if (reply) {
902cb3c0 537 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
538 recv = resp.d.asBytes;
539 PrintAndLog("received %i octets",resp.arg[0]);
540 hexout = (char *)malloc(resp.arg[0] * 3 + 1);
fdb67f1c 541 if (hexout != NULL) {
902cb3c0 542 for (int i = 0; i < resp.arg[0]; i++) { // data in hex
7bb9d33e 543 sprintf(&hexout[i * 3], "%02X ", recv[i]);
fdb67f1c 544 }
545 PrintAndLog("%s", hexout);
546 free(hexout);
547 }
9455b51c 548 } else {
549 PrintAndLog("timeout while waiting for reply.");
550 }
551
552 } // if reply
553 return 0;
554}
555
556
0546b4aa 557/**
558 * parses common HF 15 CMD parameters and prepares some data structures
559 * Parameters:
560 * **cmd command line
561 */
9455b51c 562int prepareHF15Cmd(char **cmd, UsbCommand *c, uint8_t iso15cmd[], int iso15cmdlen) {
563 int temp;
564 uint8_t *req=c->d.asBytes, uid[8];
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