Skip to content

Commit

Permalink
chore(core): refactor boot_args
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
cepetr committed Nov 14, 2023
1 parent 7c6a6b7 commit bb6f9d8
Show file tree
Hide file tree
Showing 30 changed files with 223 additions and 128 deletions.
1 change: 1 addition & 0 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ SOURCE_BOOTLOADER = [
]

SOURCE_TREZORHAL = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/display-unix.c',
'embed/trezorhal/unix/flash.c',
'embed/trezorhal/unix/common.c',
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ SOURCE_MICROPYTHON = [
]

SOURCE_UNIX = [
'embed/trezorhal/unix/boot_args.c',
'embed/trezorhal/unix/common.c',
'embed/trezorhal/unix/display-unix.c',
'embed/trezorhal/unix/flash.c',
Expand Down
2 changes: 1 addition & 1 deletion core/embed/boardloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ int main(void) {

mpu_config_off();

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

return 0;
Expand Down
14 changes: 10 additions & 4 deletions core/embed/boardloader/memory_stm32u5a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ENTRY(reset_handler)

MEMORY {
FLASH (rx) : ORIGIN = 0x0C004000, LENGTH = 48K
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - (0x100 + 4)
BOOT_ARGS (wal) : ORIGIN = 0x300BFEFC, LENGTH = (0x100 + 4)
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - 0x100
BOOT_ARGS (wal) : ORIGIN = 0x300BFF00, LENGTH = 0x100
SRAM2 (wal) : ORIGIN = 0x300C0000, LENGTH = 64K
SRAM3 (wal) : ORIGIN = 0x300D0000, LENGTH = 832K
SRAM5 (wal) : ORIGIN = 0x301A0000, LENGTH = 832K
Expand Down Expand Up @@ -44,8 +44,6 @@ sram6_end = ORIGIN(SRAM6) + LENGTH(SRAM6);
/* reserve 256 bytes for bootloader arguments */
boot_args_start = ORIGIN(BOOT_ARGS);
boot_args_end = ORIGIN(BOOT_ARGS) + LENGTH(BOOT_ARGS);
g_boot_args = (boot_args_start + 4);
g_boot_flag = boot_args_start;

SECTIONS {
.vector_table : ALIGN(512) {
Expand Down Expand Up @@ -104,6 +102,14 @@ SECTIONS {
. = ALIGN(4);
} >SRAM5

.boot_args : ALIGN(8) {
*(.boot_command*);
. = ALIGN(8);
*(.boot_args*);
. = ALIGN(8);
} >BOOT_ARGS


/* Hard-coded address for capabilities structure */
.capabilities 0x0C00FF00 : {KEEP(*(.capabilities_section))}
}
19 changes: 0 additions & 19 deletions core/embed/bootloader/boot_internal.h

This file was deleted.

19 changes: 8 additions & 11 deletions core/embed/bootloader/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <unistd.h>

#include TREZOR_BOARD
#include "boot_internal.h"
#include "boot_args.h"
#include "bootui.h"
#include "common.h"
#include "display.h"
Expand All @@ -19,11 +19,6 @@

uint8_t *FIRMWARE_START = 0;

// Simulation of a boot command normally grabbed during reset processing
boot_command_t g_boot_command = BOOT_COMMAND_NONE;
// Simulation of a boot args normally sitting at the BOOT_ARGS region
uint8_t g_boot_args[BOOT_ARGS_SIZE];

void set_core_clock(int) {}

int bootloader_main(void);
Expand Down Expand Up @@ -55,7 +50,7 @@ void usage(void) {
printf(" -h show this help\n");
}

bool load_firmware(const char *filename) {
bool load_firmware(const char *filename, uint8_t *hash) {
// read the first 6 kB of firmware file into a buffer
FILE *file = fopen(filename, "rb");
if (!file) {
Expand Down Expand Up @@ -87,7 +82,8 @@ bool load_firmware(const char *filename) {
BLAKE2S_CTX ctx;
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
blake2s_Update(&ctx, buffer, vhdr.hdrlen + hdr->hdrlen);
blake2s_Final(&ctx, g_boot_args, BLAKE2S_DIGEST_LENGTH);
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);

return true;
}

Expand Down Expand Up @@ -131,7 +127,7 @@ __attribute__((noreturn)) int main(int argc, char **argv) {
while ((opt = getopt(argc, argv, "hslec:b:f:")) != -1) {
switch (opt) {
case 's':
g_boot_command = BOOT_COMMAND_STOP_AND_WAIT;
bootargs_set(BOOT_COMMAND_STOP_AND_WAIT, NULL, 0);
break;
case 'e':
display_error = true;
Expand All @@ -145,10 +141,11 @@ __attribute__((noreturn)) int main(int argc, char **argv) {
bitcoin_only = atoi(optarg);
break;
case 'f':
g_boot_command = BOOT_COMMAND_INSTALL_UPGRADE;
if (!load_firmware(optarg)) {
uint8_t hash[BLAKE2S_DIGEST_LENGTH];
if (!load_firmware(optarg, hash)) {
exit(1);
}
bootargs_set(BOOT_COMMAND_INSTALL_UPGRADE, hash, sizeof(hash));
break;
#ifdef USE_OPTIGA
case 'l':
Expand Down
4 changes: 2 additions & 2 deletions core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string.h>
#include <sys/types.h>

#include "boot_internal.h"
#include "boot_args.h"
#include "common.h"
#include "display.h"
#include "flash.h"
Expand Down Expand Up @@ -512,7 +512,7 @@ int bootloader_main(void) {
check_bootloader_version();
#endif

switch (g_boot_command) {
switch (bootargs_get_command()) {
case BOOT_COMMAND_STOP_AND_WAIT:
// firmare requested to stay in bootloader
stay_in_bootloader = sectrue;
Expand Down
6 changes: 5 additions & 1 deletion core/embed/bootloader/memory_stm32f4.ld
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ sram_end = ORIGIN(SRAM) + LENGTH(SRAM);
/* reserve 256 bytes for bootloader arguments */
boot_args_start = ORIGIN(BOOT_ARGS);
boot_args_end = ORIGIN(BOOT_ARGS) + LENGTH(BOOT_ARGS);
g_boot_args = boot_args_start;

_codelen = SIZEOF(.flash) + SIZEOF(.data);

Expand Down Expand Up @@ -70,4 +69,9 @@ SECTIONS {
. = ALIGN(4);
} >SRAM

.boot_args : ALIGN(8) {
*(.boot_args*);
. = ALIGN(8);
} >BOOT_ARGS

}
13 changes: 9 additions & 4 deletions core/embed/bootloader/memory_stm32u5a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ENTRY(reset_handler)

MEMORY {
FLASH (rx) : ORIGIN = 0x0C010000, LENGTH = 128K
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - (0x100 + 4)
BOOT_ARGS (wal) : ORIGIN = 0x300BFEFC, LENGTH = (0x100 + 4)
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - 0x100
BOOT_ARGS (wal) : ORIGIN = 0x300BFF00, LENGTH = 0x100
SRAM2 (wal) : ORIGIN = 0x300C0000, LENGTH = 64K
SRAM3 (wal) : ORIGIN = 0x300D0000, LENGTH = 832K
SRAM5 (wal) : ORIGIN = 0x301A0000, LENGTH = 832K
Expand Down Expand Up @@ -44,8 +44,6 @@ sram6_end = ORIGIN(SRAM6) + LENGTH(SRAM6);
/* reserve 256 bytes for bootloader arguments */
boot_args_start = ORIGIN(BOOT_ARGS);
boot_args_end = ORIGIN(BOOT_ARGS) + LENGTH(BOOT_ARGS);
g_boot_args = (boot_args_start + 4);
g_boot_flag = boot_args_start;

_codelen = SIZEOF(.flash) + SIZEOF(.data) + SIZEOF(.sensitive);

Expand Down Expand Up @@ -104,4 +102,11 @@ SECTIONS {
__fb_end = .;
. = ALIGN(4);
} >SRAM5

.boot_args : ALIGN(8) {
*(.boot_command*);
. = ALIGN(8);
*(.boot_args*);
. = ALIGN(8);
} >BOOT_ARGS
}
6 changes: 3 additions & 3 deletions core/embed/bootloader/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <pb_encode.h>
#include "messages.pb.h"

#include "boot_internal.h"
#include "boot_args.h"
#include "common.h"
#include "flash.h"
#include "image.h"
Expand Down Expand Up @@ -598,7 +598,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,

secbool is_ilu = secfalse; // interaction-less update

if (g_boot_command == BOOT_COMMAND_INSTALL_UPGRADE) {
if (bootargs_get_command() == BOOT_COMMAND_INSTALL_UPGRADE) {
BLAKE2S_CTX ctx;
uint8_t hash[BLAKE2S_DIGEST_LENGTH];
blake2s_Init(&ctx, BLAKE2S_DIGEST_LENGTH);
Expand All @@ -607,7 +607,7 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,
blake2s_Final(&ctx, hash, BLAKE2S_DIGEST_LENGTH);

// the firmware must be the same as confirmed by the user
if (memcmp(&g_boot_args[0], hash, sizeof(hash)) != 0) {
if (memcmp(bootargs_get_args()->hash, hash, sizeof(hash)) != 0) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ProcessError);
MSG_SEND_ASSIGN_STRING(message, "Firmware mismatch");
Expand Down
11 changes: 2 additions & 9 deletions core/embed/bootloader/startup_stm32f4.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,15 @@ reset_handler:
// subsequent operations, it is not necessary to insert a memory barrier instruction."
cpsie f

// r11 contains argument passed to reboot_to_bootloader()
// r11 contains the command passed to bootargs_set()
// function called when the firmware rebooted to the bootloader
ldr r0, =g_boot_command
ldr r0, =g_boot_command_shadow
str r11, [r0]

// enter the application code
bl main

b shutdown_privileged

.bss

.global g_boot_command
g_boot_command:
.word 0


.end

14 changes: 4 additions & 10 deletions core/embed/bootloader/startup_stm32u5.s
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ reset_handler:
ldr r1, = __stack_chk_guard
str r0, [r1]

//
ldr r0, =g_boot_flag
ldr r1, [r0]
// copy & clear g_boot_command
ldr r0, =g_boot_command
ldr r1, [r0]
ldr r0, =g_boot_command_shadow
str r1, [r0]
ldr r0, =g_boot_flag
ldr r0, =g_boot_command
mov r1, #0
str r1, [r0]

Expand All @@ -69,10 +69,4 @@ reset_handler:

b shutdown_privileged

.bss

.global g_boot_command
g_boot_command:
.word 0

.end
4 changes: 4 additions & 0 deletions core/embed/bootloader_ci/memory_stm32f4.ld
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ SECTIONS {
. = ALIGN(4);
} >SRAM

.boot_args : ALIGN(8) {
*(.boot_args*);
. = ALIGN(8);
} >BOOT_ARGS
}
13 changes: 9 additions & 4 deletions core/embed/bootloader_ci/memory_stm32u5a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ENTRY(reset_handler)

MEMORY {
FLASH (rx) : ORIGIN = 0x0C010000, LENGTH = 128K
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - (0x100 + 4)
BOOT_ARGS (wal) : ORIGIN = 0x300BFEFC, LENGTH = (0x100 + 4)
SRAM1 (wal) : ORIGIN = 0x30000000, LENGTH = 768K - 0x100
BOOT_ARGS (wal) : ORIGIN = 0x300BFF00, LENGTH = 0x100
SRAM2 (wal) : ORIGIN = 0x300C0000, LENGTH = 64K
SRAM3 (wal) : ORIGIN = 0x300D0000, LENGTH = 832K
SRAM5 (wal) : ORIGIN = 0x301A0000, LENGTH = 832K
Expand Down Expand Up @@ -44,8 +44,6 @@ sram6_end = ORIGIN(SRAM6) + LENGTH(SRAM6);
/* reserve 256 bytes for bootloader arguments */
boot_args_start = ORIGIN(BOOT_ARGS);
boot_args_end = ORIGIN(BOOT_ARGS) + LENGTH(BOOT_ARGS);
g_boot_args = (boot_args_start + 4);
g_boot_flag = boot_args_start;

_codelen = SIZEOF(.flash) + SIZEOF(.data) + SIZEOF(.sensitive);

Expand Down Expand Up @@ -104,4 +102,11 @@ SECTIONS {
__fb_end = .;
. = ALIGN(4);
} >SRAM5

.boot_args : ALIGN(8) {
*(.boot_command*);
. = ALIGN(8);
*(.boot_args*);
. = ALIGN(8);
} >BOOT_ARGS
}
15 changes: 0 additions & 15 deletions core/embed/bootloader_ci/startup_stm32u5.s
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ reset_handler:
ldr r1, = __stack_chk_guard
str r0, [r1]

//
ldr r0, =g_boot_flag
ldr r1, [r0]
ldr r0, =g_boot_command
str r1, [r0]
ldr r0, =g_boot_flag
mov r1, #0
str r1, [r0]

// re-enable exceptions
// according to "ARM Cortex-M Programming Guide to Memory Barrier Instructions" Application Note 321, section 4.7:
// "If it is not necessary to ensure that a pended interrupt is recognized immediately before
Expand All @@ -69,10 +60,4 @@ reset_handler:

b shutdown_privileged

.bss

.global g_boot_command
g_boot_command:
.word 0

.end
3 changes: 2 additions & 1 deletion core/embed/extmod/modtrezorutils/modtrezorutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ STATIC mp_obj_t mod_trezorutils_reboot_to_bootloader(size_t n_args,
mp_get_buffer_raise(args[1], &boot_args, MP_BUFFER_READ);
}

svc_reboot_to_bootloader(boot_command, boot_args.buf, boot_args.len);
bootargs_set(boot_command, boot_args.buf, boot_args.len);
svc_reboot_to_bootloader();
#endif
return mp_const_none;
}
Expand Down
Loading

0 comments on commit bb6f9d8

Please sign in to comment.