Newer
Older
AMI-Aptio-BIOS-Reversed / AmiCRBPkg / Chipset / SB / SBPEI / SBPEI.c
@Ajax Dong Ajax Dong 7 days ago 11 KB Recovering names
//
// SBPEI.efi - Decompiled source cleanup
//

#include <Uefi.h>
#include <PiPei.h>

char *
internal_memcpy (
  char *dst,
  char *src,
  unsigned int count
  )
{
  unsigned int count_1;
  char *dst_1;
  char *src_1;

  count_1 = count;
  if (src < dst && &src[count - 1] >= dst) {
    src_1 = &src[count - 1];
    dst_1 = &dst[count - 1];
  } else {
    count_1 = count & 3;
    qmemcpy (dst, src, 4 * (count >> 2));
    src_1 = &src[4 * (count >> 2)];
    dst_1 = &dst[4 * (count >> 2)];
  }

  qmemcpy (dst_1, src_1, count_1);
  return dst;
}

void *
internal_memset (
  void *buf,
  unsigned int count,
  char value
  )
{
  memset (buf, value, count);
  return buf;
}

int
fn_addr (
  int a1,
  int a2,
  int a3,
  int a4
  )
{
  do {
    *(_DWORD *)(a1 + 8 * a2 - 8) = a3;
    *(_DWORD *)(a1 + 8 * a2-- - 4) = a4;
  } while (a2);

  return a1;
}

void *
internal_memset32 (
  void *buf,
  unsigned int count,
  int value
  )
{
  memset32 (buf, value, count);
  return buf;
}

EFI_STATUS
EFIAPI
ModuleEntryPoint (
  EFI_HANDLE ImageHandle,
  EFI_SYSTEM_TABLE *SystemTable
  )
{
  (*(void (**)(EFI_SYSTEM_TABLE *, int, int, _DWORD, _DWORD, _DWORD))(LODWORD (SystemTable->Hdr.Signature) + 88)) (
    SystemTable,
    1,
    397312,
    0,
    0,
    0
    );

  InitializeSbPeiEnvironment ();
  if ((*(int (**)(EFI_SYSTEM_TABLE *, void *))(LODWORD (SystemTable->Hdr.Signature) + 24)) (
        SystemTable,
        &unk_FFE309D4
        ) < 0)
  {
    DebugAssert (
      -1,
      SystemTable,
      "ASSERT in %s on %i: %s\n",
      "e:\\hs\\AmiCRBPkg\\Chipset\\SB\\SBPEI.c",
      239,
      "!EFI_ERROR(Status)"
      );
    while (1)
      ;
  }

  return 0;
}

char
InitializeSbPeiEnvironment (
  VOID
  )
{
  unsigned __int8 n0x12;
  int n128_1;
  __int16 v2;
  unsigned __int8 n0xA0;
  unsigned __int8 n0x64;
  int v5;
  int v7;
  int n128;
  _DWORD v9[4];

  __outbyte (0x70u, 8u);
  n0x12 = __inbyte (0x71u);
  if (n0x12 > 0x12u) {
    __outbyte (0x70u, 8u);
    __outbyte (0x71u, 0);
  }

  n128_1 = (unsigned __int16)ReadPortWordChecked (0x404u);
  n128 = n128_1;
  if ((n128_1 & 0x80u) != 0) {
    v2 = ReadPortWordChecked (0x406u);
    v9[3] = 0;
    v9[2] = 512;
    v9[1] = 0;
    v9[0] = 1024000;
    GetMmioBaseFromResourceList (0, 0, v9, &v7);
    if ((ReadIoWordChecked ((unsigned __int16 *)(v7 + 164)) & 4) == 0) {
      __outbyte (0x70u, 0x32u);
      n0xA0 = __inbyte (0x71u);
      if (n0xA0 < 0xA0u && (n0xA0 & 0xFu) < 0xA) {
        n0x64 = BcdByteToBinary (n0xA0) + 1;
        if (n0x64 >= 0x64u) {
          v5 = GetPeiServicesTable ();
          if (v5) {
            (*(void (**)(const char *, int, const char *))(v5 + 4)) (
              "e:\\hs\\MdePkg\\Library\\BaseLib\\String.c",
              1785,
              "Value < 100"
              );
          }
        }
        __outbyte (0x70u, 0x32u);
        __outbyte (0x71u, (n0x64 % 0xAu) | (16 * (n0x64 / 0xAu)));
      }
    }

    LOBYTE (n128_1) = 0x80;
    __outbyte (0x404u, 0x80u);
    if ((_WORD)n128 == 128 && !v2) {
      __outdword (0x534u, 0x2000u);
      LOBYTE (n128_1) = 0;
    }
  }

  return n128_1;
}

