IoReportDetectedDevice

NTSTATUS __stdcall IoReportDetectedDevice(
        _DRIVER_OBJECT *DriverObject,
        _INTERFACE_TYPE LegacyBusType,
        UINT64 BusNumber,
        UINT64 SlotNumber,
        _CM_RESOURCE_LIST *ResourceList,
        _IO_RESOURCE_REQUIREMENTS_LIST *ResourceRequirements,
        UINT8 ResourceAssigned,
        _DEVICE_OBJECT **DeviceObject){
  _DEVICE_NODE *v9; 
  unsigned int v10; 
  _DRIVER_EXTENSION *DriverExtension; 
  _UNICODE_STRING *p_ServiceKeyName; 
  _DEVICE_OBJECT *v14; 
  NTSTATUS result; 
  _UNICODE_STRING *v16; 
  NTSTATUS appended; 
  INT64 v18; 
  NTSTATUS v19; 
  int v20; 
  _ETHREAD *CurrentThread; 
  _DEVICE_NODE *v22; 
  unsigned __int16 v23; 
  unsigned __int64 v24; 
  WCHAR *v25; 
  __int64 v26; 
  __int16 v27; 
  _QWORD *v28; 
  __int64 v29; 
  _IO_RESOURCE_REQUIREMENTS_LIST *v30; 
  WCHAR v31; 
  unsigned int v32; 
  char v33; 
  wchar_t *Buffer; 
  wchar_t *i; 
  _DEVICE_OBJECT *v36; 
  _DEVICE_OBJECT *v37; 
  NTSTATUS v38; 
  VOID **v39; 
  NTSTATUS DeviceInstancePath; 
  int v41; 
  WCHAR *v42; 
  NTSTATUS v43; 
  UINT64 v44; 
  _DEVICE_NODE *v45; 
  WCHAR v46; 
  _DRIVER_OBJECT *v47; 
  unsigned int v48; 
  bool v49; 
  UINT16 *p_Length; 
  NTSTATUS LegacyDeviceIds; 
  int v52; 
  unsigned int Problem; 
  int v54; 
  __int64 v55; 
  size_t v56; 
  VOID **PoolWithTag; 
  _CM_RESOURCE_LIST *v58; 
  _DRIVER_OBJECT *v59; 
  _UNICODE_STRING *v60; 
  _DEVICE_NODE *v61; 
  UINT64 dwFlags; 
  UINT64 dwFlagsa; 
  UINT64 dwFlagsb; 
  UINT8 *pszFormat; 
  INT64 pszFormata; 
  _DEVICE_OBJECT *pszFormatb; 
  INT64 a7; 
  INT64 a7a; 
  UINT64 Flags; 
  UINT64 Flagsa; 
  INT64 v72; 
  char v73; 
  char Created[15]; 
  UINT64 MatchingKey; 
  _DEVICE_NODE *DeviceNode; 
  _UNICODE_STRING Destination; 
  UINT8 PropertyBuffer[4]; 
  _DWORD cbMax[3]; 
  VOID *Handle; 
  UINT64 SlotNumbera; 
  VOID *DeviceKey; 
  WCHAR *psz; 
  UINT64 pcbLength; 
  _DRIVER_OBJECT *v85; 
  _IO_RESOURCE_REQUIREMENTS_LIST *v86; 
  HPNPOPCTX *hPnpOpCtx; 
  __int128 v88; 
  __int64 Length; 
  WCHAR *j; 
  _DEVICE_OBJECT **v91; 
  _NTSTRSAFE_PWSTR pszDest[100]; 

  v86 = ResourceRequirements;
  v9 = 0i64;
  v91 = DeviceObject;
  v10 = BusNumber;
  DriverExtension = DriverObject->DriverExtension;
  v85 = DriverObject;
  p_ServiceKeyName = &DriverExtension->ServiceKeyName;
  v14 = *DeviceObject;
  LODWORD(SlotNumbera) = SlotNumber;
  Created[1] = 0;
  *(_QWORD *)&Destination.Length = 26214400i64;
  psz = 0i64;
  memset(cbMax, 0, sizeof(cbMax));
  pcbLength = 0i64;
  *(_DWORD *)PropertyBuffer = 0;
  DeviceKey = 0i64;
  Created[0] = 0;
  DeviceNode = 0i64;
  LODWORD(MatchingKey) = 0;
  *(_QWORD *)&Created[7] = 0i64;
  Destination.Buffer = (wchar_t *)pszDest;
  Handle = 0i64;
  v73 = 0;
  hPnpOpCtx = 0i64;
  v88 = 0i64;
  if( v14 )
  {
    v9 = (_DEVICE_NODE *)v14->DeviceObjectExtension->DeviceNode;
    DeviceNode = v9;
    if( !v9 )
      return -1073741578;
    goto LABEL_7;
  }
  if( (DriverObject->Flags & 4) != 0 )
  {
    Buffer = p_ServiceKeyName->Buffer;
    for( i = &Buffer[((unsigned __int64)p_ServiceKeyName->Length >> 1) - 1]; i != Buffer; --i )
    {
      if( *i == 92 )
      {
        *((_QWORD *)&v88 + 1) = i + 1;
        LOWORD(v88) = p_ServiceKeyName->Length
                    - 2 * ((__int64)(unsigned int)((_DWORD)i + 2 - LODWORD(p_ServiceKeyName->Buffer)) >> 1);
        WORD1(v88) = v88;
        goto LABEL_3;
      }
    }
    return -1073741585;
  }
  else
  {
LABEL_3:
    result = RtlAppendUnicodeToString(&Destination, (WCHAR *)L"ROOT\\");
    if( result >= 0 )
    {
      v16 = (_UNICODE_STRING *)&v88;
      if( (DriverObject->Flags & 4) == 0 )
        v16 = p_ServiceKeyName;
      result = RtlAppendUnicodeStringToString(&Destination, v16);
      if( result >= 0 )
      {
LABEL_7:
        appended = PiPnpRtlBeginOperation(&hPnpOpCtx);
        if( appended < 0 )
          goto LABEL_40;
        PpDevNodeLockTree(PPL_TREEOP_ALLOW_READS);
        if( v9 || (DriverObject->Flags & 4) != 0 )
        {
          v20 = 0;
        }
        else
        {
          v19 = IopDuplicateDetection(LegacyBusType, v10, (unsigned int)SlotNumbera, &DeviceNode);
          v20 = 0;
          appended = v19;
          if( v19 >= 0 )
            LODWORD(MatchingKey) = DeviceNode != 0i64;
        }
        CurrentThread = (_ETHREAD *)KeGetCurrentThread();
        --CurrentThread->Tcb.KernelApcDisable;
        ExAcquireResourceExclusiveLite((UINT64)&PnpReplaceEvent + 6688, 1, v18);
        v22 = DeviceNode;
        if( DeviceNode )
        {
LABEL_25:
          if( (_DWORD)MatchingKey )
          {
            if( (unsigned int)PipAreDriversLoaded((__int64)v22)
              || (v22->Flags & 0x6000) != 0
              && ((Problem = v22->Problem, Problem > 0x1C) || (v54 = 268697602, !_bittest(&v54, Problem))) )
            {
              HalPutDmaAdapter((PADAPTER_OBJECT)v22->PhysicalDeviceObject);
              appended = -1073741810;
              goto LABEL_71;
            }
            if( !v29 )
              PipClearDevNodeProblem(v22);
          }
          if( !*(_QWORD *)&Created[7] )
          {
            appended = PnpDeviceObjectToDeviceInstance((__int64)v22->PhysicalDeviceObject, (VOID **)&Created[7], 983103);
            if( appended < 0 )
            {
LABEL_71:
              if( Created[0] )
              {
                if( Handle )
                  ZwDeleteKey(Handle);
                if( DeviceKey )
                  ZwDeleteKey(DeviceKey);
                if( *(_QWORD *)&Created[7] )
                  ZwDeleteKey(*(VOID **)&Created[7]);
                PnpCleanupDeviceRegistryValues(&v22->InstancePath);
                PpDevNodeRemoveFromTree(v22);
                IoDeleteDevice(v22->PhysicalDeviceObject);
                HalPutDmaAdapter((PADAPTER_OBJECT)v22->PhysicalDeviceObject);
              }
              goto LABEL_40;
            }
          }
LABEL_31:
          v30 = v86;
          if( !ResourceList && !v86
            || (LODWORD(dwFlags) = 983103,
                v41 = CmOpenDeviceRegKey(
                        *(&stru_140CF2E80 + 607),
                        v22->InstancePath.Buffer,
                        CM_KEY_TYPE_DEVICE_LOGCONF,
                        0i64,
                        dwFlags,
                        1u,
                        &Handle,
                        0i64),
                appended = v41,
                v41 >= 0)
            && (!ResourceList || (v41 = PnpSetRegistryResourceList(Handle, v55, ResourceList), appended = v41, v41 >= 0))
            && (!v30 || (v41 = PnpSetRegistryRequirementsList(Handle, v55, v30), appended = v41, v41 >= 0)) )
          {
            if( ResourceAssigned )
            {
              PipSetDevNodeFlags(v22, 0x100ui64);
              PnpSetRegistryDword(*(VOID **)&Created[7], L"NoResourceAtInitTime", v31);
              v32 = PnpDetermineResourceListSize(ResourceList);
              IopWriteAllocatedResourcesToRegistry(v22, ResourceList, v32);
            }
            else if( ResourceList && ResourceList->Count && ResourceList->List[0].PartialResourceList.Count )
            {
              v56 = PnpDetermineResourceListSize(ResourceList);
              PoolWithTag = ExAllocatePoolWithTag(1ui64, v56, 1198550608i64);
              v58 = (_CM_RESOURCE_LIST *)PoolWithTag;
              if( !PoolWithTag )
              {
LABEL_70:
                appended = -1073741670;
                PipSetDevNodeProblem(v22, 3ui64, -1073741670);
                goto LABEL_71;
              }
              memmove(PoolWithTag, ResourceList, v56);
              pszFormatb = v22->PhysicalDeviceObject;
              v59 = pszFormatb->DriverObject;
              Created[2] = 0;
              appended = IoReportResourceUsageInternal(
                           ArbiterRequestLegacyReported,
                           v60,
                           v59,
                           0i64,
                           dwFlags,
                           pszFormatb,
                           v58,
                           Flags,
                           v72,
                           (UINT8 *)&Created[2]);
              ExFreePoolWithTag(v58, 0x47706E50u);
              if( appended < 0 || Created[2] )
              {
                if( !PipIsDevNodeDNStarted((__int64)v22) )
                  PipSetDevNodeProblem(v61, 0xCui64, appended);
                appended = -1073741800;
              }
            }
            else
            {
              PipSetDevNodeFlags(v22, 0x100ui64);
            }
LABEL_35:
            v33 = v73;
LABEL_36:
            if( appended >= 0 )
            {
              v22->NumaNodeIndex = v22->Parent->NumaNodeIndex;
              IopDoDeferredSetInterfaceState(v22);
              PipSetDevNodeState(v22, DeviceNodeStartPostWork);
              if( v33 )
                PipSetDevNodeFlags(v22, 0x80000000ui64);
              *v91 = v22->PhysicalDeviceObject;
              goto LABEL_40;
            }
            goto LABEL_71;
          }
          goto LABEL_116;
        }
        appended = RtlAppendUnicodeToString(&Destination, (WCHAR *)L"\\");
        if( appended < 0 )
        {
LABEL_40:
          ExReleaseResourceLite((PERESOURCE)((char *)&PnpReplaceEvent + 6688));
          KeLeaveCriticalRegion();
          PpDevNodeUnlockTree(PPL_TREEOP_ALLOW_READS);
          if( Handle )
            ZwClose((_HANDLE)Handle);
          if( DeviceKey )
            ZwClose((_HANDLE)DeviceKey);
          if( *(_QWORD *)&Created[7] )
            ZwClose(*(_HANDLE *)&Created[7]);
          if( hPnpOpCtx )
            PiPnpRtlEndOperation(hPnpOpCtx);
          return appended;
        }
        Length = Destination.Length;
        v23 = Destination.Length;
        v24 = (400 - (unsigned __int64)Destination.Length) >> 1;
        v25 = (WCHAR *)pszDest + ((unsigned __int64)Destination.Length >> 1);
        for( j = v25; ; v25 = j )
        {
          Destination.Length = v23;
          LODWORD(a7) = v20;
          SlotNumbera = (UINT64)v25;
          RtlStringCchPrintfExW(v25, v24, (WCHAR **)&SlotNumbera, 0i64, 0i64, (WCHAR *)L"%04u");
          v26 = (__int64)(SlotNumbera - 2 * ((unsigned __int64)Destination.Length >> 1) - (_QWORD)pszDest) >> 1;
          if( (_DWORD)v26 == -1 )
            v27 = 400 - Destination.Length;
          else
            v27 = 2 * v26;
          LODWORD(pszFormat) = 0;
          Destination.Length += v27;
          appended = CmCreateDevice(
                       *(&stru_140CF2E80 + 607),
                       Destination.Buffer,
                       (WCHAR *)0xF003F,
                       (INT64)&Created[7],
                       (VOID **)Created,
                       pszFormat,
                       a7,
                       Flags,
                       v72);
          if( appended < 0 )
            goto LABEL_71;
          if( Created[0] )
            goto LABEL_23;
          LODWORD(MatchingKey) = 0;
          if( IopIsReportedAlready(
                 &Destination,
                 *(VOID **)&Created[7],
                 p_ServiceKeyName,
                 (_HANDLE)ResourceList,
                 &MatchingKey,
                 pszFormata,
                 a7,
                 Flags) )
          {
            break;
          }
          ZwClose(*(_HANDLE *)&Created[7]);
          v23 = Length;
          ++v20;
        }
        v28 = PnpDeviceObjectFromDeviceInstanceWithTag((__int64)&Destination, 0x746C6644u);
        *(_QWORD *)&cbMax[1] = v28;
        if( v28 )
        {
          v22 = *(_DEVICE_NODE **)(v28[39] + 40i64);
        }
        else
        {
          appended = -1073741823;
          v22 = 0i64;
        }
        DeviceNode = v22;
LABEL_23:
        if( appended < 0 )
          goto LABEL_71;
        if( v22 )
          goto LABEL_25;
        appended = IopCreateRootEnumeratedDeviceObject(&cbMax[1]);
        if( appended < 0 )
          goto LABEL_71;
        v36 = *(_DEVICE_OBJECT **)&cbMax[1];
        v37 = *(_DEVICE_OBJECT **)&cbMax[1];
        v33 = 1;
        v73 = 1;
        *(_DWORD *)(*(_QWORD *)&cbMax[1] + 48i64) |= 0x1000u;
        v38 = PipAllocateDeviceNode(v37, (PDEVICE_NODE *)&DeviceNode);
        v22 = DeviceNode;
        if( v38 == -1073740946 || !DeviceNode )
        {
          IoDeleteDevice(v36);
          appended = -1073741670;
          goto LABEL_71;
        }
        if( (v85->Flags & 4) == 0 )
        {
          v39 = ExAllocatePoolWithTag(1ui64, p_ServiceKeyName->Length, 1215327824i64);
          v22->ServiceName.Buffer = (wchar_t *)v39;
          if( !v39 )
            goto LABEL_70;
          v22->ServiceName.MaximumLength = p_ServiceKeyName->Length;
          DeviceInstancePath = RtlUpcaseUnicodeString(&v22->ServiceName, p_ServiceKeyName, 0);
          appended = DeviceInstancePath;
          if( DeviceInstancePath < 0 )
            goto LABEL_80;
        }
        DeviceInstancePath = PnpAllocateDeviceInstancePath((__int64)v22, (unsigned int)Destination.Length + 2);
        appended = DeviceInstancePath;
        if( DeviceInstancePath < 0
          || (DeviceInstancePath = RtlUpcaseUnicodeString(&v22->InstancePath, &Destination, 0),
              appended = DeviceInstancePath,
              DeviceInstancePath < 0) )
        {
LABEL_80:
          PipSetDevNodeProblem(v22, 3ui64, DeviceInstancePath);
          goto LABEL_36;
        }
        LODWORD(Flags) = 0;
        v22->InstancePath.Buffer[(unsigned __int64)v22->InstancePath.Length >> 1] = 0;
        LODWORD(a7) = 4;
        LODWORD(dwFlags) = 4;
        *(_DWORD *)PropertyBuffer = 32;
        v41 = CmSetDeviceRegProp(
                *(&stru_140CF2E80 + 607),
                v22->InstancePath.Buffer,
                *(VOID **)&Created[7],
                CM_REG_PROP_CONFIGFLAGS,
                dwFlags,
                PropertyBuffer,
                a7,
                Flags);
        appended = v41;
        if( v41 >= 0 )
        {
          Created[1] = -1;
          v41 = PnpSetObjectProperty(
                  *(&stru_140CF2E80 + 607),
                  (INT64)v22->InstancePath.Buffer,
                  1u,
                  *(INT64 *)&Created[7],
                  0i64,
                  (__int64)&DEVPKEY_Device_Reported,
                  17,
                  (__int64)&Created[1],
                  1,
                  0);
          appended = v41;
          if( v41 >= 0 )
          {
            v41 = PnpUnicodeStringToWstr((__int16 **)&psz, cbMax, &p_ServiceKeyName->Length);
            appended = v41;
            if( v41 >= 0 )
            {
              v42 = psz;
              appended = RtlStringCbLengthW(psz, cbMax[0], &pcbLength);
              if( appended < 0 )
              {
                PnpUnicodeStringToWstrFree(v42, (__int64)p_ServiceKeyName);
LABEL_88:
                v43 = appended;
                goto LABEL_89;
              }
              LODWORD(Flagsa) = 0;
              LODWORD(a7a) = pcbLength + 2;
              LODWORD(dwFlagsa) = 1;
              appended = CmSetDeviceRegProp(
                           *(&stru_140CF2E80 + 607),
                           v22->InstancePath.Buffer,
                           *(VOID **)&Created[7],
                           CM_REG_PROP_SERVICE,
                           dwFlagsa,
                           (UINT8 *)v42,
                           a7a,
                           Flagsa);
              PnpUnicodeStringToWstrFree(v42, (__int64)p_ServiceKeyName);
              if( appended < 0 )
                goto LABEL_88;
              LODWORD(dwFlagsb) = 983103;
              v41 = CmOpenDeviceRegKey(
                      *(&stru_140CF2E80 + 607),
                      v22->InstancePath.Buffer,
                      CM_KEY_TYPE_DEVICE_CONTROL,
                      0i64,
                      dwFlagsb,
                      1u,
                      &DeviceKey,
                      0i64);
              appended = v41;
              if( v41 >= 0 )
              {
                v41 = PnpSetRegistryDword(DeviceKey, L"DeviceReported", v46);
                appended = v41;
                if( v41 >= 0 )
                {
                  v47 = v85;
                  v48 = v85->Flags;
                  v49 = (v48 & 4) == 0;
                  if( (v48 & 4) == 0 )
                  {
                    PpDeviceRegistration(&Destination, 1u, &v22->ServiceName, 1u);
                    v49 = (v47->Flags & 4) == 0;
                  }
                  p_Length = (UINT16 *)&v88;
                  if( v49 )
                    p_Length = &p_ServiceKeyName->Length;
                  LegacyDeviceIds = IopCreateLegacyDeviceIds((INT64)v36, p_Length, (INT64)ResourceList);
                  appended = LegacyDeviceIds;
                  v45 = v22;
                  if( LegacyDeviceIds < 0 )
                  {
                    v43 = LegacyDeviceIds;
                    v44 = 19i64;
                    goto LABEL_93;
                  }
                  PipSetDevNodeFlags(v22, 0x11ui64);
                  v22->NumaNodeIndex = *(_DWORD *)(*(&PnpReplaceEvent + 831) + 660i64);
                  PipSetDevNodeState(v22, DeviceNodeInitialized);
                  PpDevNodeInsertIntoTree(*(&PnpReplaceEvent + 831), v22);
                  v52 = PnpMapDeviceObjectToDeviceInstance((__int64)v36, (__int64)&v22->InstancePath);
                  appended = v52;
                  if( v52 < 0 )
                  {
                    v43 = v52;
                    v44 = 3i64;
LABEL_91:
                    v45 = v22;
LABEL_93:
                    PipSetDevNodeProblem(v45, v44, v43);
                    goto LABEL_35;
                  }
                  ObfReferenceObject(v36);
                  goto LABEL_31;
                }
              }
            }
          }
        }
LABEL_116:
        v43 = v41;
LABEL_89:
        v44 = 19i64;
        goto LABEL_91;
      }
    }
  }
  return result;
}

Referenced by:

HalpDriverEntry