2019-09-08 03:04:01 +02:00
|
|
|
; Memory layout:
|
2019-09-16 14:05:15 +02:00
|
|
|
%define self 0x00600 ; 1 sector
|
2019-09-15 19:26:09 +02:00
|
|
|
%define fattab 0x01000 ; variable size
|
|
|
|
%define rootdir 0x06000 ; variable size
|
|
|
|
%define psp (prog - 0x100)
|
|
|
|
%define prog 0x07C00
|
|
|
|
|
|
|
|
%define arguments (psp + 0x81)
|
2019-05-19 22:07:08 +02:00
|
|
|
|
2019-09-14 15:17:44 +02:00
|
|
|
org self
|
2019-09-08 03:04:01 +02:00
|
|
|
|
2019-09-15 19:26:09 +02:00
|
|
|
jmp short init
|
|
|
|
|
|
|
|
cluster_offset:
|
|
|
|
dw 0
|
|
|
|
|
|
|
|
times (0x0B - ($-$$)) db 0
|
|
|
|
|
2019-05-19 22:07:08 +02:00
|
|
|
fdc:
|
2019-03-27 23:05:00 +01:00
|
|
|
.ss:
|
|
|
|
dw 0x200 ; sector size
|
|
|
|
.sc:
|
2019-03-31 22:53:14 +02:00
|
|
|
db 2 ; sectors per cluster
|
2019-03-27 23:05:00 +01:00
|
|
|
.rsc:
|
2019-03-31 22:53:14 +02:00
|
|
|
dw 1 ; reserved sector count
|
2019-03-27 23:05:00 +01:00
|
|
|
.fn:
|
2019-03-31 22:53:14 +02:00
|
|
|
db 2 ; number of file allocation tables
|
2019-03-27 23:05:00 +01:00
|
|
|
.rde:
|
2019-03-31 22:53:14 +02:00
|
|
|
dw 0x70 ; 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-31 22:53:14 +02:00
|
|
|
dw 2
|
2019-03-25 09:51:37 +01:00
|
|
|
.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:
|
2019-05-07 19:50:07 +02:00
|
|
|
dd 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
|
|
|
|
2019-09-23 23:19:12 +02:00
|
|
|
; mformat keeps writing until here
|
|
|
|
; if we place init earlier, code gets overwritten
|
|
|
|
times (62 - ($-$$)) nop
|
|
|
|
|
2019-09-15 19:26:09 +02:00
|
|
|
init:
|
2019-09-14 15:17:44 +02:00
|
|
|
cli
|
|
|
|
|
|
|
|
; Stack grows down from PSP + 64k
|
|
|
|
mov ax, (psp >> 4)
|
|
|
|
mov ss, ax
|
|
|
|
xor sp, sp
|
2019-09-15 21:49:58 +02:00
|
|
|
push sp
|
2019-09-14 15:17:44 +02:00
|
|
|
|
|
|
|
; Relocate from [prog] to [self]
|
|
|
|
xor ax, ax
|
|
|
|
mov ds, ax
|
|
|
|
mov es, ax
|
|
|
|
mov si, prog
|
|
|
|
mov di, self
|
|
|
|
mov cx, 0x100
|
|
|
|
rep movsw
|
|
|
|
|
2019-09-15 19:26:09 +02:00
|
|
|
jmp 0:main
|
2019-09-14 15:17:44 +02:00
|
|
|
|
2019-09-15 19:26:09 +02:00
|
|
|
main:
|
2019-09-14 15:17:44 +02:00
|
|
|
mov [fdc.drv], dl ; save drive number in fd
|
|
|
|
sti
|
|
|
|
|
|
|
|
; load fat table into memory
|
|
|
|
mov ax, [fdc.rsc]
|
|
|
|
mov cx, [fdc.sf]
|
|
|
|
xor dx, dx
|
|
|
|
mov bx, fattab
|
|
|
|
call load_sectors
|
|
|
|
|
|
|
|
; calculate length of rootdir
|
|
|
|
mov ax, [fdc.rde]
|
|
|
|
mov cl, 4
|
|
|
|
shr ax, cl ; 32 bytes per entry
|
|
|
|
mov cx, ax
|
|
|
|
|
|
|
|
; load root dir
|
|
|
|
xor dx, dx
|
|
|
|
mov ax, [fdc.sf]
|
|
|
|
mul byte [fdc.fn]
|
|
|
|
add ax, [fdc.rsc]
|
|
|
|
mov bx, rootdir
|
|
|
|
call load_sectors
|
|
|
|
|
|
|
|
; remember where we left off
|
|
|
|
; clusters start after rootdir
|
|
|
|
mov [cluster_offset], ax
|
|
|
|
|
|
|
|
call load_file
|
2019-09-15 19:26:09 +02:00
|
|
|
mov bp, 0x3332
|
2019-09-14 15:17:44 +02:00
|
|
|
jc error
|
|
|
|
|
2019-09-15 21:49:58 +02:00
|
|
|
; setup int 19h call if prog returns
|
2019-09-14 15:17:44 +02:00
|
|
|
mov WORD [psp], 0x19CD
|
|
|
|
|
2019-09-15 21:49:58 +02:00
|
|
|
; clean registers
|
2019-09-14 15:17:44 +02:00
|
|
|
xor ax, ax
|
|
|
|
xor cx, cx
|
|
|
|
xor dx, dx
|
|
|
|
xor bx, bx
|
2019-09-15 21:49:58 +02:00
|
|
|
|
|
|
|
; restore boot drive number
|
2019-09-14 15:17:44 +02:00
|
|
|
mov dl, [fdc.drv]
|
2019-09-15 21:49:58 +02:00
|
|
|
|
|
|
|
; setup segment registers
|
|
|
|
push ss
|
|
|
|
pop ds
|
|
|
|
push ss
|
|
|
|
pop es
|
|
|
|
|
|
|
|
; jump into target
|
2019-09-14 15:17:44 +02:00
|
|
|
jmp 0x07B0:0x0100
|
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
; Read sectors from disk
|
|
|
|
; Does not return on error
|
|
|
|
; ax and bx will be incremented, cx decremented
|
|
|
|
; in dx:ax sector number
|
|
|
|
; es:bx buffer
|
|
|
|
; cx number of sectors to read
|
|
|
|
; out dx:ax next sector to read
|
|
|
|
; es:bx next free buffer
|
|
|
|
; cx zero
|
|
|
|
load_sectors:
|
|
|
|
; fail instantly if reading sectors > 16 bit
|
|
|
|
test dx, dx
|
2019-09-15 19:26:09 +02:00
|
|
|
mov bp, 0x3330
|
2019-09-08 03:04:01 +02:00
|
|
|
jnz error
|
|
|
|
|
|
|
|
.loop:
|
|
|
|
push ax
|
2019-04-20 23:02:35 +02:00
|
|
|
push cx
|
|
|
|
push dx
|
|
|
|
|
2019-09-18 00:41:39 +02:00
|
|
|
; add partition offset (required for HDD)
|
|
|
|
add ax, [fdc.po]
|
|
|
|
adc dx, [fdc.po+2]
|
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
; calculate CHS data
|
|
|
|
div word [cs:fdc.spt] ; ax:temp = (lba / spt)
|
|
|
|
inc dx ; dx:sector = (lba % spt) + 1
|
|
|
|
mov cl, dl ; sector number
|
|
|
|
xor dx, dx
|
|
|
|
div word [cs:fdc.nos] ; ax:cylinder = (tmp / heads)
|
|
|
|
; dx:head = (tmp % heads)
|
|
|
|
mov ch, al ; cylinder number
|
|
|
|
mov dh, dl ; head number
|
|
|
|
mov dl, [cs:fdc.drv] ; driver number
|
|
|
|
mov ax, 0x0201 ; ah=0x02 al=0x01
|
|
|
|
int 0x13
|
2019-09-15 19:26:09 +02:00
|
|
|
mov bp, 0x3331
|
2019-09-08 03:04:01 +02:00
|
|
|
jc error
|
2019-03-31 22:53:14 +02:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
pop dx
|
|
|
|
pop cx
|
|
|
|
pop ax
|
2019-03-29 23:32:50 +01:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
; count up for next sector
|
|
|
|
add bx, 0x0200
|
|
|
|
inc ax
|
2019-03-31 15:08:39 +02:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
loop .loop
|
|
|
|
ret
|
2019-05-19 22:07:08 +02:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
; Load the file in [filename]
|
|
|
|
; or exit with carry set
|
|
|
|
load_file:
|
2019-09-14 15:17:44 +02:00
|
|
|
mov si, rootdir
|
2019-05-19 22:07:08 +02:00
|
|
|
mov cx, [fdc.rde]
|
2019-09-08 03:04:01 +02:00
|
|
|
.loop:
|
|
|
|
call file_match
|
|
|
|
je read_clusters
|
2019-05-19 22:07:08 +02:00
|
|
|
add si, 0x20
|
2019-09-08 03:04:01 +02:00
|
|
|
loop .loop
|
2019-09-15 19:26:09 +02:00
|
|
|
stc
|
2019-09-08 03:04:01 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
; Compares the FAT dirent against [filename]
|
|
|
|
; in ds:si filename
|
|
|
|
; out ZF zero flag set if equal
|
|
|
|
file_match:
|
2019-05-19 22:07:08 +02:00
|
|
|
push cx
|
2019-09-08 03:04:01 +02:00
|
|
|
push si
|
|
|
|
mov di, filename
|
|
|
|
mov cx, 11
|
|
|
|
repe cmpsb
|
2019-05-19 22:07:08 +02:00
|
|
|
pop si
|
2019-09-08 03:04:01 +02:00
|
|
|
pop cx
|
2019-05-19 22:07:08 +02:00
|
|
|
ret
|
2019-03-31 15:08:39 +02:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
; Read the file given by cluster number
|
|
|
|
; into the target program area
|
2019-05-19 22:07:08 +02:00
|
|
|
; in ax cluster number
|
2019-09-08 03:04:01 +02:00
|
|
|
read_clusters:
|
|
|
|
add si, 0x1A
|
|
|
|
lodsw
|
2019-09-14 15:17:44 +02:00
|
|
|
mov bx, prog
|
2019-09-08 03:04:01 +02:00
|
|
|
.loop:
|
|
|
|
; read cluster into area for target file
|
|
|
|
push ax
|
|
|
|
sub ax, 2
|
|
|
|
mul BYTE [fdc.sc]
|
|
|
|
add ax, [cluster_offset]
|
|
|
|
xor dx, dx
|
|
|
|
mov cx, [fdc.sc]
|
|
|
|
xor ch, ch
|
|
|
|
call load_sectors
|
|
|
|
pop ax
|
|
|
|
|
|
|
|
; calculate index in FAT
|
|
|
|
mov si, ax
|
|
|
|
shr si, 1
|
|
|
|
add si, ax
|
2019-09-14 15:17:44 +02:00
|
|
|
add si, fattab
|
2019-09-08 03:04:01 +02:00
|
|
|
|
|
|
|
; load entry from FAT, truncate to 12 bit
|
|
|
|
mov dx, [si]
|
|
|
|
test ax, 1
|
|
|
|
jz .noshift
|
|
|
|
mov cl, 4
|
|
|
|
shr dx, 4
|
|
|
|
.noshift:
|
|
|
|
mov ax, dx
|
|
|
|
|
|
|
|
and ax, 0x0FFF
|
|
|
|
cmp ax, 0x0FF8
|
|
|
|
jc .loop
|
|
|
|
|
2019-05-19 22:07:08 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
error:
|
2019-09-08 03:04:01 +02:00
|
|
|
mov ax, bp
|
2019-03-31 15:08:39 +02:00
|
|
|
mov ah, 0x0e
|
2019-09-08 03:04:01 +02:00
|
|
|
mov bx, 7
|
2019-03-31 15:08:39 +02:00
|
|
|
int 0x10
|
2019-09-08 03:04:01 +02:00
|
|
|
mov al, 0x21
|
|
|
|
int 0x10
|
|
|
|
xor ax, ax
|
|
|
|
int 0x16
|
|
|
|
int 0x19
|
|
|
|
|
2019-09-23 22:25:39 +02:00
|
|
|
filename:
|
|
|
|
db "KERNEL COM"
|
2019-03-29 22:40:44 +01:00
|
|
|
|
2019-09-08 03:04:01 +02:00
|
|
|
times (0x1FE - ($-$$)) db 0
|
2019-03-29 22:40:44 +01:00
|
|
|
|
2019-03-25 09:51:37 +01:00
|
|
|
dw 0xAA55
|