Page 1 of 1

[SOLVED] ATA PIO mode disk driver doesn't work on laptop

Posted: Mon Aug 05, 2024 5:47 am
by stskyblade
I wrote a disk driver to read hard disk. It works on Qemu but doesn't on my laptop.

My laptop model is Asus x450vc. It is an old computer, and has two hard disks. One is Sandisk 128GB SSD connecting to the original SATA interface. This disk contains my OS code.

According to this link: https://wiki.osdev.org/ATA_PIO_Mode#ATA_Driver , I think SATA disk is an ATA-compliant drive and the ATA PIO mode disk driver should work on it.

This disk driver works on Qemu emulator but doesn't on my laptop. This is what I saw (I have tested for both primary & secondary bus, both master & slave device):
- Reading the Regular Status byte (port 0x1f7 for primary bus) gives me 0xFF, which is an invalid value. It didn't pass the Floating Bus test
- I wrote a random value to port 0x1f2. It should be the same value if I read it back. But I got 0xFF again. It didn't pass the Random value write/read test
- I performed the IDENTIFY command test on the IO port. But I got 0xFF for both Status register (0x1f7), and port 0x1f4, and port 0x1f5. Which meanings the drive is not ATA.

Source code can be found here:
https://github.com/stskyblade/StarOS/bl ... ader.S#L49

Step to reproduce:

Code: Select all

make qemu	# this will build my os, and test it on Qemu. This is OK.
make burn	# Be careful: this will burn my disk image to /dev/sdb.
# insert the disk to my laptop and boot


Re: ATA PIO mode disk driver doesn't work on laptop

Posted: Mon Aug 05, 2024 10:29 am
by Octocontrabass
stskyblade wrote: Mon Aug 05, 2024 5:47 amIt is an old computer,
The wiki is older. You need to enumerate PCI to find your PC's IDE controller (if it has one). When you enumerate PCI, you'll be able to figure out which IO ports are assigned to the IDE controller.
stskyblade wrote: Mon Aug 05, 2024 5:47 amand the ATA PIO mode disk driver should work on it.
Only if the SATA HBA is configured to operate in IDE mode. If it's in AHCI mode, you'll need an AHCI driver to access the drive. Many SATA controllers can be switched to IDE mode with a BIOS setup option, but some of them can't.

Re: ATA PIO mode disk driver doesn't work on laptop

Posted: Mon Aug 12, 2024 9:27 pm
by stskyblade
Octocontrabass wrote: Mon Aug 05, 2024 10:29 am
stskyblade wrote: Mon Aug 05, 2024 5:47 amIt is an old computer,
The wiki is older. You need to enumerate PCI to find your PC's IDE controller (if it has one). When you enumerate PCI, you'll be able to figure out which IO ports are assigned to the IDE controller.
stskyblade wrote: Mon Aug 05, 2024 5:47 amand the ATA PIO mode disk driver should work on it.
Only if the SATA HBA is configured to operate in IDE mode. If it's in AHCI mode, you'll need an AHCI driver to access the drive. Many SATA controllers can be switched to IDE mode with a BIOS setup option, but some of them can't.

Thank you for your advice. I read related pages and modify my disk driver code. It works now. Here is the explanation:
There are two working mode for SATA disk in my laptop, AHCI mode and IDE mode. AHCI mode is default. In this mode, the host adaptor appears to be a PCI SATA controller. If you change it to IDE mode in BIOS, it's a PCI IDE controller now.
Again, there are two mode for PCI IDE controller, PCI native mode and Compatibility mode. For PCI native mode, you should use IO port other than 0x1F0 and 0x170. For Compatitable mode, IO port 0x1F0 is OK.

So, it's really simple to solve this bug:
0. Enter BIOS, change SATA disk to IDE mode
1. enumerate PCI device to find the host adaptor you are using
2. change it to Compatibility mode if possible
3. ATA PIO mode driver should work know

Related links
https://wiki.osdev.org/PCI_IDE_Controller
https://wiki.osdev.org/PCI#Command_Register