@@ -265,7 +265,6 @@ static int wolfBoot_swap_and_final_erase(int resume)
265265 wb_flash_write (boot , tmpBootPos , (void * )tmpBuffer , sizeof (tmpBuffer ));
266266 }
267267 /* erase the last boot sector(s) */
268- wolfBoot_printf ("Erasing unused boot sectors...\n" );
269268 wb_flash_erase (boot , WOLFBOOT_PARTITION_SIZE - eraseLen , eraseLen );
270269 /* set the encryption key */
271270#ifdef EXT_ENCRYPTED
@@ -282,7 +281,6 @@ static int wolfBoot_swap_and_final_erase(int resume)
282281 /* mark boot as TESTING */
283282 wolfBoot_set_partition_state (PART_BOOT , IMG_STATE_TESTING );
284283 /* erase the last sector(s) of update */
285- wolfBoot_printf ("Erasing unused update sectors...\n" );
286284 wb_flash_erase (update , WOLFBOOT_PARTITION_SIZE - eraseLen , eraseLen );
287285 return 0 ;
288286}
@@ -509,9 +507,6 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
509507 uint32_t up_v ;
510508#endif
511509 uint32_t cur_ver , upd_ver ;
512- #ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
513- size_t remainderBytes ;
514- #endif
515510
516511 wolfBoot_printf ("Staring Update (fallback allowed %d)\n" , fallback_allowed );
517512
@@ -668,37 +663,30 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
668663 }
669664 }
670665
671- #ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
672- /* Performant option: Erase remainder of flash sectors in one HAL command */
673-
674666#ifdef NVM_FLASH_WRITEONCE
675- /* erase up until the start of the second-to-last sector for writeonce */
676- remainderBytes =
677- WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - (2 * sector_size );
667+ /* erase up until the start of the second-to-last sector for writeonce */
668+ size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - (2 * sector_size );
678669#else
679- /* erase up until the start of the last sector */
680- remainderBytes =
681- WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - sector_size ;
670+ /* erase up until the start of the last sector */
671+ size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - sector_size ;
682672#endif
683- wb_flash_erase (& boot , sector * sector_size , remainderBytes );
684- wb_flash_erase (& update , sector * sector_size , remainderBytes );
685673
686- #else /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
687- /* Smaller code size option: Iterate over every remaining sector and erase it
688- * individually. Required on some targets (stm32f4) to pass code size check */
674+ wolfBoot_printf ("Erasing remainder of partition (%d sectors)...\n" ,
675+ size /sector_size );
689676
690- /* erase to the last sector, writeonce has 2 sectors */
691- while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE -
692- sector_size
693- #ifdef NVM_FLASH_WRITEONCE
694- * 2
695- #endif
696- ) {
677+ #ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
678+ /* Performant option: Erase remainder of flash sectors in one HAL command */
679+ wb_flash_erase (& boot , sector * sector_size , size );
680+ wb_flash_erase (& update , sector * sector_size , size );
681+ #else
682+ /* Smaller code size option: Iterate over every remaining sector and erase
683+ * individually. Required on some targets (like stm32f4) due to code size */
684+ while (size >= sector_size ) {
697685 wb_flash_erase (& boot , sector * sector_size , sector_size );
698686 wb_flash_erase (& update , sector * sector_size , sector_size );
699687 sector ++ ;
688+ size -= sector_size ;
700689 }
701-
702690#endif /* !WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
703691
704692 /* start re-entrant final erase, return code is only for resumption in
@@ -728,9 +716,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
728716 wolfBoot_copy_sector (& update , & boot , sector );
729717 sector ++ ;
730718 }
731- while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
719+ /* erase remainder of partition */
720+ size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size );
721+ wolfBoot_printf ("Erasing remainder of partition (%d sectors)...\n" ,
722+ size /sector_size );
723+ while (size >= sector_size ) {
732724 wb_flash_erase (& boot , sector * sector_size , sector_size );
733725 sector ++ ;
726+ size -= sector_size ;
734727 }
735728 wolfBoot_set_partition_state (PART_BOOT , IMG_STATE_SUCCESS );
736729
0 commit comments