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-10-11 19:15:22 +00:00
|
|
|
#include "rng.h"
|
2017-02-11 15:47:36 +00:00
|
|
|
#include "sdcard.h"
|
2017-10-11 21:53:29 +00:00
|
|
|
|
|
|
|
#include "lowlevel.h"
|
2017-04-06 15:21:03 +00:00
|
|
|
#include "version.h"
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2017-10-26 13:10:29 +00:00
|
|
|
const uint8_t BOARDLOADER_KEY_M = 2;
|
|
|
|
const uint8_t BOARDLOADER_KEY_N = 3;
|
|
|
|
static const uint8_t * const BOARDLOADER_KEYS[] = {
|
|
|
|
#if PRODUCTION
|
|
|
|
(const uint8_t *)"\x0e\xb9\x85\x6b\xe9\xba\x7e\x97\x2c\x7f\x34\xea\xc1\xed\x9b\x6f\xd0\xef\xd1\x72\xec\x00\xfa\xf0\xc5\x89\x75\x9d\xa4\xdd\xfb\xa0",
|
|
|
|
(const uint8_t *)"\xac\x8a\xb4\x0b\x32\xc9\x86\x55\x79\x8f\xd5\xda\x5e\x19\x2b\xe2\x7a\x22\x30\x6e\xa0\x5c\x6d\x27\x7c\xdf\xf4\xa3\xf4\x12\x5c\xd8",
|
|
|
|
(const uint8_t *)"\xce\x0f\xcd\x12\x54\x3e\xf5\x93\x6c\xf2\x80\x49\x82\x13\x67\x07\x86\x3d\x17\x29\x5f\xac\xed\x72\xaf\x17\x1d\x6e\x65\x13\xff\x06",
|
|
|
|
#else
|
|
|
|
(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",
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2017-10-14 10:56:39 +00:00
|
|
|
static uint32_t check_sdcard(void)
|
2017-02-11 15:47:36 +00:00
|
|
|
{
|
2017-10-27 01:32:26 +00:00
|
|
|
if (sectrue != sdcard_is_present()) {
|
2017-10-14 10:56:39 +00:00
|
|
|
return 0;
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(sdcard_power_on(), NULL);
|
2017-02-11 15:47:36 +00:00
|
|
|
|
|
|
|
uint64_t cap = sdcard_get_capacity_in_bytes();
|
2017-02-17 16:11:34 +00:00
|
|
|
if (cap < 1024 * 1024) {
|
2017-02-11 15:47:36 +00:00
|
|
|
sdcard_power_off();
|
2017-10-14 10:56:39 +00:00
|
|
|
return 0;
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 15:23:25 +00:00
|
|
|
uint32_t buf[IMAGE_HEADER_SIZE / sizeof(uint32_t)];
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-11-06 11:47:42 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
const secbool read_status = sdcard_read_blocks(buf, 0, IMAGE_HEADER_SIZE / SDCARD_BLOCK_SIZE);
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
sdcard_power_off();
|
|
|
|
|
2017-10-14 10:56:39 +00:00
|
|
|
image_header hdr;
|
|
|
|
|
2017-11-06 11:47:42 +00:00
|
|
|
if ((sectrue == read_status) && (sectrue == load_image_header((const uint8_t *)buf, BOOTLOADER_IMAGE_MAGIC, BOOTLOADER_IMAGE_MAXSIZE, BOARDLOADER_KEY_M, BOARDLOADER_KEY_N, BOARDLOADER_KEYS, &hdr))) {
|
2017-10-14 10:56:39 +00:00
|
|
|
return hdr.codelen;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 18:24:28 +00:00
|
|
|
static void progress_callback(int pos, int len) {
|
2017-06-20 15:32:21 +00:00
|
|
|
display_printf(".");
|
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
static secbool copy_sdcard(void)
|
2017-02-17 14:49:43 +00:00
|
|
|
{
|
2017-10-06 09:11:59 +00:00
|
|
|
display_backlight(255);
|
2017-10-11 16:10:49 +00:00
|
|
|
|
2017-10-13 15:19:14 +00:00
|
|
|
display_printf("TREZOR Boardloader\n");
|
|
|
|
display_printf("==================\n\n");
|
|
|
|
|
|
|
|
display_printf("bootloader found on the SD card\n\n");
|
|
|
|
display_printf("applying bootloader in 10 seconds\n\n");
|
2017-10-11 16:10:49 +00:00
|
|
|
display_printf("unplug now if you want to abort\n\n");
|
|
|
|
|
2017-10-15 20:20:13 +00:00
|
|
|
uint32_t codelen;
|
|
|
|
|
2017-10-13 15:19:14 +00:00
|
|
|
for (int i = 10; i >= 0; i--) {
|
|
|
|
display_printf("%d ", i);
|
|
|
|
hal_delay(1000);
|
2017-10-15 20:20:13 +00:00
|
|
|
codelen = check_sdcard();
|
2017-10-27 01:32:26 +00:00
|
|
|
if (0 == codelen) {
|
2017-10-15 20:20:13 +00:00
|
|
|
display_printf("\n\nno SD card, aborting\n");
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-10-15 20:20:13 +00:00
|
|
|
}
|
2017-10-13 15:19:14 +00:00
|
|
|
}
|
2017-10-11 16:10:49 +00:00
|
|
|
|
2017-10-15 20:20:13 +00:00
|
|
|
display_printf("\n\nerasing flash:\n\n");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-10-16 12:22:10 +00:00
|
|
|
// erase all flash (except boardloader)
|
2017-12-09 17:00:10 +00:00
|
|
|
const uint8_t sectors[] = {
|
2017-12-12 23:11:25 +00:00
|
|
|
3,
|
2017-10-16 18:24:28 +00:00
|
|
|
FLASH_SECTOR_STORAGE_1,
|
|
|
|
FLASH_SECTOR_STORAGE_2,
|
|
|
|
FLASH_SECTOR_BOOTLOADER,
|
|
|
|
FLASH_SECTOR_FIRMWARE_START,
|
|
|
|
7,
|
|
|
|
8,
|
|
|
|
9,
|
|
|
|
10,
|
|
|
|
FLASH_SECTOR_FIRMWARE_END,
|
|
|
|
FLASH_SECTOR_UNUSED_START,
|
|
|
|
13,
|
|
|
|
14,
|
|
|
|
FLASH_SECTOR_UNUSED_END,
|
|
|
|
FLASH_SECTOR_FIRMWARE_EXTRA_START,
|
|
|
|
18,
|
|
|
|
19,
|
|
|
|
20,
|
|
|
|
21,
|
|
|
|
22,
|
|
|
|
FLASH_SECTOR_FIRMWARE_EXTRA_END,
|
|
|
|
};
|
2017-12-09 17:00:10 +00:00
|
|
|
if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_callback)) {
|
2017-06-20 15:32:21 +00:00
|
|
|
display_printf(" failed\n");
|
2017-10-26 21:51:39 +00:00
|
|
|
return secfalse;
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
2017-10-13 15:19:14 +00:00
|
|
|
display_printf(" done\n\n");
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-12-16 17:42:06 +00:00
|
|
|
ensure(flash_unlock(), NULL);
|
2017-09-27 09:08:46 +00:00
|
|
|
|
2017-10-14 10:56:39 +00:00
|
|
|
// copy bootloader from SD card to Flash
|
|
|
|
display_printf("copying new bootloader from SD card\n\n");
|
|
|
|
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(sdcard_power_on(), NULL);
|
2017-10-14 10:56:39 +00:00
|
|
|
|
|
|
|
uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
|
2017-10-25 23:17:57 +00:00
|
|
|
for (int i = 0; i < (IMAGE_HEADER_SIZE + codelen) / SDCARD_BLOCK_SIZE; i++) {
|
2017-12-16 16:54:04 +00:00
|
|
|
ensure(sdcard_read_blocks(buf, i, 1), NULL);
|
2017-02-17 14:49:43 +00:00
|
|
|
for (int j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
2018-01-23 12:43:18 +00:00
|
|
|
ensure(flash_write_word(FLASH_SECTOR_BOOTLOADER, i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]), NULL);
|
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-12-16 16:54:04 +00:00
|
|
|
ensure(flash_lock(), NULL);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2017-10-13 15:19:14 +00:00
|
|
|
display_printf("\ndone\n\n");
|
2017-10-05 18:18:35 +00:00
|
|
|
display_printf("Unplug the device and remove the SD card\n");
|
2016-10-03 14:17:49 +00:00
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
return sectrue;
|
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-12-09 13:36:02 +00:00
|
|
|
if (sectrue != reset_flags_check()) {
|
2017-10-22 16:26:50 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:35:41 +00:00
|
|
|
// need the systick timer running before many HAL operations.
|
|
|
|
// want the PVD enabled before flash operations too.
|
|
|
|
periph_init();
|
|
|
|
|
|
|
|
if (sectrue != flash_configure_option_bytes()) {
|
|
|
|
const uint8_t sectors[] = {
|
2017-10-16 19:43:07 +00:00
|
|
|
FLASH_SECTOR_STORAGE_1,
|
|
|
|
FLASH_SECTOR_STORAGE_2,
|
|
|
|
};
|
2017-12-16 16:54:04 +00:00
|
|
|
// display is not initialized so don't call ensure
|
|
|
|
secbool r = flash_erase_sectors(sectors, sizeof(sectors), NULL);
|
|
|
|
(void)r;
|
2017-10-22 18:28:10 +00:00
|
|
|
return 2;
|
2017-10-14 10:44:36 +00:00
|
|
|
}
|
2017-10-12 12:35:01 +00:00
|
|
|
|
2017-10-11 21:11:59 +00:00
|
|
|
clear_otg_hs_memory();
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
display_init();
|
|
|
|
sdcard_init();
|
2017-03-23 15:22:58 +00:00
|
|
|
|
2017-02-17 14:49:43 +00:00
|
|
|
if (check_sdcard()) {
|
2017-10-26 21:51:39 +00:00
|
|
|
return copy_sdcard() == sectrue ? 0 : 3;
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
2017-02-16 12:48:28 +00:00
|
|
|
|
2017-10-13 15:19:14 +00:00
|
|
|
image_header hdr;
|
|
|
|
|
|
|
|
ensure(
|
2017-10-26 13:10:29 +00:00
|
|
|
load_image_header((const uint8_t *)BOOTLOADER_START, BOOTLOADER_IMAGE_MAGIC, BOOTLOADER_IMAGE_MAXSIZE, BOARDLOADER_KEY_M, BOARDLOADER_KEY_N, BOARDLOADER_KEYS, &hdr),
|
2017-10-13 15:19:14 +00:00
|
|
|
"invalid bootloader header");
|
|
|
|
|
2017-12-16 14:12:58 +00:00
|
|
|
const uint8_t sectors[] = {
|
2017-10-26 17:09:53 +00:00
|
|
|
FLASH_SECTOR_BOOTLOADER,
|
|
|
|
};
|
2017-10-26 15:16:59 +00:00
|
|
|
ensure(
|
2017-10-26 17:09:53 +00:00
|
|
|
check_image_contents(&hdr, IMAGE_HEADER_SIZE, sectors, 1),
|
2017-10-26 15:16:59 +00:00
|
|
|
"invalid bootloader hash");
|
|
|
|
|
2017-10-25 23:17:57 +00:00
|
|
|
jump_to(BOOTLOADER_START + IMAGE_HEADER_SIZE);
|
2016-07-20 16:33:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|