From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by mx.groups.io with SMTP id smtpd.web10.1497.1684889921522927356 for ; Tue, 23 May 2023 17:58:41 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=k9eAZ9mU; spf=pass (domain: intel.com, ip: 134.134.136.126, mailfrom: nathaniel.l.desimone@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684889921; x=1716425921; h=from:to:cc:subject:date:message-id:references: in-reply-to:mime-version; bh=1xRe0k98IcR5UVM7Ye2vtnoBgjxjkn3T1UUgt+/uC6Q=; b=k9eAZ9mU9jA2by4Z7Hl6e3wH+yK8e5MvRYXKuJpPaoy4E0nmBnFv4ijK Ctmt3/Gr1luWZTnRddKPKqMfmvdzOWKf6plu+NLdQzQ7hBA/zdeu0i56S REQtZHQTJzqiV8zqnOdASkMLs7x7Jms01zCDpSftLqVkie+90XTafqnql cb229LqjTN/UpT+/YkSQ6J04+Shnchqx+L0gli6HDuymnAwXO9fSGSmFV WePIF3ICEFyGS3MJgavIykEo2PBZbJ/ynr0G9rKs1HgkbANPCCPiTa25Q 2f98qtoPNGGbn1j9h2YUO2b3xvp2iUotcZ5RwjX5vsVR2VX3s88jM+Cco w==; X-IronPort-AV: E=McAfee;i="6600,9927,10719"; a="337995634" X-IronPort-AV: E=Sophos;i="6.00,187,1681196400"; d="scan'208,217";a="337995634" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 May 2023 17:58:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10719"; a="816357396" X-IronPort-AV: E=Sophos;i="6.00,187,1681196400"; d="scan'208,217";a="816357396" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga002.fm.intel.com with ESMTP; 23 May 2023 17:58:40 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) 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 17:58:39 -0700 Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx610.amr.corp.intel.com (10.22.229.23) 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 17:58:39 -0700 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (104.47.57.175) by edgegateway.intel.com (134.134.137.102) 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 17:58:39 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cK2MNrGWpvAacU28e0Ur2FV2JpJzdUWTeU7AYelO1BrK6tcUhAUXQVcmXyWrNEK1ztTO0InfSaToESlLX37fU+fnoG+tayhyE0Si75Revhujodoav2inEJCFmbd7WhVTlLiiuz2/wa2y7GlxjbLEVuCr2eJN5VeB9PjIyfH/Nl+JeFZqnwfIs6GtdRU0HGifJXOTjlh0XAaD9bIyNLB6JA+l5Ye5sPYSM6NYEXDNI+iW0E8JC89PWL3D+UdA87Bl3hsTV6FFzPOPgkNZHeEUgtMwwPEL+3rXBAaxYmClGvP7hDzIN7pvLYrBx2YFN8NJKRIX/7TMmD05xbfqnsg6gw== 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=f5q64cyaTA6tOZj8UxGzoXB7J8tmbUfmJ6dz3zPMfOA=; b=USrntf7irY2koEUHpIY/lTnoMUHyk5fH25OBlWQZDcJwrrKT5QxtoHaullx28H53zMjt+WEr0bRwq7KqS/QAfTaJCnyjP0PjZpI2uOFTEsHoLAqURnA9rFFT+7qzgu8www4I9Y+SpvazDuZRrv9VPBUBz2G1KF3BsWCl1NCINe1DGIYgEbtJ52pDnZiJ20S4DE+s8yKzAVZCRi2bWTPkItJep0jwpG+qb51S3HSGmkimXTAteYOyMaSpFk1yVnojeXnyLvAhNtil7QIcWBmyzG04j5DuFyqc5RunyxKC/ZB+3xYxLzcbbQlcwBXzY3M+KzFzYyqiOVIK9s42t1t+dg== 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 MW4PR11MB5821.namprd11.prod.outlook.com (2603:10b6:303:184::5) by MW4PR11MB6739.namprd11.prod.outlook.com (2603:10b6:303:20b::19) 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 00:58:37 +0000 Received: from MW4PR11MB5821.namprd11.prod.outlook.com ([fe80::c8e1:6df6:9662:e42a]) by MW4PR11MB5821.namprd11.prod.outlook.com ([fe80::c8e1:6df6:9662:e42a%6]) with mapi id 15.20.6411.025; Wed, 24 May 2023 00:58:36 +0000 From: "Nate DeSimone" 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 Thread-Topic: [edk2-platforms:PATCH v2] MinPlatformPkg: Fix SetLargeVariable fail issue Thread-Index: AQHZjdjfKtXOHIJBLE2hvESZ4Pci1a9omdSF Date: Wed, 24 May 2023 00:58:36 +0000 Message-ID: References: <20230524004403.3338-1-miki.shindo@intel.com> In-Reply-To: <20230524004403.3338-1-miki.shindo@intel.com> 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: MW4PR11MB5821:EE_|MW4PR11MB6739:EE_ x-ms-office365-filtering-correlation-id: f4188dc7-42d4-4a69-a351-08db5bf20182 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: BKVYlq7WO0gwdPkP/x0h8jQbiYgjKRXuRTCS5dVwabpEvza73wXId7RS8AUgwnQlQrWpD8M5BHEaJSkl1Wn7oA4ixbzZIVGoQxmFRAvJ8YbbTy11wnE8KQ2XVLJ/2SwTnv8caeUul50rPoTuB+pOyUgezniRRqmJgKdCKhLVbLbxznHtPhEuBNHy+0B9t/U7jdDouDUUvwZBBuSxH3yDClZYS6oXZ58kqfLgbifav5EGk5q7E/X9sN6wwZs6zte/RExXxAtdJHUXDl6hmZYxvE71cNpud51N+Qo8qh0kevA5xPFe9MTJpn5PCMpNVK9/jSptNhcnl63mSoWCT5lB8G181Ni//aT4yz+GWRfVQn9cCEdbQwsaD+N0chMeLis/GLP2DKS5siCebIpyWeFURoEe4rCFM3t+k5QEfjcDbEybI4REvTIlr7tFzORhb8bThbDCDTvuROPIN7v+wRO6VMXemOg7tD/HNeBfEWMcH1SVTgI9bx23WS16q32h+oua80r99mYFeEtwjNKkLjx+w1s6DmH/NGk4llRGbDqJCPRNUbjuzvdDTRPzChxUB2Z6+2UZytaqJfg2HYwyJa6XKSckb0f/vFiGpL4T/gWT4ms= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MW4PR11MB5821.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(376002)(366004)(396003)(39860400002)(346002)(136003)(451199021)(9686003)(53546011)(83380400001)(122000001)(33656002)(82960400001)(26005)(38100700002)(6506007)(107886003)(55016003)(186003)(30864003)(2906002)(478600001)(71200400001)(966005)(316002)(76116006)(4326008)(66476007)(66946007)(66556008)(64756008)(66446008)(91956017)(38070700005)(166002)(86362001)(7696005)(41300700001)(5660300002)(52536014)(8676002)(110136005)(8936002)(54906003);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?Gzdqrq5lfUGtsqs6Cb9ORhpdrVta7gexHn+HhakSZf72eGCS9/ULzB1+Dd/p?= =?us-ascii?Q?630nvHV0hlK1UGwa5WCWOnPeLDLhS64Q65jnAU+bi2yqEtzgKOcAuJHUX+3Q?= =?us-ascii?Q?FFrPW0+1cXOU25/BH8t2g5/dcU9PB2fvsmBXXR1OMLWdBjIG/k86iLJFQlGp?= =?us-ascii?Q?HXw/8LSd3t1TW68w88c0b/jqbqSXDK9eoXVuFsExk2y0mIj4Xkkax+COXkDf?= =?us-ascii?Q?dboBxWlcbFolb1c5DbgNWzJ46ZEdCQbMUDH2JYot8oidYJ6pat8R77y5dfAC?= =?us-ascii?Q?w/Uuyu8XDYslVYe627IdG5jl6CRUOqkrqMiiP9x9m5ax70nFit219EuNn7d2?= =?us-ascii?Q?/BXaP8qRySq87VF71MxQBpCq009eKDIiIaFSjVNJfAtnQanxcapJggSEgC6n?= =?us-ascii?Q?u658MEFIqti53lzmUCzi0lhPWEpqMSR37wVx4osYfCXAqPbC0DRiHgZY0IBg?= =?us-ascii?Q?4EmGYgOPoTf1yAX8+wvnpLmvWrxnXQF+PlsJIDLmYY+1a25T+S2klKEhQhfr?= =?us-ascii?Q?XZChCkxcnxCZ7Ue/n1Ew5IpSPPCK9PWSohZIA9msI5eJPmbOm8FXocF7Jcxr?= =?us-ascii?Q?JvOXOW7TkrLnsGg4iWWUHdga53Rt+5u670o2izVGdYZyzXIhZOs/8GJKkYWE?= =?us-ascii?Q?rJie6TtN5IKxXxNJHXKAAlbcHs825OnKNrBvxYSDOyq5OaizZZ6vpDEYy6XG?= =?us-ascii?Q?y79VCZT7PB80XFDsac38YhcyZGQzJL5btGL2OYd19OhZPm1Z1BzvkYzhM0VO?= =?us-ascii?Q?SbaFF7MC38l34zes1Nm8v7XMLUq2SumBwKNiWLQlaebnsLc7/0wd5EPLevQz?= =?us-ascii?Q?DSNxiI4mSL2F74ZVquUSjg8g1R5Ni2JJwwnnsazsIREzT56GgPJ+hqp4Erms?= =?us-ascii?Q?vSeXfZLU/xqs/bpjSyG/HplbPYypYdii71hdgTd4oq2Zlgal62PD1AOGVko3?= =?us-ascii?Q?xZCbRaASAWV/95uuv0RFigr6/FLDAKq3HgyR0LqkbnstqolE6PCTITnqQbwY?= =?us-ascii?Q?W8CAi4L8+ydVosNySsD3Lybi42qQHcc12/GgK3kOYH02GXkxyN3/h4wnrrih?= =?us-ascii?Q?JVtKI9MNimh8lHghunIDfaDQiKvloa/fxMOAFOrjkgFrZGHtnjgYJSptBNiM?= =?us-ascii?Q?pVWy0xbt/NzCqpoHbMcwy41n2MfIuBfrAIXrKb0G7tBDrdjlCbvHrvoTqugy?= =?us-ascii?Q?rol1rDlD9wquxvHaDxJPNXqgRIXKJbvS0ICClmClXQFm+qb0v5kqaxhI0WUY?= =?us-ascii?Q?XgjEylvom3H6hoY5L8SHBEBTFUyf9vQvCKKSxYKaheRBOtDUflLmYxNpk6Il?= =?us-ascii?Q?V4KXcm68zWgSA6uTpQ51cEOId4jJqh1XOnc2CFzcxBCsKWzALRRRQzGWVEpw?= =?us-ascii?Q?CcmOxKzvSjF/qXcHpRxGo325uPpr/Z5GDXiToCKSkQ4hD84Zs+yLGnKDJaXa?= =?us-ascii?Q?TJRqV0+3R0e8BhdFiwYoxC+LOPHfgC85gOfLK+xvqe8ABmgvbrb6JHAAL6gp?= =?us-ascii?Q?aUYRn10eSnLJRZKghorXS4SzrTHzMwgiEIIJoe3DLxofwp2glUeRlJvUNovM?= =?us-ascii?Q?ZFxOZKPfj/mBHo7GuMJkcao24djbAtOc3mVDRfbiQWE5YIjbxQRxKmzzFrqe?= =?us-ascii?Q?5xRwp6Ihkh19BNXWDOqcgjM=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MW4PR11MB5821.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4188dc7-42d4-4a69-a351-08db5bf20182 X-MS-Exchange-CrossTenant-originalarrivaltime: 24 May 2023 00:58:36.8477 (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: xH9ai3UcCsyzBM3RjWFPKyYj5hIBYC3uJj2o8/YwJfgXmTM3/baL3guQc0lzwonoRAp7EZKF1ua26snI+SUbiapkgnEsEXuEyoIxJOaVHaE= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW4PR11MB6739 Return-Path: nathaniel.l.desimone@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: multipart/alternative; boundary="_000_MW4PR11MB5821AB5F4AD4228C0C6E846DCD419MW4PR11MB5821namp_" --_000_MW4PR11MB5821AB5F4AD4228C0C6E846DCD419MW4PR11MB5821namp_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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 , Desimone, Nathaniel L , Gao, Liming , Dong, Eric <= eric.dong@intel.com>, 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_MW4PR11MB5821AB5F4AD4228C0C6E846DCD419MW4PR11MB5821namp_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Thank you, Miki, an= d Xiaoqiang!

 

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

 

From: Shindo, Miki <mi= ki.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, Liming <gaoliming@byosoft= .com.cn>, Dong, Eric <eric.dong@intel.com>, Zhang, Xiaoqiang <x= iaoqiang.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@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Eric Dong <eric.dong@intel.com>
Co-authored-by: Xiaoqiang Zhang <xiaoqiang.zhang@intel.com>
Signed-off-by: Miki Shindo <miki.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_MW4PR11MB5821AB5F4AD4228C0C6E846DCD419MW4PR11MB5821namp_--