fix typo
[reactos.git] / reactos / ntoskrnl / io / pnpmgr / pnpmgr.c
index 3ad01f8..54a595f 100644 (file)
@@ -19,6 +19,8 @@
 
 /* GLOBALS *******************************************************************/
 
+#define ENUM_ROOT L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum"
+
 PDEVICE_NODE IopRootDeviceNode;
 KSPIN_LOCK IopDeviceTreeLock;
 ERESOURCE PpRegistryDeviceResource;
@@ -310,31 +312,33 @@ IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject,
                     OUT PVOID PropertyBuffer,
                     OUT PULONG ResultLength)
 {
-   PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
-   DEVICE_CAPABILITIES DeviceCaps;
-   ULONG Length;
-   PVOID Data = NULL;
-   PWSTR Ptr;
-   NTSTATUS Status;
+    PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
+    DEVICE_CAPABILITIES DeviceCaps;
+    ULONG Length;
+    PVOID Data = NULL;
+    PWSTR Ptr;
+    NTSTATUS Status;
 
-   DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject, DeviceProperty);
+    DPRINT("IoGetDeviceProperty(0x%p %d)\n", DeviceObject, DeviceProperty);
 
-   if (DeviceNode == NULL)
-      return STATUS_INVALID_DEVICE_REQUEST;
+    *ResultLength = 0;
 
-   switch (DeviceProperty)
-   {
-      case DevicePropertyBusNumber:
-         Length = sizeof(ULONG);
-         Data = &DeviceNode->ChildBusNumber;
-         break;
+    if (DeviceNode == NULL)
+        return STATUS_INVALID_DEVICE_REQUEST;
 
-      /* Complete, untested */
-      case DevicePropertyBusTypeGuid:
-         /* Sanity check */
-         if ((DeviceNode->ChildBusTypeIndex != 0xFFFF) &&
-             (DeviceNode->ChildBusTypeIndex < IopBusTypeGuidList->GuidCount))
-         {
+    switch (DeviceProperty)
+    {
+    case DevicePropertyBusNumber:
+        Length = sizeof(ULONG);
+        Data = &DeviceNode->ChildBusNumber;
+        break;
+
+        /* Complete, untested */
+    case DevicePropertyBusTypeGuid:
+        /* Sanity check */
+        if ((DeviceNode->ChildBusTypeIndex != 0xFFFF) &&
+            (DeviceNode->ChildBusTypeIndex < IopBusTypeGuidList->GuidCount))
+        {
             /* Return the GUID */
             *ResultLength = sizeof(GUID);
 
@@ -346,44 +350,44 @@ IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject,
 
             /* Copy the GUID */
             RtlCopyMemory(PropertyBuffer,
-                          &(IopBusTypeGuidList->Guids[DeviceNode->ChildBusTypeIndex]),
-                          sizeof(GUID));
+                &(IopBusTypeGuidList->Guids[DeviceNode->ChildBusTypeIndex]),
+                sizeof(GUID));
             return STATUS_SUCCESS;
-         }
-         else
-         {
+        }
+        else
+        {
             return STATUS_OBJECT_NAME_NOT_FOUND;
-         }
-         break;
+        }
+        break;
 
-      case DevicePropertyLegacyBusType:
-         Length = sizeof(INTERFACE_TYPE);
-         Data = &DeviceNode->ChildInterfaceType;
-         break;
+    case DevicePropertyLegacyBusType:
+        Length = sizeof(INTERFACE_TYPE);
+        Data = &DeviceNode->ChildInterfaceType;
+        break;
 
-      case DevicePropertyAddress:
-         /* Query the device caps */
-         Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps);
-         if (NT_SUCCESS(Status) && (DeviceCaps.Address != (ULONG)-1))
-         {
+    case DevicePropertyAddress:
+        /* Query the device caps */
+        Status = IopQueryDeviceCapabilities(DeviceNode, &DeviceCaps);
+        if (NT_SUCCESS(Status) && (DeviceCaps.Address != (ULONG)-1))
+        {
             /* Return length */
             *ResultLength = sizeof(ULONG);
 
             /* Check if the buffer given was large enough */
             if (BufferLength < *ResultLength)
             {
-               return STATUS_BUFFER_TOO_SMALL;
+                return STATUS_BUFFER_TOO_SMALL;
             }
 
             /* Return address */
             *(PULONG)PropertyBuffer = DeviceCaps.Address;
             return STATUS_SUCCESS;
-         }
-         else
-         {
+        }
+        else
+        {
             return STATUS_OBJECT_NAME_NOT_FOUND;
-         }
-         break;
+        }
+        break;
 
 //    case DevicePropertyUINumber:
 //      if (DeviceNode->CapabilityFlags == NULL)
