:: commit 806766aa9025e008aa29e1f0d3f21ce7b56c716c

ethan <ethanm4984@gmail.com> — 2020-09-30 23:08

parents: 2eac51b602

protocol code clean up

diff --git a/stage2/Makefile b/stage2/Makefile
index f2051cfe..14d36653 100644
--- a/stage2/Makefile
+++ b/stage2/Makefile
@@ -1,6 +1,6 @@
-CC = i386-elf-gcc
-LD = i386-elf-gcc
-OBJCOPY = i386-elf-objcopy
+CC = gcc -m32
+LD = gcc -m32
+OBJCOPY = objcopy
 
 CFLAGS = -flto -Os -pipe -Wall -Wextra
 
diff --git a/stage2/lib/image.c b/stage2/lib/image.c
index 37cb8b8f..a265a562 100644
--- a/stage2/lib/image.c
+++ b/stage2/lib/image.c
@@ -1,6 +1,9 @@
 #include <stdint.h>
 #include <stddef.h>
 #include <lib/image.h>
+#include <lib/config.h>
+#include <lib/blib.h>
+#include <mm/pmm.h>
 #include <lib/bmp.h>
 
 int open_image(struct image *image, struct file_handle *file) {
@@ -11,3 +14,35 @@ int open_image(struct image *image, struct file_handle *file) {
 
     return -1;
 }
+
+struct kernel_loc get_kernel_loc(int boot_drive) {
+    int kernel_drive; {
+        char buf[32];
+        if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
+            kernel_drive = boot_drive;
+        } else {
+            kernel_drive = (int)strtoui(buf);
+        }
+    }
+
+    int kernel_part; {
+        char buf[32];
+        if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
+            panic("KERNEL_PARTITION not specified");
+        } else {
+            kernel_part = (int)strtoui(buf);
+        }
+    }
+
+    char *kernel_path = conv_mem_alloc(128);
+    if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
+        panic("KERNEL_PATH not specified");
+    }
+
+    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
+    if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
+        panic("Could not open kernel file");
+    }
+
+    return (struct kernel_loc) { kernel_drive, kernel_part, kernel_path, fd };
+} 
diff --git a/stage2/lib/image.h b/stage2/lib/image.h
index 09af1228..d0068ff8 100644
--- a/stage2/lib/image.h
+++ b/stage2/lib/image.h
@@ -12,6 +12,15 @@ struct image {
     void *local;
 };
 
+struct kernel_loc {
+    int kernel_drive; 
+    int kernel_part;
+    char *kernel_path;
+    struct file_handle *fd;
+};
+
 int open_image(struct image *image, struct file_handle *file);
 
+struct kernel_loc get_kernel_loc(int boot_drive);
+
 #endif
diff --git a/stage2/protos/linux.c b/stage2/protos/linux.c
index 4fa19853..9848e95f 100644
--- a/stage2/protos/linux.c
+++ b/stage2/protos/linux.c
@@ -52,36 +52,10 @@ static void spinup(uint16_t real_mode_code_seg, uint16_t kernel_entry_seg) {
 }
 
 void linux_load(char *cmdline, int boot_drive) {
-    int kernel_drive; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
-            kernel_drive = boot_drive;
-        } else {
-            kernel_drive = (int)strtoui(buf);
-        }
-    }
-
-    int kernel_part; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
-            panic("KERNEL_PARTITION not specified");
-        } else {
-            kernel_part = (int)strtoui(buf);
-        }
-    }
-
-    char *kernel_path = conv_mem_alloc(128);
-    if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
-        panic("KERNEL_PATH not specified");
-    }
-
-    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
-    if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
-        panic("Could not open kernel file");
-    }
+    struct kernel_loc kernel = get_kernel_loc(boot_drive);
 
     uint32_t signature;
