Leanpub Header

Skip to main content

FAT12

Understanding and Implementing the Classic File System in C

FAT12
This book is 96% completeLast updated on 2026-08-16
+74% in the last 90 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
APP
232
Pages
44,983Words
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 We Will Build
  4. How This Book Is Structured
  5. A Note on Style
  6. How to Read This Book
  7. About the Author
  8. Feedback

Prerequisites

  1. Operating System
  2. C Compiler
  3. mkfs.fat
  4. QEMU (Chapter 11 Only)
  5. Hex Dump Utility
  6. Getting the Code (Optional)

Part I: Reading

Chapter 1: Hello, Disk!

  1. Creating a Virtual Disk
  2. Sectors and LBA
  3. The Block Device
  4. Reading the Volume Label
  5. Tracing the Translation
  6. What’s Next

Chapter 2: The Boot Sector

  1. What Is the Boot Sector?
  2. Mirroring the Layout in C
  3. Mounting and Unmounting
  4. Bytes Per Sector
  5. Total Sectors
  6. Reserved Sector(s)
  7. Extended BPB
  8. Booting from FAT12
  9. What’s Next

Chapter 3: The Root Directory

  1. The Directory Entry
  2. Position and Size
  3. Hexdumping the Root Directory
  4. The 8.3 Name Format
  5. The Attribute Byte
  6. Decoding Timestamps
  7. Directory Iteration
  8. The ls Command
  9. What’s Next

Chapter 4: Reading Files

  1. Clusters: The Allocation Units
  2. Locating Data on Disk
  3. The Cluster Chain
  4. Decoding the 12-Bit Packing
  5. Loading the FAT
  6. Locating a File
  7. Reading a Cluster Chain
  8. Putting It All Together
  9. The cat Command
  10. What’s Next

Chapter 5: Full Path Walking

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

Part II: Writing

Chapter 6: Writing Files

  1. Manipulating FAT Entries
  2. Cluster Allocation
  3. Writing the Data
  4. Directory Entry Creation
  5. Putting It All Together
  6. The Write CLI Command
  7. What’s Next

Chapter 7: Creating Directories

  1. What Are . and ..?
  2. Extending a Directory Chain
  3. Finding or Extending a Slot
  4. Directory Manipulation Helpers
  5. Updating create_dir_entry
  6. Updating fat12_close
  7. The fat12_mkdir Function
  8. Helper: Creating Directory Contents
  9. The mkdir CLI Command
  10. Verification
  11. Bonus: Copy
  12. What’s Next

Chapter 8: Deleting

  1. Freeing the cluster chain
  2. Marking a directory entry deleted
  3. When is a directory empty?
  4. Putting it all together
  5. The rm CLI Command
  6. Verifying Deletion
  7. What’s Next

Chapter 9: Moving (and Renaming)

  1. The Two Cases
  2. Updating . and ..
  3. The fat12_move Function
  4. The mv CLI Command
  5. Computing the Geometry
  6. The Boot Sector
  7. The File Allocation Tables
  8. The Root Directory
  9. The Data Region
  10. The format CLI Command
  11. Verification
  12. What’s Next

Chapter 11: Into the Kernel

  1. The Freestanding Environment
  2. The ATA Block Device Driver
  3. VGA Text Mode
  4. The Kernel
  5. Booting the Kernel
  6. What’s Next

Appendixes

Appendix: Boot Sector Reference Fields

  1. Hidden Sectors (Partitioning)
  2. CHS (Cylinder-Head-Sector) Addressing
  3. Media Descriptor
  4. Extended BPB — Additional Fields
  5. OEM Name (Original Equipment Manufacturer)
  6. Summary

Appendix: FAT Family Reference

  1. FAT Entry Widths and Values
  2. Root directory
  3. BPB
  4. Volume Size Limits

Appendix: FAT12 Cheat Sheet

  1. Disk Layout
  2. Position Formulas
  3. Boot Sector (512 bytes)
  4. Directory Entry (32 bytes)
  5. FAT12 Entry (12 bits)
  6. Timestamp Format
  7. 8.3 Filename Format
  8. Quick Reference: Cluster Math

Appendix: Long Filename Support (LFN / VFAT)

  1. Overview
  2. How It Works
  3. LFN Entry Structure (32 bytes)
  4. Sequence Number
  5. Character Encoding
  6. Checksum Algorithm
  7. 8.3 Alias Generation
  8. Reading LFN Entries
  9. Writing LFN Entries
  10. Deleting LFN Entries
  11. Moving and Renaming
  12. Worked Example
  13. Summary

Conclusion

  1. The Architecture
  2. What’s 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