Revert "BaseTools/FMMT: Add a tool FMMT"

This reverts commit 080981d72d.
There are the concerns on code design and code quality, and
request to rewrite FCE, BfmLib and FMMT for the review.

Cc: Leif Lindholm <leif.lindholm@linaro.org>
Cc: Feng Bob C <bob.c.feng@intel.com>
Signed-off-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Feng Bob C <bob.c.feng@intel.com>
This commit is contained in:
Liming Gao 2019-07-10 23:25:18 +08:00
parent 60ec54afc3
commit b9479a7f7b
11 changed files with 0 additions and 9036 deletions

View File

@ -1,29 +0,0 @@
#!/usr/bin/env bash
full_cmd=${BASH_SOURCE:-$0} # see http://mywiki.wooledge.org/BashFAQ/028 for a discussion of why $0 is not a good choice here
dir=$(dirname "$full_cmd")
cmd=${full_cmd##*/}
if [ -n "$WORKSPACE" ] && [ -e "$WORKSPACE/Conf/BaseToolsCBinaries" ]
then
exec "$WORKSPACE/Conf/BaseToolsCBinaries/$cmd"
elif [ -n "$WORKSPACE" ] && [ -e "$EDK_TOOLS_PATH/Source/C" ]
then
if [ ! -e "$EDK_TOOLS_PATH/Source/C/bin/$cmd" ]
then
echo "BaseTools C Tool binary was not found ($cmd)"
echo "You may need to run:"
echo " make -C $EDK_TOOLS_PATH/Source/C"
else
exec "$EDK_TOOLS_PATH/Source/C/bin/$cmd" "$@"
fi
elif [ -e "$dir/../../Source/C/bin/$cmd" ]
then
exec "$dir/../../Source/C/bin/$cmd" "$@"
else
echo "Unable to find the real '$cmd' to run"
echo "This message was printed by"
echo " $0"
exit 127
fi

File diff suppressed because it is too large Load Diff

View File

@ -1,479 +0,0 @@
/** @file
Structures and functions declaration.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _BIN_FILE_MANAGER_
#define _BIN_FILE_MANAGER_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __GNUC__
#include <unistd.h>
#else
#include <io.h>
#include <direct.h>
#endif
#include <FvLib.h>
#include <Common/UefiBaseTypes.h>
#include <Common/PiFirmwareVolume.h>
#include <Common/PiFirmwareFile.h>
#include <Protocol/GuidedSectionExtraction.h>
#include "CommonLib.h"
#include "EfiUtilityMsgs.h"
#include "ParseInf.h"
#include "ParseGuidedSectionTools.h"
#include "StringFuncs.h"
#include "Compress.h"
#include "Decompress.h"
#ifndef _MAX_PATH
#define _MAX_PATH 500
#endif
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#endif
#ifdef __GNUC__
#define OS_SEP '/'
#define OS_SEP_STR "/"
#define COPY_STR "cp \"%s\" \"%s\" > /dev/null"
#define RMDIR_STR "rm -r \"%s\" > /dev/null"
#define DEL_STR "rm \"%s\" > /dev/null"
#else
#define OS_SEP '\\'
#define OS_SEP_STR "\\"
#define COPY_STR "copy \"%s\" \"%s\" > NUL"
#define RMDIR_STR "rmdir /S /Q \"%s\" > NUL"
#define DEL_STR "del \"%s\" > NUL"
#endif
#define UTILITY_NAME "Firmware Module Management Tool(FMMT)"
#define UTILITY_SHORT_NAME "FMMT"
#define UTILITY_MAJOR_VERSION 0
#define UTILITY_MINOR_VERSION 23
#define MAX_BASENAME_LEN 60 // not good to HardCode, but let's be reasonable
#define EFI_SECTION_ERROR EFIERR (100)
//
// The maximum number of Pad file guid entries.
//
#define MAX_NUMBER_OF_PAD_FILE_GUIDS 1024
//
// The maximum number of block map entries supported by the library
//
#define MAX_NUMBER_OF_FV_BLOCKS 100
//
// The maximum number of sections in an FFS file.
//
#define MAX_NUMBER_OF_SECTION_IN_FFS 100
//
// The maximum number of files in the FV supported by the library
//
#define MAX_NUMBER_OF_FILES_IN_FV 1000
#define MAX_NUMBER_OF_FILES_IN_CAP 1000
///
/// If present, this must be the first and only opcode,
/// EFI_DEP_BEFORE is only used by DXE driver.
///
#define EFI_DEP_BEFORE 0x00
///
/// If present, this must be the first and only opcode,
/// EFI_DEP_AFTER is only used by DXE driver.
///
#define EFI_DEP_AFTER 0x01
#define EFI_DEP_PUSH 0x02
#define EFI_DEP_AND 0x03
#define EFI_DEP_OR 0x04
#define EFI_DEP_NOT 0x05
#define EFI_DEP_TRUE 0x06
#define EFI_DEP_FALSE 0x07
#define EFI_DEP_END 0x08
///
/// If present, this must be the first opcode,
/// EFI_DEP_SOR is only used by DXE driver.
///
#define EFI_DEP_SOR 0x09
//
// INF file strings
//
#define OPTIONS_SECTION_STRING "[options]"
#define ATTRIBUTES_SECTION_STRING "[attributes]"
#define FILES_SECTION_STRING "[files]"
#define FV_BASE_ADDRESS_STRING "[FV_BASE_ADDRESS]"
//
// Options section
//
#define EFI_FV_BASE_ADDRESS_STRING "EFI_BASE_ADDRESS"
#define EFI_FV_FILE_NAME_STRING "EFI_FILE_NAME"
#define EFI_NUM_BLOCKS_STRING "EFI_NUM_BLOCKS"
#define EFI_BLOCK_SIZE_STRING "EFI_BLOCK_SIZE"
#define EFI_GUID_STRING "EFI_GUID"
#define EFI_FV_FILESYSTEMGUID_STRING "EFI_FV_GUID"
#define EFI_FV_NAMEGUID_STRING "EFI_FVNAME_GUID"
#define EFI_CAPSULE_GUID_STRING "EFI_CAPSULE_GUID"
#define EFI_CAPSULE_HEADER_SIZE_STRING "EFI_CAPSULE_HEADER_SIZE"
#define EFI_CAPSULE_FLAGS_STRING "EFI_CAPSULE_FLAGS"
#define EFI_CAPSULE_VERSION_STRING "EFI_CAPSULE_VERSION"
#define EFI_FV_TOTAL_SIZE_STRING "EFI_FV_TOTAL_SIZE"
#define EFI_FV_TAKEN_SIZE_STRING "EFI_FV_TAKEN_SIZE"
#define EFI_FV_SPACE_SIZE_STRING "EFI_FV_SPACE_SIZE"
typedef UINT32 FMMT_ENCAP_TYPE;
#define MAX_LEVEL_IN_FV_FILE 32
//
// Types of FMMT_ENCAP_TREENODE_TYPE
//
#define FMMT_ENCAP_TREE_FV 0x1
#define FMMT_ENCAP_TREE_FFS 0x2
#define FMMT_ENCAP_TREE_GUIDED_SECTION 0x3
#define FMMT_ENCAP_TREE_COMPRESS_SECTION 0x4
#define FMMT_ENCAP_TREE_FV_SECTION 0x5
extern EFI_HANDLE mParsedGuidedSectionTools;
#define TEMP_DIR_NAME "FmmtTemp"
//
// Structure to keep a list of GUID-To-BaseNames
//
typedef struct _GUID_TO_BASENAME {
struct _GUID_TO_BASENAME *Next;
INT8 Guid[PRINTED_GUID_BUFFER_SIZE];
INT8 BaseName[MAX_BASENAME_LEN];
} GUID_TO_BASENAME;
typedef struct _GUID_SEC_TOOL_ENTRY {
EFI_GUID Guid;
CHAR8* Name;
CHAR8* Path;
struct _GUID_SEC_TOOL_ENTRY *Next;
} GUID_SEC_TOOL_ENTRY;
//
// Private data types
//
//
// Component information
//
typedef struct {
UINTN Size;
CHAR8 ComponentName[_MAX_PATH];
} COMPONENT_INFO;
typedef struct {
CHAR8 FfsName[_MAX_PATH];
//
// UI Name for this FFS file, if has.
//
CHAR16 UiName[_MAX_PATH];
UINT32 UiNameSize;
//
// Total section number in this FFS.
//
UINT32 TotalSectionNum;
//
// Describe the position of the FFS file.
//
UINT8 Level;
//
// If this FFS has no encapsulate section, this flag will set to True.
//
BOOLEAN IsLeaf;
//
// Section type for each section in FFS.
//
EFI_SECTION_TYPE SectionType[MAX_NUMBER_OF_SECTION_IN_FFS];
//
// Offset relative to current FV
//
UINT32 Offset;
UINT8 FvLevel;
EFI_GUID GuidName;
UINT8 *Depex;
UINT32 DepexLen;
BOOLEAN IsHandle;
BOOLEAN IsFvStart;
BOOLEAN IsFvEnd;
}FFS_ATTRIBUTES;
typedef struct __ENCAP_INFO_DATA{
//
// Now Level
//
UINT8 Level;
//
// Encapsulate type.
//
FMMT_ENCAP_TYPE Type;
//
// Data, if it's FV, should be FV header.
//
VOID *Data;
//
//FvId, match FvId with FvGuidName.
//
UINT8 FvId;
//
// if FV ExtHeaderOffset not to zero, should also have FvExtHeader information
//
EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
CHAR16 UiName[_MAX_PATH];
UINT32 UiNameSize;
UINT8 *Depex;
UINT32 DepexLen;
//
// Next node.
//
struct __ENCAP_INFO_DATA *NextNode;
//
// Right node.
//
struct __ENCAP_INFO_DATA *RightNode;
} ENCAP_INFO_DATA;
typedef struct _FFS_INFOMATION{
CHAR8 *FFSName;
UINT32 InFvId;
UINT8 ParentLevel;
BOOLEAN IsFFS;
CHAR16 UiName[_MAX_PATH];
UINT32 UiNameSize;
UINT8 *Depex;
UINT32 DepexLen;
BOOLEAN FfsFoundFlag;
struct _FFS_INFOMATION *Next;
} FFS_INFORMATION;
//
// FV and capsule information holder
//
typedef struct _FV_INFOMATION{
EFI_FIRMWARE_VOLUME_HEADER *FvHeader;
EFI_FIRMWARE_VOLUME_EXT_HEADER *FvExtHeader;
UINT32 ImageAddress;
UINT32 FfsNumbers;
CHAR8 FvName[_MAX_PATH];
EFI_FV_BLOCK_MAP_ENTRY FvBlocks[MAX_NUMBER_OF_FV_BLOCKS];
FFS_ATTRIBUTES FfsAttuibutes[MAX_NUMBER_OF_FILES_IN_FV];
EFI_FFS_FILE_HEADER2 FfsHeader[MAX_NUMBER_OF_FILES_IN_FV];
struct _FV_INFOMATION *FvNext;
ENCAP_INFO_DATA *EncapData;
UINT8 FvLevel;
CHAR8 *FvUiName;
UINT8 MulFvLevel;
CHAR8 AlignmentStr[16];
FFS_INFORMATION *ChildFvFFS;
} FV_INFORMATION;
typedef struct _FIRMWARE_DEVICE {
///
/// Size of FD file
///
UINT32 Size;
FV_INFORMATION *Fv;
} FIRMWARE_DEVICE;
typedef struct _FILENode {
CHAR8 *FileName;
UINT8 SubLevel;
struct _FILENode *Next;
} FILENode;
typedef struct {
CHAR8 *FvId;
FILENode *NewFile;
FILENode *OldFile;
FIRMWARE_DEVICE *FdData;
UINT8 FvLevel;
FV_INFORMATION *FvInFd;
} Data;
EFI_STATUS
LibFindFvInFd (
IN FILE *InputFile,
IN OUT FIRMWARE_DEVICE **FdData
);
/**
TODO: Add function description
@param[in] Fv - Firmware Volume to get information from
@return EFI_STATUS
**/
EFI_STATUS
LibGetFvInfo (
IN VOID *Fv,
IN OUT FV_INFORMATION *CurrentFv,
IN CHAR8 *FvName,
IN UINT8 Level,
IN ENCAP_INFO_DATA **CurrentFvEncapData,
IN UINT32 *FfsCount,
IN OUT UINT8 *FvCount,
IN BOOLEAN ViewFlag,
IN BOOLEAN IsChildFv
);
/*
Get size info from FV file.
@param[in]
@param[out]
@retval
*/
EFI_STATUS
LibGetFvSize (
IN FILE *InputFile,
OUT UINT32 *FvSize
);
/**
This function returns the next larger size that meets the alignment
requirement specified.
@param[in] ActualSize The size.
@param[in] Alignment The desired alignment.
@retval EFI_SUCCESS Function completed successfully.
@retval EFI_ABORTED The function encountered an error.
**/
UINT32
GetOccupiedSize (
IN UINT32 ActualSize,
IN UINT32 Alignment
);
/**
Converts ASCII characters to Unicode.
Assumes that the Unicode characters are only these defined in the ASCII set.
String - Pointer to string that is written to FILE.
UniString - Pointer to unicode string
The address to the ASCII string - same as AsciiStr.
**/
VOID
LibAscii2Unicode (
IN CHAR8 *String,
OUT CHAR16 *UniString
);
/**
Delete a directory and files in it.
@param[in] DirName Name of the directory need to be deleted.
@return EFI_INVALID_PARAMETER
@return EFI_SUCCESS
**/
EFI_STATUS
LibRmDir (
IN CHAR8* DirName
);
/**
Delete a file.
@param[in] FileName Name of the file need to be deleted.
@return EFI_INVALID_PARAMETER
@return EFI_SUCCESS
**/
EFI_STATUS
LibFmmtDeleteFile(
IN CHAR8 *FileName
);
/**
Free the whole Fd data structure.
@param[in] Fd The pointer point to the Fd data structure.
**/
VOID
LibFmmtFreeFd (
FIRMWARE_DEVICE *Fd
);
EFI_STATUS
LibEncapNewFvFile(
IN FV_INFORMATION *FvInFd,
IN CHAR8 *TemDir,
IN ENCAP_INFO_DATA *CurrentEncapData,
IN UINT32 Level_Break,
OUT FFS_INFORMATION **OutputFile
);
EFI_STATUS
LibLocateFvViaFvId (
IN FIRMWARE_DEVICE *FdData,
IN CHAR8 *FvId,
IN OUT FV_INFORMATION **FvInFd
);
EFI_HANDLE
LibPreDefinedGuidedTools (
VOID
);
EFI_STATUS
FvBufGetSize(
IN VOID *Fv,
OUT UINTN *Size
);
EFI_STATUS
FvBufFindNextFile(
IN VOID *Fv,
IN OUT UINTN *Key,
OUT VOID **File
);
#endif

View File

@ -1,6 +0,0 @@
a31280ad-481e-41b6-95e8-127f4c984779 TIANO TianoCompress
ee4e5898-3914-4259-9d6e-dc7bd79403cf LZMA LzmaCompress
fc1bcdb0-7d31-49aa-936a-a4600d9dd083 CRC32 GenCrc32
d42ae6bd-1352-4bfb-909a-ca72a6eae889 LZMAF86 LzmaF86Compress
3d532050-5cda-4fd0-879e-0f7f630d5afb BROTLI BrotliCompress

File diff suppressed because it is too large Load Diff

View File

@ -1,16 +0,0 @@
## @file
# GNU/Linux makefile for 'FMMT' module build.
#
# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
MAKEROOT ?= ..
APPNAME = FMMT
LIBS = -lCommon
OBJECTS = FmmtLib.o Rebase.o FirmwareModuleManagement.o
include $(MAKEROOT)/Makefiles/app.makefile

View File

@ -1,17 +0,0 @@
## @file
# Windows makefile for 'FMMT' module build.
#
# Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#
!INCLUDE ..\Makefiles\ms.common
APPNAME = FMMT
LIBS = $(LIB_PATH)\Common.lib
OBJECTS = FirmwareModuleManagement.obj FmmtLib.obj Rebase.obj
!INCLUDE ..\Makefiles\ms.app

View File

@ -1,846 +0,0 @@
/** @file
Library to rebase PE image.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include "Rebase.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef __GNUC__
#include <unistd.h>
#else
#include <io.h>
#include <direct.h>
#endif
#include <PeCoffLib.h>
#include <CommonLib.h>
#include <IndustryStandard/PeImage.h>
#include <FvLib.h>
#include "EfiUtilityMsgs.h"
static
EFI_STATUS
FfsRebaseImageRead(
IN VOID *FileHandle,
IN UINTN FileOffset,
IN OUT UINT32 *ReadSize,
OUT VOID *Buffer
);
EFI_STATUS
RebaseFfs(
IN OUT UINT64 BaseAddress,
IN CHAR8 *FileName,
IN OUT EFI_FFS_FILE_HEADER *FfsFile,
IN UINTN XipOffset
)
/*++
Routine Description:
This function determines if a file is XIP and should be rebased. It will
rebase any PE32 sections found in the file using the base address.
Arguments:
FvInfo A pointer to FV_INFO struture.
FileName Ffs File PathName
FfsFile A pointer to Ffs file image.
XipOffset The offset address to use for rebasing the XIP file image.
Returns:
EFI_SUCCESS The image was properly rebased.
EFI_INVALID_PARAMETER An input parameter is invalid.
EFI_ABORTED An error occurred while rebasing the input file image.
EFI_OUT_OF_RESOURCES Could not allocate a required resource.
EFI_NOT_FOUND No compressed sections could be found.
--*/
{
EFI_STATUS Status;
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
PE_COFF_LOADER_IMAGE_CONTEXT OrigImageContext;
EFI_PHYSICAL_ADDRESS XipBase;
EFI_PHYSICAL_ADDRESS NewPe32BaseAddress;
UINTN Index;
EFI_FILE_SECTION_POINTER CurrentPe32Section;
EFI_FFS_FILE_STATE SavedState;
EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
EFI_TE_IMAGE_HEADER *TEImageHeader;
UINT8 *MemoryImagePointer;
EFI_IMAGE_SECTION_HEADER *SectionHeader;
CHAR8 PeFileName[MAX_LONG_FILE_PATH];
CHAR8 *Cptr;
FILE *PeFile;
UINT8 *PeFileBuffer;
UINT32 PeFileSize;
CHAR8 *PdbPointer;
UINT32 FfsHeaderSize;
UINT32 CurSecHdrSize;
CHAR8 *LongFilePathName;
Index = 0;
MemoryImagePointer = NULL;
TEImageHeader = NULL;
ImgHdr = NULL;
SectionHeader = NULL;
Cptr = NULL;
PeFile = NULL;
PeFileBuffer = NULL;
//
// Don't need to relocate image when BaseAddress is zero and no ForceRebase Flag specified.
//
if (BaseAddress == 0) {
return EFI_SUCCESS;
}
XipBase = BaseAddress + XipOffset;
//
// We only process files potentially containing PE32 sections.
//
switch (FfsFile->Type) {
case EFI_FV_FILETYPE_SECURITY_CORE:
case EFI_FV_FILETYPE_PEI_CORE:
case EFI_FV_FILETYPE_PEIM:
case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
case EFI_FV_FILETYPE_DRIVER:
case EFI_FV_FILETYPE_DXE_CORE:
break;
case EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE:
//
// Rebase the inside FvImage.
//
GetChildFvFromFfs (BaseAddress, FfsFile, XipOffset);
//
// Search PE/TE section in FV sectin.
//
break;
default:
return EFI_SUCCESS;
}
FfsHeaderSize = GetFfsHeaderLength(FfsFile);
//
// Rebase each PE32 section
//
Status = EFI_SUCCESS;
for (Index = 1;; Index++) {
//
// Init Value
//
NewPe32BaseAddress = 0;
//
// Find Pe Image
//
Status = GetSectionByType(FfsFile, EFI_SECTION_PE32, Index, &CurrentPe32Section);
if (EFI_ERROR(Status)) {
break;
}
CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
//
// Initialize context
//
memset(&ImageContext, 0, sizeof (ImageContext));
ImageContext.Handle = (VOID *)((UINTN)CurrentPe32Section.Pe32Section + CurSecHdrSize);
ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)FfsRebaseImageRead;
Status = PeCoffLoaderGetImageInfo(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int)Status);
return Status;
}
//if ((ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
// (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64)) {
// mArm = TRUE;
//}
//
// Keep Image Context for PE image in FV
//
memcpy(&OrigImageContext, &ImageContext, sizeof (ImageContext));
//
// Get File PdbPointer
//
PdbPointer = PeCoffLoaderGetPdbPointer(ImageContext.Handle);
if (PdbPointer == NULL) {
PdbPointer = FileName;
}
//
// Get PeHeader pointer
//
ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN)CurrentPe32Section.Pe32Section + CurSecHdrSize + ImageContext.PeCoffHeaderOffset);
//
// Calculate the PE32 base address, based on file type
//
switch (FfsFile->Type) {
case EFI_FV_FILETYPE_SECURITY_CORE:
case EFI_FV_FILETYPE_PEI_CORE:
case EFI_FV_FILETYPE_PEIM:
case EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER:
//
// Check if section-alignment and file-alignment match or not
//
if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
//
// Xip module has the same section alignment and file alignment.
//
Error(NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
return EFI_ABORTED;
}
//
// PeImage has no reloc section. It will try to get reloc data from the original EFI image.
//
if (ImageContext.RelocationsStripped) {
//
// Construct the original efi file Name
//
if (strlen (FileName) > MAX_LONG_FILE_PATH - 1) {
Error(NULL, 0, 3000, "Invalid", "The file name for %s is too long.", FileName);
return EFI_ABORTED;
}
strncpy(PeFileName, FileName, MAX_LONG_FILE_PATH - 1);
PeFileName[MAX_LONG_FILE_PATH - 1] = 0;
Cptr = PeFileName + strlen(PeFileName);
while (*Cptr != '.') {
Cptr--;
}
if (*Cptr != '.') {
Error(NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
return EFI_ABORTED;
}
else {
*(Cptr + 1) = 'e';
*(Cptr + 2) = 'f';
*(Cptr + 3) = 'i';
*(Cptr + 4) = '\0';
}
LongFilePathName = LongFilePath(PeFileName);
if (LongFilePathName == NULL) {
Error(NULL, 0, 3000, "Invalid", "Fail to get long file path for file %s.", FileName);
return EFI_ABORTED;
}
PeFile = fopen(LongFilePathName, "rb");
if (PeFile == NULL) {
Warning(NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
//Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
//return EFI_ABORTED;
break;
}
//
// Get the file size
//
PeFileSize = _filelength(fileno(PeFile));
PeFileBuffer = (UINT8 *)malloc(PeFileSize);
if (PeFileBuffer == NULL) {
Error(NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
fclose(PeFile);
return EFI_OUT_OF_RESOURCES;
}
//
// Read Pe File
//
fread(PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
//
// close file
//
fclose(PeFile);
//
// Handle pointer to the original efi image.
//
ImageContext.Handle = PeFileBuffer;
Status = PeCoffLoaderGetImageInfo(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int)Status);
return Status;
}
ImageContext.RelocationsStripped = FALSE;
}
NewPe32BaseAddress = XipBase + (UINTN)CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
break;
case EFI_FV_FILETYPE_DRIVER:
case EFI_FV_FILETYPE_DXE_CORE:
//
// Check if section-alignment and file-alignment match or not
//
if ((ImgHdr->Pe32.OptionalHeader.SectionAlignment != ImgHdr->Pe32.OptionalHeader.FileAlignment)) {
//
// Xip module has the same section alignment and file alignment.
//
Error(NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);
return EFI_ABORTED;
}
NewPe32BaseAddress = XipBase + (UINTN)CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;
break;
default:
//
// Not supported file type
//
return EFI_SUCCESS;
}
//
// Relocation doesn't exist
//
if (ImageContext.RelocationsStripped) {
Warning(NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
continue;
}
//
// Relocation exist and rebase
//
//
// Load and Relocate Image Data
//
MemoryImagePointer = (UINT8 *)malloc((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
if (MemoryImagePointer == NULL) {
Error(NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
return EFI_OUT_OF_RESOURCES;
}
memset((VOID *)MemoryImagePointer, 0, (UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
ImageContext.ImageAddress = ((UINTN)MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN)ImageContext.SectionAlignment - 1));
Status = PeCoffLoaderLoadImage(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
free((VOID *)MemoryImagePointer);
return Status;
}
ImageContext.DestinationAddress = NewPe32BaseAddress;
Status = PeCoffLoaderRelocateImage(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName);
free((VOID *)MemoryImagePointer);
return Status;
}
//
// Copy Relocated data to raw image file.
//
SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(
(UINTN)ImgHdr +
sizeof (UINT32)+
sizeof (EFI_IMAGE_FILE_HEADER)+
ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader
);
for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index++, SectionHeader++) {
CopyMem(
(UINT8 *)CurrentPe32Section.Pe32Section + CurSecHdrSize + SectionHeader->PointerToRawData,
(VOID*)(UINTN)(ImageContext.ImageAddress + SectionHeader->VirtualAddress),
SectionHeader->SizeOfRawData
);
}
free((VOID *)MemoryImagePointer);
MemoryImagePointer = NULL;
if (PeFileBuffer != NULL) {
free(PeFileBuffer);
PeFileBuffer = NULL;
}
//
// Update Image Base Address
//
if (ImgHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
ImgHdr->Pe32.OptionalHeader.ImageBase = (UINT32)NewPe32BaseAddress;
}
else if (ImgHdr->Pe32Plus.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
ImgHdr->Pe32Plus.OptionalHeader.ImageBase = NewPe32BaseAddress;
}
else {
Error(NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
ImgHdr->Pe32.OptionalHeader.Magic,
FileName
);
return EFI_ABORTED;
}
//
// Now update file checksum
//
if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
SavedState = FfsFile->State;
FfsFile->IntegrityCheck.Checksum.File = 0;
FfsFile->State = 0;
FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8(
(UINT8 *)((UINT8 *)FfsFile + FfsHeaderSize),
GetFfsFileLength(FfsFile) - FfsHeaderSize
);
FfsFile->State = SavedState;
}
}
if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE &&
FfsFile->Type != EFI_FV_FILETYPE_PEI_CORE &&
FfsFile->Type != EFI_FV_FILETYPE_PEIM &&
FfsFile->Type != EFI_FV_FILETYPE_COMBINED_PEIM_DRIVER &&
FfsFile->Type != EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE
) {
//
// Only Peim code may have a TE section
//
return EFI_SUCCESS;
}
//
// Now process TE sections
//
for (Index = 1;; Index++) {
NewPe32BaseAddress = 0;
//
// Find Te Image
//
Status = GetSectionByType(FfsFile, EFI_SECTION_TE, Index, &CurrentPe32Section);
if (EFI_ERROR(Status)) {
break;
}
CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);
//
// Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off
// by GenTEImage
//
TEImageHeader = (EFI_TE_IMAGE_HEADER *)((UINT8 *)CurrentPe32Section.Pe32Section + CurSecHdrSize);
//
// Initialize context, load image info.
//
memset(&ImageContext, 0, sizeof (ImageContext));
ImageContext.Handle = (VOID *)TEImageHeader;
ImageContext.ImageRead = (PE_COFF_LOADER_READ_FILE)FfsRebaseImageRead;
Status = PeCoffLoaderGetImageInfo(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int)Status);
return Status;
}
//if ((ImageContext.Machine == EFI_IMAGE_MACHINE_ARMT) ||
// (ImageContext.Machine == EFI_IMAGE_MACHINE_AARCH64)) {
// mArm = TRUE;
//}
//
// Keep Image Context for TE image in FV
//
memcpy(&OrigImageContext, &ImageContext, sizeof (ImageContext));
//
// Get File PdbPointer
//
PdbPointer = PeCoffLoaderGetPdbPointer(ImageContext.Handle);
if (PdbPointer == NULL) {
PdbPointer = FileName;
}
//
// Set new rebased address.
//
NewPe32BaseAddress = XipBase + (UINTN)TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) \
- TEImageHeader->StrippedSize - (UINTN)FfsFile;
//
// if reloc is stripped, try to get the original efi image to get reloc info.
//
if (ImageContext.RelocationsStripped) {
//
// Construct the original efi file name
//
if (strlen (FileName) > MAX_LONG_FILE_PATH - 1) {
Error(NULL, 0, 3000, "Invalid", "The file name for %s is too long.", FileName);
return EFI_ABORTED;
}
strncpy(PeFileName, FileName, MAX_LONG_FILE_PATH - 1);
PeFileName[MAX_LONG_FILE_PATH - 1] = 0;
Cptr = PeFileName + strlen(PeFileName);
while (*Cptr != '.') {
Cptr--;
}
if (*Cptr != '.') {
Error(NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
return EFI_ABORTED;
}
else {
*(Cptr + 1) = 'e';
*(Cptr + 2) = 'f';
*(Cptr + 3) = 'i';
*(Cptr + 4) = '\0';
}
LongFilePathName = LongFilePath(PeFileName);
if (LongFilePathName == NULL) {
Error(NULL, 0, 3000, "Invalid", "Fail to get long file path for file %s.", FileName);
return EFI_ABORTED;
}
PeFile = fopen(LongFilePathName, "rb");
if (PeFile == NULL) {
Warning(NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
//Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);
//return EFI_ABORTED;
}
else {
//
// Get the file size
//
PeFileSize = _filelength(fileno(PeFile));
PeFileBuffer = (UINT8 *)malloc(PeFileSize);
if (PeFileBuffer == NULL) {
Error(NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
fclose(PeFile);
return EFI_OUT_OF_RESOURCES;
}
//
// Read Pe File
//
fread(PeFileBuffer, sizeof (UINT8), PeFileSize, PeFile);
//
// close file
//
fclose(PeFile);
//
// Append reloc section into TeImage
//
ImageContext.Handle = PeFileBuffer;
Status = PeCoffLoaderGetImageInfo(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int)Status);
return Status;
}
ImageContext.RelocationsStripped = FALSE;
}
}
//
// Relocation doesn't exist
//
if (ImageContext.RelocationsStripped) {
Warning(NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);
continue;
}
//
// Relocation exist and rebase
//
//
// Load and Relocate Image Data
//
MemoryImagePointer = (UINT8 *)malloc((UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
if (MemoryImagePointer == NULL) {
Error(NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);
return EFI_OUT_OF_RESOURCES;
}
memset((VOID *)MemoryImagePointer, 0, (UINTN)ImageContext.ImageSize + ImageContext.SectionAlignment);
ImageContext.ImageAddress = ((UINTN)MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN)ImageContext.SectionAlignment - 1));
Status = PeCoffLoaderLoadImage(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);
free((VOID *)MemoryImagePointer);
return Status;
}
//
// Reloacate TeImage
//
ImageContext.DestinationAddress = NewPe32BaseAddress;
Status = PeCoffLoaderRelocateImage(&ImageContext);
if (EFI_ERROR(Status)) {
Error(NULL, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of TE image %s", FileName);
free((VOID *)MemoryImagePointer);
return Status;
}
//
// Copy the relocated image into raw image file.
//
SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(TEImageHeader + 1);
for (Index = 0; Index < TEImageHeader->NumberOfSections; Index++, SectionHeader++) {
if (!ImageContext.IsTeImage) {
CopyMem(
(UINT8 *)TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER)-TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
(VOID*)(UINTN)(ImageContext.ImageAddress + SectionHeader->VirtualAddress),
SectionHeader->SizeOfRawData
);
}
else {
CopyMem(
(UINT8 *)TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER)-TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,
(VOID*)(UINTN)(ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER)-TEImageHeader->StrippedSize + SectionHeader->VirtualAddress),
SectionHeader->SizeOfRawData
);
}
}
//
// Free the allocated memory resource
//
free((VOID *)MemoryImagePointer);
MemoryImagePointer = NULL;
if (PeFileBuffer != NULL) {
free(PeFileBuffer);
PeFileBuffer = NULL;
}
//
// Update Image Base Address
//
TEImageHeader->ImageBase = NewPe32BaseAddress;
//
// Now update file checksum
//
if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {
SavedState = FfsFile->State;
FfsFile->IntegrityCheck.Checksum.File = 0;
FfsFile->State = 0;
FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8(
(UINT8 *)((UINT8 *)FfsFile + FfsHeaderSize),
GetFfsFileLength(FfsFile) - FfsHeaderSize
);
FfsFile->State = SavedState;
}
}
return EFI_SUCCESS;
}
EFI_STATUS
FfsRebaseImageRead(
IN VOID *FileHandle,
IN UINTN FileOffset,
IN OUT UINT32 *ReadSize,
OUT VOID *Buffer
)
/*++
Routine Description:
Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
Arguments:
FileHandle - The handle to the PE/COFF file
FileOffset - The offset, in bytes, into the file to read
ReadSize - The number of bytes to read from the file starting at FileOffset
Buffer - A pointer to the buffer to read the data into.
Returns:
EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
--*/
{
CHAR8 *Destination8;
CHAR8 *Source8;
UINT32 Length;
Destination8 = Buffer;
Source8 = (CHAR8 *)((UINTN)FileHandle + FileOffset);
Length = *ReadSize;
while (Length--) {
*(Destination8++) = *(Source8++);
}
return EFI_SUCCESS;
}
EFI_STATUS
GetChildFvFromFfs (
IN UINT64 BaseAddress,
IN EFI_FFS_FILE_HEADER *FfsFile,
IN UINTN XipOffset
)
/*++
Routine Description:
This function gets all child FvImages in the input FfsFile, and records
their base address to the parent image.
Arguments:
FvInfo A pointer to FV_INFO struture.
FfsFile A pointer to Ffs file image that may contain FvImage.
XipOffset The offset address to the parent FvImage base.
Returns:
EFI_SUCCESS Base address of child Fv image is recorded.
--*/
{
EFI_STATUS Status;
UINTN Index;
EFI_FILE_SECTION_POINTER SubFvSection;
EFI_FIRMWARE_VOLUME_HEADER *SubFvImageHeader;
EFI_PHYSICAL_ADDRESS SubFvBaseAddress;
EFI_FIRMWARE_VOLUME_HEADER *OrigFvHeader;
UINT32 OrigFvLength;
EFI_PHYSICAL_ADDRESS OrigFvBaseAddress;
EFI_FFS_FILE_HEADER *CurrentFile;
//
// Initialize FV library, saving previous values
//
OrigFvHeader = NULL;
GetFvHeader (&OrigFvHeader, &OrigFvLength);
OrigFvBaseAddress = BaseAddress;
for (Index = 1;; Index++) {
//
// Find FV section
//
Status = GetSectionByType (FfsFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Index, &SubFvSection);
if (EFI_ERROR (Status)) {
break;
}
SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + GetSectionHeaderLength(SubFvSection.FVImageSection));
//
// Rebase on Flash
//
SubFvBaseAddress = OrigFvBaseAddress + (UINTN) SubFvImageHeader - (UINTN) FfsFile + XipOffset;
//mFvBaseAddress[mFvBaseAddressNumber ++ ] = SubFvBaseAddress;
BaseAddress = SubFvBaseAddress;
InitializeFvLib(SubFvImageHeader, (UINT32) SubFvImageHeader->FvLength);
Status = GetNextFile (NULL, &CurrentFile);
if (EFI_ERROR (Status)) {
Error (NULL, 0, 0003, "error parsing FV image", "FFS file can't be found");
continue;
}
while (CurrentFile) {
RebaseFfs (BaseAddress, "", CurrentFile, (UINTN) CurrentFile - (UINTN) SubFvImageHeader);
Status = GetNextFile (CurrentFile, &CurrentFile);
if (EFI_ERROR (Status)) {
break;
}
}
}
BaseAddress = OrigFvBaseAddress;
if (OrigFvHeader != NULL) {
InitializeFvLib(OrigFvHeader, OrigFvLength);
}
return EFI_SUCCESS;
}
EFI_STATUS
GetPe32Info (
IN UINT8 *Pe32,
OUT UINT32 *EntryPoint,
OUT UINT32 *BaseOfCode,
OUT UINT16 *MachineType
)
/*++
Routine Description:
Retrieves the PE32 entry point offset and machine type from PE image or TeImage.
See EfiImage.h for machine types. The entry point offset is from the beginning
of the PE32 buffer passed in.
Arguments:
Pe32 Beginning of the PE32.
EntryPoint Offset from the beginning of the PE32 to the image entry point.
BaseOfCode Base address of code.
MachineType Magic number for the machine type.
Returns:
EFI_SUCCESS Function completed successfully.
EFI_ABORTED Error encountered.
EFI_INVALID_PARAMETER A required parameter was NULL.
EFI_UNSUPPORTED The operation is unsupported.
--*/
{
EFI_IMAGE_DOS_HEADER *DosHeader;
EFI_IMAGE_OPTIONAL_HEADER_UNION *ImgHdr;
EFI_TE_IMAGE_HEADER *TeHeader;
//
// Verify input parameters
//
if (Pe32 == NULL) {
return EFI_INVALID_PARAMETER;
}
//
// First check whether it is one TE Image.
//
TeHeader = (EFI_TE_IMAGE_HEADER *) Pe32;
if (TeHeader->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
//
// By TeImage Header to get output
//
*EntryPoint = TeHeader->AddressOfEntryPoint + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
*BaseOfCode = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;
*MachineType = TeHeader->Machine;
} else {
//
// Then check whether
// First is the DOS header
//
DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;
//
// Verify DOS header is expected
//
if (DosHeader->e_magic != EFI_IMAGE_DOS_SIGNATURE) {
Error (NULL, 0, 3000, "Invalid", "Unknown magic number in the DOS header, 0x%04X.", DosHeader->e_magic);
return EFI_UNSUPPORTED;
}
//
// Immediately following is the NT header.
//
ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) Pe32 + DosHeader->e_lfanew);
//
// Verify NT header is expected
//
if (ImgHdr->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {
Error (NULL, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr->Pe32.Signature);
return EFI_UNSUPPORTED;
}
//
// Get output
//
*EntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
*BaseOfCode = ImgHdr->Pe32.OptionalHeader.BaseOfCode;
*MachineType = ImgHdr->Pe32.FileHeader.Machine;
}
//
// Verify machine type is supported
//
if ((*MachineType != EFI_IMAGE_MACHINE_IA32) && (*MachineType != EFI_IMAGE_MACHINE_X64) && (*MachineType != EFI_IMAGE_MACHINE_EBC) &&
(*MachineType != EFI_IMAGE_MACHINE_ARMT) && (*MachineType != EFI_IMAGE_MACHINE_AARCH64)) {
Error (NULL, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");
return EFI_UNSUPPORTED;
}
return EFI_SUCCESS;
}

View File

@ -1,31 +0,0 @@
/** @file Rebase.h
Library to rebase PE image.
Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#ifndef _FMMT_REBASE_H
#define _FMMT_REBASE_H
#include <Common/UefiBaseTypes.h>
#include <Common/PiFirmwareFile.h>
EFI_STATUS
RebaseFfs(
IN OUT UINT64 BaseAddress,
IN CHAR8 *FileName,
IN OUT EFI_FFS_FILE_HEADER *FfsFile,
IN UINTN XipOffset
);
EFI_STATUS
GetChildFvFromFfs (
IN UINT64 BaseAddress,
IN EFI_FFS_FILE_HEADER *FfsFile,
IN UINTN XipOffset
);
#endif

View File

@ -47,7 +47,6 @@ VFRAUTOGEN = VfrCompile/VfrLexer.h
APPLICATIONS = \
BrotliCompress \
VfrCompile \
FMMT \
BfmLib \
EfiRom \
FCE \

View File

@ -12,7 +12,6 @@ LIBRARIES = Common
APPLICATIONS = \
VfrCompile \
BrotliCompress \
FMMT \
BfmLib \
EfiRom \
FCE \