Kick off your book project in 2 hours! Live workshop on Zoom. You’ll leave with a real book project, progress on your first chapter, and a clear plan to keep going. Tuesday, June 16, 2026. Learn more…

Leanpub Header

Skip to main content

FAT12

Understanding and Implementing the Classic File System in C

This book is 85% completeLast updated on 2026-06-06

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
218
Pages
40,912Words
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. Decoding Timestamps
  3. Where Is the Root Directory?
  4. Geometry Functions
  5. Loading the Root Directory
  6. Formatting an 8.3 Name
  7. The ls Command
  8. Updating the Header
  9. Building
  10. Verification: Create Real Files
  11. Debugging with a Hexdump
  12. Production Notes
  13. What Is Next

Chapter 4: Reading Files

  1. What the FAT Actually Is
  2. Where the FAT Lives
  3. Data Region Geometry
  4. Reading an 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. Building
  10. Verification: Nested Directories
  11. Production Notes
  12. The FAT Family: FAT32 Root Cluster
  13. 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. Extending fat12_open
  13. Adding fat12_write
  14. Updating fat12_close
  15. Updating the Header
  16. Updating the cat Command
  17. The Write CLI Command
  18. Building
  19. Verification
  20. Production Notes
  21. FAT Family: Writing Across FAT Variants
  22. What Is Next

Chapter 7: Creating Directories

  1. What We Need to Write
  2. Splitting a Path into Parent and Name
  3. The fat12_mkdir Function
  4. Updating the Header
  5. The mkdir CLI Command
  6. Building
  7. Verification
  8. Production Notes
  9. FAT Family: Directories Across FAT Variants
  10. What Is Next

Chapter 8: Deleting Files and Directories

  1. The Deleted Entry Marker
  2. Freeing a Cluster Chain
  3. The fat12_remove Function
  4. The fat12_rmdir Function
  5. Updating the Header
  6. The rm and rmdir CLI Commands
  7. Building
  8. Verification
  9. Production Notes
  10. FAT Family: Deleting Across FAT Variants
  11. What Is Next

Chapter 9: Renaming and Moving

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

Part III: Advanced

Chapter 10: Formatting a Disk

  1. What We Are Building
  2. Computing the Layout
  3. Updating the Header
  4. The format CLI Command
  5. Building
  6. Verification
  7. Production Notes
  8. FAT Family: Formatting Across FAT Variants
  9. What Is Next

Chapter 11: Into the Kernel

  1. What Changes, What Does Not
  2. The Freestanding Environment
  3. ATA PIO Block Device
  4. The Kernel Entry Point
  5. Linking at 1 MB
  6. kernel_main
  7. Building
  8. Running in QEMU
  9. Debugging Tips
  10. Production Notes
  11. FAT Family: Multiplatform Payoff
  12. 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