Centralize and improve boot sector disk access

This commit is contained in:
Juhani Krekelä 2021-07-04 17:36:25 +03:00
parent 96dcfa48bb
commit 591fe91f93
1 changed files with 72 additions and 65 deletions

View File

@ -97,25 +97,15 @@ calc_constants:
fat: fat:
; FAT1 (the main one) follows right after bootsector(s) ; FAT1 (the main one) follows right after bootsector(s)
mov ax, [reservedsectors] mov ax, [reservedsectors]
call chs
; TODO: handle possible crossing of track boundary
mov ax, [sectorsperfat]
mov ah, 2
mov dl, [drivenumber]
mov bx, 0x8000 mov bx, 0x8000
int 0x13 mov cx, [sectorsperfat]
call loadsectors
root_dir: root_dir:
mov ax, [rootdir] mov ax, [rootdir]
call chs
; TODO: handle possible crossing of track boundary
mov ax, [rootdirsectors]
mov ah, 2
mov dl, [drivenumber]
mov bx, 0x500 mov bx, 0x500
int 0x13 mov cx, [rootdirsectors]
call loadsectors
search_root: search_root:
mov bx, [rootdirentries] mov bx, [rootdirentries]
@ -198,27 +188,12 @@ found:
add ax, [rootdir] add ax, [rootdir]
add ax, [rootdirsectors] add ax, [rootdirsectors]
; Load sectors .load:
pop bx ; Load sectors
xor cx, cx pop bx
mov cl, [sectorspercluster] xor cx, cx
mov dl, [drivenumber] mov cl, [sectorspercluster]
.loop: call loadsectors
push ax
push cx
call chs
mov ah, 2
mov al, 1
int 0x13
pop cx
pop ax
add bx, 512
inc ax
loop .loop
.next: .next:
pop ax pop ax
@ -268,45 +243,77 @@ execute_kernel:
mov dl, [drivenumber] mov dl, [drivenumber]
jmp 0:0x500 jmp 0:0x500
chs: ; Note: bx will point to after the read data
loadsectors:
push ax push ax
push bx push cx
push dx
; Save drive number .loop:
mov bl, dl push ax
push cx
xor dx, dx .chs:
; cylinder (track) - head - sector xor dx, dx
; cylinder = LBA / sectorspertrack / heads ; cylinder (track) - head - sector
; head = LBA / sectorspertrack % heads ; cylinder = LBA / sectorspertrack / heads
; sector = LBA % sectorspertrack + 1 ; head = LBA / sectorspertrack % heads
div word [sectorspertrack] ; sector = LBA % sectorspertrack + 1
; ax = LBA / sectorspertrack div word [sectorspertrack]
; dx = LBA % sectorspertrack ; ax = LBA / sectorspertrack
; dx = LBA % sectorspertrack
; sector ; sector
mov cl, dl mov cl, dl
inc cl inc cl
xor dx, dx xor dx, dx
div word [heads] div word [heads]
; ax = LBA / sectorspertrack / heads ; ax = LBA / sectorspertrack / heads
; dx = LBA / sectorspertrack % heads ; dx = LBA / sectorspertrack % heads
; head ; head
mov dh, dl mov dh, dl
; cylinder (track) ; cylinder (track)
mov ch, al mov ch, al
;shr ax, 1 shr ax, 1
;shr ax, 1 shr ax, 1
;and al, 0xC0 and al, 0xC0
;or cl, al or cl, al
; Restore drive number ; Copy number of sectors to still read to ax and cap at 127
mov dl, bl pop ax
push ax
cmp al, 127
jbe .read
mov al, 127
pop bx .read:
mov ah, 2
mov dl, [drivenumber]
int 0x13
; Decrement cx by the number of sectors read
pop cx
xor ah, ah
sub cx, ax
; Increment bx by the number of sectors read times 512
xor dx, dx
mov dh, al
shl dx, 1
add bx, dx
; Increment ax by the number of sectors read
pop dx
add ax, dx
test cx, cx
jnz .loop
pop dx
pop cx
pop ax pop ax
ret ret