1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-01 11:58:28 +00:00

refactor(core/embed): improve bootuils api

[no changelog]
This commit is contained in:
cepetr 2024-08-16 13:33:58 +02:00 committed by cepetr
parent 0666e6ea9a
commit b1a41ded79
8 changed files with 35 additions and 26 deletions

View File

@ -308,7 +308,7 @@ STATIC mp_obj_t mod_trezorutils_reboot_to_bootloader(size_t n_args,
} }
bootargs_set(boot_command, boot_args.buf, boot_args.len); bootargs_set(boot_command, boot_args.buf, boot_args.len);
svc_reboot_to_bootloader(); reboot_to_bootloader();
#endif #endif
return mp_const_none; return mp_const_none;
} }

View File

@ -58,7 +58,7 @@ error_shutdown_ex(const char *title, const char *message, const char *footer) {
} }
term_printf("\n%s\n", footer); term_printf("\n%s\n", footer);
display_backlight(255); display_backlight(255);
trezor_shutdown(); secure_shutdown();
#endif #endif
} }
@ -99,7 +99,7 @@ __fatal_error(const char *msg, const char *file, int line) {
#endif #endif
term_printf("\nPlease contact Trezor support.\n"); term_printf("\nPlease contact Trezor support.\n");
display_backlight(255); display_backlight(255);
trezor_shutdown(); secure_shutdown();
#endif #endif
} }

View File

@ -763,7 +763,7 @@ static void test_otp_write_device_variant(const char *args) {
vcp_println("OK"); vcp_println("OK");
} }
static void test_reboot(void) { svc_reboot(); } static void test_reboot(void) { reboot(); }
void cpuid_read(void) { void cpuid_read(void) {
uint32_t cpuid[3]; uint32_t cpuid[3];

View File

@ -1,7 +1,7 @@
mod ffi { mod ffi {
extern "C" { extern "C" {
// trezorhal/common.c // trezorhal/bootuils.c
pub fn trezor_shutdown() -> !; pub fn secure_shutdown() -> !;
} }
} }
@ -11,7 +11,7 @@ use crate::ui::{
}; };
fn shutdown() -> ! { fn shutdown() -> ! {
unsafe { ffi::trezor_shutdown() } unsafe { ffi::secure_shutdown() }
} }
/// Shows an error message and shuts down the device. /// Shows an error message and shuts down the device.

View File

@ -4,7 +4,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
// Defines boot command for 'svc_reboot_to_bootloader()' function // Defines boot command for 'reboot_to_bootloader()' function
typedef enum { typedef enum {
// Normal boot sequence // Normal boot sequence
BOOT_COMMAND_NONE = 0x00000000, BOOT_COMMAND_NONE = 0x00000000,
@ -36,8 +36,15 @@ boot_command_t bootargs_get_command();
// Returns the pointer to boot arguments // Returns the pointer to boot arguments
const boot_args_t* bootargs_get_args(); const boot_args_t* bootargs_get_args();
void __attribute__((noreturn)) trezor_shutdown(void); // Reboots the device into the bootloader.
void __attribute__((noreturn)) svc_reboot_to_bootloader(void); // The bootloader will read the command set by `bootargs_set()`.
void __attribute__((noreturn)) svc_reboot(void); void __attribute__((noreturn)) reboot_to_bootloader(void);
// Causes immediate reset of the device.
void __attribute__((noreturn)) reboot(void);
// Safely shuts down the device (clears secrets, memory, etc.).
// This function is called when the device is in an unrecoverable state.
void __attribute__((noreturn)) secure_shutdown(void);
#endif // TREZORHAL_BOOTUTILS_H #endif // TREZORHAL_BOOTUTILS_H

View File

@ -11,13 +11,13 @@
// to the bootloader. // to the bootloader.
// 1. In the bootloader, its value is set in the startup code. // 1. In the bootloader, its value is set in the startup code.
// 2. In the firmware it holds command for the next boot and it is used // 2. In the firmware it holds command for the next boot and it is used
// when svc_reboot_to_bootloader() is called // when reboot_to_bootloader() is called
boot_command_t g_boot_command_shadow; boot_command_t g_boot_command_shadow;
#ifdef STM32U5 #ifdef STM32U5
// The 'g_boot_command' is persistent variable that holds the 'command' // The 'g_boot_command' is persistent variable that holds the 'command'
// for the next reboot/jump to the bootloader. Its value is set to // for the next reboot/jump to the bootloader. Its value is set to
// g_boot_command_shadow when 'svc_reboot_to_bootloader()' is called. // g_boot_command_shadow when 'reboot_to_bootloader()' is called.
boot_command_t __attribute__((section(".boot_command"))) g_boot_command; boot_command_t __attribute__((section(".boot_command"))) g_boot_command;
#endif #endif
@ -47,7 +47,7 @@ boot_command_t bootargs_get_command() { return g_boot_command_shadow; }
const boot_args_t* bootargs_get_args() { return &g_boot_args; } const boot_args_t* bootargs_get_args() { return &g_boot_args; }
void __attribute__((noreturn)) trezor_shutdown(void) { void __attribute__((noreturn)) secure_shutdown(void) {
display_deinit(DISPLAY_RETAIN_CONTENT); display_deinit(DISPLAY_RETAIN_CONTENT);
#if defined(STM32U5) #if defined(STM32U5)
@ -63,7 +63,7 @@ void __attribute__((noreturn)) trezor_shutdown(void) {
; ;
} }
void svc_reboot_to_bootloader(void) { void reboot_to_bootloader(void) {
boot_command_t boot_command = bootargs_get_command(); boot_command_t boot_command = bootargs_get_command();
display_deinit(DISPLAY_RESET_CONTENT); display_deinit(DISPLAY_RESET_CONTENT);
#ifdef ENSURE_COMPATIBLE_SETTINGS #ifdef ENSURE_COMPATIBLE_SETTINGS
@ -83,4 +83,4 @@ void svc_reboot_to_bootloader(void) {
#endif #endif
} }
void svc_reboot(void) { NVIC_SystemReset(); } void reboot(void) { NVIC_SystemReset(); }

View File

@ -1,6 +1,8 @@
#include "../bootutils.h" #include "../bootutils.h"
#include <common.h> #include <common.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
// The 'g_boot_command_shadow' variable stores the 'command' for the next // The 'g_boot_command_shadow' variable stores the 'command' for the next
@ -42,3 +44,13 @@ void bootargs_clear() {
boot_command_t bootargs_get_command() { return g_boot_command_shadow; } boot_command_t bootargs_get_command() { return g_boot_command_shadow; }
const boot_args_t* bootargs_get_args() { return &g_boot_args; } const boot_args_t* bootargs_get_args() { return &g_boot_args; }
void __attribute__((noreturn)) secure_shutdown(void) {
printf("SHUTDOWN\n");
// Wait some time to let the user see the displayed
// message before shutting down
hal_delay(3000);
exit(3);
}

View File

@ -30,16 +30,6 @@
void __attribute__((noreturn)) main_clean_exit(); void __attribute__((noreturn)) main_clean_exit();
void __attribute__((noreturn)) trezor_shutdown(void) {
printf("SHUTDOWN\n");
// Wait some time to let the user see the displayed
// message before shutting down
hal_delay(3000);
exit(3);
}
static int SDLCALL emulator_event_filter(void *userdata, SDL_Event *event) { static int SDLCALL emulator_event_filter(void *userdata, SDL_Event *event) {
switch (event->type) { switch (event->type) {
case SDL_QUIT: case SDL_QUIT: