From 60597275d8f9cb4e891ac795091b955421a86950 Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 3 Oct 2024 10:27:40 +0200 Subject: [PATCH 1/7] refactor(core): remove set_core_clock from emulator [no changelog] --- core/embed/bootloader/emulator.c | 2 -- core/embed/bootloader/emulator.h | 2 -- core/embed/bootloader/main.c | 4 ---- core/embed/kernel/main.c | 4 ---- core/embed/trezorhal/stm32f4/platform.h | 2 -- core/embed/trezorhal/stm32f4/system.c | 6 ++++++ core/embed/trezorhal/stm32u5/platform.h | 8 -------- 7 files changed, 6 insertions(+), 22 deletions(-) diff --git a/core/embed/bootloader/emulator.c b/core/embed/bootloader/emulator.c index a2541419ab0..f0b09f83020 100644 --- a/core/embed/bootloader/emulator.c +++ b/core/embed/bootloader/emulator.c @@ -20,8 +20,6 @@ uint8_t *FIRMWARE_START = 0; -void set_core_clock(int) {} - // used in fw emulator to raise python exception on exit void __attribute__((noreturn)) main_clean_exit() { exit(3); } diff --git a/core/embed/bootloader/emulator.h b/core/embed/bootloader/emulator.h index 7cd1adb01a7..0da76966625 100644 --- a/core/embed/bootloader/emulator.h +++ b/core/embed/bootloader/emulator.h @@ -1,7 +1,6 @@ #ifndef __EMULATOR_H__ #define __EMULATOR_H__ -#define CLOCK_180_MHZ 0 #define mini_snprintf snprintf #undef FIRMWARE_START @@ -12,7 +11,6 @@ extern uint8_t *FIRMWARE_START; void emulator_poll_events(void); -void set_core_clock(int); __attribute__((noreturn)) void jump_to(uint32_t address); #endif diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index bc34682af1a..3590f1b3525 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -361,10 +361,6 @@ int bootloader_main(void) { rdi_init(); -#if defined TREZOR_MODEL_T - set_core_clock(CLOCK_180_MHZ); -#endif - #ifdef USE_HASH_PROCESSOR hash_processor_init(); #endif diff --git a/core/embed/kernel/main.c b/core/embed/kernel/main.c index 77a361ca23b..02b6e4c3c51 100644 --- a/core/embed/kernel/main.c +++ b/core/embed/kernel/main.c @@ -69,10 +69,6 @@ static void optiga_log_hex(const char *prefix, const uint8_t *data, #endif void drivers_init() { -#if defined TREZOR_MODEL_T - set_core_clock(CLOCK_180_MHZ); -#endif - #ifdef STM32U5 tamper_init(); #endif diff --git a/core/embed/trezorhal/stm32f4/platform.h b/core/embed/trezorhal/stm32f4/platform.h index c7314654463..13542a9960c 100644 --- a/core/embed/trezorhal/stm32f4/platform.h +++ b/core/embed/trezorhal/stm32f4/platform.h @@ -37,6 +37,4 @@ void jump_to(uint32_t address); void jump_to_with_flag(uint32_t address, uint32_t register_flag); void clear_otg_hs_memory(void); -extern uint32_t __stack_chk_guard; - #endif // TREZORHAL_STM32_H diff --git a/core/embed/trezorhal/stm32f4/system.c b/core/embed/trezorhal/stm32f4/system.c index abe59f11259..c206f59de19 100644 --- a/core/embed/trezorhal/stm32f4/system.c +++ b/core/embed/trezorhal/stm32f4/system.c @@ -23,6 +23,7 @@ #include "bootutils.h" #include "mpu.h" +#include "platform.h" #include "systask.h" #include "system.h" #include "systick.h" @@ -35,6 +36,11 @@ #ifdef KERNEL_MODE void system_init(systask_error_handler_t error_handler) { +#if defined(TREZOR_MODEL_T) && (!defined(BOARDLOADER)) + // Early boardloader versions on Model T initialized the CPU clock to 168MHz. + // We need to set it to the STM32F429's maximum - 180MHz. + set_core_clock(CLOCK_180_MHZ); +#endif mpu_init(); mpu_reconfig(MPU_MODE_DEFAULT); systask_scheduler_init(error_handler); diff --git a/core/embed/trezorhal/stm32u5/platform.h b/core/embed/trezorhal/stm32u5/platform.h index d5c9ffb6e51..0b24be25d78 100644 --- a/core/embed/trezorhal/stm32u5/platform.h +++ b/core/embed/trezorhal/stm32u5/platform.h @@ -29,19 +29,11 @@ #define FLASH_BURST_WORDS (8 * FLASH_QUADWORD_WORDS) #define FLASH_BURST_SIZE (FLASH_BURST_WORDS * sizeof(uint32_t)) -typedef enum { - CLOCK_160_MHZ = 0, -} clock_settings_t; - -void set_core_clock(clock_settings_t settings); - // the following functions are defined in util.s void memset_reg(volatile void *start, volatile void *stop, uint32_t val); void jump_to(uint32_t address); void jump_to_with_flag(uint32_t address, uint32_t register_flag); -extern uint32_t __stack_chk_guard; - void check_oem_keys(void); #endif // TREZORHAL_STM32_H From 638a72a3a27acfb75c1e6b2eb5383690daac311f Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 3 Oct 2024 10:55:56 +0200 Subject: [PATCH 2/7] refactor(core): fix platform header includes [no changelog] --- core/embed/boardloader/main.c | 1 + core/embed/kernel/main.c | 1 + core/embed/trezorhal/common.h | 1 - core/embed/trezorhal/flash.h | 11 ++++++++++- core/embed/trezorhal/stm32f4/bootutils.c | 1 + core/embed/trezorhal/stm32f4/optiga_hal.c | 1 + core/embed/trezorhal/stm32f4/systick.c | 1 - core/embed/trezorhal/stm32f4/systimer.c | 1 - .../stm32f4/xdisplay/st-7789/display_internal.h | 2 ++ core/embed/trezorhal/stm32u5/common.c | 1 - core/embed/trezorhal/stm32u5/platform.h | 7 ------- core/embed/trezorhal/stm32u5/secret.c | 2 ++ core/embed/trezorhal/unix/button.c | 1 - core/embed/trezorhal/unix/touch.c | 1 - 14 files changed, 18 insertions(+), 14 deletions(-) diff --git a/core/embed/boardloader/main.c b/core/embed/boardloader/main.c index c1d7c836d97..adbea6734d6 100644 --- a/core/embed/boardloader/main.c +++ b/core/embed/boardloader/main.c @@ -32,6 +32,7 @@ #include "image.h" #include "model.h" #include "mpu.h" +#include "platform.h" #include "rng.h" #include "rsod.h" #include "secret.h" diff --git a/core/embed/kernel/main.c b/core/embed/kernel/main.c index 02b6e4c3c51..b642e694949 100644 --- a/core/embed/kernel/main.c +++ b/core/embed/kernel/main.c @@ -37,6 +37,7 @@ #include "mpu.h" #include "optiga_commands.h" #include "optiga_transport.h" +#include "platform.h" #include "random_delays.h" #include "rsod.h" #include "sdcard.h" diff --git a/core/embed/trezorhal/common.h b/core/embed/trezorhal/common.h index 3c92580c84a..c231f82a0c5 100644 --- a/core/embed/trezorhal/common.h +++ b/core/embed/trezorhal/common.h @@ -25,7 +25,6 @@ #include "secbool.h" #include "error_handling.h" -#include "platform.h" #include "systick.h" #ifndef MIN_8bits diff --git a/core/embed/trezorhal/flash.h b/core/embed/trezorhal/flash.h index ae0169af788..e753ded8bac 100644 --- a/core/embed/trezorhal/flash.h +++ b/core/embed/trezorhal/flash.h @@ -24,9 +24,18 @@ #include #include "flash_ll.h" -#include "platform.h" #include "secbool.h" +#ifdef STM32U5 + +#define FLASH_QUADWORD_WORDS (4) +#define FLASH_QUADWORD_SIZE (FLASH_QUADWORD_WORDS * sizeof(uint32_t)) + +#define FLASH_BURST_WORDS (8 * FLASH_QUADWORD_WORDS) +#define FLASH_BURST_SIZE (FLASH_BURST_WORDS * sizeof(uint32_t)) + +#endif + void flash_init(void); #endif // TREZORHAL_FLASH_H diff --git a/core/embed/trezorhal/stm32f4/bootutils.c b/core/embed/trezorhal/stm32f4/bootutils.c index cfb4d1a81f0..0bd724a64d1 100644 --- a/core/embed/trezorhal/stm32f4/bootutils.c +++ b/core/embed/trezorhal/stm32f4/bootutils.c @@ -27,6 +27,7 @@ #include "irq.h" #include "model.h" #include "mpu.h" +#include "platform.h" #ifdef KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/optiga_hal.c b/core/embed/trezorhal/stm32f4/optiga_hal.c index b3d659fcf13..b1d60acfa53 100644 --- a/core/embed/trezorhal/stm32f4/optiga_hal.c +++ b/core/embed/trezorhal/stm32f4/optiga_hal.c @@ -1,6 +1,7 @@ #include "optiga_hal.h" #include "common.h" #include TREZOR_BOARD +#include STM32_HAL_H #ifdef KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/systick.c b/core/embed/trezorhal/stm32f4/systick.c index 5bfbe2cac29..359f3e27246 100644 --- a/core/embed/trezorhal/stm32f4/systick.c +++ b/core/embed/trezorhal/stm32f4/systick.c @@ -22,7 +22,6 @@ #include "irq.h" #include "mpu.h" -#include "platform.h" #include "systemview.h" #include "systick.h" diff --git a/core/embed/trezorhal/stm32f4/systimer.c b/core/embed/trezorhal/stm32f4/systimer.c index 10736a5b771..278958af823 100644 --- a/core/embed/trezorhal/stm32f4/systimer.c +++ b/core/embed/trezorhal/stm32f4/systimer.c @@ -21,7 +21,6 @@ #include #include "irq.h" -#include "platform.h" #include "systick_internal.h" #include "systimer.h" diff --git a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_internal.h b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_internal.h index 4bcad64c8c4..7185a5c1261 100644 --- a/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_internal.h +++ b/core/embed/trezorhal/stm32f4/xdisplay/st-7789/display_internal.h @@ -1,6 +1,8 @@ #ifndef TREZORHAL_DISPLAY_INTERNAL_H #define TREZORHAL_DISPLAY_INTERNAL_H +#include STM32_HAL_H + #include #ifdef XFRAMEBUFFER diff --git a/core/embed/trezorhal/stm32u5/common.c b/core/embed/trezorhal/stm32u5/common.c index 5efbb408874..15cd44460d4 100644 --- a/core/embed/trezorhal/stm32u5/common.c +++ b/core/embed/trezorhal/stm32u5/common.c @@ -26,7 +26,6 @@ #include "error_handling.h" #include "flash_otp.h" #include "model.h" -#include "platform.h" #include "rand.h" #include "secret.h" diff --git a/core/embed/trezorhal/stm32u5/platform.h b/core/embed/trezorhal/stm32u5/platform.h index 0b24be25d78..ff990e12c2c 100644 --- a/core/embed/trezorhal/stm32u5/platform.h +++ b/core/embed/trezorhal/stm32u5/platform.h @@ -20,15 +20,8 @@ #ifndef TREZORHAL_STM32_H #define TREZORHAL_STM32_H -#include STM32_HAL_H #include -#define FLASH_QUADWORD_WORDS (4) -#define FLASH_QUADWORD_SIZE (FLASH_QUADWORD_WORDS * sizeof(uint32_t)) - -#define FLASH_BURST_WORDS (8 * FLASH_QUADWORD_WORDS) -#define FLASH_BURST_SIZE (FLASH_BURST_WORDS * sizeof(uint32_t)) - // the following functions are defined in util.s void memset_reg(volatile void *start, volatile void *stop, uint32_t val); void jump_to(uint32_t address); diff --git a/core/embed/trezorhal/stm32u5/secret.c b/core/embed/trezorhal/stm32u5/secret.c index 69a060c7cc9..c9b22cfe4ec 100644 --- a/core/embed/trezorhal/stm32u5/secret.c +++ b/core/embed/trezorhal/stm32u5/secret.c @@ -1,3 +1,5 @@ +#include STM32_HAL_H + #include "secret.h" #include #include diff --git a/core/embed/trezorhal/unix/button.c b/core/embed/trezorhal/unix/button.c index 3ff9cd4b6fa..9ab6731e0ae 100644 --- a/core/embed/trezorhal/unix/button.c +++ b/core/embed/trezorhal/unix/button.c @@ -23,7 +23,6 @@ #include "button.h" #include "common.h" -#include "platform.h" static char last_left = 0, last_right = 0; diff --git a/core/embed/trezorhal/unix/touch.c b/core/embed/trezorhal/unix/touch.c index d2aa918e1f4..6c4f34f5c31 100644 --- a/core/embed/trezorhal/unix/touch.c +++ b/core/embed/trezorhal/unix/touch.c @@ -24,7 +24,6 @@ #include #include "common.h" -#include "platform.h" #include "touch.h" extern int sdl_display_res_x, sdl_display_res_y; From cefecd7eed8a4f8a14cdc78ea2d7697c3d091f73 Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 3 Oct 2024 13:23:56 +0200 Subject: [PATCH 3/7] refactor(emulator): improve SDL event polling logic [no changelog] --- core/SConscript.bootloader_emu | 1 - core/SConscript.unix | 1 - core/embed/bootloader/emulator.c | 29 +++++++++- core/embed/bootloader/emulator.h | 1 - core/embed/bootloader/main.c | 5 +- .../extmod/modtrezorio/modtrezorio-poll.h | 9 ++- core/embed/trezorhal/unix/button.c | 1 - core/embed/trezorhal/unix/common.c | 58 ------------------- core/embed/trezorhal/unix/platform.h | 2 - core/embed/trezorhal/unix/touch.c | 2 - core/embed/unix/main.c | 28 ++++++++- 11 files changed, 65 insertions(+), 72 deletions(-) delete mode 100644 core/embed/trezorhal/unix/common.c delete mode 100644 core/embed/trezorhal/unix/platform.h diff --git a/core/SConscript.bootloader_emu b/core/SConscript.bootloader_emu index 366975557a8..df96b15871a 100644 --- a/core/SConscript.bootloader_emu +++ b/core/SConscript.bootloader_emu @@ -120,7 +120,6 @@ SOURCE_BOOTLOADER = [ SOURCE_TREZORHAL = [ 'embed/trezorhal/unix/bootutils.c', - 'embed/trezorhal/unix/common.c', 'embed/trezorhal/unix/flash.c', 'embed/trezorhal/unix/flash_otp.c', 'embed/trezorhal/unix/mpu.c', diff --git a/core/SConscript.unix b/core/SConscript.unix index 4474eb502ae..50d366478cf 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -389,7 +389,6 @@ SOURCE_MICROPYTHON = [ SOURCE_UNIX = [ 'embed/trezorhal/unix/bootutils.c', - 'embed/trezorhal/unix/common.c', 'embed/trezorhal/unix/entropy.c', 'embed/trezorhal/unix/flash_otp.c', 'embed/trezorhal/unix/flash.c', diff --git a/core/embed/bootloader/emulator.c b/core/embed/bootloader/emulator.c index f0b09f83020..b0fec6514e3 100644 --- a/core/embed/bootloader/emulator.c +++ b/core/embed/bootloader/emulator.c @@ -1,6 +1,8 @@ #include #include +#include + #include TREZOR_BOARD #include "bootargs.h" #include "bootui.h" @@ -20,9 +22,6 @@ uint8_t *FIRMWARE_START = 0; -// used in fw emulator to raise python exception on exit -void __attribute__((noreturn)) main_clean_exit() { exit(3); } - int bootloader_main(void); // assuming storage is single subarea @@ -89,7 +88,31 @@ bool load_firmware(const char *filename, uint8_t *hash) { return true; } +static int sdl_event_filter(void *userdata, SDL_Event *event) { + switch (event->type) { + case SDL_QUIT: + exit(3); + return 0; + case SDL_KEYUP: + if (event->key.repeat) { + return 0; + } + switch (event->key.keysym.sym) { + case SDLK_ESCAPE: + exit(3); + return 0; + case SDLK_p: + display_save("emu"); + return 0; + } + break; + } + return 1; +} + __attribute__((noreturn)) int main(int argc, char **argv) { + SDL_SetEventFilter(sdl_event_filter, NULL); + display_init(DISPLAY_RESET_CONTENT); flash_init(); flash_otp_init(); diff --git a/core/embed/bootloader/emulator.h b/core/embed/bootloader/emulator.h index 0da76966625..2ecd41f72ab 100644 --- a/core/embed/bootloader/emulator.h +++ b/core/embed/bootloader/emulator.h @@ -10,7 +10,6 @@ extern uint8_t *FIRMWARE_START; -void emulator_poll_events(void); __attribute__((noreturn)) void jump_to(uint32_t address); #endif diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index 3590f1b3525..fe152bba7bc 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -79,6 +79,7 @@ #include "version_check.h" #ifdef TREZOR_EMULATOR +#include "SDL.h" #include "emulator.h" #else #include "compiler_traits.h" @@ -149,7 +150,9 @@ static usb_result_t bootloader_usb_loop(const vendor_header *const vhdr, for (;;) { #ifdef TREZOR_EMULATOR - emulator_poll_events(); + // Ensures that SDL events are processed. This prevents the emulator from + // freezing when the user interacts with the window. + SDL_PumpEvents(); #endif int r = usb_webusb_read_blocking(USB_IFACE_NUM, buf, USB_PACKET_SIZE, USB_TIMEOUT); diff --git a/core/embed/extmod/modtrezorio/modtrezorio-poll.h b/core/embed/extmod/modtrezorio/modtrezorio-poll.h index a5f1bc89c26..c07904e5600 100644 --- a/core/embed/extmod/modtrezorio/modtrezorio-poll.h +++ b/core/embed/extmod/modtrezorio/modtrezorio-poll.h @@ -24,6 +24,10 @@ #include "display.h" #include "embed/extmod/trezorobj.h" +#ifdef TREZOR_EMULATOR +#include "SDL.h" +#endif + #define USB_DATA_IFACE (253) #define BUTTON_IFACE (254) #define TOUCH_IFACE (255) @@ -78,7 +82,10 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref, const mp_uint_t mode = i & 0xFF00; #if defined TREZOR_EMULATOR - emulator_poll_events(); + // Ensures that SDL events are processed even if the ifaces list + // contains only USB interfaces. This prevents the emulator from + // freezing when the user interacts with the window. + SDL_PumpEvents(); #endif if (false) { diff --git a/core/embed/trezorhal/unix/button.c b/core/embed/trezorhal/unix/button.c index 9ab6731e0ae..01a096ff261 100644 --- a/core/embed/trezorhal/unix/button.c +++ b/core/embed/trezorhal/unix/button.c @@ -32,7 +32,6 @@ char button_state_right(void) { return last_right; } uint32_t button_read(void) { SDL_Event event; - SDL_PumpEvents(); if (SDL_PollEvent(&event) > 0) { switch (event.type) { case SDL_KEYDOWN: diff --git a/core/embed/trezorhal/unix/common.c b/core/embed/trezorhal/unix/common.c deleted file mode 100644 index de06ef6deeb..00000000000 --- a/core/embed/trezorhal/unix/common.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "display.h" -#include "memzero.h" - -void __attribute__((noreturn)) main_clean_exit(); - -static int SDLCALL emulator_event_filter(void *userdata, SDL_Event *event) { - switch (event->type) { - case SDL_QUIT: - main_clean_exit(); - return 0; - case SDL_KEYUP: - if (event->key.repeat) { - return 0; - } - switch (event->key.keysym.sym) { - case SDLK_ESCAPE: - main_clean_exit(); - return 0; - case SDLK_p: - display_save("emu"); - return 0; - } - break; - } - return 1; -} - -void emulator_poll_events(void) { - SDL_PumpEvents(); - SDL_FilterEvents(emulator_event_filter, NULL); -} diff --git a/core/embed/trezorhal/unix/platform.h b/core/embed/trezorhal/unix/platform.h deleted file mode 100644 index 0cab1411c1c..00000000000 --- a/core/embed/trezorhal/unix/platform.h +++ /dev/null @@ -1,2 +0,0 @@ - -void emulator_poll_events(void); diff --git a/core/embed/trezorhal/unix/touch.c b/core/embed/trezorhal/unix/touch.c index 6c4f34f5c31..5b8e24c8d93 100644 --- a/core/embed/trezorhal/unix/touch.c +++ b/core/embed/trezorhal/unix/touch.c @@ -251,9 +251,7 @@ uint32_t touch_get_event(void) { return TOUCH_END | touch_pack_xy(driver->last_x, driver->last_y); } - emulator_poll_events(); SDL_Event event; - SDL_PumpEvents(); int ev_x = 0; int ev_y = 0; diff --git a/core/embed/unix/main.c b/core/embed/unix/main.c index 9241ee58e24..655e91d0fe6 100644 --- a/core/embed/unix/main.c +++ b/core/embed/unix/main.c @@ -24,6 +24,8 @@ * THE SOFTWARE. */ +#include + #include #include #include @@ -412,7 +414,7 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) { // Inject SystemExit exception. This is primarily needed by prof.py to run the // atexit() handler. -void __attribute__((noreturn)) main_clean_exit() { +static void __attribute__((noreturn)) main_clean_exit() { const int status = 3; fflush(stdout); fflush(stderr); @@ -469,6 +471,28 @@ static int do_import_module(const char *modname) { return 0; } +static int sdl_event_filter(void *userdata, SDL_Event *event) { + switch (event->type) { + case SDL_QUIT: + main_clean_exit(); + return 0; + case SDL_KEYUP: + if (event->key.repeat) { + return 0; + } + switch (event->key.keysym.sym) { + case SDLK_ESCAPE: + main_clean_exit(); + return 0; + case SDLK_p: + display_save("emu"); + return 0; + } + break; + } + return 1; +} + MP_NOINLINE int main_(int argc, char **argv) { #ifdef SIGPIPE // Do not raise SIGPIPE, instead return EPIPE. Otherwise, e.g. writing @@ -491,6 +515,8 @@ MP_NOINLINE int main_(int argc, char **argv) { systick_init(); systimer_init(); + SDL_SetEventFilter(sdl_event_filter, NULL); + display_init(DISPLAY_RESET_CONTENT); #if USE_TOUCH From b6df205fd8d9a60d87744967acdb6925fc38f010 Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 3 Oct 2024 17:23:20 +0200 Subject: [PATCH 4/7] refactor(core): remove common.c on all platforms [no changelog] --- core/embed/boardloader/main.c | 4 -- core/embed/trezorhal/stm32f4/common.c | 58 ------------------- core/embed/trezorhal/stm32f4/platform.c | 19 ++++++ core/embed/trezorhal/stm32f4/platform.h | 1 - .../embed/trezorhal/stm32f4/startup_stage_0.s | 7 ++- core/embed/trezorhal/stm32u5/common.c | 32 ---------- core/site_scons/models/stm32f4_common.py | 1 - core/site_scons/models/stm32u5_common.py | 1 - 8 files changed, 25 insertions(+), 98 deletions(-) delete mode 100644 core/embed/trezorhal/stm32f4/common.c delete mode 100644 core/embed/trezorhal/stm32u5/common.c diff --git a/core/embed/boardloader/main.c b/core/embed/boardloader/main.c index adbea6734d6..18054329b0d 100644 --- a/core/embed/boardloader/main.c +++ b/core/embed/boardloader/main.c @@ -258,10 +258,6 @@ int main(void) { secret_init(); -#ifdef STM32F4 - clear_otg_hs_memory(); -#endif - #ifdef USE_SDRAM sdram_init(); #endif diff --git a/core/embed/trezorhal/stm32f4/common.c b/core/embed/trezorhal/stm32f4/common.c deleted file mode 100644 index 7ac11fe6acb..00000000000 --- a/core/embed/trezorhal/stm32f4/common.c +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include STM32_HAL_H - -#include - -#include "common.h" -#include "display.h" -#include "error_handling.h" -#include "model.h" - -#include "flash_otp.h" -#include "platform.h" -#include "rand.h" - -#include "stm32f4xx_ll_utils.h" - -#ifdef TREZOR_MODEL_T -#include "backlight_pwm.h" -#endif - -#ifdef KERNEL_MODE - -// reference RM0090 section 35.12.1 Figure 413 -#define USB_OTG_HS_DATA_FIFO_RAM (USB_OTG_HS_PERIPH_BASE + 0x20000U) -#define USB_OTG_HS_DATA_FIFO_SIZE (4096U) - -void clear_otg_hs_memory(void) { - // use the HAL version due to section 2.1.6 of STM32F42xx Errata sheet - __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); // enable USB_OTG_HS peripheral clock so - // that the peripheral memory is - // accessible - memset_reg( - (volatile void *)USB_OTG_HS_DATA_FIFO_RAM, - (volatile void *)(USB_OTG_HS_DATA_FIFO_RAM + USB_OTG_HS_DATA_FIFO_SIZE), - 0); - __HAL_RCC_USB_OTG_HS_CLK_DISABLE(); // disable USB OTG_HS peripheral clock as - // the peripheral is not needed right now -} - -#endif // KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/platform.c b/core/embed/trezorhal/stm32f4/platform.c index 8d801d507fd..0719b8793a5 100644 --- a/core/embed/trezorhal/stm32f4/platform.c +++ b/core/embed/trezorhal/stm32f4/platform.c @@ -207,4 +207,23 @@ void set_core_clock(clock_settings_t settings) { } #endif +// reference RM0090 section 35.12.1 Figure 413 +#define USB_OTG_HS_DATA_FIFO_RAM (USB_OTG_HS_PERIPH_BASE + 0x20000U) +#define USB_OTG_HS_DATA_FIFO_SIZE (4096U) + +// Clears USB FIFO memory to prevent data leakage of sensitive information +__attribute((used)) void clear_otg_hs_memory(void) { + // use the HAL version due to section 2.1.6 of STM32F42xx Errata sheet + __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); // enable USB_OTG_HS peripheral clock so + // that the peripheral memory is + // accessible + memset_reg( + (volatile void *)USB_OTG_HS_DATA_FIFO_RAM, + (volatile void *)(USB_OTG_HS_DATA_FIFO_RAM + USB_OTG_HS_DATA_FIFO_SIZE), + 0); + + __HAL_RCC_USB_OTG_HS_CLK_DISABLE(); // disable USB OTG_HS peripheral clock as + // the peripheral is not needed right now +} + #endif // KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/platform.h b/core/embed/trezorhal/stm32f4/platform.h index 13542a9960c..25365abcf96 100644 --- a/core/embed/trezorhal/stm32f4/platform.h +++ b/core/embed/trezorhal/stm32f4/platform.h @@ -35,6 +35,5 @@ void set_core_clock(clock_settings_t settings); void memset_reg(volatile void *start, volatile void *stop, uint32_t val); void jump_to(uint32_t address); void jump_to_with_flag(uint32_t address, uint32_t register_flag); -void clear_otg_hs_memory(void); #endif // TREZORHAL_STM32_H diff --git a/core/embed/trezorhal/stm32f4/startup_stage_0.s b/core/embed/trezorhal/stm32f4/startup_stage_0.s index b255baf3723..cbb9e921d4e 100644 --- a/core/embed/trezorhal/stm32f4/startup_stage_0.s +++ b/core/embed/trezorhal/stm32f4/startup_stage_0.s @@ -43,11 +43,16 @@ reset_handler: ldr r2, =data_size // size in bytes bl memcpy - // setup the stack protector (see build script "-fstack-protector-all") with an unpredictable value + // setup the stack protector (see build script "-fstack-protector-all") + // with an unpredictable value bl rng_get ldr r1, = __stack_chk_guard str r0, [r1] + // Clear the USB FIFO memory to prevent data leakage of sensitive information. + // This peripheral memory is not cleared by a device reset. + bl clear_otg_hs_memory + // enter the application code bl main diff --git a/core/embed/trezorhal/stm32u5/common.c b/core/embed/trezorhal/stm32u5/common.c deleted file mode 100644 index 15cd44460d4..00000000000 --- a/core/embed/trezorhal/stm32u5/common.c +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include STM32_HAL_H - -#include - -#include "common.h" -#include "display.h" -#include "error_handling.h" -#include "flash_otp.h" -#include "model.h" -#include "rand.h" -#include "secret.h" - -#include "stm32u5xx_ll_utils.h" diff --git a/core/site_scons/models/stm32f4_common.py b/core/site_scons/models/stm32f4_common.py index 9f44b6a0f7b..8152fc30965 100644 --- a/core/site_scons/models/stm32f4_common.py +++ b/core/site_scons/models/stm32f4_common.py @@ -43,7 +43,6 @@ def stm32f4_common_files(env, defines, sources, paths): "embed/trezorhal/stm32f4/applet.c", "embed/trezorhal/stm32f4/board_capabilities.c", "embed/trezorhal/stm32f4/bootutils.c", - "embed/trezorhal/stm32f4/common.c", "embed/trezorhal/stm32f4/entropy.c", "embed/trezorhal/stm32f4/flash.c", "embed/trezorhal/stm32f4/flash_otp.c", diff --git a/core/site_scons/models/stm32u5_common.py b/core/site_scons/models/stm32u5_common.py index 46183ba1e15..2f93167649c 100644 --- a/core/site_scons/models/stm32u5_common.py +++ b/core/site_scons/models/stm32u5_common.py @@ -52,7 +52,6 @@ def stm32u5_common_files(env, defines, sources, paths): "embed/trezorhal/stm32u5/applet.c", "embed/trezorhal/stm32u5/board_capabilities.c", "embed/trezorhal/stm32u5/bootutils.c", - "embed/trezorhal/stm32u5/common.c", "embed/trezorhal/stm32u5/entropy.c", "embed/trezorhal/stm32u5/flash.c", "embed/trezorhal/stm32u5/flash_otp.c", From 567b2d37ab26f3d9e8b0bb41e367c72498c2cb6e Mon Sep 17 00:00:00 2001 From: cepetr Date: Fri, 4 Oct 2024 11:21:32 +0200 Subject: [PATCH 5/7] refactor(core): introduce startup_init.c [no changelog] --- core/embed/models/T2T1/compat_settings.c | 26 ++++++++++++-- core/embed/trezorhal/stm32f4/platform.h | 9 ----- .../stm32f4/{platform.c => startup_init.c} | 1 + core/embed/trezorhal/stm32f4/startup_init.h | 36 +++++++++++++++++++ core/embed/trezorhal/stm32f4/system.c | 4 +++ .../stm32u5/{platform.c => startup_init.c} | 0 core/site_scons/models/stm32f4_common.py | 2 +- core/site_scons/models/stm32u5_common.py | 2 +- 8 files changed, 67 insertions(+), 13 deletions(-) rename core/embed/trezorhal/stm32f4/{platform.c => startup_init.c} (99%) create mode 100644 core/embed/trezorhal/stm32f4/startup_init.h rename core/embed/trezorhal/stm32u5/{platform.c => startup_init.c} (100%) diff --git a/core/embed/models/T2T1/compat_settings.c b/core/embed/models/T2T1/compat_settings.c index a737b3c3760..c8115cb93a9 100644 --- a/core/embed/models/T2T1/compat_settings.c +++ b/core/embed/models/T2T1/compat_settings.c @@ -1,4 +1,26 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ -#include "platform.h" +#include "startup_init.h" -void ensure_compatible_settings(void) { set_core_clock(CLOCK_168_MHZ); } +void ensure_compatible_settings(void) { + // Early version of bootloader on T2T1 expects 168 MHz core clock. + // So we need to set it here before handover to the bootloader. + set_core_clock(CLOCK_168_MHZ); +} diff --git a/core/embed/trezorhal/stm32f4/platform.h b/core/embed/trezorhal/stm32f4/platform.h index 25365abcf96..c2e2b8b0164 100644 --- a/core/embed/trezorhal/stm32f4/platform.h +++ b/core/embed/trezorhal/stm32f4/platform.h @@ -23,15 +23,6 @@ #include STM32_HAL_H #include -typedef enum { - CLOCK_180_MHZ = 0, - CLOCK_168_MHZ = 1, - CLOCK_120_MHZ = 2, -} clock_settings_t; - -void set_core_clock(clock_settings_t settings); -// the following functions are defined in util.s - void memset_reg(volatile void *start, volatile void *stop, uint32_t val); void jump_to(uint32_t address); void jump_to_with_flag(uint32_t address, uint32_t register_flag); diff --git a/core/embed/trezorhal/stm32f4/platform.c b/core/embed/trezorhal/stm32f4/startup_init.c similarity index 99% rename from core/embed/trezorhal/stm32f4/platform.c rename to core/embed/trezorhal/stm32f4/startup_init.c index 0719b8793a5..a2f5d8bb739 100644 --- a/core/embed/trezorhal/stm32f4/platform.c +++ b/core/embed/trezorhal/stm32f4/startup_init.c @@ -19,6 +19,7 @@ #include STM32_HAL_H +#include "startup_init.h" #include "platform.h" #include "rng.h" #include "systick.h" diff --git a/core/embed/trezorhal/stm32f4/startup_init.h b/core/embed/trezorhal/stm32f4/startup_init.h new file mode 100644 index 00000000000..97d13a97601 --- /dev/null +++ b/core/embed/trezorhal/stm32f4/startup_init.h @@ -0,0 +1,36 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TREZORHAL_STM32F4_STARTUP_INIT_H +#define TREZORHAL_STM32F4_STARTUP_INIT_H + +#ifdef TREZOR_MODEL_T + +typedef enum { + CLOCK_180_MHZ = 0, + CLOCK_168_MHZ = 1, + CLOCK_120_MHZ = 2, +} clock_settings_t; + +// Alters core clock frequency +void set_core_clock(clock_settings_t settings); + +#endif + +#endif // TREZORHAL_STM32F4_STARTUP_INIT_H diff --git a/core/embed/trezorhal/stm32f4/system.c b/core/embed/trezorhal/stm32f4/system.c index c206f59de19..61e20c5d315 100644 --- a/core/embed/trezorhal/stm32f4/system.c +++ b/core/embed/trezorhal/stm32f4/system.c @@ -29,6 +29,10 @@ #include "systick.h" #include "systimer.h" +#if defined(TREZOR_MODEL_T) && (!defined(BOARDLOADER)) +#include "startup_init.h" +#endif + #ifndef HardFault_IRQn #define HardFault_IRQn (-13) // not defined in stm32lib/cmsis/stm32429xx.h #endif diff --git a/core/embed/trezorhal/stm32u5/platform.c b/core/embed/trezorhal/stm32u5/startup_init.c similarity index 100% rename from core/embed/trezorhal/stm32u5/platform.c rename to core/embed/trezorhal/stm32u5/startup_init.c diff --git a/core/site_scons/models/stm32f4_common.py b/core/site_scons/models/stm32f4_common.py index 8152fc30965..18add8554ce 100644 --- a/core/site_scons/models/stm32f4_common.py +++ b/core/site_scons/models/stm32f4_common.py @@ -51,10 +51,10 @@ def stm32f4_common_files(env, defines, sources, paths): "embed/trezorhal/stm32f4/lowlevel.c", "embed/trezorhal/stm32f4/monoctr.c", "embed/trezorhal/stm32f4/mpu.c", - "embed/trezorhal/stm32f4/platform.c", "embed/trezorhal/stm32f4/random_delays.c", "embed/trezorhal/stm32f4/rng.c", "embed/trezorhal/stm32f4/secret.c", + "embed/trezorhal/stm32f4/startup_init.c", "embed/trezorhal/stm32f4/syscall.c", "embed/trezorhal/stm32f4/syscall_dispatch.c", "embed/trezorhal/stm32f4/syscall_probe.c", diff --git a/core/site_scons/models/stm32u5_common.py b/core/site_scons/models/stm32u5_common.py index 2f93167649c..2ab4d6aa853 100644 --- a/core/site_scons/models/stm32u5_common.py +++ b/core/site_scons/models/stm32u5_common.py @@ -61,11 +61,11 @@ def stm32u5_common_files(env, defines, sources, paths): "embed/trezorhal/stm32u5/hash_processor.c", "embed/trezorhal/stm32u5/monoctr.c", "embed/trezorhal/stm32u5/mpu.c", - "embed/trezorhal/stm32u5/platform.c", "embed/trezorhal/stm32u5/random_delays.c", "embed/trezorhal/stm32u5/rng.c", "embed/trezorhal/stm32u5/secret.c", "embed/trezorhal/stm32u5/secure_aes.c", + "embed/trezorhal/stm32u5/startup_init.c", "embed/trezorhal/stm32u5/syscall.c", "embed/trezorhal/stm32u5/syscall_dispatch.c", "embed/trezorhal/stm32u5/syscall_probe.c", From b43a9f1d9bcaa6ef7257a3220b010fdc4aeaa1f2 Mon Sep 17 00:00:00 2001 From: cepetr Date: Fri, 4 Oct 2024 13:28:09 +0200 Subject: [PATCH 6/7] refactor(core): decompose lowlevel module [no changelog] --- core/embed/boardloader/main.c | 10 ++-- core/embed/bootloader/main.c | 7 ++- core/embed/kernel/main.c | 5 ++ .../models/D001/boards/stm32f429i-disc1.h | 1 + core/embed/models/T2B1/boards/trezor_r_v10.h | 1 + core/embed/models/T2B1/boards/trezor_r_v3.h | 1 + core/embed/models/T2B1/boards/trezor_r_v4.h | 1 + core/embed/models/T2B1/boards/trezor_r_v6.h | 1 + core/embed/models/T2T1/boards/trezor_t.h | 1 + .../models/T3B1/boards/trezor_t3b1_revB.h | 1 + .../models/T3T1/boards/trezor_t3t1_revE.h | 1 + .../embed/models/T3T1/boards/trezor_t3t1_v4.h | 1 + .../trezorhal/{lowlevel.h => option_bytes.h} | 9 +-- core/embed/trezorhal/pvd.h | 33 +++++++++++ core/embed/trezorhal/reset_flags.h | 35 ++++++++++++ .../stm32f4/{lowlevel.c => option_bytes.c} | 52 +---------------- core/embed/trezorhal/stm32f4/pvd.c | 57 +++++++++++++++++++ core/embed/trezorhal/stm32f4/reset_flags.c | 44 ++++++++++++++ core/embed/trezorhal/stm32f4/startup_init.c | 18 ++++++ core/embed/trezorhal/stm32f4/systask.c | 12 ---- .../stm32u5/{lowlevel.c => option_bytes.c} | 50 +--------------- core/embed/trezorhal/stm32u5/pvd.c | 1 + core/embed/trezorhal/stm32u5/reset_flags.c | 45 +++++++++++++++ core/embed/trezorhal/stm32u5/startup_init.c | 18 ++++++ core/site_scons/models/stm32f4_common.py | 4 +- core/site_scons/models/stm32u5_common.py | 4 +- 26 files changed, 289 insertions(+), 124 deletions(-) rename core/embed/trezorhal/{lowlevel.h => option_bytes.h} (84%) create mode 100644 core/embed/trezorhal/pvd.h create mode 100644 core/embed/trezorhal/reset_flags.h rename core/embed/trezorhal/stm32f4/{lowlevel.c => option_bytes.c} (77%) create mode 100644 core/embed/trezorhal/stm32f4/pvd.c create mode 100644 core/embed/trezorhal/stm32f4/reset_flags.c rename core/embed/trezorhal/stm32u5/{lowlevel.c => option_bytes.c} (83%) create mode 120000 core/embed/trezorhal/stm32u5/pvd.c create mode 100644 core/embed/trezorhal/stm32u5/reset_flags.c diff --git a/core/embed/boardloader/main.c b/core/embed/boardloader/main.c index 18054329b0d..6e021bd20fc 100644 --- a/core/embed/boardloader/main.c +++ b/core/embed/boardloader/main.c @@ -33,6 +33,8 @@ #include "model.h" #include "mpu.h" #include "platform.h" +#include "pvd.h" +#include "reset_flags.h" #include "rng.h" #include "rsod.h" #include "secret.h" @@ -57,9 +59,9 @@ #endif #endif -#include "lowlevel.h" #include "model.h" #include "monoctr.h" +#include "option_bytes.h" #include "version.h" #include "memzero.h" @@ -240,9 +242,9 @@ int main(void) { reset_flags_reset(); - // need the systick timer running before many HAL operations. - // want the PVD enabled before flash operations too. - periph_init(); +#ifdef USE_PVD + pvd_init(); +#endif if (sectrue != flash_configure_option_bytes()) { // display is not initialized so don't call ensure diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index fe152bba7bc..d8cfebddd94 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -29,8 +29,9 @@ #include "flash_otp.h" #include "flash_utils.h" #include "image.h" -#include "lowlevel.h" #include "messages.pb.h" +#include "option_bytes.h" +#include "pvd.h" #include "random_delays.h" #include "rsod.h" #include "secbool.h" @@ -364,6 +365,10 @@ int bootloader_main(void) { rdi_init(); +#ifdef USE_PVD + pvd_init(); +#endif + #ifdef USE_HASH_PROCESSOR hash_processor_init(); #endif diff --git a/core/embed/kernel/main.c b/core/embed/kernel/main.c index b642e694949..16c139a3b24 100644 --- a/core/embed/kernel/main.c +++ b/core/embed/kernel/main.c @@ -38,6 +38,7 @@ #include "optiga_commands.h" #include "optiga_transport.h" #include "platform.h" +#include "pvd.h" #include "random_delays.h" #include "rsod.h" #include "sdcard.h" @@ -74,6 +75,10 @@ void drivers_init() { tamper_init(); #endif +#ifdef USE_PVD + pvd_init(); +#endif + rdi_init(); #ifdef RDI diff --git a/core/embed/models/D001/boards/stm32f429i-disc1.h b/core/embed/models/D001/boards/stm32f429i-disc1.h index 28198342d25..30620b36975 100644 --- a/core/embed/models/D001/boards/stm32f429i-disc1.h +++ b/core/embed/models/D001/boards/stm32f429i-disc1.h @@ -12,6 +12,7 @@ #define USE_TOUCH 1 #define USE_SDRAM 1 #define USE_RGB_COLORS 1 +#define USE_PVD 1 #define I2C_COUNT 1 #define I2C_INSTANCE_0 I2C3 diff --git a/core/embed/models/T2B1/boards/trezor_r_v10.h b/core/embed/models/T2B1/boards/trezor_r_v10.h index 24cda1e8ccf..4b50ff880ae 100644 --- a/core/embed/models/T2B1/boards/trezor_r_v10.h +++ b/core/embed/models/T2B1/boards/trezor_r_v10.h @@ -7,6 +7,7 @@ #define USE_SBU 1 #define USE_I2C 1 #define USE_CONSUMPTION_MASK 1 +#define USE_PVD #define DISPLAY_RESX 128 #define DISPLAY_RESY 64 diff --git a/core/embed/models/T2B1/boards/trezor_r_v3.h b/core/embed/models/T2B1/boards/trezor_r_v3.h index ffe8b691489..d8cfb1ee5a9 100644 --- a/core/embed/models/T2B1/boards/trezor_r_v3.h +++ b/core/embed/models/T2B1/boards/trezor_r_v3.h @@ -5,6 +5,7 @@ #define USE_BUTTON 1 #define USE_SBU 1 +#define USE_PVD 1 #define DISPLAY_RESX 128 #define DISPLAY_RESY 128 diff --git a/core/embed/models/T2B1/boards/trezor_r_v4.h b/core/embed/models/T2B1/boards/trezor_r_v4.h index 9dbbb62671e..ac7ba7ca95b 100644 --- a/core/embed/models/T2B1/boards/trezor_r_v4.h +++ b/core/embed/models/T2B1/boards/trezor_r_v4.h @@ -5,6 +5,7 @@ #define USE_BUTTON 1 #define USE_SBU 1 +#define USE_PVD 1 #define DISPLAY_RESX 128 #define DISPLAY_RESY 64 diff --git a/core/embed/models/T2B1/boards/trezor_r_v6.h b/core/embed/models/T2B1/boards/trezor_r_v6.h index d177c3d726c..561df4d297a 100644 --- a/core/embed/models/T2B1/boards/trezor_r_v6.h +++ b/core/embed/models/T2B1/boards/trezor_r_v6.h @@ -5,6 +5,7 @@ #define USE_BUTTON 1 #define USE_SBU 1 +#define USE_PVD 1 #define DISPLAY_RESX 128 #define DISPLAY_RESY 64 diff --git a/core/embed/models/T2T1/boards/trezor_t.h b/core/embed/models/T2T1/boards/trezor_t.h index 60d569648b2..42f5a91370c 100644 --- a/core/embed/models/T2T1/boards/trezor_t.h +++ b/core/embed/models/T2T1/boards/trezor_t.h @@ -10,6 +10,7 @@ #define USE_RGB_COLORS 1 #define USE_BACKLIGHT 1 #define USE_DISP_I8080_8BIT_DW 1 +#define USE_PVD 1 #define DISPLAY_RESX 240 #define DISPLAY_RESY 240 diff --git a/core/embed/models/T3B1/boards/trezor_t3b1_revB.h b/core/embed/models/T3B1/boards/trezor_t3b1_revB.h index 7c57e7623d0..6949dcb0052 100644 --- a/core/embed/models/T3B1/boards/trezor_t3b1_revB.h +++ b/core/embed/models/T3B1/boards/trezor_t3b1_revB.h @@ -11,6 +11,7 @@ #define USE_SBU 1 #define USE_HASH_PROCESSOR 1 #define USE_CONSUMPTION_MASK 1 +#define USE_PVD 1 #define DISPLAY_LEGACY_HEADER "displays/vg-2864ksweg01.h" diff --git a/core/embed/models/T3T1/boards/trezor_t3t1_revE.h b/core/embed/models/T3T1/boards/trezor_t3t1_revE.h index 425b9afc498..e37d8544b7d 100644 --- a/core/embed/models/T3T1/boards/trezor_t3t1_revE.h +++ b/core/embed/models/T3T1/boards/trezor_t3t1_revE.h @@ -12,6 +12,7 @@ #define USE_HAPTIC 1 #define USE_BACKLIGHT 1 #define USE_HASH_PROCESSOR 1 +#define USE_PVD 1 #define DISPLAY_RESX 240 #define DISPLAY_RESY 240 diff --git a/core/embed/models/T3T1/boards/trezor_t3t1_v4.h b/core/embed/models/T3T1/boards/trezor_t3t1_v4.h index 1a8741e0a5f..9cbe6e5fefa 100644 --- a/core/embed/models/T3T1/boards/trezor_t3t1_v4.h +++ b/core/embed/models/T3T1/boards/trezor_t3t1_v4.h @@ -13,6 +13,7 @@ #define USE_HAPTIC 1 #define USE_BACKLIGHT 1 #define USE_HASH_PROCESSOR 1 +#define USE_PVD 1 #define DISPLAY_RESX 240 #define DISPLAY_RESY 240 diff --git a/core/embed/trezorhal/lowlevel.h b/core/embed/trezorhal/option_bytes.h similarity index 84% rename from core/embed/trezorhal/lowlevel.h rename to core/embed/trezorhal/option_bytes.h index 7d6cf789163..449f2c3b46c 100644 --- a/core/embed/trezorhal/lowlevel.h +++ b/core/embed/trezorhal/option_bytes.h @@ -17,8 +17,8 @@ * along with this program. If not, see . */ -#ifndef __TREZORHAL_LOWLEVEL_H__ -#define __TREZORHAL_LOWLEVEL_H__ +#ifndef TREZORHAL_OPTION_BYTES_H +#define TREZORHAL_OPTION_BYTES_H #include "secbool.h" @@ -29,10 +29,7 @@ void flash_lock_option_bytes(void); void flash_unlock_option_bytes(void); uint32_t flash_set_option_bytes(void); secbool flash_configure_option_bytes(void); -void periph_init(void); -secbool reset_flags_check(void); -void reset_flags_reset(void); #endif // KERNEL_MODE -#endif // __TREZORHAL_LOWLEVEL_H__ +#endif // TREZORHAL_OPTION_BYTES_H diff --git a/core/embed/trezorhal/pvd.h b/core/embed/trezorhal/pvd.h new file mode 100644 index 00000000000..74e288702d1 --- /dev/null +++ b/core/embed/trezorhal/pvd.h @@ -0,0 +1,33 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TREZORHAL_PVD_H +#define TREZORHAL_PVD_H + +#ifdef KERNEL_MODE + +// Initialize the Programmable Voltage Detector (PVD) peripheral. +// +// The PVD is used to detect a voltage drop below a certain threshold +// and trigger an interrupt. This is used to safely shutdown the device. +void pvd_init(void); + +#endif // KERNEL_MODE + +#endif // TREZORHAL_PVD_H diff --git a/core/embed/trezorhal/reset_flags.h b/core/embed/trezorhal/reset_flags.h new file mode 100644 index 00000000000..52ae40d5571 --- /dev/null +++ b/core/embed/trezorhal/reset_flags.h @@ -0,0 +1,35 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef TREZORHAL_RESET_FLAGS_H +#define TREZORHAL_RESET_FLAGS_H + +#include "secbool.h" + +#ifdef KERNEL_MODE + +// Checks if the CPU reset flags indicate an expected type of reset. +secbool reset_flags_check(void); + +// Clear the CPU register that holds the reset flags. +void reset_flags_reset(void); + +#endif // KERNEL_MODE + +#endif // TREZORHAL_RESET_FLAGS_H diff --git a/core/embed/trezorhal/stm32f4/lowlevel.c b/core/embed/trezorhal/stm32f4/option_bytes.c similarity index 77% rename from core/embed/trezorhal/stm32f4/lowlevel.c rename to core/embed/trezorhal/stm32f4/option_bytes.c index 1174991f193..be42e799adf 100644 --- a/core/embed/trezorhal/stm32f4/lowlevel.c +++ b/core/embed/trezorhal/stm32f4/option_bytes.c @@ -19,11 +19,9 @@ #include STM32_HAL_H -#include "lowlevel.h" - -#include - +#include "option_bytes.h" #include "flash_otp.h" +#include "mpu.h" #ifdef KERNEL_MODE @@ -154,50 +152,4 @@ secbool flash_configure_option_bytes(void) { return secfalse; // notify that we DID have to change the option bytes } -void periph_init(void) { - // STM32F4xx HAL library initialization: - // - configure the Flash prefetch, instruction and data caches - // - configure the Systick to generate an interrupt each 1 msec - // - set NVIC Group Priority to 4 - // - global MSP (MCU Support Package) initialization - HAL_Init(); - - // Enable GPIO clocks - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOD_CLK_ENABLE(); - - // enable the PVD (programmable voltage detector). - // select the "2.7V" threshold (level 5). - // this detector will be active regardless of the - // flash option byte BOR setting. - __HAL_RCC_PWR_CLK_ENABLE(); - PWR_PVDTypeDef pvd_config = {0}; - pvd_config.PVDLevel = PWR_PVDLEVEL_5; - pvd_config.Mode = PWR_PVD_MODE_IT_RISING_FALLING; - HAL_PWR_ConfigPVD(&pvd_config); - HAL_PWR_EnablePVD(); - NVIC_EnableIRQ(PVD_IRQn); -} - -secbool reset_flags_check(void) { -#if PRODUCTION - // this is effective enough that it makes development painful, so only use it - // for production. check the reset flags to assure that we arrive here due to - // a regular full power-on event, and not as a result of a lesser reset. - if ((RCC->CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | - RCC_CSR_SFTRSTF | RCC_CSR_PORRSTF | RCC_CSR_PINRSTF | - RCC_CSR_BORRSTF)) != - (RCC_CSR_PORRSTF | RCC_CSR_PINRSTF | RCC_CSR_BORRSTF)) { - return secfalse; - } -#endif - return sectrue; -} - -void reset_flags_reset(void) { - RCC->CSR |= RCC_CSR_RMVF; // clear the reset flags -} - #endif // #ifdef KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/pvd.c b/core/embed/trezorhal/stm32f4/pvd.c new file mode 100644 index 00000000000..328197a5c45 --- /dev/null +++ b/core/embed/trezorhal/stm32f4/pvd.c @@ -0,0 +1,57 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include TREZOR_BOARD +#include STM32_HAL_H + +#include "bootutils.h" +#include "mpu.h" + +#if defined(KERNEL_MODE) && defined(USE_PVD) + +void pvd_init(void) { + // enable the PVD (programmable voltage detector). + // select the "2.8V" threshold (level 5). + // this detector will be active regardless of the + // flash option byte BOR setting. + __HAL_RCC_PWR_CLK_ENABLE(); + PWR_PVDTypeDef pvd_config = {0}; + pvd_config.PVDLevel = PWR_PVDLEVEL_5; + pvd_config.Mode = PWR_PVD_MODE_IT_RISING_FALLING; + HAL_PWR_ConfigPVD(&pvd_config); + HAL_PWR_EnablePVD(); +#ifdef STM32U5 + NVIC_EnableIRQ(PVD_PVM_IRQn); +#else + NVIC_EnableIRQ(PVD_IRQn); +#endif +} + +void PVD_PVM_IRQHandler(void) { + mpu_reconfig(MPU_MODE_DEFAULT); +#ifdef BACKLIGHT_PWM_TIM + // Turn off display backlight + BACKLIGHT_PWM_TIM->BACKLIGHT_PWM_TIM_CCR = 0; +#endif + // from util.s + extern void shutdown_privileged(void); + shutdown_privileged(); +} + +#endif // defined(KERNEL_MODE) && defined(USE_PVD) diff --git a/core/embed/trezorhal/stm32f4/reset_flags.c b/core/embed/trezorhal/stm32f4/reset_flags.c new file mode 100644 index 00000000000..675dc2dc54b --- /dev/null +++ b/core/embed/trezorhal/stm32f4/reset_flags.c @@ -0,0 +1,44 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include STM32_HAL_H + +#include "reset_flags.h" + +#ifdef KERNEL_MODE + +secbool reset_flags_check(void) { +#if PRODUCTION + // this is effective enough that it makes development painful, so only use it + // for production. check the reset flags to assure that we arrive here due to + // a regular full power-on event, and not as a result of a lesser reset. + if ((RCC->CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | + RCC_CSR_SFTRSTF | RCC_CSR_PINRSTF | RCC_CSR_BORRSTF | + RCC_CSR_OBLRSTF)) != (RCC_CSR_PINRSTF | RCC_CSR_BORRSTF)) { + return secfalse; + } +#endif + return sectrue; +} + +void reset_flags_reset(void) { + RCC->CSR |= RCC_CSR_RMVF; // clear the reset flags +} + +#endif diff --git a/core/embed/trezorhal/stm32f4/startup_init.c b/core/embed/trezorhal/stm32f4/startup_init.c index a2f5d8bb739..823cbbdb4f2 100644 --- a/core/embed/trezorhal/stm32f4/startup_init.c +++ b/core/embed/trezorhal/stm32f4/startup_init.c @@ -159,6 +159,24 @@ void SystemInit(void) { // set CP10 and CP11 to enable full access to the fpu coprocessor; ARMv7-M // Architecture Reference Manual section B3.2.20 SCB->CPACR |= ((3U << 22) | (3U << 20)); + + // Configure Flash prefetch, Instruction cache, Data cache +#if (INSTRUCTION_CACHE_ENABLE != 0U) + __HAL_FLASH_INSTRUCTION_CACHE_ENABLE(); +#endif + +#if (PREFETCH_ENABLE != 0U) + __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); +#endif + + // Set Interrupt Group Priority + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + + // Enable GPIO clocks + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); } #ifdef TREZOR_MODEL_T diff --git a/core/embed/trezorhal/stm32f4/systask.c b/core/embed/trezorhal/stm32f4/systask.c index ec73670c1d7..d128fee454a 100644 --- a/core/embed/trezorhal/stm32f4/systask.c +++ b/core/embed/trezorhal/stm32f4/systask.c @@ -634,16 +634,4 @@ void NMI_Handler(void) { mpu_restore(mpu_mode); } -// from util.s -extern void shutdown_privileged(void); - -void PVD_PVM_IRQHandler(void) { - mpu_reconfig(MPU_MODE_DEFAULT); -#ifdef BACKLIGHT_PWM_TIM - // Turn off display backlight - BACKLIGHT_PWM_TIM->BACKLIGHT_PWM_TIM_CCR = 0; -#endif - shutdown_privileged(); -} - #endif // KERNEL_MODE diff --git a/core/embed/trezorhal/stm32u5/lowlevel.c b/core/embed/trezorhal/stm32u5/option_bytes.c similarity index 83% rename from core/embed/trezorhal/stm32u5/lowlevel.c rename to core/embed/trezorhal/stm32u5/option_bytes.c index 7270180782d..d3a95eb50ab 100644 --- a/core/embed/trezorhal/stm32u5/lowlevel.c +++ b/core/embed/trezorhal/stm32u5/option_bytes.c @@ -21,7 +21,7 @@ #ifdef KERNEL_MODE -#include "lowlevel.h" +#include "option_bytes.h" #include "common.h" #include "flash.h" #include "model.h" @@ -41,7 +41,6 @@ #ifdef VDD_3V3 // BOR LEVEL 0: Reset level threshold is around 2.8 V #define WANT_BOR_LEVEL (OB_BOR_LEVEL_4) -#define USE_PVD 1 #elif VDD_1V8 // BOR LEVEL 0: Reset level threshold is around 1.7 V #define WANT_BOR_LEVEL (OB_BOR_LEVEL_0) @@ -266,51 +265,4 @@ secbool flash_configure_option_bytes(void) { return secfalse; // notify that we DID have to change the option bytes } -void periph_init(void) { - // STM32U5xx HAL library initialization: - // - configure the Flash prefetch, instruction and data caches - // - configure the Systick to generate an interrupt each 1 msec - // - set NVIC Group Priority to 4 - // - global MSP (MCU Support Package) initialization - HAL_Init(); - - // Enable GPIO clocks - __HAL_RCC_GPIOA_CLK_ENABLE(); - __HAL_RCC_GPIOB_CLK_ENABLE(); - __HAL_RCC_GPIOC_CLK_ENABLE(); - __HAL_RCC_GPIOD_CLK_ENABLE(); - -#ifdef USE_PVD - // enable the PVD (programmable voltage detector). - // select the "2.8V" threshold (level 5). - // this detector will be active regardless of the - // flash option byte BOR setting. - __HAL_RCC_PWR_CLK_ENABLE(); - PWR_PVDTypeDef pvd_config = {0}; - pvd_config.PVDLevel = PWR_PVDLEVEL_5; - pvd_config.Mode = PWR_PVD_MODE_IT_RISING_FALLING; - HAL_PWR_ConfigPVD(&pvd_config); - HAL_PWR_EnablePVD(); - NVIC_EnableIRQ(PVD_PVM_IRQn); -#endif -} - -secbool reset_flags_check(void) { -#if PRODUCTION - // this is effective enough that it makes development painful, so only use it - // for production. check the reset flags to assure that we arrive here due to - // a regular full power-on event, and not as a result of a lesser reset. - if ((RCC->CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | - RCC_CSR_SFTRSTF | RCC_CSR_PINRSTF | RCC_CSR_BORRSTF | - RCC_CSR_OBLRSTF)) != (RCC_CSR_PINRSTF | RCC_CSR_BORRSTF)) { - return secfalse; - } -#endif - return sectrue; -} - -void reset_flags_reset(void) { - RCC->CSR |= RCC_CSR_RMVF; // clear the reset flags -} - #endif // #ifdef KERNEL_MODE diff --git a/core/embed/trezorhal/stm32u5/pvd.c b/core/embed/trezorhal/stm32u5/pvd.c new file mode 120000 index 00000000000..e94aab5b3eb --- /dev/null +++ b/core/embed/trezorhal/stm32u5/pvd.c @@ -0,0 +1 @@ +../stm32f4/pvd.c \ No newline at end of file diff --git a/core/embed/trezorhal/stm32u5/reset_flags.c b/core/embed/trezorhal/stm32u5/reset_flags.c new file mode 100644 index 00000000000..ce4a664b936 --- /dev/null +++ b/core/embed/trezorhal/stm32u5/reset_flags.c @@ -0,0 +1,45 @@ +/* + * This file is part of the Trezor project, https://trezor.io/ + * + * Copyright (c) SatoshiLabs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include STM32_HAL_H + +#include "reset_flags.h" + +#ifdef KERNEL_MODE + +secbool reset_flags_check(void) { +#if PRODUCTION + // this is effective enough that it makes development painful, so only use it + // for production. check the reset flags to assure that we arrive here due to + // a regular full power-on event, and not as a result of a lesser reset. + if ((RCC->CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | + RCC_CSR_SFTRSTF | RCC_CSR_PORRSTF | RCC_CSR_PINRSTF | + RCC_CSR_BORRSTF)) != + (RCC_CSR_PORRSTF | RCC_CSR_PINRSTF | RCC_CSR_BORRSTF)) { + return secfalse; + } +#endif + return sectrue; +} + +void reset_flags_reset(void) { + RCC->CSR |= RCC_CSR_RMVF; // clear the reset flags +} + +#endif diff --git a/core/embed/trezorhal/stm32u5/startup_init.c b/core/embed/trezorhal/stm32u5/startup_init.c index 393a43b5303..a5f26742acd 100644 --- a/core/embed/trezorhal/stm32u5/startup_init.c +++ b/core/embed/trezorhal/stm32u5/startup_init.c @@ -214,6 +214,24 @@ void SystemInit(void) { // enable instruction cache in default 2-way mode ICACHE->CR = ICACHE_CR_EN; + + /* Configure Flash prefetch */ +#if (PREFETCH_ENABLE != 0U) + __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); +#endif /* PREFETCH_ENABLE */ + + /* Set Interrupt Group Priority */ + HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); + + /* Update the SystemCoreClock global variable */ + /// SystemCoreClock = HAL_RCC_GetSysClockFreq() >> AHBPrescTable[(RCC->CFGR2 & + /// RCC_CFGR2_HPRE) >> RCC_CFGR2_HPRE_Pos]; + + // Enable GPIO clocks + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_GPIOB_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + __HAL_RCC_GPIOD_CLK_ENABLE(); } #endif // #ifdef KERNEL_MODE diff --git a/core/site_scons/models/stm32f4_common.py b/core/site_scons/models/stm32f4_common.py index 18add8554ce..400595207f1 100644 --- a/core/site_scons/models/stm32f4_common.py +++ b/core/site_scons/models/stm32f4_common.py @@ -48,10 +48,12 @@ def stm32f4_common_files(env, defines, sources, paths): "embed/trezorhal/stm32f4/flash_otp.c", "embed/trezorhal/stm32f4/fwutils.c", "embed/trezorhal/stm32f4/layout.c", - "embed/trezorhal/stm32f4/lowlevel.c", "embed/trezorhal/stm32f4/monoctr.c", "embed/trezorhal/stm32f4/mpu.c", + "embed/trezorhal/stm32f4/option_bytes.c", + "embed/trezorhal/stm32f4/pvd.c", "embed/trezorhal/stm32f4/random_delays.c", + "embed/trezorhal/stm32f4/reset_flags.c", "embed/trezorhal/stm32f4/rng.c", "embed/trezorhal/stm32f4/secret.c", "embed/trezorhal/stm32f4/startup_init.c", diff --git a/core/site_scons/models/stm32u5_common.py b/core/site_scons/models/stm32u5_common.py index 2ab4d6aa853..c9ac21e29bc 100644 --- a/core/site_scons/models/stm32u5_common.py +++ b/core/site_scons/models/stm32u5_common.py @@ -57,11 +57,13 @@ def stm32u5_common_files(env, defines, sources, paths): "embed/trezorhal/stm32u5/flash_otp.c", "embed/trezorhal/stm32u5/fwutils.c", "embed/trezorhal/stm32u5/layout.c", - "embed/trezorhal/stm32u5/lowlevel.c", "embed/trezorhal/stm32u5/hash_processor.c", "embed/trezorhal/stm32u5/monoctr.c", "embed/trezorhal/stm32u5/mpu.c", + "embed/trezorhal/stm32u5/option_bytes.c", + "embed/trezorhal/stm32u5/pvd.c", "embed/trezorhal/stm32u5/random_delays.c", + "embed/trezorhal/stm32u5/reset_flags.c", "embed/trezorhal/stm32u5/rng.c", "embed/trezorhal/stm32u5/secret.c", "embed/trezorhal/stm32u5/secure_aes.c", From 91c5ea35559a0a75a359fa422a2cd3780e25d1da Mon Sep 17 00:00:00 2001 From: cepetr Date: Fri, 4 Oct 2024 14:28:34 +0200 Subject: [PATCH 7/7] refactor(core): get rid of platform.h [no changelog] --- core/embed/boardloader/main.c | 1 - core/embed/bootloader/main.c | 1 - core/embed/kernel/main.c | 2 +- core/embed/trezorhal/bootutils.h | 14 +++++++++ core/embed/trezorhal/option_bytes.h | 2 ++ core/embed/trezorhal/stm32f4/bootutils.c | 3 +- core/embed/trezorhal/stm32f4/platform.h | 30 ------------------- core/embed/trezorhal/stm32f4/startup_init.c | 4 +-- core/embed/trezorhal/stm32f4/system.c | 1 - core/embed/trezorhal/stm32u5/platform.h | 32 --------------------- core/embed/trezorhal/stm32u5/startup_init.c | 3 +- 11 files changed, 22 insertions(+), 71 deletions(-) delete mode 100644 core/embed/trezorhal/stm32f4/platform.h delete mode 100644 core/embed/trezorhal/stm32u5/platform.h diff --git a/core/embed/boardloader/main.c b/core/embed/boardloader/main.c index 6e021bd20fc..c144b0bbec6 100644 --- a/core/embed/boardloader/main.c +++ b/core/embed/boardloader/main.c @@ -32,7 +32,6 @@ #include "image.h" #include "model.h" #include "mpu.h" -#include "platform.h" #include "pvd.h" #include "reset_flags.h" #include "rng.h" diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index d8cfebddd94..e99aecd5da5 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -85,7 +85,6 @@ #else #include "compiler_traits.h" #include "mpu.h" -#include "platform.h" #endif #define USB_IFACE_NUM 0 diff --git a/core/embed/kernel/main.c b/core/embed/kernel/main.c index 16c139a3b24..ad442a3f370 100644 --- a/core/embed/kernel/main.c +++ b/core/embed/kernel/main.c @@ -37,7 +37,7 @@ #include "mpu.h" #include "optiga_commands.h" #include "optiga_transport.h" -#include "platform.h" +#include "option_bytes.h" #include "pvd.h" #include "random_delays.h" #include "rsod.h" diff --git a/core/embed/trezorhal/bootutils.h b/core/embed/trezorhal/bootutils.h index cd1e84dbaa9..d5d27ef907c 100644 --- a/core/embed/trezorhal/bootutils.h +++ b/core/embed/trezorhal/bootutils.h @@ -46,4 +46,18 @@ void __attribute__((noreturn)) reboot_and_upgrade(const uint8_t hash[32]); // unrecoverable error state. void __attribute__((noreturn)) secure_shutdown(void); +// Alternative memset with slightly different arguments +// +// This function writes a 32-bit value to a range of memory addresses. +// The range is defined by the start and stop pointers and must +// be aligned to 4 bytes. +void memset_reg(volatile void *start, volatile void *stop, uint32_t val); + +// Jumps to the next booting stage (e.g. bootloader to firmware). +// `address` points to the flash at the vector table of the next stage. +// +// Before jumping, the function disables all interrupts and clears the +// memory and registers that could contain sensitive information. +void jump_to(uint32_t address); + #endif // TREZORHAL_BOOTUTILS_H diff --git a/core/embed/trezorhal/option_bytes.h b/core/embed/trezorhal/option_bytes.h index 449f2c3b46c..802db911ba3 100644 --- a/core/embed/trezorhal/option_bytes.h +++ b/core/embed/trezorhal/option_bytes.h @@ -30,6 +30,8 @@ void flash_unlock_option_bytes(void); uint32_t flash_set_option_bytes(void); secbool flash_configure_option_bytes(void); +void check_oem_keys(void); + #endif // KERNEL_MODE #endif // TREZORHAL_OPTION_BYTES_H diff --git a/core/embed/trezorhal/stm32f4/bootutils.c b/core/embed/trezorhal/stm32f4/bootutils.c index 0bd724a64d1..d0e9aad5fa8 100644 --- a/core/embed/trezorhal/stm32f4/bootutils.c +++ b/core/embed/trezorhal/stm32f4/bootutils.c @@ -27,7 +27,6 @@ #include "irq.h" #include "model.h" #include "mpu.h" -#include "platform.h" #ifdef KERNEL_MODE @@ -160,6 +159,8 @@ reboot_with_args(boot_command_t command, const void* args, size_t args_size) { ensure_thread_mode(); + // from util.s + extern void jump_to_with_flag(uint32_t address, uint32_t reset_flag); jump_to_with_flag(BOOTLOADER_START + IMAGE_HEADER_SIZE, g_boot_command); for (;;) ; diff --git a/core/embed/trezorhal/stm32f4/platform.h b/core/embed/trezorhal/stm32f4/platform.h deleted file mode 100644 index c2e2b8b0164..00000000000 --- a/core/embed/trezorhal/stm32f4/platform.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef TREZORHAL_STM32_H -#define TREZORHAL_STM32_H - -#include STM32_HAL_H -#include - -void memset_reg(volatile void *start, volatile void *stop, uint32_t val); -void jump_to(uint32_t address); -void jump_to_with_flag(uint32_t address, uint32_t register_flag); - -#endif // TREZORHAL_STM32_H diff --git a/core/embed/trezorhal/stm32f4/startup_init.c b/core/embed/trezorhal/stm32f4/startup_init.c index 823cbbdb4f2..9b2d4be9861 100644 --- a/core/embed/trezorhal/stm32f4/startup_init.c +++ b/core/embed/trezorhal/stm32f4/startup_init.c @@ -18,12 +18,12 @@ */ #include STM32_HAL_H +#include TREZOR_BOARD #include "startup_init.h" -#include "platform.h" +#include "bootutils.h" #include "rng.h" #include "systick.h" -#include TREZOR_BOARD #ifdef KERNEL_MODE diff --git a/core/embed/trezorhal/stm32f4/system.c b/core/embed/trezorhal/stm32f4/system.c index 61e20c5d315..1083bf99fc0 100644 --- a/core/embed/trezorhal/stm32f4/system.c +++ b/core/embed/trezorhal/stm32f4/system.c @@ -23,7 +23,6 @@ #include "bootutils.h" #include "mpu.h" -#include "platform.h" #include "systask.h" #include "system.h" #include "systick.h" diff --git a/core/embed/trezorhal/stm32u5/platform.h b/core/embed/trezorhal/stm32u5/platform.h deleted file mode 100644 index ff990e12c2c..00000000000 --- a/core/embed/trezorhal/stm32u5/platform.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef TREZORHAL_STM32_H -#define TREZORHAL_STM32_H - -#include - -// the following functions are defined in util.s -void memset_reg(volatile void *start, volatile void *stop, uint32_t val); -void jump_to(uint32_t address); -void jump_to_with_flag(uint32_t address, uint32_t register_flag); - -void check_oem_keys(void); - -#endif // TREZORHAL_STM32_H diff --git a/core/embed/trezorhal/stm32u5/startup_init.c b/core/embed/trezorhal/stm32u5/startup_init.c index a5f26742acd..c08df7316b8 100644 --- a/core/embed/trezorhal/stm32u5/startup_init.c +++ b/core/embed/trezorhal/stm32u5/startup_init.c @@ -17,11 +17,10 @@ * along with this program. If not, see . */ +#include TREZOR_BOARD #include STM32_HAL_H -#include "platform.h" #include "rng.h" -#include TREZOR_BOARD #ifdef KERNEL_MODE