-    fread(fd, &signature, 0x202, sizeof(uint32_t));
+    fread(kernel.fd, &signature, 0x202, sizeof(uint32_t));
 
     // validate signature
     if (signature != 0x53726448) {
@@ -89,7 +63,7 @@ void linux_load(char *cmdline, int boot_drive) {
     }
 
     size_t setup_code_size = 0;
-    fread(fd, &setup_code_size, 0x1f1, 1);
+    fread(kernel.fd, &setup_code_size, 0x1f1, 1);
 
     if (setup_code_size == 0)
         setup_code_size = 4;
@@ -104,7 +78,7 @@ void linux_load(char *cmdline, int boot_drive) {
 
     void *real_mode_code = conv_mem_alloc_aligned(real_mode_code_size, 0x1000);
 
-    fread(fd, real_mode_code, 0, real_mode_code_size);
+    fread(kernel.fd, real_mode_code, 0, real_mode_code_size);
 
     size_t heap_end_ptr = ((real_mode_code_size & 0x0f) + 0x10) - 0x200;
     *((uint16_t *)(real_mode_code + 0x224)) = (uint16_t)heap_end_ptr;
@@ -142,8 +116,8 @@ void linux_load(char *cmdline, int boot_drive) {
 
     // load kernel
     print("Loading kernel...\n");
-    memmap_alloc_range(KERNEL_LOAD_ADDR, fd->size - real_mode_code_size, 0);
-    fread(fd, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, fd->size - real_mode_code_size);
+    memmap_alloc_range(KERNEL_LOAD_ADDR, kernel.fd->size - real_mode_code_size, 0);
+    fread(kernel.fd, (void *)KERNEL_LOAD_ADDR, real_mode_code_size, kernel.fd->size - real_mode_code_size);
 
     char initrd_path[64];
     if (!config_get_value(initrd_path, 0, 64, "INITRD_PATH"))
@@ -152,14 +126,14 @@ void linux_load(char *cmdline, int boot_drive) {
     int initrd_part; {
         char buf[32];
         if (!config_get_value(buf, 0, 32, "INITRD_PARTITION")) {
-            initrd_part = fd->partition;
+            initrd_part = kernel.fd->partition;
         } else {
             initrd_part = (int)strtoui(buf);
         }
     }
 
     struct file_handle initrd;
-    if (fopen(&initrd, fd->disk, initrd_part, initrd_path)) {
+    if (fopen(&initrd, kernel.fd->disk, initrd_part, initrd_path)) {
         panic("Failed to open initrd");
     }
 
diff --git a/stage2/protos/stivale.c b/stage2/protos/stivale.c
index 1250442f..ce1a7477 100644
--- a/stage2/protos/stivale.c
+++ b/stage2/protos/stivale.c
@@ -26,37 +26,11 @@ struct stivale_struct stivale_struct = {0};
 void stivale_load(char *cmdline, int boot_drive) {
     stivale_struct.flags |= (1 << 0);  // set bit 0 since we are BIOS and not UEFI
 
-    int kernel_drive; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
-            kernel_drive = boot_drive;
-        } else {
-            kernel_drive = (int)strtoui(buf);
-        }
-    }
-
-    int kernel_part; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
-            panic("KERNEL_PARTITION not specified");
-        } else {
-            kernel_part = (int)strtoui(buf);
-        }
-    }
-
-    char *kernel_path = conv_mem_alloc(128);
-    if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
-        panic("KERNEL_PATH not specified");
-    }
-
-    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
-    if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
-        panic("Could not open kernel file");
-    }
+    struct kernel_loc kernel = get_kernel_loc(boot_drive);
 
     struct stivale_header stivale_hdr;
 
-    int bits = elf_bits(fd);
+    int bits = elf_bits(kernel.fd);
 
     int ret;
 
@@ -78,20 +52,20 @@ void stivale_load(char *cmdline, int boot_drive) {
                 level5pg = true;
             }
 
-            ret = elf64_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
+            ret = elf64_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
 
             if (!ret && ((stivale_hdr.flags >> 2) & 1)) {
                 // KASLR is enabled, set the slide
                 slide = rand64() & KASLR_SLIDE_BITMASK;
 
                 // Re-read the .stivalehdr with slid relocations
-                ret = elf64_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
+                ret = elf64_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header), slide);
             }
 
             break;
         }
         case 32:
