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 NTSTATUS
VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject, VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
IN ULONG DiskSector, IN ULONG DiskSector,
IN ULONG SectorCount, IN ULONG SectorCount,
IN OUT PUCHAR Buffer) IN OUT PUCHAR Buffer)
{ {
@ -37,7 +37,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
KeInitializeEvent (&event, NotificationEvent, FALSE); KeInitializeEvent (&event, NotificationEvent, FALSE);
sectorSize = BLOCKSIZE * SectorCount; sectorSize = BLOCKSIZE * SectorCount;
DPRINT ("VFATReadSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n", DPRINT ("VfatReadSectors(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
pDeviceObject, DiskSector, Buffer); pDeviceObject, DiskSector, Buffer);
DPRINT ("sectorNumber %08lx:%08lx sectorSize %ld\n", DPRINT ("sectorNumber %08lx:%08lx sectorSize %ld\n",
(unsigned long int) sectorNumber.u.LowPart, (unsigned long int) sectorNumber.u.LowPart,
@ -49,14 +49,14 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
pDeviceObject, pDeviceObject,
Buffer, Buffer,
sectorSize, sectorSize,
&sectorNumber, &sectorNumber,
&event, &event,
&IoStatus); &IoStatus);
if (Irp == NULL) if (Irp == NULL)
{ {
DPRINT("IoBuildSynchronousFsdRequest failed\n"); DPRINT("IoBuildSynchronousFsdRequest failed\n");
return(STATUS_UNSUCCESSFUL);; return(STATUS_UNSUCCESSFUL);
} }
DPRINT ("Calling IO Driver... with irp %x\n", Irp); DPRINT ("Calling IO Driver... with irp %x\n", Irp);
@ -73,7 +73,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
if (!NT_SUCCESS (Status)) 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", DPRINT ("(pDeviceObject %x, DiskSector %x, Buffer %x, offset 0x%x%x)\n",
pDeviceObject, DiskSector, Buffer, sectorNumber.u.HighPart, pDeviceObject, DiskSector, Buffer, sectorNumber.u.HighPart,
sectorNumber.u.LowPart); sectorNumber.u.LowPart);
@ -86,7 +86,7 @@ VfatReadSectors (IN PDEVICE_OBJECT pDeviceObject,
NTSTATUS NTSTATUS
VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject, VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
IN ULONG DiskSector, IN ULONG DiskSector,
IN ULONG SectorCount, IN ULONG SectorCount,
IN PUCHAR Buffer) IN PUCHAR Buffer)
{ {
LARGE_INTEGER sectorNumber; LARGE_INTEGER sectorNumber;
@ -96,7 +96,7 @@ VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
NTSTATUS Status; NTSTATUS Status;
ULONG sectorSize; ULONG sectorSize;
DPRINT ("VFATWriteSector(pDeviceObject %x, DiskSector %d, Buffer %x)\n", DPRINT ("VfatWriteSectors(pDeviceObject %x, DiskSector %d, Buffer %x)\n",
pDeviceObject, DiskSector, Buffer); pDeviceObject, DiskSector, Buffer);
sectorNumber.u.LowPart = DiskSector << 9; sectorNumber.u.LowPart = DiskSector << 9;
@ -134,7 +134,7 @@ VfatWriteSectors (IN PDEVICE_OBJECT pDeviceObject,
if (!NT_SUCCESS (Status)) if (!NT_SUCCESS (Status))
{ {
DPRINT1 ("IO failed!!! VFATWriteSectors : Error code: %x\n", Status); DPRINT1 ("IO failed!!! VfatWriteSectors : Error code: %x\n", Status);
return (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 * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
@ -27,7 +27,6 @@
/* INCLUDES *****************************************************************/ /* INCLUDES *****************************************************************/
#include <ddk/ntddk.h> #include <ddk/ntddk.h>
#include <wchar.h>
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
@ -40,185 +39,249 @@ static PDRIVER_OBJECT VfatDriverObject;
/* FUNCTIONS ****************************************************************/ /* FUNCTIONS ****************************************************************/
BOOLEAN static NTSTATUS
VfatHasFileSystem (PDEVICE_OBJECT DeviceToMount) VfatHasFileSystem(PDEVICE_OBJECT DeviceToMount,
PBOOLEAN RecognizedFS)
/* /*
* FUNCTION: Tests if the device contains a filesystem that can be mounted * FUNCTION: Tests if the device contains a filesystem that can be mounted
* by this fsd * by this fsd
*/ */
{ {
BootSector *Boot; BootSector *Boot;
NTSTATUS Status;
Boot = ExAllocatePool (NonPagedPool, 512); Boot = ExAllocatePool(NonPagedPool, 512);
/* FIXME: Check status */ Status = VfatReadSectors(DeviceToMount, 0, 1, (UCHAR *) Boot);
VfatReadSectors (DeviceToMount, 0, 1, (UCHAR *) Boot); if (!NT_SUCCESS(Status))
{
return Status;
}
DPRINT ("Boot->SysType %.5s\n", Boot->SysType); DPRINT("Boot->SysType %.5s\n", Boot->SysType);
if (strncmp (Boot->SysType, "FAT12", 5) == 0 || if (strncmp(Boot->SysType, "FAT12", 5) == 0 ||
strncmp (Boot->SysType, "FAT16", 5) == 0 || strncmp(Boot->SysType, "FAT16", 5) == 0 ||
strncmp (((struct _BootSector32 *) (Boot))->SysType, "FAT32", 5) == 0) strncmp(((struct _BootSector32 *) (Boot))->SysType, "FAT32", 5) == 0)
{ {
ExFreePool (Boot); *RecognizedFS = TRUE;
return (TRUE); }
} else
ExFreePool (Boot); {
return (FALSE); *RecognizedFS = FALSE;
}
ExFreePool(Boot);
return STATUS_SUCCESS;
} }
NTSTATUS
static NTSTATUS
VfatMountDevice (PDEVICE_EXTENSION DeviceExt, PDEVICE_OBJECT DeviceToMount) VfatMountDevice (PDEVICE_EXTENSION DeviceExt, PDEVICE_OBJECT DeviceToMount)
/* /*
* FUNCTION: Mounts the device * FUNCTION: Mounts the device
*/ */
{ {
DPRINT ("Mounting VFAT device..."); NTSTATUS Status;
DPRINT ("DeviceExt %x\n", DeviceExt);
DeviceExt->Boot = ExAllocatePool (NonPagedPool, 512); DPRINT("Mounting VFAT device...");
/* FIXME: Check status */ DPRINT("DeviceExt %x\n", DeviceExt);
VfatReadSectors (DeviceToMount, 0, 1, (UCHAR *) DeviceExt->Boot);
DeviceExt->FATStart = DeviceExt->Boot->ReservedSectors; DeviceExt->Boot = ExAllocatePool(NonPagedPool, 512);
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) Status = VfatReadSectors(DeviceToMount, 0, 1, (UCHAR *) DeviceExt->Boot);
{ if (!NT_SUCCESS(Status))
DbgPrint("FAT12\n"); {
DeviceExt->FatType = FAT12; return Status;
} }
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; 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) VfatMount (PDEVICE_OBJECT DeviceToMount)
/* /*
* FUNCTION: Mount the filesystem * FUNCTION: Mount the filesystem
*/ */
{ {
PDEVICE_OBJECT DeviceObject; PDEVICE_OBJECT DeviceObject;
PDEVICE_EXTENSION DeviceExt; PDEVICE_EXTENSION DeviceExt;
NTSTATUS Status; BOOLEAN RecognizedFS;
NTSTATUS Status;
IoCreateDevice (VfatDriverObject, Status = VfatHasFileSystem (DeviceToMount, &RecognizedFS);
sizeof (DEVICE_EXTENSION), if (!NT_SUCCESS(Status))
NULL, FILE_DEVICE_FILE_SYSTEM, 0, FALSE, &DeviceObject); {
DeviceObject->Flags = DeviceObject->Flags | DO_DIRECT_IO; return Status;
DeviceExt = (PVOID) DeviceObject->DeviceExtension; }
// use same vpb as device disk
DeviceObject->Vpb = DeviceToMount->Vpb; if (RecognizedFS == FALSE)
VfatMountDevice (DeviceExt, DeviceToMount); {
DeviceObject->Vpb->Flags |= VPB_MOUNTED; DPRINT("VFAT: Unrecognized Volume\n");
DeviceExt->StorageDevice = IoAttachDeviceToDeviceStack (DeviceObject, 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); DeviceToMount);
DeviceExt->StreamStorageDevice = DeviceExt->StreamStorageDevice = IoCreateStreamFileObject(NULL,
IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice); DeviceExt->StorageDevice);
Status = CcInitializeFileCache(DeviceExt->StreamStorageDevice, Status = CcInitializeFileCache(DeviceExt->StreamStorageDevice,
&DeviceExt->StorageBcb, &DeviceExt->StorageBcb,
PAGESIZE); PAGESIZE);
if (DeviceExt->FatType == FAT12) if (!NT_SUCCESS(Status))
{ {
DeviceExt->Fat12StorageDevice = /* FIXME: delete device object */
IoCreateStreamFileObject(NULL, DeviceExt->StorageDevice); return Status;
Status = CcInitializeFileCache(DeviceExt->Fat12StorageDevice, }
&DeviceExt->Fat12StorageBcb,
PAGESIZE * 3);
}
ExInitializeResourceLite (&DeviceExt->DirResource);
ExInitializeResourceLite (&DeviceExt->FatResource);
KeInitializeSpinLock (&DeviceExt->FcbListLock); if (DeviceExt->FatType == FAT12)
InitializeListHead (&DeviceExt->FcbListHead); {
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 */ KeInitializeSpinLock (&DeviceExt->FcbListLock);
if (DeviceExt->FatType == FAT12 || DeviceExt->FatType == FAT16) InitializeListHead (&DeviceExt->FcbListHead);
DeviceObject->Vpb->SerialNumber =
((struct _BootSector *) (DeviceExt->Boot))->VolumeID;
else if (DeviceExt->FatType == FAT32)
DeviceObject->Vpb->SerialNumber =
((struct _BootSector32 *) (DeviceExt->Boot))->VolumeID;
/* read volume label */ /* read serial number */
ReadVolumeLabel (DeviceExt, DeviceObject->Vpb); 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 NTSTATUS STDCALL
VfatFileSystemControl (PDEVICE_OBJECT DeviceObject, PIRP Irp) VfatFileSystemControl (PDEVICE_OBJECT DeviceObject, PIRP Irp)
/* /*
* FUNCTION: File system control * FUNCTION: File system control
*/ */
{ {
PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation (Irp); PIO_STACK_LOCATION Stack = IoGetCurrentIrpStackLocation (Irp);
// PVPB vpb = Stack->Parameters.Mount.Vpb; NTSTATUS Status;
PDEVICE_OBJECT DeviceToMount = Stack->Parameters.Mount.DeviceObject;
NTSTATUS Status;
// DPRINT("VFAT FSC\n"); switch (Stack->MinorFunction)
DbgPrint ("VFAT FSC\n"); {
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)) case IRP_MN_VERIFY_VOLUME:
{ DPRINT1("VFAT FSC: IRP_MN_VERIFY_VOLUME\n");
DPRINT ("VFAT: Recognized volume\n"); Status = STATUS_INVALID_DEVICE_REQUEST;
Status = VfatMount (DeviceToMount); break;
}
else
{
DPRINT ("VFAT: Unrecognized Volume\n");
Status = STATUS_UNRECOGNIZED_VOLUME;
}
Irp->IoStatus.Status = Status; default:
Irp->IoStatus.Information = 0; DPRINT1("VFAT FSC: MinorFunction %d\n", Stack->MinorFunction);
Status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
IoCompleteRequest (Irp, IO_NO_INCREMENT); Irp->IoStatus.Status = Status;
return (Status); Irp->IoStatus.Information = 0;
IoCompleteRequest (Irp, IO_NO_INCREMENT);
return (Status);
} }
NTSTATUS STDCALL NTSTATUS STDCALL
DriverEntry (PDRIVER_OBJECT _DriverObject, PUNICODE_STRING RegistryPath) DriverEntry (PDRIVER_OBJECT _DriverObject, PUNICODE_STRING RegistryPath)
/* /*
@ -229,42 +292,50 @@ DriverEntry (PDRIVER_OBJECT _DriverObject, PUNICODE_STRING RegistryPath)
* RETURNS: Success or failure * RETURNS: Success or failure
*/ */
{ {
PDEVICE_OBJECT DeviceObject; PDEVICE_OBJECT DeviceObject;
NTSTATUS ret; UNICODE_STRING DeviceName;
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"); RtlInitUnicodeString(&DeviceName,
ret = IoCreateDevice (VfatDriverObject, 0, &DeviceName, L"\\Device\\Vfat");
FILE_DEVICE_FILE_SYSTEM, 0, FALSE, &DeviceObject); Status = IoCreateDevice(VfatDriverObject,
if (ret != STATUS_SUCCESS) 0,
{ &DeviceName,
return (ret); FILE_DEVICE_FILE_SYSTEM,
} 0,
FALSE,
&DeviceObject);
if (!NT_SUCCESS(Status))
{
return (Status);
}
DeviceObject->Flags = DO_DIRECT_IO; DeviceObject->Flags = DO_DIRECT_IO;
VfatDriverObject->MajorFunction[IRP_MJ_CLOSE] = VfatClose; VfatDriverObject->MajorFunction[IRP_MJ_CLOSE] = VfatClose;
VfatDriverObject->MajorFunction[IRP_MJ_CREATE] = VfatCreate; VfatDriverObject->MajorFunction[IRP_MJ_CREATE] = VfatCreate;
VfatDriverObject->MajorFunction[IRP_MJ_READ] = VfatRead; VfatDriverObject->MajorFunction[IRP_MJ_READ] = VfatRead;
VfatDriverObject->MajorFunction[IRP_MJ_WRITE] = VfatWrite; VfatDriverObject->MajorFunction[IRP_MJ_WRITE] = VfatWrite;
VfatDriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] = VfatDriverObject->MajorFunction[IRP_MJ_FILE_SYSTEM_CONTROL] =
VfatFileSystemControl; VfatFileSystemControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = VfatDriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] =
VfatQueryInformation; VfatQueryInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = VfatDriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] =
VfatSetInformation; VfatSetInformation;
VfatDriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] = VfatDriverObject->MajorFunction[IRP_MJ_DIRECTORY_CONTROL] =
VfatDirectoryControl; VfatDirectoryControl;
VfatDriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] = VfatDriverObject->MajorFunction[IRP_MJ_QUERY_VOLUME_INFORMATION] =
VfatQueryVolumeInformation; VfatQueryVolumeInformation;
VfatDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = VfatShutdown; VfatDriverObject->MajorFunction[IRP_MJ_SHUTDOWN] = VfatShutdown;
VfatDriverObject->DriverUnload = NULL; VfatDriverObject->DriverUnload = NULL;
IoRegisterFileSystem (DeviceObject); IoRegisterFileSystem(DeviceObject);
return (STATUS_SUCCESS); return STATUS_SUCCESS;
} }
/* EOF */