filesystemsfat16

Determining the starting sector of the root directory in a FAT16 drive


I want to offload some very large program variables out of memory, and into disk files.

I've read dozens of pages on INT 13H, CHS, the MBR, and the File Allocation Table, and i've learned almost everything I need to know, except I can't seem to find a working way to calculate the starting sector of the root directory.

According to this page, http://nerdclub-uk.blogspot.co.uk/2012/11/understanding-fat-tables.html, determining it is as simple as adding together the sector offset that contains the boot record, the number of reserved sectors, and the amount of sectors in each copy of the FAT.

After examining my boot sector I plugged in all of those values, and this is what I ended up with.

1+1+(2*256), which equals 514.

So, my root directory should begin at sector offset 514. The highest number of sectors per cylinder is 63.

514 / 63 = 8r10

So, my root directory should be at cylinder 8, sector 10.

However, there is just zeros in that sector, and all the sectors around it.

Obviously i'm doing something very wrong, but after a day of Googling i'm about ready to give up.


Solution

  • Newer drives use LBA (logical block addressing) so CHS doesn't come into play much anymore. You should be accessing sector 514 (LBA sector 513 as the first sector is 0 for LBA) from your computation. What's in that sector?

    As for CHS addressing, each cylinder consists of a number of heads (tracks) on each side of the platter(s) and each head (track) a number of accessible sectors around the disk.

    https://en.wikipedia.org/wiki/Cylinder-head-sector has a formula to translate (c,h,s) tuple to LBA sector value:

    CHS tuples can be mapped onto LBA addresses using the following formula:

    A = (c ⋅ Nheads + h) ⋅ Nsectors + (s − 1), where A is the LBA address, Nheads is the number of heads on the disk, Nsectors is the maximum number of sectors per track, and (c, h, s) is the CHS address.

    and LBA to CHS address conversion https://en.wikipedia.org/wiki/Logical_block_addressing#CHS_conversion

    LBA addresses can be mapped to CHS tuples with the following formula ("mod" is the modulo operation, i.e. the remainder, and "÷" is integer division, i.e. the quotient of the division where any fractional part is discarded):

    C = LBA ÷ (HeadsPerCylinder (HPC) × SectorsPerTrack (SPT))

    H = (LBA ÷ SPT) mod HPC

    S = (LBA mod SPT) + 1