From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=HxLHvto7; spf=pass (domain: kernel.org, ip: 139.178.84.217, mailfrom: ardb@kernel.org) 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) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B4F62C433C1; Tue, 6 Sep 2022 11:59:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662465575; bh=OrjeSsAJpwOWLxVdeDaBsKF3Tvt/fg79NIRSelxJXiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HxLHvto7RuCsnaMWNje1TBI2s/AdCWNnkTdznj5icN1egj7Z7dZZjcVG+AvQ83xKy Q2HLT+abJ7G/shzg35QcQb4tOonFZiVKvpLEPSC8m/CkTkq0q1Ko0kIogNbGoxYA1J D++fe3LgPslMu3wMQ2Kl3S+TMYa7wmVBADVHYoA+FsZA0FWiUskJjrWQ4mv3Stdu9b uWbcM9VebvYgyQlNqAr0HCaThVF1mvsoEAoX6SCCSGo9r1juhVp3q1x7beV3UZOGp3 RVJ4novSbVq/hbpDFlLWgMdbqiTI0BUvMr8L3Jsm9sExnakFqROWA6uXSF/C6PwtrE YO07PyHIJRnEA== 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: [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> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220906115926.115493-1-ardb@kernel.org> References: <20220906115926.115493-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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=0D + Entry point to a EFI/DXE driver. This version is specific to QEMU, and t= ies=0D + dispatch of the driver in question on the value of a QEMU fw_cfg boolean= =0D + variable which is referenced by name via a fixed pointer PCD.=0D +=0D +Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.
=0D +Copyright (c) 2022, Google LLC. All rights reserved.
=0D +SPDX-License-Identifier: BSD-2-Clause-Patent=0D +=0D +**/=0D +=0D +#include =0D +=0D +#include =0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + Unloads an image from memory.=0D +=0D + This function is a callback that a driver registers to do cleanup=0D + when the UnloadImage boot service function is called.=0D +=0D + @param ImageHandle The handle to the image to unload.=0D +=0D + @return Status returned by all unload().=0D +=0D +**/=0D +STATIC=0D +EFI_STATUS=0D +EFIAPI=0D +_DriverUnloadHandler (=0D + EFI_HANDLE ImageHandle=0D + )=0D +{=0D + EFI_STATUS Status;=0D +=0D + //=0D + // If an UnloadImage() handler is specified, then call it=0D + //=0D + Status =3D ProcessModuleUnloadList (ImageHandle);=0D +=0D + //=0D + // If the driver specific unload handler does not return an error, then = call=0D + // all of the library destructors. If the unload handler returned an er= ror,=0D + // then the driver can not be unloaded, and the library destructors shou= ld=0D + // not be called=0D + //=0D + if (!EFI_ERROR (Status)) {=0D + ProcessLibraryDestructorList (ImageHandle, gST);=0D + }=0D +=0D + //=0D + // Return the status from the driver specific unload handler=0D + //=0D + return Status;=0D +}=0D +=0D +/**=0D + The entry point of PE/COFF Image for a DXE Driver, DXE Runtime Driver, o= r=0D + UEFI Driver.=0D +=0D + @param ImageHandle The image handle of the DXE Driver, D= XE=0D + Runtime Driver, or UEFI Driver.=0D + @param SystemTable A pointer to the EFI System Table.=0D +=0D + @retval EFI_SUCCESS The DXE Driver, DXE Runtime Driver, o= r=0D + UEFI Driver exited normally.=0D + @retval EFI_INCOMPATIBLE_VERSION _gUefiDriverRevision is greater than= =0D + SystemTable->Hdr.Revision.=0D + @retval Other Return value from=0D + ProcessModuleEntryPointList().=0D +=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +_ModuleEntryPoint (=0D + IN EFI_HANDLE ImageHandle,=0D + IN EFI_SYSTEM_TABLE *SystemTable=0D + )=0D +{=0D + EFI_STATUS Status;=0D + EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;=0D + RETURN_STATUS RetStatus;=0D + BOOLEAN Enabled;=0D +=0D + if (_gUefiDriverRevision !=3D 0) {=0D + //=0D + // Make sure that the EFI/UEFI spec revision of the platform is >=3D E= FI/UEFI=0D + // spec revision of the driver=0D + //=0D + if (SystemTable->Hdr.Revision < _gUefiDriverRevision) {=0D + return EFI_INCOMPATIBLE_VERSION;=0D + }=0D + }=0D +=0D + //=0D + // Call constructor for all libraries=0D + //=0D + ProcessLibraryConstructorList (ImageHandle, SystemTable);=0D +=0D + //=0D + // Install unload handler...=0D + //=0D + if (_gDriverUnloadImageCount !=3D 0) {=0D + Status =3D gBS->HandleProtocol (=0D + ImageHandle,=0D + &gEfiLoadedImageProtocolGuid,=0D + (VOID **)&LoadedImage=0D + );=0D + ASSERT_EFI_ERROR (Status);=0D + LoadedImage->Unload =3D _DriverUnloadHandler;=0D + }=0D +=0D + RetStatus =3D QemuFwCfgParseBool (=0D + FixedPcdGetPtr (PcdEntryPointOverrideFwCfgVarName),=0D + &Enabled);=0D + if (!RETURN_ERROR (RetStatus) && !Enabled) {=0D + //=0D + // The QEMU fw_cfg variable tells us not to load this image. So abort= .=0D + //=0D + Status =3D EFI_ABORTED;=0D + } else {=0D + //=0D + // Call the driver entry point=0D + //=0D + Status =3D ProcessModuleEntryPointList (ImageHandle, SystemTable);=0D + }=0D +=0D + //=0D + // If all of the drivers returned errors, or we if are aborting, then in= voke=0D + // all of the library destructors=0D + //=0D + if (EFI_ERROR (Status)) {=0D + ProcessLibraryDestructorList (ImageHandle, SystemTable);=0D + }=0D +=0D + //=0D + // Return the cumulative return status code from all of the driver entry= =0D + // points=0D + //=0D + return Status;=0D +}=0D 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=0D +# Entry point to a EFI/DXE driver. This version is specific to QEMU, and = ties=0D +# dispatch of the driver in question on the value of a QEMU fw_cfg boolea= n=0D +# variable which is referenced by name via a fixed pointer PCD.=0D +#=0D +# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
=0D +# Copyright (c) 2022, Google LLC. All rights reserved.
=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 1.29=0D + BASE_NAME =3D UefiDriverEntryPointFwCfgOverrideLib= =0D + FILE_GUID =3D 73349b79-f148-43b8-b24e-9098a6f3e1db= =0D + MODULE_TYPE =3D UEFI_DRIVER=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D UefiDriverEntryPoint|DXE_DRIVER DXE_R= UNTIME_DRIVER UEFI_DRIVER=0D +=0D +[Sources]=0D + UefiDriverEntryPointFwCfgOverrideLib.c=0D +=0D +[Packages]=0D + MdePkg/MdePkg.dec=0D + OvmfPkg/OvmfPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseLib=0D + DebugLib=0D + QemuFwCfgSimpleParserLib=0D + UefiBootServicesTableLib=0D +=0D +[Protocols]=0D + gEfiLoadedImageProtocolGuid ## SOMETIMES_CONSUMES=0D +=0D +[FixedPcd]=0D + gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName=0D +=0D +#=0D +# For UEFI drivers, these architectural protocols defined in PI 1.0 spec n= eed=0D +# to be appended and merged to the final dependency section.=0D +#=0D +[Depex.common.UEFI_DRIVER]=0D + gEfiBdsArchProtocolGuid AND=0D + gEfiCpuArchProtocolGuid AND=0D + gEfiMetronomeArchProtocolGuid AND=0D + gEfiMonotonicCounterArchProtocolGuid AND=0D + gEfiRealTimeClockArchProtocolGuid AND=0D + gEfiResetArchProtocolGuid AND=0D + gEfiRuntimeArchProtocolGuid AND=0D + gEfiSecurityArchProtocolGuid AND=0D + gEfiTimerArchProtocolGuid AND=0D + gEfiVariableWriteArchProtocolGuid AND=0D + gEfiVariableArchProtocolGuid AND=0D + gEfiWatchdogTimerArchProtocolGuid=0D 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)=0D gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize|0x200000|UINT32|0x65=0D =0D + ## The QEMU fw_cfg variable that UefiDriverEntryPointFwCfgOverrideLib wi= ll=0D + # check to decide whether to abort dispatch of the driver it is linked = into.=0D + gUefiOvmfPkgTokenSpaceGuid.PcdEntryPointOverrideFwCfgVarName|""|VOID*|0x= 68=0D +=0D [PcdsDynamic, PcdsDynamicEx]=0D gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2=0D gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x1= 0=0D --=20 2.35.1