PipCallDriverAddDevice

__int64 __fastcall PipCallDriverAddDevice(ULONG_PTR BugCheckParameter2, __int64 a2){
  CHAR v4; 
  UINT8 UsePdoCharacteristics; 
  void *QuadPart; 
  DEVICE_OBJECT **v7; 
  __int64 v8; 
  int v9; 
  int ObjectProperty; 
  const wchar_t *v11; 
  __int64 v12; 
  __int64 v13; 
  const wchar_t *v14; 
  INT64 v15; 
  int v16; 
  __int64 *v17; 
  __int64 v18; 
  int v20; 
  DRIVER_OBJECT **v21; 
  int v22; 
  unsigned int v23; 
  __int64 v24; 
  int v25; 
  int v26; 
  int v27; 
  DEVICE_OBJECT *v28; 
  union _LARGE_INTEGER v29; 
  unsigned __int8 j; 
  __int64 v31; 
  __int64 *v32; 
  _BOOL8 v33; 
  NTSTATUS v34; 
  INTERFACE_TYPE *v35; 
  __int64 v36; 
  int v37; 
  _PNP_DEVNODE_STATE *v38; 
  __int64 v39; 
  _PNP_DEVNODE_STATE *v40; 
  int v41; 
  int v42; 
  int v43; 
  int v44; 
  void *PoolWithTag; 
  int DeviceRegProp; 
  WCHAR v47; 
  int v48; 
  int i; 
  void *v50; 
  const wchar_t *v51; 
  const WCHAR *v52; 
  char *v53; 
  int v54; 
  const wchar_t *v55; 
  int v56; 
  const WCHAR *v57; 
  INT64 v58; 
  SIZE_T NumberOfBytes; 
  char v60[4]; 
  int v61; 
  int v62; 
  HANDLE Handle; 
  unsigned int v64; 
  PVOID P; 
  __int16 v66[2]; 
  int v67; 
  HANDLE InstallerClassKey; 
  int v69; 
  DEVICE_OBJECT **v70; 
  union _LARGE_INTEGER SystemBootTime; 
  struct _UNICODE_STRING DestinationString; 
  __int64 v73; 
  __int64 v74; 
  INT64 result[2]; 
  __int64 v76[3]; 
  __int64 v77; 
  WCHAR SourceString[40]; 
  v60[0] = 0;
  InstallerClassKey = 0i64;
  v61 = 0;
  v64 = 0;
  v4 = 0;
  Handle = 0i64;
  UsePdoCharacteristics = 0;
  v66[0] = 0;
  v73 = 0i64;
  NumberOfBytes = 0i64;
  v62 = 0;
  v69 = 0;
  memset((INT64)result, 0i64);
  QuadPart = 0i64;
  v7 = 0i64;
  SystemBootTime.QuadPart = 0i64;
  v67 = 0;
  v8 = 6i64;
  SourceString[0] = 0;
  v74 = 6i64;
  DestinationString = 0i64;
  P = 0i64;
  v70 = 0i64;
  if( *(_BYTE *)(BugCheckParameter2 + 688) && !*(_BYTE *)(a2 + 4) )
  {
    v9 = -1073741823;
    goto LABEL_30;
  }
  *(_BYTE *)(BugCheckParameter2 + 688) = 0;
  v9 = CmOpenDeviceRegKey(
         PiPnpRtlCtx,
         *(const wchar_t **)(BugCheckParameter2 + 48),
         16,
         0,
         131097,
         0,
         (__int64)&Handle,
         0i64);
  if( v9 < 0 )
    goto LABEL_29;
  if( (*(_DWORD *)(*(_QWORD *)(BugCheckParameter2 + 32) + 48i64) & 0x2000000) != 0
    && ((int)PnpGetObjectProperty(
               PiPnpRtlCtx,
               *(_QWORD *)(BugCheckParameter2 + 48),
               1i64,
               (__int64)Handle,
               0i64,
               (__int64)&DEVPKEY_Device_DebuggerSafe,
               &v67,
               (unsigned __int64)&v64,
               4,
               (ULONG *)&NumberOfBytes + 1,
               0) < 0
     || v67 != 7
     || HIDWORD(NumberOfBytes) != 4
     || !v64) )
  {
    v44 = 53;
LABEL_120:
    PnpRequestDeviceRemoval(BugCheckParameter2, 0, v44, 0);
LABEL_112:
    v9 = -1073741106;
    goto LABEL_29;
  }
  if( *(_DWORD *)(BugCheckParameter2 + 660) == -1 )
  {
    if( PnpQueryProximityNode
      && (int)PnpGetObjectProperty(
                PiPnpRtlCtx,
                *(_QWORD *)(BugCheckParameter2 + 48),
                1i64,
                (__int64)Handle,
                0i64,
                (__int64)&DEVPKEY_Device_Numa_Proximity_Domain,
                &v67,
                (unsigned __int64)&v64,
                4,
                (ULONG *)&NumberOfBytes + 1,
                0) >= 0
      && v64 <= 0xFF
      && (int)((__int64(__fastcall *)(_QWORD, __int16 *))PnpQueryProximityNode)(v64, v66) >= 0 )
    {
      *(_DWORD *)(BugCheckParameter2 + 660) = (unsigned __int16)v66[0];
    }
    v23 = *(_DWORD *)(BugCheckParameter2 + 660);
    if( v23 == -1 )
    {
      v24 = *(_QWORD *)(BugCheckParameter2 + 16);
      if( v24 )
      {
        v23 = *(_DWORD *)(v24 + 660);
        *(_DWORD *)(BugCheckParameter2 + 660) = v23;
      }
      else
      {
        *(_DWORD *)(BugCheckParameter2 + 660) = -2;
        v23 = -2;
      }
    }
    if( v23 != -2 )
    {
      v64 = v23;
      PnpSetObjectProperty(
        PiPnpRtlCtx,
        *(_QWORD *)(BugCheckParameter2 + 48),
        1u,
        (__int64)Handle,
        0i64,
        (__int64)&DEVPKEY_Device_Numa_Node,
        7,
        (__int64)&v64,
        4u,
        0);
    }
  }
  ObjectProperty = PnpGetObjectProperty(
                     PiPnpRtlCtx,
                     *(_QWORD *)(BugCheckParameter2 + 48),
                     1i64,
                     (__int64)Handle,
                     0i64,
                     (__int64)DEVPKEY_Device_PreventDriverLoad,
                     &v67,
                     (unsigned __int64)&v73,
                     8,
                     (ULONG *)&NumberOfBytes + 1,
                     0);
  if( ((int)(ObjectProperty + 0x80000000) < 0 || ObjectProperty == -1073741789)
    && (int)PnpGetStableSystemBootTime(&SystemBootTime) >= 0 )
  {
    if( ObjectProperty != -1073741789
      && v67 == 16
      && HIDWORD(NumberOfBytes) == 8
      && !PnpBootMode
      && SystemBootTime.QuadPart == v73 )
    {
      goto LABEL_28;
    }
    PnpSetObjectProperty(
      PiPnpRtlCtx,
      *(_QWORD *)(BugCheckParameter2 + 48),
      1u,
      (__int64)Handle,
      0i64,
      (__int64)DEVPKEY_Device_PreventDriverLoad,
      0,
      0i64,
      0,
      0);
  }
  v11 = *(const wchar_t **)(BugCheckParameter2 + 48);
  LODWORD(NumberOfBytes) = 78;
  v9 = 0;
  if( (int)CmGetDeviceRegProp(
              PiPnpRtlCtx,
              v11,
              (__int64)Handle,
              9,
              (__int64)&v62,
              (__int64)SourceString,
              (__int64)&NumberOfBytes,
              0) >= 0
    && v62 == 1
    && (_DWORD)NumberOfBytes )
  {
    RtlInitUnicodeStringEx(&DestinationString, SourceString);
    if( InitSafeBootMode )
    {
      if( !IopSafebootDriverLoad(&DestinationString) )
      {
        LODWORD(NumberOfBytes) = 256;
        PoolWithTag = ExAllocatePoolWithTag(PagedPool, 0x100ui64, 0x6E657050ui64);
        if( PoolWithTag )
        {
          DeviceRegProp = CmGetDeviceRegProp(
                            PiPnpRtlCtx,
                            *(const wchar_t **)(BugCheckParameter2 + 48),
                            (__int64)Handle,
                            1,
                            (__int64)&v62,
                            (__int64)PoolWithTag,
                            (__int64)&NumberOfBytes,
                            0);
          if( DeviceRegProp == -1073741789 )
          {
            ExFreePoolWithTag(PoolWithTag, 0);
            PoolWithTag = ExAllocatePoolWithTag(PagedPool, (unsigned int)NumberOfBytes, 0x6E657050ui64);
          }
          else if( DeviceRegProp >= 0 )
          {
            DestinationString = 0i64;
            RtlInitUnicodeString(&DestinationString, (PCWSTR)PoolWithTag, v47);
          }
        }
        IopBootLog(&DestinationString, 0);
        if( PoolWithTag )
          ExFreePoolWithTag(PoolWithTag, 0);
        goto LABEL_28;
      }
      v4 = v60[0];
    }
    CmOpenInstallerClassRegKey(PiPnpRtlCtx, SourceString, v12, v13, 131097, 0, (__int64)&InstallerClassKey, 0i64);
  }
  v14 = *(const wchar_t **)(BugCheckParameter2 + 48);
  LODWORD(NumberOfBytes) = 4;
  if( (int)CmGetDeviceRegProp(
              PiPnpRtlCtx,
              v14,
              (__int64)Handle,
              11,
              (__int64)&v62,
              (__int64)&v61,
              (__int64)&NumberOfBytes,
              0) < 0
    || v62 != 4
    || (_DWORD)NumberOfBytes != 4 )
  {
    v61 = 0;
  }
  result[1] = a2;
  result[0] = BugCheckParameter2;
  LODWORD(NumberOfBytes) = 512;
  P = ExAllocatePoolWithTag(PagedPool, 0x200ui64, 0x6E657050ui64);
  if( P )
  {
    if( (v61 & 0x80000) != 0 )
    {
      if( InstallerClassKey )
      {
        LODWORD(v58) = 0;
        v48 = PnpCallDriverQueryServiceHelper(
                &P,
                (ULONG *)&NumberOfBytes,
                0i64,
                (INT64)&DEVPKEY_DeviceClass_ConfigFilters,
                SourceString,
                (INT64)InstallerClassKey,
                1,
                v58,
                v4,
                result);
        v9 = v48;
        if( v48 == -1073741772 || v48 == -1073741275 )
          v9 = 0;
        if( v9 < 0 )
          goto LABEL_24;
        for( i = PnpGetObjectProperty(
                    PiPnpRtlCtx,
                    (__int64)SourceString,
                    2i64,
                    (__int64)InstallerClassKey,
                    0i64,
                    (__int64)DEVPKEY_DeviceClass_ConfigNotifyWnfTriggers,
                    &v69,
                    (unsigned __int64)P,
                    NumberOfBytes,
                    (ULONG *)&NumberOfBytes + 1,
                    0);
              ;
              i = PnpGetObjectProperty(
                    PiPnpRtlCtx,
                    (__int64)SourceString,
                    2i64,
                    (__int64)InstallerClassKey,
                    0i64,
                    (__int64)DEVPKEY_DeviceClass_ConfigNotifyWnfTriggers,
                    &v69,
                    (unsigned __int64)v50,
                    NumberOfBytes,
                    (ULONG *)&NumberOfBytes + 1,
                    0) )
        {
          v9 = i;
          if( i != -1073741789 )
            break;
          if( HIDWORD(NumberOfBytes) <= (unsigned int)NumberOfBytes )
          {
            v9 = -1073741823;
            goto LABEL_24;
          }
          ExFreePoolWithTag(P, 0);
          LODWORD(NumberOfBytes) = HIDWORD(NumberOfBytes);
          v50 = ExAllocatePoolWithTag(PagedPool, HIDWORD(NumberOfBytes), 0x6E657050ui64);
          P = v50;
          if( !v50 )
          {
            v9 = -1073741670;
            break;
          }
        }
        if( v9 >= 0 )
        {
          if( v69 == 4099 && (NumberOfBytes & 0x700000000i64) == 0 && HIDWORD(NumberOfBytes) )
          {
            v53 = (char *)P;
            v54 = HIDWORD(NumberOfBytes) >> 3;
            do
            {
              ZwUpdateWnfStateData();
              v53 += 8;
              --v54;
            }
            while( v54 );
            v44 = 56;
            goto LABEL_120;
          }
        }
        else if( v9 == -1073741772 || v9 == -1073741275 )
        {
          v9 = 0;
        }
        if( v9 < 0 )
          goto LABEL_24;
      }
      if( !v76[0] )
      {
        v51 = *(const wchar_t **)(BugCheckParameter2 + 48);
        HIDWORD(NumberOfBytes) = 4;
        if( (int)CmGetDeviceRegProp(
                    PiPnpRtlCtx,
                    v51,
                    (__int64)Handle,
                    11,
                    (__int64)&v62,
                    (__int64)&v61,
                    (__int64)&NumberOfBytes + 4,
                    0) >= 0
          && v62 == 4
          && HIDWORD(NumberOfBytes) == 4
          && (v61 & 0x80000) != 0 )
        {
          v52 = *(const WCHAR **)(BugCheckParameter2 + 48);
          v61 &= ~0x80000u;
          CmSetDeviceRegProp(PiPnpRtlCtx, v52, (__int64)Handle, 0xBu, 4u, (__int64)&v61, 4u, 0);
        }
      }
    }
    if( (*(_DWORD *)(BugCheckParameter2 + 396) & 0x4000000) == 0 )
    {
      LODWORD(v58) = 1;
      v25 = PnpCallDriverQueryServiceHelper(
              &P,
              (ULONG *)&NumberOfBytes,
              19i64,
              (INT64)&DEVPKEY_Device_CompoundLowerFilters,
              *(WCHAR **)(BugCheckParameter2 + 48),
              (INT64)Handle,
              0,
              v58,
              v4,
              result);
      v9 = v25;
      if( v25 == -1073741275 || v25 == -1073741772 )
      {
        PipSetDevNodeFlags(BugCheckParameter2, 0x4000000);
        v9 = 0;
      }
    }
    if( v9 >= 0 )
    {
      if( InstallerClassKey )
      {
        if( (*(_DWORD *)(BugCheckParameter2 + 396) & 0x8000000) == 0 )
        {
          LODWORD(v58) = 2;
          v22 = PnpCallDriverQueryServiceHelper(
                  &P,
                  (ULONG *)&NumberOfBytes,
                  19i64,
                  (INT64)&DEVPKEY_DeviceClass_CompoundLowerFilters,
                  SourceString,
                  (INT64)InstallerClassKey,
                  1,
                  v58,
                  v4,
                  result);
          v9 = v22;
          if( v22 == -1073741275 || v22 == -1073741772 )
          {
            PipSetDevNodeFlags(BugCheckParameter2, 0x8000000);
            v9 = 0;
          }
        }
      }
      if( v9 >= 0 )
      {
        if( (*(_DWORD *)(BugCheckParameter2 + 396) & 0x10000000) != 0 )
        {
          v9 = -1073741772;
        }
        else
        {
          LODWORD(v58) = 3;
          v20 = PnpCallDriverQueryServiceHelper(
                  &P,
                  (ULONG *)&NumberOfBytes,
                  5i64,
                  0i64,
                  *(WCHAR **)(BugCheckParameter2 + 48),
                  (INT64)Handle,
                  0,
                  v58,
                  v4,
                  result);
          v9 = v20;
          if( v20 == -1073741275 || v20 == -1073741772 )
          {
            v9 = -1073741772;
            PipSetDevNodeFlags(BugCheckParameter2, 0x10000000);
          }
        }
      }
    }
  }
  else
  {
    v9 = -1073741670;
  }
LABEL_24:
  v16 = *(_DWORD *)(BugCheckParameter2 + 396);
  if( (v16 & 0x1000) != 0 )
  {
    v9 = 0;
    goto LABEL_29;
  }
  if( v9 >= 0 )
  {
    if( *(_QWORD *)(v77 + 8) )
    {
      PipSetDevNodeProblem(BugCheckParameter2, 19, -1073741438);
      v9 = -1073741823;
      goto LABEL_29;
    }
    goto LABEL_65;
  }
  if( v9 == -1073741772 )
  {
    if( (*(_DWORD *)(BugCheckParameter2 + 560) & 0x100) == 0 )
    {
LABEL_28:
      v9 = -1073741823;
      goto LABEL_29;
    }
    PipClearDevNodeProblem(BugCheckParameter2, 0i64, v15);
    v16 = *(_DWORD *)(BugCheckParameter2 + 396);
    v9 = 0;
    UsePdoCharacteristics = 1;
LABEL_65:
    if( (v16 & 0x20000000) == 0 )
    {
      LODWORD(v58) = 4;
      v26 = PnpCallDriverQueryServiceHelper(
              &P,
              (ULONG *)&NumberOfBytes,
              18i64,
              (INT64)&DEVPKEY_Device_CompoundUpperFilters,
              *(WCHAR **)(BugCheckParameter2 + 48),
              (INT64)Handle,
              0,
              v58,
              v4,
              result);
      v9 = v26;
      if( v26 == -1073741275 || v26 == -1073741772 )
      {
        PipSetDevNodeFlags(BugCheckParameter2, 0x20000000);
        v9 = 0;
      }
    }
    if( v9 < 0 )
      goto LABEL_29;
    if( InstallerClassKey )
    {
      if( (*(_DWORD *)(BugCheckParameter2 + 396) & 0x40000000) == 0 )
      {
        LODWORD(v58) = 5;
        v27 = PnpCallDriverQueryServiceHelper(
                &P,
                (ULONG *)&NumberOfBytes,
                18i64,
                (INT64)&DEVPKEY_DeviceClass_CompoundUpperFilters,
                SourceString,
                (INT64)InstallerClassKey,
                1,
                v58,
                v4,
                result);
        v9 = v27;
        if( v27 == -1073741275 || v27 == -1073741772 )
        {
          PipSetDevNodeFlags(BugCheckParameter2, 0x40000000);
          v9 = 0;
        }
      }
    }
    if( v9 < 0 )
      goto LABEL_29;
    v9 = PiDmaGuardProcessPreAddDevice((LARGE_INTEGER *)result, (LARGE_INTEGER *)Handle);
    if( v9 < 0 )
      goto LABEL_29;
    v28 = 0i64;
    v29.LowPart = IoGetAttachedDeviceReferenceWithTag(*(PDEVICE_OBJECT *)(BugCheckParameter2 + 32), 0x65706E50u);
    SystemBootTime = v29;
    for( j = 0; j < 6u; ++j )
    {
      if( j == 3 )
      {
        LODWORD(v39) = IoGetAttachedDeviceReferenceWithTag(*(PDEVICE_OBJECT *)(BugCheckParameter2 + 32), 0x65706E50u);
        v70 = (DEVICE_OBJECT **)v39;
        if( UsePdoCharacteristics )
        {
          if( !v77 )
            PipSetDevNodeState((_DEVICE_NODE *)BugCheckParameter2, DeviceNodeDriversAdded, v40);
        }
      }
      v31 = v76[j];
      v32 = (__int64 *)v31;
      if( v31 )
      {
        do
        {
          v37 = PnpCallAddDevice(
                  BugCheckParameter2,
                  *v32,
                  *(__int64(__fastcall **)(struct _DRIVER_OBJECT *, _DEVICE_OBJECT *))(*(_QWORD *)(*v32 + 48) + 8i64),
                  j);
          *(_DWORD *)(*v32 + 16) |= 0x400u;
          if( v37 < 0 )
          {
            if( j == 3 )
            {
              QuadPart = (void *)SystemBootTime.QuadPart;
              IovUtilMarkStack(
                *(DEVICE_OBJECT **)(BugCheckParameter2 + 32),
                *(DEVICE_OBJECT **)(SystemBootTime.QuadPart + 24),
                v28,
                0i64);
              v42 = v41;
              *(_DWORD *)(BugCheckParameter2 + 392) = v41;
              v43 = 31;
              goto LABEL_111;
            }
          }
          else
          {
            if( j == 3 )
              v28 = v70[3];
            PipSetDevNodeState((_DEVICE_NODE *)BugCheckParameter2, DeviceNodeDriversAdded, v38);
          }
          v32 = (__int64 *)v32[1];
        }
        while( v32 );
      }
      if( !j )
      {
        if( v31 )
        {
          v55 = *(const wchar_t **)(BugCheckParameter2 + 48);
          HIDWORD(NumberOfBytes) = 4;
          v56 = CmGetDeviceRegProp(
                  PiPnpRtlCtx,
                  v55,
                  (__int64)Handle,
                  11,
                  (__int64)&v62,
                  (__int64)&v61,
                  (__int64)&NumberOfBytes + 4,
                  0);
          if( v56 >= 0 && v62 == 4 && HIDWORD(NumberOfBytes) == 4 && (v61 & 0x80000) != 0 )
          {
            v57 = *(const WCHAR **)(BugCheckParameter2 + 48);
            v61 &= ~0x80000u;
            CmSetDeviceRegProp(PiPnpRtlCtx, v57, (__int64)Handle, 0xBu, 4u, (__int64)&v61, 4u, 0);
          }
        }
      }
    }
    v33 = !v28 || UsePdoCharacteristics;
    QuadPart = (void *)SystemBootTime.QuadPart;
    IovUtilMarkStack(
      *(DEVICE_OBJECT **)(BugCheckParameter2 + 32),
      *(DEVICE_OBJECT **)(SystemBootTime.QuadPart + 24),
      v28,
      v33);
    v34 = PipChangeDeviceObjectFromRegistryProperties(
            *(DEVICE_OBJECT **)(BugCheckParameter2 + 32),
            Handle,
            SourceString,
            InstallerClassKey,
            UsePdoCharacteristics);
    if( v34 >= 0 )
    {
      if( IopQueryLegacyBusInformation(
             *(DEVICE_OBJECT **)(BugCheckParameter2 + 32),
             v35,
             (UINT64 *)(BugCheckParameter2 + 448)) >= 0 )
      {
        IopInsertLegacyBusDeviceNode(
          BugCheckParameter2,
          *(unsigned int *)(BugCheckParameter2 + 448),
          *(unsigned int *)(BugCheckParameter2 + 452));
      }
      else
      {
        *(_DWORD *)(BugCheckParameter2 + 448) = -1;
        *(_DWORD *)(BugCheckParameter2 + 452) = -16;
      }
      PnpRaiseNtPlugPlayDevicePropertyChangeEvent(v36, *(_QWORD *)(BugCheckParameter2 + 48), 23);
      if( (*(_DWORD *)(BugCheckParameter2 + 560) & 0x100000) == 0
        || (v9 = PiProcessDriversLoadedOnSecureDevice(BugCheckParameter2), v9 >= 0) )
      {
        if( PipDmaGuardPolicy && *(_QWORD *)(BugCheckParameter2 + 720) )
          v9 = PipDmgEnforceEnumerationPolicy((PVOID)BugCheckParameter2);
        else
          v9 = 0;
      }
      v7 = v70;
      goto LABEL_29;
    }
    v42 = v34;
    v43 = 50;
LABEL_111:
    PnpRequestDeviceRemoval(BugCheckParameter2, 0, v43, v42);
    v7 = v70;
    goto LABEL_112;
  }
LABEL_29:
  v8 = 6i64;
LABEL_30:
  v17 = v76;
  do
  {
    v18 = *v17;
    if( *v17 )
    {
      do
      {
        v21 = (DRIVER_OBJECT **)v18;
        v18 = *(_QWORD *)(v18 + 8);
        if( PnPBootDriversInitialized )
          PnpUnloadAttachedDriver(*v21);
        HalPutDmaAdapter((PADAPTER_OBJECT)*v21);
        ExFreePoolWithTag(v21, 0);
      }
      while( v18 );
      v8 = v74;
    }
    ++v17;
    v74 = --v8;
  }
  while( v8 );
  if( Handle )
    ZwClose(Handle);
  if( InstallerClassKey )
    ZwClose(InstallerClassKey);
  if( QuadPart )
    ObfDereferenceObjectWithTag(QuadPart, 0x65706E50ui64);
  if( v7 )
    ObfDereferenceObjectWithTag(v7, 0x65706E50ui64);
  if( P )
    ExFreePoolWithTag(P, 0);
  return(unsigned int)v9;
}

Referenced by:

PiProcessAddBootDevices
PipProcessDevNodeTree