]> cvs.zerfleddert.de Git - proxmark3-svn/commitdiff
Updated windows client to handle new flashing method
authord18c7db <d18c7db@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Wed, 2 Sep 2009 08:30:22 +0000 (08:30 +0000)
committerd18c7db <d18c7db@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Wed, 2 Sep 2009 08:30:22 +0000 (08:30 +0000)
winsrc/prox.cpp

index bbc9adbda78cf66beb5a0a3319b324b43b66152d..4b80e3cc98895785987ff8065b07bcfa698f9ebc 100644 (file)
@@ -12,6 +12,7 @@ extern "C" {
 \r
 #define OUR_VID 0x9ac4\r
 #define OUR_PID 0x4b8f\r
 \r
 #define OUR_VID 0x9ac4\r
 #define OUR_PID 0x4b8f\r
+#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)\r
 \r
 int offline = 0;\r
 HANDLE UsbHandle;\r
 \r
 int offline = 0;\r
 HANDLE UsbHandle;\r
@@ -224,12 +225,30 @@ void SendCommand(UsbCommand *c, BOOL wantAck)
 static DWORD ExpectedAddr;\r
 static BYTE QueuedToSend[256];\r
 static BOOL AllWritten;\r
 static DWORD ExpectedAddr;\r
 static BYTE QueuedToSend[256];\r
 static BOOL AllWritten;\r
-\r
-static void FlushPrevious(void)\r
+#define PHYSICAL_FLASH_START 0x100000\r
+\r
+struct partition {\r
+       int start;\r
+       int end;\r
+       int precious;\r
+       const char *name;\r
+};\r
+struct partition partitions[] = {\r
+               {0x100000, 0x102000, 1, "bootrom"},\r
+               {0x102000, 0x110000, 0, "fpga"},\r
+               {0x110000, 0x140000, 0, "os"},\r
+};\r
+\r
+/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old\r
+ * bootroms.\r
+ */\r
+static void FlushPrevious(int translate)\r
 {\r
        UsbCommand c;\r
        memset(&c, 0, sizeof(c));\r
 \r
 {\r
        UsbCommand c;\r
        memset(&c, 0, sizeof(c));\r
 \r
+//     printf("expected = %08x flush, ", ExpectedAddr);\r
+\r
        int i;\r
        for(i = 0; i < 240; i += 48) {\r
                c.cmd = CMD_SETUP_WRITE;\r
        int i;\r
        for(i = 0; i < 240; i += 48) {\r
                c.cmd = CMD_SETUP_WRITE;\r
@@ -240,6 +259,9 @@ static void FlushPrevious(void)
 \r
        c.cmd = CMD_FINISH_WRITE;\r
        c.ext1 = (ExpectedAddr-1) & (~255);\r
 \r
        c.cmd = CMD_FINISH_WRITE;\r
        c.ext1 = (ExpectedAddr-1) & (~255);\r
+       if(translate) {\r
+               c.ext1 -= PHYSICAL_FLASH_START;\r
+       }\r
        printf("Flashing address: %08x\r", c.ext1);\r
        memcpy(c.d.asBytes, QueuedToSend+240, 16);\r
        SendCommand(&c, TRUE);\r
        printf("Flashing address: %08x\r", c.ext1);\r
        memcpy(c.d.asBytes, QueuedToSend+240, 16);\r
        SendCommand(&c, TRUE);\r
@@ -247,10 +269,17 @@ static void FlushPrevious(void)
        AllWritten = TRUE;\r
 }\r
 \r
        AllWritten = TRUE;\r
 }\r
 \r
-static void GotByte(DWORD where, BYTE which)\r
+/* Where must be between start_addr (inclusive) and end_addr (exclusive).\r
+ */\r
+static void GotByte(int where, BYTE which, int start_addr, int end_addr, int translate)\r
 {\r
        AllWritten = FALSE;\r
 \r
 {\r
        AllWritten = FALSE;\r
 \r
+       if(where < start_addr || where >= end_addr) {\r
+               printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr);\r
+               exit(-1);\r
+       }\r
+\r
        if(where != ExpectedAddr) {\r
                printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);\r
                exit(-1);\r
        if(where != ExpectedAddr) {\r
                printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);\r
                exit(-1);\r
@@ -260,7 +289,7 @@ static void GotByte(DWORD where, BYTE which)
 \r
        if((where & 255) == 255) {\r
                // we have completed a full page\r
 \r
        if((where & 255) == 255) {\r
                // we have completed a full page\r
-               FlushPrevious();\r
+               FlushPrevious(translate);\r
        }\r
 }\r
 \r
        }\r
 }\r
 \r
@@ -282,9 +311,9 @@ static BYTE HexByte(char *s)
        return (HexVal(s[0]) << 4) | HexVal(s[1]);\r
 }\r
 \r
        return (HexVal(s[0]) << 4) | HexVal(s[1]);\r
 }\r
 \r
