Skip to content

Commit 9078ede

Browse files
twcook86danielinux
authored andcommitted
Add an update test to the test-app.
Fix bugs found from the update test.
1 parent f819c04 commit 9078ede

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

src/libwolfboot.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,12 @@ static int RAMFUNCTION trailer_write(uint8_t part, uintptr_t addr, uint8_t val)
355355

356356
nvm_cached_sector = nvm_select_fresh_sector(part);
357357
addr_read = addr_align - (nvm_cached_sector * NVM_CACHE_SIZE);
358-
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
358+
#ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
359+
if (hal_flash_is_erased_at((uintptr_t)addr_read))
360+
XMEMSET(NVM_CACHE, 0xFF, NVM_CACHE_SIZE);
361+
else
362+
#endif
363+
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
359364
NVM_CACHE[addr_off] = val;
360365

361366
/* Calculate write address */
@@ -399,7 +404,12 @@ static int RAMFUNCTION partition_magic_write(uint8_t part, uintptr_t addr)
399404
nvm_cached_sector = nvm_select_fresh_sector(part);
400405
addr_read = base - (nvm_cached_sector * NVM_CACHE_SIZE);
401406
addr_write = base - (!nvm_cached_sector * NVM_CACHE_SIZE);
402-
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
407+
#ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
408+
if (hal_flash_is_erased_at((uintptr_t)addr_read))
409+
XMEMSET(NVM_CACHE, 0xFF, NVM_CACHE_SIZE);
410+
else
411+
#endif
412+
XMEMCPY(NVM_CACHE, (void*)addr_read, NVM_CACHE_SIZE);
403413
XMEMCPY(NVM_CACHE + off, &wolfboot_magic_trail, sizeof(uint32_t));
404414
ret = hal_flash_write(addr_write, NVM_CACHE, WOLFBOOT_SECTOR_SIZE);
405415
if (ret != 0)
@@ -852,12 +862,17 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
852862
#else
853863
uint32_t magic = WOLFBOOT_MAGIC_TRAIL;
854864
uint32_t offset = SECTOR_FLAGS_SIZE;
855-
#ifdef FLAGS_HOME
865+
# ifdef FLAGS_HOME
856866
offset -= (PART_BOOT_ENDFLAGS - PART_UPDATE_ENDFLAGS);
857-
#endif
867+
# endif
858868
selSec = nvm_select_fresh_sector(PART_UPDATE);
859-
XMEMCPY(NVM_CACHE, (uint8_t*)lastSector - WOLFBOOT_SECTOR_SIZE * selSec,
860-
WOLFBOOT_SECTOR_SIZE);
869+
# ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
870+
if (hal_flash_is_erased_at((uintptr_t)lastSector - WOLFBOOT_SECTOR_SIZE * selSec))
871+
XMEMSET(NVM_CACHE, 0xFF, WOLFBOOT_SECTOR_SIZE);
872+
else
873+
# endif
874+
XMEMCPY(NVM_CACHE, (uint8_t*)lastSector - WOLFBOOT_SECTOR_SIZE * selSec,
875+
WOLFBOOT_SECTOR_SIZE);
861876
/* write to the non selected sector */
862877
hal_flash_erase(lastSector - WOLFBOOT_SECTOR_SIZE * !selSec,
863878
WOLFBOOT_SECTOR_SIZE);
@@ -872,7 +887,6 @@ void RAMFUNCTION wolfBoot_update_trigger(void)
872887
#endif
873888
}
874889

875-
876890
if (FLAGS_UPDATE_EXT()) {
877891
ext_flash_lock();
878892
} else {

test-app/app_lpc55s69.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,27 @@ void main(void)
8787
wolfBoot_printf("Hello from firmware version %d\n", boot_ver);
8888

8989
if (boot_ver == 1) {
90+
uint32_t update_ver;
91+
9092
/* blue on */
9193
GPIO_PinWrite(GPIO, 1, BLUE_LED, 0);
94+
95+
#ifdef WOLFCRYPT_SECURE_MODE
96+
update_ver = wolfBoot_nsc_update_firmware_version();
97+
#else
98+
update_ver = wolfBoot_update_firmware_version();
99+
#endif
100+
101+
if (update_ver != 0) {
102+
wolfBoot_printf("Update firmware detected, version: 0x%lx\n", update_ver);
103+
wolfBoot_printf("Triggering update...\n");
104+
#ifdef WOLFCRYPT_SECURE_MODE
105+
wolfBoot_nsc_update_trigger();
106+
#else
107+
wolfBoot_update_trigger();
108+
#endif
109+
wolfBoot_printf("...done. Reboot to apply.\n");
110+
}
92111
}
93112
else {
94113
/* green on */

0 commit comments

Comments
 (0)