From nobody Mon Feb 9 06:06:18 2026 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+73740+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+73740+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1617737075; cv=none; d=zohomail.com; s=zohoarc; b=FLonn8VpqW6Jy9XhXPvujzlbyjjYNYllGudHywmyz7fbuLpf+1KsIET3A4fZP/HWfJaKPup0Y1FS6v7BHP0zQMtb9CZG6smzkwZGWniy3LtIVBmWfUehkRPKEsevHb7LWSB3jg45I0O46rOmica5RyDOPx89Ranpnjx3lMJMWIA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1617737075; 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=LwDDwsSJbVQU0Q7BamQcrkdVtIjygD17gtVkxvw6YhI=; b=E/EKK+aRoMY1Ko6ytuVFkdpE8jxTVuloleMXuEPRJHfs9MvcUZk1CeZ/HBzM+gq5fOplC/uQKm6FKdgOahrvfeDN8fcWaU+r3IU7Ufb+NfM0MHn6IszkK/UMp/pBXEZPptRyupPF9by+iA9U07kbUB94tKJP+5pOCs4v0c/CrGQ= 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+73740+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 1617737075779129.9388261008072; Tue, 6 Apr 2021 12:24:35 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id I5nbYY1788612xxc8WdEFkrN; Tue, 06 Apr 2021 12:24:35 -0700 X-Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web09.1925.1617737069885878594 for ; Tue, 06 Apr 2021 12:24:30 -0700 IronPort-SDR: QHGE66aPjnd95fevzFS9LEbInsr4IhCOQmexdBpEghiEoRM+KayK7qJkYT+F64/4TafMkyjlus 1KpZei8lLLEQ== X-IronPort-AV: E=McAfee;i="6000,8403,9946"; a="278404601" X-IronPort-AV: E=Sophos;i="5.82,201,1613462400"; d="scan'208";a="278404601" 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:29 -0700 IronPort-SDR: w2FFKwTb9T9lCteVeBKG095tMHr+D22oB/0lv3dSgIbrFdJAzqiVBVY1x9u4urcYaOTaK29g+B LP03gMar14bA== X-IronPort-AV: E=Sophos;i="5.82,201,1613462400"; d="scan'208";a="448734186" 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 4/4] MinPlatformPkg: Add LargeVariableWriteLib Date: Tue, 6 Apr 2021 12:24:11 -0700 Message-Id: <20210406192411.6888-5-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: 1MgfwD5JXv1yVF0eTAscWWrXx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1617737075; bh=265HaI30bAq4HRTHHZ0ZL5xns6L6wFzopIDOSNunMzU=; h=Cc:Date:From:Reply-To:Subject:To; b=hY5WoY6SDFhvwPo2W4CYJCOXX5NCapZL8GQ4YLRPJNktlvlYeI4GyJmWHX/r80yBLgr 7H5B2/OwRveANzH8hAJyto8aFhFW43n54nqo5OVHbNXGX91kB4JfLUQvuqHUSafZ6ho3r U1mjFj0eF+XQ39cVH+gUybvNFEG7Gx1dJUk= 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. 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 | 1 + .../Include/Library/LargeVariableWriteLib.h | 64 +++ .../BaseLargeVariableWriteLib.inf | 50 ++ .../LargeVariableWriteLib.c | 479 ++++++++++++++++++ 4 files changed, 594 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..6d597447f1 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Include/Library/LargeVariableWriteLib.h @@ -0,0 +1,64 @@ +/** @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. + + 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 + +/** + 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..afecb31e29 --- /dev/null +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableWriteLib/BaseL= argeVariableWriteLib.inf @@ -0,0 +1,50 @@ +## @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. +# +# 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 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 (#73740): https://edk2.groups.io/g/devel/message/73740 Mute This Topic: https://groups.io/mt/81898862/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-