IopQueryDeviceResources

VOID __stdcall IopQueryDeviceResources(_DEVICE_OBJECT *DeviceObject, UINT64 ResourceType, PVOID *Resource, UINT64 *a4){
  int v5; 
  _IO_RESOURCE_REQUIREMENTS_LIST *v8; 
  __int64 v9; 
  NTSTATUS v10; 
  CM_RESOURCE_LIST *v11; 
  unsigned int *v12; 
  int DeviceResourcesFromRegistry; 
  UINT8 *PoolWithTag; 
  void *v15; 
  UINT64 v16; 
  void *v17; 
  PVOID v18; 
  int v19; 
  int v20; 
  PVOID Data; 
  UINT64 DataSize; 
  struct _UNICODE_STRING ValueName; 
  PVOID P; 
  IO_RESOURCE_REQUIREMENTS_LIST *MergedList; 
  void *Src; 
  _WORD result[68]; 
  UINT64 ExactMatch; 
  unsigned int Length; 
  HANDLE KeyHandle; 
  PVOID v31; 
  *Resource = 0i64;
  v5 = ResourceType;
  KeyHandle = 0i64;
  Src = 0i64;
  P = 0i64;
  v31 = 0i64;
  MergedList = 0i64;
  v8 = 0i64;
  LODWORD(ExactMatch) = 0;
  *(_DWORD *)a4 = 0;
  memset((INT64)result, 0i64);
  v9 = *(_QWORD *)(*((_QWORD *)DeviceObject + 39) + 40i64);
  *(_QWORD *)&ValueName.Length = v9;
  if( !v5 )
  {
    if( (*(_DWORD *)(v9 + 396) & 1) != 0 )
    {
      PnpGetDeviceResourcesFromRegistry(DeviceObject, 0, 7u, Resource, (unsigned int *)a4);
    }
    else
    {
      result[0] = 2587;
      v10 = IopSynchronousCall(DeviceObject, (IO_STACK_LOCATION *)result, 3221225659i64, 0i64, (UINT64 *)Resource);
      if( v10 == -1073741637 )
      {
        *Resource = 0i64;
        v10 = 0;
      }
      if( v10 >= 0 )
        *(_DWORD *)a4 = PnpDetermineResourceListSize((CM_RESOURCE_LIST *)*Resource);
    }
    return;
  }
  if( (unsigned int)PnpGetDeviceResourcesFromRegistry(DeviceObject, 0, 2u, Resource, &Length) != -1073741772 )
  {
    if( *Resource )
    {
      v8 = PnpCmResourcesToIoResources(v11, (UINT64)*Resource, 0i64);
      ExFreePoolWithTag(*Resource, 0);
      if( !v8 )
      {
        *Resource = 0i64;
        *(_DWORD *)a4 = 0;
        return;
      }
      *Resource = v8;
      *(_DWORD *)a4 = *(_DWORD *)v8;
    }
    goto LABEL_17;
  }
  if( (unsigned int)PnpGetDeviceResourcesFromRegistry(DeviceObject, 1u, 1u, &KeyHandle, &Length) != -1073741772 )
  {
    v8 = (_IO_RESOURCE_REQUIREMENTS_LIST *)KeyHandle;
    goto LABEL_16;
  }
  if( (*(_DWORD *)(v9 + 396) & 1) != 0 )
  {
    DeviceResourcesFromRegistry = PnpGetDeviceResourcesFromRegistry(DeviceObject, 1u, 2u, &KeyHandle, &Length);
    if( DeviceResourcesFromRegistry == -1073741772 )
      goto LABEL_14;
    goto LABEL_50;
  }
  v12 = *(unsigned int **)(v9 + 440);
  if( !v12 )
  {
    DeviceResourcesFromRegistry = PpIrpQueryResourceRequirements(
                                    DeviceObject,
                                    (IO_RESOURCE_REQUIREMENTS_LIST **)&KeyHandle);
    if( DeviceResourcesFromRegistry == -1073741637 )
    {
      v8 = 0i64;
LABEL_14:
      DeviceResourcesFromRegistry = 0;
      goto LABEL_15;
    }
LABEL_50:
    v8 = (_IO_RESOURCE_REQUIREMENTS_LIST *)KeyHandle;
LABEL_15:
    if( DeviceResourcesFromRegistry < 0 )
      return;
    goto LABEL_16;
  }
  PoolWithTag = (UINT8 *)ExAllocatePoolWithTag(PagedPool, *v12, 0x20207050ui64);
  v8 = (_IO_RESOURCE_REQUIREMENTS_LIST *)PoolWithTag;
  if( !PoolWithTag )
    return;
  memmove(PoolWithTag, *(UINT8 **)(v9 + 440), **(unsigned int **)(v9 + 440));
LABEL_16:
  if( (int)PnpGetDeviceResourcesFromRegistry(DeviceObject, 0, 4u, &P, &Length) >= 0 )
  {
    v18 = P;
    if( !P || !*(_DWORD *)P || *((_DWORD *)P + 1) != 5 )
    {
      v19 = PnpFilterResourceRequirementsList(
              v8,
              (_CM_RESOURCE_LIST *)P,
              (_IO_RESOURCE_REQUIREMENTS_LIST **)&v31,
              &ExactMatch);
      if( v18 )
        ExFreePoolWithTag(v18, 0);
      if( v19 < 0 )
      {
        if( v8 )
          ExFreePoolWithTag(v8, 0);
        return;
      }
      v9 = *(_QWORD *)&ValueName.Length;
      if( (*(_DWORD *)(*(_QWORD *)&ValueName.Length + 396i64) & 1) != 0
        || (_DWORD)ExactMatch && *((_DWORD *)v8 + 7) <= 1u )
      {
        if( v8 )
          ExFreePoolWithTag(v8, 0);
        v8 = (_IO_RESOURCE_REQUIREMENTS_LIST *)v31;
      }
      else
      {
        v20 = PnpMergeFilteredResourceRequirementsList((IO_RESOURCE_REQUIREMENTS_LIST *)v31, v8, &MergedList);
        if( v8 )
          ExFreePoolWithTag(v8, 0);
        if( v31 )
          ExFreePoolWithTag(v31, 0);
        if( v20 < 0 )
          return;
        v8 = MergedList;
      }
    }
  }
LABEL_17:
  if( IopFilterResourceRequirementsCall(DeviceObject, v8, &Src) >= 0 )
  {
    v15 = Src;
    *(_DWORD *)(&ValueName.MaximumLength + 1) = 0;
    KeyHandle = 0i64;
    if( Src )
    {
      v16 = *(unsigned int *)Src;
      *(_DWORD *)a4 = *(_DWORD *)Src;
      v17 = ExAllocatePoolWithTag(PagedPool, v16, 0x20207050ui64);
      *Resource = v17;
      if( !v17 )
      {
        ExFreePoolWithTag(v15, 0);
        return;
      }
      memmove((UINT8 *)v17, (UINT8 *)v15, *(unsigned int *)a4);
      ExFreePoolWithTag(v15, 0);
    }
    else
    {
      *(_DWORD *)a4 = 0;
      *Resource = 0i64;
    }
    if( (int)CmOpenDeviceRegKey(PiPnpRtlCtx, *(const wchar_t **)(v9 + 48), 19, 0, 131097, 1, (__int64)&KeyHandle, 0i64) >= 0 )
    {
      ValueName.Buffer = L"FilteredConfigVector";
      LODWORD(DataSize) = *(_DWORD *)a4;
      Data = *Resource;
      *(_DWORD *)&ValueName.Length = 2752552;
      ZwSetValueKey(KeyHandle, &ValueName, 0i64, 0xAui64, Data, DataSize);
      ZwClose(KeyHandle);
    }
  }
  else
  {
    *Resource = v8;
    if( v8 )
      *(_DWORD *)a4 = *(_DWORD *)v8;
    else
      *(_DWORD *)a4 = 0;
  }
}

Referenced by:

IopReleaseDeviceResources
PiQueryAndAllocateBootResources
PnpGetResourceRequirementsForAssignTable