diff options
author | Marvin Borner | 2020-02-22 17:22:17 +0100 |
---|---|---|
committer | Marvin Borner | 2020-02-22 17:22:17 +0100 |
commit | add6efeb22ffb7695d5c9addcef073fc653f700e (patch) | |
tree | 147c99f2f3e037c877a34468f1494c412ec53416 /src/kernel/fs | |
parent | 86ef6a779a42cf5701632ccb82714a006bae4ee9 (diff) |
Well basically nothing really works I guess
I've worked quite a while on several small things which I didn't commit but I'm going away for a week (holiday) soooo I synced these useless and dumb files :)
Diffstat (limited to 'src/kernel/fs')
-rw-r--r-- | src/kernel/fs/elf.h | 53 | ||||
-rw-r--r-- | src/kernel/fs/elfloader.c | 64 | ||||
-rw-r--r-- | src/kernel/fs/load.c | 44 | ||||
-rw-r--r-- | src/kernel/fs/load.h | 16 |
4 files changed, 177 insertions, 0 deletions
diff --git a/src/kernel/fs/elf.h b/src/kernel/fs/elf.h new file mode 100644 index 0000000..9be6b4d --- /dev/null +++ b/src/kernel/fs/elf.h @@ -0,0 +1,53 @@ +#ifndef MELVIX_ELF_H +#define MELVIX_ELF_H + +#include <stdint.h> + +typedef struct { + uint32_t sig; +} elf_priv_data; + +typedef struct { + unsigned char e_ident[16]; + uint16_t e_type; + uint16_t e_machine; + uint32_t e_version; + uint32_t e_entry; + uint32_t e_phoff; + uint32_t e_shoff; + uint32_t e_flags; + uint16_t e_ehsize; + uint16_t e_phentsize; + uint16_t e_phnum; + uint16_t e_shentsize; + uint16_t e_shnum; + uint16_t e_shstrndx; +} elf_header_t; + +typedef struct { + uint32_t sh_name; + uint32_t sh_type; + uint32_t sh_flags; + uint32_t sh_addr; + uint32_t sh_offset; + uint32_t sh_size; + uint32_t sh_link; + uint32_t sh_info; + uint32_t sh_addralign; + uint32_t sh_entsize; +} elf_section_header_t; + +typedef struct { + uint32_t p_type; + uint32_t p_offset; + uint32_t p_vaddr; + uint32_t p_paddr; + uint32_t p_filesz; + uint32_t p_memsz; + uint32_t p_flags; + uint32_t p_align; +} elf_program_header_t; + +void elf_init(); + +#endif diff --git a/src/kernel/fs/elfloader.c b/src/kernel/fs/elfloader.c new file mode 100644 index 0000000..5b81a5e --- /dev/null +++ b/src/kernel/fs/elfloader.c @@ -0,0 +1,64 @@ +#include <kernel/fs/elf.h> +#include <kernel/lib/stdio.h> +#include <kernel/memory/alloc.h> +#include <kernel/lib/lib.h> +#include <kernel/memory/paging.h> +#include "load.h" + +elf_priv_data *elf_probe(uint8_t *buf) +{ + elf_header_t *header = (elf_header_t *) buf; + /* The first four bytes are 0x7f and 'ELF' */ + if (header->e_ident[0] == 0x7f && + header->e_ident[1] == 'E' && header->e_ident[2] == 'L' && header->e_ident[3] == 'F') { + /* Valid ELF! */ + serial_printf("Buffer is valid ELF file!"); + return (void *) 1; + } + return 0; +} + +uint8_t elf_start(uint8_t *buf) +{ + elf_header_t *header = (elf_header_t *) buf; + serial_printf("Type: %s%s%s", + header->e_ident[4] == 1 ? "32bit " : "64 bit", + header->e_ident[5] == 1 ? "Little Endian " : "Big endian ", + header->e_ident[6] == 1 ? "True ELF " : "buggy ELF "); + if (header->e_type != 2) { + serial_printf("File is not executable!"); + return 0; + } + /* Map the virtual space */ + uint32_t phys_loc = loader_get_unused_load_location(); + uint32_t cr3 = kmalloc(4096); + /* Find first program header and loop through them */ + elf_program_header_t *ph = (elf_program_header_t *) (buf + header->e_phoff); + for (int i = 0; i < header->e_phnum; i++, ph++) { + switch (ph->p_type) { + case 0: /* NULL */ + break; + case 1: /* LOAD */ + serial_printf("LOAD: offset 0x%x vaddr 0x%x paddr 0x%x filesz 0x%x memsz 0x%x", + ph->p_offset, ph->p_vaddr, ph->p_paddr, ph->p_filesz, ph->p_memsz); + paging_map(phys_loc, ph->p_vaddr, PT_PRESENT | PT_RW | PT_USED); + memcpy(ph->p_vaddr, buf + ph->p_offset, ph->p_filesz); + break; + default: /* @TODO add more */ + serial_printf("Unsupported p_type! Bail out!"); + return 0; + } + } + + return 0;//START("elf32", header->e_entry); +} + +void elf_init() +{ + loader_t *elfloader = (loader_t *) kmalloc(sizeof(loader_t)); + elfloader->name = "ELF32"; + elfloader->probe = (void *) elf_probe; + elfloader->start = (void *) elf_start; + register_loader(elfloader); + // _kill(); +}
\ No newline at end of file diff --git a/src/kernel/fs/load.c b/src/kernel/fs/load.c index cd422d0..5a476b5 100644 --- a/src/kernel/fs/load.c +++ b/src/kernel/fs/load.c @@ -5,6 +5,8 @@ #include <kernel/system.h> #include <kernel/fs/iso9660/iso9660.h> #include <kernel/memory/alloc.h> +#include <kernel/lib/stdio.h> +#include <kernel/lib/lib.h> void load_binaries() { @@ -31,4 +33,46 @@ void load_binaries() kfree(user_e); } vga_log("Successfully loaded binaries"); +} + +#define MAX_LOADERS 16 + +loader_t **loaders = 0; +uint8_t last_loader = 0; + +uint32_t last_load_loc = 0x400000; + +void loader_init() +{ + serial_printf("Setting up loader"); + loaders = (loader_t **) kmalloc(MAX_LOADERS * sizeof(uint32_t)); + memset(loaders, 0, MAX_LOADERS * sizeof(uint32_t)); + // _kill(); +} + +void register_loader(loader_t *load) +{ + if (last_loader + 1 > MAX_LOADERS) return; + if (!load) return; + loaders[last_loader] = load; + last_loader++; + serial_printf("Registered %s loader as id %d", load->name, last_loader - 1); +} + +uint32_t loader_get_unused_load_location() +{ + last_load_loc += 0x400000; + return last_load_loc; +} + +uint8_t exec_start(uint8_t *buf) +{ + for (int i = 0; i < MAX_LOADERS; i++) { + if (!loaders[i]) break; + void *priv = loaders[i]->probe(buf); + if (priv) { + return loaders[i]->start(buf, priv); + } + } + return 0; }
\ No newline at end of file diff --git a/src/kernel/fs/load.h b/src/kernel/fs/load.h index 7a0ec06..4ba545f 100644 --- a/src/kernel/fs/load.h +++ b/src/kernel/fs/load.h @@ -16,4 +16,20 @@ struct font { void load_binaries(); +typedef struct { + char *name; + + void *(*probe)(uint8_t *buf); + + uint8_t (*start)(uint8_t *buf, void *priv); +} loader_t; + +void loader_init(); + +void register_loader(loader_t *load); + +uint8_t exec_start(uint8_t *buf); + +uint32_t loader_get_unused_load_location(); + #endif |