-            ret = elf32_load_section(fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
+            ret = elf32_load_section(kernel.fd, &stivale_hdr, ".stivalehdr", sizeof(struct stivale_header));
             break;
         default:
             panic("stivale: Not 32 nor 64 bit x86 ELF file.");
@@ -117,10 +91,10 @@ void stivale_load(char *cmdline, int boot_drive) {
 
     switch (bits) {
         case 64:
-            elf64_load(fd, &entry_point, &top_used_addr, slide, 10);
+            elf64_load(kernel.fd, &entry_point, &top_used_addr, slide, 10);
             break;
         case 32:
-            elf32_load(fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 10);
+            elf32_load(kernel.fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 10);
             break;
     }
 
@@ -149,14 +123,14 @@ void stivale_load(char *cmdline, int boot_drive) {
         int part; {
             char buf[32];
             if (!config_get_value(buf, i, 32, "MODULE_PARTITION")) {
-                part = kernel_part;
+                part = kernel.kernel_part;
             } else {
                 part = (int)strtoui(buf);
             }
         }
 
         struct file_handle f;
-        if (fopen(&f, fd->disk, part, module_file)) {
+        if (fopen(&f, kernel.fd->disk, part, module_file)) {
             panic("Requested module with path \"%s\" not found!\n", module_file);
         }
 
diff --git a/stage2/protos/stivale2.c b/stage2/protos/stivale2.c
index 8a3a0db8..dfe88a89 100644
--- a/stage2/protos/stivale2.c
+++ b/stage2/protos/stivale2.c
@@ -7,6 +7,7 @@
 #include <lib/elf.h>
 #include <lib/blib.h>
 #include <lib/acpi.h>
+#include <lib/image.h>
 #include <lib/config.h>
 #include <lib/time.h>
 #include <lib/print.h>
@@ -49,37 +50,11 @@ static void append_tag(struct stivale2_struct *s, struct stivale2_tag *tag) {
 }
 
 void stivale2_load(char *cmdline, int boot_drive) {
-    int kernel_drive; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_DRIVE")) {
-            kernel_drive = boot_drive;
-        } else {
-            kernel_drive = (int)strtoui(buf);
-        }
-    }
-
-    int kernel_part; {
-        char buf[32];
-        if (!config_get_value(buf, 0, 32, "KERNEL_PARTITION")) {
-            panic("KERNEL_PARTITION not specified");
-        } else {
-            kernel_part = (int)strtoui(buf);
-        }
-    }
-
-    char *kernel_path = conv_mem_alloc(128);
-    if (!config_get_value(kernel_path, 0, 128, "KERNEL_PATH")) {
-        panic("KERNEL_PATH not specified");
-    }
-
-    struct file_handle *fd = conv_mem_alloc(sizeof(struct file_handle));
-    if (fopen(fd, kernel_drive, kernel_part, kernel_path)) {
-        panic("Could not open kernel file");
-    }
+    struct kernel_loc kernel = get_kernel_loc(boot_drive);
 
     struct stivale2_header stivale2_hdr;
 
-    int bits = elf_bits(fd);
+    int bits = elf_bits(kernel.fd);
 
     int ret;
 
@@ -101,20 +76,20 @@ void stivale2_load(char *cmdline, int boot_drive) {
                 level5pg = true;
             }
 
-            ret = elf64_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
+            ret = elf64_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
 
             if (!ret && (stivale2_hdr.flags & 1)) {
                 // KASLR is enabled, set the slide
                 slide = rand64() & KASLR_SLIDE_BITMASK;
 
                 // Re-read the .stivale2hdr with slid relocations
-                ret = elf64_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
+                ret = elf64_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header), slide);
             }
 
             break;
         }
         case 32:
-            ret = elf32_load_section(fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header));
+            ret = elf32_load_section(kernel.fd, &stivale2_hdr, ".stivale2hdr", sizeof(struct stivale2_header));
             break;
         default:
             panic("stivale2: Not 32 nor 64 bit x86 ELF file.");
@@ -140,10 +115,10 @@ void stivale2_load(char *cmdline, int boot_drive) {
 
     switch (bits) {
         case 64:
-            elf64_load(fd, &entry_point, &top_used_addr, slide, 0x1001);
+            elf64_load(kernel.fd, &entry_point, &top_used_addr, slide, 0x1001);
             break;
         case 32:
-            elf32_load(fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 0x1001);
+            elf32_load(kernel.fd, (uint32_t *)&entry_point, (uint32_t *)&top_used_addr, 0x1001);
             break;
     }
 
@@ -194,14 +169,14 @@ void stivale2_load(char *cmdline, int boot_drive) {
         int part; {
             char buf[32];
             if (!config_get_value(buf, i, 32, "MODULE_PARTITION")) {
-                part = kernel_part;
+                part = kernel.kernel_part;
             } else {
                 part = (int)strtoui(buf);
             }
         }
 
         struct file_handle f;
-        if (fopen(&f, fd->disk, part, module_file)) {
+        if (fopen(&f, kernel.fd->disk, part, module_file)) {
             panic("Requested module with path \"%s\" not found!\n", module_file);
         }
 
tab: 248 wrap: offon