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.46671.1685355444391069298 for ; Mon, 29 May 2023 03:17:24 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=SRZlXU8c; 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 E126F613EA; Mon, 29 May 2023 10:17:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E293EC433A0; Mon, 29 May 2023 10:17:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1685355443; bh=MY+opErYBUGhy8iPIblZOaV12DWI8mjOUsu3SUKinPU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SRZlXU8cSoKQzKZi9FffUsJHhyxyT4zXg3S54l5Tcww0QK1UB8fqgaPwTWW2dNhlC 2Yq3eyDDEuDgnjBWBy0Fq24NX5Me2L2u+HI0xsU78AVX3f6W/dzfo/AsbZPU91P4nQ v4Pjdd7/Aqd0dBJuyDMJJPjnDUUyiN6AoSsH9IF2S+w78odas6DLEXFryQanYu1fZN fjsKrKqRaV2UtbFz1EE6oQyOIHIUlja8F164xkjlaPLtE+mtXmAznYkNg4LYlAjnvJ Hj7FFl6KyeVDHZU1iPpjHiDLKlj9tskzqGUrM6uC21Hg+bGyZDwJKQgRgvxLysCpbO 0cbAsNutorvgg== 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: [RFC PATCH 02/11] MdeModulePkg/DxeCore: Remove unused DstBuffer arg from LoadImage Date: Mon, 29 May 2023 12:16:56 +0200 Message-Id: <20230529101705.2476949-3-ardb@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529101705.2476949-1-ardb@kernel.org> References: <20230529101705.2476949-1-ardb@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable The DstBuffer and NumberOfPages arguments to CoreLoadImageCommon () are never set by its only caller CoreLoadImage() so let's drop them from the prototype. Signed-off-by: Ard Biesheuvel --- MdeModulePkg/Core/Dxe/Image/Image.c | 174 +++++++------------- 1 file changed, 56 insertions(+), 118 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Im= age/Image.c index 2f2dfe5d0496dc4f..6625d0cd0ff82107 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -559,7 +559,6 @@ CoreIsImageTypeSupported ( boot selection.=0D @param Pe32Handle The handle of PE32 image=0D @param Image PE image to be loaded=0D - @param DstBuffer The buffer to store the image=0D @param Attribute The bit mask of attributes to set for th= e load=0D PE image=0D =0D @@ -570,17 +569,16 @@ CoreIsImageTypeSupported ( @retval EFI_BUFFER_TOO_SMALL Buffer for image is too small=0D =0D **/=0D +STATIC=0D EFI_STATUS=0D CoreLoadPeImage (=0D IN BOOLEAN BootPolicy,=0D IN VOID *Pe32Handle,=0D IN LOADED_IMAGE_PRIVATE_DATA *Image,=0D - IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,=0D IN UINT32 Attribute=0D )=0D {=0D EFI_STATUS Status;=0D - BOOLEAN DstBufAlocated;=0D UINTN Size;=0D =0D ZeroMem (&Image->ImageContext, sizeof (Image->ImageContext));=0D @@ -633,99 +631,67 @@ CoreLoadPeImage ( }=0D =0D //=0D - // Allocate memory of the correct memory type aligned on the required im= age boundary=0D + // Allocate Destination Buffer as caller did not pass it in=0D //=0D - DstBufAlocated =3D FALSE;=0D - if (DstBuffer =3D=3D 0) {=0D - //=0D - // Allocate Destination Buffer as caller did not pass it in=0D - //=0D =0D - if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {=0D - Size =3D (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.= SectionAlignment;=0D - } else {=0D - Size =3D (UINTN)Image->ImageContext.ImageSize;=0D - }=0D + if (Image->ImageContext.SectionAlignment > EFI_PAGE_SIZE) {=0D + Size =3D (UINTN)Image->ImageContext.ImageSize + Image->ImageContext.Se= ctionAlignment;=0D + } else {=0D + Size =3D (UINTN)Image->ImageContext.ImageSize;=0D + }=0D =0D - Image->NumberOfPages =3D EFI_SIZE_TO_PAGES (Size);=0D + Image->NumberOfPages =3D EFI_SIZE_TO_PAGES (Size);=0D =0D - //=0D - // If the image relocations have not been stripped, then load at any a= ddress.=0D - // Otherwise load at the address at which it was linked.=0D - //=0D - // Memory below 1MB should be treated reserved for CSM and there shoul= d be=0D - // no modules whose preferred load addresses are below 1MB.=0D - //=0D - Status =3D EFI_OUT_OF_RESOURCES;=0D - //=0D - // If Loading Module At Fixed Address feature is enabled, the module s= hould be loaded to=0D - // a specified address.=0D - //=0D - if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) !=3D 0 ) {=0D - Status =3D GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageCo= ntext));=0D -=0D - if (EFI_ERROR (Status)) {=0D - //=0D - // If the code memory is not ready, invoke CoreAllocatePage with A= llocateAnyPages to load the driver.=0D - //=0D - DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Loadin= g module at fixed address failed since specified memory is not available.\n= "));=0D -=0D - Status =3D CoreAllocatePages (=0D - AllocateAnyPages,=0D - (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryTy= pe),=0D - Image->NumberOfPages,=0D - &Image->ImageContext.ImageAddress=0D - );=0D - }=0D - } else {=0D - if ((Image->ImageContext.ImageAddress >=3D 0x100000) || Image->Image= Context.RelocationsStripped) {=0D - Status =3D CoreAllocatePages (=0D - AllocateAddress,=0D - (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryTy= pe),=0D - Image->NumberOfPages,=0D - &Image->ImageContext.ImageAddress=0D - );=0D - }=0D -=0D - if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) = {=0D - Status =3D CoreAllocatePages (=0D - AllocateAnyPages,=0D - (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryTy= pe),=0D - Image->NumberOfPages,=0D - &Image->ImageContext.ImageAddress=0D - );=0D - }=0D - }=0D + //=0D + // If the image relocations have not been stripped, then load at any add= ress.=0D + // Otherwise load at the address at which it was linked.=0D + //=0D + // Memory below 1MB should be treated reserved for CSM and there should = be=0D + // no modules whose preferred load addresses are below 1MB.=0D + //=0D + Status =3D EFI_OUT_OF_RESOURCES;=0D + //=0D + // If Loading Module At Fixed Address feature is enabled, the module sho= uld be loaded to=0D + // a specified address.=0D + //=0D + if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) !=3D 0 ) {=0D + Status =3D GetPeCoffImageFixLoadingAssignedAddress (&(Image->ImageCont= ext));=0D =0D if (EFI_ERROR (Status)) {=0D - return Status;=0D - }=0D + //=0D + // If the code memory is not ready, invoke CoreAllocatePage with All= ocateAnyPages to load the driver.=0D + //=0D + DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Loading = module at fixed address failed since specified memory is not available.\n")= );=0D =0D - DstBufAlocated =3D TRUE;=0D + Status =3D CoreAllocatePages (=0D + AllocateAnyPages,=0D + (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType= ),=0D + Image->NumberOfPages,=0D + &Image->ImageContext.ImageAddress=0D + );=0D + }=0D } else {=0D - //=0D - // Caller provided the destination buffer=0D - //=0D -=0D - if (Image->ImageContext.RelocationsStripped && (Image->ImageContext.Im= ageAddress !=3D DstBuffer)) {=0D - //=0D - // If the image relocations were stripped, and the caller provided a= =0D - // destination buffer address that does not match the address that t= he=0D - // image is linked at, then the image cannot be loaded.=0D - //=0D - return EFI_INVALID_PARAMETER;=0D + if ((Image->ImageContext.ImageAddress >=3D 0x100000) || Image->ImageCo= ntext.RelocationsStripped) {=0D + Status =3D CoreAllocatePages (=0D + AllocateAddress,=0D + (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType= ),=0D + Image->NumberOfPages,=0D + &Image->ImageContext.ImageAddress=0D + );=0D }=0D =0D - if ((Image->NumberOfPages !=3D 0) &&=0D - (Image->NumberOfPages <=0D - (EFI_SIZE_TO_PAGES ((UINTN)Image->ImageContext.ImageSize + Image-= >ImageContext.SectionAlignment))))=0D - {=0D - Image->NumberOfPages =3D EFI_SIZE_TO_PAGES ((UINTN)Image->ImageConte= xt.ImageSize + Image->ImageContext.SectionAlignment);=0D - return EFI_BUFFER_TOO_SMALL;=0D + if (EFI_ERROR (Status) && !Image->ImageContext.RelocationsStripped) {= =0D + Status =3D CoreAllocatePages (=0D + AllocateAnyPages,=0D + (EFI_MEMORY_TYPE)(Image->ImageContext.ImageCodeMemoryType= ),=0D + Image->NumberOfPages,=0D + &Image->ImageContext.ImageAddress=0D + );=0D }=0D + }=0D =0D - Image->NumberOfPages =3D EFI_SIZE_TO_PAGES ((UINTN)Image->= ImageContext.ImageSize + Image->ImageContext.SectionAlignment);=0D - Image->ImageContext.ImageAddress =3D DstBuffer;=0D + if (EFI_ERROR (Status)) {=0D + return Status;=0D }=0D =0D Image->ImageBasePage =3D Image->ImageContext.ImageAddress;=0D @@ -875,12 +841,9 @@ CoreLoadPeImage ( //=0D // Free memory.=0D //=0D -=0D - if (DstBufAlocated) {=0D - CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages)= ;=0D - Image->ImageContext.ImageAddress =3D 0;=0D - Image->ImageBasePage =3D 0;=0D - }=0D + CoreFreePages (Image->ImageContext.ImageAddress, Image->NumberOfPages);= =0D + Image->ImageContext.ImageAddress =3D 0;=0D + Image->ImageBasePage =3D 0;=0D =0D if (Image->ImageContext.FixupData !=3D NULL) {=0D CoreFreePool (Image->ImageContext.FixupData);=0D @@ -1094,12 +1057,6 @@ CoreUnloadAndCloseImage ( @param SourceBuffer If not NULL, a pointer to the memory loc= ation=0D containing a copy of the image to be loa= ded.=0D @param SourceSize The size in bytes of SourceBuffer.=0D - @param DstBuffer The buffer to store the image=0D - @param NumberOfPages If not NULL, it inputs a pointer to the = page=0D - number of DstBuffer and outputs a pointe= r to=0D - the page number of the image. If this nu= mber is=0D - not enough, return EFI_BUFFER_TOO_SMALL= and=0D - this parameter contains the required num= ber.=0D @param ImageHandle Pointer to the returned image handle tha= t is=0D created when the image is successfully l= oaded.=0D @param Attribute The bit mask of attributes to set for th= e load=0D @@ -1132,8 +1089,6 @@ CoreLoadImageCommon ( IN EFI_DEVICE_PATH_PROTOCOL *FilePath,=0D IN VOID *SourceBuffer OPTIONAL,=0D IN UINTN SourceSize,=0D - IN EFI_PHYSICAL_ADDRESS DstBuffer OPTIONAL,=0D - IN OUT UINTN *NumberOfPages OPTIONAL,=0D OUT EFI_HANDLE *ImageHandle,=0D IN UINT32 Attribute=0D )=0D @@ -1342,12 +1297,7 @@ CoreLoadImageCommon ( Image->Info.Revision =3D EFI_LOADED_IMAGE_PROTOCOL_REVISION;=0D Image->Info.FilePath =3D DuplicateDevicePath (FilePath);=0D Image->Info.ParentHandle =3D ParentImageHandle;=0D -=0D - if (NumberOfPages !=3D NULL) {=0D - Image->NumberOfPages =3D *NumberOfPages;=0D - } else {=0D - Image->NumberOfPages =3D 0;=0D - }=0D + Image->NumberOfPages =3D 0;=0D =0D //=0D // Install the protocol interfaces for this image=0D @@ -1367,21 +1317,11 @@ CoreLoadImageCommon ( //=0D // Load the image.=0D //=0D - Status =3D CoreLoadPeImage (BootPolicy, &FHand, Image, DstBuffer, Attrib= ute);=0D + Status =3D CoreLoadPeImage (BootPolicy, &FHand, Image, Attribute);=0D if (EFI_ERROR (Status)) {=0D - if ((Status =3D=3D EFI_BUFFER_TOO_SMALL) || (Status =3D=3D EFI_OUT_OF_= RESOURCES)) {=0D - if (NumberOfPages !=3D NULL) {=0D - *NumberOfPages =3D Image->NumberOfPages;=0D - }=0D - }=0D -=0D goto Done;=0D }=0D =0D - if (NumberOfPages !=3D NULL) {=0D - *NumberOfPages =3D Image->NumberOfPages;=0D - }=0D -=0D //=0D // Register the image in the Debug Image Info Table if the attribute is = set=0D //=0D @@ -1473,7 +1413,7 @@ CoreLoadImageCommon ( //=0D if (EFI_ERROR (Status)) {=0D if (Image !=3D NULL) {=0D - CoreUnloadAndCloseImage (Image, (BOOLEAN)(DstBuffer =3D=3D 0));=0D + CoreUnloadAndCloseImage (Image, TRUE);=0D Image =3D NULL;=0D }=0D } else if (EFI_ERROR (SecurityStatus)) {=0D @@ -1546,8 +1486,6 @@ CoreLoadImage ( FilePath,=0D SourceBuffer,=0D SourceSize,=0D - (EFI_PHYSICAL_ADDRESS)(UINTN)NULL,=0D - NULL,=0D ImageHandle,=0D EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION | EFI_LOAD_P= E_IMAGE_ATTRIBUTE_DEBUG_IMAGE_INFO_TABLE_REGISTRATION=0D );=0D --=20 2.39.2