lean up event model so that this will work under OS X (and hopefully Linux)

still need to refactor some stuff -- lots of code duplication here that
we can get rid of
This commit is contained in:
bushing 2009-12-22 12:46:04 +00:00
commit 9b25560889
5 changed files with 395 additions and 447 deletions

File diff suppressed because it is too large Load diff

View file

@ -16,6 +16,16 @@ static uint32_t ExpectedAddr;
static uint8_t QueuedToSend[256]; static uint8_t QueuedToSend[256];
static bool AllWritten; static bool AllWritten;
#define PHYSICAL_FLASH_START 0x100000 #define PHYSICAL_FLASH_START 0x100000
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 { struct partition {
int start; int start;
@ -44,7 +54,8 @@ static void FlushPrevious(int translate)
c.cmd = CMD_SETUP_WRITE; c.cmd = CMD_SETUP_WRITE;
memcpy(c.d.asBytes, QueuedToSend+i, 48); memcpy(c.d.asBytes, QueuedToSend+i, 48);
c.arg[0] = (i/4); c.arg[0] = (i/4);
SendCommand(&c, true); SendCommand(&c);
WaitForAck();
} }
c.cmd = CMD_FINISH_WRITE; c.cmd = CMD_FINISH_WRITE;
@ -54,7 +65,8 @@ static void FlushPrevious(int translate)
} }
printf("c.arg[0] = %08x\r", c.arg[0]); printf("c.arg[0] = %08x\r", c.arg[0]);
memcpy(c.d.asBytes, QueuedToSend+240, 16); memcpy(c.d.asBytes, QueuedToSend+240, 16);
SendCommand(&c, true); SendCommand(&c);
WaitForAck();
AllWritten = true; AllWritten = true;
} }
@ -161,7 +173,8 @@ static int PrepareFlash(struct partition *p, const char *filename, unsigned int
} else { } else {
c.arg[2] = 0; c.arg[2] = 0;
} }
SendCommand(&c, true); SendCommand(&c);
WaitForAck();
translate = 0; translate = 0;
} else { } else {
fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n"); fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
@ -179,7 +192,7 @@ static unsigned int GetProxmarkState(void)
UsbCommand c; UsbCommand c;
c.cmd = CMD_DEVICE_INFO; c.cmd = CMD_DEVICE_INFO;
SendCommand(&c, false); SendCommand(&c);
UsbCommand resp; UsbCommand resp;
ReceiveCommand(&resp); ReceiveCommand(&resp);
@ -234,13 +247,13 @@ static unsigned int EnterFlashState(void)
* enter the bootrom on the next boot. * enter the bootrom on the next boot.
*/ */
c.cmd = CMD_START_FLASH; c.cmd = CMD_START_FLASH;
SendCommand(&c, false); 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,"(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... "); fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
} else { } else {
/* Old style handover: Ask the user to press the button, then reset the board */ /* Old style handover: Ask the user to press the button, then reset the board */
c.cmd = CMD_HARDWARE_RESET; c.cmd = CMD_HARDWARE_RESET;
SendCommand(&c, false); SendCommand(&c);
fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n"); fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
fprintf(stderr,"Waiting for Proxmark to reappear on USB... "); fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
} }
@ -346,7 +359,7 @@ int main(int argc, char **argv) {
UsbCommand c; UsbCommand c;
bzero(&c, sizeof(c)); bzero(&c, sizeof(c));
c.cmd = CMD_HARDWARE_RESET; c.cmd = CMD_HARDWARE_RESET;
SendCommand(&c, false); SendCommand(&c);
CloseProxmark(); CloseProxmark();

View file

@ -17,6 +17,7 @@
int offline = 0; int offline = 0;
HANDLE UsbHandle; HANDLE UsbHandle;
extern unsigned int current_command;
static void ShowError(void) static void ShowError(void)
{ {
@ -188,7 +189,7 @@ void ReceiveCommand(UsbCommand *c)
} }
} }
void SendCommand(UsbCommand *c, bool wantAck) void SendCommand(UsbCommand *c)
{ {
BYTE buf[65]; BYTE buf[65];
buf[0] = 0; buf[0] = 0;
@ -212,14 +213,15 @@ void SendCommand(UsbCommand *c, bool wantAck)
ShowError(); ShowError();
exit(-1); exit(-1);
} }
current_command = c->cmd;
}
if(wantAck) { void WaitForAck(void) {
UsbCommand ack; UsbCommand ack;
ReceiveCommand(&ack); ReceiveCommand(&ack);
if(ack.cmd != CMD_ACK) { if(ack.cmd != CMD_ACK) {
printf("bad ACK\n"); printf("bad ACK\n");
exit(-1); exit(-1);
}
} }
} }
@ -255,7 +257,8 @@ static void FlushPrevious(int translate)
c.cmd = CMD_SETUP_WRITE; c.cmd = CMD_SETUP_WRITE;
memcpy(c.d.asBytes, QueuedToSend+i, 48); memcpy(c.d.asBytes, QueuedToSend+i, 48);
c.arg[0] = (i/4); c.arg[0] = (i/4);
SendCommand(&c, TRUE); SendCommand(&c);
WaitForAck();
} }
c.cmd = CMD_FINISH_WRITE; c.cmd = CMD_FINISH_WRITE;
@ -265,7 +268,8 @@ static void FlushPrevious(int translate)
} }
printf("Flashing address: %08x\r", c.arg[0]); printf("Flashing address: %08x\r", c.arg[0]);
memcpy(c.d.asBytes, QueuedToSend+240, 16); memcpy(c.d.asBytes, QueuedToSend+240, 16);
SendCommand(&c, TRUE); SendCommand(&c);
WaitForAck();
AllWritten = TRUE; AllWritten = TRUE;
} }
@ -372,7 +376,8 @@ static int PrepareFlash(struct partition *p, const char *filename, unsigned int
} else { } else {
c.arg[2] = 0; c.arg[2] = 0;
} }
SendCommand(&c, TRUE); SendCommand(&c);
WaitForAck();
translate = 0; translate = 0;
} else { } else {
fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n"); fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
@ -390,7 +395,7 @@ static unsigned int GetProxmarkState(void)
UsbCommand c; UsbCommand c;
c.cmd = CMD_DEVICE_INFO; c.cmd = CMD_DEVICE_INFO;
SendCommand(&c, FALSE); SendCommand(&c);
UsbCommand resp; UsbCommand resp;
ReceiveCommand(&resp); ReceiveCommand(&resp);
@ -445,13 +450,13 @@ static unsigned int EnterFlashState(void)
* enter the bootrom on the next boot. * enter the bootrom on the next boot.
*/ */
c.cmd = CMD_START_FLASH; c.cmd = CMD_START_FLASH;
SendCommand(&c, FALSE); 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,"(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... "); fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
} else { } else {
/* Old style handover: Ask the user to press the button, then reset the board */ /* Old style handover: Ask the user to press the button, then reset the board */
c.cmd = CMD_HARDWARE_RESET; c.cmd = CMD_HARDWARE_RESET;
SendCommand(&c, FALSE); SendCommand(&c);
fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n"); fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
fprintf(stderr,"Waiting for Proxmark to reappear on USB... "); fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
} }

