diff --git a/bootsect.asm b/bootsect.asm index cbb3122..6847bd6 100644 --- a/bootsect.asm +++ b/bootsect.asm @@ -97,25 +97,15 @@ calc_constants: fat: ; FAT1 (the main one) follows right after bootsector(s) mov ax, [reservedsectors] - call chs - - ; TODO: handle possible crossing of track boundary - mov ax, [sectorsperfat] - mov ah, 2 - mov dl, [drivenumber] mov bx, 0x8000 - int 0x13 + mov cx, [sectorsperfat] + call loadsectors root_dir: mov ax, [rootdir] - call chs - - ; TODO: handle possible crossing of track boundary - mov ax, [rootdirsectors] - mov ah, 2 - mov dl, [drivenumber] mov bx, 0x500 - int 0x13 + mov cx, [rootdirsectors] + call loadsectors search_root: mov bx, [rootdirentries] @@ -198,27 +188,12 @@ found: add ax, [rootdir] add ax, [rootdirsectors] - ; Load sectors - pop bx - xor cx, cx - mov cl, [sectorspercluster] - mov dl, [drivenumber] - .loop: - 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 + .load: + ; Load sectors + pop bx + xor cx, cx + mov cl, [sectorspercluster] + call loadsectors .next: pop ax @@ -268,45 +243,77 @@ execute_kernel: mov dl, [drivenumber] jmp 0:0x500 -chs: +; Note: bx will point to after the read data +loadsectors: push ax - push bx + push cx + push dx - ; Save drive number - mov bl, dl + .loop: + push ax + push cx - xor dx, dx - ; cylinder (track) - head - sector - ; cylinder = LBA / sectorspertrack / heads - ; head = LBA / sectorspertrack % heads - ; sector = LBA % sectorspertrack + 1 - div word [sectorspertrack] - ; ax = LBA / sectorspertrack - ; dx = LBA % sectorspertrack + .chs: + xor dx, dx + ; cylinder (track) - head - sector + ; cylinder = LBA / sectorspertrack / heads + ; head = LBA / sectorspertrack % heads + ; sector = LBA % sectorspertrack + 1 + div word [sectorspertrack] + ; ax = LBA / sectorspertrack + ; dx = LBA % sectorspertrack - ; sector - mov cl, dl - inc cl + ; sector + mov cl, dl + inc cl - xor dx, dx - div word [heads] - ; ax = LBA / sectorspertrack / heads - ; dx = LBA / sectorspertrack % heads + xor dx, dx + div word [heads] + ; ax = LBA / sectorspertrack / heads + ; dx = LBA / sectorspertrack % heads - ; head - mov dh, dl + ; head + mov dh, dl - ; cylinder (track) - mov ch, al - ;shr ax, 1 - ;shr ax, 1 - ;and al, 0xC0 - ;or cl, al + ; cylinder (track) + mov ch, al + shr ax, 1 + shr ax, 1 + and al, 0xC0 + or cl, al - ; Restore drive number - mov dl, bl + ; Copy number of sectors to still read to ax and cap at 127 + 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 ret