[edk2] [PATCH 09/12] OvmfPkg PlatformPei: Set flash variable PCDs

Jordan Justen posted 12 patches 7 years, 7 months ago
[edk2] [PATCH 09/12] OvmfPkg PlatformPei: Set flash variable PCDs
Posted by Jordan Justen 7 years, 7 months ago
This allows the PEI based variable drivers to run.

Cc: Laszlo Ersek <lersek@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Jordan Justen <jordan.l.justen@intel.com>
---
 OvmfPkg/PlatformPei/Platform.c      |  34 +---------
 OvmfPkg/PlatformPei/Platform.h      |   7 ++-
 OvmfPkg/PlatformPei/PlatformPei.inf |  10 ++-
 OvmfPkg/PlatformPei/Vars.c          | 122 ++++++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+), 33 deletions(-)
 create mode 100644 OvmfPkg/PlatformPei/Vars.c

diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c
index 77a8a16c15..2e943d6e7b 100644
--- a/OvmfPkg/PlatformPei/Platform.c
+++ b/OvmfPkg/PlatformPei/Platform.c
@@ -1,7 +1,7 @@
 /**@file
   Platform PEI driver
 
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
   Copyright (c) 2011, Andrei Warkentin <andreiw@motorola.com>
 
   This program and the accompanying materials
@@ -499,35 +499,6 @@ BootModeInitialization (
 
 
 VOID
-ReserveEmuVariableNvStore (
-  )
-{
-  EFI_PHYSICAL_ADDRESS VariableStore;
-  RETURN_STATUS        PcdStatus;
-
-  //
-  // Allocate storage for NV variables early on so it will be
-  // at a consistent address.  Since VM memory is preserved
-  // across reboots, this allows the NV variable storage to survive
-  // a VM reboot.
-  //
-  VariableStore =
-    (EFI_PHYSICAL_ADDRESS)(UINTN)
-      AllocateAlignedRuntimePages (
-        EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)),
-        PcdGet32 (PcdFlashNvStorageFtwSpareSize)
-        );
-  DEBUG ((EFI_D_INFO,
-          "Reserved variable store memory: 0x%lX; size: %dkb\n",
-          VariableStore,
-          (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024
-        ));
-  PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
-  ASSERT_RETURN_ERROR (PcdStatus);
-}
-
-
-VOID
 DebugDumpCmos (
   VOID
   )
@@ -660,8 +631,9 @@ InitializePlatform (
   //
   mHostBridgeDevId = PciRead16 (OVMF_HOSTBRIDGE_DID);
 
+  SetupVariables ();
+
   if (mBootMode != BOOT_ON_S3_RESUME) {
-    ReserveEmuVariableNvStore ();
     PeiFvInitialization ();
     MemMapInitialization ();
     NoexecDxeInitialization ();
diff --git a/OvmfPkg/PlatformPei/Platform.h b/OvmfPkg/PlatformPei/Platform.h
index 18f42c3f0e..dfbdb8b75d 100644
--- a/OvmfPkg/PlatformPei/Platform.h
+++ b/OvmfPkg/PlatformPei/Platform.h
@@ -1,7 +1,7 @@
 /** @file
   Platform PEI module include file.
 
-  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
   This program and the accompanying materials
   are licensed and made available under the terms and conditions of the BSD License
   which accompanies this distribution.  The full text of the license may be found at
@@ -95,6 +95,11 @@ XenPublishRamRegions (
   VOID
   );
 
+VOID
+SetupVariables (
+  VOID
+  );
+
 extern EFI_BOOT_MODE mBootMode;
 
 extern BOOLEAN mS3Supported;
diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf
index 53c6dd445a..0eaf27e553 100644
--- a/OvmfPkg/PlatformPei/PlatformPei.inf
+++ b/OvmfPkg/PlatformPei/PlatformPei.inf
@@ -2,7 +2,7 @@
 #  Platform PEI driver
 #
 #  This module provides platform specific function to detect boot mode.
-#  Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
 #
 #  This program and the accompanying materials
 #  are licensed and made available under the terms and conditions of the BSD License
@@ -34,6 +34,7 @@
   Fv.c
   MemDetect.c
   Platform.c
+  Vars.c
   Xen.c
 
 [Packages]
@@ -83,9 +84,16 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio64Size
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfDecompressionScratchEnd
   gUefiOvmfPkgTokenSpaceGuid.PcdQ35TsegMbytes
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageVariableBase
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageFtwWorkingBase
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageFtwSpareBase
   gEfiMdePkgTokenSpaceGuid.PcdGuidedExtractHandlerTableAddress
   gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved
   gEfiMdeModulePkgTokenSpaceGuid.PcdPciDisableBusEnumeration
diff --git a/OvmfPkg/PlatformPei/Vars.c b/OvmfPkg/PlatformPei/Vars.c
new file mode 100644
index 0000000000..563f847a55
--- /dev/null
+++ b/OvmfPkg/PlatformPei/Vars.c
@@ -0,0 +1,122 @@
+/**@file
+  Platform PEI Variable Store Initialization
+
+  Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
+
+  This program and the accompanying materials are licensed and made
+  available under the terms and conditions of the BSD License which
+  accompanies this distribution. The full text of the license may be
+  found at http://opensource.org/licenses/bsd-license.php
+
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS"
+  BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER
+  EXPRESS OR IMPLIED.
+
+**/
+
+#include <PiPei.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
+
+#include "Platform.h"
+
+#define OVMF_FVB_BLOCK_SIZE (FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))
+#define OVMF_FVB_SIZE (2 * FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize))
+#define OVMF_FV_HEADER_LENGTH OFFSET_OF (FVB_FV_HDR_AND_VARS_TEMPLATE, VarHdr)
+
+
+VOID
+ReserveEmuVariableNvStore (
+  )
+{
+  EFI_PHYSICAL_ADDRESS VariableStore;
+  UINT32               Offset;
+  RETURN_STATUS        PcdStatus;
+
+  //
+  // Allocate storage for NV variables early on so it will be
+  // at a consistent address.  Since VM memory is preserved
+  // across reboots, this allows the NV variable storage to survive
+  // a VM reboot.
+  //
+  VariableStore =
+    (EFI_PHYSICAL_ADDRESS)(UINTN)
+      AllocateAlignedRuntimePages (
+        EFI_SIZE_TO_PAGES (OVMF_FVB_SIZE),
+        PcdGet32 (PcdFlashNvStorageFtwSpareSize)
+        );
+  ASSERT (VariableStore != 0);
+  ASSERT ((VariableStore + OVMF_FVB_SIZE) <= MAX_ADDRESS);
+  DEBUG ((EFI_D_INFO,
+          "Reserved variable store memory: 0x%lX; size: %dkb\n",
+          VariableStore,
+          (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024
+        ));
+  PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
+  ASSERT_RETURN_ERROR (PcdStatus);
+
+  //
+  // Initialize the main FV header and variable store header
+  //
+  PcdStatus = PcdSet64S (
+                PcdFlashNvStorageVariableBase64,
+                VariableStore);
+  ASSERT_RETURN_ERROR (PcdStatus);
+
+  //
+  // Initialize the Fault Tolerant Write data area
+  //
+  Offset = PcdGet32 (PcdVariableStoreSize);
+  PcdStatus = PcdSet32S (
+                PcdFlashNvStorageFtwWorkingBase,
+                VariableStore + Offset);
+  ASSERT_RETURN_ERROR (PcdStatus);
+
+  //
+  // Initialize the Fault Tolerant Write spare block
+  //
+  Offset = FixedPcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  PcdStatus = PcdSet32S (
+                PcdFlashNvStorageFtwSpareBase,
+                VariableStore + Offset);
+  ASSERT_RETURN_ERROR (PcdStatus);
+}
+
+
+VOID
+SetupVariables (
+  VOID
+  )
+{
+  RETURN_STATUS        PcdStatus;
+
+  if (PcdGetBool (PcdOvmfFlashVariablesEnable)) {
+    //
+    // If flash is enabled, then set the variable PCD to point
+    // directly at flash.
+    //
+    PcdStatus = PcdSet64S (
+      PcdFlashNvStorageVariableBase64,
+      (UINTN) PcdGet32 (PcdOvmfFlashNvStorageVariableBase)
+      );
+    ASSERT_RETURN_ERROR (PcdStatus);
+    PcdStatus = PcdSet32S (
+      PcdFlashNvStorageFtwWorkingBase,
+      PcdGet32 (PcdOvmfFlashNvStorageFtwWorkingBase)
+      );
+    ASSERT_RETURN_ERROR (PcdStatus);
+    PcdStatus = PcdSet32S (
+      PcdFlashNvStorageFtwSpareBase,
+      PcdGet32 (PcdOvmfFlashNvStorageFtwSpareBase)
+      );
+    ASSERT_RETURN_ERROR (PcdStatus);
+  } else {
+    //
+    // If flash is not enabled, then allocate a buffer and initialize
+    // it if necessary for variable operations.
+    //
+    ReserveEmuVariableNvStore ();
+  }
+}
-- 
2.11.0

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel