2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS VFAT filesystem library
5 * PURPOSE: Fat16 support
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
9 * EK 05/04-2003 Created
17 GetShiftCount(IN ULONG Value
)
32 CalcVolumeSerialNumber(VOID
)
34 LARGE_INTEGER SystemTime
;
35 TIME_FIELDS TimeFields
;
39 NtQuerySystemTime (&SystemTime
);
40 RtlTimeToTimeFields (&SystemTime
, &TimeFields
);
42 Buffer
= (PUCHAR
)&Serial
;
43 Buffer
[0] = (UCHAR
)(TimeFields
.Year
& 0xFF) + (UCHAR
)(TimeFields
.Hour
& 0xFF);
44 Buffer
[1] = (UCHAR
)(TimeFields
.Year
>> 8) + (UCHAR
)(TimeFields
.Minute
& 0xFF);
45 Buffer
[2] = (UCHAR
)(TimeFields
.Month
& 0xFF) + (UCHAR
)(TimeFields
.Second
& 0xFF);
46 Buffer
[3] = (UCHAR
)(TimeFields
.Day
& 0xFF) + (UCHAR
)(TimeFields
.Milliseconds
& 0xFF);
53 Fat16WriteBootSector(IN HANDLE FileHandle
,
54 IN PFAT16_BOOT_SECTOR BootSector
,
55 IN OUT PFORMAT_CONTEXT Context
)
57 IO_STATUS_BLOCK IoStatusBlock
;
59 PFAT16_BOOT_SECTOR NewBootSector
;
60 LARGE_INTEGER FileOffset
;
62 /* Allocate buffer for new bootsector */
63 NewBootSector
= (PFAT16_BOOT_SECTOR
)RtlAllocateHeap(RtlGetProcessHeap(),
65 BootSector
->BytesPerSector
);
66 if (NewBootSector
== NULL
)
67 return STATUS_INSUFFICIENT_RESOURCES
;
69 /* Zero the new bootsector */
70 memset(NewBootSector
, 0, BootSector
->BytesPerSector
);
72 /* Copy FAT16 BPB to new bootsector */
73 memcpy(&NewBootSector
->OEMName
[0],
74 &BootSector
->OEMName
[0],
75 FIELD_OFFSET(FAT16_BOOT_SECTOR
, Res2
) - FIELD_OFFSET(FAT16_BOOT_SECTOR
, OEMName
)); /* FAT16 BPB length (up to (not including) Res2) */
77 /* Write the boot sector signature */
78 NewBootSector
->Signature1
= 0xAA550000;
81 FileOffset
.QuadPart
= 0ULL;
82 Status
= NtWriteFile(FileHandle
,
88 BootSector
->BytesPerSector
,
91 if (!NT_SUCCESS(Status
))
93 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
94 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBootSector
);
98 UpdateProgress(Context
, 1);
100 /* Free the new boot sector */
101 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBootSector
);
108 Fat16WriteFAT(IN HANDLE FileHandle
,
109 IN ULONG SectorOffset
,
110 IN PFAT16_BOOT_SECTOR BootSector
,
111 IN OUT PFORMAT_CONTEXT Context
)
113 IO_STATUS_BLOCK IoStatusBlock
;
116 LARGE_INTEGER FileOffset
;
120 /* Allocate buffer */
121 Buffer
= (PUCHAR
)RtlAllocateHeap(RtlGetProcessHeap(),
125 return STATUS_INSUFFICIENT_RESOURCES
;
127 /* Zero the buffer */
128 memset(Buffer
, 0, 32 * 1024);
131 Buffer
[0] = 0xf8; /* Media type */
135 Buffer
[2] = 0xff; /* Clean shutdown, no disk read/write errors, end-of-cluster (EOC) mark */
138 /* Write first sector of the FAT */
139 FileOffset
.QuadPart
= (SectorOffset
+ BootSector
->ReservedSectors
) * BootSector
->BytesPerSector
;
140 Status
= NtWriteFile(FileHandle
,
146 BootSector
->BytesPerSector
,
149 if (!NT_SUCCESS(Status
))
151 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
152 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
156 UpdateProgress(Context
, 1);
158 /* Zero the begin of the buffer */
159 memset(Buffer
, 0, 4);
161 /* Zero the rest of the FAT */
162 Sectors
= 32 * 1024 / BootSector
->BytesPerSector
;
163 for (i
= 1; i
< (ULONG
)BootSector
->FATSectors
; i
+= Sectors
)
165 /* Zero some sectors of the FAT */
166 FileOffset
.QuadPart
= (SectorOffset
+ BootSector
->ReservedSectors
+ i
) * BootSector
->BytesPerSector
;
168 if (((ULONG
)BootSector
->FATSectors
- i
) <= Sectors
)
170 Sectors
= (ULONG
)BootSector
->FATSectors
- i
;
173 Status
= NtWriteFile(FileHandle
,
179 Sectors
* BootSector
->BytesPerSector
,
182 if (!NT_SUCCESS(Status
))
184 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
185 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
189 UpdateProgress(Context
, Sectors
);
192 /* Free the buffer */
193 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
200 Fat16WriteRootDirectory(IN HANDLE FileHandle
,
201 IN PFAT16_BOOT_SECTOR BootSector
,
202 IN OUT PFORMAT_CONTEXT Context
)
204 IO_STATUS_BLOCK IoStatusBlock
;
205 NTSTATUS Status
= STATUS_SUCCESS
;
207 LARGE_INTEGER FileOffset
;
208 ULONG FirstRootDirSector
;
209 ULONG RootDirSectors
;
213 DPRINT("BootSector->ReservedSectors = %hu\n", BootSector
->ReservedSectors
);
214 DPRINT("BootSector->FATSectors = %hu\n", BootSector
->FATSectors
);
215 DPRINT("BootSector->SectorsPerCluster = %u\n", BootSector
->SectorsPerCluster
);
218 RootDirSectors
= ((BootSector
->RootEntries
* 32) +
219 (BootSector
->BytesPerSector
- 1)) / BootSector
->BytesPerSector
;
221 BootSector
->ReservedSectors
+ (BootSector
->FATCount
* BootSector
->FATSectors
);
223 DPRINT("RootDirSectors = %lu\n", RootDirSectors
);
224 DPRINT("FirstRootDirSector = %lu\n", FirstRootDirSector
);
226 /* Allocate buffer for the cluster */
227 Buffer
= (PUCHAR
)RtlAllocateHeap(RtlGetProcessHeap(),
231 return STATUS_INSUFFICIENT_RESOURCES
;
233 /* Zero the buffer */
234 memset(Buffer
, 0, 32 * 1024);
236 Sectors
= 32 * 1024 / BootSector
->BytesPerSector
;
237 for (i
= 0; i
< RootDirSectors
; i
+= Sectors
)
239 /* Zero some sectors of the root directory */
240 FileOffset
.QuadPart
= (FirstRootDirSector
+ i
) * BootSector
->BytesPerSector
;
242 if ((RootDirSectors
- i
) <= Sectors
)
244 Sectors
= RootDirSectors
- i
;
247 Status
= NtWriteFile(FileHandle
,
253 Sectors
* BootSector
->BytesPerSector
,
256 if (!NT_SUCCESS(Status
))
258 DPRINT("NtWriteFile() failed (Status %lx)\n", Status
);
259 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
263 UpdateProgress(Context
, Sectors
);
266 /* Free the buffer */
267 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer
);
274 Fat16Format(IN HANDLE FileHandle
,
275 IN PPARTITION_INFORMATION PartitionInfo
,
276 IN PDISK_GEOMETRY DiskGeometry
,
277 IN PUNICODE_STRING Label
,
278 IN BOOLEAN QuickFormat
,
279 IN ULONG ClusterSize
,
280 IN OUT PFORMAT_CONTEXT Context
)
282 FAT16_BOOT_SECTOR BootSector
;
283 OEM_STRING VolumeLabel
;
285 ULONG RootDirSectors
;
291 /* Calculate cluster size */
292 if (ClusterSize
== 0)
294 if (PartitionInfo
->PartitionLength
.QuadPart
< 16LL * 1024LL * 1024LL)
296 /* Partition < 16MB ==> 1KB Cluster */
299 else if (PartitionInfo
->PartitionLength
.QuadPart
< 128LL * 1024LL * 1024LL)
301 /* Partition < 128MB ==> 2KB Cluster */
304 else if (PartitionInfo
->PartitionLength
.QuadPart
< 256LL * 1024LL * 1024LL)
306 /* Partition < 256MB ==> 4KB Cluster */
311 /* Partition >= 256MB (< 512MB) ==> 8KB Cluster */
316 SectorCount
= PartitionInfo
->PartitionLength
.QuadPart
>>
317 GetShiftCount(DiskGeometry
->BytesPerSector
); /* Use shifting to avoid 64-bit division */
319 memset(&BootSector
, 0, sizeof(FAT16_BOOT_SECTOR
));
320 memcpy(&BootSector
.OEMName
[0], "MSWIN4.1", 8);
321 BootSector
.BytesPerSector
= DiskGeometry
->BytesPerSector
;
322 BootSector
.SectorsPerCluster
= ClusterSize
/ BootSector
.BytesPerSector
;
323 BootSector
.ReservedSectors
= 1;
324 BootSector
.FATCount
= 2;
325 BootSector
.RootEntries
= 512;
326 BootSector
.Sectors
= (SectorCount
< 0x10000) ? (unsigned short)SectorCount
: 0;
327 BootSector
.Media
= 0xf8;
328 BootSector
.FATSectors
= 0; /* Set later. See below. */
329 BootSector
.SectorsPerTrack
= DiskGeometry
->SectorsPerTrack
;
330 BootSector
.Heads
= DiskGeometry
->TracksPerCylinder
;
331 BootSector
.HiddenSectors
= PartitionInfo
->HiddenSectors
;
332 BootSector
.SectorsHuge
= (SectorCount
>= 0x10000) ? (unsigned long)SectorCount
: 0;
333 BootSector
.Drive
= DiskGeometry
->MediaType
== FixedMedia
? 0x80 : 0x00;
334 BootSector
.ExtBootSignature
= 0x29;
335 BootSector
.VolumeID
= CalcVolumeSerialNumber();
336 if ((Label
== NULL
) || (Label
->Buffer
== NULL
))
338 memcpy(&BootSector
.VolumeLabel
[0], "NO NAME ", 11);
342 RtlUnicodeStringToOemString(&VolumeLabel
, Label
, TRUE
);
343 memset(&BootSector
.VolumeLabel
[0], ' ', 11);
344 memcpy(&BootSector
.VolumeLabel
[0], VolumeLabel
.Buffer
,
345 VolumeLabel
.Length
< 11 ? VolumeLabel
.Length
: 11);
346 RtlFreeOemString(&VolumeLabel
);
349 memcpy(&BootSector
.SysType
[0], "FAT16 ", 8);
351 DPRINT("BootSector.SectorsHuge = %lx\n", BootSector
.SectorsHuge
);
353 RootDirSectors
= ((BootSector
.RootEntries
* 32) +
354 (BootSector
.BytesPerSector
- 1)) / BootSector
.BytesPerSector
;
356 /* Calculate number of FAT sectors */
357 /* (BootSector.BytesPerSector / 2) FAT entries (16bit) fit into one sector */
358 TmpVal1
= SectorCount
- (BootSector
.ReservedSectors
+ RootDirSectors
);
359 TmpVal2
= ((BootSector
.BytesPerSector
/ 2) * BootSector
.SectorsPerCluster
) + BootSector
.FATCount
;
360 TmpVal3
= (TmpVal1
+ (TmpVal2
- 1)) / TmpVal2
;
361 BootSector
.FATSectors
= (unsigned short)(TmpVal3
& 0xffff);
362 DPRINT("BootSector.FATSectors = %hu\n", BootSector
.FATSectors
);
364 /* Init context data */
365 Context
->TotalSectorCount
=
366 1 + (BootSector
.FATSectors
* 2) + RootDirSectors
;
368 Status
= Fat16WriteBootSector(FileHandle
,
371 if (!NT_SUCCESS(Status
))
373 DPRINT("Fat16WriteBootSector() failed with status 0x%.08x\n", Status
);
377 /* Write first FAT copy */
378 Status
= Fat16WriteFAT(FileHandle
,
382 if (!NT_SUCCESS(Status
))
384 DPRINT("Fat16WriteFAT() failed with status 0x%.08x\n", Status
);
388 /* Write second FAT copy */
389 Status
= Fat16WriteFAT(FileHandle
,
390 (ULONG
)BootSector
.FATSectors
,
393 if (!NT_SUCCESS(Status
))
395 DPRINT("Fat16WriteFAT() failed with status 0x%.08x.\n", Status
);
399 Status
= Fat16WriteRootDirectory(FileHandle
,
402 if (!NT_SUCCESS(Status
))
404 DPRINT("Fat16WriteRootDirectory() failed with status 0x%.08x\n", Status
);
409 /* FIXME: Fill remaining sectors */