From nobody Tue Feb 10 05:17:44 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+105367+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+105367+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1685355454; cv=none; d=zohomail.com; s=zohoarc; b=RrsZp3zFurnjSCeYM/m4ZZzpqEmGdlw4MChNAqGSHCsGKtQ7lMXUbTfdESf70TzgNqnR21SQkY+Ova5OZE6Y1gW0bLVOTuWpXS9JWtyHds+zhXtiU4inROmCPyPHc9svNtk5UY0gK4PXcd/wWaWNcoA136DAheDXrYz/99DKPls= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1685355454; 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=13XHVrIB79xrk43NEuj4JuBZz8C1he7lXlI/6ziDwKo=; b=B3ls0Do+NHByOpDe2xWYY11gQX3CBjmL140GFYbQ1DgZ5mVBhaNBrFwGfLNkfQvhPBC6BKNvFKviXswzZfK7N1Tj2H21r/9qYay3+M1+Q6XYjihsB+Iz/uV2xiPQRnVnCNlTKw/eT+GbgkLE+1+LiX0yMeTg4Si2lWmof+a6F+8= 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+105367+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 1685355454020604.2908460087955; Mon, 29 May 2023 03:17:34 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id fLxIYY1788612xKkjhOZGq7I; Mon, 29 May 2023 03:17:33 -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.46679.1685355452933148710 for ; Mon, 29 May 2023 03:17:33 -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 6BB5560AF5; Mon, 29 May 2023 10:17:32 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 71213C433EF; Mon, 29 May 2023 10:17:29 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Ray Ni , Jiewen Yao , Gerd Hoffmann , Taylor Beebe , Oliver Smith-Denny , Dandan Bi , Liming Gao , "Kinney, Michael D" , Leif Lindholm , Michael Kubacki Subject: [edk2-devel] [RFC PATCH 05/11] MdeModulePkg/DxeCore: Use memory mapped FV protocol to avoid image copy Date: Mon, 29 May 2023 12:16:59 +0200 Message-Id: <20230529101705.2476949-6-ardb@kernel.org> In-Reply-To: <20230529101705.2476949-1-ardb@kernel.org> References: <20230529101705.2476949-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: qFLDIRBkQvBvAX7Q3bfeXkYBx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1685355453; bh=TP2WDpS5ZZ0YNiccb3Zh1RbucK/UlcJT0ns9lgQCqgQ=; h=Cc:Date:From:Reply-To:Subject:To; b=JqWTGtXxBHnNOgHuvyXcgwS25+ciqNSP7tH7yFT07EIRmYJFZD3ikCpnBeVHL6LK3T4 AXNhSpmK+5HnkeNr8mD+qbs+6NCI1GRknXAoAuuc5+X9jd61FhwlcKlAS4NvYjDGqolCW cP/c/TBJZKVbwu7kqXKa6wjHRiaeQaQJeaQ= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1685355455893100003 Content-Type: text/plain; charset="utf-8" Use the memory mapped FV protocol to obtain the existing location in memory and the size of an image being loaded from a firmware volume. This removes the need to do a memcopy of the file data. Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/DxeMain.h | 1 + MdeModulePkg/Core/Dxe/DxeMain.inf | 3 + MdeModulePkg/Core/Dxe/Image/Image.c | 111 +++++++++++++++++--- MdeModulePkg/Include/Protocol/MemoryMappedFv.h | 59 +++++++++++ MdeModulePkg/MdeModulePkg.dec | 3 + 5 files changed, 163 insertions(+), 14 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/DxeMain.h b/MdeModulePkg/Core/Dxe/DxeMai= n.h index 43daa037be441150..a695b457c79b65bb 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.h +++ b/MdeModulePkg/Core/Dxe/DxeMain.h @@ -45,6 +45,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include #include #include +#include #include #include #include diff --git a/MdeModulePkg/Core/Dxe/DxeMain.inf b/MdeModulePkg/Core/Dxe/DxeM= ain.inf index 35d5bf0dee6f7f3f..a7175cb364b9b5de 100644 --- a/MdeModulePkg/Core/Dxe/DxeMain.inf +++ b/MdeModulePkg/Core/Dxe/DxeMain.inf @@ -153,6 +153,9 @@ [Protocols] gEfiLoadedImageDevicePathProtocolGuid ## PRODUCES gEfiHiiPackageListProtocolGuid ## SOMETIMES_PRODUCES gEfiSmmBase2ProtocolGuid ## SOMETIMES_CONSUMES + ## PRODUCES + ## CONSUMES + gEdkiiMemoryMappedFvProtocolGuid gEdkiiPeCoffImageEmulatorProtocolGuid ## SOMETIMES_CONSUMES =20 # Arch Protocols diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Im= age/Image.c index f30e369370a09609..3dfab4829b3ca17f 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -1043,6 +1043,76 @@ CoreUnloadAndCloseImage ( CoreFreePool (Image); } =20 +/** + Get the image file data and size directly from a memory mapped FV + + If FilePath is NULL, then NULL is returned. + If FileSize is NULL, then NULL is returned. + If AuthenticationStatus is NULL, then NULL is returned. + + @param[in] FvHandle The firmware volume handle + @param[in] FilePath The pointer to the device path of = the file + that is abstracted to the file buf= fer. + @param[out] FileSize The pointer to the size of the abs= tracted + file buffer. + @param[out] AuthenticationStatus Pointer to the authentication stat= us. + + @retval NULL FilePath is NULL, or FileSize is NULL, or AuthenticationS= tatus + is NULL, or the file is not memory mapped + @retval other The abstracted file buffer. +**/ +STATIC +VOID * +GetFileFromMemoryMappedFv ( + IN EFI_HANDLE FvHandle, + IN CONST EFI_DEVICE_PATH_PROTOCOL *FilePath, + OUT UINTN *FileSize, + OUT UINT32 *AuthenticationStatus + ) +{ + EDKII_MEMORY_MAPPED_FV_PROTOCOL *MemMappedFv; + CONST EFI_GUID *NameGuid; + EFI_PHYSICAL_ADDRESS Address; + EFI_STATUS Status; + + if ((FilePath =3D=3D NULL) || + (FileSize =3D=3D NULL) || + (AuthenticationStatus =3D=3D NULL)) + { + return NULL; + } + + NameGuid =3D EfiGetNameGuidFromFwVolDevicePathNode ( + (CONST MEDIA_FW_VOL_FILEPATH_DEVICE_PATH *)FilePath); + if (NameGuid =3D=3D NULL) { + return NULL; + } + + Status =3D gBS->HandleProtocol ( + FvHandle, + &gEdkiiMemoryMappedFvProtocolGuid, + (VOID **)&MemMappedFv + ); + if (EFI_ERROR (Status)) { + ASSERT (Status =3D=3D EFI_UNSUPPORTED); + return NULL; + } + + Status =3D MemMappedFv->GetLocationAndSize ( + MemMappedFv, + NameGuid, + EFI_SECTION_PE32, + &Address, + FileSize, + AuthenticationStatus + ); + if (EFI_ERROR (Status) || (Address > (MAX_ADDRESS - *FileSize))) { + return NULL; + } + + return (VOID *)(UINTN)Address; +} + /** Loads an EFI image into memory and returns a handle to the image. =20 @@ -1164,6 +1234,16 @@ CoreLoadImageCommon ( Status =3D CoreLocateDevicePath (&gEfiFirmwareVolume2ProtocolGuid, &Ha= ndleFilePath, &DeviceHandle); if (!EFI_ERROR (Status)) { ImageIsFromFv =3D TRUE; + + // + // If possible, use the memory mapped file image directly, rather th= an copying it into a buffer + // + FHand.Source =3D GetFileFromMemoryMappedFv ( + DeviceHandle, + HandleFilePath, + &FHand.SourceSize, + &AuthenticationStatus + ); } else { HandleFilePath =3D FilePath; Status =3D CoreLocateDevicePath (&gEfiSimpleFileSystemProtoc= olGuid, &HandleFilePath, &DeviceHandle); @@ -1187,21 +1267,24 @@ CoreLoadImageCommon ( // // Get the source file buffer by its device path. // - FHand.Source =3D GetFileBufferByFilePath ( - BootPolicy, - FilePath, - &FHand.SourceSize, - &AuthenticationStatus - ); if (FHand.Source =3D=3D NULL) { - Status =3D EFI_NOT_FOUND; - } else { - FHand.FreeBuffer =3D TRUE; - if (ImageIsFromLoadFile) { - // - // LoadFile () may cause the device path of the Handle be updated. - // - OriginalFilePath =3D AppendDevicePath (DevicePathFromHandle (Devic= eHandle), Node); + FHand.Source =3D GetFileBufferByFilePath ( + BootPolicy, + FilePath, + &FHand.SourceSize, + &AuthenticationStatus + ); + + if (FHand.Source =3D=3D NULL) { + Status =3D EFI_NOT_FOUND; + } else { + FHand.FreeBuffer =3D TRUE; + if (ImageIsFromLoadFile) { + // + // LoadFile () may cause the device path of the Handle be update= d. + // + OriginalFilePath =3D AppendDevicePath (DevicePathFromHandle (Dev= iceHandle), Node); + } } } } diff --git a/MdeModulePkg/Include/Protocol/MemoryMappedFv.h b/MdeModulePkg/= Include/Protocol/MemoryMappedFv.h new file mode 100644 index 0000000000000000..821009122113a658 --- /dev/null +++ b/MdeModulePkg/Include/Protocol/MemoryMappedFv.h @@ -0,0 +1,59 @@ +/** @file + Protocol to obtain information about files in memory mapped firmware vol= umes + + Copyright (c) 2023, Google LLC. All rights reserved.
+ + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#ifndef EDKII_MEMORY_MAPPED_FV_H_ +#define EDKII_MEMORY_MAPPED_FV_H_ + +#define EDKII_MEMORY_MAPPED_FV_PROTOCOL_GUID \ + { 0xb9bfa973, 0x5384, 0x441e, { 0xa4, 0xe7, 0x20, 0xe6, 0x5d, 0xaf, 0x2e= , 0x0f } } + +typedef struct _EDKII_MEMORY_MAPPED_FV_PROTOCOL EDKII_MEMORY_MAPPED_FV_PRO= TOCOL; + +// +// Function Prototypes +// + +/** + Get the physical address and size of a file's section in a memory mapped= FV + + @param[in] This The protocol pointer + @param[in] NameGuid The name GUID of the file + @param[in] SectionType The file section from which to retrieve addres= s and size + @param[out] FileAddress The physical address of the file + @param[out] FileSize The size of the file + @param[out] AuthStatus The authentication status associated with the = file + + @retval EFI_SUCCESS Information about the file was retrieved = successfully. + @retval EFI_INVALID_PARAMETER FileAddress was NULL, FileSize was NULL, = AuthStatus + was NULL. + @retval EFI_NOT_FOUND No section of the specified type could be= located in + the specified file. + +**/ +typedef +EFI_STATUS +(EFIAPI *GET_LOCATION_AND_SIZE)( + IN EDKII_MEMORY_MAPPED_FV_PROTOCOL *This, + IN CONST EFI_GUID *NameGuid, + IN EFI_SECTION_TYPE SectionType, + OUT EFI_PHYSICAL_ADDRESS *FileAddress, + OUT UINTN *FileSize, + OUT UINT32 *AuthStatus + ); + +// +// Protocol interface structure +// +struct _EDKII_MEMORY_MAPPED_FV_PROTOCOL { + GET_LOCATION_AND_SIZE GetLocationAndSize; +}; + +extern EFI_GUID gEdkiiMemoryMappedFvProtocolGuid; + +#endif diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec index d65dae18aa81e569..2d72ac733d82195e 100644 --- a/MdeModulePkg/MdeModulePkg.dec +++ b/MdeModulePkg/MdeModulePkg.dec @@ -679,6 +679,9 @@ [Protocols] ## Include/Protocol/PlatformBootManager.h gEdkiiPlatformBootManagerProtocolGuid =3D { 0xaa17add4, 0x756c, 0x460d, = { 0x94, 0xb8, 0x43, 0x88, 0xd7, 0xfb, 0x3e, 0x59 } } =20 + ## Include/Protocol/MemoryMappedFv.h + gEdkiiMemoryMappedFvProtocolGuid =3D { 0xb9bfa973, 0x5384, 0x441e, { 0xa= 4, 0xe7, 0x20, 0xe6, 0x5d, 0xaf, 0x2e, 0x0f } } + # # [Error.gEfiMdeModulePkgTokenSpaceGuid] # 0x80000001 | Invalid value provided. --=20 2.39.2 -=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 (#105367): https://edk2.groups.io/g/devel/message/105367 Mute This Topic: https://groups.io/mt/99197138/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-