diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2015-05-06 22:22:41 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2015-05-06 22:22:41 +0300 |
commit | 116d1ede0cc0d5d57f593264251668531252c74b (patch) | |
tree | ab4618829e4ab95e231e059133b8b7d1533ab6c2 /src/test | |
download | windows7-drivers-116d1ede0cc0d5d57f593264251668531252c74b.tar.gz windows7-drivers-116d1ede0cc0d5d57f593264251668531252c74b.zip |
initial commit
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/device.c | 200 | ||||
-rw-r--r-- | src/test/device.h | 13 | ||||
-rw-r--r-- | src/test/main.c | 24 | ||||
-rw-r--r-- | src/test/makefile | 1 | ||||
-rw-r--r-- | src/test/sources | 3 |
5 files changed, 241 insertions, 0 deletions
diff --git a/src/test/device.c b/src/test/device.c new file mode 100644 index 0000000..43ac3bc --- /dev/null +++ b/src/test/device.c @@ -0,0 +1,200 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "device.h" + +#include <ntddk.h> + +static NTSTATUS device_open(DEVICE_OBJECT *device_object, IRP *irp) +{ + NTSTATUS status = STATUS_SUCCESS; + + irp->IoStatus.Status = status; + irp->IoStatus.Information = 0; + IoCompleteRequest(irp, IO_NO_INCREMENT); + return status; +} + +typedef NTSTATUS (*ioctl_handler)(void *, unsigned long, + void *, unsigned long, + ULONG_PTR *); + +static NTSTATUS handle_say_hello(void *in_buf, + unsigned long in_buf_size, + void *out_buf, + unsigned long out_buf_size, + ULONG_PTR *nbwritten) +{ + DbgPrint("Hello, world!\n"); + return STATUS_SUCCESS; +} + +static NTSTATUS handle_exchange_ints(void *in_buf, + unsigned long in_buf_size, + void *out_buf, + unsigned long out_buf_size, + ULONG_PTR *nbwritten) +{ + unsigned int read; + unsigned int written = 0xdeadbeef; + + if (in_buf_size != sizeof(read)) + return STATUS_INVALID_BUFFER_SIZE; + + RtlCopyMemory(&read, in_buf, in_buf_size); + DbgPrint("%08x\n", read); + + if (out_buf_size < sizeof(written)) + return STATUS_BUFFER_TOO_SMALL; + + RtlCopyMemory(out_buf, &written, sizeof(written)); + *nbwritten += sizeof(written); + + return STATUS_SUCCESS; +} + +#define SAY_HELLO CTL_CODE(0x8000, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) +#define EXCHANGE_INTS CTL_CODE(0x8001, 0x800, METHOD_BUFFERED, FILE_ANY_ACCESS) + +static NTSTATUS device_ioctl(DEVICE_OBJECT *device_object, IRP *irp) +{ + IO_STACK_LOCATION *io_stack_loc; + void* in_buf, *out_buf; + unsigned long in_buf_size, out_buf_size; + ioctl_handler handler; + NTSTATUS status = STATUS_UNSUCCESSFUL; + + irp->IoStatus.Status = status; + irp->IoStatus.Information = 0; + io_stack_loc = IoGetCurrentIrpStackLocation(irp); + + in_buf = out_buf = irp->AssociatedIrp.SystemBuffer; + in_buf_size = io_stack_loc->Parameters.DeviceIoControl.InputBufferLength; + out_buf_size = io_stack_loc->Parameters.DeviceIoControl.OutputBufferLength; + + switch (io_stack_loc->Parameters.DeviceIoControl.IoControlCode) + { + case SAY_HELLO: + handler = handle_say_hello; + break; + case EXCHANGE_INTS: + handler = handle_exchange_ints; + break; + default: + status = irp->IoStatus.Status = STATUS_NOT_SUPPORTED; + goto complete_request; + } + + status = irp->IoStatus.Status = handler(in_buf, in_buf_size, + out_buf, out_buf_size, + &irp->IoStatus.Information); + +complete_request: + IoCompleteRequest(irp, IO_NO_INCREMENT); + + return status; +} + +typedef struct +{ + const wchar_t *path; + const wchar_t *symlink; +} device_info; + +typedef struct +{ + DEVICE_OBJECT *object; + UNICODE_STRING path; + UNICODE_STRING symlink; +} device; + +#define NUMOF_DEVICES 2 + +static device_info devices_info[NUMOF_DEVICES] = +{ + { + L"\\Device\\test_device1", + L"\\DosDevices\\test_device1", + }, + { + L"\\Device\\test_device2", + L"\\DosDevices\\test_device2", + }, +}; + +static device devices[NUMOF_DEVICES]; + +static void destroy_device(int i) +{ + IoDeleteSymbolicLink(&devices[i].symlink); + IoDeleteDevice(devices[i].object); +} + +void destroy_devices() +{ + int i; + for (i = 0; i < NUMOF_DEVICES; ++i) + destroy_device(i); +} + +static NTSTATUS set_up_device(DRIVER_OBJECT *driver_object, int i) +{ + NTSTATUS status = STATUS_SUCCESS; + + DbgPrint("Setting up device...\n"); + DbgPrint("\tPath: %ws\n", devices_info[i].path); + DbgPrint("\tSymlink: %ws\n", devices_info[i].symlink); + + RtlInitUnicodeString(&devices[i].path, devices_info[i].path); + RtlInitUnicodeString(&devices[i].symlink, devices_info[i].symlink); + + status = IoCreateDevice(driver_object, + 0, + &devices[i].path, + FILE_DEVICE_UNKNOWN, + FILE_DEVICE_SECURE_OPEN, + FALSE, + &devices[i].object); + + if (!NT_SUCCESS(status)) + return status; + + devices[i].object->Flags |= DO_BUFFERED_IO; + devices[i].object->Flags &= ~DO_DEVICE_INITIALIZING; + + if (!NT_SUCCESS(status = IoCreateSymbolicLink(&devices[i].symlink, + &devices[i].path))) + goto delete_device; + + return status; + +delete_device: + IoDeleteDevice(devices[i].object); + + return status; +} + +NTSTATUS set_up_devices(DRIVER_OBJECT *driver_object) +{ + int i, j; + NTSTATUS status = STATUS_SUCCESS; + + for (i = 0; i < NUMOF_DEVICES; ++i) + if (!NT_SUCCESS(status = set_up_device(driver_object, i))) + goto destroy_devices; + + driver_object->MajorFunction[IRP_MJ_CREATE] = device_open; + driver_object->MajorFunction[IRP_MJ_DEVICE_CONTROL] = device_ioctl; + + return status; + +destroy_devices: + for (j = 0; j < i; ++j) + destroy_device(j); + + return status; +} diff --git a/src/test/device.h b/src/test/device.h new file mode 100644 index 0000000..a5ecc31 --- /dev/null +++ b/src/test/device.h @@ -0,0 +1,13 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#pragma once + +#include <ntddk.h> + +NTSTATUS set_up_devices(DRIVER_OBJECT *); +void destroy_devices(); diff --git a/src/test/main.c b/src/test/main.c new file mode 100644 index 0000000..f6b4fbf --- /dev/null +++ b/src/test/main.c @@ -0,0 +1,24 @@ +/** + * \file + * \author Egor Tensin <Egor.Tensin@gmail.com> + * \copyright This file is licensed under the terms of the MIT License. + * See LICENSE.txt for details. + */ + +#include "device.h" + +#include <ntddk.h> + +VOID on_driver_unload(DRIVER_OBJECT *driver_object) +{ + DbgPrint("Unloading test driver...\n"); + destroy_devices(); +} + +NTSTATUS DriverEntry(DRIVER_OBJECT *driver_object, + UNICODE_STRING *registry_path) +{ + DbgPrint("Loading test driver...\n"); + driver_object->DriverUnload = on_driver_unload; + return set_up_devices(driver_object); +} diff --git a/src/test/makefile b/src/test/makefile new file mode 100644 index 0000000..5acbbd2 --- /dev/null +++ b/src/test/makefile @@ -0,0 +1 @@ +!INCLUDE $(NTMAKEENV)\makefile.def diff --git a/src/test/sources b/src/test/sources new file mode 100644 index 0000000..245a1a7 --- /dev/null +++ b/src/test/sources @@ -0,0 +1,3 @@ +TARGETTYPE = DRIVER +TARGETNAME = test +SOURCES = device.c main.c |