Updated windows client to handle new flashing method

This commit is contained in:
d18c7db 2009-09-02 08:30:22 +00:00
parent 523f4c90a1
commit 3cc63bdf91

View file

@ -12,6 +12,7 @@ extern "C" {
#define OUR_VID 0x9ac4 #define OUR_VID 0x9ac4
#define OUR_PID 0x4b8f #define OUR_PID 0x4b8f
#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
int offline = 0; int offline = 0;
HANDLE UsbHandle; HANDLE UsbHandle;
@ -224,12 +225,30 @@ void SendCommand(UsbCommand *c, BOOL wantAck)
static DWORD ExpectedAddr; static DWORD ExpectedAddr;
static BYTE QueuedToSend[256]; static BYTE QueuedToSend[256];
static BOOL AllWritten; static BOOL AllWritten;
#define PHYSICAL_FLASH_START 0x100000
static void FlushPrevious(void) 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; UsbCommand c;
memset(&c, 0, sizeof(c)); memset(&c, 0, sizeof(c));
// printf("expected = %08x flush, ", ExpectedAddr);
int i; int i;
for(i = 0; i < 240; i += 48) { for(i = 0; i < 240; i += 48) {
c.cmd = CMD_SETUP_WRITE; c.cmd = CMD_SETUP_WRITE;
@ -240,6 +259,9 @@ static void FlushPrevious(void)
c.cmd = CMD_FINISH_WRITE; c.cmd = CMD_FINISH_WRITE;
c.ext1 = (ExpectedAddr-1) & (~255); c.ext1 = (ExpectedAddr-1) & (~255);
if(translate) {
c.ext1 -= PHYSICAL_FLASH_START;
}
printf("Flashing address: %08x\r", c.ext1); printf("Flashing address: %08x\r", c.ext1);
memcpy(c.d.asBytes, QueuedToSend+240, 16); memcpy(c.d.asBytes, QueuedToSend+240, 16);
SendCommand(&c, TRUE); SendCommand(&c, TRUE);
@ -247,10 +269,17 @@ static void FlushPrevious(void)
AllWritten = TRUE; AllWritten = TRUE;
} }
static void GotByte(DWORD where, BYTE which) /* Where must be between start_addr (inclusive) and end_addr (exclusive).
*/
static void GotByte(int where, BYTE which, int start_addr, int end_addr, int translate)
{ {
AllWritten = FALSE; 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) { if(where != ExpectedAddr) {
printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr); printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
exit(-1); exit(-1);
@ -260,7 +289,7 @@ static void GotByte(DWORD where, BYTE which)
if((where & 255) == 255) { if((where & 255) == 255) {
// we have completed a full page // we have completed a full page
FlushPrevious(); FlushPrevious(translate);
} }
} }
@ -282,9 +311,9 @@ static BYTE HexByte(char *s)
return (HexVal(s[0]) << 4) | HexVal(s[1]); return (HexVal(s[0]) << 4) | HexVal(s[1]);
} }
static void LoadFlashFromSRecords(char *file, int addr) static void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)
{ {
ExpectedAddr = addr; ExpectedAddr = start_addr;
FILE *f = fopen(file, "r"); FILE *f = fopen(file, "r");
if(!f) { if(!f) {
@ -306,34 +335,175 @@ static void LoadFlashFromSRecords(char *file, int addr)
sscanf(addrStr, "%x", &addr); sscanf(addrStr, "%x", &addr);
s += 8; 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; int i;
for(i = 0; i < len; i++) { for(i = 0; i < len; i++) {
while((addr+i) > ExpectedAddr) { while((addr+i) > ExpectedAddr) {
GotByte(ExpectedAddr, 0xff); GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);
} }
GotByte(addr+i, HexByte(s)); GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);
s += 2; s += 2;
} }
} }
} }
if(!AllWritten) FlushPrevious(); if(!AllWritten) FlushPrevious(translate);
fclose(f); fclose(f);
printf("\ndone.\n"); 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.ext1 = p->start;
c.ext2 = p->end;
/* Only send magic when flashing bootrom */
if(p->precious) {
c.ext3 = START_FLASH_MAGIC;
} else {
c.ext3 = 0;
}
SendCommand(&c, TRUE);
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, FALSE);
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.ext1;
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, FALSE);
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, FALSE);
fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
}
Sleep(1000);
while(!UsbConnect()) { Sleep(1000); }
fprintf(stderr,"Found.\n");
return GetProxmarkState();
}
return 0;
}
static void usage(char **argv)
{
int i;
printf("Usage: %s gui\n", argv[0]);
printf(" %s offline\n", argv[0]);
printf(" %s areas file.s19\n", argv[0]);
printf(" Known areas are:");
for(i=0; i<(sizeof(partitions)/sizeof(partitions[0])); i++) {
fprintf(stderr, " %s", partitions[i].name);
}
printf("\n");
}
/* On first call, have *offset = -1, *length = 0; */
static int find_next_area(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) int main(int argc, char **argv)
{ {
int i = 0; int i = 0;
if(argc < 2) { if(argc < 2) {
printf("Usage: %s bootrom file.s19\n", argv[0]); usage(argv);
printf(" %s load osimage.s19\n", argv[0]); exit(-1);
printf(" %s fpga fpgaimg.s19\n", argv[0]);
printf(" %s gui\n", argv[0]);
printf(" %s offline\n", argv[0]);
return -1;
} }
// Only do this if NOT in offline mode // Only do this if NOT in offline mode
@ -355,32 +525,55 @@ int main(int argc, char **argv)
} }
} }
if(strcmp(argv[1], "bootrom")==0 || strcmp(argv[1], "load")==0 || strcmp(argv[1], "fpga")==0) { if(strcmp(argv[1], "gui")==0) {
if(argc != 3) {
printf("Need filename.\n");
return -1;
}
if(strcmp(argv[1], "bootrom")==0) {
LoadFlashFromSRecords(argv[2], 0);
} else if(strcmp(argv[1], "fpga")==0) {
LoadFlashFromSRecords(argv[2], 0x2000);
} else {
LoadFlashFromSRecords(argv[2], 0x10000);
}
} else if(strcmp(argv[1], "gui")==0) {
ShowGui(); ShowGui();
} else if(strcmp(argv[1], "offline")==0) { } else if(strcmp(argv[1], "offline")==0) {
offline = 1; offline = 1;
ShowGui(); ShowGui();
} else if(strcmp(argv[1], "cmd")==0) {
if(argc != 3) {
printf("Need command.\n");
return -1;
} }
ExecCmd(argv[2]);
/* Count area arguments */
int areas = 0, offset=-1, length=0;
while(find_next_area(argv[1], &offset, &length)) areas++;
if(areas != argc - 2) {
usage(argv);
exit(-1);
}
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 { } else {
printf("Command '%s' not recognized.\n", argv[1]); fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]);
return -1; PrepareFlash(p, argv[2+current_area], state);
}
current_area++;
} }
return 0; return 0;