diff options
author | dzwdz | 2022-07-23 17:57:56 +0200 |
---|---|---|
committer | dzwdz | 2022-07-23 17:57:56 +0200 |
commit | cd12cbc75564fafd9c2519cdf1085e651c9d7cfd (patch) | |
tree | acc02960280b4e360a932e48c228a46b78070574 | |
parent | 6cd59ad0124d8a22e8cbb77f87e8f0aa20633c59 (diff) |
create a bootstrap ELF loader, that'll load init
-rw-r--r-- | Makefile | 8 | ||||
l--------- | src/user_bootstrap/elfload.c | 1 | ||||
l--------- | src/user_bootstrap/elfload.h | 1 | ||||
-rw-r--r-- | src/user_bootstrap/linker.ld | 31 | ||||
-rw-r--r-- | src/user_bootstrap/main.c | 59 | ||||
l--------- | src/user_bootstrap/syscall.c | 1 | ||||
l--------- | src/user_bootstrap/syscall.s | 1 | ||||
-rw-r--r-- | src/usertestelf.c | 1 |
8 files changed, 99 insertions, 4 deletions
@@ -14,8 +14,8 @@ QFLAGS += -display none endif define from_sources - $(patsubst src/%.s,out/obj/%.s.o,$(shell find $(1) -type f -name '*.s')) \ - $(patsubst src/%.c,out/obj/%.c.o,$(shell find $(1) -type f -name '*.c')) + $(patsubst src/%.s,out/obj/%.s.o,$(shell find $(1) -type f,l -name '*.s')) \ + $(patsubst src/%.c,out/obj/%.c.o,$(shell find $(1) -type f,l -name '*.c')) endef @@ -60,7 +60,7 @@ out/fs/boot/kernel.bin: src/kernel/linker.ld $(call from_sources, src/kernel/) $ @$(CC) $(LFLAGS) -T $^ -o $@ grub-file --is-x86-multiboot $@ -out/raw_init: src/user/linker.ld $(call from_sources, src/user/) $(call from_sources, src/shared/) +out/bootstrap: src/user_bootstrap/linker.ld $(call from_sources, src/user_bootstrap/) $(call from_sources, src/shared/) @mkdir -p $(@D) @$(CC) $(LFLAGS) -T $^ -o $@ @@ -75,7 +75,7 @@ out/test.elf: src/usertestelf.ld out/obj/usertestelf.c.o out/obj/user/lib/syscal out/initrd.tar: $(shell find initrd/) out/test.elf cd initrd; tar chf ../$@ * -out/fs/boot/init: out/raw_init out/initrd.tar +out/fs/boot/init: out/bootstrap out/initrd.tar @mkdir -p $(@D) @cat $^ > $@ diff --git a/src/user_bootstrap/elfload.c b/src/user_bootstrap/elfload.c new file mode 120000 index 0000000..75a5cb2 --- /dev/null +++ b/src/user_bootstrap/elfload.c @@ -0,0 +1 @@ +../user/lib/elfload.c
\ No newline at end of file diff --git a/src/user_bootstrap/elfload.h b/src/user_bootstrap/elfload.h new file mode 120000 index 0000000..5b09fdc --- /dev/null +++ b/src/user_bootstrap/elfload.h @@ -0,0 +1 @@ +../user/lib/elfload.h
\ No newline at end of file diff --git a/src/user_bootstrap/linker.ld b/src/user_bootstrap/linker.ld new file mode 100644 index 0000000..f2c9d24 --- /dev/null +++ b/src/user_bootstrap/linker.ld @@ -0,0 +1,31 @@ +ENTRY(main) +OUTPUT_FORMAT("binary") + +SECTIONS +{ + . = 2M; + .text BLOCK(4K) : ALIGN(4K) + { + *(.text.startup) + *(.text) + } + .rodata BLOCK(4K) : ALIGN(4K) + { + *(.rodata) + } + .data BLOCK(4K) : ALIGN(4K) + { + *(.data) + } + + _initrd = .; /* is just appended onto the end of the binary */ + . += 2M; + + _bss_start = .; + .bss BLOCK(4K) : ALIGN(4K) + { + *(COMMON) + *(.bss) + } + _bss_end = .; +} diff --git a/src/user_bootstrap/main.c b/src/user_bootstrap/main.c new file mode 100644 index 0000000..baa9803 --- /dev/null +++ b/src/user_bootstrap/main.c @@ -0,0 +1,59 @@ +#include <shared/flags.h> +#include <shared/mem.h> +#include <shared/syscalls.h> +#include <user_bootstrap/elfload.h> + +extern char _bss_start; +extern char _bss_end; +extern char _initrd; + +/* libc stubs */ +int printf(const char *fmt, ...) {(void)fmt; return 0;} +void *malloc(size_t size) {(void)size; _syscall_exit(1);} +void free(void *ptr) {} +int file_read(libc_file *f, char *buf, size_t len) {(void)f; (void)buf; (void)len; _syscall_exit(1);} + + +static void *tar_find(void *tar, const char *path); +static int oct_parse(char *str, size_t len); + +static void *tar_find(void *tar, const char *path) { + size_t path_len = strlen(path); + int size; + if (path_len > 100) return NULL; + + for (size_t off = 0;;) { + if (0 != memcmp(tar + off + 257, "ustar", 5)) + return NULL; // not a metadata sector, end of archive + + size = oct_parse(tar + off + 124, 11); + if (0 == memcmp(tar + off, path, path_len) && + *(char*)(tar + off + path_len) == '\0') + { + return tar + off + 512; + } + off += 512; + off += (size + 511) & ~511; + } +} + +static int oct_parse(char *str, size_t len) { + int res = 0; + for (size_t i = 0; i < len; i++) { + res *= 8; + res += str[i] - '0'; // no format checking + } + return res; +} + + +__attribute__((section(".text.startup"))) +int main(void) { + _syscall_memflag(&_bss_start, &_bss_end - &_bss_start, MEMFLAG_PRESENT); + + void *init = tar_find(&_initrd, "test.elf"); + elf_exec(init); + _syscall_debug_klog("bootstrap failed", sizeof("bootstrap failed")); + + _syscall_exit(0); +} diff --git a/src/user_bootstrap/syscall.c b/src/user_bootstrap/syscall.c new file mode 120000 index 0000000..c6d5b0f --- /dev/null +++ b/src/user_bootstrap/syscall.c @@ -0,0 +1 @@ +../user/lib/syscall.c
\ No newline at end of file diff --git a/src/user_bootstrap/syscall.s b/src/user_bootstrap/syscall.s new file mode 120000 index 0000000..ac2e0f9 --- /dev/null +++ b/src/user_bootstrap/syscall.s @@ -0,0 +1 @@ +../user/lib/syscall.s
\ No newline at end of file diff --git a/src/usertestelf.c b/src/usertestelf.c index 218f684..1cbfcc5 100644 --- a/src/usertestelf.c +++ b/src/usertestelf.c @@ -13,6 +13,7 @@ extern char _image_base[]; static void printf_backend(void *arg, const char *buf, size_t len) { (void)arg; _syscall_write(1, buf, len, -1); + _syscall_debug_klog(buf, len); } int printf(const char *fmt, ...) { |