From 6036bdcd71d8077afacec8c6d6f05d28ae86b219 Mon Sep 17 00:00:00 2001 From: Fred <8153358+slackingfred@users.noreply.github.com> Date: Sun, 26 Nov 2023 17:31:41 -0800 Subject: [PATCH] [fat] align start of data region to MiB --- src/format_fat32.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/format_fat32.c b/src/format_fat32.c index 451f6605..eb911bd7 100644 --- a/src/format_fat32.c +++ b/src/format_fat32.c @@ -42,6 +42,8 @@ FormatStatus = ERROR_SEVERITY_ERROR|FAC(FACILITY_STORAGE)|err; \ goto out; } while(0) +#define USER_AREA_ALIGN_BYTES (1 * 1024 * 1024) + extern BOOL write_as_esp; /* Large FAT32 */ @@ -175,6 +177,7 @@ BOOL FormatLargeFAT32(DWORD DriveIndex, uint64_t PartitionOffset, DWORD ClusterS DWORD BytesPerSect = 0; DWORD SectorsPerCluster = 0; DWORD TotalSectors = 0; + DWORD AlignSectors = 0; DWORD SystemAreaSize = 0; DWORD UserAreaSize = 0; ULONGLONG qTotalSectors = 0; @@ -295,7 +298,6 @@ BOOL FormatLargeFAT32(DWORD DriveIndex, uint64_t PartitionOffset, DWORD ClusterS SectorsPerCluster = ClusterSize / BytesPerSect; pFAT32BootSect->bSecPerClus = (BYTE)SectorsPerCluster; - pFAT32BootSect->wRsvdSecCnt = (WORD)ReservedSectCount; pFAT32BootSect->bNumFATs = (BYTE)NumFATs; pFAT32BootSect->wRootEntCnt = 0; pFAT32BootSect->wTotSec16 = 0; @@ -310,6 +312,13 @@ BOOL FormatLargeFAT32(DWORD DriveIndex, uint64_t PartitionOffset, DWORD ClusterS FatSize = GetFATSizeSectors(pFAT32BootSect->dTotSec32, pFAT32BootSect->wRsvdSecCnt, pFAT32BootSect->bSecPerClus, pFAT32BootSect->bNumFATs, BytesPerSect); + // Recalculate reserved sector count after FAT size is known + SystemAreaSize = ReservedSectCount + NumFATs * FatSize; // We round up from here to integral MiB + AlignSectors = USER_AREA_ALIGN_BYTES / BytesPerSect; + SystemAreaSize = (SystemAreaSize + AlignSectors - 1) / AlignSectors * AlignSectors; + ReservedSectCount = SystemAreaSize - NumFATs * FatSize; + + pFAT32BootSect->wRsvdSecCnt = (WORD)ReservedSectCount; pFAT32BootSect->dFATSz32 = FatSize; pFAT32BootSect->wExtFlags = 0; pFAT32BootSect->wFSVer = 0;