ReactOS 0.4.16-dev-334-g4d9f67c
fat12.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS VFAT filesystem library
4 * FILE: fat12.c
5 * PURPOSE: Fat12 support
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Eric Kohl
8 */
9
10/* INCLUDES *******************************************************************/
11
12#include "vfatlib.h"
13
14#define NDEBUG
15#include <debug.h>
16
17
18/* FUNCTIONS ******************************************************************/
19
20static NTSTATUS
24{
27 PFAT16_BOOT_SECTOR NewBootSector;
29
30 /* Allocate buffer for new bootsector */
31 NewBootSector = (PFAT16_BOOT_SECTOR)RtlAllocateHeap(RtlGetProcessHeap (),
32 0,
34 if (NewBootSector == NULL)
36
37 /* Zero the new bootsector */
39
40 /* Copy FAT16 BPB to new bootsector */
41 memcpy(NewBootSector, BootSector,
43 /* FAT16 BPB length (up to (not including) Res2) */
44
45 /* Write the boot sector signature */
46 NewBootSector->Signature1 = 0xAA550000;
47
48 /* Write sector 0 */
49 FileOffset.QuadPart = 0ULL;
51 NULL,
52 NULL,
53 NULL,
55 NewBootSector,
58 NULL);
59 if (!NT_SUCCESS(Status))
60 {
61 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
62 goto done;
63 }
64
66
67done:
68 /* Free the buffer */
69 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBootSector);
70 return Status;
71}
72
73
74static NTSTATUS
79{
84 ULONG i;
85 ULONG Size;
86 ULONG Sectors;
87
88 /* Allocate buffer */
89 Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
90 0,
91 32 * 1024);
92 if (Buffer == NULL)
94
95 /* Zero the buffer */
96 RtlZeroMemory(Buffer, 32 * 1024);
97
98 /* FAT cluster 0 & 1*/
99 Buffer[0] = 0xf8; /* Media type */
100 Buffer[1] = 0xff;
101 Buffer[2] = 0xff;
102
103 /* Write first sector of the FAT */
106 NULL,
107 NULL,
108 NULL,
110 Buffer,
112 &FileOffset,
113 NULL);
114 if (!NT_SUCCESS(Status))
115 {
116 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
117 goto done;
118 }
119
121
122 /* Zero the begin of the buffer */
124
125 /* Zero the rest of the FAT */
126 Sectors = 32 * 1024 / BootSector->BytesPerSector;
127 for (i = 1; i < (ULONG)BootSector->FATSectors; i += Sectors)
128 {
129 /* Zero some sectors of the FAT */
131 if (((ULONG)BootSector->FATSectors - i) <= Sectors)
132 {
133 Sectors = (ULONG)BootSector->FATSectors - i;
134 }
135
136 Size = Sectors * BootSector->BytesPerSector;
138 NULL,
139 NULL,
140 NULL,
142 Buffer,
143 Size,
144 &FileOffset,
145 NULL);
146 if (!NT_SUCCESS(Status))
147 {
148 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
149 goto done;
150 }
151
152 UpdateProgress(Context, Sectors);
153 }
154
155done:
156 /* Free the buffer */
157 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
158 return Status;
159}
160
161
162static NTSTATUS
166{
171 ULONG FirstRootDirSector;
172 ULONG RootDirSectors;
173 ULONG Sectors;
174 ULONG Size;
175 ULONG i;
176
177 DPRINT("BootSector->ReservedSectors = %hu\n", BootSector->ReservedSectors);
178 DPRINT("BootSector->FATSectors = %hu\n", BootSector->FATSectors);
179 DPRINT("BootSector->SectorsPerCluster = %u\n", BootSector->SectorsPerCluster);
180
181 /* Write cluster */
182 RootDirSectors = ((BootSector->RootEntries * 32) +
184 FirstRootDirSector =
185 BootSector->ReservedSectors + (BootSector->FATCount * BootSector->FATSectors);
186
187 DPRINT("RootDirSectors = %lu\n", RootDirSectors);
188 DPRINT("FirstRootDirSector = %lu\n", FirstRootDirSector);
189
190 /* Allocate buffer for the cluster */
191 Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
192 0,
193 32 * 1024);
194 if (Buffer == NULL)
196
197 /* Zero the buffer */
198 RtlZeroMemory(Buffer, 32 * 1024);
199
200 Sectors = 32 * 1024 / BootSector->BytesPerSector;
201 for (i = 0; i < RootDirSectors; i += Sectors)
202 {
203 /* Zero some sectors of the root directory */
204 FileOffset.QuadPart = (FirstRootDirSector + i) * BootSector->BytesPerSector;
205
206 if ((RootDirSectors - i) <= Sectors)
207 {
208 Sectors = RootDirSectors - i;
209 }
210
211 Size = Sectors * BootSector->BytesPerSector;
212
214 NULL,
215 NULL,
216 NULL,
218 Buffer,
219 Size,
220 &FileOffset,
221 NULL);
222 if (!NT_SUCCESS(Status))
223 {
224 DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
225 goto done;
226 }
227
228 UpdateProgress(Context, Sectors);
229 }
230
231done:
232 /* Free the buffer */
233 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
234 return Status;
235}
236
237
241 IN PDISK_GEOMETRY DiskGeometry,
246{
248 OEM_STRING VolumeLabel;
250 ULONG RootDirSectors;
251 ULONG TmpVal1;
252 ULONG TmpVal2;
253 ULONG TmpVal3;
255
256 /* Calculate cluster size */
257 if (ClusterSize == 0)
258 {
259 if (DiskGeometry->MediaType == FixedMedia)
260 {
261 /* 4KB Cluster (Harddisk only) */
262 ClusterSize = 4096;
263 }
264 else
265 {
266 /* 512 byte cluster (floppy) */
267 ClusterSize = 512;
268 }
269 }
270
271 SectorCount = PartitionInfo->PartitionLength.QuadPart >>
272 GetShiftCount(DiskGeometry->BytesPerSector); /* Use shifting to avoid 64-bit division */
273
274 DPRINT("SectorCount = %lu\n", SectorCount);
275
277 memcpy(&BootSector.OEMName[0], "MSWIN4.1", 8);
278 /* FIXME: Add dummy bootloader for real */
279 BootSector.Jump[0] = 0xeb;
280 BootSector.Jump[1] = 0x3c;
281 BootSector.Jump[2] = 0x90;
282 BootSector.BytesPerSector = DiskGeometry->BytesPerSector;
285 BootSector.FATCount = 2;
287 BootSector.Sectors = (SectorCount < 0x10000) ? (unsigned short)SectorCount : 0;
288 BootSector.Media = 0xf8;
289 BootSector.FATSectors = 0; /* Set later. See below. */
290 BootSector.SectorsPerTrack = DiskGeometry->SectorsPerTrack;
291 BootSector.Heads = DiskGeometry->TracksPerCylinder;
292 BootSector.HiddenSectors = PartitionInfo->HiddenSectors;
293 BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0;
294 BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00;
295 BootSector.ExtBootSignature = 0x29;
297 if ((Label == NULL) || (Label->Buffer == NULL))
298 {
299 memcpy(&BootSector.VolumeLabel[0], "NO NAME ", 11);
300 }
301 else
302 {
303 RtlUnicodeStringToOemString(&VolumeLabel, Label, TRUE);
304 RtlFillMemory(&BootSector.VolumeLabel[0], 11, ' ');
305 memcpy(&BootSector.VolumeLabel[0], VolumeLabel.Buffer,
306 VolumeLabel.Length < 11 ? VolumeLabel.Length : 11);
307 RtlFreeOemString(&VolumeLabel);
308 }
309
310 memcpy(&BootSector.SysType[0], "FAT12 ", 8);
311
312 RootDirSectors = ((BootSector.RootEntries * 32) +
314
315 /* Calculate number of FAT sectors */
316 /* ((BootSector.BytesPerSector * 2) / 3) FAT entries (12bit) fit into one sector */
317 TmpVal1 = SectorCount - (BootSector.ReservedSectors + RootDirSectors);
318 TmpVal2 = (((BootSector.BytesPerSector * 2) / 3) * BootSector.SectorsPerCluster) + BootSector.FATCount;
319 TmpVal3 = (TmpVal1 + (TmpVal2 - 1)) / TmpVal2;
320 BootSector.FATSectors = (unsigned short)(TmpVal3 & 0xffff);
321
322 DPRINT("BootSector.FATSectors = %hx\n", BootSector.FATSectors);
323
324 /* Init context data */
325 Context->TotalSectorCount =
326 1 + (BootSector.FATSectors * 2) + RootDirSectors;
327
328 if (!QuickFormat)
329 {
330 Context->TotalSectorCount += SectorCount;
331
336 Context);
337 if (!NT_SUCCESS(Status))
338 {
339 DPRINT("FatWipeSectors() failed with status 0x%.08x\n", Status);
340 return Status;
341 }
342 }
343
345 &BootSector,
346 Context);
347 if (!NT_SUCCESS(Status))
348 {
349 DPRINT("Fat12WriteBootSector() failed with status 0x%.08x\n", Status);
350 return Status;
351 }
352
353 /* Write first FAT copy */
355 0,
356 &BootSector,
357 Context);
358 if (!NT_SUCCESS(Status))
359 {
360 DPRINT("Fat12WriteFAT() failed with status 0x%.08x\n", Status);
361 return Status;
362 }
363
364 /* Write second FAT copy */
366 (ULONG)BootSector.FATSectors,
367 &BootSector,
368 Context);
369 if (!NT_SUCCESS(Status))
370 {
371 DPRINT("Fat12WriteFAT() failed with status 0x%.08x.\n", Status);
372 return Status;
373 }
374
376 &BootSector,
377 Context);
378 if (!NT_SUCCESS(Status))
379 {
380 DPRINT("Fat12WriteRootDirectory() failed with status 0x%.08x\n", Status);
381 }
382
383 return Status;
384}
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
BOOL QuickFormat
Definition: format.c:66
PWCHAR Label
Definition: format.c:70
DWORD ClusterSize
Definition: format.c:67
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:590
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
#define SectorOffset(L)
Definition: cdprocs.h:1622
Definition: bufpool.h:45
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
unsigned short(__cdecl typeof(TIFFCurrentDirectory))(struct tiff *)
Definition: typeof.h:94
static NTSTATUS Fat12WriteRootDirectory(IN HANDLE FileHandle, IN PFAT16_BOOT_SECTOR BootSector, IN OUT PFORMAT_CONTEXT Context)
Definition: fat12.c:163
static NTSTATUS Fat12WriteFAT(IN HANDLE FileHandle, IN ULONG SectorOffset, IN PFAT16_BOOT_SECTOR BootSector, IN OUT PFORMAT_CONTEXT Context)
Definition: fat12.c:75
static NTSTATUS Fat12WriteBootSector(IN HANDLE FileHandle, IN PFAT16_BOOT_SECTOR BootSector, IN OUT PFORMAT_CONTEXT Context)
Definition: fat12.c:21
NTSTATUS Fat12Format(IN HANDLE FileHandle, IN PPARTITION_INFORMATION PartitionInfo, IN PDISK_GEOMETRY DiskGeometry, IN PUNICODE_STRING Label, IN BOOLEAN QuickFormat, IN ULONG ClusterSize, IN OUT PFORMAT_CONTEXT Context)
Definition: fat12.c:239
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID NTAPI RtlFreeOemString(POEM_STRING OemString)
#define RtlFillMemory(Dest, Length, Fill)
Definition: winternl.h:599
struct _FAT16_BOOT_SECTOR * PFAT16_BOOT_SECTOR
if(dx< 0)
Definition: linetemp.h:194
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define ULL(a, b)
Definition: format_msg.c:27
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
NTSYSAPI NTSTATUS NTAPI RtlUnicodeStringToOemString(POEM_STRING DestinationString, PCUNICODE_STRING SourceString, BOOLEAN AllocateDestinationString)
NTSYSAPI NTSTATUS NTAPI NtWriteFile(IN HANDLE hFile, IN HANDLE hEvent OPTIONAL, IN PIO_APC_ROUTINE IoApcRoutine OPTIONAL, IN PVOID IoApcContext OPTIONAL, OUT PIO_STATUS_BLOCK pIoStatusBlock, IN PVOID WriteBuffer, IN ULONG WriteBufferLength, IN PLARGE_INTEGER FileOffset OPTIONAL, IN PULONG LockOperationKey OPTIONAL)
@ FixedMedia
Definition: ntdddisk.h:383
ULONG SectorCount
Definition: part_xbox.c:31
ULONG GetShiftCount(IN ULONG Value)
Definition: common.c:21
NTSTATUS FatWipeSectors(IN HANDLE FileHandle, IN ULONG TotalSectors, IN ULONG SectorsPerCluster, IN ULONG BytesPerSector, IN OUT PFORMAT_CONTEXT Context)
Definition: common.c:56
ULONG CalcVolumeSerialNumber(VOID)
Definition: common.c:35
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
WORD Heads
Definition: fatfs.h:84
WORD RootEntries
Definition: fatfs.h:79
DWORD HiddenSectors
Definition: fatfs.h:85
WORD SectorsPerTrack
Definition: fatfs.h:83
WORD ReservedSectors
Definition: fatfs.h:77
BYTE SectorsPerCluster
Definition: fatfs.h:76
WORD BytesPerSector
Definition: fatfs.h:75
unsigned long Signature1
Definition: vfatlib.h:51
#define FIELD_OFFSET(t, f)
Definition: typedefs.h:255
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
STRING OEM_STRING
Definition: umtypes.h:203
VOID UpdateProgress(PFORMAT_CONTEXT Context, ULONG Increment)
Definition: vfatlib.c:321
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_In_ ULONG _In_ struct _SET_PARTITION_INFORMATION_EX * PartitionInfo
Definition: iofuncs.h:2105