public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Sunil V L" <sunilvl@ventanamicro.com>
To: devel@edk2.groups.io
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>,
	Jiewen Yao <jiewen.yao@intel.com>,
	Jordan Justen <jordan.l.justen@intel.com>,
	Gerd Hoffmann <kraxel@redhat.com>,
	Daniel Schaefer <git@danielschaefer.me>
Subject: [edk2-staging/RiscV64QemuVirt PATCH V5 27/30] OvmfPkg: Add VirtNorFlashPlatformLib library
Date: Sun, 30 Oct 2022 18:58:39 +0530	[thread overview]
Message-ID: <20221030132842.54077-28-sunilvl@ventanamicro.com> (raw)
In-Reply-To: <20221030132842.54077-1-sunilvl@ventanamicro.com>

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4076

Add the VirtNorFlashPlatformLib library for qemu virt machines.

Add two instances of the library. One which uses DT information
and the other which is static. They are copied from
ArmVirtPkg and SbsaQemu. Two PCD variables used by the library
are added in the OvmfPkg.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Daniel Schaefer <git@danielschaefer.me>
Signed-off-by: Sunil V L <sunilvl@ventanamicro.com>
---
 OvmfPkg/OvmfPkg.dec                                                   |   4 +
 OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf |  40 ++++++
 OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf     |  30 +++++
 OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c   | 136 ++++++++++++++++++++
 OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c       |  40 ++++++
 5 files changed, 250 insertions(+)

diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec
index ba5194114c5c..142c34193d60 100644
--- a/OvmfPkg/OvmfPkg.dec
+++ b/OvmfPkg/OvmfPkg.dec
@@ -409,6 +409,10 @@ [PcdsFixedAtBuild]
   #  check to decide whether to abort dispatch of the driver it is linked into.
   gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName|""|VOID*|0x68
 
+  ## The base address and size of the FVMAIN
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvBaseAddress|0|UINT64|0x71
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvSize|0|UINT32|0x72
+
 [PcdsDynamic, PcdsDynamicEx]
   gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x10
diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf
new file mode 100644
index 000000000000..5b7a45d15782
--- /dev/null
+++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.inf
@@ -0,0 +1,40 @@
+#/** @file
+#
+#  Component description file for VirtNorFlashDeviceTreeLib module
+#
+#  Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = VirtNorFlashDeviceTreeLib
+  FILE_GUID                      = 42C30D8E-BFAD-4E77-9041-E7DAAE88DF7A
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = VirtNorFlashPlatformLib
+
+[Sources.common]
+  VirtNorFlashDeviceTreeLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  OvmfPkg/OvmfPkg.dec
+  EmbeddedPkg/EmbeddedPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  UefiBootServicesTableLib
+
+[Protocols]
+  gFdtClientProtocolGuid          ## CONSUMES
+
+[Depex]
+  gFdtClientProtocolGuid
+
+[Pcd]
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvBaseAddress
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFvSize
diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf
new file mode 100644
index 000000000000..4e87bd437380
--- /dev/null
+++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.inf
@@ -0,0 +1,30 @@
+#/** @file
+#
+#  Component description file for VirtNorFlashStaticLib module
+#
+#  Copyright (c) 2014, Linaro Ltd. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+#**/
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = VirtNorFlashStaticLib
+  FILE_GUID                      = 064742F1-E531-4D7D-A154-22315889CC23
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = VirtNorFlashPlatformLib
+
+[Sources.common]
+  VirtNorFlashStaticLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  OvmfPkg/OvmfPkg.dec
+
+[Pcd]
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFdBaseAddress
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFirmwareFdSize
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase
diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c
new file mode 100644
index 000000000000..08750e66095d
--- /dev/null
+++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashDeviceTreeLib.c
@@ -0,0 +1,136 @@
+/** @file
+
+ Copyright (c) 2014-2018, Linaro Ltd. All rights reserved.<BR>
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+ **/
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/VirtNorFlashPlatformLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+
+#include <Protocol/FdtClient.h>
+
+#define QEMU_NOR_BLOCK_SIZE  SIZE_256KB
+
+#define MAX_FLASH_BANKS  4
+
+EFI_STATUS
+VirtNorFlashPlatformInitialization (
+  VOID
+  )
+{
+  return EFI_SUCCESS;
+}
+
+VIRT_NOR_FLASH_DESCRIPTION  mNorFlashDevices[MAX_FLASH_BANKS];
+
+EFI_STATUS
+VirtNorFlashPlatformGetDevices (
+  OUT VIRT_NOR_FLASH_DESCRIPTION  **NorFlashDescriptions,
+  OUT UINT32                      *Count
+  )
+{
+  FDT_CLIENT_PROTOCOL  *FdtClient;
+  INT32                Node;
+  EFI_STATUS           Status;
+  EFI_STATUS           FindNodeStatus;
+  CONST UINT32         *Reg;
+  UINT32               PropSize;
+  UINT32               Num;
+  UINT64               Base;
+  UINT64               Size;
+
+  Status = gBS->LocateProtocol (
+                  &gFdtClientProtocolGuid,
+                  NULL,
+                  (VOID **)&FdtClient
+                  );
+  ASSERT_EFI_ERROR (Status);
+
+  Num = 0;
+  for (FindNodeStatus = FdtClient->FindCompatibleNode (
+                                     FdtClient,
+                                     "cfi-flash",
+                                     &Node
+                                     );
+       !EFI_ERROR (FindNodeStatus) && Num < MAX_FLASH_BANKS;
+       FindNodeStatus = FdtClient->FindNextCompatibleNode (
+                                     FdtClient,
+                                     "cfi-flash",
+                                     Node,
+                                     &Node
+                                     ))
+  {
+    Status = FdtClient->GetNodeProperty (
+                          FdtClient,
+                          Node,
+                          "reg",
+                          (CONST VOID **)&Reg,
+                          &PropSize
+                          );
+    if (EFI_ERROR (Status)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "%a: GetNodeProperty () failed (Status == %r)\n",
+        __FUNCTION__,
+        Status
+        ));
+      continue;
+    }
+
+    ASSERT ((PropSize % (4 * sizeof (UINT32))) == 0);
+
+    while (PropSize >= (4 * sizeof (UINT32)) && Num < MAX_FLASH_BANKS) {
+      Base = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[0]));
+      Size = SwapBytes64 (ReadUnaligned64 ((VOID *)&Reg[2]));
+      Reg += 4;
+
+      PropSize -= 4 * sizeof (UINT32);
+
+      //
+      // Disregard any flash devices that overlap with the primary FV.
+      // The firmware is not updatable from inside the guest anyway.
+      //
+      if ((PcdGet64 (PcdOvmfFvBaseAddress) + PcdGet32 (PcdOvmfFvSize) > Base) &&
+          ((Base + Size) > PcdGet64 (PcdOvmfFvBaseAddress)))
+      {
+        continue;
+      }
+
+      mNorFlashDevices[Num].DeviceBaseAddress = (UINTN)Base;
+      mNorFlashDevices[Num].RegionBaseAddress = (UINTN)Base;
+      mNorFlashDevices[Num].Size              = (UINTN)Size;
+      mNorFlashDevices[Num].BlockSize         = QEMU_NOR_BLOCK_SIZE;
+      Num++;
+    }
+
+    //
+    // UEFI takes ownership of the NOR flash, and exposes its functionality
+    // through the UEFI Runtime Services GetVariable, SetVariable, etc. This
+    // means we need to disable it in the device tree to prevent the OS from
+    // attaching its device driver as well.
+    // Note that this also hides other flash banks, but the only other flash
+    // bank we expect to encounter is the one that carries the UEFI executable
+    // code, which is not intended to be guest updatable, and is usually backed
+    // in a readonly manner by QEMU anyway.
+    //
+    Status = FdtClient->SetNodeProperty (
+                          FdtClient,
+                          Node,
+                          "status",
+                          "disabled",
+                          sizeof ("disabled")
+                          );
+    if (EFI_ERROR (Status)) {
+      DEBUG ((DEBUG_WARN, "Failed to set NOR flash status to 'disabled'\n"));
+    }
+  }
+
+  *NorFlashDescriptions = mNorFlashDevices;
+  *Count                = Num;
+
+  return EFI_SUCCESS;
+}
diff --git a/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c
new file mode 100644
index 000000000000..fdc2ccb6294e
--- /dev/null
+++ b/OvmfPkg/Library/VirtNorFlashPlatformLib/VirtNorFlashStaticLib.c
@@ -0,0 +1,40 @@
+/** @file
+
+ Copyright (c) 2019, Linaro Ltd. All rights reserved
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+ **/
+
+#include <Base.h>
+#include <PiDxe.h>
+#include <Library/VirtNorFlashPlatformLib.h>
+
+#define QEMU_NOR_BLOCK_SIZE  SIZE_256KB
+
+EFI_STATUS
+VirtNorFlashPlatformInitialization (
+  VOID
+  )
+{
+  return EFI_SUCCESS;
+}
+
+VIRT_NOR_FLASH_DESCRIPTION  mNorFlashDevice =
+{
+  FixedPcdGet32 (PcdOvmfFdBaseAddress),
+  FixedPcdGet64 (PcdFlashNvStorageVariableBase),
+  FixedPcdGet32 (PcdOvmfFirmwareFdSize),
+  QEMU_NOR_BLOCK_SIZE
+};
+
+EFI_STATUS
+VirtNorFlashPlatformGetDevices (
+  OUT VIRT_NOR_FLASH_DESCRIPTION  **NorFlashDescriptions,
+  OUT UINT32                      *Count
+  )
+{
+  *NorFlashDescriptions = &mNorFlashDevice;
+  *Count                = 1;
+  return EFI_SUCCESS;
+}
-- 
2.38.0


  parent reply	other threads:[~2022-10-30 13:30 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-30 13:28 [edk2-staging/RiscV64QemuVirt PATCH V5 00/30] Add support for RISC-V virt machine Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 01/30] MdePkg/Register: Add register definition header files for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 02/30] MdePkg: Add RISCV_EFI_BOOT_PROTOCOL related definitions Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 03/30] MdePkg/BaseLib: RISC-V: Add few more helper functions Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 04/30] MdePkg: Add BaseRiscVSbiLib Library for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 05/30] OvmfPkg/PlatformInitLib: Refactor to allow other architectures Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 06/30] OvmfPkg/PlatformInitLib: Add support for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 07/30] OvmfPkg/ResetSystemLib: Refactor to allow other architectures Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 08/30] OvmfPkg/ResetSystemLib: Add support for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 09/30] OvmfPkg/Sec: Refactor to allow other architectures Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 10/30] OvmfPkg/Sec: Add RISC-V support Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 11/30] OvmfPkg/PlatformPei: Refactor to allow other architectures Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 12/30] OvmfPkg/PlatformPei: Add support for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 13/30] UefiCpuPkg/CpuTimerLib: Refactor to allow other architectures Sunil V L
