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