📄 netdisk.c
字号:
#include <ntddk.h>
#include <ntdddisk.h>
#include <ntddcdrm.h>
#include <ntverp.h>
#include "ioctl.h"
#include <stdio.h>
#include <KWin32.h>
#include "server/nfs.h"
typedef struct
{
HANDLE hFile;
LARGE_INTEGER FileSize;
}St_VDiskExtension, * P_VDiskExtension;
NTSTATUS
VDisk_Open(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp );
NTSTATUS
VDisk_Close(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp );
NTSTATUS
VDisk_Read(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp );
NTSTATUS
VDisk_Write(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp );
VOID
VDisk_Unload( IN PDRIVER_OBJECT DriverObject );
NTSTATUS
VDisk_DeviceControl(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp );
PDEVICE_OBJECT g_pControlDevice;
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath)
{
NTSTATUS Status = STATUS_SUCCESS;
PDEVICE_OBJECT pDevObj = NULL;
UNICODE_STRING uniNtNameString;
UNICODE_STRING uniWin32NameString;
if( !NFS_Initialize() )
{
return STATUS_INSUFFICIENT_RESOURCES;
}
// Initialize the Dispatch table
DriverObject->MajorFunction[IRP_MJ_CREATE] = VDisk_Open;
DriverObject->MajorFunction[IRP_MJ_CLOSE] = VDisk_Close;
DriverObject->MajorFunction[IRP_MJ_READ] = VDisk_Read;
DriverObject->MajorFunction[IRP_MJ_WRITE] = VDisk_Write;
DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VDisk_DeviceControl;
// Specify the Unload routine
DriverObject->DriverUnload = VDisk_Unload;
// Create a control device for handling IO Control requests
RtlInitUnicodeString( &uniNtNameString, L"\\Device\\NetDisk" );
Status = IoCreateDevice(
DriverObject,
0, // We don't use a device extension
&uniNtNameString, // The Device Name, specified with a unicode string
FILE_DEVICE_UNKNOWN, // Our control device is a use-defined device
0, // No standard device characteristics
FALSE, // This isn't an exclusive device
&pDevObj
);
if ( NT_SUCCESS(Status) )
{
RtlInitUnicodeString( &uniWin32NameString, L"\\DosDevices\\NetDisk" );
Status = IoCreateSymbolicLink( &uniWin32NameString, &uniNtNameString );
if (!NT_SUCCESS(Status))
{
IoDeleteDevice( pDevObj );
}
g_pControlDevice = pDevObj;
}
return Status;
}
VOID
VDisk_Unload(
IN PDRIVER_OBJECT DriverObject
)
{
UNICODE_STRING uniWin32NameString;
RtlInitUnicodeString( &uniWin32NameString, L"\\DosDevices\\vdiskctl" );
IoDeleteSymbolicLink( &uniWin32NameString );
IoDeleteDevice( g_pControlDevice );
}
NTSTATUS
VDisk_Open(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp
)
{
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
NTSTATUS
VDisk_Close(
IN PDEVICE_OBJECT DeviceObject,
IN PIRP pIrp
)
{
pIrp->IoStatus.Status = STATUS_SUCCESS;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return STATUS_SUCCESS;
}
NTSTATUS
VDisk_Read(
IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp )
{
NTSTATUS Status;
PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
P_VDiskExtension pDeviceExt = (P_VDiskExtension)pDeviceObject->DeviceExtension;
PVOID pSystemBuffer;
DbgPrint( "VDisk_Read - %#x, %#x\n",
(ULONG)pIrpSp->Parameters.Read.ByteOffset.QuadPart,
pIrpSp->Parameters.Read.Length );
if( pIrpSp->Parameters.Read.ByteOffset.QuadPart == 0 )
{
DbgPrint( "\n" );
}
if( pDeviceExt->hFile == NULL )
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
if ((pIrpSp->Parameters.Read.ByteOffset.QuadPart +
pIrpSp->Parameters.Read.Length) >
pDeviceExt->FileSize.QuadPart )
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
pSystemBuffer = MmGetSystemAddressForMdlSafe(
pIrp->MdlAddress, NormalPagePriority);
if (pSystemBuffer == NULL)
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
/*Status = ZwReadFile(
pDeviceExt->hFile,
NULL, // Event
NULL, // ApcRoutine
NULL, // ApcContext
&pIrp->IoStatus,
pSystemBuffer,
pIrpSp->Parameters.Read.Length,
&pIrpSp->Parameters.Read.ByteOffset, // ByteOffset
0 ); // Key*/
if( NFS_SeekFile( pDeviceExt->hFile, pIrpSp->Parameters.Read.ByteOffset.QuadPart ) )
{
if( NFS_ReadFile( pDeviceExt->hFile, pSystemBuffer,
pIrpSp->Parameters.Read.Length ) == pIrpSp->Parameters.Read.Length )
{
Status = STATUS_SUCCESS;
}
else
{
Status = STATUS_IO_DEVICE_ERROR;
}
}
else
{
Status = STATUS_IO_DEVICE_ERROR;
}
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return Status;
}
NTSTATUS
VDisk_Write(
IN PDEVICE_OBJECT pDeviceObject,
IN PIRP pIrp )
{
NTSTATUS Status;
PIO_STACK_LOCATION pIrpSp = IoGetCurrentIrpStackLocation (pIrp);
P_VDiskExtension pDeviceExt = (P_VDiskExtension)pDeviceObject->DeviceExtension;
PVOID pSystemBuffer;
DbgPrint( "VDisk_Write - %#x, %#x\n",
(ULONG)pIrpSp->Parameters.Write.ByteOffset.QuadPart,
pIrpSp->Parameters.Write.Length );
if( pDeviceExt->hFile == NULL )
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
if ((pIrpSp->Parameters.Write.ByteOffset.QuadPart +
pIrpSp->Parameters.Write.Length) >
pDeviceExt->FileSize.QuadPart)
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
pSystemBuffer = MmGetSystemAddressForMdlSafe(
pIrp->MdlAddress, NormalPagePriority);
if (pSystemBuffer == NULL)
{
pIrp->IoStatus.Status = STATUS_INVALID_PARAMETER;
pIrp->IoStatus.Information = 0;
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return pIrp->IoStatus.Status;
}
/*Status = ZwWriteFile(
pDeviceExt->hFile,
NULL, // Event
NULL, // ApcRoutine
NULL, // ApcContext
&pIrp->IoStatus,
pSystemBuffer,
pIrpSp->Parameters.Write.Length,
&pIrpSp->Parameters.Write.ByteOffset, // ByteOffset
0 ); // Key*/
if( NFS_SeekFile( pDeviceExt->hFile, pIrpSp->Parameters.Write.ByteOffset.QuadPart ) )
{
if( NFS_WriteFile( pDeviceExt->hFile, pSystemBuffer,
pIrpSp->Parameters.Write.Length ) == pIrpSp->Parameters.Write.Length )
{
Status = STATUS_SUCCESS;
}
else
{
Status = STATUS_IO_DEVICE_ERROR;
}
}
else
{
Status = STATUS_IO_DEVICE_ERROR;
}
IoCompleteRequest( pIrp, IO_NO_INCREMENT );
return Status;
}
#define IOCTL_DISK_GET_PARTITION_INFO_EX CTL_CODE(IOCTL_DISK_BASE, 0x0012, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_DISK_GET_LENGTH_INFO CTL_CODE(IOCTL_DISK_BASE, 0x0017, METHOD_BUFFERED, FILE_READ_ACCESS)
PCSTR GetIoCtlName( ULONG ulIoControlCode )
{
static char s_aryBuffer[100];
switch( ulIoControlCode )
{
case IOCTL_DISK_CHECK_VERIFY:
return "IOCTL_DISK_CHECK_VERIFY";
case IOCTL_CDROM_CHECK_VERIFY:
return "IOCTL_CDROM_CHECK_VERIFY";
case IOCTL_STORAGE_CHECK_VERIFY:
return "IOCTL_STORAGE_CHECK_VERIFY";
case IOCTL_STORAGE_CHECK_VERIFY2:
return "IOCTL_STORAGE_CHECK_VERIFY2";
case IOCTL_DISK_GET_DRIVE_GEOMETRY:
return "IOCTL_DISK_GET_DRIVE_GEOMETRY";
case IOCTL_CDROM_GET_DRIVE_GEOMETRY:
return "IOCTL_CDROM_GET_DRIVE_GEOMETRY";
case IOCTL_DISK_GET_LENGTH_INFO:
return "IOCTL_DISK_GET_LENGTH_INFO";
case IOCTL_DISK_GET_PARTITION_INFO:
return "IOCTL_DISK_GET_PARTITION_INFO";
case IOCTL_DISK_GET_PARTITION_INFO_EX:
return "IOCTL_DISK_GET_PARTITION_INFO_EX";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -