Set BPB data from the BIOS, properly support loading data from sectors >63, calculate CHS addressing properly. Calculate certain important values rather than harcoding them.

This commit is contained in:
Xnoe 2021-09-06 15:35:37 +01:00
parent 8f1e13e749
commit 26d231a7c9
Signed by: xnoe
GPG Key ID: 45AC398F44F0DAFE
2 changed files with 161 additions and 69 deletions

122
boot.asm
View File

@ -1,24 +1,28 @@
[BITS 16] ;tell the assembler that its a 16 bit code
[BITS 16]
jmp short bootcode
nop
bpOEMid db "XNOE "
bpBytesPerSector dw 512
bpSectorsPerCluster db 1
bpReservedSectors dw 1
bpNoFATs db 2
bpRootDirEntries dw 256
bpLVSectors dw 8419
bpMediumID db 0F0h
bpSectorsPerFat dw 9
bpLVSectors dw 8586
bpMediumID db 0xF8
bpSectorsPerFat dw 34
bpSectorsPerTrack dw 18
bpSides dw 2
bpHiddenSectors dd 0
bpLargeSectors dd 0
bpDriveNo dw 0
bpSignature db 41
bpVolumeID dd 00000000h
bpHeads dw 2
bpHiddenSectors dw 0
TIMES 36 - ($ - $$) db 0
bpDriveNo db 0
db 0
bpSignature db 0x29
bpVolumeID dd 0x0
bpVolumeLabel db "XNOE OS "
bpFileSystem db "FAT16 "
@ -26,7 +30,18 @@ bootcode:
mov ax, 7c0h
mov ds, ax
mov byte [drive], dl
mov byte [bpDriveNo], dl
; Get the disk configuration from the BIOS
mov ah, 08h
int 13h
add dh, 1
movzx dx, dh
mov word [bpHeads], dx
and cl, 03fh
movzx cx, cl
mov word [bpSectorsPerTrack], cx
mov si, boot_msg
call _boot_print
@ -35,12 +50,13 @@ bootcode:
mov es, ax
mov bx, buffer
mov ah, 2
mov al, 16
mov cl, 20
mov ch, 0
mov dh, 0
mov dl, byte [drive]
; Calculate position of rootdirentries
mov ax, word [bpSectorsPerFat]
movzx cx, byte [bpNoFATs]
mul cx
add ax, 2
call prep_i13
int 13h
@ -57,11 +73,6 @@ kernel_finder:
mov di, buffer
add di, ax
; mov si, di
; call _boot_print
; mov si, new_line
; call _boot_print
mov si, kernel_file
mov cx, 11
rep cmpsb
@ -78,6 +89,7 @@ kernel_finder:
jmp $
kernel_found:
; jmp $
mov ax, [es:di+0fh]
mov word [cluster], ax
@ -86,29 +98,41 @@ fat_loader:
mov es, ax
mov bx, buffer
mov ax, word [bpSectorsPerFat]
mov ah, 2
mov al, 9
mov cl, 2
mov ch, 0
mov dh, 0
mov dl, byte [drive]
mov dl, byte [bpDriveNo]
int 13h
kernel_loader:
; Calculate file offset
mov ax, word [bpSectorsPerFat]
movzx cx, byte [bpNoFATs]
mul cx
push ax
mov ax, word [bpRootDirEntries]
mov cx, 16
div cx
mov bx, ax
pop ax
add ax, bx
mov word [fileoffset], ax
mov ax, 2000h
mov es, ax
mov bx, word [pointer]
mov al, byte [cluster]
add al, 34
movzx ax, byte [cluster]
add ax, word [fileoffset]
mov cl, al
mov al, 1
mov ah, 2
mov ch, 0
mov dh, 0
mov dl, byte [drive]
call prep_i13
int 13h
@ -117,7 +141,6 @@ kernel_loader:
mov si, word [cluster]
shl si, 1
add si, buffer
; add si, 1
cmp word [si], 0ffffh
je kernel_loaded
@ -125,10 +148,6 @@ kernel_loader:
jmp kernel_loader
kernel_loaded:
; Pass boot device to the kernel
xor ax, ax
mov al, byte [drive]
push ax
jmp 2000h:0h
_boot_print:
@ -149,11 +168,34 @@ kernel_nf db "KERNEL.BIN Missing!", 13, 10, 0
new_line db " ", 0
kernel_file db "KERNEL BIN"
fileoffset dw 0
cluster dw 0
pointer dw 0
drive db 0
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0
DW 0xAA55 ; add boot signature at the end of bootloader
; AX set to the logical sector number.
prep_i13:
xor dx, dx
div word [bpSectorsPerTrack]
push dx
xor dx, dx
div word [bpHeads]
mov ch, al
mov dh, dl
mov dl, byte [bpDriveNo]
pop ax
mov cl, al
mov al, 1
mov ah, 2
ret
TIMES 510 - ($ - $$) db 0
DW 0xAA55
buffer:

