2 * PROJECT: ReactOS Kernel
3 * COPYRIGHT: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/pnpmgr/pnpmgr.c
5 * PURPOSE: Initializes the PnP manager
6 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7 * Copyright 2007 Hervé Poussineau (hpoussin@reactos.org)
10 /* INCLUDES ******************************************************************/
16 /* GLOBALS *******************************************************************/
18 PDEVICE_NODE IopRootDeviceNode
;
19 KSPIN_LOCK IopDeviceTreeLock
;
20 ERESOURCE PpRegistryDeviceResource
;
21 KGUARDED_MUTEX PpDeviceReferenceTableLock
;
22 RTL_AVL_TABLE PpDeviceReferenceTable
;
24 extern ULONG ExpInitializationPhase
;
25 extern BOOLEAN ExpInTextModeSetup
;
26 extern BOOLEAN PnpSystemInit
;
28 /* DATA **********************************************************************/
30 PDRIVER_OBJECT IopRootDriverObject
;
31 PIO_BUS_TYPE_GUID_LIST PnpBusTypeGuidList
= NULL
;
33 typedef struct _INVALIDATE_DEVICE_RELATION_DATA
35 PDEVICE_OBJECT DeviceObject
;
36 DEVICE_RELATION_TYPE Type
;
37 PIO_WORKITEM WorkItem
;
38 } INVALIDATE_DEVICE_RELATION_DATA
, *PINVALIDATE_DEVICE_RELATION_DATA
;
40 /* FUNCTIONS *****************************************************************/
43 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
44 IN ULONG CreateOptions
,
48 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
);
51 IopPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
, BOOLEAN Force
);
55 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject
)
57 return ((PEXTENDED_DEVOBJ_EXTENSION
)DeviceObject
->DeviceObjectExtension
)->DeviceNode
;
61 IopFixupDeviceId(PWCHAR String
)
63 ULONG Length
= wcslen(String
), i
;
65 for (i
= 0; i
< Length
; i
++)
67 if (String
[i
] == L
'\\')
74 IopInstallCriticalDevice(PDEVICE_NODE DeviceNode
)
77 HANDLE CriticalDeviceKey
, InstanceKey
;
78 OBJECT_ATTRIBUTES ObjectAttributes
;
79 UNICODE_STRING CriticalDeviceKeyU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\CriticalDeviceDatabase");
80 UNICODE_STRING CompatibleIdU
= RTL_CONSTANT_STRING(L
"CompatibleIDs");
81 UNICODE_STRING HardwareIdU
= RTL_CONSTANT_STRING(L
"HardwareID");
82 UNICODE_STRING ServiceU
= RTL_CONSTANT_STRING(L
"Service");
83 UNICODE_STRING ClassGuidU
= RTL_CONSTANT_STRING(L
"ClassGUID");
84 PKEY_VALUE_PARTIAL_INFORMATION PartialInfo
;
85 ULONG HidLength
= 0, CidLength
= 0, BufferLength
;
86 PWCHAR IdBuffer
, OriginalIdBuffer
;
88 /* Open the device instance key */
89 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
90 if (Status
!= STATUS_SUCCESS
)
93 Status
= ZwQueryValueKey(InstanceKey
,
95 KeyValuePartialInformation
,
99 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
101 ZwClose(InstanceKey
);
105 Status
= ZwQueryValueKey(InstanceKey
,
107 KeyValuePartialInformation
,
111 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
116 BufferLength
= HidLength
+ CidLength
;
117 BufferLength
-= (((CidLength
!= 0) ? 2 : 1) * FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
));
119 /* Allocate a buffer to hold data from both */
120 OriginalIdBuffer
= IdBuffer
= ExAllocatePool(PagedPool
, BufferLength
);
123 ZwClose(InstanceKey
);
127 /* Compute the buffer size */
128 if (HidLength
> CidLength
)
129 BufferLength
= HidLength
;
131 BufferLength
= CidLength
;
133 PartialInfo
= ExAllocatePool(PagedPool
, BufferLength
);
136 ZwClose(InstanceKey
);
137 ExFreePool(OriginalIdBuffer
);
141 Status
= ZwQueryValueKey(InstanceKey
,
143 KeyValuePartialInformation
,
147 if (Status
!= STATUS_SUCCESS
)
149 ExFreePool(PartialInfo
);
150 ExFreePool(OriginalIdBuffer
);
151 ZwClose(InstanceKey
);
155 /* Copy in HID info first (without 2nd terminating NULL if CID is present) */
156 HidLength
= PartialInfo
->DataLength
- ((CidLength
!= 0) ? sizeof(WCHAR
) : 0);
157 RtlCopyMemory(IdBuffer
, PartialInfo
->Data
, HidLength
);
161 Status
= ZwQueryValueKey(InstanceKey
,
163 KeyValuePartialInformation
,
167 if (Status
!= STATUS_SUCCESS
)
169 ExFreePool(PartialInfo
);
170 ExFreePool(OriginalIdBuffer
);
171 ZwClose(InstanceKey
);
176 CidLength
= PartialInfo
->DataLength
;
177 RtlCopyMemory(((PUCHAR
)IdBuffer
) + HidLength
, PartialInfo
->Data
, CidLength
);
180 /* Free our temp buffer */
181 ExFreePool(PartialInfo
);
183 InitializeObjectAttributes(&ObjectAttributes
,
185 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
188 Status
= ZwOpenKey(&CriticalDeviceKey
,
189 KEY_ENUMERATE_SUB_KEYS
,
191 if (!NT_SUCCESS(Status
))
193 /* The critical device database doesn't exist because
194 * we're probably in 1st stage setup, but it's ok */
195 ExFreePool(OriginalIdBuffer
);
196 ZwClose(InstanceKey
);
202 ULONG StringLength
= (ULONG
)wcslen(IdBuffer
) + 1, Index
;
204 IopFixupDeviceId(IdBuffer
);
206 /* Look through all subkeys for a match */
207 for (Index
= 0; TRUE
; Index
++)
210 PKEY_BASIC_INFORMATION BasicInfo
;
212 Status
= ZwEnumerateKey(CriticalDeviceKey
,
218 if (Status
== STATUS_NO_MORE_ENTRIES
)
220 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
222 UNICODE_STRING ChildIdNameU
, RegKeyNameU
;
224 BasicInfo
= ExAllocatePool(PagedPool
, NeededLength
);
228 ExFreePool(OriginalIdBuffer
);
229 ZwClose(CriticalDeviceKey
);
230 ZwClose(InstanceKey
);
234 Status
= ZwEnumerateKey(CriticalDeviceKey
,
240 if (Status
!= STATUS_SUCCESS
)
242 /* This shouldn't happen */
243 ExFreePool(BasicInfo
);
247 ChildIdNameU
.Buffer
= IdBuffer
;
248 ChildIdNameU
.MaximumLength
= ChildIdNameU
.Length
= (StringLength
- 1) * sizeof(WCHAR
);
249 RegKeyNameU
.Buffer
= BasicInfo
->Name
;
250 RegKeyNameU
.MaximumLength
= RegKeyNameU
.Length
= BasicInfo
->NameLength
;
252 if (RtlEqualUnicodeString(&ChildIdNameU
, &RegKeyNameU
, TRUE
))
254 HANDLE ChildKeyHandle
;
256 InitializeObjectAttributes(&ObjectAttributes
,
258 OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
,
262 Status
= ZwOpenKey(&ChildKeyHandle
,
265 if (Status
!= STATUS_SUCCESS
)
267 ExFreePool(BasicInfo
);
271 /* Check if there's already a driver installed */
272 Status
= ZwQueryValueKey(InstanceKey
,
274 KeyValuePartialInformation
,
278 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
280 ExFreePool(BasicInfo
);
284 Status
= ZwQueryValueKey(ChildKeyHandle
,
286 KeyValuePartialInformation
,
290 if (Status
!= STATUS_BUFFER_OVERFLOW
&& Status
!= STATUS_BUFFER_TOO_SMALL
)
292 ExFreePool(BasicInfo
);
296 PartialInfo
= ExAllocatePool(PagedPool
, NeededLength
);
299 ExFreePool(OriginalIdBuffer
);
300 ExFreePool(BasicInfo
);
301 ZwClose(InstanceKey
);
302 ZwClose(ChildKeyHandle
);
303 ZwClose(CriticalDeviceKey
);
307 /* Read ClassGUID entry in the CDDB */
308 Status
= ZwQueryValueKey(ChildKeyHandle
,
310 KeyValuePartialInformation
,
314 if (Status
!= STATUS_SUCCESS
)
316 ExFreePool(BasicInfo
);
320 /* Write it to the ENUM key */
321 Status
= ZwSetValueKey(InstanceKey
,
326 PartialInfo
->DataLength
);
327 if (Status
!= STATUS_SUCCESS
)
329 ExFreePool(BasicInfo
);
330 ExFreePool(PartialInfo
);
331 ZwClose(ChildKeyHandle
);
335 Status
= ZwQueryValueKey(ChildKeyHandle
,
337 KeyValuePartialInformation
,
341 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
343 ExFreePool(PartialInfo
);
344 PartialInfo
= ExAllocatePool(PagedPool
, NeededLength
);
347 ExFreePool(OriginalIdBuffer
);
348 ExFreePool(BasicInfo
);
349 ZwClose(InstanceKey
);
350 ZwClose(ChildKeyHandle
);
351 ZwClose(CriticalDeviceKey
);
355 /* Read the service entry from the CDDB */
356 Status
= ZwQueryValueKey(ChildKeyHandle
,
358 KeyValuePartialInformation
,
362 if (Status
!= STATUS_SUCCESS
)
364 ExFreePool(BasicInfo
);
365 ExFreePool(PartialInfo
);
366 ZwClose(ChildKeyHandle
);
370 /* Write it to the ENUM key */
371 Status
= ZwSetValueKey(InstanceKey
,
376 PartialInfo
->DataLength
);
377 if (Status
!= STATUS_SUCCESS
)
379 ExFreePool(BasicInfo
);
380 ExFreePool(PartialInfo
);
381 ZwClose(ChildKeyHandle
);
385 DPRINT1("Installed service '%S' for critical device '%wZ'\n", PartialInfo
->Data
, &ChildIdNameU
);
389 DPRINT1("Installed NULL service for critical device '%wZ'\n", &ChildIdNameU
);
392 ExFreePool(OriginalIdBuffer
);
393 ExFreePool(PartialInfo
);
394 ExFreePool(BasicInfo
);
395 ZwClose(InstanceKey
);
396 ZwClose(ChildKeyHandle
);
397 ZwClose(CriticalDeviceKey
);
403 ExFreePool(BasicInfo
);
407 /* Umm, not sure what happened here */
412 /* Advance to the next ID */
413 IdBuffer
+= StringLength
;
416 ExFreePool(OriginalIdBuffer
);
417 ZwClose(InstanceKey
);
418 ZwClose(CriticalDeviceKey
);
423 IopInitializeDevice(PDEVICE_NODE DeviceNode
,
424 PDRIVER_OBJECT DriverObject
)
431 /* Special case for bus driven devices */
432 DeviceNode
->Flags
|= DNF_ADDED
;
433 return STATUS_SUCCESS
;
436 if (!DriverObject
->DriverExtension
->AddDevice
)
438 DeviceNode
->Flags
|= DNF_LEGACY_DRIVER
;
441 if (DeviceNode
->Flags
& DNF_LEGACY_DRIVER
)
443 DeviceNode
->Flags
|= DNF_ADDED
+ DNF_STARTED
;
444 return STATUS_SUCCESS
;
447 /* This is a Plug and Play driver */
448 DPRINT("Plug and Play driver found\n");
449 ASSERT(DeviceNode
->PhysicalDeviceObject
);
451 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
452 &DriverObject
->DriverName
,
453 &DeviceNode
->InstancePath
);
454 Status
= DriverObject
->DriverExtension
->AddDevice(
455 DriverObject
, DeviceNode
->PhysicalDeviceObject
);
456 if (!NT_SUCCESS(Status
))
458 DPRINT1("%wZ->AddDevice(%wZ) failed with status 0x%x\n",
459 &DriverObject
->DriverName
,
460 &DeviceNode
->InstancePath
,
462 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
466 Fdo
= IoGetAttachedDeviceReference(DeviceNode
->PhysicalDeviceObject
);
468 /* Check if we have a ACPI device (needed for power management) */
469 if (Fdo
->DeviceType
== FILE_DEVICE_ACPI
)
471 static BOOLEAN SystemPowerDeviceNodeCreated
= FALSE
;
473 /* There can be only one system power device */
474 if (!SystemPowerDeviceNodeCreated
)
476 PopSystemPowerDeviceNode
= DeviceNode
;
477 ObReferenceObject(PopSystemPowerDeviceNode
->PhysicalDeviceObject
);
478 SystemPowerDeviceNodeCreated
= TRUE
;
482 ObDereferenceObject(Fdo
);
484 IopDeviceNodeSetFlag(DeviceNode
, DNF_ADDED
);
486 return STATUS_SUCCESS
;
492 IopSendEject(IN PDEVICE_OBJECT DeviceObject
)
494 IO_STACK_LOCATION Stack
;
497 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
498 Stack
.MajorFunction
= IRP_MJ_PNP
;
499 Stack
.MinorFunction
= IRP_MN_EJECT
;
501 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
507 IopSendSurpriseRemoval(IN PDEVICE_OBJECT DeviceObject
)
509 IO_STACK_LOCATION Stack
;
512 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
513 Stack
.MajorFunction
= IRP_MJ_PNP
;
514 Stack
.MinorFunction
= IRP_MN_SURPRISE_REMOVAL
;
516 /* Drivers should never fail a IRP_MN_SURPRISE_REMOVAL request */
517 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
523 IopQueryRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
525 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
526 IO_STACK_LOCATION Stack
;
532 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVE_PENDING
,
533 &DeviceNode
->InstancePath
);
535 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
536 Stack
.MajorFunction
= IRP_MJ_PNP
;
537 Stack
.MinorFunction
= IRP_MN_QUERY_REMOVE_DEVICE
;
539 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
541 IopNotifyPlugPlayNotification(DeviceObject
,
542 EventCategoryTargetDeviceChange
,
543 &GUID_TARGET_DEVICE_QUERY_REMOVE
,
547 if (!NT_SUCCESS(Status
))
549 DPRINT1("Removal vetoed by %wZ\n", &DeviceNode
->InstancePath
);
550 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVAL_VETOED
,
551 &DeviceNode
->InstancePath
);
560 IopQueryStopDevice(IN PDEVICE_OBJECT DeviceObject
)
562 IO_STACK_LOCATION Stack
;
565 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
566 Stack
.MajorFunction
= IRP_MJ_PNP
;
567 Stack
.MinorFunction
= IRP_MN_QUERY_STOP_DEVICE
;
569 return IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
575 IopSendRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
577 IO_STACK_LOCATION Stack
;
580 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
581 Stack
.MajorFunction
= IRP_MJ_PNP
;
582 Stack
.MinorFunction
= IRP_MN_REMOVE_DEVICE
;
584 /* Drivers should never fail a IRP_MN_REMOVE_DEVICE request */
585 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
587 IopNotifyPlugPlayNotification(DeviceObject
,
588 EventCategoryTargetDeviceChange
,
589 &GUID_TARGET_DEVICE_REMOVE_COMPLETE
,
597 IopCancelRemoveDevice(IN PDEVICE_OBJECT DeviceObject
)
599 IO_STACK_LOCATION Stack
;
602 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
603 Stack
.MajorFunction
= IRP_MJ_PNP
;
604 Stack
.MinorFunction
= IRP_MN_CANCEL_REMOVE_DEVICE
;
606 /* Drivers should never fail a IRP_MN_CANCEL_REMOVE_DEVICE request */
607 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
609 IopNotifyPlugPlayNotification(DeviceObject
,
610 EventCategoryTargetDeviceChange
,
611 &GUID_TARGET_DEVICE_REMOVE_CANCELLED
,
619 IopSendStopDevice(IN PDEVICE_OBJECT DeviceObject
)
621 IO_STACK_LOCATION Stack
;
624 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
625 Stack
.MajorFunction
= IRP_MJ_PNP
;
626 Stack
.MinorFunction
= IRP_MN_STOP_DEVICE
;
628 /* Drivers should never fail a IRP_MN_STOP_DEVICE request */
629 IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
634 IopStartDevice2(IN PDEVICE_OBJECT DeviceObject
)
636 IO_STACK_LOCATION Stack
;
637 PDEVICE_NODE DeviceNode
;
640 DEVICE_CAPABILITIES DeviceCapabilities
;
642 /* Get the device node */
643 DeviceNode
= IopGetDeviceNode(DeviceObject
);
645 ASSERT(!(DeviceNode
->Flags
& DNF_DISABLED
));
647 /* Build the I/O stack locaiton */
648 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
649 Stack
.MajorFunction
= IRP_MJ_PNP
;
650 Stack
.MinorFunction
= IRP_MN_START_DEVICE
;
652 Stack
.Parameters
.StartDevice
.AllocatedResources
=
653 DeviceNode
->ResourceList
;
654 Stack
.Parameters
.StartDevice
.AllocatedResourcesTranslated
=
655 DeviceNode
->ResourceListTranslated
;
658 Status
= IopSynchronousCall(DeviceObject
, &Stack
, &Dummy
);
659 if (!NT_SUCCESS(Status
))
661 /* Send an IRP_MN_REMOVE_DEVICE request */
662 IopRemoveDevice(DeviceNode
);
664 /* Set the appropriate flag */
665 DeviceNode
->Flags
|= DNF_START_FAILED
;
667 DPRINT1("Warning: PnP Start failed (%wZ) [Status: 0x%x]\n", &DeviceNode
->InstancePath
, Status
);
671 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after start)\n");
673 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
674 if (!NT_SUCCESS(Status
))
676 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
679 /* Invalidate device state so IRP_MN_QUERY_PNP_DEVICE_STATE is sent */
680 IoInvalidateDeviceState(DeviceObject
);
682 /* Otherwise, mark us as started */
683 DeviceNode
->Flags
|= DNF_STARTED
;
684 DeviceNode
->Flags
&= ~DNF_STOPPED
;
686 /* We now need enumeration */
687 DeviceNode
->Flags
|= DNF_NEED_ENUMERATION_ONLY
;
692 IopStartAndEnumerateDevice(IN PDEVICE_NODE DeviceNode
)
694 PDEVICE_OBJECT DeviceObject
;
699 ASSERT((DeviceNode
->Flags
& DNF_ADDED
));
700 ASSERT((DeviceNode
->Flags
& (DNF_RESOURCE_ASSIGNED
|
701 DNF_RESOURCE_REPORTED
|
702 DNF_NO_RESOURCE_REQUIRED
)));
704 /* Get the device object */
705 DeviceObject
= DeviceNode
->PhysicalDeviceObject
;
707 /* Check if we're not started yet */
708 if (!(DeviceNode
->Flags
& DNF_STARTED
))
711 IopStartDevice2(DeviceObject
);
714 /* Do we need to query IDs? This happens in the case of manual reporting */
716 if (DeviceNode
->Flags
& DNF_NEED_QUERY_IDS
)
718 DPRINT1("Warning: Device node has DNF_NEED_QUERY_IDS\n");
719 /* And that case shouldn't happen yet */
724 /* Make sure we're started, and check if we need enumeration */
725 if ((DeviceNode
->Flags
& DNF_STARTED
) &&
726 (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
))
729 IoSynchronousInvalidateDeviceRelations(DeviceObject
, BusRelations
);
730 Status
= STATUS_SUCCESS
;
735 Status
= STATUS_SUCCESS
;
744 PDEVICE_NODE DeviceNode
)
748 DPRINT("Stopping device: %wZ\n", &DeviceNode
->InstancePath
);
750 Status
= IopQueryStopDevice(DeviceNode
->PhysicalDeviceObject
);
751 if (NT_SUCCESS(Status
))
753 IopSendStopDevice(DeviceNode
->PhysicalDeviceObject
);
755 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
756 DeviceNode
->Flags
|= DNF_STOPPED
;
758 return STATUS_SUCCESS
;
766 PDEVICE_NODE DeviceNode
)
769 HANDLE InstanceHandle
= INVALID_HANDLE_VALUE
, ControlHandle
= INVALID_HANDLE_VALUE
;
770 UNICODE_STRING KeyName
;
771 OBJECT_ATTRIBUTES ObjectAttributes
;
773 if (DeviceNode
->Flags
& DNF_DISABLED
)
774 return STATUS_SUCCESS
;
776 Status
= IopAssignDeviceResources(DeviceNode
);
777 if (!NT_SUCCESS(Status
))
781 IopStartAndEnumerateDevice(DeviceNode
);
783 /* FIX: Should be done in new device instance code */
784 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceHandle
);
785 if (!NT_SUCCESS(Status
))
788 /* FIX: Should be done in IoXxxPrepareDriverLoading */
790 RtlInitUnicodeString(&KeyName
, L
"Control");
791 InitializeObjectAttributes(&ObjectAttributes
,
793 OBJ_CASE_INSENSITIVE
,
796 Status
= ZwCreateKey(&ControlHandle
, KEY_SET_VALUE
, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
797 if (!NT_SUCCESS(Status
))
800 RtlInitUnicodeString(&KeyName
, L
"ActiveService");
801 Status
= ZwSetValueKey(ControlHandle
, &KeyName
, 0, REG_SZ
, DeviceNode
->ServiceName
.Buffer
, DeviceNode
->ServiceName
.Length
);
805 if (ControlHandle
!= INVALID_HANDLE_VALUE
)
806 ZwClose(ControlHandle
);
808 if (InstanceHandle
!= INVALID_HANDLE_VALUE
)
809 ZwClose(InstanceHandle
);
816 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode
,
817 PDEVICE_CAPABILITIES DeviceCaps
)
819 IO_STATUS_BLOCK StatusBlock
;
820 IO_STACK_LOCATION Stack
;
823 UNICODE_STRING ValueName
;
825 /* Set up the Header */
826 RtlZeroMemory(DeviceCaps
, sizeof(DEVICE_CAPABILITIES
));
827 DeviceCaps
->Size
= sizeof(DEVICE_CAPABILITIES
);
828 DeviceCaps
->Version
= 1;
829 DeviceCaps
->Address
= -1;
830 DeviceCaps
->UINumber
= -1;
832 /* Set up the Stack */
833 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
834 Stack
.Parameters
.DeviceCapabilities
.Capabilities
= DeviceCaps
;
837 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
839 IRP_MN_QUERY_CAPABILITIES
,
841 if (!NT_SUCCESS(Status
))
843 DPRINT1("IRP_MN_QUERY_CAPABILITIES failed with status 0x%x\n", Status
);
847 DeviceNode
->CapabilityFlags
= *(PULONG
)((ULONG_PTR
)&DeviceCaps
->Version
+ sizeof(DeviceCaps
->Version
));
849 if (DeviceCaps
->NoDisplayInUI
)
850 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
852 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
854 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
855 if (NT_SUCCESS(Status
))
857 /* Set 'Capabilities' value */
858 RtlInitUnicodeString(&ValueName
, L
"Capabilities");
859 Status
= ZwSetValueKey(InstanceKey
,
863 (PVOID
)&DeviceNode
->CapabilityFlags
,
866 /* Set 'UINumber' value */
867 if (DeviceCaps
->UINumber
!= MAXULONG
)
869 RtlInitUnicodeString(&ValueName
, L
"UINumber");
870 Status
= ZwSetValueKey(InstanceKey
,
874 &DeviceCaps
->UINumber
,
883 IopAsynchronousInvalidateDeviceRelations(
884 IN PDEVICE_OBJECT DeviceObject
,
885 IN PVOID InvalidateContext
)
887 PINVALIDATE_DEVICE_RELATION_DATA Data
= InvalidateContext
;
889 IoSynchronousInvalidateDeviceRelations(
893 ObDereferenceObject(Data
->DeviceObject
);
894 IoFreeWorkItem(Data
->WorkItem
);
899 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT
*DeviceObject
)
903 if (PopSystemPowerDeviceNode
)
905 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
906 *DeviceObject
= PopSystemPowerDeviceNode
->PhysicalDeviceObject
;
907 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
909 return STATUS_SUCCESS
;
912 return STATUS_UNSUCCESSFUL
;
917 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid
)
919 USHORT i
= 0, FoundIndex
= 0xFFFF;
923 /* Acquire the lock */
924 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
926 /* Loop all entries */
927 while (i
< PnpBusTypeGuidList
->GuidCount
)
929 /* Try to find a match */
930 if (RtlCompareMemory(BusTypeGuid
,
931 &PnpBusTypeGuidList
->Guids
[i
],
932 sizeof(GUID
)) == sizeof(GUID
))
941 /* Check if we have to grow the list */
942 if (PnpBusTypeGuidList
->GuidCount
)
944 /* Calculate the new size */
945 NewSize
= sizeof(IO_BUS_TYPE_GUID_LIST
) +
946 (sizeof(GUID
) * PnpBusTypeGuidList
->GuidCount
);
948 /* Allocate the new copy */
949 NewList
= ExAllocatePool(PagedPool
, NewSize
);
953 ExFreePool(PnpBusTypeGuidList
);
957 /* Now copy them, decrease the size too */
958 NewSize
-= sizeof(GUID
);
959 RtlCopyMemory(NewList
, PnpBusTypeGuidList
, NewSize
);
961 /* Free the old list */
962 ExFreePool(PnpBusTypeGuidList
);
964 /* Use the new buffer */
965 PnpBusTypeGuidList
= NewList
;
968 /* Copy the new GUID */
969 RtlCopyMemory(&PnpBusTypeGuidList
->Guids
[PnpBusTypeGuidList
->GuidCount
],
973 /* The new entry is the index */
974 FoundIndex
= (USHORT
)PnpBusTypeGuidList
->GuidCount
;
975 PnpBusTypeGuidList
->GuidCount
++;
978 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
984 * Creates a device node
987 * ParentNode = Pointer to parent device node
988 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
989 * to have the root device node create one
990 * (eg. for legacy drivers)
991 * DeviceNode = Pointer to storage for created device node
997 IopCreateDeviceNode(PDEVICE_NODE ParentNode
,
998 PDEVICE_OBJECT PhysicalDeviceObject
,
999 PUNICODE_STRING ServiceName
,
1000 PDEVICE_NODE
*DeviceNode
)
1005 UNICODE_STRING FullServiceName
;
1006 UNICODE_STRING LegacyPrefix
= RTL_CONSTANT_STRING(L
"LEGACY_");
1007 UNICODE_STRING UnknownDeviceName
= RTL_CONSTANT_STRING(L
"UNKNOWN");
1008 UNICODE_STRING KeyName
, ClassName
;
1009 PUNICODE_STRING ServiceName1
;
1012 UNICODE_STRING ClassGUID
;
1014 HANDLE InstanceHandle
;
1016 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
1017 ParentNode
, PhysicalDeviceObject
, ServiceName
);
1019 Node
= (PDEVICE_NODE
)ExAllocatePool(NonPagedPool
, sizeof(DEVICE_NODE
));
1022 return STATUS_INSUFFICIENT_RESOURCES
;
1025 RtlZeroMemory(Node
, sizeof(DEVICE_NODE
));
1028 ServiceName1
= &UnknownDeviceName
;
1030 ServiceName1
= ServiceName
;
1032 if (!PhysicalDeviceObject
)
1034 FullServiceName
.MaximumLength
= LegacyPrefix
.Length
+ ServiceName1
->Length
;
1035 FullServiceName
.Length
= 0;
1036 FullServiceName
.Buffer
= ExAllocatePool(PagedPool
, FullServiceName
.MaximumLength
);
1037 if (!FullServiceName
.Buffer
)
1040 return STATUS_INSUFFICIENT_RESOURCES
;
1043 RtlAppendUnicodeStringToString(&FullServiceName
, &LegacyPrefix
);
1044 RtlAppendUnicodeStringToString(&FullServiceName
, ServiceName1
);
1046 Status
= PnpRootCreateDevice(&FullServiceName
, NULL
, &PhysicalDeviceObject
, &Node
->InstancePath
);
1047 if (!NT_SUCCESS(Status
))
1049 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status
);
1054 /* Create the device key for legacy drivers */
1055 Status
= IopCreateDeviceKeyPath(&Node
->InstancePath
, REG_OPTION_VOLATILE
, &InstanceHandle
);
1056 if (!NT_SUCCESS(Status
))
1058 ZwClose(InstanceHandle
);
1060 ExFreePool(FullServiceName
.Buffer
);
1064 Node
->ServiceName
.Buffer
= ExAllocatePool(PagedPool
, ServiceName1
->Length
);
1065 if (!Node
->ServiceName
.Buffer
)
1067 ZwClose(InstanceHandle
);
1069 ExFreePool(FullServiceName
.Buffer
);
1073 Node
->ServiceName
.MaximumLength
= ServiceName1
->Length
;
1074 Node
->ServiceName
.Length
= 0;
1076 RtlAppendUnicodeStringToString(&Node
->ServiceName
, ServiceName1
);
1080 RtlInitUnicodeString(&KeyName
, L
"Service");
1081 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ServiceName
->Buffer
, ServiceName
->Length
);
1084 if (NT_SUCCESS(Status
))
1086 RtlInitUnicodeString(&KeyName
, L
"Legacy");
1089 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_DWORD
, &LegacyValue
, sizeof(LegacyValue
));
1090 if (NT_SUCCESS(Status
))
1092 RtlInitUnicodeString(&KeyName
, L
"Class");
1094 RtlInitUnicodeString(&ClassName
, L
"LegacyDriver");
1095 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassName
.Buffer
, ClassName
.Length
);
1097 if (NT_SUCCESS(Status
))
1099 RtlInitUnicodeString(&KeyName
, L
"ClassGUID");
1101 RtlInitUnicodeString(&ClassGUID
, L
"{8ECC055D-047F-11D1-A537-0000F8753ED1}");
1102 Status
= ZwSetValueKey(InstanceHandle
, &KeyName
, 0, REG_SZ
, ClassGUID
.Buffer
, ClassGUID
.Length
);
1108 ZwClose(InstanceHandle
);
1109 ExFreePool(FullServiceName
.Buffer
);
1111 if (!NT_SUCCESS(Status
))
1117 IopDeviceNodeSetFlag(Node
, DNF_LEGACY_DRIVER
);
1118 IopDeviceNodeSetFlag(Node
, DNF_PROCESSED
);
1119 IopDeviceNodeSetFlag(Node
, DNF_ADDED
);
1120 IopDeviceNodeSetFlag(Node
, DNF_STARTED
);
1123 Node
->PhysicalDeviceObject
= PhysicalDeviceObject
;
1125 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= Node
;
1129 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
1130 Node
->Parent
= ParentNode
;
1131 Node
->Sibling
= ParentNode
->Child
;
1132 ParentNode
->Child
= Node
;
1133 if (ParentNode
->LastChild
== NULL
)
1134 ParentNode
->LastChild
= Node
;
1135 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
1136 Node
->Level
= ParentNode
->Level
+ 1;
1139 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
1143 return STATUS_SUCCESS
;
1147 IopFreeDeviceNode(PDEVICE_NODE DeviceNode
)
1150 PDEVICE_NODE PrevSibling
= NULL
;
1152 /* All children must be deleted before a parent is deleted */
1153 ASSERT(!DeviceNode
->Child
);
1155 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
1157 ASSERT(DeviceNode
->PhysicalDeviceObject
);
1159 ObDereferenceObject(DeviceNode
->PhysicalDeviceObject
);
1161 /* Get previous sibling */
1162 if (DeviceNode
->Parent
&& DeviceNode
->Parent
->Child
!= DeviceNode
)
1164 PrevSibling
= DeviceNode
->Parent
->Child
;
1165 while (PrevSibling
->Sibling
!= DeviceNode
)
1166 PrevSibling
= PrevSibling
->Sibling
;
1169 /* Unlink from parent if it exists */
1170 if (DeviceNode
->Parent
)
1172 if (DeviceNode
->Parent
->LastChild
== DeviceNode
)
1174 DeviceNode
->Parent
->LastChild
= PrevSibling
;
1176 PrevSibling
->Sibling
= NULL
;
1178 if (DeviceNode
->Parent
->Child
== DeviceNode
)
1179 DeviceNode
->Parent
->Child
= DeviceNode
->Sibling
;
1182 /* Unlink from sibling list */
1184 PrevSibling
->Sibling
= DeviceNode
->Sibling
;
1186 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
1188 RtlFreeUnicodeString(&DeviceNode
->InstancePath
);
1190 RtlFreeUnicodeString(&DeviceNode
->ServiceName
);
1192 if (DeviceNode
->ResourceList
)
1194 ExFreePool(DeviceNode
->ResourceList
);
1197 if (DeviceNode
->ResourceListTranslated
)
1199 ExFreePool(DeviceNode
->ResourceListTranslated
);
1202 if (DeviceNode
->ResourceRequirements
)
1204 ExFreePool(DeviceNode
->ResourceRequirements
);
1207 if (DeviceNode
->BootResources
)
1209 ExFreePool(DeviceNode
->BootResources
);
1212 ExFreePool(DeviceNode
);
1214 return STATUS_SUCCESS
;
1219 IopSynchronousCall(IN PDEVICE_OBJECT DeviceObject
,
1220 IN PIO_STACK_LOCATION IoStackLocation
,
1221 OUT PVOID
*Information
)
1224 PIO_STACK_LOCATION IrpStack
;
1225 IO_STATUS_BLOCK IoStatusBlock
;
1228 PDEVICE_OBJECT TopDeviceObject
;
1231 /* Call the top of the device stack */
1232 TopDeviceObject
= IoGetAttachedDeviceReference(DeviceObject
);
1234 /* Allocate an IRP */
1235 Irp
= IoAllocateIrp(TopDeviceObject
->StackSize
, FALSE
);
1236 if (!Irp
) return STATUS_INSUFFICIENT_RESOURCES
;
1238 /* Initialize to failure */
1239 Irp
->IoStatus
.Status
= IoStatusBlock
.Status
= STATUS_NOT_SUPPORTED
;
1240 Irp
->IoStatus
.Information
= IoStatusBlock
.Information
= 0;
1242 /* Special case for IRP_MN_FILTER_RESOURCE_REQUIREMENTS */
1243 if (IoStackLocation
->MinorFunction
== IRP_MN_FILTER_RESOURCE_REQUIREMENTS
)
1245 /* Copy the resource requirements list into the IOSB */
1246 Irp
->IoStatus
.Information
=
1247 IoStatusBlock
.Information
= (ULONG_PTR
)IoStackLocation
->Parameters
.FilterResourceRequirements
.IoResourceRequirementList
;
1250 /* Initialize the event */
1251 KeInitializeEvent(&Event
, SynchronizationEvent
, FALSE
);
1254 Irp
->UserIosb
= &IoStatusBlock
;
1255 Irp
->UserEvent
= &Event
;
1258 Irp
->Tail
.Overlay
.Thread
= PsGetCurrentThread();
1259 IoQueueThreadIrp(Irp
);
1261 /* Copy-in the stack */
1262 IrpStack
= IoGetNextIrpStackLocation(Irp
);
1263 *IrpStack
= *IoStackLocation
;
1265 /* Call the driver */
1266 Status
= IoCallDriver(TopDeviceObject
, Irp
);
1267 if (Status
== STATUS_PENDING
)
1270 KeWaitForSingleObject(&Event
,
1275 Status
= IoStatusBlock
.Status
;
1278 /* Return the information */
1279 *Information
= (PVOID
)IoStatusBlock
.Information
;
1285 IopInitiatePnpIrp(IN PDEVICE_OBJECT DeviceObject
,
1286 IN OUT PIO_STATUS_BLOCK IoStatusBlock
,
1287 IN UCHAR MinorFunction
,
1288 IN PIO_STACK_LOCATION Stack OPTIONAL
)
1290 IO_STACK_LOCATION IoStackLocation
;
1292 /* Fill out the stack information */
1293 RtlZeroMemory(&IoStackLocation
, sizeof(IO_STACK_LOCATION
));
1294 IoStackLocation
.MajorFunction
= IRP_MJ_PNP
;
1295 IoStackLocation
.MinorFunction
= MinorFunction
;
1299 RtlCopyMemory(&IoStackLocation
.Parameters
,
1301 sizeof(Stack
->Parameters
));
1304 /* Do the PnP call */
1305 IoStatusBlock
->Status
= IopSynchronousCall(DeviceObject
,
1307 (PVOID
)&IoStatusBlock
->Information
);
1308 return IoStatusBlock
->Status
;
1312 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context
)
1314 PDEVICE_NODE ParentDeviceNode
;
1315 PDEVICE_NODE ChildDeviceNode
;
1318 /* Copy context data so we don't overwrite it in subsequent calls to this function */
1319 ParentDeviceNode
= Context
->DeviceNode
;
1321 /* Call the action routine */
1322 Status
= (Context
->Action
)(ParentDeviceNode
, Context
->Context
);
1323 if (!NT_SUCCESS(Status
))
1328 /* Traversal of all children nodes */
1329 for (ChildDeviceNode
= ParentDeviceNode
->Child
;
1330 ChildDeviceNode
!= NULL
;
1331 ChildDeviceNode
= ChildDeviceNode
->Sibling
)
1333 /* Pass the current device node to the action routine */
1334 Context
->DeviceNode
= ChildDeviceNode
;
1336 Status
= IopTraverseDeviceTreeNode(Context
);
1337 if (!NT_SUCCESS(Status
))
1348 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context
)
1352 DPRINT("Context 0x%p\n", Context
);
1354 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
1355 Context
->DeviceNode
, Context
->FirstDeviceNode
, Context
->Action
, Context
->Context
);
1357 /* Start from the specified device node */
1358 Context
->DeviceNode
= Context
->FirstDeviceNode
;
1360 /* Recursively traverse the device tree */
1361 Status
= IopTraverseDeviceTreeNode(Context
);
1362 if (Status
== STATUS_UNSUCCESSFUL
)
1364 /* The action routine just wanted to terminate the traversal with status
1365 code STATUS_SUCCESS */
1366 Status
= STATUS_SUCCESS
;
1374 * IopCreateDeviceKeyPath
1376 * Creates a registry key
1380 * Name of the key to be created.
1382 * Handle to the newly created key
1385 * This method can create nested trees, so parent of RegistryPath can
1386 * be not existant, and will be created if needed.
1390 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath
,
1391 IN ULONG CreateOptions
,
1394 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(ENUM_ROOT
);
1395 HANDLE hParent
= NULL
, hKey
;
1396 OBJECT_ATTRIBUTES ObjectAttributes
;
1397 UNICODE_STRING KeyName
;
1398 LPCWSTR Current
, Last
;
1402 /* Assume failure */
1405 /* Create a volatile device tree in 1st stage so we have a clean slate
1406 * for enumeration using the correct HAL (chosen in 1st stage setup) */
1407 if (ExpInTextModeSetup
) CreateOptions
|= REG_OPTION_VOLATILE
;
1409 /* Open root key for device instances */
1410 Status
= IopOpenRegistryKeyEx(&hParent
, NULL
, &EnumU
, KEY_CREATE_SUB_KEY
);
1411 if (!NT_SUCCESS(Status
))
1413 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU
, Status
);
1417 Current
= KeyName
.Buffer
= RegistryPath
->Buffer
;
1418 Last
= &RegistryPath
->Buffer
[RegistryPath
->Length
/ sizeof(WCHAR
)];
1420 /* Go up to the end of the string */
1421 while (Current
<= Last
)
1423 if (Current
!= Last
&& *Current
!= '\\')
1425 /* Not the end of the string and not a separator */
1430 /* Prepare relative key name */
1431 Length
= (USHORT
)((ULONG_PTR
)Current
- (ULONG_PTR
)KeyName
.Buffer
);
1432 KeyName
.MaximumLength
= KeyName
.Length
= Length
;
1433 DPRINT("Create '%wZ'\n", &KeyName
);
1436 InitializeObjectAttributes(&ObjectAttributes
,
1438 OBJ_CASE_INSENSITIVE
,
1441 Status
= ZwCreateKey(&hKey
,
1442 Current
== Last
? KEY_ALL_ACCESS
: KEY_CREATE_SUB_KEY
,
1449 /* Close parent key handle, we don't need it anymore */
1453 /* Key opening/creating failed? */
1454 if (!NT_SUCCESS(Status
))
1456 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName
, Status
);
1460 /* Check if it is the end of the string */
1461 if (Current
== Last
)
1463 /* Yes, return success */
1465 return STATUS_SUCCESS
;
1468 /* Start with this new parent key */
1471 KeyName
.Buffer
= (LPWSTR
)Current
;
1474 return STATUS_UNSUCCESSFUL
;
1478 IopSetDeviceInstanceData(HANDLE InstanceKey
,
1479 PDEVICE_NODE DeviceNode
)
1481 OBJECT_ATTRIBUTES ObjectAttributes
;
1482 UNICODE_STRING KeyName
;
1487 HANDLE ControlHandle
;
1489 DPRINT("IopSetDeviceInstanceData() called\n");
1491 /* Create the 'LogConf' key */
1492 RtlInitUnicodeString(&KeyName
, L
"LogConf");
1493 InitializeObjectAttributes(&ObjectAttributes
,
1495 OBJ_CASE_INSENSITIVE
,
1498 Status
= ZwCreateKey(&LogConfKey
,
1503 REG_OPTION_VOLATILE
,
1505 if (NT_SUCCESS(Status
))
1507 /* Set 'BootConfig' value */
1508 if (DeviceNode
->BootResources
!= NULL
)
1510 ResCount
= DeviceNode
->BootResources
->Count
;
1513 RtlInitUnicodeString(&KeyName
, L
"BootConfig");
1514 Status
= ZwSetValueKey(LogConfKey
,
1518 DeviceNode
->BootResources
,
1519 PnpDetermineResourceListSize(DeviceNode
->BootResources
));
1523 /* Set 'BasicConfigVector' value */
1524 if (DeviceNode
->ResourceRequirements
!= NULL
&&
1525 DeviceNode
->ResourceRequirements
->ListSize
!= 0)
1527 RtlInitUnicodeString(&KeyName
, L
"BasicConfigVector");
1528 Status
= ZwSetValueKey(LogConfKey
,
1531 REG_RESOURCE_REQUIREMENTS_LIST
,
1532 DeviceNode
->ResourceRequirements
,
1533 DeviceNode
->ResourceRequirements
->ListSize
);
1536 ZwClose(LogConfKey
);
1539 /* Set the 'ConfigFlags' value */
1540 RtlInitUnicodeString(&KeyName
, L
"ConfigFlags");
1541 Status
= ZwQueryValueKey(InstanceKey
,
1543 KeyValueBasicInformation
,
1547 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
1549 /* Write the default value */
1550 ULONG DefaultConfigFlags
= 0;
1551 Status
= ZwSetValueKey(InstanceKey
,
1555 &DefaultConfigFlags
,
1556 sizeof(DefaultConfigFlags
));
1559 /* Create the 'Control' key */
1560 RtlInitUnicodeString(&KeyName
, L
"Control");
1561 InitializeObjectAttributes(&ObjectAttributes
,
1563 OBJ_CASE_INSENSITIVE
,
1566 Status
= ZwCreateKey(&ControlHandle
, 0, &ObjectAttributes
, 0, NULL
, REG_OPTION_VOLATILE
, NULL
);
1568 if (NT_SUCCESS(Status
))
1569 ZwClose(ControlHandle
);
1571 DPRINT("IopSetDeviceInstanceData() done\n");
1577 * IopGetParentIdPrefix
1579 * Retrieve (or create) a string which identifies a device.
1583 * Pointer to device node.
1585 * Pointer to the string where is returned the parent node identifier
1588 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1589 * valid and its Buffer field is NULL-terminated. The caller needs to
1590 * to free the string with RtlFreeUnicodeString when it is no longer
1595 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode
,
1596 PUNICODE_STRING ParentIdPrefix
)
1598 ULONG KeyNameBufferLength
;
1599 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation
= NULL
;
1600 UNICODE_STRING KeyName
= {0, 0, NULL
};
1601 UNICODE_STRING KeyValue
;
1602 UNICODE_STRING ValueName
;
1607 /* HACK: As long as some devices have a NULL device
1608 * instance path, the following test is required :(
1610 if (DeviceNode
->Parent
->InstancePath
.Length
== 0)
1612 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1613 &DeviceNode
->InstancePath
);
1614 return STATUS_UNSUCCESSFUL
;
1617 /* 1. Try to retrieve ParentIdPrefix from registry */
1618 KeyNameBufferLength
= FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION
, Data
[0]) + MAX_PATH
* sizeof(WCHAR
);
1619 ParentIdPrefixInformation
= ExAllocatePool(PagedPool
, KeyNameBufferLength
+ sizeof(WCHAR
));
1620 if (!ParentIdPrefixInformation
)
1622 return STATUS_INSUFFICIENT_RESOURCES
;
1625 KeyName
.Buffer
= ExAllocatePool(PagedPool
, (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
);
1626 if (!KeyName
.Buffer
)
1628 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1632 KeyName
.MaximumLength
= (49 * sizeof(WCHAR
)) + DeviceNode
->Parent
->InstancePath
.Length
;
1634 RtlAppendUnicodeToString(&KeyName
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1635 RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->Parent
->InstancePath
);
1637 Status
= IopOpenRegistryKeyEx(&hKey
, NULL
, &KeyName
, KEY_QUERY_VALUE
| KEY_SET_VALUE
);
1638 if (!NT_SUCCESS(Status
))
1640 RtlInitUnicodeString(&ValueName
, L
"ParentIdPrefix");
1641 Status
= ZwQueryValueKey(
1643 KeyValuePartialInformation
, ParentIdPrefixInformation
,
1644 KeyNameBufferLength
, &KeyNameBufferLength
);
1645 if (NT_SUCCESS(Status
))
1647 if (ParentIdPrefixInformation
->Type
!= REG_SZ
)
1648 Status
= STATUS_UNSUCCESSFUL
;
1651 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1652 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1656 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
1658 KeyValue
.Length
= KeyValue
.MaximumLength
= (USHORT
)ParentIdPrefixInformation
->DataLength
;
1659 KeyValue
.Buffer
= (PWSTR
)ParentIdPrefixInformation
->Data
;
1663 /* 2. Create the ParentIdPrefix value */
1664 crc32
= RtlComputeCrc32(0,
1665 (PUCHAR
)DeviceNode
->Parent
->InstancePath
.Buffer
,
1666 DeviceNode
->Parent
->InstancePath
.Length
);
1668 swprintf((PWSTR
)ParentIdPrefixInformation
->Data
, L
"%lx&%lx", DeviceNode
->Parent
->Level
, crc32
);
1669 RtlInitUnicodeString(&KeyValue
, (PWSTR
)ParentIdPrefixInformation
->Data
);
1671 /* 3. Try to write the ParentIdPrefix to registry */
1672 Status
= ZwSetValueKey(hKey
,
1676 (PVOID
)KeyValue
.Buffer
,
1677 ((ULONG
)wcslen(KeyValue
.Buffer
) + 1) * sizeof(WCHAR
));
1680 if (NT_SUCCESS(Status
))
1682 /* Duplicate the string to return it */
1683 Status
= RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE
, &KeyValue
, ParentIdPrefix
);
1685 ExFreePool(ParentIdPrefixInformation
);
1686 RtlFreeUnicodeString(&KeyName
);
1693 IopQueryHardwareIds(PDEVICE_NODE DeviceNode
,
1696 IO_STACK_LOCATION Stack
;
1697 IO_STATUS_BLOCK IoStatusBlock
;
1699 UNICODE_STRING ValueName
;
1701 ULONG Length
, TotalLength
;
1703 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1705 RtlZeroMemory(&Stack
, sizeof(Stack
));
1706 Stack
.Parameters
.QueryId
.IdType
= BusQueryHardwareIDs
;
1707 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1711 if (NT_SUCCESS(Status
))
1714 * FIXME: Check for valid characters, if there is invalid characters
1718 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1719 DPRINT("Hardware IDs:\n");
1722 DPRINT(" %S\n", Ptr
);
1723 Length
= (ULONG
)wcslen(Ptr
) + 1;
1726 TotalLength
+= Length
;
1728 DPRINT("TotalLength: %hu\n", TotalLength
);
1731 RtlInitUnicodeString(&ValueName
, L
"HardwareID");
1732 Status
= ZwSetValueKey(InstanceKey
,
1736 (PVOID
)IoStatusBlock
.Information
,
1737 (TotalLength
+ 1) * sizeof(WCHAR
));
1738 if (!NT_SUCCESS(Status
))
1740 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
1745 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1752 IopQueryCompatibleIds(PDEVICE_NODE DeviceNode
,
1755 IO_STACK_LOCATION Stack
;
1756 IO_STATUS_BLOCK IoStatusBlock
;
1758 UNICODE_STRING ValueName
;
1760 ULONG Length
, TotalLength
;
1762 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1764 RtlZeroMemory(&Stack
, sizeof(Stack
));
1765 Stack
.Parameters
.QueryId
.IdType
= BusQueryCompatibleIDs
;
1766 Status
= IopInitiatePnpIrp(
1767 DeviceNode
->PhysicalDeviceObject
,
1771 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
1774 * FIXME: Check for valid characters, if there is invalid characters
1778 Ptr
= (PWSTR
)IoStatusBlock
.Information
;
1779 DPRINT("Compatible IDs:\n");
1782 DPRINT(" %S\n", Ptr
);
1783 Length
= (ULONG
)wcslen(Ptr
) + 1;
1786 TotalLength
+= Length
;
1788 DPRINT("TotalLength: %hu\n", TotalLength
);
1791 RtlInitUnicodeString(&ValueName
, L
"CompatibleIDs");
1792 Status
= ZwSetValueKey(InstanceKey
,
1796 (PVOID
)IoStatusBlock
.Information
,
1797 (TotalLength
+ 1) * sizeof(WCHAR
));
1798 if (!NT_SUCCESS(Status
))
1800 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status
);
1805 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1813 * IopActionInterrogateDeviceStack
1815 * Retrieve information for all (direct) child nodes of a parent node.
1819 * Pointer to device node.
1821 * Pointer to parent node to retrieve child node information for.
1824 * Any errors that occur are logged instead so that all child services have a chance
1825 * of being interrogated.
1829 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode
,
1832 IO_STATUS_BLOCK IoStatusBlock
;
1833 PDEVICE_NODE ParentDeviceNode
;
1834 WCHAR InstancePath
[MAX_PATH
];
1835 IO_STACK_LOCATION Stack
;
1837 ULONG RequiredLength
;
1839 HANDLE InstanceKey
= NULL
;
1840 UNICODE_STRING ValueName
;
1841 UNICODE_STRING ParentIdPrefix
= { 0, 0, NULL
};
1842 DEVICE_CAPABILITIES DeviceCapabilities
;
1844 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode
, Context
);
1845 DPRINT("PDO 0x%p\n", DeviceNode
->PhysicalDeviceObject
);
1847 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
1850 * We are called for the parent too, but we don't need to do special
1851 * handling for this node
1854 if (DeviceNode
== ParentDeviceNode
)
1856 DPRINT("Success\n");
1857 return STATUS_SUCCESS
;
1861 * Make sure this device node is a direct child of the parent device node
1862 * that is given as an argument
1865 if (DeviceNode
->Parent
!= ParentDeviceNode
)
1867 DPRINT("Skipping 2+ level child\n");
1868 return STATUS_SUCCESS
;
1871 /* Skip processing if it was already completed before */
1872 if (DeviceNode
->Flags
& DNF_PROCESSED
)
1875 return STATUS_SUCCESS
;
1879 Status
= ZwQueryDefaultLocale(FALSE
, &LocaleId
);
1880 if (!NT_SUCCESS(Status
))
1882 DPRINT1("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status
);
1887 * FIXME: For critical errors, cleanup and disable device, but always
1888 * return STATUS_SUCCESS.
1891 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1893 Stack
.Parameters
.QueryId
.IdType
= BusQueryDeviceID
;
1894 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1898 if (NT_SUCCESS(Status
))
1900 /* Copy the device id string */
1901 wcscpy(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1904 * FIXME: Check for valid characters, if there is invalid characters
1910 DPRINT1("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1912 /* We have to return success otherwise we abort the traverse operation */
1913 return STATUS_SUCCESS
;
1916 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
1918 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCapabilities
);
1919 if (!NT_SUCCESS(Status
))
1921 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status
);
1923 /* We have to return success otherwise we abort the traverse operation */
1924 return STATUS_SUCCESS
;
1927 /* This bit is only check after enumeration */
1928 if (DeviceCapabilities
.HardwareDisabled
)
1930 /* FIXME: Cleanup device */
1931 DeviceNode
->Flags
|= DNF_DISABLED
;
1932 return STATUS_SUCCESS
;
1935 DeviceNode
->Flags
&= ~DNF_DISABLED
;
1937 if (!DeviceCapabilities
.UniqueID
)
1939 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1940 DPRINT("Instance ID is not unique\n");
1941 Status
= IopGetParentIdPrefix(DeviceNode
, &ParentIdPrefix
);
1942 if (!NT_SUCCESS(Status
))
1944 DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status
);
1946 /* We have to return success otherwise we abort the traverse operation */
1947 return STATUS_SUCCESS
;
1951 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1953 Stack
.Parameters
.QueryId
.IdType
= BusQueryInstanceID
;
1954 Status
= IopInitiatePnpIrp(DeviceNode
->PhysicalDeviceObject
,
1958 if (NT_SUCCESS(Status
))
1960 /* Append the instance id string */
1961 wcscat(InstancePath
, L
"\\");
1962 if (ParentIdPrefix
.Length
> 0)
1964 /* Add information from parent bus device to InstancePath */
1965 wcscat(InstancePath
, ParentIdPrefix
.Buffer
);
1966 if (IoStatusBlock
.Information
&& *(PWSTR
)IoStatusBlock
.Information
)
1967 wcscat(InstancePath
, L
"&");
1969 if (IoStatusBlock
.Information
)
1970 wcscat(InstancePath
, (PWSTR
)IoStatusBlock
.Information
);
1973 * FIXME: Check for valid characters, if there is invalid characters
1979 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status
);
1981 RtlFreeUnicodeString(&ParentIdPrefix
);
1983 if (!RtlCreateUnicodeString(&DeviceNode
->InstancePath
, InstancePath
))
1985 DPRINT("No resources\n");
1986 /* FIXME: Cleanup and disable device */
1989 DPRINT("InstancePath is %S\n", DeviceNode
->InstancePath
.Buffer
);
1992 * Create registry key for the instance id, if it doesn't exist yet
1994 Status
= IopCreateDeviceKeyPath(&DeviceNode
->InstancePath
, 0, &InstanceKey
);
1995 if (!NT_SUCCESS(Status
))
1997 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status
);
1999 /* We have to return success otherwise we abort the traverse operation */
2000 return STATUS_SUCCESS
;
2003 IopQueryHardwareIds(DeviceNode
, InstanceKey
);
2005 IopQueryCompatibleIds(DeviceNode
, InstanceKey
);
2007 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
2009 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextDescription
;
2010 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2011 Status
= IopInitiatePnpIrp(
2012 DeviceNode
->PhysicalDeviceObject
,
2014 IRP_MN_QUERY_DEVICE_TEXT
,
2016 /* This key is mandatory, so even if the Irp fails, we still write it */
2017 RtlInitUnicodeString(&ValueName
, L
"DeviceDesc");
2018 if (ZwQueryValueKey(InstanceKey
, &ValueName
, KeyValueBasicInformation
, NULL
, 0, &RequiredLength
) == STATUS_OBJECT_NAME_NOT_FOUND
)
2020 if (NT_SUCCESS(Status
) &&
2021 IoStatusBlock
.Information
&&
2022 (*(PWSTR
)IoStatusBlock
.Information
!= 0))
2024 /* This key is overriden when a driver is installed. Don't write the
2025 * new description if another one already exists */
2026 Status
= ZwSetValueKey(InstanceKey
,
2030 (PVOID
)IoStatusBlock
.Information
,
2031 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
2035 UNICODE_STRING DeviceDesc
= RTL_CONSTANT_STRING(L
"Unknown device");
2036 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status
);
2038 Status
= ZwSetValueKey(InstanceKey
,
2043 DeviceDesc
.MaximumLength
);
2045 if (!NT_SUCCESS(Status
))
2047 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status
);
2053 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
2055 Stack
.Parameters
.QueryDeviceText
.DeviceTextType
= DeviceTextLocationInformation
;
2056 Stack
.Parameters
.QueryDeviceText
.LocaleId
= LocaleId
;
2057 Status
= IopInitiatePnpIrp(
2058 DeviceNode
->PhysicalDeviceObject
,
2060 IRP_MN_QUERY_DEVICE_TEXT
,
2062 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2064 DPRINT("LocationInformation: %S\n", (PWSTR
)IoStatusBlock
.Information
);
2065 RtlInitUnicodeString(&ValueName
, L
"LocationInformation");
2066 Status
= ZwSetValueKey(InstanceKey
,
2070 (PVOID
)IoStatusBlock
.Information
,
2071 ((ULONG
)wcslen((PWSTR
)IoStatusBlock
.Information
) + 1) * sizeof(WCHAR
));
2072 if (!NT_SUCCESS(Status
))
2074 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status
);
2079 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2082 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
2084 Status
= IopInitiatePnpIrp(
2085 DeviceNode
->PhysicalDeviceObject
,
2087 IRP_MN_QUERY_BUS_INFORMATION
,
2089 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2091 PPNP_BUS_INFORMATION BusInformation
=
2092 (PPNP_BUS_INFORMATION
)IoStatusBlock
.Information
;
2094 DeviceNode
->ChildBusNumber
= BusInformation
->BusNumber
;
2095 DeviceNode
->ChildInterfaceType
= BusInformation
->LegacyBusType
;
2096 DeviceNode
->ChildBusTypeIndex
= IopGetBusTypeGuidIndex(&BusInformation
->BusTypeGuid
);
2097 ExFreePool(BusInformation
);
2101 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2103 DeviceNode
->ChildBusNumber
= 0xFFFFFFF0;
2104 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
2105 DeviceNode
->ChildBusTypeIndex
= -1;
2108 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
2110 Status
= IopInitiatePnpIrp(
2111 DeviceNode
->PhysicalDeviceObject
,
2113 IRP_MN_QUERY_RESOURCES
,
2115 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
2117 DeviceNode
->BootResources
=
2118 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
2119 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
2123 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
2124 DeviceNode
->BootResources
= NULL
;
2127 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
2129 Status
= IopInitiatePnpIrp(
2130 DeviceNode
->PhysicalDeviceObject
,
2132 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
2134 if (NT_SUCCESS(Status
))
2136 DeviceNode
->ResourceRequirements
=
2137 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
2141 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
2142 DeviceNode
->ResourceRequirements
= NULL
;
2145 if (InstanceKey
!= NULL
)
2147 IopSetDeviceInstanceData(InstanceKey
, DeviceNode
);
2150 ZwClose(InstanceKey
);
2152 IopDeviceNodeSetFlag(DeviceNode
, DNF_PROCESSED
);
2154 if (!IopDeviceNodeHasFlag(DeviceNode
, DNF_LEGACY_DRIVER
))
2156 /* Report the device to the user-mode pnp manager */
2157 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED
,
2158 &DeviceNode
->InstancePath
);
2161 return STATUS_SUCCESS
;
2166 IopHandleDeviceRemoval(
2167 IN PDEVICE_NODE DeviceNode
,
2168 IN PDEVICE_RELATIONS DeviceRelations
)
2170 PDEVICE_NODE Child
= DeviceNode
->Child
, NextChild
;
2174 if (DeviceNode
== IopRootDeviceNode
)
2177 while (Child
!= NULL
)
2179 NextChild
= Child
->Sibling
;
2182 for (i
= 0; DeviceRelations
&& i
< DeviceRelations
->Count
; i
++)
2184 if (IopGetDeviceNode(DeviceRelations
->Objects
[i
]) == Child
)
2191 if (!Found
&& !(Child
->Flags
& DNF_WILL_BE_REMOVED
))
2193 /* Send removal IRPs to all of its children */
2194 IopPrepareDeviceForRemoval(Child
->PhysicalDeviceObject
, TRUE
);
2196 /* Send the surprise removal IRP */
2197 IopSendSurpriseRemoval(Child
->PhysicalDeviceObject
);
2199 /* Tell the user-mode PnP manager that a device was removed */
2200 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
2201 &Child
->InstancePath
);
2203 /* Send the remove device IRP */
2204 IopSendRemoveDevice(Child
->PhysicalDeviceObject
);
2213 IN PDEVICE_OBJECT DeviceObject
)
2215 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
2216 DEVICETREE_TRAVERSE_CONTEXT Context
;
2217 PDEVICE_RELATIONS DeviceRelations
;
2218 PDEVICE_OBJECT ChildDeviceObject
;
2219 IO_STATUS_BLOCK IoStatusBlock
;
2220 PDEVICE_NODE ChildDeviceNode
;
2221 IO_STACK_LOCATION Stack
;
2225 DPRINT("DeviceObject 0x%p\n", DeviceObject
);
2227 if (DeviceNode
->Flags
& DNF_NEED_ENUMERATION_ONLY
)
2229 DeviceNode
->Flags
&= ~DNF_NEED_ENUMERATION_ONLY
;
2231 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
2232 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL
,
2233 &DeviceNode
->InstancePath
);
2236 DeviceNode
->Flags
&= ~DNF_NEED_TO_ENUM
;
2238 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2240 Stack
.Parameters
.QueryDeviceRelations
.Type
= BusRelations
;
2242 Status
= IopInitiatePnpIrp(
2245 IRP_MN_QUERY_DEVICE_RELATIONS
,
2247 if (!NT_SUCCESS(Status
) || Status
== STATUS_PENDING
)
2249 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
2253 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
2256 * Send removal IRPs for devices that have disappeared
2257 * NOTE: This code handles the case where no relations are specified
2259 IopHandleDeviceRemoval(DeviceNode
, DeviceRelations
);
2261 /* Now we bail if nothing was returned */
2262 if (!DeviceRelations
)
2264 /* We're all done */
2265 DPRINT("No PDOs\n");
2266 return STATUS_SUCCESS
;
2269 DPRINT("Got %u PDOs\n", DeviceRelations
->Count
);
2272 * Create device nodes for all discovered devices
2274 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
2276 ChildDeviceObject
= DeviceRelations
->Objects
[i
];
2277 ASSERT((ChildDeviceObject
->Flags
& DO_DEVICE_INITIALIZING
) == 0);
2279 ChildDeviceNode
= IopGetDeviceNode(ChildDeviceObject
);
2280 if (!ChildDeviceNode
)
2282 /* One doesn't exist, create it */
2283 Status
= IopCreateDeviceNode(
2288 if (NT_SUCCESS(Status
))
2290 /* Mark the node as enumerated */
2291 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2293 /* Mark the DO as bus enumerated */
2294 ChildDeviceObject
->Flags
|= DO_BUS_ENUMERATED_DEVICE
;
2298 /* Ignore this DO */
2299 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status
, i
);
2300 ObDereferenceObject(ChildDeviceObject
);
2305 /* Mark it as enumerated */
2306 ChildDeviceNode
->Flags
|= DNF_ENUMERATED
;
2307 ObDereferenceObject(ChildDeviceObject
);
2310 ExFreePool(DeviceRelations
);
2313 * Retrieve information about all discovered children from the bus driver
2315 IopInitDeviceTreeTraverseContext(
2318 IopActionInterrogateDeviceStack
,
2321 Status
= IopTraverseDeviceTree(&Context
);
2322 if (!NT_SUCCESS(Status
))
2324 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2329 * Retrieve configuration from the registry for discovered children
2331 IopInitDeviceTreeTraverseContext(
2334 IopActionConfigureChildServices
,
2337 Status
= IopTraverseDeviceTree(&Context
);
2338 if (!NT_SUCCESS(Status
))
2340 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status
);
2345 * Initialize services for discovered children.
2347 Status
= IopInitializePnpServices(DeviceNode
);
2348 if (!NT_SUCCESS(Status
))
2350 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status
);
2354 DPRINT("IopEnumerateDevice() finished\n");
2355 return STATUS_SUCCESS
;
2360 * IopActionConfigureChildServices
2362 * Retrieve configuration for all (direct) child nodes of a parent node.
2366 * Pointer to device node.
2368 * Pointer to parent node to retrieve child node configuration for.
2371 * Any errors that occur are logged instead so that all child services have a chance of beeing
2376 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode
,
2379 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
2380 PDEVICE_NODE ParentDeviceNode
;
2381 PUNICODE_STRING Service
;
2382 UNICODE_STRING ClassGUID
;
2384 DEVICE_CAPABILITIES DeviceCaps
;
2386 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode
, Context
);
2388 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2391 * We are called for the parent too, but we don't need to do special
2392 * handling for this node
2394 if (DeviceNode
== ParentDeviceNode
)
2396 DPRINT("Success\n");
2397 return STATUS_SUCCESS
;
2401 * Make sure this device node is a direct child of the parent device node
2402 * that is given as an argument
2405 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2407 DPRINT("Skipping 2+ level child\n");
2408 return STATUS_SUCCESS
;
2411 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2413 DPRINT1("Child not ready to be configured\n");
2414 return STATUS_SUCCESS
;
2417 if (!(DeviceNode
->Flags
& (DNF_DISABLED
| DNF_STARTED
| DNF_ADDED
)))
2419 WCHAR RegKeyBuffer
[MAX_PATH
];
2420 UNICODE_STRING RegKey
;
2422 /* Install the service for this if it's in the CDDB */
2423 IopInstallCriticalDevice(DeviceNode
);
2426 RegKey
.MaximumLength
= sizeof(RegKeyBuffer
);
2427 RegKey
.Buffer
= RegKeyBuffer
;
2430 * Retrieve configuration from Enum key
2433 Service
= &DeviceNode
->ServiceName
;
2435 RtlZeroMemory(QueryTable
, sizeof(QueryTable
));
2436 RtlInitUnicodeString(Service
, NULL
);
2437 RtlInitUnicodeString(&ClassGUID
, NULL
);
2439 QueryTable
[0].Name
= L
"Service";
2440 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2441 QueryTable
[0].EntryContext
= Service
;
2443 QueryTable
[1].Name
= L
"ClassGUID";
2444 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
;
2445 QueryTable
[1].EntryContext
= &ClassGUID
;
2446 QueryTable
[1].DefaultType
= REG_SZ
;
2447 QueryTable
[1].DefaultData
= L
"";
2448 QueryTable
[1].DefaultLength
= 0;
2450 RtlAppendUnicodeToString(&RegKey
, L
"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2451 RtlAppendUnicodeStringToString(&RegKey
, &DeviceNode
->InstancePath
);
2453 Status
= RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE
,
2454 RegKey
.Buffer
, QueryTable
, NULL
, NULL
);
2456 if (!NT_SUCCESS(Status
))
2458 /* FIXME: Log the error */
2459 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2460 &DeviceNode
->InstancePath
, Status
);
2461 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2462 return STATUS_SUCCESS
;
2465 if (Service
->Buffer
== NULL
)
2467 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
)) &&
2468 DeviceCaps
.RawDeviceOK
)
2470 DPRINT1("%wZ is using parent bus driver (%wZ)\n", &DeviceNode
->InstancePath
, &ParentDeviceNode
->ServiceName
);
2472 DeviceNode
->ServiceName
.Length
= 0;
2473 DeviceNode
->ServiceName
.MaximumLength
= 0;
2474 DeviceNode
->ServiceName
.Buffer
= NULL
;
2476 else if (ClassGUID
.Length
!= 0)
2478 /* Device has a ClassGUID value, but no Service value.
2479 * Suppose it is using the NULL driver, so state the
2480 * device is started */
2481 DPRINT("%wZ is using NULL driver\n", &DeviceNode
->InstancePath
);
2482 IopDeviceNodeSetFlag(DeviceNode
, DNF_STARTED
);
2486 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2488 return STATUS_SUCCESS
;
2491 DPRINT("Got Service %S\n", Service
->Buffer
);
2494 return STATUS_SUCCESS
;
2498 * IopActionInitChildServices
2500 * Initialize the service for all (direct) child nodes of a parent node
2504 * Pointer to device node.
2506 * Pointer to parent node to initialize child node services for.
2509 * If the driver image for a service is not loaded and initialized
2510 * it is done here too. Any errors that occur are logged instead so
2511 * that all child services have a chance of being initialized.
2515 IopActionInitChildServices(PDEVICE_NODE DeviceNode
,
2518 PDEVICE_NODE ParentDeviceNode
;
2520 BOOLEAN BootDrivers
= !PnpSystemInit
;
2522 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode
, Context
);
2524 ParentDeviceNode
= (PDEVICE_NODE
)Context
;
2527 * We are called for the parent too, but we don't need to do special
2528 * handling for this node
2530 if (DeviceNode
== ParentDeviceNode
)
2532 DPRINT("Success\n");
2533 return STATUS_SUCCESS
;
2537 * Make sure this device node is a direct child of the parent device node
2538 * that is given as an argument
2541 if (DeviceNode
->Parent
!= ParentDeviceNode
)
2543 DPRINT("Skipping 2+ level child\n");
2544 return STATUS_SUCCESS
;
2547 if (!(DeviceNode
->Flags
& DNF_PROCESSED
))
2549 DPRINT1("Child not ready to be added\n");
2550 return STATUS_SUCCESS
;
2553 if (IopDeviceNodeHasFlag(DeviceNode
, DNF_STARTED
) ||
2554 IopDeviceNodeHasFlag(DeviceNode
, DNF_ADDED
) ||
2555 IopDeviceNodeHasFlag(DeviceNode
, DNF_DISABLED
))
2556 return STATUS_SUCCESS
;
2558 if (DeviceNode
->ServiceName
.Buffer
== NULL
)
2560 /* We don't need to worry about loading the driver because we're
2561 * being driven in raw mode so our parent must be loaded to get here */
2562 Status
= IopInitializeDevice(DeviceNode
, NULL
);
2563 if (NT_SUCCESS(Status
))
2565 Status
= IopStartDevice(DeviceNode
);
2566 if (!NT_SUCCESS(Status
))
2568 DPRINT1("IopStartDevice(%wZ) failed with status 0x%08x\n",
2569 &DeviceNode
->InstancePath
, Status
);
2575 PLDR_DATA_TABLE_ENTRY ModuleObject
;
2576 PDRIVER_OBJECT DriverObject
;
2578 /* Get existing DriverObject pointer (in case the driver has
2579 already been loaded and initialized) */
2580 Status
= IopGetDriverObject(
2582 &DeviceNode
->ServiceName
,
2585 if (!NT_SUCCESS(Status
))
2587 /* Driver is not initialized, try to load it */
2588 Status
= IopLoadServiceModule(&DeviceNode
->ServiceName
, &ModuleObject
);
2590 if (NT_SUCCESS(Status
) || Status
== STATUS_IMAGE_ALREADY_LOADED
)
2592 /* STATUS_IMAGE_ALREADY_LOADED means this driver
2593 was loaded by the bootloader */
2594 if ((Status
!= STATUS_IMAGE_ALREADY_LOADED
) ||
2595 (Status
== STATUS_IMAGE_ALREADY_LOADED
&& !DriverObject
))
2597 /* Initialize the driver */
2598 Status
= IopInitializeDriverModule(DeviceNode
, ModuleObject
,
2599 &DeviceNode
->ServiceName
, FALSE
, &DriverObject
);
2603 Status
= STATUS_SUCCESS
;
2608 DPRINT1("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
2609 &DeviceNode
->ServiceName
, Status
);
2613 /* Driver is loaded and initialized at this point */
2614 if (NT_SUCCESS(Status
))
2616 /* Initialize the device, including all filters */
2617 Status
= PipCallDriverAddDevice(DeviceNode
, FALSE
, DriverObject
);
2622 * Don't disable when trying to load only boot drivers
2626 IopDeviceNodeSetFlag(DeviceNode
, DNF_DISABLED
);
2627 IopDeviceNodeSetFlag(DeviceNode
, DNF_START_FAILED
);
2628 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2629 DPRINT1("Initialization of service %S failed (Status %x)\n",
2630 DeviceNode
->ServiceName
.Buffer
, Status
);
2635 return STATUS_SUCCESS
;
2639 * IopInitializePnpServices
2641 * Initialize services for discovered children
2645 * Top device node to start initializing services.
2651 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode
)
2653 DEVICETREE_TRAVERSE_CONTEXT Context
;
2655 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode
);
2657 IopInitDeviceTreeTraverseContext(
2660 IopActionInitChildServices
,
2663 return IopTraverseDeviceTree(&Context
);
2666 static NTSTATUS INIT_FUNCTION
2667 IopEnumerateDetectedDevices(
2669 IN PUNICODE_STRING RelativePath OPTIONAL
,
2671 IN BOOLEAN EnumerateSubKeys
,
2672 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources
,
2673 IN ULONG ParentBootResourcesLength
)
2675 UNICODE_STRING IdentifierU
= RTL_CONSTANT_STRING(L
"Identifier");
2676 UNICODE_STRING HardwareIDU
= RTL_CONSTANT_STRING(L
"HardwareID");
2677 UNICODE_STRING ConfigurationDataU
= RTL_CONSTANT_STRING(L
"Configuration Data");
2678 UNICODE_STRING BootConfigU
= RTL_CONSTANT_STRING(L
"BootConfig");
2679 UNICODE_STRING LogConfU
= RTL_CONSTANT_STRING(L
"LogConf");
2680 OBJECT_ATTRIBUTES ObjectAttributes
;
2681 HANDLE hDevicesKey
= NULL
;
2682 HANDLE hDeviceKey
= NULL
;
2683 HANDLE hLevel1Key
, hLevel2Key
= NULL
, hLogConf
;
2684 UNICODE_STRING Level2NameU
;
2685 WCHAR Level2Name
[5];
2686 ULONG IndexDevice
= 0;
2688 PKEY_BASIC_INFORMATION pDeviceInformation
= NULL
;
2689 ULONG DeviceInfoLength
= sizeof(KEY_BASIC_INFORMATION
) + 50 * sizeof(WCHAR
);
2690 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation
= NULL
;
2691 ULONG ValueInfoLength
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 50 * sizeof(WCHAR
);
2692 UNICODE_STRING DeviceName
, ValueName
;
2694 PCM_FULL_RESOURCE_DESCRIPTOR BootResources
= NULL
;
2695 ULONG BootResourcesLength
;
2698 const UNICODE_STRING IdentifierPci
= RTL_CONSTANT_STRING(L
"PCI");
2699 UNICODE_STRING HardwareIdPci
= RTL_CONSTANT_STRING(L
"*PNP0A03\0");
2700 static ULONG DeviceIndexPci
= 0;
2701 const UNICODE_STRING IdentifierSerial
= RTL_CONSTANT_STRING(L
"SerialController");
2702 UNICODE_STRING HardwareIdSerial
= RTL_CONSTANT_STRING(L
"*PNP0501\0");
2703 static ULONG DeviceIndexSerial
= 0;
2704 const UNICODE_STRING IdentifierKeyboard
= RTL_CONSTANT_STRING(L
"KeyboardController");
2705 UNICODE_STRING HardwareIdKeyboard
= RTL_CONSTANT_STRING(L
"*PNP0303\0");
2706 static ULONG DeviceIndexKeyboard
= 0;
2707 const UNICODE_STRING IdentifierMouse
= RTL_CONSTANT_STRING(L
"PointerController");
2708 UNICODE_STRING HardwareIdMouse
= RTL_CONSTANT_STRING(L
"*PNP0F13\0");
2709 static ULONG DeviceIndexMouse
= 0;
2710 const UNICODE_STRING IdentifierParallel
= RTL_CONSTANT_STRING(L
"ParallelController");
2711 UNICODE_STRING HardwareIdParallel
= RTL_CONSTANT_STRING(L
"*PNP0400\0");
2712 static ULONG DeviceIndexParallel
= 0;
2713 const UNICODE_STRING IdentifierFloppy
= RTL_CONSTANT_STRING(L
"FloppyDiskPeripheral");
2714 UNICODE_STRING HardwareIdFloppy
= RTL_CONSTANT_STRING(L
"*PNP0700\0");
2715 static ULONG DeviceIndexFloppy
= 0;
2716 const UNICODE_STRING IdentifierIsa
= RTL_CONSTANT_STRING(L
"ISA");
2717 UNICODE_STRING HardwareIdIsa
= RTL_CONSTANT_STRING(L
"*PNP0A00\0");
2718 static ULONG DeviceIndexIsa
= 0;
2719 UNICODE_STRING HardwareIdKey
;
2720 PUNICODE_STRING pHardwareId
;
2721 ULONG DeviceIndex
= 0;
2722 PUCHAR CmResourceList
;
2727 Status
= IopOpenRegistryKeyEx(&hDevicesKey
, hBaseKey
, RelativePath
, KEY_ENUMERATE_SUB_KEYS
);
2728 if (!NT_SUCCESS(Status
))
2730 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2735 hDevicesKey
= hBaseKey
;
2737 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2738 if (!pDeviceInformation
)
2740 DPRINT("ExAllocatePool() failed\n");
2741 Status
= STATUS_NO_MEMORY
;
2745 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2746 if (!pValueInformation
)
2748 DPRINT("ExAllocatePool() failed\n");
2749 Status
= STATUS_NO_MEMORY
;
2755 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2756 if (Status
== STATUS_NO_MORE_ENTRIES
)
2758 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2760 ExFreePool(pDeviceInformation
);
2761 DeviceInfoLength
= RequiredSize
;
2762 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2763 if (!pDeviceInformation
)
2765 DPRINT("ExAllocatePool() failed\n");
2766 Status
= STATUS_NO_MEMORY
;
2769 Status
= ZwEnumerateKey(hDevicesKey
, IndexDevice
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2771 if (!NT_SUCCESS(Status
))
2773 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2778 /* Open device key */
2779 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2780 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2782 Status
= IopOpenRegistryKeyEx(&hDeviceKey
, hDevicesKey
, &DeviceName
,
2783 KEY_QUERY_VALUE
+ (EnumerateSubKeys
? KEY_ENUMERATE_SUB_KEYS
: 0));
2784 if (!NT_SUCCESS(Status
))
2786 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
2790 /* Read boot resources, and add then to parent ones */
2791 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2792 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2794 ExFreePool(pValueInformation
);
2795 ValueInfoLength
= RequiredSize
;
2796 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2797 if (!pValueInformation
)
2799 DPRINT("ExAllocatePool() failed\n");
2800 ZwDeleteKey(hLevel2Key
);
2801 Status
= STATUS_NO_MEMORY
;
2804 Status
= ZwQueryValueKey(hDeviceKey
, &ConfigurationDataU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2806 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
2808 BootResources
= ParentBootResources
;
2809 BootResourcesLength
= ParentBootResourcesLength
;
2811 else if (!NT_SUCCESS(Status
))
2813 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2816 else if (pValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
)
2818 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_FULL_RESOURCE_DESCRIPTOR
);
2823 static const ULONG Header
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
.PartialDescriptors
);
2825 /* Concatenate current resources and parent ones */
2826 if (ParentBootResourcesLength
== 0)
2827 BootResourcesLength
= pValueInformation
->DataLength
;
2829 BootResourcesLength
= ParentBootResourcesLength
2830 + pValueInformation
->DataLength
2832 BootResources
= ExAllocatePool(PagedPool
, BootResourcesLength
);
2835 DPRINT("ExAllocatePool() failed\n");
2838 if (ParentBootResourcesLength
< sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
2840 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2842 else if (ParentBootResources
->PartialResourceList
.PartialDescriptors
[ParentBootResources
->PartialResourceList
.Count
- 1].Type
== CmResourceTypeDeviceSpecific
)
2844 RtlCopyMemory(BootResources
, pValueInformation
->Data
, pValueInformation
->DataLength
);
2846 (PVOID
)((ULONG_PTR
)BootResources
+ pValueInformation
->DataLength
),
2847 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2848 ParentBootResourcesLength
- Header
);
2849 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2853 RtlCopyMemory(BootResources
, pValueInformation
->Data
, Header
);
2855 (PVOID
)((ULONG_PTR
)BootResources
+ Header
),
2856 (PVOID
)((ULONG_PTR
)ParentBootResources
+ Header
),
2857 ParentBootResourcesLength
- Header
);
2859 (PVOID
)((ULONG_PTR
)BootResources
+ ParentBootResourcesLength
),
2860 pValueInformation
->Data
+ Header
,
2861 pValueInformation
->DataLength
- Header
);
2862 BootResources
->PartialResourceList
.Count
+= ParentBootResources
->PartialResourceList
.Count
;
2866 if (EnumerateSubKeys
)
2871 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2872 if (Status
== STATUS_NO_MORE_ENTRIES
)
2874 else if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2876 ExFreePool(pDeviceInformation
);
2877 DeviceInfoLength
= RequiredSize
;
2878 pDeviceInformation
= ExAllocatePool(PagedPool
, DeviceInfoLength
);
2879 if (!pDeviceInformation
)
2881 DPRINT("ExAllocatePool() failed\n");
2882 Status
= STATUS_NO_MEMORY
;
2885 Status
= ZwEnumerateKey(hDeviceKey
, IndexSubKey
, KeyBasicInformation
, pDeviceInformation
, DeviceInfoLength
, &RequiredSize
);
2887 if (!NT_SUCCESS(Status
))
2889 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status
);
2893 DeviceName
.Length
= DeviceName
.MaximumLength
= (USHORT
)pDeviceInformation
->NameLength
;
2894 DeviceName
.Buffer
= pDeviceInformation
->Name
;
2896 Status
= IopEnumerateDetectedDevices(
2902 BootResourcesLength
);
2903 if (!NT_SUCCESS(Status
))
2908 /* Read identifier */
2909 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2910 if (Status
== STATUS_BUFFER_OVERFLOW
|| Status
== STATUS_BUFFER_TOO_SMALL
)
2912 ExFreePool(pValueInformation
);
2913 ValueInfoLength
= RequiredSize
;
2914 pValueInformation
= ExAllocatePool(PagedPool
, ValueInfoLength
);
2915 if (!pValueInformation
)
2917 DPRINT("ExAllocatePool() failed\n");
2918 Status
= STATUS_NO_MEMORY
;
2921 Status
= ZwQueryValueKey(hDeviceKey
, &IdentifierU
, KeyValuePartialInformation
, pValueInformation
, ValueInfoLength
, &RequiredSize
);
2923 if (!NT_SUCCESS(Status
))
2925 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
2927 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status
);
2930 ValueName
.Length
= ValueName
.MaximumLength
= 0;
2932 else if (pValueInformation
->Type
!= REG_SZ
)
2934 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation
->Type
, REG_SZ
);
2939 /* Assign hardware id to this device */
2940 ValueName
.Length
= ValueName
.MaximumLength
= (USHORT
)pValueInformation
->DataLength
;
2941 ValueName
.Buffer
= (PWCHAR
)pValueInformation
->Data
;
2942 if (ValueName
.Length
>= sizeof(WCHAR
) && ValueName
.Buffer
[ValueName
.Length
/ sizeof(WCHAR
) - 1] == UNICODE_NULL
)
2943 ValueName
.Length
-= sizeof(WCHAR
);
2946 if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierSerial
, FALSE
) == 0)
2948 pHardwareId
= &HardwareIdSerial
;
2949 DeviceIndex
= DeviceIndexSerial
++;
2951 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierKeyboard
, FALSE
) == 0)
2953 pHardwareId
= &HardwareIdKeyboard
;
2954 DeviceIndex
= DeviceIndexKeyboard
++;
2956 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierMouse
, FALSE
) == 0)
2958 pHardwareId
= &HardwareIdMouse
;
2959 DeviceIndex
= DeviceIndexMouse
++;
2961 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierParallel
, FALSE
) == 0)
2963 pHardwareId
= &HardwareIdParallel
;
2964 DeviceIndex
= DeviceIndexParallel
++;
2966 else if (RelativePath
&& RtlCompareUnicodeString(RelativePath
, &IdentifierFloppy
, FALSE
) == 0)
2968 pHardwareId
= &HardwareIdFloppy
;
2969 DeviceIndex
= DeviceIndexFloppy
++;
2971 else if (NT_SUCCESS(Status
))
2973 /* Try to also match the device identifier */
2974 if (RtlCompareUnicodeString(&ValueName
, &IdentifierPci
, FALSE
) == 0)
2976 pHardwareId
= &HardwareIdPci
;
2977 DeviceIndex
= DeviceIndexPci
++;
2979 else if (RtlCompareUnicodeString(&ValueName
, &IdentifierIsa
, FALSE
) == 0)
2981 pHardwareId
= &HardwareIdIsa
;
2982 DeviceIndex
= DeviceIndexIsa
++;
2986 DPRINT("Unknown device '%wZ'\n", &ValueName
);
2992 /* Unknown key path */
2993 DPRINT("Unknown key path '%wZ'\n", RelativePath
);
2997 /* Prepare hardware id key (hardware id value without final \0) */
2998 HardwareIdKey
= *pHardwareId
;
2999 HardwareIdKey
.Length
-= sizeof(UNICODE_NULL
);
3001 /* Add the detected device to Root key */
3002 InitializeObjectAttributes(&ObjectAttributes
, &HardwareIdKey
, OBJ_KERNEL_HANDLE
, hRootKey
, NULL
);
3003 Status
= ZwCreateKey(
3009 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
3011 if (!NT_SUCCESS(Status
))
3013 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3016 swprintf(Level2Name
, L
"%04lu", DeviceIndex
);
3017 RtlInitUnicodeString(&Level2NameU
, Level2Name
);
3018 InitializeObjectAttributes(&ObjectAttributes
, &Level2NameU
, OBJ_KERNEL_HANDLE
, hLevel1Key
, NULL
);
3019 Status
= ZwCreateKey(
3021 KEY_SET_VALUE
| KEY_CREATE_SUB_KEY
,
3025 ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0,
3027 ZwClose(hLevel1Key
);
3028 if (!NT_SUCCESS(Status
))
3030 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3033 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName
, DeviceIndex
, &HardwareIdKey
);
3034 Status
= ZwSetValueKey(hLevel2Key
, &HardwareIDU
, 0, REG_MULTI_SZ
, pHardwareId
->Buffer
, pHardwareId
->MaximumLength
);
3035 if (!NT_SUCCESS(Status
))
3037 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3038 ZwDeleteKey(hLevel2Key
);
3041 /* Create 'LogConf' subkey */
3042 InitializeObjectAttributes(&ObjectAttributes
, &LogConfU
, OBJ_KERNEL_HANDLE
, hLevel2Key
, NULL
);
3043 Status
= ZwCreateKey(
3049 REG_OPTION_VOLATILE
,
3051 if (!NT_SUCCESS(Status
))
3053 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3054 ZwDeleteKey(hLevel2Key
);
3057 if (BootResourcesLength
>= sizeof(CM_FULL_RESOURCE_DESCRIPTOR
))
3059 CmResourceList
= ExAllocatePool(PagedPool
, BootResourcesLength
+ sizeof(ULONG
));
3060 if (!CmResourceList
)
3063 ZwDeleteKey(hLevel2Key
);
3067 /* Add the list count (1st member of CM_RESOURCE_LIST) */
3069 RtlCopyMemory(CmResourceList
,
3073 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
3074 RtlCopyMemory(CmResourceList
+ sizeof(ULONG
),
3076 BootResourcesLength
);
3078 /* Save boot resources to 'LogConf\BootConfig' */
3079 Status
= ZwSetValueKey(hLogConf
, &BootConfigU
, 0, REG_RESOURCE_LIST
, CmResourceList
, BootResourcesLength
+ sizeof(ULONG
));
3080 if (!NT_SUCCESS(Status
))
3082 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status
);
3084 ZwDeleteKey(hLevel2Key
);
3091 if (BootResources
&& BootResources
!= ParentBootResources
)
3093 ExFreePool(BootResources
);
3094 BootResources
= NULL
;
3098 ZwClose(hLevel2Key
);
3103 ZwClose(hDeviceKey
);
3108 Status
= STATUS_SUCCESS
;
3111 if (hDevicesKey
&& hDevicesKey
!= hBaseKey
)
3112 ZwClose(hDevicesKey
);
3114 ZwClose(hDeviceKey
);
3115 if (pDeviceInformation
)
3116 ExFreePool(pDeviceInformation
);
3117 if (pValueInformation
)
3118 ExFreePool(pValueInformation
);
3122 static BOOLEAN INIT_FUNCTION
3123 IopIsFirmwareMapperDisabled(VOID
)
3125 UNICODE_STRING KeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CURRENTCONTROLSET\\Control\\Pnp");
3126 UNICODE_STRING KeyNameU
= RTL_CONSTANT_STRING(L
"DisableFirmwareMapper");
3127 OBJECT_ATTRIBUTES ObjectAttributes
;
3129 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation
;
3130 ULONG DesiredLength
, Length
;
3134 InitializeObjectAttributes(&ObjectAttributes
, &KeyPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3135 Status
= ZwOpenKey(&hPnpKey
, KEY_QUERY_VALUE
, &ObjectAttributes
);
3136 if (NT_SUCCESS(Status
))
3138 Status
= ZwQueryValueKey(hPnpKey
,
3140 KeyValuePartialInformation
,
3144 if ((Status
== STATUS_BUFFER_TOO_SMALL
) ||
3145 (Status
== STATUS_BUFFER_OVERFLOW
))
3147 Length
= DesiredLength
;
3148 KeyInformation
= ExAllocatePool(PagedPool
, Length
);
3151 Status
= ZwQueryValueKey(hPnpKey
,
3153 KeyValuePartialInformation
,
3157 if (NT_SUCCESS(Status
) && KeyInformation
->DataLength
== sizeof(ULONG
))
3159 KeyValue
= (ULONG
)(*KeyInformation
->Data
);
3163 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed\n", &KeyPathU
, &KeyNameU
);
3166 ExFreePool(KeyInformation
);
3170 DPRINT1("Failed to allocate memory for registry query\n");
3175 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed with status 0x%08lx\n", &KeyPathU
, &KeyNameU
, Status
);
3182 DPRINT1("ZwOpenKey(%wZ) failed with status 0x%08lx\n", &KeyPathU
, Status
);
3185 DPRINT1("Firmware mapper is %s\n", KeyValue
!= 0 ? "disabled" : "enabled");
3187 return (KeyValue
!= 0) ? TRUE
: FALSE
;
3193 IopUpdateRootKey(VOID
)
3195 UNICODE_STRING EnumU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
3196 UNICODE_STRING RootPathU
= RTL_CONSTANT_STRING(L
"Root");
3197 UNICODE_STRING MultiKeyPathU
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
3198 OBJECT_ATTRIBUTES ObjectAttributes
;
3199 HANDLE hEnum
, hRoot
;
3202 InitializeObjectAttributes(&ObjectAttributes
, &EnumU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, NULL
, NULL
);
3203 Status
= ZwCreateKey(&hEnum
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3204 if (!NT_SUCCESS(Status
))
3206 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status
);
3210 InitializeObjectAttributes(&ObjectAttributes
, &RootPathU
, OBJ_KERNEL_HANDLE
| OBJ_CASE_INSENSITIVE
, hEnum
, NULL
);
3211 Status
= ZwCreateKey(&hRoot
, KEY_CREATE_SUB_KEY
, &ObjectAttributes
, 0, NULL
, 0, NULL
);
3213 if (!NT_SUCCESS(Status
))
3215 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3219 if (!IopIsFirmwareMapperDisabled())
3221 Status
= IopOpenRegistryKeyEx(&hEnum
, NULL
, &MultiKeyPathU
, KEY_ENUMERATE_SUB_KEYS
);
3222 if (!NT_SUCCESS(Status
))
3224 /* Nothing to do, don't return with an error status */
3225 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status
);
3227 return STATUS_SUCCESS
;
3229 Status
= IopEnumerateDetectedDevices(
3240 /* Enumeration is disabled */
3241 Status
= STATUS_SUCCESS
;
3251 IopOpenRegistryKeyEx(PHANDLE KeyHandle
,
3253 PUNICODE_STRING Name
,
3254 ACCESS_MASK DesiredAccess
)
3256 OBJECT_ATTRIBUTES ObjectAttributes
;
3263 InitializeObjectAttributes(&ObjectAttributes
,
3265 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3269 Status
= ZwOpenKey(KeyHandle
, DesiredAccess
, &ObjectAttributes
);
3276 IopCreateRegistryKeyEx(OUT PHANDLE Handle
,
3277 IN HANDLE RootHandle OPTIONAL
,
3278 IN PUNICODE_STRING KeyName
,
3279 IN ACCESS_MASK DesiredAccess
,
3280 IN ULONG CreateOptions
,
3281 OUT PULONG Disposition OPTIONAL
)
3283 OBJECT_ATTRIBUTES ObjectAttributes
;
3284 ULONG KeyDisposition
, RootHandleIndex
= 0, i
= 1, NestedCloseLevel
= 0;
3286 HANDLE HandleArray
[2];
3287 BOOLEAN Recursing
= TRUE
;
3289 UNICODE_STRING KeyString
;
3290 NTSTATUS Status
= STATUS_SUCCESS
;
3293 /* P1 is start, pp is end */
3294 p1
= KeyName
->Buffer
;
3295 pp
= (PVOID
)((ULONG_PTR
)p1
+ KeyName
->Length
);
3297 /* Create the target key */
3298 InitializeObjectAttributes(&ObjectAttributes
,
3300 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3303 Status
= ZwCreateKey(&HandleArray
[i
],
3311 /* Now we check if this failed */
3312 if ((Status
== STATUS_OBJECT_NAME_NOT_FOUND
) && (RootHandle
))
3314 /* Target key failed, so we'll need to create its parent. Setup array */
3315 HandleArray
[0] = NULL
;
3316 HandleArray
[1] = RootHandle
;
3318 /* Keep recursing for each missing parent */
3321 /* And if we're deep enough, close the last handle */
3322 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3324 /* We're setup to ping-pong between the two handle array entries */
3325 RootHandleIndex
= i
;
3328 /* Clear the one we're attempting to open now */
3329 HandleArray
[i
] = NULL
;
3331 /* Process the parent key name */
3332 for (p
= p1
; ((p
< pp
) && (*p
!= OBJ_NAME_PATH_SEPARATOR
)); p
++);
3333 Length
= (USHORT
)(p
- p1
) * sizeof(WCHAR
);
3335 /* Is there a parent name? */
3338 /* Build the unicode string for it */
3339 KeyString
.Buffer
= p1
;
3340 KeyString
.Length
= KeyString
.MaximumLength
= Length
;
3342 /* Now try opening the parent */
3343 InitializeObjectAttributes(&ObjectAttributes
,
3345 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
3346 HandleArray
[RootHandleIndex
],
3348 Status
= ZwCreateKey(&HandleArray
[i
],
3355 if (NT_SUCCESS(Status
))
3357 /* It worked, we have one more handle */
3362 /* Parent key creation failed, abandon loop */
3369 /* We don't have a parent name, probably corrupted key name */
3370 Status
= STATUS_INVALID_PARAMETER
;
3375 /* Now see if there's more parents to create */
3377 if ((p
== pp
) || (p1
== pp
))
3379 /* We're done, hopefully successfully, so stop */
3384 /* Outer loop check for handle nesting that requires closing the top handle */
3385 if (NestedCloseLevel
> 1) ZwClose(HandleArray
[RootHandleIndex
]);
3388 /* Check if we broke out of the loop due to success */
3389 if (NT_SUCCESS(Status
))
3391 /* Return the target handle (we closed all the parent ones) and disposition */
3392 *Handle
= HandleArray
[i
];
3393 if (Disposition
) *Disposition
= KeyDisposition
;
3396 /* Return the success state */
3402 IopGetRegistryValue(IN HANDLE Handle
,
3404 OUT PKEY_VALUE_FULL_INFORMATION
*Information
)
3406 UNICODE_STRING ValueString
;
3408 PKEY_VALUE_FULL_INFORMATION FullInformation
;
3412 RtlInitUnicodeString(&ValueString
, ValueName
);
3414 Status
= ZwQueryValueKey(Handle
,
3416 KeyValueFullInformation
,
3420 if ((Status
!= STATUS_BUFFER_OVERFLOW
) &&
3421 (Status
!= STATUS_BUFFER_TOO_SMALL
))
3426 FullInformation
= ExAllocatePool(NonPagedPool
, Size
);
3427 if (!FullInformation
) return STATUS_INSUFFICIENT_RESOURCES
;
3429 Status
= ZwQueryValueKey(Handle
,
3431 KeyValueFullInformation
,
3435 if (!NT_SUCCESS(Status
))
3437 ExFreePool(FullInformation
);
3441 *Information
= FullInformation
;
3442 return STATUS_SUCCESS
;
3445 RTL_GENERIC_COMPARE_RESULTS
3447 PiCompareInstancePath(IN PRTL_AVL_TABLE Table
,
3448 IN PVOID FirstStruct
,
3449 IN PVOID SecondStruct
)
3457 // The allocation function is called by the generic table package whenever
3458 // it needs to allocate memory for the table.
3463 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3473 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table
,
3482 PpInitializeDeviceReferenceTable(VOID
)
3484 /* Setup the guarded mutex and AVL table */
3485 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock
);
3486 RtlInitializeGenericTableAvl(
3487 &PpDeviceReferenceTable
,
3488 (PRTL_AVL_COMPARE_ROUTINE
)PiCompareInstancePath
,
3489 (PRTL_AVL_ALLOCATE_ROUTINE
)PiAllocateGenericTableEntry
,
3490 (PRTL_AVL_FREE_ROUTINE
)PiFreeGenericTableEntry
,
3498 /* Initialize the resource when accessing device registry data */
3499 ExInitializeResourceLite(&PpRegistryDeviceResource
);
3501 /* Setup the device reference AVL table */
3502 PpInitializeDeviceReferenceTable();
3510 /* Check the initialization phase */
3511 switch (ExpInitializationPhase
)
3516 return PiInitPhase0();
3522 //return PiInitPhase1();
3526 /* Don't know any other phase! Bugcheck! */
3527 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL
);
3532 LONG IopNumberDeviceNodes
;
3536 PipAllocateDeviceNode(IN PDEVICE_OBJECT PhysicalDeviceObject
)
3538 PDEVICE_NODE DeviceNode
;
3542 DeviceNode
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(DEVICE_NODE
), 'donD');
3543 if (!DeviceNode
) return DeviceNode
;
3546 InterlockedIncrement(&IopNumberDeviceNodes
);
3549 RtlZeroMemory(DeviceNode
, sizeof(DEVICE_NODE
));
3550 DeviceNode
->InterfaceType
= InterfaceTypeUndefined
;
3551 DeviceNode
->BusNumber
= -1;
3552 DeviceNode
->ChildInterfaceType
= InterfaceTypeUndefined
;
3553 DeviceNode
->ChildBusNumber
= -1;
3554 DeviceNode
->ChildBusTypeIndex
= -1;
3555 // KeInitializeEvent(&DeviceNode->EnumerationMutex, SynchronizationEvent, TRUE);
3556 InitializeListHead(&DeviceNode
->DeviceArbiterList
);
3557 InitializeListHead(&DeviceNode
->DeviceTranslatorList
);
3558 InitializeListHead(&DeviceNode
->TargetDeviceNotify
);
3559 InitializeListHead(&DeviceNode
->DockInfo
.ListEntry
);
3560 InitializeListHead(&DeviceNode
->PendedSetInterfaceState
);
3562 /* Check if there is a PDO */
3563 if (PhysicalDeviceObject
)
3565 /* Link it and remove the init flag */
3566 DeviceNode
->PhysicalDeviceObject
= PhysicalDeviceObject
;
3567 ((PEXTENDED_DEVOBJ_EXTENSION
)PhysicalDeviceObject
->DeviceObjectExtension
)->DeviceNode
= DeviceNode
;
3568 PhysicalDeviceObject
->Flags
&= ~DO_DEVICE_INITIALIZING
;
3571 /* Return the node */
3575 /* PUBLIC FUNCTIONS **********************************************************/
3579 PnpBusTypeGuidGet(IN USHORT Index
,
3580 IN LPGUID BusTypeGuid
)
3582 NTSTATUS Status
= STATUS_SUCCESS
;
3584 /* Acquire the lock */
3585 ExAcquireFastMutex(&PnpBusTypeGuidList
->Lock
);
3588 if (Index
< PnpBusTypeGuidList
->GuidCount
)
3591 RtlCopyMemory(BusTypeGuid
, &PnpBusTypeGuidList
->Guids
[Index
], sizeof(GUID
));
3596 Status
= STATUS_OBJECT_NAME_NOT_FOUND
;
3599 /* Release lock and return status */
3600 ExReleaseFastMutex(&PnpBusTypeGuidList
->Lock
);
3606 PnpDeviceObjectToDeviceInstance(IN PDEVICE_OBJECT DeviceObject
,
3607 IN PHANDLE DeviceInstanceHandle
,
3608 IN ACCESS_MASK DesiredAccess
)
3612 PDEVICE_NODE DeviceNode
;
3613 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
3616 /* Open the enum key */
3617 Status
= IopOpenRegistryKeyEx(&KeyHandle
,
3621 if (!NT_SUCCESS(Status
)) return Status
;
3623 /* Make sure we have an instance path */
3624 DeviceNode
= IopGetDeviceNode(DeviceObject
);
3625 if ((DeviceNode
) && (DeviceNode
->InstancePath
.Length
))
3627 /* Get the instance key */
3628 Status
= IopOpenRegistryKeyEx(DeviceInstanceHandle
,
3630 &DeviceNode
->InstancePath
,
3636 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3639 /* Close the handle and return status */
3646 PnpDetermineResourceListSize(IN PCM_RESOURCE_LIST ResourceList
)
3648 ULONG FinalSize
, PartialSize
, EntrySize
, i
, j
;
3649 PCM_FULL_RESOURCE_DESCRIPTOR FullDescriptor
;
3650 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
3652 /* If we don't have one, that's easy */
3653 if (!ResourceList
) return 0;
3655 /* Start with the minimum size possible */
3656 FinalSize
= FIELD_OFFSET(CM_RESOURCE_LIST
, List
);
3658 /* Loop each full descriptor */
3659 FullDescriptor
= ResourceList
->List
;
3660 for (i
= 0; i
< ResourceList
->Count
; i
++)
3662 /* Start with the minimum size possible */
3663 PartialSize
= FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR
, PartialResourceList
) +
3664 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST
, PartialDescriptors
);
3666 /* Loop each partial descriptor */
3667 PartialDescriptor
= FullDescriptor
->PartialResourceList
.PartialDescriptors
;
3668 for (j
= 0; j
< FullDescriptor
->PartialResourceList
.Count
; j
++)
3670 /* Start with the minimum size possible */
3671 EntrySize
= sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
3673 /* Check if there is extra data */
3674 if (PartialDescriptor
->Type
== CmResourceTypeDeviceSpecific
)
3677 EntrySize
+= PartialDescriptor
->u
.DeviceSpecificData
.DataSize
;
3680 /* The size of partial descriptors is bigger */
3681 PartialSize
+= EntrySize
;
3683 /* Go to the next partial descriptor */
3684 PartialDescriptor
= (PVOID
)((ULONG_PTR
)PartialDescriptor
+ EntrySize
);
3687 /* The size of full descriptors is bigger */
3688 FinalSize
+= PartialSize
;
3690 /* Go to the next full descriptor */
3691 FullDescriptor
= (PVOID
)((ULONG_PTR
)FullDescriptor
+ PartialSize
);
3694 /* Return the final size */
3700 PiGetDeviceRegistryProperty(IN PDEVICE_OBJECT DeviceObject
,
3705 IN PULONG BufferLength
)
3708 HANDLE KeyHandle
, SubHandle
;
3709 UNICODE_STRING KeyString
;
3710 PKEY_VALUE_FULL_INFORMATION KeyValueInfo
= NULL
;
3714 /* Find the instance key */
3715 Status
= PnpDeviceObjectToDeviceInstance(DeviceObject
, &KeyHandle
, KEY_READ
);
3716 if (NT_SUCCESS(Status
))
3718 /* Check for name given by caller */
3722 RtlInitUnicodeString(&KeyString
, KeyName
);
3723 Status
= IopOpenRegistryKeyEx(&SubHandle
,
3727 if (NT_SUCCESS(Status
))
3729 /* And use this handle instead */
3731 KeyHandle
= SubHandle
;
3735 /* Check if sub-key handle succeeded (or no-op if no key name given) */
3736 if (NT_SUCCESS(Status
))
3738 /* Now get the size of the property */
3739 Status
= IopGetRegistryValue(KeyHandle
,
3748 /* Fail if any of the registry operations failed */
3749 if (!NT_SUCCESS(Status
)) return Status
;
3751 /* Check how much data we have to copy */
3752 Length
= KeyValueInfo
->DataLength
;
3753 if (*BufferLength
>= Length
)
3755 /* Check for a match in the value type */
3756 if (KeyValueInfo
->Type
== ValueType
)
3759 RtlCopyMemory(Buffer
,
3760 (PVOID
)((ULONG_PTR
)KeyValueInfo
+
3761 KeyValueInfo
->DataOffset
),
3766 /* Invalid registry property type, fail */
3767 Status
= STATUS_INVALID_PARAMETER_2
;
3772 /* Buffer is too small to hold data */
3773 Status
= STATUS_BUFFER_TOO_SMALL
;
3776 /* Return the required buffer length, free the buffer, and return status */
3777 *BufferLength
= Length
;
3778 ExFreePool(KeyValueInfo
);
3782 #define PIP_RETURN_DATA(x, y) {ReturnLength = x; Data = y; Status = STATUS_SUCCESS; break;}
3783 #define PIP_REGISTRY_DATA(x, y) {ValueName = x; ValueType = y; break;}
3784 #define PIP_UNIMPLEMENTED() {UNIMPLEMENTED; while(TRUE); break;}
3791 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject
,
3792 IN DEVICE_REGISTRY_PROPERTY DeviceProperty
,
3793 IN ULONG BufferLength
,
3794 OUT PVOID PropertyBuffer
,
3795 OUT PULONG ResultLength
)
3797 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
3798 DEVICE_CAPABILITIES DeviceCaps
;
3799 ULONG ReturnLength
= 0, Length
= 0, ValueType
;
3800 PWCHAR ValueName
= NULL
, EnumeratorNameEnd
, DeviceInstanceName
;
3802 NTSTATUS Status
= STATUS_BUFFER_TOO_SMALL
;
3804 POBJECT_NAME_INFORMATION ObjectNameInfo
= NULL
;
3805 BOOLEAN NullTerminate
= FALSE
;
3807 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject
, DeviceProperty
);
3809 /* Assume failure */
3812 /* Only PDOs can call this */
3813 if (!DeviceNode
) return STATUS_INVALID_DEVICE_REQUEST
;
3815 /* Handle all properties */
3816 switch (DeviceProperty
)
3818 case DevicePropertyBusTypeGuid
:
3820 /* Get the GUID from the internal cache */
3821 Status
= PnpBusTypeGuidGet(DeviceNode
->ChildBusTypeIndex
, &BusTypeGuid
);
3822 if (!NT_SUCCESS(Status
)) return Status
;
3824 /* This is the format of the returned data */
3825 PIP_RETURN_DATA(sizeof(GUID
), &BusTypeGuid
);
3827 case DevicePropertyLegacyBusType
:
3829 /* Validate correct interface type */
3830 if (DeviceNode
->ChildInterfaceType
== InterfaceTypeUndefined
)
3831 return STATUS_OBJECT_NAME_NOT_FOUND
;
3833 /* This is the format of the returned data */
3834 PIP_RETURN_DATA(sizeof(INTERFACE_TYPE
), &DeviceNode
->ChildInterfaceType
);
3836 case DevicePropertyBusNumber
:
3838 /* Validate correct bus number */
3839 if ((DeviceNode
->ChildBusNumber
& 0x80000000) == 0x80000000)
3840 return STATUS_OBJECT_NAME_NOT_FOUND
;
3842 /* This is the format of the returned data */
3843 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceNode
->ChildBusNumber
);
3845 case DevicePropertyEnumeratorName
:
3847 /* Get the instance path */
3848 DeviceInstanceName
= DeviceNode
->InstancePath
.Buffer
;
3851 ASSERT((BufferLength
& 1) == 0);
3852 ASSERT(DeviceInstanceName
!= NULL
);
3854 /* Get the name from the path */
3855 EnumeratorNameEnd
= wcschr(DeviceInstanceName
, OBJ_NAME_PATH_SEPARATOR
);
3856 ASSERT(EnumeratorNameEnd
);
3858 /* This string needs to be NULL-terminated */
3859 NullTerminate
= TRUE
;
3861 /* This is the format of the returned data */
3862 PIP_RETURN_DATA((ULONG
)(EnumeratorNameEnd
- DeviceInstanceName
) * sizeof(WCHAR
),
3863 DeviceInstanceName
);
3865 case DevicePropertyAddress
:
3867 /* Query the device caps */
3868 Status
= IopQueryDeviceCapabilities(DeviceNode
, &DeviceCaps
);
3869 if (!NT_SUCCESS(Status
) || (DeviceCaps
.Address
== MAXULONG
))
3870 return STATUS_OBJECT_NAME_NOT_FOUND
;
3872 /* This is the format of the returned data */
3873 PIP_RETURN_DATA(sizeof(ULONG
), &DeviceCaps
.Address
);
3875 case DevicePropertyBootConfigurationTranslated
:
3877 /* Validate we have resources */
3878 if (!DeviceNode
->BootResources
)
3879 // if (!DeviceNode->BootResourcesTranslated) // FIXFIX: Need this field
3881 /* No resources will still fake success, but with 0 bytes */
3883 return STATUS_SUCCESS
;
3886 /* This is the format of the returned data */
3887 PIP_RETURN_DATA(PnpDetermineResourceListSize(DeviceNode
->BootResources
), // FIXFIX: Should use BootResourcesTranslated
3888 DeviceNode
->BootResources
); // FIXFIX: Should use BootResourcesTranslated
3890 case DevicePropertyPhysicalDeviceObjectName
:
3892 /* Sanity check for Unicode-sized string */
3893 ASSERT((BufferLength
& 1) == 0);
3895 /* Allocate name buffer */
3896 Length
= BufferLength
+ sizeof(OBJECT_NAME_INFORMATION
);
3897 ObjectNameInfo
= ExAllocatePool(PagedPool
, Length
);
3898 if (!ObjectNameInfo
) return STATUS_INSUFFICIENT_RESOURCES
;
3900 /* Query the PDO name */
3901 Status
= ObQueryNameString(DeviceObject
,
3905 if (Status
== STATUS_INFO_LENGTH_MISMATCH
)
3907 /* It's up to the caller to try again */
3908 Status
= STATUS_BUFFER_TOO_SMALL
;
3911 /* This string needs to be NULL-terminated */
3912 NullTerminate
= TRUE
;
3914 /* Return if successful */
3915 if (NT_SUCCESS(Status
)) PIP_RETURN_DATA(ObjectNameInfo
->Name
.Length
,
3916 ObjectNameInfo
->Name
.Buffer
);
3918 /* Let the caller know how big the name is */
3919 *ResultLength
-= sizeof(OBJECT_NAME_INFORMATION
);
3922 /* Handle the registry-based properties */
3923 case DevicePropertyUINumber
:
3924 PIP_REGISTRY_DATA(REGSTR_VAL_UI_NUMBER
, REG_DWORD
);
3925 case DevicePropertyLocationInformation
:
3926 PIP_REGISTRY_DATA(REGSTR_VAL_LOCATION_INFORMATION
, REG_SZ
);
3927 case DevicePropertyDeviceDescription
:
3928 PIP_REGISTRY_DATA(REGSTR_VAL_DEVDESC
, REG_SZ
);
3929 case DevicePropertyHardwareID
:
3930 PIP_REGISTRY_DATA(REGSTR_VAL_HARDWAREID
, REG_MULTI_SZ
);
3931 case DevicePropertyCompatibleIDs
:
3932 PIP_REGISTRY_DATA(REGSTR_VAL_COMPATIBLEIDS
, REG_MULTI_SZ
);
3933 case DevicePropertyBootConfiguration
:
3934 PIP_REGISTRY_DATA(REGSTR_VAL_BOOTCONFIG
, REG_RESOURCE_LIST
);
3935 case DevicePropertyClassName
:
3936 PIP_REGISTRY_DATA(REGSTR_VAL_CLASS
, REG_SZ
);
3937 case DevicePropertyClassGuid
:
3938 PIP_REGISTRY_DATA(REGSTR_VAL_CLASSGUID
, REG_SZ
);
3939 case DevicePropertyDriverKeyName
:
3940 PIP_REGISTRY_DATA(REGSTR_VAL_DRIVER
, REG_SZ
);
3941 case DevicePropertyManufacturer
:
3942 PIP_REGISTRY_DATA(REGSTR_VAL_MFG
, REG_SZ
);
3943 case DevicePropertyFriendlyName
:
3944 PIP_REGISTRY_DATA(REGSTR_VAL_FRIENDLYNAME
, REG_SZ
);
3945 case DevicePropertyContainerID
:
3946 //PIP_REGISTRY_DATA(REGSTR_VAL_CONTAINERID, REG_SZ); // Win7
3947 PIP_UNIMPLEMENTED();
3948 case DevicePropertyRemovalPolicy
:
3949 PIP_UNIMPLEMENTED();
3950 case DevicePropertyInstallState
:
3951 PIP_UNIMPLEMENTED();
3952 case DevicePropertyResourceRequirements
:
3953 PIP_UNIMPLEMENTED();
3954 case DevicePropertyAllocatedResources
:
3955 PIP_UNIMPLEMENTED();
3957 return STATUS_INVALID_PARAMETER_2
;
3960 /* Having a registry value name implies registry data */
3963 /* We know up-front how much data to expect */
3964 *ResultLength
= BufferLength
;
3966 /* Go get the data, use the LogConf subkey if necessary */
3967 Status
= PiGetDeviceRegistryProperty(DeviceObject
,
3971 DevicePropertyBootConfiguration
) ?
3976 else if (NT_SUCCESS(Status
))
3978 /* We know up-front how much data to expect, check the caller's buffer */
3979 *ResultLength
= ReturnLength
+ (NullTerminate
? sizeof(UNICODE_NULL
) : 0);
3980 if (*ResultLength
<= BufferLength
)
3982 /* Buffer is all good, copy the data */
3983 RtlCopyMemory(PropertyBuffer
, Data
, ReturnLength
);
3985 /* Check if we need to NULL-terminate the string */
3988 /* Terminate the string */
3989 ((PWCHAR
)PropertyBuffer
)[ReturnLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
3992 /* This is the success path */
3993 Status
= STATUS_SUCCESS
;
3998 Status
= STATUS_BUFFER_TOO_SMALL
;
4002 /* Free any allocation we may have made, and return the status code */
4003 if (ObjectNameInfo
) ExFreePool(ObjectNameInfo
);
4012 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4014 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
4015 IO_STACK_LOCATION Stack
;
4018 IO_STATUS_BLOCK IoStatusBlock
;
4020 RtlZeroMemory(&Stack
, sizeof(IO_STACK_LOCATION
));
4021 Stack
.MajorFunction
= IRP_MJ_PNP
;
4022 Stack
.MinorFunction
= IRP_MN_QUERY_PNP_DEVICE_STATE
;
4024 Status
= IopSynchronousCall(PhysicalDeviceObject
, &Stack
, (PVOID
*)&PnPFlags
);
4025 if (!NT_SUCCESS(Status
))
4027 DPRINT1("IRP_MN_QUERY_PNP_DEVICE_STATE failed with status 0x%x\n", Status
);
4031 if (PnPFlags
& PNP_DEVICE_NOT_DISABLEABLE
)
4032 DeviceNode
->UserFlags
|= DNUF_NOT_DISABLEABLE
;
4034 DeviceNode
->UserFlags
&= ~DNUF_NOT_DISABLEABLE
;
4036 if (PnPFlags
& PNP_DEVICE_DONT_DISPLAY_IN_UI
)
4037 DeviceNode
->UserFlags
|= DNUF_DONT_SHOW_IN_UI
;
4039 DeviceNode
->UserFlags
&= ~DNUF_DONT_SHOW_IN_UI
;
4041 if ((PnPFlags
& PNP_DEVICE_REMOVED
) ||
4042 ((PnPFlags
& PNP_DEVICE_FAILED
) && !(PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)))
4044 /* Surprise removal */
4046 IopSendSurpriseRemoval(PhysicalDeviceObject
);
4048 /* Tell the user-mode PnP manager that a device was removed */
4049 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL
,
4050 &DeviceNode
->InstancePath
);
4052 IopSendRemoveDevice(PhysicalDeviceObject
);
4054 else if ((PnPFlags
& PNP_DEVICE_FAILED
) && (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
))
4056 /* Stop for resource rebalance */
4058 Status
= IopStopDevice(DeviceNode
);
4059 if (!NT_SUCCESS(Status
))
4061 DPRINT1("Failed to stop device for rebalancing\n");
4063 /* Stop failed so don't rebalance */
4064 PnPFlags
&= ~PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
;
4068 /* Resource rebalance */
4069 if (PnPFlags
& PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED
)
4071 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
4073 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4075 IRP_MN_QUERY_RESOURCES
,
4077 if (NT_SUCCESS(Status
) && IoStatusBlock
.Information
)
4079 DeviceNode
->BootResources
=
4080 (PCM_RESOURCE_LIST
)IoStatusBlock
.Information
;
4081 IopDeviceNodeSetFlag(DeviceNode
, DNF_HAS_BOOT_CONFIG
);
4085 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status
);
4086 DeviceNode
->BootResources
= NULL
;
4089 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
4091 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4093 IRP_MN_QUERY_RESOURCE_REQUIREMENTS
,
4095 if (NT_SUCCESS(Status
))
4097 DeviceNode
->ResourceRequirements
=
4098 (PIO_RESOURCE_REQUIREMENTS_LIST
)IoStatusBlock
.Information
;
4102 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status
);
4103 DeviceNode
->ResourceRequirements
= NULL
;
4106 /* IRP_MN_FILTER_RESOURCE_REQUIREMENTS is called indirectly by IopStartDevice */
4107 if (IopStartDevice(DeviceNode
) != STATUS_SUCCESS
)
4109 DPRINT1("Restart after resource rebalance failed\n");
4111 DeviceNode
->Flags
&= ~(DNF_STARTED
| DNF_START_REQUEST_PENDING
);
4112 DeviceNode
->Flags
|= DNF_START_FAILED
;
4114 IopRemoveDevice(DeviceNode
);
4120 * @name IoOpenDeviceRegistryKey
4122 * Open a registry key unique for a specified driver or device instance.
4124 * @param DeviceObject Device to get the registry key for.
4125 * @param DevInstKeyType Type of the key to return.
4126 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
4127 * @param DevInstRegKey Handle to the opened registry key on
4128 * successful return.
4136 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject
,
4137 IN ULONG DevInstKeyType
,
4138 IN ACCESS_MASK DesiredAccess
,
4139 OUT PHANDLE DevInstRegKey
)
4141 static WCHAR RootKeyName
[] =
4142 L
"\\Registry\\Machine\\System\\CurrentControlSet\\";
4143 static WCHAR ProfileKeyName
[] =
4144 L
"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
4145 static WCHAR ClassKeyName
[] = L
"Control\\Class\\";
4146 static WCHAR EnumKeyName
[] = L
"Enum\\";
4147 static WCHAR DeviceParametersKeyName
[] = L
"Device Parameters";
4148 ULONG KeyNameLength
;
4149 LPWSTR KeyNameBuffer
;
4150 UNICODE_STRING KeyName
;
4151 ULONG DriverKeyLength
;
4152 OBJECT_ATTRIBUTES ObjectAttributes
;
4153 PDEVICE_NODE DeviceNode
= NULL
;
4156 DPRINT("IoOpenDeviceRegistryKey() called\n");
4158 if ((DevInstKeyType
& (PLUGPLAY_REGKEY_DEVICE
| PLUGPLAY_REGKEY_DRIVER
)) == 0)
4160 DPRINT1("IoOpenDeviceRegistryKey(): got wrong params, exiting... \n");
4161 return STATUS_INVALID_PARAMETER
;
4164 if (!IopIsValidPhysicalDeviceObject(DeviceObject
))
4165 return STATUS_INVALID_DEVICE_REQUEST
;
4166 DeviceNode
= IopGetDeviceNode(DeviceObject
);
4169 * Calculate the length of the base key name. This is the full
4170 * name for driver key or the name excluding "Device Parameters"
4171 * subkey for device key.
4174 KeyNameLength
= sizeof(RootKeyName
);
4175 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4176 KeyNameLength
+= sizeof(ProfileKeyName
) - sizeof(UNICODE_NULL
);
4177 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4179 KeyNameLength
+= sizeof(ClassKeyName
) - sizeof(UNICODE_NULL
);
4180 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4181 0, NULL
, &DriverKeyLength
);
4182 if (Status
!= STATUS_BUFFER_TOO_SMALL
)
4184 KeyNameLength
+= DriverKeyLength
;
4188 KeyNameLength
+= sizeof(EnumKeyName
) - sizeof(UNICODE_NULL
) +
4189 DeviceNode
->InstancePath
.Length
;
4193 * Now allocate the buffer for the key name...
4196 KeyNameBuffer
= ExAllocatePool(PagedPool
, KeyNameLength
);
4197 if (KeyNameBuffer
== NULL
)
4198 return STATUS_INSUFFICIENT_RESOURCES
;
4201 KeyName
.MaximumLength
= (USHORT
)KeyNameLength
;
4202 KeyName
.Buffer
= KeyNameBuffer
;
4205 * ...and build the key name.
4208 KeyName
.Length
+= sizeof(RootKeyName
) - sizeof(UNICODE_NULL
);
4209 RtlCopyMemory(KeyNameBuffer
, RootKeyName
, KeyName
.Length
);
4211 if (DevInstKeyType
& PLUGPLAY_REGKEY_CURRENT_HWPROFILE
)
4212 RtlAppendUnicodeToString(&KeyName
, ProfileKeyName
);
4214 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4216 RtlAppendUnicodeToString(&KeyName
, ClassKeyName
);
4217 Status
= IoGetDeviceProperty(DeviceObject
, DevicePropertyDriverKeyName
,
4218 DriverKeyLength
, KeyNameBuffer
+
4219 (KeyName
.Length
/ sizeof(WCHAR
)),
4221 if (!NT_SUCCESS(Status
))
4223 DPRINT1("Call to IoGetDeviceProperty() failed with Status 0x%08lx\n", Status
);
4224 ExFreePool(KeyNameBuffer
);
4227 KeyName
.Length
+= (USHORT
)DriverKeyLength
- sizeof(UNICODE_NULL
);
4231 RtlAppendUnicodeToString(&KeyName
, EnumKeyName
);
4232 Status
= RtlAppendUnicodeStringToString(&KeyName
, &DeviceNode
->InstancePath
);
4233 if (DeviceNode
->InstancePath
.Length
== 0)
4235 ExFreePool(KeyNameBuffer
);
4241 * Open the base key.
4243 Status
= IopOpenRegistryKeyEx(DevInstRegKey
, NULL
, &KeyName
, DesiredAccess
);
4244 if (!NT_SUCCESS(Status
))
4246 DPRINT1("IoOpenDeviceRegistryKey(%wZ): Base key doesn't exist, exiting... (Status 0x%08lx)\n", &KeyName
, Status
);
4247 ExFreePool(KeyNameBuffer
);
4250 ExFreePool(KeyNameBuffer
);
4253 * For driver key we're done now.
4256 if (DevInstKeyType
& PLUGPLAY_REGKEY_DRIVER
)
4260 * Let's go further. For device key we must open "Device Parameters"
4261 * subkey and create it if it doesn't exist yet.
4264 RtlInitUnicodeString(&KeyName
, DeviceParametersKeyName
);
4265 InitializeObjectAttributes(&ObjectAttributes
, &KeyName
,
4266 OBJ_CASE_INSENSITIVE
, *DevInstRegKey
, NULL
);
4267 Status
= ZwCreateKey(DevInstRegKey
, DesiredAccess
, &ObjectAttributes
,
4268 0, NULL
, ExpInTextModeSetup
? REG_OPTION_VOLATILE
: 0, NULL
);
4269 ZwClose(ObjectAttributes
.RootDirectory
);
4276 IopQueryRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
, BOOLEAN Force
)
4278 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
, FailedRemoveDevice
;
4282 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4283 ChildDeviceNode
= ParentDeviceNode
->Child
;
4284 while (ChildDeviceNode
!= NULL
)
4286 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4287 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4289 Status
= IopPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
, Force
);
4290 if (!NT_SUCCESS(Status
))
4292 FailedRemoveDevice
= ChildDeviceNode
;
4296 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4297 ChildDeviceNode
= NextDeviceNode
;
4299 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4301 return STATUS_SUCCESS
;
4304 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4305 ChildDeviceNode
= ParentDeviceNode
->Child
;
4306 while (ChildDeviceNode
!= NULL
)
4308 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4309 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4311 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
4313 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4314 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4315 if (ChildDeviceNode
== FailedRemoveDevice
)
4318 ChildDeviceNode
= NextDeviceNode
;
4320 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4322 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4329 IopSendRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
4331 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
4334 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4335 ChildDeviceNode
= ParentDeviceNode
->Child
;
4336 while (ChildDeviceNode
!= NULL
)
4338 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4339 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4341 IopSendRemoveDevice(ChildDeviceNode
->PhysicalDeviceObject
);
4343 ChildDeviceNode
= NextDeviceNode
;
4345 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4347 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4352 IopCancelRemoveChildDevices(PDEVICE_NODE ParentDeviceNode
)
4354 PDEVICE_NODE ChildDeviceNode
, NextDeviceNode
;
4357 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4358 ChildDeviceNode
= ParentDeviceNode
->Child
;
4359 while (ChildDeviceNode
!= NULL
)
4361 NextDeviceNode
= ChildDeviceNode
->Sibling
;
4362 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4364 IopCancelPrepareDeviceForRemoval(ChildDeviceNode
->PhysicalDeviceObject
);
4366 ChildDeviceNode
= NextDeviceNode
;
4368 KeAcquireSpinLock(&IopDeviceTreeLock
, &OldIrql
);
4370 KeReleaseSpinLock(&IopDeviceTreeLock
, OldIrql
);
4375 IopQueryRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
, BOOLEAN Force
)
4377 /* This function DOES NOT dereference the device objects on SUCCESS
4378 * but it DOES dereference device objects on FAILURE */
4383 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4385 Status
= IopPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
], Force
);
4386 if (!NT_SUCCESS(Status
))
4393 return STATUS_SUCCESS
;
4396 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4397 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4398 for (i
= 0; i
<= j
; i
++)
4400 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4401 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4402 DeviceRelations
->Objects
[i
] = NULL
;
4404 for (; i
< DeviceRelations
->Count
; i
++)
4406 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4407 DeviceRelations
->Objects
[i
] = NULL
;
4409 ExFreePool(DeviceRelations
);
4416 IopSendRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4418 /* This function DOES dereference the device objects in all cases */
4422 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4424 IopSendRemoveDevice(DeviceRelations
->Objects
[i
]);
4425 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4426 DeviceRelations
->Objects
[i
] = NULL
;
4429 ExFreePool(DeviceRelations
);
4434 IopCancelRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations
)
4436 /* This function DOES dereference the device objects in all cases */
4440 for (i
= 0; i
< DeviceRelations
->Count
; i
++)
4442 IopCancelPrepareDeviceForRemoval(DeviceRelations
->Objects
[i
]);
4443 ObDereferenceObject(DeviceRelations
->Objects
[i
]);
4444 DeviceRelations
->Objects
[i
] = NULL
;
4447 ExFreePool(DeviceRelations
);
4451 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject
)
4453 IO_STACK_LOCATION Stack
;
4454 IO_STATUS_BLOCK IoStatusBlock
;
4455 PDEVICE_RELATIONS DeviceRelations
;
4458 IopCancelRemoveDevice(DeviceObject
);
4460 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4462 Status
= IopInitiatePnpIrp(DeviceObject
,
4464 IRP_MN_QUERY_DEVICE_RELATIONS
,
4466 if (!NT_SUCCESS(Status
))
4468 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4469 DeviceRelations
= NULL
;
4473 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4476 if (DeviceRelations
)
4477 IopCancelRemoveDeviceRelations(DeviceRelations
);
4481 IopPrepareDeviceForRemoval(IN PDEVICE_OBJECT DeviceObject
, BOOLEAN Force
)
4483 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(DeviceObject
);
4484 IO_STACK_LOCATION Stack
;
4485 IO_STATUS_BLOCK IoStatusBlock
;
4486 PDEVICE_RELATIONS DeviceRelations
;
4489 if ((DeviceNode
->UserFlags
& DNUF_NOT_DISABLEABLE
) && !Force
)
4491 DPRINT1("Removal not allowed for %wZ\n", &DeviceNode
->InstancePath
);
4492 return STATUS_UNSUCCESSFUL
;
4495 if (!Force
&& IopQueryRemoveDevice(DeviceObject
) != STATUS_SUCCESS
)
4497 DPRINT1("Removal vetoed by failing the query remove request\n");
4499 IopCancelRemoveDevice(DeviceObject
);
4501 return STATUS_UNSUCCESSFUL
;
4504 Stack
.Parameters
.QueryDeviceRelations
.Type
= RemovalRelations
;
4506 Status
= IopInitiatePnpIrp(DeviceObject
,
4508 IRP_MN_QUERY_DEVICE_RELATIONS
,
4510 if (!NT_SUCCESS(Status
))
4512 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4513 DeviceRelations
= NULL
;
4517 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4520 if (DeviceRelations
)
4522 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
, Force
);
4523 if (!NT_SUCCESS(Status
))
4527 Status
= IopQueryRemoveChildDevices(DeviceNode
, Force
);
4528 if (!NT_SUCCESS(Status
))
4530 if (DeviceRelations
)
4531 IopCancelRemoveDeviceRelations(DeviceRelations
);
4535 DeviceNode
->Flags
|= DNF_WILL_BE_REMOVED
;
4536 if (DeviceRelations
)
4537 IopSendRemoveDeviceRelations(DeviceRelations
);
4538 IopSendRemoveChildDevices(DeviceNode
);
4540 return STATUS_SUCCESS
;
4544 IopRemoveDevice(PDEVICE_NODE DeviceNode
)
4548 DPRINT("Removing device: %wZ\n", &DeviceNode
->InstancePath
);
4550 Status
= IopPrepareDeviceForRemoval(DeviceNode
->PhysicalDeviceObject
, FALSE
);
4551 if (NT_SUCCESS(Status
))
4553 IopSendRemoveDevice(DeviceNode
->PhysicalDeviceObject
);
4554 IopQueueTargetDeviceEvent(&GUID_DEVICE_SAFE_REMOVAL
,
4555 &DeviceNode
->InstancePath
);
4556 return STATUS_SUCCESS
;
4567 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject
)
4569 PDEVICE_NODE DeviceNode
= IopGetDeviceNode(PhysicalDeviceObject
);
4570 PDEVICE_RELATIONS DeviceRelations
;
4571 IO_STATUS_BLOCK IoStatusBlock
;
4572 IO_STACK_LOCATION Stack
;
4573 DEVICE_CAPABILITIES Capabilities
;
4576 IopQueueTargetDeviceEvent(&GUID_DEVICE_KERNEL_INITIATED_EJECT
,
4577 &DeviceNode
->InstancePath
);
4579 if (IopQueryDeviceCapabilities(DeviceNode
, &Capabilities
) != STATUS_SUCCESS
)
4584 Stack
.Parameters
.QueryDeviceRelations
.Type
= EjectionRelations
;
4586 Status
= IopInitiatePnpIrp(PhysicalDeviceObject
,
4588 IRP_MN_QUERY_DEVICE_RELATIONS
,
4590 if (!NT_SUCCESS(Status
))
4592 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status
);
4593 DeviceRelations
= NULL
;
4597 DeviceRelations
= (PDEVICE_RELATIONS
)IoStatusBlock
.Information
;
4600 if (DeviceRelations
)
4602 Status
= IopQueryRemoveDeviceRelations(DeviceRelations
, FALSE
);
4603 if (!NT_SUCCESS(Status
))
4607 Status
= IopQueryRemoveChildDevices(DeviceNode
, FALSE
);
4608 if (!NT_SUCCESS(Status
))
4610 if (DeviceRelations
)
4611 IopCancelRemoveDeviceRelations(DeviceRelations
);
4615 if (IopPrepareDeviceForRemoval(PhysicalDeviceObject
, FALSE
) != STATUS_SUCCESS
)
4617 if (DeviceRelations
)
4618 IopCancelRemoveDeviceRelations(DeviceRelations
);
4619 IopCancelRemoveChildDevices(DeviceNode
);
4623 if (DeviceRelations
)
4624 IopSendRemoveDeviceRelations(DeviceRelations
);
4625 IopSendRemoveChildDevices(DeviceNode
);
4627 if (Capabilities
.EjectSupported
)
4629 if (IopSendEject(PhysicalDeviceObject
) != STATUS_SUCCESS
)
4636 DeviceNode
->Flags
|= DNF_DISABLED
;
4639 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT
,
4640 &DeviceNode
->InstancePath
);
4645 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT_VETOED
,
4646 &DeviceNode
->InstancePath
);
4654 IoInvalidateDeviceRelations(
4655 IN PDEVICE_OBJECT DeviceObject
,
4656 IN DEVICE_RELATION_TYPE Type
)
4658 PIO_WORKITEM WorkItem
;
4659 PINVALIDATE_DEVICE_RELATION_DATA Data
;
4661 Data
= ExAllocatePool(NonPagedPool
, sizeof(INVALIDATE_DEVICE_RELATION_DATA
));
4664 WorkItem
= IoAllocateWorkItem(DeviceObject
);
4671 ObReferenceObject(DeviceObject
);
4672 Data
->DeviceObject
= DeviceObject
;
4674 Data
->WorkItem
= WorkItem
;
4678 IopAsynchronousInvalidateDeviceRelations
,
4688 IoSynchronousInvalidateDeviceRelations(
4689 IN PDEVICE_OBJECT DeviceObject
,
4690 IN DEVICE_RELATION_TYPE Type
)
4697 /* Enumerate the device */
4698 return IopEnumerateDevice(DeviceObject
);
4699 case PowerRelations
:
4700 /* Not handled yet */
4701 return STATUS_NOT_IMPLEMENTED
;
4702 case TargetDeviceRelation
:
4704 return STATUS_SUCCESS
;
4706 /* Ejection relations are not supported */
4707 return STATUS_NOT_SUPPORTED
;
4716 IoTranslateBusAddress(IN INTERFACE_TYPE InterfaceType
,
4718 IN PHYSICAL_ADDRESS BusAddress
,
4719 IN OUT PULONG AddressSpace
,
4720 OUT PPHYSICAL_ADDRESS TranslatedAddress
)
4722 /* FIXME: Notify the resource arbiter */
4724 return HalTranslateBusAddress(InterfaceType
,