How FAT16 Works

FAT16 is one of the simplest disk filesystems ever to see wide use. It originated with MS-DOS in the early 1980s and went on to run on a huge range of home computers and embedded systems of that era — including, on the Atari side, TOS. Despite its age, it's still simple enough to hold entirely in your head, which makes it a good filesystem to actually understand rather than just use.

This article walks through how a FAT16 volume is laid out on disk, what each part is for, and how the pieces work together to turn a stream of sectors into files and folders.

The physical layout

A FAT16 volume is divided into four regions, always in this order: a boot sector, two copies of the File Allocation Table, a fixed-size root directory, and the data area. Everything up to and including the root directory is addressed in sectors; from the data area onward, addressing switches to clusters — a cluster being one or more sectors grouped together as the smallest unit a file can occupy.

Boot 1 sector FAT #1 2 sectors FAT #2 2 sectors, backup copy Root directory 4 sectors, fixed size 64 entries × 32 bytes Data area clusters 2, 3, 4, 5, ... for as long as the volume runs everything here is addressed by cluster number, not sector sector 0 sector 1 sector 5 sector 9 = cluster 2

For reference, here is the same layout at the level of individual sectors, for a volume with 4 sectors per cluster:

SECTOR #
========
  0      +-----------------------------+
         | Boot sector (BPB + code)    | 512 bytes
         +-----------------------------+

  1      +-----------------------------+
         | FAT #1                      |
  2      | (2 sectors)                 |
         +-----------------------------+

  3      +-----------------------------+
         | FAT #2                      |
  4      | (2 sectors, backup copy)    |
         +-----------------------------+

  5      +-----------------------------+
         | Root directory              |
  6      | (4 sectors, fixed size)     |
  7      | 64 entries × 32 bytes      |
  8      |                             |
         +-----------------------------+

  9      +=============================+
         | Data area begins here       |
         | (= cluster 2)               |
         +=============================+

The boot sector and the BPB

The very first sector holds a small amount of boot code plus the BIOS Parameter Block (BPB): a table of fields describing this particular disk, so that software doesn't have to hard-code sector sizes or disk geometry. Here are its key fields, in the order they appear in the sector:

OffsetSizeFieldWhat it means
0x0B2BPB_BytsPerSecBytes per sector — almost always 512.
0x0D1BPB_SecPerClusSectors per cluster. A bigger cluster means a smaller FAT and less bookkeeping overhead, but more wasted space ("slack") at the end of small files.
0x0E2BPB_RsvdSecCntReserved sectors before the first FAT. On a simple FAT16 volume this is just the boot sector itself, so it's normally 1.
0x101BPB_NumFATsNumber of FAT copies — always 2: one working copy, one backup.
0x112BPB_RootEntCntNumber of root directory entries. Fixed at format time — see "The root directory is special" below.
0x132BPB_TotSec16Total sectors on the volume, for volumes small enough that this 16-bit field doesn't overflow.
0x151BPB_MediaDisk type byte, e.g. 0xF8 for a fixed disk. This same byte reappears as the low byte of FAT entry 0 (see below).
0x162BPB_FATSz16Sectors occupied by one copy of the FAT.
0x204BPB_TotSec32Total sectors, as a 32-bit value, used instead of BPB_TotSec16 once the volume is too large for that 16-bit field.

Why "16"? Each FAT entry is 16 bits wide, so a FAT16 volume can address at most about 65,524 clusters. Fewer clusters than roughly 4,085 and the same basic idea becomes FAT12 instead (12-bit entries, used on floppy disks); more than about 65,524 clusters and you need FAT32. The name is simply the entry width.

From sectors to clusters

Cluster numbering starts at 2, not 0, because FAT entries 0 and 1 are reserved rather than describing real clusters: entry 0 holds a copy of the media descriptor byte, and entry 1 is reserved too. So the first cluster of actual data is always numbered 2, whatever the cluster size is. With 4 sectors per cluster, for example:

ClusterSectorsContents
29 – 12First data cluster
313 – 16
417 – 20
521 – 24
625 – 28
...

The FAT: a linked list of clusters

This is the idea the whole filesystem is named after. A directory entry for a file stores only two things about where its data lives: the number of its first cluster, and its size in bytes — nothing more. To find the rest of a file that spans more than one cluster, you look up that first cluster's number as an index into the FAT. The value stored there is one of:

  • the number of the next cluster — the file continues there;
  • an end-of-chain marker, 0xFFF8–0xFFFF — this was the last cluster of the file;
  • 0x0000 — the cluster is free, not part of any file;
  • 0xFFF7 — a bad cluster, not to be used.

