summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohannes Stoelp <johannes.stoelp@gmail.com>2025-04-13 23:15:53 +0200
committerJohannes Stoelp <johannes.stoelp@gmail.com>2025-04-14 22:43:55 +0200
commita115fc9ad01a605aaad6886cd0fa8d05c328d403 (patch)
tree9275e89f659770df5b94e04edf3c723b19c6fcf8
parent2449af676962af487dabc8099862e5c404d9efd8 (diff)
downloadzig-playground-a115fc9ad01a605aaad6886cd0fa8d05c328d403.tar.gz
zig-playground-a115fc9ad01a605aaad6886cd0fa8d05c328d403.zip
mbr: read disk lba addressing exampleHEADmain
-rw-r--r--x86-bare-metal/mbr-disk-lba/.gitignore1
-rw-r--r--x86-bare-metal/mbr-disk-lba/Makefile40
-rw-r--r--x86-bare-metal/mbr-disk-lba/mbr.S125
-rw-r--r--x86-bare-metal/mbr-disk-lba/mbr.ld26
-rw-r--r--x86-bare-metal/mbr-disk-lba/zmbr.zig66
5 files changed, 258 insertions, 0 deletions
diff --git a/x86-bare-metal/mbr-disk-lba/.gitignore b/x86-bare-metal/mbr-disk-lba/.gitignore
new file mode 100644
index 0000000..a92a67f
--- /dev/null
+++ b/x86-bare-metal/mbr-disk-lba/.gitignore
@@ -0,0 +1 @@
+BUILD/ \ No newline at end of file
diff --git a/x86-bare-metal/mbr-disk-lba/Makefile b/x86-bare-metal/mbr-disk-lba/Makefile
new file mode 100644
index 0000000..f897450
--- /dev/null
+++ b/x86-bare-metal/mbr-disk-lba/Makefile
@@ -0,0 +1,40 @@
+O := BUILD
+
+all: dump_elf dump_bin
+
+$(O)/boot: $(O)/boot.elf
+ # MBR 512 bytes (sector 1 - lba 0)
+ objcopy -O binary $< $@
+ # Craft 512 bytes (sector 2 - lba 1)
+ printf "aaaa" >> $@
+ dd if=/dev/zero bs=1 count=508 >> $@
+ # Craft 512 bytes (sector 3 - lba 2)
+ printf "bbbb" >> $@
+ dd if=/dev/zero bs=1 count=508 >> $@
+
+$(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 -fno-strip -femit-bin=$@ -target x86-freestanding-none -O ReleaseSmall $<
+
+clean:
+ $(RM) -r $(O)
+
+$(O):
+ mkdir -p $(O)
+
+dump_elf: $(O)/boot.elf
+ @#objdump -Mintel --disassemble=kmain --visualize-jumps=extended-color $<
+ readelf -W -l $<
+ size $<
+ size $< | awk '/$(notdir $<)/ { print "MBR utilization " $$1/512 "%" }'
+
+dump_bin: $(O)/boot
+ hexdump -C $<
+
+run: $(O)/boot
+ qemu-system-i386 -hda $< $(QEMU_ARGS)
diff --git a/x86-bare-metal/mbr-disk-lba/mbr.S b/x86-bare-metal/mbr-disk-lba/mbr.S
new file mode 100644
index 0000000..df2cef1
--- /dev/null
+++ b/x86-bare-metal/mbr-disk-lba/mbr.S
@@ -0,0 +1,125 @@
+// -- 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
+
+// LBA - disk address packet.
+lba_pkt:
+ .byte 0x10 // Size of this disk packet in bytes (16).
+ .byte 0 // Reserved.
+ .2byte 2 // Number of blocks to read (sectors 512 bytes?) to read.
+ .4byte 0x7e00 // Destination address.
+ .8byte 1 // Starting lba block number (0 indexed, MBR is at 0).
+
+entry_rm16:
+ // Disk extended read.
+ // ah = 42h
+ // dl = drive number
+ // ds:si = address of disk packet
+ // Return
+ // cf = 0 (success) 1 (failed)
+ // ah = 0 (success) error code (failed)
+
+ // [1] http://www.ctyme.com/intr/rb-0708.htm
+ mov ah, 0x42
+ //mov dl, #drive // bios puts boot disk into dl
+ lea si, [lba_pkt]
+ int 0x13
+
+ jnc 2f
+1:
+ hlt
+ jmp 1b
+2:
+ // 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-disk-lba/mbr.ld b/x86-bare-metal/mbr-disk-lba/mbr.ld
new file mode 100644
index 0000000..b93543a
--- /dev/null
+++ b/x86-bare-metal/mbr-disk-lba/mbr.ld
@@ -0,0 +1,26 @@
+/*OUTPUT_FORMAT(binary)*/
+OUTPUT_FORMAT(elf32-i386)
+OUTPUT_ARCH(i386)
+
+SECTIONS {
+ . = 0x7c00;
+ .boot : {
+ *(.boot)
+ }
+ .text : { *(.text) }
+ .data : { *(.data) }
+ .bss : { *(.bss) }
+ .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-disk-lba/zmbr.zig b/x86-bare-metal/mbr-disk-lba/zmbr.zig
new file mode 100644
index 0000000..e5b06ea
--- /dev/null
+++ b/x86-bare-metal/mbr-disk-lba/zmbr.zig
@@ -0,0 +1,66 @@
+// Frambuffer limits.
+const COLS = 80;
+const ROWS = 25;
+
+// Frambuffer cursor.
+var col: u16 = 0;
+var row: u16 = 0;
+
+// Frambuffer.
+const fb: []u16 = @as([*]u16, @ptrFromInt(0xB8000))[0 .. COLS * ROWS];
+
+/// Clear screen (all black).
+fn clear_screen() void {
+ for (fb) |*ch| {
+ ch.* = 0;
+ }
+}
+
+/// Draw string to current cursor position.
+fn puts(str: []const u8) 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
+ for (str) |ch| {
+ if (ch == '\n') {
+ col = 0;
+ row += 1;
+ } else {
+ const pos = (row * COLS + col);
+ // bg - black; fg - white;
+ fb[pos] = @as(u16, 15) << 8 | ch;
+ col += 1;
+ }
+ if (col == COLS) {
+ row += 1;
+ if (row == ROWS) {
+ row = 0;
+ }
+ }
+ }
+}
+
+// kmain should be "callconv(.naked)", once issue is fixed.
+// https://github.com/ziglang/zig/issues/18183
+export fn kmain() noreturn {
+ clear_screen();
+
+ // Print first bytes of LBA block 1 we loaded from disk (sector 2).
+ const lba1: []const u8 = @as([*]const u8, @ptrFromInt(0x7e00))[0..4];
+ puts("lba1: ");
+ puts(lba1);
+ puts("\n");
+
+ // Print first bytes of LBA block 2 we loaded from disk (sector 3).
+ const lba2: []const u8 = @as([*]const u8, @ptrFromInt(0x8000))[0..4];
+ puts("lba2: ");
+ puts(lba2);
+ puts("\n");
+
+ while (true) {
+ asm volatile ("hlt");
+ }
+}