Skip to content

Instantly share code, notes, and snippets.

@colejhudson
Created March 11, 2021 07:10
Show Gist options
  • Save colejhudson/cbcdd98b55a9e4674328ae89ac5f6cce to your computer and use it in GitHub Desktop.
Save colejhudson/cbcdd98b55a9e4674328ae89ac5f6cce to your computer and use it in GitHub Desktop.
diff --git a/boot/load.c b/boot/load.c
index f80808c..05d583a 100644
--- a/boot/load.c
+++ b/boot/load.c
@@ -1,10 +1,9 @@
+#include "memory.h"
#include "printf.h"
#include "modes.h"
#include "vga.h"
-void test(void);
-
-__attribute__ ((naked)) void main() {
+void main() {
__asm__ volatile(
"movw $0x10, %%ax\n\t"
"movw %%ax, %%ss\n\t"
@@ -17,11 +16,51 @@ __attribute__ ((naked)) void main() {
: "ax"
);
- vga_puts("testing string formatting\n");
+ vga_puts("Starting!\n");
+
+ short int *ss = 0;
+ __asm__ (
+ "movl %%ss, %%eax\n\t"
+ "movl %%eax, %0\n\t"
+ : "=rm" (ss)
+ : /* no inputs */
+ : "ax"
+ );
+
+ unsigned int *esp = 0;
+ __asm__ (
+ "movl %%esp, %0\n\t"
+ : "=rm" (esp)
+ : /* no inputs */
+ );
+
+ unsigned int ebp = 0;
+ __asm__ (
+ "movl %%ebp, %0\n\t"
+ : "=rm" (ebp)
+ : /* no inputs */
+ );
+
+ char *fmt = (
+ "Boot Entry: 0x%x\n"
+ "Loader Entry: 0x%x\n"
+ "ss: 0x%x\n"
+ "esp: 0x%x\n"
+ "ebp: 0x%x\n"
+ );
+
+ char buf[200];
+ vsprintf(buf, 200, fmt, 0x7C00, 0x7E00, ss, esp, ebp);
+ vga_puts(buf);
+
+ uint32_t map_entry = 0;
+ map_entry = smap_get_entry(&map_entry, sizeof(map_entry));
- test();
+ char map_entry_msg[100];
+ vsprintf(map_entry_msg, 100, "map: %x", map_entry);
+ vga_puts(map_entry_msg);
- vga_puts("Success!");
+ vga_puts("Done!");
__asm__ volatile(
"hlt"
@@ -31,8 +70,51 @@ __attribute__ ((naked)) void main() {
);
}
-void test() {
- char buf[40];
- vsprintf(buf, 40, "0x%x\n", 254);
- vga_puts(buf);
-}
+uint32_t smap_get_entry(uint32_t *entry, uint32_t size) {
+ if(entry == 0 || entry <= 0xFFFF) {
+ return 0;
+ }
+
+ if(size < 24) {
+ return 0;
+ }
+
+ /* Hexadecimal representation of the string "SMAP" */
+ uint32_t signature = 0x534D4150;
+ uint32_t segment = entry >> 4;
+ uint32_t offset = entry - segment;
+ uint32_t prev_entry = *entry;
+
+ uint16_t es = 0;
+ __asm__ volatile(
+ "mov %%es, %0\n\t"
+ : "=rm" (es)
+ );
+
+ enter_virtual_8086_mode();
+
+ __asm__ volatile(
+ "movl %0, %%eax\n\t"
+ "movl %%eax, %%es\n\t"
+
+ "movl 0xe820, %%eax\n\t"
+ "movw %1, %%di\n\t"
+ "movl %2, %%edx\n\t"
+ "movl %3, %%ebx\n\t"
+
+ "int $0x15\n\t"
+ : /* no outputs */
+ : (segment), (offset), (signature)
+ : "eax", "ebx", "di"
+ );
+
+ exit_virtual_8086_mode();
+
+ __asm__(
+ "mov %0, %%es\n\t"
+ : /* no outputs */
+ : (es)
+ );
+
+ return 0;
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment