]> git.zerfleddert.de Git - proxmark3-svn/commitdiff
refactor flashing code into flash.h, so we can share it between platforms
authorbushing <bushing@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Mon, 4 Jan 2010 05:11:08 +0000 (05:11 +0000)
committerbushing <bushing@ef4ab9da-24cd-11de-8aaa-f3a34680c41f>
Mon, 4 Jan 2010 05:11:08 +0000 (05:11 +0000)
client/Makefile
client/flash.c [new file with mode: 0644]
client/flash.h [new file with mode: 0644]
client/flasher.c
client/prox.c

index b8707a5ed398fc797ba775c4e95515a52b85b0f8..8bbc0273cd35103cd347be8090d36b4be7d2a6b2 100644 (file)
@@ -4,7 +4,7 @@ WINCC=c:\mingw\bin\gcc
 
 LDLIBS = -L/opt/local/lib -L/usr/local/lib -lusb -lreadline -lpthread
 LDFLAGS = $(COMMON_FLAGS)
 
 LDLIBS = -L/opt/local/lib -L/usr/local/lib -lusb -lreadline -lpthread
 LDFLAGS = $(COMMON_FLAGS)
-CFLAGS = -I. -I/opt/local/include -Wall -Wno-unused-function $(COMMON_FLAGS)
+CFLAGS = -I. -I/opt/local/include -Wall -Wno-unused-function $(COMMON_FLAGS) -g3
 
 WINLIBS = -lgdi32 -lsetupapi
 CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall
 
 WINLIBS = -lgdi32 -lsetupapi
 CXXFLAGS = $(shell pkg-config --cflags QtCore QtGui 2>/dev/null) -Wall
@@ -41,8 +41,8 @@ all: $(BINS)
 all-static: LDLIBS:=-static $(LDLIBS)
 all-static: snooper cli flasher
 
 all-static: LDLIBS:=-static $(LDLIBS)
 all-static: snooper cli flasher
 
-prox.exe: prox.c wingui.c command.c
-       $(WINCC) $(CFLAGS) $(DEFINES) -o prox.exe prox.c wingui.c command.c $(WINLIBS)
+prox.exe: prox.c wingui.c command.c flash.c
+       $(WINCC) $(CFLAGS) $(DEFINES) -o prox.exe prox.c wingui.c command.c flash.c $(WINLIBS)
        
 proxmark3: LDLIBS+=$(QTLDLIBS)
 proxmark3: proxmark3.o gui.o command.o usb.o $(QTGUI)
        
 proxmark3: LDLIBS+=$(QTLDLIBS)
 proxmark3: proxmark3.o gui.o command.o usb.o $(QTGUI)
@@ -53,7 +53,7 @@ snooper: snooper.o gui.o command.o usb.o guidummy.o
 
 cli: cli.o gui.o command.o usb.o guidummy.o
 
 
 cli: cli.o gui.o command.o usb.o guidummy.o
 
-flasher: flasher.o usb.o
+flasher: flash.o flasher.o usb.o
 
 proxguiqt.moc.cpp: proxguiqt.h
        $(MOC) -o$@ $^
 
 proxguiqt.moc.cpp: proxguiqt.h
        $(MOC) -o$@ $^
