mirror of
https://github.com/acidanthera/audk.git
synced 2025-09-25 18:48:42 +02:00
BaseTools/ImageTool: Use AllocateAlignedCodePages() for images
This commit is contained in:
parent
240e920841
commit
d5eecc2e2d
@ -18,6 +18,7 @@
|
|||||||
#include <Library/BaseLib.h>
|
#include <Library/BaseLib.h>
|
||||||
#include <Library/BaseMemoryLib.h>
|
#include <Library/BaseMemoryLib.h>
|
||||||
#include <Library/BaseOverflowLib.h>
|
#include <Library/BaseOverflowLib.h>
|
||||||
|
#include <Library/MemoryAllocationLib.h>
|
||||||
#include <UserFile.h>
|
#include <UserFile.h>
|
||||||
#include "../../UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfCommon.h"
|
#include "../../UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfCommon.h"
|
||||||
|
|
||||||
|
@ -10,35 +10,6 @@
|
|||||||
#define PE_COFF_SECT_NAME_RESRC ".rsrc\0\0"
|
#define PE_COFF_SECT_NAME_RESRC ".rsrc\0\0"
|
||||||
#define PE_COFF_SECT_NAME_DEBUG ".debug\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
|
static
|
||||||
bool
|
bool
|
||||||
ScanPeGetHeaderInfo (
|
ScanPeGetHeaderInfo (
|
||||||
@ -432,11 +403,11 @@ ToolContextConstructPe (
|
|||||||
{
|
{
|
||||||
PE_COFF_LOADER_IMAGE_CONTEXT Context;
|
PE_COFF_LOADER_IMAGE_CONTEXT Context;
|
||||||
RETURN_STATUS Status;
|
RETURN_STATUS Status;
|
||||||
|
UINT32 ImageSize;
|
||||||
|
UINT32 ImageAlignment;
|
||||||
UINT32 DestinationSize;
|
UINT32 DestinationSize;
|
||||||
bool Overflow;
|
UINT32 DestinationPages;
|
||||||
void *Destination;
|
void *Destination;
|
||||||
uintptr_t Addend;
|
|
||||||
void *AlignedDest;
|
|
||||||
bool Result;
|
bool Result;
|
||||||
|
|
||||||
assert (Image != NULL);
|
assert (Image != NULL);
|
||||||
@ -453,25 +424,24 @@ ToolContextConstructPe (
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Overflow = OverflowGetDestinationSize (&Context, &DestinationSize);
|
ImageSize = PeCoffGetSizeOfImage (&Context);
|
||||||
if (Overflow) {
|
DestinationPages = EFI_SIZE_TO_PAGES (ImageSize);
|
||||||
fprintf (stderr, "ImageTool: DestinationSize is too huge\n");
|
DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages);
|
||||||
return false;
|
ImageAlignment = PeCoffGetSectionAlignment (&Context);
|
||||||
}
|
|
||||||
|
|
||||||
Destination = calloc (1, DestinationSize);
|
Destination = AllocateAlignedCodePages (
|
||||||
|
DestinationPages,
|
||||||
|
ImageAlignment
|
||||||
|
);
|
||||||
if (Destination == NULL) {
|
if (Destination == NULL) {
|
||||||
fprintf (stderr, "ImageTool: Could not allocate Destination buffer\n");
|
fprintf (stderr, "ImageTool: Could not allocate Destination buffer\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Addend = ALIGN_VALUE_ADDEND ((uintptr_t)Destination, EFI_PAGE_SIZE);
|
Status = PeCoffLoadImage (&Context, Destination, DestinationSize);
|
||||||
AlignedDest = (char *)Destination + Addend;
|
|
||||||
|
|
||||||
Status = PeCoffLoadImage (&Context, AlignedDest, DestinationSize - Addend);
|
|
||||||
if (RETURN_ERROR (Status)) {
|
if (RETURN_ERROR (Status)) {
|
||||||
fprintf (stderr, "ImageTool: Could not Load Image\n");
|
fprintf (stderr, "ImageTool: Could not Load Image\n");
|
||||||
free (Destination);
|
FreeAlignedPages (Destination, DestinationPages);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -480,21 +450,21 @@ ToolContextConstructPe (
|
|||||||
Result = ScanPeGetHeaderInfo (&Image->HeaderInfo, &Context, ModuleType);
|
Result = ScanPeGetHeaderInfo (&Image->HeaderInfo, &Context, ModuleType);
|
||||||
if (!Result) {
|
if (!Result) {
|
||||||
fprintf (stderr, "ImageTool: Could not retrieve header info\n");
|
fprintf (stderr, "ImageTool: Could not retrieve header info\n");
|
||||||
free (Destination);
|
FreeAlignedPages (Destination, DestinationPages);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result = ScanPeGetDebugInfo (&Image->DebugInfo, &Context);
|
Result = ScanPeGetDebugInfo (&Image->DebugInfo, &Context);
|
||||||
if (!Result) {
|
if (!Result) {
|
||||||
fprintf (stderr, "ImageTool: Could not retrieve debug info\n");
|
fprintf (stderr, "ImageTool: Could not retrieve debug info\n");
|
||||||
free (Destination);
|
FreeAlignedPages (Destination, DestinationPages);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Result = ScanPeGetSegmentInfo (&Image->SegmentInfo, &Image->HiiInfo, &Context);
|
Result = ScanPeGetSegmentInfo (&Image->SegmentInfo, &Image->HiiInfo, &Context);
|
||||||
if (!Result) {
|
if (!Result) {
|
||||||
fprintf (stderr, "ImageTool: Could not retrieve segment info\n");
|
fprintf (stderr, "ImageTool: Could not retrieve segment info\n");
|
||||||
free (Destination);
|
FreeAlignedPages (Destination, DestinationPages);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +473,7 @@ ToolContextConstructPe (
|
|||||||
fprintf (stderr, "ImageTool: Could not retrieve reloc info\n");
|
fprintf (stderr, "ImageTool: Could not retrieve reloc info\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
free (Destination);
|
FreeAlignedPages (Destination, DestinationPages);
|
||||||
|
|
||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user