Working on stivale
diff --git a/src/lib/elf.c b/src/lib/elf.c
index 0270733b..939c71c3 100644
--- a/src/lib/elf.c
+++ b/src/lib/elf.c
@@ -59,10 +59,25 @@ struct elf_shdr {
uint64_t sh_entsize;
};
-int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name) {
+int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name, size_t limit) {
struct elf_hdr hdr;
echfs_read(fd, &hdr, 0, sizeof(struct elf_hdr));
+ if (strncmp((char *)hdr.ident, "\177ELF", 4)) {
+ print("elf: Not a valid ELF file.\n");
+ return 1;
+ }
+
+ if (hdr.ident[EI_DATA] != BITS_LE) {
+ print("elf: Not a Little-endian ELF file.\n");
+ return 1;
+ }
+
+ if (hdr.machine != ARCH_X86_64) {
+ print("elf: Not an x86_64 ELF file.\n");
+ return 1;
+ }
+
struct elf_shdr shstrtab;
echfs_read(fd, &shstrtab, hdr.shoff + hdr.shstrndx * sizeof(struct elf_shdr),
sizeof(struct elf_shdr));
@@ -76,12 +91,14 @@ int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *nam
sizeof(struct elf_shdr));
if (!strcmp(&names[section.sh_name], name)) {
+ if (section.sh_size > limit)
+ return 3;
echfs_read(fd, buffer, section.sh_offset, section.sh_size);
return 0;
}
}
- return 1;
+ return 2;
}
#define FIXED_HIGHER_HALF_OFFSET ((uint64_t)0xffffffff80000000)
diff --git a/src/lib/elf.h b/src/lib/elf.h
index c8edee9e..447db2ed 100644
--- a/src/lib/elf.h
+++ b/src/lib/elf.h
@@ -5,6 +5,6 @@
#include <fs/echfs.h>
int elf_load(struct echfs_file_handle *fd, uint64_t *entry_point);
-int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name);
+int elf_load_section(struct echfs_file_handle *fd, void *buffer, const char *name, size_t limit);
#endif
diff --git a/src/protos/stivale.c b/src/protos/stivale.c
index c9bda16f..a4e19410 100644
--- a/src/protos/stivale.c
+++ b/src/protos/stivale.c
@@ -2,16 +2,56 @@
#include <stddef.h>
#include <protos/stivale.h>
#include <lib/elf.h>
+#include <lib/blib.h>
struct stivale_header {
- uint64_t magic;
uint64_t stack;
uint8_t video_mode; // 0 = default at boot (CGA text mode). 1 = graphical VESA
-};
+} __attribute__((packed));
+
+struct stivale_module {
+ uint64_t begin;
+ uint64_t end;
+ char string[128];
+} __attribute__((packed));
+
+struct stivale_struct {
+ char *cmdline;
+ uint64_t memory_map_addr;
+ uint64_t memory_map_entries;
+ uint64_t framebuffer_addr;
+ uint16_t framebuffer_pitch;
+ uint16_t framebuffer_width;
+ uint16_t framebuffer_height;
+ uint16_t framebuffer_bpp;
+ uint64_t module_count;
+ struct stivale_module modules[];
+} __attribute__((packed));
+
+struct stivale_struct stivale_struct;
void stivale_load(struct echfs_file_handle *fd) {
uint64_t entry_point;
+ struct stivale_header stivale_hdr;
+ int ret = elf_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
+ switch (ret) {
+ case 1:
+ print("stivale: File is not a valid ELF.\n");
+ for (;;);
+ case 2:
+ print("stivale: Section .stivalehdr not found.\n");
+ for (;;);
+ case 3:
+ print("stivale: Section .stivalehdr exceeds the size of the struct.\n");
+ for (;;);
+ default:
+ break;
+ }
+
+ print("stivale: Requested stack at %X\n", stivale_hdr.stack);
+ print("stivale: Video mode: %u\n", stivale_hdr.video_mode);
+
elf_load(fd, &entry_point);
volatile struct {
@@ -66,6 +106,6 @@ void stivale_load(struct echfs_file_handle *fd) {
"jmp [rbx]\n\t"
".code32\n\t"
:
- : "a" (pagemap), "b" (&entry_point)
+ : "a" (pagemap), "b" (&entry_point), "S" (&stivale_struct)
);
}
diff --git a/test/test.asm b/test/test.asm
index e51b3611..f3785f0d 100644
--- a/test/test.asm
+++ b/test/test.asm
@@ -1,7 +1,17 @@
; This is a compliant "kernel" meant for testing purposes.
; Header
+section .stivalehdr
+stivale_header:
+ dq stack.top
+ db 0
+
+section .bss
+
+stack:
+ resb 4096
+ .top:
section .text
