1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>, Hagen Fritsch
4 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
5 // at your option, any later version. See the LICENSE.txt file for the text of
7 //-----------------------------------------------------------------------------
8 // High frequency ISO14443A commands
9 //-----------------------------------------------------------------------------
14 #include "iso14443crc.h"
18 #include "cmdparser.h"
22 static int CmdHelp(const char *Cmd
);
24 int CmdHF14AList(const char *Cmd
)
27 GetFromBigBuf(got
, sizeof(got
));
29 PrintAndLog("recorded activity:");
30 PrintAndLog(" ETU :rssi: who bytes");
31 PrintAndLog("---------+----+----+-----------");
42 int timestamp
= *((uint32_t *)(got
+i
));
43 if (timestamp
& 0x80000000) {
44 timestamp
&= 0x7fffffff;
51 int parityBits
= *((uint32_t *)(got
+i
+4));
52 // 4 bytes of additional information...
53 // maximum of 32 additional parity bit information
56 // at each quarter bit period we can send power level (16 levels)
57 // or each half bit period in 256 levels.
65 if (i
+ len
>= 1900) {
69 uint8_t *frame
= (got
+i
+9);
71 // Break and stick with current result if buffer was not completely full
72 if (frame
[0] == 0x44 && frame
[1] == 0x44 && frame
[3] == 0x44) { break; }
76 for (j
= 0; j
< len
; j
++) {
81 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
84 //if((parityBits >> (len - j - 1)) & 0x01) {
85 if (isResponse
&& (oddparity
!= ((parityBits
>> (len
- j
- 1)) & 0x01))) {
86 sprintf(line
+(j
*4), "%02x! ", frame
[j
]);
89 sprintf(line
+(j
*4), "%02x ", frame
[j
]);
97 for (j
= 0; j
< (len
- 1); j
++) {
98 // gives problems... search for the reason..
99 /*if(frame[j] == 0xAA) {
102 crc = "[1] Two drops close after each other";
105 crc = "[2] Potential SOC with a drop in second half of bitperiod";
108 crc = "[3] Segment Z after segment X is not possible";
111 crc = "[4] Parity bit of a fully received byte was wrong";
114 crc = "[?] Unknown error";
121 if (strlen(crc
)==0) {
122 ComputeCrc14443(CRC_14443_A
, frame
, len
-2, &b1
, &b2
);
123 if (b1
!= frame
[len
-2] || b2
!= frame
[len
-1]) {
124 crc
= (isResponse
& (len
< 6)) ? "" : " !crc";
133 char metricString
[100];
135 sprintf(metricString
, "%3d", metric
);
137 strcpy(metricString
, " ");
140 PrintAndLog(" +%7d: %s: %s %s %s",
141 (prev
< 0 ? 0 : (timestamp
- prev
)),
143 (isResponse
? "TAG" : " "), line
, crc
);
151 void iso14a_set_timeout(uint32_t timeout
) {
152 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_SET_TIMEOUT
, 0, timeout
}};
156 int CmdHF14AMifare(const char *Cmd
)
158 UsbCommand c
= {CMD_READER_MIFARE
, {strtol(Cmd
, NULL
, 0), 0, 0}};
163 int CmdHF14AReader(const char *Cmd
)
165 UsbCommand c
= {CMD_READER_ISO_14443a
, {ISO14A_CONNECT
, 0, 0}};
167 UsbCommand
* resp
= WaitForResponse(CMD_ACK
);
168 uint8_t * uid
= resp
->d
.asBytes
;
169 iso14a_card_select_t
* card
= uid
+ 12;
171 if(resp
->arg
[0] == 0) {
172 PrintAndLog("iso14443a card select failed");
176 PrintAndLog("ATQA : %02x %02x", card
->atqa
[0], card
->atqa
[1]);
177 PrintAndLog(" UID : %s", sprint_hex(uid
, 12));
178 PrintAndLog(" SAK : %02x [%d]", card
->sak
, resp
->arg
[0]);
179 if(resp
->arg
[0] == 1)
180 PrintAndLog(" ATS : %s", sprint_hex(card
->ats
, card
->ats_len
));
182 PrintAndLog("proprietary non-iso14443a card found, RATS not supported");
187 // ## simulate iso14443a tag
188 // ## greg - added ability to specify tag UID
189 int CmdHF14ASim(const char *Cmd
)
192 unsigned int hi
= 0, lo
= 0;
194 while (sscanf(&Cmd
[i
++], "%1x", &n
) == 1) {
195 hi
= (hi
<< 4) | (lo
>> 28);
196 lo
= (lo
<< 4) | (n
& 0xf);
199 // c.arg should be set to *Cmd or convert *Cmd to the correct format for a uid
200 UsbCommand c
= {CMD_SIMULATE_TAG_ISO_14443a
, {hi
, lo
, 0}};
201 PrintAndLog("Emulating 14443A TAG with UID %x%16x", hi
, lo
);
206 int CmdHF14ASnoop(const char *Cmd
)
208 UsbCommand c
= {CMD_SNOOP_ISO_14443a
};
213 static command_t CommandTable
[] =
215 {"help", CmdHelp
, 1, "This help"},
216 {"list", CmdHF14AList
, 0, "List ISO 14443a history"},
217 {"mifare", CmdHF14AMifare
, 0, "Read out sector 0 parity error messages"},
218 {"reader", CmdHF14AReader
, 0, "Act like an ISO14443 Type A reader"},
219 {"sim", CmdHF14ASim
, 0, "<UID> -- Fake ISO 14443a tag"},
220 {"snoop", CmdHF14ASnoop
, 0, "Eavesdrop ISO 14443 Type A"},
221 {NULL
, NULL
, 0, NULL
}
224 int CmdHF14A(const char *Cmd
)
226 CmdsParse(CommandTable
, Cmd
);
230 int CmdHelp(const char *Cmd
)
232 CmdsHelp(CommandTable
);