Issue with ATA IDENTIFY command on QEMU

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
FelixBoop
Posts: 13
Joined: Thu Apr 24, 2014 1:35 pm

Issue with ATA IDENTIFY command on QEMU

Post by FelixBoop »

Hello all,

I've been working on a basic ATA driver, but I'm running into a strange problem when I try to issue the command IDENTIFY (ECh) to a drive:

Code: Select all

cli

mov dx, 0x3f6 ;clear both control registers
xor al, al
out dx, al
mov dx, 0x376
out dx, al

mov dx, 0x177 ;now we need to check if there even are any drives 
in al, dx
cmp al, 0xff ;if status is all ones, there is no drive at all
je ata_err

;now we need to check that we at least /have/ a responsive drive
mov dx, 0x172
mov al, 0x72
out dx, al
inc dx
mov al, 0x73
out dx, al
inc dx
mov al, 0xfa ;just a random value
out dx, al
mov dx, 0x172
in al, dx
cmp al, 0x72
jne stop_it ;this means we have no responsive drives

mov dx, 0x176 ;drive select register
mov al, 0xa0 ;master drive
out dx, al

mov dx, 0x177 ;now send IDENTIFY (0xec) to command reg
mov al, 0xec
out dx, al
in al, dx ;immediately read status after command
call fnc_print_hex ;print human-readable contents of al
cmp al, 0x00 ;if status is zero after IDENTIFY command, drive doesn't exist
je ata_err
On the primary bus (IO ports xFx), there is no master or slave drive, as status returns 00h after IDENTIFY command on both. On the secondary bus (IO x7x), the slave drive has the same behavior, but the master returns 41h (10000001b).

If I'm not mistaken, this means the busy and error bits are both set after issuing the command. Is this a problem with QEMU (v 0.9.0 under Windows (I know)), or am I doing something wrong with my code?

I'm booting off of an CD-ROM ISO, and I'm zero-ing both bus's control registers before attempting anything. I also check that the secondary returns old CHS values written to its registers.

My Sources

ATA PIO Mode
http://lateblt.tripod.com/atapi.htm
http://forum.osdev.org/viewtopic.php?t=15087
SAUCE CD IV - The most schwaaay OS.
User avatar
BrightLight
Member
Member
Posts: 901
Joined: Sat Dec 27, 2014 9:11 am
Location: Maadi, Cairo, Egypt
Contact:

Re: Issue with ATA IDENTIFY command on QEMU

Post by BrightLight »

The BSY bit is bit 7 (0x80) not bit 6 (0x40). Your returned status (0x41) seems normal to me, because you're using a CD-ROM, which really is ATAPI and not ATA. Basically, ATAPI allows you send SCSI commands over the ATA bus. Most CD-ROMs are ATAPI devices.

After sending the ATA IDENTIFY command and receiving an error, read the ID of the device to check whether the error was caused by the device itself, or there is no device, or the device is a CD-ROM. If it is a CD-ROM, you perform the exact same IDENTIFY sequence but with the ATAPI IDENTIFY command byte instead of ATA IDENTIFY. I don't recall the exact values for each register/command I mentioned, but you can find them somewhere on our Wiki. ;)

If I were to guess from old memory however, the ATAPI IDENTIFY command is either 0xA1 or 0xA2. Again, just a guess.
You know your OS is advanced when you stop using the Intel programming guide as a reference.
User avatar
FelixBoop
Posts: 13
Joined: Thu Apr 24, 2014 1:35 pm

Re: Issue with ATA IDENTIFY command on QEMU

Post by FelixBoop »

omarrx024 wrote:The BSY bit is bit 7 (0x80) not bit 6 (0x40). Your returned status (0x41) seems normal to me, because you're using a CD-ROM, which really is ATAPI and not ATA. Basically, ATAPI allows you send SCSI commands over the ATA bus. Most CD-ROMs are ATAPI devices.
This is what I was missing, thank you! I was thinking that it was a normal ATA PIO device. #-o Now it works just fine with command 0xA1.
SAUCE CD IV - The most schwaaay OS.
User avatar
BenLunt
Member
Member
Posts: 941
Joined: Sat Nov 22, 2014 6:33 pm
Location: USA
Contact:

Re: Issue with ATA IDENTIFY command on QEMU

Post by BenLunt »

FelixBoop wrote:
omarrx024 wrote:...because you're using a CD-ROM, which really is ATAPI and not ATA. Basically, ATAPI allows you send SCSI commands over the ATA bus. Most CD-ROMs are ATAPI devices.
This is what I was missing, thank you! I was thinking that it was a normal ATA PIO device. #-o Now it works just fine with command 0xA1.
Just to add that this is the technique you are suppose to use to detect whether the device is ATA or ATAPI. Granted, you can and should check the two register values after a reset, but you should also then send the ATA IDENTIFY command.

An ATAPI will purposely fail an ATA IDENTIFY, therefore if you send the ATA IDENTIFY and it is successful, you know you have an ATA only device. If it fails, you either have a faulty ATA device or an ATAPI device, so try the ATAPI IDENTIFY. If it is successful, you have an ATAPI device.

Also, an ATA IDENTIFY command must wait for the drive to be ready, and ATAPI IDENTIFY, you do not have to wait for it to be ready. (via the ATA_STATUS_RDY bit)

Ben
Post Reply