From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id E0B54D81111 for ; Fri, 17 May 2024 09:49:45 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=+IZi4dGVB39RHtue5XVyXEwVLUO6/8g/Ry5Kol4uyBg=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1715939384; v=1; b=0qkFSqiSi16l93GTMsnTD8gSTuA5eu7H38hiCnbLmOeW4Oy+sswCHJDeU9sYqMa04eBsYND0 odSIVkZUkxgYGxIE82apNqAWerrqngYoE4ciqJcQkvxboy8hDEPtlf8JmBff8S0h3obBEC2kQxj EJS3PDL7Z/cfFXGSXslmIl8JVH7d4m2gr68GeG/52JCPY7a0c8aAu2Fgt8EjdY+G/aFAkCQcHSz bjFGGbqYXCBkQCgHWWZ7AJmEDSJLDr8HhajQ4BFHcoSlYhglnzpVJMNlGZNvlG05QRwYUHJwrdP M+Nnr+tufJ9SNRSqxoBCvGchS1WKLcE5RsxNjzWUbIcaw== X-Received: by 127.0.0.2 with SMTP id impIYY7687511xDmoPbpRnss; Fri, 17 May 2024 02:49:44 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.16]) by mx.groups.io with SMTP id smtpd.web10.36016.1715939370910548931 for ; Fri, 17 May 2024 02:49:43 -0700 X-CSE-ConnectionGUID: NNlAgv4xT+yPQ8UpEvnlTA== X-CSE-MsgGUID: PPyMRPLzQ5+dJWhE9GU4Vw== X-IronPort-AV: E=McAfee;i="6600,9927,11074"; a="11570301" X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="11570301" X-Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa110.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2024 02:49:44 -0700 X-CSE-ConnectionGUID: O2ie/yLZT/iu2nG++9Jshg== X-CSE-MsgGUID: keMUGaE+Q5u4zfVvUZpSbg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,167,1712646000"; d="scan'208";a="31739255" X-Received: from unknown (HELO shwdeopenlab702.ccr.corp.intel.com) ([10.239.55.43]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 May 2024 02:49:43 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Ray Ni , Liming Gao , Jiaxin Wu Subject: [edk2-devel] [PATCH 5/9] MdeModulePkg:Create gEdkiiVariableRuntimeCacheInfoHobGuid Date: Fri, 17 May 2024 17:49:13 +0800 Message-Id: <20240517094917.513-6-dun.tan@intel.com> In-Reply-To: <20240517094917.513-1-dun.tan@intel.com> References: <20240517094917.513-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Fri, 17 May 2024 02:49:43 -0700 Resent-From: dun.tan@intel.com Reply-To: devel@edk2.groups.io,dun.tan@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: oK42JyWOcqXge4nt4Hfm7EWGx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=0qkFSqiS; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none) Install the callback of gEfiPeiMemoryDiscoveredPpiGuid to create gEdkiiVariableRuntimeCacheInfoHobGuid in VariablePei module. When PcdEnableVariableRuntimeCache is TRUE, the callback will be installed to allocate the needed buffer for different type variable runtime cache, unblock the buffer and build this HOB. Then the runtime cache buffer address and size will be saved in the HOB content. Signed-off-by: Dun Tan Cc: Ray Ni Cc: Liming Gao Cc: Jiaxin Wu --- MdeModulePkg/Universal/Variable/Pei/Variable.c | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- MdeModulePkg/Universal/Variable/Pei/Variable.h | 3 +++ MdeModulePkg/Universal/Variable/Pei/VariablePei.inf | 8 +++++++- 3 files changed, 307 insertions(+), 2 deletions(-) diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.c b/MdeModulePkg/Universal/Variable/Pei/Variable.c index 26a4c73b45..15419eb437 100644 --- a/MdeModulePkg/Universal/Variable/Pei/Variable.c +++ b/MdeModulePkg/Universal/Variable/Pei/Variable.c @@ -2,7 +2,7 @@ Implement ReadOnly Variable Services required by PEIM and install PEI ReadOnly Varaiable2 PPI. These services operates the non volatile storage space. -Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent @@ -24,6 +24,31 @@ EFI_PEI_PPI_DESCRIPTOR mPpiListVariable = { &mVariablePpi }; +/** + Build gEdkiiVariableRuntimeCacheInfoHobGuid. + + @param[in] PeiServices General purpose services available to every PEIM. + @param[in] NotifyDescriptor The notification structure this PEIM registered on install. + @param[in] Ppi The memory discovered PPI. Not used. + + @retval EFI_SUCCESS The function completed successfully. + @retval others Failed to build VariableRuntimeCacheInfo Hob. + +**/ +EFI_STATUS +EFIAPI +BuildVariableRuntimeCacheInfoHob ( + IN EFI_PEI_SERVICES **PeiServices, + IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor, + IN VOID *Ppi + ); + +EFI_PEI_NOTIFY_DESCRIPTOR mPostMemNotifyList = { + (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), + &gEfiPeiMemoryDiscoveredPpiGuid, + BuildVariableRuntimeCacheInfoHob +}; + /** Provide the functionality of the variable services. @@ -41,6 +66,10 @@ PeimInitializeVariableServices ( IN CONST EFI_PEI_SERVICES **PeiServices ) { + if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) { + PeiServicesNotifyPpi (&mPostMemNotifyList); + } + return PeiServicesInstallPpi (&mPpiListVariable); } @@ -1250,3 +1279,270 @@ PeiGetNextVariableName ( } } } + +/** + Calculate the auth variable storage size converted from normal variable storage. + + @param[in] StoreInfo Pointer to the store info + @param[in] NormalHobVarStorage Pointer to the normal variable storage header + + @retval the auth variable storage size +**/ +UINTN +CalculateAuthVarStorageSize ( + IN VARIABLE_STORE_INFO *StoreInfo, + IN VARIABLE_STORE_HEADER *NormalHobVarStorage + ) +{ + VARIABLE_HEADER *StartPtr; + VARIABLE_HEADER *EndPtr; + UINTN AuthVarStroageSize; + + AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER); + + // + // Calculate Auth Variable Storage Size + // + StartPtr = GetStartPointer (NormalHobVarStorage); + EndPtr = GetEndPointer (NormalHobVarStorage); + while (StartPtr < EndPtr) { + if (StartPtr->State == VAR_ADDED) { + AuthVarStroageSize = HEADER_ALIGN (AuthVarStroageSize); + AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER); + AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE (StartPtr->NameSize); + AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE (StartPtr->DataSize); + } + + StartPtr = GetNextVariablePtr (StoreInfo, StartPtr, StartPtr); + } + + return AuthVarStroageSize; +} + +/** + Calculate Hob variable cache size. + + @param[in] NvAuthFlag If the NV variable store is Auth. + + @retval Maximum of Nv variable cache size. + +**/ +UINTN +CalculateHobVariableCacheSize ( + IN BOOLEAN NvAuthFlag + ) +{ + VARIABLE_STORE_INFO StoreInfo; + VARIABLE_STORE_HEADER *VariableStoreHeader; + + VariableStoreHeader = NULL; + GetHobVariableStore (&StoreInfo, &VariableStoreHeader); + + if (VariableStoreHeader == NULL) { + return 0; + } + + if (NvAuthFlag == StoreInfo.AuthFlag) { + return VariableStoreHeader->Size; + } else { + // + // Normal NV variable store + Auth HOB variable store is not supported + // + ASSERT (NvAuthFlag && (!StoreInfo.AuthFlag)); + + // + // Need to calculate auth variable storage size converted from normal variable storage + // + return CalculateAuthVarStorageSize (&StoreInfo, VariableStoreHeader); + } +} + +/** + Calculate Nv variable cache size. + + @param[out] NvAuthFlag If the NV variable store is Auth. + + @retval Maximum of Nv variable cache size. + +**/ +UINTN +CalculateNvVariableCacheSize ( + OUT BOOLEAN *NvAuthFlag + ) +{ + EFI_STATUS Status; + EFI_HOB_GUID_TYPE *GuidHob; + EFI_FIRMWARE_VOLUME_HEADER *FvHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + EFI_PHYSICAL_ADDRESS NvStorageBase; + UINT32 NvStorageSize; + UINT64 NvStorageSize64; + FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *FtwLastWriteData; + + if (PcdGetBool (PcdEmuVariableNvModeEnable)) { + return PcdGet32 (PcdVariableStoreSize); + } + + Status = GetVariableFlashNvStorageInfo (&NvStorageBase, &NvStorageSize64); + ASSERT_EFI_ERROR (Status); + + Status = SafeUint64ToUint32 (NvStorageSize64, &NvStorageSize); + ASSERT_EFI_ERROR (Status); + ASSERT (NvStorageBase != 0); + FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)NvStorageBase; + + // + // Check the FTW last write data hob. + // + GuidHob = GetFirstGuidHob (&gEdkiiFaultTolerantWriteGuid); + if (GuidHob != NULL) { + FtwLastWriteData = (FAULT_TOLERANT_WRITE_LAST_WRITE_DATA *)GET_GUID_HOB_DATA (GuidHob); + if (FtwLastWriteData->TargetAddress == NvStorageBase) { + // + // Let FvHeader point to spare block. + // + FvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)FtwLastWriteData->SpareAddress; + } + } + + VariableStoreHeader = (VARIABLE_STORE_HEADER *)((UINT8 *)FvHeader + FvHeader->HeaderLength); + *NvAuthFlag = (BOOLEAN)(CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGuid)); + + return NvStorageSize - FvHeader->HeaderLength; +} + +/** + Build gEdkiiVariableRuntimeCacheInfoHobGuid. + + @param[in] PeiServices General purpose services available to every PEIM. + @param[in] NotifyDescriptor The notification structure this PEIM registered on install. + @param[in] Ppi The memory discovered PPI. Not used. + + @retval EFI_SUCCESS The function completed successfully. + @retval others Failed to build VariableRuntimeCacheInfo Hob. + +**/ +EFI_STATUS +EFIAPI +BuildVariableRuntimeCacheInfoHob ( + IN EFI_PEI_SERVICES **PeiServices, + IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor, + IN VOID *Ppi + ) +{ + VARIABLE_RUNTIME_CACHE_INFO *VariableRuntimeCacheHob; + EFI_STATUS Status; + VOID *Buffer; + UINTN BufferSize; + BOOLEAN NvAuthFlag; + UINTN Pages; + + VariableRuntimeCacheHob = BuildGuidHob (&gEdkiiVariableRuntimeCacheInfoHobGuid, sizeof (VARIABLE_RUNTIME_CACHE_INFO)); + ASSERT (VariableRuntimeCacheHob != NULL); + ZeroMem (VariableRuntimeCacheHob, sizeof (VARIABLE_RUNTIME_CACHE_INFO)); + + // + // AllocateRuntimePages for CACHE_INFO_FLAG and unblock it. + // + Pages = EFI_SIZE_TO_PAGES (sizeof (CACHE_INFO_FLAG)); + Buffer = AllocateRuntimePages (Pages); + ASSERT (Buffer != NULL); + Status = MmUnblockMemoryRequest ( + (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, + Pages + ); + if ((Status != EFI_UNSUPPORTED) && EFI_ERROR (Status)) { + return Status; + } + + VariableRuntimeCacheHob->CacheInfoFlag = (CACHE_INFO_FLAG *)(UINTN)Buffer; + DEBUG (( + DEBUG_INFO, + "PeiVariable: CACHE_INFO_FLAG Buffer is: 0x%x, number of pages is: 0x%x\n", + (UINTN)(VariableRuntimeCacheHob->CacheInfoFlag), + Pages + )); + + // + // AllocateRuntimePages for VolatileCache and unblock it. + // + BufferSize = PcdGet32 (PcdVariableStoreSize); + if (BufferSize > 0) { + Pages = EFI_SIZE_TO_PAGES (BufferSize); + Buffer = AllocateRuntimePages (Pages); + ASSERT (Buffer != NULL); + Status = MmUnblockMemoryRequest ( + (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, + Pages + ); + if ((Status != EFI_UNSUPPORTED) && EFI_ERROR (Status)) { + return Status; + } + + VariableRuntimeCacheHob->RuntimeVolatileCacheBuffer = (UINTN)Buffer; + VariableRuntimeCacheHob->RuntimeVolatileCachePages = Pages; + } + + DEBUG (( + DEBUG_INFO, + "PeiVariable: Volatile cache Buffer is: 0x%x, number of pages is: 0x%x\n", + VariableRuntimeCacheHob->RuntimeVolatileCacheBuffer, + VariableRuntimeCacheHob->RuntimeVolatileCachePages + )); + + // + // AllocateRuntimePages for NVCache and unblock it. + // + BufferSize = CalculateNvVariableCacheSize (&NvAuthFlag); + if (BufferSize > 0) { + Pages = EFI_SIZE_TO_PAGES (BufferSize); + Buffer = AllocateRuntimePages (Pages); + ASSERT (Buffer != NULL); + Status = MmUnblockMemoryRequest ( + (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, + Pages + ); + if ((Status != EFI_UNSUPPORTED) && EFI_ERROR (Status)) { + return Status; + } + + VariableRuntimeCacheHob->RuntimeNvCacheBuffer = (UINTN)Buffer; + VariableRuntimeCacheHob->RuntimeNvCachePages = Pages; + } + + DEBUG (( + DEBUG_INFO, + "PeiVariable: NV cache Buffer is: 0x%x, number of pages is: 0x%x\n", + VariableRuntimeCacheHob->RuntimeNvCacheBuffer, + VariableRuntimeCacheHob->RuntimeNvCachePages + )); + + // + // AllocateRuntimePages for HobCache and unblock it. + // + BufferSize = CalculateHobVariableCacheSize (NvAuthFlag); + if (BufferSize > 0) { + Pages = EFI_SIZE_TO_PAGES (BufferSize); + Buffer = AllocateRuntimePages (Pages); + ASSERT (Buffer != NULL); + Status = MmUnblockMemoryRequest ( + (EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, + Pages + ); + if ((Status != EFI_UNSUPPORTED) && EFI_ERROR (Status)) { + return Status; + } + + VariableRuntimeCacheHob->RuntimeHobCacheBuffer = (UINTN)Buffer; + VariableRuntimeCacheHob->RuntimeHobCachePages = Pages; + } + + DEBUG (( + DEBUG_INFO, + "PeiVariable: HOB cache Buffer is: 0x%x, number of pages is: 0x%x\n", + VariableRuntimeCacheHob->RuntimeHobCacheBuffer, + VariableRuntimeCacheHob->RuntimeHobCachePages + )); + + return EFI_SUCCESS; +} diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.h b/MdeModulePkg/Universal/Variable/Pei/Variable.h index 51effbf799..aa0d79f166 100644 --- a/MdeModulePkg/Universal/Variable/Pei/Variable.h +++ b/MdeModulePkg/Universal/Variable/Pei/Variable.h @@ -22,11 +22,14 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include +#include #include #include #include #include +#include typedef enum { VariableStoreTypeHob, diff --git a/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf b/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf index 7264a24bdf..f2dc7c042c 100644 --- a/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf +++ b/MdeModulePkg/Universal/Variable/Pei/VariablePei.inf @@ -3,7 +3,7 @@ # # This module implements ReadOnly Variable Services required by PEIM and installs PEI ReadOnly Varaiable2 PPI. # -# Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+# Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause-Patent # ## @@ -41,6 +41,8 @@ PeiServicesLib SafeIntLib VariableFlashInfoLib + MmUnblockMemoryLib + MemoryAllocationLib [Guids] ## CONSUMES ## GUID # Variable store header @@ -56,12 +58,16 @@ ## SOMETIMES_CONSUMES ## HOB ## CONSUMES ## GUID # Dependence gEdkiiFaultTolerantWriteGuid + gEdkiiVariableRuntimeCacheInfoHobGuid [Ppis] gEfiPeiReadOnlyVariable2PpiGuid ## PRODUCES + gEfiPeiMemoryDiscoveredPpiGuid ## CONSUMES [Pcd] gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable ## SOMETIMES_CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize ## CONSUMES + gEfiMdeModulePkgTokenSpaceGuid.PcdEnableVariableRuntimeCache ## CONSUMES [Depex] gEdkiiFaultTolerantWriteGuid -- 2.31.1.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#119022): https://edk2.groups.io/g/devel/message/119022 Mute This Topic: https://groups.io/mt/106150802/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-