| 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 | |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | #include <limits.h> |
| 15 | #include "ui.h" |
| 16 | #include "proxmark3.h" |
| 17 | #include "cmdparser.h" |
| 18 | #include "cmdhw.h" |
| 19 | #include "cmdmain.h" |
| 20 | #include "cmddata.h" |
| 21 | #include "data.h" |
| 22 | |
| 23 | /* low-level hardware control */ |
| 24 | |
| 25 | static int CmdHelp(const char *Cmd); |
| 26 | |
| 27 | static void lookupChipID(uint32_t iChipID, uint32_t mem_used) |
| 28 | { |
| 29 | char asBuff[100]; |
| 30 | uint32_t mem_avail = 0; |
| 31 | |
| 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: |
| 109 | mem_avail = 0; |
| 110 | break; |
| 111 | case 1: |
| 112 | mem_avail = 8; |
| 113 | break; |
| 114 | case 2: |
| 115 | mem_avail = 16; |
| 116 | break; |
| 117 | case 3: |
| 118 | mem_avail = 32; |
| 119 | break; |
| 120 | case 5: |
| 121 | mem_avail = 64; |
| 122 | break; |
| 123 | case 7: |
| 124 | mem_avail = 128; |
| 125 | break; |
| 126 | case 9: |
| 127 | mem_avail = 256; |
| 128 | break; |
| 129 | case 10: |
| 130 | mem_avail = 512; |
| 131 | break; |
| 132 | case 12: |
| 133 | mem_avail = 1024; |
| 134 | break; |
| 135 | case 14: |
| 136 | mem_avail = 2048; |
| 137 | break; |
| 138 | } |
| 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 | ); |
| 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 | |
| 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}}; |
| 376 | if (c.arg[0] < 19 || c.arg[0] > 255) { |
| 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 | { |
| 403 | return CmdTuneSamples(Cmd); |
| 404 | } |
| 405 | |
| 406 | int CmdVersion(const char *Cmd) |
| 407 | { |
| 408 | |
| 409 | clearCommandBuffer(); |
| 410 | UsbCommand c = {CMD_VERSION}; |
| 411 | static UsbCommand resp = {0, {0, 0, 0}}; |
| 412 | |
| 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"); |
| 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 | |
| 430 | int CmdStatus(const char *Cmd) |
| 431 | { |
| 432 | uint8_t speed_test_buffer[USB_CMD_DATA_SIZE]; |
| 433 | sample_buf = speed_test_buffer; |
| 434 | |
| 435 | clearCommandBuffer(); |
| 436 | UsbCommand c = {CMD_STATUS}; |
| 437 | SendCommand(&c); |
| 438 | if (!WaitForResponseTimeout(CMD_ACK,&c,1900)) { |
| 439 | PrintAndLog("Status command failed. USB Speed Test timed out"); |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | int CmdPing(const char *Cmd) |
| 446 | { |
| 447 | clearCommandBuffer(); |
| 448 | UsbCommand resp; |
| 449 | UsbCommand c = {CMD_PING}; |
| 450 | SendCommand(&c); |
| 451 | if (WaitForResponseTimeout(CMD_ACK,&resp,1000)) { |
| 452 | PrintAndLog("Ping successful"); |
| 453 | }else{ |
| 454 | PrintAndLog("Ping failed"); |
| 455 | } |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static command_t CommandTable[] = |
| 460 | { |
| 461 | {"help", CmdHelp, 1, "This help"}, |
| 462 | {"detectreader", CmdDetectReader,0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"}, |
| 463 | {"fpgaoff", CmdFPGAOff, 0, "Set FPGA off"}, |
| 464 | {"lcd", CmdLCD, 0, "<HEX command> <count> -- Send command/data to LCD"}, |
| 465 | {"lcdreset", CmdLCDReset, 0, "Hardware reset LCD"}, |
| 466 | {"readmem", CmdReadmem, 0, "[address] -- Read memory at decimal address from flash"}, |
| 467 | {"reset", CmdReset, 0, "Reset the Proxmark3"}, |
| 468 | {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"}, |
| 469 | {"setmux", CmdSetMux, 0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"}, |
| 470 | {"tune", CmdTune, 0, "['l'|'h'] -- Measure antenna tuning (option 'l' or 'h' to limit to LF or HF)"}, |
| 471 | {"version", CmdVersion, 0, "Show version information about the connected Proxmark"}, |
| 472 | {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"}, |
| 473 | {"ping", CmdPing, 0, "Test if the pm3 is responsive"}, |
| 474 | {NULL, NULL, 0, NULL} |
| 475 | }; |
| 476 | |
| 477 | int CmdHW(const char *Cmd) |
| 478 | { |
| 479 | CmdsParse(CommandTable, Cmd); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | int CmdHelp(const char *Cmd) |
| 484 | { |
| 485 | CmdsHelp(CommandTable); |
| 486 | return 0; |
| 487 | } |