-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathdelta.c
More file actions
430 lines (398 loc) · 14.5 KB
/
delta.c
File metadata and controls
430 lines (398 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* delta.c
*
* Copyright (C) 2025 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef DELTA_UPDATES
#include <stdint.h>
#include <string.h>
#include <delta.h>
#define ESC 0x7f
#if (defined(__IAR_SYSTEMS_ICC__) && (__IAR_SYSTEMS_ICC__ > 8)) || \
defined(__GNUC__)
#define BLOCK_HDR_PACKED __attribute__ ((packed))
#else
#define BLOCK_HDR_PACKED
#endif
struct BLOCK_HDR_PACKED block_hdr {
uint8_t esc;
uint8_t off[3];
uint8_t sz[2];
};
#define BLOCK_HDR_SIZE (sizeof (struct block_hdr))
#define BLOCK_OFF_MAX 0xFFFFFFU
#if defined(EXT_ENCRYPTED) && defined(__WOLFBOOT)
#include "image.h"
#define ext_flash_check_write ext_flash_encrypt_write
#define ext_flash_check_read ext_flash_decrypt_read
#elif defined(__WOLFBOOT)
#include "hal.h"
#define ext_flash_check_write ext_flash_write
#define ext_flash_check_read ext_flash_read
#endif
int wb_patch_init(WB_PATCH_CTX *bm, uint8_t *src, uint32_t ssz, uint8_t *patch,
uint32_t psz)
{
if (!bm || ssz == 0 || psz == 0) {
return -1;
}
memset(bm, 0, sizeof(WB_PATCH_CTX));
bm->src_base = src;
bm->src_size = ssz;
bm->patch_base = patch;
bm->patch_size = psz;
#ifdef EXT_FLASH
bm->patch_cache_start = 0xFFFFFFFF;
#endif
return 0;
}
#ifdef EXT_FLASH
#define PATCH_CACHE_SIZE 256
#define DELTA_SWAP_CACHE_SIZE 1024
static inline uint8_t *patch_read_cache(WB_PATCH_CTX *ctx)
{
if (ctx->patch_cache_start != 0xFFFFFFFF) {
if (ctx->patch_cache_start == ctx->p_off)
return ctx->patch_cache;
if (ctx->p_off < ctx->patch_cache_start +
(DELTA_PATCH_BLOCK_SIZE - BLOCK_HDR_SIZE))
return ctx->patch_cache + ctx->p_off - ctx->patch_cache_start;
}
ctx->patch_cache_start = ctx->p_off;
ext_flash_check_read(
(uintptr_t)(ctx->patch_base + ctx->p_off),
ctx->patch_cache, DELTA_PATCH_BLOCK_SIZE);
return ctx->patch_cache;
}
#else
static inline uint8_t *patch_read_cache(WB_PATCH_CTX *ctx)
{
return ctx->patch_base + ctx->p_off;
}
#endif
int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
{
struct block_hdr *hdr;
uint32_t dst_off = 0;
uint32_t src_off;
uint16_t sz;
uint32_t copy_sz;
uint32_t resume_sz;
if (!ctx)
return -1;
if (len < BLOCK_HDR_SIZE)
return -1;
while ( ( (ctx->matching != 0) || (ctx->p_off < ctx->patch_size)) && (dst_off < len)) {
uint8_t *pp = patch_read_cache(ctx);
if (ctx->matching) {
/* Resume matching block from previous sector */
resume_sz = ctx->blk_sz;
if (resume_sz > len)
resume_sz = len;
if (ctx->blk_off > ctx->src_size ||
resume_sz > ctx->src_size - ctx->blk_off)
return -1;
memcpy(dst + dst_off, ctx->src_base + ctx->blk_off, resume_sz);
if (ctx->blk_sz > len) {
ctx->blk_sz -= len;
ctx->blk_off += len;
} else {
ctx->blk_off = 0;
ctx->blk_sz = 0;
ctx->matching = 0;
}
dst_off += resume_sz;
continue;
}
if (*pp == ESC) {
if ((ctx->patch_size - ctx->p_off) < 2)
return -1;
if (*(pp + 1) == ESC) {
*(dst + dst_off) = ESC;
/* Two bytes of the patch have been consumed to produce ESC */
ctx->p_off += 2;
dst_off++;
continue;
} else {
if ((ctx->patch_size - ctx->p_off) < BLOCK_HDR_SIZE)
return -1;
hdr = (struct block_hdr *)pp;
src_off = (hdr->off[0] << 16) + (hdr->off[1] << 8) +
hdr->off[2];
sz = (hdr->sz[0] << 8) + hdr->sz[1];
if (src_off > ctx->src_size ||
sz > ctx->src_size - src_off)
return -1;
ctx->matching = 1;
if (sz > (len - dst_off)) {
copy_sz = len - dst_off;
ctx->blk_off = src_off + copy_sz;
ctx->blk_sz = sz - copy_sz;
} else {
copy_sz = sz;
}
memcpy(dst + dst_off, ctx->src_base + src_off, copy_sz);
if (sz == copy_sz) {
/* End of the block, reset counters and matching state */
ctx->matching = 0;
ctx->blk_off = 0;
}
ctx->p_off += BLOCK_HDR_SIZE;
dst_off += copy_sz;
}
} else {
*(dst + dst_off) = *pp;
dst_off++;
ctx->p_off++;
}
}
return dst_off;
}
#ifndef __WOLFBOOT
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h> /* INT_MAX */
#include <inttypes.h> /* PRIu32 */
static uint32_t wolfboot_sector_size = 0;
int wb_diff_get_sector_size(void)
{
uint32_t sec_sz = 0;
char *env_sector_size = NULL;
#if defined(_MSC_VER) /* MSVC only, not _WIN32 that includes MSYS2/MinGW */
char* dup = NULL;
size_t len = 0;
if ((_dupenv_s(&dup, &len, "WOLFBOOT_SECTOR_SIZE") == 0) && dup) {
env_sector_size = dup;
}
#else
env_sector_size = getenv("WOLFBOOT_SECTOR_SIZE");
#endif
if (!env_sector_size) {
fprintf(stderr, "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
"order to sign a delta update.\n");
#if defined(_MSC_VER)
free(dup);
#endif
exit(6);
} else {
sec_sz = atoi(env_sector_size);
if (sec_sz == 0) {
errno = 0;
sec_sz = strtol(env_sector_size, NULL, 16);
if (errno != 0) {
fprintf(stderr, "Invalid WOLFBOOT_SECTOR_SIZE value\n");
#if defined(_MSC_VER)
free(dup);
#endif
exit(6);
}
}
}
#if defined(_MSC_VER)
free(dup);
#endif
if (sec_sz == 0) {
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE cannot be 0\n");
exit(6);
}
if (sec_sz > (uint32_t)INT_MAX) {
fprintf(stderr, "WOLFBOOT_SECTOR_SIZE (%" PRIu32 ") exceeds INT_MAX (%d)\n",
sec_sz, INT_MAX);
exit(6);
}
return (int)sec_sz;
}
int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_b, uint32_t len_b)
{
if (!ctx || (len_a == 0) || (len_b == 0))
return -1;
memset(ctx, 0, sizeof(WB_DIFF_CTX));
ctx->src_a = src_a;
ctx->src_b = src_b;
ctx->size_a = len_a;
ctx->size_b = len_b;
wolfboot_sector_size = wb_diff_get_sector_size();
printf("WOLFBOOT_SECTOR_SIZE: %u\n", wolfboot_sector_size);
return 0;
}
int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
{
struct block_hdr hdr;
int found;
uint8_t *pa, *pb;
uint16_t match_len;
uintptr_t blk_start;
uintptr_t p_off = 0;
if (ctx->off_b >= ctx->size_b)
return 0;
if (len < BLOCK_HDR_SIZE)
return -1;
while ((ctx->off_b + BLOCK_HDR_SIZE < ctx->size_b) && (len > p_off + BLOCK_HDR_SIZE)) {
uintptr_t page_start = ctx->off_b / wolfboot_sector_size;
uintptr_t pa_start;
found = 0;
if (p_off + BLOCK_HDR_SIZE > len)
return (int)p_off;
/* 'A' Patch base is valid for addresses in blocks ahead.
* For matching previous blocks, 'B' is used as base instead.
*
* This mechanism ensures that the patch can refer to lower position
* in a FLASH memory that is being patched, using the destination as
* base for the sectors that have already been updated.
*/
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)
break;
if ((ctx->size_b - ctx->off_b) < BLOCK_HDR_SIZE)
break;
if ((wolfboot_sector_size - (ctx->off_b % wolfboot_sector_size)) < BLOCK_HDR_SIZE)
break;
if ((memcmp(pa, (ctx->src_b + ctx->off_b), BLOCK_HDR_SIZE) == 0)) {
uintptr_t b_start;
uint8_t *pa_limit = ctx->src_a + ctx->size_a;
/* Identical areas of BLOCK_HDR_SIZE bytes match between the images.
* initialize match_len; blk_start is the relative offset within
* the src image.
*/
match_len = BLOCK_HDR_SIZE;
blk_start = pa - ctx->src_a;
if (blk_start > BLOCK_OFF_MAX)
return -1;
b_start = ctx->off_b;
pa+= BLOCK_HDR_SIZE;
ctx->off_b += BLOCK_HDR_SIZE;
while ((pa < pa_limit) &&
(ctx->off_b < ctx->size_b) &&
(*pa == *(ctx->src_b + ctx->off_b))) {
/* Extend matching block if possible, as long as the
* identical sequence continues.
*/
if ((pa + 1) >= pa_limit) {
/* Stop matching if the source image size limit is hit. */
break;
}
if ((b_start / wolfboot_sector_size) < ((ctx->off_b + 1) / wolfboot_sector_size)) {
/* Stop matching when the sector bound is hit. */
break;
}
/* Increase match len, test next byte */
pa++;
ctx->off_b++;
match_len++;
}
hdr.esc = ESC;
hdr.off[0] = ((blk_start >> 16) & 0x000000FF);
hdr.off[1] = ((blk_start >> 8) & 0x000000FF);
hdr.off[2] = ((blk_start) & 0x000000FF);
hdr.sz[0] = ((match_len >> 8) & 0x00FF);
hdr.sz[1] = ((match_len) & 0x00FF);
memcpy(patch + p_off, &hdr, sizeof(hdr));
p_off += BLOCK_HDR_SIZE;
found = 1;
break;
} else pa++;
}
if (!found) {
/* Try matching an earlier section in the resulting image */
uintptr_t pb_end = page_start * wolfboot_sector_size;
uint8_t *pb_limit = ctx->src_b + pb_end;
pb = ctx->src_b;
while (((uintptr_t)(pb - ctx->src_b) < pb_end) && (p_off < len)) {
/* Check image boundary */
if ((ctx->size_b - ctx->off_b) < BLOCK_HDR_SIZE)
break;
if ((uintptr_t)(ctx->size_b - (pb - ctx->src_b)) < BLOCK_HDR_SIZE)
break;
/* Don't try matching backwards if the distance between the two
* blocks is smaller than one sector.
*/
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)) {
/* A match was found between the current pointer and a
* previously patched area in the resulting image.
* Initialize match_len and set the blk_start to the beginning
* of the matching area in the image.
*/
match_len = BLOCK_HDR_SIZE;
blk_start = pb - ctx->src_b;
if (blk_start > BLOCK_OFF_MAX)
return -1;
pb+= BLOCK_HDR_SIZE;
ctx->off_b += BLOCK_HDR_SIZE;
while ((pb < pb_limit) &&
(ctx->off_b < ctx->size_b) &&
(*pb == *(ctx->src_b + ctx->off_b))) {
/* Extend match as long as the areas have the
* same content. Block skipping in this case is
* not a problem since the distance between the patched
* area and the area to patch is always larger than one
* block size.
*/
pb++;
if (pb >= pb_limit) {
pb--;
break;
}
match_len++;
ctx->off_b++;
}
hdr.esc = ESC;
hdr.off[0] = ((blk_start >> 16) & 0x000000FF);
hdr.off[1] = ((blk_start >> 8) & 0x000000FF);
hdr.off[2] = ((blk_start) & 0x000000FF);
hdr.sz[0] = ((match_len >> 8) & 0x00FF);
hdr.sz[1] = ((match_len) & 0x00FF);
memcpy(patch + p_off, &hdr, sizeof(hdr));
p_off += BLOCK_HDR_SIZE;
found = 1;
break;
} else pb++;
}
}
if (!found) {
if (*(ctx->src_b + ctx->off_b) == ESC) {
if ((p_off + 1) >= (len - BLOCK_HDR_SIZE))
break;
*(patch + p_off++) = ESC;
*(patch + p_off++) = ESC;
} else {
*(patch + p_off++) = *(ctx->src_b + ctx->off_b);
}
ctx->off_b++;
}
}
while ((p_off < len - BLOCK_HDR_SIZE) && ctx->off_b < ctx->size_b) {
if (*(ctx->src_b + ctx->off_b) == ESC) {
if ((p_off + 1) >= (len - BLOCK_HDR_SIZE))
break;
*(patch + p_off++) = ESC;
*(patch + p_off++) = ESC;
} else {
*(patch + p_off++) = *(ctx->src_b + ctx->off_b);
}
ctx->off_b++;
}
return (int)p_off;
}
#endif /* __WOLFBOOT */
#endif /* DELTA_UPDATES */