Newer
Older
AMI-Aptio-BIOS-Reversed / LenovoServerPkg / SetupDefaults / SetupDefaultLoad / SetupDefaultLoadGpio.c
unsigned __int64 __fastcall GetGpioPadOwnership(unsigned int a1, __int64 a2, __int64 a3, char a4, int *a5)
{
  unsigned int pad; // ebx
  unsigned __int64 group; // rdi
  __int64 gpio_table; // rsi
  _QWORD gpio_group_count[5]; // [rsp+20h] [rbp-28h] BYREF
  int ownership; // [rsp+68h] [rbp+20h] BYREF

  LOBYTE(ownership) = a4; /*0x5a32*/
  pad = (unsigned __int16)a1; /*0x5a41*/
  group = BYTE2(a1); /*0x5a4a*/
  if ( !IsSupportedPchSku(a1) )
  {
    DebugPrint(
      0x80000000LL,
      "GPIO ERROR: Incorrect GpioPad define used on this chipset (Group=%d, Pad=%d)!\n",
      (unsigned int)group,
      pad);
    return 0x8000000000000003uLL; /*0x5a77*/
  }
  gpio_table = GetGpioInfoTable(gpio_group_count); /*0x5a86*/
  if ( group >= gpio_group_count[0] )
  {
    DebugPrint(0x80000000LL, "GPIO ERROR: Group argument (%d) exceeds GPIO group range\n", (unsigned int)group);
    return 0x8000000000000002uLL; /*0x5ab1*/
  }
  if ( pad >= *(_DWORD *)(gpio_table + 60 * group + 56) )
  {
    DebugPrint(0x80000000LL, "GPIO ERROR: Pin number (%d) exceeds possible range for this group\n", pad);
    return 0x8000000000000002uLL; /*0x5ac7*/
  }
  GetGpioPadConfigWord(a1, &ownership); /*0x5ad1*/
  if ( ownership )
  {
    DebugPrint(0x80000000LL, "GPIO ERROR: Accessing pad not owned by host (Group=%d, Pad=%d)!\n", (unsigned int)group, pad);
    return 0x8000000000000003uLL; /*0x5ae4*/
  }
  *a5 = *(_DWORD *)((unsigned __int16)(*(_DWORD *)(gpio_table + 60 * group + 52) + 8 * pad)
                  | ((*(unsigned __int8 *)(gpio_table + 60 * group) | 0xFD00LL) << 16))
      & 2;
  return 0; /*0x5b1d*/
}

__int64 __fastcall GetGpioPadConfig(int a1, _DWORD *a2, int a3, int a4)
{
  __int64 v5; // rax
  __int64 v6; // rdi

  v5 = GetGpioPadOwnership(a1, (_DWORD)a2, a3, a4, (__int64)a2); /*0x5b3a*/
  v6 = v5; /*0x5b3f*/
  if ( v5 < 0 ) /*0x5b45*/
  {
    DebugPrint(0x80000000LL, "\nASSERT_EFI_ERROR (Status = %r)\n", v5); /*0x5b56*/
    DebugAssert(
      (__int64)"e:\\hs\\PurleySktPkg\\SouthClusterLbg\\Library\\PeiDxeSmmGpioLib\\GpioLib.c",
      935,
      (__int64)"!EFI_ERROR (Status)");
  }
  *a2 >>= 1; /*0x5b73*/
  return v6; /*0x5b7d*/
}

unsigned __int64 __fastcall GetGpioPadConfigWord(int a1, unsigned int *a2)
{
  unsigned int v2; // ebx
  unsigned __int64 v3; // rdi
  _DWORD *v5; // rax
  __int64 n1389; // rdx
  unsigned __int64 v8; // [rsp+40h] [rbp+18h] BYREF

  v2 = (unsigned __int16)a1; /*0x5b95*/
  v3 = BYTE2(a1); /*0x5ba0*/
  v5 = GetGpioInfoTable(&v8); /*0x5ba6*/
  if ( v3 >= v8 )
  {
    DebugPrint(0x80000000LL, "GPIO ERROR: Group argument (%d) exceeds GPIO group range\n", v3);
    n1389 = 1389; /*0x5bcb*/
LABEL_3:
    DebugAssert(
      (__int64)"e:\\hs\\PurleySktPkg\\SouthClusterLbg\\Library\\PeiDxeSmmGpioLib\\GpioLib.c",
      n1389,
      (__int64)"((BOOLEAN)(0==1))");
    return 0x8000000000000002uLL; /*0x5bed*/
  }
  if ( v2 >= v5[15 * (unsigned int)v3 + 14] )
  {
    DebugPrint(0x80000000LL, "GPIO ERROR: Pin number (%d) exceeds possible range for this group\n", v2);
    n1389 = 1398; /*0x5c0d*/
    goto LABEL_3; /*0x5c12*/
  }
  *a2 = (*(_DWORD *)(((LOBYTE(v5[15 * (unsigned int)v3]) | 0xFD00LL) << 16)
                   | (unsigned __int16)(LOWORD(v5[15 * (unsigned int)v3 + 1]) + 4 * (v2 >> 3)))
       & (unsigned int)(3 << (4 * (v2 & 7)))) >> (4 * (v2 & 7));
  return 0; /*0x5c5b*/
}

