Skip to content

don't use BPB_TotSec16 to determine FAT type#547

Open
Freax13 wants to merge 2 commits intomainfrom
fix/large-fat16
Open

don't use BPB_TotSec16 to determine FAT type#547
Freax13 wants to merge 2 commits intomainfrom
fix/large-fat16

Conversation

@Freax13
Copy link
Member

@Freax13 Freax13 commented Mar 5, 2026

Description for BPB_TotSec16:

This field is the old 16-bit total count of sectors on the volume. This count includes the count of all sectors in all four regions of the volume. This field can be 0; if it is 0, then BPB_TotSec32 must be non-zero. For FAT32 volumes, this field must be 0. For FAT12 and FAT16 volumes, this field contains the sector count, and BPB_TotSec32 is 0 if the total sector count “fits” (is less than 0x10000).

Source: https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc

TL;DR: BPB_TotSec16 can be zero even for FAT12/FAT16 volumes if the sector count exceeds 0x10000.

Instead, let's use BPB_FATSz16. Unlike BPB_TotSec16, BPB_FATSz16 must be non-zero for FAT12/FAT16 volumes because BPB_FATSz32 only exists in the FAT32 BPB. According to the document mentioned above, BPB_FATSz16 must be zero for FAT32 volumes.

Closes #546

Freax13 added 2 commits March 5, 2026 08:36
Description for BPB_TotSec16:
> This field is the old 16-bit total count of sectors on the volume.
> This count includes the count of all sectors in all four regions of
> the volume. This field can be 0; if it is 0, then BPB_TotSec32 must
> be non-zero. For FAT32 volumes, this field must be 0. For FAT12 and
> FAT16 volumes, this field contains the sector count, and BPB_TotSec32
> is 0 if the total sector count “fits” (is less than 0x10000).
Source: https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/fatgen103.doc

TL;DR: BPB_TotSec16 can be zero even for FAT12/FAT16 volumes if the
sector count exceeds 0x10000.

Instead, let's use BPB_FATSz16. Unlike BPB_TotSec16, BPB_FATSz16 must
be non-zero for FAT12/FAT16 volumes because BPB_FATSz32 only exists in
the FAT32 BPB. According to the document mentioned above, BPB_FATSz16
must be zero for FAT32 volumes.
@Freax13 Freax13 requested a review from phil-opp March 5, 2026 07:49
@Freax13
Copy link
Member Author

Freax13 commented Mar 5, 2026

@NikoPit Please give this a try and report back to us if this solves your issue.

Copy link
Member

@phil-opp phil-opp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panics when trying to add a ramdisk

2 participants