2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS VFATx filesystem library
8 * CSH 05/04-2003 Created
13 #include <ndk/obfuncs.h>
19 VfatxFormat(IN PUNICODE_STRING DriveRoot
,
20 IN FMIFS_MEDIA_FLAG MediaFlag
,
21 IN PUNICODE_STRING Label
,
22 IN BOOLEAN QuickFormat
,
24 IN PFMIFSCALLBACK Callback
)
26 OBJECT_ATTRIBUTES ObjectAttributes
;
27 DISK_GEOMETRY DiskGeometry
;
30 PARTITION_INFORMATION PartitionInfo
;
31 FORMAT_CONTEXT Context
;
34 DPRINT("VfatxFormat(DriveRoot '%wZ')\n", DriveRoot
);
36 Context
.TotalSectorCount
= 0;
37 Context
.CurrentSectorCount
= 0;
38 Context
.Callback
= Callback
;
39 Context
.Success
= FALSE
;
42 InitializeObjectAttributes(&ObjectAttributes
,
48 Status
= NtOpenFile(&FileHandle
,
49 FILE_GENERIC_READ
| FILE_GENERIC_WRITE
,
53 FILE_SYNCHRONOUS_IO_ALERT
);
54 if (!NT_SUCCESS(Status
))
56 DPRINT("NtOpenFile() failed with status 0x%.08x\n", Status
);
60 Status
= NtDeviceIoControlFile(FileHandle
,
65 IOCTL_DISK_GET_DRIVE_GEOMETRY
,
69 sizeof(DISK_GEOMETRY
));
70 if (!NT_SUCCESS(Status
))
72 DPRINT("IOCTL_DISK_GET_DRIVE_GEOMETRY failed with status 0x%.08x\n", Status
);
77 if (DiskGeometry
.MediaType
== FixedMedia
)
79 DPRINT("Cylinders %I64d\n", DiskGeometry
.Cylinders
.QuadPart
);
80 DPRINT("TracksPerCylinder %ld\n", DiskGeometry
.TracksPerCylinder
);
81 DPRINT("SectorsPerTrack %ld\n", DiskGeometry
.SectorsPerTrack
);
82 DPRINT("BytesPerSector %ld\n", DiskGeometry
.BytesPerSector
);
83 DPRINT("DiskSize %I64d\n",
84 DiskGeometry
.Cylinders
.QuadPart
*
85 (ULONGLONG
)DiskGeometry
.TracksPerCylinder
*
86 (ULONGLONG
)DiskGeometry
.SectorsPerTrack
*
87 (ULONGLONG
)DiskGeometry
.BytesPerSector
);
89 Status
= NtDeviceIoControlFile(FileHandle
,
94 IOCTL_DISK_GET_PARTITION_INFO
,
98 sizeof(PARTITION_INFORMATION
));
99 if (!NT_SUCCESS(Status
))
101 DPRINT("IOCTL_DISK_GET_PARTITION_INFO failed with status 0x%.08x\n", Status
);
107 * FIXME: This is a hack!
108 * Partitioning software MUST set the correct number of hidden sectors!
110 PartitionInfo
.HiddenSectors
= DiskGeometry
.SectorsPerTrack
;
114 PartitionInfo
.PartitionType
= 0;
115 PartitionInfo
.StartingOffset
.QuadPart
= 0ULL;
116 PartitionInfo
.PartitionLength
.QuadPart
=
117 DiskGeometry
.Cylinders
.QuadPart
*
118 (ULONGLONG
)DiskGeometry
.TracksPerCylinder
*
119 (ULONGLONG
)DiskGeometry
.SectorsPerTrack
*
120 (ULONGLONG
)DiskGeometry
.BytesPerSector
;
121 PartitionInfo
.HiddenSectors
= 0;
122 PartitionInfo
.PartitionNumber
= 0;
123 PartitionInfo
.BootIndicator
= FALSE
;
124 PartitionInfo
.RewritePartition
= FALSE
;
125 PartitionInfo
.RecognizedPartition
= FALSE
;
128 DPRINT("PartitionType 0x%x\n", PartitionInfo
.PartitionType
);
129 DPRINT("StartingOffset %I64d\n", PartitionInfo
.StartingOffset
.QuadPart
);
130 DPRINT("PartitionLength %I64d\n", PartitionInfo
.PartitionLength
.QuadPart
);
131 DPRINT("HiddenSectors %lu\n", PartitionInfo
.HiddenSectors
);
132 DPRINT("PartitionNumber %d\n", PartitionInfo
.PartitionNumber
);
133 DPRINT("BootIndicator 0x%x\n", PartitionInfo
.BootIndicator
);
134 DPRINT("RewritePartition %d\n", PartitionInfo
.RewritePartition
);
135 DPRINT("RecognizedPartition %d\n", PartitionInfo
.RecognizedPartition
);
137 if (Callback
!= NULL
)
140 Callback(PROGRESS
, 0, (PVOID
)&Context
.Percent
);
143 Status
= FatxFormat(FileHandle
,
150 if (Callback
!= NULL
)
152 Context
.Success
= (BOOLEAN
)(NT_SUCCESS(Status
));
153 Callback(DONE
, 0, (PVOID
)&Context
.Success
);
156 DPRINT("VfatxFormat() done. Status 0x%.08x\n", Status
);
163 VfatxUpdateProgress(IN PFORMAT_CONTEXT Context
,
168 Context
->CurrentSectorCount
+= (ULONGLONG
)Increment
;
170 NewPercent
= (Context
->CurrentSectorCount
* 100ULL) / Context
->TotalSectorCount
;
172 if (NewPercent
> Context
->Percent
)
174 Context
->Percent
= NewPercent
;
175 if (Context
->Callback
!= NULL
)
177 Context
->Callback(PROGRESS
, 0, &Context
->Percent
);