22
22
#include <stdint.h>
23
23
#include <string.h>
24
24
#include <delta.h>
25
- #include <target.h> /* WOLFBOOT_SECTOR_SIZE */
26
25
27
26
28
27
#define ESC 0x7f
29
28
29
+
30
30
#if (defined(__IAR_SYSTEMS_ICC__ ) && (__IAR_SYSTEMS_ICC__ > 8 )) || \
31
31
defined(__GNUC__ )
32
32
#define BLOCK_HDR_PACKED __attribute__ ((packed))
@@ -46,7 +46,7 @@ struct BLOCK_HDR_PACKED block_hdr {
46
46
#include "encrypt.h"
47
47
#define ext_flash_check_write ext_flash_encrypt_write
48
48
#define ext_flash_check_read ext_flash_decrypt_read
49
- #else
49
+ #elif defined( __WOLFBOOT )
50
50
#include "hal.h"
51
51
#define ext_flash_check_write ext_flash_write
52
52
#define ext_flash_check_read ext_flash_read
@@ -169,6 +169,36 @@ int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len)
169
169
return dst_off ;
170
170
}
171
171
172
+ #ifndef __WOLFBOOT
173
+
174
+ #include <stdio.h>
175
+ #include <stdlib.h>
176
+ #include <errno.h>
177
+
178
+ static uint32_t wolfboot_sector_size = 0 ;
179
+
180
+ int wb_diff_get_sector_size (void )
181
+ {
182
+ uint32_t sec_sz = 0 ;
183
+ char * env_sector_size = NULL ;
184
+ env_sector_size = getenv ("WOLFBOOT_SECTOR_SIZE" );
185
+ if (!env_sector_size ) {
186
+ fprintf (stderr , "Please set the WOLFBOOT_SECTOR_SIZE environment variable in\n"
187
+ "order to sign a delta update.\n" );
188
+ exit (6 );
189
+ } else {
190
+ sec_sz = atoi (env_sector_size );
191
+ if (sec_sz == 0 ) {
192
+ errno = 0 ;
193
+ sec_sz = strtol (env_sector_size , NULL , 16 );
194
+ if (errno != 0 ) {
195
+ fprintf (stderr , "Invalid WOLFBOOT_SECTOR_SIZE value\n" );
196
+ exit (6 );
197
+ }
198
+ }
199
+ }
200
+ return sec_sz ;
201
+ }
172
202
173
203
int wb_diff_init (WB_DIFF_CTX * ctx , uint8_t * src_a , uint32_t len_a , uint8_t * src_b , uint32_t len_b )
174
204
{
@@ -179,6 +209,8 @@ int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_
179
209
ctx -> src_b = src_b ;
180
210
ctx -> size_a = len_a ;
181
211
ctx -> size_b = len_b ;
212
+ wolfboot_sector_size = wb_diff_get_sector_size ();
213
+ printf ("WOLFBOOT_SECTOR_SIZE: %u\n" , wolfboot_sector_size );
182
214
return 0 ;
183
215
}
184
216
@@ -196,7 +228,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
196
228
return -1 ;
197
229
198
230
while ((ctx -> off_b + BLOCK_HDR_SIZE < ctx -> size_b ) && (len > p_off + BLOCK_HDR_SIZE )) {
199
- uintptr_t page_start = ctx -> off_b / WOLFBOOT_SECTOR_SIZE ;
231
+ uintptr_t page_start = ctx -> off_b / wolfboot_sector_size ;
200
232
uintptr_t pa_start ;
201
233
found = 0 ;
202
234
if (p_off + BLOCK_HDR_SIZE > len )
@@ -210,14 +242,14 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
210
242
* base for the sectors that have already been updated.
211
243
*/
212
244
213
- pa_start = WOLFBOOT_SECTOR_SIZE * page_start ;
245
+ pa_start = wolfboot_sector_size * page_start ;
214
246
pa = ctx -> src_a + pa_start ;
215
247
while (((uintptr_t )(pa - ctx -> src_a ) < (uintptr_t )ctx -> size_a ) && (p_off < len )) {
216
248
if ((uintptr_t )(ctx -> size_a - (pa - ctx -> src_a )) < BLOCK_HDR_SIZE )
217
249
break ;
218
250
if ((ctx -> size_b - ctx -> off_b ) < BLOCK_HDR_SIZE )
219
251
break ;
220
- if ((WOLFBOOT_SECTOR_SIZE - (ctx -> off_b % WOLFBOOT_SECTOR_SIZE )) < BLOCK_HDR_SIZE )
252
+ if ((wolfboot_sector_size - (ctx -> off_b % wolfboot_sector_size )) < BLOCK_HDR_SIZE )
221
253
break ;
222
254
if ((memcmp (pa , (ctx -> src_b + ctx -> off_b ), BLOCK_HDR_SIZE ) == 0 )) {
223
255
uintptr_t b_start ;
@@ -238,7 +270,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
238
270
/* Stop matching if the source image size limit is hit. */
239
271
break ;
240
272
}
241
- if ((b_start / WOLFBOOT_SECTOR_SIZE ) < ((ctx -> off_b + 1 ) / WOLFBOOT_SECTOR_SIZE )) {
273
+ if ((b_start / wolfboot_sector_size ) < ((ctx -> off_b + 1 ) / wolfboot_sector_size )) {
242
274
/* Stop matching when the sector bound is hit. */
243
275
break ;
244
276
}
@@ -262,7 +294,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
262
294
}
263
295
if (!found ) {
264
296
/* Try matching an earlier section in the resulting image */
265
- uintptr_t pb_end = page_start * WOLFBOOT_SECTOR_SIZE ;
297
+ uintptr_t pb_end = page_start * wolfboot_sector_size ;
266
298
pb = ctx -> src_b ;
267
299
while (((uintptr_t )(pb - ctx -> src_b ) < pb_end ) && (p_off < len )) {
268
300
/* Check image boundary */
@@ -274,7 +306,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
274
306
/* Don't try matching backwards if the distance between the two
275
307
* blocks is smaller than one sector.
276
308
*/
277
- if (WOLFBOOT_SECTOR_SIZE > (page_start * WOLFBOOT_SECTOR_SIZE )
309
+ if (wolfboot_sector_size > (page_start * wolfboot_sector_size )
278
310
- (pb - ctx -> src_b ))
279
311
break ;
280
312
@@ -338,5 +370,6 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
338
370
}
339
371
return (int )p_off ;
340
372
}
373
+ #endif /* __WOLFBOOT */
341
374
342
375
#endif /* DELTA_UPDATES */
0 commit comments