1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-11 17:08:15 +00:00

refactor(core): introduce drivers init/deinit in boot/boardloader

[no changelog]
This commit is contained in:
cepetr 2024-12-10 09:53:59 +01:00
parent 1a8a202e44
commit d531b02ef6
8 changed files with 121 additions and 96 deletions

View File

@ -114,7 +114,7 @@ env.Replace(
'-fstack-protector-strong ' '-fstack-protector-strong '
+ env.get('ENV')["CPU_CCFLAGS"] + CCFLAGS_MOD, + env.get('ENV')["CPU_CCFLAGS"] + CCFLAGS_MOD,
CCFLAGS_QSTR='-DNO_QSTR -DN_X64 -DN_X86 -DN_THUMB', 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=[ CPPPATH=[
'embed/projects/boardloader', 'embed/projects/boardloader',
'embed/rtl/inc', 'embed/rtl/inc',
@ -169,7 +169,7 @@ program_elf = env.Command(
target='boardloader.elf', target='boardloader.elf',
source=obj_program, source=obj_program,
action= 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) env.Depends(program_elf, linkerscript_gen)

View File

@ -74,6 +74,33 @@ static const uint8_t * const BOARDLOADER_KEYS[] = {
#endif #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) { static uint8_t get_bootloader_min_version(void) {
uint8_t version = 0; uint8_t version = 0;
ensure(monoctr_read(MONOCTR_BOOTLOADER_VERSION, &version), "monoctr read"); ensure(monoctr_read(MONOCTR_BOOTLOADER_VERSION, &version), "monoctr read");
@ -234,40 +261,19 @@ int main(void) {
reset_flags_reset(); reset_flags_reset();
#ifdef USE_PVD
pvd_init();
#endif
if (sectrue != flash_configure_option_bytes()) { if (sectrue != flash_configure_option_bytes()) {
// display is not initialized so don't call ensure // display is not initialized so don't call ensure
erase_storage(NULL); erase_storage(NULL);
return 2; return 2;
} }
#ifdef USE_TAMPER
tamper_init();
#endif
#ifdef USE_TRUSTZONE #ifdef USE_TRUSTZONE
tz_init_boardloader(); tz_init_boardloader();
#endif #endif
secret_init(); drivers_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();
#ifdef USE_SD_CARD
// If the bootloader is being updated from SD card, we need to preserve the // 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 // 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 // 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. // This includes the version of bootloader potentially updated from SD card.
write_bootloader_min_version(hdr->monotonic); write_bootloader_min_version(hdr->monotonic);
display_deinit(DISPLAY_JUMP_BEHAVIOR); drivers_deinit();
ensure_compatible_settings(); system_deinit();
mpu_reconfig(MPU_MODE_DISABLED);
// g_boot_command is preserved on STM32U5 // g_boot_command is preserved on STM32U5
jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE)); jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE));

View File

@ -90,6 +90,56 @@ void failed_jump_to_firmware(void);
CONFIDENTIAL volatile secbool dont_optimize_out_true = sectrue; CONFIDENTIAL volatile secbool dont_optimize_out_true = sectrue;
CONFIDENTIAL void (*volatile firmware_jump_fn)(void) = failed_jump_to_firmware; 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) { static void usb_init_all(secbool usb21_landing) {
usb_dev_info_t dev_info = { usb_dev_info_t dev_info = {
.device_class = 0x00, .device_class = 0x00,
@ -324,11 +374,9 @@ void real_jump_to_firmware(void) {
ui_screen_boot_stage_1(false); ui_screen_boot_stage_1(false);
} }
display_deinit(DISPLAY_JUMP_BEHAVIOR); drivers_deinit();
ensure_compatible_settings(); system_deinit();
mpu_reconfig(MPU_MODE_DISABLED);
jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE)); jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));
} }
@ -347,41 +395,11 @@ int main(void) {
int bootloader_main(void) { int bootloader_main(void) {
#endif #endif
secbool stay_in_bootloader = secfalse; secbool stay_in_bootloader = secfalse;
secbool touch_initialized = secfalse;
system_init(&rsod_panic_handler); system_init(&rsod_panic_handler);
random_delays_init(); drivers_init(&touch_initialized);
#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
ui_screen_boot_stage_1(false); ui_screen_boot_stage_1(false);
@ -449,22 +467,6 @@ int bootloader_main(void) {
firmware_present_backup = firmware_present; 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 #if PRODUCTION && !defined STM32U5
// for STM32U5, this check is moved to boardloader // for STM32U5, this check is moved to boardloader
ensure_bootloader_min_version(); ensure_bootloader_min_version();

View File

@ -36,10 +36,6 @@
#include <util/image.h> #include <util/image.h>
#include <util/rsod.h> #include <util/rsod.h>
#ifdef USE_TOUCH
#include <io/touch.h>
#endif
#include "version.h" #include "version.h"
#include "bootui.h" #include "bootui.h"
@ -52,6 +48,22 @@
#define USB_IFACE_NUM 0 #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) { static void usb_init_all(secbool usb21_landing) {
usb_dev_info_t dev_info = { usb_dev_info_t dev_info = {
.device_class = 0x00, .device_class = 0x00,
@ -180,22 +192,13 @@ static secbool check_vendor_header_lock(const vendor_header *const vhdr) {
int main(void) { int main(void) {
system_init(&rsod_panic_handler); system_init(&rsod_panic_handler);
random_delays_init(); drivers_init();
#ifdef USE_TOUCH
touch_init();
#endif
#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
#if PRODUCTION && !defined STM32U5 #if PRODUCTION && !defined STM32U5
// for STM32U5, this check is moved to boardloader // for STM32U5, this check is moved to boardloader
ensure_bootloader_min_version(); ensure_bootloader_min_version();
#endif #endif
gfx_clear();
const image_header *hdr = NULL; const image_header *hdr = NULL;
vendor_header vhdr; vendor_header vhdr;
// detect whether the device contains a valid firmware // detect whether the device contains a valid firmware
@ -269,7 +272,9 @@ int main(void) {
// do not check any trust flags on header, proceed // 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)); jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));

View File

@ -31,6 +31,10 @@
// with an error // with an error
void system_init(systask_error_handler_t error_handler); 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. // Calls the error handler in the emergency mode.
// //
// This function is called when the system encounters a critical error // This function is called when the system encounters a critical error

View File

@ -57,6 +57,13 @@ void system_init(systask_error_handler_t error_handler) {
systimer_init(); 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(int exitcode) { systask_exit(NULL, exitcode); }
void system_exit_error_ex(const char* title, size_t title_len, void system_exit_error_ex(const char* title, size_t title_len,

View File

@ -34,6 +34,8 @@ void system_init(systask_error_handler_t error_handler) {
systimer_init(); systimer_init();
} }
void system_deinit(void) { systick_deinit(); }
void system_exit(int exitcode) { void system_exit(int exitcode) {
if (g_error_handler != NULL) { if (g_error_handler != NULL) {
systask_postmortem_t pminfo = {0}; systask_postmortem_t pminfo = {0};

View File

@ -36,6 +36,7 @@ def configure(
("HW_REVISION", str(hw_revision)), ("HW_REVISION", str(hw_revision)),
("HSE_VALUE", "32000000"), ("HSE_VALUE", "32000000"),
("USE_HSE", "1"), ("USE_HSE", "1"),
("FIXED_HW_DEINIT", "1"),
] ]
sources += [ sources += [