minor boot0 cleanup

main
pantonshire 1 year ago
parent cdb70a4105
commit 8a75404437

@ -40,6 +40,7 @@ GPT_SECTORS_LOADED equ 0x16
GPT_CURRENT_LBA equ 0x18
STAGE2_GPT_ENTRY_ADDR equ 0x1a
main:
; Disable interrupts
cli
@ -71,7 +72,7 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov di, 0x00
mov ah, 0x08
int 0x13
jc .panic
jc panic
; Load sectors per track into cx & spill
and cl, 0x3f
xor ch, ch
@ -84,14 +85,14 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
; Load LBA 1.
mov ax, 1
mov bx, 0x7e00
call .read_lba
call read_lba
; Check the GPT header magic "EFI PART"
mov cx, GPT_MAGIC_LEN
mov si, gpt_magic
mov di, 0x7e00
repe cmpsb
jne .panic
jne panic
; Ensure the 8-byte GPT starting LBA fits in 16 bits
mov di, 0x7e00 ; The rep increments di so we need to reset it
@ -99,7 +100,7 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov bx, [di + 0x4a]
or ax, bx
or eax, eax
jnz .panic
jnz panic
; Load and spill the GPT starting LBA
mov ax, [di + 0x48]
push ax
@ -123,9 +124,9 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
; - If each entry is larger than a sector (512 bytes), they'll be sector-aligned.
; - If each entry is smaller than a sector, an integer number of them will fit into a sector.
or eax, eax ; Test size != 0 because 128 * 2^n != 0
jz .panic
jz panic
and eax, 127 ; Test size is a multiple of 128
jnz .panic
jnz panic
; Use the (n & (n - 1)) == 0 trick to test if the entry size is a power of 2. Since we already
; know it's a nonzero multiple of 128, if size is a power of 2 then size = 128 * 2^n holds.
; Therefore we don't need to bother dividing by 128 first (shr 7), which saves a couple of bytes.
@ -133,13 +134,13 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov ecx, ebx
dec ecx
and ecx, eax
jnz .panic
jnz panic
; Find the "sector stride", which is the number of sectors we increment by each time we want to
; load a new entry.
shr eax, 9 ; Divide by sector size to get sectors per entry
cmp eax, 0xffff ; Make sure sectors per entry fits in 16 bits
ja .panic
ja panic
or ax, ax
jnz .gpt_sector_stride_loaded
; Sector stride must be at least one or we'll load the same sector each time!
@ -175,7 +176,7 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov dx, [bp - GPT_CURRENT_ENTRY_IDX]
cmp [bp - GPT_N_ENTRIES_16], dx
; Panic if we've run out of partitions and haven't found the second stage yet.
jbe .panic
jbe panic
; If we haven't loaded any sectors yet, load the first one.
cmp word [bp - GPT_SECTORS_LOADED], 0
@ -192,7 +193,7 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
; Read the current LBA to 0x8000 (just past the end of the GPT header)
mov ax, [bp - GPT_CURRENT_LBA]
mov bx, 0x8000
call .read_lba
call read_lba
; Increment number of sectors loaded
inc word [bp - GPT_SECTORS_LOADED]
@ -224,9 +225,9 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov ebx, [si + 0x24]
; Ensure it fits in 16 bits.
or ebx, ebx
jnz .panic
jnz panic
cmp ebx, 0xffff
ja .panic
ja panic
; Load partition LBA end.
mov ecx, [si + 0x28]
mov edx, [si + 0x2c]
@ -235,11 +236,11 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
or edx, edx
jnz .stage2_end_lba_ok
cmp eax, ecx
ja .panic
ja panic
.stage2_end_lba_ok:
mov bx, 0x8200
call .read_lba
call read_lba
jmp bx
; Load a single boot disk sector. Panic on failure.
@ -247,7 +248,7 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
; - ax: LBA to load
; - bx: address to read sector to
; Clobber: ax, cx, dx
.read_lba:
read_lba:
; sector - 1 = LBA % sectors_per_track
; temp = LBA / sectors_per_track
; head = temp % n_heads
@ -270,10 +271,10 @@ STAGE2_GPT_ENTRY_ADDR equ 0x1a
mov al, 1
; Read sector
int 0x13
jc .panic
jc panic
ret
.panic:
panic:
mov ax, 0x0003
int 0x10
mov word fs:[0x0000], 0x4f21
@ -287,7 +288,7 @@ guid_stage2 db 0x69, 0xea, 0xff, 0xfd, 0x51, 0x36, 0x2f, 0x44, \
0xa1, 0x1d, 0x88, 0xa0, 0x9b, 0xf3, 0x72, 0xdd
GUID_LEN equ $ - guid_stage2
; MBR bootstrap field is 440 bytes long
%if ($ - $$) > 440
%error "exceeded mbr bootstrap field size"
%endif
; MBR bootstrap field is 440 bytes long
%if ($ - $$) > 440
%error "exceeded mbr bootstrap field size"
%endif

@ -6,6 +6,7 @@
- <https://www.eecg.utoronto.ca/~amza/www.mindsec.com/files/x86regs.html>
- <https://en.wikibooks.org/wiki/X86_Assembly/16,_32,_and_64_Bits>
- <https://www.plantation-productions.com/Webster/www.artofasm.com/DOS/ch04/CH04-2.html>
- <https://www.tortall.net/projects/yasm/manual/html/nasm-local-label.html>
## Boot sequence
- <https://wiki.osdev.org/Boot_Sequence>

Loading…
Cancel
Save