Revamp MBR

New features:
- ES:DI (potential PnP structure) is preserved
- FAT detection now works by checking for BPB values
- Uses booterr procedure
This commit is contained in:
Nero 2020-04-21 23:23:37 +02:00
parent bbfcd42f61
commit abc7b563cd

View File

@ -1,51 +1,65 @@
; Memory layout:
%define self 0x00600 ; 1 sector
%define prog 0x07C00 ; 1 sector
; FDC fields in VBR
%define spt (prog + 0x18)
%define nos (prog + 0x1A)
%define po (prog + 0x1C)
%define bps (bs + 0x0B)
%define md (bs + 0x15)
%define spt (bs + 0x18)
%define nos (bs + 0x1A)
%define po (bs + 0x1C)
org self
cpu 8086
; blah dont judge me, i miss 8080
%macro retnz 0
jz short ($+3)
ret
%endmacro
%macro callc 1
jnc short ($+5)
call %1
%endmacro
init:
cli
cpu 8086
org 0x0600
bs: equ 0x7C00
; Stack grows down from PSP + 64k
init: cli
xor ax, ax
mov ss, ax
mov sp, ax
xor sp, sp
; save driver number
push dx
; Relocate from [prog] to [self]
; save PnP ptr
push es
push di
; Relocate away from bootsector loading area
mov ds, ax
mov es, ax
mov si, prog
mov di, self
mov si, bs
mov di, $$
mov cx, 0x100
rep movsw
jmp 0:main
main:
mov bp, 0x3335
mov si, part1
%include "inc/booterr.asm"
; find active partition or bail out
; result in si
findp: mov si, part1
mov cx, 4
.loop:
test BYTE [si], 0x80
jnz loadpart
.loop: test BYTE [si], 0x80
retnz
add si, 0x10
loop .loop
jmp error
call errcll
main: ; find active partition
call findp
loadpart:
; transfer starting address into DAP
push si
add si, 0x08
mov di, dap.blocknum
mov di, dap.num
movsw
movsw
pop si
@ -53,24 +67,30 @@ loadpart:
; load sector
push si
mov si, dap
mov bp, 0x3336
mov ah, 0x42
stc
int 0x13
jc error
callc errcll
pop si
cmp BYTE [si+4], 0x01
jne jump
; FAT bpb has "bytes per sector" at this place
; we support only 512 byte sectors anyways
cmp word [bps], 512
jne chain
adjust:
push dx
; Media descriptor byte is never smaller than 0xF0
cmp byte [md], 0xF0
jc chain
mov bp, 0x3337
; ask BIOS what our disk geometry is
mov ah, 0x08
stc
int 0x13
jc error
jc chain
; ES might be messed up from int 13h, fix it
push cs
pop es
; update sectors per track
xor ax, ax
@ -90,47 +110,33 @@ adjust:
movsw
pop si
chain: ; restore PnP structure
pop di
pop es
pop dx
jump:
jmp 0:prog
error:
mov ax, bp
mov ah, 0x0e
mov bx, 7
int 0x10
mov al, 0x21
int 0x10
xor ax, ax
int 0x16
int 0x19
xor cx, cx
mov bp, si
; chain into partition boot sector
jmp 0:bs
dap:
.size:
.size: db 0
db 0
db 0
.count:
dw 1
.buffer:
dw prog
.count: dw 1
.buf: dw bs
dw 0
.blocknum:
dq 0
.num: dq 0
times (0x1BE - ($-$$)) db 0
part1:
db 0x80
.chs_start:
times (0x1BE - ($-$$)) db 0
part1: db 0x80
db 0xFF, 0xFF, 0xFF
.type:
db 0x01
.chs_end:
db 0xFF, 0xFF, 0xFF
.begin:
dd 1
.end:
dd (FLOPPY * 2)
times (0x1FE - ($-$$)) db 0
dw 0xAA55
times (0x1FE - ($-$$)) db 0
; Boot signature
dw 0xAA55