ATA Identify Device

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
jsaiko
Posts: 5
Joined: Mon Apr 30, 2007 9:59 am

ATA Identify Device

Post by jsaiko »

Hi All. Im attempting to write an ATA device driver and im having issues with the Identify Device command. Im just trying to get a sector count of connected drives. I read through the specs and all but it just isnt adding up. I dont have the code in front of me but It stats like bytes 59 and 60 are suppose to give the sector count. When I multiply this number I get by 512... (512 bytes per sector) The result is no where near the correct value of the disk size. Any idea what im doing wrong?
User avatar
mystran
Member
Member
Posts: 670
Joined: Thu Mar 08, 2007 11:08 am

Post by mystran »

What size is the disk, and what values are you getting?

If you are getting too small values for a large disk, there are various limit at various BIOS and such which cause you to get limited sizes.

Disk size with traditional addressing is cylinders*heads*sectors*blocksize, and without having bothered with ATA yet, I can tell that LBA32 or LBA48 addressing won't fit in 2 bytes... so get the cylinders and heads as well, or switch to LBA.
The real problem with goto is not with the control transfer, but with environments. Properly tail-recursive closures get both right.
jsaiko
Posts: 5
Joined: Mon Apr 30, 2007 9:59 am

Post by jsaiko »

Ok, Its actually bytes 60 and 61 but the values are too small... Im using a vmware 8gig disk. If it is not typical to get the size of the disk from the sector count using the ECh command please point me in the right direction.
jsaiko
Posts: 5
Joined: Mon Apr 30, 2007 9:59 am

Post by jsaiko »

Ok when I say bytes I actually mean 16bit words :D

So in theory LBA32 would be able to fit a sector count in 2 words... However this obviously wont work with LBA48.
jsaiko
Posts: 5
Joined: Mon Apr 30, 2007 9:59 am

Post by jsaiko »

My dumb @$$ figured it out using the cylinders*heads*sectors*blocksize method. I was using 32bit integers and the math got messed up. using a double fixed it.
grimpy
Posts: 11
Joined: Thu May 03, 2007 11:27 am

Post by grimpy »

Do you mean you used the C type 'double' ? You shouldn't do that in a kernel, because you'll screw up the fpu state for user apps.
jsaiko
Posts: 5
Joined: Mon Apr 30, 2007 9:59 am

Post by jsaiko »

well if I cant use double what would be appropriate? a 32bit integer isnt large enough to hold a byte count.
Aali
Member
Member
Posts: 58
Joined: Sat Apr 14, 2007 12:13 pm

Post by Aali »

i know its not standard, but you could use long long, which is guaranteed to be atleast 64 bits
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

Aali wrote:i know its not standard, but you could use long long, which is guaranteed to be atleast 64 bits
or if you use gcc + extensions use:

Code: Select all

typedef signed int64s      __attribute__((mode(DI)));
Guaranteed to be 64 bits :wink:
Author of COBOS
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

well if I cant use double what would be appropriate? a 32bit integer isnt large enough to hold a byte count.
It's not a byte count. It's a sector count. In other words, the 'byte count' is:

Code: Select all

2^32 * (byts_per_sec)
Which, in a 512-byte allocation, is a maximum of 2097152 MB, or about 2048 GB, or 2 TB. (correct my maths if it's wrong).

The hardware has no notion of 'bytes per sector', it's giving you the count of sectors on the disk.

Edit: so far, desktop OS's don't have to handle anything more than really 500 GB... Once drive sizes are > 2 TB the whole ATA specification will have to be redone. Can't wait :P
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Post by Brendan »

Hi,
pcmattman wrote:Edit: so far, desktop OS's don't have to handle anything more than really 500 GB... Once drive sizes are > 2 TB the whole ATA specification will have to be redone. Can't wait :P
Fortunately, you don't need to wait...

The current ATA specification already has support for 48-bit LBA addressing and 48-bit total number of sectors (see "words(103:100)" or the 8 bytes at offset 0xC8 in the "Identify Device" data structure). They won't need to change things until we reach 131072 TiB disks.

Even then the existing "Identify Device" data structure could easily handle a 64-bit total number of sectors, and the existing functions for 48-bit LBA could be extended to 52-bit LBA by defining 4 reserved flags.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

The way I use identify device it's 4 bytes for the total sector count (32-bit). Maybe I'm interpreting the specifications wrong.

I probably am, given the fact that my ATA code doesn't work on real PCs.
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Yeah it is 4 bytes for the Total Number of User Addressable sectors. Its location is at words 60 & 61.
Post Reply