From nobody Thu Apr 18 20:00:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+73678+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73678+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617529296; cv=none; d=zohomail.com; s=zohoarc; b=VYam9V7C9isc+bStkHx1mxpPBAqNdyCHvy8ME/+1PG6GB6G1+EQrChwm2oJlSfFHNFlf7/6qllFHuD2FbMl/hXD2CGfKSVk0UdLDmGoBVI08pq2mdvhgUPt8Gg6OE/5a3jfCQ1gD80neYRgDCLpcjobHuUVWXnHvEAuRWN0HqZI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617529296; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=loC7SjlIzygRE7N6n0dCLXTNcUE8J2dmhfllJ4NjZi4=; b=Yk+uNfmdRzjrxaDtc3omUOmdQjGv5tQ/VI9Bcon6xFPHjMjlissuXFoJuHbOzI/06BFtbolG5mixSxTrhiq7cLBO6aUtO8otL1I8ZIuYCTENkrMws/Y0Z6iSnu4K+CiABvUOuKwojEE/GoIhLJbOTJt2PPOfmgFHZd6Ijp4XkeA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73678+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1617529296208832.8487620896533; Sun, 4 Apr 2021 02:41:36 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id aJTVYY1788612xE2SaDhSeJ0; Sun, 04 Apr 2021 02:41:35 -0700 X-Received: from mga12.intel.com (mga12.intel.com []) by mx.groups.io with SMTP id smtpd.web09.41073.1617529294169250305 for ; Sun, 04 Apr 2021 02:41:34 -0700 IronPort-SDR: uMNcjVZhHTy0V6OdjqFXAxdBl4wRgRsqRfHJhaGkNXfpHR0rh4UZXqOQRoVkSM9NOVQgo+HJAp UL4utMLf27hg== X-IronPort-AV: E=McAfee;i="6000,8403,9943"; a="172133870" X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="172133870" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:33 -0700 IronPort-SDR: I7ZgUUqymasggTh27CXMPET9jFRN25YWZWrHNPJLqEbjTZvzk6eqUr3fzTlKbbD5eAGJ5NmosW UvE+mw38ItCw== X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="420334762" X-Received: from nldesimo-desk1.amr.corp.intel.com ([10.212.146.162]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:33 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Chasel Chiu , Liming Gao , Eric Dong , Michael Kubacki , Isaac Oram Subject: [edk2-devel] [edk2-platforms] [PATCH v2 1/4] MinPlatformPkg: Add VariableReadLib Date: Sun, 4 Apr 2021 02:40:36 -0700 Message-Id: <20210404094039.2701-2-nathaniel.l.desimone@intel.com> In-Reply-To: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> References: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nathaniel.l.desimone@intel.com X-Gm-Message-State: O3cYRMbPUa6ndncz6szR0CIox1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617529295; bh=56AG6fyE1H7FatfyQNmxwOQD83JYIE7kJjgl+YwddbY=; h=Cc:Date:From:Reply-To:Subject:To; b=M4o8ZxLxKlr0iyogfGa+Of3JidBjYQihllyVrX0IlHXnVAHA5qoxX9W71K8z22kOiC5 mluIreOXrYECZj28A8WDKWGYyIKDYWBoHrwmBs7qUp9jfie70qneglyQ0HE6BrRBIfrVz HF8w9a0UwUoRX65aoemv6TduEwM1+bdpWA8= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" VariableReadLib is a phase agnostic libary for reading UEFI Variables. This library provides the MinGetVariable() and MinGetNextVariableName() APIs which are usable PEI, DXE, and SMM. Cc: Chasel Chiu Cc: Liming Gao Cc: Eric Dong Cc: Michael Kubacki Cc: Isaac Oram Signed-off-by: Nate DeSimone --- .../MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc | 10 +- .../MinPlatformPkg/Include/Dsc/CorePeiLib.dsc | 9 +- .../Include/Library/VariableReadLib.h | 87 ++++++++++ .../DxeRuntimeVariableReadLib.c | 115 +++++++++++++ .../DxeRuntimeVariableReadLib.inf | 41 +++++ .../PeiVariableReadLib/PeiVariableReadLib.c | 153 ++++++++++++++++++ .../PeiVariableReadLib/PeiVariableReadLib.inf | 42 +++++ .../SmmVariableReadCommon.c | 114 +++++++++++++ .../StandaloneMmVariableReadLib.inf | 50 ++++++ .../StandaloneMmVariableReadLibConstructor.c | 48 ++++++ .../TraditionalMmVariableReadLib.inf | 49 ++++++ .../TraditionalMmVariableReadLibConstructor.c | 48 ++++++ .../Intel/MinPlatformPkg/MinPlatformPkg.dsc | 3 +- 13 files changed, 761 insertions(+), 8 deletions(-) create mode 100644 Platform/Intel/MinPlatformPkg/Include/Library/VariableR= eadLib.h create mode 100644 Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariabl= eReadLib/DxeRuntimeVariableReadLib.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariabl= eReadLib/DxeRuntimeVariableReadLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLi= b/PeiVariableReadLib.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLi= b/PeiVariableReadLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLi= b/SmmVariableReadCommon.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLi= b/StandaloneMmVariableReadLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLi= b/StandaloneMmVariableReadLibConstructor.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLi= b/TraditionalMmVariableReadLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLi= b/TraditionalMmVariableReadLibConstructor.c diff --git a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc b/Pla= tform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc index fa9098d525..0db1250ab7 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc +++ b/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc @@ -1,7 +1,7 @@ ## @file # Platform description. # -# Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.
# # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -11,7 +11,7 @@ # # Generic EDKII Lib # - =20 + # # DXE phase common # @@ -23,7 +23,7 @@ ExtractGuidedSectionLib|MdePkg/Library/DxeExtractGuidedSectionLib/DxeExt= ractGuidedSectionLib.inf =20 HstiLib|MdePkg/Library/DxeHstiLib/DxeHstiLib.inf - =20 + LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf =20 CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/DxeCpuE= xceptionHandlerLib.inf @@ -46,6 +46,9 @@ =20 VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf =20 +[LibraryClasses.common.DXE_CORE, LibraryClasses.common.DXE_DRIVER, Library= Classes.common.DXE_RUNTIME_DRIVER, LibraryClasses.common.UEFI_DRIVER, Libra= ryClasses.common.UEFI_APPLICATION] + VariableReadLib|MinPlatformPkg/Library/DxeRuntimeVariableReadLib/DxeRunt= imeVariableReadLib.inf + [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf MemoryAllocationLib|MdeModulePkg/Library/DxeCoreMemoryAllocationLib/DxeC= oreMemoryAllocationLib.inf @@ -89,6 +92,7 @@ CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SmmCpuE= xceptionHandlerLib.inf Tcg2PhysicalPresenceLib|SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/S= mmTcg2PhysicalPresenceLib.inf BaseCryptLib|CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf + VariableReadLib|MinPlatformPkg/Library/SmmVariableReadLib/TraditionalMmV= ariableReadLib.inf =20 [LibraryClasses.common.SMM_CORE] PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf diff --git a/Platform/Intel/MinPlatformPkg/Include/Dsc/CorePeiLib.dsc b/Pla= tform/Intel/MinPlatformPkg/Include/Dsc/CorePeiLib.dsc index 2bcaed05a1..d64873ac6d 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Dsc/CorePeiLib.dsc +++ b/Platform/Intel/MinPlatformPkg/Include/Dsc/CorePeiLib.dsc @@ -1,7 +1,7 @@ ## @file # Platform description. # -# Copyright (c) 2017, Intel Corporation. All rights reserved.
+# Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.
# # SPDX-License-Identifier: BSD-2-Clause-Patent # @@ -10,11 +10,11 @@ # # Generic EDKII Lib # - =20 + # # PEI phase common # - =20 + [LibraryClasses.common.SEC,LibraryClasses.common.PEI_CORE,LibraryClasses.c= ommon.PEIM] S3BootScriptLib|MdePkg/Library/BaseS3BootScriptLibNull/BaseS3BootScriptL= ibNull.inf PcdLib|MdePkg/Library/PeiPcdLib/PeiPcdLib.inf @@ -52,7 +52,7 @@ !if gMinPlatformPkgTokenSpaceGuid.PcdPerformanceEnable =3D=3D TRUE PerformanceLib|MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.= inf !endif - =20 + [LibraryClasses.common.PEIM] CpuExceptionHandlerLib|UefiCpuPkg/Library/CpuExceptionHandlerLib/SecPeiC= puExceptionHandlerLib.inf TimerLib|PcAtChipsetPkg/Library/AcpiTimerLib/PeiAcpiTimerLib.inf @@ -70,3 +70,4 @@ !if gMinPlatformPkgTokenSpaceGuid.PcdPerformanceEnable =3D=3D TRUE PerformanceLib|MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.= inf !endif + VariableReadLib|MinPlatformPkg/Library/PeiVariableReadLib/PeiVariableRea= dLib.inf diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/VariableReadLib.= h b/Platform/Intel/MinPlatformPkg/Include/Library/VariableReadLib.h new file mode 100644 index 0000000000..6ff762a960 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/VariableReadLib.h @@ -0,0 +1,87 @@ +/** @file + Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. For example, the PEI implementation of = this + library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses + the UEFI Runtime Services Table, and the SMM implementation uses + EFI_SMM_VARIABLE_PROTOCOL. + + Using this library allows code to be written in a generic manner that ca= n be + used in PEI, DXE, or SMM without modification. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +/** + Returns the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[out] Attributes If not NULL, a pointer to the memory loca= tion to return the + attributes bitmask for the variable. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +VarLibGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes, OPTIONAL + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ); + +/** + Enumerates the current variable names. + + @param[in, out] VariableNameSize The size of the VariableName buffer. T= he size must be large + enough to fit input string supplied in= VariableName buffer. + @param[in, out] VariableName On input, supplies the last VariableNa= me that was returned + by GetNextVariableName(). On output, r= eturns the Nullterminated + string of the current variable. + @param[in, out] VendorGuid On input, supplies the last VendorGuid= that was returned by + GetNextVariableName(). On output, retu= rns the + VendorGuid of the current variable. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = result. + VariableNameSize has been updated with the= size needed to complete the request. + @retval EFI_INVALID_PARAMETER VariableNameSize is NULL. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and Vendo= rGuid are not a name and + GUID of an existing variable. + @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first = VariableNameSize bytes of + the input VariableName buffer. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due to= a hardware error. + +**/ +EFI_STATUS +EFIAPI +VarLibGetNextVariableName ( + IN OUT UINTN *VariableNameSize, + IN OUT CHAR16 *VariableName, + IN OUT EFI_GUID *VendorGuid + ); diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableReadLi= b/DxeRuntimeVariableReadLib.c b/Platform/Intel/MinPlatformPkg/Library/DxeRu= ntimeVariableReadLib/DxeRuntimeVariableReadLib.c new file mode 100644 index 0000000000..f611891d6a --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableReadLib/DxeRu= ntimeVariableReadLib.c @@ -0,0 +1,115 @@ +/** @file + DXE Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. For example, the PEI implementation of = this + library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses + the UEFI Runtime Services Table, and the SMM implementation uses + EFI_SMM_VARIABLE_PROTOCOL. + + Using this library allows code to be written in a generic manner that ca= n be + used in PEI, DXE, or SMM without modification. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +/** + Returns the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[out] Attributes If not NULL, a pointer to the memory loca= tion to return the + attributes bitmask for the variable. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +VarLibGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes, OPTIONAL + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (gRT !=3D NULL) { + Status =3D gRT->GetVariable ( + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + } + return Status; +} + +/** + Enumerates the current variable names. + + @param[in, out] VariableNameSize The size of the VariableName buffer. T= he size must be large + enough to fit input string supplied in= VariableName buffer. + @param[in, out] VariableName On input, supplies the last VariableNa= me that was returned + by GetNextVariableName(). On output, r= eturns the Nullterminated + string of the current variable. + @param[in, out] VendorGuid On input, supplies the last VendorGuid= that was returned by + GetNextVariableName(). On output, retu= rns the + VendorGuid of the current variable. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = result. + VariableNameSize has been updated with the= size needed to complete the request. + @retval EFI_INVALID_PARAMETER VariableNameSize is NULL. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and Vendo= rGuid are not a name and + GUID of an existing variable. + @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first = VariableNameSize bytes of + the input VariableName buffer. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due to= a hardware error. + +**/ +EFI_STATUS +EFIAPI +VarLibGetNextVariableName ( + IN OUT UINTN *VariableNameSize, + IN OUT CHAR16 *VariableName, + IN OUT EFI_GUID *VendorGuid + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (gRT !=3D NULL) { + Status =3D gRT->GetNextVariableName ( + VariableNameSize, + VariableName, + VendorGuid + ); + } + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableReadLi= b/DxeRuntimeVariableReadLib.inf b/Platform/Intel/MinPlatformPkg/Library/Dxe= RuntimeVariableReadLib/DxeRuntimeVariableReadLib.inf new file mode 100644 index 0000000000..848b76344b --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableReadLib/DxeRu= ntimeVariableReadLib.inf @@ -0,0 +1,41 @@ +## @file +# Component description file for DXE Variable Read Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for reading from UEFI variables. For example, the PEI implementation of = this +# library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in PEI, DXE, or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxeRuntimeVariableReadLib + FILE_GUID =3D 9C357AD8-2BF4-450C-9E65-C0938F6D2424 + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + LIBRARY_CLASS =3D VariableReadLib|DXE_CORE DXE_DRIVER D= XE_RUNTIME_DRIVER UEFI_APPLICATION UEFI_DRIVER + +[Packages] + MdePkg/MdePkg.dec + +[Sources] + DxeRuntimeVariableReadLib.c + +[LibraryClasses] + UefiRuntimeServicesTableLib + +[Protocols] + gEfiVariableArchProtocolGuid ## CONSUMES + +[Depex] + gEfiVariableArchProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLib/PeiVa= riableReadLib.c b/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLib/= PeiVariableReadLib.c new file mode 100644 index 0000000000..d9fbce7981 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLib/PeiVariableR= eadLib.c @@ -0,0 +1,153 @@ +/** @file + PEI Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. For example, the PEI implementation of = this + library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses + the UEFI Runtime Services Table, and the SMM implementation uses + EFI_SMM_VARIABLE_PROTOCOL. + + Using this library allows code to be written in a generic manner that ca= n be + used in PEI, DXE, or SMM without modification. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include +#include + +/** + Returns the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[out] Attributes If not NULL, a pointer to the memory loca= tion to return the + attributes bitmask for the variable. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +VarLibGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes, OPTIONAL + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + EFI_STATUS Status; + EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi; + + // + // Locate the variable PPI. + // + Status =3D PeiServicesLocatePpi ( + &gEfiPeiReadOnlyVariable2PpiGuid, + 0, + NULL, + &VariablePpi + ); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + if (VariablePpi !=3D NULL) { + Status =3D VariablePpi->GetVariable ( + VariablePpi, + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + } else { + Status =3D EFI_UNSUPPORTED; + } + return Status; +} + +/** + Enumerates the current variable names. + + @param[in, out] VariableNameSize The size of the VariableName buffer. T= he size must be large + enough to fit input string supplied in= VariableName buffer. + @param[in, out] VariableName On input, supplies the last VariableNa= me that was returned + by GetNextVariableName(). On output, r= eturns the Nullterminated + string of the current variable. + @param[in, out] VendorGuid On input, supplies the last VendorGuid= that was returned by + GetNextVariableName(). On output, retu= rns the + VendorGuid of the current variable. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = result. + VariableNameSize has been updated with the= size needed to complete the request. + @retval EFI_INVALID_PARAMETER VariableNameSize is NULL. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and Vendo= rGuid are not a name and + GUID of an existing variable. + @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first = VariableNameSize bytes of + the input VariableName buffer. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due to= a hardware error. + +**/ +EFI_STATUS +EFIAPI +VarLibGetNextVariableName ( + IN OUT UINTN *VariableNameSize, + IN OUT CHAR16 *VariableName, + IN OUT EFI_GUID *VendorGuid + ) +{ + EFI_STATUS Status; + EFI_PEI_READ_ONLY_VARIABLE2_PPI *VariablePpi; + + // + // Locate the variable PPI. + // + Status =3D PeiServicesLocatePpi ( + &gEfiPeiReadOnlyVariable2PpiGuid, + 0, + NULL, + &VariablePpi + ); + ASSERT_EFI_ERROR (Status); + if (EFI_ERROR (Status)) { + return Status; + } + + if (VariablePpi !=3D NULL) { + Status =3D VariablePpi->NextVariableName ( + VariablePpi, + VariableNameSize, + VariableName, + VendorGuid + ); + } else { + Status =3D EFI_UNSUPPORTED; + } + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLib/PeiVa= riableReadLib.inf b/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLi= b/PeiVariableReadLib.inf new file mode 100644 index 0000000000..b1d8a4b9ea --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/PeiVariableReadLib/PeiVariableR= eadLib.inf @@ -0,0 +1,42 @@ +## @file +# Component description file for PEI Variable Read Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for reading from UEFI variables. For example, the PEI implementation of = this +# library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in PEI, DXE, or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D PeiVariableReadLib + FILE_GUID =3D C8707767-5D9D-476B-81EE-8FAFA7098224 + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D PEIM + LIBRARY_CLASS =3D VariableReadLib|PEI_CORE PEIM + +[Packages] + MdePkg/MdePkg.dec + +[Sources] + PeiVariableReadLib.c + +[LibraryClasses] + DebugLib + PeiServicesLib + +[Ppis] + gEfiPeiReadOnlyVariable2PpiGuid ## CONSUMES + +[Depex] + gEfiPeiReadOnlyVariable2PpiGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/SmmVa= riableReadCommon.c b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadL= ib/SmmVariableReadCommon.c new file mode 100644 index 0000000000..b663b93999 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/SmmVariableR= eadCommon.c @@ -0,0 +1,114 @@ +/** @file + SMM Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. + + This is the common implementation pieces that are shared between + traditional SMM and standalone MM. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +EFI_SMM_VARIABLE_PROTOCOL *mVariableReadLibSmmVariable =3D NULL; + +/** + Returns the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[out] Attributes If not NULL, a pointer to the memory loca= tion to return the + attributes bitmask for the variable. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +VarLibGetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes, OPTIONAL + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (mVariableReadLibSmmVariable !=3D NULL) { + Status =3D mVariableReadLibSmmVariable->SmmGetVariable ( + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + } + return Status; +} + +/** + Enumerates the current variable names. + + @param[in, out] VariableNameSize The size of the VariableName buffer. T= he size must be large + enough to fit input string supplied in= VariableName buffer. + @param[in, out] VariableName On input, supplies the last VariableNa= me that was returned + by GetNextVariableName(). On output, r= eturns the Nullterminated + string of the current variable. + @param[in, out] VendorGuid On input, supplies the last VendorGuid= that was returned by + GetNextVariableName(). On output, retu= rns the + VendorGuid of the current variable. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_BUFFER_TOO_SMALL The VariableNameSize is too small for the = result. + VariableNameSize has been updated with the= size needed to complete the request. + @retval EFI_INVALID_PARAMETER VariableNameSize is NULL. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER The input values of VariableName and Vendo= rGuid are not a name and + GUID of an existing variable. + @retval EFI_INVALID_PARAMETER Null-terminator is not found in the first = VariableNameSize bytes of + the input VariableName buffer. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due to= a hardware error. + +**/ +EFI_STATUS +EFIAPI +VarLibGetNextVariableName ( + IN OUT UINTN *VariableNameSize, + IN OUT CHAR16 *VariableName, + IN OUT EFI_GUID *VendorGuid + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (mVariableReadLibSmmVariable !=3D NULL) { + Status =3D mVariableReadLibSmmVariable->SmmGetNextVariableName ( + VariableNameSize, + VariableName, + VendorGuid + ); + } + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/Stand= aloneMmVariableReadLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVaria= bleReadLib/StandaloneMmVariableReadLib.inf new file mode 100644 index 0000000000..96a4a25fd7 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/StandaloneMm= VariableReadLib.inf @@ -0,0 +1,50 @@ +## @file +# Component description file for Standalone MM Variable Read Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for reading from UEFI variables. For example, the PEI implementation of = this +# library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in PEI, DXE, or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D SmmVariableReadLib + FILE_GUID =3D 46246048-856E-4C60-9026-F15E20C03B68 + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D MM_STANDALONE + PI_SPECIFICATION_VERSION =3D 0x00010032 + LIBRARY_CLASS =3D VariableReadLib|MM_STANDALONE + CONSTRUCTOR =3D StandaloneMmVariableReadLibConstructor + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[Sources] + SmmVariableReadCommon.c + StandaloneMmVariableReadLibConstructor.c + +[LibraryClasses] + DebugLib + MmServicesTableLib + +[Guids] + +[Protocols] + gEfiSmmVariableProtocolGuid ## CONSUMES + +[Pcd] + +[Depex] + gEfiSmmVariableProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/Stand= aloneMmVariableReadLibConstructor.c b/Platform/Intel/MinPlatformPkg/Library= /SmmVariableReadLib/StandaloneMmVariableReadLibConstructor.c new file mode 100644 index 0000000000..d9fb915bb4 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/StandaloneMm= VariableReadLibConstructor.c @@ -0,0 +1,48 @@ +/** @file + Standalone MM Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. + + This is the standalone MM specific LibraryClass constructor. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include + +extern EFI_SMM_VARIABLE_PROTOCOL *mVariableReadLibSmmVariable; + +/** + The constructor function acquires the EFI SMM Variable Services + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the MM System Table. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +StandaloneMmVariableReadLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_MM_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Locate SmmVariableProtocol. + // + Status =3D gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, = (VOID **) &mVariableReadLibSmmVariable); + ASSERT_EFI_ERROR (Status); + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/Tradi= tionalMmVariableReadLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVari= ableReadLib/TraditionalMmVariableReadLib.inf new file mode 100644 index 0000000000..39cd8371dc --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/TraditionalM= mVariableReadLib.inf @@ -0,0 +1,49 @@ +## @file +# Component description file for Traditional MM Variable Read Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for reading from UEFI variables. For example, the PEI implementation of = this +# library uses EFI_PEI_READ_ONLY_VARIABLE2_PPI. The DXE implementation acc= esses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in PEI, DXE, or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D TraditionalMmVariableReadLib + FILE_GUID =3D 50910542-A4ED-4142-AF25-25E141C016FC + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D DXE_SMM_DRIVER + LIBRARY_CLASS =3D VariableReadLib|DXE_SMM_DRIVER SMM_CO= RE + CONSTRUCTOR =3D TraditionalMmVariableReadLibConstruct= or + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[Sources] + SmmVariableReadCommon.c + TraditionalMmVariableReadLibConstructor.c + +[LibraryClasses] + DebugLib + SmmServicesTableLib + +[Guids] + +[Protocols] + gEfiSmmVariableProtocolGuid ## CONSUMES + +[Pcd] + +[Depex] + gEfiSmmVariableProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/Tradi= tionalMmVariableReadLibConstructor.c b/Platform/Intel/MinPlatformPkg/Librar= y/SmmVariableReadLib/TraditionalMmVariableReadLibConstructor.c new file mode 100644 index 0000000000..5d35bedc05 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableReadLib/TraditionalM= mVariableReadLibConstructor.c @@ -0,0 +1,48 @@ +/** @file + Traditional MM Variable Read Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. + + This is the traditional SMM specific LibraryClass constructor. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include + +extern EFI_SMM_VARIABLE_PROTOCOL *mVariableReadLibSmmVariable; + +/** + The constructor function acquires the EFI SMM Variable Services + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +TraditionalMmVariableReadLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Locate SmmVariableProtocol. + // + Status =3D gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,= (VOID **) &mVariableReadLibSmmVariable); + ASSERT_EFI_ERROR (Status); + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc b/Platform/In= tel/MinPlatformPkg/MinPlatformPkg.dsc index 998ee79095..18b5c6f5b1 100644 --- a/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc +++ b/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc @@ -1,7 +1,7 @@ ## @file # Platform description. # -# Copyright (c) 2017 - 2020, Intel Corporation. All rights reserved.
+# Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.
# Copyright (c) Microsoft Corporation.
# # SPDX-License-Identifier: BSD-2-Clause-Patent @@ -120,6 +120,7 @@ PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf SpiFlashCommonLib|MinPlatformPkg/Flash/Library/SpiFlashCommonLibNull/Spi= FlashCommonLibNull.inf StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint= /StandaloneMmDriverEntryPoint.inf + VariableReadLib|MinPlatformPkg/Library/SmmVariableReadLib/StandaloneMmVa= riableReadLib.inf =20 ##########################################################################= ######################### # --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#73678): https://edk2.groups.io/g/devel/message/73678 Mute This Topic: https://groups.io/mt/81841053/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Apr 18 20:00:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+73679+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73679+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617529298; cv=none; d=zohomail.com; s=zohoarc; b=QZK5OsYpyDSbkumHwqUevBoaNvBkoGydeAgM+KP6XMgVZ6EdFRfoWyK8elWFRpK2ufKYflAg9b5Q/BWHJDa7e7xMl9Xnm8tV1ThxA7ROTzcNDT3uj+1176rd9gT7+Sge+ya360HzFb/WW8F0c5SVy1KCUXLwBXg/lrcHMsi6Z+E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617529298; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=MjOmKQIOAyQf3R/MYBoUanNQECOmjJNK7BuweAHxEsI=; b=PMtK9pc7S1DSA1fZbu7vxH6nOkXS8KwO5kpezy5jmJbf+nhNQ3c7lgV80Zr/cEmOvnv52Cxd1n9CAt8SK5TZriWxkmnm0ukLpVfzbICb+RO82Z28Z6jr3v8T1XJBi8GvM8tWV+VGRNAAzL/g6SwqGKGDmLGD6LRzsU9Jp90ImBM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73679+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1617529298709869.4880276579858; Sun, 4 Apr 2021 02:41:38 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id xRojYY1788612xYjlnwTPdMJ; Sun, 04 Apr 2021 02:41:38 -0700 X-Received: from mga12.intel.com (mga12.intel.com []) by mx.groups.io with SMTP id smtpd.web09.41073.1617529294169250305 for ; Sun, 04 Apr 2021 02:41:34 -0700 IronPort-SDR: n7ie1QuS6qkUvqCSBgFZNz67gM45Tyqjh1PW9hNr8s4kuOBLjU17ZyBbFXKb6Igo0HHUm+82C0 +yXn+pa1HIbw== X-IronPort-AV: E=McAfee;i="6000,8403,9943"; a="172133871" X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="172133871" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:34 -0700 IronPort-SDR: v+fJ8M2CadlApP8H0r6V+VhUzuChkbpKW2ITVf/a7gdDoDid/ZJmulu4Z3quhzXc13b+tK3hwf 9pZz4Zlp4zpA== X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="420334764" X-Received: from nldesimo-desk1.amr.corp.intel.com ([10.212.146.162]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:33 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Chasel Chiu , Liming Gao , Eric Dong , Michael Kubacki , Isaac Oram Subject: [edk2-devel] [edk2-platforms] [PATCH v2 2/4] MinPlatformPkg: Add VariableWriteLib Date: Sun, 4 Apr 2021 02:40:37 -0700 Message-Id: <20210404094039.2701-3-nathaniel.l.desimone@intel.com> In-Reply-To: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> References: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nathaniel.l.desimone@intel.com X-Gm-Message-State: nSIOayjZXRmKaRpXQVBhUnBPx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617529298; bh=vieXroBelxzeEuSCoYQJSGWwa3xz2dZugh3O7yi/caE=; h=Cc:Date:From:Reply-To:Subject:To; b=Ej5rxYCEZs6gt2ZmCABiuZ1g3lHZrbu+iATfCsmOvlg2w083fjoMC9klZLyu64GDT4E egNTaK+CZS3HP+wsUIXcF+ii1SviHgSvkUgGhqfqikZRtDdiPNsVtXQZSwnYoGRXc8+ZC NnFF+deaLZMOZ88Bc6+BTR81KEID5lVjM34= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" VariableWriteLib is a phase agnostic library for writing to UEFI Variables. This library provides the MinSetVariable(), MinQueryVariableInfo(), MinIsVariableRequestToLockSupported(), and MinVariableRequestToLock() APIs which are usable in DXE and SMM. Cc: Chasel Chiu Cc: Liming Gao Cc: Eric Dong Cc: Michael Kubacki Cc: Isaac Oram Signed-off-by: Nate DeSimone --- .../MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc | 2 + .../Include/Library/VariableWriteLib.h | 129 +++++++++ .../DxeRuntimeVariableWriteLib.c | 256 ++++++++++++++++++ .../DxeRuntimeVariableWriteLib.inf | 49 ++++ .../SmmVariableWriteCommon.c | 167 ++++++++++++ .../StandaloneMmVariableWriteLib.inf | 45 +++ .../StandaloneMmVariableWriteLibConstructor.c | 48 ++++ .../TraditionalMmVariableWriteLib.inf | 44 +++ ...TraditionalMmVariableWriteLibConstructor.c | 48 ++++ .../Intel/MinPlatformPkg/MinPlatformPkg.dsc | 1 + 10 files changed, 789 insertions(+) create mode 100644 Platform/Intel/MinPlatformPkg/Include/Library/VariableW= riteLib.h create mode 100644 Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariabl= eWriteLib/DxeRuntimeVariableWriteLib.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariabl= eWriteLib/DxeRuntimeVariableWriteLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteL= ib/SmmVariableWriteCommon.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteL= ib/StandaloneMmVariableWriteLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteL= ib/StandaloneMmVariableWriteLibConstructor.c create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteL= ib/TraditionalMmVariableWriteLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteL= ib/TraditionalMmVariableWriteLibConstructor.c diff --git a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc b/Pla= tform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc index 0db1250ab7..57847e6a4d 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc +++ b/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreDxeLib.dsc @@ -48,6 +48,7 @@ =20 [LibraryClasses.common.DXE_CORE, LibraryClasses.common.DXE_DRIVER, Library= Classes.common.DXE_RUNTIME_DRIVER, LibraryClasses.common.UEFI_DRIVER, Libra= ryClasses.common.UEFI_APPLICATION] VariableReadLib|MinPlatformPkg/Library/DxeRuntimeVariableReadLib/DxeRunt= imeVariableReadLib.inf + VariableWriteLib|MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeRu= ntimeVariableWriteLib.inf =20 [LibraryClasses.common.DXE_CORE] HobLib|MdePkg/Library/DxeCoreHobLib/DxeCoreHobLib.inf @@ -93,6 +94,7 @@ Tcg2PhysicalPresenceLib|SecurityPkg/Library/SmmTcg2PhysicalPresenceLib/S= mmTcg2PhysicalPresenceLib.inf BaseCryptLib|CryptoPkg/Library/BaseCryptLib/SmmCryptLib.inf VariableReadLib|MinPlatformPkg/Library/SmmVariableReadLib/TraditionalMmV= ariableReadLib.inf + VariableWriteLib|MinPlatformPkg/Library/SmmVariableWriteLib/TraditionalM= mVariableWriteLib.inf =20 [LibraryClasses.common.SMM_CORE] PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib= .h b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h new file mode 100644 index 0000000000..c52aec93ec --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h @@ -0,0 +1,129 @@ +/** @file + Variable Write Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for writing to UEFI variables. For example, the DXE implementation acces= ses + the UEFI Runtime Services Table, and the SMM implementation uses + EFI_SMM_VARIABLE_PROTOCOL. + + Using this library allows code to be written in a generic manner that ca= n be + used in DXE or SMM without modification. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +/** + Sets the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] Attributes Attributes bitmask to set for the variabl= e. + @param[in] DataSize The size in bytes of the Data buffer. Unl= ess the EFI_VARIABLE_APPEND_WRITE or + EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRI= TE_ACCESS attribute is set, a size of zero + causes the variable to be deleted. When t= he EFI_VARIABLE_APPEND_WRITE attribute is + set, then a SetVariable() call with a Dat= aSize of zero will not cause any change to + the variable value (the timestamp associa= ted with the variable may be updated however + even if no new data value is provided,see= the description of the + EFI_VARIABLE_AUTHENTICATION_2 descriptor = below. In this case the DataSize will not + be zero since the EFI_VARIABLE_AUTHENTICA= TION_2 descriptor will be populated). + @param[in] Data The contents for the variable. + + @retval EFI_SUCCESS The firmware has successfully stored the = variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits,= name, and GUID was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold t= he variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + @retval EFI_SECURITY_VIOLATION The variable could not be written due to = EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, + but the AuthInfo does NOT pass the valida= tion check carried out by the firmware. + + @retval EFI_NOT_FOUND The variable trying to be updated or dele= ted was not found. + +**/ +EFI_STATUS +EFIAPI +VarLibSetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, + IN UINTN DataSize, + IN VOID *Data + ); + +/** + Returns information about the EFI variables. + + @param[in] Attributes Attributes bitmask to specify = the type of variables on + which to return information. + @param[out] MaximumVariableStorageSize On output the maximum size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] RemainingVariableStorageSize Returns the remaining size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] MaximumVariableSize Returns the maximum size of th= e individual EFI + variables associated with the = attributes specified. + + @retval EFI_SUCCESS Valid answer returned. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute= bits was supplied + @retval EFI_UNSUPPORTED The attribute is not supported on t= his platform, and the + MaximumVariableStorageSize, + RemainingVariableStorageSize, Maxim= umVariableSize + are undefined. +**/ +EFI_STATUS +EFIAPI +VarLibQueryVariableInfo ( + IN UINT32 Attributes, + OUT UINT64 *MaximumVariableStorageSize, + OUT UINT64 *RemainingVariableStorageSize, + OUT UINT64 *MaximumVariableSize + ); + +/** + Indicates if the VarLibVariableRequestToLock() API is supported by the c= urrent + VariableWriteLib implementation. At time of writting, this API is not + available in SMM or after ExitBootServices. + + @retval TRUE The VarLibVariableRequestToLock() API is s= upported + @retval FALSE The VarLibVariableRequestToLock() API is n= ot supported +**/ +BOOLEAN +EFIAPI +VarLibIsVariableRequestToLockSupported ( + VOID + ); + +/** + Mark a variable that will become read-only after leaving the DXE phase o= f execution. + Write request coming from SMM environment through EFI_SMM_VARIABLE_PROTO= COL is allowed. + + @param[in] This The EDKII_VARIABLE_LOCK_PROTOCOL instance. + @param[in] VariableName A pointer to the variable name that will be mad= e read-only subsequently. + @param[in] VendorGuid A pointer to the vendor GUID that will be made = read-only subsequently. + + @retval EFI_SUCCESS The variable specified by the VariableName= and the VendorGuid was marked + as pending to be read-only. + @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL. + Or VariableName is an empty string. + @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVE= NT_GROUP_READY_TO_BOOT has + already been signaled. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the l= ock request. +**/ +EFI_STATUS +EFIAPI +VarLibVariableRequestToLock ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ); diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteL= ib/DxeRuntimeVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/Dxe= RuntimeVariableWriteLib/DxeRuntimeVariableWriteLib.c new file mode 100644 index 0000000000..3b7f377bc2 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c @@ -0,0 +1,256 @@ +/** @file + DXE Variable Write Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for writing to UEFI variables. For example, the DXE implementation acces= ses + the UEFI Runtime Services Table, and the SMM implementation uses + EFI_SMM_VARIABLE_PROTOCOL. + + Using this library allows code to be written in a generic manner that ca= n be + used in DXE or SMM without modification. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include +#include + +#include +#include +#include +#include + +STATIC EDKII_VARIABLE_LOCK_PROTOCOL *mVariableWriteLibVariableLock =3D NU= LL; + +/** + Sets the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] Attributes Attributes bitmask to set for the variabl= e. + @param[in] DataSize The size in bytes of the Data buffer. Unl= ess the EFI_VARIABLE_APPEND_WRITE or + EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRI= TE_ACCESS attribute is set, a size of zero + causes the variable to be deleted. When t= he EFI_VARIABLE_APPEND_WRITE attribute is + set, then a SetVariable() call with a Dat= aSize of zero will not cause any change to + the variable value (the timestamp associa= ted with the variable may be updated however + even if no new data value is provided,see= the description of the + EFI_VARIABLE_AUTHENTICATION_2 descriptor = below. In this case the DataSize will not + be zero since the EFI_VARIABLE_AUTHENTICA= TION_2 descriptor will be populated). + @param[in] Data The contents for the variable. + + @retval EFI_SUCCESS The firmware has successfully stored the = variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits,= name, and GUID was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold t= he variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + @retval EFI_SECURITY_VIOLATION The variable could not be written due to = EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, + but the AuthInfo does NOT pass the valida= tion check carried out by the firmware. + + @retval EFI_NOT_FOUND The variable trying to be updated or dele= ted was not found. + +**/ +EFI_STATUS +EFIAPI +VarLibSetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (gRT !=3D NULL) { + Status =3D gRT->SetVariable ( + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + } + return Status; +} + +/** + Returns information about the EFI variables. + + @param[in] Attributes Attributes bitmask to specify = the type of variables on + which to return information. + @param[out] MaximumVariableStorageSize On output the maximum size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] RemainingVariableStorageSize Returns the remaining size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] MaximumVariableSize Returns the maximum size of th= e individual EFI + variables associated with the = attributes specified. + + @retval EFI_SUCCESS Valid answer returned. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute= bits was supplied + @retval EFI_UNSUPPORTED The attribute is not supported on t= his platform, and the + MaximumVariableStorageSize, + RemainingVariableStorageSize, Maxim= umVariableSize + are undefined. +**/ +EFI_STATUS +EFIAPI +VarLibQueryVariableInfo ( + IN UINT32 Attributes, + OUT UINT64 *MaximumVariableStorageSize, + OUT UINT64 *RemainingVariableStorageSize, + OUT UINT64 *MaximumVariableSize + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (gRT !=3D NULL) { + Status =3D gRT->QueryVariableInfo ( + Attributes, + MaximumVariableStorageSize, + RemainingVariableStorageSize, + MaximumVariableSize + ); + } + return Status; +} + +/** + Indicates if the VarLibVariableRequestToLock() API is supported by the c= urrent + VariableWriteLib implementation. At time of writting, this API is not + available in SMM or after ExitBootServices. + + @retval TRUE The VarLibVariableRequestToLock() API is s= upported + @retval FALSE The VarLibVariableRequestToLock() API is n= ot supported +**/ +BOOLEAN +EFIAPI +VarLibIsVariableRequestToLockSupported ( + VOID + ) +{ + if (mVariableWriteLibVariableLock !=3D NULL) { + return TRUE; + } else { + return FALSE; + } +} + +/** + Mark a variable that will become read-only after leaving the DXE phase o= f execution. + Write request coming from SMM environment through EFI_SMM_VARIABLE_PROTO= COL is allowed. + + @param[in] This The EDKII_VARIABLE_LOCK_PROTOCOL instance. + @param[in] VariableName A pointer to the variable name that will be mad= e read-only subsequently. + @param[in] VendorGuid A pointer to the vendor GUID that will be made = read-only subsequently. + + @retval EFI_SUCCESS The variable specified by the VariableName= and the VendorGuid was marked + as pending to be read-only. + @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL. + Or VariableName is an empty string. + @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVE= NT_GROUP_READY_TO_BOOT has + already been signaled. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the l= ock request. +**/ +EFI_STATUS +EFIAPI +VarLibVariableRequestToLock ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (mVariableWriteLibVariableLock !=3D NULL) { + Status =3D mVariableWriteLibVariableLock->RequestToLock ( + mVariableWriteLibVariableLoc= k, + VariableName, + VendorGuid + ); + } + return Status; +} + +/** + Exit Boot Services Event notification handler. + + @param[in] Event Event whose notification function is being invoked. + @param[in] Context Pointer to the notification function's context. + +**/ +VOID +EFIAPI +DxeRuntimeVariableWriteLibOnExitBootServices ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + mVariableWriteLibVariableLock =3D NULL; +} + +/** + The constructor function acquires the Variable Lock Protocol + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +DxeRuntimeVariableWriteLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_EVENT ExitBootServiceEvent; + EFI_EVENT LegacyBootEvent; + + // + // Locate VariableLockProtocol. + // + Status =3D gBS->LocateProtocol (&gEdkiiVariableLockProtocolGuid, NULL, (= VOID **)&mVariableWriteLibVariableLock); + ASSERT_EFI_ERROR (Status); + + // + // Register the event to inform SMM variable that it is at runtime. + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + DxeRuntimeVariableWriteLibOnExitBootServices, + NULL, + &gEfiEventExitBootServicesGuid, + &ExitBootServiceEvent + ); + ASSERT_EFI_ERROR (Status); + + // + // Register the event to inform SMM variable that it is at runtime for l= egacy boot. + // Reuse OnExitBootServices() here. + // + Status =3D EfiCreateEventLegacyBootEx ( + TPL_NOTIFY, + DxeRuntimeVariableWriteLibOnExitBootServices, + NULL, + &LegacyBootEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteL= ib/DxeRuntimeVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/D= xeRuntimeVariableWriteLib/DxeRuntimeVariableWriteLib.inf new file mode 100644 index 0000000000..704a8ac7cc --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.inf @@ -0,0 +1,49 @@ +## @file +# Component description file for DXE Variable Write Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for writing to UEFI variables. For example, the DXE implementation acces= ses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in DXE or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D DxeRuntimeVariableWriteLib + FILE_GUID =3D 9681E383-5FD4-47A4-B4F8-6651EE603E4E + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + LIBRARY_CLASS =3D VariableWriteLib|DXE_CORE DXE_DRIVER = DXE_RUNTIME_DRIVER UEFI_APPLICATION UEFI_DRIVER + CONSTRUCTOR =3D DxeRuntimeVariableWriteLibConstructor + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[Sources] + DxeRuntimeVariableWriteLib.c + +[LibraryClasses] + DebugLib + UefiLib + UefiBootServicesTableLib + UefiRuntimeServicesTableLib + +[Guids] + gEfiEventExitBootServicesGuid ## CONSUMES ## Event + +[Protocols] + gEfiVariableWriteArchProtocolGuid ## CONSUMES + gEdkiiVariableLockProtocolGuid ## CONSUMES + +[Depex] + gEfiVariableWriteArchProtocolGuid AND gEdkiiVariableLockProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmV= ariableWriteCommon.c b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWri= teLib/SmmVariableWriteCommon.c new file mode 100644 index 0000000000..e8d7d19ed1 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c @@ -0,0 +1,167 @@ +/** @file + SMM Variable Write Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for writing to UEFI variables. + + This is the common implementation pieces that are shared between + traditional SMM and standalone MM. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable =3D NULL; + +/** + Sets the value of a variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] Attributes Attributes bitmask to set for the variabl= e. + @param[in] DataSize The size in bytes of the Data buffer. Unl= ess the EFI_VARIABLE_APPEND_WRITE or + EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRI= TE_ACCESS attribute is set, a size of zero + causes the variable to be deleted. When t= he EFI_VARIABLE_APPEND_WRITE attribute is + set, then a SetVariable() call with a Dat= aSize of zero will not cause any change to + the variable value (the timestamp associa= ted with the variable may be updated however + even if no new data value is provided,see= the description of the + EFI_VARIABLE_AUTHENTICATION_2 descriptor = below. In this case the DataSize will not + be zero since the EFI_VARIABLE_AUTHENTICA= TION_2 descriptor will be populated). + @param[in] Data The contents for the variable. + + @retval EFI_SUCCESS The firmware has successfully stored the = variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits,= name, and GUID was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold t= he variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + @retval EFI_SECURITY_VIOLATION The variable could not be written due to = EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS being set, + but the AuthInfo does NOT pass the valida= tion check carried out by the firmware. + + @retval EFI_NOT_FOUND The variable trying to be updated or dele= ted was not found. + +**/ +EFI_STATUS +EFIAPI +VarLibSetVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN UINT32 Attributes, + IN UINTN DataSize, + IN VOID *Data + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (mVariableWriteLibSmmVariable !=3D NULL) { + Status =3D mVariableWriteLibSmmVariable->SmmSetVariable ( + VariableName, + VendorGuid, + Attributes, + DataSize, + Data + ); + } + return Status; +} + +/** + Returns information about the EFI variables. + + @param[in] Attributes Attributes bitmask to specify = the type of variables on + which to return information. + @param[out] MaximumVariableStorageSize On output the maximum size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] RemainingVariableStorageSize Returns the remaining size of = the storage space + available for the EFI variable= s associated with the + attributes specified. + @param[out] MaximumVariableSize Returns the maximum size of th= e individual EFI + variables associated with the = attributes specified. + + @retval EFI_SUCCESS Valid answer returned. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute= bits was supplied + @retval EFI_UNSUPPORTED The attribute is not supported on t= his platform, and the + MaximumVariableStorageSize, + RemainingVariableStorageSize, Maxim= umVariableSize + are undefined. +**/ +EFI_STATUS +EFIAPI +VarLibQueryVariableInfo ( + IN UINT32 Attributes, + OUT UINT64 *MaximumVariableStorageSize, + OUT UINT64 *RemainingVariableStorageSize, + OUT UINT64 *MaximumVariableSize + ) +{ + EFI_STATUS Status =3D EFI_UNSUPPORTED; + + if (mVariableWriteLibSmmVariable !=3D NULL) { + Status =3D mVariableWriteLibSmmVariable->SmmQueryVariableInfo ( + Attributes, + MaximumVariableStorageSize, + RemainingVariableStorageSize, + MaximumVariableSize + ); + } + return Status; +} + +/** + Indicates if the VarLibVariableRequestToLock() API is supported by the c= urrent + VariableWriteLib implementation. At time of writting, this API is not + available in SMM or after ExitBootServices. + + @retval TRUE The VarLibVariableRequestToLock() API is s= upported + @retval FALSE The VarLibVariableRequestToLock() API is n= ot supported +**/ +BOOLEAN +EFIAPI +VarLibIsVariableRequestToLockSupported ( + VOID + ) +{ + return FALSE; +} + +/** + Mark a variable that will become read-only after leaving the DXE phase o= f execution. + Write request coming from SMM environment through EFI_SMM_VARIABLE_PROTO= COL is allowed. + + @param[in] This The EDKII_VARIABLE_LOCK_PROTOCOL instance. + @param[in] VariableName A pointer to the variable name that will be mad= e read-only subsequently. + @param[in] VendorGuid A pointer to the vendor GUID that will be made = read-only subsequently. + + @retval EFI_SUCCESS The variable specified by the VariableName= and the VendorGuid was marked + as pending to be read-only. + @retval EFI_INVALID_PARAMETER VariableName or VendorGuid is NULL. + Or VariableName is an empty string. + @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVE= NT_GROUP_READY_TO_BOOT has + already been signaled. + @retval EFI_OUT_OF_RESOURCES There is not enough resource to hold the l= ock request. +**/ +EFI_STATUS +EFIAPI +VarLibVariableRequestToLock ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + // + // Variable lock protocol is not accessible from SMM + // + return EFI_UNSUPPORTED; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVar= iableWriteLib/StandaloneMmVariableWriteLib.inf new file mode 100644 index 0000000000..4cafc421c4 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf @@ -0,0 +1,45 @@ +## @file +# Component description file for Standalone MM Variable Write Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for writing to UEFI variables. For example, the DXE implementation acces= ses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in DXE or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x0001001B + BASE_NAME =3D SmmVariableWriteLib + FILE_GUID =3D 62A023A6-DEBA-4407-A617-18963090DAFD + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D MM_STANDALONE + PI_SPECIFICATION_VERSION =3D 0x00010032 + LIBRARY_CLASS =3D VariableWriteLib|MM_STANDALONE + CONSTRUCTOR =3D StandaloneMmVariableWriteLibConstruct= or + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[Sources] + SmmVariableWriteCommon.c + StandaloneMmVariableWriteLibConstructor.c + +[LibraryClasses] + DebugLib + MmServicesTableLib + +[Protocols] + gEfiSmmVariableProtocolGuid ## CONSUMES + +[Depex] + gEfiSmmVariableProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libra= ry/SmmVariableWriteLib/StandaloneMmVariableWriteLibConstructor.c new file mode 100644 index 0000000000..4493929328 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c @@ -0,0 +1,48 @@ +/** @file + Standalone MM Variable Write Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. + + This is the standalone MM specific LibraryClass constructor. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include + +extern EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable; + +/** + The constructor function acquires the EFI SMM Variable Services + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the MM System Table. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +StandaloneMmVariableWriteLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_MM_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Locate SmmVariableProtocol. + // + Status =3D gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, = (VOID **) &mVariableWriteLibSmmVariable); + ASSERT_EFI_ERROR (Status); + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVa= riableWriteLib/TraditionalMmVariableWriteLib.inf new file mode 100644 index 0000000000..5d833b7e0f --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf @@ -0,0 +1,44 @@ +## @file +# Component description file for Traditional MM Variable Write Lib +# +# This library provides phase agnostic access to the UEFI Variable Service= s. +# This is done by implementing a wrapper on top of the phase specific mech= anism +# for writing to UEFI variables. For example, the DXE implementation acces= ses +# the UEFI Runtime Services Table, and the SMM implementation uses +# EFI_SMM_VARIABLE_PROTOCOL. +# +# Using this library allows code to be written in a generic manner that ca= n be +# used in DXE or SMM without modification. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D TraditionalMmVariableWriteLib + FILE_GUID =3D 8C385E9B-C260-466C-91D2-43D839712680 + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D DXE_SMM_DRIVER + LIBRARY_CLASS =3D VariableWriteLib|DXE_SMM_DRIVER SMM_C= ORE + CONSTRUCTOR =3D TraditionalMmVariableWriteLibConstruc= tor + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[Sources] + SmmVariableWriteCommon.c + TraditionalMmVariableWriteLibConstructor.c + +[LibraryClasses] + DebugLib + SmmServicesTableLib + +[Protocols] + gEfiSmmVariableProtocolGuid ## CONSUMES + +[Depex] + gEfiSmmVariableProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libr= ary/SmmVariableWriteLib/TraditionalMmVariableWriteLibConstructor.c new file mode 100644 index 0000000000..472ccc5aad --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c @@ -0,0 +1,48 @@ +/** @file + Traditional MM Variable Write Lib + + This library provides phase agnostic access to the UEFI Variable Service= s. + This is done by implementing a wrapper on top of the phase specific mech= anism + for reading from UEFI variables. + + This is the traditional SMM specific LibraryClass constructor. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include + +extern EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable; + +/** + The constructor function acquires the EFI SMM Variable Services + + @param ImageHandle The firmware allocated handle for the EFI image. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The constructor always returns RETURN_SUCCESS. + +**/ +EFI_STATUS +EFIAPI +TraditionalMmVariableWriteLibConstructor ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + // + // Locate SmmVariableProtocol. + // + Status =3D gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,= (VOID **) &mVariableWriteLibSmmVariable); + ASSERT_EFI_ERROR (Status); + return Status; +} diff --git a/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc b/Platform/In= tel/MinPlatformPkg/MinPlatformPkg.dsc index 18b5c6f5b1..e968ec4cb2 100644 --- a/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc +++ b/Platform/Intel/MinPlatformPkg/MinPlatformPkg.dsc @@ -121,6 +121,7 @@ SpiFlashCommonLib|MinPlatformPkg/Flash/Library/SpiFlashCommonLibNull/Spi= FlashCommonLibNull.inf StandaloneMmDriverEntryPoint|MdePkg/Library/StandaloneMmDriverEntryPoint= /StandaloneMmDriverEntryPoint.inf VariableReadLib|MinPlatformPkg/Library/SmmVariableReadLib/StandaloneMmVa= riableReadLib.inf + VariableWriteLib|MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneMm= VariableWriteLib.inf =20 ##########################################################################= ######################### # --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#73679): https://edk2.groups.io/g/devel/message/73679 Mute This Topic: https://groups.io/mt/81841054/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Apr 18 20:00:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+73680+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73680+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617529297; cv=none; d=zohomail.com; s=zohoarc; b=BKI31nveybh0/bGI+G2o5R0rtGj9ffBv9mtonEemp1i75Wi10DK5vcPOgxfgkQGyGiPFl0DUaihYJrBJTYQRnVh3bz+TrafApHzFauM0trRtMkXrtnVL2FbLD8rmnMBXc3FQcUDNKlec30/PydqJuBX3VmQiwZu+Qp67XRbaf8A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617529297; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=mvZYafiHZphFrWqa9O4GP4RnGL0U60DpXmhmTAUOs3Q=; b=FuX3CF6cvXcnWp5OSMuMcvvAujSA968jSGZ2cGXciyJIa1kHZBZ/KJ+5nXiXCvRbwOj0bad4HR9cvIc9Ro+tKzX62rLDhlS99Vafv2gL6ia6KATpYGCsq9VAI4aXgH5gxymdZBZJw3mT+3ZwV6C6YIQUXMUDnJXjxONNPNr/ArM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73680+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 161752929725520.571847960935088; Sun, 4 Apr 2021 02:41:37 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id p8tiYY1788612x0xuN4g33uO; Sun, 04 Apr 2021 02:41:36 -0700 X-Received: from mga12.intel.com (mga12.intel.com []) by mx.groups.io with SMTP id smtpd.web09.41073.1617529294169250305 for ; Sun, 04 Apr 2021 02:41:35 -0700 IronPort-SDR: z61MqiIxNhOc9t8DHAAdP7+oxtvZtKyx9lSCTHnab6EcsDzqWktL1v9yY55YiH0nWdoxfPLOLs kmThE/4zvZ8g== X-IronPort-AV: E=McAfee;i="6000,8403,9943"; a="172133872" X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="172133872" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:34 -0700 IronPort-SDR: JTuATf3+vht1N2wYwhJer284vM5zlr9zXcbMd09+lHsZ5/jMFQ2IwnYQpVoEQ0JGuCd/3KL4oh phVlczG5Zh2A== X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="420334768" X-Received: from nldesimo-desk1.amr.corp.intel.com ([10.212.146.162]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:33 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Chasel Chiu , Liming Gao , Eric Dong , Michael Kubacki , Isaac Oram Subject: [edk2-devel] [edk2-platforms] [PATCH v2 3/4] MinPlatformPkg: Add LargeVariableReadLib Date: Sun, 4 Apr 2021 02:40:38 -0700 Message-Id: <20210404094039.2701-4-nathaniel.l.desimone@intel.com> In-Reply-To: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> References: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nathaniel.l.desimone@intel.com X-Gm-Message-State: qhZN1deEUYK7QK4WgdqIcV2ex1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617529296; bh=r8UofwuVCcZNnaWO+s7IUsMk6lvjYMvB7cnHYq36gbE=; h=Cc:Date:From:Reply-To:Subject:To; b=B5NvXUaTpstIDu6SyA/SE/D+NUJ52gvdQ7a8Z1AN3lvAU+8UYJoyEcYqsAj7KUCcT1Q URps4uMK0YRKZYIimcW+BEFTiFiyvMjPpHG9TYSd09zxsL/+7EG+H7V33EYJwzGMwhzY/ AbM96/FEM2zQz0yjjV0kHvOkt4hKym2ypms= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" LargeVariableReadLib is used to retrieve large data sets using the UEFI Variable Services. At time of writting, most UEFI Variable Services implementations to not allow more than 64KB of data to be stored in a single UEFI variable. This library will split data sets across multiple variables as needed. It adds the GetLargeVariable() API to provide this service. Cc: Chasel Chiu Cc: Liming Gao Cc: Eric Dong Cc: Michael Kubacki Cc: Isaac Oram Signed-off-by: Nate DeSimone --- .../Include/Dsc/CoreCommonLib.dsc | 5 +- .../Include/Library/LargeVariableReadLib.h | 50 +++++ .../BaseLargeVariableReadLib.inf | 44 ++++ .../LargeVariableReadLib.c | 199 ++++++++++++++++++ 4 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 Platform/Intel/MinPlatformPkg/Include/Library/LargeVari= ableReadLib.h create mode 100644 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariable= ReadLib/BaseLargeVariableReadLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariable= ReadLib/LargeVariableReadLib.c diff --git a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc b/= Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc index cf2940cf02..5f2ad3f0f0 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc +++ b/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc @@ -135,13 +135,14 @@ VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf PlatformSecureLib|SecurityPkg/Library/PlatformSecureLibNull/PlatformSecu= reLibNull.inf AuthVariableLib|MdeModulePkg/Library/AuthVariableLibNull/AuthVariableLib= Null.inf - =20 + !if gMinPlatformPkgTokenSpaceGuid.PcdUefiSecureBootEnable =3D=3D TRUE AuthVariableLib|SecurityPkg/Library/AuthVariableLib/AuthVariableLib.inf !endif =20 SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf BmpSupportLib|MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupportLib.i= nf + LargeVariableReadLib|MinPlatformPkg/Library/BaseLargeVariableReadLib/Bas= eLargeVariableReadLib.inf =20 # # CryptLib @@ -165,4 +166,4 @@ =20 SmbusLib|MdePkg/Library/BaseSmbusLibNull/BaseSmbusLibNull.inf VariablePolicyLib|MdeModulePkg/Library/VariablePolicyLib/VariablePolicyL= ib.inf - VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/Var= iablePolicyHelperLib.inf \ No newline at end of file + VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/Var= iablePolicyHelperLib.inf diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableRea= dLib.h b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableReadLib= .h new file mode 100644 index 0000000000..adcfff7a34 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableReadLib.h @@ -0,0 +1,50 @@ +/** @file + Large Variable Read Lib + + This library is used to retrieve large data sets using the UEFI Variable + Services. At time of writing, most UEFI Variable Services implementation= s to + not allow more than 64KB of data to be stored in a single UEFI variable.= This + library will split data sets across multiple variables as needed. + + In the case where more than one variable is needed to store the data, an + integer number will be added to the end of the variable name. This number + will be incremented for each variable as needed to retrieve the entire d= ata + set. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +/** + Returns the value of a large variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +GetLargeVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ); diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableReadLib= /BaseLargeVariableReadLib.inf b/Platform/Intel/MinPlatformPkg/Library/BaseL= argeVariableReadLib/BaseLargeVariableReadLib.inf new file mode 100644 index 0000000000..d522aa84ab --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableReadLib/BaseLa= rgeVariableReadLib.inf @@ -0,0 +1,44 @@ +## @file +# Component description file for Large Variable Read Library +# +# This library is used to retrieve large data sets using the UEFI Variable +# Services. At time of writing, most UEFI Variable Services implementation= s to +# not allow more than 64KB of data to be stored in a single UEFI variable.= This +# library will split data sets across multiple variables as needed. +# +# In the case where more than one variable is needed to store the data, an +# integer number will be added to the end of the variable name. This number +# will be incremented for each variable as needed to retrieve the entire d= ata +# set. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseLargeVariableReadLib + FILE_GUID =3D 4E9D7D31-A7A0-4004-AE93-D12F1AB08730 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D LargeVariableReadLib + +# +# VALID_ARCHITECTURES =3D IA32 X64 EBC +# + +[Sources] + LargeVariableReadLib.c + +[Packages] + MdePkg/MdePkg.dec + MinPlatformPkg/MinPlatformPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + PrintLib + VariableReadLib diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableReadLib= /LargeVariableReadLib.c b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVa= riableReadLib/LargeVariableReadLib.c new file mode 100644 index 0000000000..115f3aeb17 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableReadLib/LargeV= ariableReadLib.c @@ -0,0 +1,199 @@ +/** @file + Large Variable Read Lib + + This library is used to retrieve large data sets using the UEFI Variable + Services. At time of writing, most UEFI Variable Services implementation= s to + not allow more than 64KB of data to be stored in a single UEFI variable.= This + library will split data sets across multiple variables as needed. + + In the case where more than one variable is needed to store the data, an + integer number will be added to the end of the variable name. This number + will be incremented for each variable as needed to retrieve the entire d= ata + set. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include + +// +// 1024 was choosen because this is the size of the SMM communication buff= er +// used by VariableDxeSmm to transfer the VariableName from DXE to SMM. Ch= oosing +// the same size will prevent this library from limiting variable names any +// more than the MdeModulePkg implementation of UEFI Variable Services doe= s. +// +#define MAX_VARIABLE_NAME_SIZE 1024 + +// +// The 2012 Windows Hardware Requirements specified a minimum variable siz= e of +// 32KB. By setting the maximum allowed number of variables to 0x20000, th= is +// allows up to 4GB of data to be stored on most UEFI implementations in +// existence. Older UEFI implementations were known to only provide 8KB per +// variable. In this case, up to 1GB can be stored. Since 1GB vastly excee= ds the +// size of any known NvStorage FV, choosing this number should effectively +// enable all available NvStorage space to be used to store the given data. +// +#define MAX_VARIABLE_SPLIT 131072 // 0x20000 + +// +// There are 6 digits in the number 131072, which means the length of the = string +// representation of this number will be at most 6 characters long. +// +#define MAX_VARIABLE_SPLIT_DIGITS 6 + +/** + Returns the value of a large variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's + variable. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in, out] DataSize On input, the size in bytes of the return= Data buffer. + On output the size of data returned in Da= ta. + @param[out] Data The buffer to return the contents of the = variable. May be NULL + with a zero DataSize in order to determin= e the size buffer needed. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The variable was not found. + @retval EFI_BUFFER_TOO_SMALL The DataSize is too small for the result. + @retval EFI_INVALID_PARAMETER VariableName is NULL. + @retval EFI_INVALID_PARAMETER VendorGuid is NULL. + @retval EFI_INVALID_PARAMETER DataSize is NULL. + @retval EFI_INVALID_PARAMETER The DataSize is not too small and Data is= NULL. + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_SECURITY_VIOLATION The variable could not be retrieved due t= o an authentication failure. + +**/ +EFI_STATUS +EFIAPI +GetLargeVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + CHAR16 TempVariableName[MAX_VARIABLE_NAME_SIZE]; + EFI_STATUS Status; + UINTN TotalSize; + UINTN VarDataSize; + UINTN Index; + UINTN VariableSize; + UINTN BytesRemaining; + UINT8 *OffsetPtr; + + VarDataSize =3D 0; + + // + // First check if a variable with the given name exists + // + Status =3D VarLibGetVariable (VariableName, VendorGuid, NULL, &VarDataSi= ze, NULL); + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + if (*DataSize >=3D VarDataSize) { + if (Data =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Done; + } + DEBUG ((DEBUG_INFO, "GetLargeVariable: Single Variable Found\n")); + Status =3D VarLibGetVariable (VariableName, VendorGuid, NULL, DataSi= ze, Data); + goto Done; + } else { + *DataSize =3D VarDataSize; + Status =3D EFI_BUFFER_TOO_SMALL; + goto Done; + } + + } else if (Status =3D=3D EFI_NOT_FOUND) { + // + // Check if the first variable of a multi-variable set exists + // + if (StrLen (VariableName) >=3D (MAX_VARIABLE_NAME_SIZE - MAX_VARIABLE_= SPLIT_DIGITS)) { + DEBUG ((DEBUG_ERROR, "GetLargeVariable: Variable name too long\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + VarDataSize =3D 0; + Index =3D 0; + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", Vari= ableName, Index); + Status =3D VarLibGetVariable (TempVariableName, VendorGuid, NULL, &Var= DataSize, NULL); + + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + // + // The first variable exists. Calculate the total size of all the va= riables. + // + DEBUG ((DEBUG_INFO, "GetLargeVariable: Multiple Variables Found\n")); + TotalSize =3D 0; + for (Index =3D 0; Index < MAX_VARIABLE_SPLIT; Index++) { + VarDataSize =3D 0; + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", = VariableName, Index); + Status =3D VarLibGetVariable (TempVariableName, VendorGuid, NULL, = &VarDataSize, NULL); + if (Status !=3D EFI_BUFFER_TOO_SMALL) { + break; + } + TotalSize +=3D VarDataSize; + } + DEBUG ((DEBUG_INFO, "TotalSize =3D %d, NumVariables =3D %d\n", Total= Size, Index)); + + // + // Check if the user provided a large enough buffer + // + if (*DataSize >=3D TotalSize) { + if (Data =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Done; + } + + // + // Read the data from all variables + // + OffsetPtr =3D (UINT8 *) Data; + BytesRemaining =3D *DataSize; + for (Index =3D 0; Index < MAX_VARIABLE_SPLIT; Index++) { + VarDataSize =3D 0; + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d"= , VariableName, Index); + VariableSize =3D BytesRemaining; + DEBUG ((DEBUG_INFO, "Reading %s, Guid =3D %g,", TempVariableName= , VendorGuid)); + Status =3D VarLibGetVariable (TempVariableName, VendorGuid, NULL= , &VariableSize, (VOID *) OffsetPtr); + DEBUG ((DEBUG_INFO, " Size %d\n", VariableSize)); + if (EFI_ERROR (Status)) { + if (Status =3D=3D EFI_NOT_FOUND) { + DEBUG ((DEBUG_INFO, "No more variables found\n")); + Status =3D EFI_SUCCESS; // The end has been reached + } + goto Done; + } + + if (VariableSize < BytesRemaining) { + BytesRemaining -=3D VariableSize; + OffsetPtr +=3D VariableSize; + } else { + DEBUG ((DEBUG_INFO, "All data has been read\n")); + BytesRemaining =3D 0; + break; + } + } //End of for loop + + goto Done; + } else { + *DataSize =3D TotalSize; + Status =3D EFI_BUFFER_TOO_SMALL; + goto Done; + } + } else { + Status =3D EFI_NOT_FOUND; + } + } + +Done: + DEBUG ((DEBUG_ERROR, "GetLargeVariable: Status =3D %r\n", Status)); + return Status; +} --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#73680): https://edk2.groups.io/g/devel/message/73680 Mute This Topic: https://groups.io/mt/81841055/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- From nobody Thu Apr 18 20:00:58 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+73681+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73681+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617529297; cv=none; d=zohomail.com; s=zohoarc; b=dO6vk19Gf8yFI+6D4JVllBBq01TTqTlAmsLq67dxm1R14fzHUrsBm3NOtu+dTR6Lq/33MGgToMc0JVhU1N6w56n4zO7c7km5Z5Tfgd9SdrvQt2NXrTH26Tf8DlNfomYIm1Z3bSqwNZgJxkizKgqYGKz5lviFFg+E2IY2PjNmH7Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617529297; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=P2Z4MJfJ08VMPT3AvbYfxRN/52RpRlhXu+cu7m/cmU0=; b=ifTyenxIAfjDudK/v7FIQGly0IFfynLrriwJmLSJsLUI1LQpNn4scrChq7Ji2P8S4Kr2zAqnvGMZERKjxuCGAn9Wgfb4/G9Y7b7jx2Rq7OvDfCTnOfRm4N437s8kNa2ChS/OW7IMY3mj6h0Cn8F+V7O6UV9yk2K9kD2X2VCCz1M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+73681+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1617529297059650.6730954386319; Sun, 4 Apr 2021 02:41:37 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id PFYRYY1788612xJgvTumKC0w; Sun, 04 Apr 2021 02:41:36 -0700 X-Received: from mga12.intel.com (mga12.intel.com []) by mx.groups.io with SMTP id smtpd.web09.41073.1617529294169250305 for ; Sun, 04 Apr 2021 02:41:35 -0700 IronPort-SDR: r3R3LtrGegczo86SeYID8QxB4i/UMXj5NmRjWXza2Ilh5dZD9/mgDrNF/XNYH1lG8vK+8WfjG8 I3pxxcA4NOUw== X-IronPort-AV: E=McAfee;i="6000,8403,9943"; a="172133873" X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="172133873" X-Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:34 -0700 IronPort-SDR: Pd+vC8pmj3QfA98EO88GcHwDlGA9P/xEstnBDTi/d6tG7eza0voUvBse2CbWahdUQ+WfcjExp0 UmrVlpjMQHBw== X-IronPort-AV: E=Sophos;i="5.81,304,1610438400"; d="scan'208";a="420334771" X-Received: from nldesimo-desk1.amr.corp.intel.com ([10.212.146.162]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Apr 2021 02:41:34 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Chasel Chiu , Liming Gao , Eric Dong , Michael Kubacki , Isaac Oram Subject: [edk2-devel] [edk2-platforms] [PATCH v2 4/4] MinPlatformPkg: Add LargeVariableWriteLib Date: Sun, 4 Apr 2021 02:40:39 -0700 Message-Id: <20210404094039.2701-5-nathaniel.l.desimone@intel.com> In-Reply-To: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> References: <20210404094039.2701-1-nathaniel.l.desimone@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,nathaniel.l.desimone@intel.com X-Gm-Message-State: kPKEKWYndT5Ggk9y9MGjs0WLx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617529296; bh=zYVHl/TA10D56XLct3+wmyxPv7BFhuH/V7IjGpG5Kxg=; h=Cc:Date:From:Reply-To:Subject:To; b=EvyPOeVUPDZEt84u+Zh9O2x7pTV30AqeeJkcFczoLS0tK6TnOXzbEkqPH2rk/a7LNkz W1vIi8QkuBmRkMmSCRMALcr0eaUo13rKV+wg9+GagmfHDu/ywXz4MjuwLwbzJKIIN6sfs Ckxcv+OXXdij7qMLw/MQJXGmBKouWlg36Qk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" LargeVariableWriteLib is used to store large data sets using the UEFI Variable Services. At time of writting, most UEFI Variable Services implementations to not allow more than 64KB of data to be stored in a single UEFI variable. This library will split data sets across multiple variables as needed. It adds the SetLargeVariable() API to provide this service. Cc: Chasel Chiu Cc: Liming Gao Cc: Eric Dong Cc: Michael Kubacki Cc: Isaac Oram Signed-off-by: Nate DeSimone --- .../Include/Dsc/CoreCommonLib.dsc | 1 + .../Include/Library/LargeVariableWriteLib.h | 58 +++ .../BaseLargeVariableWriteLib.inf | 44 ++ .../LargeVariableWriteLib.c | 479 ++++++++++++++++++ 4 files changed, 582 insertions(+) create mode 100644 Platform/Intel/MinPlatformPkg/Include/Library/LargeVari= ableWriteLib.h create mode 100644 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariable= WriteLib/BaseLargeVariableWriteLib.inf create mode 100644 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariable= WriteLib/LargeVariableWriteLib.c diff --git a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc b/= Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc index 5f2ad3f0f0..78d66b9072 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc +++ b/Platform/Intel/MinPlatformPkg/Include/Dsc/CoreCommonLib.dsc @@ -143,6 +143,7 @@ SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf BmpSupportLib|MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupportLib.i= nf LargeVariableReadLib|MinPlatformPkg/Library/BaseLargeVariableReadLib/Bas= eLargeVariableReadLib.inf + LargeVariableWriteLib|MinPlatformPkg/Library/BaseLargeVariableWriteLib/B= aseLargeVariableWriteLib.inf =20 # # CryptLib diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableWri= teLib.h b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableWriteL= ib.h new file mode 100644 index 0000000000..d23232ba07 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableWriteLib.h @@ -0,0 +1,58 @@ +/** @file + Large Variable Write Lib + + This library is used to store large data sets using the UEFI Variable Se= rvices. + At time of writting, most UEFI Variable Services implementations to not = allow + more than 64KB of data to be stored in a single UEFI variable. This libr= ary + will split data sets across multiple variables as needed. + + In the case where more than one variable is needed to store the data, an + integer number will be added to the end of the variable name. This number + will be incremented for each variable as needed to store the entire data= set. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +/** + Sets the value of a large variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] LockVariable If TRUE, any further writes to the variab= le will be prevented until the next reset. + Note: LockVariable must be FALSE when run= ning in SMM or after ExitBootServices. + @param[in] DataSize The size in bytes of the Data buffer. A s= ize of zero causes the variable to be deleted. + If DataSize is zero, then LockVariable mu= st be FALSE since a variable that does not + exist cannot be locked. + @param[in] Data The contents for the variable. + + @retval EFI_SUCCESS The firmware has successfully stored the = variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of LockVariable, n= ame, and GUID was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_INVALID_PARAMETER DataSize is zero and LockVariable is TRUE + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold t= he variable and its data. + @retval EFI_OUT_OF_RESOURCES The VariableName is longer than 1018 char= acters + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + + @retval EFI_NOT_FOUND The variable trying to be updated or dele= ted was not found. + +**/ +EFI_STATUS +EFIAPI +SetLargeVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN LockVariable, + IN UINTN DataSize, + IN VOID *Data + ); diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableWriteLi= b/BaseLargeVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/Bas= eLargeVariableWriteLib/BaseLargeVariableWriteLib.inf new file mode 100644 index 0000000000..c52718296c --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableWriteLib/BaseL= argeVariableWriteLib.inf @@ -0,0 +1,44 @@ +## @file +# Component description file for Large Variable Write Library +# +# This library is used to store large data sets using the UEFI Variable Se= rvices. +# At time of writting, most UEFI Variable Services implementations to not = allow +# more than 64KB of data to be stored in a single UEFI variable. This libr= ary +# will split data sets across multiple variables as needed. +# +# In the case where more than one variable is needed to store the data, an +# integer number will be added to the end of the variable name. This number +# will be incremented for each variable as needed to store the entire data= set. +# +# Copyright (c) 2021, Intel Corporation. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D BaseLargeVariableWriteLib + FILE_GUID =3D 13E7DD75-FBE0-4B92-87A9-6BED253EB99E + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D LargeVariableWriteLib + +# +# VALID_ARCHITECTURES =3D IA32 X64 EBC +# + +[Sources] + LargeVariableWriteLib.c + +[Packages] + MdePkg/MdePkg.dec + MinPlatformPkg/MinPlatformPkg.dec + +[LibraryClasses] + BaseLib + BaseMemoryLib + DebugLib + PrintLib + VariableReadLib + VariableWriteLib diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableWriteLi= b/LargeVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/BaseLarge= VariableWriteLib/LargeVariableWriteLib.c new file mode 100644 index 0000000000..5cf4776707 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableWriteLib/Large= VariableWriteLib.c @@ -0,0 +1,479 @@ +/** @file + Large Variable Write Lib + + This library is used to store large data sets using the UEFI Variable Se= rvices. + At time of writting, most UEFI Variable Services implementations to not = allow + more than 64KB of data to be stored in a single UEFI variable. This libr= ary + will split data sets across multiple variables as needed. + + In the case where more than one variable is needed to store the data, an + integer number will be added to the end of the variable name. This number + will be incremented for each variable as needed to store the entire data= set. + + Copyright (c) 2021, Intel Corporation. All rights reserved.
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include + +// +// 1024 was choosen because this is the size of the SMM communication buff= er +// used by VariableDxeSmm to transfer the VariableName from DXE to SMM. Ch= oosing +// the same size will prevent this library from limiting variable names any +// more than the MdeModulePkg implementation of UEFI Variable Services doe= s. +// +#define MAX_VARIABLE_NAME_SIZE 1024 + +// +// The 2012 Windows Hardware Requirements specified a minimum variable siz= e of +// 32KB. By setting the maximum allowed number of variables to 0x20000, th= is +// allows up to 4GB of data to be stored on most UEFI implementations in +// existence. Older UEFI implementations were known to only provide 8KB per +// variable. In this case, up to 1GB can be stored. Since 1GB vastly excee= ds the +// size of any known NvStorage FV, choosing this number should effectively +// enable all available NvStorage space to be used to store the given data. +// +#define MAX_VARIABLE_SPLIT 131072 + +// +// There are 6 digits in the number 131072, which means the length of the = string +// representation of this number will be at most 6 characters long. +// +#define MAX_VARIABLE_SPLIT_DIGITS 6 + +// +// On some architectures (Ex. Itanium) there a requirement to +// maintain DWORD alignment of the variable data. Hence the +// maximum possible padding size is 3. +// +#define MAX_VARIABLE_NAME_PAD_SIZE 3 + +/** + Returns the amount of space remaining for storage of + non-volatile runtime accessible UEFI variables. Runtime accessible UEFI + variables are chosen because this is a BASE LibraryClass. Accordingly, i= t is + possible for a Runtime DXE driver to include this library. To simplify + implementation we only consider the runtime accessible case. + + @retval The remaining storage space for non-volatile runtime accessible = UEFI variables in bytes. + +**/ +UINT64 +GetRemainingVariableStorageSpace ( + VOID + ) +{ + EFI_STATUS Status; + UINT64 MaximumVariableStorageSize; + UINT64 RemainingVariableStorageSize; + UINT64 MaximumVariableSize; + + Status =3D EFI_SUCCESS; + MaximumVariableStorageSize =3D 0; + RemainingVariableStorageSize =3D 0; + MaximumVariableSize =3D 0; + + Status =3D VarLibQueryVariableInfo ( + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |= EFI_VARIABLE_RUNTIME_ACCESS, + &MaximumVariableStorageSize, + &RemainingVariableStorageSize, + &MaximumVariableSize + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "GetVariableSplitSize: QueryVariableInfo failed, = Status =3D %r\n", Status)); + ASSERT_EFI_ERROR (Status); + return 0; + } + + return RemainingVariableStorageSize; +} + +/** + Returns the maximum size of an individual non-volatile runtime accessible + UEFI variable. Runtime accessible UEFI variables are chosen because this= is + a BASE LibraryClass. Accordingly, it is possible for a Runtime DXE drive= r to + include this library. To simplify implementation we only consider the ru= ntime + accessible case. + + @param[in] VariableNameLength Length of the variable name + + @retval The maximum size of an individual UEFI variable. + +**/ +UINT64 +GetVariableSplitSize ( + IN UINTN VariableNameLength + ) +{ + EFI_STATUS Status; + UINT64 MaximumVariableStorageSize; + UINT64 RemainingVariableStorageSize; + UINT64 VariableSplitSize; + + Status =3D EFI_SUCCESS; + MaximumVariableStorageSize =3D 0; + RemainingVariableStorageSize =3D 0; + VariableSplitSize =3D 0; + + Status =3D VarLibQueryVariableInfo ( + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS |= EFI_VARIABLE_RUNTIME_ACCESS, + &MaximumVariableStorageSize, + &RemainingVariableStorageSize, + &VariableSplitSize + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "GetVariableSplitSize: QueryVariableInfo failed, = Status =3D %r\n", Status)); + ASSERT_EFI_ERROR (Status); + return 0; + } else { + // + // The Variable Name counts as part of the consumed NV storage + // space for a UEFI Variable + // + if ((((VariableNameLength + 1) * sizeof (CHAR16)) + MAX_VARIABLE_NAME_= PAD_SIZE) >=3D VariableSplitSize) { + VariableSplitSize =3D 0; + } else { + VariableSplitSize -=3D ((VariableNameLength + 1) * sizeof (CHAR16)) = + MAX_VARIABLE_NAME_PAD_SIZE; + } + } + + return VariableSplitSize; +} + +/** + Deletes a large variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + + @retval EFI_SUCCESS The firmware has successfully deleted the= variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of name, and GUID = was supplied. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_OUT_OF_RESOURCES The VariableName is longer than 1018 char= acters + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + + @retval EFI_NOT_FOUND The variable trying to be deleted was not= found. + +**/ +EFI_STATUS +EFIAPI +DeleteLargeVariableInternal ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + CHAR16 TempVariableName[MAX_VARIABLE_NAME_SIZE]; + EFI_STATUS Status; + EFI_STATUS Status2; + UINTN VarDataSize; + UINTN Index; + + VarDataSize =3D 0; + + // + // First check if a variable with the given name exists + // + Status =3D VarLibGetVariable (VariableName, VendorGuid, NULL, &VarDataSi= ze, NULL); + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + DEBUG ((DEBUG_INFO, "DeleteLargeVariableInternal: Deleting Single Vari= able\n")); + Status =3D VarLibSetVariable ( + VariableName, + VendorGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCES= S | EFI_VARIABLE_RUNTIME_ACCESS, + 0, + NULL + ); + goto Done; + } else if (Status =3D=3D EFI_NOT_FOUND) { + + // + // Check if the first variable of a multi-variable set exists + // + if (StrLen (VariableName) >=3D (MAX_VARIABLE_NAME_SIZE - MAX_VARIABLE_= SPLIT_DIGITS)) { + DEBUG ((DEBUG_ERROR, "DeleteLargeVariableInternal: Variable name too= long\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + VarDataSize =3D 0; + Index =3D 0; + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", Vari= ableName, Index); + Status =3D VarLibGetVariable (TempVariableName, VendorGuid, NULL, &Var= DataSize, NULL); + if (Status =3D=3D EFI_BUFFER_TOO_SMALL) { + + // + // The first variable exists. Delete all the variables. + // + DEBUG ((DEBUG_INFO, "DeleteLargeVariableInternal: Multiple Variables= Found\n")); + Status =3D EFI_SUCCESS; + for (Index =3D 0; Index < MAX_VARIABLE_SPLIT; Index++) { + VarDataSize =3D 0; + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", = VariableName, Index); + DEBUG ((DEBUG_INFO, "Deleting %s, Guid =3D %g\n", TempVariableName= , VendorGuid)); + Status2 =3D VarLibSetVariable ( + TempVariableName, + VendorGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_A= CCESS | EFI_VARIABLE_RUNTIME_ACCESS, + 0, + NULL + ); + if (Status2 =3D=3D EFI_NOT_FOUND) { + DEBUG ((DEBUG_INFO, "DeleteLargeVariableInternal: Deletion Compl= ete.\n")); + break; + } else if (EFI_ERROR (Status2)) { + DEBUG ((DEBUG_ERROR, "DeleteLargeVariableInternal: Error deletin= g variable: Status =3D %r\n", Status2)); + Status =3D Status2; + } + } // End of for loop + } else { + Status =3D EFI_NOT_FOUND; + } + } +Done: + DEBUG ((DEBUG_ERROR, "DeleteLargeVariableInternal: Status =3D %r\n", Sta= tus)); + return Status; +} + +/** + Sets the value of a large variable. + + @param[in] VariableName A Null-terminated string that is the name= of the vendor's variable. + Each VariableName is unique for each Vend= orGuid. VariableName must + contain 1 or more characters. If Variable= Name is an empty string, + then EFI_INVALID_PARAMETER is returned. + @param[in] VendorGuid A unique identifier for the vendor. + @param[in] LockVariable If TRUE, any further writes to the variab= le will be prevented until the next reset. + Note: LockVariable must be FALSE when run= ning in SMM or after ExitBootServices. + @param[in] DataSize The size in bytes of the Data buffer. A s= ize of zero causes the variable to be deleted. + If DataSize is zero, then LockVariable mu= st be FALSE since a variable that does not + exist cannot be locked. + @param[in] Data The contents for the variable. + + @retval EFI_SUCCESS The firmware has successfully stored the = variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of LockVariable, n= ame, and GUID was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty string. + @retval EFI_INVALID_PARAMETER DataSize is zero and LockVariable is TRUE + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold t= he variable and its data. + @retval EFI_OUT_OF_RESOURCES The VariableName is longer than 1018 char= acters + @retval EFI_DEVICE_ERROR The variable could not be retrieved due t= o a hardware error. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be delete= d. + + @retval EFI_NOT_FOUND The variable trying to be updated or dele= ted was not found. + +**/ +EFI_STATUS +EFIAPI +SetLargeVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN LockVariable, + IN UINTN DataSize, + IN VOID *Data + ) +{ + CHAR16 TempVariableName[MAX_VARIABLE_NAME_SIZE]; + UINT64 VariableSplitSize; + UINT64 RemainingVariableStorage; + EFI_STATUS Status; + EFI_STATUS Status2; + UINTN VariableNameLength; + UINTN Index; + UINTN VariablesSaved; + UINT8 *OffsetPtr; + UINTN BytesRemaining; + UINTN SizeToSave; + + // + // Check input parameters. + // + if (VariableName =3D=3D NULL || VariableName[0] =3D=3D 0 || VendorGuid = =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if (DataSize !=3D 0 && Data =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + if (DataSize =3D=3D 0 && LockVariable) { + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Cannot lock a variable that is= being deleted\n")); + return EFI_INVALID_PARAMETER; + } + + VariablesSaved =3D 0; + if (LockVariable && !VarLibIsVariableRequestToLockSupported ()) { + Status =3D EFI_INVALID_PARAMETER; + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Variable locking is not curr= ently supported\n")); + goto Done; + } + + // + // Check if the variable is being deleted. + // + if (DataSize =3D=3D 0) { + DEBUG ((DEBUG_INFO, "SetLargeVariable: Variable is being deleted.\n")); + Status =3D DeleteLargeVariableInternal (VariableName, VendorGuid); + goto Done; + } + + VariableNameLength =3D StrLen (VariableName); + VariableSplitSize =3D GetVariableSplitSize (VariableNameLength); + if (DataSize <=3D VariableSplitSize) { + + // + // A single variable is sufficient to store the data, only create one. + // + DEBUG ((DEBUG_INFO, "SetLargeVariable: Saving using single variable.\n= ")); + Status =3D VarLibSetVariable ( + VariableName, + VendorGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS= | EFI_VARIABLE_RUNTIME_ACCESS, + DataSize, + Data + ); + if (EFI_ERROR (Status)) { + goto Done; + } + if (LockVariable) { + Status =3D VarLibVariableRequestToLock (VariableName, VendorGuid); + } + } else { + // + // Multiple variables are needed + // + + // + // Check the length of the variable name is short enough to allow an i= nteger + // to be appended. + // + if (VariableNameLength >=3D (MAX_VARIABLE_NAME_SIZE - MAX_VARIABLE_SPL= IT_DIGITS)) { + Status =3D EFI_OUT_OF_RESOURCES; + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Variable name too long\n")); + goto Done; + } + + // + // Check that it is possible to store the data using less than + // MAX_VARIABLE_SPLIT variables + // + if ((DataSize / (VariableSplitSize - MAX_VARIABLE_SPLIT_DIGITS)) > MAX= _VARIABLE_SPLIT) { + DEBUG (( + DEBUG_ERROR, + "SetLargeVariable: More than %d variables are needed to store the = data, which exceeds the maximum supported\n", + MAX_VARIABLE_SPLIT + )); + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + // + // Check that there is enough space remaining in the UEFI Variable Ser= vices + // Non-Volatile storage to store the data. + // + RemainingVariableStorage =3D GetRemainingVariableStorageSpace (); + if (DataSize > RemainingVariableStorage) { + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Not enough NV storage space = to store the data\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + DEBUG ((DEBUG_INFO, "SetLargeVariable: Saving using multiple variables= .\n")); + OffsetPtr =3D (UINT8 *) Data; + BytesRemaining =3D DataSize; + VariablesSaved =3D 0; + + // + // Store chunks of data in UEFI variables until all data is stored + // + for (Index =3D 0; (Index < MAX_VARIABLE_SPLIT) && (BytesRemaining > 0)= ; Index++) { + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", Va= riableName, Index); + + SizeToSave =3D 0; + VariableNameLength =3D StrLen (TempVariableName); + VariableSplitSize =3D GetVariableSplitSize (VariableNameLength); + if (VariableSplitSize =3D=3D 0) { + DEBUG ((DEBUG_ERROR, "Unable to save variable, out of NV storage s= pace\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + + if (BytesRemaining > VariableSplitSize) { + SizeToSave =3D (UINTN) VariableSplitSize; + } else { + SizeToSave =3D BytesRemaining; + } + DEBUG ((DEBUG_INFO, "Saving %s, Guid =3D %g, Size %d\n", TempVariabl= eName, VendorGuid, SizeToSave)); + Status =3D VarLibSetVariable ( + TempVariableName, + VendorGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCES= S | EFI_VARIABLE_RUNTIME_ACCESS, + SizeToSave, + (VOID *) OffsetPtr + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Error writting variable: S= tatus =3D %r\n", Status)); + goto Done; + } + VariablesSaved =3D Index; + BytesRemaining -=3D SizeToSave; + OffsetPtr +=3D SizeToSave; + } // End of for loop + + // + // If the user requested that the variables be locked, lock them now t= hat + // all data is saved. + // + if (LockVariable) { + for (Index =3D 0; Index < VariablesSaved; Index++) { + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", = VariableName, Index); + + DEBUG ((DEBUG_INFO, "Locking %s, Guid =3D %g\n", TempVariableName,= VendorGuid)); + Status =3D VarLibVariableRequestToLock (TempVariableName, VendorGu= id); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Error locking variable: = Status =3D %r\n", Status)); + VariablesSaved =3D 0; + goto Done; + } + } + } + } + +Done: + if (EFI_ERROR (Status) && VariablesSaved > 0) { + DEBUG ((DEBUG_ERROR, "SetLargeVariable: An error was encountered, dele= ting variables with partially stored data\n")); + for (Index =3D 0; Index < VariablesSaved; Index++) { + ZeroMem (TempVariableName, MAX_VARIABLE_NAME_SIZE); + UnicodeSPrint (TempVariableName, MAX_VARIABLE_NAME_SIZE, L"%s%d", Va= riableName, Index); + + DEBUG ((DEBUG_INFO, "Deleting %s, Guid =3D %g\n", TempVariableName, = VendorGuid)); + Status2 =3D VarLibSetVariable ( + TempVariableName, + VendorGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACC= ESS | EFI_VARIABLE_RUNTIME_ACCESS, + 0, + NULL + ); + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Error deleting variable: Statu= s =3D %r\n", Status2)); + } + } + DEBUG ((DEBUG_ERROR, "SetLargeVariable: Status =3D %r\n", Status)); + return Status; +} --=20 2.27.0.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#73681): https://edk2.groups.io/g/devel/message/73681 Mute This Topic: https://groups.io/mt/81841056/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-