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

char *
BaseCopyMem (
  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 *
BaseZeroMem (
  void         *buf,
  unsigned int count
  )
{
  memset (buf, 0, count);
  return buf;
}

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

int
BaseSetMem32 (
  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 *
BaseSetMem (
  void         *buf,
  unsigned int count,
  int          value
  )
{
  memset32 (buf, value, count);
  return buf;
}

EFI_STATUS
PcdPeimEntryPoint (
  EFI_HANDLE        PeiServices,
  EFI_SYSTEM_TABLE  *PeiServicesTbl
  )
{
  int         ServicesTbl;
  int         Status1;
  int         DebugLib;
  int         PeiServicesTable;
  int         Status2;
  EFI_STATUS  Status2_1;
  int         DebugLibPtr;

  PeiPcdInit (PeiServices);
  ServicesTbl = GetPeiServicesTable ();
  Status1 = (*(int ( **)(int, void *))(*(_DWORD *)ServicesTbl + 24))(ServicesTbl, &unk_FFE4D230);
  if (Status1 < 0) {
    DebugPrint (0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", Status1);
    DebugLib = DebugGetDebugLib ();
    if (DebugLib) {
      (*(void ( **)(const char *, int, const char *))(DebugLib + 4)) (
        "e:\\hs\\MdeModulePkg\\Universal\\PCD\\Pei\\Pcd.c",
        158,
        "!EFI_ERROR (Status)"
        );
    }
  }

  PeiServicesTable = GetPeiServicesTable ();
  Status2 = (*(int ( **)(int, void *))(*(_DWORD *)PeiServicesTable + 24))(PeiServicesTable, &unk_FFE4D218);
  Status2_1 = Status2;
  if (Status2 < 0) {
    DebugPrint (0x80000000, "\nASSERT_EFI_ERROR (Status = %r)\n", Status2);
    DebugLibPtr = DebugGetDebugLib ();
    if (DebugLibPtr) {
      (*(void ( **)(const char *, int, const char *))(DebugLibPtr + 4)) (
        "e:\\hs\\MdeModulePkg\\Universal\\PCD\\Pei\\Pcd.c",
        164,
        "!EFI_ERROR (Status)"
        );
    }
  }

  return Status2_1;
}

int
PeiPcdGet8Ppi (
  unsigned int a1,
  int         *a2
  )
{
  return PeiPcdGetInfo (0, a1, a2);
}

int
PeiPcdGet32Ppi (
  int          a1,
  unsigned int a2,
  int         *a3
  )
{
  return PeiPcdGetInfo (a1, a2, a3);
}

int
PeiPcdGetSkuId (
  VOID
  )
{
  return *(_DWORD *)(PeiPcdGetPcdDb () + 24);
}

int
PeiPcdSetSku (
  int SkuId
  )
{
  int          SkuId_1;
  _DWORD      *PcdDatabase;
  unsigned int SkuIndex;
  int          SkuId_2;
  unsigned int *SkuTable;
  unsigned int TableFlag;
  unsigned int SkuIndex_1;
  _DWORD      *SkuEntry;

  SkuId_1 = SkuId;
  PcdDatabase = (_DWORD *)PeiPcdGetPcdDb ();
  SkuIndex = 0;
  SkuId_2 = PcdDatabase[6];
  if (SkuId != SkuId_2 || PcdDatabase[7]) {
    if (*((_QWORD *)PcdDatabase + 3)) {
      DebugPrint (0x80000000, "PcdPei - The SKU Id could be changed only once.");
      DebugPrint (
        0x80000000,
        "PcdPei - The SKU Id was set to 0x%lx already, it could not be set to 0x%lx any more.",
        PcdDatabase[6],
        PcdDatabase[7]
        );
      SkuId_2 = DebugGetDebugLib ();
      if (SkuId_2) {
        return (*(int ( **)(const char *, int, const char *))(SkuId_2 + 4)) (
          "e:\\hs\\MdeModulePkg\\Universal\\PCD\\Pei\\Pcd.c",
          281,
          "((BOOLEAN)(0==1))"
          );
      }
    } else {
      SkuTable = (_DWORD *)((char *)PcdDatabase + PcdDatabase[14]);
      TableFlag = SkuTable[1];
      SkuIndex_1 = *SkuTable;
      if (TableFlag || SkuIndex_1) {
        for (SkuEntry = SkuTable + 2; ; SkuEntry += 2) {
          if (SkuId_1 == *SkuEntry) {
            SkuId_1 = SkuId;
            if (!SkuEntry[1]) {
              break;
            }
          }
          ++SkuIndex;
          if (!TableFlag && SkuIndex >= SkuIndex_1) {
            return DebugPrint (64, "PcdPei - Invalid input SkuId, the default SKU Id will be still used.\n");
          }
        }
        SkuId_2 = DebugPrint (64, "PcdPei - Set current SKU Id to 0x%lx.\n", SkuId);
        PcdDatabase[6] = SkuId;
        PcdDatabase[7] = 0;
      } else {
        return DebugPrint (64, "PcdPei - Invalid input SkuId, the default SKU Id will be still used.\n");
      }
    }
  }
  return SkuId_2;
}

char
PeiPcdGet8 (
  unsigned int a1
  )
{
  return *(_BYTE *)PeiPcdGetTokenValue (a1, 1u);
}

__int16
PeiPcdGet16 (
  unsigned int a1
  )
{
  int TokenValue;
  int DebugLib;

  TokenValue = PeiPcdGetTokenValue (a1, 2u);
  if (!TokenValue) {
    DebugLib = DebugGetDebugLib ();
    if (DebugLib) {
      (*(void ( **)(const char *, int, const char *))(DebugLib + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseLib\\Unaligned.c",
        38,
        "Buffer != ((void *) 0)"
        );
    }
  }
  return *(_WORD *)TokenValue;
}

int
PeiPcdGet32 (
  unsigned int a1
  )
{
  int TokenValue;
  int DebugLib;

  TokenValue = PeiPcdGetTokenValue (a1, 4u);
  if (!TokenValue) {
    DebugLib = DebugGetDebugLib ();
    if (DebugLib) {
      (*(void ( **)(const char *, int, const char *))(DebugLib + 4)) (
        "e:\\hs\\MdePkg\\Library\\BaseLib\\Unaligned.c",
        141,
        "Buffer != ((void *) 0)"
        );
    }
  }
  return *(_DWORD *)TokenValue;
}

