Skip to content

Commit d39657c

Browse files
committed
[VFATLIB] Add GPT partition support
1 parent bdf1b0c commit d39657c

File tree

5 files changed

+116
-64
lines changed

5 files changed

+116
-64
lines changed

sdk/lib/fslib/vfatlib/fat12.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ Fat12WriteRootDirectory(IN HANDLE FileHandle,
237237

238238
NTSTATUS
239239
Fat12Format(IN HANDLE FileHandle,
240-
IN PPARTITION_INFORMATION PartitionInfo,
240+
IN PPARTITION_INFORMATION_EX PartitionInfo,
241241
IN PDISK_GEOMETRY DiskGeometry,
242242
IN PUNICODE_STRING Label,
243243
IN BOOLEAN QuickFormat,
@@ -289,7 +289,7 @@ Fat12Format(IN HANDLE FileHandle,
289289
BootSector.FATSectors = 0; /* Set later. See below. */
290290
BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack;
291291
BootSector.Heads = DiskGeometry->TracksPerCylinder;
292-
BootSector.HiddenSectors = PartitionInfo->HiddenSectors;
292+
BootSector.HiddenSectors = (PartitionInfo->PartitionStyle == PARTITION_STYLE_MBR) ? PartitionInfo->Mbr.HiddenSectors : 0;
293293
BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0;
294294
BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00;
295295
BootSector.ExtBootSignature = 0x29;

sdk/lib/fslib/vfatlib/fat16.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Fat16WriteRootDirectory(IN HANDLE FileHandle,
236236

237237
NTSTATUS
238238
Fat16Format(IN HANDLE FileHandle,
239-
IN PPARTITION_INFORMATION PartitionInfo,
239+
IN PPARTITION_INFORMATION_EX PartitionInfo,
240240
IN PDISK_GEOMETRY DiskGeometry,
241241
IN PUNICODE_STRING Label,
242242
IN BOOLEAN QuickFormat,
@@ -296,7 +296,7 @@ Fat16Format(IN HANDLE FileHandle,
296296
BootSector.FATSectors = 0; /* Set later. See below. */
297297
BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack;
298298
BootSector.Heads = DiskGeometry->TracksPerCylinder;
299-
BootSector.HiddenSectors = PartitionInfo->HiddenSectors;
299+
BootSector.HiddenSectors = (PartitionInfo->PartitionStyle == PARTITION_STYLE_MBR) ? PartitionInfo->Mbr.HiddenSectors : 0;
300300
BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0;
301301
BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00;
302302
BootSector.ExtBootSignature = 0x29;

sdk/lib/fslib/vfatlib/fat32.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ Fat32WriteRootDirectory(IN HANDLE FileHandle,
384384

385385
NTSTATUS
386386
Fat32Format(IN HANDLE FileHandle,
387-
IN PPARTITION_INFORMATION PartitionInfo,
387+
IN PPARTITION_INFORMATION_EX PartitionInfo,
388388
IN PDISK_GEOMETRY DiskGeometry,
389389
IN PUNICODE_STRING Label,
390390
IN BOOLEAN QuickFormat,
@@ -441,7 +441,7 @@ Fat32Format(IN HANDLE FileHandle,
441441
BootSector.FATSectors = 0;
442442
BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack;
443443
BootSector.Heads = DiskGeometry->TracksPerCylinder;
444-
BootSector.HiddenSectors = PartitionInfo->HiddenSectors;
444+
BootSector.HiddenSectors = (PartitionInfo->PartitionStyle == PARTITION_STYLE_MBR) ? PartitionInfo->Mbr.HiddenSectors : 0;
445445
BootSector.SectorsHuge = PartitionInfo->PartitionLength.QuadPart >>
446446
GetShiftCount(BootSector.BytesPerSector); /* Use shifting to avoid 64-bit division */
447447
BootSector.FATSectors32 = 0; /* Set later */

sdk/lib/fslib/vfatlib/vfatlib.c

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ VfatFormat(
6060
DISK_GEOMETRY DiskGeometry;
6161
IO_STATUS_BLOCK Iosb;
6262
HANDLE FileHandle;
63-
PARTITION_INFORMATION PartitionInfo;
63+
PARTITION_INFORMATION_EX PartitionInfo;
6464
FORMAT_CONTEXT Context;
65+
FAT_TYPE FatType = FAT_UNKNOWN;
6566
NTSTATUS Status, LockStatus;
6667

6768
DPRINT("VfatFormat(DriveRoot '%wZ')\n", DriveRoot);
@@ -128,94 +129,139 @@ VfatFormat(
128129
NULL,
129130
NULL,
130131
&Iosb,
131-
IOCTL_DISK_GET_PARTITION_INFO,
132+
IOCTL_DISK_GET_PARTITION_INFO_EX,
132133
NULL,
133134
0,
134135
&PartitionInfo,
135-
sizeof(PARTITION_INFORMATION));
136+
sizeof(PARTITION_INFORMATION_EX));
136137
if (!NT_SUCCESS(Status))
137138
{
138-
DPRINT("IOCTL_DISK_GET_PARTITION_INFO failed with status 0x%08x\n", Status);
139+
DPRINT("IOCTL_DISK_GET_PARTITION_INFO_EX failed with status 0x%08x\n", Status);
139140
NtClose(FileHandle);
140141
return FALSE;
141142
}
142143
}
143144
else
144145
{
145-
PartitionInfo.PartitionType = 0;
146+
PartitionInfo.PartitionStyle = PARTITION_STYLE_MBR;
147+
PartitionInfo.Mbr.PartitionType = 0;
146148
PartitionInfo.StartingOffset.QuadPart = 0ULL;
147149
PartitionInfo.PartitionLength.QuadPart =
148150
DiskGeometry.Cylinders.QuadPart *
149151
(ULONGLONG)DiskGeometry.TracksPerCylinder *
150152
(ULONGLONG)DiskGeometry.SectorsPerTrack *
151153
(ULONGLONG)DiskGeometry.BytesPerSector;
152-
PartitionInfo.HiddenSectors = 0;
154+
PartitionInfo.Mbr.HiddenSectors = 0;
153155
PartitionInfo.PartitionNumber = 0;
154-
PartitionInfo.BootIndicator = FALSE;
156+
PartitionInfo.Mbr.BootIndicator = FALSE;
155157
PartitionInfo.RewritePartition = FALSE;
156-
PartitionInfo.RecognizedPartition = FALSE;
158+
PartitionInfo.Mbr.RecognizedPartition = FALSE;
157159
}
158160

159161
/* If it already has a FAT FS, we'll use that type.
160162
* If it doesn't, we will determine the FAT type based on size and offset */
161-
if (PartitionInfo.PartitionType != PARTITION_FAT_12 &&
162-
PartitionInfo.PartitionType != PARTITION_FAT_16 &&
163-
PartitionInfo.PartitionType != PARTITION_HUGE &&
164-
PartitionInfo.PartitionType != PARTITION_XINT13 &&
165-
PartitionInfo.PartitionType != PARTITION_FAT32 &&
166-
PartitionInfo.PartitionType != PARTITION_FAT32_XINT13)
163+
164+
if (PartitionInfo.PartitionStyle == PARTITION_STYLE_MBR)
167165
{
168-
/* Determine the correct type based upon size and offset (copied from usetup) */
169-
if (PartitionInfo.PartitionLength.QuadPart < (4200LL * 1024LL))
166+
if (PartitionInfo.Mbr.PartitionType == PARTITION_FAT_12)
170167
{
171-
/* FAT12 CHS partition (disk is smaller than 4.1MB) */
172-
PartitionInfo.PartitionType = PARTITION_FAT_12;
168+
FatType = FAT_12;
169+
}
170+
else if (PartitionInfo.Mbr.PartitionType == PARTITION_FAT_16 ||
171+
PartitionInfo.Mbr.PartitionType == PARTITION_HUGE ||
172+
PartitionInfo.Mbr.PartitionType == PARTITION_XINT13)
173+
{
174+
FatType = FAT_16;
173175
}
174-
else if (PartitionInfo.StartingOffset.QuadPart < (1024LL * 255LL * 63LL * 512LL))
176+
else if (PartitionInfo.Mbr.PartitionType == PARTITION_FAT32 ||
177+
PartitionInfo.Mbr.PartitionType == PARTITION_FAT32_XINT13)
175178
{
176-
/* Partition starts below the 8.4GB boundary ==> CHS partition */
179+
FatType = FAT_32;
180+
}
177181

178-
if (PartitionInfo.PartitionLength.QuadPart < (32LL * 1024LL * 1024LL))
182+
if (FatType == FAT_UNKNOWN)
183+
{
184+
/* Determine the correct type based upon size and offset (copied from usetup) */
185+
if (PartitionInfo.PartitionLength.QuadPart < (4200LL * 1024LL))
179186
{
180-
/* FAT16 CHS partition (partition size < 32MB) */
181-
PartitionInfo.PartitionType = PARTITION_FAT_16;
187+
/* FAT12 CHS partition (disk is smaller than 4.1MB) */
188+
FatType = FAT_12;
182189
}
183-
else if (PartitionInfo.PartitionLength.QuadPart < (512LL * 1024LL * 1024LL))
190+
else if (PartitionInfo.StartingOffset.QuadPart < (1024LL * 255LL * 63LL * 512LL))
184191
{
185-
/* FAT16 CHS partition (partition size < 512MB) */
186-
PartitionInfo.PartitionType = PARTITION_HUGE;
192+
/* Partition starts below the 8.4GB boundary ==> CHS partition */
193+
194+
if (PartitionInfo.PartitionLength.QuadPart < (32LL * 1024LL * 1024LL))
195+
{
196+
/* FAT16 CHS partition (partition size < 32MB) */
197+
FatType = FAT_16;
198+
}
199+
else if (PartitionInfo.PartitionLength.QuadPart < (512LL * 1024LL * 1024LL))
200+
{
201+
/* FAT16 CHS partition (partition size < 512MB) */
202+
FatType = FAT_16;
203+
}
204+
else
205+
{
206+
/* FAT32 CHS partition (partition size >= 512MB) */
207+
FatType = FAT_32;
208+
}
187209
}
188210
else
189211
{
190-
/* FAT32 CHS partition (partition size >= 512MB) */
191-
PartitionInfo.PartitionType = PARTITION_FAT32;
212+
/* Partition starts above the 8.4GB boundary ==> LBA partition */
213+
214+
if (PartitionInfo.PartitionLength.QuadPart < (512LL * 1024LL * 1024LL))
215+
{
216+
/* FAT16 LBA partition (partition size < 512MB) */
217+
FatType = FAT_16;
218+
}
219+
else
220+
{
221+
/* FAT32 LBA partition (partition size >= 512MB) */
222+
FatType = FAT_32;
223+
}
192224
}
193225
}
226+
227+
DPRINT("PartitionType 0x%x\n", PartitionInfo.Mbr.PartitionType);
228+
DPRINT("StartingOffset %I64d\n", PartitionInfo.StartingOffset.QuadPart);
229+
DPRINT("PartitionLength %I64d\n", PartitionInfo.PartitionLength.QuadPart);
230+
DPRINT("HiddenSectors %lu\n", PartitionInfo.Mbr.HiddenSectors);
231+
DPRINT("PartitionNumber %d\n", PartitionInfo.PartitionNumber);
232+
DPRINT("BootIndicator 0x%x\n", PartitionInfo.Mbr.BootIndicator);
233+
DPRINT("RewritePartition %d\n", PartitionInfo.RewritePartition);
234+
DPRINT("RecognizedPartition %d\n", PartitionInfo.Mbr.RecognizedPartition);
235+
}
236+
else if (PartitionInfo.PartitionStyle == PARTITION_STYLE_GPT)
237+
{
238+
if (PartitionInfo.PartitionLength.QuadPart < (4200LL * 1024LL))
239+
{
240+
/* FAT12 CHS partition (disk is smaller than 4.1MB) */
241+
FatType = FAT_12;
242+
}
243+
else if (PartitionInfo.PartitionLength.QuadPart < (512LL * 1024LL * 1024LL))
244+
{
245+
/* FAT16 partition (partition size < 512MB) */
246+
FatType = FAT_16;
247+
}
248+
else if (PartitionInfo.PartitionLength.QuadPart <= (32LL * 1024LL * 1024LL * 1024LL))
249+
{
250+
/* FAT32 partition (partition size < 32GB) */
251+
FatType = FAT_32;
252+
}
194253
else
195254
{
196-
/* Partition starts above the 8.4GB boundary ==> LBA partition */
197-
198-
if (PartitionInfo.PartitionLength.QuadPart < (512LL * 1024LL * 1024LL))
199-
{
200-
/* FAT16 LBA partition (partition size < 512MB) */
201-
PartitionInfo.PartitionType = PARTITION_XINT13;
202-
}
203-
else
204-
{
205-
/* FAT32 LBA partition (partition size >= 512MB) */
206-
PartitionInfo.PartitionType = PARTITION_FAT32_XINT13;
207-
}
255+
DPRINT1("The partition ist too large (> 32 GB) for the FAT file system!\n");
256+
NtClose(FileHandle);
257+
return FALSE;
208258
}
209-
}
210259

211-
DPRINT("PartitionType 0x%x\n", PartitionInfo.PartitionType);
212-
DPRINT("StartingOffset %I64d\n", PartitionInfo.StartingOffset.QuadPart);
213-
DPRINT("PartitionLength %I64d\n", PartitionInfo.PartitionLength.QuadPart);
214-
DPRINT("HiddenSectors %lu\n", PartitionInfo.HiddenSectors);
215-
DPRINT("PartitionNumber %d\n", PartitionInfo.PartitionNumber);
216-
DPRINT("BootIndicator 0x%x\n", PartitionInfo.BootIndicator);
217-
DPRINT("RewritePartition %d\n", PartitionInfo.RewritePartition);
218-
DPRINT("RecognizedPartition %d\n", PartitionInfo.RecognizedPartition);
260+
DPRINT("StartingOffset %I64d\n", PartitionInfo.StartingOffset.QuadPart);
261+
DPRINT("PartitionLength %I64d\n", PartitionInfo.PartitionLength.QuadPart);
262+
DPRINT("PartitionNumber %d\n", PartitionInfo.PartitionNumber);
263+
DPRINT("RewritePartition %d\n", PartitionInfo.RewritePartition);
264+
}
219265

220266
if (Callback != NULL)
221267
{
@@ -238,7 +284,7 @@ VfatFormat(
238284
DPRINT1("WARNING: Failed to lock volume for formatting! Format may fail! (Status: 0x%x)\n", LockStatus);
239285
}
240286

241-
if (PartitionInfo.PartitionType == PARTITION_FAT_12)
287+
if (FatType == FAT_12)
242288
{
243289
/* FAT12 */
244290
Status = Fat12Format(FileHandle,
@@ -249,9 +295,7 @@ VfatFormat(
249295
ClusterSize,
250296
&Context);
251297
}
252-
else if (PartitionInfo.PartitionType == PARTITION_FAT_16 ||
253-
PartitionInfo.PartitionType == PARTITION_HUGE ||
254-
PartitionInfo.PartitionType == PARTITION_XINT13)
298+
else if (FatType == FAT_16)
255299
{
256300
/* FAT16 */
257301
Status = Fat16Format(FileHandle,
@@ -262,8 +306,7 @@ VfatFormat(
262306
ClusterSize,
263307
&Context);
264308
}
265-
else if (PartitionInfo.PartitionType == PARTITION_FAT32 ||
266-
PartitionInfo.PartitionType == PARTITION_FAT32_XINT13)
309+
else if (FatType == FAT_32)
267310
{
268311
/* FAT32 */
269312
Status = Fat32Format(FileHandle,

sdk/lib/fslib/vfatlib/vfatlib.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,21 @@ typedef struct _FORMAT_CONTEXT
109109
ULONG Percent;
110110
} FORMAT_CONTEXT, *PFORMAT_CONTEXT;
111111

112+
typedef enum
113+
{
114+
FAT_UNKNOWN,
115+
FAT_12,
116+
FAT_16,
117+
FAT_32
118+
} FAT_TYPE;
119+
120+
112121
#include "common.h"
113122

114123

115124
NTSTATUS
116125
Fat12Format(HANDLE FileHandle,
117-
PPARTITION_INFORMATION PartitionInfo,
126+
PPARTITION_INFORMATION_EX PartitionInfo,
118127
PDISK_GEOMETRY DiskGeometry,
119128
PUNICODE_STRING Label,
120129
BOOLEAN QuickFormat,
@@ -123,7 +132,7 @@ Fat12Format(HANDLE FileHandle,
123132

124133
NTSTATUS
125134
Fat16Format(HANDLE FileHandle,
126-
PPARTITION_INFORMATION PartitionInfo,
135+
PPARTITION_INFORMATION_EX PartitionInfo,
127136
PDISK_GEOMETRY DiskGeometry,
128137
PUNICODE_STRING Label,
129138
BOOLEAN QuickFormat,
@@ -132,7 +141,7 @@ Fat16Format(HANDLE FileHandle,
132141

133142
NTSTATUS
134143
Fat32Format(HANDLE FileHandle,
135-
PPARTITION_INFORMATION PartitionInfo,
144+
PPARTITION_INFORMATION_EX PartitionInfo,
136145
PDISK_GEOMETRY DiskGeometry,
137146
PUNICODE_STRING Label,
138147
BOOLEAN QuickFormat,

0 commit comments

Comments
 (0)