From 0fd34f23c744a5944e22f816f04158c1d851d4be Mon Sep 17 00:00:00 2001 From: Marco Oliverio Date: Thu, 13 Jul 2023 07:31:57 +0000 Subject: [PATCH] ELF: add mmu callback to map segments before loading --- include/elf.h | 2 ++ src/elf.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/elf.h b/include/elf.h index 7549e0a6..4d7e8d54 100644 --- a/include/elf.h +++ b/include/elf.h @@ -136,6 +136,8 @@ typedef struct elf64_program_header { } elf64_program_header; +typedef int (*elf_mmu_map_cb)(uint64_t, uint64_t, uint32_t); +int elf_load_image_mmu(uint8_t *image, uintptr_t *entry, elf_mmu_map_cb mmu_cb); int elf_load_image(uint8_t *image, uintptr_t *entry); diff --git a/src/elf.c b/src/elf.c index 425b1852..713aa21f 100644 --- a/src/elf.c +++ b/src/elf.c @@ -67,7 +67,7 @@ /* Loader for elf32 or elf64 format program headers * Returns the entry point function */ -int elf_load_image(uint8_t *image, uintptr_t *entry) +int elf_load_image_mmu(uint8_t *image, uintptr_t *entry, elf_mmu_map_cb mmu_cb) { elf32_header* h32 = (elf32_header*)image; elf64_header* h64 = (elf64_header*)image; @@ -134,6 +134,17 @@ int elf_load_image(uint8_t *image, uintptr_t *entry) #endif #ifndef ELF_PARSER + if (mmu_cb != NULL) { + if (mmu_cb(vaddr, paddr, mem_size) != 0) { +#ifdef DEBUG_ELF + wolfBoot_printf( + "\tFail to map %u bytes to %p (p %p)\n", + (uint32_t)mem_size, (void*)vaddr, (void*)paddr); +#endif + continue; + } + } + memcpy((void*)(uintptr_t)vaddr, image + offset, file_size); if (mem_size > file_size) { memset((void*)(uintptr_t)(vaddr + file_size), 0, @@ -153,4 +164,10 @@ int elf_load_image(uint8_t *image, uintptr_t *entry) return 0; } +int elf_load_image(uint8_t *image, uintptr_t *entry) +{ + + return elf_load_image_mmu(image, entry, NULL); +} + #endif /* WOLFBOOT_ELF */