From nobody Thu Mar 28 17:33:56 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+73739+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+73739+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617737081; cv=none; d=zohomail.com; s=zohoarc; b=hIiSJV3nOdvvwth7NZ8dFX+zjr+Nv4e2SqwsIdd3jWoVuUU/Ui5xFd+zteaRalKqFNyxq8AU09MFrfdEjW8bXU+2RsNJXzDmqPzlghEnOiV6gXgye/WXUC0JLF22DhguBmiNHSApbvmTOZ6zKap2mm3JjgGL/90gHXoBIq3Y/YE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617737081; 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=aUBJVmlxjI6gVGRmyXPN8v3Twm7otPB8ruvL/IPJmw0=; b=YTFBNvL0u/FurMKtZ16fX0HrAQ9W3hm5MSbsQkHHp1/YxlIFv7CSw6dBa4bKik6hzzHqztJp5j+LfGyFjbvRo6zE3m8ojBFMRDxjke75BSNurAnj2rgNur+tScmQ/EnNee2vdB+I3N9nxzAih+c43jhGrbN/mhOIDM7pYWeuvTY= 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+73739+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 1617737081766518.6825430271704; Tue, 6 Apr 2021 12:24:41 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 3JqCYY1788612xfPgElk8nC3; Tue, 06 Apr 2021 12:24:41 -0700 X-Received: from mga05.intel.com (mga05.intel.com []) by mx.groups.io with SMTP id smtpd.web09.1924.1617737068757224224 for ; Tue, 06 Apr 2021 12:24:29 -0700 IronPort-SDR: YhX3qSKc/J4vrSaNWBciUrgwcHE8fanEQUmc6eXWMRmkUi8FpKwzLRbaOoVpgFoF3SjXXiOakq zbuAXqbj2nbQ== X-IronPort-AV: E=McAfee;i="6000,8403,9946"; a="278404598" X-IronPort-AV: E=Sophos;i="5.82,201,1613462400"; d="scan'208";a="278404598" X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2021 12:24:28 -0700 IronPort-SDR: 9qo2tgATfYVEEXITuhxv0A7GEAdmFxHQysb34M7PmvJsQZexOvUJ29hceTw7+Pyu5xRvJcAlvp RCmtsHgiZM1g== X-IronPort-AV: E=Sophos;i="5.82,201,1613462400"; d="scan'208";a="448734180" X-Received: from nldesimo-desk1.amr.corp.intel.com ([10.209.7.29]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Apr 2021 12:24:28 -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 v3 3/4] MinPlatformPkg: Add LargeVariableReadLib Date: Tue, 6 Apr 2021 12:24:10 -0700 Message-Id: <20210406192411.6888-4-nathaniel.l.desimone@intel.com> In-Reply-To: <20210406192411.6888-1-nathaniel.l.desimone@intel.com> References: <20210406192411.6888-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: lNMsL0gNPObek3D3gHfzqQfKx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617737081; bh=k1r5h+JcX8ONwbBF3gx9cUAS5oIngXwLuLgdTLI+4Ao=; h=Cc:Date:From:Reply-To:Subject:To; b=FGZf2wFcL4NIhUxdkwIV/+GXiuFpmdLWFPShCqeoHNLYInBlv3HFxRcZTN+zfPOYAw4 6cueIQQ1wvTy4VVVcu5y6/02JrwUokl00Q3PeDVRecqtCvuKhrSlW6gPLkXDjcROxmhO+ KCfvLIe9HXkrH0w9+t6zJkWUG81GaGM3qZI= 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. The primary use for this library is to create binary compatible drivers and OpROMs which need to work both with TianoCore and other UEFI PI implementations. When customizing and recompiling the platform firmware image is possible, adjusting the value of PcdMaxVariableSize may provide a simpler solution to this problem. Cc: Chasel Chiu Cc: Liming Gao Cc: Eric Dong Cc: Michael Kubacki Cc: Isaac Oram Signed-off-by: Nate DeSimone Reviewed-by: Isaac Oram --- .../Include/Dsc/CoreCommonLib.dsc | 5 +- .../Include/Library/LargeVariableReadLib.h | 56 +++++ .../BaseLargeVariableReadLib.inf | 50 +++++ .../LargeVariableReadLib.c | 199 ++++++++++++++++++ 4 files changed, 308 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..5579492727 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableReadLib.h @@ -0,0 +1,56 @@ +/** @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. + + The primary use for this library is to create binary compatible drivers + and OpROMs which need to work both with TianoCore and other UEFI PI + implementations. When customizing and recompiling the platform firmware = image + is possible, adjusting the value of PcdMaxVariableSize may provide a sim= pler + solution to this problem. + + 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..822febd62b --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableReadLib/BaseLa= rgeVariableReadLib.inf @@ -0,0 +1,50 @@ +## @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. +# +# The primary use for this library is to create binary compatible drivers +# and OpROMs which need to work both with TianoCore and other UEFI PI +# implementations. When customizing and recompiling the platform firmware = image +# is possible, adjusting the value of PcdMaxVariableSize may provide a sim= pler +# solution to this problem. +# +# 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 (#73739): https://edk2.groups.io/g/devel/message/73739 Mute This Topic: https://groups.io/mt/81898861/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-