@@ -392,161 +396,165 @@ IoGetDeviceProperty(IN PDEVICE_OBJECT DeviceObject,
 //      Data = &DeviceNode->CapabilityFlags->UINumber;
 //      break;
 
-      case DevicePropertyClassName:
-      case DevicePropertyClassGuid:
-      case DevicePropertyDriverKeyName:
-      case DevicePropertyManufacturer:
-      case DevicePropertyFriendlyName:
-      case DevicePropertyHardwareID:
-      case DevicePropertyCompatibleIDs:
-      case DevicePropertyDeviceDescription:
-      case DevicePropertyLocationInformation:
-      case DevicePropertyUINumber:
-      {
-         LPWSTR RegistryPropertyName, KeyNameBuffer;
-         UNICODE_STRING KeyName, ValueName;
-         OBJECT_ATTRIBUTES ObjectAttributes;
-         KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
-         ULONG ValueInformationLength;
-         HANDLE KeyHandle;
-         NTSTATUS Status;
-
-         switch (DeviceProperty)
-         {
+    case DevicePropertyClassName:
+    case DevicePropertyClassGuid:
+    case DevicePropertyDriverKeyName:
+    case DevicePropertyManufacturer:
+    case DevicePropertyFriendlyName:
+    case DevicePropertyHardwareID:
+    case DevicePropertyCompatibleIDs:
+    case DevicePropertyDeviceDescription:
+    case DevicePropertyLocationInformation:
+    case DevicePropertyUINumber:
+        {
+            LPCWSTR RegistryPropertyName;
+            UNICODE_STRING EnumRoot = RTL_CONSTANT_STRING(ENUM_ROOT);
+            UNICODE_STRING ValueName;
+            OBJECT_ATTRIBUTES ObjectAttributes;
+            KEY_VALUE_PARTIAL_INFORMATION *ValueInformation;
+            ULONG ValueInformationLength;
+            HANDLE KeyHandle, EnumRootHandle;
+            NTSTATUS Status;
+
+            switch (DeviceProperty)
+            {
             case DevicePropertyClassName:
-               RegistryPropertyName = L"Class"; break;
+                RegistryPropertyName = L"Class"; break;
             case DevicePropertyClassGuid:
-               RegistryPropertyName = L"ClassGuid"; break;
+                RegistryPropertyName = L"ClassGuid"; break;
             case DevicePropertyDriverKeyName:
-               RegistryPropertyName = L"Driver"; break;
+                RegistryPropertyName = L"Driver"; break;
             case DevicePropertyManufacturer:
-               RegistryPropertyName = L"Mfg"; break;
+                RegistryPropertyName = L"Mfg"; break;
             case DevicePropertyFriendlyName:
-               RegistryPropertyName = L"FriendlyName"; break;
+                RegistryPropertyName = L"FriendlyName"; break;
             case DevicePropertyHardwareID:
-               RegistryPropertyName = L"HardwareID"; break;
+                RegistryPropertyName = L"HardwareID"; break;
             case DevicePropertyCompatibleIDs:
-               RegistryPropertyName = L"CompatibleIDs"; break;
+                RegistryPropertyName = L"CompatibleIDs"; break;
             case DevicePropertyDeviceDescription:
-               RegistryPropertyName = L"DeviceDesc"; break;
+                RegistryPropertyName = L"DeviceDesc"; break;
             case DevicePropertyLocationInformation:
-               RegistryPropertyName = L"LocationInformation"; break;
+                RegistryPropertyName = L"LocationInformation"; break;
             case DevicePropertyUINumber:
-               RegistryPropertyName = L"UINumber"; break;
+                RegistryPropertyName = L"UINumber"; break;
             default:
-               RegistryPropertyName = NULL; break;
-         }
+                /* Should not happen */
+                ASSERT(FALSE);
+                return STATUS_UNSUCCESSFUL;
+            }
 
-         KeyNameBuffer = ExAllocatePool(PagedPool,
-            (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
+            DPRINT("Registry property %S\n", RegistryPropertyName);
 
-         DPRINT("KeyNameBuffer: 0x%p, value %S\n", KeyNameBuffer, RegistryPropertyName);
+            /* Open Enum key */
+            InitializeObjectAttributes(&ObjectAttributes, &EnumRoot,
+                OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
+            Status = ZwOpenKey(&EnumRootHandle, 0, &ObjectAttributes);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("Error opening ENUM_ROOT, Status=0x%08x\n", Status);
+                return Status;
+            }
 
-         if (KeyNameBuffer == NULL)
-            return STATUS_INSUFFICIENT_RESOURCES;
+            /* Open instance key */
+            InitializeObjectAttributes(&ObjectAttributes, &DeviceNode->InstancePath,
+                OBJ_CASE_INSENSITIVE, EnumRootHandle, NULL);
 
-         wcscpy(KeyNameBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
-         wcscat(KeyNameBuffer, DeviceNode->InstancePath.Buffer);
-         RtlInitUnicodeString(&KeyName, KeyNameBuffer);
-         InitializeObjectAttributes(&ObjectAttributes, &KeyName,
-                                    OBJ_CASE_INSENSITIVE, NULL, NULL);
+            Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("Error opening InstancePath, Status=0x%08x\n", Status);
+                return Status;
+            }
 
-         Status = ZwOpenKey(&KeyHandle, KEY_READ, &ObjectAttributes);
-         ExFreePool(KeyNameBuffer);
-         if (!NT_SUCCESS(Status))
-            return Status;
+            /* Allocate buffer to read as much data as required by the caller */
+            ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
+                Data[0]) + BufferLength;
+            ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
+            if (!ValueInformation)
+            {
+                ZwClose(KeyHandle);
+                return STATUS_INSUFFICIENT_RESOURCES;
+            }
 
-         RtlInitUnicodeString(&ValueName, RegistryPropertyName);
-         ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
-                                               Data[0]) + BufferLength;
-         ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
-         if (ValueInformation == NULL)
-         {
+            /* Read the value */
+            RtlInitUnicodeString(&ValueName, RegistryPropertyName);
+            Status = ZwQueryValueKey(KeyHandle, &ValueName,
+                KeyValuePartialInformation, ValueInformation,
+                ValueInformationLength,
+                &ValueInformationLength);
             ZwClose(KeyHandle);
-            return STATUS_INSUFFICIENT_RESOURCES;
-         }
-
-         Status = ZwQueryValueKey(KeyHandle, &ValueName,
-                                  KeyValuePartialInformation, ValueInformation,
-                                  ValueInformationLength,
-                                  &ValueInformationLength);
-         *ResultLength = ValueInformation->DataLength;
-         ZwClose(KeyHandle);
-
-         if (!NT_SUCCESS(Status))
-         {
-            ExFreePool(ValueInformation);
-            if (Status == STATUS_BUFFER_OVERFLOW)
-               return STATUS_BUFFER_TOO_SMALL;
-            else
-               return Status;
-         }
-
-         /* FIXME: Verify the value (NULL-terminated, correct format). */
 
-         RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
-                       ValueInformation->DataLength);
-         ExFreePool(ValueInformation);
+            /* Return data */
+            *ResultLength = ValueInformation->DataLength;
 
-         return STATUS_SUCCESS;
-      }
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("Problem: Status=0x%08x, ResultLength = %d\n", Status, *ResultLength);
+                ExFreePool(ValueInformation);
+                if (Status == STATUS_BUFFER_OVERFLOW)
+                    return STATUS_BUFFER_TOO_SMALL;
+                else
+                    return Status;
+            }
 
