diff --git a/reactos/sdk/lib/fslib/vfatlib/fat32.c b/reactos/sdk/lib/fslib/vfatlib/fat32.c index 640cf055471..030f1206b6c 100644 --- a/reactos/sdk/lib/fslib/vfatlib/fat32.c +++ b/reactos/sdk/lib/fslib/vfatlib/fat32.c @@ -397,6 +397,9 @@ Fat32Format(IN HANDLE FileHandle, ULONG TmpVal1; ULONG TmpVal2; NTSTATUS Status; + ULONG UsableFatEntries; + ULONG FirstDataSector; + ULONG DataClusters; /* Calculate cluster size */ if (ClusterSize == 0) @@ -469,6 +472,20 @@ Fat32Format(IN HANDLE FileHandle, BootSector.FATSectors32 = (TmpVal1 + (TmpVal2 - 1)) / TmpVal2; DPRINT("FATSectors32 = %lu\n", BootSector.FATSectors32); + /* edge case: first 2 fat entries are not usable, so the calculation might need a correction */ + UsableFatEntries = BootSector.FATSectors32 * (BootSector.BytesPerSector / 4) - 2; + FirstDataSector = BootSector.ReservedSectors + BootSector.FATCount * BootSector.FATSectors32; + DataClusters = (BootSector.SectorsHuge - FirstDataSector) / BootSector.SectorsPerCluster; + if (DataClusters > UsableFatEntries) + { + /* Need more fat entries */ + BootSector.FATSectors32 += (DataClusters - UsableFatEntries); + + DPRINT("UsableFatEntries = %lu\n", UsableFatEntries); + DPRINT("DataClusters = %lu\n", DataClusters); + DPRINT("BootSector.FATSectors32 incremented to %lu\n", BootSector.FATSectors32); + } + /* Init context data */ Context->TotalSectorCount = 2 + (BootSector.FATSectors32 * BootSector.FATCount) + BootSector.SectorsPerCluster;