From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web11.128.1675798989732216064 for ; Tue, 07 Feb 2023 11:43:10 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=O5IXKLjE; spf=pass (domain: intel.com, ip: 134.134.136.65, mailfrom: chasel.chiu@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675798989; x=1707334989; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=Bekx+st3qqJArsVFnBgyqz8NpSwhVIPl7lr9nbPhGVI=; b=O5IXKLjEhJsbTlpt+wVjzzxFjei/pT2cbun5VTQZCe92F8RPxw6+aKor 2SwBJOx3/gN/coCyhk7cdGsYSuMTW+Rtjc7cbJyuJVSMV3a9cZGgc6FJW 28viqdmBTL0LcjnIadTlCXxx80o6g0nWEQTN1NmTEWWgskQoWAwQS0O1P ElV61LCA0MPire/hAIGUOFCeRM2aG8ZOTls+quBXoBqWvKK1ASWpUTx21 jjvbJsDzXBiq8CDwNzyvP4DWGHmMZbIcqPGiv3dB03YVvc/gS2J8SJ8Qo rK9ntr1pJF30IKJjI1qM/cyM38s1YLs0FvVRQWKoTBtm98L3J7uUAx54X A==; X-IronPort-AV: E=McAfee;i="6500,9779,10614"; a="331740091" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="331740091" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 11:43:09 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10614"; a="735655699" X-IronPort-AV: E=Sophos;i="5.97,278,1669104000"; d="scan'208";a="735655699" Received: from wanggan1-mobl1.amr.corp.intel.com (HELO cchiu4-mobl.gar.corp.intel.com) ([10.212.162.80]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2023 11:43:08 -0800 From: "Chiu, Chasel" To: devel@edk2.groups.io Cc: Chasel Chiu , Ashraf Ali S , Isaac Oram , Rangasai V Chaganty , Ray Ni , Michael Kubacki Subject: [edk2-platforms: PATCH v3] IntelSiliconPkg/SpiFvbServiceSmm: Rewrite VariableStore header. Date: Tue, 7 Feb 2023 11:42:58 -0800 Message-Id: <20230207194258.2598-1-chasel.chiu@intel.com> X-Mailer: git-send-email 2.35.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable When invalid VariableStore FV header detected, current SpiFvbService will erase both FV and VariableStore headers from flash, however, it will only rewrite FV header back and cause invalid VariableStore header. This patch adding the support for rewriting both FV header and VariableStore header when VariableStore corruption happened. Platform has to set PcdFlashVariableStoreType to inform SpiFvbService which VariableStoreType should be rewritten. Cc: Ashraf Ali S Cc: Isaac Oram Cc: Rangasai V Chaganty Cc: Ray Ni Cc: Michael Kubacki Signed-off-by: Chasel Chiu --- Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServiceMm.= c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServiceSmm= .inf | 4 ++++ Silicon/Intel/IntelSiliconPkg/IntelSiliconPkg.dec = | 8 ++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiF= vbServiceMm.c b/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/S= piFvbServiceMm.c index 6b4bcdcfe3..6338442e1a 100644 --- a/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServi= ceMm.c +++ b/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServi= ceMm.c @@ -12,6 +12,7 @@ #include =0D #include =0D #include =0D +#include =0D =0D /**=0D The function installs EFI_FIRMWARE_VOLUME_BLOCK protocol=0D @@ -114,6 +115,10 @@ FvbInitialize ( UINT32 BytesWritten;=0D UINTN BytesErased;=0D UINT64 NvStorageFvSize;=0D + UINT32 ExpectedBytesWritten;=0D + VARIABLE_STORE_HEADER *VariableStoreHeader;=0D + UINT8 VariableStoreType;=0D + UINT8 *NvStoreBuffer;=0D =0D Status =3D GetVariableFlashNvStorageInfo (&BaseAddress, &NvStorageFvSize= );=0D if (EFI_ERROR (Status)) {=0D @@ -186,8 +191,57 @@ FvbInitialize ( }=0D continue;=0D }=0D - BytesWritten =3D FvHeader->HeaderLength;=0D - Status =3D SpiFlashWrite ((UINTN)BaseAddress, &BytesWritten, (UINT= 8*)FvHeader);=0D +=0D + BytesWritten =3D FvHeader->HeaderLength;=0D + ExpectedBytesWritten =3D BytesWritten;=0D + if (Idx !=3D 0) {=0D + Status =3D SpiFlashWrite ((UINTN)BaseAddress, &BytesWritten, (UI= NT8 *)FvHeader);=0D + } else {=0D + //=0D + // This is Variable Store, rewrite both EFI_FIRMWARE_VOLUME_HEAD= ER and VARIABLE_STORE_HEADER=0D + //=0D + NvStoreBuffer =3D NULL;=0D + NvStoreBuffer =3D AllocateZeroPool (sizeof (VARIABLE_STORE_HEADE= R) + FvHeader->HeaderLength);=0D + if (NvStoreBuffer !=3D NULL) {=0D + //=0D + // Combine FV header and VariableStore header into the buffer.= =0D + //=0D + CopyMem (NvStoreBuffer, FvHeader, FvHeader->HeaderLength);=0D + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *)(NvStoreBuffe= r + FvHeader->HeaderLength);=0D + VariableStoreType =3D PcdGet8 (PcdFlashVariableStoreType);=0D + switch (VariableStoreType) {=0D + case 0:=0D + DEBUG ((DEBUG_ERROR, "Type: gEfiVariableGuid\n"));=0D + CopyGuid (&VariableStoreHeader->Signature, &gEfiVariableGu= id);=0D + break;=0D + case 1:=0D + DEBUG ((DEBUG_ERROR, "Type: gEfiAuthenticatedVariableGuid\= n"));=0D + CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthentica= tedVariableGuid);=0D + break;=0D + default:=0D + break;=0D + }=0D +=0D + //=0D + // Initialize common VariableStore header fields=0D + //=0D + VariableStoreHeader->Size =3D PcdGet32 (PcdFlashNvStorage= VariableSize) - FvHeader->HeaderLength;=0D + VariableStoreHeader->Format =3D VARIABLE_STORE_FORMATTED;=0D + VariableStoreHeader->State =3D VARIABLE_STORE_HEALTHY;=0D + VariableStoreHeader->Reserved =3D 0;=0D + VariableStoreHeader->Reserved1 =3D 0;=0D + //=0D + // Write buffer to flash=0D + //=0D + BytesWritten =3D FvHeader->HeaderLength + sizeof (VARI= ABLE_STORE_HEADER);=0D + ExpectedBytesWritten =3D BytesWritten;=0D + Status =3D SpiFlashWrite ((UINTN)BaseAddress, &B= ytesWritten, NvStoreBuffer);=0D + FreePool (NvStoreBuffer);=0D + } else {=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + }=0D + }=0D +=0D if (EFI_ERROR (Status)) {=0D DEBUG ((DEBUG_WARN, "ERROR - SpiFlashWrite Error %r\n", Status)= );=0D if (FvHeader !=3D NULL) {=0D @@ -195,9 +249,9 @@ FvbInitialize ( }=0D continue;=0D }=0D - if (BytesWritten !=3D FvHeader->HeaderLength) {=0D - DEBUG ((DEBUG_WARN, "ERROR - BytesWritten !=3D HeaderLength\n"))= ;=0D - DEBUG ((DEBUG_INFO, " BytesWritten =3D 0x%X\n HeaderLength =3D 0= x%X\n", BytesWritten, FvHeader->HeaderLength));=0D + if (BytesWritten !=3D ExpectedBytesWritten) {=0D + DEBUG ((DEBUG_WARN, "ERROR - BytesWritten !=3D ExpectedBytesWrit= ten\n"));=0D + DEBUG ((DEBUG_INFO, " BytesWritten =3D 0x%X\n ExpectedBytesWritt= en =3D 0x%X\n", BytesWritten, ExpectedBytesWritten));=0D if (FvHeader !=3D NULL) {=0D FreePool (FvHeader);=0D }=0D diff --git a/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiF= vbServiceSmm.inf b/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbServic= e/SpiFvbServiceSmm.inf index 0cfa3f909b..0485b73679 100644 --- a/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServi= ceSmm.inf +++ b/Silicon/Intel/IntelSiliconPkg/Feature/Flash/SpiFvbService/SpiFvbServi= ceSmm.inf @@ -45,6 +45,8 @@ [Pcd]=0D gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvBase ## CONSUM= ES=0D gIntelSiliconPkgTokenSpaceGuid.PcdFlashMicrocodeFvSize ## CONSUM= ES=0D + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize ## SOMETI= MES_CONSUMES=0D + gIntelSiliconPkgTokenSpaceGuid.PcdFlashVariableStoreType ## SOMETI= MES_CONSUMES=0D =0D [Sources]=0D FvbInfo.c=0D @@ -61,6 +63,8 @@ [Guids]=0D gEfiFirmwareFileSystem2Guid ## CONSUMES=0D gEfiSystemNvDataFvGuid ## CONSUMES=0D + gEfiVariableGuid ## SOMETIMES_CONSUMES=0D + gEfiAuthenticatedVariableGuid ## SOMETIMES_CONSUMES=0D =0D [Depex]=0D TRUE=0D diff --git a/Silicon/Intel/IntelSiliconPkg/IntelSiliconPkg.dec b/Silicon/In= tel/IntelSiliconPkg/IntelSiliconPkg.dec index 485cb3e80a..63dae756ad 100644 --- a/Silicon/Intel/IntelSiliconPkg/IntelSiliconPkg.dec +++ b/Silicon/Intel/IntelSiliconPkg/IntelSiliconPkg.dec @@ -186,3 +186,11 @@ # @Prompt VTd abort DMA mode support.=0D gIntelSiliconPkgTokenSpaceGuid.PcdVTdSupportAbortDmaMode|FALSE|BOOLEAN|0= x0000000C=0D =0D + ## Define Flash Variable Store type.

=0D + # When Flash Variable Store corruption happened, the SpiFvbService will= recreate Variable Store=0D + # with valid header information provided by this PCD value.
=0D + # 0: Variable Store is gEfiVariableGuid type.
=0D + # 1: Variable Store is gEfiAuthenticatedVariableGuid type.
=0D + # Other value: reserved for future use.
=0D + # @Prompt Flash Variable Store type.=0D + gIntelSiliconPkgTokenSpaceGuid.PcdFlashVariableStoreType|0x00|UINT8|0x00= 00000E=0D --=20 2.35.0.windows.1