@@ -663,31 +663,38 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
663663 }
664664 }
665665
666+ /* calculate number of remaining bytes */
667+ /* reserve 1 sector for status (2 sectors for NV write once) */
668+ #if defined(WOLFBOOT_FLASH_MULTI_SECTOR_ERASE ) || defined(PRINTF_ENABLED )
666669#ifdef NVM_FLASH_WRITEONCE
667- /* erase up until the start of the second-to-last sector for writeonce */
668670 size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - (2 * sector_size );
669671#else
670- /* erase up until the start of the last sector */
671672 size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size ) - sector_size ;
672673#endif
673674
674- wolfBoot_printf ("Erasing remainder of partition (%d sectors)...\n" ,
675+ wolfBoot_printf ("Erasing remainder of partitions (%d sectors)...\n" ,
675676 size /sector_size );
677+ #endif
676678
677679#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
678680 /* Performant option: Erase remainder of flash sectors in one HAL command */
681+ /* If the HAL supports erase of multiple sectors this could improve performance */
679682 wb_flash_erase (& boot , sector * sector_size , size );
680683 wb_flash_erase (& update , sector * sector_size , size );
681684#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 ) {
685+ /* Iterate over every remaining sector and erase individually. */
686+ /* This loop is smallest code size */
687+ while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE -
688+ sector_size
689+ #ifdef NVM_FLASH_WRITEONCE
690+ * 2
691+ #endif
692+ ) {
685693 wb_flash_erase (& boot , sector * sector_size , sector_size );
686694 wb_flash_erase (& update , sector * sector_size , sector_size );
687695 sector ++ ;
688- size -= sector_size ;
689696 }
690- #endif /* ! WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
697+ #endif /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
691698
692699 /* start re-entrant final erase, return code is only for resumption in
693700 * wolfBoot_start*/
@@ -699,7 +706,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
699706 hal_flash_lock ();
700707
701708#else /* DISABLE_BACKUP */
702- /* Direct Swap without power fail saftey */
709+ /* Direct Swap without power fail safety */
703710
704711 hal_flash_unlock ();
705712 #ifdef EXT_FLASH
@@ -717,13 +724,14 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
717724 sector ++ ;
718725 }
719726 /* erase remainder of partition */
727+ #ifdef PRINTF_ENABLED
720728 size = WOLFBOOT_PARTITION_SIZE - (sector * sector_size );
721729 wolfBoot_printf ("Erasing remainder of partition (%d sectors)...\n" ,
722730 size /sector_size );
723- while (size >= sector_size ) {
731+ #endif
732+ while ((sector * sector_size ) < WOLFBOOT_PARTITION_SIZE ) {
724733 wb_flash_erase (& boot , sector * sector_size , sector_size );
725734 sector ++ ;
726- size -= sector_size ;
727735 }
728736 wolfBoot_set_partition_state (PART_BOOT , IMG_STATE_SUCCESS );
729737
0 commit comments