Page 1 of 1

[Solved] Calculate Disk Geometry from Disk Size

Posted: Fri Nov 16, 2012 11:16 pm
by trinopoty
How can one calculate the disk geometry with nothing but the size of the disk in sectors.
Without even the clue if the disk is removable or fixed or floppy.

Re: Calculate Disk Geometry from Disk Size

Posted: Fri Nov 16, 2012 11:46 pm
by Brendan
Hi,
trinopoty wrote:How can one calculate the disk geometry with nothing but the size of the disk in sectors.
Without even the clue if the disk is removable or fixed or floppy.
You can't. Consider this formula:

x * y * z = total;

If you know the value of 3 variables you can determine the fourth/missing value. If you only know the value of 2 of the variables then you're stuck, and if you only know the value of one variable you're stuck twice as much.

However; you do know that x, y and z are positive integers. This rules out some of the billions of possible solutions. For example, if the total is 13 then you know that x can't be 4 because 13 /4 is not an integer.

Anything beyond that is guesswork and/or estimation.


Cheers,

Brendan

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 12:00 am
by trinopoty
We can narrow down the possibilities by considering that a fixed disk is at-least 5 MB. Anything lower than that is removable/floppy disk.
After that, the size can be compared to standards in case of removable/floppy media.

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 12:44 am
by Brendan
Hi,
trinopoty wrote:We can narrow down the possibilities by considering that a fixed disk is at-least 5 MB. Anything lower than that is removable/floppy disk.
After that, the size can be compared to standards in case of removable/floppy media.
For floppies it's easy enough if you avoid the extremely old stuff (e.g. there's 2 different "320 KiB" formats - one for 5.25 inch disks and one for 3.5 inch disks) and also avoid the unusual stuff (e.g. nothing prevents a 3.5 inch disk from being formatted as a 1200 KiB disk).

For hard disks you could create a database of every hard drive ever manufactured, and that would narrow down the options a lot. Of course that won't solve most cases completely; and then there's RAID arrays to consider (e.g. a pair of 100 GiB physical drives using striping and pretending to be a 200 GiB logical drive, or a set of four 100 GiB drives pretending to be whatever it works out to for RAID5).

In any case, it's best/easiest to ask the device driver.


Cheers,

Brendan

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 1:58 am
by Kazinsal
Solution: don't use disk geometry if you can avoid it. Most computers with at least a 486 in them will support LBA extensions in BIOS interrupts. After that you can just use LBA directly through the disk controllers.

If you need disk geometry that badly, the BIOS should be able to tell you. Failing that, I think ATA IDENTIFY will do what you want.

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 8:05 am
by trinopoty
Well the problem is, I am working with disk images under windows.
Disk images provide only one info, the size in sectors. I have to somehow use that info to calculate the geometry.

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 3:38 pm
by Brendan
Hi,
trinopoty wrote:Well the problem is, I am working with disk images under windows.
Disk images provide only one info, the size in sectors. I have to somehow use that info to calculate the geometry.
That creates a whole new level of headaches - calculate the geometry for what?

If you're calculating the physical disk's geometry for the file itself, then you don't have a physical disk and the only thing that might make sense is "sectors per track = total size; heads = 1, cylinders = 1".

If you're calculating what the physical disk's geometry might become in a specific emulator; then you can either look at the emulator's source code (e.g. Bochs, Qemu) or run a series of tests on that emulator (if it's closed source), and determine how the emulator itself calculates the disk's geometry from the image's size. In this case, an alternative option would be to ask the emulator (e.g. use the ATA "Identify" command to ask the emulated disk what it is).

If you're calculating the fake geometry that a (real or emulated) BIOS might use (e.g. for the old/obsolete "int 0x13" functions or for an MBR or partition table) and not calculating the disk's physical geometry at all; then the same process (look at the BIOS's source code or run a series of tests on that specific BIOS) would work. Note that the calculation that a specific BIOS uses may or may not depend on the physical disk's geometry; and that different BIOSs may do the calculation completely differently. In this case, an alternative option would be to ask the BIOS (e.g. use the old "get drive parameters" function).


Cheers,

Brendan

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 3:57 pm
by SparrowOS
CHS<28 LBA<48 LBA

Re: Calculate Disk Geometry from Disk Size

Posted: Sat Nov 17, 2012 10:25 pm
by trinopoty
Well then the best option is to set the CHS of MBR to invalid i.e. 0xFFFFFF, and force everything to use LBA or do their own math based on BIOS reported geometry.

Re: Calculate Disk Geometry from Disk Size

Posted: Sun Nov 18, 2012 4:30 am
by Yoda
This task is unsolvable for more reasons than just a multitude of X*Y*Z=N solutions.
There are three other reasons:
1. There are two geometries - BIOS and physical geometry. Physical geometry is limited to 16 heads, while BIOS limited to 256 (and old MS-DOS implementations are limited to 255). So, which geometry we need to find?
2. BIOS gets geometry converted from physical. But there are several methods of conversion. You'll get different geometries by using different methods.
3. The equation to solve is not necessarily X*Y*Z=N. More correct is to solve X*Y*Z>=N, since drive may have actual number of sectors slightly less than product C*H*S. Last track is not full.

Re: Calculate Disk Geometry from Disk Size

Posted: Sun Nov 18, 2012 4:47 am
by bluemoon
Furthermore, there is not necessary a unique solution; there may be multiple solutions and it's up to the BIOS or whoever got the numbers to interpret the them.

Re: Calculate Disk Geometry from Disk Size

Posted: Sun Nov 18, 2012 7:05 pm
by linguofreak
Brendan wrote: If you're calculating the fake geometry that a (real or emulated) BIOS might use (e.g. for the old/obsolete "int 0x13" functions or for an MBR or partition table) and not calculating the disk's physical geometry at all; then the same process (look at the BIOS's source code or run a series of tests on that specific BIOS) would work. Note that the calculation that a specific BIOS uses may or may not depend on the physical disk's geometry; and that different BIOSs may do the calculation completely differently. In this case, an alternative option would be to ask the BIOS (e.g. use the old "get drive parameters" function).
This may end up with a catch-22 though: Some emulators ask the user to supply the geometry for disk images.