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