From nobody Sat Feb 7 08:43:52 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+93215+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+93215+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1662465578; cv=none; d=zohomail.com; s=zohoarc; b=e/8dY9V02e8RUKb3RFu+aELcW+PL7zu0MkmmOGOe9JzWSCslCj/kriHRdPUlu+Mme5xplhw8rYhVhDZ2PxtN7ruFljnXzMdcIQFe+vJRLUGWtSx+jC9R47q8hxBFC/mQGSimAKvfCaYOCE79f6EGnuT6Cw3PlHgt3FRXQrZBt5k= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1662465578; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=3dd3s9AHW5KBw0SmFWWTK92srr/2wlwRg9sEC1t62PA=; b=J5cl0WAaGihT+/yiX2EZpefL5ZqFR106QYtUGjf95T9UYh4PKQwBQZDbmUg5ly975suI/CChZ7FhvIAnJveaoO0fphnuW4mVeQ4vn168mLtd+gC0KsjjS3BGOsQ/3Am4wbeNtLOM5ETtvorVpvZbn09uOr6T4ilgRM/0AgqeX+0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+93215+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1662465578001832.9222306402584; Tue, 6 Sep 2022 04:59:38 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id avY8YY1788612xuahHSL2FvX; Tue, 06 Sep 2022 04:59:37 -0700 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.3062.1662465576881433796 for ; Tue, 06 Sep 2022 04:59:37 -0700 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 388F3614CE; Tue, 6 Sep 2022 11:59:36 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4F62C433C1; Tue, 6 Sep 2022 11:59:33 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Yuan Yu , Laszlo Ersek , Gerd Hoffmann , Pawel Polawski , Oliver Steffen , Jiewen Yao , "Brian J . Johnson" Subject: [edk2-devel] [PATCH v3 1/3] OvmfPkg: Introduce alternate UefiDriverEntrypoint to inhibit driver load Date: Tue, 6 Sep 2022 13:59:24 +0200 Message-Id: <20220906115926.115493-2-ardb@kernel.org> In-Reply-To: <20220906115926.115493-1-ardb@kernel.org> References: <20220906115926.115493-1-ardb@kernel.org> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,ardb@kernel.org X-Gm-Message-State: 2wsGI8yRYwLx6dE7VdPyCmMGx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1662465577; bh=TvmuB2LZsrdXEDufb/jYElmLhB/a042Jj3RwG4bhzBw=; h=Cc:Date:From:Reply-To:Subject:To; b=PdWzrfHtdcXTxxq+WoWQL1zmRB6whjPeELJHigB9zZ/leR/rNElz201FMO4d0HUeGka 4jz2/m96LwR/ESiut7Epe5ErBGZvdGaZg5cQ+ZypzX1geOCebawDuGlBJNlJPp+HylND5 QdO/nmw8DBHdQzPrhSKGiwj3t1RrJs4eiuk= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1662465579621100006 Content-Type: text/plain; charset="utf-8" Add a new library that can be incorporated into any driver built from source, and which permits loading of the driver to be inhibited based on the value of a QEMU fw_cfg boolean variable. This will be used in a subsequent patch to allow dispatch of the IPv4 and IPv6 network protocol driver to be controlled from the QEMU command line. This approach is based on the notion that all UEFI and DXE drivers share a single UefiDriverEntryPoint implementation, which we can easily swap out at build time with one that will abort execution based on the value of some QEMU fw_cfg variable. Signed-off-by: Ard Biesheuvel Reviewed-by: Laszlo Ersek --- OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDriverEntryPointF= wCfgOverrideLib.c | 147 ++++++++++++++++++++ OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDriverEntryPointF= wCfgOverrideLib.inf | 57 ++++++++ OvmfPkg/OvmfPkg.dec = | 4 + 3 files changed, 208 insertions(+) diff --git a/OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDrive= rEntryPointFwCfgOverrideLib.c b/OvmfPkg/Library/UefiDriverEntryPointFwCfgOv= errideLib/UefiDriverEntryPointFwCfgOverrideLib.c new file mode 100644 index 000000000000..6eaf0cfd16ad --- /dev/null +++ b/OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDriverEntryP= ointFwCfgOverrideLib.c @@ -0,0 +1,147 @@ +/** @file + Entry point to a EFI/DXE driver. This version is specific to QEMU, and t= ies + dispatch of the driver in question on the value of a QEMU fw_cfg boolean + variable which is referenced by name via a fixed pointer PCD. + +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
+Copyright (c) 2022, Google LLC. All rights reserved.
+SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include + +#include + +#include +#include +#include +#include +#include + +/** + Unloads an image from memory. + + This function is a callback that a driver registers to do cleanup + when the UnloadImage boot service function is called. + + @param ImageHandle The handle to the image to unload. + + @return Status returned by all unload(). + +**/ +STATIC +EFI_STATUS +EFIAPI +_DriverUnloadHandler ( + EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + + // + // If an UnloadImage() handler is specified, then call it + // + Status =3D ProcessModuleUnloadList (ImageHandle); + + // + // If the driver specific unload handler does not return an error, then = call + // all of the library destructors. If the unload handler returned an er= ror, + // then the driver can not be unloaded, and the library destructors shou= ld + // not be called + // + if (!EFI_ERROR (Status)) { + ProcessLibraryDestructorList (ImageHandle, gST); + } + + // + // Return the status from the driver specific unload handler + // + return Status; +} + +/** + The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, or + UEFI Driver. + + @param ImageHandle The image handle of the DXE Driver, D= XE + Runtime Driver, or UEFI Driver. + @param SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The DXE Driver, DXE Runtime Driver, or + UEFI Driver exited normally. + @retval EFI_INCOMPATIBLE_VERSION _gUefiDriverRevision is greater than + SystemTable->Hdr.Revision. + @retval Other Return value from + ProcessModuleEntryPointList(). + +**/ +EFI_STATUS +EFIAPI +_ModuleEntryPoint ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + EFI_LOADED_IMAGE_PROTOCOL *LoadedImage; + RETURN_STATUS RetStatus; + BOOLEAN Enabled; + + if (_gUefiDriverRevision !=3D 0) { + // + // Make sure that the EFI/UEFI spec revision of the platform is >=3D E= FI/UEFI + // spec revision of the driver + // + if (SystemTable->Hdr.Revision < _gUefiDriverRevision) { + return EFI_INCOMPATIBLE_VERSION; + } + } + + // + // Call constructor for all libraries + // + ProcessLibraryConstructorList (ImageHandle, SystemTable); + + // + // Install unload handler... + // + if (_gDriverUnloadImageCount !=3D 0) { + Status =3D gBS->HandleProtocol ( + ImageHandle, + &gEfiLoadedImageProtocolGuid, + (VOID **)&LoadedImage + ); + ASSERT_EFI_ERROR (Status); + LoadedImage->Unload =3D _DriverUnloadHandler; + } + + RetStatus =3D QemuFwCfgParseBool ( + FixedPcdGetPtr (PcdEntryPointOverrideFwCfgVarName), + &Enabled); + if (!RETURN_ERROR (RetStatus) && !Enabled) { + // + // The QEMU fw_cfg variable tells us not to load this image. So abort. + // + Status =3D EFI_ABORTED; + } else { + // + // Call the driver entry point + // + Status =3D ProcessModuleEntryPointList (ImageHandle, SystemTable); + } + + // + // If all of the drivers returned errors, or we if are aborting, then in= voke + // all of the library destructors + // + if (EFI_ERROR (Status)) { + ProcessLibraryDestructorList (ImageHandle, SystemTable); + } + + // + // Return the cumulative return status code from all of the driver entry + // points + // + return Status; +} diff --git a/OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDrive= rEntryPointFwCfgOverrideLib.inf b/OvmfPkg/Library/UefiDriverEntryPointFwCfg= OverrideLib/UefiDriverEntryPointFwCfgOverrideLib.inf new file mode 100644 index 000000000000..263e00ceef66 --- /dev/null +++ b/OvmfPkg/Library/UefiDriverEntryPointFwCfgOverrideLib/UefiDriverEntryP= ointFwCfgOverrideLib.inf @@ -0,0 +1,57 @@ +## @file +# Entry point to a EFI/DXE driver. This version is specific to QEMU, and = ties +# dispatch of the driver in question on the value of a QEMU fw_cfg boolean +# variable which is referenced by name via a fixed pointer PCD. +# +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+# Copyright (c) 2022, Google LLC. All rights reserved.
+# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION =3D 1.29 + BASE_NAME =3D UefiDriverEntryPointFwCfgOverrideLib + FILE_GUID =3D 73349b79-f148-43b8-b24e-9098a6f3e1db + MODULE_TYPE =3D UEFI_DRIVER + VERSION_STRING =3D 1.0 + LIBRARY_CLASS =3D UefiDriverEntryPoint|DXE_DRIVER DXE_R= UNTIME_DRIVER UEFI_DRIVER + +[Sources] + UefiDriverEntryPointFwCfgOverrideLib.c + +[Packages] + MdePkg/MdePkg.dec + OvmfPkg/OvmfPkg.dec + +[LibraryClasses] + BaseLib + DebugLib + QemuFwCfgSimpleParserLib + UefiBootServicesTableLib + +[Protocols] + gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES + +[FixedPcd] + gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName + +# +# For UEFI drivers, these architectural protocols defined in PI 1.0 spec n= eed +# to be appended and merged to the final dependency section. +# +[Depex.common.UEFI_DRIVER] + gEfiBdsArchProtocolGuid AND + gEfiCpuArchProtocolGuid AND + gEfiMetronomeArchProtocolGuid AND + gEfiMonotonicCounterArchProtocolGuid AND + gEfiRealTimeClockArchProtocolGuid AND + gEfiResetArchProtocolGuid AND + gEfiRuntimeArchProtocolGuid AND + gEfiSecurityArchProtocolGuid AND + gEfiTimerArchProtocolGuid AND + gEfiVariableWriteArchProtocolGuid AND + gEfiVariableArchProtocolGuid AND + gEfiWatchdogTimerArchProtocolGuid diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index 5af76a540529..9816aa41377d 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -399,6 +399,10 @@ [PcdsFixedAtBuild] ## The Tdx accept page size. 0x1000(4k),0x200000(2M) gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize|0x200000|UINT32|0x65 =20 + ## The QEMU fw_cfg variable that UefiDriverEntryPointFwCfgOverrideLib wi= ll + # check to decide whether to abort dispatch of the driver it is linked = into. + gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName|""|VOID*|0x= 68 + [PcdsDynamic, PcdsDynamicEx] gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x10 --=20 2.35.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#93215): https://edk2.groups.io/g/devel/message/93215 Mute This Topic: https://groups.io/mt/93498688/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-