From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web11.37328.1644860689532669706 for ; Mon, 14 Feb 2022 09:44:50 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=lvTdO8gR; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: guo.dong@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644860689; x=1676396689; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=fUsiGhrTu+auGLzVAxiPCKCBujRaN3HuDr2mpKzy1uQ=; b=lvTdO8gRkl3uNHKuCLP5HuYYXvsK2FTiXiMQGv8ct3QhZEOmCmFfStNA X7X3A98TZgGEhp5YQpnfk5xcNRBifdc6sxsvrFSBeRaVCJ2fQsPqxy3XV tIMPjyCAqB8UxtvU+SGCRE60cD2Y15F+ar8d5hDko/QBahpc+hw/PKhcd oBYKCigNghv4HCYoycPZ5AFJH3xRBwtfqXT2Y9RkWggYrqUQHyLY7mBBt 1hQ4Vg0b1m0ZWBZNFQKFVhC+W4gnlceLtpMOFT/KevScrs1kUO/t8U3TH 4qtTwARcGealymmJSMG+gY0pZQcpfPkNsyFdPP2A9AItkLeaYO+i8OinZ g==; X-IronPort-AV: E=McAfee;i="6200,9189,10258"; a="313421949" X-IronPort-AV: E=Sophos;i="5.88,368,1635231600"; d="scan'208";a="313421949" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Feb 2022 09:38:09 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,368,1635231600"; d="scan'208";a="635326107" Received: from fmsmsx604.amr.corp.intel.com ([10.18.126.84]) by orsmga004.jf.intel.com with ESMTP; 14 Feb 2022 09:38:09 -0800 Received: from fmsmsx609.amr.corp.intel.com (10.18.126.89) by fmsmsx604.amr.corp.intel.com (10.18.126.84) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20; Mon, 14 Feb 2022 09:38:08 -0800 Received: from fmsmsx609.amr.corp.intel.com (10.18.126.89) by fmsmsx609.amr.corp.intel.com (10.18.126.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20; Mon, 14 Feb 2022 09:38:08 -0800 Received: from fmsedg602.ED.cps.intel.com (10.1.192.136) by fmsmsx609.amr.corp.intel.com (10.18.126.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20 via Frontend Transport; Mon, 14 Feb 2022 09:38:08 -0800 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (104.47.56.173) by edgegateway.intel.com (192.55.55.71) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2308.20; Mon, 14 Feb 2022 09:38:07 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kmgn6g+DziNHUdw4iVw+4cwGwlM/Zw3PNMsEShhOLybA9ldogOg9egJFHy+XwGm+r8Mi2kJk6Yf0B74SoYluqWO62yjw/N/i0sKmUMWcFJhtFpaUC3AI92L/1se/PR3cMERMqyn8GHmy/2mwsAjNavneNTKSdLDJqZ+5k94+zIZvxWqSdUdaXB4FlhJof4mr19UmsVuGn+w4OZZLRPg5nHf0+iSnfgimFP04YcxruJ17TFF2C8priGE+d4dbEEMcFcspBfAytcuQLR9ynVfzCYxnyXpvL1aiV5RJUpS3SyArUZQkv9GtS0aFLF1ia5QRrOA0J68ZfATJ1AdydvcKbg== 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=4lNoRcnqylDlF2azvRKZ3AIsjC9fnhY3CPSMlQlbsKk=; b=Hel9p1ISCs0ttFttXfkt/Boa8cIyJQbDWGtDOMaKI/06fSlKm7PfTtuXM1ZwWsKRdk427zcGrmDkergZkeNeeIyYvOgFb3b3MPhZ2Y1iQkLnKscTHguewFuK3UstWX2afUHHS3/wMmjBejPkNaTeb3jeDhK/GAAnDj2rM4XcyD1uL3RgV9V+kJD/87SKjrPPwtrg3nnRtYrWPgSRtP8fAkcZ6x56ppJmnaPB6GdZ/8+Vy+WqGAdKpo1rsxQE5dYZe50SCh6WYvyHeLtIoDK+Ij/XA+8/xzjSCkix1hPQmzr7H3qk+BVvdj+Xs9A2e4nf27BD0ZsHrwr9iPrquyb2NQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from BYAPR11MB3622.namprd11.prod.outlook.com (2603:10b6:a03:fe::30) by DM5PR1101MB2156.namprd11.prod.outlook.com (2603:10b6:4:51::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4975.19; Mon, 14 Feb 2022 17:38:03 +0000 Received: from BYAPR11MB3622.namprd11.prod.outlook.com ([fe80::c461:5de1:9cf:23cf]) by BYAPR11MB3622.namprd11.prod.outlook.com ([fe80::c461:5de1:9cf:23cf%7]) with mapi id 15.20.4975.019; Mon, 14 Feb 2022 17:38:03 +0000 From: "Guo Dong" To: "Rhodes, Sean" , "devel@edk2.groups.io" CC: Patrick Rudolph , "Ma, Maurice" , "Ni, Ray" , "You, Benjamin" Subject: Re: [PATCH 1/2] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Thread-Topic: [PATCH 1/2] UefiPayloadPkg: Add support for Firmware Volume Block Protocol Thread-Index: AQHYH3CP7PbOjjAO6UOFRhl+OtWLDqyTTq8Q Date: Mon, 14 Feb 2022 17:38:03 +0000 Message-ID: References: <20c774e720e2d7f1bbc1a6bff73d752b624e9bba.1644602106.git.sean@starlabs.systems> In-Reply-To: <20c774e720e2d7f1bbc1a6bff73d752b624e9bba.1644602106.git.sean@starlabs.systems> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: dlp-product: dlpe-windows dlp-reaction: no-action dlp-version: 11.6.200.16 authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 4454cf17-963d-4029-2e15-08d9efe0c0da x-ms-traffictypediagnostic: DM5PR1101MB2156:EE_ x-ld-processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:1201; x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: dDxF02xoQK7ObKjteQGGQa2gmev16ITnpA5aZ8ReSjwSu0+oysxhX9A7Yv9MSLVt80OoEqbjRcvNu+aMfR8KczXt6Mb5Luij8s6usBnNe5VAG+d9V1nAlM23imlgpd69ob3fbLSjr1JMWHUxHw8rYRMwWYnBQKMMNzi6AjYkSf2ZjYtRXHxcOAjUlAhSPX5hmDHBYkarjFgo2BzYEjGCmJt60EH6bc65T0cizoZOKiPwmMqhcREl7gjsA2dB6ZHh4XL2IC8J2J3gBld1uewbCF/XFXIp4isUCk65neFuS9AP7WbIEfvgaSmdqQD9oiKfJA07AVwfzOG7XpQv6TutOFx9g5ztCuFNhdNbMuuUwK7xsW8z8xZ5QZ0C0B568ZYcOgjPFeflMFog377nv2CVJV/BaD9h/sXfSXTfEOo1Ufz1zuFLz1pYJSY4hLNNPoaIQ0IhjYgLD5T8cxJCFr4ukjEkaUDT/WfRIsCunpkCxC2vZ1D6qlsr5dyEfxjbU68fCTNu3FfCMhFONbGqDQkP8rt/CoB1jWEDVYv857Gsj0NeC7REwK0S2M5ApEgXcogzJMASuWja+quCdtTGVX+hE69U3BSXFIWoK11VhNmNBUmEcjIMCQ8qNxzer5NNrLHpK8hUDZ3uY0R9u7UtuygsBm+Rw0DtUXi/7UXUaY1gbOPH7KuQwCMtXoq7ixrfT317UmPhXfz+5TD2eC+Uz9eax8IgAYQb2OC+Nvl79kpD/Tc= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR11MB3622.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(366004)(2906002)(82960400001)(38070700005)(122000001)(38100700002)(19627235002)(30864003)(52536014)(508600001)(8676002)(5660300002)(8936002)(4326008)(66946007)(76116006)(66556008)(66476007)(66446008)(64756008)(54906003)(110136005)(316002)(107886003)(83380400001)(53546011)(9686003)(7696005)(6506007)(71200400001)(186003)(86362001)(33656002)(55016003)(579004)(559001)(44824005);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?i93Lf+FE1IBaWx72qFixGnl4gezXIRk+pJcVQPsWYqzqMYGqqGuzS36mldjX?= =?us-ascii?Q?2VWCHyTYaCyp7R9NYqTkafv9OIdqL4frU/UjqWmDs0QxLeqKkdSVrn3FS+Ea?= =?us-ascii?Q?7Hg0QXe/0eBIc97A29S+jhSBag5fhMh4GPaKIW9qYgBKahNZbu3jtaAKElwf?= =?us-ascii?Q?eNYnMC/GyJbHZGUErk8GNyG8F6S7vOxD5kxHZRXK4mawmw7eWngUGIzOw026?= =?us-ascii?Q?Eb1+aN0MHlYHzATphTJLfefcTAcBvyjXwoC/cPiygOdFqbWijSkafhWfXnvL?= =?us-ascii?Q?cH0PqN8JRyIid+Me3mwzUPdjRKn/R06knYVSaLg9eecjDrkKX0U3nck4GEja?= =?us-ascii?Q?3F/uQ3tfI7LmYPAUWh1g/EG1ZuP9Al7KklWwb9EmumK4flLfGCUUYCDBWPxk?= =?us-ascii?Q?sTZU6GFFwJo+L2qXJvpwn3tqpJve3Xk4HstDEX+ONaREbIxTVlVyDa4aO95N?= =?us-ascii?Q?1gef4QWwr8jvZZ/Y3dPV5OmVufZtvy6bVTKYbKqM/YawEPOExEV95F61seZC?= =?us-ascii?Q?QGYzQWGPNLMtV2EI7cVxku4XcoAJ2LkapRbcVADmum1Umo3SF8U57uYR+uTH?= =?us-ascii?Q?MQIobAt/Ll+m5qpYi2VJKn30FXQYm0kkQj6bG6fuAM4AnHN3bXLGXzCDOBky?= =?us-ascii?Q?tayQifXPpd8bUsyGB2Jwfd6bFnGjkMLMTwnclKFWEeviD51zmE5FJY40nm22?= =?us-ascii?Q?byQIaWFU6NNNs6UUBBRZgd13qZgvUCcHemSxKZFS7HPKQXHIMHY2spDdtmvo?= =?us-ascii?Q?VzbVEvPYHlqbQcMts6ymdYM6HeK3xpScDdxckEvkAOf5EkBXkYFhdjypJ4h7?= =?us-ascii?Q?2uIrV28Ona4slme8LxwaHmmSky6TbRTTEw/IGv8uq4YQZwzaZo4X+Ufrl04R?= =?us-ascii?Q?foUhDwefp+19/LMiOQPIFHUjB9uIg6q/M2HhB+iEFdLTjJS0S85Q3b31tt3n?= =?us-ascii?Q?2sUiu1uKdBpCi6cmCAphBr9Ci8aFMavG6HZRP0HquQbW2NnWHhub+PiOkCjp?= =?us-ascii?Q?7hnIAG5p/x1mPnRx+FXlpbqg7C/5Ka7VCTDVVwv8Lv6F0rxoTY28+cAoDz4n?= =?us-ascii?Q?Zo+vv00WiaGvyyislCUld2a6HvxE2/bd5CJD4Ocr50uHilzPmOAgykFnJJvy?= =?us-ascii?Q?REcPM2RC1rtoS5GGpfwd9NGDCvql34sA1RooRP2qLaRCIeNuoOUwDF5xu8gJ?= =?us-ascii?Q?BdSAfGddTRGotdJMzEM22F9ZGS7ElaL3lVdpWQ4D4yzvumNleiCoIZQYHxQJ?= =?us-ascii?Q?hklhmjDlk6Jckv17u+hHWsXz1Tnf7fk+D0VjVSuN5B479E5pTRCfuVXdtsAR?= =?us-ascii?Q?os2RI2PF4FP8b5U6E+p2lv1x+MEG2FM7qE8jY4vRrnNYo1LmCBduGfeTiQlq?= =?us-ascii?Q?NNe9JYd6voyy/dSzLgAmfYYzLKq7r5RXxfsYIniWovCuFS0FtdNb3wErAcXh?= =?us-ascii?Q?R2I9oZ726RMNwVdwKQGFH6Bvlg/LNhjZvz9bcvsg4uJuws6C4CX1BBRPBW7B?= =?us-ascii?Q?ZIOxK/pa4/HBkq0J4cKaDvhDH+w6JRwXwylQvWj0yK0dMu/fR2SsDZs8DJcT?= =?us-ascii?Q?GlU7WlOGZCj5MCCMvS+AX+d4JeOhAiADfhmUElVZORIeV7MmfdHARe82MIaM?= =?us-ascii?Q?sIOBighOO11HRprzrT/bC4SrMbR91RRAKP8VqsbEtXHBQVkAegMggYJvpgeV?= =?us-ascii?Q?NyHZlxvjCIRW9drvLmTdDBevveg=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: BYAPR11MB3622.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4454cf17-963d-4029-2e15-08d9efe0c0da X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Feb 2022 17:38:03.5154 (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: KRtg+GIAZtR8MNUPY3QYqeVELFUHXYGvBlUYk1uT1o5cJfT2OhWyO4Zc2MqVkZ3uE3rnD00uvfV0Ngv3lv0jdg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1101MB2156 Return-Path: guo.dong@intel.com X-OriginatorOrg: intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Sean, This is a big patch. Here are some high level comments. For BlSMMStoreDxe/BlSMMStoreDxe.inf, how about rename it to SmmStoreFvb/Smm= StoreFvbRuntimeDxe.inf with below reasons? a) We don't need "Bl" since Payload would always work with bootloader.=20 b) We could keep "SMMStore" but I prefer using "SmmStore" to align with EDK= 2 naming style. c) Prefer adding Fvb to indicate this is a module to install FVB protocol. d) this module is a runtime DXE module.=20 For "UefiPayloadPkg/Include/Library/SMMStoreLib.h", change to "UefiPayloadP= kg/Include/Library/SmmStoreLib.h", And we don't need have different bootloader implementation as below. .../Library/CbSMMStoreLib/CbSMMStoreLib.inf=20 .../Library/CbSMMStoreLib/CorebootSMMStore.c=20 .../Library/SblSMMStoreLib/SblSMMStore.c =20 .../Library/SblSMMStoreLib/SblSMMStoreLib.inf I prefer having a common implantation "Library/SmmStoreLib/SmmStoreLib.inf"= that as long as the bootloader provides the required HOB defined in SmmStoreInfoGuid.h, this library would work with bootloader = on the SMM store operation. You updated ParseLib as below to get required bootloader info. BlParseLib.h= is a legacy APIs while UEFI payload is trending to support universal payload specification. So I prefer not changing this Library if = possible. How about you provide a new PlatformSupportLib instance to parse additional coreboot info?=20 UefiPayloadPkg/Include/Library/BlParseLib.h .../Library/CbParseLib/CbParseLib.c=20 .../Library/SblParseLib/SblParseLib.c =20 Thanks, Guo -----Original Message----- From: Sean Rhodes =20 Sent: Friday, February 11, 2022 10:55 AM To: devel@edk2.groups.io Cc: Dong, Guo ; Patrick Rudolph Subject: [PATCH 1/2] UefiPayloadPkg: Add support for Firmware Volume Block = Protocol From: Patrick Rudolph This adds support for FVB in order to support a platform independent and non-volatile variable store on UefiPayloadPkg. It is required for non-volatile variable support, TPM support, Secureboot support and more. Since commit bc744f5893fc4d53275ed26dd8d968011c6a09c1 coreboot supports the SMMSTORE v2 feature. It implements a SMI handler that is able to write, read and erase pages in the boot media (SPI flash). The communication is done using a fixed communication buffer that is allocated in CBMEM. The existence of this optional feature is advertised by a coreboot table. When the SMMSTORE feature is not available the variable emulation is used by setting PcdEmuVariableNvModeEnable to TRUE. Add a library for SMMStore to be used in DXE. The DXE component provides runtime services and takes care of virtual to physical mapping the communication buffers between SMM and OS. Make use of the APRIORI DXE to initialize an empty store on the first boot and set the PCDs to sane values before the variable driver is loaded. Tests on Intel(R) Xeon(R) E-2288G CPU @ 3.70G showed that the SMI isn't triggered with a probability of 1:40 of all cases when called in a tight loop. The CPU continues running and the SMI is triggeres asynchronously a few clock cycles later. coreboot only handels synchronous APM request and does nothing on asynchronous APM triggers. As there's no livesign from SMM it's impossible to tell if the handler has run. Just wait a bit and try again to trigger a synchronous SMI. Tests confirmed that out of 5 million tries the SMI is now always handled. Tested on Linux and Windows 10 on real hardware. Currently this cannot be tested on coreboot and qemu as it doesn't support the SMMSTORE on qemu. Signed-off-by: Patrick Rudolph --- UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.c | 352 ++++++++ UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.h | 118 +++ .../BlSMMStoreDxe/BlSMMStoreDxe.inf | 62 ++ .../BlSMMStoreDxe/BlSMMStoreFvbDxe.c | 834 ++++++++++++++++++ UefiPayloadPkg/Include/Coreboot.h | 13 + .../Include/Guid/SMMSTOREInfoGuid.h | 27 + UefiPayloadPkg/Include/Library/BlParseLib.h | 16 + UefiPayloadPkg/Include/Library/SMMStoreLib.h | 98 ++ .../Library/CbParseLib/CbParseLib.c | 40 + .../Library/CbSMMStoreLib/CbSMMStoreLib.inf | 28 + .../Library/CbSMMStoreLib/CorebootSMMStore.c | 317 +++++++ .../Library/SblParseLib/SblParseLib.c | 15 + .../Library/SblSMMStoreLib/SblSMMStore.c | 100 +++ .../Library/SblSMMStoreLib/SblSMMStoreLib.inf | 28 + .../UefiPayloadEntry/UefiPayloadEntry.c | 13 + .../UefiPayloadEntry/UefiPayloadEntry.h | 1 + .../UefiPayloadEntry/UefiPayloadEntry.inf | 2 + UefiPayloadPkg/UefiPayloadPkg.dec | 9 + UefiPayloadPkg/UefiPayloadPkg.dsc | 31 +- 19 files changed, 2095 insertions(+), 9 deletions(-) create mode 100644 UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.c create mode 100644 UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.h create mode 100644 UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.inf create mode 100644 UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreFvbDxe.c create mode 100644 UefiPayloadPkg/Include/Guid/SMMSTOREInfoGuid.h create mode 100644 UefiPayloadPkg/Include/Library/SMMStoreLib.h create mode 100644 UefiPayloadPkg/Library/CbSMMStoreLib/CbSMMStoreLib.inf create mode 100644 UefiPayloadPkg/Library/CbSMMStoreLib/CorebootSMMStore.c create mode 100644 UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStore.c create mode 100644 UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStoreLib.in= f diff --git a/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.c b/UefiPayloadPkg/= BlSMMStoreDxe/BlSMMStoreDxe.c new file mode 100644 index 0000000000..ca2b0c682b --- /dev/null +++ b/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.c @@ -0,0 +1,352 @@ +/** @file BlSMMStoreDxe.c + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "BlSMMStoreDxe.h" + +STATIC EFI_EVENT mSMMStoreVirtualAddrChangeEvent; + +// +// Global variable declarations +// +SMMSTORE_INSTANCE *mSMMStoreInstance; + +SMMSTORE_INSTANCE mSMMStoreInstanceTemplate =3D { + SMMSTORE_SIGNATURE, // Signature + NULL, // Handle ... NEED TO BE FILLED + { + 0, // MediaId ... NEED TO BE FILLED + FALSE, // RemovableMedia + TRUE, // MediaPresent + FALSE, // LogicalPartition + FALSE, // ReadOnly + FALSE, // WriteCaching; + 0, // BlockSize ... NEED TO BE FILLED + 4, // IoAlign + 0, // LastBlock ... NEED TO BE FILLED + 0, // LowestAlignedLba + 1, // LogicalBlocksPerPhysicalBlock + }, // Media; + + { + FvbGetAttributes, // GetAttributes + FvbSetAttributes, // SetAttributes + FvbGetPhysicalAddress, // GetPhysicalAddress + FvbGetBlockSize, // GetBlockSize + FvbRead, // Read + FvbWrite, // Write + FvbEraseBlocks, // EraseBlocks + NULL, // ParentHandle + }, // FvbProtoccol; + { + { + { + HARDWARE_DEVICE_PATH, + HW_VENDOR_DP, + { + (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End)), + (UINT8)(OFFSET_OF (NOR_FLASH_DEVICE_PATH, End) >> 8) + } + }, + { 0x0, 0x0, 0x0, { 0x0, 0x0, 0x0, 0x0,= 0x0, 0x0, 0x0, 0x0 } + }, // GU= ID ... NEED TO BE FILLED + }, + 0, // Index + { + END_DEVICE_PATH_TYPE, + END_ENTIRE_DEVICE_PATH_SUBTYPE, + { sizeof (EFI_DEVICE_PATH_PROTOCOL), 0 } + } + } // DevicePath +}; + +STATIC +EFI_STATUS +SMMStoreCreateInstance ( + IN UINTN NumberofBlocks, + IN UINTN BlockSize, + OUT SMMSTORE_INSTANCE **SMMStoreInstance + ) +{ + EFI_STATUS Status; + SMMSTORE_INSTANCE *Instance; + + ASSERT (SMMStoreInstance !=3D NULL); + + Instance =3D AllocateRuntimeCopyPool (sizeof (SMMSTORE_INSTANCE), &mSMMS= toreInstanceTemplate); + if (Instance =3D=3D NULL) { + return EFI_OUT_OF_RESOURCES; + } + + Instance->Media.MediaId =3D 0; + Instance->Media.BlockSize =3D BlockSize; + Instance->Media.LastBlock =3D NumberofBlocks - 1; + + CopyGuid (&Instance->DevicePath.Vendor.Guid, &gEfiCallerIdGuid); + Instance->DevicePath.Index =3D (UINT8)0; + + Status =3D SMMStoreFvbInitialize (Instance); + if (EFI_ERROR (Status)) { + FreePool (Instance); + return Status; + } + + Status =3D gBS->InstallMultipleProtocolInterfaces ( + &Instance->Handle, + &gEfiDevicePathProtocolGuid, + &Instance->DevicePath, + &gEfiFirmwareVolumeBlockProtocolGuid, + &Instance->FvbProtocol, + NULL + ); + if (EFI_ERROR (Status)) { + FreePool (Instance); + return Status; + } + + DEBUG ((DEBUG_INFO, "%a: Created a new instance\n", __FUNCTION__)); + + *SMMStoreInstance =3D Instance; + return Status; +} + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers in + lib to virtual mode. + + @param[in] Event The Event that is being processed + @param[in] Context Event Context +**/ +VOID +EFIAPI +BlSMMStoreVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // Convert Fvb + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.EraseBl= ocks); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.GetAttr= ibutes); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.GetBloc= kSize); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.GetPhys= icalAddress); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.Read); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.SetAttr= ibutes); + EfiConvertPointer (0x0, (VOID **)&mSMMStoreInstance->FvbProtocol.Write); + + SMMStoreVirtualNotifyEvent (Event, Context); + + return; +} + +EFI_STATUS +EFIAPI +BlSMMSTOREInitialise ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + VOID *ComBuf; + VOID *GuidHob; + SMMSTORE_INFO *SMMStoreInfoHob; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR GcdDescriptor; + + if (PcdGetBool (PcdEmuVariableNvModeEnable)) { + DEBUG ((DEBUG_WARN, "Variable emulation is active! Skipping driver ini= t.\n")); + return EFI_SUCCESS; + } + + // + // Find the SMMSTORE information guid hob + // + GuidHob =3D GetFirstGuidHob (&gEfiSMMSTOREInfoHobGuid); + if (GuidHob =3D=3D NULL) { + DEBUG ((DEBUG_WARN, "SMMSTORE not supported! Skipping driver init.\n")= ); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return EFI_SUCCESS; + } + + // + // Allocate Communication Buffer for arguments to pass to SMM + // + ComBuf =3D AllocateRuntimePool (SMMSTORE_COMBUF_SIZE); + if (!ComBuf) { + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return EFI_OUT_OF_RESOURCES; + } + + // + // Place SMMSTORE information hob in a runtime buffer + // + SMMStoreInfoHob =3D AllocateRuntimePool (GET_GUID_HOB_DATA_SIZE (GuidHob= )); + if (!SMMStoreInfoHob) { + FreePool (ComBuf); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return EFI_OUT_OF_RESOURCES; + } + + CopyMem (SMMStoreInfoHob, GET_GUID_HOB_DATA (GuidHob), GET_GUID_HOB_DATA= _SIZE (GuidHob)); + + if (!SMMStoreInfoHob->MmioAddress || + !SMMStoreInfoHob->ComBuffer || + !SMMStoreInfoHob->BlockSize || + !SMMStoreInfoHob->NumBlocks) + { + DEBUG ((DEBUG_ERROR, "%a: Invalid data in SMMStore Info hob\n", __FUNC= TION__)); + FreePool (ComBuf); + FreePool (SMMStoreInfoHob); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return EFI_WRITE_PROTECTED; + } + + // + // Update PCDs for VariableRuntimeDxe + // Can't do it later as VariableRuntimeDxe has no Depex + // + PcdSet32S ( + PcdFlashNvStorageVariableBase, + PcdGet32 (PcdFlashNvStorageVariableBase) + SMMStoreInfoHob->MmioAddres= s + ); + PcdSet32S ( + PcdFlashNvStorageFtwWorkingBase, + PcdGet32 (PcdFlashNvStorageFtwWorkingBase) + SMMStoreInfoHob->MmioAddr= ess + ); + PcdSet32S ( + PcdFlashNvStorageFtwSpareBase, + PcdGet32 (PcdFlashNvStorageFtwSpareBase) + SMMStoreInfoHob->MmioAddres= s + ); + + Status =3D SMMStoreInitialize (ComBuf, SMMStoreInfoHob); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: Failed to initialize SMMStore\n", + __FUNCTION__ + )); + FreePool (ComBuf); + FreePool (SMMStoreInfoHob); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return Status; + } + + mSMMStoreInstance =3D AllocateRuntimePool (sizeof (SMMSTORE_INSTANCE *))= ; + if (!mSMMStoreInstance) { + DEBUG ((DEBUG_ERROR, "%a: Out of resources\n", __FUNCTION__)); + FreePool (ComBuf); + FreePool (SMMStoreInfoHob); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return EFI_OUT_OF_RESOURCES; + } + + Status =3D SMMStoreCreateInstance ( + SMMStoreInfoHob->NumBlocks, + SMMStoreInfoHob->BlockSize, + &mSMMStoreInstance + ); + if (EFI_ERROR (Status)) { + DEBUG (( + DEBUG_ERROR, + "%a: Fail to create instance for SMMStore\n", + __FUNCTION__ + )); + FreePool (ComBuf); + FreePool (SMMStoreInfoHob); + PcdSetBoolS (PcdEmuVariableNvModeEnable, TRUE); + return Status; + } + + // + // Register for the virtual address change event + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + BlSMMStoreVirtualNotifyEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mSMMStoreVirtualAddrChangeEvent + ); + ASSERT_EFI_ERROR (Status); + + // + // Finally mark the SMM communication buffer provided by CB or SBL as ru= ntime memory + // + Status =3D gDS->GetMemorySpaceDescriptor (SMMStoreInfoHob->ComBuffer, &G= cdDescriptor); + if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType !=3D EfiGcdMemory= TypeReserved)) { + DEBUG (( + DEBUG_INFO, + "%a: No memory space descriptor for com buffer found\n", + __FUNCTION__ + )); + + // + // Add a new entry if not covered by existing mapping + // + Status =3D gDS->AddMemorySpace ( + EfiGcdMemoryTypeReserved, + SMMStoreInfoHob->ComBuffer, + SMMStoreInfoHob->ComBufferSize, + EFI_MEMORY_WB | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + } + + // + // Mark as runtime service + // + Status =3D gDS->SetMemorySpaceAttributes ( + SMMStoreInfoHob->ComBuffer, + SMMStoreInfoHob->ComBufferSize, + EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + // + // Mark the memory mapped store as MMIO memory + // + Status =3D gDS->GetMemorySpaceDescriptor (SMMStoreInfoHob->MmioAddress, = &GcdDescriptor); + if (EFI_ERROR (Status) || (GcdDescriptor.GcdMemoryType !=3D EfiGcdMemory= TypeMemoryMappedIo)) { + DEBUG (( + DEBUG_INFO, + "%a: No memory space descriptor for com buffer found\n", + __FUNCTION__ + )); + + // + // Add a new entry if not covered by existing mapping + // + Status =3D gDS->AddMemorySpace ( + EfiGcdMemoryTypeMemoryMappedIo, + SMMStoreInfoHob->MmioAddress, + SMMStoreInfoHob->NumBlocks * SMMStoreInfoHob->BlockSiz= e, + EFI_MEMORY_UC | EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + } + + // + // Mark as runtime service + // + Status =3D gDS->SetMemorySpaceAttributes ( + SMMStoreInfoHob->MmioAddress, + SMMStoreInfoHob->NumBlocks * SMMStoreInfoHob->BlockSize, + EFI_MEMORY_RUNTIME + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.h b/UefiPayloadPkg/= BlSMMStoreDxe/BlSMMStoreDxe.h new file mode 100644 index 0000000000..87fcbc3894 --- /dev/null +++ b/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.h @@ -0,0 +1,118 @@ +/** @file BlSMMStoreDxe.h + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __COREBOOT_SMM_STORE_DXE_H__ +#define __COREBOOT_SMM_STORE_DXE_H__ + + +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#define SMMSTORE_SIGNATURE SIGNATURE_32('S', 'M', 'M= ', 'S') +#define INSTANCE_FROM_FVB_THIS(a) CR(a, SMMSTORE_INSTANCE, = FvbProtocol, SMMSTORE_SIGNATURE) + +typedef struct _SMMSTORE_INSTANCE SMMSTORE_INSTANCE; + +#pragma pack (1) +typedef struct { + VENDOR_DEVICE_PATH Vendor; + UINT8 Index; + EFI_DEVICE_PATH_PROTOCOL End; +} NOR_FLASH_DEVICE_PATH; +#pragma pack () + +struct _SMMSTORE_INSTANCE { + UINT32 Signature; + EFI_HANDLE Handle; + EFI_BLOCK_IO_MEDIA Media; + + EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL FvbProtocol; + + NOR_FLASH_DEVICE_PATH DevicePath; +}; + +// +// BlSMMStoreFvbDxe.c +// + +EFI_STATUS +EFIAPI +SMMStoreFvbInitialize ( + IN SMMSTORE_INSTANCE* Instance + ); + +EFI_STATUS +EFIAPI +FvbGetAttributes( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ); + +EFI_STATUS +EFIAPI +FvbSetAttributes( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ); + +EFI_STATUS +EFIAPI +FvbGetPhysicalAddress( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_PHYSICAL_ADDRESS *Address + ); + +EFI_STATUS +EFIAPI +FvbGetBlockSize( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + OUT UINTN *BlockSize, + OUT UINTN *NumberOfBlocks + ); + +EFI_STATUS +EFIAPI +FvbRead( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN OUT UINT8 *Buffer + ); + +EFI_STATUS +EFIAPI +FvbWrite( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN UINT8 *Buffer + ); + +EFI_STATUS +EFIAPI +FvbEraseBlocks( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + ... + ); + + +#endif /* __COREBOOT_SMM_STORE_DXE_H__ */ diff --git a/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.inf b/UefiPayloadPk= g/BlSMMStoreDxe/BlSMMStoreDxe.inf new file mode 100644 index 0000000000..823a191de6 --- /dev/null +++ b/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreDxe.inf @@ -0,0 +1,62 @@ +#/** @file +# +# Component description file for SMMSTORE module +# +# Copyright (c) 2020, 9elements Agency GmbH
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +#**/ +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D UefiPayloadBlSMMStoreDxe + FILE_GUID =3D A0402FCA-6B25-4CEA-B7DD-C08F99714B29 + MODULE_TYPE =3D DXE_RUNTIME_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D BlSMMSTOREInitialise + +[Sources.common] + BlSMMStoreDxe.h + BlSMMStoreDxe.c + BlSMMStoreFvbDxe.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + EmbeddedPkg/EmbeddedPkg.dec + UefiPayloadPkg/UefiPayloadPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + HobLib + SmmStoreLib + UefiLib + UefiDriverEntryPoint + UefiBootServicesTableLib + UefiRuntimeLib + DxeServicesTableLib + +[Guids] + gEfiSystemNvDataFvGuid + gEfiVariableGuid + gEfiAuthenticatedVariableGuid + gEfiEventVirtualAddressChangeGuid + gEdkiiNvVarStoreFormattedGuid ## PRODUCES ## PROTOCOL + gEfiSMMSTOREInfoHobGuid ## CONSUMES + +[Protocols] + gEfiDevicePathProtocolGuid ## BY_START + gEfiFirmwareVolumeBlockProtocolGuid ## BY_START + +[Pcd] + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize + gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable + +[Depex] + BEFORE gVariableRuntimeDxeFileGuid \ No newline at end of file diff --git a/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreFvbDxe.c b/UefiPayloadP= kg/BlSMMStoreDxe/BlSMMStoreFvbDxe.c new file mode 100644 index 0000000000..8337a1b5e3 --- /dev/null +++ b/UefiPayloadPkg/BlSMMStoreDxe/BlSMMStoreFvbDxe.c @@ -0,0 +1,834 @@ +/*++ @file BlSMMStoreFvbDxe.c + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + + --*/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "BlSMMStoreDxe.h" + +STATIC EFI_EVENT mFvbVirtualAddrChangeEvent; +STATIC UINTN mFlashNvStorageVariableBase; + +/// +/// The Firmware Volume Block Protocol is the low-level interface +/// to a firmware volume. File-level access to a firmware volume +/// should not be done using the Firmware Volume Block Protocol. +/// Normal access to a firmware volume must use the Firmware +/// Volume Protocol. Typically, only the file system driver that +/// produces the Firmware Volume Protocol will bind to the +/// Firmware Volume Block Protocol. +/// + +/** + Initialises the FV Header and Variable Store Header + to support variable operations. + + @param[in] Ptr - Location to initialise the headers + +**/ +EFI_STATUS +InitializeFvAndVariableStoreHeaders ( + IN SMMSTORE_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + VOID *Headers; + UINTN HeadersLength; + EFI_FIRMWARE_VOLUME_HEADER *FirmwareVolumeHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + + HeadersLength =3D sizeof (EFI_FIRMWARE_VOLUME_HEADER) + sizeof (EFI_FV_B= LOCK_MAP_ENTRY) + sizeof (VARIABLE_STORE_HEADER); + Headers =3D AllocateZeroPool (HeadersLength); + + // FirmwareVolumeHeader->FvLength is declared to have the Variable area = AND the FTW working area AND the FTW Spare contiguous. + ASSERT (PcdGet32 (PcdFlashNvStorageVariableBase) + PcdGet32 (PcdFlashNvS= torageVariableSize) =3D=3D PcdGet32 (PcdFlashNvStorageFtwWorkingBase)); + ASSERT (PcdGet32 (PcdFlashNvStorageFtwWorkingBase) + PcdGet32 (PcdFlashN= vStorageFtwWorkingSize) =3D=3D PcdGet32 (PcdFlashNvStorageFtwSpareBase)); + + // Check if the size of the area is at least one block size + ASSERT ((PcdGet32 (PcdFlashNvStorageVariableSize) > 0) && (PcdGet32 (Pcd= FlashNvStorageVariableSize) / Instance->Media.BlockSize > 0)); + ASSERT ((PcdGet32 (PcdFlashNvStorageFtwWorkingSize) > 0) && (PcdGet32 (P= cdFlashNvStorageFtwWorkingSize) / Instance->Media.BlockSize > 0)); + ASSERT ((PcdGet32 (PcdFlashNvStorageFtwSpareSize) > 0) && (PcdGet32 (Pcd= FlashNvStorageFtwSpareSize) / Instance->Media.BlockSize > 0)); + + // + // EFI_FIRMWARE_VOLUME_HEADER + // + FirmwareVolumeHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)Headers; + CopyGuid (&FirmwareVolumeHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid= ); + FirmwareVolumeHeader->FvLength =3D + PcdGet32 (PcdFlashNvStorageVariableSize) + + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + + PcdGet32 (PcdFlashNvStorageFtwSpareSize); + FirmwareVolumeHeader->Signature =3D EFI_FVH_SIGNATURE; + FirmwareVolumeHeader->Attributes =3D (EFI_FVB_ATTRIBUTES_2)( + EFI_FVB2_READ_= ENABLED_CAP | // Reads may be enabled + EFI_FVB2_READ_= STATUS | // Reads are currently enabled + EFI_FVB2_STICK= Y_WRITE | // A block erase is required to flip bits into EFI_FVB2_ERA= SE_POLARITY + EFI_FVB2_MEMOR= Y_MAPPED | // It is memory mapped + EFI_FVB2_ERASE= _POLARITY | // After erasure all bits take this value (i.e. '1') + EFI_FVB2_WRITE= _STATUS | // Writes are currently enabled + EFI_FVB2_WRITE= _ENABLED_CAP // Writes may be enabled + ); + FirmwareVolumeHeader->HeaderLength =3D sizeof (EFI_FIRMWARE_VOL= UME_HEADER) + sizeof (EFI_FV_BLOCK_MAP_ENTRY); + FirmwareVolumeHeader->Revision =3D EFI_FVH_REVISION; + FirmwareVolumeHeader->BlockMap[0].NumBlocks =3D Instance->Media.LastBloc= k + 1; + FirmwareVolumeHeader->BlockMap[0].Length =3D Instance->Media.BlockSiz= e; + FirmwareVolumeHeader->BlockMap[1].NumBlocks =3D 0; + FirmwareVolumeHeader->BlockMap[1].Length =3D 0; + FirmwareVolumeHeader->Checksum =3D CalculateCheckSum16 ((UI= NT16 *)FirmwareVolumeHeader, FirmwareVolumeHeader->HeaderLength); + + // + // VARIABLE_STORE_HEADER + // + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *)((UINTN)Headers + Firmw= areVolumeHeader->HeaderLength); + CopyGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVariableGui= d); + VariableStoreHeader->Size =3D PcdGet32 (PcdFlashNvStorageVariableSize)= - FirmwareVolumeHeader->HeaderLength; + VariableStoreHeader->Format =3D VARIABLE_STORE_FORMATTED; + VariableStoreHeader->State =3D VARIABLE_STORE_HEALTHY; + + // Install the combined super-header in the store + Status =3D FvbWrite (&Instance->FvbProtocol, 0, 0, &HeadersLength, Heade= rs); + + FreePool (Headers); + return Status; +} + +/** + Check the integrity of firmware volume header. + + @param[in] FwVolHeader - A pointer to a firmware volume header + + @retval EFI_SUCCESS - The firmware volume is consistent + @retval EFI_NOT_FOUND - The firmware volume has been corrupted. + +**/ +EFI_STATUS +ValidateFvHeader ( + IN SMMSTORE_INSTANCE *Instance + ) +{ + UINT16 Checksum; + EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + UINTN VariableStoreLength; + UINTN FvLength; + EFI_STATUS TempStatus; + UINTN BufferSize; + UINTN BufferSizeReqested; + + BufferSizeReqested =3D sizeof (EFI_FIRMWARE_VOLUME_HEADER); + FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (Buffe= rSizeReqested); + if (!FwVolHeader) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize =3D BufferSizeReqested; + TempStatus =3D SMMStoreRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader); + if (EFI_ERROR (TempStatus) || (BufferSizeReqested !=3D BufferSize)) { + FreePool (FwVolHeader); + return EFI_DEVICE_ERROR; + } + + FvLength =3D PcdGet32 (PcdFlashNvStorageVariableSize) + PcdGet32 (PcdFla= shNvStorageFtwWorkingSize) + + PcdGet32 (PcdFlashNvStorageFtwSpareSize); + + // + // Verify the header revision, header signature, length + // Length of FvBlock cannot be 2**64-1 + // HeaderLength cannot be an odd number + // + if ( (FwVolHeader->Revision !=3D EFI_FVH_REVISION) + || (FwVolHeader->Signature !=3D EFI_FVH_SIGNATURE) + || (FwVolHeader->FvLength !=3D FvLength) + ) + { + DEBUG (( + DEBUG_INFO, + "%a: No Firmware Volume header present\n", + __FUNCTION__ + )); + FreePool (FwVolHeader); + return EFI_NOT_FOUND; + } + + // Check the Firmware Volume Guid + if ( CompareGuid (&FwVolHeader->FileSystemGuid, &gEfiSystemNvDataFvGuid)= =3D=3D FALSE ) { + DEBUG (( + DEBUG_INFO, + "%a: Firmware Volume Guid non-compatible\n", + __FUNCTION__ + )); + FreePool (FwVolHeader); + return EFI_NOT_FOUND; + } + + BufferSizeReqested =3D FwVolHeader->HeaderLength; + FreePool (FwVolHeader); + FwVolHeader =3D (EFI_FIRMWARE_VOLUME_HEADER *)AllocatePool (BufferSizeRe= qested); + if (!FwVolHeader) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize =3D BufferSizeReqested; + TempStatus =3D SMMStoreRead (0, 0, &BufferSize, (UINT8 *)FwVolHeader); + if (EFI_ERROR (TempStatus) || (BufferSizeReqested !=3D BufferSize)) { + FreePool (FwVolHeader); + return EFI_DEVICE_ERROR; + } + + // Verify the header checksum + Checksum =3D CalculateSum16 ((UINT16 *)FwVolHeader, FwVolHeader->HeaderL= ength); + if (Checksum !=3D 0) { + DEBUG (( + DEBUG_INFO, + "%a: FV checksum is invalid (Checksum:0x%X)\n", + __FUNCTION__, + Checksum + )); + FreePool (FwVolHeader); + return EFI_NOT_FOUND; + } + + BufferSizeReqested =3D sizeof (VARIABLE_STORE_HEADER); + VariableStoreHeader =3D (VARIABLE_STORE_HEADER *)AllocatePool (BufferSiz= eReqested); + if (!VariableStoreHeader) { + return EFI_OUT_OF_RESOURCES; + } + + BufferSize =3D BufferSizeReqested; + TempStatus =3D SMMStoreRead (0, FwVolHeader->HeaderLength, &BufferSize, = (UINT8 *)VariableStoreHeader); + if (EFI_ERROR (TempStatus) || (BufferSizeReqested !=3D BufferSize)) { + FreePool (VariableStoreHeader); + FreePool (FwVolHeader); + return EFI_DEVICE_ERROR; + } + + // Check the Variable Store Guid + if (!CompareGuid (&VariableStoreHeader->Signature, &gEfiVariableGuid) && + !CompareGuid (&VariableStoreHeader->Signature, &gEfiAuthenticatedVar= iableGuid)) + { + DEBUG (( + DEBUG_INFO, + "%a: Variable Store Guid non-compatible\n", + __FUNCTION__ + )); + FreePool (FwVolHeader); + FreePool (VariableStoreHeader); + return EFI_NOT_FOUND; + } + + VariableStoreLength =3D PcdGet32 (PcdFlashNvStorageVariableSize) - FwVol= Header->HeaderLength; + if (VariableStoreHeader->Size !=3D VariableStoreLength) { + DEBUG (( + DEBUG_INFO, + "%a: Variable Store Length does not match\n", + __FUNCTION__ + )); + FreePool (FwVolHeader); + FreePool (VariableStoreHeader); + return EFI_NOT_FOUND; + } + + FreePool (FwVolHeader); + FreePool (VariableStoreHeader); + + return EFI_SUCCESS; +} + +/** + The GetAttributes() function retrieves the attributes and + current settings of the block. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL ins= tance. + + @param Attributes Pointer to EFI_FVB_ATTRIBUTES_2 in which the attribut= es and + current settings are returned. + Type EFI_FVB_ATTRIBUTES_2 is defined in EFI_FIRMWARE_= VOLUME_HEADER. + + @retval EFI_SUCCESS The firmware volume attributes were returned. + + **/ +EFI_STATUS +EFIAPI +FvbGetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ) +{ + EFI_FVB_ATTRIBUTES_2 FlashFvbAttributes; + SMMSTORE_INSTANCE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + FlashFvbAttributes =3D (EFI_FVB_ATTRIBUTES_2)( + + EFI_FVB2_READ_ENABLED_CAP | = // Reads may be enabled + EFI_FVB2_READ_STATUS | = // Reads are currently enabled + EFI_FVB2_STICKY_WRITE | = // A block erase is required to flip bits into EFI_FVB2_ERASE_POLARITY + EFI_FVB2_MEMORY_MAPPED | = // It is memory mapped + EFI_FVB2_ERASE_POLARITY = // After erasure all bits take this value (i.e. '1') + + ); + + // Check if it is write protected + if (Instance->Media.ReadOnly !=3D TRUE) { + FlashFvbAttributes =3D FlashFvbAttributes | + EFI_FVB2_WRITE_STATUS | // Writes are curren= tly enabled + EFI_FVB2_WRITE_ENABLED_CAP; // Writes may be ena= bled + } + + *Attributes =3D FlashFvbAttributes; + + DEBUG ((DEBUG_BLKIO, "FvbGetAttributes(0x%X)\n", *Attributes)); + + return EFI_SUCCESS; +} + +/** + The SetAttributes() function sets configurable firmware volume attributes + and returns the new settings of the firmware volume. + + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL instance. + + @param Attributes On input, Attributes is a pointer to EFI_= FVB_ATTRIBUTES_2 + that contains the desired firmware volume= settings. + On successful return, it contains the new= settings of + the firmware volume. + Type EFI_FVB_ATTRIBUTES_2 is defined in E= FI_FIRMWARE_VOLUME_HEADER. + + @retval EFI_SUCCESS The firmware volume attributes were retur= ned. + + @retval EFI_INVALID_PARAMETER The attributes requested are in conflict = with the capabilities + as declared in the firmware volume header= . + + **/ +EFI_STATUS +EFIAPI +FvbSetAttributes ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN OUT EFI_FVB_ATTRIBUTES_2 *Attributes + ) +{ + DEBUG ((DEBUG_BLKIO, "FvbSetAttributes(0x%X) is not supported\n", *Attri= butes)); + return EFI_UNSUPPORTED; +} + +/** + The GetPhysicalAddress() function retrieves the base address of + a memory-mapped firmware volume. This function should be called + only for memory-mapped firmware volumes. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROTOC= OL instance. + + @param Address Pointer to a caller-allocated + EFI_PHYSICAL_ADDRESS that, on successful + return from GetPhysicalAddress(), contains the + base address of the firmware volume. + + @retval EFI_SUCCESS The firmware volume base address was returned. + + @retval EFI_NOT_SUPPORTED The firmware volume is not memory mapped. + + **/ +EFI_STATUS +EFIAPI +FvbGetPhysicalAddress ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + OUT EFI_PHYSICAL_ADDRESS *Address + ) +{ + ASSERT (Address !=3D NULL); + + *Address =3D mFlashNvStorageVariableBase; + return EFI_SUCCESS; +} + +/** + The GetBlockSize() function retrieves the size of the requested + block. It also returns the number of additional blocks with + the identical size. The GetBlockSize() function is used to + retrieve the block map (see EFI_FIRMWARE_VOLUME_HEADER). + + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL instance. + + @param Lba Indicates the block for which to return t= he size. + + @param BlockSize Pointer to a caller-allocated UINTN in wh= ich + the size of the block is returned. + + @param NumberOfBlocks Pointer to a caller-allocated UINTN in + which the number of consecutive blocks, + starting with Lba, is returned. All + blocks in this range have a size of + BlockSize. + + + @retval EFI_SUCCESS The firmware volume base address was retu= rned. + + @retval EFI_INVALID_PARAMETER The requested LBA is out of range. + + **/ +EFI_STATUS +EFIAPI +FvbGetBlockSize ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + OUT UINTN *BlockSize, + OUT UINTN *NumberOfBlocks + ) +{ + EFI_STATUS Status; + SMMSTORE_INSTANCE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize(Lba=3D%ld, BlockSize=3D0x%x, LastB= lock=3D%ld)\n", Lba, Instance->Media.BlockSize, Instance->Media.LastBlock))= ; + + if (Lba > Instance->Media.LastBlock) { + DEBUG ((DEBUG_ERROR, "FvbGetBlockSize: ERROR - Parameter LBA %ld is be= yond the last Lba (%ld).\n", Lba, Instance->Media.LastBlock)); + Status =3D EFI_INVALID_PARAMETER; + } else { + *BlockSize =3D (UINTN)Instance->Media.BlockSize; + *NumberOfBlocks =3D (UINTN)(Instance->Media.LastBlock - Lba + 1); + + DEBUG ((DEBUG_BLKIO, "FvbGetBlockSize: *BlockSize=3D0x%x, *NumberOfBlo= cks=3D0x%x.\n", *BlockSize, *NumberOfBlocks)); + + Status =3D EFI_SUCCESS; + } + + return Status; +} + +/** + Reads the specified number of bytes into a buffer from the specified bloc= k. + + The Read() function reads the requested number of bytes from the + requested block and stores them in the provided buffer. + Implementations should be mindful that the firmware volume + might be in the ReadDisabled state. If it is in this state, + the Read() function must return the status code + EFI_ACCESS_DENIED without modifying the contents of the + buffer. The Read() function must also prevent spanning block + boundaries. If a read is requested that would span a block + boundary, the read must read up to the boundary but not + beyond. The output parameter NumBytes must be set to correctly + indicate the number of bytes actually read. The caller must be + aware that a read may be partially completed. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROT= OCOL instance. + + @param Lba The starting logical block index from which t= o read. + + @param Offset Offset into the block at which to begin readi= ng. + + @param NumBytes Pointer to a UINTN. + At entry, *NumBytes contains the total size o= f the buffer. + At exit, *NumBytes contains the total number = of bytes read. + + @param Buffer Pointer to a caller-allocated buffer that wil= l be used + to hold the data that is read. + + @retval EFI_SUCCESS The firmware volume was read successfully, a= nd contents are + in Buffer. + + @retval EFI_BAD_BUFFER_SIZE Read attempted across an LBA boundary. + On output, NumBytes contains the total number= of bytes + returned in Buffer. + + @retval EFI_ACCESS_DENIED The firmware volume is in the ReadDisabled st= ate. + + @retval EFI_DEVICE_ERROR The block device is not functioning correctly= and could not be read. + + **/ +EFI_STATUS +EFIAPI +FvbRead ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN OUT UINT8 *Buffer + ) +{ + UINTN BlockSize; + SMMSTORE_INSTANCE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + DEBUG ((DEBUG_BLKIO, "FvbRead(Parameters: Lba=3D%ld, Offset=3D0x%x, *Num= Bytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer)); + + // Cache the block size to avoid de-referencing pointers all the time + BlockSize =3D Instance->Media.BlockSize; + + DEBUG ((DEBUG_BLKIO, "FvbRead: Check if (Offset=3D0x%x + NumBytes=3D0x%x= ) <=3D BlockSize=3D0x%x\n", Offset, *NumBytes, BlockSize)); + + // The read must not span block boundaries. + // We need to check each variable individually because adding two large = values together overflows. + if ((Offset >=3D BlockSize) || + (*NumBytes > BlockSize) || + ((Offset + *NumBytes) > BlockSize)) + { + DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=3D= 0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", Offset, *NumBytes, BlockSize= )); + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to read + if (*NumBytes =3D=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + return SMMStoreRead (Lba, Offset, NumBytes, Buffer); +} + +/** + Writes the specified number of bytes from the input buffer to the block. + + The Write() function writes the specified number of bytes from + the provided buffer to the specified block and offset. If the + firmware volume is sticky write, the caller must ensure that + all the bits of the specified range to write are in the + EFI_FVB_ERASE_POLARITY state before calling the Write() + function, or else the result will be unpredictable. This + unpredictability arises because, for a sticky-write firmware + volume, a write may negate a bit in the EFI_FVB_ERASE_POLARITY + state but cannot flip it back again. Before calling the + Write() function, it is recommended for the caller to first call + the EraseBlocks() function to erase the specified block to + write. A block erase cycle will transition bits from the + (NOT)EFI_FVB_ERASE_POLARITY state back to the + EFI_FVB_ERASE_POLARITY state. Implementations should be + mindful that the firmware volume might be in the WriteDisabled + state. If it is in this state, the Write() function must + return the status code EFI_ACCESS_DENIED without modifying the + contents of the firmware volume. The Write() function must + also prevent spanning block boundaries. If a write is + requested that spans a block boundary, the write must store up + to the boundary but not beyond. The output parameter NumBytes + must be set to correctly indicate the number of bytes actually + written. The caller must be aware that a write may be + partially completed. All writes, partial or otherwise, must be + fully flushed to the hardware before the Write() service + returns. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_PROT= OCOL instance. + + @param Lba The starting logical block index to write to. + + @param Offset Offset into the block at which to begin writi= ng. + + @param NumBytes The pointer to a UINTN. + At entry, *NumBytes contains the total size o= f the buffer. + At exit, *NumBytes contains the total number = of bytes actually written. + + @param Buffer The pointer to a caller-allocated buffer that= contains the source for the write. + + @retval EFI_SUCCESS The firmware volume was written successfully. + + @retval EFI_BAD_BUFFER_SIZE The write was attempted across an LBA boundar= y. + On output, NumBytes contains the total number= of bytes + actually written. + + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabled s= tate. + + @retval EFI_DEVICE_ERROR The block device is malfunctioning and could = not be written. + + + **/ +EFI_STATUS +EFIAPI +FvbWrite ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + IN EFI_LBA Lba, + IN UINTN Offset, + IN OUT UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + UINTN BlockSize; + SMMSTORE_INSTANCE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + DEBUG ((DEBUG_BLKIO, "FvbWrite(Parameters: Lba=3D%ld, Offset=3D0x%x, *Nu= mBytes=3D0x%x, Buffer @ 0x%08x)\n", Lba, Offset, *NumBytes, Buffer)); + + // Cache the block size to avoid de-referencing pointers all the time + BlockSize =3D Instance->Media.BlockSize; + + // The read must not span block boundaries. + // We need to check each variable individually because adding two large = values together overflows. + if ((Offset >=3D BlockSize) || + (*NumBytes > BlockSize) || + ((Offset + *NumBytes) > BlockSize)) + { + DEBUG ((DEBUG_ERROR, "FvbRead: ERROR - EFI_BAD_BUFFER_SIZE: (Offset=3D= 0x%x + NumBytes=3D0x%x) > BlockSize=3D0x%x\n", Offset, *NumBytes, BlockSize= )); + return EFI_BAD_BUFFER_SIZE; + } + + // We must have some bytes to read + if (*NumBytes =3D=3D 0) { + return EFI_BAD_BUFFER_SIZE; + } + + return SMMStoreWrite (Lba, Offset, NumBytes, Buffer); +} + +/** + Erases and initialises a firmware volume block. + + The EraseBlocks() function erases one or more blocks as denoted + by the variable argument list. The entire parameter list of + blocks must be verified before erasing any blocks. If a block is + requested that does not exist within the associated firmware + volume (it has a larger index than the last block of the + firmware volume), the EraseBlocks() function must return the + status code EFI_INVALID_PARAMETER without modifying the contents + of the firmware volume. Implementations should be mindful that + the firmware volume might be in the WriteDisabled state. If it + is in this state, the EraseBlocks() function must return the + status code EFI_ACCESS_DENIED without modifying the contents of + the firmware volume. All calls to EraseBlocks() must be fully + flushed to the hardware before the EraseBlocks() service + returns. + + @param This Indicates the EFI_FIRMWARE_VOLUME_BLOCK2_= PROTOCOL + instance. + + @param ... The variable argument list is a list of t= uples. + Each tuple describes a range of LBAs to e= rase + and consists of the following: + - An EFI_LBA that indicates the starting = LBA + - A UINTN that indicates the number of bl= ocks to erase. + + The list is terminated with an EFI_LBA_LI= ST_TERMINATOR. + For example, the following indicates that= two ranges of blocks + (5-7 and 10-11) are to be erased: + EraseBlocks (This, 5, 3, 10, 2, EFI_LBA_L= IST_TERMINATOR); + + @retval EFI_SUCCESS The erase request successfully completed. + + @retval EFI_ACCESS_DENIED The firmware volume is in the WriteDisabl= ed state. + + @retval EFI_DEVICE_ERROR The block device is not functioning corre= ctly and could not be written. + The firmware device may have been partial= ly erased. + + @retval EFI_INVALID_PARAMETER One or more of the LBAs listed in the var= iable argument list do + not exist in the firmware volume. + + **/ +EFI_STATUS +EFIAPI +FvbEraseBlocks ( + IN CONST EFI_FIRMWARE_VOLUME_BLOCK2_PROTOCOL *This, + ... + ) +{ + EFI_STATUS Status; + VA_LIST Args; + EFI_LBA StartingLba; // Lba from which we start erasing + UINTN NumOfLba; // Number of Lba blocks to erase + SMMSTORE_INSTANCE *Instance; + + Instance =3D INSTANCE_FROM_FVB_THIS (This); + + DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks()\n")); + + Status =3D EFI_SUCCESS; + + // Detect WriteDisabled state + if (Instance->Media.ReadOnly =3D=3D TRUE) { + // Firmware volume is in WriteDisabled state + DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Device is in WriteDisabl= ed state.\n")); + return EFI_ACCESS_DENIED; + } + + // Before erasing, check the entire list of parameters to ensure all spe= cified blocks are valid + + VA_START (Args, This); + do { + // Get the Lba from which we start erasing + StartingLba =3D VA_ARG (Args, EFI_LBA); + + // Have we reached the end of the list? + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) { + // Exit the while loop + break; + } + + // How many Lba blocks are we requested to erase? + NumOfLba =3D VA_ARG (Args, UINTN); + + // All blocks must be within range + DEBUG (( + DEBUG_BLKIO, + "FvbEraseBlocks: Check if: ( StartingLba=3D%ld + NumOfLba=3D%Lu - 1 = ) > LastBlock=3D%ld.\n", + StartingLba, + (UINT64)NumOfLba, + Instance->Media.LastBlock + )); + if ((NumOfLba =3D=3D 0) || ((StartingLba + NumOfLba - 1) > Instance->M= edia.LastBlock)) { + VA_END (Args); + DEBUG ((DEBUG_ERROR, "FvbEraseBlocks: ERROR - Lba range goes past th= e last Lba.\n")); + Status =3D EFI_INVALID_PARAMETER; + goto EXIT; + } + } while (TRUE); + + VA_END (Args); + + // + // To get here, all must be ok, so start erasing + // + VA_START (Args, This); + do { + // Get the Lba from which we start erasing + StartingLba =3D VA_ARG (Args, EFI_LBA); + + // Have we reached the end of the list? + if (StartingLba =3D=3D EFI_LBA_LIST_TERMINATOR) { + // Exit the while loop + break; + } + + // How many Lba blocks are we requested to erase? + NumOfLba =3D VA_ARG (Args, UINTN); + + // Go through each one and erase it + while (NumOfLba > 0) { + // Erase it + DEBUG ((DEBUG_BLKIO, "FvbEraseBlocks: Erasing Lba=3D%ld\n", Starting= Lba)); + Status =3D SMMStoreEraseBlock (StartingLba); + if (EFI_ERROR (Status)) { + VA_END (Args); + Status =3D EFI_DEVICE_ERROR; + goto EXIT; + } + + // Move to the next Lba + StartingLba++; + NumOfLba--; + } + } while (TRUE); + + VA_END (Args); + +EXIT: + return Status; +} + +/** + Fixup internal data so that EFI can be call in virtual mode. + Call the passed in Child Notify event and convert any pointers in + lib to virtual mode. + + @param[in] Event The Event that is being processed + @param[in] Context Event Context +**/ +VOID +EFIAPI +FvbVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&mFlashNvStorageVariableBase); + return; +} + +EFI_STATUS +EFIAPI +SMMStoreFvbInitialize ( + IN SMMSTORE_INSTANCE *Instance + ) +{ + EFI_STATUS Status; + UINT32 FvbNumLba; + EFI_BOOT_MODE BootMode; + + DEBUG ((DEBUG_BLKIO, "NorFlashFvbInitialize\n")); + ASSERT ((Instance !=3D NULL)); + + mFlashNvStorageVariableBase =3D PcdGet32 (PcdFlashNvStorageVariableBase)= ; + + BootMode =3D GetBootModeHob (); + if (BootMode =3D=3D BOOT_WITH_DEFAULT_SETTINGS) { + Status =3D EFI_INVALID_PARAMETER; + } else { + // Determine if there is a valid header at the beginning of the NorFla= sh + Status =3D ValidateFvHeader (Instance); + } + + // Install the Default FVB header if required + if (EFI_ERROR (Status)) { + // There is no valid header, so time to install one. + DEBUG ((DEBUG_INFO, "%a: The FVB Header is not valid.\n", __FUNCTION__= )); + DEBUG (( + DEBUG_INFO, + "%a: Installing a correct one for this volume.\n", + __FUNCTION__ + )); + + // Erase all the NorFlash that is reserved for variable storage + FvbNumLba =3D (PcdGet32 (PcdFlashNvStorageVariableSize) + + PcdGet32 (PcdFlashNvStorageFtwWorkingSize) + + PcdGet32 (PcdFlashNvStorageFtwSpareSize)) / Instance->Med= ia.BlockSize; + + Status =3D FvbEraseBlocks (&Instance->FvbProtocol, (EFI_LBA)0, FvbNumL= ba, EFI_LBA_LIST_TERMINATOR); + if (EFI_ERROR (Status)) { + return Status; + } + + // Install all appropriate headers + Status =3D InitializeFvAndVariableStoreHeaders (Instance); + if (EFI_ERROR (Status)) { + return Status; + } + } else { + DEBUG ((DEBUG_INFO, "%a: FVB header is valid\n", __FUNCTION__)); + } + + // + // The driver implementing the variable read service can now be dispatch= ed; + // the varstore headers are in place. + // + Status =3D gBS->InstallProtocolInterface ( + &gImageHandle, + &gEdkiiNvVarStoreFormattedGuid, + EFI_NATIVE_INTERFACE, + NULL + ); + ASSERT_EFI_ERROR (Status); + + // + // Register for the virtual address change event + // + Status =3D gBS->CreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + FvbVirtualNotifyEvent, + NULL, + &gEfiEventVirtualAddressChangeGuid, + &mFvbVirtualAddrChangeEvent + ); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/UefiPayloadPkg/Include/Coreboot.h b/UefiPayloadPkg/Include/Cor= eboot.h index a3e1109fe8..ef6fda1d4d 100644 --- a/UefiPayloadPkg/Include/Coreboot.h +++ b/UefiPayloadPkg/Include/Coreboot.h @@ -236,6 +236,19 @@ struct cb_cbmem_tab { UINT64 cbmem_tab; }; =20 +#define CB_TAG_SMMSTOREV2 0x0039 +struct cb_smmstorev2 { + UINT32 tag; + UINT32 size; + UINT32 num_blocks; /* Number of writeable blocks in SMM */ + UINT32 block_size; /* Size of a block in byte. Default: 64 KiB */ + UINT32 mmap_addr; /* MMIO address of the store for read only acce= ss */ + UINT32 com_buffer; /* Physical address of the communication buffer= */ + UINT32 com_buffer_size; /* Size of the communication buffer in byte */ + UINT8 apm_cmd; /* The command byte to write to the APM I/O por= t */ + UINT8 unused[3]; /* Set to zero */ +}; + /* Helpful macros */ =20 #define MEM_RANGE_COUNT(_rec) \ diff --git a/UefiPayloadPkg/Include/Guid/SMMSTOREInfoGuid.h b/UefiPayloadPk= g/Include/Guid/SMMSTOREInfoGuid.h new file mode 100644 index 0000000000..552f86115b --- /dev/null +++ b/UefiPayloadPkg/Include/Guid/SMMSTOREInfoGuid.h @@ -0,0 +1,27 @@ +/** @file + This file defines the hob structure for system tables like ACPI, SMBIOS = tables. + + Copyright (c) 2020, 9elements Agency GmbH
+ SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __SMMSTORE_GUID_H__ +#define __SMMSTORE_GUID_H__ + +/// +/// System Table Information GUID +/// +extern EFI_GUID gEfiSMMSTOREInfoHobGuid; + +typedef struct { + UINT64 ComBuffer; + UINT32 ComBufferSize; + UINT32 NumBlocks; + UINT32 BlockSize; + UINT64 MmioAddress; + UINT8 ApmCmd; + UINT8 Reserved0[3]; +} SMMSTORE_INFO; + +#endif diff --git a/UefiPayloadPkg/Include/Library/BlParseLib.h b/UefiPayloadPkg/I= nclude/Library/BlParseLib.h index 82be5a9eb8..946035ab1f 100644 --- a/UefiPayloadPkg/Include/Library/BlParseLib.h +++ b/UefiPayloadPkg/Include/Library/BlParseLib.h @@ -17,6 +17,7 @@ #include #include #include +#include =20 #define GET_BOOTLOADER_PARAMETER() PcdGet64 (PcdBootloaderParameter) =20 @@ -148,4 +149,19 @@ ParseMiscInfo ( VOID ); =20 +/** + Find the video frame buffer device information + + @param SMMSTOREInfo Pointer to the SMMSTORE_INFO structure + + @retval RETURN_SUCCESS Successfully find the SMM store buffer inform= ation. + @retval RETURN_NOT_FOUND Failed to find the SMM store buffer informati= on . + +**/ +RETURN_STATUS +EFIAPI +ParseSMMSTOREInfo ( + OUT SMMSTORE_INFO *SMMSTOREInfo + ); + #endif diff --git a/UefiPayloadPkg/Include/Library/SMMStoreLib.h b/UefiPayloadPkg/= Include/Library/SMMStoreLib.h new file mode 100644 index 0000000000..be7b9a54a1 --- /dev/null +++ b/UefiPayloadPkg/Include/Library/SMMStoreLib.h @@ -0,0 +1,98 @@ +/** @file SMMStoreLib.h + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef __SMM_STORE_LIB_H__ +#define __SMM_STORE_LIB_H__ + +#include +#include +#include + +#define SMMSTORE_COMBUF_SIZE 16 + +/** + Read from SMMStore + + @param[in] Lba The starting logical block index to read from. + @param[in] Offset Offset into the block at which to begin reading. + @param[in] NumBytes On input, indicates the requested read size. On + output, indicates the actual number of bytes read + @param[in] Buffer Pointer to the buffer to read into. + +**/ +EFI_STATUS +SMMStoreRead ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ); + + +/** + Write to SMMStore + + @param[in] Lba The starting logical block index to write to. + @param[in] Offset Offset into the block at which to begin writing. + @param[in] NumBytes On input, indicates the requested write size. On + output, indicates the actual number of bytes written + @param[in] Buffer Pointer to the data to write. + +**/ +EFI_STATUS +SMMStoreWrite ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ); + + +/** + Erase a block using the SMMStore + + @param Lba The logical block index to erase. + +**/ +EFI_STATUS +SMMStoreEraseBlock ( + IN EFI_LBA Lba + ); + + +/** + Notify the SMMStore Library about a VirtualNotify + +**/ + +VOID +EFIAPI +SMMStoreVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ); + +/** + Initializes SMMStore support + + @param[in] Ptr A runtime buffer where arguments are sto= red + for SMM communication + @param[in] SmmStoreInfoHob A runtime buffer with a copy of the + SmmStore Info Hob + + @retval EFI_WRITE_PROTECTED The SMMSTORE is not present. + @retval EFI_SUCCESS The SMMSTORE is supported. + +**/ +EFI_STATUS +SMMStoreInitialize ( + IN VOID *Ptr, + IN SMMSTORE_INFO *SmmStoreInfoHob + ); + +#endif /* __SMM_STORE_LIB_H__ */ diff --git a/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c b/UefiPayloadPk= g/Library/CbParseLib/CbParseLib.c index 8a353f77f6..ab6c21b910 100644 --- a/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c +++ b/UefiPayloadPkg/Library/CbParseLib/CbParseLib.c @@ -604,3 +604,43 @@ ParseMiscInfo ( { return RETURN_SUCCESS; } + +/** + Find the SMM store information + @param SMMSTOREInfo Pointer to the SMMSTORE_INFO structure + @retval RETURN_SUCCESS Successfully find the SMM store buffer inform= ation. + @retval RETURN_NOT_FOUND Failed to find the SMM store buffer informati= on . +**/ +RETURN_STATUS +EFIAPI +ParseSMMSTOREInfo ( + OUT SMMSTORE_INFO *SMMSTOREInfo + ) +{ + struct cb_smmstorev2 *CbSSRec; + + if (SMMSTOREInfo =3D=3D NULL) { + return RETURN_INVALID_PARAMETER; + } + + CbSSRec =3D FindCbTag (CB_TAG_SMMSTOREV2); + if (CbSSRec =3D=3D NULL) { + return RETURN_NOT_FOUND; + } + + DEBUG ((DEBUG_INFO, "Found SMM Store information\n")); + DEBUG ((DEBUG_INFO, "block size: 0x%x\n", CbSSRec->block_size)); + DEBUG ((DEBUG_INFO, "number of blocks: 0x%x\n", CbSSRec->num_blocks)); + DEBUG ((DEBUG_INFO, "communication buffer: 0x%x\n", CbSSRec->com_buffer)= ); + DEBUG ((DEBUG_INFO, "communication buffer size: 0x%x\n", CbSSRec->com_bu= ffer_size)); + DEBUG ((DEBUG_INFO, "MMIO address of store: 0x%x\n", CbSSRec->mmap_addr)= ); + + SMMSTOREInfo->ComBuffer =3D CbSSRec->com_buffer; + SMMSTOREInfo->ComBufferSize =3D CbSSRec->com_buffer_size; + SMMSTOREInfo->BlockSize =3D CbSSRec->block_size; + SMMSTOREInfo->NumBlocks =3D CbSSRec->num_blocks; + SMMSTOREInfo->MmioAddress =3D CbSSRec->mmap_addr; + SMMSTOREInfo->ApmCmd =3D CbSSRec->apm_cmd; + + return RETURN_SUCCESS; +} diff --git a/UefiPayloadPkg/Library/CbSMMStoreLib/CbSMMStoreLib.inf b/UefiP= ayloadPkg/Library/CbSMMStoreLib/CbSMMStoreLib.inf new file mode 100644 index 0000000000..c93d959f94 --- /dev/null +++ b/UefiPayloadPkg/Library/CbSMMStoreLib/CbSMMStoreLib.inf @@ -0,0 +1,28 @@ +## @file +# SMMStore library for coreboot +# +# Copyright (c) 2020 9elements Agency GmbH.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D CbSmmStoreLib + FILE_GUID =3D 40A2CBC6-CFB8-447b-A90E-298E88FD345E + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D SmmStoreLib + +[Sources] + CorebootSMMStore.c + +[LibraryClasses] + BaseMemoryLib + DebugLib + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiPayloadPkg/UefiPayloadPkg.dec diff --git a/UefiPayloadPkg/Library/CbSMMStoreLib/CorebootSMMStore.c b/Uefi= PayloadPkg/Library/CbSMMStoreLib/CorebootSMMStore.c new file mode 100644 index 0000000000..687cb124ba --- /dev/null +++ b/UefiPayloadPkg/Library/CbSMMStoreLib/CorebootSMMStore.c @@ -0,0 +1,317 @@ +/** @file CorebootSMMStoreDxe.c + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include + +#include +#include +#include +#include +#include + +/* + * calls into SMM to use the SMMSTORE implementation for persistent storag= e. + * The given cmd and subcmd is in eax and the argument in ebx. + * On successful invocation the result is in eax. + */ +static UINT32 +call_smm ( + UINT8 cmd, + UINT8 subcmd, + UINT32 arg + ) +{ + CONST UINT32 eax =3D ((subcmd << 8) | cmd); + CONST UINT32 ebx =3D arg; + UINT32 res; + UINTN i; + + /* Retry a few times to make sure it works */ + for (i =3D 0; i < 5; i++) { + __asm__ __volatile__ ( + "\toutb %b0, $0xb2\n" + : "=3Da" (res) + : "a" (eax), "b" (ebx) + : "memory"); + if (res =3D=3D eax) { + /** + There might ba a delay between writing the SMI trigger register an= d + entering SMM, in which case the SMI handler will do nothing as onl= y + synchronous SMIs are handled. In addition when there's no SMI hand= ler + or the SMMSTORE feature isn't compiled in, no register will be mod= ified. + + As there's no livesign from SMM, just wait a bit for the handler t= o fire, + and then try again. + **/ + for (UINTN j =3D 0; j < 0x10000; j++) { + CpuPause (); + } + } else { + break; + } + } + + return res; +} + +#define SMMSTORE_RET_SUCCESS 0 +#define SMMSTORE_RET_FAILURE 1 +#define SMMSTORE_RET_UNSUPPORTED 2 + +/* Version 2 only */ +#define SMMSTORE_CMD_INIT 4 +#define SMMSTORE_CMD_RAW_READ 5 +#define SMMSTORE_CMD_RAW_WRITE 6 +#define SMMSTORE_CMD_RAW_CLEAR 7 + +/* + * This allows the payload to store raw data in the flash regions. + * This can be used by a FaultTolerantWrite implementation, that uses at l= east + * two regions in an A/B update scheme. + */ + +#pragma pack(1) + +/* + * Reads a chunk of raw data with size @bufsize from the block specified b= y + * @block_id starting at @bufoffset. + * The read data is placed in @buf. + * + * @block_id must be less than num_blocks + * @bufoffset + @bufsize must be less than block_size + */ +struct smmstore_params_raw_write { + UINT32 bufsize; + UINT32 bufoffset; + UINT32 block_id; +}; + +/* + * Writes a chunk of raw data with size @bufsize to the block specified by + * @block_id starting at @bufoffset. + * + * @block_id must be less than num_blocks + * @bufoffset + @bufsize must be less than block_size + */ +struct smmstore_params_raw_read { + UINT32 bufsize; + UINT32 bufoffset; + UINT32 block_id; +}; + +/* + * Erases the specified block. + * + * @block_id must be less than num_blocks + */ +struct smmstore_params_raw_clear { + UINT32 block_id; +}; + +typedef struct smmstore_comm_buffer { + union { + struct smmstore_params_raw_write raw_write; + struct smmstore_params_raw_read raw_read; + struct smmstore_params_raw_clear raw_clear; + }; +} SMMSTORE_COMBUF; +#pragma pack(0) + +/* + * A memory buffer to place arguments in. + */ +STATIC SMMSTORE_COMBUF *mArgComBuf; +STATIC UINT32 mArgComBufPhys; + +/* + * Metadata provided by the first stage bootloader. + */ +STATIC SMMSTORE_INFO *mSmmStoreInfo; + +/** + Read from SMMStore + + @param[in] Lba The starting logical block index to read from. + @param[in] Offset Offset into the block at which to begin reading. + @param[in] NumBytes On input, indicates the requested read size. On + output, indicates the actual number of bytes read + @param[in] Buffer Pointer to the buffer to read into. + +**/ +EFI_STATUS +SMMStoreRead ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + UINT32 Result; + + if (!mSmmStoreInfo) { + return EFI_NO_MEDIA; + } + + if (Lba >=3D mSmmStoreInfo->NumBlocks) { + return EFI_INVALID_PARAMETER; + } + + if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) || + ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize)) + { + return EFI_INVALID_PARAMETER; + } + + mArgComBuf->raw_read.bufsize =3D *NumBytes; + mArgComBuf->raw_read.bufoffset =3D Offset; + mArgComBuf->raw_read.block_id =3D Lba; + + Result =3D call_smm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_READ, mArgC= omBufPhys); + if (Result =3D=3D SMMSTORE_RET_FAILURE) { + return EFI_DEVICE_ERROR; + } else if (Result =3D=3D SMMSTORE_RET_UNSUPPORTED) { + return EFI_UNSUPPORTED; + } else if (Result !=3D SMMSTORE_RET_SUCCESS) { + return EFI_NO_RESPONSE; + } + + CopyMem (Buffer, (VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), *Nu= mBytes); + + return EFI_SUCCESS; +} + +/** + Write to SMMStore + + @param[in] Lba The starting logical block index to write to. + @param[in] Offset Offset into the block at which to begin writing. + @param[in] NumBytes On input, indicates the requested write size. On + output, indicates the actual number of bytes written + @param[in] Buffer Pointer to the data to write. + +**/ +EFI_STATUS +SMMStoreWrite ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + UINTN Result; + + if (!mSmmStoreInfo) { + return EFI_NO_MEDIA; + } + + if (Lba >=3D mSmmStoreInfo->NumBlocks) { + return EFI_INVALID_PARAMETER; + } + + if (((*NumBytes + Offset) > mSmmStoreInfo->BlockSize) || + ((*NumBytes + Offset) > mSmmStoreInfo->ComBufferSize)) + { + return EFI_INVALID_PARAMETER; + } + + mArgComBuf->raw_write.bufsize =3D *NumBytes; + mArgComBuf->raw_write.bufoffset =3D Offset; + mArgComBuf->raw_write.block_id =3D Lba; + + CopyMem ((VOID *)(UINTN)(mSmmStoreInfo->ComBuffer + Offset), Buffer, *Nu= mBytes); + + Result =3D call_smm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_WRITE, mArg= ComBufPhys); + if (Result =3D=3D SMMSTORE_RET_FAILURE) { + return EFI_DEVICE_ERROR; + } else if (Result =3D=3D SMMSTORE_RET_UNSUPPORTED) { + return EFI_UNSUPPORTED; + } else if (Result !=3D SMMSTORE_RET_SUCCESS) { + return EFI_NO_RESPONSE; + } + + return EFI_SUCCESS; +} + +/** + Erase a SMMStore block + + @param Lba The logical block index to erase. + +**/ +EFI_STATUS +SMMStoreEraseBlock ( + IN EFI_LBA Lba + ) +{ + UINTN Result; + + if (!mSmmStoreInfo) { + return EFI_NO_MEDIA; + } + + if (Lba >=3D mSmmStoreInfo->NumBlocks) { + return EFI_INVALID_PARAMETER; + } + + mArgComBuf->raw_clear.block_id =3D Lba; + + Result =3D call_smm (mSmmStoreInfo->ApmCmd, SMMSTORE_CMD_RAW_CLEAR, mArg= ComBufPhys); + if (Result =3D=3D SMMSTORE_RET_FAILURE) { + return EFI_DEVICE_ERROR; + } else if (Result =3D=3D SMMSTORE_RET_UNSUPPORTED) { + return EFI_UNSUPPORTED; + } else if (Result !=3D SMMSTORE_RET_SUCCESS) { + return EFI_NO_RESPONSE; + } + + return EFI_SUCCESS; +} + +VOID +EFIAPI +SMMStoreVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EfiConvertPointer (0x0, (VOID **)&mArgComBuf); + if (mSmmStoreInfo) { + EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo->ComBuffer); + EfiConvertPointer (0x0, (VOID **)&mSmmStoreInfo); + } + + return; +} + +/** + Initializes SMMStore support + + @param[in] Ptr A runtime buffer where arguments are sto= red + for SMM communication + @param[in] SmmStoreInfoHob A runtime buffer with a copy of the + SmmStore Info Hob + + @retval EFI_WRITE_PROTECTED The SMMSTORE is not present. + @retval EFI_SUCCESS The SMMSTORE is supported. + +**/ +EFI_STATUS +SMMStoreInitialize ( + IN VOID *Ptr, + IN SMMSTORE_INFO *SmmStoreInfoHob + ) +{ + ASSERT (Ptr !=3D NULL); + ASSERT (SmmStoreInfoHob !=3D NULL); + + mArgComBuf =3D Ptr; + mArgComBufPhys =3D (UINT32)(UINTN)mArgComBuf; + + mSmmStoreInfo =3D SmmStoreInfoHob; + + return EFI_SUCCESS; +} diff --git a/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c b/UefiPayload= Pkg/Library/SblParseLib/SblParseLib.c index d88238bfdc..64f4164d66 100644 --- a/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c +++ b/UefiPayloadPkg/Library/SblParseLib/SblParseLib.c @@ -289,3 +289,18 @@ ParseMiscInfo ( =20 return Status; } + +/** + Find the video frame buffer device information + @param SMMSTOREInfo Pointer to the SMMSTORE_INFO structure + @retval RETURN_SUCCESS Successfully find the SMM store buffer inform= ation. + @retval RETURN_NOT_FOUND Failed to find the SMM store buffer informati= on . +**/ +RETURN_STATUS +EFIAPI +ParseSMMSTOREInfo ( + OUT SMMSTORE_INFO *SMMSTOREInfo + ) +{ + return RETURN_NOT_FOUND; +} diff --git a/UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStore.c b/UefiPayl= oadPkg/Library/SblSMMStoreLib/SblSMMStore.c new file mode 100644 index 0000000000..449355483c --- /dev/null +++ b/UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStore.c @@ -0,0 +1,100 @@ +/** @file SblSMMStore.c + + Copyright (c) 2020, 9elements Agency GmbH
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ +#include + +#include +#include +#include +#include + +/** + Read from SMMStore + + @param[in] Lba The starting logical block index to read from. + @param[in] Offset Offset into the block at which to begin reading. + @param[in] NumBytes On input, indicates the requested read size. On + output, indicates the actual number of bytes read + @param[in] Buffer Pointer to the buffer to read into. + +**/ +EFI_STATUS +SMMStoreRead ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + return EFI_UNSUPPORTED; +} + +/** + Write to SMMStore + + @param[in] Lba The starting logical block index to write to. + @param[in] Offset Offset into the block at which to begin writing. + @param[in] NumBytes On input, indicates the requested write size. On + output, indicates the actual number of bytes written + @param[in] Buffer Pointer to the data to write. + +**/ +EFI_STATUS +SMMStoreWrite ( + IN EFI_LBA Lba, + IN UINTN Offset, + IN UINTN *NumBytes, + IN UINT8 *Buffer + ) +{ + return EFI_UNSUPPORTED; +} + +/** + Erase a SMMStore block + + @param Lba The logical block index to erase. + +**/ +EFI_STATUS +SMMStoreEraseBlock ( + IN EFI_LBA Lba + ) +{ + return EFI_UNSUPPORTED; +} + +VOID +EFIAPI +SMMStoreVirtualNotifyEvent ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + return; +} + +/** + Initializes SMMStore support + + @param[in] Ptr A runtime buffer where arguments are sto= red + for SMM communication + @param[in] SmmStoreInfoHob A runtime buffer with a copy of the + SmmStore Info Hob + + @retval EFI_WRITE_PROTECTED The SMMSTORE is not present. + @retval EFI_SUCCESS The SMMSTORE is supported. + +**/ +EFI_STATUS +SMMStoreInitialize ( + IN VOID *Ptr, + IN SMMSTORE_INFO *SmmStoreInfoHob + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStoreLib.inf b/Uef= iPayloadPkg/Library/SblSMMStoreLib/SblSMMStoreLib.inf new file mode 100644 index 0000000000..cf6246ed19 --- /dev/null +++ b/UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStoreLib.inf @@ -0,0 +1,28 @@ +## @file +# SMMStore library for Slimbootloader +# +# Copyright (c) 2020 9elements Agency GmbH.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D SblSmmStoreLib + FILE_GUID =3D 2CA0BC03-A619-4B88-A0C5-06A1992750C3 + MODULE_TYPE =3D BASE + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D SmmStoreLib + +[Sources] + SblSMMStore.c + +[LibraryClasses] + BaseMemoryLib + DebugLib + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UefiPayloadPkg/UefiPayloadPkg.dec diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c b/UefiPaylo= adPkg/UefiPayloadEntry/UefiPayloadEntry.c index 0fed1e3691..08bd4d302b 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c +++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.c @@ -225,6 +225,8 @@ BuildHobFromBl ( { EFI_STATUS Status; ACPI_BOARD_INFO *AcpiBoardInfo; + SMMSTORE_INFO SMMSTOREInfo; + SMMSTORE_INFO *NewSMMSTOREInfo; EFI_PEI_GRAPHICS_INFO_HOB GfxInfo; EFI_PEI_GRAPHICS_INFO_HOB *NewGfxInfo; EFI_PEI_GRAPHICS_DEVICE_INFO_HOB GfxDeviceInfo; @@ -271,6 +273,17 @@ BuildHobFromBl ( DEBUG ((DEBUG_INFO, "Created graphics device info hob\n")); } =20 + // + // Create guid hob for SMMSTORE + // + Status =3D ParseSMMSTOREInfo (&SMMSTOREInfo); + if (!EFI_ERROR (Status)) { + NewSMMSTOREInfo =3D BuildGuidHob (&gEfiSMMSTOREInfoHobGuid, sizeof (SM= MSTOREInfo)); + ASSERT (NewSMMSTOREInfo !=3D NULL); + CopyMem (NewSMMSTOREInfo, &SMMSTOREInfo, sizeof (SMMSTOREInfo)); + DEBUG ((DEBUG_INFO, "Created SMMSTORE info hob\n")); + } + // // Creat SmBios table Hob // diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h b/UefiPaylo= adPkg/UefiPayloadEntry/UefiPayloadEntry.h index 56e0a4c639..fae05228cf 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h +++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.h @@ -36,6 +36,7 @@ #include #include #include +#include =20 #define LEGACY_8259_MASK_REGISTER_MASTER 0x21 #define LEGACY_8259_MASK_REGISTER_SLAVE 0xA1 diff --git a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf b/UefiPay= loadPkg/UefiPayloadEntry/UefiPayloadEntry.inf index 1847d6481a..7837d15403 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf +++ b/UefiPayloadPkg/UefiPayloadEntry/UefiPayloadEntry.inf @@ -55,6 +55,7 @@ PeCoffLib PlatformSupportLib UefiCpuLib + PcdLib =20 [Guids] gEfiMemoryTypeInformationGuid @@ -65,6 +66,7 @@ gUniversalPayloadSmbiosTableGuid gUniversalPayloadAcpiTableGuid gUniversalPayloadSerialPortInfoGuid + gEfiSMMSTOREInfoHobGuid =20 [FeaturePcd.IA32] gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode ## CONSUME= S diff --git a/UefiPayloadPkg/UefiPayloadPkg.dec b/UefiPayloadPkg/UefiPayload= Pkg.dec index 551f0a4915..aeb1f8428e 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dec +++ b/UefiPayloadPkg/UefiPayloadPkg.dec @@ -42,6 +42,15 @@ gSmmRegisterInfoGuid =3D { 0xaa9bd7a7, 0xcafb, 0x4499, { 0xa4, 0xa9,= 0xb, 0x34, 0x6b, 0x40, 0xa6, 0x22 } } gS3CommunicationGuid =3D { 0x88e31ba1, 0x1856, 0x4b8b, { 0xbb, 0xdf,= 0xf8, 0x16, 0xdd, 0x94, 0xa, 0xef } } =20 + gEfiSMMSTOREInfoHobGuid =3D { 0xf585ca19, 0x881b, 0x44fb, { 0x3f, 0x3d,= 0x81, 0x89, 0x7c, 0x57, 0xbb, 0x01 } } + +[Guids.common] + # + # Following Guid must match FILE_GUID in + # MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf + # + gVariableRuntimeDxeFileGuid =3D { 0xcbd2e4d5, 0x7068, 0x4ff5, { 0xb4, 0x= 62, 0x98, 0x22, 0xb4, 0xad, 0x8d, 0x60 } } + [Ppis] gEfiPayLoadHobBasePpiGuid =3D { 0xdbe23aa1, 0xa342, 0x4b97, {0x85, 0xb6,= 0xb2, 0x26, 0xf1, 0x61, 0x73, 0x89} } =20 diff --git a/UefiPayloadPkg/UefiPayloadPkg.dsc b/UefiPayloadPkg/UefiPayload= Pkg.dsc index 1ce96a51c1..eb5ba12e67 100644 --- a/UefiPayloadPkg/UefiPayloadPkg.dsc +++ b/UefiPayloadPkg/UefiPayloadPkg.dsc @@ -269,6 +269,11 @@ VarCheckLib|MdeModulePkg/Library/VarCheckLib/VarCheckLib.inf VariablePolicyLib|MdeModulePkg/Library/VariablePolicyLib/VariablePolicyL= ib.inf VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/Var= iablePolicyHelperLib.inf +!if $(BOOTLOADER) =3D=3D "COREBOOT" + SmmStoreLib|UefiPayloadPkg/Library/CbSMMStoreLib/CbSMMStoreLib.inf +!else + SmmStoreLib|UefiPayloadPkg/Library/SblSMMStoreLib/SblSMMStoreLib.inf +!endif VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf =20 [LibraryClasses.common.SEC] @@ -375,14 +380,11 @@ gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmEnableBspElection|FALSE =20 [PcdsFixedAtBuild] - gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x10000 + # UEFI spec: Minimal value is 0x8000! + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxVariableSize|0x8000 + gEfiMdeModulePkgTokenSpaceGuid.PcdMaxAuthVariableSize|0x8800 gEfiMdeModulePkgTokenSpaceGuid.PcdMaxHardwareErrorVariableSize|0x8000 gEfiMdeModulePkgTokenSpaceGuid.PcdVariableStoreSize|0x10000 -!if $(VARIABLE_SUPPORT) =3D=3D "EMU" - gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable |TRUE -!else - gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable |FALSE -!endif =20 gEfiMdeModulePkgTokenSpaceGuid.PcdVpdBaseAddress|0x0 gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE @@ -465,10 +467,17 @@ !endif gEfiMdeModulePkgTokenSpaceGuid.PcdResetOnMemoryTypeInformationChange|FAL= SE gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved|0 - gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase64|0 + gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableBase|0 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0 - gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|3 + gEfiMdePkgTokenSpaceGuid.PcdPlatformBootTimeOut|$(PLATFORM_BOOT_TIMEOUT) +# Might be needed +# !if $(VARIABLE_SUPPORT) =3D=3D "EMU" +# gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable |TRUE +# !else + gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvModeEnable |FALSE +# !endif + !if $(VARIABLE_SUPPORT) =3D=3D "SPI" gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize |0 gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingSize|0 @@ -566,7 +575,11 @@ !endif PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcatRealTimeClockRuntimeDxe.i= nf !if $(EMU_VARIABLE_ENABLE) =3D=3D TRUE - MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf + MdeModulePkg/Universal/FaultTolerantWriteDxe/FaultTolerantWriteDxe.inf + MdeModulePkg/Universal/Variable/RuntimeDxe/VariableRuntimeDxe.inf { + + NULL|MdeModulePkg/Library/VarCheckUefiLib/VarCheckUefiLib.inf + } !endif # # Following are the DXE drivers --=20 2.32.0