-   case DevicePropertyBootConfiguration:
-      Length = 0;
-      if (DeviceNode->BootResources->Count != 0)
-      {
-         Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
-      }
-      Data = &DeviceNode->BootResources;
-      break;
+            /* FIXME: Verify the value (NULL-terminated, correct format). */
+            RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
+                ValueInformation->DataLength);
+            ExFreePool(ValueInformation);
 
-   /* FIXME: use a translated boot configuration instead */
-   case DevicePropertyBootConfigurationTranslated:
-      Length = 0;
-      if (DeviceNode->BootResources->Count != 0)
-      {
-         Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
-      }
-      Data = &DeviceNode->BootResources;
-      break;
+            return STATUS_SUCCESS;
+        }
 
-   case DevicePropertyEnumeratorName:
-      Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
-      if (Ptr != NULL)
-      {
-         Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
-         Data = DeviceNode->InstancePath.Buffer;
-      }
-      else
-      {
-         Length = 0;
-         Data = NULL;
-      }
-      break;
+    case DevicePropertyBootConfiguration:
+        Length = 0;
+        if (DeviceNode->BootResources->Count != 0)
+        {
+            Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
+        }
+        Data = &DeviceNode->BootResources;
+        break;
 
-   case DevicePropertyPhysicalDeviceObjectName:
-      Length = DeviceNode->InstancePath.Length + sizeof(WCHAR);
-      Data = DeviceNode->InstancePath.Buffer;
-      break;
+        /* FIXME: use a translated boot configuration instead */
+    case DevicePropertyBootConfigurationTranslated:
+        Length = 0;
+        if (DeviceNode->BootResources->Count != 0)
+        {
+            Length = CM_RESOURCE_LIST_SIZE(DeviceNode->BootResources);
+        }
+        Data = &DeviceNode->BootResources;
+        break;
+
+    case DevicePropertyEnumeratorName:
+        /* A buffer overflow can't happen here, since InstancePath
+        * always contains the enumerator name followed by \\ */
+        Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
+        ASSERT(Ptr);
+        Length = (Ptr - DeviceNode->InstancePath.Buffer + 1) * sizeof(WCHAR);
+        Data = DeviceNode->InstancePath.Buffer;
+        break;
+
+    case DevicePropertyPhysicalDeviceObjectName:
+        /* InstancePath buffer is NULL terminated, so we can do this */
+        Length = DeviceNode->InstancePath.MaximumLength;
+        Data = DeviceNode->InstancePath.Buffer;
+        break;
 
