Fix a debug message
[reactos.git] / reactos / ntoskrnl / io / pnpmgr.c
index acb0e47..c1fca7e 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: pnpmgr.c,v 1.23 2004/03/12 19:40:29 navaraf Exp $
+/* $Id: pnpmgr.c,v 1.42 2004/10/22 18:34:11 ekohl Exp $
  *
  * COPYRIGHT:      See COPYING in the top level directory
  * PROJECT:        ReactOS kernel
 
 /* INCLUDES ******************************************************************/
 
-#include <ddk/ntddk.h>
-#include <reactos/bugcodes.h>
-#include <internal/io.h>
-#include <internal/po.h>
-#include <internal/ldr.h>
-#include <internal/module.h>
-
+#include <ntoskrnl.h>
 #include <ole32/guiddef.h>
-//#include <ddk/pnpfuncs.h>
 #ifdef DEFINE_GUID
 DEFINE_GUID(GUID_CLASS_COMPORT,          0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, 0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73);
 DEFINE_GUID(GUID_SERENUM_BUS_ENUMERATOR, 0x4D36E978L, 0xE325, 0x11CE, 0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18);
 #endif // DEFINE_GUID
 
-#define NDEBUG
+//#define NDEBUG
 #include <internal/debug.h>
 
 
@@ -62,16 +55,11 @@ IoInvalidateDeviceRelations(
 {
 }
 
-PPNP_BUS_INFORMATION FASTCALL
-IopQueryBusInformation(
+PDEVICE_NODE FASTCALL
+IopGetDeviceNode(
   PDEVICE_OBJECT DeviceObject)
 {
-  IO_STATUS_BLOCK IoStatusBlock;
-  IO_STACK_LOCATION Stack;
-  
-  return NT_SUCCESS(IopInitiatePnpIrp(DeviceObject, &IoStatusBlock,
-    IRP_MN_QUERY_BUS_INFORMATION, &Stack)) ?
-    (PPNP_BUS_INFORMATION)IoStatusBlock.Information : NULL;
+  return DeviceObject->DeviceObjectExtension->DeviceNode;
 }
 
 /*
@@ -86,96 +74,218 @@ IoGetDeviceProperty(
   OUT PVOID PropertyBuffer,
   OUT PULONG ResultLength)
 {
-  PPNP_BUS_INFORMATION BusInformation;
+  PDEVICE_NODE DeviceNode = IopGetDeviceNode(DeviceObject);
+  ULONG Length;
+  PVOID Data;
+  PWSTR Ptr;
 
-  DPRINT("IoGetDeviceProperty called");
+  DPRINT("IoGetDeviceProperty(%x %d)\n", DeviceObject, DeviceProperty);
+
+  if (DeviceNode == NULL)
+    return STATUS_INVALID_DEVICE_REQUEST;
 
-  /*
-   * Used IRPs:
-   *  IRP_MN_QUERY_ID
-   *  IRP_MN_QUERY_BUS_INFORMATION
-   */
   switch (DeviceProperty)
   {
-    /* Complete, untested */
     case DevicePropertyBusNumber:
-      *ResultLength = sizeof(ULONG);
-      if (BufferLength < sizeof(ULONG))
-        return STATUS_BUFFER_TOO_SMALL;
-      BusInformation = IopQueryBusInformation(DeviceObject);
-      if (BusInformation != NULL)
-      {
-        *((ULONG *)PropertyBuffer) = BusInformation->BusNumber;
-        ExFreePool(BusInformation);
-        return STATUS_UNSUCCESSFUL;
-      }
-      return STATUS_SUCCESS;
+      if (DeviceNode->BusInformation == NULL)
+         return STATUS_INVALID_DEVICE_REQUEST;
+      Length = sizeof(ULONG);
+      Data = &DeviceNode->BusInformation->BusNumber;
+      break;
 
     /* Complete, untested */
     case DevicePropertyBusTypeGuid:
+      if (DeviceNode->BusInformation == NULL)
+         return STATUS_INVALID_DEVICE_REQUEST;
       *ResultLength = 39 * sizeof(WCHAR);
       if (BufferLength < (39 * sizeof(WCHAR)))
         return STATUS_BUFFER_TOO_SMALL;
-      BusInformation = IopQueryBusInformation(DeviceObject);
-      if (BusInformation != NULL)
-      {
-        swprintf((PWSTR)PropertyBuffer,
-          L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
-          BusInformation->BusTypeGuid.Data1,
-          BusInformation->BusTypeGuid.Data2,
-          BusInformation->BusTypeGuid.Data3,
-          BusInformation->BusTypeGuid.Data4[0],
-          BusInformation->BusTypeGuid.Data4[1],
-          BusInformation->BusTypeGuid.Data4[2],
-          BusInformation->BusTypeGuid.Data4[3],
-          BusInformation->BusTypeGuid.Data4[4],
-          BusInformation->BusTypeGuid.Data4[5],
-          BusInformation->BusTypeGuid.Data4[6],
-          BusInformation->BusTypeGuid.Data4[7]);
-        ExFreePool(BusInformation);
-        return STATUS_UNSUCCESSFUL;
-      }
+      swprintf((PWSTR)PropertyBuffer,
+        L"{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
+        DeviceNode->BusInformation->BusTypeGuid.Data1,
+        DeviceNode->BusInformation->BusTypeGuid.Data2,
+        DeviceNode->BusInformation->BusTypeGuid.Data3,
+        DeviceNode->BusInformation->BusTypeGuid.Data4[0],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[1],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[2],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[3],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[4],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[5],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[6],
+        DeviceNode->BusInformation->BusTypeGuid.Data4[7]);
       return STATUS_SUCCESS;
 
-    /* Complete, untested */
     case DevicePropertyLegacyBusType:
-      *ResultLength = sizeof(INTERFACE_TYPE);
-      if (BufferLength < sizeof(INTERFACE_TYPE))
-        return STATUS_BUFFER_TOO_SMALL;
-      BusInformation = IopQueryBusInformation(DeviceObject);
-      if (BusInformation != NULL)
-      {
-        RtlCopyMemory(
-          PropertyBuffer,
-          &BusInformation->LegacyBusType,
-          sizeof(INTERFACE_TYPE));
-        ExFreePool(BusInformation);
-        return STATUS_UNSUCCESSFUL;
-      }
-      return STATUS_SUCCESS;
+      if (DeviceNode->BusInformation == NULL)
+         return STATUS_INVALID_DEVICE_REQUEST;
+      Length = sizeof(INTERFACE_TYPE);
+      Data = &DeviceNode->BusInformation->LegacyBusType;
+      break;
 
     case DevicePropertyAddress:
-    case DevicePropertyBootConfiguration:
-    case DevicePropertyBootConfigurationTranslated:
-    case DevicePropertyClassGuid:
+      if (DeviceNode->CapabilityFlags == NULL)
+         return STATUS_INVALID_DEVICE_REQUEST;
+      Length = sizeof(ULONG);
+      Data = &DeviceNode->CapabilityFlags->Address;
+      break;
+
+    case DevicePropertyUINumber:
+      if (DeviceNode->CapabilityFlags == NULL)
+         return STATUS_INVALID_DEVICE_REQUEST;
+      Length = sizeof(ULONG);
+      Data = &DeviceNode->CapabilityFlags->UINumber;
+      break;
+
     case DevicePropertyClassName:
-    case DevicePropertyCompatibleIDs:
-    case DevicePropertyDeviceDescription:
+    case DevicePropertyClassGuid:
     case DevicePropertyDriverKeyName:
-    case DevicePropertyEnumeratorName: 
+    case DevicePropertyManufacturer:
     case DevicePropertyFriendlyName:
     case DevicePropertyHardwareID:
+    case DevicePropertyCompatibleIDs:
+    case DevicePropertyDeviceDescription:
     case DevicePropertyLocationInformation:
-    case DevicePropertyManufacturer:
+      {
+        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:
+            RegistryPropertyName = L"Class"; break;
+          case DevicePropertyClassGuid:
+            RegistryPropertyName = L"ClassGuid"; break;
+          case DevicePropertyDriverKeyName:
+            RegistryPropertyName = L"Driver"; break;
+          case DevicePropertyManufacturer:
+            RegistryPropertyName = L"Mfg"; break;
+          case DevicePropertyFriendlyName:
+            RegistryPropertyName = L"FriendlyName"; break;
+          case DevicePropertyHardwareID:
+            RegistryPropertyName = L"HardwareID"; break;
+          case DevicePropertyCompatibleIDs:
+            RegistryPropertyName = L"CompatibleIDs"; break;
+          case DevicePropertyDeviceDescription:
+            RegistryPropertyName = L"DeviceDesc"; break;
+          case DevicePropertyLocationInformation:
+            RegistryPropertyName = L"LocationInformation"; break;
+          default:
+            RegistryPropertyName = NULL; break;
+        }
+
+        KeyNameBuffer = ExAllocatePool(PagedPool,
+          (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
+       
+       DPRINT1("KeyNameBuffer: %x, value %S\n", 
+               KeyNameBuffer, RegistryPropertyName);
+
+        if (KeyNameBuffer == NULL)
+          return STATUS_INSUFFICIENT_RESOURCES;
+
+        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);
+        ExFreePool(KeyNameBuffer);
+        if (!NT_SUCCESS(Status))
+          return Status;
+
+        RtlInitUnicodeString(&ValueName, RegistryPropertyName);
+        ValueInformationLength = FIELD_OFFSET(KEY_VALUE_PARTIAL_INFORMATION,
+                                 Data[0]) + BufferLength;
+        ValueInformation = ExAllocatePool(PagedPool, ValueInformationLength);
+        if (ValueInformation == NULL)
+        {
+          ZwClose(KeyHandle);
+          return STATUS_INSUFFICIENT_RESOURCES;
+        }
+
+        Status = ZwQueryValueKey(KeyHandle, &ValueName,
+                                 KeyValuePartialInformation, ValueInformation,
+                                 ValueInformationLength,
+                                 &ValueInformationLength);
+        *ResultLength = ValueInformation->DataLength;
+        ZwClose(KeyHandle);
+
+        if (ValueInformation->DataLength > BufferLength)
+          Status = STATUS_BUFFER_TOO_SMALL;
+
+        if (!NT_SUCCESS(Status))
+        {
+          ExFreePool(ValueInformation);
+          return Status;
+        }
+
+        /* FIXME: Verify the value (NULL-terminated, correct format). */
+
+        RtlCopyMemory(PropertyBuffer, ValueInformation->Data,
+                      ValueInformation->DataLength);
+        ExFreePool(ValueInformation);
+
+        return STATUS_SUCCESS;
+      }
+
+    case DevicePropertyBootConfiguration:
+      Length = 0;
+      if (DeviceNode->BootResourceList->Count != 0)
+      {
+       Length = sizeof(CM_RESOURCE_LIST) +
+                ((DeviceNode->BootResourceList->Count - 1) * sizeof(CM_FULL_RESOURCE_DESCRIPTOR));
+      }
+      Data = &DeviceNode->BootResourceList;
+      break;
+
+    /* FIXME: use a translated boot configuration instead */
+    case DevicePropertyBootConfigurationTranslated:
+      Length = 0;
+      if (DeviceNode->BootResourceList->Count != 0)
+      {
+       Length = sizeof(CM_RESOURCE_LIST) +
+                ((DeviceNode->BootResourceList->Count - 1) * sizeof(CM_FULL_RESOURCE_DESCRIPTOR));
+      }
+      Data = &DeviceNode->BootResourceList;
+      break;
+
+    case DevicePropertyEnumeratorName:
+      Ptr = wcschr(DeviceNode->InstancePath.Buffer, L'\\');
+      if (Ptr != NULL)
+      {
+       Length = (ULONG)((ULONG_PTR)Ptr - (ULONG_PTR)DeviceNode->InstancePath.Buffer) + sizeof(WCHAR);
+      }
+      else
+      {
+       Length = 0;
+       Data = NULL;
+      }
+
     case DevicePropertyPhysicalDeviceObjectName:
-    case DevicePropertyUINumber:
-       break;
+      return STATUS_NOT_IMPLEMENTED;
 
     default:
-       return STATUS_INVALID_PARAMETER_2;
+      return STATUS_INVALID_PARAMETER_2;
   }
 
-  return STATUS_NOT_IMPLEMENTED;
+  *ResultLength = Length;
+  if (BufferLength < Length)
+    return STATUS_BUFFER_TOO_SMALL;
+  RtlCopyMemory(PropertyBuffer, Data, Length);
+
+  /* Terminate the string */
+  if (DeviceProperty == DevicePropertyEnumeratorName)
+  {
+    Ptr = (PWSTR)PropertyBuffer;
+    Ptr[(Length / sizeof(WCHAR)) - 1] = 0;
+  }
+
+  return STATUS_SUCCESS;
 }
 
 /*
@@ -199,6 +309,47 @@ IoOpenDeviceRegistryKey(
   IN ACCESS_MASK DesiredAccess,
   OUT PHANDLE DevInstRegKey)
 {
+  static const WCHAR ClassKeyName[] = {
+    '\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
+    'S','y','s','t','e','m','\\','C','u','r','r','e','n','t','C','o','n','t',
+    'r','o','l','S','e','t','\\','C','o','n','t','r','o','l','\\',
+    'C','l','a','s','s','\\'};
+  LPWSTR KeyNameBuffer;
+  UNICODE_STRING KeyName;
+  ULONG DriverKeyLength;
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  NTSTATUS Status;
+
+  if (DevInstKeyType == PLUGPLAY_REGKEY_DRIVER)
+  {
+    Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
+                                 0, NULL, &DriverKeyLength);
+    if (Status != STATUS_BUFFER_TOO_SMALL)
+      return Status;
+  
+    KeyNameBuffer = ExAllocatePool(PagedPool, DriverKeyLength + sizeof(ClassKeyName));
+    if (KeyNameBuffer == NULL)
+      return STATUS_INSUFFICIENT_RESOURCES;
+
+    RtlCopyMemory(KeyNameBuffer, ClassKeyName, sizeof(ClassKeyName));
+    Status = IoGetDeviceProperty(DeviceObject, DevicePropertyDriverKeyName,
+                                 DriverKeyLength, KeyNameBuffer +
+                                 (sizeof(ClassKeyName) / sizeof(WCHAR)),
+                                 &DriverKeyLength);
+    if (!NT_SUCCESS(Status))
+    {
+      ExFreePool(KeyNameBuffer);
+      return Status;
+    }
+
+    RtlInitUnicodeString(&KeyName, KeyNameBuffer);
+    InitializeObjectAttributes(&ObjectAttributes, &KeyName,
+                               OBJ_CASE_INSENSITIVE, NULL, NULL);
+    Status = ZwOpenKey(DevInstRegKey, DesiredAccess, &ObjectAttributes);
+    ExFreePool(KeyNameBuffer);
+    return Status;
+  }
+
   return STATUS_NOT_IMPLEMENTED;
 }
 
@@ -208,8 +359,10 @@ IoOpenDeviceRegistryKey(
 VOID
 STDCALL
 IoRequestDeviceEject(
-  IN PDEVICE_OBJECT PhysicalDeviceObject)
+    IN PDEVICE_OBJECT PhysicalDeviceObject
+    )
 {
+       UNIMPLEMENTED;
 }
 
 
@@ -250,13 +403,16 @@ IopGetSystemPowerDeviceObject(PDEVICE_OBJECT *DeviceObject)
 {
   KIRQL OldIrql;
 
-  assert(PopSystemPowerDeviceNode);
+  if (PopSystemPowerDeviceNode)
+  {
+    KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
+    *DeviceObject = PopSystemPowerDeviceNode->Pdo;
+    KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
 
-  KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
-  *DeviceObject = PopSystemPowerDeviceNode->Pdo;
-  KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
+    return STATUS_SUCCESS;
+  }
 
-  return STATUS_SUCCESS;
+  return STATUS_UNSUCCESSFUL;
 }
 
 /**********************************************************************
@@ -309,6 +465,8 @@ IopCreateDeviceNode(PDEVICE_NODE ParentNode,
 
   Node->Pdo = PhysicalDeviceObject;
 
+  PhysicalDeviceObject->DeviceObjectExtension->DeviceNode = Node;
+
   if (ParentNode)
     {
       KeAcquireSpinLock(&IopDeviceTreeLock, &OldIrql);
@@ -316,7 +474,7 @@ IopCreateDeviceNode(PDEVICE_NODE ParentNode,
       Node->NextSibling = ParentNode->Child;
       if (ParentNode->Child != NULL)
        {
-         ParentNode->Child->PrevSibling = Node;          
+         ParentNode->Child->PrevSibling = Node;
        }
       ParentNode->Child = Node;
       KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
@@ -344,21 +502,21 @@ IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
   /* Unlink from parent if it exists */
 
   if ((DeviceNode->Parent) && (DeviceNode->Parent->Child == DeviceNode))
-    {
-      DeviceNode->Parent->Child = DeviceNode->NextSibling;
-    }
+  {
+    DeviceNode->Parent->Child = DeviceNode->NextSibling;
+  }
 
   /* Unlink from sibling list */
 
   if (DeviceNode->PrevSibling)
-    {
-      DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
-    }
+  {
+    DeviceNode->PrevSibling->NextSibling = DeviceNode->NextSibling;
+  }
 
   if (DeviceNode->NextSibling)
-    {
-  DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
-    }
+  {
+    DeviceNode->NextSibling->PrevSibling = DeviceNode->PrevSibling;
+  }
 
   KeReleaseSpinLock(&IopDeviceTreeLock, OldIrql);
 
@@ -367,41 +525,29 @@ IopFreeDeviceNode(PDEVICE_NODE DeviceNode)
   RtlFreeUnicodeString(&DeviceNode->ServiceName);
 
   if (DeviceNode->CapabilityFlags)
-    {
-  ExFreePool(DeviceNode->CapabilityFlags);
-    }
+  {
+    ExFreePool(DeviceNode->CapabilityFlags);
+  }
 
   if (DeviceNode->CmResourceList)
-    {
-  ExFreePool(DeviceNode->CmResourceList);
-    }
+  {
+    ExFreePool(DeviceNode->CmResourceList);
+  }
 
-  if (DeviceNode->BootResourcesList)
-    {
-  ExFreePool(DeviceNode->BootResourcesList);
-    }
+  if (DeviceNode->BootResourceList)
+  {
+    ExFreePool(DeviceNode->BootResourceList);
+  }
 
   if (DeviceNode->ResourceRequirementsList)
-    {
-  ExFreePool(DeviceNode->ResourceRequirementsList);
-    }
-
-  RtlFreeUnicodeString(&DeviceNode->DeviceID);
-
-  RtlFreeUnicodeString(&DeviceNode->InstanceID);
-
-  RtlFreeUnicodeString(&DeviceNode->HardwareIDs);
-
-  RtlFreeUnicodeString(&DeviceNode->CompatibleIDs);
-
-  RtlFreeUnicodeString(&DeviceNode->DeviceText);
-
-  RtlFreeUnicodeString(&DeviceNode->DeviceTextLocation);
+  {
+    ExFreePool(DeviceNode->ResourceRequirementsList);
+  }
 
   if (DeviceNode->BusInformation)
-    {
-  ExFreePool(DeviceNode->BusInformation);
-    }
+  {
+    ExFreePool(DeviceNode->BusInformation);
+  }
 
   ExFreePool(DeviceNode);
 
@@ -426,22 +572,22 @@ IopInitiatePnpIrp(
 
   KeInitializeEvent(
     &Event,
-         NotificationEvent,
-         FALSE);
-
-  /* PNP IRPs are always initialized with a status code of
-     STATUS_NOT_IMPLEMENTED */
-  IoStatusBlock->Status = STATUS_NOT_IMPLEMENTED;
-  IoStatusBlock->Information = 0;
+    NotificationEvent,
+    FALSE);
 
   Irp = IoBuildSynchronousFsdRequest(
     IRP_MJ_PNP,
     TopDeviceObject,
-         NULL,
-         0,
-         NULL,
-         &Event,
-         IoStatusBlock);
+    NULL,
+    0,
+    NULL,
+    &Event,
+    IoStatusBlock);
+
+  /* PNP IRPs are always initialized with a status code of
+     STATUS_NOT_IMPLEMENTED */
+  Irp->IoStatus.Status = STATUS_NOT_IMPLEMENTED;
+  Irp->IoStatus.Information = 0;
 
   IrpSp = IoGetNextIrpStackLocation(Irp);
   IrpSp->MinorFunction = MinorFunction;
@@ -454,15 +600,15 @@ IopInitiatePnpIrp(
       sizeof(Stack->Parameters));
   }
 
-       Status = IoCallDriver(TopDeviceObject, Irp);
-       if (Status == STATUS_PENDING)
-         {
-                 KeWaitForSingleObject(
+  Status = IoCallDriver(TopDeviceObject, Irp);
+  if (Status == STATUS_PENDING)
+    {
+      KeWaitForSingleObject(
         &Event,
         Executive,
-                   KernelMode,
-                   FALSE,
-                   NULL);
+        KernelMode,
+        FALSE,
+        NULL);
       Status = IoStatusBlock->Status;
     }
 
@@ -477,7 +623,7 @@ IopQueryCapabilities(
   PDEVICE_OBJECT Pdo,
   PDEVICE_CAPABILITIES *Capabilities)
 {
-  IO_STATUS_BLOCK      IoStatusBlock;
+  IO_STATUS_BLOCK IoStatusBlock;
   PDEVICE_CAPABILITIES Caps;
   IO_STACK_LOCATION Stack;
   NTSTATUS Status;
@@ -583,7 +729,8 @@ IopTraverseDeviceTree(
 
 
 static NTSTATUS
-IopCreateDeviceKeyPath(PWSTR Path)
+IopCreateDeviceKeyPath(PWSTR Path,
+                      PHANDLE Handle)
 {
   OBJECT_ATTRIBUTES ObjectAttributes;
   WCHAR KeyBuffer[MAX_PATH];
@@ -593,6 +740,8 @@ IopCreateDeviceKeyPath(PWSTR Path)
   PWCHAR Current;
   PWCHAR Next;
 
+  *Handle = NULL;
+
   if (_wcsnicmp(Path, L"\\Registry\\", 10) != 0)
     {
       return STATUS_INVALID_PARAMETER;
@@ -606,8 +755,8 @@ IopCreateDeviceKeyPath(PWSTR Path)
   Current = wcschr (Current, '\\') + 1;
   Current = wcschr (Current, '\\') + 1;
 
-  do
-   {
+  while (TRUE)
+    {
       Next = wcschr (Current, '\\');
       if (Next == NULL)
        {
@@ -639,16 +788,92 @@ IopCreateDeviceKeyPath(PWSTR Path)
          return Status;
        }
 
-      NtClose (KeyHandle);
-
-      if (Next != NULL)
+      if (Next == NULL)
+       {
+         *Handle = KeyHandle;
+         return STATUS_SUCCESS;
+       }
+      else
        {
+         NtClose (KeyHandle);
          *Next = L'\\';
        }
 
       Current = Next + 1;
     }
-   while (Next != NULL);
+
+  return STATUS_UNSUCCESSFUL;
+}
+
+
+static NTSTATUS
+IopSetDeviceInstanceData(HANDLE InstanceKey,
+                        PDEVICE_NODE DeviceNode)
+{
+  OBJECT_ATTRIBUTES ObjectAttributes;
+  UNICODE_STRING KeyName;
+  HANDLE LogConfKey;
+  ULONG ResCount;
+  ULONG ListSize;
+  NTSTATUS Status;
+
+  DPRINT("IopSetDeviceInstanceData() called\n");
+
+  /* Create the 'LogConf' key */
+  RtlInitUnicodeString(&KeyName,
+                      L"LogConf");
+  InitializeObjectAttributes(&ObjectAttributes,
+                            &KeyName,
+                            OBJ_CASE_INSENSITIVE,
+                            InstanceKey,
+                            NULL);
+  Status = NtCreateKey(&LogConfKey,
+                      KEY_ALL_ACCESS,
+                      &ObjectAttributes,
+                      0,
+                      NULL,
+                      0,
+                      NULL);
+  if (NT_SUCCESS(Status))
+  {
+    /* Set 'BootConfig' value */
+    if (DeviceNode->BootResourceList != NULL)
+    {
+      ResCount = DeviceNode->BootResourceList->Count;
+      if (ResCount != 0)
+      {
+       ListSize = sizeof(CM_RESOURCE_LIST) +
+                  ((ResCount - 1) * sizeof(CM_FULL_RESOURCE_DESCRIPTOR));
+
+       RtlInitUnicodeString(&KeyName,
+                            L"BootConfig");
+       Status = NtSetValueKey(LogConfKey,
+                              &KeyName,
+                              0,
+                              REG_RESOURCE_LIST,
+                              &DeviceNode->BootResourceList,
+                              ListSize);
+      }
+    }
+
+    /* Set 'BasicConfigVector' value */
+    if (DeviceNode->ResourceRequirementsList != NULL &&
+       DeviceNode->ResourceRequirementsList->ListSize != 0)
+    {
+      RtlInitUnicodeString(&KeyName,
+                          L"BasicConfigVector");
+      Status = NtSetValueKey(LogConfKey,
+                            &KeyName,
+                            0,
+                            REG_RESOURCE_REQUIREMENTS_LIST,
+                            &DeviceNode->ResourceRequirementsList,
+                            DeviceNode->ResourceRequirementsList->ListSize);
+    }
+
+    NtClose(LogConfKey);
+  }
+
+  DPRINT("IopSetDeviceInstanceData() done\n");
 
   return STATUS_SUCCESS;
 }
@@ -684,6 +909,11 @@ IopActionInterrogateDeviceStack(
    IO_STACK_LOCATION Stack;
    NTSTATUS Status;
    PWSTR KeyBuffer;
+   PWSTR Ptr;
+   USHORT Length;
+   USHORT TotalLength;
+   HANDLE InstanceKey = NULL;
+   UNICODE_STRING ValueName;
 
    DPRINT("IopActionInterrogateDeviceStack(%p, %p)\n", DeviceNode, Context);
    DPRINT("PDO %x\n", DeviceNode->Pdo);
@@ -728,9 +958,8 @@ IopActionInterrogateDeviceStack(
       &Stack);
    if (NT_SUCCESS(Status))
    {
-      RtlInitUnicodeString(
-         &DeviceNode->DeviceID,
-         (LPWSTR)IoStatusBlock.Information);
+      /* Copy the device id string */
+      wcscpy(InstancePath, (PWSTR)IoStatusBlock.Information);
 
       /*
        * FIXME: Check for valid characters, if there is invalid characters
@@ -740,7 +969,6 @@ IopActionInterrogateDeviceStack(
    else
    {
       DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
-      RtlInitUnicodeString(&DeviceNode->DeviceID, NULL);
    }
 
    DPRINT("Sending IRP_MN_QUERY_ID.BusQueryInstanceID to device stack\n");
@@ -753,9 +981,9 @@ IopActionInterrogateDeviceStack(
       &Stack);
    if (NT_SUCCESS(Status))
    {
-      RtlInitUnicodeString(
-      &DeviceNode->InstanceID,
-      (LPWSTR)IoStatusBlock.Information);
+      /* Append the instance id string */
+      wcscat(InstancePath, L"\\");
+      wcscat(InstancePath, (PWSTR)IoStatusBlock.Information);
 
       /*
        * FIXME: Check for valid characters, if there is invalid characters
@@ -765,12 +993,8 @@ IopActionInterrogateDeviceStack(
    else
    {
       DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
-      RtlInitUnicodeString(&DeviceNode->InstanceID, NULL);
    }
 
-   /* FIXME: SEND IRP_QUERY_ID.BusQueryHardwareIDs */
-   /* FIXME: SEND IRP_QUERY_ID.BusQueryCompatibleIDs */
-
    Status = IopQueryCapabilities(DeviceNode->Pdo, &DeviceNode->CapabilityFlags);
    if (NT_SUCCESS(Status))
    {
@@ -779,6 +1003,156 @@ IopActionInterrogateDeviceStack(
    {
    }
 
+   if (!DeviceNode->CapabilityFlags->UniqueID)
+   {
+      DPRINT("Instance ID is not unique\n");
+      /* FIXME: Add information from parent bus driver to InstancePath */
+   }
+
+   if (!IopCreateUnicodeString(&DeviceNode->InstancePath, InstancePath, PagedPool))
+   {
+      DPRINT("No resources\n");
+      /* FIXME: Cleanup and disable device */
+   }
+
+   DPRINT1("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
+
+   /*
+    * Create registry key for the instance id, if it doesn't exist yet
+    */
+   KeyBuffer = ExAllocatePool(
+      PagedPool,
+      (49 * sizeof(WCHAR)) + DeviceNode->InstancePath.Length);
+   wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
+   wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);
+   Status = IopCreateDeviceKeyPath(KeyBuffer,
+                                  &InstanceKey);
+   ExFreePool(KeyBuffer);
+   if (!NT_SUCCESS(Status))
+   {
+      DPRINT1("Failed to create the instance key! (Status %lx)\n", Status);
+   }
+
+
+   if (DeviceNode->CapabilityFlags != NULL)
+   {
+      /* Set 'Capabilities' value */
+      RtlInitUnicodeString(&ValueName,
+                          L"Capabilities");
+      Status = NtSetValueKey(InstanceKey,
+                            &ValueName,
+                            0,
+                            REG_DWORD,
+                            (PVOID)((ULONG_PTR)&DeviceNode->CapabilityFlags + 4),
+                            sizeof(ULONG));
+
+      /* Set 'UINumber' value */
+      if (DeviceNode->CapabilityFlags->UINumber != (ULONG)-1)
+      {
+         RtlInitUnicodeString(&ValueName,
+                             L"UINumber");
+         Status = NtSetValueKey(InstanceKey,
+                               &ValueName,
+                               0,
+                               REG_DWORD,
+                               &DeviceNode->CapabilityFlags->UINumber,
+                               sizeof(ULONG));
+      }
+   }
+
+   DPRINT("Sending IRP_MN_QUERY_ID.BusQueryHardwareIDs to device stack\n");
+
+   Stack.Parameters.QueryId.IdType = BusQueryHardwareIDs;
+   Status = IopInitiatePnpIrp(
+      DeviceNode->Pdo,
+      &IoStatusBlock,
+      IRP_MN_QUERY_ID,
+      &Stack);
+   if (NT_SUCCESS(Status))
+   {
+      /*
+       * FIXME: Check for valid characters, if there is invalid characters
+       * then bugcheck.
+       */
+      TotalLength = 0;
+      Ptr = (PWSTR)IoStatusBlock.Information;
+      DPRINT("Hardware IDs:\n");
+      while (*Ptr)
+      {
+       DPRINT("  %S\n", Ptr);
+       Length = wcslen(Ptr) + 1;
+
+       Ptr += Length;
+       TotalLength += Length;
+      }
+      DPRINT("TotalLength: %hu\n", TotalLength);
+      DPRINT("\n");
+
+      RtlInitUnicodeString(&ValueName,
+                          L"HardwareID");
+      Status = NtSetValueKey(InstanceKey,
+                            &ValueName,
+                            0,
+                            REG_MULTI_SZ,
+                            (PVOID)IoStatusBlock.Information,
+                            (TotalLength + 1) * sizeof(WCHAR));
+      if (!NT_SUCCESS(Status))
+       {
+          DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
+       }
+   }
+   else
+   {
+      DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
+   }
+
+   DPRINT("Sending IRP_MN_QUERY_ID.BusQueryCompatibleIDs to device stack\n");
+
+   Stack.Parameters.QueryId.IdType = BusQueryCompatibleIDs;
+   Status = IopInitiatePnpIrp(
+      DeviceNode->Pdo,
+      &IoStatusBlock,
+      IRP_MN_QUERY_ID,
+      &Stack);
+   if (NT_SUCCESS(Status))
+   {
+      /*
+       * FIXME: Check for valid characters, if there is invalid characters
+       * then bugcheck.
+       */
+      TotalLength = 0;
+      Ptr = (PWSTR)IoStatusBlock.Information;
+      DPRINT("Compatible IDs:\n");
+      while (*Ptr)
+      {
+       DPRINT("  %S\n", Ptr);
+       Length = wcslen(Ptr) + 1;
+
+       Ptr += Length;
+       TotalLength += Length;
+      }
+      DPRINT("TotalLength: %hu\n", TotalLength);
+      DPRINT("\n");
+
+      RtlInitUnicodeString(&ValueName,
+                          L"CompatibleIDs");
+      Status = NtSetValueKey(InstanceKey,
+                            &ValueName,
+                            0,
+                            REG_MULTI_SZ,
+                            (PVOID)IoStatusBlock.Information,
+                            (TotalLength + 1) * sizeof(WCHAR));
+      if (!NT_SUCCESS(Status))
+       {
+          DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
+       }
+   }
+   else
+   {
+      DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
+   }
+
+
    DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextDescription to device stack\n");
 
    Stack.Parameters.QueryDeviceText.DeviceTextType = DeviceTextDescription;
@@ -790,14 +1164,22 @@ IopActionInterrogateDeviceStack(
       &Stack);
    if (NT_SUCCESS(Status))
    {
-      RtlInitUnicodeString(
-         &DeviceNode->DeviceText,
-         (LPWSTR)IoStatusBlock.Information);
+      RtlInitUnicodeString(&ValueName,
+                          L"DeviceDesc");
+      Status = NtSetValueKey(InstanceKey,
+                            &ValueName,
+                            0,
+                            REG_SZ,
+                            (PVOID)IoStatusBlock.Information,
+                            (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
+      if (!NT_SUCCESS(Status))
+       {
+          DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
+       }
    }
    else
    {
       DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
-      RtlInitUnicodeString(&DeviceNode->DeviceText, NULL);
    }
 
    DPRINT("Sending IRP_MN_QUERY_DEVICE_TEXT.DeviceTextLocation to device stack\n");
@@ -811,14 +1193,23 @@ IopActionInterrogateDeviceStack(
       &Stack);
    if (NT_SUCCESS(Status))
    {
-      RtlInitUnicodeString(
-         &DeviceNode->DeviceTextLocation,
-         (LPWSTR)IoStatusBlock.Information);
+      DPRINT("LocationInformation: %S\n", (PWSTR)IoStatusBlock.Information);
+      RtlInitUnicodeString(&ValueName,
+                          L"LocationInformation");
+      Status = NtSetValueKey(InstanceKey,
+                            &ValueName,
+                            0,
+                            REG_SZ,
+                            (PVOID)IoStatusBlock.Information,
+                            (wcslen((PWSTR)IoStatusBlock.Information) + 1) * sizeof(WCHAR));
+      if (!NT_SUCCESS(Status))
+       {
+          DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status);
+       }
    }
    else
    {
       DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
-      RtlInitUnicodeString(&DeviceNode->DeviceTextLocation, NULL);
    }
 
    DPRINT("Sending IRP_MN_QUERY_BUS_INFORMATION to device stack\n");
@@ -848,14 +1239,14 @@ IopActionInterrogateDeviceStack(
       NULL);
    if (NT_SUCCESS(Status))
    {
-      DeviceNode->BootResourcesList =
+      DeviceNode->BootResourceList =
          (PCM_RESOURCE_LIST)IoStatusBlock.Information;
       DeviceNode->Flags |= DNF_HAS_BOOT_CONFIG;
    }
    else
    {
       DPRINT("IopInitiatePnpIrp() failed (Status %x)\n", Status);
-      DeviceNode->BootResourcesList = NULL;
+      DeviceNode->BootResourceList = NULL;
    }
 
    DPRINT("Sending IRP_MN_QUERY_RESOURCE_REQUIREMENTS to device stack\n");
@@ -876,37 +1267,14 @@ IopActionInterrogateDeviceStack(
       DeviceNode->ResourceRequirementsList = NULL;
    }
 
-   /*
-    * Assemble the instance path for the device
-    */
-
-   wcscpy(InstancePath, DeviceNode->DeviceID.Buffer);
-   wcscat(InstancePath, L"\\");
-   wcscat(InstancePath, DeviceNode->InstanceID.Buffer);
-
-   if (!DeviceNode->CapabilityFlags->UniqueID)
-   {
-      DPRINT("Instance ID is not unique\n");
-      /* FIXME: Add information from parent bus driver to InstancePath */
-   }
 
-   if (!IopCreateUnicodeString(&DeviceNode->InstancePath, InstancePath, PagedPool))
+   if (InstanceKey != NULL)
    {
-      DPRINT("No resources\n");
-      /* FIXME: Cleanup and disable device */
+      IopSetDeviceInstanceData(InstanceKey, DeviceNode);
    }
 
-   DPRINT("InstancePath is %S\n", DeviceNode->InstancePath.Buffer);
+   NtClose(InstanceKey);
 
-   /*
-    * Create registry key for the instance id, if it doesn't exist yet
-    */  
-
-   KeyBuffer = ExAllocatePool(PagedPool, (49 + DeviceNode->InstancePath.Length) * sizeof(WCHAR));
-   wcscpy(KeyBuffer, L"\\Registry\\Machine\\System\\CurrentControlSet\\Enum\\");
-   wcscat(KeyBuffer, DeviceNode->InstancePath.Buffer);  
-   IopCreateDeviceKeyPath(KeyBuffer);
-   ExFreePool(KeyBuffer);
    DeviceNode->Flags |= DNF_PROCESSED;
 
    return STATUS_SUCCESS;
@@ -1071,11 +1439,28 @@ IopActionInitChildServices(
        !IopDeviceNodeHasFlag(DeviceNode, DNF_ADDED) &&
        !IopDeviceNodeHasFlag(DeviceNode, DNF_STARTED))
    {
-      Status = IopInitializeDeviceNodeService(DeviceNode, BootDrivers);
+      PMODULE_OBJECT ModuleObject;
+      PDRIVER_OBJECT DriverObject;
+
+      Status = IopLoadServiceModule(&DeviceNode->ServiceName, &ModuleObject);
       if (NT_SUCCESS(Status))
       {
-         IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
-      } else
+         Status = IopInitializeDriverModule(DeviceNode, ModuleObject, FALSE, &DriverObject);
+         if (NT_SUCCESS(Status))
+         {
+            /* Attach lower level filter drivers. */
+            IopAttachFilterDrivers(DeviceNode, TRUE);
+            /* Initialize the function driver for the device node */
+            Status = IopInitializeDevice(DeviceNode, DriverObject);
+            if (NT_SUCCESS(Status))
+            {
+               IopDeviceNodeSetFlag(DeviceNode, DNF_STARTED);
+               /* Attach upper level filter drivers. */
+               IopAttachFilterDrivers(DeviceNode, FALSE);
+            }
+         }
+      }
+      else
       {
          /*
           * Don't disable when trying to load only boot drivers
@@ -1137,6 +1522,7 @@ IopActionInitBootServices(
  * Parameters
  *    DeviceNode
  *       Top device node to start initializing services.
+ *
  *    BootDrivers
  *       When set to TRUE, only drivers marked as boot start will
  *       be loaded. Otherwise, all drivers will be loaded.
@@ -1176,15 +1562,18 @@ IopInitializePnpServices(
 
 NTSTATUS
 IopInvalidateDeviceRelations(
-  IN PDEVICE_NODE DeviceNode,
-  IN DEVICE_RELATION_TYPE Type,
-  IN BOOLEAN BootDriver)
+   IN PDEVICE_NODE DeviceNode,
+   IN DEVICE_RELATION_TYPE Type)
 {
    DEVICETREE_TRAVERSE_CONTEXT Context;
    PDEVICE_RELATIONS DeviceRelations;
    IO_STATUS_BLOCK IoStatusBlock;
    PDEVICE_NODE ChildDeviceNode;
    IO_STACK_LOCATION Stack;
+   BOOL BootDrivers;
+   OBJECT_ATTRIBUTES ObjectAttributes;
+   UNICODE_STRING LinkName;
+   HANDLE Handle;
    NTSTATUS Status;
    ULONG i;
 
@@ -1275,12 +1664,39 @@ IopInvalidateDeviceRelations(
       return Status;
    }
 
+   /*
+    * Get the state of the system boot. If the \\SystemRoot link isn't
+    * created yet, we will assume that it's possible to load only boot
+    * drivers.
+    */
+
+   RtlInitUnicodeString(&LinkName, L"\\SystemRoot");
+
+   InitializeObjectAttributes(
+      &ObjectAttributes,
+      &LinkName,
+      0,
+      NULL,
+      NULL);
+
+   Status = NtOpenFile(
+      &Handle,
+      FILE_ALL_ACCESS,
+      &ObjectAttributes,
+      &IoStatusBlock,
+      0,
+      0);
+   BootDrivers = NT_SUCCESS(Status) ? FALSE : TRUE;
+
+   NtClose(Handle);
+
    /*
     * Initialize services for discovered children. Only boot drivers will
     * be loaded from boot driver!
     */
 
-   Status = IopInitializePnpServices(DeviceNode, BootDriver);
+   Status = IopInitializePnpServices(DeviceNode, BootDrivers);
    if (!NT_SUCCESS(Status))
    {
       DPRINT("IopInitializePnpServices() failed with status (%x)\n", Status);
@@ -1290,6 +1706,7 @@ IopInvalidateDeviceRelations(
    return STATUS_SUCCESS;
 }
 
+
 VOID INIT_FUNCTION
 PnpInit(VOID)
 {
@@ -1308,7 +1725,7 @@ PnpInit(VOID)
    if (!NT_SUCCESS(Status))
    {
       CPRINT("IoCreateDriverObject() failed\n");
-      KEBUGCHECK(PHASE1_INITIALIZATION_FAILED);
+      KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
    }
 
    Status = IoCreateDevice(IopRootDriverObject, 0, NULL, FILE_DEVICE_CONTROLLER,
@@ -1316,17 +1733,17 @@ PnpInit(VOID)
    if (!NT_SUCCESS(Status))
    {
       CPRINT("IoCreateDevice() failed\n");
-      KEBUGCHECK(PHASE1_INITIALIZATION_FAILED);
+      KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
    }
 
    Status = IopCreateDeviceNode(NULL, Pdo, &IopRootDeviceNode);
    if (!NT_SUCCESS(Status))
    {
       CPRINT("Insufficient resources\n");
-      KEBUGCHECK(PHASE1_INITIALIZATION_FAILED);
+      KEBUGCHECKEX(PHASE1_INITIALIZATION_FAILED, Status, 0, 0, 0);
    }
+
    IopRootDeviceNode->Pdo->Flags |= DO_BUS_ENUMERATED_DEVICE;
-   IopRootDeviceNode->DriverObject = IopRootDriverObject;
    PnpRootDriverEntry(IopRootDriverObject, NULL);
    IopRootDriverObject->DriverExtension->AddDevice(
       IopRootDriverObject,