CHG: a bit code clean up,

FIX: a comparison with signed vs unsigned was solved.   I'm reluctant to change this code since it is a vital piece.
This commit is contained in:
iceman1001 2016-03-01 07:00:56 +01:00
commit cf69eca0ff

View file

@ -9,13 +9,12 @@
#include <proxmark3.h> #include <proxmark3.h>
#include "usb_cdc.h" #include "usb_cdc.h"
#include "cmd.h" #include "cmd.h"
//#include "usb_hid.h"
void DbpString(char *str) { void DbpString(char *str) {
byte_t len = 0; byte_t len = 0;
while (str[len] != 0x00) { while (str[len] != 0x00)
len++; ++len;
}
cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len); cmd_send(CMD_DEBUG_PRINT_STRING,len,0,0,(byte_t*)str,len);
} }
@ -84,8 +83,7 @@ static void ConfigClocks(void)
; ;
} }
static void Fatal(void) static void Fatal(void) {
{
for(;;); for(;;);
} }
@ -94,9 +92,7 @@ void UsbPacketReceived(uint8_t *packet, int len) {
UsbCommand* c = (UsbCommand *)packet; UsbCommand* c = (UsbCommand *)packet;
volatile uint32_t *p; volatile uint32_t *p;
if(len != sizeof(UsbCommand)) { if(len != sizeof(UsbCommand)) Fatal();
Fatal();
}
uint32_t arg0 = (uint32_t)c->arg[0]; uint32_t arg0 = (uint32_t)c->arg[0];
@ -105,9 +101,9 @@ void UsbPacketReceived(uint8_t *packet, int len) {
dont_ack = 1; dont_ack = 1;
arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM | arg0 = DEVICE_INFO_FLAG_BOOTROM_PRESENT | DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM |
DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH; DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH;
if(common_area.flags.osimage_present) { if(common_area.flags.osimage_present)
arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT; arg0 |= DEVICE_INFO_FLAG_OSIMAGE_PRESENT;
}
cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0); cmd_send(CMD_DEVICE_INFO,arg0,1,2,0,0);
} break; } break;
@ -116,14 +112,13 @@ void UsbPacketReceived(uint8_t *packet, int len) {
* whole memory region, only the last 8 bits are decoded. * whole memory region, only the last 8 bits are decoded.
*/ */
p = (volatile uint32_t *)&_flash_start; p = (volatile uint32_t *)&_flash_start;
for(i = 0; i < 12; i++) { for(i = 0; i < 12; i++)
p[i+arg0] = c->d.asDwords[i]; p[i+arg0] = c->d.asDwords[i];
}
} break; } break;
case CMD_FINISH_WRITE: { case CMD_FINISH_WRITE: {
uint32_t* flash_mem = (uint32_t*)(&_flash_start); uint32_t* flash_mem = (uint32_t*)(&_flash_start);
for (size_t j=0; j<2; j++) { for ( int j=0; j<2; j++) {
for(i = 0+(64*j); i < 64+(64*j); i++) { for(i = 0+(64*j); i < 64+(64*j); i++) {
flash_mem[i] = c->d.asDwords[i]; flash_mem[i] = c->d.asDwords[i];
} }
@ -159,9 +154,11 @@ void UsbPacketReceived(uint8_t *packet, int len) {
} break; } break;
case CMD_START_FLASH: { case CMD_START_FLASH: {
if(c->arg[2] == START_FLASH_MAGIC) bootrom_unlocked = 1; if(c->arg[2] == START_FLASH_MAGIC)
else bootrom_unlocked = 0; bootrom_unlocked = 1;
{ else
bootrom_unlocked = 0;
int prot_start = (int)&_bootrom_start; int prot_start = (int)&_bootrom_start;
int prot_end = (int)&_bootrom_end; int prot_end = (int)&_bootrom_end;
int allow_start = (int)&_flash_start; int allow_start = (int)&_flash_start;
@ -172,8 +169,9 @@ void UsbPacketReceived(uint8_t *packet, int len) {
/* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected /* Only allow command if the bootrom is unlocked, or the parameters are outside of the protected
* bootrom area. In any case they must be within the flash area. * bootrom area. In any case they must be within the flash area.
*/ */
if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start))) if( (bootrom_unlocked || ((cmd_start >= prot_end) || (cmd_end < prot_start))) &&
&& (cmd_start >= allow_start) && (cmd_end <= allow_end) ) { (cmd_start >= allow_start) &&
(cmd_end <= allow_end) ) {
start_addr = cmd_start; start_addr = cmd_start;
end_addr = cmd_end; end_addr = cmd_end;
} else { } else {
@ -181,7 +179,6 @@ void UsbPacketReceived(uint8_t *packet, int len) {
dont_ack = 1; dont_ack = 1;
cmd_send(CMD_NACK,0,0,0,0,0); cmd_send(CMD_NACK,0,0,0,0,0);
} }
}
} break; } break;
default: { default: {
@ -189,9 +186,8 @@ void UsbPacketReceived(uint8_t *packet, int len) {
} break; } break;
} }
if(!dont_ack) { if(!dont_ack)
cmd_send(CMD_ACK,arg0,0,0,0,0); cmd_send(CMD_ACK,arg0,0,0,0,0);
}
} }
static void flash_mode(int externally_entered) static void flash_mode(int externally_entered)
@ -203,16 +199,16 @@ static void flash_mode(int externally_entered)
size_t rx_len; size_t rx_len;
usb_enable(); usb_enable();
for (volatile size_t i=0; i<0x100000; i++); for (volatile size_t i=0; i<0x100000; i++)
;
for(;;) { for(;;) {
WDT_HIT(); WDT_HIT();
if (usb_poll()) { if (usb_poll()) {
rx_len = usb_read(rx,sizeof(UsbCommand)); rx_len = usb_read(rx,sizeof(UsbCommand));
if (rx_len) { if (rx_len)
UsbPacketReceived(rx,rx_len); UsbPacketReceived(rx,rx_len);
}
} }
if(!externally_entered && !BUTTON_PRESS()) { if(!externally_entered && !BUTTON_PRESS()) {
@ -220,7 +216,8 @@ static void flash_mode(int externally_entered)
usb_disable(); usb_disable();
LED_B_ON(); LED_B_ON();
AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST; AT91C_BASE_RSTC->RSTC_RCR = RST_CONTROL_KEY | AT91C_RSTC_PROCRST;
for(;;); for(;;)
;
} }
if(externally_entered && BUTTON_PRESS()) { if(externally_entered && BUTTON_PRESS()) {
/* Let the user's button press override the automatic leave */ /* Let the user's button press override the automatic leave */
@ -305,9 +302,11 @@ void BootROM(void)
if(!common_area_present){ if(!common_area_present){
/* Common area not ok, initialize it */ /* Common area not ok, initialize it */
int i; for(i=0; i<sizeof(common_area); i++) { /* Makeshift memset, no need to drag util.c into this */ int i;
/* Makeshift memset, no need to drag util.c into this */
for(i=0; i<sizeof(common_area); i++)
((char*)&common_area)[i] = 0; ((char*)&common_area)[i] = 0;
}
common_area.magic = COMMON_AREA_MAGIC; common_area.magic = COMMON_AREA_MAGIC;
common_area.version = 1; common_area.version = 1;
common_area.flags.bootrom_present = 1; common_area.flags.bootrom_present = 1;