//
// TcgPeiplatform.efi - cleaned decompilation
//

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

VOID *
EFIAPI
InternalMemSetMem (
  IN VOID   *Buffer,
  IN UINTN  Count,
  IN CHAR8  Value
  )
{
  return memset (Buffer, Value, Count);
}

VOID *
EFIAPI
InternalMemSetMem32 (
  IN VOID    *Buffer,
  IN UINTN   Count,
  IN UINT32  Value1,
  IN UINT32  Value2
  )
{
  UINT32  *Ptr;

  Ptr = (UINT32 *)Buffer;
  while (Count != 0) {
    *Ptr++ = Value1;
    *Ptr++ = Value2;
    Count--;
  }

  return Buffer;
}

VOID *
EFIAPI
InternalMemZeroMem (
  IN VOID   *Buffer,
  IN UINTN  Count
  )
{
  return memset (Buffer, 0, Count);
}

VOID *
EFIAPI
InternalMemCopyMem (
  OUT VOID        *Destination,
  IN  CONST VOID   *Source,
  IN  UINTN        Count
  )
{
  return memmove (Destination, Source, Count);
}

STATIC
VOID *
EFIAPI
GetDebugOutputProtocol (
  VOID
  )
{
  EFI_PEI_SERVICES  *PeiServices;
  VOID              *DebugOutput;

  PeiServices = GetPeiServicesTable ();
  if (EFI_ERROR (PeiServices->LocatePpi (PeiServices, &gEfiPeiDebugOutputPpiGuid, 0, NULL, &DebugOutput))) {
    return NULL;
  }

  return DebugOutput;
}

INTN
EFIAPI
TpmIsPresent (
  VOID
  )
{
  UINT8  Value;
  UINT8  TpmState;

  Value = __inbyte (0x70);
  __outbyte (0x70, (UINT8)((Value & 0x80) | 0x4A));
  TpmState = __inbyte (0x71);

  if (TpmState <= 3) {
    if (TpmState == 0) {
      return 0;
    }
  } else if (TpmState == 0) {
    TpmState = (UINT8)((MEMORY[0xFDAF0490] & 2) | 1);
    if (TpmState == 0) {
      return 0;
    }
  }

  if (TpmState != 0xFF) {
    return (TpmState != 1) ? EFI_NOT_FOUND : EFI_DEVICE_ERROR;
  }

  return 0;
}

EFI_PEI_SERVICES *
EFIAPI
GetPeiServicesTable (
  VOID
  )
{
  EFI_PEI_SERVICES  *PeiServices;

  PeiServices = NULL;
  AsmReadIdtr (&PeiServices);
  if (PeiServices == NULL) {
    return NULL;
  }

  return PeiServices;
}

VOID
EFIAPI
AsmReadIdtr (
  OUT VOID  *Idtr
  )
{
  if (Idtr == NULL) {
    return;
  }

  __sidt (Idtr);
}

VOID *
EFIAPI
GetPcdProtocol (
  IN VOID  *This
  )
{
  EFI_PEI_SERVICES  *PeiServices;
  EFI_STATUS        Status;

  PeiServices = GetPeiServicesTable ();
  if (PeiServices == NULL) {
    return This;
  }

  Status = PeiServices->LocatePpi (PeiServices, NULL, 0, NULL, NULL);
  if (EFI_ERROR (Status)) {
    DebugPrint (DEBUG_ERROR, "\nASSERT_EFI_ERROR (Status = %r)\n", Status);
  }

  return This;
}

INTN
EFIAPI
GetPcdPpi (
  IN VOID  *This
  )
{
  VOID  *PcdProtocol;

  PcdProtocol = GetPcdProtocol (This);
  return ((INTN (*)(INTN))PcdProtocol)(5);
}

INTN
EFIAPI
PcdSetSku (
  IN VOID  *This
  )
{
  UINTN   Address;
  INTN    DebugOutput;

  Address = (UINTN)GetPcdPpi (This) + 1024064;
  if ((Address & 1) != 0) {
    DebugOutput = (INTN)GetDebugOutputProtocol ();
    if (DebugOutput != 0) {
      ((VOID (*)(CONST CHAR8 *, INTN, CONST CHAR8 *))(DebugOutput + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseIoLibIntrinsic\\IoLib.c",
        183,
        "(Address & 1) == 0"
        );
    }
  }

  *(UINT16 *)Address = 1280;
  return 1280;
}

EFI_STATUS
EFIAPI
GetDebugOutput (
  VOID
  )
{
  EFI_PEI_SERVICES  *PeiServices;
  VOID              *DebugOutput;
  EFI_STATUS        Status;

  PeiServices = GetPeiServicesTable ();
  Status = PeiServices->LocatePpi (PeiServices, &gEfiPeiDebugOutputPpiGuid, 0, NULL, &DebugOutput);
  if (EFI_ERROR (Status)) {
    return 0;
  }

  return (EFI_STATUS)(UINTN)DebugOutput;
}

EFI_STATUS
EFIAPI
DebugPrint (
  IN UINTN        ErrorLevel,
  IN CONST CHAR8  *Format,
  ...
  )
{
  VA_LIST  Va;
  VOID     *DebugOutput;

  VA_START (Va, Format);
  DebugOutput = (VOID *)GetDebugOutput ();
  if (DebugOutput != NULL && (TpmIsPresent () & (INTN)ErrorLevel) != 0) {
    return ((EFI_STATUS (*)(UINTN, CONST CHAR8 *, VA_LIST))(DebugOutput))(ErrorLevel, Format, Va);
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
DebugAssert (
  IN CONST CHAR8  *FileName,
  IN INTN         LineNumber,
  IN CONST CHAR8  *Description
  )
{
  VOID  *DebugOutput;

  DebugOutput = (VOID *)GetDebugOutput ();
  if (DebugOutput != NULL) {
    return ((EFI_STATUS (*)(CONST CHAR8 *, INTN, CONST CHAR8 *))((UINTN)DebugOutput + 4))(FileName, LineNumber, Description);
  }

  return EFI_SUCCESS;
}

EFI_STATUS
EFIAPI
ModuleEntryPoint (
  IN EFI_HANDLE        ImageHandle,
  IN EFI_SYSTEM_TABLE  *SystemTable
  )
{
  EFI_STATUS  Status;
  VOID        *Registration;

  if (*(CHAR8 *)(GetPcdPpi (NULL) + 1024068) >= 0) {
    PcdSetSku (NULL);
    *(UINT8 *)(GetPcdPpi (NULL) + 1024068) |= 0x80;
  }

  Registration = NULL;
  if (SystemTable->BootServices->LocateProtocol (&gEfiPeiDebugOutputPpiGuid, NULL, &Registration) < 0) {
    return 0;
  }

  ((VOID (*)(EFI_SYSTEM_TABLE *))Registration)(SystemTable);

  Status = SystemTable->BootServices->InstallConfigurationTable (
                             SystemTable,
                             &gEfiPeiDebugOutputPpiGuid,
                             ImageHandle
                             );
  if ((Status & EFI_ERROR_MASK) == 0) {
    return SystemTable->BootServices->HandleProtocol (
                               SystemTable,
                               &gEfiPeiDebugOutputPpiGuid
                               );
  }

  return Status;
}
