aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-07-30 16:15:39 +0200
committerMarvin Borner2020-07-30 16:15:54 +0200
commit46fb7dcf479ac85361d8eaae5af3ea27a6b93a2d (patch)
treeb71cc5056914c6709cef18022e8da9b243323998
parentf092bf03baf3b2c13c9dc462bdc697caddccd347 (diff)
Something something with elfs and witches
-rw-r--r--Makefile24
-rw-r--r--apps/link.ld24
-rw-r--r--apps/test.c88
-rwxr-xr-xrun2
-rw-r--r--src/config.h4
-rw-r--r--src/features/elf.c18
-rw-r--r--src/main.c2
-rw-r--r--src/test.c5
8 files changed, 137 insertions, 30 deletions
diff --git a/Makefile b/Makefile
index 20e236f..061aafa 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
# MIT License, Copyright (c) 2020 Marvin Borner
-COBJS = src/main.o \
+COBJS_KERNEL = src/main.o \
src/drivers/vesa.o \
src/drivers/cpu.o \
src/drivers/serial.o \
@@ -18,10 +18,10 @@ COBJS = src/main.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
+COBJS_APPS = apps/test.o
+CC = ccache cross/opt/bin/i686-elf-gcc
+LD = ccache cross/opt/bin/i686-elf-ld
+AS = ccache nasm
# Flags to make the binary smaller TODO: Remove after indirect pointer support!
CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os
@@ -29,7 +29,7 @@ CSFLAGS = -mpreferred-stack-boundary=2 -fno-asynchronous-unwind-tables -Os
# TODO: Use lib as external library
CFLAGS = $(CSFLAGS) -Wall -Wextra -nostdlib -nostdinc -ffreestanding -fno-builtin -fno-pic -mgeneral-regs-only -std=c99 -m32 -pedantic-errors -Isrc/lib/inc/ -Isrc/inc/
-ASFLAGS = -f elf32
+ASFLAGS = -f elf32 -O3
all: compile clean
@@ -39,16 +39,16 @@ all: compile clean
%_asm.o: %.asm
@$(AS) $(ASFLAGS) $< -o $@
-kernel: $(COBJS)
+kernel: $(COBJS_KERNEL)
-apps: $(CTOBJS)
+apps: $(COBJS_APPS)
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/
+ @$(LD) -N -emain -Ttext 0x00050000 -o build/kernel.bin $(COBJS_KERNEL) --oformat binary
+ @$(CC) $(CFLAGS) -o build/debug.o $(COBJS_KERNEL)
+ @$(LD) -N -emain -Tapps/link.ld -o build/test apps/test.o
clean:
- @find src/ -name "*.o" -type f -delete
+ @find src/ apps/ -name "*.o" -type f -delete
diff --git a/apps/link.ld b/apps/link.ld
new file mode 100644
index 0000000..ee41578
--- /dev/null
+++ b/apps/link.ld
@@ -0,0 +1,24 @@
+ENTRY(main)
+SECTIONS
+{
+ .text 0x40000000:
+ {
+ code = .; _code = .; __code = .;
+ *(.text)
+ }
+
+ .data ALIGN(0x400000):
+ {
+ data = .; _data = .; __data = .;
+ *(.data)
+ *(.rodata)
+ }
+
+ .bss ALIGN(0x400000):
+ {
+ bss = .; _bss = .; __bss = .;
+ *(.bss)
+ }
+
+ end = .; _end = .; __end = .;
+}
diff --git a/apps/test.c b/apps/test.c
new file mode 100644
index 0000000..f9e40cb
--- /dev/null
+++ b/apps/test.c
@@ -0,0 +1,88 @@
+#include <def.h>
+
+u32 strlen(const char *s)
+{
+ const char *ss = s;
+ while (*ss)
+ ss++;
+ return ss - s;
+}
+
+u8 inb(u16 port)
+{
+ u8 value;
+ __asm__ volatile("inb %1, %0" : "=a"(value) : "Nd"(port));
+ return value;
+}
+
+u16 inw(u16 port)
+{
+ u16 value;
+ __asm__ volatile("inw %1, %0" : "=a"(value) : "Nd"(port));
+ return value;
+}
+
+u32 inl(u16 port)
+{
+ u32 value;
+ __asm__ volatile("inl %1, %0" : "=a"(value) : "Nd"(port));
+ return value;
+}
+
+void insl(u16 port, void *addr, int n)
+{
+ __asm__ volatile("cld; rep insl"
+ : "=D"(addr), "=c"(n)
+ : "d"(port), "0"(addr), "1"(n)
+ : "memory", "cc");
+}
+
+void outb(u16 port, u8 data)
+{
+ __asm__ volatile("outb %0, %1" ::"a"(data), "Nd"(port));
+}
+
+void outw(u16 port, u16 data)
+{
+ __asm__ volatile("outw %0, %1" ::"a"(data), "Nd"(port));
+}
+
+void outl(u16 port, u32 data)
+{
+ __asm__ volatile("outl %0, %1" ::"a"(data), "Nd"(port));
+}
+
+void serial_install()
+{
+ outb(0x3f8 + 1, 0x00);
+ outb(0x3f8 + 3, 0x80);
+ outb(0x3f8 + 0, 0x03);
+ outb(0x3f8 + 1, 0x00);
+ outb(0x3f8 + 3, 0x03);
+ outb(0x3f8 + 2, 0xC7);
+ outb(0x3f8 + 4, 0x0B);
+}
+
+int is_transmit_empty()
+{
+ return inb(0x3f8 + 5) & 0x20;
+}
+
+void serial_put(char ch)
+{
+ while (is_transmit_empty() == 0)
+ ;
+ outb(0x3f8, (u8)ch);
+}
+
+void serial_print(const char *data)
+{
+ for (u32 i = 0; i < strlen(data); i++)
+ serial_put(data[i]);
+}
+
+void main()
+{
+ serial_install();
+ serial_print("WELCOME TO USERSPACE");
+}
diff --git a/run b/run
index 3ceed87..2cf0cc5 100755
--- a/run
+++ b/run
@@ -96,7 +96,7 @@ make_build() {
mkdir -p mnt/
sudo mount build/disk.img mnt/
sudo cp -r disk/* mnt/
- sudo cp build/test.o mnt/
+ sudo cp build/test mnt/
cat disk/**/* >/dev/null
sudo umount mnt/
rm -rf mnt/
diff --git a/src/config.h b/src/config.h
index 4d8f5ce..29c0e23 100644
--- a/src/config.h
+++ b/src/config.h
@@ -7,8 +7,8 @@
int MELVIX_VERSION = 0;
#define USERNAME "Neo"
-#define FONT_PATH "/font/ter-p32n.psf"
-//#define FONT_PATH "/font/spleen-16x32.psfu"
+//#define FONT_PATH "/font/ter-p32n.psf"
+#define FONT_PATH "/font/spleen-16x32.psfu"
#define NETWORK "rtl8139"
#endif
diff --git a/src/features/elf.c b/src/features/elf.c
index 339add0..f92f399 100644
--- a/src/features/elf.c
+++ b/src/features/elf.c
@@ -3,16 +3,13 @@
#include <elf.h>
#include <fs.h>
#include <mem.h>
+#include <str.h>
-int elf_verify(struct elf_header *header)
+int elf_verify(struct elf_header *h)
{
- 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;
+ return (h->ident[0] == ELF_MAG && !strncmp((char *)&h->ident[1], "ELF", 3) &&
+ h->ident[4] == ELF_32 && h->ident[5] == ELF_LITTLE && h->ident[6] == ELF_CURRENT &&
+ h->machine == ELF_386 && (h->type == ET_REL || h->type == ET_EXEC));
}
void elf_load(char *path)
@@ -28,7 +25,10 @@ void elf_load(char *path)
u32 offset = (p->vaddr - p->paddr);
while (p < p_end) {
- memcpy((void *)p->paddr, (void *)((u32)data + p->offset), p->filesz);
+ printf("\nheader: 0x%x\n", p->paddr);
+ printf("filesz: %d\n", p->filesz);
+ /* memcpy(p->paddr, (u32)data + p->offset, p->filesz); */
+ memcpy((u32 *)p->paddr, (u32 *)((u32)data + p->offset), p->filesz);
p++;
}
diff --git a/src/main.c b/src/main.c
index 665a11f..283d937 100644
--- a/src/main.c
+++ b/src/main.c
@@ -42,7 +42,7 @@ 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");
+ elf_load("/test");
printf("loaded");
while (1) {
diff --git a/src/test.c b/src/test.c
deleted file mode 100644
index c0f214b..0000000
--- a/src/test.c
+++ /dev/null
@@ -1,5 +0,0 @@
-void main()
-{
- while (1) {
- }
-}