diff --git a/client/flash.c b/client/flash.c
new file mode 100644 (file)
index 0000000..af322d3
--- /dev/null
@@ -0,0 +1,326 @@
+#ifdef WIN32
+#include <windows.h>
+#include <setupapi.h>
+#include <ctype.h>
+#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
+BOOL UsbConnect(void);
+#else
+#include <usb.h>
+#endif
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <strings.h>
+#include <string.h>
+#include <errno.h>
+#include <ctype.h>
+
+#include "prox.h"
+#ifndef WIN32
+#include "proxmark3.h"
+#endif
+#include "flash.h"
+
+static uint32_t ExpectedAddr;
+static uint8_t QueuedToSend[256];
+static bool AllWritten;
+#define PHYSICAL_FLASH_START 0x100000
+
+void WaitForAck(void) {
+       UsbCommand ack;
+       ReceiveCommand(&ack);
+       if(ack.cmd != CMD_ACK) {
+               printf("bad ACK\n");
+               exit(-1);
+       }
+}
+
+struct partition partitions[] = {
+               {0x100000, 0x102000, 1, "bootrom"},
+               {0x102000, 0x110000, 0, "fpga"},
+               {0x110000, 0x140000, 0, "os"},
+               {0, 0, 0, NULL},
+};
+
+/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
+ * bootroms.
+ */
+void FlushPrevious(int translate)
+{
+       UsbCommand c;
+       memset(&c, 0, sizeof(c));
+
+       printf("expected = %08x flush, ", ExpectedAddr);
+
+       int i;
+       for(i = 0; i < 240; i += 48) {
+               c.cmd = CMD_SETUP_WRITE;
+               memcpy(c.d.asBytes, QueuedToSend+i, 48);
+               c.arg[0] = (i/4);
+               SendCommand(&c);
+               WaitForAck();
+       }
+
+       c.cmd = CMD_FINISH_WRITE;
+       c.arg[0] = (ExpectedAddr-1) & (~255);
+       if(translate) {
+               c.arg[0] -= PHYSICAL_FLASH_START;
+       }
+       printf("c.arg[0] = %08x\r", c.arg[0]);
+       memcpy(c.d.asBytes, QueuedToSend+240, 16);
+       SendCommand(&c);
+       WaitForAck();
+
+       AllWritten = true;
+}
+
+/* Where must be between start_addr (inclusive) and end_addr (exclusive).
+ */
+void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate)
+{
+       AllWritten = false;
+       
+       if(where < start_addr || where >= end_addr) {
+               printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr);
+               exit(-1);
+       }
+
+       if(where != ExpectedAddr) {
+               printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
+               exit(-1);
+       }
+       QueuedToSend[where & 255] = which;
+       ExpectedAddr++;
+
+       if((where & 255) == 255) {
+               // we have completed a full page
+               FlushPrevious(translate);
+       }
+}
+
+int HexVal(int c)
+{
+       c = tolower(c);
+       if(c >= '0' && c <= '9') {
+               return c - '0';
+       } else if(c >= 'a' && c <= 'f') {
+               return (c - 'a') + 10;
+       } else {
+               printf("bad hex digit '%c'\n", c);
+               exit(-1);
+       }
+}
+
+uint8_t HexByte(char *s)
+{
+       return (HexVal(s[0]) << 4) | HexVal(s[1]);
+}
+
+void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)
+{
+       ExpectedAddr = start_addr;
+       
+       FILE *f = fopen(file, "r");
+       if(!f) {
+               printf("couldn't open file\n");
+               exit(-1);
+       }
+
+       char line[512];
+       while(fgets(line, sizeof(line), f)) {
+               if(memcmp(line, "S3", 2)==0) {
+                       char *s = line + 2;
+                       int len = HexByte(s) - 5;
+                       s += 2;
+
+                       char addrStr[9];
+                       memcpy(addrStr, s, 8);
+                       addrStr[8] = '\0';
+                       uint32_t addr;
+                       sscanf(addrStr, "%x", &addr);
+                       s += 8;
+                       
+                       /* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */
+                       if(addr < PHYSICAL_FLASH_START) 
+                               addr += PHYSICAL_FLASH_START;
+
+                       int i;
+                       for(i = 0; i < len; i++) {
+                               while((addr+i) > ExpectedAddr) {
+                                       GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);
+                               }
+                               GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);
+                               s += 2;
+                       }
+               }
+       }
+
+       if(!AllWritten) FlushPrevious(translate);
+
+       fclose(f);
+       printf("\ndone.\n");
+}
+
+int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
+{
+       int translate = 0;
+       if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
+               UsbCommand c;
+               c.cmd = CMD_START_FLASH;
+               c.arg[0] = p->start;
+               c.arg[1] = p->end;
+               
+               /* Only send magic when flashing bootrom */
+               if(p->precious) {
+                       c.arg[2] = START_FLASH_MAGIC;
+               } else {
+                       c.arg[2] = 0;
+               }
+               SendCommand(&c);
+               WaitForAck();
+               translate = 0;
+       } else {
+               fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
+               fprintf(stderr, "         It is recommended that you update your bootloader\n\n");
+               translate = 1;
+       }
+       
+       LoadFlashFromSRecords(filename, p->start, p->end, translate);
+       return 1;
+}
+
+unsigned int GetProxmarkState(void)
+{
+       unsigned int state = 0;
+       
+       UsbCommand c;
+       c.cmd = CMD_DEVICE_INFO;
+       SendCommand(&c);
+       
+       UsbCommand resp;
+       ReceiveCommand(&resp);
+       /* Three cases: 
+        * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
+        * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
+        * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
+        */
+       
+       switch(resp.cmd) {
+       case CMD_ACK:
+               state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;
+               break;
+       case CMD_DEBUG_PRINT_STRING:
+               state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;
+               break;
+       case CMD_DEVICE_INFO:
+               state = resp.arg[0];
+               break;
+       default:
+               fprintf(stderr, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp.cmd);
+               exit(-1);
+               break;
+       }
+       
+       return state;
+}
+
+unsigned int EnterFlashState(void)
+{
+       unsigned int state = GetProxmarkState();
+       
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {
+               /* Already in flash state, we're done. */
+               return state;
+       }
+       
+       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
+               fprintf(stderr,"Entering flash-mode...\n");
+               UsbCommand c;
+               bzero(&c, sizeof(c));
+               
+               if( (state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) ) {
+                       /* New style handover: Send CMD_START_FLASH, which will reset the board and
+                        * enter the bootrom on the next boot.
+                        */
+                       c.cmd = CMD_START_FLASH;
+                       SendCommand(&c);
+                       fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
+                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
+               } else {
+                       /* Old style handover: Ask the user to press the button, then reset the board */
+                       c.cmd = CMD_HARDWARE_RESET;
+                       SendCommand(&c);
+                       fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
+                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
+               }
+
+#ifdef WIN32
+               Sleep(1000);
+               while(!UsbConnect()) { Sleep(1000); }
+#else
+               CloseProxmark();
+               sleep(1);
+
+               while(!(devh=OpenProxmark(0))) { sleep(1); }
+#endif
+               fprintf(stderr,"Found.\n");
+
+               return GetProxmarkState();
+       }
+       
+       return 0;
+}
+
+/* On first call, have *offset = -1, *length = 0; */
+int find_next_area(const char *str, int *offset, int *length)
+{
+       if(*str == '\0') return 0;
+       if((*offset >= 0) && str[*offset + *length] == '\0') return 0;
+       *offset += 1 + *length;
+       
+       char *next_comma = strchr(str + *offset, ',');
+       if(next_comma == NULL) {
+               *length = strlen(str) - *offset;
+       } else {
+               *length = next_comma-(str+*offset);
+       }
+       return 1;
+}
+
+void do_flash(char **argv) {
+       unsigned int state = EnterFlashState();
+
+       if (!(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)) {
+               fprintf(stderr, "Proxmark would not enter flash state, abort\n");
+               exit(-1);
+       }
+
+       int offset=-1, length=0;
+       int current_area = 0;
+       while(find_next_area(argv[1], &offset, &length)) {
+               int i;
+               struct partition *p = NULL;
+               for (i=0; i<sizeof(partitions)/sizeof(partitions[0]); i++) {
+                       if (strncmp(partitions[i].name, argv[1] + offset, length) == 0) {
+                               /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
+                               * be written in full. The other names may be abbreviated.
+                               */
+                               if(!partitions[i].precious || (strlen(partitions[i].name) == length))
+                                       p = &partitions[i];
+                               break;
+                       }
+               }
+
+               if(p == NULL) {
+                       fprintf(stderr, "Warning: area name '");
+                       fwrite(argv[1]+offset, length, 1, stderr);
+                       fprintf(stderr, "' unknown, ignored\n");
+               } else {
+                       fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]);
+                       PrepareFlash(p, argv[2+current_area], state);
+               }
+               current_area++;
+       }
+}
diff --git a/client/flash.h b/client/flash.h
new file mode 100644 (file)
index 0000000..8d18cab
--- /dev/null
@@ -0,0 +1,15 @@
+struct partition {
+       int start;
+       int end;
+       int precious;
+       const char *name;
+};
+
+void FlushPrevious(int translate);
+void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate);
+unsigned int EnterFlashState(void);
+int PrepareFlash(struct partition *p, const char *filename, unsigned int state);
+int find_next_area(const char *str, int *offset, int *length);
+
+#define PHYSICAL_FLASH_START 0x100000
+void do_flash(char **argv);
index ebcd3391880e0323750a3e584535854c5a13e5d7..3b5c1ad5fc742f6ac28c687d5c0eb39c722bd40b 100644 (file)
 
 #include "prox.h"
 #include "proxmark3.h"
 
 #include "prox.h"
 #include "proxmark3.h"
