WdipSemLoadScenarioTable

NTSTATUS __stdcall WdipSemLoadScenarioTable(){
  INT64 v0; 
  UINT64 v1; 
  int v2; 
  unsigned int v3; 
  NTSTATUS v4; 
  bool v5; 
  unsigned int v6; 
  VOID *v7; 
  INT64 v8; 
  NTSTATUS NextScenario; 
  unsigned int v10; 
  unsigned int v11; 
  unsigned int v13; 
  unsigned int v14; 
  __int64 v15; 
  __int64 v16; 
  int *Length; 
  UINT64 *ResultLength; 
  UINT64 v19; 
  _HANDLE KeyHandle[2]; 
  VOID *Handle; 
  int v22; 
  int v23; 
  __int64 v24; 
  VOID *Key; 
  _UNICODE_STRING DestinationString[2]; 
  _OSVERSIONINFOEXW VersionInfo; 
  _GUID Guid; 
  int KeyInformation[38]; 

  Key = 0i64;
  Handle = 0i64;
  *(_QWORD *)KeyHandle = 0i64;
  memset(DestinationString, 0, 24);
  v0 = 0i64;
  LODWORD(v19) = 0;
  v22 = 0;
  v23 = 0;
  Guid = 0i64;
  memset(&VersionInfo, 0i64, sizeof(VersionInfo));
  v1 = 0i64;
  v2 = WdipSemOpenRegistryKey(L"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET\\Control\\WDI\\Scenarios", 0i64, &Key);
  if( v2 >= 0 )
  {
    v3 = 0;
    while( 1 )
    {
      while( 1 )
      {
        do
        {
          while( 1 )
          {
            do
            {
              do
              {
LABEL_3:
                if( Handle )
                {
                  ZwClose((_HANDLE)Handle);
                  Handle = 0i64;
                }
                if( *(_QWORD *)KeyHandle )
                {
                  ZwClose(KeyHandle[0]);
                  *(_QWORD *)KeyHandle = 0i64;
                }
                if( *(_QWORD *)&DestinationString[0].Length )
                {
                  ZwClose(*(_HANDLE *)&DestinationString[0].Length);
                  *(_QWORD *)&DestinationString[0].Length = 0i64;
                }
                memset(KeyInformation, 0i64, sizeof(KeyInformation));
                LODWORD(Length) = 150;
                v4 = ZwEnumerateKey(Key, v3++, KeyBasicInformation, KeyInformation, (UINT64)Length, &v19);
                if( v4 == -2147483622 )
                {
                  v2 = 0;
                  WdipSemEnableAllProviders();
                  goto LABEL_36;
                }
              }
              while( v4 < 0 );
              if( KeyInformation[3] >= 0x80u )
              {
                v2 = -2147483643;
                goto LABEL_36;
              }
              *((_WORD *)&KeyInformation[4] + ((unsigned __int64)(unsigned int)KeyInformation[3] >> 1)) = 0;
              RtlInitUnicodeString((PUNICODE_STRING)&DestinationString[0].Buffer, (PCWSTR)&KeyInformation[4]);
            }
            while( RtlGUIDFromString((_UNICODE_STRING *)&DestinationString[0].Buffer, &Guid) < 0 );
            if( !*(&ExBootDevicesRemovedEvent + 557) )
              break;
            v13 = *(_DWORD *)(*(&ExBootDevicesRemovedEvent + 557) + 1024i64);
            v14 = 0;
            if( !v13 )
              break;
            while( 1 )
            {
              v15 = 16i64 * v14;
              v16 = *(_QWORD *)&Guid.Data1 - *(_QWORD *)(v15 + *(&ExBootDevicesRemovedEvent + 557));
              if( *(_QWORD *)&Guid.Data1 == *(_QWORD *)(v15 + *(&ExBootDevicesRemovedEvent + 557)) )
                v16 = *(_QWORD *)Guid.Data4 - *(_QWORD *)(v15 + *(&ExBootDevicesRemovedEvent + 557) + 8);
              if( !v16 )
                break;
              if( ++v14 >= v13 )
                goto LABEL_14;
            }
          }
LABEL_14:
          ;
        }
        while( WdipSemOpenRegistryKey((const WCHAR *)&KeyInformation[4], Key, &Handle) < 0 );
        LODWORD(v24) = 1;
        v5 = 1;
        if( WdipSemOpenRegistryKey(L"Config", Handle, (VOID **)KeyHandle) >= 0 )
          break;
LABEL_16:
        if( WdipSemOpenRegistryKey(L"Instrumentation", Handle, (VOID **)DestinationString) >= 0 )
        {
          v6 = 0;
          while( 1 )
          {
            while( 1 )
            {
              if( v0 )
                WdipSemFreeScenario((_WDIP_SEM_SCENARIO_INFO *)v0);
              v7 = WdipSemFastAllocate(WDI_SEM_ALLOC_SCENARIO, 0x4A0ui64);
              v0 = (INT64)v7;
              if( !v7 )
              {
                v2 = -1073741670;
                goto LABEL_36;
              }
              memset(v7, 0i64, 0x4A0u);
              *(_BYTE *)(v0 + 1176) = v5;
              NextScenario = WdipSemLoadNextScenario(
                               *(VOID **)&DestinationString[0].Length,
                               v6++,
                               (_WDIP_SEM_SCENARIO_INFO *)v0,
                               v8,
                               (INT64)Length,
                               (INT64)ResultLength,
                               v19,
                               *(INT64 *)KeyHandle);
              v10 = NextScenario;
              if( NextScenario == -2147483622 )
                goto LABEL_3;
              if( NextScenario >= 0 )
                break;
              if( EtwEventEnabled(*(&ExBootDevicesRemovedEvent + 560), &WDI_SEM_EVENT_INIT_MISCONFIG) )
                WdipSemWriteMisconfigEvent((_GUID *)v0, *(_WORD *)(v0 + 16), v10);
            }
            if( *(&ExBootDevicesRemovedEvent + 1112) >= 0x40u )
              break;
            v11 = OldNumProviders;
            if( WdipSemUpdateProviderTableWithScenario((_WDIP_SEM_SCENARIO_INFO *)v0) < 0
              || *(_DWORD *)(v0 + 52) && WdipSemAddScenarioToTable((_WDIP_SEM_SCENARIO_INFO *)v0) < 0 )
            {
              WdipSemRollBackProviderTable(v11);
            }
            else
            {
              WdipSemUpdateProviderEntriesForScenario((_WDIP_SEM_SCENARIO_INFO *)v0);
              if( *(_DWORD *)(v0 + 52) )
                v0 = 0i64;
            }
          }
          if( EtwEventEnabled(*(&ExBootDevicesRemovedEvent + 560), &WDI_SEM_EVENT_INIT_SCENARIO_MAX) )
            WdipSemWriteScenarioLimitExceededEvent(
              (const in6_addr *)&WDI_SEM_EVENT_INIT_SCENARIO_MAX,
              v0,
              *(_WORD *)(v0 + 16));
        }
      }
      Length = &v22;
      if( WdipSemQueryValueFromRegistry(KeyHandle[0], L"ScenarioExecutionEnabled", 4i64) < 0 || v22 )
      {
        Length = &v23;
        if( WdipSemQueryValueFromRegistry(KeyHandle[0], L"ScenarioExecutionRestrictions", 4i64) < 0
          || (v23 & 1) == 0
          || (memset(&VersionInfo, 0i64, sizeof(VersionInfo)),
              VersionInfo.wSuiteMask = 16,
              v1 = VerSetConditionMask(v1, 0x40ui64, 6u),
              RtlVerifyVersionInfo(&VersionInfo, 0x40ui64, v1) < 0) )
        {
          ResultLength = &v19;
          Length = (int *)&v24;
          if( WdipSemQueryValueFromRegistry(KeyHandle[0], L"ScenarioTimeoutEnabled", 4i64) >= 0 )
            v5 = (_DWORD)v24 != 0;
          goto LABEL_16;
        }
      }
    }
  }
LABEL_36:
  if( Key )
  {
    ZwClose((_HANDLE)Key);
    Key = 0i64;
  }
  if( Handle )
  {
    ZwClose((_HANDLE)Handle);
    Handle = 0i64;
  }
  if( *(_QWORD *)KeyHandle )
  {
    ZwClose(KeyHandle[0]);
    *(_QWORD *)KeyHandle = 0i64;
  }
  if( *(_QWORD *)&DestinationString[0].Length )
    ZwClose(*(_HANDLE *)&DestinationString[0].Length);
  if( v0 )
    WdipSemFreeScenario((_WDIP_SEM_SCENARIO_INFO *)v0);
  return v2;
}

Referenced by:

WdipSemCleanStart