diff --git a/config/examples/sim-self-update-monolithic.config b/config/examples/sim-self-update-monolithic.config index adc2cce8..a2d0597c 100644 --- a/config/examples/sim-self-update-monolithic.config +++ b/config/examples/sim-self-update-monolithic.config @@ -6,6 +6,7 @@ WOLFBOOT_SMALL_STACK?=0 SPI_FLASH=0 DEBUG=1 RAM_CODE=1 +SELF_UPDATE_MONOLITHIC=1 WOLFBOOT_VERSION=1 # sizes should be multiple of system page size diff --git a/options.mk b/options.mk index 03899acf..108e330a 100644 --- a/options.mk +++ b/options.mk @@ -74,6 +74,12 @@ ifeq ($(WOLFBOOT_TPM_SEAL),1) endif endif +## Monolithic self-update: erase covers fw_size so the payload can span +## the bootloader region into the contiguous boot partition. +ifeq ($(SELF_UPDATE_MONOLITHIC),1) + CFLAGS+=-DWOLFBOOT_SELF_UPDATE_MONOLITHIC +endif + ## Persist wolfBoot self header at fixed address ## Invariants and defaults are enforced in wolfboot.h ifeq ($(WOLFBOOT_SELF_HEADER),1) diff --git a/src/update_flash.c b/src/update_flash.c index 387160dc..b5e588bd 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -68,11 +68,19 @@ static uint8_t buffer[FLASHBUFFER_SIZE] XALIGNED(4); #endif -static void RAMFUNCTION wolfBoot_erase_bootloader(void) +static void RAMFUNCTION wolfBoot_erase_bootloader(uint32_t len) { - uint32_t len = WOLFBOOT_PARTITION_BOOT_ADDRESS - ARCH_FLASH_OFFSET; +#ifdef WOLFBOOT_SELF_UPDATE_MONOLITHIC + /* Erase the full write range (rounded up to sector boundary) so that + * a monolithic payload that spills past the bootloader region into the + * contiguous boot partition lands on erased flash. */ + len = ((len + WOLFBOOT_SECTOR_SIZE - 1) / + WOLFBOOT_SECTOR_SIZE) * WOLFBOOT_SECTOR_SIZE; +#else + (void)len; + len = WOLFBOOT_PARTITION_BOOT_ADDRESS - ARCH_FLASH_OFFSET; +#endif hal_flash_erase(ARCH_FLASH_OFFSET, len); - } #include @@ -155,7 +163,7 @@ static void RAMFUNCTION wolfBoot_self_update(struct wolfBoot_image *src) #endif hal_flash_unlock(); - wolfBoot_erase_bootloader(); + wolfBoot_erase_bootloader(src->fw_size); #ifdef EXT_FLASH if (PART_IS_EXT(src)) { while (pos < src->fw_size) {