NeuroAgent

FAT32 BPB: Complete Definition Guide

Complete FAT32 BPB tutorial covering structure, field values, calculations, and common issues. Practical examples for filesystem implementation and troubleshooting.

Question

How to properly define a BIOS Parameter Block (BPB) for FAT32?

I’ve searched extensively for a complete example of a BIOS Parameter Block (BPB) for FAT32, but most resources only explain what a BPB is without providing practical examples. While I successfully created a BPB for FAT12 following tutorials on BrokenThorn.com, I’m encountering difficulties when adapting it for FAT32.

I understand that FAT12 and FAT32 share many BPB fields, but I need clarification on FAT32-specific values:

  • How many sectors per FAT should there be for FAT32?
  • How do I determine the total number of sectors on a FAT32 disk?
  • What are the FAT32-specific fields that differ from FAT12?

More generally, I’d like to understand the process for writing a proper BPB for any filesystem:

  • How do you calculate values like the number of sectors or sectors per FAT?
  • Where can I find reliable documentation or tools for defining these fields?
  • What standard references or specifications do OS developers typically follow?

I want to understand the process behind building a correct BPB from scratch, not just copying values. I’ve tried reusing my FAT12 BPB with modified values for FAT32, but when using acopy to write files to the disk image, some file information is being written to the FAT area instead of the root directory.

Any guidance, working examples, or resources would be greatly appreciated.

NeuroAgent

The BIOS Parameter Block (BPB) for FAT32 requires specific field values and extensions beyond FAT12, with the key difference being that FAT32 uses a 32-bit FAT structure and additional fields in the Extended BIOS Parameter Block (EBPB). While FAT12 and FAT16 share many common BPB fields, FAT32 introduces several FAT32-specific fields and requires different calculation methods for parameters like sectors per FAT and total sectors.


Contents

FAT32 BPB Structure Overview

The FAT32 BPB structure extends the original BPB format by adding 28 bytes of FAT32-specific information, followed by the remaining 26 bytes of the Extended BIOS Parameter Block (EBPB). This extension allows FAT32 to support much larger volumes than FAT16.

The BPB is located at offset 0B (11 decimal) in the boot sector and contains both standard fields (shared with FAT12/FAT16) and FAT32-specific extensions. The complete structure must be properly initialized for the filesystem to function correctly.

Standard BPB Fields (Shared with FAT12/FAT16)

  • Bytes per Sector: 2 bytes at offset 11, indicating sector size (typically 512, 1024, 2048, or 4096 bytes)
  • Sectors per Cluster: 1 byte at offset 13, determining how many sectors make up one allocation unit
  • Reserved Sectors: 2 bytes at offset 14, specifying sectors before FAT (usually 32 for FAT32)
  • Number of FATS: 1 byte at offset 16 (always 2 for FAT32)
  • Root Entries: 2 bytes at offset 17 (must be 0 for FAT32, as root directory is now cluster-based)
  • Total Sectors: 2 bytes at offset 19 (must be 0 for FAT32, as this field is too small for FAT32 volumes)
  • Media Descriptor: 1 byte at offset 21
  • Sectors per FAT: 2 bytes at offset 22 (must be 0 for FAT32, as FAT size is now 32-bit)
  • Sectors per Track: 2 bytes at offset 24
  • Number of Heads: 2 bytes at offset 26
  • Hidden Sectors: 4 bytes at offset 28
  • Total Sectors (Large): 4 bytes at offset 32 (used instead of the 2-byte field for FAT32)

FAT32-Specific EBPB Fields

The FAT32 EBPB adds these critical fields after the standard BPB:

  • FAT Size (32-bit): 4 bytes at offset 36, containing the size of one FAT in sectors
  • Extended Flags: 2 bytes at offset 40
  • FS Version: 2 bytes at offset 42 (usually 0000h for FAT32)
  • Root Cluster: 4 bytes at offset 44, pointing to the first cluster of the root directory
  • FS Information Sector: 2 bytes at offset 48 (usually 1, pointing to FSINFO sector)
  • Boot Sector Backup: 2 bytes at offset 50 (usually 6, backup boot sector location)
  • Reserved: 12 bytes at offset 52 (must be zero)
  • Drive Number: 1 byte at offset 64
  • Windows NT Flags: 1 byte at offset 65
  • Signature: 1 byte at offset 66 (usually 29h)
  • Volume ID: 4 bytes at offset 67
  • Volume Label: 11 bytes at offset 71
  • Filesystem Type: 8 bytes at offset 82 (typically "FAT32 ")

FAT32-Specific Fields and Values

Sectors per FAT

Unlike FAT12/FAT16 where sectors per FAT is stored in a 2-byte field, FAT32 uses a 4-byte field at offset 36 in the EBPB. This allows FAT32 to support much larger FAT tables.

How to calculate sectors per FAT:

Sectors per FAT = (Total FAT size in bytes) / Bytes per Sector

