public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Michael Kubacki" <mikuback@linux.microsoft.com>
To: devel@edk2.groups.io
Cc: Jian J Wang <jian.j.wang@intel.com>,
	Hao A Wu <hao.a.wu@intel.com>,
	Liming Gao <gaoliming@byosoft.com.cn>,
	Ard Biesheuvel <ardb@kernel.org>,
	Sami Mujawar <sami.mujawar@arm.com>
Subject: [PATCH v5 2/8] MdeModulePkg/VariableFlashInfoLib: Add initial library
Date: Mon, 25 Apr 2022 21:29:12 -0400	[thread overview]
Message-ID: <20220426012918.1216-3-mikuback@linux.microsoft.com> (raw)
In-Reply-To: <20220426012918.1216-1-mikuback@linux.microsoft.com>

From: Michael Kubacki <michael.kubacki@microsoft.com>

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

Adds a new library class VariableFlashInfoLib that abstracts access
to variable flash information. The instance provided first attempts
to retrieve information from the Variable Flash Info HOB. If that
HOB is not present, it falls back to the PCDs defined in
MdeModulePkg.

This fall back behavior provides backward compatibility for platforms
that only provide PCDs but also allows platforms that need to
dynamically provide the information using the Variable Flash Info HOB
to do so at runtime.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Acked-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Sami Mujawar <sami.mujawar@arm.com>
---
 MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.c   | 179 ++++++++++++++++++++
 MdeModulePkg/Include/Library/VariableFlashInfoLib.h                        |  68 ++++++++
 MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf |  48 ++++++
 MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.uni |  12 ++
 MdeModulePkg/MdeModulePkg.dec                                              |   4 +
 MdeModulePkg/MdeModulePkg.dsc                                              |   2 +
 6 files changed, 313 insertions(+)