int
GetSbSataDeviceAddress (
  int a1,
  _BYTE *a2,
  _BYTE *a3,
  _BYTE *a4,
  _BYTE *a5
  )
{
  *a3 = a2[9];
  *a4 = a2[10];
  *a5 = a2[11];
  return 0;
}

int
RecInitSbSataController (
  int a1,
  unsigned __int8 *a2,
  int a3
  )
{
  int v3;
  int v4;
  void *v5;
  int v6;
  unsigned __int16 *v7;
  unsigned __int16 v8;
  void *v10;

  PeiDebugPrint (64, "\nSbPei.c : \nRecInitSbSataController");
  v3 = ((a2[11] & 7 | (8 * (a2[10] & 0x1F | (32 * a2[9])))) << 12) | 0x9E;
  v4 = PcdGet16FromProtocolValue (v10);
  LOBYTE (v5) = *(_BYTE *)(v4 + v3) & 0xFE;
  *(_BYTE *)(v4 + v3) = (_BYTE)v5;
  v6 = ((a2[11] & 7 | (8 * (a2[10] & 0x1F | (32 * a2[9])))) << 12) | 4;
  v7 = (unsigned __int16 *)(PcdGet16FromProtocolValue (v5) + v6);
  v8 = ReadIoWordChecked (v7);
  WriteIoWordChecked (v7, v8 | 6);
  *(_DWORD *)(a3 + 12) = -1;
  return 0;
}

