Page 1 of 1

IDE tutorial

Posted: Fri May 27, 2005 12:42 pm
by vlavilla
can anyone please point me to a tutorial on how to implement IDE hard drive access into my OS?

i have found sites describing IDE drives and some sample code, but none that explain how to use it.

thanks

Re:IDE tutorial

Posted: Fri May 27, 2005 2:29 pm
by vlavilla
i mean, ATA not IDE.

Re:IDE tutorial

Posted: Sat May 28, 2005 10:05 am
by DennisCGc
Hi,

Since no-one else replied to this topic, I'm going to take a shot :)

First, you really need the "official" documents, which describes the ATA commands. http://www.t13.org/#Project_drafts is the location of it. You can download the "d1410r3b ATA/ATAPI-6 revision 3b" document. (You can download others as well).
Especially section 8 is quite handy.

Here I'll attempt to describe some info about ATA.

Most modern computers have 2 IDE-controllers, called Primary and Secondary controller. Each controller allows two ATA(PI) devices (Harddisks, CDROM/CDRW drives, DVDROM/DVDRW+/- drives). The first device of a controller is called "master", the second device is called "slave".

Now each controller gets an I/O range. 0x1f0-0x1f7 and 0x3f6 is meant for the primary controller, 0x170-0x177 and 0x376 is meant for the secondary controller.
It's just a difference of 0x80, but it's the same. (meaning; 0x1f0's purpose is the same as 0x170's purpose)

Now, if we for example want to read a sector from the master @ the primary controller, you should do the following:

1. Check if the IDE controller is busy. You can do that by reading the I/O port 0x3f6 or 0x377, in our case it should be 0x3f6. (More info can be found in the document)
2. If it's not busy, do some I/O writings to 0x1f1,0x1f2, etc. What you should write, it's all in the document ;)
3. IRQ 14 or 15 is triggered. (So yes, it's recommended to have these, it eases our live :), you can do the actual reading without having an IDT up. I think you should read 0x3f6/0x377 the whole time checking if it's busy, if not anymore, the reading is done)
4. Check if there was an error, you can use the status flag (1f7h or 177h)
5. If there was no error, you can read the words (yeah, you read it well, words, not bytes). You can use 1f0h or 170h. Example:

Code: Select all

mov  dx,1f0h
in ax,dx
As far as I can tell, ax is little-endian, so, putting it into memory like this:

Code: Select all

mov [esi],ax
will lead to this:
"01" instead of "10". (First one was the correct order)
You HAVE TO read from it 256x times. (because 256*2=512)
6. End it :)

If someone else sees a mistake, correct me then.
If you have any questions, ask it :)

Anyway,
HTH.

DennisCGc.

Re:IDE tutorial

Posted: Mon May 30, 2005 9:28 am
by CloudNine
Here's a shameless plug for my new programming site - c9online.l2p.net. It contains a tutorial on identifying hard drives and reading and writing hard-drives, but it's all still a work in progress. If people get something out of it, I might be motivated to add to it :)