]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OvmfPkg/SecureBootConfigDxe/SecureBootConfigMisc.c
OvmfPkg: Add custom SecureBootConfigDxe that doesn't reset
[mirror_edk2.git] / OvmfPkg / SecureBootConfigDxe / SecureBootConfigMisc.c
diff --git a/OvmfPkg/SecureBootConfigDxe/SecureBootConfigMisc.c b/OvmfPkg/SecureBootConfigDxe/SecureBootConfigMisc.c
new file mode 100644 (file)
index 0000000..13c7c27
--- /dev/null
@@ -0,0 +1,334 @@
+/** @file\r
+  Helper functions for SecureBoot configuration module.\r
+\r
+Copyright (c) 2012, Intel Corporation. All rights reserved.<BR>\r
+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
+**/\r
+\r
+#include "SecureBootConfigImpl.h"\r
+\r
+/**\r
+  Read file content into BufferPtr, the size of the allocate buffer \r
+  is *FileSize plus AddtionAllocateSize.\r
+\r
+  @param[in]       FileHandle            The file to be read.\r
+  @param[in, out]  BufferPtr             Pointers to the pointer of allocated buffer.\r
+  @param[out]      FileSize              Size of input file\r
+  @param[in]       AddtionAllocateSize   Addtion size the buffer need to be allocated. \r
+                                         In case the buffer need to contain others besides the file content.\r
+  \r
+  @retval   EFI_SUCCESS                  The file was read into the buffer.\r
+  @retval   EFI_INVALID_PARAMETER        A parameter was invalid.\r
+  @retval   EFI_OUT_OF_RESOURCES         A memory allocation failed.\r
+  @retval   others                       Unexpected error.\r
+\r
+**/\r
+EFI_STATUS\r
+ReadFileContent (\r
+  IN      EFI_FILE_HANDLE           FileHandle,\r
+  IN OUT  VOID                      **BufferPtr,\r
+     OUT  UINTN                     *FileSize,\r
+  IN      UINTN                     AddtionAllocateSize\r
+  )\r
+\r
+{\r
+  UINTN      BufferSize;\r
+  UINT64     SourceFileSize;\r
+  VOID       *Buffer;\r
+  EFI_STATUS Status;\r
+\r
+  if ((FileHandle == NULL) || (FileSize == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Buffer = NULL;\r
+\r
+  //\r
+  // Get the file size\r
+  //\r
+  Status = FileHandle->SetPosition (FileHandle, (UINT64) -1);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  Status = FileHandle->GetPosition (FileHandle, &SourceFileSize);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+  \r
+  Status = FileHandle->SetPosition (FileHandle, 0);\r
+  if (EFI_ERROR (Status)) {\r
+    goto ON_EXIT;\r
+  }\r
+\r
+  BufferSize = (UINTN) SourceFileSize + AddtionAllocateSize;\r
+  Buffer =  AllocateZeroPool(BufferSize);\r
+  if (Buffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  BufferSize = (UINTN) SourceFileSize;\r
+  *FileSize  = BufferSize;\r
+\r
+  Status = FileHandle->Read (FileHandle, &BufferSize, Buffer);\r
+  if (EFI_ERROR (Status) || BufferSize != *FileSize) {\r
+    FreePool (Buffer);\r
+    Buffer = NULL;\r
+    Status  = EFI_BAD_BUFFER_SIZE;\r
+    goto ON_EXIT;\r
+  }\r
+\r
+ON_EXIT:\r
+  \r
+  *BufferPtr = Buffer;\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Close an open file handle.\r
+\r
+  @param[in] FileHandle           The file handle to close.\r
+  \r
+**/\r
+VOID\r
+CloseFile (\r
+  IN EFI_FILE_HANDLE   FileHandle\r
+  )\r
+{\r
+  if (FileHandle != NULL) {\r
+    FileHandle->Close (FileHandle);  \r
+  }\r
+}\r
+\r
+/**\r
+  Convert a nonnegative integer to an octet string of a specified length.\r
+\r
+  @param[in]   Integer          Pointer to the nonnegative integer to be converted\r
+  @param[in]   IntSizeInWords   Length of integer buffer in words\r
+  @param[out]  OctetString      Converted octet string of the specified length \r
+  @param[in]   OSSizeInBytes    Intended length of resulting octet string in bytes\r
+\r
+Returns:\r
+\r
+  @retval   EFI_SUCCESS            Data conversion successfully\r
+  @retval   EFI_BUFFER_TOOL_SMALL  Buffer is too small for output string\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+Int2OctStr (\r
+  IN     CONST UINTN                *Integer,\r
+  IN     UINTN                      IntSizeInWords,\r
+     OUT UINT8                      *OctetString,\r
+  IN     UINTN                      OSSizeInBytes\r
+  )\r
+{\r
+  CONST UINT8  *Ptr1;\r
+  UINT8        *Ptr2;\r
+\r
+  for (Ptr1 = (CONST UINT8 *)Integer, Ptr2 = OctetString + OSSizeInBytes - 1;\r
+       Ptr1 < (UINT8 *)(Integer + IntSizeInWords) && Ptr2 >= OctetString;\r
+       Ptr1++, Ptr2--) {\r
+    *Ptr2 = *Ptr1;\r
+  }\r
+       \r
+  for (; Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords) && *Ptr1 == 0; Ptr1++);\r
+  \r
+  if (Ptr1 < (CONST UINT8 *)(Integer + IntSizeInWords)) {\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  \r
+  if (Ptr2 >= OctetString) {\r
+    ZeroMem (OctetString, Ptr2 - OctetString + 1);\r
+  }\r
+  \r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+\r
+/**\r
+  Convert a String to Guid Value.\r
+\r
+  @param[in]   Str        Specifies the String to be converted.\r
+  @param[in]   StrLen     Number of Unicode Characters of String (exclusive \0)\r
+  @param[out]  Guid       Return the result Guid value.\r
+\r
+  @retval    EFI_SUCCESS           The operation is finished successfully.\r
+  @retval    EFI_NOT_FOUND         Invalid string.\r
+\r
+**/\r
+EFI_STATUS\r
+StringToGuid (\r
+  IN   CHAR16           *Str, \r
+  IN   UINTN            StrLen, \r
+  OUT  EFI_GUID         *Guid\r
+  )\r
+{\r
+  CHAR16             *PtrBuffer;\r
+  CHAR16             *PtrPosition;\r
+  UINT16             *Buffer;\r
+  UINTN              Data;\r
+  UINTN              Index;\r
+  UINT16             Digits[3];\r
+\r
+  Buffer = (CHAR16 *) AllocateZeroPool (sizeof (CHAR16) * (StrLen + 1));\r
+  if (Buffer == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  StrCpy (Buffer, Str);\r
+\r
+  //\r
+  // Data1\r
+  //\r
+  PtrBuffer       = Buffer;\r
+  PtrPosition     = PtrBuffer; \r
+  while (*PtrBuffer != L'\0') {\r
+    if (*PtrBuffer == L'-') {\r
+      break;\r
+    }\r
+    PtrBuffer++;\r
+  }\r
+  if (*PtrBuffer == L'\0') {\r
+    FreePool (Buffer);\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  *PtrBuffer      = L'\0';\r
+  Data            = StrHexToUintn (PtrPosition);\r
+  Guid->Data1     = (UINT32)Data;\r
+\r
+  //\r
+  // Data2\r
+  //\r
+  PtrBuffer++;\r
+  PtrPosition     = PtrBuffer;\r
+  while (*PtrBuffer != L'\0') {\r
+    if (*PtrBuffer == L'-') {\r
+      break;\r
+    }\r
+    PtrBuffer++;\r
+  }\r
+  if (*PtrBuffer == L'\0') {\r
+    FreePool (Buffer);\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  *PtrBuffer      = L'\0';\r
+  Data            = StrHexToUintn (PtrPosition);\r
+  Guid->Data2     = (UINT16)Data;\r
+\r
+  //\r
+  // Data3\r
+  //\r
+  PtrBuffer++;\r
+  PtrPosition     = PtrBuffer;\r
+  while (*PtrBuffer != L'\0') {\r
+    if (*PtrBuffer == L'-') {\r
+      break;\r
+    }\r
+    PtrBuffer++;\r
+  }\r
+  if (*PtrBuffer == L'\0') {\r
+    FreePool (Buffer);\r
+    return EFI_NOT_FOUND;\r
+  }\r
+  *PtrBuffer      = L'\0';\r
+  Data            = StrHexToUintn (PtrPosition);\r
+  Guid->Data3     = (UINT16)Data;\r
+\r
+  //\r
+  // Data4[0..1]\r
+  //\r
+  for ( Index = 0 ; Index < 2 ; Index++) {\r
+    PtrBuffer++;\r
+    if ((*PtrBuffer == L'\0') || ( *(PtrBuffer + 1) == L'\0')) {\r
+      FreePool (Buffer);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+    Digits[0]     = *PtrBuffer;\r
+    PtrBuffer++;\r
+    Digits[1]     = *PtrBuffer;\r
+    Digits[2]     = L'\0';\r
+    Data          = StrHexToUintn (Digits);\r
+    Guid->Data4[Index] = (UINT8)Data;\r
+  }\r
+\r
+  //\r
+  // skip the '-'\r
+  //\r
+  PtrBuffer++;\r
+  if ((*PtrBuffer != L'-' ) || ( *PtrBuffer == L'\0')) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Data4[2..7]\r
+  //\r
+  for ( ; Index < 8; Index++) {\r
+    PtrBuffer++;\r
+    if ((*PtrBuffer == L'\0') || ( *(PtrBuffer + 1) == L'\0')) {\r
+      FreePool (Buffer);\r
+      return EFI_NOT_FOUND;\r
+    }\r
+    Digits[0]     = *PtrBuffer;\r
+    PtrBuffer++;\r
+    Digits[1]     = *PtrBuffer;\r
+    Digits[2]     = L'\0';\r
+    Data          = StrHexToUintn (Digits);\r
+    Guid->Data4[Index] = (UINT8)Data;\r
+  }\r
+\r
+  FreePool (Buffer);\r
+  \r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Worker function that prints an EFI_GUID into specified Buffer.\r
+\r
+  @param[in]     Guid          Pointer to GUID to print.\r
+  @param[in]     Buffer        Buffer to print Guid into.\r
+  @param[in]     BufferSize    Size of Buffer.\r
+  \r
+  @retval    Number of characters printed.\r
+\r
+**/\r
+UINTN\r
+GuidToString (\r
+  IN  EFI_GUID  *Guid,\r
+  IN  CHAR16    *Buffer,\r
+  IN  UINTN     BufferSize\r
+  )\r
+{\r
+  UINTN Size;\r
+\r
+  Size = UnicodeSPrint (\r
+            Buffer,\r
+            BufferSize, \r
+            L"%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",\r
+            (UINTN)Guid->Data1,                    \r
+            (UINTN)Guid->Data2,\r
+            (UINTN)Guid->Data3,\r
+            (UINTN)Guid->Data4[0],\r
+            (UINTN)Guid->Data4[1],\r
+            (UINTN)Guid->Data4[2],\r
+            (UINTN)Guid->Data4[3],\r
+            (UINTN)Guid->Data4[4],\r
+            (UINTN)Guid->Data4[5],\r
+            (UINTN)Guid->Data4[6],\r
+            (UINTN)Guid->Data4[7]\r
+            );\r
+\r
+  //\r
+  // SPrint will null terminate the string. The -1 skips the null\r
+  //\r
+  return Size - 1;\r
+}\r