There are two major modes for addressing sectors on disk. One is called CHS mode. Sectors in this mode are grouped under heads. How many sectors are under one head is determined by the device parameter called SectorsPerHead. Further, heads are grouped under cylinders. How many heads are under one cylinder is determined by another device parameter, HeadsPerCylinder. The remaining disk parameter is NumberOfCylinders, which, together with HeadsPerCylinder and SectorsPerHead unambiguously determines disk capacity in CHS mode:
CHSCapacity=NumberOfCylinders*HeadsPerCylinder*SectorsPerHead*512
Needless to say, neither of disk parameters should be equal to zero. If any of them is equal to zero, such disk should be considered invalid
You need Cylinder, Head, and Sector to identify the sector in CHS mode. Identification is unique as long as you stay in the same CHS translation mode. However, it is not unique across different translation modes. Devices, in conjunction with BIOS, may translate Cylinder and Head values to make it possible for BIOS to access large amounts of data. The mechanism of this translation is discussed below. Head and cylinder numbers always start from zero, sector numbers start from one. You will likely perform some arithmetic operations on CHS values. The formulas below will help you. Note that you can use them only if you know disk parameters.
To add S sectors to some CHS address follow these steps:
Sector=Sector+S;
Head=Head+(Sector div SectorsPerHead);
Sector=Sector mod SectorsPerHead;
Cylinder=Cylinder+(Head div HeadsPerCylinder);
Head=Head mod HeadsPerCylinder;
You should always check if the resulting Cylinder is less than NumberOfCylinders. If this is not the case, the data on disk that made you do this calculation was corrupted.
To subtract one sector from the given CHS address follow these steps:
if Sector=0
Sector=SectorsPerHead-1;
if Head=0
Head=HeadsPerCylinder-1;
if Cylinder=0
error, data was corrupted
else
Cylinder=Cylinder-1;
else
Head=Head-1;
else
Sector=Sector-1;
The second mode for addressing sectors is called LBA. It stands for Linear Block Addressing, which means exactly that. You need only sector number to address the sector. Linear sector numbers start from zero. Disk capacity in LBA mode is determined by NumberOfSectors, which is certainly device parameter. The following equality must always hold:
LBA=(Cylinder*HeadsPerCylinder+Head)*SectorsPerHead+Sector-1
Thus, CHS (0, 0, 1) corresponds to LBA (0). One might think that CHS (NumberOfCylinders-1, HeadsPerCylinder-1, SectorsPerHead) corresponds to LBA (NumberOfSectors-1), but this is not the case. In practice, one can usually address slightly more sectors in LBA mode than he does in CHS mode. According to my rules described earlier, you should ignore extra LBA sectors that might exist in CHS partitions. Refer to EIDE or SCSI documentation for the ways of determining physical disk parameters. Refer to your BIOS manual for the way of determining logical disk parameters.
There are two key operations that can be performed on sectors in any mode: read and write. Refer to the manuals described above for the ways of performing these operations. From now on, you must be able to read and write sectors given their LBA or CHS address.
Physical Number of Cylinders | Physical Heads Per Cyl | Physical Sectors Per Head | Power of Two for Translation |
1..1024 | 1..256 | 1..64 | 1 |
1025..2048 | 1..128 | 1..64 | 2 |
2049..4096 | 1..64 | 1..64 | 4 |
4097..8192 | 1..32 | 1..64 | 8 |
8193..16384 | 1..16 | 1..64 | 16 |
16385..32768 | 1..8 | 1..64 | 32 |
32769..65536 | 1..4 | 1..64 | 64 |
It is obvious that with some values of NumberOfCylinders disk may have different capacity in different CHS modes. However, I have never seen this in practice. The situation when physical disk parameters do not fit in this table is only possible if disk capacity is more than 8 GB or when disk has more than 64 sectors per head. Such disks are not yet handled by BIOS correctly. That is why I cannot provide any guidelines for these cases.