2022-11-06 12:32   ` [edk2-devel] " Chang, Abner
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 14/30] UefiCpuPkg/CpuTimerLib: Add support for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 15/30] UefiCpuPkg/CpuExceptionHandlerLib: Refactor to allow other architectures Sunil V L
2022-11-06 12:35   ` [edk2-devel] " Chang, Abner
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 16/30] UefiCpuPkg/CpuExceptionHandlerLib: Add support for RISC-V Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 17/30] UefiCpuPkg/CpuDxe: Refactor to allow other architectures Sunil V L
2022-11-06 12:36   ` [edk2-devel] " Chang, Abner
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 18/30] UefiCpuPkg/CpuDxe: Add support for RISC-V Sunil V L
2022-11-06 12:36   ` [edk2-devel] " Chang, Abner
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 19/30] UefiCpuPkg/CpuDxe: Add RISCV_EFI_BOOT_PROTOCOL support Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 20/30] UefiCpuPkg: Add CpuTimerDxe module Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 21/30] ArmVirtPkg/PlatformHasAcpiDtDxe: Move to OvmfPkg Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 22/30] ArmVirtPkg: Fix up the location of PlatformHasAcpiDtDxe Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 23/30] ArmVirtPkg/PlatformBootManagerLib: Move to OvmfPkg Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 24/30] ArmVirtPkg: Fix up the paths to PlatformBootManagerLib Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 25/30] EmbeddedPkg/NvVarStoreFormattedLib: Migrate to MdeModulePkg Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 26/30] ArmVirtPkg: Update the references to NvVarStoreFormattedLib Sunil V L
2022-10-30 13:28 ` Sunil V L [this message]
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 28/30] OvmfPkg: RiscVVirt: Add Qemu Virt platform support Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 29/30] Maintainers.txt: Add entry for OvmfPkg/RiscVVirt Sunil V L
2022-10-30 13:28 ` [edk2-staging/RiscV64QemuVirt PATCH V5 30/30] UefiCpuPkg/UefiCpuPkg.ci.yaml: Ignore RISC-V file Sunil V L

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=20221030132842.54077-28-sunilvl@ventanamicro.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