From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by mx.groups.io with SMTP id smtpd.web11.1671.1684890458458150063 for ; Tue, 23 May 2023 18:07:38 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=n23bSMyI; spf=pass (domain: intel.com, ip: 192.55.52.151, 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=1684890458; x=1716426458; h=from:to:cc:subject:date:message-id:references: in-reply-to:mime-version; bh=rDW5Z1zMoTPAFYBFg7mNuGwYdv0RniaIqXZ7HozInsI=; b=n23bSMyI2spP2i7QVeZzjmBKYLRnX8OczP8Wk2xWjnzVmMlLvA5QgHB4 1UYvecLUu1zR5wlva7mwphJaX3DNXBwdWXujEfm4grQG6a8oru899W60i ClAXeOhZJ4zsQq1hm97nxUVmjZz0uM0i9TBSozd47Nn1FsBm56z/PhYor 6JuukGFhlssPbGkouCDIeUL9gATa/ydB96NZExPUuZinoWOKwVDvzT9Op k7mzth4Drut1Lxo05hFoZLI2ZFzq8TKgmmwlMipwh7sozOpqSOfPqyHfd QM09EBRBCITv3N9VoBS+9amAKH8rI0o/Tf6NCQPNEXhptjDnybyrxGkdB w==; X-IronPort-AV: E=McAfee;i="6600,9927,10719"; a="333776389" X-IronPort-AV: E=Sophos;i="6.00,187,1681196400"; d="scan'208,217";a="333776389" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2023 18:07:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10719"; a="1034278083" X-IronPort-AV: E=Sophos;i="6.00,187,1681196400"; d="scan'208,217";a="1034278083" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga005.fm.intel.com with ESMTP; 23 May 2023 18:07:37 -0700 Received: from orsmsx603.amr.corp.intel.com (10.22.229.16) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23; Tue, 23 May 2023 18:07:37 -0700 Received: from orsedg603.ED.cps.intel.com (10.7.248.4) by orsmsx603.amr.corp.intel.com (10.22.229.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.23 via Frontend Transport; Tue, 23 May 2023 18:07:37 -0700 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (104.47.74.49) by edgegateway.intel.com (134.134.137.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.23; Tue, 23 May 2023 18:07:37 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YOOFWLhevkac3b2Y9i0ENqWfuBFZZFugKPIZqftpcW8g/eduo1WSOo2YOhyUXa6nXcZOwQGeRWRQXQuOPIcL8mjZVGieKtI/FlTMeM71JR0GInmR/35h2aO8s3DJWFdFJ7usxokfQJZ/U8vaH+1uz5GaboVfzO6Xnis+EHM0+JLhqXvltI0s7CMsWMtMZ/luN9rzyCFjVVJZ7yZcU7uGNev+7Ei+6rUjRHN2M3yeCaAn4aYghY6IGt5finF19rDhP3Wj+3MDX6xYai+wLmeTYpUy0yfGtZawa8G1M324Q92+odoWfsVHFVCAzXzhqpfSnnoWouxdaogTATr7ieoa3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ofbNIlD3ysj8YlXYonebrO3L6jzU/RI/qoPVD6c84TQ=; b=bAbzSw3IwFR7N6I7IxWookpOPfeWrlPniWpZ8VVLjF2D9lqaNytLdoLRBJtPYS5O9i9aWT046dsz+sX+NOOK3JIrb6FEklm2Zn0wOazMb8xTQ7rzbR/UJd+GHsisiubDh8NfpuLjha9fE8x7zQlmsQnGZsIZup1Bt81Np/hfa9OMf8gFAhl1S5+t3M4jxrJNCYfUVoQRXvSkUQ4hPMli36yNG7FiPupoBCQCfKDgVqp1R918nYbc0GyvCi8pwFWB+Kb6ZLvG5YbEsYR3uYjKKqRYjDUNsam0R95LNTeC8432mQMqQuu2afzKLY3XQ783sMiN9LiNgUndZLKkWGjkLA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Received: from BN9PR11MB5483.namprd11.prod.outlook.com (2603:10b6:408:104::10) by SJ0PR11MB4845.namprd11.prod.outlook.com (2603:10b6:a03:2d1::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6411.28; Wed, 24 May 2023 01:07:34 +0000 Received: from BN9PR11MB5483.namprd11.prod.outlook.com ([fe80::1eb1:2222:1823:8e7a]) by BN9PR11MB5483.namprd11.prod.outlook.com ([fe80::1eb1:2222:1823:8e7a%5]) with mapi id 15.20.6411.028; Wed, 24 May 2023 01:07:34 +0000 From: "Chiu, Chasel" To: "Desimone, Nathaniel L" , "Shindo, Miki" , "devel@edk2.groups.io" CC: "Gao, Liming" , "Dong, Eric" , "Zhang, Xiaoqiang" Subject: Re: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVariable fail issue Thread-Topic: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVariable fail issue Thread-Index: AQHZjdjfxd4vVvDa/UWwVxFGjY/vba9omgsAgAACVQA= Date: Wed, 24 May 2023 01:07:34 +0000 Message-ID: References: <20230524004403.3338-1-miki.shindo@intel.com> In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: BN9PR11MB5483:EE_|SJ0PR11MB4845:EE_ x-ms-office365-filtering-correlation-id: 949ea93e-1a66-4a8e-3e50-08db5bf341bd x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: SpzG0lPMQkhFKBPQI7h6qej2pJ2pPBm0DXCaaxp/8wiRFu2frHdNWejRExAt9YrZ9yJuvpty+yjs1Xgp+GUssbNcNz1BM/2UAbUACHYLwEI5po/JQA0VFKEiXhxkyra45uRuE//jwQs7b6H/BLBri7XMcC5sTZRSJtCLSVAE8oqZduPvUrWvEhBK0REgtfgsIgdk4RGTaVZIyuJwupaUH2qMJUg/0KCkqKeHEbfiGERoD6/t+Hj2t4TinfrGW+U+myfUzDul4KKTFSa0BDqnvW2/eMg+HEEdGq3VxEZ/xmoEZso4Vq4grwcRtoi4BzskbKiFftkmrWkhhFQNrT2uctR9AGNxjytAH3juMwcCLR7xoRmdz0Fz/IWcKAekOIk0DEcBQw/hEKGYjf1oepkALfosSgLPYgSo8t2wBDke0tj89aa6sjZ8ZZ1U3GXW8lCvyHhnKAAH4w0rLfc4YA/AHM3XEuMMxUnALuhnktvUOQMdJdNI9aQNRhH47sHwdvifvm0ZFSe0PJY8ERAgZ0PgYbvLA8mgJn4v3ycYNvcW1M1McVUWAQ6bSSokwAtzZzSbaxbAwKW5mmz0RmeKXq8KsBaGX3k/mG5FCBEj1P4AVyw= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BN9PR11MB5483.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(396003)(376002)(366004)(39860400002)(136003)(346002)(451199021)(7696005)(54906003)(966005)(41300700001)(478600001)(110136005)(4326008)(316002)(66556008)(166002)(66446008)(66476007)(66946007)(71200400001)(64756008)(76116006)(8936002)(52536014)(5660300002)(38070700005)(86362001)(8676002)(107886003)(38100700002)(82960400001)(122000001)(6506007)(26005)(83380400001)(9686003)(53546011)(55016003)(30864003)(2906002)(33656002)(186003);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?Ylpl0wOfFfDtuEU6rJIo0eGXsb6iUa6DS5L/0B6QEhqvC1iNRpVlIgJpUZWV?= =?us-ascii?Q?LKD8E5erUNbnpKdZUEwNm6i674AgtfCkeqE+Q2KemaUq2G3Lz3Nv8x+L8v4r?= =?us-ascii?Q?q4tDk/YRu9kJcxDdk6a8/5fF9J4yzXO+UatjNMZntKXO5S89zz+qsrdDxjMs?= =?us-ascii?Q?eCOXs7piyYSFBztpl9Ea86tJs0QMw5MKfTVkE3v+5C+UL1xJ5XlJSSRPgPGS?= =?us-ascii?Q?UFBfS7i+xtOnF6k7pHzcnS9/TrMq8XUFNmfFuAwx+/sTcoZQUPMCENI3E0dC?= =?us-ascii?Q?v7AWHVEmHis08rZerMiJM6uXlrgN9jYENaKWBhG8DOD2bln2iLDNr27gLTZz?= =?us-ascii?Q?tU1HznuDsldokQD3Rt2MUYahA55y15DvyoLS3+j5K+toYo9/DEzbyQerqWMN?= =?us-ascii?Q?X2jatV/ZtdYGlUuM6XYzWEZfqtJqNDKumcFhImBe+imNoH0c134sewBsUFWj?= =?us-ascii?Q?UND2qrOP1hX1fq7RTR0NKy0unDMDmvjQdbU6TGFM9y/gl0fbCce58AfMg9rV?= =?us-ascii?Q?I9CPMxrk9wDxldiFCDbecpgew6mro6AVWkk+VF+yRPWbsKA69aTdcKEY6aEk?= =?us-ascii?Q?k5qG2NaW/J4D/uui1onE7G08rInWwZ92nyLlnlQWfMxztPSoWOcplgLzi53l?= =?us-ascii?Q?PMt7NpH4NH/qQrjIMs5KdX3VngrLsNkzMNmQSwICZSHO3HJiUhor97VksF7c?= =?us-ascii?Q?nhQJGHBr7xV35YCOzXAcvRHh/ADwZoSBWlsiDKCvfFgA5uXzusSHRZxOevh1?= =?us-ascii?Q?u6h3VePNxOWLUaXd2VTPDUPPThw23SHqlNKvIWnLQwXtEYvGsoaxZs43uIXB?= =?us-ascii?Q?NFWSz8dsO52kQ2KW9eVOXp9FWgWnQraCd5M1qtWGpWKkeUmhukWt1AlYuRLc?= =?us-ascii?Q?Ihk+xFMrny+s8BOzluAg0rLZb/41zz4wsGn/LTeaqMkYMrJ+LtrIHZIYo7xd?= =?us-ascii?Q?sSoO5pGj/CyduMSKOOERU1tH6DfNLv47RP5iZHahS2FMVnMDvvohlvlzSesy?= =?us-ascii?Q?tcreBVS9NMkiwHiPuFsQY46yCqqF216oGBEYTCt+PyjC8YcbcXwPkNyAJmFI?= =?us-ascii?Q?LvRVQfd2Y470T+42D8pt7GqPTHamkgBOHgQWk++TmNrFYIhPb3NlNgcdhvkv?= =?us-ascii?Q?3bY1STLa5UE3BGQU8MzOOeRw7tiUzmZe+f0EIwguzMmC9A+8qEab43W3OuNr?= =?us-ascii?Q?wCUlkJeOATK0ejDBDKmMxCj0uTwo3NcpRBfjxkZQW4t6GSmFhm9ziYcajAkF?= =?us-ascii?Q?aaMrBmafo3tohdpZhfRymSkhgdOvqPa6Eikpp8Iurpq/Uug3pSbSHRJp9pZ1?= =?us-ascii?Q?4O5pyM7u8T0kZklWnP0ORwmIqsO4VxAFQrYvn3JwC7EK5o7KuDymq9S7fBPZ?= =?us-ascii?Q?/iR0b4Nvr6Za9oCujbf6W18DXKyjCgYIIOH5wv07Q+r2R50+DxfafEzYkcOe?= =?us-ascii?Q?DnDj/8LAvlrpnBunF02Qrsi77PnyELj23sMGpNuSQxH5uXWj3ghZEyqTF/yi?= =?us-ascii?Q?/Gxju9HT5aGAp9UyJh0rLKN6+8FzUB1/dBqkKSFoFaKHaw5X3tYLS23N+318?= =?us-ascii?Q?dziwC/7OJ9EmlFq9VdY2x9MfFiA51u+zE65RK+is?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BN9PR11MB5483.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 949ea93e-1a66-4a8e-3e50-08db5bf341bd X-MS-Exchange-CrossTenant-originalarrivaltime: 24 May 2023 01:07:34.0923 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: jsBlTul65RIQZZBImFfFmiQIZM4hCIHtECcSujBXFcNory7Tu1T6hPqRPgtHSwNp4+CUEKh0r6fRxNKtxBCJOA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR11MB4845 Return-Path: chasel.chiu@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_BN9PR11MB548369EC3AB41503E4C6B3E8E6419BN9PR11MB5483namp_" --_000_BN9PR11MB548369EC3AB41503E4C6B3E8E6419BN9PR11MB5483namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Chasel Chiu > Thanks, Chasel From: Desimone, Nathaniel L Sent: Tuesday, May 23, 2023 5:59 PM To: Shindo, Miki ; devel@edk2.groups.io Cc: Chiu, Chasel ; Gao, Liming ; Dong, Eric ; Zhang, Xiaoqiang Subject: Re: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVariable= fail issue Thank you, Miki, and Xiaoqiang! Reviewed-by: Nate DeSimone > From: Shindo, Miki > Date: Tuesday, May 23, 2023 at 5:44 PM To: devel@edk2.groups.io > Cc: Chiu, Chasel >, Des= imone, Nathaniel L >, Gao, Liming >, Dong, Eric >= , Zhang, Xiaoqiang > Subject: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVariable fai= l issue REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4454 On Server platform, when the large variable "FspNvsBuffer" is already in th= e UEFI variable store and the remaining variable storage space is less than= the large variable size, and also not in OS runtime, then we need to add t= he size of the current data that will end up being replaced by the new data= to the remaining space before we decide that there is not enough space to = store the large variable. Cc: Chasel Chiu > Cc: Nate DeSimone > Cc: Liming Gao > Cc: Eric Dong > Co-authored-by: Xiaoqiang Zhang > Signed-off-by: Miki Shindo > --- Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVariableWr= iteLib.c | 10 +++++++++- Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeRuntim= eVariableWriteLib.c | 15 +++++++++++++++ Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariableWrite= Common.c | 16 ++++++++++++++++ Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneMmVari= ableWriteLibConstructor.c | 30 ++++++++++++++++++++++++++++++ Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/TraditionalMmVar= iableWriteLibConstructor.c | 30 ++++++++++++++++++++++++++++++ Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h = | 12 ++++++++++++ Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeVariab= leWriteLib.inf | 1 + Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneMmVari= ableWriteLib.inf | 3 ++- Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/TraditionalMmVar= iableWriteLib.inf | 3 ++- 9 files changed, 117 insertions(+), 3 deletions(-) diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/Lar= geVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVaria= bleLib/LargeVariableWriteLib.c index de23ae6160..4bf9a6994f 100644 --- a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVaria= bleWriteLib.c +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVaria= bleWriteLib.c @@ -22,7 +22,7 @@ #include #include #include - +#include #include "LargeVariableCommon.h" /** @@ -270,6 +270,7 @@ SetLargeVariable ( UINT8 *OffsetPtr; UINTN BytesRemaining; UINTN SizeToSave; + UINTN BufferSize =3D 0; // // Check input parameters. @@ -365,6 +366,13 @@ SetLargeVariable ( // Non-Volatile storage to store the data. // RemainingVariableStorage =3D GetRemainingVariableStorageSpace (); + // + // Check if current variable already existed in NV storage variable sp= ace + // + Status =3D GetLargeVariable (VariableName, VendorGuid, &BufferSize, NU= LL); + if ((Status =3D=3D EFI_BUFFER_TOO_SMALL) && (BufferSize !=3D 0) && !Va= rLibAtOsRuntime ()) { + RemainingVariableStorage =3D RemainingVariableStorage + BufferSize; + } if (DataSize > RemainingVariableStorage) { DEBUG ((DEBUG_ERROR, "SetLargeVariable: Not enough NV storage space = to store the data\n")); Status =3D EFI_OUT_OF_RESOURCES; diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteL= ib/DxeRuntimeVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/Dxe= RuntimeVariableWriteLib/DxeRuntimeVariableWriteLib.c index 28730f858b..9ca4734f24 100644 --- a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c +++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c @@ -195,6 +195,21 @@ VarLibVariableRequestToLock ( return Status; } +/** + Indicator of whether it is runtime or not. + + @retval TRUE It is Runtime. + @retval FALSE It is not Runtime. +**/ +BOOLEAN +EFIAPI +VarLibAtOsRuntime ( + VOID + ) +{ + return (mVariableWriteLibVariablePolicy =3D=3D NULL) ? TRUE : FALSE; +} + /** Close events when driver unloaded. diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmV= ariableWriteCommon.c b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWri= teLib/SmmVariableWriteCommon.c index 50ebb544b8..cd7118d1fb 100644 --- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c @@ -18,6 +18,7 @@ #include EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable =3D NULL; +BOOLEAN mEfiAtRuntime =3D FALSE; /** Sets the value of a variable. @@ -169,3 +170,18 @@ VarLibVariableRequestToLock ( // return EFI_UNSUPPORTED; } + +/** + Indicator of whether it is runtime or not. + + @retval TRUE It is Runtime. + @retval FALSE It is not Runtime. +**/ +BOOLEAN +EFIAPI +VarLibAtOsRuntime ( + VOID + ) +{ + return mEfiAtRuntime; +} diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libra= ry/SmmVariableWriteLib/StandaloneMmVariableWriteLibConstructor.c index d39418abd2..8c2b7d18f5 100644 --- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c @@ -20,6 +20,28 @@ #include extern EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable; +extern BOOLEAN mEfiAtRuntime; + +/** + Callback for ExitBootService, which is registered at the constructor. + This callback sets a global variable mEfiAtRuntime to indicate whether + it is after ExitBootService. + + @param[in] Protocol Protocol unique ID. + @param[in] Interface Interface instance. + @param[in] Handle The handle on which the interface is installe= d. +**/ +EFI_STATUS +EFIAPI +VarLibExitBootServicesCallback ( + IN CONST EFI_GUID *Protocol, + IN VOID *Interface, + IN EFI_HANDLE Handle + ) +{ + mEfiAtRuntime =3D TRUE; + return EFI_SUCCESS; +} /** The constructor function acquires the EFI SMM Variable Services @@ -41,11 +63,19 @@ StandaloneMmVariableWriteLibConstructor ( ) { EFI_STATUS Status; + VOID *Registration =3D NULL; // // Locate SmmVariableProtocol. // Status =3D gMmst->MmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL, = (VOID **) &mVariableWriteLibSmmVariable); ASSERT_EFI_ERROR (Status); + + // + // Register VarLibExitBootServicesCallback for gEdkiiSmmExitBootServices= ProtocolGuid. + // + Status =3D SmmRegisterProtocolNotify (&gEdkiiSmmExitBootServicesProtocol= Guid, VarLibExitBootServicesCallback, &Registration); + ASSERT_EFI_ERROR (Status); + return Status; } diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libr= ary/SmmVariableWriteLib/TraditionalMmVariableWriteLibConstructor.c index d142527e17..abc1e25cde 100644 --- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c @@ -20,6 +20,28 @@ #include extern EFI_SMM_VARIABLE_PROTOCOL *mVariableWriteLibSmmVariable; +extern BOOLEAN mEfiAtRuntime; + +/** + Callback for ExitBootService, which is registered at the constructor. + This callback sets a global variable mEfiAtRuntime to indicate whether + it is after ExitBootService. + + @param[in] Protocol Protocol unique ID. + @param[in] Interface Interface instance. + @param[in] Handle The handle on which the interface is installe= d. +**/ +EFI_STATUS +EFIAPI +VarLibExitBootServicesCallback ( + IN CONST EFI_GUID *Protocol, + IN VOID *Interface, + IN EFI_HANDLE Handle + ) +{ + mEfiAtRuntime =3D TRUE; + return EFI_SUCCESS; +} /** The constructor function acquires the EFI SMM Variable Services @@ -41,11 +63,19 @@ TraditionalMmVariableWriteLibConstructor ( ) { EFI_STATUS Status; + VOID *Registration =3D NULL; // // Locate SmmVariableProtocol. // Status =3D gSmst->SmmLocateProtocol (&gEfiSmmVariableProtocolGuid, NULL,= (VOID **) &mVariableWriteLibSmmVariable); ASSERT_EFI_ERROR (Status); + + // + // Register VarLibExitBootServicesCallback for gEdkiiSmmExitBootServices= ProtocolGuid. + // + Status =3D SmmRegisterProtocolNotify (&gEdkiiSmmExitBootServicesProtocol= Guid, VarLibExitBootServicesCallback, &Registration); + ASSERT_EFI_ERROR (Status); + return Status; } diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib= .h b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h index fab87f2e48..bc0b52d782 100644 --- a/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h +++ b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h @@ -135,4 +135,16 @@ VarLibVariableRequestToLock ( IN EFI_GUID *VendorGuid ); +/** + Indicator of whether it is runtime or not. + + @retval TRUE It is Runtime. + @retval FALSE It is not Runtime. +**/ +BOOLEAN +EFIAPI +VarLibAtOsRuntime ( + VOID + ); + #endif // _VARIABLE_WRITE_LIB_H_ diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/Bas= eLargeVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/BaseLarg= eVariableLib/BaseLargeVariableWriteLib.inf index 2493a94596..cbc2a5d93a 100644 --- a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeV= ariableWriteLib.inf +++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeV= ariableWriteLib.inf @@ -49,3 +49,4 @@ PrintLib VariableReadLib VariableWriteLib + LargeVariableReadLib diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVar= iableWriteLib/StandaloneMmVariableWriteLib.inf index 0d1c63a297..868be49630 100644 --- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf @@ -39,7 +39,8 @@ MmServicesTableLib [Protocols] - gEfiSmmVariableProtocolGuid ## CONSUMES + gEfiSmmVariableProtocolGuid ## CONSUMES + gEdkiiSmmExitBootServicesProtocolGuid ## CONSUMES [Depex] gEfiSmmVariableProtocolGuid diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVa= riableWriteLib/TraditionalMmVariableWriteLib.inf index 5d833b7e0f..4aaab069ab 100644 --- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf +++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf @@ -38,7 +38,8 @@ SmmServicesTableLib [Protocols] - gEfiSmmVariableProtocolGuid ## CONSUMES + gEfiSmmVariableProtocolGuid ## CONSUMES + gEdkiiSmmExitBootServicesProtocolGuid ## CONSUMES [Depex] gEfiSmmVariableProtocolGuid -- 2.39.1.windows.1 --_000_BN9PR11MB548369EC3AB41503E4C6B3E8E6419BN9PR11MB5483namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

 

Reviewed-by= : Chasel Chiu <chasel.chiu@intel.com>

 

Thanks,<= /span>

Chasel

 

 

From:= Desimone, Nathaniel L <nathaniel.l.des= imone@intel.com>
Sent: Tuesday, May 23, 2023 5:59 PM
To: Shindo, Miki <miki.shindo@intel.com>; devel@edk2.groups.io=
Cc: Chiu, Chasel <chasel.chiu@intel.com>; Gao, Liming <gaol= iming@byosoft.com.cn>; Dong, Eric <eric.dong@intel.com>; Zhang, Xi= aoqiang <xiaoqiang.zhang@intel.com>
Subject: Re: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeV= ariable fail issue

 

Thank you, Miki, an= d Xiaoqiang!

 

Reviewed-by: Nate D= eSimone <nathaniel.l.d= esimone@intel.com>

 

From: Shindo, Miki <miki.shindo@intel.com>
Date: Tuesday, May 23, 2023 at 5:44 PM
To: devel@edk2.groups.io= <devel@edk2.groups.io> Cc: Chiu, Chasel <chasel= .chiu@intel.com>, Desimone, Nathaniel L <nathaniel.l.desimone@intel.com>, Gao, Limi= ng <gaoliming@byosoft.com.cn= >, Dong, Eric <eric.dong@intel.com<= /a>>, Zhang, Xiaoqiang <= xiaoqiang.zhang@intel.com>
Subject: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVaria= ble fail issue

REF: https://b= ugzilla.tianocore.org/show_bug.cgi?id=3D4454

On Server platform, when the large variable "FspNvsBuffer" is alr= eady in the UEFI variable store and the remaining variable storage space is= less than the large variable size, and also not in OS runtime, then we nee= d to add the size of the current data that will end up being replaced by the new data to the remaining space before w= e decide that there is not enough space to store the large variable.

Cc: Chasel Chiu <chasel.chiu@in= tel.com>
Cc: Nate DeSimone <nat= haniel.l.desimone@intel.com>
Cc: Liming Gao <gaoliming@by= osoft.com.cn>
Cc: Eric Dong <eric.dong@intel.co= m>
Co-authored-by: Xiaoqiang Zhang <xiaoqiang.zhang@intel.com>
Signed-off-by: Miki Shindo <mik= i.shindo@intel.com>
---
 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVaria= bleWriteLib.c          &n= bsp;        | 10 +++++++++-
 Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c        | 15 +++= ++++++++++++
 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c          &n= bsp;        | 16 ++++++++++++++++
 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c  | 30 ++++++++++++++++++++++++++++++  Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c | 30 ++++++++++++++++++++++++++++++
 Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h = ;            &n= bsp;            = ;           | 12 ++++++++= ++++
 Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeV= ariableWriteLib.inf         &n= bsp;   |  1 +
 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf         =   |  3 ++-
 Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf         = ; |  3 ++-
 9 files changed, 117 insertions(+), 3 deletions(-)

diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/Lar= geVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVaria= bleLib/LargeVariableWriteLib.c
index de23ae6160..4bf9a6994f 100644
--- a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVaria= bleWriteLib.c
+++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/LargeVaria= bleWriteLib.c
@@ -22,7 +22,7 @@
 #include <Library/PrintLib.h>

 #include <Library/VariableReadLib.h>

 #include <Library/VariableWriteLib.h>

-

+#include <Library/LargeVariableReadLib.h>

 #include "LargeVariableCommon.h"

 

 /**

@@ -270,6 +270,7 @@ SetLargeVariable (
   UINT8         *OffsetP= tr;

   UINTN         BytesRem= aining;

   UINTN         SizeToSa= ve;

+  UINTN         BufferSize = =3D 0;

 

   //

   // Check input parameters.

@@ -365,6 +366,13 @@ SetLargeVariable (
     // Non-Volatile storage to store the data.

     //

     RemainingVariableStorage =3D GetRemainingVariableS= torageSpace ();

+    //

+    // Check if current variable already existed in NV stor= age variable space

+    //

+    Status =3D GetLargeVariable (VariableName, VendorGuid, = &BufferSize, NULL);

+    if ((Status =3D=3D EFI_BUFFER_TOO_SMALL) && (Bu= fferSize !=3D 0) && !VarLibAtOsRuntime ()) {

+      RemainingVariableStorage =3D RemainingVaria= bleStorage + BufferSize;

+    }

     if (DataSize > RemainingVariableStorage) {

       DEBUG ((DEBUG_ERROR, "SetLargeVar= iable: Not enough NV storage space to store the data\n"));

       Status =3D EFI_OUT_OF_RESOURCES;

diff --git a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteL= ib/DxeRuntimeVariableWriteLib.c b/Platform/Intel/MinPlatformPkg/Library/Dxe= RuntimeVariableWriteLib/DxeRuntimeVariableWriteLib.c
index 28730f858b..9ca4734f24 100644
--- a/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c
+++ b/Platform/Intel/MinPlatformPkg/Library/DxeRuntimeVariableWriteLib/DxeR= untimeVariableWriteLib.c
@@ -195,6 +195,21 @@ VarLibVariableRequestToLock (
   return Status;

 }

 

+/**

+  Indicator of whether it is runtime or not.

+

+  @retval TRUE        It is Runtim= e.

+  @retval FALSE       It is not Runtime= .

+**/

+BOOLEAN

+EFIAPI

+VarLibAtOsRuntime (

+  VOID

+  )

+{

+  return (mVariableWriteLibVariablePolicy =3D=3D NULL) ? TRUE : FALSE= ;

+}

+

 /**

   Close events when driver unloaded.

 

diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmV= ariableWriteCommon.c b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWri= teLib/SmmVariableWriteCommon.c
index 50ebb544b8..cd7118d1fb 100644
--- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c
+++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/SmmVariable= WriteCommon.c
@@ -18,6 +18,7 @@
 #include <Protocol/SmmVariable.h>

 

 EFI_SMM_VARIABLE_PROTOCOL  *mVariableWriteLibSmmVariable =3D NUL= L;

+BOOLEAN           &= nbsp;        mEfiAtRuntime =3D FALSE;
 

 /**

   Sets the value of a variable.

@@ -169,3 +170,18 @@ VarLibVariableRequestToLock (
   //

   return EFI_UNSUPPORTED;

 }

+

+/**

+  Indicator of whether it is runtime or not.

+

+  @retval TRUE        It is Runtim= e.

+  @retval FALSE       It is not Runtime= .

+**/

+BOOLEAN

+EFIAPI

+VarLibAtOsRuntime (

+  VOID

+  )

+{

+  return mEfiAtRuntime;

+}

diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libra= ry/SmmVariableWriteLib/StandaloneMmVariableWriteLibConstructor.c
index d39418abd2..8c2b7d18f5 100644
--- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c
+++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLibConstructor.c
@@ -20,6 +20,28 @@
 #include <Library/MmServicesTableLib.h>

 

 extern EFI_SMM_VARIABLE_PROTOCOL  *mVariableWriteLibSmmVariable;=

+extern BOOLEAN          =           mEfiAtRuntime;

+

+/**

+  Callback for ExitBootService, which is registered at the constructo= r.

+  This callback sets a global variable mEfiAtRuntime to indicate whet= her

+  it is after ExitBootService.

+

+  @param[in] Protocol        Proto= col unique ID.

+  @param[in] Interface       Interface = instance.

+  @param[in] Handle        &n= bsp; The handle on which the interface is installed.

+**/

+EFI_STATUS

+EFIAPI

+VarLibExitBootServicesCallback (

+  IN      CONST EFI_GUID   *Protoc= ol,

+  IN      VOID     =         *Interface,

+  IN      EFI_HANDLE    =     Handle

+  )

+{

+  mEfiAtRuntime =3D TRUE;

+  return EFI_SUCCESS;

+}

 

 /**

   The constructor function acquires the EFI SMM Variable Service= s

@@ -41,11 +63,19 @@ StandaloneMmVariableWriteLibConstructor (
   )

 {

   EFI_STATUS    Status;

+  VOID          *Registr= ation =3D NULL;

 

   //

   // Locate SmmVariableProtocol.

   //

   Status =3D gMmst->MmLocateProtocol (&gEfiSmmVariablePro= tocolGuid, NULL, (VOID **) &mVariableWriteLibSmmVariable);

   ASSERT_EFI_ERROR (Status);

+

+  //

+  // Register VarLibExitBootServicesCallback for gEdkiiSmmExitBootSer= vicesProtocolGuid.

+  //

+  Status =3D SmmRegisterProtocolNotify (&gEdkiiSmmExitBootService= sProtocolGuid, VarLibExitBootServicesCallback, &Registration);

+  ASSERT_EFI_ERROR (Status);

+

   return Status;

 }

diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLibConstructor.c b/Platform/Intel/MinPlatformPkg/Libr= ary/SmmVariableWriteLib/TraditionalMmVariableWriteLibConstructor.c
index d142527e17..abc1e25cde 100644
--- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c
+++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLibConstructor.c
@@ -20,6 +20,28 @@
 #include <Library/SmmServicesTableLib.h>

 

 extern EFI_SMM_VARIABLE_PROTOCOL  *mVariableWriteLibSmmVariable;=

+extern BOOLEAN          =           mEfiAtRuntime;

+

+/**

+  Callback for ExitBootService, which is registered at the constructo= r.

+  This callback sets a global variable mEfiAtRuntime to indicate whet= her

+  it is after ExitBootService.

+

+  @param[in] Protocol        Proto= col unique ID.

+  @param[in] Interface       Interface = instance.

+  @param[in] Handle        &n= bsp; The handle on which the interface is installed.

+**/

+EFI_STATUS

+EFIAPI

+VarLibExitBootServicesCallback (

+  IN      CONST EFI_GUID   *Protoc= ol,

+  IN      VOID     =         *Interface,

+  IN      EFI_HANDLE    =     Handle

+  )

+{

+  mEfiAtRuntime =3D TRUE;

+  return EFI_SUCCESS;

+}

 

 /**

   The constructor function acquires the EFI SMM Variable Service= s

@@ -41,11 +63,19 @@ TraditionalMmVariableWriteLibConstructor (
   )

 {

   EFI_STATUS    Status;

+  VOID          *Registr= ation =3D NULL;

 

   //

   // Locate SmmVariableProtocol.

   //

   Status =3D gSmst->SmmLocateProtocol (&gEfiSmmVariablePr= otocolGuid, NULL, (VOID **) &mVariableWriteLibSmmVariable);

   ASSERT_EFI_ERROR (Status);

+

+  //

+  // Register VarLibExitBootServicesCallback for gEdkiiSmmExitBootSer= vicesProtocolGuid.

+  //

+  Status =3D SmmRegisterProtocolNotify (&gEdkiiSmmExitBootService= sProtocolGuid, VarLibExitBootServicesCallback, &Registration);

+  ASSERT_EFI_ERROR (Status);

+

   return Status;

 }

diff --git a/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib= .h b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h
index fab87f2e48..bc0b52d782 100644
--- a/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h
+++ b/Platform/Intel/MinPlatformPkg/Include/Library/VariableWriteLib.h
@@ -135,4 +135,16 @@ VarLibVariableRequestToLock (
   IN  EFI_GUID       &nb= sp;            = *VendorGuid

   );

 

+/**

+  Indicator of whether it is runtime or not.

+

+  @retval TRUE        It is Runtim= e.

+  @retval FALSE       It is not Runtime= .

+**/

+BOOLEAN

+EFIAPI

+VarLibAtOsRuntime (

+  VOID

+  );

+

 #endif  // _VARIABLE_WRITE_LIB_H_

diff --git a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/Bas= eLargeVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/BaseLarg= eVariableLib/BaseLargeVariableWriteLib.inf
index 2493a94596..cbc2a5d93a 100644
--- a/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeV= ariableWriteLib.inf
+++ b/Platform/Intel/MinPlatformPkg/Library/BaseLargeVariableLib/BaseLargeV= ariableWriteLib.inf
@@ -49,3 +49,4 @@
   PrintLib

   VariableReadLib

   VariableWriteLib

+  LargeVariableReadLib

diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Stan= daloneMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVar= iableWriteLib/StandaloneMmVariableWriteLib.inf
index 0d1c63a297..868be49630 100644
--- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf
+++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/StandaloneM= mVariableWriteLib.inf
@@ -39,7 +39,8 @@
   MmServicesTableLib

 

 [Protocols]

-  gEfiSmmVariableProtocolGuid   ## CONSUMES

+  gEfiSmmVariableProtocolGuid      &nbs= p;      ## CONSUMES

+  gEdkiiSmmExitBootServicesProtocolGuid   ## CONSUMES

 

 [Depex]

   gEfiSmmVariableProtocolGuid

diff --git a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Trad= itionalMmVariableWriteLib.inf b/Platform/Intel/MinPlatformPkg/Library/SmmVa= riableWriteLib/TraditionalMmVariableWriteLib.inf
index 5d833b7e0f..4aaab069ab 100644
--- a/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf
+++ b/Platform/Intel/MinPlatformPkg/Library/SmmVariableWriteLib/Traditional= MmVariableWriteLib.inf
@@ -38,7 +38,8 @@
   SmmServicesTableLib

 

 [Protocols]

-  gEfiSmmVariableProtocolGuid   ## CONSUMES

+  gEfiSmmVariableProtocolGuid      &nbs= p;      ## CONSUMES

+  gEdkiiSmmExitBootServicesProtocolGuid   ## CONSUMES

 

 [Depex]

   gEfiSmmVariableProtocolGuid

--
2.39.1.windows.1

--_000_BN9PR11MB548369EC3AB41503E4C6B3E8E6419BN9PR11MB5483namp_--