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