View File

@ -1,3 +1,17 @@
bpBytesPerSector equ 0x0b
bpSectorsPerCluster equ 0x0d
bpReservedSectors equ 0x0e
bpNoFATs equ 0x10
bpRootDirEntries equ 0x11
bpLVSectors equ 0x13
bpMediumID equ 0x15
bpSectorsPerFat equ 0x16
bpSectorsPerTrack equ 0x18
bpHeads equ 0x1a
bpHiddenSectors equ 0x1c
bpDriveNo equ 0x24
bpSignature equ 0x26
[BITS 16]
; Update the IVT with our interrupt handler
mov bx, 0
@ -8,40 +22,59 @@
mov ax, 2000h
mov ds, ax
pop ax
mov byte [bootdisk], al
mov ax, 7c0h
mov fs, ax
; Set up kernel specifics
; Root Directory Entries will be at 2000h:2000h
; FAT1 will be at 2000h:4000h
; Load sectors RDE to 2000h:2000h
mov ax, 2000h
mov es, ax
mov bx, 2000h
mov ah, 2
mov al, 16
mov cl, 20
mov ch, 0
mov dh, 0
mov dl, [bootdisk]
; Load sectors 20 through 36 to 2000h:2000h
; Calculate position of RDE
mov ax, word [fs:bpSectorsPerFat]
movzx cx, byte [fs:bpNoFATs]
mul cx
add ax, 2
call prep_i13
mov al, 16
int 13h
; Load sectors 2 through 36 to 2000h:4000h
mov ax, 2000h
mov es, ax
mov bx, 4000h
mov ax, 2
call prep_i13
mov ax, word [fs:bpSectorsPerFat]
mov ah, 2
mov al, 9
mov cl, 2
mov ch, 0
mov dh, 0
mov dl, [bootdisk]
; Load sectors 2 through 11 to 2000h:4000h
int 13h
; Calculate file offset
mov ax, word [fs:bpSectorsPerFat]
movzx cx, byte [fs:bpNoFATs]
mul cx
push ax
mov ax, word [fs:bpRootDirEntries]
mov cx, 16
div cx
mov bx, ax
pop ax
add ax, bx
mov word [fileoffset], ax
push msg
call print
@ -165,8 +198,6 @@ data:
newline db 13, 10, 0
bootdisk db 0
program_two db "HELLO BIN"
@ -300,6 +331,9 @@ load_file:
mov bp, sp
push ds
push fs
mov ax, 7c0h
mov fs, ax
mov ax, 2000h
mov ds, ax
@ -308,16 +342,9 @@ load_file:
load_file_loop:
mov bx, word [bp + 6]
mov al, byte [bp + 8]
add al, 34
mov cl, al
mov al, 1
mov ah, 2
mov ch, 0
mov dh, 0
mov dl, byte [bootdisk]
movzx ax, byte [bp + 8]
add ax, word [fileoffset]
call prep_i13
int 13h
@ -334,6 +361,7 @@ load_file_loop:
jmp load_file_loop
load_file_loaded:
pop fs
pop ds
pop bp
ret 6
@ -369,7 +397,6 @@ decode_filename_loop:
decode_filename_stage2:
mov bx, 8
mov cx, 3
; add si, 1
decode_filename_stage2_loop:
@ -419,3 +446,26 @@ _ihdlr_4:
_ihdlr_5:
_ihdlr_fin:
iret
prep_i13:
xor dx, dx
div word [fs:bpSectorsPerTrack]
push dx
xor dx, dx
div word [fs:bpHeads]
mov ch, al
mov dh, dl
mov dl, byte [fs:bpDriveNo]
pop ax
mov cl, al
mov al, 1
mov ah, 2
ret
fileoffset dw 0