rdos/boot/floppy.asm

199 lines
3.8 KiB
NASM
Raw Normal View History

org 0x0600
2019-03-25 09:51:37 +01:00
2019-03-27 23:05:00 +01:00
fdc: ; FDC Descriptor as per ECMA-107
jmp _startup
times (0x03 - ($-$$)) nop
2019-03-25 09:51:37 +01:00
.oem:
2019-03-27 23:05:00 +01:00
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
2019-03-28 00:06:13 +01:00
.ts:
dw 720 ; total number of sectors
2019-03-27 23:05:00 +01:00
.mi: ; medium identifier
2019-03-25 09:51:37 +01:00
db 0xFD ; 5.25-inch Double sided, 40 tracks per side, 9 sectors per track (360 KB)
2019-03-27 23:05:00 +01:00
.sf: ; sectors per fat
2019-03-25 09:51:37 +01:00
dw 0
.spt:
dw 9 ; sectors per track
2019-03-27 23:05:00 +01:00
.nos:
dw 2 ; number of sides (heads)
.po:
dd 0 ; partition offset (in LBA blocks)
2019-03-28 00:06:13 +01:00
.lrgts:
dw 0
2019-03-27 23:05:00 +01:00
.drv:
2019-03-26 21:46:40 +01:00
db 0 ; drive number
db 0
2019-03-27 23:05:00 +01:00
db 0x29 ; efdc signature
.vid:
dd 0 ; volume id
.vlabel:
db "2B"
times (54 - ($-$$)) db " "
.fstype:
db "FAT12"
times (62 - ($-$$)) db " "
2019-03-25 09:51:37 +01:00
_startup:
2019-03-28 00:05:46 +01:00
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
xor sp, sp
2019-03-31 14:44:50 +02:00
; relocate
2019-03-28 00:05:46 +01:00
mov si, 0x7C00
mov di, 0x0600
2019-03-31 14:44:50 +02:00
mov cx, 0x0100
rep movsw
; adjust CS
jmp 0x0000:main
2019-03-28 00:05:46 +01:00
main:
2019-03-29 22:40:44 +01:00
mov [fdc.drv], dl ; backup drive number
2019-03-29 23:32:50 +01:00
call fix_chs
call find_first_sector
call error
db "END OF IMPLEMENTATION", 0
; Write AX, a string and bail out
; Return pointer is the string start
; String must be 0 suffixed
error:
call print16
pop si
mov bx, 0x0000
mov al, 0x20
.loop:
mov ah, 0x0e
int 0x10
lodsb
cmp al, 0x00
jne .loop
.hlt:
hlt
jmp .hlt
; Read CHS data from BIOS and fix fdc values
fix_chs:
2019-03-27 23:05:00 +01:00
mov ah, 0x08
2019-03-31 14:44:50 +02:00
int 0x13
jc .end ; skip if function does not exist
2019-03-27 23:05:00 +01:00
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
.end:
ret
2019-03-29 22:40:44 +01:00
; Load the root directory [buffer]
find_first_sector:
mov bx, [fdc.ss] ; bytes per sector
2019-03-28 00:06:13 +01:00
mov cl, 5
shr bx, cl ; div by 2^5, 32 bytes per directory entity
mov ax, [fdc.rde] ; number of root directory entities
2019-03-28 00:06:13 +01:00
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
.blkloop:
2019-03-28 00:06:13 +01:00
call loadblk
add bx, buffer
add ax, 1
loop .blkloop
mov cx, [fdc.rde]
mov bx, buffer
.dirloop:
push cx
mov cx, 0x000B
mov si, bx
mov di, bootfilename
repe cmpsb
2019-03-28 00:06:13 +01:00
pop cx
jz .found
add bx, 0x0020 ; 32 bytes per directory entry
loop .dirloop
2019-03-31 14:44:50 +02:00
call error
db "NOT FOUND", 0
.found:
add bx, 26
mov ax, [bx]
ret
2019-03-28 00:06:13 +01:00
2019-03-31 14:44:50 +02:00
; Load a single FAT cluster into memory
; in ax cluster number
; bx buffer
2019-03-31 14:44:50 +02:00
;
loadsec:
call print16
mov ax, bx
call print16
2019-03-31 14:44:50 +02:00
ret
2019-03-28 00:06:13 +01:00
; Load a single block into memory
2019-03-31 14:44:50 +02:00
; Does not return on error
2019-03-27 23:05:00 +01:00
; in ax sector number
; bx buffer
2019-03-27 23:05:00 +01:00
loadblk:
2019-03-28 00:06:13 +01:00
push ax
push cx
push dx
2019-03-27 23:05:00 +01:00
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
2019-03-29 23:32:50 +01:00
jc .error
2019-03-28 00:06:13 +01:00
pop dx
pop cx
pop ax
2019-03-27 23:05:00 +01:00
ret
2019-03-29 23:32:50 +01:00
.error:
xchg al, ah
2019-03-31 14:44:50 +02:00
call error
db "DISK ERROR", 0
2019-03-26 21:46:40 +01:00
2019-03-25 09:51:37 +01:00
%include "print.asm"
2019-03-29 22:40:44 +01:00
times (0x1F0 - ($-$$)) db 0
bootfilename:
2019-03-29 22:40:44 +01:00
db "BOOT"
times (0x1F8 - ($-$$)) db " "
db "BIN"
times (0x1FE - ($-$$)) db 0
2019-03-25 09:51:37 +01:00
dw 0xAA55
buffer: