PiInitializeDevice

INT64 __stdcall PiInitializeDevice(PCUNICODE_STRING SourceString, INT64 a2){
  WCHAR *PoolWithTag; 
  _ETHREAD *CurrentThread; 
  _QWORD *v5; 
  int DeviceInstancePath; 
  wchar_t *v7; 
  unsigned __int16 Length; 
  int v10; 
  _DEVICE_OBJECT *v11; 
  int v12; 
  _DEVICE_NODE *v13; 
  _PNP_DEVNODE_STATE *v14; 
  __int16 v15; 
  int FailedInstallProblemStatus; 
  int v17; 
  UNICODE_STRING *v18; 
  _UNICODE_STRING *v19; 
  UINT64 v20; 
  int v21; 
  __int64 v22; 
  __int64 v23; 
  __int64 v24; 
  INT64 v25; 
  INT64 v26; 
  INT64 v27; 
  HANDLE Handle; 
  PWCHAR InFreeString; 
  PVOID Object; 
  _DEVICE_NODE *DeviceNode; 
  PVOID P; 
  struct _UNICODE_STRING DestinationString; 
  char v34; 
  int v35; 
  INT64 v36; 
  LODWORD(v27) = 0;
  PoolWithTag = 0i64;
  v35 = 0;
  LODWORD(v36) = 0;
  *(_DWORD *)(&DestinationString.MaximumLength + 1) = 0;
  P = 0i64;
  InFreeString = 0i64;
  DeviceNode = 0i64;
  Handle = 0i64;
  PpDevNodeLockTree(1);
  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  --*((_WORD *)CurrentThread + 242);
  ExAcquireResourceExclusiveLite(&PnpRegistryDeviceResource, 1u);
  v34 = 1;
  v5 = PnpDeviceObjectFromDeviceInstanceWithTag((INT64)SourceString, 0x43706E50ui64);
  Object = v5;
  if( v5 )
  {
    ObfDereferenceObjectWithTag(v5, 0x43706E50ui64);
    DeviceInstancePath = 0;
LABEL_3:
    v7 = InFreeString;
LABEL_4:
    ExReleaseResourceLite(&PnpRegistryDeviceResource);
    KeLeaveCriticalRegion();
    goto LABEL_5;
  }
  DeviceInstancePath = PnpUnicodeStringToWstr(&InFreeString, 0i64, (UNICODE_STRING *)SourceString);
  if( DeviceInstancePath < 0 )
    goto LABEL_3;
  DeviceInstancePath = PiPnpRtlBeginOperation((__int64 **)&P);
  if( DeviceInstancePath < 0 )
    goto LABEL_3;
  v7 = InFreeString;
  DeviceInstancePath = CmOpenDeviceRegKey(PiPnpRtlCtx, InFreeString, 16, 0, 983103, 0, (__int64)&Handle, 0i64);
  if( DeviceInstancePath < 0 )
    goto LABEL_4;
  *(_DWORD *)&DestinationString.Length = 0;
  v35 = 512;
  DestinationString.Buffer = 0i64;
  Length = 0;
  PoolWithTag = (WCHAR *)ExAllocatePoolWithTag(PagedPool, 0x200ui64, 0x20207050ui64);
  if( !PoolWithTag )
  {
    DeviceInstancePath = -1073741670;
    goto LABEL_4;
  }
  if( (int)CmGetDeviceRegProp(
              PiPnpRtlCtx,
              v7,
              (__int64)Handle,
              5,
              (__int64)&v36,
              (__int64)PoolWithTag,
              (__int64)&v35,
              0) >= 0
    && (_DWORD)v36 == 1
    && v35 )
  {
    if( (int)RtlInitUnicodeStringEx(&DestinationString, PoolWithTag) >= 0 )
    {
      Length = DestinationString.Length;
    }
    else
    {
      *(_DWORD *)&DestinationString.Length = 0;
      Length = 0;
      DestinationString.Buffer = 0i64;
    }
  }
  v10 = IopCreateRootEnumeratedDeviceObject(&Object);
  v11 = (_DEVICE_OBJECT *)Object;
  DeviceInstancePath = v10;
  if( v10 < 0 )
    goto LABEL_48;
  *((_DWORD *)Object + 12) |= 0x1000u;
  v12 = PipAllocateDeviceNode((__int64)v11, (void **)&DeviceNode);
  v13 = DeviceNode;
  DeviceInstancePath = v12;
  if( !DeviceNode )
  {
    if( v12 == -1073740946 )
      DeviceInstancePath = -1073741670;
LABEL_47:
    if( DeviceInstancePath >= 0 )
      goto LABEL_50;
    goto LABEL_48;
  }
  PipSetDevNodeFlags((__int64)DeviceNode, 17);
  PipSetDevNodeState(v13, DeviceNodeInitialized, v14);
  v35 = 4;
  if( (int)CmGetDeviceRegProp(PiPnpRtlCtx, v7, (__int64)Handle, 11, (__int64)&v36, (__int64)&v27, (__int64)&v35, 0) >= 0 )
  {
    if( (_DWORD)v36 == 4 && v35 == 4 )
    {
      v15 = v27;
    }
    else
    {
      v15 = 0;
      LODWORD(v27) = 0;
    }
    if( (v15 & 0x20) != 0 )
    {
      FailedInstallProblemStatus = 0;
      v17 = 18;
LABEL_40:
      PipSetDevNodeProblem((__int64)v13, v17, FailedInstallProblemStatus);
      goto LABEL_41;
    }
    if( (v15 & 0x2000) != 0 )
    {
      FailedInstallProblemStatus = 0;
      v17 = 16;
      goto LABEL_40;
    }
    if( (v15 & 0x40) != 0 )
    {
      FailedInstallProblemStatus = PiDevCfgGetFailedInstallProblemStatus(v7, Handle);
      v17 = 28;
      goto LABEL_40;
    }
  }
LABEL_41:
  DeviceInstancePath = PnpAllocateDeviceInstancePath((__int64)v13, (unsigned int)SourceString->Length + 2);
  if( DeviceInstancePath >= 0 )
  {
    PnpCopyDeviceInstancePath((__int64)v13, SourceString, v18);
    if( !Length
      || (PnpConcatenateUnicodeStrings((_UNICODE_STRING *)((char *)v13 + 56), &DestinationString, v19, v20, v25, v26),
          v21 < 0) )
    {
      *((_DWORD *)v13 + 14) = 0;
      *((_QWORD *)v13 + 8) = 0i64;
    }
    DeviceInstancePath = PnpMapDeviceObjectToDeviceInstance(
                           *((DEVICE_OBJECT **)v13 + 4),
                           (UNICODE_STRING *)((char *)v13 + 40));
    if( DeviceInstancePath >= 0 )
    {
      PpDevNodeInsertIntoTree((__int64)IopRootDeviceNode, (__int64)v13);
      ObfReferenceObject(v11);
      ExReleaseResourceLite(&PnpRegistryDeviceResource);
      KeLeaveCriticalRegion();
      v34 = 0;
      PnpQueryAndSaveDeviceNodeCapabilities((__int64)v13);
      PnpRaiseNtPlugPlayDevicePropertyChangeEvent(v22, *((_QWORD *)v13 + 6), 1);
      PnpRaiseNtPlugPlayDevicePropertyChangeEvent(v23, *((_QWORD *)v13 + 6), 14);
      PnpRaiseNtPlugPlayDevicePropertyChangeEvent(v24, *((_QWORD *)IopRootDeviceNode + 6), 15);
      PnpSetPlugPlayEvent(&GUID_DEVICE_ENUMERATED, *((DEVICE_OBJECT **)v13 + 4));
      goto LABEL_47;
    }
  }
LABEL_48:
  if( v11 )
    IoDeleteDevice(v11);
LABEL_50:
  if( v34 )
    goto LABEL_4;
LABEL_5:
  PpDevNodeUnlockTree(1);
  if( Handle )
    ZwClose(Handle);
  if( P )
    PiPnpRtlEndOperation(P);
  if( v7 )
    PnpUnicodeStringToWstrFree(v7, (_UNICODE_STRING *)SourceString);
  if( PoolWithTag )
    ExFreePoolWithTag(PoolWithTag, 0);
  return(unsigned int)DeviceInstancePath;
}

Referenced by:

PiCMCreateDevice