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 PROGRAMS = hello.com
DISTFILES = $(PROGRAMS) kernel.bs DISTFILES = $(PROGRAMS) kernel.bs
@ -55,15 +55,6 @@ fat4.bs: boot/fat.asm
fat6.bs: boot/fat.asm fat6.bs: boot/fat.asm
$(NASM) $(NASM_ARGS) -DFAT16 -DCHS -DLARGE -o $@ $< $(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 # BIOS option roms
%.rom: rom/%.asm utils/fix-rom %.rom: rom/%.asm utils/fix-rom
$(NASM) $(NASM_ARGS) -o $@ $< && 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: ; Takes defines:
; FAT12 FAT has 1.5 bytes per entry ; FAT12 FAT has 1.5 bytes per entry
; FAT16 FAT has WORD 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 cpu 8086
org 0x0800 org 0x0800
@ -14,14 +10,12 @@
%include "inc/bpb.asm" %include "inc/bpb.asm"
bpb: equ 0x080B times ( 0x0B - ($-$$) ) db 0
params: times bpb_size db 0
; Area for BPB ; Area for BPB
%ifdef FAT32
times ( 0x5A - ($-$$) ) db 0
%else
times ( 0x3E - ($-$$) ) db 0 times ( 0x3E - ($-$$) ) db 0
%endif
init: xor ax, ax init: xor ax, ax
mov ss, ax mov ss, ax
@ -45,7 +39,7 @@ init: xor ax, ax
; offsets relative to FS ; offsets relative to FS
readd: ; add offset of cluster data area to DX:AX readd: ; add offset of cluster data area to DX:AX
push cx push cx
mov di, [bp+bpb_rde] mov di, [bp+bpb.direntries]
mov cl, 4 mov cl, 4
shr di, cl ; 32 bytes per entry shr di, cl ; 32 bytes per entry
add ax, di 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) readr: ; add offset to rootdir to DX:AX (FAT12/FAT16 only)
push cx push cx
xor ch, ch xor ch, ch
mov cl, byte [bp+bpb_fn] mov cl, byte [bp+bpb.fatnumber]
fatlp: fatlp: add ax, [bp+bpb.fatsectors]
%ifdef FAT32
add ax, [bp+bpb7_lsf]
adc dx, [bp+bpb7_lsf+2]
%else
add ax, [bp+bpb_sf]
adc dx, 0 adc dx, 0
%endif
loop fatlp loop fatlp
pop cx pop cx
readf: ; add offset to FAT table to DX:AX readf: ; add offset to FAT table to DX:AX
add ax, word [bp+bpb_rsc] add ax, word [bp+bpb.reservedsects]
adc dx, 0 adc dx, 0
readp: ; read sector DX:AX from partition readp: ; read sector DX:AX from partition
add ax, word [bp+bpb_po] add ax, word [bp+bpb.sectoroffset]
adc dx, word [bp+bpb_po+2] adc dx, word [bp+bpb.sectoroffset+2]
jnc ($+5) jnc ($+5)
call errcll 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 read_: ; read sector DX:AX from disk
; uses basic CHS disk IO ; uses basic CHS disk IO
; this cant reliable read over track boundaries ; this cant reliable read over track boundaries
@ -118,15 +72,15 @@ chslp: push ax
push bx push bx
; calculate CHS data ; 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 inc dx ; dx:sector = (lba % spt) + 1
mov cl, dl ; sector number mov cl, dl ; sector number
xor dx, dx 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) ; dx:head = (tmp % heads)
mov ch, al ; cylinder number mov ch, al ; cylinder number
mov dh, dl ; head number mov dh, dl ; head number
mov dl, [bp+bpb7_dn] ; drive number mov dl, [bp+bpb.drivenum] ; drive number
; set up read buffer ; set up read buffer
mov es, bx mov es, bx
@ -150,7 +104,6 @@ chslp: push ax
; loop on CX ; loop on CX
loop chslp loop chslp
ret ret
%endif
next: ; Advances DX:AX to next FAT entry next: ; Advances DX:AX to next FAT entry
push ax push ax
@ -205,18 +158,6 @@ shftl: clc
xor dx, dx xor dx, dx
%endif %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 ret
; reads current cluster into [dest] ; reads current cluster into [dest]
@ -230,7 +171,7 @@ readc: ; load cluster number
; convert cluster number to sector number ; convert cluster number to sector number
; this is some cheapo multiplication with 2^n ; this is some cheapo multiplication with 2^n
mov cl, [bp+bpb_sc] mov cl, [bp+bpb.clustersects]
l02: shr cl, 1 l02: shr cl, 1
jz l02e jz l02e
clc clc
@ -239,7 +180,7 @@ l02: shr cl, 1
jmp l02 jmp l02
l02e: l02e:
; dx:ax is now sector num in data area ; dx:ax is now sector num in data area
mov cx, [bp+bpb_sc] mov cx, [bp+bpb.clustersects]
xor ch, ch xor ch, ch
call readd call readd
@ -255,13 +196,7 @@ l02e:
ret ret
; Load root directory ; Load root directory
loadr: loadr: mov ax, [bp+bpb.direntries]
%ifdef FAT32
mov ax, 2
xor dx, dx
jmp load
%else
mov ax, [bp+bpb_rde]
mov cl, 4 mov cl, 4
shr ax, cl shr ax, cl
mov cx, ax mov cx, ax
@ -269,7 +204,6 @@ loadr:
xor dx, dx xor dx, dx
mov bx, 0x07C0 mov bx, 0x07C0
jmp readr jmp readr
%endif
; Load file identified by fsattr ; Load file identified by fsattr
; Directory data is expected at 0x07C00 ; Directory data is expected at 0x07C00
@ -302,20 +236,18 @@ lloop: call readc
%ifdef FAT16 %ifdef FAT16
cmp word ax, 0xFFF8 cmp word ax, 0xFFF8
%endif %endif
%ifdef FAT32
cmp word dx, 0x0FFF
%endif
jc lloop jc lloop
ret ret
main: ; Set up BP to point to BPB main: ; Set up BP to point to BPB
; Relative addressing saves one instruction byte per access ; Relative addressing saves one instruction byte per access
mov bp, bpb mov bp, params
; Save DL contents ; Save DL contents
mov [bp+bpb7_dn], dl mov [bp+bpb.drivenum], dl
; load root directory ; load root directory
call loadr call loadr
; search for first system directory ; search for first system directory
mov ah, 0x14 mov ah, 0x14
call loadf call loadf
@ -324,7 +256,7 @@ main: ; Set up BP to point to BPB
call loadf call loadf
; restore drive number ; restore drive number
mov dl, [bp+bpb7_dn] mov dl, [bp+bpb.drivenum]
; restore potential partition table ; restore potential partition table
pop si pop si

