2929#include "printf.h"
3030#include "nrf5340.h"
3131#include "spi_flash.h"
32+ #include "hal/armv8m_tz.h"
33+ #include "target.h"
3234
3335/* TODO:
3436 * Key Storage: See 7.1.18.4.2 Key storage:
@@ -133,6 +135,50 @@ static SharedMem_t* shm = (SharedMem_t*)&shm_shadow;
133135 #endif
134136#endif
135137
138+ #ifdef TZEN
139+ static void hal_spu_init (void ) {
140+ uint8_t nsc_size_index ;
141+ uint8_t region ;
142+ uint8_t start_region ;
143+ uint8_t end_region ;
144+
145+ /* Make sure SAU is disabled, configure everything through SPU */
146+ SAU_CTRL = SAU_INIT_CTRL_ALLNS ;
147+
148+ /* Flash: Non-Secure Callable */
149+ nsc_size_index = (WOLFBOOT_NSC_SIZE > 2048 ) ? 8 :
150+ (WOLFBOOT_NSC_SIZE > 1024 ) ? 7 :
151+ (WOLFBOOT_NSC_SIZE > 512 ) ? 6 :
152+ (WOLFBOOT_NSC_SIZE > 256 ) ? 5 :
153+ (WOLFBOOT_NSC_SIZE > 128 ) ? 4 :
154+ (WOLFBOOT_NSC_SIZE > 64 ) ? 3 :
155+ (WOLFBOOT_NSC_SIZE > 32 ) ? 2 :
156+ (WOLFBOOT_NSC_SIZE > 0 ) ? 1 : 0 ;
157+
158+ SPU_FLASHNSC_REGION (0 ) = ((WOLFBOOT_NSC_ADDRESS / SPU_FLASH_BLOCK_SIZE ) &
159+ SPU_FLASHNSC_REGION_MASK ) | SPU_FLASHNSC_REGION_LOCK ;
160+ SPU_FLASHNSC_SIZE (0 ) = nsc_size_index | SPU_FLASHNSC_SIZE_LOCK ;
161+
162+ /* Flash: non-secure application area */
163+ start_region = WOLFBOOT_PARTITION_BOOT_ADDRESS / SPU_FLASH_BLOCK_SIZE ;
164+ end_region = (WOLFBOOT_PARTITION_BOOT_ADDRESS + WOLFBOOT_PARTITION_SIZE ) / SPU_FLASH_BLOCK_SIZE ;
165+
166+ for (region = start_region ; region < end_region ; region ++ ) {
167+ SPU_FLASHREGION_PERM (region ) = (SPU_FLASHREGION_PERM (region ) &
168+ ~SPU_FLASHREGION_PERM_SECATTR ) | SPU_FLASHREGION_PERM_LOCK ;
169+ }
170+
171+ /* RAM: non-secure application area */
172+ start_region = 0x20000 / SPU_RAM_BLOCK_SIZE ;
173+ end_region = 0x24000 / SPU_RAM_BLOCK_SIZE ;
174+
175+ for (region = start_region ; region < end_region ; region ++ ) {
176+ SPU_RAMREGION_PERM (region ) = (SPU_RAMREGION_PERM (region ) &
177+ ~SPU_RAMREGION_PERM_SECATTR ) | SPU_RAMREGION_PERM_LOCK ;
178+ }
179+ }
180+ #endif
181+
136182void uart_init (void )
137183{
138184 /* nRF5340-DK: (P0.20 or P1.01)
@@ -220,8 +266,9 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
220266 ((((uint32_t )data ) + i ) & 0x03 ) == 0 )) {
221267 src = (uint32_t * )data ;
222268 dst = (uint32_t * )address ;
223- /* set both secure and non-secure registers */
269+ #if TZ_SECURE () || defined( TARGET_nrf5340_net )
224270 NVMC_CONFIG = NVMC_CONFIG_WEN ;
271+ #endif
225272 NVMC_CONFIGNS = NVMC_CONFIG_WEN ;
226273 while (NVMC_READY == 0 );
227274 dst [i >> 2 ] = src [i >> 2 ];
@@ -234,8 +281,9 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
234281 dst = (uint32_t * )(address - off );
235282 val = dst [i >> 2 ];
236283 vbytes [off ] = data [i ];
237- /* set both secure and non-secure registers */
284+ #if TZ_SECURE () || defined( TARGET_nrf5340_net )
238285 NVMC_CONFIG = NVMC_CONFIG_WEN ;
286+ #endif
239287 NVMC_CONFIGNS = NVMC_CONFIG_WEN ;
240288 while (NVMC_READY == 0 );
241289 dst [i >> 2 ] = val ;
@@ -257,7 +305,9 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
257305 address &= ~(FLASH_PAGE_SIZE - 1 );
258306 for (p = address ; p <= end ; p += FLASH_PAGE_SIZE ) {
259307 /* set both secure and non-secure registers */
308+ #if TZ_SECURE () || defined(TARGET_nrf5340_net )
260309 NVMC_CONFIG = NVMC_CONFIG_EEN ;
310+ #endif
261311 NVMC_CONFIGNS = NVMC_CONFIG_EEN ;
262312 while (NVMC_READY == 0 );
263313 * (volatile uint32_t * )p = 0xFFFFFFFF ;
@@ -716,6 +766,10 @@ void hal_init(void)
716766
717767 hal_net_check_version ();
718768#endif
769+
770+ #ifdef TZEN
771+ hal_spu_init ();
772+ #endif
719773}
720774
721775#ifdef __WOLFBOOT
@@ -733,8 +787,8 @@ int hal_flash_protect(uint32_t start, uint32_t len)
733787 if (start + len > FLASH_SIZE )
734788 len = FLASH_SIZE - start ;
735789
736- region = (start / SPU_BLOCK_SIZE );
737- n = (len / SPU_BLOCK_SIZE );
790+ region = (start / SPU_FLASH_BLOCK_SIZE );
791+ n = (len / SPU_FLASH_BLOCK_SIZE );
738792
739793 for (i = 0 ; i < n ; i ++ ) {
740794 /* do not allow write to this region and lock till next reset */
@@ -749,6 +803,25 @@ int hal_flash_protect(uint32_t start, uint32_t len)
749803 return 0 ;
750804}
751805
806+ #ifdef TZEN
807+ static void periph_unsecure () {
808+ /* Unsecure both GPIO ports */
809+ SPU_PERIPHID_PERM (GPIO_PERIPHID ) &= ~SPU_PERIPHID_PERM_SECATTR ;
810+ //SPU_GPIOPORT_PERM(0) = (1 << 29);
811+ SPU_GPIOPORT_PERM (0 ) = 0 ;
812+ SPU_GPIOPORT_PERM (1 ) = 0 ;
813+
814+ /* Unsecure UARTE0 */
815+ SPU_PERIPHID_PERM (SERIAL0_PERIPHID ) &= ~SPU_PERIPHID_PERM_SECATTR ;
816+
817+ /* Unsecure NVMC */
818+ SPU_PERIPHID_PERM (KMU_NVMC_PERIPHID ) &= ~SPU_PERIPHID_PERM_SECATTR ;
819+
820+ /* Unsecure RTC0 */
821+ SPU_PERIPHID_PERM (RTC0_PERIPHID ) &= ~SPU_PERIPHID_PERM_SECATTR ;
822+ }
823+ #endif
824+
752825void hal_prepare_boot (void )
753826{
754827 /* Write protect bootloader region of flash */
@@ -771,16 +844,28 @@ void hal_prepare_boot(void)
771844 #endif
772845
773846 #if defined(TARGET_nrf5340_app ) && defined(NRF_SYNC_CORES )
774- /* if core synchronization enabled,
775- * then wait for update_done or do_boot (5 seconds, 30 for update) */
847+ /* If core synchronization enabled,
848+ * then wait for update_done or do_boot (5 seconds, 30 for update).
849+ * Longer wait in DEBUG mode because the net core boots much
850+ * slower. */
776851 wolfBoot_printf ("Waiting for network core...\n" );
852+ #ifndef DEBUG
777853 (void )hal_shm_status_wait (& shm -> core .net ,
778854 (SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT ),
779855 doUpdateNet ? 30 * 1000 : 5 * 1000 );
856+ #else
857+ (void )hal_shm_status_wait (& shm -> core .net ,
858+ (SHARED_STATUS_UPDATE_DONE | SHARED_STATUS_DO_BOOT ),
859+ doUpdateNet ? 45 * 1000 : 20 * 1000 );
860+ #endif
780861 #endif
781862 }
782863
783864 hal_shm_cleanup ();
865+
866+ #ifdef TZEN
867+ periph_unsecure ();
868+ #endif
784869}
785870#endif /* __WOLFBOOT */
786871
0 commit comments