]> git.zerfleddert.de Git - proxmark3-svn/blame - client/cmdhw.c
add: added a noncedistance method
[proxmark3-svn] / client / cmdhw.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// 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
25static int CmdHelp(const char *Cmd);
26
9783989b 27static 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 311int 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
328int CmdFPGAOff(const char *Cmd)
329{
330 UsbCommand c = {CMD_FPGA_MAJOR_MODE_OFF};
331 SendCommand(&c);
332 return 0;
333}
334
335int 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
348int 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
355int 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
362int 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 */
373int 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
385int 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
401int CmdTune(const char *Cmd)
402{
d3499d36 403 return CmdTuneSamples(Cmd);
7fe9b0b7 404}
405
406int CmdVersion(const char *Cmd)
407{
0de8e387 408
7838f4be 409 clearCommandBuffer();
f62b5e12 410 UsbCommand c = {CMD_VERSION};
9783989b 411 static UsbCommand resp = {0, {0, 0, 0}};
7838f4be 412
9783989b 413 if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available
f62b5e12 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 }
7838f4be 420 } else {
421 PrintAndLog("[[[ Cached information ]]]\n");
422 PrintAndLog("Prox/RFID mark3 RFID instrument");
423 PrintAndLog((char*)resp.d.asBytes);
424 lookupChipID(resp.arg[0], resp.arg[1]);
425 PrintAndLog("");
426 }
427 return 0;
428}
429
430int CmdStatus(const char *Cmd)
431{
f62b5e12 432 uint8_t speed_test_buffer[USB_CMD_DATA_SIZE];
433 sample_buf = speed_test_buffer;
f62b5e12 434
435 clearCommandBuffer();
0de8e387 436 UsbCommand c = {CMD_STATUS};
7838f4be 437 SendCommand(&c);
0de8e387 438 if (!WaitForResponseTimeout(CMD_ACK,&c,1900)) {
f62b5e12 439 PrintAndLog("Status command failed. USB Speed Test timed out");
440 }
7838f4be 441 return 0;
442}
443
f62b5e12 444
7838f4be 445int CmdPing(const char *Cmd)
446{
447 clearCommandBuffer();
448 UsbCommand resp;
449 UsbCommand c = {CMD_PING};
450 SendCommand(&c);
0de8e387 451 if (WaitForResponseTimeout(CMD_ACK,&resp,1000))
7838f4be 452 PrintAndLog("Ping successfull");
0de8e387 453 else
454 PrintAndLog("Ping failed");
455 return 0;
7fe9b0b7 456}
457
458static command_t CommandTable[] =
459{
0de8e387 460 {"help", CmdHelp, 1, "This help"},
461 {"detectreader", CmdDetectReader,0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"},
462 {"fpgaoff", CmdFPGAOff, 0, "Set FPGA off"},
463 {"lcd", CmdLCD, 0, "<HEX command> <count> -- Send command/data to LCD"},
464 {"lcdreset", CmdLCDReset, 0, "Hardware reset LCD"},
465 {"readmem", CmdReadmem, 0, "[address] -- Read memory at decimal address from flash"},
466 {"reset", CmdReset, 0, "Reset the Proxmark3"},
467 {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"},
468 {"setmux", CmdSetMux, 0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"},
469 {"tune", CmdTune, 0, "Measure antenna tuning"},
470 {"version", CmdVersion, 0, "Show version information about the connected Proxmark"},
7838f4be 471 {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"},
472 {"ping", CmdPing, 0, "Test if the pm3 is responsive"},
0de8e387 473 {NULL, NULL, 0, NULL}
7fe9b0b7 474};
475
4c36581b 476int CmdHW(const char *Cmd) {
477 clearCommandBuffer();
0de8e387 478 CmdsParse(CommandTable, Cmd);
479 return 0;
7fe9b0b7 480}
481
482int CmdHelp(const char *Cmd)
483{
0de8e387 484 CmdsHelp(CommandTable);
485 return 0;
7fe9b0b7 486}
Impressum, Datenschutz