2222#include <stdint.h>
2323#include <string.h>
2424#include <delta.h>
25- #include <target.h> /* WOLFBOOT_SECTOR_SIZE */
2625
2726
2827#define ESC 0x7f
2928
29+
3030#if (defined(__IAR_SYSTEMS_ICC__ ) && (__IAR_SYSTEMS_ICC__ > 8 )) || \
3131 defined(__GNUC__ )
3232#define BLOCK_HDR_PACKED __attribute__ ((packed))
@@ -46,7 +46,7 @@ struct BLOCK_HDR_PACKED block_hdr {
4646#include "encrypt.h"
4747#define ext_flash_check_write ext_flash_encrypt_write
4848#define ext_flash_check_read ext_flash_decrypt_read
49- #else
49+ #elif defined( __WOLFBOOT )
5050#include "hal.h"
5151#define ext_flash_check_write ext_flash_write
5252#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)
169169 return dst_off ;
170170}
171171
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+ }
172202
173203int wb_diff_init (WB_DIFF_CTX * ctx , uint8_t * src_a , uint32_t len_a , uint8_t * src_b , uint32_t len_b )
174204{
@@ -179,6 +209,8 @@ int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, uint8_t *src_
179209 ctx -> src_b = src_b ;
180210 ctx -> size_a = len_a ;
181211 ctx -> size_b = len_b ;
212+ wolfboot_sector_size = wb_diff_get_sector_size ();
213+ printf ("WOLFBOOT_SECTOR_SIZE: %u\n" , wolfboot_sector_size );
182214 return 0 ;
183215}
184216
@@ -196,7 +228,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
196228 return -1 ;
197229
198230 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 ;
200232 uintptr_t pa_start ;
201233 found = 0 ;
202234 if (p_off + BLOCK_HDR_SIZE > len )
@@ -210,14 +242,14 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
210242 * base for the sectors that have already been updated.
211243 */
212244
213- pa_start = WOLFBOOT_SECTOR_SIZE * page_start ;
245+ pa_start = wolfboot_sector_size * page_start ;
214246 pa = ctx -> src_a + pa_start ;
215247 while (((uintptr_t )(pa - ctx -> src_a ) < (uintptr_t )ctx -> size_a ) && (p_off < len )) {
216248 if ((uintptr_t )(ctx -> size_a - (pa - ctx -> src_a )) < BLOCK_HDR_SIZE )
217249 break ;
218250 if ((ctx -> size_b - ctx -> off_b ) < BLOCK_HDR_SIZE )
219251 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 )
221253 break ;
222254 if ((memcmp (pa , (ctx -> src_b + ctx -> off_b ), BLOCK_HDR_SIZE ) == 0 )) {
223255 uintptr_t b_start ;
@@ -238,7 +270,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
238270 /* Stop matching if the source image size limit is hit. */
239271 break ;
240272 }
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 )) {
242274 /* Stop matching when the sector bound is hit. */
243275 break ;
244276 }
@@ -262,7 +294,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
262294 }
263295 if (!found ) {
264296 /* 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 ;
266298 pb = ctx -> src_b ;
267299 while (((uintptr_t )(pb - ctx -> src_b ) < pb_end ) && (p_off < len )) {
268300 /* Check image boundary */
@@ -274,7 +306,7 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
274306 /* Don't try matching backwards if the distance between the two
275307 * blocks is smaller than one sector.
276308 */
277- if (WOLFBOOT_SECTOR_SIZE > (page_start * WOLFBOOT_SECTOR_SIZE )
309+ if (wolfboot_sector_size > (page_start * wolfboot_sector_size )
278310 - (pb - ctx -> src_b ))
279311 break ;
280312
@@ -338,5 +370,6 @@ int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len)
338370 }
339371 return (int )p_off ;
340372}
373+ #endif /* __WOLFBOOT */
341374
342375#endif /* DELTA_UPDATES */
0 commit comments