Skip to content

Commit c45268e

Browse files
bigbrettdanielinux
authored andcommitted
monolithic self-updates: force DISABLE_BACKUP=1, eliminate swap, eliminate update code
1 parent d387e39 commit c45268e

File tree

6 files changed

+51
-8
lines changed

6 files changed

+51
-8
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ assemble_internal_flash.dd: FORCE
470470
0 wolfboot.bin \
471471
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) test-app/image_v1_signed.bin \
472472
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS)-$(ARCH_FLASH_OFFSET))) /tmp/swap \
473-
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) /tmp/swap
473+
$(if $(DISABLE_BACKUP),,$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS)-$(ARCH_FLASH_OFFSET))) /tmp/swap) # swap unused with DISABLE_BACKUP
474474

475475
internal_flash.dd: $(BINASSEMBLE) wolfboot.bin $(BOOT_IMG) $(PRIVATE_KEY) test-app/image_v1_signed.bin
476476
@echo "\t[MERGE] internal_flash.dd"

config/examples/sim-self-update-monolithic.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ WOLFBOOT_PARTITION_SIZE=0x40000
1414
WOLFBOOT_SECTOR_SIZE=0x1000
1515
WOLFBOOT_PARTITION_BOOT_ADDRESS=0x20000
1616
WOLFBOOT_PARTITION_UPDATE_ADDRESS=0x60000
17-
WOLFBOOT_PARTITION_SWAP_ADDRESS=0xA0000
1817

1918
# required for keytools
2019
WOLFBOOT_FIXED_PARTITIONS=1

include/wolfboot/wolfboot.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ extern "C" {
487487

488488
#endif /* defined WOLFBOOT */
489489

490+
/* Monolithic self-update: imply DISABLE_BACKUP and enforce prerequisites */
491+
#ifdef WOLFBOOT_SELF_UPDATE_MONOLITHIC
492+
#ifndef DISABLE_BACKUP
493+
#define DISABLE_BACKUP
494+
#endif
495+
#ifdef DELTA_UPDATES
496+
#error "DELTA_UPDATES is not compatible with WOLFBOOT_SELF_UPDATE_MONOLITHIC"
497+
#endif
498+
#ifdef NVM_FLASH_WRITEONCE
499+
#error "NVM_FLASH_WRITEONCE is not compatible with WOLFBOOT_SELF_UPDATE_MONOLITHIC"
500+
#endif
501+
#ifndef RAM_CODE
502+
#error "WOLFBOOT_SELF_UPDATE_MONOLITHIC requires RAM_CODE"
503+
#endif
504+
#endif
505+
490506
#define PART_BOOT 0
491507
#define PART_UPDATE 1
492508
#define PART_SWAP 2

options.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ ifeq ($(WOLFBOOT_SELF_UPDATE_MONOLITHIC),1)
9898
endif
9999
ifeq ($(SELF_UPDATE_MONOLITHIC),1)
100100
CFLAGS+=-DWOLFBOOT_SELF_UPDATE_MONOLITHIC
101+
DISABLE_BACKUP=1
101102
endif
102103

103104
## Persist wolfBoot self header at fixed address
@@ -732,6 +733,7 @@ endif
732733
ifeq ($(DISABLE_BACKUP),1)
733734
$(warning DISABLE_BACKUP=1 disables power-fail-safe updates; losing power during an update can leave BOOT partially written and unrecoverable)
734735
CFLAGS+= -D"DISABLE_BACKUP"
736+
WOLFBOOT_PARTITION_SWAP_ADDRESS?=0
735737
endif
736738

737739
DEBUG_SYMBOLS?=0

src/update_flash.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ void RAMFUNCTION wolfBoot_check_self_update(void)
243243
}
244244
#endif /* RAM_CODE for self_update */
245245

246+
#ifndef WOLFBOOT_SELF_UPDATE_MONOLITHIC
247+
/* The swap-based update machinery (wolfBoot_copy_sector, wolfBoot_update, etc.)
248+
* is not used in monolithic self-update mode. */
249+
246250
static int RAMFUNCTION wolfBoot_copy_sector(struct wolfBoot_image *src,
247251
struct wolfBoot_image *dst, uint32_t sector)
248252
{
@@ -860,7 +864,10 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
860864
* magic has not been set flag will have an un-determined value when we go
861865
* to check it */
862866
uint8_t flag = SECT_FLAG_NEW;
863-
struct wolfBoot_image boot, update, swap;
867+
struct wolfBoot_image boot, update;
868+
#ifndef DISABLE_BACKUP
869+
struct wolfBoot_image swap;
870+
#endif
864871
uint16_t update_type;
865872
uint32_t fw_size;
866873
uint32_t size;
@@ -912,7 +919,9 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
912919
return -1;
913920
#endif
914921
wolfBoot_open_image(&boot, PART_BOOT);
922+
#ifndef DISABLE_BACKUP
915923
wolfBoot_open_image(&swap, PART_SWAP);
924+
#endif
916925

917926
#if defined(EXT_ENCRYPTED) && defined(DELTA_UPDATES)
918927
wolfBoot_printf("Update partition fallback image: %d\n", fallback_image);
@@ -1270,6 +1279,7 @@ static int RAMFUNCTION wolfBoot_update(int fallback_allowed)
12701279
#ifdef __CCRX__
12711280
#pragma section
12721281
#endif
1282+
#endif /* !WOLFBOOT_SELF_UPDATE_MONOLITHIC */
12731283

12741284
#if defined(ARCH_SIM) && defined(WOLFBOOT_TPM) && defined(WOLFBOOT_TPM_SEAL)
12751285
int wolfBoot_unlock_disk(void)
@@ -1382,12 +1392,14 @@ int wolfBoot_unlock_disk(void)
13821392
void RAMFUNCTION wolfBoot_start(void)
13831393
{
13841394
int bootRet;
1395+
#ifndef WOLFBOOT_SELF_UPDATE_MONOLITHIC
13851396
int updateRet;
13861397
#ifndef DISABLE_BACKUP
13871398
int resumedFinalErase;
13881399
#endif
13891400
uint8_t bootState;
13901401
uint8_t updateState;
1402+
#endif /* !WOLFBOOT_SELF_UPDATE_MONOLITHIC */
13911403
struct wolfBoot_image boot;
13921404

13931405
#if defined(ARCH_SIM) && defined(WOLFBOOT_TPM) && defined(WOLFBOOT_TPM_SEAL)
@@ -1398,6 +1410,8 @@ void RAMFUNCTION wolfBoot_start(void)
13981410
wolfBoot_check_self_update();
13991411
#endif
14001412

1413+
#ifndef WOLFBOOT_SELF_UPDATE_MONOLITHIC
1414+
14011415
#ifdef NVM_FLASH_WRITEONCE
14021416
/* nvm_select_fresh_sector needs unlocked flash in cases where the unused
14031417
* sector needs to be erased */
@@ -1456,6 +1470,12 @@ void RAMFUNCTION wolfBoot_start(void)
14561470
}
14571471
}
14581472

