Newer
Older
AMI-Aptio-BIOS-Reversed / DxeCore / Misc / Misc.c
@Ajax Dong Ajax Dong 2 days ago 10 KB Init
#include "../uefi_headers/Uefi.h"

// ==================================================================
// DXE Misc: Memory Attributes Table, Properties Table, Memory Protection
// Source: MdeModulePkg/Core/Dxe/Misc/
//   MemoryAttributesTable.c, PropertiesTable.c
// ==================================================================

// PropertiesTableNotify
// PropertiesTableNotify -- Set byte flag indicating properties table change
//
void PropertiesTableNotify()
{
  byte_26468 = 1; /*0xbeec*/
}

// CoreBuildMemoryAttributesTable
// CoreBuildMemoryAttributesTable -- Build memory attributes table via GetMemoryMap + install config
//
void __fastcall CoreBuildMemoryAttributesTable(__int64 a1, __int64 a2, __int64 a3, double a4)
{
  __int64 v4; // rax
  __int64 v5; // rdx
  __int64 v6; // r8
  char *i_1; // rbx
  __int64 v8; // rdi
  unsigned __int64 count_1; // rsi
  unsigned __int64 i_2; // r13
  unsigned __int64 v11; // rax
  unsigned int v12; // edi
  unsigned __int64 i; // r14
  char *__ImageBase; // rbx
  __int64 v15; // rdx
  __int64 v16; // r8
  char *dst; // r12
  char *j; // r15
  __int64 v19; // rax
  __int64 v20; // rdx
  __int64 v21; // r8
  int v22; // [rsp+80h] [rbp+48h] BYREF
  unsigned __int64 n232; // [rsp+88h] [rbp+50h] BYREF
  unsigned __int64 count; // [rsp+90h] [rbp+58h] BYREF
  __int64 v25; // [rsp+98h] [rbp+60h] BYREF

  if ( !byte_263D8 ) /*0x100c2*/
  {
    if ( (qword_260D0 & 1) != 0 ) /*0x100cf*/
    {
      if ( !_ImageBase_3 ) /*0x1010e*/
      {
        v4 = (*((__int64 (__fastcall **)(void *, __int64))buf_0 + 24))(&unk_22590, -1); /*0x10122*/
        if ( v4 < 0 ) /*0x1012b*/
        {
          Assert_3(0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", v4); /*0x10139*/
          Assert_7( /*0x1014d*/
            (__int64)"e:\\hs\\MdeModulePkg\\Core\\Dxe\\Misc\\MemoryAttributesTable.c",
            118,
            (__int64)"!EFI_ERROR (Status)",
            a4);
        }
      }
      n232 = 0; /*0x10156*/
      if ( DxeConfig_26(&n232, 0, &v25, &count, &v22) != 0x8000000000000005uLL ) /*0x1017f*/
        Assert_7( /*0x10190*/
          (__int64)"e:\\hs\\MdeModulePkg\\Core\\Dxe\\Misc\\MemoryAttributesTable.c",
          130,
          (__int64)"Status == ((RETURN_STATUS)(0x8000000000000000ULL | (5)))",
          a4);
      do /*0x101ea*/
      {
        i_1 = Assert_112(n232, v5, v6, a4); /*0x1019e*/
        if ( !i_1 ) /*0x101a4*/
          Assert_7( /*0x101b5*/
            (__int64)"e:\\hs\\MdeModulePkg\\Core\\Dxe\\Misc\\MemoryAttributesTable.c",
            134,
            (__int64)"MemoryMap != ((void *) 0)",
            a4);
        v8 = DxeConfig_26(&n232, i_1, &v25, &count, &v22); /*0x101d7*/
        if ( v8 < 0 ) /*0x101dd*/
          AssertCpuDeadLoop_10((unsigned __int64)i_1, v5, v6, a4); /*0x101e2*/
      }
      while ( v8 == 0x8000000000000005uLL ); /*0x101ea*/
      count_1 = count; /*0x101f2*/
      i_2 = (unsigned __int64)i_1; /*0x101f6*/
      v11 = n232 / count; /*0x101f9*/
      v12 = 0; /*0x101fc*/
      for ( i = n232 / count; v11; --v11 ) /*0x101f9*/
      {
        if ( (unsigned int)(*(_DWORD *)i_1 - 5) <= 1 ) /*0x1020f*/
          ++v12; /*0x10211*/
        i_1 += count; /*0x10213*/
      }
      __ImageBase = Assert_112(count * v12 + 16, n232 % count, v6, a4); /*0x1022b*/
      if ( !__ImageBase ) /*0x10231*/
        Assert_7( /*0x10242*/
          (__int64)"e:\\hs\\MdeModulePkg\\Core\\Dxe\\Misc\\MemoryAttributesTable.c",
          164,
          (__int64)"MemoryAttributesTable != ((void *) 0)",
          a4);
      *((_DWORD *)__ImageBase + 1) = v12; /*0x10247*/
      *(_DWORD *)__ImageBase = 1; /*0x10256*/
      *((_DWORD *)__ImageBase + 2) = count_1; /*0x1025e*/
      *((_DWORD *)__ImageBase + 3) = 0; /*0x10261*/
      Assert_3(0x400000, "MemoryAttributesTable:\n"); /*0x10265*/
      Assert_3(0x400000, "  Version              - 0x%08x\n", *(_DWORD *)__ImageBase); /*0x10276*/
      Assert_3(0x400000, "  NumberOfEntries      - 0x%08x\n", *((_DWORD *)__ImageBase + 1)); /*0x10288*/
      Assert_3(0x400000, "  DescriptorSize       - 0x%08x\n", *((_DWORD *)__ImageBase + 2)); /*0x1029a*/
      dst = __ImageBase + 16; /*0x1029f*/
      for ( j = (char *)i_2; i; --i ) /*0x102a9*/
      {
        if ( (unsigned int)(*(_DWORD *)j - 5) <= 1 ) /*0x102b8*/
        {
          MemConfig_1(dst, j, count_1, a4); /*0x102c7*/
          *((_QWORD *)dst + 4) &= 0x8000000000024000uLL; /*0x102dd*/
          Assert_3(0x400000, "Entry (0x%x)\n", (_DWORD)dst); /*0x102e8*/
          Assert_3(0x400000, "  Type              - 0x%x\n", *(_DWORD *)dst); /*0x102fb*/
          Assert_3(0x400000, "  PhysicalStart     - 0x%016lx\n", *((_QWORD *)dst + 1)); /*0x1030f*/
          Assert_3(0x400000, "  VirtualStart      - 0x%016lx\n", *((_QWORD *)dst + 2)); /*0x10323*/
          Assert_3(0x400000, "  NumberOfPages     - 0x%016lx\n", *((_QWORD *)dst + 3)); /*0x10337*/
          Assert_3(0x400000, "  Attribute         - 0x%016lx\n", *((_QWORD *)dst + 4)); /*0x1034b*/
          dst += count_1; /*0x10350*/
        }
        j += count_1; /*0x10353*/
      }
      AssertCpuDeadLoop_10(i_2, v15, v16, a4); /*0x10363*/
      v19 = (*((__int64 (__fastcall **)(void *, char *))buf_0 + 24))(&unk_22590, __ImageBase); /*0x10379*/
      if ( v19 < 0 ) /*0x10382*/
      {
        Assert_3(0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", v19); /*0x10393*/
        Assert_7( /*0x103ab*/
          (__int64)"e:\\hs\\MdeModulePkg\\Core\\Dxe\\Misc\\MemoryAttributesTable.c",
          199,
          (__int64)"!EFI_ERROR (Status)",
          a4);
      }
      if ( _ImageBase_3 ) /*0x103ba*/
        AssertCpuDeadLoop_10(_ImageBase_3, v20, v21, a4); /*0x103bc*/
      _ImageBase_3 = (unsigned __int64)__ImageBase; /*0x103c1*/
    }
    else
    {
      Assert_3(0x400000, "MemoryProtectionAttribute NON_EXECUTABLE_PE_DATA is not set, "); /*0x100df*/
      Assert_3(0x400000, "because Runtime Driver Section Alignment is not %dK.\n", 4); /*0x100f1*/
    }
  }
}

// CoreMemoryAttributesTableCallback
// CoreMemoryAttributesTableCallback -- MemoryAttributesTable callback wrapper
//
void __fastcall CoreMemoryAttributesTableCallback(__int64 a1, __int64 a2, __int64 a3, double a4)
{
  CoreBuildMemoryAttributesTable(a1, a2, a3, a4); /*0x103e0*/
  byte_264B0 = 1; /*0x103e5*/
}

// DebugPrintHexBytes
// DebugPrintHexBytes -- Print hex bytes helper for debug output
//
unsigned __int64 __fastcall DebugPrintHexBytes(unsigned __int64 a1, __int64 a2, __int64 a3, double a4)
{
  unsigned int n16; // edx
  __int64 n10_1; // rdi
  __int64 n10; // rbx
  __int64 n10_2; // rcx
  unsigned __int64 n10_3; // rax
  int v10; // [rsp+38h] [rbp+10h] BYREF

  if ( !qword_263C8 ) /*0x1046c*/
    return 0xA000000000000002uLL; /*0x10478*/
  n16 = *(_DWORD *)(qword_263C8 + 8); /*0x1048c*/
  if ( a1 <= 0x1999999999999999LL ) /*0x1048f*/
  {
    n10_3 = Assert_44(10 * a1, n16, &v10, a4); /*0x104be*/
    if ( v10 ) /*0x104c8*/
      ++n10_3; /*0x104ca*/
    n10_2 = n10_3; /*0x104cd*/
  }
  else
  {
    n10_1 = Assert_44(a1, n16, &v10, a4); /*0x10496*/
    n10 = 10; /*0x10499*/
    do /*0x104aa*/
    {
      sub_103FC(n10_1); /*0x104a1*/
      --n10; /*0x104a6*/
    }
    while ( n10 ); /*0x104aa*/
    if ( !v10 ) /*0x104b0*/
      return 0; /*0x104b0*/
    n10_2 = 10; /*0x104b2*/
  }
  sub_103FC(n10_2); /*0x104d0*/
  return 0; /*0x104dc*/
}

// CoreGetMemoryMapWithAttributes
// CoreGetMemoryMapWithAttributes -- Get memory map with attribute manipulation
//
__int64 __fastcall CoreGetMemoryMapWithAttributes(
        __int64 a1,
        _QWORD *a2,
        unsigned __int64 *p_p___ImageBase,
        __int64 n16,
        char *src)
{
  char *src_1; // rsi
  __int64 p___ImageBase; // r8
  __int64 v9; // rdx
  unsigned __int64 v11; // rdi
  __int64 result; // rax
  unsigned __int64 n16_3; // rax
  char *v14; // rsi
  unsigned __int64 v15; // rdx
  unsigned __int64 n16_4; // rcx
  __int64 n16_2; // [rsp+30h] [rbp-10h] BYREF
  unsigned __int64 v18; // [rsp+38h] [rbp-8h] BYREF
  unsigned __int64 n16_5; // [rsp+70h] [rbp+30h] BYREF
  __int64 n16_1; // [rsp+88h] [rbp+48h] BYREF

  n16_1 = n16; /*0x104e9*/
  src_1 = src; /*0x104fc*/
  p___ImageBase = *p_p___ImageBase; /*0x10503*/
  v9 = *a2; /*0x10509*/
  n16_2 = n16; /*0x1050f*/
  v11 = 0; /*0x10513*/
  result = (*(__int64 (__fastcall **)(__int64, __int64, __int64, __int64 *, char *))(a1 + 32))( /*0x1051e*/
             a1,
             v9,
             p___ImageBase,
             &n16_2,
             src);
  if ( result ) /*0x10524*/
  {
    if ( result != 0x8000000000000004uLL ) /*0x1053f*/
      return result; /*0x1053f*/
    n16_3 = n16_1 - n16_2; /*0x10549*/
    v14 = &src_1[n16_2]; /*0x1054d*/
    ++*a2; /*0x10551*/
    n16_1 = n16_3; /*0x10554*/
    while ( n16_3 ) /*0x1055b*/
    {
      result = (*(__int64 (__fastcall **)(__int64, _QWORD, unsigned __int64 *, unsigned __int64 *))(a1 + 24))( /*0x1056f*/
                 a1,
                 *a2,
                 &n16_5,
                 &v18);
      if ( result < 0 ) /*0x10576*/
        return result; /*0x10576*/
      v15 = v18; /*0x1057c*/
      v11 = 0; /*0x10580*/
      n16_3 = n16_1; /*0x10582*/
      n16_4 = n16_5; /*0x10586*/
      if ( v18 ) /*0x1058d*/
      {
        while ( n16_3 >= n16_4 ) /*0x10592*/
        {
          result = (*(__int64 (__fastcall **)(__int64, unsigned __int64, _QWORD, unsigned __int64 *, char *))(a1 + 32))( /*0x105a9*/
                     a1,
                     v11 + *a2,
                     0,
                     &n16_5,
                     v14);
          if ( result < 0 ) /*0x105b0*/
            return result; /*0x105b0*/
          n16_4 = n16_5; /*0x105b2*/
          ++v11; /*0x105b6*/
          v14 += n16_5; /*0x105bd*/
          v15 = v18; /*0x105c0*/
          n16_3 = n16_1 - n16_5; /*0x105c4*/
          n16_1 -= n16_5; /*0x105c7*/
          if ( v11 >= v18 ) /*0x105ce*/
            goto LABEL_11; /*0x105ce*/
        }
LABEL_13:
        if ( n16_3 ) /*0x105e0*/
        {
          result = (*(__int64 (__fastcall **)(__int64, unsigned __int64, _QWORD, __int64 *, char *))(a1 + 32))( /*0x105f7*/
                     a1,
                     v11 + *a2,
                     0,
                     &n16_1,
                     v14);
          if ( result < 0 ) /*0x105fe*/
            return result; /*0x105fe*/
          n16_3 = n16_1; /*0x10600*/
        }
        break; /*0x10600*/
      }
LABEL_11:
      if ( n16_3 < n16_4 ) /*0x105d3*/
        goto LABEL_13; /*0x105d3*/
      *a2 += v15; /*0x105d5*/
    }
    *a2 += v11; /*0x10604*/
    *p_p___ImageBase = n16_3; /*0x10607*/
  }
  else
  {
    *p_p___ImageBase += n16_1; /*0x1052a*/
  }
  return 0; /*0x10611*/
}