Page 1 of 1

basic hdd read question

Posted: Tue Aug 29, 2006 3:07 am
by robert macabre
i feel like i must be retarded :] i couldn't get my floppy driver working, so i thought i should start on hdd and just take a break from the fd. guess what? i feel worse off than i was with the fd. i can't find much material. i checked bonafide and mega-tokyo and didn't see anything. i checked osrc and one of the links i found was to this code:

(the full code is at http://www.nondot.org/sabre/os/files/Disk/HD_PORTS.asm, but i think this covers it)

Code: Select all

	mov     dx,1f6h         ;Drive and head port
	mov     al,0a0h         ;Drive 0, head 0
	out     dx,al

	mov     dx,1f2h         ;Sector count port
	mov     al,1            ;Read one sector
	out     dx,al

	mov     dx,1f3h         ;Sector number port
	mov     al,1            ;Read sector one
	out     dx,al

	mov     dx,1f4h         ;Cylinder low port
	mov     al,0            ;Cylinder 0
	out     dx,al

	mov     dx,1f5h         ;Cylinder high port
	mov     al,0            ;The rest of the cylinder 0
	out     dx,al

	mov     dx,1f7h         ;Command port
	mov     al,20h          ;Read with retry.
	out     dx,al
still_going:
	in      al,dx
	test    al,8            ;This means the sector buffer requires
				;servicing.
	jz      still_going     ;Don't continue until the sector buffer
				;is ready.

	mov     cx,512/2        ;One sector /2
	mov     di,offset buffer
	mov     dx,1f0h         ;Data port - data comes in and out of here.
	rep     insw

;   ------

	mov     ax,201h         ;Read using int13h then compare buffers.
	mov     dx,80h
	mov     cx,1
	mov     bx,offset buffer2
	int     13h

	mov     cx,512
	mov     si,offset buffer
	mov     di,offset buffer2
	repe    cmpsb
	jne     failure
	mov     ah,9
	mov     dx,offset readmsg
	int     21h
	jmp     good_exit
failure:
	mov     ah,9
	mov     dx,offset failmsg
	int     21h
good_exit:
	mov     ax,4c00h        ;Exit the program
	int     21h

	readmsg db      'The buffers match.  Hard disk read using ports.$'
	failmsg db      'The buffers do not match.$'
so, i ported it to C just to see if it worked, and was really happy when i saw that the serial window said, "HDD read test success." turns out that was a stupid programming mistake, and it actually didn't work. what i got was 512 'FF's... if you know what i mean ;] ;]

i'm very tired right now, so if anyone could tell me why the above code does not work for a read of sector 0, i'd be very thankful.

oh, ps, i DIDN'T port over the stuff about using the bios to read and compare. i think the author was just using it to verify successful completion bla bla bla and i doubt it was important seeings how the purpose of code is to read with the ports. my success check was if the first 16 bytes on the read were 0-15 (first thing i thought of to put in the disk image :])

Posted: Tue Aug 29, 2006 8:08 am
by carbonBased
Uhhmm.... in what environment are you trying to run this code?

I ask, as this code isn't suitable for osdev, as it appears to be DOS code (int 21h is a DOS software interrupt).

Also, if you intend to use this in a protected mode OS, you'll have to remove the BIOS ints (13h).

Unfortunately, I don't have any HDD docs to forward (although searching for IDE ports on google might help... I believe I've found stuff like this before), but I can tell you that you'll be (usually) wanting to control the HDD via ports and/or DMA in an OS. This info is a little bit more difficult to find (and digest) but it is out there.

--Jeff

Posted: Tue Aug 29, 2006 7:54 pm
by bubach
Maybe some of the things I've collected can help you:
http://bos.asmhackers.net/docs/ata/

Posted: Wed Aug 30, 2006 1:57 am
by prajwal
for floppy driver, Intel has a doc which explains with flowchart for read/write, format, seek, recaliberate etc.... which is sufficient to write ur floppy driver

coming to HDD, u r right... there is no such convincing doc,

what bubach has posted is a good source....

Even i was there at a same state once.... whatever doc u read u won't get any/clear idea
on how to write the code for a protected mode os....
furthur, these devices basic operations are stereotypic.... i mean the steps invovled in init, read/ write, use dma/pio etc are defined and there not much creativity/invovative ideas that one can use while writting basics of these drivers... So, there is no harm in seeing other
os's source code in these areas.... LINUX Kernel source is the best....

Posted: Wed Aug 30, 2006 1:24 pm
by robert macabre
well, like i said, i took out the stuff about the int.

my cable modem broke and so i've been internet-less and it left me some time to go through all sorts of technical things i downloaded about the hdc and i couldn't find anything wrong with the code. i ran it on my actual computer and it worked just fine. it only doesn't work on qemu. weird.

but, i finally found what i was looking for in a monster of a hard disk reference by maxtor. i've got all the commands i could ever possibly want to reference and it's not as complicated as it looks :]

and thanks for the tip about the intel flowchart. i think i had seen it once before, but have no idea where so i'll have to look at it again.

Posted: Wed Aug 30, 2006 7:39 pm
by prajwal
u can find 82077A intel Floppy Controller Docs (in pdf format) at

1. http://www.nondot.org/sabre/os/articles ... iscDrives/
2. http://www.osdever.net/documents.php?cat=0&sort=1