]>
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" |
2bdd68c3 | 20 | #include "cmddata.h" |
bfb01844 | 21 | #include "data.h" |
7fe9b0b7 | 22 | |
23 | /* low-level hardware control */ | |
24 | ||
25 | static int CmdHelp(const char *Cmd); | |
26 | ||
e6153040 | 27 | static void lookupChipID(uint32_t iChipID, uint32_t mem_used) |
4f269f63 | 28 | { |
29 | char asBuff[100]; | |
e6153040 | 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: | |
e6153040 | 109 | mem_avail = 0; |
4f269f63 | 110 | break; |
111 | case 1: | |
e6153040 | 112 | mem_avail = 8; |
4f269f63 | 113 | break; |
114 | case 2: | |
e6153040 | 115 | mem_avail = 16; |
4f269f63 | 116 | break; |
117 | case 3: | |
e6153040 | 118 | mem_avail = 32; |
4f269f63 | 119 | break; |
120 | case 5: | |
e6153040 | 121 | mem_avail = 64; |
4f269f63 | 122 | break; |
123 | case 7: | |
e6153040 | 124 | mem_avail = 128; |
4f269f63 | 125 | break; |
126 | case 9: | |
e6153040 | 127 | mem_avail = 256; |
4f269f63 | 128 | break; |
129 | case 10: | |
e6153040 | 130 | mem_avail = 512; |
4f269f63 | 131 | break; |
132 | case 12: | |
e6153040 | 133 | mem_avail = 1024; |
4f269f63 | 134 | break; |
135 | case 14: | |
e6153040 | 136 | mem_avail = 2048; |
4f269f63 | 137 | break; |
138 | } | |
e6153040 | 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 | { | |
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 | SendCommand(&c); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | // ## FPGA Control | |
328 | int CmdFPGAOff(const char *Cmd) | |
329 | { | |
330 | UsbCommand c = {CMD_FPGA_MAJOR_MODE_OFF}; | |
331 | SendCommand(&c); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | int CmdLCD(const char *Cmd) | |
336 | { | |
337 | int i, j; | |
338 | ||
339 | UsbCommand c={CMD_LCD}; | |
340 | sscanf(Cmd, "%x %d", &i, &j); | |
341 | while (j--) { | |
342 | c.arg[0] = i & 0x1ff; | |
343 | SendCommand(&c); | |
344 | } | |
345 | return 0; | |
346 | } | |
347 | ||
348 | int CmdLCDReset(const char *Cmd) | |
349 | { | |
350 | UsbCommand c = {CMD_LCD_RESET, {strtol(Cmd, NULL, 0), 0, 0}}; | |
351 | SendCommand(&c); | |
352 | return 0; | |
353 | } | |
354 | ||
355 | int CmdReadmem(const char *Cmd) | |
356 | { | |
357 | UsbCommand c = {CMD_READ_MEM, {strtol(Cmd, NULL, 0), 0, 0}}; | |
358 | SendCommand(&c); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | int CmdReset(const char *Cmd) | |
363 | { | |
364 | UsbCommand c = {CMD_HARDWARE_RESET}; | |
365 | SendCommand(&c); | |
366 | return 0; | |
367 | } | |
368 | ||
369 | /* | |
370 | * Sets the divisor for LF frequency clock: lets the user choose any LF frequency below | |
371 | * 600kHz. | |
372 | */ | |
373 | int CmdSetDivisor(const char *Cmd) | |
374 | { | |
375 | UsbCommand c = {CMD_SET_LF_DIVISOR, {strtol(Cmd, NULL, 0), 0, 0}}; | |
7bb9d33e | 376 | if (c.arg[0] < 19 || c.arg[0] > 255) { |
7fe9b0b7 | 377 | PrintAndLog("divisor must be between 19 and 255"); |
378 | } else { | |
379 | SendCommand(&c); | |
380 | PrintAndLog("Divisor set, expected freq=%dHz", 12000000 / (c.arg[0]+1)); | |
381 | } | |
382 | return 0; | |
383 | } | |
384 | ||
385 | int CmdSetMux(const char *Cmd) | |
386 | { | |
387 | UsbCommand c={CMD_SET_ADC_MUX}; | |
388 | if (strcmp(Cmd, "lopkd") == 0) { | |
389 | c.arg[0] = 0; | |
390 | } else if (strcmp(Cmd, "loraw") == 0) { | |
391 | c.arg[0] = 1; | |
392 | } else if (strcmp(Cmd, "hipkd") == 0) { | |
393 | c.arg[0] = 2; | |
394 | } else if (strcmp(Cmd, "hiraw") == 0) { | |
395 | c.arg[0] = 3; | |
396 | } | |
397 | SendCommand(&c); | |
398 | return 0; | |
399 | } | |
400 | ||
401 | int CmdTune(const char *Cmd) | |
402 | { | |
2bdd68c3 | 403 | return CmdTuneSamples(Cmd); |
7fe9b0b7 | 404 | } |
405 | ||
406 | int CmdVersion(const char *Cmd) | |
407 | { | |
8e074056 | 408 | |
e2012d1b | 409 | clearCommandBuffer(); |
8e074056 | 410 | UsbCommand c = {CMD_VERSION}; |
e2012d1b | 411 | static UsbCommand resp = {0, {0, 0, 0}}; |
db2dc28d | 412 | |
e2012d1b MHS |
413 | if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available |
414 | SendCommand(&c); | |
415 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
416 | PrintAndLog("Prox/RFID mark3 RFID instrument"); | |
417 | PrintAndLog((char*)resp.d.asBytes); | |
418 | lookupChipID(resp.arg[0], resp.arg[1]); | |
419 | } | |
420 | } else { | |
421 | PrintAndLog("[[[ Cached information ]]]\n"); | |
8e074056 | 422 | PrintAndLog("Prox/RFID mark3 RFID instrument"); |
423 | PrintAndLog((char*)resp.d.asBytes); | |
424 | lookupChipID(resp.arg[0], resp.arg[1]); | |
e2012d1b | 425 | PrintAndLog(""); |
8e074056 | 426 | } |
e2012d1b MHS |
427 | return 0; |
428 | } | |
db2dc28d | 429 | |
e2012d1b MHS |
430 | int CmdStatus(const char *Cmd) |
431 | { | |
bfb01844 | 432 | uint8_t speed_test_buffer[USB_CMD_DATA_SIZE]; |
433 | sample_buf = speed_test_buffer; | |
434 | #define USB_SPEED_TEST_SIZE (1000*USB_CMD_DATA_SIZE) | |
435 | ||
436 | clearCommandBuffer(); | |
437 | UsbCommand c = {CMD_STATUS, {USB_SPEED_TEST_SIZE}}; | |
e2012d1b | 438 | SendCommand(&c); |
bfb01844 | 439 | if (!WaitForResponseTimeout(CMD_ACK,&c,1500)) { |
440 | PrintAndLog("Status command failed. USB Speed Test timed out"); | |
441 | } | |
e2012d1b MHS |
442 | return 0; |
443 | } | |
444 | ||
bfb01844 | 445 | |
e2012d1b MHS |
446 | int CmdPing(const char *Cmd) |
447 | { | |
448 | clearCommandBuffer(); | |
449 | UsbCommand resp; | |
450 | UsbCommand c = {CMD_PING}; | |
451 | SendCommand(&c); | |
452 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { | |
453 | PrintAndLog("Ping successfull"); | |
454 | }else{ | |
455 | PrintAndLog("Ping failed"); | |
456 | } | |
8e074056 | 457 | return 0; |
7fe9b0b7 | 458 | } |
459 | ||
460 | static command_t CommandTable[] = | |
461 | { | |
e2012d1b MHS |
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"}, | |
473 | {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"}, | |
474 | {"ping", CmdPing, 0, "Test if the pm3 is responsive"}, | |
475 | {NULL, NULL, 0, NULL} | |
7fe9b0b7 | 476 | }; |
477 | ||
478 | int CmdHW(const char *Cmd) | |
479 | { | |
480 | CmdsParse(CommandTable, Cmd); | |
481 | return 0; | |
482 | } | |
483 | ||
484 | int CmdHelp(const char *Cmd) | |
485 | { | |
486 | CmdsHelp(CommandTable); | |
487 | return 0; | |
488 | } |