When writing your own operating system, one of the first serious challenges you will face is supporting file systems. To load programs, configurations, or drivers, the OS must be able to read data from a storage medium. In this series of articles, we will trace the path from "bare metal" to a working file system, starting with a classic — FAT12.
Why FAT12?
For an OS developer, FAT12 is an ideal learning ground for several reasons:
Structural Simplicity: It lacks the complex trees or heavy metadata found in NTFS or ext4.
Predictability: The structure is rigidly defined and easy to implement at a low level.
Widespread Support: Almost any emulator (like QEMU) and BIOS easily work with FAT12 images.
The Anatomy of a FAT12 Disk
A FAT12 disk is organized into sectors (typically 512 bytes each). The layout is strictly sequential, consisting of several key areas:
1. The Boot Sector (BIOS Parameter Block)
The very first sector of the disk is the Boot Sector. It contains the "BIOS Parameter Block" (BPB), which is a collection of critical metadata that tells the OS how to interpret the rest of the disk. Key fields include:
Bytes per sector: Usually 512.
Sectors per cluster: How many sectors make up one logical unit (a cluster).
Reserved sectors: The number of sectors before the first FAT.
Number of FATs: Usually 2 (for redundancy).
Root Directory Entries: The number of slots available for the root directory.
2. The FAT (File Allocation Table)
The FAT is essentially a map of the disk. It is an array where each entry corresponds to a cluster on the disk.
If a FAT entry is 0x000, the cluster is empty.
If it contains a value (e.g., 0x003), it means this cluster is part of a file and the next cluster in the chain is cluster 3.
A special value (like 0xFFF) marks the End of Chain (EOC).
This "linked list" structure is what makes FAT12 so simple to implement, though it can lead to fragmentation over time.
3. The Root Directory
Unlike modern file systems where the root directory is just another folder, in FAT12, the root directory is a fixed-size area located immediately after the FATs. Each entry in the directory is 32 bytes long and contains:
Filename and Extension: Split into separate fields.
Attributes: Read-only, Hidden, System, Directory, etc.
Creation/Access Time and Date.
Starting Cluster: The first cluster of the file.
File Size: The total size in bytes.
4. Data Area
The rest of the disk is the Data Area, which is divided into clusters. This is where the actual content of your files resides.
How the OS Reads a File: The Workflow
- To read a file, your OS must follow these steps:
- Locate the Root Directory: Use the BPB info to find where the root directory starts.
- Search for the Filename: Scan the directory entries until you find a match.
- Get the First Cluster: Extract the "Starting Cluster" from the directory entry.
Follow the Chain:
- Read the data from the starting cluster.
- Look up that cluster's index in the FAT to find the next cluster.
- Repeat until you hit the End of Chain marker.
Understanding FAT12 is a fundamental step in learning how software interacts with hardware. Once you can successfully traverse the FAT and read directory entries, you have built the foundation for a real file system driver. In the next article, we will dive into the actual implementation in C/Assembly.