diff --git a/hal/mcxa.c b/hal/mcxa.c index 1fdb22001d..b567089492 100644 --- a/hal/mcxa.c +++ b/hal/mcxa.c @@ -32,6 +32,14 @@ /* Flash driver */ #include "fsl_romapi.h" +#include "hal/armv8m_tz.h" + +#if (defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && !defined(NONSECURE_APP)) +# define TZ_SECURE() (1) +#else +# define TZ_SECURE() (0) +#endif + /*!< Core clock frequency: 96000000Hz */ #define BOARD_BOOTCLOCKFRO96M_CORE_CLOCK 96000000UL @@ -61,8 +69,32 @@ void hal_init(void) FLASH_Init(&pflash); } +#ifdef TZEN +static void mcxa_configure_sau(void) +{ + /* Disable SAU */ + SAU_CTRL = 0; + + /* Configure SAU regions - adjust addresses based on MCXA memory map */ + sau_init_region(0, 0x00000000, 0x0003FFFF, 1); /* Secure flash */ + sau_init_region(1, 0x00040000, 0x0007FFFF, 0); /* Non-secure flash */ + sau_init_region(2, 0x20000000, 0x2001FFFF, 1); /* Secure RAM */ + sau_init_region(3, 0x20020000, 0x2003FFFF, 0); /* Non-secure RAM */ + sau_init_region(4, 0x40000000, 0x5FFFFFFF, 0); /* Non-secure peripherals */ + + /* Enable SAU */ + SAU_CTRL = SAU_INIT_CTRL_ENABLE; + + /* Enable securefault handler */ + SCB_SHCSR |= SCB_SHCSR_SECUREFAULT_EN; +} +#endif + void hal_prepare_boot(void) { +#ifdef TZEN + mcxa_configure_sau(); +#endif } #endif /* __WOLFBOOT */ @@ -76,6 +108,10 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +#if TZ_SECURE() + /* Add TrustZone-specific handling if needed */ +#endif + while (len > 0) { if ((len < 16) || address & 0x0F) { uint8_t aligned_qword[16]; @@ -118,6 +154,10 @@ void RAMFUNCTION hal_flash_lock(void) int RAMFUNCTION hal_flash_erase(uint32_t address, int len) { +#if TZ_SECURE() + /* Add TrustZone-specific handling if needed */ +#endif + while ((address % 4) != 0) address --; if (FLASH_EraseSector(&pflash, address, len, kFLASH_ApiEraseKey) != kStatus_Success) diff --git a/hal/mcxw.c b/hal/mcxw.c index 1627ac37c6..1568ed5709 100644 --- a/hal/mcxw.c +++ b/hal/mcxw.c @@ -37,6 +37,14 @@ #include "fsl_flash_api.h" #include "fsl_ccm32k.h" +#include "hal/armv8m_tz.h" + +#if (defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U) && !defined(NONSECURE_APP)) +# define TZ_SECURE() (1) +#else +# define TZ_SECURE() (0) +#endif + #define FLASH FMU0 /*!< Core clock frequency: 48000000Hz */ @@ -61,9 +69,32 @@ void __assert_func(const char *a, int b, const char *c, const char *d) } -void hal_prepare_boot(void) +#ifdef TZEN +static void mcxw_configure_sau(void) { + /* Disable SAU */ + SAU_CTRL = 0; + + /* Configure SAU regions - adjust addresses based on MCXW memory map */ + sau_init_region(0, 0x00000000, 0x0003FFFF, 1); /* Secure flash */ + sau_init_region(1, 0x00040000, 0x0007FFFF, 0); /* Non-secure flash */ + sau_init_region(2, 0x20000000, 0x2001FFFF, 1); /* Secure RAM */ + sau_init_region(3, 0x20020000, 0x2003FFFF, 0); /* Non-secure RAM */ + sau_init_region(4, 0x40000000, 0x5FFFFFFF, 0); /* Non-secure peripherals */ + + /* Enable SAU */ + SAU_CTRL = SAU_INIT_CTRL_ENABLE; + + /* Enable securefault handler */ + SCB_SHCSR |= SCB_SHCSR_SECUREFAULT_EN; +} +#endif +void hal_prepare_boot(void) +{ +#ifdef TZEN + mcxw_configure_sau(); +#endif } #endif @@ -91,6 +122,10 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len) 0xFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF }; +#if TZ_SECURE() + /* Add TrustZone-specific handling if needed */ +#endif + while (len > 0) { if ((len < (int)flash_word_size) || (address & (flash_word_size - 1))) { uint32_t aligned_qword[4]; @@ -144,6 +179,11 @@ void RAMFUNCTION hal_flash_lock(void) int RAMFUNCTION hal_flash_erase(uint32_t address, int len) { status_t result; + +#if TZ_SECURE() + /* Add TrustZone-specific handling if needed */ +#endif + if (address % pflash_sector_size) address -= address % pflash_sector_size; while (len > 0) { diff --git a/test-app/app_mcxw.c b/test-app/app_mcxw.c index 903d91cb25..0a2769d963 100644 --- a/test-app/app_mcxw.c +++ b/test-app/app_mcxw.c @@ -76,7 +76,15 @@ void main(void) gpio_portA_init(20); hal_init(); - if (bootVer == 1) { + + /* Check if we're in testing mode after an update */ + if (wolfBoot_get_partition_state(PART_BOOT, NULL) == IMG_STATE_TESTING) { + /* Mark update successful */ + wolfBoot_success(); + /* Green LED ON, GPIOA port A pin 19 */ + GPIO_PinWrite(GPIOA, 19, 0); + } + else if (bootVer == 1) { /* Blue LED ON, GPIOA port A pin 20 */ GPIO_PinWrite(GPIOA, 20, 0); wolfBoot_update_trigger(); @@ -84,8 +92,6 @@ void main(void) else { /* Green LED ON, GPIOA port A pin 19 */ GPIO_PinWrite(GPIOA, 19, 0); - /* mark boot successful */ - wolfBoot_success(); } /* busy wait */