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
->OEMName
[0],
42 &BootSector
->OEMName
[0],
43 FIELD_OFFSET(FAT16_BOOT_SECTOR
, Res2
) - FIELD_OFFSET(FAT16_BOOT_SECTOR
, OEMName
));
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 BootSector
.BytesPerSector
= DiskGeometry
->BytesPerSector
;
280 BootSector
.SectorsPerCluster
= ClusterSize
/ BootSector
.BytesPerSector
;
281 BootSector
.ReservedSectors
= 1;
282 BootSector
.FATCount
= 2;
283 BootSector
.RootEntries
= 512;
284 BootSector
.Sectors
= (SectorCount
< 0x10000) ? (unsigned short)SectorCount
: 0;
285 BootSector
.Media
= 0xf8;
286 BootSector
.FATSectors
= 0; /* Set later. See below. */
287 BootSector
.SectorsPerTrack
= DiskGeometry
->SectorsPerTrack
;
288 BootSector
.Heads
= DiskGeometry
->TracksPerCylinder
;
289 BootSector
.HiddenSectors
= PartitionInfo
->HiddenSectors
;
290 BootSector
.SectorsHuge
= (SectorCount
>= 0x10000) ? (unsigned long)SectorCount
: 0;
291 BootSector
.Drive
= (DiskGeometry
->MediaType
== FixedMedia
) ? 0x80 : 0x00;
292 BootSector
.ExtBootSignature
= 0x29;
293 BootSector
.VolumeID
= CalcVolumeSerialNumber();
294 if ((Label
== NULL
) || (Label
->Buffer
== NULL
))
296 memcpy(&BootSector
.VolumeLabel
[0], "NO NAME ", 11);
300 RtlUnicodeStringToOemString(&VolumeLabel
, Label
, TRUE
);
301 RtlFillMemory(&BootSector
.VolumeLabel
[0], 11, ' ');
302 memcpy(&BootSector
.VolumeLabel
[0], VolumeLabel
.Buffer
,
303 VolumeLabel
.Length
< 11 ? VolumeLabel
.Length
: 11);
304 RtlFreeOemString(&VolumeLabel
);
307 memcpy(&BootSector
.SysType
[0], "FAT12 ", 8);
309 RootDirSectors
= ((BootSector
.RootEntries
* 32) +
310 (BootSector
.BytesPerSector
- 1)) / BootSector
.BytesPerSector
;
312 /* Calculate number of FAT sectors */
313 /* ((BootSector.BytesPerSector * 2) / 3) FAT entries (12bit) fit into one sector */
314 TmpVal1
= SectorCount
- (BootSector
.ReservedSectors
+ RootDirSectors
);
315 TmpVal2
= (((BootSector
.BytesPerSector
* 2) / 3) * BootSector
.SectorsPerCluster
) + BootSector
.FATCount
;
316 TmpVal3
= (TmpVal1
+ (TmpVal2
- 1)) / TmpVal2
;
317 BootSector
.FATSectors
= (unsigned short)(TmpVal3
& 0xffff);
319 DPRINT("BootSector.FATSectors = %hx\n", BootSector
.FATSectors
);
321 /* Init context data */
322 Context
->TotalSectorCount
=
323 1 + (BootSector
.FATSectors
* 2) + RootDirSectors
;
327 Context
->TotalSectorCount
+= SectorCount
;
329 Status
= FatWipeSectors(FileHandle
,
331 (ULONG
)BootSector
.SectorsPerCluster
,
332 (ULONG
)BootSector
.BytesPerSector
,
334 if (!NT_SUCCESS(Status
))
336 DPRINT("FatWipeSectors() failed with status 0x%.08x\n", Status
);
341 Status
= Fat12WriteBootSector(FileHandle
,
344 if (!NT_SUCCESS(Status
))
346 DPRINT("Fat12WriteBootSector() failed with status 0x%.08x\n", Status
);
350 /* Write first FAT copy */
351 Status
= Fat12WriteFAT(FileHandle
,
355 if (!NT_SUCCESS(Status
))
357 DPRINT("Fat12WriteFAT() failed with status 0x%.08x\n", Status
);
361 /* Write second FAT copy */
362 Status
= Fat12WriteFAT(FileHandle
,
363 (ULONG
)BootSector
.FATSectors
,
366 if (!NT_SUCCESS(Status
))
368 DPRINT("Fat12WriteFAT() failed with status 0x%.08x.\n", Status
);
372 Status
= Fat12WriteRootDirectory(FileHandle
,
375 if (!NT_SUCCESS(Status
))
377 DPRINT("Fat12WriteRootDirectory() failed with status 0x%.08x\n", Status
);