The FAT size depends on the number of clusters in the volume. Each FAT entry in FAT32 is 4 bytes (32 bits), with the high 4 bits reserved, leaving 28 bits for cluster numbers.

Total Number of Sectors

FAT32 uses the 4-byte “Total Sectors (Large)” field at offset 32 instead of the 2-byte field used by FAT12/FAT16. This allows addressing volumes up to 2 TB in size.

To determine total sectors:

Total Sectors = (Total volume size in bytes) / (Bytes per Sector)

FAT32-Specific Field Values

  • Root Entries: Must be 0 for FAT32 (at offset 17)
  • Sectors per FAT: Must be 0 for FAT32 (at offset 22)
  • Total Sectors: Must be 0 for FAT32 (at offset 19)
  • Number of FATS: Always 2 (at offset 16)
  • Reserved Sectors: Typically 32 for FAT32 (at offset 14)

Calculating Key BPB Parameters

Step-by-Step Calculation Process

  1. Determine Volume Parameters:

    • Total volume size (e.g., 1GB = 1,073,741,824 bytes)
    • Bytes per sector (typically 512)
    • Sectors per cluster (depends on volume size and desired cluster size)
  2. Calculate Total Sectors:

    Total Sectors = Total Volume Size / Bytes per Sector
    
  3. Determine Sectors per Cluster:

    • For small volumes (32MB-256MB): 1 sector/cluster
    • For medium volumes (256MB-8GB): 8 sectors/cluster
    • For large volumes (8GB-16GB): 16 sectors/cluster
    • For very large volumes (16GB+): 32 sectors/cluster
  4. Calculate Number of Clusters:

    Number of Clusters = Total Sectors / Sectors per Cluster
    
  5. Calculate FAT Size:

    FAT Size in bytes = Number of Clusters × 4 bytes
    Sectors per FAT = FAT Size in bytes / Bytes per Sector
    
  6. Calculate Data Area Size:

    Reserved Sectors = 32 (typical for FAT32)
    Number of FATS = 2
    Root Directory Sectors = 0 (FAT32 uses cluster-based root)
    
    FAT Area Size = Reserved Sectors + (Sectors per FAT × Number of FATS)
    Data Area Size = Total Sectors - FAT Area Size
    

Example Calculation for 1GB Volume

  • Total Volume Size: 1,073,741,824 bytes
  • Bytes per Sector: 512
  • Sectors per Cluster: 16
Total Sectors = 1,073,741,824 / 512 = 2,097,152 sectors
Number of Clusters = 2,097,152 / 16 = 131,072 clusters
FAT Size in bytes = 131,072 × 4 = 524,288 bytes
Sectors per FAT = 524,288 / 512 = 1,024 sectors
Reserved Sectors = 32
FAT Area Size = 32 + (1,024 × 2) = 2,080 sectors
Data Area Size = 2,097,152 - 2,080 = 2,095,072 sectors

Complete FAT32 BPB Example

Here’s a properly structured FAT32 BPB with explanations for each field:

assembly
; FAT32 Boot Sector with BPB
jmp short BootStart
nop
OEMIdentifier       db 'MSDOS5.0'        ; 8 bytes at offset 3
BPB_BytesPerSector  dw 512               ; 2 bytes at offset 11
BPB_SectorsPerCluster db 16               ; 1 byte at offset 13
BPB_ReservedSectors dw 32                ; 2 bytes at offset 14
BPB_NumFATs         db 2                 ; 1 byte at offset 16
BPB_RootEntries     dw 0                 ; 2 bytes at offset 17 (must be 0 for FAT32)
BPB_TotalSectors    dw 0                 ; 2 bytes at offset 19 (must be 0 for FAT32)
BPB_MediaDescriptor db 0xF8              ; 1 byte at offset 21
BPB_SectorsPerFAT   dw 0                 ; 2 bytes at offset 22 (must be 0 for FAT32)
BPB_SectorsPerTrack dw 63                ; 2 bytes at offset 24
BPB_NumHeads        dw 255               ; 2 bytes at offset 26
BPB_HiddenSectors   dd 0                 ; 4 bytes at offset 28
BPB_TotalSectorsLarge dd 2097152         ; 4 bytes at offset 32 (total sectors)
; FAT32 Specific EBPB Fields
BPB_FATSize32       dd 1024               ; 4 bytes at offset 36 (sectors per FAT)
BPB_ExtFlags        dw 0                 ; 2 bytes at offset 40
BPB_FSVersion       dw 0                 ; 2 bytes at offset 42
BPB_RootCluster     dd 2                 ; 4 bytes at offset 44 (first cluster of root)
BPB_FSInfoSector    dw 1                 ; 2 bytes at offset 48
BPB_BootSectorBackup dw 6                ; 2 bytes at offset 50
BPB_Reserved        times 12 db 0        ; 12 bytes at offset 52
BPB_DriveNumber     db 0x80              ; 1 byte at offset 64
BPB_Reserved1       db 0                 ; 1 byte at offset 65
BPB_BootSignature   db 29h               ; 1 byte at offset 66
BPB_VolumeID        dd 0x12345678        ; 4 bytes at offset 67
BPB_VolumeLabel     db 'NO NAME    '     ; 11 bytes at offset 71
BPB_FileSystemType  db 'FAT32   '        ; 8 bytes at offset 82

; Rest of boot code and boot signature at offset 1FE-1FF

Key Field Explanations:

  • BPB_BytesPerSector: 512 (standard floppy/hard disk sector size)
  • BPB_SectorsPerCluster: 16 (16 sectors per cluster = 8KB cluster size)
  • BPB_ReservedSectors: 32 (sectors before FAT, including boot sector)
  • BPB_NumFATs: 2 (standard for FAT filesystems)
  • BPB_RootEntries: 0 (FAT32 doesn’t use fixed-size root directory)
  • BPB_TotalSectors: 0 (too small for FAT32, use BPB_TotalSectorsLarge)
  • BPB_SectorsPerFAT: 0 (too small for FAT32, use BPB_FATSize32)
  • BPB_TotalSectorsLarge: 2,097,152 (1GB volume with 512-byte sectors)
  • BPB_FATSize32: 1,024 (FAT table size in sectors)
  • BPB_RootCluster: 2 (first cluster of root directory, after FAT and data area)

Common Issues and Solutions

Issue: Files Writing to FAT Area Instead of Root Directory

This typically occurs when:

  1. Root Cluster is incorrect: The root directory cluster must point to the first data cluster after the FAT area
  2. FAT size calculation is wrong: Incorrect FAT size can misalign the data area
  3. Reserved sectors are insufficient: FAT32 typically needs more reserved sectors than FAT12

Solution:

  • Calculate the first data cluster: First Data Cluster = Reserved Sectors + (Sectors per FAT × Number of FATS) / Sectors per Cluster
  • Ensure BPB_RootCluster points to this calculated cluster
  • Verify that FAT size calculation accounts for all cluster entries

Issue: Volume Not Recognized by Operating Systems

Common causes:

  • Boot signature missing: The 0xAA55 signature at offset 1FE-1FF is required
  • Media descriptor incorrect: Should typically be 0xF8 for hard disks, 0xF0 for removable media
  • Filesystem type mismatch: Ensure BPB_FileSystemType is 'FAT32 ’ with proper padding

Issue: Cluster Size Mismatch

  • Sectors per cluster must be power of 2: 1, 2, 4, 8, 16, 32, 64, 128
  • Cluster size should be appropriate for volume size: Too small clusters waste space on FAT, too large clusters waste space in files

Reliable Documentation and Tools

Official Specifications

  1. Microsoft’s EFI FAT32 Specification: Defines the exact structure and requirements for FAT32 filesystems
  2. Open Source FAT Documentation:

Development Tools

  1. Fat32 Formatter Tools: Use existing tools to generate reference BPB structures
  2. Disk Image Analysis Tools:
    • hexedit or xxd for examining low-level structures
    • file command for filesystem type detection
  3. Custom BPB Calculators: Write scripts to calculate BPB parameters based on volume specifications

Verification and Testing

  1. Boot Sector Validation: Ensure the boot sector can be loaded by BIOS
  2. FAT Consistency Checks: Verify FAT entries point to valid clusters
  3. Cross-platform Testing: Test on multiple operating systems (Windows, Linux, macOS)

Learning Resources

  1. Filesystem Development Tutorials:
  2. Sample Code: Many open source projects provide working BPB implementations

Conclusion

Creating a proper FAT32 BPB requires careful attention to both the standard BPB fields and the FAT32-specific extensions. The key differences from FAT12 include the use of 32-bit FAT fields, cluster-based root directory, and different calculation methods for critical parameters like sectors per FAT and total sectors.

To build a correct FAT32 BPB from scratch:

  1. Calculate total sectors based on your volume size
  2. Determine appropriate sectors per cluster for your volume size
  3. Compute FAT size using the 32-bit field (4 bytes per cluster entry)
  4. Set FAT32-specific fields to zero where required
  5. Point root directory to the correct first data cluster
  6. Include proper boot signature and filesystem identification

The most common issues when adapting FAT12 BPBs to FAT32 involve incorrect field calculations, especially for FAT size and root directory location. Always verify your calculations and test thoroughly with the tools you intend to use.

For ongoing development, refer to the official Microsoft FAT32 specification and leverage the active OS development community for troubleshooting and advanced filesystem implementation techniques.

Sources

  1. Wikipedia - BIOS parameter block
  2. Wikipedia - Design of the FAT file system
  3. OSDev Wiki - FAT Filesystem
  4. Eric Lo’s FAT32 Filesystem Overview
  5. NTFS.com - Boot Sector and Bootstrap Modifications
  6. HandWiki - BIOS parameter block
  7. Averstak FAT Boot Sector Documentation
  8. Computer Science Journal - FAT32 Data Structure Review