/** @file\r
+Converts a pe32+ image to an FW, Te image type, or other specific image.\r
\r
-Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
This program and the accompanying materials\r
are licensed and made available under the terms and conditions of the BSD License\r
which accompanies this distribution. The full text of the license may be found at\r
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
\r
-Module Name:\r
-\r
- GenFw.c\r
-\r
-Abstract:\r
-\r
- Converts a pe32+ image to an FW, Te image type, or other specific image.\r
-\r
**/\r
\r
#include "WinNtInclude.h"\r
#define DEFAULT_MC_PAD_BYTE_VALUE 0xFF\r
#define DEFAULT_MC_ALIGNMENT 16\r
\r
-#ifndef _MAX_PATH\r
-#define _MAX_PATH 500\r
-#endif\r
-\r
#define STATUS_IGNORE 0xA\r
//\r
// Structure definition for a microcode header\r
\r
--*/\r
{\r
- fprintf (stdout, "%s Version %d.%d\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
+ fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
}\r
\r
STATIC\r
//\r
// Copyright declaration\r
//\r
- fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
+ fprintf (stdout, "Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.\n\n");\r
\r
//\r
// Details Option\r
PIC_PEIM, RELOCATABLE_PEIM, BS_DRIVER, RT_DRIVER,\n\\r
APPLICATION, SAL_RT_DRIVER to support all module types\n\\r
It can only be used together with --keepexceptiontable,\n\\r
- --keepzeropending, -r, -o option.It is a action option.\n\\r
- If it is combined with other action options, the later\n\\r
- input action option will override the previous one.\n");\r
+ --keepzeropending, --keepoptionalheader, -r, -o option.\n\\r
+ It is a action option. If it is combined with other action options,\n\\r
+ the later input action option will override the previous one.\n");\r
fprintf (stdout, " -c, --acpi Create Acpi table.\n\\r
It can't be combined with other action options\n\\r
except for -o, -r option. It is a action option.\n\\r
input action option will override the previous one.\n");\r
fprintf (stdout, " -t, --terse Create Te Image.\n\\r
It can only be used together with --keepexceptiontable,\n\\r
- --keepzeropending, -r, -o option.It is a action option.\n\\r
- If it is combined with other action options, the later\n\\r
- input action option will override the previous one.\n");\r
+ --keepzeropending, --keepoptionalheader, -r, -o option.\n\\r
+ It is a action option. If it is combined with other action options,\n\\r
+ the later input action option will override the previous one.\n");\r
fprintf (stdout, " -u, --dump Dump TeImage Header.\n\\r
It can't be combined with other action options\n\\r
except for -o, -r option. It is a action option.\n\\r
fprintf (stdout, " --keepexceptiontable Don't clear exception table.\n\\r
This option can be used together with -e or -t.\n\\r
It doesn't work for other options.\n");\r
+ fprintf (stdout, " --keepoptionalheader Don't zero PE/COFF optional header fields.\n\\r
+ This option can be used together with -e or -t.\n\\r
+ It doesn't work for other options.\n");\r
fprintf (stdout, " --keepzeropending Don't strip zero pending of .reloc.\n\\r
This option can be used together with -e or -t.\n\\r
It doesn't work for other options.\n");\r
SectionHeader->SizeOfRawData\r
);\r
}\r
- SectionHeader->SizeOfRawData = SectionHeader->Misc.VirtualSize;\r
+ //\r
+ // Make the size of raw data in section header alignment.\r
+ //\r
+ SectionHeader->SizeOfRawData = (SectionHeader->Misc.VirtualSize + PeHdr->Pe32.OptionalHeader.FileAlignment - 1) & (~(PeHdr->Pe32.OptionalHeader.FileAlignment - 1));\r
SectionHeader->PointerToRawData = SectionHeader->VirtualAddress;\r
}\r
\r
}\r
}\r
\r
- //\r
- // No available section header is found.\r
- //\r
- if (Index == ImgHdr->Pe32.FileHeader.NumberOfSections) {\r
- return EFI_NOT_FOUND;\r
- }\r
- \r
//\r
// BaseAddress is set to section header.\r
//\r
STATUS Status;\r
BOOLEAN ReplaceFlag;\r
BOOLEAN KeepExceptionTableFlag;\r
+ BOOLEAN KeepOptionalHeaderFlag;\r
BOOLEAN KeepZeroPendingFlag;\r
UINT64 LogLevel;\r
EFI_TE_IMAGE_HEADER TEImageHeader;\r
Optional32 = NULL;\r
Optional64 = NULL;\r
KeepExceptionTableFlag = FALSE;\r
+ KeepOptionalHeaderFlag = FALSE;\r
KeepZeroPendingFlag = FALSE;\r
NumberOfFormPacakge = 0;\r
HiiPackageListBuffer = NULL;\r
continue;\r
}\r
\r
+ if (stricmp(argv[0], "--keepoptionalheader") == 0) {\r
+ KeepOptionalHeaderFlag = TRUE;\r
+ argc--;\r
+ argv++;\r
+ continue;\r
+ }\r
+\r
if (stricmp (argv[0], "--keepzeropending") == 0) {\r
KeepZeroPendingFlag = TRUE;\r
argc --;\r
// Open output file and Write image into the output file.\r
//\r
if (OutImageName != NULL) {\r
- fpOut = fopen (OutImageName, "rb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "rb");\r
if (fpOut != NULL) {\r
//\r
// Get Output file time stamp\r
//\r
// Open input file and read file data into file buffer.\r
//\r
- fpIn = fopen (mInImageName, "rb");\r
+ fpIn = fopen (LongFilePath (mInImageName), "rb");\r
if (fpIn == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
goto Finish;\r
//\r
// Open output file handle.\r
//\r
- fpOut = fopen (OutImageName, "wb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "wb");\r
if (!fpOut) {\r
Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
goto Finish;\r
//\r
HiiPackageListHeader.PackageLength = sizeof (EFI_HII_PACKAGE_LIST_HEADER);\r
for (Index = 0; Index < InputFileNum; Index ++) {\r
- fpIn = fopen (InputFileName [Index], "rb");\r
+ fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
if (fpIn == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
goto Finish;\r
memcpy (HiiPackageListBuffer, &HiiPackageListHeader, sizeof (HiiPackageListHeader));\r
HiiPackageDataPointer = HiiPackageListBuffer + sizeof (HiiPackageListHeader);\r
for (Index = 0; Index < InputFileNum; Index ++) {\r
- fpIn = fopen (InputFileName [Index], "rb");\r
+ fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
if (fpIn == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
free (HiiPackageListBuffer);\r
//\r
// Open output file handle.\r
//\r
- fpOut = fopen (OutImageName, "wb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "wb");\r
if (!fpOut) {\r
Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
goto Finish;\r
}\r
for (Index = 0; Index < InputFileNum; Index ++) {\r
- fpIn = fopen (InputFileName [Index], "rb");\r
+ fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
if (!fpIn) {\r
Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
goto Finish;\r
// Convert MicroCode.txt file to MicroCode.bin file\r
//\r
if (mOutImageType == FW_MCI_IMAGE) {\r
- fpIn = fopen (mInImageName, "r");\r
+ fpIn = fopen (LongFilePath (mInImageName), "r");\r
if (fpIn == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
goto Finish;\r
// Open the output file handle.\r
//\r
if (ReplaceFlag) {\r
- fpInOut = fopen (mInImageName, "wb");\r
+ fpInOut = fopen (LongFilePath (mInImageName), "wb");\r
if (fpInOut == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
goto Finish;\r
}\r
} else {\r
if (OutImageName != NULL) {\r
- fpOut = fopen (OutImageName, "wb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "wb");\r
} else {\r
fpOut = stdout;\r
}\r
\r
if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->Pe32.OptionalHeader;\r
- Optional32->MajorOperatingSystemVersion = 0;\r
- Optional32->MinorOperatingSystemVersion = 0;\r
- Optional32->MajorImageVersion = 0;\r
- Optional32->MinorImageVersion = 0;\r
- Optional32->MajorSubsystemVersion = 0;\r
- Optional32->MinorSubsystemVersion = 0;\r
- Optional32->Win32VersionValue = 0;\r
- Optional32->CheckSum = 0;\r
- Optional32->SizeOfStackReserve = 0;\r
- Optional32->SizeOfStackCommit = 0;\r
- Optional32->SizeOfHeapReserve = 0;\r
- Optional32->SizeOfHeapCommit = 0;\r
-\r
+ if (!KeepOptionalHeaderFlag) {\r
+ Optional32->MajorOperatingSystemVersion = 0;\r
+ Optional32->MinorOperatingSystemVersion = 0;\r
+ Optional32->MajorImageVersion = 0;\r
+ Optional32->MinorImageVersion = 0;\r
+ Optional32->MajorSubsystemVersion = 0;\r
+ Optional32->MinorSubsystemVersion = 0;\r
+ Optional32->Win32VersionValue = 0;\r
+ Optional32->CheckSum = 0;\r
+ Optional32->SizeOfStackReserve = 0;\r
+ Optional32->SizeOfStackCommit = 0;\r
+ Optional32->SizeOfHeapReserve = 0;\r
+ Optional32->SizeOfHeapCommit = 0;\r
+ }\r
TEImageHeader.AddressOfEntryPoint = Optional32->AddressOfEntryPoint;\r
TEImageHeader.BaseOfCode = Optional32->BaseOfCode;\r
TEImageHeader.ImageBase = (UINT64) (Optional32->ImageBase);\r
}\r
} else if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
Optional64 = (EFI_IMAGE_OPTIONAL_HEADER64 *)&PeHdr->Pe32.OptionalHeader;\r
- Optional64->MajorOperatingSystemVersion = 0;\r
- Optional64->MinorOperatingSystemVersion = 0;\r
- Optional64->MajorImageVersion = 0;\r
- Optional64->MinorImageVersion = 0;\r
- Optional64->MajorSubsystemVersion = 0;\r
- Optional64->MinorSubsystemVersion = 0;\r
- Optional64->Win32VersionValue = 0;\r
- Optional64->CheckSum = 0;\r
- Optional64->SizeOfStackReserve = 0;\r
- Optional64->SizeOfStackCommit = 0;\r
- Optional64->SizeOfHeapReserve = 0;\r
- Optional64->SizeOfHeapCommit = 0;\r
-\r
+ if (!KeepOptionalHeaderFlag) {\r
+ Optional64->MajorOperatingSystemVersion = 0;\r
+ Optional64->MinorOperatingSystemVersion = 0;\r
+ Optional64->MajorImageVersion = 0;\r
+ Optional64->MinorImageVersion = 0;\r
+ Optional64->MajorSubsystemVersion = 0;\r
+ Optional64->MinorSubsystemVersion = 0;\r
+ Optional64->Win32VersionValue = 0;\r
+ Optional64->CheckSum = 0;\r
+ Optional64->SizeOfStackReserve = 0;\r
+ Optional64->SizeOfStackCommit = 0;\r
+ Optional64->SizeOfHeapReserve = 0;\r
+ Optional64->SizeOfHeapCommit = 0;\r
+ }\r
TEImageHeader.AddressOfEntryPoint = Optional64->AddressOfEntryPoint;\r
TEImageHeader.BaseOfCode = Optional64->BaseOfCode;\r
TEImageHeader.ImageBase = (UINT64) (Optional64->ImageBase);\r
//\r
// Update File when File is changed.\r
//\r
- fpInOut = fopen (mInImageName, "wb");\r
+ fpInOut = fopen (LongFilePath (mInImageName), "wb");\r
if (fpInOut == NULL) {\r
Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
goto Finish;\r
//\r
// Update File when File is changed or File is old.\r
//\r
- fpOut = fopen (OutImageName, "wb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "wb");\r
if (fpOut == NULL) {\r
Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
goto Finish;\r
if (OutputFileBuffer == NULL) {\r
remove (OutImageName);\r
} else {\r
- fpOut = fopen (OutImageName, "wb");\r
+ fpOut = fopen (LongFilePath (OutImageName), "wb");\r
fwrite (OutputFileBuffer, 1, OutputFileLength, fpOut);\r
fclose (fpOut);\r
}\r
if (ReportFileName != NULL) {\r
strcpy (ReportFileName, OutImageName);\r
strcpy (ReportFileName + (FileLen - 4), ".txt"); \r
- ReportFile = fopen (ReportFileName, "w+");\r
+ ReportFile = fopen (LongFilePath (ReportFileName), "w+");\r
if (ReportFile != NULL) {\r
fprintf (ReportFile, "MODULE_SIZE = %u\n", (unsigned) mImageSize);\r
fprintf (ReportFile, "TIME_STAMP = %u\n", (unsigned) mImageTimeStamp);\r
// Get Debug, Export and Resource EntryTable RVA address.\r
// Resource Directory entry need to review.\r
//\r
- if (FileHdr->Machine == EFI_IMAGE_MACHINE_IA32) {\r
- Optional32Hdr = (EFI_IMAGE_OPTIONAL_HEADER32 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));\r
+ Optional32Hdr = (EFI_IMAGE_OPTIONAL_HEADER32 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));\r
+ if (Optional32Hdr->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINT8 *) Optional32Hdr + FileHdr->SizeOfOptionalHeader);\r
if (Optional32Hdr->NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_EXPORT && \\r
Optional32Hdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size != 0) {\r