OSDev.org https://forum.osdev.org/ |
|
ATA driver hangs on initialization while polling BSY https://forum.osdev.org/viewtopic.php?f=1&t=33487 |
Page 1 of 1 |
Author: | K3achas [ Wed Feb 06, 2019 11:30 am ] |
Post subject: | ATA driver hangs on initialization while polling BSY |
My code for a bootloader hangs while polling BSY before pretty much everywhere I poll BSY (i.e. BSY never seems to clear). Code: hdd_init: mov al, 0x06 mov dx, 0x3f6 out dx, al ; do a software reset mov dx, 0x1f7 in al, dx in al, dx in al, dx in al, dx ; 400ns delay in al, dx cmp al, 0xff je mfail ; drive missing error poll0: in al, dx test al, 0x40 ; test DRDY to make sure the drive is ready to switch pause ; jz poll0 ; this hangs infinitely dec dx mov al, 0xa0 out dx, al ; select drive 0 mov dx, 0x1f1 xor al, al out dx, al ; send 0 to features register add dx, 6 in al, dx in al, dx in al, dx in al, dx ; 400ns delay in al, dx cmp al, 0xff je mfail ; drive missing error test al, 0x01 jnz mfail ; drive select error xor al, al mov dx, 0x1f2 out dx, al inc dx ; 0x1f3 out dx, al inc dx ; 0x1f4 out dx, al inc dx ; 0x1f5 out dx, al mov al, 0xec add dx, 2 ; 0x1f7 out dx, al ; send identify command in al, dx in al, dx in al, dx in al, dx ; 400ns delay in al, dx cmp al, 0 je mfail sub dx, 2 ; 0x1f5 in al, dx test al, al jnz pfail dec dx ; 0x1f4 in al, dx test al, al jnz pfail poll5: pause in al, dx test al, 0x80 jnz poll5 ; wait for BSY to clear -- this may also hang ; now to find a gpt table mov dx, 0x1f2 mov al, 1 out dx, al ; sector count inc dx mov al, 1 out dx, al ; low byte of LBA inc dx xor al, al out dx, al inc dx xor al, al out dx, al ; higher bytes of LBA mov dx, 0x1f7 in al, dx in al, dx in al, dx in al, dx ; 400ns delay poll10: pause in al, dx and al, 0xc0 cmp al, 0x40 jne poll10 ; test RDY and BSY -- or this one hangs mov al, 0x20 out dx, al ; read using LBA 28 in al, dx in al, dx in al, dx in al, dx ; 400ns delay in al, dx test al, 0x01 je dfail poll20: in al, dx test al, 0x40 ; test RDY to make sure the drive is spun up pause jz poll20 poll30: in al, dx test al, 0x80 ; test BSY to make sure the drive is preparing to read pause jz poll30 poll40: in al, dx test al, 0x80 ; test BSY to make sure the drive is ready to read pause jnz poll40 poll50: in al, dx test al, 0x08 pause jnz poll50 ; test RDX to make sure the drive is really ready to read I've tried to search for similar problems, but haven't found anything that fixed it. Any help would be much appreciated. |
Author: | BenLunt [ Wed Feb 06, 2019 3:18 pm ] |
Post subject: | Re: ATA driver hangs on initialization while polling BSY |
K3achas wrote: My code for a bootloader hangs while polling BSY before pretty much everywhere I poll BSY (i.e. BSY never seems to clear). Code: hdd_init: mov al, 0x06 mov dx, 0x3f6 out dx, al ; do a software reset First, if this is a boot loader, why are you not calling the firmware to read from the drive? i.e.: why not call the BIOS interrupt 13h services? Second, you set the reset bit, but never clear it. If it is always in reset, of course the busy bit will be set. Set the reset bit, wait for at least 5 uS then clear it waiting at least 2 mS before doing anything else. May I suggest a few things as well? You hard code the port addresses (0x3F6, 0x1F7, etc). It would be better to call this as a function, passing the base address and alternate base address. Also, #defines are your friend. For example: Code: hdd_init: mov al,ATA_DEV_CNTRL_RESET | ATA_DEV_CNTRL_nINT mov dx,word ptr [alt_base] add dx,ATA_DEV_CONTROL out dx, al ; *****start****** a software reset Ben - http://www.fysnet.net/osdesign_book_series.htm |
Author: | K3achas [ Wed Feb 06, 2019 5:59 pm ] |
Post subject: | Re: ATA driver hangs on initialization while polling BSY |
Thanks, I didn't catch that by reading the wiki. I'll make the suggested changes to clean the code. Also, I should have mentioned that it's running in long mode and only has access to the hardware itself (and the rather limited EFI runtime services). |
Author: | Octocontrabass [ Thu Feb 07, 2019 7:22 am ] |
Post subject: | Re: ATA driver hangs on initialization while polling BSY |
Is there any particular reason you don't want to use the boot services in your bootloader? |
Author: | K3achas [ Thu Feb 07, 2019 1:20 pm ] |
Post subject: | Re: ATA driver hangs on initialization while polling BSY |
The main reason is that that way I can reclaim used memory a lot easier (I don't have to, say, add a loaded program just to reclaim pages used by the bootloader), but it also helps to do the proper hardware setup in kernel mode (i.e. a bootloader, as I use a tiny microkernel architecturally limited to 8 KiB) as opposed to making it way longer in usermode. Additionally, it's harder to find resources on EFI's boot services than it is on the hardware. |
Author: | Octocontrabass [ Fri Feb 08, 2019 4:58 am ] |
Post subject: | Re: ATA driver hangs on initialization while polling BSY |
I don't see how using boot services to load the kernel will change how you reclaim the bootloader's pages. Plus, if you use boot services, you won't have to keep updating your bootloader as hardware changes. For example, you're writing an IDE driver, but most PCs use AHCI nowadays. In the future, that may be replaced by NVMe. |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |