2018-02-26 13:06:10 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2018-02-26 13:06:10 +00:00
|
|
|
*
|
|
|
|
* Copyright (c) SatoshiLabs
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2024-11-01 11:55:31 +00:00
|
|
|
#include <trezor_model.h>
|
|
|
|
#include <trezor_rtl.h>
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <gfx/gfx_bitblt.h>
|
|
|
|
#include <gfx/gfx_draw.h>
|
|
|
|
#include <gfx/terminal.h>
|
|
|
|
#include <io/display.h>
|
|
|
|
#include <sec/monoctr.h>
|
|
|
|
#include <sec/rng.h>
|
|
|
|
#include <sec/secret.h>
|
|
|
|
#include <sys/bootutils.h>
|
|
|
|
#include <sys/mpu.h>
|
|
|
|
#include <sys/reset_flags.h>
|
|
|
|
#include <sys/system.h>
|
|
|
|
#include <sys/systick.h>
|
|
|
|
#include <util/board_capabilities.h>
|
|
|
|
#include <util/flash.h>
|
|
|
|
#include <util/flash_utils.h>
|
|
|
|
#include <util/image.h>
|
|
|
|
#include <util/option_bytes.h>
|
|
|
|
#include <util/rsod.h>
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#ifdef USE_PVD
|
|
|
|
#include <sys/pvd.h>
|
2022-04-25 09:46:09 +00:00
|
|
|
#endif
|
2024-11-15 09:07:01 +00:00
|
|
|
|
|
|
|
#ifdef USE_SD_CARD
|
|
|
|
#include <io/sdcard.h>
|
2023-02-09 22:56:03 +00:00
|
|
|
#endif
|
2024-11-15 09:07:01 +00:00
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sec/hash_processor.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_TRUSTZONE
|
|
|
|
#include <sys/trustzone.h>
|
2023-12-15 22:50:33 +00:00
|
|
|
#endif
|
2017-10-11 21:53:29 +00:00
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#ifdef USE_TAMPER
|
|
|
|
#include <sys/tamper.h>
|
2024-08-05 15:13:54 +00:00
|
|
|
#endif
|
|
|
|
|
2024-10-31 08:23:37 +00:00
|
|
|
#include "memzero.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[] = {
|
2023-06-01 11:27:55 +00:00
|
|
|
#if !PRODUCTION
|
|
|
|
(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-10-26 13:10:29 +00:00
|
|
|
#else
|
2023-06-01 12:21:51 +00:00
|
|
|
MODEL_BOARDLOADER_KEYS
|
2017-10-26 13:10:29 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2024-08-27 10:36:35 +00:00
|
|
|
static uint8_t get_bootloader_min_version(void) {
|
|
|
|
uint8_t version = 0;
|
|
|
|
ensure(monoctr_read(MONOCTR_BOOTLOADER_VERSION, &version), "monoctr read");
|
|
|
|
return version;
|
2024-02-26 08:49:58 +00:00
|
|
|
}
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2024-08-27 10:36:35 +00:00
|
|
|
static void write_bootloader_min_version(uint8_t version) {
|
2024-02-26 08:49:58 +00:00
|
|
|
if (version > get_bootloader_min_version()) {
|
2024-08-27 10:36:35 +00:00
|
|
|
ensure(monoctr_write(MONOCTR_BOOTLOADER_VERSION, version), "monoctr write");
|
2023-06-28 08:51:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-01 20:05:45 +00:00
|
|
|
struct BoardCapabilities capabilities
|
2022-05-31 11:29:04 +00:00
|
|
|
__attribute__((section(".capabilities_section"))) = {
|
|
|
|
.header = CAPABILITIES_HEADER,
|
2023-06-01 12:21:51 +00:00
|
|
|
.model_tag = TAG_MODEL_NAME,
|
2022-11-21 13:12:49 +00:00
|
|
|
.model_length = sizeof(uint32_t),
|
|
|
|
.model_name = HW_MODEL,
|
2023-06-01 12:21:51 +00:00
|
|
|
.version_tag = TAG_BOARDLOADER_VERSION,
|
2022-05-31 11:29:04 +00:00
|
|
|
.version_length = sizeof(struct BoardloaderVersion),
|
|
|
|
.version = {.version_major = VERSION_MAJOR,
|
|
|
|
.version_minor = VERSION_MINOR,
|
|
|
|
.version_patch = VERSION_PATCH,
|
|
|
|
.version_build = VERSION_BUILD},
|
2023-06-01 12:21:51 +00:00
|
|
|
.terminator_tag = TAG_TERMINATOR,
|
2022-05-31 11:29:04 +00:00
|
|
|
.terminator_length = 0};
|
|
|
|
|
2020-02-25 16:59:16 +00:00
|
|
|
// we use SRAM as SD card read buffer (because DMA can't access the CCMRAM)
|
2024-11-13 10:04:53 +00:00
|
|
|
__attribute__((section(".buf")))
|
|
|
|
uint32_t sdcard_buf[BOOTLOADER_MAXSIZE / sizeof(uint32_t)];
|
2020-02-25 16:59:16 +00:00
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#if defined USE_SD_CARD
|
2019-03-29 15:26:02 +00:00
|
|
|
static uint32_t check_sdcard(void) {
|
|
|
|
if (sectrue != sdcard_power_on()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
uint64_t cap = sdcard_get_capacity_in_bytes();
|
|
|
|
if (cap < 1024 * 1024) {
|
|
|
|
sdcard_power_off();
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2020-02-25 16:59:16 +00:00
|
|
|
memzero(sdcard_buf, IMAGE_HEADER_SIZE);
|
2017-02-11 15:47:36 +00:00
|
|
|
|
2024-09-23 14:25:18 +00:00
|
|
|
const secbool read_status =
|
|
|
|
sdcard_read_blocks(sdcard_buf, 0, BOOTLOADER_MAXSIZE / SDCARD_BLOCK_SIZE);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_power_off();
|
2017-10-14 10:56:39 +00:00
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
if (sectrue == read_status) {
|
|
|
|
const image_header *hdr =
|
|
|
|
read_image_header((const uint8_t *)sdcard_buf, BOOTLOADER_IMAGE_MAGIC,
|
2024-09-23 14:25:18 +00:00
|
|
|
BOOTLOADER_MAXSIZE);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
if (hdr != (const image_header *)sdcard_buf) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue != check_image_model(hdr)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue != check_image_header_sig(hdr, BOARDLOADER_KEY_M,
|
|
|
|
BOARDLOADER_KEY_N,
|
|
|
|
BOARDLOADER_KEYS)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-23 14:25:18 +00:00
|
|
|
_Static_assert(IMAGE_CHUNK_SIZE >= BOOTLOADER_MAXSIZE,
|
2024-03-01 19:02:07 +00:00
|
|
|
"BOOTLOADER IMAGE MAXSIZE too large for IMAGE_CHUNK_SIZE");
|
|
|
|
|
2024-08-06 11:15:24 +00:00
|
|
|
const uint32_t headers_end_offset = hdr->hdrlen;
|
|
|
|
const uint32_t code_start_offset = IMAGE_CODE_ALIGN(headers_end_offset);
|
|
|
|
|
|
|
|
for (uint32_t i = headers_end_offset; i < code_start_offset; i++) {
|
|
|
|
if (((uint8_t *)sdcard_buf)[i] != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sectrue !=
|
|
|
|
(check_single_hash(hdr->hashes,
|
|
|
|
(const uint8_t *)sdcard_buf + code_start_offset,
|
|
|
|
hdr->codelen))) {
|
2024-03-01 19:02:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = IMAGE_HASH_DIGEST_LENGTH; i < sizeof(hdr->hashes); i++) {
|
|
|
|
if (hdr->hashes[i] != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-26 08:49:58 +00:00
|
|
|
if (hdr->monotonic < get_bootloader_min_version()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
return hdr->codelen;
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2022-11-21 13:12:49 +00:00
|
|
|
|
|
|
|
return 0;
|
2017-06-20 15:32:21 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
static void progress_callback(int pos, int len) { term_printf("."); }
|
2017-10-11 16:10:49 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static secbool copy_sdcard(void) {
|
2024-11-13 10:04:53 +00:00
|
|
|
display_set_backlight(255);
|
2017-10-13 15:19:14 +00:00
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("Trezor Boardloader\n");
|
|
|
|
term_printf("==================\n\n");
|
2017-10-11 16:10:49 +00:00
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("bootloader found on the SD card\n\n");
|
|
|
|
term_printf("applying bootloader in 10 seconds\n\n");
|
|
|
|
term_printf("unplug now if you want to abort\n\n");
|
2017-10-15 20:20:13 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
uint32_t codelen;
|
2017-10-11 16:10:49 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
for (int i = 10; i >= 0; i--) {
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("%d ", i);
|
2019-03-29 15:26:02 +00:00
|
|
|
hal_delay(1000);
|
|
|
|
codelen = check_sdcard();
|
|
|
|
if (0 == codelen) {
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("\n\nno SD card, aborting\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
return secfalse;
|
2017-02-11 15:47:36 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("\n\nerasing flash:\n\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
|
|
|
|
// erase all flash (except boardloader)
|
2024-09-07 11:50:10 +00:00
|
|
|
if (sectrue != erase_device(progress_callback)) {
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf(" failed\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
return secfalse;
|
|
|
|
}
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf(" done\n\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
|
|
|
|
ensure(flash_unlock_write(), NULL);
|
|
|
|
|
|
|
|
// copy bootloader from SD card to Flash
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("copying new bootloader from SD card\n\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2024-03-13 15:33:33 +00:00
|
|
|
ensure(flash_area_write_data(&BOOTLOADER_AREA, 0, sdcard_buf,
|
|
|
|
IMAGE_HEADER_SIZE + codelen),
|
|
|
|
NULL);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(flash_lock_write(), NULL);
|
2017-02-17 14:49:43 +00:00
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("\ndone\n\n");
|
|
|
|
term_printf("Unplug the device and remove the SD card\n");
|
2016-10-03 14:17:49 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return sectrue;
|
2017-02-17 14:49:43 +00:00
|
|
|
}
|
2022-04-25 09:46:09 +00:00
|
|
|
#endif
|
2016-10-04 16:01:48 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
int main(void) {
|
2024-09-18 07:24:59 +00:00
|
|
|
system_init(&rsod_panic_handler);
|
2024-07-12 09:35:49 +00:00
|
|
|
|
2021-03-10 17:31:51 +00:00
|
|
|
reset_flags_reset();
|
2017-10-22 16:26:50 +00:00
|
|
|
|
2024-10-04 11:28:09 +00:00
|
|
|
#ifdef USE_PVD
|
|
|
|
pvd_init();
|
|
|
|
#endif
|
2017-10-31 13:35:41 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (sectrue != flash_configure_option_bytes()) {
|
|
|
|
// display is not initialized so don't call ensure
|
2024-09-07 11:50:10 +00:00
|
|
|
erase_storage(NULL);
|
2019-03-29 15:26:02 +00:00
|
|
|
return 2;
|
|
|
|
}
|
2017-10-12 12:35:01 +00:00
|
|
|
|
2024-10-31 16:05:44 +00:00
|
|
|
#ifdef USE_TAMPER
|
2023-06-28 08:51:30 +00:00
|
|
|
tamper_init();
|
2024-10-31 08:23:37 +00:00
|
|
|
#endif
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2024-10-31 08:23:37 +00:00
|
|
|
#ifdef USE_TRUSTZONE
|
2024-10-31 08:23:37 +00:00
|
|
|
tz_init_boardloader();
|
2023-06-28 08:51:30 +00:00
|
|
|
#endif
|
|
|
|
|
2024-09-25 14:40:47 +00:00
|
|
|
secret_init();
|
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
|
|
|
hash_processor_init();
|
|
|
|
#endif
|
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
gfx_bitblt_init();
|
2024-08-05 15:13:54 +00:00
|
|
|
|
2024-05-31 13:16:35 +00:00
|
|
|
display_init(DISPLAY_RESET_CONTENT);
|
|
|
|
|
2024-11-13 10:04:53 +00:00
|
|
|
gfx_clear();
|
2023-12-20 21:05:50 +00:00
|
|
|
display_refresh();
|
2022-04-25 09:46:09 +00:00
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#if defined USE_SD_CARD
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_init();
|
2017-03-23 15:22:58 +00:00
|
|
|
|
2024-02-26 08:49:58 +00:00
|
|
|
// If the bootloader is being updated from SD card, we need to preserve the
|
|
|
|
// monotonic counter from the old bootloader. This is in case that the old
|
|
|
|
// bootloader did not have the chance yet to write its monotonic counter to
|
|
|
|
// the secret area - which normally happens later in the flow.
|
|
|
|
const image_header *old_hdr = read_image_header(
|
|
|
|
(const uint8_t *)BOOTLOADER_START, BOOTLOADER_IMAGE_MAGIC,
|
|
|
|
flash_area_get_size(&BOOTLOADER_AREA));
|
|
|
|
|
|
|
|
if ((old_hdr != NULL) &&
|
|
|
|
(sectrue == check_image_header_sig(old_hdr, BOARDLOADER_KEY_M,
|
|
|
|
BOARDLOADER_KEY_N,
|
|
|
|
BOARDLOADER_KEYS)) &&
|
|
|
|
(sectrue ==
|
|
|
|
check_image_contents(old_hdr, IMAGE_HEADER_SIZE, &BOOTLOADER_AREA))) {
|
|
|
|
write_bootloader_min_version(old_hdr->monotonic);
|
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
if (check_sdcard()) {
|
|
|
|
return copy_sdcard() == sectrue ? 0 : 3;
|
|
|
|
}
|
2022-04-25 09:46:09 +00:00
|
|
|
#endif
|
2017-02-16 12:48:28 +00:00
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
const image_header *hdr = read_image_header(
|
|
|
|
(const uint8_t *)BOOTLOADER_START, BOOTLOADER_IMAGE_MAGIC,
|
|
|
|
flash_area_get_size(&BOOTLOADER_AREA));
|
2017-10-13 15:19:14 +00:00
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
ensure(hdr == (const image_header *)BOOTLOADER_START ? sectrue : secfalse,
|
2019-03-29 15:26:02 +00:00
|
|
|
"invalid bootloader header");
|
2017-10-13 15:19:14 +00:00
|
|
|
|
2022-11-21 13:12:49 +00:00
|
|
|
ensure(check_image_header_sig(hdr, BOARDLOADER_KEY_M, BOARDLOADER_KEY_N,
|
|
|
|
BOARDLOADER_KEYS),
|
|
|
|
"invalid bootloader signature");
|
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
ensure(check_image_contents(hdr, IMAGE_HEADER_SIZE, &BOOTLOADER_AREA),
|
2019-03-29 15:26:02 +00:00
|
|
|
"invalid bootloader hash");
|
2017-10-26 15:16:59 +00:00
|
|
|
|
2024-02-26 08:49:58 +00:00
|
|
|
uint8_t bld_min_version = get_bootloader_min_version();
|
|
|
|
ensure((hdr->monotonic >= bld_min_version) * sectrue,
|
|
|
|
"BOOTLOADER DOWNGRADED");
|
|
|
|
// Write the bootloader version to the secret area.
|
|
|
|
// This includes the version of bootloader potentially updated from SD card.
|
|
|
|
write_bootloader_min_version(hdr->monotonic);
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2024-09-04 09:17:11 +00:00
|
|
|
display_deinit(DISPLAY_JUMP_BEHAVIOR);
|
2024-06-24 15:00:51 +00:00
|
|
|
|
2022-10-26 17:22:18 +00:00
|
|
|
ensure_compatible_settings();
|
2022-08-16 14:51:10 +00:00
|
|
|
|
2024-09-07 11:50:10 +00:00
|
|
|
mpu_reconfig(MPU_MODE_DISABLED);
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2023-10-31 11:38:11 +00:00
|
|
|
// g_boot_command is preserved on STM32U5
|
2024-07-10 13:18:21 +00:00
|
|
|
jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE));
|
2016-07-20 16:33:49 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return 0;
|
2016-07-20 16:33:49 +00:00
|
|
|
}
|