summaryrefslogtreecommitdiff
path: root/x86-bare-metal
diff options
context:
space:
mode:
Diffstat (limited to 'x86-bare-metal')
-rw-r--r--x86-bare-metal/mbr-textmode/.gitignore1
-rw-r--r--x86-bare-metal/mbr-textmode/Makefile28
-rw-r--r--x86-bare-metal/mbr-textmode/mbr.S98
-rw-r--r--x86-bare-metal/mbr-textmode/mbr.ld25
-rw-r--r--x86-bare-metal/mbr-textmode/zmbr.zig39
5 files changed, 191 insertions, 0 deletions
diff --git a/x86-bare-metal/mbr-textmode/.gitignore b/x86-bare-metal/mbr-textmode/.gitignore
new file mode 100644
index 0000000..a92a67f
--- /dev/null
+++ b/x86-bare-metal/mbr-textmode/.gitignore
@@ -0,0 +1 @@
+BUILD/ \ No newline at end of file
diff --git a/x86-bare-metal/mbr-textmode/Makefile b/x86-bare-metal/mbr-textmode/Makefile
new file mode 100644
index 0000000..7642791
--- /dev/null
+++ b/x86-bare-metal/mbr-textmode/Makefile
@@ -0,0 +1,28 @@
+O := BUILD
+
+$(O)/boot: $(O)/boot.elf | dump_info
+ objcopy -O binary $< $@
+
+$(O)/boot.elf: mbr.ld $(O)/mbr.o $(O)/zmbr.o
+ ld -o $@ -nostdlib -T $^
+
+$(O)/mbr.o: mbr.S | $(O)
+ gcc -m32 -c -o $@ -ffreestanding mbr.S
+
+$(O)/zmbr.o: zmbr.zig | $(O)
+ zig build-obj -femit-bin=$@ -target x86-freestanding-none -O ReleaseSmall $<
+
+clean:
+ $(RM) -r $(O)
+
+$(O):
+ mkdir -p $(O)
+
+dump_info: $(O)/boot.elf
+ objdump -Mintel --disassemble=kmain --visualize-jumps=extended-color $<
+ readelf -W -l $<
+ size $<
+ size $< | awk '/$(notdir $<)/ { print "MBR utilization " $$1/512 "%" }'
+
+run: $(O)/boot
+ qemu-system-i386 -hda $< $(QEMU_ARGS)
diff --git a/x86-bare-metal/mbr-textmode/mbr.S b/x86-bare-metal/mbr-textmode/mbr.S
new file mode 100644
index 0000000..be41bed
--- /dev/null
+++ b/x86-bare-metal/mbr-textmode/mbr.S
@@ -0,0 +1,98 @@
+// -- BOOT TEXT SECTION ---------------------------------------------------------
+
+.code16
+.intel_syntax noprefix
+
+.section .boot, "ax", @progbits
+ // Disable interrupts.
+ cli
+
+ // Clear segment selectors.
+ xor ax, ax
+ mov ds, ax
+ mov es, ax
+ mov ss, ax
+ mov fs, ax
+ mov gs, ax
+
+ // Set cs to 0x0000, as some BIOSes load the MBR to either 07c0:0000 or 0000:7c000.
+ jmp 0x0000:entry_rm16
+
+entry_rm16:
+ // Get current video mode [1].
+ // Return:
+ // ah number of columns
+ // al display mode (see table in [2])
+ //
+ // [1] http://www.ctyme.com/intr/rb-0108.htm
+ // [2] http://www.ctyme.com/intr/rb-0069.htm
+ mov ah, 0xf
+ int 0x10
+
+ // Execpt that the bios initializes text mode 0x3.
+ // * 80x25 text mode (cols x rows)
+ // * 2 byte per character
+ // [15] blink [14:12] bg color [11:8] fg color [7:0] char
+ // * 0xB80000 screen address
+ cmp al, 0x3
+ // Else we indicate an error with a blue screen.
+ je 2f
+ // Set background color [1].
+ //
+ // [1] http://www.ctyme.com/intr/rb-0101.htm
+ mov ah, 0xb
+ mov bx, 1
+ int 0x10
+1:
+ hlt
+ jmp 1b
+2:
+
+ // Enable A20 address line.
+ in al, 0x92
+ or al, 2
+ out 0x92, al
+
+ // Load GDT descriptor.
+ lgdt [gdt_desc]
+
+ // Enable protected mode (set CR0.PE bit).
+ mov eax, cr0
+ or eax, (1 << 0)
+ mov cr0, eax
+
+ // Far jump which loads segment selector (0x0008) into cs.
+ // 0x0008 -> RPL=0, TI=0(GDT), I=1
+ jmp 0x0008:entry_pm32
+
+.code32
+entry_pm32:
+ // Select data segment selector (0x0010) for ds.
+ mov ax, gdt_data - gdt
+ mov ds, ax
+
+ // Initialize stack pointer.
+ // Real Mode memory (https://wiki.osdev.org/Memory_Map_(x86)
+ // 0x00000500 - 0x00007BFF | 29.75 KiB | conventional memory
+ mov esp, 0x7c00
+
+ // Enter zmbr.zig:kmain.
+ // Should not return, but for safety we emit a call rather than a jmp.
+ call kmain
+
+1:
+ hlt
+ jmp 1b
+
+// -- RODATA SECTION ------------------------------------------------------------
+
+.section .rodata, "a", @progbits
+.balign 8
+gdt:
+ .8byte 0x0000000000000000 // 0x00 | null descriptor
+ .8byte 0x00cf9a000000ffff // 0x08 | 32 bit, code (rx), present, dpl=0, g=4K, base=0, limit=fffff
+gdt_data:
+ .8byte 0x00cf92000000ffff // 0x10 | 32 bit, data (rw), present, dpl=0, g=4K, base=0, limit=fffff
+gdt_desc:
+ .2byte (. - gdt - 1) // size
+ .4byte gdt // address
diff --git a/x86-bare-metal/mbr-textmode/mbr.ld b/x86-bare-metal/mbr-textmode/mbr.ld
new file mode 100644
index 0000000..372ea42
--- /dev/null
+++ b/x86-bare-metal/mbr-textmode/mbr.ld
@@ -0,0 +1,25 @@
+/*OUTPUT_FORMAT(binary)*/
+OUTPUT_FORMAT(elf32-i386)
+OUTPUT_ARCH(i386)
+
+SECTIONS {
+ . = 0x7c00;
+ .boot : {
+ *(.boot)
+ }
+ .text : { *(.text) }
+ .data : { *(.data) }
+ .rodata : { *(.rodata) }
+ _boot_end = .;
+
+ . = 0x7c00 + 510;
+ .mbr.magic : {
+ BYTE(0x55);
+ BYTE(0xaa);
+ }
+
+ /*/DISCARD/ : { *(.*) }*/
+ /*rest : { *(.*) }*/
+
+ ASSERT(_boot_end - 0x7c00 < 510, "boot sector must fit in 510 bytes")
+}
diff --git a/x86-bare-metal/mbr-textmode/zmbr.zig b/x86-bare-metal/mbr-textmode/zmbr.zig
new file mode 100644
index 0000000..2d95249
--- /dev/null
+++ b/x86-bare-metal/mbr-textmode/zmbr.zig
@@ -0,0 +1,39 @@
+const COLS = 80;
+const ROWS = 25;
+
+/// Clear screen (all black).
+fn clear_screen(video: []u16) void {
+ for (video) |*ch| {
+ ch.* = 0;
+ }
+}
+
+/// Draw the color palette.
+fn draw_palette(video: []u16) void {
+ // Each framebuffer entry in text mode is 16bit wide.
+ // [15] blink
+ // [14:12] bg color (3 bit)
+ // [11: 8] fg color (4 bit)
+ // [ 7: 0] ascii character
+ //
+ // https://en.wikipedia.org/wiki/VGA_text_mode
+
+ // Print each bg / fg combination once.
+ for (video, 0..0x80) |*ch, i| {
+ ch.* = @as(u16, @truncate(i)) << 8 | 'a';
+ }
+}
+
+// kmain should be "callconv(.naked)", once issue is fixed.
+// https://github.com/ziglang/zig/issues/18183
+export fn kmain() noreturn {
+ // Take a slice to VGA video memory (mode 3h text mode 80x25).
+ const video: []u16 = @as([*]u16, @ptrFromInt(0xB8000))[0 .. COLS * ROWS];
+
+ clear_screen(video);
+ draw_palette(video);
+
+ while (true) {
+ asm volatile ("hlt");
+ }
+}