View file

@ -14,7 +14,8 @@ typedef WORD uint16_t;
// prox.cpp // prox.cpp
void ReceiveCommand(UsbCommand *c); void ReceiveCommand(UsbCommand *c);
bool ReceiveCommandPoll(UsbCommand *c); bool ReceiveCommandPoll(UsbCommand *c);
void SendCommand(UsbCommand *c, bool); void SendCommand(UsbCommand *c);
void WaitForAck(void);
void wait_for_response(uint32_t command_type); void wait_for_response(uint32_t command_type);
// gui.cpp // gui.cpp

View file

@ -1,6 +1,5 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <unistd.h> #include <unistd.h>
#include <usb.h> #include <usb.h>
@ -9,18 +8,20 @@
#include "prox.h" #include "prox.h"
#include "proxmark3.h" #include "proxmark3.h"
#include "../include/usb_cmd.h"
usb_dev_handle *devh = NULL; usb_dev_handle *devh = NULL;
static unsigned int claimed_iface = 0; static unsigned int claimed_iface = 0;
unsigned char return_on_error = 0; unsigned char return_on_error = 0;
unsigned char error_occured = 0; unsigned char error_occured = 0;
extern unsigned int current_command;
void SendCommand(UsbCommand *c, bool wantAck) { void SendCommand(UsbCommand *c) {
int ret; int ret;
#if 0 #if 0
printf("Sending %d bytes\n", sizeof(UsbCommand)); printf("Sending %d bytes\n", sizeof(UsbCommand));
#endif #endif
current_command = c->cmd;
ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000); ret = usb_bulk_write(devh, 0x01, (char*)c, sizeof(UsbCommand), 1000);
if (ret<0) { if (ret<0) {
error_occured = 1; error_occured = 1;
@ -40,15 +41,19 @@ void SendCommand(UsbCommand *c, bool wantAck) {
return; return;
} }
/*
if(wantAck) { if(wantAck) {
UsbCommand ack; UsbCommand ack;
printf("waiting for ack\n");
ReceiveCommand(&ack); ReceiveCommand(&ack);
if(ack.cmd != CMD_ACK) { if(ack.cmd != CMD_ACK) {
printf("bad ACK\n"); printf("bad ACK\n");
exit(-1); exit(-1);
} }
} else {
printf("not waiting for ack\n");
} }
*/
} }
bool ReceiveCommandPoll(UsbCommand *c) { bool ReceiveCommandPoll(UsbCommand *c) {
@ -60,7 +65,7 @@ bool ReceiveCommandPoll(UsbCommand *c) {
if (ret != -ETIMEDOUT) { if (ret != -ETIMEDOUT) {
error_occured = 1; error_occured = 1;
if (return_on_error) if (return_on_error)
return 0; return false;
fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n", fprintf(stderr, "read failed: %s(%d)!\nTrying to reopen device...\n",
usb_strerror(), ret); usb_strerror(), ret);
@ -73,7 +78,7 @@ bool ReceiveCommandPoll(UsbCommand *c) {
printf(PROXPROMPT); printf(PROXPROMPT);
fflush(NULL); fflush(NULL);
return 0; return false;
} }
} else { } else {
if (ret && (ret < sizeof(UsbCommand))) { if (ret && (ret < sizeof(UsbCommand))) {
@ -96,11 +101,17 @@ bool ReceiveCommandPoll(UsbCommand *c) {
#endif #endif
} }
return ret > 0; return ret != 0;
} }
void ReceiveCommand(UsbCommand *c) { void ReceiveCommand(UsbCommand *c) {
while(!ReceiveCommandPoll(c)) {} // printf("%s()\n", __FUNCTION__);
int retval = 0;
do {
retval = ReceiveCommandPoll(c);
if (retval != 1) printf("ReceiveCommandPoll returned %d\n", retval);
} while(retval<0);
// printf("recv %x\n", c->cmd);
} }
usb_dev_handle* findProxmark(int verbose, unsigned int *iface) { usb_dev_handle* findProxmark(int verbose, unsigned int *iface) {