Repository navigation
FAT Boot Sector
The JFAT boot-sector/BPB decoder, its mount-time validation rules, FSInfo policy and FAT-copy selection.
The first sector of a FAT volume carries the BIOS Parameter Block (BPB). BootSector (fs/src/fs/org/jnode/fs/jfat/BootSector.java) decodes it, computes the derived geometry (FATSz, FirstDataSector, DataSec, CountOfClusters, FAT type) and validates the result before JFAT mounts the volume.
The 2026-11 JFAT hardening round replaced "parse and hope" with a strict decode pipeline and added explicit policies for FSInfo and active FAT copies. A malformed volume now fails at mount with FileSystemException and a field/value message instead of surfacing later as a chain walk through reserved clusters.
| Class / File | Role |
|---|---|
fs/src/fs/org/jnode/fs/jfat/BootSector.java |
BPB decode, validation, geometry computation, setters/write |
fs/src/fs/org/jnode/fs/jfat/Fat.java |
Fat.create() sector-size guard, active-FAT index, FAT copy comparison |
fs/src/fs/org/jnode/fs/jfat/FatCache.java |
Reads/writes routed to the active FAT copy |
fs/src/fs/org/jnode/fs/jfat/FatFormatter.java |
Format-side geometry guard and FSInfo values |
fs/src/fs/org/jnode/fs/jfat/FatFileSystemFormatter.java |
VFS format entry point; sector-count/disk-size conversion |
fs/src/fs/org/jnode/fs/jfat/FatFsInfo.java |
FSInfo sector structure |
A decode runs in a fixed order:
read 512-byte sector
→ decode() // field extraction
→ validateBpb() // BPB field sets and FAT12/16/32 selection
→ compute() // FATSz, FirstDataSector, DataSec, CountOfClusters, type
→ validateGeometry() // TotSec/FirstDataSector, FAT12 cluster-count warning
→ validateFat32() // FSVer and RootClus (FAT32 only)
BootSector(byte[]) and BootSector.read(BlockDeviceAPI) throw FileSystemException on any hard failure.
validateBpb() enforces:
| Field | Rule |
|---|---|
BPB_BytsPerSec |
one of 512, 1024, 2048, 4096 |
BPB_Media |
0xF0 (removable) or 0xF8..0xFF
|
BPB_SecPerClus |
non-zero power of two |
BPB_NumFATs |
>= 1 |
| FAT type |
RootEntCnt == 0 && FATSz16 == 0 means FAT32 |
BPB_FATSz32 |
non-zero for FAT32 |
BPB_FATSz16 |
non-zero for FAT12/16 |
BPB_RootEntCnt |
zero iff FAT32 |
FAT32 BPB_ExtFlags
|
if bit 7 is set, active index < BPB_NumFATs
|
validateFat32() also requires BPB_FSVer == 0x0000 (the only version defined when JFAT was written) and BPB_RootClus in 2..CountOfClusters + 1. Zero or one are reserved and would otherwise walk through reserved entries deep in the I/O path.
validateGeometry() fails when TotSec < FirstDataSector. A CountOfClusters < 4085 value is only a warning: legitimate small FAT12 fixtures (e.g. 2847 clusters) must still mount.
Detection uses the Microsoft FAT specification rule, not the medium descriptor:
boolean isFat32 = (BPB_RootEntCnt == 0 && BPB_FATSz16 == 0);This must happen before choosing FATSz, because some mkfs.fat versions put the real total in TotSec16 and leave TotSec32 == 0. For FAT32 the root directory is a cluster chain, so RootDirSectors is zero and DataSec = TotSec - FirstDataSector.
JFAT is a 512-byte-sector implementation. Fat.create() calls checkSectorSize(FSBlockDeviceAPI) and throws FileSystemException before any read if the device reports a different sector size. Devices that do not implement FSBlockDeviceAPI cannot report a size and are assumed to be 512-byte.
FatFormatter and FatFileSystemFormatter enforce the same constraint at format time. Both sides must stay in sync; the formatter previously accepted non-512 sectors and produced volumes the mount path rejected.
Fat never reads or updates the on-disk FSInfo sector after format time. The authoritative free-cluster count is Fat.getFreeClusterCount(), seeded by a full freeEntries() scan at mount and maintained incrementally by noteFreeTransition. FatFileSystem.getFreeSpace() must not regress to an O(n) scan.
FatFormatter writes FSI_Free_Count and FSI_Nxt_Free as 0xFFFFFFFF ("unknown") for foreign readers. Because JFAT does not trust FSInfo, an invalid/out-of-range stored value is simply ignored. The earlier free-count bug was a wrong countOfClusters formula (root-directory sectors omitted, int arithmetic); it now uses long and subtracts rootDirSectors.
FAT32 BPB_ExtFlags bit 7 means "only one FAT is active". The low nibble names the active copy.
-
Mirroring enabled (bit 7 clear): reads hit FAT copy 0; dirty elements are written to all
BPB_NumFATscopies;compareFatCopies()compares copy 0 against the rest. -
Mirroring disabled (bit 7 set): reads and writes both target the active copy, so they cannot disagree; dirty elements are written only there;
compareFatCopies()reports no divergence because divergence against an intentionally inactive copy is legitimate.
If the declared active index is >= BPB_NumFATs, mount is refused at decode time rather than guessing.
FAT12/FAT16 have no high 16 bits of the first cluster. A non-zero FstClusHi is ignored, but FatFileSystem.shouldWarnClusterHighIgnored() emits the warning once per mount rather than once per directory entry. On FAT32, FstClusHi > 0xFFF is a hard error.
FatFormatter uses long for disk size and FAT/FSInfo sector offsets; DiskSizeGB uses 1000L, not 1024L. Formatting refuses countOfClusters > 1L << 28. FatFileSystemFormatter.format rejects numberOfSectors > Integer.MAX_VALUE before casting to int, and formats the DOS serial from the full epoch milliseconds rather than truncating to int.
The sector buffer is authoritative. set* methods write through to the buffer and mark it dirty, but they do not refresh decoded/computed fields. To observe a set* value through a getter, re-read the sector into a new BootSector; the field view of the current instance is stale.
| Accessor | Kind | Refreshed by set*? |
|---|---|---|
getBytesPerSector, getSectorsPerCluster, getNrFats, getNrRootDirEntries
|
decoded | no |
getExtFlags, getActiveFat, getRootDirectoryStartCluster
|
decoded (FAT32) | no |
getSectorsPerFat, getFirstDataSector, getCountOfClusters, getType
|
computed | no |
getSector/buffer content |
sector bytes | yes |
After calling a setter, construct a new BootSector from the buffer (or call read again) before trusting the accessors.
A 512-byte-sector FAT32 volume:
BPB_BytsPerSec = 512, SecPerClus = 8 (4 KiB clusters)
ReservedSecCnt = 32, NumFATs = 2, FATSz32 = 1000, TotSec32 = 2048000
RootEntCnt = 0, FATSz16 = 0 → isFat32
RootDirSectors = 0
FirstDataSector = 32 + 2*1000 = 2032
DataSec = 2048000 - 2032 = 2045968
CountOfClusters = 2045968 / 8 = 255746
RootClus must be in 2..255747
If BPB_ExtFlags = 0x81, bit 7 is set and the low nibble says FAT copy 1 is active; NumFATs = 2 makes index 1 legal. JFAT then reads and writes only FAT copy 1 and does not compare it against copy 0.
| Condition | Check | Error |
|---|---|---|
BPB_BytsPerSec not 512/1024/2048/4096 |
validateBpb |
Invalid BPB_BytsPerSec |
BPB_Media outside 0xF0/0xF8..0xFF
|
validateBpb |
Invalid BPB_Media |
BPB_SecPerClus zero or not a power of two |
validateBpb |
Invalid BPB_SecPerClus |
BPB_NumFATs < 1 |
validateBpb |
Invalid BPB_NumFATs |
FAT32 with FATSz32 == 0
|
validateBpb |
Invalid BPB_FATSz32 |
FAT12/16 with FATSz16 == 0
|
validateBpb |
Invalid BPB_FATSz16 |
RootEntCnt inconsistent with FAT32 detection |
validateBpb |
Invalid BPB_RootEntCnt |
Single-active-FAT index >= NumFATs
|
validateBpb |
Invalid BPB_ExtFlags |
TotSec < FirstDataSector |
validateGeometry |
Invalid TotSec |
FAT32 FSVer != 0
|
validateFat32 |
Invalid BPB_FSVer |
FAT32 RootClus < 2 or > CountOfClusters + 1
|
validateFat32 |
Invalid BPB_RootClus |
| Device sector size != 512 | Fat.create |
FileSystemException |
CountOfClusters < 4085
|
validateGeometry |
warning only |
FatFormatter writes the fixed FAT32 layout:
| Sector | Content |
|---|---|
| 0 | Boot sector |
| 1 | FSInfo (FSI_Free_Count/FSI_Nxt_Free = 0xFFFFFFFF) |
| 2 | More boot code |
| 3-5 | Unused |
| 6 | Backup boot sector |
| 7 | Backup FSInfo |
| 8 | Backup boot code |
| 9-31 | Reserved |
| 32.. | FAT copies |
Geometry constants include NumOfFATs = 2, BackupBootSector = 6, ReservedSectorCount = 32, NB_HEADS = 255, SECTOR_PER_TRACK = 63. Disk-size and sector-offset arithmetic is long; DiskSizeGB divides by 1000L.
| Test | Focus |
|---|---|
BootSectorValidationTest |
each BPB/FAT32/geometry rejection |
FatSectorSizeGuardTest |
non-512 mount and format rejection |
FatFormatterRawStructureTest |
raw on-disk boot/FSInfo layout |
FatFormatterRoundTripTest |
format → mount round trip |
FatFsInfoRuntimeTest |
FSInfo is written as unknown and not consulted |
FatCopyComparisonTest |
mirrored vs single-active-FAT comparison |
FatApiConsistencyTest |
accessors vs decoded fields |
- On a mount failure, read the
FileSystemExceptionmessage:validateBpb,validateGeometryandvalidateFat32name the field and value. - If a volume mounts but the first file access walks reserved clusters, check
RootClusandExtFlags. - If a formatter-produced volume is rejected, compare
FatFormatter's sector size andFatFileSystemFormatter's sector-count cast with theFat.createguard. - If free space looks wrong, do not inspect FSInfo; check
Fat.getFreeClusterCount()and the FAT scan.
-
The 4096-byte BPB is decoded but the buffer is 512 bytes.
BPB_BytsPerSecis taken from the bytes read, not fromapi.getSectorSize(); the mount guard exists so a 4096-byte-sector device is rejected rather than silently truncated. -
FAT type is not
CountOfClusters-derived. Detection is theRootEntCnt == 0 && FATSz16 == 0rule; the cluster-count thresholds are only a warning. - FSInfo is write-only at format time. Do not add runtime FSInfo reads without also making the count update atomic with the FAT update.
-
Active-FAT validation happens once at decode. The read and write paths must agree on the winner; changing
ExtFlagsafter mount is not supported. -
set*methods do not re-decode. Re-read the sector after mutation. - The formatter and mount guards are paired. A change to one without the other re-opens the "format succeeds, mount refuses" failure.
- JFAT-FatChain — cluster allocation, traversal and durability
- FAT-Filesystem — FAT12/16/32 subsystem overview
-
Block-Device-Layer —
FSBlockDeviceAPI, sector I/O - Filesystem-Layer — filesystem driver integration