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