Skip to content

Commit 871f03c

Browse files
committed
Fix external image hdr_ok state on open failure
F/2580
1 parent 6ef9e02 commit 871f03c

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/image.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,6 +1451,7 @@ int wolfBoot_open_image_external(struct wolfBoot_image* img, uint8_t part,
14511451
uint8_t* addr)
14521452
{
14531453
uint8_t* image;
1454+
int ret;
14541455
if (img == NULL)
14551456
return -1;
14561457

@@ -1460,7 +1461,10 @@ int wolfBoot_open_image_external(struct wolfBoot_image* img, uint8_t part,
14601461
img->hdr_ok = 1;
14611462
hdr_cpy_done = 0; /* reset hdr "open" flag */
14621463
image = fetch_hdr_cpy(img);
1463-
return wolfBoot_open_image_address(img, image);
1464+
ret = wolfBoot_open_image_address(img, image);
1465+
if (ret != 0)
1466+
img->hdr_ok = 0;
1467+
return ret;
14641468
}
14651469
#endif /* EXT_FLASH */
14661470

tools/unit-tests/unit-image.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,15 @@ START_TEST(test_open_image)
870870
ck_assert_ptr_eq(img.fw_base, (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS
871871
+ 256);
872872

873+
/* Invalid external header must keep hdr_ok cleared on failure */
874+
ext_flash_erase(0, WOLFBOOT_SECTOR_SIZE);
875+
memset(&img, 0, sizeof(img));
876+
hdr_cpy_done = 0;
877+
ret = wolfBoot_open_image_external(&img, PART_UPDATE,
878+
(uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS);
879+
ck_assert_int_eq(ret, -1);
880+
ck_assert_uint_eq(img.hdr_ok, 0);
881+
873882
/* Self header must reject sizes beyond the partition payload budget */
874883
memset(self_hdr, 0xFF, sizeof(self_hdr));
875884
((uint32_t *)self_hdr)[0] = WOLFBOOT_MAGIC;

0 commit comments

Comments
 (0)