-   default:
-      return STATUS_INVALID_PARAMETER_2;
-  }
+    default:
+        return STATUS_INVALID_PARAMETER_2;
+    }
 
-  *ResultLength = Length;
-  if (BufferLength < Length)
-     return STATUS_BUFFER_TOO_SMALL;
-  RtlCopyMemory(PropertyBuffer, Data, Length);
+    /* Prepare returned values */
+    *ResultLength = Length;
+    if (BufferLength < Length)
+        return STATUS_BUFFER_TOO_SMALL;
+    RtlCopyMemory(PropertyBuffer, Data, Length);
 
-  /* Terminate the string */
-  if (DeviceProperty == DevicePropertyEnumeratorName
-     || DeviceProperty == DevicePropertyPhysicalDeviceObjectName)
-  {
-     Ptr = (PWSTR)PropertyBuffer;
-     Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
-  }
+    /* NULL terminate the string (if required) */
+    if (DeviceProperty == DevicePropertyEnumeratorName)
+        ((LPWSTR)PropertyBuffer)[Length / sizeof(WCHAR)] = UNICODE_NULL;
 
-  return STATUS_SUCCESS;
+    return STATUS_SUCCESS;
 }
 
 /*
@@ -1062,82 +1070,110 @@ IopTraverseDeviceTree(PDEVICETREE_TRAVERSE_CONTEXT Context)
 }
 
 
-static
+/*
+ * IopCreateDeviceKeyPath
+ *
+ * Creates a registry key
+ *
+ * Parameters
+ *    RegistryPath
+ *        Name of the key to be created.
+ *    Handle
+ *        Handle to the newly created key
+ *
+ * Remarks
+ *     This method can create nested trees, so parent of RegistryPath can
+ *     be not existant, and will be created if needed.
+ */
 NTSTATUS
