Skip to content

Commit d9104ec

Browse files
committed
Fix scatter-gather ELF
1 parent 1109194 commit d9104ec

File tree

5 files changed

+55
-32
lines changed

5 files changed

+55
-32
lines changed

hal/sim.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <sys/stat.h>
3232
#include <fcntl.h>
3333
#include <unistd.h>
34+
#include <errno.h>
3435

3536
#ifdef __APPLE__
3637
#include <mach-o/loader.h>
@@ -308,6 +309,7 @@ void do_boot(const uint32_t *app_offset)
308309
{
309310
int ret;
310311
size_t app_size = WOLFBOOT_PARTITION_SIZE - IMAGE_HEADER_SIZE;
312+
wolfBoot_printf("Simulator do_boot app_offset = %p\n", app_offset);
311313

312314
if (flashLocked == 0) {
313315
wolfBoot_printf("WARNING FLASH IS UNLOCKED AT BOOT");
@@ -352,26 +354,34 @@ void do_boot(const uint32_t *app_offset)
352354

353355
main = (main_entry)((uint8_t*)pSymbolAddress + epc->entryoff);
354356
main(main_argc, main_argv, NULL, NULL);
355-
#elif defined ELF_SCATTERED
356-
uint8_t *entry_point = (sim_ram_base + 0x100000);
357+
358+
#elif defined (ELF_SCATTERED)
359+
uint8_t *entry_point = (sim_ram_base + (unsigned long)app_offset);
357360
printf("entry point: %p\n", entry_point);
358361
printf("app offset: %p\n", app_offset);
359362
typedef int (*main_entry)(int, char**);
360363
main_entry main;
361364
main = (main_entry)(entry_point);
362-
main(main_argc, main_argv);
365+
366+
/* TODO: call main ! */
367+
/* main(main_argc, main_argv); */
368+
wolfBoot_printf("Simulator for ELF_SCATTERED image not implemented yet. Exiting...\n");
369+
exit(0);
363370
#else
364371
char *envp[1] = {NULL};
365372
int fd = memfd_create("test_app", 0);
373+
size_t wret;
366374
if (fd == -1) {
367375
wolfBoot_printf( "memfd error\n");
368376
exit(-1);
369377
}
370378

371-
if ((size_t)write(fd, app_offset, app_size) != app_size) {
372-
wolfBoot_printf( "can't write test-app to memfd\n");
379+
wret = write(fd, app_offset, app_size);
380+
if (wret != app_size) {
381+
wolfBoot_printf( "can't write test-app to memfd, address %p: fd %d rval %d errno %d\n", app_offset, fd, wret, errno);
373382
exit(-1);
374383
}
384+
wolfBoot_printf("Stored test-app to memfd, address %p (%zu bytes)\n", app_offset, wret);
375385

376386
ret = fexecve(fd, main_argv, envp);
377387
wolfBoot_printf( "fexecve error\n");

include/elf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ typedef int (*elf_mmu_map_cb)(uint64_t, uint64_t, uint32_t);
169169
int elf_load_image_mmu(uint8_t *image, uintptr_t *entry, elf_mmu_map_cb mmu_cb);
170170
int elf_load_image(uint8_t *image, uintptr_t *entry, int is_ext);
171171
int elf_store_image_scattered(const unsigned char *image, unsigned long *entry_out, int ext_flash);
172-
int elf_check_image_scattered(uint8_t part);
172+
int elf_check_image_scattered(uint8_t part, unsigned long *entry_out);
173173
int elf_hdr_size(const unsigned char *ehdr);
174174
int elf_open(const unsigned char *ehdr, int *is_elf32);
175175

src/elf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,10 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
212212

213213
if (ph[i].type != ELF_PT_LOAD)
214214
continue;
215-
216215
paddr = (unsigned long)ph[i].paddr;
217216
offset = (unsigned long)ph[i].offset;
218217
filesz = (unsigned long)ph[i].file_size;
218+
printf("Writing section at address %lx offset %lx\n", paddr, offset);
219219
#ifdef EXT_FLASH
220220
if (ext_flash) {
221221
ext_flash_unlock();
@@ -227,12 +227,12 @@ int elf_store_image_scattered(const unsigned char *hdr, unsigned long *entry_out
227227
#endif
228228
{
229229
hal_flash_unlock();
230-
hal_flash_erase(paddr, filesz);
231-
hal_flash_write(paddr, image + offset, filesz);
230+
hal_flash_erase(paddr + ARCH_FLASH_OFFSET, filesz);
231+
hal_flash_write(paddr + ARCH_FLASH_OFFSET, image + offset, filesz);
232232
hal_flash_lock();
233233
}
234234
}
235-
} else { /* 64 bit ELF */
235+
} else { /* 32 bit ELF */
236236
const elf64_header *eh;
237237
const elf64_program_header *ph;
238238
wolfBoot_printf("ELF image is 64 bit\n");
@@ -281,7 +281,7 @@ int elf_load_image(uint8_t *image, uintptr_t *entry, int ext_flash)
281281
#ifdef MMU
282282
return elf_load_image_mmu(image, entry, NULL);
283283
#else
284-
return elf_store_image_scattered(image, entry, ext_flash);
284+
return elf_store_image_scattered(image, (unsigned long *)entry, ext_flash);
285285
#endif
286286
}
287287

src/image.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

src/update_flash.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,13 +1069,13 @@ void RAMFUNCTION wolfBoot_start(void)
10691069
PART_SANITY_CHECK(&boot);
10701070

10711071
#ifdef ELF_SCATTERED
1072-
uintptr_t entry;
1072+
unsigned long entry;
10731073
void *base = (void *)WOLFBOOT_PARTITION_BOOT_ADDRESS;
10741074
wolfBoot_printf("ELF Scattered image digest check\n");
1075-
if (elf_check_image_scattered(PART_BOOT) <0) {
1075+
if (elf_check_image_scattered(PART_BOOT, &entry) < 0) {
10761076
wolfBoot_printf("ELF Scattered image digest check: failed. Restoring scattered image...\n");
10771077
elf_store_image_scattered(base, &entry, PART_IS_EXT(boot));
1078-
if (elf_check_image_scattered(PART_BOOT) < 0) {
1078+
if (elf_check_image_scattered(PART_BOOT, &entry) < 0) {
10791079
wolfBoot_printf("Fatal: Could not verify digest after scattering. Panic().\n");
10801080
wolfBoot_panic();
10811081
}

0 commit comments

Comments
 (0)