2015-12-27 00:57:51 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2016-03-02 01:08:54 +00:00
|
|
|
* Hermes Belusca-Maito (hermes.belusca@sfr.fr)
|
2015-12-27 00:57:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2016-01-13 14:12:04 +00:00
|
|
|
/***** Wipe function for FAT12, FAT16 and FAT32 formats *****/
|
2015-12-27 00:57:51 +00:00
|
|
|
NTSTATUS
|
2016-01-11 20:44:06 +00:00
|
|
|
FatWipeSectors(
|
2015-12-27 00:57:51 +00:00
|
|
|
IN HANDLE FileHandle,
|
2016-01-11 20:44:06 +00:00
|
|
|
IN ULONG TotalSectors,
|
|
|
|
IN ULONG SectorsPerCluster,
|
|
|
|
IN ULONG BytesPerSector,
|
2015-12-27 00:57:51 +00:00
|
|
|
IN OUT PFORMAT_CONTEXT Context)
|
|
|
|
{
|
|
|
|
IO_STATUS_BLOCK IoStatusBlock;
|
|
|
|
PUCHAR Buffer;
|
|
|
|
LARGE_INTEGER FileOffset;
|
|
|
|
ULONGLONG Sector;
|
|
|
|
ULONG Length;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
Length = SectorsPerCluster * BytesPerSector;
|
|
|
|
|
2015-12-27 00:57:51 +00:00
|
|
|
/* Allocate buffer for the cluster */
|
|
|
|
Buffer = (PUCHAR)RtlAllocateHeap(RtlGetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
2016-01-11 20:44:06 +00:00
|
|
|
Length);
|
2015-12-27 00:57:51 +00:00
|
|
|
if (Buffer == NULL)
|
|
|
|
return STATUS_INSUFFICIENT_RESOURCES;
|
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
/* Wipe all clusters */
|
2015-12-27 00:57:51 +00:00
|
|
|
Sector = 0;
|
2016-01-11 20:44:06 +00:00
|
|
|
while (Sector + SectorsPerCluster < TotalSectors)
|
2015-12-27 00:57:51 +00:00
|
|
|
{
|
2016-01-11 20:44:06 +00:00
|
|
|
FileOffset.QuadPart = Sector * BytesPerSector;
|
2015-12-27 00:57:51 +00:00
|
|
|
|
|
|
|
Status = NtWriteFile(FileHandle,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&IoStatusBlock,
|
|
|
|
Buffer,
|
|
|
|
Length,
|
|
|
|
&FileOffset,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
UpdateProgress(Context, SectorsPerCluster);
|
2015-12-27 00:57:51 +00:00
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
Sector += SectorsPerCluster;
|
2015-12-27 00:57:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
/* Wipe the trailing space behind the last cluster */
|
|
|
|
if (Sector < TotalSectors)
|
2015-12-27 00:57:51 +00:00
|
|
|
{
|
2016-01-11 20:44:06 +00:00
|
|
|
DPRINT("Remaining sectors %lu\n", TotalSectors - Sector);
|
2015-12-27 00:57:51 +00:00
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
FileOffset.QuadPart = Sector * BytesPerSector;
|
|
|
|
Length = (TotalSectors - Sector) * BytesPerSector;
|
2015-12-27 00:57:51 +00:00
|
|
|
|
|
|
|
Status = NtWriteFile(FileHandle,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&IoStatusBlock,
|
|
|
|
Buffer,
|
|
|
|
Length,
|
|
|
|
&FileOffset,
|
|
|
|
NULL);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("NtWriteFile() failed (Status %lx)\n", Status);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-01-11 20:44:06 +00:00
|
|
|
UpdateProgress(Context, TotalSectors - Sector);
|
2015-12-27 00:57:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
/* Free the buffer */
|
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|