+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