win-pvdrivers

view xenhide/xenhide.c @ 52:05f63c07888b

xenhide: use sizeof()
author Andy Grover <andy.grover@oracle.com>
date Mon Dec 17 14:01:41 2007 -0800 (2007-12-17)
parents d47d595b0b19
children 17319a4920eb
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenhide.h"
21 #include <stdlib.h>
23 DRIVER_INITIALIZE DriverEntry;
24 static NTSTATUS
25 XenHide_AddDevice(WDFDRIVER Driver, PWDFDEVICE_INIT DeviceInit);
26 static VOID
27 XenHide_IoInternalDeviceControl(WDFQUEUE Queue, WDFREQUEST Request, size_t OutputBufferLength, size_t InputBufferLength, ULONG IoControlCode);
29 #ifdef ALLOC_PRAGMA
30 #pragma alloc_text (INIT, DriverEntry)
31 #pragma alloc_text (PAGE, XenHide_AddDevice)
32 #endif
34 static BOOLEAN AutoEnumerate;
36 static WDFDEVICE Device;
38 NTSTATUS
39 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
40 {
41 WDF_DRIVER_CONFIG config;
42 NTSTATUS status;
43 UNICODE_STRING RegKeyName;
44 UNICODE_STRING RegValueName;
45 HANDLE RegHandle;
46 OBJECT_ATTRIBUTES RegObjectAttributes;
47 char Buf[200];
48 ULONG BufLen = 200;
49 PKEY_VALUE_PARTIAL_INFORMATION KeyPartialValue;
50 int State = 0;
51 int StartPos = 0;
52 WCHAR *SystemStartOptions;
53 size_t SystemStartOptionsLen;
54 size_t i;
56 KdPrint((__DRIVER_NAME " --> DriverEntry\n"));
58 RtlInitUnicodeString(&RegKeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Control");
59 InitializeObjectAttributes(&RegObjectAttributes, &RegKeyName, OBJ_CASE_INSENSITIVE, NULL, NULL);
60 status = ZwOpenKey(&RegHandle, KEY_READ, &RegObjectAttributes);
61 if(!NT_SUCCESS(status))
62 {
63 KdPrint((__DRIVER_NAME " ZwOpenKey returned %08x\n", status));
64 }
66 RtlInitUnicodeString(&RegValueName, L"SystemStartOptions");
67 status = ZwQueryValueKey(RegHandle, &RegValueName, KeyValuePartialInformation, Buf, BufLen, &BufLen);
68 if(!NT_SUCCESS(status))
69 {
70 KdPrint((__DRIVER_NAME " ZwQueryKeyValue returned %08x\n", status));
71 }
72 KdPrint((__DRIVER_NAME " BufLen = %d\n", BufLen));
73 KeyPartialValue = (PKEY_VALUE_PARTIAL_INFORMATION)Buf;
74 KdPrint((__DRIVER_NAME " Buf = %ws\n", KeyPartialValue->Data));
75 SystemStartOptions = (WCHAR *)KeyPartialValue->Data;
77 AutoEnumerate = FALSE;
79 RtlStringCbLengthW(SystemStartOptions, KeyPartialValue->DataLength, &SystemStartOptionsLen);
81 for (i = 0; i <= SystemStartOptionsLen/2; i++)
82 {
83 //KdPrint((__DRIVER_NAME " pos = %d, state = %d, char = '%wc' (%d)\n", i, State, SystemStartOptions[i], SystemStartOptions[i]));
85 switch (State)
86 {
87 case 0:
88 if (SystemStartOptions[i] == L'G')
89 {
90 StartPos = i;
91 State = 2;
92 } else if (SystemStartOptions[i] != L' ')
93 {
94 State = 1;
95 }
96 break;
97 case 1:
98 if (SystemStartOptions[i] == L' ')
99 State = 0;
100 break;
101 case 2:
102 if (SystemStartOptions[i] == L'P')
103 State = 3;
104 else
105 State = 0;
106 break;
107 case 3:
108 if (SystemStartOptions[i] == L'L')
109 State = 4;
110 else
111 State = 0;
112 break;
113 case 4:
114 if (SystemStartOptions[i] == L'P')
115 State = 5;
116 else
117 State = 0;
118 break;
119 case 5:
120 if (SystemStartOptions[i] == L'V')
121 State = 6;
122 else
123 State = 0;
124 break;
125 case 6:
126 if (SystemStartOptions[i] == L' ' || SystemStartOptions[i] == 0)
127 AutoEnumerate = TRUE;
128 State = 0;
129 break;
130 }
131 }
133 KdPrint((__DRIVER_NAME " AutoEnumerate = %d\n", AutoEnumerate));
136 WDF_DRIVER_CONFIG_INIT(&config, XenHide_AddDevice);
137 status = WdfDriverCreate(
138 DriverObject,
139 RegistryPath,
140 WDF_NO_OBJECT_ATTRIBUTES,
141 &config,
142 WDF_NO_HANDLE);
143 if(!NT_SUCCESS(status))
144 {
145 //KdPrint((__DRIVER_NAME " WdfDriverCreate failed with status 0x%08x\n", status));
146 }
148 KdPrint((__DRIVER_NAME " <-- DriverEntry\n"));
150 return status;
151 }
153 static NTSTATUS
154 XenHide_PreprocessWdmIrpPNP(WDFDEVICE Device, PIRP Irp);
156 static NTSTATUS
157 XenHide_AddDevice(
158 IN WDFDRIVER Driver,
159 IN PWDFDEVICE_INIT DeviceInit
160 )
161 {
162 NTSTATUS status;
163 WDF_OBJECT_ATTRIBUTES attributes;
164 UCHAR MinorFunctions[1] = { IRP_MN_QUERY_DEVICE_RELATIONS };
166 UNREFERENCED_PARAMETER(Driver);
168 KdPrint((__DRIVER_NAME " --> DeviceAdd\n"));
170 WdfFdoInitSetFilter(DeviceInit);
172 WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
174 WdfDeviceInitAssignWdmIrpPreprocessCallback(DeviceInit, XenHide_PreprocessWdmIrpPNP, IRP_MJ_PNP, MinorFunctions, 1);
176 status = WdfDeviceCreate(&DeviceInit, &attributes, &Device);
177 if(!NT_SUCCESS(status))
178 {
179 KdPrint((__DRIVER_NAME " WdfDeviceCreate failed with status 0x%08x\n", status));
180 return status;
181 }
183 KdPrint((__DRIVER_NAME " <-- DeviceAdd\n"));
185 return status;
186 }
188 static int
189 XenHide_StringMatches(PWCHAR String1, PWCHAR String2)
190 {
191 for(;*String1 != 0 && *String2 != 0 && *String1 == *String2; String1++, String2++);
192 return ((*String1 == 0 && *String2 == 0) || (*String1 == 0 && *String2 == L'\n') || (*String1 == L'\n' && *String2 == 0));
193 }
195 static NTSTATUS
196 XenHide_IoCompletion(PDEVICE_OBJECT DeviceObject, PIRP Irp, PVOID Context)
197 {
198 ULONG i;
199 PDEVICE_RELATIONS Relations;
200 WCHAR Buffer[1000];
201 PWCHAR Ptr;
202 ULONG Length;
203 size_t StrLen;
204 int Match;
205 int Offset = 0;
207 UNREFERENCED_PARAMETER(DeviceObject);
208 UNREFERENCED_PARAMETER(Context);
210 KdPrint((__DRIVER_NAME " --> IoCompletion\n"));
212 Relations = (PDEVICE_RELATIONS)Irp->IoStatus.Information;
214 for (i = 0; i < Relations->Count; i++)
215 {
216 Length = sizeof(Buffer);
217 IoGetDeviceProperty(Relations->Objects[i], DevicePropertyDeviceDescription, Length, Buffer, &Length);
218 KdPrint((__DRIVER_NAME " %3d - %ws\n", i, Buffer));
219 Length = sizeof(Buffer);
220 IoGetDeviceProperty(Relations->Objects[i], DevicePropertyHardwareID, Length, Buffer, &Length);
221 Match = 0;
222 StrLen = 0;
223 for (Ptr = Buffer; *Ptr != 0; Ptr += StrLen + 1)
224 {
225 KdPrint((__DRIVER_NAME " - %ws\n", Ptr));
226 if (XenHide_StringMatches(Ptr, L"PCI\\VEN_8086&DEV_7010&SUBSYS_00015853")) {
227 Match = 1;
228 break;
229 }
230 RtlStringCchLengthW(Ptr, Length, &StrLen);
231 }
232 if (Match)
233 {
234 KdPrint((__DRIVER_NAME " (Match)\n"));
235 Offset++;
236 }
237 if (Offset != 0)
238 Relations->Objects[i] = Relations->Objects[i + Offset];
239 }
240 Relations->Count -= Offset;
242 KdPrint((__DRIVER_NAME " <-- IoCompletion\n"));
244 return Irp->IoStatus.Status;
245 }
249 static NTSTATUS
250 XenHide_PreprocessWdmIrpPNP(WDFDEVICE Device, PIRP Irp)
251 {
252 NTSTATUS Status = STATUS_SUCCESS;
253 PIO_STACK_LOCATION Stack;
255 KdPrint((__DRIVER_NAME " --> WdmIrpPreprocessPNP\n"));
257 Stack = IoGetCurrentIrpStackLocation(Irp);
259 switch (Stack->MinorFunction) {
260 case IRP_MN_QUERY_DEVICE_RELATIONS:
261 KdPrint((__DRIVER_NAME " IRP_MN_QUERY_DEVICE_RELATIONS Device = %08x Irp = %08x, Stack = %08x\n", Device, Irp, Stack));
262 switch (Stack->Parameters.QueryDeviceRelations.Type)
263 {
264 case BusRelations:
265 KdPrint((__DRIVER_NAME " BusRelations\n"));
266 if (AutoEnumerate)
267 {
268 IoCopyCurrentIrpStackLocationToNext(Irp);
269 IoSetCompletionRoutine(Irp, XenHide_IoCompletion, NULL, TRUE, TRUE, TRUE);
270 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
271 }
272 else
273 {
274 IoSkipCurrentIrpStackLocation(Irp);
275 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
276 }
277 break;
278 case EjectionRelations:
279 IoSkipCurrentIrpStackLocation(Irp);
280 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
281 KdPrint((__DRIVER_NAME " EjectionRelations\n"));
282 break;
283 case RemovalRelations:
284 IoSkipCurrentIrpStackLocation(Irp);
285 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
286 KdPrint((__DRIVER_NAME " RemovalRelations\n"));
287 break;
288 case TargetDeviceRelation:
289 IoSkipCurrentIrpStackLocation(Irp);
290 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
291 KdPrint((__DRIVER_NAME " TargetDeviceRelation\n"));
292 break;
293 default:
294 IoSkipCurrentIrpStackLocation(Irp);
295 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
296 KdPrint((__DRIVER_NAME " Unknown Type %d\n", Stack->Parameters.QueryDeviceRelations.Type));
297 break;
298 }
299 break;
300 default:
301 IoSkipCurrentIrpStackLocation(Irp);
302 Status = WdfDeviceWdmDispatchPreprocessedIrp(Device, Irp);
303 KdPrint((__DRIVER_NAME " Unknown Minor %d\n", Stack->MinorFunction));
304 break;
305 }
307 KdPrint((__DRIVER_NAME " <-- WdmIrpPreprocessPNP (returning with status %08x\n", Status));
309 return Status;
310 }