2016-07-20 16:33:49 +00:00
|
|
|
#include STM32_HAL_H
|
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-03-21 00:41:49 +00:00
|
|
|
#include "common.h"
|
2016-07-20 16:33:49 +00:00
|
|
|
#include "display.h"
|
2017-04-01 10:57:14 +00:00
|
|
|
#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"
|
2017-04-06 15:21:03 +00:00
|
|
|
#include "version.h"
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2017-04-05 15:41:10 +00:00
|
|
|
#define IMAGE_MAGIC 0x4C5A5254 // TRZL
|
|
|
|
#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
|
|
|
|
|
2017-03-21 14:56:50 +00:00
|
|
|
void pendsv_isr_handler(void) {
|
|
|
|
__fatal_error("pendsv");
|
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
|
|
|
{
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("checking for SD card");
|
2017-02-17 14:49:43 +00:00
|
|
|
|
|
|
|
if (!sdcard_is_present()) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("no SD card found");
|
2017-02-17 14:49:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("SD card found");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
|
|
|
sdcard_power_on();
|
|
|
|
|
|
|
|
uint64_t cap = sdcard_get_capacity_in_bytes();
|
2017-02-17 16:11:34 +00:00
|
|
|
if (cap < 1024 * 1024) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("SD card too small");
|
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
|
|
|
}
|
|
|
|
|
2017-04-01 10:57:14 +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();
|
|
|
|
|
2017-04-05 15:41:10 +00:00
|
|
|
if (image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, NULL)) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("SD card header is valid");
|
2017-02-17 14:49:43 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("SD card header is invalid");
|
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
|
|
|
}
|
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
bool copy_sdcard(void)
|
2017-02-17 14:49:43 +00:00
|
|
|
{
|
|
|
|
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINT("erasing flash ");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-03-21 15:06:22 +00:00
|
|
|
// erase flash (except bootloader)
|
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);
|
2017-04-05 13:10:33 +00:00
|
|
|
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;
|
2017-03-29 00:42:55 +00:00
|
|
|
for (int i = 2; i < 12; i++) {
|
2017-02-17 16:11:34 +00:00
|
|
|
EraseInitStruct.Sector = i;
|
|
|
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
|
|
|
HAL_FLASH_Lock();
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN(" failed");
|
2017-02-17 16:11:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINT(".");
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN(" done");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("copying new loader from SD card");
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
sdcard_power_on();
|
|
|
|
|
2017-03-21 15:06:22 +00:00
|
|
|
// copy loader from SD card to Flash
|
2017-02-17 14:49:43 +00:00
|
|
|
uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
|
2017-02-17 16:11:34 +00:00
|
|
|
sdcard_read_blocks((uint8_t *)buf, 0, 1);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2017-04-01 10:57:14 +00:00
|
|
|
image_header hdr;
|
2017-04-05 15:41:10 +00:00
|
|
|
if (!image_parse_header((const uint8_t *)buf, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("invalid header");
|
2017-04-01 11:53:08 +00:00
|
|
|
sdcard_power_off();
|
|
|
|
HAL_FLASH_Lock();
|
2017-02-17 16:11:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-01 10:57:14 +00:00
|
|
|
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-05 13:10:33 +00:00
|
|
|
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, LOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("copy failed");
|
2017-02-17 16:11:34 +00:00
|
|
|
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
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
sdcard_power_off();
|
2017-02-17 14:49:43 +00:00
|
|
|
HAL_FLASH_Lock();
|
|
|
|
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("done");
|
2016-10-03 14:17:49 +00:00
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
return true;
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
2016-10-04 16:01:48 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
void check_and_jump(void)
|
|
|
|
{
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("checking loader");
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
image_header hdr;
|
2017-04-05 14:24:43 +00:00
|
|
|
|
2017-04-05 15:41:10 +00:00
|
|
|
if (image_parse_header((const uint8_t *)LOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("valid loader header");
|
2017-04-05 14:24:43 +00:00
|
|
|
} else {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("invalid loader header");
|
2017-04-05 13:10:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image_check_signature((const uint8_t *)LOADER_START, &hdr, NULL)) {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("valid loader signature");
|
2017-04-05 14:24:43 +00:00
|
|
|
|
2017-04-01 10:57:14 +00:00
|
|
|
// TODO: remove debug wait
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("waiting 1 second");
|
2017-04-01 10:57:14 +00:00
|
|
|
HAL_Delay(1000);
|
|
|
|
// end
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("JUMP!");
|
2017-04-01 10:57:14 +00:00
|
|
|
jump_to(LOADER_START + HEADER_SIZE);
|
2017-04-05 14:24:43 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
} else {
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("invalid loader signature");
|
2017-04-01 00:32:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-03-30 14:47:02 +00:00
|
|
|
SCB->VTOR = BOOTLOADER_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 failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != flash_init()) {
|
|
|
|
__fatal_error("flash_init failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != sdcard_init()) {
|
|
|
|
__fatal_error("sdcard_init failed");
|
|
|
|
}
|
2017-03-23 15:22:58 +00:00
|
|
|
|
|
|
|
display_clear();
|
|
|
|
display_backlight(255);
|
|
|
|
|
2017-04-06 15:25:14 +00:00
|
|
|
DPRINTLN("TREZOR Bootloader " VERSION_STR);
|
|
|
|
DPRINTLN("=================");
|
|
|
|
DPRINTLN("starting bootloader");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
if (check_sdcard()) {
|
2017-02-17 16:11:34 +00:00
|
|
|
if (!copy_sdcard()) {
|
2017-03-21 00:41:49 +00:00
|
|
|
__fatal_error("halt");
|
2017-02-17 16:11:34 +00:00
|
|
|
}
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
2017-02-16 12:48:28 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
check_and_jump();
|
2016-10-03 14:17:49 +00:00
|
|
|
|
2017-03-21 00:41:49 +00:00
|
|
|
__fatal_error("halt");
|
2016-07-20 16:33:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|