@@ -1332,7 +1332,7 @@ int wolfBoot_verify_integrity(struct wolfBoot_image *img)
13321332
13331333#define PADDING_BLOCK_SIZE 64
13341334
1335- int elf_check_image_scattered (uint8_t part )
1335+ int elf_check_image_scattered (uint8_t part , unsigned long * entry_out )
13361336{
13371337 /* Open the partition containing the image */
13381338 struct wolfBoot_image boot ;
@@ -1349,6 +1349,8 @@ int elf_check_image_scattered(uint8_t part)
13491349 int stored_sha_len ;
13501350 int i ;
13511351 uint8_t padding_block [PADDING_BLOCK_SIZE ];
1352+ int entry_out_set = 0 ;
1353+
13521354
13531355 wolfBoot_hash_t ctx ;
13541356 if (wolfBoot_open_image (& boot , part ) < 0 )
@@ -1389,18 +1391,22 @@ int elf_check_image_scattered(uint8_t part)
13891391 }
13901392 wolfBoot_printf ("Hashed ELF header.\n" );
13911393
1392-
1393-
13941394 /* Feed the program headers to the hash function */
13951395 if (is_elf32 ) {
13961396 elf32_header * eh = (elf32_header * )elf_h ;
13971397 elf32_program_header * ph ;
13981398 entry_count = eh -> ph_entry_count ;
13991399 entry_size = eh -> ph_entry_size ;
14001400 entry_off = eh -> ph_offset ;
1401+ if (!entry_out_set ) {
1402+ * entry_out = eh -> entry ;
1403+ entry_out_set = 1 ;
1404+ }
14011405
1406+ wolfBoot_printf ("EH entry offset: %d\n" , entry_off );
1407+ ph = (elf32_program_header * )(elf_h + entry_off );
14021408 /* Add padding until the first program header into hash function */
1403- len = entry_off - elf_hdr_sz ;
1409+ len = ph [ 0 ]. offset - elf_hdr_sz ;
14041410 wolfBoot_printf ("Adding %d bytes padding\n" , len );
14051411 while (len > 0 ) {
14061412 if (len > PADDING_BLOCK_SIZE ) {
@@ -1411,24 +1417,26 @@ int elf_check_image_scattered(uint8_t part)
14111417 break ;
14121418 }
14131419 }
1414-
1415- ph = (elf32_program_header * )(elf_h + entry_off );
1416- for (i = 0 ; i < entry_count ; ++ i ) {
1420+ for (i = 0 ; i < entry_count ; i ++ ) {
14171421 unsigned long paddr ;
14181422 unsigned long filesz ;
14191423 unsigned long offset ;
14201424 paddr = (unsigned long )ph [i ].paddr ;
14211425 offset = (unsigned long )ph [i ].offset ;
14221426 filesz = (unsigned long )ph [i ].file_size ;
1427+ wolfBoot_printf ("Paddr: 0x%lx offset: %lu, size: %lu\n" , paddr ,
1428+ offset , filesz );
14231429
14241430 /* Feed any non-loaded parts to the hash function */
14251431 if (ph [i ].type != ELF_PT_LOAD ) {
14261432 len = filesz ;
1433+ //wolfBoot_printf("Feeding ghost segment, len %d\n", len);
1434+ continue ;
14271435 while (len > 0 ) {
14281436 if (len > WOLFBOOT_SHA_BLOCK_SIZE ) {
14291437 update_hash (& ctx , elf_h + offset , WOLFBOOT_SHA_BLOCK_SIZE );
14301438 len -= WOLFBOOT_SHA_BLOCK_SIZE ;
1431- offset += WOLFBOOT_SHA_BLOCK_SIZE ;
1439+ paddr += WOLFBOOT_SHA_BLOCK_SIZE ;
14321440 } else {
14331441 update_hash (& ctx , elf_h + offset , len );
14341442 break ;
@@ -1437,23 +1445,24 @@ int elf_check_image_scattered(uint8_t part)
14371445 } else {
14381446 /* Feed the loaded parts to the hash function */
14391447 len = filesz ;
1448+ wolfBoot_printf ("Feeding stored segment, len %d\n" , len );
14401449 while (len > 0 ) {
14411450 if (len > WOLFBOOT_SHA_BLOCK_SIZE ) {
14421451 update_hash (& ctx , (void * )(paddr + ARCH_FLASH_OFFSET ),
14431452 WOLFBOOT_SHA_BLOCK_SIZE );
14441453 len -= WOLFBOOT_SHA_BLOCK_SIZE ;
1445- offset += WOLFBOOT_SHA_BLOCK_SIZE ;
1454+ paddr += WOLFBOOT_SHA_BLOCK_SIZE ;
14461455 } else {
1447- update_hash (& ctx , (void * )(paddr + ARCH_FLASH_OFFSET ),
1448- len );
1456+ update_hash (& ctx , (void * )(paddr + ARCH_FLASH_OFFSET ),
1457+ len );
14491458 break ;
14501459 }
14511460 }
14521461 }
14531462 /* Add padding until next program header, if any. */
1454- if ((i < entry_count - 1 ) && (ph [i + 1 ].offset > offset )) {
1463+ if ((i < entry_count - 1 ) && (ph [i + 1 ].offset > ( offset + filesz ) )) {
14551464 unsigned long padding = ph [i + 1 ].offset - (offset + filesz );
1456- wolfBoot_printf ("Adding padding: %lu\n" , padding );
1465+ wolfBoot_printf ("Adding padding: %lu (from %p to %p) \n" , padding , offset + filesz , ph [ i + 1 ]. offset );
14571466 while (padding > 0 ) {
14581467 if (padding > PADDING_BLOCK_SIZE ) {
14591468 update_hash (& ctx , padding_block , PADDING_BLOCK_SIZE );
@@ -1467,12 +1476,16 @@ int elf_check_image_scattered(uint8_t part)
14671476 final_offset = offset + filesz ;
14681477 }
14691478 }
1470- }else { /* 64-bit ELF */
1479+ } else { /* 64-bit ELF */
14711480 elf64_header * eh = (elf64_header * )elf_h ;
14721481 elf64_program_header * ph ;
14731482 entry_count = eh -> ph_entry_count ;
14741483 entry_size = eh -> ph_entry_size ;
14751484 entry_off = eh -> ph_offset ;
1485+ if (!entry_out_set ) {
1486+ * entry_out = eh -> entry ;
1487+ entry_out_set = 1 ;
1488+ }
14761489
14771490 wolfBoot_printf ("EH entry offset: %d\n" , entry_off );
14781491 ph = (elf64_program_header * )(elf_h + entry_off );
@@ -1507,7 +1520,7 @@ int elf_check_image_scattered(uint8_t part)
15071520 if (len > WOLFBOOT_SHA_BLOCK_SIZE ) {
15081521 update_hash (& ctx , elf_h + offset , WOLFBOOT_SHA_BLOCK_SIZE );
15091522 len -= WOLFBOOT_SHA_BLOCK_SIZE ;
1510- offset += WOLFBOOT_SHA_BLOCK_SIZE ;
1523+ paddr += WOLFBOOT_SHA_BLOCK_SIZE ;
15111524 } else {
15121525 update_hash (& ctx , elf_h + offset , len );
15131526 break ;
@@ -1522,7 +1535,7 @@ int elf_check_image_scattered(uint8_t part)
15221535 update_hash (& ctx , (void * )(paddr + ARCH_FLASH_OFFSET ),
15231536 WOLFBOOT_SHA_BLOCK_SIZE );
15241537 len -= WOLFBOOT_SHA_BLOCK_SIZE ;
1525- offset += WOLFBOOT_SHA_BLOCK_SIZE ;
1538+ paddr += WOLFBOOT_SHA_BLOCK_SIZE ;
15261539 } else {
15271540 update_hash (& ctx , (void * )(paddr + ARCH_FLASH_OFFSET ),
15281541 len );
@@ -1531,7 +1544,7 @@ int elf_check_image_scattered(uint8_t part)
15311544 }
15321545 }
15331546 /* Add padding until next program header, if any. */
1534- if ((i < entry_count - 1 ) && (ph [i + 1 ].offset > offset )) {
1547+ if ((i < entry_count - 1 ) && (ph [i + 1 ].offset > ( offset + filesz ) )) {
15351548 unsigned long padding = ph [i + 1 ].offset - (offset + filesz );
15361549 wolfBoot_printf ("Adding padding: %lu\n" , padding );
15371550 while (padding > 0 ) {
@@ -1550,7 +1563,7 @@ int elf_check_image_scattered(uint8_t part)
15501563 }
15511564 if (final_offset < 0 )
15521565 return -1 ;
1553- if (final_offset + IMAGE_HEADER_SIZE > boot .fw_size )
1566+ if (final_offset + IMAGE_HEADER_SIZE > ( long ) boot .fw_size )
15541567 return -1 ;
15551568
15561569 len = boot .fw_size - final_offset ;
0 commit comments