| 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[120]; |
| 30 | memset(asBuff, 0, sizeof(asBuff)); |
| 31 | uint32_t mem_avail = 0; |
| 32 | |
| 33 | switch(iChipID) { |
| 34 | case 0x270B0A40: sprintf(asBuff,"AT91SAM7S512 Rev A"); break; |
| 35 | case 0x270B0A4F: sprintf(asBuff,"AT91SAM7S512 Rev B"); break; |
| 36 | case 0x270D0940: sprintf(asBuff,"AT91SAM7S256 Rev A"); break; |
| 37 | case 0x270B0941: sprintf(asBuff,"AT91SAM7S256 Rev B"); break; |
| 38 | case 0x270B0942: sprintf(asBuff,"AT91SAM7S256 Rev C"); break; |
| 39 | case 0x270B0943: sprintf(asBuff,"AT91SAM7S256 Rev D"); break; |
| 40 | case 0x270C0740: sprintf(asBuff,"AT91SAM7S128 Rev A"); break; |
| 41 | case 0x270A0741: sprintf(asBuff,"AT91SAM7S128 Rev B"); break; |
| 42 | case 0x270A0742: sprintf(asBuff,"AT91SAM7S128 Rev C"); break; |
| 43 | case 0x270A0743: sprintf(asBuff,"AT91SAM7S128 Rev D"); break; |
| 44 | case 0x27090540: sprintf(asBuff,"AT91SAM7S64 Rev A"); break; |
| 45 | case 0x27090543: sprintf(asBuff,"AT91SAM7S64 Rev B"); break; |
| 46 | case 0x27090544: sprintf(asBuff,"AT91SAM7S64 Rev C"); break; |
| 47 | case 0x27080342: sprintf(asBuff,"AT91SAM7S321 Rev A"); break; |
| 48 | case 0x27080340: sprintf(asBuff,"AT91SAM7S32 Rev A"); break; |
| 49 | case 0x27080341: sprintf(asBuff,"AT91SAM7S32 Rev B"); break; |
| 50 | case 0x27050241: sprintf(asBuff,"AT9SAM7S161 Rev A"); break; |
| 51 | case 0x27050240: sprintf(asBuff,"AT91SAM7S16 Rev A"); break; |
| 52 | } |
| 53 | PrintAndLog("uC: %s",asBuff); |
| 54 | switch( (iChipID & 0xE0) >> 5) { |
| 55 | case 1: sprintf(asBuff,"ARM946ES"); break; |
| 56 | case 2: sprintf(asBuff,"ARM7TDMI"); break; |
| 57 | case 4: sprintf(asBuff,"ARM920T"); break; |
| 58 | case 5: sprintf(asBuff,"ARM926EJS"); break; |
| 59 | } |
| 60 | PrintAndLog("Embedded Processor: %s",asBuff); |
| 61 | switch( (iChipID & 0xF00) >> 8) { |
| 62 | case 0: mem_avail = 0; break; |
| 63 | case 1: mem_avail = 8; break; |
| 64 | case 2: mem_avail = 16; break; |
| 65 | case 3: mem_avail = 32; break; |
| 66 | case 5: mem_avail = 64; break; |
| 67 | case 7: mem_avail = 128; break; |
| 68 | case 9: mem_avail = 256; break; |
| 69 | case 10: mem_avail = 512; break; |
| 70 | case 12: mem_avail = 1024; break; |
| 71 | case 14: mem_avail = 2048; break; |
| 72 | } |
| 73 | |
| 74 | uint32_t mem_left = 0; |
| 75 | if ( mem_avail > 0 ) |
| 76 | mem_left = (mem_avail * 1024) - mem_used; |
| 77 | |
| 78 | PrintAndLog("Nonvolatile Program Memory Size: %uK bytes. Used: %u bytes (%2.0f\%). Free: %u bytes (%2.0f\%).", |
| 79 | mem_avail, |
| 80 | mem_used, |
| 81 | mem_avail == 0 ? 0.0f : (float)mem_used/(mem_avail*1024)*100, |
| 82 | mem_left, |
| 83 | mem_avail == 0 ? 0.0f : (float)mem_left/(mem_avail*1024)*100 |
| 84 | ); |
| 85 | |
| 86 | switch( (iChipID & 0xF000) >> 12 ) { |
| 87 | case 0: sprintf(asBuff,"None"); break; |
| 88 | case 1: sprintf(asBuff,"8K bytes"); break; |
| 89 | case 2: sprintf(asBuff,"16K bytes"); break; |
| 90 | case 3: sprintf(asBuff,"32K bytes"); break; |
| 91 | case 5: sprintf(asBuff,"64K bytes"); break; |
| 92 | case 7: sprintf(asBuff,"128K bytes"); break; |
| 93 | case 9: sprintf(asBuff,"256K bytes"); break; |
| 94 | case 10: sprintf(asBuff,"512K bytes"); break; |
| 95 | case 12: sprintf(asBuff,"1024K bytes"); break; |
| 96 | case 14: sprintf(asBuff,"2048K bytes"); break; |
| 97 | } |
| 98 | PrintAndLog("Second Nonvolatile Program Memory Size: %s",asBuff); |
| 99 | switch( (iChipID & 0xF0000) >> 16) { |
| 100 | case 1: sprintf(asBuff,"1K bytes"); break; |
| 101 | case 2: sprintf(asBuff,"2K bytes"); break; |
| 102 | case 3: sprintf(asBuff,"6K bytes"); break; |
| 103 | case 4: sprintf(asBuff,"112K bytes"); break; |
| 104 | case 5: sprintf(asBuff,"4K bytes"); break; |
| 105 | case 6: sprintf(asBuff,"80K bytes"); break; |
| 106 | case 7: sprintf(asBuff,"160K bytes"); break; |
| 107 | case 8: sprintf(asBuff,"8K bytes"); break; |
| 108 | case 9: sprintf(asBuff,"16K bytes"); break; |
| 109 | case 10: sprintf(asBuff,"32K bytes"); break; |
| 110 | case 11: sprintf(asBuff,"64K bytes"); break; |
| 111 | case 12: sprintf(asBuff,"128K bytes"); break; |
| 112 | case 13: sprintf(asBuff,"256K bytes"); break; |
| 113 | case 14: sprintf(asBuff,"96K bytes"); break; |
| 114 | case 15: sprintf(asBuff,"512K bytes");break; |
| 115 | } |
| 116 | PrintAndLog("Internal SRAM Size: %s",asBuff); |
| 117 | switch( (iChipID & 0xFF00000) >> 20) { |
| 118 | case 0x19: sprintf(asBuff,"AT91SAM9xx Series"); break; |
| 119 | case 0x29: sprintf(asBuff,"AT91SAM9XExx Series"); break; |
| 120 | case 0x34: sprintf(asBuff,"AT91x34 Series"); break; |
| 121 | case 0x37: sprintf(asBuff,"CAP7 Series"); break; |
| 122 | case 0x39: sprintf(asBuff,"CAP9 Series"); break; |
| 123 | case 0x3B: sprintf(asBuff,"CAP11 Series"); break; |
| 124 | case 0x40: sprintf(asBuff,"AT91x40 Series"); break; |
| 125 | case 0x42: sprintf(asBuff,"AT91x42 Series"); break; |
| 126 | case 0x55: sprintf(asBuff,"AT91x55 Series"); break; |
| 127 | case 0x60: sprintf(asBuff,"AT91SAM7Axx Series"); break; |
| 128 | case 0x61: sprintf(asBuff,"AT91SAM7AQxx Series"); break; |
| 129 | case 0x63: sprintf(asBuff,"AT91x63 Series"); break; |
| 130 | case 0x70: sprintf(asBuff,"AT91SAM7Sxx Series"); break; |
| 131 | case 0x71: sprintf(asBuff,"AT91SAM7XCxx Series"); break; |
| 132 | case 0x72: sprintf(asBuff,"AT91SAM7SExx Series"); break; |
| 133 | case 0x73: sprintf(asBuff,"AT91SAM7Lxx Series"); break; |
| 134 | case 0x75: sprintf(asBuff,"AT91SAM7Xxx Series"); break; |
| 135 | case 0x92: sprintf(asBuff,"AT91x92 Series"); break; |
| 136 | case 0xF0: sprintf(asBuff,"AT75Cxx Series"); break; |
| 137 | } |
| 138 | PrintAndLog("Architecture Identifier: %s",asBuff); |
| 139 | switch( (iChipID & 0x70000000) >> 28 ) { |
| 140 | case 0: sprintf(asBuff,"ROM"); break; |
| 141 | case 1: sprintf(asBuff,"ROMless or on-chip Flash"); break; |
| 142 | case 2: sprintf(asBuff,"Embedded Flash Memory"); break; |
| 143 | case 3: sprintf(asBuff,"ROM and Embedded Flash Memory\nNVPSIZ is ROM size\nNVPSIZ2 is Flash size"); break; |
| 144 | case 4: sprintf(asBuff,"SRAM emulating ROM"); break; |
| 145 | } |
| 146 | PrintAndLog("Nonvolatile Program Memory Type: %s",asBuff); |
| 147 | } |
| 148 | |
| 149 | int CmdDetectReader(const char *Cmd) |
| 150 | { |
| 151 | UsbCommand c = {CMD_LISTEN_READER_FIELD}; |
| 152 | // 'l' means LF - 125/134 kHz |
| 153 | if(*Cmd == 'l') { |
| 154 | c.arg[0] = 1; |
| 155 | } else if (*Cmd == 'h') { |
| 156 | c.arg[0] = 2; |
| 157 | } else if (*Cmd != '\0') { |
| 158 | PrintAndLog("use 'detectreader' or 'detectreader l' or 'detectreader h'"); |
| 159 | return 0; |
| 160 | } |
| 161 | clearCommandBuffer(); |
| 162 | SendCommand(&c); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | // ## FPGA Control |
| 167 | int CmdFPGAOff(const char *Cmd) |
| 168 | { |
| 169 | UsbCommand c = {CMD_FPGA_MAJOR_MODE_OFF}; |
| 170 | clearCommandBuffer(); |
| 171 | SendCommand(&c); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | int CmdLCD(const char *Cmd) |
| 176 | { |
| 177 | int i, j; |
| 178 | |
| 179 | UsbCommand c = {CMD_LCD}; |
| 180 | sscanf(Cmd, "%x %d", &i, &j); |
| 181 | while (j--) { |
| 182 | c.arg[0] = i & 0x1ff; |
| 183 | clearCommandBuffer(); |
| 184 | SendCommand(&c); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | int CmdLCDReset(const char *Cmd) |
| 190 | { |
| 191 | UsbCommand c = {CMD_LCD_RESET, {strtol(Cmd, NULL, 0), 0, 0}}; |
| 192 | clearCommandBuffer(); |
| 193 | SendCommand(&c); |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int CmdReadmem(const char *Cmd) |
| 198 | { |
| 199 | UsbCommand c = {CMD_READ_MEM, {strtol(Cmd, NULL, 0), 0, 0}}; |
| 200 | clearCommandBuffer(); |
| 201 | SendCommand(&c); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | int CmdReset(const char *Cmd) |
| 206 | { |
| 207 | UsbCommand c = {CMD_HARDWARE_RESET}; |
| 208 | clearCommandBuffer(); |
| 209 | SendCommand(&c); |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Sets the divisor for LF frequency clock: lets the user choose any LF frequency below |
| 215 | * 600kHz. |
| 216 | */ |
| 217 | int CmdSetDivisor(const char *Cmd) |
| 218 | { |
| 219 | UsbCommand c = {CMD_SET_LF_DIVISOR, {strtol(Cmd, NULL, 0), 0, 0}}; |
| 220 | |
| 221 | if (c.arg[0] < 19 || c.arg[0] > 255) { |
| 222 | PrintAndLog("divisor must be between 19 and 255"); |
| 223 | return 1; |
| 224 | } |
| 225 | // 12 000 000 (12Mhz) |
| 226 | // |
| 227 | clearCommandBuffer(); |
| 228 | SendCommand(&c); |
| 229 | PrintAndLog("Divisor set, expected %.1f KHz", ((double)12000 / (c.arg[0]+1)) ); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | int CmdSetMux(const char *Cmd) |
| 234 | { |
| 235 | UsbCommand c = {CMD_SET_ADC_MUX}; |
| 236 | if (strcmp(Cmd, "lopkd") == 0) c.arg[0] = 0; |
| 237 | else if (strcmp(Cmd, "loraw") == 0) c.arg[0] = 1; |
| 238 | else if (strcmp(Cmd, "hipkd") == 0) c.arg[0] = 2; |
| 239 | else if (strcmp(Cmd, "hiraw") == 0) c.arg[0] = 3; |
| 240 | clearCommandBuffer(); |
| 241 | SendCommand(&c); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int CmdTune(const char *Cmd) |
| 246 | { |
| 247 | return CmdTuneSamples(Cmd); |
| 248 | } |
| 249 | |
| 250 | int CmdVersion(const char *Cmd) |
| 251 | { |
| 252 | clearCommandBuffer(); |
| 253 | UsbCommand c = {CMD_VERSION}; |
| 254 | static UsbCommand resp = {0, {0, 0, 0}}; |
| 255 | |
| 256 | if (resp.arg[0] == 0 && resp.arg[1] == 0) { // no cached information available |
| 257 | SendCommand(&c); |
| 258 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) { |
| 259 | PrintAndLog("Proxmark3 RFID instrument"); |
| 260 | PrintAndLog((char*)resp.d.asBytes); |
| 261 | lookupChipID(resp.arg[0], resp.arg[1]); |
| 262 | } |
| 263 | } else { |
| 264 | PrintAndLog("[[[ Cached information ]]]\n"); |
| 265 | PrintAndLog("Proxmark3 RFID instrument"); |
| 266 | PrintAndLog((char*)resp.d.asBytes); |
| 267 | lookupChipID(resp.arg[0], resp.arg[1]); |
| 268 | PrintAndLog(""); |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int CmdStatus(const char *Cmd) |
| 274 | { |
| 275 | uint8_t speed_test_buffer[USB_CMD_DATA_SIZE]; |
| 276 | sample_buf = speed_test_buffer; |
| 277 | clearCommandBuffer(); |
| 278 | UsbCommand c = {CMD_STATUS}; |
| 279 | SendCommand(&c); |
| 280 | if (!WaitForResponseTimeout(CMD_ACK, &c, 1900)) |
| 281 | PrintAndLog("Status command failed. USB Speed Test timed out"); |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | int CmdPing(const char *Cmd) |
| 286 | { |
| 287 | clearCommandBuffer(); |
| 288 | UsbCommand resp; |
| 289 | UsbCommand c = {CMD_PING}; |
| 290 | SendCommand(&c); |
| 291 | if (WaitForResponseTimeout(CMD_ACK, &resp, 1000)) |
| 292 | PrintAndLog("Ping successful"); |
| 293 | else |
| 294 | PrintAndLog("Ping failed"); |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static command_t CommandTable[] = |
| 299 | { |
| 300 | {"help", CmdHelp, 1, "This help"}, |
| 301 | {"detectreader", CmdDetectReader,0, "['l'|'h'] -- Detect external reader field (option 'l' or 'h' to limit to LF or HF)"}, |
| 302 | {"fpgaoff", CmdFPGAOff, 0, "Set FPGA off"}, |
| 303 | {"lcd", CmdLCD, 0, "<HEX command> <count> -- Send command/data to LCD"}, |
| 304 | {"lcdreset", CmdLCDReset, 0, "Hardware reset LCD"}, |
| 305 | {"readmem", CmdReadmem, 0, "[address] -- Read memory at decimal address from flash"}, |
| 306 | {"reset", CmdReset, 0, "Reset the Proxmark3"}, |
| 307 | {"setlfdivisor", CmdSetDivisor, 0, "<19 - 255> -- Drive LF antenna at 12Mhz/(divisor+1)"}, |
| 308 | {"setmux", CmdSetMux, 0, "<loraw|hiraw|lopkd|hipkd> -- Set the ADC mux to a specific value"}, |
| 309 | {"tune", CmdTune, 0, "Measure antenna tuning"}, |
| 310 | {"version", CmdVersion, 0, "Show version information about the connected Proxmark"}, |
| 311 | {"status", CmdStatus, 0, "Show runtime status information about the connected Proxmark"}, |
| 312 | {"ping", CmdPing, 0, "Test if the pm3 is responsive"}, |
| 313 | {NULL, NULL, 0, NULL} |
| 314 | }; |
| 315 | |
| 316 | int CmdHW(const char *Cmd) { |
| 317 | clearCommandBuffer(); |
| 318 | CmdsParse(CommandTable, Cmd); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | int CmdHelp(const char *Cmd) |
| 323 | { |
| 324 | CmdsHelp(CommandTable); |
| 325 | return 0; |
| 326 | } |