-IopCreateDeviceKeyPath(PWSTR Path,
-                       PHANDLE Handle)
+NTAPI
+IopCreateDeviceKeyPath(IN PCUNICODE_STRING RegistryPath,
+                       OUT PHANDLE Handle)
 {
-   OBJECT_ATTRIBUTES ObjectAttributes;
-   WCHAR KeyBuffer[MAX_PATH];
-   UNICODE_STRING KeyName;
-   HANDLE KeyHandle;
-   NTSTATUS Status;
-   PWCHAR Current;
-   PWCHAR Next;
+    UNICODE_STRING EnumU = RTL_CONSTANT_STRING(ENUM_ROOT);
+    HANDLE hParent = NULL, hKey;
+    OBJECT_ATTRIBUTES ObjectAttributes;
+    UNICODE_STRING KeyName;
+    LPCWSTR Current, Last;
+    ULONG dwLength;
+    NTSTATUS Status;
 
-   *Handle = NULL;
+    /* Assume failure */
+    *Handle = NULL;
 
-   if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
-   {
-      return STATUS_INVALID_PARAMETER;
-   }
+    /* Open root key for device instances */
+    InitializeObjectAttributes(&ObjectAttributes,
+                               &EnumU,
+                               OBJ_CASE_INSENSITIVE,
+                               NULL,
+                               NULL);
+    Status = ZwOpenKey(&hParent,
+                       KEY_CREATE_SUB_KEY,
+                       &ObjectAttributes);
+    if (!NT_SUCCESS(Status))
+    {
+        DPRINT1("ZwOpenKey('%wZ') failed with status 0x%08lx\n", &EnumU, Status);
+        return Status;
+    }
 
-   wcsncpy (KeyBuffer, Path, MAX_PATH-1);
+    Current = KeyName.Buffer = RegistryPath->Buffer;
+    Last = &RegistryPath->Buffer[RegistryPath->Length / sizeof(WCHAR)];
 
-   /* Skip \\Registry\\ */
-   Current = KeyBuffer;
-   Current = wcschr (Current, L'\\') + 1;
-   Current = wcschr (Current, L'\\') + 1;
+    /* Go up to the end of the string */
+    while (Current <= Last)
+    {
+        if (Current != Last && *Current != '\\')
+        {
+            /* Not the end of the string and not a separator */
+            Current++;
+            continue;
+        }
 
-   while (TRUE)
-   {
-      Next = wcschr (Current, L'\\');
-      if (Next == NULL)
-      {
-         /* The end */
-      }
-      else
-      {
-         *Next = 0;
-      }
+        /* Prepare relative key name */
+        dwLength = (ULONG_PTR)Current - (ULONG_PTR)KeyName.Buffer;
+        KeyName.MaximumLength = KeyName.Length = dwLength;
+        DPRINT("Create '%wZ'\n", &KeyName);
 
-      RtlInitUnicodeString (&KeyName, KeyBuffer);
-      InitializeObjectAttributes (&ObjectAttributes,
-                                  &KeyName,
-                                  OBJ_CASE_INSENSITIVE,
-                                  NULL,
-                                  NULL);
+        /* Open key */
+        InitializeObjectAttributes(&ObjectAttributes,
+                                   &KeyName,
+                                   OBJ_CASE_INSENSITIVE,
+                                   hParent,
+                                   NULL);
+        Status = ZwCreateKey(&hKey,
+                             Current == Last ? KEY_ALL_ACCESS : KEY_CREATE_SUB_KEY,
+                             &ObjectAttributes,
+                             0,
+                             NULL,
+                             0,
+                             NULL);
 
-      DPRINT("Create '%S'\n", KeyName.Buffer);
+        /* Close parent key handle, we don't need it anymore */
+        if (hParent)
+            ZwClose(hParent);
 
-      Status = ZwCreateKey (&KeyHandle,
-                            KEY_ALL_ACCESS,
-                            &ObjectAttributes,
-                            0,
-                            NULL,
-                            0,
-                            NULL);
-      if (!NT_SUCCESS (Status))
-      {
-         DPRINT ("ZwCreateKey() failed with status %x\n", Status);
-         return Status;
-      }
+        /* Key opening/creating failed? */
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT1("ZwCreateKey('%wZ') failed with status 0x%08lx\n", &KeyName, Status);
+            return Status;
+        }
 
-      if (Next == NULL)
-      {
-         *Handle = KeyHandle;
-         return STATUS_SUCCESS;
-      }
-      else
-      {
-         ZwClose (KeyHandle);
-         *Next = L'\\';
-      }
+        /* Check if it is the end of the string */
+        if (Current == Last)
+        {
+            /* Yes, return success */
+            *Handle = hKey;
+            return STATUS_SUCCESS;
+        }
 
-      Current = Next + 1;
-   }
+        /* Start with this new parent key */
+        hParent = hKey;
+        Current++;
+        KeyName.Buffer = (LPWSTR)Current;
+    }
 
