Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add verification of flash writes #1

Merged
merged 4 commits into from
Sep 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.yagarto
version.txt
bootrom*
*.elf
*.exe
41 changes: 40 additions & 1 deletion bootrom/bootrom.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ static void Fatal(void)
for(;;);
}

static unsigned int crc32(volatile void* memory, unsigned int length)
{
unsigned int crc = 0xffffffff;
unsigned char* data = (unsigned char*)memory;
unsigned int i;
int j;

for (i = 0; i < length; ++i) {
unsigned int byte = *data++;
crc = crc ^ byte;

for (j = 7; j >= 0; j--) { // Do eight times.
unsigned int mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
}

return ~crc;
}

void UsbPacketReceived(BYTE *packet, int len)
{
int i;
Expand All @@ -74,13 +94,19 @@ void UsbPacketReceived(BYTE *packet, int len)

switch(c->cmd) {
case CMD_DEVICE_INFO:
c->ext1 = CMD_VERSION;
// copy size of the bootloader (if tag matches)
c->ext2 = (*(DWORD*)0x100208 == 0xb007c0de) ? *(DWORD*)0x10020c : 0;
// copy size of the arm firmware (if recent enough)
c->ext3 = (*(DWORD*)0x102020 == 0x600dc0de) ? *(DWORD*)0x102024 : 0;
break;

case CMD_SETUP_WRITE:
p = (volatile DWORD *)0;
for(i = 0; i < 12; i++) {
p[i+c->ext1] = c->d.asDwords[i];
}
c->ext1 = crc32(c->d.asDwords, 12 * sizeof(c->d.asDwords[0]));
break;

case CMD_FINISH_WRITE:
Expand All @@ -94,11 +120,22 @@ void UsbPacketReceived(BYTE *packet, int len)
FCMD_WRITE_PAGE;
while(!(MC_FLASH_STATUS & MC_FLASH_STATUS_READY))
;

c->ext1 = crc32(c->d.asDwords, 4 * sizeof(c->d.asDwords[0]));
break;

case CMD_HARDWARE_RESET:
break;

case CMD_CRC32_MEMORY:
{
void *p = (void*)c->ext1;
unsigned int len = c->ext2;
unsigned int crc = crc32(p,len);
c->ext1 = crc;
break;
}

default:
Fatal();
break;
Expand Down Expand Up @@ -135,9 +172,11 @@ void Bootrom(void)
// stack setup?
USB_D_PLUS_PULLUP_OFF();

int always_connect_usb = 0x1 & *(DWORD*)0x200010;

for(i = 0; i < 10000; i++) LED_OFF(); // delay a bit, before testing the key

if (PIO_PIN_DATA_STATUS&(1<<GPIO_KEY)) goto run_flash;
if (!always_connect_usb && PIO_PIN_DATA_STATUS&(1<<GPIO_KEY)) goto run_flash;

// disable watchdog
WDT_MODE = WDT_MODE_DISABLE;
Expand Down
3 changes: 3 additions & 0 deletions bootrom/flash-reset.s
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ start:
b 0x00102030
b 0x00102038

length:
.word __rodata_end__

Reset:
ldr sp, = 0x00203ff8
bl CMain
Expand Down
1 change: 1 addition & 0 deletions bootrom/ldscript-flash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ SECTIONS
. = 0x00000000;
.text : { obj/flash-reset.o(.text) *(.text) }
.rodata : { *(.rodata) }
__rodata_end__ = .;
. = 0x00200000;
.data : { *(.data) }
__bss_start__ = .;
Expand Down
6 changes: 6 additions & 0 deletions bootrom/ram-reset.s
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@
start:
ldr sp, = 0x00203ff8
bl Bootrom

.word 0xB007C0DE
.word __bss_start__-0x00200000
.word 0x00000000
.word 0xfafffaff

3 changes: 3 additions & 0 deletions include/usb_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ typedef struct {
} d;
} UsbCommand;

#define CMD_VERSION 0x00010002

// For the bootloader
#define CMD_DEVICE_INFO 0x0000
#define CMD_SETUP_WRITE 0x0001
#define CMD_FINISH_WRITE 0x0003
#define CMD_HARDWARE_RESET 0x0004
#define CMD_CRC32_MEMORY 0x0005
#define CMD_ACK 0x00ff

#endif
Loading