int
ReadPortWordChecked (
  unsigned __int16 n1028
  )
{
  int v2;
  int result;

  if ((n1028 & 1) != 0) {
    v2 = GetPeiServicesTable ();
    if (v2) {
      (*(void (**)(const char *, int, const char *))(v2 + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLibMsc.c",
        133,
        "(Port & 1) == 0"
        );
    }
  }

  LOWORD (result) = __inword (n1028);
  return (unsigned __int16)result;
}

int
ReadIoWordChecked (
  unsigned __int16 *a1
  )
{
  int v2;

  if (((unsigned __int8)a1 & 1) != 0) {
    v2 = GetPeiServicesTable ();
    if (v2) {
      (*(void (**)(const char *, int, const char *))(v2 + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLib.c",
        151,
        "(Address & 1) == 0"
        );
    }
  }

  return *a1;
}

__int16
WriteIoWordChecked (
  _WORD *a1,
  __int16 a2
  )
{
  int v4;

  if (((unsigned __int8)a1 & 1) != 0) {
    v4 = GetPeiServicesTable ();
    if (v4) {
      (*(void (**)(const char *, int, const char *))(v4 + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLib.c",
        183,
        "(Address & 1) == 0"
        );
    }
  }

  *a1 = a2;
  return a2;
}

int
BcdByteToBinary (
  unsigned __int8 n0xA0
  )
{
  int v2;
  int v3;

  if (n0xA0 >= 0xA0u) {
    v2 = GetPeiServicesTable ();
    if (v2) {
      (*(void (**)(const char *, int, const char *))(v2 + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseLib\\String.c",
        1809,
        "Value < 0xa0"
        );
    }
  }

  if ((n0xA0 & 0xFu) >= 0xA) {
    v3 = GetPeiServicesTable ();
    if (v3) {
      (*(void (**)(const char *, int, const char *))(v3 + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseLib\\String.c",
        1810,
        "(Value & 0xf) < 0xa"
        );
    }
  }

  return (n0xA0 & 0xF) + 10 * (n0xA0 >> 4);
}

int
GetPeiServicesTable (
  VOID
  )
{
  int v0;
  int v2;
  int v3;

  v0 = GetPeiServices ();
  if ((*(int (**)(int, void *, _DWORD, int *, int *))(*(_DWORD *)v0 + 32))(v0, &unk_FFE30998, 0, &v2, &v3) >= 0) {
    return v3;
  }

  return 0;
}

int
PeiDebugPrint (
  int n64,
  char *_nSbPei.c_:__nRecInitSbSataController,
  ...
  )
{
  int result;
  int (**v3)(int, char *, char *);
  va_list va;

  va_start (va, _nSbPei.c_:__nRecInitSbSataController);
  result = GetPeiServicesTable ();
  v3 = (int (**)(int, char *, char *))result;
  if (result) {
    result = ReadRtcRegisterB ();
    if ((result & n64) != 0) {
      return (*v3)(n64, _nSbPei.c_:__nRecInitSbSataController, (char *)va);
    }
  }

  return result;
}

int
PeiAssert (
  int e:__hs__MdePkg__Library__PeiServicesTablePointerLibIdt__PeiServ,
  int n48,
  int PeiServices____((void__)_0)
  )
{
  int result;

  result = GetPeiServicesTable ();
  if (result) {
    return (*(int (**)(int, int, int))(result + 4)) (
      e:__hs__MdePkg__Library__PeiServicesTablePointerLibIdt__PeiServ,
      n48,
      PeiServices____((void__)_0)
      );
  }

  return result;
}

int
GetPcdProtocol (
  void *this
  )
{
  int v1;
  int v2;
  int v3;
  int v5;

  v1 = GetPeiServices ();
  v5 = (int)&v5;
  v2 = (*(int (**)(int, void *, _DWORD, _DWORD))(*(_DWORD *)v1 + 32))(v1, &unk_FFE309A8, 0, 0);
  if (v2 < 0) {
    PeiDebugPrint (0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", v2);
    v3 = GetPeiServicesTable ();
    if (v3) {
      (*(void (**)(const char *, int, const char *))(v3 + 4)) (
        "e:\\hs\\MdePkg\\Library\\PeiPcdLib\\PeiPcdLib.c",
        49,
        "!EFI_ERROR (Status)"
        );
    }
  }

  return v5;
}

int
PcdGet16FromProtocol (
  void *this
  )
{
  int v2;

  v2 = GetPcdProtocol (this);
  return (*(int (**)(void *))(v2 + 16))(this);
}

int
PcdGet32FromProtocol (
  void *this
  )
{
  int v2;

  v2 = GetPcdProtocol (this);
  return (*(int (**)(void *))(v2 + 20))(this);
}

int
DebugAssert (
  int a1,
  EFI_SYSTEM_TABLE *SystemTable,
  char *ASSERT_in_%s_on_%i:_%s_n,
  ...
  )
{
  int result;
  char *ASSERT_in_%s_on_%i:_%s_n_1;
  va_list va;

  va_start (va, ASSERT_in_%s_on_%i:_%s_n);
  result = (*(int (__stdcall **)(EFI_SYSTEM_TABLE *))(LODWORD (SystemTable->Hdr.Signature) + 32))(SystemTable);
  if (result >= 0) {
    if (SystemTable) {
      result = ReadRtcRegisterB ();
      if ((result & a1) != 0) {
        ASSERT_in_%s_on_%i:_%s_n_1 = ASSERT_in_%s_on_%i:_%s_n;
        if (*ASSERT_in_%s_on_%i:_%s_n) {
          do {
            if (*ASSERT_in_%s_on_%i:_%s_n_1 == 37) {
              if (*++ASSERT_in_%s_on_%i:_%s_n_1 == 115) {
                *ASSERT_in_%s_on_%i:_%s_n_1 = 97;
              } else if (*ASSERT_in_%s_on_%i:_%s_n_1 == 71) {
                *ASSERT_in_%s_on_%i:_%s_n_1 = 103;
              }
            }
            ++ASSERT_in_%s_on_%i:_%s_n_1;
          } while (*ASSERT_in_%s_on_%i:_%s_n_1);
          ASSERT_in_%s_on_%i:_%s_n_1 = ASSERT_in_%s_on_%i:_%s_n;
        }

        return ((int (*)(int, char *, char *))LODWORD (SystemTable->Hdr.Signature)) (
          a1,
          ASSERT_in_%s_on_%i:_%s_n_1,
          (char *)va
          );
      }
    }
  }

  return result;
}

int
PcdGet16FromProtocolValue (
  void *this
  )
{
  int v1;

  v1 = GetPcdProtocol (this);
  return (*(int (**)(int))(v1 + 16))(5);
}

int *
GetMmioBaseFromResourceList (
  int a1,
  int a2,
  _DWORD *a3,
  int *a4
  )
{
  int v4;

  v4 = ResolvePcdResourceOffset ((int)a3) + (*a3 & 0xFFFFFFF);
  *a4 = v4;
  return a4;
}

int
ReadRtcRegisterB (
  VOID
  )
{
  unsigned __int8 v0;
  char n3;
  char n3_1;

  v0 = __inbyte (0x70u);
  __outbyte (0x70u, v0 & 0x80 | 0x4A);
  n3 = __inbyte (0x71u);
  n3_1 = n3;
  if ((unsigned __int8)n3 <= 3u) {
LABEL_4:
    if (!n3_1) {
      return 0;
    }
    goto LABEL_5;
  }

  n3_1 = n3;
  if (!n3) {
    n3_1 = MEMORY[0xFDAF0490] & 2 | 1;
    goto LABEL_4;
  }

LABEL_5:
  if (n3_1 != -1) {
    return n3_1 != 1 ? -2147483578 : -2147483644;
  }

  return 0;
}

int
InitializePcdProtocolCache (
  _DWORD *a1,
  int n8
  )
{
  int v3;

  v3 = PcdGet32FromProtocol ((void *)6);
  a1[1] = 16 * n8 + 16;
  if (!a1[4] && !a1[5]) {
    *(_DWORD *)(v3 + 16) = PcdGet16FromProtocol ((void *)5);
    *(_DWORD *)(v3 + 20) = 0;
  }

  return 0;
}

int
ResolvePcdResourceOffset (
  int a1
  )
{
  int v2;
  int v3;
  int v4;

  if (*(_DWORD *)(a1 + 12)) {
    v3 = *(_DWORD *)(*(_DWORD *)(a1 + 12) + 4 * (unsigned __int16)*(_DWORD *)(a1 + 4) + 255544);
    v4 = *(_DWORD *)(*(_DWORD *)(a1 + 12) + 4 * (unsigned __int16)*(_DWORD *)(a1 + 4) + 255560);
  } else {
    v2 = PcdGet32FromProtocol ((void *)6);
    if (*(_DWORD *)(v2 + 4)) {
      v3 = *(_DWORD *)(v2 + 16 * (unsigned __int16)*(_DWORD *)(a1 + 4) + 20);
      v4 = *(_DWORD *)(v2 + 16 * (unsigned __int16)*(_DWORD *)(a1 + 4) + 16);
    } else {
      InitializePcdProtocolCache (dword_FFE30A18, 8);
      v3 = dword_FFE30A2C[4 * (unsigned __int16)*(_DWORD *)(a1 + 4)];
      v4 = dword_FFE30A28[4 * (unsigned __int16)*(_DWORD *)(a1 + 4)];
      if (!v4 && !v3) {
        v4 = PcdGet16FromProtocol ((void *)5);
        v3 = 0;
      }
    }
  }

  if (!v4 && !v3) {
    return PcdGet16FromProtocol ((void *)5);
  }

  return v4;
}

int
GetPeiServices (
  VOID
  )
{
  int v0;
  _BYTE v2[8];

  ReadIdtr (v2);
  v0 = *(_DWORD *)(*(_DWORD *)&v2[2] - 4);
  if (!v0) {
    PeiAssert (
      (int)"e:\\hs\\MdePkg\\Library\\PeiServicesTablePointerLibIdt\\PeiServicesTablePointer.c",
      48,
      (int)"PeiServices != ((void *) 0)"
      );
  }

  return v0;
}

void *
__thiscall
ReadIdtr (
  void *this
  )
{
  void *this_1;

  if (!this) {
    PeiAssert (
      (int)"e:\\hs\\MdePkg\\Library\\BaseLib\\X86ReadIdtr.c",
      37,
      (int)"Idtr != ((void *) 0)"
      );
  }

  this_1 = this;
  __sidt (this);
  return this_1;
}