Start working on FAT32 bootloader

This commit is contained in:
Nero 2020-03-16 17:24:39 +00:00
parent 65be1051b3
commit 660944e70f
2 changed files with 136 additions and 7 deletions

View File

@ -9,7 +9,7 @@ QEMU = qemu-system-$(QEMU_ARCH)
QEMU_ARGS = $(addprefix --option-rom ,$(ROMS)) QEMU_ARGS = $(addprefix --option-rom ,$(ROMS))
NASM = nasm NASM = nasm
NASM_ARGS = -s -I. -Ilib -w-macro-params NASM_ARGS = -s -I. -Ilib -w-macro-params -DFLOPPY=$(FLOPPY)
EMUL = utils/emul EMUL = utils/emul
@ -62,8 +62,12 @@ fdimage.img: fat.bs $(DISTFILES)
mformat -C -i $@ -f $(FLOPPY) -B fat.bs :: mformat -C -i $@ -f $(FLOPPY) -B fat.bs ::
mcopy -i $@ $(DISTFILES) :: mcopy -i $@ $(DISTFILES) ::
hdimage.img: boot/mbr.bs fdimage.img hdimage.img: mbr.bs fdimage.img
cat boot/mbr.bs fdimage.img >$@ cat mbr.bs fdimage.img >$@
part.img: fat32.bs
dd if=/dev/zero bs=1024 count=33k of=$@
mformat -F -i part.img -B fat32.bs ::
clean: clean:
rm -f *.com *.bs *.0 *.lst *.img *.bin *.rom rm -f *.com *.bs *.0 *.lst *.img *.bin *.rom
@ -72,11 +76,11 @@ clean:
qemu-floppy: fdimage.img $(ROMS) qemu-floppy: fdimage.img $(ROMS)
$(QEMU) $(QEMU_ARGS) -boot a -fda fdimage.img $(QEMU) $(QEMU_ARGS) -boot a -fda fdimage.img
qemu-hdd: hdimage.img $(ROMS) qemu-hdd: part.img $(ROMS)
$(QEMU) $(QEMU_ARGS) -boot c -hda hdimage.img $(QEMU) $(QEMU_ARGS) -boot c -hda part.img
qemu-pxe: pxe.bs $(ROMS) qemu-pxe: pxeboot.0 $(ROMS)
$(QEMU) $(QEMU_ARGS) -boot n \ $(QEMU) $(QEMU_ARGS) -boot n \
-option-rom /usr/share/qemu/pxe-rtl8139.rom \ -option-rom /usr/share/qemu/pxe-rtl8139.rom \
-device e1000,netdev=mynet0,mac=52:54:00:12:34:56 \ -device e1000,netdev=mynet0,mac=52:54:00:12:34:56 \
-netdev user,id=mynet0,net=192.168.76.0/24,dhcpstart=192.168.76.9,tftp=$(CURDIR),bootfile=pxe.bs -netdev user,id=mynet0,net=192.168.76.0/24,dhcpstart=192.168.76.9,tftp=$(CURDIR),bootfile=pxeboot.0

125
fat32.asm Normal file
View File

@ -0,0 +1,125 @@
; Bootsector for FAT32 filesystems
org 0x0800
jmp near init
; WORD reserved sector count
rsc: equ ( $$ + 0x00E )
; BYTE number of FATs
fn: equ ( $$ + 0x010 )
; DWORD hidden sector count (partition offset)
po: equ ( $$ + 0x01C )
; DWORD sectors per FAT
sf: equ ( $$ + 0x024 )
; BYTE drive number (we set it from dl)
dn: equ ( $$ + 0x040 )
; Area for BPB
times (0x5A - ($-$$)) db 0
init: xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
mov si, sp
mov di, 0x0800
mov cx, 0x0100
rep movsw
jmp 0x0:main
msg: pop si
mloop: lodsb
test al, al
jz mend
mov ah, 0x0e
mov bx, 7
int 0x10
jmp mloop
mend: push si
ret
err: call msg
db "DISKERR", 0x0A, 0x0D, 0
hlt: hlt
jmp hlt
; Requires DWORD ptr and QWORD sector on the stack
read: pop bp
push cx
mov ax, 0x10
push ax
mov ah, 0x42
mov dl, 0x80
mov si, sp
int 0x13
jc err
pop ax
pop cx
push bp
ret
; Advances cluster number to next entry
next: ; Upper two words for sector num
xor ax, ax
push ax
push ax
; Lower two words for sector num
mov ax, [clus]
mov dx, [clus+2]
; 32-bit >> 7
mov cl, 7
shftl: clc
rcr dx, 1
rcr ax, 1
loop shftl
; Add reserved sector count
add ax, word [rsc]
adc dx, 0
push dx
push ax
; target buffer
push cs
mov ax, buf
push ax
mov cx, 1
call read
; Stow off offset in FAT sector
mov si, [clus]
sal si, 1
sal si, 1
and si, 0x1FF
add si, buf
mov di, clus
movsw
movsw
; 1x QWORD, 1x DWORD = 12 bytes
add sp, 12
ret
main: call next
mov ax, [clus]
mov dx, [clus+2]
int 3
xor ah,ah
int 0x16
int 0x19
jmp hlt
clus: dd 2
; Padding and signature
times (0x1FE - ($-$$)) db 0
dw 0xAA55
buf: