OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
OcPeCoffFixupInit.c File Reference
#include <Base.h>
#include <Uefi/UefiBaseType.h>
#include <IndustryStandard/PeImage2.h>
#include <Guid/WinCertificate.h>
#include <Library/BaseMemoryLib.h>
#include <Library/BaseOverflowLib.h>
#include <Library/DebugLib.h>
#include <Library/PcdLib.h>
#include <Library/PeCoffLib2.h>
#include <Library/OcStringLib.h>
#include "BasePeCoffLib2Internals.h"

Go to the source code of this file.

Functions

STATIC RETURN_STATUS InternalVerifySections (IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context, IN UINT32 FileSize, OUT UINT32 *StartAddress, IN BOOLEAN InMemoryFixup)
 
STATIC RETURN_STATUS InternalValidateRelocInfo (IN CONST PE_COFF_LOADER_IMAGE_CONTEXT *Context, IN UINT32 StartAddress)
 
STATIC RETURN_STATUS InternalInitializePe (IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context, IN UINT32 FileSize, IN BOOLEAN InMemoryFixup)
 
RETURN_STATUS OcPeCoffFixupInitializeContext (OUT PE_COFF_LOADER_IMAGE_CONTEXT *Context, IN CONST VOID *FileBuffer, IN UINT32 FileSize, IN BOOLEAN InMemoryFixup)
 

Detailed Description

Implements APIs to fix certain issues in legacy EFI files in memory before loading.

Very closely based on MdePkg/Library/BasePeCoffLib2/PeCoffInit.c, and intentionally kept more similar to that file than it would otherwise need to be, to easily allow diffing and importing future changes if required.

Copyright (c) 2023, Mike Beaton, Vitaly Cheptsov. All rights reserved.
Copyright (c) 2020 - 2021, Marvin Häuser. All rights reserved.
Copyright (c) 2020, Vitaly Cheptsov. All rights reserved.
Copyright (c) 2020, ISP RAS. All rights reserved.
Portions copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
Portions copyright (c) 2008 - 2010, Apple Inc. All rights reserved.
Portions copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.

SPDX-License-Identifier: BSD-3-Clause

Definition in file OcPeCoffFixupInit.c.

Function Documentation

◆ InternalInitializePe()

STATIC RETURN_STATUS InternalInitializePe ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * Context,
IN UINT32 FileSize,
IN BOOLEAN InMemoryFixup )

Verify the PE32 or PE32+ Image and initialise Context.

Used offsets and ranges must be aligned and in the bounds of the raw file. Image section Headers and basic Relocation information must be Well-formed.

Parameters
[in,out]ContextThe context describing the Image. Must have been initialised by PeCoffInitializeContext().
[in]FileSizeThe size, in Bytes, of Context->FileBuffer.
[in]InMemoryFixupIf TRUE, fixes are made to image in memory. If FALSE, Context is initialised as if fixes were made, but no changes are made to loaded image.
Return values
RETURN_SUCCESSThe PE Image is Well-formed.
otherThe PE Image is malformed.

Definition at line 409 of file OcPeCoffFixupInit.c.

◆ InternalValidateRelocInfo()

STATIC RETURN_STATUS InternalValidateRelocInfo ( IN CONST PE_COFF_LOADER_IMAGE_CONTEXT * Context,
IN UINT32 StartAddress )

Verify the basic Image Relocation information.

The preferred Image load address must be aligned by the section alignment. The Relocation Directory must be contained within the Image section memory. The Relocation Directory must be sufficiently aligned in memory.

Parameters
[in]ContextThe context describing the Image. Must have been initialised by PeCoffInitializeContext().
[in]StartAddressThe RVA of the first Image section.
Return values
RETURN_SUCCESSThe basic Image Relocation information is well-formed.
otherThe basic Image Relocation information is malformed.

Definition at line 327 of file OcPeCoffFixupInit.c.

◆ InternalVerifySections()

STATIC RETURN_STATUS InternalVerifySections ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT * Context,
IN UINT32 FileSize,
OUT UINT32 * StartAddress,
IN BOOLEAN InMemoryFixup )

Verify the Image section Headers and initialise the Image memory space size.

The first Image section must be the beginning of the memory space, or be contiguous to the aligned Image Headers. Sections must be disjoint and, depending on the policy, contiguous in the memory space space. The section data must be in bounds bounds of the file buffer.

Parameters
[in,out]ContextThe context describing the Image. Must have been initialised by PeCoffInitializeContext().
[in]FileSizeThe size, in Bytes, of Context->FileBuffer.
[out]StartAddressOn output, the RVA of the first Image section.
[in]InMemoryFixupIf TRUE, fixes are made to image in memory. If FALSE, Context is initialised as if fixes were made, but no changes are made to loaded image.
Return values
RETURN_SUCCESSThe Image section Headers are well-formed.
otherThe Image section Headers are malformed.

Definition at line 61 of file OcPeCoffFixupInit.c.

◆ OcPeCoffFixupInitializeContext()

RETURN_STATUS OcPeCoffFixupInitializeContext ( OUT PE_COFF_LOADER_IMAGE_CONTEXT * Context,
IN CONST VOID * FileBuffer,
IN UINT32 FileSize,
IN BOOLEAN InMemoryFixup )

Fix W^X and section overlap issues in loaded TE, PE32, or PE32+ Image in memory while initialising Context.

Closely based on PeCoffInitializeContext from PeCoffLib2.

The approach of modifying the image in memory is basically incompatible with secure boot, although: a) Certain firmware may allow optionally registering the hash of any image which does not load, which would still work. b) It is fairly crazy anyway to want to apply secure boot to the old, insecure .efi files which need these fixups.

Parameters
[out]ContextThe context describing the Image.
[in]FileBufferThe file data to parse as PE Image.
[in]FileSizeThe size, in Bytes, of FileBuffer.
[in]InMemoryFixupIf TRUE, fixes are made to image in memory. If FALSE, Context is initialised as if fixes were made, but no changes are made to loaded image.
Return values
RETURN_SUCCESSThe Image context has been initialised successfully.
otherThe file data is malformed.

Definition at line 739 of file OcPeCoffFixupInit.c.