-static void LoadFlashFromSRecords(char *file, int addr)\r
+static void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)\r
 {\r
 {\r
-       ExpectedAddr = addr;\r
+       ExpectedAddr = start_addr;\r
 \r
        FILE *f = fopen(file, "r");\r
        if(!f) {\r
 \r
        FILE *f = fopen(file, "r");\r
        if(!f) {\r
@@ -306,36 +335,177 @@ static void LoadFlashFromSRecords(char *file, int addr)
                        sscanf(addrStr, "%x", &addr);\r
                        s += 8;\r
 \r
                        sscanf(addrStr, "%x", &addr);\r
                        s += 8;\r
 \r
+                       /* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */\r
+                       if(addr < PHYSICAL_FLASH_START)\r
+                               addr += PHYSICAL_FLASH_START;\r
+\r
                        int i;\r
                        for(i = 0; i < len; i++) {\r
                                while((addr+i) > ExpectedAddr) {\r
                        int i;\r
                        for(i = 0; i < len; i++) {\r
                                while((addr+i) > ExpectedAddr) {\r
-                                       GotByte(ExpectedAddr, 0xff);\r
+                                       GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);\r
                                }\r
                                }\r
-                               GotByte(addr+i, HexByte(s));\r
+                               GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);\r
                                s += 2;\r
                        }\r
                }\r
        }\r
 \r
                                s += 2;\r
                        }\r
                }\r
        }\r
 \r
-       if(!AllWritten) FlushPrevious();\r
+       if(!AllWritten) FlushPrevious(translate);\r
 \r
        fclose(f);\r
        printf("\ndone.\n");\r
 }\r
 \r
 \r
        fclose(f);\r
        printf("\ndone.\n");\r
 }\r
 \r
