Skip to content

Commit 1970fbd

Browse files
committed
Fix for header type on SPI flash
1 parent 732c82c commit 1970fbd

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

include/wolfboot/wolfboot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ void wolfBoot_erase_partition(uint8_t part);
7373
void wolfBoot_update_trigger(void);
7474
void wolfBoot_success(void);
7575
uint32_t wolfBoot_get_image_version(uint8_t part);
76+
uint16_t wolfBoot_get_image_type(uint8_t part);
7677
#define wolfBoot_current_firmware_version() wolfBoot_get_image_version(PART_BOOT)
7778
#define wolfBoot_update_firmware_version() wolfBoot_get_image_version(PART_UPDATE)
7879

src/libwolfboot.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,31 @@ uint32_t wolfBoot_get_image_version(uint8_t part)
304304
}
305305
return 0;
306306
}
307+
308+
uint16_t wolfBoot_get_image_type(uint8_t part)
309+
{
310+
uint16_t *type_field = NULL;
311+
uint8_t *image = NULL;
312+
uint32_t *magic = NULL;
313+
if(part == PART_UPDATE) {
314+
#ifdef PART_UPDATE_EXT
315+
ext_flash_read((uint32_t)WOLFBOOT_PARTITION_UPDATE_ADDRESS, hdr_cpy, IMAGE_HEADER_SIZE);
316+
hdr_cpy_done = 1;
317+
image = hdr_cpy;
318+
#else
319+
image = (uint8_t *)WOLFBOOT_PARTITION_UPDATE_ADDRESS;
320+
#endif
321+
}
322+
if (part == PART_BOOT)
323+
image = (uint8_t *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
324+
325+
if (image) {
326+
magic = (uint32_t *)image;
327+
if (*magic != WOLFBOOT_MAGIC)
328+
return 0;
329+
wolfBoot_find_header(image + IMAGE_HEADER_OFFSET, HDR_IMG_TYPE, (void *)&type_field);
330+
if (type_field)
331+
return *type_field;
332+
}
333+
return 0;
334+
}

src/loader.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,15 @@ static int wolfBoot_update(int fallback_allowed)
9595
/* Check the first sector to detect interrupted update */
9696
if ((wolfBoot_get_sector_flag(PART_UPDATE, 0, &flag) < 0) || (flag == SECT_FLAG_NEW))
9797
{
98-
uint8_t *update_type;
98+
uint16_t update_type;
9999
/* In case this is a new update, do the required
100100
* checks on the firmware update
101101
* before starting the swap
102102
*/
103103

104-
if (wolfBoot_find_header(update.hdr + IMAGE_HEADER_OFFSET, HDR_IMG_TYPE, &update_type) == sizeof(uint16_t)) {
105-
if ((update_type[0] != HDR_IMG_TYPE_APP) || update_type[1] != (HDR_IMG_TYPE_AUTH >> 8))
106-
return -1;
107-
}
104+
update_type = wolfBoot_get_image_type(PART_UPDATE);
105+
if (((update_type & 0x00FF) != HDR_IMG_TYPE_APP) || ((update_type & 0xFF00) != HDR_IMG_TYPE_AUTH))
106+
return -1;
108107
if (!update.hdr_ok || (wolfBoot_verify_integrity(&update) < 0)
109108
|| (wolfBoot_verify_authenticity(&update) < 0)) {
110109
return -1;
@@ -307,9 +306,7 @@ static void wolfBoot_check_self_update(void)
307306
/* Check for self update in the UPDATE partition */
308307
if ((wolfBoot_get_partition_state(PART_UPDATE, &st) == 0) && (st == IMG_STATE_UPDATING) &&
309308
(wolfBoot_open_image(&update, PART_UPDATE) == 0) &&
310-
(wolfBoot_find_header(update.hdr + IMAGE_HEADER_OFFSET, HDR_IMG_TYPE, &update_type) == sizeof(uint16_t)) &&
311-
update_type[0] == HDR_IMG_TYPE_WOLFBOOT &&
312-
update_type[1] == (HDR_IMG_TYPE_AUTH >> 8)) {
309+
wolfBoot_get_image_type(PART_UPDATE) == (HDR_IMG_TYPE_WOLFBOOT | HDR_IMG_TYPE_AUTH)) {
313310
uint32_t update_version = wolfBoot_update_firmware_version();
314311
if (update_version <= wolfboot_version) {
315312
hal_flash_unlock();

0 commit comments

Comments
 (0)