From a8b24c7b0102c65543ec7cb3c68e8fa5e500f3d8 Mon Sep 17 00:00:00 2001 From: Elaina Claus Date: Thu, 11 Sep 2025 19:05:50 -0400 Subject: [PATCH] add functions to get the mbr/vbr in stage2 --- src/stage2/stage2.nasm | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/stage2/stage2.nasm b/src/stage2/stage2.nasm index 1d90258..d74c65b 100755 --- a/src/stage2/stage2.nasm +++ b/src/stage2/stage2.nasm @@ -193,6 +193,62 @@ hcf: ; ; ############################## +; int read_mbr(int boot_drive, void* dst) +; destination buffer needs 512 bytes of space +read_mbr: + __CDECL16_PROC_ENTRY +.func: + __BOCHS_MAGIC_DEBUG + ; read mbr on boot drive to memory (for the partition table) + movzx ax, byte [bp - 4] + push ax ; drive_num (2) + push 0x01 ; count (2) + + push dword 0x0 ; lba (4) + + push word [bp - 6] ; offset = dst + push __STAGE2_SEGMENT ; this segment + __CDECL16_CALL read_disk_raw, 12 + ; TODO: this needs error checking, zero checking, check the sig a bunch of stuff... +.endf: + __CDECL16_PROC_EXIT +.error: + ERROR STEVIA_DEBUG_ERR + +; int read_vbr(int boot_drive, void* mbr, void* buf) +read_vbr: + __CDECL16_PROC_ENTRY +.func: + __BOCHS_MAGIC_DEBUG + ; read vbr on boot partition to memory (for fat bpb/ebpb) +.calc_part_offset: + mov bx, [bp - 6] + add bx, 0x1B8 ; offset to partition table in mbr + mov cx, 4 ; only checking 4 entries +.find_active_L0: + mov al, byte [bx + PartEntry_t.attributes] + test al, 0x80 ; 0x80 == 1000_0000b + jnz read_vbr.active_found + add bx, 0x10 ; add 16 bytes to offset + loop read_vbr.find_active_L0 + jmp read_vbr.error +.active_found: + movzx ax, byte [bp - 4] + push ax ; drive_num (2) + push 0x01 ; count (2) + + mov eax, dword [bx + PartEntry_t.lba_start] + push dword eax ; lba (4) + + push word [bp - 6] ; offset = dst + push __STAGE2_SEGMENT ; this segment + __CDECL16_CALL read_disk_raw, 12 + ; vbr (with fat bpb/ebpb) is at the buffer now +.endf: + __CDECL16_PROC_EXIT +.error: + ERROR STEVIA_DEBUG_ERR + ; set ds and es segments back to the base of the loader %ifnmacro __TINY_DS_ES %macro __TINY_DS_ES 0