TianoCore EDK2 master
Loading...
Searching...
No Matches
ImagePropertiesRecordLib.c File Reference

Go to the source code of this file.

Macros

#define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size)    ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))
 
#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size)    ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
 

Functions

STATIC UINT64 EfiPagesToSize (IN UINT64 Pages)
 
STATIC UINT64 EfiSizeToPages (IN UINT64 Size)
 
STATIC VOID FreeImageRecordCodeSections (IMAGE_PROPERTIES_RECORD *ImageRecord)
 
STATIC VOID SortMemoryMap (IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN MemoryMapSize, IN UINTN DescriptorSize)
 
STATIC IMAGE_PROPERTIES_RECORDGetImageRecordByAddress (IN EFI_PHYSICAL_ADDRESS Buffer, IN UINT64 Length, IN LIST_ENTRY *ImageRecordList)
 
STATIC UINTN SetNewRecord (IN IMAGE_PROPERTIES_RECORD *ImageRecord, IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN UINTN DescriptorSize)
 
STATIC UINTN GetMaxSplitRecordCount (IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN LIST_ENTRY *ImageRecordList)
 
STATIC UINTN SplitRecord (IN EFI_MEMORY_DESCRIPTOR *OldRecord, IN OUT EFI_MEMORY_DESCRIPTOR *NewRecord, IN UINTN MaxSplitRecordCount, IN UINTN DescriptorSize, IN LIST_ENTRY *ImageRecordList)
 
EFI_STATUS EFIAPI SplitTable (IN OUT UINTN *MemoryMapSize, IN OUT EFI_MEMORY_DESCRIPTOR *MemoryMap, IN UINTN DescriptorSize, IN LIST_ENTRY *ImageRecordList, IN UINTN NumberOfAdditionalDescriptors)
 
EFI_STATUS EFIAPI SwapImageRecordCodeSection (IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *FirstImageRecordCodeSection, IN IMAGE_PROPERTIES_RECORD_CODE_SECTION *SecondImageRecordCodeSection)
 
EFI_STATUS EFIAPI SortImageRecordCodeSection (IN IMAGE_PROPERTIES_RECORD *ImageRecord)
 
BOOLEAN EFIAPI IsImageRecordCodeSectionValid (IN IMAGE_PROPERTIES_RECORD *ImageRecord)
 
EFI_STATUS EFIAPI SwapImageRecord (IN IMAGE_PROPERTIES_RECORD *FirstImageRecord, IN IMAGE_PROPERTIES_RECORD *SecondImageRecord)
 
EFI_STATUS EFIAPI SortImageRecord (IN LIST_ENTRY *ImageRecordList)
 
STATIC VOID GetFilename (IN CHAR8 *PdbPointer, OUT CHAR8 *EfiFileName, IN UINTN EfiFileNameSize)
 
VOID EFIAPI DumpImageRecords (IN LIST_ENTRY *ImageRecordList)
 
IMAGE_PROPERTIES_RECORD *EFIAPI FindImageRecord (IN EFI_PHYSICAL_ADDRESS ImageBase, IN UINT64 ImageSize, IN LIST_ENTRY *ImageRecordList)
 
EFI_STATUS EFIAPI CreateImagePropertiesRecord (IN CONST VOID *ImageBase, IN CONST UINT64 ImageSize, IN CONST UINT32 *RequiredAlignment OPTIONAL, OUT IMAGE_PROPERTIES_RECORD *ImageRecord)
 
VOID EFIAPI DeleteImagePropertiesRecord (IN IMAGE_PROPERTIES_RECORD *ImageRecord)
 

Detailed Description

Provides definitions and functionality for manipulating IMAGE_PROPERTIES_RECORD.

Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
Copyright (c) Microsoft Corporation. SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file ImagePropertiesRecordLib.c.

Macro Definition Documentation

◆ NEXT_MEMORY_DESCRIPTOR

#define NEXT_MEMORY_DESCRIPTOR (   MemoryDescriptor,
  Size 
)     ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))

Definition at line 23 of file ImagePropertiesRecordLib.c.

◆ PREVIOUS_MEMORY_DESCRIPTOR

#define PREVIOUS_MEMORY_DESCRIPTOR (   MemoryDescriptor,
  Size 
)     ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size)))

Definition at line 20 of file ImagePropertiesRecordLib.c.

Function Documentation

◆ CreateImagePropertiesRecord()

EFI_STATUS EFIAPI CreateImagePropertiesRecord ( IN CONST VOID *  ImageBase,
IN CONST UINT64  ImageSize,
IN CONST UINT32 *RequiredAlignment  OPTIONAL,
OUT IMAGE_PROPERTIES_RECORD ImageRecord 
)

