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 "proxmark3.h"
16 #include "cmdparser.h"
22 #include "cmdhflegic.h"
23 #include "cmdhficlass.h"
26 static int CmdHelp(const char *Cmd
);
28 int CmdHFTune(const char *Cmd
)
30 UsbCommand c
={CMD_MEASURE_ANTENNA_TUNING_HF
};
34 // for the time being. Need better Bigbuf handling.
35 #define TRACE_SIZE 3000
37 #define ICLASS_CMD_ACTALL 0x0A
38 #define ICLASS_CMD_IDENTIFY 0x0C
39 #define ICLASS_CMD_READ 0x0C
40 #define ICLASS_CMD_SELECT 0x81
41 #define ICLASS_CMD_PAGESEL 0x84
42 #define ICLASS_CMD_READCHECK 0x88
43 #define ICLASS_CMD_CHECK 0x05
44 #define ICLASS_CMD_SOF 0x0F
45 #define ICLASS_CMD_HALT 0x00
47 #define iso14443_CMD_WUPA 0x52
48 #define iso14443_CMD_SELECT 0x93
49 #define iso14443_CMD_SELECT_2 0x95
50 #define iso14443_CMD_REQ 0x26
51 #define iso14443_CMD_READBLOCK 0x30
52 #define iso14443_CMD_WRITEBLOCK 0xA0
53 #define iso14443_CMD_INC 0xC0
54 #define iso14443_CMD_DEC 0xC1
55 #define iso14443_CMD_RESTORE 0xC2
56 #define iso14443_CMD_TRANSFER 0xB0
57 #define iso14443_CMD_HALT 0x50
58 #define iso14443_CMD_RATS 0xE0
61 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
65 case iso14443_CMD_WUPA
: snprintf(exp
,size
,"WUPA"); break;
66 case iso14443_CMD_SELECT
:{
69 snprintf(exp
,size
,"SELECT_UID"); break;
72 snprintf(exp
,size
,"SELECT_ALL"); break;
75 case iso14443_CMD_SELECT_2
: snprintf(exp
,size
,"SELECT_2"); break;
76 case iso14443_CMD_REQ
: snprintf(exp
,size
,"REW"); break;
77 case iso14443_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
78 case iso14443_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
79 case iso14443_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
80 case iso14443_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
81 case iso14443_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
82 case iso14443_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
83 case iso14443_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
84 case iso14443_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
85 default: snprintf(exp
,size
,"?"); break;
90 void annotateIclass(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
93 if(cmdsize
> 1 && cmd
[0] == ICLASS_CMD_READ
)
95 snprintf(exp
,size
,"READ(%d)",cmd
[1]);
101 case ICLASS_CMD_ACTALL
: snprintf(exp
,size
,"ACTALL"); break;
102 case ICLASS_CMD_IDENTIFY
: snprintf(exp
,size
,"IDENTIFY"); break;
103 case ICLASS_CMD_SELECT
: snprintf(exp
,size
,"SELECT"); break;
104 case ICLASS_CMD_PAGESEL
: snprintf(exp
,size
,"PAGESEL"); break;
105 case ICLASS_CMD_READCHECK
: snprintf(exp
,size
,"READCHECK"); break;
106 case ICLASS_CMD_CHECK
: snprintf(exp
,size
,"CHECK"); break;
107 case ICLASS_CMD_SOF
: snprintf(exp
,size
,"SOF"); break;
108 case ICLASS_CMD_HALT
: snprintf(exp
,size
,"HALT"); break;
109 default: snprintf(exp
,size
,"?"); break;
116 uint16_t printTraceLine(uint16_t tracepos
, uint8_t* trace
, bool iclass
, bool showWaitCycles
)
119 uint16_t duration
, data_len
,parity_len
;
121 uint32_t timestamp
, first_timestamp
, EndOfTransmissionTimestamp
;
122 char explanation
[30] = {0};
124 first_timestamp
= *((uint32_t *)(trace
));
125 timestamp
= *((uint32_t *)(trace
+ tracepos
));
126 // Break and stick with current result if buffer was not completely full
127 if (timestamp
== 0x44444444) return TRACE_SIZE
;
130 duration
= *((uint16_t *)(trace
+ tracepos
));
132 data_len
= *((uint16_t *)(trace
+ tracepos
));
135 if (data_len
& 0x8000) {
141 parity_len
= (data_len
-1)/8 + 1;
143 if (tracepos
+ data_len
+ parity_len
>= TRACE_SIZE
) {
147 uint8_t *frame
= trace
+ tracepos
;
148 tracepos
+= data_len
;
149 uint8_t *parityBytes
= trace
+ tracepos
;
150 tracepos
+= parity_len
;
152 //--- Draw the data column
154 for (int j
= 0; j
< data_len
; j
++) {
155 int oddparity
= 0x01;
158 for (k
=0 ; k
<8 ; k
++) {
159 oddparity
^= (((frame
[j
] & 0xFF) >> k
) & 0x01);
162 uint8_t parityBits
= parityBytes
[j
>>3];
164 if (isResponse
&& (oddparity
!= ((parityBits
>> (7-(j
&0x0007))) & 0x01))) {
165 sprintf(line
[j
/16]+((j
%16)*4), "%02x! ", frame
[j
]);
167 sprintf(line
[j
/16]+((j
%16)*4), "%02x ", frame
[j
]);
170 //--- Draw the CRC column
171 bool crcError
= false;
177 if(!isResponse
&& data_len
== 4 ) {
178 // Rough guess that this is a command from the reader
179 // For iClass the command byte is not part of the CRC
180 ComputeCrc14443(CRC_ICLASS
, &frame
[1], data_len
-3, &b1
, &b2
);
183 // For other data.. CRC might not be applicable (UPDATE commands etc.)
184 ComputeCrc14443(CRC_ICLASS
, frame
, data_len
-2, &b1
, &b2
);
187 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
193 ComputeCrc14443(CRC_14443_A
, frame
, data_len
-2, &b1
, &b2
);
195 if (b1
!= frame
[data_len
-2] || b2
!= frame
[data_len
-1]) {
196 if(!(isResponse
& (data_len
< 6)))
204 char *crc
= crcError
? "!crc" :" ";
206 EndOfTransmissionTimestamp
= timestamp
+ duration
;
210 if(iclass
) annotateIclass(explanation
,sizeof(explanation
),frame
,data_len
);
211 else annotateIso14443a(explanation
,sizeof(explanation
),frame
,data_len
);
214 int num_lines
= (data_len
- 1)/16 + 1;
215 for (int j
= 0; j
< num_lines
; j
++) {
217 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
218 (timestamp
- first_timestamp
),
219 (EndOfTransmissionTimestamp
- first_timestamp
),
220 (isResponse
? "Tag" : "Rdr"),
222 (j
== num_lines
-1) ? crc
: " ",
223 (j
== num_lines
-1) ? explanation
: "");
225 PrintAndLog(" | | | %-64s| %s| %s",
227 (j
== num_lines
-1)?crc
:" ",
228 (j
== num_lines
-1) ? explanation
: "");
232 bool next_isResponse
= *((uint16_t *)(trace
+ tracepos
+ 6)) & 0x8000;
234 if (showWaitCycles
&& !isResponse
&& next_isResponse
) {
235 uint32_t next_timestamp
= *((uint32_t *)(trace
+ tracepos
));
236 if (next_timestamp
!= 0x44444444) {
237 PrintAndLog(" %9d | %9d | %s | fdt (Frame Delay Time): %d",
238 (EndOfTransmissionTimestamp
- first_timestamp
),
239 (next_timestamp
- first_timestamp
),
241 (next_timestamp
- EndOfTransmissionTimestamp
));
247 int CmdHFList(const char *Cmd
)
249 bool showWaitCycles
= false;
251 int tlen
= param_getstr(Cmd
,0,type
);
252 char param
= param_getchar(Cmd
, 1);
256 if(tlen
== 0 || (strcmp(type
, "iclass") != 0 && strcmp(type
,"14a") != 0))
260 if(param
== 'h' || (param
!=0 && param
!= 'f'))
266 PrintAndLog("List protocol data in trace buffer.");
267 PrintAndLog("Usage: hf list [14a|iclass] [f]");
268 PrintAndLog(" 14a - interpret data as iso14443a communications");
269 PrintAndLog(" iclass - interpret data as iclass communications");
270 PrintAndLog(" f - show frame delay times as well");
272 PrintAndLog("example: hf list 14a f");
273 PrintAndLog("example: hf list iclass");
276 if(strcmp(type
, "iclass") == 0)
282 showWaitCycles
= true;
286 uint8_t trace
[TRACE_SIZE
];
287 uint16_t tracepos
= 0;
288 GetFromBigBuf(trace
, TRACE_SIZE
, 0);
289 WaitForResponse(CMD_ACK
, NULL
);
291 PrintAndLog("Recorded Activity");
293 PrintAndLog("Start = Start of Start Bit, End = End of last modulation. Src = Source of Transfer");
294 PrintAndLog("iso14443a - All times are in carrier periods (1/13.56Mhz)");
295 PrintAndLog("iClass - Timings are not as accurate");
297 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
298 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
300 while(tracepos
< TRACE_SIZE
)
302 tracepos
= printTraceLine(tracepos
, trace
, iclass
, showWaitCycles
);
308 static command_t CommandTable
[] =
310 {"help", CmdHelp
, 1, "This help"},
311 {"14a", CmdHF14A
, 1, "{ ISO14443A RFIDs... }"},
312 {"14b", CmdHF14B
, 1, "{ ISO14443B RFIDs... }"},
313 {"15", CmdHF15
, 1, "{ ISO15693 RFIDs... }"},
314 {"epa", CmdHFEPA
, 1, "{ German Identification Card... }"},
315 {"legic", CmdHFLegic
, 0, "{ LEGIC RFIDs... }"},
316 {"iclass", CmdHFiClass
, 1, "{ ICLASS RFIDs... }"},
317 {"mf", CmdHFMF
, 1, "{ MIFARE RFIDs... }"},
318 {"tune", CmdHFTune
, 0, "Continuously measure HF antenna tuning"},
319 {"list", CmdHFList
, 1, "List protocol data in trace buffer"},
320 {NULL
, NULL
, 0, NULL
}
323 int CmdHF(const char *Cmd
)
325 CmdsParse(CommandTable
, Cmd
);
329 int CmdHelp(const char *Cmd
)
331 CmdsHelp(CommandTable
);