aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-07-29 15:55:13 +0200
committerMarvin Borner2020-07-29 15:55:13 +0200
commitf092bf03baf3b2c13c9dc462bdc697caddccd347 (patch)
tree90844499041a648692704a3e5b0040634963fb33
parentbe0246208d1fe7727f94221ea7562f9a9cce1659 (diff)
Added elf loading and test app
-rw-r--r--Makefile7
-rwxr-xr-xrun1
-rw-r--r--src/features/elf.c39
-rw-r--r--src/features/gui.c2
-rw-r--r--src/inc/elf.h61
-rw-r--r--src/main.c3
-rw-r--r--src/test.c5
7 files changed, 116 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 4b97fcc..20e236f 100644
--- a/Makefile
+++ b/Makefile
@@ -12,11 +12,13 @@ COBJS = src/main.o \
src/features/fs.o \
src/features/psf.o \
src/features/gui.o \
+ src/features/elf.o \
src/lib/str.o \
src/lib/mem.o \
src/lib/math.o \
src/lib/conv.o \
src/lib/print.o
+CTOBJS = src/test.o
CC = cross/opt/bin/i686-elf-gcc
LD = cross/opt/bin/i686-elf-ld
AS = nasm
@@ -39,11 +41,14 @@ all: compile clean
kernel: $(COBJS)
-compile: kernel
+apps: $(CTOBJS)
+
+compile: kernel apps
@mkdir -p build/
@$(AS) -f bin src/entry.asm -o build/boot.bin
@$(LD) -N -emain -Ttext 0x00050000 -o build/kernel.bin $(COBJS) --oformat binary
@$(CC) $(CFLAGS) -emain -o build/debug.o $(COBJS)
+ @cp $(CTOBJS) build/
clean:
@find src/ -name "*.o" -type f -delete
diff --git a/run b/run
index 3774f4d..3ceed87 100755
--- a/run
+++ b/run
@@ -96,6 +96,7 @@ make_build() {
mkdir -p mnt/
sudo mount build/disk.img mnt/
sudo cp -r disk/* mnt/
+ sudo cp build/test.o mnt/
cat disk/**/* >/dev/null
sudo umount mnt/
rm -rf mnt/
diff --git a/src/features/elf.c b/src/features/elf.c
new file mode 100644
index 0000000..339add0
--- /dev/null
+++ b/src/features/elf.c
@@ -0,0 +1,39 @@
+#include <assert.h>
+#include <def.h>
+#include <elf.h>
+#include <fs.h>
+#include <mem.h>
+
+int elf_verify(struct elf_header *header)
+{
+ if (header->ident[0] == ELF_MAG && header->ident[1] == 'E' && header->ident[2] == 'L' &&
+ header->ident[3] == 'F' && header->ident[4] == ELF_32 &&
+ header->ident[5] == ELF_LITTLE && header->ident[6] == ELF_CURRENT &&
+ header->machine == ELF_386 && (header->type == ET_REL || header->type == ET_EXEC)) {
+ return 1;
+ }
+ return 0;
+}
+
+void elf_load(char *path)
+{
+ u32 *data = read_file(path);
+
+ struct elf_header *h = (struct elf_header *)data;
+ assert(elf_verify(h));
+
+ struct elf_program_header *p = (struct elf_program_header *)((u32)data + h->phoff);
+ struct elf_program_header *p_end =
+ (struct elf_program_header *)((u32)p + (h->phentsize * h->phnum));
+
+ u32 offset = (p->vaddr - p->paddr);
+ while (p < p_end) {
+ memcpy((void *)p->paddr, (void *)((u32)data + p->offset), p->filesz);
+ p++;
+ }
+
+ void (*entry)();
+ entry = (void (*)())(h->entry - offset);
+
+ entry();
+}
diff --git a/src/features/gui.c b/src/features/gui.c
index fa0641c..d080cb1 100644
--- a/src/features/gui.c
+++ b/src/features/gui.c
@@ -37,7 +37,7 @@ void gui_write(int x, int y, const u32 c[3], char *text)
}
// Abstraction
-int x, y = 1;
+int x, y = 0;
const u32 c[3] = { 0xff, 0xff, 0xff };
void gui_term_write_char(char ch)
{
diff --git a/src/inc/elf.h b/src/inc/elf.h
new file mode 100644
index 0000000..c10ba4e
--- /dev/null
+++ b/src/inc/elf.h
@@ -0,0 +1,61 @@
+// MIT License, Copyright (c) 2020 Marvin Borner
+
+#ifndef ELF_H
+#define ELF_H
+
+#include <def.h>
+
+#define ELF_MAG 0x7F // 0
+#define ELF_32 1 // 4: 32-bit Architecture
+#define ELF_LITTLE 1 // 5: Little Endian
+#define ELF_CURRENT 1 // 6: ELF Current Version
+#define ELF_386 3 // header->machine x86 machine type
+
+#define ET_NONE 0 // Unkown type
+#define ET_REL 1 // Relocatable file
+#define ET_EXEC 2 // Executable file
+
+struct elf_header {
+ u8 ident[16];
+ u16 type;
+ u16 machine;
+ u32 version;
+ u32 entry;
+ u32 phoff;
+ u32 shoff;
+ u32 flags;
+ u16 ehsize;
+ u16 phentsize;
+ u16 phnum;
+ u16 shentsize;
+ u16 shnum;
+ u16 shstrndx;
+};
+
+struct elf_section_header {
+ u32 name;
+ u32 type;
+ u32 flags;
+ u32 addr;
+ u32 offset;
+ u32 size;
+ u32 link;
+ u32 info;
+ u32 addralign;
+ u32 entsize;
+};
+
+struct elf_program_header {
+ u32 type;
+ u32 offset;
+ u32 vaddr;
+ u32 paddr;
+ u32 filesz;
+ u32 memsz;
+ u32 flags;
+ u32 align;
+};
+
+void elf_load(char *path);
+
+#endif
diff --git a/src/main.c b/src/main.c
index 92ec61c..665a11f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,6 +3,7 @@
#include "config.h"
#include <boot.h>
#include <def.h>
+#include <elf.h>
#include <fs.h>
#include <gui.h>
#include <interrupts.h>
@@ -41,6 +42,8 @@ void main(struct mem_info *mem_info, struct vid_info *vid_info)
gui_init(FONT_PATH);
gui_term_write("Wake up, " USERNAME "...\n");
+ elf_load("/test.o");
+ printf("loaded");
while (1) {
};
diff --git a/src/test.c b/src/test.c
new file mode 100644
index 0000000..c0f214b
--- /dev/null
+++ b/src/test.c
@@ -0,0 +1,5 @@
+void main()
+{
+ while (1) {
+ }
+}