diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index d5ff4a4993..34db7c35b1 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -105,9 +105,9 @@ static secbool copy_sdcard(void) // erase all flash (except boardloader) static const uint8_t sectors[] = { - 3, FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, + 3, FLASH_SECTOR_BOOTLOADER, FLASH_SECTOR_FIRMWARE_START, 7, @@ -168,12 +168,8 @@ int main(void) periph_init(); if (sectrue != flash_configure_option_bytes()) { - static const uint8_t sectors[] = { - FLASH_SECTOR_STORAGE_1, - FLASH_SECTOR_STORAGE_2, - }; // display is not initialized so don't call ensure - secbool r = flash_erase_sectors(sectors, sizeof(sectors), NULL); + secbool r = flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL); (void)r; return 2; } diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index a97607aa90..26e3285a77 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -262,7 +262,7 @@ main_start: firmware_present = load_image_header((const uint8_t *)(FIRMWARE_START + vhdr.hdrlen), FIRMWARE_IMAGE_MAGIC, FIRMWARE_IMAGE_MAXSIZE, vhdr.vsig_m, vhdr.vsig_n, vhdr.vpub, &hdr); } if (sectrue == firmware_present) { - firmware_present = check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, firmware_sectors, FIRMWARE_SECTORS_COUNT); + firmware_present = check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT); } // start the bootloader if no or broken firmware found ... @@ -286,11 +286,7 @@ main_start: ui_fadein(); // erase storage - static const uint8_t sectors_storage[] = { - FLASH_SECTOR_STORAGE_1, - FLASH_SECTOR_STORAGE_2, - }; - ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL); + ensure(flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL), NULL); // and start the usb loop if (bootloader_usb_loop(NULL, NULL) != sectrue) { @@ -353,7 +349,7 @@ main_start: "invalid firmware header"); ensure( - check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, firmware_sectors, FIRMWARE_SECTORS_COUNT), + check_image_contents(&hdr, IMAGE_HEADER_SIZE + vhdr.hdrlen, FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT), "invalid firmware hash"); // if all VTRUST flags are unset = ultimate trust => skip the procedure diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index 70cf066a19..faa0902811 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -39,22 +39,6 @@ #define MSG_HEADER1_LEN 9 #define MSG_HEADER2_LEN 1 -const uint8_t firmware_sectors[FIRMWARE_SECTORS_COUNT] = { - FLASH_SECTOR_FIRMWARE_START, - 7, - 8, - 9, - 10, - FLASH_SECTOR_FIRMWARE_END, - FLASH_SECTOR_FIRMWARE_EXTRA_START, - 18, - 19, - 20, - 21, - 22, - FLASH_SECTOR_FIRMWARE_EXTRA_END, -}; - secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size) { if (buf[0] != '?' || buf[1] != '#' || buf[2] != '#') { @@ -479,13 +463,9 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu // if firmware is not upgrade, erase storage if (sectrue != is_upgrade) { - static const uint8_t sectors_storage[] = { - FLASH_SECTOR_STORAGE_1, - FLASH_SECTOR_STORAGE_2, - }; - ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL); + ensure(flash_erase_sectors(STORAGE_SECTORS, STORAGE_SECTORS_COUNT, NULL), NULL); } - ensure(flash_erase_sectors(firmware_sectors, FIRMWARE_SECTORS_COUNT, ui_screen_install_progress_erase), NULL); + ensure(flash_erase_sectors(FIRMWARE_SECTORS, FIRMWARE_SECTORS_COUNT, ui_screen_install_progress_erase), NULL); firstskip = IMAGE_HEADER_SIZE + vhdr.hdrlen; } @@ -521,7 +501,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu const uint32_t * const src = (const uint32_t * const)chunk_buffer; for (int i = 0; i < chunk_size / sizeof(uint32_t); i++) { - ensure(flash_write_word(firmware_sectors[firmware_block], i * sizeof(uint32_t), src[i]), NULL); + ensure(flash_write_word(FIRMWARE_SECTORS[firmware_block], i * sizeof(uint32_t), src[i]), NULL); } ensure(flash_lock_write(), NULL); @@ -546,9 +526,9 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) { static const uint8_t sectors[] = { - 3, FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, + // 3, // skip because of MPU protection FLASH_SECTOR_FIRMWARE_START, 7, 8, @@ -558,7 +538,7 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) FLASH_SECTOR_UNUSED_START, 13, 14, - FLASH_SECTOR_UNUSED_END, + // FLASH_SECTOR_UNUSED_END, // skip because of MPU protection FLASH_SECTOR_FIRMWARE_EXTRA_START, 18, 19, diff --git a/embed/bootloader/messages.h b/embed/bootloader/messages.h index 1c0d514ee0..d509dd9e13 100644 --- a/embed/bootloader/messages.h +++ b/embed/bootloader/messages.h @@ -28,7 +28,6 @@ #define USB_PACKET_SIZE 64 #define FIRMWARE_UPLOAD_CHUNK_RETRY_COUNT 2 -extern const uint8_t firmware_sectors[FIRMWARE_SECTORS_COUNT]; secbool msg_parse_header(const uint8_t *buf, uint16_t *msg_id, uint32_t *msg_size); diff --git a/embed/trezorhal/flash.c b/embed/trezorhal/flash.c index b270b7cc56..b91c2c8f96 100644 --- a/embed/trezorhal/flash.c +++ b/embed/trezorhal/flash.c @@ -54,6 +54,27 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = { [24] = 0x08200000, // last element - not a valid sector }; +const uint8_t FIRMWARE_SECTORS [FIRMWARE_SECTORS_COUNT] = { + FLASH_SECTOR_FIRMWARE_START, + 7, + 8, + 9, + 10, + FLASH_SECTOR_FIRMWARE_END, + FLASH_SECTOR_FIRMWARE_EXTRA_START, + 18, + 19, + 20, + 21, + 22, + FLASH_SECTOR_FIRMWARE_EXTRA_END, +}; + +const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT] = { + FLASH_SECTOR_STORAGE_1, + FLASH_SECTOR_STORAGE_2, +}; + void flash_init(void) { } diff --git a/embed/trezorhal/flash.h b/embed/trezorhal/flash.h index dd707cf90f..285a1e76fc 100644 --- a/embed/trezorhal/flash.h +++ b/embed/trezorhal/flash.h @@ -60,6 +60,13 @@ // 22 #define FLASH_SECTOR_FIRMWARE_EXTRA_END 23 +#define BOOTLOADER_SECTORS_COUNT (1) +#define STORAGE_SECTORS_COUNT (2) +#define FIRMWARE_SECTORS_COUNT (6 + 7) + +extern const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT]; +extern const uint8_t FIRMWARE_SECTORS[FIRMWARE_SECTORS_COUNT]; + // note: FLASH_SR_RDERR is STM32F42xxx and STM32F43xxx specific (STM32F427) (reference RM0090 section 3.7.5) #ifndef STM32F427xx #define FLASH_SR_RDERR 0 diff --git a/embed/trezorhal/image.h b/embed/trezorhal/image.h index c9153bf97f..7eef845317 100644 --- a/embed/trezorhal/image.h +++ b/embed/trezorhal/image.h @@ -27,9 +27,6 @@ #define BOOTLOADER_START 0x08020000 #define FIRMWARE_START 0x08040000 -#define BOOTLOADER_SECTORS_COUNT (1) -#define FIRMWARE_SECTORS_COUNT (6 + 7) - #define IMAGE_HEADER_SIZE 0x400 #define IMAGE_SIG_SIZE 65 #define IMAGE_CHUNK_SIZE (128 * 1024) diff --git a/embed/unix/flash.c b/embed/unix/flash.c index 4e68a99538..8e4febe6ab 100644 --- a/embed/unix/flash.c +++ b/embed/unix/flash.c @@ -62,6 +62,27 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = { [24] = 0x08200000, // last element - not a valid sector }; +const uint8_t FIRMWARE_SECTORS[FIRMWARE_SECTORS_COUNT] = { + FLASH_SECTOR_FIRMWARE_START, + 7, + 8, + 9, + 10, + FLASH_SECTOR_FIRMWARE_END, + FLASH_SECTOR_FIRMWARE_EXTRA_START, + 18, + 19, + 20, + 21, + 22, + FLASH_SECTOR_FIRMWARE_EXTRA_END, +}; + +const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT] = { + FLASH_SECTOR_STORAGE_1, + FLASH_SECTOR_STORAGE_2, +}; + static uint8_t *FLASH_BUFFER; static uint32_t FLASH_SIZE;