Skip to content

Commit 2958738

Browse files
committed
Fix double-write-after-erase in NVM...WRITEONCE
when wolfBoot_update_trigger is called on a device with NVM_ write-once workaround, it will cause an illegal second write to flash to set the UPDATING flag. Instead, write the flag and the trailer in NVM_CACHE before committing to the flash.
1 parent 2b996f8 commit 2958738

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/libwolfboot.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,13 +753,25 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
753753
* not match what's in wolfBoot */
754754
if (FLAGS_UPDATE_EXT()) {
755755
ext_flash_erase(lastSector, SECTOR_FLAGS_SIZE);
756+
wolfBoot_set_partition_state(PART_UPDATE, st);
756757
} else {
757758
#ifndef NVM_FLASH_WRITEONCE
758759
hal_flash_erase(lastSector, SECTOR_FLAGS_SIZE);
760+
wolfBoot_set_partition_state(PART_UPDATE, st);
759761
#else
762+
uint32_t magic_trail = WOLFBOOT_MAGIC_TRAIL;
760763
selSec = nvm_select_fresh_sector(PART_UPDATE);
761764
XMEMCPY(NVM_CACHE, (uint8_t*)lastSector - WOLFBOOT_SECTOR_SIZE * selSec,
762765
WOLFBOOT_SECTOR_SIZE);
766+
767+
/* Set the IMG_STATE_UPDATING flag and
768+
* the trailer magic in cache before committing to flash
769+
*/
770+
NVM_CACHE[WOLFBOOT_SECTOR_SIZE - (sizeof(uint32_t) + 1)] =
771+
IMG_STATE_UPDATING;
772+
XMEMCPY(NVM_CACHE + WOLFBOOT_SECTOR_SIZE - sizeof(uint32_t),
773+
&magic_trail, sizeof(uint32_t));
774+
763775
/* write to the non selected sector */
764776
hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec,
765777
WOLFBOOT_SECTOR_SIZE);
@@ -771,8 +783,6 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
771783
#endif
772784
}
773785

774-
wolfBoot_set_partition_state(PART_UPDATE, st);
775-
776786
if (FLAGS_UPDATE_EXT()) {
777787
ext_flash_lock();
778788
} else {

0 commit comments

Comments
 (0)