IoReportDetectedDevice

NTSTATUS __stdcall IoReportDetectedDevice(
        PDRIVER_OBJECT DriverObject,
        INTERFACE_TYPE LegacyBusType,
        ULONG BusNumber,
        ULONG SlotNumber,
        PCM_RESOURCE_LIST ResourceList,
        PIO_RESOURCE_REQUIREMENTS_LIST ResourceRequirements,
        BOOLEAN ResourceAssigned,
        PDEVICE_OBJECT *DeviceObject){
  INT64 v9; 
  _DRIVER_EXTENSION *DriverExtension; 
  _UNICODE_STRING *p_ServiceKeyName; 
  PDEVICE_OBJECT v14; 
  NTSTATUS result; 
  UNICODE_STRING *v16; 
  int appended; 
  int v18; 
  BOOLEAN v19; 
  _ETHREAD *CurrentThread; 
  INT64 v21; 
  unsigned __int16 v22; 
  unsigned __int64 v23; 
  wchar_t *v24; 
  signed __int64 v25; 
  __int16 v26; 
  _DEVICE_OBJECT *v27; 
  const WCHAR *v28; 
  __int64 v29; 
  PIO_RESOURCE_REQUIREMENTS_LIST v30; 
  INT64 v31; 
  INT64 v32; 
  unsigned int v33; 
  char v34; 
  _PNP_DEVNODE_STATE *v35; 
  wchar_t *Buffer; 
  wchar_t *i; 
  _DEVICE_OBJECT *v38; 
  __int64 v39; 
  int DeviceNode; 
  VOID *v41; 
  int DeviceInstancePath; 
  int v43; 
  WCHAR *v44; 
  int v45; 
  int v46; 
  __int64 v47; 
  INT64 v48; 
  INT64 v49; 
  PDRIVER_OBJECT v50; 
  unsigned int Flags; 
  bool v52; 
  UINT16 *p_Length; 
  int LegacyDeviceIds; 
  _PNP_DEVNODE_STATE *v55; 
  NTSTATUS v56; 
  unsigned int v57; 
  int v58; 
  CM_RESOURCE_LIST *v59; 
  UINT64 v60; 
  UINT8 *PoolWithTag; 
  UINT8 *v62; 
  CM_RESOURCE_LIST *v63; 
  DRIVER_OBJECT *v64; 
  __int64 v65; 
  _CM_RESOURCE_LIST *dwFlags; 
  _CM_RESOURCE_LIST *dwFlagsa; 
  _IO_RESOURCE_REQUIREMENTS_LIST *pszFormat; 
  _IO_RESOURCE_REQUIREMENTS_LIST *pszFormata; 
  wchar_t *pszFormatb; 
  BOOLEAN a7; 
  BOOLEAN a7a; 
  PDEVICE_OBJECT *a8; 
  PDEVICE_OBJECT *a8a; 
  INT64 a9; 
  INT64 a9a; 
  INT64 a10; 
  INT64 a10a; 
  INT64 a11; 
  HANDLE Handle; 
  UINT64 MatchingKey; 
  INT64 a14; 
  struct _UNICODE_STRING Destination; 
  INT64 v84; 
  _DEVICE_OBJECT *cbMax_4; 
  HANDLE KeyHandle; 
  NTSTRSAFE_PWSTR ppszDestEnd; 
  HANDLE v88; 
  STRSAFE_PCNZWCH psz; 
  size_t pcbLength; 
  PDRIVER_OBJECT v91; 
  PIO_RESOURCE_REQUIREMENTS_LIST v92; 
  PVOID P; 
  __int128 v94; 
  INT64 Length; 
  wchar_t *j; 
  PDEVICE_OBJECT *v97; 
  wchar_t pszDest[200]; 
  v92 = ResourceRequirements;
  v9 = 0i64;
  v97 = DeviceObject;
  DriverExtension = DriverObject->DriverExtension;
  v91 = DriverObject;
  p_ServiceKeyName = &DriverExtension->ServiceKeyName;
  v14 = *DeviceObject;
  LODWORD(ppszDestEnd) = SlotNumber;
  *(_QWORD *)&Destination.Length = 26214400i64;
  psz = 0i64;
  pcbLength = 0i64;
  v84 = 0i64;
  v88 = 0i64;
  *(_WORD *)((char *)&a11 + 1) = 0;
  a14 = 0i64;
  LODWORD(MatchingKey) = 0;
  Handle = 0i64;
  Destination.Buffer = pszDest;
  KeyHandle = 0i64;
  LOBYTE(a11) = 0;
  cbMax_4 = 0i64;
  P = 0i64;
  v94 = 0i64;
  if( v14 )
  {
    v9 = *(_QWORD *)(*((_QWORD *)v14 + 39) + 40i64);
    a14 = 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 *)&v94 + 1) = i + 1;
        LOWORD(v94) = p_ServiceKeyName->Length
                    - 2 * ((__int64)(unsigned int)((_DWORD)i + 2 - LODWORD(p_ServiceKeyName->Buffer)) >> 1);
        WORD1(v94) = v94;
        goto LABEL_3;
      }
    }
    return -1073741585;
  }
  else
  {
LABEL_3:
    result = RtlAppendUnicodeToString(&Destination, (PWCHAR)L"ROOT\\");
    if( result >= 0 )
    {
      v16 = (UNICODE_STRING *)&v94;
      if( (DriverObject->Flags & 4) == 0 )
        v16 = p_ServiceKeyName;
      result = RtlAppendUnicodeStringToString(&Destination, v16);
      if( result >= 0 )
      {
LABEL_7:
        appended = PiPnpRtlBeginOperation((__int64 **)&P);
        if( appended < 0 )
          goto LABEL_40;
        PpDevNodeLockTree(1);
        if( v9 || (DriverObject->Flags & 4) != 0 )
        {
          v19 = 0;
        }
        else
        {
          v18 = IopDuplicateDetection(LegacyBusType, BusNumber, (unsigned int)ppszDestEnd, &a14);
          v19 = 0;
          appended = v18;
          if( v18 >= 0 )
            LODWORD(MatchingKey) = a14 != 0;
        }
        CurrentThread = (_ETHREAD *)KeGetCurrentThread();
        --*((_WORD *)CurrentThread + 242);
        ExAcquireResourceExclusiveLite(&PnpRegistryDeviceResource, 1u);
        v21 = a14;
        if( a14 )
        {
LABEL_25:
          if( (_DWORD)MatchingKey )
          {
            if( (unsigned int)PipAreDriversLoaded((_DEVICE_NODE *)v21)
              || (*(_DWORD *)(v21 + 396) & 0x6000) != 0
              && ((v57 = *(_DWORD *)(v21 + 404), v57 > 0x1C) || (v58 = 268697602, !_bittest(&v58, v57))) )
            {
              HalPutDmaAdapter(*(PADAPTER_OBJECT *)(v21 + 32));
              appended = -1073741810;
              goto LABEL_71;
            }
            if( !v29 )
              PipClearDevNodeProblem(v21, v28, 0i64);
          }
          if( !Handle )
          {
            appended = PnpDeviceObjectToDeviceInstance(*(DEVICE_OBJECT **)(v21 + 32), &Handle, 0xF003Fui64);
            if( appended < 0 )
            {
LABEL_71:
              if( BYTE1(a11) )
              {
                if( KeyHandle )
                  ZwDeleteKey(KeyHandle);
                if( v88 )
                  ZwDeleteKey(v88);
                if( Handle )
                  ZwDeleteKey(Handle);
                PnpCleanupDeviceRegistryValues((UNICODE_STRING *)(v21 + 40));
                PpDevNodeRemoveFromTree((_DEVICE_NODE *)v21);
                IoDeleteDevice(*(_DEVICE_OBJECT **)(v21 + 32));
                HalPutDmaAdapter(*(PADAPTER_OBJECT *)(v21 + 32));
              }
              goto LABEL_40;
            }
          }
LABEL_31:
          v30 = v92;
          if( !ResourceList && !v92
            || (v43 = CmOpenDeviceRegKey(
                        PiPnpRtlCtx,
                        *(const wchar_t **)(v21 + 48),
                        20,
                        0,
                        983103,
                        1,
                        (__int64)&KeyHandle,
                        0i64),
                appended = v43,
                v43 >= 0)
            && (!ResourceList || (v43 = PnpSetRegistryResourceList(KeyHandle, v59), appended = v43, v43 >= 0))
            && (!v30
             || (v43 = PnpSetRegistryRequirementsList(KeyHandle, (IO_RESOURCE_REQUIREMENTS_LIST *)v59),
                 appended = v43,
                 v43 >= 0)) )
          {
            if( ResourceAssigned )
            {
              PipSetDevNodeFlags(v21, 256);
              PnpSetRegistryDword(
                (INT64)Handle,
                (UINT64)L"NoResourceAtInitTime",
                v31,
                v32,
                dwFlags,
                pszFormat,
                a7,
                a8,
                a9,
                a10,
                a11,
                (INT64)Handle,
                MatchingKey,
                a14,
                *(INT64 *)&Destination.Length,
                (INT64)Destination.Buffer,
                v84,
                (INT64)cbMax_4,
                (INT64)KeyHandle,
                (INT64)ppszDestEnd,
                (INT64)v88,
                (INT64)psz,
                pcbLength,
                (INT64)v91,
                (INT64)v92,
                (INT64)P,
                v94,
                *((INT64 *)&v94 + 1),
                Length);
              v33 = PnpDetermineResourceListSize(ResourceList);
              IopWriteAllocatedResourcesToRegistry((_DEVICE_NODE *)v21, ResourceList, v33);
            }
            else if( ResourceList && *(_DWORD *)ResourceList && *((_DWORD *)ResourceList + 4) )
            {
              v60 = (unsigned int)PnpDetermineResourceListSize(ResourceList);
              PoolWithTag = (UINT8 *)ExAllocatePoolWithTag(PagedPool, v60, 0x47706E50ui64);
              v62 = PoolWithTag;
              if( !PoolWithTag )
              {
LABEL_70:
                appended = -1073741670;
                PipSetDevNodeProblem(v21, 3, -1073741670);
                goto LABEL_71;
              }
              memmove(PoolWithTag, (UINT8 *)ResourceList, v60);
              pszFormatb = *(wchar_t **)(v21 + 32);
              v63 = (CM_RESOURCE_LIST *)*((_QWORD *)pszFormatb + 1);
              BYTE3(a11) = 0;
              appended = IoReportResourceUsageInternal(
                           ArbiterRequestLegacyReported,
                           v64,
                           v63,
                           0i64,
                           dwFlags,
                           (UINT8 *)pszFormatb);
              ExFreePoolWithTag(v62, 0x47706E50u);
              if( appended < 0 || BYTE3(a11) )
              {
                if( !(unsigned int)PipIsDevNodeDNStarted((_DEVICE_NODE *)v21) )
                  PipSetDevNodeProblem(v65, 12, appended);
                appended = -1073741800;
              }
            }
            else
            {
              PipSetDevNodeFlags(v21, 256);
            }
LABEL_35:
            v34 = a11;
LABEL_36:
            if( appended >= 0 )
            {
              *(_DWORD *)(v21 + 660) = *(_DWORD *)(*(_QWORD *)(v21 + 16) + 660i64);
              IopDoDeferredSetInterfaceState((_DEVICE_NODE *)v21);
              PipSetDevNodeState((_DEVICE_NODE *)v21, DeviceNodeStartPostWork, v35);
              if( v34 )
                PipSetDevNodeFlags(v21, 0x80000000);
              *v97 = *(PDEVICE_OBJECT *)(v21 + 32);
              goto LABEL_40;
            }
            goto LABEL_71;
          }
          goto LABEL_116;
        }
        appended = RtlAppendUnicodeToString(&Destination, (PWCHAR)L"\\");
        if( appended < 0 )
        {
LABEL_40:
          ExReleaseResourceLite(&PnpRegistryDeviceResource);
          KeLeaveCriticalRegion();
          PpDevNodeUnlockTree(1);
          if( KeyHandle )
            ZwClose(KeyHandle);
          if( v88 )
            ZwClose(v88);
          if( Handle )
            ZwClose(Handle);
          if( P )
            PiPnpRtlEndOperation(P);
          return appended;
        }
        Length = Destination.Length;
        v22 = Destination.Length;
        v23 = (400 - (unsigned __int64)Destination.Length) >> 1;
        v24 = &pszDest[(unsigned __int64)Destination.Length >> 1];
        for( j = v24; ; v24 = j )
        {
          Destination.Length = v22;
          a7 = v19;
          ppszDestEnd = v24;
          RtlStringCchPrintfExW(v24, v23, &ppszDestEnd, 0i64, 0i64, L"%04u");
          v25 = &ppszDestEnd[-((unsigned __int64)Destination.Length >> 1)] - pszDest;
          if( (_DWORD)v25 == -1 )
            v26 = 400 - Destination.Length;
          else
            v26 = 2 * v25;
          Destination.Length += v26;
          appended = CmCreateDevice(PiPnpRtlCtx, Destination.Buffer, 983103, &Handle, (_BYTE *)&a11 + 1, 0);
          if( appended < 0 )
            goto LABEL_71;
          if( BYTE1(a11) )
            goto LABEL_23;
          LODWORD(MatchingKey) = 0;
          if( IopIsReportedAlready(&Destination, Handle, p_ServiceKeyName, ResourceList, &MatchingKey) )
            break;
          ZwClose(Handle);
          v22 = Length;
          ++v19;
        }
        v27 = (_DEVICE_OBJECT *)PnpDeviceObjectFromDeviceInstanceWithTag((INT64)&Destination, 0x746C6644ui64);
        cbMax_4 = v27;
        if( v27 )
        {
          v21 = *(_QWORD *)(*((_QWORD *)v27 + 39) + 40i64);
        }
        else
        {
          appended = -1073741823;
          v21 = 0i64;
        }
        a14 = v21;
LABEL_23:
        if( appended < 0 )
          goto LABEL_71;
        if( v21 )
          goto LABEL_25;
        appended = IopCreateRootEnumeratedDeviceObject(&cbMax_4);
        if( appended < 0 )
          goto LABEL_71;
        v38 = cbMax_4;
        v39 = (__int64)cbMax_4;
        v34 = 1;
        LOBYTE(a11) = 1;
        *((_DWORD *)cbMax_4 + 12) |= 0x1000u;
        DeviceNode = PipAllocateDeviceNode(v39, (void **)&a14);
        v21 = a14;
        if( DeviceNode == -1073740946 || !a14 )
        {
          IoDeleteDevice(v38);
          appended = -1073741670;
          goto LABEL_71;
        }
        if( (v91->Flags & 4) == 0 )
        {
          v41 = ExAllocatePoolWithTag(PagedPool, p_ServiceKeyName->Length, 0x48706E50ui64);
          *(_QWORD *)(v21 + 64) = v41;
          if( !v41 )
            goto LABEL_70;
          *(_WORD *)(v21 + 58) = p_ServiceKeyName->Length;
          DeviceInstancePath = RtlUpcaseUnicodeString((UNICODE_STRING *)(v21 + 56), p_ServiceKeyName, 0);
          appended = DeviceInstancePath;
          if( DeviceInstancePath < 0 )
            goto LABEL_80;
        }
        DeviceInstancePath = PnpAllocateDeviceInstancePath(v21, (unsigned int)Destination.Length + 2);
        appended = DeviceInstancePath;
        if( DeviceInstancePath < 0
          || (DeviceInstancePath = RtlUpcaseUnicodeString((UNICODE_STRING *)(v21 + 40), &Destination, 0),
              appended = DeviceInstancePath,
              DeviceInstancePath < 0) )
        {
LABEL_80:
          PipSetDevNodeProblem(v21, 3, DeviceInstancePath);
          goto LABEL_36;
        }
        *(_WORD *)(*(_QWORD *)(v21 + 48) + 2 * ((unsigned __int64)*(unsigned __int16 *)(v21 + 40) >> 1)) = 0;
        LODWORD(v84) = 32;
        v43 = CmSetDeviceRegProp(
                PiPnpRtlCtx,
                *(const WCHAR **)(v21 + 48),
                (__int64)Handle,
                0xBu,
                4u,
                (__int64)&v84,
                4u,
                0);
        appended = v43;
        if( v43 >= 0 )
        {
          BYTE2(a11) = -1;
          v43 = PnpSetObjectProperty(
                  PiPnpRtlCtx,
                  *(_QWORD *)(v21 + 48),
                  1u,
                  (__int64)Handle,
                  0i64,
                  (__int64)&DEVPKEY_Device_Reported,
                  17,
                  (__int64)&a11 + 2,
                  1u,
                  0);
          appended = v43;
          if( v43 >= 0 )
          {
            v43 = PnpUnicodeStringToWstr((WCHAR **)&psz, (UINT64 *)((char *)&v84 + 4), p_ServiceKeyName);
            appended = v43;
            if( v43 >= 0 )
            {
              v44 = (WCHAR *)psz;
              appended = RtlStringCbLengthW(psz, HIDWORD(v84), &pcbLength);
              if( appended < 0 )
              {
                PnpUnicodeStringToWstrFree(v44, p_ServiceKeyName);
LABEL_88:
                v45 = appended;
                goto LABEL_89;
              }
              appended = CmSetDeviceRegProp(
                           PiPnpRtlCtx,
                           *(const WCHAR **)(v21 + 48),
                           (__int64)Handle,
                           5u,
                           1u,
                           (__int64)v44,
                           (int)pcbLength + 2,
                           0);
              PnpUnicodeStringToWstrFree(v44, p_ServiceKeyName);
              if( appended < 0 )
                goto LABEL_88;
              v43 = CmOpenDeviceRegKey(
                      PiPnpRtlCtx,
                      *(const wchar_t **)(v21 + 48),
                      19,
                      0,
                      983103,
                      1,
                      (__int64)&v88,
                      0i64);
              appended = v43;
              if( v43 >= 0 )
              {
                v43 = PnpSetRegistryDword(
                        (INT64)v88,
                        (UINT64)L"DeviceReported",
                        v48,
                        v49,
                        dwFlagsa,
                        pszFormata,
                        a7a,
                        a8a,
                        a9a,
                        a10a,
                        a11,
                        (INT64)Handle,
                        MatchingKey,
                        a14,
                        *(INT64 *)&Destination.Length,
                        (INT64)Destination.Buffer,
                        v84,
                        (INT64)cbMax_4,
                        (INT64)KeyHandle,
                        (INT64)ppszDestEnd,
                        (INT64)v88,
                        (INT64)psz,
                        pcbLength,
                        (INT64)v91,
                        (INT64)v92,
                        (INT64)P,
                        v94,
                        *((INT64 *)&v94 + 1),
                        Length);
                appended = v43;
                if( v43 >= 0 )
                {
                  v50 = v91;
                  Flags = v91->Flags;
                  v52 = (Flags & 4) == 0;
                  if( (Flags & 4) == 0 )
                  {
                    PpDeviceRegistration(&Destination, 1u, (UNICODE_STRING *)(v21 + 56), 1u);
                    v52 = (v50->Flags & 4) == 0;
                  }
                  p_Length = (UINT16 *)&v94;
                  if( v52 )
                    p_Length = &p_ServiceKeyName->Length;
                  LegacyDeviceIds = IopCreateLegacyDeviceIds((INT64)v38, p_Length, (INT64)ResourceList);
                  appended = LegacyDeviceIds;
                  v47 = v21;
                  if( LegacyDeviceIds < 0 )
                  {
                    v45 = LegacyDeviceIds;
                    v46 = 19;
                    goto LABEL_93;
                  }
                  PipSetDevNodeFlags(v21, 17);
                  *(_DWORD *)(v21 + 660) = *((_DWORD *)IopRootDeviceNode + 165);
                  PipSetDevNodeState((_DEVICE_NODE *)v21, DeviceNodeInitialized, v55);
                  PpDevNodeInsertIntoTree((__int64)IopRootDeviceNode, v21);
                  v56 = PnpMapDeviceObjectToDeviceInstance(v38, (UNICODE_STRING *)(v21 + 40));
                  appended = v56;
                  if( v56 < 0 )
                  {
                    v45 = v56;
                    v46 = 3;
LABEL_91:
                    v47 = v21;
LABEL_93:
                    PipSetDevNodeProblem(v47, v46, v45);
                    goto LABEL_35;
                  }
                  ObfReferenceObject(v38);
                  goto LABEL_31;
                }
              }
            }
          }
        }
LABEL_116:
        v45 = v43;
LABEL_89:
        v46 = 19;
        goto LABEL_91;
      }
    }
  }
  return result;
}

Referenced by:

HalpDriverEntry