Make kernel and fat.bs use BPB struct

This commit is contained in:
Nero 2020-09-15 21:38:26 +02:00
parent 303b1756e0
commit 9cd4c04c42
3 changed files with 247 additions and 336 deletions

View File

@ -1,4 +1,4 @@
FLOPPY = 360
FLOPPY = 1440
PROGRAMS = hello.com
DISTFILES = $(PROGRAMS) kernel.bs
@ -55,15 +55,6 @@ fat4.bs: boot/fat.asm
fat6.bs: boot/fat.asm
$(NASM) $(NASM_ARGS) -DFAT16 -DCHS -DLARGE -o $@ $<
fatb.bs: boot/fat.asm
$(NASM) $(NASM_ARGS) -DFAT32 -DCHS -DLARGE -o $@ $<
fatc.bs: boot/fat.asm
$(NASM) $(NASM_ARGS) -DFAT32 -DLBA -DLARGE -o $@ $<
fate.bs: boot/fat.asm
$(NASM) $(NASM_ARGS) -DFAT16 -DLBA -DLARGE -o $@ $<
# BIOS option roms
%.rom: rom/%.asm utils/fix-rom
$(NASM) $(NASM_ARGS) -o $@ $< && utils/fix-rom $@

View File

@ -1,11 +1,7 @@
; Bootsector for FAT32 filesystems
; Bootsector for FAT12/FAT16 filesystems
; Takes defines:
; FAT12 FAT has 1.5 bytes per entry
; FAT16 FAT has WORD per entry
; FAT32 FAT has DWORD per entry
; CHS uses int13h/AH=02h
; LARGE requires 32 bit sector numbers
; LBA uses int13h/AH=42h
cpu 8086
org 0x0800
@ -14,14 +10,12 @@
%include "inc/bpb.asm"
bpb: equ 0x080B
times ( 0x0B - ($-$$) ) db 0
params: times bpb_size db 0
; Area for BPB
%ifdef FAT32
times ( 0x5A - ($-$$) ) db 0
%else
times ( 0x3E - ($-$$) ) db 0
%endif
init: xor ax, ax
mov ss, ax
@ -45,7 +39,7 @@ init: xor ax, ax
; offsets relative to FS
readd: ; add offset of cluster data area to DX:AX
push cx
mov di, [bp+bpb_rde]
mov di, [bp+bpb.direntries]
mov cl, 4
shr di, cl ; 32 bytes per entry
add ax, di
@ -54,60 +48,20 @@ readd: ; add offset of cluster data area to DX:AX
readr: ; add offset to rootdir to DX:AX (FAT12/FAT16 only)
push cx
xor ch, ch
mov cl, byte [bp+bpb_fn]
fatlp:
%ifdef FAT32
add ax, [bp+bpb7_lsf]
adc dx, [bp+bpb7_lsf+2]
%else
add ax, [bp+bpb_sf]
mov cl, byte [bp+bpb.fatnumber]
fatlp: add ax, [bp+bpb.fatsectors]
adc dx, 0
%endif
loop fatlp
pop cx
readf: ; add offset to FAT table to DX:AX
add ax, word [bp+bpb_rsc]
add ax, word [bp+bpb.reservedsects]
adc dx, 0
readp: ; read sector DX:AX from partition
add ax, word [bp+bpb_po]
adc dx, word [bp+bpb_po+2]
add ax, word [bp+bpb.sectoroffset]
adc dx, word [bp+bpb.sectoroffset+2]
jnc ($+5)
call errcll
%ifdef LBA
read_: ; read sector DX:AX from disk
; this uses the EBIOS extensions
; qword sector number DX:AX
push cs
push cs
push dx
push ax
; dword target buffer: BX:0
push bx
push cs
; word sector number
push cx
; size & passing
mov di, 0x10
push di
mov si, sp
mov ah, 0x42
mov dl, [bp+bpb7_dn]
stc
int 0x13
jnc ($+5)
call errcll
add sp, di
ret
%endif
%ifdef CHS
read_: ; read sector DX:AX from disk
; uses basic CHS disk IO
; this cant reliable read over track boundaries
@ -118,15 +72,15 @@ chslp: push ax
push bx
; calculate CHS data
div word [bp+bpb_spt] ; ax:temp = (lba / spt)
div word [bp+bpb.tracksectors] ; ax:temp = (lba / spt)
inc dx ; dx:sector = (lba % spt) + 1
mov cl, dl ; sector number
xor dx, dx
div word [bp+bpb_nos] ; ax:cylinder = (tmp / heads)
div word [bp+bpb.heads] ; ax:cylinder = (tmp / heads)
; dx:head = (tmp % heads)
mov ch, al ; cylinder number
mov dh, dl ; head number
mov dl, [bp+bpb7_dn] ; drive number
mov dl, [bp+bpb.drivenum] ; drive number
; set up read buffer
mov es, bx
@ -150,7 +104,6 @@ chslp: push ax
; loop on CX
loop chslp
ret
%endif
next: ; Advances DX:AX to next FAT entry
push ax
@ -205,18 +158,6 @@ shftl: clc
xor dx, dx
%endif
%ifdef FAT32
; multiply with 4
sal si, 1
sal si, 1
; make sure its within sector range
and si, 0x1FF
add si, buf
; load dword
lodsw
mov dx, [si]
%endif
ret
; reads current cluster into [dest]
@ -230,7 +171,7 @@ readc: ; load cluster number
; convert cluster number to sector number
; this is some cheapo multiplication with 2^n
mov cl, [bp+bpb_sc]
mov cl, [bp+bpb.clustersects]
l02: shr cl, 1
jz l02e
clc
@ -239,7 +180,7 @@ l02: shr cl, 1
jmp l02
l02e:
; dx:ax is now sector num in data area
mov cx, [bp+bpb_sc]
mov cx, [bp+bpb.clustersects]
xor ch, ch
call readd
@ -255,13 +196,7 @@ l02e:
ret
; Load root directory
loadr:
%ifdef FAT32
mov ax, 2
xor dx, dx
jmp load
%else
mov ax, [bp+bpb_rde]
loadr: mov ax, [bp+bpb.direntries]
mov cl, 4
shr ax, cl
mov cx, ax
@ -269,7 +204,6 @@ loadr:
xor dx, dx
mov bx, 0x07C0
jmp readr
%endif
; Load file identified by fsattr
; Directory data is expected at 0x07C00
@ -302,20 +236,18 @@ lloop: call readc
%ifdef FAT16
cmp word ax, 0xFFF8
%endif
%ifdef FAT32
cmp word dx, 0x0FFF
%endif
jc lloop
ret
main: ; Set up BP to point to BPB
; Relative addressing saves one instruction byte per access
mov bp, bpb
mov bp, params
; Save DL contents
mov [bp+bpb7_dn], dl
mov [bp+bpb.drivenum], dl
; load root directory
call loadr
; search for first system directory
mov ah, 0x14
call loadf
@ -324,7 +256,7 @@ main: ; Set up BP to point to BPB
call loadf
; restore drive number
mov dl, [bp+bpb7_dn]
mov dl, [bp+bpb.drivenum]
; restore potential partition table
pop si

