KERNEL_VERSION_MAJOR: 0 -> 5
[reactos.git] / regtests / kmregtests / driver.c
1 /*
2  * PROJECT:         ReactOS kernel
3  * FILE:            regtests/kmregtests/driver.c
4  * PURPOSE:         Kernel-mode regression testing driver
5  * PROGRAMMER:      Casper S. Hornstrup (chorns@users.sourceforge.net)
6  * UPDATE HISTORY:
7  *      06-07-2003  CSH  Created
8  */
9 #define NTOS_MODE_KERNEL
10 #include <ntos.h>
11 #include "regtests.h"
12 #include "kmregtests.h"
13
14 #define NDEBUG
15 #include <debug.h>
16
17 PVOID
18 AllocateMemory(ULONG Size)
19 {
20   return ExAllocatePool(NonPagedPool, Size);
21 }
22
23
24 VOID
25 FreeMemory(PVOID Base)
26 {
27   ExFreePool(NonPagedPool);
28 }
29
30 VOID
31 ShutdownBochs()
32 {
33   /* Shutdown bochs programmatically */
34   WRITE_PORT_BUFFER_UCHAR((PUCHAR) 0x8900,
35     (PUCHAR) "Shutdown",
36     strlen("Shutdown"));
37 }
38
39 NTSTATUS
40 STDCALL
41 KMRegTestsRun(
42   PIRP Irp,
43   PIO_STACK_LOCATION IrpSp)
44 {
45   InitializeTests();
46   RegisterTests();
47   PerformTests();
48   ShutdownBochs();
49
50   Irp->IoStatus.Status = STATUS_SUCCESS;
51   Irp->IoStatus.Information = 0;
52   return STATUS_SUCCESS;
53 }
54
55 NTSTATUS
56 STDCALL
57 KMRegTestsDispatch(
58   PDEVICE_OBJECT DeviceObject,
59   PIRP Irp)
60 /*
61  * FUNCTION: IOCTL dispatch routine
62  * ARGUMENTS:
63  *     DeviceObject = Pointer to a device object for this driver
64  *     Irp          = Pointer to a I/O request packet
65  * RETURNS:
66  *     Status of the operation
67  */
68 {
69   NTSTATUS Status;
70   PIO_STACK_LOCATION IrpSp;
71
72   IrpSp = IoGetCurrentIrpStackLocation(Irp);
73
74   DPRINT("Called. DeviceObject is at (0x%X), IRP is at (0x%X), IrpSp->FileObject (0x%X).\n",
75       DeviceObject, Irp, IrpSp->FileObject);
76
77   Irp->IoStatus.Information = 0;
78
79   switch (IrpSp->Parameters.DeviceIoControl.IoControlCode) {
80   case IOCTL_KMREGTESTS_RUN:
81       Status = KMRegTestsRun(Irp, IrpSp);
82       break;
83
84   default:
85       DPRINT("Unknown IOCTL (0x%X).\n",
86         IrpSp->Parameters.DeviceIoControl.IoControlCode);
87       Status = STATUS_NOT_IMPLEMENTED;
88       break;
89   }
90
91   if (Status != STATUS_PENDING) {
92       Irp->IoStatus.Status = Status;
93       IoCompleteRequest(Irp, IO_NO_INCREMENT);
94   }
95
96   DPRINT("Leaving. Status (0x%X).\n", Status);
97
98         return Status;
99 }
100
101 NTSTATUS STDCALL
102 KMRegTestsOpenClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
103 {
104   PIO_STACK_LOCATION piosStack = IoGetCurrentIrpStackLocation(Irp);
105   NTSTATUS nErrCode;
106
107   nErrCode = STATUS_SUCCESS;
108
109   switch(piosStack->MajorFunction)
110     {
111       /* Opening and closing handles to the device */
112       case IRP_MJ_CREATE:
113       case IRP_MJ_CLOSE:
114         break;
115
116       /* Write data */
117       case IRP_MJ_WRITE:
118         /* Ignore */
119         Irp->IoStatus.Information = 0;
120         break;
121
122       /* Read data */
123       case IRP_MJ_READ:
124         /* Ignore */
125         Irp->IoStatus.Information = 0;
126         nErrCode = STATUS_END_OF_FILE;
127         break;
128
129       /* Unsupported operations */
130       default:
131         nErrCode = STATUS_NOT_IMPLEMENTED;
132     }
133
134   Irp->IoStatus.Status = nErrCode;
135   IoCompleteRequest(Irp, IO_NO_INCREMENT);
136
137   return nErrCode;
138 }
139
140 NTSTATUS STDCALL
141 KMRegTestsUnload(PDRIVER_OBJECT DriverObject)
142 {
143   return STATUS_SUCCESS;
144 }
145
146 NTSTATUS STDCALL
147 DriverEntry(PDRIVER_OBJECT DriverObject,
148             PUNICODE_STRING RegistryPath)
149 {
150   PDEVICE_OBJECT DeviceObject;
151   UNICODE_STRING DeviceName;
152   UNICODE_STRING DosName;
153   NTSTATUS Status;
154
155   /* Register driver routines */
156   DriverObject->MajorFunction[IRP_MJ_CLOSE] = (PDRIVER_DISPATCH) KMRegTestsOpenClose;
157   DriverObject->MajorFunction[IRP_MJ_CREATE] = (PDRIVER_DISPATCH) KMRegTestsOpenClose;
158   DriverObject->MajorFunction[IRP_MJ_WRITE] = (PDRIVER_DISPATCH) KMRegTestsOpenClose;
159   DriverObject->MajorFunction[IRP_MJ_READ] = (PDRIVER_DISPATCH) KMRegTestsOpenClose;
160   DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = (PDRIVER_DISPATCH) KMRegTestsDispatch;
161   DriverObject->DriverUnload = (PDRIVER_UNLOAD) KMRegTestsUnload;
162
163   /* Create device */
164   RtlInitUnicodeString(&DeviceName,
165     L"\\Device\\KMRegTests");
166
167   Status = IoCreateDevice(DriverObject,
168     0,
169     &DeviceName,
170     FILE_DEVICE_NULL,
171     0,
172     FALSE,
173     &DeviceObject);
174   if (!NT_SUCCESS(Status))
175     {
176       return Status;
177     }
178
179   DeviceObject->Flags |= DO_BUFFERED_IO;
180
181   return Status;
182 }