1473+
#else /* WOLFBOOT_SELF_UPDATE_MONOLITHIC */
1474+
#ifdef SECURE_PKCS11
1475+
WP11_Library_Init();
1476+
#endif
1477+
#endif /* !WOLFBOOT_SELF_UPDATE_MONOLITHIC */
1478+
14591479
bootRet = wolfBoot_open_image(&boot, PART_BOOT);
14601480
wolfBoot_printf("Booting version: 0x%x\n",
14611481
wolfBoot_get_blob_version(boot.hdr));
@@ -1467,6 +1487,7 @@ void RAMFUNCTION wolfBoot_start(void)
14671487
) {
14681488
wolfBoot_printf("Boot failed: Hdr %d, Hash %d, Sig %d\n",
14691489
boot.hdr_ok, boot.sha_ok, boot.signature_ok);
1490+
#ifndef WOLFBOOT_SELF_UPDATE_MONOLITHIC
14701491
wolfBoot_printf("Trying emergency update\n");
14711492
if (likely(wolfBoot_update(1) < 0)) {
14721493
/* panic: no boot option available. */
@@ -1490,6 +1511,13 @@ void RAMFUNCTION wolfBoot_start(void)
14901511
wolfBoot_panic();
14911512
}
14921513
}
1514+
#else
1515+
/* Monolithic mode: no emergency update path available */
1516+
#ifdef WOLFBOOT_TPM
1517+
wolfBoot_tpm2_deinit();
1518+
#endif
1519+
wolfBoot_panic();
1520+
#endif /* !WOLFBOOT_SELF_UPDATE_MONOLITHIC */
14931521
}
14941522
if ((boot.hdr_ok != 1U) || (boot.sha_ok != 1U) ||
14951523
(boot.signature_ok != 1U)) {

tools/test.mk

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,15 +282,13 @@ test-sim-self-update-monolithic: wolfboot.bin test-app/image_v1_signed.bin FORCE
282282
$(Q)dd if=/dev/zero bs=$$(($(WOLFBOOT_PARTITION_SIZE))) count=1 2>/dev/null | tr '\000' '\377' > update_part.dd
283283
$(Q)dd if=monolithic_payload_v2_signed.bin of=update_part.dd bs=1 conv=notrunc
284284
$(Q)printf "pBOOT" | dd of=update_part.dd bs=1 seek=$$(($(WOLFBOOT_PARTITION_SIZE) - 5)) conv=notrunc
285-
@# Create erased boot and swap partitions
285+
@# Create erased boot partition
286286
$(Q)dd if=/dev/zero bs=$$(($(WOLFBOOT_PARTITION_SIZE))) count=1 2>/dev/null | tr '\000' '\377' > boot_part.dd
287-
$(Q)dd if=/dev/zero bs=$$(($(WOLFBOOT_SECTOR_SIZE))) count=1 2>/dev/null | tr '\000' '\377' > erased_sec.dd
288-
@# Assemble flash: wolfboot.bin at 0, empty boot partition, update partition, swap
287+
@# Assemble flash: wolfboot.bin at 0, empty boot partition, update partition
289288
$(Q)$(BINASSEMBLE) internal_flash.dd \
290289
0 wolfboot.bin \
291290
$$(($(WOLFBOOT_PARTITION_BOOT_ADDRESS) - $(ARCH_FLASH_OFFSET))) boot_part.dd \
292-
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS) - $(ARCH_FLASH_OFFSET))) update_part.dd \
293-
$$(($(WOLFBOOT_PARTITION_SWAP_ADDRESS) - $(ARCH_FLASH_OFFSET))) erased_sec.dd
291+
$$(($(WOLFBOOT_PARTITION_UPDATE_ADDRESS) - $(ARCH_FLASH_OFFSET))) update_part.dd
294292
@# Run simulator - self-update fires, copies monolithic payload to offset 0
295293
$(Q)./wolfboot.elf get_version || true
296294
@# Verify bootloader region contains 0xAA pattern (dummy bootloader was written)

0 commit comments

Comments
 (0)