From d8efa3209121eca6240303a8b9b96172e9e8a826 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 17 Feb 2017 15:49:43 +0100 Subject: [PATCH] bootloader: more wip --- micropython/stmhal/Makefile.bootloader | 1 - micropython/stmhal/bootloader/basic.c | 9 +- micropython/stmhal/bootloader/bootloader.c | 120 +++++++++++++-------- micropython/stmhal/bootloader/crypto.c | 4 +- micropython/stmhal/bootloader/ui.c | 17 --- micropython/stmhal/bootloader/ui.h | 11 -- tools/firmwarectl | 2 +- 7 files changed, 84 insertions(+), 80 deletions(-) delete mode 100644 micropython/stmhal/bootloader/ui.c delete mode 100644 micropython/stmhal/bootloader/ui.h diff --git a/micropython/stmhal/Makefile.bootloader b/micropython/stmhal/Makefile.bootloader index aab134781a..bf950de933 100644 --- a/micropython/stmhal/Makefile.bootloader +++ b/micropython/stmhal/Makefile.bootloader @@ -81,7 +81,6 @@ SRC_C = \ bootloader/bootloader.c \ bootloader/crypto.c \ bootloader/sdcard.c \ - bootloader/ui.c \ system_stm32.c \ $(wildcard boards/$(BOARD)/*.c) diff --git a/micropython/stmhal/bootloader/basic.c b/micropython/stmhal/bootloader/basic.c index 81625ffad7..f58ffa131c 100644 --- a/micropython/stmhal/bootloader/basic.c +++ b/micropython/stmhal/bootloader/basic.c @@ -1,20 +1,19 @@ #include STM32_HAL_H +#include "display.h" // ### from main.c void __attribute__((noreturn)) __fatal_error(const char *msg) { for (volatile uint32_t delay = 0; delay < 10000000; delay++) { } - // TODO: printf("FATAL ERROR: %s\n", msg); + display_print("FATAL ERROR:\n", -1); + display_print(msg, -1); + display_print("\n", -1); for (;;) { __WFI(); } } -void nlr_jump_fail(void *val) { - __fatal_error("FATAL: uncaught exception"); -} - // ### from stm32_it.c void SysTick_Handler(void) { diff --git a/micropython/stmhal/bootloader/bootloader.c b/micropython/stmhal/bootloader/bootloader.c index c197a91e6d..15c92b8721 100644 --- a/micropython/stmhal/bootloader/bootloader.c +++ b/micropython/stmhal/bootloader/bootloader.c @@ -3,7 +3,6 @@ #include #include "crypto.h" -#include "ui.h" #include "display.h" #include "sdcard.h" @@ -11,6 +10,9 @@ #define STAGE2_START 0x08010000 #define STAGE2_SIZE (64 * 1024) +#define BOOTLOADER_PRINT(X) display_print(X, -1) +#define BOOTLOADER_PRINTLN(X) display_print(X "\n", -1) + void SystemClock_Config(void); void periph_init(void) @@ -31,26 +33,45 @@ void periph_init(void) display_backlight(255); } -void check_sdcard() +bool check_sdcard(void) { - if (!sdcard_is_present()) return; + BOOTLOADER_PRINTLN("checking for SD card"); + + if (!sdcard_is_present()) { + BOOTLOADER_PRINTLN("no SD card found"); + return false; + } + + BOOTLOADER_PRINTLN("SD card found"); sdcard_power_on(); uint64_t cap = sdcard_get_capacity_in_bytes(); - if (cap < SDCARD_BLOCK_SIZE + STAGE2_SIZE) { + if (cap < STAGE2_SIZE) { + BOOTLOADER_PRINTLN("SD card too small"); sdcard_power_off(); - return; + return false; } uint8_t buf[SDCARD_BLOCK_SIZE] __attribute__((aligned(4))); sdcard_read_blocks(buf, 0, 1); - if (!check_header(buf)) { - sdcard_power_off(); - return; + sdcard_power_off(); + + if (check_header(buf)) { + BOOTLOADER_PRINTLN("SD card header is valid"); + return true; + } else { + BOOTLOADER_PRINTLN("SD card header is invalid"); + return false; } +} + +void copy_sdcard(void) +{ + + BOOTLOADER_PRINTLN("erasing old stage 2"); // erase STAGE2_SECTOR HAL_FLASH_Unlock(); @@ -64,55 +85,70 @@ void check_sdcard() uint32_t SectorError = 0; if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) { HAL_FLASH_Lock(); - sdcard_power_off(); return; } - // copy stage 2 from SD card to Flash - uint32_t src; - int block = 0; - int offset = 256; + BOOTLOADER_PRINTLN("copying new stage 2 from SD card"); - for (int i = 0; i < STAGE2_SIZE / 4; i++) { - memcpy(&src, buf + offset, 4); - if (HAL_FLASH_Program(TYPEPROGRAM_WORD, STAGE2_START + i * 4, src) != HAL_OK) { - break; - } - offset += 4; - if (offset == SDCARD_BLOCK_SIZE) { - offset = 0; - block++; - sdcard_read_blocks(buf, block, 1); + // copy stage 2 from SD card to Flash + uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)]; + + sdcard_power_on(); + for (int i = 0; i < STAGE2_SIZE / SDCARD_BLOCK_SIZE; i++) { + sdcard_read_blocks((uint8_t *)buf, i, 1); + for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) { + if (HAL_FLASH_Program(TYPEPROGRAM_WORD, STAGE2_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) { + break; + } } } - HAL_FLASH_Lock(); sdcard_power_off(); + + HAL_FLASH_Lock(); + + BOOTLOADER_PRINTLN("done"); } - -int main(void) { - - periph_init(); - - screen_stage1(); - - check_sdcard(); - - check_signature(); - - if (check_header((const uint8_t *)STAGE2_START)) { - screen_stage2_jump(); - // TODO: jump to second stage - } - - screen_stage2_invalid(); - +void halt(void) +{ for (;;) { display_backlight(255); HAL_Delay(950); display_backlight(0); HAL_Delay(50); } +} + +int main(void) +{ + + periph_init(); + + BOOTLOADER_PRINTLN("TREZOR Bootloader"); + BOOTLOADER_PRINTLN("================="); + BOOTLOADER_PRINTLN("starting stage 1"); + + if (check_sdcard()) { + copy_sdcard(); + } + + BOOTLOADER_PRINTLN("checking stage 2"); + if (check_header((const uint8_t *)STAGE2_START)) { + BOOTLOADER_PRINTLN("valid stage 2 header"); + if (check_signature()) { + BOOTLOADER_PRINTLN("valid stage 2 signature"); + BOOTLOADER_PRINTLN("JUMP!"); + // TODO: jump to second stage + halt(); + } else { + BOOTLOADER_PRINTLN("invalid stage 2 signature"); + } + } else { + BOOTLOADER_PRINTLN("invalid stage 2 header"); + } + + BOOTLOADER_PRINTLN("HALT!"); + halt(); return 0; } diff --git a/micropython/stmhal/bootloader/crypto.c b/micropython/stmhal/bootloader/crypto.c index b8e054a52a..0414e83ecd 100644 --- a/micropython/stmhal/bootloader/crypto.c +++ b/micropython/stmhal/bootloader/crypto.c @@ -33,7 +33,7 @@ bool check_header(const uint8_t *data) uint32_t codelen; memcpy(&codelen, data + 12, 4); - if (codelen != 64 * 1024) return false; + if (codelen != 64 * 1024 - 256) return false; uint32_t version; memcpy(&version, data + 16, 4); @@ -46,8 +46,6 @@ bool check_header(const uint8_t *data) uint8_t sig[64]; memcpy(sig, data + 0x00C0, 64); - // TODO: check signature - return true; } diff --git a/micropython/stmhal/bootloader/ui.c b/micropython/stmhal/bootloader/ui.c deleted file mode 100644 index c8cb4c9c52..0000000000 --- a/micropython/stmhal/bootloader/ui.c +++ /dev/null @@ -1,17 +0,0 @@ -#include "ui.h" -#include "display.h" - -void screen_stage1(void) -{ - display_print("BL stage 1\n", -1); -} - -void screen_stage2_jump(void) -{ - display_print("BL stage 2 jump\n", -1); -} - -void screen_stage2_invalid(void) -{ - display_print("BL stage 2 invalid\n", -1); -} diff --git a/micropython/stmhal/bootloader/ui.h b/micropython/stmhal/bootloader/ui.h deleted file mode 100644 index a95c784ce3..0000000000 --- a/micropython/stmhal/bootloader/ui.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef __BOOTLOADER_UI_H__ -#define __BOOTLOADER_UI_H__ - -#define ui_WHITE 0xFFFF -#define ui_BLACK 0x0000 - -void screen_stage1(void); -void screen_stage2_jump(void); -void screen_stage2_invalid(void); - -#endif diff --git a/tools/firmwarectl b/tools/firmwarectl index 3b1ce0fbfa..d8bfff1b14 100755 --- a/tools/firmwarectl +++ b/tools/firmwarectl @@ -42,7 +42,7 @@ class BootloaderImage: self.reserved, self.sigidx, self.sig = header assert self.magic == b'TRZB' assert self.hdrlen == 256 - assert self.codelen == 64*1024 + assert self.codelen == 64*1024 - 256 assert self.reserved == 171 * b'\x00' self.sigidx = bitmap_to_tuple(self.sigidx) self.code = data[256:]