[USB]
[reactos.git] / reactos / ntoskrnl / io / iomgr / deviface.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/deviface.c
5 * PURPOSE: Device interface functions
6 *
7 * PROGRAMMERS: Filip Navara (xnavara@volny.cz)
8 * Matthew Brace (ismarc@austin.rr.com)
9 * Hervé Poussineau (hpoussin@reactos.org)
10 */
11
12 /* INCLUDES ******************************************************************/
13
14 #include <ntoskrnl.h>
15
16 #define NDEBUG
17 #include <debug.h>
18
19 /* FUNCTIONS *****************************************************************/
20
21 static PWCHAR BaseKeyString = L"\\Registry\\Machine\\System\\CurrentControlSet\\Control\\DeviceClasses\\";
22
23 static
24 NTSTATUS
25 OpenRegistryHandlesFromSymbolicLink(IN PUNICODE_STRING SymbolicLinkName,
26 IN ACCESS_MASK DesiredAccess,
27 IN OPTIONAL PHANDLE GuidKey,
28 IN OPTIONAL PHANDLE DeviceKey,
29 IN OPTIONAL PHANDLE InstanceKey)
30 {
31 OBJECT_ATTRIBUTES ObjectAttributes;
32 WCHAR PathBuffer[MAX_PATH];
33 UNICODE_STRING BaseKeyU;
34 UNICODE_STRING GuidString, SubKeyName, ReferenceString;
35 PWCHAR StartPosition, EndPosition;
36 HANDLE ClassesKey;
37 PHANDLE GuidKeyRealP, DeviceKeyRealP, InstanceKeyRealP;
38 HANDLE GuidKeyReal, DeviceKeyReal, InstanceKeyReal;
39 NTSTATUS Status;
40
41 SubKeyName.Buffer = NULL;
42
43 if (GuidKey != NULL)
44 GuidKeyRealP = GuidKey;
45 else
46 GuidKeyRealP = &GuidKeyReal;
47
48 if (DeviceKey != NULL)
49 DeviceKeyRealP = DeviceKey;
50 else
51 DeviceKeyRealP = &DeviceKeyReal;
52
53 if (InstanceKey != NULL)
54 InstanceKeyRealP = InstanceKey;
55 else
56 InstanceKeyRealP = &InstanceKeyReal;
57
58 *GuidKeyRealP = INVALID_HANDLE_VALUE;
59 *DeviceKeyRealP = INVALID_HANDLE_VALUE;
60 *InstanceKeyRealP = INVALID_HANDLE_VALUE;
61
62 BaseKeyU.Buffer = PathBuffer;
63 BaseKeyU.Length = 0;
64 BaseKeyU.MaximumLength = MAX_PATH * sizeof(WCHAR);
65
66 RtlAppendUnicodeToString(&BaseKeyU, BaseKeyString);
67
68 /* Open the DeviceClasses key */
69 InitializeObjectAttributes(&ObjectAttributes,
70 &BaseKeyU,
71 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
72 NULL,
73 NULL);
74 Status = ZwOpenKey(&ClassesKey,
75 DesiredAccess | KEY_ENUMERATE_SUB_KEYS,
76 &ObjectAttributes);
77 if (!NT_SUCCESS(Status))
78 {
79 DPRINT1("Failed to open %wZ\n", &BaseKeyU);
80 goto cleanup;
81 }
82
83 StartPosition = wcschr(SymbolicLinkName->Buffer, L'{');
84 EndPosition = wcschr(SymbolicLinkName->Buffer, L'}');
85 if (!StartPosition || !EndPosition || StartPosition > EndPosition)
86 {
87 DPRINT1("Bad symbolic link: %wZ\n", SymbolicLinkName);
88 return STATUS_INVALID_PARAMETER_1;
89 }
90 GuidString.Buffer = StartPosition;
91 GuidString.MaximumLength = GuidString.Length = (USHORT)((ULONG_PTR)(EndPosition + 1) - (ULONG_PTR)StartPosition);
92
93 InitializeObjectAttributes(&ObjectAttributes,
94 &GuidString,
95 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
96 ClassesKey,
97 NULL);
98 Status = ZwOpenKey(GuidKeyRealP,
99 DesiredAccess | KEY_ENUMERATE_SUB_KEYS,
100 &ObjectAttributes);
101 ZwClose(ClassesKey);
102 if (!NT_SUCCESS(Status))
103 {
104 DPRINT1("Failed to open %wZ%wZ (%x)\n", &BaseKeyU, &GuidString, Status);
105 goto cleanup;
106 }
107
108 SubKeyName.MaximumLength = SymbolicLinkName->Length + sizeof(WCHAR);
109 SubKeyName.Length = 0;
110 SubKeyName.Buffer = ExAllocatePool(PagedPool, SubKeyName.MaximumLength);
111 if (!SubKeyName.Buffer)
112 {
113 Status = STATUS_INSUFFICIENT_RESOURCES;
114 goto cleanup;
115 }
116
117 RtlAppendUnicodeStringToString(&SubKeyName,
118 SymbolicLinkName);
119
120 SubKeyName.Buffer[SubKeyName.Length / sizeof(WCHAR)] = UNICODE_NULL;
121
122 SubKeyName.Buffer[0] = L'#';
123 SubKeyName.Buffer[1] = L'#';
124 SubKeyName.Buffer[2] = L'?';
125 SubKeyName.Buffer[3] = L'#';
126
127 ReferenceString.Buffer = wcsrchr(SubKeyName.Buffer, '\\');
128 if (ReferenceString.Buffer != NULL)
129 {
130 ReferenceString.Buffer[0] = L'#';
131
132 SubKeyName.Length = (USHORT)((ULONG_PTR)(ReferenceString.Buffer) - (ULONG_PTR)SubKeyName.Buffer);
133 ReferenceString.Length = SymbolicLinkName->Length - SubKeyName.Length;
134 }
135 else
136 {
137 RtlInitUnicodeString(&ReferenceString, L"#");
138 }
139
140 InitializeObjectAttributes(&ObjectAttributes,
141 &SubKeyName,
142 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
143 *GuidKeyRealP,
144 NULL);
145 Status = ZwOpenKey(DeviceKeyRealP,
146 DesiredAccess | KEY_ENUMERATE_SUB_KEYS,
147 &ObjectAttributes);
148 if (!NT_SUCCESS(Status))
149 {
150 DPRINT1("Failed to open %wZ%wZ\\%wZ\n", &BaseKeyU, &GuidString, &SubKeyName);
151 goto cleanup;
152 }
153
154 InitializeObjectAttributes(&ObjectAttributes,
155 &ReferenceString,
156 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
157 *DeviceKeyRealP,
158 NULL);
159 Status = ZwOpenKey(InstanceKeyRealP,
160 DesiredAccess,
161 &ObjectAttributes);
162 if (!NT_SUCCESS(Status))
163 {
164 DPRINT1("Failed to open %wZ%wZ\\%wZ%\\%wZ (%x)\n", &BaseKeyU, &GuidString, &SubKeyName, &ReferenceString, Status);
165 goto cleanup;
166 }
167
168 Status = STATUS_SUCCESS;
169
170 cleanup:
171 if (SubKeyName.Buffer != NULL)
172 ExFreePool(SubKeyName.Buffer);
173
174 if (NT_SUCCESS(Status))
175 {
176 if (!GuidKey)
177 ZwClose(*GuidKeyRealP);
178
179 if (!DeviceKey)
180 ZwClose(*DeviceKeyRealP);
181
182 if (!InstanceKey)
183 ZwClose(*InstanceKeyRealP);
184 }
185 else
186 {
187 if (*GuidKeyRealP != INVALID_HANDLE_VALUE)
188 ZwClose(*GuidKeyRealP);
189
190 if (*DeviceKeyRealP != INVALID_HANDLE_VALUE)
191 ZwClose(*DeviceKeyRealP);
192
193 if (*InstanceKeyRealP != INVALID_HANDLE_VALUE)
194 ZwClose(*InstanceKeyRealP);
195 }
196
197 return Status;
198 }
199 /*++
200 * @name IoOpenDeviceInterfaceRegistryKey
201 * @unimplemented
202 *
203 * Provides a handle to the device's interface instance registry key.
204 * Documented in WDK.
205 *
206 * @param SymbolicLinkName
207 * Pointer to a string which identifies the device interface instance
208 *
209 * @param DesiredAccess
210 * Desired ACCESS_MASK used to access the key (like KEY_READ,
211 * KEY_WRITE, etc)
212 *
213 * @param DeviceInterfaceKey
214 * If a call has been succesfull, a handle to the registry key
215 * will be stored there
216 *
217 * @return Three different NTSTATUS values in case of errors, and STATUS_SUCCESS
218 * otherwise (see WDK for details)
219 *
220 * @remarks Must be called at IRQL = PASSIVE_LEVEL in the context of a system thread
221 *
222 *--*/
223 NTSTATUS
224 NTAPI
225 IoOpenDeviceInterfaceRegistryKey(IN PUNICODE_STRING SymbolicLinkName,
226 IN ACCESS_MASK DesiredAccess,
227 OUT PHANDLE DeviceInterfaceKey)
228 {
229 HANDLE InstanceKey, DeviceParametersKey;
230 NTSTATUS Status;
231 OBJECT_ATTRIBUTES ObjectAttributes;
232 UNICODE_STRING DeviceParametersU = RTL_CONSTANT_STRING(L"Device Parameters");
233
234 Status = OpenRegistryHandlesFromSymbolicLink(SymbolicLinkName,
235 KEY_CREATE_SUB_KEY,
236 NULL,
237 NULL,
238 &InstanceKey);
239 if (!NT_SUCCESS(Status))
240 return Status;
241
242 InitializeObjectAttributes(&ObjectAttributes,
243 &DeviceParametersU,
244 OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
245 InstanceKey,
246 NULL);
247 Status = ZwCreateKey(&DeviceParametersKey,
248 DesiredAccess,
249 &ObjectAttributes,
250 0,
251 NULL,
252 REG_OPTION_NON_VOLATILE,
253 NULL);
254 ZwClose(InstanceKey);
255
256 if (NT_SUCCESS(Status))
257 *DeviceInterfaceKey = DeviceParametersKey;
258
259 return Status;
260 }
261
262 /*++
263 * @name IoGetDeviceInterfaceAlias
264 * @unimplemented
265 *
266 * Returns the alias device interface of the specified device interface
267 * instance, if the alias exists.
268 * Documented in WDK.
269 *
270 * @param SymbolicLinkName
271 * Pointer to a string which identifies the device interface instance
272 *
273 * @param AliasInterfaceClassGuid
274 * See WDK
275 *
276 * @param AliasSymbolicLinkName
277 * See WDK
278 *
279 * @return Three different NTSTATUS values in case of errors, and STATUS_SUCCESS
280 * otherwise (see WDK for details)
281 *
282 * @remarks Must be called at IRQL = PASSIVE_LEVEL in the context of a system thread
283 *
284 *--*/
285 NTSTATUS
286 NTAPI
287 IoGetDeviceInterfaceAlias(IN PUNICODE_STRING SymbolicLinkName,
288 IN CONST GUID *AliasInterfaceClassGuid,
289 OUT PUNICODE_STRING AliasSymbolicLinkName)
290 {
291 return STATUS_NOT_IMPLEMENTED;
292 }
293
294 /*++
295 * @name IopOpenInterfaceKey
296 *
297 * Returns the alias device interface of the specified device interface
298 *
299 * @param InterfaceClassGuid
300 * FILLME
301 *
302 * @param DesiredAccess
303 * FILLME
304 *
305 * @param pInterfaceKey
306 * FILLME
307 *
308 * @return Usual NTSTATUS
309 *
310 * @remarks None
311 *
312 *--*/
313 static NTSTATUS
314 IopOpenInterfaceKey(IN CONST GUID *InterfaceClassGuid,
315 IN ACCESS_MASK DesiredAccess,
316 OUT HANDLE *pInterfaceKey)
317 {
318 UNICODE_STRING LocalMachine = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\");
319 UNICODE_STRING GuidString;
320 UNICODE_STRING KeyName;
321 OBJECT_ATTRIBUTES ObjectAttributes;
322 HANDLE InterfaceKey = INVALID_HANDLE_VALUE;
323 NTSTATUS Status;
324
325 GuidString.Buffer = KeyName.Buffer = NULL;
326
327 Status = RtlStringFromGUID(InterfaceClassGuid, &GuidString);
328 if (!NT_SUCCESS(Status))
329 {
330 DPRINT("RtlStringFromGUID() failed with status 0x%08lx\n", Status);
331 goto cleanup;
332 }
333
334 KeyName.Length = 0;
335 KeyName.MaximumLength = LocalMachine.Length + ((USHORT)wcslen(REGSTR_PATH_DEVICE_CLASSES) + 1) * sizeof(WCHAR) + GuidString.Length;
336 KeyName.Buffer = ExAllocatePool(PagedPool, KeyName.MaximumLength);
337 if (!KeyName.Buffer)
338 {
339 DPRINT("ExAllocatePool() failed\n");
340 Status = STATUS_INSUFFICIENT_RESOURCES;
341 goto cleanup;
342 }
343
344 Status = RtlAppendUnicodeStringToString(&KeyName, &LocalMachine);
345 if (!NT_SUCCESS(Status))
346 {
347 DPRINT("RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status);
348 goto cleanup;
349 }
350 Status = RtlAppendUnicodeToString(&KeyName, REGSTR_PATH_DEVICE_CLASSES);
351 if (!NT_SUCCESS(Status))
352 {
353 DPRINT("RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status);
354 goto cleanup;
355 }
356 Status = RtlAppendUnicodeToString(&KeyName, L"\\");
357 if (!NT_SUCCESS(Status))
358 {
359 DPRINT("RtlAppendUnicodeToString() failed with status 0x%08lx\n", Status);
360 goto cleanup;
361 }
362 Status = RtlAppendUnicodeStringToString(&KeyName, &GuidString);
363 if (!NT_SUCCESS(Status))
364 {
365 DPRINT("RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status);
366 goto cleanup;
367 }
368
369 InitializeObjectAttributes(
370 &ObjectAttributes,
371 &KeyName,
372 OBJ_CASE_INSENSITIVE,
373 NULL,
374 NULL);
375 Status = ZwOpenKey(
376 &InterfaceKey,
377 DesiredAccess,
378 &ObjectAttributes);
379 if (!NT_SUCCESS(Status))
380 {
381 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
382 goto cleanup;
383 }
384
385 *pInterfaceKey = InterfaceKey;
386 Status = STATUS_SUCCESS;
387
388 cleanup:
389 if (!NT_SUCCESS(Status))
390 {
391 if (InterfaceKey != INVALID_HANDLE_VALUE)
392 ZwClose(InterfaceKey);
393 }
394 RtlFreeUnicodeString(&GuidString);
395 RtlFreeUnicodeString(&KeyName);
396 return Status;
397 }
398
399 /*++
400 * @name IoGetDeviceInterfaces
401 * @implemented
402 *
403 * Returns a list of device interfaces of a particular device interface class.
404 * Documented in WDK
405 *
406 * @param InterfaceClassGuid
407 * Points to a class GUID specifying the device interface class
408 *
409 * @param PhysicalDeviceObject
410 * Points to an optional PDO that narrows the search to only the
411 * device interfaces of the device represented by the PDO
412 *
413 * @param Flags
414 * Specifies flags that modify the search for device interfaces. The
415 * DEVICE_INTERFACE_INCLUDE_NONACTIVE flag specifies that the list of
416 * returned symbolic links should contain also disabled device
417 * interfaces in addition to the enabled ones.
418 *
419 * @param SymbolicLinkList
420 * Points to a character pointer that is filled in on successful return
421 * with a list of unicode strings identifying the device interfaces
422 * that match the search criteria. The newly allocated buffer contains
423 * a list of symbolic link names. Each unicode string in the list is
424 * null-terminated; the end of the whole list is marked by an additional
425 * NULL. The caller is responsible for freeing the buffer (ExFreePool)
426 * when it is no longer needed.
427 * If no device interfaces match the search criteria, this routine
428 * returns STATUS_SUCCESS and the string contains a single NULL
429 * character.
430 *
431 * @return Usual NTSTATUS
432 *
433 * @remarks None
434 *
435 *--*/
436 NTSTATUS
437 NTAPI
438 IoGetDeviceInterfaces(IN CONST GUID *InterfaceClassGuid,
439 IN PDEVICE_OBJECT PhysicalDeviceObject OPTIONAL,
440 IN ULONG Flags,
441 OUT PWSTR *SymbolicLinkList)
442 {
443 UNICODE_STRING Control = RTL_CONSTANT_STRING(L"Control");
444 UNICODE_STRING SymbolicLink = RTL_CONSTANT_STRING(L"SymbolicLink");
445 HANDLE InterfaceKey = INVALID_HANDLE_VALUE;
446 HANDLE DeviceKey = INVALID_HANDLE_VALUE;
447 HANDLE ReferenceKey = INVALID_HANDLE_VALUE;
448 HANDLE ControlKey = INVALID_HANDLE_VALUE;
449 PKEY_BASIC_INFORMATION DeviceBi = NULL;
450 PKEY_BASIC_INFORMATION ReferenceBi = NULL;
451 PKEY_VALUE_PARTIAL_INFORMATION bip = NULL;
452 PKEY_VALUE_PARTIAL_INFORMATION PartialInfo;
453 UNICODE_STRING KeyName;
454 OBJECT_ATTRIBUTES ObjectAttributes;
455 BOOLEAN FoundRightPDO = FALSE;
456 ULONG i = 0, j, Size, NeededLength, ActualLength, LinkedValue;
457 UNICODE_STRING ReturnBuffer = { 0, 0, NULL };
458 NTSTATUS Status;
459
460 PAGED_CODE();
461
462 Status = IopOpenInterfaceKey(InterfaceClassGuid, KEY_ENUMERATE_SUB_KEYS, &InterfaceKey);
463 if (!NT_SUCCESS(Status))
464 {
465 DPRINT("IopOpenInterfaceKey() failed with status 0x%08lx\n", Status);
466 goto cleanup;
467 }
468
469 /* Enumerate subkeys (ie the different device objets) */
470 while (TRUE)
471 {
472 Status = ZwEnumerateKey(
473 InterfaceKey,
474 i,
475 KeyBasicInformation,
476 NULL,
477 0,
478 &Size);
479 if (Status == STATUS_NO_MORE_ENTRIES)
480 {
481 break;
482 }
483 else if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
484 {
485 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
486 goto cleanup;
487 }
488
489 DeviceBi = ExAllocatePool(PagedPool, Size);
490 if (!DeviceBi)
491 {
492 DPRINT("ExAllocatePool() failed\n");
493 Status = STATUS_INSUFFICIENT_RESOURCES;
494 goto cleanup;
495 }
496 Status = ZwEnumerateKey(
497 InterfaceKey,
498 i++,
499 KeyBasicInformation,
500 DeviceBi,
501 Size,
502 &Size);
503 if (!NT_SUCCESS(Status))
504 {
505 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
506 goto cleanup;
507 }
508
509 /* Open device key */
510 KeyName.Length = KeyName.MaximumLength = (USHORT)DeviceBi->NameLength;
511 KeyName.Buffer = DeviceBi->Name;
512 InitializeObjectAttributes(
513 &ObjectAttributes,
514 &KeyName,
515 OBJ_CASE_INSENSITIVE,
516 InterfaceKey,
517 NULL);
518 Status = ZwOpenKey(
519 &DeviceKey,
520 KEY_ENUMERATE_SUB_KEYS,
521 &ObjectAttributes);
522 if (!NT_SUCCESS(Status))
523 {
524 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
525 goto cleanup;
526 }
527
528 if (PhysicalDeviceObject)
529 {
530 /* Check if we are on the right physical device object,
531 * by reading the DeviceInstance string
532 */
533 DPRINT1("PhysicalDeviceObject != NULL. Case not implemented.\n");
534 //FoundRightPDO = TRUE;
535 Status = STATUS_NOT_IMPLEMENTED;
536 goto cleanup;
537 }
538
539 /* Enumerate subkeys (ie the different reference strings) */
540 j = 0;
541 while (TRUE)
542 {
543 Status = ZwEnumerateKey(
544 DeviceKey,
545 j,
546 KeyBasicInformation,
547 NULL,
548 0,
549 &Size);
550 if (Status == STATUS_NO_MORE_ENTRIES)
551 {
552 break;
553 }
554 else if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
555 {
556 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
557 goto cleanup;
558 }
559
560 ReferenceBi = ExAllocatePool(PagedPool, Size);
561 if (!ReferenceBi)
562 {
563 DPRINT("ExAllocatePool() failed\n");
564 Status = STATUS_INSUFFICIENT_RESOURCES;
565 goto cleanup;
566 }
567 Status = ZwEnumerateKey(
568 DeviceKey,
569 j++,
570 KeyBasicInformation,
571 ReferenceBi,
572 Size,
573 &Size);
574 if (!NT_SUCCESS(Status))
575 {
576 DPRINT("ZwEnumerateKey() failed with status 0x%08lx\n", Status);
577 goto cleanup;
578 }
579
580 KeyName.Length = KeyName.MaximumLength = (USHORT)ReferenceBi->NameLength;
581 KeyName.Buffer = ReferenceBi->Name;
582 if (RtlEqualUnicodeString(&KeyName, &Control, TRUE))
583 {
584 /* Skip Control subkey */
585 goto NextReferenceString;
586 }
587
588 /* Open reference key */
589 InitializeObjectAttributes(
590 &ObjectAttributes,
591 &KeyName,
592 OBJ_CASE_INSENSITIVE,
593 DeviceKey,
594 NULL);
595 Status = ZwOpenKey(
596 &ReferenceKey,
597 KEY_QUERY_VALUE,
598 &ObjectAttributes);
599 if (!NT_SUCCESS(Status))
600 {
601 DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
602 goto cleanup;
603 }
604
605 if (!(Flags & DEVICE_INTERFACE_INCLUDE_NONACTIVE))
606 {
607 /* We have to check if the interface is enabled, by
608 * reading the Linked value in the Control subkey
609 */
610 InitializeObjectAttributes(
611 &ObjectAttributes,
612 &Control,
613 OBJ_CASE_INSENSITIVE,
614 ReferenceKey,
615 NULL);
616 Status = ZwOpenKey(
617 &ControlKey,
618 KEY_QUERY_VALUE,
619 &ObjectAttributes);
620 if (Status == STATUS_OBJECT_NAME_NOT_FOUND)
621 {
622 /* That's OK. The key doesn't exist (yet) because
623 * the interface is not activated.
624 */
625 goto NextReferenceString;
626 }
627 else if (!NT_SUCCESS(Status))
628 {
629 DPRINT1("ZwOpenKey() failed with status 0x%08lx\n", Status);
630 goto cleanup;
631 }
632
633 RtlInitUnicodeString(&KeyName, L"Linked");
634 Status = ZwQueryValueKey(ControlKey,
635 &KeyName,
636 KeyValuePartialInformation,
637 NULL,
638 0,
639 &NeededLength);
640 if (Status == STATUS_BUFFER_TOO_SMALL)
641 {
642 ActualLength = NeededLength;
643 PartialInfo = ExAllocatePool(NonPagedPool, ActualLength);
644 if (!PartialInfo)
645 {
646 Status = STATUS_INSUFFICIENT_RESOURCES;
647 goto cleanup;
648 }
649
650 Status = ZwQueryValueKey(ControlKey,
651 &KeyName,
652 KeyValuePartialInformation,
653 PartialInfo,
654 ActualLength,
655 &NeededLength);
656 if (!NT_SUCCESS(Status))
657 {
658 DPRINT1("ZwQueryValueKey #2 failed (%x)\n", Status);
659 ExFreePool(PartialInfo);
660 goto cleanup;
661 }
662
663 if (PartialInfo->Type != REG_DWORD || PartialInfo->DataLength != sizeof(ULONG))
664 {
665 DPRINT1("Bad registry read\n");
666 ExFreePool(PartialInfo);
667 goto cleanup;
668 }
669
670 RtlCopyMemory(&LinkedValue,
671 PartialInfo->Data,
672 PartialInfo->DataLength);
673
674 ExFreePool(PartialInfo);
675 if (LinkedValue == 0)
676 {
677 /* This interface isn't active */
678 goto NextReferenceString;
679 }
680 }
681 else
682 {
683 DPRINT1("ZwQueryValueKey #1 failed (%x)\n", Status);
684 goto cleanup;
685 }
686 }
687
688 /* Read the SymbolicLink string and add it into SymbolicLinkList */
689 Status = ZwQueryValueKey(
690 ReferenceKey,
691 &SymbolicLink,
692 KeyValuePartialInformation,
693 NULL,
694 0,
695 &Size);
696 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
697 {
698 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status);
699 goto cleanup;
700 }
701 bip = ExAllocatePool(PagedPool, Size);
702 if (!bip)
703 {
704 DPRINT("ExAllocatePool() failed\n");
705 Status = STATUS_INSUFFICIENT_RESOURCES;
706 goto cleanup;
707 }
708 Status = ZwQueryValueKey(
709 ReferenceKey,
710 &SymbolicLink,
711 KeyValuePartialInformation,
712 bip,
713 Size,
714 &Size);
715 if (!NT_SUCCESS(Status))
716 {
717 DPRINT("ZwQueryValueKey() failed with status 0x%08lx\n", Status);
718 goto cleanup;
719 }
720 else if (bip->Type != REG_SZ)
721 {
722 DPRINT("Unexpected registry type 0x%lx (expected 0x%lx)\n", bip->Type, REG_SZ);
723 Status = STATUS_UNSUCCESSFUL;
724 goto cleanup;
725 }
726 else if (bip->DataLength < 5 * sizeof(WCHAR))
727 {
728 DPRINT("Registry string too short (length %lu, expected %lu at least)\n", bip->DataLength < 5 * sizeof(WCHAR));
729 Status = STATUS_UNSUCCESSFUL;
730 goto cleanup;
731 }
732 KeyName.Length = KeyName.MaximumLength = (USHORT)bip->DataLength - 4 * sizeof(WCHAR);
733 KeyName.Buffer = &((PWSTR)bip->Data)[4];
734
735 /* Add new symbolic link to symbolic link list */
736 if (ReturnBuffer.Length + KeyName.Length + sizeof(WCHAR) > ReturnBuffer.MaximumLength)
737 {
738 PWSTR NewBuffer;
739 ReturnBuffer.MaximumLength = (USHORT)max(ReturnBuffer.MaximumLength * 2,
740 ReturnBuffer.Length + KeyName.Length + 2 * sizeof(WCHAR));
741 NewBuffer = ExAllocatePool(PagedPool, ReturnBuffer.MaximumLength);
742 if (!NewBuffer)
743 {
744 DPRINT("ExAllocatePool() failed\n");
745 Status = STATUS_INSUFFICIENT_RESOURCES;
746 goto cleanup;
747 }
748 RtlCopyMemory(NewBuffer, ReturnBuffer.Buffer, ReturnBuffer.Length);
749 if (ReturnBuffer.Buffer)
750 ExFreePool(ReturnBuffer.Buffer);
751 ReturnBuffer.Buffer = NewBuffer;
752 }
753 DPRINT("Adding symbolic link %wZ\n", &KeyName);
754 Status = RtlAppendUnicodeStringToString(&ReturnBuffer, &KeyName);
755 if (!NT_SUCCESS(Status))
756 {
757 DPRINT("RtlAppendUnicodeStringToString() failed with status 0x%08lx\n", Status);
758 goto cleanup;
759 }
760 /* RtlAppendUnicodeStringToString added a NULL at the end of the
761 * destination string, but didn't increase the Length field.
762 * Do it for it.
763 */
764 ReturnBuffer.Length += sizeof(WCHAR);
765
766 NextReferenceString:
767 ExFreePool(ReferenceBi);
768 ReferenceBi = NULL;
769 if (bip)
770 ExFreePool(bip);
771 bip = NULL;
772 if (ReferenceKey != INVALID_HANDLE_VALUE)
773 {
774 ZwClose(ReferenceKey);
775 ReferenceKey = INVALID_HANDLE_VALUE;
776 }
777 if (ControlKey != INVALID_HANDLE_VALUE)
778 {
779 ZwClose(ControlKey);
780 ControlKey = INVALID_HANDLE_VALUE;
781 }
782 }
783 if (FoundRightPDO)
784 {
785 /* No need to go further, as we already have found what we searched */
786 break;
787 }
788
789 ExFreePool(DeviceBi);
790 DeviceBi = NULL;
791 ZwClose(DeviceKey);
792 DeviceKey = INVALID_HANDLE_VALUE;
793 }
794
795 /* Add final NULL to ReturnBuffer */
796 if (ReturnBuffer.Length >= ReturnBuffer.MaximumLength)
797 {
798 PWSTR NewBuffer;
799 ReturnBuffer.MaximumLength += sizeof(WCHAR);
800 NewBuffer = ExAllocatePool(PagedPool, ReturnBuffer.MaximumLength);
801 if (!NewBuffer)
802 {
803 DPRINT("ExAllocatePool() failed\n");
804 Status = STATUS_INSUFFICIENT_RESOURCES;
805 goto cleanup;
806 }
807 if (ReturnBuffer.Buffer)
808 {
809 RtlCopyMemory(NewBuffer, ReturnBuffer.Buffer, ReturnBuffer.Length);
810 ExFreePool(ReturnBuffer.Buffer);
811 }
812 ReturnBuffer.Buffer = NewBuffer;
813 }
814 ReturnBuffer.Buffer[ReturnBuffer.Length / sizeof(WCHAR)] = UNICODE_NULL;
815 *SymbolicLinkList = ReturnBuffer.Buffer;
816 Status = STATUS_SUCCESS;
817
818 cleanup:
819 if (!NT_SUCCESS(Status) && ReturnBuffer.Buffer)
820 ExFreePool(ReturnBuffer.Buffer);
821 if (InterfaceKey != INVALID_HANDLE_VALUE)
822 ZwClose(InterfaceKey);
823 if (DeviceKey != INVALID_HANDLE_VALUE)
824 ZwClose(DeviceKey);
825 if (ReferenceKey != INVALID_HANDLE_VALUE)
826 ZwClose(ReferenceKey);
827 if (ControlKey != INVALID_HANDLE_VALUE)
828 ZwClose(ControlKey);
829 if (DeviceBi)
830 ExFreePool(DeviceBi);
831 if (ReferenceBi)
832 ExFreePool(ReferenceBi);
833 if (bip)
834 ExFreePool(bip);
835 return Status;
836 }
837
838 /*++
839 * @name IoRegisterDeviceInterface
840 * @implemented
841 *
842 * Registers a device interface class, if it has not been previously registered,
843 * and creates a new instance of the interface class, which a driver can
844 * subsequently enable for use by applications or other system components.
845 * Documented in WDK.
846 *
847 * @param PhysicalDeviceObject
848 * Points to an optional PDO that narrows the search to only the
849 * device interfaces of the device represented by the PDO
850 *
851 * @param InterfaceClassGuid
852 * Points to a class GUID specifying the device interface class
853 *
854 * @param ReferenceString
855 * Optional parameter, pointing to a unicode string. For a full
856 * description of this rather rarely used param (usually drivers
857 * pass NULL here) see WDK
858 *
859 * @param SymbolicLinkName
860 * Pointer to the resulting unicode string
861 *
862 * @return Usual NTSTATUS
863 *
864 * @remarks Must be called at IRQL = PASSIVE_LEVEL in the context of a
865 * system thread
866 *
867 *--*/
868 NTSTATUS
869 NTAPI
870 IoRegisterDeviceInterface(IN PDEVICE_OBJECT PhysicalDeviceObject,
871 IN CONST GUID *InterfaceClassGuid,
872 IN PUNICODE_STRING ReferenceString OPTIONAL,
873 OUT PUNICODE_STRING SymbolicLinkName)
874 {
875 PUNICODE_STRING InstancePath;
876 UNICODE_STRING GuidString;
877 UNICODE_STRING SubKeyName;
878 UNICODE_STRING InterfaceKeyName;
879 UNICODE_STRING BaseKeyName;
880 UCHAR PdoNameInfoBuffer[sizeof(OBJECT_NAME_INFORMATION) + (256 * sizeof(WCHAR))];
881 POBJECT_NAME_INFORMATION PdoNameInfo = (POBJECT_NAME_INFORMATION)PdoNameInfoBuffer;
882 UNICODE_STRING DeviceInstance = RTL_CONSTANT_STRING(L"DeviceInstance");
883 UNICODE_STRING SymbolicLink = RTL_CONSTANT_STRING(L"SymbolicLink");
884 HANDLE ClassKey;
885 HANDLE InterfaceKey;
886 HANDLE SubKey;
887 ULONG StartIndex;
888 OBJECT_ATTRIBUTES ObjectAttributes;
889 ULONG i;
890 NTSTATUS Status, SymLinkStatus;
891 PEXTENDED_DEVOBJ_EXTENSION DeviceObjectExtension;
892
893 ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
894
895 DPRINT("IoRegisterDeviceInterface(): PDO %p, RefString: %wZ\n",
896 PhysicalDeviceObject, ReferenceString);
897
898 /* Parameters must pass three border of checks */
899 DeviceObjectExtension = (PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension;
900
901 /* 1st level: Presence of a Device Node */
902 if (DeviceObjectExtension->DeviceNode == NULL)
903 {
904 DPRINT("PhysicalDeviceObject 0x%p doesn't have a DeviceNode\n", PhysicalDeviceObject);
905 return STATUS_INVALID_DEVICE_REQUEST;
906 }
907
908 /* 2nd level: Presence of an non-zero length InstancePath */
909 if (DeviceObjectExtension->DeviceNode->InstancePath.Length == 0)
910 {
911 DPRINT("PhysicalDeviceObject 0x%p's DOE has zero-length InstancePath\n", PhysicalDeviceObject);
912 return STATUS_INVALID_DEVICE_REQUEST;
913 }
914
915 /* 3rd level: Optional, based on WDK documentation */
916 if (ReferenceString != NULL)
917 {
918 /* Reference string must not contain path-separator symbols */
919 for (i = 0; i < ReferenceString->Length / sizeof(WCHAR); i++)
920 {
921 if ((ReferenceString->Buffer[i] == '\\') ||
922 (ReferenceString->Buffer[i] == '/'))
923 return STATUS_INVALID_DEVICE_REQUEST;
924 }
925 }
926
927 Status = RtlStringFromGUID(InterfaceClassGuid, &GuidString);
928 if (!NT_SUCCESS(Status))
929 {
930 DPRINT("RtlStringFromGUID() failed with status 0x%08lx\n", Status);
931 return Status;
932 }
933
934 /* Create Pdo name: \Device\xxxxxxxx (unnamed device) */
935 Status = ObQueryNameString(
936 PhysicalDeviceObject,
937 PdoNameInfo,
938 sizeof(PdoNameInfoBuffer),
939 &i);
940 if (!NT_SUCCESS(Status))
941 {
942 DPRINT("ObQueryNameString() failed with status 0x%08lx\n", Status);
943 return Status;
944 }
945 ASSERT(PdoNameInfo->Name.Length);
946
947 /* Create base key name for this interface: HKLM\SYSTEM\CurrentControlSet\Control\DeviceClasses\{GUID} */
948 ASSERT(((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode);
949 InstancePath = &((PEXTENDED_DEVOBJ_EXTENSION)PhysicalDeviceObject->DeviceObjectExtension)->DeviceNode->InstancePath;
950 BaseKeyName.Length = (USHORT)wcslen(BaseKeyString) * sizeof(WCHAR);
951 BaseKeyName.MaximumLength = BaseKeyName.Length
952 + GuidString.Length;
953 BaseKeyName.Buffer = ExAllocatePool(
954 PagedPool,
955 BaseKeyName.MaximumLength);
956 if (!BaseKeyName.Buffer)
957 {
958 DPRINT("ExAllocatePool() failed\n");
959 return STATUS_INSUFFICIENT_RESOURCES;
960 }
961 wcscpy(BaseKeyName.Buffer, BaseKeyString);
962 RtlAppendUnicodeStringToString(&BaseKeyName, &GuidString);
963
964 /* Create BaseKeyName key in registry */
965 InitializeObjectAttributes(
966 &ObjectAttributes,
967 &BaseKeyName,
968 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE | OBJ_OPENIF,
969 NULL, /* RootDirectory */
970 NULL); /* SecurityDescriptor */
971
972 Status = ZwCreateKey(
973 &ClassKey,
974 KEY_WRITE,
975 &ObjectAttributes,
976 0, /* TileIndex */
977 NULL, /* Class */
978 REG_OPTION_VOLATILE,
979 NULL); /* Disposition */
980
981 if (!NT_SUCCESS(Status))
982 {
983 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
984 ExFreePool(BaseKeyName.Buffer);
985 return Status;
986 }
987
988 /* Create key name for this interface: ##?#ACPI#PNP0501#1#{GUID} */
989 InterfaceKeyName.Length = 0;
990 InterfaceKeyName.MaximumLength =
991 4 * sizeof(WCHAR) + /* 4 = size of ##?# */
992 InstancePath->Length +
993 sizeof(WCHAR) + /* 1 = size of # */
994 GuidString.Length;
995 InterfaceKeyName.Buffer = ExAllocatePool(
996 PagedPool,
997 InterfaceKeyName.MaximumLength);
998 if (!InterfaceKeyName.Buffer)
999 {
1000 DPRINT("ExAllocatePool() failed\n");
1001 return STATUS_INSUFFICIENT_RESOURCES;
1002 }
1003
1004 RtlAppendUnicodeToString(&InterfaceKeyName, L"##?#");
1005 StartIndex = InterfaceKeyName.Length / sizeof(WCHAR);
1006 RtlAppendUnicodeStringToString(&InterfaceKeyName, InstancePath);
1007 for (i = 0; i < InstancePath->Length / sizeof(WCHAR); i++)
1008 {
1009 if (InterfaceKeyName.Buffer[StartIndex + i] == '\\')
1010 InterfaceKeyName.Buffer[StartIndex + i] = '#';
1011 }
1012 RtlAppendUnicodeToString(&InterfaceKeyName, L"#");
1013 RtlAppendUnicodeStringToString(&InterfaceKeyName, &GuidString);
1014
1015 /* Create the interface key in registry */
1016 InitializeObjectAttributes(
1017 &ObjectAttributes,
1018 &InterfaceKeyName,
1019 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE | OBJ_OPENIF,
1020 ClassKey,
1021 NULL); /* SecurityDescriptor */
1022
1023 Status = ZwCreateKey(
1024 &InterfaceKey,
1025 KEY_WRITE,
1026 &ObjectAttributes,
1027 0, /* TileIndex */
1028 NULL, /* Class */
1029 REG_OPTION_VOLATILE,
1030 NULL); /* Disposition */
1031
1032 if (!NT_SUCCESS(Status))
1033 {
1034 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
1035 ZwClose(ClassKey);
1036 ExFreePool(BaseKeyName.Buffer);
1037 return Status;
1038 }
1039
1040 /* Write DeviceInstance entry. Value is InstancePath */
1041 Status = ZwSetValueKey(
1042 InterfaceKey,
1043 &DeviceInstance,
1044 0, /* TileIndex */
1045 REG_SZ,
1046 InstancePath->Buffer,
1047 InstancePath->Length);
1048 if (!NT_SUCCESS(Status))
1049 {
1050 DPRINT("ZwSetValueKey() failed with status 0x%08lx\n", Status);
1051 ZwClose(InterfaceKey);
1052 ZwClose(ClassKey);
1053 ExFreePool(InterfaceKeyName.Buffer);
1054 ExFreePool(BaseKeyName.Buffer);
1055 return Status;
1056 }
1057
1058 /* Create subkey. Name is #ReferenceString */
1059 SubKeyName.Length = 0;
1060 SubKeyName.MaximumLength = sizeof(WCHAR);
1061 if (ReferenceString && ReferenceString->Length)
1062 SubKeyName.MaximumLength += ReferenceString->Length;
1063 SubKeyName.Buffer = ExAllocatePool(
1064 PagedPool,
1065 SubKeyName.MaximumLength);
1066 if (!SubKeyName.Buffer)
1067 {
1068 DPRINT("ExAllocatePool() failed\n");
1069 ZwClose(InterfaceKey);
1070 ZwClose(ClassKey);
1071 ExFreePool(InterfaceKeyName.Buffer);
1072 ExFreePool(BaseKeyName.Buffer);
1073 return STATUS_INSUFFICIENT_RESOURCES;
1074 }
1075 RtlAppendUnicodeToString(&SubKeyName, L"#");
1076 if (ReferenceString && ReferenceString->Length)
1077 RtlAppendUnicodeStringToString(&SubKeyName, ReferenceString);
1078
1079 /* Create SubKeyName key in registry */
1080 InitializeObjectAttributes(
1081 &ObjectAttributes,
1082 &SubKeyName,
1083 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
1084 InterfaceKey, /* RootDirectory */
1085 NULL); /* SecurityDescriptor */
1086
1087 Status = ZwCreateKey(
1088 &SubKey,
1089 KEY_WRITE,
1090 &ObjectAttributes,
1091 0, /* TileIndex */
1092 NULL, /* Class */
1093 REG_OPTION_VOLATILE,
1094 NULL); /* Disposition */
1095
1096 if (!NT_SUCCESS(Status))
1097 {
1098 DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
1099 ZwClose(InterfaceKey);
1100 ZwClose(ClassKey);
1101 ExFreePool(InterfaceKeyName.Buffer);
1102 ExFreePool(BaseKeyName.Buffer);
1103 return Status;
1104 }
1105
1106 /* Create symbolic link name: \??\ACPI#PNP0501#1#{GUID}\ReferenceString */
1107 SymbolicLinkName->Length = 0;
1108 SymbolicLinkName->MaximumLength = SymbolicLinkName->Length
1109 + 4 * sizeof(WCHAR) /* 4 = size of \??\ */
1110 + InstancePath->Length
1111 + sizeof(WCHAR) /* 1 = size of # */
1112 + GuidString.Length
1113 + sizeof(WCHAR); /* final NULL */
1114 if (ReferenceString && ReferenceString->Length)
1115 SymbolicLinkName->MaximumLength += sizeof(WCHAR) + ReferenceString->Length;
1116 SymbolicLinkName->Buffer = ExAllocatePool(
1117 PagedPool,
1118 SymbolicLinkName->MaximumLength);
1119 if (!SymbolicLinkName->Buffer)
1120 {
1121 DPRINT("ExAllocatePool() failed\n");
1122 ZwClose(SubKey);
1123 ZwClose(InterfaceKey);
1124 ZwClose(ClassKey);
1125 ExFreePool(InterfaceKeyName.Buffer);
1126 ExFreePool(SubKeyName.Buffer);
1127 ExFreePool(BaseKeyName.Buffer);
1128 return STATUS_INSUFFICIENT_RESOURCES;
1129 }
1130 RtlAppendUnicodeToString(SymbolicLinkName, L"\\??\\");
1131 StartIndex = SymbolicLinkName->Length / sizeof(WCHAR);
1132 RtlAppendUnicodeStringToString(SymbolicLinkName, InstancePath);
1133 for (i = 0; i < InstancePath->Length / sizeof(WCHAR); i++)
1134 {
1135 if (SymbolicLinkName->Buffer[StartIndex + i] == '\\')
1136 SymbolicLinkName->Buffer[StartIndex + i] = '#';
1137 }
1138 RtlAppendUnicodeToString(SymbolicLinkName, L"#");
1139 RtlAppendUnicodeStringToString(SymbolicLinkName, &GuidString);
1140 SymbolicLinkName->Buffer[SymbolicLinkName->Length/sizeof(WCHAR)] = L'\0';
1141
1142 /* Create symbolic link */
1143 DPRINT("IoRegisterDeviceInterface(): creating symbolic link %wZ -> %wZ\n", SymbolicLinkName, &PdoNameInfo->Name);
1144 SymLinkStatus = IoCreateSymbolicLink(SymbolicLinkName, &PdoNameInfo->Name);
1145
1146 /* If the symbolic link already exists, return an informational success status */
1147 if (SymLinkStatus == STATUS_OBJECT_NAME_COLLISION)
1148 {
1149 /* HACK: Delete the existing symbolic link and update it to the new PDO name */
1150 IoDeleteSymbolicLink(SymbolicLinkName);
1151 IoCreateSymbolicLink(SymbolicLinkName, &PdoNameInfo->Name);
1152 SymLinkStatus = STATUS_OBJECT_NAME_EXISTS;
1153 }
1154
1155 if (!NT_SUCCESS(SymLinkStatus))
1156 {
1157 DPRINT1("IoCreateSymbolicLink() failed with status 0x%08lx\n", SymLinkStatus);
1158 ZwClose(SubKey);
1159 ZwClose(InterfaceKey);
1160 ZwClose(ClassKey);
1161 ExFreePool(SubKeyName.Buffer);
1162 ExFreePool(InterfaceKeyName.Buffer);
1163 ExFreePool(BaseKeyName.Buffer);
1164 ExFreePool(SymbolicLinkName->Buffer);
1165 return SymLinkStatus;
1166 }
1167
1168 if (ReferenceString && ReferenceString->Length)
1169 {
1170 RtlAppendUnicodeToString(SymbolicLinkName, L"\\");
1171 RtlAppendUnicodeStringToString(SymbolicLinkName, ReferenceString);
1172 }
1173 SymbolicLinkName->Buffer[SymbolicLinkName->Length/sizeof(WCHAR)] = L'\0';
1174
1175 /* Write symbolic link name in registry */
1176 SymbolicLinkName->Buffer[1] = '\\';
1177 Status = ZwSetValueKey(
1178 SubKey,
1179 &SymbolicLink,
1180 0, /* TileIndex */
1181 REG_SZ,
1182 SymbolicLinkName->Buffer,
1183 SymbolicLinkName->Length);
1184 if (!NT_SUCCESS(Status))
1185 {
1186 DPRINT1("ZwSetValueKey() failed with status 0x%08lx\n", Status);
1187 ExFreePool(SymbolicLinkName->Buffer);
1188 }
1189 else
1190 {
1191 SymbolicLinkName->Buffer[1] = '?';
1192 }
1193
1194 ZwClose(SubKey);
1195 ZwClose(InterfaceKey);
1196 ZwClose(ClassKey);
1197 ExFreePool(SubKeyName.Buffer);
1198 ExFreePool(InterfaceKeyName.Buffer);
1199 ExFreePool(BaseKeyName.Buffer);
1200
1201 return NT_SUCCESS(Status) ? SymLinkStatus : Status;
1202 }
1203
1204 /*++
1205 * @name IoSetDeviceInterfaceState
1206 * @implemented
1207 *
1208 * Enables or disables an instance of a previously registered device
1209 * interface class.
1210 * Documented in WDK.
1211 *
1212 * @param SymbolicLinkName
1213 * Pointer to the string identifying instance to enable or disable
1214 *
1215 * @param Enable
1216 * TRUE = enable, FALSE = disable
1217 *
1218 * @return Usual NTSTATUS
1219 *
1220 * @remarks Must be called at IRQL = PASSIVE_LEVEL in the context of a
1221 * system thread
1222 *
1223 *--*/
1224 NTSTATUS
1225 NTAPI
1226 IoSetDeviceInterfaceState(IN PUNICODE_STRING SymbolicLinkName,
1227 IN BOOLEAN Enable)
1228 {
1229 PDEVICE_OBJECT PhysicalDeviceObject;
1230 PFILE_OBJECT FileObject;
1231 UNICODE_STRING GuidString;
1232 UNICODE_STRING SymLink;
1233 PWCHAR StartPosition;
1234 PWCHAR EndPosition;
1235 NTSTATUS Status;
1236 LPCGUID EventGuid;
1237 HANDLE InstanceHandle, ControlHandle;
1238 UNICODE_STRING KeyName;
1239 OBJECT_ATTRIBUTES ObjectAttributes;
1240 ULONG LinkedValue;
1241 GUID DeviceGuid;
1242
1243 if (SymbolicLinkName == NULL)
1244 return STATUS_INVALID_PARAMETER_1;
1245
1246 DPRINT("IoSetDeviceInterfaceState('%wZ', %d)\n", SymbolicLinkName, Enable);
1247
1248 /* Symbolic link name is \??\ACPI#PNP0501#1#{GUID}\ReferenceString */
1249 /* Get GUID from SymbolicLinkName */
1250 StartPosition = wcschr(SymbolicLinkName->Buffer, L'{');
1251 EndPosition = wcschr(SymbolicLinkName->Buffer, L'}');
1252 if (!StartPosition ||!EndPosition || StartPosition > EndPosition)
1253 {
1254 DPRINT1("IoSetDeviceInterfaceState() returning STATUS_INVALID_PARAMETER_1\n");
1255 return STATUS_INVALID_PARAMETER_1;
1256 }
1257 GuidString.Buffer = StartPosition;
1258 GuidString.MaximumLength = GuidString.Length = (USHORT)((ULONG_PTR)(EndPosition + 1) - (ULONG_PTR)StartPosition);
1259
1260 SymLink.Buffer = SymbolicLinkName->Buffer;
1261 SymLink.MaximumLength = SymLink.Length = (USHORT)((ULONG_PTR)(EndPosition + 1) - (ULONG_PTR)SymLink.Buffer);
1262 DPRINT("IoSetDeviceInterfaceState('%wZ', %d)\n", SymbolicLinkName, Enable);
1263
1264 Status = OpenRegistryHandlesFromSymbolicLink(SymbolicLinkName,
1265 KEY_CREATE_SUB_KEY,
1266 NULL,
1267 NULL,
1268 &InstanceHandle);
1269 if (!NT_SUCCESS(Status))
1270 return Status;
1271
1272 RtlInitUnicodeString(&KeyName, L"Control");
1273 InitializeObjectAttributes(&ObjectAttributes,
1274 &KeyName,
1275 OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
1276 InstanceHandle,
1277 NULL);
1278 Status = ZwCreateKey(&ControlHandle,
1279 KEY_SET_VALUE,
1280 &ObjectAttributes,
1281 0,
1282 NULL,
1283 REG_OPTION_VOLATILE,
1284 NULL);
1285 ZwClose(InstanceHandle);
1286 if (!NT_SUCCESS(Status))
1287 {
1288 DPRINT1("Failed to create the Control subkey\n");
1289 return Status;
1290 }
1291
1292 LinkedValue = (Enable ? 1 : 0);
1293
1294 RtlInitUnicodeString(&KeyName, L"Linked");
1295 Status = ZwSetValueKey(ControlHandle,
1296 &KeyName,
1297 0,
1298 REG_DWORD,
1299 &LinkedValue,
1300 sizeof(ULONG));
1301 ZwClose(ControlHandle);
1302 if (!NT_SUCCESS(Status))
1303 {
1304 DPRINT1("Failed to write the Linked value\n");
1305 return Status;
1306 }
1307
1308 /* Get pointer to the PDO */
1309 Status = IoGetDeviceObjectPointer(
1310 &SymLink,
1311 0, /* DesiredAccess */
1312 &FileObject,
1313 &PhysicalDeviceObject);
1314 if (!NT_SUCCESS(Status))
1315 {
1316 DPRINT1("IoGetDeviceObjectPointer() failed with status 0x%08lx\n", Status);
1317 return Status;
1318 }
1319
1320 Status = RtlGUIDFromString(&GuidString, &DeviceGuid);
1321 if (!NT_SUCCESS(Status))
1322 {
1323 DPRINT1("RtlGUIDFromString() failed with status 0x%08lx\n", Status);
1324 return Status;
1325 }
1326
1327 EventGuid = Enable ? &GUID_DEVICE_INTERFACE_ARRIVAL : &GUID_DEVICE_INTERFACE_REMOVAL;
1328 IopNotifyPlugPlayNotification(
1329 PhysicalDeviceObject,
1330 EventCategoryDeviceInterfaceChange,
1331 EventGuid,
1332 &DeviceGuid,
1333 (PVOID)SymbolicLinkName);
1334
1335 ObDereferenceObject(FileObject);
1336 DPRINT("Status %x\n", Status);
1337 return STATUS_SUCCESS;
1338 }
1339
1340 /* EOF */