PipResetDevices

INT64 __fastcall PipResetDevices(INT64 a1){
  INT64 v1; 
  ULONG v2; 
  INT64 result; 
  __int64 v4; 
  int v5; 
  __int64 v6; 
  void *v7; 
  _ETHREAD *CurrentThread; 
  int v9; 
  void *PoolWithTag; 
  __int64 v11; 
  _WORD *v12; 
  int v13; 
  _ETHREAD *v14; 
  void *v15; 
  __int64 v16; 
  __int64 v17; 
  unsigned int v18; 
  int v19; 
  int v20; 
  int MatchingFilteredDeviceList; 
  int v22; 
  VOID *v23; 
  WCHAR *i; 
  __int64 v25; 
  unsigned int v26; 
  int v27; 
  int v28; 
  int v29; 
  _WORD *v30; 
  WCHAR *v31; 
  UINT64 v32; 
  __int64 v33; 
  unsigned int v34; 
  int v35; 
  int v36; 
  int v37; 
  void *v38; 
  UINT64 v39; 
  void *v40; 
  int v41; 
  int v42; 
  int v43; 
  int v44; 
  unsigned int v45; 
  void *v46; 
  INT64 v47; 
  unsigned int v48; 
  unsigned int v49; 
  int v50; 
  v47 = a1;
  v1 = PiPnpRtlCtx;
  v2 = 0;
  v39 = PiPnpRtlCtx;
  v46 = 0i64;
  v42 = 0;
  v50 = 0;
  result = PnpCtxRegOpenKey(PiPnpRtlCtx, (char *)0x80000002i64, L"SYSTEM", 0, 1u, &v46);
  if( (int)result >= 0 )
  {
    v41 = 4;
    v5 = PnpCtxRegQueryValue(v4, v46, L"DevModeEverEnabled", &v42, &v50, (__int64)&v41);
    v45 = v5;
    PnpCtxRegCloseKey(v6, v46);
    if( v5 >= 0 && v50 )
    {
      return(unsigned int)v5;
    }
    else
    {
      LODWORD(v47) = 0;
      v38 = 0i64;
      v7 = 0i64;
      CurrentThread = (_ETHREAD *)KeGetCurrentThread();
      v9 = 0;
      v36 = 0;
      PoolWithTag = 0i64;
      v35 = 0;
      v34 = 0;
      --*((_WORD *)CurrentThread + 242);
      ExAcquireResourceExclusiveLite(&PnpRegistryDeviceResource, 1u);
      if( (int)PnpCtxRegOpenKey(
                  v1,
                  (char *)0x80000002i64,
                  L"System\\CurrentControlSet\\Control\\StateSeparation\\PnP\\ResetDevices\\Classes",
                  0,
                  0x20019u,
                  &v38) >= 0
        && (int)PnpCtxRegQueryInfoKey(v11, v38, 0i64, 0i64, &v34, &v35, 0i64) >= 0 )
      {
        v18 = v34;
        if( v34 )
        {
          v19 = v35 + 1;
          PoolWithTag = ExAllocatePoolWithTag(PagedPool, 2i64 * (unsigned int)(v35 + 1), 0x6E697050ui64);
          if( PoolWithTag )
          {
            v11 = 0i64;
            if( v18 )
            {
              while( 1 )
              {
                v43 = v19;
                v20 = PnpCtxRegEnumValue(0i64, v38, v2, PoolWithTag, &v43, 0i64, 0i64, 0i64);
                v11 = 0i64;
                if( v20 >= 0 )
                {
                  DbgPrintEx(0x20u, 0, "Resetting devices in device setup class '%ws'\n", PoolWithTag);
                  MatchingFilteredDeviceList = CmGetMatchingFilteredDeviceList(
                                                 v1,
                                                 (__int64)PoolWithTag,
                                                 128,
                                                 0i64,
                                                 0i64,
                                                 (__int64)v7,
                                                 v9,
                                                 (__int64)&v47);
                  if( MatchingFilteredDeviceList == -1073741789 )
                  {
                    if( v7 )
                      ExFreePoolWithTag(v7, 0);
                    v22 = v47;
                    v36 = v47;
                    v23 = ExAllocatePoolWithTag(PagedPool, 2i64 * (unsigned int)v47, 0x6E697050ui64);
                    v11 = 0i64;
                    v7 = v23;
                    if( !v23 )
                    {
LABEL_38:
                      v2 = 0;
                      break;
                    }
                    MatchingFilteredDeviceList = CmGetMatchingFilteredDeviceList(
                                                   v1,
                                                   (__int64)PoolWithTag,
                                                   128,
                                                   0i64,
                                                   0i64,
                                                   (__int64)v23,
                                                   v22,
                                                   (__int64)&v47);
                  }
                  v11 = 0i64;
                  if( MatchingFilteredDeviceList >= 0 )
                  {
                    if( (_DWORD)v47 )
                    {
                      for( i = (WCHAR *)v7; *i; i += v25 + 1 )
                      {
                        PipResetDevice(v1, i);
                        v25 = -1i64;
                        v11 = 0i64;
                        do
                          ++v25;
                        while( i[v25] );
                      }
                    }
                  }
                }
                v9 = v36;
                if( ++v2 >= v18 )
                  goto LABEL_38;
              }
            }
          }
        }
      }
      if( v38 )
        PnpCtxRegCloseKey(v11, v38);
      if( PoolWithTag )
        ExFreePoolWithTag(PoolWithTag, 0);
      if( v7 )
        ExFreePoolWithTag(v7, 0);
      ExReleaseResourceLite(&PnpRegistryDeviceResource);
      KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
      v48 = 0;
      v12 = 0i64;
      v40 = 0i64;
      v13 = 0;
      v14 = (_ETHREAD *)KeGetCurrentThread();
      v15 = 0i64;
      v37 = 0;
      v49 = 0;
      --*((_WORD *)v14 + 242);
      ExAcquireResourceExclusiveLite(&PnpRegistryDeviceResource, 1u);
      v16 = v39;
      if( (int)PnpCtxRegOpenKey(
                  v39,
                  (char *)0x80000002i64,
                  L"System\\CurrentControlSet\\Control\\StateSeparation\\PnP\\ResetDevices\\Services",
                  0,
                  0x20019u,
                  &v40) >= 0
        && (int)PnpCtxRegQueryInfoKey(v17, v40, 0i64, 0i64, &v49, &v37, 0i64) >= 0 )
      {
        v26 = v49;
        if( v49 )
        {
          v27 = v37 + 1;
          LODWORD(v47) = v37 + 1;
          v15 = ExAllocatePoolWithTag(PagedPool, 2i64 * (unsigned int)(v37 + 1), 0x6E697050ui64);
          if( v15 )
          {
            v17 = 0i64;
            if( v26 )
            {
              do
              {
                v44 = v27;
                v28 = PnpCtxRegEnumValue(0i64, v40, v2, v15, &v44, 0i64, 0i64, 0i64);
                v17 = 0i64;
                if( v28 >= 0 )
                {
                  DbgPrintEx(0x20u, 0, "Resetting devices using service '%ws'\n", v15);
                  v29 = CmGetMatchingFilteredDeviceList(
                          v16,
                          (__int64)v15,
                          2,
                          0i64,
                          0i64,
                          (__int64)v12,
                          v13,
                          (__int64)&v48);
                  if( v29 == -1073741789 )
                  {
                    if( v12 )
                      ExFreePoolWithTag(v12, 0);
                    v13 = v48;
                    v30 = ExAllocatePoolWithTag(PagedPool, 2i64 * v48, 0x6E697050ui64);
                    v17 = 0i64;
                    v12 = v30;
                    if( !v30 )
                      break;
                    v29 = CmGetMatchingFilteredDeviceList(
                            v16,
                            (__int64)v15,
                            2,
                            0i64,
                            0i64,
                            (__int64)v30,
                            v13,
                            (__int64)&v48);
                  }
                  v17 = 0i64;
                  if( v29 >= 0 && v48 )
                  {
                    v31 = v12;
                    if( *v12 )
                    {
                      v32 = v39;
                      do
                      {
                        PipResetDevice(v32, v31);
                        v33 = -1i64;
                        v17 = 0i64;
                        do
                          ++v33;
                        while( v31[v33] );
                        v31 += v33 + 1;
                      }
                      while( *v31 );
                      v26 = v49;
                      v27 = v47;
                    }
                    v16 = v39;
                  }
                }
                ++v2;
              }
              while( v2 < v26 );
            }
          }
        }
      }
      if( v40 )
        PnpCtxRegCloseKey(v17, v40);
      if( v15 )
        ExFreePoolWithTag(v15, 0);
      if( v12 )
        ExFreePoolWithTag(v12, 0);
      ExReleaseResourceLite(&PnpRegistryDeviceResource);
      KeLeaveCriticalRegionThread((__int64)KeGetCurrentThread());
      return v45;
    }
  }
  return result;
}

Referenced by:

IopInitializePlugPlayServices