Communicating with hard drive

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
etollerson
Posts: 2
Joined: Sun Oct 29, 2006 1:06 pm

Communicating with hard drive

Post by etollerson »

I have been trying to find information on communicating with the IDE/ATA controller to access the hard drive. I have googled it to death and have looked on OS sites but still can't find the information. I have found bits and pieces but it is hard to put together. I was wondering if any of you know of a good site that explains it. I have found sites that state the registers and the commands but I don't know how that maps into the I/O memory location. Also, once it is there how do you let the hard drive know you're ready for it to look at the memory and do what was commanded? Obviously I am not talking about BIOS Int 13 and I don't want to use BIOS. I want to directly talk to the hard drive/controller.
Any help is greatly appreciated.

etollerson
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

I AM GO TO LET YOU IN TO A BIG SECRET :shock:
Hdd is a piece of p**s, but no where will tell you this, as they want you to think they are clever :wink:.
Now the harder part is the FAT part, not the hdd part.
Let say we want to load 512 byte from the primary device
We do this

Code: Select all

hdbase1   equ  0x1f0       ; 0x1f0 for primary device                              
hdid     equ  0x00     ; 0x00 for master hd
Then the code to read from hdd

Code: Select all

 ;====================================================;
 ; hd_read.                                           ;
 ;====================================================;
HddRead:                  ; eax block to read
        pushad
        push  eax
newhdread:
        mov edx,[hdbase1]
        inc   edx
        mov   al,0
        out   dx,al

        inc   edx
        mov   al,1
        out   dx,al

        inc   edx
        pop   ax
        out   dx,al

        inc   edx
        shr   ax,8
        out   dx,al

        inc   edx
        pop   ax
        out   dx,al

        inc   edx
        shr   ax,8
        and   al,1+2+4+8
        add   al,[hdid1]
        add   al,128+64+32
        out   dx,al

        inc   edx
        mov   al,20h
        out   dx,al
hddwait:
        in    al,dx
        test  al,128
        jnz   hddwait

        mov   edi,HddBuffer
        mov   ecx,256
        mov edx,[hdbase1]
        cld
        rep   insw

        popad
        ret
Now that is the only bit of code that is needed to read from hdd, all the rest is FAT code. I would let you into more SECRET code, but most people like to do it the hard way :cry: .
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

best site ive found, is www.ata-atapi.com -- it contains all the information about the ATA specification, and code samples, and links to the actual docs
etollerson
Posts: 2
Joined: Sun Oct 29, 2006 1:06 pm

Post by etollerson »

Thanks Dex, I had no idea that it was that easy. Now I will have to look into DMA even though I don't know if it is worth it. I finally put in some good search terms that yielded usable results. Through this forum I have found good links about ATA. It seems that talking to the CD-ROM is a real pain, but I am not concerned about that right now.

Also thanks to JAAman fo posting the web site. I also found that when looking earlier today.
User avatar
omin0us
Member
Member
Posts: 49
Joined: Tue Oct 17, 2006 6:53 pm
Location: Los Angeles, CA
Contact:

Post by omin0us »

http://www.t13.org/docs2004/d1532v1r4b-ATA-ATAPI-7.pdf

That is the official ATA-ATAPI 7 specs. Its not too hard to follow.
I was able to code a working ata driver using that primarily, and looking at a few examples from other kernels.
http://ominos.sourceforge.net - my kernel
#ominos @ irc.freenode.net
http://dtors.ath.cx - my blog
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Post by Pype.Clicker »

i strongly encourage anyone willing to run tests with HDDs to print and walk through the code offered (yep, that's PD code) on www.ata-atapi.com. It has it all: PIO, DMA and UDMA mode, for both ATA disks and ATAPI cdroms ... not so sure about SATA/SATAPI support, though.
Post Reply