From 02fde17b6fb45a35082b14d386fd050b738aaa92 Mon Sep 17 00:00:00 2001 From: Pierre Schweitzer Date: Sun, 22 May 2016 07:56:45 +0000 Subject: [PATCH 1/1] [VFATLIB] Update the amount of FAT entries taking into account that the first two are reserved and thus not usable. Patch by Wim Hueskes CORE-11283 #resolve #comment Committed in r71370, thanks for your patch! svn path=/trunk/; revision=71370 --- reactos/sdk/lib/fslib/vfatlib/fat32.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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; -- 2.17.1