Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Startup/shutdown refactoring #4499

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions core/SConscript.boardloader
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ env = Environment(ENV=os.environ,

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_BOARDLOADER = [
f"embed/sys/startup/{FILE_SUFFIX}/startup_stage_0.s",
'embed/projects/boardloader/main.c',
]

Expand Down
3 changes: 0 additions & 3 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_BOOTLOADER = [
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_1.s',
'embed/projects/bootloader/header.S',
'embed/projects/bootloader/bootui.c',
'embed/projects/bootloader/main.c',
Expand Down
3 changes: 0 additions & 3 deletions core/SConscript.bootloader_ci
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,7 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_BOOTLOADER = [
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_1.s',
'embed/projects/bootloader_ci/header.S',
'embed/projects/bootloader_ci/bootui.c',
'embed/projects/bootloader_ci/main.c',
Expand Down
3 changes: 0 additions & 3 deletions core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,11 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_FIRMWARE = [
'embed/projects/firmware/header.S',
'embed/projects/firmware/main.c',
'embed/projects/firmware/mphalport.c',
'embed/projects/firmware/nlrthumb.c',
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_4.s',
]

if 'sd_card' in FEATURES_AVAILABLE:
Expand Down
3 changes: 0 additions & 3 deletions core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,8 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_FIRMWARE = [
'embed/projects/kernel/main.c',
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_2.s',
]

if 'sd_card' in FEATURES_AVAILABLE:
Expand Down
4 changes: 0 additions & 4 deletions core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']


SOURCE_PRODTEST = [
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_2.s',
'embed/projects/prodtest/header.S',
'embed/projects/prodtest/main.c',
'embed/projects/prodtest/prodtest_common.c',
Expand Down
3 changes: 0 additions & 3 deletions core/SConscript.reflash
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ env = Environment(

FEATURES_AVAILABLE = models.configure_board(TREZOR_MODEL, HW_REVISION, FEATURES_WANTED, env, CPPDEFINES_HAL, SOURCE_HAL, PATH_HAL)

FILE_SUFFIX= env.get('ENV')['SUFFIX']

SOURCE_REFLASH = [
f'embed/sys/startup/{FILE_SUFFIX}/startup_stage_2.s',
'embed/projects/reflash/header.S',
'embed/projects/reflash/main.c',
]
Expand Down
13 changes: 4 additions & 9 deletions core/embed/io/display/ltdc_dsi/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ bool display_init(display_content_mode_t mode) {

__HAL_LTDC_ENABLE_IT(&drv->hlcd_ltdc, LTDC_IT_LI | LTDC_IT_FU | LTDC_IT_TE);

gfx_bitblt_init();

drv->initialized = true;
return true;

Expand All @@ -425,17 +427,10 @@ bool display_init(display_content_mode_t mode) {
void display_deinit(display_content_mode_t mode) {
display_driver_t *drv = &g_display_driver;

if (mode == DISPLAY_RETAIN_CONTENT) {
// This is a temporary workaround for T3W1 to avoid clearing
// the display after drawing RSOD screen in `secure_shutdown()`
// function. The workaround should be removed once we have
// proper replacement for `secure_shutdown()` that resets the
// device instead of waiting for manual power off.
return;
}

GPIO_InitTypeDef GPIO_InitStructure = {0};

gfx_bitblt_deinit();

NVIC_DisableIRQ(LTDC_IRQn);
NVIC_DisableIRQ(LTDC_ER_IRQn);

Expand Down
4 changes: 4 additions & 0 deletions core/embed/io/display/st-7789/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ bool display_init(display_content_mode_t mode) {
#endif
#endif

gfx_bitblt_init();

drv->initialized = true;
return true;
}
Expand All @@ -110,6 +112,8 @@ void display_deinit(display_content_mode_t mode) {
#endif
#endif

gfx_bitblt_deinit();

mpu_set_active_fb(NULL, 0);

backlight_pwm_deinit(mode == DISPLAY_RESET_CONTENT ? BACKLIGHT_RESET
Expand Down
4 changes: 4 additions & 0 deletions core/embed/io/display/stm32f429i-disc1/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ bool display_init(display_content_mode_t mode) {
ili9341_init();
}

gfx_bitblt_init();

drv->initialized = true;
return true;
}

void display_deinit(display_content_mode_t mode) {
display_driver_t *drv = &g_display_driver;

gfx_bitblt_deinit();

mpu_set_active_fb(NULL, 0);

drv->initialized = false;
Expand Down
5 changes: 5 additions & 0 deletions core/embed/io/display/unix/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ bool display_init(display_content_mode_t mode) {
#else
drv->orientation_angle = 0;
#endif

gfx_bitblt_init();

drv->initialized = true;
return true;
}
Expand All @@ -189,6 +192,8 @@ void display_deinit(display_content_mode_t mode) {
return;
}

gfx_bitblt_deinit();

SDL_FreeSurface(drv->prev_saved);
SDL_FreeSurface(drv->buffer);
if (drv->background != NULL) {
Expand Down
4 changes: 4 additions & 0 deletions core/embed/io/display/vg-2864/display_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ bool display_init(display_content_mode_t mode) {
display_init_spi(drv);
}

gfx_bitblt_init();

drv->initialized = true;
return true;
}
Expand All @@ -327,6 +329,8 @@ void display_deinit(display_content_mode_t mode) {

mpu_set_active_fb(NULL, 0);

gfx_bitblt_deinit();

drv->initialized = false;
}

Expand Down
5 changes: 1 addition & 4 deletions core/embed/projects/boardloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ static void drivers_init(void) {
#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
gfx_bitblt_init();
display_init(DISPLAY_RESET_CONTENT);
#ifdef USE_SD_CARD
sdcard_init();
Expand All @@ -103,12 +102,10 @@ static void drivers_deinit(void) {
#ifdef FIXED_HW_DEINIT
// TODO
#endif
gfx_bitblt_deinit();
display_deinit(DISPLAY_JUMP_BEHAVIOR);
#ifdef USE_POWERCTL
powerctl_deinit();
#endif
ensure_compatible_settings();
}

static uint8_t get_bootloader_min_version(void) {
Expand Down Expand Up @@ -332,7 +329,7 @@ int main(void) {
system_deinit();

// g_boot_command is preserved on STM32U5
jump_to(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE));
jump_to_next_stage(IMAGE_CODE_ALIGN(BOOTLOADER_START + IMAGE_HEADER_SIZE));

return 0;
}
9 changes: 6 additions & 3 deletions core/embed/projects/bootloader/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <io/display.h>
#include <sys/bootargs.h>
#include <sys/bootutils.h>
#include <sys/systick.h>
#include <util/flash.h>
#include <util/flash_otp.h>
Expand Down Expand Up @@ -111,7 +112,7 @@ static int sdl_event_filter(void *userdata, SDL_Event *event) {
return 1;
}

__attribute__((noreturn)) int main(int argc, char **argv) {
int main(int argc, char **argv) {
SDL_SetEventFilter(sdl_event_filter, NULL);

display_init(DISPLAY_RESET_CONTENT);
Expand Down Expand Up @@ -189,10 +190,12 @@ __attribute__((noreturn)) int main(int argc, char **argv) {

bootloader_main();
hal_delay(3000);
jump_to(0);
jump_to_next_stage(0);

return 0;
}

void jump_to(uint32_t address) {
void jump_to_next_stage(uint32_t address) {
bool storage_is_erased =
storage_empty(&STORAGE_AREAS[0]) && storage_empty(&STORAGE_AREAS[1]);

Expand Down
2 changes: 0 additions & 2 deletions core/embed/projects/bootloader/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@

extern uint8_t *FIRMWARE_START;

__attribute__((noreturn)) void jump_to(uint32_t address);

#endif
6 changes: 2 additions & 4 deletions core/embed/projects/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ static void drivers_init(secbool *touch_initialized) {
#ifdef USE_HASH_PROCESSOR
hash_processor_init();
#endif
gfx_bitblt_init();
display_init(DISPLAY_JUMP_BEHAVIOR);
unit_properties_init();

Expand Down Expand Up @@ -138,9 +137,7 @@ static void drivers_deinit(void) {
button_deinit();
#endif
#endif
gfx_bitblt_deinit();
display_deinit(DISPLAY_JUMP_BEHAVIOR);
ensure_compatible_settings();
}

static void usb_init_all(secbool usb21_landing) {
Expand Down Expand Up @@ -381,7 +378,8 @@ void real_jump_to_firmware(void) {

system_deinit();

jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));
jump_to_next_stage(
IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));
}

#ifdef USE_RESET_TO_BOOT
Expand Down
3 changes: 2 additions & 1 deletion core/embed/projects/bootloader_ci/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ int main(void) {

system_deinit();

jump_to(IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));
jump_to_next_stage(
IMAGE_CODE_ALIGN(FIRMWARE_START + vhdr.hdrlen + IMAGE_HEADER_SIZE));

return 0;
}
29 changes: 27 additions & 2 deletions core/embed/projects/firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "ports/stm32/gccollect.h"
#include "ports/stm32/pendsv.h"

#include <sys/linker_utils.h>
#include <sys/systask.h>
#include <sys/system.h>
#include <util/rsod.h>
Expand All @@ -41,6 +42,10 @@
#include "zkp_context.h"
#endif

// symbols defined in the linker script
extern uint8_t _stack_section_start;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can move the extens to linker_utils?

extern uint8_t _stack_section_end;

int main(uint32_t cmd, void *arg) {
if (cmd == 1) {
systask_postmortem_t *info = (systask_postmortem_t *)arg;
Expand All @@ -57,8 +62,9 @@ int main(uint32_t cmd, void *arg) {
printf("CORE: Preparing stack\n");
// Stack limit should be less than real stack size, so we have a chance
// to recover from limit hit.
mp_stack_set_top(&_estack);
mp_stack_set_limit((char *)&_estack - (char *)&_sstack - 1024);
mp_stack_set_top(&_stack_section_end);
mp_stack_set_limit((char *)&_stack_section_end -
(char *)&_stack_section_start - 1024);

#if MICROPY_ENABLE_PYSTACK
static mp_obj_t pystack[1024];
Expand Down Expand Up @@ -106,3 +112,22 @@ mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);

// `reset_handler` is the application entry point (first routine called
// from kernel)
__attribute((no_stack_protector)) void reset_handler(uint32_t cmd, void *arg,
uint32_t random_value) {
// Initialize linker script defined sections (.bss, .data, ...)
init_linker_sections();

// Initialize stack protector
extern uint32_t __stack_chk_guard;
__stack_chk_guard = random_value;

// Now everything is perfectly initialized and we can do anything
// in C code

int main_result = main(cmd, arg);

system_exit(main_result);
}
16 changes: 8 additions & 8 deletions core/embed/projects/kernel/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ void drivers_init() {
hash_processor_init();
#endif

gfx_bitblt_init();

display_init(DISPLAY_JUMP_BEHAVIOR);

#ifdef USE_OEM_KEYS_CHECK
Expand Down Expand Up @@ -222,8 +220,8 @@ static void show_rsod(const systask_postmortem_t *pminfo) {
applet_run(&coreapp);

if (coreapp.task.pminfo.reason == TASK_TERM_REASON_EXIT) {
// If the RSOD was shown successfully, proceed to shutdown
secure_shutdown();
// RSOD was shown successfully
return;
}
}
#endif
Expand All @@ -245,8 +243,9 @@ static void init_and_show_rsod(const systask_postmortem_t *pminfo) {
// Show RSOD
show_rsod(pminfo);

// Wait for the user to manually power off the device
secure_shutdown();
// Wait for the user to read the RSOD and then reboots
// (or enters an infinite loop if RSOD_INFINITE_LOOP is defined)
reboot_after_rsod();
}

// Kernel panic handler
Expand Down Expand Up @@ -285,8 +284,9 @@ int main(void) {
// Coreapp crashed, show RSOD
show_rsod(&coreapp.task.pminfo);

// Wait for the user to manually power off the device
secure_shutdown();
// Wait for the user to read the RSOD and then reboots
// (or enters an infinite loop if RSOD_INFINITE_LOOP is defined)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the need for this comment indicates that the function name is not ideal

reboot_after_rsod();

return 0;
}
Loading
Loading