diff --git a/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.c b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.c
new file mode 100644
index 000000000000..d5897225897b
--- /dev/null
+++ b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.c
@@ -0,0 +1,179 @@
+/** @file
+  Variable Flash Information Library
+
+  Copyright (c) Microsoft Corporation<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Pi/PiMultiPhase.h>
+#include <Guid/VariableFlashInfo.h>
+#include <Library/DebugLib.h>
+#include <Library/HobLib.h>
+#include <Library/VariableFlashInfoLib.h>
+
+/**
+  Get the HOB that contains variable flash information.
+
+  @param[out] VariableFlashInfo   Pointer to a pointer to set to the variable flash information structure.
+
+  @retval EFI_SUCCESS             Variable flash information was found successfully.
+  @retval EFI_INVALID_PARAMETER   The VariableFlashInfo pointer given is NULL.
+  @retval EFI_NOT_FOUND           Variable flash information could not be found.
+
+**/
+STATIC
+EFI_STATUS
+GetVariableFlashInfoFromHob (
+  OUT VARIABLE_FLASH_INFO  **VariableFlashInfo
+  )
+{
+  EFI_HOB_GUID_TYPE  *GuidHob;
+
+  if (VariableFlashInfo == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  GuidHob = GetFirstGuidHob (&gVariableFlashInfoHobGuid);
+  if (GuidHob == NULL) {
+    return EFI_NOT_FOUND;
+  }
+
+  *VariableFlashInfo = GET_GUID_HOB_DATA (GuidHob);
+
+  //
+  // Assert if more than one variable flash information HOB is present.
+  //
+  DEBUG_CODE (
+    if ((GetNextGuidHob (&gVariableFlashInfoHobGuid, GET_NEXT_HOB (GuidHob)) != NULL)) {
+    DEBUG ((DEBUG_ERROR, "ERROR: Found two variable flash information HOBs\n"));
+    ASSERT (FALSE);
+  }
+
+    );
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Get the base address and size for the NV storage area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The NV storage base address.
+  @param[out] Length         The NV storage length in bytes.
+
+  @retval EFI_SUCCESS             NV storage information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashNvStorageInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  )
+{
+  EFI_STATUS           Status;
+  VARIABLE_FLASH_INFO  *VariableFlashInfo;
+
+  if ((BaseAddress == NULL) || (Length == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
+  if (!EFI_ERROR (Status)) {
+    *BaseAddress = VariableFlashInfo->NvVariableBaseAddress;
+    *Length      = VariableFlashInfo->NvVariableLength;
+  } else {
+    *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageVariableBase64) != 0 ?
+                                          PcdGet64 (PcdFlashNvStorageVariableBase64) :
+                                          PcdGet32 (PcdFlashNvStorageVariableBase)
+                                          );
+    *Length = (UINT64)PcdGet32 (PcdFlashNvStorageVariableSize);
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Get the base address and size for the fault tolerant write (FTW) spare
+  area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The FTW spare base address.
+  @param[out] Length         The FTW spare length in bytes.
+
+  @retval EFI_SUCCESS             FTW spare information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+  @retval EFI_NOT_FOUND           FTW spare information could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashFtwSpareInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  )
+{
+  EFI_STATUS           Status;
+  VARIABLE_FLASH_INFO  *VariableFlashInfo;
+
+  if ((BaseAddress == NULL) || (Length == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
+  if (!EFI_ERROR (Status)) {
+    *BaseAddress = VariableFlashInfo->FtwSpareBaseAddress;
+    *Length      = VariableFlashInfo->FtwSpareLength;
+  } else {
+    *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageFtwSpareBase64) != 0 ?
+                                          PcdGet64 (PcdFlashNvStorageFtwSpareBase64) :
+                                          PcdGet32 (PcdFlashNvStorageFtwSpareBase)
+                                          );
+    *Length = (UINT64)PcdGet32 (PcdFlashNvStorageFtwSpareSize);
+  }
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Get the base address and size for the fault tolerant write (FTW) working
+  area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The FTW working area base address.
+  @param[out] Length         The FTW working area length in bytes.
+
+  @retval EFI_SUCCESS             FTW working information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+  @retval EFI_NOT_FOUND           FTW working information could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashFtwWorkingInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  )
+{
+  EFI_STATUS           Status;
+  VARIABLE_FLASH_INFO  *VariableFlashInfo;
+
+  if ((BaseAddress == NULL) || (Length == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  Status = GetVariableFlashInfoFromHob (&VariableFlashInfo);
+  if (!EFI_ERROR (Status)) {
+    *BaseAddress = VariableFlashInfo->FtwWorkingBaseAddress;
+    *Length      = VariableFlashInfo->FtwWorkingLength;
+  } else {
+    *BaseAddress = (EFI_PHYSICAL_ADDRESS)(PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) != 0 ?
+                                          PcdGet64 (PcdFlashNvStorageFtwWorkingBase64) :
+                                          PcdGet32 (PcdFlashNvStorageFtwWorkingBase)
+                                          );
+    *Length = (UINT64)PcdGet32 (PcdFlashNvStorageFtwWorkingSize);
+  }
+
+  return EFI_SUCCESS;
+}
diff --git a/MdeModulePkg/Include/Library/VariableFlashInfoLib.h b/MdeModulePkg/Include/Library/VariableFlashInfoLib.h
new file mode 100644
index 000000000000..1367be9376ea
--- /dev/null
+++ b/MdeModulePkg/Include/Library/VariableFlashInfoLib.h
@@ -0,0 +1,68 @@
+/** @file
+  Variable Flash Information Library
+
+Copyright (c) Microsoft Corporation<BR>
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef VARIABLE_FLASH_INFO_LIB_H_
+#define VARIABLE_FLASH_INFO_LIB_H_
+
+/**
+  Get the base address and size for the NV storage area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The NV storage base address.
+  @param[out] Length         The NV storage length in bytes.
+
+  @retval EFI_SUCCESS             NV storage information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+  @retval EFI_NOT_FOUND           NV storage information could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashNvStorageInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  );
+
+/**
+  Get the base address and size for the fault tolerant write (FTW) spare
+  area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The FTW spare base address.
+  @param[out] Length         The FTW spare length in bytes.
+
+  @retval EFI_SUCCESS             FTW spare information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+  @retval EFI_NOT_FOUND           FTW spare information could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashFtwSpareInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  );
+
+/**
+  Get the base address and size for the fault tolerant write (FTW) working
+  area used for UEFI variable storage.
+
+  @param[out] BaseAddress    The FTW working area base address.
+  @param[out] Length         The FTW working area length in bytes.
+
+  @retval EFI_SUCCESS             FTW working information was found successfully.
+  @retval EFI_INVALID_PARAMETER   A required pointer parameter is NULL.
+  @retval EFI_NOT_FOUND           FTW working information could not be found.
+
+**/
+EFI_STATUS
+EFIAPI
+GetVariableFlashFtwWorkingInfo (
+  OUT EFI_PHYSICAL_ADDRESS  *BaseAddress,
+  OUT UINT64                *Length
+  );
+
+#endif
diff --git a/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
new file mode 100644
index 000000000000..70175e75f9b1
--- /dev/null
+++ b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
@@ -0,0 +1,48 @@
+## @file
+#  Variable Flash Information Library
+#
+#  Provides services to access UEFI variable flash information.
+#
+#  Copyright (c) Microsoft Corporation<BR>
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+##
+
+[Defines]
+  INF_VERSION       = 0x00010005
+  BASE_NAME         = BaseVariableFlashInfoLib
+  MODULE_UNI_FILE   = BaseVariableFlashInfoLib.uni
+  FILE_GUID         = DEC426C9-C92E-4BAD-8E93-3F61C261118B
+  MODULE_TYPE       = BASE
+  VERSION_STRING    = 1.0
+  LIBRARY_CLASS     = VariableFlashInfoLib
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = ANY
+#
+
+[Sources]
+  BaseVariableFlashInfoLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[LibraryClasses]
+  DebugLib
+  HobLib
+
+[Guids]
+  gVariableFlashInfoHobGuid     ## CONSUMES     ## HOB
+
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase      ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64    ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize      ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase      ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64    ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize      ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase    ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64  ## SOMETIMES_CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize    ## SOMETIMES_CONSUMES
diff --git a/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.uni b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.uni
new file mode 100644
index 000000000000..9a5348fa02a0
--- /dev/null
+++ b/MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.uni
@@ -0,0 +1,12 @@
+// /** @file
+// Variable Flash Information Library
+//
+// Copyright (c) Microsoft Corporation<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_MODULE_ABSTRACT     #language en-US "UEFI variable flash information library"
+
+#string STR_MODULE_DESCRIPTION  #language en-US "Provides services to access UEFI variable flash information."
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index 4e82f5836096..2bcb9f9453af 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -154,6 +154,10 @@ [LibraryClasses]
   #
   VariablePolicyHelperLib|Include/Library/VariablePolicyHelperLib.h
 
+  ##  @libraryclass  Provides services to access UEFI variable flash information.
+  #
+  VariableFlashInfoLib|Include/Library/VariableFlashInfoLib.h
+
 [Guids]
   ## MdeModule package token space guid
   # Include/Guid/MdeModulePkgTokenSpace.h
diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index b1d83461865e..90a0a7ec4a7c 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -103,6 +103,7 @@ [LibraryClasses]
   DisplayUpdateProgressLib|MdeModulePkg/Library/DisplayUpdateProgressLibGraphics/DisplayUpdateProgressLibGraphics.inf
   VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf
   MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
+  VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
 
 [LibraryClasses.EBC.PEIM]
   IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
@@ -440,6 +441,7 @@ [Components]
   MdeModulePkg/Library/FmpAuthenticationLibNull/FmpAuthenticationLibNull.inf
   MdeModulePkg/Library/DxeCapsuleLibFmp/DxeCapsuleLib.inf
   MdeModulePkg/Library/DxeCapsuleLibFmp/DxeRuntimeCapsuleLib.inf
+  MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
 
 [Components.IA32, Components.X64, Components.AARCH64]
   MdeModulePkg/Universal/EbcDxe/EbcDxe.inf
-- 
2.28.0.windows.1


  parent reply	other threads:[~2022-04-26  1:29 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-26  1:29 [PATCH v5 0/8] Add Variable Flash Info HOB Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 1/8] MdeModulePkg: " Michael Kubacki
2022-04-26  1:29 ` Michael Kubacki [this message]
2022-04-26  1:29 ` [PATCH v5 3/8] MdeModulePkg/Variable: Consume Variable Flash Info Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 4/8] MdeModulePkg/FaultTolerantWrite: " Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 5/8] ArmVirtPkg/ArmVirt.dsc.inc: Add VariableFlashInfoLib Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 6/8] EmulatorPkg: " Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 7/8] OvmfPkg: " Michael Kubacki
2022-04-26  2:14   ` [edk2-devel] " Yao, Jiewen
2022-04-26  2:27     ` Michael Kubacki
2022-04-26  1:29 ` [PATCH v5 8/8] UefiPayloadPkg: " Michael Kubacki
2022-04-29 13:45 ` [PATCH v5 0/8] Add Variable Flash Info HOB Ard Biesheuvel
2022-04-29 15:48   ` Michael Kubacki
2022-05-05  1:27     ` 回复: [edk2-devel] " gaoliming
2022-05-06  1:52       ` Michael Kubacki
2022-05-10 15:01         ` Michael Kubacki
2022-05-13 18:23           ` Michael Kubacki
2022-05-14  1:16             ` 回复: " gaoliming
2022-05-16 15:27               ` Michael Kubacki
2022-05-16 17:36                 ` Ard Biesheuvel
2022-05-17  4:14                   ` Michael Kubacki
2022-05-17  5:22                     ` 回复: " gaoliming
2022-05-17 13:06                       ` Michael Kubacki
2022-05-17 16:10                         ` Michael Kubacki
2022-05-19  3:45                         ` 回复: " gaoliming
     [not found]                         ` <16F064E8C9D4EA0D.2722@groups.io>
2022-05-19  6:20                           ` gaoliming

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=20220426012918.1216-3-mikuback@linux.microsoft.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