From 176b6eb808f4d02d871c30f6ec19daa179f374d1 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Fri, 9 Jul 2021 14:43:12 +0200
Subject: Multiboot structs

---
 run                      |  2 +-
 src/entry/bootsector.asm |  2 ++
 src/loader/impl/mb1.c    | 25 ++++++++++++++++++-------
 src/loader/inc/cfg.h     |  2 ++
 src/loader/int.c         |  9 +++------
 src/loader/main.c        |  8 ++++++--
 6 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/run b/run
index 72ae723..ee456ae 100755
--- a/run
+++ b/run
@@ -105,7 +105,7 @@ build() {
 }
 
 emulate() {
-    qemu-system-i386 -d guest_errors -cpu max -serial stdio -m 256M -vga std -drive file=build/disk.img,format=raw,index=1,media=disk
+    qemu-system-i386 -enable-kvm -d guest_errors -cpu max -serial stdio -m 256M -vga std -drive file=build/disk.img,format=raw,index=1,media=disk
 }
 
 # Always generate updated configurations
diff --git a/src/entry/bootsector.asm b/src/entry/bootsector.asm
index a98c22b..d66fb09 100644
--- a/src/entry/bootsector.asm
+++ b/src/entry/bootsector.asm
@@ -105,7 +105,9 @@ protected_mode:
 	mov gs, ax
 	mov ss, ax
 
+	push dx ; Pass disk
 	call loader
+
 	jmp error_loop
 bits 16
 
diff --git a/src/loader/impl/mb1.c b/src/loader/impl/mb1.c
index 5541c6e..4aa267a 100644
--- a/src/loader/impl/mb1.c
+++ b/src/loader/impl/mb1.c
@@ -9,7 +9,7 @@
 // The address where data gets stored
 #define MB1_LOAD_ADDRESS 0x10000
 
-struct multiboot_entry {
+struct mb1_entry {
 	u32 magic;
 	u32 flags;
 	u32 checksum; // Everything after that is optional
@@ -25,7 +25,7 @@ struct multiboot_entry {
 };
 
 // The (really simple) multiboot checksum algorithm
-static u32 mb1_checksum(struct multiboot_entry *entry)
+static u32 mb1_checksum(struct mb1_entry *entry)
 {
 	return -(entry->magic + entry->flags);
 }
@@ -34,16 +34,27 @@ static u32 mb1_checksum(struct multiboot_entry *entry)
 static u32 mb1_store(void *data, u32 size)
 {
 	static u32 offset = 0;
-	memcpy((void *)MB1_LOAD_ADDRESS, data, size);
+	memcpy((void *)(MB1_LOAD_ADDRESS + offset), data, size);
 	offset += size;
-	return MB1_LOAD_ADDRESS + (size - offset);
+	return MB1_LOAD_ADDRESS + (offset - size);
 }
 
 // Load the mb1 structs into memory
-static void mb1_load(struct multiboot_entry *entry)
+static void mb1_load(struct mb1_entry *entry)
 {
-	(void)mb1_store;
 	(void)entry;
+
+	struct mb1_info info_struct = { 0 };
+	struct mb1_info *info = (void *)mb1_store(&info_struct, sizeof(info_struct));
+
+	// Set boot device
+	info->flags |= MB1_INFO_BOOTDEV;
+	info->boot_device = boot_disk;
+
+	// Set bootloader name
+	info->flags |= MB1_INFO_BOOT_LOADER_NAME;
+	char loader_name[] = "SegelBoot";
+	info->boot_loader_name = mb1_store(loader_name, sizeof(loader_name));
 }
 
 // Jump to kernel with correct info pointer in eax
@@ -71,7 +82,7 @@ u8 mb1_detect(struct cfg_entry *cfg)
 		return 0;
 
 	// Find start of multiboot entry by searching for magic
-	struct multiboot_entry *entry = 0;
+	struct mb1_entry *entry = 0;
 	for (u32 i = 0; i < sizeof(header); i++) {
 		u32 *p = (u32 *)&header[i];
 		if (*p == MB1_MAGIC) {
diff --git a/src/loader/inc/cfg.h b/src/loader/inc/cfg.h
index f3a15ab..cb91ab2 100644
--- a/src/loader/inc/cfg.h
+++ b/src/loader/inc/cfg.h
@@ -6,6 +6,8 @@
 #include <def.h>
 #include <impl.h>
 
+extern u8 boot_disk;
+
 // Global config element index (can actually be anything but wth)
 #define CFG_GLOBAL 0
 
diff --git a/src/loader/int.c b/src/loader/int.c
index 67b80f0..6bfea09 100644
--- a/src/loader/int.c
+++ b/src/loader/int.c
@@ -125,19 +125,16 @@ static u32 int_event_handler(struct int_frame *frame)
  * Universal handler
  */
 
-static u8 int_enabled = 1;
-
 u32 int_handler(u32 esp);
 u32 int_handler(u32 esp)
 {
 	struct int_frame *frame = (struct int_frame *)esp;
-	if (frame->int_no < 32) {
+	if (frame->int_no < 32)
 		int_trap_handler(frame);
-	} else if (int_enabled && frame->int_no < 48) {
+	else if (frame->int_no < 48)
 		esp = int_event_handler(frame);
-	} else if (frame->int_no >= 48) {
+	else
 		panic("Unknown interrupt\n");
-	}
 
 	pic_ack(frame->int_no);
 	return esp;
diff --git a/src/loader/main.c b/src/loader/main.c
index aa8e62d..ed3014b 100644
--- a/src/loader/main.c
+++ b/src/loader/main.c
@@ -15,9 +15,13 @@
  * Entry
  */
 
-int start(void);
-int start(void)
+u8 boot_disk = 0;
+
+int start(u8 disk);
+int start(u8 disk)
 {
+	boot_disk = disk;
+
 	vga_clear();
 	serial_install();
 
-- 
cgit v1.2.3