2

I'm writing a two stage bootloader for a FAT12 filesystem. The stage1 of the bootloader loads the stage2 from a floppy disk which is in FAT12 filesystem. Now I am having problem converting the cluster number (that I obtain from the FAT table) to a format containing the track, head and sector number. I was following the tutorial http://www.brokenthorn.com/Resources/OSDev6.html for making the bootloader.

My confusion here is that in the tutorial the Cluster Number obtained from the FAT is converted to LBA(Linear Block Address) format first and then converted to CHS(Cylinder Head Sector) Format before reading the sector into memory.

Why can't I directly convert the Cluster Number into CHS format?? Does the FAT table not store the Cluster Numbers linearly?? I want to know exactly what i am missing here??

The link to the source code of the bootloader used in the tutorial is at the end of the page of the link http://www.brokenthorn.com/Resources/OSDev6.html.

2 Answers2

3

The cluster numbers are linear but - as has been previously mentioned - are relative to the data area, with cluster 2 being the first cluster of the data area. Reads from the disk, however, are in terms of disk sectors, and each FAT cluster may contain multiple sectors - this is what the conversion to LBA is for - to convert from a cluster number to a sector number (so, subtract 2 from the required cluster number - to account for cluster 2 being the first cluster of the data area, then multiply by the number of disk sectors per cluster, and then add the number of disk sectors in use before the data area, to come up with the absolute disk sector(s) where our data is stored.

The older BIOS int 0x13 functions didn't read from the disk in terms of absolute sectors though - they read a specifc sector, by a specific head, on a specific cylinder (http://en.wikipedia.org/wiki/Cylinder-head-sector). Therefore if you use these functions you need to take the added step of working out which cylinder/head/sector corresponds to the absolute sector you want to read. An alternative is - if available - to use the extended read int 0x13 function, which takes absolute sector (LBA) addresses directly.

Michael
  • 1,136
  • 1
  • 7
  • 15
1

LBA and CHS are used to uniquely identify all physical sectors on the disk.

The cluster number, OTOH, only makes sense within a partition, it's relative to the beginning of the partition (its data area) and it can uniquely identify blocks of more than one sector within the partition.

So, there's quite some difference between the two things, albeit their function is similar.

And the hardware (or the BIOS) does not and should not know anything about clusters. So, you have to convert cluster numbers into LBAs (and then possibly into CHS) in order to access data on the storage device.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • When you say that "the cluster number,...., is relative to the beginning of the partition(its data area)..." do you mean that the cluster number in the FAT table is relative to the beginning of the data region of the FAT12 that comes after the root directory?? – Saurav Deb Purkayastha Feb 09 '13 at 07:50
  • See the official documentation from Microsoft on FAT12/16/32, [fatgen103.doc](http://msdn.microsoft.com/en-US/windows/hardware/gg463084). – Alexey Frunze Feb 09 '13 at 08:10
  • I am a bit confused on this. What i have understood is that the FAT provides a cluster number relative to the start of data area. So now if my data area starts at cluster 32 (the bootsector, FAT1, FAT2, RootDirectory use 32 sectors and I have 1 sector/cluster) and I got a cluster number 4 from the FAT table then how do i calculate the linear address of the cluster? If i put this in the formula (cluster-2)*sectorsPerCluster I get the answer as cluster 2 which is obviously not correct. Can you please tell me exactly what the cluster number in the FAT table is?? – Saurav Deb Purkayastha Feb 09 '13 at 08:40
  • An obvious way in this situation would be to add the cluster number from FAT to the start cluster of data area(i.e.31). But i know this is the wrong way. I am not able to exactly understand what kind of cluster number the FAT is providing. Please HELP! – Saurav Deb Purkayastha Feb 09 '13 at 08:48
  • Cluster numbers begin at 2, AFAIR, with 0 and 1 being reserved. Cluster 2 is the first cluster within the data area. It does not reflect the size of the preceding parts such as the boot sector, the copies of the allocation table and the root directory. – Alexey Frunze Feb 09 '13 at 08:49
  • Okay.. So when I have a cluster number 4 that means I need to access Cluster 4 of the data area? But when I see the disk as a whole and want to access the cluster 4 shouldn't I be doing startCluster(data area) + Cluster Number?? [Sorry for not understanding it easily!] – Saurav Deb Purkayastha Feb 09 '13 at 08:58
  • You subtract 2 from the cluster number, multiply that by the number of sectors per cluster and that's the sector number within the data area. `FirstSectorofCluster = ((N – 2) * BPB_SecPerClus) + FirstDataSector;` is the formula in the document. Start reading the document. It's all there. Read carefully and think. – Alexey Frunze Feb 09 '13 at 09:30
  • I get it now! I missed this line you said "Cluster 2 is the first cluster within data area". I thought Cluster's 0 and 1 are reserved in the data area. But infact Cluster "numbers" 0 and 1 are reserved and therefore Cluster number 2 corresponds to 1st cluster in the data area and hence the (N-2) in the formula!! Thanks @Alexey Frunze.. :-) – Saurav Deb Purkayastha Feb 09 '13 at 10:26
  • Only those two values are reserved and the two corresponding entries in the allocation table itself. Data sectors aren't wasted because of this "2". – Alexey Frunze Feb 09 '13 at 10:28