e4fdd12b4f54ccf1332ae9ca1368c9a2c23c00a2
[reactos.git] / reactos / ntoskrnl / io / pnpmgr.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/io/pnpmgr.c
5 * PURPOSE: Initializes the PnP manager
6 *
7 * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
8 * Hervé Poussineau (hpoussin@reactos.org)
9 */
10
11 /* INCLUDES ******************************************************************/
12
13 #include <ntoskrnl.h>
14
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* GLOBALS *******************************************************************/
19
20 PDEVICE_NODE IopRootDeviceNode;
21 KSPIN_LOCK IopDeviceTreeLock;
22
23 /* DATA **********************************************************************/
24
25 PDRIVER_OBJECT IopRootDriverObject;
26 PIO_BUS_TYPE_GUID_LIST IopBusTypeGuidList = NULL;
27
28 // Static CRC table
29 ULONG crc32Table[256] =
30 {
31 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA,
32 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3,
33 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
34 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91,
35 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
36 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
37 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC,
38 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5,
39 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
40 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
41 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940,
42 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
43 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116,
44 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F,
45 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
46 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D,
47
48 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A,
49 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
50 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818,
51 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
52 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
53 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457,
54 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C,
55 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
56 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
57 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB,
58 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
59 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9,
60 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086,
61 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
62 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4,
63 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD,
64
65 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
66 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683,
67 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
68 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
69 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE,
70 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7,
71 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
72 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
73 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252,
74 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
75 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60,
76 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79,
77 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
78 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F,
79 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04,
80 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
81
82 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A,
83 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
84 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
85 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21,
86 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E,
87 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
88 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
89 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45,
90 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
91 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB,
92 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0,
93 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
94 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6,
95 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF,
96 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
97 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D,
98 };
99
100
101 /* FUNCTIONS *****************************************************************/
102
103 PDEVICE_NODE FASTCALL
104 IopGetDeviceNode(
105 PDEVICE_OBJECT DeviceObject)
106 {
107 return ((PEXTENDED_DEVOBJ_EXTENSION)DeviceObject->DeviceObjectExtension)->DeviceNode;
108 }
109
110 NTSTATUS
111 STDCALL
112 IopQueryDeviceCapabilities(PDEVICE_NODE DeviceNode,
113 PDEVICE_CAPABILITIES DeviceCaps)
114 {
115 IO_STATUS_BLOCK StatusBlock;
116 IO_STACK_LOCATION Stack;
117
118 /* Set up the Header */
119 RtlZeroMemory(DeviceCaps, sizeof(DEVICE_CAPABILITIES));
120 DeviceCaps->Size = sizeof(DEVICE_CAPABILITIES);
121 DeviceCaps->Version = 1;
122 DeviceCaps->Address = -1;
123 DeviceCaps->UINumber = -1;
124
125 /* Set up the Stack */
126 RtlZeroMemory(&Stack, sizeof(IO_STACK_LOCATION));
127 Stack.Parameters.DeviceCapabilities.Capabilities = DeviceCaps;
128
129 /* Send the IRP */
130 return IopInitiatePnpIrp(DeviceNode->PhysicalDeviceObject,
131 &StatusBlock,
132 IRP_MN_QUERY_CAPABILITIES,
133 &Stack);
134 }
135
136 /*
137 * @implemented
138 */
139 VOID
140 STDCALL
141 IoInvalidateDeviceRelations(
142 IN PDEVICE_OBJECT DeviceObject,
143 IN DEVICE_RELATION_TYPE Type)
144 {
145 IopInvalidateDeviceRelations(IopGetDeviceNode(DeviceObject), Type);
146 }
147
148 /*
149 * @unimplemented
150 */
151 NTSTATUS
152 STDCALL
153 IoGetDeviceProperty(
154 IN PDEVICE_OBJECT DeviceObject,
155 IN DEVICE_REGISTRY_PROPERTY DeviceProperty,
156 IN ULONG BufferLength,
157 OUT PVOID PropertyBuffer,
158 OUT PULONG ResultLength)
159 {
160 PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
161 DEVICE_CAPABILITIES DeviceCaps;
162 ULONG Length;
163 PVOID Data = NULL;
164 PWSTR Ptr;
165 NTSTATUS Status;
166
167 DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject, DeviceProperty);
168
169 if (DeviceNode == NULL)
170 return STATUS_INVALID_DEVICE_REQUEST;
171
172 switch (DeviceProperty)
173 {
174 case DevicePropertyBusNumber:
175 Length = sizeof(ULONG);
176 Data = &DeviceNode->ChildBusNumber;
177 break;
178
179 /* Complete, untested */
180 case DevicePropertyBusTypeGuid:
181 /* Sanity check */
182 if ((DeviceNode->ChildBusTypeIndex != 0xFFFF) &&
183 (DeviceNode->ChildBusTypeIndex < IopBusTypeGuidList->GuidCount))
184 {
185 /* Return the GUID */
186 *ResultLength = sizeof(GUID);
187
188 /* Check if the buffer given was large enough */
189 if (BufferLength < *ResultLength)
190 {
191 return STATUS_BUFFER_TOO_SMALL;
192 }
193
194 /* Copy the GUID */
195 RtlCopyMemory(PropertyBuffer,
196 &(IopBusTypeGuidList->Guids[DeviceNode->ChildBusTypeIndex]),
197 sizeof(GUID));
198 return STATUS_SUCCESS;
199 }
200 else
201 {
202 return STATUS_OBJECT_NAME_NOT_FOUND;
203 }
204 break;
205
206 case DevicePropertyLegacyBusType:
207 Length = sizeof(INTERFACE_TYPE);
208 Data = &DeviceNode->ChildInterfaceType;
209 break;
210
211 case DevicePropertyAddress:
212
213 /* Query the device caps */
214 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps);
215 if (NT_SUCCESS(Status) && (DeviceCaps.Address != -1))
216 {
217 /* Return length */
218 *ResultLength = sizeof(ULONG);
219
220 /* Check if the buffer given was large enough */
221 if (BufferLength < *ResultLength)
222 {
223 return STATUS_BUFFER_TOO_SMALL;
224 }
225
226 /* Return address */
227 *(PULONG)PropertyBuffer = DeviceCaps.Address;
228 return STATUS_SUCCESS;
229 }
230 else
231 {
232 return STATUS_OBJECT_NAME_NOT_FOUND;
233 }
234 break;
235
236 // case DevicePropertyUINumber:
237 // if (DeviceNode->CapabilityFlags == NULL)
238 // return STATUS_INVALID_DEVICE_REQUEST;
239 // Length = sizeof(ULONG);
240 // Data = &DeviceNode->CapabilityFlags->UINumber;
241 // break;
242
243 case DevicePropertyClassName:
244 case DevicePropertyClassGuid:
245 case DevicePropertyDriverKeyName:
246 case DevicePropertyManufacturer:
247 case DevicePropertyFriendlyName:
248 case DevicePropertyHardwareID:
249 case DevicePropertyCompatibleIDs:
250 case DevicePropertyDeviceDescription:
251 case DevicePropertyLocationInformation:
252 case DevicePropertyUINumber:
253 {
254 LPWSTR RegistryPropertyName, KeyNameBuffer;
255 UNICODE_STRING KeyName, ValueName;
256 OBJECT_ATTRIBUTES ObjectAttributes;
257 KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
258 ULONG ValueInformationLength;
259 HANDLE KeyHandle;
260 NTSTATUS Status;
261
262 switch (DeviceProperty)
263 {
264 case DevicePropertyClassName:
265 RegistryPropertyName = L"Class"; break;
266 case DevicePropertyClassGuid:
267 RegistryPropertyName = L"ClassGuid"; break;
268 case DevicePropertyDriverKeyName:
269 RegistryPropertyName = L"Driver"; break;
270 case DevicePropertyManufacturer:
271 RegistryPropertyName = L"Mfg"; break;
272 case DevicePropertyFriendlyName:
273 RegistryPropertyName = L"FriendlyName"; break;
274 case DevicePropertyHardwareID:
275 RegistryPropertyName = L"HardwareID"; break;
276 case DevicePropertyCompatibleIDs:
277 RegistryPropertyName = L"CompatibleIDs"; break;
278 case DevicePropertyDeviceDescription:
279 RegistryPropertyName = L"DeviceDesc"; break;
280 case DevicePropertyLocationInformation:
281 RegistryPropertyName = L"LocationInformation"; break;
282 case DevicePropertyUINumber:
283 RegistryPropertyName = L"UINumber"; break;
284 default:
285 RegistryPropertyName = NULL; break;
286 }
287
288 KeyNameBuffer = ExAllocatePool(PagedPool,
289 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
290
291 DPRINT("KeyNameBuffer: 0x%p, value %S\n",
292 KeyNameBuffer, RegistryPropertyName);
293
294 if (KeyNameBuffer == NULL)
295 return STATUS_INSUFFICIENT_RESOURCES;
296
297 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
298 wcscat(KeyNameBuffer, DeviceNode->InstancePath.Buffer);
299 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
300 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
301 OBJ_CASE_INSENSITIVE, NULL, NULL);
302
303 Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
304 ExFreePool(KeyNameBuffer);
305 if (!NT_SUCCESS(Status))
306 return Status;
307
308 RtlInitUnicodeString(&ValueName, RegistryPropertyName);
309 ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
310 Data[0]) + BufferLength;
311 ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
312 if (ValueInformation == NULL)
313 {
314 ZwClose(KeyHandle);
315 return STATUS_INSUFFICIENT_RESOURCES;
316 }
317
318 Status = ZwQueryValueKey(KeyHandle, &ValueName,
319 KeyValuePartialInformation, ValueInformation,
320 ValueInformationLength,
321 &ValueInformationLength);
322 *ResultLength = ValueInformation->DataLength;
323 ZwClose(KeyHandle);
324
325 if (ValueInformation->DataLength > BufferLength)
326 Status = STATUS_BUFFER_TOO_SMALL;
327
328 if (!NT_SUCCESS(Status))
329 {
330 ExFreePool(ValueInformation);
331 return Status;
332 }
333
334 /* FIXME: Verify the value (NULL-terminated, correct format). */
335
336 RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
337 ValueInformation->DataLength);
338 ExFreePool(ValueInformation);
339
340 return STATUS_SUCCESS;
341 }
342
343 case DevicePropertyBootConfiguration:
344 Length = 0;
345 if (DeviceNode->BootResources->Count != 0)
346 {
347 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
348 }
349 Data = &DeviceNode->BootResources;
350 break;
351
352 /* FIXME: use a translated boot configuration instead */
353 case DevicePropertyBootConfigurationTranslated:
354 Length = 0;
355 if (DeviceNode->BootResources->Count != 0)
356 {
357 Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
358 }
359 Data = &DeviceNode->BootResources;
360 break;
361
362 case DevicePropertyEnumeratorName:
363 Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
364 if (Ptr != NULL)
365 {
366 Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
367 Data = DeviceNode->InstancePath.Buffer;
368 }
369 else
370 {
371 Length = 0;
372 Data = NULL;
373 }
374 break;
375
376 case DevicePropertyPhysicalDeviceObjectName:
377 Length = DeviceNode->InstancePath.Length + sizeof(WCHAR);
378 Data = DeviceNode->InstancePath.Buffer;
379 break;
380
381 default:
382 return STATUS_INVALID_PARAMETER_2;
383 }
384
385 *ResultLength = Length;
386 if (BufferLength < Length)
387 return STATUS_BUFFER_TOO_SMALL;
388 RtlCopyMemory(PropertyBuffer, Data, Length);
389
390 /* Terminate the string */
391 if (DeviceProperty == DevicePropertyEnumeratorName
392 || DeviceProperty == DevicePropertyPhysicalDeviceObjectName)
393 {
394 Ptr = (PWSTR)PropertyBuffer;
395 Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
396 }
397
398 return STATUS_SUCCESS;
399 }
400
401 /*
402 * @unimplemented
403 */
404 VOID
405 STDCALL
406 IoInvalidateDeviceState(
407 IN PDEVICE_OBJECT PhysicalDeviceObject)
408 {
409 }
410
411 /**
412 * @name IoOpenDeviceRegistryKey
413 *
414 * Open a registry key unique for a specified driver or device instance.
415 *
416 * @param DeviceObject Device to get the registry key for.
417 * @param DevInstKeyType Type of the key to return.
418 * @param DesiredAccess Access mask (eg. KEY_READ | KEY_WRITE).
419 * @param DevInstRegKey Handle to the opened registry key on
420 * successful return.
421 *
422 * @return Status.
423 *
424 * @implemented
425 */
426 NTSTATUS
427 STDCALL
428 IoOpenDeviceRegistryKey(
429 IN PDEVICE_OBJECT DeviceObject,
430 IN ULONG DevInstKeyType,
431 IN ACCESS_MASK DesiredAccess,
432 OUT PHANDLE DevInstRegKey)
433 {
434 static WCHAR RootKeyName[] =
435 L"\\Registry\\Machine\\System\\CurrentControlSet\\";
436 static WCHAR ProfileKeyName[] =
437 L"Hardware Profiles\\Current\\System\\CurrentControlSet\\";
438 static WCHAR ClassKeyName[] = L"Control\\Class\\";
439 static WCHAR EnumKeyName[] = L"Enum\\";
440 static WCHAR DeviceParametersKeyName[] = L"Device Parameters\\";
441 ULONG KeyNameLength;
442 LPWSTR KeyNameBuffer;
443 UNICODE_STRING KeyName;
444 ULONG DriverKeyLength;
445 OBJECT_ATTRIBUTES ObjectAttributes;
446 PDEVICE_NODE DeviceNode = NULL;
447 NTSTATUS Status;
448
449 if ((DevInstKeyType & (PLUGPLAY_REGKEY_DEVICE | PLUGPLAY_REGKEY_DRIVER)) == 0)
450 return STATUS_INVALID_PARAMETER;
451
452 /*
453 * Calculate the length of the base key name. This is the full
454 * name for driver key or the name excluding "Device Parameters"
455 * subkey for device key.
456 */
457
458 KeyNameLength = sizeof(RootKeyName);
459 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
460 KeyNameLength += sizeof(ProfileKeyName) - sizeof(UNICODE_NULL);
461 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
462 {
463 KeyNameLength += sizeof(ClassKeyName) - sizeof(UNICODE_NULL);
464 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
465 0, NULL, &DriverKeyLength);
466 if (Status != STATUS_BUFFER_TOO_SMALL)
467 return Status;
468 KeyNameLength += DriverKeyLength;
469 }
470 else
471 {
472 DeviceNode = IopGetDeviceNode(DeviceObject);
473 KeyNameLength += sizeof(EnumKeyName) - sizeof(UNICODE_NULL) +
474 DeviceNode->InstancePath.Length;
475 }
476
477 /*
478 * Now allocate the buffer for the key name...
479 */
480
481 KeyNameBuffer = ExAllocatePool(PagedPool, KeyNameLength);
482 if (KeyNameBuffer == NULL)
483 return STATUS_INSUFFICIENT_RESOURCES;
484
485 KeyName.Length = 0;
486 KeyName.MaximumLength = KeyNameLength;
487 KeyName.Buffer = KeyNameBuffer;
488
489 /*
490 * ...and build the key name.
491 */
492
493 KeyName.Length += sizeof(RootKeyName) - sizeof(UNICODE_NULL);
494 RtlCopyMemory(KeyNameBuffer, RootKeyName, KeyName.Length);
495
496 if (DevInstKeyType & PLUGPLAY_REGKEY_CURRENT_HWPROFILE)
497 RtlAppendUnicodeToString(&KeyName, ProfileKeyName);
498
499 if (DevInstKeyType & PLUGPLAY_REGKEY_DRIVER)
500 {
501 RtlAppendUnicodeToString(&KeyName, ClassKeyName);
502 Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
503 DriverKeyLength, KeyNameBuffer +
504 (KeyName.Length / sizeof(WCHAR)),
505 &DriverKeyLength);
506 if (!NT_SUCCESS(Status))
507 {
508 ExFreePool(KeyNameBuffer);
509 return Status;
510 }
511 KeyName.Length += DriverKeyLength - sizeof(UNICODE_NULL);
512 }
513 else
514 {
515 RtlAppendUnicodeToString(&KeyName, EnumKeyName);
516 Status = RtlAppendUnicodeStringToString(&KeyName, &DeviceNode->InstancePath);
517 if (DeviceNode->InstancePath.Length == 0)
518 {
519 ExFreePool(KeyNameBuffer);
520 return Status;
521 }
522 }
523
524 /*
525 * Open the base key.
526 */
527
528 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
529 OBJ_CASE_INSENSITIVE, NULL, NULL);
530 Status = ZwOpenKey(DevInstRegKey, DesiredAccess, &ObjectAttributes);
531 ExFreePool(KeyNameBuffer);
532
533 /*
534 * For driver key we're done now. Also if the base key doesn't
535 * exist we can bail out with error...
536 */
537
538 if ((DevInstKeyType & PLUGPLAY_REGKEY_DRIVER) || !NT_SUCCESS(Status))
539 return Status;
540
541 /*
542 * Let's go further. For device key we must open "Device Parameters"
543 * subkey and create it if it doesn't exist yet.
544 */
545
546 RtlInitUnicodeString(&KeyName, DeviceParametersKeyName);
547 InitializeObjectAttributes(&ObjectAttributes, &KeyName,
548 OBJ_CASE_INSENSITIVE, *DevInstRegKey, NULL);
549 Status = ZwCreateKey(DevInstRegKey, DesiredAccess, &ObjectAttributes,
550 0, NULL, REG_OPTION_NON_VOLATILE, NULL);
551 ZwClose(ObjectAttributes.RootDirectory);
552
553 return Status;
554 }
555
556 /*
557 * @unimplemented
558 */
559 VOID
560 STDCALL
561 IoRequestDeviceEject(
562 IN PDEVICE_OBJECT PhysicalDeviceObject
563 )
564 {
565 UNIMPLEMENTED;
566 }
567
568
569 NTSTATUS
570 IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
571 {
572 KIRQL OldIrql;
573
574 if (PopSystemPowerDeviceNode)
575 {
576 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
577 *DeviceObject = PopSystemPowerDeviceNode->PhysicalDeviceObject;
578 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
579
580 return STATUS_SUCCESS;
581 }
582
583 return STATUS_UNSUCCESSFUL;
584 }
585
586 USHORT
587 STDCALL
588 IopGetBusTypeGuidIndex(LPGUID BusTypeGuid)
589 {
590 USHORT i = 0, FoundIndex = 0xFFFF;
591 ULONG NewSize;
592 PVOID NewList;
593
594 /* Acquire the lock */
595 ExAcquireFastMutex(&IopBusTypeGuidList->Lock);
596
597 /* Loop all entries */
598 while (i < IopBusTypeGuidList->GuidCount)
599 {
600 /* Try to find a match */
601 if (RtlCompareMemory(BusTypeGuid,
602 &IopBusTypeGuidList->Guids[i],
603 sizeof(GUID)))
604 {
605 /* Found it */
606 FoundIndex = i;
607 goto Quickie;
608 }
609 }
610
611 /* Check if we have to grow the list */
612 if (IopBusTypeGuidList->GuidCount)
613 {
614 /* Calculate the new size */
615 NewSize = sizeof(IO_BUS_TYPE_GUID_LIST) +
616 (sizeof(GUID) * IopBusTypeGuidList->GuidCount);
617
618 /* Allocate the new copy */
619 NewList = ExAllocatePool(PagedPool, NewSize);
620
621 /* Now copy them, decrease the size too */
622 NewSize -= sizeof(GUID);
623 RtlCopyMemory(NewList, IopBusTypeGuidList, NewSize);
624
625 /* Free the old list */
626 ExFreePool(IopBusTypeGuidList);
627
628 /* Use the new buffer */
629 IopBusTypeGuidList = NewList;
630 }
631
632 /* Copy the new GUID */
633 RtlCopyMemory(&IopBusTypeGuidList->Guids[IopBusTypeGuidList->GuidCount],
634 BusTypeGuid,
635 sizeof(GUID));
636
637 /* The new entry is the index */
638 FoundIndex = IopBusTypeGuidList->GuidCount;
639 IopBusTypeGuidList->GuidCount++;
640
641 Quickie:
642 ExReleaseFastMutex(&IopBusTypeGuidList->Lock);
643 return FoundIndex;
644 }
645
646 /*
647 * DESCRIPTION
648 * Creates a device node
649 *
650 * ARGUMENTS
651 * ParentNode = Pointer to parent device node
652 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
653 * to have the root device node create one
654 * (eg. for legacy drivers)
655 * DeviceNode = Pointer to storage for created device node
656 *
657 * RETURN VALUE
658 * Status
659 */
660 NTSTATUS
661 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
662 PDEVICE_OBJECT PhysicalDeviceObject,
663 PDEVICE_NODE *DeviceNode)
664 {
665 PDEVICE_NODE Node;
666 NTSTATUS Status;
667 KIRQL OldIrql;
668
669 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p\n",
670 ParentNode, PhysicalDeviceObject);
671
672 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
673 if (!Node)
674 {
675 return STATUS_INSUFFICIENT_RESOURCES;
676 }
677
678 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
679
680 if (!PhysicalDeviceObject)
681 {
682 Status = PnpRootCreateDevice(&PhysicalDeviceObject);
683 if (!NT_SUCCESS(Status))
684 {
685 ExFreePool(Node);
686 return Status;
687 }
688
689 /* This is for drivers passed on the command line to ntoskrnl.exe */
690 IopDeviceNodeSetFlag(Node, DNF_STARTED);
691 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
692 }
693
694 Node->PhysicalDeviceObject = PhysicalDeviceObject;
695
696 ((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode = Node;
697
698 if (ParentNode)
699 {
700 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
701 Node->Parent = ParentNode;
702 Node->NextSibling = ParentNode->Child;
703 if (ParentNode->Child != NULL)
704 {
705 ParentNode->Child->PrevSibling = Node;
706 }
707 ParentNode->Child = Node;
708 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
709 Node->Level = ParentNode->Level + 1;
710 }
711
712 *DeviceNode = Node;
713
714 return STATUS_SUCCESS;
715 }
716
717 NTSTATUS
718 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
719 {
720 KIRQL OldIrql;
721
722 /* All children must be deleted before a parent is deleted */
723 ASSERT(!DeviceNode->Child);
724
725 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
726
727 ASSERT(DeviceNode->PhysicalDeviceObject);
728
729 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
730
731 /* Unlink from parent if it exists */
732
733 if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
734 {
735 DeviceNode->Parent->Child = DeviceNode->NextSibling;
736 }
737
738 /* Unlink from sibling list */
739
740 if (DeviceNode->PrevSibling)
741 {
742 DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
743 }
744
745 if (DeviceNode->NextSibling)
746 {
747 DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
748 }
749
750 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
751
752 RtlFreeUnicodeString(&DeviceNode->InstancePath);
753
754 RtlFreeUnicodeString(&DeviceNode->ServiceName);
755
756 if (DeviceNode->ResourceList)
757 {
758 ExFreePool(DeviceNode->ResourceList);
759 }
760
761 if (DeviceNode->ResourceListTranslated)
762 {
763 ExFreePool(DeviceNode->ResourceListTranslated);
764 }
765
766 if (DeviceNode->ResourceRequirements)
767 {
768 ExFreePool(DeviceNode->ResourceRequirements);
769 }
770
771 if (DeviceNode->BootResources)
772 {
773 ExFreePool(DeviceNode->BootResources);
774 }
775
776 ExFreePool(DeviceNode);
777
778 return STATUS_SUCCESS;
779 }
780
781 NTSTATUS
782 IopInitiatePnpIrp(
783 PDEVICE_OBJECT DeviceObject,
784 PIO_STATUS_BLOCK IoStatusBlock,
785 ULONG MinorFunction,
786 PIO_STACK_LOCATION Stack OPTIONAL)
787 {
788 PDEVICE_OBJECT TopDeviceObject;
789 PIO_STACK_LOCATION IrpSp;
790 NTSTATUS Status;
791 KEVENT Event;
792 PIRP Irp;
793
794 /* Always call the top of the device stack */
795 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
796
797 KeInitializeEvent(
798 &Event,
799 NotificationEvent,
800 FALSE);
801
802 Irp = IoBuildSynchronousFsdRequest(
803 IRP_MJ_PNP,
804 TopDeviceObject,
805 NULL,
806 0,
807 NULL,
808 &Event,
809 IoStatusBlock);
810
811 /* PNP IRPs are always initialized with a status code of
812 STATUS_NOT_IMPLEMENTED */
813 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
814 Irp->IoStatus.Information = 0;
815
816 IrpSp = IoGetNextIrpStackLocation(Irp);
817 IrpSp->MinorFunction = MinorFunction;
818
819 if (Stack)
820 {
821 RtlMoveMemory(
822 &IrpSp->Parameters,
823 &Stack->Parameters,
824 sizeof(Stack->Parameters));
825 }
826
827 Status = IoCallDriver(TopDeviceObject, Irp);
828 if (Status == STATUS_PENDING)
829 {
830 KeWaitForSingleObject(
831 &Event,
832 Executive,
833 KernelMode,
834 FALSE,
835 NULL);
836 Status = IoStatusBlock->Status;
837 }
838
839 ObDereferenceObject(TopDeviceObject);
840
841 return Status;
842 }
843
844
845 NTSTATUS
846 IopTraverseDeviceTreeNode(
847 PDEVICETREE_TRAVERSE_CONTEXT Context)
848 {
849 PDEVICE_NODE ParentDeviceNode;
850 PDEVICE_NODE ChildDeviceNode;
851 NTSTATUS Status;
852
853 /* Copy context data so we don't overwrite it in subsequent calls to this function */
854 ParentDeviceNode = Context->DeviceNode;
855
856 /* Call the action routine */
857 Status = (Context->Action)(ParentDeviceNode, Context->Context);
858 if (!NT_SUCCESS(Status))
859 {
860 return Status;
861 }
862
863 /* Traversal of all children nodes */
864 for (ChildDeviceNode = ParentDeviceNode->Child;
865 ChildDeviceNode != NULL;
866 ChildDeviceNode = ChildDeviceNode->NextSibling)
867 {
868 /* Pass the current device node to the action routine */
869 Context->DeviceNode = ChildDeviceNode;
870
871 Status = IopTraverseDeviceTreeNode(Context);
872 if (!NT_SUCCESS(Status))
873 {
874 return Status;
875 }
876 }
877
878 return Status;
879 }
880
881
882 NTSTATUS
883 IopTraverseDeviceTree(
884 PDEVICETREE_TRAVERSE_CONTEXT Context)
885 {
886 NTSTATUS Status;
887
888 DPRINT("Context 0x%p\n", Context);
889
890 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
891 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
892
893 /* Start from the specified device node */
894 Context->DeviceNode = Context->FirstDeviceNode;
895
896 /* Recursively traverse the device tree */
897 Status = IopTraverseDeviceTreeNode(Context);
898 if (Status == STATUS_UNSUCCESSFUL)
899 {
900 /* The action routine just wanted to terminate the traversal with status
901 code STATUS_SUCCESS */
902 Status = STATUS_SUCCESS;
903 }
904
905 return Status;
906 }
907
908
909 static NTSTATUS
910 IopCreateDeviceKeyPath(PWSTR Path,
911 PHANDLE Handle)
912 {
913 OBJECT_ATTRIBUTES ObjectAttributes;
914 WCHAR KeyBuffer[MAX_PATH];
915 UNICODE_STRING KeyName;
916 HANDLE KeyHandle;
917 NTSTATUS Status;
918 PWCHAR Current;
919 PWCHAR Next;
920
921 *Handle = NULL;
922
923 if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
924 {
925 return STATUS_INVALID_PARAMETER;
926 }
927
928 wcsncpy (KeyBuffer, Path, MAX_PATH-1);
929
930 /* Skip \\Registry\\ */
931 Current = KeyBuffer;
932 Current = wcschr (Current, L'\\') + 1;
933 Current = wcschr (Current, L'\\') + 1;
934
935 while (TRUE)
936 {
937 Next = wcschr (Current, L'\\');
938 if (Next == NULL)
939 {
940 /* The end */
941 }
942 else
943 {
944 *Next = 0;
945 }
946
947 RtlInitUnicodeString (&KeyName, KeyBuffer);
948 InitializeObjectAttributes (&ObjectAttributes,
949 &KeyName,
950 OBJ_CASE_INSENSITIVE,
951 NULL,
952 NULL);
953
954 DPRINT("Create '%S'\n", KeyName.Buffer);
955
956 Status = ZwCreateKey (&KeyHandle,
957 KEY_ALL_ACCESS,
958 &ObjectAttributes,
959 0,
960 NULL,
961 0,
962 NULL);
963 if (!NT_SUCCESS (Status))
964 {
965 DPRINT ("ZwCreateKey() failed with status %x\n", Status);
966 return Status;
967 }
968
969 if (Next == NULL)
970 {
971 *Handle = KeyHandle;
972 return STATUS_SUCCESS;
973 }
974 else
975 {
976 ZwClose (KeyHandle);
977 *Next = L'\\';
978 }
979
980 Current = Next + 1;
981 }
982
983 return STATUS_UNSUCCESSFUL;
984 }
985
986
987 static NTSTATUS
988 IopSetDeviceInstanceData(HANDLE InstanceKey,
989 PDEVICE_NODE DeviceNode)
990 {
991 OBJECT_ATTRIBUTES ObjectAttributes;
992 UNICODE_STRING KeyName;
993 HANDLE LogConfKey;
994 ULONG ResCount;
995 ULONG ListSize;
996 NTSTATUS Status;
997
998 DPRINT("IopSetDeviceInstanceData() called\n");
999
1000 /* Create the 'LogConf' key */
1001 RtlInitUnicodeString(&KeyName,
1002 L"LogConf");
1003 InitializeObjectAttributes(&ObjectAttributes,
1004 &KeyName,
1005 OBJ_CASE_INSENSITIVE,
1006 InstanceKey,
1007 NULL);
1008 Status = ZwCreateKey(&LogConfKey,
1009 KEY_ALL_ACCESS,
1010 &ObjectAttributes,
1011 0,
1012 NULL,
1013 0,
1014 NULL);
1015 if (NT_SUCCESS(Status))
1016 {
1017 /* Set 'BootConfig' value */
1018 if (DeviceNode->BootResources != NULL)
1019 {
1020 ResCount = DeviceNode->BootResources->Count;
1021 if (ResCount != 0)
1022 {
1023 ListSize = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
1024
1025 RtlInitUnicodeString(&KeyName,
1026 L"BootConfig");
1027 Status = ZwSetValueKey(LogConfKey,
1028 &KeyName,
1029 0,
1030 REG_RESOURCE_LIST,
1031 &DeviceNode->BootResources,
1032 ListSize);
1033 }
1034 }
1035
1036 /* Set 'BasicConfigVector' value */
1037 if (DeviceNode->ResourceRequirements != NULL &&
1038 DeviceNode->ResourceRequirements->ListSize != 0)
1039 {
1040 RtlInitUnicodeString(&KeyName,
1041 L"BasicConfigVector");
1042 Status = ZwSetValueKey(LogConfKey,
1043 &KeyName,
1044 0,
1045 REG_RESOURCE_REQUIREMENTS_LIST,
1046 DeviceNode->ResourceRequirements,
1047 DeviceNode->ResourceRequirements->ListSize);
1048 }
1049
1050 ZwClose(LogConfKey);
1051 }
1052
1053 #if 0
1054 if (DeviceNode->PhysicalDeviceObject != NULL)
1055 {
1056 /* Create the 'Control' key */
1057 RtlInitUnicodeString(&KeyName,
1058 L"Control");
1059 InitializeObjectAttributes(&ObjectAttributes,
1060 &KeyName,
1061 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
1062 InstanceKey,
1063 NULL);
1064 Status = ZwCreateKey(&LogConfKey,
1065 KEY_ALL_ACCESS,
1066 &ObjectAttributes,
1067 0,
1068 NULL,
1069 REG_OPTION_VOLATILE,
1070 NULL);
1071 if (NT_SUCCESS(Status))
1072 {
1073 ULONG Reference = (ULONG)DeviceNode->PhysicalDeviceObject;
1074 RtlInitUnicodeString(&KeyName,
1075 L"DeviceReference");
1076 Status = ZwSetValueKey(LogConfKey,
1077 &KeyName,
1078 0,
1079 REG_DWORD,
1080 &Reference,
1081 sizeof(PVOID));
1082
1083 ZwClose(LogConfKey);
1084 }
1085 }
1086 #endif
1087
1088 DPRINT("IopSetDeviceInstanceData() done\n");
1089
1090 return STATUS_SUCCESS;
1091 }
1092
1093
1094 NTSTATUS
1095 IopAssignDeviceResources(
1096 PDEVICE_NODE DeviceNode)
1097 {
1098 PIO_RESOURCE_LIST ResourceList;
1099 PIO_RESOURCE_DESCRIPTOR ResourceDescriptor;
1100 PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw, DescriptorTranslated;
1101 ULONG NumberOfResources = 0;
1102 ULONG i;
1103 NTSTATUS Status;
1104
1105 /* Fill DeviceNode->ResourceList and DeviceNode->ResourceListTranslated;
1106 * by using DeviceNode->ResourceRequirements */
1107
1108 if (!DeviceNode->ResourceRequirements
1109 || DeviceNode->ResourceRequirements->AlternativeLists == 0)
1110 {
1111 DeviceNode->ResourceList = DeviceNode->ResourceListTranslated = NULL;
1112 return STATUS_SUCCESS;
1113 }
1114
1115 /* FIXME: that's here that PnP arbiter should go */
1116 /* Actually, simply use resource list #0 as assigned resource list */
1117 ResourceList = &DeviceNode->ResourceRequirements->List[0];
1118 if (ResourceList->Version != 1 || ResourceList->Revision != 1)
1119 {
1120 Status = STATUS_REVISION_MISMATCH;
1121 goto ByeBye;
1122 }
1123
1124 DeviceNode->ResourceList = ExAllocatePool(PagedPool,
1125 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1126 if (!DeviceNode->ResourceList)
1127 {
1128 Status = STATUS_INSUFFICIENT_RESOURCES;
1129 goto ByeBye;
1130 }
1131
1132 DeviceNode->ResourceListTranslated = ExAllocatePool(PagedPool,
1133 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1134 if (!DeviceNode->ResourceListTranslated)
1135 {
1136 Status = STATUS_INSUFFICIENT_RESOURCES;
1137 goto ByeBye;
1138 }
1139
1140 DeviceNode->ResourceList->Count = 1;
1141 DeviceNode->ResourceList->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1142 DeviceNode->ResourceList->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1143 DeviceNode->ResourceList->List[0].PartialResourceList.Version = 1;
1144 DeviceNode->ResourceList->List[0].PartialResourceList.Revision = 1;
1145
1146 DeviceNode->ResourceListTranslated->Count = 1;
1147 DeviceNode->ResourceListTranslated->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1148 DeviceNode->ResourceListTranslated->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1149 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Version = 1;
1150 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Revision = 1;
1151
1152 for (i = 0; i < ResourceList->Count; i++)
1153 {
1154 ResourceDescriptor = &ResourceList->Descriptors[i];
1155
1156 if (ResourceDescriptor->Option == 0 || ResourceDescriptor->Option == IO_RESOURCE_PREFERRED)
1157 {
1158 DescriptorRaw = &DeviceNode->ResourceList->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1159 DescriptorTranslated = &DeviceNode->ResourceListTranslated->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1160 NumberOfResources++;
1161
1162 /* Copy ResourceDescriptor to DescriptorRaw and DescriptorTranslated */
1163 DescriptorRaw->Type = DescriptorTranslated->Type = ResourceDescriptor->Type;
1164 DescriptorRaw->ShareDisposition = DescriptorTranslated->ShareDisposition = ResourceDescriptor->ShareDisposition;
1165 DescriptorRaw->Flags = DescriptorTranslated->Flags = ResourceDescriptor->Flags;
1166 switch (ResourceDescriptor->Type)
1167 {
1168 case CmResourceTypePort:
1169 {
1170 ULONG AddressSpace = 0; /* IO space */
1171 DescriptorRaw->u.Port.Start = ResourceDescriptor->u.Port.MinimumAddress;
1172 DescriptorRaw->u.Port.Length = DescriptorTranslated->u.Port.Length
1173 = ResourceDescriptor->u.Port.Length;
1174 if (!HalTranslateBusAddress(
1175 DeviceNode->ResourceRequirements->InterfaceType,
1176 DeviceNode->ResourceRequirements->BusNumber,
1177 DescriptorRaw->u.Port.Start,
1178 &AddressSpace,
1179 &DescriptorTranslated->u.Port.Start))
1180 {
1181 Status = STATUS_UNSUCCESSFUL;
1182 goto ByeBye;
1183 }
1184 break;
1185 }
1186 case CmResourceTypeInterrupt:
1187 {
1188 INTERFACE_TYPE BusType;
1189 ULONG SlotNumber;
1190 ULONG ret;
1191 UCHAR Irq;
1192
1193 DescriptorRaw->u.Interrupt.Level = 0;
1194 DescriptorRaw->u.Interrupt.Vector = ResourceDescriptor->u.Interrupt.MinimumVector;
1195 /* FIXME: HACK: if we have a PCI device, we try
1196 * to keep the IRQ assigned by the BIOS */
1197 if (NT_SUCCESS(IoGetDeviceProperty(
1198 DeviceNode->PhysicalDeviceObject,
1199 DevicePropertyLegacyBusType,
1200 sizeof(INTERFACE_TYPE),
1201 &BusType,
1202 &ret)) && BusType == PCIBus)
1203 {
1204 /* We have a PCI bus */
1205 if (NT_SUCCESS(IoGetDeviceProperty(
1206 DeviceNode->PhysicalDeviceObject,
1207 DevicePropertyAddress,
1208 sizeof(ULONG),
1209 &SlotNumber,
1210 &ret)) && SlotNumber > 0)
1211 {
1212 /* We have a good slot number */
1213 ret = HalGetBusDataByOffset(PCIConfiguration,
1214 DeviceNode->ResourceRequirements->BusNumber,
1215 SlotNumber,
1216 &Irq,
1217 0x3c /* PCI_INTERRUPT_LINE */,
1218 sizeof(UCHAR));
1219 if (ret != 0 && ret != 2
1220 && ResourceDescriptor->u.Interrupt.MinimumVector <= Irq
1221 && ResourceDescriptor->u.Interrupt.MaximumVector >= Irq)
1222 {
1223 /* The device already has an assigned IRQ */
1224 DescriptorRaw->u.Interrupt.Vector = Irq;
1225 }
1226 else
1227 {
1228 DPRINT1("Trying to assign IRQ 0x%lx to %wZ\n",
1229 DescriptorRaw->u.Interrupt.Vector,
1230 &DeviceNode->InstancePath);
1231 Irq = (UCHAR)DescriptorRaw->u.Interrupt.Vector;
1232 ret = HalSetBusDataByOffset(PCIConfiguration,
1233 DeviceNode->ResourceRequirements->BusNumber,
1234 SlotNumber,
1235 &Irq,
1236 0x3c /* PCI_INTERRUPT_LINE */,
1237 sizeof(UCHAR));
1238 if (ret == 0 || ret == 2)
1239 KEBUGCHECK(0);
1240 }
1241 }
1242 }
1243
1244 DescriptorTranslated->u.Interrupt.Level = 0;
1245 DescriptorTranslated->u.Interrupt.Vector = HalGetInterruptVector(
1246 DeviceNode->ResourceRequirements->InterfaceType,
1247 DeviceNode->ResourceRequirements->BusNumber,
1248 DescriptorRaw->u.Interrupt.Level,
1249 DescriptorRaw->u.Interrupt.Vector,
1250 (PKIRQL)&DescriptorTranslated->u.Interrupt.Level,
1251 &DescriptorRaw->u.Interrupt.Affinity);
1252 DescriptorTranslated->u.Interrupt.Affinity = DescriptorRaw->u.Interrupt.Affinity;
1253 break;
1254 }
1255 case CmResourceTypeMemory:
1256 {
1257 ULONG AddressSpace = 1; /* Memory space */
1258 DescriptorRaw->u.Memory.Start = ResourceDescriptor->u.Memory.MinimumAddress;
1259 DescriptorRaw->u.Memory.Length = DescriptorTranslated->u.Memory.Length
1260 = ResourceDescriptor->u.Memory.Length;
1261 if (!HalTranslateBusAddress(
1262 DeviceNode->ResourceRequirements->InterfaceType,
1263 DeviceNode->ResourceRequirements->BusNumber,
1264 DescriptorRaw->u.Memory.Start,
1265 &AddressSpace,
1266 &DescriptorTranslated->u.Memory.Start))
1267 {
1268 Status = STATUS_UNSUCCESSFUL;
1269 goto ByeBye;
1270 }
1271 break;
1272 }
1273 case CmResourceTypeDma:
1274 {
1275 DescriptorRaw->u.Dma.Channel = DescriptorTranslated->u.Dma.Channel
1276 = ResourceDescriptor->u.Dma.MinimumChannel;
1277 DescriptorRaw->u.Dma.Port = DescriptorTranslated->u.Dma.Port
1278 = 0; /* FIXME */
1279 DescriptorRaw->u.Dma.Reserved1 = DescriptorTranslated->u.Dma.Reserved1
1280 = 0;
1281 break;
1282 }
1283 /*case CmResourceTypeBusNumber:
1284 {
1285 DescriptorRaw->u.BusNumber.Start = DescriptorTranslated->u.BusNumber.Start
1286 = ResourceDescriptor->u.BusNumber.MinBusNumber;
1287 DescriptorRaw->u.BusNumber.Length = DescriptorTranslated->u.BusNumber.Length
1288 = ResourceDescriptor->u.BusNumber.Length;
1289 DescriptorRaw->u.BusNumber.Reserved = DescriptorTranslated->u.BusNumber.Reserved
1290 = ResourceDescriptor->u.BusNumber.Reserved;
1291 break;
1292 }*/
1293 /*CmResourceTypeDevicePrivate:
1294 case CmResourceTypePcCardConfig:
1295 case CmResourceTypeMfCardConfig:
1296 {
1297 RtlCopyMemory(
1298 &DescriptorRaw->u.DevicePrivate,
1299 &ResourceDescriptor->u.DevicePrivate,
1300 sizeof(ResourceDescriptor->u.DevicePrivate));
1301 RtlCopyMemory(
1302 &DescriptorTranslated->u.DevicePrivate,
1303 &ResourceDescriptor->u.DevicePrivate,
1304 sizeof(ResourceDescriptor->u.DevicePrivate));
1305 break;
1306 }*/
1307 default:
1308 DPRINT1("IopAssignDeviceResources(): unknown resource descriptor type 0x%x\n", ResourceDescriptor->Type);
1309 NumberOfResources--;
1310 }
1311 }
1312
1313 }
1314
1315 DeviceNode->ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
1316 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Count = NumberOfResources;
1317
1318 return STATUS_SUCCESS;
1319
1320 ByeBye:
1321 if (DeviceNode->ResourceList)
1322 {
1323 ExFreePool(DeviceNode->ResourceList);
1324 DeviceNode->ResourceList = NULL;
1325 }
1326 if (DeviceNode->ResourceListTranslated)
1327 {
1328 ExFreePool(DeviceNode->ResourceListTranslated);
1329 DeviceNode->ResourceListTranslated = NULL;
1330 }
1331
1332 return Status;
1333 }
1334
1335
1336 /*
1337 * IopGetParentIdPrefix
1338 *
1339 * Retrieve (or create) a string which identifies a device.
1340 *
1341 * Parameters
1342 * DeviceNode
1343 * Pointer to device node.
1344 * ParentIdPrefix
1345 * Pointer to the string where is returned the parent node identifier
1346 *
1347 * Remarks
1348 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1349 * valid and its Buffer field is NULL-terminated. The caller needs to
1350 * to free the string with RtlFreeUnicodeString when it is no longer
1351 * needed.
1352 */
1353
1354 NTSTATUS
1355 IopGetParentIdPrefix(
1356 PDEVICE_NODE DeviceNode,
1357 PUNICODE_STRING ParentIdPrefix)
1358 {
1359 ULONG KeyNameBufferLength;
1360 PWSTR KeyNameBuffer = NULL;
1361 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation = NULL;
1362 UNICODE_STRING KeyName;
1363 UNICODE_STRING KeyValue;
1364 UNICODE_STRING ValueName;
1365 OBJECT_ATTRIBUTES ObjectAttributes;
1366 HANDLE hKey = INVALID_HANDLE_VALUE;
1367 PBYTE currentByte;
1368 ULONG crc32 = 0;
1369 ULONG i;
1370 NTSTATUS Status;
1371
1372 /* HACK: As long as some devices have a NULL device
1373 * instance path, the following test is required :(
1374 */
1375 if (DeviceNode->Parent->InstancePath.Length == 0)
1376 return STATUS_UNSUCCESSFUL;
1377
1378 /* 1. Try to retrieve ParentIdPrefix from registry */
1379 KeyNameBufferLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + MAX_PATH * sizeof(WCHAR);
1380 ParentIdPrefixInformation = ExAllocatePool(PagedPool, KeyNameBufferLength + sizeof(WCHAR));
1381 if (!ParentIdPrefixInformation)
1382 {
1383 Status = STATUS_INSUFFICIENT_RESOURCES;
1384 goto cleanup;
1385 }
1386 KeyNameBuffer = ExAllocatePool(PagedPool, (49 * sizeof(WCHAR)) + DeviceNode->Parent->InstancePath.Length);
1387 if (!KeyNameBuffer)
1388 {
1389 Status = STATUS_INSUFFICIENT_RESOURCES;
1390 goto cleanup;
1391 }
1392 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1393 wcscat(KeyNameBuffer, DeviceNode->Parent->InstancePath.Buffer);
1394 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
1395 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
1396 Status = ZwOpenKey(&hKey, KEY_QUERY_VALUE | KEY_SET_VALUE, &ObjectAttributes);
1397 if (!NT_SUCCESS(Status))
1398 goto cleanup;
1399 RtlInitUnicodeString(&ValueName, L"ParentIdPrefix");
1400 Status = ZwQueryValueKey(
1401 hKey, &ValueName,
1402 KeyValuePartialInformation, ParentIdPrefixInformation,
1403 KeyNameBufferLength, &KeyNameBufferLength);
1404 if (NT_SUCCESS(Status))
1405 {
1406 if (ParentIdPrefixInformation->Type != REG_SZ)
1407 Status = STATUS_UNSUCCESSFUL;
1408 goto cleanup;
1409 }
1410 if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
1411 goto cleanup;
1412
1413 /* 2. Create the ParentIdPrefix value */
1414 currentByte = (PBYTE)DeviceNode->Parent;
1415 for (i = 0; i < sizeof(DEVICE_NODE); i++, currentByte++)
1416 crc32 = (crc32 >> 8) ^ crc32Table[*currentByte ^ (crc32 & 0xff)];
1417 crc32 = ~crc32;
1418 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1419 swprintf(KeyValue.Buffer, L"%lx&%lx", DeviceNode->Parent->Level, crc32);
1420
1421 /* 3. Try to write the ParentIdPrefix to registry */
1422 RtlInitUnicodeString(&KeyValue, KeyValue.Buffer);
1423 Status = ZwSetValueKey(
1424 hKey, &ValueName,
1425 0, REG_SZ,
1426 (PVOID)KeyValue.Buffer,
1427 (wcslen(KeyValue.Buffer) + 1) * sizeof(WCHAR));
1428
1429 cleanup:
1430 if (NT_SUCCESS(Status))
1431 {
1432 /* Duplicate the string to return it */
1433 KeyValue.Length = KeyValue.MaximumLength = ParentIdPrefixInformation->DataLength;
1434 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1435 Status = RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, &KeyValue, ParentIdPrefix);
1436 }
1437 ExFreePool(ParentIdPrefixInformation);
1438 ExFreePool(KeyNameBuffer);
1439 if (hKey != INVALID_HANDLE_VALUE)
1440 ZwClose(hKey);
1441 return Status;
1442 }
1443
1444
1445 /*
1446 * IopActionInterrogateDeviceStack
1447 *
1448 * Retrieve information for all (direct) child nodes of a parent node.
1449 *
1450 * Parameters
1451 * DeviceNode
1452 * Pointer to device node.
1453 * Context
1454 * Pointer to parent node to retrieve child node information for.
1455 *
1456 * Remarks
1457 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1458 * when we reach a device node which is not a direct child of the device
1459 * node for which we retrieve information of child nodes for. Any errors
1460 * that occur is logged instead so that all child services have a chance
1461 * of being interrogated.
1462 */
1463
1464 NTSTATUS
1465 IopActionInterrogateDeviceStack(
1466 PDEVICE_NODE DeviceNode,
1467 PVOID Context)
1468 {
1469 IO_STATUS_BLOCK IoStatusBlock;
1470 PDEVICE_NODE ParentDeviceNode;
1471 WCHAR InstancePath[MAX_PATH];
1472 IO_STACK_LOCATION Stack;
1473 NTSTATUS Status;
1474 PWSTR KeyBuffer;
1475 PWSTR Ptr;
1476 USHORT Length;
1477 USHORT TotalLength;
1478 ULONG RequiredLength;
1479 LCID LocaleId;
1480 HANDLE InstanceKey = NULL;
1481 UNICODE_STRING ValueName;
1482 UNICODE_STRING ParentIdPrefix = { 0 };
1483 DEVICE_CAPABILITIES DeviceCapabilities;
1484
1485 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
1486 DPRINT("PDO 0x%p\n", DeviceNode->PhysicalDeviceObject);
1487
1488 ParentDeviceNode = (PDEVICE_NODE)Context;
1489
1490 /*
1491 * We are called for the parent too, but we don't need to do special
1492 * handling for this node
1493 */
1494
1495 if (DeviceNode == ParentDeviceNode)
1496 {
1497 DPRINT("Success\n");
1498 return STATUS_SUCCESS;
1499 }
1500
1501 /*
1502 * Make sure this device node is a direct child of the parent device node
1503 * that is given as an argument
1504 */
1505
1506 if (DeviceNode->Parent != ParentDeviceNode)
1507 {
1508 /* Stop the traversal immediately and indicate successful operation */
1509 DPRINT("Stop\n");
1510 return STATUS_UNSUCCESSFUL;
1511 }
1512
1513 /* Get Locale ID */
1514 Status = ZwQueryDefaultLocale(FALSE, &LocaleId);
1515 if (!NT_SUCCESS(Status))
1516 {
1517 DPRINT("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status);
1518 return Status;
1519 }
1520
1521 /*
1522 * FIXME: For critical errors, cleanup and disable device, but always
1523 * return STATUS_SUCCESS.
1524 */
1525
1526 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1527
1528 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
1529 Status = IopInitiatePnpIrp(
1530 DeviceNode->PhysicalDeviceObject,
1531 &IoStatusBlock,
1532 IRP_MN_QUERY_ID,
1533 &Stack);
1534 if (NT_SUCCESS(Status))
1535 {
1536 /* Copy the device id string */
1537 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
1538
1539 /*
1540 * FIXME: Check for valid characters, if there is invalid characters
1541 * then bugcheck.
1542 */
1543 }
1544 else
1545 {
1546 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1547 }
1548
1549 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack\n");
1550
1551 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
1552 if (!NT_SUCCESS(Status))
1553 {
1554 DPRINT("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status);
1555 }
1556
1557 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCapabilities + 4);
1558
1559 if (!DeviceCapabilities.UniqueID)
1560 {
1561 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1562 DPRINT("Instance ID is not unique\n");
1563 Status = IopGetParentIdPrefix(DeviceNode, &ParentIdPrefix);
1564 if (!NT_SUCCESS(Status))
1565 {
1566 DPRINT("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status);
1567 }
1568 }
1569
1570 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1571
1572 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
1573 Status = IopInitiatePnpIrp(
1574 DeviceNode->PhysicalDeviceObject,
1575 &IoStatusBlock,
1576 IRP_MN_QUERY_ID,
1577 &Stack);
1578 if (NT_SUCCESS(Status))
1579 {
1580 /* Append the instance id string */
1581 wcscat(InstancePath, L"\\");
1582 if (ParentIdPrefix.Length > 0)
1583 {
1584 /* Add information from parent bus device to InstancePath */
1585 wcscat(InstancePath, ParentIdPrefix.Buffer);
1586 if (*(PWSTR)IoStatusBlock.Information)
1587 wcscat(InstancePath, L"&");
1588 }
1589 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1590
1591 /*
1592 * FIXME: Check for valid characters, if there is invalid characters
1593 * then bugcheck
1594 */
1595 }
1596 else
1597 {
1598 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1599 }
1600 RtlFreeUnicodeString(&ParentIdPrefix);
1601
1602 if (!RtlCreateUnicodeString(&DeviceNode->InstancePath, InstancePath))
1603 {
1604 DPRINT("No resources\n");
1605 /* FIXME: Cleanup and disable device */
1606 }
1607
1608 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1609
1610 /*
1611 * Create registry key for the instance id, if it doesn't exist yet
1612 */
1613 KeyBuffer = ExAllocatePool(
1614 PagedPool,
1615 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
1616 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1617 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
1618 Status = IopCreateDeviceKeyPath(KeyBuffer,
1619 &InstanceKey);
1620 ExFreePool(KeyBuffer);
1621 if (!NT_SUCCESS(Status))
1622 {
1623 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1624 }
1625
1626
1627 {
1628 /* Set 'Capabilities' value */
1629 RtlInitUnicodeString(&ValueName,
1630 L"Capabilities");
1631 Status = ZwSetValueKey(InstanceKey,
1632 &ValueName,
1633 0,
1634 REG_DWORD,
1635 (PVOID)&DeviceNode->CapabilityFlags,
1636 sizeof(ULONG));
1637
1638 /* Set 'UINumber' value */
1639 if (DeviceCapabilities.UINumber != (ULONG)-1)
1640 {
1641 RtlInitUnicodeString(&ValueName,
1642 L"UINumber");
1643 Status = ZwSetValueKey(InstanceKey,
1644 &ValueName,
1645 0,
1646 REG_DWORD,
1647 &DeviceCapabilities.UINumber,
1648 sizeof(ULONG));
1649 }
1650 }
1651
1652 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1653
1654 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1655 Status = IopInitiatePnpIrp(
1656 DeviceNode->PhysicalDeviceObject,
1657 &IoStatusBlock,
1658 IRP_MN_QUERY_ID,
1659 &Stack);
1660 if (NT_SUCCESS(Status))
1661 {
1662 /*
1663 * FIXME: Check for valid characters, if there is invalid characters
1664 * then bugcheck.
1665 */
1666 TotalLength = 0;
1667 Ptr = (PWSTR)IoStatusBlock.Information;
1668 DPRINT("Hardware IDs:\n");
1669 while (*Ptr)
1670 {
1671 DPRINT(" %S\n", Ptr);
1672 Length = wcslen(Ptr) + 1;
1673
1674 Ptr += Length;
1675 TotalLength += Length;
1676 }
1677 DPRINT("TotalLength: %hu\n", TotalLength);
1678 DPRINT("\n");
1679
1680 RtlInitUnicodeString(&ValueName,
1681 L"HardwareID");
1682 Status = ZwSetValueKey(InstanceKey,
1683 &ValueName,
1684 0,
1685 REG_MULTI_SZ,
1686 (PVOID)IoStatusBlock.Information,
1687 (TotalLength + 1) * sizeof(WCHAR));
1688 if (!NT_SUCCESS(Status))
1689 {
1690 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1691 }
1692 }
1693 else
1694 {
1695 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1696 }
1697
1698 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1699
1700 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1701 Status = IopInitiatePnpIrp(
1702 DeviceNode->PhysicalDeviceObject,
1703 &IoStatusBlock,
1704 IRP_MN_QUERY_ID,
1705 &Stack);
1706 if (NT_SUCCESS(Status))
1707 {
1708 /*
1709 * FIXME: Check for valid characters, if there is invalid characters
1710 * then bugcheck.
1711 */
1712 TotalLength = 0;
1713 Ptr = (PWSTR)IoStatusBlock.Information;
1714 DPRINT("Compatible IDs:\n");
1715 while (*Ptr)
1716 {
1717 DPRINT(" %S\n", Ptr);
1718 Length = wcslen(Ptr) + 1;
1719
1720 Ptr += Length;
1721 TotalLength += Length;
1722 }
1723 DPRINT("TotalLength: %hu\n", TotalLength);
1724 DPRINT("\n");
1725
1726 RtlInitUnicodeString(&ValueName,
1727 L"CompatibleIDs");
1728 Status = ZwSetValueKey(InstanceKey,
1729 &ValueName,
1730 0,
1731 REG_MULTI_SZ,
1732 (PVOID)IoStatusBlock.Information,
1733 (TotalLength + 1) * sizeof(WCHAR));
1734 if (!NT_SUCCESS(Status))
1735 {
1736 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1737 }
1738 }
1739 else
1740 {
1741 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1742 }
1743
1744
1745 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1746
1747 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1748 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1749 Status = IopInitiatePnpIrp(
1750 DeviceNode->PhysicalDeviceObject,
1751 &IoStatusBlock,
1752 IRP_MN_QUERY_DEVICE_TEXT,
1753 &Stack);
1754 if (NT_SUCCESS(Status))
1755 {
1756 RtlInitUnicodeString(&ValueName, L"DeviceDesc");
1757 if (ZwQueryValueKey(InstanceKey, &ValueName, KeyValueBasicInformation, NULL, 0, &RequiredLength) == STATUS_OBJECT_NAME_NOT_FOUND)
1758 {
1759 /* This key is overriden when a driver is installed. Don't write the
1760 * new description if another one already exists */
1761 Status = ZwSetValueKey(InstanceKey,
1762 &ValueName,
1763 0,
1764 REG_SZ,
1765 (PVOID)IoStatusBlock.Information,
1766 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1767 }
1768 if (!NT_SUCCESS(Status))
1769 {
1770 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status);
1771 }
1772 }
1773 else
1774 {
1775 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1776 }
1777
1778 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1779
1780 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1781 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1782 Status = IopInitiatePnpIrp(
1783 DeviceNode->PhysicalDeviceObject,
1784 &IoStatusBlock,
1785 IRP_MN_QUERY_DEVICE_TEXT,
1786 &Stack);
1787 if (NT_SUCCESS(Status))
1788 {
1789 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1790 RtlInitUnicodeString(&ValueName,
1791 L"LocationInformation");
1792 Status = ZwSetValueKey(InstanceKey,
1793 &ValueName,
1794 0,
1795 REG_SZ,
1796 (PVOID)IoStatusBlock.Information,
1797 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1798 if (!NT_SUCCESS(Status))
1799 {
1800 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1801 }
1802 }
1803 else
1804 {
1805 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1806 }
1807
1808 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1809
1810 Status = IopInitiatePnpIrp(
1811 DeviceNode->PhysicalDeviceObject,
1812 &IoStatusBlock,
1813 IRP_MN_QUERY_BUS_INFORMATION,
1814 NULL);
1815 if (NT_SUCCESS(Status))
1816 {
1817 PPNP_BUS_INFORMATION BusInformation =
1818 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1819
1820 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1821 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1822 DeviceNode->ChildBusTypeIndex = IopGetBusTypeGuidIndex(&BusInformation->BusTypeGuid);
1823 ExFreePool(BusInformation);
1824 }
1825 else
1826 {
1827 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1828
1829 DeviceNode->ChildBusNumber = 0xFFFFFFF0;
1830 DeviceNode->ChildInterfaceType = InterfaceTypeUndefined;
1831 DeviceNode->ChildBusTypeIndex = -1;
1832 }
1833
1834 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1835
1836 Status = IopInitiatePnpIrp(
1837 DeviceNode->PhysicalDeviceObject,
1838 &IoStatusBlock,
1839 IRP_MN_QUERY_RESOURCES,
1840 NULL);
1841 if (NT_SUCCESS(Status))
1842 {
1843 DeviceNode->BootResources =
1844 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1845 DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
1846 }
1847 else
1848 {
1849 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1850 DeviceNode->BootResources = NULL;
1851 }
1852
1853 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1854
1855 Status = IopInitiatePnpIrp(
1856 DeviceNode->PhysicalDeviceObject,
1857 &IoStatusBlock,
1858 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1859 NULL);
1860 if (NT_SUCCESS(Status))
1861 {
1862 DeviceNode->ResourceRequirements =
1863 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1864 }
1865 else
1866 {
1867 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1868 DeviceNode->ResourceRequirements = NULL;
1869 }
1870
1871
1872 if (InstanceKey != NULL)
1873 {
1874 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1875 }
1876
1877 ZwClose(InstanceKey);
1878
1879 Status = IopAssignDeviceResources(DeviceNode);
1880 if (!NT_SUCCESS(Status))
1881 {
1882 DPRINT("IopAssignDeviceResources() failed (Status %x)\n", Status);
1883 }
1884
1885 DeviceNode->Flags |= DNF_PROCESSED;
1886
1887 /* Report the device to the user-mode pnp manager */
1888 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1889 &DeviceNode->InstancePath);
1890
1891 return STATUS_SUCCESS;
1892 }
1893
1894 /*
1895 * IopActionConfigureChildServices
1896 *
1897 * Retrieve configuration for all (direct) child nodes of a parent node.
1898 *
1899 * Parameters
1900 * DeviceNode
1901 * Pointer to device node.
1902 * Context
1903 * Pointer to parent node to retrieve child node configuration for.
1904 *
1905 * Remarks
1906 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1907 * when we reach a device node which is not a direct child of the device
1908 * node for which we configure child services for. Any errors that occur is
1909 * logged instead so that all child services have a chance of beeing
1910 * configured.
1911 */
1912
1913 NTSTATUS
1914 IopActionConfigureChildServices(
1915 PDEVICE_NODE DeviceNode,
1916 PVOID Context)
1917 {
1918 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1919 PDEVICE_NODE ParentDeviceNode;
1920 PUNICODE_STRING Service;
1921 NTSTATUS Status;
1922
1923 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
1924
1925 ParentDeviceNode = (PDEVICE_NODE)Context;
1926
1927 /*
1928 * We are called for the parent too, but we don't need to do special
1929 * handling for this node
1930 */
1931 if (DeviceNode == ParentDeviceNode)
1932 {
1933 DPRINT("Success\n");
1934 return STATUS_SUCCESS;
1935 }
1936
1937 /*
1938 * Make sure this device node is a direct child of the parent device node
1939 * that is given as an argument
1940 */
1941 if (DeviceNode->Parent != ParentDeviceNode)
1942 {
1943 /* Stop the traversal immediately and indicate successful operation */
1944 DPRINT("Stop\n");
1945 return STATUS_UNSUCCESSFUL;
1946 }
1947
1948 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
1949 {
1950 WCHAR RegKeyBuffer[MAX_PATH];
1951 UNICODE_STRING RegKey;
1952
1953 RegKey.Length = 0;
1954 RegKey.MaximumLength = sizeof(RegKeyBuffer);
1955 RegKey.Buffer = RegKeyBuffer;
1956
1957 /*
1958 * Retrieve configuration from Enum key
1959 */
1960
1961 Service = &DeviceNode->ServiceName;
1962
1963 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1964 RtlInitUnicodeString(Service, NULL);
1965
1966 QueryTable[0].Name = L"Service";
1967 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1968 QueryTable[0].EntryContext = Service;
1969
1970 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1971 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
1972
1973 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1974 RegKey.Buffer, QueryTable, NULL, NULL);
1975
1976 if (!NT_SUCCESS(Status))
1977 {
1978 DPRINT("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1979 /* FIXME: Log the error */
1980 CPRINT("Could not retrieve configuration for device %S (Status %x)\n",
1981 DeviceNode->InstancePath.Buffer, Status);
1982 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1983 return STATUS_SUCCESS;
1984 }
1985
1986 if (Service->Buffer == NULL)
1987 {
1988 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1989 return STATUS_SUCCESS;
1990 }
1991
1992 DPRINT("Got Service %S\n", Service->Buffer);
1993 }
1994
1995 return STATUS_SUCCESS;
1996 }
1997
1998 /*
1999 * IopActionInitChildServices
2000 *
2001 * Initialize the service for all (direct) child nodes of a parent node
2002 *
2003 * Parameters
2004 * DeviceNode
2005 * Pointer to device node.
2006 * Context
2007 * Pointer to parent node to initialize child node services for.
2008 * BootDrivers
2009 * Load only driver marked as boot start.
2010 *
2011 * Remarks
2012 * If the driver image for a service is not loaded and initialized
2013 * it is done here too. We only return a status code indicating an
2014 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
2015 * not a direct child of the device node for which we initialize
2016 * child services for. Any errors that occur is logged instead so
2017 * that all child services have a chance of being initialized.
2018 */
2019
2020 NTSTATUS
2021 IopActionInitChildServices(
2022 PDEVICE_NODE DeviceNode,
2023 PVOID Context,
2024 BOOLEAN BootDrivers)
2025 {
2026 PDEVICE_NODE ParentDeviceNode;
2027 NTSTATUS Status;
2028
2029 DPRINT("IopActionInitChildServices(%p, %p, %d)\n", DeviceNode, Context,
2030 BootDrivers);
2031
2032 ParentDeviceNode = (PDEVICE_NODE)Context;
2033
2034 /*
2035 * We are called for the parent too, but we don't need to do special
2036 * handling for this node
2037 */
2038 if (DeviceNode == ParentDeviceNode)
2039 {
2040 DPRINT("Success\n");
2041 return STATUS_SUCCESS;
2042 }
2043
2044 /*
2045 * Make sure this device node is a direct child of the parent device node
2046 * that is given as an argument
2047 */
2048 #if 0
2049 if (DeviceNode->Parent != ParentDeviceNode)
2050 {
2051 /*
2052 * Stop the traversal immediately and indicate unsuccessful operation
2053 */
2054 DPRINT("Stop\n");
2055 return STATUS_UNSUCCESSFUL;
2056 }
2057 #endif
2058
2059 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED) &&
2060 !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
2061 !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
2062 {
2063 PLDR_DATA_TABLE_ENTRY ModuleObject;
2064 PDRIVER_OBJECT DriverObject;
2065
2066 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
2067 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
2068 {
2069 if (Status != STATUS_IMAGE_ALREADY_LOADED)
2070 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
2071 &DeviceNode->ServiceName, FALSE, &DriverObject);
2072 else
2073 {
2074 /* get existing DriverObject pointer */
2075 Status = IopGetDriverObject(
2076 &DriverObject,
2077 &DeviceNode->ServiceName,
2078 FALSE);
2079 }
2080 if (NT_SUCCESS(Status))
2081 {
2082 /* Attach lower level filter drivers. */
2083 IopAttachFilterDrivers(DeviceNode, TRUE);
2084 /* Initialize the function driver for the device node */
2085 Status = IopInitializeDevice(DeviceNode, DriverObject);
2086 if (NT_SUCCESS(Status))
2087 {
2088 /* Attach upper level filter drivers. */
2089 IopAttachFilterDrivers(DeviceNode, FALSE);
2090 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
2091
2092 Status = IopStartDevice(DeviceNode);
2093 }
2094 }
2095 }
2096 else
2097 {
2098 /*
2099 * Don't disable when trying to load only boot drivers
2100 */
2101 if (!BootDrivers)
2102 {
2103 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2104 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
2105 }
2106 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2107 CPRINT("Initialization of service %S failed (Status %x)\n",
2108 DeviceNode->ServiceName.Buffer, Status);
2109 }
2110 } else
2111 {
2112 DPRINT("Service %S is disabled or already initialized\n",
2113 DeviceNode->ServiceName.Buffer);
2114 }
2115
2116 return STATUS_SUCCESS;
2117 }
2118
2119 /*
2120 * IopActionInitAllServices
2121 *
2122 * Initialize the service for all (direct) child nodes of a parent node. This
2123 * function just calls IopActionInitChildServices with BootDrivers = FALSE.
2124 */
2125
2126 NTSTATUS
2127 IopActionInitAllServices(
2128 PDEVICE_NODE DeviceNode,
2129 PVOID Context)
2130 {
2131 return IopActionInitChildServices(DeviceNode, Context, FALSE);
2132 }
2133
2134 /*
2135 * IopActionInitBootServices
2136 *
2137 * Initialize the boot start services for all (direct) child nodes of a
2138 * parent node. This function just calls IopActionInitChildServices with
2139 * BootDrivers = TRUE.
2140 */
2141 NTSTATUS
2142 IopActionInitBootServices(
2143 PDEVICE_NODE DeviceNode,
2144 PVOID Context)
2145 {
2146 return IopActionInitChildServices(DeviceNode, Context, TRUE);
2147 }
2148
2149 /*
2150 * IopInitializePnpServices
2151 *
2152 * Initialize services for discovered children
2153 *
2154 * Parameters
2155 * DeviceNode
2156 * Top device node to start initializing services.
2157 *
2158 * BootDrivers
2159 * When set to TRUE, only drivers marked as boot start will
2160 * be loaded. Otherwise, all drivers will be loaded.
2161 *
2162 * Return Value
2163 * Status
2164 */
2165 NTSTATUS
2166 IopInitializePnpServices(
2167 IN PDEVICE_NODE DeviceNode,
2168 IN BOOLEAN BootDrivers)
2169 {
2170 DEVICETREE_TRAVERSE_CONTEXT Context;
2171
2172 DPRINT("IopInitializePnpServices(%p, %d)\n", DeviceNode, BootDrivers);
2173
2174 if (BootDrivers)
2175 {
2176 IopInitDeviceTreeTraverseContext(
2177 &Context,
2178 DeviceNode,
2179 IopActionInitBootServices,
2180 DeviceNode);
2181 }
2182 else
2183 {
2184 IopInitDeviceTreeTraverseContext(
2185 &Context,
2186 DeviceNode,
2187 IopActionInitAllServices,
2188 DeviceNode);
2189 }
2190
2191 return IopTraverseDeviceTree(&Context);
2192 }
2193
2194
2195 NTSTATUS
2196 IopInvalidateDeviceRelations(
2197 IN PDEVICE_NODE DeviceNode,
2198 IN DEVICE_RELATION_TYPE Type)
2199 {
2200 DEVICETREE_TRAVERSE_CONTEXT Context;
2201 PDEVICE_RELATIONS DeviceRelations;
2202 IO_STATUS_BLOCK IoStatusBlock;
2203 PDEVICE_NODE ChildDeviceNode;
2204 IO_STACK_LOCATION Stack;
2205 BOOL BootDrivers;
2206 OBJECT_ATTRIBUTES ObjectAttributes;
2207 UNICODE_STRING LinkName;
2208 HANDLE Handle;
2209 NTSTATUS Status;
2210 ULONG i;
2211
2212 DPRINT("DeviceNode 0x%p\n", DeviceNode);
2213
2214 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2215
2216 Stack.Parameters.QueryDeviceRelations.Type = Type/*BusRelations*/;
2217
2218 Status = IopInitiatePnpIrp(
2219 DeviceNode->PhysicalDeviceObject,
2220 &IoStatusBlock,
2221 IRP_MN_QUERY_DEVICE_RELATIONS,
2222 &Stack);
2223 if (!NT_SUCCESS(Status))
2224 {
2225 DPRINT("IopInitiatePnpIrp() failed\n");
2226 return Status;
2227 }
2228
2229 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
2230
2231 if ((!DeviceRelations) || (DeviceRelations->Count <= 0))
2232 {
2233 DPRINT("No PDOs\n");
2234 if (DeviceRelations)
2235 {
2236 ExFreePool(DeviceRelations);
2237 }
2238 return STATUS_SUCCESS;
2239 }
2240
2241 DPRINT("Got %d PDOs\n", DeviceRelations->Count);
2242
2243 /*
2244 * Create device nodes for all discovered devices
2245 */
2246
2247 for (i = 0; i < DeviceRelations->Count; i++)
2248 {
2249 Status = IopCreateDeviceNode(
2250 DeviceNode,
2251 DeviceRelations->Objects[i],
2252 &ChildDeviceNode);
2253 DeviceNode->Flags |= DNF_ENUMERATED;
2254 if (!NT_SUCCESS(Status))
2255 {
2256 DPRINT("No resources\n");
2257 for (i = 0; i < DeviceRelations->Count; i++)
2258 ObDereferenceObject(DeviceRelations->Objects[i]);
2259 ExFreePool(DeviceRelations);
2260 return STATUS_INSUFFICIENT_RESOURCES;
2261 }
2262 }
2263 ExFreePool(DeviceRelations);
2264
2265 /*
2266 * Retrieve information about all discovered children from the bus driver
2267 */
2268
2269 IopInitDeviceTreeTraverseContext(
2270 &Context,
2271 DeviceNode,
2272 IopActionInterrogateDeviceStack,
2273 DeviceNode);
2274
2275 Status = IopTraverseDeviceTree(&Context);
2276 if (!NT_SUCCESS(Status))
2277 {
2278 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2279 return Status;
2280 }
2281
2282 /*
2283 * Retrieve configuration from the registry for discovered children
2284 */
2285
2286 IopInitDeviceTreeTraverseContext(
2287 &Context,
2288 DeviceNode,
2289 IopActionConfigureChildServices,
2290 DeviceNode);
2291
2292 Status = IopTraverseDeviceTree(&Context);
2293 if (!NT_SUCCESS(Status))
2294 {
2295 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2296 return Status;
2297 }
2298
2299 /*
2300 * Get the state of the system boot. If the \\SystemRoot link isn't
2301 * created yet, we will assume that it's possible to load only boot
2302 * drivers.
2303 */
2304
2305 RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
2306
2307 InitializeObjectAttributes(
2308 &ObjectAttributes,
2309 &LinkName,
2310 0,
2311 NULL,
2312 NULL);
2313
2314 Status = ZwOpenFile(
2315 &Handle,
2316 FILE_ALL_ACCESS,
2317 &ObjectAttributes,
2318 &IoStatusBlock,
2319 0,
2320 0);
2321 if(NT_SUCCESS(Status))
2322 {
2323 BootDrivers = FALSE;
2324 ZwClose(Handle);
2325 }
2326 else
2327 BootDrivers = TRUE;
2328
2329 /*
2330 * Initialize services for discovered children. Only boot drivers will
2331 * be loaded from boot driver!
2332 */
2333
2334 Status = IopInitializePnpServices(DeviceNode, BootDrivers);
2335 if (!NT_SUCCESS(Status))
2336 {
2337 DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
2338 return Status;
2339 }
2340
2341 return STATUS_SUCCESS;
2342 }
2343
2344
2345 static NTSTATUS INIT_FUNCTION
2346 IopSetRootDeviceInstanceData(PDEVICE_NODE DeviceNode)
2347 {
2348 #if 0
2349 PWSTR KeyBuffer;
2350 HANDLE InstanceKey = NULL;
2351 NTSTATUS Status;
2352
2353 /* Create registry key for the instance id, if it doesn't exist yet */
2354 KeyBuffer = ExAllocatePool(PagedPool,
2355 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
2356 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2357 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
2358 Status = IopCreateDeviceKeyPath(KeyBuffer,
2359 &InstanceKey);
2360 ExFreePool(KeyBuffer);
2361 if (!NT_SUCCESS(Status))
2362 {
2363 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
2364 return Status;
2365 }
2366
2367 /* FIXME: Set 'ConfigFlags' value */
2368
2369 ZwClose(InstanceKey);
2370
2371 return Status;
2372 #endif
2373 return STATUS_SUCCESS;
2374 }
2375
2376
2377 VOID INIT_FUNCTION
2378 PnpInit(VOID)
2379 {
2380 PDEVICE_OBJECT Pdo;
2381 NTSTATUS Status;
2382
2383 DPRINT("PnpInit()\n");
2384
2385 KeInitializeSpinLock(&IopDeviceTreeLock);
2386
2387 /* Initialize the Bus Type GUID List */
2388 IopBusTypeGuidList = ExAllocatePool(PagedPool, sizeof(IO_BUS_TYPE_GUID_LIST));
2389 RtlZeroMemory(IopBusTypeGuidList, sizeof(IO_BUS_TYPE_GUID_LIST));
2390 ExInitializeFastMutex(&IopBusTypeGuidList->Lock);
2391
2392 /* Initialize PnP-Event notification support */
2393 Status = IopInitPlugPlayEvents();
2394 if (!NT_SUCCESS(Status))
2395 {
2396 CPRINT("IopInitPlugPlayEvents() failed\n");
2397 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2398 }
2399
2400 /*
2401 * Create root device node
2402 */
2403
2404 Status = IopCreateDriverObject(&IopRootDriverObject, NULL, 0, FALSE, NULL, 0);
2405 if (!NT_SUCCESS(Status))
2406 {
2407 CPRINT("IoCreateDriverObject() failed\n");
2408 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2409 }
2410
2411 Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
2412 0, FALSE, &Pdo);
2413 if (!NT_SUCCESS(Status))
2414 {
2415 CPRINT("IoCreateDevice() failed\n");
2416 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2417 }
2418
2419 Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
2420 if (!NT_SUCCESS(Status))
2421 {
2422 CPRINT("Insufficient resources\n");
2423 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2424 }
2425
2426 if (!RtlCreateUnicodeString(&IopRootDeviceNode->InstancePath,
2427 L"HTREE\\ROOT\\0"))
2428 {
2429 CPRINT("Failed to create the instance path!\n");
2430 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, STATUS_UNSUCCESSFUL, 0, 0, 0);
2431 }
2432
2433 /* Report the device to the user-mode pnp manager */
2434 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
2435 &IopRootDeviceNode->InstancePath);
2436
2437 IopRootDeviceNode->PhysicalDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
2438 PnpRootDriverEntry(IopRootDriverObject, NULL);
2439 IopRootDriverObject->DriverExtension->AddDevice(
2440 IopRootDriverObject,
2441 IopRootDeviceNode->PhysicalDeviceObject);
2442 }
2443
2444
2445 VOID INIT_FUNCTION
2446 PnpInit2(VOID)
2447 {
2448 NTSTATUS Status;
2449
2450 /* Set root device instance data */
2451 Status = IopSetRootDeviceInstanceData(IopRootDeviceNode);
2452 if (!NT_SUCCESS(Status))
2453 {
2454 CPRINT("Failed to set instance data\n");
2455 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2456 }
2457 }
2458
2459 /* EOF */