+#include "flash.h"
 
 
-static uint32_t ExpectedAddr;
-static uint8_t QueuedToSend[256];
-static bool AllWritten;
-#define PHYSICAL_FLASH_START 0x100000
 unsigned int current_command = CMD_UNKNOWN;
 
 unsigned int current_command = CMD_UNKNOWN;
 
-void WaitForAck(void) {
-       UsbCommand ack;
-       ReceiveCommand(&ack);
-       if(ack.cmd != CMD_ACK) {
-               printf("bad ACK\n");
-               exit(-1);
-       }
-}
-
-struct partition {
-       int start;
-       int end;
-       int precious;
-       const char *name;
-};
-struct partition partitions[] = {
-               {0x100000, 0x102000, 1, "bootrom"},
-               {0x102000, 0x110000, 0, "fpga"},
-               {0x110000, 0x140000, 0, "os"},
-};
-
-/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
- * bootroms.
- */
-static void FlushPrevious(int translate)
-{
-       UsbCommand c;
-       memset(&c, 0, sizeof(c));
-
-       printf("expected = %08x flush, ", ExpectedAddr);
-
-       int i;
-       for(i = 0; i < 240; i += 48) {
-               c.cmd = CMD_SETUP_WRITE;
-               memcpy(c.d.asBytes, QueuedToSend+i, 48);
-               c.arg[0] = (i/4);
-               SendCommand(&c);
-               WaitForAck();
-       }
-
-       c.cmd = CMD_FINISH_WRITE;
-       c.arg[0] = (ExpectedAddr-1) & (~255);
-       if(translate) {
-               c.arg[0] -= PHYSICAL_FLASH_START;
-       }
-       printf("c.arg[0] = %08x\r", c.arg[0]);
-       memcpy(c.d.asBytes, QueuedToSend+240, 16);
-       SendCommand(&c);
-       WaitForAck();
-
-       AllWritten = true;
-}
-
-/* Where must be between start_addr (inclusive) and end_addr (exclusive).
- */
-static void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate)
-{
-       AllWritten = false;
-       
-       if(where < start_addr || where >= end_addr) {
-               printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr);
-               exit(-1);
-       }
-
-       if(where != ExpectedAddr) {
-               printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
-               exit(-1);
-       }
-       QueuedToSend[where & 255] = which;
-       ExpectedAddr++;
-
-       if((where & 255) == 255) {
-               // we have completed a full page
-               FlushPrevious(translate);
-       }
-}
-
-static int HexVal(int c)
-{
-       c = tolower(c);
-       if(c >= '0' && c <= '9') {
-               return c - '0';
-       } else if(c >= 'a' && c <= 'f') {
-               return (c - 'a') + 10;
-       } else {
-               printf("bad hex digit '%c'\n", c);
-               exit(-1);
-       }
-}
-
-static uint8_t HexByte(char *s)
-{
-       return (HexVal(s[0]) << 4) | HexVal(s[1]);
-}
-
-static void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)
-{
-       ExpectedAddr = start_addr;
-       
-       FILE *f = fopen(file, "r");
-       if(!f) {
-               printf("couldn't open file\n");
-               exit(-1);
-       }
-
-       char line[512];
-       while(fgets(line, sizeof(line), f)) {
-               if(memcmp(line, "S3", 2)==0) {
-                       char *s = line + 2;
-                       int len = HexByte(s) - 5;
-                       s += 2;
-
-                       char addrStr[9];
-                       memcpy(addrStr, s, 8);
-                       addrStr[8] = '\0';
-                       uint32_t addr;
-                       sscanf(addrStr, "%x", &addr);
-                       s += 8;
-                       
-                       /* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */
-                       if(addr < PHYSICAL_FLASH_START) 
-                               addr += PHYSICAL_FLASH_START;
-
-                       int i;
-                       for(i = 0; i < len; i++) {
-                               while((addr+i) > ExpectedAddr) {
-                                       GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);
-                               }
-                               GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);
-                               s += 2;
-                       }
-               }
-       }
-
-       if(!AllWritten) FlushPrevious(translate);
-
-       fclose(f);
-       printf("\ndone.\n");
-}
-
-static int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
-{
-       int translate = 0;
-       if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
-               UsbCommand c;
-               c.cmd = CMD_START_FLASH;
-               c.arg[0] = p->start;
-               c.arg[1] = p->end;
-               
-               /* Only send magic when flashing bootrom */
-               if(p->precious) {
-                       c.arg[2] = START_FLASH_MAGIC;
-               } else {
-                       c.arg[2] = 0;
-               }
-               SendCommand(&c);
-               WaitForAck();
-               translate = 0;
-       } else {
-               fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
-               fprintf(stderr, "         It is recommended that you update your bootloader\n\n");
-               translate = 1;
-       }
-       
-       LoadFlashFromSRecords(filename, p->start, p->end, translate);
-       return 1;
-}
-
-static unsigned int GetProxmarkState(void)
-{
-       unsigned int state = 0;
-       
-       UsbCommand c;
-       c.cmd = CMD_DEVICE_INFO;
-       SendCommand(&c);
-       
-       UsbCommand resp;
-       ReceiveCommand(&resp);
-       /* Three cases: 
-        * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
-        * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
-        * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
-        */
-       
-       switch(resp.cmd) {
-       case CMD_ACK:
-               state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;
-               break;
-       case CMD_DEBUG_PRINT_STRING:
-               state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;
-               break;
-       case CMD_DEVICE_INFO:
-               state = resp.arg[0];
-               break;
-       default:
-               fprintf(stderr, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp.cmd);
-               exit(-1);
-               break;
-       }
-       
-#if 0
-       if(state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) printf("New bootrom present\n");
-       if(state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) printf("New osimage present\n");
-       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) printf("Currently in bootrom\n");
-       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) printf("Currently in OS\n");
-#endif
-       
-       return state;
-}
-
-static unsigned int EnterFlashState(void)
-{
-       unsigned int state = GetProxmarkState();
-       
-       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {
-               /* Already in flash state, we're done. */
-               return state;
-       }
-       
-       if(state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
-               fprintf(stderr,"Entering flash-mode...\n");
-               UsbCommand c;
-               bzero(&c, sizeof(c));
-               
-               if( (state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT) ) {
-                       /* New style handover: Send CMD_START_FLASH, which will reset the board and
-                        * enter the bootrom on the next boot.
-                        */
-                       c.cmd = CMD_START_FLASH;
-                       SendCommand(&c);
-                       fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
-                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
-               } else {
-                       /* Old style handover: Ask the user to press the button, then reset the board */
-                       c.cmd = CMD_HARDWARE_RESET;
-                       SendCommand(&c);
-                       fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
-                       fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
-               }
-               
-               CloseProxmark();
-               sleep(1);
-
-               while(!(devh=OpenProxmark(0))) { sleep(1); }
-               fprintf(stderr,"Found.\n");
-
-               return GetProxmarkState();
-       }
-       
-       return 0;
-}
+extern struct partition partitions[];
 
 static void usage(char **argv)
 {
 
 static void usage(char **argv)
 {
@@ -276,30 +23,16 @@ static void usage(char **argv)
        fprintf(stderr, "Usage:   %s areas image [image [image]]\n", argv[0]);
        fprintf(stderr, "         areas is a comma-separated list of areas to flash, with no spaces\n");
        fprintf(stderr, "               Known areas are:");
        fprintf(stderr, "Usage:   %s areas image [image [image]]\n", argv[0]);
        fprintf(stderr, "         areas is a comma-separated list of areas to flash, with no spaces\n");
        fprintf(stderr, "               Known areas are:");
-       for(i=0; i<(sizeof(partitions)/sizeof(partitions[0])); i++) {
+
+       for(i=0; partitions[i].name != NULL; i++) {
                fprintf(stderr, " %s", partitions[i].name);
        }
                fprintf(stderr, " %s", partitions[i].name);
        }
+
        fprintf(stderr, "\n");
        fprintf(stderr, "         image is the path to the corresponding image\n\n");
        fprintf(stderr, "Example: %s os,fpga path/to/osimage.s19 path/to/fpgaimage.s19\n", argv[0]);
 }
 
        fprintf(stderr, "\n");
        fprintf(stderr, "         image is the path to the corresponding image\n\n");
        fprintf(stderr, "Example: %s os,fpga path/to/osimage.s19 path/to/fpgaimage.s19\n", argv[0]);
 }
 
