From 1da446a8fb0aae047cfa79ce2bc15cbae5b1229a Mon Sep 17 00:00:00 2001 From: Andrew Kozlik Date: Fri, 29 Apr 2022 05:42:05 +0200 Subject: [PATCH] refactor: Move flash_init to emulator main. --- core/embed/trezorhal/flash.c | 2 -- core/embed/unix/main.c | 4 ++++ legacy/flash.c | 2 -- legacy/flash.h | 2 -- storage/norcow.c | 1 - storage/tests/c/flash.c | 8 ++------ storage/tests/c/flash.h | 2 -- storage/tests/c0/flash.c | 8 ++------ storage/tests/c0/flash.h | 2 -- storage/tests/c0/norcow.c | 1 - 10 files changed, 8 insertions(+), 24 deletions(-) diff --git a/core/embed/trezorhal/flash.c b/core/embed/trezorhal/flash.c index 2f663262f..8f3192944 100644 --- a/core/embed/trezorhal/flash.c +++ b/core/embed/trezorhal/flash.c @@ -81,8 +81,6 @@ const uint8_t STORAGE_SECTORS[STORAGE_SECTORS_COUNT] = { FLASH_SECTOR_STORAGE_2, }; -void flash_init(void) {} - secbool flash_unlock_write(void) { HAL_FLASH_Unlock(); FLASH->SR |= FLASH_STATUS_ALL_FLAGS; // clear all status flags diff --git a/core/embed/unix/main.c b/core/embed/unix/main.c index 0ee321667..4c797cbea 100644 --- a/core/embed/unix/main.c +++ b/core/embed/unix/main.c @@ -39,6 +39,7 @@ #include "extmod/misc.h" #include "extmod/vfs_posix.h" +#include "flash.h" #include "genhdr/mpversion.h" #include "input.h" #include "py/builtin.h" @@ -479,6 +480,9 @@ MP_NOINLINE int main_(int argc, char **argv) { pre_process_options(argc, argv); + // Map trezor.flash to memory. + flash_init(); + #if MICROPY_ENABLE_GC char *heap = malloc(heap_size); gc_init(heap, heap + heap_size); diff --git a/legacy/flash.c b/legacy/flash.c index 839a2a2a4..14c6531ce 100644 --- a/legacy/flash.c +++ b/legacy/flash.c @@ -48,8 +48,6 @@ static secbool flash_check_success(uint32_t status) { : sectrue; } -void flash_init(void) {} - secbool flash_unlock_write(void) { svc_flash_unlock(); return sectrue; diff --git a/legacy/flash.h b/legacy/flash.h index 1b8657d07..819da411d 100644 --- a/legacy/flash.h +++ b/legacy/flash.h @@ -36,8 +36,6 @@ (FLASH_SR_RDERR | FLASH_SR_PGSERR | FLASH_SR_PGPERR | FLASH_SR_PGAERR | \ FLASH_SR_WRPERR | FLASH_SR_SOP | FLASH_SR_EOP) -void flash_init(void); - secbool __wur flash_unlock_write(void); secbool __wur flash_lock_write(void); diff --git a/storage/norcow.c b/storage/norcow.c index 2ead2d1ba..4d21ee61b 100644 --- a/storage/norcow.c +++ b/storage/norcow.c @@ -299,7 +299,6 @@ static void compact(void) { * Initializes storage */ void norcow_init(uint32_t *norcow_version) { - flash_init(); secbool found = secfalse; *norcow_version = 0; norcow_active_sector = 0; diff --git a/storage/tests/c/flash.c b/storage/tests/c/flash.c index ea1a60387..2054c23a2 100644 --- a/storage/tests/c/flash.c +++ b/storage/tests/c/flash.c @@ -51,14 +51,10 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = { [23] = 0x081E0000, // - 0x081FFFFF | 128 KiB [24] = 0x08200000, // last element - not a valid sector }; -const uint32_t FLASH_SIZE = 0x200000; +const uint32_t FLASH_SIZE = + FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]; uint8_t *FLASH_BUFFER = NULL; -void flash_init(void) { - assert(FLASH_SIZE == - FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]); -} - secbool flash_unlock_write(void) { return sectrue; } secbool flash_lock_write(void) { return sectrue; } diff --git a/storage/tests/c/flash.h b/storage/tests/c/flash.h index df35ae7f0..82508e477 100644 --- a/storage/tests/c/flash.h +++ b/storage/tests/c/flash.h @@ -26,8 +26,6 @@ #define FLASH_SECTOR_COUNT 24 -void flash_init(void); - secbool __wur flash_unlock_write(void); secbool __wur flash_lock_write(void); diff --git a/storage/tests/c0/flash.c b/storage/tests/c0/flash.c index d1feedb4c..f3a9de001 100644 --- a/storage/tests/c0/flash.c +++ b/storage/tests/c0/flash.c @@ -52,14 +52,10 @@ static const uint32_t FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT + 1] = { [24] = 0x08200000, // last element - not a valid sector }; -const uint32_t FLASH_SIZE = 0x200000; +const uint32_t FLASH_SIZE = + FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]; uint8_t *FLASH_BUFFER = NULL; -void flash_init(void) { - assert(FLASH_SIZE == - FLASH_SECTOR_TABLE[FLASH_SECTOR_COUNT] - FLASH_SECTOR_TABLE[0]); -} - secbool flash_unlock(void) { return sectrue; } secbool flash_lock(void) { return sectrue; } diff --git a/storage/tests/c0/flash.h b/storage/tests/c0/flash.h index a098fd56a..1d83b53bb 100644 --- a/storage/tests/c0/flash.h +++ b/storage/tests/c0/flash.h @@ -26,8 +26,6 @@ #define FLASH_SECTOR_COUNT 24 -void flash_init(void); - secbool __wur flash_unlock(void); secbool __wur flash_lock(void); diff --git a/storage/tests/c0/norcow.c b/storage/tests/c0/norcow.c index 1f65412ea..1081391a8 100644 --- a/storage/tests/c0/norcow.c +++ b/storage/tests/c0/norcow.c @@ -220,7 +220,6 @@ static void compact() { * Initializes storage */ void norcow_init(void) { - flash_init(); secbool found = secfalse; // detect active sector - starts with magic for (uint8_t i = 0; i < NORCOW_SECTOR_COUNT; i++) {