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-10 17:24:21 +00:00
|
|
|
#define IMAGE_MAGIC 0x425A5254 // TRZB
|
2017-04-05 15:41:10 +00:00
|
|
|
#define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024)
|
|
|
|
|
2017-03-21 14:56:50 +00:00
|
|
|
void pendsv_isr_handler(void) {
|
2017-04-28 13:39:22 +00:00
|
|
|
__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
|
|
|
{
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("checking for SD card\n");
|
2017-02-17 14:49:43 +00:00
|
|
|
|
|
|
|
if (!sdcard_is_present()) {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("no SD card found\n");
|
2017-02-17 14:49:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-28 17:50:51 +00:00
|
|
|
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();
|
2017-02-17 16:11:34 +00:00
|
|
|
if (cap < 1024 * 1024) {
|
2017-04-28 17:50:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
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-28 17:50:51 +00:00
|
|
|
display_printf("SD card header is valid\n");
|
2017-02-17 14:49:43 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2017-04-28 17:50:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-06-20 17:53:24 +00:00
|
|
|
static void progress_callback(uint16_t val) {
|
2017-06-20 15:32:21 +00:00
|
|
|
display_printf(".");
|
|
|
|
}
|
|
|
|
|
2017-02-17 16:11:34 +00:00
|
|
|
bool copy_sdcard(void)
|
2017-02-17 14:49:43 +00:00
|
|
|
{
|
2017-04-28 17:50:51 +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-09-27 09:08:46 +00:00
|
|
|
if (!flash_erase_sectors(FLASH_SECTOR_BOARDLOADER_END + 1, FLASH_SECTOR_FIRMWARE_END, progress_callback)) {
|
2017-06-20 15:32:21 +00:00
|
|
|
display_printf(" failed\n");
|
|
|
|
return false;
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf(" done\n");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("copying new bootloader from SD card\n");
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2017-02-17 16:11:34 +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)];
|
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-28 17:50:51 +00:00
|
|
|
display_printf("invalid header\n");
|
2017-04-01 11:53:08 +00:00
|
|
|
sdcard_power_off();
|
2017-02-17 16:11:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-27 09:08:46 +00:00
|
|
|
if (!flash_unlock()) {
|
|
|
|
display_printf("could not unlock flash\n");
|
|
|
|
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-09-28 09:27:58 +00:00
|
|
|
if (!flash_write_word(BOOTLOADER_START + i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j])) {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("copy failed\n");
|
2017-02-17 16:11:34 +00:00
|
|
|
sdcard_power_off();
|
2017-09-27 09:08:46 +00:00
|
|
|
flash_lock();
|
2017-02-17 16:11:34 +00:00
|
|
|
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-09-27 09:08:46 +00:00
|
|
|
flash_lock();
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("done\n");
|
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-09-29 15:31:59 +00:00
|
|
|
const uint8_t BOARDLOADER_KEY_M = 1;
|
|
|
|
const uint8_t BOARDLOADER_KEY_N = 3;
|
|
|
|
static const uint8_t * const BOARDLOADER_KEYS[] = {
|
|
|
|
(const uint8_t *)"\xdb\x99\x5f\xe2\x51\x69\xd1\x41\xca\xb9\xbb\xba\x92\xba\xa0\x1f\x9f\x2e\x1e\xce\x7d\xf4\xcb\x2a\xc0\x51\x90\xf3\x7f\xcc\x1f\x9d",
|
|
|
|
(const uint8_t *)"\x21\x52\xf8\xd1\x9b\x79\x1d\x24\x45\x32\x42\xe1\x5f\x2e\xab\x6c\xb7\xcf\xfa\x7b\x6a\x5e\xd3\x00\x97\x96\x0e\x06\x98\x81\xdb\x12",
|
|
|
|
(const uint8_t *)"\x22\xfc\x29\x77\x92\xf0\xb6\xff\xc0\xbf\xcf\xdb\x7e\xdb\x0c\x0a\xa1\x4e\x02\x5a\x36\x5e\xc0\xe3\x42\xe8\x6e\x38\x29\xcb\x74\xb6",
|
|
|
|
};
|
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
void check_and_jump(void)
|
|
|
|
{
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("checking bootloader\n");
|
2017-04-05 13:10:33 +00:00
|
|
|
|
|
|
|
image_header hdr;
|
2017-04-05 14:24:43 +00:00
|
|
|
|
2017-04-10 17:24:21 +00:00
|
|
|
if (image_parse_header((const uint8_t *)BOOTLOADER_START, IMAGE_MAGIC, IMAGE_MAXSIZE, &hdr)) {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("valid bootloader header\n");
|
2017-04-05 14:24:43 +00:00
|
|
|
} else {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("invalid bootloader header\n");
|
2017-04-05 13:10:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-29 15:31:59 +00:00
|
|
|
if (image_check_signature((const uint8_t *)BOOTLOADER_START, &hdr, BOARDLOADER_KEY_M, BOARDLOADER_KEY_N, BOARDLOADER_KEYS)) {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("valid bootloader signature\n");
|
|
|
|
display_printf("JUMP!\n");
|
2017-04-10 17:24:21 +00:00
|
|
|
jump_to(BOOTLOADER_START + HEADER_SIZE);
|
2017-04-05 14:24:43 +00:00
|
|
|
|
2017-04-01 00:32:05 +00:00
|
|
|
} else {
|
2017-04-28 17:50:51 +00:00
|
|
|
display_printf("invalid bootloader signature\n");
|
2017-04-01 00:32:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
int main(void)
|
|
|
|
{
|
2017-02-11 15:47:36 +00:00
|
|
|
periph_init();
|
|
|
|
|
2017-03-28 11:04:54 +00:00
|
|
|
if (0 != display_init()) {
|
2017-04-28 13:39:22 +00:00
|
|
|
__fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__);
|
2017-03-28 11:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != flash_init()) {
|
2017-04-28 13:39:22 +00:00
|
|
|
__fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__);
|
2017-03-28 11:04:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 != sdcard_init()) {
|
2017-04-28 13:39:22 +00:00
|
|
|
__fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__);
|
2017-03-28 11:04:54 +00:00
|
|
|
}
|
2017-03-23 15:22:58 +00:00
|
|
|
|
2017-04-28 17:50:51 +00:00
|
|
|
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()) {
|
2017-02-17 16:11:34 +00:00
|
|
|
if (!copy_sdcard()) {
|
2017-04-28 13:39:22 +00:00
|
|
|
__fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__);
|
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-04-28 13:39:22 +00:00
|
|
|
__fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__);
|
2016-07-20 16:33:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|