merge ROS Shell without integrated explorer part into trunk
[reactos.git] / reactos / ntoskrnl / io / pnpmgr.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/io/pnpmgr.c
6 * PURPOSE: Initializes the PnP manager
7 *
8 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <ddk/wdmguid.h>
15
16 #define NDEBUG
17 #include <internal/debug.h>
18
19 /* GLOBALS *******************************************************************/
20
21 PDEVICE_NODE IopRootDeviceNode;
22 KSPIN_LOCK IopDeviceTreeLock;
23
24 /* DATA **********************************************************************/
25
26 PDRIVER_OBJECT IopRootDriverObject;
27
28 /* FUNCTIONS *****************************************************************/
29
30 /*
31 * @unimplemented
32 */
33 VOID
34 STDCALL
35 IoInvalidateDeviceRelations(
36 IN PDEVICE_OBJECT DeviceObject,
37 IN DEVICE_RELATION_TYPE Type)
38 {
39 CHECKPOINT1;
40 }
41
42 PDEVICE_NODE FASTCALL
43 IopGetDeviceNode(
44 PDEVICE_OBJECT DeviceObject)
45 {
46 return DeviceObject->DeviceObjectExtension->DeviceNode;
47 }
48
49 /*
50 * @unimplemented
51 */
52 NTSTATUS
53 STDCALL
54 IoGetDeviceProperty(
55 IN PDEVICE_OBJECT DeviceObject,
56 IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
57 IN ULONG BufferLength,
58 OUT PVOID PropertyBuffer,
59 OUT PULONG ResultLength)
60 {
61 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
62 ULONG Length;
63 PVOID Data;
64 PWSTR Ptr;
65
66 DPRINT("IoGetDeviceProperty(%x %d)\n", DeviceObject, DeviceProperty);
67
68 if (DeviceNode == NULL)
69 return STATUS_INVALID_DEVICE_REQUEST;
70
71 switch (DeviceProperty)
72 {
73 case DevicePropertyBusNumber:
74 Length = sizeof(ULONG);
75 Data = &DeviceNode->ChildBusNumber;
76 break;
77
78 /* Complete, untested */
79 case DevicePropertyBusTypeGuid:
80 *ResultLength = 39 * sizeof(WCHAR);
81 if (BufferLength < (39 * sizeof(WCHAR)))
82 return STATUS_BUFFER_TOO_SMALL;
83 swprintf((PWSTR)PropertyBuffer,
84 L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
85 DeviceNode->BusTypeGuid.Data1,
86 DeviceNode->BusTypeGuid.Data2,
87 DeviceNode->BusTypeGuid.Data3,
88 DeviceNode->BusTypeGuid.Data4[0],
89 DeviceNode->BusTypeGuid.Data4[1],
90 DeviceNode->BusTypeGuid.Data4[2],
91 DeviceNode->BusTypeGuid.Data4[3],
92 DeviceNode->BusTypeGuid.Data4[4],
93 DeviceNode->BusTypeGuid.Data4[5],
94 DeviceNode->BusTypeGuid.Data4[6],
95 DeviceNode->BusTypeGuid.Data4[7]);
96 return STATUS_SUCCESS;
97
98 case DevicePropertyLegacyBusType:
99 Length = sizeof(INTERFACE_TYPE);
100 Data = &DeviceNode->ChildInterfaceType;
101 break;
102
103 case DevicePropertyAddress:
104 Length = sizeof(ULONG);
105 Data = &DeviceNode->Address;
106 break;
107
108 // case DevicePropertyUINumber:
109 // if (DeviceNode->CapabilityFlags == NULL)
110 // return STATUS_INVALID_DEVICE_REQUEST;
111 // Length = sizeof(ULONG);
112 // Data = &DeviceNode->CapabilityFlags->UINumber;
113 // break;
114
115 case DevicePropertyClassName:
116 case DevicePropertyClassGuid:
117 case DevicePropertyDriverKeyName:
118 case DevicePropertyManufacturer:
119 case DevicePropertyFriendlyName:
120 case DevicePropertyHardwareID:
121 case DevicePropertyCompatibleIDs:
122 case DevicePropertyDeviceDescription:
123 case DevicePropertyLocationInformation:
124 case DevicePropertyUINumber:
125 {
126 LPWSTR RegistryPropertyName, KeyNameBuffer;
127 UNICODE_STRING KeyName, ValueName;
128 OBJECT_ATTRIBUTES ObjectAttributes;
129 KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
130 ULONG ValueInformationLength;
131 HANDLE KeyHandle;
132 NTSTATUS Status;
133
134 switch (DeviceProperty)
135 {
136 case DevicePropertyClassName:
137 RegistryPropertyName = L"Class"; break;
138 case DevicePropertyClassGuid:
139 RegistryPropertyName = L"ClassGuid"; break;
140 case DevicePropertyDriverKeyName:
141 RegistryPropertyName = L"Driver"; break;
142 case DevicePropertyManufacturer:
143 RegistryPropertyName = L"Mfg"; break;
144 case DevicePropertyFriendlyName:
145 RegistryPropertyName = L"FriendlyName"; break;
146 case DevicePropertyHardwareID:
147 RegistryPropertyName = L"HardwareID"; break;
148 case DevicePropertyCompatibleIDs:
149 RegistryPropertyName = L"CompatibleIDs"; break;
150 case DevicePropertyDeviceDescription:
151 RegistryPropertyName = L"DeviceDesc"; break;
152 case DevicePropertyLocationInformation:
153 RegistryPropertyName = L"LocationInformation"; break;
154 case DevicePropertyUINumber:
155 RegistryPropertyName = L"UINumber"; break;
156 default:
157 RegistryPropertyName = NULL; break;
158 }
159
160 KeyNameBuffer = ExAllocatePool(PagedPool,
161 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
162
163 DPRINT("KeyNameBuffer: %x, value %S\n",
164 KeyNameBuffer, RegistryPropertyName);
165
166 if (KeyNameBuffer == NULL)
167 return STATUS_INSUFFICIENT_RESOURCES;
168
169 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
170 wcscat(KeyNameBuffer, DeviceNode->InstancePath.Buffer);
171 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
172 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
173 OBJ_CASE_INSENSITIVE, NULL, NULL);
174
175 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
176 ExFreePool(KeyNameBuffer);
177 if (!NT_SUCCESS(Status))
178 return Status;
179
180 RtlInitUnicodeString(&ValueName, RegistryPropertyName);
181 ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
182 Data[0]) + BufferLength;
183 ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
184 if (ValueInformation == NULL)
185 {
186 ZwClose(KeyHandle);
187 return STATUS_INSUFFICIENT_RESOURCES;
188 }
189
190 Status = ZwQueryValueKey(KeyHandle, &ValueName,
191 KeyValuePartialInformation, ValueInformation,
192 ValueInformationLength,
193 &ValueInformationLength);
194 *ResultLength = ValueInformation->DataLength;
195 ZwClose(KeyHandle);
196
197 if (ValueInformation->DataLength > BufferLength)
198 Status = STATUS_BUFFER_TOO_SMALL;
199
200 if (!NT_SUCCESS(Status))
201 {
202 ExFreePool(ValueInformation);
203 return Status;
204 }
205
206 /* FIXME: Verify the value (NULL-terminated, correct format). */
207
208 RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
209 ValueInformation->DataLength);
210 ExFreePool(ValueInformation);
211
212 return STATUS_SUCCESS;
213 }
214
215 case DevicePropertyBootConfiguration:
216 Length = 0;
217 if (DeviceNode->BootResources->Count != 0)
218 {
219 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
220 }
221 Data = &DeviceNode->BootResources;
222 break;
223
224 /* FIXME: use a translated boot configuration instead */
225 case DevicePropertyBootConfigurationTranslated:
226 Length = 0;
227 if (DeviceNode->BootResources->Count != 0)
228 {
229 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
230 }
231 Data = &DeviceNode->BootResources;
232 break;
233
234 case DevicePropertyEnumeratorName:
235 Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
236 if (Ptr != NULL)
237 {
238 Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
239 }
240 else
241 {
242 Length = 0;
243 Data = NULL;
244 }
245
246 case DevicePropertyPhysicalDeviceObjectName:
247 return STATUS_NOT_IMPLEMENTED;
248
249 default:
250 return STATUS_INVALID_PARAMETER_2;
251 }
252
253 *ResultLength = Length;
254 if (BufferLength < Length)
255 return STATUS_BUFFER_TOO_SMALL;
256 RtlCopyMemory(PropertyBuffer, Data, Length);
257
258 /* Terminate the string */
259 if (DeviceProperty == DevicePropertyEnumeratorName)
260 {
261 Ptr = (PWSTR)PropertyBuffer;
262 Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
263 }
264
265 return STATUS_SUCCESS;
266 }
267
268 /*
269 * @unimplemented
270 */
271 VOID
272 STDCALL
273 IoInvalidateDeviceState(
274 IN PDEVICE_OBJECT PhysicalDeviceObject)
275 {
276 }
277
278 /**
279 * @name IoOpenDeviceRegistryKey
280 *
281 * Open a registry key unique for a specified driver or device instance.
282 *
283 * @param DeviceObject Device to get the registry key for.
284 * @param DevInstKeyType Type of the key to return.
285 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
286 * @param DevInstRegKey Handle to the opened registry key on
287 * successful return.
288 *
289 * @return Status.
290 *
291 * @implemented
292 */
293 NTSTATUS
294 STDCALL
295 IoOpenDeviceRegistryKey(
296 IN PDEVICE_OBJECT DeviceObject,
297 IN ULONG DevInstKeyType,
298 IN ACCESS_MASK DesiredAccess,
299 OUT PHANDLE DevInstRegKey)
300 {
301 static WCHAR RootKeyName[] =
302 L"\\Registry\\Machine\\System\\CurrentControlSet\\";
303 static WCHAR ProfileKeyName[] =
304 L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
305 static WCHAR ClassKeyName[] = L"Control\\Class\\";
306 static WCHAR EnumKeyName[] = L"Enum\\";
307 static WCHAR DeviceParametersKeyName[] = L"Device Parameters\\";
308 ULONG KeyNameLength;
309 LPWSTR KeyNameBuffer;
310 UNICODE_STRING KeyName;
311 ULONG DriverKeyLength;
312 OBJECT_ATTRIBUTES ObjectAttributes;
313 PDEVICE_NODE DeviceNode = NULL;
314 NTSTATUS Status;
315
316 if ((DevInstKeyType & (PLUGPLAY_REGKEY_DEVICE | PLUGPLAY_REGKEY_DRIVER)) == 0)
317 return STATUS_INVALID_PARAMETER;
318
319 /*
320 * Calculate the length of the base key name. This is the full
321 * name for driver key or the name excluding "Device Parameters"
322 * subkey for device key.
323 */
324
325 KeyNameLength = sizeof(RootKeyName);
326 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
327 KeyNameLength += sizeof(ProfileKeyName) - sizeof(UNICODE_NULL);
328 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
329 {
330 KeyNameLength += sizeof(ClassKeyName) - sizeof(UNICODE_NULL);
331 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
332 0, NULL, &DriverKeyLength);
333 if (Status != STATUS_BUFFER_TOO_SMALL)
334 return Status;
335 KeyNameLength += DriverKeyLength;
336 }
337 else
338 {
339 DeviceNode = IopGetDeviceNode(DeviceObject);
340 KeyNameLength += sizeof(EnumKeyName) - sizeof(UNICODE_NULL) +
341 DeviceNode->InstancePath.Length;
342 }
343
344 /*
345 * Now allocate the buffer for the key name...
346 */
347
348 KeyNameBuffer = ExAllocatePool(PagedPool, KeyNameLength);
349 if (KeyNameBuffer == NULL)
350 return STATUS_INSUFFICIENT_RESOURCES;
351
352 KeyName.Length = 0;
353 KeyName.MaximumLength = KeyNameLength;
354 KeyName.Buffer = KeyNameBuffer;
355
356 /*
357 * ...and build the key name.
358 */
359
360 KeyName.Length += sizeof(RootKeyName) - sizeof(UNICODE_NULL);
361 RtlCopyMemory(KeyNameBuffer, RootKeyName, KeyName.Length);
362
363 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
364 RtlAppendUnicodeToString(&KeyName, ProfileKeyName);
365
366 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
367 {
368 RtlAppendUnicodeToString(&KeyName, ClassKeyName);
369 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
370 DriverKeyLength, KeyNameBuffer +
371 (KeyName.Length / sizeof(WCHAR)),
372 &DriverKeyLength);
373 if (!NT_SUCCESS(Status))
374 {
375 ExFreePool(KeyNameBuffer);
376 return Status;
377 }
378 KeyName.Length += DriverKeyLength - sizeof(UNICODE_NULL);
379 }
380 else
381 {
382 RtlAppendUnicodeToString(&KeyName, EnumKeyName);
383 Status = RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->InstancePath);
384 if (DeviceNode->InstancePath.Length == 0)
385 {
386 ExFreePool(KeyNameBuffer);
387 return Status;
388 }
389 }
390
391 /*
392 * Open the base key.
393 */
394
395 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
396 OBJ_CASE_INSENSITIVE, NULL, NULL);
397 Status = ZwOpenKey(DevInstRegKey, DesiredAccess, &ObjectAttributes);
398 ExFreePool(KeyNameBuffer);
399
400 /*
401 * For driver key we're done now. Also if the base key doesn't
402 * exist we can bail out with error...
403 */
404
405 if ((DevInstKeyType & PLUGPLAY_REGKEY_DRIVER) || !NT_SUCCESS(Status))
406 return Status;
407
408 /*
409 * Let's go further. For device key we must open "Device Parameters"
410 * subkey and create it if it doesn't exist yet.
411 */
412
413 RtlInitUnicodeString(&KeyName, DeviceParametersKeyName);
414 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
415 OBJ_CASE_INSENSITIVE, *DevInstRegKey, NULL);
416 Status = ZwCreateKey(DevInstRegKey, DesiredAccess, &ObjectAttributes,
417 0, NULL, REG_OPTION_NON_VOLATILE, NULL);
418 ZwClose(ObjectAttributes.RootDirectory);
419
420 return Status;
421 }
422
423 /*
424 * @unimplemented
425 */
426 VOID
427 STDCALL
428 IoRequestDeviceEject(
429 IN PDEVICE_OBJECT PhysicalDeviceObject
430 )
431 {
432 UNIMPLEMENTED;
433 }
434
435
436 BOOLEAN
437 IopCreateUnicodeString(
438 PUNICODE_STRING Destination,
439 PWSTR Source,
440 POOL_TYPE PoolType)
441 {
442 ULONG Length;
443
444 if (!Source)
445 {
446 RtlInitUnicodeString(Destination, NULL);
447 return TRUE;
448 }
449
450 Length = (wcslen(Source) + 1) * sizeof(WCHAR);
451
452 Destination->Buffer = ExAllocatePool(PoolType, Length);
453
454 if (Destination->Buffer == NULL)
455 {
456 return FALSE;
457 }
458
459 RtlCopyMemory(Destination->Buffer, Source, Length);
460
461 Destination->MaximumLength = Length;
462
463 Destination->Length = Length - sizeof(WCHAR);
464
465 return TRUE;
466 }
467
468 NTSTATUS
469 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
470 {
471 KIRQL OldIrql;
472
473 if (PopSystemPowerDeviceNode)
474 {
475 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
476 *DeviceObject = PopSystemPowerDeviceNode->PhysicalDeviceObject;
477 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
478
479 return STATUS_SUCCESS;
480 }
481
482 return STATUS_UNSUCCESSFUL;
483 }
484
485 /*
486 * DESCRIPTION
487 * Creates a device node
488 *
489 * ARGUMENTS
490 * ParentNode = Pointer to parent device node
491 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
492 * to have the root device node create one
493 * (eg. for legacy drivers)
494 * DeviceNode = Pointer to storage for created device node
495 *
496 * RETURN VALUE
497 * Status
498 */
499 NTSTATUS
500 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
501 PDEVICE_OBJECT PhysicalDeviceObject,
502 PDEVICE_NODE *DeviceNode)
503 {
504 PDEVICE_NODE Node;
505 NTSTATUS Status;
506 KIRQL OldIrql;
507
508 DPRINT("ParentNode %x PhysicalDeviceObject %x\n",
509 ParentNode, PhysicalDeviceObject);
510
511 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
512 if (!Node)
513 {
514 return STATUS_INSUFFICIENT_RESOURCES;
515 }
516
517 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
518
519 if (!PhysicalDeviceObject)
520 {
521 Status = PnpRootCreateDevice(&PhysicalDeviceObject);
522 if (!NT_SUCCESS(Status))
523 {
524 ExFreePool(Node);
525 return Status;
526 }
527
528 /* This is for drivers passed on the command line to ntoskrnl.exe */
529 IopDeviceNodeSetFlag(Node, DNF_STARTED);
530 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
531 }
532
533 Node->PhysicalDeviceObject = PhysicalDeviceObject;
534
535 PhysicalDeviceObject->DeviceObjectExtension->DeviceNode = Node;
536
537 if (ParentNode)
538 {
539 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
540 Node->Parent = ParentNode;
541 Node->NextSibling = ParentNode->Child;
542 if (ParentNode->Child != NULL)
543 {
544 ParentNode->Child->PrevSibling = Node;
545 }
546 ParentNode->Child = Node;
547 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
548 }
549
550 *DeviceNode = Node;
551
552 return STATUS_SUCCESS;
553 }
554
555 NTSTATUS
556 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
557 {
558 KIRQL OldIrql;
559
560 /* All children must be deleted before a parent is deleted */
561 ASSERT(!DeviceNode->Child);
562
563 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
564
565 ASSERT(DeviceNode->PhysicalDeviceObject);
566
567 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
568
569 /* Unlink from parent if it exists */
570
571 if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
572 {
573 DeviceNode->Parent->Child = DeviceNode->NextSibling;
574 }
575
576 /* Unlink from sibling list */
577
578 if (DeviceNode->PrevSibling)
579 {
580 DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
581 }
582
583 if (DeviceNode->NextSibling)
584 {
585 DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
586 }
587
588 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
589
590 RtlFreeUnicodeString(&DeviceNode->InstancePath);
591
592 RtlFreeUnicodeString(&DeviceNode->ServiceName);
593
594 if (DeviceNode->ResourceList)
595 {
596 ExFreePool(DeviceNode->ResourceList);
597 }
598
599 if (DeviceNode->ResourceListTranslated)
600 {
601 ExFreePool(DeviceNode->ResourceListTranslated);
602 }
603
604 if (DeviceNode->ResourceRequirements)
605 {
606 ExFreePool(DeviceNode->ResourceRequirements);
607 }
608
609 if (DeviceNode->BootResources)
610 {
611 ExFreePool(DeviceNode->BootResources);
612 }
613
614 ExFreePool(DeviceNode);
615
616 return STATUS_SUCCESS;
617 }
618
619 NTSTATUS
620 IopInitiatePnpIrp(
621 PDEVICE_OBJECT DeviceObject,
622 PIO_STATUS_BLOCK IoStatusBlock,
623 ULONG MinorFunction,
624 PIO_STACK_LOCATION Stack OPTIONAL)
625 {
626 PDEVICE_OBJECT TopDeviceObject;
627 PIO_STACK_LOCATION IrpSp;
628 NTSTATUS Status;
629 KEVENT Event;
630 PIRP Irp;
631
632 /* Always call the top of the device stack */
633 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
634
635 KeInitializeEvent(
636 &Event,
637 NotificationEvent,
638 FALSE);
639
640 Irp = IoBuildSynchronousFsdRequest(
641 IRP_MJ_PNP,
642 TopDeviceObject,
643 NULL,
644 0,
645 NULL,
646 &Event,
647 IoStatusBlock);
648
649 /* PNP IRPs are always initialized with a status code of
650 STATUS_NOT_IMPLEMENTED */
651 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
652 Irp->IoStatus.Information = 0;
653
654 IrpSp = IoGetNextIrpStackLocation(Irp);
655 IrpSp->MinorFunction = MinorFunction;
656
657 if (Stack)
658 {
659 RtlMoveMemory(
660 &IrpSp->Parameters,
661 &Stack->Parameters,
662 sizeof(Stack->Parameters));
663 }
664
665 Status = IoCallDriver(TopDeviceObject, Irp);
666 if (Status == STATUS_PENDING)
667 {
668 KeWaitForSingleObject(
669 &Event,
670 Executive,
671 KernelMode,
672 FALSE,
673 NULL);
674 Status = IoStatusBlock->Status;
675 }
676
677 ObDereferenceObject(TopDeviceObject);
678
679 return Status;
680 }
681
682
683 NTSTATUS
684 IopTraverseDeviceTreeNode(
685 PDEVICETREE_TRAVERSE_CONTEXT Context)
686 {
687 PDEVICE_NODE ParentDeviceNode;
688 PDEVICE_NODE ChildDeviceNode;
689 NTSTATUS Status;
690
691 /* Copy context data so we don't overwrite it in subsequent calls to this function */
692 ParentDeviceNode = Context->DeviceNode;
693
694 /* Call the action routine */
695 Status = (Context->Action)(ParentDeviceNode, Context->Context);
696 if (!NT_SUCCESS(Status))
697 {
698 return Status;
699 }
700
701 /* Traversal of all children nodes */
702 for (ChildDeviceNode = ParentDeviceNode->Child;
703 ChildDeviceNode != NULL;
704 ChildDeviceNode = ChildDeviceNode->NextSibling)
705 {
706 /* Pass the current device node to the action routine */
707 Context->DeviceNode = ChildDeviceNode;
708
709 Status = IopTraverseDeviceTreeNode(Context);
710 if (!NT_SUCCESS(Status))
711 {
712 return Status;
713 }
714 }
715
716 return Status;
717 }
718
719
720 NTSTATUS
721 IopTraverseDeviceTree(
722 PDEVICETREE_TRAVERSE_CONTEXT Context)
723 {
724 NTSTATUS Status;
725
726 DPRINT("Context %x\n", Context);
727
728 DPRINT("IopTraverseDeviceTree(DeviceNode %x FirstDeviceNode %x Action %x Context %x)\n",
729 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
730
731 /* Start from the specified device node */
732 Context->DeviceNode = Context->FirstDeviceNode;
733
734 /* Recursively traverse the device tree */
735 Status = IopTraverseDeviceTreeNode(Context);
736 if (Status == STATUS_UNSUCCESSFUL)
737 {
738 /* The action routine just wanted to terminate the traversal with status
739 code STATUS_SUCCESS */
740 Status = STATUS_SUCCESS;
741 }
742
743 return Status;
744 }
745
746
747 static NTSTATUS
748 IopCreateDeviceKeyPath(PWSTR Path,
749 PHANDLE Handle)
750 {
751 OBJECT_ATTRIBUTES ObjectAttributes;
752 WCHAR KeyBuffer[MAX_PATH];
753 UNICODE_STRING KeyName;
754 HANDLE KeyHandle;
755 NTSTATUS Status;
756 PWCHAR Current;
757 PWCHAR Next;
758
759 *Handle = NULL;
760
761 if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
762 {
763 return STATUS_INVALID_PARAMETER;
764 }
765
766 wcsncpy (KeyBuffer, Path, MAX_PATH-1);
767
768 /* Skip \\Registry\\ */
769 Current = KeyBuffer;
770 Current = wcschr (Current, L'\\') + 1;
771 Current = wcschr (Current, L'\\') + 1;
772
773 while (TRUE)
774 {
775 Next = wcschr (Current, L'\\');
776 if (Next == NULL)
777 {
778 /* The end */
779 }
780 else
781 {
782 *Next = 0;
783 }
784
785 RtlInitUnicodeString (&KeyName, KeyBuffer);
786 InitializeObjectAttributes (&ObjectAttributes,
787 &KeyName,
788 OBJ_CASE_INSENSITIVE,
789 NULL,
790 NULL);
791
792 DPRINT("Create '%S'\n", KeyName.Buffer);
793
794 Status = ZwCreateKey (&KeyHandle,
795 KEY_ALL_ACCESS,
796 &ObjectAttributes,
797 0,
798 NULL,
799 0,
800 NULL);
801 if (!NT_SUCCESS (Status))
802 {
803 DPRINT ("ZwCreateKey() failed with status %x\n", Status);
804 return Status;
805 }
806
807 if (Next == NULL)
808 {
809 *Handle = KeyHandle;
810 return STATUS_SUCCESS;
811 }
812 else
813 {
814 ZwClose (KeyHandle);
815 *Next = L'\\';
816 }
817
818 Current = Next + 1;
819 }
820
821 return STATUS_UNSUCCESSFUL;
822 }
823
824
825 static NTSTATUS
826 IopSetDeviceInstanceData(HANDLE InstanceKey,
827 PDEVICE_NODE DeviceNode)
828 {
829 OBJECT_ATTRIBUTES ObjectAttributes;
830 UNICODE_STRING KeyName;
831 HANDLE LogConfKey;
832 ULONG ResCount;
833 ULONG ListSize;
834 NTSTATUS Status;
835
836 DPRINT("IopSetDeviceInstanceData() called\n");
837
838 /* Create the 'LogConf' key */
839 RtlInitUnicodeString(&KeyName,
840 L"LogConf");
841 InitializeObjectAttributes(&ObjectAttributes,
842 &KeyName,
843 OBJ_CASE_INSENSITIVE,
844 InstanceKey,
845 NULL);
846 Status = ZwCreateKey(&LogConfKey,
847 KEY_ALL_ACCESS,
848 &ObjectAttributes,
849 0,
850 NULL,
851 0,
852 NULL);
853 if (NT_SUCCESS(Status))
854 {
855 /* Set 'BootConfig' value */
856 if (DeviceNode->BootResources != NULL)
857 {
858 ResCount = DeviceNode->BootResources->Count;
859 if (ResCount != 0)
860 {
861 ListSize = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
862
863 RtlInitUnicodeString(&KeyName,
864 L"BootConfig");
865 Status = ZwSetValueKey(LogConfKey,
866 &KeyName,
867 0,
868 REG_RESOURCE_LIST,
869 &DeviceNode->BootResources,
870 ListSize);
871 }
872 }
873
874 /* Set 'BasicConfigVector' value */
875 if (DeviceNode->ResourceRequirements != NULL &&
876 DeviceNode->ResourceRequirements->ListSize != 0)
877 {
878 RtlInitUnicodeString(&KeyName,
879 L"BasicConfigVector");
880 Status = ZwSetValueKey(LogConfKey,
881 &KeyName,
882 0,
883 REG_RESOURCE_REQUIREMENTS_LIST,
884 &DeviceNode->ResourceRequirements,
885 DeviceNode->ResourceRequirements->ListSize);
886 }
887
888 ZwClose(LogConfKey);
889 }
890
891 DPRINT("IopSetDeviceInstanceData() done\n");
892
893 return STATUS_SUCCESS;
894 }
895
896
897 /*
898 * IopActionInterrogateDeviceStack
899 *
900 * Retrieve information for all (direct) child nodes of a parent node.
901 *
902 * Parameters
903 * DeviceNode
904 * Pointer to device node.
905 * Context
906 * Pointer to parent node to retrieve child node information for.
907 *
908 * Remarks
909 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
910 * when we reach a device node which is not a direct child of the device
911 * node for which we retrieve information of child nodes for. Any errors
912 * that occur is logged instead so that all child services have a chance
913 * of being interrogated.
914 */
915
916 NTSTATUS
917 IopActionInterrogateDeviceStack(
918 PDEVICE_NODE DeviceNode,
919 PVOID Context)
920 {
921 IO_STATUS_BLOCK IoStatusBlock;
922 PDEVICE_NODE ParentDeviceNode;
923 WCHAR InstancePath[MAX_PATH];
924 IO_STACK_LOCATION Stack;
925 NTSTATUS Status;
926 PWSTR KeyBuffer;
927 PWSTR Ptr;
928 USHORT Length;
929 USHORT TotalLength;
930 HANDLE InstanceKey = NULL;
931 UNICODE_STRING ValueName;
932 DEVICE_CAPABILITIES DeviceCapabilities;
933
934 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
935 DPRINT("PDO %x\n", DeviceNode->PhysicalDeviceObject);
936
937 ParentDeviceNode = (PDEVICE_NODE)Context;
938
939 /*
940 * We are called for the parent too, but we don't need to do special
941 * handling for this node
942 */
943
944 if (DeviceNode == ParentDeviceNode)
945 {
946 DPRINT("Success\n");
947 return STATUS_SUCCESS;
948 }
949
950 /*
951 * Make sure this device node is a direct child of the parent device node
952 * that is given as an argument
953 */
954
955 if (DeviceNode->Parent != ParentDeviceNode)
956 {
957 /* Stop the traversal immediately and indicate successful operation */
958 DPRINT("Stop\n");
959 return STATUS_UNSUCCESSFUL;
960 }
961
962 /*
963 * FIXME: For critical errors, cleanup and disable device, but always
964 * return STATUS_SUCCESS.
965 */
966
967 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
968
969 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
970 Status = IopInitiatePnpIrp(
971 DeviceNode->PhysicalDeviceObject,
972 &IoStatusBlock,
973 IRP_MN_QUERY_ID,
974 &Stack);
975 if (NT_SUCCESS(Status))
976 {
977 /* Copy the device id string */
978 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
979
980 /*
981 * FIXME: Check for valid characters, if there is invalid characters
982 * then bugcheck.
983 */
984 }
985 else
986 {
987 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
988 }
989
990 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
991
992 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
993 Status = IopInitiatePnpIrp(
994 DeviceNode->PhysicalDeviceObject,
995 &IoStatusBlock,
996 IRP_MN_QUERY_ID,
997 &Stack);
998 if (NT_SUCCESS(Status))
999 {
1000 /* Append the instance id string */
1001 wcscat(InstancePath, L"\\");
1002 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1003
1004 /*
1005 * FIXME: Check for valid characters, if there is invalid characters
1006 * then bugcheck
1007 */
1008 }
1009 else
1010 {
1011 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1012 }
1013
1014 RtlZeroMemory(&DeviceCapabilities, sizeof(DEVICE_CAPABILITIES));
1015 DeviceCapabilities.Size = sizeof(DEVICE_CAPABILITIES);
1016 DeviceCapabilities.Version = 1;
1017 DeviceCapabilities.Address = -1;
1018 DeviceCapabilities.UINumber = -1;
1019
1020 Stack.Parameters.DeviceCapabilities.Capabilities = &DeviceCapabilities;
1021 Status = IopInitiatePnpIrp(
1022 DeviceNode->PhysicalDeviceObject,
1023 &IoStatusBlock,
1024 IRP_MN_QUERY_CAPABILITIES,
1025 &Stack);
1026 if (NT_SUCCESS(Status))
1027 {
1028 }
1029 else
1030 {
1031 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1032 }
1033
1034 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCapabilities + 4);
1035 DeviceNode->Address = DeviceCapabilities.Address;
1036
1037 if (!DeviceCapabilities.UniqueID)
1038 {
1039 DPRINT("Instance ID is not unique\n");
1040 /* FIXME: Add information from parent bus driver to InstancePath */
1041 }
1042
1043 if (!IopCreateUnicodeString(&DeviceNode->InstancePath, InstancePath, PagedPool))
1044 {
1045 DPRINT("No resources\n");
1046 /* FIXME: Cleanup and disable device */
1047 }
1048
1049 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1050
1051 /*
1052 * Create registry key for the instance id, if it doesn't exist yet
1053 */
1054 KeyBuffer = ExAllocatePool(
1055 PagedPool,
1056 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
1057 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1058 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
1059 Status = IopCreateDeviceKeyPath(KeyBuffer,
1060 &InstanceKey);
1061 ExFreePool(KeyBuffer);
1062 if (!NT_SUCCESS(Status))
1063 {
1064 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1065 }
1066
1067
1068 {
1069 /* Set 'Capabilities' value */
1070 RtlInitUnicodeString(&ValueName,
1071 L"Capabilities");
1072 Status = ZwSetValueKey(InstanceKey,
1073 &ValueName,
1074 0,
1075 REG_DWORD,
1076 (PVOID)&DeviceNode->CapabilityFlags,
1077 sizeof(ULONG));
1078
1079 /* Set 'UINumber' value */
1080 if (DeviceCapabilities.UINumber != (ULONG)-1)
1081 {
1082 RtlInitUnicodeString(&ValueName,
1083 L"UINumber");
1084 Status = ZwSetValueKey(InstanceKey,
1085 &ValueName,
1086 0,
1087 REG_DWORD,
1088 &DeviceCapabilities.UINumber,
1089 sizeof(ULONG));
1090 }
1091 }
1092
1093 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1094
1095 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1096 Status = IopInitiatePnpIrp(
1097 DeviceNode->PhysicalDeviceObject,
1098 &IoStatusBlock,
1099 IRP_MN_QUERY_ID,
1100 &Stack);
1101 if (NT_SUCCESS(Status))
1102 {
1103 /*
1104 * FIXME: Check for valid characters, if there is invalid characters
1105 * then bugcheck.
1106 */
1107 TotalLength = 0;
1108 Ptr = (PWSTR)IoStatusBlock.Information;
1109 DPRINT("Hardware IDs:\n");
1110 while (*Ptr)
1111 {
1112 DPRINT(" %S\n", Ptr);
1113 Length = wcslen(Ptr) + 1;
1114
1115 Ptr += Length;
1116 TotalLength += Length;
1117 }
1118 DPRINT("TotalLength: %hu\n", TotalLength);
1119 DPRINT("\n");
1120
1121 RtlInitUnicodeString(&ValueName,
1122 L"HardwareID");
1123 Status = ZwSetValueKey(InstanceKey,
1124 &ValueName,
1125 0,
1126 REG_MULTI_SZ,
1127 (PVOID)IoStatusBlock.Information,
1128 (TotalLength + 1) * sizeof(WCHAR));
1129 if (!NT_SUCCESS(Status))
1130 {
1131 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1132 }
1133 }
1134 else
1135 {
1136 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1137 }
1138
1139 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1140
1141 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1142 Status = IopInitiatePnpIrp(
1143 DeviceNode->PhysicalDeviceObject,
1144 &IoStatusBlock,
1145 IRP_MN_QUERY_ID,
1146 &Stack);
1147 if (NT_SUCCESS(Status))
1148 {
1149 /*
1150 * FIXME: Check for valid characters, if there is invalid characters
1151 * then bugcheck.
1152 */
1153 TotalLength = 0;
1154 Ptr = (PWSTR)IoStatusBlock.Information;
1155 DPRINT("Compatible IDs:\n");
1156 while (*Ptr)
1157 {
1158 DPRINT(" %S\n", Ptr);
1159 Length = wcslen(Ptr) + 1;
1160
1161 Ptr += Length;
1162 TotalLength += Length;
1163 }
1164 DPRINT("TotalLength: %hu\n", TotalLength);
1165 DPRINT("\n");
1166
1167 RtlInitUnicodeString(&ValueName,
1168 L"CompatibleIDs");
1169 Status = ZwSetValueKey(InstanceKey,
1170 &ValueName,
1171 0,
1172 REG_MULTI_SZ,
1173 (PVOID)IoStatusBlock.Information,
1174 (TotalLength + 1) * sizeof(WCHAR));
1175 if (!NT_SUCCESS(Status))
1176 {
1177 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1178 }
1179 }
1180 else
1181 {
1182 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1183 }
1184
1185
1186 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1187
1188 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1189 Stack.Parameters.QueryDeviceText.LocaleId = 0; /* FIXME */
1190 Status = IopInitiatePnpIrp(
1191 DeviceNode->PhysicalDeviceObject,
1192 &IoStatusBlock,
1193 IRP_MN_QUERY_DEVICE_TEXT,
1194 &Stack);
1195 if (NT_SUCCESS(Status))
1196 {
1197 RtlInitUnicodeString(&ValueName,
1198 L"DeviceDesc");
1199 Status = ZwSetValueKey(InstanceKey,
1200 &ValueName,
1201 0,
1202 REG_SZ,
1203 (PVOID)IoStatusBlock.Information,
1204 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1205 if (!NT_SUCCESS(Status))
1206 {
1207 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1208 }
1209 }
1210 else
1211 {
1212 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1213 }
1214
1215 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1216
1217 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1218 Stack.Parameters.QueryDeviceText.LocaleId = 0; // FIXME
1219 Status = IopInitiatePnpIrp(
1220 DeviceNode->PhysicalDeviceObject,
1221 &IoStatusBlock,
1222 IRP_MN_QUERY_DEVICE_TEXT,
1223 &Stack);
1224 if (NT_SUCCESS(Status))
1225 {
1226 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1227 RtlInitUnicodeString(&ValueName,
1228 L"LocationInformation");
1229 Status = ZwSetValueKey(InstanceKey,
1230 &ValueName,
1231 0,
1232 REG_SZ,
1233 (PVOID)IoStatusBlock.Information,
1234 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1235 if (!NT_SUCCESS(Status))
1236 {
1237 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1238 }
1239 }
1240 else
1241 {
1242 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1243 }
1244
1245 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1246
1247 Status = IopInitiatePnpIrp(
1248 DeviceNode->PhysicalDeviceObject,
1249 &IoStatusBlock,
1250 IRP_MN_QUERY_BUS_INFORMATION,
1251 NULL);
1252 if (NT_SUCCESS(Status))
1253 {
1254 PPNP_BUS_INFORMATION BusInformation =
1255 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1256
1257 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1258 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1259 memcpy(&DeviceNode->BusTypeGuid,
1260 &BusInformation->BusTypeGuid,
1261 sizeof(GUID));
1262 ExFreePool(BusInformation);
1263 }
1264 else
1265 {
1266 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1267
1268 DeviceNode->ChildBusNumber = -1;
1269 DeviceNode->ChildInterfaceType = -1;
1270 memset(&DeviceNode->BusTypeGuid,
1271 0,
1272 sizeof(GUID));
1273 }
1274
1275 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1276
1277 Status = IopInitiatePnpIrp(
1278 DeviceNode->PhysicalDeviceObject,
1279 &IoStatusBlock,
1280 IRP_MN_QUERY_RESOURCES,
1281 NULL);
1282 if (NT_SUCCESS(Status))
1283 {
1284 DeviceNode->BootResources =
1285 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1286 DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
1287 }
1288 else
1289 {
1290 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1291 DeviceNode->BootResources = NULL;
1292 }
1293
1294 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1295
1296 Status = IopInitiatePnpIrp(
1297 DeviceNode->PhysicalDeviceObject,
1298 &IoStatusBlock,
1299 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1300 NULL);
1301 if (NT_SUCCESS(Status))
1302 {
1303 DeviceNode->ResourceRequirements =
1304 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1305 }
1306 else
1307 {
1308 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1309 DeviceNode->ResourceRequirements = NULL;
1310 }
1311
1312
1313 if (InstanceKey != NULL)
1314 {
1315 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1316 }
1317
1318 ZwClose(InstanceKey);
1319
1320 DeviceNode->Flags |= DNF_PROCESSED;
1321
1322 /* Report the device to the user-mode pnp manager */
1323 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1324 &DeviceNode->InstancePath);
1325
1326 return STATUS_SUCCESS;
1327 }
1328
1329 /*
1330 * IopActionConfigureChildServices
1331 *
1332 * Retrieve configuration for all (direct) child nodes of a parent node.
1333 *
1334 * Parameters
1335 * DeviceNode
1336 * Pointer to device node.
1337 * Context
1338 * Pointer to parent node to retrieve child node configuration for.
1339 *
1340 * Remarks
1341 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1342 * when we reach a device node which is not a direct child of the device
1343 * node for which we configure child services for. Any errors that occur is
1344 * logged instead so that all child services have a chance of beeing
1345 * configured.
1346 */
1347
1348 NTSTATUS
1349 IopActionConfigureChildServices(
1350 PDEVICE_NODE DeviceNode,
1351 PVOID Context)
1352 {
1353 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1354 PDEVICE_NODE ParentDeviceNode;
1355 PUNICODE_STRING Service;
1356 NTSTATUS Status;
1357
1358 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
1359
1360 ParentDeviceNode = (PDEVICE_NODE)Context;
1361
1362 /*
1363 * We are called for the parent too, but we don't need to do special
1364 * handling for this node
1365 */
1366 if (DeviceNode == ParentDeviceNode)
1367 {
1368 DPRINT("Success\n");
1369 return STATUS_SUCCESS;
1370 }
1371
1372 /*
1373 * Make sure this device node is a direct child of the parent device node
1374 * that is given as an argument
1375 */
1376 if (DeviceNode->Parent != ParentDeviceNode)
1377 {
1378 /* Stop the traversal immediately and indicate successful operation */
1379 DPRINT("Stop\n");
1380 return STATUS_UNSUCCESSFUL;
1381 }
1382
1383 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
1384 {
1385 WCHAR RegKeyBuffer[MAX_PATH];
1386 UNICODE_STRING RegKey;
1387
1388 RegKey.Length = 0;
1389 RegKey.MaximumLength = sizeof(RegKeyBuffer);
1390 RegKey.Buffer = RegKeyBuffer;
1391
1392 /*
1393 * Retrieve configuration from Enum key
1394 */
1395
1396 Service = &DeviceNode->ServiceName;
1397
1398 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1399 RtlInitUnicodeString(Service, NULL);
1400
1401 QueryTable[0].Name = L"Service";
1402 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1403 QueryTable[0].EntryContext = Service;
1404
1405 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1406 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
1407
1408 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1409 RegKey.Buffer, QueryTable, NULL, NULL);
1410
1411 if (!NT_SUCCESS(Status))
1412 {
1413 DPRINT("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1414 /* FIXME: Log the error */
1415 CPRINT("Could not retrieve configuration for device %S (Status %x)\n",
1416 DeviceNode->InstancePath.Buffer, Status);
1417 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1418 return STATUS_SUCCESS;
1419 }
1420
1421 if (Service->Buffer == NULL)
1422 {
1423 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1424 return STATUS_SUCCESS;
1425 }
1426
1427 DPRINT("Got Service %S\n", Service->Buffer);
1428 }
1429
1430 return STATUS_SUCCESS;
1431 }
1432
1433 /*
1434 * IopActionInitChildServices
1435 *
1436 * Initialize the service for all (direct) child nodes of a parent node
1437 *
1438 * Parameters
1439 * DeviceNode
1440 * Pointer to device node.
1441 * Context
1442 * Pointer to parent node to initialize child node services for.
1443 * BootDrivers
1444 * Load only driver marked as boot start.
1445 *
1446 * Remarks
1447 * If the driver image for a service is not loaded and initialized
1448 * it is done here too. We only return a status code indicating an
1449 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
1450 * not a direct child of the device node for which we initialize
1451 * child services for. Any errors that occur is logged instead so
1452 * that all child services have a chance of being initialized.
1453 */
1454
1455 NTSTATUS
1456 IopActionInitChildServices(
1457 PDEVICE_NODE DeviceNode,
1458 PVOID Context,
1459 BOOLEAN BootDrivers)
1460 {
1461 PDEVICE_NODE ParentDeviceNode;
1462 NTSTATUS Status;
1463
1464 DPRINT("IopActionInitChildServices(%p, %p, %d)\n", DeviceNode, Context,
1465 BootDrivers);
1466
1467 ParentDeviceNode = (PDEVICE_NODE)Context;
1468
1469 /*
1470 * We are called for the parent too, but we don't need to do special
1471 * handling for this node
1472 */
1473 if (DeviceNode == ParentDeviceNode)
1474 {
1475 DPRINT("Success\n");
1476 return STATUS_SUCCESS;
1477 }
1478
1479 /*
1480 * Make sure this device node is a direct child of the parent device node
1481 * that is given as an argument
1482 */
1483 #if 0
1484 if (DeviceNode->Parent != ParentDeviceNode)
1485 {
1486 /*
1487 * Stop the traversal immediately and indicate unsuccessful operation
1488 */
1489 DPRINT("Stop\n");
1490 return STATUS_UNSUCCESSFUL;
1491 }
1492 #endif
1493
1494 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED) &&
1495 !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
1496 !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
1497 {
1498 PMODULE_OBJECT ModuleObject;
1499 PDRIVER_OBJECT DriverObject;
1500
1501 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
1502 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
1503 {
1504 if (Status != STATUS_IMAGE_ALREADY_LOADED)
1505 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
1506 &DeviceNode->ServiceName, FALSE, &DriverObject);
1507 else
1508 {
1509 /* get existing DriverObject pointer */
1510 Status = IopCreateDriverObject(
1511 &DriverObject,
1512 &DeviceNode->ServiceName,
1513 OBJ_OPENIF,
1514 FALSE,
1515 ModuleObject->Base,
1516 ModuleObject->Length);
1517 }
1518 if (NT_SUCCESS(Status))
1519 {
1520 /* Attach lower level filter drivers. */
1521 IopAttachFilterDrivers(DeviceNode, TRUE);
1522 /* Initialize the function driver for the device node */
1523 Status = IopInitializeDevice(DeviceNode, DriverObject);
1524 if (NT_SUCCESS(Status))
1525 {
1526 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
1527 /* Attach upper level filter drivers. */
1528 IopAttachFilterDrivers(DeviceNode, FALSE);
1529 }
1530 }
1531 }
1532 else
1533 {
1534 /*
1535 * Don't disable when trying to load only boot drivers
1536 */
1537 if (!BootDrivers)
1538 {
1539 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1540 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
1541 }
1542 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
1543 CPRINT("Initialization of service %S failed (Status %x)\n",
1544 DeviceNode->ServiceName.Buffer, Status);
1545 }
1546 } else
1547 {
1548 DPRINT("Service %S is disabled or already initialized\n",
1549 DeviceNode->ServiceName.Buffer);
1550 }
1551
1552 return STATUS_SUCCESS;
1553 }
1554
1555 /*
1556 * IopActionInitAllServices
1557 *
1558 * Initialize the service for all (direct) child nodes of a parent node. This
1559 * function just calls IopActionInitChildServices with BootDrivers = FALSE.
1560 */
1561
1562 NTSTATUS
1563 IopActionInitAllServices(
1564 PDEVICE_NODE DeviceNode,
1565 PVOID Context)
1566 {
1567 return IopActionInitChildServices(DeviceNode, Context, FALSE);
1568 }
1569
1570 /*
1571 * IopActionInitBootServices
1572 *
1573 * Initialize the boot start services for all (direct) child nodes of a
1574 * parent node. This function just calls IopActionInitChildServices with
1575 * BootDrivers = TRUE.
1576 */
1577 NTSTATUS
1578 IopActionInitBootServices(
1579 PDEVICE_NODE DeviceNode,
1580 PVOID Context)
1581 {
1582 return IopActionInitChildServices(DeviceNode, Context, TRUE);
1583 }
1584
1585 /*
1586 * IopInitializePnpServices
1587 *
1588 * Initialize services for discovered children
1589 *
1590 * Parameters
1591 * DeviceNode
1592 * Top device node to start initializing services.
1593 *
1594 * BootDrivers
1595 * When set to TRUE, only drivers marked as boot start will
1596 * be loaded. Otherwise, all drivers will be loaded.
1597 *
1598 * Return Value
1599 * Status
1600 */
1601 NTSTATUS
1602 IopInitializePnpServices(
1603 IN PDEVICE_NODE DeviceNode,
1604 IN BOOLEAN BootDrivers)
1605 {
1606 DEVICETREE_TRAVERSE_CONTEXT Context;
1607
1608 DPRINT("IopInitializePnpServices(%p, %d)\n", DeviceNode, BootDrivers);
1609
1610 if (BootDrivers)
1611 {
1612 IopInitDeviceTreeTraverseContext(
1613 &Context,
1614 DeviceNode,
1615 IopActionInitBootServices,
1616 DeviceNode);
1617 } else
1618 {
1619 IopInitDeviceTreeTraverseContext(
1620 &Context,
1621 DeviceNode,
1622 IopActionInitAllServices,
1623 DeviceNode);
1624 }
1625
1626 return IopTraverseDeviceTree(&Context);
1627 }
1628
1629
1630 NTSTATUS
1631 IopInvalidateDeviceRelations(
1632 IN PDEVICE_NODE DeviceNode,
1633 IN DEVICE_RELATION_TYPE Type)
1634 {
1635 DEVICETREE_TRAVERSE_CONTEXT Context;
1636 PDEVICE_RELATIONS DeviceRelations;
1637 IO_STATUS_BLOCK IoStatusBlock;
1638 PDEVICE_NODE ChildDeviceNode;
1639 IO_STACK_LOCATION Stack;
1640 BOOL BootDrivers;
1641 OBJECT_ATTRIBUTES ObjectAttributes;
1642 UNICODE_STRING LinkName;
1643 HANDLE Handle;
1644 NTSTATUS Status;
1645 ULONG i;
1646
1647 DPRINT("DeviceNode %x\n", DeviceNode);
1648
1649 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
1650
1651 Stack.Parameters.QueryDeviceRelations.Type = Type/*BusRelations*/;
1652
1653 Status = IopInitiatePnpIrp(
1654 DeviceNode->PhysicalDeviceObject,
1655 &IoStatusBlock,
1656 IRP_MN_QUERY_DEVICE_RELATIONS,
1657 &Stack);
1658 if (!NT_SUCCESS(Status))
1659 {
1660 DPRINT("IopInitiatePnpIrp() failed\n");
1661 return Status;
1662 }
1663
1664 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
1665
1666 if ((!DeviceRelations) || (DeviceRelations->Count <= 0))
1667 {
1668 DPRINT("No PDOs\n");
1669 if (DeviceRelations)
1670 {
1671 ExFreePool(DeviceRelations);
1672 }
1673 return STATUS_SUCCESS;
1674 }
1675
1676 DPRINT("Got %d PDOs\n", DeviceRelations->Count);
1677
1678 /*
1679 * Create device nodes for all discovered devices
1680 */
1681
1682 for (i = 0; i < DeviceRelations->Count; i++)
1683 {
1684 Status = IopCreateDeviceNode(
1685 DeviceNode,
1686 DeviceRelations->Objects[i],
1687 &ChildDeviceNode);
1688 DeviceNode->Flags |= DNF_ENUMERATED;
1689 if (!NT_SUCCESS(Status))
1690 {
1691 DPRINT("No resources\n");
1692 for (i = 0; i < DeviceRelations->Count; i++)
1693 ObDereferenceObject(DeviceRelations->Objects[i]);
1694 ExFreePool(DeviceRelations);
1695 return STATUS_INSUFFICIENT_RESOURCES;
1696 }
1697 }
1698 ExFreePool(DeviceRelations);
1699
1700 /*
1701 * Retrieve information about all discovered children from the bus driver
1702 */
1703
1704 IopInitDeviceTreeTraverseContext(
1705 &Context,
1706 DeviceNode,
1707 IopActionInterrogateDeviceStack,
1708 DeviceNode);
1709
1710 Status = IopTraverseDeviceTree(&Context);
1711 if (!NT_SUCCESS(Status))
1712 {
1713 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
1714 return Status;
1715 }
1716
1717 /*
1718 * Retrieve configuration from the registry for discovered children
1719 */
1720
1721 IopInitDeviceTreeTraverseContext(
1722 &Context,
1723 DeviceNode,
1724 IopActionConfigureChildServices,
1725 DeviceNode);
1726
1727 Status = IopTraverseDeviceTree(&Context);
1728 if (!NT_SUCCESS(Status))
1729 {
1730 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
1731 return Status;
1732 }
1733
1734 /*
1735 * Get the state of the system boot. If the \\SystemRoot link isn't
1736 * created yet, we will assume that it's possible to load only boot
1737 * drivers.
1738 */
1739
1740 RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
1741
1742 InitializeObjectAttributes(
1743 &ObjectAttributes,
1744 &LinkName,
1745 0,
1746 NULL,
1747 NULL);
1748
1749 Status = ZwOpenFile(
1750 &Handle,
1751 FILE_ALL_ACCESS,
1752 &ObjectAttributes,
1753 &IoStatusBlock,
1754 0,
1755 0);
1756 if(NT_SUCCESS(Status))
1757 {
1758 BootDrivers = FALSE;
1759 ZwClose(Handle);
1760 }
1761 else
1762 BootDrivers = TRUE;
1763
1764 /*
1765 * Initialize services for discovered children. Only boot drivers will
1766 * be loaded from boot driver!
1767 */
1768
1769 Status = IopInitializePnpServices(DeviceNode, BootDrivers);
1770 if (!NT_SUCCESS(Status))
1771 {
1772 DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
1773 return Status;
1774 }
1775
1776 return STATUS_SUCCESS;
1777 }
1778
1779
1780 VOID INIT_FUNCTION
1781 PnpInit(VOID)
1782 {
1783 PDEVICE_OBJECT Pdo;
1784 NTSTATUS Status;
1785
1786 DPRINT("PnpInit()\n");
1787
1788 KeInitializeSpinLock(&IopDeviceTreeLock);
1789
1790 /* Initialize PnP-Event notification support */
1791 Status = IopInitPlugPlayEvents();
1792 if (!NT_SUCCESS(Status))
1793 {
1794 CPRINT("IopInitPlugPlayEvents() failed\n");
1795 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
1796 }
1797
1798 /*
1799 * Create root device node
1800 */
1801
1802 Status = IopCreateDriverObject(&IopRootDriverObject, NULL, 0, FALSE, NULL, 0);
1803 if (!NT_SUCCESS(Status))
1804 {
1805 CPRINT("IoCreateDriverObject() failed\n");
1806 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
1807 }
1808
1809 Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
1810 0, FALSE, &Pdo);
1811 if (!NT_SUCCESS(Status))
1812 {
1813 CPRINT("IoCreateDevice() failed\n");
1814 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
1815 }
1816
1817 Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
1818 if (!NT_SUCCESS(Status))
1819 {
1820 CPRINT("Insufficient resources\n");
1821 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
1822 }
1823
1824 if (!IopCreateUnicodeString(&IopRootDeviceNode->InstancePath,
1825 L"HTREE\\Root\\0",
1826 PagedPool))
1827 {
1828 CPRINT("Failed to create the instance path!\n");
1829 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, STATUS_UNSUCCESSFUL, 0, 0, 0);
1830 }
1831
1832 /* Report the device to the user-mode pnp manager */
1833 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1834 &IopRootDeviceNode->InstancePath);
1835
1836 IopRootDeviceNode->PhysicalDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
1837 PnpRootDriverEntry(IopRootDriverObject, NULL);
1838 IopRootDriverObject->DriverExtension->AddDevice(
1839 IopRootDriverObject,
1840 IopRootDeviceNode->PhysicalDeviceObject);
1841 }
1842
1843 /* EOF */