From nobody Thu Mar 28 08:01:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+48071+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48071+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1569473478; cv=none; d=zoho.com; s=zohoarc; b=DO/0xZWLhiBApwKMk/JkxaSfND/tQEMAh0eOBK49brccFh7g2Oz6Epx84ykksOoTShOFkAHfQtaq+MeZlb0bRw7gCM1aPnWbaqePSwoyHsqvOZO7uy26zr0YrP7NMP2jyczXvajb+B6GKPtsGYJkSa3bnPmzXPKMGLq9F8ir15E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569473478; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=XfRe36qaBW5my50KmcELDKeV8mRo3OxxATneNIQcL1A=; b=kVz4CgwEUrk0G4PUYTLo7ZD6twuezHS18OAE3o7E4+WobU9gITAmZZgw4wV5Oi42FoYrBI46BzvLEqm+0/mfYXQjElMGUhKcRRZyEoOXVsydx1ohpJ+8hlVbcy3LmVvanW1EbuOQJSsi9H07HUbE0TtWwaCNj5yywTF6IyKWMH8= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48071+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1569473478344962.975346605916; Wed, 25 Sep 2019 21:51:18 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 2UhWYY1788612xf6dy5n9TRW; Wed, 25 Sep 2019 21:51:17 -0700 X-Received: from mga02.intel.com (mga02.intel.com []) by groups.io with SMTP; Wed, 25 Sep 2019 21:51:16 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 21:51:16 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,550,1559545200"; d="scan'208";a="191570746" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2019 21:51:16 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V1 1/5] MdeModulePkg/Variable: Consolidate common parsing functions Date: Wed, 25 Sep 2019 21:50:42 -0700 Message-Id: <20190926045046.34592-2-michael.a.kubacki@intel.com> In-Reply-To: <20190926045046.34592-1-michael.a.kubacki@intel.com> References: <20190926045046.34592-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: 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,michael.a.kubacki@intel.com X-Gm-Message-State: RJWjV2ZdNHWImMUCqqFFeMbrx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1569473477; bh=iB8f8AEYXTjrJaxshcch9R4ewFiGtwtIyLk71OGesWQ=; h=Cc:Date:From:Reply-To:Subject:To; b=nxl2xNmlq7xUjZD3sy9gur7NOgIOEjzvX21XntSBZN/lpshh0N9YmOSOdVocchOtmRa NP0SEJm4/Hvu3RnJ+XpLY4LiKzZLE27zGjW1LVvczVvND2wfVVB1f+L+WOynredN8wVKR L0vQqzuagztoF3eHO6D2fwtNukAxRMtwZfo= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" This change moves the following functions into a dedicated file so they may be used in other variable files as needed. Furthermore, it reduces the overall size of the common Variable.c file. * DataSizeOfVariable () * FindVariableEx () * GetEndPointer () * GetNextVariableEx () * GetNextVariablePtr () * GetStartPointer () * GetVariableDataOffset () * GetVariableDataPtr () * GetVariableHeaderSize () * GetVariableNamePtr () * GetVariableStoreStatus () * GetVendorGuidPtr () * IsAuthenticatedVariable () * IsValidVariableHeader () * NameSizeOfVariable () * SetDataSizeOfVariable () * SetNameSizeOfVariable () * UpdateVariableInfo () * VariableCompareTimeStampInternal () Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf | 4= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf | 4= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf | 8= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf | 9= + MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h | 119= --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h | 25= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h | 342= ++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c | 784= +------------------ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c | 11= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c | 28= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c | 816= ++++++++++++++++++++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 2= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 3= +- 13 files changed, 1273 insertions(+), 882 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.= inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf index 641376c9c5..08a5490787 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf @@ -36,6 +36,10 @@ Variable.c VariableDxe.c Variable.h + VariableNonVolatile.c + VariableNonVolatile.h + VariableParsing.c + VariableParsing.h PrivilegePolymorphic.h Measurement.c TcgMorLockDxe.c diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/M= deModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index 0a160d269d..6dc2721b81 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -45,6 +45,10 @@ Variable.c VariableTraditionalMm.c VariableSmm.c + VariableNonVolatile.c + VariableNonVolatile.h + VariableParsing.c + VariableParsing.h VarCheck.c Variable.h PrivilegePolymorphic.h diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i= nf index 14894e6f13..1873b4fe43 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf @@ -13,7 +13,7 @@ # may not be modified without authorization. If platform fails to protect= these resources, # the authentication service provided in this driver will be broken, and = the behavior is undefined. # -# Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
+# Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -39,6 +39,9 @@ VariableSmmRuntimeDxe.c PrivilegePolymorphic.h Measurement.c + Variable.h + VariableParsing.c + VariableParsing.h =20 [Packages] MdePkg/MdePkg.dec @@ -65,6 +68,9 @@ gEdkiiVariableLockProtocolGuid ## PRODUCES gEdkiiVarCheckProtocolGuid ## PRODUCES =20 +[FeaturePcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics #= # CONSUMES + [Guids] gEfiEventVirtualAddressChangeGuid ## CONSUMES ## Event gEfiEventExitBootServicesGuid ## CONSUMES ## Event diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneM= m.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf index 21bc81163b..ca9d23ce9f 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf @@ -45,6 +45,10 @@ Variable.c VariableSmm.c VariableStandaloneMm.c + VariableNonVolatile.c + VariableNonVolatile.h + VariableParsing.c + VariableParsing.h VarCheck.c Variable.h PrivilegePolymorphic.h @@ -99,6 +103,11 @@ ## SOMETIMES_PRODUCES ## Variable:L"Lang" gEfiGlobalVariableGuid =20 + ## SOMETIMES_CONSUMES ## Variable:L"db" + ## SOMETIMES_CONSUMES ## Variable:L"dbx" + ## SOMETIMES_CONSUMES ## Variable:L"dbt" + gEfiImageSecurityDatabaseGuid + gEfiMemoryOverwriteControlDataGuid ## SOMETIMES_CONSUMES ##= Variable:L"MemoryOverwriteRequestControl" gEfiMemoryOverwriteRequestControlLockGuid ## SOMETIMES_PRODUCES ##= Variable:L"MemoryOverwriteRequestControlLock" =20 diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.h index 9eac43759f..fb574b2e32 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h @@ -179,89 +179,6 @@ FindVariable ( IN BOOLEAN IgnoreRtCheck ); =20 -/** - - Gets the pointer to the end of the variable storage area. - - This function gets pointer to the end of the variable storage - area, according to the input variable store header. - - @param VarStoreHeader Pointer to the Variable Store Header. - - @return Pointer to the end of the variable storage area. - -**/ -VARIABLE_HEADER * -GetEndPointer ( - IN VARIABLE_STORE_HEADER *VarStoreHeader - ); - -/** - This code gets the size of variable header. - - @return Size of variable header in bytes in type UINTN. - -**/ -UINTN -GetVariableHeaderSize ( - VOID - ); - -/** - - This code gets the pointer to the variable name. - - @param Variable Pointer to the Variable Header. - - @return Pointer to Variable Name which is Unicode encoding. - -**/ -CHAR16 * -GetVariableNamePtr ( - IN VARIABLE_HEADER *Variable - ); - -/** - This code gets the pointer to the variable guid. - - @param Variable Pointer to the Variable Header. - - @return A EFI_GUID* pointer to Vendor Guid. - -**/ -EFI_GUID * -GetVendorGuidPtr ( - IN VARIABLE_HEADER *Variable - ); - -/** - - This code gets the pointer to the variable data. - - @param Variable Pointer to the Variable Header. - - @return Pointer to Variable Data. - -**/ -UINT8 * -GetVariableDataPtr ( - IN VARIABLE_HEADER *Variable - ); - -/** - - This code gets the size of variable data. - - @param Variable Pointer to the Variable Header. - - @return Size of variable in bytes. - -**/ -UINTN -DataSizeOfVariable ( - IN VARIABLE_HEADER *Variable - ); - /** This function is to check if the remaining variable space is enough to s= et all Variables from argument list successfully. The purpose of the check @@ -450,17 +367,6 @@ ReclaimForOS( VOID ); =20 -/** - Get non-volatile maximum variable size. - - @return Non-volatile maximum variable size. - -**/ -UINTN -GetNonVolatileMaxVariableSize ( - VOID - ); - /** Get maximum variable size, covering both non-volatile and volatile varia= bles. =20 @@ -546,31 +452,6 @@ VariableServiceGetVariable ( OUT VOID *Data OPTIONAL ); =20 -/** - This code Finds the Next available variable. - - Caution: This function may receive untrusted input. - This function may be invoked in SMM mode. This function will do basic va= lidation, before parse the data. - - @param[in] VariableName Pointer to variable name. - @param[in] VendorGuid Variable Vendor Guid. - @param[out] VariablePtr Pointer to variable header address. - - @retval EFI_SUCCESS The function completed successfully. - @retval EFI_NOT_FOUND The next variable was not found. - @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, wh= ile 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. - -**/ -EFI_STATUS -EFIAPI -VariableServiceGetNextVariableInternal ( - IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid, - OUT VARIABLE_HEADER **VariablePtr - ); - /** =20 This code Finds the Next available variable. diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile= .h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h new file mode 100644 index 0000000000..82572262ef --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.h @@ -0,0 +1,25 @@ +/** @file + Common variable non-volatile store routines. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _VARIABLE_NON_VOLATILE_H_ +#define _VARIABLE_NON_VOLATILE_H_ + +#include "Variable.h" + +/** + Get non-volatile maximum variable size. + + @return Non-volatile maximum variable size. + +**/ +UINTN +GetNonVolatileMaxVariableSize ( + VOID + ); + +#endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h b= /MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h new file mode 100644 index 0000000000..bd617fca10 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.h @@ -0,0 +1,342 @@ +/** @file + Common variable helper routines are shared by the DXE_RUNTIME variable + module and the DXE_SMM variable module. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _VARIABLE_PARSING_H_ +#define _VARIABLE_PARSING_H_ + +#include +#include "Variable.h" + +/** + + This code checks if variable header is valid or not. + + @param Variable Pointer to the Variable Header. + @param VariableStoreEnd Pointer to the Variable Store End. + + @retval TRUE Variable header is valid. + @retval FALSE Variable header is not valid. + +**/ +BOOLEAN +IsValidVariableHeader ( + IN VARIABLE_HEADER *Variable, + IN VARIABLE_HEADER *VariableStoreEnd + ); + +/** + + This code gets the current status of Variable Store. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @retval EfiRaw Variable store status is raw. + @retval EfiValid Variable store status is valid. + @retval EfiInvalid Variable store status is invalid. + +**/ +VARIABLE_STORE_STATUS +GetVariableStoreStatus ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ); + +/** + This code gets the size of variable header. + + @return Size of variable header in bytes in type UINTN. + +**/ +UINTN +GetVariableHeaderSize ( + VOID + ); + +/** + + This code gets the size of name of variable. + + @param Variable Pointer to the Variable Header. + + @return UINTN Size of variable in bytes. + +**/ +UINTN +NameSizeOfVariable ( + IN VARIABLE_HEADER *Variable + ); + +/** + This code sets the size of name of variable. + + @param[in] Variable Pointer to the Variable Header. + @param[in] NameSize Name size to set. + +**/ +VOID +SetNameSizeOfVariable ( + IN VARIABLE_HEADER *Variable, + IN UINTN NameSize + ); + +/** + + This code gets the size of variable data. + + @param Variable Pointer to the Variable Header. + + @return Size of variable in bytes. + +**/ +UINTN +DataSizeOfVariable ( + IN VARIABLE_HEADER *Variable + ); + +/** + This code sets the size of variable data. + + @param[in] Variable Pointer to the Variable Header. + @param[in] DataSize Data size to set. + +**/ +VOID +SetDataSizeOfVariable ( + IN VARIABLE_HEADER *Variable, + IN UINTN DataSize + ); + +/** + + This code gets the pointer to the variable name. + + @param Variable Pointer to the Variable Header. + + @return Pointer to Variable Name which is Unicode encoding. + +**/ +CHAR16 * +GetVariableNamePtr ( + IN VARIABLE_HEADER *Variable + ); + +/** + This code gets the pointer to the variable guid. + + @param Variable Pointer to the Variable Header. + + @return A EFI_GUID* pointer to Vendor Guid. + +**/ +EFI_GUID * +GetVendorGuidPtr ( + IN VARIABLE_HEADER *Variable + ); + +/** + + This code gets the pointer to the variable data. + + @param Variable Pointer to the Variable Header. + + @return Pointer to Variable Data. + +**/ +UINT8 * +GetVariableDataPtr ( + IN VARIABLE_HEADER *Variable + ); + +/** + This code gets the variable data offset related to variable header. + + @param Variable Pointer to the Variable Header. + + @return Variable Data offset. + +**/ +UINTN +GetVariableDataOffset ( + IN VARIABLE_HEADER *Variable + ); + +/** + + This code gets the pointer to the next variable header. + + @param Variable Pointer to the Variable Header. + + @return Pointer to next variable header. + +**/ +VARIABLE_HEADER * +GetNextVariablePtr ( + IN VARIABLE_HEADER *Variable + ); + +/** + + Gets the pointer to the first variable header in given variable store ar= ea. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @return Pointer to the first variable header. + +**/ +VARIABLE_HEADER * +GetStartPointer ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ); + +/** + + Gets the pointer to the end of the variable storage area. + + This function gets pointer to the end of the variable storage + area, according to the input variable store header. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @return Pointer to the end of the variable storage area. + +**/ +VARIABLE_HEADER * +GetEndPointer ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ); + +/** + Returns if this is a variable that always requires authenticated writes. + There may be other scenarios that result in a variable not identified by= the + function to also require authentication. + + @param[in] VariableName Name of variable. + @param[in] VendorGuid Guid of variable. + + @retval TRUE The variable always requires authenticated writ= es + @retval FALSE The variable may or may not require authenticat= ed writes +**/ +BOOLEAN +IsAuthenticatedVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ); + +/** + Compare two EFI_TIME data. + + + @param FirstTime A pointer to the first EFI_TIME data. + @param SecondTime A pointer to the second EFI_TIME data. + + @retval TRUE The FirstTime is not later than the SecondTim= e. + @retval FALSE The FirstTime is later than the SecondTime. + +**/ +BOOLEAN +VariableCompareTimeStampInternal ( + IN EFI_TIME *FirstTime, + IN EFI_TIME *SecondTime + ); + +/** + Find the variable in the specified variable store. + + @param[in] VariableName Name of the variable to be found + @param[in] VendorGuid Vendor GUID to be found. + @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS = attribute + check at runtime when searching var= iable. + @param[in, out] PtrTrack Variable Track Pointer structure th= at contains Variable Information. + + @retval EFI_SUCCESS Variable found successfully + @retval EFI_NOT_FOUND Variable not found +**/ +EFI_STATUS +FindVariableEx ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN IgnoreRtCheck, + IN OUT VARIABLE_POINTER_TRACK *PtrTrack + ); + +/** + This code finds the next available variable. + + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode. This function will do basic va= lidation, before parse the data. + + @param[in] VariableName Pointer to variable name. + @param[in] VendorGuid Variable Vendor Guid. + @param[in] VariableStoreList A list of variable stores that should be u= sed to get the next variable. + The maximum number of entries is the max v= alue of VARIABLE_STORE_TYPE. + @param[out] VariablePtr Pointer to variable header address. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_INVALID_PARAMETER If VariableName is nt an empty string, whi= le 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. + +**/ +EFI_STATUS +EFIAPI +GetNextVariableEx ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN VARIABLE_STORE_HEADER **VariableStoreList, + OUT VARIABLE_HEADER **VariablePtr + ); + +/** + Routine used to track statistical information about variable usage. + The data is stored in the EFI system table so it can be accessed later. + VariableInfo.efi can dump out the table. Only Boot Services variable + accesses are tracked by this code. The PcdVariableCollectStatistics + build flag controls if this feature is enabled. + + A read that hits in the cache will have Read and Cache true for + the transaction. Data is allocated by this routine, but never + freed. + + @param[in] VariableName Name of the Variable to track. + @param[in] VendorGuid Guid of the Variable to track. + @param[in] Volatile TRUE if volatile FALSE if non-volatile. + @param[in] Read TRUE if GetVariable() was called. + @param[in] Write TRUE if SetVariable() was called. + @param[in] Delete TRUE if deleted via SetVariable(). + @param[in] Cache TRUE for a cache hit. + @param[in,out] VariableInfo Pointer to a pointer of VARIABLE_INFO_ENT= RY structures. + +**/ +VOID +UpdateVariableInfo ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN Volatile, + IN BOOLEAN Read, + IN BOOLEAN Write, + IN BOOLEAN Delete, + IN BOOLEAN Cache, + IN OUT VARIABLE_INFO_ENTRY **VariableInfo + ); + +/** + Initializes context needed for variable helpers. + + @param[in] AuthFormat If true then indicates authenticate= d variables are supported + + @retval EFI_SUCCESS Initialized successfully + @retval Others An error occurred during initializa= tion +**/ +EFI_STATUS +EFIAPI +InitVariableHelpers ( + IN BOOLEAN AuthFormat + ); + +#endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.c index f32c9c2808..d14fecc830 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -23,6 +23,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent **/ =20 #include "Variable.h" +#include "VariableNonVolatile.h" +#include "VariableParsing.h" =20 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; =20 @@ -92,131 +94,6 @@ AUTH_VAR_LIB_CONTEXT_IN mAuthContextIn =3D { =20 AUTH_VAR_LIB_CONTEXT_OUT mAuthContextOut; =20 -/** - Routine used to track statistical information about variable usage. - The data is stored in the EFI system table so it can be accessed later. - VariableInfo.efi can dump out the table. Only Boot Services variable - accesses are tracked by this code. The PcdVariableCollectStatistics - build flag controls if this feature is enabled. - - A read that hits in the cache will have Read and Cache true for - the transaction. Data is allocated by this routine, but never - freed. - - @param[in] VariableName Name of the Variable to track. - @param[in] VendorGuid Guid of the Variable to track. - @param[in] Volatile TRUE if volatile FALSE if non-volatile. - @param[in] Read TRUE if GetVariable() was called. - @param[in] Write TRUE if SetVariable() was called. - @param[in] Delete TRUE if deleted via SetVariable(). - @param[in] Cache TRUE for a cache hit. - -**/ -VOID -UpdateVariableInfo ( - IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid, - IN BOOLEAN Volatile, - IN BOOLEAN Read, - IN BOOLEAN Write, - IN BOOLEAN Delete, - IN BOOLEAN Cache - ) -{ - VARIABLE_INFO_ENTRY *Entry; - - if (FeaturePcdGet (PcdVariableCollectStatistics)) { - - if (AtRuntime ()) { - // Don't collect statistics at runtime. - return; - } - - if (gVariableInfo =3D=3D NULL) { - // - // On the first call allocate a entry and place a pointer to it in - // the EFI System Table. - // - gVariableInfo =3D AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY)); - ASSERT (gVariableInfo !=3D NULL); - - CopyGuid (&gVariableInfo->VendorGuid, VendorGuid); - gVariableInfo->Name =3D AllocateZeroPool (StrSize (VariableName)); - ASSERT (gVariableInfo->Name !=3D NULL); - StrCpyS (gVariableInfo->Name, StrSize(VariableName)/sizeof(CHAR16), = VariableName); - gVariableInfo->Volatile =3D Volatile; - } - - - for (Entry =3D gVariableInfo; Entry !=3D NULL; Entry =3D Entry->Next) { - if (CompareGuid (VendorGuid, &Entry->VendorGuid)) { - if (StrCmp (VariableName, Entry->Name) =3D=3D 0) { - if (Read) { - Entry->ReadCount++; - } - if (Write) { - Entry->WriteCount++; - } - if (Delete) { - Entry->DeleteCount++; - } - if (Cache) { - Entry->CacheCount++; - } - - return; - } - } - - if (Entry->Next =3D=3D NULL) { - // - // If the entry is not in the table add it. - // Next iteration of the loop will fill in the data. - // - Entry->Next =3D AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY)); - ASSERT (Entry->Next !=3D NULL); - - CopyGuid (&Entry->Next->VendorGuid, VendorGuid); - Entry->Next->Name =3D AllocateZeroPool (StrSize (VariableName)); - ASSERT (Entry->Next->Name !=3D NULL); - StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), = VariableName); - Entry->Next->Volatile =3D Volatile; - } - - } - } -} - - -/** - - This code checks if variable header is valid or not. - - @param Variable Pointer to the Variable Header. - @param VariableStoreEnd Pointer to the Variable Store End. - - @retval TRUE Variable header is valid. - @retval FALSE Variable header is not valid. - -**/ -BOOLEAN -IsValidVariableHeader ( - IN VARIABLE_HEADER *Variable, - IN VARIABLE_HEADER *VariableStoreEnd - ) -{ - if ((Variable =3D=3D NULL) || (Variable >=3D VariableStoreEnd) || (Varia= ble->StartId !=3D VARIABLE_DATA)) { - // - // Variable is NULL or has reached the end of variable store, - // or the StartId is not correct. - // - return FALSE; - } - - return TRUE; -} - - /** =20 This function writes data to the FWH at the correct LBA even if the LBAs @@ -376,345 +253,6 @@ UpdateVariableStore ( return EFI_SUCCESS; } =20 - -/** - - This code gets the current status of Variable Store. - - @param VarStoreHeader Pointer to the Variable Store Header. - - @retval EfiRaw Variable store status is raw. - @retval EfiValid Variable store status is valid. - @retval EfiInvalid Variable store status is invalid. - -**/ -VARIABLE_STORE_STATUS -GetVariableStoreStatus ( - IN VARIABLE_STORE_HEADER *VarStoreHeader - ) -{ - if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariable= Guid) || - CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) && - VarStoreHeader->Format =3D=3D VARIABLE_STORE_FORMATTED && - VarStoreHeader->State =3D=3D VARIABLE_STORE_HEALTHY - ) { - - return EfiValid; - } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] =3D=3D 0xffffffff= && - ((UINT32 *)(&VarStoreHeader->Signature))[1] =3D=3D 0xffffffff= && - ((UINT32 *)(&VarStoreHeader->Signature))[2] =3D=3D 0xffffffff= && - ((UINT32 *)(&VarStoreHeader->Signature))[3] =3D=3D 0xffffffff= && - VarStoreHeader->Size =3D=3D 0xffffffff && - VarStoreHeader->Format =3D=3D 0xff && - VarStoreHeader->State =3D=3D 0xff - ) { - - return EfiRaw; - } else { - return EfiInvalid; - } -} - -/** - This code gets the size of variable header. - - @return Size of variable header in bytes in type UINTN. - -**/ -UINTN -GetVariableHeaderSize ( - VOID - ) -{ - UINTN Value; - - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - Value =3D sizeof (AUTHENTICATED_VARIABLE_HEADER); - } else { - Value =3D sizeof (VARIABLE_HEADER); - } - - return Value; -} - -/** - - This code gets the size of name of variable. - - @param Variable Pointer to the Variable Header. - - @return UINTN Size of variable in bytes. - -**/ -UINTN -NameSizeOfVariable ( - IN VARIABLE_HEADER *Variable - ) -{ - AUTHENTICATED_VARIABLE_HEADER *AuthVariable; - - AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - if (AuthVariable->State =3D=3D (UINT8) (-1) || - AuthVariable->DataSize =3D=3D (UINT32) (-1) || - AuthVariable->NameSize =3D=3D (UINT32) (-1) || - AuthVariable->Attributes =3D=3D (UINT32) (-1)) { - return 0; - } - return (UINTN) AuthVariable->NameSize; - } else { - if (Variable->State =3D=3D (UINT8) (-1) || - Variable->DataSize =3D=3D (UINT32) (-1) || - Variable->NameSize =3D=3D (UINT32) (-1) || - Variable->Attributes =3D=3D (UINT32) (-1)) { - return 0; - } - return (UINTN) Variable->NameSize; - } -} - -/** - This code sets the size of name of variable. - - @param[in] Variable Pointer to the Variable Header. - @param[in] NameSize Name size to set. - -**/ -VOID -SetNameSizeOfVariable ( - IN VARIABLE_HEADER *Variable, - IN UINTN NameSize - ) -{ - AUTHENTICATED_VARIABLE_HEADER *AuthVariable; - - AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - AuthVariable->NameSize =3D (UINT32) NameSize; - } else { - Variable->NameSize =3D (UINT32) NameSize; - } -} - -/** - - This code gets the size of variable data. - - @param Variable Pointer to the Variable Header. - - @return Size of variable in bytes. - -**/ -UINTN -DataSizeOfVariable ( - IN VARIABLE_HEADER *Variable - ) -{ - AUTHENTICATED_VARIABLE_HEADER *AuthVariable; - - AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - if (AuthVariable->State =3D=3D (UINT8) (-1) || - AuthVariable->DataSize =3D=3D (UINT32) (-1) || - AuthVariable->NameSize =3D=3D (UINT32) (-1) || - AuthVariable->Attributes =3D=3D (UINT32) (-1)) { - return 0; - } - return (UINTN) AuthVariable->DataSize; - } else { - if (Variable->State =3D=3D (UINT8) (-1) || - Variable->DataSize =3D=3D (UINT32) (-1) || - Variable->NameSize =3D=3D (UINT32) (-1) || - Variable->Attributes =3D=3D (UINT32) (-1)) { - return 0; - } - return (UINTN) Variable->DataSize; - } -} - -/** - This code sets the size of variable data. - - @param[in] Variable Pointer to the Variable Header. - @param[in] DataSize Data size to set. - -**/ -VOID -SetDataSizeOfVariable ( - IN VARIABLE_HEADER *Variable, - IN UINTN DataSize - ) -{ - AUTHENTICATED_VARIABLE_HEADER *AuthVariable; - - AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - AuthVariable->DataSize =3D (UINT32) DataSize; - } else { - Variable->DataSize =3D (UINT32) DataSize; - } -} - -/** - - This code gets the pointer to the variable name. - - @param Variable Pointer to the Variable Header. - - @return Pointer to Variable Name which is Unicode encoding. - -**/ -CHAR16 * -GetVariableNamePtr ( - IN VARIABLE_HEADER *Variable - ) -{ - return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ()); -} - -/** - This code gets the pointer to the variable guid. - - @param Variable Pointer to the Variable Header. - - @return A EFI_GUID* pointer to Vendor Guid. - -**/ -EFI_GUID * -GetVendorGuidPtr ( - IN VARIABLE_HEADER *Variable - ) -{ - AUTHENTICATED_VARIABLE_HEADER *AuthVariable; - - AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; - if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { - return &AuthVariable->VendorGuid; - } else { - return &Variable->VendorGuid; - } -} - -/** - - This code gets the pointer to the variable data. - - @param Variable Pointer to the Variable Header. - - @return Pointer to Variable Data. - -**/ -UINT8 * -GetVariableDataPtr ( - IN VARIABLE_HEADER *Variable - ) -{ - UINTN Value; - - // - // Be careful about pad size for alignment. - // - Value =3D (UINTN) GetVariableNamePtr (Variable); - Value +=3D NameSizeOfVariable (Variable); - Value +=3D GET_PAD_SIZE (NameSizeOfVariable (Variable)); - - return (UINT8 *) Value; -} - -/** - This code gets the variable data offset related to variable header. - - @param Variable Pointer to the Variable Header. - - @return Variable Data offset. - -**/ -UINTN -GetVariableDataOffset ( - IN VARIABLE_HEADER *Variable - ) -{ - UINTN Value; - - // - // Be careful about pad size for alignment - // - Value =3D GetVariableHeaderSize (); - Value +=3D NameSizeOfVariable (Variable); - Value +=3D GET_PAD_SIZE (NameSizeOfVariable (Variable)); - - return Value; -} - -/** - - This code gets the pointer to the next variable header. - - @param Variable Pointer to the Variable Header. - - @return Pointer to next variable header. - -**/ -VARIABLE_HEADER * -GetNextVariablePtr ( - IN VARIABLE_HEADER *Variable - ) -{ - UINTN Value; - - Value =3D (UINTN) GetVariableDataPtr (Variable); - Value +=3D DataSizeOfVariable (Variable); - Value +=3D GET_PAD_SIZE (DataSizeOfVariable (Variable)); - - // - // Be careful about pad size for alignment. - // - return (VARIABLE_HEADER *) HEADER_ALIGN (Value); -} - -/** - - Gets the pointer to the first variable header in given variable store ar= ea. - - @param VarStoreHeader Pointer to the Variable Store Header. - - @return Pointer to the first variable header. - -**/ -VARIABLE_HEADER * -GetStartPointer ( - IN VARIABLE_STORE_HEADER *VarStoreHeader - ) -{ - // - // The start of variable store. - // - return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1); -} - -/** - - Gets the pointer to the end of the variable storage area. - - This function gets pointer to the end of the variable storage - area, according to the input variable store header. - - @param VarStoreHeader Pointer to the Variable Store Header. - - @return Pointer to the end of the variable storage area. - -**/ -VARIABLE_HEADER * -GetEndPointer ( - IN VARIABLE_STORE_HEADER *VarStoreHeader - ) -{ - // - // The end of variable store - // - return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarSto= reHeader->Size); -} - /** Record variable error flag. =20 @@ -1228,75 +766,6 @@ Done: return Status; } =20 -/** - Find the variable in the specified variable store. - - @param[in] VariableName Name of the variable to be found - @param[in] VendorGuid Vendor GUID to be found. - @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS = attribute - check at runtime when searching var= iable. - @param[in, out] PtrTrack Variable Track Pointer structure th= at contains Variable Information. - - @retval EFI_SUCCESS Variable found successfully - @retval EFI_NOT_FOUND Variable not found -**/ -EFI_STATUS -FindVariableEx ( - IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid, - IN BOOLEAN IgnoreRtCheck, - IN OUT VARIABLE_POINTER_TRACK *PtrTrack - ) -{ - VARIABLE_HEADER *InDeletedVariable; - VOID *Point; - - PtrTrack->InDeletedTransitionPtr =3D NULL; - - // - // Find the variable by walk through HOB, volatile and non-volatile vari= able store. - // - InDeletedVariable =3D NULL; - - for ( PtrTrack->CurrPtr =3D PtrTrack->StartPtr - ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr) - ; PtrTrack->CurrPtr =3D GetNextVariablePtr (PtrTrack->CurrPtr) - ) { - if (PtrTrack->CurrPtr->State =3D=3D VAR_ADDED || - PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION & VAR_A= DDED) - ) { - if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attribute= s & EFI_VARIABLE_RUNTIME_ACCESS) !=3D 0)) { - if (VariableName[0] =3D=3D 0) { - if (PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION &= VAR_ADDED)) { - InDeletedVariable =3D PtrTrack->CurrPtr; - } else { - PtrTrack->InDeletedTransitionPtr =3D InDeletedVariable; - return EFI_SUCCESS; - } - } else { - if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr= ))) { - Point =3D (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr); - - ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) !=3D 0); - if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTr= ack->CurrPtr)) =3D=3D 0) { - if (PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITI= ON & VAR_ADDED)) { - InDeletedVariable =3D PtrTrack->CurrPtr; - } else { - PtrTrack->InDeletedTransitionPtr =3D InDeletedVariable; - return EFI_SUCCESS; - } - } - } - } - } - } - } - - PtrTrack->CurrPtr =3D InDeletedVariable; - return (PtrTrack->CurrPtr =3D=3D NULL) ? EFI_NOT_FOUND : EFI_SUCCESS; -} - - /** Finds variable in storage blocks of volatile and non-volatile storage ar= eas. =20 @@ -2078,38 +1547,6 @@ AutoUpdateLangVariable ( } } =20 -/** - Compare two EFI_TIME data. - - - @param FirstTime A pointer to the first EFI_TIME data. - @param SecondTime A pointer to the second EFI_TIME data. - - @retval TRUE The FirstTime is not later than the SecondTim= e. - @retval FALSE The FirstTime is later than the SecondTime. - -**/ -BOOLEAN -VariableCompareTimeStampInternal ( - IN EFI_TIME *FirstTime, - IN EFI_TIME *SecondTime - ) -{ - if (FirstTime->Year !=3D SecondTime->Year) { - return (BOOLEAN) (FirstTime->Year < SecondTime->Year); - } else if (FirstTime->Month !=3D SecondTime->Month) { - return (BOOLEAN) (FirstTime->Month < SecondTime->Month); - } else if (FirstTime->Day !=3D SecondTime->Day) { - return (BOOLEAN) (FirstTime->Day < SecondTime->Day); - } else if (FirstTime->Hour !=3D SecondTime->Hour) { - return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour); - } else if (FirstTime->Minute !=3D SecondTime->Minute) { - return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute); - } - - return (BOOLEAN) (FirstTime->Second <=3D SecondTime->Second); -} - /** Update the variable region with Variable information. If EFI_VARIABLE_AU= THENTICATED_WRITE_ACCESS is set, index of associated public key is needed. @@ -2205,7 +1642,7 @@ UpdateVariable ( // go to delete this variable in variable HOB and // try to flush other variables from HOB to flash. // - UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE,= TRUE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, FALSE,= TRUE, FALSE, &gVariableInfo); FlushHobVariableToFlash (VariableName, VendorGuid); return EFI_SUCCESS; } @@ -2322,7 +1759,7 @@ UpdateVariable ( &State ); if (!EFI_ERROR (Status)) { - UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, = FALSE, FALSE, TRUE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, = FALSE, FALSE, TRUE, FALSE, &gVariableInfo); if (!Variable->Volatile) { CacheVariable->CurrPtr->State =3D State; FlushHobVariableToFlash (VariableName, VendorGuid); @@ -2341,7 +1778,7 @@ UpdateVariable ( // // Variable content unchanged and no need to update timestamp, just = return. // - UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FA= LSE, TRUE, FALSE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, Variable->Volatile, FA= LSE, TRUE, FALSE, FALSE, &gVariableInfo); Status =3D EFI_SUCCESS; goto Done; } else if ((CacheVariable->CurrPtr->State =3D=3D VAR_ADDED) || @@ -2570,7 +2007,7 @@ UpdateVariable ( CacheVariable->CurrPtr =3D (VARIABLE_HEADER *)((UINTN) CacheVari= able->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr)); CacheVariable->InDeletedTransitionPtr =3D NULL; } - UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, = FALSE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, FALSE, FALSE, TRUE, = FALSE, FALSE, &gVariableInfo); FlushHobVariableToFlash (VariableName, VendorGuid); } else { if (IsCommonUserVariable && ((VarSize + mVariableModuleGlobal->Com= monUserVariableTotalSize) > mVariableModuleGlobal->CommonMaxUserVariableSpa= ce)) { @@ -2720,7 +2157,7 @@ UpdateVariable ( CacheVariable->CurrPtr =3D (VARIABLE_HEADER *)((UINTN) CacheVari= able->StartPtr + ((UINTN) Variable->CurrPtr - (UINTN) Variable->StartPtr)); CacheVariable->InDeletedTransitionPtr =3D NULL; } - UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, F= ALSE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, TRUE, FALSE, TRUE, F= ALSE, FALSE, &gVariableInfo); } goto Done; } @@ -2791,7 +2228,7 @@ UpdateVariable ( } =20 if (!EFI_ERROR (Status)) { - UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, F= ALSE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, Volatile, FALSE, TRUE, F= ALSE, FALSE, &gVariableInfo); if (!Volatile) { FlushHobVariableToFlash (VariableName, VendorGuid); } @@ -2870,7 +2307,7 @@ VariableServiceGetVariable ( } =20 *DataSize =3D VarDataSize; - UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE,= FALSE, FALSE, FALSE); + UpdateVariableInfo (VariableName, VendorGuid, Variable.Volatile, TRUE,= FALSE, FALSE, FALSE, &gVariableInfo); =20 Status =3D EFI_SUCCESS; goto Done; @@ -2885,166 +2322,6 @@ Done: return Status; } =20 -/** - This code Finds the Next available variable. - - Caution: This function may receive untrusted input. - This function may be invoked in SMM mode. This function will do basic va= lidation, before parse the data. - - @param[in] VariableName Pointer to variable name. - @param[in] VendorGuid Variable Vendor Guid. - @param[out] VariablePtr Pointer to variable header address. - - @retval EFI_SUCCESS The function completed successfully. - @retval EFI_NOT_FOUND The next variable was not found. - @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, wh= ile 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. - -**/ -EFI_STATUS -EFIAPI -VariableServiceGetNextVariableInternal ( - IN CHAR16 *VariableName, - IN EFI_GUID *VendorGuid, - OUT VARIABLE_HEADER **VariablePtr - ) -{ - VARIABLE_STORE_TYPE Type; - VARIABLE_POINTER_TRACK Variable; - VARIABLE_POINTER_TRACK VariableInHob; - VARIABLE_POINTER_TRACK VariablePtrTrack; - EFI_STATUS Status; - VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax]; - - Status =3D FindVariable (VariableName, VendorGuid, &Variable, &mVariable= ModuleGlobal->VariableGlobal, FALSE); - if (Variable.CurrPtr =3D=3D NULL || EFI_ERROR (Status)) { - // - // For VariableName is an empty string, FindVariable() will try to fin= d and return - // the first qualified variable, and if FindVariable() returns error (= EFI_NOT_FOUND) - // as no any variable is found, still go to return the error (EFI_NOT_= FOUND). - // - if (VariableName[0] !=3D 0) { - // - // For VariableName is not an empty string, and FindVariable() retur= ns error as - // VariableName and VendorGuid are not a name and GUID of an existin= g variable, - // there is no way to get next variable, follow spec to return EFI_I= NVALID_PARAMETER. - // - Status =3D EFI_INVALID_PARAMETER; - } - goto Done; - } - - if (VariableName[0] !=3D 0) { - // - // If variable name is not NULL, get next variable. - // - Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); - } - - // - // 0: Volatile, 1: HOB, 2: Non-Volatile. - // The index and attributes mapping must be kept in this order as FindVa= riable - // makes use of this mapping to implement search algorithm. - // - VariableStoreHeader[VariableStoreTypeVolatile] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase; - VariableStoreHeader[VariableStoreTypeHob] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase; - VariableStoreHeader[VariableStoreTypeNv] =3D mNvVariableCache; - - while (TRUE) { - // - // Switch from Volatile to HOB, to Non-Volatile. - // - while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) { - // - // Find current storage index - // - for (Type =3D (VARIABLE_STORE_TYPE) 0; Type < VariableStoreTypeMax; = Type++) { - if ((VariableStoreHeader[Type] !=3D NULL) && (Variable.StartPtr = =3D=3D GetStartPointer (VariableStoreHeader[Type]))) { - break; - } - } - ASSERT (Type < VariableStoreTypeMax); - // - // Switch to next storage - // - for (Type++; Type < VariableStoreTypeMax; Type++) { - if (VariableStoreHeader[Type] !=3D NULL) { - break; - } - } - // - // Capture the case that - // 1. current storage is the last one, or - // 2. no further storage - // - if (Type =3D=3D VariableStoreTypeMax) { - Status =3D EFI_NOT_FOUND; - goto Done; - } - Variable.StartPtr =3D GetStartPointer (VariableStoreHeader[Type]); - Variable.EndPtr =3D GetEndPointer (VariableStoreHeader[Type]); - Variable.CurrPtr =3D Variable.StartPtr; - } - - // - // Variable is found - // - if (Variable.CurrPtr->State =3D=3D VAR_ADDED || Variable.CurrPtr->Stat= e =3D=3D (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { - if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_R= UNTIME_ACCESS) !=3D 0)) { - if (Variable.CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION & VA= R_ADDED)) { - // - // If it is a IN_DELETED_TRANSITION variable, - // and there is also a same ADDED one at the same time, - // don't return it. - // - VariablePtrTrack.StartPtr =3D Variable.StartPtr; - VariablePtrTrack.EndPtr =3D Variable.EndPtr; - Status =3D FindVariableEx ( - GetVariableNamePtr (Variable.CurrPtr), - GetVendorGuidPtr (Variable.CurrPtr), - FALSE, - &VariablePtrTrack - ); - if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State =3D= =3D VAR_ADDED) { - Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); - continue; - } - } - - // - // Don't return NV variable when HOB overrides it - // - if ((VariableStoreHeader[VariableStoreTypeHob] !=3D NULL) && (Vari= ableStoreHeader[VariableStoreTypeNv] !=3D NULL) && - (Variable.StartPtr =3D=3D GetStartPointer (VariableStoreHeader= [VariableStoreTypeNv])) - ) { - VariableInHob.StartPtr =3D GetStartPointer (VariableStoreHeader[= VariableStoreTypeHob]); - VariableInHob.EndPtr =3D GetEndPointer (VariableStoreHeader[= VariableStoreTypeHob]); - Status =3D FindVariableEx ( - GetVariableNamePtr (Variable.CurrPtr), - GetVendorGuidPtr (Variable.CurrPtr), - FALSE, - &VariableInHob - ); - if (!EFI_ERROR (Status)) { - Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); - continue; - } - } - - *VariablePtr =3D Variable.CurrPtr; - Status =3D EFI_SUCCESS; - goto Done; - } - } - - Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); - } - -Done: - return Status; -} - /** =20 This code Finds the Next available variable. @@ -3082,6 +2359,7 @@ VariableServiceGetNextVariableName ( UINTN MaxLen; UINTN VarNameSize; VARIABLE_HEADER *VariablePtr; + VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax]; =20 if (VariableNameSize =3D=3D NULL || VariableName =3D=3D NULL || VendorGu= id =3D=3D NULL) { return EFI_INVALID_PARAMETER; @@ -3101,7 +2379,16 @@ VariableServiceGetNextVariableName ( =20 AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.Variabl= eServicesLock); =20 - Status =3D VariableServiceGetNextVariableInternal (VariableName, VendorG= uid, &VariablePtr); + // + // 0: Volatile, 1: HOB, 2: Non-Volatile. + // The index and attributes mapping must be kept in this order as FindVa= riable + // makes use of this mapping to implement search algorithm. + // + VariableStoreHeader[VariableStoreTypeVolatile] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase; + VariableStoreHeader[VariableStoreTypeHob] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase; + VariableStoreHeader[VariableStoreTypeNv] =3D mNvVariableCache; + + Status =3D GetNextVariableEx (VariableName, VendorGuid, VariableStoreHea= der, &VariablePtr); if (!EFI_ERROR (Status)) { VarNameSize =3D NameSizeOfVariable (VariablePtr); ASSERT (VarNameSize !=3D 0); @@ -3720,25 +3007,6 @@ ReclaimForOS( } } =20 -/** - Get non-volatile maximum variable size. - - @return Non-volatile maximum variable size. - -**/ -UINTN -GetNonVolatileMaxVariableSize ( - VOID - ) -{ - if (PcdGet32 (PcdHwErrStorageSize) !=3D 0) { - return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVa= riableSize)), - PcdGet32 (PcdMaxHardwareErrorVariableSize)); - } else { - return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariabl= eSize)); - } -} - /** Get maximum variable size, covering both non-volatile and volatile varia= bles. =20 @@ -4024,7 +3292,7 @@ InitNonVolatileVariableStore ( return Status; } mVariableModuleGlobal->VariableGlobal.EmuNvMode =3D TRUE; - DEBUG ((DEBUG_INFO, "Variable driver will work at emulated non-volatil= e variable mode!\n")); + DEBUG ((DEBUG_INFO, "Variable driver will work in emulated non-volatil= e variable mode!\n")); } else { Status =3D InitRealNonVolatileVariableStore (&VariableStoreBase); if (EFI_ERROR (Status)) { @@ -4040,6 +3308,9 @@ InitNonVolatileVariableStore ( mVariableModuleGlobal->MaxVariableSize =3D PcdGet32 (PcdMaxVariableSize); mVariableModuleGlobal->MaxAuthVariableSize =3D ((PcdGet32 (PcdMaxAuthVar= iableSize) !=3D 0) ? PcdGet32 (PcdMaxAuthVariableSize) : mVariableModuleGlo= bal->MaxVariableSize); =20 + Status =3D InitVariableHelpers (mVariableModuleGlobal->VariableGlobal.Au= thFormat); + ASSERT_EFI_ERROR (Status); + // // Parse non-volatile variable data and get last variable offset. // @@ -4470,18 +3741,13 @@ VariableCommonInitialize ( =20 // // mVariableModuleGlobal->VariableGlobal.AuthFormat - // has been initialized in InitNonVolatileVariableStore(). + // is initialized in InitNonVolatileVariableStore(). // if (mVariableModuleGlobal->VariableGlobal.AuthFormat) { DEBUG ((EFI_D_INFO, "Variable driver will work with auth variable form= at!\n")); - // - // Set AuthSupport to FALSE first, VariableWriteServiceInitialize() wi= ll initialize it. - // - mVariableModuleGlobal->VariableGlobal.AuthSupport =3D FALSE; VariableGuid =3D &gEfiAuthenticatedVariableGuid; } else { DEBUG ((EFI_D_INFO, "Variable driver will work without auth variable s= upport!\n")); - mVariableModuleGlobal->VariableGlobal.AuthSupport =3D FALSE; VariableGuid =3D &gEfiVariableGuid; } =20 diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c b/M= deModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c index cb6fcebe2d..232d9ffe25 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableExLib.c @@ -1,12 +1,13 @@ /** @file Provides variable driver extended services. =20 -Copyright (c) 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ =20 #include "Variable.h" +#include "VariableParsing.h" =20 /** Finds variable in storage blocks of volatile and non-volatile storage ar= eas. @@ -97,10 +98,16 @@ VariableExLibFindNextVariable ( EFI_STATUS Status; VARIABLE_HEADER *VariablePtr; AUTHENTICATED_VARIABLE_HEADER *AuthVariablePtr; + VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax]; =20 - Status =3D VariableServiceGetNextVariableInternal ( + VariableStoreHeader[VariableStoreTypeVolatile] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileVariableBase; + VariableStoreHeader[VariableStoreTypeHob] =3D (VARIABLE_STORE_HEADE= R *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVariableBase; + VariableStoreHeader[VariableStoreTypeNv] =3D mNvVariableCache; + + Status =3D GetNextVariableEx ( VariableName, VendorGuid, + VariableStoreHeader, &VariablePtr ); if (EFI_ERROR (Status)) { diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile= .c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c new file mode 100644 index 0000000000..b1b6d8282f --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableNonVolatile.c @@ -0,0 +1,28 @@ +/** @file + Common variable non-volatile store routines. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VariableNonVolatile.h" + +/** + Get non-volatile maximum variable size. + + @return Non-volatile maximum variable size. + +**/ +UINTN +GetNonVolatileMaxVariableSize ( + VOID + ) +{ + if (PcdGet32 (PcdHwErrStorageSize) !=3D 0) { + return MAX (MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVa= riableSize)), + PcdGet32 (PcdMaxHardwareErrorVariableSize)); + } else { + return MAX (PcdGet32 (PcdMaxVariableSize), PcdGet32 (PcdMaxAuthVariabl= eSize)); + } +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c b= /MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c new file mode 100644 index 0000000000..d448e5a264 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableParsing.c @@ -0,0 +1,816 @@ +/** @file + The common variable helper routines shared by the DXE_RUNTIME variable + module and the DXE_SMM variable module. + + Caution: This module requires additional review when modified. + This driver will have external input - variable data. They may be input = in SMM mode. + This external input must be validated carefully to avoid security issue = like + buffer overflow, integer overflow. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VariableParsing.h" + +typedef struct { + CONST CHAR16 *VariableName; + EFI_GUID *VendorGuid; +} VARIABLE_TYPE; + +VARIABLE_TYPE mAlwaysAuthenticatedVariables[] =3D { + {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid}, + {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid}, + {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid}, + {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid}, + {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid}, + {EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid}, +}; + +STATIC BOOLEAN mAuthFormat; + +/** + + This code checks if variable header is valid or not. + + @param Variable Pointer to the Variable Header. + @param VariableStoreEnd Pointer to the Variable Store End. + + @retval TRUE Variable header is valid. + @retval FALSE Variable header is not valid. + +**/ +BOOLEAN +IsValidVariableHeader ( + IN VARIABLE_HEADER *Variable, + IN VARIABLE_HEADER *VariableStoreEnd + ) +{ + if ((Variable =3D=3D NULL) || (Variable >=3D VariableStoreEnd) || (Varia= ble->StartId !=3D VARIABLE_DATA)) { + // + // Variable is NULL or has reached the end of variable store, + // or the StartId is not correct. + // + return FALSE; + } + + return TRUE; +} + +/** + + This code gets the current status of Variable Store. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @retval EfiRaw Variable store status is raw. + @retval EfiValid Variable store status is valid. + @retval EfiInvalid Variable store status is invalid. + +**/ +VARIABLE_STORE_STATUS +GetVariableStoreStatus ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ) +{ + if ((CompareGuid (&VarStoreHeader->Signature, &gEfiAuthenticatedVariable= Guid) || + CompareGuid (&VarStoreHeader->Signature, &gEfiVariableGuid)) && + VarStoreHeader->Format =3D=3D VARIABLE_STORE_FORMATTED && + VarStoreHeader->State =3D=3D VARIABLE_STORE_HEALTHY + ) { + + return EfiValid; + } else if (((UINT32 *)(&VarStoreHeader->Signature))[0] =3D=3D 0xffffffff= && + ((UINT32 *)(&VarStoreHeader->Signature))[1] =3D=3D 0xffffffff= && + ((UINT32 *)(&VarStoreHeader->Signature))[2] =3D=3D 0xffffffff= && + ((UINT32 *)(&VarStoreHeader->Signature))[3] =3D=3D 0xffffffff= && + VarStoreHeader->Size =3D=3D 0xffffffff && + VarStoreHeader->Format =3D=3D 0xff && + VarStoreHeader->State =3D=3D 0xff + ) { + + return EfiRaw; + } else { + return EfiInvalid; + } +} + +/** + This code gets the size of variable header. + + @return Size of variable header in bytes in type UINTN. + +**/ +UINTN +GetVariableHeaderSize ( + VOID + ) +{ + UINTN Value; + + if (mAuthFormat) { + Value =3D sizeof (AUTHENTICATED_VARIABLE_HEADER); + } else { + Value =3D sizeof (VARIABLE_HEADER); + } + + return Value; +} + +/** + + This code gets the size of name of variable. + + @param Variable Pointer to the Variable Header. + + @return UINTN Size of variable in bytes. + +**/ +UINTN +NameSizeOfVariable ( + IN VARIABLE_HEADER *Variable + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (mAuthFormat) { + if (AuthVariable->State =3D=3D (UINT8) (-1) || + AuthVariable->DataSize =3D=3D (UINT32) (-1) || + AuthVariable->NameSize =3D=3D (UINT32) (-1) || + AuthVariable->Attributes =3D=3D (UINT32) (-1)) { + return 0; + } + return (UINTN) AuthVariable->NameSize; + } else { + if (Variable->State =3D=3D (UINT8) (-1) || + Variable->DataSize =3D=3D (UINT32) (-1) || + Variable->NameSize =3D=3D (UINT32) (-1) || + Variable->Attributes =3D=3D (UINT32) (-1)) { + return 0; + } + return (UINTN) Variable->NameSize; + } +} + +/** + This code sets the size of name of variable. + + @param[in] Variable Pointer to the Variable Header. + @param[in] NameSize Name size to set. + +**/ +VOID +SetNameSizeOfVariable ( + IN VARIABLE_HEADER *Variable, + IN UINTN NameSize + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (mAuthFormat) { + AuthVariable->NameSize =3D (UINT32) NameSize; + } else { + Variable->NameSize =3D (UINT32) NameSize; + } +} + +/** + + This code gets the size of variable data. + + @param Variable Pointer to the Variable Header. + + @return Size of variable in bytes. + +**/ +UINTN +DataSizeOfVariable ( + IN VARIABLE_HEADER *Variable + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (mAuthFormat) { + if (AuthVariable->State =3D=3D (UINT8) (-1) || + AuthVariable->DataSize =3D=3D (UINT32) (-1) || + AuthVariable->NameSize =3D=3D (UINT32) (-1) || + AuthVariable->Attributes =3D=3D (UINT32) (-1)) { + return 0; + } + return (UINTN) AuthVariable->DataSize; + } else { + if (Variable->State =3D=3D (UINT8) (-1) || + Variable->DataSize =3D=3D (UINT32) (-1) || + Variable->NameSize =3D=3D (UINT32) (-1) || + Variable->Attributes =3D=3D (UINT32) (-1)) { + return 0; + } + return (UINTN) Variable->DataSize; + } +} + +/** + This code sets the size of variable data. + + @param[in] Variable Pointer to the Variable Header. + @param[in] DataSize Data size to set. + +**/ +VOID +SetDataSizeOfVariable ( + IN VARIABLE_HEADER *Variable, + IN UINTN DataSize + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (mAuthFormat) { + AuthVariable->DataSize =3D (UINT32) DataSize; + } else { + Variable->DataSize =3D (UINT32) DataSize; + } +} + +/** + + This code gets the pointer to the variable name. + + @param Variable Pointer to the Variable Header. + + @return Pointer to Variable Name which is Unicode encoding. + +**/ +CHAR16 * +GetVariableNamePtr ( + IN VARIABLE_HEADER *Variable + ) +{ + return (CHAR16 *) ((UINTN) Variable + GetVariableHeaderSize ()); +} + +/** + This code gets the pointer to the variable guid. + + @param Variable Pointer to the Variable Header. + + @return A EFI_GUID* pointer to Vendor Guid. + +**/ +EFI_GUID * +GetVendorGuidPtr ( + IN VARIABLE_HEADER *Variable + ) +{ + AUTHENTICATED_VARIABLE_HEADER *AuthVariable; + + AuthVariable =3D (AUTHENTICATED_VARIABLE_HEADER *) Variable; + if (mAuthFormat) { + return &AuthVariable->VendorGuid; + } else { + return &Variable->VendorGuid; + } +} + +/** + + This code gets the pointer to the variable data. + + @param Variable Pointer to the Variable Header. + + @return Pointer to Variable Data. + +**/ +UINT8 * +GetVariableDataPtr ( + IN VARIABLE_HEADER *Variable + ) +{ + UINTN Value; + + // + // Be careful about pad size for alignment. + // + Value =3D (UINTN) GetVariableNamePtr (Variable); + Value +=3D NameSizeOfVariable (Variable); + Value +=3D GET_PAD_SIZE (NameSizeOfVariable (Variable)); + + return (UINT8 *) Value; +} + +/** + This code gets the variable data offset related to variable header. + + @param Variable Pointer to the Variable Header. + + @return Variable Data offset. + +**/ +UINTN +GetVariableDataOffset ( + IN VARIABLE_HEADER *Variable + ) +{ + UINTN Value; + + // + // Be careful about pad size for alignment + // + Value =3D GetVariableHeaderSize (); + Value +=3D NameSizeOfVariable (Variable); + Value +=3D GET_PAD_SIZE (NameSizeOfVariable (Variable)); + + return Value; +} + +/** + + This code gets the pointer to the next variable header. + + @param Variable Pointer to the Variable Header. + + @return Pointer to next variable header. + +**/ +VARIABLE_HEADER * +GetNextVariablePtr ( + IN VARIABLE_HEADER *Variable + ) +{ + UINTN Value; + + Value =3D (UINTN) GetVariableDataPtr (Variable); + Value +=3D DataSizeOfVariable (Variable); + Value +=3D GET_PAD_SIZE (DataSizeOfVariable (Variable)); + + // + // Be careful about pad size for alignment. + // + return (VARIABLE_HEADER *) HEADER_ALIGN (Value); +} + +/** + + Gets the pointer to the first variable header in given variable store ar= ea. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @return Pointer to the first variable header. + +**/ +VARIABLE_HEADER * +GetStartPointer ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ) +{ + // + // The end of variable store. + // + return (VARIABLE_HEADER *) HEADER_ALIGN (VarStoreHeader + 1); +} + +/** + + Gets the pointer to the end of the variable storage area. + + This function gets pointer to the end of the variable storage + area, according to the input variable store header. + + @param VarStoreHeader Pointer to the Variable Store Header. + + @return Pointer to the end of the variable storage area. + +**/ +VARIABLE_HEADER * +GetEndPointer ( + IN VARIABLE_STORE_HEADER *VarStoreHeader + ) +{ + // + // The end of variable store + // + return (VARIABLE_HEADER *) HEADER_ALIGN ((UINTN) VarStoreHeader + VarSto= reHeader->Size); +} + +/** + Returns if this is a variable that always requires authenticated writes. + There may be other scenarios that result in a variable not identified by= the + function to also require authentication. + + @param[in] VariableName Name of variable. + @param[in] VendorGuid Guid of variable. + + @retval TRUE The variable always requires authenticated writ= es + @retval FALSE The variable may or may not require authenticat= ed writes +**/ +BOOLEAN +IsAuthenticatedVariable ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid + ) +{ + UINTN Index; + + for (Index =3D 0; Index < sizeof (mAlwaysAuthenticatedVariables) / sizeo= f (mAlwaysAuthenticatedVariables[0]); Index++) { + if ((StrCmp (VariableName, mAlwaysAuthenticatedVariables[Index].Variab= leName) =3D=3D 0) && + (CompareGuid (VendorGuid, mAlwaysAuthenticatedVariables[Index].Ven= dorGuid))) { + return TRUE; + } + } + return FALSE; +} + +/** + Compare two EFI_TIME data. + + + @param FirstTime A pointer to the first EFI_TIME data. + @param SecondTime A pointer to the second EFI_TIME data. + + @retval TRUE The FirstTime is not later than the SecondTim= e. + @retval FALSE The FirstTime is later than the SecondTime. + +**/ +BOOLEAN +VariableCompareTimeStampInternal ( + IN EFI_TIME *FirstTime, + IN EFI_TIME *SecondTime + ) +{ + if (FirstTime->Year !=3D SecondTime->Year) { + return (BOOLEAN) (FirstTime->Year < SecondTime->Year); + } else if (FirstTime->Month !=3D SecondTime->Month) { + return (BOOLEAN) (FirstTime->Month < SecondTime->Month); + } else if (FirstTime->Day !=3D SecondTime->Day) { + return (BOOLEAN) (FirstTime->Day < SecondTime->Day); + } else if (FirstTime->Hour !=3D SecondTime->Hour) { + return (BOOLEAN) (FirstTime->Hour < SecondTime->Hour); + } else if (FirstTime->Minute !=3D SecondTime->Minute) { + return (BOOLEAN) (FirstTime->Minute < SecondTime->Minute); + } + + return (BOOLEAN) (FirstTime->Second <=3D SecondTime->Second); +} + +/** + Find the variable in the specified variable store. + + @param[in] VariableName Name of the variable to be found + @param[in] VendorGuid Vendor GUID to be found. + @param[in] IgnoreRtCheck Ignore EFI_VARIABLE_RUNTIME_ACCESS = attribute + check at runtime when searching var= iable. + @param[in, out] PtrTrack Variable Track Pointer structure th= at contains Variable Information. + + @retval EFI_SUCCESS Variable found successfully + @retval EFI_NOT_FOUND Variable not found +**/ +EFI_STATUS +FindVariableEx ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN IgnoreRtCheck, + IN OUT VARIABLE_POINTER_TRACK *PtrTrack + ) +{ + VARIABLE_HEADER *InDeletedVariable; + VOID *Point; + + PtrTrack->InDeletedTransitionPtr =3D NULL; + + // + // Find the variable by walk through HOB, volatile and non-volatile vari= able store. + // + InDeletedVariable =3D NULL; + + for ( PtrTrack->CurrPtr =3D PtrTrack->StartPtr + ; IsValidVariableHeader (PtrTrack->CurrPtr, PtrTrack->EndPtr) + ; PtrTrack->CurrPtr =3D GetNextVariablePtr (PtrTrack->CurrPtr) + ) { + if (PtrTrack->CurrPtr->State =3D=3D VAR_ADDED || + PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION & VAR_A= DDED) + ) { + if (IgnoreRtCheck || !AtRuntime () || ((PtrTrack->CurrPtr->Attribute= s & EFI_VARIABLE_RUNTIME_ACCESS) !=3D 0)) { + if (VariableName[0] =3D=3D 0) { + if (PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION &= VAR_ADDED)) { + InDeletedVariable =3D PtrTrack->CurrPtr; + } else { + PtrTrack->InDeletedTransitionPtr =3D InDeletedVariable; + return EFI_SUCCESS; + } + } else { + if (CompareGuid (VendorGuid, GetVendorGuidPtr (PtrTrack->CurrPtr= ))) { + Point =3D (VOID *) GetVariableNamePtr (PtrTrack->CurrPtr); + + ASSERT (NameSizeOfVariable (PtrTrack->CurrPtr) !=3D 0); + if (CompareMem (VariableName, Point, NameSizeOfVariable (PtrTr= ack->CurrPtr)) =3D=3D 0) { + if (PtrTrack->CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITI= ON & VAR_ADDED)) { + InDeletedVariable =3D PtrTrack->CurrPtr; + } else { + PtrTrack->InDeletedTransitionPtr =3D InDeletedVariable; + return EFI_SUCCESS; + } + } + } + } + } + } + } + + PtrTrack->CurrPtr =3D InDeletedVariable; + return (PtrTrack->CurrPtr =3D=3D NULL) ? EFI_NOT_FOUND : EFI_SUCCESS; +} + +/** + This code finds the next available variable. + + Caution: This function may receive untrusted input. + This function may be invoked in SMM mode. This function will do basic va= lidation, before parse the data. + + @param[in] VariableName Pointer to variable name. + @param[in] VendorGuid Variable Vendor Guid. + @param[in] VariableStoreList A list of variable stores that should be u= sed to get the next variable. + The maximum number of entries is the max v= alue of VARIABLE_STORE_TYPE. + @param[out] VariablePtr Pointer to variable header address. + + @retval EFI_SUCCESS The function completed successfully. + @retval EFI_NOT_FOUND The next variable was not found. + @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, wh= ile 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. + +**/ +EFI_STATUS +EFIAPI +GetNextVariableEx ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN VARIABLE_STORE_HEADER **VariableStoreList, + OUT VARIABLE_HEADER **VariablePtr + ) +{ + EFI_STATUS Status; + VARIABLE_STORE_TYPE StoreType; + VARIABLE_POINTER_TRACK Variable; + VARIABLE_POINTER_TRACK VariableInHob; + VARIABLE_POINTER_TRACK VariablePtrTrack; + + Status =3D EFI_NOT_FOUND; + + if (VariableStoreList =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + // Check if the variable exists in the given variable store list + for (StoreType =3D (VARIABLE_STORE_TYPE) 0; StoreType < VariableStoreTyp= eMax; StoreType++) { + if (VariableStoreList[StoreType] =3D=3D NULL) { + continue; + } + + Variable.StartPtr =3D GetStartPointer (VariableStoreList[StoreType]); + Variable.EndPtr =3D GetEndPointer (VariableStoreList[StoreType]); + Variable.Volatile =3D (BOOLEAN) (StoreType =3D=3D VariableStoreTypeVol= atile); + + Status =3D FindVariableEx (VariableName, VendorGuid, FALSE, &Variable); + if (!EFI_ERROR (Status)) { + break; + } + } + + if (Variable.CurrPtr =3D=3D NULL || EFI_ERROR (Status)) { + // + // For VariableName is an empty string, FindVariable() will try to fin= d and return + // the first qualified variable, and if FindVariable() returns error (= EFI_NOT_FOUND) + // as no any variable is found, still go to return the error (EFI_NOT_= FOUND). + // + if (VariableName[0] !=3D 0) { + // + // For VariableName is not an empty string, and FindVariable() retur= ns error as + // VariableName and VendorGuid are not a name and GUID of an existin= g variable, + // there is no way to get next variable, follow spec to return EFI_I= NVALID_PARAMETER. + // + Status =3D EFI_INVALID_PARAMETER; + } + goto Done; + } + + if (VariableName[0] !=3D 0) { + // + // If variable name is not empty, get next variable. + // + Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); + } + + while (TRUE) { + // + // Switch to the next variable store if needed + // + while (!IsValidVariableHeader (Variable.CurrPtr, Variable.EndPtr)) { + // + // Find current storage index + // + for (StoreType =3D (VARIABLE_STORE_TYPE) 0; StoreType < VariableStor= eTypeMax; StoreType++) { + if ((VariableStoreList[StoreType] !=3D NULL) && (Variable.StartPtr= =3D=3D GetStartPointer (VariableStoreList[StoreType]))) { + break; + } + } + ASSERT (StoreType < VariableStoreTypeMax); + // + // Switch to next storage + // + for (StoreType++; StoreType < VariableStoreTypeMax; StoreType++) { + if (VariableStoreList[StoreType] !=3D NULL) { + break; + } + } + // + // Capture the case that + // 1. current storage is the last one, or + // 2. no further storage + // + if (StoreType =3D=3D VariableStoreTypeMax) { + Status =3D EFI_NOT_FOUND; + goto Done; + } + Variable.StartPtr =3D GetStartPointer (VariableStoreList[StoreType]); + Variable.EndPtr =3D GetEndPointer (VariableStoreList[StoreType]); + Variable.CurrPtr =3D Variable.StartPtr; + } + + // + // Variable is found + // + if (Variable.CurrPtr->State =3D=3D VAR_ADDED || Variable.CurrPtr->Stat= e =3D=3D (VAR_IN_DELETED_TRANSITION & VAR_ADDED)) { + if (!AtRuntime () || ((Variable.CurrPtr->Attributes & EFI_VARIABLE_R= UNTIME_ACCESS) !=3D 0)) { + if (Variable.CurrPtr->State =3D=3D (VAR_IN_DELETED_TRANSITION & VA= R_ADDED)) { + // + // If it is a IN_DELETED_TRANSITION variable, + // and there is also a same ADDED one at the same time, + // don't return it. + // + VariablePtrTrack.StartPtr =3D Variable.StartPtr; + VariablePtrTrack.EndPtr =3D Variable.EndPtr; + Status =3D FindVariableEx ( + GetVariableNamePtr (Variable.CurrPtr), + GetVendorGuidPtr (Variable.CurrPtr), + FALSE, + &VariablePtrTrack + ); + if (!EFI_ERROR (Status) && VariablePtrTrack.CurrPtr->State =3D= =3D VAR_ADDED) { + Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); + continue; + } + } + + // + // Don't return NV variable when HOB overrides it + // + if ((VariableStoreList[VariableStoreTypeHob] !=3D NULL) && (Variab= leStoreList[VariableStoreTypeNv] !=3D NULL) && + (Variable.StartPtr =3D=3D GetStartPointer (VariableStoreList[V= ariableStoreTypeNv])) + ) { + VariableInHob.StartPtr =3D GetStartPointer (VariableStoreList[Va= riableStoreTypeHob]); + VariableInHob.EndPtr =3D GetEndPointer (VariableStoreList[Va= riableStoreTypeHob]); + Status =3D FindVariableEx ( + GetVariableNamePtr (Variable.CurrPtr), + GetVendorGuidPtr (Variable.CurrPtr), + FALSE, + &VariableInHob + ); + if (!EFI_ERROR (Status)) { + Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); + continue; + } + } + + *VariablePtr =3D Variable.CurrPtr; + Status =3D EFI_SUCCESS; + goto Done; + } + } + + Variable.CurrPtr =3D GetNextVariablePtr (Variable.CurrPtr); + } + +Done: + return Status; +} + +/** + Routine used to track statistical information about variable usage. + The data is stored in the EFI system table so it can be accessed later. + VariableInfo.efi can dump out the table. Only Boot Services variable + accesses are tracked by this code. The PcdVariableCollectStatistics + build flag controls if this feature is enabled. + + A read that hits in the cache will have Read and Cache true for + the transaction. Data is allocated by this routine, but never + freed. + + @param[in] VariableName Name of the Variable to track. + @param[in] VendorGuid Guid of the Variable to track. + @param[in] Volatile TRUE if volatile FALSE if non-volatile. + @param[in] Read TRUE if GetVariable() was called. + @param[in] Write TRUE if SetVariable() was called. + @param[in] Delete TRUE if deleted via SetVariable(). + @param[in] Cache TRUE for a cache hit. + @param[in,out] VariableInfo Pointer to a pointer of VARIABLE_INFO_ENT= RY structures. + +**/ +VOID +UpdateVariableInfo ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + IN BOOLEAN Volatile, + IN BOOLEAN Read, + IN BOOLEAN Write, + IN BOOLEAN Delete, + IN BOOLEAN Cache, + IN OUT VARIABLE_INFO_ENTRY **VariableInfo + ) +{ + VARIABLE_INFO_ENTRY *Entry; + + if (FeaturePcdGet (PcdVariableCollectStatistics)) { + if (VariableName =3D=3D NULL || VendorGuid =3D=3D NULL || VariableInfo= =3D=3D NULL) { + return; + } + if (AtRuntime ()) { + // Don't collect statistics at runtime. + return; + } + + if (*VariableInfo =3D=3D NULL) { + // + // On the first call allocate a entry and place a pointer to it in + // the EFI System Table. + // + *VariableInfo =3D AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY)); + ASSERT (*VariableInfo !=3D NULL); + + CopyGuid (&(*VariableInfo)->VendorGuid, VendorGuid); + (*VariableInfo)->Name =3D AllocateZeroPool (StrSize (VariableName)); + ASSERT ((*VariableInfo)->Name !=3D NULL); + StrCpyS ((*VariableInfo)->Name, StrSize(VariableName)/sizeof(CHAR16)= , VariableName); + (*VariableInfo)->Volatile =3D Volatile; + } + + + for (Entry =3D (*VariableInfo); Entry !=3D NULL; Entry =3D Entry->Next= ) { + if (CompareGuid (VendorGuid, &Entry->VendorGuid)) { + if (StrCmp (VariableName, Entry->Name) =3D=3D 0) { + if (Read) { + Entry->ReadCount++; + } + if (Write) { + Entry->WriteCount++; + } + if (Delete) { + Entry->DeleteCount++; + } + if (Cache) { + Entry->CacheCount++; + } + + return; + } + } + + if (Entry->Next =3D=3D NULL) { + // + // If the entry is not in the table add it. + // Next iteration of the loop will fill in the data. + // + Entry->Next =3D AllocateZeroPool (sizeof (VARIABLE_INFO_ENTRY)); + ASSERT (Entry->Next !=3D NULL); + + CopyGuid (&Entry->Next->VendorGuid, VendorGuid); + Entry->Next->Name =3D AllocateZeroPool (StrSize (VariableName)); + ASSERT (Entry->Next->Name !=3D NULL); + StrCpyS (Entry->Next->Name, StrSize(VariableName)/sizeof(CHAR16), = VariableName); + Entry->Next->Volatile =3D Volatile; + } + } + } +} + +/** + Initializes context needed for variable helpers. + + @param[in] AuthFormat If true then indicates authenticate= d variables are supported + + @retval EFI_SUCCESS Initialized successfully + @retval Others An error occurred during initializa= tion +**/ +EFI_STATUS +EFIAPI +InitVariableHelpers ( + IN BOOLEAN AuthFormat + ) +{ + mAuthFormat =3D AuthFormat; + + return EFI_SUCCESS; +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/Mde= ModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index ec463d063e..bda531d104 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -30,6 +30,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent =20 #include #include "Variable.h" +#include "VariableNonVolatile.h" +#include "VariableParsing.h" =20 BOOLEAN mAtRuntime = =3D FALSE; UINT8 *mVariableBufferPaylo= ad =3D NULL; diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 0a1888e5ef..5bf90039d6 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -13,7 +13,7 @@ =20 InitCommunicateBuffer() is really function to check the variable data si= ze. =20 -Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -39,6 +39,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include =20 #include "PrivilegePolymorphic.h" +#include "VariableParsing.h" =20 EFI_HANDLE mHandle =3D NULL; EFI_SMM_VARIABLE_PROTOCOL *mSmmVariable =3D NULL; --=20 2.16.2.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 (#48071): https://edk2.groups.io/g/devel/message/48071 Mute This Topic: https://groups.io/mt/34295309/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 Mar 28 08:01:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+48072+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48072+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1569473478; cv=none; d=zoho.com; s=zohoarc; b=VankwwvZbxNILt36s2G4tnPSI1ymi+0XXlJ4HQjYGqGfMYob3IVJMENrfoaaasg3GQSFT2cliMJ18f0qNQ9oWZnPbLR2x5w9ES6rmcWX0SIARN/ywsCNTKcgFldWFxofTQcRvpOpIdn9/5pHRkK/ru0BBPYM4Dj+91UQ4N2y6RQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569473478; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=lK8C2wNg3QauZzN1h9LgqgKVCLQvz4plXiNJx3Ttb9o=; b=MGC0DmJJ7fas2FCaDcyovcgShKaiKQz1pmJUaQ54ga+c8F6Qm13p7zoK5VsqvhyRCqzZqqp8y0AldxSe6vuVxWr8hGp8/tZoQuDxJzTUtocv3SYqPyEwd7dpJOxI2bRLWMvipy3Qrzefkt1XwHi1smIqCt6M7pFd+vgRL0EzIe0= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48072+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1569473478643210.27575943064642; Wed, 25 Sep 2019 21:51:18 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 4nIXYY1788612xtU2bINkyfW; Wed, 25 Sep 2019 21:51:18 -0700 X-Received: from mga02.intel.com (mga02.intel.com []) by groups.io with SMTP; Wed, 25 Sep 2019 21:51:18 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 21:51:17 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,550,1559545200"; d="scan'208";a="191570753" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2019 21:51:17 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V1 2/5] MdeModulePkg VariableInfo: Always consider RT DXE and SMM stats Date: Wed, 25 Sep 2019 21:50:43 -0700 Message-Id: <20190926045046.34592-3-michael.a.kubacki@intel.com> In-Reply-To: <20190926045046.34592-1-michael.a.kubacki@intel.com> References: <20190926045046.34592-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: 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,michael.a.kubacki@intel.com X-Gm-Message-State: iccIgIEeceajAnTPLMrfmGZUx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1569473478; bh=KFsXp5VtppfZyyScH2WwR63uG1QnBg2rlsg7qTHlEMQ=; h=Cc:Date:From:Reply-To:Subject:To; b=ehxzTSDhkAWy0ecO94cjLpPUOhO5fBVrowwqRJL/LN2VtDEv4bizLOg9k8pUYUn8IvN RZBDw3wFaPfOGJ2thXd+BlbLvU1RzALFedoCq9C6omZ6rypXOVAQW1ilTKKBswklXZUZW 7W0TupI7/AG3RzpQv+wVqSO5SdMmrrIfePA= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2220 The current VariableInfo application only checks for variable statistics from SMM if the variable information entries are not present in the UEFI System Configuration table as published by the DXE UEFI variable driver (VariableRuntimeDxe). This change first checks for variable information entries in the UEFI System Configuration but always checks for entries in SMM as well. If the SMM variable driver is not present, an instance of EFI_SMM_VARIABLE_PROTOCOL will not be found and the search for SMM variable statistics will be aborted (an SW SMI to get variable statistics will not be triggered). In the case variable statistics are provided by both a Runtime DXE driver (e.g. VariableSmmRuntimeDxe) and a SMM driver (VariableSmm), this change will clearly identify statistics from each respective driver. Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/Application/VariableInfo/VariableInfo.c | 37 ++++++++++------= ---- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/MdeModulePkg/Application/VariableInfo/VariableInfo.c b/MdeModu= lePkg/Application/VariableInfo/VariableInfo.c index f213471e9a..c04ba18213 100644 --- a/MdeModulePkg/Application/VariableInfo/VariableInfo.c +++ b/MdeModulePkg/Application/VariableInfo/VariableInfo.c @@ -3,7 +3,7 @@ this utility will print out the statistics information. You can use cons= ole redirection to capture the data. =20 - Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.
+ Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -126,7 +126,7 @@ PrintInfoFromSmm ( ASSERT (CommBuffer !=3D NULL); ZeroMem (CommBuffer, RealCommSize); =20 - Print (L"Non-Volatile SMM Variables:\n"); + Print (L"SMM Driver Non-Volatile Variables:\n"); do { CommSize =3D RealCommSize; Status =3D GetVariableStatisticsData (CommBuffer, &CommSize); @@ -155,7 +155,7 @@ PrintInfoFromSmm ( } } while (TRUE); =20 - Print (L"Volatile SMM Variables:\n"); + Print (L"SMM Driver Volatile Variables:\n"); ZeroMem (CommBuffer, RealCommSize); do { CommSize =3D RealCommSize; @@ -207,24 +207,18 @@ UefiMain ( IN EFI_SYSTEM_TABLE *SystemTable ) { - EFI_STATUS Status; + EFI_STATUS RuntimeDxeStatus; + EFI_STATUS SmmStatus; VARIABLE_INFO_ENTRY *VariableInfo; VARIABLE_INFO_ENTRY *Entry; =20 - Status =3D EfiGetSystemConfigurationTable (&gEfiVariableGuid, (VOID **)&= Entry); - if (EFI_ERROR (Status) || (Entry =3D=3D NULL)) { - Status =3D EfiGetSystemConfigurationTable (&gEfiAuthenticatedVariableG= uid, (VOID **)&Entry); + RuntimeDxeStatus =3D EfiGetSystemConfigurationTable (&gEfiVariableGuid, = (VOID **) &Entry); + if (EFI_ERROR (RuntimeDxeStatus) || (Entry =3D=3D NULL)) { + RuntimeDxeStatus =3D EfiGetSystemConfigurationTable (&gEfiAuthenticate= dVariableGuid, (VOID **) &Entry); } =20 - if (EFI_ERROR (Status) || (Entry =3D=3D NULL)) { - Status =3D PrintInfoFromSmm (); - if (!EFI_ERROR (Status)) { - return Status; - } - } - - if (!EFI_ERROR (Status) && (Entry !=3D NULL)) { - Print (L"Non-Volatile EFI Variables:\n"); + if (!EFI_ERROR (RuntimeDxeStatus) && (Entry !=3D NULL)) { + Print (L"Runtime DXE Driver Non-Volatile EFI Variables:\n"); VariableInfo =3D Entry; do { if (!VariableInfo->Volatile) { @@ -242,7 +236,7 @@ UefiMain ( VariableInfo =3D VariableInfo->Next; } while (VariableInfo !=3D NULL); =20 - Print (L"Volatile EFI Variables:\n"); + Print (L"Runtime DXE Driver Volatile EFI Variables:\n"); VariableInfo =3D Entry; do { if (VariableInfo->Volatile) { @@ -258,14 +252,19 @@ UefiMain ( } VariableInfo =3D VariableInfo->Next; } while (VariableInfo !=3D NULL); + } =20 - } else { + SmmStatus =3D PrintInfoFromSmm (); + + if (EFI_ERROR (RuntimeDxeStatus) && EFI_ERROR (SmmStatus)) { Print (L"Warning: Variable Dxe/Smm driver doesn't enable the feature o= f statistical information!\n"); Print (L"If you want to see this info, please:\n"); Print (L" 1. Set PcdVariableCollectStatistics as TRUE\n"); Print (L" 2. Rebuild Variable Dxe/Smm driver\n"); Print (L" 3. Run \"VariableInfo\" cmd again\n"); + + return EFI_NOT_FOUND; } =20 - return Status; + return EFI_SUCCESS; } --=20 2.16.2.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 (#48072): https://edk2.groups.io/g/devel/message/48072 Mute This Topic: https://groups.io/mt/34295310/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 Mar 28 08:01:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+48073+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48073+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1569473480; cv=none; d=zoho.com; s=zohoarc; b=KaWJGZ5Oyt4wQpqhfNAN1I9v1CxfQBZDbVuD7Yd0W8IJ9s1ucJUM7iHEydzp8/EjSoUMka0hO1hwufJdn82LVnSNkQjd4DRucdxiZy391CwBxaFxH9CstbbT5JvEOjCTQeijvzzxstor2GJ/aBvKyHVGfhtaAtM4jlQHnF6Ovcs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569473480; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=LFeVZvIJgW9jaLIsC7xT0CbwvRHh/jKlWDgbZxqu8jg=; b=mYOL5EY7GwMlzd6hingJTWwxcJN2jklZ2MjoiJRAAGAhH9Xk5++HCeukApTjIPHS0JXKHhVcrZPZw6xM+K+sb0OfbA3c3ynmfy8DB7Fkvqld06GO+HH9K4+bg3hHqOkbgJuaQRHYBfchtCsFrcUnsgKzV9ULas4160fk9VnrhmI= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48073+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1569473480950528.0914859732676; Wed, 25 Sep 2019 21:51:20 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id GzRlYY1788612xOcyaqBJiQR; Wed, 25 Sep 2019 21:51:20 -0700 X-Received: from mga02.intel.com (mga02.intel.com []) by groups.io with SMTP; Wed, 25 Sep 2019 21:51:20 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 21:51:19 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,550,1559545200"; d="scan'208";a="191570766" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2019 21:51:19 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V1 3/5] MdeModulePkg/Variable: Add RT GetVariable() cache support Date: Wed, 25 Sep 2019 21:50:44 -0700 Message-Id: <20190926045046.34592-4-michael.a.kubacki@intel.com> In-Reply-To: <20190926045046.34592-1-michael.a.kubacki@intel.com> References: <20190926045046.34592-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: 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,michael.a.kubacki@intel.com X-Gm-Message-State: VoVxvRmZRPBk2WAL8iB7085rx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1569473480; bh=QZ7EAp2k8a/ctGh1RBU+cBx8J+msVssIupRRPAEmNPM=; h=Cc:Date:From:Reply-To:Subject:To; b=Mr5uMZqpG+1TP+kC0pUGtdoash3UPD6kRj8OL3T49oZq4lsq5XGmrZAdK3Koi05TMrR 9NWkdqoUBrvbPR/ism7A6lNsIIDORmxU6wA6QhSh+KBnMGP2RxafZ0ElJ8xC0RfoPz26y MeFhWHvZDE/ePPXoScivJXDzN1a+0myWh4A= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2220 This change reduces SMIs for GetVariable () by maintaining a UEFI variable cache in Runtime DXE in addition to the pre- existing cache in SMRAM. When the Runtime Service GetVariable() is invoked, a Runtime DXE cache is used instead of triggering an SMI to VariableSmm. This can improve overall system performance by servicing variable read requests without rendezvousing all cores into SMM. The following are important points regarding this change. 1. All of the non-volatile storage contents are loaded into the cache upon driver load. This one time load operation from storage is preferred as opposed to building the cache on demand. An on- demand cache would require a fallback SMI to load data into the cache as variables are requested. 2. SetVariable () requests will continue to always trigger an SMI. This occurs regardless of whether the variable is volatile or non-volatile. 3. Both volatile and non-volatile variables are cached in a runtime buffer. As is the case in the current EDK II variable driver, they continue to be cached in separate buffers. 4. The cache in Runtime DXE and SMM are intended to be exact copies of one another. All SMM variable accesses only return data from the SMM cache. The runtime caches are only updated after the variable I/O operation is successful in SMM. The runtime caches are only updated from SMM. 5. Synchronization mechanisms are in place to ensure the runtime cache content integrity with the SMM cache. These may result in updates to runtime cache that are the same in content but different in offset and size from updates to the SMM cache. When using SMM variables, two caches will now be present. 1. "Runtime Cache" - Maintained in VariableSmmRuntimeDxe. Used to service Runtime Services GetVariable () and GetNextVariableName () callers. 2. "SMM Cache" - Maintained in VariableSmm to service SMM GetVariable () and GetNextVariableName () callers. a. This cache is retained so SMM modules do not operate on data outside SMRAM. It is possible to view UEFI variable read and write statistics by setting the gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics FeaturePcd to TRUE and using the VariableInfo UEFI application in MdeModulePkg to dump variable statistics to the console. By doing so, a user can view the number of GetVariable () hits from the Runtime DXE variable driver (Runtime Cache hits) and the SMM variable driver (SMM Cache hits). SMM Cache hits for GetVariable () will occur when SMM modules invoke GetVariable (). Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf | 2= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf | 2= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf | 24= + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf | 2= + MdeModulePkg/Include/Guid/SmmVariableCommon.h | 29= +- MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h | 39= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h | 47= ++ MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c | 44= +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c | 153= +++++ MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 114= +++- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 605= +++++++++++++++++--- 11 files changed, 958 insertions(+), 103 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.= inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf index 08a5490787..ceea5d1ff9 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf @@ -40,6 +40,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h PrivilegePolymorphic.h Measurement.c TcgMorLockDxe.c diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf b/M= deModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf index 6dc2721b81..bc3033588d 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.inf @@ -49,6 +49,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h VarCheck.c Variable.h PrivilegePolymorphic.h diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.i= nf index 1873b4fe43..8c075c9132 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf @@ -42,6 +42,8 @@ Variable.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h =20 [Packages] MdePkg/MdePkg.dec @@ -52,6 +54,7 @@ BaseLib UefiBootServicesTableLib DebugLib + TimerLib UefiRuntimeLib DxeServicesTableLib UefiDriverEntryPoint @@ -68,10 +71,29 @@ gEdkiiVariableLockProtocolGuid ## PRODUCES gEdkiiVarCheckProtocolGuid ## PRODUCES =20 +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdHwErrStorageSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxUserNvVariableSpaceSize #= # CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdBoottimeReservedNvVariableSpaceSize #= # CONSUMES + [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdVariableCollectStatistics #= # CONSUMES =20 [Guids] + ## PRODUCES ## GUID # Signature of Variable store header + ## CONSUMES ## GUID # Signature of Variable store header + ## SOMETIMES_PRODUCES ## SystemTable + gEfiAuthenticatedVariableGuid + + ## PRODUCES ## GUID # Signature of Variable store header + ## CONSUMES ## GUID # Signature of Variable store header + ## SOMETIMES_PRODUCES ## SystemTable + gEfiVariableGuid + gEfiEventVirtualAddressChangeGuid ## CONSUMES ## Event gEfiEventExitBootServicesGuid ## CONSUMES ## Event ## CONSUMES ## GUID # Locate protocol @@ -88,6 +110,8 @@ ## SOMETIMES_CONSUMES ## Variable:L"dbt" gEfiImageSecurityDatabaseGuid =20 + gEdkiiPiSmmCommunicationRegionTableGuid ## SOMETIMES_CONSUMES ## S= ystemTable + [Depex] gEfiSmmCommunicationProtocolGuid =20 diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneM= m.inf b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf index ca9d23ce9f..95c5310c0b 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableStandaloneMm.inf @@ -49,6 +49,8 @@ VariableNonVolatile.h VariableParsing.c VariableParsing.h + VariableRuntimeCache.c + VariableRuntimeCache.h VarCheck.c Variable.h PrivilegePolymorphic.h diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h b/MdeModulePkg/I= nclude/Guid/SmmVariableCommon.h index c527a59891..ceef44dfd2 100644 --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h @@ -1,7 +1,7 @@ /** @file The file defined some common structures used for communicating between S= MM variable module and SMM variable wrapper module. =20 -Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2019, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent =20 **/ @@ -9,6 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #ifndef _SMM_VARIABLE_COMMON_H_ #define _SMM_VARIABLE_COMMON_H_ =20 +#include #include =20 #define EFI_SMM_VARIABLE_WRITE_GUID \ @@ -66,6 +67,16 @@ typedef struct { #define SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET 10 =20 #define SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE 11 +// +// The payload for this function is SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIA= BLE_CACHE_CONTEXT +// +#define SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT 12 + +#define SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE 13 +// +// The payload for this function is SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_C= ACHE_INFO +// +#define SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO 14 =20 /// /// Size of SMM communicate header, without including the payload. @@ -120,4 +131,20 @@ typedef struct { UINTN VariablePayloadSize; } SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE; =20 +typedef struct { + BOOLEAN *ReadLock; + BOOLEAN *PendingUpdate; + BOOLEAN *HobFlushComplete; + VARIABLE_STORE_HEADER *RuntimeHobCache; + VARIABLE_STORE_HEADER *RuntimeNvCache; + VARIABLE_STORE_HEADER *RuntimeVolatileCache; +} SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT; + +typedef struct { + UINTN TotalHobStorageSize; + UINTN TotalNvStorageSize; + UINTN TotalVolatileStorageSize; + BOOLEAN AuthenticatedVariableUsage; +} SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO; + #endif // _SMM_VARIABLE_COMMON_H_ diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.h index fb574b2e32..b9723c0250 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h @@ -57,6 +57,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent /// #define ISO_639_2_ENTRY_SIZE 3 =20 +/// +/// The timeout to in 10us units to wait for the +/// variable runtime cache read lock to be acquired. +/// +#define VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT 200000 + typedef enum { VariableStoreTypeVolatile, VariableStoreTypeHob, @@ -64,6 +70,21 @@ typedef enum { VariableStoreTypeMax } VARIABLE_STORE_TYPE; =20 +typedef struct { + UINT32 PendingUpdateOffset; + UINT32 PendingUpdateLength; + VARIABLE_STORE_HEADER *Store; +} VARIABLE_RUNTIME_CACHE; + +typedef struct { + BOOLEAN *ReadLock; + BOOLEAN *PendingUpdate; + BOOLEAN *HobFlushComplete; + VARIABLE_RUNTIME_CACHE VariableRuntimeHobCache; + VARIABLE_RUNTIME_CACHE VariableRuntimeNvCache; + VARIABLE_RUNTIME_CACHE VariableRuntimeVolatileCache; +} VARIABLE_RUNTIME_CACHE_CONTEXT; + typedef struct { VARIABLE_HEADER *CurrPtr; // @@ -79,14 +100,16 @@ typedef struct { } VARIABLE_POINTER_TRACK; =20 typedef struct { - EFI_PHYSICAL_ADDRESS HobVariableBase; - EFI_PHYSICAL_ADDRESS VolatileVariableBase; - EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; - EFI_LOCK VariableServicesLock; - UINT32 ReentrantState; - BOOLEAN AuthFormat; - BOOLEAN AuthSupport; - BOOLEAN EmuNvMode; + EFI_PHYSICAL_ADDRESS HobVariableBase; + EFI_PHYSICAL_ADDRESS HobVariableBackupBase; + EFI_PHYSICAL_ADDRESS VolatileVariableBase; + EFI_PHYSICAL_ADDRESS NonVolatileVariableBase; + VARIABLE_RUNTIME_CACHE_CONTEXT VariableRuntimeCacheContext; + EFI_LOCK VariableServicesLock; + UINT32 ReentrantState; + BOOLEAN AuthFormat; + BOOLEAN AuthSupport; + BOOLEAN EmuNvMode; } VARIABLE_GLOBAL; =20 typedef struct { diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCach= e.h b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h new file mode 100644 index 0000000000..09b83eb215 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.h @@ -0,0 +1,47 @@ +/** @file + The common variable volatile store routines shared by the DXE_RUNTIME va= riable + module and the DXE_SMM variable module. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef _VARIABLE_RUNTIME_CACHE_H_ +#define _VARIABLE_RUNTIME_CACHE_H_ + +#include "Variable.h" + +/** + Copies any pending updates to runtime variable caches. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCacheEx ( + VOID + ); + +/** + Synchronizes the runtime variable caches with all pending updates outsid= e runtime. + + Ensures all conditions are met to maintain coherency for runtime cache u= pdates. + + @param[in] VariableRuntimeCache Variable runtime cache structure for the= runtime cache being synchronized. + @param[in] Offset Offset in bytes to apply the update. + @param[in] Length Length of data in bytes of the update. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCache ( + IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache, + IN UINTN Offset, + IN UINTN Length + ); + +#endif diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c b/MdeMod= ulePkg/Universal/Variable/RuntimeDxe/Variable.c index d14fecc830..ca2118d69f 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c @@ -25,6 +25,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Variable.h" #include "VariableNonVolatile.h" #include "VariableParsing.h" +#include "VariableRuntimeCache.h" =20 VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; =20 @@ -332,6 +333,12 @@ RecordVarErrorFlag ( // Update the data in NV cache. // *VarErrFlag =3D TempFlag; + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCa= cheContext.VariableRuntimeNvCache, + (UINTN) VarErrFlag - (UINTN) mNvVariableCache + (UINTN) = mVariableModuleGlobal->VariableGlobal.NonVolatileVariableBase, + sizeof (TempFlag) + ); + ASSERT_EFI_ERROR (Status); } } } @@ -755,12 +762,24 @@ Reclaim ( =20 Done: if (IsVolatile || mVariableModuleGlobal->VariableGlobal.EmuNvMode) { + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCach= eContext.VariableRuntimeVolatileCache, + 0, + VariableStoreHeader->Size + ); + ASSERT_EFI_ERROR (Status); FreePool (ValidBuffer); } else { // // For NV variable reclaim, we use mNvVariableCache as the buffer, so = copy the data back. // - CopyMem (mNvVariableCache, (UINT8 *)(UINTN)VariableBase, VariableStore= Header->Size); + CopyMem (mNvVariableCache, (UINT8 *) (UINTN) VariableBase, VariableSto= reHeader->Size); + Status =3D SynchronizeRuntimeVariableCache ( + &(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCac= heContext.VariableRuntimeNvCache), + 0, + VariableStoreHeader->Size + ); + ASSERT_EFI_ERROR (Status); } =20 return Status; @@ -1592,6 +1611,7 @@ UpdateVariable ( VARIABLE_POINTER_TRACK *Variable; VARIABLE_POINTER_TRACK NvVariable; VARIABLE_STORE_HEADER *VariableStoreHeader; + VARIABLE_RUNTIME_CACHE *VolatileCacheInstance; UINT8 *BufferForMerge; UINTN MergedBufSize; BOOLEAN DataReady; @@ -2235,6 +2255,21 @@ UpdateVariable ( } =20 Done: + if (!EFI_ERROR (Status)) { + if (Variable->Volatile) { + VolatileCacheInstance =3D &(mVariableModuleGlobal->VariableGlobal.Va= riableRuntimeCacheContext.VariableRuntimeVolatileCache); + } else { + VolatileCacheInstance =3D &(mVariableModuleGlobal->VariableGlobal.Va= riableRuntimeCacheContext.VariableRuntimeNvCache); + } + + Status =3D SynchronizeRuntimeVariableCache ( + VolatileCacheInstance, + 0, + VolatileCacheInstance->Store->Size + ); + ASSERT_EFI_ERROR (Status); + } + return Status; } =20 @@ -3409,6 +3444,12 @@ FlushHobVariableToFlash ( ErrorFlag =3D TRUE; } } + Status =3D SynchronizeRuntimeVariableCache ( + &mVariableModuleGlobal->VariableGlobal.VariableRuntimeCach= eContext.VariableRuntimeHobCache, + 0, + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCache= Context.VariableRuntimeHobCache.Store->Size + ); + ASSERT_EFI_ERROR (Status); if (ErrorFlag) { // // We still have HOB variable(s) not flushed in flash. @@ -3419,6 +3460,7 @@ FlushHobVariableToFlash ( // All HOB variables have been flushed in flash. // DEBUG ((EFI_D_INFO, "Variable driver: all HOB variables have been fl= ushed in flash.\n")); + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= HobFlushComplete) =3D TRUE; if (!AtRuntime ()) { FreePool ((VOID *) VariableStoreHeader); } diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCach= e.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c new file mode 100644 index 0000000000..2642d9b000 --- /dev/null +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeCache.c @@ -0,0 +1,153 @@ +/** @file + The common variable volatile store routines shared by the DXE_RUNTIME va= riable + module and the DXE_SMM variable module. + + Caution: This module requires additional review when modified. + This driver will have external input - variable data. They may be input = in SMM mode. + This external input must be validated carefully to avoid security issue = like + buffer overflow, integer overflow. + +Copyright (c) 2019, Intel Corporation. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include "VariableParsing.h" +#include "VariableRuntimeCache.h" + +extern VARIABLE_MODULE_GLOBAL *mVariableModuleGlobal; +extern VARIABLE_STORE_HEADER *mNvVariableCache; + +/** + Copies any pending updates to runtime variable caches. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCacheEx ( + VOID + ) +{ + if ( + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeNvCache.Store =3D=3D NULL || + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeVolatileCache.Store =3D=3D NULL || + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pend= ingUpdate =3D=3D NULL + ) { + return EFI_UNSUPPORTED; + } + + if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= PendingUpdate)) { + if ( + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Va= riableRuntimeHobCache.Store !=3D NULL && + mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0 + ) { + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.Variabl= eRuntimeCacheContext.VariableRuntimeHobCache.Store) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContex= t.VariableRuntimeHobCache.PendingUpdateOffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.HobVari= ableBase) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContex= t.VariableRuntimeHobCache.PendingUpdateOffset + ), + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeHobCache.PendingUpdateLength + ); + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Va= riableRuntimeHobCache.PendingUpdateLength =3D 0; + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Va= riableRuntimeHobCache.PendingUpdateOffset =3D 0; + } + + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VariableR= untimeCacheContext.VariableRuntimeNvCache.Store) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeNvCache.PendingUpdateOffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mNvVariableCache) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeNvCache.PendingUpdateOffset + ), + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Va= riableRuntimeNvCache.PendingUpdateLength + ); + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeNvCache.PendingUpdateLength =3D 0; + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeNvCache.PendingUpdateOffset =3D 0; + + CopyMem ( + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VariableR= untimeCacheContext.VariableRuntimeVolatileCache.Store) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeVolatileCache.PendingUpdateOffset + ), + (VOID *) ( + ((UINT8 *) (UINTN) mVariableModuleGlobal->VariableGlobal.VolatileV= ariableBase) + + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= VariableRuntimeVolatileCache.PendingUpdateOffset + ), + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Va= riableRuntimeVolatileCache.PendingUpdateLength + ); + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeVolatileCache.PendingUpdateLength =3D 0; + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Vari= ableRuntimeVolatileCache.PendingUpdateOffset =3D 0; + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pe= ndingUpdate) =3D FALSE; + } + + return EFI_SUCCESS; +} + +/** + Synchronizes the runtime variable caches with all pending updates outsid= e runtime. + + Ensures all conditions are met to maintain coherency for runtime cache u= pdates. + + @param[in] VariableRuntimeCache Variable runtime cache structure for the= runtime cache being synchronized. + @param[in] Offset Offset in bytes to apply the update. + @param[in] Length Length of data in bytes of the update. + + @retval EFI_UNSUPPORTED The volatile store to be updated is not = initialized properly. + @retval EFI_SUCCESS The volatile store was updated successfu= lly. + +**/ +EFI_STATUS +SynchronizeRuntimeVariableCache ( + IN VARIABLE_RUNTIME_CACHE *VariableRuntimeCache, + IN UINTN Offset, + IN UINTN Length + ) +{ + if (VariableRuntimeCache =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } else if (VariableRuntimeCache->Store =3D=3D NULL) { + // Runtime cache is not available yet at this point, + // Return EFI_SUCCESS instead of EFI_NOT_AVAILABLE_YET to let it pro= gress + return EFI_SUCCESS; + } + + if ( + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pend= ingUpdate =3D=3D NULL || + mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Read= Lock =3D=3D NULL + ) { + return EFI_UNSUPPORTED; + } + + if ( + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pe= ndingUpdate) && + VariableRuntimeCache->PendingUpdateLength > 0 + ) { + VariableRuntimeCache->PendingUpdateLength =3D + (UINT32) ( + MAX ( + (UINTN) (VariableRuntimeCache->PendingUpdateOffset + VariableRun= timeCache->PendingUpdateLength), + Offset + Length + ) - MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Offset) + ); + VariableRuntimeCache->PendingUpdateOffset =3D + (UINT32) MIN ((UINTN) VariableRuntimeCache->PendingUpdateOffset, Off= set); + } else { + VariableRuntimeCache->PendingUpdateLength =3D (UINT32) Length; + VariableRuntimeCache->PendingUpdateOffset =3D (UINT32) Offset; + } + *(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.Pend= ingUpdate) =3D TRUE; + + if (*(mVariableModuleGlobal->VariableGlobal.VariableRuntimeCacheContext.= ReadLock) =3D=3D FALSE) { + return SynchronizeRuntimeVariableCacheEx (); + } + + return EFI_SUCCESS; +} diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/Mde= ModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index bda531d104..1cb6092582 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -32,6 +32,9 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "Variable.h" #include "VariableNonVolatile.h" #include "VariableParsing.h" +#include "VariableRuntimeCache.h" + +extern VARIABLE_STORE_HEADER *mNvVariableCache; =20 BOOLEAN mAtRuntime = =3D FALSE; UINT8 *mVariableBufferPaylo= ad =3D NULL; @@ -452,25 +455,29 @@ SmmVariableGetStatistics ( EFI_STATUS EFIAPI SmmVariableHandler ( - IN EFI_HANDLE DispatchHandle, - IN CONST VOID *RegisterContext, - IN OUT VOID *CommBuffer, - IN OUT UINTN *CommBufferSize + IN EFI_HANDLE DispatchHandle, + IN CONST VOID *RegisterContext, + IN OUT VOID *CommBuffer, + IN OUT UINTN *CommBufferSize ) { - EFI_STATUS Status; - SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHea= der; - SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader; - SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName; - SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo; - SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; - VARIABLE_INFO_ENTRY *VariableInfo; - SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock; - SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariableProper= ty; - UINTN InfoSize; - UINTN NameBufferSize; - UINTN CommBufferPayloadSize; - UINTN TempCommBufferSize; + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunc= tionHeader; + SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHead= er; + SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariable= Name; + SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableIn= fo; + SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; + SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *RuntimeVariable= CacheContext; + SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *GetRuntimeCache= Info; + SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE *VariableToLock; + SMM_VARIABLE_COMMUNICATE_VAR_CHECK_VARIABLE_PROPERTY *CommVariablePro= perty; + VARIABLE_INFO_ENTRY *VariableInfo; + VARIABLE_RUNTIME_CACHE_CONTEXT *VariableCacheCo= ntext; + VARIABLE_STORE_HEADER *VariableCache; + UINTN InfoSize; + UINTN NameBufferSize; + UINTN CommBufferPayloa= dSize; + UINTN TempCommBufferSi= ze; =20 // // If input is invalid, stop processing this SMI @@ -790,6 +797,79 @@ SmmVariableHandler ( ); CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, Co= mmBufferPayloadSize); break; + case SMM_VARIABLE_FUNCTION_INIT_RUNTIME_VARIABLE_CACHE_CONTEXT: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_FUNCTION_INIT_RUNTI= ME_VARIABLE_CACHE_CONTEXT)) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: SMM communi= cation buffer size invalid!\n")); + } else if (mEndOfDxe) { + DEBUG ((DEBUG_ERROR, "InitRuntimeVariableCacheContext: Cannot init= context after end of DXE!\n")); + } else { + RuntimeVariableCacheContext =3D (SMM_VARIABLE_COMMUNICATE_RUNTIME_= VARIABLE_CACHE_CONTEXT *) SmmVariableFunctionHeader->Data; + VariableCacheContext =3D &mVariableModuleGlobal->VariableGlobal.Va= riableRuntimeCacheContext; + + ASSERT (RuntimeVariableCacheContext->RuntimeVolatileCache !=3D NUL= L); + ASSERT (RuntimeVariableCacheContext->RuntimeNvCache !=3D NULL); + ASSERT (RuntimeVariableCacheContext->PendingUpdate !=3D NULL); + ASSERT (RuntimeVariableCacheContext->ReadLock !=3D NULL); + ASSERT (RuntimeVariableCacheContext->HobFlushComplete !=3D NULL); + + VariableCacheContext->VariableRuntimeHobCache.Store =3D Runti= meVariableCacheContext->RuntimeHobCache; + VariableCacheContext->VariableRuntimeVolatileCache.Store =3D Runti= meVariableCacheContext->RuntimeVolatileCache; + VariableCacheContext->VariableRuntimeNvCache.Store =3D Runti= meVariableCacheContext->RuntimeNvCache; + VariableCacheContext->PendingUpdate =3D Runti= meVariableCacheContext->PendingUpdate; + VariableCacheContext->ReadLock =3D Runti= meVariableCacheContext->ReadLock; + VariableCacheContext->HobFlushComplete =3D Runti= meVariableCacheContext->HobFlushComplete; + + // Set up the intial pending request since the RT cache needs to b= e in sync with SMM cache + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase =3D=3D 0= ) { + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateOffse= t =3D 0; + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLengt= h =3D 0; + } else { + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableMod= uleGlobal->VariableGlobal.HobVariableBase; + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateOffse= t =3D 0; + VariableCacheContext->VariableRuntimeHobCache.PendingUpdateLengt= h =3D (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCac= he); + CopyGuid (&(VariableCacheContext->VariableRuntimeHobCache.Store-= >Signature), &(VariableCache->Signature)); + } + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModu= leGlobal->VariableGlobal.VolatileVariableBase; + VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateOf= fset =3D 0; + VariableCacheContext->VariableRuntimeVolatileCache.PendingUpdateLe= ngth =3D (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) Variab= leCache); + CopyGuid (&(VariableCacheContext->VariableRuntimeVolatileCache.Sto= re->Signature), &(VariableCache->Signature)); + + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mNvVariableCa= che; + VariableCacheContext->VariableRuntimeNvCache.PendingUpdateOffset = =3D 0; + VariableCacheContext->VariableRuntimeNvCache.PendingUpdateLength = =3D (UINT32) ((UINTN) GetEndPointer (VariableCache) - (UINTN) VariableCache= ); + CopyGuid (&(VariableCacheContext->VariableRuntimeNvCache.Store->Si= gnature), &(VariableCache->Signature)); + + *(VariableCacheContext->PendingUpdate) =3D TRUE; + *(VariableCacheContext->ReadLock) =3D FALSE; + *(VariableCacheContext->HobFlushComplete) =3D FALSE; + } + Status =3D EFI_SUCCESS; + break; + case SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE: + Status =3D SynchronizeRuntimeVariableCacheEx (); + break; + case SMM_VARIABLE_FUNCTION_GET_RUNTIME_CACHE_INFO: + if (CommBufferPayloadSize < sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUN= TIME_CACHE_INFO)) { + DEBUG ((DEBUG_ERROR, "GetRuntimeCacheInfo: SMM communication buffe= r size invalid!\n")); + return EFI_SUCCESS; + } + GetRuntimeCacheInfo =3D (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_= INFO *) SmmVariableFunctionHeader->Data; + + if (mVariableModuleGlobal->VariableGlobal.HobVariableBase > 0) { + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModul= eGlobal->VariableGlobal.HobVariableBase; + GetRuntimeCacheInfo->TotalHobStorageSize =3D VariableCache->Size; + } else { + GetRuntimeCacheInfo->TotalHobStorageSize =3D 0; + } + + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mVariableModule= Global->VariableGlobal.VolatileVariableBase; + GetRuntimeCacheInfo->TotalVolatileStorageSize =3D VariableCache->Siz= e; + VariableCache =3D (VARIABLE_STORE_HEADER *) (UINTN) mNvVariableCach= e; + GetRuntimeCacheInfo->TotalNvStorageSize =3D (UINTN) VariableCache->S= ize; + GetRuntimeCacheInfo->AuthenticatedVariableUsage =3D mVariableModuleG= lobal->VariableGlobal.AuthFormat; + + Status =3D EFI_SUCCESS; + break; =20 default: Status =3D EFI_UNSUPPORTED; diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 5bf90039d6..237908e5a2 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -32,10 +32,12 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include #include #include =20 #include +#include #include =20 #include "PrivilegePolymorphic.h" @@ -47,8 +49,19 @@ EFI_EVENT mVirtualAddressChangeEv= ent =3D NULL; EFI_SMM_COMMUNICATION_PROTOCOL *mSmmCommunication =3D NULL; UINT8 *mVariableBuffer =3D NULL; UINT8 *mVariableBufferPhysical =3D NULL; +VARIABLE_INFO_ENTRY *mVariableInfo =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeHobCacheBuffer = =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeNvCacheBuffer = =3D NULL; +VARIABLE_STORE_HEADER *mVariableRuntimeVolatileCacheBuffer = =3D NULL; UINTN mVariableBufferSize; +UINTN mVariableRuntimeHobCacheBufferSize; +UINTN mVariableRuntimeNvCacheBufferSize; +UINTN mVariableRuntimeVolatileCacheBufferSize; UINTN mVariableBufferPayloadSize; +BOOLEAN mVariableRuntimeCachePendingUpdate; +BOOLEAN mVariableRuntimeCacheReadLock; +BOOLEAN mVariableAuthFormat; +BOOLEAN mHobFlushComplete; EFI_LOCK mVariableServicesLock; EDKII_VARIABLE_LOCK_PROTOCOL mVariableLock; EDKII_VAR_CHECK_PROTOCOL mVarCheck; @@ -108,6 +121,73 @@ ReleaseLockOnlyAtBootTime ( } } =20 +/** + Return TRUE if ExitBootServices () has been called. + + @retval TRUE If ExitBootServices () has been called. +**/ +BOOLEAN +AtRuntime ( + VOID + ) +{ + return EfiAtRuntime (); +} + +/** + Initialize the variable cache buffer as an empty variable store. + + @param[out] VariableCacheBuffer A pointer to pointer of a cache = variable store. + @param[in,out] TotalVariableCacheSize On input, the minimum size neede= d for the UEFI variable store cache + buffer that is allocated. On out= put, the actual size of the buffer allocated. + If TotalVariableCacheSize is zer= o, a buffer will not be allocated and the + function will return with EFI_SU= CCESS. + + @retval EFI_SUCCESS The variable cache was allocated and ini= tialized successfully. + @retval EFI_INVALID_PARAMETER A given pointer is NULL or an invalid va= riable store size was specified. + @retval EFI_OUT_OF_RESOURCES Insufficient resources are available to = allocate the variable store cache buffer. + +**/ +EFI_STATUS +EFIAPI +InitVariableCache ( + OUT VARIABLE_STORE_HEADER **VariableCacheBuffer, + IN OUT UINTN *TotalVariableCacheSize + ) +{ + VARIABLE_STORE_HEADER *VariableCacheStorePtr; + + if (TotalVariableCacheSize =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + if (*TotalVariableCacheSize =3D=3D 0) { + return EFI_SUCCESS; + } + if (VariableCacheBuffer =3D=3D NULL || *TotalVariableCacheSize < sizeof = (VARIABLE_STORE_HEADER)) { + return EFI_INVALID_PARAMETER; + } + *TotalVariableCacheSize =3D ALIGN_VALUE (*TotalVariableCacheSize, sizeof= (UINT32)); + + // + // Allocate NV Storage Cache and initialize it to all 1's (like an erase= d FV) + // + *VariableCacheBuffer =3D (VARIABLE_STORE_HEADER *) AllocateRuntimePages= ( + EFI_SIZE_TO_PAGES (*TotalVariableCacheSize) + ); + if (*VariableCacheBuffer =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + VariableCacheStorePtr =3D *VariableCacheBuffer; + SetMem32 ((VOID *) VariableCacheStorePtr, *TotalVariableCacheSize, (UINT= 32) 0xFFFFFFFF); + + ZeroMem ((VOID *) VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER)); + VariableCacheStorePtr->Size =3D (UINT32) *TotalVariableCacheSize; + VariableCacheStorePtr->Format =3D VARIABLE_STORE_FORMATTED; + VariableCacheStorePtr->State =3D VARIABLE_STORE_HEALTHY; + + return EFI_SUCCESS; +} + /** Initialize the communicate buffer using DataSize and Function. =20 @@ -154,6 +234,69 @@ InitCommunicateBuffer ( } =20 =20 +/** + Gets a SMM communicate buffer from the EDKII_PI_SMM_COMMUNICATION_REGION= _TABLE installed as an entry in the UEFI + system configuration table. A generic SMM communication buffer DXE drive= r may install the table or a custom table + may be installed by a platform-specific driver. + + The communicate size is: SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COM= MUNICATE_HEADER_SIZE + + DataSize. + + @param[in,out] CommBufferSize On input, the minimum size needed for = the communication buffer. + On output, the SMM buffer size availab= le at CommBuffer. + @param[out] CommBuffer A pointer to an SMM communication buff= er pointer. + + @retval EFI_SUCCESS The communication buffer was found suc= cessfully. + @retval EFI_INVALID_PARAMETER A given pointer is NULL or the CommBuf= ferSize is zero. + @retval EFI_NOT_FOUND The EDKII_PI_SMM_COMMUNICATION_REGION_= TABLE was not found. + @retval EFI_OUT_OF_RESOURCES A valid SMM communicate buffer for the= requested size is not available. + +**/ +EFI_STATUS +GetCommunicateBuffer ( + IN OUT UINTN *CommBufferSize, + OUT UINT8 **CommBuffer + ) +{ + EFI_STATUS Status; + EDKII_PI_SMM_COMMUNICATION_REGION_TABLE *PiSmmCommunicationRegionTable; + EFI_MEMORY_DESCRIPTOR *Entry; + UINTN EntrySize; + UINT32 Index; + + if (CommBuffer =3D=3D NULL || CommBufferSize =3D=3D NULL || *CommBufferS= ize =3D=3D 0) { + return EFI_INVALID_PARAMETER; + } + + Status =3D EfiGetSystemConfigurationTable ( + &gEdkiiPiSmmCommunicationRegionTableGuid, + (VOID **) &PiSmmCommunicationRegionTable + ); + if (EFI_ERROR (Status) || PiSmmCommunicationRegionTable =3D=3D NULL) { + return EFI_NOT_FOUND; + } + + Entry =3D (EFI_MEMORY_DESCRIPTOR *) (PiSmmCommunicationRegionTable + 1); + EntrySize =3D 0; + for (Index =3D 0; Index < PiSmmCommunicationRegionTable->NumberOfEntries= ; Index++) { + if (Entry->Type =3D=3D EfiConventionalMemory) { + EntrySize =3D EFI_PAGES_TO_SIZE ((UINTN) Entry->NumberOfPages); + if (EntrySize >=3D *CommBufferSize) { + break; + } + } + Entry =3D (EFI_MEMORY_DESCRIPTOR *) ((UINT8 *) Entry + PiSmmCommunicat= ionRegionTable->DescriptorSize); + } + + if (Index < PiSmmCommunicationRegionTable->NumberOfEntries) { + *CommBufferSize =3D EntrySize; + *CommBuffer =3D (UINT8 *) (UINTN) Entry->PhysicalStart; + return EFI_SUCCESS; + } + + return EFI_OUT_OF_RESOURCES; +} + /** Send the data in communicate buffer to SMM. =20 @@ -425,6 +568,171 @@ Done: return Status; } =20 +/** + Signals SMM to synchronize any pending variable updates with the runtime= cache(s). + +**/ +VOID +EFIAPI +SyncRuntimeCache ( + VOID + ) +{ + // + // Init the communicate buffer. The buffer data size is: + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE. + // + InitCommunicateBuffer (NULL, 0, SMM_VARIABLE_FUNCTION_SYNC_RUNTIME_CACHE= ); + + // + // Send data to SMM. + // + SendCommunicateBuffer (0); +} + +/** + Check whether a SMI must be triggered to retrieve pending cache updates. + + If the variable HOB was finished being flushed since the last check for = a runtime cache update, this function + will prevent the HOB cache from being used for future runtime cache hits. + +**/ +VOID +EFIAPI +CheckForRuntimeCacheSync ( + VOID + ) +{ + if (mVariableRuntimeCachePendingUpdate) { + SyncRuntimeCache (); + } + ASSERT (!mVariableRuntimeCachePendingUpdate); + + // + // The HOB variable data may have finished being flushed in the runtime = cache sync update + // + if (mHobFlushComplete && mVariableRuntimeHobCacheBuffer !=3D NULL) { + if (!AtRuntime ()) { + FreePool (mVariableRuntimeHobCacheBuffer); + } + mVariableRuntimeHobCacheBuffer =3D NULL; + } +} + +/** + This code finds variable in a volatile memory store. + + Caution: This function may receive untrusted input. + The data size is external input, so this function will validate it caref= ully to avoid buffer overflow. + + @param[in] VariableName Name of Variable to be found. + @param[in] VendorGuid Variable vendor GUID. + @param[out] Attributes Attribute value of the variable found. + @param[in, out] DataSize Size of Data found. If size is less t= han the + data, this value contains the require= d size. + @param[out] Data Data pointer. + + @retval EFI_SUCCESS Found the specified variable. + @retval EFI_INVALID_PARAMETER Invalid parameter. + @retval EFI_NOT_FOUND The specified variable could not be f= ound. + +**/ +EFI_STATUS +EFIAPI +FindVariableInRuntimeCache ( + IN CHAR16 *VariableName, + IN EFI_GUID *VendorGuid, + OUT UINT32 *Attributes OPTIONAL, + IN OUT UINTN *DataSize, + OUT VOID *Data OPTIONAL + ) +{ + EFI_STATUS Status; + UINTN DelayIndex; + UINTN TempDataSize; + VARIABLE_POINTER_TRACK RtPtrTrack; + VARIABLE_STORE_TYPE StoreType; + VARIABLE_STORE_HEADER *VariableStoreList[VariableStoreTypeMax]; + + Status =3D EFI_NOT_FOUND; + + if (VariableName =3D=3D NULL || VendorGuid =3D=3D NULL || DataSize =3D= =3D NULL) { + return EFI_INVALID_PARAMETER; + } + + for (DelayIndex =3D 0; mVariableRuntimeCacheReadLock && DelayIndex < VAR= IABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) { + MicroSecondDelay (10); + } + if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) { + ASSERT (!mVariableRuntimeCacheReadLock); + + mVariableRuntimeCacheReadLock =3D TRUE; + CheckForRuntimeCacheSync (); + + if (!mVariableRuntimeCachePendingUpdate) { + // + // 0: Volatile, 1: HOB, 2: Non-Volatile. + // The index and attributes mapping must be kept in this order as Fi= ndVariable + // makes use of this mapping to implement search algorithm. + // + VariableStoreList[VariableStoreTypeVolatile] =3D mVariableRuntimeVol= atileCacheBuffer; + VariableStoreList[VariableStoreTypeHob] =3D mVariableRuntimeHob= CacheBuffer; + VariableStoreList[VariableStoreTypeNv] =3D mVariableRuntimeNvC= acheBuffer; + + for (StoreType =3D (VARIABLE_STORE_TYPE) 0; StoreType < VariableStor= eTypeMax; StoreType++) { + if (VariableStoreList[StoreType] =3D=3D NULL) { + continue; + } + + RtPtrTrack.StartPtr =3D GetStartPointer (VariableStoreList[StoreTy= pe]); + RtPtrTrack.EndPtr =3D GetEndPointer (VariableStoreList[StoreTy= pe]); + RtPtrTrack.Volatile =3D (BOOLEAN) (StoreType =3D=3D VariableStoreT= ypeVolatile); + + Status =3D FindVariableEx (VariableName, VendorGuid, FALSE, &RtPtr= Track); + if (!EFI_ERROR (Status)) { + break; + } + } + + if (!EFI_ERROR (Status)) { + // + // Get data size + // + TempDataSize =3D DataSizeOfVariable (RtPtrTrack.CurrPtr); + ASSERT (TempDataSize !=3D 0); + + if (*DataSize >=3D TempDataSize) { + if (Data =3D=3D NULL) { + Status =3D EFI_INVALID_PARAMETER; + goto Done; + } + + CopyMem (Data, GetVariableDataPtr (RtPtrTrack.CurrPtr), TempData= Size); + if (Attributes !=3D NULL) { + *Attributes =3D RtPtrTrack.CurrPtr->Attributes; + } + + *DataSize =3D TempDataSize; + + UpdateVariableInfo (VariableName, VendorGuid, RtPtrTrack.Volatil= e, TRUE, FALSE, FALSE, TRUE, &mVariableInfo); + + Status =3D EFI_SUCCESS; + goto Done; + } else { + *DataSize =3D TempDataSize; + Status =3D EFI_BUFFER_TOO_SMALL; + goto Done; + } + } + } + } + +Done: + mVariableRuntimeCacheReadLock =3D FALSE; + + return Status; +} + /** This code finds variable in storage blocks (Volatile or Non-Volatile). =20 @@ -455,91 +763,21 @@ RuntimeServiceGetVariable ( ) { EFI_STATUS Status; - UINTN PayloadSize; - SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader; - UINTN TempDataSize; - UINTN VariableNameSize; =20 if (VariableName =3D=3D NULL || VendorGuid =3D=3D NULL || DataSize =3D= =3D NULL) { return EFI_INVALID_PARAMETER; } - - TempDataSize =3D *DataSize; - VariableNameSize =3D StrSize (VariableName); - SmmVariableHeader =3D NULL; - - // - // If VariableName exceeds SMM payload limit. Return failure - // - if (VariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIA= BLE_COMMUNICATE_ACCESS_VARIABLE, Name)) { - return EFI_INVALID_PARAMETER; - } - - AcquireLockOnlyAtBootTime(&mVariableServicesLock); - - // - // Init the communicate buffer. The buffer data size is: - // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + = PayloadSize. - // - if (TempDataSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_= COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize) { - // - // If output data buffer exceed SMM payload limit. Trim output buffer = to SMM payload size - // - TempDataSize =3D mVariableBufferPayloadSize - OFFSET_OF (SMM_VARIABLE_= COMMUNICATE_ACCESS_VARIABLE, Name) - VariableNameSize; - } - PayloadSize =3D OFFSET_OF (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Nam= e) + VariableNameSize + TempDataSize; - - Status =3D InitCommunicateBuffer ((VOID **)&SmmVariableHeader, PayloadSi= ze, SMM_VARIABLE_FUNCTION_GET_VARIABLE); - if (EFI_ERROR (Status)) { - goto Done; - } - ASSERT (SmmVariableHeader !=3D NULL); - - CopyGuid (&SmmVariableHeader->Guid, VendorGuid); - SmmVariableHeader->DataSize =3D TempDataSize; - SmmVariableHeader->NameSize =3D VariableNameSize; - if (Attributes =3D=3D NULL) { - SmmVariableHeader->Attributes =3D 0; - } else { - SmmVariableHeader->Attributes =3D *Attributes; - } - CopyMem (SmmVariableHeader->Name, VariableName, SmmVariableHeader->NameS= ize); - - // - // Send data to SMM. - // - Status =3D SendCommunicateBuffer (PayloadSize); - - // - // Get data from SMM. - // - if (Status =3D=3D EFI_SUCCESS || Status =3D=3D EFI_BUFFER_TOO_SMALL) { - // - // SMM CommBuffer DataSize can be a trimed value - // Only update DataSize when needed - // - *DataSize =3D SmmVariableHeader->DataSize; - } - if (Attributes !=3D NULL) { - *Attributes =3D SmmVariableHeader->Attributes; - } - - if (EFI_ERROR (Status)) { - goto Done; - } - - if (Data !=3D NULL) { - CopyMem (Data, (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->N= ameSize, SmmVariableHeader->DataSize); - } else { - Status =3D EFI_INVALID_PARAMETER; + if (VariableName[0] =3D=3D 0) { + return EFI_NOT_FOUND; } =20 -Done: + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + Status =3D FindVariableInRuntimeCache (VariableName, VendorGuid, Attrib= utes, DataSize, Data); ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; } =20 - /** This code Finds the Next available variable. =20 @@ -871,6 +1109,17 @@ OnReadyToBoot ( // SendCommunicateBuffer (0); =20 + // + // Install the system configuration table for variable info data captured + // + if (FeaturePcdGet (PcdVariableCollectStatistics)) { + if (mVariableAuthFormat) { + gBS->InstallConfigurationTable (&gEfiAuthenticatedVariableGuid, mVar= iableInfo); + } else { + gBS->InstallConfigurationTable (&gEfiVariableGuid, mVariableInfo); + } + } + gBS->CloseEvent (Event); } =20 @@ -894,6 +1143,9 @@ VariableAddressChangeEvent ( { EfiConvertPointer (0x0, (VOID **) &mVariableBuffer); EfiConvertPointer (0x0, (VOID **) &mSmmCommunication); + EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeHobCacheBuffer); + EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeNvCacheBuffer); + EfiConvertPointer (0x0, (VOID **) &mVariableRuntimeVolatileCacheBuffer); } =20 /** @@ -970,6 +1222,173 @@ Done: return Status; } =20 +/** + This code gets information needed from SMM for runtime cache initializat= ion. + + @param[out] TotalHobStorageSize Output pointer for the total HOB= storage size in bytes. + @param[out] TotalNvStorageSize Output pointer for the total non= -volatile storage size in bytes. + @param[out] TotalVolatileStorageSize Output pointer for the total vol= atile storage size in bytes. + @param[out] AuthenticatedVariableUsage Output pointer that indicates if= authenticated variables are to be used. + + @retval EFI_SUCCESS Retrieved the size successfully. + @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is = NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate a CommBuffer. + @retval Others Could not retrieve the size succ= essfully.; + +**/ +EFI_STATUS +EFIAPI +GetRuntimeCacheInfo ( + OUT UINTN *TotalHobStorageSize, + OUT UINTN *TotalNvStorageSize, + OUT UINTN *TotalVolatileStorageSize, + OUT BOOLEAN *AuthenticatedVariableUsage + ) +{ + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO *SmmGetRuntimeCacheI= nfo; + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicateHeade= r; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunction= Header; + UINTN CommSize; + UINTN CommBufferSize; + UINT8 *CommBuffer; + + SmmGetRuntimeCacheInfo =3D NULL; + CommBuffer =3D NULL; + + if (TotalHobStorageSize =3D=3D NULL || TotalNvStorageSize =3D=3D NULL ||= TotalVolatileStorageSize =3D=3D NULL || AuthenticatedVariableUsage =3D=3D = NULL) { + return EFI_INVALID_PARAMETER; + } + + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + + CommSize =3D SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEAD= ER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO); + CommBufferSize =3D CommSize; + Status =3D GetCommunicateBuffer (&CommBufferSize, &CommBuffer); + if (EFI_ERROR (Status)) { + goto Done; + } + if (CommBuffer =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + ZeroMem (CommBuffer, CommBufferSize); + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGui= d); + SmmCommunicateHeader->MessageLength =3D SMM_VARIABLE_COMMUNICATE_HEADER_= SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_INFO); + + SmmVariableFunctionHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCom= municateHeader->Data; + SmmVariableFunctionHeader->Function =3D SMM_VARIABLE_FUNCTION_GET_RUNTIM= E_CACHE_INFO; + SmmGetRuntimeCacheInfo =3D (SMM_VARIABLE_COMMUNICATE_GET_RUNTIME_CACHE_I= NFO *) SmmVariableFunctionHeader->Data; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate (mSmmCommunication, CommBuffer= , &CommSize); + ASSERT_EFI_ERROR (Status); + if (CommSize <=3D SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + Status =3D EFI_BAD_BUFFER_SIZE; + goto Done; + } + + Status =3D SmmVariableFunctionHeader->ReturnStatus; + if (EFI_ERROR (Status)) { + goto Done; + } + + // + // Get data from SMM. + // + *TotalHobStorageSize =3D SmmGetRuntimeCacheInfo->TotalHobStorageSize; + *TotalNvStorageSize =3D SmmGetRuntimeCacheInfo->TotalNvStorageSize; + *TotalVolatileStorageSize =3D SmmGetRuntimeCacheInfo->TotalVolatileStora= geSize; + *AuthenticatedVariableUsage =3D SmmGetRuntimeCacheInfo->AuthenticatedVar= iableUsage; + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; +} + +/** + Sends the runtime variable cache context information to SMM. + + @retval EFI_SUCCESS Retrieved the size successfully. + @retval EFI_INVALID_PARAMETER TotalNvStorageSize parameter is NULL. + @retval EFI_OUT_OF_RESOURCES Could not allocate a CommBuffer. + @retval Others Could not retrieve the size successful= ly.; + +**/ +EFI_STATUS +EFIAPI +SendRuntimeVariableCacheContextToSmm ( + VOID + ) +{ + EFI_STATUS Status; + SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *SmmRuntimeVar= CacheContext; + EFI_SMM_COMMUNICATE_HEADER *SmmCommunicat= eHeader; + SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFu= nctionHeader; + UINTN CommSize; + UINTN CommBufferSize; + UINT8 *CommBuffer; + + SmmRuntimeVarCacheContext =3D NULL; + CommBuffer =3D NULL; + + AcquireLockOnlyAtBootTime (&mVariableServicesLock); + + // + // Init the communicate buffer. The buffer data size is: + // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + = sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + // + CommSize =3D SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEAD= ER_SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + CommBufferSize =3D CommSize; + Status =3D GetCommunicateBuffer (&CommBufferSize, &CommBuffer); + if (EFI_ERROR (Status)) { + goto Done; + } + if (CommBuffer =3D=3D NULL) { + Status =3D EFI_OUT_OF_RESOURCES; + goto Done; + } + ZeroMem (CommBuffer, CommBufferSize); + + SmmCommunicateHeader =3D (EFI_SMM_COMMUNICATE_HEADER *) CommBuffer; + CopyGuid (&SmmCommunicateHeader->HeaderGuid, &gEfiSmmVariableProtocolGui= d); + SmmCommunicateHeader->MessageLength =3D SMM_VARIABLE_COMMUNICATE_HEADER_= SIZE + sizeof (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT); + + SmmVariableFunctionHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *) SmmCom= municateHeader->Data; + SmmVariableFunctionHeader->Function =3D SMM_VARIABLE_FUNCTION_INIT_RUNTI= ME_VARIABLE_CACHE_CONTEXT; + SmmRuntimeVarCacheContext =3D (SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE= _CACHE_CONTEXT *) SmmVariableFunctionHeader->Data; + + SmmRuntimeVarCacheContext->RuntimeHobCache =3D mVariableRuntimeHobCacheB= uffer; + SmmRuntimeVarCacheContext->RuntimeVolatileCache =3D mVariableRuntimeVola= tileCacheBuffer; + SmmRuntimeVarCacheContext->RuntimeNvCache =3D mVariableRuntimeNvCacheBuf= fer; + SmmRuntimeVarCacheContext->PendingUpdate =3D &mVariableRuntimeCachePendi= ngUpdate; + SmmRuntimeVarCacheContext->ReadLock =3D &mVariableRuntimeCacheReadLock; + SmmRuntimeVarCacheContext->HobFlushComplete =3D &mHobFlushComplete; + + // + // Send data to SMM. + // + Status =3D mSmmCommunication->Communicate (mSmmCommunication, CommBuffer= , &CommSize); + ASSERT_EFI_ERROR (Status); + if (CommSize <=3D SMM_VARIABLE_COMMUNICATE_HEADER_SIZE) { + Status =3D EFI_BAD_BUFFER_SIZE; + goto Done; + } + + Status =3D SmmVariableFunctionHeader->ReturnStatus; + if (EFI_ERROR (Status)) { + goto Done; + } + +Done: + ReleaseLockOnlyAtBootTime (&mVariableServicesLock); + return Status; +} + /** Initialize variable service and install Variable Architectural protocol. =20 @@ -986,7 +1405,7 @@ SmmVariableReady ( { EFI_STATUS Status; =20 - Status =3D gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOI= D **)&mSmmVariable); + Status =3D gBS->LocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, (VOI= D **) &mSmmVariable); if (EFI_ERROR (Status)) { return; } @@ -1008,6 +1427,40 @@ SmmVariableReady ( // mVariableBufferPhysical =3D mVariableBuffer; =20 + // + // Allocate runtime variable cache memory buffers. + // + Status =3D GetRuntimeCacheInfo ( + &mVariableRuntimeHobCacheBufferSize, + &mVariableRuntimeNvCacheBufferSize, + &mVariableRuntimeVolatileCacheBufferSize, + &mVariableAuthFormat + ); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeHobCacheBuffer, &mVaria= bleRuntimeHobCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeNvCacheBuffer, &mVari= ableRuntimeNvCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableCache (&mVariableRuntimeVolatileCacheBuffer= , &mVariableRuntimeVolatileCacheBufferSize); + if (!EFI_ERROR (Status)) { + Status =3D InitVariableHelpers (mVariableAuthFormat); + ASSERT_EFI_ERROR (Status); + + Status =3D SendRuntimeVariableCacheContextToSmm (); + if (!EFI_ERROR (Status)) { + SyncRuntimeCache (); + } + } + } + } + if (EFI_ERROR (Status)) { + mVariableRuntimeHobCacheBuffer =3D NULL; + mVariableRuntimeNvCacheBuffer =3D NULL; + mVariableRuntimeVolatileCacheBuffer =3D NULL; + } + } + ASSERT_EFI_ERROR (Status); + gRT->GetVariable =3D RuntimeServiceGetVariable; gRT->GetNextVariableName =3D RuntimeServiceGetNextVariableName; gRT->SetVariable =3D RuntimeServiceSetVariable; --=20 2.16.2.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 (#48073): https://edk2.groups.io/g/devel/message/48073 Mute This Topic: https://groups.io/mt/34295311/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 Mar 28 08:01:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+48074+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48074+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1569473481; cv=none; d=zoho.com; s=zohoarc; b=bC5B4E0jxh0I3u9J0sffY2cVI63I5hnO56cIOsd62f7VSScw6IO7Foxh0KtYCEFFZhzt7k/CjSkGZ1EcXoel00iNVC6SyuGiNh9dOXy+YGEsgYBsTbOzwh4WJv8ZS+Uqlh7yf9OFrnYgmd3dYeiNvW1EPct/FG9Y46CeU3Pd/4c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569473481; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=SIr/FNzAbGcrN9pXHTKIVX/PfrUVJV2yOr5osXqtxCI=; b=mi80dAJkpTDowDmEGC0jMixkBFGWte3/vW2tTJY44KwgRV/Ao+ba9QdW9UH3+MSVK3WMy1xIDcF8L2ISPPxDuo0Sg5mslKmqAICfpjW+EziQCgz++aa4/RTbTc5fECxLV7cdqu51iQibRpt43pKgugIrBXxCC277mdBH82ma72E= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48074+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 1569473481601571.1859237331364; Wed, 25 Sep 2019 21:51:21 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id FBP7YY1788612xrSRntSGKkB; Wed, 25 Sep 2019 21:51:21 -0700 X-Received: from mga02.intel.com (mga02.intel.com []) by groups.io with SMTP; Wed, 25 Sep 2019 21:51:20 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 21:51:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,550,1559545200"; d="scan'208";a="191570772" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2019 21:51:20 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V1 4/5] MdeModulePkg/Variable: Add RT GetNextVariableName() cache support Date: Wed, 25 Sep 2019 21:50:45 -0700 Message-Id: <20190926045046.34592-5-michael.a.kubacki@intel.com> In-Reply-To: <20190926045046.34592-1-michael.a.kubacki@intel.com> References: <20190926045046.34592-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: 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,michael.a.kubacki@intel.com X-Gm-Message-State: lFNSGgvtjR5Hsxm4hXzLm4lvx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1569473481; bh=EKST+rymNFCytl0Bhk98i0lRJ0UDV90fdsJSY1NIY3U=; h=Cc:Date:From:Reply-To:Subject:To; b=UOPn+I7wPw9uIUnvReVoW3Is3NUUstbimPMzVbODui/qvy7uJyXH28FPGX24bOaAif0 gOv37OscCjWZiUbGMQ2F0SgGgaM0p8OoxBrtIf72hUGIH+rVRDRuaR/UreJVheYLS3I3B HNVrEUiy9N4lZOLT7S2PIIBeRp1i3QtYESY= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" https://bugzilla.tianocore.org/show_bug.cgi?id=3D2220 This change implements the Runtime Service GetNextVariableName() using the Runtime Cache in VariableSmmRuntimeDxe. Runtime Service calls to GetNextVariableName() will no longer trigger a SW SMI. Overall system performance and stability will be improved by eliminating an SMI for these calls as they typically result in a relatively large number of invocations to retrieve all variable names in all variable stores present. Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 118 += ++++++++----------- 1 file changed, 50 insertions(+), 68 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeD= xe.c b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c index 237908e5a2..ff3c390a7d 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c @@ -799,87 +799,69 @@ RuntimeServiceGetNextVariableName ( IN OUT EFI_GUID *VendorGuid ) { - EFI_STATUS Status; - UINTN PayloadSize; - SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *SmmGetNextVariableName; - UINTN OutVariableNameSize; - UINTN InVariableNameSize; + EFI_STATUS Status; + UINTN DelayIndex; + UINTN MaxLen; + UINTN VarNameSize; + VARIABLE_HEADER *VariablePtr; + VARIABLE_STORE_HEADER *VariableStoreHeader[VariableStoreTypeMax]; + + Status =3D EFI_NOT_FOUND; =20 if (VariableNameSize =3D=3D NULL || VariableName =3D=3D NULL || VendorGu= id =3D=3D NULL) { return EFI_INVALID_PARAMETER; } =20 - OutVariableNameSize =3D *VariableNameSize; - InVariableNameSize =3D StrSize (VariableName); - SmmGetNextVariableName =3D NULL; - // - // If input string exceeds SMM payload limit. Return failure + // Calculate the possible maximum length of name string, including the N= ull terminator. // - if (InVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VAR= IABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) { + MaxLen =3D *VariableNameSize / sizeof (CHAR16); + if ((MaxLen =3D=3D 0) || (StrnLenS (VariableName, MaxLen) =3D=3D MaxLen)= ) { + // + // Null-terminator is not found in the first VariableNameSize bytes of= the input VariableName buffer, + // follow spec to return EFI_INVALID_PARAMETER. + // return EFI_INVALID_PARAMETER; } =20 - AcquireLockOnlyAtBootTime(&mVariableServicesLock); + AcquireLockOnlyAtBootTime (&mVariableServicesLock); =20 - // - // Init the communicate buffer. The buffer data size is: - // SMM_COMMUNICATE_HEADER_SIZE + SMM_VARIABLE_COMMUNICATE_HEADER_SIZE + = PayloadSize. - // - if (OutVariableNameSize > mVariableBufferPayloadSize - OFFSET_OF (SMM_VA= RIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) { - // - // If output buffer exceed SMM payload limit. Trim output buffer to SM= M payload size - // - OutVariableNameSize =3D mVariableBufferPayloadSize - OFFSET_OF (SMM_VA= RIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name); + for (DelayIndex =3D 0; mVariableRuntimeCacheReadLock && DelayIndex < VAR= IABLE_RT_CACHE_READ_LOCK_TIMEOUT; DelayIndex++) { + MicroSecondDelay (10); } - // - // Payload should be Guid + NameSize + MAX of Input & Output buffer - // - PayloadSize =3D OFFSET_OF (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NA= ME, Name) + MAX (OutVariableNameSize, InVariableNameSize); - - Status =3D InitCommunicateBuffer ((VOID **)&SmmGetNextVariableName, Payl= oadSize, SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME); - if (EFI_ERROR (Status)) { - goto Done; - } - ASSERT (SmmGetNextVariableName !=3D NULL); - - // - // SMM comm buffer->NameSize is buffer size for return string - // - SmmGetNextVariableName->NameSize =3D OutVariableNameSize; - - CopyGuid (&SmmGetNextVariableName->Guid, VendorGuid); - // - // Copy whole string - // - CopyMem (SmmGetNextVariableName->Name, VariableName, InVariableNameSize); - if (OutVariableNameSize > InVariableNameSize) { - ZeroMem ((UINT8 *) SmmGetNextVariableName->Name + InVariableNameSize, = OutVariableNameSize - InVariableNameSize); - } - - // - // Send data to SMM - // - Status =3D SendCommunicateBuffer (PayloadSize); - - // - // Get data from SMM. - // - if (Status =3D=3D EFI_SUCCESS || Status =3D=3D EFI_BUFFER_TOO_SMALL) { - // - // SMM CommBuffer NameSize can be a trimed value - // Only update VariableNameSize when needed - // - *VariableNameSize =3D SmmGetNextVariableName->NameSize; - } - if (EFI_ERROR (Status)) { - goto Done; + if (DelayIndex < VARIABLE_RT_CACHE_READ_LOCK_TIMEOUT) { + ASSERT (!mVariableRuntimeCacheReadLock); + + CheckForRuntimeCacheSync (); + mVariableRuntimeCacheReadLock =3D TRUE; + + if (!mVariableRuntimeCachePendingUpdate) { + // + // 0: Volatile, 1: HOB, 2: Non-Volatile. + // The index and attributes mapping must be kept in this order as Fi= ndVariable + // makes use of this mapping to implement search algorithm. + // + VariableStoreHeader[VariableStoreTypeVolatile] =3D mVariableRuntimeV= olatileCacheBuffer; + VariableStoreHeader[VariableStoreTypeHob] =3D mVariableRuntimeH= obCacheBuffer; + VariableStoreHeader[VariableStoreTypeNv] =3D mVariableRuntimeN= vCacheBuffer; + + Status =3D GetNextVariableEx (VariableName, VendorGuid, VariableStor= eHeader, &VariablePtr); + if (!EFI_ERROR (Status)) { + VarNameSize =3D NameSizeOfVariable (VariablePtr); + ASSERT (VarNameSize !=3D 0); + if (VarNameSize <=3D *VariableNameSize) { + CopyMem (VariableName, GetVariableNamePtr (VariablePtr), VarName= Size); + CopyMem (VendorGuid, GetVendorGuidPtr (VariablePtr), sizeof (EFI= _GUID)); + Status =3D EFI_SUCCESS; + } else { + Status =3D EFI_BUFFER_TOO_SMALL; + } + + *VariableNameSize =3D VarNameSize; + } + } } - - CopyGuid (VendorGuid, &SmmGetNextVariableName->Guid); - CopyMem (VariableName, SmmGetNextVariableName->Name, SmmGetNextVariableN= ame->NameSize); - -Done: + mVariableRuntimeCacheReadLock =3D FALSE; ReleaseLockOnlyAtBootTime (&mVariableServicesLock); return Status; } --=20 2.16.2.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 (#48074): https://edk2.groups.io/g/devel/message/48074 Mute This Topic: https://groups.io/mt/34295312/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 Mar 28 08:01:01 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) client-ip=66.175.222.12; envelope-from=bounce+27952+48075+1787277+3901457@groups.io; helo=web01.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48075+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1569473482; cv=none; d=zoho.com; s=zohoarc; b=nWD5b2VQFCsfYfdQM1QGyaOZGQNAWb0hiz3uAjPye5+bW1xkp9ay7Qcs2AOukgRBVMFJ/eKtbC6JDpxvbg7AkwJgjkjOjGNLMZZfIjDGVMRcshgG/zHsKNk9yDs3SeHlQWW7vFOImyQSx7RTaVQHfvzqNKZFrd5eVYmKtGy91Gw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1569473482; h=Cc:Date:From:In-Reply-To:List-Id:List-Unsubscribe:Message-ID:Reply-To:References:Sender:Subject:To:ARC-Authentication-Results; bh=VEPiOPiJAOKmjXuvpUK+oP4YkuaOpOUcIOFegiZo4Rg=; b=E6ny1Vj1BWiplmXHFClyRsuOH4T7lYoUcQwlmFUviM/IKnba24gGwJG9+YSAKMbzVNl5MPiepMpjqH3qL9cA3nN6v9qD+9/7PYn9P0Y+TfGLOuM1rcwtlW7pUqSUUy/0omZ8GurIoOpWCBKB5JwtMRVz7gr6y69vCjglrgxrOd0= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of groups.io designates 66.175.222.12 as permitted sender) smtp.mailfrom=bounce+27952+48075+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) header.from= Received: from web01.groups.io (web01.groups.io [66.175.222.12]) by mx.zohomail.com with SMTPS id 156947348296462.35639830223931; Wed, 25 Sep 2019 21:51:22 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 70h4YY1788612xIIuFZX25sA; Wed, 25 Sep 2019 21:51:22 -0700 X-Received: from mga02.intel.com (mga02.intel.com []) by groups.io with SMTP; Wed, 25 Sep 2019 21:51:21 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 25 Sep 2019 21:51:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.64,550,1559545200"; d="scan'208";a="191570779" X-Received: from makuback-desk1.amr.corp.intel.com ([10.7.159.162]) by orsmga003.jf.intel.com with ESMTP; 25 Sep 2019 21:51:21 -0700 From: "Kubacki, Michael A" To: devel@edk2.groups.io Cc: Dandan Bi , Ard Biesheuvel , Eric Dong , Laszlo Ersek , Liming Gao , Michael D Kinney , Ray Ni , Jian J Wang , Hao A Wu , Jiewen Yao Subject: [edk2-devel] [PATCH V1 5/5] MdeModulePkg/VariableSmm: Remove unused SMI handler functions Date: Wed, 25 Sep 2019 21:50:46 -0700 Message-Id: <20190926045046.34592-6-michael.a.kubacki@intel.com> In-Reply-To: <20190926045046.34592-1-michael.a.kubacki@intel.com> References: <20190926045046.34592-1-michael.a.kubacki@intel.com> Precedence: Bulk List-Unsubscribe: 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,michael.a.kubacki@intel.com X-Gm-Message-State: WsVdRrvI8gLajDBOaYmG1Zydx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1569473482; bh=pAXE1LQcBrFTw65ziJICSMSRnJS8cNEAFgMlW47F2vM=; h=Cc:Date:From:Reply-To:Subject:To; b=uhU0Fej/5BGjFHeK1jEEEOHbBsklWAgOL5qIIZHR1/ZgjSnqguQo9xfNg8SPrlhpPU3 tc80uPTdh+zx1QnCLPILK7VvQnX20UjC8OdWlTYkeS1JSQotan2Aq+LyHK+SKKn4Glx6l DcqZgpXkWJvwOw2PCegE4k8HLNsXIPOc9Yk= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D2220 Since Runtime Services GetVariable() and GetNextVariableName() no longer trigger a SW SMI, this change removes the code for handling those requests from VariableSmm.c. The following SMM communicate functions are removed from SmmVariableHandler(): 1. SMM_VARIABLE_FUNCTION_GET_VARIABLE 2. SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME The function numbers are retained so any calls to those functions from previously built drivers will return EFI_UNSUPPORTED. Cc: Dandan Bi Cc: Ard Biesheuvel Cc: Eric Dong Cc: Laszlo Ersek Cc: Liming Gao Cc: Michael D Kinney Cc: Ray Ni Cc: Jian J Wang Cc: Hao A Wu Cc: Jiewen Yao Signed-off-by: Michael Kubacki --- MdeModulePkg/Include/Guid/SmmVariableCommon.h | 4 +- MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c | 101 -----------= --------- 2 files changed, 1 insertion(+), 104 deletions(-) diff --git a/MdeModulePkg/Include/Guid/SmmVariableCommon.h b/MdeModulePkg/I= nclude/Guid/SmmVariableCommon.h index ceef44dfd2..6a73d1e21a 100644 --- a/MdeModulePkg/Include/Guid/SmmVariableCommon.h +++ b/MdeModulePkg/Include/Guid/SmmVariableCommon.h @@ -106,7 +106,7 @@ typedef struct { EFI_GUID Guid; UINTN NameSize; // Return name buffer size CHAR16 Name[1]; -} SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME; +} SMM_VARIABLE_COMMUNICATE_LOCK_VARIABLE; =20 /// /// This structure is used to communicate with SMI handler by QueryVariabl= eInfo. @@ -118,8 +118,6 @@ typedef struct { UINT32 Attributes; } SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO; =20 -typedef SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME SMM_VARIABLE_COMMU= NICATE_LOCK_VARIABLE; - typedef struct { EFI_GUID Guid; UINTN NameSize; diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c b/Mde= ModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c index 1cb6092582..72448ddb46 100644 --- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c +++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmm.c @@ -464,7 +464,6 @@ SmmVariableHandler ( EFI_STATUS Status; SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunc= tionHeader; SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHead= er; - SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariable= Name; SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableIn= fo; SMM_VARIABLE_COMMUNICATE_GET_PAYLOAD_SIZE *GetPayloadSize; SMM_VARIABLE_COMMUNICATE_RUNTIME_VARIABLE_CACHE_CONTEXT *RuntimeVariable= CacheContext; @@ -475,7 +474,6 @@ SmmVariableHandler ( VARIABLE_RUNTIME_CACHE_CONTEXT *VariableCacheCo= ntext; VARIABLE_STORE_HEADER *VariableCache; UINTN InfoSize; - UINTN NameBufferSize; UINTN CommBufferPayloa= dSize; UINTN TempCommBufferSi= ze; =20 @@ -505,105 +503,6 @@ SmmVariableHandler ( =20 SmmVariableFunctionHeader =3D (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuf= fer; switch (SmmVariableFunctionHeader->Function) { - case SMM_VARIABLE_FUNCTION_GET_VARIABLE: - if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCES= S_VARIABLE, Name)) { - DEBUG ((EFI_D_ERROR, "GetVariable: SMM communication buffer size i= nvalid!\n")); - return EFI_SUCCESS; - } - // - // Copy the input communicate buffer payload to pre-allocated SMM va= riable buffer payload. - // - CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, Co= mmBufferPayloadSize); - SmmVariableHeader =3D (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) m= VariableBufferPayload; - if (((UINTN)(~0) - SmmVariableHeader->DataSize < OFFSET_OF(SMM_VARIA= BLE_COMMUNICATE_ACCESS_VARIABLE, Name)) || - ((UINTN)(~0) - SmmVariableHeader->NameSize < OFFSET_OF(SMM_VARIAB= LE_COMMUNICATE_ACCESS_VARIABLE, Name) + SmmVariableHeader->DataSize)) { - // - // Prevent InfoSize overflow happen - // - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - InfoSize =3D OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE, Nam= e) - + SmmVariableHeader->DataSize + SmmVariableHeader->NameSi= ze; - - // - // SMRAM range check already covered before - // - if (InfoSize > CommBufferPayloadSize) { - DEBUG ((EFI_D_ERROR, "GetVariable: Data size exceed communication = buffer size limit!\n")); - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - - // - // The VariableSpeculationBarrier() call here is to ensure the previ= ous - // range/content checks for the CommBuffer have been completed befor= e the - // subsequent consumption of the CommBuffer content. - // - VariableSpeculationBarrier (); - if (SmmVariableHeader->NameSize < sizeof (CHAR16) || SmmVariableHead= er->Name[SmmVariableHeader->NameSize/sizeof (CHAR16) - 1] !=3D L'\0') { - // - // Make sure VariableName is A Null-terminated string. - // - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - - Status =3D VariableServiceGetVariable ( - SmmVariableHeader->Name, - &SmmVariableHeader->Guid, - &SmmVariableHeader->Attributes, - &SmmVariableHeader->DataSize, - (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->Nam= eSize - ); - CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, Co= mmBufferPayloadSize); - break; - - case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME: - if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_N= EXT_VARIABLE_NAME, Name)) { - DEBUG ((EFI_D_ERROR, "GetNextVariableName: SMM communication buffe= r size invalid!\n")); - return EFI_SUCCESS; - } - // - // Copy the input communicate buffer payload to pre-allocated SMM va= riable buffer payload. - // - CopyMem (mVariableBufferPayload, SmmVariableFunctionHeader->Data, Co= mmBufferPayloadSize); - GetNextVariableName =3D (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_= NAME *) mVariableBufferPayload; - if ((UINTN)(~0) - GetNextVariableName->NameSize < OFFSET_OF(SMM_VARI= ABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME, Name)) { - // - // Prevent InfoSize overflow happen - // - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - InfoSize =3D OFFSET_OF(SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NA= ME, Name) + GetNextVariableName->NameSize; - - // - // SMRAM range check already covered before - // - if (InfoSize > CommBufferPayloadSize) { - DEBUG ((EFI_D_ERROR, "GetNextVariableName: Data size exceed commun= ication buffer size limit!\n")); - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - - NameBufferSize =3D CommBufferPayloadSize - OFFSET_OF(SMM_VARIABLE_CO= MMUNICATE_GET_NEXT_VARIABLE_NAME, Name); - if (NameBufferSize < sizeof (CHAR16) || GetNextVariableName->Name[Na= meBufferSize/sizeof (CHAR16) - 1] !=3D L'\0') { - // - // Make sure input VariableName is A Null-terminated string. - // - Status =3D EFI_ACCESS_DENIED; - goto EXIT; - } - - Status =3D VariableServiceGetNextVariableName ( - &GetNextVariableName->NameSize, - GetNextVariableName->Name, - &GetNextVariableName->Guid - ); - CopyMem (SmmVariableFunctionHeader->Data, mVariableBufferPayload, Co= mmBufferPayloadSize); - break; - case SMM_VARIABLE_FUNCTION_SET_VARIABLE: if (CommBufferPayloadSize < OFFSET_OF(SMM_VARIABLE_COMMUNICATE_ACCES= S_VARIABLE, Name)) { DEBUG ((EFI_D_ERROR, "SetVariable: SMM communication buffer size i= nvalid!\n")); --=20 2.16.2.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 (#48075): https://edk2.groups.io/g/devel/message/48075 Mute This Topic: https://groups.io/mt/34295313/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-