@@ -245,6 +245,11 @@ static int wolfBoot_swap_and_final_erase(int resume)
245245 if ((resume == 1 ) && (swapDone == 0 ) && (st != IMG_STATE_FINAL_FLAGS )) {
246246 return -1 ;
247247 }
248+ hal_flash_unlock ();
249+ #ifdef EXT_FLASH
250+ ext_flash_unlock ();
251+ #endif
252+
248253 if (swapDone == 0 ) {
249254 /* IMG_STATE_FINAL_FLAGS allows re-entry without blowing away swap */
250255 if (st != IMG_STATE_FINAL_FLAGS ) {
@@ -282,6 +287,12 @@ static int wolfBoot_swap_and_final_erase(int resume)
282287 wolfBoot_set_partition_state (PART_BOOT , IMG_STATE_TESTING );
283288 /* erase the last sector(s) of update */
284289 wb_flash_erase (update , WOLFBOOT_PARTITION_SIZE - eraseLen , eraseLen );
290+
291+ #ifdef EXT_FLASH
292+ ext_flash_lock ();
293+ #endif
294+ hal_flash_lock ();
295+
285296 return 0 ;
286297}
287298#endif
@@ -480,15 +491,17 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot,
480491 sector ++ ;
481492 }
482493#ifndef DISABLE_BACKUP
483- /* start re-entrant final erase, return code is only for resumption in
484- * wolfBoot_start */
485- wolfBoot_swap_and_final_erase (0 );
486494#endif
487495out :
488496#ifdef EXT_FLASH
489497 ext_flash_lock ();
490498#endif
491499 hal_flash_lock ();
500+ /* start re-entrant final erase, return code is only for resumption in
501+ * wolfBoot_start */
502+ if (ret == 0 ) {
503+ wolfBoot_swap_and_final_erase (0 );
504+ }
492505 /* encryption key was not erased, will be erased by success */
493506 return ret ;
494507}
@@ -741,14 +754,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
741754 }
742755#endif /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
743756
744- /* start re-entrant final erase, return code is only for resumption in
745- * wolfBoot_start*/
746- wolfBoot_swap_and_final_erase (0 );
747757 /* encryption key was not erased, will be erased by success */
748758 #ifdef EXT_FLASH
749759 ext_flash_lock ();
750760 #endif
751761 hal_flash_lock ();
762+ /* start re-entrant final erase, return code is only for resumption in
763+ * wolfBoot_start*/
764+ wolfBoot_swap_and_final_erase (0 );
752765
753766#else /* DISABLE_BACKUP */
754767 /* Direct Swap without power fail safety */
0 commit comments