@@ -48,8 +48,6 @@ static void RAMFUNCTION hal_flash_nonsecure_unlock(void)
4848 DMB ();
4949 FLASH_NS_KEYR = FLASH_KEY2 ;
5050 DMB ();
51- while ((FLASH_NS_CR & FLASH_CR_LOCK ) != 0 )
52- ;
5351 }
5452}
5553
@@ -97,37 +95,42 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
9795 int page_n , reg_idx ;
9896 uint32_t reg ;
9997 uint32_t end = address + len ;
98+ uint32_t start_address = address ;
10099 uint32_t bank = 0 ;
101100 int pos ;
102101
103102 if (!is_range_nonsecure (address , len ))
104103 return ;
105- while (address < end ) {
106- if (address < FLASH_BANK2_BASE ) {
107- page_n = (address - ARCH_FLASH_OFFSET ) / FLASH_PAGE_SIZE ;
108- bank = 0 ;
109- } else {
110- page_n = (address - FLASH_BANK2_BASE ) / FLASH_PAGE_SIZE ;
111- bank = 1 ;
112- }
113104
105+ if (address < FLASH_BANK2_BASE ) {
106+ page_n = (address - ARCH_FLASH_OFFSET ) / FLASH_PAGE_SIZE ;
107+ bank = 0 ;
108+ } else {
109+ page_n = (address - FLASH_BANK2_BASE ) / FLASH_PAGE_SIZE ;
110+ bank = 1 ;
111+ }
114112#ifdef PLATFORM_stm32h5
115- /* Take into account current swap configuration */
116- if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK ) >> 31 )
117- bank = !bank ;
113+ /* Take into account current swap configuration */
114+ if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK ) >> 31 )
115+ bank = !bank ;
118116#endif
117+ while (address < end ) {
119118 reg_idx = page_n / 32 ;
120119 pos = page_n % 32 ;
121120 hal_flash_wait_complete (bank );
122121 hal_flash_clear_errors (bank );
123- hal_flash_nonsecure_unlock ();
124122 if (bank == 0 )
125123 FLASH_SECBB1 [reg_idx ] |= ( 1 << pos );
126124 else
127125 FLASH_SECBB2 [reg_idx ] |= ( 1 << pos );
128126 ISB ();
129127 hal_flash_wait_complete (bank );
130- hal_flash_nonsecure_lock ();
128+ address += FLASH_PAGE_SIZE ;
129+ page_n ++ ;
130+ }
131+
132+ address = start_address ;
133+ while (address < end ) {
131134 /* Erase claimed non-secure page, in secure mode */
132135#ifndef PLATFORM_stm32h5
133136 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 ));
@@ -156,9 +159,48 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
156159#if defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3U )
157160void hal_tz_release_nonsecure_area (void )
158161{
162+ #ifndef DUALBANK_SWAP
159163 int i ;
160164 for (i = 0 ; i < FLASH_SECBB_NREGS ; i ++ )
161165 FLASH_SECBB2 [i ] = 0 ;
166+ #else
167+ uint32_t addr ;
168+ int bank_swp = 0 ;
169+ /* Take into account current swap configuration */
170+ if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK ) >> 31 )
171+ bank_swp = 1 ;
172+
173+ /* Bank 1 */
174+ for (addr = WOLFBOOT_PARTITION_BOOT_ADDRESS ;
175+ addr < FLASH_BANK2_BASE ; addr += FLASH_PAGE_SIZE ) {
176+ uint32_t page_n = (addr - FLASHMEM_ADDRESS_SPACE ) / FLASH_PAGE_SIZE ;
177+ uint32_t reg_idx = page_n / 32 ;
178+ uint32_t pos = page_n % 32 ;
179+ hal_flash_wait_complete (0 );
180+ hal_flash_clear_errors (0 );
181+ if (!bank_swp )
182+ FLASH_SECBB1 [reg_idx ] &= ~( 1 << pos );
183+ else
184+ FLASH_SECBB2 [reg_idx ] &= ~( 1 << pos );
185+ ISB ();
186+ hal_flash_wait_complete (0 );
187+ }
188+ /* Bank 2 */
189+ for (addr = WOLFBOOT_PARTITION_UPDATE_ADDRESS ;
190+ addr < FLASH_TOP ; addr += FLASH_PAGE_SIZE ) {
191+ uint32_t page_n = (addr - FLASH_BANK2_BASE ) / FLASH_PAGE_SIZE ;
192+ uint32_t reg_idx = page_n / 32 ;
193+ uint32_t pos = page_n % 32 ;
194+ hal_flash_wait_complete (1 );
195+ hal_flash_clear_errors (1 );
196+ if (!bank_swp )
197+ FLASH_SECBB2 [reg_idx ] &= ~( 1 << pos );
198+ else
199+ FLASH_SECBB1 [reg_idx ] &= ~( 1 << pos );
200+ ISB ();
201+ hal_flash_wait_complete (1 );
202+ }
203+ #endif
162204}
163205#else
164206#define release_nonsecure_area (...) do{}while(0)
@@ -243,13 +285,14 @@ void hal_tz_sau_init(void)
243285{
244286 uint32_t page_n = 0 ;
245287 /* SAU is set up before staging. Set up all areas as secure. */
288+
246289 /* Non-secure callable: NSC functions area */
247290 sau_init_region (0 , 0x0C038000 , 0x0C040000 , 1 );
248291
249- /* Non- Secure: application flash area (first bank) */
292+ /* Secure: application flash area (first bank) */
250293 sau_init_region (1 , WOLFBOOT_PARTITION_BOOT_ADDRESS , FLASH_BANK2_BASE - 1 , 0 );
251294
252- /* Non- Secure: application flash area (second bank) */
295+ /* Secure: application flash area (second bank) */
253296 sau_init_region (2 , WOLFBOOT_PARTITION_UPDATE_ADDRESS , FLASH_TOP - 1 , 0 );
254297
255298 /* Secure RAM regions in SRAM1/SRAM2 */
0 commit comments