2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS VFAT filesystem library
5 * PURPOSE: Fat12 support
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
10 /* INCLUDES *******************************************************************/
18 /* FUNCTIONS ******************************************************************/
21 Fat12WriteBootSector(IN HANDLE FileHandle
,
22 IN PFAT16_BOOT_SECTOR BootSector
,
23 IN OUT PFORMAT_CONTEXT Context
)
25 IO_STATUS_BLOCK IoStatusBlock
;
27 PFAT16_BOOT_SECTOR NewBootSector
;
28 LARGE_INTEGER FileOffset
;
30 /* Allocate buffer for new bootsector */
31 NewBootSector
= (PFAT16_BOOT_SECTOR
)RtlAllocateHeap(RtlGetProcessHeap (),
33 BootSector
->BytesPerSector
);
34 if (NewBootSector
== NULL
)
35 return STATUS_INSUFFICIENT_RESOURCES
;
37 /* Zero the new bootsector */
38 RtlZeroMemory(NewBootSector
, BootSector
->BytesPerSector
);
40 /* Copy FAT16 BPB to new bootsector */
41 memcpy(&NewBootSector
->Jump
[0],
43 FIELD_OFFSET(FAT16_BOOT_SECTOR
, Res2
) - FIELD_OFFSET(FAT16_BOOT_SECTOR
, Jump
));
44 /* FAT16 BPB length (up to (not including) Res2) */
46 /* Write the boot sector signature */
47 NewBootSector
->Signature1
= 0xAA550000;
50 FileOffset
.QuadPart
= 0ULL;
51 Status
= NtWriteFile(FileHandle
,
57 BootSector
->BytesPerSector
,
60 if (!NT_SUCCESS(Status
))
62 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
66 UpdateProgress(Context
, 1);
70 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBootSector
);
76 Fat12WriteFAT(IN HANDLE FileHandle
,
77 IN ULONG SectorOffset
,
78 IN PFAT16_BOOT_SECTOR BootSector
,
79 IN OUT PFORMAT_CONTEXT Context
)
81 IO_STATUS_BLOCK IoStatusBlock
;
84 LARGE_INTEGER FileOffset
;
90 Buffer
= (PUCHAR
)RtlAllocateHeap(RtlGetProcessHeap(),
94 return STATUS_INSUFFICIENT_RESOURCES
;
97 RtlZeroMemory(Buffer
, 32 * 1024);
99 /* FAT cluster 0 & 1*/
100 Buffer
[0] = 0xf8; /* Media type */
104 /* Write first sector of the FAT */
105 FileOffset
.QuadPart
= (SectorOffset
+ BootSector
->ReservedSectors
) * BootSector
->BytesPerSector
;
106 Status
= NtWriteFile(FileHandle
,
112 BootSector
->BytesPerSector
,
115 if (!NT_SUCCESS(Status
))
117 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
121 UpdateProgress(Context
, 1);
123 /* Zero the begin of the buffer */
124 RtlZeroMemory(Buffer
, 3);
126 /* Zero the rest of the FAT */
127 Sectors
= 32 * 1024 / BootSector
->BytesPerSector
;
128 for (i
= 1; i
< (ULONG
)BootSector
->FATSectors
; i
+= Sectors
)
130 /* Zero some sectors of the FAT */
131 FileOffset
.QuadPart
= (SectorOffset
+ BootSector
->ReservedSectors
+ i
) * BootSector
->BytesPerSector
;
132 if (((ULONG
)BootSector
->FATSectors
- i
) <= Sectors
)
134 Sectors
= (ULONG
)BootSector
->FATSectors
- i
;
137 Size
= Sectors
* BootSector
->BytesPerSector
;
138 Status
= NtWriteFile(FileHandle
,
147 if (!NT_SUCCESS(Status
))
149 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
153 UpdateProgress(Context
, Sectors
);
157 /* Free the buffer */
158 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
164 Fat12WriteRootDirectory(IN HANDLE FileHandle
,
165 IN PFAT16_BOOT_SECTOR BootSector
,
166 IN OUT PFORMAT_CONTEXT Context
)
168 IO_STATUS_BLOCK IoStatusBlock
;
169 NTSTATUS Status
= STATUS_SUCCESS
;
171 LARGE_INTEGER FileOffset
;
172 ULONG FirstRootDirSector
;
173 ULONG RootDirSectors
;
178 DPRINT("BootSector->ReservedSectors = %hu\n", BootSector
->ReservedSectors
);
179 DPRINT("BootSector->FATSectors = %hu\n", BootSector
->FATSectors
);
180 DPRINT("BootSector->SectorsPerCluster = %u\n", BootSector
->SectorsPerCluster
);
183 RootDirSectors
= ((BootSector
->RootEntries
* 32) +
184 (BootSector
->BytesPerSector
- 1)) / BootSector
->BytesPerSector
;
186 BootSector
->ReservedSectors
+ (BootSector
->FATCount
* BootSector
->FATSectors
);
188 DPRINT("RootDirSectors = %lu\n", RootDirSectors
);
189 DPRINT("FirstRootDirSector = %lu\n", FirstRootDirSector
);
191 /* Allocate buffer for the cluster */
192 Buffer
= (PUCHAR
)RtlAllocateHeap(RtlGetProcessHeap(),
196 return STATUS_INSUFFICIENT_RESOURCES
;
198 /* Zero the buffer */
199 RtlZeroMemory(Buffer
, 32 * 1024);
201 Sectors
= 32 * 1024 / BootSector
->BytesPerSector
;
202 for (i
= 0; i
< RootDirSectors
; i
+= Sectors
)
204 /* Zero some sectors of the root directory */
205 FileOffset
.QuadPart
= (FirstRootDirSector
+ i
) * BootSector
->BytesPerSector
;
207 if ((RootDirSectors
- i
) <= Sectors
)
209 Sectors
= RootDirSectors
- i
;
212 Size
= Sectors
* BootSector
->BytesPerSector
;
214 Status
= NtWriteFile(FileHandle
,
223 if (!NT_SUCCESS(Status
))
225 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
229 UpdateProgress(Context
, Sectors
);
233 /* Free the buffer */
234 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
240 Fat12Format(IN HANDLE FileHandle
,
241 IN PPARTITION_INFORMATION PartitionInfo
,
242 IN PDISK_GEOMETRY DiskGeometry
,
243 IN PUNICODE_STRING Label
,
244 IN BOOLEAN QuickFormat
,
245 IN ULONG ClusterSize
,
246 IN OUT PFORMAT_CONTEXT Context
)
248 FAT16_BOOT_SECTOR BootSector
;
249 OEM_STRING VolumeLabel
;
251 ULONG RootDirSectors
;
257 /* Calculate cluster size */
258 if (ClusterSize
== 0)
260 if (DiskGeometry
->MediaType
== FixedMedia
)
262 /* 4KB Cluster (Harddisk only) */
267 /* 512 byte cluster (floppy) */
272 SectorCount
= PartitionInfo
->PartitionLength
.QuadPart
>>
273 GetShiftCount(DiskGeometry
->BytesPerSector
); /* Use shifting to avoid 64-bit division */
275 DPRINT("SectorCount = %lu\n", SectorCount
);
277 RtlZeroMemory(&BootSector
, sizeof(FAT16_BOOT_SECTOR
));
278 memcpy(&BootSector
.OEMName
[0], "MSWIN4.1", 8);
279 /* FIXME: Add dummy bootloader for real */
280 BootSector
.Jump
[0] = 0xeb;
281 BootSector
.Jump
[1] = 0x3c;
282 BootSector
.Jump
[2] = 0x90;
283 BootSector
.BytesPerSector
= DiskGeometry
->BytesPerSector
;
284 BootSector
.SectorsPerCluster
= ClusterSize
/ BootSector
.BytesPerSector
;
285 BootSector
.ReservedSectors
= 1;
286 BootSector
.FATCount
= 2;
287 BootSector
.RootEntries
= 512;
288 BootSector
.Sectors
= (SectorCount
< 0x10000) ? (unsigned short)SectorCount
: 0;
289 BootSector
.Media
= 0xf8;
290 BootSector
.FATSectors
= 0; /* Set later. See below. */
291 BootSector
.SectorsPerTrack
= DiskGeometry
->SectorsPerTrack
;
292 BootSector
.Heads
= DiskGeometry
->TracksPerCylinder
;
293 BootSector
.HiddenSectors
= PartitionInfo
->HiddenSectors
;
294 BootSector
.SectorsHuge
= (SectorCount
>= 0x10000) ? (unsigned long)SectorCount
: 0;
295 BootSector
.Drive
= (DiskGeometry
->MediaType
== FixedMedia
) ? 0x80 : 0x00;
296 BootSector
.ExtBootSignature
= 0x29;
297 BootSector
.VolumeID
= CalcVolumeSerialNumber();
298 if ((Label
== NULL
) || (Label
->Buffer
== NULL
))
300 memcpy(&BootSector
.VolumeLabel
[0], "NO NAME ", 11);
304 RtlUnicodeStringToOemString(&VolumeLabel
, Label
, TRUE
);
305 RtlFillMemory(&BootSector
.VolumeLabel
[0], 11, ' ');
306 memcpy(&BootSector
.VolumeLabel
[0], VolumeLabel
.Buffer
,
307 VolumeLabel
.Length
< 11 ? VolumeLabel
.Length
: 11);
308 RtlFreeOemString(&VolumeLabel
);
311 memcpy(&BootSector
.SysType
[0], "FAT12 ", 8);
313 RootDirSectors
= ((BootSector
.RootEntries
* 32) +
314 (BootSector
.BytesPerSector
- 1)) / BootSector
.BytesPerSector
;
316 /* Calculate number of FAT sectors */
317 /* ((BootSector.BytesPerSector * 2) / 3) FAT entries (12bit) fit into one sector */
318 TmpVal1
= SectorCount
- (BootSector
.ReservedSectors
+ RootDirSectors
);
319 TmpVal2
= (((BootSector
.BytesPerSector
* 2) / 3) * BootSector
.SectorsPerCluster
) + BootSector
.FATCount
;
320 TmpVal3
= (TmpVal1
+ (TmpVal2
- 1)) / TmpVal2
;
321 BootSector
.FATSectors
= (unsigned short)(TmpVal3
& 0xffff);
323 DPRINT("BootSector.FATSectors = %hx\n", BootSector
.FATSectors
);
325 /* Init context data */
326 Context
->TotalSectorCount
=
327 1 + (BootSector
.FATSectors
* 2) + RootDirSectors
;
331 Context
->TotalSectorCount
+= SectorCount
;
333 Status
= FatWipeSectors(FileHandle
,
335 (ULONG
)BootSector
.SectorsPerCluster
,
336 (ULONG
)BootSector
.BytesPerSector
,
338 if (!NT_SUCCESS(Status
))
340 DPRINT("FatWipeSectors() failed with status 0x%.08x\n", Status
);
345 Status
= Fat12WriteBootSector(FileHandle
,
348 if (!NT_SUCCESS(Status
))
350 DPRINT("Fat12WriteBootSector() failed with status 0x%.08x\n", Status
);
354 /* Write first FAT copy */
355 Status
= Fat12WriteFAT(FileHandle
,
359 if (!NT_SUCCESS(Status
))
361 DPRINT("Fat12WriteFAT() failed with status 0x%.08x\n", Status
);
365 /* Write second FAT copy */
366 Status
= Fat12WriteFAT(FileHandle
,
367 (ULONG
)BootSector
.FATSectors
,
370 if (!NT_SUCCESS(Status
))
372 DPRINT("Fat12WriteFAT() failed with status 0x%.08x.\n", Status
);
376 Status
= Fat12WriteRootDirectory(FileHandle
,
379 if (!NT_SUCCESS(Status
))
381 DPRINT("Fat12WriteRootDirectory() failed with status 0x%.08x\n", Status
);