Newer
Older
AMI-Aptio-BIOS-Reversed / AmiModulePkg / TCG2 / Common / TcgPlatformSetupPeiPolicy / TcgPlatformSetupPeiPolicy / TcgPlatformSetupPeiPolicy.c
@Ajax Dong Ajax Dong 7 days ago 7 KB Recovering names
#include <Uefi.h>
#include <PiPei.h>

int CompareMem(_BYTE *DestBuffer, _BYTE *a2, int Length)
{
  bool v6;

  do {
    if (!Length) {
      break;
    }
    v6 = *DestBuffer++ == *a2++;
    --Length;
  } while (v6);

  return (unsigned __int8)*(DestBuffer - 1) - (unsigned __int8)*(a2 - 1);
}

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

int CopyMemBackwards(int DstBase, int Count, int LoVal, int HiVal)
{
  do {
    *(_DWORD *)(DstBase + 8 * Count - 8) = LoVal;
    *(_DWORD *)(DstBase + 8 * Count-- - 4) = HiVal;
  } while (Count);

  return DstBase;
}

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

char *CopyMem(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;
}

int GetDebugOutputInterface(void)
{
  int PeiServicesTablePointer;
  _BYTE GuidBuffer[4];
  int DebugInterface;

  PeiServicesTablePointer = GetPeiServicesTablePointer();
  if ((*(int (**)(int, void *, _DWORD, _BYTE *, int *))(*(_DWORD *)PeiServicesTablePointer + 32))(
          PeiServicesTablePointer, &unk_FFE182F4, 0, GuidBuffer, &DebugInterface) >= 0) {
    return DebugInterface;
  }
  return 0;
}

int GetTpmState(void)
{
  unsigned __int8 CmosIndex;
  char TpmStateRead;
  char TpmStateRead_1;

  CmosIndex = __inbyte(0x70u);
  __outbyte(0x70u, CmosIndex & 0x80 | 0x4A);
  TpmStateRead = __inbyte(0x71u);
  TpmStateRead_1 = TpmStateRead;
  if ((unsigned __int8)TpmStateRead <= 3u) {
LABEL_4:
    if (!TpmStateRead_1) {
      return 0;
    }
    goto LABEL_5;
  }
  TpmStateRead_1 = n3;
  if (!n3) {
    TpmStateRead_1 = MEMORY[0xFDAF0490] & 2 | 1;
    goto LABEL_4;
  }
LABEL_5:
  if (TpmStateRead_1 != -1) {
    return TpmStateRead_1 != 1 ? -2147483578 : -2147483644;
  }
  return 0;
}

int DebugPrint(int a1, const char *_nASSERT_EFI_ERROR_(Status___%r)_n, ...)
{
  int DebugInterface_1;
  int (**DebugInterface)(int, const char *, char *);
  va_list va;

  va_start(va, _nASSERT_EFI_ERROR_(Status___%r)_n);
  DebugInterface_1 = GetDebugOutputInterface();
  DebugInterface = (int (**)(int, const char *, char *))DebugInterface_1;
  if (DebugInterface_1) {
    DebugInterface_1 = GetTpmState();
    if ((DebugInterface_1 & a1) != 0) {
      return (*DebugInterface)(a1, _nASSERT_EFI_ERROR_(Status___%r)_n, (char *)va);
    }
  }
  return DebugInterface_1;
}

