diff --git a/EmbeddedPkg/Include/Library/PrePiLib.h b/EmbeddedPkg/Include/Library/PrePiLib.h index 28e638ee78..6d2f8a98dd 100644 --- a/EmbeddedPkg/Include/Library/PrePiLib.h +++ b/EmbeddedPkg/Include/Library/PrePiLib.h @@ -688,7 +688,7 @@ LoadUefiImage ( IN VOID *UefiImage, IN UINT32 UefiImageSize, OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, + OUT UINT32 *ImageSize, OUT EFI_PHYSICAL_ADDRESS *EntryPoint ); diff --git a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c index a877c5c89b..98d5eee1cc 100644 --- a/EmbeddedPkg/Library/PrePiLib/PrePiLib.c +++ b/EmbeddedPkg/Library/PrePiLib/PrePiLib.c @@ -26,28 +26,32 @@ EFI_STATUS EFIAPI LoadUefiImage ( IN VOID *UefiImage, - IN UINT32 UefiImageSize, + IN UINT32 UefiImageSize, OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, + OUT UINT32 *DestinationSize, OUT EFI_PHYSICAL_ADDRESS *EntryPoint ) { RETURN_STATUS Status; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; + UINT32 ImageSize; VOID *Buffer; UINT32 BufferSize; + UINT32 BufferPages; UINT32 BufferAlignment; Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize); ASSERT_EFI_ERROR (Status); - BufferSize = UefiImageGetImageSize (&ImageContext); + ImageSize = UefiImageGetImageSize (&ImageContext); + BufferPages = EFI_SIZE_TO_PAGES (ImageSize); + BufferSize = EFI_PAGES_TO_SIZE (BufferPages); BufferAlignment = UefiImageGetSegmentAlignment (&ImageContext); // // Allocate Memory for the image // - Buffer = AllocateAlignedCodePages (EFI_SIZE_TO_PAGES (BufferSize), BufferAlignment); + Buffer = AllocateAlignedCodePages (BufferPages, BufferAlignment); ASSERT (Buffer != 0); // @@ -56,9 +60,9 @@ LoadUefiImage ( Status = UefiImageLoadImageForExecution (&ImageContext, Buffer, BufferSize, NULL, 0); ASSERT_EFI_ERROR (Status); - *ImageAddress = (UINTN) Buffer; - *ImageSize = BufferSize; - *EntryPoint = (UINTN) UefiImageLoaderGetImageEntryPoint (&ImageContext); + *ImageAddress = (UINTN) Buffer; + *DestinationSize = BufferSize; + *EntryPoint = (UINTN) UefiImageLoaderGetImageEntryPoint (&ImageContext); return Status; } @@ -80,7 +84,7 @@ LoadDxeCoreFromFfsFile ( VOID *UefiImage; UINT32 UefiImageSize; EFI_PHYSICAL_ADDRESS ImageAddress; - UINT64 ImageSize; + UINT32 DestinationSize; EFI_PHYSICAL_ADDRESS EntryPoint; VOID *BaseOfStack; VOID *TopOfStack; @@ -92,7 +96,7 @@ LoadDxeCoreFromFfsFile ( return Status; } - Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &ImageSize, &EntryPoint); + Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &DestinationSize, &EntryPoint); // For NT32 Debug Status = SecWinNtPeiLoadFile (UefiImage, &ImageAddress, &ImageSize, &EntryPoint); ASSERT_EFI_ERROR (Status); @@ -102,7 +106,7 @@ LoadDxeCoreFromFfsFile ( Status = FfsGetFileInfo (FileHandle, &FvFileInfo); ASSERT_EFI_ERROR (Status); - BuildModuleHob (&FvFileInfo.FileName, (EFI_PHYSICAL_ADDRESS)(UINTN)ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, EntryPoint); + BuildModuleHob (&FvFileInfo.FileName, (EFI_PHYSICAL_ADDRESS)(UINTN)ImageAddress, DestinationSize, EntryPoint); DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)EntryPoint)); diff --git a/MdeModulePkg/Core/Dxe/Image/Image.c b/MdeModulePkg/Core/Dxe/Image/Image.c index cd67818517..af69e84d40 100644 --- a/MdeModulePkg/Core/Dxe/Image/Image.c +++ b/MdeModulePkg/Core/Dxe/Image/Image.c @@ -395,19 +395,13 @@ CheckAndMarkFixLoadingMemoryUsageBitMap ( **/ EFI_STATUS GetUefiImageFixLoadingAssignedAddress ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, - OUT EFI_PHYSICAL_ADDRESS *LoadAddress + OUT EFI_PHYSICAL_ADDRESS *LoadAddress, + IN UINT64 ValueInSectionHeader, + IN UINT32 ImageDestSize ) { EFI_STATUS Status; - UINT64 ValueInSectionHeader; EFI_PHYSICAL_ADDRESS FixLoadingAddress; - UINT32 SizeOfImage; - - Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); - if (RETURN_ERROR (Status)) { - return Status; - } if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) { // @@ -425,8 +419,7 @@ GetUefiImageFixLoadingAssignedAddress ( // // Check if the memory range is available. // - SizeOfImage = UefiImageGetImageSize (ImageContext); - Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, SizeOfImage); + Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, ImageDestSize); *LoadAddress = FixLoadingAddress; DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status = %r \n", (VOID *)(UINTN)FixLoadingAddress, Status)); @@ -511,8 +504,11 @@ CoreLoadPeImage ( { EFI_STATUS Status; BOOLEAN DstBufAlocated; - UINT32 Size; - UINT32 Alignment; + UINT32 ImageSize; + UINT32 ImageAlignment; + UINT64 ValueInSectionHeader; + UINT32 DstBufPages; + UINT32 DstBufSize; EFI_MEMORY_TYPE ImageCodeMemoryType; EFI_MEMORY_TYPE ImageDataMemoryType; UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RelocationData; @@ -558,8 +554,10 @@ CoreLoadPeImage ( return EFI_UNSUPPORTED; } - Size = UefiImageGetImageSize (ImageContext); - Alignment = UefiImageGetSegmentAlignment (ImageContext); + ImageSize = UefiImageGetImageSize (ImageContext); + DstBufPages = EFI_SIZE_TO_PAGES (ImageSize); + DstBufSize = EFI_PAGES_TO_SIZE (DstBufPages); + ImageAlignment = UefiImageGetSegmentAlignment (ImageContext); BufferAddress = 0; // @@ -570,7 +568,7 @@ CoreLoadPeImage ( // // Allocate Destination Buffer as caller did not pass it in // - Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size); + Image->NumberOfPages = DstBufPages; // // If the image relocations have not been stripped, then load at any address. @@ -585,7 +583,12 @@ CoreLoadPeImage ( // a specified address. // if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0 ) { - Status = GetUefiImageFixLoadingAssignedAddress (ImageContext, &BufferAddress); + Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); + if (RETURN_ERROR (Status)) { + return Status; + } + + Status = GetUefiImageFixLoadingAssignedAddress (&BufferAddress, ValueInSectionHeader, DstBufSize); if (!EFI_ERROR (Status)) { if (BufferAddress != UefiImageGetPreferredAddress (ImageContext) && UefiImageGetRelocsStripped (ImageContext)) { @@ -605,7 +608,7 @@ CoreLoadPeImage ( Status = AllocatePagesEx ( AllocateAddress, ImageCodeMemoryType, - Image->NumberOfPages, + DstBufPages, &BufferAddress ); } @@ -614,8 +617,8 @@ CoreLoadPeImage ( Status = AllocateAlignedPagesEx ( AllocateAnyPages, ImageCodeMemoryType, - Image->NumberOfPages, - Alignment, + DstBufPages, + ImageAlignment, &BufferAddress ); } @@ -645,14 +648,14 @@ CoreLoadPeImage ( if ((Image->NumberOfPages != 0) && (Image->NumberOfPages < - (EFI_SIZE_TO_PAGES (Size)))) + DstBufPages)) { - Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size); + Image->NumberOfPages = DstBufPages; ASSERT (FALSE); return EFI_BUFFER_TOO_SMALL; } - Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size); + Image->NumberOfPages = DstBufPages; BufferAddress = *DstBuffer; } @@ -691,7 +694,7 @@ CoreLoadPeImage ( Status = UefiImageLoadImageForExecution ( ImageContext, (VOID *)(UINTN)BufferAddress, - Size, + DstBufSize, RelocationData, RelocDataSize ); @@ -715,7 +718,7 @@ CoreLoadPeImage ( // Image->Type = UefiImageGetSubsystem (ImageContext); Image->Info.ImageBase = (VOID *)(UINTN)BufferAddress; - Image->Info.ImageSize = UefiImageGetImageSize (ImageContext); + Image->Info.ImageSize = ImageSize; Image->Info.ImageCodeType = ImageCodeMemoryType; Image->Info.ImageDataType = ImageDataMemoryType; if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) { diff --git a/MdeModulePkg/Core/Pei/Image/Image.c b/MdeModulePkg/Core/Pei/Image/Image.c index 2afb2ffe32..ca57bfaefc 100644 --- a/MdeModulePkg/Core/Pei/Image/Image.c +++ b/MdeModulePkg/Core/Pei/Image/Image.c @@ -102,20 +102,14 @@ CheckAndMarkFixLoadingMemoryUsageBitMap ( **/ EFI_STATUS GetUefiImageFixLoadingAssignedAddress ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, - IN PEI_CORE_INSTANCE *Private, - OUT EFI_PHYSICAL_ADDRESS *LoadAddress + OUT EFI_PHYSICAL_ADDRESS *LoadAddress, + IN UINT64 ValueInSectionHeader, + IN UINT32 ImageDestSize, + IN PEI_CORE_INSTANCE *Private ) { EFI_STATUS Status; - UINT64 ValueInSectionHeader; EFI_PHYSICAL_ADDRESS FixLoadingAddress; - UINT32 SizeOfImage; - - Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); - if (RETURN_ERROR (Status)) { - return Status; - } if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) { // @@ -133,8 +127,7 @@ GetUefiImageFixLoadingAssignedAddress ( // // Check if the memory range is available. // - SizeOfImage = UefiImageGetImageSize (ImageContext); - Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, SizeOfImage); + Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, ImageDestSize); *LoadAddress = FixLoadingAddress; DEBUG ((EFI_D_INFO|EFI_D_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address 0x%11p. Status= %r \n", (VOID *)(UINTN)FixLoadingAddress, Status)); @@ -171,15 +164,18 @@ LoadAndRelocateUefiImage ( EFI_STATUS Status; BOOLEAN Success; PEI_CORE_INSTANCE *Private; - UINT32 DynamicImageSize; - UINT32 DynamicImageAlignment; + UINT32 ImageSize; + UINT32 ImageAlignment; + UINT64 ValueInSectionHeader; BOOLEAN IsXipImage; EFI_STATUS ReturnStatus; BOOLEAN IsS3Boot; BOOLEAN IsPeiModule; BOOLEAN IsRegisterForShadow; EFI_FV_FILE_INFO FileInfo; - EFI_PHYSICAL_ADDRESS LoadAddress; + UINT32 DestinationPages; + UINT32 DestinationSize; + EFI_PHYSICAL_ADDRESS Destination; UINT16 Machine; BOOLEAN LoadDynamically; @@ -250,7 +246,9 @@ LoadAndRelocateUefiImage ( } LoadDynamically = FALSE; - DynamicImageSize = 0; + ImageSize = UefiImageGetImageSize (ImageContext); + DestinationPages = EFI_SIZE_TO_PAGES (ImageSize); + DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages); // // Allocate Memory for the image when memory is ready, and image is relocatable. @@ -266,11 +264,13 @@ LoadAndRelocateUefiImage ( Success = FALSE; if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { - Status = GetUefiImageFixLoadingAssignedAddress(ImageContext, Private, &LoadAddress); - if (!EFI_ERROR (Status)){ - DynamicImageSize = UefiImageGetImageSize (ImageContext); + Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); + if (!RETURN_ERROR (Status)) { + Status = GetUefiImageFixLoadingAssignedAddress(&Destination, ValueInSectionHeader, DestinationSize, Private); + } - Success = LoadAddress == UefiImageGetPreferredAddress (ImageContext); + if (!EFI_ERROR (Status)){ + Success = Destination == UefiImageGetPreferredAddress (ImageContext); if (!Success) { DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since relocs have been stripped.\n")); @@ -284,14 +284,13 @@ LoadAndRelocateUefiImage ( // // Allocate more buffer to avoid buffer overflow. // - DynamicImageSize = UefiImageGetImageSize (ImageContext); - DynamicImageAlignment = UefiImageGetSegmentAlignment (ImageContext); + ImageAlignment = UefiImageGetSegmentAlignment (ImageContext); - LoadAddress = (UINTN)AllocateAlignedCodePages ( - EFI_SIZE_TO_PAGES (DynamicImageSize), - DynamicImageAlignment + Destination = (UINTN)AllocateAlignedCodePages ( + DestinationPages, + ImageAlignment ); - Success = LoadAddress != 0; + Success = Destination != 0; } if (Success) { @@ -301,8 +300,8 @@ LoadAndRelocateUefiImage ( // Status = UefiImageLoadImageForExecution ( ImageContext, - (VOID *) (UINTN)LoadAddress, - DynamicImageSize, + (VOID *) (UINTN)Destination, + DestinationSize, NULL, 0 ); diff --git a/MdeModulePkg/Core/PiSmmCore/Dispatcher.c b/MdeModulePkg/Core/PiSmmCore/Dispatcher.c index 648371270d..f024bc1433 100644 --- a/MdeModulePkg/Core/PiSmmCore/Dispatcher.c +++ b/MdeModulePkg/Core/PiSmmCore/Dispatcher.c @@ -216,23 +216,16 @@ CheckAndMarkFixLoadingMemoryUsageBitMap ( **/ EFI_STATUS GetUefiImageFixLoadingAssignedAddress ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, - OUT EFI_PHYSICAL_ADDRESS *LoadAddress + OUT EFI_PHYSICAL_ADDRESS *LoadAddress, + IN UINT64 ValueInSectionHeader, + IN UINT32 ImageDestSize ) { RETURN_STATUS Status; - UINT64 ValueInSectionHeader; EFI_PHYSICAL_ADDRESS FixLoadingAddress; - UINT32 SizeOfImage; - - Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); - if (RETURN_ERROR (Status)) { - return Status; - } FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + ValueInSectionHeader); - SizeOfImage = UefiImageGetImageSize (ImageContext); - Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, SizeOfImage); + Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, ImageDestSize); *LoadAddress = FixLoadingAddress; DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status)); @@ -269,14 +262,16 @@ SmmLoadImage ( UINTN FilePathSize; VOID *Buffer; UINTN Size; - UINTN PageCount; + UINT32 DstBufferPages; EFI_GUID *NameGuid; EFI_STATUS Status; EFI_STATUS SecurityStatus; EFI_HANDLE DeviceHandle; + UINT32 ImageSize; + UINT32 ImageAlignment; + UINT64 ValueInSectionHeader; VOID *DstBuffer; UINT32 DstBufferSize; - UINT32 DstBufferAlignment; EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath; EFI_DEVICE_PATH_PROTOCOL *HandleFilePath; @@ -426,8 +421,10 @@ SmmLoadImage ( return EFI_UNSUPPORTED; } - DstBufferSize = UefiImageGetImageSize (ImageContext); - DstBufferAlignment = UefiImageGetSegmentAlignment (ImageContext); + ImageSize = UefiImageGetImageSize (ImageContext); + DstBufferPages = EFI_SIZE_TO_PAGES (ImageSize); + DstBufferSize = EFI_PAGES_TO_SIZE (DstBufferPages); + ImageAlignment = UefiImageGetSegmentAlignment (ImageContext); // // if Loading module at Fixed Address feature is enabled, then cut out a memory range started from TESG BASE // to hold the Smm driver code @@ -436,22 +433,24 @@ SmmLoadImage ( // // Get the fixed loading address assigned by Build tool // - Status = GetUefiImageFixLoadingAssignedAddress (ImageContext, &LoadAddress); + Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader); + if (!RETURN_ERROR (Status)) { + Status = GetUefiImageFixLoadingAssignedAddress (&LoadAddress, ValueInSectionHeader, DstBufferSize); + } + if (!EFI_ERROR (Status)) { // // Since the memory range to load Smm core already been cut out, so no need to allocate and free this range // following statements is to bypass SmmFreePages // - PageCount = 0; - DstBuffer = (VOID *)(UINTN)LoadAddress; + DstBufferPages = 0; + DstBuffer = (VOID *)(UINTN)LoadAddress; } else { DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n")); // // allocate the memory to load the SMM driver // - PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DstBufferSize); - - DstBuffer = AllocateAlignedCodePages (PageCount, DstBufferAlignment); + DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment); if (DstBuffer == NULL) { if (Buffer != NULL) { gBS->FreePool (Buffer); @@ -461,9 +460,7 @@ SmmLoadImage ( } } } else { - PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DstBufferSize); - - DstBuffer = AllocateAlignedCodePages (PageCount, DstBufferAlignment); + DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment); if (DstBuffer == NULL) { if (Buffer != NULL) { gBS->FreePool (Buffer); @@ -488,7 +485,7 @@ SmmLoadImage ( gBS->FreePool (Buffer); } - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } @@ -497,7 +494,7 @@ SmmLoadImage ( // DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (ImageContext); DriverEntry->ImageBuffer = (UINTN)DstBuffer; - DriverEntry->NumberOfPage = PageCount; + DriverEntry->NumberOfPage = DstBufferPages; // // Allocate a Loaded Image Protocol in EfiBootServicesData @@ -508,7 +505,7 @@ SmmLoadImage ( gBS->FreePool (Buffer); } - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } @@ -536,14 +533,14 @@ SmmLoadImage ( gBS->FreePool (Buffer); } - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath)); DriverEntry->LoadedImage->ImageBase = DstBuffer; - DriverEntry->LoadedImage->ImageSize = UefiImageGetImageSize (ImageContext); + DriverEntry->LoadedImage->ImageSize = ImageSize; DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode; DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData; @@ -557,14 +554,14 @@ SmmLoadImage ( } gBS->FreePool (DriverEntry->LoadedImage->FilePath); - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize(FilePath)); DriverEntry->SmmLoadedImage.ImageBase = DstBuffer; - DriverEntry->SmmLoadedImage.ImageSize = UefiImageGetImageSize (ImageContext); + DriverEntry->SmmLoadedImage.ImageSize = ImageSize; DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode; DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData; diff --git a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c index b63968f7b2..524a74bdc4 100644 --- a/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c +++ b/MdeModulePkg/Core/PiSmmCore/PiSmmIpl.c @@ -976,8 +976,10 @@ ExecuteSmmCoreFromSmram ( EFI_STATUS Status; VOID *SourceBuffer; UINTN SourceSize; + UINT32 ImageSize; + UINT32 ImageAlignment; + UINT32 DestinationPages; UINT32 DestinationSize; - UINT32 DestinationAlignment; UINT32 AlignSubtrahend; UINTN PageCount; EFI_IMAGE_ENTRY_POINT EntryPoint; @@ -1014,8 +1016,10 @@ ExecuteSmmCoreFromSmram ( return EFI_UNSUPPORTED; } - DestinationSize = UefiImageGetImageSize (&gSmmCorePrivate->PiSmmCoreImageContext); - DestinationAlignment = UefiImageGetSegmentAlignment (&gSmmCorePrivate->PiSmmCoreImageContext); + ImageSize = UefiImageGetImageSize (&gSmmCorePrivate->PiSmmCoreImageContext); + DestinationPages = EFI_SIZE_TO_PAGES (ImageSize); + DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages); + ImageAlignment = UefiImageGetSegmentAlignment (&gSmmCorePrivate->PiSmmCoreImageContext); // // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to // the address assigned by build tool. @@ -1042,9 +1046,9 @@ ExecuteSmmCoreFromSmram ( // AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND ( SmramRange->CpuStart + SmramRange->PhysicalSize, - DestinationAlignment + ImageAlignment ); - PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DestinationSize) + (UINTN)EFI_SIZE_TO_PAGES ((UINTN)AlignSubtrahend); + PageCount = (UINTN)DestinationPages + (UINTN)EFI_SIZE_TO_PAGES ((UINTN)AlignSubtrahend); ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0); ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount)); @@ -1067,9 +1071,9 @@ ExecuteSmmCoreFromSmram ( // AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND ( SmramRange->CpuStart + SmramRange->PhysicalSize, - DestinationAlignment + ImageAlignment ); - PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DestinationSize) + (UINTN)EFI_SIZE_TO_PAGES ((UINTN)AlignSubtrahend); + PageCount = (UINTN)DestinationPages + (UINTN)EFI_SIZE_TO_PAGES ((UINTN)AlignSubtrahend); ASSERT ((SmramRange->PhysicalSize & EFI_PAGE_MASK) == 0); ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount)); diff --git a/SourceLevelDebugPkg/Library/UefiImageExtraActionLibDebug/UefiImageExtraActionLib.c b/SourceLevelDebugPkg/Library/UefiImageExtraActionLibDebug/UefiImageExtraActionLib.c index 8ab9824572..35f7db1258 100644 --- a/SourceLevelDebugPkg/Library/UefiImageExtraActionLibDebug/UefiImageExtraActionLib.c +++ b/SourceLevelDebugPkg/Library/UefiImageExtraActionLibDebug/UefiImageExtraActionLib.c @@ -45,8 +45,8 @@ IsDrxEnabled ( **/ VOID UefiImageLoaderExtraActionCommon ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, - IN UINTN Signature + IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, + IN UINTN Signature ) { BOOLEAN InterruptState; @@ -217,7 +217,7 @@ UefiImageLoaderExtraActionCommon ( VOID EFIAPI UefiImageLoaderRelocateImageExtraAction ( - IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext + IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext ) { UefiImageLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE); diff --git a/StandaloneMmPkg/Core/Dispatcher.c b/StandaloneMmPkg/Core/Dispatcher.c index 663b847465..75f2e9a06c 100644 --- a/StandaloneMmPkg/Core/Dispatcher.c +++ b/StandaloneMmPkg/Core/Dispatcher.c @@ -274,9 +274,10 @@ MmLoadImage ( { UINT32 ImageSize; UINT32 ImageAlignment; - UINTN PageCount; EFI_STATUS Status; VOID *DstBuffer; + UINT32 DstBufferPages; + UINT32 DstBufferSize; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName)); @@ -291,12 +292,12 @@ MmLoadImage ( return Status; } - ImageSize = UefiImageGetImageSize (&ImageContext, &ImageSize); + ImageSize = UefiImageGetImageSize (&ImageContext); + DstBufferPages = EFI_SIZE_TO_PAGES (ImageSize); + DstBufferSize = EFI_PAGES_TO_SIZE (DstBufferPages); ImageAlignment = UefiImageGetSegmentAlignment (&ImageContext); - PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN) ImageSize); - - DstBuffer = AllocateAlignedCodePages (PageCount, ImageAlignment); + DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment); if (DstBuffer == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -304,9 +305,9 @@ MmLoadImage ( // // Load the image to our new buffer // - Status = UefiImageLoadImageForExecution (&ImageContext, (VOID *) (UINTN) DstBuffer, ImageSize, NULL, 0); + Status = UefiImageLoadImageForExecution (&ImageContext, DstBuffer, DstBufferSize, NULL, 0); if (EFI_ERROR (Status)) { - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } @@ -315,7 +316,7 @@ MmLoadImage ( // DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext); DriverEntry->ImageBuffer = (UINTN)DstBuffer; - DriverEntry->NumberOfPage = PageCount; + DriverEntry->NumberOfPage = DstBufferPages; if (mEfiSystemTable != NULL) { Status = mEfiSystemTable->BootServices->AllocatePool ( @@ -324,7 +325,7 @@ MmLoadImage ( (VOID **)&DriverEntry->LoadedImage ); if (EFI_ERROR (Status)) { - FreeAlignedPages (DstBuffer, PageCount); + FreeAlignedPages (DstBuffer, DstBufferPages); return Status; } @@ -368,7 +369,7 @@ MmLoadImage ( DEBUG (( DEBUG_INFO | DEBUG_LOAD, "Loading MM driver at 0x%11p EntryPoint=0x%11p ", - (VOID *)(UINTN)ImageBase, + DstBuffer, FUNCTION_ENTRY_POINT (UefiImageLoaderGetImageEntryPoint (&ImageContext)) )); diff --git a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c index 2d0df9a9c1..d8e8862fb5 100644 --- a/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c +++ b/UefiPayloadPkg/UefiPayloadEntry/LoadDxeCore.c @@ -11,7 +11,7 @@ /** Loads and relocates a PE/COFF image - @param[in] UefiImage Point to a Pe/Coff image. + @param[in] UefiImage Point to a Pe/Coff image. @param[out] ImageAddress The image memory address after relocation. @param[out] ImageSize The image size. @param[out] EntryPoint The image entry point. @@ -21,17 +21,19 @@ **/ EFI_STATUS LoadUefiImage ( - IN VOID *UefiImage, - IN UINT32 UefiImageSize, + IN VOID *UefiImage, + IN UINT32 UefiImageSize, OUT EFI_PHYSICAL_ADDRESS *ImageAddress, - OUT UINT64 *ImageSize, + OUT UINT64 *DestinationSize, OUT EFI_PHYSICAL_ADDRESS *EntryPoint ) { RETURN_STATUS Status; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; - UINT32 BufferSize; - UINT32 BufferAlignment; + UINT32 ImageSize; + UINT32 ImageAlignment; + UINT32 BufferPages; + UINT32 BufferSize; VOID *Buffer; Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize); @@ -40,13 +42,15 @@ LoadUefiImage ( return Status; } - BufferSize = UefiImageGetImageSize (&ImageContext); - BufferAlignment = UefiImageGetSegmentAlignment (&ImageContext); + ImageSize = UefiImageGetImageSize (&ImageContext); + BufferPages = EFI_SIZE_TO_PAGES (ImageSize); + BufferSize = EFI_PAGES_TO_SIZE (BufferPages); + ImageAlignment = UefiImageGetSegmentAlignment (&ImageContext); // // Allocate Memory for the image // - Buffer = AllocateAlignedCodePages (EFI_SIZE_TO_PAGES (BufferSize), BufferAlignment); + Buffer = AllocateAlignedCodePages (BufferPages, ImageAlignment); if (Buffer == NULL) { return EFI_OUT_OF_RESOURCES; } @@ -66,9 +70,9 @@ LoadUefiImage ( return Status; } - *ImageAddress = (UINTN)Buffer; - *ImageSize = BufferSize; - *EntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext); + *ImageAddress = (UINTN)Buffer; + *DestinationSize = BufferSize; + *EntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext); return EFI_SUCCESS; } @@ -230,7 +234,7 @@ LoadDxeCore ( VOID *UefiImage; UINT32 UefiImageSize; EFI_PHYSICAL_ADDRESS ImageAddress; - UINT64 ImageSize; + UINT64 DestinationSize; PayloadFv = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdPayloadFdMemBase); @@ -268,12 +272,12 @@ LoadDxeCore ( // // Get DXE core info // - Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &ImageSize, DxeCoreEntryPoint); + Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &DestinationSize, DxeCoreEntryPoint); if (EFI_ERROR (Status)) { return Status; } - BuildModuleHob (&FileHeader->Name, ImageAddress, EFI_SIZE_TO_PAGES ((UINT32)ImageSize) * EFI_PAGE_SIZE, *DxeCoreEntryPoint); + BuildModuleHob (&FileHeader->Name, ImageAddress, DestinationSize, *DxeCoreEntryPoint); return EFI_SUCCESS; }