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