From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web10.4780.1587866602747242336 for ; Sat, 25 Apr 2020 19:03:23 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@intel.onmicrosoft.com header.s=selector2-intel-onmicrosoft-com header.b=Du1g1Npu; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: guomin.jiang@intel.com) IronPort-SDR: niVLgWcJZguLMhEYouo9aXj9H4m1eK7K+XErEo3Lk1MHHcSAf3Qg69SC6sRnaKSKKgxDIZKkJu Pg3dCl3Qt8PQ== X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Apr 2020 19:03:21 -0700 IronPort-SDR: /iPpeDGlfJPHLgb7kQw7Fj703NysPkTubqKPnUrvqrBKwvCGeb1ZOhzFquVxjXAECsEuTT1C3j pJm4Haqk5CuQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.73,318,1583222400"; d="scan'208";a="260279528" Received: from orsmsx106.amr.corp.intel.com ([10.22.225.133]) by orsmga006.jf.intel.com with ESMTP; 25 Apr 2020 19:03:21 -0700 Received: from orsmsx158.amr.corp.intel.com (10.22.240.20) by ORSMSX106.amr.corp.intel.com (10.22.225.133) with Microsoft SMTP Server (TLS) id 14.3.439.0; Sat, 25 Apr 2020 19:03:20 -0700 Received: from ORSEDG002.ED.cps.intel.com (10.7.248.5) by ORSMSX158.amr.corp.intel.com (10.22.240.20) with Microsoft SMTP Server (TLS) id 14.3.439.0; Sat, 25 Apr 2020 19:03:20 -0700 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (104.47.55.176) by edgegateway.intel.com (134.134.137.101) with Microsoft SMTP Server (TLS) id 14.3.439.0; Sat, 25 Apr 2020 19:03:20 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OK9yx+IHmd/qMPWZTeXPYFVp2BC4e6kedhEyctwwkcC/HnrNMD22Zpoc3021pSYbbJZEs27+5ea4o4So1o9r4F4T1gBYdBFoOLFRL3vdcpDgyHcAzE5kje0rf2vY2iOG4I7J4UZ037rTFUISLdTlDfi0dsSp7rSGGfZg4p/MD7WFRvOx/6Q+TSTubGXAMHxSK5hh8T/52Nb6qRke/+Wf3Wx7fEnAaOkwXAoY8DYYpcuF44sP6WtgFrCkmB6uukNI2gBSTyeHEROmJenyKXAXNBX1yJnFXB0HPwiJN4dLILjbwxp39PvExOF49E0hP0s0mzy/Z8jtmHWNIXh//i/Yyg== 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-SenderADCheck; bh=xqR5Br5EjrITfKUvKJi7AcI6ULxSPYs4L8fwY22ubuk=; b=bSLuZRw+cbLpc5V3+1YKfcexyrOeEU7PPYMpD5nYDf2UaYeRacoG3bmzAjcugqCHEPdHRZCrrNdfR70I70WyLQUFzbOdJw5vZNyc6vGzHimWDm2Q2VErwrfIhsEQstkpZNsdVBZFW37TepY/yIae/OFB2Stk+COM9wB3gVenY5RmHLz8hnuEsHuxTmH6LvKQEj2p0sRPwc6zyK9LJV19tjnjQyKRp+GKdjZqxEWdeQqVDiAQ8vRuAxdCbUfTHIfA0TGAvcwshYR4S5GyqZv0wB9ojdhv8iMvK9eFOP6r0a9VgpaQAP5fe6TKChAUxesd7e9aDsXZr3WEq5Lx6oV20Q== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.onmicrosoft.com; s=selector2-intel-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xqR5Br5EjrITfKUvKJi7AcI6ULxSPYs4L8fwY22ubuk=; b=Du1g1NpuXADG78eOXx5+a/Sz4+b6N6NvFhF/Pmk1GyncUBLg+0q1pxEaMEemIu3s9BQvE6eHiWwiLOfBeW5wYxc45NHZJIk8hyVk30W+4zNlrrlhaHRjCPT6u3YKdpbeqdRNtY19NnI1rGlcf/zuGWBJ/Lihx1wc3c/7nTz57GE= Received: from DM6PR11MB2955.namprd11.prod.outlook.com (20.177.217.95) by DM6PR11MB2748.namprd11.prod.outlook.com (20.176.100.11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2937.22; Sun, 26 Apr 2020 02:03:18 +0000 Received: from DM6PR11MB2955.namprd11.prod.outlook.com ([fe80::ccd4:4b0d:535a:58be]) by DM6PR11MB2955.namprd11.prod.outlook.com ([fe80::ccd4:4b0d:535a:58be%7]) with mapi id 15.20.2921.030; Sun, 26 Apr 2020 02:03:17 +0000 From: "Guomin Jiang" To: "devel@edk2.groups.io" , "michael.kubacki@outlook.com" CC: "Wang, Jian J" , "Wu, Hao A" , "Gao, Liming" Subject: Re: [edk2-devel] [PATCH v1 3/9] MdeModulePkg: Define the VariablePolicyHelperLib Thread-Topic: [edk2-devel] [PATCH v1 3/9] MdeModulePkg: Define the VariablePolicyHelperLib Thread-Index: AQHWD2dQNjDDF8McX0ODRO7RVrNdeqiKvVsg Date: Sun, 26 Apr 2020 02:03:17 +0000 Message-ID: References: <20200410183802.21192-1-michael.kubacki@outlook.com> In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-version: 11.2.0.6 dlp-product: dlpe-windows dlp-reaction: no-action authentication-results: spf=none (sender IP is ) smtp.mailfrom=guomin.jiang@intel.com; x-originating-ip: [134.191.221.109] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 77ab0778-df04-4a8b-c7f6-08d7e985fcdc x-ms-traffictypediagnostic: DM6PR11MB2748: x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:6430; x-forefront-prvs: 03853D523D x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM6PR11MB2955.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFTY:;SFS:(396003)(346002)(366004)(376002)(39850400004)(136003)(45080400002)(54906003)(55016002)(30864003)(66556008)(66446008)(66476007)(33656002)(66946007)(52536014)(64756008)(5660300002)(71200400001)(966005)(110136005)(9686003)(86362001)(76116006)(478600001)(81156014)(53546011)(8936002)(6506007)(107886003)(26005)(4326008)(7696005)(2906002)(186003)(316002)(8676002)(559001)(579004);DIR:OUT;SFP:1102; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: pNEppT13w56F8llkTI8zBBEUXobSwJ9+aXEzk7DcNc2YH/ZkX5RiRgtck+77I0lFZM5OLFvNxeMBHVPY9B4TJpk+hBuLV5NilE7R0lTIWYjZTYBdbcH5gOknn2oYuwX2Z6O/lgnXTpuGlNLgKxysN7hW/JPg6bWgG1QHux+gCvVUBTrdYEIRMdCvCtvJueuSraCG/Y1bJWuGFxBqWbLv4djnqvZ4OuPKH5qcAeVWOrnmvBz9eHFQ7GCCMrmBDm0crt/j13HwCuqXvcvgH/P7bct+NTtDayClZjJYSbySIDdZ6ia1+Kuv0HgBSTo54Exoni/0ttfjFhS/ff24mwCG2P+r6IMDqPoAmLzpOLK/B8aCQ1yqOkPMLR+m+TshD0y9CUguPD8ai8ryOjCphrfptC4iAeRlRZxkyOI7lhPJyUegLNFGru6H7iQWA9To2v9o+ZOgg28GxcUuSs7BkRsRWYN533rKMXwFe+cgXnjYngUpZqFFG06S96wJG0/Jxh530Yj5/b9cwuJdD94RjpkEGw== x-ms-exchange-antispam-messagedata: JYnEYrJAxf7xcu3FnDb70VvOPCcjhP84PMrBPu934RsNRT9uW+QAn46tdqaOHmHaTsa6EEXPfktAXaRW+SRQwQELTh6DQ3oxHhE72SrSw0nIkL37LLBqYlrgKgylP/w3liPQdt6tEBcvkod6/rVs5v9rN4/EFD2cuR5wKnx1lo5hVaaGBXZrrhnmVUUWvZMZh5lR4f0NVZoi86eWZHML8mKLFfd4r14CawFj7oZirSMr/TLTyY4n0tzHvPp4ZKm6s1qOdYJ15PMd/Uj7sV4vsAzgpS1Thy8+AQ5SRapRD13EDR0kWE3YKhUXSHeT7dFeo+eUgd4NvxmuDtTOSu7YHGqfwFvnnoe34glPI7xy/tXzBvv5utswwjaEd+OSPCbuTnCp/R4bDeeIdqRI8f777lZLnqGLJfzp5cZj561srwEm8Mh8Ubh6FDtjU+9pa+Jme3A+IhCc6TQIH+5GbgE+heu6UjtVLbyOXOBL+0vrFnAkMkPVHJJDm+vP8na9NTHMFbchgqfrVsMsu2ot5sWWW09nUEJLx6x4aJud7BEoO9B3BQjBbsUozM4DuvQbYRHzT1qAYIwk+GdFYW1+M2cRUrbbPfpZMIy1H9uo0937jV0mi0Q/9Ud81VD/9KmaXF/8iTlVEzdjvPbkH13I4PuXoV9QpTWEGPESdImndnGMBootQDNhgkx54TlVwPPwERQ+hg7It8hmV/4ozLOj9CB6mVO3D3Y0q2Xf/9jdZ1gYp2Ic9pAQufsOE3pFk5FxDHrnj1FjojzN0sb0XJfl2aU0bdwshC+2bO3R+D6NG9gC2RUmT7YMJBQLKFaCJUjPiwzg MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 77ab0778-df04-4a8b-c7f6-08d7e985fcdc X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Apr 2020 02:03:17.7085 (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: kOl9suHGyGSOI20eraxlQK5/ndRyiyxbA3m3NM86/UDFg7k61ZsE2l0ywknIX9lEq4b6+hn19kOWWi3VynntUA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR11MB2748 Return-Path: guomin.jiang@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Add comment inline. > -----Original Message----- > From: devel@edk2.groups.io On Behalf Of Michael > Kubacki > Sent: Saturday, April 11, 2020 2:38 AM > To: devel@edk2.groups.io > Cc: Wang, Jian J ; Wu, Hao A = ; > Gao, Liming > Subject: [edk2-devel] [PATCH v1 3/9] MdeModulePkg: Define the > VariablePolicyHelperLib >=20 > From: Bret Barkelew >=20 > https://bugzilla.tianocore.org/show_bug.cgi?id=3D2522 >=20 > VariablePolicy is an updated interface to replace VarLock and > VarCheckProtocol. >=20 > Add the VariablePolicyHelperLib library, containing several functions to= help > with the repetitive process of creating a correctly structured and packe= d > VariablePolicy entry. >=20 > Cc: Jian J Wang > Cc: Hao A Wu > Cc: Liming Gao > Signed-off-by: Bret Barkelew > Signed-off-by: Michael Kubacki > --- > MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.c > | 396 ++++++++++++++++++++ > MdeModulePkg/Include/Library/VariablePolicyHelperLib.h = | 164 > ++++++++ > MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.in= f > | 36 ++ > MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.uni > | 12 + > MdeModulePkg/MdeModulePkg.dec = | 5 + > MdeModulePkg/MdeModulePkg.dsc = | 2 + > 6 files changed, 615 insertions(+) >=20 > diff --git > a/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.c > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.c > new file mode 100644 > index 000000000000..7cf58b6cb31c > --- /dev/null > +++ > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperL > +++ ib.c > @@ -0,0 +1,396 @@ > +/** @file -- VariablePolicyHelperLib.c > +This library contains helper functions for marshalling and registering > +new policies with the VariablePolicy infrastructure. > + > +This library is currently written against VariablePolicy revision 0x000= 10000. > + > +Copyright (c) Microsoft Corporation. > +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#include > + > +#include > +#include > +#include > +#include > + > +#include > + > +/** > + This internal helper function populates the header structure, > + all common fields, and takes care of fix-ups. > + > + NOTE: Only use this internally. Assumes correctly-sized buffers. > + > + @param[out] EntPtr Pointer to the buffer to be populated. > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] LockPolicyType LockPolicyType for the VariablePoli= cy. > + > +**/ > +STATIC > +VOID > +PopulateCommonData ( > + OUT VARIABLE_POLICY_ENTRY *EntPtr, > + IN CONST EFI_GUID *Namespace, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN UINT8 LockPolicyType > + ) > +{ > + EntPtr->Version =3D VARIABLE_POLICY_ENTRY_REVISION; > + CopyGuid( &EntPtr->Namespace, Namespace ); > + EntPtr->MinSize =3D MinSize; > + EntPtr->MaxSize =3D MaxSize; > + EntPtr->AttributesMustHave =3D AttributesMustHave; > + EntPtr->AttributesCantHave =3D AttributesCantHave; > + EntPtr->LockPolicyType =3D LockPolicyType; > + > + // NOTE: As a heler, fix up MaxSize for compatibility with the old mo= del. > + if (EntPtr->MaxSize =3D=3D 0) { > + EntPtr->MaxSize =3D VARIABLE_POLICY_NO_MAX_SIZE; } > + > + return; > +} > + > + > +/** > + This helper function will allocate and populate a new VariablePolicy > + structure for a policy that does not contain any sub-structures (such > +as > + VARIABLE_LOCK_ON_VAR_STATE_POLICY). > + > + NOTE: Caller will need to free structure once finished. > + > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] LockPolicyType LockPolicyType for the VariablePoli= cy. > + @param[out] NewEntry If successful, will be set to a pointer to th= e > allocated buffer containing the > + new policy. > + > + @retval EFI_SUCCESS Operation completed successfully = and > structure is populated. > + @retval EFI_INVALID_PARAMETER Namespace is NULL. > + @retval EFI_INVALID_PARAMETER LockPolicyType is invalid for a b= asic > structure. > + @retval EFI_BUFFER_TOO_SMALL Finished structure would not fit = in > UINT16 size. > + @retval EFI_OUT_OF_RESOURCES Could not allocate sufficient spa= ce > for structure. > + > +**/ > +EFI_STATUS > +EFIAPI > +CreateBasicVariablePolicy ( > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN UINT8 LockPolicyType, > + OUT VARIABLE_POLICY_ENTRY **NewEntry > + ) > +{ > + UINTN TotalSize; > + UINTN NameSize; > + VARIABLE_POLICY_ENTRY *EntPtr; > + CHAR16 *CopyName; > + > + // Check some initial invalid parameters for this function. > + if (Namespace =3D=3D NULL || NewEntry =3D=3D NULL) { > + return EFI_INVALID_PARAMETER; > + } > + if (LockPolicyType !=3D VARIABLE_POLICY_TYPE_NO_LOCK && > + LockPolicyType !=3D VARIABLE_POLICY_TYPE_LOCK_NOW && > + LockPolicyType !=3D VARIABLE_POLICY_TYPE_LOCK_ON_CREATE) { > + return EFI_INVALID_PARAMETER; > + } > + > + // Now we've gotta determine the total size of the buffer required > + for // the VariablePolicy structure. > + TotalSize =3D sizeof( VARIABLE_POLICY_ENTRY ); if (Name !=3D NULL) { > + NameSize =3D StrnSizeS( Name, MAX_UINT16 ); > + TotalSize +=3D NameSize; > + } > + // Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size. > + ASSERT( TotalSize <=3D MAX_UINT16 ); > + if (TotalSize > MAX_UINT16) { > + return EFI_BUFFER_TOO_SMALL; > + } > + > + // Allocate a buffer to hold all the data. We're on the home stretch. > + *NewEntry =3D AllocatePool( TotalSize ); if (*NewEntry =3D=3D NULL) = { > + return EFI_OUT_OF_RESOURCES; > + } > + > + // If we're still here, we're basically done. > + // Copy the data and GET... OUT.... > + EntPtr =3D *NewEntry; > + PopulateCommonData ( EntPtr, > + Namespace, > + MinSize, > + MaxSize, > + AttributesMustHave, > + AttributesCantHave, > + LockPolicyType ); > + EntPtr->Size =3D (UINT16)TotalSize; // This is sa= fe because we've > already checked. > + EntPtr->OffsetToName =3D sizeof(VARIABLE_POLICY_ENTRY); > + if (Name !=3D NULL) { > + CopyName =3D (CHAR16*)((UINT8*)EntPtr + EntPtr->OffsetToName); > + CopyMem( CopyName, Name, NameSize ); } > + > + return EFI_SUCCESS; > +} > + > + > +/** > + This helper function will allocate and populate a new VariablePolicy > + structure for a policy with a lock type of > VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE. > + > + NOTE: Caller will need to free structure once finished. > + > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] VarStateNamespace Pointer to the EFI_GUID for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace. > + @param[in] VarStateValue Value for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value. > + @param[in] VarStateName Pointer to the CHAR16 array for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name. > + @param[out] NewEntry If successful, will be set to a pointer to th= e > allocated buffer containing the > + new policy. > + > + @retval EFI_SUCCESS Operation completed successfully = and > structure is populated. > + @retval EFI_INVALID_PARAMETER Namespace, VarStateNamespace, > VarStateName is NULL. > + @retval EFI_BUFFER_TOO_SMALL Finished structure would not fit = in > UINT16 size. > + @retval EFI_OUT_OF_RESOURCES Could not allocate sufficient spa= ce > for structure. > + > +**/ > +EFI_STATUS > +EFIAPI > +CreateVarStateVariablePolicy ( > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN CONST EFI_GUID *VarStateNamespace, > + IN UINT8 VarStateValue, > + IN CONST CHAR16 *VarStateName, > + OUT VARIABLE_POLICY_ENTRY **NewEntry > + ) > +{ > + UINTN TotalSize; > + UINTN NameSize; > + UINTN VarStateNameSize; > + VARIABLE_POLICY_ENTRY *EntPtr; > + CHAR16 *CopyName; > + VARIABLE_LOCK_ON_VAR_STATE_POLICY *CopyPolicy; > + > + // Check some initial invalid parameters for this function. > + if (Namespace =3D=3D NULL || VarStateNamespace =3D=3D NULL || > + VarStateName =3D=3D NULL || NewEntry =3D=3D NULL) { > + return EFI_INVALID_PARAMETER; > + } > + > + // Now we've gotta determine the total size of the buffer required > + for // the VariablePolicy structure. > + VarStateNameSize =3D StrnSizeS( VarStateName, MAX_UINT16 ); TotalSiz= e > + =3D sizeof( VARIABLE_POLICY_ENTRY ) + > + sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLICY) + > + VarStateNameSize; > + if (Name !=3D NULL) { > + NameSize =3D StrnSizeS( Name, MAX_UINT16 ); > + TotalSize +=3D NameSize; > + } > + // Make sure the size fits within a VARIABLE_POLICY_ENTRY.Size. > + ASSERT( TotalSize <=3D MAX_UINT16 ); > + if (TotalSize > MAX_UINT16) { > + return EFI_BUFFER_TOO_SMALL; > + } > + > + // Allocate a buffer to hold all the data. We're on the home stretch. > + *NewEntry =3D AllocatePool( TotalSize ); if (*NewEntry =3D=3D NULL) = { > + return EFI_OUT_OF_RESOURCES; > + } > + > + // If we're still here, we're basically done. > + // Copy the data and GET... OUT.... > + EntPtr =3D *NewEntry; > + PopulateCommonData ( EntPtr, > + Namespace, > + MinSize, > + MaxSize, > + AttributesMustHave, > + AttributesCantHave, > + VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE ); > + EntPtr->Size =3D (UINT16)TotalSize; // This is sa= fe because we've > already checked. > + EntPtr->OffsetToName =3D sizeof(VARIABLE_POLICY_ENTRY) + > + sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLIC= Y) + > + (UINT16)VarStateNameSize; > + > + CopyPolicy =3D (VARIABLE_LOCK_ON_VAR_STATE_POLICY*)((UINT8*)EntPtr > + > + sizeof(VARIABLE_POLICY_ENTRY)); CopyName =3D > + (CHAR16*)((UINT8*)CopyPolicy + > + sizeof(VARIABLE_LOCK_ON_VAR_STATE_POLICY)); > + CopyGuid( &CopyPolicy->Namespace, VarStateNamespace ); > + CopyPolicy->Value =3D VarStateValue; CopyMem( CopyName, > VarStateName, > + VarStateNameSize ); > + > + if (Name !=3D NULL) { > + CopyName =3D (CHAR16*)((UINT8*)EntPtr + EntPtr->OffsetToName); > + CopyMem( CopyName, Name, NameSize ); } > + > + return EFI_SUCCESS; > +} > + > + > +/** > + This helper function does everything that CreateBasicVariablePolicy() > +does, but also > + uses the passed in protocol to register the policy with the infrastru= cture. > + Does not return a buffer, does not require the caller to free anythin= g. > + > + @param[in] VariablePolicy Pointer to a valid instance of the Variab= lePolicy > protocol. > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] LockPolicyType LockPolicyType for the VariablePoli= cy. > + > + @retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL. > + @retval EFI_STATUS Status returned by CreateBasicVaria= blePolicy() > or RegisterVariablePolicy(). > + > +**/ > +EFI_STATUS > +EFIAPI > +RegisterBasicVariablePolicy ( > + IN VARIABLE_POLICY_PROTOCOL *VariablePolicy, > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN UINT8 LockPolicyType > + ) > +{ > + VARIABLE_POLICY_ENTRY *NewEntry; > + EFI_STATUS Status; > + > + // Check the simple things. > + if (VariablePolicy =3D=3D NULL) { > + return EFI_INVALID_PARAMETER; > + } > + > + // Create the new entry and make sure that everything worked. > + NewEntry =3D NULL; > + Status =3D CreateBasicVariablePolicy( Namespace, > + Name, > + MinSize, > + MaxSize, > + AttributesMustHave, > + AttributesCantHave, > + LockPolicyType, > + &NewEntry ); > + > + // If that was successful, attempt to register the new policy. > + if (!EFI_ERROR( Status )) { > + Status =3D VariablePolicy->RegisterVariablePolicy( NewEntry ); } > + > + // If we allocated the buffer, free the buffer. > + if (NewEntry !=3D NULL) { > + FreePool( NewEntry ); > + } > + > + return Status; > +} > + > + > +/** > + This helper function does everything that CreateBasicVariablePolicy() > +does, but also According to the description, it should do the CreateVarStateVariablePolic= y(). This two register routine is same, can we merge it into one routine? Use R= egisterVariablePolicy() to present it? > + uses the passed in protocol to register the policy with the infrastru= cture. > + Does not return a buffer, does not require the caller to free anythin= g. > + > + @param[in] VariablePolicy Pointer to a valid instance of the Variab= lePolicy > protocol. > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] VarStateNamespace Pointer to the EFI_GUID for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace. > + @param[in] VarStateName Pointer to the CHAR16 array for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name. > + @param[in] VarStateValue Value for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value. > + > + @retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL. > + @retval EFI_STATUS Status returned by CreateBasicVariablePolic= y() or > RegisterVariablePolicy(). > + > +**/ > +EFI_STATUS > +EFIAPI > +RegisterVarStateVariablePolicy ( > + IN VARIABLE_POLICY_PROTOCOL *VariablePolicy, > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN CONST EFI_GUID *VarStateNamespace, > + IN CONST CHAR16 *VarStateName, > + IN UINT8 VarStateValue > + ) > +{ > + VARIABLE_POLICY_ENTRY *NewEntry; > + EFI_STATUS Status; > + > + // Check the simple things. > + if (VariablePolicy =3D=3D NULL) { > + return EFI_INVALID_PARAMETER; > + } > + > + // Create the new entry and make sure that everything worked. > + NewEntry =3D NULL; > + Status =3D CreateVarStateVariablePolicy( Namespace, > + Name, > + MinSize, > + MaxSize, > + AttributesMustHave, > + AttributesCantHave, > + VarStateNamespace, > + VarStateValue, > + VarStateName, > + &NewEntry ); > + > + // If that was successful, attempt to register the new policy. > + if (!EFI_ERROR( Status )) { > + Status =3D VariablePolicy->RegisterVariablePolicy( NewEntry ); } > + > + // If we allocated the buffer, free the buffer. > + if (NewEntry !=3D NULL) { > + FreePool( NewEntry ); > + } > + > + return Status; > +} > diff --git a/MdeModulePkg/Include/Library/VariablePolicyHelperLib.h > b/MdeModulePkg/Include/Library/VariablePolicyHelperLib.h > new file mode 100644 > index 000000000000..721a55931aab > --- /dev/null > +++ b/MdeModulePkg/Include/Library/VariablePolicyHelperLib.h > @@ -0,0 +1,164 @@ > +/** @file -- VariablePolicyHelperLib.h > +This library contains helper functions for marshalling and registering > +new policies with the VariablePolicy infrastructure. > + > +Copyright (c) Microsoft Corporation. > +SPDX-License-Identifier: BSD-2-Clause-Patent > + > +**/ > + > +#ifndef _VARIABLE_POLICY_HELPER_LIB_H_ > +#define _VARIABLE_POLICY_HELPER_LIB_H_ > + > +#include > + > +/** > + This helper function will allocate and populate a new VariablePolicy > + structure for a policy that does not contain any sub-structures (such > +as > + VARIABLE_LOCK_ON_VAR_STATE_POLICY). > + > + NOTE: Caller will need to free structure once finished. > + > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] LockPolicyType LockPolicyType for the VariablePoli= cy. > + @param[out] NewEntry If successful, will be set to a pointer to th= e > allocated buffer containing the > + new policy. > + > + @retval EFI_SUCCESS Operation completed successfully = and > structure is populated. > + @retval EFI_INVALID_PARAMETER Namespace is NULL. > + @retval EFI_INVALID_PARAMETER LockPolicyType is invalid for a b= asic > structure. > + @retval EFI_BUFFER_TOO_SMALL Finished structure would not fit = in > UINT16 size. > + @retval EFI_OUT_OF_RESOURCES Could not allocate sufficient spa= ce > for structure. > + > +**/ > +EFI_STATUS > +EFIAPI > +CreateBasicVariablePolicy ( > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN UINT8 LockPolicyType, > + OUT VARIABLE_POLICY_ENTRY **NewEntry > + ); > + > + > +/** > + This helper function will allocate and populate a new VariablePolicy > + structure for a policy with a lock type of > VARIABLE_POLICY_TYPE_LOCK_ON_VAR_STATE. > + > + NOTE: Caller will need to free structure once finished. > + > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] VarStateNamespace Pointer to the EFI_GUID for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace. > + @param[in] VarStateValue Value for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value. > + @param[in] VarStateName Pointer to the CHAR16 array for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name. > + @param[out] NewEntry If successful, will be set to a pointer to th= e > allocated buffer containing the > + new policy. > + > + @retval EFI_SUCCESS Operation completed successfully = and > structure is populated. > + @retval EFI_INVALID_PARAMETER Namespace, VarStateNamespace, > VarStateName is NULL. > + @retval EFI_BUFFER_TOO_SMALL Finished structure would not fit = in > UINT16 size. > + @retval EFI_OUT_OF_RESOURCES Could not allocate sufficient spa= ce > for structure. > + > +**/ > +EFI_STATUS > +EFIAPI > +CreateVarStateVariablePolicy ( > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN CONST EFI_GUID *VarStateNamespace, > + IN UINT8 VarStateValue, > + IN CONST CHAR16 *VarStateName, > + OUT VARIABLE_POLICY_ENTRY **NewEntry > + ); > + > + > +/** > + This helper function does everything that CreateBasicVariablePolicy() > +does, but also > + uses the passed in protocol to register the policy with the infrastru= cture. > + Does not return a buffer, does not require the caller to free anythin= g. > + > + @param[in] VariablePolicy Pointer to a valid instance of the Variab= lePolicy > protocol. > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] LockPolicyType LockPolicyType for the VariablePoli= cy. > + > + @retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL. > + @retval EFI_STATUS Status returned by CreateBasicVaria= blePolicy() > or RegisterVariablePolicy(). > + > +**/ > +EFI_STATUS > +EFIAPI > +RegisterBasicVariablePolicy ( > + IN VARIABLE_POLICY_PROTOCOL *VariablePolicy, > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN UINT8 LockPolicyType > + ); > + > + > +/** > + This helper function does everything that CreateBasicVariablePolicy() > +does, but also > + uses the passed in protocol to register the policy with the infrastru= cture. > + Does not return a buffer, does not require the caller to free anythin= g. > + > + @param[in] VariablePolicy Pointer to a valid instance of the Variab= lePolicy > protocol. > + @param[in] Namespace Pointer to an EFI_GUID for the target variabl= e > namespace that this policy will protect. > + @param[in] Name [Optional] If provided, a pointer to the CHAR= 16 > array for the target variable name. > + Otherwise, will create a policy that targets = an entire > namespace. > + @param[in] MinSize MinSize for the VariablePolicy. > + @param[in] MaxSize MaxSize for the VariablePolicy. > + @param[in] AttributesMustHave AttributesMustHave for the > VariablePolicy. > + @param[in] AttributesCantHave AttributesCantHave for the > VariablePolicy. > + @param[in] VarStateNamespace Pointer to the EFI_GUID for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Namespace. > + @param[in] VarStateName Pointer to the CHAR16 array for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Name. > + @param[in] VarStateValue Value for the > VARIABLE_LOCK_ON_VAR_STATE_POLICY.Value. > + > + @retval EFI_INVALID_PARAMETER VariablePolicy pointer is NULL. > + @retval EFI_STATUS Status returned by CreateBasicVariablePolic= y() or > RegisterVariablePolicy(). > + > +**/ > +EFI_STATUS > +EFIAPI > +RegisterVarStateVariablePolicy ( > + IN VARIABLE_POLICY_PROTOCOL *VariablePolicy, > + IN CONST EFI_GUID *Namespace, > + IN CONST CHAR16 *Name OPTIONAL, > + IN UINT32 MinSize, > + IN UINT32 MaxSize, > + IN UINT32 AttributesMustHave, > + IN UINT32 AttributesCantHave, > + IN CONST EFI_GUID *VarStateNamespace, > + IN CONST CHAR16 *VarStateName, > + IN UINT8 VarStateValue > + ); > + > +#endif // _VARIABLE_POLICY_HELPER_LIB_H_ > diff --git > a/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.i > nf > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.i > nf > new file mode 100644 > index 000000000000..551435dce8d3 > --- /dev/null > +++ > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperL > +++ ib.inf > @@ -0,0 +1,36 @@ > +## @file VariablePolicyHelperLib.inf > +# This library contains helper functions for marshalling and > +registering # new policies with the VariablePolicy infrastructure. > +# > +# This library is currently written against VariablePolicy revision 0x0= 0010000. > +# > +## > +# Copyright (c) Microsoft Corporation. > +# SPDX-License-Identifier: BSD-2-Clause-Patent ## > + > + > +[Defines] > + INF_VERSION =3D 0x00010017 > + BASE_NAME =3D VariablePolicyHelperLib > + # MODULE_UNI_FILE =3D VariablePolicyHelperLib.uni > + FILE_GUID =3D B3C2206B-FDD1-4AED-8352-FC5EC34C5630 > + VERSION_STRING =3D 1.0 > + MODULE_TYPE =3D BASE > + LIBRARY_CLASS =3D VariablePolicyHelperLib > + > + > +[Sources] > + VariablePolicyHelperLib.c > + > + > +[Packages] > + MdePkg/MdePkg.dec > + MdeModulePkg/MdeModulePkg.dec > + > + > +[LibraryClasses] > + BaseLib > + DebugLib > + MemoryAllocationLib > + BaseMemoryLib > diff --git > a/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.u > ni > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.u > ni > new file mode 100644 > index 000000000000..39cbf11a4ce9 > --- /dev/null > +++ > b/MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperL > +++ ib.uni > @@ -0,0 +1,12 @@ > +// /** @file > +// VariablePolicyHelperLib.uni > +// > +// Copyright (c) Microsoft Corporation. > +// SPDX-License-Identifier: BSD-2-Clause-Patent // // **/ > + > + > +#string STR_MODULE_ABSTRACT #language en-US "Library contai= ning > helper functions for marshalling and registering new policies with the > VariablePolicy infrastructure" > + > +#string STR_MODULE_DESCRIPTION #language en-US "Library > containing helper functions for marshalling and registering new policies= with > the VariablePolicy infrastructure" > diff --git a/MdeModulePkg/MdeModulePkg.dec > b/MdeModulePkg/MdeModulePkg.dec > index 990e23b07a08..8fd0a6a4fdfb 100644 > --- a/MdeModulePkg/MdeModulePkg.dec > +++ b/MdeModulePkg/MdeModulePkg.dec > @@ -147,6 +147,11 @@ > # > DisplayUpdateProgressLib|Include/Library/DisplayUpdateProgressLib.h >=20 > + ## @libraryclass This library contains helper functions for marshal= ling and > + # registering new policies with the VariablePolicy infrastructure. > + # > + VariablePolicyHelperLib|Include/Library/VariablePolicyHelperLib.h > + > [Guids] > ## MdeModule package token space guid > # Include/Guid/MdeModulePkgTokenSpace.h > diff --git a/MdeModulePkg/MdeModulePkg.dsc > b/MdeModulePkg/MdeModulePkg.dsc > index 8501dae88eb1..c27a3b488a32 100644 > --- a/MdeModulePkg/MdeModulePkg.dsc > +++ b/MdeModulePkg/MdeModulePkg.dsc > @@ -99,6 +99,7 @@ >=20 > BmpSupportLib|MdeModulePkg/Library/BaseBmpSupportLib/BaseBmpSupp > ortLib.inf > SafeIntLib|MdePkg/Library/BaseSafeIntLib/BaseSafeIntLib.inf >=20 > DisplayUpdateProgressLib|MdeModulePkg/Library/DisplayUpdateProgressLi > bGraphics/DisplayUpdateProgressLibGraphics.inf > + > VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/Va > riablePolicyHelperLib.inf >=20 > [LibraryClasses.EBC.PEIM] > IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf > @@ -224,6 +225,7 @@ > MdeModulePkg/Library/UefiHiiServicesLib/UefiHiiServicesLib.inf > MdeModulePkg/Library/BaseHobLibNull/BaseHobLibNull.inf >=20 > MdeModulePkg/Library/BaseMemoryAllocationLibNull/BaseMemoryAllocati > onLibNull.inf > + > MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf >=20 > MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf > MdeModulePkg/Bus/Pci/PciSioSerialDxe/PciSioSerialDxe.inf > -- > 2.16.3.windows.1 >=20 >=20 >=20