Skip to content

Commit 3c6d021

Browse files
Add TrustZone support for MCXA and MCXW targets
Co-Authored-By: [email protected] <[email protected]>
1 parent f9b92cb commit 3c6d021

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

hal/mcxa.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
/* Flash driver */
3333
#include "fsl_romapi.h"
3434

35+
#include "hal/armv8m_tz.h"
36+
37+
#if (defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && !defined(NONSECURE_APP))
38+
# define TZ_SECURE() (1)
39+
#else
40+
# define TZ_SECURE() (0)
41+
#endif
42+
3543
/*!< Core clock frequency: 96000000Hz */
3644
#define BOARD_BOOTCLOCKFRO96M_CORE_CLOCK 96000000UL
3745

@@ -61,8 +69,32 @@ void hal_init(void)
6169
FLASH_Init(&pflash);
6270
}
6371

72+
#ifdef TZEN
73+
static void mcxa_configure_sau(void)
74+
{
75+
/* Disable SAU */
76+
SAU_CTRL = 0;
77+
78+
/* Configure SAU regions - adjust addresses based on MCXA memory map */
79+
sau_init_region(0, 0x00000000, 0x0003FFFF, 1); /* Secure flash */
80+
sau_init_region(1, 0x00040000, 0x0007FFFF, 0); /* Non-secure flash */
81+
sau_init_region(2, 0x20000000, 0x2001FFFF, 1); /* Secure RAM */
82+
sau_init_region(3, 0x20020000, 0x2003FFFF, 0); /* Non-secure RAM */
83+
sau_init_region(4, 0x40000000, 0x5FFFFFFF, 0); /* Non-secure peripherals */
84+
85+
/* Enable SAU */
86+
SAU_CTRL = SAU_INIT_CTRL_ENABLE;
87+
88+
/* Enable securefault handler */
89+
SCB_SHCSR |= SCB_SHCSR_SECUREFAULT_EN;
90+
}
91+
#endif
92+
6493
void hal_prepare_boot(void)
6594
{
95+
#ifdef TZEN
96+
mcxa_configure_sau();
97+
#endif
6698
}
6799

68100
#endif /* __WOLFBOOT */
@@ -76,6 +108,10 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
76108
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
77109
};
78110

111+
#if TZ_SECURE()
112+
/* Add TrustZone-specific handling if needed */
113+
#endif
114+
79115
while (len > 0) {
80116
if ((len < 16) || address & 0x0F) {
81117
uint8_t aligned_qword[16];
@@ -118,6 +154,10 @@ void RAMFUNCTION hal_flash_lock(void)
118154

119155
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
120156
{
157+
#if TZ_SECURE()
158+
/* Add TrustZone-specific handling if needed */
159+
#endif
160+
121161
while ((address % 4) != 0)
122162
address --;
123163
if (FLASH_EraseSector(&pflash, address, len, kFLASH_ApiEraseKey) != kStatus_Success)

hal/mcxw.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
#include "fsl_flash_api.h"
3838
#include "fsl_ccm32k.h"
3939

40+
#include "hal/armv8m_tz.h"
41+
42+
#if (defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && !defined(NONSECURE_APP))
43+
# define TZ_SECURE() (1)
44+
#else
45+
# define TZ_SECURE() (0)
46+
#endif
47+
4048
#define FLASH FMU0
4149

4250
/*!< Core clock frequency: 48000000Hz */
@@ -61,9 +69,32 @@ void __assert_func(const char *a, int b, const char *c, const char *d)
6169
}
6270

6371

64-
void hal_prepare_boot(void)
72+
#ifdef TZEN
73+
static void mcxw_configure_sau(void)
6574
{
75+
/* Disable SAU */
76+
SAU_CTRL = 0;
77+
78+
/* Configure SAU regions - adjust addresses based on MCXW memory map */
79+
sau_init_region(0, 0x00000000, 0x0003FFFF, 1); /* Secure flash */
80+
sau_init_region(1, 0x00040000, 0x0007FFFF, 0); /* Non-secure flash */
81+
sau_init_region(2, 0x20000000, 0x2001FFFF, 1); /* Secure RAM */
82+
sau_init_region(3, 0x20020000, 0x2003FFFF, 0); /* Non-secure RAM */
83+
sau_init_region(4, 0x40000000, 0x5FFFFFFF, 0); /* Non-secure peripherals */
84+
85+
/* Enable SAU */
86+
SAU_CTRL = SAU_INIT_CTRL_ENABLE;
87+
88+
/* Enable securefault handler */
89+
SCB_SHCSR |= SCB_SHCSR_SECUREFAULT_EN;
90+
}
91+
#endif
6692

93+
void hal_prepare_boot(void)
94+
{
95+
#ifdef TZEN
96+
mcxw_configure_sau();
97+
#endif
6798
}
6899

69100
#endif
@@ -91,6 +122,10 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
91122
0xFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
92123
};
93124

125+
#if TZ_SECURE()
126+
/* Add TrustZone-specific handling if needed */
127+
#endif
128+
94129
while (len > 0) {
95130
if ((len < (int)flash_word_size) || (address & (flash_word_size - 1))) {
96131
uint32_t aligned_qword[4];
@@ -144,6 +179,11 @@ void RAMFUNCTION hal_flash_lock(void)
144179
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
145180
{
146181
status_t result;
182+
183+
#if TZ_SECURE()
184+
/* Add TrustZone-specific handling if needed */
185+
#endif
186+
147187
if (address % pflash_sector_size)
148188
address -= address % pflash_sector_size;
149189
while (len > 0) {

test-app/app_mcxw.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,22 @@ void main(void)
7676
gpio_portA_init(20);
7777

7878
hal_init();
79-
if (bootVer == 1) {
79+
80+
/* Check if we're in testing mode after an update */
81+
if (wolfBoot_get_partition_state(PART_BOOT, NULL) == IMG_STATE_TESTING) {
82+
/* Mark update successful */
83+
wolfBoot_success();
84+
/* Green LED ON, GPIOA port A pin 19 */
85+
GPIO_PinWrite(GPIOA, 19, 0);
86+
}
87+
else if (bootVer == 1) {
8088
/* Blue LED ON, GPIOA port A pin 20 */
8189
GPIO_PinWrite(GPIOA, 20, 0);
8290
wolfBoot_update_trigger();
8391
}
8492
else {
8593
/* Green LED ON, GPIOA port A pin 19 */
8694
GPIO_PinWrite(GPIOA, 19, 0);
87-
/* mark boot successful */
88-
wolfBoot_success();
8995
}
9096

9197
/* busy wait */

0 commit comments

Comments
 (0)