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 ******************************************************************/
18 /* GLOBALS *******************************************************************/
20 PDEVICE_NODE IopRootDeviceNode
;
21 KSPIN_LOCK IopDeviceTreeLock
;
22 ERESOURCE PpRegistryDeviceResource
;
23 KGUARDED_MUTEX PpDeviceReferenceTableLock
;
24 RTL_AVL_TABLE PpDeviceReferenceTable
;
26 extern ULONG ExpInitializationPhase
;
27 extern BOOLEAN PnpSystemInit
;
29 /* DATA **********************************************************************/
31 PDRIVER_OBJECT IopRootDriverObject
;
32 FAST_MUTEX IopBusTypeGuidListLock
;
33 PIO_BUS_TYPE_GUID_LIST IopBusTypeGuidList
= NULL
;
35 #if defined (ALLOC_PRAGMA)
36 #pragma alloc_text(INIT, PnpInit)
37 #pragma alloc_text(INIT, PnpInit2)
40 typedef struct _INVALIDATE_DEVICE_RELATION_DATA
42 PDEVICE_OBJECT DeviceObject
;
43 DEVICE_RELATION_TYPE Type
;
44 PIO_WORKITEM WorkItem
;
45 } INVALIDATE_DEVICE_RELATION_DATA
, *PINVALIDATE_DEVICE_RELATION_DATA
;
47 /* FUNCTIONS *****************************************************************/
50 IopAssignDeviceResources(
51 IN PDEVICE_NODE DeviceNode
,
52 OUT ULONG
*pRequiredSize
);
55 IopTranslateDeviceResources(
56 IN PDEVICE_NODE DeviceNode
,
57 IN ULONG RequiredSize
);
60 IopUpdateResourceMapForPnPDevice(
61 IN PDEVICE_NODE DeviceNode
);
65 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
70 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject
)
72 return ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
77 IopInitializeDevice(PDEVICE_NODE DeviceNode
,
78 PDRIVER_OBJECT DriverObject
)
83 if (!DriverObject
->DriverExtension
->AddDevice
)
84 return STATUS_SUCCESS
;
86 /* This is a Plug and Play driver */
87 DPRINT("Plug and Play driver found\n");
88 ASSERT(DeviceNode
->PhysicalDeviceObject
);
90 /* Check if this plug-and-play driver is used as a legacy one for this device node */
91 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
93 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
94 return STATUS_SUCCESS
;
97 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
98 &DriverObject
->DriverName
,
99 &DeviceNode
->InstancePath
);
100 Status
= DriverObject
->DriverExtension
->AddDevice(
101 DriverObject
, DeviceNode
->PhysicalDeviceObject
);
102 if (!NT_SUCCESS(Status
))
104 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
108 /* Check if driver added a FDO above the PDO */
109 Fdo
= IoGetAttachedDeviceReference(DeviceNode
->PhysicalDeviceObject
);
110 if (Fdo
== DeviceNode
->PhysicalDeviceObject
)
112 /* FIXME: What do we do? Unload the driver or just disable the device? */
113 DPRINT1("An FDO was not attached\n");
114 ObDereferenceObject(Fdo
);
115 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
116 return STATUS_UNSUCCESSFUL
;
119 /* Check if we have a ACPI device (needed for power management) */
120 if (Fdo
->DeviceType
== FILE_DEVICE_ACPI
)
122 static BOOLEAN SystemPowerDeviceNodeCreated
= FALSE
;
124 /* There can be only one system power device */
125 if (!SystemPowerDeviceNodeCreated
)
127 PopSystemPowerDeviceNode
= DeviceNode
;
128 ObReferenceObject(PopSystemPowerDeviceNode
);
129 SystemPowerDeviceNodeCreated
= TRUE
;
133 ObDereferenceObject(Fdo
);
135 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
136 IopDeviceNodeSetFlag(DeviceNode
, DNF_NEED_ENUMERATION_ONLY
);
138 return STATUS_SUCCESS
;
143 PDEVICE_NODE DeviceNode
)
145 IO_STATUS_BLOCK IoStatusBlock
;
146 IO_STACK_LOCATION Stack
;
147 ULONG RequiredLength
;
149 HANDLE InstanceHandle
, ControlHandle
;
150 UNICODE_STRING KeyName
;
151 OBJECT_ATTRIBUTES ObjectAttributes
;
153 IopDeviceNodeSetFlag(DeviceNode
, DNF_ASSIGNING_RESOURCES
);
154 DPRINT("Sending IRP_MN_FILTER_RESOURCE_REQUIREMENTS to device stack\n");
155 Stack
.Parameters
.FilterResourceRequirements
.IoResourceRequirementList
= DeviceNode
->ResourceRequirements
;
156 Status
= IopInitiatePnpIrp(
157 DeviceNode
->PhysicalDeviceObject
,
159 IRP_MN_FILTER_RESOURCE_REQUIREMENTS
,
161 if (!NT_SUCCESS(Status
) && Status
!= STATUS_NOT_SUPPORTED
)
163 DPRINT("IopInitiatePnpIrp(IRP_MN_FILTER_RESOURCE_REQUIREMENTS) failed\n");
166 DeviceNode
->ResourceRequirements
= (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
168 Status
= IopAssignDeviceResources(DeviceNode
, &RequiredLength
);
169 if (NT_SUCCESS(Status
))
171 Status
= IopTranslateDeviceResources(DeviceNode
, RequiredLength
);
172 if (NT_SUCCESS(Status
))
174 Status
= IopUpdateResourceMapForPnPDevice(DeviceNode
);
175 if (!NT_SUCCESS(Status
))
177 DPRINT("IopUpdateResourceMap() failed (Status 0x%08lx)\n", Status
);
182 DPRINT("IopTranslateDeviceResources() failed (Status 0x%08lx)\n", Status
);
187 DPRINT("IopAssignDeviceResources() failed (Status 0x%08lx)\n", Status
);
189 IopDeviceNodeClearFlag(DeviceNode
, DNF_ASSIGNING_RESOURCES
);
191 DPRINT("Sending IRP_MN_START_DEVICE to driver\n");
192 Stack
.Parameters
.StartDevice
.AllocatedResources
= DeviceNode
->ResourceList
;
193 Stack
.Parameters
.StartDevice
.AllocatedResourcesTranslated
= DeviceNode
->ResourceListTranslated
;
196 * Windows NT Drivers receive IRP_MN_START_DEVICE in a critical region and
197 * actually _depend_ on this!. This is because NT will lock the Device Node
198 * with an ERESOURCE, which of course requires APCs to be disabled.
200 KeEnterCriticalRegion();
202 Status
= IopInitiatePnpIrp(
203 DeviceNode
->PhysicalDeviceObject
,
208 KeLeaveCriticalRegion();
210 if (!NT_SUCCESS(Status
))
212 DPRINT("IopInitiatePnpIrp() failed\n");
216 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_NEED_ENUMERATION_ONLY
))
218 DPRINT("Device needs enumeration, invalidating bus relations\n");
219 /* Invalidate device relations synchronously
220 (otherwise there will be dirty read of DeviceNode) */
221 IopEnumerateDevice(DeviceNode
->PhysicalDeviceObject
);
222 IopDeviceNodeClearFlag(DeviceNode
, DNF_NEED_ENUMERATION_ONLY
);
226 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, &InstanceHandle
);
227 if (!NT_SUCCESS(Status
))
230 RtlInitUnicodeString(&KeyName
, L
"Control");
231 InitializeObjectAttributes(&ObjectAttributes
,
233 OBJ_CASE_INSENSITIVE
,
236 Status
= ZwCreateKey(&ControlHandle
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
237 if (!NT_SUCCESS(Status
))
239 ZwClose(InstanceHandle
);
243 RtlInitUnicodeString(&KeyName
, L
"ActiveService");
244 Status
= ZwSetValueKey(ControlHandle
, &KeyName
, 0, REG_SZ
, DeviceNode
->ServiceName
.Buffer
, DeviceNode
->ServiceName
.Length
);
246 if (NT_SUCCESS(Status
))
247 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
249 ZwClose(ControlHandle
);
250 ZwClose(InstanceHandle
);
257 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode
,
258 PDEVICE_CAPABILITIES DeviceCaps
)
260 IO_STATUS_BLOCK StatusBlock
;
261 IO_STACK_LOCATION Stack
;
263 /* Set up the Header */
264 RtlZeroMemory(DeviceCaps
, sizeof(DEVICE_CAPABILITIES
));
265 DeviceCaps
->Size
= sizeof(DEVICE_CAPABILITIES
);
266 DeviceCaps
->Version
= 1;
267 DeviceCaps
->Address
= -1;
268 DeviceCaps
->UINumber
= -1;
270 /* Set up the Stack */
271 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
272 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= DeviceCaps
;
275 return IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
277 IRP_MN_QUERY_CAPABILITIES
,
282 IopAsynchronousInvalidateDeviceRelations(
283 IN PDEVICE_OBJECT DeviceObject
,
284 IN PVOID InvalidateContext
)
286 PINVALIDATE_DEVICE_RELATION_DATA Data
= InvalidateContext
;
288 IoSynchronousInvalidateDeviceRelations(
292 ObDereferenceObject(Data
->DeviceObject
);
293 IoFreeWorkItem(Data
->WorkItem
);
298 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT
*DeviceObject
)
302 if (PopSystemPowerDeviceNode
)
304 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
305 *DeviceObject
= PopSystemPowerDeviceNode
->PhysicalDeviceObject
;
306 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
308 return STATUS_SUCCESS
;
311 return STATUS_UNSUCCESSFUL
;
316 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid
)
318 USHORT i
= 0, FoundIndex
= 0xFFFF;
322 /* Acquire the lock */
323 ExAcquireFastMutex(&IopBusTypeGuidListLock
);
325 /* Loop all entries */
326 while (i
< IopBusTypeGuidList
->GuidCount
)
328 /* Try to find a match */
329 if (RtlCompareMemory(BusTypeGuid
,
330 &IopBusTypeGuidList
->Guids
[i
],
331 sizeof(GUID
)) == sizeof(GUID
))
340 /* Check if we have to grow the list */
341 if (IopBusTypeGuidList
->GuidCount
)
343 /* Calculate the new size */
344 NewSize
= sizeof(IO_BUS_TYPE_GUID_LIST
) +
345 (sizeof(GUID
) * IopBusTypeGuidList
->GuidCount
);
347 /* Allocate the new copy */
348 NewList
= ExAllocatePool(PagedPool
, NewSize
);
352 ExFreePool(IopBusTypeGuidList
);
356 /* Now copy them, decrease the size too */
357 NewSize
-= sizeof(GUID
);
358 RtlCopyMemory(NewList
, IopBusTypeGuidList
, NewSize
);
360 /* Free the old list */
361 ExFreePool(IopBusTypeGuidList
);
363 /* Use the new buffer */
364 IopBusTypeGuidList
= NewList
;
367 /* Copy the new GUID */
368 RtlCopyMemory(&IopBusTypeGuidList
->Guids
[IopBusTypeGuidList
->GuidCount
],
372 /* The new entry is the index */
373 FoundIndex
= (USHORT
)IopBusTypeGuidList
->GuidCount
;
374 IopBusTypeGuidList
->GuidCount
++;
377 ExReleaseFastMutex(&IopBusTypeGuidListLock
);
383 * Creates a device node
386 * ParentNode = Pointer to parent device node
387 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
388 * to have the root device node create one
389 * (eg. for legacy drivers)
390 * DeviceNode = Pointer to storage for created device node
396 IopCreateDeviceNode(PDEVICE_NODE ParentNode
,
397 PDEVICE_OBJECT PhysicalDeviceObject
,
398 PUNICODE_STRING ServiceName
,
399 PDEVICE_NODE
*DeviceNode
)
404 UNICODE_STRING FullServiceName
;
405 UNICODE_STRING LegacyPrefix
= RTL_CONSTANT_STRING(L
"LEGACY_");
406 UNICODE_STRING UnknownDeviceName
= RTL_CONSTANT_STRING(L
"UNKNOWN");
409 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
410 ParentNode
, PhysicalDeviceObject
, ServiceName
);
412 Node
= (PDEVICE_NODE
)ExAllocatePool(NonPagedPool
, sizeof(DEVICE_NODE
));
415 return STATUS_INSUFFICIENT_RESOURCES
;
418 RtlZeroMemory(Node
, sizeof(DEVICE_NODE
));
421 ServiceName
= &UnknownDeviceName
;
423 if (!PhysicalDeviceObject
)
425 FullServiceName
.MaximumLength
= LegacyPrefix
.Length
+ ServiceName
->Length
;
426 FullServiceName
.Length
= 0;
427 FullServiceName
.Buffer
= ExAllocatePool(PagedPool
, FullServiceName
.MaximumLength
);
428 if (!FullServiceName
.Buffer
)
431 return STATUS_INSUFFICIENT_RESOURCES
;
434 RtlAppendUnicodeStringToString(&FullServiceName
, &LegacyPrefix
);
435 RtlAppendUnicodeStringToString(&FullServiceName
, ServiceName
);
437 Status
= PnpRootCreateDevice(&FullServiceName
, &PhysicalDeviceObject
, &Node
->InstancePath
);
438 if (!NT_SUCCESS(Status
))
440 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status
);
445 /* Create the device key for legacy drivers */
446 Status
= IopCreateDeviceKeyPath(&Node
->InstancePath
, &TempHandle
);
447 if (NT_SUCCESS(Status
))
450 ExFreePool(FullServiceName
.Buffer
);
452 /* This is for drivers passed on the command line to ntoskrnl.exe */
453 IopDeviceNodeSetFlag(Node
, DNF_STARTED
);
454 IopDeviceNodeSetFlag(Node
, DNF_LEGACY_DRIVER
);
457 Node
->PhysicalDeviceObject
= PhysicalDeviceObject
;
459 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= Node
;
463 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
464 Node
->Parent
= ParentNode
;
465 Node
->Sibling
= ParentNode
->Child
;
466 ParentNode
->Child
= Node
;
467 if (ParentNode
->LastChild
== NULL
)
468 ParentNode
->LastChild
= Node
;
469 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
470 Node
->Level
= ParentNode
->Level
+ 1;
473 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
477 return STATUS_SUCCESS
;
481 IopFreeDeviceNode(PDEVICE_NODE DeviceNode
)
484 PDEVICE_NODE PrevSibling
= NULL
;
486 /* All children must be deleted before a parent is deleted */
487 ASSERT(!DeviceNode
->Child
);
489 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
491 ASSERT(DeviceNode
->PhysicalDeviceObject
);
493 ObDereferenceObject(DeviceNode
->PhysicalDeviceObject
);
495 /* Get previous sibling */
496 if (DeviceNode
->Parent
&& DeviceNode
->Parent
->Child
!= DeviceNode
)
498 PrevSibling
= DeviceNode
->Parent
->Child
;
499 while (PrevSibling
->Sibling
!= DeviceNode
)
500 PrevSibling
= PrevSibling
->Sibling
;
503 /* Unlink from parent if it exists */
504 if (DeviceNode
->Parent
)
506 if (DeviceNode
->Parent
->LastChild
== DeviceNode
)
508 DeviceNode
->Parent
->LastChild
= PrevSibling
;
510 PrevSibling
->Sibling
= NULL
;
512 if (DeviceNode
->Parent
->Child
== DeviceNode
)
513 DeviceNode
->Parent
->Child
= DeviceNode
->Sibling
;
516 /* Unlink from sibling list */
518 PrevSibling
->Sibling
= DeviceNode
->Sibling
;
520 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
522 RtlFreeUnicodeString(&DeviceNode
->InstancePath
);
524 RtlFreeUnicodeString(&DeviceNode
->ServiceName
);
526 if (DeviceNode
->ResourceList
)
528 ExFreePool(DeviceNode
->ResourceList
);
531 if (DeviceNode
->ResourceListTranslated
)
533 ExFreePool(DeviceNode
->ResourceListTranslated
);
536 if (DeviceNode
->ResourceRequirements
)
538 ExFreePool(DeviceNode
->ResourceRequirements
);
541 if (DeviceNode
->BootResources
)
543 ExFreePool(DeviceNode
->BootResources
);
546 ExFreePool(DeviceNode
);
548 return STATUS_SUCCESS
;
552 IopInitiatePnpIrp(PDEVICE_OBJECT DeviceObject
,
553 PIO_STATUS_BLOCK IoStatusBlock
,
555 PIO_STACK_LOCATION Stack OPTIONAL
)
557 PDEVICE_OBJECT TopDeviceObject
;
558 PIO_STACK_LOCATION IrpSp
;
563 /* Always call the top of the device stack */
564 TopDeviceObject
= IoGetAttachedDeviceReference(DeviceObject
);
571 Irp
= IoBuildSynchronousFsdRequest(
580 /* PNP IRPs are initialized with a status code of STATUS_NOT_SUPPORTED */
581 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
582 Irp
->IoStatus
.Information
= 0;
584 if (MinorFunction
== IRP_MN_FILTER_RESOURCE_REQUIREMENTS
)
586 Irp
->IoStatus
.Information
= (ULONG_PTR
)Stack
->Parameters
.FilterResourceRequirements
.IoResourceRequirementList
;
589 IrpSp
= IoGetNextIrpStackLocation(Irp
);
590 IrpSp
->MinorFunction
= (UCHAR
)MinorFunction
;
594 RtlCopyMemory(&IrpSp
->Parameters
,
596 sizeof(Stack
->Parameters
));
599 Status
= IoCallDriver(TopDeviceObject
, Irp
);
600 if (Status
== STATUS_PENDING
)
602 KeWaitForSingleObject(&Event
,
607 Status
= IoStatusBlock
->Status
;
610 ObDereferenceObject(TopDeviceObject
);
617 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context
)
619 PDEVICE_NODE ParentDeviceNode
;
620 PDEVICE_NODE ChildDeviceNode
;
623 /* Copy context data so we don't overwrite it in subsequent calls to this function */
624 ParentDeviceNode
= Context
->DeviceNode
;
626 /* Call the action routine */
627 Status
= (Context
->Action
)(ParentDeviceNode
, Context
->Context
);
628 if (!NT_SUCCESS(Status
))
633 /* Traversal of all children nodes */
634 for (ChildDeviceNode
= ParentDeviceNode
->Child
;
635 ChildDeviceNode
!= NULL
;
636 ChildDeviceNode
= ChildDeviceNode
->Sibling
)
638 /* Pass the current device node to the action routine */
639 Context
->DeviceNode
= ChildDeviceNode
;
641 Status
= IopTraverseDeviceTreeNode(Context
);
642 if (!NT_SUCCESS(Status
))
653 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context
)
657 DPRINT("Context 0x%p\n", Context
);
659 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
660 Context
->DeviceNode
, Context
->FirstDeviceNode
, Context
->Action
, Context
->Context
);
662 /* Start from the specified device node */
663 Context
->DeviceNode
= Context
->FirstDeviceNode
;
665 /* Recursively traverse the device tree */
666 Status
= IopTraverseDeviceTreeNode(Context
);
667 if (Status
== STATUS_UNSUCCESSFUL
)
669 /* The action routine just wanted to terminate the traversal with status
670 code STATUS_SUCCESS */
671 Status
= STATUS_SUCCESS
;
679 * IopCreateDeviceKeyPath
681 * Creates a registry key
685 * Name of the key to be created.
687 * Handle to the newly created key
690 * This method can create nested trees, so parent of RegistryPath can
691 * be not existant, and will be created if needed.
695 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
698 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(ENUM_ROOT
);
699 HANDLE hParent
= NULL
, hKey
;
700 OBJECT_ATTRIBUTES ObjectAttributes
;
701 UNICODE_STRING KeyName
;
702 LPCWSTR Current
, Last
;
709 /* Open root key for device instances */
710 Status
= IopOpenRegistryKeyEx(&hParent
, NULL
, &EnumU
, KEY_CREATE_SUB_KEY
);
711 if (!NT_SUCCESS(Status
))
713 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
717 Current
= KeyName
.Buffer
= RegistryPath
->Buffer
;
718 Last
= &RegistryPath
->Buffer
[RegistryPath
->Length
/ sizeof(WCHAR
)];
720 /* Go up to the end of the string */
721 while (Current
<= Last
)
723 if (Current
!= Last
&& *Current
!= '\\')
725 /* Not the end of the string and not a separator */
730 /* Prepare relative key name */
731 dwLength
= (ULONG_PTR
)Current
- (ULONG_PTR
)KeyName
.Buffer
;
732 KeyName
.MaximumLength
= KeyName
.Length
= dwLength
;
733 DPRINT("Create '%wZ'\n", &KeyName
);
736 InitializeObjectAttributes(&ObjectAttributes
,
738 OBJ_CASE_INSENSITIVE
,
741 Status
= ZwCreateKey(&hKey
,
742 Current
== Last
? KEY_ALL_ACCESS
: KEY_CREATE_SUB_KEY
,
749 /* Close parent key handle, we don't need it anymore */
753 /* Key opening/creating failed? */
754 if (!NT_SUCCESS(Status
))
756 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName
, Status
);
760 /* Check if it is the end of the string */
763 /* Yes, return success */
765 return STATUS_SUCCESS
;
768 /* Start with this new parent key */
771 KeyName
.Buffer
= (LPWSTR
)Current
;
774 return STATUS_UNSUCCESSFUL
;
778 IopUpdateResourceMap(IN PDEVICE_NODE DeviceNode
, PWCHAR Level1Key
, PWCHAR Level2Key
)
782 HANDLE PnpMgrLevel1
, PnpMgrLevel2
, ResourceMapKey
;
783 UNICODE_STRING KeyName
;
784 OBJECT_ATTRIBUTES ObjectAttributes
;
786 RtlInitUnicodeString(&KeyName
,
787 L
"\\Registry\\Machine\\HARDWARE\\RESOURCEMAP");
788 InitializeObjectAttributes(&ObjectAttributes
,
790 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
793 Status
= ZwCreateKey(&ResourceMapKey
,
800 if (!NT_SUCCESS(Status
))
803 RtlInitUnicodeString(&KeyName
, Level1Key
);
804 InitializeObjectAttributes(&ObjectAttributes
,
806 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
809 Status
= ZwCreateKey(&PnpMgrLevel1
,
816 ZwClose(ResourceMapKey
);
817 if (!NT_SUCCESS(Status
))
820 RtlInitUnicodeString(&KeyName
, Level2Key
);
821 InitializeObjectAttributes(&ObjectAttributes
,
823 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
826 Status
= ZwCreateKey(&PnpMgrLevel2
,
833 ZwClose(PnpMgrLevel1
);
834 if (!NT_SUCCESS(Status
))
837 if (DeviceNode
->ResourceList
)
840 UNICODE_STRING NameU
;
841 UNICODE_STRING Suffix
;
844 ASSERT(DeviceNode
->ResourceListTranslated
);
846 NameU
.Buffer
= NameBuff
;
848 NameU
.MaximumLength
= 256 * sizeof(WCHAR
);
850 Status
= IoGetDeviceProperty(DeviceNode
->PhysicalDeviceObject
,
851 DevicePropertyPhysicalDeviceObjectName
,
855 ASSERT(Status
== STATUS_SUCCESS
);
857 NameU
.Length
= (USHORT
)OldLength
;
859 RtlInitUnicodeString(&Suffix
, L
".Raw");
860 RtlAppendUnicodeStringToString(&NameU
, &Suffix
);
862 Status
= ZwSetValueKey(PnpMgrLevel2
,
866 DeviceNode
->ResourceList
,
867 CM_RESOURCE_LIST_SIZE(DeviceNode
->ResourceList
));
868 if (!NT_SUCCESS(Status
))
870 ZwClose(PnpMgrLevel2
);
874 /* "Remove" the suffix by setting the length back to what it used to be */
875 NameU
.Length
= (USHORT
)OldLength
;
877 RtlInitUnicodeString(&Suffix
, L
".Translated");
878 RtlAppendUnicodeStringToString(&NameU
, &Suffix
);
880 Status
= ZwSetValueKey(PnpMgrLevel2
,
884 DeviceNode
->ResourceListTranslated
,
885 CM_RESOURCE_LIST_SIZE(DeviceNode
->ResourceListTranslated
));
886 ZwClose(PnpMgrLevel2
);
887 if (!NT_SUCCESS(Status
))
892 ZwClose(PnpMgrLevel2
);
895 IopDeviceNodeSetFlag(DeviceNode
, DNF_RESOURCE_ASSIGNED
);
897 return STATUS_SUCCESS
;
901 IopUpdateResourceMapForPnPDevice(IN PDEVICE_NODE DeviceNode
)
903 return IopUpdateResourceMap(DeviceNode
, L
"PnP Manager", L
"PnpManager");
907 IopSetDeviceInstanceData(HANDLE InstanceKey
,
908 PDEVICE_NODE DeviceNode
)
910 OBJECT_ATTRIBUTES ObjectAttributes
;
911 UNICODE_STRING KeyName
;
914 ULONG ListSize
, ResultLength
;
916 HANDLE ControlHandle
;
918 DPRINT("IopSetDeviceInstanceData() called\n");
920 /* Create the 'LogConf' key */
921 RtlInitUnicodeString(&KeyName
, L
"LogConf");
922 InitializeObjectAttributes(&ObjectAttributes
,
924 OBJ_CASE_INSENSITIVE
,
927 Status
= ZwCreateKey(&LogConfKey
,
934 if (NT_SUCCESS(Status
))
936 /* Set 'BootConfig' value */
937 if (DeviceNode
->BootResources
!= NULL
)
939 ResCount
= DeviceNode
->BootResources
->Count
;
942 ListSize
= CM_RESOURCE_LIST_SIZE(DeviceNode
->BootResources
);
944 RtlInitUnicodeString(&KeyName
, L
"BootConfig");
945 Status
= ZwSetValueKey(LogConfKey
,
949 DeviceNode
->BootResources
,
954 /* Set 'BasicConfigVector' value */
955 if (DeviceNode
->ResourceRequirements
!= NULL
&&
956 DeviceNode
->ResourceRequirements
->ListSize
!= 0)
958 RtlInitUnicodeString(&KeyName
, L
"BasicConfigVector");
959 Status
= ZwSetValueKey(LogConfKey
,
962 REG_RESOURCE_REQUIREMENTS_LIST
,
963 DeviceNode
->ResourceRequirements
,
964 DeviceNode
->ResourceRequirements
->ListSize
);
970 /* Set the 'ConfigFlags' value */
971 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
972 Status
= ZwQueryValueKey(InstanceKey
,
974 KeyValueBasicInformation
,
978 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
980 /* Write the default value */
981 ULONG DefaultConfigFlags
= 0;
982 Status
= ZwSetValueKey(InstanceKey
,
987 sizeof(DefaultConfigFlags
));
990 /* Create the 'Control' key */
991 RtlInitUnicodeString(&KeyName
, L
"Control");
992 InitializeObjectAttributes(&ObjectAttributes
,
994 OBJ_CASE_INSENSITIVE
,
997 Status
= ZwCreateKey(&ControlHandle
, 0, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
999 if (NT_SUCCESS(Status
))
1000 ZwClose(ControlHandle
);
1002 DPRINT("IopSetDeviceInstanceData() done\n");
1008 IopCheckResourceDescriptor(
1009 IN PCM_PARTIAL_RESOURCE_DESCRIPTOR ResDesc
,
1010 IN PCM_RESOURCE_LIST ResourceList
,
1012 OUT OPTIONAL PCM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDescriptor
)
1015 BOOLEAN Result
= FALSE
;
1017 if (ResDesc
->ShareDisposition
== CmResourceShareShared
)
1020 for (i
= 0; i
< ResourceList
->Count
; i
++)
1022 PCM_PARTIAL_RESOURCE_LIST ResList
= &ResourceList
->List
[i
].PartialResourceList
;
1023 for (ii
= 0; ii
< ResList
->Count
; ii
++)
1025 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResDesc2
= &ResList
->PartialDescriptors
[ii
];
1027 /* We don't care about shared resources */
1028 if (ResDesc
->ShareDisposition
== CmResourceShareShared
&&
1029 ResDesc2
->ShareDisposition
== CmResourceShareShared
)
1032 /* Make sure we're comparing the same types */
1033 if (ResDesc
->Type
!= ResDesc2
->Type
)
1036 switch (ResDesc
->Type
)
1038 case CmResourceTypeMemory
:
1039 if ((ResDesc
->u
.Memory
.Start
.QuadPart
< ResDesc2
->u
.Memory
.Start
.QuadPart
&&
1040 ResDesc
->u
.Memory
.Start
.QuadPart
+ ResDesc
->u
.Memory
.Length
>
1041 ResDesc2
->u
.Memory
.Start
.QuadPart
) || (ResDesc2
->u
.Memory
.Start
.QuadPart
<
1042 ResDesc
->u
.Memory
.Start
.QuadPart
&& ResDesc2
->u
.Memory
.Start
.QuadPart
+
1043 ResDesc2
->u
.Memory
.Length
> ResDesc
->u
.Memory
.Start
.QuadPart
))
1047 DPRINT1("Resource conflict: Memory (0x%x to 0x%x vs. 0x%x to 0x%x)\n",
1048 ResDesc
->u
.Memory
.Start
.QuadPart
, ResDesc
->u
.Memory
.Start
.QuadPart
+
1049 ResDesc
->u
.Memory
.Length
, ResDesc2
->u
.Memory
.Start
.QuadPart
,
1050 ResDesc2
->u
.Memory
.Start
.QuadPart
+ ResDesc2
->u
.Memory
.Length
);
1059 case CmResourceTypePort
:
1060 if ((ResDesc
->u
.Port
.Start
.QuadPart
< ResDesc2
->u
.Port
.Start
.QuadPart
&&
1061 ResDesc
->u
.Port
.Start
.QuadPart
+ ResDesc
->u
.Port
.Length
>
1062 ResDesc2
->u
.Port
.Start
.QuadPart
) || (ResDesc2
->u
.Port
.Start
.QuadPart
<
1063 ResDesc
->u
.Port
.Start
.QuadPart
&& ResDesc2
->u
.Port
.Start
.QuadPart
+
1064 ResDesc2
->u
.Port
.Length
> ResDesc
->u
.Port
.Start
.QuadPart
))
1068 DPRINT1("Resource conflict: Port (0x%x to 0x%x vs. 0x%x to 0x%x)\n",
1069 ResDesc
->u
.Port
.Start
.QuadPart
, ResDesc
->u
.Port
.Start
.QuadPart
+
1070 ResDesc
->u
.Port
.Length
, ResDesc2
->u
.Port
.Start
.QuadPart
,
1071 ResDesc2
->u
.Port
.Start
.QuadPart
+ ResDesc2
->u
.Port
.Length
);
1080 case CmResourceTypeInterrupt
:
1081 if (ResDesc
->u
.Interrupt
.Vector
== ResDesc2
->u
.Interrupt
.Vector
)
1085 DPRINT1("Resource conflict: IRQ (0x%x 0x%x vs. 0x%x 0x%x)\n",
1086 ResDesc
->u
.Interrupt
.Vector
, ResDesc
->u
.Interrupt
.Level
,
1087 ResDesc2
->u
.Interrupt
.Vector
, ResDesc2
->u
.Interrupt
.Level
);
1096 case CmResourceTypeBusNumber
:
1097 if ((ResDesc
->u
.BusNumber
.Start
< ResDesc2
->u
.BusNumber
.Start
&&
1098 ResDesc
->u
.BusNumber
.Start
+ ResDesc
->u
.BusNumber
.Length
>
1099 ResDesc2
->u
.BusNumber
.Start
) || (ResDesc2
->u
.BusNumber
.Start
<
1100 ResDesc
->u
.BusNumber
.Start
&& ResDesc2
->u
.BusNumber
.Start
+
1101 ResDesc2
->u
.BusNumber
.Length
> ResDesc
->u
.BusNumber
.Start
))
1105 DPRINT1("Resource conflict: Bus number (0x%x to 0x%x vs. 0x%x to 0x%x)\n",
1106 ResDesc
->u
.BusNumber
.Start
, ResDesc
->u
.BusNumber
.Start
+
1107 ResDesc
->u
.BusNumber
.Length
, ResDesc2
->u
.BusNumber
.Start
,
1108 ResDesc2
->u
.BusNumber
.Start
+ ResDesc2
->u
.BusNumber
.Length
);
1117 case CmResourceTypeDma
:
1118 if (ResDesc
->u
.Dma
.Channel
== ResDesc2
->u
.Dma
.Channel
)
1122 DPRINT1("Resource conflict: Dma (0x%x 0x%x vs. 0x%x 0x%x)\n",
1123 ResDesc
->u
.Dma
.Channel
, ResDesc
->u
.Dma
.Port
,
1124 ResDesc2
->u
.Dma
.Channel
, ResDesc2
->u
.Dma
.Port
);
1138 if (Result
&& ConflictingDescriptor
)
1140 RtlCopyMemory(ConflictingDescriptor
,
1142 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
));
1150 IopCheckForResourceConflict(
1151 IN PCM_RESOURCE_LIST ResourceList1
,
1152 IN PCM_RESOURCE_LIST ResourceList2
,
1154 OUT OPTIONAL PCM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDescriptor
)
1157 BOOLEAN Result
= FALSE
;
1159 for (i
= 0; i
< ResourceList1
->Count
; i
++)
1161 PCM_PARTIAL_RESOURCE_LIST ResList
= &ResourceList1
->List
[i
].PartialResourceList
;
1162 for (ii
= 0; ii
< ResList
->Count
; ii
++)
1164 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResDesc
= &ResList
->PartialDescriptors
[ii
];
1166 Result
= IopCheckResourceDescriptor(ResDesc
,
1169 ConflictingDescriptor
);
1170 if (Result
) goto ByeBye
;
1181 IopDetectResourceConflict(
1182 IN PCM_RESOURCE_LIST ResourceList
,
1184 OUT OPTIONAL PCM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDescriptor
)
1186 OBJECT_ATTRIBUTES ObjectAttributes
;
1187 UNICODE_STRING KeyName
;
1188 HANDLE ResourceMapKey
= INVALID_HANDLE_VALUE
, ChildKey2
= INVALID_HANDLE_VALUE
, ChildKey3
= INVALID_HANDLE_VALUE
;
1189 ULONG KeyInformationLength
, RequiredLength
, KeyValueInformationLength
, KeyNameInformationLength
;
1190 PKEY_BASIC_INFORMATION KeyInformation
;
1191 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInformation
;
1192 PKEY_VALUE_BASIC_INFORMATION KeyNameInformation
;
1193 ULONG ChildKeyIndex1
= 0, ChildKeyIndex2
= 0, ChildKeyIndex3
= 0;
1196 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE\\RESOURCEMAP");
1197 InitializeObjectAttributes(&ObjectAttributes
, &KeyName
, OBJ_CASE_INSENSITIVE
, 0, NULL
);
1198 Status
= ZwOpenKey(&ResourceMapKey
, KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
, &ObjectAttributes
);
1199 if (!NT_SUCCESS(Status
))
1201 /* The key is missing which means we are the first device */
1202 return STATUS_SUCCESS
;
1207 Status
= ZwEnumerateKey(ResourceMapKey
,
1209 KeyBasicInformation
,
1213 if (Status
== STATUS_NO_MORE_ENTRIES
)
1215 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
1217 KeyInformationLength
= RequiredLength
;
1218 KeyInformation
= ExAllocatePool(PagedPool
, KeyInformationLength
);
1219 if (!KeyInformation
)
1221 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1225 Status
= ZwEnumerateKey(ResourceMapKey
,
1227 KeyBasicInformation
,
1229 KeyInformationLength
,
1235 if (!NT_SUCCESS(Status
))
1238 KeyName
.Buffer
= KeyInformation
->Name
;
1239 KeyName
.MaximumLength
= KeyName
.Length
= KeyInformation
->NameLength
;
1240 InitializeObjectAttributes(&ObjectAttributes
,
1242 OBJ_CASE_INSENSITIVE
,
1245 Status
= ZwOpenKey(&ChildKey2
, KEY_ENUMERATE_SUB_KEYS
| KEY_QUERY_VALUE
, &ObjectAttributes
);
1246 ExFreePool(KeyInformation
);
1247 if (!NT_SUCCESS(Status
))
1252 Status
= ZwEnumerateKey(ChildKey2
,
1254 KeyBasicInformation
,
1258 if (Status
== STATUS_NO_MORE_ENTRIES
)
1260 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
1262 KeyInformationLength
= RequiredLength
;
1263 KeyInformation
= ExAllocatePool(PagedPool
, KeyInformationLength
);
1264 if (!KeyInformation
)
1266 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1270 Status
= ZwEnumerateKey(ChildKey2
,
1272 KeyBasicInformation
,
1274 KeyInformationLength
,
1280 if (!NT_SUCCESS(Status
))
1283 KeyName
.Buffer
= KeyInformation
->Name
;
1284 KeyName
.MaximumLength
= KeyName
.Length
= KeyInformation
->NameLength
;
1285 InitializeObjectAttributes(&ObjectAttributes
,
1287 OBJ_CASE_INSENSITIVE
,
1290 Status
= ZwOpenKey(&ChildKey3
, KEY_QUERY_VALUE
, &ObjectAttributes
);
1291 ExFreePool(KeyInformation
);
1292 if (!NT_SUCCESS(Status
))
1297 Status
= ZwEnumerateValueKey(ChildKey3
,
1299 KeyValuePartialInformation
,
1303 if (Status
== STATUS_NO_MORE_ENTRIES
)
1305 else if (Status
== STATUS_BUFFER_TOO_SMALL
)
1307 KeyValueInformationLength
= RequiredLength
;
1308 KeyValueInformation
= ExAllocatePool(PagedPool
, KeyValueInformationLength
);
1309 if (!KeyValueInformation
)
1311 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1315 Status
= ZwEnumerateValueKey(ChildKey3
,
1317 KeyValuePartialInformation
,
1318 KeyValueInformation
,
1319 KeyValueInformationLength
,
1324 if (!NT_SUCCESS(Status
))
1327 Status
= ZwEnumerateValueKey(ChildKey3
,
1329 KeyValueBasicInformation
,
1333 if (Status
== STATUS_BUFFER_TOO_SMALL
)
1335 KeyNameInformationLength
= RequiredLength
;
1336 KeyNameInformation
= ExAllocatePool(PagedPool
, KeyNameInformationLength
+ sizeof(WCHAR
));
1337 if (!KeyNameInformation
)
1339 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1343 Status
= ZwEnumerateValueKey(ChildKey3
,
1345 KeyValueBasicInformation
,
1347 KeyNameInformationLength
,
1355 if (!NT_SUCCESS(Status
))
1358 KeyNameInformation
->Name
[KeyNameInformation
->NameLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1360 /* Skip translated entries */
1361 if (wcsstr(KeyNameInformation
->Name
, L
".Translated"))
1363 ExFreePool(KeyNameInformation
);
1367 ExFreePool(KeyNameInformation
);
1369 if (IopCheckForResourceConflict(ResourceList
,
1370 (PCM_RESOURCE_LIST
)KeyValueInformation
->Data
,
1372 ConflictingDescriptor
))
1374 ExFreePool(KeyValueInformation
);
1375 Status
= STATUS_CONFLICTING_ADDRESSES
;
1379 ExFreePool(KeyValueInformation
);
1385 if (ResourceMapKey
!= INVALID_HANDLE_VALUE
)
1386 ZwClose(ResourceMapKey
);
1387 if (ChildKey2
!= INVALID_HANDLE_VALUE
)
1389 if (ChildKey3
!= INVALID_HANDLE_VALUE
)
1392 if (Status
== STATUS_NO_MORE_ENTRIES
)
1393 Status
= STATUS_SUCCESS
;
1399 IopCheckDescriptorForConflict(PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
, OPTIONAL PCM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDescriptor
)
1401 CM_RESOURCE_LIST CmList
;
1405 CmList
.List
[0].InterfaceType
= InterfaceTypeUndefined
;
1406 CmList
.List
[0].BusNumber
= 0;
1407 CmList
.List
[0].PartialResourceList
.Version
= 1;
1408 CmList
.List
[0].PartialResourceList
.Revision
= 1;
1409 CmList
.List
[0].PartialResourceList
.Count
= 1;
1410 CmList
.List
[0].PartialResourceList
.PartialDescriptors
[0] = *CmDesc
;
1412 Status
= IopDetectResourceConflict(&CmList
, TRUE
, ConflictingDescriptor
);
1413 if (Status
== STATUS_CONFLICTING_ADDRESSES
)
1420 IopFindBusNumberResource(
1421 IN PIO_RESOURCE_DESCRIPTOR IoDesc
,
1422 OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
)
1425 CM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDesc
;
1427 ASSERT(IoDesc
->Type
== CmDesc
->Type
);
1428 ASSERT(IoDesc
->Type
== CmResourceTypeBusNumber
);
1430 for (Start
= IoDesc
->u
.BusNumber
.MinBusNumber
;
1431 Start
< IoDesc
->u
.BusNumber
.MaxBusNumber
;
1434 CmDesc
->u
.BusNumber
.Length
= IoDesc
->u
.BusNumber
.Length
;
1435 CmDesc
->u
.BusNumber
.Start
= Start
;
1437 if (IopCheckDescriptorForConflict(CmDesc
, &ConflictingDesc
))
1439 Start
+= ConflictingDesc
.u
.BusNumber
.Start
+ ConflictingDesc
.u
.BusNumber
.Length
;
1451 IopFindMemoryResource(
1452 IN PIO_RESOURCE_DESCRIPTOR IoDesc
,
1453 OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
)
1456 CM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDesc
;
1458 ASSERT(IoDesc
->Type
== CmDesc
->Type
);
1459 ASSERT(IoDesc
->Type
== CmResourceTypeMemory
);
1461 for (Start
= IoDesc
->u
.Memory
.MinimumAddress
.QuadPart
;
1462 Start
< IoDesc
->u
.Memory
.MaximumAddress
.QuadPart
;
1465 CmDesc
->u
.Memory
.Length
= IoDesc
->u
.Memory
.Length
;
1466 CmDesc
->u
.Memory
.Start
.QuadPart
= Start
;
1468 if (IopCheckDescriptorForConflict(CmDesc
, &ConflictingDesc
))
1470 Start
+= ConflictingDesc
.u
.Memory
.Start
.QuadPart
+ ConflictingDesc
.u
.Memory
.Length
;
1482 IopFindPortResource(
1483 IN PIO_RESOURCE_DESCRIPTOR IoDesc
,
1484 OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
)
1487 CM_PARTIAL_RESOURCE_DESCRIPTOR ConflictingDesc
;
1489 ASSERT(IoDesc
->Type
== CmDesc
->Type
);
1490 ASSERT(IoDesc
->Type
== CmResourceTypePort
);
1492 for (Start
= IoDesc
->u
.Port
.MinimumAddress
.QuadPart
;
1493 Start
< IoDesc
->u
.Port
.MaximumAddress
.QuadPart
;
1496 CmDesc
->u
.Port
.Length
= IoDesc
->u
.Port
.Length
;
1497 CmDesc
->u
.Port
.Start
.QuadPart
= Start
;
1499 if (IopCheckDescriptorForConflict(CmDesc
, &ConflictingDesc
))
1501 Start
+= ConflictingDesc
.u
.Port
.Start
.QuadPart
+ ConflictingDesc
.u
.Port
.Length
;
1514 IN PIO_RESOURCE_DESCRIPTOR IoDesc
,
1515 OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
)
1519 ASSERT(IoDesc
->Type
== CmDesc
->Type
);
1520 ASSERT(IoDesc
->Type
== CmResourceTypeDma
);
1522 for (Channel
= IoDesc
->u
.Dma
.MinimumChannel
;
1523 Channel
< IoDesc
->u
.Dma
.MaximumChannel
;
1526 CmDesc
->u
.Dma
.Channel
= Channel
;
1527 CmDesc
->u
.Dma
.Port
= 0;
1529 if (!IopCheckDescriptorForConflict(CmDesc
, NULL
))
1537 IopFindInterruptResource(
1538 IN PIO_RESOURCE_DESCRIPTOR IoDesc
,
1539 OUT PCM_PARTIAL_RESOURCE_DESCRIPTOR CmDesc
)
1543 ASSERT(IoDesc
->Type
== CmDesc
->Type
);
1544 ASSERT(IoDesc
->Type
== CmResourceTypeInterrupt
);
1546 for (Vector
= IoDesc
->u
.Interrupt
.MinimumVector
;
1547 Vector
< IoDesc
->u
.Interrupt
.MaximumVector
;
1550 CmDesc
->u
.Interrupt
.Vector
= Vector
;
1551 CmDesc
->u
.Interrupt
.Level
= Vector
;
1552 CmDesc
->u
.Interrupt
.Affinity
= (KAFFINITY
)-1;
1554 if (!IopCheckDescriptorForConflict(CmDesc
, NULL
))
1562 IopCreateResourceListFromRequirements(
1563 IN PIO_RESOURCE_REQUIREMENTS_LIST RequirementsList
,
1564 OUT PCM_RESOURCE_LIST
*ResourceList
)
1567 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResDesc
;
1569 Size
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
1570 for (i
= 0; i
< RequirementsList
->AlternativeLists
; i
++)
1572 PIO_RESOURCE_LIST ResList
= &RequirementsList
->List
[i
];
1573 Size
+= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
)
1574 + ResList
->Count
* sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1577 DPRINT1("Allocating resource list: %d\n", Size
);
1578 *ResourceList
= ExAllocatePool(PagedPool
, Size
);
1580 return STATUS_INSUFFICIENT_RESOURCES
;
1582 (*ResourceList
)->Count
= 1;
1583 (*ResourceList
)->List
[0].BusNumber
= RequirementsList
->BusNumber
;
1584 (*ResourceList
)->List
[0].InterfaceType
= RequirementsList
->InterfaceType
;
1585 (*ResourceList
)->List
[0].PartialResourceList
.Version
= 1;
1586 (*ResourceList
)->List
[0].PartialResourceList
.Revision
= 1;
1587 (*ResourceList
)->List
[0].PartialResourceList
.Count
= 0;
1589 ResDesc
= &(*ResourceList
)->List
[0].PartialResourceList
.PartialDescriptors
[0];
1591 for (i
= 0; i
< RequirementsList
->AlternativeLists
; i
++)
1593 PIO_RESOURCE_LIST ResList
= &RequirementsList
->List
[i
];
1594 for (ii
= 0; ii
< ResList
->Count
; ii
++)
1596 PIO_RESOURCE_DESCRIPTOR ReqDesc
= &ResList
->Descriptors
[ii
];
1598 /* FIXME: Handle alternate ranges */
1599 if (ReqDesc
->Option
== IO_RESOURCE_ALTERNATIVE
)
1602 ResDesc
->Type
= ReqDesc
->Type
;
1603 ResDesc
->Flags
= ReqDesc
->Flags
;
1604 ResDesc
->ShareDisposition
= ReqDesc
->ShareDisposition
;
1606 switch (ReqDesc
->Type
)
1608 case CmResourceTypeInterrupt
:
1609 if (!IopFindInterruptResource(ReqDesc
, ResDesc
))
1611 DPRINT1("Failed to find an available interrupt resource (0x%x to 0x%x)\n",
1612 ReqDesc
->u
.Interrupt
.MinimumVector
, ReqDesc
->u
.Interrupt
.MaximumVector
);
1614 if (ReqDesc
->Option
== 0)
1616 ExFreePool(*ResourceList
);
1617 return STATUS_CONFLICTING_ADDRESSES
;
1622 case CmResourceTypePort
:
1623 if (!IopFindPortResource(ReqDesc
, ResDesc
))
1625 DPRINT1("Failed to find an available port resource (0x%x to 0x%x length: 0x%x)\n",
1626 ReqDesc
->u
.Port
.MinimumAddress
.QuadPart
, ReqDesc
->u
.Port
.MaximumAddress
.QuadPart
,
1627 ReqDesc
->u
.Port
.Length
);
1629 if (ReqDesc
->Option
== 0)
1631 ExFreePool(*ResourceList
);
1632 return STATUS_CONFLICTING_ADDRESSES
;
1637 case CmResourceTypeMemory
:
1638 if (!IopFindMemoryResource(ReqDesc
, ResDesc
))
1640 DPRINT1("Failed to find an available memory resource (0x%x to 0x%x length: 0x%x)\n",
1641 ReqDesc
->u
.Memory
.MinimumAddress
.QuadPart
, ReqDesc
->u
.Memory
.MaximumAddress
.QuadPart
,
1642 ReqDesc
->u
.Memory
.Length
);
1644 if (ReqDesc
->Option
== 0)
1646 ExFreePool(*ResourceList
);
1647 return STATUS_CONFLICTING_ADDRESSES
;
1652 case CmResourceTypeBusNumber
:
1653 if (!IopFindBusNumberResource(ReqDesc
, ResDesc
))
1655 DPRINT1("Failed to find an available bus number resource (0x%x to 0x%x length: 0x%x)\n",
1656 ReqDesc
->u
.BusNumber
.MinBusNumber
, ReqDesc
->u
.BusNumber
.MaxBusNumber
,
1657 ReqDesc
->u
.BusNumber
.Length
);
1659 if (ReqDesc
->Option
== 0)
1661 ExFreePool(*ResourceList
);
1662 return STATUS_CONFLICTING_ADDRESSES
;
1667 case CmResourceTypeDma
:
1668 if (!IopFindDmaResource(ReqDesc
, ResDesc
))
1670 DPRINT1("Failed to find an available dma resource (0x%x to 0x%x)\n",
1671 ReqDesc
->u
.Dma
.MinimumChannel
, ReqDesc
->u
.Dma
.MaximumChannel
);
1673 if (ReqDesc
->Option
== 0)
1675 ExFreePool(*ResourceList
);
1676 return STATUS_CONFLICTING_ADDRESSES
;
1682 DPRINT1("Unsupported resource type: %x\n", ReqDesc
->Type
);
1686 (*ResourceList
)->List
[0].PartialResourceList
.Count
++;
1691 return STATUS_SUCCESS
;
1695 IopAssignDeviceResources(
1696 IN PDEVICE_NODE DeviceNode
,
1697 OUT ULONG
*pRequiredSize
)
1699 PCM_PARTIAL_RESOURCE_LIST pPartialResourceList
;
1705 if (!DeviceNode
->BootResources
&& !DeviceNode
->ResourceRequirements
)
1707 /* No resource needed for this device */
1708 DeviceNode
->ResourceList
= NULL
;
1710 return STATUS_SUCCESS
;
1713 /* Fill DeviceNode->ResourceList
1714 * FIXME: the PnP arbiter should go there!
1715 * Actually, use the BootResources if provided, else the resource requirements
1718 if (DeviceNode
->BootResources
)
1720 /* Browse the boot resources to know if we have some custom structures */
1721 Size
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
1722 for (i
= 0; i
< DeviceNode
->BootResources
->Count
; i
++)
1724 pPartialResourceList
= &DeviceNode
->BootResources
->List
[i
].PartialResourceList
;
1725 Size
+= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
)
1726 + pPartialResourceList
->Count
* sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1727 for (j
= 0; j
< pPartialResourceList
->Count
; j
++)
1729 if (pPartialResourceList
->PartialDescriptors
[j
].Type
== CmResourceTypeDeviceSpecific
)
1730 Size
+= pPartialResourceList
->PartialDescriptors
[j
].u
.DeviceSpecificData
.DataSize
;
1734 DeviceNode
->ResourceList
= ExAllocatePool(PagedPool
, Size
);
1735 if (!DeviceNode
->ResourceList
)
1737 Status
= STATUS_NO_MEMORY
;
1740 RtlCopyMemory(DeviceNode
->ResourceList
, DeviceNode
->BootResources
, Size
);
1742 Status
= IopDetectResourceConflict(DeviceNode
->ResourceList
, FALSE
, NULL
);
1743 if (NT_SUCCESS(Status
) || !DeviceNode
->ResourceRequirements
)
1745 if (!NT_SUCCESS(Status
) && !DeviceNode
->ResourceRequirements
)
1747 DPRINT1("Using conflicting boot resources because no requirements were supplied!\n");
1750 *pRequiredSize
= Size
;
1751 return STATUS_SUCCESS
;
1755 DPRINT1("Boot resources for %wZ cause a resource conflict!\n", &DeviceNode
->InstancePath
);
1756 ExFreePool(DeviceNode
->ResourceList
);
1760 Status
= IopCreateResourceListFromRequirements(DeviceNode
->ResourceRequirements
,
1761 &DeviceNode
->ResourceList
);
1762 if (!NT_SUCCESS(Status
))
1765 Size
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
1766 for (i
= 0; i
< DeviceNode
->ResourceList
->Count
; i
++)
1768 pPartialResourceList
= &DeviceNode
->ResourceList
->List
[i
].PartialResourceList
;
1769 Size
+= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
)
1770 + pPartialResourceList
->Count
* sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
1773 Status
= IopDetectResourceConflict(DeviceNode
->ResourceList
, FALSE
, NULL
);
1774 if (!NT_SUCCESS(Status
))
1777 *pRequiredSize
= Size
;
1778 return STATUS_SUCCESS
;
1781 if (DeviceNode
->ResourceList
)
1783 ExFreePool(DeviceNode
->ResourceList
);
1784 DeviceNode
->ResourceList
= NULL
;
1792 IopTranslateDeviceResources(
1793 IN PDEVICE_NODE DeviceNode
,
1794 IN ULONG RequiredSize
)
1796 PCM_PARTIAL_RESOURCE_LIST pPartialResourceList
;
1797 PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw
, DescriptorTranslated
;
1801 if (!DeviceNode
->ResourceList
)
1803 DeviceNode
->ResourceListTranslated
= NULL
;
1804 return STATUS_SUCCESS
;
1807 /* That's easy to translate a resource list. Just copy the
1808 * untranslated one and change few fields in the copy
1810 DeviceNode
->ResourceListTranslated
= ExAllocatePool(PagedPool
, RequiredSize
);
1811 if (!DeviceNode
->ResourceListTranslated
)
1813 Status
=STATUS_NO_MEMORY
;
1816 RtlCopyMemory(DeviceNode
->ResourceListTranslated
, DeviceNode
->ResourceList
, RequiredSize
);
1818 for (i
= 0; i
< DeviceNode
->ResourceList
->Count
; i
++)
1820 pPartialResourceList
= &DeviceNode
->ResourceList
->List
[i
].PartialResourceList
;
1821 for (j
= 0; j
< pPartialResourceList
->Count
; j
++)
1823 DescriptorRaw
= &pPartialResourceList
->PartialDescriptors
[j
];
1824 DescriptorTranslated
= &DeviceNode
->ResourceListTranslated
->List
[i
].PartialResourceList
.PartialDescriptors
[j
];
1825 switch (DescriptorRaw
->Type
)
1827 case CmResourceTypePort
:
1829 ULONG AddressSpace
= 1; /* IO space */
1830 if (!HalTranslateBusAddress(
1831 DeviceNode
->ResourceList
->List
[i
].InterfaceType
,
1832 DeviceNode
->ResourceList
->List
[i
].BusNumber
,
1833 DescriptorRaw
->u
.Port
.Start
,
1835 &DescriptorTranslated
->u
.Port
.Start
))
1837 Status
= STATUS_UNSUCCESSFUL
;
1842 case CmResourceTypeInterrupt
:
1844 DescriptorTranslated
->u
.Interrupt
.Vector
= HalGetInterruptVector(
1845 DeviceNode
->ResourceList
->List
[i
].InterfaceType
,
1846 DeviceNode
->ResourceList
->List
[i
].BusNumber
,
1847 DescriptorRaw
->u
.Interrupt
.Level
,
1848 DescriptorRaw
->u
.Interrupt
.Vector
,
1849 (PKIRQL
)&DescriptorTranslated
->u
.Interrupt
.Level
,
1850 &DescriptorRaw
->u
.Interrupt
.Affinity
);
1853 case CmResourceTypeMemory
:
1855 ULONG AddressSpace
= 0; /* Memory space */
1856 if (!HalTranslateBusAddress(
1857 DeviceNode
->ResourceList
->List
[i
].InterfaceType
,
1858 DeviceNode
->ResourceList
->List
[i
].BusNumber
,
1859 DescriptorRaw
->u
.Memory
.Start
,
1861 &DescriptorTranslated
->u
.Memory
.Start
))
1863 Status
= STATUS_UNSUCCESSFUL
;
1868 case CmResourceTypeDma
:
1869 case CmResourceTypeBusNumber
:
1870 case CmResourceTypeDeviceSpecific
:
1874 DPRINT1("Unknown resource descriptor type 0x%x\n", DescriptorRaw
->Type
);
1875 Status
= STATUS_NOT_IMPLEMENTED
;
1880 return STATUS_SUCCESS
;
1883 /* Yes! Also delete ResourceList because ResourceList and
1884 * ResourceListTranslated should be a pair! */
1885 ExFreePool(DeviceNode
->ResourceList
);
1886 DeviceNode
->ResourceList
= NULL
;
1887 if (DeviceNode
->ResourceListTranslated
)
1889 ExFreePool(DeviceNode
->ResourceListTranslated
);
1890 DeviceNode
->ResourceList
= NULL
;
1897 * IopGetParentIdPrefix
1899 * Retrieve (or create) a string which identifies a device.
1903 * Pointer to device node.
1905 * Pointer to the string where is returned the parent node identifier
1908 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1909 * valid and its Buffer field is NULL-terminated. The caller needs to
1910 * to free the string with RtlFreeUnicodeString when it is no longer
1915 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode
,
1916 PUNICODE_STRING ParentIdPrefix
)
1918 ULONG KeyNameBufferLength
;
1919 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation
= NULL
;
1920 UNICODE_STRING KeyName
;
1921 UNICODE_STRING KeyValue
;
1922 UNICODE_STRING ValueName
;
1927 /* HACK: As long as some devices have a NULL device
1928 * instance path, the following test is required :(
1930 if (DeviceNode
->Parent
->InstancePath
.Length
== 0)
1932 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1933 &DeviceNode
->InstancePath
);
1934 return STATUS_UNSUCCESSFUL
;
1937 /* 1. Try to retrieve ParentIdPrefix from registry */
1938 KeyNameBufferLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
[0]) + MAX_PATH
* sizeof(WCHAR
);
1939 ParentIdPrefixInformation
= ExAllocatePool(PagedPool
, KeyNameBufferLength
+ sizeof(WCHAR
));
1940 if (!ParentIdPrefixInformation
)
1942 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1947 KeyName
.Buffer
= ExAllocatePool(PagedPool
, (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
);
1948 if (!KeyName
.Buffer
)
1950 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1954 KeyName
.MaximumLength
= (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
;
1956 RtlAppendUnicodeToString(&KeyName
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1957 RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->Parent
->InstancePath
);
1959 Status
= IopOpenRegistryKeyEx(&hKey
, NULL
, &KeyName
, KEY_QUERY_VALUE
| KEY_SET_VALUE
);
1960 if (!NT_SUCCESS(Status
))
1962 RtlInitUnicodeString(&ValueName
, L
"ParentIdPrefix");
1963 Status
= ZwQueryValueKey(
1965 KeyValuePartialInformation
, ParentIdPrefixInformation
,
1966 KeyNameBufferLength
, &KeyNameBufferLength
);
1967 if (NT_SUCCESS(Status
))
1969 if (ParentIdPrefixInformation
->Type
!= REG_SZ
)
1970 Status
= STATUS_UNSUCCESSFUL
;
1973 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1974 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1978 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1980 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1981 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1985 /* 2. Create the ParentIdPrefix value */
1986 crc32
= RtlComputeCrc32(0,
1987 (PUCHAR
)DeviceNode
->Parent
->InstancePath
.Buffer
,
1988 DeviceNode
->Parent
->InstancePath
.Length
);
1990 swprintf((PWSTR
)ParentIdPrefixInformation
->Data
, L
"%lx&%lx", DeviceNode
->Parent
->Level
, crc32
);
1991 RtlInitUnicodeString(&KeyValue
, (PWSTR
)ParentIdPrefixInformation
->Data
);
1993 /* 3. Try to write the ParentIdPrefix to registry */
1994 Status
= ZwSetValueKey(hKey
,
1998 (PVOID
)KeyValue
.Buffer
,
1999 (wcslen(KeyValue
.Buffer
) + 1) * sizeof(WCHAR
));
2002 if (NT_SUCCESS(Status
))
2004 /* Duplicate the string to return it */
2005 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
, &KeyValue
, ParentIdPrefix
);
2007 ExFreePool(ParentIdPrefixInformation
);
2008 RtlFreeUnicodeString(&KeyName
);
2016 * IopActionInterrogateDeviceStack
2018 * Retrieve information for all (direct) child nodes of a parent node.
2022 * Pointer to device node.
2024 * Pointer to parent node to retrieve child node information for.
2027 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
2028 * when we reach a device node which is not a direct child of the device
2029 * node for which we retrieve information of child nodes for. Any errors
2030 * that occur is logged instead so that all child services have a chance
2031 * of being interrogated.
2035 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
2038 IO_STATUS_BLOCK IoStatusBlock
;
2039 PDEVICE_NODE ParentDeviceNode
;
2040 WCHAR InstancePath
[MAX_PATH
];
2041 IO_STACK_LOCATION Stack
;
2046 ULONG RequiredLength
;
2048 HANDLE InstanceKey
= NULL
;
2049 UNICODE_STRING ValueName
;
2050 UNICODE_STRING ParentIdPrefix
= { 0, 0, NULL
};
2051 DEVICE_CAPABILITIES DeviceCapabilities
;
2053 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode
, Context
);
2054 DPRINT("PDO 0x%p\n", DeviceNode
->PhysicalDeviceObject
);
2056 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2059 * We are called for the parent too, but we don't need to do special
2060 * handling for this node
2063 if (DeviceNode
== ParentDeviceNode
)
2065 DPRINT("Success\n");
2066 return STATUS_SUCCESS
;
2070 * Make sure this device node is a direct child of the parent device node
2071 * that is given as an argument
2074 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2076 /* Stop the traversal immediately and indicate successful operation */
2078 return STATUS_UNSUCCESSFUL
;
2082 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
2083 if (!NT_SUCCESS(Status
))
2085 DPRINT("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status
);
2090 * FIXME: For critical errors, cleanup and disable device, but always
2091 * return STATUS_SUCCESS.
2094 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
2096 Stack
.Parameters
.QueryId
.IdType
= BusQueryDeviceID
;
2097 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2101 if (NT_SUCCESS(Status
))
2103 /* Copy the device id string */
2104 wcscpy(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
2107 * FIXME: Check for valid characters, if there is invalid characters
2113 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2116 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack\n");
2118 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
2119 if (!NT_SUCCESS(Status
))
2121 DPRINT("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
2124 DeviceNode
->CapabilityFlags
= *(PULONG
)((ULONG_PTR
)&DeviceCapabilities
+ 4);
2126 if (!DeviceCapabilities
.UniqueID
)
2128 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
2129 DPRINT("Instance ID is not unique\n");
2130 Status
= IopGetParentIdPrefix(DeviceNode
, &ParentIdPrefix
);
2131 if (!NT_SUCCESS(Status
))
2133 DPRINT("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status
);
2137 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
2139 Stack
.Parameters
.QueryId
.IdType
= BusQueryInstanceID
;
2140 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2144 if (NT_SUCCESS(Status
))
2146 /* Append the instance id string */
2147 wcscat(InstancePath
, L
"\\");
2148 if (ParentIdPrefix
.Length
> 0)
2150 /* Add information from parent bus device to InstancePath */
2151 wcscat(InstancePath
, ParentIdPrefix
.Buffer
);
2152 if (IoStatusBlock
.Information
&& *(PWSTR
)IoStatusBlock
.Information
)
2153 wcscat(InstancePath
, L
"&");
2155 if (IoStatusBlock
.Information
)
2156 wcscat(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
2159 * FIXME: Check for valid characters, if there is invalid characters
2165 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2167 RtlFreeUnicodeString(&ParentIdPrefix
);
2169 if (!RtlCreateUnicodeString(&DeviceNode
->InstancePath
, InstancePath
))
2171 DPRINT("No resources\n");
2172 /* FIXME: Cleanup and disable device */
2175 DPRINT("InstancePath is %S\n", DeviceNode
->InstancePath
.Buffer
);
2178 * Create registry key for the instance id, if it doesn't exist yet
2180 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, &InstanceKey
);
2181 if (!NT_SUCCESS(Status
))
2183 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status
);
2187 /* Set 'Capabilities' value */
2188 RtlInitUnicodeString(&ValueName
, L
"Capabilities");
2189 Status
= ZwSetValueKey(InstanceKey
,
2193 (PVOID
)&DeviceNode
->CapabilityFlags
,
2196 /* Set 'UINumber' value */
2197 if (DeviceCapabilities
.UINumber
!= MAXULONG
)
2199 RtlInitUnicodeString(&ValueName
, L
"UINumber");
2200 Status
= ZwSetValueKey(InstanceKey
,
2204 &DeviceCapabilities
.UINumber
,
2209 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
2211 Stack
.Parameters
.QueryId
.IdType
= BusQueryHardwareIDs
;
2212 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
2216 if (NT_SUCCESS(Status
))
2219 * FIXME: Check for valid characters, if there is invalid characters
2223 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
2224 DPRINT("Hardware IDs:\n");
2227 DPRINT(" %S\n", Ptr
);
2228 Length
= wcslen(Ptr
) + 1;
2231 TotalLength
+= Length
;
2233 DPRINT("TotalLength: %hu\n", TotalLength
);
2236 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
2237 Status
= ZwSetValueKey(InstanceKey
,
2241 (PVOID
)IoStatusBlock
.Information
,
2242 (TotalLength
+ 1) * sizeof(WCHAR
));
2243 if (!NT_SUCCESS(Status
))
2245 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
2250 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2253 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
2255 Stack
.Parameters
.QueryId
.IdType
= BusQueryCompatibleIDs
;
2256 Status
= IopInitiatePnpIrp(
2257 DeviceNode
->PhysicalDeviceObject
,
2261 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2264 * FIXME: Check for valid characters, if there is invalid characters
2268 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
2269 DPRINT("Compatible IDs:\n");
2272 DPRINT(" %S\n", Ptr
);
2273 Length
= wcslen(Ptr
) + 1;
2276 TotalLength
+= Length
;
2278 DPRINT("TotalLength: %hu\n", TotalLength
);
2281 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
2282 Status
= ZwSetValueKey(InstanceKey
,
2286 (PVOID
)IoStatusBlock
.Information
,
2287 (TotalLength
+ 1) * sizeof(WCHAR
));
2288 if (!NT_SUCCESS(Status
))
2290 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status
);
2295 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
2298 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
2300 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextDescription
;
2301 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2302 Status
= IopInitiatePnpIrp(
2303 DeviceNode
->PhysicalDeviceObject
,
2305 IRP_MN_QUERY_DEVICE_TEXT
,
2307 /* This key is mandatory, so even if the Irp fails, we still write it */
2308 RtlInitUnicodeString(&ValueName
, L
"DeviceDesc");
2309 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
2311 if (NT_SUCCESS(Status
) &&
2312 IoStatusBlock
.Information
&&
2313 (*(PWSTR
)IoStatusBlock
.Information
!= 0))
2315 /* This key is overriden when a driver is installed. Don't write the
2316 * new description if another one already exists */
2317 Status
= ZwSetValueKey(InstanceKey
,
2321 (PVOID
)IoStatusBlock
.Information
,
2322 (wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
2326 UNICODE_STRING DeviceDesc
= RTL_CONSTANT_STRING(L
"Unknown device");
2327 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status
);
2329 Status
= ZwSetValueKey(InstanceKey
,
2334 DeviceDesc
.MaximumLength
);
2336 if (!NT_SUCCESS(Status
))
2338 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status
);
2344 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
2346 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextLocationInformation
;
2347 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2348 Status
= IopInitiatePnpIrp(
2349 DeviceNode
->PhysicalDeviceObject
,
2351 IRP_MN_QUERY_DEVICE_TEXT
,
2353 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2355 DPRINT("LocationInformation: %S\n", (PWSTR
)IoStatusBlock
.Information
);
2356 RtlInitUnicodeString(&ValueName
, L
"LocationInformation");
2357 Status
= ZwSetValueKey(InstanceKey
,
2361 (PVOID
)IoStatusBlock
.Information
,
2362 (wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
2363 if (!NT_SUCCESS(Status
))
2365 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
2370 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2373 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
2375 Status
= IopInitiatePnpIrp(
2376 DeviceNode
->PhysicalDeviceObject
,
2378 IRP_MN_QUERY_BUS_INFORMATION
,
2380 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2382 PPNP_BUS_INFORMATION BusInformation
=
2383 (PPNP_BUS_INFORMATION
)IoStatusBlock
.Information
;
2385 DeviceNode
->ChildBusNumber
= BusInformation
->BusNumber
;
2386 DeviceNode
->ChildInterfaceType
= BusInformation
->LegacyBusType
;
2387 DeviceNode
->ChildBusTypeIndex
= IopGetBusTypeGuidIndex(&BusInformation
->BusTypeGuid
);
2388 ExFreePool(BusInformation
);
2392 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2394 DeviceNode
->ChildBusNumber
= 0xFFFFFFF0;
2395 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
2396 DeviceNode
->ChildBusTypeIndex
= -1;
2399 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
2401 Status
= IopInitiatePnpIrp(
2402 DeviceNode
->PhysicalDeviceObject
,
2404 IRP_MN_QUERY_RESOURCES
,
2406 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2408 DeviceNode
->BootResources
=
2409 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
2410 DeviceNode
->Flags
|= DNF_HAS_BOOT_CONFIG
;
2414 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2415 DeviceNode
->BootResources
= NULL
;
2418 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
2420 Status
= IopInitiatePnpIrp(
2421 DeviceNode
->PhysicalDeviceObject
,
2423 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
2425 if (NT_SUCCESS(Status
))
2427 DeviceNode
->ResourceRequirements
=
2428 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
2429 if (IoStatusBlock
.Information
)
2430 IopDeviceNodeSetFlag(DeviceNode
, DNF_RESOURCE_REPORTED
);
2432 IopDeviceNodeSetFlag(DeviceNode
, DNF_NO_RESOURCE_REQUIRED
);
2436 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
2437 DeviceNode
->ResourceRequirements
= NULL
;
2441 if (InstanceKey
!= NULL
)
2443 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
2446 ZwClose(InstanceKey
);
2448 IopDeviceNodeSetFlag(DeviceNode
, DNF_PROCESSED
);
2450 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
2452 /* Report the device to the user-mode pnp manager */
2453 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
2454 &DeviceNode
->InstancePath
);
2457 return STATUS_SUCCESS
;
2463 IN PDEVICE_OBJECT DeviceObject
)
2465 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
2466 DEVICETREE_TRAVERSE_CONTEXT Context
;
2467 PDEVICE_RELATIONS DeviceRelations
;
2468 PDEVICE_OBJECT ChildDeviceObject
;
2469 IO_STATUS_BLOCK IoStatusBlock
;
2470 PDEVICE_NODE ChildDeviceNode
;
2471 IO_STACK_LOCATION Stack
;
2475 DPRINT("DeviceObject 0x%p\n", DeviceObject
);
2477 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
2479 /* Report the device to the user-mode pnp manager */
2480 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
2481 &DeviceNode
->InstancePath
);
2483 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2485 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
2487 Status
= IopInitiatePnpIrp(
2490 IRP_MN_QUERY_DEVICE_RELATIONS
,
2492 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
2494 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
2498 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
2500 if (!DeviceRelations
)
2502 DPRINT("No PDOs\n");
2503 return STATUS_UNSUCCESSFUL
;
2506 DPRINT("Got %u PDOs\n", DeviceRelations
->Count
);
2509 * Create device nodes for all discovered devices
2511 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
2513 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
2514 ASSERT((ChildDeviceObject
->Flags
& DO_DEVICE_INITIALIZING
) == 0);
2516 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
2517 if (!ChildDeviceNode
)
2519 /* One doesn't exist, create it */
2520 Status
= IopCreateDeviceNode(
2525 if (NT_SUCCESS(Status
))
2527 /* Mark the node as enumerated */
2528 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2530 /* Mark the DO as bus enumerated */
2531 ChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
2535 /* Ignore this DO */
2536 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status
, i
);
2537 ObDereferenceObject(ChildDeviceNode
);
2542 /* Mark it as enumerated */
2543 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2544 ObDereferenceObject(ChildDeviceObject
);
2547 ExFreePool(DeviceRelations
);
2550 * Retrieve information about all discovered children from the bus driver
2552 IopInitDeviceTreeTraverseContext(
2555 IopActionInterrogateDeviceStack
,
2558 Status
= IopTraverseDeviceTree(&Context
);
2559 if (!NT_SUCCESS(Status
))
2561 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2566 * Retrieve configuration from the registry for discovered children
2568 IopInitDeviceTreeTraverseContext(
2571 IopActionConfigureChildServices
,
2574 Status
= IopTraverseDeviceTree(&Context
);
2575 if (!NT_SUCCESS(Status
))
2577 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2582 * Initialize services for discovered children.
2584 Status
= IopInitializePnpServices(DeviceNode
);
2585 if (!NT_SUCCESS(Status
))
2587 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status
);
2591 DPRINT("IopEnumerateDevice() finished\n");
2592 return STATUS_SUCCESS
;
2597 * IopActionConfigureChildServices
2599 * Retrieve configuration for all (direct) child nodes of a parent node.
2603 * Pointer to device node.
2605 * Pointer to parent node to retrieve child node configuration for.
2608 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
2609 * when we reach a device node which is not a direct child of the device
2610 * node for which we configure child services for. Any errors that occur is
2611 * logged instead so that all child services have a chance of beeing
2616 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode
,
2619 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
2620 PDEVICE_NODE ParentDeviceNode
;
2621 PUNICODE_STRING Service
;
2622 UNICODE_STRING ClassGUID
;
2624 DEVICE_CAPABILITIES DeviceCaps
;
2626 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode
, Context
);
2628 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2631 * We are called for the parent too, but we don't need to do special
2632 * handling for this node
2634 if (DeviceNode
== ParentDeviceNode
)
2636 DPRINT("Success\n");
2637 return STATUS_SUCCESS
;
2641 * Make sure this device node is a direct child of the parent device node
2642 * that is given as an argument
2644 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2646 /* Stop the traversal immediately and indicate successful operation */
2648 return STATUS_UNSUCCESSFUL
;
2651 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
))
2653 WCHAR RegKeyBuffer
[MAX_PATH
];
2654 UNICODE_STRING RegKey
;
2657 RegKey
.MaximumLength
= sizeof(RegKeyBuffer
);
2658 RegKey
.Buffer
= RegKeyBuffer
;
2661 * Retrieve configuration from Enum key
2664 Service
= &DeviceNode
->ServiceName
;
2666 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
2667 RtlInitUnicodeString(Service
, NULL
);
2668 RtlInitUnicodeString(&ClassGUID
, NULL
);
2670 QueryTable
[0].Name
= L
"Service";
2671 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2672 QueryTable
[0].EntryContext
= Service
;
2674 QueryTable
[1].Name
= L
"ClassGUID";
2675 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2676 QueryTable
[1].EntryContext
= &ClassGUID
;
2677 QueryTable
[1].DefaultType
= REG_SZ
;
2678 QueryTable
[1].DefaultData
= L
"";
2679 QueryTable
[1].DefaultLength
= 0;
2681 RtlAppendUnicodeToString(&RegKey
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2682 RtlAppendUnicodeStringToString(&RegKey
, &DeviceNode
->InstancePath
);
2684 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
2685 RegKey
.Buffer
, QueryTable
, NULL
, NULL
);
2687 if (!NT_SUCCESS(Status
))
2689 /* FIXME: Log the error */
2690 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2691 &DeviceNode
->InstancePath
, Status
);
2692 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2693 return STATUS_SUCCESS
;
2696 if (Service
->Buffer
== NULL
)
2698 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
)) &&
2699 DeviceCaps
.RawDeviceOK
)
2701 DPRINT1("%wZ is using parent bus driver (%wZ)\n", &DeviceNode
->InstancePath
, &ParentDeviceNode
->ServiceName
);
2703 DeviceNode
->ServiceName
.Length
= 0;
2704 DeviceNode
->ServiceName
.MaximumLength
= ParentDeviceNode
->ServiceName
.MaximumLength
;
2705 DeviceNode
->ServiceName
.Buffer
= ExAllocatePool(PagedPool
, DeviceNode
->ServiceName
.MaximumLength
);
2706 if (!DeviceNode
->ServiceName
.Buffer
)
2707 return STATUS_SUCCESS
;
2709 RtlCopyUnicodeString(&DeviceNode
->ServiceName
, &ParentDeviceNode
->ServiceName
);
2711 IopDeviceNodeSetFlag(DeviceNode
, DNF_LEGACY_DRIVER
);
2713 else if (ClassGUID
.Length
!= 0)
2715 /* Device has a ClassGUID value, but no Service value.
2716 * Suppose it is using the NULL driver, so state the
2717 * device is started */
2718 DPRINT1("%wZ is using NULL driver\n", &DeviceNode
->InstancePath
);
2719 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2723 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2725 return STATUS_SUCCESS
;
2728 DPRINT("Got Service %S\n", Service
->Buffer
);
2731 return STATUS_SUCCESS
;
2735 * IopActionInitChildServices
2737 * Initialize the service for all (direct) child nodes of a parent node
2741 * Pointer to device node.
2743 * Pointer to parent node to initialize child node services for.
2746 * If the driver image for a service is not loaded and initialized
2747 * it is done here too. We only return a status code indicating an
2748 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
2749 * not a direct child of the device node for which we initialize
2750 * child services for. Any errors that occur is logged instead so
2751 * that all child services have a chance of being initialized.
2755 IopActionInitChildServices(PDEVICE_NODE DeviceNode
,
2758 PDEVICE_NODE ParentDeviceNode
;
2760 BOOLEAN BootDrivers
= !PnpSystemInit
;
2762 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode
, Context
);
2764 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2767 * We are called for the parent too, but we don't need to do special
2768 * handling for this node
2770 if (DeviceNode
== ParentDeviceNode
)
2772 DPRINT("Success\n");
2773 return STATUS_SUCCESS
;
2777 * Make sure this device node is a direct child of the parent device node
2778 * that is given as an argument
2781 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2784 * Stop the traversal immediately and indicate unsuccessful operation
2787 return STATUS_UNSUCCESSFUL
;
2791 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
) &&
2792 !IopDeviceNodeHasFlag(DeviceNode
, DNF_ADDED
) &&
2793 !IopDeviceNodeHasFlag(DeviceNode
, DNF_STARTED
))
2795 PLDR_DATA_TABLE_ENTRY ModuleObject
;
2796 PDRIVER_OBJECT DriverObject
;
2798 /* Get existing DriverObject pointer (in case the driver has
2799 already been loaded and initialized) */
2800 Status
= IopGetDriverObject(
2802 &DeviceNode
->ServiceName
,
2805 if (!NT_SUCCESS(Status
))
2807 /* Driver is not initialized, try to load it */
2808 Status
= IopLoadServiceModule(&DeviceNode
->ServiceName
, &ModuleObject
);
2810 if (NT_SUCCESS(Status
) || Status
== STATUS_IMAGE_ALREADY_LOADED
)
2812 /* STATUS_IMAGE_ALREADY_LOADED means this driver
2813 was loaded by the bootloader */
2814 if ((Status
!= STATUS_IMAGE_ALREADY_LOADED
) ||
2815 (Status
== STATUS_IMAGE_ALREADY_LOADED
&& !DriverObject
))
2817 /* Initialize the driver */
2818 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
,
2819 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
2823 Status
= STATUS_SUCCESS
;
2828 DPRINT1("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
2829 &DeviceNode
->ServiceName
, Status
);
2833 /* Driver is loaded and initialized at this point */
2834 if (NT_SUCCESS(Status
))
2836 /* Attach lower level filter drivers. */
2837 IopAttachFilterDrivers(DeviceNode
, TRUE
);
2838 /* Initialize the function driver for the device node */
2839 Status
= IopInitializeDevice(DeviceNode
, DriverObject
);
2841 if (NT_SUCCESS(Status
))
2843 /* Attach upper level filter drivers. */
2844 IopAttachFilterDrivers(DeviceNode
, FALSE
);
2845 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2847 Status
= IopStartDevice(DeviceNode
);
2851 DPRINT1("IopInitializeDevice(%wZ) failed with status 0x%08x\n",
2852 &DeviceNode
->InstancePath
, Status
);
2858 * Don't disable when trying to load only boot drivers
2862 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2863 IopDeviceNodeSetFlag(DeviceNode
, DNF_START_FAILED
);
2864 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2865 DPRINT1("Initialization of service %S failed (Status %x)\n",
2866 DeviceNode
->ServiceName
.Buffer
, Status
);
2872 DPRINT("Device %wZ is disabled or already initialized\n",
2873 &DeviceNode
->InstancePath
);
2876 return STATUS_SUCCESS
;
2880 * IopInitializePnpServices
2882 * Initialize services for discovered children
2886 * Top device node to start initializing services.
2892 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode
)
2894 DEVICETREE_TRAVERSE_CONTEXT Context
;
2896 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode
);
2898 IopInitDeviceTreeTraverseContext(
2901 IopActionInitChildServices
,
2904 return IopTraverseDeviceTree(&Context
);
2907 static NTSTATUS INIT_FUNCTION
2908 IopEnumerateDetectedDevices(
2910 IN PUNICODE_STRING RelativePath OPTIONAL
,
2912 IN BOOLEAN EnumerateSubKeys
,
2913 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources
,
2914 IN ULONG ParentBootResourcesLength
)
2916 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
2917 UNICODE_STRING DeviceDescU
= RTL_CONSTANT_STRING(L
"DeviceDesc");
2918 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
2919 UNICODE_STRING ConfigurationDataU
= RTL_CONSTANT_STRING(L
"Configuration Data");
2920 UNICODE_STRING BootConfigU
= RTL_CONSTANT_STRING(L
"BootConfig");
2921 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
2922 OBJECT_ATTRIBUTES ObjectAttributes
;
2923 HANDLE hDevicesKey
= NULL
;
2924 HANDLE hDeviceKey
= NULL
;
2925 HANDLE hLevel1Key
, hLevel2Key
= NULL
, hLogConf
;
2926 UNICODE_STRING Level2NameU
;
2927 WCHAR Level2Name
[5];
2928 ULONG IndexDevice
= 0;
2930 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
2931 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
2932 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
2933 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
2934 UNICODE_STRING DeviceName
, ValueName
;
2936 PCM_FULL_RESOURCE_DESCRIPTOR BootResources
= NULL
;
2937 ULONG BootResourcesLength
;
2940 const UNICODE_STRING IdentifierPci
= RTL_CONSTANT_STRING(L
"PCI");
2941 UNICODE_STRING HardwareIdPci
= RTL_CONSTANT_STRING(L
"*PNP0A03\0");
2942 static ULONG DeviceIndexPci
= 0;
2943 const UNICODE_STRING IdentifierSerial
= RTL_CONSTANT_STRING(L
"SerialController");
2944 UNICODE_STRING HardwareIdSerial
= RTL_CONSTANT_STRING(L
"*PNP0501\0");
2945 static ULONG DeviceIndexSerial
= 0;
2946 const UNICODE_STRING IdentifierKeyboard
= RTL_CONSTANT_STRING(L
"KeyboardController");
2947 UNICODE_STRING HardwareIdKeyboard
= RTL_CONSTANT_STRING(L
"*PNP0303\0");
2948 static ULONG DeviceIndexKeyboard
= 0;
2949 const UNICODE_STRING IdentifierMouse
= RTL_CONSTANT_STRING(L
"PointerController");
2950 UNICODE_STRING HardwareIdMouse
= RTL_CONSTANT_STRING(L
"*PNP0F13\0");
2951 static ULONG DeviceIndexMouse
= 0;
2952 const UNICODE_STRING IdentifierParallel
= RTL_CONSTANT_STRING(L
"ParallelController");
2953 UNICODE_STRING HardwareIdParallel
= RTL_CONSTANT_STRING(L
"*PNP0400\0");
2954 static ULONG DeviceIndexParallel
= 0;
2955 const UNICODE_STRING IdentifierFloppy
= RTL_CONSTANT_STRING(L
"FloppyDiskPeripheral");
2956 UNICODE_STRING HardwareIdFloppy
= RTL_CONSTANT_STRING(L
"*PNP0700\0");
2957 static ULONG DeviceIndexFloppy
= 0;
2958 const UNICODE_STRING IdentifierIsa
= RTL_CONSTANT_STRING(L
"ISA");
2959 UNICODE_STRING HardwareIdIsa
= RTL_CONSTANT_STRING(L
"*PNP0A00\0");
2960 static ULONG DeviceIndexIsa
= 0;
2961 UNICODE_STRING HardwareIdKey
;
2962 PUNICODE_STRING pHardwareId
;
2963 ULONG DeviceIndex
= 0;
2964 BOOLEAN IsDeviceDesc
;
2965 PUCHAR CmResourceList
;
2970 Status
= IopOpenRegistryKeyEx(&hDevicesKey
, hBaseKey
, RelativePath
, KEY_ENUMERATE_SUB_KEYS
);
2971 if (!NT_SUCCESS(Status
))
2973 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2978 hDevicesKey
= hBaseKey
;
2980 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2981 if (!pDeviceInformation
)
2983 DPRINT("ExAllocatePool() failed\n");
2984 Status
= STATUS_NO_MEMORY
;
2988 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2989 if (!pValueInformation
)
2991 DPRINT("ExAllocatePool() failed\n");
2992 Status
= STATUS_NO_MEMORY
;
2998 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2999 if (Status
== STATUS_NO_MORE_ENTRIES
)
3001 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3003 ExFreePool(pDeviceInformation
);
3004 DeviceInfoLength
= RequiredSize
;
3005 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3006 if (!pDeviceInformation
)
3008 DPRINT("ExAllocatePool() failed\n");
3009 Status
= STATUS_NO_MEMORY
;
3012 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3014 if (!NT_SUCCESS(Status
))
3016 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
3021 /* Open device key */
3022 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
3023 DeviceName
.Buffer
= pDeviceInformation
->Name
;
3025 Status
= IopOpenRegistryKeyEx(&hDeviceKey
, hDevicesKey
, &DeviceName
,
3026 KEY_QUERY_VALUE
+ (EnumerateSubKeys
? KEY_ENUMERATE_SUB_KEYS
: 0));
3027 if (!NT_SUCCESS(Status
))
3029 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3033 /* Read boot resources, and add then to parent ones */
3034 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3035 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3037 ExFreePool(pValueInformation
);
3038 ValueInfoLength
= RequiredSize
;
3039 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3040 if (!pValueInformation
)
3042 DPRINT("ExAllocatePool() failed\n");
3043 ZwDeleteKey(hLevel2Key
);
3044 Status
= STATUS_NO_MEMORY
;
3047 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3049 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
3051 BootResources
= ParentBootResources
;
3052 BootResourcesLength
= ParentBootResourcesLength
;
3054 else if (!NT_SUCCESS(Status
))
3056 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
3059 else if (pValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
)
3061 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_FULL_RESOURCE_DESCRIPTOR
);
3066 static const ULONG Header
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
);
3068 /* Concatenate current resources and parent ones */
3069 if (ParentBootResourcesLength
== 0)
3070 BootResourcesLength
= pValueInformation
->DataLength
;
3072 BootResourcesLength
= ParentBootResourcesLength
3073 + pValueInformation
->DataLength
3075 BootResources
= ExAllocatePool(PagedPool
, BootResourcesLength
);
3078 DPRINT("ExAllocatePool() failed\n");
3081 if (ParentBootResourcesLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
3083 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
3085 else if (ParentBootResources
->PartialResourceList
.PartialDescriptors
[ParentBootResources
->PartialResourceList
.Count
- 1].Type
== CmResourceTypeDeviceSpecific
)
3087 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
3089 (PVOID
)((ULONG_PTR
)BootResources
+ pValueInformation
->DataLength
),
3090 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
3091 ParentBootResourcesLength
- Header
);
3092 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
3096 RtlCopyMemory(BootResources
, pValueInformation
->Data
, Header
);
3098 (PVOID
)((ULONG_PTR
)BootResources
+ Header
),
3099 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
3100 ParentBootResourcesLength
- Header
);
3102 (PVOID
)((ULONG_PTR
)BootResources
+ ParentBootResourcesLength
),
3103 pValueInformation
->Data
+ Header
,
3104 pValueInformation
->DataLength
- Header
);
3105 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
3109 if (EnumerateSubKeys
)
3114 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3115 if (Status
== STATUS_NO_MORE_ENTRIES
)
3117 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3119 ExFreePool(pDeviceInformation
);
3120 DeviceInfoLength
= RequiredSize
;
3121 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3122 if (!pDeviceInformation
)
3124 DPRINT("ExAllocatePool() failed\n");
3125 Status
= STATUS_NO_MEMORY
;
3128 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3130 if (!NT_SUCCESS(Status
))
3132 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
3136 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
3137 DeviceName
.Buffer
= pDeviceInformation
->Name
;
3139 Status
= IopEnumerateDetectedDevices(
3145 BootResourcesLength
);
3146 if (!NT_SUCCESS(Status
))
3151 /* Read identifier */
3152 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3153 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3155 ExFreePool(pValueInformation
);
3156 ValueInfoLength
= RequiredSize
;
3157 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3158 if (!pValueInformation
)
3160 DPRINT("ExAllocatePool() failed\n");
3161 Status
= STATUS_NO_MEMORY
;
3164 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3166 if (!NT_SUCCESS(Status
))
3168 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
3170 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
3173 ValueName
.Length
= ValueName
.MaximumLength
= 0;
3175 else if (pValueInformation
->Type
!= REG_SZ
)
3177 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
3182 /* Assign hardware id to this device */
3183 ValueName
.Length
= ValueName
.MaximumLength
= (USHORT
)pValueInformation
->DataLength
;
3184 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
3185 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
3186 ValueName
.Length
-= sizeof(WCHAR
);
3189 if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierSerial
, FALSE
) == 0)
3191 pHardwareId
= &HardwareIdSerial
;
3192 DeviceIndex
= DeviceIndexSerial
++;
3193 IsDeviceDesc
= TRUE
;
3195 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierKeyboard
, FALSE
) == 0)
3197 pHardwareId
= &HardwareIdKeyboard
;
3198 DeviceIndex
= DeviceIndexKeyboard
++;
3199 IsDeviceDesc
= FALSE
;
3201 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierMouse
, FALSE
) == 0)
3203 pHardwareId
= &HardwareIdMouse
;
3204 DeviceIndex
= DeviceIndexMouse
++;
3205 IsDeviceDesc
= FALSE
;
3207 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierParallel
, FALSE
) == 0)
3209 pHardwareId
= &HardwareIdParallel
;
3210 DeviceIndex
= DeviceIndexParallel
++;
3211 IsDeviceDesc
= FALSE
;
3213 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierFloppy
, FALSE
) == 0)
3215 pHardwareId
= &HardwareIdFloppy
;
3216 DeviceIndex
= DeviceIndexFloppy
++;
3217 IsDeviceDesc
= FALSE
;
3219 else if (NT_SUCCESS(Status
))
3221 /* Try to also match the device identifier */
3222 if (RtlCompareUnicodeString(&ValueName
, &IdentifierPci
, FALSE
) == 0)
3224 pHardwareId
= &HardwareIdPci
;
3225 DeviceIndex
= DeviceIndexPci
++;
3226 IsDeviceDesc
= FALSE
;
3228 else if (RtlCompareUnicodeString(&ValueName
, &IdentifierIsa
, FALSE
) == 0)
3230 pHardwareId
= &HardwareIdIsa
;
3231 DeviceIndex
= DeviceIndexIsa
++;
3232 IsDeviceDesc
= FALSE
;
3236 DPRINT("Unknown device '%wZ'\n", &ValueName
);
3242 /* Unknown key path */
3243 DPRINT("Unknown key path '%wZ'\n", RelativePath
);
3247 /* Prepare hardware id key (hardware id value without final \0) */
3248 HardwareIdKey
= *pHardwareId
;
3249 HardwareIdKey
.Length
-= sizeof(UNICODE_NULL
);
3251 /* Add the detected device to Root key */
3252 InitializeObjectAttributes(&ObjectAttributes
, &HardwareIdKey
, OBJ_KERNEL_HANDLE
, hRootKey
, NULL
);
3253 Status
= ZwCreateKey(
3259 REG_OPTION_NON_VOLATILE
,
3261 if (!NT_SUCCESS(Status
))
3263 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3266 swprintf(Level2Name
, L
"%04lu", DeviceIndex
);
3267 RtlInitUnicodeString(&Level2NameU
, Level2Name
);
3268 InitializeObjectAttributes(&ObjectAttributes
, &Level2NameU
, OBJ_KERNEL_HANDLE
, hLevel1Key
, NULL
);
3269 Status
= ZwCreateKey(
3271 KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
,
3275 REG_OPTION_NON_VOLATILE
,
3277 ZwClose(hLevel1Key
);
3278 if (!NT_SUCCESS(Status
))
3280 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3283 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName
, DeviceIndex
, &HardwareIdKey
);
3286 Status
= ZwSetValueKey(hLevel2Key
, &DeviceDescU
, 0, REG_SZ
, ValueName
.Buffer
, ValueName
.MaximumLength
);
3287 if (!NT_SUCCESS(Status
))
3289 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3290 ZwDeleteKey(hLevel2Key
);
3294 Status
= ZwSetValueKey(hLevel2Key
, &HardwareIDU
, 0, REG_MULTI_SZ
, pHardwareId
->Buffer
, pHardwareId
->MaximumLength
);
3295 if (!NT_SUCCESS(Status
))
3297 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3298 ZwDeleteKey(hLevel2Key
);
3301 /* Create 'LogConf' subkey */
3302 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
, hLevel2Key
, NULL
);
3303 Status
= ZwCreateKey(
3309 REG_OPTION_VOLATILE
,
3311 if (!NT_SUCCESS(Status
))
3313 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3314 ZwDeleteKey(hLevel2Key
);
3317 if (BootResourcesLength
>= sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
3319 CmResourceList
= ExAllocatePool(PagedPool
, BootResourcesLength
+ sizeof(ULONG
));
3320 if (!CmResourceList
)
3323 ZwDeleteKey(hLevel2Key
);
3327 /* Add the list count (1st member of CM_RESOURCE_LIST) */
3329 RtlCopyMemory(CmResourceList
,
3333 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
3334 RtlCopyMemory(CmResourceList
+ sizeof(ULONG
),
3336 BootResourcesLength
);
3338 /* Save boot resources to 'LogConf\BootConfig' */
3339 Status
= ZwSetValueKey(hLogConf
, &BootConfigU
, 0, REG_FULL_RESOURCE_DESCRIPTOR
, CmResourceList
, BootResourcesLength
+ sizeof(ULONG
));
3340 if (!NT_SUCCESS(Status
))
3342 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3344 ZwDeleteKey(hLevel2Key
);
3351 if (BootResources
&& BootResources
!= ParentBootResources
)
3353 ExFreePool(BootResources
);
3354 BootResources
= NULL
;
3358 ZwClose(hLevel2Key
);
3363 ZwClose(hDeviceKey
);
3368 Status
= STATUS_SUCCESS
;
3371 if (hDevicesKey
&& hDevicesKey
!= hBaseKey
)
3372 ZwClose(hDevicesKey
);
3374 ZwClose(hDeviceKey
);
3375 if (pDeviceInformation
)
3376 ExFreePool(pDeviceInformation
);
3377 if (pValueInformation
)
3378 ExFreePool(pValueInformation
);
3382 static BOOLEAN INIT_FUNCTION
3383 IopIsAcpiComputer(VOID
)
3388 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
3389 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
3390 UNICODE_STRING AcpiBiosIdentifier
= RTL_CONSTANT_STRING(L
"ACPI BIOS");
3391 OBJECT_ATTRIBUTES ObjectAttributes
;
3392 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
3393 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
3394 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
3395 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
3397 ULONG IndexDevice
= 0;
3398 UNICODE_STRING DeviceName
, ValueName
;
3399 HANDLE hDevicesKey
= NULL
;
3400 HANDLE hDeviceKey
= NULL
;
3402 BOOLEAN ret
= FALSE
;
3404 InitializeObjectAttributes(&ObjectAttributes
, &MultiKeyPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3405 Status
= ZwOpenKey(&hDevicesKey
, KEY_ENUMERATE_SUB_KEYS
, &ObjectAttributes
);
3406 if (!NT_SUCCESS(Status
))
3408 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3412 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3413 if (!pDeviceInformation
)
3415 DPRINT("ExAllocatePool() failed\n");
3416 Status
= STATUS_NO_MEMORY
;
3420 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3421 if (!pDeviceInformation
)
3423 DPRINT("ExAllocatePool() failed\n");
3424 Status
= STATUS_NO_MEMORY
;
3430 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3431 if (Status
== STATUS_NO_MORE_ENTRIES
)
3433 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3435 ExFreePool(pDeviceInformation
);
3436 DeviceInfoLength
= RequiredSize
;
3437 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
3438 if (!pDeviceInformation
)
3440 DPRINT("ExAllocatePool() failed\n");
3441 Status
= STATUS_NO_MEMORY
;
3444 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
3446 if (!NT_SUCCESS(Status
))
3448 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
3453 /* Open device key */
3454 DeviceName
.Length
= DeviceName
.MaximumLength
= pDeviceInformation
->NameLength
;
3455 DeviceName
.Buffer
= pDeviceInformation
->Name
;
3456 InitializeObjectAttributes(&ObjectAttributes
, &DeviceName
, OBJ_KERNEL_HANDLE
, hDevicesKey
, NULL
);
3461 if (!NT_SUCCESS(Status
))
3463 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3467 /* Read identifier */
3468 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3469 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
3471 ExFreePool(pValueInformation
);
3472 ValueInfoLength
= RequiredSize
;
3473 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
3474 if (!pValueInformation
)
3476 DPRINT("ExAllocatePool() failed\n");
3477 Status
= STATUS_NO_MEMORY
;
3480 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
3482 if (!NT_SUCCESS(Status
))
3484 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
3487 else if (pValueInformation
->Type
!= REG_SZ
)
3489 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
3493 ValueName
.Length
= ValueName
.MaximumLength
= pValueInformation
->DataLength
;
3494 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
3495 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
3496 ValueName
.Length
-= sizeof(WCHAR
);
3497 if (RtlCompareUnicodeString(&ValueName
, &AcpiBiosIdentifier
, FALSE
) == 0)
3499 DPRINT("Found ACPI BIOS\n");
3505 ZwClose(hDeviceKey
);
3510 if (pDeviceInformation
)
3511 ExFreePool(pDeviceInformation
);
3512 if (pValueInformation
)
3513 ExFreePool(pValueInformation
);
3515 ZwClose(hDevicesKey
);
3517 ZwClose(hDeviceKey
);
3522 static NTSTATUS INIT_FUNCTION
3523 IopUpdateRootKey(VOID
)
3525 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
3526 UNICODE_STRING RootPathU
= RTL_CONSTANT_STRING(L
"Root");
3527 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
3528 UNICODE_STRING DeviceDescU
= RTL_CONSTANT_STRING(L
"DeviceDesc");
3529 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
3530 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
3531 UNICODE_STRING HalAcpiDevice
= RTL_CONSTANT_STRING(L
"ACPI_HAL");
3532 UNICODE_STRING HalAcpiId
= RTL_CONSTANT_STRING(L
"0000");
3533 UNICODE_STRING HalAcpiDeviceDesc
= RTL_CONSTANT_STRING(L
"HAL ACPI");
3534 UNICODE_STRING HalAcpiHardwareID
= RTL_CONSTANT_STRING(L
"*PNP0C08\0");
3535 OBJECT_ATTRIBUTES ObjectAttributes
;
3536 HANDLE hEnum
, hRoot
, hHalAcpiDevice
, hHalAcpiId
, hLogConf
;
3539 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3540 Status
= ZwCreateKey(&hEnum
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3541 if (!NT_SUCCESS(Status
))
3543 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3547 InitializeObjectAttributes(&ObjectAttributes
, &RootPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hEnum
, NULL
);
3548 Status
= ZwCreateKey(&hRoot
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3550 if (!NT_SUCCESS(Status
))
3552 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3556 if (IopIsAcpiComputer())
3558 InitializeObjectAttributes(&ObjectAttributes
, &HalAcpiDevice
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hRoot
, NULL
);
3559 Status
= ZwCreateKey(&hHalAcpiDevice
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3561 if (!NT_SUCCESS(Status
))
3563 InitializeObjectAttributes(&ObjectAttributes
, &HalAcpiId
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hHalAcpiDevice
, NULL
);
3564 Status
= ZwCreateKey(&hHalAcpiId
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3565 ZwClose(hHalAcpiDevice
);
3566 if (!NT_SUCCESS(Status
))
3568 Status
= ZwSetValueKey(hHalAcpiId
, &DeviceDescU
, 0, REG_SZ
, HalAcpiDeviceDesc
.Buffer
, HalAcpiDeviceDesc
.MaximumLength
);
3569 if (NT_SUCCESS(Status
))
3570 Status
= ZwSetValueKey(hHalAcpiId
, &HardwareIDU
, 0, REG_MULTI_SZ
, HalAcpiHardwareID
.Buffer
, HalAcpiHardwareID
.MaximumLength
);
3571 if (NT_SUCCESS(Status
))
3573 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hHalAcpiId
, NULL
);
3574 Status
= ZwCreateKey(&hLogConf
, 0, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
3575 if (NT_SUCCESS(Status
))
3578 ZwClose(hHalAcpiId
);
3583 Status
= IopOpenRegistryKeyEx(&hEnum
, NULL
, &MultiKeyPathU
, KEY_ENUMERATE_SUB_KEYS
);
3584 if (!NT_SUCCESS(Status
))
3586 /* Nothing to do, don't return with an error status */
3587 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3589 return STATUS_SUCCESS
;
3591 Status
= IopEnumerateDetectedDevices(
3606 IopOpenRegistryKeyEx(PHANDLE KeyHandle
,
3608 PUNICODE_STRING Name
,
3609 ACCESS_MASK DesiredAccess
)
3611 OBJECT_ATTRIBUTES ObjectAttributes
;
3618 InitializeObjectAttributes(&ObjectAttributes
,
3620 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3624 Status
= ZwOpenKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
);
3631 IopGetRegistryValue(IN HANDLE Handle
,
3633 OUT PKEY_VALUE_FULL_INFORMATION
*Information
)
3635 UNICODE_STRING ValueString
;
3637 PKEY_VALUE_FULL_INFORMATION FullInformation
;
3641 RtlInitUnicodeString(&ValueString
, ValueName
);
3643 Status
= ZwQueryValueKey(Handle
,
3645 KeyValueFullInformation
,
3649 if ((Status
!= STATUS_BUFFER_OVERFLOW
) &&
3650 (Status
!= STATUS_BUFFER_TOO_SMALL
))
3655 FullInformation
= ExAllocatePool(NonPagedPool
, Size
);
3656 if (!FullInformation
) return STATUS_INSUFFICIENT_RESOURCES
;
3658 Status
= ZwQueryValueKey(Handle
,
3660 KeyValueFullInformation
,
3664 if (!NT_SUCCESS(Status
))
3666 ExFreePool(FullInformation
);
3670 *Information
= FullInformation
;
3671 return STATUS_SUCCESS
;
3674 static NTSTATUS INIT_FUNCTION
3676 PnpDriverInitializeEmpty(IN
struct _DRIVER_OBJECT
*DriverObject
, IN PUNICODE_STRING RegistryPath
)
3678 return STATUS_SUCCESS
;
3687 DPRINT("PnpInit()\n");
3689 KeInitializeSpinLock(&IopDeviceTreeLock
);
3690 ExInitializeFastMutex(&IopBusTypeGuidListLock
);
3692 /* Initialize the Bus Type GUID List */
3693 IopBusTypeGuidList
= ExAllocatePool(NonPagedPool
, sizeof(IO_BUS_TYPE_GUID_LIST
));
3694 if (!IopBusTypeGuidList
) {
3695 DPRINT1("ExAllocatePool() failed\n");
3696 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, STATUS_NO_MEMORY
, 0, 0, 0);
3699 RtlZeroMemory(IopBusTypeGuidList
, sizeof(IO_BUS_TYPE_GUID_LIST
));
3700 ExInitializeFastMutex(&IopBusTypeGuidList
->Lock
);
3702 /* Initialize PnP-Event notification support */
3703 Status
= IopInitPlugPlayEvents();
3704 if (!NT_SUCCESS(Status
))
3706 DPRINT1("IopInitPlugPlayEvents() failed\n");
3707 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
3711 * Create root device node
3714 Status
= IopCreateDriver(NULL
, PnpDriverInitializeEmpty
, NULL
, 0, 0, &IopRootDriverObject
);
3715 if (!NT_SUCCESS(Status
))
3717 DPRINT1("IoCreateDriverObject() failed\n");
3718 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
3721 Status
= IoCreateDevice(IopRootDriverObject
, 0, NULL
, FILE_DEVICE_CONTROLLER
,
3723 if (!NT_SUCCESS(Status
))
3725 DPRINT1("IoCreateDevice() failed\n");
3726 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
3729 Status
= IopCreateDeviceNode(NULL
, Pdo
, NULL
, &IopRootDeviceNode
);
3730 if (!NT_SUCCESS(Status
))
3732 DPRINT1("Insufficient resources\n");
3733 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
3736 if (!RtlCreateUnicodeString(&IopRootDeviceNode
->InstancePath
,
3739 DPRINT1("Failed to create the instance path!\n");
3740 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, STATUS_NO_MEMORY
, 0, 0, 0);
3743 /* Report the device to the user-mode pnp manager */
3744 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
3745 &IopRootDeviceNode
->InstancePath
);
3747 IopRootDeviceNode
->PhysicalDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
3748 PnpRootDriverEntry(IopRootDriverObject
, NULL
);
3749 IopRootDeviceNode
->PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
3750 IopRootDriverObject
->DriverExtension
->AddDevice(
3751 IopRootDriverObject
,
3752 IopRootDeviceNode
->PhysicalDeviceObject
);
3754 /* Move information about devices detected by Freeloader to SYSTEM\CurrentControlSet\Root\ */
3755 Status
= IopUpdateRootKey();
3756 if (!NT_SUCCESS(Status
))
3758 DPRINT1("IopUpdateRootKey() failed\n");
3759 KeBugCheckEx(PHASE1_INITIALIZATION_FAILED
, Status
, 0, 0, 0);
3763 RTL_GENERIC_COMPARE_RESULTS
3765 PiCompareInstancePath(IN PRTL_AVL_TABLE Table
,
3766 IN PVOID FirstStruct
,
3767 IN PVOID SecondStruct
)
3775 // The allocation function is called by the generic table package whenever
3776 // it needs to allocate memory for the table.
3781 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3791 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3800 PpInitializeDeviceReferenceTable(VOID
)
3802 /* Setup the guarded mutex and AVL table */
3803 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock
);
3804 RtlInitializeGenericTableAvl(
3805 &PpDeviceReferenceTable
,
3806 (PRTL_AVL_COMPARE_ROUTINE
)PiCompareInstancePath
,
3807 (PRTL_AVL_ALLOCATE_ROUTINE
)PiAllocateGenericTableEntry
,
3808 (PRTL_AVL_FREE_ROUTINE
)PiFreeGenericTableEntry
,
3816 /* Initialize the resource when accessing device registry data */
3817 ExInitializeResourceLite(&PpRegistryDeviceResource
);
3819 /* Setup the device reference AVL table */
3820 PpInitializeDeviceReferenceTable();
3828 /* Check the initialization phase */
3829 switch (ExpInitializationPhase
)
3834 return PiInitPhase0();
3840 //return PiInitPhase1();
3844 /* Don't know any other phase! Bugcheck! */
3845 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
3850 /* PUBLIC FUNCTIONS **********************************************************/
3857 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject
,
3858 IN DEVICE_REGISTRY_PROPERTY DeviceProperty
,
3859 IN ULONG BufferLength
,
3860 OUT PVOID PropertyBuffer
,
3861 OUT PULONG ResultLength
)
3863 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
3864 DEVICE_CAPABILITIES DeviceCaps
;
3869 POBJECT_NAME_INFORMATION ObjectNameInfo
= NULL
;
3870 ULONG RequiredLength
, ObjectNameInfoLength
;
3872 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject
, DeviceProperty
);
3876 if (DeviceNode
== NULL
)
3877 return STATUS_INVALID_DEVICE_REQUEST
;
3879 switch (DeviceProperty
)
3881 case DevicePropertyBusNumber
:
3882 Length
= sizeof(ULONG
);
3883 Data
= &DeviceNode
->ChildBusNumber
;
3886 /* Complete, untested */
3887 case DevicePropertyBusTypeGuid
:
3889 if ((DeviceNode
->ChildBusTypeIndex
!= 0xFFFF) &&
3890 (DeviceNode
->ChildBusTypeIndex
< IopBusTypeGuidList
->GuidCount
))
3892 /* Return the GUID */
3893 *ResultLength
= sizeof(GUID
);
3895 /* Check if the buffer given was large enough */
3896 if (BufferLength
< *ResultLength
)
3898 return STATUS_BUFFER_TOO_SMALL
;
3902 RtlCopyMemory(PropertyBuffer
,
3903 &(IopBusTypeGuidList
->Guids
[DeviceNode
->ChildBusTypeIndex
]),
3905 return STATUS_SUCCESS
;
3909 return STATUS_OBJECT_NAME_NOT_FOUND
;
3913 case DevicePropertyLegacyBusType
:
3914 Length
= sizeof(INTERFACE_TYPE
);
3915 Data
= &DeviceNode
->ChildInterfaceType
;
3918 case DevicePropertyAddress
:
3919 /* Query the device caps */
3920 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
);
3921 if (NT_SUCCESS(Status
) && (DeviceCaps
.Address
!= MAXULONG
))
3924 *ResultLength
= sizeof(ULONG
);
3926 /* Check if the buffer given was large enough */
3927 if (BufferLength
< *ResultLength
)
3929 return STATUS_BUFFER_TOO_SMALL
;
3932 /* Return address */
3933 *(PULONG
)PropertyBuffer
= DeviceCaps
.Address
;
3934 return STATUS_SUCCESS
;
3938 return STATUS_OBJECT_NAME_NOT_FOUND
;
3942 // case DevicePropertyUINumber:
3943 // if (DeviceNode->CapabilityFlags == NULL)
3944 // return STATUS_INVALID_DEVICE_REQUEST;
3945 // Length = sizeof(ULONG);
3946 // Data = &DeviceNode->CapabilityFlags->UINumber;
3949 case DevicePropertyClassName
:
3950 case DevicePropertyClassGuid
:
3951 case DevicePropertyDriverKeyName
:
3952 case DevicePropertyManufacturer
:
3953 case DevicePropertyFriendlyName
:
3954 case DevicePropertyHardwareID
:
3955 case DevicePropertyCompatibleIDs
:
3956 case DevicePropertyDeviceDescription
:
3957 case DevicePropertyLocationInformation
:
3958 case DevicePropertyUINumber
:
3960 LPCWSTR RegistryPropertyName
;
3961 UNICODE_STRING EnumRoot
= RTL_CONSTANT_STRING(ENUM_ROOT
);
3962 UNICODE_STRING ValueName
;
3963 KEY_VALUE_PARTIAL_INFORMATION
*ValueInformation
;
3964 ULONG ValueInformationLength
;
3965 HANDLE KeyHandle
, EnumRootHandle
;
3968 switch (DeviceProperty
)
3970 case DevicePropertyClassName
:
3971 RegistryPropertyName
= L
"Class"; break;
3972 case DevicePropertyClassGuid
:
3973 RegistryPropertyName
= L
"ClassGuid"; break;
3974 case DevicePropertyDriverKeyName
:
3975 RegistryPropertyName
= L
"Driver"; break;
3976 case DevicePropertyManufacturer
:
3977 RegistryPropertyName
= L
"Mfg"; break;
3978 case DevicePropertyFriendlyName
:
3979 RegistryPropertyName
= L
"FriendlyName"; break;
3980 case DevicePropertyHardwareID
:
3981 RegistryPropertyName
= L
"HardwareID"; break;
3982 case DevicePropertyCompatibleIDs
:
3983 RegistryPropertyName
= L
"CompatibleIDs"; break;
3984 case DevicePropertyDeviceDescription
:
3985 RegistryPropertyName
= L
"DeviceDesc"; break;
3986 case DevicePropertyLocationInformation
:
3987 RegistryPropertyName
= L
"LocationInformation"; break;
3988 case DevicePropertyUINumber
:
3989 RegistryPropertyName
= L
"UINumber"; break;
3991 /* Should not happen */
3993 return STATUS_UNSUCCESSFUL
;
3996 DPRINT("Registry property %S\n", RegistryPropertyName
);
3999 Status
= IopOpenRegistryKeyEx(&EnumRootHandle
, NULL
,
4000 &EnumRoot
, KEY_READ
);
4001 if (!NT_SUCCESS(Status
))
4003 DPRINT1("Error opening ENUM_ROOT, Status=0x%08x\n", Status
);
4007 /* Open instance key */
4008 Status
= IopOpenRegistryKeyEx(&KeyHandle
, EnumRootHandle
,
4009 &DeviceNode
->InstancePath
, KEY_READ
);
4010 if (!NT_SUCCESS(Status
))
4012 DPRINT1("Error opening InstancePath, Status=0x%08x\n", Status
);
4013 ZwClose(EnumRootHandle
);
4017 /* Allocate buffer to read as much data as required by the caller */
4018 ValueInformationLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
,
4019 Data
[0]) + BufferLength
;
4020 ValueInformation
= ExAllocatePool(PagedPool
, ValueInformationLength
);
4021 if (!ValueInformation
)
4024 return STATUS_INSUFFICIENT_RESOURCES
;
4027 /* Read the value */
4028 RtlInitUnicodeString(&ValueName
, RegistryPropertyName
);
4029 Status
= ZwQueryValueKey(KeyHandle
, &ValueName
,
4030 KeyValuePartialInformation
, ValueInformation
,
4031 ValueInformationLength
,
4032 &ValueInformationLength
);
4036 *ResultLength
= ValueInformation
->DataLength
;
4038 if (!NT_SUCCESS(Status
))
4040 ExFreePool(ValueInformation
);
4041 if (Status
== STATUS_BUFFER_OVERFLOW
)
4042 return STATUS_BUFFER_TOO_SMALL
;
4043 DPRINT1("Problem: Status=0x%08x, ResultLength = %d\n", Status
, *ResultLength
);
4047 /* FIXME: Verify the value (NULL-terminated, correct format). */
4048 RtlCopyMemory(PropertyBuffer
, ValueInformation
->Data
,
4049 ValueInformation
->DataLength
);
4050 ExFreePool(ValueInformation
);
4052 return STATUS_SUCCESS
;
4055 case DevicePropertyBootConfiguration
:
4057 if (DeviceNode
->BootResources
->Count
!= 0)
4059 Length
= CM_RESOURCE_LIST_SIZE(DeviceNode
->BootResources
);
4061 Data
= DeviceNode
->BootResources
;
4064 /* FIXME: use a translated boot configuration instead */
4065 case DevicePropertyBootConfigurationTranslated
:
4067 if (DeviceNode
->BootResources
->Count
!= 0)
4069 Length
= CM_RESOURCE_LIST_SIZE(DeviceNode
->BootResources
);
4071 Data
= DeviceNode
->BootResources
;
4074 case DevicePropertyEnumeratorName
:
4075 /* A buffer overflow can't happen here, since InstancePath
4076 * always contains the enumerator name followed by \\ */
4077 Ptr
= wcschr(DeviceNode
->InstancePath
.Buffer
, L
'\\');
4079 Length
= (Ptr
- DeviceNode
->InstancePath
.Buffer
) * sizeof(WCHAR
);
4080 Data
= DeviceNode
->InstancePath
.Buffer
;
4083 case DevicePropertyPhysicalDeviceObjectName
:
4084 Status
= ObQueryNameString(DeviceNode
->PhysicalDeviceObject
,
4088 if (Status
== STATUS_SUCCESS
)
4093 else if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
4095 ObjectNameInfoLength
= RequiredLength
;
4096 ObjectNameInfo
= ExAllocatePool(PagedPool
, ObjectNameInfoLength
);
4097 if (!ObjectNameInfo
)
4098 return STATUS_INSUFFICIENT_RESOURCES
;
4100 Status
= ObQueryNameString(DeviceNode
->PhysicalDeviceObject
,
4102 ObjectNameInfoLength
,
4104 if (NT_SUCCESS(Status
))
4106 Length
= ObjectNameInfo
->Name
.Length
;
4107 Data
= ObjectNameInfo
->Name
.Buffer
;
4117 return STATUS_INVALID_PARAMETER_2
;
4120 /* Prepare returned values */
4121 *ResultLength
= Length
;
4122 if (BufferLength
< Length
)
4124 if (ObjectNameInfo
!= NULL
)
4125 ExFreePool(ObjectNameInfo
);
4127 return STATUS_BUFFER_TOO_SMALL
;
4129 RtlCopyMemory(PropertyBuffer
, Data
, Length
);
4131 /* NULL terminate the string (if required) */
4132 if (DeviceProperty
== DevicePropertyEnumeratorName
||
4133 DeviceProperty
== DevicePropertyPhysicalDeviceObjectName
)
4134 ((LPWSTR
)PropertyBuffer
)[Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
4136 if (ObjectNameInfo
!= NULL
)
4137 ExFreePool(ObjectNameInfo
);
4139 return STATUS_SUCCESS
;
4147 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4153 * @name IoOpenDeviceRegistryKey
4155 * Open a registry key unique for a specified driver or device instance.
4157 * @param DeviceObject Device to get the registry key for.
4158 * @param DevInstKeyType Type of the key to return.
4159 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
4160 * @param DevInstRegKey Handle to the opened registry key on
4161 * successful return.
4169 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject
,
4170 IN ULONG DevInstKeyType
,
4171 IN ACCESS_MASK DesiredAccess
,
4172 OUT PHANDLE DevInstRegKey
)
4174 static WCHAR RootKeyName
[] =
4175 L
"\\Registry\\Machine\\System\\CurrentControlSet\\";
4176 static WCHAR ProfileKeyName
[] =
4177 L
"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
4178 static WCHAR ClassKeyName
[] = L
"Control\\Class\\";
4179 static WCHAR EnumKeyName
[] = L
"Enum\\";
4180 static WCHAR DeviceParametersKeyName
[] = L
"Device Parameters";
4181 ULONG KeyNameLength
;
4182 LPWSTR KeyNameBuffer
;
4183 UNICODE_STRING KeyName
;
4184 ULONG DriverKeyLength
;
4185 OBJECT_ATTRIBUTES ObjectAttributes
;
4186 PDEVICE_NODE DeviceNode
= NULL
;
4189 DPRINT("IoOpenDeviceRegistryKey() called\n");
4191 if ((DevInstKeyType
& (PLUGPLAY_REGKEY_DEVICE
| PLUGPLAY_REGKEY_DRIVER
)) == 0)
4193 DPRINT1("IoOpenDeviceRegistryKey(): got wrong params, exiting... \n");
4194 return STATUS_INVALID_PARAMETER
;
4198 * Calculate the length of the base key name. This is the full
4199 * name for driver key or the name excluding "Device Parameters"
4200 * subkey for device key.
4203 KeyNameLength
= sizeof(RootKeyName
);
4204 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4205 KeyNameLength
+= sizeof(ProfileKeyName
) - sizeof(UNICODE_NULL
);
4206 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4208 KeyNameLength
+= sizeof(ClassKeyName
) - sizeof(UNICODE_NULL
);
4209 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4210 0, NULL
, &DriverKeyLength
);
4211 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
4213 KeyNameLength
+= DriverKeyLength
;
4217 DeviceNode
= IopGetDeviceNode(DeviceObject
);
4218 KeyNameLength
+= sizeof(EnumKeyName
) - sizeof(UNICODE_NULL
) +
4219 DeviceNode
->InstancePath
.Length
;
4223 * Now allocate the buffer for the key name...
4226 KeyNameBuffer
= ExAllocatePool(PagedPool
, KeyNameLength
);
4227 if (KeyNameBuffer
== NULL
)
4228 return STATUS_INSUFFICIENT_RESOURCES
;
4231 KeyName
.MaximumLength
= (USHORT
)KeyNameLength
;
4232 KeyName
.Buffer
= KeyNameBuffer
;
4235 * ...and build the key name.
4238 KeyName
.Length
+= sizeof(RootKeyName
) - sizeof(UNICODE_NULL
);
4239 RtlCopyMemory(KeyNameBuffer
, RootKeyName
, KeyName
.Length
);
4241 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4242 RtlAppendUnicodeToString(&KeyName
, ProfileKeyName
);
4244 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4246 RtlAppendUnicodeToString(&KeyName
, ClassKeyName
);
4247 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4248 DriverKeyLength
, KeyNameBuffer
+
4249 (KeyName
.Length
/ sizeof(WCHAR
)),
4251 if (!NT_SUCCESS(Status
))
4253 DPRINT1("Call to IoGetDeviceProperty() failed with Status 0x%08lx\n", Status
);
4254 ExFreePool(KeyNameBuffer
);
4257 KeyName
.Length
+= (USHORT
)DriverKeyLength
- sizeof(UNICODE_NULL
);
4261 RtlAppendUnicodeToString(&KeyName
, EnumKeyName
);
4262 Status
= RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->InstancePath
);
4263 if (DeviceNode
->InstancePath
.Length
== 0)
4265 ExFreePool(KeyNameBuffer
);
4271 * Open the base key.
4273 Status
= IopOpenRegistryKeyEx(DevInstRegKey
, NULL
, &KeyName
, DesiredAccess
);
4274 if (!NT_SUCCESS(Status
))
4276 DPRINT1("IoOpenDeviceRegistryKey(%wZ): Base key doesn't exist, exiting... (Status 0x%08lx)\n", &KeyName
, Status
);
4277 ExFreePool(KeyNameBuffer
);
4280 ExFreePool(KeyNameBuffer
);
4283 * For driver key we're done now.
4286 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4290 * Let's go further. For device key we must open "Device Parameters"
4291 * subkey and create it if it doesn't exist yet.
4294 RtlInitUnicodeString(&KeyName
, DeviceParametersKeyName
);
4295 InitializeObjectAttributes(&ObjectAttributes
, &KeyName
,
4296 OBJ_CASE_INSENSITIVE
, *DevInstRegKey
, NULL
);
4297 Status
= ZwCreateKey(DevInstRegKey
, DesiredAccess
, &ObjectAttributes
,
4298 0, NULL
, REG_OPTION_NON_VOLATILE
, NULL
);
4299 ZwClose(ObjectAttributes
.RootDirectory
);
4309 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4319 IoInvalidateDeviceRelations(
4320 IN PDEVICE_OBJECT DeviceObject
,
4321 IN DEVICE_RELATION_TYPE Type
)
4323 PIO_WORKITEM WorkItem
;
4324 PINVALIDATE_DEVICE_RELATION_DATA Data
;
4326 Data
= ExAllocatePool(PagedPool
, sizeof(INVALIDATE_DEVICE_RELATION_DATA
));
4329 WorkItem
= IoAllocateWorkItem(DeviceObject
);
4336 ObReferenceObject(DeviceObject
);
4337 Data
->DeviceObject
= DeviceObject
;
4339 Data
->WorkItem
= WorkItem
;
4343 IopAsynchronousInvalidateDeviceRelations
,
4353 IoSynchronousInvalidateDeviceRelations(
4354 IN PDEVICE_OBJECT DeviceObject
,
4355 IN DEVICE_RELATION_TYPE Type
)
4362 /* Enumerate the device */
4363 return IopEnumerateDevice(DeviceObject
);
4364 case PowerRelations
:
4365 /* Not handled yet */
4366 return STATUS_NOT_IMPLEMENTED
;
4367 case TargetDeviceRelation
:
4369 return STATUS_SUCCESS
;
4371 /* Ejection relations are not supported */
4372 return STATUS_NOT_SUPPORTED
;
4381 IoTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
4383 IN PHYSICAL_ADDRESS BusAddress
,
4384 IN OUT PULONG AddressSpace
,
4385 OUT PPHYSICAL_ADDRESS TranslatedAddress
)