Floppy Disk - Detect valid FAT12 filesystems

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Floppy Disk - Detect valid FAT12 filesystems

Post by Combuster »

128MB = 262144 sectors > 16 bits. Sounds like 1K clusters and a valid FAT32 system.

And yes, the number of clusters must correspond to the amount of bits in the FAT as per the standard.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Prochamber
Member
Member
Posts: 100
Joined: Wed Mar 13, 2013 2:27 am

Re: Floppy Disk - Detect valid FAT12 filesystems

Post by Prochamber »

Combuster wrote:128MB = 262144 sectors > 16 bits. Sounds like 1K clusters and a valid FAT32 system.

And yes, the number of clusters must correspond to the amount of bits in the FAT as per the standard.
If you're talking about the 'Sectors per FAT' field, this doesn't always bear an exact correspondence. For example a floppy disk has 2880 logical sectors, each 512 bytes and there is one sectors per cluster. Subtract the 31 clusters used by FAT, root directory, etc there is 2849 usable clusters. That would take 4273.5 bytes to address in FAT12 which is 8.34 clusters.

Sectors per FAT requires whole clusters, so it needs to be rounded up to nine to address the whole disk. Sure, the sectors at the end of the FAT don't actually exist but you can compare the cluster offset to the number of logical sectors to find the end point.

If our driver compares the size of the FAT to the total clusters we get:
(9 * 512) / 2849 = 1.62

Therefore dividing the size of the FAT by the Sectors Per FAT only gives an approximate answer.
That's a good case, I've seen 1.44 MB floppy disk images produced by certain companies that only address ~800 KB in the FAT.
TachyonOS - Violates causality on 95% of attempts. Runs at approximately 1.5c.
AbstractYouShudNow
Member
Member
Posts: 92
Joined: Tue Aug 14, 2012 8:51 am

Re: Floppy Disk - Detect valid FAT12 filesystems

Post by AbstractYouShudNow »

If you meant how to determine if a disk contains FAT (of any type) or another filesystem, then interpret the first sector as containing a BPB, and perform these checks:
  • Check if the BS_OEMName is composed of printable characters
  • Check that BPB_BytsPerSec is the count of bytes per sector on your drive (which your driver can provide)
Anyways, don't try to check whether the disk geometry values match these of the device.

But the real way to do that, or at least the way Windows does that, is to check whether BPB_Media equals the low byte of the first FAT entry (fat[0]).

If you wonder how I know this, then no I haven't retro-engineered Windows, but
  • This is how the specification tells, in the description of BPB_Media :
    The only other important point is that whatever value is put
    in here must also be put in the low byte of the FAT[0] entry. This
    dates back to the old MS-DOS 1.x media determination noted
    earlier and is no longer usually used for anything.
  • When implementing my FAT driver, I made hard tests to see what is considered as a FAT disk by Windows (I only had this at the time), and tried to mount various disk images which contained a FAT filesystem in which I had introduced an error, and the only time Windows did not detect a FAT drive was when this did not hold true. Similar practice showed that Windows doesn't care about the BS_VolLab field of the BPB (Win7 at least).
PS: For reference, http://staff.washington.edu/dittrich/misc/fatgen103.pdf (for the spec)
Post Reply