2016-07-20 16:33:49 +00:00
|
|
|
#include STM32_HAL_H
|
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-10-04 16:01:48 +00:00
|
|
|
#include "crypto.h"
|
2017-03-21 00:41:49 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2016-07-20 16:33:49 +00:00
|
|
|
#include "display.h"
|
2017-02-11 15:47:36 +00:00
|
|
|
#include "sdcard.h"
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
#define STAGE2_START 0x08010000
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2017-03-21 00:41:49 +00:00
|
|
|
#define BOOTLOADER_FGCOLOR 0xFFFF
|
|
|
|
#define BOOTLOADER_BGCOLOR 0x0000
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2017-03-21 00:41:49 +00:00
|
|
|
#define BOOTLOADER_PRINT(X) do { display_print(X, -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0)
|
|
|
|
#define BOOTLOADER_PRINTLN(X) do { display_print(X "\n", -1); display_print_out(BOOTLOADER_FGCOLOR, BOOTLOADER_BGCOLOR); } while(0)
|
2017-03-18 11:02:39 +00:00
|
|
|
|
|
|
|
void mp_hal_stdout_tx_str(const char *str) {
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
void periph_init(void)
|
|
|
|
{
|
2016-07-20 16:33:49 +00:00
|
|
|
HAL_Init();
|
|
|
|
|
|
|
|
SystemClock_Config();
|
|
|
|
|
|
|
|
__GPIOA_CLK_ENABLE();
|
|
|
|
__GPIOB_CLK_ENABLE();
|
|
|
|
__GPIOC_CLK_ENABLE();
|
|
|
|
__GPIOD_CLK_ENABLE();
|
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
sdcard_init();
|
|
|
|
|
2016-07-20 16:33:49 +00:00
|
|
|
display_init();
|
2016-09-28 16:15:06 +00:00
|
|
|
display_clear();
|
2017-02-11 15:47:36 +00:00
|
|
|
display_backlight(255);
|
|
|
|
}
|
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
bool check_sdcard(void)
|
2017-02-11 15:47:36 +00:00
|
|
|
{
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("checking for SD card");
|
|
|
|
|
|
|
|
if (!sdcard_is_present()) {
|
|
|
|
BOOTLOADER_PRINTLN("no SD card found");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOTLOADER_PRINTLN("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-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("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
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t buf[SDCARD_BLOCK_SIZE] __attribute__((aligned(4)));
|
|
|
|
|
|
|
|
sdcard_read_blocks(buf, 0, 1);
|
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
sdcard_power_off();
|
|
|
|
|
2017-02-19 16:48:28 +00:00
|
|
|
if (parse_header(buf, NULL, NULL, NULL)) {
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("SD card header is valid");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
BOOTLOADER_PRINTLN("SD card header is invalid");
|
|
|
|
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-02-17 16:11:34 +00:00
|
|
|
BOOTLOADER_PRINT("erasing flash ");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
// erase flash (except stage 1)
|
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 = TYPEERASE_SECTORS;
|
|
|
|
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
|
|
|
|
EraseInitStruct.NbSectors = 1;
|
|
|
|
uint32_t SectorError = 0;
|
2017-02-17 16:11:34 +00:00
|
|
|
for (int i = 3; i <= 11; i++) { // TODO: change start to 2
|
|
|
|
EraseInitStruct.Sector = i;
|
|
|
|
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
|
|
|
|
HAL_FLASH_Lock();
|
|
|
|
BOOTLOADER_PRINTLN(" failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
BOOTLOADER_PRINT(".");
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
2017-02-17 16:11:34 +00:00
|
|
|
BOOTLOADER_PRINTLN(" done");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("copying new stage 2 from SD card");
|
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
sdcard_power_on();
|
|
|
|
|
2017-02-11 15:47:36 +00:00
|
|
|
// copy stage 2 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-02-17 16:11:34 +00:00
|
|
|
uint32_t codelen;
|
2017-02-19 16:48:28 +00:00
|
|
|
if (!parse_header((uint8_t *)buf, &codelen, NULL, NULL)) {
|
2017-02-17 16:11:34 +00:00
|
|
|
BOOTLOADER_PRINTLN("wrong header");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 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++) {
|
|
|
|
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, STAGE2_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]) != HAL_OK) {
|
2017-02-17 16:11:34 +00:00
|
|
|
BOOTLOADER_PRINTLN("copy failed");
|
|
|
|
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();
|
|
|
|
|
|
|
|
BOOTLOADER_PRINTLN("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-02-17 14:49:43 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-02-11 15:47:36 +00:00
|
|
|
periph_init();
|
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("TREZOR Bootloader");
|
|
|
|
BOOTLOADER_PRINTLN("=================");
|
|
|
|
BOOTLOADER_PRINTLN("starting stage 1");
|
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-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("checking stage 2");
|
2017-02-19 16:48:28 +00:00
|
|
|
if (parse_header((const uint8_t *)STAGE2_START, NULL, NULL, NULL)) {
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("valid stage 2 header");
|
2017-02-19 16:48:28 +00:00
|
|
|
if (check_signature((const uint8_t *)STAGE2_START)) {
|
2017-02-17 14:49:43 +00:00
|
|
|
BOOTLOADER_PRINTLN("valid stage 2 signature");
|
|
|
|
BOOTLOADER_PRINTLN("JUMP!");
|
|
|
|
// TODO: jump to second stage
|
2017-03-21 00:41:49 +00:00
|
|
|
__fatal_error("halt");
|
2017-02-17 14:49:43 +00:00
|
|
|
} else {
|
|
|
|
BOOTLOADER_PRINTLN("invalid stage 2 signature");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
BOOTLOADER_PRINTLN("invalid stage 2 header");
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|