7feb2d61810ff4108aa7a456c29e422209d24987
[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 (!NT_SUCCESS(Status))
326 {
327 ExFreePool(ValueInformation);
328 if (Status == STATUS_BUFFER_OVERFLOW)
329 return STATUS_BUFFER_TOO_SMALL;
330 else
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 i++;
610 }
611
612 /* Check if we have to grow the list */
613 if (IopBusTypeGuidList->GuidCount)
614 {
615 /* Calculate the new size */
616 NewSize = sizeof(IO_BUS_TYPE_GUID_LIST) +
617 (sizeof(GUID) * IopBusTypeGuidList->GuidCount);
618
619 /* Allocate the new copy */
620 NewList = ExAllocatePool(PagedPool, NewSize);
621
622 /* Now copy them, decrease the size too */
623 NewSize -= sizeof(GUID);
624 RtlCopyMemory(NewList, IopBusTypeGuidList, NewSize);
625
626 /* Free the old list */
627 ExFreePool(IopBusTypeGuidList);
628
629 /* Use the new buffer */
630 IopBusTypeGuidList = NewList;
631 }
632
633 /* Copy the new GUID */
634 RtlCopyMemory(&IopBusTypeGuidList->Guids[IopBusTypeGuidList->GuidCount],
635 BusTypeGuid,
636 sizeof(GUID));
637
638 /* The new entry is the index */
639 FoundIndex = IopBusTypeGuidList->GuidCount;
640 IopBusTypeGuidList->GuidCount++;
641
642 Quickie:
643 ExReleaseFastMutex(&IopBusTypeGuidList->Lock);
644 return FoundIndex;
645 }
646
647 /*
648 * DESCRIPTION
649 * Creates a device node
650 *
651 * ARGUMENTS
652 * ParentNode = Pointer to parent device node
653 * PhysicalDeviceObject = Pointer to PDO for device object. Pass NULL
654 * to have the root device node create one
655 * (eg. for legacy drivers)
656 * DeviceNode = Pointer to storage for created device node
657 *
658 * RETURN VALUE
659 * Status
660 */
661 NTSTATUS
662 IopCreateDeviceNode(PDEVICE_NODE ParentNode,
663 PDEVICE_OBJECT PhysicalDeviceObject,
664 PDEVICE_NODE *DeviceNode)
665 {
666 PDEVICE_NODE Node;
667 NTSTATUS Status;
668 KIRQL OldIrql;
669
670 DPRINT("ParentNode 0x%p PhysicalDeviceObject 0x%p\n",
671 ParentNode, PhysicalDeviceObject);
672
673 Node = (PDEVICE_NODE)ExAllocatePool(NonPagedPool, sizeof(DEVICE_NODE));
674 if (!Node)
675 {
676 return STATUS_INSUFFICIENT_RESOURCES;
677 }
678
679 RtlZeroMemory(Node, sizeof(DEVICE_NODE));
680
681 if (!PhysicalDeviceObject)
682 {
683 Status = PnpRootCreateDevice(&PhysicalDeviceObject);
684 if (!NT_SUCCESS(Status))
685 {
686 ExFreePool(Node);
687 return Status;
688 }
689
690 /* This is for drivers passed on the command line to ntoskrnl.exe */
691 IopDeviceNodeSetFlag(Node, DNF_STARTED);
692 IopDeviceNodeSetFlag(Node, DNF_LEGACY_DRIVER);
693 }
694
695 Node->PhysicalDeviceObject = PhysicalDeviceObject;
696
697 ((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode = Node;
698
699 if (ParentNode)
700 {
701 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
702 Node->Parent = ParentNode;
703 Node->NextSibling = ParentNode->Child;
704 if (ParentNode->Child != NULL)
705 {
706 ParentNode->Child->PrevSibling = Node;
707 }
708 ParentNode->Child = Node;
709 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
710 Node->Level = ParentNode->Level + 1;
711 }
712
713 *DeviceNode = Node;
714
715 return STATUS_SUCCESS;
716 }
717
718 NTSTATUS
719 IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
720 {
721 KIRQL OldIrql;
722
723 /* All children must be deleted before a parent is deleted */
724 ASSERT(!DeviceNode->Child);
725
726 KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
727
728 ASSERT(DeviceNode->PhysicalDeviceObject);
729
730 ObDereferenceObject(DeviceNode->PhysicalDeviceObject);
731
732 /* Unlink from parent if it exists */
733
734 if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
735 {
736 DeviceNode->Parent->Child = DeviceNode->NextSibling;
737 }
738
739 /* Unlink from sibling list */
740
741 if (DeviceNode->PrevSibling)
742 {
743 DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
744 }
745
746 if (DeviceNode->NextSibling)
747 {
748 DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
749 }
750
751 KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
752
753 RtlFreeUnicodeString(&DeviceNode->InstancePath);
754
755 RtlFreeUnicodeString(&DeviceNode->ServiceName);
756
757 if (DeviceNode->ResourceList)
758 {
759 ExFreePool(DeviceNode->ResourceList);
760 }
761
762 if (DeviceNode->ResourceListTranslated)
763 {
764 ExFreePool(DeviceNode->ResourceListTranslated);
765 }
766
767 if (DeviceNode->ResourceRequirements)
768 {
769 ExFreePool(DeviceNode->ResourceRequirements);
770 }
771
772 if (DeviceNode->BootResources)
773 {
774 ExFreePool(DeviceNode->BootResources);
775 }
776
777 ExFreePool(DeviceNode);
778
779 return STATUS_SUCCESS;
780 }
781
782 NTSTATUS
783 IopInitiatePnpIrp(
784 PDEVICE_OBJECT DeviceObject,
785 PIO_STATUS_BLOCK IoStatusBlock,
786 ULONG MinorFunction,
787 PIO_STACK_LOCATION Stack OPTIONAL)
788 {
789 PDEVICE_OBJECT TopDeviceObject;
790 PIO_STACK_LOCATION IrpSp;
791 NTSTATUS Status;
792 KEVENT Event;
793 PIRP Irp;
794
795 /* Always call the top of the device stack */
796 TopDeviceObject = IoGetAttachedDeviceReference(DeviceObject);
797
798 KeInitializeEvent(
799 &Event,
800 NotificationEvent,
801 FALSE);
802
803 Irp = IoBuildSynchronousFsdRequest(
804 IRP_MJ_PNP,
805 TopDeviceObject,
806 NULL,
807 0,
808 NULL,
809 &Event,
810 IoStatusBlock);
811
812 /* PNP IRPs are always initialized with a status code of
813 STATUS_NOT_IMPLEMENTED */
814 Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
815 Irp->IoStatus.Information = 0;
816
817 IrpSp = IoGetNextIrpStackLocation(Irp);
818 IrpSp->MinorFunction = MinorFunction;
819
820 if (Stack)
821 {
822 RtlMoveMemory(
823 &IrpSp->Parameters,
824 &Stack->Parameters,
825 sizeof(Stack->Parameters));
826 }
827
828 Status = IoCallDriver(TopDeviceObject, Irp);
829 if (Status == STATUS_PENDING)
830 {
831 KeWaitForSingleObject(
832 &Event,
833 Executive,
834 KernelMode,
835 FALSE,
836 NULL);
837 Status = IoStatusBlock->Status;
838 }
839
840 ObDereferenceObject(TopDeviceObject);
841
842 return Status;
843 }
844
845
846 NTSTATUS
847 IopTraverseDeviceTreeNode(
848 PDEVICETREE_TRAVERSE_CONTEXT Context)
849 {
850 PDEVICE_NODE ParentDeviceNode;
851 PDEVICE_NODE ChildDeviceNode;
852 NTSTATUS Status;
853
854 /* Copy context data so we don't overwrite it in subsequent calls to this function */
855 ParentDeviceNode = Context->DeviceNode;
856
857 /* Call the action routine */
858 Status = (Context->Action)(ParentDeviceNode, Context->Context);
859 if (!NT_SUCCESS(Status))
860 {
861 return Status;
862 }
863
864 /* Traversal of all children nodes */
865 for (ChildDeviceNode = ParentDeviceNode->Child;
866 ChildDeviceNode != NULL;
867 ChildDeviceNode = ChildDeviceNode->NextSibling)
868 {
869 /* Pass the current device node to the action routine */
870 Context->DeviceNode = ChildDeviceNode;
871
872 Status = IopTraverseDeviceTreeNode(Context);
873 if (!NT_SUCCESS(Status))
874 {
875 return Status;
876 }
877 }
878
879 return Status;
880 }
881
882
883 NTSTATUS
884 IopTraverseDeviceTree(
885 PDEVICETREE_TRAVERSE_CONTEXT Context)
886 {
887 NTSTATUS Status;
888
889 DPRINT("Context 0x%p\n", Context);
890
891 DPRINT("IopTraverseDeviceTree(DeviceNode 0x%p FirstDeviceNode 0x%p Action %x Context 0x%p)\n",
892 Context->DeviceNode, Context->FirstDeviceNode, Context->Action, Context->Context);
893
894 /* Start from the specified device node */
895 Context->DeviceNode = Context->FirstDeviceNode;
896
897 /* Recursively traverse the device tree */
898 Status = IopTraverseDeviceTreeNode(Context);
899 if (Status == STATUS_UNSUCCESSFUL)
900 {
901 /* The action routine just wanted to terminate the traversal with status
902 code STATUS_SUCCESS */
903 Status = STATUS_SUCCESS;
904 }
905
906 return Status;
907 }
908
909
910 static NTSTATUS
911 IopCreateDeviceKeyPath(PWSTR Path,
912 PHANDLE Handle)
913 {
914 OBJECT_ATTRIBUTES ObjectAttributes;
915 WCHAR KeyBuffer[MAX_PATH];
916 UNICODE_STRING KeyName;
917 HANDLE KeyHandle;
918 NTSTATUS Status;
919 PWCHAR Current;
920 PWCHAR Next;
921
922 *Handle = NULL;
923
924 if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
925 {
926 return STATUS_INVALID_PARAMETER;
927 }
928
929 wcsncpy (KeyBuffer, Path, MAX_PATH-1);
930
931 /* Skip \\Registry\\ */
932 Current = KeyBuffer;
933 Current = wcschr (Current, L'\\') + 1;
934 Current = wcschr (Current, L'\\') + 1;
935
936 while (TRUE)
937 {
938 Next = wcschr (Current, L'\\');
939 if (Next == NULL)
940 {
941 /* The end */
942 }
943 else
944 {
945 *Next = 0;
946 }
947
948 RtlInitUnicodeString (&KeyName, KeyBuffer);
949 InitializeObjectAttributes (&ObjectAttributes,
950 &KeyName,
951 OBJ_CASE_INSENSITIVE,
952 NULL,
953 NULL);
954
955 DPRINT("Create '%S'\n", KeyName.Buffer);
956
957 Status = ZwCreateKey (&KeyHandle,
958 KEY_ALL_ACCESS,
959 &ObjectAttributes,
960 0,
961 NULL,
962 0,
963 NULL);
964 if (!NT_SUCCESS (Status))
965 {
966 DPRINT ("ZwCreateKey() failed with status %x\n", Status);
967 return Status;
968 }
969
970 if (Next == NULL)
971 {
972 *Handle = KeyHandle;
973 return STATUS_SUCCESS;
974 }
975 else
976 {
977 ZwClose (KeyHandle);
978 *Next = L'\\';
979 }
980
981 Current = Next + 1;
982 }
983
984 return STATUS_UNSUCCESSFUL;
985 }
986
987
988 static NTSTATUS
989 IopSetDeviceInstanceData(HANDLE InstanceKey,
990 PDEVICE_NODE DeviceNode)
991 {
992 OBJECT_ATTRIBUTES ObjectAttributes;
993 UNICODE_STRING KeyName;
994 HANDLE LogConfKey;
995 ULONG ResCount;
996 ULONG ListSize;
997 NTSTATUS Status;
998
999 DPRINT("IopSetDeviceInstanceData() called\n");
1000
1001 /* Create the 'LogConf' key */
1002 RtlInitUnicodeString(&KeyName,
1003 L"LogConf");
1004 InitializeObjectAttributes(&ObjectAttributes,
1005 &KeyName,
1006 OBJ_CASE_INSENSITIVE,
1007 InstanceKey,
1008 NULL);
1009 Status = ZwCreateKey(&LogConfKey,
1010 KEY_ALL_ACCESS,
1011 &ObjectAttributes,
1012 0,
1013 NULL,
1014 0,
1015 NULL);
1016 if (NT_SUCCESS(Status))
1017 {
1018 /* Set 'BootConfig' value */
1019 if (DeviceNode->BootResources != NULL)
1020 {
1021 ResCount = DeviceNode->BootResources->Count;
1022 if (ResCount != 0)
1023 {
1024 ListSize = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
1025
1026 RtlInitUnicodeString(&KeyName,
1027 L"BootConfig");
1028 Status = ZwSetValueKey(LogConfKey,
1029 &KeyName,
1030 0,
1031 REG_RESOURCE_LIST,
1032 &DeviceNode->BootResources,
1033 ListSize);
1034 }
1035 }
1036
1037 /* Set 'BasicConfigVector' value */
1038 if (DeviceNode->ResourceRequirements != NULL &&
1039 DeviceNode->ResourceRequirements->ListSize != 0)
1040 {
1041 RtlInitUnicodeString(&KeyName,
1042 L"BasicConfigVector");
1043 Status = ZwSetValueKey(LogConfKey,
1044 &KeyName,
1045 0,
1046 REG_RESOURCE_REQUIREMENTS_LIST,
1047 DeviceNode->ResourceRequirements,
1048 DeviceNode->ResourceRequirements->ListSize);
1049 }
1050
1051 ZwClose(LogConfKey);
1052 }
1053
1054 #if 0
1055 if (DeviceNode->PhysicalDeviceObject != NULL)
1056 {
1057 /* Create the 'Control' key */
1058 RtlInitUnicodeString(&KeyName,
1059 L"Control");
1060 InitializeObjectAttributes(&ObjectAttributes,
1061 &KeyName,
1062 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
1063 InstanceKey,
1064 NULL);
1065 Status = ZwCreateKey(&LogConfKey,
1066 KEY_ALL_ACCESS,
1067 &ObjectAttributes,
1068 0,
1069 NULL,
1070 REG_OPTION_VOLATILE,
1071 NULL);
1072 if (NT_SUCCESS(Status))
1073 {
1074 ULONG Reference = (ULONG)DeviceNode->PhysicalDeviceObject;
1075 RtlInitUnicodeString(&KeyName,
1076 L"DeviceReference");
1077 Status = ZwSetValueKey(LogConfKey,
1078 &KeyName,
1079 0,
1080 REG_DWORD,
1081 &Reference,
1082 sizeof(PVOID));
1083
1084 ZwClose(LogConfKey);
1085 }
1086 }
1087 #endif
1088
1089 DPRINT("IopSetDeviceInstanceData() done\n");
1090
1091 return STATUS_SUCCESS;
1092 }
1093
1094
1095 NTSTATUS
1096 IopAssignDeviceResources(
1097 PDEVICE_NODE DeviceNode)
1098 {
1099 PIO_RESOURCE_LIST ResourceList;
1100 PIO_RESOURCE_DESCRIPTOR ResourceDescriptor;
1101 PCM_PARTIAL_RESOURCE_DESCRIPTOR DescriptorRaw, DescriptorTranslated;
1102 ULONG NumberOfResources = 0;
1103 ULONG i;
1104 NTSTATUS Status;
1105
1106 /* Fill DeviceNode->ResourceList and DeviceNode->ResourceListTranslated;
1107 * by using DeviceNode->ResourceRequirements */
1108
1109 if (!DeviceNode->ResourceRequirements
1110 || DeviceNode->ResourceRequirements->AlternativeLists == 0)
1111 {
1112 DeviceNode->ResourceList = DeviceNode->ResourceListTranslated = NULL;
1113 return STATUS_SUCCESS;
1114 }
1115
1116 /* FIXME: that's here that PnP arbiter should go */
1117 /* Actually, simply use resource list #0 as assigned resource list */
1118 ResourceList = &DeviceNode->ResourceRequirements->List[0];
1119 if (ResourceList->Version != 1 || ResourceList->Revision != 1)
1120 {
1121 Status = STATUS_REVISION_MISMATCH;
1122 goto ByeBye;
1123 }
1124
1125 DeviceNode->ResourceList = ExAllocatePool(PagedPool,
1126 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1127 if (!DeviceNode->ResourceList)
1128 {
1129 Status = STATUS_INSUFFICIENT_RESOURCES;
1130 goto ByeBye;
1131 }
1132
1133 DeviceNode->ResourceListTranslated = ExAllocatePool(PagedPool,
1134 sizeof(CM_RESOURCE_LIST) + ResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR));
1135 if (!DeviceNode->ResourceListTranslated)
1136 {
1137 Status = STATUS_INSUFFICIENT_RESOURCES;
1138 goto ByeBye;
1139 }
1140
1141 DeviceNode->ResourceList->Count = 1;
1142 DeviceNode->ResourceList->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1143 DeviceNode->ResourceList->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1144 DeviceNode->ResourceList->List[0].PartialResourceList.Version = 1;
1145 DeviceNode->ResourceList->List[0].PartialResourceList.Revision = 1;
1146
1147 DeviceNode->ResourceListTranslated->Count = 1;
1148 DeviceNode->ResourceListTranslated->List[0].InterfaceType = DeviceNode->ResourceRequirements->InterfaceType;
1149 DeviceNode->ResourceListTranslated->List[0].BusNumber = DeviceNode->ResourceRequirements->BusNumber;
1150 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Version = 1;
1151 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Revision = 1;
1152
1153 for (i = 0; i < ResourceList->Count; i++)
1154 {
1155 ResourceDescriptor = &ResourceList->Descriptors[i];
1156
1157 if (ResourceDescriptor->Option == 0 || ResourceDescriptor->Option == IO_RESOURCE_PREFERRED)
1158 {
1159 DescriptorRaw = &DeviceNode->ResourceList->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1160 DescriptorTranslated = &DeviceNode->ResourceListTranslated->List[0].PartialResourceList.PartialDescriptors[NumberOfResources];
1161 NumberOfResources++;
1162
1163 /* Copy ResourceDescriptor to DescriptorRaw and DescriptorTranslated */
1164 DescriptorRaw->Type = DescriptorTranslated->Type = ResourceDescriptor->Type;
1165 DescriptorRaw->ShareDisposition = DescriptorTranslated->ShareDisposition = ResourceDescriptor->ShareDisposition;
1166 DescriptorRaw->Flags = DescriptorTranslated->Flags = ResourceDescriptor->Flags;
1167 switch (ResourceDescriptor->Type)
1168 {
1169 case CmResourceTypePort:
1170 {
1171 ULONG AddressSpace = 0; /* IO space */
1172 DescriptorRaw->u.Port.Start = ResourceDescriptor->u.Port.MinimumAddress;
1173 DescriptorRaw->u.Port.Length = DescriptorTranslated->u.Port.Length
1174 = ResourceDescriptor->u.Port.Length;
1175 if (!HalTranslateBusAddress(
1176 DeviceNode->ResourceRequirements->InterfaceType,
1177 DeviceNode->ResourceRequirements->BusNumber,
1178 DescriptorRaw->u.Port.Start,
1179 &AddressSpace,
1180 &DescriptorTranslated->u.Port.Start))
1181 {
1182 Status = STATUS_UNSUCCESSFUL;
1183 goto ByeBye;
1184 }
1185 break;
1186 }
1187 case CmResourceTypeInterrupt:
1188 {
1189 INTERFACE_TYPE BusType;
1190 ULONG SlotNumber;
1191 ULONG ret;
1192 UCHAR Irq;
1193
1194 DescriptorRaw->u.Interrupt.Level = 0;
1195 DescriptorRaw->u.Interrupt.Vector = ResourceDescriptor->u.Interrupt.MinimumVector;
1196 /* FIXME: HACK: if we have a PCI device, we try
1197 * to keep the IRQ assigned by the BIOS */
1198 if (NT_SUCCESS(IoGetDeviceProperty(
1199 DeviceNode->PhysicalDeviceObject,
1200 DevicePropertyLegacyBusType,
1201 sizeof(INTERFACE_TYPE),
1202 &BusType,
1203 &ret)) && BusType == PCIBus)
1204 {
1205 /* We have a PCI bus */
1206 if (NT_SUCCESS(IoGetDeviceProperty(
1207 DeviceNode->PhysicalDeviceObject,
1208 DevicePropertyAddress,
1209 sizeof(ULONG),
1210 &SlotNumber,
1211 &ret)) && SlotNumber > 0)
1212 {
1213 /* We have a good slot number */
1214 ret = HalGetBusDataByOffset(PCIConfiguration,
1215 DeviceNode->ResourceRequirements->BusNumber,
1216 SlotNumber,
1217 &Irq,
1218 0x3c /* PCI_INTERRUPT_LINE */,
1219 sizeof(UCHAR));
1220 if (ret != 0 && ret != 2
1221 && ResourceDescriptor->u.Interrupt.MinimumVector <= Irq
1222 && ResourceDescriptor->u.Interrupt.MaximumVector >= Irq)
1223 {
1224 /* The device already has an assigned IRQ */
1225 DescriptorRaw->u.Interrupt.Vector = Irq;
1226 }
1227 else
1228 {
1229 DPRINT1("Trying to assign IRQ 0x%lx to %wZ\n",
1230 DescriptorRaw->u.Interrupt.Vector,
1231 &DeviceNode->InstancePath);
1232 Irq = (UCHAR)DescriptorRaw->u.Interrupt.Vector;
1233 ret = HalSetBusDataByOffset(PCIConfiguration,
1234 DeviceNode->ResourceRequirements->BusNumber,
1235 SlotNumber,
1236 &Irq,
1237 0x3c /* PCI_INTERRUPT_LINE */,
1238 sizeof(UCHAR));
1239 if (ret == 0 || ret == 2)
1240 KEBUGCHECK(0);
1241 }
1242 }
1243 }
1244
1245 DescriptorTranslated->u.Interrupt.Level = 0;
1246 DescriptorTranslated->u.Interrupt.Vector = HalGetInterruptVector(
1247 DeviceNode->ResourceRequirements->InterfaceType,
1248 DeviceNode->ResourceRequirements->BusNumber,
1249 DescriptorRaw->u.Interrupt.Level,
1250 DescriptorRaw->u.Interrupt.Vector,
1251 (PKIRQL)&DescriptorTranslated->u.Interrupt.Level,
1252 &DescriptorRaw->u.Interrupt.Affinity);
1253 DescriptorTranslated->u.Interrupt.Affinity = DescriptorRaw->u.Interrupt.Affinity;
1254 break;
1255 }
1256 case CmResourceTypeMemory:
1257 {
1258 ULONG AddressSpace = 1; /* Memory space */
1259 DescriptorRaw->u.Memory.Start = ResourceDescriptor->u.Memory.MinimumAddress;
1260 DescriptorRaw->u.Memory.Length = DescriptorTranslated->u.Memory.Length
1261 = ResourceDescriptor->u.Memory.Length;
1262 if (!HalTranslateBusAddress(
1263 DeviceNode->ResourceRequirements->InterfaceType,
1264 DeviceNode->ResourceRequirements->BusNumber,
1265 DescriptorRaw->u.Memory.Start,
1266 &AddressSpace,
1267 &DescriptorTranslated->u.Memory.Start))
1268 {
1269 Status = STATUS_UNSUCCESSFUL;
1270 goto ByeBye;
1271 }
1272 break;
1273 }
1274 case CmResourceTypeDma:
1275 {
1276 DescriptorRaw->u.Dma.Channel = DescriptorTranslated->u.Dma.Channel
1277 = ResourceDescriptor->u.Dma.MinimumChannel;
1278 DescriptorRaw->u.Dma.Port = DescriptorTranslated->u.Dma.Port
1279 = 0; /* FIXME */
1280 DescriptorRaw->u.Dma.Reserved1 = DescriptorTranslated->u.Dma.Reserved1
1281 = 0;
1282 break;
1283 }
1284 case CmResourceTypeBusNumber:
1285 {
1286 DescriptorRaw->u.BusNumber.Start = DescriptorTranslated->u.BusNumber.Start
1287 = ResourceDescriptor->u.BusNumber.MinBusNumber;
1288 DescriptorRaw->u.BusNumber.Length = DescriptorTranslated->u.BusNumber.Length
1289 = ResourceDescriptor->u.BusNumber.Length;
1290 DescriptorRaw->u.BusNumber.Reserved = DescriptorTranslated->u.BusNumber.Reserved
1291 = ResourceDescriptor->u.BusNumber.Reserved;
1292 break;
1293 }
1294 /*CmResourceTypeDevicePrivate:
1295 case CmResourceTypePcCardConfig:
1296 case CmResourceTypeMfCardConfig:
1297 {
1298 RtlCopyMemory(
1299 &DescriptorRaw->u.DevicePrivate,
1300 &ResourceDescriptor->u.DevicePrivate,
1301 sizeof(ResourceDescriptor->u.DevicePrivate));
1302 RtlCopyMemory(
1303 &DescriptorTranslated->u.DevicePrivate,
1304 &ResourceDescriptor->u.DevicePrivate,
1305 sizeof(ResourceDescriptor->u.DevicePrivate));
1306 break;
1307 }*/
1308 default:
1309 DPRINT1("IopAssignDeviceResources(): unknown resource descriptor type 0x%x\n", ResourceDescriptor->Type);
1310 NumberOfResources--;
1311 }
1312 }
1313
1314 }
1315
1316 DeviceNode->ResourceList->List[0].PartialResourceList.Count = NumberOfResources;
1317 DeviceNode->ResourceListTranslated->List[0].PartialResourceList.Count = NumberOfResources;
1318
1319 return STATUS_SUCCESS;
1320
1321 ByeBye:
1322 if (DeviceNode->ResourceList)
1323 {
1324 ExFreePool(DeviceNode->ResourceList);
1325 DeviceNode->ResourceList = NULL;
1326 }
1327 if (DeviceNode->ResourceListTranslated)
1328 {
1329 ExFreePool(DeviceNode->ResourceListTranslated);
1330 DeviceNode->ResourceListTranslated = NULL;
1331 }
1332
1333 return Status;
1334 }
1335
1336
1337 /*
1338 * IopGetParentIdPrefix
1339 *
1340 * Retrieve (or create) a string which identifies a device.
1341 *
1342 * Parameters
1343 * DeviceNode
1344 * Pointer to device node.
1345 * ParentIdPrefix
1346 * Pointer to the string where is returned the parent node identifier
1347 *
1348 * Remarks
1349 * If the return code is STATUS_SUCCESS, the ParentIdPrefix string is
1350 * valid and its Buffer field is NULL-terminated. The caller needs to
1351 * to free the string with RtlFreeUnicodeString when it is no longer
1352 * needed.
1353 */
1354
1355 NTSTATUS
1356 IopGetParentIdPrefix(
1357 PDEVICE_NODE DeviceNode,
1358 PUNICODE_STRING ParentIdPrefix)
1359 {
1360 ULONG KeyNameBufferLength;
1361 PWSTR KeyNameBuffer = NULL;
1362 PKEY_VALUE_PARTIAL_INFORMATION ParentIdPrefixInformation = NULL;
1363 UNICODE_STRING KeyName;
1364 UNICODE_STRING KeyValue;
1365 UNICODE_STRING ValueName;
1366 OBJECT_ATTRIBUTES ObjectAttributes;
1367 HANDLE hKey = INVALID_HANDLE_VALUE;
1368 PBYTE currentByte;
1369 ULONG crc32 = 0;
1370 ULONG i;
1371 NTSTATUS Status;
1372
1373 /* HACK: As long as some devices have a NULL device
1374 * instance path, the following test is required :(
1375 */
1376 if (DeviceNode->Parent->InstancePath.Length == 0)
1377 return STATUS_UNSUCCESSFUL;
1378
1379 /* 1. Try to retrieve ParentIdPrefix from registry */
1380 KeyNameBufferLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION, Data[0]) + MAX_PATH * sizeof(WCHAR);
1381 ParentIdPrefixInformation = ExAllocatePool(PagedPool, KeyNameBufferLength + sizeof(WCHAR));
1382 if (!ParentIdPrefixInformation)
1383 {
1384 Status = STATUS_INSUFFICIENT_RESOURCES;
1385 goto cleanup;
1386 }
1387 KeyNameBuffer = ExAllocatePool(PagedPool, (49 * sizeof(WCHAR)) + DeviceNode->Parent->InstancePath.Length);
1388 if (!KeyNameBuffer)
1389 {
1390 Status = STATUS_INSUFFICIENT_RESOURCES;
1391 goto cleanup;
1392 }
1393 wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1394 wcscat(KeyNameBuffer, DeviceNode->Parent->InstancePath.Buffer);
1395 RtlInitUnicodeString(&KeyName, KeyNameBuffer);
1396 InitializeObjectAttributes(&ObjectAttributes, &KeyName, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
1397 Status = ZwOpenKey(&hKey, KEY_QUERY_VALUE | KEY_SET_VALUE, &ObjectAttributes);
1398 if (!NT_SUCCESS(Status))
1399 goto cleanup;
1400 RtlInitUnicodeString(&ValueName, L"ParentIdPrefix");
1401 Status = ZwQueryValueKey(
1402 hKey, &ValueName,
1403 KeyValuePartialInformation, ParentIdPrefixInformation,
1404 KeyNameBufferLength, &KeyNameBufferLength);
1405 if (NT_SUCCESS(Status))
1406 {
1407 if (ParentIdPrefixInformation->Type != REG_SZ)
1408 Status = STATUS_UNSUCCESSFUL;
1409 else
1410 {
1411 KeyValue.Length = KeyValue.MaximumLength = ParentIdPrefixInformation->DataLength;
1412 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1413 }
1414 goto cleanup;
1415 }
1416 if (Status != STATUS_OBJECT_NAME_NOT_FOUND)
1417 {
1418 KeyValue.Length = KeyValue.MaximumLength = ParentIdPrefixInformation->DataLength;
1419 KeyValue.Buffer = (PWSTR)ParentIdPrefixInformation->Data;
1420 goto cleanup;
1421 }
1422
1423 /* 2. Create the ParentIdPrefix value */
1424 currentByte = (PBYTE)DeviceNode->Parent->InstancePath.Buffer;
1425 for (i = 0; i < DeviceNode->Parent->InstancePath.Length; i++, currentByte++)
1426 crc32 = (crc32 >> 8) ^ crc32Table[*currentByte ^ (crc32 & 0xff)];
1427 crc32 = ~crc32;
1428 swprintf((PWSTR)ParentIdPrefixInformation->Data, L"%lx&%lx", DeviceNode->Parent->Level, crc32);
1429 RtlInitUnicodeString(&KeyValue, (PWSTR)ParentIdPrefixInformation->Data);
1430
1431 /* 3. Try to write the ParentIdPrefix to registry */
1432 Status = ZwSetValueKey(
1433 hKey, &ValueName,
1434 0, REG_SZ,
1435 (PVOID)KeyValue.Buffer,
1436 (wcslen(KeyValue.Buffer) + 1) * sizeof(WCHAR));
1437
1438 cleanup:
1439 if (NT_SUCCESS(Status))
1440 {
1441 /* Duplicate the string to return it */
1442 Status = RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE, &KeyValue, ParentIdPrefix);
1443 }
1444 ExFreePool(ParentIdPrefixInformation);
1445 ExFreePool(KeyNameBuffer);
1446 if (hKey != INVALID_HANDLE_VALUE)
1447 ZwClose(hKey);
1448 return Status;
1449 }
1450
1451
1452 /*
1453 * IopActionInterrogateDeviceStack
1454 *
1455 * Retrieve information for all (direct) child nodes of a parent node.
1456 *
1457 * Parameters
1458 * DeviceNode
1459 * Pointer to device node.
1460 * Context
1461 * Pointer to parent node to retrieve child node information for.
1462 *
1463 * Remarks
1464 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1465 * when we reach a device node which is not a direct child of the device
1466 * node for which we retrieve information of child nodes for. Any errors
1467 * that occur is logged instead so that all child services have a chance
1468 * of being interrogated.
1469 */
1470
1471 NTSTATUS
1472 IopActionInterrogateDeviceStack(
1473 PDEVICE_NODE DeviceNode,
1474 PVOID Context)
1475 {
1476 IO_STATUS_BLOCK IoStatusBlock;
1477 PDEVICE_NODE ParentDeviceNode;
1478 WCHAR InstancePath[MAX_PATH];
1479 IO_STACK_LOCATION Stack;
1480 NTSTATUS Status;
1481 PWSTR KeyBuffer;
1482 PWSTR Ptr;
1483 USHORT Length;
1484 USHORT TotalLength;
1485 ULONG RequiredLength;
1486 LCID LocaleId;
1487 HANDLE InstanceKey = NULL;
1488 UNICODE_STRING ValueName;
1489 UNICODE_STRING ParentIdPrefix = { 0 };
1490 DEVICE_CAPABILITIES DeviceCapabilities;
1491
1492 DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
1493 DPRINT("PDO 0x%p\n", DeviceNode->PhysicalDeviceObject);
1494
1495 ParentDeviceNode = (PDEVICE_NODE)Context;
1496
1497 /*
1498 * We are called for the parent too, but we don't need to do special
1499 * handling for this node
1500 */
1501
1502 if (DeviceNode == ParentDeviceNode)
1503 {
1504 DPRINT("Success\n");
1505 return STATUS_SUCCESS;
1506 }
1507
1508 /*
1509 * Make sure this device node is a direct child of the parent device node
1510 * that is given as an argument
1511 */
1512
1513 if (DeviceNode->Parent != ParentDeviceNode)
1514 {
1515 /* Stop the traversal immediately and indicate successful operation */
1516 DPRINT("Stop\n");
1517 return STATUS_UNSUCCESSFUL;
1518 }
1519
1520 /* Get Locale ID */
1521 Status = ZwQueryDefaultLocale(FALSE, &LocaleId);
1522 if (!NT_SUCCESS(Status))
1523 {
1524 DPRINT("ZwQueryDefaultLocale() failed with status 0x%lx\n", Status);
1525 return Status;
1526 }
1527
1528 /*
1529 * FIXME: For critical errors, cleanup and disable device, but always
1530 * return STATUS_SUCCESS.
1531 */
1532
1533 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryDeviceID to device stack\n");
1534
1535 Stack.Parameters.QueryId.IdType = BusQueryDeviceID;
1536 Status = IopInitiatePnpIrp(
1537 DeviceNode->PhysicalDeviceObject,
1538 &IoStatusBlock,
1539 IRP_MN_QUERY_ID,
1540 &Stack);
1541 if (NT_SUCCESS(Status))
1542 {
1543 /* Copy the device id string */
1544 wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
1545
1546 /*
1547 * FIXME: Check for valid characters, if there is invalid characters
1548 * then bugcheck.
1549 */
1550 }
1551 else
1552 {
1553 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1554 }
1555
1556 DPRINT("Sending IRP_MN_QUERY_CAPABILITIES to device stack\n");
1557
1558 Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCapabilities);
1559 if (!NT_SUCCESS(Status))
1560 {
1561 DPRINT("IopInitiatePnpIrp() failed (Status 0x%08lx)\n", Status);
1562 }
1563
1564 DeviceNode->CapabilityFlags = *(PULONG)((ULONG_PTR)&DeviceCapabilities + 4);
1565
1566 if (!DeviceCapabilities.UniqueID)
1567 {
1568 /* Device has not a unique ID. We need to prepend parent bus unique identifier */
1569 DPRINT("Instance ID is not unique\n");
1570 Status = IopGetParentIdPrefix(DeviceNode, &ParentIdPrefix);
1571 if (!NT_SUCCESS(Status))
1572 {
1573 DPRINT("IopGetParentIdPrefix() failed (Status 0x%08lx)\n", Status);
1574 }
1575 }
1576
1577 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
1578
1579 Stack.Parameters.QueryId.IdType = BusQueryInstanceID;
1580 Status = IopInitiatePnpIrp(
1581 DeviceNode->PhysicalDeviceObject,
1582 &IoStatusBlock,
1583 IRP_MN_QUERY_ID,
1584 &Stack);
1585 if (NT_SUCCESS(Status))
1586 {
1587 /* Append the instance id string */
1588 wcscat(InstancePath, L"\\");
1589 if (ParentIdPrefix.Length > 0)
1590 {
1591 /* Add information from parent bus device to InstancePath */
1592 wcscat(InstancePath, ParentIdPrefix.Buffer);
1593 if (*(PWSTR)IoStatusBlock.Information)
1594 wcscat(InstancePath, L"&");
1595 }
1596 wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
1597
1598 /*
1599 * FIXME: Check for valid characters, if there is invalid characters
1600 * then bugcheck
1601 */
1602 }
1603 else
1604 {
1605 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1606 }
1607 RtlFreeUnicodeString(&ParentIdPrefix);
1608
1609 if (!RtlCreateUnicodeString(&DeviceNode->InstancePath, InstancePath))
1610 {
1611 DPRINT("No resources\n");
1612 /* FIXME: Cleanup and disable device */
1613 }
1614
1615 DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
1616
1617 /*
1618 * Create registry key for the instance id, if it doesn't exist yet
1619 */
1620 KeyBuffer = ExAllocatePool(
1621 PagedPool,
1622 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
1623 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1624 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
1625 Status = IopCreateDeviceKeyPath(KeyBuffer,
1626 &InstanceKey);
1627 ExFreePool(KeyBuffer);
1628 if (!NT_SUCCESS(Status))
1629 {
1630 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
1631 }
1632
1633
1634 {
1635 /* Set 'Capabilities' value */
1636 RtlInitUnicodeString(&ValueName,
1637 L"Capabilities");
1638 Status = ZwSetValueKey(InstanceKey,
1639 &ValueName,
1640 0,
1641 REG_DWORD,
1642 (PVOID)&DeviceNode->CapabilityFlags,
1643 sizeof(ULONG));
1644
1645 /* Set 'UINumber' value */
1646 if (DeviceCapabilities.UINumber != (ULONG)-1)
1647 {
1648 RtlInitUnicodeString(&ValueName,
1649 L"UINumber");
1650 Status = ZwSetValueKey(InstanceKey,
1651 &ValueName,
1652 0,
1653 REG_DWORD,
1654 &DeviceCapabilities.UINumber,
1655 sizeof(ULONG));
1656 }
1657 }
1658
1659 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
1660
1661 Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
1662 Status = IopInitiatePnpIrp(
1663 DeviceNode->PhysicalDeviceObject,
1664 &IoStatusBlock,
1665 IRP_MN_QUERY_ID,
1666 &Stack);
1667 if (NT_SUCCESS(Status))
1668 {
1669 /*
1670 * FIXME: Check for valid characters, if there is invalid characters
1671 * then bugcheck.
1672 */
1673 TotalLength = 0;
1674 Ptr = (PWSTR)IoStatusBlock.Information;
1675 DPRINT("Hardware IDs:\n");
1676 while (*Ptr)
1677 {
1678 DPRINT(" %S\n", Ptr);
1679 Length = wcslen(Ptr) + 1;
1680
1681 Ptr += Length;
1682 TotalLength += Length;
1683 }
1684 DPRINT("TotalLength: %hu\n", TotalLength);
1685 DPRINT("\n");
1686
1687 RtlInitUnicodeString(&ValueName,
1688 L"HardwareID");
1689 Status = ZwSetValueKey(InstanceKey,
1690 &ValueName,
1691 0,
1692 REG_MULTI_SZ,
1693 (PVOID)IoStatusBlock.Information,
1694 (TotalLength + 1) * sizeof(WCHAR));
1695 if (!NT_SUCCESS(Status))
1696 {
1697 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1698 }
1699 }
1700 else
1701 {
1702 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1703 }
1704
1705 DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
1706
1707 Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
1708 Status = IopInitiatePnpIrp(
1709 DeviceNode->PhysicalDeviceObject,
1710 &IoStatusBlock,
1711 IRP_MN_QUERY_ID,
1712 &Stack);
1713 if (NT_SUCCESS(Status))
1714 {
1715 /*
1716 * FIXME: Check for valid characters, if there is invalid characters
1717 * then bugcheck.
1718 */
1719 TotalLength = 0;
1720 Ptr = (PWSTR)IoStatusBlock.Information;
1721 DPRINT("Compatible IDs:\n");
1722 while (*Ptr)
1723 {
1724 DPRINT(" %S\n", Ptr);
1725 Length = wcslen(Ptr) + 1;
1726
1727 Ptr += Length;
1728 TotalLength += Length;
1729 }
1730 DPRINT("TotalLength: %hu\n", TotalLength);
1731 DPRINT("\n");
1732
1733 RtlInitUnicodeString(&ValueName,
1734 L"CompatibleIDs");
1735 Status = ZwSetValueKey(InstanceKey,
1736 &ValueName,
1737 0,
1738 REG_MULTI_SZ,
1739 (PVOID)IoStatusBlock.Information,
1740 (TotalLength + 1) * sizeof(WCHAR));
1741 if (!NT_SUCCESS(Status))
1742 {
1743 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1744 }
1745 }
1746 else
1747 {
1748 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1749 }
1750
1751
1752 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
1753
1754 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
1755 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1756 Status = IopInitiatePnpIrp(
1757 DeviceNode->PhysicalDeviceObject,
1758 &IoStatusBlock,
1759 IRP_MN_QUERY_DEVICE_TEXT,
1760 &Stack);
1761 if (NT_SUCCESS(Status))
1762 {
1763 RtlInitUnicodeString(&ValueName, L"DeviceDesc");
1764 if (ZwQueryValueKey(InstanceKey, &ValueName, KeyValueBasicInformation, NULL, 0, &RequiredLength) == STATUS_OBJECT_NAME_NOT_FOUND)
1765 {
1766 /* This key is overriden when a driver is installed. Don't write the
1767 * new description if another one already exists */
1768 Status = ZwSetValueKey(InstanceKey,
1769 &ValueName,
1770 0,
1771 REG_SZ,
1772 (PVOID)IoStatusBlock.Information,
1773 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1774 }
1775 if (!NT_SUCCESS(Status))
1776 {
1777 DPRINT1("ZwSetValueKey() failed (Status 0x%lx)\n", Status);
1778 }
1779 }
1780 else
1781 {
1782 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1783 }
1784
1785 DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
1786
1787 Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextLocationInformation;
1788 Stack.Parameters.QueryDeviceText.LocaleId = LocaleId;
1789 Status = IopInitiatePnpIrp(
1790 DeviceNode->PhysicalDeviceObject,
1791 &IoStatusBlock,
1792 IRP_MN_QUERY_DEVICE_TEXT,
1793 &Stack);
1794 if (NT_SUCCESS(Status))
1795 {
1796 DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
1797 RtlInitUnicodeString(&ValueName,
1798 L"LocationInformation");
1799 Status = ZwSetValueKey(InstanceKey,
1800 &ValueName,
1801 0,
1802 REG_SZ,
1803 (PVOID)IoStatusBlock.Information,
1804 (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
1805 if (!NT_SUCCESS(Status))
1806 {
1807 DPRINT1("ZwSetValueKey() failed (Status %lx)\n", Status);
1808 }
1809 }
1810 else
1811 {
1812 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1813 }
1814
1815 DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
1816
1817 Status = IopInitiatePnpIrp(
1818 DeviceNode->PhysicalDeviceObject,
1819 &IoStatusBlock,
1820 IRP_MN_QUERY_BUS_INFORMATION,
1821 NULL);
1822 if (NT_SUCCESS(Status))
1823 {
1824 PPNP_BUS_INFORMATION BusInformation =
1825 (PPNP_BUS_INFORMATION)IoStatusBlock.Information;
1826
1827 DeviceNode->ChildBusNumber = BusInformation->BusNumber;
1828 DeviceNode->ChildInterfaceType = BusInformation->LegacyBusType;
1829 DeviceNode->ChildBusTypeIndex = IopGetBusTypeGuidIndex(&BusInformation->BusTypeGuid);
1830 ExFreePool(BusInformation);
1831 }
1832 else
1833 {
1834 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1835
1836 DeviceNode->ChildBusNumber = 0xFFFFFFF0;
1837 DeviceNode->ChildInterfaceType = InterfaceTypeUndefined;
1838 DeviceNode->ChildBusTypeIndex = -1;
1839 }
1840
1841 DPRINT("Sending IRP_MN_QUERY_RESOURCES to device stack\n");
1842
1843 Status = IopInitiatePnpIrp(
1844 DeviceNode->PhysicalDeviceObject,
1845 &IoStatusBlock,
1846 IRP_MN_QUERY_RESOURCES,
1847 NULL);
1848 if (NT_SUCCESS(Status))
1849 {
1850 DeviceNode->BootResources =
1851 (PCM_RESOURCE_LIST)IoStatusBlock.Information;
1852 DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
1853 }
1854 else
1855 {
1856 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1857 DeviceNode->BootResources = NULL;
1858 }
1859
1860 DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
1861
1862 Status = IopInitiatePnpIrp(
1863 DeviceNode->PhysicalDeviceObject,
1864 &IoStatusBlock,
1865 IRP_MN_QUERY_RESOURCE_REQUIREMENTS,
1866 NULL);
1867 if (NT_SUCCESS(Status))
1868 {
1869 DeviceNode->ResourceRequirements =
1870 (PIO_RESOURCE_REQUIREMENTS_LIST)IoStatusBlock.Information;
1871 }
1872 else
1873 {
1874 DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
1875 DeviceNode->ResourceRequirements = NULL;
1876 }
1877
1878
1879 if (InstanceKey != NULL)
1880 {
1881 IopSetDeviceInstanceData(InstanceKey, DeviceNode);
1882 }
1883
1884 ZwClose(InstanceKey);
1885
1886 Status = IopAssignDeviceResources(DeviceNode);
1887 if (!NT_SUCCESS(Status))
1888 {
1889 DPRINT("IopAssignDeviceResources() failed (Status %x)\n", Status);
1890 }
1891
1892 DeviceNode->Flags |= DNF_PROCESSED;
1893
1894 /* Report the device to the user-mode pnp manager */
1895 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
1896 &DeviceNode->InstancePath);
1897
1898 return STATUS_SUCCESS;
1899 }
1900
1901 /*
1902 * IopActionConfigureChildServices
1903 *
1904 * Retrieve configuration for all (direct) child nodes of a parent node.
1905 *
1906 * Parameters
1907 * DeviceNode
1908 * Pointer to device node.
1909 * Context
1910 * Pointer to parent node to retrieve child node configuration for.
1911 *
1912 * Remarks
1913 * We only return a status code indicating an error (STATUS_UNSUCCESSFUL)
1914 * when we reach a device node which is not a direct child of the device
1915 * node for which we configure child services for. Any errors that occur is
1916 * logged instead so that all child services have a chance of beeing
1917 * configured.
1918 */
1919
1920 NTSTATUS
1921 IopActionConfigureChildServices(
1922 PDEVICE_NODE DeviceNode,
1923 PVOID Context)
1924 {
1925 RTL_QUERY_REGISTRY_TABLE QueryTable[2];
1926 PDEVICE_NODE ParentDeviceNode;
1927 PUNICODE_STRING Service;
1928 NTSTATUS Status;
1929
1930 DPRINT("IopActionConfigureChildServices(%p, %p)\n", DeviceNode, Context);
1931
1932 ParentDeviceNode = (PDEVICE_NODE)Context;
1933
1934 /*
1935 * We are called for the parent too, but we don't need to do special
1936 * handling for this node
1937 */
1938 if (DeviceNode == ParentDeviceNode)
1939 {
1940 DPRINT("Success\n");
1941 return STATUS_SUCCESS;
1942 }
1943
1944 /*
1945 * Make sure this device node is a direct child of the parent device node
1946 * that is given as an argument
1947 */
1948 if (DeviceNode->Parent != ParentDeviceNode)
1949 {
1950 /* Stop the traversal immediately and indicate successful operation */
1951 DPRINT("Stop\n");
1952 return STATUS_UNSUCCESSFUL;
1953 }
1954
1955 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED))
1956 {
1957 WCHAR RegKeyBuffer[MAX_PATH];
1958 UNICODE_STRING RegKey;
1959
1960 RegKey.Length = 0;
1961 RegKey.MaximumLength = sizeof(RegKeyBuffer);
1962 RegKey.Buffer = RegKeyBuffer;
1963
1964 /*
1965 * Retrieve configuration from Enum key
1966 */
1967
1968 Service = &DeviceNode->ServiceName;
1969
1970 RtlZeroMemory(QueryTable, sizeof(QueryTable));
1971 RtlInitUnicodeString(Service, NULL);
1972
1973 QueryTable[0].Name = L"Service";
1974 QueryTable[0].Flags = RTL_QUERY_REGISTRY_DIRECT;
1975 QueryTable[0].EntryContext = Service;
1976
1977 RtlAppendUnicodeToString(&RegKey, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
1978 RtlAppendUnicodeStringToString(&RegKey, &DeviceNode->InstancePath);
1979
1980 Status = RtlQueryRegistryValues(RTL_REGISTRY_ABSOLUTE,
1981 RegKey.Buffer, QueryTable, NULL, NULL);
1982
1983 if (!NT_SUCCESS(Status))
1984 {
1985 DPRINT("RtlQueryRegistryValues() failed (Status %x)\n", Status);
1986 /* FIXME: Log the error */
1987 CPRINT("Could not retrieve configuration for device %S (Status %x)\n",
1988 DeviceNode->InstancePath.Buffer, Status);
1989 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1990 return STATUS_SUCCESS;
1991 }
1992
1993 if (Service->Buffer == NULL)
1994 {
1995 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
1996 return STATUS_SUCCESS;
1997 }
1998
1999 DPRINT("Got Service %S\n", Service->Buffer);
2000 }
2001
2002 return STATUS_SUCCESS;
2003 }
2004
2005 /*
2006 * IopActionInitChildServices
2007 *
2008 * Initialize the service for all (direct) child nodes of a parent node
2009 *
2010 * Parameters
2011 * DeviceNode
2012 * Pointer to device node.
2013 * Context
2014 * Pointer to parent node to initialize child node services for.
2015 * BootDrivers
2016 * Load only driver marked as boot start.
2017 *
2018 * Remarks
2019 * If the driver image for a service is not loaded and initialized
2020 * it is done here too. We only return a status code indicating an
2021 * error (STATUS_UNSUCCESSFUL) when we reach a device node which is
2022 * not a direct child of the device node for which we initialize
2023 * child services for. Any errors that occur is logged instead so
2024 * that all child services have a chance of being initialized.
2025 */
2026
2027 NTSTATUS
2028 IopActionInitChildServices(
2029 PDEVICE_NODE DeviceNode,
2030 PVOID Context,
2031 BOOLEAN BootDrivers)
2032 {
2033 PDEVICE_NODE ParentDeviceNode;
2034 NTSTATUS Status;
2035
2036 DPRINT("IopActionInitChildServices(%p, %p, %d)\n", DeviceNode, Context,
2037 BootDrivers);
2038
2039 ParentDeviceNode = (PDEVICE_NODE)Context;
2040
2041 /*
2042 * We are called for the parent too, but we don't need to do special
2043 * handling for this node
2044 */
2045 if (DeviceNode == ParentDeviceNode)
2046 {
2047 DPRINT("Success\n");
2048 return STATUS_SUCCESS;
2049 }
2050
2051 /*
2052 * Make sure this device node is a direct child of the parent device node
2053 * that is given as an argument
2054 */
2055 #if 0
2056 if (DeviceNode->Parent != ParentDeviceNode)
2057 {
2058 /*
2059 * Stop the traversal immediately and indicate unsuccessful operation
2060 */
2061 DPRINT("Stop\n");
2062 return STATUS_UNSUCCESSFUL;
2063 }
2064 #endif
2065
2066 if (!IopDeviceNodeHasFlag(DeviceNode, DNF_DISABLED) &&
2067 !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
2068 !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
2069 {
2070 PLDR_DATA_TABLE_ENTRY ModuleObject;
2071 PDRIVER_OBJECT DriverObject;
2072
2073 Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
2074 if (NT_SUCCESS(Status) || Status == STATUS_IMAGE_ALREADY_LOADED)
2075 {
2076 if (Status != STATUS_IMAGE_ALREADY_LOADED)
2077 Status = IopInitializeDriverModule(DeviceNode, ModuleObject,
2078 &DeviceNode->ServiceName, FALSE, &DriverObject);
2079 else
2080 {
2081 /* get existing DriverObject pointer */
2082 Status = IopGetDriverObject(
2083 &DriverObject,
2084 &DeviceNode->ServiceName,
2085 FALSE);
2086 }
2087 if (NT_SUCCESS(Status))
2088 {
2089 /* Attach lower level filter drivers. */
2090 IopAttachFilterDrivers(DeviceNode, TRUE);
2091 /* Initialize the function driver for the device node */
2092 Status = IopInitializeDevice(DeviceNode, DriverObject);
2093 if (NT_SUCCESS(Status))
2094 {
2095 /* Attach upper level filter drivers. */
2096 IopAttachFilterDrivers(DeviceNode, FALSE);
2097 IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
2098
2099 Status = IopStartDevice(DeviceNode);
2100 }
2101 }
2102 }
2103 else
2104 {
2105 /*
2106 * Don't disable when trying to load only boot drivers
2107 */
2108 if (!BootDrivers)
2109 {
2110 IopDeviceNodeSetFlag(DeviceNode, DNF_DISABLED);
2111 IopDeviceNodeSetFlag(DeviceNode, DNF_START_FAILED);
2112 }
2113 /* FIXME: Log the error (possibly in IopInitializeDeviceNodeService) */
2114 CPRINT("Initialization of service %S failed (Status %x)\n",
2115 DeviceNode->ServiceName.Buffer, Status);
2116 }
2117 } else
2118 {
2119 DPRINT("Service %S is disabled or already initialized\n",
2120 DeviceNode->ServiceName.Buffer);
2121 }
2122
2123 return STATUS_SUCCESS;
2124 }
2125
2126 /*
2127 * IopActionInitAllServices
2128 *
2129 * Initialize the service for all (direct) child nodes of a parent node. This
2130 * function just calls IopActionInitChildServices with BootDrivers = FALSE.
2131 */
2132
2133 NTSTATUS
2134 IopActionInitAllServices(
2135 PDEVICE_NODE DeviceNode,
2136 PVOID Context)
2137 {
2138 return IopActionInitChildServices(DeviceNode, Context, FALSE);
2139 }
2140
2141 /*
2142 * IopActionInitBootServices
2143 *
2144 * Initialize the boot start services for all (direct) child nodes of a
2145 * parent node. This function just calls IopActionInitChildServices with
2146 * BootDrivers = TRUE.
2147 */
2148 NTSTATUS
2149 IopActionInitBootServices(
2150 PDEVICE_NODE DeviceNode,
2151 PVOID Context)
2152 {
2153 return IopActionInitChildServices(DeviceNode, Context, TRUE);
2154 }
2155
2156 /*
2157 * IopInitializePnpServices
2158 *
2159 * Initialize services for discovered children
2160 *
2161 * Parameters
2162 * DeviceNode
2163 * Top device node to start initializing services.
2164 *
2165 * BootDrivers
2166 * When set to TRUE, only drivers marked as boot start will
2167 * be loaded. Otherwise, all drivers will be loaded.
2168 *
2169 * Return Value
2170 * Status
2171 */
2172 NTSTATUS
2173 IopInitializePnpServices(
2174 IN PDEVICE_NODE DeviceNode,
2175 IN BOOLEAN BootDrivers)
2176 {
2177 DEVICETREE_TRAVERSE_CONTEXT Context;
2178
2179 DPRINT("IopInitializePnpServices(%p, %d)\n", DeviceNode, BootDrivers);
2180
2181 if (BootDrivers)
2182 {
2183 IopInitDeviceTreeTraverseContext(
2184 &Context,
2185 DeviceNode,
2186 IopActionInitBootServices,
2187 DeviceNode);
2188 }
2189 else
2190 {
2191 IopInitDeviceTreeTraverseContext(
2192 &Context,
2193 DeviceNode,
2194 IopActionInitAllServices,
2195 DeviceNode);
2196 }
2197
2198 return IopTraverseDeviceTree(&Context);
2199 }
2200
2201
2202 NTSTATUS
2203 IopInvalidateDeviceRelations(
2204 IN PDEVICE_NODE DeviceNode,
2205 IN DEVICE_RELATION_TYPE Type)
2206 {
2207 DEVICETREE_TRAVERSE_CONTEXT Context;
2208 PDEVICE_RELATIONS DeviceRelations;
2209 IO_STATUS_BLOCK IoStatusBlock;
2210 PDEVICE_NODE ChildDeviceNode;
2211 IO_STACK_LOCATION Stack;
2212 BOOL BootDrivers;
2213 OBJECT_ATTRIBUTES ObjectAttributes;
2214 UNICODE_STRING LinkName;
2215 HANDLE Handle;
2216 NTSTATUS Status;
2217 ULONG i;
2218
2219 DPRINT("DeviceNode 0x%p\n", DeviceNode);
2220
2221 DPRINT("Sending IRP_MN_QUERY_DEVICE_RELATIONS to device stack\n");
2222
2223 Stack.Parameters.QueryDeviceRelations.Type = Type/*BusRelations*/;
2224
2225 Status = IopInitiatePnpIrp(
2226 DeviceNode->PhysicalDeviceObject,
2227 &IoStatusBlock,
2228 IRP_MN_QUERY_DEVICE_RELATIONS,
2229 &Stack);
2230 if (!NT_SUCCESS(Status))
2231 {
2232 DPRINT("IopInitiatePnpIrp() failed\n");
2233 return Status;
2234 }
2235
2236 DeviceRelations = (PDEVICE_RELATIONS)IoStatusBlock.Information;
2237
2238 if ((!DeviceRelations) || (DeviceRelations->Count <= 0))
2239 {
2240 DPRINT("No PDOs\n");
2241 if (DeviceRelations)
2242 {
2243 ExFreePool(DeviceRelations);
2244 }
2245 return STATUS_SUCCESS;
2246 }
2247
2248 DPRINT("Got %d PDOs\n", DeviceRelations->Count);
2249
2250 /*
2251 * Create device nodes for all discovered devices
2252 */
2253
2254 for (i = 0; i < DeviceRelations->Count; i++)
2255 {
2256 Status = IopCreateDeviceNode(
2257 DeviceNode,
2258 DeviceRelations->Objects[i],
2259 &ChildDeviceNode);
2260 DeviceNode->Flags |= DNF_ENUMERATED;
2261 if (!NT_SUCCESS(Status))
2262 {
2263 DPRINT("No resources\n");
2264 for (i = 0; i < DeviceRelations->Count; i++)
2265 ObDereferenceObject(DeviceRelations->Objects[i]);
2266 ExFreePool(DeviceRelations);
2267 return STATUS_INSUFFICIENT_RESOURCES;
2268 }
2269 }
2270 ExFreePool(DeviceRelations);
2271
2272 /*
2273 * Retrieve information about all discovered children from the bus driver
2274 */
2275
2276 IopInitDeviceTreeTraverseContext(
2277 &Context,
2278 DeviceNode,
2279 IopActionInterrogateDeviceStack,
2280 DeviceNode);
2281
2282 Status = IopTraverseDeviceTree(&Context);
2283 if (!NT_SUCCESS(Status))
2284 {
2285 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2286 return Status;
2287 }
2288
2289 /*
2290 * Retrieve configuration from the registry for discovered children
2291 */
2292
2293 IopInitDeviceTreeTraverseContext(
2294 &Context,
2295 DeviceNode,
2296 IopActionConfigureChildServices,
2297 DeviceNode);
2298
2299 Status = IopTraverseDeviceTree(&Context);
2300 if (!NT_SUCCESS(Status))
2301 {
2302 DPRINT("IopTraverseDeviceTree() failed with status (%x)\n", Status);
2303 return Status;
2304 }
2305
2306 /*
2307 * Get the state of the system boot. If the \\SystemRoot link isn't
2308 * created yet, we will assume that it's possible to load only boot
2309 * drivers.
2310 */
2311
2312 RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
2313
2314 InitializeObjectAttributes(
2315 &ObjectAttributes,
2316 &LinkName,
2317 0,
2318 NULL,
2319 NULL);
2320
2321 Status = ZwOpenFile(
2322 &Handle,
2323 FILE_ALL_ACCESS,
2324 &ObjectAttributes,
2325 &IoStatusBlock,
2326 0,
2327 0);
2328 if(NT_SUCCESS(Status))
2329 {
2330 BootDrivers = FALSE;
2331 ZwClose(Handle);
2332 }
2333 else
2334 BootDrivers = TRUE;
2335
2336 /*
2337 * Initialize services for discovered children. Only boot drivers will
2338 * be loaded from boot driver!
2339 */
2340
2341 Status = IopInitializePnpServices(DeviceNode, BootDrivers);
2342 if (!NT_SUCCESS(Status))
2343 {
2344 DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
2345 return Status;
2346 }
2347
2348 return STATUS_SUCCESS;
2349 }
2350
2351
2352 static NTSTATUS INIT_FUNCTION
2353 IopSetRootDeviceInstanceData(PDEVICE_NODE DeviceNode)
2354 {
2355 #if 0
2356 PWSTR KeyBuffer;
2357 HANDLE InstanceKey = NULL;
2358 NTSTATUS Status;
2359
2360 /* Create registry key for the instance id, if it doesn't exist yet */
2361 KeyBuffer = ExAllocatePool(PagedPool,
2362 (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
2363 wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
2364 wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
2365 Status = IopCreateDeviceKeyPath(KeyBuffer,
2366 &InstanceKey);
2367 ExFreePool(KeyBuffer);
2368 if (!NT_SUCCESS(Status))
2369 {
2370 DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
2371 return Status;
2372 }
2373
2374 /* FIXME: Set 'ConfigFlags' value */
2375
2376 ZwClose(InstanceKey);
2377
2378 return Status;
2379 #endif
2380 return STATUS_SUCCESS;
2381 }
2382
2383
2384 VOID INIT_FUNCTION
2385 PnpInit(VOID)
2386 {
2387 PDEVICE_OBJECT Pdo;
2388 NTSTATUS Status;
2389
2390 DPRINT("PnpInit()\n");
2391
2392 KeInitializeSpinLock(&IopDeviceTreeLock);
2393
2394 /* Initialize the Bus Type GUID List */
2395 IopBusTypeGuidList = ExAllocatePool(PagedPool, sizeof(IO_BUS_TYPE_GUID_LIST));
2396 RtlZeroMemory(IopBusTypeGuidList, sizeof(IO_BUS_TYPE_GUID_LIST));
2397 ExInitializeFastMutex(&IopBusTypeGuidList->Lock);
2398
2399 /* Initialize PnP-Event notification support */
2400 Status = IopInitPlugPlayEvents();
2401 if (!NT_SUCCESS(Status))
2402 {
2403 CPRINT("IopInitPlugPlayEvents() failed\n");
2404 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2405 }
2406
2407 /*
2408 * Create root device node
2409 */
2410
2411 Status = IopCreateDriverObject(&IopRootDriverObject, NULL, 0, FALSE, NULL, 0);
2412 if (!NT_SUCCESS(Status))
2413 {
2414 CPRINT("IoCreateDriverObject() failed\n");
2415 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2416 }
2417
2418 Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
2419 0, FALSE, &Pdo);
2420 if (!NT_SUCCESS(Status))
2421 {
2422 CPRINT("IoCreateDevice() failed\n");
2423 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2424 }
2425
2426 Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
2427 if (!NT_SUCCESS(Status))
2428 {
2429 CPRINT("Insufficient resources\n");
2430 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2431 }
2432
2433 if (!RtlCreateUnicodeString(&IopRootDeviceNode->InstancePath,
2434 L"HTREE\\ROOT\\0"))
2435 {
2436 CPRINT("Failed to create the instance path!\n");
2437 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, STATUS_UNSUCCESSFUL, 0, 0, 0);
2438 }
2439
2440 /* Report the device to the user-mode pnp manager */
2441 IopQueueTargetDeviceEvent(&GUID_DEVICE_ARRIVAL,
2442 &IopRootDeviceNode->InstancePath);
2443
2444 IopRootDeviceNode->PhysicalDeviceObject->Flags |= DO_BUS_ENUMERATED_DEVICE;
2445 PnpRootDriverEntry(IopRootDriverObject, NULL);
2446 IopRootDriverObject->DriverExtension->AddDevice(
2447 IopRootDriverObject,
2448 IopRootDeviceNode->PhysicalDeviceObject);
2449 }
2450
2451
2452 VOID INIT_FUNCTION
2453 PnpInit2(VOID)
2454 {
2455 NTSTATUS Status;
2456
2457 /* Set root device instance data */
2458 Status = IopSetRootDeviceInstanceData(IopRootDeviceNode);
2459 if (!NT_SUCCESS(Status))
2460 {
2461 CPRINT("Failed to set instance data\n");
2462 KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
2463 }
2464 }
2465
2466 /* EOF */