From bfa3ea4e5e1e2ee96dc5b3911143cef9bf4e9eed Mon Sep 17 00:00:00 2001 From: cepetr Date: Thu, 3 Oct 2024 17:23:20 +0200 Subject: [PATCH] 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 adbea6734..18054329b 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 7ac11fe6a..000000000 --- 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 8d801d507..0719b8793 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 13542a996..25365abcf 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 b255baf37..cbb9e921d 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 15cd44460..000000000 --- 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 9f44b6a0f..8152fc309 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 46183ba1e..2f9316764 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",