+static int PrepareFlash(struct partition *p, const char *filename, unsigned int state)\r
+{\r
+       int translate = 0;\r
+       if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {\r
+               UsbCommand c;\r
+               c.cmd = CMD_START_FLASH;\r
+               c.ext1 = p->start;\r
+               c.ext2 = p->end;\r
+\r
+               /* Only send magic when flashing bootrom */\r
+               if(p->precious) {\r
+                       c.ext3 = START_FLASH_MAGIC;\r
+               } else {\r
+                       c.ext3 = 0;\r
+               }\r
+               SendCommand(&c, TRUE);\r
+               translate = 0;\r
+       } else {\r
+               fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");\r
+               fprintf(stderr, "         It is recommended that you update your bootloader\n\n");\r
+               translate = 1;\r
+       }\r
+\r
+       LoadFlashFromSRecords(filename, p->start, p->end, translate);\r
+       return 1;\r
+}\r
+\r
+static unsigned int GetProxmarkState(void)\r
+{\r
+       unsigned int state = 0;\r
+\r
+       UsbCommand c;\r
+       c.cmd = CMD_DEVICE_INFO;\r
+       SendCommand(&c, FALSE);\r
+\r
+       UsbCommand resp;\r
+       ReceiveCommand(&resp);\r
+       /* Three cases:\r
+        * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK\r
+        * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"\r
+        * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags\r
+        */\r
+\r
+       switch(resp.cmd) {\r
+       case CMD_ACK:\r
+               state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;\r
+               break;\r
+       case CMD_DEBUG_PRINT_STRING:\r
+               state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;\r
+               break;\r
+       case CMD_DEVICE_INFO:\r
+               state = resp.ext1;\r
+               break;\r
+       default:\r
+               fprintf(stderr, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp.cmd);\r
+               exit(-1);\r
+               break;\r
+       }\r
+\r
+#if 0\r
+       if(state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) printf("New bootrom present\n");\r
+       if(state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) printf("New osimage present\n");\r
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) printf("Currently in bootrom\n");\r
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) printf("Currently in OS\n");\r
+#endif\r
+\r
+       return state;\r
+}\r
+\r
+static unsigned int EnterFlashState(void)\r
+{\r
+       unsigned int state = GetProxmarkState();\r
+\r
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {\r
+               /* Already in flash state, we're done. */\r
+               return state;\r
+       }\r
+\r
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {\r
+               fprintf(stderr,"Entering flash-mode...\n");\r
+               UsbCommand c;\r
+               bzero(&c, sizeof(c));\r
+\r
+               if( (state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) ) {\r
+                       /* New style handover: Send CMD_START_FLASH, which will reset the board and\r
+                        * enter the bootrom on the next boot.\r
+                        */\r
+                       c.cmd = CMD_START_FLASH;\r
+                       SendCommand(&c, FALSE);\r
+                       fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");\r
+                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");\r
+               } else {\r
+                       /* Old style handover: Ask the user to press the button, then reset the board */\r
+                       c.cmd = CMD_HARDWARE_RESET;\r
+                       SendCommand(&c, FALSE);\r
+                       fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");\r
+                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");\r
+               }\r
+\r
+\r
+               Sleep(1000);\r
+\r
+               while(!UsbConnect()) { Sleep(1000); }\r
+               fprintf(stderr,"Found.\n");\r
+\r
+               return GetProxmarkState();\r
+       }\r
+\r
+       return 0;\r
+}\r
+\r
+static void usage(char **argv)\r
+{\r
+       int i;\r
+               printf("Usage: %s gui\n", argv[0]);\r
+               printf("       %s offline\n", argv[0]);\r
+               printf("       %s areas file.s19\n", argv[0]);\r
+               printf("               Known areas are:");\r
+               for(i=0; i<(sizeof(partitions)/sizeof(partitions[0])); i++) {\r
+                       fprintf(stderr, " %s", partitions[i].name);\r
+               }\r
+\r
+               printf("\n");\r
+}\r
+\r
+/* On first call, have *offset = -1, *length = 0; */\r
+static int find_next_area(char *str, int *offset, int *length)\r
+{\r
+       if(*str == '\0') return 0;\r
+       if((*offset >= 0) && str[*offset + *length] == '\0') return 0;\r
+       *offset += 1 + *length;\r
+\r
+       char *next_comma = strchr(str + *offset, ',');\r
+       if(next_comma == NULL) {\r
+               *length = strlen(str) - *offset;\r
+       } else {\r
+               *length = next_comma-(str+*offset);\r
+       }\r
+       return 1;\r
+}\r
+\r
 int main(int argc, char **argv)\r
 {\r
        int i = 0;\r
 int main(int argc, char **argv)\r
 {\r
        int i = 0;\r
-       \r
+\r
        if(argc < 2) {\r
        if(argc < 2) {\r
-               printf("Usage: %s bootrom file.s19\n", argv[0]);\r
-               printf("       %s load osimage.s19\n", argv[0]);\r
-               printf("       %s fpga fpgaimg.s19\n", argv[0]);\r
-               printf("       %s gui\n", argv[0]);\r
-               printf("       %s offline\n", argv[0]);\r
-               return -1;\r
+               usage(argv);\r
+               exit(-1);\r
        }\r
        }\r
-       \r
+\r
        // Only do this if NOT in offline mode\r
        if (strcmp(argv[1], "offline"))\r
        {\r
        // Only do this if NOT in offline mode\r
        if (strcmp(argv[1], "offline"))\r
        {\r
@@ -354,33 +524,56 @@ int main(int argc, char **argv)
                        Sleep(5);\r
                }\r
        }\r
                        Sleep(5);\r
                }\r
        }\r
