public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sami Mujawar" <sami.mujawar@arm.com>
To: <devel@edk2.groups.io>
Cc: Sami Mujawar <sami.mujawar@arm.com>, <ardb+tianocore@kernel.org>,
	<quic_llindhol@quicinc.com>, <kraxel@redhat.com>,
	<Pierre.Gondois@arm.com>, <jean-philippe@linaro.org>,
	<Matteo.Carlini@arm.com>, <Akanksha.Jain2@arm.com>,
	<Ben.Adderson@arm.com>, <Sibel.Allinson@arm.com>, <nd@arm.com>
Subject: [PATCH v2 3/5] ArmVirtPkg: Fallback to variable emulation if no CFI is found
Date: Fri, 19 May 2023 15:55:38 +0100	[thread overview]
Message-ID: <20230519145540.46700-4-sami.mujawar@arm.com> (raw)
In-Reply-To: <20230519145540.46700-1-sami.mujawar@arm.com>

The kvmtool option '--flash <flash filename>' is used to launch
a guests VM with a CFI flash device that maps the flash file
specified at the command line.
However, kvmtool allows guest VMs to be launched without a CFI
flash device. In such scenarios the firmware can utilize the
emulated variable storage for UEFI variables. To support this
the PCD gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable
must be set to TRUE.

Therefore, update the NorFlashKvmtoolLib to fallback to variable
emulation if a CFI device is not detected. Also improve the error
logging.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c      | 38 +++++++++++++++++---
 ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf |  3 +-
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c
index 43f5858644b1f47ada17e00fba55a670ab5862bd..2beeefdd272d6f8841f7d0b972394739b745982e 100644
--- a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c
+++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtool.c
@@ -1,7 +1,7 @@
 /** @file
    An instance of the NorFlashPlatformLib for Kvmtool platform.
 
- Copyright (c) 2020, ARM Ltd. All rights reserved.<BR>
+ Copyright (c) 2020 - 2023, Arm Ltd. All rights reserved.<BR>
 
  SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -228,7 +228,7 @@ NorFlashPlatformLibConstructor (
   CONST CHAR8   *Label;
   UINT32        LabelLen;
 
-  if (mNorFlashDeviceCount != 0) {
+  if ((mNorFlashDeviceCount != 0) || PcdGetBool (PcdEmuVariableNvModeEnable)) {
     return EFI_SUCCESS;
   }
 
@@ -337,9 +337,39 @@ NorFlashPlatformLibConstructor (
     }
 
     if (mNorFlashDevices[UefiVarStoreIndex].DeviceBaseAddress != 0) {
-      return SetupVariableStore (&mNorFlashDevices[UefiVarStoreIndex]);
+      Status = SetupVariableStore (&mNorFlashDevices[UefiVarStoreIndex]);
+      if (EFI_ERROR (Status)) {
+        DEBUG ((
+          DEBUG_ERROR,
+          "ERROR: Failed to setup variable store, Status = %r\n",
+          Status
+          ));
+        ASSERT (0);
+      }
+    } else {
+      DEBUG ((
+        DEBUG_ERROR,
+        "ERROR: Invalid Flash device Base address\n"
+        ));
+      ASSERT (0);
+      Status = EFI_NOT_FOUND;
+    }
+  } else {
+    // No Flash device found fallback to Runtime Variable Emulation.
+    DEBUG ((
+      DEBUG_INFO,
+      "INFO: No Flash device found fallback to Runtime Variable Emulation.\n"
+      ));
+    Status = PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE);
+    if (EFI_ERROR (Status)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "ERROR: Failed to set PcdEmuVariableNvModeEnable, Status = %r\n",
+        Status
+        ));
+      ASSERT (0);
     }
   }
 
-  return EFI_NOT_FOUND;
+  return Status;
 }
diff --git a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf
index b5f35d4782896761e7975a6e5c196ff0fab0d6db..fba1245e41ec4b146db79a821b8343247377af41 100644
--- a/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf
+++ b/ArmVirtPkg/Library/NorFlashKvmtoolLib/NorFlashKvmtoolLib.inf
@@ -1,7 +1,7 @@
 ## @file
 #  Nor Flash library for Kvmtool.
 #
-#  Copyright (c) 2020, ARM Ltd. All rights reserved.<BR>
+#  Copyright (c) 2020 - 2023, Arm Ltd. All rights reserved.<BR>
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
 ##
@@ -39,6 +39,7 @@ [Pcd]
   gArmTokenSpaceGuid.PcdFvBaseAddress
   gArmTokenSpaceGuid.PcdFvSize
 
+  gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase
-- 
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


  parent reply	other threads:[~2023-05-19 14:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19 14:55 [PATCH v2 0/5] ArmVirtPkg: Add dynamic CFI flash detection for Kvmtool guests Sami Mujawar
2023-05-19 14:55 ` [PATCH v2 1/5] ArmPkg: Configure PcdEmuVariableNvModeEnable as a dynamic PCD Sami Mujawar
2023-05-19 14:55 ` [PATCH v2 2/5] ArmVirtPkg: Define variables for emulating runtime variables Sami Mujawar
2023-05-19 14:55 ` Sami Mujawar [this message]
2023-05-19 14:55 ` [PATCH v2 4/5] ArmVirtPkg: Dispatch variable service if variable emulation is enabled Sami Mujawar
2023-05-19 14:55 ` [PATCH v2 5/5] ArmVirtPkg/PrePi: Allocate separate stack for Dxe phase Sami Mujawar
2023-05-25 15:51 ` [PATCH v2 0/5] ArmVirtPkg: Add dynamic CFI flash detection for Kvmtool guests Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230519145540.46700-4-sami.mujawar@arm.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox