]> git.proxmox.com Git - mirror_edk2.git/blobdiff - SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.c
SecurityPkg: SecureBootVariableLib: Added unit tests
[mirror_edk2.git] / SecurityPkg / Library / SecureBootVariableLib / UnitTest / MockUefiLib.c
diff --git a/SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.c b/SecurityPkg/Library/SecureBootVariableLib/UnitTest/MockUefiLib.c
new file mode 100644 (file)
index 0000000..df271c3
--- /dev/null
@@ -0,0 +1,201 @@
+/** @file\r
+  The UEFI Library provides functions and macros that simplify the development of\r
+  UEFI Drivers and UEFI Applications.  These functions and macros help manage EFI\r
+  events, build simple locks utilizing EFI Task Priority Levels (TPLs), install\r
+  EFI Driver Model related protocols, manage Unicode string tables for UEFI Drivers,\r
+  and print messages on the console output and standard error devices.\r
+\r
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiRuntimeServicesTableLib.h>\r
+\r
+/**\r
+  Returns the status whether get the variable success. The function retrieves\r
+  variable  through the UEFI Runtime Service GetVariable().  The\r
+  returned buffer is allocated using AllocatePool().  The caller is responsible\r
+  for freeing this buffer with FreePool().\r
+\r
+  If Name  is NULL, then ASSERT().\r
+  If Guid  is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetVariable2 (\r
+  IN CONST CHAR16    *Name,\r
+  IN CONST EFI_GUID  *Guid,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
+\r
+  ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
+\r
+  //\r
+  // Try to get the variable size.\r
+  //\r
+  BufferSize = 0;\r
+  *Value     = NULL;\r
+  if (Size != NULL) {\r
+    *Size = 0;\r
+  }\r
+\r
+  Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer to get the variable.\r
+  //\r
+  *Value = AllocatePool (BufferSize);\r
+  ASSERT (*Value != NULL);\r
+  if (*Value == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Get the variable data.\r
+  //\r
+  Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, NULL, &BufferSize, *Value);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (*Value);\r
+    *Value = NULL;\r
+  }\r
+\r
+  if (Size != NULL) {\r
+    *Size = BufferSize;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/** Return the attributes of the variable.\r
+\r
+  Returns the status whether get the variable success. The function retrieves\r
+  variable  through the UEFI Runtime Service GetVariable().  The\r
+  returned buffer is allocated using AllocatePool().  The caller is responsible\r
+  for freeing this buffer with FreePool().  The attributes are returned if\r
+  the caller provides a valid Attribute parameter.\r
+\r
+  If Name  is NULL, then ASSERT().\r
+  If Guid  is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[in]  Guid  The pointer to an EFI_GUID structure\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+  @param[out] Attr  The pointer to the variable attributes as found in var store\r
+\r
+  @retval EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @retval EFI_SUCCESS               Find the specified variable.\r
+  @retval Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetVariable3 (\r
+  IN CONST CHAR16    *Name,\r
+  IN CONST EFI_GUID  *Guid,\r
+  OUT VOID           **Value,\r
+  OUT UINTN          *Size OPTIONAL,\r
+  OUT UINT32         *Attr OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
+\r
+  ASSERT (Name != NULL && Guid != NULL && Value != NULL);\r
+\r
+  //\r
+  // Try to get the variable size.\r
+  //\r
+  BufferSize = 0;\r
+  *Value     = NULL;\r
+  if (Size != NULL) {\r
+    *Size = 0;\r
+  }\r
+\r
+  if (Attr != NULL) {\r
+    *Attr = 0;\r
+  }\r
+\r
+  Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
+  if (Status != EFI_BUFFER_TOO_SMALL) {\r
+    return Status;\r
+  }\r
+\r
+  //\r
+  // Allocate buffer to get the variable.\r
+  //\r
+  *Value = AllocatePool (BufferSize);\r
+  ASSERT (*Value != NULL);\r
+  if (*Value == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Get the variable data.\r
+  //\r
+  Status = gRT->GetVariable ((CHAR16 *)Name, (EFI_GUID *)Guid, Attr, &BufferSize, *Value);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (*Value);\r
+    *Value = NULL;\r
+  }\r
+\r
+  if (Size != NULL) {\r
+    *Size = BufferSize;\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Returns a pointer to an allocated buffer that contains the contents of a\r
+  variable retrieved through the UEFI Runtime Service GetVariable().  This\r
+  function always uses the EFI_GLOBAL_VARIABLE GUID to retrieve variables.\r
+  The returned buffer is allocated using AllocatePool().  The caller is\r
+  responsible for freeing this buffer with FreePool().\r
+\r
+  If Name is NULL, then ASSERT().\r
+  If Value is NULL, then ASSERT().\r
+\r
+  @param[in]  Name  The pointer to a Null-terminated Unicode string.\r
+  @param[out] Value The buffer point saved the variable info.\r
+  @param[out] Size  The buffer size of the variable.\r
+\r
+  @return EFI_OUT_OF_RESOURCES      Allocate buffer failed.\r
+  @return EFI_SUCCESS               Find the specified variable.\r
+  @return Others Errors             Return errors from call to gRT->GetVariable.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetEfiGlobalVariable2 (\r
+  IN CONST CHAR16  *Name,\r
+  OUT VOID         **Value,\r
+  OUT UINTN        *Size OPTIONAL\r
+  )\r
+{\r
+  return GetVariable2 (Name, &gEfiGlobalVariableGuid, Value, Size);\r
+}\r