diff --git a/core/SConscript.boardloader b/core/SConscript.boardloader index a30ece64f8..103a73caf4 100644 --- a/core/SConscript.boardloader +++ b/core/SConscript.boardloader @@ -114,7 +114,7 @@ env.Replace( '-fstack-protector-strong ' + env.get('ENV')["CPU_CCFLAGS"] + CCFLAGS_MOD, CCFLAGS_QSTR='-DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB', - LINKFLAGS="-T build/boardloader/memory.ld -Wl,--gc-sections -Wl,-Map=build/boardloader/boardloader.map -Wl,--warn-common -Wl,--print-memory-usage", + LINKFLAGS="-T build/boardloader/memory.ld -Wl,--gc-sections -Wl,-Map=build/boardloader/boardloader.map -Wl,--warn-common -Wl,--undefined=__errno -Wl,--print-memory-usage", CPPPATH=[ 'embed/projects/boardloader', 'embed/rtl/inc', @@ -169,7 +169,7 @@ program_elf = env.Command( target='boardloader.elf', source=obj_program, action= - '$LINK -o $TARGET $CCFLAGS $CFLAGS $LINKFLAGS $SOURCES -lc_nano -lgcc', + '$LINK -o $TARGET $CCFLAGS $CFLAGS $LINKFLAGS $SOURCES -lc_nano -lm -lgcc', ) env.Depends(program_elf, linkerscript_gen) diff --git a/core/embed/projects/boardloader/main.c b/core/embed/projects/boardloader/main.c index 84b7e3567e..2741d850cf 100644 --- a/core/embed/projects/boardloader/main.c +++ b/core/embed/projects/boardloader/main.c @@ -74,6 +74,33 @@ static const uint8_t * const BOARDLOADER_KEYS[] = { #endif }; +static void drivers_init(void) { +#ifdef USE_PVD + pvd_init(); +#endif +#ifdef USE_TAMPER + tamper_init(); +#endif + secret_init(); +#ifdef USE_HASH_PROCESSOR + hash_processor_init(); +#endif + gfx_bitblt_init(); + display_init(DISPLAY_RESET_CONTENT); +#ifdef USE_SD_CARD + sdcard_init(); +#endif +} + +static void drivers_deinit(void) { +#ifdef FIXED_HW_DEINIT + // TODO +#endif + + display_deinit(DISPLAY_JUMP_BEHAVIOR); + ensure_compatible_settings(); +} + static uint8_t get_bootloader_min_version(void) { uint8_t version = 0; ensure(monoctr_read(MONOCTR_BOOTLOADER_VERSION, &version), "monoctr read"); @@ -234,40 +261,19 @@ int main(void) { reset_flags_reset(); -#ifdef USE_PVD - pvd_init(); -#endif - if (sectrue != flash_configure_option_bytes()) { // display is not initialized so don't call ensure erase_storage(NULL); return 2; } -#ifdef USE_TAMPER - tamper_init(); -#endif - #ifdef USE_TRUSTZONE tz_init_boardloader(); #endif - secret_init(); - -#ifdef USE_HASH_PROCESSOR - hash_processor_init(); -#endif - - gfx_bitblt_init(); - - display_init(DISPLAY_RESET_CONTENT); - - gfx_clear(); - display_refresh(); - -#if defined USE_SD_CARD - sdcard_init(); + drivers_init(); +#ifdef USE_SD_CARD // If the bootloader is being updated from SD card, we need to preserve the // monotonic counter from the old bootloader. This is in case that the old // bootloader did not have the chance yet to write its monotonic counter to @@ -311,11 +317,9 @@ int main(void) { // This includes the version of bootloader potentially updated from SD card. write_bootloader_min_version(hdr->monotonic); - display_deinit(DISPLAY_JUMP_BEHAVIOR); + drivers_deinit(); - ensure_compatible_settings(); - - mpu_reconfig(MPU_MODE_DISABLED); + system_deinit(); // g_boot_command is preserved on STM32U5 jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE)); diff --git a/core/embed/projects/bootloader/main.c b/core/embed/projects/bootloader/main.c index e4beeff043..8205ec80b8 100644 --- a/core/embed/projects/bootloader/main.c +++ b/core/embed/projects/bootloader/main.c @@ -90,6 +90,56 @@ void failed_jump_to_firmware(void); CONFIDENTIAL volatile secbool dont_optimize_out_true = sectrue; CONFIDENTIAL void (*volatile firmware_jump_fn)(void) = failed_jump_to_firmware; +static void drivers_init(secbool *touch_initialized) { + random_delays_init(); +#ifdef USE_PVD + pvd_init(); +#endif +#ifdef USE_HASH_PROCESSOR + hash_processor_init(); +#endif + gfx_bitblt_init(); + display_init(DISPLAY_JUMP_BEHAVIOR); + unit_properties_init(); + +#ifdef USE_TOUCH + secbool allow_touchless_mode = secfalse; +#if defined TREZOR_MODEL_T3T1 || defined TREZOR_MODEL_T3W1 + // on T3T1 and T3W1, tester needs to run without touch, so making an exception + // until unit variant is written in OTP + const secbool manufacturing_mode = + unit_properties()->locked ? secfalse : sectrue; + allow_touchless_mode = manufacturing_mode; + +#endif + *touch_initialized = touch_init(); + if (allow_touchless_mode != sectrue) { + ensure(*touch_initialized, "Touch screen panel was not loaded properly."); + } +#endif + +#ifdef USE_OPTIGA + optiga_hal_init(); +#endif +#ifdef USE_BUTTON + button_init(); +#endif +#ifdef USE_CONSUMPTION_MASK + consumption_mask_init(); +#endif +#ifdef USE_RGB_LED + rgb_led_init(); +#endif +} + +static void drivers_deinit(void) { +#ifdef FIXED_HW_DEINIT + // TODO +#endif + display_deinit(DISPLAY_JUMP_BEHAVIOR); + ensure_compatible_settings(); +} + static void usb_init_all(secbool usb21_landing) { usb_dev_info_t dev_info = { .device_class = 0x00, @@ -324,11 +374,9 @@ void real_jump_to_firmware(void) { ui_screen_boot_stage_1(false); } - display_deinit(DISPLAY_JUMP_BEHAVIOR); + drivers_deinit(); - ensure_compatible_settings(); - - mpu_reconfig(MPU_MODE_DISABLED); + system_deinit(); jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE)); } @@ -347,41 +395,11 @@ int main(void) { int bootloader_main(void) { #endif secbool stay_in_bootloader = secfalse; + secbool touch_initialized = secfalse; system_init(&rsod_panic_handler); - random_delays_init(); - -#ifdef USE_PVD - pvd_init(); -#endif - -#ifdef USE_HASH_PROCESSOR - hash_processor_init(); -#endif - - gfx_bitblt_init(); - - display_init(DISPLAY_JUMP_BEHAVIOR); - - unit_properties_init(); - -#ifdef USE_TOUCH - secbool touch_initialized = secfalse; - secbool allow_touchless_mode = secfalse; -#if defined TREZOR_MODEL_T3T1 || defined TREZOR_MODEL_T3W1 - // on T3T1 and T3W1, tester needs to run without touch, so making an exception - // until unit variant is written in OTP - const secbool manufacturing_mode = - unit_properties()->locked ? secfalse : sectrue; - allow_touchless_mode = manufacturing_mode; - -#endif - touch_initialized = touch_init(); - if (allow_touchless_mode != sectrue) { - ensure(touch_initialized, "Touch screen panel was not loaded properly."); - } -#endif + drivers_init(&touch_initialized); ui_screen_boot_stage_1(false); @@ -449,22 +467,6 @@ int bootloader_main(void) { firmware_present_backup = firmware_present; } -#ifdef USE_OPTIGA - optiga_hal_init(); -#endif - -#ifdef USE_BUTTON - button_init(); -#endif - -#ifdef USE_CONSUMPTION_MASK - consumption_mask_init(); -#endif - -#ifdef USE_RGB_LED - rgb_led_init(); -#endif - #if PRODUCTION && !defined STM32U5 // for STM32U5, this check is moved to boardloader ensure_bootloader_min_version(); diff --git a/core/embed/projects/bootloader_ci/main.c b/core/embed/projects/bootloader_ci/main.c index ca521a7d75..cc790e4ff3 100644 --- a/core/embed/projects/bootloader_ci/main.c +++ b/core/embed/projects/bootloader_ci/main.c @@ -36,10 +36,6 @@ #include #include -#ifdef USE_TOUCH -#include -#endif - #include "version.h" #include "bootui.h" @@ -52,6 +48,22 @@ #define USB_IFACE_NUM 0 +static void drivers_init(void) { + display_init(DISPLAY_RESET_CONTENT); + + random_delays_init(); + +#ifdef USE_HASH_PROCESSOR + hash_processor_init(); +#endif +} + +static void drivers_deinit(void) { +#ifdef FIXED_HW_DEINIT + display_deinit(DISPLAY_RESET_CONTENT); +#endif +} + static void usb_init_all(secbool usb21_landing) { usb_dev_info_t dev_info = { .device_class = 0x00, @@ -180,22 +192,13 @@ static secbool check_vendor_header_lock(const vendor_header *const vhdr) { int main(void) { system_init(&rsod_panic_handler); - random_delays_init(); -#ifdef USE_TOUCH - touch_init(); -#endif - -#ifdef USE_HASH_PROCESSOR - hash_processor_init(); -#endif + drivers_init(); #if PRODUCTION && !defined STM32U5 // for STM32U5, this check is moved to boardloader ensure_bootloader_min_version(); #endif - gfx_clear(); - const image_header *hdr = NULL; vendor_header vhdr; // detect whether the device contains a valid firmware @@ -269,7 +272,9 @@ int main(void) { // do not check any trust flags on header, proceed - mpu_reconfig(MPU_MODE_DISABLED); + drivers_deinit(); + + system_deinit(); jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE)); diff --git a/core/embed/sys/task/inc/sys/system.h b/core/embed/sys/task/inc/sys/system.h index a758bacd95..c36b968fb2 100644 --- a/core/embed/sys/task/inc/sys/system.h +++ b/core/embed/sys/task/inc/sys/system.h @@ -31,6 +31,10 @@ // with an error void system_init(systask_error_handler_t error_handler); +// Deinitializes the system services before handover +// to next booting stage. +void system_deinit(void); + // Calls the error handler in the emergency mode. // // This function is called when the system encounters a critical error diff --git a/core/embed/sys/task/stm32/system.c b/core/embed/sys/task/stm32/system.c index 9c860e0e6a..1bf6f4072b 100644 --- a/core/embed/sys/task/stm32/system.c +++ b/core/embed/sys/task/stm32/system.c @@ -57,6 +57,13 @@ void system_init(systask_error_handler_t error_handler) { systimer_init(); } +void system_deinit(void) { +#ifdef FIXED_HW_DEINIT + systick_deinit(); +#endif + mpu_reconfig(MPU_MODE_DISABLED); +} + void system_exit(int exitcode) { systask_exit(NULL, exitcode); } void system_exit_error_ex(const char* title, size_t title_len, diff --git a/core/embed/sys/task/unix/system.c b/core/embed/sys/task/unix/system.c index d1da5c196a..48157962e9 100644 --- a/core/embed/sys/task/unix/system.c +++ b/core/embed/sys/task/unix/system.c @@ -34,6 +34,8 @@ void system_init(systask_error_handler_t error_handler) { systimer_init(); } +void system_deinit(void) { systick_deinit(); } + void system_exit(int exitcode) { if (g_error_handler != NULL) { systask_postmortem_t pminfo = {0}; diff --git a/core/site_scons/models/T3W1/trezor_t3w1_revA.py b/core/site_scons/models/T3W1/trezor_t3w1_revA.py index 05e89c4613..82a8e90b1b 100644 --- a/core/site_scons/models/T3W1/trezor_t3w1_revA.py +++ b/core/site_scons/models/T3W1/trezor_t3w1_revA.py @@ -36,6 +36,7 @@ def configure( ("HW_REVISION", str(hw_revision)), ("HSE_VALUE", "32000000"), ("USE_HSE", "1"), + ("FIXED_HW_DEINIT", "1"), ] sources += [