BaseTools/ImageTool: Use AllocateAlignedCodePages() for images

This commit is contained in:
Marvin Häuser 2023-04-01 00:05:07 +02:00
parent 240e920841
commit d5eecc2e2d
2 changed files with 18 additions and 47 deletions

View File

@ -18,6 +18,7 @@
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/MemoryAllocationLib.h>
#include <UserFile.h>
#include "../../UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfCommon.h"

View File

@ -10,35 +10,6 @@
#define PE_COFF_SECT_NAME_RESRC ".rsrc\0\0"
#define PE_COFF_SECT_NAME_DEBUG ".debug\0"
static
bool
OverflowGetDestinationSize (
IN PE_COFF_LOADER_IMAGE_CONTEXT *Context,
OUT UINT32 *Size
)
{
UINT32 AlignedSize;
UINT32 SegmentAlignment;
assert (Context != NULL);
assert (Size != NULL);
AlignedSize = PeCoffGetSizeOfImage (Context);
SegmentAlignment = PeCoffGetSectionAlignment (Context);
if (SegmentAlignment < EFI_PAGE_SIZE) {
SegmentAlignment = EFI_PAGE_SIZE;
}
//
// The Image needs to be at least EFI_PAGE_SIZE aligned inside the calloc() buffer.
//
return BaseOverflowAddU32 (
AlignedSize,
SegmentAlignment - 1,
Size
);
}
static
bool
ScanPeGetHeaderInfo (
@ -432,11 +403,11 @@ ToolContextConstructPe (
{
PE_COFF_LOADER_IMAGE_CONTEXT Context;
RETURN_STATUS Status;
UINT32 ImageSize;
UINT32 ImageAlignment;
UINT32 DestinationSize;
bool Overflow;
UINT32 DestinationPages;
void *Destination;
uintptr_t Addend;
void *AlignedDest;
bool Result;
assert (Image != NULL);
@ -453,25 +424,24 @@ ToolContextConstructPe (
return false;
}
Overflow = OverflowGetDestinationSize (&Context, &DestinationSize);
if (Overflow) {
fprintf (stderr, "ImageTool: DestinationSize is too huge\n");
return false;
}
ImageSize = PeCoffGetSizeOfImage (&Context);
DestinationPages = EFI_SIZE_TO_PAGES (ImageSize);
DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages);
ImageAlignment = PeCoffGetSectionAlignment (&Context);
Destination = calloc (1, DestinationSize);
Destination = AllocateAlignedCodePages (
DestinationPages,
ImageAlignment
);
if (Destination == NULL) {
fprintf (stderr, "ImageTool: Could not allocate Destination buffer\n");
return false;
}
Addend = ALIGN_VALUE_ADDEND ((uintptr_t)Destination, EFI_PAGE_SIZE);
AlignedDest = (char *)Destination + Addend;
Status = PeCoffLoadImage (&Context, AlignedDest, DestinationSize - Addend);
Status = PeCoffLoadImage (&Context, Destination, DestinationSize);
if (RETURN_ERROR (Status)) {
fprintf (stderr, "ImageTool: Could not Load Image\n");
free (Destination);
FreeAlignedPages (Destination, DestinationPages);
return false;
}
@ -480,21 +450,21 @@ ToolContextConstructPe (
Result = ScanPeGetHeaderInfo (&Image->HeaderInfo, &Context, ModuleType);
if (!Result) {
fprintf (stderr, "ImageTool: Could not retrieve header info\n");
free (Destination);
FreeAlignedPages (Destination, DestinationPages);
return false;
}
Result = ScanPeGetDebugInfo (&Image->DebugInfo, &Context);
if (!Result) {
fprintf (stderr, "ImageTool: Could not retrieve debug info\n");
free (Destination);
FreeAlignedPages (Destination, DestinationPages);
return false;
}
Result = ScanPeGetSegmentInfo (&Image->SegmentInfo, &Image->HiiInfo, &Context);
if (!Result) {
fprintf (stderr, "ImageTool: Could not retrieve segment info\n");
free (Destination);
FreeAlignedPages (Destination, DestinationPages);
return false;
}
@ -503,7 +473,7 @@ ToolContextConstructPe (
fprintf (stderr, "ImageTool: Could not retrieve reloc info\n");
}
free (Destination);
FreeAlignedPages (Destination, DestinationPages);
return Result;
}