Creates an IMAGE_PROPERTIES_RECORD from a loaded PE image. The PE/COFF header will be found and parsed to determine the number of code segments and their base addresses and sizes.

Parameters
[in]ImageBaseBase of the PE image
[in]ImageSizeSize of the PE image
[in]RequiredAlignmentIf non-NULL, the alignment specified in the PE/COFF header will be compared against this value.
[out]ImageRecordOn out, a populated image properties record
Return values
EFI_INVALID_PARAMETERThis function ImageBase or ImageRecord was NULL, or the image located at ImageBase was not a valid PE/COFF image
EFI_OUT_OF_RESOURCESFailure to Allocate()
EFI_ABORTEDThe input Alignment was non-NULL and did not match the alignment specified in the PE/COFF header
EFI_SUCCESSThe image properties record was successfully created

Definition at line 1013 of file ImagePropertiesRecordLib.c.

◆ DeleteImagePropertiesRecord()

VOID EFIAPI DeleteImagePropertiesRecord ( IN IMAGE_PROPERTIES_RECORD ImageRecord)

Deleted an image properties record. The function will also call RemoveEntryList() on each code segment and the input ImageRecord before freeing each pool.

Parameters
[in]ImageRecordThe IMAGE_PROPERTIES_RECORD to delete

Definition at line 1179 of file ImagePropertiesRecordLib.c.

◆ DumpImageRecords()

VOID EFIAPI DumpImageRecords ( IN LIST_ENTRY ImageRecordList)

Debug dumps the input list of IMAGE_PROPERTIES_RECORD structs.

Parameters
[in]ImageRecordListHead of the IMAGE_PROPERTIES_RECORD list

Definition at line 879 of file ImagePropertiesRecordLib.c.

◆ EfiPagesToSize()

STATIC UINT64 EfiPagesToSize ( IN UINT64  Pages)

Converts a number of pages to a size in bytes.

NOTE: Do not use EFI_PAGES_TO_SIZE because it handles UINTN only.

Parameters
[in]PagesThe number of EFI_PAGES.
Return values
Thenumber of bytes associated with the input number of pages.

Definition at line 37 of file ImagePropertiesRecordLib.c.

◆ EfiSizeToPages()

STATIC UINT64 EfiSizeToPages ( IN UINT64  Size)

Converts a size, in bytes, to a number of EFI_PAGESs.

NOTE: Do not use EFI_SIZE_TO_PAGES because it handles UINTN only.

Parameters
[in]SizeA size in bytes.
Return values
Thenumber of pages associated with the input number of bytes.

Definition at line 56 of file ImagePropertiesRecordLib.c.

◆ FindImageRecord()

IMAGE_PROPERTIES_RECORD *EFIAPI FindImageRecord ( IN EFI_PHYSICAL_ADDRESS  ImageBase,
IN UINT64  ImageSize,
IN LIST_ENTRY ImageRecordList 
)

Find image record according to image base and size.

Parameters
[in]ImageBaseBase of PE image
[in]ImageSizeSize of PE image
[in]ImageRecordListImage record list to be searched
Return values
NULLNo IMAGE_PROPERTIES_RECORD matches ImageBase and ImageSize in the input ImageRecordList
OtherThe found IMAGE_PROPERTIES_RECORD

Definition at line 960 of file ImagePropertiesRecordLib.c.

◆ FreeImageRecordCodeSections()

STATIC VOID FreeImageRecordCodeSections ( IMAGE_PROPERTIES_RECORD ImageRecord)

Frees the memory for each ImageRecordCodeSection within an ImageRecord and removes the entries from the list. It does not free the ImageRecord itself.

Parameters
[in]ImageRecordThe ImageRecord in which to free code sections

Definition at line 72 of file ImagePropertiesRecordLib.c.

◆ GetFilename()

STATIC VOID GetFilename ( IN CHAR8 *  PdbPointer,
OUT CHAR8 *  EfiFileName,
IN UINTN  EfiFileNameSize 
)

Extract the .efi filename out of the input PDB.

Parameters
[in]PdbPointerPointer to the PDB file path.
[out]EfiFileNamePointer to the .efi filename.
[in]EfiFileNameSizeSize of the .efi filename buffer.

Definition at line 830 of file ImagePropertiesRecordLib.c.

◆ GetImageRecordByAddress()

STATIC IMAGE_PROPERTIES_RECORD * GetImageRecordByAddress ( IN EFI_PHYSICAL_ADDRESS  Buffer,
IN UINT64  Length,
IN LIST_ENTRY ImageRecordList 
)

Return the first image record, whose [ImageBase, ImageSize] covered by [Buffer, Length].

