96 lines
2.9 KiB
C
96 lines
2.9 KiB
C
//---
|
|
// fxBoot:loader:entry - ELF Loader entry
|
|
//---
|
|
#include "fxBoot/hypervisor.h"
|
|
#include "fxBoot/fs/smemfs.h"
|
|
#include "fxBoot/elf.h"
|
|
|
|
#include <gint/std/stdlib.h>
|
|
#include <gint/std/string.h>
|
|
|
|
#include "./src/hypervisor/internal/elf.h"
|
|
|
|
/* error string list */
|
|
const struct hel_error_db image_error_db[] = {
|
|
{.id = hel_image_success, .strerror = "valid"},
|
|
{.id = hel_image_size_error, .strerror = "size error"},
|
|
{.id = hel_image_type_error, .strerror = "type error"},
|
|
{.id = hel_image_mem_error, .strerror = "out of memory error"},
|
|
{.id = 0xdeb0cad0, .strerror = NULL},
|
|
};
|
|
|
|
/* hypervisor_elf_loader_image_error(): Display image error information */
|
|
int hypervisor_elf_loader_image_error(int errnum)
|
|
{
|
|
return (hypervisor_elf_loader_error(image_error_db,
|
|
"ELF image ", errnum));
|
|
}
|
|
|
|
/* loader_load_image() - Load the program into Virtual Memory */
|
|
int hypervisor_elf_loader_image_load(struct hworld *world,
|
|
struct smemfs_inode *inode, Elf32_Ehdr *header)
|
|
{
|
|
Elf32_Phdr program;
|
|
uintptr_t paddress;
|
|
uintptr_t vmin;
|
|
uintptr_t vmax;
|
|
off_t offset;
|
|
uint16_t i;
|
|
|
|
|
|
/* Walk one time to get the entier program size and check ELF
|
|
validity. */
|
|
i = -1;
|
|
vmin = 0xffffffff;
|
|
vmax = 0x00000000;
|
|
world->memory.program.size = 0;
|
|
while (++i < header->e_phnum) {
|
|
offset = header->e_phoff + (i * sizeof(Elf32_Phdr));
|
|
if (smemfs_pread(inode, &program, sizeof(Elf32_Phdr), offset)
|
|
!= sizeof(Elf32_Phdr)) {
|
|
return (hel_image_size_error);
|
|
}
|
|
if (program.p_vaddr < vmin)
|
|
vmin = program.p_vaddr;
|
|
if (program.p_vaddr + program.p_memsz > vmax)
|
|
vmax = program.p_vaddr + program.p_memsz;
|
|
}
|
|
world->memory.program.size = vmax - vmin;
|
|
world->memory.program.elf.vmin = vmin;
|
|
world->memory.program.elf.vmax = vmax;
|
|
|
|
/* Allocate programe memory */
|
|
world->memory.program.start = calloc(world->memory.program.size, 1);
|
|
if (world->memory.program.start == NULL)
|
|
return (hel_image_mem_error);
|
|
|
|
/* Now, load all program section into physical memory. To do this, we
|
|
read each program header, generate the "real" physical address of
|
|
the segment then dump data.
|
|
|
|
Note that the p_filesz can be smaller than p_memsz so, we need to
|
|
wipe the segment area before the dump. */
|
|
i = -1;
|
|
while (++i < header->e_phnum) {
|
|
offset = header->e_phoff + (i * sizeof(Elf32_Phdr));
|
|
smemfs_pread(inode, &program, sizeof(Elf32_Phdr), offset);
|
|
|
|
paddress = (uintptr_t)program.p_vaddr - vmin;
|
|
paddress += (uintptr_t)world->memory.program.start;
|
|
|
|
memset((void*)paddress, 0x00, program.p_memsz);
|
|
smemfs_pread(inode, (void*)paddress,
|
|
program.p_filesz, program.p_offset);
|
|
}
|
|
|
|
/* Generate program entry address */
|
|
world->context.cpu.spc = (uintptr_t)header->e_entry - vmin;
|
|
world->context.cpu.spc += (uintptr_t)world->memory.program.start;
|
|
terminal_write("- SPC: %p\n", world->context.cpu.spc);
|
|
|
|
/* Generate other information */
|
|
world->context.cpu.ssr = 0x40000000;
|
|
//TODO: PR register !
|
|
return (hel_image_success);
|
|
}
|