2 * PROJECT: ReactOS Kernel
3 * COPYRIGHT: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/pnpmgr.c
5 * PURPOSE: Initializes the PnP manager
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Copyright 2007 Hervé Poussineau (hpoussin@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* GLOBALS *******************************************************************/
18 PDEVICE_NODE IopRootDeviceNode
;
19 KSPIN_LOCK IopDeviceTreeLock
;
20 ERESOURCE PpRegistryDeviceResource
;
21 KGUARDED_MUTEX PpDeviceReferenceTableLock
;
22 RTL_AVL_TABLE PpDeviceReferenceTable
;
24 extern ULONG ExpInitializationPhase
;
25 extern BOOLEAN ExpInTextModeSetup
;
26 extern BOOLEAN PnpSystemInit
;
28 /* DATA **********************************************************************/
30 PDRIVER_OBJECT IopRootDriverObject
;
31 PIO_BUS_TYPE_GUID_LIST PnpBusTypeGuidList
= NULL
;
33 typedef struct _INVALIDATE_DEVICE_RELATION_DATA
35 PDEVICE_OBJECT DeviceObject
;
36 DEVICE_RELATION_TYPE Type
;
37 PIO_WORKITEM WorkItem
;
38 } INVALIDATE_DEVICE_RELATION_DATA
, *PINVALIDATE_DEVICE_RELATION_DATA
;
40 /* FUNCTIONS *****************************************************************/
43 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
44 IN ULONG CreateOptions
,
48 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
);
51 IopPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
, BOOLEAN Force
);
55 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject
)
57 return ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
62 IopInitializeDevice(PDEVICE_NODE DeviceNode
,
63 PDRIVER_OBJECT DriverObject
)
70 /* Special case for bus driven devices */
71 DeviceNode
->Flags
|= DNF_ADDED
;
72 return STATUS_SUCCESS
;
75 if (!DriverObject
->DriverExtension
->AddDevice
)
77 DeviceNode
->Flags
|= DNF_LEGACY_DRIVER
;
80 if (DeviceNode
->Flags
& DNF_LEGACY_DRIVER
)
82 DeviceNode
->Flags
|= DNF_ADDED
+ DNF_STARTED
;
83 return STATUS_SUCCESS
;
86 /* This is a Plug and Play driver */
87 DPRINT("Plug and Play driver found\n");
88 ASSERT(DeviceNode
->PhysicalDeviceObject
);
90 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
91 &DriverObject
->DriverName
,
92 &DeviceNode
->InstancePath
);
93 Status
= DriverObject
->DriverExtension
->AddDevice(
94 DriverObject
, DeviceNode
->PhysicalDeviceObject
);
95 if (!NT_SUCCESS(Status
))
97 DPRINT1("%wZ->AddDevice(%wZ) failed with status 0x%x\n",
98 &DriverObject
->DriverName
,
99 &DeviceNode
->InstancePath
,
101 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
105 Fdo
= IoGetAttachedDeviceReference(DeviceNode
->PhysicalDeviceObject
);
107 /* Check if we have a ACPI device (needed for power management) */
108 if (Fdo
->DeviceType
== FILE_DEVICE_ACPI
)
110 static BOOLEAN SystemPowerDeviceNodeCreated
= FALSE
;
112 /* There can be only one system power device */
113 if (!SystemPowerDeviceNodeCreated
)
115 PopSystemPowerDeviceNode
= DeviceNode
;
116 ObReferenceObject(PopSystemPowerDeviceNode
->PhysicalDeviceObject
);
117 SystemPowerDeviceNodeCreated
= TRUE
;
121 ObDereferenceObject(Fdo
);
123 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
125 return STATUS_SUCCESS
;
131 IopSendEject(IN PDEVICE_OBJECT DeviceObject
)
133 IO_STACK_LOCATION Stack
;
136 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
137 Stack
.MajorFunction
= IRP_MJ_PNP
;
138 Stack
.MinorFunction
= IRP_MN_EJECT
;
140 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
146 IopSendSurpriseRemoval(IN PDEVICE_OBJECT DeviceObject
)
148 IO_STACK_LOCATION Stack
;
151 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
152 Stack
.MajorFunction
= IRP_MJ_PNP
;
153 Stack
.MinorFunction
= IRP_MN_SURPRISE_REMOVAL
;
155 /* Drivers should never fail a IRP_MN_SURPRISE_REMOVAL request */
156 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
162 IopQueryRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
164 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
165 IO_STACK_LOCATION Stack
;
171 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVE_PENDING
,
172 &DeviceNode
->InstancePath
);
174 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
175 Stack
.MajorFunction
= IRP_MJ_PNP
;
176 Stack
.MinorFunction
= IRP_MN_QUERY_REMOVE_DEVICE
;
178 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
180 IopNotifyPlugPlayNotification(DeviceObject
,
181 EventCategoryTargetDeviceChange
,
182 &GUID_TARGET_DEVICE_QUERY_REMOVE
,
186 if (!NT_SUCCESS(Status
))
188 DPRINT1("Removal vetoed by %wZ\n", &DeviceNode
->InstancePath
);
189 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVAL_VETOED
,
190 &DeviceNode
->InstancePath
);
199 IopQueryStopDevice(IN PDEVICE_OBJECT DeviceObject
)
201 IO_STACK_LOCATION Stack
;
204 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
205 Stack
.MajorFunction
= IRP_MJ_PNP
;
206 Stack
.MinorFunction
= IRP_MN_QUERY_STOP_DEVICE
;
208 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
214 IopSendRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
216 IO_STACK_LOCATION Stack
;
219 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
220 Stack
.MajorFunction
= IRP_MJ_PNP
;
221 Stack
.MinorFunction
= IRP_MN_REMOVE_DEVICE
;
223 /* Drivers should never fail a IRP_MN_REMOVE_DEVICE request */
224 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
226 IopNotifyPlugPlayNotification(DeviceObject
,
227 EventCategoryTargetDeviceChange
,
228 &GUID_TARGET_DEVICE_REMOVE_COMPLETE
,
236 IopCancelRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
238 IO_STACK_LOCATION Stack
;
241 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
242 Stack
.MajorFunction
= IRP_MJ_PNP
;
243 Stack
.MinorFunction
= IRP_MN_CANCEL_REMOVE_DEVICE
;
245 /* Drivers should never fail a IRP_MN_CANCEL_REMOVE_DEVICE request */
246 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
248 IopNotifyPlugPlayNotification(DeviceObject
,
249 EventCategoryTargetDeviceChange
,
250 &GUID_TARGET_DEVICE_REMOVE_CANCELLED
,
258 IopSendStopDevice(IN PDEVICE_OBJECT DeviceObject
)
260 IO_STACK_LOCATION Stack
;
263 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
264 Stack
.MajorFunction
= IRP_MJ_PNP
;
265 Stack
.MinorFunction
= IRP_MN_STOP_DEVICE
;
267 /* Drivers should never fail a IRP_MN_STOP_DEVICE request */
268 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
273 IopStartDevice2(IN PDEVICE_OBJECT DeviceObject
)
275 IO_STACK_LOCATION Stack
;
276 PDEVICE_NODE DeviceNode
;
279 DEVICE_CAPABILITIES DeviceCapabilities
;
281 /* Get the device node */
282 DeviceNode
= IopGetDeviceNode(DeviceObject
);
284 ASSERT(!(DeviceNode
->Flags
& DNF_DISABLED
));
286 /* Build the I/O stack locaiton */
287 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
288 Stack
.MajorFunction
= IRP_MJ_PNP
;
289 Stack
.MinorFunction
= IRP_MN_START_DEVICE
;
291 Stack
.Parameters
.StartDevice
.AllocatedResources
=
292 DeviceNode
->ResourceList
;
293 Stack
.Parameters
.StartDevice
.AllocatedResourcesTranslated
=
294 DeviceNode
->ResourceListTranslated
;
297 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
298 if (!NT_SUCCESS(Status
))
300 /* Send an IRP_MN_REMOVE_DEVICE request */
301 IopRemoveDevice(DeviceNode
);
303 /* Set the appropriate flag */
304 DeviceNode
->Flags
|= DNF_START_FAILED
;
306 DPRINT1("Warning: PnP Start failed (%wZ) [Status: 0x%x]\n", &DeviceNode
->InstancePath
, Status
);
310 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after start)\n");
312 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
313 if (!NT_SUCCESS(Status
))
315 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
318 /* Invalidate device state so IRP_MN_QUERY_PNP_DEVICE_STATE is sent */
319 IoInvalidateDeviceState(DeviceObject
);
321 /* Otherwise, mark us as started */
322 DeviceNode
->Flags
|= DNF_STARTED
;
323 DeviceNode
->Flags
&= ~DNF_STOPPED
;
325 /* We now need enumeration */
326 DeviceNode
->Flags
|= DNF_NEED_ENUMERATION_ONLY
;
331 IopStartAndEnumerateDevice(IN PDEVICE_NODE DeviceNode
)
333 PDEVICE_OBJECT DeviceObject
;
338 ASSERT((DeviceNode
->Flags
& DNF_ADDED
));
339 ASSERT((DeviceNode
->Flags
& (DNF_RESOURCE_ASSIGNED
|
340 DNF_RESOURCE_REPORTED
|
341 DNF_NO_RESOURCE_REQUIRED
)));
343 /* Get the device object */
344 DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
346 /* Check if we're not started yet */
347 if (!(DeviceNode
->Flags
& DNF_STARTED
))
350 IopStartDevice2(DeviceObject
);
353 /* Do we need to query IDs? This happens in the case of manual reporting */
355 if (DeviceNode
->Flags
& DNF_NEED_QUERY_IDS
)
357 DPRINT1("Warning: Device node has DNF_NEED_QUERY_IDS\n");
358 /* And that case shouldn't happen yet */
363 /* Make sure we're started, and check if we need enumeration */
364 if ((DeviceNode
->Flags
& DNF_STARTED
) &&
365 (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
))
368 IoSynchronousInvalidateDeviceRelations(DeviceObject
, BusRelations
);
369 Status
= STATUS_SUCCESS
;
374 Status
= STATUS_SUCCESS
;
383 PDEVICE_NODE DeviceNode
)
387 DPRINT("Stopping device: %wZ\n", &DeviceNode
->InstancePath
);
389 Status
= IopQueryStopDevice(DeviceNode
->PhysicalDeviceObject
);
390 if (NT_SUCCESS(Status
))
392 IopSendStopDevice(DeviceNode
->PhysicalDeviceObject
);
394 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
395 DeviceNode
->Flags
|= DNF_STOPPED
;
397 return STATUS_SUCCESS
;
405 PDEVICE_NODE DeviceNode
)
408 HANDLE InstanceHandle
= INVALID_HANDLE_VALUE
, ControlHandle
= INVALID_HANDLE_VALUE
;
409 UNICODE_STRING KeyName
;
410 OBJECT_ATTRIBUTES ObjectAttributes
;
412 if (DeviceNode
->Flags
& DNF_DISABLED
)
413 return STATUS_SUCCESS
;
415 Status
= IopAssignDeviceResources(DeviceNode
);
416 if (!NT_SUCCESS(Status
))
420 IopStartAndEnumerateDevice(DeviceNode
);
422 /* FIX: Should be done in new device instance code */
423 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceHandle
);
424 if (!NT_SUCCESS(Status
))
427 /* FIX: Should be done in IoXxxPrepareDriverLoading */
429 RtlInitUnicodeString(&KeyName
, L
"Control");
430 InitializeObjectAttributes(&ObjectAttributes
,
432 OBJ_CASE_INSENSITIVE
,
435 Status
= ZwCreateKey(&ControlHandle
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
436 if (!NT_SUCCESS(Status
))
439 RtlInitUnicodeString(&KeyName
, L
"ActiveService");
440 Status
= ZwSetValueKey(ControlHandle
, &KeyName
, 0, REG_SZ
, DeviceNode
->ServiceName
.Buffer
, DeviceNode
->ServiceName
.Length
);
444 if (ControlHandle
!= INVALID_HANDLE_VALUE
)
445 ZwClose(ControlHandle
);
447 if (InstanceHandle
!= INVALID_HANDLE_VALUE
)
448 ZwClose(InstanceHandle
);
455 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode
,
456 PDEVICE_CAPABILITIES DeviceCaps
)
458 IO_STATUS_BLOCK StatusBlock
;
459 IO_STACK_LOCATION Stack
;
462 UNICODE_STRING ValueName
;
464 /* Set up the Header */
465 RtlZeroMemory(DeviceCaps
, sizeof(DEVICE_CAPABILITIES
));
466 DeviceCaps
->Size
= sizeof(DEVICE_CAPABILITIES
);
467 DeviceCaps
->Version
= 1;
468 DeviceCaps
->Address
= -1;
469 DeviceCaps
->UINumber
= -1;
471 /* Set up the Stack */
472 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
473 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= DeviceCaps
;
476 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
478 IRP_MN_QUERY_CAPABILITIES
,
480 if (!NT_SUCCESS(Status
))
482 DPRINT1("IRP_MN_QUERY_CAPABILITIES failed with status 0x%x\n", Status
);
486 DeviceNode
->CapabilityFlags
= *(PULONG
)((ULONG_PTR
)&DeviceCaps
->Version
+ sizeof(DeviceCaps
->Version
));
488 if (DeviceCaps
->NoDisplayInUI
)
489 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
491 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
493 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
494 if (NT_SUCCESS(Status
))
496 /* Set 'Capabilities' value */
497 RtlInitUnicodeString(&ValueName
, L
"Capabilities");
498 Status
= ZwSetValueKey(InstanceKey
,
502 (PVOID
)&DeviceNode
->CapabilityFlags
,
505 /* Set 'UINumber' value */
506 if (DeviceCaps
->UINumber
!= MAXULONG
)
508 RtlInitUnicodeString(&ValueName
, L
"UINumber");
509 Status
= ZwSetValueKey(InstanceKey
,
513 &DeviceCaps
->UINumber
,
522 IopAsynchronousInvalidateDeviceRelations(
523 IN PDEVICE_OBJECT DeviceObject
,
524 IN PVOID InvalidateContext
)
526 PINVALIDATE_DEVICE_RELATION_DATA Data
= InvalidateContext
;
528 IoSynchronousInvalidateDeviceRelations(
532 ObDereferenceObject(Data
->DeviceObject
);
533 IoFreeWorkItem(Data
->WorkItem
);
538 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT
*DeviceObject
)
542 if (PopSystemPowerDeviceNode
)
544 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
545 *DeviceObject
= PopSystemPowerDeviceNode
->PhysicalDeviceObject
;
546 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
548 return STATUS_SUCCESS
;
551 return STATUS_UNSUCCESSFUL
;
556 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid
)
558 USHORT i
= 0, FoundIndex
= 0xFFFF;
562 /* Acquire the lock */
563 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
565 /* Loop all entries */
566 while (i
< PnpBusTypeGuidList
->GuidCount
)
568 /* Try to find a match */
569 if (RtlCompareMemory(BusTypeGuid
,
570 &PnpBusTypeGuidList
->Guids
[i
],
571 sizeof(GUID
)) == sizeof(GUID
))
580 /* Check if we have to grow the list */
581 if (PnpBusTypeGuidList
->GuidCount
)
583 /* Calculate the new size */
584 NewSize
= sizeof(IO_BUS_TYPE_GUID_LIST
) +
585 (sizeof(GUID
) * PnpBusTypeGuidList
->GuidCount
);
587 /* Allocate the new copy */
588 NewList
= ExAllocatePool(PagedPool
, NewSize
);
592 ExFreePool(PnpBusTypeGuidList
);
596 /* Now copy them, decrease the size too */
597 NewSize
-= sizeof(GUID
);
598 RtlCopyMemory(NewList
, PnpBusTypeGuidList
, NewSize
);
600 /* Free the old list */
601 ExFreePool(PnpBusTypeGuidList
);
603 /* Use the new buffer */
604 PnpBusTypeGuidList
= NewList
;
607 /* Copy the new GUID */
608 RtlCopyMemory(&PnpBusTypeGuidList
->Guids
[PnpBusTypeGuidList
->GuidCount
],
612 /* The new entry is the index */
613 FoundIndex
= (USHORT
)PnpBusTypeGuidList
->GuidCount
;
614 PnpBusTypeGuidList
->GuidCount
++;
617 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
623 * Creates a device node
626 * ParentNode = Pointer to parent device node
627 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
628 * to have the root device node create one
629 * (eg. for legacy drivers)
630 * DeviceNode = Pointer to storage for created device node
636 IopCreateDeviceNode(PDEVICE_NODE ParentNode
,
637 PDEVICE_OBJECT PhysicalDeviceObject
,
638 PUNICODE_STRING ServiceName
,
639 PDEVICE_NODE
*DeviceNode
)
644 UNICODE_STRING FullServiceName
;
645 UNICODE_STRING LegacyPrefix
= RTL_CONSTANT_STRING(L
"LEGACY_");
646 UNICODE_STRING UnknownDeviceName
= RTL_CONSTANT_STRING(L
"UNKNOWN");
647 UNICODE_STRING KeyName
, ClassName
;
648 PUNICODE_STRING ServiceName1
;
651 UNICODE_STRING ClassGUID
;
653 HANDLE InstanceHandle
;
655 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
656 ParentNode
, PhysicalDeviceObject
, ServiceName
);
658 Node
= (PDEVICE_NODE
)ExAllocatePool(NonPagedPool
, sizeof(DEVICE_NODE
));
661 return STATUS_INSUFFICIENT_RESOURCES
;
664 RtlZeroMemory(Node
, sizeof(DEVICE_NODE
));
667 ServiceName1
= &UnknownDeviceName
;
669 ServiceName1
= ServiceName
;
671 if (!PhysicalDeviceObject
)
673 FullServiceName
.MaximumLength
= LegacyPrefix
.Length
+ ServiceName1
->Length
;
674 FullServiceName
.Length
= 0;
675 FullServiceName
.Buffer
= ExAllocatePool(PagedPool
, FullServiceName
.MaximumLength
);
676 if (!FullServiceName
.Buffer
)
679 return STATUS_INSUFFICIENT_RESOURCES
;
682 RtlAppendUnicodeStringToString(&FullServiceName
, &LegacyPrefix
);
683 RtlAppendUnicodeStringToString(&FullServiceName
, ServiceName1
);
685 Status
= PnpRootCreateDevice(&FullServiceName
, NULL
, &PhysicalDeviceObject
, &Node
->InstancePath
);
686 if (!NT_SUCCESS(Status
))
688 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status
);
693 /* Create the device key for legacy drivers */
694 Status
= IopCreateDeviceKeyPath(&Node
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceHandle
);
695 if (!NT_SUCCESS(Status
))
697 ZwClose(InstanceHandle
);
699 ExFreePool(FullServiceName
.Buffer
);
703 Node
->ServiceName
.Buffer
= ExAllocatePool(PagedPool
, ServiceName1
->Length
);
704 if (!Node
->ServiceName
.Buffer
)
706 ZwClose(InstanceHandle
);
708 ExFreePool(FullServiceName
.Buffer
);
712 Node
->ServiceName
.MaximumLength
= ServiceName1
->Length
;
713 Node
->ServiceName
.Length
= 0;
715 RtlAppendUnicodeStringToString(&Node
->ServiceName
, ServiceName1
);
719 RtlInitUnicodeString(&KeyName
, L
"Service");
720 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ServiceName
->Buffer
, ServiceName
->Length
);
723 if (NT_SUCCESS(Status
))
725 RtlInitUnicodeString(&KeyName
, L
"Legacy");
728 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_DWORD
, &LegacyValue
, sizeof(LegacyValue
));
729 if (NT_SUCCESS(Status
))
731 RtlInitUnicodeString(&KeyName
, L
"Class");
733 RtlInitUnicodeString(&ClassName
, L
"LegacyDriver");
734 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassName
.Buffer
, ClassName
.Length
);
736 if (NT_SUCCESS(Status
))
738 RtlInitUnicodeString(&KeyName
, L
"ClassGUID");
740 RtlInitUnicodeString(&ClassGUID
, L
"{8ECC055D-047F-11D1-A537-0000F8753ED1}");
741 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassGUID
.Buffer
, ClassGUID
.Length
);
747 ZwClose(InstanceHandle
);
748 ExFreePool(FullServiceName
.Buffer
);
750 if (!NT_SUCCESS(Status
))
756 IopDeviceNodeSetFlag(Node
, DNF_LEGACY_DRIVER
);
757 IopDeviceNodeSetFlag(Node
, DNF_PROCESSED
);
758 IopDeviceNodeSetFlag(Node
, DNF_ADDED
);
759 IopDeviceNodeSetFlag(Node
, DNF_STARTED
);
762 Node
->PhysicalDeviceObject
= PhysicalDeviceObject
;
764 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= Node
;
768 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
769 Node
->Parent
= ParentNode
;
770 Node
->Sibling
= ParentNode
->Child
;
771 ParentNode
->Child
= Node
;
772 if (ParentNode
->LastChild
== NULL
)
773 ParentNode
->LastChild
= Node
;
774 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
775 Node
->Level
= ParentNode
->Level
+ 1;
778 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
782 return STATUS_SUCCESS
;
786 IopFreeDeviceNode(PDEVICE_NODE DeviceNode
)
789 PDEVICE_NODE PrevSibling
= NULL
;
791 /* All children must be deleted before a parent is deleted */
792 ASSERT(!DeviceNode
->Child
);
794 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
796 ASSERT(DeviceNode
->PhysicalDeviceObject
);
798 ObDereferenceObject(DeviceNode
->PhysicalDeviceObject
);
800 /* Get previous sibling */
801 if (DeviceNode
->Parent
&& DeviceNode
->Parent
->Child
!= DeviceNode
)
803 PrevSibling
= DeviceNode
->Parent
->Child
;
804 while (PrevSibling
->Sibling
!= DeviceNode
)
805 PrevSibling
= PrevSibling
->Sibling
;
808 /* Unlink from parent if it exists */
809 if (DeviceNode
->Parent
)
811 if (DeviceNode
->Parent
->LastChild
== DeviceNode
)
813 DeviceNode
->Parent
->LastChild
= PrevSibling
;
815 PrevSibling
->Sibling
= NULL
;
817 if (DeviceNode
->Parent
->Child
== DeviceNode
)
818 DeviceNode
->Parent
->Child
= DeviceNode
->Sibling
;
821 /* Unlink from sibling list */
823 PrevSibling
->Sibling
= DeviceNode
->Sibling
;
825 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
827 RtlFreeUnicodeString(&DeviceNode
->InstancePath
);
829 RtlFreeUnicodeString(&DeviceNode
->ServiceName
);
831 if (DeviceNode
->ResourceList
)
833 ExFreePool(DeviceNode
->ResourceList
);
836 if (DeviceNode
->ResourceListTranslated
)
838 ExFreePool(DeviceNode
->ResourceListTranslated
);
841 if (DeviceNode
->ResourceRequirements
)
843 ExFreePool(DeviceNode
->ResourceRequirements
);
846 if (DeviceNode
->BootResources
)
848 ExFreePool(DeviceNode
->BootResources
);
851 ExFreePool(DeviceNode
);
853 return STATUS_SUCCESS
;
858 IopSynchronousCall(IN PDEVICE_OBJECT DeviceObject
,
859 IN PIO_STACK_LOCATION IoStackLocation
,
860 OUT PVOID
*Information
)
863 PIO_STACK_LOCATION IrpStack
;
864 IO_STATUS_BLOCK IoStatusBlock
;
867 PDEVICE_OBJECT TopDeviceObject
;
870 /* Call the top of the device stack */
871 TopDeviceObject
= IoGetAttachedDeviceReference(DeviceObject
);
873 /* Allocate an IRP */
874 Irp
= IoAllocateIrp(TopDeviceObject
->StackSize
, FALSE
);
875 if (!Irp
) return STATUS_INSUFFICIENT_RESOURCES
;
877 /* Initialize to failure */
878 Irp
->IoStatus
.Status
= IoStatusBlock
.Status
= STATUS_NOT_SUPPORTED
;
879 Irp
->IoStatus
.Information
= IoStatusBlock
.Information
= 0;
881 /* Special case for IRP_MN_FILTER_RESOURCE_REQUIREMENTS */
882 if (IoStackLocation
->MinorFunction
== IRP_MN_FILTER_RESOURCE_REQUIREMENTS
)
884 /* Copy the resource requirements list into the IOSB */
885 Irp
->IoStatus
.Information
=
886 IoStatusBlock
.Information
= (ULONG_PTR
)IoStackLocation
->Parameters
.FilterResourceRequirements
.IoResourceRequirementList
;
889 /* Initialize the event */
890 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
893 Irp
->UserIosb
= &IoStatusBlock
;
894 Irp
->UserEvent
= &Event
;
897 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
898 IoQueueThreadIrp(Irp
);
900 /* Copy-in the stack */
901 IrpStack
= IoGetNextIrpStackLocation(Irp
);
902 *IrpStack
= *IoStackLocation
;
904 /* Call the driver */
905 Status
= IoCallDriver(TopDeviceObject
, Irp
);
906 if (Status
== STATUS_PENDING
)
909 KeWaitForSingleObject(&Event
,
914 Status
= IoStatusBlock
.Status
;
917 /* Return the information */
918 *Information
= (PVOID
)IoStatusBlock
.Information
;
924 IopInitiatePnpIrp(IN PDEVICE_OBJECT DeviceObject
,
925 IN OUT PIO_STATUS_BLOCK IoStatusBlock
,
926 IN UCHAR MinorFunction
,
927 IN PIO_STACK_LOCATION Stack OPTIONAL
)
929 IO_STACK_LOCATION IoStackLocation
;
931 /* Fill out the stack information */
932 RtlZeroMemory(&IoStackLocation
, sizeof(IO_STACK_LOCATION
));
933 IoStackLocation
.MajorFunction
= IRP_MJ_PNP
;
934 IoStackLocation
.MinorFunction
= MinorFunction
;
938 RtlCopyMemory(&IoStackLocation
.Parameters
,
940 sizeof(Stack
->Parameters
));
943 /* Do the PnP call */
944 IoStatusBlock
->Status
= IopSynchronousCall(DeviceObject
,
946 (PVOID
)&IoStatusBlock
->Information
);
947 return IoStatusBlock
->Status
;
951 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context
)
953 PDEVICE_NODE ParentDeviceNode
;
954 PDEVICE_NODE ChildDeviceNode
;
957 /* Copy context data so we don't overwrite it in subsequent calls to this function */
958 ParentDeviceNode
= Context
->DeviceNode
;
960 /* Call the action routine */
961 Status
= (Context
->Action
)(ParentDeviceNode
, Context
->Context
);
962 if (!NT_SUCCESS(Status
))
967 /* Traversal of all children nodes */
968 for (ChildDeviceNode
= ParentDeviceNode
->Child
;
969 ChildDeviceNode
!= NULL
;
970 ChildDeviceNode
= ChildDeviceNode
->Sibling
)
972 /* Pass the current device node to the action routine */
973 Context
->DeviceNode
= ChildDeviceNode
;
975 Status
= IopTraverseDeviceTreeNode(Context
);
976 if (!NT_SUCCESS(Status
))
987 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context
)
991 DPRINT("Context 0x%p\n", Context
);
993 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
994 Context
->DeviceNode
, Context
->FirstDeviceNode
, Context
->Action
, Context
->Context
);
996 /* Start from the specified device node */
997 Context
->DeviceNode
= Context
->FirstDeviceNode
;
999 /* Recursively traverse the device tree */
1000 Status
= IopTraverseDeviceTreeNode(Context
);
1001 if (Status
== STATUS_UNSUCCESSFUL
)
1003 /* The action routine just wanted to terminate the traversal with status
1004 code STATUS_SUCCESS */
1005 Status
= STATUS_SUCCESS
;
1013 * IopCreateDeviceKeyPath
1015 * Creates a registry key
1019 * Name of the key to be created.
1021 * Handle to the newly created key
1024 * This method can create nested trees, so parent of RegistryPath can
1025 * be not existant, and will be created if needed.
1029 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
1030 IN ULONG CreateOptions
,
1033 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(ENUM_ROOT
);
1034 HANDLE hParent
= NULL
, hKey
;
1035 OBJECT_ATTRIBUTES ObjectAttributes
;
1036 UNICODE_STRING KeyName
;
1037 LPCWSTR Current
, Last
;
1041 /* Assume failure */
1044 /* Create a volatile device tree in 1st stage so we have a clean slate
1045 * for enumeration using the correct HAL (chosen in 1st stage setup) */
1046 if (ExpInTextModeSetup
) CreateOptions
|= REG_OPTION_VOLATILE
;
1048 /* Open root key for device instances */
1049 Status
= IopOpenRegistryKeyEx(&hParent
, NULL
, &EnumU
, KEY_CREATE_SUB_KEY
);
1050 if (!NT_SUCCESS(Status
))
1052 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
1056 Current
= KeyName
.Buffer
= RegistryPath
->Buffer
;
1057 Last
= &RegistryPath
->Buffer
[RegistryPath
->Length
/ sizeof(WCHAR
)];
1059 /* Go up to the end of the string */
1060 while (Current
<= Last
)
1062 if (Current
!= Last
&& *Current
!= '\\')
1064 /* Not the end of the string and not a separator */
1069 /* Prepare relative key name */
1070 Length
= (USHORT
)((ULONG_PTR
)Current
- (ULONG_PTR
)KeyName
.Buffer
);
1071 KeyName
.MaximumLength
= KeyName
.Length
= Length
;
1072 DPRINT("Create '%wZ'\n", &KeyName
);
1075 InitializeObjectAttributes(&ObjectAttributes
,
1077 OBJ_CASE_INSENSITIVE
,
1080 Status
= ZwCreateKey(&hKey
,
1081 Current
== Last
? KEY_ALL_ACCESS
: KEY_CREATE_SUB_KEY
,
1088 /* Close parent key handle, we don't need it anymore */
1092 /* Key opening/creating failed? */
1093 if (!NT_SUCCESS(Status
))
1095 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName
, Status
);
1099 /* Check if it is the end of the string */
1100 if (Current
== Last
)
1102 /* Yes, return success */
1104 return STATUS_SUCCESS
;
1107 /* Start with this new parent key */
1110 KeyName
.Buffer
= (LPWSTR
)Current
;
1113 return STATUS_UNSUCCESSFUL
;
1117 IopSetDeviceInstanceData(HANDLE InstanceKey
,
1118 PDEVICE_NODE DeviceNode
)
1120 OBJECT_ATTRIBUTES ObjectAttributes
;
1121 UNICODE_STRING KeyName
;
1126 HANDLE ControlHandle
;
1128 DPRINT("IopSetDeviceInstanceData() called\n");
1130 /* Create the 'LogConf' key */
1131 RtlInitUnicodeString(&KeyName
, L
"LogConf");
1132 InitializeObjectAttributes(&ObjectAttributes
,
1134 OBJ_CASE_INSENSITIVE
,
1137 Status
= ZwCreateKey(&LogConfKey
,
1142 REG_OPTION_VOLATILE
,
1144 if (NT_SUCCESS(Status
))
1146 /* Set 'BootConfig' value */
1147 if (DeviceNode
->BootResources
!= NULL
)
1149 ResCount
= DeviceNode
->BootResources
->Count
;
1152 RtlInitUnicodeString(&KeyName
, L
"BootConfig");
1153 Status
= ZwSetValueKey(LogConfKey
,
1157 DeviceNode
->BootResources
,
1158 PnpDetermineResourceListSize(DeviceNode
->BootResources
));
1162 /* Set 'BasicConfigVector' value */
1163 if (DeviceNode
->ResourceRequirements
!= NULL
&&
1164 DeviceNode
->ResourceRequirements
->ListSize
!= 0)
1166 RtlInitUnicodeString(&KeyName
, L
"BasicConfigVector");
1167 Status
= ZwSetValueKey(LogConfKey
,
1170 REG_RESOURCE_REQUIREMENTS_LIST
,
1171 DeviceNode
->ResourceRequirements
,
1172 DeviceNode
->ResourceRequirements
->ListSize
);
1175 ZwClose(LogConfKey
);
1178 /* Set the 'ConfigFlags' value */
1179 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
1180 Status
= ZwQueryValueKey(InstanceKey
,
1182 KeyValueBasicInformation
,
1186 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1188 /* Write the default value */
1189 ULONG DefaultConfigFlags
= 0;
1190 Status
= ZwSetValueKey(InstanceKey
,
1194 &DefaultConfigFlags
,
1195 sizeof(DefaultConfigFlags
));
1198 /* Create the 'Control' key */
1199 RtlInitUnicodeString(&KeyName
, L
"Control");
1200 InitializeObjectAttributes(&ObjectAttributes
,
1202 OBJ_CASE_INSENSITIVE
,
1205 Status
= ZwCreateKey(&ControlHandle
, 0, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
1207 if (NT_SUCCESS(Status
))
1208 ZwClose(ControlHandle
);
1210 DPRINT("IopSetDeviceInstanceData() done\n");
1216 * IopGetParentIdPrefix
1218 * Retrieve (or create) a string which identifies a device.
1222 * Pointer to device node.
1224 * Pointer to the string where is returned the parent node identifier
1227 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1228 * valid and its Buffer field is NULL-terminated. The caller needs to
1229 * to free the string with RtlFreeUnicodeString when it is no longer
1234 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode
,
1235 PUNICODE_STRING ParentIdPrefix
)
1237 ULONG KeyNameBufferLength
;
1238 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation
= NULL
;
1239 UNICODE_STRING KeyName
= {0, 0, NULL
};
1240 UNICODE_STRING KeyValue
;
1241 UNICODE_STRING ValueName
;
1246 /* HACK: As long as some devices have a NULL device
1247 * instance path, the following test is required :(
1249 if (DeviceNode
->Parent
->InstancePath
.Length
== 0)
1251 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1252 &DeviceNode
->InstancePath
);
1253 return STATUS_UNSUCCESSFUL
;
1256 /* 1. Try to retrieve ParentIdPrefix from registry */
1257 KeyNameBufferLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
[0]) + MAX_PATH
* sizeof(WCHAR
);
1258 ParentIdPrefixInformation
= ExAllocatePool(PagedPool
, KeyNameBufferLength
+ sizeof(WCHAR
));
1259 if (!ParentIdPrefixInformation
)
1261 return STATUS_INSUFFICIENT_RESOURCES
;
1264 KeyName
.Buffer
= ExAllocatePool(PagedPool
, (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
);
1265 if (!KeyName
.Buffer
)
1267 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1271 KeyName
.MaximumLength
= (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
;
1273 RtlAppendUnicodeToString(&KeyName
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1274 RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->Parent
->InstancePath
);
1276 Status
= IopOpenRegistryKeyEx(&hKey
, NULL
, &KeyName
, KEY_QUERY_VALUE
| KEY_SET_VALUE
);
1277 if (!NT_SUCCESS(Status
))
1279 RtlInitUnicodeString(&ValueName
, L
"ParentIdPrefix");
1280 Status
= ZwQueryValueKey(
1282 KeyValuePartialInformation
, ParentIdPrefixInformation
,
1283 KeyNameBufferLength
, &KeyNameBufferLength
);
1284 if (NT_SUCCESS(Status
))
1286 if (ParentIdPrefixInformation
->Type
!= REG_SZ
)
1287 Status
= STATUS_UNSUCCESSFUL
;
1290 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1291 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1295 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1297 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1298 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1302 /* 2. Create the ParentIdPrefix value */
1303 crc32
= RtlComputeCrc32(0,
1304 (PUCHAR
)DeviceNode
->Parent
->InstancePath
.Buffer
,
1305 DeviceNode
->Parent
->InstancePath
.Length
);
1307 swprintf((PWSTR
)ParentIdPrefixInformation
->Data
, L
"%lx&%lx", DeviceNode
->Parent
->Level
, crc32
);
1308 RtlInitUnicodeString(&KeyValue
, (PWSTR
)ParentIdPrefixInformation
->Data
);
1310 /* 3. Try to write the ParentIdPrefix to registry */
1311 Status
= ZwSetValueKey(hKey
,
1315 (PVOID
)KeyValue
.Buffer
,
1316 ((ULONG
)wcslen(KeyValue
.Buffer
) + 1) * sizeof(WCHAR
));
1319 if (NT_SUCCESS(Status
))
1321 /* Duplicate the string to return it */
1322 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
, &KeyValue
, ParentIdPrefix
);
1324 ExFreePool(ParentIdPrefixInformation
);
1325 RtlFreeUnicodeString(&KeyName
);
1332 IopQueryHardwareIds(PDEVICE_NODE DeviceNode
,
1335 IO_STACK_LOCATION Stack
;
1336 IO_STATUS_BLOCK IoStatusBlock
;
1338 UNICODE_STRING ValueName
;
1340 ULONG Length
, TotalLength
;
1342 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1344 RtlZeroMemory(&Stack
, sizeof(Stack
));
1345 Stack
.Parameters
.QueryId
.IdType
= BusQueryHardwareIDs
;
1346 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1350 if (NT_SUCCESS(Status
))
1353 * FIXME: Check for valid characters, if there is invalid characters
1357 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1358 DPRINT("Hardware IDs:\n");
1361 DPRINT(" %S\n", Ptr
);
1362 Length
= (ULONG
)wcslen(Ptr
) + 1;
1365 TotalLength
+= Length
;
1367 DPRINT("TotalLength: %hu\n", TotalLength
);
1370 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
1371 Status
= ZwSetValueKey(InstanceKey
,
1375 (PVOID
)IoStatusBlock
.Information
,
1376 (TotalLength
+ 1) * sizeof(WCHAR
));
1377 if (!NT_SUCCESS(Status
))
1379 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
1384 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1391 IopQueryCompatibleIds(PDEVICE_NODE DeviceNode
,
1394 IO_STACK_LOCATION Stack
;
1395 IO_STATUS_BLOCK IoStatusBlock
;
1397 UNICODE_STRING ValueName
;
1399 ULONG Length
, TotalLength
;
1401 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1403 RtlZeroMemory(&Stack
, sizeof(Stack
));
1404 Stack
.Parameters
.QueryId
.IdType
= BusQueryCompatibleIDs
;
1405 Status
= IopInitiatePnpIrp(
1406 DeviceNode
->PhysicalDeviceObject
,
1410 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1413 * FIXME: Check for valid characters, if there is invalid characters
1417 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1418 DPRINT("Compatible IDs:\n");
1421 DPRINT(" %S\n", Ptr
);
1422 Length
= (ULONG
)wcslen(Ptr
) + 1;
1425 TotalLength
+= Length
;
1427 DPRINT("TotalLength: %hu\n", TotalLength
);
1430 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
1431 Status
= ZwSetValueKey(InstanceKey
,
1435 (PVOID
)IoStatusBlock
.Information
,
1436 (TotalLength
+ 1) * sizeof(WCHAR
));
1437 if (!NT_SUCCESS(Status
))
1439 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status
);
1444 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1452 * IopActionInterrogateDeviceStack
1454 * Retrieve information for all (direct) child nodes of a parent node.
1458 * Pointer to device node.
1460 * Pointer to parent node to retrieve child node information for.
1463 * Any errors that occur are logged instead so that all child services have a chance
1464 * of being interrogated.
1468 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
1471 IO_STATUS_BLOCK IoStatusBlock
;
1472 PDEVICE_NODE ParentDeviceNode
;
1473 WCHAR InstancePath
[MAX_PATH
];
1474 IO_STACK_LOCATION Stack
;
1476 ULONG RequiredLength
;
1478 HANDLE InstanceKey
= NULL
;
1479 UNICODE_STRING ValueName
;
1480 UNICODE_STRING ParentIdPrefix
= { 0, 0, NULL
};
1481 DEVICE_CAPABILITIES DeviceCapabilities
;
1483 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode
, Context
);
1484 DPRINT("PDO 0x%p\n", DeviceNode
->PhysicalDeviceObject
);
1486 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
1489 * We are called for the parent too, but we don't need to do special
1490 * handling for this node
1493 if (DeviceNode
== ParentDeviceNode
)
1495 DPRINT("Success\n");
1496 return STATUS_SUCCESS
;
1500 * Make sure this device node is a direct child of the parent device node
1501 * that is given as an argument
1504 if (DeviceNode
->Parent
!= ParentDeviceNode
)
1506 DPRINT("Skipping 2+ level child\n");
1507 return STATUS_SUCCESS
;
1510 /* Skip processing if it was already completed before */
1511 if (DeviceNode
->Flags
& DNF_PROCESSED
)
1514 return STATUS_SUCCESS
;
1518 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
1519 if (!NT_SUCCESS(Status
))
1521 DPRINT1("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status
);
1526 * FIXME: For critical errors, cleanup and disable device, but always
1527 * return STATUS_SUCCESS.
1530 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1532 Stack
.Parameters
.QueryId
.IdType
= BusQueryDeviceID
;
1533 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1537 if (NT_SUCCESS(Status
))
1539 /* Copy the device id string */
1540 wcscpy(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1543 * FIXME: Check for valid characters, if there is invalid characters
1549 DPRINT1("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1551 /* We have to return success otherwise we abort the traverse operation */
1552 return STATUS_SUCCESS
;
1555 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
1557 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
1558 if (!NT_SUCCESS(Status
))
1560 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
1562 /* We have to return success otherwise we abort the traverse operation */
1563 return STATUS_SUCCESS
;
1566 /* This bit is only check after enumeration */
1567 if (DeviceCapabilities
.HardwareDisabled
)
1569 /* FIXME: Cleanup device */
1570 DeviceNode
->Flags
|= DNF_DISABLED
;
1571 return STATUS_SUCCESS
;
1574 DeviceNode
->Flags
&= ~DNF_DISABLED
;
1576 if (!DeviceCapabilities
.UniqueID
)
1578 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1579 DPRINT("Instance ID is not unique\n");
1580 Status
= IopGetParentIdPrefix(DeviceNode
, &ParentIdPrefix
);
1581 if (!NT_SUCCESS(Status
))
1583 DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status
);
1585 /* We have to return success otherwise we abort the traverse operation */
1586 return STATUS_SUCCESS
;
1590 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1592 Stack
.Parameters
.QueryId
.IdType
= BusQueryInstanceID
;
1593 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1597 if (NT_SUCCESS(Status
))
1599 /* Append the instance id string */
1600 wcscat(InstancePath
, L
"\\");
1601 if (ParentIdPrefix
.Length
> 0)
1603 /* Add information from parent bus device to InstancePath */
1604 wcscat(InstancePath
, ParentIdPrefix
.Buffer
);
1605 if (IoStatusBlock
.Information
&& *(PWSTR
)IoStatusBlock
.Information
)
1606 wcscat(InstancePath
, L
"&");
1608 if (IoStatusBlock
.Information
)
1609 wcscat(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1612 * FIXME: Check for valid characters, if there is invalid characters
1618 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1620 RtlFreeUnicodeString(&ParentIdPrefix
);
1622 if (!RtlCreateUnicodeString(&DeviceNode
->InstancePath
, InstancePath
))
1624 DPRINT("No resources\n");
1625 /* FIXME: Cleanup and disable device */
1628 DPRINT("InstancePath is %S\n", DeviceNode
->InstancePath
.Buffer
);
1631 * Create registry key for the instance id, if it doesn't exist yet
1633 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
1634 if (!NT_SUCCESS(Status
))
1636 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status
);
1638 /* We have to return success otherwise we abort the traverse operation */
1639 return STATUS_SUCCESS
;
1642 IopQueryHardwareIds(DeviceNode
, InstanceKey
);
1644 IopQueryCompatibleIds(DeviceNode
, InstanceKey
);
1646 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1648 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextDescription
;
1649 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
1650 Status
= IopInitiatePnpIrp(
1651 DeviceNode
->PhysicalDeviceObject
,
1653 IRP_MN_QUERY_DEVICE_TEXT
,
1655 /* This key is mandatory, so even if the Irp fails, we still write it */
1656 RtlInitUnicodeString(&ValueName
, L
"DeviceDesc");
1657 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
1659 if (NT_SUCCESS(Status
) &&
1660 IoStatusBlock
.Information
&&
1661 (*(PWSTR
)IoStatusBlock
.Information
!= 0))
1663 /* This key is overriden when a driver is installed. Don't write the
1664 * new description if another one already exists */
1665 Status
= ZwSetValueKey(InstanceKey
,
1669 (PVOID
)IoStatusBlock
.Information
,
1670 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
1674 UNICODE_STRING DeviceDesc
= RTL_CONSTANT_STRING(L
"Unknown device");
1675 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status
);
1677 Status
= ZwSetValueKey(InstanceKey
,
1682 DeviceDesc
.MaximumLength
);
1684 if (!NT_SUCCESS(Status
))
1686 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status
);
1692 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1694 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextLocationInformation
;
1695 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
1696 Status
= IopInitiatePnpIrp(
1697 DeviceNode
->PhysicalDeviceObject
,
1699 IRP_MN_QUERY_DEVICE_TEXT
,
1701 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1703 DPRINT("LocationInformation: %S\n", (PWSTR
)IoStatusBlock
.Information
);
1704 RtlInitUnicodeString(&ValueName
, L
"LocationInformation");
1705 Status
= ZwSetValueKey(InstanceKey
,
1709 (PVOID
)IoStatusBlock
.Information
,
1710 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
1711 if (!NT_SUCCESS(Status
))
1713 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
1718 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1721 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1723 Status
= IopInitiatePnpIrp(
1724 DeviceNode
->PhysicalDeviceObject
,
1726 IRP_MN_QUERY_BUS_INFORMATION
,
1728 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1730 PPNP_BUS_INFORMATION BusInformation
=
1731 (PPNP_BUS_INFORMATION
)IoStatusBlock
.Information
;
1733 DeviceNode
->ChildBusNumber
= BusInformation
->BusNumber
;
1734 DeviceNode
->ChildInterfaceType
= BusInformation
->LegacyBusType
;
1735 DeviceNode
->ChildBusTypeIndex
= IopGetBusTypeGuidIndex(&BusInformation
->BusTypeGuid
);
1736 ExFreePool(BusInformation
);
1740 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1742 DeviceNode
->ChildBusNumber
= 0xFFFFFFF0;
1743 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
1744 DeviceNode
->ChildBusTypeIndex
= -1;
1747 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1749 Status
= IopInitiatePnpIrp(
1750 DeviceNode
->PhysicalDeviceObject
,
1752 IRP_MN_QUERY_RESOURCES
,
1754 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1756 DeviceNode
->BootResources
=
1757 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
1758 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
1762 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
1763 DeviceNode
->BootResources
= NULL
;
1766 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1768 Status
= IopInitiatePnpIrp(
1769 DeviceNode
->PhysicalDeviceObject
,
1771 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
1773 if (NT_SUCCESS(Status
))
1775 DeviceNode
->ResourceRequirements
=
1776 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
1780 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
1781 DeviceNode
->ResourceRequirements
= NULL
;
1784 if (InstanceKey
!= NULL
)
1786 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
1789 ZwClose(InstanceKey
);
1791 IopDeviceNodeSetFlag(DeviceNode
, DNF_PROCESSED
);
1793 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
1795 /* Report the device to the user-mode pnp manager */
1796 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
1797 &DeviceNode
->InstancePath
);
1800 return STATUS_SUCCESS
;
1805 IopHandleDeviceRemoval(
1806 IN PDEVICE_NODE DeviceNode
,
1807 IN PDEVICE_RELATIONS DeviceRelations
)
1809 PDEVICE_NODE Child
= DeviceNode
->Child
, NextChild
;
1813 while (Child
!= NULL
)
1815 NextChild
= Child
->Sibling
;
1818 for (i
= 0; DeviceRelations
&& i
< DeviceRelations
->Count
; i
++)
1820 if (IopGetDeviceNode(DeviceRelations
->Objects
[i
]) == Child
)
1827 if (!Found
&& !(Child
->Flags
& DNF_WILL_BE_REMOVED
))
1829 /* Send removal IRPs to all of its children */
1830 IopPrepareDeviceForRemoval(Child
->PhysicalDeviceObject
, TRUE
);
1832 /* Send the surprise removal IRP */
1833 IopSendSurpriseRemoval(Child
->PhysicalDeviceObject
);
1835 /* Tell the user-mode PnP manager that a device was removed */
1836 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
1837 &Child
->InstancePath
);
1839 /* Send the remove device IRP */
1840 IopSendRemoveDevice(Child
->PhysicalDeviceObject
);
1849 IN PDEVICE_OBJECT DeviceObject
)
1851 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
1852 DEVICETREE_TRAVERSE_CONTEXT Context
;
1853 PDEVICE_RELATIONS DeviceRelations
;
1854 PDEVICE_OBJECT ChildDeviceObject
;
1855 IO_STATUS_BLOCK IoStatusBlock
;
1856 PDEVICE_NODE ChildDeviceNode
;
1857 IO_STACK_LOCATION Stack
;
1861 DPRINT("DeviceObject 0x%p\n", DeviceObject
);
1863 if (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
)
1865 DeviceNode
->Flags
&= ~DNF_NEED_ENUMERATION_ONLY
;
1867 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
1868 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
1869 &DeviceNode
->InstancePath
);
1872 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
1874 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
1876 Status
= IopInitiatePnpIrp(
1879 IRP_MN_QUERY_DEVICE_RELATIONS
,
1881 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
1883 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
1887 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
1890 * Send removal IRPs for devices that have disappeared
1891 * NOTE: This code handles the case where no relations are specified
1893 IopHandleDeviceRemoval(DeviceNode
, DeviceRelations
);
1895 /* Now we bail if nothing was returned */
1896 if (!DeviceRelations
)
1898 /* We're all done */
1899 DPRINT("No PDOs\n");
1900 return STATUS_SUCCESS
;
1903 DPRINT("Got %u PDOs\n", DeviceRelations
->Count
);
1906 * Create device nodes for all discovered devices
1908 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
1910 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
1911 ASSERT((ChildDeviceObject
->Flags
& DO_DEVICE_INITIALIZING
) == 0);
1913 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
1914 if (!ChildDeviceNode
)
1916 /* One doesn't exist, create it */
1917 Status
= IopCreateDeviceNode(
1922 if (NT_SUCCESS(Status
))
1924 /* Mark the node as enumerated */
1925 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
1927 /* Mark the DO as bus enumerated */
1928 ChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
1932 /* Ignore this DO */
1933 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status
, i
);
1934 ObDereferenceObject(ChildDeviceObject
);
1939 /* Mark it as enumerated */
1940 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
1941 ObDereferenceObject(ChildDeviceObject
);
1944 ExFreePool(DeviceRelations
);
1947 * Retrieve information about all discovered children from the bus driver
1949 IopInitDeviceTreeTraverseContext(
1952 IopActionInterrogateDeviceStack
,
1955 Status
= IopTraverseDeviceTree(&Context
);
1956 if (!NT_SUCCESS(Status
))
1958 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
1963 * Retrieve configuration from the registry for discovered children
1965 IopInitDeviceTreeTraverseContext(
1968 IopActionConfigureChildServices
,
1971 Status
= IopTraverseDeviceTree(&Context
);
1972 if (!NT_SUCCESS(Status
))
1974 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
1979 * Initialize services for discovered children.
1981 Status
= IopInitializePnpServices(DeviceNode
);
1982 if (!NT_SUCCESS(Status
))
1984 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status
);
1988 DPRINT("IopEnumerateDevice() finished\n");
1989 return STATUS_SUCCESS
;
1994 * IopActionConfigureChildServices
1996 * Retrieve configuration for all (direct) child nodes of a parent node.
2000 * Pointer to device node.
2002 * Pointer to parent node to retrieve child node configuration for.
2005 * Any errors that occur are logged instead so that all child services have a chance of beeing
2010 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode
,
2013 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
2014 PDEVICE_NODE ParentDeviceNode
;
2015 PUNICODE_STRING Service
;
2016 UNICODE_STRING ClassGUID
;
2018 DEVICE_CAPABILITIES DeviceCaps
;
2020 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode
, Context
);
2022 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2025 * We are called for the parent too, but we don't need to do special
2026 * handling for this node
2028 if (DeviceNode
== ParentDeviceNode
)
2030 DPRINT("Success\n");
2031 return STATUS_SUCCESS
;
2035 * Make sure this device node is a direct child of the parent device node
2036 * that is given as an argument
2039 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2041 DPRINT("Skipping 2+ level child\n");
2042 return STATUS_SUCCESS
;
2045 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2047 DPRINT1("Child not ready to be configured\n");
2048 return STATUS_SUCCESS
;
2051 if (!(DeviceNode
->Flags
& (DNF_DISABLED
| DNF_STARTED
| DNF_ADDED
)))
2053 WCHAR RegKeyBuffer
[MAX_PATH
];
2054 UNICODE_STRING RegKey
;
2057 RegKey
.MaximumLength
= sizeof(RegKeyBuffer
);
2058 RegKey
.Buffer
= RegKeyBuffer
;
2061 * Retrieve configuration from Enum key
2064 Service
= &DeviceNode
->ServiceName
;
2066 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
2067 RtlInitUnicodeString(Service
, NULL
);
2068 RtlInitUnicodeString(&ClassGUID
, NULL
);
2070 QueryTable
[0].Name
= L
"Service";
2071 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2072 QueryTable
[0].EntryContext
= Service
;
2074 QueryTable
[1].Name
= L
"ClassGUID";
2075 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2076 QueryTable
[1].EntryContext
= &ClassGUID
;
2077 QueryTable
[1].DefaultType
= REG_SZ
;
2078 QueryTable
[1].DefaultData
= L
"";
2079 QueryTable
[1].DefaultLength
= 0;
2081 RtlAppendUnicodeToString(&RegKey
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2082 RtlAppendUnicodeStringToString(&RegKey
, &DeviceNode
->InstancePath
);
2084 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
2085 RegKey
.Buffer
, QueryTable
, NULL
, NULL
);
2087 if (!NT_SUCCESS(Status
))
2089 /* FIXME: Log the error */
2090 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2091 &DeviceNode
->InstancePath
, Status
);
2092 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2093 return STATUS_SUCCESS
;
2096 if (Service
->Buffer
== NULL
)
2098 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
)) &&
2099 DeviceCaps
.RawDeviceOK
)
2101 DPRINT1("%wZ is using parent bus driver (%wZ)\n", &DeviceNode
->InstancePath
, &ParentDeviceNode
->ServiceName
);
2103 DeviceNode
->ServiceName
.Length
= 0;
2104 DeviceNode
->ServiceName
.MaximumLength
= 0;
2105 DeviceNode
->ServiceName
.Buffer
= NULL
;
2107 else if (ClassGUID
.Length
!= 0)
2109 /* Device has a ClassGUID value, but no Service value.
2110 * Suppose it is using the NULL driver, so state the
2111 * device is started */
2112 DPRINT("%wZ is using NULL driver\n", &DeviceNode
->InstancePath
);
2113 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2117 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2119 return STATUS_SUCCESS
;
2122 DPRINT("Got Service %S\n", Service
->Buffer
);
2125 return STATUS_SUCCESS
;
2129 * IopActionInitChildServices
2131 * Initialize the service for all (direct) child nodes of a parent node
2135 * Pointer to device node.
2137 * Pointer to parent node to initialize child node services for.
2140 * If the driver image for a service is not loaded and initialized
2141 * it is done here too. Any errors that occur are logged instead so
2142 * that all child services have a chance of being initialized.
2146 IopActionInitChildServices(PDEVICE_NODE DeviceNode
,
2149 PDEVICE_NODE ParentDeviceNode
;
2151 BOOLEAN BootDrivers
= !PnpSystemInit
;
2153 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode
, Context
);
2155 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2158 * We are called for the parent too, but we don't need to do special
2159 * handling for this node
2161 if (DeviceNode
== ParentDeviceNode
)
2163 DPRINT("Success\n");
2164 return STATUS_SUCCESS
;
2168 * Make sure this device node is a direct child of the parent device node
2169 * that is given as an argument
2172 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2174 DPRINT("Skipping 2+ level child\n");
2175 return STATUS_SUCCESS
;
2178 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2180 DPRINT1("Child not ready to be added\n");
2181 return STATUS_SUCCESS
;
2184 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_STARTED
) ||
2185 IopDeviceNodeHasFlag(DeviceNode
, DNF_ADDED
) ||
2186 IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
))
2187 return STATUS_SUCCESS
;
2189 if (DeviceNode
->ServiceName
.Buffer
== NULL
)
2191 /* We don't need to worry about loading the driver because we're
2192 * being driven in raw mode so our parent must be loaded to get here */
2193 Status
= IopInitializeDevice(DeviceNode
, NULL
);
2194 if (NT_SUCCESS(Status
))
2196 Status
= IopStartDevice(DeviceNode
);
2197 if (!NT_SUCCESS(Status
))
2199 DPRINT1("IopStartDevice(%wZ) failed with status 0x%08x\n",
2200 &DeviceNode
->InstancePath
, Status
);
2206 PLDR_DATA_TABLE_ENTRY ModuleObject
;
2207 PDRIVER_OBJECT DriverObject
;
2209 /* Get existing DriverObject pointer (in case the driver has
2210 already been loaded and initialized) */
2211 Status
= IopGetDriverObject(
2213 &DeviceNode
->ServiceName
,
2216 if (!NT_SUCCESS(Status
))
2218 /* Driver is not initialized, try to load it */
2219 Status
= IopLoadServiceModule(&DeviceNode
->ServiceName
, &ModuleObject
);
2221 if (NT_SUCCESS(Status
) || Status
== STATUS_IMAGE_ALREADY_LOADED
)
2223 /* STATUS_IMAGE_ALREADY_LOADED means this driver
2224 was loaded by the bootloader */
2225 if ((Status
!= STATUS_IMAGE_ALREADY_LOADED
) ||
2226 (Status
== STATUS_IMAGE_ALREADY_LOADED
&& !DriverObject
))
2228 /* Initialize the driver */
2229 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
,
2230 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
2234 Status
= STATUS_SUCCESS
;
2239 DPRINT1("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
2240 &DeviceNode
->ServiceName
, Status
);
2244 /* Driver is loaded and initialized at this point */
2245 if (NT_SUCCESS(Status
))
2247 /* Initialize the device, including all filters */
2248 Status
= PipCallDriverAddDevice(DeviceNode
, FALSE
, DriverObject
);
2253 * Don't disable when trying to load only boot drivers
2257 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2258 IopDeviceNodeSetFlag(DeviceNode
, DNF_START_FAILED
);
2259 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2260 DPRINT1("Initialization of service %S failed (Status %x)\n",
2261 DeviceNode
->ServiceName
.Buffer
, Status
);
2266 return STATUS_SUCCESS
;
2270 * IopInitializePnpServices
2272 * Initialize services for discovered children
2276 * Top device node to start initializing services.
2282 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode
)
2284 DEVICETREE_TRAVERSE_CONTEXT Context
;
2286 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode
);
2288 IopInitDeviceTreeTraverseContext(
2291 IopActionInitChildServices
,
2294 return IopTraverseDeviceTree(&Context
);
2297 static NTSTATUS INIT_FUNCTION
2298 IopEnumerateDetectedDevices(
2300 IN PUNICODE_STRING RelativePath OPTIONAL
,
2302 IN BOOLEAN EnumerateSubKeys
,
2303 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources
,
2304 IN ULONG ParentBootResourcesLength
)
2306 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
2307 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
2308 UNICODE_STRING ConfigurationDataU
= RTL_CONSTANT_STRING(L
"Configuration Data");
2309 UNICODE_STRING BootConfigU
= RTL_CONSTANT_STRING(L
"BootConfig");
2310 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
2311 OBJECT_ATTRIBUTES ObjectAttributes
;
2312 HANDLE hDevicesKey
= NULL
;
2313 HANDLE hDeviceKey
= NULL
;
2314 HANDLE hLevel1Key
, hLevel2Key
= NULL
, hLogConf
;
2315 UNICODE_STRING Level2NameU
;
2316 WCHAR Level2Name
[5];
2317 ULONG IndexDevice
= 0;
2319 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
2320 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
2321 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
2322 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
2323 UNICODE_STRING DeviceName
, ValueName
;
2325 PCM_FULL_RESOURCE_DESCRIPTOR BootResources
= NULL
;
2326 ULONG BootResourcesLength
;
2329 const UNICODE_STRING IdentifierPci
= RTL_CONSTANT_STRING(L
"PCI");
2330 UNICODE_STRING HardwareIdPci
= RTL_CONSTANT_STRING(L
"*PNP0A03\0");
2331 static ULONG DeviceIndexPci
= 0;
2332 const UNICODE_STRING IdentifierSerial
= RTL_CONSTANT_STRING(L
"SerialController");
2333 UNICODE_STRING HardwareIdSerial
= RTL_CONSTANT_STRING(L
"*PNP0501\0");
2334 static ULONG DeviceIndexSerial
= 0;
2335 const UNICODE_STRING IdentifierKeyboard
= RTL_CONSTANT_STRING(L
"KeyboardController");
2336 UNICODE_STRING HardwareIdKeyboard
= RTL_CONSTANT_STRING(L
"*PNP0303\0");
2337 static ULONG DeviceIndexKeyboard
= 0;
2338 const UNICODE_STRING IdentifierMouse
= RTL_CONSTANT_STRING(L
"PointerController");
2339 UNICODE_STRING HardwareIdMouse
= RTL_CONSTANT_STRING(L
"*PNP0F13\0");
2340 static ULONG DeviceIndexMouse
= 0;
2341 const UNICODE_STRING IdentifierParallel
= RTL_CONSTANT_STRING(L
"ParallelController");
2342 UNICODE_STRING HardwareIdParallel
= RTL_CONSTANT_STRING(L
"*PNP0400\0");
2343 static ULONG DeviceIndexParallel
= 0;
2344 const UNICODE_STRING IdentifierFloppy
= RTL_CONSTANT_STRING(L
"FloppyDiskPeripheral");
2345 UNICODE_STRING HardwareIdFloppy
= RTL_CONSTANT_STRING(L
"*PNP0700\0");
2346 static ULONG DeviceIndexFloppy
= 0;
2347 const UNICODE_STRING IdentifierIsa
= RTL_CONSTANT_STRING(L
"ISA");
2348 UNICODE_STRING HardwareIdIsa
= RTL_CONSTANT_STRING(L
"*PNP0A00\0");
2349 static ULONG DeviceIndexIsa
= 0;
2350 UNICODE_STRING HardwareIdKey
;
2351 PUNICODE_STRING pHardwareId
;
2352 ULONG DeviceIndex
= 0;
2353 PUCHAR CmResourceList
;
2358 Status
= IopOpenRegistryKeyEx(&hDevicesKey
, hBaseKey
, RelativePath
, KEY_ENUMERATE_SUB_KEYS
);
2359 if (!NT_SUCCESS(Status
))
2361 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2366 hDevicesKey
= hBaseKey
;
2368 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2369 if (!pDeviceInformation
)
2371 DPRINT("ExAllocatePool() failed\n");
2372 Status
= STATUS_NO_MEMORY
;
2376 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2377 if (!pValueInformation
)
2379 DPRINT("ExAllocatePool() failed\n");
2380 Status
= STATUS_NO_MEMORY
;
2386 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2387 if (Status
== STATUS_NO_MORE_ENTRIES
)
2389 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2391 ExFreePool(pDeviceInformation
);
2392 DeviceInfoLength
= RequiredSize
;
2393 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2394 if (!pDeviceInformation
)
2396 DPRINT("ExAllocatePool() failed\n");
2397 Status
= STATUS_NO_MEMORY
;
2400 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2402 if (!NT_SUCCESS(Status
))
2404 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2409 /* Open device key */
2410 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2411 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2413 Status
= IopOpenRegistryKeyEx(&hDeviceKey
, hDevicesKey
, &DeviceName
,
2414 KEY_QUERY_VALUE
+ (EnumerateSubKeys
? KEY_ENUMERATE_SUB_KEYS
: 0));
2415 if (!NT_SUCCESS(Status
))
2417 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2421 /* Read boot resources, and add then to parent ones */
2422 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2423 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2425 ExFreePool(pValueInformation
);
2426 ValueInfoLength
= RequiredSize
;
2427 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2428 if (!pValueInformation
)
2430 DPRINT("ExAllocatePool() failed\n");
2431 ZwDeleteKey(hLevel2Key
);
2432 Status
= STATUS_NO_MEMORY
;
2435 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2437 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
2439 BootResources
= ParentBootResources
;
2440 BootResourcesLength
= ParentBootResourcesLength
;
2442 else if (!NT_SUCCESS(Status
))
2444 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2447 else if (pValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
)
2449 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_FULL_RESOURCE_DESCRIPTOR
);
2454 static const ULONG Header
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
);
2456 /* Concatenate current resources and parent ones */
2457 if (ParentBootResourcesLength
== 0)
2458 BootResourcesLength
= pValueInformation
->DataLength
;
2460 BootResourcesLength
= ParentBootResourcesLength
2461 + pValueInformation
->DataLength
2463 BootResources
= ExAllocatePool(PagedPool
, BootResourcesLength
);
2466 DPRINT("ExAllocatePool() failed\n");
2469 if (ParentBootResourcesLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
2471 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2473 else if (ParentBootResources
->PartialResourceList
.PartialDescriptors
[ParentBootResources
->PartialResourceList
.Count
- 1].Type
== CmResourceTypeDeviceSpecific
)
2475 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2477 (PVOID
)((ULONG_PTR
)BootResources
+ pValueInformation
->DataLength
),
2478 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2479 ParentBootResourcesLength
- Header
);
2480 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2484 RtlCopyMemory(BootResources
, pValueInformation
->Data
, Header
);
2486 (PVOID
)((ULONG_PTR
)BootResources
+ Header
),
2487 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2488 ParentBootResourcesLength
- Header
);
2490 (PVOID
)((ULONG_PTR
)BootResources
+ ParentBootResourcesLength
),
2491 pValueInformation
->Data
+ Header
,
2492 pValueInformation
->DataLength
- Header
);
2493 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2497 if (EnumerateSubKeys
)
2502 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2503 if (Status
== STATUS_NO_MORE_ENTRIES
)
2505 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2507 ExFreePool(pDeviceInformation
);
2508 DeviceInfoLength
= RequiredSize
;
2509 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2510 if (!pDeviceInformation
)
2512 DPRINT("ExAllocatePool() failed\n");
2513 Status
= STATUS_NO_MEMORY
;
2516 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2518 if (!NT_SUCCESS(Status
))
2520 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2524 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2525 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2527 Status
= IopEnumerateDetectedDevices(
2533 BootResourcesLength
);
2534 if (!NT_SUCCESS(Status
))
2539 /* Read identifier */
2540 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2541 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2543 ExFreePool(pValueInformation
);
2544 ValueInfoLength
= RequiredSize
;
2545 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2546 if (!pValueInformation
)
2548 DPRINT("ExAllocatePool() failed\n");
2549 Status
= STATUS_NO_MEMORY
;
2552 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2554 if (!NT_SUCCESS(Status
))
2556 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
2558 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2561 ValueName
.Length
= ValueName
.MaximumLength
= 0;
2563 else if (pValueInformation
->Type
!= REG_SZ
)
2565 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
2570 /* Assign hardware id to this device */
2571 ValueName
.Length
= ValueName
.MaximumLength
= (USHORT
)pValueInformation
->DataLength
;
2572 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
2573 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
2574 ValueName
.Length
-= sizeof(WCHAR
);
2577 if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierSerial
, FALSE
) == 0)
2579 pHardwareId
= &HardwareIdSerial
;
2580 DeviceIndex
= DeviceIndexSerial
++;
2582 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierKeyboard
, FALSE
) == 0)
2584 pHardwareId
= &HardwareIdKeyboard
;
2585 DeviceIndex
= DeviceIndexKeyboard
++;
2587 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierMouse
, FALSE
) == 0)
2589 pHardwareId
= &HardwareIdMouse
;
2590 DeviceIndex
= DeviceIndexMouse
++;
2592 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierParallel
, FALSE
) == 0)
2594 pHardwareId
= &HardwareIdParallel
;
2595 DeviceIndex
= DeviceIndexParallel
++;
2597 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierFloppy
, FALSE
) == 0)
2599 pHardwareId
= &HardwareIdFloppy
;
2600 DeviceIndex
= DeviceIndexFloppy
++;
2602 else if (NT_SUCCESS(Status
))
2604 /* Try to also match the device identifier */
2605 if (RtlCompareUnicodeString(&ValueName
, &IdentifierPci
, FALSE
) == 0)
2607 pHardwareId
= &HardwareIdPci
;
2608 DeviceIndex
= DeviceIndexPci
++;
2610 else if (RtlCompareUnicodeString(&ValueName
, &IdentifierIsa
, FALSE
) == 0)
2612 pHardwareId
= &HardwareIdIsa
;
2613 DeviceIndex
= DeviceIndexIsa
++;
2617 DPRINT("Unknown device '%wZ'\n", &ValueName
);
2623 /* Unknown key path */
2624 DPRINT("Unknown key path '%wZ'\n", RelativePath
);
2628 /* Prepare hardware id key (hardware id value without final \0) */
2629 HardwareIdKey
= *pHardwareId
;
2630 HardwareIdKey
.Length
-= sizeof(UNICODE_NULL
);
2632 /* Add the detected device to Root key */
2633 InitializeObjectAttributes(&ObjectAttributes
, &HardwareIdKey
, OBJ_KERNEL_HANDLE
, hRootKey
, NULL
);
2634 Status
= ZwCreateKey(
2640 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
2642 if (!NT_SUCCESS(Status
))
2644 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2647 swprintf(Level2Name
, L
"%04lu", DeviceIndex
);
2648 RtlInitUnicodeString(&Level2NameU
, Level2Name
);
2649 InitializeObjectAttributes(&ObjectAttributes
, &Level2NameU
, OBJ_KERNEL_HANDLE
, hLevel1Key
, NULL
);
2650 Status
= ZwCreateKey(
2652 KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
,
2656 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
2658 ZwClose(hLevel1Key
);
2659 if (!NT_SUCCESS(Status
))
2661 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2664 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName
, DeviceIndex
, &HardwareIdKey
);
2665 Status
= ZwSetValueKey(hLevel2Key
, &HardwareIDU
, 0, REG_MULTI_SZ
, pHardwareId
->Buffer
, pHardwareId
->MaximumLength
);
2666 if (!NT_SUCCESS(Status
))
2668 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
2669 ZwDeleteKey(hLevel2Key
);
2672 /* Create 'LogConf' subkey */
2673 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
, hLevel2Key
, NULL
);
2674 Status
= ZwCreateKey(
2680 REG_OPTION_VOLATILE
,
2682 if (!NT_SUCCESS(Status
))
2684 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2685 ZwDeleteKey(hLevel2Key
);
2688 if (BootResourcesLength
>= sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
2690 CmResourceList
= ExAllocatePool(PagedPool
, BootResourcesLength
+ sizeof(ULONG
));
2691 if (!CmResourceList
)
2694 ZwDeleteKey(hLevel2Key
);
2698 /* Add the list count (1st member of CM_RESOURCE_LIST) */
2700 RtlCopyMemory(CmResourceList
,
2704 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
2705 RtlCopyMemory(CmResourceList
+ sizeof(ULONG
),
2707 BootResourcesLength
);
2709 /* Save boot resources to 'LogConf\BootConfig' */
2710 Status
= ZwSetValueKey(hLogConf
, &BootConfigU
, 0, REG_RESOURCE_LIST
, CmResourceList
, BootResourcesLength
+ sizeof(ULONG
));
2711 if (!NT_SUCCESS(Status
))
2713 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
2715 ZwDeleteKey(hLevel2Key
);
2722 if (BootResources
&& BootResources
!= ParentBootResources
)
2724 ExFreePool(BootResources
);
2725 BootResources
= NULL
;
2729 ZwClose(hLevel2Key
);
2734 ZwClose(hDeviceKey
);
2739 Status
= STATUS_SUCCESS
;
2742 if (hDevicesKey
&& hDevicesKey
!= hBaseKey
)
2743 ZwClose(hDevicesKey
);
2745 ZwClose(hDeviceKey
);
2746 if (pDeviceInformation
)
2747 ExFreePool(pDeviceInformation
);
2748 if (pValueInformation
)
2749 ExFreePool(pValueInformation
);
2753 static BOOLEAN INIT_FUNCTION
2754 IopIsFirmwareMapperDisabled(VOID
)
2756 UNICODE_STRING KeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CURRENTCONTROLSET\\Control\\Pnp");
2757 UNICODE_STRING KeyNameU
= RTL_CONSTANT_STRING(L
"DisableFirmwareMapper");
2758 OBJECT_ATTRIBUTES ObjectAttributes
;
2760 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation
;
2761 ULONG DesiredLength
, Length
;
2765 InitializeObjectAttributes(&ObjectAttributes
, &KeyPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
2766 Status
= ZwOpenKey(&hPnpKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
2767 if (NT_SUCCESS(Status
))
2769 Status
= ZwQueryValueKey(hPnpKey
,
2771 KeyValuePartialInformation
,
2775 if ((Status
== STATUS_BUFFER_TOO_SMALL
) ||
2776 (Status
== STATUS_BUFFER_OVERFLOW
))
2778 Length
= DesiredLength
;
2779 KeyInformation
= ExAllocatePool(PagedPool
, Length
);
2782 Status
= ZwQueryValueKey(hPnpKey
,
2784 KeyValuePartialInformation
,
2788 if (NT_SUCCESS(Status
) && KeyInformation
->DataLength
== sizeof(ULONG
))
2790 KeyValue
= (ULONG
)(*KeyInformation
->Data
);
2794 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed\n", &KeyPathU
, &KeyNameU
);
2797 ExFreePool(KeyInformation
);
2801 DPRINT1("Failed to allocate memory for registry query\n");
2806 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed with status 0x%08lx\n", &KeyPathU
, &KeyNameU
, Status
);
2813 DPRINT1("ZwOpenKey(%wZ) failed with status 0x%08lx\n", &KeyPathU
, Status
);
2816 DPRINT1("Firmware mapper is %s\n", KeyValue
!= 0 ? "disabled" : "enabled");
2818 return (KeyValue
!= 0) ? TRUE
: FALSE
;
2824 IopUpdateRootKey(VOID
)
2826 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
2827 UNICODE_STRING RootPathU
= RTL_CONSTANT_STRING(L
"Root");
2828 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
2829 OBJECT_ATTRIBUTES ObjectAttributes
;
2830 HANDLE hEnum
, hRoot
;
2833 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
2834 Status
= ZwCreateKey(&hEnum
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
2835 if (!NT_SUCCESS(Status
))
2837 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status
);
2841 InitializeObjectAttributes(&ObjectAttributes
, &RootPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hEnum
, NULL
);
2842 Status
= ZwCreateKey(&hRoot
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
2844 if (!NT_SUCCESS(Status
))
2846 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2850 if (!IopIsFirmwareMapperDisabled())
2852 Status
= IopOpenRegistryKeyEx(&hEnum
, NULL
, &MultiKeyPathU
, KEY_ENUMERATE_SUB_KEYS
);
2853 if (!NT_SUCCESS(Status
))
2855 /* Nothing to do, don't return with an error status */
2856 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2858 return STATUS_SUCCESS
;
2860 Status
= IopEnumerateDetectedDevices(
2871 /* Enumeration is disabled */
2872 Status
= STATUS_SUCCESS
;
2882 IopOpenRegistryKeyEx(PHANDLE KeyHandle
,
2884 PUNICODE_STRING Name
,
2885 ACCESS_MASK DesiredAccess
)
2887 OBJECT_ATTRIBUTES ObjectAttributes
;
2894 InitializeObjectAttributes(&ObjectAttributes
,
2896 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2900 Status
= ZwOpenKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
);
2907 IopCreateRegistryKeyEx(OUT PHANDLE Handle
,
2908 IN HANDLE RootHandle OPTIONAL
,
2909 IN PUNICODE_STRING KeyName
,
2910 IN ACCESS_MASK DesiredAccess
,
2911 IN ULONG CreateOptions
,
2912 OUT PULONG Disposition OPTIONAL
)
2914 OBJECT_ATTRIBUTES ObjectAttributes
;
2915 ULONG KeyDisposition
, RootHandleIndex
= 0, i
= 1, NestedCloseLevel
= 0;
2917 HANDLE HandleArray
[2];
2918 BOOLEAN Recursing
= TRUE
;
2920 UNICODE_STRING KeyString
;
2921 NTSTATUS Status
= STATUS_SUCCESS
;
2924 /* P1 is start, pp is end */
2925 p1
= KeyName
->Buffer
;
2926 pp
= (PVOID
)((ULONG_PTR
)p1
+ KeyName
->Length
);
2928 /* Create the target key */
2929 InitializeObjectAttributes(&ObjectAttributes
,
2931 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2934 Status
= ZwCreateKey(&HandleArray
[i
],
2942 /* Now we check if this failed */
2943 if ((Status
== STATUS_OBJECT_NAME_NOT_FOUND
) && (RootHandle
))
2945 /* Target key failed, so we'll need to create its parent. Setup array */
2946 HandleArray
[0] = NULL
;
2947 HandleArray
[1] = RootHandle
;
2949 /* Keep recursing for each missing parent */
2952 /* And if we're deep enough, close the last handle */
2953 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
2955 /* We're setup to ping-pong between the two handle array entries */
2956 RootHandleIndex
= i
;
2959 /* Clear the one we're attempting to open now */
2960 HandleArray
[i
] = NULL
;
2962 /* Process the parent key name */
2963 for (p
= p1
; ((p
< pp
) && (*p
!= OBJ_NAME_PATH_SEPARATOR
)); p
++);
2964 Length
= (USHORT
)(p
- p1
) * sizeof(WCHAR
);
2966 /* Is there a parent name? */
2969 /* Build the unicode string for it */
2970 KeyString
.Buffer
= p1
;
2971 KeyString
.Length
= KeyString
.MaximumLength
= Length
;
2973 /* Now try opening the parent */
2974 InitializeObjectAttributes(&ObjectAttributes
,
2976 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
2977 HandleArray
[RootHandleIndex
],
2979 Status
= ZwCreateKey(&HandleArray
[i
],
2986 if (NT_SUCCESS(Status
))
2988 /* It worked, we have one more handle */
2993 /* Parent key creation failed, abandon loop */
3000 /* We don't have a parent name, probably corrupted key name */
3001 Status
= STATUS_INVALID_PARAMETER
;
3006 /* Now see if there's more parents to create */
3008 if ((p
== pp
) || (p1
== pp
))
3010 /* We're done, hopefully successfully, so stop */
3015 /* Outer loop check for handle nesting that requires closing the top handle */
3016 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3019 /* Check if we broke out of the loop due to success */
3020 if (NT_SUCCESS(Status
))
3022 /* Return the target handle (we closed all the parent ones) and disposition */
3023 *Handle
= HandleArray
[i
];
3024 if (Disposition
) *Disposition
= KeyDisposition
;
3027 /* Return the success state */
3033 IopGetRegistryValue(IN HANDLE Handle
,
3035 OUT PKEY_VALUE_FULL_INFORMATION
*Information
)
3037 UNICODE_STRING ValueString
;
3039 PKEY_VALUE_FULL_INFORMATION FullInformation
;
3043 RtlInitUnicodeString(&ValueString
, ValueName
);
3045 Status
= ZwQueryValueKey(Handle
,
3047 KeyValueFullInformation
,
3051 if ((Status
!= STATUS_BUFFER_OVERFLOW
) &&
3052 (Status
!= STATUS_BUFFER_TOO_SMALL
))
3057 FullInformation
= ExAllocatePool(NonPagedPool
, Size
);
3058 if (!FullInformation
) return STATUS_INSUFFICIENT_RESOURCES
;
3060 Status
= ZwQueryValueKey(Handle
,
3062 KeyValueFullInformation
,
3066 if (!NT_SUCCESS(Status
))
3068 ExFreePool(FullInformation
);
3072 *Information
= FullInformation
;
3073 return STATUS_SUCCESS
;
3076 RTL_GENERIC_COMPARE_RESULTS
3078 PiCompareInstancePath(IN PRTL_AVL_TABLE Table
,
3079 IN PVOID FirstStruct
,
3080 IN PVOID SecondStruct
)
3088 // The allocation function is called by the generic table package whenever
3089 // it needs to allocate memory for the table.
3094 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3104 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3113 PpInitializeDeviceReferenceTable(VOID
)
3115 /* Setup the guarded mutex and AVL table */
3116 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock
);
3117 RtlInitializeGenericTableAvl(
3118 &PpDeviceReferenceTable
,
3119 (PRTL_AVL_COMPARE_ROUTINE
)PiCompareInstancePath
,
3120 (PRTL_AVL_ALLOCATE_ROUTINE
)PiAllocateGenericTableEntry
,
3121 (PRTL_AVL_FREE_ROUTINE
)PiFreeGenericTableEntry
,
3129 /* Initialize the resource when accessing device registry data */
3130 ExInitializeResourceLite(&PpRegistryDeviceResource
);
3132 /* Setup the device reference AVL table */
3133 PpInitializeDeviceReferenceTable();
3141 /* Check the initialization phase */
3142 switch (ExpInitializationPhase
)
3147 return PiInitPhase0();
3153 //return PiInitPhase1();
3157 /* Don't know any other phase! Bugcheck! */
3158 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
3163 LONG IopNumberDeviceNodes
;
3167 PipAllocateDeviceNode(IN PDEVICE_OBJECT PhysicalDeviceObject
)
3169 PDEVICE_NODE DeviceNode
;
3173 DeviceNode
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(DEVICE_NODE
), 'donD');
3174 if (!DeviceNode
) return DeviceNode
;
3177 InterlockedIncrement(&IopNumberDeviceNodes
);
3180 RtlZeroMemory(DeviceNode
, sizeof(DEVICE_NODE
));
3181 DeviceNode
->InterfaceType
= InterfaceTypeUndefined
;
3182 DeviceNode
->BusNumber
= -1;
3183 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
3184 DeviceNode
->ChildBusNumber
= -1;
3185 DeviceNode
->ChildBusTypeIndex
= -1;
3186 // KeInitializeEvent(&DeviceNode->EnumerationMutex, SynchronizationEvent, TRUE);
3187 InitializeListHead(&DeviceNode
->DeviceArbiterList
);
3188 InitializeListHead(&DeviceNode
->DeviceTranslatorList
);
3189 InitializeListHead(&DeviceNode
->TargetDeviceNotify
);
3190 InitializeListHead(&DeviceNode
->DockInfo
.ListEntry
);
3191 InitializeListHead(&DeviceNode
->PendedSetInterfaceState
);
3193 /* Check if there is a PDO */
3194 if (PhysicalDeviceObject
)
3196 /* Link it and remove the init flag */
3197 DeviceNode
->PhysicalDeviceObject
= PhysicalDeviceObject
;
3198 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= DeviceNode
;
3199 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
3202 /* Return the node */
3206 /* PUBLIC FUNCTIONS **********************************************************/
3210 PnpBusTypeGuidGet(IN USHORT Index
,
3211 IN LPGUID BusTypeGuid
)
3213 NTSTATUS Status
= STATUS_SUCCESS
;
3215 /* Acquire the lock */
3216 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
3219 if (Index
< PnpBusTypeGuidList
->GuidCount
)
3222 RtlCopyMemory(BusTypeGuid
, &PnpBusTypeGuidList
->Guids
[Index
], sizeof(GUID
));
3227 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3230 /* Release lock and return status */
3231 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
3237 PnpDeviceObjectToDeviceInstance(IN PDEVICE_OBJECT DeviceObject
,
3238 IN PHANDLE DeviceInstanceHandle
,
3239 IN ACCESS_MASK DesiredAccess
)
3243 PDEVICE_NODE DeviceNode
;
3244 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
3247 /* Open the enum key */
3248 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
3252 if (!NT_SUCCESS(Status
)) return Status
;
3254 /* Make sure we have an instance path */
3255 DeviceNode
= IopGetDeviceNode(DeviceObject
);
3256 if ((DeviceNode
) && (DeviceNode
->InstancePath
.Length
))
3258 /* Get the instance key */
3259 Status
= IopOpenRegistryKeyEx(DeviceInstanceHandle
,
3261 &DeviceNode
->InstancePath
,
3267 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3270 /* Close the handle and return status */
3277 PnpDetermineResourceListSize(IN PCM_RESOURCE_LIST ResourceList
)
3279 ULONG FinalSize
, PartialSize
, EntrySize
, i
, j
;
3280 PCM_FULL_RESOURCE_DESCRIPTOR FullDescriptor
;
3281 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
3283 /* If we don't have one, that's easy */
3284 if (!ResourceList
) return 0;
3286 /* Start with the minimum size possible */
3287 FinalSize
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
3289 /* Loop each full descriptor */
3290 FullDescriptor
= ResourceList
->List
;
3291 for (i
= 0; i
< ResourceList
->Count
; i
++)
3293 /* Start with the minimum size possible */
3294 PartialSize
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
) +
3295 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST
, PartialDescriptors
);
3297 /* Loop each partial descriptor */
3298 PartialDescriptor
= FullDescriptor
->PartialResourceList
.PartialDescriptors
;
3299 for (j
= 0; j
< FullDescriptor
->PartialResourceList
.Count
; j
++)
3301 /* Start with the minimum size possible */
3302 EntrySize
= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
3304 /* Check if there is extra data */
3305 if (PartialDescriptor
->Type
== CmResourceTypeDeviceSpecific
)
3308 EntrySize
+= PartialDescriptor
->u
.DeviceSpecificData
.DataSize
;
3311 /* The size of partial descriptors is bigger */
3312 PartialSize
+= EntrySize
;
3314 /* Go to the next partial descriptor */
3315 PartialDescriptor
= (PVOID
)((ULONG_PTR
)PartialDescriptor
+ EntrySize
);
3318 /* The size of full descriptors is bigger */
3319 FinalSize
+= PartialSize
;
3321 /* Go to the next full descriptor */
3322 FullDescriptor
= (PVOID
)((ULONG_PTR
)FullDescriptor
+ PartialSize
);
3325 /* Return the final size */
3331 PiGetDeviceRegistryProperty(IN PDEVICE_OBJECT DeviceObject
,
3336 IN PULONG BufferLength
)
3339 HANDLE KeyHandle
, SubHandle
;
3340 UNICODE_STRING KeyString
;
3341 PKEY_VALUE_FULL_INFORMATION KeyValueInfo
= NULL
;
3345 /* Find the instance key */
3346 Status
= PnpDeviceObjectToDeviceInstance(DeviceObject
, &KeyHandle
, KEY_READ
);
3347 if (NT_SUCCESS(Status
))
3349 /* Check for name given by caller */
3353 RtlInitUnicodeString(&KeyString
, KeyName
);
3354 Status
= IopOpenRegistryKeyEx(&SubHandle
,
3358 if (NT_SUCCESS(Status
))
3360 /* And use this handle instead */
3362 KeyHandle
= SubHandle
;
3366 /* Check if sub-key handle succeeded (or no-op if no key name given) */
3367 if (NT_SUCCESS(Status
))
3369 /* Now get the size of the property */
3370 Status
= IopGetRegistryValue(KeyHandle
,
3379 /* Fail if any of the registry operations failed */
3380 if (!NT_SUCCESS(Status
)) return Status
;
3382 /* Check how much data we have to copy */
3383 Length
= KeyValueInfo
->DataLength
;
3384 if (*BufferLength
>= Length
)
3386 /* Check for a match in the value type */
3387 if (KeyValueInfo
->Type
== ValueType
)
3390 RtlCopyMemory(Buffer
,
3391 (PVOID
)((ULONG_PTR
)KeyValueInfo
+
3392 KeyValueInfo
->DataOffset
),