From 7c800f183f5941732a4b78a8c73dec326800b578 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Tue, 20 Feb 2024 11:05:58 +0100 Subject: [PATCH] refactor(core): make translations blob handling HW agnostic [no changelog] --- core/SConscript.firmware | 1 + core/SConscript.unix | 2 +- .../{trezorhal/stm32f4 => lib}/translations.c | 19 ++++++++++++++++--- core/embed/{trezorhal => lib}/translations.h | 0 core/embed/trezorhal/unix/translations.c | 1 - core/site_scons/boards/stm32f4_common.py | 1 - 6 files changed, 18 insertions(+), 6 deletions(-) rename core/embed/{trezorhal/stm32f4 => lib}/translations.c (61%) rename core/embed/{trezorhal => lib}/translations.h (100%) delete mode 120000 core/embed/trezorhal/unix/translations.c diff --git a/core/SConscript.firmware b/core/SConscript.firmware index fa6d02157d..ebdeb4fbb6 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -197,6 +197,7 @@ SOURCE_MOD += [ 'embed/lib/image.c', 'embed/lib/mini_printf.c', 'embed/lib/terminal.c', + 'embed/lib/translations.c', 'embed/lib/unit_variant.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', diff --git a/core/SConscript.unix b/core/SConscript.unix index 3a93dbc744..f3d20cd66a 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -197,6 +197,7 @@ SOURCE_MOD += [ 'embed/lib/fonts/fonts.c', 'embed/lib/image.c', 'embed/lib/terminal.c', + 'embed/lib/translations.c', 'embed/lib/unit_variant.c', 'vendor/micropython/lib/uzlib/adler32.c', 'vendor/micropython/lib/uzlib/crc32.c', @@ -375,7 +376,6 @@ SOURCE_MICROPYTHON = [ SOURCE_UNIX = [ 'embed/trezorhal/unix/boot_args.c', - 'embed/trezorhal/unix/translations.c', 'embed/trezorhal/unix/common.c', 'embed/trezorhal/unix/display-unix.c', 'embed/trezorhal/unix/flash.c', diff --git a/core/embed/trezorhal/stm32f4/translations.c b/core/embed/lib/translations.c similarity index 61% rename from core/embed/trezorhal/stm32f4/translations.c rename to core/embed/lib/translations.c index d01debbf4b..108b318a62 100644 --- a/core/embed/trezorhal/stm32f4/translations.c +++ b/core/embed/lib/translations.c @@ -13,11 +13,24 @@ bool translations_write(const uint8_t* data, uint32_t offset, uint32_t len) { } ensure(flash_unlock_write(), "translations_write unlock"); - for (int i = 0; i < len; i++) { - // TODO optimize by writing by (quad)words - ensure(flash_area_write_byte(&TRANSLATIONS_AREA, offset + i, data[i]), + for (int i = 0; i < (len / FLASH_BLOCK_SIZE); i++) { + // todo consider alignment + ensure(flash_area_write_block(&TRANSLATIONS_AREA, + offset + (i * FLASH_BLOCK_SIZE), + (const uint32_t*)&data[i * FLASH_BLOCK_SIZE]), "translations_write write"); } + + if (len % FLASH_BLOCK_SIZE) { + flash_block_t block = {0}; + memset(block, 0xFF, FLASH_BLOCK_SIZE); + memcpy(block, data + (len / FLASH_BLOCK_SIZE) * FLASH_BLOCK_SIZE, + len % FLASH_BLOCK_SIZE); + ensure(flash_area_write_block( + &TRANSLATIONS_AREA, + offset + (len / FLASH_BLOCK_SIZE) * FLASH_BLOCK_SIZE, block), + "translations_write write rest"); + } ensure(flash_lock_write(), "translations_write lock"); return true; } diff --git a/core/embed/trezorhal/translations.h b/core/embed/lib/translations.h similarity index 100% rename from core/embed/trezorhal/translations.h rename to core/embed/lib/translations.h diff --git a/core/embed/trezorhal/unix/translations.c b/core/embed/trezorhal/unix/translations.c deleted file mode 120000 index 21b03004c7..0000000000 --- a/core/embed/trezorhal/unix/translations.c +++ /dev/null @@ -1 +0,0 @@ -../stm32f4/translations.c \ No newline at end of file diff --git a/core/site_scons/boards/stm32f4_common.py b/core/site_scons/boards/stm32f4_common.py index fff6d27962..c9d83588cf 100644 --- a/core/site_scons/boards/stm32f4_common.py +++ b/core/site_scons/boards/stm32f4_common.py @@ -53,7 +53,6 @@ def stm32f4_common_files(env, defines, sources, paths): "embed/trezorhal/stm32f4/random_delays.c", "embed/trezorhal/stm32f4/rng.c", "embed/trezorhal/stm32f4/vectortable.s", - "embed/trezorhal/stm32f4/translations.c", ] # boardloader needs separate assembler for some function unencumbered by various FW+bootloader hacks