OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 18, 2024 2:27 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Interrupt 13H Reading Floppy Sectors - Never returns
PostPosted: Sun Jun 24, 2018 7:45 pm 
Offline
Member
Member
User avatar

Joined: Mon Dec 28, 2015 11:11 am
Posts: 401
Recently I wanted to improve my bootloader, and since my script file also compiles the bootsector, and I broke my operating system.
Main boot sector file:
Code:
bits   16
org      0x7c00
jmp   main
times 0x0b-($-$$) db 0
%include "fat_boot_sector.asm"
%include "real_mode_floppy.asm"
%include "real_mode_print.asm"
%include "real_mode_fat_12.asm"
bsAdditional           DB "Quartz Bootsector", 0x0A, 0x0D, 0
bsErrorMsg             DD 0
bsErrorMsgB            DB "Boot file `/qloader` can not be found.", 0x0A, 0x0D, 0
dFile db "QLOADER    "
main: ;Start
    xchg bx, bx
    cli
    xor ax, ax
    mov ss, ax
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov sp, 0x7C00
    sti
    mov si, bsAdditional
    call print
    mov WORD [bufloc], 0x0150
    push 1
    push 32
    call ReadSectors

    mov ax, dFile
    mov WORD [FileName], ax
   mov WORD [bsErrorMsg], bsErrorMsgB
    call FindFile ; >>> Here it gets stuck
    xor ax, ax
    mov bx, ax
    mov cx, ax
    mov dx, ax
    jmp 0:0x8000
times 510-($-$$) db 0
dw 0xAA55

FAT12 related routines:
Code:
%ifndef FAT_12
%define FAT_12
FileDirSeg dw 0
FileDir dw 0x3900
DirEntryN dw 224
FileBuf dw 0x0800
FileName dw 0
FindFile:
    xor dx, dx
    mov si, WORD [FileDir] ;ds:si = 0:FileDir
    mov di, WORD [FileName];es:di = 0:(string location)
    mov ax, WORD [FileDirSeg]
    mov ds, ax
    findloop:
    mov cx, 11
    push si
    push di
    mov al, BYTE [si]
    test al, al
    jz nfound
    repz cmpsb
    jz found
    pop di
    pop si
    inc dx
    add si, 0x20
    cmp dx, [DirEntryN];TODO
    jnz findloop
    ;Not found
    nfound:
    xor ax, ax
    mov ds, ax
    mov si, [bsErrorMsg]
    call print
    cli
    hlt
    found:
    pop di
    pop si
    push ds
    xor ax, ax
    mov ds, ax
    mov WORD [bufloc], ax
    ;Entry is in DS:SI
    add si, 26 ;First sector
    pop ds
    mov ax, WORD [si] ;AX contains the sector
    xor cx, cx
    mov ds, cx
    LoadToMem:
    mov bx, ax
    add bx, 31
    push ax
    push bx
    push 1
    call ReadSectors ; >>> Here it gets stuck
    pop ax
    add WORD [bufloc], 0x20
    ;Now find the next sector.
    xor dx, dx
    mov cx, 2
    div cx
    test dx, dx
    jz even_entry
    ;odd entry
    mov dx, 3
    mul dx
    mov bx, ax
    mov ax, word [bx+0x1501]
    shr ax, 4
    cmp ax, 0xFF0
    jl LoadToMem
    jmp DoneLoad
    even_entry:
    mov dx, 3
    mul dx
    mov bx, ax
    mov ax, word [bx+0x1500]
    and ax, 0x0FFF
    cmp ax, 0xFF0
    jl LoadToMem
    DoneLoad:
    ret
%endif

ReadSectors:
Code:
%ifndef _floppy
%include "fat_boot_sector.asm"
%define _floppy
sector db 0
head db 0
track db 0
count db 0
bufloc dw 0x0050
stacktemp dw 0
;PROCEDURE
;Reads sectors. (Real Mode)
;Two entries for stack:
;1st - Sector count
;2st - First sector given in LBA
ReadSectors:
    pop ax
    mov WORD [stacktemp], ax
    pop ax
    mov BYTE [count], al
    pop ax
    xor dx, dx
    div WORD [bpbSectorsPerTrack]
    inc dl
    mov BYTE [sector], dl
    xor dx, dx
    div WORD [bpbHeadsPerCylinder]
    mov BYTE [head], dl
    mov BYTE [track], al ;LBA to CHS
    read_try:
    xor ah, ah
    xor dl, dl
    int 0x13 ;Reset
    jc read_try
    xor dl, dl
    mov bx, WORD [bufloc] ;Loading at bufloc:0
    push es
    mov es, bx
    xor bx, bx
    xor dx, dx
    mov ah, 0x02
    mov al, BYTE [count]
    mov ch, BYTE [track]
    mov cl, BYTE [sector]
    mov dh, BYTE [head]
    int 0x13 ; >>> Here it gets stuck
    jc read_try
    pop es
    push word [stacktemp]
    ret