int DebugOutputString(int e:__hs__MdePkg__Library__PeiServicesTablePointerLibIdt__PeiServ, int n48,
                      int PeiServices____((void__)_0))
{
  int DebugInterface;

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

int GetPeiServicesTablePointer(void)
{
  int PeiServices;
  _BYTE IdtrStorage[2];
  int IdtrBase;

  ReadIdtr(IdtrStorage);
  PeiServices = *(_DWORD *)(IdtrBase - 4);
  if (!PeiServices) {
    DebugOutputString((int)"e:\\hs\\MdePkg\\Library\\PeiServicesTablePointerLibIdt\\PeiServicesTablePointer.c",
                      48, (int)"PeiServices != ((void *) 0)");
  }
  return PeiServices;
}

void *__thiscall ReadIdtr(void *Idtr)
{
  void *Idtr_1;

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

// bad sp value at call has been detected, the output may be wrong!
EFI_STATUS TcgPlatformSetupPolicyEntry(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
  int (**TpmProtocol)();
  int PeiServices;
  int Status;
  int Status_1;
  int DebugOutputInterface;
  _DWORD *PolicyDataPtr;
  char TpmDeviceSelect;
  char TpmIntStsPolarity;
  char TpmDeviceEnable;
  char TpmPtpIrqEnable;
  char TpmSubOwner;
  char Tpm2AcpiRev;
  char TpmClearOwner;
  char TpmIntType;
  char TpmFlags1;
  char TpmFlags2;
  char TpmFlags3;
  char TpmFlags4;
  char TpmInterfaceType;
  char TpmMiscFlags;
  unsigned __int8 TpmTypeBits0;
  unsigned __int8 TpmTypeBits1;
  unsigned __int8 TpmTypeBits2;
  unsigned __int8 TpmTypeBits3;
  unsigned __int8 TpmTypeBits4;
  _DWORD PpiBuffer[2];
  int PolicyData;

  PpiBuffer[1] = 814;
  PeiServices = SystemTable->Hdr.Signature;
  PolicyData = 0;
  if ((*(int (**)(EFI_SYSTEM_TABLE *, void *, _DWORD, _DWORD, _DWORD *))(PeiServices + 32))(SystemTable,
                                                                                             &unk_FFE182E4, 0, 0,
                                                                                             PpiBuffer) < 0 ||
      (*(int (**)(EFI_SYSTEM_TABLE *, int, int, int *))(LODWORD(SystemTable->Hdr.Signature) + 52))(SystemTable, 4, 51,
                                                                                                     &PolicyData) < 0) {
    return 0;
  }

  PpiBuffer[0] = TpmProtocol;
  Status = (*TpmProtocol)();
  Status_1 = Status;
  if (Status < 0) {
    DebugPrint(0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", Status);
    DebugOutputInterface = GetDebugOutputInterface();
    if (DebugOutputInterface) {
      (*(void (**)(const char *, int, const char *))(DebugOutputInterface + 4))(
          "e:\\hs\\AmiModulePkg\\TCG2\\Common\\TcgPlatformSetupPeiPolicy\\TcgPlatformSetupPeiPolicy.c", 213,
          "!EFI_ERROR (Status)");
    }
  }
  if (PolicyData) {
    if (Status_1 >= 0) {
      (*(void (**)(int, int, _DWORD))(LODWORD(SystemTable->Hdr.Signature) + 84))(PolicyData + 24, 27, 0);
      PolicyDataPtr = (_DWORD *)(PolicyData + 8);
      *(_DWORD *)(PolicyData + 8) = *(_DWORD *)byte_FFE18304;
      *++PolicyDataPtr = unk_FFE18308;
      *++PolicyDataPtr = unk_FFE1830C;
      PolicyDataPtr[1] = unk_FFE18310;
      *(_BYTE *)(PolicyData + 24) = TpmDeviceEnable;
      *(_BYTE *)(PolicyData + 25) = TpmDeviceSelect;
      *(_BYTE *)(PolicyData + 26) = Tpm2AcpiRev;
      *(_BYTE *)(PolicyData + 27) = TpmIntStsPolarity;
      *(_BYTE *)(PolicyData + 42) = TpmClearOwner;
      *(_BYTE *)(PolicyData + 29) = TpmIntType;
      *(_BYTE *)(PolicyData + 45) = TpmFlags3;
      *(_BYTE *)(PolicyData + 44) = TpmFlags2;
      *(_BYTE *)(PolicyData + 43) = TpmFlags1;
      *(_BYTE *)(PolicyData + 46) = TpmFlags4;
      *(_BYTE *)(PolicyData + 30) = TpmInterfaceType;
      *(_BYTE *)(PolicyData + 36) = TpmSubOwner;
      *(_BYTE *)(PolicyData + 35) = TpmPtpIrqEnable;
      *(_DWORD *)(PolicyData + 47) =
          TpmTypeBits0 | TpmTypeBits1 | TpmTypeBits2 | TpmTypeBits3 | (16 * TpmTypeBits4);
      *(_BYTE *)(PolicyData + 39) = TpmMiscFlags;
    }
  }
  return (*(int (**)(EFI_SYSTEM_TABLE *, void *))(LODWORD(SystemTable->Hdr.Signature) + 24))(SystemTable,
                                                                                            &unk_FFE18324);
}