Bugfix hf 14a raw:
[proxmark3-svn] / client / cmdhf14a.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
f89c7050 2// 2011, Merlok
534983d7 3// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
a553f267 4//
5// This code is licensed to you under the terms of the GNU GPL, version 2 or,
6// at your option, any later version. See the LICENSE.txt file for the text of
7// the license.
8//-----------------------------------------------------------------------------
9// High frequency ISO14443A commands
10//-----------------------------------------------------------------------------
11
7fe9b0b7 12#include <stdio.h>
590f8ff9 13#include <stdlib.h>
7fe9b0b7 14#include <string.h>
f397b5cc 15#include <unistd.h>
20f9a2a1 16#include "util.h"
7fe9b0b7 17#include "iso14443crc.h"
18#include "data.h"
902cb3c0 19#include "proxmark3.h"
7fe9b0b7 20#include "ui.h"
21#include "cmdparser.h"
22#include "cmdhf14a.h"
534983d7 23#include "common.h"
20f9a2a1 24#include "cmdmain.h"
902cb3c0 25#include "mifare.h"
7fe9b0b7 26
27static int CmdHelp(const char *Cmd);
5f6d6c90 28static void waitCmd(uint8_t iLen);
7fe9b0b7 29
30int CmdHF14AList(const char *Cmd)
31{
7bc95e2e 32 bool ShowWaitCycles = false;
33 char param = param_getchar(Cmd, 0);
34
35 if (param == 'h' || (param != 0 && param != 'f')) {
36 PrintAndLog("List data in trace buffer.");
37 PrintAndLog("Usage: hf 14a list [f]");
38 PrintAndLog("f - show frame delay times as well");
39 PrintAndLog("sample: hf 14a list f");
40 return 0;
41 }
42
43 if (param == 'f') {
44 ShowWaitCycles = true;
45 }
46
47 uint8_t got[1920];
48 GetFromBigBuf(got,sizeof(got),0);
49 WaitForResponse(CMD_ACK,NULL);
50
51 PrintAndLog("Recorded Activity");
52 PrintAndLog("");
53 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
54 PrintAndLog("All times are in carrier periods (1/13.56Mhz)");
55 PrintAndLog("");
56 PrintAndLog(" Start | End | Src | Data");
57 PrintAndLog("-----------|-----------|-----|--------");
58
59 int i = 0;
60 uint32_t first_timestamp = 0;
61 uint32_t timestamp;
62 uint32_t EndOfTransmissionTimestamp = 0;
63
64 for (;;) {
65 if(i >= 1900) {
66 break;
e691fc45 67 }
7bc95e2e 68
69 bool isResponse;
70 timestamp = *((uint32_t *)(got+i));
71 if (timestamp & 0x80000000) {
72 timestamp &= 0x7fffffff;
73 isResponse = true;
74 } else {
75 isResponse = false;
76 }
77
78 if(i==0) {
79 first_timestamp = timestamp;
80 }
81
82 int parityBits = *((uint32_t *)(got+i+4));
83
84 int len = got[i+8];
85
86 if (len > 100) {
87 break;
88 }
89 if (i + len >= 1900) {
90 break;
91 }
92
93 uint8_t *frame = (got+i+9);
94
95 // Break and stick with current result if buffer was not completely full
96 if (frame[0] == 0x44 && frame[1] == 0x44 && frame[2] == 0x44 && frame[3] == 0x44) break;
97
98 char line[1000] = "";
99 int j;
100 if (len) {
101 for (j = 0; j < len; j++) {
102 int oddparity = 0x01;
103 int k;
104
105 for (k=0;k<8;k++) {
106 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
107 }
108
109 //if((parityBits >> (len - j - 1)) & 0x01) {
110 if (isResponse && (oddparity != ((parityBits >> (len - j - 1)) & 0x01))) {
111 sprintf(line+(j*4), "%02x! ", frame[j]);
112 } else {
113 sprintf(line+(j*4), "%02x ", frame[j]);
114 }
115 }
116 } else {
117 if (ShowWaitCycles) {
118 uint32_t next_timestamp = (*((uint32_t *)(got+i+9))) & 0x7fffffff;
119 sprintf(line, "fdt (Frame Delay Time): %d", (next_timestamp - timestamp));
120 }
121 }
122
123 char *crc;
124 crc = "";
125 if (len > 2) {
126 uint8_t b1, b2;
127 for (j = 0; j < (len - 1); j++) {
128 // gives problems... search for the reason..
129 /*if(frame[j] == 0xAA) {
130 switch(frame[j+1]) {
131 case 0x01:
132 crc = "[1] Two drops close after each other";
133 break;
134 case 0x02:
135 crc = "[2] Potential SOC with a drop in second half of bitperiod";
136 break;
137 case 0x03:
138 crc = "[3] Segment Z after segment X is not possible";
139 break;
140 case 0x04:
141 crc = "[4] Parity bit of a fully received byte was wrong";
142 break;
143 default:
144 crc = "[?] Unknown error";
145 break;
146 }
147 break;
148 }*/
149 }
150
151 if (strlen(crc)==0) {
152 ComputeCrc14443(CRC_14443_A, frame, len-2, &b1, &b2);
153 if (b1 != frame[len-2] || b2 != frame[len-1]) {
154 crc = (isResponse & (len < 6)) ? "" : " !crc";
155 } else {
156 crc = "";
157 }
158 }
159 } else {
160 crc = ""; // SHORT
161 }
162
163 i += (len + 9);
164
165 EndOfTransmissionTimestamp = (*((uint32_t *)(got+i))) & 0x7fffffff;
166
167 if (!ShowWaitCycles) i += 9;
168
169 PrintAndLog(" %9d | %9d | %s | %s %s",
170 (timestamp - first_timestamp),
171 (EndOfTransmissionTimestamp - first_timestamp),
172 (len?(isResponse ? "Tag" : "Rdr"):" "),
173 line, crc);
174
175 }
f89c7050 176 return 0;
7fe9b0b7 177}
178
534983d7 179void iso14a_set_timeout(uint32_t timeout) {
180 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_SET_TIMEOUT, 0, timeout}};
181 SendCommand(&c);
182}
183
7fe9b0b7 184int CmdHF14AReader(const char *Cmd)
185{
19d6d91f 186 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT | ISO14A_NO_DISCONNECT, 0, 0}};
534983d7 187 SendCommand(&c);
902cb3c0 188
189 UsbCommand resp;
7bc95e2e 190 WaitForResponse(CMD_ACK,&resp);
902cb3c0 191
19d6d91f 192 iso14a_card_select_t card;
193 memcpy(&card, (iso14a_card_select_t *)resp.d.asBytes, sizeof(iso14a_card_select_t));
534983d7 194
19d6d91f 195 uint64_t select_status = resp.arg[0];
196
197 if(select_status == 0) {
534983d7 198 PrintAndLog("iso14443a card select failed");
199 return 0;
200 }
201
19d6d91f 202 PrintAndLog("ATQA : %02x %02x", card.atqa[1], card.atqa[0]);
203 PrintAndLog(" UID : %s", sprint_hex(card.uid, card.uidlen));
204 PrintAndLog(" SAK : %02x [%d]", card.sak, resp.arg[0]);
713e7ffb 205
19d6d91f 206 switch (card.sak) {
79a73ab2 207 case 0x00: PrintAndLog("TYPE : NXP MIFARE Ultralight | Ultralight C"); break;
208 case 0x04: PrintAndLog("TYPE : NXP MIFARE (various !DESFire !DESFire EV1)"); break;
e691fc45 209 case 0x08: PrintAndLog("TYPE : NXP MIFARE CLASSIC 1k | Plus 2k SL1"); break;
79a73ab2 210 case 0x09: PrintAndLog("TYPE : NXP MIFARE Mini 0.3k"); break;
e691fc45 211 case 0x10: PrintAndLog("TYPE : NXP MIFARE Plus 2k SL2"); break;
212 case 0x11: PrintAndLog("TYPE : NXP MIFARE Plus 4k SL2"); break;
213 case 0x18: PrintAndLog("TYPE : NXP MIFARE Classic 4k | Plus 4k SL1"); break;
214 case 0x20: PrintAndLog("TYPE : NXP MIFARE DESFire 4k | DESFire EV1 2k/4k/8k | Plus 2k/4k SL3 | JCOP 31/41"); break;
79a73ab2 215 case 0x24: PrintAndLog("TYPE : NXP MIFARE DESFire | DESFire EV1"); break;
216 case 0x28: PrintAndLog("TYPE : JCOP31 or JCOP41 v2.3.1"); break;
217 case 0x38: PrintAndLog("TYPE : Nokia 6212 or 6131 MIFARE CLASSIC 4K"); break;
218 case 0x88: PrintAndLog("TYPE : Infineon MIFARE CLASSIC 1K"); break;
219 case 0x98: PrintAndLog("TYPE : Gemplus MPCOS"); break;
9ca155ba
M
220 default: ;
221 }
19d6d91f 222
223
224 // try to request ATS even if tag claims not to support it
225 if (select_status == 2) {
226 uint8_t rats[] = { 0xE0, 0x80 }; // FSDI=8 (FSD=256), CID=0
227 c.arg[0] = ISO14A_RAW | ISO14A_APPEND_CRC | ISO14A_NO_DISCONNECT;
228 c.arg[1] = 2;
229 c.arg[2] = 0;
230 memcpy(c.d.asBytes, rats, 2);
231 SendCommand(&c);
232 WaitForResponse(CMD_ACK,&resp);
233
234 memcpy(&card.ats, resp.d.asBytes, resp.arg[0]);
235 card.ats_len = resp.arg[0];
236 }
237
238 // disconnect
239 c.arg[0] = 0;
240 c.arg[1] = 0;
241 c.arg[2] = 0;
242 SendCommand(&c);
243
244
245 if(card.ats_len >= 3) { // a valid ATS consists of at least the length byte TL and 2 CRC bytes
561f7c11 246 bool ta1 = 0, tb1 = 0, tc1 = 0;
247 int pos;
248
19d6d91f 249 if (!(card.sak & 0x02)) {
250 PrintAndLog("SAK incorrectly claims that card doesn't support RATS");
251 }
252 PrintAndLog(" ATS : %s", sprint_hex(card.ats, card.ats_len));
253 if (card.ats_len > 0) {
254 PrintAndLog(" - TL : length is %d bytes", card.ats[0]);
561f7c11 255 }
19d6d91f 256 if (card.ats_len > 1) {
257 ta1 = (card.ats[1] & 0x10) == 0x10;
258 tb1 = (card.ats[1] & 0x20) == 0x20;
259 tc1 = (card.ats[1] & 0x40) == 0x40;
561f7c11 260 PrintAndLog(" - T0 : TA1 is%s present, TB1 is%s present, "
261 "TC1 is%s present, FSCI is %d",
262 (ta1 ? "" : " NOT"), (tb1 ? "" : " NOT"), (tc1 ? "" : " NOT"),
19d6d91f 263 (card.ats[1] & 0x0f));
561f7c11 264 }
265 pos = 2;
19d6d91f 266 if (ta1 && card.ats_len > pos) {
561f7c11 267 char dr[16], ds[16];
268 dr[0] = ds[0] = '\0';
19d6d91f 269 if (card.ats[pos] & 0x10) strcat(ds, "2, ");
270 if (card.ats[pos] & 0x20) strcat(ds, "4, ");
271 if (card.ats[pos] & 0x40) strcat(ds, "8, ");
272 if (card.ats[pos] & 0x01) strcat(dr, "2, ");
273 if (card.ats[pos] & 0x02) strcat(dr, "4, ");
274 if (card.ats[pos] & 0x04) strcat(dr, "8, ");
561f7c11 275 if (strlen(ds) != 0) ds[strlen(ds) - 2] = '\0';
276 if (strlen(dr) != 0) dr[strlen(dr) - 2] = '\0';
277 PrintAndLog(" - TA1 : different divisors are%s supported, "
278 "DR: [%s], DS: [%s]",
19d6d91f 279 (card.ats[pos] & 0x80 ? " NOT" : ""), dr, ds);
561f7c11 280 pos++;
281 }
19d6d91f 282 if (tb1 && card.ats_len > pos) {
561f7c11 283 PrintAndLog(" - TB1 : SFGI = %d, FWI = %d",
19d6d91f 284 (card.ats[pos] & 0x08),
285 (card.ats[pos] & 0x80) >> 4);
561f7c11 286 pos++;
287 }
19d6d91f 288 if (tc1 && card.ats_len > pos) {
561f7c11 289 PrintAndLog(" - TC1 : NAD is%s supported, CID is%s supported",
19d6d91f 290 (card.ats[pos] & 0x01) ? "" : " NOT",
291 (card.ats[pos] & 0x02) ? "" : " NOT");
561f7c11 292 pos++;
293 }
19d6d91f 294 if (card.ats_len > pos) {
561f7c11 295 char *tip = "";
19d6d91f 296 if (card.ats_len - pos > 7) {
297 if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x01\xBC\xD6", 7) == 0) {
561f7c11 298 tip = "-> MIFARE Plus X 2K or 4K";
19d6d91f 299 } else if (memcmp(card.ats + pos, "\xC1\x05\x2F\x2F\x00\x35\xC7", 7) == 0) {
561f7c11 300 tip = "-> MIFARE Plus S 2K or 4K";
301 }
302 }
19d6d91f 303 PrintAndLog(" - HB : %s%s", sprint_hex(card.ats + pos, card.ats_len - pos - 2), tip);
304 if (card.ats[pos] == 0xC1) {
561f7c11 305 PrintAndLog(" c1 -> Mifare or (multiple) virtual cards of various type");
306 PrintAndLog(" %02x -> Length is %d bytes",
19d6d91f 307 card.ats[pos + 1], card.ats[pos + 1]);
308 switch (card.ats[pos + 2] & 0xf0) {
561f7c11 309 case 0x10:
310 PrintAndLog(" 1x -> MIFARE DESFire");
311 break;
312 case 0x20:
313 PrintAndLog(" 2x -> MIFARE Plus");
314 break;
315 }
19d6d91f 316 switch (card.ats[pos + 2] & 0x0f) {
561f7c11 317 case 0x00:
318 PrintAndLog(" x0 -> <1 kByte");
319 break;
320 case 0x01:
321 PrintAndLog(" x0 -> 1 kByte");
322 break;
323 case 0x02:
324 PrintAndLog(" x0 -> 2 kByte");
325 break;
326 case 0x03:
327 PrintAndLog(" x0 -> 4 kByte");
328 break;
329 case 0x04:
330 PrintAndLog(" x0 -> 8 kByte");
331 break;
332 }
19d6d91f 333 switch (card.ats[pos + 3] & 0xf0) {
561f7c11 334 case 0x00:
335 PrintAndLog(" 0x -> Engineering sample");
336 break;
337 case 0x20:
338 PrintAndLog(" 2x -> Released");
339 break;
340 }
19d6d91f 341 switch (card.ats[pos + 3] & 0x0f) {
561f7c11 342 case 0x00:
343 PrintAndLog(" x0 -> Generation 1");
344 break;
345 case 0x01:
346 PrintAndLog(" x1 -> Generation 2");
347 break;
348 case 0x02:
349 PrintAndLog(" x2 -> Generation 3");
350 break;
351 }
19d6d91f 352 switch (card.ats[pos + 4] & 0x0f) {
561f7c11 353 case 0x00:
354 PrintAndLog(" x0 -> Only VCSL supported");
355 break;
356 case 0x01:
357 PrintAndLog(" x1 -> VCS, VCSL, and SVC supported");
358 break;
359 case 0x0E:
360 PrintAndLog(" xE -> no VCS command supported");
361 break;
362 }
363 }
364 }
79a73ab2 365 } else {
19d6d91f 366 PrintAndLog("proprietary non iso14443-4 card found, RATS not supported");
367 }
534983d7 368
19d6d91f 369 return select_status;
7fe9b0b7 370}
371
db22dfe6 372// Collect ISO14443 Type A UIDs
373int CmdHF14ACUIDs(const char *Cmd)
374{
375 // requested number of UIDs
376 int n = atoi(Cmd);
377 // collect at least 1 (e.g. if no parameter was given)
378 n = n > 0 ? n : 1;
379
380 PrintAndLog("Collecting %d UIDs", n);
381 PrintAndLog("Start: %u", time(NULL));
382 // repeat n times
383 for (int i = 0; i < n; i++) {
384 // execute anticollision procedure
385 UsbCommand c = {CMD_READER_ISO_14443a, {ISO14A_CONNECT, 0, 0}};
386 SendCommand(&c);
902cb3c0 387
72b1090a 388 UsbCommand resp;
389 WaitForResponse(CMD_ACK,&resp);
902cb3c0 390
72b1090a 391 iso14a_card_select_t *card = (iso14a_card_select_t *) resp.d.asBytes;
db22dfe6 392
393 // check if command failed
902cb3c0 394 if (resp.arg[0] == 0) {
db22dfe6 395 PrintAndLog("Card select failed.");
396 } else {
72b1090a 397 char uid_string[20];
398 for (uint16_t i = 0; i < card->uidlen; i++) {
399 sprintf(&uid_string[2*i], "%02X", card->uid[i]);
db22dfe6 400 }
72b1090a 401 PrintAndLog("%s", uid_string);
db22dfe6 402 }
403 }
404 PrintAndLog("End: %u", time(NULL));
405
406 return 1;
407}
408
7fe9b0b7 409// ## simulate iso14443a tag
410// ## greg - added ability to specify tag UID
411int CmdHF14ASim(const char *Cmd)
81cd0474 412{
413 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,{0,0,0}};
414
415 // Retrieve the tag type
416 uint8_t tagtype = param_get8ex(Cmd,0,0,10);
417
418 // When no argument was given, just print help message
419 if (tagtype == 0) {
420 PrintAndLog("");
421 PrintAndLog(" Emulating ISO/IEC 14443 type A tag with 4 or 7 byte UID");
422 PrintAndLog("");
423 PrintAndLog(" syntax: hf 14a sim <type> <uid>");
424 PrintAndLog(" types: 1 = MIFARE Classic");
425 PrintAndLog(" 2 = MIFARE Ultralight");
426 PrintAndLog(" 3 = MIFARE DESFIRE");
427 PrintAndLog(" 4 = ISO/IEC 14443-4");
428 PrintAndLog("");
429 return 1;
430 }
431
432 // Store the tag type
433 c.arg[0] = tagtype;
434
435 // Retrieve the full 4 or 7 byte long uid
436 uint64_t long_uid = param_get64ex(Cmd,1,0,16);
437
438 // Are we handling the (optional) second part uid?
439 if (long_uid > 0xffffffff) {
125a98a1 440 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 7 byte UID (%014"llx")",long_uid);
81cd0474 441 // Store the second part
442 c.arg[2] = (long_uid & 0xffffffff);
443 long_uid >>= 32;
444 // Store the first part, ignore the first byte, it is replaced by cascade byte (0x88)
445 c.arg[1] = (long_uid & 0xffffff);
446 } else {
447 PrintAndLog("Emulating ISO/IEC 14443 type A tag with 4 byte UID (%08x)",long_uid);
448 // Only store the first part
449 c.arg[1] = long_uid & 0xffffffff;
450 }
451/*
452 // At lease save the mandatory first part of the UID
453 c.arg[0] = long_uid & 0xffffffff;
454
455
456 // At lease save the mandatory first part of the UID
457 c.arg[0] = long_uid & 0xffffffff;
458
459 if (c.arg[1] == 0) {
460 PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
461 }
462
463 switch (c.arg[0]) {
464 case 1: {
465 PrintAndLog("Emulating ISO/IEC 14443-3 type A tag with 4 byte UID");
466 UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
467 } break;
468 case 2: {
469 PrintAndLog("Emulating ISO/IEC 14443-4 type A tag with 7 byte UID");
470 } break;
471 default: {
472 PrintAndLog("Error: unkown tag type (%d)",c.arg[0]);
473 PrintAndLog("syntax: hf 14a sim <uid>",c.arg[0]);
474 PrintAndLog(" type1: 4 ",c.arg[0]);
475
476 return 1;
477 } break;
478 }
479*/
480/*
7fe9b0b7 481 unsigned int hi = 0, lo = 0;
482 int n = 0, i = 0;
483 while (sscanf(&Cmd[i++], "%1x", &n ) == 1) {
484 hi= (hi << 4) | (lo >> 28);
485 lo= (lo << 4) | (n & 0xf);
486 }
81cd0474 487*/
488// UsbCommand c = {CMD_SIMULATE_TAG_ISO_14443a,param_get32ex(Cmd,0,0,10),param_get32ex(Cmd,1,0,16),param_get32ex(Cmd,2,0,16)};
489// PrintAndLog("Emulating ISO/IEC 14443 type A tag with UID %01d %08x %08x",c.arg[0],c.arg[1],c.arg[2]);
7fe9b0b7 490 SendCommand(&c);
491 return 0;
492}
493
5cd9ec01
M
494int CmdHF14ASnoop(const char *Cmd) {
495 int param = 0;
496
497 if (param_getchar(Cmd, 0) == 'h') {
498 PrintAndLog("It get data from the field and saves it into command buffer.");
499 PrintAndLog("Buffer accessible from command hf 14a list.");
500 PrintAndLog("Usage: hf 14a snoop [c][r]");
501 PrintAndLog("c - triggered by first data from card");
502 PrintAndLog("r - triggered by first 7-bit request from reader (REQ,WUP,...)");
503 PrintAndLog("sample: hf 14a snoop c r");
504 return 0;
505 }
506
507 for (int i = 0; i < 2; i++) {
508 char ctmp = param_getchar(Cmd, i);
509 if (ctmp == 'c' || ctmp == 'C') param |= 0x01;
510 if (ctmp == 'r' || ctmp == 'R') param |= 0x02;
511 }
512
513 UsbCommand c = {CMD_SNOOP_ISO_14443a, {param, 0, 0}};
7fe9b0b7 514 SendCommand(&c);
515 return 0;
516}
517
5f6d6c90 518int CmdHF14ACmdRaw(const char *cmd) {
519 UsbCommand c = {CMD_READER_ISO_14443a, {0, 0, 0}};
520 uint8_t reply=1;
521 uint8_t crc=0;
522 uint8_t power=0;
523 uint8_t active=0;
524 uint8_t active_select=0;
525 uint16_t numbits=0;
526 char buf[5]="";
527 int i=0;
528 uint8_t data[100];
529 unsigned int datalen=0, temp;
530
531 if (strlen(cmd)<2) {
532 PrintAndLog("Usage: hf 14a raw [-r] [-c] [-p] [-f] [-b] <number of bits> <0A 0B 0C ... hex>");
533 PrintAndLog(" -r do not read response");
534 PrintAndLog(" -c calculate and append CRC");
535 PrintAndLog(" -p leave the signal field ON after receive");
536 PrintAndLog(" -a active signal field ON without select");
537 PrintAndLog(" -s active signal field ON with select");
538 PrintAndLog(" -b number of bits to send. Useful for send partial byte");
539 return 0;
540 }
541
542 // strip
543 while (*cmd==' ' || *cmd=='\t') cmd++;
544
545 while (cmd[i]!='\0') {
546 if (cmd[i]==' ' || cmd[i]=='\t') { i++; continue; }
547 if (cmd[i]=='-') {
548 switch (cmd[i+1]) {
549 case 'r':
550 reply=0;
551 break;
552 case 'c':
553 crc=1;
554 break;
555 case 'p':
556 power=1;
557 break;
558 case 'a':
559 active=1;
560 break;
561 case 's':
562 active_select=1;
563 break;
564 case 'b':
565 sscanf(cmd+i+2,"%d",&temp);
566 numbits = temp & 0xFFFF;
567 i+=3;
568 while(cmd[i]!=' ' && cmd[i]!='\0') { i++; }
569 i-=2;
570 break;
571 default:
572 PrintAndLog("Invalid option");
573 return 0;
574 }
575 i+=2;
576 continue;
577 }
578 if ((cmd[i]>='0' && cmd[i]<='9') ||
579 (cmd[i]>='a' && cmd[i]<='f') ||
580 (cmd[i]>='A' && cmd[i]<='F') ) {
581 buf[strlen(buf)+1]=0;
582 buf[strlen(buf)]=cmd[i];
583 i++;
584
585 if (strlen(buf)>=2) {
586 sscanf(buf,"%x",&temp);
587 data[datalen]=(uint8_t)(temp & 0xff);
588 datalen++;
589 *buf=0;
590 }
591 continue;
592 }
593 PrintAndLog("Invalid char on input");
594 return 0;
595 }
596 if(crc && datalen>0)
597 {
598 uint8_t first, second;
599 ComputeCrc14443(CRC_14443_A, data, datalen, &first, &second);
600 data[datalen++] = first;
601 data[datalen++] = second;
602 }
603
604 if(active || active_select)
605 {
606 c.arg[0] |= ISO14A_CONNECT;
607 if(active)
608 c.arg[0] |= ISO14A_NO_SELECT;
609 }
610 if(power)
611 c.arg[0] |= ISO14A_NO_DISCONNECT;
612 if(datalen>0)
613 c.arg[0] |= ISO14A_RAW;
614
615 c.arg[1] = datalen;
616 c.arg[2] = numbits;
617 memcpy(c.d.asBytes,data,datalen);
618
619 SendCommand(&c);
620
621 if (reply) {
622 if(active_select)
623 waitCmd(1);
624 if(datalen>0)
625 waitCmd(0);
626 } // if reply
627 return 0;
628}
629
630static void waitCmd(uint8_t iSelect)
631{
632 uint8_t *recv;
633 UsbCommand resp;
634 char *hexout;
635
636 if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) {
637 recv = resp.d.asBytes;
638 uint8_t iLen = iSelect ? resp.arg[1] : resp.arg[0];
639 PrintAndLog("received %i octets",iLen);
640 if(!iLen)
641 return;
642 hexout = (char *)malloc(iLen * 3 + 1);
643 if (hexout != NULL) {
5f6d6c90 644 for (int i = 0; i < iLen; i++) { // data in hex
f66021cf 645 sprintf(&hexout[i * 3], "%02X ", recv[i]);
5f6d6c90 646 }
647 PrintAndLog("%s", hexout);
648 free(hexout);
649 } else {
650 PrintAndLog("malloc failed your client has low memory?");
651 }
652 } else {
653 PrintAndLog("timeout while waiting for reply.");
654 }
655}
656
7fe9b0b7 657static command_t CommandTable[] =
658{
5acd09bd 659 {"help", CmdHelp, 1, "This help"},
660 {"list", CmdHF14AList, 0, "List ISO 14443a history"},
661 {"reader", CmdHF14AReader, 0, "Act like an ISO14443 Type A reader"},
662 {"cuids", CmdHF14ACUIDs, 0, "<n> Collect n>0 ISO14443 Type A UIDs in one go"},
663 {"sim", CmdHF14ASim, 0, "<UID> -- Fake ISO 14443a tag"},
664 {"snoop", CmdHF14ASnoop, 0, "Eavesdrop ISO 14443 Type A"},
5f6d6c90 665 {"raw", CmdHF14ACmdRaw, 0, "Send raw hex data to tag"},
7fe9b0b7 666 {NULL, NULL, 0, NULL}
667};
668
902cb3c0 669int CmdHF14A(const char *Cmd) {
f397b5cc 670 // flush
902cb3c0 671 WaitForResponseTimeout(CMD_ACK,NULL,100);
f397b5cc
M
672
673 // parse
7fe9b0b7 674 CmdsParse(CommandTable, Cmd);
675 return 0;
676}
677
678int CmdHelp(const char *Cmd)
679{
680 CmdsHelp(CommandTable);
681 return 0;
682}
Impressum, Datenschutz