-       \r
-       if(strcmp(argv[1], "bootrom")==0 || strcmp(argv[1], "load")==0 || strcmp(argv[1], "fpga")==0) {\r
-               if(argc != 3) {\r
-                       printf("Need filename.\n");\r
-                       return -1;\r
-               }\r
-               if(strcmp(argv[1], "bootrom")==0) {\r
-                       LoadFlashFromSRecords(argv[2], 0);\r
-               } else if(strcmp(argv[1], "fpga")==0) {\r
-                       LoadFlashFromSRecords(argv[2], 0x2000);\r
-               } else {\r
-                       LoadFlashFromSRecords(argv[2], 0x10000);\r
-               }\r
-       } else if(strcmp(argv[1], "gui")==0) {\r
+\r
+       if(strcmp(argv[1], "gui")==0) {\r
                ShowGui();\r
        } else if(strcmp(argv[1], "offline")==0) {\r
                offline = 1;\r
                ShowGui();\r
                ShowGui();\r
        } else if(strcmp(argv[1], "offline")==0) {\r
                offline = 1;\r
                ShowGui();\r
-       } else if(strcmp(argv[1], "cmd")==0) {\r
-               if(argc != 3) {\r
-                       printf("Need command.\n");\r
-                       return -1;\r
+       }\r
+\r
+       /* Count area arguments */\r
+       int areas = 0, offset=-1, length=0;\r
+       while(find_next_area(argv[1], &offset, &length)) areas++;\r
+\r
+       if(areas != argc - 2) {\r
+               usage(argv);\r
+               exit(-1);\r
+       }\r
+\r
+       unsigned int state = EnterFlashState();\r
+\r
+       if( !(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) ) {\r
+               fprintf(stderr, "Proxmark would not enter flash state, abort\n");\r
+               exit(-1);\r
+       }\r
+\r
+       offset=-1; length=0;\r
+       int current_area = 0;\r
+       while(find_next_area(argv[1], &offset, &length)) {\r
+               int i;\r
+               struct partition *p = NULL;\r
+               for(i=0; i<sizeof(partitions)/sizeof(partitions[0]); i++) {\r
+                       if(strncmp(partitions[i].name, argv[1] + offset, length) == 0) {\r
+                               /* Check if the name matches the bootrom partition, and if so, require "bootrom" to\r
+                                * be written in full. The other names may be abbreviated.\r
+                                */\r
+                               if(!partitions[i].precious || (strlen(partitions[i].name) == length)) {\r
+                                       p = &partitions[i];\r
+                               }\r
+                               break;\r
+                       }\r
                }\r
                }\r
-               ExecCmd(argv[2]);\r
-       } else {\r
-               printf("Command '%s' not recognized.\n", argv[1]);\r
-               return -1;\r
+\r
+               if(p == NULL) {\r
+                       fprintf(stderr, "Warning: area name '");\r
+                       fwrite(argv[1]+offset, length, 1, stderr);\r
+                       fprintf(stderr, "' unknown, ignored\n");\r
+               } else {\r
+                       fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]);\r
+                       PrepareFlash(p, argv[2+current_area], state);\r
+               }\r
+               current_area++;\r
        }\r
 \r
        return 0;\r
        }\r
 \r
        return 0;\r
Impressum, Datenschutz