diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index 00788c5f9..de4710ea9 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -75,7 +75,6 @@ static bool copy_sdcard(void) uint8_t sectors[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, - FLASH_SECTOR_PIN_AREA, FLASH_SECTOR_BOOTLOADER, FLASH_SECTOR_FIRMWARE_START, 7, @@ -94,8 +93,9 @@ static bool copy_sdcard(void) 21, 22, FLASH_SECTOR_FIRMWARE_EXTRA_END, + FLASH_SECTOR_PIN_AREA, }; - if (!flash_erase_sectors(sectors, 2 + 1 + 1 + 6 + 4 + 7, progress_callback)) { + if (!flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7 + 1, progress_callback)) { display_printf(" failed\n"); return false; } @@ -153,7 +153,10 @@ int main(void) #if PRODUCTION flash_set_option_bytes(); if (!flash_check_option_bytes()) { - uint8_t sectors[] = {FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2}; + uint8_t sectors[] = { + FLASH_SECTOR_STORAGE_1, + FLASH_SECTOR_STORAGE_2, + }; flash_erase_sectors(sectors, 2, NULL); ensure(0, "wrong option bytes"); } diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index eac92e8d8..a38e802df 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -356,7 +356,6 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) uint8_t sectors[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, - FLASH_SECTOR_PIN_AREA, FLASH_SECTOR_FIRMWARE_START, 7, 8, @@ -374,8 +373,9 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) 21, 22, FLASH_SECTOR_FIRMWARE_EXTRA_END, + FLASH_SECTOR_PIN_AREA, }; - if (!flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7, progress_wipe)) { + if (!flash_erase_sectors(sectors, 2 + 6 + 4 + 7 + 1, progress_wipe)) { MSG_SEND_INIT(Failure); MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError); MSG_SEND_ASSIGN_STRING(message, "Could not erase flash"); diff --git a/embed/trezorhal/flash.c b/embed/trezorhal/flash.c index ccd890a46..8c008dfea 100644 --- a/embed/trezorhal/flash.c +++ b/embed/trezorhal/flash.c @@ -3,6 +3,38 @@ #include #include "flash.h" +// see docs/memory.md for more information + +#define SECTOR_COUNT 24 + +static const uint32_t SECTOR_TABLE[SECTOR_COUNT + 1] = { + [ 0] = 0x08000000, // - 0x08003FFF | 16 KiB + [ 1] = 0x08004000, // - 0x08007FFF | 16 KiB + [ 2] = 0x08008000, // - 0x0800BFFF | 16 KiB + [ 3] = 0x0800C000, // - 0x0800FFFF | 16 KiB + [ 4] = 0x08010000, // - 0x0801FFFF | 64 KiB + [ 5] = 0x08020000, // - 0x0803FFFF | 128 KiB + [ 6] = 0x08040000, // - 0x0805FFFF | 128 KiB + [ 7] = 0x08060000, // - 0x0807FFFF | 128 KiB + [ 8] = 0x08080000, // - 0x0809FFFF | 128 KiB + [ 9] = 0x080A0000, // - 0x080BFFFF | 128 KiB + [10] = 0x080C0000, // - 0x080DFFFF | 128 KiB + [11] = 0x080E0000, // - 0x080FFFFF | 128 KiB + [12] = 0x08100000, // - 0x08103FFF | 16 KiB + [13] = 0x08104000, // - 0x08107FFF | 16 KiB + [14] = 0x08108000, // - 0x0810BFFF | 16 KiB + [15] = 0x0810C000, // - 0x0810FFFF | 16 KiB + [16] = 0x08110000, // - 0x0811FFFF | 64 KiB + [17] = 0x08120000, // - 0x0813FFFF | 128 KiB + [18] = 0x08140000, // - 0x0815FFFF | 128 KiB + [19] = 0x08160000, // - 0x0817FFFF | 128 KiB + [20] = 0x08180000, // - 0x0819FFFF | 128 KiB + [21] = 0x081A0000, // - 0x081BFFFF | 128 KiB + [22] = 0x081C0000, // - 0x081DFFFF | 128 KiB + [23] = 0x081E0000, // - 0x081FFFFF | 128 KiB + [24] = 0x08200000, // last element - not a valid sector +}; + int flash_init(void) { return 0; @@ -40,6 +72,13 @@ bool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int p flash_lock(); return false; } + // check whether the sector was really deleted (contains only 0xFF) + uint32_t addr_start = SECTOR_TABLE[sectors[i]], addr_end = SECTOR_TABLE[sectors[i] + 1]; + for (uint32_t addr = addr_start; addr < addr_end; addr += 4) { + if (*((const uint32_t *)addr) != 0xFFFFFFFF) { + return false; + } + } if (progress) { progress(i + 1, len); } diff --git a/embed/trezorhal/flash.h b/embed/trezorhal/flash.h index ed40c24c6..84086ff12 100644 --- a/embed/trezorhal/flash.h +++ b/embed/trezorhal/flash.h @@ -38,7 +38,6 @@ // 22 // 23 #define FLASH_SECTOR_FIRMWARE_EXTRA_END 23 -#define FLASH_SECTOR_LAST 23 int flash_init(void);