]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: implement LockBoxLib
authorLaszlo Ersek <lersek@redhat.com>
Tue, 4 Mar 2014 08:03:23 +0000 (08:03 +0000)
committerjljusten <jljusten@6f19259b-4bc3-4df7-8a09-765794883524>
Tue, 4 Mar 2014 08:03:23 +0000 (08:03 +0000)
The S3 suspend/resume infrastructure depends on the LockBox library class.
The edk2 tree currently contains Null and SMM instances. The Null instance
is useless, and the SMM instance would require SMM emulation by including
the SMM core and adding several new drivers, which is deemed too complex.

Hence add a simple LockBoxLib instance for OVMF.

jordan.l.justen@intel.com:
 * use PCDs instead of EmuNvramLib
   - clear memory in PlatformPei on non S3 boots
 * allocate NVS memory and store a pointer to that memory
   - reduces memory use at fixed locations

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15301 6f19259b-4bc3-4df7-8a09-765794883524

15 files changed:
OvmfPkg/Library/LockBoxLib/LockBoxBase.c [new file with mode: 0644]
OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf [new file with mode: 0644]
OvmfPkg/Library/LockBoxLib/LockBoxDxe.c [new file with mode: 0644]
OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf [new file with mode: 0644]
OvmfPkg/Library/LockBoxLib/LockBoxLib.c [new file with mode: 0644]
OvmfPkg/Library/LockBoxLib/LockBoxLib.h [new file with mode: 0644]
OvmfPkg/OvmfPkg.dec
OvmfPkg/OvmfPkgIa32.dsc
OvmfPkg/OvmfPkgIa32.fdf
OvmfPkg/OvmfPkgIa32X64.dsc
OvmfPkg/OvmfPkgIa32X64.fdf
OvmfPkg/OvmfPkgX64.dsc
OvmfPkg/OvmfPkgX64.fdf
OvmfPkg/PlatformPei/MemDetect.c
OvmfPkg/PlatformPei/PlatformPei.inf

diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxBase.c b/OvmfPkg/Library/LockBoxLib/LockBoxBase.c
new file mode 100644 (file)
index 0000000..8f36770
--- /dev/null
@@ -0,0 +1,42 @@
+/** @file\r
+\r
+  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+\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 <Uefi.h>\r
+\r
+#include <Library/DebugLib.h>\r
+#include <LockBoxLib.h>\r
+\r
+/**\r
+  Allocates a buffer of type EfiACPIMemoryNVS.\r
+\r
+  Allocates the number bytes specified by AllocationSize of type\r
+  EfiACPIMemoryNVS and returns a pointer to the allocated buffer.\r
+  If AllocationSize is 0, then a valid buffer of 0 size is\r
+  returned.  If there is not enough memory remaining to satisfy\r
+  the request, then NULL is returned.\r
+\r
+  @param  AllocationSize        The number of bytes to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocateAcpiNvsPool (\r
+  IN UINTN  AllocationSize\r
+  )\r
+{\r
+  ASSERT_EFI_ERROR (RETURN_UNSUPPORTED);\r
+  return NULL;\r
+}\r
diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf b/OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
new file mode 100644 (file)
index 0000000..7203d07
--- /dev/null
@@ -0,0 +1,44 @@
+## @file\r
+#\r
+#  Library implementing the LockBox interface for OVMF\r
+#\r
+#  Copyright (C) 2013, Red Hat, Inc.\r
+#  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials are licensed and made available\r
+#  under the terms and conditions of the BSD License which accompanies this\r
+#  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\r
+#  IMPLIED.\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = LockBoxBaseLib\r
+  FILE_GUID                      = 17CA9B37-5BAB-492C-A09C-7121FBE34CE6\r
+  MODULE_TYPE                    = BASE\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = LockBoxLib\r
+\r
+  CONSTRUCTOR                    = LockBoxLibInitialize\r
+\r
+[Sources]\r
+  LockBoxBase.c\r
+  LockBoxLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseMemoryLib\r
+  DebugLib\r
+\r
+[Pcd]\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxDxe.c b/OvmfPkg/Library/LockBoxLib/LockBoxDxe.c
new file mode 100644 (file)
index 0000000..c8f2ec8
--- /dev/null
@@ -0,0 +1,119 @@
+/** @file\r
+\r
+  Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+\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 <Uefi.h>\r
+\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <LockBoxLib.h>\r
+\r
+/**\r
+  Allocate memory below 4G memory address.\r
+\r
+  This function allocates memory below 4G memory address.\r
+\r
+  @param  MemoryType   Memory type of memory to allocate.\r
+  @param  Size         Size of memory to allocate.\r
+  \r
+  @return Allocated address for output.\r
+\r
+**/\r
+STATIC\r
+VOID *\r
+AllocateMemoryBelow4G (\r
+  IN EFI_MEMORY_TYPE    MemoryType,\r
+  IN UINTN              Size\r
+  )\r
+{\r
+  UINTN                 Pages;\r
+  EFI_PHYSICAL_ADDRESS  Address;\r
+  EFI_STATUS            Status;\r
+  VOID*                 Buffer;\r
+  UINTN                 AllocRemaining;\r
+\r
+  Pages = EFI_SIZE_TO_PAGES (Size);\r
+  Address = 0xffffffff;\r
+\r
+  //\r
+  // Since we need to use gBS->AllocatePages to get a buffer below\r
+  // 4GB, there is a good chance that space will be wasted for very\r
+  // small allocation. We keep track of unused portions of the page\r
+  // allocations, and use these to allocate memory for small buffers.\r
+  //\r
+  ASSERT (mLockBoxGlobal->Signature == LOCK_BOX_GLOBAL_SIGNATURE);\r
+  if ((UINTN) mLockBoxGlobal->SubPageRemaining >= Size) {\r
+    Buffer = (VOID*)(UINTN) mLockBoxGlobal->SubPageBuffer;\r
+    mLockBoxGlobal->SubPageBuffer += (UINT32) Size;\r
+    mLockBoxGlobal->SubPageRemaining -= (UINT32) Size;\r
+    return Buffer;\r
+  }\r
+\r
+  Status  = gBS->AllocatePages (\r
+                   AllocateMaxAddress,\r
+                   MemoryType,\r
+                   Pages,\r
+                   &Address\r
+                   );\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
+\r
+  Buffer = (VOID *) (UINTN) Address;\r
+  ZeroMem (Buffer, EFI_PAGES_TO_SIZE (Pages));\r
+\r
+  AllocRemaining = EFI_PAGES_TO_SIZE (Pages) - Size;\r
+  if (AllocRemaining > (UINTN) mLockBoxGlobal->SubPageRemaining) {\r
+    mLockBoxGlobal->SubPageBuffer = (UINT32) (Address + Size);\r
+    mLockBoxGlobal->SubPageRemaining = (UINT32) AllocRemaining;\r
+  }\r
+\r
+  return Buffer;\r
+}\r
+\r
+\r
+/**\r
+  Allocates a buffer of type EfiACPIMemoryNVS.\r
+\r
+  Allocates the number bytes specified by AllocationSize of type\r
+  EfiACPIMemoryNVS and returns a pointer to the allocated buffer.\r
+  If AllocationSize is 0, then a valid buffer of 0 size is\r
+  returned.  If there is not enough memory remaining to satisfy\r
+  the request, then NULL is returned.\r
+\r
+  @param  AllocationSize        The number of bytes to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocateAcpiNvsPool (\r
+  IN UINTN  AllocationSize\r
+  )\r
+{\r
+  return AllocateMemoryBelow4G (EfiACPIMemoryNVS, AllocationSize);\r
+}\r
+\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+LockBoxDxeLibInitialize (\r
+  IN EFI_HANDLE        ImageHandle,\r
+  IN EFI_SYSTEM_TABLE  *SystemTable\r
+  )\r
+{\r
+  return LockBoxLibInitialize ();\r
+}\r
diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf b/OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf
new file mode 100644 (file)
index 0000000..a4d27a5
--- /dev/null
@@ -0,0 +1,45 @@
+## @file\r
+#\r
+#  Library implementing the LockBox interface for OVMF\r
+#\r
+#  Copyright (C) 2013, Red Hat, Inc.\r
+#  Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+#\r
+#  This program and the accompanying materials are licensed and made available\r
+#  under the terms and conditions of the BSD License which accompanies this\r
+#  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\r
+#  IMPLIED.\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION                    = 0x00010005\r
+  BASE_NAME                      = LockBoxDxeLib\r
+  FILE_GUID                      = f61c9a34-2e18-44ce-af2f-21a998e64fda\r
+  MODULE_TYPE                    = DXE_DRIVER\r
+  VERSION_STRING                 = 1.0\r
+  LIBRARY_CLASS                  = LockBoxLib\r
+\r
+  CONSTRUCTOR                    = LockBoxDxeLibInitialize\r
+\r
+[Sources]\r
+  LockBoxDxe.c\r
+  LockBoxLib.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  BaseMemoryLib\r
+  DebugLib\r
+  UefiBootServicesTableLib\r
+\r
+[Pcd]\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxLib.c b/OvmfPkg/Library/LockBoxLib/LockBoxLib.c
new file mode 100644 (file)
index 0000000..af3bb7f
--- /dev/null
@@ -0,0 +1,376 @@
+/** @file\r
+\r
+  Library implementing the LockBox interface for OVMF\r
+\r
+  Copyright (C) 2013, Red Hat, Inc.\r
+  Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
+\r
+  This program and the accompanying materials are licensed and made available\r
+  under the terms and conditions of the BSD License which accompanies this\r
+  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, WITHOUT\r
+  WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseMemoryLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/LockBoxLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <LockBoxLib.h>\r
+\r
+#pragma pack(1)\r
+typedef struct {\r
+  EFI_GUID             Guid;\r
+  EFI_PHYSICAL_ADDRESS OrigAddress;\r
+  EFI_PHYSICAL_ADDRESS CopyAddress;\r
+  UINT32               Size;\r
+  UINT64               Attributes;\r
+} LOCK_BOX_ENTRY;\r
+#pragma pack()\r
+\r
+LOCK_BOX_GLOBAL *mLockBoxGlobal = NULL;\r
+STATIC LOCK_BOX_ENTRY *StartOfEntries = NULL;\r
+STATIC LOCK_BOX_ENTRY *EndOfEntries = NULL;\r
+\r
+RETURN_STATUS\r
+EFIAPI\r
+LockBoxLibInitialize (\r
+  VOID\r
+  )\r
+{\r
+  UINTN NumEntries;\r
+\r
+  if (PcdGet32 (PcdOvmfLockBoxStorageSize) < sizeof (LOCK_BOX_GLOBAL)) {\r
+    return RETURN_UNSUPPORTED;\r
+  }\r
+\r
+  mLockBoxGlobal = (LOCK_BOX_GLOBAL *)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase);\r
+  StartOfEntries = ((LOCK_BOX_ENTRY *) (mLockBoxGlobal + 1));\r
+  NumEntries = ((PcdGet32 (PcdOvmfLockBoxStorageSize) - sizeof (LOCK_BOX_GLOBAL)) /\r
+                sizeof (LOCK_BOX_ENTRY));\r
+  EndOfEntries = StartOfEntries + NumEntries;    \r
+  if (mLockBoxGlobal->Signature != LOCK_BOX_GLOBAL_SIGNATURE) {\r
+    //\r
+    // Note: This code depends on the lock box being cleared in early\r
+    // PEI before usage, so the SubPageBuffer and SubPageRemaining\r
+    // fields don't need to be set to 0.\r
+    //\r
+    mLockBoxGlobal->Signature = LOCK_BOX_GLOBAL_SIGNATURE;\r
+  }\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Find LockBox entry based on GUID.\r
+\r
+  @param[in] Guid  The GUID to search for.\r
+\r
+  @return  Address of the LOCK_BOX_ENTRY found.\r
+\r
+           If NULL, then the item was not found, and there is no space\r
+           left to store a new item.\r
+\r
+           If non-NULL and LOCK_BOX_ENTRY.Size == 0, then the item was not\r
+           found, but a new item can be inserted at the returned location.\r
+\r
+           If non-NULL and LOCK_BOX_ENTRY.Size > 0, then the item was found.\r
+**/\r
+STATIC\r
+LOCK_BOX_ENTRY *\r
+EFIAPI\r
+FindHeaderByGuid (\r
+  IN CONST EFI_GUID *Guid\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+\r
+  for (Header = StartOfEntries; Header < EndOfEntries; Header++) {\r
+    if (Header->Size == 0 || CompareGuid (Guid, &Header->Guid)) {\r
+      return Header;\r
+    }\r
+  }\r
+\r
+  return NULL;\r
+}\r
+\r
+\r
+/**\r
+  This function will save confidential information to lockbox.\r
+\r
+  @param Guid       the guid to identify the confidential information\r
+  @param Buffer     the address of the confidential information\r
+  @param Length     the length of the confidential information\r
+\r
+  @retval RETURN_SUCCESS            the information is saved successfully.\r
+  @retval RETURN_INVALID_PARAMETER  the Guid is NULL, or Buffer is NULL, or\r
+                                    Length is 0\r
+  @retval RETURN_ALREADY_STARTED    the requested GUID already exist.\r
+  @retval RETURN_OUT_OF_RESOURCES   no enough resource to save the information.\r
+  @retval RETURN_ACCESS_DENIED      it is too late to invoke this interface\r
+  @retval RETURN_NOT_STARTED        it is too early to invoke this interface\r
+  @retval RETURN_UNSUPPORTED        the service is not supported by\r
+                                    implementaion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SaveLockBox (\r
+  IN  GUID                        *Guid,\r
+  IN  VOID                        *Buffer,\r
+  IN  UINTN                       Length\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+  VOID            *CopyBuffer;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a: Guid=%g Buffer=%p Length=0x%x\n", __FUNCTION__,\r
+    Guid, Buffer, (UINT32) Length));\r
+\r
+  if (Guid == NULL || Buffer == NULL || Length == 0) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Length > 0xFFFFFFFF) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  Header = FindHeaderByGuid (Guid);\r
+  if (Header == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  if (Header->Size > 0) {\r
+    return RETURN_ALREADY_STARTED;\r
+  }\r
+\r
+  CopyBuffer = AllocateAcpiNvsPool (Length);\r
+  if (CopyBuffer == NULL) {\r
+    return RETURN_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // overwrite the current terminator header with new metadata\r
+  //\r
+  CopyGuid (&Header->Guid, Guid);\r
+  Header->OrigAddress = (UINTN) Buffer;\r
+  Header->CopyAddress = (UINTN) CopyBuffer;\r
+  Header->Size        = (UINT32) Length;\r
+  Header->Attributes  = 0;\r
+\r
+  //\r
+  // copy contents\r
+  //\r
+  CopyMem (CopyBuffer, Buffer, Length);\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  This function will set lockbox attributes.\r
+\r
+  @param Guid       the guid to identify the confidential information\r
+  @param Attributes the attributes of the lockbox\r
+\r
+  @retval RETURN_SUCCESS            the information is saved successfully.\r
+  @retval RETURN_INVALID_PARAMETER  attributes is invalid.\r
+  @retval RETURN_NOT_FOUND          the requested GUID not found.\r
+  @retval RETURN_ACCESS_DENIED      it is too late to invoke this interface\r
+  @retval RETURN_NOT_STARTED        it is too early to invoke this interface\r
+  @retval RETURN_UNSUPPORTED        the service is not supported by\r
+                                    implementaion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+SetLockBoxAttributes (\r
+  IN  GUID                        *Guid,\r
+  IN  UINT64                      Attributes\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a: Guid=%g Attributes=0x%Lx\n", __FUNCTION__, Guid,\r
+    Attributes));\r
+\r
+  if (Guid == NULL) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  Header = FindHeaderByGuid (Guid);\r
+  if (!Header || Header->Size == 0) {\r
+    return RETURN_NOT_FOUND;\r
+  }\r
+  Header->Attributes = Attributes;\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  This function will update confidential information to lockbox.\r
+\r
+  @param Guid   the guid to identify the original confidential information\r
+  @param Offset the offset of the original confidential information\r
+  @param Buffer the address of the updated confidential information\r
+  @param Length the length of the updated confidential information\r
+\r
+  @retval RETURN_SUCCESS            the information is saved successfully.\r
+  @retval RETURN_INVALID_PARAMETER  the Guid is NULL, or Buffer is NULL, or\r
+                                    Length is 0.\r
+  @retval RETURN_NOT_FOUND          the requested GUID not found.\r
+  @retval RETURN_BUFFER_TOO_SMALL   the original buffer to too small to hold\r
+                                    new information.\r
+  @retval RETURN_ACCESS_DENIED      it is too late to invoke this interface\r
+  @retval RETURN_NOT_STARTED        it is too early to invoke this interface\r
+  @retval RETURN_UNSUPPORTED        the service is not supported by\r
+                                    implementaion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+UpdateLockBox (\r
+  IN  GUID                        *Guid,\r
+  IN  UINTN                       Offset,\r
+  IN  VOID                        *Buffer,\r
+  IN  UINTN                       Length\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a: Guid=%g Offset=0x%x Length=0x%x\n", __FUNCTION__,\r
+    Guid, (UINT32) Offset, (UINT32) Length));\r
+\r
+  if (Guid == NULL || Buffer == NULL || Length == 0) {\r
+    return RETURN_INVALID_PARAMETER;\r
+  }\r
+\r
+  Header = FindHeaderByGuid (Guid);\r
+  if (!Header || Header->Size == 0) {\r
+    return RETURN_NOT_FOUND;\r
+  }\r
+\r
+  if (Header->Size < Offset ||\r
+      Length > Header->Size - Offset) {\r
+    return RETURN_BUFFER_TOO_SMALL;\r
+  }\r
+\r
+  CopyMem ((UINT8 *)(UINTN) (Header->CopyAddress) + Offset, Buffer, Length);\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  This function will restore confidential information from lockbox.\r
+\r
+  @param Guid   the guid to identify the confidential information\r
+  @param Buffer the address of the restored confidential information\r
+                NULL means restored to original address, Length MUST be NULL at\r
+                same time.\r
+  @param Length the length of the restored confidential information\r
+\r
+  @retval RETURN_SUCCESS            the information is restored successfully.\r
+  @retval RETURN_INVALID_PARAMETER  the Guid is NULL, or one of Buffer and\r
+                                    Length is NULL.\r
+  @retval RETURN_WRITE_PROTECTED    Buffer and Length are NULL, but the LockBox\r
+                                    has no LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE\r
+                                    attribute.\r
+  @retval RETURN_BUFFER_TOO_SMALL   the Length is too small to hold the\r
+                                    confidential information.\r
+  @retval RETURN_NOT_FOUND          the requested GUID not found.\r
+  @retval RETURN_NOT_STARTED        it is too early to invoke this interface\r
+  @retval RETURN_ACCESS_DENIED      not allow to restore to the address\r
+  @retval RETURN_UNSUPPORTED        the service is not supported by\r
+                                    implementaion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+RestoreLockBox (\r
+  IN  GUID                        *Guid,\r
+  IN  VOID                        *Buffer, OPTIONAL\r
+  IN  OUT UINTN                   *Length  OPTIONAL\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+\r
+  DEBUG ((DEBUG_VERBOSE, "%a: Guid=%g Buffer=%p\n", __FUNCTION__, Guid,\r
+    Buffer));\r
+\r
+  if ((Guid == NULL) ||\r
+      ((Buffer == NULL) && (Length != NULL)) ||\r
+      ((Buffer != NULL) && (Length == NULL))) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Header = FindHeaderByGuid (Guid);\r
+  if (!Header || Header->Size == 0) {\r
+    return RETURN_NOT_FOUND;\r
+  }\r
+\r
+  if (Buffer == NULL) {\r
+    if (!(Header->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE)) {\r
+      return RETURN_WRITE_PROTECTED;\r
+    }\r
+    if (Header->OrigAddress + (Header->Size - 1) > MAX_ADDRESS) {\r
+      return RETURN_UNSUPPORTED;\r
+    }\r
+    Buffer = (VOID *)(UINTN) Header->OrigAddress;\r
+  }\r
+\r
+  //\r
+  // Set RestoreLength\r
+  //\r
+  if (Length != NULL) {\r
+    if (Header->Size > *Length) {\r
+      //\r
+      // Input buffer is too small to hold all data.\r
+      //\r
+      *Length = Header->Size;\r
+      return EFI_BUFFER_TOO_SMALL;\r
+    }\r
+    *Length = Header->Size;\r
+  }\r
+\r
+  CopyMem (Buffer, (VOID*)(UINTN) Header->CopyAddress, Header->Size);\r
+\r
+  return RETURN_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  This function will restore confidential information from all lockbox which\r
+  have RestoreInPlace attribute.\r
+\r
+  @retval RETURN_SUCCESS            the information is restored successfully.\r
+  @retval RETURN_NOT_STARTED        it is too early to invoke this interface\r
+  @retval RETURN_UNSUPPORTED        the service is not supported by\r
+                                    implementaion.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+RestoreAllLockBoxInPlace (\r
+  VOID\r
+  )\r
+{\r
+  LOCK_BOX_ENTRY *Header;\r
+\r
+  for (Header = StartOfEntries;\r
+       Header < EndOfEntries && Header->Size > 0;\r
+       Header++) {\r
+    if (Header->Attributes & LOCK_BOX_ATTRIBUTE_RESTORE_IN_PLACE) {\r
+      VOID *Buffer;\r
+\r
+      if (Header->OrigAddress + (Header->Size - 1) > MAX_ADDRESS) {\r
+        return RETURN_UNSUPPORTED;\r
+      }\r
+      Buffer = (VOID *)(UINTN) Header->OrigAddress;\r
+      CopyMem (Buffer, (VOID*)(UINTN)Header->CopyAddress, Header->Size);\r
+      DEBUG ((DEBUG_VERBOSE, "%a: Guid=%g Buffer=%p\n", __FUNCTION__,\r
+        Header->Guid, Buffer));\r
+    }\r
+  }\r
+  return RETURN_SUCCESS;\r
+}\r
diff --git a/OvmfPkg/Library/LockBoxLib/LockBoxLib.h b/OvmfPkg/Library/LockBoxLib/LockBoxLib.h
new file mode 100644 (file)
index 0000000..5adb65f
--- /dev/null
@@ -0,0 +1,60 @@
+/** @file\r
+  \r
+ Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
+\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
+#ifndef __LOCK_BOX_LIB_IMPL_H__\r
+#define __LOCK_BOX_LIB_IMPL_H__\r
+\r
+#pragma pack(1)\r
+\r
+typedef struct {\r
+  UINT32               Signature;\r
+  UINT32               SubPageBuffer;\r
+  UINT32               SubPageRemaining;\r
+} LOCK_BOX_GLOBAL;\r
+\r
+#define LOCK_BOX_GLOBAL_SIGNATURE SIGNATURE_32('L', 'B', 'G', 'S')\r
+\r
+extern LOCK_BOX_GLOBAL *mLockBoxGlobal;\r
+\r
+#pragma pack()\r
+\r
+/**\r
+  Allocates a buffer of type EfiACPIMemoryNVS.\r
+\r
+  Allocates the number bytes specified by AllocationSize of type\r
+  EfiACPIMemoryNVS and returns a pointer to the allocated buffer.\r
+  If AllocationSize is 0, then a valid buffer of 0 size is\r
+  returned.  If there is not enough memory remaining to satisfy\r
+  the request, then NULL is returned.\r
+\r
+  @param  AllocationSize        The number of bytes to allocate.\r
+\r
+  @return A pointer to the allocated buffer or NULL if allocation fails.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+AllocateAcpiNvsPool (\r
+  IN UINTN  AllocationSize\r
+  );\r
+\r
+\r
+RETURN_STATUS\r
+EFIAPI\r
+LockBoxLibInitialize (\r
+  VOID\r
+  );\r
+\r
+\r
+#endif\r
index 8a52bb143bd89689e462bef966426e6b01771841..b8a4cd5840c98c2d70f0da9dc0df38be1191c31c 100644 (file)
@@ -85,6 +85,8 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|0x0|UINT32|0x13\r
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize|0x0|UINT32|0x14\r
   gUefiOvmfPkgTokenSpaceGuid.PcdS3AcpiReservedMemoryBase|0x0|UINT32|0x17\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase|0x0|UINT32|0x18\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize|0x0|UINT32|0x19\r
 \r
 [PcdsDynamic, PcdsDynamicEx]\r
   gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2\r
index cbb97d8f22d2116da5063d243682a93cf8f0af1e..d4bfbfa816bd01bc25c2d3ee25736f093cc262f3 100644 (file)
@@ -99,7 +99,7 @@
   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf\r
   VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf\r
   LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf\r
-  LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf\r
 \r
 !ifdef $(SOURCE_DEBUG_ENABLE)\r
   DpcLib|MdeModulePkg/Library/DxeDpcLib/DxeDpcLib.inf\r
   PlatformBdsLib|OvmfPkg/Library/PlatformBdsLib/PlatformBdsLib.inf\r
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf\r
 \r
 [LibraryClasses.common.UEFI_APPLICATION]\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
index d935d9798507304b904a5a843e87340f7c759dab..a03c52a4c181fd0b87ebc08b6e944e8238167f59 100644 (file)
@@ -138,6 +138,9 @@ NumBlocks     = 0x80
 0x000000|0x006000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesSize\r
 \r
+0x006000|0x001000\r
+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
+\r
 0x010000|0x008000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize\r
 \r
index 8e5baa260cb5ea531b508e0e30b5093f01de8864..90a73e0f6ac24b1e7ef3805ce5156c662c9bde33 100644 (file)
   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf\r
   VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf\r
   LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf\r
-  LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf\r
 \r
 !ifdef $(SOURCE_DEBUG_ENABLE)\r
   DpcLib|MdeModulePkg/Library/DxeDpcLib/DxeDpcLib.inf\r
   PlatformBdsLib|OvmfPkg/Library/PlatformBdsLib/PlatformBdsLib.inf\r
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf\r
 \r
 [LibraryClasses.common.UEFI_APPLICATION]\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
index 29f365f63f116df2794313e10ffa476e192faade..93fece0dc1a022c904d826fd8bb956373037fe66 100644 (file)
@@ -138,6 +138,9 @@ NumBlocks     = 0x80
 0x000000|0x006000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesSize\r
 \r
+0x006000|0x001000\r
+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
+\r
 0x010000|0x008000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize\r
 \r
index c934ddc2db34d70d7021cecde941444807e5736b..d9384f9153ea3ad092cad4e31e8524b32ece7b06 100644 (file)
   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLib.inf\r
   VirtioLib|OvmfPkg/Library/VirtioLib/VirtioLib.inf\r
   LoadLinuxLib|OvmfPkg/Library/LoadLinuxLib/LoadLinuxLib.inf\r
-  LockBoxLib|MdeModulePkg/Library/LockBoxNullLib/LockBoxNullLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf\r
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf\r
 \r
 !ifdef $(SOURCE_DEBUG_ENABLE)\r
   DpcLib|MdeModulePkg/Library/DxeDpcLib/DxeDpcLib.inf\r
   PlatformBdsLib|OvmfPkg/Library/PlatformBdsLib/PlatformBdsLib.inf\r
   CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuExceptionHandlerLib.inf\r
+  LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxDxeLib.inf\r
 \r
 [LibraryClasses.common.UEFI_APPLICATION]\r
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf\r
index c08024e8ea635c6ba7185aa90d2021e8a803243b..b1444d9e8fe28bf73c0af92f9a4818c6ee94951d 100644 (file)
@@ -138,6 +138,9 @@ NumBlocks     = 0x80
 0x000000|0x006000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesSize\r
 \r
+0x006000|0x001000\r
+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
+\r
 0x010000|0x008000\r
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize\r
 \r
index 37030e6898390fcbd2e88ffe5d71363f546ba1b3..c1350b931f3a445bc2e9800928aa80e58e109009 100644 (file)
@@ -24,6 +24,7 @@ Module Name:
 //\r
 // The Library classes this module consumes\r
 //\r
+#include <Library/BaseMemoryLib.h>\r
 #include <Library/DebugLib.h>\r
 #include <Library/HobLib.h>\r
 #include <Library/IoLib.h>\r
@@ -217,5 +218,21 @@ InitializeRamRegions (
       EfiACPIMemoryNVS\r
       );\r
 #endif\r
+\r
+    //\r
+    // Reserve the lock box storage area\r
+    //\r
+    // Since this memory range will be used on S3 resume, it must be\r
+    // reserved as ACPI NVS.\r
+    //\r
+    ZeroMem (\r
+      (VOID*)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
+      (UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize)\r
+      );\r
+    BuildMemoryAllocationHob (\r
+      (EFI_PHYSICAL_ADDRESS)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageBase),\r
+      (UINT64)(UINTN) PcdGet32 (PcdOvmfLockBoxStorageSize),\r
+      EfiACPIMemoryNVS\r
+      );\r
   }\r
 }\r
index c91513882a2ddc38964d38004237f4dc731ac8cb..3b47bb70dd079ff3ac32dfbb5d25ba69b9eeda52 100644 (file)
@@ -70,6 +70,8 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize\r
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesBase\r
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPageTablesSize\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageBase\r
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfLockBoxStorageSize\r
   gEfiIntelFrameworkModulePkgTokenSpaceGuid.PcdS3AcpiReservedMemorySize\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize\r