6#include "../Include/Uefi.h"
8#include <Library/BaseMemoryLib.h>
9#include <Library/BaseOverflowLib.h>
10#include <Library/MemoryAllocationLib.h>
11#include <Library/PeCoffLib2.h>
12#include <Library/DebugLib.h>
13#include <Library/UefiBootServicesTableLib.h>
28 IN OUT VOID *HashContext,
35 D = (CONST UINT8 *)Data;
41 for (UINTN i = 0; i < DataLength; i++) {
60 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context
64 PE_COFF_LOADER_RUNTIME_CONTEXT *RtCtx;
67 Status = PeCoffLoaderGetRuntimeContextSize (Context, &RtCtxSize);
68 if (EFI_ERROR (Status)) {
69 return EFI_UNSUPPORTED;
72 RtCtx = AllocatePool (RtCtxSize);
74 return EFI_UNSUPPORTED;
77 Status = PeCoffRelocateImage (Context, 0x69696969, RtCtx, RtCtxSize);
78 if (EFI_ERROR (Status)) {
83 Status = PeCoffRuntimeRelocateImage (Context->ImageBuffer, Context->SizeOfImage, 0x96969696, RtCtx);
93 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context,
94 OUT VOID *Destination,
95 IN UINT32 DestinationSize
102 Status = PeCoffLoadImage (Context, Destination, DestinationSize);
103 if (EFI_ERROR (Status)) {
107 Status = PeCoffGetPdbPath (Context, (CONST CHAR8 **)&PdbPath, &PdbPathSize);
108 if (!EFI_ERROR (Status)) {
109 ZeroMem (PdbPath, PdbPathSize);
112 if (!Context->RelocsStripped) {
113 if (Context->Subsystem != EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
114 Status = PeCoffRelocateImage (Context, (UINTN)(Context->ImageBuffer), NULL, 0);
120 if (EFI_ERROR (Status)) {
124 PeCoffDiscardSections (Context);
132 IN CONST UINT8 *Data,
144 if (
Size - ConfigSize >=
sizeof (UINT8)) {
145 ConfigSize +=
sizeof (UINT8);
146 LastByte = Data[
Size - ConfigSize];
148 LastByte = MAX_UINT8;
151 PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch) = (LastByte & 1U) != 0;
152 PcdGetBool (PcdImageLoaderHashProhibitOverlap) = (LastByte & 2U) != 0;
153 PcdGetBool (PcdImageLoaderLoadHeader) = (LastByte & 4U) != 0;
154 PcdGetBool (PcdImageLoaderDebugSupport) = (LastByte & 8U) != 0;
155 PcdGetBool (PcdImageLoaderAllowMisalignedOffset) = (LastByte & 16U) != 0;
156 PcdGetBool (PcdImageLoaderRemoveXForWX) = (LastByte & 32U) != 0;
158 if (
Size - ConfigSize >=
sizeof (UINT32)) {
159 ConfigSize +=
sizeof (UINT32);
160 CopyMem (&LastByte, &Data[
Size - ConfigSize],
sizeof (UINT32));
162 LastByte = MAX_UINT32;
165 PcdGet32 (PcdImageLoaderAlignmentPolicy) = LastByte;
167 if (
Size - ConfigSize >=
sizeof (UINT32)) {
168 ConfigSize +=
sizeof (UINT32);
169 CopyMem (&LastByte, &Data[
Size - ConfigSize],
sizeof (UINT32));
171 LastByte = MAX_UINT32;
174 PcdGet32 (PcdImageLoaderRelocTypePolicy) = LastByte;
178 if (
Size - ConfigSize >=
sizeof (UINT64)) {
179 ConfigSize +=
sizeof (UINT64);
189 IN CONST VOID *FileBuffer,
195 PE_COFF_LOADER_IMAGE_CONTEXT Context;
198 UINT32 DestinationSize;
199 UINT32 DestinationPages;
200 UINT32 DestinationAlignment;
203 Status = PeCoffInitializeContext (&Context, FileBuffer, FileSize, UefiImageOriginFv);
204 if (EFI_ERROR (Status)) {
205 return EFI_UNSUPPORTED;
208 Result = PeCoffHashImageAuthenticode (
214 return EFI_UNSUPPORTED;
217 ImageSize = PeCoffGetSizeOfImage (&Context);
218 DestinationPages = EFI_SIZE_TO_PAGES (ImageSize);
219 DestinationSize = EFI_PAGES_TO_SIZE (DestinationPages);
220 DestinationAlignment = PeCoffGetSectionAlignment (&Context);
222 if (DestinationSize >= BASE_16MB) {
223 return EFI_UNSUPPORTED;
226 Destination = AllocateAlignedCodePages (
230 if (Destination == NULL) {
231 return EFI_UNSUPPORTED;
236 FreeAlignedPages (Destination, DestinationPages);
253 PcdGet8 (PcdDebugRaisePropertyMask) = 0;
259 NewData = AllocatePool (
Size);
260 if (NewData != NULL) {
267 DEBUG_POOL | DEBUG_PAGE,
268 "UMEM: Allocated %u pools %u pages\n",
287 DEBUG ((DEBUG_ERROR,
"Please provide a valid PE image path\n"));
291 PcdGet32 (PcdFixedDebugPrintErrorLevel) |= DEBUG_INFO;
292 PcdGet32 (PcdDebugPrintErrorLevel) |= DEBUG_INFO;
297 PcdGet32 (PcdImageProtectionPolicy) = 0x00;
302 if ((Image =
UserReadFile (argv[1], &ImageSize)) == NULL) {
303 DEBUG ((DEBUG_ERROR,
"Read fail\n"));
309 if (EFI_ERROR (Status)) {
DMG_SIZE_DEVICE_PATH Size
STATIC UINTN mHashDependency
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
STATIC VOID LoadConfig(IN CONST UINT8 *Data, IN UINTN Size)
STATIC UINT64 mHashesMask
STATIC BOOLEAN HashUpdate(IN OUT VOID *HashContext, IN CONST VOID *Data, IN UINTN DataLength)
STATIC EFI_STATUS PeCoffTestLoad(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context, OUT VOID *Destination, IN UINT32 DestinationSize)
STATIC EFI_STATUS PeCoffTestRtReloc(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context)
STATIC EFI_STATUS PeCoffTestLoadFull(IN CONST VOID *FileBuffer, IN UINT32 FileSize)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
UINT8 * UserReadFile(IN CONST CHAR8 *FileName, OUT UINT32 *Size)
VOID ConfigureMemoryAllocations(IN CONST UINT8 *Data, IN UINTN Size, IN OUT UINT32 *ConfigSize)