[PNP]
[reactos.git] / reactos / ntoskrnl / io / pnpmgr / pnpmgr.c
1 /*
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)
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ntoskrnl.h>
13 #define NDEBUG
14 #include <debug.h>
15
16 /* GLOBALS *******************************************************************/
17
18 PDEVICE_NODE IopRootDeviceNode;
19 KSPIN_LOCK IopDeviceTreeLock;
20 ERESOURCE PpRegistryDeviceResource;
21 KGUARDED_MUTEX PpDeviceReferenceTableLock;
22 RTL_AVL_TABLE PpDeviceReferenceTable;
23
24 extern ULONG ExpInitializationPhase;
25 extern BOOLEAN ExpInTextModeSetup;
26 extern BOOLEAN PnpSystemInit;
27
28 /* DATA **********************************************************************/
29
30 PDRIVER_OBJECT IopRootDriverObject;
31 PIO_BUS_TYPE_GUID_LIST PnpBusTypeGuidList = NULL;
32
33 typedef struct _INVALIDATE_DEVICE_RELATION_DATA
34 {
35 PDEVICE_OBJECT DeviceObject;
36 DEVICE_RELATION_TYPE Type;
37 PIO_WORKITEM WorkItem;
38 } INVALIDATE_DEVICE_RELATION_DATA, *PINVALIDATE_DEVICE_RELATION_DATA;
39
40 /* FUNCTIONS *****************************************************************/
41 NTSTATUS
42 NTAPI
43 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath,
44 IN ULONG CreateOptions,
45 OUT PHANDLE Handle);
46
47 VOID
48 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject);
49
50 NTSTATUS
51 IopPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject);
52
53 PDEVICE_NODE
54 FASTCALL
55 IopGetDeviceNode(PDEVICE_OBJECT DeviceObject)
56 {
57 return ((PEXTENDED_DEVOBJ_EXTENSION)DeviceObject->DeviceObjectExtension)->DeviceNode;
58 }
59
60 NTSTATUS
61 FASTCALL
62 IopInitializeDevice(PDEVICE_NODE DeviceNode,
63 PDRIVER_OBJECT DriverObject)
64 {
65 PDEVICE_OBJECT Fdo;
66 NTSTATUS Status;
67
68 if (!DriverObject)
69 {
70 /* Special case for bus driven devices */
71 DeviceNode->Flags |= DNF_ADDED;
72 return STATUS_SUCCESS;
73 }
74
75 if (!DriverObject->DriverExtension->AddDevice)
76 {
77 DeviceNode->Flags |= DNF_LEGACY_DRIVER;
78 }
79
80 if (DeviceNode->Flags & DNF_LEGACY_DRIVER)
81 {
82 DeviceNode->Flags |= DNF_ADDED + DNF_STARTED;
83 return STATUS_SUCCESS;
84 }
85
86 /* This is a Plug and Play driver */
87 DPRINT("Plug and Play driver found\n");
88 ASSERT(DeviceNode->PhysicalDeviceObject);
89
90 DPRINT("Calling %wZ->AddDevice(%wZ)\n",
91 &DriverObject->DriverName,
92 &DeviceNode->InstancePath);
93 Status = DriverObject->DriverExtension->AddDevice(
94 DriverObject, DeviceNode->PhysicalDeviceObject);
95 if (!NT_SUCCESS(Status))
96 {
97 DPRINT1("%wZ->AddDevice(%wZ) failed with status 0x%x\n",
98 &DriverObject->DriverName,
99 &DeviceNode->InstancePath,
100 Status);
101 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
102 return Status;
103 }
104
105 /* Check if driver added a FDO above the PDO */
106 Fdo = IoGetAttachedDeviceReference(DeviceNode->PhysicalDeviceObject);
107 if (Fdo == DeviceNode->PhysicalDeviceObject)
108 {
109 /* FIXME: What do we do? Unload the driver or just disable the device? */
110 DPRINT1("An FDO was not attached\n");
111 ObDereferenceObject(Fdo);
112 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
113 return STATUS_UNSUCCESSFUL;
114 }
115
116 /* Check if we have a ACPI device (needed for power management) */
117 if (Fdo->DeviceType == FILE_DEVICE_ACPI)
118 {
119 static BOOLEAN SystemPowerDeviceNodeCreated = FALSE;
120
121 /* There can be only one system power device */
122 if (!SystemPowerDeviceNodeCreated)
123 {
124 PopSystemPowerDeviceNode = DeviceNode;
125 ObReferenceObject(PopSystemPowerDeviceNode->PhysicalDeviceObject);
126 SystemPowerDeviceNodeCreated = TRUE;
127 }
128 }
129
130 ObDereferenceObject(Fdo);
131
132 IopDeviceNodeSetFlag(DeviceNode, DNF_ADDED);
133
134 return STATUS_SUCCESS;
135 }
136
137 static
138 NTSTATUS
139 NTAPI
140 IopSendEject(IN PDEVICE_OBJECT DeviceObject)
141 {
142 IO_STACK_LOCATION Stack;
143 PVOID Dummy;
144
145 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
146 Stack.MajorFunction = IRP_MJ_PNP;
147 Stack.MinorFunction = IRP_MN_EJECT;
148
149 return IopSynchronousCall(DeviceObject, &Stack, &Dummy);
150 }
151
152 static
153 VOID
154 NTAPI
155 IopSendSurpriseRemoval(IN PDEVICE_OBJECT DeviceObject)
156 {
157 IO_STACK_LOCATION Stack;
158 PVOID Dummy;
159
160 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
161 Stack.MajorFunction = IRP_MJ_PNP;
162 Stack.MinorFunction = IRP_MN_SURPRISE_REMOVAL;
163
164 /* Drivers should never fail a IRP_MN_SURPRISE_REMOVAL request */
165 IopSynchronousCall(DeviceObject, &Stack, &Dummy);
166 }
167
168 static
169 NTSTATUS
170 NTAPI
171 IopQueryRemoveDevice(IN PDEVICE_OBJECT DeviceObject)
172 {
173 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
174 IO_STACK_LOCATION Stack;
175 PVOID Dummy;
176 NTSTATUS Status;
177
178 ASSERT(DeviceNode);
179
180 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVE_PENDING,
181 &DeviceNode->InstancePath);
182
183 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
184 Stack.MajorFunction = IRP_MJ_PNP;
185 Stack.MinorFunction = IRP_MN_QUERY_REMOVE_DEVICE;
186
187 Status = IopSynchronousCall(DeviceObject, &Stack, &Dummy);
188
189 IopNotifyPlugPlayNotification(DeviceObject,
190 EventCategoryTargetDeviceChange,
191 &GUID_TARGET_DEVICE_QUERY_REMOVE,
192 NULL,
193 NULL);
194
195 if (!NT_SUCCESS(Status))
196 {
197 DPRINT1("Removal vetoed by %wZ\n", &DeviceNode->InstancePath);
198 IopQueueTargetDeviceEvent(&GUID_DEVICE_REMOVAL_VETOED,
199 &DeviceNode->InstancePath);
200 }
201
202 return Status;
203 }
204
205 static
206 NTSTATUS
207 NTAPI
208 IopQueryStopDevice(IN PDEVICE_OBJECT DeviceObject)
209 {
210 IO_STACK_LOCATION Stack;
211 PVOID Dummy;
212
213 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
214 Stack.MajorFunction = IRP_MJ_PNP;
215 Stack.MinorFunction = IRP_MN_QUERY_STOP_DEVICE;
216
217 return IopSynchronousCall(DeviceObject, &Stack, &Dummy);
218 }
219
220 static
221 VOID
222 NTAPI
223 IopSendRemoveDevice(IN PDEVICE_OBJECT DeviceObject)
224 {
225 IO_STACK_LOCATION Stack;
226 PVOID Dummy;
227
228 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
229 Stack.MajorFunction = IRP_MJ_PNP;
230 Stack.MinorFunction = IRP_MN_REMOVE_DEVICE;
231
232 /* Drivers should never fail a IRP_MN_REMOVE_DEVICE request */
233 IopSynchronousCall(DeviceObject, &Stack, &Dummy);
234
235 IopNotifyPlugPlayNotification(DeviceObject,
236 EventCategoryTargetDeviceChange,
237 &GUID_TARGET_DEVICE_REMOVE_COMPLETE,
238 NULL,
239 NULL);
240 }
241
242 static
243 VOID
244 NTAPI
245 IopCancelRemoveDevice(IN PDEVICE_OBJECT DeviceObject)
246 {
247 IO_STACK_LOCATION Stack;
248 PVOID Dummy;
249
250 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
251 Stack.MajorFunction = IRP_MJ_PNP;
252 Stack.MinorFunction = IRP_MN_CANCEL_REMOVE_DEVICE;
253
254 /* Drivers should never fail a IRP_MN_CANCEL_REMOVE_DEVICE request */
255 IopSynchronousCall(DeviceObject, &Stack, &Dummy);
256
257 IopNotifyPlugPlayNotification(DeviceObject,
258 EventCategoryTargetDeviceChange,
259 &GUID_TARGET_DEVICE_REMOVE_CANCELLED,
260 NULL,
261 NULL);
262 }
263
264 static
265 VOID
266 NTAPI
267 IopSendStopDevice(IN PDEVICE_OBJECT DeviceObject)
268 {
269 IO_STACK_LOCATION Stack;
270 PVOID Dummy;
271
272 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
273 Stack.MajorFunction = IRP_MJ_PNP;
274 Stack.MinorFunction = IRP_MN_STOP_DEVICE;
275
276 /* Drivers should never fail a IRP_MN_STOP_DEVICE request */
277 IopSynchronousCall(DeviceObject, &Stack, &Dummy);
278 }
279
280 VOID
281 NTAPI
282 IopStartDevice2(IN PDEVICE_OBJECT DeviceObject)
283 {
284 IO_STACK_LOCATION Stack;
285 PDEVICE_NODE DeviceNode;
286 NTSTATUS Status;
287 PVOID Dummy;
288 DEVICE_CAPABILITIES DeviceCapabilities;
289
290 /* Get the device node */
291 DeviceNode = IopGetDeviceNode(DeviceObject);
292
293 ASSERT(!(DeviceNode->Flags & DNF_DISABLED));
294
295 /* Build the I/O stack locaiton */
296 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
297 Stack.MajorFunction = IRP_MJ_PNP;
298 Stack.MinorFunction = IRP_MN_START_DEVICE;
299
300 Stack.Parameters.StartDevice.AllocatedResources =
301 DeviceNode->ResourceList;
302 Stack.Parameters.StartDevice.AllocatedResourcesTranslated =
303 DeviceNode->ResourceListTranslated;
304
305 /* Do the call */
306 Status = IopSynchronousCall(DeviceObject, &Stack, &Dummy);
307 if (!NT_SUCCESS(Status))
308 {
309 /* Send an IRP_MN_REMOVE_DEVICE request */
310 IopRemoveDevice(DeviceNode);
311
312 /* Set the appropriate flag */
313 DeviceNode->Flags |= DNF_START_FAILED;
314
315 DPRINT1("Warning: PnP Start failed (%wZ) [Status: 0x%x]\n", &DeviceNode->InstancePath, Status);
316 return;
317 }
318
319 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after start)\n");
320
321 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
322 if (!NT_SUCCESS(Status))
323 {
324 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status);
325 }
326
327 /* Invalidate device state so IRP_MN_QUERY_PNP_DEVICE_STATE is sent */
328 IoInvalidateDeviceState(DeviceObject);
329
330 /* Otherwise, mark us as started */
331 DeviceNode->Flags |= DNF_STARTED;
332 DeviceNode->Flags &= ~DNF_STOPPED;
333
334 /* We now need enumeration */
335 DeviceNode->Flags |= DNF_NEED_ENUMERATION_ONLY;
336 }
337
338 NTSTATUS
339 NTAPI
340 IopStartAndEnumerateDevice(IN PDEVICE_NODE DeviceNode)
341 {
342 PDEVICE_OBJECT DeviceObject;
343 NTSTATUS Status;
344 PAGED_CODE();
345
346 /* Sanity check */
347 ASSERT((DeviceNode->Flags & DNF_ADDED));
348 ASSERT((DeviceNode->Flags & (DNF_RESOURCE_ASSIGNED |
349 DNF_RESOURCE_REPORTED |
350 DNF_NO_RESOURCE_REQUIRED)));
351
352 /* Get the device object */
353 DeviceObject = DeviceNode->PhysicalDeviceObject;
354
355 /* Check if we're not started yet */
356 if (!(DeviceNode->Flags & DNF_STARTED))
357 {
358 /* Start us */
359 IopStartDevice2(DeviceObject);
360 }
361
362 /* Do we need to query IDs? This happens in the case of manual reporting */
363 #if 0
364 if (DeviceNode->Flags & DNF_NEED_QUERY_IDS)
365 {
366 DPRINT1("Warning: Device node has DNF_NEED_QUERY_IDS\n");
367 /* And that case shouldn't happen yet */
368 ASSERT(FALSE);
369 }
370 #endif
371
372 /* Make sure we're started, and check if we need enumeration */
373 if ((DeviceNode->Flags & DNF_STARTED) &&
374 (DeviceNode->Flags & DNF_NEED_ENUMERATION_ONLY))
375 {
376 /* Enumerate us */
377 IoSynchronousInvalidateDeviceRelations(DeviceObject, BusRelations);
378 Status = STATUS_SUCCESS;
379 }
380 else
381 {
382 /* Nothing to do */
383 Status = STATUS_SUCCESS;
384 }
385
386 /* Return */
387 return Status;
388 }
389
390 NTSTATUS
391 IopStopDevice(
392 PDEVICE_NODE DeviceNode)
393 {
394 NTSTATUS Status;
395
396 DPRINT("Stopping device: %wZ\n", &DeviceNode->InstancePath);
397
398 Status = IopQueryStopDevice(DeviceNode->PhysicalDeviceObject);
399 if (NT_SUCCESS(Status))
400 {
401 IopSendStopDevice(DeviceNode->PhysicalDeviceObject);
402
403 DeviceNode->Flags &= ~(DNF_STARTED | DNF_START_REQUEST_PENDING);
404 DeviceNode->Flags |= DNF_STOPPED;
405
406 return STATUS_SUCCESS;
407 }
408
409 return Status;
410 }
411
412 NTSTATUS
413 IopStartDevice(
414 PDEVICE_NODE DeviceNode)
415 {
416 NTSTATUS Status;
417 HANDLE InstanceHandle = INVALID_HANDLE_VALUE, ControlHandle = INVALID_HANDLE_VALUE;
418 UNICODE_STRING KeyName;
419 OBJECT_ATTRIBUTES ObjectAttributes;
420
421 if (DeviceNode->Flags & DNF_DISABLED)
422 return STATUS_SUCCESS;
423
424 Status = IopAssignDeviceResources(DeviceNode);
425 if (!NT_SUCCESS(Status))
426 goto ByeBye;
427
428 /* New PnP ABI */
429 IopStartAndEnumerateDevice(DeviceNode);
430
431 /* FIX: Should be done in new device instance code */
432 Status = IopCreateDeviceKeyPath(&DeviceNode->InstancePath, 0, &InstanceHandle);
433 if (!NT_SUCCESS(Status))
434 goto ByeBye;
435
436 /* FIX: Should be done in IoXxxPrepareDriverLoading */
437 // {
438 RtlInitUnicodeString(&KeyName, L"Control");
439 InitializeObjectAttributes(&ObjectAttributes,
440 &KeyName,
441 OBJ_CASE_INSENSITIVE,
442 InstanceHandle,
443 NULL);
444 Status = ZwCreateKey(&ControlHandle, KEY_SET_VALUE, &ObjectAttributes, 0, NULL, REG_OPTION_VOLATILE, NULL);
445 if (!NT_SUCCESS(Status))
446 goto ByeBye;
447
448 RtlInitUnicodeString(&KeyName, L"ActiveService");
449 Status = ZwSetValueKey(ControlHandle, &KeyName, 0, REG_SZ, DeviceNode->ServiceName.Buffer, DeviceNode->ServiceName.Length);
450 // }
451
452 ByeBye:
453 if (ControlHandle != INVALID_HANDLE_VALUE)
454 ZwClose(ControlHandle);
455
456 if (InstanceHandle != INVALID_HANDLE_VALUE)
457 ZwClose(InstanceHandle);
458
459 return Status;
460 }
461
462 NTSTATUS
463 NTAPI
464 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode,
465 PDEVICE_CAPABILITIES DeviceCaps)
466 {
467 IO_STATUS_BLOCK StatusBlock;
468 IO_STACK_LOCATION Stack;
469 NTSTATUS Status;
470 HANDLE InstanceKey;
471 UNICODE_STRING ValueName;
472
473 /* Set up the Header */
474 RtlZeroMemory(DeviceCaps, sizeof(DEVICE_CAPABILITIES));
475 DeviceCaps->Size = sizeof(DEVICE_CAPABILITIES);
476 DeviceCaps->Version = 1;
477 DeviceCaps->Address = -1;
478 DeviceCaps->UINumber = -1;
479
480 /* Set up the Stack */
481 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
482 Stack.Parameters.DeviceCapabilities.Capabilities = DeviceCaps;
483
484 /* Send the IRP */
485 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
486 &StatusBlock,
487 IRP_MN_QUERY_CAPABILITIES,
488 &Stack);
489 if (!NT_SUCCESS(Status))
490 {
491 DPRINT1("IRP_MN_QUERY_CAPABILITIES failed with status 0x%x\n", Status);
492 return Status;
493 }
494
495 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCaps->Version + sizeof(DeviceCaps->Version));
496
497 if (DeviceCaps->NoDisplayInUI)
498 DeviceNode->UserFlags |= DNUF_DONT_SHOW_IN_UI;
499 else
500 DeviceNode->UserFlags &= ~DNUF_DONT_SHOW_IN_UI;
501
502 Status = IopCreateDeviceKeyPath(&DeviceNode->InstancePath, 0, &InstanceKey);
503 if (NT_SUCCESS(Status))
504 {
505 /* Set 'Capabilities' value */
506 RtlInitUnicodeString(&ValueName, L"Capabilities");
507 Status = ZwSetValueKey(InstanceKey,
508 &ValueName,
509 0,
510 REG_DWORD,
511 (PVOID)&DeviceNode->CapabilityFlags,
512 sizeof(ULONG));
513
514 /* Set 'UINumber' value */
515 if (DeviceCaps->UINumber != MAXULONG)
516 {
517 RtlInitUnicodeString(&ValueName, L"UINumber");
518 Status = ZwSetValueKey(InstanceKey,
519 &ValueName,
520 0,
521 REG_DWORD,
522 &DeviceCaps->UINumber,
523 sizeof(ULONG));
524 }
525 }
526
527 return Status;
528 }
529
530 static VOID NTAPI
531 IopAsynchronousInvalidateDeviceRelations(
532 IN PDEVICE_OBJECT DeviceObject,
533 IN PVOID InvalidateContext)
534 {
535 PINVALIDATE_DEVICE_RELATION_DATA Data = InvalidateContext;
536
537 IoSynchronousInvalidateDeviceRelations(
538 Data->DeviceObject,
539 Data->Type);
540
541 ObDereferenceObject(Data->DeviceObject);
542 IoFreeWorkItem(Data->WorkItem);
543 ExFreePool(Data);
544 }
545
546 NTSTATUS
547 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
548 {
549 KIRQL OldIrql;
550
551 if (PopSystemPowerDeviceNode)
552 {
553 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
554 *DeviceObject = PopSystemPowerDeviceNode->PhysicalDeviceObject;
555 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
556
557 return STATUS_SUCCESS;
558 }
559
560 return STATUS_UNSUCCESSFUL;
561 }
562
563 USHORT
564 NTAPI
565 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid)
566 {
567 USHORT i = 0, FoundIndex = 0xFFFF;
568 ULONG NewSize;
569 PVOID NewList;
570
571 /* Acquire the lock */
572 ExAcquireFastMutex(&PnpBusTypeGuidList->Lock);
573
574 /* Loop all entries */
575 while (i < PnpBusTypeGuidList->GuidCount)
576 {
577 /* Try to find a match */
578 if (RtlCompareMemory(BusTypeGuid,
579 &PnpBusTypeGuidList->Guids[i],
580 sizeof(GUID)) == sizeof(GUID))
581 {
582 /* Found it */
583 FoundIndex = i;
584 goto Quickie;
585 }
586 i++;
587 }
588
589 /* Check if we have to grow the list */
590 if (PnpBusTypeGuidList->GuidCount)
591 {
592 /* Calculate the new size */
593 NewSize = sizeof(IO_BUS_TYPE_GUID_LIST) +
594 (sizeof(GUID) * PnpBusTypeGuidList->GuidCount);
595
596 /* Allocate the new copy */
597 NewList = ExAllocatePool(PagedPool, NewSize);
598
599 if (!NewList) {
600 /* Fail */
601 ExFreePool(PnpBusTypeGuidList);
602 goto Quickie;
603 }
604
605 /* Now copy them, decrease the size too */
606 NewSize -= sizeof(GUID);
607 RtlCopyMemory(NewList, PnpBusTypeGuidList, NewSize);
608
609 /* Free the old list */
610 ExFreePool(PnpBusTypeGuidList);
611
612 /* Use the new buffer */
613 PnpBusTypeGuidList = NewList;
614 }
615
616 /* Copy the new GUID */
617 RtlCopyMemory(&PnpBusTypeGuidList->Guids[PnpBusTypeGuidList->GuidCount],
618 BusTypeGuid,
619 sizeof(GUID));
620
621 /* The new entry is the index */
622 FoundIndex = (USHORT)PnpBusTypeGuidList->GuidCount;
623 PnpBusTypeGuidList->GuidCount++;
624
625 Quickie:
626 ExReleaseFastMutex(&PnpBusTypeGuidList->Lock);
627 return FoundIndex;
628 }
629
630 /*
631 * DESCRIPTION
632 * Creates a device node
633 *
634 * ARGUMENTS
635 * ParentNode = Pointer to parent device node
636 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
637 * to have the root device node create one
638 * (eg. for legacy drivers)
639 * DeviceNode = Pointer to storage for created device node
640 *
641 * RETURN VALUE
642 * Status
643 */
644 NTSTATUS
645 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
646 PDEVICE_OBJECT PhysicalDeviceObject,
647 PUNICODE_STRING ServiceName,
648 PDEVICE_NODE *DeviceNode)
649 {
650 PDEVICE_NODE Node;
651 NTSTATUS Status;
652 KIRQL OldIrql;
653 UNICODE_STRING FullServiceName;
654 UNICODE_STRING LegacyPrefix = RTL_CONSTANT_STRING(L"LEGACY_");
655 UNICODE_STRING UnknownDeviceName = RTL_CONSTANT_STRING(L"UNKNOWN");
656 UNICODE_STRING KeyName, ClassName;
657 PUNICODE_STRING ServiceName1;
658 ULONG LegacyValue;
659 #if 0
660 UNICODE_STRING ClassGUID;
661 #endif
662 HANDLE InstanceHandle;
663
664 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p ServiceName %wZ\n",
665 ParentNode, PhysicalDeviceObject, ServiceName);
666
667 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
668 if (!Node)
669 {
670 return STATUS_INSUFFICIENT_RESOURCES;
671 }
672
673 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
674
675 if (!ServiceName)
676 ServiceName1 = &UnknownDeviceName;
677 else
678 ServiceName1 = ServiceName;
679
680 if (!PhysicalDeviceObject)
681 {
682 FullServiceName.MaximumLength = LegacyPrefix.Length + ServiceName1->Length;
683 FullServiceName.Length = 0;
684 FullServiceName.Buffer = ExAllocatePool(PagedPool, FullServiceName.MaximumLength);
685 if (!FullServiceName.Buffer)
686 {
687 ExFreePool(Node);
688 return STATUS_INSUFFICIENT_RESOURCES;
689 }
690
691 RtlAppendUnicodeStringToString(&FullServiceName, &LegacyPrefix);
692 RtlAppendUnicodeStringToString(&FullServiceName, ServiceName1);
693
694 Status = PnpRootCreateDevice(&FullServiceName, NULL, &PhysicalDeviceObject, &Node->InstancePath);
695 if (!NT_SUCCESS(Status))
696 {
697 DPRINT1("PnpRootCreateDevice() failed with status 0x%08X\n", Status);
698 ExFreePool(Node);
699 return Status;
700 }
701
702 /* Create the device key for legacy drivers */
703 Status = IopCreateDeviceKeyPath(&Node->InstancePath, REG_OPTION_VOLATILE, &InstanceHandle);
704 if (!NT_SUCCESS(Status))
705 {
706 ZwClose(InstanceHandle);
707 ExFreePool(Node);
708 ExFreePool(FullServiceName.Buffer);
709 return Status;
710 }
711
712 Node->ServiceName.Buffer = ExAllocatePool(PagedPool, ServiceName1->Length);
713 if (!Node->ServiceName.Buffer)
714 {
715 ZwClose(InstanceHandle);
716 ExFreePool(Node);
717 ExFreePool(FullServiceName.Buffer);
718 return Status;
719 }
720
721 Node->ServiceName.MaximumLength = ServiceName1->Length;
722 Node->ServiceName.Length = 0;
723
724 RtlAppendUnicodeStringToString(&Node->ServiceName, ServiceName1);
725
726 if (ServiceName)
727 {
728 RtlInitUnicodeString(&KeyName, L"Service");
729 Status = ZwSetValueKey(InstanceHandle, &KeyName, 0, REG_SZ, ServiceName->Buffer, ServiceName->Length);
730 }
731
732 if (NT_SUCCESS(Status))
733 {
734 RtlInitUnicodeString(&KeyName, L"Legacy");
735
736 LegacyValue = 1;
737 Status = ZwSetValueKey(InstanceHandle, &KeyName, 0, REG_DWORD, &LegacyValue, sizeof(LegacyValue));
738 if (NT_SUCCESS(Status))
739 {
740 RtlInitUnicodeString(&KeyName, L"Class");
741
742 RtlInitUnicodeString(&ClassName, L"LegacyDriver");
743 Status = ZwSetValueKey(InstanceHandle, &KeyName, 0, REG_SZ, ClassName.Buffer, ClassName.Length);
744 #if 0
745 if (NT_SUCCESS(Status))
746 {
747 RtlInitUnicodeString(&KeyName, L"ClassGUID");
748
749 RtlInitUnicodeString(&ClassGUID, L"{8ECC055D-047F-11D1-A537-0000F8753ED1}");
750 Status = ZwSetValueKey(InstanceHandle, &KeyName, 0, REG_SZ, ClassGUID.Buffer, ClassGUID.Length);
751 }
752 #endif
753 }
754 }
755
756 ZwClose(InstanceHandle);
757 ExFreePool(FullServiceName.Buffer);
758
759 if (!NT_SUCCESS(Status))
760 {
761 ExFreePool(Node);
762 return Status;
763 }
764
765 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
766 IopDeviceNodeSetFlag(Node, DNF_PROCESSED);
767 IopDeviceNodeSetFlag(Node, DNF_ADDED);
768 IopDeviceNodeSetFlag(Node, DNF_STARTED);
769 }
770
771 Node->PhysicalDeviceObject = PhysicalDeviceObject;
772
773 ((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode = Node;
774
775 if (ParentNode)
776 {
777 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
778 Node->Parent = ParentNode;
779 Node->Sibling = ParentNode->Child;
780 ParentNode->Child = Node;
781 if (ParentNode->LastChild == NULL)
782 ParentNode->LastChild = Node;
783 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
784 Node->Level = ParentNode->Level + 1;
785 }
786
787 PhysicalDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
788
789 *DeviceNode = Node;
790
791 return STATUS_SUCCESS;
792 }
793
794 NTSTATUS
795 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
796 {
797 KIRQL OldIrql;
798 PDEVICE_NODE PrevSibling = NULL;
799
800 /* All children must be deleted before a parent is deleted */
801 ASSERT(!DeviceNode->Child);
802
803 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
804
805 ASSERT(DeviceNode->PhysicalDeviceObject);
806
807 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
808
809 /* Get previous sibling */
810 if (DeviceNode->Parent && DeviceNode->Parent->Child != DeviceNode)
811 {
812 PrevSibling = DeviceNode->Parent->Child;
813 while (PrevSibling->Sibling != DeviceNode)
814 PrevSibling = PrevSibling->Sibling;
815 }
816
817 /* Unlink from parent if it exists */
818 if (DeviceNode->Parent)
819 {
820 if (DeviceNode->Parent->LastChild == DeviceNode)
821 {
822 DeviceNode->Parent->LastChild = PrevSibling;
823 if (PrevSibling)
824 PrevSibling->Sibling = NULL;
825 }
826 if (DeviceNode->Parent->Child == DeviceNode)
827 DeviceNode->Parent->Child = DeviceNode->Sibling;
828 }
829
830 /* Unlink from sibling list */
831 if (PrevSibling)
832 PrevSibling->Sibling = DeviceNode->Sibling;
833
834 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
835
836 RtlFreeUnicodeString(&DeviceNode->InstancePath);
837
838 RtlFreeUnicodeString(&DeviceNode->ServiceName);
839
840 if (DeviceNode->ResourceList)
841 {
842 ExFreePool(DeviceNode->ResourceList);
843 }
844
845 if (DeviceNode->ResourceListTranslated)
846 {
847 ExFreePool(DeviceNode->ResourceListTranslated);
848 }
849
850 if (DeviceNode->ResourceRequirements)
851 {
852 ExFreePool(DeviceNode->ResourceRequirements);
853 }
854
855 if (DeviceNode->BootResources)
856 {
857 ExFreePool(DeviceNode->BootResources);
858 }
859
860 ExFreePool(DeviceNode);
861
862 return STATUS_SUCCESS;
863 }
864
865 NTSTATUS
866 NTAPI
867 IopSynchronousCall(IN PDEVICE_OBJECT DeviceObject,
868 IN PIO_STACK_LOCATION IoStackLocation,
869 OUT PVOID *Information)
870 {
871 PIRP Irp;
872 PIO_STACK_LOCATION IrpStack;
873 IO_STATUS_BLOCK IoStatusBlock;
874 KEVENT Event;
875 NTSTATUS Status;
876 PDEVICE_OBJECT TopDeviceObject;
877 PAGED_CODE();
878
879 /* Call the top of the device stack */
880 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
881
882 /* Allocate an IRP */
883 Irp = IoAllocateIrp(TopDeviceObject->StackSize, FALSE);
884 if (!Irp) return STATUS_INSUFFICIENT_RESOURCES;
885
886 /* Initialize to failure */
887 Irp->IoStatus.Status = IoStatusBlock.Status = STATUS_NOT_SUPPORTED;
888 Irp->IoStatus.Information = IoStatusBlock.Information = 0;
889
890 /* Special case for IRP_MN_FILTER_RESOURCE_REQUIREMENTS */
891 if (IoStackLocation->MinorFunction == IRP_MN_FILTER_RESOURCE_REQUIREMENTS)
892 {
893 /* Copy the resource requirements list into the IOSB */
894 Irp->IoStatus.Information =
895 IoStatusBlock.Information = (ULONG_PTR)IoStackLocation->Parameters.FilterResourceRequirements.IoResourceRequirementList;
896 }
897
898 /* Initialize the event */
899 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
900
901 /* Set them up */
902 Irp->UserIosb = &IoStatusBlock;
903 Irp->UserEvent = &Event;
904
905 /* Queue the IRP */
906 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
907 IoQueueThreadIrp(Irp);
908
909 /* Copy-in the stack */
910 IrpStack = IoGetNextIrpStackLocation(Irp);
911 *IrpStack = *IoStackLocation;
912
913 /* Call the driver */
914 Status = IoCallDriver(TopDeviceObject, Irp);
915 if (Status == STATUS_PENDING)
916 {
917 /* Wait for it */
918 KeWaitForSingleObject(&Event,
919 Executive,
920 KernelMode,
921 FALSE,
922 NULL);
923 Status = IoStatusBlock.Status;
924 }
925
926 /* Return the information */
927 *Information = (PVOID)IoStatusBlock.Information;
928 return Status;
929 }
930
931 NTSTATUS
932 NTAPI
933 IopInitiatePnpIrp(IN PDEVICE_OBJECT DeviceObject,
934 IN OUT PIO_STATUS_BLOCK IoStatusBlock,
935 IN UCHAR MinorFunction,
936 IN PIO_STACK_LOCATION Stack OPTIONAL)
937 {
938 IO_STACK_LOCATION IoStackLocation;
939
940 /* Fill out the stack information */
941 RtlZeroMemory(&IoStackLocation, sizeof(IO_STACK_LOCATION));
942 IoStackLocation.MajorFunction = IRP_MJ_PNP;
943 IoStackLocation.MinorFunction = MinorFunction;
944 if (Stack)
945 {
946 /* Copy the rest */
947 RtlCopyMemory(&IoStackLocation.Parameters,
948 &Stack->Parameters,
949 sizeof(Stack->Parameters));
950 }
951
952 /* Do the PnP call */
953 IoStatusBlock->Status = IopSynchronousCall(DeviceObject,
954 &IoStackLocation,
955 (PVOID)&IoStatusBlock->Information);
956 return IoStatusBlock->Status;
957 }
958
959 NTSTATUS
960 IopTraverseDeviceTreeNode(PDEVICETREE_TRAVERSE_CONTEXT Context)
961 {
962 PDEVICE_NODE ParentDeviceNode;
963 PDEVICE_NODE ChildDeviceNode;
964 NTSTATUS Status;
965
966 /* Copy context data so we don't overwrite it in subsequent calls to this function */
967 ParentDeviceNode = Context->DeviceNode;
968
969 /* Call the action routine */
970 Status = (Context->Action)(ParentDeviceNode, Context->Context);
971 if (!NT_SUCCESS(Status))
972 {
973 return Status;
974 }
975
976 /* Traversal of all children nodes */
977 for (ChildDeviceNode = ParentDeviceNode->Child;
978 ChildDeviceNode != NULL;
979 ChildDeviceNode = ChildDeviceNode->Sibling)
980 {
981 /* Pass the current device node to the action routine */
982 Context->DeviceNode = ChildDeviceNode;
983
984 Status = IopTraverseDeviceTreeNode(Context);
985 if (!NT_SUCCESS(Status))
986 {
987 return Status;
988 }
989 }
990
991 return Status;
992 }
993
994
995 NTSTATUS
996 IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context)
997 {
998 NTSTATUS Status;
999
1000 DPRINT("Context 0x%p\n", Context);
1001
1002 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
1003 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
1004
1005 /* Start from the specified device node */
1006 Context->DeviceNode = Context->FirstDeviceNode;
1007
1008 /* Recursively traverse the device tree */
1009 Status = IopTraverseDeviceTreeNode(Context);
1010 if (Status == STATUS_UNSUCCESSFUL)
1011 {
1012 /* The action routine just wanted to terminate the traversal with status
1013 code STATUS_SUCCESS */
1014 Status = STATUS_SUCCESS;
1015 }
1016
1017 return Status;
1018 }
1019
1020
1021 /*
1022 * IopCreateDeviceKeyPath
1023 *
1024 * Creates a registry key
1025 *
1026 * Parameters
1027 * RegistryPath
1028 * Name of the key to be created.
1029 * Handle
1030 * Handle to the newly created key
1031 *
1032 * Remarks
1033 * This method can create nested trees, so parent of RegistryPath can
1034 * be not existant, and will be created if needed.
1035 */
1036 NTSTATUS
1037 NTAPI
1038 IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath,
1039 IN ULONG CreateOptions,
1040 OUT PHANDLE Handle)
1041 {
1042 UNICODE_STRING EnumU = RTL_CONSTANT_STRING(ENUM_ROOT);
1043 HANDLE hParent = NULL, hKey;
1044 OBJECT_ATTRIBUTES ObjectAttributes;
1045 UNICODE_STRING KeyName;
1046 LPCWSTR Current, Last;
1047 USHORT Length;
1048 NTSTATUS Status;
1049
1050 /* Assume failure */
1051 *Handle = NULL;
1052
1053 /* Create a volatile device tree in 1st stage so we have a clean slate
1054 * for enumeration using the correct HAL (chosen in 1st stage setup) */
1055 if (ExpInTextModeSetup) CreateOptions |= REG_OPTION_VOLATILE;
1056
1057 /* Open root key for device instances */
1058 Status = IopOpenRegistryKeyEx(&hParent, NULL, &EnumU, KEY_CREATE_SUB_KEY);
1059 if (!NT_SUCCESS(Status))
1060 {
1061 DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU, Status);
1062 return Status;
1063 }
1064
1065 Current = KeyName.Buffer = RegistryPath->Buffer;
1066 Last = &RegistryPath->Buffer[RegistryPath->Length / sizeof(WCHAR)];
1067
1068 /* Go up to the end of the string */
1069 while (Current <= Last)
1070 {
1071 if (Current != Last && *Current != '\\')
1072 {
1073 /* Not the end of the string and not a separator */
1074 Current++;
1075 continue;
1076 }
1077
1078 /* Prepare relative key name */
1079 Length = (USHORT)((ULONG_PTR)Current - (ULONG_PTR)KeyName.Buffer);
1080 KeyName.MaximumLength = KeyName.Length = Length;
1081 DPRINT("Create '%wZ'\n", &KeyName);
1082
1083 /* Open key */
1084 InitializeObjectAttributes(&ObjectAttributes,
1085 &KeyName,
1086 OBJ_CASE_INSENSITIVE,
1087 hParent,
1088 NULL);
1089 Status = ZwCreateKey(&hKey,
1090 Current == Last ? KEY_ALL_ACCESS : KEY_CREATE_SUB_KEY,
1091 &ObjectAttributes,
1092 0,
1093 NULL,
1094 CreateOptions,
1095 NULL);
1096
1097 /* Close parent key handle, we don't need it anymore */
1098 if (hParent)
1099 ZwClose(hParent);
1100
1101 /* Key opening/creating failed? */
1102 if (!NT_SUCCESS(Status))
1103 {
1104 DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName, Status);
1105 return Status;
1106 }
1107
1108 /* Check if it is the end of the string */
1109 if (Current == Last)
1110 {
1111 /* Yes, return success */
1112 *Handle = hKey;
1113 return STATUS_SUCCESS;
1114 }
1115
1116 /* Start with this new parent key */
1117 hParent = hKey;
1118 Current++;
1119 KeyName.Buffer = (LPWSTR)Current;
1120 }
1121
1122 return STATUS_UNSUCCESSFUL;
1123 }
1124
1125 NTSTATUS
1126 IopSetDeviceInstanceData(HANDLE InstanceKey,
1127 PDEVICE_NODE DeviceNode)
1128 {
1129 OBJECT_ATTRIBUTES ObjectAttributes;
1130 UNICODE_STRING KeyName;
1131 HANDLE LogConfKey;
1132 ULONG ResCount;
1133 ULONG ResultLength;
1134 NTSTATUS Status;
1135 HANDLE ControlHandle;
1136
1137 DPRINT("IopSetDeviceInstanceData() called\n");
1138
1139 /* Create the 'LogConf' key */
1140 RtlInitUnicodeString(&KeyName, L"LogConf");
1141 InitializeObjectAttributes(&ObjectAttributes,
1142 &KeyName,
1143 OBJ_CASE_INSENSITIVE,
1144 InstanceKey,
1145 NULL);
1146 Status = ZwCreateKey(&LogConfKey,
1147 KEY_ALL_ACCESS,
1148 &ObjectAttributes,
1149 0,
1150 NULL,
1151 REG_OPTION_VOLATILE,
1152 NULL);
1153 if (NT_SUCCESS(Status))
1154 {
1155 /* Set 'BootConfig' value */
1156 if (DeviceNode->BootResources != NULL)
1157 {
1158 ResCount = DeviceNode->BootResources->Count;
1159 if (ResCount != 0)
1160 {
1161 RtlInitUnicodeString(&KeyName, L"BootConfig");
1162 Status = ZwSetValueKey(LogConfKey,
1163 &KeyName,
1164 0,
1165 REG_RESOURCE_LIST,
1166 DeviceNode->BootResources,
1167 PnpDetermineResourceListSize(DeviceNode->BootResources));
1168 }
1169 }
1170
1171 /* Set 'BasicConfigVector' value */
1172 if (DeviceNode->ResourceRequirements != NULL &&
1173 DeviceNode->ResourceRequirements->ListSize != 0)
1174 {
1175 RtlInitUnicodeString(&KeyName, L"BasicConfigVector");
1176 Status = ZwSetValueKey(LogConfKey,
1177 &KeyName,
1178 0,
1179 REG_RESOURCE_REQUIREMENTS_LIST,
1180 DeviceNode->ResourceRequirements,
1181 DeviceNode->ResourceRequirements->ListSize);
1182 }
1183
1184 ZwClose(LogConfKey);
1185 }
1186
1187 /* Set the 'ConfigFlags' value */
1188 RtlInitUnicodeString(&KeyName, L"ConfigFlags");
1189 Status = ZwQueryValueKey(InstanceKey,
1190 &KeyName,
1191 KeyValueBasicInformation,
1192 NULL,
1193 0,
1194 &ResultLength);
1195 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
1196 {
1197 /* Write the default value */
1198 ULONG DefaultConfigFlags = 0;
1199 Status = ZwSetValueKey(InstanceKey,
1200 &KeyName,
1201 0,
1202 REG_DWORD,
1203 &DefaultConfigFlags,
1204 sizeof(DefaultConfigFlags));
1205 }
1206
1207 /* Create the 'Control' key */
1208 RtlInitUnicodeString(&KeyName, L"Control");
1209 InitializeObjectAttributes(&ObjectAttributes,
1210 &KeyName,
1211 OBJ_CASE_INSENSITIVE,
1212 InstanceKey,
1213 NULL);
1214 Status = ZwCreateKey(&ControlHandle, 0, &ObjectAttributes, 0, NULL, REG_OPTION_VOLATILE, NULL);
1215
1216 if (NT_SUCCESS(Status))
1217 ZwClose(ControlHandle);
1218
1219 DPRINT("IopSetDeviceInstanceData() done\n");
1220
1221 return Status;
1222 }
1223
1224 /*
1225 * IopGetParentIdPrefix
1226 *
1227 * Retrieve (or create) a string which identifies a device.
1228 *
1229 * Parameters
1230 * DeviceNode
1231 * Pointer to device node.
1232 * ParentIdPrefix
1233 * Pointer to the string where is returned the parent node identifier
1234 *
1235 * Remarks
1236 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1237 * valid and its Buffer field is NULL-terminated. The caller needs to
1238 * to free the string with RtlFreeUnicodeString when it is no longer
1239 * needed.
1240 */
1241
1242 NTSTATUS
1243 IopGetParentIdPrefix(PDEVICE_NODE DeviceNode,
1244 PUNICODE_STRING ParentIdPrefix)
1245 {
1246 ULONG KeyNameBufferLength;
1247 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation = NULL;
1248 UNICODE_STRING KeyName;
1249 UNICODE_STRING KeyValue;
1250 UNICODE_STRING ValueName;
1251 HANDLE hKey = NULL;
1252 ULONG crc32;
1253 NTSTATUS Status;
1254
1255 /* HACK: As long as some devices have a NULL device
1256 * instance path, the following test is required :(
1257 */
1258 if (DeviceNode->Parent->InstancePath.Length == 0)
1259 {
1260 DPRINT1("Parent of %wZ has NULL Instance path, please report!\n",
1261 &DeviceNode->InstancePath);
1262 return STATUS_UNSUCCESSFUL;
1263 }
1264
1265 /* 1. Try to retrieve ParentIdPrefix from registry */
1266 KeyNameBufferLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + MAX_PATH * sizeof(WCHAR);
1267 ParentIdPrefixInformation = ExAllocatePool(PagedPool, KeyNameBufferLength + sizeof(WCHAR));
1268 if (!ParentIdPrefixInformation)
1269 {
1270 Status = STATUS_INSUFFICIENT_RESOURCES;
1271 goto cleanup;
1272 }
1273
1274
1275 KeyName.Buffer = ExAllocatePool(PagedPool, (49 * sizeof(WCHAR)) + DeviceNode->Parent->InstancePath.Length);
1276 if (!KeyName.Buffer)
1277 {
1278 Status = STATUS_INSUFFICIENT_RESOURCES;
1279 goto cleanup;
1280 }
1281 KeyName.Length = 0;
1282 KeyName.MaximumLength = (49 * sizeof(WCHAR)) + DeviceNode->Parent->InstancePath.Length;
1283
1284 RtlAppendUnicodeToString(&KeyName, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1285 RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->Parent->InstancePath);
1286
1287 Status = IopOpenRegistryKeyEx(&hKey, NULL, &KeyName, KEY_QUERY_VALUE | KEY_SET_VALUE);
1288 if (!NT_SUCCESS(Status))
1289 goto cleanup;
1290 RtlInitUnicodeString(&ValueName, L"ParentIdPrefix");
1291 Status = ZwQueryValueKey(
1292 hKey, &ValueName,
1293 KeyValuePartialInformation, ParentIdPrefixInformation,
1294 KeyNameBufferLength, &KeyNameBufferLength);
1295 if (NT_SUCCESS(Status))
1296 {
1297 if (ParentIdPrefixInformation->Type != REG_SZ)
1298 Status = STATUS_UNSUCCESSFUL;
1299 else
1300 {
1301 KeyValue.Length = KeyValue.MaximumLength = (USHORT)ParentIdPrefixInformation->DataLength;
1302 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1303 }
1304 goto cleanup;
1305 }
1306 if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
1307 {
1308 KeyValue.Length = KeyValue.MaximumLength = (USHORT)ParentIdPrefixInformation->DataLength;
1309 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1310 goto cleanup;
1311 }
1312
1313 /* 2. Create the ParentIdPrefix value */
1314 crc32 = RtlComputeCrc32(0,
1315 (PUCHAR)DeviceNode->Parent->InstancePath.Buffer,
1316 DeviceNode->Parent->InstancePath.Length);
1317
1318 swprintf((PWSTR)ParentIdPrefixInformation->Data, L"%lx&%lx", DeviceNode->Parent->Level, crc32);
1319 RtlInitUnicodeString(&KeyValue, (PWSTR)ParentIdPrefixInformation->Data);
1320
1321 /* 3. Try to write the ParentIdPrefix to registry */
1322 Status = ZwSetValueKey(hKey,
1323 &ValueName,
1324 0,
1325 REG_SZ,
1326 (PVOID)KeyValue.Buffer,
1327 ((ULONG)wcslen(KeyValue.Buffer) + 1) * sizeof(WCHAR));
1328
1329 cleanup:
1330 if (NT_SUCCESS(Status))
1331 {
1332 /* Duplicate the string to return it */
1333 Status = RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, &KeyValue, ParentIdPrefix);
1334 }
1335 ExFreePool(ParentIdPrefixInformation);
1336 RtlFreeUnicodeString(&KeyName);
1337 if (hKey != NULL)
1338 ZwClose(hKey);
1339 return Status;
1340 }
1341
1342 NTSTATUS
1343 IopQueryHardwareIds(PDEVICE_NODE DeviceNode,
1344 HANDLE InstanceKey)
1345 {
1346 IO_STACK_LOCATION Stack;
1347 IO_STATUS_BLOCK IoStatusBlock;
1348 PWSTR Ptr;
1349 UNICODE_STRING ValueName;
1350 NTSTATUS Status;
1351 ULONG Length, TotalLength;
1352
1353 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1354
1355 RtlZeroMemory(&Stack, sizeof(Stack));
1356 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1357 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1358 &IoStatusBlock,
1359 IRP_MN_QUERY_ID,
1360 &Stack);
1361 if (NT_SUCCESS(Status))
1362 {
1363 /*
1364 * FIXME: Check for valid characters, if there is invalid characters
1365 * then bugcheck.
1366 */
1367 TotalLength = 0;
1368 Ptr = (PWSTR)IoStatusBlock.Information;
1369 DPRINT("Hardware IDs:\n");
1370 while (*Ptr)
1371 {
1372 DPRINT(" %S\n", Ptr);
1373 Length = (ULONG)wcslen(Ptr) + 1;
1374
1375 Ptr += Length;
1376 TotalLength += Length;
1377 }
1378 DPRINT("TotalLength: %hu\n", TotalLength);
1379 DPRINT("\n");
1380
1381 RtlInitUnicodeString(&ValueName, L"HardwareID");
1382 Status = ZwSetValueKey(InstanceKey,
1383 &ValueName,
1384 0,
1385 REG_MULTI_SZ,
1386 (PVOID)IoStatusBlock.Information,
1387 (TotalLength + 1) * sizeof(WCHAR));
1388 if (!NT_SUCCESS(Status))
1389 {
1390 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1391 }
1392 }
1393 else
1394 {
1395 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1396 }
1397
1398 return Status;
1399 }
1400
1401 NTSTATUS
1402 IopQueryCompatibleIds(PDEVICE_NODE DeviceNode,
1403 HANDLE InstanceKey)
1404 {
1405 IO_STACK_LOCATION Stack;
1406 IO_STATUS_BLOCK IoStatusBlock;
1407 PWSTR Ptr;
1408 UNICODE_STRING ValueName;
1409 NTSTATUS Status;
1410 ULONG Length, TotalLength;
1411
1412 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1413
1414 RtlZeroMemory(&Stack, sizeof(Stack));
1415 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1416 Status = IopInitiatePnpIrp(
1417 DeviceNode->PhysicalDeviceObject,
1418 &IoStatusBlock,
1419 IRP_MN_QUERY_ID,
1420 &Stack);
1421 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1422 {
1423 /*
1424 * FIXME: Check for valid characters, if there is invalid characters
1425 * then bugcheck.
1426 */
1427 TotalLength = 0;
1428 Ptr = (PWSTR)IoStatusBlock.Information;
1429 DPRINT("Compatible IDs:\n");
1430 while (*Ptr)
1431 {
1432 DPRINT(" %S\n", Ptr);
1433 Length = (ULONG)wcslen(Ptr) + 1;
1434
1435 Ptr += Length;
1436 TotalLength += Length;
1437 }
1438 DPRINT("TotalLength: %hu\n", TotalLength);
1439 DPRINT("\n");
1440
1441 RtlInitUnicodeString(&ValueName, L"CompatibleIDs");
1442 Status = ZwSetValueKey(InstanceKey,
1443 &ValueName,
1444 0,
1445 REG_MULTI_SZ,
1446 (PVOID)IoStatusBlock.Information,
1447 (TotalLength + 1) * sizeof(WCHAR));
1448 if (!NT_SUCCESS(Status))
1449 {
1450 DPRINT1("ZwSetValueKey() failed (Status %lx) or no Compatible ID returned\n", Status);
1451 }
1452 }
1453 else
1454 {
1455 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1456 }
1457
1458 return Status;
1459 }
1460
1461
1462 /*
1463 * IopActionInterrogateDeviceStack
1464 *
1465 * Retrieve information for all (direct) child nodes of a parent node.
1466 *
1467 * Parameters
1468 * DeviceNode
1469 * Pointer to device node.
1470 * Context
1471 * Pointer to parent node to retrieve child node information for.
1472 *
1473 * Remarks
1474 * Any errors that occur are logged instead so that all child services have a chance
1475 * of being interrogated.
1476 */
1477
1478 NTSTATUS
1479 IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode,
1480 PVOID Context)
1481 {
1482 IO_STATUS_BLOCK IoStatusBlock;
1483 PDEVICE_NODE ParentDeviceNode;
1484 WCHAR InstancePath[MAX_PATH];
1485 IO_STACK_LOCATION Stack;
1486 NTSTATUS Status;
1487 ULONG RequiredLength;
1488 LCID LocaleId;
1489 HANDLE InstanceKey = NULL;
1490 UNICODE_STRING ValueName;
1491 UNICODE_STRING ParentIdPrefix = { 0, 0, NULL };
1492 DEVICE_CAPABILITIES DeviceCapabilities;
1493
1494 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
1495 DPRINT("PDO 0x%p\n", DeviceNode->PhysicalDeviceObject);
1496
1497 ParentDeviceNode = (PDEVICE_NODE)Context;
1498
1499 /*
1500 * We are called for the parent too, but we don't need to do special
1501 * handling for this node
1502 */
1503
1504 if (DeviceNode == ParentDeviceNode)
1505 {
1506 DPRINT("Success\n");
1507 return STATUS_SUCCESS;
1508 }
1509
1510 /*
1511 * Make sure this device node is a direct child of the parent device node
1512 * that is given as an argument
1513 */
1514
1515 if (DeviceNode->Parent != ParentDeviceNode)
1516 {
1517 DPRINT("Skipping 2+ level child\n");
1518 return STATUS_SUCCESS;
1519 }
1520
1521 /* Skip processing if it was already completed before */
1522 if (DeviceNode->Flags & DNF_PROCESSED)
1523 {
1524 /* Nothing to do */
1525 return STATUS_SUCCESS;
1526 }
1527
1528 /* Get Locale ID */
1529 Status = ZwQueryDefaultLocale(FALSE, &LocaleId);
1530 if (!NT_SUCCESS(Status))
1531 {
1532 DPRINT1("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status);
1533 return Status;
1534 }
1535
1536 /*
1537 * FIXME: For critical errors, cleanup and disable device, but always
1538 * return STATUS_SUCCESS.
1539 */
1540
1541 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1542
1543 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
1544 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1545 &IoStatusBlock,
1546 IRP_MN_QUERY_ID,
1547 &Stack);
1548 if (NT_SUCCESS(Status))
1549 {
1550 /* Copy the device id string */
1551 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
1552
1553 /*
1554 * FIXME: Check for valid characters, if there is invalid characters
1555 * then bugcheck.
1556 */
1557 }
1558 else
1559 {
1560 DPRINT1("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1561
1562 /* We have to return success otherwise we abort the traverse operation */
1563 return STATUS_SUCCESS;
1564 }
1565
1566 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack (after enumeration)\n");
1567
1568 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
1569 if (!NT_SUCCESS(Status))
1570 {
1571 DPRINT1("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status);
1572
1573 /* We have to return success otherwise we abort the traverse operation */
1574 return STATUS_SUCCESS;
1575 }
1576
1577 /* This bit is only check after enumeration */
1578 if (DeviceCapabilities.HardwareDisabled)
1579 {
1580 /* FIXME: Cleanup device */
1581 DeviceNode->Flags |= DNF_DISABLED;
1582 return STATUS_SUCCESS;
1583 }
1584 else
1585 DeviceNode->Flags &= ~DNF_DISABLED;
1586
1587 if (!DeviceCapabilities.UniqueID)
1588 {
1589 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1590 DPRINT("Instance ID is not unique\n");
1591 Status = IopGetParentIdPrefix(DeviceNode, &ParentIdPrefix);
1592 if (!NT_SUCCESS(Status))
1593 {
1594 DPRINT1("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status);
1595
1596 /* We have to return success otherwise we abort the traverse operation */
1597 return STATUS_SUCCESS;
1598 }
1599 }
1600
1601 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1602
1603 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
1604 Status = IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
1605 &IoStatusBlock,
1606 IRP_MN_QUERY_ID,
1607 &Stack);
1608 if (NT_SUCCESS(Status))
1609 {
1610 /* Append the instance id string */
1611 wcscat(InstancePath, L"\\");
1612 if (ParentIdPrefix.Length > 0)
1613 {
1614 /* Add information from parent bus device to InstancePath */
1615 wcscat(InstancePath, ParentIdPrefix.Buffer);
1616 if (IoStatusBlock.Information && *(PWSTR)IoStatusBlock.Information)
1617 wcscat(InstancePath, L"&");
1618 }
1619 if (IoStatusBlock.Information)
1620 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1621
1622 /*
1623 * FIXME: Check for valid characters, if there is invalid characters
1624 * then bugcheck
1625 */
1626 }
1627 else
1628 {
1629 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1630 }
1631 RtlFreeUnicodeString(&ParentIdPrefix);
1632
1633 if (!RtlCreateUnicodeString(&DeviceNode->InstancePath, InstancePath))
1634 {
1635 DPRINT("No resources\n");
1636 /* FIXME: Cleanup and disable device */
1637 }
1638
1639 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1640
1641 /*
1642 * Create registry key for the instance id, if it doesn't exist yet
1643 */
1644 Status = IopCreateDeviceKeyPath(&DeviceNode->InstancePath, 0, &InstanceKey);
1645 if (!NT_SUCCESS(Status))
1646 {
1647 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1648
1649 /* We have to return success otherwise we abort the traverse operation */
1650 return STATUS_SUCCESS;
1651 }
1652
1653 IopQueryHardwareIds(DeviceNode, InstanceKey);
1654
1655 IopQueryCompatibleIds(DeviceNode, InstanceKey);
1656
1657 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1658
1659 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1660 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1661 Status = IopInitiatePnpIrp(
1662 DeviceNode->PhysicalDeviceObject,
1663 &IoStatusBlock,
1664 IRP_MN_QUERY_DEVICE_TEXT,
1665 &Stack);
1666 /* This key is mandatory, so even if the Irp fails, we still write it */
1667 RtlInitUnicodeString(&ValueName, L"DeviceDesc");
1668 if (ZwQueryValueKey(InstanceKey, &ValueName, KeyValueBasicInformation, NULL, 0, &RequiredLength) == STATUS_OBJECT_NAME_NOT_FOUND)
1669 {
1670 if (NT_SUCCESS(Status) &&
1671 IoStatusBlock.Information &&
1672 (*(PWSTR)IoStatusBlock.Information != 0))
1673 {
1674 /* This key is overriden when a driver is installed. Don't write the
1675 * new description if another one already exists */
1676 Status = ZwSetValueKey(InstanceKey,
1677 &ValueName,
1678 0,
1679 REG_SZ,
1680 (PVOID)IoStatusBlock.Information,
1681 ((ULONG)wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1682 }
1683 else
1684 {
1685 UNICODE_STRING DeviceDesc = RTL_CONSTANT_STRING(L"Unknown device");
1686 DPRINT("Driver didn't return DeviceDesc (Status 0x%08lx), so place unknown device there\n", Status);
1687
1688 Status = ZwSetValueKey(InstanceKey,
1689 &ValueName,
1690 0,
1691 REG_SZ,
1692 DeviceDesc.Buffer,
1693 DeviceDesc.MaximumLength);
1694
1695 if (!NT_SUCCESS(Status))
1696 {
1697 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status);
1698 }
1699
1700 }
1701 }
1702
1703 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1704
1705 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1706 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1707 Status = IopInitiatePnpIrp(
1708 DeviceNode->PhysicalDeviceObject,
1709 &IoStatusBlock,
1710 IRP_MN_QUERY_DEVICE_TEXT,
1711 &Stack);
1712 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1713 {
1714 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1715 RtlInitUnicodeString(&ValueName, L"LocationInformation");
1716 Status = ZwSetValueKey(InstanceKey,
1717 &ValueName,
1718 0,
1719 REG_SZ,
1720 (PVOID)IoStatusBlock.Information,
1721 ((ULONG)wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1722 if (!NT_SUCCESS(Status))
1723 {
1724 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1725 }
1726 }
1727 else
1728 {
1729 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1730 }
1731
1732 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1733
1734 Status = IopInitiatePnpIrp(
1735 DeviceNode->PhysicalDeviceObject,
1736 &IoStatusBlock,
1737 IRP_MN_QUERY_BUS_INFORMATION,
1738 NULL);
1739 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1740 {
1741 PPNP_BUS_INFORMATION BusInformation =
1742 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1743
1744 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1745 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1746 DeviceNode->ChildBusTypeIndex = IopGetBusTypeGuidIndex(&BusInformation->BusTypeGuid);
1747 ExFreePool(BusInformation);
1748 }
1749 else
1750 {
1751 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1752
1753 DeviceNode->ChildBusNumber = 0xFFFFFFF0;
1754 DeviceNode->ChildInterfaceType = InterfaceTypeUndefined;
1755 DeviceNode->ChildBusTypeIndex = -1;
1756 }
1757
1758 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1759
1760 Status = IopInitiatePnpIrp(
1761 DeviceNode->PhysicalDeviceObject,
1762 &IoStatusBlock,
1763 IRP_MN_QUERY_RESOURCES,
1764 NULL);
1765 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
1766 {
1767 DeviceNode->BootResources =
1768 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1769 IopDeviceNodeSetFlag(DeviceNode, DNF_HAS_BOOT_CONFIG);
1770 }
1771 else
1772 {
1773 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
1774 DeviceNode->BootResources = NULL;
1775 }
1776
1777 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1778
1779 Status = IopInitiatePnpIrp(
1780 DeviceNode->PhysicalDeviceObject,
1781 &IoStatusBlock,
1782 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1783 NULL);
1784 if (NT_SUCCESS(Status))
1785 {
1786 DeviceNode->ResourceRequirements =
1787 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1788 }
1789 else
1790 {
1791 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status);
1792 DeviceNode->ResourceRequirements = NULL;
1793 }
1794
1795 if (InstanceKey != NULL)
1796 {
1797 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1798 }
1799
1800 ZwClose(InstanceKey);
1801
1802 IopDeviceNodeSetFlag(DeviceNode, DNF_PROCESSED);
1803
1804 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_LEGACY_DRIVER))
1805 {
1806 /* Report the device to the user-mode pnp manager */
1807 IopQueueTargetDeviceEvent(&GUID_DEVICE_ENUMERATED,
1808 &DeviceNode->InstancePath);
1809 }
1810
1811 return STATUS_SUCCESS;
1812 }
1813
1814 static
1815 VOID
1816 IopHandleDeviceRemoval(
1817 IN PDEVICE_NODE DeviceNode,
1818 IN PDEVICE_RELATIONS DeviceRelations)
1819 {
1820 PDEVICE_NODE Child = DeviceNode->Child, NextChild;
1821 ULONG i;
1822 BOOLEAN Found;
1823
1824 while (Child != NULL)
1825 {
1826 NextChild = Child->Sibling;
1827 Found = FALSE;
1828
1829 for (i = 0; DeviceRelations && i < DeviceRelations->Count; i++)
1830 {
1831 if (IopGetDeviceNode(DeviceRelations->Objects[i]) == Child)
1832 {
1833 Found = TRUE;
1834 break;
1835 }
1836 }
1837
1838 if (!Found)
1839 {
1840 IopSendSurpriseRemoval(Child->PhysicalDeviceObject);
1841
1842 /* Tell the user-mode PnP manager that a device was removed */
1843 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL,
1844 &Child->InstancePath);
1845
1846 IopSendRemoveDevice(Child->PhysicalDeviceObject);
1847 }
1848
1849 Child = NextChild;
1850 }
1851 }
1852
1853 NTSTATUS
1854 IopEnumerateDevice(
1855 IN PDEVICE_OBJECT DeviceObject)
1856 {
1857 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
1858 DEVICETREE_TRAVERSE_CONTEXT Context;
1859 PDEVICE_RELATIONS DeviceRelations;
1860 PDEVICE_OBJECT ChildDeviceObject;
1861 IO_STATUS_BLOCK IoStatusBlock;
1862 PDEVICE_NODE ChildDeviceNode;
1863 IO_STACK_LOCATION Stack;
1864 NTSTATUS Status;
1865 ULONG i;
1866
1867 DPRINT("DeviceObject 0x%p\n", DeviceObject);
1868
1869 if (DeviceNode->Flags & DNF_NEED_ENUMERATION_ONLY)
1870 {
1871 DeviceNode->Flags &= ~DNF_NEED_ENUMERATION_ONLY;
1872
1873 DPRINT("Sending GUID_DEVICE_ARRIVAL\n");
1874 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1875 &DeviceNode->InstancePath);
1876 }
1877
1878 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
1879
1880 Stack.Parameters.QueryDeviceRelations.Type = BusRelations;
1881
1882 Status = IopInitiatePnpIrp(
1883 DeviceObject,
1884 &IoStatusBlock,
1885 IRP_MN_QUERY_DEVICE_RELATIONS,
1886 &Stack);
1887 if (!NT_SUCCESS(Status) || Status == STATUS_PENDING)
1888 {
1889 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status);
1890 return Status;
1891 }
1892
1893 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
1894
1895 /*
1896 * Send removal IRPs for devices that have disappeared
1897 * NOTE: This code handles the case where no relations are specified
1898 */
1899 IopHandleDeviceRemoval(DeviceNode, DeviceRelations);
1900
1901 /* Now we bail if nothing was returned */
1902 if (!DeviceRelations)
1903 {
1904 /* We're all done */
1905 DPRINT("No PDOs\n");
1906 return STATUS_SUCCESS;
1907 }
1908
1909 DPRINT("Got %u PDOs\n", DeviceRelations->Count);
1910
1911 /*
1912 * Create device nodes for all discovered devices
1913 */
1914 for (i = 0; i < DeviceRelations->Count; i++)
1915 {
1916 ChildDeviceObject = DeviceRelations->Objects[i];
1917 ASSERT((ChildDeviceObject->Flags & DO_DEVICE_INITIALIZING) == 0);
1918
1919 ChildDeviceNode = IopGetDeviceNode(ChildDeviceObject);
1920 if (!ChildDeviceNode)
1921 {
1922 /* One doesn't exist, create it */
1923 Status = IopCreateDeviceNode(
1924 DeviceNode,
1925 ChildDeviceObject,
1926 NULL,
1927 &ChildDeviceNode);
1928 if (NT_SUCCESS(Status))
1929 {
1930 /* Mark the node as enumerated */
1931 ChildDeviceNode->Flags |= DNF_ENUMERATED;
1932
1933 /* Mark the DO as bus enumerated */
1934 ChildDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
1935 }
1936 else
1937 {
1938 /* Ignore this DO */
1939 DPRINT1("IopCreateDeviceNode() failed with status 0x%08x. Skipping PDO %u\n", Status, i);
1940 ObDereferenceObject(ChildDeviceObject);
1941 }
1942 }
1943 else
1944 {
1945 /* Mark it as enumerated */
1946 ChildDeviceNode->Flags |= DNF_ENUMERATED;
1947 ObDereferenceObject(ChildDeviceObject);
1948 }
1949 }
1950 ExFreePool(DeviceRelations);
1951
1952 /*
1953 * Retrieve information about all discovered children from the bus driver
1954 */
1955 IopInitDeviceTreeTraverseContext(
1956 &Context,
1957 DeviceNode,
1958 IopActionInterrogateDeviceStack,
1959 DeviceNode);
1960
1961 Status = IopTraverseDeviceTree(&Context);
1962 if (!NT_SUCCESS(Status))
1963 {
1964 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status);
1965 return Status;
1966 }
1967
1968 /*
1969 * Retrieve configuration from the registry for discovered children
1970 */
1971 IopInitDeviceTreeTraverseContext(
1972 &Context,
1973 DeviceNode,
1974 IopActionConfigureChildServices,
1975 DeviceNode);
1976
1977 Status = IopTraverseDeviceTree(&Context);
1978 if (!NT_SUCCESS(Status))
1979 {
1980 DPRINT("IopTraverseDeviceTree() failed with status 0x%08lx\n", Status);
1981 return Status;
1982 }
1983
1984 /*
1985 * Initialize services for discovered children.
1986 */
1987 Status = IopInitializePnpServices(DeviceNode);
1988 if (!NT_SUCCESS(Status))
1989 {
1990 DPRINT("IopInitializePnpServices() failed with status 0x%08lx\n", Status);
1991 return Status;
1992 }
1993
1994 DPRINT("IopEnumerateDevice() finished\n");
1995 return STATUS_SUCCESS;
1996 }
1997
1998
1999 /*
2000 * IopActionConfigureChildServices
2001 *
2002 * Retrieve configuration for all (direct) child nodes of a parent node.
2003 *
2004 * Parameters
2005 * DeviceNode
2006 * Pointer to device node.
2007 * Context
2008 * Pointer to parent node to retrieve child node configuration for.
2009 *
2010 * Remarks
2011 * Any errors that occur are logged instead so that all child services have a chance of beeing
2012 * configured.
2013 */
2014
2015 NTSTATUS
2016 IopActionConfigureChildServices(PDEVICE_NODE DeviceNode,
2017 PVOID Context)
2018 {
2019 RTL_QUERY_REGISTRY_TABLE QueryTable[3];
2020 PDEVICE_NODE ParentDeviceNode;
2021 PUNICODE_STRING Service;
2022 UNICODE_STRING ClassGUID;
2023 NTSTATUS Status;
2024 DEVICE_CAPABILITIES DeviceCaps;
2025
2026 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
2027
2028 ParentDeviceNode = (PDEVICE_NODE)Context;
2029
2030 /*
2031 * We are called for the parent too, but we don't need to do special
2032 * handling for this node
2033 */
2034 if (DeviceNode == ParentDeviceNode)
2035 {
2036 DPRINT("Success\n");
2037 return STATUS_SUCCESS;
2038 }
2039
2040 /*
2041 * Make sure this device node is a direct child of the parent device node
2042 * that is given as an argument
2043 */
2044
2045 if (DeviceNode->Parent != ParentDeviceNode)
2046 {
2047 DPRINT("Skipping 2+ level child\n");
2048 return STATUS_SUCCESS;
2049 }
2050
2051 if (!(DeviceNode->Flags & DNF_PROCESSED))
2052 {
2053 DPRINT1("Child not ready to be configured\n");
2054 return STATUS_SUCCESS;
2055 }
2056
2057 if (!(DeviceNode->Flags & (DNF_DISABLED | DNF_STARTED | DNF_ADDED)))
2058 {
2059 WCHAR RegKeyBuffer[MAX_PATH];
2060 UNICODE_STRING RegKey;
2061
2062 RegKey.Length = 0;
2063 RegKey.MaximumLength = sizeof(RegKeyBuffer);
2064 RegKey.Buffer = RegKeyBuffer;
2065
2066 /*
2067 * Retrieve configuration from Enum key
2068 */
2069
2070 Service = &DeviceNode->ServiceName;
2071
2072 RtlZeroMemory(QueryTable, sizeof(QueryTable));
2073 RtlInitUnicodeString(Service, NULL);
2074 RtlInitUnicodeString(&ClassGUID, NULL);
2075
2076 QueryTable[0].Name = L"Service";
2077 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
2078 QueryTable[0].EntryContext = Service;
2079
2080 QueryTable[1].Name = L"ClassGUID";
2081 QueryTable[1].Flags = RTL_QUERY_REGISTRY_DIRECT;
2082 QueryTable[1].EntryContext = &ClassGUID;
2083 QueryTable[1].DefaultType = REG_SZ;
2084 QueryTable[1].DefaultData = L"";
2085 QueryTable[1].DefaultLength = 0;
2086
2087 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2088 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
2089
2090 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
2091 RegKey.Buffer, QueryTable, NULL, NULL);
2092
2093 if (!NT_SUCCESS(Status))
2094 {
2095 /* FIXME: Log the error */
2096 DPRINT("Could not retrieve configuration for device %wZ (Status 0x%08x)\n",
2097 &DeviceNode->InstancePath, Status);
2098 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2099 return STATUS_SUCCESS;
2100 }
2101
2102 if (Service->Buffer == NULL)
2103 {
2104 if (NT_SUCCESS(IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps)) &&
2105 DeviceCaps.RawDeviceOK)
2106 {
2107 DPRINT1("%wZ is using parent bus driver (%wZ)\n", &DeviceNode->InstancePath, &ParentDeviceNode->ServiceName);
2108
2109 DeviceNode->ServiceName.Length = 0;
2110 DeviceNode->ServiceName.MaximumLength = 0;
2111 DeviceNode->ServiceName.Buffer = NULL;
2112 }
2113 else if (ClassGUID.Length != 0)
2114 {
2115 /* Device has a ClassGUID value, but no Service value.
2116 * Suppose it is using the NULL driver, so state the
2117 * device is started */
2118 DPRINT("%wZ is using NULL driver\n", &DeviceNode->InstancePath);
2119 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
2120 }
2121 else
2122 {
2123 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2124 }
2125 return STATUS_SUCCESS;
2126 }
2127
2128 DPRINT("Got Service %S\n", Service->Buffer);
2129 }
2130
2131 return STATUS_SUCCESS;
2132 }
2133
2134 /*
2135 * IopActionInitChildServices
2136 *
2137 * Initialize the service for all (direct) child nodes of a parent node
2138 *
2139 * Parameters
2140 * DeviceNode
2141 * Pointer to device node.
2142 * Context
2143 * Pointer to parent node to initialize child node services for.
2144 *
2145 * Remarks
2146 * If the driver image for a service is not loaded and initialized
2147 * it is done here too. Any errors that occur are logged instead so
2148 * that all child services have a chance of being initialized.
2149 */
2150
2151 NTSTATUS
2152 IopActionInitChildServices(PDEVICE_NODE DeviceNode,
2153 PVOID Context)
2154 {
2155 PDEVICE_NODE ParentDeviceNode;
2156 NTSTATUS Status;
2157 BOOLEAN BootDrivers = !PnpSystemInit;
2158
2159 DPRINT("IopActionInitChildServices(%p, %p)\n", DeviceNode, Context);
2160
2161 ParentDeviceNode = (PDEVICE_NODE)Context;
2162
2163 /*
2164 * We are called for the parent too, but we don't need to do special
2165 * handling for this node
2166 */
2167 if (DeviceNode == ParentDeviceNode)
2168 {
2169 DPRINT("Success\n");
2170 return STATUS_SUCCESS;
2171 }
2172
2173 /*
2174 * Make sure this device node is a direct child of the parent device node
2175 * that is given as an argument
2176 */
2177
2178 if (DeviceNode->Parent != ParentDeviceNode)
2179 {
2180 DPRINT("Skipping 2+ level child\n");
2181 return STATUS_SUCCESS;
2182 }
2183
2184 if (!(DeviceNode->Flags & DNF_PROCESSED))
2185 {
2186 DPRINT1("Child not ready to be added\n");
2187 return STATUS_SUCCESS;
2188 }
2189
2190 if (IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED) ||
2191 IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) ||
2192 IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
2193 return STATUS_SUCCESS;
2194
2195 if (DeviceNode->ServiceName.Buffer == NULL)
2196 {
2197 /* We don't need to worry about loading the driver because we're
2198 * being driven in raw mode so our parent must be loaded to get here */
2199 Status = IopInitializeDevice(DeviceNode, NULL);
2200 if (NT_SUCCESS(Status))
2201 {
2202 Status = IopStartDevice(DeviceNode);
2203 if (!NT_SUCCESS(Status))
2204 {
2205 DPRINT1("IopStartDevice(%wZ) failed with status 0x%08x\n",
2206 &DeviceNode->InstancePath, Status);
2207 }
2208 }
2209 }
2210 else
2211 {
2212 PLDR_DATA_TABLE_ENTRY ModuleObject;
2213 PDRIVER_OBJECT DriverObject;
2214
2215 /* Get existing DriverObject pointer (in case the driver has
2216 already been loaded and initialized) */
2217 Status = IopGetDriverObject(
2218 &DriverObject,
2219 &DeviceNode->ServiceName,
2220 FALSE);
2221
2222 if (!NT_SUCCESS(Status))
2223 {
2224 /* Driver is not initialized, try to load it */
2225 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
2226
2227 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
2228 {
2229 /* STATUS_IMAGE_ALREADY_LOADED means this driver
2230 was loaded by the bootloader */
2231 if ((Status != STATUS_IMAGE_ALREADY_LOADED) ||
2232 (Status == STATUS_IMAGE_ALREADY_LOADED && !DriverObject))
2233 {
2234 /* Initialize the driver */
2235 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
2236 &DeviceNode->ServiceName, FALSE, &DriverObject);
2237 }
2238 else
2239 {
2240 Status = STATUS_SUCCESS;
2241 }
2242 }
2243 else
2244 {
2245 DPRINT1("IopLoadServiceModule(%wZ) failed with status 0x%08x\n",
2246 &DeviceNode->ServiceName, Status);
2247 }
2248 }
2249
2250 /* Driver is loaded and initialized at this point */
2251 if (NT_SUCCESS(Status))
2252 {
2253 /* Initialize the device, including all filters */
2254 Status = PipCallDriverAddDevice(DeviceNode, FALSE, DriverObject);
2255 }
2256 else
2257 {
2258 /*
2259 * Don't disable when trying to load only boot drivers
2260 */
2261 if (!BootDrivers)
2262 {
2263 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2264 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
2265 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2266 DPRINT1("Initialization of service %S failed (Status %x)\n",
2267 DeviceNode->ServiceName.Buffer, Status);
2268 }
2269 }
2270 }
2271
2272 return STATUS_SUCCESS;
2273 }
2274
2275 /*
2276 * IopInitializePnpServices
2277 *
2278 * Initialize services for discovered children
2279 *
2280 * Parameters
2281 * DeviceNode
2282 * Top device node to start initializing services.
2283 *
2284 * Return Value
2285 * Status
2286 */
2287 NTSTATUS
2288 IopInitializePnpServices(IN PDEVICE_NODE DeviceNode)
2289 {
2290 DEVICETREE_TRAVERSE_CONTEXT Context;
2291
2292 DPRINT("IopInitializePnpServices(%p)\n", DeviceNode);
2293
2294 IopInitDeviceTreeTraverseContext(
2295 &Context,
2296 DeviceNode,
2297 IopActionInitChildServices,
2298 DeviceNode);
2299
2300 return IopTraverseDeviceTree(&Context);
2301 }
2302
2303 static NTSTATUS INIT_FUNCTION
2304 IopEnumerateDetectedDevices(
2305 IN HANDLE hBaseKey,
2306 IN PUNICODE_STRING RelativePath OPTIONAL,
2307 IN HANDLE hRootKey,
2308 IN BOOLEAN EnumerateSubKeys,
2309 IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources,
2310 IN ULONG ParentBootResourcesLength)
2311 {
2312 UNICODE_STRING IdentifierU = RTL_CONSTANT_STRING(L"Identifier");
2313 UNICODE_STRING HardwareIDU = RTL_CONSTANT_STRING(L"HardwareID");
2314 UNICODE_STRING ConfigurationDataU = RTL_CONSTANT_STRING(L"Configuration Data");
2315 UNICODE_STRING BootConfigU = RTL_CONSTANT_STRING(L"BootConfig");
2316 UNICODE_STRING LogConfU = RTL_CONSTANT_STRING(L"LogConf");
2317 OBJECT_ATTRIBUTES ObjectAttributes;
2318 HANDLE hDevicesKey = NULL;
2319 HANDLE hDeviceKey = NULL;
2320 HANDLE hLevel1Key, hLevel2Key = NULL, hLogConf;
2321 UNICODE_STRING Level2NameU;
2322 WCHAR Level2Name[5];
2323 ULONG IndexDevice = 0;
2324 ULONG IndexSubKey;
2325 PKEY_BASIC_INFORMATION pDeviceInformation = NULL;
2326 ULONG DeviceInfoLength = sizeof(KEY_BASIC_INFORMATION) + 50 * sizeof(WCHAR);
2327 PKEY_VALUE_PARTIAL_INFORMATION pValueInformation = NULL;
2328 ULONG ValueInfoLength = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 50 * sizeof(WCHAR);
2329 UNICODE_STRING DeviceName, ValueName;
2330 ULONG RequiredSize;
2331 PCM_FULL_RESOURCE_DESCRIPTOR BootResources = NULL;
2332 ULONG BootResourcesLength;
2333 NTSTATUS Status;
2334
2335 const UNICODE_STRING IdentifierPci = RTL_CONSTANT_STRING(L"PCI");
2336 UNICODE_STRING HardwareIdPci = RTL_CONSTANT_STRING(L"*PNP0A03\0");
2337 static ULONG DeviceIndexPci = 0;
2338 const UNICODE_STRING IdentifierSerial = RTL_CONSTANT_STRING(L"SerialController");
2339 UNICODE_STRING HardwareIdSerial = RTL_CONSTANT_STRING(L"*PNP0501\0");
2340 static ULONG DeviceIndexSerial = 0;
2341 const UNICODE_STRING IdentifierKeyboard = RTL_CONSTANT_STRING(L"KeyboardController");
2342 UNICODE_STRING HardwareIdKeyboard = RTL_CONSTANT_STRING(L"*PNP0303\0");
2343 static ULONG DeviceIndexKeyboard = 0;
2344 const UNICODE_STRING IdentifierMouse = RTL_CONSTANT_STRING(L"PointerController");
2345 UNICODE_STRING HardwareIdMouse = RTL_CONSTANT_STRING(L"*PNP0F13\0");
2346 static ULONG DeviceIndexMouse = 0;
2347 const UNICODE_STRING IdentifierParallel = RTL_CONSTANT_STRING(L"ParallelController");
2348 UNICODE_STRING HardwareIdParallel = RTL_CONSTANT_STRING(L"*PNP0400\0");
2349 static ULONG DeviceIndexParallel = 0;
2350 const UNICODE_STRING IdentifierFloppy = RTL_CONSTANT_STRING(L"FloppyDiskPeripheral");
2351 UNICODE_STRING HardwareIdFloppy = RTL_CONSTANT_STRING(L"*PNP0700\0");
2352 static ULONG DeviceIndexFloppy = 0;
2353 const UNICODE_STRING IdentifierIsa = RTL_CONSTANT_STRING(L"ISA");
2354 UNICODE_STRING HardwareIdIsa = RTL_CONSTANT_STRING(L"*PNP0A00\0");
2355 static ULONG DeviceIndexIsa = 0;
2356 UNICODE_STRING HardwareIdKey;
2357 PUNICODE_STRING pHardwareId;
2358 ULONG DeviceIndex = 0;
2359 PUCHAR CmResourceList;
2360 ULONG ListCount;
2361
2362 if (RelativePath)
2363 {
2364 Status = IopOpenRegistryKeyEx(&hDevicesKey, hBaseKey, RelativePath, KEY_ENUMERATE_SUB_KEYS);
2365 if (!NT_SUCCESS(Status))
2366 {
2367 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
2368 goto cleanup;
2369 }
2370 }
2371 else
2372 hDevicesKey = hBaseKey;
2373
2374 pDeviceInformation = ExAllocatePool(PagedPool, DeviceInfoLength);
2375 if (!pDeviceInformation)
2376 {
2377 DPRINT("ExAllocatePool() failed\n");
2378 Status = STATUS_NO_MEMORY;
2379 goto cleanup;
2380 }
2381
2382 pValueInformation = ExAllocatePool(PagedPool, ValueInfoLength);
2383 if (!pValueInformation)
2384 {
2385 DPRINT("ExAllocatePool() failed\n");
2386 Status = STATUS_NO_MEMORY;
2387 goto cleanup;
2388 }
2389
2390 while (TRUE)
2391 {
2392 Status = ZwEnumerateKey(hDevicesKey, IndexDevice, KeyBasicInformation, pDeviceInformation, DeviceInfoLength, &RequiredSize);
2393 if (Status == STATUS_NO_MORE_ENTRIES)
2394 break;
2395 else if (Status == STATUS_BUFFER_OVERFLOW || Status == STATUS_BUFFER_TOO_SMALL)
2396 {
2397 ExFreePool(pDeviceInformation);
2398 DeviceInfoLength = RequiredSize;
2399 pDeviceInformation = ExAllocatePool(PagedPool, DeviceInfoLength);
2400 if (!pDeviceInformation)
2401 {
2402 DPRINT("ExAllocatePool() failed\n");
2403 Status = STATUS_NO_MEMORY;
2404 goto cleanup;
2405 }
2406 Status = ZwEnumerateKey(hDevicesKey, IndexDevice, KeyBasicInformation, pDeviceInformation, DeviceInfoLength, &RequiredSize);
2407 }
2408 if (!NT_SUCCESS(Status))
2409 {
2410 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
2411 goto cleanup;
2412 }
2413 IndexDevice++;
2414
2415 /* Open device key */
2416 DeviceName.Length = DeviceName.MaximumLength = (USHORT)pDeviceInformation->NameLength;
2417 DeviceName.Buffer = pDeviceInformation->Name;
2418
2419 Status = IopOpenRegistryKeyEx(&hDeviceKey, hDevicesKey, &DeviceName,
2420 KEY_QUERY_VALUE + (EnumerateSubKeys ? KEY_ENUMERATE_SUB_KEYS : 0));
2421 if (!NT_SUCCESS(Status))
2422 {
2423 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
2424 goto cleanup;
2425 }
2426
2427 /* Read boot resources, and add then to parent ones */
2428 Status = ZwQueryValueKey(hDeviceKey, &ConfigurationDataU, KeyValuePartialInformation, pValueInformation, ValueInfoLength, &RequiredSize);
2429 if (Status == STATUS_BUFFER_OVERFLOW || Status == STATUS_BUFFER_TOO_SMALL)
2430 {
2431 ExFreePool(pValueInformation);
2432 ValueInfoLength = RequiredSize;
2433 pValueInformation = ExAllocatePool(PagedPool, ValueInfoLength);
2434 if (!pValueInformation)
2435 {
2436 DPRINT("ExAllocatePool() failed\n");
2437 ZwDeleteKey(hLevel2Key);
2438 Status = STATUS_NO_MEMORY;
2439 goto cleanup;
2440 }
2441 Status = ZwQueryValueKey(hDeviceKey, &ConfigurationDataU, KeyValuePartialInformation, pValueInformation, ValueInfoLength, &RequiredSize);
2442 }
2443 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
2444 {
2445 BootResources = ParentBootResources;
2446 BootResourcesLength = ParentBootResourcesLength;
2447 }
2448 else if (!NT_SUCCESS(Status))
2449 {
2450 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status);
2451 goto nextdevice;
2452 }
2453 else if (pValueInformation->Type != REG_FULL_RESOURCE_DESCRIPTOR)
2454 {
2455 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation->Type, REG_FULL_RESOURCE_DESCRIPTOR);
2456 goto nextdevice;
2457 }
2458 else
2459 {
2460 static const ULONG Header = FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList.PartialDescriptors);
2461
2462 /* Concatenate current resources and parent ones */
2463 if (ParentBootResourcesLength == 0)
2464 BootResourcesLength = pValueInformation->DataLength;
2465 else
2466 BootResourcesLength = ParentBootResourcesLength
2467 + pValueInformation->DataLength
2468 - Header;
2469 BootResources = ExAllocatePool(PagedPool, BootResourcesLength);
2470 if (!BootResources)
2471 {
2472 DPRINT("ExAllocatePool() failed\n");
2473 goto nextdevice;
2474 }
2475 if (ParentBootResourcesLength < sizeof(CM_FULL_RESOURCE_DESCRIPTOR))
2476 {
2477 RtlCopyMemory(BootResources, pValueInformation->Data, pValueInformation->DataLength);
2478 }
2479 else if (ParentBootResources->PartialResourceList.PartialDescriptors[ParentBootResources->PartialResourceList.Count - 1].Type == CmResourceTypeDeviceSpecific)
2480 {
2481 RtlCopyMemory(BootResources, pValueInformation->Data, pValueInformation->DataLength);
2482 RtlCopyMemory(
2483 (PVOID)((ULONG_PTR)BootResources + pValueInformation->DataLength),
2484 (PVOID)((ULONG_PTR)ParentBootResources + Header),
2485 ParentBootResourcesLength - Header);
2486 BootResources->PartialResourceList.Count += ParentBootResources->PartialResourceList.Count;
2487 }
2488 else
2489 {
2490 RtlCopyMemory(BootResources, pValueInformation->Data, Header);
2491 RtlCopyMemory(
2492 (PVOID)((ULONG_PTR)BootResources + Header),
2493 (PVOID)((ULONG_PTR)ParentBootResources + Header),
2494 ParentBootResourcesLength - Header);
2495 RtlCopyMemory(
2496 (PVOID)((ULONG_PTR)BootResources + ParentBootResourcesLength),
2497 pValueInformation->Data + Header,
2498 pValueInformation->DataLength - Header);
2499 BootResources->PartialResourceList.Count += ParentBootResources->PartialResourceList.Count;
2500 }
2501 }
2502
2503 if (EnumerateSubKeys)
2504 {
2505 IndexSubKey = 0;
2506 while (TRUE)
2507 {
2508 Status = ZwEnumerateKey(hDeviceKey, IndexSubKey, KeyBasicInformation, pDeviceInformation, DeviceInfoLength, &RequiredSize);
2509 if (Status == STATUS_NO_MORE_ENTRIES)
2510 break;
2511 else if (Status == STATUS_BUFFER_OVERFLOW || Status == STATUS_BUFFER_TOO_SMALL)
2512 {
2513 ExFreePool(pDeviceInformation);
2514 DeviceInfoLength = RequiredSize;
2515 pDeviceInformation = ExAllocatePool(PagedPool, DeviceInfoLength);
2516 if (!pDeviceInformation)
2517 {
2518 DPRINT("ExAllocatePool() failed\n");
2519 Status = STATUS_NO_MEMORY;
2520 goto cleanup;
2521 }
2522 Status = ZwEnumerateKey(hDeviceKey, IndexSubKey, KeyBasicInformation, pDeviceInformation, DeviceInfoLength, &RequiredSize);
2523 }
2524 if (!NT_SUCCESS(Status))
2525 {
2526 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
2527 goto cleanup;
2528 }
2529 IndexSubKey++;
2530 DeviceName.Length = DeviceName.MaximumLength = (USHORT)pDeviceInformation->NameLength;
2531 DeviceName.Buffer = pDeviceInformation->Name;
2532
2533 Status = IopEnumerateDetectedDevices(
2534 hDeviceKey,
2535 &DeviceName,
2536 hRootKey,
2537 TRUE,
2538 BootResources,
2539 BootResourcesLength);
2540 if (!NT_SUCCESS(Status))
2541 goto cleanup;
2542 }
2543 }
2544
2545 /* Read identifier */
2546 Status = ZwQueryValueKey(hDeviceKey, &IdentifierU, KeyValuePartialInformation, pValueInformation, ValueInfoLength, &RequiredSize);
2547 if (Status == STATUS_BUFFER_OVERFLOW || Status == STATUS_BUFFER_TOO_SMALL)
2548 {
2549 ExFreePool(pValueInformation);
2550 ValueInfoLength = RequiredSize;
2551 pValueInformation = ExAllocatePool(PagedPool, ValueInfoLength);
2552 if (!pValueInformation)
2553 {
2554 DPRINT("ExAllocatePool() failed\n");
2555 Status = STATUS_NO_MEMORY;
2556 goto cleanup;
2557 }
2558 Status = ZwQueryValueKey(hDeviceKey, &IdentifierU, KeyValuePartialInformation, pValueInformation, ValueInfoLength, &RequiredSize);
2559 }
2560 if (!NT_SUCCESS(Status))
2561 {
2562 if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
2563 {
2564 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status);
2565 goto nextdevice;
2566 }
2567 ValueName.Length = ValueName.MaximumLength = 0;
2568 }
2569 else if (pValueInformation->Type != REG_SZ)
2570 {
2571 DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation->Type, REG_SZ);
2572 goto nextdevice;
2573 }
2574 else
2575 {
2576 /* Assign hardware id to this device */
2577 ValueName.Length = ValueName.MaximumLength = (USHORT)pValueInformation->DataLength;
2578 ValueName.Buffer = (PWCHAR)pValueInformation->Data;
2579 if (ValueName.Length >= sizeof(WCHAR) && ValueName.Buffer[ValueName.Length / sizeof(WCHAR) - 1] == UNICODE_NULL)
2580 ValueName.Length -= sizeof(WCHAR);
2581 }
2582
2583 if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierSerial, FALSE) == 0)
2584 {
2585 pHardwareId = &HardwareIdSerial;
2586 DeviceIndex = DeviceIndexSerial++;
2587 }
2588 else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierKeyboard, FALSE) == 0)
2589 {
2590 pHardwareId = &HardwareIdKeyboard;
2591 DeviceIndex = DeviceIndexKeyboard++;
2592 }
2593 else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierMouse, FALSE) == 0)
2594 {
2595 pHardwareId = &HardwareIdMouse;
2596 DeviceIndex = DeviceIndexMouse++;
2597 }
2598 else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierParallel, FALSE) == 0)
2599 {
2600 pHardwareId = &HardwareIdParallel;
2601 DeviceIndex = DeviceIndexParallel++;
2602 }
2603 else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierFloppy, FALSE) == 0)
2604 {
2605 pHardwareId = &HardwareIdFloppy;
2606 DeviceIndex = DeviceIndexFloppy++;
2607 }
2608 else if (NT_SUCCESS(Status))
2609 {
2610 /* Try to also match the device identifier */
2611 if (RtlCompareUnicodeString(&ValueName, &IdentifierPci, FALSE) == 0)
2612 {
2613 pHardwareId = &HardwareIdPci;
2614 DeviceIndex = DeviceIndexPci++;
2615 }
2616 else if (RtlCompareUnicodeString(&ValueName, &IdentifierIsa, FALSE) == 0)
2617 {
2618 pHardwareId = &HardwareIdIsa;
2619 DeviceIndex = DeviceIndexIsa++;
2620 }
2621 else
2622 {
2623 DPRINT("Unknown device '%wZ'\n", &ValueName);
2624 goto nextdevice;
2625 }
2626 }
2627 else
2628 {
2629 /* Unknown key path */
2630 DPRINT("Unknown key path '%wZ'\n", RelativePath);
2631 goto nextdevice;
2632 }
2633
2634 /* Prepare hardware id key (hardware id value without final \0) */
2635 HardwareIdKey = *pHardwareId;
2636 HardwareIdKey.Length -= sizeof(UNICODE_NULL);
2637
2638 /* Add the detected device to Root key */
2639 InitializeObjectAttributes(&ObjectAttributes, &HardwareIdKey, OBJ_KERNEL_HANDLE, hRootKey, NULL);
2640 Status = ZwCreateKey(
2641 &hLevel1Key,
2642 KEY_CREATE_SUB_KEY,
2643 &ObjectAttributes,
2644 0,
2645 NULL,
2646 ExpInTextModeSetup ? REG_OPTION_VOLATILE : 0,
2647 NULL);
2648 if (!NT_SUCCESS(Status))
2649 {
2650 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
2651 goto nextdevice;
2652 }
2653 swprintf(Level2Name, L"%04lu", DeviceIndex);
2654 RtlInitUnicodeString(&Level2NameU, Level2Name);
2655 InitializeObjectAttributes(&ObjectAttributes, &Level2NameU, OBJ_KERNEL_HANDLE, hLevel1Key, NULL);
2656 Status = ZwCreateKey(
2657 &hLevel2Key,
2658 KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
2659 &ObjectAttributes,
2660 0,
2661 NULL,
2662 ExpInTextModeSetup ? REG_OPTION_VOLATILE : 0,
2663 NULL);
2664 ZwClose(hLevel1Key);
2665 if (!NT_SUCCESS(Status))
2666 {
2667 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
2668 goto nextdevice;
2669 }
2670 DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName, DeviceIndex, &HardwareIdKey);
2671 Status = ZwSetValueKey(hLevel2Key, &HardwareIDU, 0, REG_MULTI_SZ, pHardwareId->Buffer, pHardwareId->MaximumLength);
2672 if (!NT_SUCCESS(Status))
2673 {
2674 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status);
2675 ZwDeleteKey(hLevel2Key);
2676 goto nextdevice;
2677 }
2678 /* Create 'LogConf' subkey */
2679 InitializeObjectAttributes(&ObjectAttributes, &LogConfU, OBJ_KERNEL_HANDLE, hLevel2Key, NULL);
2680 Status = ZwCreateKey(
2681 &hLogConf,
2682 KEY_SET_VALUE,
2683 &ObjectAttributes,
2684 0,
2685 NULL,
2686 REG_OPTION_VOLATILE,
2687 NULL);
2688 if (!NT_SUCCESS(Status))
2689 {
2690 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
2691 ZwDeleteKey(hLevel2Key);
2692 goto nextdevice;
2693 }
2694 if (BootResourcesLength >= sizeof(CM_FULL_RESOURCE_DESCRIPTOR))
2695 {
2696 CmResourceList = ExAllocatePool(PagedPool, BootResourcesLength + sizeof(ULONG));
2697 if (!CmResourceList)
2698 {
2699 ZwClose(hLogConf);
2700 ZwDeleteKey(hLevel2Key);
2701 goto nextdevice;
2702 }
2703
2704 /* Add the list count (1st member of CM_RESOURCE_LIST) */
2705 ListCount = 1;
2706 RtlCopyMemory(CmResourceList,
2707 &ListCount,
2708 sizeof(ULONG));
2709
2710 /* Now add the actual list (2nd member of CM_RESOURCE_LIST) */
2711 RtlCopyMemory(CmResourceList + sizeof(ULONG),
2712 BootResources,
2713 BootResourcesLength);
2714
2715 /* Save boot resources to 'LogConf\BootConfig' */
2716 Status = ZwSetValueKey(hLogConf, &BootConfigU, 0, REG_RESOURCE_LIST, CmResourceList, BootResourcesLength + sizeof(ULONG));
2717 if (!NT_SUCCESS(Status))
2718 {
2719 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status);
2720 ZwClose(hLogConf);
2721 ZwDeleteKey(hLevel2Key);
2722 goto nextdevice;
2723 }
2724 }
2725 ZwClose(hLogConf);
2726
2727 nextdevice:
2728 if (BootResources && BootResources != ParentBootResources)
2729 {
2730 ExFreePool(BootResources);
2731 BootResources = NULL;
2732 }
2733 if (hLevel2Key)
2734 {
2735 ZwClose(hLevel2Key);
2736 hLevel2Key = NULL;
2737 }
2738 if (hDeviceKey)
2739 {
2740 ZwClose(hDeviceKey);
2741 hDeviceKey = NULL;
2742 }
2743 }
2744
2745 Status = STATUS_SUCCESS;
2746
2747 cleanup:
2748 if (hDevicesKey && hDevicesKey != hBaseKey)
2749 ZwClose(hDevicesKey);
2750 if (hDeviceKey)
2751 ZwClose(hDeviceKey);
2752 if (pDeviceInformation)
2753 ExFreePool(pDeviceInformation);
2754 if (pValueInformation)
2755 ExFreePool(pValueInformation);
2756 return Status;
2757 }
2758
2759 static BOOLEAN INIT_FUNCTION
2760 IopIsFirmwareMapperDisabled(VOID)
2761 {
2762 UNICODE_STRING KeyPathU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\SYSTEM\\CURRENTCONTROLSET\\Control\\Pnp");
2763 UNICODE_STRING KeyNameU = RTL_CONSTANT_STRING(L"DisableFirmwareMapper");
2764 OBJECT_ATTRIBUTES ObjectAttributes;
2765 HANDLE hPnpKey;
2766 PKEY_VALUE_PARTIAL_INFORMATION KeyInformation;
2767 ULONG DesiredLength, Length;
2768 ULONG KeyValue = 0;
2769 NTSTATUS Status;
2770
2771 InitializeObjectAttributes(&ObjectAttributes, &KeyPathU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
2772 Status = ZwOpenKey(&hPnpKey, KEY_QUERY_VALUE, &ObjectAttributes);
2773 if (NT_SUCCESS(Status))
2774 {
2775 Status = ZwQueryValueKey(hPnpKey,
2776 &KeyNameU,
2777 KeyValuePartialInformation,
2778 NULL,
2779 0,
2780 &DesiredLength);
2781 if ((Status == STATUS_BUFFER_TOO_SMALL) ||
2782 (Status == STATUS_BUFFER_OVERFLOW))
2783 {
2784 Length = DesiredLength;
2785 KeyInformation = ExAllocatePool(PagedPool, Length);
2786 if (KeyInformation)
2787 {
2788 Status = ZwQueryValueKey(hPnpKey,
2789 &KeyNameU,
2790 KeyValuePartialInformation,
2791 KeyInformation,
2792 Length,
2793 &DesiredLength);
2794 if (NT_SUCCESS(Status) && KeyInformation->DataLength == sizeof(ULONG))
2795 {
2796 KeyValue = (ULONG)(*KeyInformation->Data);
2797 }
2798 else
2799 {
2800 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed\n", &KeyPathU, &KeyNameU);
2801 }
2802
2803 ExFreePool(KeyInformation);
2804 }
2805 else
2806 {
2807 DPRINT1("Failed to allocate memory for registry query\n");
2808 }
2809 }
2810 else
2811 {
2812 DPRINT1("ZwQueryValueKey(%wZ%wZ) failed with status 0x%08lx\n", &KeyPathU, &KeyNameU, Status);
2813 }
2814
2815 ZwClose(hPnpKey);
2816 }
2817 else
2818 {
2819 DPRINT1("ZwOpenKey(%wZ) failed with status 0x%08lx\n", &KeyPathU, Status);
2820 }
2821
2822 DPRINT1("Firmware mapper is %s\n", KeyValue != 0 ? "disabled" : "enabled");
2823
2824 return (KeyValue != 0) ? TRUE : FALSE;
2825 }
2826
2827 NTSTATUS
2828 NTAPI
2829 INIT_FUNCTION
2830 IopUpdateRootKey(VOID)
2831 {
2832 UNICODE_STRING EnumU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
2833 UNICODE_STRING RootPathU = RTL_CONSTANT_STRING(L"Root");
2834 UNICODE_STRING MultiKeyPathU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
2835 OBJECT_ATTRIBUTES ObjectAttributes;
2836 HANDLE hEnum, hRoot;
2837 NTSTATUS Status;
2838
2839 InitializeObjectAttributes(&ObjectAttributes, &EnumU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, NULL, NULL);
2840 Status = ZwCreateKey(&hEnum, KEY_CREATE_SUB_KEY, &ObjectAttributes, 0, NULL, 0, NULL);
2841 if (!NT_SUCCESS(Status))
2842 {
2843 DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status);
2844 return Status;
2845 }
2846
2847 InitializeObjectAttributes(&ObjectAttributes, &RootPathU, OBJ_KERNEL_HANDLE | OBJ_CASE_INSENSITIVE, hEnum, NULL);
2848 Status = ZwCreateKey(&hRoot, KEY_CREATE_SUB_KEY, &ObjectAttributes, 0, NULL, 0, NULL);
2849 ZwClose(hEnum);
2850 if (!NT_SUCCESS(Status))
2851 {
2852 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status);
2853 return Status;
2854 }
2855
2856 if (!IopIsFirmwareMapperDisabled())
2857 {
2858 Status = IopOpenRegistryKeyEx(&hEnum, NULL, &MultiKeyPathU, KEY_ENUMERATE_SUB_KEYS);
2859 if (!NT_SUCCESS(Status))
2860 {
2861 /* Nothing to do, don't return with an error status */
2862 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
2863 ZwClose(hRoot);
2864 return STATUS_SUCCESS;
2865 }
2866 Status = IopEnumerateDetectedDevices(
2867 hEnum,
2868 NULL,
2869 hRoot,
2870 TRUE,
2871 NULL,
2872 0);
2873 ZwClose(hEnum);
2874 }
2875 else
2876 {
2877 /* Enumeration is disabled */
2878 Status = STATUS_SUCCESS;
2879 }
2880
2881 ZwClose(hRoot);
2882
2883 return Status;
2884 }
2885
2886 NTSTATUS
2887 NTAPI
2888 IopOpenRegistryKeyEx(PHANDLE KeyHandle,
2889 HANDLE ParentKey,
2890 PUNICODE_STRING Name,
2891 ACCESS_MASK DesiredAccess)
2892 {
2893 OBJECT_ATTRIBUTES ObjectAttributes;
2894 NTSTATUS Status;
2895
2896 PAGED_CODE();
2897
2898 *KeyHandle = NULL;
2899
2900 InitializeObjectAttributes(&ObjectAttributes,
2901 Name,
2902 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2903 ParentKey,
2904 NULL);
2905
2906 Status = ZwOpenKey(KeyHandle, DesiredAccess, &ObjectAttributes);
2907
2908 return Status;
2909 }
2910
2911 NTSTATUS
2912 NTAPI
2913 IopCreateRegistryKeyEx(OUT PHANDLE Handle,
2914 IN HANDLE RootHandle OPTIONAL,
2915 IN PUNICODE_STRING KeyName,
2916 IN ACCESS_MASK DesiredAccess,
2917 IN ULONG CreateOptions,
2918 OUT PULONG Disposition OPTIONAL)
2919 {
2920 OBJECT_ATTRIBUTES ObjectAttributes;
2921 ULONG KeyDisposition, RootHandleIndex = 0, i = 1, NestedCloseLevel = 0;
2922 USHORT Length;
2923 HANDLE HandleArray[2];
2924 BOOLEAN Recursing = TRUE;
2925 PWCHAR pp, p, p1;
2926 UNICODE_STRING KeyString;
2927 NTSTATUS Status = STATUS_SUCCESS;
2928 PAGED_CODE();
2929
2930 /* P1 is start, pp is end */
2931 p1 = KeyName->Buffer;
2932 pp = (PVOID)((ULONG_PTR)p1 + KeyName->Length);
2933
2934 /* Create the target key */
2935 InitializeObjectAttributes(&ObjectAttributes,
2936 KeyName,
2937 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2938 RootHandle,
2939 NULL);
2940 Status = ZwCreateKey(&HandleArray[i],
2941 DesiredAccess,
2942 &ObjectAttributes,
2943 0,
2944 NULL,
2945 CreateOptions,
2946 &KeyDisposition);
2947
2948 /* Now we check if this failed */
2949 if ((Status == STATUS_OBJECT_NAME_NOT_FOUND) && (RootHandle))
2950 {
2951 /* Target key failed, so we'll need to create its parent. Setup array */
2952 HandleArray[0] = NULL;
2953 HandleArray[1] = RootHandle;
2954
2955 /* Keep recursing for each missing parent */
2956 while (Recursing)
2957 {
2958 /* And if we're deep enough, close the last handle */
2959 if (NestedCloseLevel > 1) ZwClose(HandleArray[RootHandleIndex]);
2960
2961 /* We're setup to ping-pong between the two handle array entries */
2962 RootHandleIndex = i;
2963 i = (i + 1) & 1;
2964
2965 /* Clear the one we're attempting to open now */
2966 HandleArray[i] = NULL;
2967
2968 /* Process the parent key name */
2969 for (p = p1; ((p < pp) && (*p != OBJ_NAME_PATH_SEPARATOR)); p++);
2970 Length = (USHORT)(p - p1) * sizeof(WCHAR);
2971
2972 /* Is there a parent name? */
2973 if (Length)
2974 {
2975 /* Build the unicode string for it */
2976 KeyString.Buffer = p1;
2977 KeyString.Length = KeyString.MaximumLength = Length;
2978
2979 /* Now try opening the parent */
2980 InitializeObjectAttributes(&ObjectAttributes,
2981 &KeyString,
2982 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
2983 HandleArray[RootHandleIndex],
2984 NULL);
2985 Status = ZwCreateKey(&HandleArray[i],
2986 DesiredAccess,
2987 &ObjectAttributes,
2988 0,
2989 NULL,
2990 CreateOptions,
2991 &KeyDisposition);
2992 if (NT_SUCCESS(Status))
2993 {
2994 /* It worked, we have one more handle */
2995 NestedCloseLevel++;
2996 }
2997 else
2998 {
2999 /* Parent key creation failed, abandon loop */
3000 Recursing = FALSE;
3001 continue;
3002 }
3003 }
3004 else
3005 {
3006 /* We don't have a parent name, probably corrupted key name */
3007 Status = STATUS_INVALID_PARAMETER;
3008 Recursing = FALSE;
3009 continue;
3010 }
3011
3012 /* Now see if there's more parents to create */
3013 p1 = p + 1;
3014 if ((p == pp) || (p1 == pp))
3015 {
3016 /* We're done, hopefully successfully, so stop */
3017 Recursing = FALSE;
3018 }
3019 }
3020
3021 /* Outer loop check for handle nesting that requires closing the top handle */
3022 if (NestedCloseLevel > 1) ZwClose(HandleArray[RootHandleIndex]);
3023 }
3024
3025 /* Check if we broke out of the loop due to success */
3026 if (NT_SUCCESS(Status))
3027 {
3028 /* Return the target handle (we closed all the parent ones) and disposition */
3029 *Handle = HandleArray[i];
3030 if (Disposition) *Disposition = KeyDisposition;
3031 }
3032
3033 /* Return the success state */
3034 return Status;
3035 }
3036
3037 NTSTATUS
3038 NTAPI
3039 IopGetRegistryValue(IN HANDLE Handle,
3040 IN PWSTR ValueName,
3041 OUT PKEY_VALUE_FULL_INFORMATION *Information)
3042 {
3043 UNICODE_STRING ValueString;
3044 NTSTATUS Status;
3045 PKEY_VALUE_FULL_INFORMATION FullInformation;
3046 ULONG Size;
3047 PAGED_CODE();
3048
3049 RtlInitUnicodeString(&ValueString, ValueName);
3050
3051 Status = ZwQueryValueKey(Handle,
3052 &ValueString,
3053 KeyValueFullInformation,
3054 NULL,
3055 0,
3056 &Size);
3057 if ((Status != STATUS_BUFFER_OVERFLOW) &&
3058 (Status != STATUS_BUFFER_TOO_SMALL))
3059 {
3060 return Status;
3061 }
3062
3063 FullInformation = ExAllocatePool(NonPagedPool, Size);
3064 if (!FullInformation) return STATUS_INSUFFICIENT_RESOURCES;
3065
3066 Status = ZwQueryValueKey(Handle,
3067 &ValueString,
3068 KeyValueFullInformation,
3069 FullInformation,
3070 Size,
3071 &Size);
3072 if (!NT_SUCCESS(Status))
3073 {
3074 ExFreePool(FullInformation);
3075 return Status;
3076 }
3077
3078 *Information = FullInformation;
3079 return STATUS_SUCCESS;
3080 }
3081
3082 RTL_GENERIC_COMPARE_RESULTS
3083 NTAPI
3084 PiCompareInstancePath(IN PRTL_AVL_TABLE Table,
3085 IN PVOID FirstStruct,
3086 IN PVOID SecondStruct)
3087 {
3088 /* FIXME: TODO */
3089 ASSERT(FALSE);
3090 return 0;
3091 }
3092
3093 //
3094 // The allocation function is called by the generic table package whenever
3095 // it needs to allocate memory for the table.
3096 //
3097
3098 PVOID
3099 NTAPI
3100 PiAllocateGenericTableEntry(IN PRTL_AVL_TABLE Table,
3101 IN CLONG ByteSize)
3102 {
3103 /* FIXME: TODO */
3104 ASSERT(FALSE);
3105 return NULL;
3106 }
3107
3108 VOID
3109 NTAPI
3110 PiFreeGenericTableEntry(IN PRTL_AVL_TABLE Table,
3111 IN PVOID Buffer)
3112 {
3113 /* FIXME: TODO */
3114 ASSERT(FALSE);
3115 }
3116
3117 VOID
3118 NTAPI
3119 PpInitializeDeviceReferenceTable(VOID)
3120 {
3121 /* Setup the guarded mutex and AVL table */
3122 KeInitializeGuardedMutex(&PpDeviceReferenceTableLock);
3123 RtlInitializeGenericTableAvl(
3124 &PpDeviceReferenceTable,
3125 (PRTL_AVL_COMPARE_ROUTINE)PiCompareInstancePath,
3126 (PRTL_AVL_ALLOCATE_ROUTINE)PiAllocateGenericTableEntry,
3127 (PRTL_AVL_FREE_ROUTINE)PiFreeGenericTableEntry,
3128 NULL);
3129 }
3130
3131 BOOLEAN
3132 NTAPI
3133 PiInitPhase0(VOID)
3134 {
3135 /* Initialize the resource when accessing device registry data */
3136 ExInitializeResourceLite(&PpRegistryDeviceResource);
3137
3138 /* Setup the device reference AVL table */
3139 PpInitializeDeviceReferenceTable();
3140 return TRUE;
3141 }
3142
3143 BOOLEAN
3144 NTAPI
3145 PpInitSystem(VOID)
3146 {
3147 /* Check the initialization phase */
3148 switch (ExpInitializationPhase)
3149 {
3150 case 0:
3151
3152 /* Do Phase 0 */
3153 return PiInitPhase0();
3154
3155 case 1:
3156
3157 /* Do Phase 1 */
3158 return TRUE;
3159 //return PiInitPhase1();
3160
3161 default:
3162
3163 /* Don't know any other phase! Bugcheck! */
3164 KeBugCheck(UNEXPECTED_INITIALIZATION_CALL);
3165 return FALSE;
3166 }
3167 }
3168
3169 LONG IopNumberDeviceNodes;
3170
3171 PDEVICE_NODE
3172 NTAPI
3173 PipAllocateDeviceNode(IN PDEVICE_OBJECT PhysicalDeviceObject)
3174 {
3175 PDEVICE_NODE DeviceNode;
3176 PAGED_CODE();
3177
3178 /* Allocate it */
3179 DeviceNode = ExAllocatePoolWithTag(NonPagedPool, sizeof(DEVICE_NODE), 'donD');
3180 if (!DeviceNode) return DeviceNode;
3181
3182 /* Statistics */
3183 InterlockedIncrement(&IopNumberDeviceNodes);
3184
3185 /* Set it up */
3186 RtlZeroMemory(DeviceNode, sizeof(DEVICE_NODE));
3187 DeviceNode->InterfaceType = InterfaceTypeUndefined;
3188 DeviceNode->BusNumber = -1;
3189 DeviceNode->ChildInterfaceType = InterfaceTypeUndefined;
3190 DeviceNode->ChildBusNumber = -1;
3191 DeviceNode->ChildBusTypeIndex = -1;
3192 // KeInitializeEvent(&DeviceNode->EnumerationMutex, SynchronizationEvent, TRUE);
3193 InitializeListHead(&DeviceNode->DeviceArbiterList);
3194 InitializeListHead(&DeviceNode->DeviceTranslatorList);
3195 InitializeListHead(&DeviceNode->TargetDeviceNotify);
3196 InitializeListHead(&DeviceNode->DockInfo.ListEntry);
3197 InitializeListHead(&DeviceNode->PendedSetInterfaceState);
3198
3199 /* Check if there is a PDO */
3200 if (PhysicalDeviceObject)
3201 {
3202 /* Link it and remove the init flag */
3203 DeviceNode->PhysicalDeviceObject = PhysicalDeviceObject;
3204 ((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode = DeviceNode;
3205 PhysicalDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
3206 }
3207
3208 /* Return the node */
3209 return DeviceNode;
3210 }
3211
3212 /* PUBLIC FUNCTIONS **********************************************************/
3213
3214 NTSTATUS
3215 NTAPI
3216 PnpBusTypeGuidGet(IN USHORT Index,
3217 IN LPGUID BusTypeGuid)
3218 {
3219 NTSTATUS Status = STATUS_SUCCESS;
3220
3221 /* Acquire the lock */
3222 ExAcquireFastMutex(&PnpBusTypeGuidList->Lock);
3223
3224 /* Validate size */
3225 if (Index < PnpBusTypeGuidList->GuidCount)
3226 {
3227 /* Copy the data */
3228 RtlCopyMemory(BusTypeGuid, &PnpBusTypeGuidList->Guids[Index], sizeof(GUID));
3229 }
3230 else
3231 {
3232 /* Failure path */
3233 Status = STATUS_OBJECT_NAME_NOT_FOUND;
3234 }
3235
3236 /* Release lock and return status */
3237 ExReleaseFastMutex(&PnpBusTypeGuidList->Lock);
3238 return Status;
3239 }
3240
3241 NTSTATUS
3242 NTAPI
3243 PnpDeviceObjectToDeviceInstance(IN PDEVICE_OBJECT DeviceObject,
3244 IN PHANDLE DeviceInstanceHandle,
3245 IN ACCESS_MASK DesiredAccess)
3246 {
3247 NTSTATUS Status;
3248 HANDLE KeyHandle;
3249 PDEVICE_NODE DeviceNode;
3250 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\ENUM");
3251 PAGED_CODE();
3252
3253 /* Open the enum key */
3254 Status = IopOpenRegistryKeyEx(&KeyHandle,
3255 NULL,
3256 &KeyName,
3257 KEY_READ);
3258 if (!NT_SUCCESS(Status)) return Status;
3259
3260 /* Make sure we have an instance path */
3261 DeviceNode = IopGetDeviceNode(DeviceObject);
3262 if ((DeviceNode) && (DeviceNode->InstancePath.Length))
3263 {
3264 /* Get the instance key */
3265 Status = IopOpenRegistryKeyEx(DeviceInstanceHandle,
3266 KeyHandle,
3267 &DeviceNode->InstancePath,
3268 DesiredAccess);
3269 }
3270 else
3271 {
3272 /* Fail */
3273 Status = STATUS_INVALID_DEVICE_REQUEST;
3274 }
3275
3276 /* Close the handle and return status */
3277 ZwClose(KeyHandle);
3278 return Status;
3279 }
3280
3281 ULONG
3282 NTAPI
3283 PnpDetermineResourceListSize(IN PCM_RESOURCE_LIST ResourceList)
3284 {
3285 ULONG FinalSize, PartialSize, EntrySize, i, j;
3286 PCM_FULL_RESOURCE_DESCRIPTOR FullDescriptor;
3287 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor;
3288
3289 /* If we don't have one, that's easy */
3290 if (!ResourceList) return 0;
3291
3292 /* Start with the minimum size possible */
3293 FinalSize = FIELD_OFFSET(CM_RESOURCE_LIST, List);
3294
3295 /* Loop each full descriptor */
3296 FullDescriptor = ResourceList->List;
3297 for (i = 0; i < ResourceList->Count; i++)
3298 {
3299 /* Start with the minimum size possible */
3300 PartialSize = FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList) +
3301 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors);
3302
3303 /* Loop each partial descriptor */
3304 PartialDescriptor = FullDescriptor->PartialResourceList.PartialDescriptors;
3305 for (j = 0; j < FullDescriptor->PartialResourceList.Count; j++)
3306 {
3307 /* Start with the minimum size possible */
3308 EntrySize = sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
3309
3310 /* Check if there is extra data */
3311 if (PartialDescriptor->Type == CmResourceTypeDeviceSpecific)
3312 {
3313 /* Add that data */
3314 EntrySize += PartialDescriptor->u.DeviceSpecificData.DataSize;
3315 }
3316
3317 /* The size of partial descriptors is bigger */
3318 PartialSize += EntrySize;
3319
3320 /* Go to the next partial descriptor */
3321 PartialDescriptor = (PVOID)((ULONG_PTR)PartialDescriptor + EntrySize);
3322 }
3323
3324 /* The size of full descriptors is bigger */
3325 FinalSize += PartialSize;
3326
3327 /* Go to the next full descriptor */
3328 FullDescriptor = (PVOID)((ULONG_PTR)FullDescriptor + PartialSize);
3329 }
3330
3331 /* Return the final size */
3332 return FinalSize;
3333 }
3334
3335 NTSTATUS
3336 NTAPI
3337 PiGetDeviceRegistryProperty(IN PDEVICE_OBJECT DeviceObject,
3338 IN ULONG ValueType,
3339 IN PWSTR ValueName,
3340 IN PWSTR KeyName,
3341 OUT PVOID Buffer,
3342 IN PULONG BufferLength)
3343 {
3344 NTSTATUS Status;
3345 HANDLE KeyHandle, SubHandle;
3346 UNICODE_STRING KeyString;
3347 PKEY_VALUE_FULL_INFORMATION KeyValueInfo = NULL;
3348 ULONG Length;
3349 PAGED_CODE();
3350
3351 /* Find the instance key */
3352 Status = PnpDeviceObjectToDeviceInstance(DeviceObject, &KeyHandle, KEY_READ);
3353 if (NT_SUCCESS(Status))
3354 {
3355 /* Check for name given by caller */
3356 if (KeyName)
3357 {
3358 /* Open this key */
3359 RtlInitUnicodeString(&KeyString, KeyName);
3360 Status = IopOpenRegistryKeyEx(&SubHandle,
3361 KeyHandle,
3362 &KeyString,
3363 KEY_READ);
3364 if (NT_SUCCESS(Status))
3365 {
3366 /* And use this handle instead */
3367 ZwClose(KeyHandle);
3368 KeyHandle = SubHandle;
3369 }
3370 }
3371
3372 /* Check if sub-key handle succeeded (or no-op if no key name given) */
3373 if (NT_SUCCESS(Status))
3374 {
3375 /* Now get the size of the property */
3376 Status = IopGetRegistryValue(KeyHandle,
3377 ValueName,
3378 &KeyValueInfo);
3379 }
3380
3381 /* Close the key */
3382 ZwClose(KeyHandle);
3383 }
3384
3385 /* Fail if any of the registry operations failed */
3386 if (!NT_SUCCESS(Status)) return Status;
3387
3388 /* Check how much data we have to copy */
3389 Length = KeyValueInfo->DataLength;
3390 if (*BufferLength >= Length)
3391 {
3392 /* Check for a match in the value type */
3393 if (KeyValueInfo->Type == ValueType)
3394 {
3395 /* Copy the data */
3396 RtlCopyMemory(Buffer,
3397 (PVOID)((ULONG_PTR)KeyValueInfo +
3398 KeyValueInfo->DataOffset),
3399 Length);
3400 }
3401 else
3402 {
3403 /* Invalid registry property type, fail */
3404 Status = STATUS_INVALID_PARAMETER_2;
3405 }
3406 }
3407 else
3408 {
3409 /* Buffer is too small to hold data */
3410 Status = STATUS_BUFFER_TOO_SMALL;
3411 }
3412
3413 /* Return the required buffer length, free the buffer, and return status */
3414 *BufferLength = Length;
3415 ExFreePool(KeyValueInfo);
3416 return Status;
3417 }
3418
3419 #define PIP_RETURN_DATA(x, y) {ReturnLength = x; Data = y; Status = STATUS_SUCCESS; break;}
3420 #define PIP_REGISTRY_DATA(x, y) {ValueName = x; ValueType = y; break;}
3421 #define PIP_UNIMPLEMENTED() {UNIMPLEMENTED; while(TRUE); break;}
3422
3423 /*
3424 * @implemented
3425 */
3426 NTSTATUS
3427 NTAPI
3428 IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject,
3429 IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
3430 IN ULONG BufferLength,
3431 OUT PVOID PropertyBuffer,
3432 OUT PULONG ResultLength)
3433 {
3434 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
3435 DEVICE_CAPABILITIES DeviceCaps;
3436 ULONG ReturnLength = 0, Length = 0, ValueType;
3437 PWCHAR ValueName = NULL, EnumeratorNameEnd, DeviceInstanceName;
3438 PVOID Data = NULL;
3439 NTSTATUS Status = STATUS_BUFFER_TOO_SMALL;
3440 GUID BusTypeGuid;
3441 POBJECT_NAME_INFORMATION ObjectNameInfo = NULL;
3442 BOOLEAN NullTerminate = FALSE;
3443
3444 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject, DeviceProperty);
3445
3446 /* Assume failure */
3447 *ResultLength = 0;
3448
3449 /* Only PDOs can call this */
3450 if (!DeviceNode) return STATUS_INVALID_DEVICE_REQUEST;
3451
3452 /* Handle all properties */
3453 switch (DeviceProperty)
3454 {
3455 case DevicePropertyBusTypeGuid:
3456
3457 /* Get the GUID from the internal cache */
3458 Status = PnpBusTypeGuidGet(DeviceNode->ChildBusTypeIndex, &BusTypeGuid);
3459 if (!NT_SUCCESS(Status)) return Status;
3460
3461 /* This is the format of the returned data */
3462 PIP_RETURN_DATA(sizeof(GUID), &BusTypeGuid);
3463
3464 case DevicePropertyLegacyBusType:
3465
3466 /* Validate correct interface type */
3467 if (DeviceNode->ChildInterfaceType == InterfaceTypeUndefined)
3468 return STATUS_OBJECT_NAME_NOT_FOUND;
3469
3470 /* This is the format of the returned data */
3471 PIP_RETURN_DATA(sizeof(INTERFACE_TYPE), &DeviceNode->ChildInterfaceType);
3472
3473 case DevicePropertyBusNumber:
3474
3475 /* Validate correct bus number */
3476 if ((DeviceNode->ChildBusNumber & 0x80000000) == 0x80000000)
3477 return STATUS_OBJECT_NAME_NOT_FOUND;
3478
3479 /* This is the format of the returned data */
3480 PIP_RETURN_DATA(sizeof(ULONG), &DeviceNode->ChildBusNumber);
3481
3482 case DevicePropertyEnumeratorName:
3483
3484 /* Get the instance path */
3485 DeviceInstanceName = DeviceNode->InstancePath.Buffer;
3486
3487 /* Sanity checks */
3488 ASSERT((BufferLength & 1) == 0);
3489 ASSERT(DeviceInstanceName != NULL);
3490
3491 /* Get the name from the path */
3492 EnumeratorNameEnd = wcschr(DeviceInstanceName, OBJ_NAME_PATH_SEPARATOR);
3493 ASSERT(EnumeratorNameEnd);
3494
3495 /* This string needs to be NULL-terminated */
3496 NullTerminate = TRUE;
3497
3498 /* This is the format of the returned data */
3499 PIP_RETURN_DATA((ULONG)(EnumeratorNameEnd - DeviceInstanceName) * sizeof(WCHAR),
3500 DeviceInstanceName);
3501
3502 case DevicePropertyAddress:
3503
3504 /* Query the device caps */
3505 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps);
3506 if (!NT_SUCCESS(Status) || (DeviceCaps.Address == MAXULONG))
3507 return STATUS_OBJECT_NAME_NOT_FOUND;
3508
3509 /* This is the format of the returned data */
3510 PIP_RETURN_DATA(sizeof(ULONG), &DeviceCaps.Address);
3511
3512 case DevicePropertyBootConfigurationTranslated:
3513
3514 /* Validate we have resources */
3515 if (!DeviceNode->BootResources)
3516 // if (!DeviceNode->BootResourcesTranslated) // FIXFIX: Need this field
3517 {
3518 /* No resources will still fake success, but with 0 bytes */
3519 *ResultLength = 0;
3520 return STATUS_SUCCESS;
3521 }
3522
3523 /* This is the format of the returned data */
3524 PIP_RETURN_DATA(PnpDetermineResourceListSize(DeviceNode->BootResources), // FIXFIX: Should use BootResourcesTranslated
3525 DeviceNode->BootResources); // FIXFIX: Should use BootResourcesTranslated
3526
3527 case DevicePropertyPhysicalDeviceObjectName:
3528
3529 /* Sanity check for Unicode-sized string */
3530 ASSERT((BufferLength & 1) == 0);
3531
3532 /* Allocate name buffer */
3533 Length = BufferLength + sizeof(OBJECT_NAME_INFORMATION);
3534 ObjectNameInfo = ExAllocatePool(PagedPool, Length);
3535 if (!ObjectNameInfo) return STATUS_INSUFFICIENT_RESOURCES;
3536
3537 /* Query the PDO name */
3538 Status = ObQueryNameString(DeviceObject,
3539 ObjectNameInfo,
3540 Length,
3541 ResultLength);
3542 if (Status == STATUS_INFO_LENGTH_MISMATCH)
3543 {
3544 /* It's up to the caller to try again */
3545 Status = STATUS_BUFFER_TOO_SMALL;
3546 }
3547
3548 /* This string needs to be NULL-terminated */
3549 NullTerminate = TRUE;
3550
3551 /* Return if successful */
3552 if (NT_SUCCESS(Status)) PIP_RETURN_DATA(ObjectNameInfo->Name.Length,
3553 ObjectNameInfo->Name.Buffer);
3554
3555 /* Let the caller know how big the name is */
3556 *ResultLength -= sizeof(OBJECT_NAME_INFORMATION);
3557 break;
3558
3559 /* Handle the registry-based properties */
3560 case DevicePropertyUINumber:
3561 PIP_REGISTRY_DATA(REGSTR_VAL_UI_NUMBER, REG_DWORD);
3562 case DevicePropertyLocationInformation:
3563 PIP_REGISTRY_DATA(REGSTR_VAL_LOCATION_INFORMATION, REG_SZ);
3564 case DevicePropertyDeviceDescription:
3565 PIP_REGISTRY_DATA(REGSTR_VAL_DEVDESC, REG_SZ);
3566 case DevicePropertyHardwareID:
3567 PIP_REGISTRY_DATA(REGSTR_VAL_HARDWAREID, REG_MULTI_SZ);
3568 case DevicePropertyCompatibleIDs:
3569 PIP_REGISTRY_DATA(REGSTR_VAL_COMPATIBLEIDS, REG_MULTI_SZ);
3570 case DevicePropertyBootConfiguration:
3571 PIP_REGISTRY_DATA(REGSTR_VAL_BOOTCONFIG, REG_RESOURCE_LIST);
3572 case DevicePropertyClassName:
3573 PIP_REGISTRY_DATA(REGSTR_VAL_CLASS, REG_SZ);
3574 case DevicePropertyClassGuid:
3575 PIP_REGISTRY_DATA(REGSTR_VAL_CLASSGUID, REG_SZ);
3576 case DevicePropertyDriverKeyName:
3577 PIP_REGISTRY_DATA(REGSTR_VAL_DRIVER, REG_SZ);
3578 case DevicePropertyManufacturer:
3579 PIP_REGISTRY_DATA(REGSTR_VAL_MFG, REG_SZ);
3580 case DevicePropertyFriendlyName:
3581 PIP_REGISTRY_DATA(REGSTR_VAL_FRIENDLYNAME, REG_SZ);
3582 case DevicePropertyContainerID:
3583 //PIP_REGISTRY_DATA(REGSTR_VAL_CONTAINERID, REG_SZ); // Win7
3584 PIP_UNIMPLEMENTED();
3585 case DevicePropertyRemovalPolicy:
3586 PIP_UNIMPLEMENTED();
3587 case DevicePropertyInstallState:
3588 PIP_UNIMPLEMENTED();
3589 case DevicePropertyResourceRequirements:
3590 PIP_UNIMPLEMENTED();
3591 case DevicePropertyAllocatedResources:
3592 PIP_UNIMPLEMENTED();
3593 default:
3594 return STATUS_INVALID_PARAMETER_2;
3595 }
3596
3597 /* Having a registry value name implies registry data */
3598 if (ValueName)
3599 {
3600 /* We know up-front how much data to expect */
3601 *ResultLength = BufferLength;
3602
3603 /* Go get the data, use the LogConf subkey if necessary */
3604 Status = PiGetDeviceRegistryProperty(DeviceObject,
3605 ValueType,
3606 ValueName,
3607 (DeviceProperty ==
3608 DevicePropertyBootConfiguration) ?
3609 L"LogConf": NULL,
3610 PropertyBuffer,
3611 ResultLength);
3612 }
3613 else if (NT_SUCCESS(Status))
3614 {
3615 /* We know up-front how much data to expect, check the caller's buffer */
3616 *ResultLength = ReturnLength + (NullTerminate ? sizeof(UNICODE_NULL) : 0);
3617 if (*ResultLength <= BufferLength)
3618 {
3619 /* Buffer is all good, copy the data */
3620 RtlCopyMemory(PropertyBuffer, Data, ReturnLength);
3621
3622 /* Check if we need to NULL-terminate the string */
3623 if (NullTerminate)
3624 {
3625 /* Terminate the string */
3626 ((PWCHAR)PropertyBuffer)[ReturnLength / sizeof(WCHAR)] = UNICODE_NULL;
3627 }
3628
3629 /* This is the success path */
3630 Status = STATUS_SUCCESS;
3631 }
3632 else
3633 {
3634 /* Failure path */
3635 Status = STATUS_BUFFER_TOO_SMALL;
3636 }
3637 }
3638
3639 /* Free any allocation we may have made, and return the status code */
3640 if (ObjectNameInfo) ExFreePool(ObjectNameInfo);
3641 return Status;
3642 }
3643
3644 /*
3645 * @implemented
3646 */
3647 VOID
3648 NTAPI
3649 IoInvalidateDeviceState(IN PDEVICE_OBJECT PhysicalDeviceObject)
3650 {
3651 PDEVICE_NODE DeviceNode = IopGetDeviceNode(PhysicalDeviceObject);
3652 IO_STACK_LOCATION Stack;
3653 ULONG PnPFlags;
3654 NTSTATUS Status;
3655 IO_STATUS_BLOCK IoStatusBlock;
3656
3657 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
3658 Stack.MajorFunction = IRP_MJ_PNP;
3659 Stack.MinorFunction = IRP_MN_QUERY_PNP_DEVICE_STATE;
3660
3661 Status = IopSynchronousCall(PhysicalDeviceObject, &Stack, (PVOID*)&PnPFlags);
3662 if (!NT_SUCCESS(Status))
3663 {
3664 DPRINT1("IRP_MN_QUERY_PNP_DEVICE_STATE failed with status 0x%x\n", Status);
3665 return;
3666 }
3667
3668 if (PnPFlags & PNP_DEVICE_NOT_DISABLEABLE)
3669 DeviceNode->UserFlags |= DNUF_NOT_DISABLEABLE;
3670 else
3671 DeviceNode->UserFlags &= ~DNUF_NOT_DISABLEABLE;
3672
3673 if (PnPFlags & PNP_DEVICE_DONT_DISPLAY_IN_UI)
3674 DeviceNode->UserFlags |= DNUF_DONT_SHOW_IN_UI;
3675 else
3676 DeviceNode->UserFlags &= ~DNUF_DONT_SHOW_IN_UI;
3677
3678 if ((PnPFlags & PNP_DEVICE_REMOVED) ||
3679 ((PnPFlags & PNP_DEVICE_FAILED) && !(PnPFlags & PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED)))
3680 {
3681 /* Surprise removal */
3682
3683 IopSendSurpriseRemoval(PhysicalDeviceObject);
3684
3685 /* Tell the user-mode PnP manager that a device was removed */
3686 IopQueueTargetDeviceEvent(&GUID_DEVICE_SURPRISE_REMOVAL,
3687 &DeviceNode->InstancePath);
3688
3689 IopSendRemoveDevice(PhysicalDeviceObject);
3690 }
3691 else if ((PnPFlags & PNP_DEVICE_FAILED) && (PnPFlags & PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED))
3692 {
3693 /* Stop for resource rebalance */
3694
3695 Status = IopStopDevice(DeviceNode);
3696 if (!NT_SUCCESS(Status))
3697 {
3698 DPRINT1("Failed to stop device for rebalancing\n");
3699
3700 /* Stop failed so don't rebalance */
3701 PnPFlags &= ~PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED;
3702 }
3703 }
3704
3705 /* Resource rebalance */
3706 if (PnPFlags & PNP_DEVICE_RESOURCE_REQUIREMENTS_CHANGED)
3707 {
3708 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
3709
3710 Status = IopInitiatePnpIrp(PhysicalDeviceObject,
3711 &IoStatusBlock,
3712 IRP_MN_QUERY_RESOURCES,
3713 NULL);
3714 if (NT_SUCCESS(Status) && IoStatusBlock.Information)
3715 {
3716 DeviceNode->BootResources =
3717 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
3718 IopDeviceNodeSetFlag(DeviceNode, DNF_HAS_BOOT_CONFIG);
3719 }
3720 else
3721 {
3722 DPRINT("IopInitiatePnpIrp() failed (Status %x) or IoStatusBlock.Information=NULL\n", Status);
3723 DeviceNode->BootResources = NULL;
3724 }
3725
3726 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
3727
3728 Status = IopInitiatePnpIrp(PhysicalDeviceObject,
3729 &IoStatusBlock,
3730 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
3731 NULL);
3732 if (NT_SUCCESS(Status))
3733 {
3734 DeviceNode->ResourceRequirements =
3735 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
3736 }
3737 else
3738 {
3739 DPRINT("IopInitiatePnpIrp() failed (Status %08lx)\n", Status);
3740 DeviceNode->ResourceRequirements = NULL;
3741 }
3742
3743 /* IRP_MN_FILTER_RESOURCE_REQUIREMENTS is called indirectly by IopStartDevice */
3744 if (IopStartDevice(DeviceNode) != STATUS_SUCCESS)
3745 {
3746 DPRINT1("Restart after resource rebalance failed\n");
3747
3748 DeviceNode->Flags &= ~(DNF_STARTED | DNF_START_REQUEST_PENDING);
3749 DeviceNode->Flags |= DNF_START_FAILED;
3750
3751 IopRemoveDevice(DeviceNode);
3752 }
3753 }
3754 }
3755
3756 /**
3757 * @name IoOpenDeviceRegistryKey
3758 *
3759 * Open a registry key unique for a specified driver or device instance.
3760 *
3761 * @param DeviceObject Device to get the registry key for.
3762 * @param DevInstKeyType Type of the key to return.
3763 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
3764 * @param DevInstRegKey Handle to the opened registry key on
3765 * successful return.
3766 *
3767 * @return Status.
3768 *
3769 * @implemented
3770 */
3771 NTSTATUS
3772 NTAPI
3773 IoOpenDeviceRegistryKey(IN PDEVICE_OBJECT DeviceObject,
3774 IN ULONG DevInstKeyType,
3775 IN ACCESS_MASK DesiredAccess,
3776 OUT PHANDLE DevInstRegKey)
3777 {
3778 static WCHAR RootKeyName[] =
3779 L"\\Registry\\Machine\\System\\CurrentControlSet\\";
3780 static WCHAR ProfileKeyName[] =
3781 L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
3782 static WCHAR ClassKeyName[] = L"Control\\Class\\";
3783 static WCHAR EnumKeyName[] = L"Enum\\";
3784 static WCHAR DeviceParametersKeyName[] = L"Device Parameters";
3785 ULONG KeyNameLength;
3786 LPWSTR KeyNameBuffer;
3787 UNICODE_STRING KeyName;
3788 ULONG DriverKeyLength;
3789 OBJECT_ATTRIBUTES ObjectAttributes;
3790 PDEVICE_NODE DeviceNode = NULL;
3791 NTSTATUS Status;
3792
3793 DPRINT("IoOpenDeviceRegistryKey() called\n");
3794
3795 if ((DevInstKeyType & (PLUGPLAY_REGKEY_DEVICE | PLUGPLAY_REGKEY_DRIVER)) == 0)
3796 {
3797 DPRINT1("IoOpenDeviceRegistryKey(): got wrong params, exiting... \n");
3798 return STATUS_INVALID_PARAMETER;
3799 }
3800
3801 if (!IopIsValidPhysicalDeviceObject(DeviceObject))
3802 return STATUS_INVALID_DEVICE_REQUEST;
3803 DeviceNode = IopGetDeviceNode(DeviceObject);
3804
3805 /*
3806 * Calculate the length of the base key name. This is the full
3807 * name for driver key or the name excluding "Device Parameters"
3808 * subkey for device key.
3809 */
3810
3811 KeyNameLength = sizeof(RootKeyName);
3812 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
3813 KeyNameLength += sizeof(ProfileKeyName) - sizeof(UNICODE_NULL);
3814 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
3815 {
3816 KeyNameLength += sizeof(ClassKeyName) - sizeof(UNICODE_NULL);
3817 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
3818 0, NULL, &DriverKeyLength);
3819 if (Status != STATUS_BUFFER_TOO_SMALL)
3820 return Status;
3821 KeyNameLength += DriverKeyLength;
3822 }
3823 else
3824 {
3825 KeyNameLength += sizeof(EnumKeyName) - sizeof(UNICODE_NULL) +
3826 DeviceNode->InstancePath.Length;
3827 }
3828
3829 /*
3830 * Now allocate the buffer for the key name...
3831 */
3832
3833 KeyNameBuffer = ExAllocatePool(PagedPool, KeyNameLength);
3834 if (KeyNameBuffer == NULL)
3835 return STATUS_INSUFFICIENT_RESOURCES;
3836
3837 KeyName.Length = 0;
3838 KeyName.MaximumLength = (USHORT)KeyNameLength;
3839 KeyName.Buffer = KeyNameBuffer;
3840
3841 /*
3842 * ...and build the key name.
3843 */
3844
3845 KeyName.Length += sizeof(RootKeyName) - sizeof(UNICODE_NULL);
3846 RtlCopyMemory(KeyNameBuffer, RootKeyName, KeyName.Length);
3847
3848 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
3849 RtlAppendUnicodeToString(&KeyName, ProfileKeyName);
3850
3851 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
3852 {
3853 RtlAppendUnicodeToString(&KeyName, ClassKeyName);
3854 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
3855 DriverKeyLength, KeyNameBuffer +
3856 (KeyName.Length / sizeof(WCHAR)),
3857 &DriverKeyLength);
3858 if (!NT_SUCCESS(Status))
3859 {
3860 DPRINT1("Call to IoGetDeviceProperty() failed with Status 0x%08lx\n", Status);
3861 ExFreePool(KeyNameBuffer);
3862 return Status;
3863 }
3864 KeyName.Length += (USHORT)DriverKeyLength - sizeof(UNICODE_NULL);
3865 }
3866 else
3867 {
3868 RtlAppendUnicodeToString(&KeyName, EnumKeyName);
3869 Status = RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->InstancePath);
3870 if (DeviceNode->InstancePath.Length == 0)
3871 {
3872 ExFreePool(KeyNameBuffer);
3873 return Status;
3874 }
3875 }
3876
3877 /*
3878 * Open the base key.
3879 */
3880 Status = IopOpenRegistryKeyEx(DevInstRegKey, NULL, &KeyName, DesiredAccess);
3881 if (!NT_SUCCESS(Status))
3882 {
3883 DPRINT1("IoOpenDeviceRegistryKey(%wZ): Base key doesn't exist, exiting... (Status 0x%08lx)\n", &KeyName, Status);
3884 ExFreePool(KeyNameBuffer);
3885 return Status;
3886 }
3887 ExFreePool(KeyNameBuffer);
3888
3889 /*
3890 * For driver key we're done now.
3891 */
3892
3893 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
3894 return Status;
3895
3896 /*
3897 * Let's go further. For device key we must open "Device Parameters"
3898 * subkey and create it if it doesn't exist yet.
3899 */
3900
3901 RtlInitUnicodeString(&KeyName, DeviceParametersKeyName);
3902 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
3903 OBJ_CASE_INSENSITIVE, *DevInstRegKey, NULL);
3904 Status = ZwCreateKey(DevInstRegKey, DesiredAccess, &ObjectAttributes,
3905 0, NULL, ExpInTextModeSetup ? REG_OPTION_VOLATILE : 0, NULL);
3906 ZwClose(ObjectAttributes.RootDirectory);
3907
3908 return Status;
3909 }
3910
3911 static
3912 NTSTATUS
3913 IopQueryRemoveChildDevices(PDEVICE_NODE ParentDeviceNode)
3914 {
3915 PDEVICE_NODE ChildDeviceNode, NextDeviceNode, FailedRemoveDevice;
3916 NTSTATUS Status;
3917 KIRQL OldIrql;
3918
3919 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3920 ChildDeviceNode = ParentDeviceNode->Child;
3921 while (ChildDeviceNode != NULL)
3922 {
3923 NextDeviceNode = ChildDeviceNode->Sibling;
3924 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3925
3926 Status = IopPrepareDeviceForRemoval(ChildDeviceNode->PhysicalDeviceObject);
3927 if (!NT_SUCCESS(Status))
3928 {
3929 FailedRemoveDevice = ChildDeviceNode;
3930 goto cleanup;
3931 }
3932
3933 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3934 ChildDeviceNode = NextDeviceNode;
3935 }
3936 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3937
3938 return STATUS_SUCCESS;
3939
3940 cleanup:
3941 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3942 ChildDeviceNode = ParentDeviceNode->Child;
3943 while (ChildDeviceNode != NULL)
3944 {
3945 NextDeviceNode = ChildDeviceNode->Sibling;
3946 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3947
3948 IopCancelPrepareDeviceForRemoval(ChildDeviceNode->PhysicalDeviceObject);
3949
3950 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
3951 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
3952 if (ChildDeviceNode == FailedRemoveDevice)
3953 return Status;
3954
3955 ChildDeviceNode = NextDeviceNode;
3956
3957 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3958 }
3959 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3960
3961 return Status;
3962 }
3963
3964 static
3965 VOID
3966 IopSendRemoveChildDevices(PDEVICE_NODE ParentDeviceNode)
3967 {
3968 PDEVICE_NODE ChildDeviceNode, NextDeviceNode;
3969 KIRQL OldIrql;
3970
3971 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3972 ChildDeviceNode = ParentDeviceNode->Child;
3973 while (ChildDeviceNode != NULL)
3974 {
3975 NextDeviceNode = ChildDeviceNode->Sibling;
3976 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3977
3978 IopSendRemoveDevice(ChildDeviceNode->PhysicalDeviceObject);
3979
3980 ChildDeviceNode = NextDeviceNode;
3981
3982 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3983 }
3984 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
3985 }
3986
3987 static
3988 VOID
3989 IopCancelRemoveChildDevices(PDEVICE_NODE ParentDeviceNode)
3990 {
3991 PDEVICE_NODE ChildDeviceNode, NextDeviceNode;
3992 KIRQL OldIrql;
3993
3994 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
3995 ChildDeviceNode = ParentDeviceNode->Child;
3996 while (ChildDeviceNode != NULL)
3997 {
3998 NextDeviceNode = ChildDeviceNode->Sibling;
3999 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
4000
4001 IopCancelPrepareDeviceForRemoval(ChildDeviceNode->PhysicalDeviceObject);
4002
4003 ChildDeviceNode = NextDeviceNode;
4004
4005 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
4006 }
4007 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
4008 }
4009
4010 static
4011 NTSTATUS
4012 IopQueryRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations)
4013 {
4014 /* This function DOES NOT dereference the device objects on SUCCESS
4015 * but it DOES dereference device objects on FAILURE */
4016
4017 ULONG i, j;
4018 NTSTATUS Status;
4019
4020 for (i = 0; i < DeviceRelations->Count; i++)
4021 {
4022 Status = IopPrepareDeviceForRemoval(DeviceRelations->Objects[i]);
4023 if (!NT_SUCCESS(Status))
4024 {
4025 j = i;
4026 goto cleanup;
4027 }
4028 }
4029
4030 return STATUS_SUCCESS;
4031
4032 cleanup:
4033 /* IRP_MN_CANCEL_REMOVE_DEVICE is also sent to the device
4034 * that failed the IRP_MN_QUERY_REMOVE_DEVICE request */
4035 for (i = 0; i <= j; i++)
4036 {
4037 IopCancelPrepareDeviceForRemoval(DeviceRelations->Objects[i]);
4038 ObDereferenceObject(DeviceRelations->Objects[i]);
4039 DeviceRelations->Objects[i] = NULL;
4040 }
4041 for (; i < DeviceRelations->Count; i++)
4042 {
4043 ObDereferenceObject(DeviceRelations->Objects[i]);
4044 DeviceRelations->Objects[i] = NULL;
4045 }
4046 ExFreePool(DeviceRelations);
4047
4048 return Status;
4049 }
4050
4051 static
4052 VOID
4053 IopSendRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations)
4054 {
4055 /* This function DOES dereference the device objects in all cases */
4056
4057 ULONG i;
4058
4059 for (i = 0; i < DeviceRelations->Count; i++)
4060 {
4061 IopSendRemoveDevice(DeviceRelations->Objects[i]);
4062 ObDereferenceObject(DeviceRelations->Objects[i]);
4063 DeviceRelations->Objects[i] = NULL;
4064 }
4065
4066 ExFreePool(DeviceRelations);
4067 }
4068
4069 static
4070 VOID
4071 IopCancelRemoveDeviceRelations(PDEVICE_RELATIONS DeviceRelations)
4072 {
4073 /* This function DOES dereference the device objects in all cases */
4074
4075 ULONG i;
4076
4077 for (i = 0; i < DeviceRelations->Count; i++)
4078 {
4079 IopCancelPrepareDeviceForRemoval(DeviceRelations->Objects[i]);
4080 ObDereferenceObject(DeviceRelations->Objects[i]);
4081 DeviceRelations->Objects[i] = NULL;
4082 }
4083
4084 ExFreePool(DeviceRelations);
4085 }
4086
4087 VOID
4088 IopCancelPrepareDeviceForRemoval(PDEVICE_OBJECT DeviceObject)
4089 {
4090 IO_STACK_LOCATION Stack;
4091 IO_STATUS_BLOCK IoStatusBlock;
4092 PDEVICE_RELATIONS DeviceRelations;
4093 NTSTATUS Status;
4094
4095 IopCancelRemoveDevice(DeviceObject);
4096
4097 Stack.Parameters.QueryDeviceRelations.Type = RemovalRelations;
4098
4099 Status = IopInitiatePnpIrp(DeviceObject,
4100 &IoStatusBlock,
4101 IRP_MN_QUERY_DEVICE_RELATIONS,
4102 &Stack);
4103 if (!NT_SUCCESS(Status))
4104 {
4105 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status);
4106 DeviceRelations = NULL;
4107 }
4108 else
4109 {
4110 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
4111 }
4112
4113 if (DeviceRelations)
4114 IopCancelRemoveDeviceRelations(DeviceRelations);
4115 }
4116
4117 NTSTATUS
4118 IopPrepareDeviceForRemoval(IN PDEVICE_OBJECT DeviceObject)
4119 {
4120 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
4121 IO_STACK_LOCATION Stack;
4122 IO_STATUS_BLOCK IoStatusBlock;
4123 PDEVICE_RELATIONS DeviceRelations;
4124 NTSTATUS Status;
4125
4126 if (DeviceNode->UserFlags & DNUF_NOT_DISABLEABLE)
4127 {
4128 DPRINT1("Removal not allowed for %wZ\n", &DeviceNode->InstancePath);
4129 return STATUS_UNSUCCESSFUL;
4130 }
4131
4132 if (IopQueryRemoveDevice(DeviceObject) != STATUS_SUCCESS)
4133 {
4134 DPRINT1("Removal vetoed by failing the query remove request\n");
4135
4136 IopCancelRemoveDevice(DeviceObject);
4137
4138 return STATUS_UNSUCCESSFUL;
4139 }
4140
4141 Stack.Parameters.QueryDeviceRelations.Type = RemovalRelations;
4142
4143 Status = IopInitiatePnpIrp(DeviceObject,
4144 &IoStatusBlock,
4145 IRP_MN_QUERY_DEVICE_RELATIONS,
4146 &Stack);
4147 if (!NT_SUCCESS(Status))
4148 {
4149 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status);
4150 DeviceRelations = NULL;
4151 }
4152 else
4153 {
4154 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
4155 }
4156
4157 if (DeviceRelations)
4158 {
4159 Status = IopQueryRemoveDeviceRelations(DeviceRelations);
4160 if (!NT_SUCCESS(Status))
4161 return Status;
4162 }
4163
4164 Status = IopQueryRemoveChildDevices(DeviceNode);
4165 if (!NT_SUCCESS(Status))
4166 {
4167 if (DeviceRelations)
4168 IopCancelRemoveDeviceRelations(DeviceRelations);
4169 return Status;
4170 }
4171
4172 if (DeviceRelations)
4173 IopSendRemoveDeviceRelations(DeviceRelations);
4174 IopSendRemoveChildDevices(DeviceNode);
4175
4176 return STATUS_SUCCESS;
4177 }
4178
4179 NTSTATUS
4180 IopRemoveDevice(PDEVICE_NODE DeviceNode)
4181 {
4182 NTSTATUS Status;
4183
4184 DPRINT("Removing device: %wZ\n", &DeviceNode->InstancePath);
4185
4186 Status = IopPrepareDeviceForRemoval(DeviceNode->PhysicalDeviceObject);
4187 if (NT_SUCCESS(Status))
4188 {
4189 IopSendRemoveDevice(DeviceNode->PhysicalDeviceObject);
4190 IopQueueTargetDeviceEvent(&GUID_DEVICE_SAFE_REMOVAL,
4191 &DeviceNode->InstancePath);
4192 DeviceNode->Flags |= DNF_WILL_BE_REMOVED;
4193 return STATUS_SUCCESS;
4194 }
4195
4196 return Status;
4197 }
4198
4199 /*
4200 * @implemented
4201 */
4202 VOID
4203 NTAPI
4204 IoRequestDeviceEject(IN PDEVICE_OBJECT PhysicalDeviceObject)
4205 {
4206 PDEVICE_NODE DeviceNode = IopGetDeviceNode(PhysicalDeviceObject);
4207 PDEVICE_RELATIONS DeviceRelations;
4208 IO_STATUS_BLOCK IoStatusBlock;
4209 IO_STACK_LOCATION Stack;
4210 DEVICE_CAPABILITIES Capabilities;
4211 NTSTATUS Status;
4212
4213 IopQueueTargetDeviceEvent(&GUID_DEVICE_KERNEL_INITIATED_EJECT,
4214 &DeviceNode->InstancePath);
4215
4216 if (IopQueryDeviceCapabilities(DeviceNode, &Capabilities) != STATUS_SUCCESS)
4217 {
4218 goto cleanup;
4219 }
4220
4221 Stack.Parameters.QueryDeviceRelations.Type = EjectionRelations;
4222
4223 Status = IopInitiatePnpIrp(PhysicalDeviceObject,
4224 &IoStatusBlock,
4225 IRP_MN_QUERY_DEVICE_RELATIONS,
4226 &Stack);
4227 if (!NT_SUCCESS(Status))
4228 {
4229 DPRINT("IopInitiatePnpIrp() failed with status 0x%08lx\n", Status);
4230 DeviceRelations = NULL;
4231 }
4232 else
4233 {
4234 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
4235 }
4236
4237 if (DeviceRelations)
4238 {
4239 Status = IopQueryRemoveDeviceRelations(DeviceRelations);
4240 if (!NT_SUCCESS(Status))
4241 goto cleanup;
4242 }
4243
4244 Status = IopQueryRemoveChildDevices(DeviceNode);
4245 if (!NT_SUCCESS(Status))
4246 {
4247 if (DeviceRelations)
4248 IopCancelRemoveDeviceRelations(DeviceRelations);
4249 goto cleanup;
4250 }
4251
4252 if (IopPrepareDeviceForRemoval(PhysicalDeviceObject) != STATUS_SUCCESS)
4253 {
4254 if (DeviceRelations)
4255 IopCancelRemoveDeviceRelations(DeviceRelations);
4256 IopCancelRemoveChildDevices(DeviceNode);
4257 goto cleanup;
4258 }
4259
4260 if (DeviceRelations)
4261 IopSendRemoveDeviceRelations(DeviceRelations);
4262 IopSendRemoveChildDevices(DeviceNode);
4263
4264 if (Capabilities.EjectSupported)
4265 {
4266 if (IopSendEject(PhysicalDeviceObject) != STATUS_SUCCESS)
4267 {
4268 goto cleanup;
4269 }
4270 }
4271 else
4272 {
4273 DeviceNode->Flags |= DNF_DISABLED;
4274 }
4275
4276 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT,
4277 &DeviceNode->InstancePath);
4278
4279 return;
4280
4281 cleanup:
4282 IopQueueTargetDeviceEvent(&GUID_DEVICE_EJECT_VETOED,
4283 &DeviceNode->InstancePath);
4284 }
4285
4286 /*
4287 * @implemented
4288 */
4289 VOID
4290 NTAPI
4291 IoInvalidateDeviceRelations(
4292 IN PDEVICE_OBJECT DeviceObject,
4293 IN DEVICE_RELATION_TYPE Type)
4294 {
4295 PIO_WORKITEM WorkItem;
4296 PINVALIDATE_DEVICE_RELATION_DATA Data;
4297
4298 Data = ExAllocatePool(NonPagedPool, sizeof(INVALIDATE_DEVICE_RELATION_DATA));
4299 if (!Data)
4300 return;
4301 WorkItem = IoAllocateWorkItem(DeviceObject);
4302 if (!WorkItem)
4303 {
4304 ExFreePool(Data);
4305 return;
4306 }
4307
4308 ObReferenceObject(DeviceObject);
4309 Data->DeviceObject = DeviceObject;
4310 Data->Type = Type;
4311 Data->WorkItem = WorkItem;
4312
4313 IoQueueWorkItem(
4314 WorkItem,
4315 IopAsynchronousInvalidateDeviceRelations,
4316 DelayedWorkQueue,
4317 Data);
4318 }
4319
4320 /*
4321 * @implemented
4322 */
4323 NTSTATUS
4324 NTAPI
4325 IoSynchronousInvalidateDeviceRelations(
4326 IN PDEVICE_OBJECT DeviceObject,
4327 IN DEVICE_RELATION_TYPE Type)
4328 {
4329 PAGED_CODE();
4330
4331 switch (Type)
4332 {
4333 case BusRelations:
4334 /* Enumerate the device */
4335 return IopEnumerateDevice(DeviceObject);
4336 case PowerRelations:
4337 /* Not handled yet */
4338 return STATUS_NOT_IMPLEMENTED;
4339 case TargetDeviceRelation:
4340 /* Nothing to do */
4341 return STATUS_SUCCESS;
4342 default:
4343 /* Ejection relations are not supported */
4344 return STATUS_NOT_SUPPORTED;
4345 }
4346 }
4347
4348 /*
4349 * @implemented
4350 */
4351 BOOLEAN
4352 NTAPI
4353 IoTranslateBusAddress(IN INTERFACE_TYPE InterfaceType,
4354 IN ULONG BusNumber,
4355 IN PHYSICAL_ADDRESS BusAddress,
4356 IN OUT PULONG AddressSpace,
4357 OUT PPHYSICAL_ADDRESS TranslatedAddress)
4358 {
4359 /* FIXME: Notify the resource arbiter */
4360
4361 return HalTranslateBusAddress(InterfaceType,
4362 BusNumber,
4363 BusAddress,
4364 AddressSpace,
4365 TranslatedAddress);
4366 }