Skip to content

Commit 5ac5191

Browse files
committed
Uniform STM32L5 flash write hal
- update partition is now secure, only accessible via NSC - removed legacy security settings in hal_flash_write/erase when running in secure mode - Added explicit IMAGE_HEADER_SIZE to default config
1 parent d4e66e5 commit 5ac5191

3 files changed

Lines changed: 28 additions & 37 deletions

File tree

config/examples/stm32l5-wolfcrypt-tz.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ FLAGS_HOME=0
3131
DISABLE_BACKUP=0
3232
WOLFCRYPT_TZ=1
3333
WOLFCRYPT_TZ_PKCS11=1
34+
35+
# Use a larger image header size to enforce alignment requirements for the interrupt vector table
36+
IMAGE_HEADER_SIZE?=1024

hal/stm32_tz.c

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ static int is_range_nonsecure(uint32_t address, int len)
9595
void hal_tz_claim_nonsecure_area(uint32_t address, int len)
9696
{
9797
int page_n, reg_idx;
98-
uint32_t reg;
9998
uint32_t end = address + len;
100-
uint32_t start_address = address;
10199
uint32_t start_page_n;
102100
uint32_t bank = 0;
103101
int pos;
@@ -124,39 +122,14 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
124122
hal_flash_wait_complete(bank);
125123
hal_flash_clear_errors(bank);
126124
if (bank == 0)
127-
FLASH_SECBB1[reg_idx] |= ( 1 << pos);
125+
FLASH_SECBB1[reg_idx] |= (1u << pos);
128126
else
129-
FLASH_SECBB2[reg_idx] |= ( 1 << pos);
127+
FLASH_SECBB2[reg_idx] |= (1u << pos);
130128
ISB();
131129
hal_flash_wait_complete(bank);
132130
address += FLASH_PAGE_SIZE;
133131
page_n++;
134132
}
135-
address = start_address;
136-
page_n = start_page_n;
137-
while (address < end) {
138-
/* Erase claimed non-secure page, in secure mode */
139-
#ifndef TARGET_stm32h5
140-
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER | FLASH_CR_BKER | FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_MER2));
141-
FLASH_CR = reg | ((page_n << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER);
142-
#else
143-
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | FLASH_CR_BER | FLASH_CR_PG | FLASH_CR_MER | FLASH_CR_BKSEL));
144-
FLASH_CR = reg | ((page_n << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | (bank << 31));
145-
#endif
146-
147-
DMB();
148-
ISB();
149-
FLASH_CR |= FLASH_CR_STRT;
150-
ISB();
151-
hal_flash_wait_complete(bank);
152-
address += FLASH_PAGE_SIZE;
153-
page_n++;
154-
}
155-
#ifndef TARGET_stm32h5
156-
FLASH_CR &= ~FLASH_CR_PER ;
157-
#else
158-
FLASH_CR &= ~FLASH_CR_SER ;
159-
#endif
160133
}
161134
#else
162135
#define claim_nonsecure_area(...) do{}while(0)
@@ -317,8 +290,8 @@ void hal_tz_sau_init(void)
317290
/* Secure RAM regions in SRAM1/SRAM2 */
318291
sau_init_region(3, 0x30000000, 0x3004FFFF, 1);
319292

320-
/* Non-secure RAM region in SRAM3 */
321-
sau_init_region(4, 0x20050000, 0x2008FFFF, 0);
293+
/* Non-secure RAM region in SRAM1/SRAM2 (STM32L5x2: 0x2000_0000..0x2003_FFFF) */
294+
sau_init_region(4, 0x20000000, 0x2003FFFF, 0);
322295

323296
/* Non-secure: internal peripherals */
324297
sau_init_region(5, 0x40000000, 0x4FFFFFFF, 0);
@@ -378,6 +351,12 @@ void hal_tz_sau_init(void)
378351
#define TRNG_CR_CONFIG1_SHIFT (20)
379352
#define TRNG_CR_CONDRST (1 << 30)
380353

354+
__attribute__((noinline)) static void RAMFUNCTION trng_trace_step(uint32_t step)
355+
{
356+
(void)step;
357+
__asm volatile("" : : "r"(step) : "memory");
358+
}
359+
381360

382361
static void hsi48_on(void)
383362
{
@@ -396,8 +375,11 @@ static void hsi48_on(void)
396375
void hal_trng_init(void)
397376
{
398377
uint32_t reg_val;
378+
trng_trace_step(1);
399379
hsi48_on();
380+
trng_trace_step(2);
400381
RCC_AHB2_CLOCK_ER |= TRNG_AHB2_CLOCK_ER;
382+
trng_trace_step(3);
401383

402384
reg_val = TRNG_CR;
403385
reg_val &= ~(0x1F << TRNG_CR_CONFIG1_SHIFT);
@@ -411,11 +393,15 @@ void hal_trng_init(void)
411393
reg_val |= 0x06 << TRNG_CR_CLKDIV_SHIFT;
412394
#endif
413395
TRNG_CR = TRNG_CR_CONDRST | reg_val;
396+
trng_trace_step(4);
414397
while ((TRNG_CR & TRNG_CR_CONDRST) == 0)
415398
;
399+
trng_trace_step(5);
416400
TRNG_CR = reg_val | TRNG_CR_RNGEN;
401+
trng_trace_step(6);
417402
while ((TRNG_SR & TRNG_SR_DRDY) == 0)
418403
;
404+
trng_trace_step(7);
419405
}
420406

421407
/* Never used (RNG keeps running when in secure-mode) */

hal/stm32l5.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "hal.h"
2727
#include "hal/stm32l5.h"
28+
#include "printf.h"
2829

2930

3031
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
@@ -146,12 +147,18 @@ void RAMFUNCTION hal_flash_opt_lock(void)
146147
FLASH_CR |= FLASH_CR_OPTLOCK;
147148
}
148149

149-
150150
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
151151
{
152152
uint32_t end_address;
153153
uint32_t p;
154154

155+
if (address < WOLFBOOT_PARTITION_BOOT_ADDRESS) {
156+
wolfBoot_printf("hal_flash_erase: addr=0x%08x len=%d (below boot)\n",
157+
address, len);
158+
} else {
159+
wolfBoot_printf("hal_flash_erase: addr=0x%08x len=%d\n", address, len);
160+
}
161+
155162
hal_flash_clear_errors(0);
156163
if (len == 0)
157164
return -1;
@@ -170,10 +177,6 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
170177
}
171178
else if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
172179
{
173-
#if TZ_SECURE()
174-
/* When in secure mode, skip erasing non-secure pages: will be erased upon claim */
175-
return 0;
176-
#endif
177180
bker = FLASH_CR_BKER;
178181
base = FLASH_BANK2_BASE;
179182
} else {
@@ -412,4 +415,3 @@ void hal_prepare_boot(void)
412415
periph_unsecure();
413416
#endif
414417
}
415-

0 commit comments

Comments
 (0)