View File

@ -1,29 +1,17 @@
; DOS 3.31 Bios Parameter Block
bpb_ss: equ 0x00 ; WORD sector size
bpb_sc: equ 0x02 ; BYTE sector / cluster
bpb_rsc: equ 0x03 ; WORD reserved sector count
bpb_fn: equ 0x05 ; BYTE fat number
bpb_rde: equ 0x06 ; WORD root dir entries
bpb_ts: equ 0x08 ; WORD total sectors
bpb_md: equ 0x0A ; BYTE media descriptor
bpb_sf: equ 0x0B ; WORD sectors / fat
bpb_spt: equ 0x0D ; WORD sectors per track
bpb_nos: equ 0x0F ; WORD number of sides
bpb_po: equ 0x11 ; DWORD partition offset
bpb_lts: equ 0x15 ; DWORD large total sectors
; DOS 4.0 Extended Bios Parameter Block
bpb4_dn: equ 0x19 ; BYTE drive number
bpb4_sig: equ 0x1B ; BYTE 29h
bpb4_ser: equ 0x1C ; DWORD serial
; DOS 7.1 Extended Bios Parameter Block
bpb7_lsf: equ 0x19 ; DWORD large sectors / fat
bpb7_rdc: equ 0x21 ; DWORD root directory cluster
bpb7_fsc: equ 0x25 ; WORD fsinfo sector
bpb7_bsc: equ 0x27 ; WORD backup boot sector
bpb7_dn: equ 0x35 ; BYTE drive number
bpb7_sig: equ 0x37 ; BYTE 28h or 29h
bpb7_ser: equ 0x38 ; DWORD serial
bpb_len: equ 0x4F
struc bpb
.sectorsize: resw 1 ; WORD sector size
.clustersects: resb 1 ; BYTE sector / cluster
.reservedsects: resw 1 ; WORD reserved sector count
.fatnumber: resb 1 ; BYTE fat number
.direntries: resw 1 ; WORD root dir entries
.total: resw 1 ; WORD total sectors
.mediadesc: resb 1 ; BYTE media descriptor
.fatsectors: resw 1 ; WORD sectors / fat
.tracksectors: resw 1 ; WORD sectors per track
.heads: resw 1 ; WORD number of sides
.sectoroffset: resd 1 ; DWORD partition offset
.longtotal: resd 1 ; DWORD large total sectors
.drivenum: resb 1 ; BYTE drive number
.signature: resb 1 ; BYTE 28h
.serial: resd 1 ; DWORD serial
endstruc