-/* On first call, have *offset = -1, *length = 0; */
-static int find_next_area(const char *str, int *offset, int *length)
-{
-       if(*str == '\0') return 0;
-       if((*offset >= 0) && str[*offset + *length] == '\0') return 0;
-       *offset += 1 + *length;
-       
-       char *next_comma = strchr(str + *offset, ',');
-       if(next_comma == NULL) {
-               *length = strlen(str) - *offset;
-       } else {
-               *length = next_comma-(str+*offset);
-       }
-       return 1;
-}
-
 int main(int argc, char **argv) {
        if(argc < 2) {
                usage(argv);
 int main(int argc, char **argv) {
        if(argc < 2) {
                usage(argv);
@@ -321,44 +54,9 @@ int main(int argc, char **argv) {
        while(!(devh=OpenProxmark(0))) { sleep(1); }
        fprintf(stderr,"Found.\n");
        
        while(!(devh=OpenProxmark(0))) { sleep(1); }
        fprintf(stderr,"Found.\n");
        
-       unsigned int state = EnterFlashState();
-       
-       if( !(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) ) {
-               fprintf(stderr, "Proxmark would not enter flash state, abort\n");
-               exit(-1);
-       }
-       
-       offset=-1; length=0;
-       int current_area = 0;
-       while(find_next_area(argv[1], &offset, &length)) {
-               int i;
-               struct partition *p = NULL;
-               for(i=0; i<sizeof(partitions)/sizeof(partitions[0]); i++) {
-                       if(strncmp(partitions[i].name, argv[1] + offset, length) == 0) {
-                               /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
-                                * be written in full. The other names may be abbreviated.
-                                */
-                               if(!partitions[i].precious || (strlen(partitions[i].name) == length)) {
-                                       p = &partitions[i];
-                               }
-                               break;
-                       }
-               }
-               
-               if(p == NULL) {
-                       fprintf(stderr, "Warning: area name '");
-                       fwrite(argv[1]+offset, length, 1, stderr);
-                       fprintf(stderr, "' unknown, ignored\n");
-               } else {
-                       fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]);
-                       PrepareFlash(p, argv[2+current_area], state);
-               }
-               current_area++;
-       }
+       do_flash(argv);
        
        
-       UsbCommand c;
-       bzero(&c, sizeof(c));
-       c.cmd = CMD_HARDWARE_RESET;
+       UsbCommand c = {CMD_HARDWARE_RESET};
        SendCommand(&c);
 
        CloseProxmark();
        SendCommand(&c);
 
        CloseProxmark();
index 94343ff0222841c5d95f1b4787941466bc2fcf6e..432047456e18a249acddf726dd5085b21bfce5d7 100644 (file)
@@ -10,6 +10,7 @@
 //}\r
 \r
 #include "prox.h"\r
 //}\r
 \r
 #include "prox.h"\r
+#include "flash.h"\r
 \r
 #define OUR_VID 0x9ac4\r
 #define OUR_PID 0x4b8f\r
 \r
 #define OUR_VID 0x9ac4\r
 #define OUR_PID 0x4b8f\r
@@ -18,6 +19,7 @@
 int offline = 0;\r
 HANDLE UsbHandle;\r
 extern unsigned int current_command;\r
 int offline = 0;\r
 HANDLE UsbHandle;\r
 extern unsigned int current_command;\r
+extern struct partition partitions[];\r
 \r
 static void ShowError(void)\r
 {\r
 \r
 static void ShowError(void)\r
 {\r
@@ -27,7 +29,7 @@ static void ShowError(void)
        printf("ERROR: %s", buf);\r
 }\r
 \r
        printf("ERROR: %s", buf);\r
 }\r
 \r
-static BOOL UsbConnect(void)\r
+BOOL UsbConnect(void)\r
 {\r
        typedef void (__stdcall *GetGuidProc)(GUID *);\r
        typedef BOOLEAN (__stdcall *GetAttrProc)(HANDLE, HIDD_ATTRIBUTES *);\r
 {\r
        typedef void (__stdcall *GetGuidProc)(GUID *);\r
        typedef BOOLEAN (__stdcall *GetAttrProc)(HANDLE, HIDD_ATTRIBUTES *);\r
@@ -216,263 +218,6 @@ void SendCommand(UsbCommand *c)
        current_command = c->cmd;\r
 }\r
 \r
        current_command = c->cmd;\r
 }\r
 \r
-void WaitForAck(void) {\r
-       UsbCommand ack;\r
-       ReceiveCommand(&ack);\r
-       if(ack.cmd != CMD_ACK) {\r
-               printf("bad ACK\n");\r
-               exit(-1);\r
-       }\r
-}\r
-\r
-static unsigned int ExpectedAddr;\r
-static BYTE QueuedToSend[256];\r
-static BOOL AllWritten;\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
-//     printf("expected = %08x flush, ", ExpectedAddr);\r
-\r
-       int i;\r
-       for(i = 0; i < 240; i += 48) {\r
-               c.cmd = CMD_SETUP_WRITE;\r
-               memcpy(c.d.asBytes, QueuedToSend+i, 48);\r
-               c.arg[0] = (i/4);\r
-               SendCommand(&c);\r
-               WaitForAck();\r
-       }\r
-\r
-       c.cmd = CMD_FINISH_WRITE;\r
-       c.arg[0] = (ExpectedAddr-1) & (~255);\r
-       if(translate) {\r
-               c.arg[0] -= PHYSICAL_FLASH_START;\r
-       }\r
-       printf("Flashing address: %08x\r", c.arg[0]);\r
-       memcpy(c.d.asBytes, QueuedToSend+240, 16);\r
-       SendCommand(&c);\r
-       WaitForAck();\r
-       \r
-       AllWritten = TRUE;\r
-}\r
-\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
-       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
-       }\r
-       QueuedToSend[where & 255] = which;\r
-       ExpectedAddr++;\r
-\r
-       if((where & 255) == 255) {\r
-               // we have completed a full page\r
-               FlushPrevious(translate);\r
-       }\r
-}\r
-\r
-static int HexVal(int c)\r
-{\r
-       c = tolower(c);\r
-       if(c >= '0' && c <= '9') {\r
-               return c - '0';\r
-       } else if(c >= 'a' && c <= 'f') {\r
-               return (c - 'a') + 10;\r
-       } else {\r
-               printf("bad hex digit '%c'\n", c);\r
-               exit(-1);\r
-       }\r
-}\r
-\r
-static BYTE HexByte(char *s)\r
-{\r
-       return (HexVal(s[0]) << 4) | HexVal(s[1]);\r
-}\r
-\r
-static void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)\r
-{\r
-       ExpectedAddr = start_addr;\r
-\r
-       FILE *f = fopen(file, "r");\r
-       if(!f) {\r
-               printf("couldn't open file\n");\r
-               exit(-1);\r
-       }\r
-\r
-       char line[512];\r
-       while(fgets(line, sizeof(line), f)) {\r
-               if(memcmp(line, "S3", 2)==0) {\r
-                       char *s = line + 2;\r
-                       int len = HexByte(s) - 5;\r
-                       s += 2;\r
-\r
-                       char addrStr[9];\r
-                       memcpy(addrStr, s, 8);\r
-                       addrStr[8] = '\0';\r
-                       unsigned int addr;\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
-                                       GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);\r
-                               }\r
-                               GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);\r
-                               s += 2;\r
-                       }\r
-               }\r
-       }\r
-\r
-       if(!AllWritten) FlushPrevious(translate);\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.arg[0] = p->start;\r
-               c.arg[1] = p->end;\r
-\r
-               /* Only send magic when flashing bootrom */\r
-               if(p->precious) {\r
-                       c.arg[2] = START_FLASH_MAGIC;\r
-               } else {\r
-                       c.arg[2] = 0;\r
-               }\r
-               SendCommand(&c);\r
-               WaitForAck();\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);\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.arg[0];\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);\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);\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
 static void usage(char **argv)\r
 {\r
        int i;\r
@@ -480,29 +225,13 @@ static void usage(char **argv)
                printf("       %s offline\n", argv[0]);\r
                printf("       %s areas file.s19\n", argv[0]);\r
                printf("               Known areas are:");\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
+               for(i=0; partitions[i].name != NULL; i++) {\r
                        fprintf(stderr, " %s", partitions[i].name);\r
                }\r
 \r
                printf("\n");\r
 }\r
 \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
@@ -547,40 +276,6 @@ int main(int argc, char **argv)
                exit(-1);\r
        }\r
 \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
-               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
+       do_flash(argv);\r
        return 0;\r
 }\r
        return 0;\r
 }\r
Impressum, Datenschutz