202 lines
3.7 KiB
NASM
202 lines
3.7 KiB
NASM
org 0x0500
|
|
|
|
fdc: ; FDC Descriptor as per ECMA-107
|
|
jmp _startup
|
|
times (0x03 - ($-$$)) nop
|
|
.oem:
|
|
db "2B" ; creating system identifier
|
|
times (0x0B - ($-$$)) db " " ; space-padded
|
|
.ss:
|
|
dw 0x200 ; sector size
|
|
.sc:
|
|
db 0 ; sectors per cluster
|
|
.rsc:
|
|
dw 2 ; reserved sector count
|
|
.fn:
|
|
db 0 ; number of file allocation tables
|
|
.rde:
|
|
dw 0 ; number of root directory entries
|
|
.ts:
|
|
dw 720 ; total number of sectors
|
|
.mi: ; medium identifier
|
|
db 0xFD ; 5.25-inch Double sided, 40 tracks per side, 9 sectors per track (360 KB)
|
|
.sf: ; sectors per fat
|
|
dw 0
|
|
.spt:
|
|
dw 9 ; sectors per track
|
|
.nos:
|
|
dw 2 ; number of sides (heads)
|
|
.po:
|
|
dd 0 ; partition offset (in LBA blocks)
|
|
.lrgts:
|
|
dw 0
|
|
.drv:
|
|
db 0 ; drive number
|
|
db 0
|
|
db 0x29 ; efdc signature
|
|
.vid:
|
|
dd 0 ; volume id
|
|
.vlabel:
|
|
db "2B"
|
|
times (54 - ($-$$)) db " "
|
|
.fstype:
|
|
db "FAT12"
|
|
times (62 - ($-$$)) db " "
|
|
|
|
_startup:
|
|
xor ax, ax
|
|
mov ds, ax
|
|
mov es, ax
|
|
mov ss, ax
|
|
xor sp, sp
|
|
mov cx, 0x0100
|
|
mov si, 0x7C00
|
|
mov di, 0x0500
|
|
rep movsw ; relocate
|
|
jmp 0x0000:main
|
|
|
|
main:
|
|
mov [fdc.drv], dl ; backup drive number
|
|
|
|
adjustchs:
|
|
mov ah, 0x08
|
|
int 0x13 ; required, QEMU detects 360kB as 1200kB with CHS 80 2 15
|
|
jc loaddir ; skip if function does not exist
|
|
inc dh
|
|
mov [fdc.nos], dh
|
|
mov ax, cx
|
|
and ax, 0x003F
|
|
mov [fdc.spt], ax ; no adjustment because sectors are 1-indexed
|
|
mov ax, cx
|
|
xchg al, ah
|
|
mov cl,6
|
|
shr ah,cl
|
|
inc ax ; convert from maximum number (0-based) to total number (1-based) of cylinders
|
|
mul word [fdc.nos] ; number of tracks = number of cylinders * heads
|
|
mul word [fdc.spt] ; number of sectors = number of tracks * sectors per track
|
|
mov [fdc.ts], ax
|
|
|
|
loaddir:
|
|
mov bx, [fdc.ss] ; bytes per sector
|
|
mov cl, 5
|
|
shr bx, cl ; div by 2^5, 32 bytes per directory entity
|
|
mov ax, [fdc.rde] ; number of root directory entities
|
|
xor dx, dx
|
|
div bx
|
|
mov cx, ax ; cx = number of rootdir sectors
|
|
mov ax, [fdc.sf] ; sectors per fat
|
|
mul byte [fdc.fn] ; number of fats
|
|
add ax, [fdc.rsc] ; ax = starting sector of rootdir
|
|
mov bx, buffer
|
|
.loop:
|
|
call loadblk
|
|
add bx, buffer
|
|
add ax, 1
|
|
loop .loop
|
|
|
|
scandir:
|
|
mov cx, [fdc.rde]
|
|
mov bx, buffer
|
|
.loop:
|
|
push cx
|
|
mov cx, 0x000B
|
|
mov si, bx
|
|
mov di, bootfilename
|
|
repe cmpsb
|
|
pop cx
|
|
jz found
|
|
add bx, 0x0020 ; 32 bytes per directory entry
|
|
loop .loop
|
|
call print_inline
|
|
db "File not found", 0x0A, 0x0D, 0
|
|
jmp hlt
|
|
found:
|
|
add bx, 26
|
|
mov ax, [bx]
|
|
call print16
|
|
jmp hlt
|
|
|
|
hlt:
|
|
hlt
|
|
jmp hlt
|
|
|
|
; Load a single block into memory
|
|
; in ax sector number
|
|
; bx buffer
|
|
; out al error code,
|
|
; carry set if error
|
|
loadblk:
|
|
push ax
|
|
push cx
|
|
push dx
|
|
xor dx, dx
|
|
div word [fdc.spt] ; ax:temp = (lba / spt)
|
|
inc dx ; dx:sector = (lba % spt) + 1
|
|
mov cl, dl ; sector number
|
|
xor dx, dx
|
|
div word [fdc.nos] ; ax:cylinder = (tmp / heads)
|
|
; dx:head = (tmp % heads)
|
|
mov ch, al ; cylinder number
|
|
mov dh, dl ; head number
|
|
mov dl, [fdc.drv] ; driver number
|
|
mov ax, 0x0201 ; ah=0x02 al=0x01
|
|
int 0x13
|
|
jc .error
|
|
pop dx
|
|
pop cx
|
|
pop ax
|
|
ret
|
|
.error:
|
|
xchg al, ah
|
|
call print_inline
|
|
db "disk error ", 0
|
|
call print8
|
|
call print_inline
|
|
db 0x0A, 0x0D, 0
|
|
jmp hlt
|
|
|
|
; in si
|
|
dump:
|
|
mov si, bx
|
|
mov cx, 0x10
|
|
.nextline:
|
|
mov ax, si
|
|
call print16
|
|
|
|
push si
|
|
call print_inline
|
|
db 0x3A, 0x20, 0
|
|
pop si
|
|
|
|
push cx
|
|
mov cx, 0x0020
|
|
.bytes:
|
|
lodsb
|
|
call print8
|
|
loop .bytes
|
|
pop cx
|
|
|
|
push si
|
|
call print_inline
|
|
db 0x0A, 0x0D, 0
|
|
pop si
|
|
|
|
loop .nextline
|
|
|
|
ret
|
|
|
|
%include "print.asm"
|
|
|
|
times (0x1F0 - ($-$$)) db 0
|
|
|
|
bootfilename:
|
|
db "BOOT"
|
|
times (0x1F8 - ($-$$)) db " "
|
|
db "BIN"
|
|
|
|
times (0x1FE - ($-$$)) db 0
|
|
|
|
dw 0xAA55
|
|
|
|
buffer:
|