UefiImageLib: Ensure consistently passing the full buffer size

This commit is contained in:
Mikhail Krichanov 2023-05-09 16:40:33 +03:00
parent 002702671a
commit c2aa73cf3d
9 changed files with 142 additions and 130 deletions

View File

@ -688,7 +688,7 @@ LoadUefiImage (
IN VOID *UefiImage, IN VOID *UefiImage,
IN UINT32 UefiImageSize, IN UINT32 UefiImageSize,
OUT EFI_PHYSICAL_ADDRESS *ImageAddress, OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
OUT UINT64 *ImageSize, OUT UINT32 *ImageSize,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint OUT EFI_PHYSICAL_ADDRESS *EntryPoint
); );

View File

@ -26,28 +26,32 @@ EFI_STATUS
EFIAPI EFIAPI
LoadUefiImage ( LoadUefiImage (
IN VOID *UefiImage, IN VOID *UefiImage,
IN UINT32 UefiImageSize, IN UINT32 UefiImageSize,
OUT EFI_PHYSICAL_ADDRESS *ImageAddress, OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
OUT UINT64 *ImageSize, OUT UINT32 *DestinationSize,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint OUT EFI_PHYSICAL_ADDRESS *EntryPoint
) )
{ {
RETURN_STATUS Status; RETURN_STATUS Status;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
UINT32 ImageSize;
VOID *Buffer; VOID *Buffer;
UINT32 BufferSize; UINT32 BufferSize;
UINT32 BufferPages;
UINT32 BufferAlignment; UINT32 BufferAlignment;
Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize); Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize);
ASSERT_EFI_ERROR (Status); 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); BufferAlignment = UefiImageGetSegmentAlignment (&ImageContext);
// //
// Allocate Memory for the image // Allocate Memory for the image
// //
Buffer = AllocateAlignedCodePages (EFI_SIZE_TO_PAGES (BufferSize), BufferAlignment); Buffer = AllocateAlignedCodePages (BufferPages, BufferAlignment);
ASSERT (Buffer != 0); ASSERT (Buffer != 0);
// //
@ -56,9 +60,9 @@ LoadUefiImage (
Status = UefiImageLoadImageForExecution (&ImageContext, Buffer, BufferSize, NULL, 0); Status = UefiImageLoadImageForExecution (&ImageContext, Buffer, BufferSize, NULL, 0);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
*ImageAddress = (UINTN) Buffer; *ImageAddress = (UINTN) Buffer;
*ImageSize = BufferSize; *DestinationSize = BufferSize;
*EntryPoint = (UINTN) UefiImageLoaderGetImageEntryPoint (&ImageContext); *EntryPoint = (UINTN) UefiImageLoaderGetImageEntryPoint (&ImageContext);
return Status; return Status;
} }
@ -80,7 +84,7 @@ LoadDxeCoreFromFfsFile (
VOID *UefiImage; VOID *UefiImage;
UINT32 UefiImageSize; UINT32 UefiImageSize;
EFI_PHYSICAL_ADDRESS ImageAddress; EFI_PHYSICAL_ADDRESS ImageAddress;
UINT64 ImageSize; UINT32 DestinationSize;
EFI_PHYSICAL_ADDRESS EntryPoint; EFI_PHYSICAL_ADDRESS EntryPoint;
VOID *BaseOfStack; VOID *BaseOfStack;
VOID *TopOfStack; VOID *TopOfStack;
@ -92,7 +96,7 @@ LoadDxeCoreFromFfsFile (
return Status; 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); // For NT32 Debug Status = SecWinNtPeiLoadFile (UefiImage, &ImageAddress, &ImageSize, &EntryPoint);
ASSERT_EFI_ERROR (Status); ASSERT_EFI_ERROR (Status);
@ -102,7 +106,7 @@ LoadDxeCoreFromFfsFile (
Status = FfsGetFileInfo (FileHandle, &FvFileInfo); Status = FfsGetFileInfo (FileHandle, &FvFileInfo);
ASSERT_EFI_ERROR (Status); 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)); DEBUG ((DEBUG_INFO | DEBUG_LOAD, "Loading DxeCore at 0x%10p EntryPoint=0x%10p\n", (VOID *)(UINTN)ImageAddress, (VOID *)(UINTN)EntryPoint));

