1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-25 07:48:10 +00:00
trezor-firmware/micropython/boardloader/main.c

173 lines
4.5 KiB
C
Raw Normal View History

#include STM32_HAL_H
2017-02-11 15:47:36 +00:00
#include <string.h>
#include "common.h"
#include "display.h"
#include "image.h"
2017-03-28 11:04:54 +00:00
#include "flash.h"
2017-02-11 15:47:36 +00:00
#include "sdcard.h"
#include "version.h"
2017-04-10 17:24:21 +00:00
#define IMAGE_MAGIC 0x425A5254 // TRZB
#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
void pendsv_isr_handler(void) {
__fatal_error("pendsv", __FILE__, __LINE__, __FUNCTION__);
2017-03-18 11:02:39 +00:00
}
2017-02-17 14:49:43 +00:00
bool check_sdcard(void)
2017-02-11 15:47:36 +00:00
{
display_printf("checking for SD card\n");
2017-02-17 14:49:43 +00:00
if (!sdcard_is_present()) {
display_printf("no SD card found\n");
2017-02-17 14:49:43 +00:00
return false;
}
display_printf("SD card found\n");
2017-02-11 15:47:36 +00:00
sdcard_power_on();
uint64_t cap = sdcard_get_capacity_in_bytes();
if (cap < 1024 * 1024) {
display_printf("SD card too small\n");
2017-02-11 15:47:36 +00:00
sdcard_power_off();
2017-02-17 14:49:43 +00:00
return false;
2017-02-11 15:47:36 +00:00
}
uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
2017-02-11 15:47:36 +00:00
sdcard_read_blocks(buf, 0, 1);
2017-02-17 14:49:43 +00:00
sdcard_power_off();
if (image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, NULL)) {
display_printf("SD card header is valid\n");
2017-02-17 14:49:43 +00:00
return true;
} else {
display_printf("SD card header is invalid\n");
2017-02-17 14:49:43 +00:00
return false;
2017-02-11 15:47:36 +00:00
}
2017-02-17 14:49:43 +00:00
}
bool copy_sdcard(void)
2017-02-17 14:49:43 +00:00
{
display_printf("erasing flash ");
2017-02-11 15:47:36 +00:00
2017-04-10 17:11:44 +00:00
// erase flash (except boardloader)
2017-02-11 15:47:36 +00:00
HAL_FLASH_Unlock();
FLASH_EraseInitTypeDef EraseInitStruct;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
2017-02-11 15:47:36 +00:00
EraseInitStruct.NbSectors = 1;
uint32_t SectorError = 0;
for (int i = 2; i < 12; i++) {
EraseInitStruct.Sector = i;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
HAL_FLASH_Lock();
display_printf(" failed\n");
return false;
}
display_printf(".");
2017-02-11 15:47:36 +00:00
}
display_printf(" done\n");
2017-02-11 15:47:36 +00:00
display_printf("copying new bootloader from SD card\n");
2017-02-17 14:49:43 +00:00
sdcard_power_on();
2017-04-10 17:24:21 +00:00
// copy bootloader from SD card to Flash
2017-02-17 14:49:43 +00:00
uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
sdcard_read_blocks((uint8_t *)buf, 0, 1);
2017-02-17 14:49:43 +00:00
image_header hdr;
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
display_printf("invalid header\n");
sdcard_power_off();
HAL_FLASH_Lock();
return false;
}
for (int i = 0; i < (HEADER_SIZE + hdr.codelen) / SDCARD_BLOCK_SIZE; i++) {
2017-02-17 14:49:43 +00:00
sdcard_read_blocks((uint8_t *)buf, i, 1);
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
2017-04-10 17:24:21 +00:00
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
display_printf("copy failed\n");
sdcard_power_off();
HAL_FLASH_Lock();
return false;
2017-02-17 14:49:43 +00:00
}
2017-02-11 15:47:36 +00:00
}
}
2017-02-17 14:49:43 +00:00
sdcard_power_off();
2017-02-17 14:49:43 +00:00
HAL_FLASH_Lock();
display_printf("done\n");
2016-10-03 14:17:49 +00:00
return true;
2017-02-17 14:49:43 +00:00
}
2016-10-04 16:01:48 +00:00
void check_and_jump(void)
{
display_printf("checking bootloader\n");
image_header hdr;
2017-04-10 17:24:21 +00:00
if (image_parse_header((const uint8_t *)BOOTLOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
display_printf("valid bootloader header\n");
} else {
display_printf("invalid bootloader header\n");
return;
}
2017-04-10 17:24:21 +00:00
if (image_check_signature((const uint8_t *)BOOTLOADER_START, &hdr, NULL)) {
display_printf("valid bootloader signature\n");
display_printf("JUMP!\n");
2017-04-10 17:24:21 +00:00
jump_to(BOOTLOADER_START + HEADER_SIZE);
} else {
display_printf("invalid bootloader signature\n");
}
}
2017-02-17 14:49:43 +00:00
int main(void)
{
2017-04-10 17:11:44 +00:00
SCB->VTOR = BOARDLOADER_START;
2017-02-11 15:47:36 +00:00
periph_init();
2017-03-28 11:04:54 +00:00
if (0 != display_init()) {
__fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__);
2017-03-28 11:04:54 +00:00
}
if (0 != flash_init()) {
__fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__);
2017-03-28 11:04:54 +00:00
}
if (0 != sdcard_init()) {
__fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__);
2017-03-28 11:04:54 +00:00
}
display_clear();
display_backlight(255);
display_printf("TREZOR Boardloader %d.%d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_BUILD);
display_printf("==================\n");
display_printf("starting boardloader\n");
2017-02-11 15:47:36 +00:00
2017-02-17 14:49:43 +00:00
if (check_sdcard()) {
if (!copy_sdcard()) {
__fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__);
}
2017-02-17 14:49:43 +00:00
}
2017-02-16 12:48:28 +00:00
check_and_jump();
2016-10-03 14:17:49 +00:00
__fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__);
return 0;
}