Fixed typos

Added more safety checks
Minor cleanup

svn path=/trunk/; revision=1662
This commit is contained in:
Eric Kohl 2001-03-06 08:19:58 +00:00
parent de10767975
commit 2d5f3d1c74
2 changed files with 245 additions and 174 deletions

View file

@ -20,7 +20,7 @@
NTSTATUS
VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
IN ULONG DiskSector,
IN ULONG DiskSector,
IN ULONG SectorCount,
IN OUT PUCHAR Buffer)
{
@ -37,7 +37,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
KeInitializeEvent (&event, NotificationEvent, FALSE);
sectorSize = BLOCKSIZE * SectorCount;
DPRINT ("VFATReadSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
DPRINT ("VfatReadSectors(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
pDeviceObject, DiskSector, Buffer);
DPRINT ("sectorNumber %08lx:%08lx sectorSize %ld\n",
(unsigned long int) sectorNumber.u.LowPart,
@ -49,14 +49,14 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
pDeviceObject,
Buffer,
sectorSize,
&sectorNumber,
&event,
&sectorNumber,
&event,
&IoStatus);
if (Irp == NULL)
{
DPRINT("IoBuildSynchronousFsdRequest failed\n");
return(STATUS_UNSUCCESSFUL);;
return(STATUS_UNSUCCESSFUL);
}
DPRINT ("Calling IO Driver... with irp %x\n", Irp);
@ -73,7 +73,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
if (!NT_SUCCESS (Status))
{
DPRINT ("IO failed!!! VFATREadSectors : Error code: %x\n", Status);
DPRINT ("IO failed!!! VfatReadSectors : Error code: %x\n", Status);
DPRINT ("(pDeviceObject %x, DiskSector %x, Buffer %x, offset 0x%x%x)\n",
pDeviceObject, DiskSector, Buffer, sectorNumber.u.HighPart,
sectorNumber.u.LowPart);
@ -86,7 +86,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
NTSTATUS
VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
IN ULONG DiskSector,
IN ULONG SectorCount,
IN ULONG SectorCount,
IN PUCHAR Buffer)
{
LARGE_INTEGER sectorNumber;
@ -96,7 +96,7 @@ VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
NTSTATUS Status;
ULONG sectorSize;
DPRINT ("VFATWriteSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
DPRINT ("VfatWriteSectors(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
pDeviceObject, DiskSector, Buffer);
sectorNumber.u.LowPart = DiskSector << 9;
@ -134,7 +134,7 @@ VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
if (!NT_SUCCESS (Status))
{
DPRINT1 ("IO failed!!! VFATWriteSectors : Error code: %x\n", Status);
DPRINT1 ("IO failed!!! VfatWriteSectors : Error code: %x\n", Status);
return (Status);
}

View file

@ -1,4 +1,4 @@
/* $Id: iface.c,v 1.49 2001/01/16 09:55:02 dwelch Exp $
/* $Id: iface.c,v 1.50 2001/03/06 08:19:58 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
@ -27,7 +27,6 @@
/* INCLUDES *****************************************************************/
#include <ddk/ntddk.h>
#include <wchar.h>
#define NDEBUG
#include <debug.h>
@ -40,185 +39,249 @@ static PDRIVER_OBJECT VfatDriverObject;
/* FUNCTIONS ****************************************************************/
BOOLEAN
VfatHasFileSystem (PDEVICE_OBJECT DeviceToMount)
static NTSTATUS
VfatHasFileSystem(PDEVICE_OBJECT DeviceToMount,
PBOOLEAN RecognizedFS)
/*
* FUNCTION: Tests if the device contains a filesystem that can be mounted
* by this fsd
*/
{
BootSector *Boot;
BootSector *Boot;
NTSTATUS Status;
Boot = ExAllocatePool (NonPagedPool, 512);
Boot = ExAllocatePool(NonPagedPool, 512);
/* FIXME: Check status */
VfatReadSectors (DeviceToMount, 0, 1, (UCHAR *) Boot);
Status = VfatReadSectors(DeviceToMount, 0, 1, (UCHAR *) Boot);
if (!NT_SUCCESS(Status))
{
return Status;
}
DPRINT ("Boot->SysType %.5s\n", Boot->SysType);
if (strncmp (Boot->SysType, "FAT12", 5) == 0 ||
strncmp (Boot->SysType, "FAT16", 5) == 0 ||
strncmp (((struct _BootSector32 *) (Boot))->SysType, "FAT32", 5) == 0)
{
ExFreePool (Boot);
return (TRUE);
}
ExFreePool (Boot);
return (FALSE);
DPRINT("Boot->SysType %.5s\n", Boot->SysType);
if (strncmp(Boot->SysType, "FAT12", 5) == 0 ||
strncmp(Boot->SysType, "FAT16", 5) == 0 ||
strncmp(((struct _BootSector32 *) (Boot))->SysType, "FAT32", 5) == 0)
{
*RecognizedFS = TRUE;
}
else
{
*RecognizedFS = FALSE;
}
ExFreePool(Boot);
return STATUS_SUCCESS;
}
NTSTATUS
static NTSTATUS
VfatMountDevice (PDEVICE_EXTENSION DeviceExt, PDEVICE_OBJECT DeviceToMount)
/*
* FUNCTION: Mounts the device
*/
{
DPRINT ("Mounting VFAT device...");
DPRINT ("DeviceExt %x\n", DeviceExt);
NTSTATUS Status;
DeviceExt->Boot = ExAllocatePool (NonPagedPool, 512);
/* FIXME: Check status */
VfatReadSectors (DeviceToMount, 0, 1, (UCHAR *) DeviceExt->Boot);
DPRINT("Mounting VFAT device...");
DPRINT("DeviceExt %x\n", DeviceExt);
DeviceExt->FATStart = DeviceExt->Boot->ReservedSectors;
DeviceExt->rootDirectorySectors =
(DeviceExt->Boot->RootEntries * 32) / DeviceExt->Boot->BytesPerSector;
DeviceExt->rootStart =
DeviceExt->FATStart +
DeviceExt->Boot->FATCount * DeviceExt->Boot->FATSectors;
DeviceExt->dataStart =
DeviceExt->rootStart + DeviceExt->rootDirectorySectors;
DeviceExt->FATEntriesPerSector = DeviceExt->Boot->BytesPerSector / 32;
DeviceExt->BytesPerCluster = DeviceExt->Boot->SectorsPerCluster *
DeviceExt->Boot->BytesPerSector;
if (DeviceExt->BytesPerCluster >= PAGESIZE &&
(DeviceExt->BytesPerCluster % PAGESIZE) != 0)
{
DbgPrint("Invalid cluster size\n");
KeBugCheck(0);
}
else if (DeviceExt->BytesPerCluster < PAGESIZE &&
(PAGESIZE % DeviceExt->BytesPerCluster) != 0)
{
DbgPrint("Invalid cluster size2\n");
KeBugCheck(0);
}
DeviceExt->Boot = ExAllocatePool(NonPagedPool, 512);
if (strncmp (DeviceExt->Boot->SysType, "FAT12", 5) == 0)
{
DbgPrint("FAT12\n");
DeviceExt->FatType = FAT12;
}
else
if (strncmp
(((struct _BootSector32 *) (DeviceExt->Boot))->SysType, "FAT32",
5) == 0)
{
DbgPrint("FAT32\n");
DeviceExt->FatType = FAT32;
DeviceExt->rootDirectorySectors = DeviceExt->Boot->SectorsPerCluster;
DeviceExt->rootStart =
DeviceExt->FATStart + DeviceExt->Boot->FATCount
* ((struct _BootSector32 *) (DeviceExt->Boot))->FATSectors32;
DeviceExt->dataStart = DeviceExt->rootStart;
}
else
{
DbgPrint("FAT16\n");
DeviceExt->FatType = FAT16;
}
Status = VfatReadSectors(DeviceToMount, 0, 1, (UCHAR *) DeviceExt->Boot);
if (!NT_SUCCESS(Status))
{
return Status;
}
return STATUS_SUCCESS;
DeviceExt->FATStart = DeviceExt->Boot->ReservedSectors;
DeviceExt->rootDirectorySectors =
(DeviceExt->Boot->RootEntries * 32) / DeviceExt->Boot->BytesPerSector;
DeviceExt->rootStart =
DeviceExt->FATStart +
DeviceExt->Boot->FATCount * DeviceExt->Boot->FATSectors;
DeviceExt->dataStart =
DeviceExt->rootStart + DeviceExt->rootDirectorySectors;
DeviceExt->FATEntriesPerSector = DeviceExt->Boot->BytesPerSector / 32;
DeviceExt->BytesPerCluster = DeviceExt->Boot->SectorsPerCluster *
DeviceExt->Boot->BytesPerSector;
if (DeviceExt->BytesPerCluster >= PAGESIZE &&
(DeviceExt->BytesPerCluster % PAGESIZE) != 0)
{
DbgPrint("Invalid cluster size\n");
KeBugCheck(0);
}
else if (DeviceExt->BytesPerCluster < PAGESIZE &&
(PAGESIZE % DeviceExt->BytesPerCluster) != 0)
{
DbgPrint("Invalid cluster size2\n");
KeBugCheck(0);
}
if (strncmp (DeviceExt->Boot->SysType, "FAT12", 5) == 0)
{
DbgPrint("FAT12\n");
DeviceExt->FatType = FAT12;
}
else if (strncmp
(((struct _BootSector32 *) (DeviceExt->Boot))->SysType, "FAT32",
5) == 0)
{
DbgPrint("FAT32\n");
DeviceExt->FatType = FAT32;
DeviceExt->rootDirectorySectors = DeviceExt->Boot->SectorsPerCluster;
DeviceExt->rootStart =
DeviceExt->FATStart + DeviceExt->Boot->FATCount
* ((struct _BootSector32 *) (DeviceExt->Boot))->FATSectors32;
DeviceExt->dataStart = DeviceExt->rootStart;
}
else
{
DbgPrint("FAT16\n");
DeviceExt->FatType = FAT16;
}
return STATUS_SUCCESS;
}
NTSTATUS
static NTSTATUS
VfatMount (PDEVICE_OBJECT DeviceToMount)
/*
* FUNCTION: Mount the filesystem
*/
{
PDEVICE_OBJECT DeviceObject;
PDEVICE_EXTENSION DeviceExt;
NTSTATUS Status;
PDEVICE_OBJECT DeviceObject;
PDEVICE_EXTENSION DeviceExt;
BOOLEAN RecognizedFS;
NTSTATUS Status;
IoCreateDevice (VfatDriverObject,
sizeof (DEVICE_EXTENSION),
NULL, FILE_DEVICE_FILE_SYSTEM, 0, FALSE, &DeviceObject);
DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO;
DeviceExt = (PVOID) DeviceObject->DeviceExtension;
// use same vpb as device disk
DeviceObject->Vpb = DeviceToMount->Vpb;
VfatMountDevice (DeviceExt, DeviceToMount);
DeviceObject->Vpb->Flags |= VPB_MOUNTED;
DeviceExt->StorageDevice = IoAttachDeviceToDeviceStack (DeviceObject,
Status = VfatHasFileSystem (DeviceToMount, &RecognizedFS);
if (!NT_SUCCESS(Status))
{
return Status;
}
if (RecognizedFS == FALSE)
{
DPRINT("VFAT: Unrecognized Volume\n");
return STATUS_UNRECOGNIZED_VOLUME;
}
DPRINT("VFAT: Recognized volume\n");
Status = IoCreateDevice(VfatDriverObject,
sizeof (DEVICE_EXTENSION),
NULL,
FILE_DEVICE_FILE_SYSTEM,
0,
FALSE,
&DeviceObject);
if (!NT_SUCCESS(Status))
{
return Status;
}
DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO;
DeviceExt = (PVOID) DeviceObject->DeviceExtension;
/* use same vpb as device disk */
DeviceObject->Vpb = DeviceToMount->Vpb;
Status = VfatMountDevice (DeviceExt, DeviceToMount);
if (!NT_SUCCESS(Status))
{
/* FIXME: delete device object */
return Status;
}
DeviceObject->Vpb->Flags |= VPB_MOUNTED;
DeviceExt->StorageDevice = IoAttachDeviceToDeviceStack(DeviceObject,
DeviceToMount);
DeviceExt->StreamStorageDevice =
IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
Status = CcInitializeFileCache(DeviceExt->StreamStorageDevice,
&DeviceExt->StorageBcb,
PAGESIZE);
if (DeviceExt->FatType == FAT12)
{
DeviceExt->Fat12StorageDevice =
IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
Status = CcInitializeFileCache(DeviceExt->Fat12StorageDevice,
&DeviceExt->Fat12StorageBcb,
PAGESIZE * 3);
}
ExInitializeResourceLite (&DeviceExt->DirResource);
ExInitializeResourceLite (&DeviceExt->FatResource);
DeviceExt->StreamStorageDevice = IoCreateStreamFileObject(NULL,
DeviceExt->StorageDevice);
Status = CcInitializeFileCache(DeviceExt->StreamStorageDevice,
&DeviceExt->StorageBcb,
PAGESIZE);
if (!NT_SUCCESS(Status))
{
/* FIXME: delete device object */
return Status;
}
KeInitializeSpinLock (&DeviceExt->FcbListLock);
InitializeListHead (&DeviceExt->FcbListHead);
if (DeviceExt->FatType == FAT12)
{
DeviceExt->Fat12StorageDevice =
IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice);
Status = CcInitializeFileCache(DeviceExt->Fat12StorageDevice,
&DeviceExt->Fat12StorageBcb,
PAGESIZE * 3);
if (!NT_SUCCESS(Status))
{
/* FIXME: delete device object */
return Status;
}
}
ExInitializeResourceLite (&DeviceExt->DirResource);
ExInitializeResourceLite (&DeviceExt->FatResource);
/* read serial number */
if (DeviceExt->FatType == FAT12 || DeviceExt->FatType == FAT16)
DeviceObject->Vpb->SerialNumber =
((struct _BootSector *) (DeviceExt->Boot))->VolumeID;
else if (DeviceExt->FatType == FAT32)
DeviceObject->Vpb->SerialNumber =
((struct _BootSector32 *) (DeviceExt->Boot))->VolumeID;
KeInitializeSpinLock (&DeviceExt->FcbListLock);
InitializeListHead (&DeviceExt->FcbListHead);
/* read volume label */
ReadVolumeLabel (DeviceExt, DeviceObject->Vpb);
/* read serial number */
if (DeviceExt->FatType == FAT12 || DeviceExt->FatType == FAT16)
DeviceObject->Vpb->SerialNumber =
((struct _BootSector *) (DeviceExt->Boot))->VolumeID;
else if (DeviceExt->FatType == FAT32)
DeviceObject->Vpb->SerialNumber =
((struct _BootSector32 *) (DeviceExt->Boot))->VolumeID;
return (STATUS_SUCCESS);
/* read volume label */
ReadVolumeLabel(DeviceExt, DeviceObject->Vpb);
return STATUS_SUCCESS;
}
NTSTATUS STDCALL
VfatFileSystemControl (PDEVICE_OBJECT DeviceObject, PIRP Irp)
/*
* FUNCTION: File system control
*/
{
PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation (Irp);
// PVPB vpb = Stack->Parameters.Mount.Vpb;
PDEVICE_OBJECT DeviceToMount = Stack->Parameters.Mount.DeviceObject;
NTSTATUS Status;
PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation (Irp);
NTSTATUS Status;
// DPRINT("VFAT FSC\n");
DbgPrint ("VFAT FSC\n");
switch (Stack->MinorFunction)
{
case IRP_MN_USER_FS_REQUEST:
DPRINT1("VFAT FSC: IRP_MN_USER_FS_REQUEST\n");
Status = STATUS_INVALID_DEVICE_REQUEST;
break;
/* FIXME: should make sure that this is actually a mount request! */
case IRP_MN_MOUNT_VOLUME:
Status = VfatMount(Stack->Parameters.Mount.DeviceObject);
break;
if (VfatHasFileSystem (DeviceToMount))
{
DPRINT ("VFAT: Recognized volume\n");
Status = VfatMount (DeviceToMount);
}
else
{
DPRINT ("VFAT: Unrecognized Volume\n");
Status = STATUS_UNRECOGNIZED_VOLUME;
}
case IRP_MN_VERIFY_VOLUME:
DPRINT1("VFAT FSC: IRP_MN_VERIFY_VOLUME\n");
Status = STATUS_INVALID_DEVICE_REQUEST;
break;
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
default:
DPRINT1("VFAT FSC: MinorFunction %d\n", Stack->MinorFunction);
Status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
IoCompleteRequest (Irp, IO_NO_INCREMENT);
return (Status);
Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;
IoCompleteRequest (Irp, IO_NO_INCREMENT);
return (Status);
}
NTSTATUS STDCALL
DriverEntry (PDRIVER_OBJECT _DriverObject, PUNICODE_STRING RegistryPath)
/*
@ -229,42 +292,50 @@ DriverEntry (PDRIVER_OBJECT _DriverObject, PUNICODE_STRING RegistryPath)
* RETURNS: Success or failure
*/
{
PDEVICE_OBJECT DeviceObject;
NTSTATUS ret;
UNICODE_STRING DeviceName;
PDEVICE_OBJECT DeviceObject;
UNICODE_STRING DeviceName;
NTSTATUS Status;
DbgPrint ("VFAT 0.0.6\n");
DbgPrint("VFAT 0.0.6\n");
VfatDriverObject = _DriverObject;
VfatDriverObject = _DriverObject;
RtlInitUnicodeString (&DeviceName, L"\\Device\\Vfat");
ret = IoCreateDevice (VfatDriverObject, 0, &DeviceName,
FILE_DEVICE_FILE_SYSTEM, 0, FALSE, &DeviceObject);
if (ret != STATUS_SUCCESS)
{
return (ret);
}
RtlInitUnicodeString(&DeviceName,
L"\\Device\\Vfat");
Status = IoCreateDevice(VfatDriverObject,
0,
&DeviceName,
FILE_DEVICE_FILE_SYSTEM,
0,
FALSE,
&DeviceObject);
if (!NT_SUCCESS(Status))
{
return (Status);
}
DeviceObject->Flags = DO_DIRECT_IO;
VfatDriverObject->MajorFunction[IRP_MJ_CLOSE] = VfatClose;
VfatDriverObject->MajorFunction[IRP_MJ_CREATE] = VfatCreate;
VfatDriverObject->MajorFunction[IRP_MJ_READ] = VfatRead;
VfatDriverObject->MajorFunction[IRP_MJ_WRITE] = VfatWrite;
VfatDriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
VfatFileSystemControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
VfatQueryInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
VfatSetInformation;
VfatDriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =
VfatDirectoryControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =
VfatQueryVolumeInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = VfatShutdown;
DeviceObject->Flags = DO_DIRECT_IO;
VfatDriverObject->MajorFunction[IRP_MJ_CLOSE] = VfatClose;
VfatDriverObject->MajorFunction[IRP_MJ_CREATE] = VfatCreate;
VfatDriverObject->MajorFunction[IRP_MJ_READ] = VfatRead;
VfatDriverObject->MajorFunction[IRP_MJ_WRITE] = VfatWrite;
VfatDriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
VfatFileSystemControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
VfatQueryInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
VfatSetInformation;
VfatDriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =
VfatDirectoryControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =
VfatQueryVolumeInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = VfatShutdown;
VfatDriverObject->DriverUnload = NULL;
VfatDriverObject->DriverUnload = NULL;
IoRegisterFileSystem (DeviceObject);
IoRegisterFileSystem(DeviceObject);
return (STATUS_SUCCESS);
return STATUS_SUCCESS;
}
/* EOF */