@@ -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 ,
0 commit comments