View File

@ -395,19 +395,13 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
**/ **/
EFI_STATUS EFI_STATUS
GetUefiImageFixLoadingAssignedAddress ( 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; EFI_STATUS Status;
UINT64 ValueInSectionHeader;
EFI_PHYSICAL_ADDRESS FixLoadingAddress; EFI_PHYSICAL_ADDRESS FixLoadingAddress;
UINT32 SizeOfImage;
Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader);
if (RETURN_ERROR (Status)) {
return Status;
}
if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) { if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {
// //
@ -425,8 +419,7 @@ GetUefiImageFixLoadingAssignedAddress (
// //
// Check if the memory range is available. // Check if the memory range is available.
// //
SizeOfImage = UefiImageGetImageSize (ImageContext); Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, ImageDestSize);
Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, SizeOfImage);
*LoadAddress = FixLoadingAddress; *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)); 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; EFI_STATUS Status;
BOOLEAN DstBufAlocated; BOOLEAN DstBufAlocated;
UINT32 Size; UINT32 ImageSize;
UINT32 Alignment; UINT32 ImageAlignment;
UINT64 ValueInSectionHeader;
UINT32 DstBufPages;
UINT32 DstBufSize;
EFI_MEMORY_TYPE ImageCodeMemoryType; EFI_MEMORY_TYPE ImageCodeMemoryType;
EFI_MEMORY_TYPE ImageDataMemoryType; EFI_MEMORY_TYPE ImageDataMemoryType;
UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RelocationData; UEFI_IMAGE_LOADER_RUNTIME_CONTEXT *RelocationData;
@ -558,8 +554,10 @@ CoreLoadPeImage (
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
Size = UefiImageGetImageSize (ImageContext); ImageSize = UefiImageGetImageSize (ImageContext);
Alignment = UefiImageGetSegmentAlignment (ImageContext); DstBufPages = EFI_SIZE_TO_PAGES (ImageSize);
DstBufSize = EFI_PAGES_TO_SIZE (DstBufPages);
ImageAlignment = UefiImageGetSegmentAlignment (ImageContext);
BufferAddress = 0; BufferAddress = 0;
// //
@ -570,7 +568,7 @@ CoreLoadPeImage (
// //
// Allocate Destination Buffer as caller did not pass it in // 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. // If the image relocations have not been stripped, then load at any address.
@ -585,7 +583,12 @@ CoreLoadPeImage (
// a specified address. // a specified address.
// //
if (PcdGet64 (PcdLoadModuleAtFixAddressEnable) != 0 ) { 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 (!EFI_ERROR (Status)) {
if (BufferAddress != UefiImageGetPreferredAddress (ImageContext) && UefiImageGetRelocsStripped (ImageContext)) { if (BufferAddress != UefiImageGetPreferredAddress (ImageContext) && UefiImageGetRelocsStripped (ImageContext)) {
@ -605,7 +608,7 @@ CoreLoadPeImage (
Status = AllocatePagesEx ( Status = AllocatePagesEx (
AllocateAddress, AllocateAddress,
ImageCodeMemoryType, ImageCodeMemoryType,
Image->NumberOfPages, DstBufPages,
&BufferAddress &BufferAddress
); );
} }
@ -614,8 +617,8 @@ CoreLoadPeImage (
Status = AllocateAlignedPagesEx ( Status = AllocateAlignedPagesEx (
AllocateAnyPages, AllocateAnyPages,
ImageCodeMemoryType, ImageCodeMemoryType,
Image->NumberOfPages, DstBufPages,
Alignment, ImageAlignment,
&BufferAddress &BufferAddress
); );
} }
@ -645,14 +648,14 @@ CoreLoadPeImage (
if ((Image->NumberOfPages != 0) && if ((Image->NumberOfPages != 0) &&
(Image->NumberOfPages < (Image->NumberOfPages <
(EFI_SIZE_TO_PAGES (Size)))) DstBufPages))
{ {
Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size); Image->NumberOfPages = DstBufPages;
ASSERT (FALSE); ASSERT (FALSE);
return EFI_BUFFER_TOO_SMALL; return EFI_BUFFER_TOO_SMALL;
} }
Image->NumberOfPages = EFI_SIZE_TO_PAGES (Size); Image->NumberOfPages = DstBufPages;
BufferAddress = *DstBuffer; BufferAddress = *DstBuffer;
} }
@ -691,7 +694,7 @@ CoreLoadPeImage (
Status = UefiImageLoadImageForExecution ( Status = UefiImageLoadImageForExecution (
ImageContext, ImageContext,
(VOID *)(UINTN)BufferAddress, (VOID *)(UINTN)BufferAddress,
Size, DstBufSize,
RelocationData, RelocationData,
RelocDataSize RelocDataSize
); );
@ -715,7 +718,7 @@ CoreLoadPeImage (
// //
Image->Type = UefiImageGetSubsystem (ImageContext); Image->Type = UefiImageGetSubsystem (ImageContext);
Image->Info.ImageBase = (VOID *)(UINTN)BufferAddress; Image->Info.ImageBase = (VOID *)(UINTN)BufferAddress;
Image->Info.ImageSize = UefiImageGetImageSize (ImageContext); Image->Info.ImageSize = ImageSize;
Image->Info.ImageCodeType = ImageCodeMemoryType; Image->Info.ImageCodeType = ImageCodeMemoryType;
Image->Info.ImageDataType = ImageDataMemoryType; Image->Info.ImageDataType = ImageDataMemoryType;
if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) { if ((Attribute & EFI_LOAD_PE_IMAGE_ATTRIBUTE_RUNTIME_REGISTRATION) != 0) {

View File

@ -102,20 +102,14 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
**/ **/
EFI_STATUS EFI_STATUS
GetUefiImageFixLoadingAssignedAddress ( GetUefiImageFixLoadingAssignedAddress (
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, OUT EFI_PHYSICAL_ADDRESS *LoadAddress,
IN PEI_CORE_INSTANCE *Private, IN UINT64 ValueInSectionHeader,
OUT EFI_PHYSICAL_ADDRESS *LoadAddress IN UINT32 ImageDestSize,
IN PEI_CORE_INSTANCE *Private
) )
{ {
EFI_STATUS Status; EFI_STATUS Status;
UINT64 ValueInSectionHeader;
EFI_PHYSICAL_ADDRESS FixLoadingAddress; EFI_PHYSICAL_ADDRESS FixLoadingAddress;
UINT32 SizeOfImage;
Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader);
if (RETURN_ERROR (Status)) {
return Status;
}
if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) { if ((INT64)PcdGet64(PcdLoadModuleAtFixAddressEnable) > 0) {
// //
@ -133,8 +127,7 @@ GetUefiImageFixLoadingAssignedAddress (
// //
// Check if the memory range is available. // Check if the memory range is available.
// //
SizeOfImage = UefiImageGetImageSize (ImageContext); Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, ImageDestSize);
Status = CheckAndMarkFixLoadingMemoryUsageBitMap (Private, FixLoadingAddress, SizeOfImage);
*LoadAddress = FixLoadingAddress; *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)); 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; EFI_STATUS Status;
BOOLEAN Success; BOOLEAN Success;
PEI_CORE_INSTANCE *Private; PEI_CORE_INSTANCE *Private;
UINT32 DynamicImageSize; UINT32 ImageSize;
UINT32 DynamicImageAlignment; UINT32 ImageAlignment;
UINT64 ValueInSectionHeader;
BOOLEAN IsXipImage; BOOLEAN IsXipImage;
EFI_STATUS ReturnStatus; EFI_STATUS ReturnStatus;
BOOLEAN IsS3Boot; BOOLEAN IsS3Boot;
BOOLEAN IsPeiModule; BOOLEAN IsPeiModule;
BOOLEAN IsRegisterForShadow; BOOLEAN IsRegisterForShadow;
EFI_FV_FILE_INFO FileInfo; EFI_FV_FILE_INFO FileInfo;
EFI_PHYSICAL_ADDRESS LoadAddress; UINT32 DestinationPages;
UINT32 DestinationSize;
EFI_PHYSICAL_ADDRESS Destination;
UINT16 Machine; UINT16 Machine;
BOOLEAN LoadDynamically; BOOLEAN LoadDynamically;
@ -250,7 +246,9 @@ LoadAndRelocateUefiImage (
} }
LoadDynamically = FALSE; 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. // Allocate Memory for the image when memory is ready, and image is relocatable.
@ -266,11 +264,13 @@ LoadAndRelocateUefiImage (
Success = FALSE; Success = FALSE;
if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) { if (PcdGet64(PcdLoadModuleAtFixAddressEnable) != 0 && (Private->HobList.HandoffInformationTable->BootMode != BOOT_ON_S3_RESUME)) {
Status = GetUefiImageFixLoadingAssignedAddress(ImageContext, Private, &LoadAddress); Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader);
if (!EFI_ERROR (Status)){ if (!RETURN_ERROR (Status)) {
DynamicImageSize = UefiImageGetImageSize (ImageContext); Status = GetUefiImageFixLoadingAssignedAddress(&Destination, ValueInSectionHeader, DestinationSize, Private);
}
Success = LoadAddress == UefiImageGetPreferredAddress (ImageContext); if (!EFI_ERROR (Status)){
Success = Destination == UefiImageGetPreferredAddress (ImageContext);
if (!Success) { if (!Success) {
DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Loading module at fixed address failed since relocs have been stripped.\n")); 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. // Allocate more buffer to avoid buffer overflow.
// //
DynamicImageSize = UefiImageGetImageSize (ImageContext); ImageAlignment = UefiImageGetSegmentAlignment (ImageContext);
DynamicImageAlignment = UefiImageGetSegmentAlignment (ImageContext);
LoadAddress = (UINTN)AllocateAlignedCodePages ( Destination = (UINTN)AllocateAlignedCodePages (
EFI_SIZE_TO_PAGES (DynamicImageSize), DestinationPages,
DynamicImageAlignment ImageAlignment
); );
Success = LoadAddress != 0; Success = Destination != 0;
} }
if (Success) { if (Success) {
@ -301,8 +300,8 @@ LoadAndRelocateUefiImage (
// //
Status = UefiImageLoadImageForExecution ( Status = UefiImageLoadImageForExecution (
ImageContext, ImageContext,
(VOID *) (UINTN)LoadAddress, (VOID *) (UINTN)Destination,
DynamicImageSize, DestinationSize,
NULL, NULL,
0 0
); );

View File

@ -216,23 +216,16 @@ CheckAndMarkFixLoadingMemoryUsageBitMap (
**/ **/
EFI_STATUS EFI_STATUS
GetUefiImageFixLoadingAssignedAddress ( 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; RETURN_STATUS Status;
UINT64 ValueInSectionHeader;
EFI_PHYSICAL_ADDRESS FixLoadingAddress; EFI_PHYSICAL_ADDRESS FixLoadingAddress;
UINT32 SizeOfImage;
Status = UefiImageGetFixedAddress (ImageContext, &ValueInSectionHeader);
if (RETURN_ERROR (Status)) {
return Status;
}
FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + ValueInSectionHeader); FixLoadingAddress = (EFI_PHYSICAL_ADDRESS)(gLoadModuleAtFixAddressSmramBase + ValueInSectionHeader);
SizeOfImage = UefiImageGetImageSize (ImageContext); Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, ImageDestSize);
Status = CheckAndMarkFixLoadingMemoryUsageBitMap (FixLoadingAddress, SizeOfImage);
*LoadAddress = FixLoadingAddress; *LoadAddress = FixLoadingAddress;
DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED INFO: Loading module at fixed address %x, Status = %r\n", FixLoadingAddress, Status)); 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; UINTN FilePathSize;
VOID *Buffer; VOID *Buffer;
UINTN Size; UINTN Size;
UINTN PageCount; UINT32 DstBufferPages;
EFI_GUID *NameGuid; EFI_GUID *NameGuid;
EFI_STATUS Status; EFI_STATUS Status;
EFI_STATUS SecurityStatus; EFI_STATUS SecurityStatus;
EFI_HANDLE DeviceHandle; EFI_HANDLE DeviceHandle;
UINT32 ImageSize;
UINT32 ImageAlignment;
UINT64 ValueInSectionHeader;
VOID *DstBuffer; VOID *DstBuffer;
UINT32 DstBufferSize; UINT32 DstBufferSize;
UINT32 DstBufferAlignment;
EFI_DEVICE_PATH_PROTOCOL *FilePath; EFI_DEVICE_PATH_PROTOCOL *FilePath;
EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath; EFI_DEVICE_PATH_PROTOCOL *OriginalFilePath;
EFI_DEVICE_PATH_PROTOCOL *HandleFilePath; EFI_DEVICE_PATH_PROTOCOL *HandleFilePath;
@ -426,8 +421,10 @@ SmmLoadImage (
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
DstBufferSize = UefiImageGetImageSize (ImageContext); ImageSize = UefiImageGetImageSize (ImageContext);
DstBufferAlignment = UefiImageGetSegmentAlignment (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 // 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 // to hold the Smm driver code
@ -436,22 +433,24 @@ SmmLoadImage (
// //
// Get the fixed loading address assigned by Build tool // 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)) { 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 // 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 // following statements is to bypass SmmFreePages
// //
PageCount = 0; DstBufferPages = 0;
DstBuffer = (VOID *)(UINTN)LoadAddress; DstBuffer = (VOID *)(UINTN)LoadAddress;
} else { } else {
DEBUG ((DEBUG_INFO|DEBUG_LOAD, "LOADING MODULE FIXED ERROR: Failed to load module at fixed address. \n")); 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 // allocate the memory to load the SMM driver
// //
PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DstBufferSize); DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment);
DstBuffer = AllocateAlignedCodePages (PageCount, DstBufferAlignment);
if (DstBuffer == NULL) { if (DstBuffer == NULL) {
if (Buffer != NULL) { if (Buffer != NULL) {
gBS->FreePool (Buffer); gBS->FreePool (Buffer);
@ -461,9 +460,7 @@ SmmLoadImage (
} }
} }
} else { } else {
PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN)DstBufferSize); DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment);
DstBuffer = AllocateAlignedCodePages (PageCount, DstBufferAlignment);
if (DstBuffer == NULL) { if (DstBuffer == NULL) {
if (Buffer != NULL) { if (Buffer != NULL) {
gBS->FreePool (Buffer); gBS->FreePool (Buffer);
@ -488,7 +485,7 @@ SmmLoadImage (
gBS->FreePool (Buffer); gBS->FreePool (Buffer);
} }
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
@ -497,7 +494,7 @@ SmmLoadImage (
// //
DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (ImageContext); DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (ImageContext);
DriverEntry->ImageBuffer = (UINTN)DstBuffer; DriverEntry->ImageBuffer = (UINTN)DstBuffer;
DriverEntry->NumberOfPage = PageCount; DriverEntry->NumberOfPage = DstBufferPages;
// //
// Allocate a Loaded Image Protocol in EfiBootServicesData // Allocate a Loaded Image Protocol in EfiBootServicesData
@ -508,7 +505,7 @@ SmmLoadImage (
gBS->FreePool (Buffer); gBS->FreePool (Buffer);
} }
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
@ -536,14 +533,14 @@ SmmLoadImage (
gBS->FreePool (Buffer); gBS->FreePool (Buffer);
} }
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath)); CopyMem (DriverEntry->LoadedImage->FilePath, FilePath, GetDevicePathSize (FilePath));
DriverEntry->LoadedImage->ImageBase = DstBuffer; DriverEntry->LoadedImage->ImageBase = DstBuffer;
DriverEntry->LoadedImage->ImageSize = UefiImageGetImageSize (ImageContext); DriverEntry->LoadedImage->ImageSize = ImageSize;
DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode; DriverEntry->LoadedImage->ImageCodeType = EfiRuntimeServicesCode;
DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData; DriverEntry->LoadedImage->ImageDataType = EfiRuntimeServicesData;
@ -557,14 +554,14 @@ SmmLoadImage (
} }
gBS->FreePool (DriverEntry->LoadedImage->FilePath); gBS->FreePool (DriverEntry->LoadedImage->FilePath);
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize(FilePath)); CopyMem (DriverEntry->SmmLoadedImage.FilePath, FilePath, GetDevicePathSize(FilePath));
DriverEntry->SmmLoadedImage.ImageBase = DstBuffer; DriverEntry->SmmLoadedImage.ImageBase = DstBuffer;
DriverEntry->SmmLoadedImage.ImageSize = UefiImageGetImageSize (ImageContext); DriverEntry->SmmLoadedImage.ImageSize = ImageSize;
DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode; DriverEntry->SmmLoadedImage.ImageCodeType = EfiRuntimeServicesCode;
DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData; DriverEntry->SmmLoadedImage.ImageDataType = EfiRuntimeServicesData;

View File

@ -976,8 +976,10 @@ ExecuteSmmCoreFromSmram (
EFI_STATUS Status; EFI_STATUS Status;
VOID *SourceBuffer; VOID *SourceBuffer;
UINTN SourceSize; UINTN SourceSize;
UINT32 ImageSize;
UINT32 ImageAlignment;
UINT32 DestinationPages;
UINT32 DestinationSize; UINT32 DestinationSize;
UINT32 DestinationAlignment;
UINT32 AlignSubtrahend; UINT32 AlignSubtrahend;
UINTN PageCount; UINTN PageCount;
EFI_IMAGE_ENTRY_POINT EntryPoint; EFI_IMAGE_ENTRY_POINT EntryPoint;
@ -1014,8 +1016,10 @@ ExecuteSmmCoreFromSmram (
return EFI_UNSUPPORTED; return EFI_UNSUPPORTED;
} }
DestinationSize = UefiImageGetImageSize (&gSmmCorePrivate->PiSmmCoreImageContext); ImageSize = UefiImageGetImageSize (&gSmmCorePrivate->PiSmmCoreImageContext);
DestinationAlignment = UefiImageGetSegmentAlignment (&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 // if Loading module at Fixed Address feature is enabled, the SMM core driver will be loaded to
// the address assigned by build tool. // the address assigned by build tool.
@ -1042,9 +1046,9 @@ ExecuteSmmCoreFromSmram (
// //
AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND ( AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND (
SmramRange->CpuStart + SmramRange->PhysicalSize, 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_PAGE_MASK) == 0);
ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount)); ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));
@ -1067,9 +1071,9 @@ ExecuteSmmCoreFromSmram (
// //
AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND ( AlignSubtrahend = ALIGN_VALUE_SUBTRAHEND (
SmramRange->CpuStart + SmramRange->PhysicalSize, 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_PAGE_MASK) == 0);
ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount)); ASSERT (SmramRange->PhysicalSize > EFI_PAGES_TO_SIZE (PageCount));

View File

@ -45,8 +45,8 @@ IsDrxEnabled (
**/ **/
VOID VOID
UefiImageLoaderExtraActionCommon ( UefiImageLoaderExtraActionCommon (
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext, IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext,
IN UINTN Signature IN UINTN Signature
) )
{ {
BOOLEAN InterruptState; BOOLEAN InterruptState;
@ -217,7 +217,7 @@ UefiImageLoaderExtraActionCommon (
VOID VOID
EFIAPI EFIAPI
UefiImageLoaderRelocateImageExtraAction ( UefiImageLoaderRelocateImageExtraAction (
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
) )
{ {
UefiImageLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE); UefiImageLoaderExtraActionCommon (ImageContext, IMAGE_LOAD_SIGNATURE);

View File

@ -274,9 +274,10 @@ MmLoadImage (
{ {
UINT32 ImageSize; UINT32 ImageSize;
UINT32 ImageAlignment; UINT32 ImageAlignment;
UINTN PageCount;
EFI_STATUS Status; EFI_STATUS Status;
VOID *DstBuffer; VOID *DstBuffer;
UINT32 DstBufferPages;
UINT32 DstBufferSize;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName)); DEBUG ((DEBUG_INFO, "MmLoadImage - %g\n", &DriverEntry->FileName));
@ -291,12 +292,12 @@ MmLoadImage (
return Status; return Status;
} }
ImageSize = UefiImageGetImageSize (&ImageContext, &ImageSize); ImageSize = UefiImageGetImageSize (&ImageContext);
DstBufferPages = EFI_SIZE_TO_PAGES (ImageSize);
DstBufferSize = EFI_PAGES_TO_SIZE (DstBufferPages);
ImageAlignment = UefiImageGetSegmentAlignment (&ImageContext); ImageAlignment = UefiImageGetSegmentAlignment (&ImageContext);
PageCount = (UINTN)EFI_SIZE_TO_PAGES ((UINTN) ImageSize); DstBuffer = AllocateAlignedCodePages (DstBufferPages, ImageAlignment);
DstBuffer = AllocateAlignedCodePages (PageCount, ImageAlignment);
if (DstBuffer == NULL) { if (DstBuffer == NULL) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }
@ -304,9 +305,9 @@ MmLoadImage (
// //
// Load the image to our new buffer // 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)) { if (EFI_ERROR (Status)) {
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
@ -315,7 +316,7 @@ MmLoadImage (
// //
DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext); DriverEntry->ImageEntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext);
DriverEntry->ImageBuffer = (UINTN)DstBuffer; DriverEntry->ImageBuffer = (UINTN)DstBuffer;
DriverEntry->NumberOfPage = PageCount; DriverEntry->NumberOfPage = DstBufferPages;
if (mEfiSystemTable != NULL) { if (mEfiSystemTable != NULL) {
Status = mEfiSystemTable->BootServices->AllocatePool ( Status = mEfiSystemTable->BootServices->AllocatePool (
@ -324,7 +325,7 @@ MmLoadImage (
(VOID **)&DriverEntry->LoadedImage (VOID **)&DriverEntry->LoadedImage
); );
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
FreeAlignedPages (DstBuffer, PageCount); FreeAlignedPages (DstBuffer, DstBufferPages);
return Status; return Status;
} }
@ -368,7 +369,7 @@ MmLoadImage (
DEBUG (( DEBUG ((
DEBUG_INFO | DEBUG_LOAD, DEBUG_INFO | DEBUG_LOAD,
"Loading MM driver at 0x%11p EntryPoint=0x%11p ", "Loading MM driver at 0x%11p EntryPoint=0x%11p ",
(VOID *)(UINTN)ImageBase, DstBuffer,
FUNCTION_ENTRY_POINT (UefiImageLoaderGetImageEntryPoint (&ImageContext)) FUNCTION_ENTRY_POINT (UefiImageLoaderGetImageEntryPoint (&ImageContext))
)); ));

View File

@ -11,7 +11,7 @@
/** /**
Loads and relocates a PE/COFF image 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] ImageAddress The image memory address after relocation.
@param[out] ImageSize The image size. @param[out] ImageSize The image size.
@param[out] EntryPoint The image entry point. @param[out] EntryPoint The image entry point.
@ -21,17 +21,19 @@
**/ **/
EFI_STATUS EFI_STATUS
LoadUefiImage ( LoadUefiImage (
IN VOID *UefiImage, IN VOID *UefiImage,
IN UINT32 UefiImageSize, IN UINT32 UefiImageSize,
OUT EFI_PHYSICAL_ADDRESS *ImageAddress, OUT EFI_PHYSICAL_ADDRESS *ImageAddress,
OUT UINT64 *ImageSize, OUT UINT64 *DestinationSize,
OUT EFI_PHYSICAL_ADDRESS *EntryPoint OUT EFI_PHYSICAL_ADDRESS *EntryPoint
) )
{ {
RETURN_STATUS Status; RETURN_STATUS Status;
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext; UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
UINT32 BufferSize; UINT32 ImageSize;
UINT32 BufferAlignment; UINT32 ImageAlignment;
UINT32 BufferPages;
UINT32 BufferSize;
VOID *Buffer; VOID *Buffer;
Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize); Status = UefiImageInitializeContext (&ImageContext, UefiImage, UefiImageSize);
@ -40,13 +42,15 @@ LoadUefiImage (
return Status; return Status;
} }
BufferSize = UefiImageGetImageSize (&ImageContext); ImageSize = UefiImageGetImageSize (&ImageContext);
BufferAlignment = UefiImageGetSegmentAlignment (&ImageContext); BufferPages = EFI_SIZE_TO_PAGES (ImageSize);
BufferSize = EFI_PAGES_TO_SIZE (BufferPages);
ImageAlignment = UefiImageGetSegmentAlignment (&ImageContext);
// //
// Allocate Memory for the image // Allocate Memory for the image
// //
Buffer = AllocateAlignedCodePages (EFI_SIZE_TO_PAGES (BufferSize), BufferAlignment); Buffer = AllocateAlignedCodePages (BufferPages, ImageAlignment);
if (Buffer == NULL) { if (Buffer == NULL) {
return EFI_OUT_OF_RESOURCES; return EFI_OUT_OF_RESOURCES;
} }
@ -66,9 +70,9 @@ LoadUefiImage (
return Status; return Status;
} }
*ImageAddress = (UINTN)Buffer; *ImageAddress = (UINTN)Buffer;
*ImageSize = BufferSize; *DestinationSize = BufferSize;
*EntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext); *EntryPoint = UefiImageLoaderGetImageEntryPoint (&ImageContext);
return EFI_SUCCESS; return EFI_SUCCESS;
} }
@ -230,7 +234,7 @@ LoadDxeCore (
VOID *UefiImage; VOID *UefiImage;
UINT32 UefiImageSize; UINT32 UefiImageSize;
EFI_PHYSICAL_ADDRESS ImageAddress; EFI_PHYSICAL_ADDRESS ImageAddress;
UINT64 ImageSize; UINT64 DestinationSize;
PayloadFv = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdPayloadFdMemBase); PayloadFv = (EFI_FIRMWARE_VOLUME_HEADER *)(UINTN)PcdGet32 (PcdPayloadFdMemBase);
@ -268,12 +272,12 @@ LoadDxeCore (
// //
// Get DXE core info // Get DXE core info
// //
Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &ImageSize, DxeCoreEntryPoint); Status = LoadUefiImage (UefiImage, UefiImageSize, &ImageAddress, &DestinationSize, DxeCoreEntryPoint);
if (EFI_ERROR (Status)) { if (EFI_ERROR (Status)) {
return 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; return EFI_SUCCESS;
} }