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 jmp short bootcode
nop nop
bpOEMid db "XNOE " bpOEMid db "XNOE "
bpBytesPerSector dw 512 bpBytesPerSector dw 512
bpSectorsPerCluster db 1 bpSectorsPerCluster db 1
bpReservedSectors dw 1 bpReservedSectors dw 1
bpNoFATs db 2 bpNoFATs db 2
bpRootDirEntries dw 256 bpRootDirEntries dw 256
bpLVSectors dw 8419 bpLVSectors dw 8586
bpMediumID db 0F0h bpMediumID db 0xF8
bpSectorsPerFat dw 9 bpSectorsPerFat dw 34
bpSectorsPerTrack dw 18 bpSectorsPerTrack dw 18
bpSides dw 2 bpHeads dw 2
bpHiddenSectors dd 0 bpHiddenSectors dw 0
bpLargeSectors dd 0
bpDriveNo dw 0 TIMES 36 - ($ - $$) db 0
bpSignature db 41
bpVolumeID dd 00000000h bpDriveNo db 0
db 0
bpSignature db 0x29
bpVolumeID dd 0x0
bpVolumeLabel db "XNOE OS " bpVolumeLabel db "XNOE OS "
bpFileSystem db "FAT16 " bpFileSystem db "FAT16 "
@ -26,7 +30,18 @@ bootcode:
mov ax, 7c0h mov ax, 7c0h
mov ds, ax 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 mov si, boot_msg
call _boot_print call _boot_print
@ -35,12 +50,13 @@ bootcode:
mov es, ax mov es, ax
mov bx, buffer mov bx, buffer
mov ah, 2 ; Calculate position of rootdirentries
mov al, 16 mov ax, word [bpSectorsPerFat]
mov cl, 20 movzx cx, byte [bpNoFATs]
mov ch, 0 mul cx
mov dh, 0
mov dl, byte [drive] add ax, 2
call prep_i13
int 13h int 13h
@ -57,11 +73,6 @@ kernel_finder:
mov di, buffer mov di, buffer
add di, ax add di, ax
; mov si, di
; call _boot_print
; mov si, new_line
; call _boot_print
mov si, kernel_file mov si, kernel_file
mov cx, 11 mov cx, 11
rep cmpsb rep cmpsb
@ -78,6 +89,7 @@ kernel_finder:
jmp $ jmp $
kernel_found: kernel_found:
; jmp $
mov ax, [es:di+0fh] mov ax, [es:di+0fh]
mov word [cluster], ax mov word [cluster], ax
@ -86,29 +98,41 @@ fat_loader:
mov es, ax mov es, ax
mov bx, buffer mov bx, buffer
mov ax, word [bpSectorsPerFat]
mov ah, 2 mov ah, 2
mov al, 9
mov cl, 2 mov cl, 2
mov ch, 0 mov ch, 0
mov dh, 0 mov dh, 0
mov dl, byte [drive] mov dl, byte [bpDriveNo]
int 13h int 13h
kernel_loader: 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 ax, 2000h
mov es, ax mov es, ax
mov bx, word [pointer] mov bx, word [pointer]
mov al, byte [cluster] movzx ax, byte [cluster]
add al, 34 add ax, word [fileoffset]
mov cl, al call prep_i13
mov al, 1
mov ah, 2
mov ch, 0
mov dh, 0
mov dl, byte [drive]
int 13h int 13h
@ -117,7 +141,6 @@ kernel_loader:
mov si, word [cluster] mov si, word [cluster]
shl si, 1 shl si, 1
add si, buffer add si, buffer
; add si, 1
cmp word [si], 0ffffh cmp word [si], 0ffffh
je kernel_loaded je kernel_loaded
@ -125,10 +148,6 @@ kernel_loader:
jmp kernel_loader jmp kernel_loader
kernel_loaded: kernel_loaded:
; Pass boot device to the kernel
xor ax, ax
mov al, byte [drive]
push ax
jmp 2000h:0h jmp 2000h:0h
_boot_print: _boot_print:
@ -149,11 +168,34 @@ kernel_nf db "KERNEL.BIN Missing!", 13, 10, 0
new_line db " ", 0 new_line db " ", 0
kernel_file db "KERNEL BIN" kernel_file db "KERNEL BIN"
fileoffset dw 0
cluster dw 0 cluster dw 0
pointer dw 0 pointer dw 0
drive db 0
TIMES 510 - ($ - $$) db 0 ;fill the rest of sector with 0 ; AX set to the logical sector number.
DW 0xAA55 ; add boot signature at the end of bootloader 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: 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] [BITS 16]
; Update the IVT with our interrupt handler ; Update the IVT with our interrupt handler
mov bx, 0 mov bx, 0
@ -8,40 +22,59 @@
mov ax, 2000h mov ax, 2000h
mov ds, ax mov ds, ax
pop ax mov ax, 7c0h
mov byte [bootdisk], al mov fs, ax
; Set up kernel specifics ; Set up kernel specifics
; Root Directory Entries will be at 2000h:2000h ; Root Directory Entries will be at 2000h:2000h
; FAT1 will be at 2000h:4000h ; FAT1 will be at 2000h:4000h
; Load sectors RDE to 2000h:2000h
mov ax, 2000h mov ax, 2000h
mov es, ax mov es, ax
mov bx, 2000h 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 int 13h
; Load sectors 2 through 36 to 2000h:4000h
mov ax, 2000h mov ax, 2000h
mov es, ax mov es, ax
mov bx, 4000h mov bx, 4000h
mov ax, 2
call prep_i13
mov ax, word [fs:bpSectorsPerFat]
mov ah, 2 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 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 push msg
call print call print
@ -165,8 +198,6 @@ data:
newline db 13, 10, 0 newline db 13, 10, 0
bootdisk db 0
program_two db "HELLO BIN" program_two db "HELLO BIN"
@ -300,6 +331,9 @@ load_file:
mov bp, sp mov bp, sp
push ds push ds
push fs
mov ax, 7c0h
mov fs, ax
mov ax, 2000h mov ax, 2000h
mov ds, ax mov ds, ax
@ -308,16 +342,9 @@ load_file:
load_file_loop: load_file_loop:
mov bx, word [bp + 6] mov bx, word [bp + 6]
movzx ax, byte [bp + 8]
mov al, byte [bp + 8] add ax, word [fileoffset]
add al, 34 call prep_i13
mov cl, al
mov al, 1
mov ah, 2
mov ch, 0
mov dh, 0
mov dl, byte [bootdisk]
int 13h int 13h
@ -334,6 +361,7 @@ load_file_loop:
jmp load_file_loop jmp load_file_loop
load_file_loaded: load_file_loaded:
pop fs
pop ds pop ds
pop bp pop bp
ret 6 ret 6
@ -369,7 +397,6 @@ decode_filename_loop:
decode_filename_stage2: decode_filename_stage2:
mov bx, 8 mov bx, 8
mov cx, 3 mov cx, 3
; add si, 1
decode_filename_stage2_loop: decode_filename_stage2_loop:
@ -418,4 +445,27 @@ _ihdlr_4:
jmp _ihdlr_fin jmp _ihdlr_fin
_ihdlr_5: _ihdlr_5:
_ihdlr_fin: _ihdlr_fin:
iret 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