|
|
|
@ -3,7 +3,6 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOTLOADER_PRINTLN("copying new stage 2 from SD card");
|
|
|
|
|
|
|
|
|
|
// copy stage 2 from SD card to Flash
|
|
|
|
|
uint32_t src;
|
|
|
|
|
int block = 0;
|
|
|
|
|
int offset = 256;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void halt(void)
|
|
|
|
|
{
|
|
|
|
|
for (;;) {
|
|
|
|
|
display_backlight(255);
|
|
|
|
|
HAL_Delay(950);
|
|
|
|
|
display_backlight(0);
|
|
|
|
|
HAL_Delay(50);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
|
int main(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
periph_init();
|
|
|
|
|
|
|
|
|
|
screen_stage1();
|
|
|
|
|
BOOTLOADER_PRINTLN("TREZOR Bootloader");
|
|
|
|
|
BOOTLOADER_PRINTLN("=================");
|
|
|
|
|
BOOTLOADER_PRINTLN("starting stage 1");
|
|
|
|
|
|
|
|
|
|
check_sdcard();
|
|
|
|
|
|
|
|
|
|
check_signature();
|
|
|
|
|
if (check_sdcard()) {
|
|
|
|
|
copy_sdcard();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOTLOADER_PRINTLN("checking stage 2");
|
|
|
|
|
if (check_header((const uint8_t *)STAGE2_START)) {
|
|
|
|
|
screen_stage2_jump();
|
|
|
|
|
// TODO: jump to second stage
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screen_stage2_invalid();
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
|
display_backlight(255);
|
|
|
|
|
HAL_Delay(950);
|
|
|
|
|
display_backlight(0);
|
|
|
|
|
HAL_Delay(50);
|
|
|
|
|
}
|
|
|
|
|
BOOTLOADER_PRINTLN("HALT!");
|
|
|
|
|
halt();
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|