Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/firmware_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,11 @@ The persisted header also sets the version a new payload must exceed. A
staged payload is installed only if its version is greater than both the
running bootloader's `WOLFBOOT_VERSION` and the version in the
self-header. The bootloader inside each payload can therefore keep the
same `WOLFBOOT_VERSION` across releases. Without the self-header only
`WOLFBOOT_VERSION` is checked, so the bootloader in each payload must be
built with `WOLFBOOT_VERSION` equal to the payload's signed version,
otherwise the update is rejected.
same `WOLFBOOT_VERSION` across releases. If the self-header is present
but its version cannot be read, the payload is not installed. Without
the self-header only `WOLFBOOT_VERSION` is checked, so the bootloader in
each payload must be built with `WOLFBOOT_VERSION` equal to the payload's
signed version, otherwise the update is rejected.

##### Restrictions

Expand Down
4 changes: 2 additions & 2 deletions src/libwolfboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -2222,7 +2222,7 @@ int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset,
#if defined(__WOLFBOOT) && defined(WOLFBOOT_LOAD_ADDRESS)
extern uint8_t _end[]; /* linker symbol: end of wolfBoot BSS */
#endif
static uint8_t dec_hdr[IMAGE_HEADER_SIZE];
static uint8_t dec_hdr[IMAGE_HEADER_SIZE] XALIGNED(4);

static int decrypt_header(uint8_t *src)
{
Expand Down Expand Up @@ -2407,7 +2407,7 @@ static uint8_t* wolfBoot_get_image_from_part(uint8_t part)
uint8_t* wolfBoot_get_self_header(void)
{
#if defined(EXT_FLASH) && defined(WOLFBOOT_SELF_HEADER_EXT)
static uint8_t hdr_buf[IMAGE_HEADER_SIZE];
static uint8_t hdr_buf[IMAGE_HEADER_SIZE] XALIGNED(4);
uint32_t magic;

#ifdef NVM_FLASH_ECC
Expand Down
16 changes: 13 additions & 3 deletions src/update_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,20 @@ void RAMFUNCTION wolfBoot_check_self_update(void)
#if defined(WOLFBOOT_SELF_UPDATE_MONOLITHIC) && defined(WOLFBOOT_SELF_HEADER)
/* The self-header has the installed payload's version, which may be
* newer than the WOLFBOOT_VERSION this bootloader was built with */
uint32_t self_version = wolfBoot_get_self_version();
uint8_t *self_hdr = wolfBoot_get_self_header();

if (self_version > current_version) {
current_version = self_version;
if (self_hdr != NULL) {
uint32_t self_version = wolfBoot_get_blob_version(self_hdr);

/* A valid header always has a version. Without it the update
* cannot be checked against the installed payload. */
if (self_version == 0) {
wolfBoot_printf("Self header version unreadable\n");
return;
}
if (self_version > current_version) {
current_version = self_version;
}
}
#endif
if (update_version <= current_version) {
Expand Down
33 changes: 33 additions & 0 deletions tools/unit-tests/unit-update-flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,36 @@ START_TEST (test_self_update_self_header_newversion_installs)
cleanup_flash();
}
END_TEST

/* The self-header is present but has no version field, so the update cannot
* be checked against it. It is not installed, and stays staged. */
START_TEST (test_self_update_self_header_no_version_denied)
{
uintptr_t hdr = WOLFBOOT_PARTITION_SELF_HEADER_ADDRESS;
uint32_t magic = WOLFBOOT_MAGIC;
uint32_t size_img = host_to_img_u32(TEST_SIZE_SMALL);

reset_mock_stats();
prepare_flash();
clear_erase_stats();
ext_flash_unlock();
ext_flash_erase(hdr, WOLFBOOT_SELF_HEADER_SIZE);
ext_flash_write(hdr, (void *)&magic, 4);
ext_flash_write(hdr + 4, (void *)&size_img, 4);
ext_flash_lock();
stage_self_update(WOLFBOOT_VERSION + 1);
ck_assert_ptr_nonnull(wolfBoot_get_self_header());
ck_assert_uint_eq(wolfBoot_get_self_version(), 0);

wolfBoot_check_self_update();

ck_assert_int_eq(arch_reboot_called, 0);
ck_assert_int_eq(erased_update, 0);
ck_assert_uint_eq(*(uint32_t *)(uintptr_t)WOLFBOOT_PARTITION_BOOT_ADDRESS,
0xFFFFFFFFu);
cleanup_flash();
}
END_TEST
#endif /* WOLFBOOT_SELF_UPDATE_MONOLITHIC && WOLFBOOT_SELF_HEADER */
#endif

Expand Down Expand Up @@ -2373,6 +2403,7 @@ Suite *wolfboot_suite(void)
TCase *self_header_sameversion = tcase_create("Self update self-header same version erased");
TCase *self_header_oldversion = tcase_create("Self update self-header older version erased");
TCase *self_header_newversion = tcase_create("Self update self-header newer version installs");
TCase *self_header_no_version = tcase_create("Self update self-header without version denied");
#endif

tcase_add_test(self_update_sameversion, test_self_update_sameversion_erased);
Expand All @@ -2383,6 +2414,7 @@ Suite *wolfboot_suite(void)
tcase_add_test(self_header_sameversion, test_self_update_self_header_sameversion_erased);
tcase_add_test(self_header_oldversion, test_self_update_self_header_oldversion_erased);
tcase_add_test(self_header_newversion, test_self_update_self_header_newversion_installs);
tcase_add_test(self_header_no_version, test_self_update_self_header_no_version_denied);
#endif

suite_add_tcase(s, self_update_sameversion);
Expand All @@ -2397,6 +2429,7 @@ Suite *wolfboot_suite(void)
suite_add_tcase(s, self_header_sameversion);
suite_add_tcase(s, self_header_oldversion);
suite_add_tcase(s, self_header_newversion);
suite_add_tcase(s, self_header_no_version);
#endif
#endif
return s;
Expand Down
Loading