From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web11.729.1649452713502073279 for ; Fri, 08 Apr 2022 14:18:33 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@linux.microsoft.com header.s=default header.b=pJnaPD+w; spf=pass (domain: linux.microsoft.com, ip: 13.77.154.182, mailfrom: mikuback@linux.microsoft.com) Received: from localhost.localdomain (unknown [47.201.46.36]) by linux.microsoft.com (Postfix) with ESMTPSA id 72CAC203A05C; Fri, 8 Apr 2022 14:18:32 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 72CAC203A05C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1649452713; bh=N1dTqpbDQbq2K2pOmDPbDlR4clZiu9yCW86UXKQThWY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pJnaPD+woYM5HGrj/aGFI+kPm3jZptIpx+svggDiJS3At++bu4IPQ9rZnqyVayQet ZUbcBsUWsQ/ntNs+PnE1epYahezQGKplCUTGDgqro1hOmyD6fzXVXruQAHUuWD+2i8 yj5JbtAFr2pTqNVM/je+m5j1WGeEtkl84Xd7rKSY= From: "Michael Kubacki" To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Liming Gao Subject: [PATCH v2 4/8] MdeModulePkg/FaultTolerantWrite: Consume Variable Flash Info Date: Fri, 8 Apr 2022 17:17:29 -0400 Message-Id: <20220408211733.1332-5-mikuback@linux.microsoft.com> X-Mailer: git-send-email 2.28.0.windows.1 In-Reply-To: <20220408211733.1332-1-mikuback@linux.microsoft.com> References: <20220408211733.1332-1-mikuback@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Michael Kubacki REF:https://bugzilla.tianocore.org/show_bug.cgi?id=3D3479 Adds support to the UEFI variable fault tolerant write (FTW) drivers to receive FTW base and size information dynamically via the Variable Flash Information library. Cc: Jian J Wang Cc: Hao A Wu Cc: Liming Gao Signed-off-by: Michael Kubacki --- MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c = | 41 +++++++++++++------- MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c = | 7 +++- MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.c = | 28 ++++++++----- MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h = | 7 +++- MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf = | 10 +---- MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.inf = | 10 +---- MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteStandalon= eMm.inf | 10 +---- MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.inf = | 10 +---- 8 files changed, 63 insertions(+), 60 deletions(-) diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c b/Mde= ModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c index 661e1487673b..f1335870e797 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FtwMisc.c @@ -987,22 +987,43 @@ InitFtwDevice ( OUT EFI_FTW_DEVICE **FtwData ) { - EFI_FTW_DEVICE *FtwDevice; + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS WorkSpaceAddress; + UINT64 Size; + UINTN FtwWorkingSize; + EFI_FTW_DEVICE *FtwDevice; + + FtwWorkingSize =3D 0; + + Status =3D GetVariableFlashFtwWorkingInfo (&WorkSpaceAddress, &Size); + ASSERT_EFI_ERROR (Status); + + Status =3D SafeUint64ToUintn (Size, &FtwWorkingSize); + // This driver currently assumes the size will be UINTN so assert the = value is safe for now. + ASSERT_EFI_ERROR (Status); =20 // // Allocate private data of this driver, // Including the FtwWorkSpace[FTW_WORK_SPACE_SIZE]. // - FtwDevice =3D AllocateZeroPool (sizeof (EFI_FTW_DEVICE) + PcdGet32 (Pc= dFlashNvStorageFtwWorkingSize)); + FtwDevice =3D AllocateZeroPool (sizeof (EFI_FTW_DEVICE) + FtwWorkingSi= ze); if (FtwDevice =3D=3D NULL) { return EFI_OUT_OF_RESOURCES; } =20 + FtwDevice->WorkSpaceAddress =3D WorkSpaceAddress; + FtwDevice->WorkSpaceLength =3D FtwWorkingSize; + + Status =3D GetVariableFlashFtwSpareInfo (&FtwDevice->SpareAreaAddress,= &Size); + ASSERT_EFI_ERROR (Status); + + Status =3D SafeUint64ToUintn (Size, &FtwDevice->SpareAreaLength); + // This driver currently assumes the size will be UINTN so assert the = value is safe for now. + ASSERT_EFI_ERROR (Status); + // // Initialize other parameters, and set WorkSpace as FTW_ERASED_BYTE. // - FtwDevice->WorkSpaceLength =3D (UINTN)PcdGet32 (PcdFlashNvStorageFtwWo= rkingSize); - FtwDevice->SpareAreaLength =3D (UINTN)PcdGet32 (PcdFlashNvStorageFtwSp= areSize); if ((FtwDevice->WorkSpaceLength =3D=3D 0) || (FtwDevice->SpareAreaLeng= th =3D=3D 0)) { DEBUG ((DEBUG_ERROR, "Ftw: Workspace or Spare block does not exist!\= n")); FreePool (FtwDevice); @@ -1015,16 +1036,6 @@ InitFtwDevice ( FtwDevice->FtwWorkSpaceLba =3D (EFI_LBA)(-1); FtwDevice->FtwSpareLba =3D (EFI_LBA)(-1); =20 - FtwDevice->WorkSpaceAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFla= shNvStorageFtwWorkingBase64); - if (FtwDevice->WorkSpaceAddress =3D=3D 0) { - FtwDevice->WorkSpaceAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdF= lashNvStorageFtwWorkingBase); - } - - FtwDevice->SpareAreaAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFla= shNvStorageFtwSpareBase64); - if (FtwDevice->SpareAreaAddress =3D=3D 0) { - FtwDevice->SpareAreaAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdF= lashNvStorageFtwSpareBase); - } - *FtwData =3D FtwDevice; return EFI_SUCCESS; } @@ -1277,7 +1288,7 @@ InitFtwProtocol ( FtwDevice->FtwLastWriteHeader =3D NULL; FtwDevice->FtwLastWriteRecord =3D NULL; =20 - InitializeLocalWorkSpaceHeader (); + InitializeLocalWorkSpaceHeader (FtwDevice->WorkSpaceLength); =20 // // Refresh the working space data from working block diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBl= ock.c b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c index 61e7a92ccea1..fd563643eb63 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/UpdateWorkingBlock.c @@ -16,10 +16,13 @@ EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER mWorkingBloc= kHeader =3D { ZERO_GUID, 0, 0 =20 Since Signature and WriteQueueSize have been known, Crc can be calcula= ted out, then the work space header will be fixed. + + @param[in] WorkSpaceLength Length in bytes of the FTW workspace a= rea. + **/ VOID InitializeLocalWorkSpaceHeader ( - VOID + IN UINTN WorkSpaceLength ) { // @@ -46,7 +49,7 @@ InitializeLocalWorkSpaceHeader ( &gEdkiiWorkingBlockSignatureGuid, sizeof (EFI_GUID) ); - mWorkingBlockHeader.WriteQueueSize =3D PcdGet32 (PcdFlashNvStorageFtwW= orkingSize) - sizeof (EFI_FAULT_TOLERANT_WORKING_BLOCK_HEADER); + mWorkingBlockHeader.WriteQueueSize =3D WorkSpaceLength - sizeof (EFI_F= AULT_TOLERANT_WORKING_BLOCK_HEADER); =20 // // Crc is calculated with all the fields except Crc and STATE, so leav= e them as FTW_ERASED_BYTE. diff --git a/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWr= itePei.c b/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWrit= ePei.c index 15543f12ed29..8c152dcbad98 100644 --- a/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.= c +++ b/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.= c @@ -16,6 +16,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include +#include =20 EFI_PEI_PPI_DESCRIPTOR mPpiListVariable =3D { (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST), @@ -212,25 +214,31 @@ PeimFaultTolerantWriteInitialize ( EFI_PHYSICAL_ADDRESS SpareAreaAddress; UINTN SpareAreaLength; EFI_PHYSICAL_ADDRESS WorkSpaceInSpareArea; + UINT64 Size; FAULT_TOLERANT_WRITE_LAST_WRITE_DATA FtwLastWrite; =20 FtwWorkingBlockHeader =3D NULL; FtwLastWriteHeader =3D NULL; FtwLastWriteRecord =3D NULL; =20 - WorkSpaceAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFlashNvStorage= FtwWorkingBase64); - if (WorkSpaceAddress =3D=3D 0) { - WorkSpaceAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFlashNvStora= geFtwWorkingBase); - } + SpareAreaAddress =3D 0; + SpareAreaLength =3D 0; + WorkSpaceAddress =3D 0; + WorkSpaceLength =3D 0; =20 - WorkSpaceLength =3D (UINTN)PcdGet32 (PcdFlashNvStorageFtwWorkingSize); + Status =3D GetVariableFlashFtwWorkingInfo (&WorkSpaceAddress, &Size); + ASSERT_EFI_ERROR (Status); =20 - SpareAreaAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet64 (PcdFlashNvStorage= FtwSpareBase64); - if (SpareAreaAddress =3D=3D 0) { - SpareAreaAddress =3D (EFI_PHYSICAL_ADDRESS)PcdGet32 (PcdFlashNvStora= geFtwSpareBase); - } + Status =3D SafeUint64ToUintn (Size, &WorkSpaceLength); + // This driver currently assumes the size will be UINTN so assert the = value is safe for now. + ASSERT_EFI_ERROR (Status); =20 - SpareAreaLength =3D (UINTN)PcdGet32 (PcdFlashNvStorageFtwSpareSize); + Status =3D GetVariableFlashFtwSpareInfo (&SpareAreaAddress, &Size); + ASSERT_EFI_ERROR (Status); + + Status =3D SafeUint64ToUintn (Size, &SpareAreaLength); + // This driver currently assumes the size will be UINTN so assert the = value is safe for now. + ASSERT_EFI_ERROR (Status); =20 // // The address of FTW working base and spare base must not be 0. diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= ite.h b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h index c14e47b5c7b2..5b84d062c294 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWrite.h @@ -26,6 +26,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include +#include =20 // // Flash erase polarity is 1 @@ -708,10 +710,13 @@ InitFtwProtocol ( =20 Since Signature and WriteQueueSize have been known, Crc can be calcula= ted out, then the work space header will be fixed. + + @param[in] WorkSpaceLength Length in bytes of the FTW workspace a= rea. + **/ VOID InitializeLocalWorkSpaceHeader ( - VOID + IN UINTN WorkSpaceLength ); =20 /** diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= iteDxe.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= iteDxe.inf index 96165614d178..d524e1849a2e 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.= inf +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.= inf @@ -46,6 +46,8 @@ [LibraryClasses] UefiLib PcdLib ReportStatusCodeLib + SafeIntLib + VariableFlashInfoLib =20 [Guids] # @@ -65,14 +67,6 @@ [Protocols] [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdFullFtwServiceEnable ## CONSUMES =20 -[Pcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## C= ONSUMES - # # gBS->CalculateCrc32() is consumed in EntryPoint. # PI spec said: When the DXE Foundation is notified that the EFI_RUNTIME= _ARCH_PROTOCOL diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= iteSmm.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= iteSmm.inf index 8cc6028470d8..8a4b9ad24657 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.= inf +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteSmm.= inf @@ -52,6 +52,8 @@ [LibraryClasses] ReportStatusCodeLib SmmMemLib BaseLib + SafeIntLib + VariableFlashInfoLib =20 [Guids] # @@ -74,14 +76,6 @@ [Protocols] [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdFullFtwServiceEnable ## CONSUMES =20 -[Pcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## C= ONSUMES - # # gBS->CalculateCrc32() is consumed in EntryPoint. # PI spec said: When the DXE Foundation is notified that the EFI_RUNTIME= _ARCH_PROTOCOL diff --git a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWr= iteStandaloneMm.inf b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultT= olerantWriteStandaloneMm.inf index d0fab7d9414f..0ac6edf771ab 100644 --- a/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteStan= daloneMm.inf +++ b/MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteStan= daloneMm.inf @@ -50,7 +50,9 @@ [LibraryClasses] MmServicesTableLib PcdLib ReportStatusCodeLib + SafeIntLib StandaloneMmDriverEntryPoint + VariableFlashInfoLib =20 [Guids] # @@ -73,13 +75,5 @@ [Protocols] [FeaturePcd] gEfiMdeModulePkgTokenSpaceGuid.PcdFullFtwServiceEnable ## CONSUMES =20 -[Pcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## C= ONSUMES - [Depex] TRUE diff --git a/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWr= itePei.inf b/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWr= itePei.inf index f90892ad4493..230138272c3a 100644 --- a/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.= inf +++ b/MdeModulePkg/Universal/FaultTolerantWritePei/FaultTolerantWritePei.= inf @@ -39,6 +39,8 @@ [LibraryClasses] HobLib BaseMemoryLib PcdLib + SafeIntLib + VariableFlashInfoLib =20 [Guids] ## SOMETIMES_PRODUCES ## HOB @@ -47,14 +49,6 @@ [Guids] gEdkiiWorkingBlockSignatureGuid ## SOMETIMES_CONSUMES = ## GUID gEfiSystemNvDataFvGuid ## SOMETIMES_CONSUMES = ## GUID =20 -[Pcd] - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase ## S= OMETIMES_CONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase64 ## C= ONSUMES - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize ## C= ONSUMES - [Depex] TRUE =20 --=20 2.28.0.windows.1