-   return STATUS_UNSUCCESSFUL;
+    return STATUS_UNSUCCESSFUL;
 }
 
 
@@ -1225,41 +1261,6 @@ IopSetDeviceInstanceData(HANDLE InstanceKey,
                            sizeof(DefaultConfigFlags));
   }
 
-#if 0
-  if (DeviceNode->PhysicalDeviceObject != NULL)
-  {
-    /* Create the 'Control' key */
-    RtlInitUnicodeString(&KeyName,
-                        L"Control");
-    InitializeObjectAttributes(&ObjectAttributes,
-                              &KeyName,
-                              OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
-                              InstanceKey,
-                              NULL);
-    Status = ZwCreateKey(&LogConfKey,
-                        KEY_ALL_ACCESS,
-                        &ObjectAttributes,
-                        0,
-                        NULL,
-                        REG_OPTION_VOLATILE,
-                        NULL);
-    if (NT_SUCCESS(Status))
-    {
-      ULONG Reference = (ULONG)DeviceNode->PhysicalDeviceObject;
-      RtlInitUnicodeString(&KeyName,
-                          L"DeviceReference");
-      Status = ZwSetValueKey(LogConfKey,
-                            &KeyName,
-                            0,
-                            REG_DWORD,
-                            &Reference,
-                            sizeof(PVOID));
-
-      ZwClose(LogConfKey);
-    }
-  }
-#endif
-
   DPRINT("IopSetDeviceInstanceData() done\n");
 
   return STATUS_SUCCESS;
@@ -1299,11 +1300,6 @@ IopAssignDeviceResources(
       for (i = 0; i < DeviceNode->BootResources->Count; i++)
       {
          pPartialResourceList = &DeviceNode->BootResources->List[i].PartialResourceList;
-         if (pPartialResourceList->Version != 1 || pPartialResourceList->Revision != 1)
-         {
-            Status = STATUS_REVISION_MISMATCH;
-            goto ByeBye;
-         }
          Size += FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList.PartialDescriptors)
             + pPartialResourceList->Count * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR);
          for (j = 0; j < pPartialResourceList->Count; j++)
