From e268aeb8b7aa6b22a675cb1611d355627716692c Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sat, 9 Dec 2017 18:00:10 +0100 Subject: [PATCH] flash: simplify/unify flash_erase_sectors usage --- embed/boardloader/main.c | 6 +++--- embed/bootloader/messages.c | 17 +++++++---------- embed/extmod/modtrezorconfig/norcow.c | 6 +++--- embed/extmod/modtrezorconfig/storage.c | 2 +- embed/reflash/main.c | 6 ++---- embed/trezorhal/flash.h | 1 + 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index e2da0f15..bdce3a29 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -85,7 +85,7 @@ static secbool copy_sdcard(void) display_printf("\n\nerasing flash:\n\n"); // erase all flash (except boardloader) - uint8_t sectors[] = { + const uint8_t sectors[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, FLASH_SECTOR_BOOTLOADER, @@ -108,7 +108,7 @@ static secbool copy_sdcard(void) FLASH_SECTOR_FIRMWARE_EXTRA_END, FLASH_SECTOR_PIN_AREA, }; - if (sectrue != flash_erase_sectors(sectors, 2 + 1 + 6 + 4 + 7 + 1, progress_callback)) { + if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_callback)) { display_printf(" failed\n"); return secfalse; } @@ -161,7 +161,7 @@ int main(void) FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, }; - flash_erase_sectors(sectors, 2, NULL); + flash_erase_sectors(sectors, sizeof(sectors), NULL); return 2; } diff --git a/embed/bootloader/messages.c b/embed/bootloader/messages.c index a6bfbb0d..81df4840 100644 --- a/embed/bootloader/messages.c +++ b/embed/bootloader/messages.c @@ -247,7 +247,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu firmware_remaining = msg_recv.has_length ? msg_recv.length : 0; if (firmware_remaining > 0 && firmware_remaining % 4 == 0) { // erase flash - uint8_t sectors[] = { + const uint8_t sectors[] = { FLASH_SECTOR_FIRMWARE_START, 7, 8, @@ -262,7 +262,7 @@ void process_msg_FirmwareErase(uint8_t iface_num, uint32_t msg_size, uint8_t *bu 22, FLASH_SECTOR_FIRMWARE_EXTRA_END, }; - if (sectrue != flash_erase_sectors(sectors, 6 + 7, progress_erase)) { + if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), progress_erase)) { MSG_SEND_INIT(Failure); MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError); MSG_SEND_ASSIGN_STRING(message, "Could not erase flash"); @@ -383,15 +383,12 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size, uint8_t *bu } if (sectrue != compare_to_current_vendor_header(&vhdr)) { - uint8_t sectors_storage[] = { + const uint8_t sectors_storage[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, }; - ensure(flash_erase_sectors(sectors_storage, 2, NULL), NULL); - uint8_t sectors_pin[] = { - FLASH_SECTOR_PIN_AREA, - }; - ensure(flash_erase_sectors(sectors_pin, 2, NULL), NULL); + ensure(flash_erase_sectors(sectors_storage, sizeof(sectors_storage), NULL), NULL); + ensure(flash_erase_sector(FLASH_SECTOR_PIN_AREA), NULL); } firstskip = IMAGE_HEADER_SIZE + vhdr.hdrlen; @@ -451,7 +448,7 @@ static void progress_wipe(int pos, int len) int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) { - uint8_t sectors[] = { + const uint8_t sectors[] = { FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2, FLASH_SECTOR_FIRMWARE_START, @@ -473,7 +470,7 @@ int process_msg_WipeDevice(uint8_t iface_num, uint32_t msg_size, uint8_t *buf) FLASH_SECTOR_FIRMWARE_EXTRA_END, FLASH_SECTOR_PIN_AREA, }; - if (sectrue != flash_erase_sectors(sectors, 2 + 6 + 4 + 7 + 1, progress_wipe)) { + if (sectrue != flash_erase_sectors(sectors, sizeof(sectors), 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/extmod/modtrezorconfig/norcow.c b/embed/extmod/modtrezorconfig/norcow.c index c6dfc393..dab06297 100644 --- a/embed/extmod/modtrezorconfig/norcow.c +++ b/embed/extmod/modtrezorconfig/norcow.c @@ -6,14 +6,14 @@ #include "../../trezorhal/flash.h" #ifndef NORCOW_SECTORS -#define NORCOW_SECTORS {FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2} +#define NORCOW_SECTORS (const uint8_t []){FLASH_SECTOR_STORAGE_1, FLASH_SECTOR_STORAGE_2} #endif // NRCW = 4e524357 #define NORCOW_MAGIC ((uint32_t)0x5743524e) #define NORCOW_MAGIC_LEN (sizeof(uint32_t)) -static uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS; +static const uint8_t norcow_sectors[NORCOW_SECTOR_COUNT] = NORCOW_SECTORS; static uint8_t norcow_active_sector = 0; static uint32_t norcow_active_offset = NORCOW_MAGIC_LEN; @@ -70,7 +70,7 @@ static secbool norcow_write(uint8_t sector, uint32_t offset, uint32_t prefix, co static void norcow_erase(uint8_t sector, secbool set_magic) { ensure(sectrue * (sector <= NORCOW_SECTOR_COUNT), "invalid sector"); - ensure(flash_erase_sectors(&norcow_sectors[sector], 1, NULL), "erase failed"); + ensure(flash_erase_sector(norcow_sectors[sector]), "erase failed"); if (sectrue == set_magic) { ensure(norcow_write(sector, 0, NORCOW_MAGIC, NULL, 0), "set magic failed"); } diff --git a/embed/extmod/modtrezorconfig/storage.c b/embed/extmod/modtrezorconfig/storage.c index 9d5060ca..0b6d3017 100644 --- a/embed/extmod/modtrezorconfig/storage.c +++ b/embed/extmod/modtrezorconfig/storage.c @@ -41,7 +41,7 @@ static void pin_fails_reset(uint32_t ofs) // ofs points to the last word of the PIN fails area. Because there is // no space left, we recycle the sector (set all words to 0xffffffff). // On next unlock attempt, we start counting from the the first word. - flash_erase_sectors((uint8_t[]) { FLASH_SECTOR_PIN_AREA }, 1, NULL); + flash_erase_sector(FLASH_SECTOR_PIN_AREA); } else { // Mark this counter as exhausted. On next unlock attempt, pinfails_get // seeks to the next word. diff --git a/embed/reflash/main.c b/embed/reflash/main.c index 4cce1869..6f8800f6 100644 --- a/embed/reflash/main.c +++ b/embed/reflash/main.c @@ -67,16 +67,14 @@ int main(void) display_printf("updating boardloader + bootloader\n"); - uint8_t sectors[] = { + const uint8_t sectors[] = { FLASH_SECTOR_BOARDLOADER_START, 1, FLASH_SECTOR_BOARDLOADER_END, FLASH_SECTOR_BOOTLOADER, }; display_printf("erasing sectors"); - ensure( - flash_erase_sectors(sectors, sizeof(sectors), progress_callback), - "flash_erase_sectors"); + ensure(flash_erase_sectors(sectors, sizeof(sectors), progress_callback), "flash_erase_sectors"); display_printf("\n"); display_printf("erased\n"); diff --git a/embed/trezorhal/flash.h b/embed/trezorhal/flash.h index dd6c431d..b7168b24 100644 --- a/embed/trezorhal/flash.h +++ b/embed/trezorhal/flash.h @@ -51,6 +51,7 @@ secbool flash_lock(void); const void *flash_get_address(uint8_t sector, uint32_t offset, uint32_t size); secbool flash_erase_sectors(const uint8_t *sectors, int len, void (*progress)(int pos, int len)); +inline secbool flash_erase_sector(uint8_t sector) { return flash_erase_sectors(§or, 1, NULL); } secbool flash_write_byte(uint32_t address, uint8_t data); secbool flash_write_word(uint32_t address, uint32_t data); secbool flash_write_byte_rel(uint8_t sector, uint32_t offset, uint8_t data);