]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EdkCompatibilityPkg/Sample/Tools/Source/Common/CommonLib.c
Add in the 1st version of ECP.
[mirror_edk2.git] / EdkCompatibilityPkg / Sample / Tools / Source / Common / CommonLib.c
diff --git a/EdkCompatibilityPkg/Sample/Tools/Source/Common/CommonLib.c b/EdkCompatibilityPkg/Sample/Tools/Source/Common/CommonLib.c
new file mode 100644 (file)
index 0000000..aa2a22e
--- /dev/null
@@ -0,0 +1,497 @@
+/*++\r
+\r
+Copyright (c) 2004, Intel Corporation                                                         \r
+All rights reserved. This program and the accompanying materials                          \r
+are licensed and made available under the terms and conditions of the BSD License         \r
+which accompanies this distribution.  The full text of the license may be found at        \r
+http://opensource.org/licenses/bsd-license.php                                            \r
+                                                                                          \r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+\r
+Module Name:\r
+\r
+  CommonLib.c\r
+\r
+Abstract:\r
+\r
+  Common Library Functions\r
\r
+--*/\r
+\r
+#include "TianoCommon.h"\r
+#include "PeiHob.h"\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <stdlib.h>\r
+#include "CommonLib.h"\r
+\r
+VOID\r
+PeiZeroMem (\r
+  IN VOID   *Buffer,\r
+  IN UINTN  Size\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Set Buffer to zero for Size bytes.\r
+\r
+Arguments:\r
+\r
+  Buffer  - Memory to set.\r
+\r
+  Size    - Number of bytes to set\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  INT8  *Ptr;\r
+\r
+  Ptr = Buffer;\r
+  while (Size--) {\r
+    *(Ptr++) = 0;\r
+  }\r
+}\r
+\r
+VOID\r
+PeiCopyMem (\r
+  IN VOID   *Destination,\r
+  IN VOID   *Source,\r
+  IN UINTN  Length\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Copy Length bytes from Source to Destination.\r
+\r
+Arguments:\r
+\r
+  Destination - Target of copy\r
+\r
+  Source      - Place to copy from\r
+\r
+  Length      - Number of bytes to copy\r
+\r
+Returns:\r
+\r
+  None\r
+\r
+--*/\r
+{\r
+  CHAR8 *Destination8;\r
+  CHAR8 *Source8;\r
+\r
+  Destination8  = Destination;\r
+  Source8       = Source;\r
+  while (Length--) {\r
+    *(Destination8++) = *(Source8++);\r
+  }\r
+}\r
+\r
+VOID\r
+ZeroMem (\r
+  IN VOID   *Buffer,\r
+  IN UINTN  Size\r
+  )\r
+{\r
+  PeiZeroMem (Buffer, Size);\r
+}\r
+\r
+VOID\r
+CopyMem (\r
+  IN VOID   *Destination,\r
+  IN VOID   *Source,\r
+  IN UINTN  Length\r
+  )\r
+{\r
+  PeiCopyMem (Destination, Source, Length);\r
+}\r
+\r
+INTN\r
+CompareGuid (\r
+  IN EFI_GUID     *Guid1,\r
+  IN EFI_GUID     *Guid2\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Compares to GUIDs\r
+\r
+Arguments:\r
+\r
+  Guid1 - guid to compare\r
+  Guid2 - guid to compare\r
+\r
+Returns:\r
+  =  0  if Guid1 == Guid2\r
+  != 0  if Guid1 != Guid2 \r
+\r
+--*/\r
+{\r
+  INT32 *g1;\r
+  INT32 *g2;\r
+  INT32 r;\r
+\r
+  //\r
+  // Compare 32 bits at a time\r
+  //\r
+  g1  = (INT32 *) Guid1;\r
+  g2  = (INT32 *) Guid2;\r
+\r
+  r   = g1[0] - g2[0];\r
+  r |= g1[1] - g2[1];\r
+  r |= g1[2] - g2[2];\r
+  r |= g1[3] - g2[3];\r
+\r
+  return r;\r
+}\r
+\r
+EFI_STATUS\r
+GetFileImage (\r
+  IN CHAR8    *InputFileName,\r
+  OUT CHAR8   **InputFileImage,\r
+  OUT UINT32  *BytesRead\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function opens a file and reads it into a memory buffer.  The function \r
+  will allocate the memory buffer and returns the size of the buffer.\r
+\r
+Arguments:\r
+\r
+  InputFileName     The name of the file to read.\r
+  InputFileImage    A pointer to the memory buffer.\r
+  BytesRead         The size of the memory buffer.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS              The function completed successfully.\r
+  EFI_INVALID_PARAMETER    One of the input parameters was invalid.\r
+  EFI_ABORTED              An error occurred.\r
+  EFI_OUT_OF_RESOURCES     No resource to complete operations.\r
+\r
+--*/\r
+{\r
+  FILE    *InputFile;\r
+  UINT32  FileSize;\r
+\r
+  //\r
+  // Verify input parameters.\r
+  //\r
+  if (InputFileName == NULL || strlen (InputFileName) == 0 || InputFileImage == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  //\r
+  // Open the file and copy contents into a memory buffer.\r
+  //\r
+  //\r
+  // Open the file\r
+  //\r
+  InputFile = fopen (InputFileName, "rb");\r
+  if (InputFile == NULL) {\r
+    printf ("ERROR: Could not open input file \"%s\".\n", InputFileName);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Go to the end so that we can determine the file size\r
+  //\r
+  if (fseek (InputFile, 0, SEEK_END)) {\r
+    printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);\r
+    fclose (InputFile);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Get the file size\r
+  //\r
+  FileSize = ftell (InputFile);\r
+  if (FileSize == -1) {\r
+    printf ("ERROR: System error parsing input file \"%s\".\n", InputFileName);\r
+    fclose (InputFile);\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Allocate a buffer\r
+  //\r
+  *InputFileImage = malloc (FileSize);\r
+  if (*InputFileImage == NULL) {\r
+    fclose (InputFile);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  //\r
+  // Reset to the beginning of the file\r
+  //\r
+  if (fseek (InputFile, 0, SEEK_SET)) {\r
+    printf ("ERROR: System error reading input file \"%s\".\n", InputFileName);\r
+    fclose (InputFile);\r
+    free (*InputFileImage);\r
+    *InputFileImage = NULL;\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Read all of the file contents.\r
+  //\r
+  *BytesRead = fread (*InputFileImage, sizeof (UINT8), FileSize, InputFile);\r
+  if (*BytesRead != sizeof (UINT8) * FileSize) {\r
+    printf ("ERROR: Reading file \"%s\"%i.\n", InputFileName);\r
+    fclose (InputFile);\r
+    free (*InputFileImage);\r
+    *InputFileImage = NULL;\r
+    return EFI_ABORTED;\r
+  }\r
+  //\r
+  // Close the file\r
+  //\r
+  fclose (InputFile);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+UINT8\r
+CalculateChecksum8 (\r
+  IN UINT8        *Buffer,\r
+  IN UINTN        Size\r
+  )\r
+/*++\r
+  \r
+Routine Description:\r
+\r
+  This function calculates the value needed for a valid UINT8 checksum\r
+\r
+Arguments:\r
+\r
+  Buffer      Pointer to buffer containing byte data of component.\r
+  Size        Size of the buffer\r
+\r
+Returns:\r
+\r
+  The 8 bit checksum value needed.\r
+\r
+--*/\r
+{\r
+  return (UINT8) (0x100 - CalculateSum8 (Buffer, Size));\r
+}\r
+\r
+UINT8\r
+CalculateSum8 (\r
+  IN UINT8  *Buffer,\r
+  IN UINT32 Size\r
+  )\r
+/*++\r
+  \r
+Routine Description::\r
+\r
+  This function calculates the UINT8 sum for the requested region.\r
+\r
+Arguments:\r
+\r
+  Buffer      Pointer to buffer containing byte data of component.\r
+  Size        Size of the buffer\r
+\r
+Returns:\r
+\r
+  The 8 bit checksum value needed.\r
+\r
+--*/\r
+{\r
+  UINTN Index;\r
+  UINT8 Sum;\r
+\r
+  Sum = 0;\r
+\r
+  //\r
+  // Perform the byte sum for buffer\r
+  //\r
+  for (Index = 0; Index < Size; Index++) {\r
+    Sum = (UINT8) (Sum + Buffer[Index]);\r
+  }\r
+\r
+  return Sum;\r
+}\r
+\r
+UINT16\r
+CalculateChecksum16 (\r
+  IN UINT16       *Buffer,\r
+  IN UINTN        Size\r
+  )\r
+/*++\r
+  \r
+Routine Description::\r
+\r
+  This function calculates the value needed for a valid UINT16 checksum\r
+\r
+Arguments:\r
+\r
+  Buffer      Pointer to buffer containing byte data of component.\r
+  Size        Size of the buffer\r
+\r
+Returns:\r
+\r
+  The 16 bit checksum value needed.\r
+\r
+--*/\r
+{\r
+  return (UINT16) (0x10000 - CalculateSum16 (Buffer, Size));\r
+}\r
+\r
+UINT16\r
+CalculateSum16 (\r
+  IN UINT16       *Buffer,\r
+  IN UINTN        Size\r
+  )\r
+/*++\r
+  \r
+Routine Description:\r
+\r
+  This function calculates the UINT16 sum for the requested region.\r
+\r
+Arguments:\r
+\r
+  Buffer      Pointer to buffer containing byte data of component.\r
+  Size        Size of the buffer\r
+\r
+Returns:\r
+\r
+  The 16 bit checksum\r
+\r
+--*/\r
+{\r
+  UINTN   Index;\r
+  UINT16  Sum;\r
+\r
+  Sum = 0;\r
+\r
+  //\r
+  // Perform the word sum for buffer\r
+  //\r
+  for (Index = 0; Index < Size; Index++) {\r
+    Sum = (UINT16) (Sum + Buffer[Index]);\r
+  }\r
+\r
+  return (UINT16) Sum;\r
+}\r
+\r
+EFI_STATUS\r
+PrintGuid (\r
+  IN EFI_GUID *Guid\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function prints a GUID to STDOUT.\r
+\r
+Arguments:\r
+\r
+  Guid    Pointer to a GUID to print.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             The GUID was printed.\r
+  EFI_INVALID_PARAMETER   The input was NULL.\r
+\r
+--*/\r
+{\r
+  if (Guid == NULL) {\r
+    printf ("ERROR: PrintGuid called with a NULL value.\n");\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  printf (\r
+    "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",\r
+    Guid->Data1,\r
+    Guid->Data2,\r
+    Guid->Data3,\r
+    Guid->Data4[0],\r
+    Guid->Data4[1],\r
+    Guid->Data4[2],\r
+    Guid->Data4[3],\r
+    Guid->Data4[4],\r
+    Guid->Data4[5],\r
+    Guid->Data4[6],\r
+    Guid->Data4[7]\r
+    );\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+PrintGuidToBuffer (\r
+  IN EFI_GUID     *Guid,\r
+  IN OUT UINT8    *Buffer,\r
+  IN UINT32       BufferLen,\r
+  IN BOOLEAN      Uppercase\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function prints a GUID to a buffer\r
+\r
+Arguments:\r
+\r
+  Guid      - Pointer to a GUID to print.\r
+  Buffer    - Pointer to a user-provided buffer to print to\r
+  BufferLen - Size of the Buffer\r
+  Uppercase - If use upper case.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             The GUID was printed.\r
+  EFI_INVALID_PARAMETER   The input was NULL.\r
+  EFI_BUFFER_TOO_SMALL    The input buffer was not big enough\r
+  \r
+--*/\r
+{\r
+  if (Guid == NULL) {\r
+    printf ("ERROR: PrintGuidToBuffer() called with a NULL value\n");\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (BufferLen < PRINTED_GUID_BUFFER_SIZE) {\r
+    printf ("ERORR: PrintGuidToBuffer() called with invalid buffer size\n");\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  if (Uppercase) {\r
+    sprintf (\r
+      Buffer,\r
+      "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",\r
+      Guid->Data1,\r
+      Guid->Data2,\r
+      Guid->Data3,\r
+      Guid->Data4[0],\r
+      Guid->Data4[1],\r
+      Guid->Data4[2],\r
+      Guid->Data4[3],\r
+      Guid->Data4[4],\r
+      Guid->Data4[5],\r
+      Guid->Data4[6],\r
+      Guid->Data4[7]\r
+      );\r
+  } else {\r
+    sprintf (\r
+      Buffer,\r
+      "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
+      Guid->Data1,\r
+      Guid->Data2,\r
+      Guid->Data3,\r
+      Guid->Data4[0],\r
+      Guid->Data4[1],\r
+      Guid->Data4[2],\r
+      Guid->Data4[3],\r
+      Guid->Data4[4],\r
+      Guid->Data4[5],\r
+      Guid->Data4[6],\r
+      Guid->Data4[7]\r
+      );\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r