View File

@ -1,29 +1,17 @@
; DOS 3.31 Bios Parameter Block struc bpb
bpb_ss: equ 0x00 ; WORD sector size .sectorsize: resw 1 ; WORD sector size
bpb_sc: equ 0x02 ; BYTE sector / cluster .clustersects: resb 1 ; BYTE sector / cluster
bpb_rsc: equ 0x03 ; WORD reserved sector count .reservedsects: resw 1 ; WORD reserved sector count
bpb_fn: equ 0x05 ; BYTE fat number .fatnumber: resb 1 ; BYTE fat number
bpb_rde: equ 0x06 ; WORD root dir entries .direntries: resw 1 ; WORD root dir entries
bpb_ts: equ 0x08 ; WORD total sectors .total: resw 1 ; WORD total sectors
bpb_md: equ 0x0A ; BYTE media descriptor .mediadesc: resb 1 ; BYTE media descriptor
bpb_sf: equ 0x0B ; WORD sectors / fat .fatsectors: resw 1 ; WORD sectors / fat
bpb_spt: equ 0x0D ; WORD sectors per track .tracksectors: resw 1 ; WORD sectors per track
bpb_nos: equ 0x0F ; WORD number of sides .heads: resw 1 ; WORD number of sides
bpb_po: equ 0x11 ; DWORD partition offset .sectoroffset: resd 1 ; DWORD partition offset
bpb_lts: equ 0x15 ; DWORD large total sectors .longtotal: resd 1 ; DWORD large total sectors
.drivenum: resb 1 ; BYTE drive number
; DOS 4.0 Extended Bios Parameter Block .signature: resb 1 ; BYTE 28h
bpb4_dn: equ 0x19 ; BYTE drive number .serial: resd 1 ; DWORD serial
bpb4_sig: equ 0x1B ; BYTE 29h endstruc
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