Parameters
[in]BufferStarting Address
[in]LengthLength to check
[in]ImageRecordListA list of IMAGE_PROPERTIES_RECORD entries to check against the memory range Buffer -> Buffer + Length
Return values
Thefirst image record covered by [Buffer, Length]

Definition at line 150 of file ImagePropertiesRecordLib.c.

◆ GetMaxSplitRecordCount()

STATIC UINTN GetMaxSplitRecordCount ( IN EFI_MEMORY_DESCRIPTOR OldRecord,
IN LIST_ENTRY ImageRecordList 
)

Return the maximum number of new entries required to describe the code and data sections of all images covered by the input OldRecord.

Parameters
[in]OldRecordA pointer to one old memory map entry.
[in]ImageRecordListA list of IMAGE_PROPERTIES_RECORD entries used when searching for an image record contained by the memory range described by OldRecord
Return values
Themaximum number of new descriptors required to describe the code and data sections of all images covered by OldRecord.

Definition at line 296 of file ImagePropertiesRecordLib.c.

◆ IsImageRecordCodeSectionValid()

BOOLEAN EFIAPI IsImageRecordCodeSectionValid ( IN IMAGE_PROPERTIES_RECORD ImageRecord)

Check if the code sections in the input ImageRecord are valid. The code sections are valid if they don't overlap, are contained within the the ImageRecord's ImageBase and ImageSize, and are contained within the MAX_ADDRESS.

Parameters
[in]ImageRecordIMAGE_PROPERTIES_RECORD to be checked
Return values
TRUEThe code sections in the input ImageRecord are valid
FALSEThe code sections in the input ImageRecord are invalid

Definition at line 664 of file ImagePropertiesRecordLib.c.

◆ SetNewRecord()

STATIC UINTN SetNewRecord ( IN IMAGE_PROPERTIES_RECORD ImageRecord,
IN OUT EFI_MEMORY_DESCRIPTOR NewRecord,
IN EFI_MEMORY_DESCRIPTOR OldRecord,
IN UINTN  DescriptorSize 
)

Break up the input OldRecord into multiple new records based on the code and data sections in the input ImageRecord.

Parameters
[in]ImageRecordAn IMAGE_PROPERTIES_RECORD whose ImageBase and ImageSize is covered by by OldRecord.
[in,out]NewRecordA pointer to several new memory map entries. The caller gurantee the buffer size be 1 + (SplitRecordCount * DescriptorSize) calculated below.
[in]OldRecordA pointer to one old memory map entry.
[in]DescriptorSizeThe size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
Return values
Thenumber of new descriptors created.

Definition at line 197 of file ImagePropertiesRecordLib.c.

◆ SortImageRecord()

EFI_STATUS EFIAPI SortImageRecord ( IN LIST_ENTRY ImageRecordList)

Sort the input ImageRecordList based upon the ImageBase from low to high.

Parameters
[in]ImageRecordListImage record list to be sorted
Return values
EFI_SUCCESSThe image record list was sorted successfully
EFI_ABORTEDAn error occurred while sorting the image record list
EFI_INVALID_PARAMETERImageRecordList is NULL

Definition at line 770 of file ImagePropertiesRecordLib.c.

◆ SortImageRecordCodeSection()

EFI_STATUS EFIAPI SortImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD ImageRecord)

Sort the code sections in the input ImageRecord based upon CodeSegmentBase from low to high.

Parameters
[in]ImageRecordIMAGE_PROPERTIES_RECORD to be sorted
Return values
EFI_SUCCESSThe code sections in the input ImageRecord were sorted successfully
EFI_ABORTEDAn error occurred while sorting the code sections in the input ImageRecord
EFI_INVALID_PARAMETERImageRecord is NULL

Definition at line 598 of file ImagePropertiesRecordLib.c.

◆ SortMemoryMap()

STATIC VOID SortMemoryMap ( IN OUT EFI_MEMORY_DESCRIPTOR MemoryMap,
IN UINTN  MemoryMapSize,
IN UINTN  DescriptorSize 
)

Sort memory map entries based upon PhysicalStart from low to high.

Parameters
[in,out]MemoryMapA pointer to the buffer in which firmware places the current memory map.
[in]MemoryMapSizeSize, in bytes, of the MemoryMap buffer.
[in]DescriptorSizeSize, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.

Definition at line 106 of file ImagePropertiesRecordLib.c.

◆ SplitRecord()

STATIC UINTN SplitRecord ( IN EFI_MEMORY_DESCRIPTOR OldRecord,
IN OUT EFI_MEMORY_DESCRIPTOR NewRecord,
IN UINTN  MaxSplitRecordCount,
IN UINTN  DescriptorSize,
IN LIST_ENTRY ImageRecordList 
)

