@@ -192,6 +192,7 @@ void RAMFUNCTION hal_flash_opt_lock(void)
192192
193193int RAMFUNCTION hal_flash_erase (uint32_t address , int len )
194194{
195+ uint32_t start_address ;
195196 uint32_t end_address ;
196197 uint32_t p ;
197198
@@ -202,8 +203,17 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
202203 if (address < 0x08000000 )
203204 return -1 ;
204205
205- end_address = address + len - 1 ;
206- for (p = address ; p < end_address ; p += FLASH_PAGE_SIZE ) {
206+ #if TZ_SECURE ()
207+ start_address = address | FLASH_SECURE_MMAP_BASE ;
208+ if (is_flash_nonsecure (address )) {
209+ hal_tz_claim_nonsecure_area (address , len );
210+ }
211+ #else
212+ start_address = address ;
213+ #endif
214+
215+ end_address = start_address + len - 1 ;
216+ for (p = start_address ; p < end_address ; p += FLASH_PAGE_SIZE ) {
207217 uint32_t reg ;
208218 uint32_t base ;
209219 uint32_t bnksel = 0 ;
@@ -214,12 +224,6 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
214224 base = FLASH_BANK2_BASE ;
215225 bnksel = 1 ;
216226 }
217- #if TZ_SECURE ()
218- /* When in secure mode, skip erasing non-secure pages: will be erased upon claim */
219- if (is_flash_nonsecure (address )) {
220- return 0 ;
221- }
222- #endif
223227 /* Check for swapped banks to invert bnksel */
224228 if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK ) >> 31 )
225229 bnksel = !bnksel ;
@@ -231,6 +235,12 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
231235 }
232236 /* If the erase operation is completed, disable the associated bits */
233237 FLASH_CR &= ~FLASH_CR_SER ;
238+
239+ #if TZ_SECURE ()
240+ if (is_flash_nonsecure (address )) {
241+ hal_tz_release_nonsecure_area ();
242+ }
243+ #endif
234244 return 0 ;
235245}
236246
0 commit comments