Respect the fact, that the driver can return NULL-pointer, instead of a string having...
[reactos.git] / reactos / ntoskrnl / io / pnpmgr.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/pnpmgr.c
5 * PURPOSE: Initializes the PnP manager
6 *
7 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * Hervé Poussineau (hpoussin@reactos.org)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* GLOBALS *******************************************************************/
19
20 PDEVICE_NODE IopRootDeviceNode;
21 KSPIN_LOCK IopDeviceTreeLock;
22
23 /* DATA **********************************************************************/
24
25 PDRIVER_OBJECT IopRootDriverObject;
26 PIO_BUS_TYPE_GUID_LIST IopBusTypeGuidList = NULL;
27
28 // Static CRC table
29 ULONG crc32Table[256] =
30 {
31 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
32 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
33 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
34 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
35 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
36 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
37 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
38 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
39 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
40 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
41 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
42 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
43 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
44 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
45 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
46 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
47
48 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
49 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
50 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
51 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
52 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
53 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
54 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
55 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
56 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
57 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
58 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
59 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
60 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
61 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
62 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
63 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
64
65 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
66 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
67 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
68 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
69 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
70 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
71 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
72 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
73 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
74 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
75 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
76 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
77 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
78 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
79 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
80 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
81
82 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
83 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
84 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
85 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
86 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
87 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
88 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
89 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
90 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
91 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
92 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
93 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
94 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
95 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
96 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
97 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
98 };
99
100 static NTSTATUS INIT_FUNCTION
101 IopSetRootDeviceInstanceData(PDEVICE_NODE DeviceNode);
102
103 #if defined (ALLOC_PRAGMA)
104 #pragma alloc_text(INIT, IopSetRootDeviceInstanceData)
105 #pragma alloc_text(INIT, PnpInit)
106 #pragma alloc_text(INIT, PnpInit2)
107 #endif
108
109
110 /* FUNCTIONS *****************************************************************/
111
112 PDEVICE_NODE
113 FASTCALL
114 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject)
115 {
116 return ((PEXTENDED_DEVOBJ_EXTENSION)DeviceObject->DeviceObjectExtension)->DeviceNode;
117 }
118
119 NTSTATUS
120 STDCALL
121 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode,
122 PDEVICE_CAPABILITIES DeviceCaps)
123 {
124 IO_STATUS_BLOCK StatusBlock;
125 IO_STACK_LOCATION Stack;
126
127 /* Set up the Header */
128 RtlZeroMemory(DeviceCaps, sizeof(DEVICE_CAPABILITIES));
129 DeviceCaps->Size = sizeof(DEVICE_CAPABILITIES);
130 DeviceCaps->Version = 1;
131 DeviceCaps->Address = -1;
132 DeviceCaps->UINumber = -1;
133
134 /* Set up the Stack */
135 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
136 Stack.Parameters.DeviceCapabilities.Capabilities = DeviceCaps;
137
138 /* Send the IRP */
139 return IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
140 &StatusBlock,
141 IRP_MN_QUERY_CAPABILITIES,
142 &Stack);
143 }
144
145 /*
146 * @implemented
147 */
148 VOID
149 STDCALL
150 IoInvalidateDeviceRelations(IN PDEVICE_OBJECT DeviceObject,
151 IN DEVICE_RELATION_TYPE Type)
152 {
153 IopInvalidateDeviceRelations(IopGetDeviceNode(DeviceObject), Type);
154 }
155
156 /*
157 * @unimplemented
158 */
159 NTSTATUS
160 STDCALL
161 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject,
162 IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
163 IN ULONG BufferLength,
164 OUT PVOID PropertyBuffer,
165 OUT PULONG ResultLength)
166 {
167 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
168 DEVICE_CAPABILITIES DeviceCaps;
169 ULONG Length;
170 PVOID Data = NULL;
171 PWSTR Ptr;
172 NTSTATUS Status;
173
174 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject, DeviceProperty);
175
176 if (DeviceNode == NULL)
177 return STATUS_INVALID_DEVICE_REQUEST;
178
179 switch (DeviceProperty)
180 {
181 case DevicePropertyBusNumber:
182 Length = sizeof(ULONG);
183 Data = &DeviceNode->ChildBusNumber;
184 break;
185
186 /* Complete, untested */
187 case DevicePropertyBusTypeGuid:
188 /* Sanity check */
189 if ((DeviceNode->ChildBusTypeIndex != 0xFFFF) &&
190 (DeviceNode->ChildBusTypeIndex < IopBusTypeGuidList->GuidCount))
191 {
192 /* Return the GUID */
193 *ResultLength = sizeof(GUID);
194
195 /* Check if the buffer given was large enough */
196 if (BufferLength < *ResultLength)
197 {
198 return STATUS_BUFFER_TOO_SMALL;
199 }
200
201 /* Copy the GUID */
202 RtlCopyMemory(PropertyBuffer,
203 &(IopBusTypeGuidList->Guids[DeviceNode->ChildBusTypeIndex]),
204 sizeof(GUID));
205 return STATUS_SUCCESS;
206 }
207 else
208 {
209 return STATUS_OBJECT_NAME_NOT_FOUND;
210 }
211 break;
212
213 case DevicePropertyLegacyBusType:
214 Length = sizeof(INTERFACE_TYPE);
215 Data = &DeviceNode->ChildInterfaceType;
216 break;
217
218 case DevicePropertyAddress:
219 /* Query the device caps */
220 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps);
221 if (NT_SUCCESS(Status) && (DeviceCaps.Address != (ULONG)-1))
222 {
223 /* Return length */
224 *ResultLength = sizeof(ULONG);
225
226 /* Check if the buffer given was large enough */
227 if (BufferLength < *ResultLength)
228 {
229 return STATUS_BUFFER_TOO_SMALL;
230 }
231
232 /* Return address */
233 *(PULONG)PropertyBuffer = DeviceCaps.Address;
234 return STATUS_SUCCESS;
235 }
236 else
237 {
238 return STATUS_OBJECT_NAME_NOT_FOUND;
239 }
240 break;
241
242 // case DevicePropertyUINumber:
243 // if (DeviceNode->CapabilityFlags == NULL)
244 // return STATUS_INVALID_DEVICE_REQUEST;
245 // Length = sizeof(ULONG);
246 // Data = &DeviceNode->CapabilityFlags->UINumber;
247 // break;
248
249 case DevicePropertyClassName:
250 case DevicePropertyClassGuid:
251 case DevicePropertyDriverKeyName:
252 case DevicePropertyManufacturer:
253 case DevicePropertyFriendlyName:
254 case DevicePropertyHardwareID:
255 case DevicePropertyCompatibleIDs:
256 case DevicePropertyDeviceDescription:
257 case DevicePropertyLocationInformation:
258 case DevicePropertyUINumber:
259 {
260 LPWSTR RegistryPropertyName, KeyNameBuffer;
261 UNICODE_STRING KeyName, ValueName;
262 OBJECT_ATTRIBUTES ObjectAttributes;
263 KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
264 ULONG ValueInformationLength;
265 HANDLE KeyHandle;
266 NTSTATUS Status;
267
268 switch (DeviceProperty)
269 {
270 case DevicePropertyClassName:
271 RegistryPropertyName = L"Class"; break;
272 case DevicePropertyClassGuid:
273 RegistryPropertyName = L"ClassGuid"; break;
274 case DevicePropertyDriverKeyName:
275 RegistryPropertyName = L"Driver"; break;
276 case DevicePropertyManufacturer:
277 RegistryPropertyName = L"Mfg"; break;
278 case DevicePropertyFriendlyName:
279 RegistryPropertyName = L"FriendlyName"; break;
280 case DevicePropertyHardwareID:
281 RegistryPropertyName = L"HardwareID"; break;
282 case DevicePropertyCompatibleIDs:
283 RegistryPropertyName = L"CompatibleIDs"; break;
284 case DevicePropertyDeviceDescription:
285 RegistryPropertyName = L"DeviceDesc"; break;
286 case DevicePropertyLocationInformation:
287 RegistryPropertyName = L"LocationInformation"; break;
288 case DevicePropertyUINumber:
289 RegistryPropertyName = L"UINumber"; break;
290 default:
291 RegistryPropertyName = NULL; break;
292 }
293
294 KeyNameBuffer = ExAllocatePool(PagedPool,
295 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
296
297 DPRINT("KeyNameBuffer: 0x%p, value %S\n", KeyNameBuffer, RegistryPropertyName);
298
299 if (KeyNameBuffer == NULL)
300 return STATUS_INSUFFICIENT_RESOURCES;
301
302 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
303 wcscat(KeyNameBuffer, DeviceNode->InstancePath.Buffer);
304 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
305 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
306 OBJ_CASE_INSENSITIVE, NULL, NULL);
307
308 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
309 ExFreePool(KeyNameBuffer);
310 if (!NT_SUCCESS(Status))
311 return Status;
312
313 RtlInitUnicodeString(&ValueName, RegistryPropertyName);
314 ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
315 Data[0]) + BufferLength;
316 ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
317 if (ValueInformation == NULL)
318 {
319 ZwClose(KeyHandle);
320 return STATUS_INSUFFICIENT_RESOURCES;
321 }
322
323 Status = ZwQueryValueKey(KeyHandle, &ValueName,
324 KeyValuePartialInformation, ValueInformation,
325 ValueInformationLength,
326 &ValueInformationLength);
327 *ResultLength = ValueInformation->DataLength;
328 ZwClose(KeyHandle);
329
330 if (!NT_SUCCESS(Status))
331 {
332 ExFreePool(ValueInformation);
333 if (Status == STATUS_BUFFER_OVERFLOW)
334 return STATUS_BUFFER_TOO_SMALL;
335 else
336 return Status;
337 }
338
339 /* FIXME: Verify the value (NULL-terminated, correct format). */
340
341 RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
342 ValueInformation->DataLength);
343 ExFreePool(ValueInformation);
344
345 return STATUS_SUCCESS;
346 }
347
348 case DevicePropertyBootConfiguration:
349 Length = 0;
350 if (DeviceNode->BootResources->Count != 0)
351 {
352 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
353 }
354 Data = &DeviceNode->BootResources;
355 break;
356
357 /* FIXME: use a translated boot configuration instead */
358 case DevicePropertyBootConfigurationTranslated:
359 Length = 0;
360 if (DeviceNode->BootResources->Count != 0)
361 {
362 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
363 }
364 Data = &DeviceNode->BootResources;
365 break;
366
367 case DevicePropertyEnumeratorName:
368 Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
369 if (Ptr != NULL)
370 {
371 Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
372 Data = DeviceNode->InstancePath.Buffer;
373 }
374 else
375 {
376 Length = 0;
377 Data = NULL;
378 }
379 break;
380
381 case DevicePropertyPhysicalDeviceObjectName:
382 Length = DeviceNode->InstancePath.Length + sizeof(WCHAR);
383 Data = DeviceNode->InstancePath.Buffer;
384 break;
385
386 default:
387 return STATUS_INVALID_PARAMETER_2;
388 }
389
390 *ResultLength = Length;
391 if (BufferLength < Length)
392 return STATUS_BUFFER_TOO_SMALL;
393 RtlCopyMemory(PropertyBuffer, Data, Length);
394
395 /* Terminate the string */
396 if (DeviceProperty == DevicePropertyEnumeratorName
397 || DeviceProperty == DevicePropertyPhysicalDeviceObjectName)
398 {
399 Ptr = (PWSTR)PropertyBuffer;
400 Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
401 }
402
403 return STATUS_SUCCESS;
404 }
405
406 /*
407 * @unimplemented
408 */
409 VOID
410 STDCALL
411 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject)
412 {
413 }
414
415 /**
416 * @name IoOpenDeviceRegistryKey
417 *
418 * Open a registry key unique for a specified driver or device instance.
419 *
420 * @param DeviceObject Device to get the registry key for.
421 * @param DevInstKeyType Type of the key to return.
422 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
423 * @param DevInstRegKey Handle to the opened registry key on
424 * successful return.
425 *
426 * @return Status.
427 *
428 * @implemented
429 */
430 NTSTATUS
431 STDCALL
432 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject,
433 IN ULONG DevInstKeyType,
434 IN ACCESS_MASK DesiredAccess,
435 OUT PHANDLE DevInstRegKey)
436 {
437 static WCHAR RootKeyName[] =
438 L"\\Registry\\Machine\\System\\CurrentControlSet\\";
439 static WCHAR ProfileKeyName[] =
440 L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
441 static WCHAR ClassKeyName[] = L"Control\\Class\\";
442 static WCHAR EnumKeyName[] = L"Enum\\";
443 static WCHAR DeviceParametersKeyName[] = L"Device Parameters";
444 ULONG KeyNameLength;
445 LPWSTR KeyNameBuffer;
446 UNICODE_STRING KeyName;
447 ULONG DriverKeyLength;
448 OBJECT_ATTRIBUTES ObjectAttributes;
449 PDEVICE_NODE DeviceNode = NULL;
450 NTSTATUS Status;
451
452 DPRINT("IoOpenDeviceRegistryKey() called\n");
453
454 if ((DevInstKeyType & (PLUGPLAY_REGKEY_DEVICE | PLUGPLAY_REGKEY_DRIVER)) == 0)
455 return STATUS_INVALID_PARAMETER;
456
457 /*
458 * Calculate the length of the base key name. This is the full
459 * name for driver key or the name excluding "Device Parameters"
460 * subkey for device key.
461 */
462
463 KeyNameLength = sizeof(RootKeyName);
464 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
465 KeyNameLength += sizeof(ProfileKeyName) - sizeof(UNICODE_NULL);
466 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
467 {
468 KeyNameLength += sizeof(ClassKeyName) - sizeof(UNICODE_NULL);
469 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
470 0, NULL, &DriverKeyLength);
471 if (Status != STATUS_BUFFER_TOO_SMALL)
472 return Status;
473 KeyNameLength += DriverKeyLength;
474 }
475 else
476 {
477 DeviceNode = IopGetDeviceNode(DeviceObject);
478 KeyNameLength += sizeof(EnumKeyName) - sizeof(UNICODE_NULL) +
479 DeviceNode->InstancePath.Length;
480 }
481
482 /*
483 * Now allocate the buffer for the key name...
484 */
485
486 KeyNameBuffer = ExAllocatePool(PagedPool, KeyNameLength);
487 if (KeyNameBuffer == NULL)
488 return STATUS_INSUFFICIENT_RESOURCES;
489
490 KeyName.Length = 0;
491 KeyName.MaximumLength = KeyNameLength;
492 KeyName.Buffer = KeyNameBuffer;
493
494 /*
495 * ...and build the key name.
496 */
497
498 KeyName.Length += sizeof(RootKeyName) - sizeof(UNICODE_NULL);
499 RtlCopyMemory(KeyNameBuffer, RootKeyName, KeyName.Length);
500
501 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
502 RtlAppendUnicodeToString(&KeyName, ProfileKeyName);
503
504 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
505 {
506 RtlAppendUnicodeToString(&KeyName, ClassKeyName);
507 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
508 DriverKeyLength, KeyNameBuffer +
509 (KeyName.Length / sizeof(WCHAR)),
510 &DriverKeyLength);
511 if (!NT_SUCCESS(Status))
512 {
513 ExFreePool(KeyNameBuffer);
514 return Status;
515 }
516 KeyName.Length += DriverKeyLength - sizeof(UNICODE_NULL);
517 }
518 else
519 {
520 RtlAppendUnicodeToString(&KeyName, EnumKeyName);
521 Status = RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->InstancePath);
522 if (DeviceNode->InstancePath.Length == 0)
523 {
524 ExFreePool(KeyNameBuffer);
525 return Status;
526 }
527 }
528
529 /*
530 * Open the base key.
531 */
532
533 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
534 OBJ_CASE_INSENSITIVE, NULL, NULL);
535 Status = ZwOpenKey(DevInstRegKey, DesiredAccess, &ObjectAttributes);
536 ExFreePool(KeyNameBuffer);
537
538 /*
539 * For driver key we're done now. Also if the base key doesn't
540 * exist we can bail out with error...
541 */
542
543 if ((DevInstKeyType & PLUGPLAY_REGKEY_DRIVER) || !NT_SUCCESS(Status))
544 {
545 DPRINT1("IoOpenDeviceRegistryKey(): Base key doesn't exist, exiting...\n");
546 return Status;
547 }
548
549 /*
550 * Let's go further. For device key we must open "Device Parameters"
551 * subkey and create it if it doesn't exist yet.
552 */
553
554 RtlInitUnicodeString(&KeyName, DeviceParametersKeyName);
555 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
556 OBJ_CASE_INSENSITIVE, *DevInstRegKey, NULL);
557 Status = ZwCreateKey(DevInstRegKey, DesiredAccess, &ObjectAttributes,
558 0, NULL, REG_OPTION_NON_VOLATILE, NULL);
559 ZwClose(ObjectAttributes.RootDirectory);
560
561 return Status;
562 }
563
564 /*
565 * @unimplemented
566 */
567 VOID
568 STDCALL
569 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject)
570 {
571 UNIMPLEMENTED;
572 }
573
574
575 NTSTATUS
576 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
577 {
578 KIRQL OldIrql;
579
580 if (PopSystemPowerDeviceNode)
581 {
582 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
583 *DeviceObject = PopSystemPowerDeviceNode->PhysicalDeviceObject;
584 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
585
586 return STATUS_SUCCESS;
587 }
588
589 return STATUS_UNSUCCESSFUL;
590 }
591
592 USHORT
593 STDCALL
594 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid)
595 {
596 USHORT i = 0, FoundIndex = 0xFFFF;
597 ULONG NewSize;
598 PVOID NewList;
599
600 /* Acquire the lock */
601 ExAcquireFastMutex(&IopBusTypeGuidList->Lock);
602
603 /* Loop all entries */
604 while (i < IopBusTypeGuidList->GuidCount)
605 {
606 /* Try to find a match */
607 if (RtlCompareMemory(BusTypeGuid,
608 &IopBusTypeGuidList->Guids[i],
609 sizeof(GUID)))
610 {
611 /* Found it */
612 FoundIndex = i;
613 goto Quickie;
614 }
615 i++;
616 }
617
618 /* Check if we have to grow the list */
619 if (IopBusTypeGuidList->GuidCount)
620 {
621 /* Calculate the new size */
622 NewSize = sizeof(IO_BUS_TYPE_GUID_LIST) +
623 (sizeof(GUID) * IopBusTypeGuidList->GuidCount);
624
625 /* Allocate the new copy */
626 NewList = ExAllocatePool(PagedPool, NewSize);
627
628 /* Now copy them, decrease the size too */
629 NewSize -= sizeof(GUID);
630 RtlCopyMemory(NewList, IopBusTypeGuidList, NewSize);
631
632 /* Free the old list */
633 ExFreePool(IopBusTypeGuidList);
634
635 /* Use the new buffer */
636 IopBusTypeGuidList = NewList;
637 }
638
639 /* Copy the new GUID */
640 RtlCopyMemory(&IopBusTypeGuidList->Guids[IopBusTypeGuidList->GuidCount],
641 BusTypeGuid,
642 sizeof(GUID));
643
644 /* The new entry is the index */
645 FoundIndex = IopBusTypeGuidList->GuidCount;
646 IopBusTypeGuidList->GuidCount++;
647
648 Quickie:
649 ExReleaseFastMutex(&IopBusTypeGuidList->Lock);
650 return FoundIndex;
651 }
652
653 /*
654 * DESCRIPTION
655 * Creates a device node
656 *
657 * ARGUMENTS
658 * ParentNode = Pointer to parent device node
659 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
660 * to have the root device node create one
661 * (eg. for legacy drivers)
662 * DeviceNode = Pointer to storage for created device node
663 *
664 * RETURN VALUE
665 * Status
666 */
667 NTSTATUS
668 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
669 PDEVICE_OBJECT PhysicalDeviceObject,
670 PDEVICE_NODE *DeviceNode)
671 {
672 PDEVICE_NODE Node;
673 NTSTATUS Status;
674 KIRQL OldIrql;
675
676 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p\n",
677 ParentNode, PhysicalDeviceObject);
678
679 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
680 if (!Node)
681 {
682 return STATUS_INSUFFICIENT_RESOURCES;
683 }
684
685 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
686
687 if (!PhysicalDeviceObject)
688 {
689 Status = PnpRootCreateDevice(&PhysicalDeviceObject);
690 if (!NT_SUCCESS(Status))
691 {
692 ExFreePool(Node);
693 return Status;
694 }
695
696 /* This is for drivers passed on the command line to ntoskrnl.exe */
697 IopDeviceNodeSetFlag(Node, DNF_STARTED);
698 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
699 }
700
701 Node->PhysicalDeviceObject = PhysicalDeviceObject;
702
703 ((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode = Node;
704
705 if (ParentNode)
706 {
707 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
708 Node->Parent = ParentNode;
709 Node->NextSibling = ParentNode->Child;
710 if (ParentNode->Child != NULL)
711 {
712 ParentNode->Child->PrevSibling = Node;
713 }
714 ParentNode->Child = Node;
715 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
716 Node->Level = ParentNode->Level + 1;
717 }
718
719 *DeviceNode = Node;
720
721 return STATUS_SUCCESS;
722 }
723
724 NTSTATUS
725 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
726 {
727 KIRQL OldIrql;
728
729 /* All children must be deleted before a parent is deleted */
730 ASSERT(!DeviceNode->Child);
731
732 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
733
734 ASSERT(DeviceNode->PhysicalDeviceObject);
735
736 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
737
738 /* Unlink from parent if it exists */
739
740 if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
741 {
742 DeviceNode->Parent->Child = DeviceNode->NextSibling;
743 }
744
745 /* Unlink from sibling list */
746
747 if (DeviceNode->PrevSibling)
748 {
749 DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
750 }
751
752 if (DeviceNode->NextSibling)
753 {
754 DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
755 }
756
757 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
758
759 RtlFreeUnicodeString(&DeviceNode->InstancePath);
760
761 RtlFreeUnicodeString(&DeviceNode->ServiceName);
762
763 if (DeviceNode->ResourceList)
764 {
765 ExFreePool(DeviceNode->ResourceList);
766 }
767
768 if (DeviceNode->ResourceListTranslated)
769 {
770 ExFreePool(DeviceNode->ResourceListTranslated);
771 }
772
773 if (DeviceNode->ResourceRequirements)
774 {
775 ExFreePool(DeviceNode->ResourceRequirements);
776 }
777
778 if (DeviceNode->BootResources)
779 {
780 ExFreePool(DeviceNode->BootResources);
781 }
782
783 ExFreePool(DeviceNode);
784
785 return STATUS_SUCCESS;
786 }
787
788 NTSTATUS
789 IopInitiatePnpIrp(PDEVICE_OBJECT DeviceObject,
790 PIO_STATUS_BLOCK IoStatusBlock,
791 ULONG MinorFunction,
792 PIO_STACK_LOCATION Stack OPTIONAL)
793 {
794 PDEVICE_OBJECT TopDeviceObject;
795 PIO_STACK_LOCATION IrpSp;
796 NTSTATUS Status;
797 KEVENT Event;
798 PIRP Irp;
799
800 /* Always call the top of the device stack */
801 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
802
803 KeInitializeEvent(
804 &Event,
805 NotificationEvent,
806 FALSE);
807
808 Irp = IoBuildSynchronousFsdRequest(
809 IRP_MJ_PNP,
810 TopDeviceObject,
811 NULL,
812 0,
813 NULL,
814 &Event,
815 IoStatusBlock);
816
817 /* PNP IRPs are always initialized with a status code of
818 STATUS_NOT_IMPLEMENTED */
819 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
820 Irp->IoStatus.Information = 0;
821
822 IrpSp = IoGetNextIrpStackLocation(Irp);
823 IrpSp->MinorFunction = MinorFunction;
824
825 if (Stack)
826 {
827 RtlMoveMemory(&IrpSp->Parameters,
828 &Stack->Parameters,
829 sizeof(Stack->Parameters));
830 }
831
832 Status = IoCallDriver(TopDeviceObject, Irp);
833 if (Status == STATUS_PENDING)
834 {
835 KeWaitForSingleObject(&Event,
836 Executive,
837 KernelMode,
838 FALSE,
839 NULL);
840 Status = IoStatusBlock->Status;
841 }
842
843 ObDereferenceObject(TopDeviceObject);
844
845 return Status;
846 }
847
848
849 NTSTATUS
850 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context)
851 {
852 PDEVICE_NODE ParentDeviceNode;
853 PDEVICE_NODE ChildDeviceNode;
854 NTSTATUS Status;
855
856 /* Copy context data so we don't overwrite it in subsequent calls to this function */
857 ParentDeviceNode = Context->DeviceNode;
858
859 /* Call the action routine */
860 Status = (Context->Action)(ParentDeviceNode, Context->Context);
861 if (!NT_SUCCESS(Status))
862 {
863 return Status;
864 }
865
866 /* Traversal of all children nodes */
867 for (ChildDeviceNode = ParentDeviceNode->Child;
868 ChildDeviceNode != NULL;
869 ChildDeviceNode = ChildDeviceNode->NextSibling)
870 {
871 /* Pass the current device node to the action routine */
872 Context->DeviceNode = ChildDeviceNode;
873
874 Status = IopTraverseDeviceTreeNode(Context);
875 if (!NT_SUCCESS(Status))
876 {
877 return Status;
878 }
879 }
880
881 return Status;
882 }
883
884
885 NTSTATUS
886 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context)
887 {
888 NTSTATUS Status;
889
890 DPRINT("Context 0x%p\n", Context);
891
892 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
893 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
894
895 /* Start from the specified device node */
896 Context->DeviceNode = Context->FirstDeviceNode;
897
898 /* Recursively traverse the device tree */
899 Status = IopTraverseDeviceTreeNode(Context);
900 if (Status == STATUS_UNSUCCESSFUL)
901 {
902 /* The action routine just wanted to terminate the traversal with status
903 code STATUS_SUCCESS */
904 Status = STATUS_SUCCESS;
905 }
906
907 return Status;
908 }
909
910
911 static
912 NTSTATUS
913 IopCreateDeviceKeyPath(PWSTR Path,
914 PHANDLE Handle)
915 {
916 OBJECT_ATTRIBUTES ObjectAttributes;
917 WCHAR KeyBuffer[MAX_PATH];
918 UNICODE_STRING KeyName;
919 HANDLE KeyHandle;
920 NTSTATUS Status;
921 PWCHAR Current;
922 PWCHAR Next;
923
924 *Handle = NULL;
925
926 if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
927 {
928 return STATUS_INVALID_PARAMETER;
929 }
930
931 wcsncpy (KeyBuffer, Path, MAX_PATH-1);
932
933 /* Skip \\Registry\\ */
934 Current = KeyBuffer;
935 Current = wcschr (Current, L'\\') + 1;
936 Current = wcschr (Current, L'\\') + 1;
937
938 while (TRUE)
939 {
940 Next = wcschr (Current, L'\\');
941 if (Next == NULL)
942 {
943 /* The end */
944 }
945 else
946 {
947 *Next = 0;
948 }
949
950 RtlInitUnicodeString (&KeyName, KeyBuffer);
951 InitializeObjectAttributes (&ObjectAttributes,
952 &KeyName,
953 OBJ_CASE_INSENSITIVE,
954 NULL,
955 NULL);
956
957 DPRINT("Create '%S'\n", KeyName.Buffer);
958
959 Status = ZwCreateKey (&KeyHandle,
960 KEY_ALL_ACCESS,
961 &ObjectAttributes,
962 0,
963 NULL,
964 0,
965 NULL);
966 if (!NT_SUCCESS (Status))
967 {
968 DPRINT ("ZwCreateKey() failed with status %x\n", Status);
969 return Status;
970 }
971
972 if (Next == NULL)
973 {
974 *Handle = KeyHandle;
975 return STATUS_SUCCESS;
976 }
977 else
978 {
979 ZwClose (KeyHandle);
980 *Next = L'\\';
981 }
982
983 Current = Next + 1;
984 }
985
986 return STATUS_UNSUCCESSFUL;
987 }
988
989
990 static
991 NTSTATUS
992 IopSetDeviceInstanceData(HANDLE InstanceKey,
993 PDEVICE_NODE DeviceNode)
994 {
995 OBJECT_ATTRIBUTES ObjectAttributes;
996 UNICODE_STRING KeyName;
997 HANDLE LogConfKey;
998 ULONG ResCount;
999 ULONG ListSize;
1000 NTSTATUS Status;
1001
1002 DPRINT("IopSetDeviceInstanceData() called\n");
1003
1004 /* Create the 'LogConf' key */
1005 RtlInitUnicodeString(&KeyName, L"LogConf");
1006 InitializeObjectAttributes(&ObjectAttributes,
1007 &KeyName,
1008 OBJ_CASE_INSENSITIVE,
1009 InstanceKey,
1010 NULL);
1011 Status = ZwCreateKey(&LogConfKey,
1012 KEY_ALL_ACCESS,
1013 &ObjectAttributes,
1014 0,
1015 NULL,
1016 0,
1017 NULL);
1018 if (NT_SUCCESS(Status))
1019 {
1020 /* Set 'BootConfig' value */
1021 if (DeviceNode->BootResources != NULL)
1022 {
1023 ResCount = DeviceNode->BootResources->Count;
1024 if (ResCount != 0)
1025 {
1026 ListSize = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
1027
1028 RtlInitUnicodeString(&KeyName, L"BootConfig");
1029 Status = ZwSetValueKey(LogConfKey,
1030 &KeyName,
1031 0,
1032 REG_RESOURCE_LIST,
1033 &DeviceNode->BootResources,
1034 ListSize);
1035 }
1036 }
1037
1038 /* Set 'BasicConfigVector' value */
1039 if (DeviceNode->ResourceRequirements != NULL &&
1040 DeviceNode->ResourceRequirements->ListSize != 0)
1041 {
1042 RtlInitUnicodeString(&KeyName, L"BasicConfigVector");
1043 Status = ZwSetValueKey(LogConfKey,
1044 &KeyName,
1045 0,
1046 REG_RESOURCE_REQUIREMENTS_LIST,
1047 DeviceNode->ResourceRequirements,
1048 DeviceNode->ResourceRequirements->ListSize);
1049 }
1050
1051 ZwClose(LogConfKey);
1052 }
1053
1054 #if 0
1055 if (DeviceNode->PhysicalDeviceObject != NULL)
1056 {
1057 /* Create the 'Control' key */
1058 RtlInitUnicodeString(&KeyName,
1059 L"Control");
1060 InitializeObjectAttributes(&ObjectAttributes,
1061 &KeyName,
1062 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
1063 InstanceKey,
1064 NULL);
1065 Status = ZwCreateKey(&LogConfKey,
1066 KEY_ALL_ACCESS,
1067 &ObjectAttributes,
1068 0,
1069 NULL,
1070 REG_OPTION_VOLATILE,
1071 NULL);
1072 if (NT_SUCCESS(Status))
1073 {
1074 ULONG Reference = (ULONG)DeviceNode->PhysicalDeviceObject;
1075 RtlInitUnicodeString(&KeyName,
1076 L"DeviceReference");
1077 Status = ZwSetValueKey(LogConfKey,
1078 &KeyName,
1079 0,
1080 REG_DWORD,
1081 &Reference,
1082 sizeof(PVOID));
1083
1084 ZwClose(LogConfKey);
1085 }
1086 }
1087 #endif
1088
1089 DPRINT("IopSetDeviceInstanceData() done\n");
1090
1091 return STATUS_SUCCESS;
1092 }
1093
1094
1095 NTSTATUS
1096 IopAssignDeviceResources(PDEVICE_NODE DeviceNode)
1097 {
1098 PIO_RESOURCE_LIST ResourceList;
1099 PIO_RESOURCE_DESCRIPTOR ResourceDescriptor;
1100 PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw, DescriptorTranslated;
1101 ULONG NumberOfResources = 0;
1102 ULONG i;
1103 NTSTATUS Status;
1104
1105 /* Fill DeviceNode->ResourceList and DeviceNode->ResourceListTranslated;
1106 * by using DeviceNode->ResourceRequirements */
1107
1108 if (!DeviceNode->ResourceRequirements
1109 || DeviceNode->ResourceRequirements->AlternativeLists == 0)
1110 {
1111 DeviceNode->ResourceList = DeviceNode->ResourceListTranslated = NULL;
1112 return STATUS_SUCCESS;
1113 }
1114
1115 /* FIXME: that's here that PnP arbiter should go */
1116 /* Actually, simply use resource list #0 as assigned resource list */
1117 ResourceList = &DeviceNode->ResourceRequirements->List[0];
1118 if (ResourceList->Version != 1 || ResourceList->Revision != 1)
1119 {
1120 Status = STATUS_REVISION_MISMATCH;
1121 goto ByeBye;
1122 }
1123
1124 DeviceNode->ResourceList = ExAllocatePool(PagedPool,
1125 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1126 if (!DeviceNode->ResourceList)
1127 {
1128 Status = STATUS_INSUFFICIENT_RESOURCES;
1129 goto ByeBye;
1130 }
1131
1132 DeviceNode->ResourceListTranslated = ExAllocatePool(PagedPool,
1133 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1134 if (!DeviceNode->ResourceListTranslated)
1135 {
1136 Status = STATUS_INSUFFICIENT_RESOURCES;
1137 goto ByeBye;
1138 }
1139
1140 DeviceNode->ResourceList->Count = 1;
1141 DeviceNode->ResourceList->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1142 DeviceNode->ResourceList->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1143 DeviceNode->ResourceList->List[0].PartialResourceList.Version = 1;
1144 DeviceNode->ResourceList->List[0].PartialResourceList.Revision = 1;
1145
1146 DeviceNode->ResourceListTranslated->Count = 1;
1147 DeviceNode->ResourceListTranslated->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1148 DeviceNode->ResourceListTranslated->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1149 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Version = 1;
1150 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Revision = 1;
1151
1152 for (i = 0; i < ResourceList->Count; i++)
1153 {
1154 ResourceDescriptor = &ResourceList->Descriptors[i];
1155
1156 if (ResourceDescriptor->Option == 0 || ResourceDescriptor->Option == IO_RESOURCE_PREFERRED)
1157 {
1158 DescriptorRaw = &DeviceNode->ResourceList->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1159 DescriptorTranslated = &DeviceNode->ResourceListTranslated->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1160 NumberOfResources++;
1161
1162 /* Copy ResourceDescriptor to DescriptorRaw and DescriptorTranslated */
1163 DescriptorRaw->Type = DescriptorTranslated->Type = ResourceDescriptor->Type;
1164 DescriptorRaw->ShareDisposition = DescriptorTranslated->ShareDisposition = ResourceDescriptor->ShareDisposition;
1165 DescriptorRaw->Flags = DescriptorTranslated->Flags = ResourceDescriptor->Flags;
1166 switch (ResourceDescriptor->Type)
1167 {
1168 case CmResourceTypePort:
1169 {
1170 ULONG AddressSpace = 0; /* IO space */
1171 DescriptorRaw->u.Port.Start = ResourceDescriptor->u.Port.MinimumAddress;
1172 DescriptorRaw->u.Port.Length = DescriptorTranslated->u.Port.Length
1173 = ResourceDescriptor->u.Port.Length;
1174 if (!HalTranslateBusAddress(
1175 DeviceNode->ResourceRequirements->InterfaceType,
1176 DeviceNode->ResourceRequirements->BusNumber,
1177 DescriptorRaw->u.Port.Start,
1178 &AddressSpace,
1179 &DescriptorTranslated->u.Port.Start))
1180 {
1181 Status = STATUS_UNSUCCESSFUL;
1182 goto ByeBye;
1183 }
1184 break;
1185 }
1186 case CmResourceTypeInterrupt:
1187 {
1188 INTERFACE_TYPE BusType;
1189 ULONG SlotNumber;
1190 ULONG ret;
1191 UCHAR Irq;
1192
1193 DescriptorRaw->u.Interrupt.Level = 0;
1194 DescriptorRaw->u.Interrupt.Vector = ResourceDescriptor->u.Interrupt.MinimumVector;
1195 /* FIXME: HACK: if we have a PCI device, we try
1196 * to keep the IRQ assigned by the BIOS */
1197 if (NT_SUCCESS(IoGetDeviceProperty(
1198 DeviceNode->PhysicalDeviceObject,
1199 DevicePropertyLegacyBusType,
1200 sizeof(INTERFACE_TYPE),
1201 &BusType,
1202 &ret)) && BusType == PCIBus)
1203 {
1204 /* We have a PCI bus */
1205 if (NT_SUCCESS(IoGetDeviceProperty(
1206 DeviceNode->PhysicalDeviceObject,
1207 DevicePropertyAddress,
1208 sizeof(ULONG),
1209 &SlotNumber,
1210 &ret)) && SlotNumber > 0)
1211 {
1212 /* We have a good slot number */
1213 ret = HalGetBusDataByOffset(PCIConfiguration,
1214 DeviceNode->ResourceRequirements->BusNumber,
1215 SlotNumber,
1216 &Irq,
1217 0x3c /* PCI_INTERRUPT_LINE */,
1218 sizeof(UCHAR));
1219 if (ret != 0 && ret != 2
1220 && ResourceDescriptor->u.Interrupt.MinimumVector <= Irq
1221 && ResourceDescriptor->u.Interrupt.MaximumVector >= Irq)
1222 {
1223 /* The device already has an assigned IRQ */
1224 DescriptorRaw->u.Interrupt.Vector = Irq;
1225 }
1226 else
1227 {
1228 DPRINT1("Trying to assign IRQ 0x%lx to %wZ\n",
1229 DescriptorRaw->u.Interrupt.Vector,
1230 &DeviceNode->InstancePath);
1231 Irq = (UCHAR)DescriptorRaw->u.Interrupt.Vector;
1232 ret = HalSetBusDataByOffset(PCIConfiguration,
1233 DeviceNode->ResourceRequirements->BusNumber,
1234 SlotNumber,
1235 &Irq,
1236 0x3c /* PCI_INTERRUPT_LINE */,
1237 sizeof(UCHAR));
1238 if (ret == 0 || ret == 2)
1239 KEBUGCHECK(0);
1240 }
1241 }
1242 }
1243
1244 DescriptorTranslated->u.Interrupt.Level = 0;
1245 DescriptorTranslated->u.Interrupt.Vector = HalGetInterruptVector(
1246 DeviceNode->ResourceRequirements->InterfaceType,
1247 DeviceNode->ResourceRequirements->BusNumber,
1248 DescriptorRaw->u.Interrupt.Level,
1249 DescriptorRaw->u.Interrupt.Vector,
1250 (PKIRQL)&DescriptorTranslated->u.Interrupt.Level,
1251 &DescriptorRaw->u.Interrupt.Affinity);
1252 DescriptorTranslated->u.Interrupt.Affinity = DescriptorRaw->u.Interrupt.Affinity;
1253 break;
1254 }
1255 case CmResourceTypeMemory:
1256 {
1257 ULONG AddressSpace = 1; /* Memory space */
1258 DescriptorRaw->u.Memory.Start = ResourceDescriptor->u.Memory.MinimumAddress;
1259 DescriptorRaw->u.Memory.Length = DescriptorTranslated->u.Memory.Length
1260 = ResourceDescriptor->u.Memory.Length;
1261 if (!HalTranslateBusAddress(
1262 DeviceNode->ResourceRequirements->InterfaceType,
1263 DeviceNode->ResourceRequirements->BusNumber,
1264 DescriptorRaw->u.Memory.Start,
1265 &AddressSpace,
1266 &DescriptorTranslated->u.Memory.Start))
1267 {
1268 Status = STATUS_UNSUCCESSFUL;
1269 goto ByeBye;
1270 }
1271 break;
1272 }
1273 case CmResourceTypeDma:
1274 {
1275 DescriptorRaw->u.Dma.Channel = DescriptorTranslated->u.Dma.Channel
1276 = ResourceDescriptor->u.Dma.MinimumChannel;
1277 DescriptorRaw->u.Dma.Port = DescriptorTranslated->u.Dma.Port
1278 = 0; /* FIXME */
1279 DescriptorRaw->u.Dma.Reserved1 = DescriptorTranslated->u.Dma.Reserved1
1280 = 0;
1281 break;
1282 }
1283 case CmResourceTypeBusNumber:
1284 {
1285 DescriptorRaw->u.BusNumber.Start = DescriptorTranslated->u.BusNumber.Start
1286 = ResourceDescriptor->u.BusNumber.MinBusNumber;
1287 DescriptorRaw->u.BusNumber.Length = DescriptorTranslated->u.BusNumber.Length
1288 = ResourceDescriptor->u.BusNumber.Length;
1289 DescriptorRaw->u.BusNumber.Reserved = DescriptorTranslated->u.BusNumber.Reserved
1290 = ResourceDescriptor->u.BusNumber.Reserved;
1291 break;
1292 }
1293 /*CmResourceTypeDevicePrivate:
1294 case CmResourceTypePcCardConfig:
1295 case CmResourceTypeMfCardConfig:
1296 {
1297 RtlCopyMemory(
1298 &DescriptorRaw->u.DevicePrivate,
1299 &ResourceDescriptor->u.DevicePrivate,
1300 sizeof(ResourceDescriptor->u.DevicePrivate));
1301 RtlCopyMemory(
1302 &DescriptorTranslated->u.DevicePrivate,
1303 &ResourceDescriptor->u.DevicePrivate,
1304 sizeof(ResourceDescriptor->u.DevicePrivate));
1305 break;
1306 }*/
1307 default:
1308 DPRINT1("IopAssignDeviceResources(): unknown resource descriptor type 0x%x\n", ResourceDescriptor->Type);
1309 NumberOfResources--;
1310 }
1311 }
1312
1313 }
1314
1315 DeviceNode->ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
1316 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Count = NumberOfResources;
1317
1318 return STATUS_SUCCESS;
1319
1320 ByeBye:
1321 if (DeviceNode->ResourceList)
1322 {
1323 ExFreePool(DeviceNode->ResourceList);
1324 DeviceNode->ResourceList = NULL;
1325 }
1326 if (DeviceNode->ResourceListTranslated)
1327 {
1328 ExFreePool(DeviceNode->ResourceListTranslated);
1329 DeviceNode->ResourceListTranslated = NULL;
1330 }
1331
1332 return Status;
1333 }
1334
1335
1336 /*
1337 * IopGetParentIdPrefix
1338 *
1339 * Retrieve (or create) a string which identifies a device.
1340 *
1341 * Parameters
1342 * DeviceNode
1343 * Pointer to device node.
1344 * ParentIdPrefix
1345 * Pointer to the string where is returned the parent node identifier
1346 *
1347 * Remarks
1348 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1349 * valid and its Buffer field is NULL-terminated. The caller needs to
1350 * to free the string with RtlFreeUnicodeString when it is no longer
1351 * needed.
1352 */
1353
1354 NTSTATUS
1355 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode,
1356 PUNICODE_STRING ParentIdPrefix)
1357 {
1358 ULONG KeyNameBufferLength;
1359 PWSTR KeyNameBuffer = NULL;
1360 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation = NULL;
1361 UNICODE_STRING KeyName;
1362 UNICODE_STRING KeyValue;
1363 UNICODE_STRING ValueName;
1364 OBJECT_ATTRIBUTES ObjectAttributes;
1365 HANDLE hKey = INVALID_HANDLE_VALUE;
1366 PBYTE currentByte;
1367 ULONG crc32 = 0;
1368 ULONG i;
1369 NTSTATUS Status;
1370
1371 /* HACK: As long as some devices have a NULL device
1372 * instance path, the following test is required :(
1373 */
1374 if (DeviceNode->Parent->InstancePath.Length == 0)
1375 return STATUS_UNSUCCESSFUL;
1376
1377 /* 1. Try to retrieve ParentIdPrefix from registry */
1378 KeyNameBufferLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + MAX_PATH * sizeof(WCHAR);
1379 ParentIdPrefixInformation = ExAllocatePool(PagedPool, KeyNameBufferLength + sizeof(WCHAR));
1380 if (!ParentIdPrefixInformation)
1381 {
1382 Status = STATUS_INSUFFICIENT_RESOURCES;
1383 goto cleanup;
1384 }
1385 KeyNameBuffer = ExAllocatePool(PagedPool, (49 * sizeof(WCHAR)) + DeviceNode->Parent->InstancePath.Length);
1386 if (!KeyNameBuffer)
1387 {
1388 Status = STATUS_INSUFFICIENT_RESOURCES;
1389 goto cleanup;
1390 }
1391 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1392 wcscat(KeyNameBuffer, DeviceNode->Parent->InstancePath.Buffer);
1393 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
1394 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
1395 Status = ZwOpenKey(&hKey, KEY_QUERY_VALUE | KEY_SET_VALUE, &ObjectAttributes);
1396 if (!NT_SUCCESS(Status))
1397 goto cleanup;
1398 RtlInitUnicodeString(&ValueName, L"ParentIdPrefix");
1399 Status = ZwQueryValueKey(
1400 hKey, &ValueName,
1401 KeyValuePartialInformation, ParentIdPrefixInformation,
1402 KeyNameBufferLength, &KeyNameBufferLength);
1403 if (NT_SUCCESS(Status))
1404 {
1405 if (ParentIdPrefixInformation->Type != REG_SZ)
1406 Status = STATUS_UNSUCCESSFUL;
1407 else
1408 {
1409 KeyValue.Length = KeyValue.MaximumLength = ParentIdPrefixInformation->DataLength;
1410 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1411 }
1412 goto cleanup;
1413 }
1414 if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
1415 {
1416 KeyValue.Length = KeyValue.MaximumLength = ParentIdPrefixInformation->DataLength;
1417 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1418 goto cleanup;
1419 }
1420
1421 /* 2. Create the ParentIdPrefix value */
1422 currentByte = (PBYTE)DeviceNode->Parent->InstancePath.Buffer;
1423 for (i = 0; i < DeviceNode->Parent->InstancePath.Length; i++, currentByte++)
1424 crc32 = (crc32 >> 8) ^ crc32Table[*currentByte ^ (crc32 & 0xff)];
1425 crc32 = ~crc32;
1426 swprintf((PWSTR)ParentIdPrefixInformation->Data, L"%lx&%lx", DeviceNode->Parent->Level, crc32);
1427 RtlInitUnicodeString(&KeyValue, (PWSTR)ParentIdPrefixInformation->Data);
1428
1429 /* 3. Try to write the ParentIdPrefix to registry */
1430 Status = ZwSetValueKey(hKey,
1431 &ValueName,
1432 0,
1433 REG_SZ,
1434 (PVOID)KeyValue.Buffer,
1435 (wcslen(KeyValue.Buffer) + 1) * sizeof(WCHAR));
1436
1437 cleanup:
1438 if (NT_SUCCESS(Status))
1439 {
1440 /* Duplicate the string to return it */
1441 Status = RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, &KeyValue, ParentIdPrefix);
1442 }
1443 ExFreePool(ParentIdPrefixInformation);
1444 ExFreePool(KeyNameBuffer);
1445 if (hKey != INVALID_HANDLE_VALUE)
1446 ZwClose(hKey);
1447 return Status;
1448 }
1449
1450
1451 /*
1452 * IopActionInterrogateDeviceStack
1453 *
1454 * Retrieve information for all (direct) child nodes of a parent node.
1455 *
1456 * Parameters
1457 * DeviceNode
1458 * Pointer to device node.
1459 * Context
1460 * Pointer to parent node to retrieve child node information for.
1461 *
1462 * Remarks
1463 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1464 * when we reach a device node which is not a direct child of the device
1465 * node for which we retrieve information of child nodes for. Any errors
1466 * that occur is logged instead so that all child services have a chance
1467 * of being interrogated.
1468 */
1469
1470 NTSTATUS
1471 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode,
1472 PVOID Context)
1473 {
1474 IO_STATUS_BLOCK IoStatusBlock;
1475 PDEVICE_NODE ParentDeviceNode;
1476 WCHAR InstancePath[MAX_PATH];
1477 IO_STACK_LOCATION Stack;
1478 NTSTATUS Status;
1479 PWSTR KeyBuffer;
1480 PWSTR Ptr;
1481 USHORT Length;
1482 USHORT TotalLength;
1483 ULONG RequiredLength;
1484 LCID LocaleId;
1485 HANDLE InstanceKey = NULL;
1486 UNICODE_STRING ValueName;
1487 UNICODE_STRING ParentIdPrefix = { 0 };
1488 DEVICE_CAPABILITIES DeviceCapabilities;
1489
1490 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
1491 DPRINT("PDO 0x%p\n", DeviceNode->PhysicalDeviceObject);
1492
1493 ParentDeviceNode = (PDEVICE_NODE)Context;
1494
1495 /*
1496 * We are called for the parent too, but we don't need to do special
1497 * handling for this node
1498 */
1499
1500 if (DeviceNode == ParentDeviceNode)
1501 {
1502 DPRINT("Success\n");
1503 return STATUS_SUCCESS;
1504 }
1505
1506 /*
1507 * Make sure this device node is a direct child of the parent device node
1508 * that is given as an argument
1509 */
1510
1511 if (DeviceNode->Parent != ParentDeviceNode)
1512 {
1513 /* Stop the traversal immediately and indicate successful operation */
1514 DPRINT("Stop\n");
1515 return STATUS_UNSUCCESSFUL;
1516 }
1517
1518 /* Get Locale ID */
1519 Status = ZwQueryDefaultLocale(FALSE, &LocaleId);
1520 if (!NT_SUCCESS(Status))
1521 {
1522 DPRINT("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status);
1523 return Status;
1524 }
1525
1526 /*
1527 * FIXME: For critical errors, cleanup and disable device, but always
1528 * return STATUS_SUCCESS.
1529 */
1530
1531 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1532
1533 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
1534 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1535 &IoStatusBlock,
1536 IRP_MN_QUERY_ID,
1537 &Stack);
1538 if (NT_SUCCESS(Status))
1539 {
1540 /* Copy the device id string */
1541 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
1542
1543 /*
1544 * FIXME: Check for valid characters, if there is invalid characters
1545 * then bugcheck.
1546 */
1547 }
1548 else
1549 {
1550 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1551 }
1552
1553 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack\n");
1554
1555 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
1556 if (!NT_SUCCESS(Status))
1557 {
1558 DPRINT("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status);
1559 }
1560
1561 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCapabilities + 4);
1562
1563 if (!DeviceCapabilities.UniqueID)
1564 {
1565 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1566 DPRINT("Instance ID is not unique\n");
1567 Status = IopGetParentIdPrefix(DeviceNode, &ParentIdPrefix);
1568 if (!NT_SUCCESS(Status))
1569 {
1570 DPRINT("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status);
1571 }
1572 }
1573
1574 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1575
1576 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
1577 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1578 &IoStatusBlock,
1579 IRP_MN_QUERY_ID,
1580 &Stack);
1581 if (NT_SUCCESS(Status))
1582 {
1583 /* Append the instance id string */
1584 wcscat(InstancePath, L"\\");
1585 if (ParentIdPrefix.Length > 0)
1586 {
1587 /* Add information from parent bus device to InstancePath */
1588 wcscat(InstancePath, ParentIdPrefix.Buffer);
1589 if (IoStatusBlock.Information && *(PWSTR)IoStatusBlock.Information)
1590 wcscat(InstancePath, L"&");
1591 }
1592 if (IoStatusBlock.Information)
1593 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1594
1595 /*
1596 * FIXME: Check for valid characters, if there is invalid characters
1597 * then bugcheck
1598 */
1599 }
1600 else
1601 {
1602 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1603 }
1604 RtlFreeUnicodeString(&ParentIdPrefix);
1605
1606 if (!RtlCreateUnicodeString(&DeviceNode->InstancePath, InstancePath))
1607 {
1608 DPRINT("No resources\n");
1609 /* FIXME: Cleanup and disable device */
1610 }
1611
1612 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1613
1614 /*
1615 * Create registry key for the instance id, if it doesn't exist yet
1616 */
1617 KeyBuffer = ExAllocatePool(
1618 PagedPool,
1619 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
1620 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1621 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
1622 Status = IopCreateDeviceKeyPath(KeyBuffer, &InstanceKey);
1623 ExFreePool(KeyBuffer);
1624 if (!NT_SUCCESS(Status))
1625 {
1626 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1627 }
1628
1629
1630 {
1631 /* Set 'Capabilities' value */
1632 RtlInitUnicodeString(&ValueName, L"Capabilities");
1633 Status = ZwSetValueKey(InstanceKey,
1634 &ValueName,
1635 0,
1636 REG_DWORD,
1637 (PVOID)&DeviceNode->CapabilityFlags,
1638 sizeof(ULONG));
1639
1640 /* Set 'UINumber' value */
1641 if (DeviceCapabilities.UINumber != (ULONG)-1)
1642 {
1643 RtlInitUnicodeString(&ValueName, L"UINumber");
1644 Status = ZwSetValueKey(InstanceKey,
1645 &ValueName,
1646 0,
1647 REG_DWORD,
1648 &DeviceCapabilities.UINumber,
1649 sizeof(ULONG));
1650 }
1651 }
1652
1653 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1654
1655 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1656 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1657 &IoStatusBlock,
1658 IRP_MN_QUERY_ID,
1659 &Stack);
1660 if (NT_SUCCESS(Status))
1661 {
1662 /*
1663 * FIXME: Check for valid characters, if there is invalid characters
1664 * then bugcheck.
1665 */
1666 TotalLength = 0;
1667 Ptr = (PWSTR)IoStatusBlock.Information;
1668 DPRINT("Hardware IDs:\n");
1669 while (*Ptr)
1670 {
1671 DPRINT(" %S\n", Ptr);
1672 Length = wcslen(Ptr) + 1;
1673
1674 Ptr += Length;
1675 TotalLength += Length;
1676 }
1677 DPRINT("TotalLength: %hu\n", TotalLength);
1678 DPRINT("\n");
1679
1680 RtlInitUnicodeString(&ValueName, L"HardwareID");
1681 Status = ZwSetValueKey(InstanceKey,
1682 &ValueName,
1683 0,
1684 REG_MULTI_SZ,
1685 (PVOID)IoStatusBlock.Information,
1686 (TotalLength + 1) * sizeof(WCHAR));
1687 if (!NT_SUCCESS(Status))
1688 {
1689 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1690 }
1691 }
1692 else
1693 {
1694 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1695 }
1696
1697 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1698
1699 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1700 Status = IopInitiatePnpIrp(
1701 DeviceNode->PhysicalDeviceObject,
1702 &IoStatusBlock,
1703 IRP_MN_QUERY_ID,
1704 &Stack);
1705 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1706 {
1707 /*
1708 * FIXME: Check for valid characters, if there is invalid characters
1709 * then bugcheck.
1710 */
1711 TotalLength = 0;
1712 Ptr = (PWSTR)IoStatusBlock.Information;
1713 DPRINT("Compatible IDs:\n");
1714 while (*Ptr)
1715 {
1716 DPRINT(" %S\n", Ptr);
1717 Length = wcslen(Ptr) + 1;
1718
1719 Ptr += Length;
1720 TotalLength += Length;
1721 }
1722 DPRINT("TotalLength: %hu\n", TotalLength);
1723 DPRINT("\n");
1724
1725 RtlInitUnicodeString(&ValueName, L"CompatibleIDs");
1726 Status = ZwSetValueKey(InstanceKey,
1727 &ValueName,
1728 0,
1729 REG_MULTI_SZ,
1730 (PVOID)IoStatusBlock.Information,
1731 (TotalLength + 1) * sizeof(WCHAR));
1732 if (!NT_SUCCESS(Status))
1733 {
1734 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status);
1735 }
1736 }
1737 else
1738 {
1739 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1740 }
1741
1742
1743 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1744
1745 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1746 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1747 Status = IopInitiatePnpIrp(
1748 DeviceNode->PhysicalDeviceObject,
1749 &IoStatusBlock,
1750 IRP_MN_QUERY_DEVICE_TEXT,
1751 &Stack);
1752 /* This key is mandatory, so even if the Irp fails, we still write it */
1753 RtlInitUnicodeString(&ValueName, L"DeviceDesc");
1754 if (ZwQueryValueKey(InstanceKey, &ValueName, KeyValueBasicInformation, NULL, 0, &RequiredLength) == STATUS_OBJECT_NAME_NOT_FOUND)
1755 {
1756 if (NT_SUCCESS(IoStatusBlock.Status) &&
1757 IoStatusBlock.Information &&
1758 (*(PWSTR)IoStatusBlock.Information != 0))
1759 {
1760 /* This key is overriden when a driver is installed. Don't write the
1761 * new description if another one already exists */
1762 Status = ZwSetValueKey(InstanceKey,
1763 &ValueName,
1764 0,
1765 REG_SZ,
1766 (PVOID)IoStatusBlock.Information,
1767 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1768 }
1769 else
1770 {
1771 UNICODE_STRING DeviceDesc;
1772 RtlInitUnicodeString(&DeviceDesc, L"Unknown Device");
1773 DPRINT("Driver didn't return DeviceDesc (Status %x), so place unknown device there\n", Status);
1774
1775 Status = ZwSetValueKey(InstanceKey,
1776 &ValueName,
1777 0,
1778 REG_SZ,
1779 &DeviceDesc,
1780 (wcslen((PWSTR)&DeviceDesc) + 1) * sizeof(WCHAR));
1781
1782 if (!NT_SUCCESS(Status))
1783 {
1784 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status);
1785 }
1786
1787 }
1788 }
1789
1790 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1791
1792 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1793 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1794 Status = IopInitiatePnpIrp(
1795 DeviceNode->PhysicalDeviceObject,
1796 &IoStatusBlock,
1797 IRP_MN_QUERY_DEVICE_TEXT,
1798 &Stack);
1799 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1800 {
1801 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1802 RtlInitUnicodeString(&ValueName, L"LocationInformation");
1803 Status = ZwSetValueKey(InstanceKey,
1804 &ValueName,
1805 0,
1806 REG_SZ,
1807 (PVOID)IoStatusBlock.Information,
1808 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1809 if (!NT_SUCCESS(Status))
1810 {
1811 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1812 }
1813 }
1814 else
1815 {
1816 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1817 }
1818
1819 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1820
1821 Status = IopInitiatePnpIrp(
1822 DeviceNode->PhysicalDeviceObject,
1823 &IoStatusBlock,
1824 IRP_MN_QUERY_BUS_INFORMATION,
1825 NULL);
1826 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1827 {
1828 PPNP_BUS_INFORMATION BusInformation =
1829 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1830
1831 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1832 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1833 DeviceNode->ChildBusTypeIndex = IopGetBusTypeGuidIndex(&BusInformation->BusTypeGuid);
1834 ExFreePool(BusInformation);
1835 }
1836 else
1837 {
1838 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1839
1840 DeviceNode->ChildBusNumber = 0xFFFFFFF0;
1841 DeviceNode->ChildInterfaceType = InterfaceTypeUndefined;
1842 DeviceNode->ChildBusTypeIndex = -1;
1843 }
1844
1845 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1846
1847 Status = IopInitiatePnpIrp(
1848 DeviceNode->PhysicalDeviceObject,
1849 &IoStatusBlock,
1850 IRP_MN_QUERY_RESOURCES,
1851 NULL);
1852 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1853 {
1854 DeviceNode->BootResources =
1855 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1856 DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
1857 }
1858 else
1859 {
1860 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1861 DeviceNode->BootResources = NULL;
1862 }
1863
1864 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1865
1866 Status = IopInitiatePnpIrp(
1867 DeviceNode->PhysicalDeviceObject,
1868 &IoStatusBlock,
1869 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1870 NULL);
1871 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1872 {
1873 DeviceNode->ResourceRequirements =
1874 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1875 }
1876 else
1877 {
1878 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1879 DeviceNode->ResourceRequirements = NULL;
1880 }
1881
1882
1883 if (InstanceKey != NULL)
1884 {
1885 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1886 }
1887
1888 ZwClose(InstanceKey);
1889
1890 Status = IopAssignDeviceResources(DeviceNode);
1891 if (!NT_SUCCESS(Status))
1892 {
1893 DPRINT("IopAssignDeviceResources() failed (Status %x)\n", Status);
1894 }
1895
1896 DeviceNode->Flags |= DNF_PROCESSED;
1897
1898 /* Report the device to the user-mode pnp manager */
1899 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1900 &DeviceNode->InstancePath);
1901
1902 return STATUS_SUCCESS;
1903 }
1904
1905 /*
1906 * IopActionConfigureChildServices
1907 *
1908 * Retrieve configuration for all (direct) child nodes of a parent node.
1909 *
1910 * Parameters
1911 * DeviceNode
1912 * Pointer to device node.
1913 * Context
1914 * Pointer to parent node to retrieve child node configuration for.
1915 *
1916 * Remarks
1917 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1918 * when we reach a device node which is not a direct child of the device
1919 * node for which we configure child services for. Any errors that occur is
1920 * logged instead so that all child services have a chance of beeing
1921 * configured.
1922 */
1923
1924 NTSTATUS
1925 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode,
1926 PVOID Context)
1927 {
1928 RTL_QUERY_REGISTRY_TABLE QueryTable[3];
1929 PDEVICE_NODE ParentDeviceNode;
1930 PUNICODE_STRING Service;
1931 UNICODE_STRING ClassGUID;
1932 UNICODE_STRING NullString = RTL_CONSTANT_STRING(L"");
1933 NTSTATUS Status;
1934
1935 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
1936
1937 ParentDeviceNode = (PDEVICE_NODE)Context;
1938
1939 /*
1940 * We are called for the parent too, but we don't need to do special
1941 * handling for this node
1942 */
1943 if (DeviceNode == ParentDeviceNode)
1944 {
1945 DPRINT("Success\n");
1946 return STATUS_SUCCESS;
1947 }
1948
1949 /*
1950 * Make sure this device node is a direct child of the parent device node
1951 * that is given as an argument
1952 */
1953 if (DeviceNode->Parent != ParentDeviceNode)
1954 {
1955 /* Stop the traversal immediately and indicate successful operation */
1956 DPRINT("Stop\n");
1957 return STATUS_UNSUCCESSFUL;
1958 }
1959
1960 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
1961 {
1962 WCHAR RegKeyBuffer[MAX_PATH];
1963 UNICODE_STRING RegKey;
1964
1965 RegKey.Length = 0;
1966 RegKey.MaximumLength = sizeof(RegKeyBuffer);
1967 RegKey.Buffer = RegKeyBuffer;
1968
1969 /*
1970 * Retrieve configuration from Enum key
1971 */
1972
1973 Service = &DeviceNode->ServiceName;
1974
1975 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1976 RtlInitUnicodeString(Service, NULL);
1977 RtlInitUnicodeString(&ClassGUID, NULL);
1978
1979 QueryTable[0].Name = L"Service";
1980 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1981 QueryTable[0].EntryContext = Service;
1982
1983 QueryTable[1].Name = L"ClassGUID";
1984 QueryTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
1985 QueryTable[1].EntryContext = &ClassGUID;
1986 QueryTable[1].DefaultType = REG_SZ;
1987 QueryTable[1].DefaultData = &NullString;
1988 QueryTable[1].DefaultLength = 0;
1989
1990 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1991 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
1992
1993 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1994 RegKey.Buffer, QueryTable, NULL, NULL);
1995
1996 if (!NT_SUCCESS(Status))
1997 {
1998 DPRINT("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1999 /* FIXME: Log the error */
2000 CPRINT("Could not retrieve configuration for device %S (Status %x)\n",
2001 DeviceNode->InstancePath.Buffer, Status);
2002 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2003 return STATUS_SUCCESS;
2004 }
2005
2006 if (Service->Buffer == NULL)
2007 {
2008 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2009
2010 if (ClassGUID.Length != 0)
2011 {
2012 /* Device has a ClassGUID value, but no Service value.
2013 * Suppose it is using the NULL driver, so state the
2014 * device is started */
2015 DPRINT("%wZ is using NULL driver\n", &DeviceNode->InstancePath);
2016 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
2017 }
2018 return STATUS_SUCCESS;
2019 }
2020
2021 DPRINT("Got Service %S\n", Service->Buffer);
2022 }
2023
2024 return STATUS_SUCCESS;
2025 }
2026
2027 /*
2028 * IopActionInitChildServices
2029 *
2030 * Initialize the service for all (direct) child nodes of a parent node
2031 *
2032 * Parameters
2033 * DeviceNode
2034 * Pointer to device node.
2035 * Context
2036 * Pointer to parent node to initialize child node services for.
2037 * BootDrivers
2038 * Load only driver marked as boot start.
2039 *
2040 * Remarks
2041 * If the driver image for a service is not loaded and initialized
2042 * it is done here too. We only return a status code indicating an
2043 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
2044 * not a direct child of the device node for which we initialize
2045 * child services for. Any errors that occur is logged instead so
2046 * that all child services have a chance of being initialized.
2047 */
2048
2049 NTSTATUS
2050 IopActionInitChildServices(PDEVICE_NODE DeviceNode,
2051 PVOID Context,
2052 BOOLEAN BootDrivers)
2053 {
2054 PDEVICE_NODE ParentDeviceNode;
2055 NTSTATUS Status;
2056
2057 DPRINT("IopActionInitChildServices(%p, %p, %d)\n", DeviceNode, Context,
2058 BootDrivers);
2059
2060 ParentDeviceNode = (PDEVICE_NODE)Context;
2061
2062 /*
2063 * We are called for the parent too, but we don't need to do special
2064 * handling for this node
2065 */
2066 if (DeviceNode == ParentDeviceNode)
2067 {
2068 DPRINT("Success\n");
2069 return STATUS_SUCCESS;
2070 }
2071
2072 /*
2073 * Make sure this device node is a direct child of the parent device node
2074 * that is given as an argument
2075 */
2076 #if 0
2077 if (DeviceNode->Parent != ParentDeviceNode)
2078 {
2079 /*
2080 * Stop the traversal immediately and indicate unsuccessful operation
2081 */
2082 DPRINT("Stop\n");
2083 return STATUS_UNSUCCESSFUL;
2084 }
2085 #endif
2086
2087 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED) &&
2088 !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
2089 !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
2090 {
2091 PLDR_DATA_TABLE_ENTRY ModuleObject;
2092 PDRIVER_OBJECT DriverObject;
2093
2094 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
2095 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
2096 {
2097 if (Status != STATUS_IMAGE_ALREADY_LOADED)
2098 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
2099 &DeviceNode->ServiceName, FALSE, &DriverObject);
2100 else
2101 {
2102 /* get existing DriverObject pointer */
2103 Status = IopGetDriverObject(
2104 &DriverObject,
2105 &DeviceNode->ServiceName,
2106 FALSE);
2107 }
2108 if (NT_SUCCESS(Status))
2109 {
2110 /* Attach lower level filter drivers. */
2111 IopAttachFilterDrivers(DeviceNode, TRUE);
2112 /* Initialize the function driver for the device node */
2113 Status = IopInitializeDevice(DeviceNode, DriverObject);
2114 if (NT_SUCCESS(Status))
2115 {
2116 /* Attach upper level filter drivers. */
2117 IopAttachFilterDrivers(DeviceNode, FALSE);
2118 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
2119
2120 Status = IopStartDevice(DeviceNode);
2121 }
2122 }
2123 }
2124 else
2125 {
2126 /*
2127 * Don't disable when trying to load only boot drivers
2128 */
2129 if (!BootDrivers)
2130 {
2131 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2132 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
2133 }
2134 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2135 CPRINT("Initialization of service %S failed (Status %x)\n",
2136 DeviceNode->ServiceName.Buffer, Status);
2137 }
2138 } else
2139 {
2140 DPRINT("Service %S is disabled or already initialized\n",
2141 DeviceNode->ServiceName.Buffer);
2142 }
2143
2144 return STATUS_SUCCESS;
2145 }
2146
2147 /*
2148 * IopActionInitAllServices
2149 *
2150 * Initialize the service for all (direct) child nodes of a parent node. This
2151 * function just calls IopActionInitChildServices with BootDrivers = FALSE.
2152 */
2153
2154 NTSTATUS
2155 IopActionInitAllServices(PDEVICE_NODE DeviceNode,
2156 PVOID Context)
2157 {
2158 return IopActionInitChildServices(DeviceNode, Context, FALSE);
2159 }
2160
2161 /*
2162 * IopActionInitBootServices
2163 *
2164 * Initialize the boot start services for all (direct) child nodes of a
2165 * parent node. This function just calls IopActionInitChildServices with
2166 * BootDrivers = TRUE.
2167 */
2168 NTSTATUS
2169 IopActionInitBootServices(PDEVICE_NODE DeviceNode,
2170 PVOID Context)
2171 {
2172 return IopActionInitChildServices(DeviceNode, Context, TRUE);
2173 }
2174
2175 /*
2176 * IopInitializePnpServices
2177 *
2178 * Initialize services for discovered children
2179 *
2180 * Parameters
2181 * DeviceNode
2182 * Top device node to start initializing services.
2183 *
2184 * BootDrivers
2185 * When set to TRUE, only drivers marked as boot start will
2186 * be loaded. Otherwise, all drivers will be loaded.
2187 *
2188 * Return Value
2189 * Status
2190 */
2191 NTSTATUS
2192 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode,
2193 IN BOOLEAN BootDrivers)
2194 {
2195 DEVICETREE_TRAVERSE_CONTEXT Context;
2196
2197 DPRINT("IopInitializePnpServices(%p, %d)\n", DeviceNode, BootDrivers);
2198
2199 if (BootDrivers)
2200 {
2201 IopInitDeviceTreeTraverseContext(
2202 &Context,
2203 DeviceNode,
2204 IopActionInitBootServices,
2205 DeviceNode);
2206 }
2207 else
2208 {
2209 IopInitDeviceTreeTraverseContext(
2210 &Context,
2211 DeviceNode,
2212 IopActionInitAllServices,
2213 DeviceNode);
2214 }
2215
2216 return IopTraverseDeviceTree(&Context);
2217 }
2218
2219
2220 NTSTATUS
2221 IopInvalidateDeviceRelations(IN PDEVICE_NODE DeviceNode,
2222 IN DEVICE_RELATION_TYPE Type)
2223 {
2224 DEVICETREE_TRAVERSE_CONTEXT Context;
2225 PDEVICE_RELATIONS DeviceRelations;
2226 IO_STATUS_BLOCK IoStatusBlock;
2227 PDEVICE_NODE ChildDeviceNode;
2228 IO_STACK_LOCATION Stack;
2229 BOOL BootDrivers;
2230 OBJECT_ATTRIBUTES ObjectAttributes;
2231 UNICODE_STRING LinkName;
2232 HANDLE Handle;
2233 NTSTATUS Status;
2234 ULONG i;
2235
2236 DPRINT("DeviceNode 0x%p\n", DeviceNode);
2237
2238 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2239
2240 Stack.Parameters.QueryDeviceRelations.Type = Type/*BusRelations*/;
2241
2242 Status = IopInitiatePnpIrp(
2243 DeviceNode->PhysicalDeviceObject,
2244 &IoStatusBlock,
2245 IRP_MN_QUERY_DEVICE_RELATIONS,
2246 &Stack);
2247 if (!NT_SUCCESS(Status))
2248 {
2249 DPRINT("IopInitiatePnpIrp() failed\n");
2250 return Status;
2251 }
2252
2253 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
2254
2255 if ((!DeviceRelations) || (DeviceRelations->Count <= 0))
2256 {
2257 DPRINT("No PDOs\n");
2258 if (DeviceRelations)
2259 {
2260 ExFreePool(DeviceRelations);
2261 }
2262 return STATUS_SUCCESS;
2263 }
2264
2265 DPRINT("Got %d PDOs\n", DeviceRelations->Count);
2266
2267 /*
2268 * Create device nodes for all discovered devices
2269 */
2270
2271 for (i = 0; i < DeviceRelations->Count; i++)
2272 {
2273 Status = IopCreateDeviceNode(
2274 DeviceNode,
2275 DeviceRelations->Objects[i],
2276 &ChildDeviceNode);
2277 DeviceNode->Flags |= DNF_ENUMERATED;
2278 if (!NT_SUCCESS(Status))
2279 {
2280 DPRINT("No resources\n");
2281 for (i = 0; i < DeviceRelations->Count; i++)
2282 ObDereferenceObject(DeviceRelations->Objects[i]);
2283 ExFreePool(DeviceRelations);
2284 return STATUS_INSUFFICIENT_RESOURCES;
2285 }
2286 }
2287 ExFreePool(DeviceRelations);
2288
2289 /*
2290 * Retrieve information about all discovered children from the bus driver
2291 */
2292
2293 IopInitDeviceTreeTraverseContext(
2294 &Context,
2295 DeviceNode,
2296 IopActionInterrogateDeviceStack,
2297 DeviceNode);
2298
2299 Status = IopTraverseDeviceTree(&Context);
2300 if (!NT_SUCCESS(Status))
2301 {
2302 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2303 return Status;
2304 }
2305
2306 /*
2307 * Retrieve configuration from the registry for discovered children
2308 */
2309
2310 IopInitDeviceTreeTraverseContext(
2311 &Context,
2312 DeviceNode,
2313 IopActionConfigureChildServices,
2314 DeviceNode);
2315
2316 Status = IopTraverseDeviceTree(&Context);
2317 if (!NT_SUCCESS(Status))
2318 {
2319 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2320 return Status;
2321 }
2322
2323 /*
2324 * Get the state of the system boot. If the \\SystemRoot link isn't
2325 * created yet, we will assume that it's possible to load only boot
2326 * drivers.
2327 */
2328
2329 RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
2330
2331 InitializeObjectAttributes(
2332 &ObjectAttributes,
2333 &LinkName,
2334 0,
2335 NULL,
2336 NULL);
2337
2338 Status = ZwOpenFile(
2339 &Handle,
2340 FILE_ALL_ACCESS,
2341 &ObjectAttributes,
2342 &IoStatusBlock,
2343 0,
2344 0);
2345 if(NT_SUCCESS(Status))
2346 {
2347 BootDrivers = FALSE;
2348 ZwClose(Handle);
2349 }
2350 else
2351 BootDrivers = TRUE;
2352
2353 /*
2354 * Initialize services for discovered children. Only boot drivers will
2355 * be loaded from boot driver!
2356 */
2357
2358 Status = IopInitializePnpServices(DeviceNode, BootDrivers);
2359 if (!NT_SUCCESS(Status))
2360 {
2361 DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
2362 return Status;
2363 }
2364
2365 return STATUS_SUCCESS;
2366 }
2367
2368
2369 static
2370 NTSTATUS
2371 INIT_FUNCTION
2372 IopSetRootDeviceInstanceData(PDEVICE_NODE DeviceNode)
2373 {
2374 #if 0
2375 PWSTR KeyBuffer;
2376 HANDLE InstanceKey = NULL;
2377 NTSTATUS Status;
2378
2379 /* Create registry key for the instance id, if it doesn't exist yet */
2380 KeyBuffer = ExAllocatePool(PagedPool,
2381 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
2382 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2383 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
2384 Status = IopCreateDeviceKeyPath(KeyBuffer,
2385 &InstanceKey);
2386 ExFreePool(KeyBuffer);
2387 if (!NT_SUCCESS(Status))
2388 {
2389 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
2390 return Status;
2391 }
2392
2393 /* FIXME: Set 'ConfigFlags' value */
2394
2395 ZwClose(InstanceKey);
2396
2397 return Status;
2398 #endif
2399 return STATUS_SUCCESS;
2400 }
2401
2402
2403 VOID INIT_FUNCTION
2404 PnpInit(VOID)
2405 {
2406 PDEVICE_OBJECT Pdo;
2407 NTSTATUS Status;
2408
2409 DPRINT("PnpInit()\n");
2410
2411 KeInitializeSpinLock(&IopDeviceTreeLock);
2412
2413 /* Initialize the Bus Type GUID List */
2414 IopBusTypeGuidList = ExAllocatePool(PagedPool, sizeof(IO_BUS_TYPE_GUID_LIST));
2415 RtlZeroMemory(IopBusTypeGuidList, sizeof(IO_BUS_TYPE_GUID_LIST));
2416 ExInitializeFastMutex(&IopBusTypeGuidList->Lock);
2417
2418 /* Initialize PnP-Event notification support */
2419 Status = IopInitPlugPlayEvents();
2420 if (!NT_SUCCESS(Status))
2421 {
2422 CPRINT("IopInitPlugPlayEvents() failed\n");
2423 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2424 }
2425
2426 /*
2427 * Create root device node
2428 */
2429
2430 Status = IopCreateDriverObject(&IopRootDriverObject, NULL, 0, FALSE, NULL, 0);
2431 if (!NT_SUCCESS(Status))
2432 {
2433 CPRINT("IoCreateDriverObject() failed\n");
2434 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2435 }
2436
2437 Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
2438 0, FALSE, &Pdo);
2439 if (!NT_SUCCESS(Status))
2440 {
2441 CPRINT("IoCreateDevice() failed\n");
2442 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2443 }
2444
2445 Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
2446 if (!NT_SUCCESS(Status))
2447 {
2448 CPRINT("Insufficient resources\n");
2449 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2450 }
2451
2452 if (!RtlCreateUnicodeString(&IopRootDeviceNode->InstancePath,
2453 L"HTREE\\ROOT\\0"))
2454 {
2455 CPRINT("Failed to create the instance path!\n");
2456 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, STATUS_UNSUCCESSFUL, 0, 0, 0);
2457 }
2458
2459 /* Report the device to the user-mode pnp manager */
2460 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
2461 &IopRootDeviceNode->InstancePath);
2462
2463 IopRootDeviceNode->PhysicalDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
2464 PnpRootDriverEntry(IopRootDriverObject, NULL);
2465 IopRootDriverObject->DriverExtension->AddDevice(
2466 IopRootDriverObject,
2467 IopRootDeviceNode->PhysicalDeviceObject);
2468 }
2469
2470
2471 VOID INIT_FUNCTION
2472 PnpInit2(VOID)
2473 {
2474 NTSTATUS Status;
2475
2476 /* Set root device instance data */
2477 Status = IopSetRootDeviceInstanceData(IopRootDeviceNode);
2478 if (!NT_SUCCESS(Status))
2479 {
2480 CPRINT("Failed to set instance data\n");
2481 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2482 }
2483 }
2484
2485 /* EOF */