Skip to content

Commit 45064ce

Browse files
authored
Merge pull request #20 from wolfSSL/wolfboot-update-fix
Wolfboot self-update fix
2 parents 732c82c + 6a5ee45 commit 45064ce

4 files changed

Lines changed: 36 additions & 10 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/image.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part)
211211
if (!img)
212212
return -1;
213213
memset(img, 0, sizeof(struct wolfBoot_image));
214+
img->part = part;
214215
if (part == PART_SWAP) {
215216
img->part = PART_SWAP;
216217
img->hdr = (void *)WOLFBOOT_PARTITION_SWAP_ADDRESS;
@@ -237,7 +238,6 @@ int wolfBoot_open_image(struct wolfBoot_image *img, uint8_t part)
237238

238239
if (*size >= WOLFBOOT_PARTITION_SIZE)
239240
return -1;
240-
img->part = part;
241241
img->hdr_ok = 1;
242242
img->fw_size = *size;
243243
img->fw_base = img->hdr + IMAGE_HEADER_SIZE;

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: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,22 +89,21 @@ static int wolfBoot_update(int fallback_allowed)
8989
if ((update.fw_size + IMAGE_HEADER_SIZE) > total_size)
9090
total_size = update.fw_size + IMAGE_HEADER_SIZE;
9191

92-
if (total_size < IMAGE_HEADER_SIZE)
92+
if (total_size <= IMAGE_HEADER_SIZE)
9393
return -1;
9494

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)