Leanpub Header

Skip to main content

FAT12

Understanding and Implementing the Classic File System in C

FAT12
This book is 100% completeLast updated on 2026-10-17
+26,497 words in the last 30 days

Stop guessing what the bytes on disk mean and build the filesystem yourself. Over twelve chapters you implement FAT12 from scratch in C, from the first sector read to formatting a fresh volume, and check every step against a hexdump. Then you boot the exact same code inside your own kernel and watch it write and read files on bare metal.

Minimum price

$19.99

$29.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
304
Pages
70,969Words
About

About

About the Book

Every file you have ever opened was a handful of bytes on a disk, plus a filesystem that knew where to find them. This book shows you how that works by having you build one yourself.

FAT12 is the filesystem DOS started with, and it's the best one to learn on. You can hold the whole thing in your head, and it still runs on real hardware. Over twelve chapters you implement it from scratch in about 1,500 lines of C: reading, writing, creating directories, deleting, moving and formatting. Then comes the payoff. You boot that same code, unchanged, inside your own kernel in QEMU, and watch it format a volume, write a file and read it back. There's no operating system underneath and no filesystem code you didn't write yourself.

Small filesystem, big reach

FAT isn't a museum piece. Almost every PC sold in the last decade boots from a FAT volume, because UEFI requires its system partition to be FAT. USB sticks, SD cards and the floppy images many OSDev tutorials boot from all use it too. FAT12 strips the design down to its essentials: one table that links every file's pieces together, and a root directory at a fixed address. Once you understand FAT12, you already understand most of FAT16 and FAT32.

Prove every byte

You never have to take the code's word for it. You decode every structure from a real volume created by mkfs.fat. Then you hexdump the disk image, trace what your code did and compare the two. Once you start writing, Linux's own FAT driver checks your work, and it has never seen your code. Every chapter ends with code that compiles with zero warnings and a command you run to watch it work.

The part other tutorials skip

FAT12 packs its table entries into 12 bits, so they don't line up with byte boundaries. That's where most tutorials gloss over the details, and where most hand-rolled implementations quietly break. Get it wrong and nothing crashes: you silently overwrite four bits of a neighboring entry, and some other file comes back as garbage weeks later. Chapter 5 takes the packing apart bit by bit until it's obvious. Once you know exactly where those twelve bits belong, you can review any FAT code.

Write it so a crash can't wreck it

Every write touches more than one place on the volume, so the order of those writes decides what a crash can break. Delete a file in the wrong order and an untimely crash leaves two files silently corrupting each other. In the right order, the worst a crash can do is leak a little free space. The book walks through the crash scenarios for each operation, so you see why each write comes first.

What you'll build

- A portable block device layer, so one FAT12 implementation runs on a disk image, a RAM disk and a real ATA drive
- Mounting, directory listing and full path walking through nested subdirectories
- Multi-cluster reads that follow a file's chain through the File Allocation Table
- Writing files, creating directories, deleting and moving, all ordered to limit what a crash can damage
- A formatter that creates a fresh FAT12 volume from nothing
- A bootable Multiboot kernel that runs it all on bare metal

Appendixes add an ATA driver so your kernel writes to a real (emulated) disk, an introduction to long filenames (LFN/VFAT), a tour of the boot sector fields the chapters don't use, and a side-by-side comparison of FAT12, FAT16 and FAT32.

Who this is for

This book is for OSDev hobbyists whose kernel boots and prints to the screen, and who now want it to load files. It's also for anyone who has ever wondered what's actually on a disk. You need solid C (pointers, structs, bitwise operations), and that's it. Everything up to the final chapter runs as a command-line tool on your own machine, so you don't need a kernel to start. The only assembly in the main chapters is a 27-line Multiboot stub.

If you've tried this before, you know the usual material. Wiki articles stop once a file can be read, tutorials switch to assembly halfway through, and the spec gives you formulas without showing how they fit together. This book goes the whole way, from the first sector read to formatting a fresh volume, all in C. The full source for every chapter is on GitHub (github.com/SoftwareFreak1/FAT12), so you can look at the code before you buy. The book is where you learn why each line is there.


Start with the free sample. It covers Chapters 1–4, from the first raw sector read to your own cat command, and stops right where Chapter 5 opens up the File Allocation Table and its 12-bit packing. If those four chapters work for you, the rest of the book will too.

Author

About the Author

Björn Götz

I'm Björn Götz, a software developer from a small village in Hesse, Germany. I taught myself to program at twelve, started a three-year software developer apprenticeship at sixteen, and have been building software professionally ever since. Most of that work is backends, plus full-stack web and mobile apps and the cloud infrastructure they run on.

After hours I go the other direction: operating systems, assembly, networking and emulators. I once wrote a CHIP-8 emulator just to see how the magic works.

Contents

Table of Contents

Introduction

  1. Why FAT12?
  2. Why Not Just Generate It?
  3. Who This Book Is For
  4. What We Will Build
  5. How This Book Is Structured
  6. How to Read This Book
  7. Not Production Code
  8. About the Author
  9. Feedback

