Skip to content

Commit fde422d

Browse files
committed
stm32h5: cede SRAM2 + clear GPIO SECCFGR for NS apps (TZEN=1)
1 parent 1ce8234 commit fde422d

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

hal/stm32_tz.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,20 @@ void hal_gtzc_init(void)
209209
* 0: Non-secure access only to block
210210
*/
211211

212-
/* Configure SRAM1 as secure (Low 256 KB) */
212+
/* Configure SRAM1 as secure (Low 256 KB).
213+
* wolfBoot links its own RAM/RAM_HEAP into the SRAM1 secure alias
214+
* (0x30000000-0x3003FFFF, see hal/stm32h5.ld), so SRAM1 must stay
215+
* secure for wolfBoot's .bss/stack/heap to remain accessible. */
213216
for (i = 0; i < 16; i++) {
214217
SET_GTZC1_MPCBBx_SECCFGR_VCTR(1, i, 0xFFFFFFFF);
215218
}
216219

217-
/* Configure SRAM2 as secure (64 KB) */
220+
/* Configure SRAM2 as non-secure (64 KB).
221+
* wolfBoot does not use SRAM2; ceding it to the NS application
222+
* widens the NS RAM window from 320 KB (SRAM3 only) to 384 KB
223+
* (SRAM2 + SRAM3). */
218224
for (i = 0; i < 4; i++) {
219-
SET_GTZC1_MPCBBx_SECCFGR_VCTR(2, i, 0xFFFFFFFF);
225+
SET_GTZC1_MPCBBx_SECCFGR_VCTR(2, i, 0x0);
220226
}
221227

222228
/* Configure SRAM3 as non-secure (320 KB) */
@@ -310,8 +316,11 @@ void hal_tz_sau_init(void)
310316
sau_init_region(1, WOLFBOOT_PARTITION_BOOT_ADDRESS,
311317
WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE - 1, 0);
312318

313-
/* Non-secure RAM region */
314-
sau_init_region(2, 0x20050000, 0x2009FFFF, 0);
319+
/* Non-secure RAM region: SRAM2 (64 KB) + SRAM3 (320 KB).
320+
* Lower bound widened from 0x20050000 to 0x20040000 to cover SRAM2,
321+
* which hal_gtzc_init also leaves non-secure. SRAM1 (0x20000000-
322+
* 0x2003FFFF) stays secure for wolfBoot's own RAM/heap. */
323+
sau_init_region(2, 0x20040000, 0x2009FFFF, 0);
315324

316325
/* Non-secure: internal peripherals */
317326
sau_init_region(3, 0x40000000, 0x4FFFFFFF, 0);

hal/stm32h5.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,23 @@ static void periph_unsecure(void)
642642
nvic_reg_off = NVIC_USART3_IRQ % 32;
643643
nvic_itns = ((volatile uint32_t *)(NVIC_ITNS_BASE + 4 * nvic_reg_pos));
644644
*nvic_itns |= (1 << nvic_reg_off);
645+
646+
/* H5 product state with TZEN=1 defaults every GPIO pin to secure
647+
* via GPIOx_SECCFGR (offset 0x30 in each GPIO block, all 16 bits
648+
* = 0xFFFF at reset). Until those bits are cleared, NS code can't
649+
* read or write the pin's MODER/AFR/ODR, and the corresponding
650+
* clock-enable bit in RCC_AHB2ENR is masked from the NS side.
651+
*
652+
* Clear SECCFGR for every pin on the ports the wolfIP NS app uses
653+
* (RMII + USART3 + LED), then enable GPIOG's clock (the existing
654+
* code only covered A/B/C/D). PD8 (USART3 TX) is already cleared
655+
* above, but covering all of GPIOD is harmless. */
656+
GPIO_SECCFGR(GPIOA_BASE) = 0u;
657+
GPIO_SECCFGR(GPIOB_BASE) = 0u;
658+
GPIO_SECCFGR(GPIOC_BASE) = 0u;
659+
GPIO_SECCFGR(GPIOD_BASE) = 0u;
660+
GPIO_SECCFGR(GPIOG_BASE) = 0u;
661+
RCC_AHB2_CLOCK_ER |= GPIOG_AHB2_CLOCK_ER;
645662
}
646663
#endif /* TZ_SECURE() */
647664

0 commit comments

Comments
 (0)