Following that chain, one link at a time, is how an entire file is read — exactly like walking a linked list. It's also why a damaged FAT is so dangerous: a file's bytes can be perfectly intact on disk, but if the chain linking its clusters together is destroyed, there's no way left to know which clusters belonged to which file.

The root directory is special

Unlike every other directory on the volume, the root directory doesn't live in the data area as an ordinary cluster chain — it has a fixed size and a fixed location, computed directly from BPB_RootEntCnt. That's why it has a hard limit on how many files and folders it can hold. Subdirectories, by contrast, are just ordinary files flagged with the directory attribute, made of ordinary cluster chains in the data area — so they can grow as large as needed.

Each directory entry, root or otherwise, is 32 bytes and includes:

FieldWhat it holds
Filename8.3 format (8 characters, a dot, a 3-character extension)
Attribute byteflags such as DIR (it's a subdirectory) or ARCH (an ordinary file)
First clusterwhere to start following the FAT chain
File sizein bytes; meaningless for a directory, which is sized by its cluster chain instead

A worked example

Suppose a disk holds this:

C:\
 +- AUTO        (directory)
 |   +- INIT.PRG   (1500 bytes)
 +- README.TXT     (600 bytes)

The root directory (sectors 5–8) has one 32-byte entry per item:

ENTRY #
========
[0]  AUTO        ATTR=DIR   FIRST_CLUSTER=2
[1]  README.TXT  ATTR=ARCH  FIRST_CLUSTER=3
[2]  0x00 -> end of directory

and the FAT itself looks like this:

FAT entryValueMeaning
[0]0xFFF8Media descriptor (reserved)
[1]0xFFFFReserved
[2]0xFFFFAUTO directory (1 cluster)
[3]0xFFFFREADME.TXT (1 cluster)
[4]0x0005INIT.PRG → continues at cluster 5
[5]0xFFFFINIT.PRG ends here
[6]0x0000Free
[7]0x0000Free
...

Here's what following those chains looks like for the two files. First, the simple case — README.TXT fits in a single cluster:

Directory entry README.TXT first_cluster=3, 600 bytes Cluster 3 the file's data (600 bytes) FAT[3] = 0xFFFF read cluster 3 check FAT[3]

0xFFFF means there's no next cluster — this short file fits entirely in a single cluster.

Now the more interesting case — INIT.PRG, which spans two clusters:

Directory entry INIT.PRG first_cluster=4 1500 bytes Cluster 4 first part of INIT.PRG's data FAT[4] = 0x0005 → cluster 5 Cluster 5 rest of INIT.PRG's data FAT[5] = 0xFFFF read cluster 4 check FAT[4] more data: go to cluster 5 check FAT[5]

FAT[4] pointing to cluster 5 is the chain; FAT[5] = 0xFFFF marks the end. That's the entire algorithm for reading a multi-cluster file — repeated for as many clusters as it takes.

One more piece completes the example: AUTO is a subdirectory, which means it's really just a file whose "contents" are more directory entries, stored in cluster 2:

OFFSET
========================================
0x0000  "."        ATTR=DIR  FIRST_CLUSTER=2
0x0020  ".."       ATTR=DIR  FIRST_CLUSTER=0
0x0040  "INIT.PRG" ATTR=ARCH FIRST_CLUSTER=4
0x0060  0x00 -> end of directory

This is the same trick recursively: AUTO's own directory entry in the root points to cluster 2, and cluster 2 is read exactly like any other cluster — it just happens to contain more 32-byte directory entries rather than file data. "." points back at AUTO's own first cluster, and ".." points at its parent (cluster 0 is a convention meaning "the root directory", which isn't a numbered cluster at all).

Finding where things start: the formulas

Everything above can be computed directly from the BPB, without ever reading the directory structures first:

reserved_sectors = BPB_RsvdSecCnt;

fat1_start = reserved_sectors;
fat2_start = fat1_start + BPB_FATSz16;

root_start =
    BPB_RsvdSecCnt
  + BPB_NumFATs * BPB_FATSz16;          // where the root directory begins

root_sectors =
    (BPB_RootEntCnt * 32 + BPB_BytsPerSec - 1)
    / BPB_BytsPerSec;                    // how many sectors the root directory needs

first_data_sector =
    root_start + root_sectors;           // where cluster 2 begins

first_data_sector is the one number everything in this article has been building up to: every cluster number N in the data area maps to sector first_data_sector + (N - 2) * BPB_SecPerClus.

The golden rules

  • Boot sector = 1 sector.
  • FATs = sectors, two copies.
  • Root directory = fixed-size sectors, not clusters.
  • Clusters only exist in the data area.
  • Cluster 2 is always the first data cluster.
  • A subdirectory is just a file whose contents are more directory entries.