Blob Blame History Raw
From 0d92c07320682a2b0f0d2560bb447ec48d373049 Mon Sep 17 00:00:00 2001
From: Laszlo Ersek <lersek@redhat.com>
Date: Fri, 5 May 2017 20:24:19 +0200
Subject: [PATCH 08/10] OvmfPkg/PlatformPei: handle non-power-of-two spare size
 for emu variables

Message-id: <20170505182421.19670-9-lersek@redhat.com>
Patchwork-id: 75034
O-Subject:  [RHEL-7.4 ovmf PATCH v2 08/10] OvmfPkg/PlatformPei: handle
	non-power-of-two spare size for emu variables
Bugzilla: 1443351
Acked-by: Paolo Bonzini <pbonzini@redhat.com>
Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
Acked-by: Miroslav Rezanina <mrezanin@redhat.com>

In commit b24fca05751f ("OvmfPkg: introduce 4MB flash image (mainly) for
Windows HCK", 2017-04-29), I changed PcdFlashNvStorageFtwSpareSize to
264KB, in the then-new default 4MB build.

While PcdFlashNvStorageFtwSpareSize remains exactly half of the entire
non-volatile store (which is 528KB), 264KB isn't itself a power of two.
This triggers an assertion failure in AllocateAlignedRuntimePages() when
PlatformPei calls it from the ReserveEmuVariableNvStore() function,
passing PcdFlashNvStorageFtwSpareSize as the Alignment parameter:

> ASSERT MdePkg/Library/PeiMemoryAllocationLib/MemoryAllocationLib.c(196):
> (Alignment & (Alignment - 1)) == 0

Round up the alignment to the next power of two if necessary.

Fixes: b24fca05751f8222acf264853709012e0ab7bf49
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
(cherry picked from commit 0c79471d6a986b858c35dc577eaeb344cc5c4cdd)
---
 OvmfPkg/PlatformPei/Platform.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c
index 5ef0fdc..6c8758c 100644
--- a/OvmfPkg/PlatformPei/Platform.c
+++ b/OvmfPkg/PlatformPei/Platform.c
@@ -503,6 +503,7 @@ ReserveEmuVariableNvStore (
 {
   EFI_PHYSICAL_ADDRESS VariableStore;
   RETURN_STATUS        PcdStatus;
+  UINT32               Alignment;
 
   //
   // Allocate storage for NV variables early on so it will be
@@ -510,16 +511,26 @@ ReserveEmuVariableNvStore (
   // across reboots, this allows the NV variable storage to survive
   // a VM reboot.
   //
+  Alignment = PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  if ((Alignment & (Alignment - 1)) != 0) {
+    //
+    // Round up Alignment to the next power of two.
+    //
+    Alignment = GetPowerOfTwo32 (Alignment) << 1;
+  }
+
   VariableStore =
     (EFI_PHYSICAL_ADDRESS)(UINTN)
       AllocateAlignedRuntimePages (
         EFI_SIZE_TO_PAGES (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)),
-        PcdGet32 (PcdFlashNvStorageFtwSpareSize)
+        Alignment
         );
   DEBUG ((EFI_D_INFO,
-          "Reserved variable store memory: 0x%lX; size: %dkb\n",
+          "Reserved variable store memory: 0x%lX; size: %dkb, "
+          "alignment: 0x%x\n",
           VariableStore,
-          (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024
+          (2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / 1024,
+          Alignment
         ));
   PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore);
   ASSERT_RETURN_ERROR (PcdStatus);
-- 
1.8.3.1