From f4e0cc61b356e8511786cac22c0e263af385d52f Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Sat, 4 Nov 2023 19:15:25 +0100 Subject: [PATCH] Delta update fixes - img_size: use 32-bit variable - remove '+1' from pa_start calculation - fix broken delta.c wb_diff check for distance between matching patterns (root cause for the delta+encrypt bug) --- include/delta.h | 2 +- src/delta.c | 5 +++-- src/update_flash.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/delta.h b/include/delta.h index 19f101ad6..b5fe145a3 100644 --- a/include/delta.h +++ b/include/delta.h @@ -67,7 +67,7 @@ int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len); int wb_patch_init(WB_PATCH_CTX *bm, uint8_t *src, uint32_t ssz, uint8_t *patch, uint32_t psz); int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len); -int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset, uint16_t **img_size); +int wolfBoot_get_delta_info(uint8_t part, int inverse, uint32_t **img_offset, uint32_t **img_size); #endif diff --git a/src/delta.c b/src/delta.c index f5a6aeca9..657846ff2 100644 --- a/src/delta.c +++ b/src/delta.c @@ -209,7 +209,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len) * base for the sectors that have already been updated. */ - pa_start = (WOLFBOOT_SECTOR_SIZE + 1) * page_start; + pa_start = WOLFBOOT_SECTOR_SIZE * page_start; pa = ctx->src_a + pa_start; while (((uintptr_t)(pa - ctx->src_a) < (uintptr_t)ctx->size_a) && (p_off < len)) { if ((uintptr_t)(ctx->size_a - (pa - ctx->src_a)) < BLOCK_HDR_SIZE) @@ -273,7 +273,8 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len) /* Don't try matching backwards if the distance between the two * blocks is smaller than one sector. */ - if (WOLFBOOT_SECTOR_SIZE > (pb - ctx->src_b) - (page_start * WOLFBOOT_SECTOR_SIZE)) + if (WOLFBOOT_SECTOR_SIZE > (page_start * WOLFBOOT_SECTOR_SIZE) + - (pb - ctx->src_b)) break; if ((memcmp(pb, (ctx->src_b + ctx->off_b), BLOCK_HDR_SIZE) == 0)) { diff --git a/src/update_flash.c b/src/update_flash.c index 5d045ad2a..85a224ed6 100644 --- a/src/update_flash.c +++ b/src/update_flash.c @@ -212,7 +212,7 @@ static int wolfBoot_delta_update(struct wolfBoot_image *boot, uint32_t offset = 0; uint16_t ptr_len; uint32_t *img_offset; - uint16_t *img_size; + uint32_t *img_size; uint32_t total_size; WB_PATCH_CTX ctx; #ifdef EXT_ENCRYPTED