FAT16 File System

615 views Asked by At

I'm building a file system that resembles FAT. It has the following setup:

| MBR | FAT Area | Data Area |

So if given the sector_size (the size of a sector in bytes), cluster_size(the size of the cluster in sectors), and disk_size (the size of the disk in clusters), how can I calculate the length of the FAT Area?

I know that MBR starts at cluster 0, and FAT Area starts at cluster 1. But I don't know what information I can use to calculate the length of the FAT Area.

Also, The root directory starts at the first cluster in the Data Area, so how can I calculate the length of the root directory as well?

1

There are 1 answers

0
Spektre On

This is how FAT12 (MSDOS 6.22) floppy image looks like:

FAT12 image example

  • on the top left is NC/VC style view of the root directory
  • on the bottom left is decoded info
  • and on the right is surface map of sectors
  • the highlighted green sectors are MSODS.SYS file

Drive geometry

  • heads - number of used surface sides
  • tracks - number of tracks per head
  • sectors - number of sectors per track
  • cluster - size of cluster

FAT

  • sector_size is smallest unit of the drive
  • cluster_size is smallest unit of the FAT table
  • so cluster=N*sector where N={1,2,3,4,...}
  • the drive size is heads*tracks*sectors*sector_size
  • now the FAT table has to cover whole area usable by DATA (the whole green/gray stuff)
  • to be exact you need 1 value per cluster.
  • this value is encoded info about:
    • next cluster the file is continuing
    • that this is last cluster of the file
    • that cluster is reserved for system,free,bad,unformated,etc...
  • so first you need to decide how many bits you want to use per each value
  • for example 8-bit value limits you to cca 250 clusters per drive
  • that is not too much ...
  • this is limiting max number of files also and space wasting due to cluster size
  • some systems like FAT do not include whole drive area (just the data area)
  • so only the area between first and last logical cluster/sector is stored inside FAT
  • that saves up a bit space and frees more cluster indexes for DATA

FAT size

  • FAT can be of fixed size (like FAT12,MDOS,etc)
    • so FAT table should cover all possible entries which is 2^FAT_entry_bits
    • for 12bit FAT12 it is 2^12=4096 entries
    • to single sector fits floor(512*8/12)=341 entries
    • so for yor FAT you need ceil(4096/341)=12 sectors
  • or variable
    • in that case its length should be encoded somewhere like in MBR
    • the real question is then do you know the cluster size or FAT size ?
    • so use one of following:
    • FAT_entries=(drive_size-reserved)/cluster_size
    • cluster_size=(drive_size-reserved)/FAT_entries
    • where reserved is area not included in FAT (let it be 0 for now)
    • now let the FAT entry to be 16bit (2Byte)
    • and have a drive of size 2heads*1024tracks*64sectprs*512Byte=64MByte
    • and want to have cluster_size=4096Byte
    • so FAT_entries=64*1024*1024/4096=16*1024
    • now just compute the number of sectors/clusters needed for is as in above example