From 4d77e22c0ae54dd2727bb568381aefe72baa8a75 Mon Sep 17 00:00:00 2001 From: Dirk Van Haerenborgh Date: Thu, 19 Nov 2026 14:02:28 +0100 Subject: [PATCH] Copy sector in chunks with smaller NVM_CACHE This commit allows to specify an NVM_CACHE_SIZE that is smaller than SECTOR_SIZE. When writing to the trailer in the case of NVM_FLASH_WRITEONCE, you currently need WOLFBOOT_SECTOR_SIZE bytes available in ram, which we don't have. By specifying a smaller NVM_CACHE_SIZE, we can copy the sector in chunks, patching in the modifications while copying. We're keeping FLASHBUFFER_SIZE as is, meaning that NVM_CACHE_SIZE has to be a strict multiple of FLASHBUFFER_SIZE. I'm not sure how this will behave with regard to power loss during the chunked writes for write once memory, nor do I know how to test that. Looking at how FLASHBUFFER_SIZE handling is implemented, I don't expect this to introduce any more issues. We could simplify this by also using FLASHBUFFER_SIZE as the amount of ram used, but I wasn't sure about te uses of it to begin with. --- src/libwolfboot.c | 246 +++++++++++++++++++++----------- tools/unit-tests/Makefile | 25 +++- tools/unit-tests/unit-enc-key.c | 56 +++++++- 3 files changed, 234 insertions(+), 93 deletions(-) diff --git a/src/libwolfboot.c b/src/libwolfboot.c index 5dc56fea3c..4e140b2d6e 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -360,6 +360,24 @@ static int RAMFUNCTION part_is_erased(uint8_t part, uintptr_t addr, int len) uint8_t NVM_CACHE[NVM_CACHE_SIZE] XALIGNED(16); static int nvm_cached_sector = 0; +#if (NVM_CACHE_SIZE <= 0) +#error "NVM_CACHE_SIZE must be positive" +#elif (NVM_CACHE_SIZE > WOLFBOOT_SECTOR_SIZE) +#error "NVM_CACHE_SIZE must not exceed WOLFBOOT_SECTOR_SIZE" +#elif ((WOLFBOOT_SECTOR_SIZE % NVM_CACHE_SIZE) != 0) +#error "WOLFBOOT_SECTOR_SIZE must be a multiple of NVM_CACHE_SIZE" +#elif ((NVM_CACHE_SIZE % WOLFBOOT_FLASH_WRITE_UNIT) != 0) +#error "NVM_CACHE_SIZE must be a multiple of WOLFBOOT_FLASH_WRITE_UNIT" +#elif (FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE) && (FLASHBUFFER_SIZE <= 0) +#error "FLASHBUFFER_SIZE must be positive" +#elif (FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE) && \ + (FLASHBUFFER_SIZE > NVM_CACHE_SIZE) +#error "When FLASHBUFFER_SIZE is used for chunked writes, it must not exceed NVM_CACHE_SIZE" +#elif (FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE) && \ + ((NVM_CACHE_SIZE % FLASHBUFFER_SIZE) != 0) +#error "When FLASHBUFFER_SIZE is used for chunked writes, NVM_CACHE_SIZE must be a multiple of FLASHBUFFER_SIZE" +#endif + /* Scrub the staging buffer without depending on wolfCrypt: ForceZero() * would pull wolfSSL headers into every NVM_FLASH_WRITEONCE build, * including tools/check_config and the STM32Cube test-app, which cannot @@ -511,6 +529,72 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part) return sel; } +typedef struct { + uint32_t offset; + const uint8_t *data; + uint32_t length; +} nvm_sector_patch_t; + +static int RAMFUNCTION nvm_sector_copy_with_patches(uintptr_t src_sector, + uintptr_t dst_sector, const nvm_sector_patch_t *patches, + size_t patch_count, int clear_sector) +{ + uint32_t off = 0, chunk_start = 0, chunk_end = 0; + int ret; + + for (off = 0; off < WOLFBOOT_SECTOR_SIZE; off += NVM_CACHE_SIZE) { + size_t i; + chunk_start = off; + chunk_end = off + NVM_CACHE_SIZE; + + if (clear_sector) + XMEMSET(NVM_CACHE, FLASH_BYTE_ERASED, NVM_CACHE_SIZE); + else + XMEMCPY(NVM_CACHE, (void *)(src_sector + off), NVM_CACHE_SIZE); + for (i = 0; i < patch_count; i++) { + uint32_t patch_end = patches[i].offset + patches[i].length; + + if ((patches[i].length > 0) && (patch_end > chunk_start) && + (patches[i].offset < chunk_end)) { + uint32_t start = (patches[i].offset > chunk_start) ? + patches[i].offset : chunk_start; + uint32_t end = (patch_end < chunk_end) ? + patch_end : chunk_end; + XMEMCPY(NVM_CACHE + (start - chunk_start), + patches[i].data + (start - patches[i].offset), + end - start); + } + } +#if FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE + uint32_t chunk_off; + for (chunk_off = 0; chunk_off < NVM_CACHE_SIZE; + chunk_off += FLASHBUFFER_SIZE) { + ret = hal_flash_write(dst_sector + off + chunk_off, + NVM_CACHE + chunk_off, FLASHBUFFER_SIZE); + if (ret != 0) + return ret; + } +#else + ret = hal_flash_write(dst_sector + off, NVM_CACHE, NVM_CACHE_SIZE); + if (ret != 0) + return ret; +#endif + } + return 0; +} + +static int RAMFUNCTION nvm_sector_copy_with_patch(uintptr_t src_sector, + uintptr_t dst_sector, uint32_t patch_offset, const uint8_t *patch, + uint32_t patch_len, int clear_sector) +{ + const nvm_sector_patch_t patch_desc = { + patch_offset, patch, patch_len + }; + + return nvm_sector_copy_with_patches(src_sector, dst_sector, &patch_desc, + 1, clear_sector); +} + /** * @brief Write the trailer in a non-volatile memory. * @@ -523,31 +607,22 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part) */ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val) { - uintptr_t addr_off = addr % NVM_CACHE_SIZE; - uintptr_t addr_align = addr - addr_off; + uintptr_t sector_base = addr - (addr % WOLFBOOT_SECTOR_SIZE); + uint32_t patch_offset = (uint32_t)(addr - sector_base); uintptr_t addr_read, addr_write; int ret = 0; nvm_cached_sector = nvm_select_fresh_sector(part); - addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE); - XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE); - NVM_CACHE[addr_off] = val; + addr_read = sector_base - (nvm_cached_sector * WOLFBOOT_SECTOR_SIZE); /* Calculate write address */ - addr_write = addr_align - ((!nvm_cached_sector) * NVM_CACHE_SIZE); + addr_write = sector_base - ((!nvm_cached_sector) * WOLFBOOT_SECTOR_SIZE); - /* Ensure that the destination was erased */ - hal_flash_erase(addr_write, NVM_CACHE_SIZE); -#if FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE - addr_off = 0; - while ((addr_off < WOLFBOOT_SECTOR_SIZE) && (ret == 0)) { - ret = hal_flash_write(addr_write + addr_off, NVM_CACHE + addr_off, - FLASHBUFFER_SIZE); - addr_off += FLASHBUFFER_SIZE; + ret = hal_flash_erase(addr_write, WOLFBOOT_SECTOR_SIZE); + if (ret == 0) { + ret = nvm_sector_copy_with_patch(addr_read, addr_write, patch_offset, + &val, sizeof(val), 0); } -#else - ret = hal_flash_write(addr_write, NVM_CACHE, NVM_CACHE_SIZE); -#endif if (ret != 0) { /* The staged sector may hold the firmware key/nonce (see * ENCRYPT_CACHE under NVM_FLASH_WRITEONCE): scrub it before @@ -557,7 +632,7 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val) } /* Once a copy has been written, erase the older sector */ - ret = hal_flash_erase(addr_read, NVM_CACHE_SIZE); + ret = hal_flash_erase(addr_read, WOLFBOOT_SECTOR_SIZE); nvm_cached_sector = !nvm_cached_sector; nvm_cache_scrub(); return ret; @@ -574,17 +649,17 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val) */ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr) { - uintptr_t off = addr % NVM_CACHE_SIZE; - uintptr_t base = (uintptr_t)addr - off; + uintptr_t sector_base = addr - (addr % WOLFBOOT_SECTOR_SIZE); + uint32_t patch_offset = (uint32_t)(addr - sector_base); uintptr_t addr_read, addr_write; + uint32_t magic = wolfboot_magic_trail; int ret; nvm_cached_sector = nvm_select_fresh_sector(part); - addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE); - addr_write = base - (!nvm_cached_sector * NVM_CACHE_SIZE); - XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE); - XMEMCPY(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t)); - ret = hal_flash_write(addr_write, NVM_CACHE, WOLFBOOT_SECTOR_SIZE); + addr_read = sector_base - (nvm_cached_sector * WOLFBOOT_SECTOR_SIZE); + addr_write = sector_base - (!nvm_cached_sector * WOLFBOOT_SECTOR_SIZE); + ret = nvm_sector_copy_with_patch(addr_read, addr_write, patch_offset, + (const uint8_t *)&magic, sizeof(magic), 0); if (ret != 0) { /* The staged sector may hold the firmware key/nonce (see * ENCRYPT_CACHE under NVM_FLASH_WRITEONCE): scrub it before @@ -599,28 +674,19 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr) } #ifdef WOLFBOOT_SHARED_TRAILER -/* Write NVM_CACHE over the spare copy of the trailer, then erase copy sel */ -static int RAMFUNCTION nvm_cache_move(uintptr_t last, int sel) +/* Copy one trailer sector to the spare copy in cache-sized chunks. */ +static int RAMFUNCTION nvm_cache_move(uintptr_t last, int sel, int clear_sector, + uint32_t patch_offset, const uint8_t *patch, uint32_t patch_len) { uintptr_t addr_read = last - ((uintptr_t)sel * WOLFBOOT_SECTOR_SIZE); uintptr_t addr_write = last - ((uintptr_t)(!sel) * WOLFBOOT_SECTOR_SIZE); int ret; -#if FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE - uintptr_t off = 0; -#endif ret = hal_flash_erase(addr_write, WOLFBOOT_SECTOR_SIZE); -#if FLASHBUFFER_SIZE != WOLFBOOT_SECTOR_SIZE - while ((off < WOLFBOOT_SECTOR_SIZE) && (ret == 0)) { - ret = hal_flash_write(addr_write + off, NVM_CACHE + off, - FLASHBUFFER_SIZE); - off += FLASHBUFFER_SIZE; - } -#else if (ret == 0) { - ret = hal_flash_write(addr_write, NVM_CACHE, NVM_CACHE_SIZE); + ret = nvm_sector_copy_with_patch(addr_read, addr_write, patch_offset, + patch, patch_len, clear_sector); } -#endif if (ret == 0) { ret = hal_flash_erase(addr_read, WOLFBOOT_SECTOR_SIZE); } @@ -635,8 +701,12 @@ static int RAMFUNCTION flags_home_rewrite(int commit, uint8_t boot_state) uintptr_t last = (PART_BOOT_ENDFLAGS - 1U) & ~((uintptr_t)WOLFBOOT_SECTOR_SIZE - 1U); uintptr_t off = PART_BOOT_ENDFLAGS - sizeof(uint32_t) - last; + uint8_t patch[1 + sizeof(uint32_t)]; + uint32_t magic = wolfboot_magic_trail; + uint32_t patch_offset = (uint32_t)(off - 1U); int sel; int pass; + int clear_sector; int ret = 0; sel = nvm_select_fresh_sector(PART_UPDATE); @@ -644,19 +714,17 @@ static int RAMFUNCTION flags_home_rewrite(int commit, uint8_t boot_state) * one nvm_select_fresh_sector() keeps when no magic is found */ for (pass = (commit && (sel == 0)) ? 0 : 1; (pass < 2) && (ret == 0); pass++) { - XMEMCPY(NVM_CACHE, - (void *)(last - ((uintptr_t)sel * WOLFBOOT_SECTOR_SIZE)), - NVM_CACHE_SIZE); - if ((pass == 1) && commit) { - XMEMSET(NVM_CACHE, FLASH_BYTE_ERASED, NVM_CACHE_SIZE); - XMEMCPY(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t)); - NVM_CACHE[off - 1U] = boot_state; - } - else if (pass == 1) { - XMEMSET(NVM_CACHE + off - 1U, FLASH_BYTE_ERASED, - sizeof(uint32_t) + 1U); + clear_sector = 0; + if (pass == 1) { + XMEMSET(patch, FLASH_BYTE_ERASED, sizeof(patch)); + if (commit) { + clear_sector = 1; + patch[0] = boot_state; + XMEMCPY(patch + 1, &magic, sizeof(magic)); + } } - ret = nvm_cache_move(last, sel); + ret = nvm_cache_move(last, sel, clear_sector, patch_offset, + (pass == 1) ? patch : NULL, (pass == 1) ? sizeof(patch) : 0); sel = !sel; } nvm_cached_sector = sel; @@ -1774,23 +1842,29 @@ void RAMFUNCTION wolfBoot_update_trigger(void) #else uint32_t magic = WOLFBOOT_MAGIC_TRAIL; uint32_t offset = SECTOR_FLAGS_SIZE; + uint8_t state_patch[1 + sizeof(uint32_t)]; + uintptr_t src_sector; + uintptr_t dst_sector; + int ret; #ifdef FLAGS_HOME offset -= (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS); #endif selSec = nvm_select_fresh_sector(PART_UPDATE); - XMEMCPY(NVM_CACHE, (uint8_t*)lastSector - WOLFBOOT_SECTOR_SIZE * selSec, - WOLFBOOT_SECTOR_SIZE); + src_sector = lastSector - WOLFBOOT_SECTOR_SIZE * selSec; + dst_sector = lastSector - WOLFBOOT_SECTOR_SIZE * !selSec; /* write to the non selected sector */ - hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec, - WOLFBOOT_SECTOR_SIZE); - - NVM_CACHE[offset] = IMG_STATE_UPDATING; - memcpy(NVM_CACHE + offset + 1, &magic, sizeof(uint32_t)); - hal_flash_write(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec, NVM_CACHE, - WOLFBOOT_SECTOR_SIZE); - /* erase the previously selected sector */ - hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * selSec, - WOLFBOOT_SECTOR_SIZE); + ret = hal_flash_erase(dst_sector, WOLFBOOT_SECTOR_SIZE); + + if (ret == 0) { + state_patch[0] = IMG_STATE_UPDATING; + memcpy(state_patch + 1, &magic, sizeof(uint32_t)); + ret = nvm_sector_copy_with_patch(src_sector, dst_sector, offset, + state_patch, sizeof(state_patch), 0); + } + if (ret == 0) { + /* erase the previously selected sector */ + (void)hal_flash_erase(src_sector, WOLFBOOT_SECTOR_SIZE); + } /* The staged sector may hold the firmware key/nonce (see * ENCRYPT_CACHE under NVM_FLASH_WRITEONCE): scrub it, as the * partition-trailer helpers do, before releasing the flash @@ -2916,7 +2990,10 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce) int ret = 0; #ifdef NVM_FLASH_WRITEONCE uintptr_t addr_off; - uint32_t trailer_relative_off = 4; + uintptr_t trailer_relative_off = 4; + uintptr_t src_sector, dst_sector; + nvm_sector_patch_t patches[4]; + size_t patch_count = 0; int sel_sec = 0; #endif #if !defined(WOLFBOOT_SMALL_STACK) && !defined(NVM_FLASH_WRITEONCE) && \ @@ -2930,39 +3007,36 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce) addr_align = addr - addr_off; /* we read from the populated sector, now write to the erased sector */ sel_sec = nvm_select_fresh_sector(PART_BOOT); - addr_align -= (sel_sec * WOLFBOOT_SECTOR_SIZE); + src_sector = addr_align - (sel_sec * WOLFBOOT_SECTOR_SIZE); + dst_sector = addr_align - (!sel_sec * WOLFBOOT_SECTOR_SIZE); hal_flash_unlock(); - /* casting to unsigned long to abide compilers on 64bit architectures */ - XMEMCPY(ENCRYPT_CACHE, - (void*)(unsigned long)(addr_align), - WOLFBOOT_SECTOR_SIZE); - /* we read from the populated sector, now write to the erased sector */ - addr_align = addr - (addr % WOLFBOOT_SECTOR_SIZE); - addr_align -= (!sel_sec * WOLFBOOT_SECTOR_SIZE); - /* Populate key + nonce in the cache */ - XMEMCPY(ENCRYPT_CACHE + addr_off, k, ENCRYPT_KEY_SIZE); - XMEMCPY(ENCRYPT_CACHE + addr_off + ENCRYPT_KEY_SIZE, nonce, - ENCRYPT_NONCE_SIZE); + patches[patch_count].offset = addr_off; + patches[patch_count].data = k; + patches[patch_count++].length = ENCRYPT_KEY_SIZE; + patches[patch_count].offset = addr_off + ENCRYPT_KEY_SIZE; + patches[patch_count].data = nonce; + patches[patch_count++].length = ENCRYPT_NONCE_SIZE; /* Add a valid trailer */ - XMEMCPY(ENCRYPT_CACHE + addr_off - trailer_relative_off, - &wolfboot_magic_trail, 4); + patches[patch_count].offset = addr_off - trailer_relative_off; + patches[patch_count].data = (const uint8_t *)&wolfboot_magic_trail; + patches[patch_count++].length = sizeof(wolfboot_magic_trail); #ifdef FLAGS_HOME trailer_relative_off += (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS); - XMEMCPY(ENCRYPT_CACHE + addr_off - trailer_relative_off, - &wolfboot_magic_trail, 4); + patches[patch_count].offset = addr_off - trailer_relative_off; + patches[patch_count].data = (const uint8_t *)&wolfboot_magic_trail; + patches[patch_count++].length = sizeof(wolfboot_magic_trail); #endif /* Writing cache back to sector "!sel_sec" */ - ret = hal_flash_write(addr_align, ENCRYPT_CACHE, WOLFBOOT_SECTOR_SIZE); + ret = nvm_sector_copy_with_patches(src_sector, dst_sector, patches, + patch_count, 0); if (ret != 0) goto exit_lock; /* Erasing original sector "sel_sec", * same one returned from by nvm_select. */ - addr_align = addr - (addr % WOLFBOOT_SECTOR_SIZE); - addr_align -= (sel_sec * WOLFBOOT_SECTOR_SIZE); - ret = hal_flash_erase(addr_align, WOLFBOOT_SECTOR_SIZE); + ret = hal_flash_erase(src_sector, WOLFBOOT_SECTOR_SIZE); #else uintptr_t boot_base = WOLFBOOT_PARTITION_BOOT_ADDRESS; uintptr_t boot_end, unit, first_addr, end_addr, first_unit, last_unit; @@ -3038,7 +3112,9 @@ static int RAMFUNCTION hal_set_key(const uint8_t *k, const uint8_t *nonce) * buffer. The static cases (NVM_FLASH_WRITEONCE aliases it to * NVM_CACHE, WOLFBOOT_SMALL_STACK has its own array) are exactly * where the plaintext would otherwise stay resident. */ -#if !defined(WOLFBOOT_ENCRYPT_CACHE) +#ifdef NVM_FLASH_WRITEONCE + nvm_cache_scrub(); +#elif !defined(WOLFBOOT_ENCRYPT_CACHE) ForceZero(ENCRYPT_CACHE, NVM_CACHE_SIZE); #endif hal_flash_lock(); diff --git a/tools/unit-tests/Makefile b/tools/unit-tests/Makefile index 24c1245162..78c4e73b8c 100644 --- a/tools/unit-tests/Makefile +++ b/tools/unit-tests/Makefile @@ -75,6 +75,7 @@ TESTS:=unit-parser unit-parser-large-header unit-fdt unit-extflash unit-string \ unit-sectorflags unit-max-space \ unit-image unit-image-hybrid unit-image-rsa unit-nvm unit-nvm-flagshome unit-enc-nvm \ unit-enc-key unit-enc-key-aes256 unit-enc-key-straddle \ + unit-enc-key-writeonce \ unit-enc-key-sector unit-enc-key-flagshome \ unit-enc-nvm-flagshome unit-delta unit-gzip unit-update-flash unit-update-flash-delta \ unit-update-flash-db \ @@ -434,6 +435,12 @@ unit-enc-key-sector:CFLAGS+=-DMOCK_PARTITIONS -DEXT_FLASH -DEXT_ENCRYPTED \ -DWOLFBOOT_SECTOR_SIZE=0x1000 -DMOCK_FLASH_ERASE_UNIT=0x1000 unit-enc-key-sector:WOLFCRYPT_SRC+= \ $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/chacha.c +unit-enc-key-writeonce:CFLAGS+=-DMOCK_PARTITIONS -DEXT_FLASH -DEXT_ENCRYPTED \ + -DENCRYPT_WITH_CHACHA -DHAVE_CHACHA -DNVM_FLASH_WRITEONCE \ + -DWOLFBOOT_SECTOR_SIZE=0x1000 -DMOCK_FLASH_ERASE_UNIT=0x1000 \ + -DNVM_CACHE_SIZE=512 -DFLASHBUFFER_SIZE=512 +unit-enc-key-writeonce:WOLFCRYPT_SRC+= \ + $(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/chacha.c unit-enc-key-flagshome:CFLAGS+=-DMOCK_PARTITIONS -DEXT_FLASH -DEXT_ENCRYPTED \ -DENCRYPT_WITH_CHACHA -DHAVE_CHACHA -DWOLFBOOT_SMALL_STACK \ -DWOLFBOOT_SECTOR_SIZE=0x1000 -DWOLFBOOT_FLASH_ERASE_UNIT=128 \ @@ -476,7 +483,7 @@ unit-update-flash-flagshome:CFLAGS+=-DMOCK_PARTITIONS -DWOLFBOOT_NO_SIGN -DUNIT_ unit-update-flash-invert:CFLAGS+=-DMOCK_PARTITIONS -DWOLFBOOT_NO_SIGN -DUNIT_TEST_AUTH \ -DWOLFBOOT_HASH_SHA256 -DPRINTF_ENABLED -DEXT_FLASH -DPART_UPDATE_EXT \ -DNVM_FLASH_WRITEONCE -DWOLFBOOT_FLAGS_INVERT=1 -DFLAGS_HOME=1 \ - -DFLASHBUFFER_SIZE=512 \ + -DFLASHBUFFER_SIZE=512 -DNVM_CACHE_SIZE=512 \ -DWOLFBOOT_ORIGIN=MOCK_ADDRESS_BOOT -DBOOTLOADER_PARTITION_SIZE=WOLFBOOT_PARTITION_SIZE # Write-once trailers with inverted flags and every partition in internal flash unit-update-flash-int-invert:CFLAGS+=-DMOCK_PARTITIONS -DWOLFBOOT_NO_SIGN -DUNIT_TEST_AUTH \ @@ -1258,7 +1265,7 @@ unit-enc-nvm-flagshome: ../../include/target.h unit-enc-nvm.c gcc -o $@ $(WOLFCRYPT_SRC) unit-enc-nvm.c $(CFLAGS) $(WOLFCRYPT_CFLAGS) $(LDFLAGS) unit-enc-key unit-enc-key-aes256 unit-enc-key-straddle \ -unit-enc-key-sector unit-enc-key-flagshome: \ +unit-enc-key-sector unit-enc-key-writeonce unit-enc-key-flagshome: \ ../../include/target.h unit-enc-key.c gcc -o $@ $(WOLFCRYPT_SRC) unit-enc-key.c $(CFLAGS) \ $(WOLFCRYPT_CFLAGS) $(LDFLAGS) @@ -2544,12 +2551,15 @@ unit-stm32u5-write: unit-stm32u5-write.c stm32u5_write_extract.h # owns NVM_CACHE, stages a key-bearing sector image, and stubs the # flash layer; after each call the whole buffer must be zeroed. nvm_cache_scrub_extract.h: ../../src/libwolfboot.c - sed -n '/^static void RAMFUNCTION nvm_cache_scrub(/,/^}/p' $< > $@ + sed -n '/^typedef struct {/,/^} nvm_sector_patch_t;/p' $< > $@ + sed -n '/^static int RAMFUNCTION nvm_sector_copy_with_patches(/,/^}/p' $< >> $@ + sed -n '/^static int RAMFUNCTION nvm_sector_copy_with_patch(/,/^}/p' $< >> $@ + sed -n '/^static void RAMFUNCTION nvm_cache_scrub(/,/^}/p' $< >> $@ sed -n '/^static int RAMFUNCTION trailer_write(/,/^}/p' $< >> $@ sed -n '/^static int RAMFUNCTION partition_magic_write(/,/^}/p' $< >> $@ unit-nvm-cache-scrub: unit-nvm-cache-scrub.c nvm_cache_scrub_extract.h - gcc -o $@ unit-nvm-cache-scrub.c $(CFLAGS) $(LDFLAGS) + gcc -o $@ unit-nvm-cache-scrub.c -DXMEMSET=memset -DFLASH_BYTE_ERASED=0xFF $(CFLAGS) $(LDFLAGS) # unit-update-trigger-scrub runs the real wolfBoot_update_trigger() # from src/libwolfboot.c over the NVM_FLASH_WRITEONCE branch @@ -2558,11 +2568,14 @@ unit-nvm-cache-scrub: unit-nvm-cache-scrub.c nvm_cache_scrub_extract.h # The function is extracted together with nvm_cache_scrub(); the build # defines NVM_FLASH_WRITEONCE so the write-once branch compiles. update_trigger_scrub_extract.h: ../../src/libwolfboot.c - sed -n '/^static void RAMFUNCTION nvm_cache_scrub(/,/^}/p' $< > $@ + sed -n '/^typedef struct {/,/^} nvm_sector_patch_t;/p' $< > $@ + sed -n '/^static int RAMFUNCTION nvm_sector_copy_with_patches(/,/^}/p' $< >> $@ + sed -n '/^static int RAMFUNCTION nvm_sector_copy_with_patch(/,/^}/p' $< >> $@ + sed -n '/^static void RAMFUNCTION nvm_cache_scrub(/,/^}/p' $< >> $@ sed -n '/^void RAMFUNCTION wolfBoot_update_trigger(/,/^}/p' $< >> $@ unit-update-trigger-scrub: unit-update-trigger-scrub.c update_trigger_scrub_extract.h - gcc -o $@ unit-update-trigger-scrub.c -DNVM_FLASH_WRITEONCE $(CFLAGS) $(LDFLAGS) + gcc -o $@ unit-update-trigger-scrub.c -DNVM_FLASH_WRITEONCE -DXMEMSET=memset -DFLASH_BYTE_ERASED=0xFF -DFLASHBUFFER_SIZE=WOLFBOOT_SECTOR_SIZE $(CFLAGS) $(LDFLAGS) # unit-sdhci-uhs-recover drives disk_read()'s UHS recovery path from the # real src/sdhci.c (any read error permanently switched the host diff --git a/tools/unit-tests/unit-enc-key.c b/tools/unit-tests/unit-enc-key.c index 933b4c1701..e74ee53938 100644 --- a/tools/unit-tests/unit-enc-key.c +++ b/tools/unit-tests/unit-enc-key.c @@ -42,6 +42,10 @@ const char *argv0; #include "unit-mock-flash.c" +#ifdef NVM_FLASH_WRITEONCE +static int active_nvm_copy; +#endif + static void reset_erase_log(void) { mock_flash_erase_count = 0; @@ -66,10 +70,22 @@ static void check_erase_log(void) { uint8_t expected[WOLFBOOT_PARTITION_SIZE / MOCK_FLASH_ERASE_UNIT] = { 0 }; uintptr_t base = WOLFBOOT_PARTITION_BOOT_ADDRESS; +#ifdef NVM_FLASH_WRITEONCE + if (active_nvm_copy == 1) { + mark_expected_range(expected, base + WOLFBOOT_PARTITION_SIZE - + WOLFBOOT_SECTOR_SIZE, WOLFBOOT_SECTOR_SIZE); + } + else { + mark_expected_range(expected, base + WOLFBOOT_PARTITION_SIZE - + (2 * WOLFBOOT_SECTOR_SIZE), WOLFBOOT_SECTOR_SIZE); + } +#else uintptr_t key_addr = base + ENCRYPT_TMP_SECRET_OFFSET; uintptr_t magic_addr = key_addr - sizeof(uint32_t); +#endif size_t i, expected_count = 0; +#ifndef NVM_FLASH_WRITEONCE #ifdef FLAGS_HOME mark_expected_range(expected, magic_addr - (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS), sizeof(uint32_t)); @@ -78,6 +94,7 @@ static void check_erase_log(void) mark_expected_range(expected, key_addr, ENCRYPT_KEY_SIZE); mark_expected_range(expected, key_addr + ENCRYPT_KEY_SIZE, ENCRYPT_NONCE_SIZE); +#endif for (i = 0; i < sizeof(expected); i++) { if (!expected[i]) @@ -99,7 +116,16 @@ static void check_flash_contents(const uint8_t *key, const uint8_t *nonce, { const uint8_t *boot = (const uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS; uintptr_t base = WOLFBOOT_PARTITION_BOOT_ADDRESS; +#ifdef NVM_FLASH_WRITEONCE + uintptr_t active_base = base + WOLFBOOT_PARTITION_SIZE - + ((active_nvm_copy ? 2 : 1) * WOLFBOOT_SECTOR_SIZE); + uintptr_t erased_base = base + WOLFBOOT_PARTITION_SIZE - + ((active_nvm_copy ? 1 : 2) * WOLFBOOT_SECTOR_SIZE); + uintptr_t key_addr = active_base + + (ENCRYPT_TMP_SECRET_OFFSET % WOLFBOOT_SECTOR_SIZE); +#else uintptr_t key_addr = base + ENCRYPT_TMP_SECRET_OFFSET; +#endif uintptr_t magic_addr = key_addr - sizeof(uint32_t); uint8_t magic[sizeof(uint32_t)]; size_t i; @@ -108,6 +134,15 @@ static void check_flash_contents(const uint8_t *key, const uint8_t *nonce, for (i = 0; i < WOLFBOOT_PARTITION_SIZE; i++) { uint8_t expected = 0x5A; +#ifdef NVM_FLASH_WRITEONCE + if ((i >= erased_base - base) && + (i < erased_base - base + WOLFBOOT_SECTOR_SIZE)) + expected = FLASH_BYTE_ERASED; + if ((i >= active_base - base + WOLFBOOT_SECTOR_SIZE - + sizeof(uint32_t)) && + (i < active_base - base + WOLFBOOT_SECTOR_SIZE)) + expected = FLASH_BYTE_ERASED; +#endif #ifdef FLAGS_HOME uintptr_t flags_magic = magic_addr - (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS); @@ -120,10 +155,10 @@ static void check_flash_contents(const uint8_t *key, const uint8_t *nonce, expected = magic[i - (magic_addr - base)]; if (i >= key_addr - base && i < key_addr - base + ENCRYPT_KEY_SIZE) - expected = erased ? 0xFF : key[i - (key_addr - base)]; + expected = erased ? FLASH_BYTE_ERASED : key[i - (key_addr - base)]; if (i >= key_addr - base + ENCRYPT_KEY_SIZE && i < key_addr - base + ENCRYPT_KEY_SIZE + ENCRYPT_NONCE_SIZE) - expected = erased ? 0xFF : + expected = erased ? FLASH_BYTE_ERASED : nonce[i - (key_addr - base) - ENCRYPT_KEY_SIZE]; ck_assert_msg(boot[i] == expected, "unexpected byte at BOOT offset 0x%lx: got 0x%02x expected 0x%02x", @@ -159,10 +194,21 @@ START_TEST(test_set_and_erase_encrypt_key) ck_assert_int_ge(ret, 0); memset(boot, 0x5A, WOLFBOOT_PARTITION_SIZE); +#ifdef NVM_FLASH_WRITEONCE + memset(boot + WOLFBOOT_PARTITION_SIZE - (2 * WOLFBOOT_SECTOR_SIZE), + FLASH_BYTE_ERASED, WOLFBOOT_SECTOR_SIZE); + memset(boot + WOLFBOOT_PARTITION_SIZE - sizeof(uint32_t), + FLASH_BYTE_ERASED, sizeof(uint32_t)); + XMEMCPY((void *)(PART_BOOT_ENDFLAGS - sizeof(wolfboot_magic_trail)), + &wolfboot_magic_trail, sizeof(wolfboot_magic_trail)); +#endif reset_erase_log(); ret = wolfBoot_set_encrypt_key(key, nonce); ck_assert_int_eq(ret, 0); ck_assert(locked); +#ifdef NVM_FLASH_WRITEONCE + active_nvm_copy = 1; +#endif check_erase_log(); check_flash_contents(key, nonce, 0); check_cache_scrubbed(); @@ -171,10 +217,15 @@ START_TEST(test_set_and_erase_encrypt_key) ret = wolfBoot_erase_encrypt_key(); ck_assert_int_eq(ret, 0); ck_assert(locked); +#ifdef NVM_FLASH_WRITEONCE + active_nvm_copy = 0; +#endif check_erase_log(); check_flash_contents(key, nonce, 1); check_cache_scrubbed(); +#ifndef NVM_FLASH_WRITEONCE + /* The failure sequence below assumes ordinary, non-ping-pong key storage. */ reset_erase_log(); hal_flash_erase_fail = 1; ret = wolfBoot_set_encrypt_key(key, nonce); @@ -209,6 +260,7 @@ START_TEST(test_set_and_erase_encrypt_key) ck_assert_int_eq(ret, -1); ck_assert(locked); check_cache_scrubbed(); +#endif } END_TEST