1 //-----------------------------------------------------------------------------
2 // Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
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 commands
9 //-----------------------------------------------------------------------------
13 //#include "proxusb.h"
14 #include "proxmark3.h"
17 #include "cmdparser.h"
23 #include "cmdhflegic.h"
24 #include "cmdhficlass.h"
27 static int CmdHelp(const char *Cmd
);
29 int CmdHFTune(const char *Cmd
)
31 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
35 // for the time being. Need better Bigbuf handling.
36 #define TRACE_SIZE 3000
38 #define ICLASS_CMD_ACTALL 0x0A
39 #define ICLASS_CMD_IDENTIFY 0x0C
40 #define ICLASS_CMD_READ 0x0C
41 #define ICLASS_CMD_SELECT 0x81
42 #define ICLASS_CMD_PAGESEL 0x84
43 #define ICLASS_CMD_READCHECK 0x88
44 #define ICLASS_CMD_CHECK 0x05
45 #define ICLASS_CMD_SOF 0x0F
46 #define ICLASS_CMD_HALT 0x00
48 #define iso14443_CMD_WUPA 0x52
49 #define iso14443_CMD_SELECT 0x93
50 #define iso14443_CMD_SELECT_2 0x95
51 #define iso14443_CMD_REQ 0x26
52 #define iso14443_CMD_READBLOCK 0x30
53 #define iso14443_CMD_WRITEBLOCK 0xA0
54 #define iso14443_CMD_INC 0xC0
55 #define iso14443_CMD_DEC 0xC1
56 #define iso14443_CMD_RESTORE 0xC2
57 #define iso14443_CMD_TRANSFER 0xB0
58 #define iso14443_CMD_HALT 0x50
59 #define iso14443_CMD_RATS 0xE0
62 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
66 case iso14443_CMD_WUPA
: snprintf(exp
,size
,"WUPA"); break;
67 case iso14443_CMD_SELECT
:{
70 snprintf(exp
,size
,"SELECT_UID"); break;
73 snprintf(exp
,size
,"SELECT_ALL"); break;
76 case iso14443_CMD_SELECT_2
: snprintf(exp
,size
,"SELECT_2"); break;
77 case iso14443_CMD_REQ
: snprintf(exp
,size
,"REW"); break;
78 case iso14443_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
79 case iso14443_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
80 case iso14443_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
81 case iso14443_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
82 case iso14443_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
83 case iso14443_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
84 case iso14443_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
85 case iso14443_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
86 default: snprintf(exp
,size
,"?"); break;
91 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
94 if(cmdsize
> 1 && cmd
[0] == ICLASS_CMD_READ
)
96 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
102 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
103 case ICLASS_CMD_IDENTIFY
: snprintf(exp
,size
,"IDENTIFY"); break;
104 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
105 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL"); break;
106 case ICLASS_CMD_READCHECK
: snprintf(exp
,size
,"READCHECK"); break;
107 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
108 case ICLASS_CMD_SOF
: snprintf(exp
,size
,"SOF"); break;
109 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
110 default: snprintf(exp
,size
,"?"); break;
117 uint16_t printTraceLine(uint16_t tracepos
, uint8_t* trace
, bool iclass
, bool showWaitCycles
)
120 uint16_t duration
, data_len
,parity_len
;
122 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
123 char explanation
[30] = {0};
125 first_timestamp
= *((uint32_t *)(trace
));
126 timestamp
= *((uint32_t *)(trace
+ tracepos
));
127 // Break and stick with current result if buffer was not completely full
128 if (timestamp
== 0x44444444) return TRACE_SIZE
;
131 duration
= *((uint16_t *)(trace
+ tracepos
));
133 data_len
= *((uint16_t *)(trace
+ tracepos
));
136 if (data_len
& 0x8000) {
142 parity_len
= (data_len
-1)/8 + 1;
144 if (tracepos
+ data_len
+ parity_len
>= TRACE_SIZE
) {
148 uint8_t *frame
= trace
+ tracepos
;
149 tracepos
+= data_len
;
150 uint8_t *parityBytes
= trace
+ tracepos
;
151 tracepos
+= parity_len
;
153 //--- Draw the data column
155 for (int j
= 0; j
< data_len
; j
++) {
156 int oddparity
= 0x01;
159 for (k
=0 ; k
<8 ; k
++) {
160 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
163 uint8_t parityBits
= parityBytes
[j
>>3];
165 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
166 sprintf(line
[j
/16]+((j
%16)*4), "%02x! ", frame
[j
]);
168 sprintf(line
[j
/16]+((j
%16)*4), "%02x ", frame
[j
]);
171 //--- Draw the CRC column
172 bool crcError
= false;
178 if(!isResponse
&& data_len
== 4 ) {
179 // Rough guess that this is a command from the reader
180 // For iClass the command byte is not part of the CRC
181 ComputeCrc14443(CRC_ICLASS
, &frame
[1], data_len
-3, &b1
, &b2
);
184 // For other data.. CRC might not be applicable (UPDATE commands etc.)
185 ComputeCrc14443(CRC_ICLASS
, frame
, data_len
-2, &b1
, &b2
);
188 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
194 ComputeCrc14443(CRC_14443_A
, frame
, data_len
-2, &b1
, &b2
);
196 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
197 if(!(isResponse
& (data_len
< 6)))
205 char *crc
= crcError
? "!crc" :" ";
207 EndOfTransmissionTimestamp
= timestamp
+ duration
;
211 if(iclass
) annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
);
212 else annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
);
215 int num_lines
= (data_len
- 1)/16 + 1;
216 for (int j
= 0; j
< num_lines
; j
++) {
218 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
219 (timestamp
- first_timestamp
),
220 (EndOfTransmissionTimestamp
- first_timestamp
),
221 (isResponse
? "Tag" : "Rdr"),
223 (j
== num_lines
-1) ? crc
: " ",
224 (j
== num_lines
-1) ? explanation
: "");
226 PrintAndLog(" | | | %-64s| %s| %s",
228 (j
== num_lines
-1)?crc
:" ",
229 (j
== num_lines
-1) ? explanation
: "");
233 bool next_isResponse
= *((uint16_t *)(trace
+ tracepos
+ 6)) & 0x8000;
235 if (showWaitCycles
&& !isResponse
&& next_isResponse
) {
236 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
237 if (next_timestamp
!= 0x44444444) {
238 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
239 (EndOfTransmissionTimestamp
- first_timestamp
),
240 (next_timestamp
- first_timestamp
),
242 (next_timestamp
- EndOfTransmissionTimestamp
));
248 int CmdHFList(const char *Cmd
)
250 bool showWaitCycles
= false;
252 int tlen
= param_getstr(Cmd
,0,type
);
253 char param
= param_getchar(Cmd
, 1);
257 if(tlen
== 0 || (strcmp(type
, "iclass") != 0 && strcmp(type
,"14a") != 0))
261 if(param
== 'h' || (param
!=0 && param
!= 'f'))
267 PrintAndLog("List protocol data in trace buffer.");
268 PrintAndLog("Usage: hf list [14a|iclass] [f]");
269 PrintAndLog(" - interpret data as iso14443a communications");
270 PrintAndLog(" iclass - interpret data as iclass communications");
271 PrintAndLog(" f - show frame delay times as well");
273 PrintAndLog("example: hf list 14a f");
274 PrintAndLog("example: hf list iclass");
277 if(strcmp(type
, "iclass") == 0)
283 showWaitCycles
= true;
287 uint8_t trace
[TRACE_SIZE
];
288 uint16_t tracepos
= 0;
289 GetFromBigBuf(trace
, TRACE_SIZE
, 0);
290 WaitForResponse(CMD_ACK
, NULL
);
292 PrintAndLog("Recorded Activity");
294 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
295 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
296 PrintAndLog("iClass - Timings are not as accurate");
298 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
299 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
301 while(tracepos
< TRACE_SIZE
)
303 tracepos
= printTraceLine(tracepos
, trace
, iclass
, showWaitCycles
);
309 static command_t CommandTable
[] =
311 {"help", CmdHelp
, 1, "This help"},
312 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
313 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
314 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
315 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
316 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
317 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
318 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
319 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
320 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
321 {NULL
, NULL
, 0, NULL
}
324 int CmdHF(const char *Cmd
)
326 CmdsParse(CommandTable
, Cmd
);
330 int CmdHelp(const char *Cmd
)
332 CmdsHelp(CommandTable
);