Skip to content

Commit

Permalink
fix(core): fix bootloader update on STM32U5
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
TychoVrahe committed Nov 24, 2023
1 parent c30d2b7 commit ffd88a3
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
4 changes: 2 additions & 2 deletions core/embed/bootloader/messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ int process_msg_FirmwareUpload(uint8_t iface_num, uint32_t msg_size,
// write a burst (8 * quadword (16 bytes)) to the flash
ensure(flash_area_write_burst(&FIRMWARE_AREA, write_offset, quadword_ptr),
NULL);
write_offset += 8 * 4 * sizeof(uint32_t);
quadword_ptr += 8 * 4;
write_offset += FLASH_BURST_LENGTH * sizeof(uint32_t);
quadword_ptr += FLASH_BURST_LENGTH;
}
ensure(flash_lock_write(), NULL);

Expand Down
18 changes: 10 additions & 8 deletions core/embed/firmware/bl_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,27 @@ void check_and_replace_bootloader(void) {

do {
uint32_t *p = decomp_out;
uint32_t last_whole_word_addr = (((uint32_t)decomp.dest) & ~3);
uint32_t last_whole_word_addr = (((uint32_t)decomp.dest) & ~0x7F);
while ((uint32_t)p < last_whole_word_addr) {
ensure(flash_area_write_word(&BOOTLOADER_AREA, offset, *p++), NULL);
offset += sizeof(uint32_t);
ensure(flash_area_write_burst(&BOOTLOADER_AREA, offset, p), NULL);
p += FLASH_BURST_LENGTH;
offset += FLASH_BURST_LENGTH * sizeof(uint32_t);
}
if ((uint8_t *)p < decomp.dest) {
// last few bytes in case of unaligned data
uint32_t d = 0;
uint32_t d[FLASH_BURST_LENGTH] = {0};
memcpy(&d, p, (uint32_t)decomp.dest - (uint32_t)p);
ensure(flash_area_write_word(&BOOTLOADER_AREA, offset, d), NULL);
offset += sizeof(uint32_t);
ensure(flash_area_write_burst(&BOOTLOADER_AREA, offset, d), NULL);
offset += FLASH_BURST_LENGTH * sizeof(uint32_t);
}
decomp.dest = (uint8_t *)decomp_out;
} while (uzlib_uncompress(&decomp) >= 0);

uint32_t d[FLASH_BURST_LENGTH] = {0};
// fill the rest of the bootloader area with 0x00
while (offset < bl_len) {
ensure(flash_area_write_word(&BOOTLOADER_AREA, offset, 0x00000000), NULL);
offset += sizeof(uint32_t);
ensure(flash_area_write_burst(&BOOTLOADER_AREA, offset, d), NULL);
offset += FLASH_BURST_LENGTH * sizeof(uint32_t);
}

ensure(flash_lock_write(), NULL);
Expand Down
10 changes: 6 additions & 4 deletions core/embed/firmware/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ int main(void) {

display_reinit();

#ifdef USE_DMA2D
dma2d_init();
#endif

#if !defined TREZOR_MODEL_1
parse_boardloader_capabilities();

Expand All @@ -130,6 +134,8 @@ int main(void) {
secbool secret_ok = secret_optiga_extract(secret);
#endif

mpu_config_firmware_initial();

#if PRODUCTION || BOOTLOADER_QA
check_and_replace_bootloader();
#endif
Expand All @@ -140,10 +146,6 @@ int main(void) {
// Init peripherals
pendsv_init();

#ifdef USE_DMA2D
dma2d_init();
#endif

fault_handlers_init();

#if defined TREZOR_MODEL_T
Expand Down
2 changes: 2 additions & 0 deletions core/embed/trezorhal/flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

#include "flash_ll.h"

#define FLASH_BURST_LENGTH (4 * 8)

void flash_init(void);

#endif // TREZORHAL_FLASH_H
1 change: 1 addition & 0 deletions core/embed/trezorhal/mpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
void mpu_config_off(void);
void mpu_config_boardloader(void);
void mpu_config_bootloader(void);
void mpu_config_firmware_initial(void);
void mpu_config_firmware(void);
void mpu_config_prodtest(void);

Expand Down
2 changes: 2 additions & 0 deletions core/embed/trezorhal/stm32f4/mpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ void mpu_config_bootloader(void) {
HAL_MPU_Enable(LL_MPU_CTRL_HARDFAULT_NMI);
}

void mpu_config_firmware_initial(void) {}

void mpu_config_firmware(void) {
// Disable MPU
HAL_MPU_Disable();
Expand Down
19 changes: 19 additions & 0 deletions core/embed/trezorhal/stm32u5/mpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ static void mpu_set_attributes() {
(FLASH_SIZE - (FIRMWARE_START + BOOTLOADER_SIZE + BOARDLOADER_SIZE + \
SECRET_SIZE + STORAGE_SIZE))

#define L3_PREV_SIZE_BLD (STORAGE_SIZE + BOOTLOADER_SIZE)

#ifdef STM32U585xx
#define GRAPHICS_START FMC_BANK1
#define GRAPHICS_SIZE SIZE_16M
Expand Down Expand Up @@ -181,6 +183,23 @@ void mpu_config_bootloader() {
HAL_MPU_Enable(LL_MPU_CTRL_HARDFAULT_NMI);
}

void mpu_config_firmware_initial() {
HAL_MPU_Disable();
mpu_set_attributes();
// clang-format off
// REGION ADDRESS SIZE TYPE WRITE UNPRIV
SET_REGION( 0, BOOTLOADER_START, L3_PREV_SIZE_BLD, FLASH_DATA, YES, YES ); // Bootloader + Storage
SET_REGION( 1, FIRMWARE_START, FIRMWARE_SIZE, FLASH_CODE, NO, YES ); // Firmware
SET_REGION( 2, L3_REST_START, L3_REST_SIZE, FLASH_DATA, YES, YES ); // Reserve
SET_REGION( 3, SRAM1_BASE, SRAM_SIZE, SRAM, YES, YES ); // SRAM1/2/3/5
SET_REGION( 4, GRAPHICS_START, GRAPHICS_SIZE, SRAM, YES, YES ); // Frame buffer or display interface
SET_REGION( 5, PERIPH_BASE_NS, SIZE_512M, PERIPHERAL, YES, YES ); // Peripherals
SET_REGION( 6, FLASH_OTP_BASE, FLASH_OTP_SIZE, FLASH_DATA, YES, YES ); // OTP
DIS_REGION( 7 );
// clang-format on
HAL_MPU_Enable(LL_MPU_CTRL_HARDFAULT_NMI);
}

void mpu_config_firmware() {
HAL_MPU_Disable();
mpu_set_attributes();
Expand Down

0 comments on commit ffd88a3

Please sign in to comment.