@@ -1877,7 +1873,7 @@ IopActionInterrogateDeviceStack(PDEVICE_NODE DeviceNode,
       (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
    wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
    wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
-   Status = IopCreateDeviceKeyPath(KeyBuffer, &InstanceKey);
+   Status = IopCreateDeviceKeyPath(/*KeyBuffer*/&DeviceNode->InstancePath, &InstanceKey);
    ExFreePool(KeyBuffer);
    if (!NT_SUCCESS(Status))
    {
@@ -2657,7 +2653,7 @@ IopInitializePnpServices(IN PDEVICE_NODE DeviceNode,
 static NTSTATUS INIT_FUNCTION
 IopEnumerateDetectedDevices(
    IN HANDLE hBaseKey,
-   IN PUNICODE_STRING RelativePath,
+   IN PUNICODE_STRING RelativePath OPTIONAL,
    IN HANDLE hRootKey,
    IN BOOLEAN EnumerateSubKeys,
    IN PCM_FULL_RESOURCE_DESCRIPTOR ParentBootResources,
@@ -2687,7 +2683,7 @@ IopEnumerateDetectedDevices(
    ULONG BootResourcesLength;
    NTSTATUS Status;
 
-   const UNICODE_STRING IdentifierPci = RTL_CONSTANT_STRING(L"PCI BIOS");
+   const UNICODE_STRING IdentifierPci = RTL_CONSTANT_STRING(L"PCI");
    UNICODE_STRING HardwareIdPci = RTL_CONSTANT_STRING(L"*PNP0A03\0");
    static ULONG DeviceIndexPci = 0;
 #ifdef ENABLE_ACPI
@@ -2704,16 +2700,22 @@ IopEnumerateDetectedDevices(
    const UNICODE_STRING IdentifierMouse = RTL_CONSTANT_STRING(L"PointerController");
    UNICODE_STRING HardwareIdMouse = RTL_CONSTANT_STRING(L"*PNP0F13\0");
    static ULONG DeviceIndexMouse = 0;
+   UNICODE_STRING HardwareIdKey;
    PUNICODE_STRING pHardwareId;
    ULONG DeviceIndex = 0;
 
-   InitializeObjectAttributes(&ObjectAttributes, RelativePath, OBJ_KERNEL_HANDLE, hBaseKey, NULL);
-   Status = ZwOpenKey(&hDevicesKey, KEY_ENUMERATE_SUB_KEYS, &ObjectAttributes);
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
-      goto cleanup;
-   }
+    if (RelativePath)
+    {
+        InitializeObjectAttributes(&ObjectAttributes, RelativePath, OBJ_KERNEL_HANDLE, hBaseKey, NULL);
+        Status = ZwOpenKey(&hDevicesKey, KEY_ENUMERATE_SUB_KEYS, &ObjectAttributes);
+        if (!NT_SUCCESS(Status))
+        {
+            DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
+            goto cleanup;
+        }
+    }
+    else
+        hDevicesKey = hBaseKey;
 
    pDeviceInformation = ExAllocatePool(PagedPool, DeviceInfoLength);
    if (!pDeviceInformation)
@@ -2801,11 +2803,6 @@ IopEnumerateDetectedDevices(
          DPRINT("Wrong registry type: got 0x%lx, expected 0x%lx\n", pValueInformation->Type, REG_FULL_RESOURCE_DESCRIPTOR);
          goto nextdevice;
       }
-      else if (((PCM_FULL_RESOURCE_DESCRIPTOR)pValueInformation->Data)->PartialResourceList.Count == 0)
-      {
-         BootResources = ParentBootResources;
-         BootResourcesLength = ParentBootResourcesLength;
-      }
       else
       {
          static const ULONG Header = FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList.PartialDescriptors);
@@ -2931,17 +2928,17 @@ IopEnumerateDetectedDevices(
             ValueName.Length -= sizeof(WCHAR);
       }
 
-      if (RtlCompareUnicodeString(RelativePath, &IdentifierSerial, FALSE) == 0)
+      if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierSerial, FALSE) == 0)
       {
          pHardwareId = &HardwareIdSerial;
          DeviceIndex = DeviceIndexSerial++;
       }
-      else if (RtlCompareUnicodeString(RelativePath, &IdentifierKeyboard, FALSE) == 0)
+      else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierKeyboard, FALSE) == 0)
       {
          pHardwareId = &HardwareIdKeyboard;
          DeviceIndex = DeviceIndexKeyboard++;
       }
-      else if (RtlCompareUnicodeString(RelativePath, &IdentifierMouse, FALSE) == 0)
+      else if (RelativePath && RtlCompareUnicodeString(RelativePath, &IdentifierMouse, FALSE) == 0)
       {
          pHardwareId = &HardwareIdMouse;
          DeviceIndex = DeviceIndexMouse++;
@@ -2971,12 +2968,16 @@ IopEnumerateDetectedDevices(
       else
       {
          /* Unknown key path */
-         DPRINT("Unknown key path %wZ\n", RelativePath);
+         DPRINT("Unknown key path '%wZ'\n", RelativePath);
          goto nextdevice;
       }
 
+      /* Prepare hardware id key (hardware id value without final \0) */
+      HardwareIdKey = *pHardwareId;
+      HardwareIdKey.Length -= sizeof(UNICODE_NULL);
+
       /* Add the detected device to Root key */
-      InitializeObjectAttributes(&ObjectAttributes, pHardwareId, OBJ_KERNEL_HANDLE, hRootKey, NULL);
+      InitializeObjectAttributes(&ObjectAttributes, &HardwareIdKey, OBJ_KERNEL_HANDLE, hRootKey, NULL);
       Status = ZwCreateKey(
          &hLevel1Key,
          KEY_CREATE_SUB_KEY,
@@ -3007,7 +3008,7 @@ IopEnumerateDetectedDevices(
          DPRINT("ZwCreateKey() failed with status 0x%08lx\n", Status);
          goto nextdevice;
       }
-      DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName, DeviceIndex, pHardwareId);
+      DPRINT("Found %wZ #%lu (%wZ)\n", &ValueName, DeviceIndex, &HardwareIdKey);
       Status = ZwSetValueKey(hLevel2Key, &DeviceDescU, 0, REG_SZ, ValueName.Buffer, ValueName.MaximumLength);
       if (!NT_SUCCESS(Status))
       {
@@ -3070,7 +3071,7 @@ nextdevice:
    Status = STATUS_SUCCESS;
 
 cleanup:
-   if (hDevicesKey)
+   if (hDevicesKey && hDevicesKey != hBaseKey)
       ZwClose(hDevicesKey);
    if (hDeviceKey)
       ZwClose(hDeviceKey);
@@ -3224,8 +3225,7 @@ cleanup:
 static NTSTATUS INIT_FUNCTION
 IopUpdateRootKey(VOID)
 {
-   UNICODE_STRING ControlSetU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet");
-   UNICODE_STRING EnumU = RTL_CONSTANT_STRING(L"Enum");
+   UNICODE_STRING EnumU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\SYSTEM\\CurrentControlSet\\Enum");
    UNICODE_STRING RootPathU = RTL_CONSTANT_STRING(L"Root");
    UNICODE_STRING MultiKeyPathU = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\MultifunctionAdapter");
    UNICODE_STRING DeviceDescU = RTL_CONSTANT_STRING(L"DeviceDesc");
@@ -3236,20 +3236,11 @@ IopUpdateRootKey(VOID)
    UNICODE_STRING HalAcpiDeviceDesc = RTL_CONSTANT_STRING(L"HAL ACPI");
    UNICODE_STRING HalAcpiHardwareID = RTL_CONSTANT_STRING(L"*PNP0C08\0");
    OBJECT_ATTRIBUTES ObjectAttributes;
-   HANDLE hControlSet, hEnum, hRoot, hHalAcpiDevice, hHalAcpiId, hLogConf;
+   HANDLE hEnum, hRoot, hHalAcpiDevice, hHalAcpiId, hLogConf;
    NTSTATUS Status;
 
-   InitializeObjectAttributes(&ObjectAttributes, &ControlSetU, OBJ_KERNEL_HANDLE, NULL, NULL);
-   Status = ZwCreateKey(&hControlSet, KEY_CREATE_SUB_KEY, &ObjectAttributes, 0, NULL, 0, NULL);
-   if (!NT_SUCCESS(Status))
-   {
-      DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status);
-      return Status;
-   }
-
-   InitializeObjectAttributes(&ObjectAttributes, &EnumU, OBJ_KERNEL_HANDLE, hControlSet, NULL);
+   InitializeObjectAttributes(&ObjectAttributes, &EnumU, OBJ_KERNEL_HANDLE, NULL, NULL);
    Status = ZwCreateKey(&hEnum, KEY_CREATE_SUB_KEY, &ObjectAttributes, 0, NULL, 0, NULL);
-   ZwClose(hControlSet);
    if (!NT_SUCCESS(Status))
    {
       DPRINT1("ZwCreateKey() failed with status 0x%08lx\n", Status);
@@ -3292,15 +3283,25 @@ IopUpdateRootKey(VOID)
    }
    else
    {
-      Status = IopEnumerateDetectedDevices(
-         NULL,
-         &MultiKeyPathU,
-         hRoot,
-         TRUE,
-         NULL,
-         0);
-      ZwClose(hRoot);
-      return Status;
+        InitializeObjectAttributes(&ObjectAttributes, &MultiKeyPathU, OBJ_KERNEL_HANDLE, NULL, NULL);
+        Status = ZwOpenKey(&hEnum, KEY_ENUMERATE_SUB_KEYS, &ObjectAttributes);
+        if (!NT_SUCCESS(Status))
+        {
+            /* Nothing to do, don't return with an error status */
+            DPRINT("ZwOpenKey() failed with status 0x%08lx\n", Status);
+            ZwClose(hRoot);
+            return STATUS_SUCCESS;
+        }
+        Status = IopEnumerateDetectedDevices(
+            hEnum,
+            NULL,
+            hRoot,
+            TRUE,
+            NULL,
+            0);
+        ZwClose(hEnum);
+        ZwClose(hRoot);
+        return Status;
    }
 }