Author: hbelusca Date: Sun Dec 27 00:57:51 2015 New Revision: 70433
URL: http://svn.reactos.org/svn/reactos?rev=70433&view=rev Log: [VFATLIB] - Gather common code in one file instead of duplicating it everywhere. - Implement full format for FAT12 and FAT16 (using the same function Fat1216WipeSectors), based on similar code for FAT32 implemented by Eric Kohl in revision 63693.
Added: trunk/reactos/lib/fslib/vfatlib/common.c (with props) trunk/reactos/lib/fslib/vfatlib/common.h (with props) Modified: trunk/reactos/lib/fslib/vfatlib/CMakeLists.txt trunk/reactos/lib/fslib/vfatlib/fat12.c trunk/reactos/lib/fslib/vfatlib/fat16.c trunk/reactos/lib/fslib/vfatlib/fat32.c trunk/reactos/lib/fslib/vfatlib/vfatlib.c trunk/reactos/lib/fslib/vfatlib/vfatlib.h
Modified: trunk/reactos/lib/fslib/vfatlib/CMakeLists.txt URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/CMakeList... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/CMakeLists.txt [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/CMakeLists.txt [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -7,6 +7,7 @@ check/file.c check/io.c check/lfn.c + common.c fat12.c fat16.c fat32.c
Added: trunk/reactos/lib/fslib/vfatlib/common.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/common.c?... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/common.c (added) +++ trunk/reactos/lib/fslib/vfatlib/common.c [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -0,0 +1,136 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS VFAT filesystem library + * FILE: lib\fslib\vfatlib\common.c + * PURPOSE: Common code for Fat support + * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) + * Eric Kohl + */ + +/* INCLUDES *******************************************************************/ + +#include "vfatlib.h" + +#define NDEBUG +#include <debug.h> + +/* FUNCTIONS ******************************************************************/ + +ULONG +GetShiftCount(IN ULONG Value) +{ + ULONG i = 1; + + while (Value > 0) + { + i++; + Value /= 2; + } + + return i - 2; +} + +ULONG +CalcVolumeSerialNumber(VOID) +{ + LARGE_INTEGER SystemTime; + TIME_FIELDS TimeFields; + ULONG Serial; + PUCHAR Buffer; + + NtQuerySystemTime(&SystemTime); + RtlTimeToTimeFields(&SystemTime, &TimeFields); + + Buffer = (PUCHAR)&Serial; + Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF); + Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF); + Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF); + Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF); + + return Serial; +} + +/***** Wipe function for FAT12 and FAT16 formats, adapted from FAT32 code *****/ +NTSTATUS +Fat1216WipeSectors( + IN HANDLE FileHandle, + IN PFAT16_BOOT_SECTOR BootSector, + IN OUT PFORMAT_CONTEXT Context) +{ + IO_STATUS_BLOCK IoStatusBlock; + PUCHAR Buffer; + LARGE_INTEGER FileOffset; + ULONGLONG Sector; + ULONG SectorsHuge; + ULONG Length; + NTSTATUS Status; + + /* Allocate buffer for the cluster */ + Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(), + HEAP_ZERO_MEMORY, + BootSector->SectorsPerCluster * BootSector->BytesPerSector); + if (Buffer == NULL) + return STATUS_INSUFFICIENT_RESOURCES; + + Sector = 0; + Length = BootSector->SectorsPerCluster * BootSector->BytesPerSector; + + SectorsHuge = (BootSector->SectorsHuge != 0 ? BootSector->SectorsHuge : BootSector->Sectors); + + while (Sector + BootSector->SectorsPerCluster < SectorsHuge) + { + FileOffset.QuadPart = Sector * BootSector->BytesPerSector; + + Status = NtWriteFile(FileHandle, + NULL, + NULL, + NULL, + &IoStatusBlock, + Buffer, + Length, + &FileOffset, + NULL); + if (!NT_SUCCESS(Status)) + { + DPRINT("NtWriteFile() failed (Status %lx)\n", Status); + goto done; + } + + UpdateProgress(Context, (ULONG)BootSector->SectorsPerCluster); + + Sector += BootSector->SectorsPerCluster; + } + + if (Sector + BootSector->SectorsPerCluster > SectorsHuge) + { + DPRINT("Remaining sectors %lu\n", SectorsHuge - Sector); + + FileOffset.QuadPart = Sector * BootSector->BytesPerSector; + Length = (SectorsHuge - Sector) * BootSector->BytesPerSector; + + Status = NtWriteFile(FileHandle, + NULL, + NULL, + NULL, + &IoStatusBlock, + Buffer, + Length, + &FileOffset, + NULL); + if (!NT_SUCCESS(Status)) + { + DPRINT("NtWriteFile() failed (Status %lx)\n", Status); + goto done; + } + + UpdateProgress(Context, SectorsHuge - Sector); + } + +done: + /* Free the buffer */ + RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer); + + return Status; +} + +/* EOF */
Propchange: trunk/reactos/lib/fslib/vfatlib/common.c ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/reactos/lib/fslib/vfatlib/common.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/common.h?... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/common.h (added) +++ trunk/reactos/lib/fslib/vfatlib/common.h [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -0,0 +1,24 @@ +/* + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS VFAT filesystem library + * FILE: lib\fslib\vfatlib\common.h + * PURPOSE: Common code for Fat support + * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) + * Eric Kohl + */ + +#ifndef _VFATCOMMON_H_ +#define _VFATCOMMON_H_ + +ULONG GetShiftCount(IN ULONG Value); +ULONG CalcVolumeSerialNumber(VOID); + +NTSTATUS +Fat1216WipeSectors( + IN HANDLE FileHandle, + IN PFAT16_BOOT_SECTOR BootSector, + IN OUT PFORMAT_CONTEXT Context); + +#endif /* _VFATCOMMON_H_ */ + +/* EOF */
Propchange: trunk/reactos/lib/fslib/vfatlib/common.h ------------------------------------------------------------------------------ svn:eol-style = native
Modified: trunk/reactos/lib/fslib/vfatlib/fat12.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat12.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat12.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat12.c [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -5,49 +5,17 @@ * PURPOSE: Fat12 support * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) * Eric Kohl - * REVISIONS: - * EK 05/04-2003 Created */ + +/* INCLUDES *******************************************************************/ + #include "vfatlib.h"
#define NDEBUG #include <debug.h>
-static ULONG -GetShiftCount(IN ULONG Value) -{ - ULONG i = 1; - - while (Value > 0) - { - i++; - Value /= 2; - } - - return i - 2; -} - - -static ULONG -CalcVolumeSerialNumber(VOID) -{ - LARGE_INTEGER SystemTime; - TIME_FIELDS TimeFields; - ULONG Serial; - PUCHAR Buffer; - - NtQuerySystemTime(&SystemTime); - RtlTimeToTimeFields(&SystemTime, &TimeFields); - - Buffer = (PUCHAR)&Serial; - Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF); - Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF); - Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF); - Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF); - - return Serial; -} - + +/* FUNCTIONS ******************************************************************/
static NTSTATUS Fat12WriteBootSector(IN HANDLE FileHandle, @@ -323,7 +291,7 @@ BootSector.Heads = DiskGeometry->TracksPerCylinder; BootSector.HiddenSectors = PartitionInfo->HiddenSectors; BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0; - BootSector.Drive = DiskGeometry->MediaType == FixedMedia ? 0x80 : 0x00; + BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; BootSector.VolumeID = CalcVolumeSerialNumber(); if ((Label == NULL) || (Label->Buffer == NULL)) @@ -357,6 +325,20 @@ Context->TotalSectorCount = 1 + (BootSector.FATSectors * 2) + RootDirSectors;
+ if (!QuickFormat) + { + Context->TotalSectorCount += SectorCount; + + Status = Fat1216WipeSectors(FileHandle, + &BootSector, + Context); + if (!NT_SUCCESS(Status)) + { + DPRINT("Fat12WipeSectors() failed with status 0x%.08x\n", Status); + return Status; + } + } + Status = Fat12WriteBootSector(FileHandle, &BootSector, Context); @@ -396,10 +378,5 @@ DPRINT("Fat12WriteRootDirectory() failed with status 0x%.08x\n", Status); }
- if (!QuickFormat) - { - /* FIXME: Fill remaining sectors */ - } - return Status; }
Modified: trunk/reactos/lib/fslib/vfatlib/fat16.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat16.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat16.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat16.c [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -5,49 +5,17 @@ * PURPOSE: Fat16 support * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) * Eric Kohl - * REVISIONS: - * EK 05/04-2003 Created */ + +/* INCLUDES *******************************************************************/ + #include "vfatlib.h"
#define NDEBUG #include <debug.h>
-static ULONG -GetShiftCount(IN ULONG Value) -{ - ULONG i = 1; - - while (Value > 0) - { - i++; - Value /= 2; - } - - return i - 2; -} - - -static ULONG -CalcVolumeSerialNumber(VOID) -{ - LARGE_INTEGER SystemTime; - TIME_FIELDS TimeFields; - ULONG Serial; - PUCHAR Buffer; - - NtQuerySystemTime (&SystemTime); - RtlTimeToTimeFields (&SystemTime, &TimeFields); - - Buffer = (PUCHAR)&Serial; - Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF); - Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF); - Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF); - Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF); - - return Serial; -} - + +/* FUNCTIONS ******************************************************************/
static NTSTATUS Fat16WriteBootSector(IN HANDLE FileHandle, @@ -330,7 +298,7 @@ BootSector.Heads = DiskGeometry->TracksPerCylinder; BootSector.HiddenSectors = PartitionInfo->HiddenSectors; BootSector.SectorsHuge = (SectorCount >= 0x10000) ? (unsigned long)SectorCount : 0; - BootSector.Drive = DiskGeometry->MediaType == FixedMedia ? 0x80 : 0x00; + BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; BootSector.VolumeID = CalcVolumeSerialNumber(); if ((Label == NULL) || (Label->Buffer == NULL)) @@ -365,6 +333,20 @@ Context->TotalSectorCount = 1 + (BootSector.FATSectors * 2) + RootDirSectors;
+ if (!QuickFormat) + { + Context->TotalSectorCount += SectorCount; + + Status = Fat1216WipeSectors(FileHandle, + &BootSector, + Context); + if (!NT_SUCCESS(Status)) + { + DPRINT("Fat16WipeSectors() failed with status 0x%.08x\n", Status); + return Status; + } + } + Status = Fat16WriteBootSector(FileHandle, &BootSector, Context); @@ -404,10 +386,5 @@ DPRINT("Fat16WriteRootDirectory() failed with status 0x%.08x\n", Status); }
- if (!QuickFormat) - { - /* FIXME: Fill remaining sectors */ - } - return Status; }
Modified: trunk/reactos/lib/fslib/vfatlib/fat32.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/fat32.c?r... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/fat32.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/fat32.c [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -5,49 +5,17 @@ * PURPOSE: Fat32 support * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) * Eric Kohl - * REVISIONS: - * EK 05/04-2003 Created */ + +/* INCLUDES *******************************************************************/ + #include "vfatlib.h"
#define NDEBUG #include <debug.h>
-static ULONG -GetShiftCount(IN ULONG Value) -{ - ULONG i = 1; - - while (Value > 0) - { - i++; - Value /= 2; - } - - return i - 2; -} - - -static ULONG -CalcVolumeSerialNumber(VOID) -{ - LARGE_INTEGER SystemTime; - TIME_FIELDS TimeFields; - ULONG Serial; - PUCHAR Buffer; - - NtQuerySystemTime (&SystemTime); - RtlTimeToTimeFields (&SystemTime, &TimeFields); - - Buffer = (PUCHAR)&Serial; - Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF); - Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF); - Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF); - Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF); - - return Serial; -} - + +/* FUNCTIONS ******************************************************************/
static NTSTATUS Fat32WriteBootSector(IN HANDLE FileHandle, @@ -490,7 +458,7 @@ BootSector.BootBackup = 6; BootSector.Drive = (DiskGeometry->MediaType == FixedMedia) ? 0x80 : 0x00; BootSector.ExtBootSignature = 0x29; - BootSector.VolumeID = CalcVolumeSerialNumber (); + BootSector.VolumeID = CalcVolumeSerialNumber(); if ((Label == NULL) || (Label->Buffer == NULL)) { memcpy(&BootSector.VolumeLabel[0], "NO NAME ", 11);
Modified: trunk/reactos/lib/fslib/vfatlib/vfatlib.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/vfatlib.c... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/vfatlib.c [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/vfatlib.c [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -4,13 +4,17 @@ * FILE: lib\fslib\vfatlib\vfatlib.c * PURPOSE: Main API * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net) - * REVISIONS: - * CSH 05/04-2003 Created */ + +/* INCLUDES *******************************************************************/ + #include "vfatlib.h"
#define NDEBUG #include <debug.h> + + +/* GLOBALS & FUNCTIONS ********************************************************/
PFMIFSCALLBACK ChkdskCallback = NULL; PVOID FsCheckMemQueue; @@ -282,7 +286,7 @@ if (Callback != NULL) { Context.Success = (BOOLEAN)(NT_SUCCESS(Status)); - Callback (DONE, 0, (PVOID)&Context.Success); + Callback(DONE, 0, (PVOID)&Context.Success); }
DPRINT("VfatFormat() done. Status 0x%.08x\n", Status); @@ -307,7 +311,7 @@ Context->Percent = NewPercent; if (Context->Callback != NULL) { - Context->Callback (PROGRESS, 0, &Context->Percent); + Context->Callback(PROGRESS, 0, &Context->Percent); } } }
Modified: trunk/reactos/lib/fslib/vfatlib/vfatlib.h URL: http://svn.reactos.org/svn/reactos/trunk/reactos/lib/fslib/vfatlib/vfatlib.h... ============================================================================== --- trunk/reactos/lib/fslib/vfatlib/vfatlib.h [iso-8859-1] (original) +++ trunk/reactos/lib/fslib/vfatlib/vfatlib.h [iso-8859-1] Sun Dec 27 00:57:51 2015 @@ -57,7 +57,6 @@ unsigned long Signature1; // 508 } FAT16_BOOT_SECTOR, *PFAT16_BOOT_SECTOR;
- typedef struct _FAT32_BOOT_SECTOR { unsigned char magic0; // 0 @@ -114,6 +113,8 @@ ULONG Percent; } FORMAT_CONTEXT, *PFORMAT_CONTEXT;
+#include "common.h" +
NTSTATUS Fat12Format(HANDLE FileHandle,