rdos/fat32.asm

126 lines
1.7 KiB
NASM

; 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: