问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/14785723/converting-the-cluster-number-stored-in-fat-table-of-fat12-filesystem-for-read