Kick off your book project in 2 hours, get started with GhostAI in 2 hours, or do both! Free live workshops, on Zoom. You’ll leave with a real book project and a clear plan to keep going. Saturday, June 27, 2026.

Leanpub Header

Skip to main content

FAT12

Understanding and Implementing the Classic File System in C

This book is 90% completeLast updated on 2026-06-19
+80% in the last 30 days

A hands-on guide to building a complete FAT12 filesystem driver in C — from reading your first sector to running code in QEMU. Write everything yourself, boot sector to cat command.

Minimum price

$9.99

$24.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
203
Pages
38,670Words
About

About

About the Book

A filesystem is just bytes on a disk waiting for a decoder ring. This book builds that ring.

FAT12 is the oldest and simplest member of the FAT family — small volumes, simple allocation, flat root directory, and still everywhere: the default for QEMU and VirtualBox floppy images, EFI boot partitions, and embedded firmware. Learn FAT12 once and you already understand FAT16 and FAT32.

You will write every line of code yourself, from the boot sector decoder up to a working cat command in QEMU:

- Build a portable block device abstraction so your filesystem code runs on files and bare metal

- Decode every field of the boot sector and compute disk geometry

- Navigate the famously awkward 12-bit File Allocation Table

- Walk the root directory, read files, and follow cluster chains

- Write files, create directories, delete and rename entries

- Format a disk from scratch

- Port the same library to a 32-bit i386 kernel via ATA PIO and VGA text mode

The code is written for understanding, not production — every I/O check is suppressed to keep the FAT12 logic front and center. Each chapter includes debugging tips and a Production callout explaining what a real-world driver would do differently.

Who this is for: You are building or tinkering with a hobby OS. You know C — pointers, structs, bitwise operations. You have skimmed filesystem tutorials but now you want to actually write one.

Author

About the Author

Björn Götz

I am Björn Götz, a German software engineer who loves understanding low-level systems and explaining them to others.

Contents

Table of Contents

Preface

  1. Why FAT12?
  2. Who This Book Is For
  3. What You Will Build
  4. What You Will Learn
  5. A Note on Style
  6. How to Read This Book
  7. About the Author
  8. Feedback

Prerequisites

  1. Operating System
  2. C Compiler and Dependencies
  3. Editor and Hex Editor
  4. Getting the Code (Optional)

Part I: Reading

Chapter 1: Hello, Disk!

  1. What Is FAT12?
  2. Creating a Virtual Disk
  3. Sectors and LBA
  4. A Tale of Four Regions
  5. Project Layout
  6. A Portable Foundation: The BlockDevice
  7. Reading Sector 0
  8. Tracing the Translation
  9. Production Notes
  10. What Is Next

Chapter 2: The Blueprint (BPB)

  1. Boot Sector Structure
  2. Mapping Bytes to Structs
  3. The FAT12 Library
  4. The Info Command
  5. Debugging
  6. Production Notes
  7. What Is Next

Chapter 3: The Root Directory

  1. What Is the Root Directory?
  2. Loading the Root Directory
  3. Formatting an 8.3 Name
  4. Decoding Timestamps
  5. The ls Command
  6. Updating the Header
  7. Building
  8. Verification: Create Real Files
  9. Debugging with a Hexdump
  10. Production Notes
  11. What Is Next

Chapter 4: Reading Files

  1. What the FAT Actually Is
  2. Where the FAT Lives
  3. Data Region Geometry
  4. Reading a FAT Entry
  5. Loading the FAT
  6. Reading a Cluster Chain
  7. File API
  8. The cat Command
  9. Updating the Header
  10. Building
  11. Verification: Create a Real File
  12. Hexdump Verification
  13. Multi-Cluster File
  14. Production Notes
  15. FAT Family: FAT16 and FAT32
  16. What Is Next

Part II: Writing

Chapter 5: Full Path Walking

  1. Subdirectories in FAT12
  2. Reading a Subdirectory from a Cluster Chain
  3. Splitting a Path into Tokens
  4. Finding an Entry by Name
  5. The Centerpiece: fat12_resolve_path
  6. Wiring into fat12_opendir
  7. Wiring into fat12_open
  8. Updating the CLI Commands
  9. Verification: Nested Directories
  10. Production Notes
  11. The FAT Family: FAT32 Root Cluster
  12. What Is Next

Chapter 6: Writing Files

  1. The Write Path
  2. Writing FAT Entries
  3. Finding Free Clusters
  4. Allocating a Chain
  5. Writing Data Clusters
  6. Syncing the FAT
  7. Loading the Parent Directory
  8. Finding a Free Slot
  9. Formatting 8.3 Names
  10. Creating a Directory Entry
  11. Updating the File Struct
  12. Splitting a Path into Parent and Name
  13. Adding Write Mode to fat12_open
  14. Adding fat12_write
  15. Updating fat12_close
  16. Updating the Header
  17. The Write CLI Command
  18. Verification
  19. Production Notes
  20. Append Mode
  21. FAT Family: Writing Across FAT Variants
  22. What Is Next

Chapter 7: Creating Directories

  1. What We Need to Do
  2. What Are . and ..?
  3. The fat12_mkdir Function
  4. Helper: Creating Directory Contents
  5. Updating the Header
  6. The mkdir CLI Command
  7. Verification
  8. Production Notes
  9. FAT Family: Directories Across FAT Variants
  10. What Is Next

Chapter 8: Deleting

  1. Production Notes
  2. FAT Family: Deleting Across FAT Variants
  3. What Is Next

Chapter 9: Renaming and Moving

  1. The Two Cases
  2. The fat12_move Function
  3. The update_dotdot Helper
  4. Updating the Header
  5. The mv CLI Command
  6. Verification
  7. Production Notes
  8. FAT Family: Rename and Move Across FAT Variants
  9. What Is Next

Part III: Advanced

Chapter 10: Formatting

  1. What We Are Building
  2. What We Need to Do
  3. compute_sectors_per_fat
  4. format_boot_sector
  5. format_fats
  6. format_root_dir
  7. Updating the Header
  8. The format CLI Command
  9. Verification
  10. Production Notes
  11. FAT Family: Formatting Across FAT Variants
  12. What Is Next

Chapter 11: Into the Kernel

  1. What Changes, What Does Not
  2. The Freestanding Environment
  3. lib.c — Heap, Memory, and String Functions
  4. ATA PIO Block Device
  5. The Kernel Entry Point
  6. Linking at 1 MB
  7. kernel_main — Boot-Time Operations
  8. Building
  9. Running in QEMU
  10. Debugging Tips
  11. Production Notes
  12. FAT Family: Multiplatform Payoff
  13. What Is Next

Conclusion

  1. The Architecture
  2. What Is Next
  3. Thank You

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.)

In fact, authors have earned over $15 million writing, publishing and selling 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