HalpPiix4Detect

CHAR __fastcall HalpPiix4Detect(CHAR a1){
  int v1; 
  int BusData; 
  CHAR CpuInfo; 
  UINT32 v5; 
  char v6; 
  unsigned int v7; 
  unsigned int v8; 
  int v9; 
  unsigned int v10; 
  int v11; 
  unsigned __int8 v12; 
  int v13; 
  bool v14; 
  unsigned int v15; 
  INT64 v16; 
  HANDLE v17; 
  PUNICODE_STRING Class; 
  PUNICODE_STRING Classa; 
  UINT64 CreateOptions; 
  UINT64 CreateOptionsa; 
  __int16 Buffer; 
  char Buffer_2; 
  int Buffer_4; 
  HANDLE Handle; 
  ULONG Disposition[2]; 
  void *KeyHandle; 
  _QWORD ObjectAttributes[7]; 
  __int64 v30[2]; 
  __int64 v31[2]; 
  __int64 v32[2]; 
  struct _UNICODE_STRING ValueName; 
  __int16 result; 
  __int16 result_2; 
  unsigned __int8 a3; 
  char v37; 
  __int128 KeyValueInformation; 
  int v39; 
  v1 = 0;
  KeyHandle = 0i64;
  Buffer = 0;
  Disposition[0] = 0;
  Buffer_4 = 0;
  Handle = 0i64;
  Buffer_2 = 0;
  LODWORD(ObjectAttributes[0]) = 0;
  memset(&ObjectAttributes[1], 0, 48);
  memset((INT64)&result, 0i64);
  *(_QWORD *)&ValueName.Length = 2490404i64;
  ValueName.Buffer = L"EnableBXWorkAround";
  v32[0] = 3276848i64;
  v32[1] = (__int64)L"Services\\ACPI\\Parameters";
  v31[0] = 1572886i64;
  v31[1] = (__int64)L"Control\\HAL";
  v30[0] = 5636180i64;
  v30[1] = (__int64)L"\\REGISTRY\\MACHINE\\SYSTEM\\CURRENTCONTROLSET";
  LOBYTE(BusData) = 0;
  v39 = 0;
  KeyValueInformation = 0i64;
  if( a1 )
  {
    LODWORD(ObjectAttributes[1]) = 48;
    ObjectAttributes[3] = v30;
    ObjectAttributes[2] = 0i64;
    LODWORD(ObjectAttributes[4]) = 64;
    *(_OWORD *)&ObjectAttributes[5] = 0i64;
    BusData = ZwOpenKey(&KeyHandle, 0x20019u, (POBJECT_ATTRIBUTES)&ObjectAttributes[1]);
    if( BusData < 0 )
      return BusData;
    ObjectAttributes[2] = KeyHandle;
    LODWORD(ObjectAttributes[1]) = 48;
    ObjectAttributes[3] = v31;
    LODWORD(ObjectAttributes[4]) = 64;
    LODWORD(CreateOptions) = 0;
    *(_OWORD *)&ObjectAttributes[5] = 0i64;
    BusData = ZwCreateKey(
                &Handle,
                0x20019ui64,
                (OBJECT_ATTRIBUTES *)&ObjectAttributes[1],
                0i64,
                0i64,
                CreateOptions,
                (UINT64 *)Disposition);
    if( BusData < 0 )
      goto LABEL_75;
  }
  else if( !HalpPiix4Present )
  {
    return BusData;
  }
  CpuInfo = HalpGetCpuInfo(0i64, 0i64, 0i64, (UINT8 *)&Buffer);
  v5 = 0;
  v6 = CpuInfo != 0 ? Buffer : 0;
  LOBYTE(Buffer) = 0;
LABEL_7:
  v7 = 0;
  v8 = 0;
LABEL_8:
  v9 = v8 & 0x1F;
  while( 1 )
  {
    v10 = v7 & 0xFFFFFF00;
    v7 = v9 | v7 & 0xFFFFFF00 | (32 * (v1 & 7));
    BusData = HalGetBusData(PCIConfiguration, v5, v9 | v10 | (32 * (v1 & 7)), &result);
    if( !BusData )
      break;
    LOBYTE(BusData) = result - 1;
    if( (unsigned __int16)(result - 1) > 0xFFFDu )
    {
      v14 = v1 == 0;
LABEL_58:
      if( v14 )
        goto LABEL_60;
      goto LABEL_59;
    }
    BusData = HalGetBusData(PCIConfiguration, v5, v7, &result);
    if( BusData )
    {
      v11 = HalpInterruptModel();
      v12 = a3;
      v13 = 1;
      if( v11 != 1 )
        goto LABEL_20;
      if( !a1 )
        goto LABEL_52;
      if( !v5 && !v8 && !v1 && result == 4358 && a3 < 0x80u )
        HalpInterruptSetMsiOverride(0);
LABEL_20:
      if( !a1 )
        goto LABEL_52;
      if( result == -32634 && ((result_2 - 29072) & 0xFFFD) == 0 && v12 <= 2u )
      {
        LODWORD(CreateOptions) = v13;
        Buffer_2 = v13;
        LODWORD(Class) = 87;
        HalGetBusDataByOffset(4i64, v5, v7, (UINT8 *)&Buffer + 1, (UINT64)Class, CreateOptions);
        v12 = a3;
        if( ((HIBYTE(Buffer) >> 5) & ((Buffer & 0x1800) != 0)) != 0 )
          HalpBroken440BX = 1;
      }
      if( (int)HalpGetChipHacks(result, result_2, v12, &Buffer_4) >= 0 )
      {
        if( (Buffer_4 & 2) != 0 )
          HalpDisableHibernate = 1;
        if( (Buffer_4 & 8) != 0 )
          HalpWhackICHUsbSmi(v5, v7);
      }
      if( v6 != 2 || (_BYTE)Buffer )
        goto LABEL_52;
      LOBYTE(BusData) = -122;
      if( result != -32634 )
        goto LABEL_55;
      if( v5 )
        goto LABEL_53;
      LOWORD(BusData) = result_2;
      if( v8 != 17 )
        goto LABEL_40;
      if( !v1 && result_2 == -26416 )
        goto LABEL_51;
      if( v1 == 1 )
      {
        if( result_2 == -26415 )
          goto LABEL_51;
LABEL_40:
        if( v8 == 16 )
        {
          if( v1 == 6 && result_2 == -24368 || v1 == 7 && result_2 == -24367 )
            goto LABEL_51;
          if( v1 == 6 )
          {
            if( result_2 == 17360 )
            {
              if( a3 )
                goto LABEL_48;
LABEL_51:
              LOBYTE(Buffer) = 1;
              HalpInterruptSetLineSpecificOverride(0x17ui64);
              HalpInterruptSetLineSpecificOverride(0x16ui64);
LABEL_52:
              LOBYTE(BusData) = -122;
              if( result == -32634 )
              {
LABEL_53:
                LOWORD(BusData) = result_2;
                goto LABEL_54;
              }
LABEL_55:
              if( !v1 )
              {
                v14 = v37 >= 0;
                goto LABEL_58;
              }
              goto LABEL_59;
            }
          }
          else
          {
LABEL_48:
            if( v1 == 7 && result_2 == 17361 )
            {
              if( a3 )
                goto LABEL_59;
              goto LABEL_51;
            }
          }
        }
      }
LABEL_54:
      if( (_WORD)BusData == 28944 )
      {
        HalpPiix4Present = 1;
        v15 = v7 & 0xFFFFFF1F | 0x60;
        HalGetBusData(PCIConfiguration, v5, v15, &result);
        LOBYTE(v16) = a3;
        HalpPiix4 = a3;
        if( (HalpWAETDeviceFlags & 2) == 0 )
          KeBugCheckEx(0x5Cu, 0x111ui64, (ULONG_PTR)"minkernel\\hals\\lib\\acpi\\xxacpi.c", 0x66Dui64, 0i64);
        if( a3 <= 1u )
        {
          LODWORD(CreateOptions) = 4;
          LODWORD(Class) = 88;
          HalpPiix4BusNumber = v5;
          HalpPiix4SlotNumber = v15;
          HalGetBusDataByOffset(4i64, v5, v15, &HalpPiix4DevActB, (UINT64)Class, CreateOptions);
          *(_DWORD *)&HalpPiix4DevActB |= 0x23u;
          LODWORD(CreateOptionsa) = 4;
          LODWORD(Classa) = 88;
          HalSetBusDataByOffset(
            4i64,
            HalpPiix4BusNumber,
            HalpPiix4SlotNumber,
            &HalpPiix4DevActB,
            (UINT64)Classa,
            CreateOptionsa);
        }
        LOBYTE(BusData) = HalpUsbLegacyStopUhciInterrupt(v16, v5, v15 & 0xFFFFFF5F, 1);
        break;
      }
      goto LABEL_55;
    }
LABEL_59:
    if( (unsigned int)++v1 >= 8 )
    {
LABEL_60:
      ++v8;
      v1 = 0;
      if( v8 < 0x20 )
        goto LABEL_8;
      if( ++v5 >= 0x100 )
        break;
      goto LABEL_7;
    }
  }
  if( a1 )
  {
    v17 = Handle;
    if( Handle )
    {
      LOBYTE(BusData) = ZwClose(Handle);
      v17 = 0i64;
      Handle = 0i64;
    }
    if( Buffer_2 )
    {
      ObjectAttributes[2] = KeyHandle;
      LODWORD(ObjectAttributes[1]) = 48;
      ObjectAttributes[3] = v32;
      LODWORD(ObjectAttributes[4]) = 64;
      LODWORD(CreateOptions) = 0;
      *(_OWORD *)&ObjectAttributes[5] = 0i64;
      BusData = ZwCreateKey(
                  &Handle,
                  0x20019ui64,
                  (OBJECT_ATTRIBUTES *)&ObjectAttributes[1],
                  0i64,
                  0i64,
                  CreateOptions,
                  (UINT64 *)Disposition);
      if( BusData >= 0 )
      {
        BusData = ZwQueryValueKey(
                    Handle,
                    &ValueName,
                    KeyValuePartialInformation,
                    &KeyValueInformation,
                    0x14u,
                    (PULONG)ObjectAttributes);
        if( BusData >= 0 )
        {
          if( DWORD2(KeyValueInformation) )
          {
            LOBYTE(BusData) = BYTE12(KeyValueInformation);
            HalpBroken440BX = BYTE12(KeyValueInformation);
          }
        }
      }
LABEL_75:
      v17 = Handle;
    }
    if( v17 )
      LOBYTE(BusData) = ZwClose(v17);
    if( KeyHandle )
      LOBYTE(BusData) = ZwClose(KeyHandle);
  }
  return BusData;
}

Referenced by:

HaliInitPowerManagement
HalpAcpiPostSleep