]>
Commit | Line | Data |
---|---|---|
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 | // Hardware commands | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
7fe9b0b7 | 11 | #include <stdio.h> |
12 | #include <stdlib.h> | |
13 | #include <string.h> | |
14 | #include <limits.h> | |
15 | #include "ui.h" | |
902cb3c0 | 16 | #include "proxmark3.h" |
7fe9b0b7 | 17 | #include "cmdparser.h" |
18 | #include "cmdhw.h" | |
4f269f63 | 19 | #include "cmdmain.h" |
14edfd09 | 20 | #include "cmddata.h" |
f62b5e12 | 21 | #include "data.h" |
7fe9b0b7 | 22 | |
23 | /* low-level hardware control */ | |
24 | ||
25 | static int CmdHelp(const char *Cmd); | |
26 | ||
9783989b | 27 | static void lookupChipID(uint32_t iChipID, uint32_t mem_used) |
4f269f63 | 28 | { |
29 | char asBuff[100]; | |
9783989b | 30 | uint32_t mem_avail = 0; |
31 | ||
4f269f63 | 32 | switch(iChipID) |
33 | { | |
34 | case 0x270B0A40: | |
35 | sprintf(asBuff,"AT91SAM7S512 Rev A"); | |
36 | break; | |
37 | case 0x270B0A4F: | |
38 | sprintf(asBuff,"AT91SAM7S512 Rev B"); | |
39 | break; | |
40 | case 0x270D0940: | |
41 | sprintf(asBuff,"AT91SAM7S256 Rev A"); | |
42 | break; | |
43 | case 0x270B0941: | |
44 | sprintf(asBuff,"AT91SAM7S256 Rev B"); | |
45 | break; | |
46 | case 0x270B0942: | |
47 | sprintf(asBuff,"AT91SAM7S256 Rev C"); | |
48 | break; | |
49 | case 0x270B0943: | |
50 | sprintf(asBuff,"AT91SAM7S256 Rev D"); | |
51 | break; | |
52 | case 0x270C0740: | |
53 | sprintf(asBuff,"AT91SAM7S128 Rev A"); | |
54 | break; | |
55 | case 0x270A0741: | |
56 | sprintf(asBuff,"AT91SAM7S128 Rev B"); | |
57 | break; | |
58 | case 0x270A0742: | |
59 | sprintf(asBuff,"AT91SAM7S128 Rev C"); | |
60 | break; | |
61 | case 0x270A0743: | |
62 | sprintf(asBuff,"AT91SAM7S128 Rev D"); | |
63 | break; | |
64 | case 0x27090540: | |
65 | sprintf(asBuff,"AT91SAM7S64 Rev A"); | |
66 | break; | |
67 | case 0x27090543: | |
68 | sprintf(asBuff,"AT91SAM7S64 Rev B"); | |
69 | break; | |
70 | case 0x27090544: | |
71 | sprintf(asBuff,"AT91SAM7S64 Rev C"); | |
72 | break; | |
73 | case 0x27080342: | |
74 | sprintf(asBuff,"AT91SAM7S321 Rev A"); | |
75 | break; | |
76 | case 0x27080340: | |
77 | sprintf(asBuff,"AT91SAM7S32 Rev A"); | |
78 | break; | |
79 | case 0x27080341: | |
80 | sprintf(asBuff,"AT91SAM7S32 Rev B"); | |
81 | break; | |
82 | case 0x27050241: | |
83 | sprintf(asBuff,"AT9SAM7S161 Rev A"); | |
84 | break; | |
85 | case 0x27050240: | |
86 | sprintf(asBuff,"AT91SAM7S16 Rev A"); | |
87 | break; | |
88 | } | |
89 | PrintAndLog("uC: %s",asBuff); | |
90 | switch((iChipID&0xE0)>>5) | |
91 | { | |
92 | case 1: | |
93 | sprintf(asBuff,"ARM946ES"); | |
94 | break; | |
95 | case 2: | |
96 | sprintf(asBuff,"ARM7TDMI"); | |
97 | break; | |
98 | case 4: | |
99 | sprintf(asBuff,"ARM920T"); | |
100 | break; | |
101 | case 5: | |
102 | sprintf(asBuff,"ARM926EJS"); | |
103 | break; | |
104 | } | |
105 | PrintAndLog("Embedded Processor: %s",asBuff); | |
106 | switch((iChipID&0xF00)>>8) | |
107 | { | |
108 | case 0: | |
9783989b | 109 | mem_avail = 0; |
4f269f63 | 110 | break; |
111 | case 1: | |
9783989b | 112 | mem_avail = 8; |
4f269f63 | 113 | break; |
114 | case 2: | |
9783989b | 115 | mem_avail = 16; |
4f269f63 | 116 | break; |
117 | case 3: | |
9783989b | 118 | mem_avail = 32; |
4f269f63 | 119 | break; |
120 | case 5: | |
9783989b | 121 | mem_avail = 64; |
4f269f63 | 122 | break; |
123 | case 7: | |
9783989b | 124 | mem_avail = 128; |
4f269f63 | 125 | break; |
126 | case 9: | |
9783989b | 127 | mem_avail = 256; |
4f269f63 | 128 | break; |
129 | case 10: | |
9783989b | 130 | mem_avail = 512; |
4f269f63 | 131 | break; |
132 | case 12: | |
9783989b | 133 | mem_avail = 1024; |
4f269f63 | 134 | break; |
135 | case 14: | |
9783989b | 136 | mem_avail = 2048; |
4f269f63 | 137 | break; |
138 | } | |
9783989b | 139 | PrintAndLog("Nonvolatile Program Memory Size: %dK bytes. Used: %d bytes (%2.0f\%). Free: %d bytes (%2.0f\%).", |
140 | mem_avail, | |
141 | mem_used, | |
142 | mem_avail == 0 ? 0 : (float)mem_used/(mem_avail*1024)*100, | |
143 | mem_avail*1024 - mem_used, | |
144 | mem_avail == 0 ? 0 : (float)(mem_avail*1024-mem_used)/(mem_avail*1024)*100 | |
145 | ); | |
4f269f63 | 146 | switch((iChipID&0xF000)>>12) |
147 | { | |
148 | case 0: | |
149 | sprintf(asBuff,"None"); | |
150 | break; | |
151 | case 1: | |
152 | sprintf(asBuff,"8K bytes"); | |
153 | break; | |
154 | case 2: | |
155 | sprintf(asBuff,"16K bytes"); | |
156 | break; | |
157 | case 3: | |
158 | sprintf(asBuff,"32K bytes"); | |
159 | break; | |
160 | case 5: | |
161 | sprintf(asBuff,"64K bytes"); | |
162 | break; | |
163 | case 7: | |
164 | sprintf(asBuff,"128K bytes"); | |
165 | break; | |
166 | case 9: | |
167 | sprintf(asBuff,"256K bytes"); | |
168 | break; | |
169 | case 10: | |
170 | sprintf(asBuff,"512K bytes"); | |
171 | break; | |
172 | case 12: | |
173 | sprintf(asBuff,"1024K bytes"); | |
174 | break; | |
175 | case 14: | |
176 | sprintf(asBuff,"2048K bytes"); | |
177 | break; | |
178 | } | |
179 | PrintAndLog("Second Nonvolatile Program Memory Size: %s",asBuff); | |
180 | switch((iChipID&0xF0000)>>16) | |
181 | { | |
182 | case 1: | |
183 | sprintf(asBuff,"1K bytes"); | |
184 | break; | |
185 | case 2: | |
186 | sprintf(asBuff,"2K bytes"); | |
187 | break; | |
188 | case 3: | |
189 | sprintf(asBuff,"6K bytes"); | |
190 | break; | |
191 | case 4: | |
192 | sprintf(asBuff,"112K bytes"); | |
193 | break; | |
194 | case 5: | |
195 | sprintf(asBuff,"4K bytes"); | |
196 | break; | |
197 | case 6: | |
198 | sprintf(asBuff,"80K bytes"); | |
199 | break; | |
200 | case 7: | |
201 | sprintf(asBuff,"160K bytes"); | |
202 | break; | |
203 | case 8: | |
204 | sprintf(asBuff,"8K bytes"); | |
205 | break; | |
206 | case 9: | |
207 | sprintf(asBuff,"16K bytes"); | |
208 | break; | |
209 | case 10: | |
210 | sprintf(asBuff,"32K bytes"); | |
211 | break; | |
212 | case 11: | |
213 | sprintf(asBuff,"64K bytes"); | |
214 | break; | |
215 | case 12: | |
216 | sprintf(asBuff,"128K bytes"); | |
217 | break; | |
218 | case 13: | |
219 | sprintf(asBuff,"256K bytes"); | |
220 | break; | |
221 | case 14: | |
222 | sprintf(asBuff,"96K bytes"); | |
223 | break; | |
224 | case 15: | |
225 | sprintf(asBuff,"512K bytes"); | |
226 | break; | |
227 | } | |
228 | PrintAndLog("Internal SRAM Size: %s",asBuff); | |
229 | switch((iChipID&0xFF00000)>>20) | |
230 | { | |
231 | case 0x19: | |
232 | sprintf(asBuff,"AT91SAM9xx Series"); | |
233 | break; | |
234 | case 0x29: | |
235 | sprintf(asBuff,"AT91SAM9XExx Series"); | |
236 | break; | |
237 | case 0x34: | |
238 | sprintf(asBuff,"AT91x34 Series"); | |
239 | break; | |
240 | case 0x37: | |
241 | sprintf(asBuff,"CAP7 Series"); | |
242 | break; | |
243 | case 0x39: | |
244 | sprintf(asBuff,"CAP9 Series"); | |
245 | break; | |
246 | case 0x3B: | |
247 | sprintf(asBuff,"CAP11 Series"); | |
248 | break; | |
249 | case 0x40: | |
250 | sprintf(asBuff,"AT91x40 Series"); | |
251 | break; | |
252 | case 0x42: | |
253 | sprintf(asBuff,"AT91x42 Series"); | |
254 | break; | |
255 | case 0x55: | |
256 | sprintf(asBuff,"AT91x55 Series"); | |
257 | break; | |
258 | case 0x60: | |
259 | sprintf(asBuff,"AT91SAM7Axx Series"); | |
260 | break; | |
261 | case 0x61: | |
262 | sprintf(asBuff,"AT91SAM7AQxx Series"); | |
263 | break; | |
264 | case 0x63: | |
265 | sprintf(asBuff,"AT91x63 Series"); | |
266 | break; | |
267 | case 0x70: | |
268 | sprintf(asBuff,"AT91SAM7Sxx Series"); | |
269 | break; | |
270 | case 0x71: | |
271 | sprintf(asBuff,"AT91SAM7XCxx Series"); | |
272 | break; | |
273 | case 0x72: | |
274 | sprintf(asBuff,"AT91SAM7SExx Series"); | |
275 | break; | |
276 | case 0x73: | |
277 | sprintf(asBuff,"AT91SAM7Lxx Series"); | |
278 | break; | |
279 | case 0x75: | |
280 | sprintf(asBuff,"AT91SAM7Xxx Series"); | |
281 | break; | |
282 | case 0x92: | |
283 | sprintf(asBuff,"AT91x92 Series"); | |
284 | break; | |
285 | case 0xF0: | |
286 | sprintf(asBuff,"AT75Cxx Series"); | |
287 | break; | |
288 | } | |
289 | PrintAndLog("Architecture Identifier: %s",asBuff); | |
290 | switch((iChipID&0x70000000)>>28) | |
291 | { | |
292 | case 0: | |
293 | sprintf(asBuff,"ROM"); | |
294 | break; | |
295 | case 1: | |
296 | sprintf(asBuff,"ROMless or on-chip Flash"); | |
297 | break; | |
298 | case 4: | |
299 | sprintf(asBuff,"SRAM emulating ROM"); | |
300 | break; | |
301 | case 2: | |
302 | sprintf(asBuff,"Embedded Flash Memory"); | |
303 | break; | |
304 | case 3: | |
305 | sprintf(asBuff,"ROM and Embedded Flash Memory\nNVPSIZ is ROM size\nNVPSIZ2 is Flash size"); | |
306 | break; | |
307 | } | |
308 | PrintAndLog("Nonvolatile Program Memory Type: %s",asBuff); | |
309 | } | |
310 | ||
7fe9b0b7 | 311 | int CmdDetectReader(const char *Cmd) |
312 | { | |
3d4207f3 | 313 | UsbCommand c = {CMD_LISTEN_READER_FIELD}; |
314 | // 'l' means LF - 125/134 kHz | |
315 | if(*Cmd == 'l') { | |
316 | c.arg[0] = 1; | |
317 | } else if (*Cmd == 'h') { | |
318 | c.arg[0] = 2; | |
319 | } else if (*Cmd != '\0') { | |
320 | PrintAndLog("use 'detectreader' or 'detectreader l' or 'detectreader h'"); | |
321 | return 0; | |
322 | } | |
323 | clearCommandBuffer(); | |
324 | SendCommand(&c); | |
325 | return 0; | |
7fe9b0b7 | 326 | } |
327 | ||
328 | // ## FPGA Control | |
329 | int CmdFPGAOff(const char *Cmd) | |
330 | { | |
3d4207f3 | 331 | UsbCommand c = {CMD_FPGA_MAJOR_MODE_OFF}; |
332 | clearCommandBuffer(); | |
333 | SendCommand(&c); | |
334 | return 0; | |
7fe9b0b7 | 335 | } |
336 | ||
337 | int CmdLCD(const char *Cmd) | |
338 | { | |
3d4207f3 | 339 | int i, j; |
7fe9b0b7 | 340 | |
3d4207f3 | 341 | UsbCommand c = {CMD_LCD}; |
342 | sscanf(Cmd, "%x %d", &i, &j); | |
343 | while (j--) { | |
344 | c.arg[0] = i & 0x1ff; | |
345 | clearCommandBuffer(); | |
346 | SendCommand(&c); | |
347 | } | |
348 | return 0; | |
7fe9b0b7 | 349 | } |
350 | ||
351 | int CmdLCDReset(const char *Cmd) | |
352 | { | |
3d4207f3 | 353 | UsbCommand c = {CMD_LCD_RESET, {strtol(Cmd, NULL, 0), 0, 0}}; |
354 | clearCommandBuffer(); | |
355 | SendCommand(&c); | |
356 | return 0; | |
7fe9b0b7 | 357 | } |
358 | ||
359 | int CmdReadmem(const char *Cmd) | |
360 | { | |
3d4207f3 | 361 | UsbCommand c = {CMD_READ_MEM, {strtol(Cmd, NULL, 0), 0, 0}}; |
362 | clearCommandBuffer(); | |
363 | SendCommand(&c); | |
364 | return 0; | |
7fe9b0b7 | 365 | } |
366 | ||
367 | int CmdReset(const char *Cmd) | |
368 | { | |
3d4207f3 | 369 | UsbCommand c = {CMD_HARDWARE_RESET}; |
370 | clearCommandBuffer(); | |
371 | SendCommand(&c); | |
372 | return 0; | |
7fe9b0b7 | 373 | } |
374 | ||
375 | /* | |
376 | * Sets the divisor for LF frequency clock: lets the user choose any LF frequency below | |
377 | * 600kHz. | |
378 | */ | |
379 | int CmdSetDivisor(const char *Cmd) | |
380 | { | |
3d4207f3 | 381 | UsbCommand c = {CMD_SET_LF_DIVISOR, {strtol(Cmd, NULL, 0), 0, 0}}; |
382 | ||
383 | if (c.arg[0] < 19 || c.arg[0] > 255) { | |
384 | PrintAndLog("divisor must be between 19 and 255"); | |
385 | return 1; | |
386 | } | |
387 | ||
388 | clearCommandBuffer(); | |
389 | SendCommand(&c); | |
390 | PrintAndLog("Divisor set, expected freq=%dHz", 12000000 / (c.arg[0]+1)); | |
391 | return 0; | |
7fe9b0b7 | 392 | } |
393 | ||
394 | int CmdSetMux(const char *Cmd) | |
395 | { | |
3d4207f3 | 396 | UsbCommand c = {CMD_SET_ADC_MUX}; |
397 | if (strcmp(Cmd, "lopkd") == 0) c.arg[0] = 0; | |
398 | else if (strcmp(Cmd, "loraw") == 0) c.arg[0] = 1; | |
399 | else if (strcmp(Cmd, "hipkd") == 0) c.arg[0] = 2; | |
400 | else if (strcmp(Cmd, "hiraw") == 0) c.arg[0] = 3; | |
401 | clearCommandBuffer(); | |
402 | SendCommand(&c); | |
403 | return 0; | |
7fe9b0b7 | 404 | } |
405 | ||
406 | int CmdTune(const char *Cmd) | |
407 | { | |
d3499d36 | 408 | return CmdTuneSamples(Cmd); |
7fe9b0b7 | 409 | } |
410 | ||
411 | int CmdVersion(const char *Cmd) | |
412 | { | |
7838f4be | 413 | clearCommandBuffer(); |
f62b5e12 | 414 | UsbCommand c = {CMD_VERSION}; |
9783989b | 415 | static UsbCommand resp = {0, {0, 0, 0}}; |
7838f4be | 416 | |
9783989b | 417 | if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available |
f62b5e12 | 418 | SendCommand(&c); |
3d4207f3 | 419 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { |
f62b5e12 | 420 | PrintAndLog("Prox/RFID mark3 RFID instrument"); |
421 | PrintAndLog((char*)resp.d.asBytes); | |
422 | lookupChipID(resp.arg[0], resp.arg[1]); | |
423 | } | |
7838f4be | 424 | } else { |
425 | PrintAndLog("[[[ Cached information ]]]\n"); | |
426 | PrintAndLog("Prox/RFID mark3 RFID instrument"); | |
427 | PrintAndLog((char*)resp.d.asBytes); | |
428 | lookupChipID(resp.arg[0], resp.arg[1]); | |
429 | PrintAndLog(""); | |
430 | } | |
431 | return 0; | |
432 | } | |
433 | ||
434 | int CmdStatus(const char *Cmd) | |
435 | { | |
f62b5e12 | 436 | uint8_t speed_test_buffer[USB_CMD_DATA_SIZE]; |
437 | sample_buf = speed_test_buffer; | |
f62b5e12 | 438 | clearCommandBuffer(); |
0de8e387 | 439 | UsbCommand c = {CMD_STATUS}; |
7838f4be | 440 | SendCommand(&c); |
3d4207f3 | 441 | if (!WaitForResponseTimeout(CMD_ACK, &c, 1900)) |
f62b5e12 | 442 | PrintAndLog("Status command failed. USB Speed Test timed out"); |
7838f4be | 443 | return 0; |
444 | } | |
445 | ||
f62b5e12 | 446 | |
7838f4be | 447 | int CmdPing(const char *Cmd) |
448 | { | |
449 | clearCommandBuffer(); | |
450 | UsbCommand resp; | |
451 | UsbCommand c = {CMD_PING}; | |
452 | SendCommand(&c); | |
3d4207f3 | 453 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) |
7838f4be | 454 | PrintAndLog("Ping successfull"); |
0de8e387 | 455 | else |
456 | PrintAndLog("Ping failed"); | |
457 | return 0; | |
7fe9b0b7 | 458 | } |
459 | ||
460 | static command_t CommandTable[] = | |
461 | { | |
0de8e387 | 462 | {"help", CmdHelp, 1, "This help"}, |
463 | {"detectreader", CmdDetectReader,0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"}, | |
464 | {"fpgaoff", CmdFPGAOff, 0, "Set FPGA off"}, | |
465 | {"lcd", CmdLCD, 0, "<HEX command> <count> -- Send command/data to LCD"}, | |
466 | {"lcdreset", CmdLCDReset, 0, "Hardware reset LCD"}, | |
467 | {"readmem", CmdReadmem, 0, "[address] -- Read memory at decimal address from flash"}, | |
468 | {"reset", CmdReset, 0, "Reset the Proxmark3"}, | |
469 | {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"}, | |
470 | {"setmux", CmdSetMux, 0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"}, | |
471 | {"tune", CmdTune, 0, "Measure antenna tuning"}, | |
472 | {"version", CmdVersion, 0, "Show version information about the connected Proxmark"}, | |
7838f4be | 473 | {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"}, |
474 | {"ping", CmdPing, 0, "Test if the pm3 is responsive"}, | |
0de8e387 | 475 | {NULL, NULL, 0, NULL} |
7fe9b0b7 | 476 | }; |
477 | ||
4c36581b | 478 | int CmdHW(const char *Cmd) { |
479 | clearCommandBuffer(); | |
0de8e387 | 480 | CmdsParse(CommandTable, Cmd); |
481 | return 0; | |
7fe9b0b7 | 482 | } |
483 | ||
484 | int CmdHelp(const char *Cmd) | |
485 | { | |
0de8e387 | 486 | CmdsHelp(CommandTable); |
487 | return 0; | |
7fe9b0b7 | 488 | } |