@@ -82,10 +82,9 @@ static int wolfBoot_verify_signature(uint8_t *hash, uint8_t *sig)
8282}
8383#endif
8484
85-
86- static uint8_t get_header (struct wolfBoot_image * img , uint8_t type , uint8_t * * ptr )
85+ static uint8_t find_header (uint8_t * haystack , uint8_t type , uint8_t * * ptr )
8786{
88- uint8_t * p = img -> hdr + IMAGE_HEADER_OFFSET ;
87+ uint8_t * p = haystack ;
8988 while (* p != 0 ) {
9089 if (* p == HDR_PADDING ) {
9190 p ++ ;
@@ -103,33 +102,35 @@ static uint8_t get_header(struct wolfBoot_image *img, uint8_t type, uint8_t **pt
103102 return 0 ;
104103}
105104
106- int wolfBoot_open_image (struct wolfBoot_image * img , uint8_t part )
105+
106+
107+ static uint8_t get_header_ext (struct wolfBoot_image * img , uint8_t type , uint8_t * * ptr );
108+
109+ static uint8_t get_header (struct wolfBoot_image * img , uint8_t type , uint8_t * * ptr )
107110{
108- uint32_t * magic ;
109- uint32_t * size ;
110- if (!img )
111- return -1 ;
112- memset (img , 0 , sizeof (struct wolfBoot_image ));
111+ #if defined(PART_UPDATE_EXT ) && defined(EXT_FLASH )
112+ if (img -> part == PART_UPDATE )
113+ return get_header_ext (img , type , ptr );
114+ #endif
115+ return find_header (img -> hdr + IMAGE_HEADER_OFFSET , type , ptr );
116+ }
113117
114- if (part == PART_BOOT )
115- img -> hdr = (void * )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
116- else if (part == PART_UPDATE )
117- img -> hdr = (void * )WOLFBOOT_PARTITION_UPDATE_ADDRESS ;
118- else
119- return -1 ;
120- magic = (uint32_t * )(img -> hdr );
121- if (* magic != WOLFBOOT_MAGIC )
122- return -1 ;
123- size = (uint32_t * )(img -> hdr + sizeof (uint32_t ));
124-
125- if (* size >= WOLFBOOT_PARTITION_SIZE )
126- return -1 ;
127- img -> part = part ;
128- img -> hdr_ok = 1 ;
129- img -> fw_size = * size ;
130- img -> fw_base = img -> hdr + IMAGE_HEADER_SIZE ;
131- img -> trailer = img -> hdr + WOLFBOOT_PARTITION_SIZE ;
132- return 0 ;
118+
119+
120+
121+ static uint8_t ext_hash_block [SHA256_BLOCK_SIZE ];
122+
123+ static uint8_t * get_sha_block (struct wolfBoot_image * img , uint32_t offset )
124+ {
125+ if (offset > img -> fw_size )
126+ return NULL ;
127+ #ifdef PART_UPDATE_EXT
128+ if (img -> part == PART_UPDATE ) {
129+ ext_flash_read (img -> fw_base + offset , ext_hash_block , SHA256_BLOCK_SIZE );
130+ return ext_hash_block ;
131+ }
132+ #endif
133+ return (uint8_t * )(img -> fw_base + offset );
133134}
134135
135136static int image_hash (struct wolfBoot_image * img , uint8_t * hash )
@@ -139,10 +140,10 @@ static int image_hash(struct wolfBoot_image *img, uint8_t *hash)
139140 uint8_t * p = img -> hdr ;
140141 uint8_t * fw_end = img -> fw_base + img -> fw_size ;
141142 int blksz ;
143+ uint32_t position = 0 ;
142144 wc_Sha256 sha256_ctx ;
143145 if (!img || !img -> hdr )
144146 return -1 ;
145-
146147 stored_sha_len = get_header (img , HDR_SHA256 , & stored_sha );
147148 if (stored_sha_len != SHA256_DIGEST_SIZE )
148149 return -1 ;
@@ -155,14 +156,17 @@ static int image_hash(struct wolfBoot_image *img, uint8_t *hash)
155156 wc_Sha256Update (& sha256_ctx , p , blksz );
156157 p += blksz ;
157158 }
158- p = img -> fw_base ;
159- while (p < fw_end ) {
159+ do {
160+ p = get_sha_block (img , position );
161+ if (p == NULL )
162+ break ;
160163 blksz = SHA256_BLOCK_SIZE ;
161- if (fw_end - p < blksz )
162- blksz = fw_end - p ;
164+ if (position + blksz > img -> fw_size )
165+ blksz = img -> fw_size - position ;
163166 wc_Sha256Update (& sha256_ctx , p , blksz );
164- p += blksz ;
165- }
167+ position += blksz ;
168+ } while (position < img -> fw_size );
169+
166170 wc_Sha256Final (& sha256_ctx , hash );
167171 return 0 ;
168172}
@@ -185,6 +189,74 @@ static void key_hash(uint8_t *hash)
185189}
186190
187191static uint8_t digest [SHA256_DIGEST_SIZE ];
192+ static uint8_t verification [IMAGE_SIGNATURE_SIZE ];
193+ #ifdef EXT_FLASH
194+
195+ static uint8_t hdr_cpy [IMAGE_HEADER_SIZE ];
196+ static int hdr_cpy_done = 0 ;
197+
198+ static uint8_t * fetch_hdr_cpy (struct wolfBoot_image * img )
199+ {
200+ if (!hdr_cpy_done ) {
201+ ext_flash_read (img -> hdr , hdr_cpy , IMAGE_HEADER_SIZE );
202+ hdr_cpy_done = 1 ;
203+ }
204+ return hdr_cpy ;
205+ }
206+
207+ static uint8_t get_header_ext (struct wolfBoot_image * img , uint8_t type , uint8_t * * ptr )
208+ {
209+ return find_header (fetch_hdr_cpy (img ) + IMAGE_HEADER_OFFSET , type , ptr );
210+ }
211+
212+ #endif
213+
214+
215+ static uint8_t * get_img_hdr (struct wolfBoot_image * img )
216+ {
217+ #ifdef PART_UPDATE_EXT
218+ if (img -> part == PART_UPDATE ) {
219+ return fetch_hdr_cpy (img );
220+ }
221+ #endif
222+ return (uint8_t * )(img -> hdr );
223+ }
224+
225+ int wolfBoot_open_image (struct wolfBoot_image * img , uint8_t part )
226+ {
227+ uint32_t * magic ;
228+ uint32_t * size ;
229+ uint8_t * image ;
230+ if (!img )
231+ return -1 ;
232+ memset (img , 0 , sizeof (struct wolfBoot_image ));
233+ if (part == PART_BOOT ) {
234+ img -> hdr = (void * )WOLFBOOT_PARTITION_BOOT_ADDRESS ;
235+ image = (uint8_t * )img -> hdr ;
236+ } else if (part == PART_UPDATE ) {
237+ img -> hdr = (void * )WOLFBOOT_PARTITION_UPDATE_ADDRESS ;
238+ #if defined(PART_UPDATE_EXT )
239+ image = fetch_hdr_cpy (img );
240+ #else
241+ image = (uint8_t * )img -> hdr ;
242+ #endif
243+ } else
244+ return -1 ;
245+ magic = (uint32_t * )(image );
246+ if (* magic != WOLFBOOT_MAGIC )
247+ return -1 ;
248+ size = (uint32_t * )(image + sizeof (uint32_t ));
249+
250+ if (* size >= WOLFBOOT_PARTITION_SIZE )
251+ return -1 ;
252+ img -> part = part ;
253+ img -> hdr_ok = 1 ;
254+ img -> fw_size = * size ;
255+ img -> fw_base = img -> hdr + IMAGE_HEADER_SIZE ;
256+ img -> trailer = img -> hdr + WOLFBOOT_PARTITION_SIZE ;
257+ return 0 ;
258+ }
259+
188260int wolfBoot_verify_integrity (struct wolfBoot_image * img )
189261{
190262 uint8_t * stored_sha ;
@@ -200,7 +272,6 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img)
200272 return 0 ;
201273}
202274
203- static uint8_t verification [IMAGE_SIGNATURE_SIZE ];
204275int wolfBoot_verify_authenticity (struct wolfBoot_image * img )
205276{
206277 uint8_t * stored_signature ;
@@ -217,7 +288,6 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
217288 if (memcmp (digest , pubkey_hint , SHA256_DIGEST_SIZE ) != 0 )
218289 return -1 ;
219290 }
220-
221291 if (image_hash (img , digest ) != 0 )
222292 return -1 ;
223293 if (wolfBoot_verify_signature (digest , stored_signature ) != 0 )
@@ -227,20 +297,3 @@ int wolfBoot_verify_authenticity(struct wolfBoot_image *img)
227297}
228298
229299
230- int wolfBoot_copy (uint32_t src , uint32_t dst , uint32_t size )
231- {
232- uint32_t * orig , * copy ;
233- uint32_t pos = 0 ;
234- if (src == dst )
235- return 0 ;
236- if ((src & 0x03 ) || (dst & 0x03 ))
237- return -1 ;
238- while (pos < size ) {
239- orig = (uint32_t * )(src + pos );
240- copy = (uint32_t * )(dst + pos );
241- while (* orig != * copy )
242- hal_flash_write (dst + pos , (void * )orig , sizeof (uint32_t ));
243- pos += sizeof (uint32_t );
244- }
245- return pos ;
246- }
0 commit comments