]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhf.c
CHG: generic code clean up. Removal of commented code.
[proxmark3-svn] / client / cmdhf.c
CommitLineData
a553f267 1//-----------------------------------------------------------------------------
2// Copyright (C) 2010 iZsh <izsh at fail0verflow.com>
3//
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
6// the license.
7//-----------------------------------------------------------------------------
8// High frequency commands
9//-----------------------------------------------------------------------------
10
7fe9b0b7 11#include <stdio.h>
4c3de57a 12#include <string.h>
902cb3c0 13#include "proxmark3.h"
7fe9b0b7 14#include "graph.h"
15#include "ui.h"
16#include "cmdparser.h"
17#include "cmdhf.h"
18#include "cmdhf14a.h"
19#include "cmdhf14b.h"
20#include "cmdhf15.h"
5acd09bd 21#include "cmdhfepa.h"
7fe9b0b7 22#include "cmdhflegic.h"
cee5a30d 23#include "cmdhficlass.h"
9ca155ba 24#include "cmdhfmf.h"
7fe9b0b7 25
26static int CmdHelp(const char *Cmd);
27
28int CmdHFTune(const char *Cmd)
29{
30 UsbCommand c={CMD_MEASURE_ANTENNA_TUNING_HF};
31 SendCommand(&c);
32 return 0;
33}
4c3de57a
MHS
34// for the time being. Need better Bigbuf handling.
35#define TRACE_SIZE 3000
36
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
46
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
59
60
61void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
62{
63 switch(cmd[0])
64 {
65 case iso14443_CMD_WUPA: snprintf(exp,size,"WUPA"); break;
66 case iso14443_CMD_SELECT:{
67 if(cmdsize > 2)
68 {
69 snprintf(exp,size,"SELECT_UID"); break;
70 }else
71 {
72 snprintf(exp,size,"SELECT_ALL"); break;
73 }
74 }
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;
86 }
87 return;
88}
89
90void annotateIclass(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize)
91{
92
93 if(cmdsize > 1 && cmd[0] == ICLASS_CMD_READ)
94 {
95 snprintf(exp,size,"READ(%d)",cmd[1]);
96 return;
97 }
98
99 switch(cmd[0])
100 {
337818f7 101 case ICLASS_CMD_ACTALL: snprintf(exp,size,"ACTALL"); break;
4c3de57a 102 case ICLASS_CMD_IDENTIFY: snprintf(exp,size,"IDENTIFY"); break;
337818f7
MHS
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;
4c3de57a
MHS
110 }
111 return;
112}
113
114
115
116uint16_t printTraceLine(uint16_t tracepos, uint8_t* trace, bool iclass, bool showWaitCycles)
117{
118 bool isResponse;
119 uint16_t duration, data_len,parity_len;
120
121 uint32_t timestamp, first_timestamp, EndOfTransmissionTimestamp;
122 char explanation[30] = {0};
123
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;
128
129 tracepos += 4;
130 duration = *((uint16_t *)(trace + tracepos));
131 tracepos += 2;
132 data_len = *((uint16_t *)(trace + tracepos));
133 tracepos += 2;
134
135 if (data_len & 0x8000) {
136 data_len &= 0x7fff;
137 isResponse = true;
138 } else {
139 isResponse = false;
140 }
141 parity_len = (data_len-1)/8 + 1;
142
143 if (tracepos + data_len + parity_len >= TRACE_SIZE) {
144 return TRACE_SIZE;
145 }
146
147 uint8_t *frame = trace + tracepos;
148 tracepos += data_len;
149 uint8_t *parityBytes = trace + tracepos;
150 tracepos += parity_len;
151
152 //--- Draw the data column
153 char line[16][110];
154 for (int j = 0; j < data_len; j++) {
155 int oddparity = 0x01;
156 int k;
157
158 for (k=0 ; k<8 ; k++) {
159 oddparity ^= (((frame[j] & 0xFF) >> k) & 0x01);
160 }
161
162 uint8_t parityBits = parityBytes[j>>3];
163
164 if (isResponse && (oddparity != ((parityBits >> (7-(j&0x0007))) & 0x01))) {
165 sprintf(line[j/16]+((j%16)*4), "%02x! ", frame[j]);
166 } else {
167 sprintf(line[j/16]+((j%16)*4), "%02x ", frame[j]);
168 }
169 }
170 //--- Draw the CRC column
171 bool crcError = false;
172
173 if (data_len > 2) {
174 uint8_t b1, b2;
175 if(iclass)
176 {
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);
181 }
182 else {
183 // For other data.. CRC might not be applicable (UPDATE commands etc.)
184 ComputeCrc14443(CRC_ICLASS, frame, data_len-2, &b1, &b2);
185 }
186
187 if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) {
188 crcError = true;
189 }
190
191 }else{//Iso 14443a
192
193 ComputeCrc14443(CRC_14443_A, frame, data_len-2, &b1, &b2);
194
195 if (b1 != frame[data_len-2] || b2 != frame[data_len-1]) {
196 if(!(isResponse & (data_len < 6)))
197 {
198 crcError = true;
199 }
200 }
201 }
202
203 }
204 char *crc = crcError ? "!crc" :" ";
205
206 EndOfTransmissionTimestamp = timestamp + duration;
207
208 if(!isResponse)
209 {
210 if(iclass) annotateIclass(explanation,sizeof(explanation),frame,data_len);
211 else annotateIso14443a(explanation,sizeof(explanation),frame,data_len);
212 }
213
214 int num_lines = (data_len - 1)/16 + 1;
215 for (int j = 0; j < num_lines; j++) {
216 if (j == 0) {
217 PrintAndLog(" %9d | %9d | %s | %-64s| %s| %s",
218 (timestamp - first_timestamp),
219 (EndOfTransmissionTimestamp - first_timestamp),
220 (isResponse ? "Tag" : "Rdr"),
221 line[j],
222 (j == num_lines-1) ? crc : " ",
223 (j == num_lines-1) ? explanation : "");
224 } else {
225 PrintAndLog(" | | | %-64s| %s| %s",
226 line[j],
227 (j == num_lines-1)?crc:" ",
228 (j == num_lines-1) ? explanation : "");
229 }
230 }
231
232 bool next_isResponse = *((uint16_t *)(trace + tracepos + 6)) & 0x8000;
233
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),
240 " ",
241 (next_timestamp - EndOfTransmissionTimestamp));
242 }
243 }
244 return tracepos;
245}
246
247int CmdHFList(const char *Cmd)
248{
249 bool showWaitCycles = false;
250 char type[40] = {0};
251 int tlen = param_getstr(Cmd,0,type);
252 char param = param_getchar(Cmd, 1);
253 bool errors = false;
254 bool iclass = false;
255 //Validate params
256 if(tlen == 0 || (strcmp(type, "iclass") != 0 && strcmp(type,"14a") != 0))
257 {
258 errors = true;
259 }
260 if(param == 'h' || (param !=0 && param != 'f'))
261 {
262 errors = true;
263 }
264
265 if (errors) {
266 PrintAndLog("List protocol data in trace buffer.");
267 PrintAndLog("Usage: hf list [14a|iclass] [f]");
337818f7 268 PrintAndLog(" 14a - interpret data as iso14443a communications");
4c3de57a 269 PrintAndLog(" iclass - interpret data as iclass communications");
337818f7 270 PrintAndLog(" f - show frame delay times as well");
4c3de57a
MHS
271 PrintAndLog("");
272 PrintAndLog("example: hf list 14a f");
273 PrintAndLog("example: hf list iclass");
274 return 0;
275 }
276 if(strcmp(type, "iclass") == 0)
277 {
278 iclass = true;
279 }
280
281 if (param == 'f') {
282 showWaitCycles = true;
283 }
284
285
286 uint8_t trace[TRACE_SIZE];
287 uint16_t tracepos = 0;
288 GetFromBigBuf(trace, TRACE_SIZE, 0);
289 WaitForResponse(CMD_ACK, NULL);
290
291 PrintAndLog("Recorded Activity");
292 PrintAndLog("");
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");
296 PrintAndLog("");
297 PrintAndLog(" Start | End | Src | Data (! denotes parity error) | CRC | Annotation |");
298 PrintAndLog("-----------|-----------|-----|-----------------------------------------------------------------|-----|--------------------|");
299
300 while(tracepos < TRACE_SIZE)
301 {
302 tracepos = printTraceLine(tracepos, trace, iclass, showWaitCycles);
303 }
304 return 0;
305}
306
7fe9b0b7 307
308static command_t CommandTable[] =
309{
310 {"help", CmdHelp, 1, "This help"},
37239a7c 311 {"14a", CmdHF14A, 1, "{ ISO14443A RFIDs... }"},
312 {"14b", CmdHF14B, 1, "{ ISO14443B RFIDs... }"},
313 {"15", CmdHF15, 1, "{ ISO15693 RFIDs... }"},
5acd09bd 314 {"epa", CmdHFEPA, 1, "{ German Identification Card... }"},
8e220a91 315 {"legic", CmdHFLegic, 0, "{ LEGIC RFIDs... }"},
cee5a30d 316 {"iclass", CmdHFiClass, 1, "{ ICLASS RFIDs... }"},
4c3de57a 317 {"mf", CmdHFMF, 1, "{ MIFARE RFIDs... }"},
c59c3405 318 {"tune", CmdHFTune, 0, "Continuously measure HF antenna tuning"},
4c3de57a
MHS
319 {"list", CmdHFList, 1, "List protocol data in trace buffer"},
320 {NULL, NULL, 0, NULL}
7fe9b0b7 321};
322
323int CmdHF(const char *Cmd)
324{
325 CmdsParse(CommandTable, Cmd);
326 return 0;
327}
328
329int CmdHelp(const char *Cmd)
330{
331 CmdsHelp(CommandTable);
332 return 0;
333}
Impressum, Datenschutz