void *__fastcall GetGpioInfoTable(_QWORD *a1)
{
  int n2; // ebx
  __int64 v3; // rax
  unsigned __int16 v4; // ax

  n2 = n2_7; /*0x5c6e*/
  if ( n2_7 == 2 )
  {
    v3 = GetLpcDeviceId(); /*0x5c7c*/
    v4 = ReadUnaligned16(v3 + 2); /*0x5c85*/
    if ( ((v4 + 24128) & 0xFF70) != 0 )
    {
      DebugPrint(0x80000000LL, "Unsupported PCH SKU, LpcDeviceId: 0x%04x!\n", v4);
      DebugAssert(
        (__int64)"e:\\hs\\PurleySktPkg\\SouthClusterLbg\\Library\\PeiDxeSmmPchInfoLib\\PchInfoLib.c",
        290,
        (__int64)"((BOOLEAN)(0==1))");
    }
    else
    {
      n2 = 1; /*0x5cca*/
    }
    n2_7 = n2; /*0x5ccf*/
  }
  if ( n2 == 1 ) /*0x5cd8*/
  {
    *a1 = 13; /*0x5cda*/
    return &unk_7600; /*0x5ce1*/
  }
  else
  {
    *a1 = 0; /*0x5cea*/
    return 0; /*0x5cee*/
  }
}

bool __fastcall IsSupportedPchSku(int a1)
{
  int n2; // eax

  n2 = GetPchSku(); /*0x5d04*/
  if ( n2 == 1 ) /*0x5d0c*/
    return (a1 & 0xFF000000) == 0x1000000; /*0x5d14*/
  if ( n2 != 2 ) /*0x5d23*/
    return 0; /*0x5d23*/
  return (a1 & 0xFF000000) == 0x2000000; /*0x5d35*/
}

__int64 __fastcall ReadUnaligned16(unsigned __int16 *a1)
{
  if ( ((unsigned __int8)a1 & 1) != 0 ) /*0x5d48*/
    DebugAssert((__int64)"e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLib.c", 151, (__int64)"(Address & 1) == 0"); /*0x5d5d*/
  return *a1; /*0x5d65*/
}

__int64 __fastcall WriteUnaligned16(_WORD *a1)
{
  if ( ((unsigned __int8)a1 & 1) != 0 ) /*0x5d78*/
    DebugAssert((__int64)"e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLib.c", 183, (__int64)"(Address & 1) == 0"); /*0x5d8d*/
  *a1 = 1280; /*0x5d97*/
  return 1280; /*0x5d9a*/
}

__int64 GetPchRevision()
{
  unsigned __int8 v0; // al
  char n3; // al
  char n3_1; // cl
  __int64 result; // rax

  v0 = __inbyte(0x70u); /*0x5da5*/
  __outbyte(0x70u, v0 & 0x80 | 0x4B); /*0x5daa*/
  n3 = __inbyte(0x71u); /*0x5db0*/
  n3_1 = n3; /*0x5db1*/
  if ( (unsigned __int8)n3 > 3u ) /*0x5db8*/
  {
    n3_1 = n3; /*0x5dba*/
    if ( !n3 ) /*0x5dc2*/
      n3_1 = MEMORY[0xFDAF0490] & 2 | 1; /*0x5dce*/
  }
  if ( (unsigned __int8)(n3_1 - 1) > 0xFDu ) /*0x5dd8*/
    return 0; /*0x5deb*/
  result = 2147483718LL; /*0x5ddd*/
  if ( n3_1 == 1 ) /*0x5de7*/
    return 2147483652LL; /*0x5de7*/
  return result; /*0x5dea*/
}

__int64 GetPcdProtocol()
{
  __int64 result; // rax
  __int64 v1; // rax

  result = qword_79F8; /*0x5df4*/
  if ( !qword_79F8 ) /*0x5dfe*/
  {
    v1 = (*(__int64 (__fastcall **)(void *, _QWORD, __int64 *))(qword_7930 + 320))(&unk_73D0, 0, &qword_79F8); /*0x5e17*/
    if ( v1 < 0 ) /*0x5e20*/
    {
      DebugPrint(0x80000000LL, "\nASSERT_EFI_ERROR (Status = %r)\n", v1); /*0x5e31*/
      DebugAssert((__int64)"e:\\hs\\MdePkg\\Library\\DxePcdLib\\DxePcdLib.c", 78, (__int64)"!EFI_ERROR (Status)"); /*0x5e49*/
    }
    result = qword_79F8; /*0x5e4e*/
    if ( !qword_79F8 ) /*0x5e58*/
    {
      DebugAssert((__int64)"e:\\hs\\MdePkg\\Library\\DxePcdLib\\DxePcdLib.c", 79, (__int64)"mPcd != ((void *) 0)"); /*0x5e6b*/
      return qword_79F8; /*0x5e70*/
    }
  }
  return result; /*0x5e77*/
}