From 1e80e82baca52a6adcae5b094dc083316cf1d9b5 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Tue, 21 Mar 2017 16:06:22 +0100 Subject: [PATCH] bootloader: change stage2 to loader --- micropython/bootloader/main.c | 28 ++++++++++++++-------------- micropython/loader/main.c | 6 ++++++ micropython/trezorhal/common.c | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index 77b9a07f..7842caec 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -8,7 +8,7 @@ #include "display.h" #include "sdcard.h" -#define STAGE2_START 0x08010000 +#define LOADER_START 0x08010000 #define BOOTLOADER_FGCOLOR 0xFFFF #define BOOTLOADER_BGCOLOR 0x0000 @@ -78,7 +78,7 @@ bool copy_sdcard(void) BOOTLOADER_PRINT("erasing flash "); - // erase flash (except stage 1) + // erase flash (except bootloader) HAL_FLASH_Unlock(); FLASH_EraseInitTypeDef EraseInitStruct; __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | @@ -98,11 +98,11 @@ bool copy_sdcard(void) } BOOTLOADER_PRINTLN(" done"); - BOOTLOADER_PRINTLN("copying new stage 2 from SD card"); + BOOTLOADER_PRINTLN("copying new loader from SD card"); sdcard_power_on(); - // copy stage 2 from SD card to Flash + // copy loader from SD card to Flash uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)]; sdcard_read_blocks((uint8_t *)buf, 0, 1); @@ -115,7 +115,7 @@ bool copy_sdcard(void) for (int i = 0; i < codelen / 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) { + if (HAL_FLASH_Program(TYPEPROGRAM_WORD, LOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) { BOOTLOADER_PRINTLN("copy failed"); sdcard_power_off(); HAL_FLASH_Lock(); @@ -138,7 +138,7 @@ int main(void) BOOTLOADER_PRINTLN("TREZOR Bootloader"); BOOTLOADER_PRINTLN("================="); - BOOTLOADER_PRINTLN("starting stage 1"); + BOOTLOADER_PRINTLN("starting bootloader"); if (check_sdcard()) { if (!copy_sdcard()) { @@ -146,19 +146,19 @@ int main(void) } } - BOOTLOADER_PRINTLN("checking stage 2"); - if (parse_header((const uint8_t *)STAGE2_START, NULL, NULL, NULL)) { - BOOTLOADER_PRINTLN("valid stage 2 header"); - if (check_signature((const uint8_t *)STAGE2_START)) { - BOOTLOADER_PRINTLN("valid stage 2 signature"); + BOOTLOADER_PRINTLN("checking loader"); + if (parse_header((const uint8_t *)LOADER_START, NULL, NULL, NULL)) { + BOOTLOADER_PRINTLN("valid loader header"); + if (check_signature((const uint8_t *)LOADER_START)) { + BOOTLOADER_PRINTLN("valid loader signature"); BOOTLOADER_PRINTLN("JUMP!"); - // TODO: jump to second stage + // TODO: jump to loader __fatal_error("halt"); } else { - BOOTLOADER_PRINTLN("invalid stage 2 signature"); + BOOTLOADER_PRINTLN("invalid loader signature"); } } else { - BOOTLOADER_PRINTLN("invalid stage 2 header"); + BOOTLOADER_PRINTLN("invalid loader header"); } __fatal_error("halt"); diff --git a/micropython/loader/main.c b/micropython/loader/main.c index f0103bf5..f515ffcc 100644 --- a/micropython/loader/main.c +++ b/micropython/loader/main.c @@ -5,6 +5,12 @@ #define FIRMWARE_START 0x08020000 +#define LOADER_FGCOLOR 0xFFFF +#define LOADER_BGCOLOR 0x0000 + +#define LOADER_PRINT(X) do { display_print(X, -1); display_print_out(LOADER_FGCOLOR, LOADER_BGCOLOR); } while(0) +#define LOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(LOADER_FGCOLOR, LOADER_BGCOLOR); } while(0) + void pendsv_isr_handler(void) { __fatal_error("pendsv"); } diff --git a/micropython/trezorhal/common.c b/micropython/trezorhal/common.c index 3263e966..e25114e7 100644 --- a/micropython/trezorhal/common.c +++ b/micropython/trezorhal/common.c @@ -3,7 +3,7 @@ #include "display.h" #define FATAL_FGCOLOR 0xFFFF -#define FATAL_BGCOLOR 0x001F +#define FATAL_BGCOLOR 0x7800 void __attribute__((noreturn)) __fatal_error(const char *msg) { for (volatile uint32_t delay = 0; delay < 10000000; delay++) {