%endif

I load the 32 sectors after first in 0x1500 (0x150:0).
I have been debugging this and could not find what is wrong, maybe because it's 3:40AM and I'm deconcentrated.
Can somebody more experienced find the problem please?
While writing this post, I noticed I put DD instead of DW in the bsErrorMsg, but I doubt that is the problem.
The bootloader finds the file in the root directory and its first sector correctly, by the way.


Top
 Profile  
 
 Post subject: Re: Interrupt 13H Reading Floppy Sectors - Never returns
PostPosted: Mon Jun 25, 2018 3:44 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

Lukand wrote:
Recently I wanted to improve my bootloader, and since my script file also compiles the bootsector, and I broke my operating system.


Here you set "bufloc" to zero:

Code:
    found:
    pop di
    pop si
    push ds
    xor ax, ax
    mov ds, ax
    mov WORD [bufloc], ax


..and here you trash the BIOS Data Area while the BIOS is in the middle of using it:

Code:
    mov bx, WORD [bufloc] ;Loading at bufloc:0
    push es
    mov es, bx
    xor bx, bx
    xor dx, dx
    mov ah, 0x02
    mov al, BYTE [count]
    mov ch, BYTE [track]
    mov cl, BYTE [sector]
    mov dh, BYTE [head]
    int 0x13 ; >>> Here it gets stuck


Notes:
  • The Pascal calling convention is horrible. Stop using it (and especially stop doing that nonsense with "stacktemp", and especially stop passing some parameters in global variables and others on the stack). Ideally you'd pass all parameters in registers.
  • Resetting the disk system ("int 0x13, ah = 0x00) before every read will ruin performance for no reason. You should only reset the disk system if there was an error, and even then I'd only do it after an even number of errors (e.g. "try, retry, reset+retry, retry, reset+retry, retry, fail").
  • Labels always go on the left so that they're easy to spot (and not indented like an instruction). There are no excuses!


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: Interrupt 13H Reading Floppy Sectors - Never returns
PostPosted: Mon Jun 25, 2018 3:57 am 
Offline
Member
Member
User avatar

Joined: Mon Dec 28, 2015 11:11 am
Posts: 401
Brendan wrote:
Hi,

Lukand wrote:
Recently I wanted to improve my bootloader, and since my script file also compiles the bootsector, and I broke my operating system.


Here you set "bufloc" to zero:

Code:
    found:
    pop di
    pop si
    push ds
    xor ax, ax
    mov ds, ax
    mov WORD [bufloc], ax


..and here you trash the BIOS Data Area while the BIOS is in the middle of using it:

Code:
    mov bx, WORD [bufloc] ;Loading at bufloc:0
    push es
    mov es, bx
    xor bx, bx
    xor dx, dx
    mov ah, 0x02
    mov al, BYTE [count]
    mov ch, BYTE [track]
    mov cl, BYTE [sector]
    mov dh, BYTE [head]
    int 0x13 ; >>> Here it gets stuck


Notes:
  • The Pascal calling convention is horrible. Stop using it (and especially stop doing that nonsense with "stacktemp", and especially stop passing some parameters in global variables and others on the stack). Ideally you'd pass all parameters in registers.
  • Resetting the disk system ("int 0x13, ah = 0x00) before every read will ruin performance for no reason. You should only reset the disk system if there was an error, and even then I'd only do it after an even number of errors (e.g. "try, retry, reset+retry, retry, reset+retry, retry, fail").
  • Labels always go on the left so that they're easy to spot (and not indented like an instruction). There are no excuses!


Cheers,

Brendan


Hey, believe it or not, but I woke up recently and almost immediately saw that ES:BX (e.g. [bufloc]) was set to zero, and just when I wanted to say I spotted the problem, you posted.
Thanks for help anyway.
By the way, I'm really not sure how did it boot before. Maybe every time accidental black hole radiation occured and it set BufLoc to 0x800 (and same for kernel)? :lol:
Neither do I know why did I cleared to zero, damn it sleep deprivation (and black holes).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 131 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group