fs/fat32: Reject reserved_sectors == 0 and root_entries == 0 on FAT12/16
diff --git a/common/fs/fat32.s2.c b/common/fs/fat32.s2.c
index 4d7b906a..cfac7417 100644
--- a/common/fs/fat32.s2.c
+++ b/common/fs/fat32.s2.c
@@ -155,6 +155,11 @@ bytes_per_sector_valid:;
return 1;
}
+ // The boot sector itself occupies at least sector 0
+ if (bpb.reserved_sectors == 0) {
+ return 1;
+ }
+
// The following mess to identify the FAT type is from the FAT spec
// at paragraph 3.5
size_t root_dir_sects = ((bpb.root_entries_count * 32) + (bpb.bytes_per_sector - 1)) / bpb.bytes_per_sector;
@@ -193,6 +198,11 @@ bytes_per_sector_valid:;
context->fat_start_lba = bpb.reserved_sectors;
context->root_entries = bpb.root_entries_count;
+ // FAT12/16 require a non-zero root directory entry count
+ if (context->type != 32 && context->root_entries == 0) {
+ return 1;
+ }
+
// Calculate root_start with overflow check
uint64_t root_start_64 = (uint64_t)context->reserved_sectors + (uint64_t)context->number_of_fats * context->sectors_per_fat;
if (root_start_64 > UINT32_MAX) {