Split the memory map into new entries based upon the PE code and data sections in ImageRecordList covered by the input OldRecord.

Parameters
[in]OldRecordA pointer to one old memory map entry.
[in,out]NewRecordA pointer to several new memory map entries. The caller gurantee the buffer size be (SplitRecordCount * DescriptorSize).
[in]MaxSplitRecordCountThe maximum number of entries post-split.
[in]DescriptorSizeThe size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
[in]ImageRecordListA list of IMAGE_PROPERTIES_RECORD entries used when searching for an image record contained by the memory range described in the existing EFI memory map descriptor OldRecord
Return values
Thenumber of split entries.

Definition at line 345 of file ImagePropertiesRecordLib.c.

◆ SplitTable()

EFI_STATUS EFIAPI SplitTable ( IN OUT UINTN MemoryMapSize,
IN OUT EFI_MEMORY_DESCRIPTOR MemoryMap,
IN UINTN  DescriptorSize,
IN LIST_ENTRY ImageRecordList,
IN UINTN  NumberOfAdditionalDescriptors 
)

Split the original memory map and add more entries to describe PE code and data sections for each image in the input ImageRecordList.

NOTE: This function assumes PE code/data section are page aligned. NOTE: This function assumes there are enough entries for the new memory map.

| | | | | | | | | 4K PAGE | DATA | CODE | DATA | CODE | DATA | 4K PAGE | | | | | | | | | Assume the above memory region is the result of one split memory map descriptor. It's unlikely that a linker will orient an image this way, but the caller must assume the worst case scenario. This image layout example contains code sections oriented in a way that maximizes the number of descriptors which would be required to describe each section. To ensure we have enough space for every descriptor of the broken up memory map, the caller must assume that every image will have the maximum number of code sections oriented in a way which maximizes the number of data sections with unrelated memory regions flanking each image within a single descriptor.

Given an image record list, the caller should use the following formula when allocating extra descriptors: NumberOfAdditionalDescriptors = (MemoryMapSize / DescriptorSize) + ((2 * <Most Code Segments in a Single Image> + 3) * <Number of Images>)

Parameters
[in,out]MemoryMapSizeIN: The size, in bytes, of the old memory map before the split. OUT: The size, in bytes, of the used descriptors of the split memory map
[in,out]MemoryMapIN: A pointer to the buffer containing the current memory map. This buffer must have enough space to accomodate the "worst case" scenario where every image in ImageRecordList needs a new descriptor to describe its code and data sections. OUT: A pointer to the updated memory map with separated image section descriptors.
[in]DescriptorSizeThe size, in bytes, of an individual EFI_MEMORY_DESCRIPTOR.
[in]ImageRecordListA list of IMAGE_PROPERTIES_RECORD entries used when searching for an image record contained by the memory range described in EFI memory map descriptors.
[in]NumberOfAdditionalDescriptorsThe number of unused descriptors at the end of the input MemoryMap. The formula in the description should be used to calculate this value.
Return values
EFI_SUCCESSThe memory map was successfully split.
EFI_INVALID_PARAMETERMemoryMapSize, MemoryMap, or ImageRecordList was NULL.

Definition at line 481 of file ImagePropertiesRecordLib.c.

◆ SwapImageRecord()

EFI_STATUS EFIAPI SwapImageRecord ( IN IMAGE_PROPERTIES_RECORD FirstImageRecord,
IN IMAGE_PROPERTIES_RECORD SecondImageRecord 
)

Swap two image records.

Parameters
[in]FirstImageRecordThe first image record.
[in]SecondImageRecordThe second image record.
Return values
EFI_SUCCESSThe image records were swapped successfully
EFI_INVALID_PARAMETERFirstImageRecord or SecondImageRecord is NULL

Definition at line 732 of file ImagePropertiesRecordLib.c.

◆ SwapImageRecordCodeSection()

EFI_STATUS EFIAPI SwapImageRecordCodeSection ( IN IMAGE_PROPERTIES_RECORD_CODE_SECTION FirstImageRecordCodeSection,
IN IMAGE_PROPERTIES_RECORD_CODE_SECTION SecondImageRecordCodeSection 
)

Swap two code sections in a single IMAGE_PROPERTIES_RECORD.

Parameters
[in]FirstImageRecordCodeSectionThe first code section
[in]SecondImageRecordCodeSectionThe second code section
Return values
EFI_SUCCESSThe code sections were swapped successfully
EFI_INVALID_PARAMETERFirstImageRecordCodeSection or SecondImageRecordCodeSection is NULL

Definition at line 564 of file ImagePropertiesRecordLib.c.