Fix update sector flag index truncation

F/2586
pull/748/head
Daniele Lacamera 2026-04-14 14:26:42 +02:00
parent cc7c3bbb68
commit af241649df
2 changed files with 41 additions and 2 deletions

View File

@ -693,7 +693,7 @@ int RAMFUNCTION wolfBoot_set_update_sector_flag(uint16_t sector,
uint32_t *magic;
uint8_t *flags;
uint8_t fl_value;
uint8_t pos = sector >> 1;
uint32_t pos = sector >> 1;
magic = get_partition_magic(PART_UPDATE);
if (*magic != wolfboot_magic_trail)
@ -748,7 +748,7 @@ int wolfBoot_get_update_sector_flag(uint16_t sector, uint8_t *flag)
{
uint32_t *magic;
uint8_t *flags;
uint8_t pos = sector >> 1;
uint32_t pos = sector >> 1;
magic = get_partition_magic(PART_UPDATE);
if (*magic != WOLFBOOT_MAGIC_TRAIL)
return -1;

View File

@ -362,6 +362,43 @@ START_TEST(test_get_update_sector_flag_rejects_invalid_magic)
}
END_TEST
START_TEST(test_update_sector_flag_high_index_does_not_alias_low_index)
{
int ret;
uint8_t st = 0;
ret = mmap_file("/tmp/wolfboot-unit-high-index.bin", (void *)MOCK_ADDRESS,
WOLFBOOT_PARTITION_SIZE, NULL);
ck_assert(ret >= 0);
#ifdef FLAGS_HOME
ret = mmap_file("/tmp/wolfboot-unit-high-index-int.bin",
(void *)MOCK_ADDRESS_BOOT, WOLFBOOT_PARTITION_SIZE, NULL);
ck_assert(ret >= 0);
#endif
ret = mmap_file("/tmp/wolfboot-unit-high-index-swap.bin",
(void *)MOCK_ADDRESS_SWAP, WOLFBOOT_SECTOR_SIZE, NULL);
ck_assert(ret >= 0);
hal_flash_unlock();
wolfBoot_erase_partition(PART_UPDATE);
ret = wolfBoot_set_update_sector_flag(1, SECT_FLAG_SWAPPING);
ck_assert_int_eq(ret, 0);
ret = wolfBoot_set_update_sector_flag(512, SECT_FLAG_UPDATED);
ck_assert_int_eq(ret, 0);
ret = wolfBoot_get_update_sector_flag(1, &st);
ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(st, SECT_FLAG_SWAPPING);
ret = wolfBoot_get_update_sector_flag(512, &st);
ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(st, SECT_FLAG_UPDATED);
hal_flash_lock();
}
END_TEST
Suite *wolfboot_suite(void)
{
@ -375,6 +412,8 @@ Suite *wolfboot_suite(void)
test_partition_magic_write_stops_on_flash_write_error);
tcase_add_test(nvm_select_fresh_sector,
test_get_update_sector_flag_rejects_invalid_magic);
tcase_add_test(nvm_select_fresh_sector,
test_update_sector_flag_high_index_does_not_alias_low_index);
suite_add_tcase(s, nvm_select_fresh_sector);
return s;