Prerequisites

  1. Operating System and CPU Architecture
  2. Command-Line Tools
  3. Kernel Tools (Chapter 12 Only)
  4. Getting the Code (Optional)

Part I: Reading

Chapter 1: Hello, Disk!

  1. Creating a Disk Image
  2. Sectors and LBA
  3. The Block Device
  4. Debug Logging
  5. The File Block Device
  6. Putting the Block Device to Work
  7. Tracing the Translation
  8. What’s Next

Chapter 2: Mounting the Filesystem

  1. Meet the Boot Sector
  2. Reading the Boot Sector
  3. Putting It All Together
  4. Mounting in Action
  5. What’s Next

Chapter 3: Listing the Root Directory

  1. Locating the Root Directory
  2. Peeking Inside the Root Directory
  3. Anatomy of a Directory Entry
  4. The 8.3 Name Format
  5. The Attribute Byte
  6. The Timestamp Fields
  7. Reading the Root Directory
  8. Filtering the Entries
  9. Putting It All Together
  10. The ls Command
  11. What’s Next

Chapter 4: Reading Files

  1. The Data Region
  2. Carving Up the Data Region
  3. Finding a File’s First Cluster
  4. Resolving a Path
  5. Putting It All Together
  6. The cat Command
  7. What’s Next

Chapter 5: Reading Multi-Cluster Files

  1. When One Cluster Isn’t Enough
  2. Chaining Clusters with the File Allocation Table
  3. Decoding the 12-Bit Entries
  4. Loading the FAT
  5. Counting the Clusters
  6. Reading a Cluster Chain
  7. Putting It All Together
  8. Finding the Missing B
  9. What’s Next

Chapter 6: Full Path Walking

  1. Subdirectories: Files in Disguise
  2. The Path Walking Algorithm
  3. Retrofitting Reads
  4. What’s Next

Part II: Writing

Chapter 7: Deleting

  1. Finding the Parent Directory
  2. Making Sure a Directory Is (Almost) Empty
  3. Freeing the Directory Entry
  4. Our First Real Write
  5. Putting It All Together
  6. The rm Command
  7. The Clusters We Left Behind
  8. Freeing the Cluster Chain
  9. Fixing the Leak
  10. Watching the Clusters Go Free
  11. What’s Next

Chapter 8: Moving (and Renaming)

  1. Encoding the Name
  2. Putting It All Together
  3. The mv Command
  4. Finding a Free Slot
  5. A Directory Trapped Inside Itself
  6. A New Home, an Old Address
  7. Completing the Move
  8. Watching Entries Move
  9. What’s Next

Chapter 9: Creating Directories

  1. Allocating a Cluster
  2. Turning a Cluster into a Directory
  3. Stamping the Moment of Creation
  4. Putting It All Together
  5. The mkdir Command
  6. Watching a Directory Come to Life
  7. What’s Next

Chapter 10: Writing Files

  1. Creating an Empty File
  2. Opening a File for Writing
  3. Buffering the Write
  4. Allocating a Cluster Chain
  5. Committing on Close
  6. The create Command
  7. Watching the Bytes Land
  8. What’s Next

Part III: Advanced

Chapter 11: Formatting

  1. FAT Size or Cluster Count: Which Came First?
  2. The Boot Sector
  3. The FAT Region
  4. The Root Directory
  5. The Data Region
  6. Putting It All Together
  7. The format Command
  8. Watching the Big Bang
  9. What’s Next

Chapter 12: Running FAT12 Inside a Minimal Kernel

  1. Anatomy of a Minimal Kernel
  2. Printing to the VGA Screen
  3. The Kernel’s First Words
  4. The Startup Code
  5. Building kernel.elf
  6. Hello, Kernel!
  7. RAM as Our Block Device
  8. The Kernel’s Main Function
  9. 404: libc Not Found
  10. Booting FAT12 on Bare Metal
  11. What’s Next

Conclusion

  1. What’s Next
  2. Thank You

Appendixes

Appendix A: Talking to a Real Disk

  1. The ATA Controller
  2. Port I/O
  3. Issuing a Command
  4. Opening
  5. Reading
  6. Writing
  7. Completing the Block Device
  8. Replacing the RAM Disk
  9. Building and Running

Appendix B: Long Filename Support (LFN / VFAT)

  1. Finding the Fragments
  2. Anatomy of an LFN Entry
  3. A Quick Look at UTF-16
  4. Decoding the Fragments
  5. Reassembling the Name
  6. The Checksum
  7. What Full LFN Support Would Take

Appendix C: The Boot Sector Fields We Skipped

  1. Booting from FAT12
  2. Which Disk? The Drive Number
  3. Where Is the Volume? Hidden Sectors
  4. Cylinders, Heads, and Sectors (CHS)
  5. The Extended BPB’s Identification Fields
  6. OEM Name (Original Equipment Manufacturer)
  7. Media Descriptor
  8. Back to Our Formatter

Appendix D: FAT12 vs. FAT16 vs. FAT32

  1. From FAT12 to FAT16
  2. From FAT16 to FAT32
  3. Telling FAT12, FAT16, and FAT32 Apart
  4. What About exFAT?
  5. Further Reading

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub