From 0d3af6a96ac16a6de29d620c5fd65f6031c868f5 Mon Sep 17 00:00:00 2001 From: tychovrahe Date: Fri, 8 Nov 2024 16:51:57 +0100 Subject: [PATCH] fix(core): fix firmware hashing on U5 [no changelog] --- core/embed/models/D002/memory.ld | 1 - core/embed/models/D002/model_D002.h | 1 - core/embed/models/T3B1/memory.ld | 1 - core/embed/models/T3B1/model_T3B1.h | 1 - core/embed/models/T3T1/memory.ld | 1 - core/embed/models/T3T1/model_T3T1.h | 1 - .../trezorhal/stm32u5/linker/u58/kernel.ld | 2 +- .../trezorhal/stm32u5/linker/u5a/kernel.ld | 2 +- core/embed/trezorhal/stm32u5/mpu.c | 20 +++++++++++-------- 9 files changed, 14 insertions(+), 16 deletions(-) diff --git a/core/embed/models/D002/memory.ld b/core/embed/models/D002/memory.ld index 759ddc4387..642b956a85 100644 --- a/core/embed/models/D002/memory.ld +++ b/core/embed/models/D002/memory.ld @@ -34,7 +34,6 @@ FIRMWARE_SECTOR_START = 0x28; FIRMWARE_SECTOR_END = 0x1f7; KERNEL_START = 0xc050000; KERNEL_MAXSIZE = 0x80000; -KERNEL_U_FLASH_SIZE = 0x200; ASSETS_START = 0xc3f0000; ASSETS_MAXSIZE = 0x10000; ASSETS_SECTOR_START = 0x1f8; diff --git a/core/embed/models/D002/model_D002.h b/core/embed/models/D002/model_D002.h index 4520ccf7ee..bde821b99b 100644 --- a/core/embed/models/D002/model_D002.h +++ b/core/embed/models/D002/model_D002.h @@ -77,7 +77,6 @@ #define FIRMWARE_SECTOR_END 0x1F7 #define KERNEL_START 0x0C050000 #define KERNEL_MAXSIZE (512 * 1024) // 512 kB -#define KERNEL_U_FLASH_SIZE 512 #define ASSETS_START 0x0C3F0000 #define ASSETS_MAXSIZE (8 * 8 * 1024) // 64 kB diff --git a/core/embed/models/T3B1/memory.ld b/core/embed/models/T3B1/memory.ld index 3fd5c061c7..efb3aa5585 100644 --- a/core/embed/models/T3B1/memory.ld +++ b/core/embed/models/T3B1/memory.ld @@ -34,7 +34,6 @@ FIRMWARE_SECTOR_START = 0x28; FIRMWARE_SECTOR_END = 0xf7; KERNEL_START = 0xc050000; KERNEL_MAXSIZE = 0x80000; -KERNEL_U_FLASH_SIZE = 0x200; ASSETS_START = 0xc1f0000; ASSETS_MAXSIZE = 0x10000; ASSETS_SECTOR_START = 0xf8; diff --git a/core/embed/models/T3B1/model_T3B1.h b/core/embed/models/T3B1/model_T3B1.h index 2a904e45da..2d04f00f1b 100644 --- a/core/embed/models/T3B1/model_T3B1.h +++ b/core/embed/models/T3B1/model_T3B1.h @@ -76,7 +76,6 @@ #define FIRMWARE_SECTOR_END 0xF7 #define KERNEL_START 0x0C050000 #define KERNEL_MAXSIZE (512 * 1024) // 512 kB -#define KERNEL_U_FLASH_SIZE 512 #define ASSETS_START 0x0C1F0000 #define ASSETS_MAXSIZE (8 * 8 * 1024) // 64 kB diff --git a/core/embed/models/T3T1/memory.ld b/core/embed/models/T3T1/memory.ld index 3fd5c061c7..efb3aa5585 100644 --- a/core/embed/models/T3T1/memory.ld +++ b/core/embed/models/T3T1/memory.ld @@ -34,7 +34,6 @@ FIRMWARE_SECTOR_START = 0x28; FIRMWARE_SECTOR_END = 0xf7; KERNEL_START = 0xc050000; KERNEL_MAXSIZE = 0x80000; -KERNEL_U_FLASH_SIZE = 0x200; ASSETS_START = 0xc1f0000; ASSETS_MAXSIZE = 0x10000; ASSETS_SECTOR_START = 0xf8; diff --git a/core/embed/models/T3T1/model_T3T1.h b/core/embed/models/T3T1/model_T3T1.h index 993a2aea28..a66400a685 100644 --- a/core/embed/models/T3T1/model_T3T1.h +++ b/core/embed/models/T3T1/model_T3T1.h @@ -76,7 +76,6 @@ #define FIRMWARE_SECTOR_END 0xF7 #define KERNEL_START 0x0C050000 #define KERNEL_MAXSIZE (512 * 1024) // 512 kB -#define KERNEL_U_FLASH_SIZE 512 #define ASSETS_START 0x0C1F0000 #define ASSETS_MAXSIZE (8 * 8 * 1024) // 64 kB diff --git a/core/embed/trezorhal/stm32u5/linker/u58/kernel.ld b/core/embed/trezorhal/stm32u5/linker/u58/kernel.ld index dc0a92e01a..e2fc8d6d74 100644 --- a/core/embed/trezorhal/stm32u5/linker/u58/kernel.ld +++ b/core/embed/trezorhal/stm32u5/linker/u58/kernel.ld @@ -136,7 +136,7 @@ SECTIONS { .uflash : ALIGN(512) { *(.uflash*); - . = ALIGN(512); + . = ALIGN(COREAPP_ALIGNMENT); } >FLASH AT>FLASH .fb : ALIGN(4) { diff --git a/core/embed/trezorhal/stm32u5/linker/u5a/kernel.ld b/core/embed/trezorhal/stm32u5/linker/u5a/kernel.ld index 212093ef21..453c0706d0 100644 --- a/core/embed/trezorhal/stm32u5/linker/u5a/kernel.ld +++ b/core/embed/trezorhal/stm32u5/linker/u5a/kernel.ld @@ -136,7 +136,7 @@ SECTIONS { .uflash : ALIGN(512) { *(.uflash*); - . = ALIGN(512); + . = ALIGN(COREAPP_ALIGNMENT); } >FLASH AT>FLASH .fb1 : ALIGN(4) { diff --git a/core/embed/trezorhal/stm32u5/mpu.c b/core/embed/trezorhal/stm32u5/mpu.c index 155b54d1cd..ebfe90792e 100644 --- a/core/embed/trezorhal/stm32u5/mpu.c +++ b/core/embed/trezorhal/stm32u5/mpu.c @@ -126,11 +126,12 @@ static void mpu_set_attributes(void) { #define STORAGE_SIZE NORCOW_SECTOR_SIZE* STORAGE_AREAS_COUNT #if defined STM32U5A9xx -#define SRAM_SIZE SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE + SRAM5_SIZE +#define SRAM_SIZE (SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE + SRAM5_SIZE) #elif defined STM32U5G9xx -#define SRAM_SIZE SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE + SRAM5_SIZE + SRAM6_SIZE +#define SRAM_SIZE \ + (SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE + SRAM5_SIZE + SRAM6_SIZE) #elif defined STM32U585xx -#define SRAM_SIZE SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE +#define SRAM_SIZE (SRAM1_SIZE + SRAM2_SIZE + SRAM3_SIZE) #else #error "Unknown MCU" #endif @@ -152,6 +153,7 @@ static void mpu_set_attributes(void) { // clang-format on +#ifdef KERNEL #ifdef STM32U585xx #define KERNEL_RAM_START (SRAM2_BASE - KERNEL_SRAM1_SIZE) #define KERNEL_RAM_SIZE \ @@ -180,9 +182,10 @@ extern uint32_t _codelen; #define KERNEL_SIZE (uint32_t) & _codelen #define KERNEL_FLASH_START KERNEL_START -#define KERNEL_FLASH_SIZE (KERNEL_SIZE - KERNEL_U_FLASH_SIZE) +#define KERNEL_FLASH_SIZE (KERNEL_SIZE - KERNEL_FLASH_U_SIZE) -#define COREAPP_FLASH_START COREAPP_CODE_ALIGN(KERNEL_FLASH_START + KERNEL_SIZE) +#define COREAPP_FLASH_START \ + (COREAPP_CODE_ALIGN(KERNEL_FLASH_START + KERNEL_SIZE) - KERNEL_FLASH_U_SIZE) #define COREAPP_FLASH_SIZE \ (FIRMWARE_MAXSIZE - (COREAPP_FLASH_START - KERNEL_FLASH_START)) @@ -198,6 +201,8 @@ extern uint32_t _codelen; #define COREAPP_RAM1_SIZE SRAM5_SIZE #endif +#endif + typedef struct { // Set if the driver is initialized bool initialized; @@ -376,9 +381,6 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) { case MPU_MODE_ASSETS: SET_REGION( 6, ASSETS_START, ASSETS_MAXSIZE, FLASH_DATA, YES, NO ); break; - case MPU_MODE_SAES: - SET_REGRUN( 6, KERNEL_FLASH_U_START, KERNEL_FLASH_U_SIZE,FLASH_CODE, NO, YES ); // Unprivileged kernel flash - break; case MPU_MODE_APP: SET_REGION( 6, ASSETS_START, ASSETS_MAXSIZE, FLASH_DATA, NO, YES ); break; @@ -393,9 +395,11 @@ mpu_mode_t mpu_reconfig(mpu_mode_t mode) { // clang-format off switch (mode) { // REGION ADDRESS SIZE TYPE WRITE UNPRIV +#ifdef KERNEL case MPU_MODE_SAES: SET_REGION( 7, KERNEL_RAM_U_START, KERNEL_RAM_U_SIZE, SRAM, YES, YES ); // Unprivileged kernel SRAM break; +#endif case MPU_MODE_APP: // DMA2D peripherals (Unprivileged, Read-Write, Non-Executable) SET_REGION( 7, 0x5002B000, SIZE_3K, PERIPHERAL, YES, YES );