3 Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 Converts a pe32+ image to an FW, Te image type, or other specific image.
22 #include "WinNtInclude.h"
27 #include <sys/types.h>
36 #include <Common/UefiBaseTypes.h>
37 #include <IndustryStandard/PeImage.h>
38 #include <Common/UefiInternalFormRepresentation.h>
41 // Acpi Table definition
43 #include <IndustryStandard/Acpi.h>
44 #include <IndustryStandard/Acpi1_0.h>
45 #include <IndustryStandard/Acpi2_0.h>
46 #include <IndustryStandard/Acpi3_0.h>
47 #include <IndustryStandard/MemoryMappedConfigurationSpaceAccessTable.h>
49 #include "CommonLib.h"
50 #include "PeCoffLib.h"
52 #include "EfiUtilityMsgs.h"
57 // Version of this utility
59 #define UTILITY_NAME "GenFw"
60 #define UTILITY_MAJOR_VERSION 0
61 #define UTILITY_MINOR_VERSION 2
63 #define HII_RESOURCE_SECTION_INDEX 1
64 #define HII_RESOURCE_SECTION_NAME "HII"
66 #define DEFAULT_MC_PAD_BYTE_VALUE 0xFF
67 #define DEFAULT_MC_ALIGNMENT 16
73 #define STATUS_IGNORE 0xA
75 // Structure definition for a microcode header
85 UINT32 DataSize
; // if 0, then TotalSize = 2048, and TotalSize field is invalid
86 UINT32 TotalSize
; // number of bytes
88 } MICROCODE_IMAGE_HEADER
;
90 static EFI_GUID mZeroGuid
= {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
92 static const char *gHiiPackageRCFileHeader
[] = {
94 "// DO NOT EDIT -- auto-generated file",
100 // Module image information
103 UINT32 mImageTimeStamp
= 0;
104 UINT32 mImageSize
= 0;
105 UINT32 mOutImageType
= FW_DUMMY_IMAGE
;
111 IN OUT UINT8
*FileBuffer
,
118 IN OUT UINT8
*FileBuffer
,
138 Print out version information for this utility.
150 fprintf (stdout
, "%s Version %d.%d %s \n", UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
177 fprintf (stdout
, "\nUsage: %s [options] <input_file>\n\n", UTILITY_NAME
);
180 // Copyright declaration
182 fprintf (stdout
, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");
187 fprintf (stdout
, "Options:\n");
188 fprintf (stdout
, " -o FileName, --outputfile FileName\n\
189 File will be created to store the ouput content.\n");
190 fprintf (stdout
, " -e EFI_FILETYPE, --efiImage EFI_FILETYPE\n\
191 Create Efi Image. EFI_FILETYPE is one of BASE,SMM_CORE,\n\
192 PEI_CORE, PEIM, DXE_CORE, DXE_DRIVER, UEFI_APPLICATION,\n\
193 SEC, DXE_SAL_DRIVER, UEFI_DRIVER, DXE_RUNTIME_DRIVER,\n\
194 DXE_SMM_DRIVER, SECURITY_CORE, COMBINED_PEIM_DRIVER,\n\
195 PIC_PEIM, RELOCATABLE_PEIM, BS_DRIVER, RT_DRIVER,\n\
196 APPLICATION, SAL_RT_DRIVER to support all module types\n\
197 It can only be used together with --keepexceptiontable,\n\
198 --keepzeropending, -r, -o option.It is a action option.\n\
199 If it is combined with other action options, the later\n\
200 input action option will override the previous one.\n");
201 fprintf (stdout
, " -c, --acpi Create Acpi table.\n\
202 It can't be combined with other action options\n\
203 except for -o, -r option. It is a action option.\n\
204 If it is combined with other action options, the later\n\
205 input action option will override the previous one.\n");
206 fprintf (stdout
, " -t, --terse Create Te Image.\n\
207 It can only be used together with --keepexceptiontable,\n\
208 --keepzeropending, -r, -o option.It is a action option.\n\
209 If it is combined with other action options, the later\n\
210 input action option will override the previous one.\n");
211 fprintf (stdout
, " -u, --dump Dump TeImage Header.\n\
212 It can't be combined with other action options\n\
213 except for -o, -r option. It is a action option.\n\
214 If it is combined with other action options, the later\n\
215 input action option will override the previous one.\n");
216 fprintf (stdout
, " -z, --zero Zero the Debug Data Fields in the PE input image file.\n\
217 It also zeros the time stamp fields.\n\
218 This option can be used to compare the binary efi image.\n\
219 It can't be combined with other action options\n\
220 except for -o, -r option. It is a action option.\n\
221 If it is combined with other action options, the later\n\
222 input action option will override the previous one.\n");
223 fprintf (stdout
, " -b, --exe2bin Convert the input EXE to the output BIN file.\n\
224 It can't be combined with other action options\n\
225 except for -o, -r option. It is a action option.\n\
226 If it is combined with other action options, the later\n\
227 input action option will override the previous one.\n");;
228 fprintf (stdout
, " -l, --stripped Strip off the relocation info from PE or TE image.\n\
229 It can't be combined with other action options\n\
230 except for -o, -r option. It is a action option.\n\
231 If it is combined with other action options, the later\n\
232 input action option will override the previous one.\n");
233 fprintf (stdout
, " -s timedate, --stamp timedate\n\
234 timedate format is \"yyyy-mm-dd 00:00:00\". if timedata \n\
235 is set to NOW, current system time is used. The support\n\
236 date scope is 1970-01-01 00+timezone:00:00\n\
237 ~ 2038-01-19 03+timezone:14:07\n\
238 The scope is adjusted according to the different zones.\n\
239 It can't be combined with other action options\n\
240 except for -o, -r option. It is a action option.\n\
241 If it is combined with other action options, the later\n\
242 input action option will override the previous one.\n");
243 fprintf (stdout
, " -m, --mcifile Convert input microcode txt file to microcode bin file.\n\
244 It can't be combined with other action options\n\
245 except for -o option. It is a action option.\n\
246 If it is combined with other action options, the later\n\
247 input action option will override the previous one.\n");
248 fprintf (stdout
, " -j, --join Combine multi microcode bin files to one file.\n\
249 It can be specified with -a, -p, -o option.\n\
250 No other options can be combined with it.\n\
251 If it is combined with other action options, the later\n\
252 input action option will override the previous one.\n");
253 fprintf (stdout
, " -a NUM, --align NUM NUM is one HEX or DEC format alignment value.\n\
254 This option is only used together with -j option.\n");
255 fprintf (stdout
, " -p NUM, --pad NUM NUM is one HEX or DEC format padding value.\n\
256 This option is only used together with -j option.\n");
257 fprintf (stdout
, " --keepexceptiontable Don't clear exception table.\n\
258 This option can be used together with -e or -t.\n\
259 It doesn't work for other options.\n");
260 fprintf (stdout
, " --keepzeropending Don't strip zero pending of .reloc.\n\
261 This option can be used together with -e or -t.\n\
262 It doesn't work for other options.\n");
263 fprintf (stdout
, " -r, --replace Overwrite the input file with the output content.\n\
264 If more input files are specified,\n\
265 the last input file will be as the output file.\n");
266 fprintf (stdout
, " -g HiiPackageListGuid, --hiiguid HiiPackageListGuid\n\
267 Guid is used to specify hii package list guid.\n\
268 Its format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx\n\
269 If not specified, the first Form FormSet guid is used.\n");
270 fprintf (stdout
, " --hiipackage Combine all input binary hii pacakges into \n\
271 a single package list as the text resource data(RC).\n\
272 It can't be combined with other action options\n\
273 except for -o option. It is a action option.\n\
274 If it is combined with other action options, the later\n\
275 input action option will override the previous one.\n");
276 fprintf (stdout
, " --hiibinpackage Combine all input binary hii pacakges into \n\
277 a single package list as the binary resource section.\n\
278 It can't be combined with other action options\n\
279 except for -o option. It is a action option.\n\
280 If it is combined with other action options, the later\n\
281 input action option will override the previous one.\n");
282 fprintf (stdout
, " --rebase NewAddress Rebase image to new base address. New address \n\
283 is also set to the first none code section header.\n\
284 It can't be combined with other action options\n\
285 except for -o or -r option. It is a action option.\n\
286 If it is combined with other action options, the later\n\
287 input action option will override the previous one.\n");
288 fprintf (stdout
, " --address NewAddress Set new address into the first none code \n\
289 section header of the input image.\n\
290 It can't be combined with other action options\n\
291 except for -o or -r option. It is a action option.\n\
292 If it is combined with other action options, the later\n\
293 input action option will override the previous one.\n");
294 fprintf (stdout
, " -v, --verbose Turn on verbose output with informational messages.\n");
295 fprintf (stdout
, " -q, --quiet Disable all messages except key message and fatal error\n");
296 fprintf (stdout
, " -d, --debug level Enable debug messages, at input debug level.\n");
297 fprintf (stdout
, " --version Show program's version number and exit\n");
298 fprintf (stdout
, " -h, --help Show this help message and exit\n");
315 AcpiTable Buffer for AcpiSection
316 Length AcpiSection Length
325 EFI_ACPI_DESCRIPTION_HEADER
*AcpiHeader
;
326 EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*Facs
;
327 UINT32 ExpectedLength
;
329 AcpiHeader
= (EFI_ACPI_DESCRIPTION_HEADER
*)AcpiTable
;
332 // Generic check for AcpiTable length.
334 if (AcpiHeader
->Length
> Length
) {
335 Error (NULL
, 0, 3000, "Invalid", "AcpiTable length check failed.", NULL
);
340 // Currently, we only check must-have tables: FADT, FACS, DSDT,
341 // and some important tables: MADT, MCFG.
343 switch (AcpiHeader
->Signature
) {
346 // "FACP" Fixed ACPI Description Table
348 case EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_SIGNATURE
:
349 switch (AcpiHeader
->Revision
) {
350 case EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION
:
351 ExpectedLength
= sizeof(EFI_ACPI_1_0_FIXED_ACPI_DESCRIPTION_TABLE
);
353 case EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION
:
354 ExpectedLength
= sizeof(EFI_ACPI_2_0_FIXED_ACPI_DESCRIPTION_TABLE
);
356 case EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION
:
357 ExpectedLength
= sizeof(EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE
);
360 if (AcpiHeader
->Revision
> EFI_ACPI_3_0_FIXED_ACPI_DESCRIPTION_TABLE_REVISION
) {
361 ExpectedLength
= AcpiHeader
->Length
;
364 Error (NULL
, 0, 3000, "Invalid", "FACP revision check failed.");
367 if (ExpectedLength
!= AcpiHeader
->Length
) {
368 Error (NULL
, 0, 3000, "Invalid", "FACP length check failed.");
374 // "FACS" Firmware ACPI Control Structure
376 case EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_SIGNATURE
:
377 Facs
= (EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
*)AcpiTable
;
378 if (Facs
->Version
> EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) {
381 if ((Facs
->Version
!= EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) &&
382 (Facs
->Version
!= EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
) &&
383 (Facs
->Version
!= EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE_VERSION
)){
384 Error (NULL
, 0, 3000, "Invalid", "FACS version check failed.");
387 if ((Facs
->Length
!= sizeof(EFI_ACPI_1_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
)) &&
388 (Facs
->Length
!= sizeof(EFI_ACPI_2_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
)) &&
389 (Facs
->Length
!= sizeof(EFI_ACPI_3_0_FIRMWARE_ACPI_CONTROL_STRUCTURE
))) {
390 Error (NULL
, 0, 3000, "Invalid", "FACS length check failed.");
396 // "DSDT" Differentiated System Description Table
398 case EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_SIGNATURE
:
399 if (AcpiHeader
->Revision
> EFI_ACPI_3_0_DIFFERENTIATED_SYSTEM_DESCRIPTION_TABLE_REVISION
) {
402 if (AcpiHeader
->Length
<= sizeof(EFI_ACPI_DESCRIPTION_HEADER
)) {
403 Error (NULL
, 0, 3000, "Invalid", "DSDT length check failed.");
409 // "APIC" Multiple APIC Description Table
411 case EFI_ACPI_3_0_MULTIPLE_APIC_DESCRIPTION_TABLE_SIGNATURE
:
412 if (AcpiHeader
->Revision
> EFI_ACPI_3_0_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION
) {
415 if ((AcpiHeader
->Revision
!= EFI_ACPI_1_0_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION
) &&
416 (AcpiHeader
->Revision
!= EFI_ACPI_2_0_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION
) &&
417 (AcpiHeader
->Revision
!= EFI_ACPI_3_0_MULTIPLE_APIC_DESCRIPTION_TABLE_REVISION
)) {
418 Error (NULL
, 0, 3000, "Invalid", "APIC revision check failed.");
421 if (AcpiHeader
->Length
<= sizeof(EFI_ACPI_DESCRIPTION_HEADER
) + sizeof(UINT32
) + sizeof(UINT32
)) {
422 Error (NULL
, 0, 3000, "Invalid", "APIC length check failed.");
428 // "MCFG" PCI Express Memory Mapped Configuration Space Base Address Description Table
430 case EFI_ACPI_3_0_PCI_EXPRESS_MEMORY_MAPPED_CONFIGURATION_SPACE_BASE_ADDRESS_DESCRIPTION_TABLE_SIGNATURE
:
431 if (AcpiHeader
->Revision
> EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION
) {
434 if (AcpiHeader
->Revision
!= EFI_ACPI_MEMORY_MAPPED_CONFIGURATION_SPACE_ACCESS_TABLE_REVISION
) {
435 Error (NULL
, 0, 3000, "Invalid", "MCFG revision check failed.");
438 if (AcpiHeader
->Length
<= sizeof(EFI_ACPI_DESCRIPTION_HEADER
) + sizeof(UINT64
)) {
439 Error (NULL
, 0, 3000, "Invalid", "MCFG length check failed.");
445 // Other table pass check
451 return STATUS_SUCCESS
;
455 SetHiiResourceHeader (
461 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
462 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*ResourceDirectoryEntry
;
463 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
464 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
467 // Fill Resource section entry
469 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiBinData
);
470 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
471 for (Index
= 0; Index
< ResourceDirectory
->NumberOfNamedEntries
; Index
++) {
472 if (ResourceDirectoryEntry
->u1
.s
.NameIsString
) {
473 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (HiiBinData
+ ResourceDirectoryEntry
->u1
.s
.NameOffset
);
475 if (ResourceDirectoryString
->Length
== 3 &&
476 ResourceDirectoryString
->String
[0] == L
'H' &&
477 ResourceDirectoryString
->String
[1] == L
'I' &&
478 ResourceDirectoryString
->String
[2] == L
'I') {
480 // Resource Type "HII" found
482 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
484 // Move to next level - resource Name
486 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiBinData
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
487 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
489 if (ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
491 // Move to next level - resource Language
493 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiBinData
+ ResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
);
494 ResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (ResourceDirectory
+ 1);
499 // Now it ought to be resource Data and update its OffsetToData value
501 if (!ResourceDirectoryEntry
->u2
.s
.DataIsDirectory
) {
502 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (HiiBinData
+ ResourceDirectoryEntry
->u2
.OffsetToData
);
503 ResourceDataEntry
->OffsetToData
= ResourceDataEntry
->OffsetToData
+ OffsetToFile
;
508 ResourceDirectoryEntry
++;
514 EFI_IMAGE_OPTIONAL_HEADER_UNION
*
519 EFI_IMAGE_DOS_HEADER
*DosHdr
;
520 EFI_IMAGE_OPTIONAL_HEADER_UNION
*PeHdr
;
523 // Read the dos & pe hdrs of the image
525 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)Data
;
526 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
527 // NO DOS header, check for PE/COFF header
528 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(Data
);
529 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
534 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(((UINT8
*)Data
) + DosHdr
->e_lfanew
);
535 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
544 PeCoffConvertImageToXip (
549 EFI_IMAGE_OPTIONAL_HEADER_UNION
*PeHdr
;
550 EFI_IMAGE_OPTIONAL_HEADER_UNION
*NewPeHdr
;
551 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
552 UINTN TotalNecessaryFileSize
;
557 UINTN FirstSectionOffset
;
558 BOOLEAN ConversionNeeded
;
560 PeHdr
= GetPeCoffHeader ((void *) *FileBuffer
);
565 if (PeHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= PeHdr
->Pe32
.OptionalHeader
.FileAlignment
) {
567 // The only reason to expand zero fill sections is to make them compatible with XIP images.
568 // If SectionAlignment is not equal to FileAlginment then it is not an XIP type image.
574 // Calculate size of XIP file, and determine if the conversion is needed.
576 ConversionNeeded
= FALSE
;
578 FirstSectionOffset
= *FileLength
;
579 TotalNecessaryFileSize
= 0;
580 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
581 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
582 SectionSize
= MAX (SectionHeader
->Misc
.VirtualSize
, SectionHeader
->SizeOfRawData
);
583 TotalNecessaryFileSize
+= SectionSize
;
584 if (SectionSize
> 0) {
585 FirstSectionOffset
= MIN (FirstSectionOffset
, SectionHeader
->VirtualAddress
);
586 XipLength
= MAX (XipLength
, SectionHeader
->VirtualAddress
+ SectionSize
);
587 if (SectionHeader
->VirtualAddress
!= SectionHeader
->PointerToRawData
) {
588 ConversionNeeded
= TRUE
;
591 if (SectionHeader
->Misc
.VirtualSize
> SectionHeader
->SizeOfRawData
) {
592 ConversionNeeded
= TRUE
;
596 if (FirstSectionOffset
< PeHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
) {
598 // If one of the sections should be loaded to an offset overlapping with
599 // the executable header, then it cannot be made into an XIP image.
601 VerboseMsg ("PE/COFF conversion to XIP is impossible due to overlap");
602 VerboseMsg ("of section data with the executable header.");
606 if (FirstSectionOffset
== *FileLength
) {
608 // If we never found a section with a non-zero size, then we
609 // skip the conversion.
614 TotalNecessaryFileSize
+= FirstSectionOffset
;
616 if (!ConversionNeeded
) {
620 if (XipLength
> (2 * TotalNecessaryFileSize
)) {
621 VerboseMsg ("PE/COFF conversion to XIP appears to be larger than necessary.");
622 VerboseMsg ("The image linking process may have left unused memory ranges.");
625 if (PeHdr
->Pe32
.FileHeader
.PointerToSymbolTable
!= 0) {
627 // This field is obsolete and should be zero
629 PeHdr
->Pe32
.FileHeader
.PointerToSymbolTable
= 0;
633 // Allocate the extra space that we need to grow the image
635 XipFile
= malloc (XipLength
);
636 memset (XipFile
, 0, XipLength
);
639 // Copy the file headers
641 memcpy (XipFile
, *FileBuffer
, PeHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
);
643 NewPeHdr
= GetPeCoffHeader ((void *)XipFile
);
644 if (NewPeHdr
== NULL
) {
650 // Copy the section data over to the appropriate XIP offsets
652 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(NewPeHdr
->Pe32
.OptionalHeader
) + NewPeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
653 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
654 if (SectionHeader
->SizeOfRawData
> 0) {
656 XipFile
+ SectionHeader
->VirtualAddress
,
657 *FileBuffer
+ SectionHeader
->PointerToRawData
,
658 SectionHeader
->SizeOfRawData
662 // Make the size of raw data in section header alignment.
664 SectionHeader
->SizeOfRawData
= (SectionHeader
->Misc
.VirtualSize
+ PeHdr
->Pe32
.OptionalHeader
.FileAlignment
- 1) & (~(PeHdr
->Pe32
.OptionalHeader
.FileAlignment
- 1));
665 SectionHeader
->PointerToRawData
= SectionHeader
->VirtualAddress
;
669 *FileLength
= XipLength
;
670 *FileBuffer
= XipFile
;
674 CreateHiiResouceSectionHeader (
675 UINT32
*pSectionHeaderSize
,
682 Create COFF resource section header
686 pSectionHeaderSize - Pointer to section header size.
687 HiiDataSize - Size of the total HII data in section.
690 The created section header buffer.
694 UINT32 HiiSectionHeaderSize
;
695 UINT32 HiiSectionOffset
;
696 UINT8
*HiiSectionHeader
;
697 EFI_IMAGE_RESOURCE_DIRECTORY
*ResourceDirectory
;
698 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*TypeResourceDirectoryEntry
;
699 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*NameResourceDirectoryEntry
;
700 EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*LanguageResourceDirectoryEntry
;
701 EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*ResourceDirectoryString
;
702 EFI_IMAGE_RESOURCE_DATA_ENTRY
*ResourceDataEntry
;
705 // Calculate the total size for the resource header (include Type, Name and Language)
706 // then allocate memory for the resource header.
708 HiiSectionHeaderSize
= 3 * (sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
))
709 + 3 * (sizeof (UINT16
) + 3 * sizeof (CHAR16
))
710 + sizeof (EFI_IMAGE_RESOURCE_DATA_ENTRY
);
711 HiiSectionHeader
= malloc (HiiSectionHeaderSize
);
712 memset (HiiSectionHeader
, 0, HiiSectionHeaderSize
);
714 HiiSectionOffset
= 0;
718 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiSectionHeader
+ HiiSectionOffset
);
719 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
);
720 ResourceDirectory
->NumberOfNamedEntries
= 1;
721 TypeResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (HiiSectionHeader
+ HiiSectionOffset
);
722 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
);
723 TypeResourceDirectoryEntry
->u1
.s
.NameIsString
= 1;
724 TypeResourceDirectoryEntry
->u2
.s
.DataIsDirectory
= 1;
725 TypeResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
= HiiSectionOffset
;
729 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiSectionHeader
+ HiiSectionOffset
);
730 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
);
731 ResourceDirectory
->NumberOfNamedEntries
= 1;
732 NameResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (HiiSectionHeader
+ HiiSectionOffset
);
733 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
);
734 NameResourceDirectoryEntry
->u1
.s
.NameIsString
= 1;
735 NameResourceDirectoryEntry
->u2
.s
.DataIsDirectory
= 1;
736 NameResourceDirectoryEntry
->u2
.s
.OffsetToDirectory
= HiiSectionOffset
;
738 // Create Language entry
740 ResourceDirectory
= (EFI_IMAGE_RESOURCE_DIRECTORY
*) (HiiSectionHeader
+ HiiSectionOffset
);
741 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY
);
742 ResourceDirectory
->NumberOfNamedEntries
= 1;
743 LanguageResourceDirectoryEntry
= (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
*) (HiiSectionHeader
+ HiiSectionOffset
);
744 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY
);
745 LanguageResourceDirectoryEntry
->u1
.s
.NameIsString
= 1;
747 // Create string entry for Type
749 TypeResourceDirectoryEntry
->u1
.s
.NameOffset
= HiiSectionOffset
;
750 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (HiiSectionHeader
+ HiiSectionOffset
);
751 ResourceDirectoryString
->Length
= 3;
752 ResourceDirectoryString
->String
[0] = L
'H';
753 ResourceDirectoryString
->String
[1] = L
'I';
754 ResourceDirectoryString
->String
[2] = L
'I';
755 HiiSectionOffset
= HiiSectionOffset
+ sizeof (ResourceDirectoryString
->Length
) + ResourceDirectoryString
->Length
* sizeof (ResourceDirectoryString
->String
[0]);
757 // Create string entry for Name
759 NameResourceDirectoryEntry
->u1
.s
.NameOffset
= HiiSectionOffset
;
760 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (HiiSectionHeader
+ HiiSectionOffset
);
761 ResourceDirectoryString
->Length
= 3;
762 ResourceDirectoryString
->String
[0] = L
'E';
763 ResourceDirectoryString
->String
[1] = L
'F';
764 ResourceDirectoryString
->String
[2] = L
'I';
765 HiiSectionOffset
= HiiSectionOffset
+ sizeof (ResourceDirectoryString
->Length
) + ResourceDirectoryString
->Length
* sizeof (ResourceDirectoryString
->String
[0]);
767 // Create string entry for Language
769 LanguageResourceDirectoryEntry
->u1
.s
.NameOffset
= HiiSectionOffset
;
770 ResourceDirectoryString
= (EFI_IMAGE_RESOURCE_DIRECTORY_STRING
*) (HiiSectionHeader
+ HiiSectionOffset
);
771 ResourceDirectoryString
->Length
= 3;
772 ResourceDirectoryString
->String
[0] = L
'B';
773 ResourceDirectoryString
->String
[1] = L
'I';
774 ResourceDirectoryString
->String
[2] = L
'N';
775 HiiSectionOffset
= HiiSectionOffset
+ sizeof (ResourceDirectoryString
->Length
) + ResourceDirectoryString
->Length
* sizeof (ResourceDirectoryString
->String
[0]);
779 LanguageResourceDirectoryEntry
->u2
.OffsetToData
= HiiSectionOffset
;
780 ResourceDataEntry
= (EFI_IMAGE_RESOURCE_DATA_ENTRY
*) (HiiSectionHeader
+ HiiSectionOffset
);
781 HiiSectionOffset
+= sizeof (EFI_IMAGE_RESOURCE_DATA_ENTRY
);
782 ResourceDataEntry
->OffsetToData
= HiiSectionOffset
;
783 ResourceDataEntry
->Size
= HiiDataSize
;
785 *pSectionHeaderSize
= HiiSectionHeaderSize
;
786 return HiiSectionHeader
;
793 IN OUT UINT32
*ReadSize
,
800 Support routine for the PE/COFF Loader that reads a buffer from a PE/COFF file
804 FileHandle - The handle to the PE/COFF file
806 FileOffset - The offset, in bytes, into the file to read
808 ReadSize - The number of bytes to read from the file starting at FileOffset
810 Buffer - A pointer to the buffer to read the data into.
814 EFI_SUCCESS - ReadSize bytes of data were read into Buffer from the PE/COFF file starting at FileOffset
822 Destination8
= Buffer
;
823 Source8
= (CHAR8
*) ((UINTN
) FileHandle
+ FileOffset
);
826 *(Destination8
++) = *(Source8
++);
833 SetAddressToSectionHeader (
835 IN OUT UINT8
*FileBuffer
,
836 IN UINT64 NewPe32BaseAddress
842 Set new base address into the section header of PeImage
846 FileName - Name of file
847 FileBuffer - Pointer to PeImage.
848 NewPe32BaseAddress - New Base Address for PE image.
852 EFI_SUCCESS Set new base address into this image successfully.
857 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
859 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
860 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
863 // Initialize context
865 memset (&ImageContext
, 0, sizeof (ImageContext
));
866 ImageContext
.Handle
= (VOID
*) FileBuffer
;
867 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) RebaseImageRead
;
868 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
869 if (EFI_ERROR (Status
)) {
870 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName
);
874 if (ImageContext
.RelocationsStripped
) {
875 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName
);
880 // Get PeHeader pointer
882 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ ImageContext
.PeCoffHeaderOffset
);
885 // Get section header list
887 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
890 sizeof (EFI_IMAGE_FILE_HEADER
) +
891 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
895 // Set base address into the first section header that doesn't point to code section.
897 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
898 if ((SectionHeader
->Characteristics
& EFI_IMAGE_SCN_CNT_CODE
) == 0) {
899 *(UINT64
*) &SectionHeader
->PointerToRelocations
= NewPe32BaseAddress
;
905 // No available section header is found.
907 if (Index
== ImgHdr
->Pe32
.FileHeader
.NumberOfSections
) {
908 return EFI_NOT_FOUND
;
912 // BaseAddress is set to section header.
920 IN OUT UINT8
*FileBuffer
,
921 IN UINT64 NewPe32BaseAddress
927 Set new base address into PeImage, and fix up PeImage based on new address.
931 FileName - Name of file
932 FileBuffer - Pointer to PeImage.
933 NewPe32BaseAddress - New Base Address for PE image.
937 EFI_INVALID_PARAMETER - BaseAddress is not valid.
938 EFI_SUCCESS - Update PeImage is correctly.
943 PE_COFF_LOADER_IMAGE_CONTEXT ImageContext
;
945 EFI_IMAGE_OPTIONAL_HEADER_UNION
*ImgHdr
;
946 UINT8
*MemoryImagePointer
;
947 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
950 // Initialize context
952 memset (&ImageContext
, 0, sizeof (ImageContext
));
953 ImageContext
.Handle
= (VOID
*) FileBuffer
;
954 ImageContext
.ImageRead
= (PE_COFF_LOADER_READ_FILE
) RebaseImageRead
;
955 Status
= PeCoffLoaderGetImageInfo (&ImageContext
);
956 if (EFI_ERROR (Status
)) {
957 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s is not valid", FileName
);
961 if (ImageContext
.RelocationsStripped
) {
962 Error (NULL
, 0, 3000, "Invalid", "The input PeImage %s has no relocation to be fixed up", FileName
);
967 // Get PeHeader pointer
969 ImgHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ ImageContext
.PeCoffHeaderOffset
);
972 // Load and Relocate Image Data
974 MemoryImagePointer
= (UINT8
*) malloc ((UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
975 if (MemoryImagePointer
== NULL
) {
976 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName
);
977 return EFI_OUT_OF_RESOURCES
;
979 memset ((VOID
*) MemoryImagePointer
, 0, (UINTN
) ImageContext
.ImageSize
+ ImageContext
.SectionAlignment
);
980 ImageContext
.ImageAddress
= ((UINTN
) MemoryImagePointer
+ ImageContext
.SectionAlignment
- 1) & (~((INT64
)ImageContext
.SectionAlignment
- 1));
982 Status
= PeCoffLoaderLoadImage (&ImageContext
);
983 if (EFI_ERROR (Status
)) {
984 Error (NULL
, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName
);
985 free ((VOID
*) MemoryImagePointer
);
989 ImageContext
.DestinationAddress
= NewPe32BaseAddress
;
990 Status
= PeCoffLoaderRelocateImage (&ImageContext
);
991 if (EFI_ERROR (Status
)) {
992 Error (NULL
, 0, 3000, "Invalid", "RelocateImage() call failed on rebase of %s", FileName
);
993 free ((VOID
*) MemoryImagePointer
);
998 // Copy Relocated data to raw image file.
1000 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (
1003 sizeof (EFI_IMAGE_FILE_HEADER
) +
1004 ImgHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
1007 for (Index
= 0; Index
< ImgHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
1009 FileBuffer
+ SectionHeader
->PointerToRawData
,
1010 (VOID
*) (UINTN
) (ImageContext
.ImageAddress
+ SectionHeader
->VirtualAddress
),
1011 SectionHeader
->SizeOfRawData
1015 free ((VOID
*) MemoryImagePointer
);
1018 // Update Image Base Address
1020 if ((ImgHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) && (ImgHdr
->Pe32
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_IA64
)) {
1021 ImgHdr
->Pe32
.OptionalHeader
.ImageBase
= (UINT32
) NewPe32BaseAddress
;
1022 } else if (ImgHdr
->Pe32Plus
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
1023 ImgHdr
->Pe32Plus
.OptionalHeader
.ImageBase
= NewPe32BaseAddress
;
1025 Error (NULL
, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",
1026 ImgHdr
->Pe32
.OptionalHeader
.Magic
,
1033 // Set new base address into section header
1035 Status
= SetAddressToSectionHeader (FileName
, FileBuffer
, NewPe32BaseAddress
);
1047 Routine Description:
1053 argc - Number of command line parameters.
1054 argv - Array of pointers to command line parameter strings.
1057 STATUS_SUCCESS - Utility exits successfully.
1058 STATUS_ERROR - Some error occurred during execution.
1063 UINT32 InputFileNum
;
1064 CHAR8
**InputFileName
;
1072 UINT32
*DataPointer
;
1073 UINT32
*OldDataPointer
;
1079 UINT32 MciAlignment
;
1081 UINT32 AllignedRelocSize
;
1084 UINT8
*OutputFileBuffer
;
1085 UINT32 OutputFileLength
;
1086 UINT8
*InputFileBuffer
;
1087 UINT32 InputFileLength
;
1088 RUNTIME_FUNCTION
*RuntimeFunction
;
1089 UNWIND_INFO
*UnwindInfo
;
1091 BOOLEAN ReplaceFlag
;
1092 BOOLEAN KeepExceptionTableFlag
;
1093 BOOLEAN KeepZeroPendingFlag
;
1095 EFI_TE_IMAGE_HEADER TEImageHeader
;
1096 EFI_TE_IMAGE_HEADER
*TeHdr
;
1097 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
1098 EFI_IMAGE_DOS_HEADER
*DosHdr
;
1099 EFI_IMAGE_OPTIONAL_HEADER_UNION
*PeHdr
;
1100 EFI_IMAGE_OPTIONAL_HEADER32
*Optional32
;
1101 EFI_IMAGE_OPTIONAL_HEADER64
*Optional64
;
1102 EFI_IMAGE_DOS_HEADER BackupDosHdr
;
1103 MICROCODE_IMAGE_HEADER
*MciHeader
;
1104 UINT8
*HiiPackageListBuffer
;
1105 UINT8
*HiiPackageDataPointer
;
1106 EFI_GUID HiiPackageListGuid
;
1107 EFI_HII_PACKAGE_LIST_HEADER HiiPackageListHeader
;
1108 EFI_HII_PACKAGE_HEADER HiiPackageHeader
;
1109 EFI_IFR_FORM_SET IfrFormSet
;
1110 UINT8 NumberOfFormPacakge
;
1111 EFI_HII_PACKAGE_HEADER EndPackage
;
1112 UINT32 HiiSectionHeaderSize
;
1113 UINT8
*HiiSectionHeader
;
1114 UINT64 NewBaseAddress
;
1115 BOOLEAN NegativeAddr
;
1117 CHAR8
*ReportFileName
;
1119 time_t InputFileTime
;
1120 time_t OutputFileTime
;
1121 struct stat Stat_Buf
;
1123 SetUtilityName (UTILITY_NAME
);
1126 // Assign to fix compile warning
1130 InputFileName
= NULL
;
1131 mInImageName
= NULL
;
1132 OutImageName
= NULL
;
1135 Status
= STATUS_SUCCESS
;
1141 MciAlignment
= DEFAULT_MC_ALIGNMENT
;
1142 MciPadValue
= DEFAULT_MC_PAD_BYTE_VALUE
;
1146 ReplaceFlag
= FALSE
;
1148 OutputFileBuffer
= NULL
;
1149 OutputFileLength
= 0;
1150 InputFileBuffer
= NULL
;
1151 InputFileLength
= 0;
1154 KeepExceptionTableFlag
= FALSE
;
1155 KeepZeroPendingFlag
= FALSE
;
1156 NumberOfFormPacakge
= 0;
1157 HiiPackageListBuffer
= NULL
;
1158 HiiPackageDataPointer
= NULL
;
1159 EndPackage
.Length
= sizeof (EFI_HII_PACKAGE_HEADER
);
1160 EndPackage
.Type
= EFI_HII_PACKAGE_END
;
1161 memset (&HiiPackageListGuid
, 0, sizeof (HiiPackageListGuid
));
1162 HiiSectionHeaderSize
= 0;
1163 HiiSectionHeader
= NULL
;
1165 NegativeAddr
= FALSE
;
1170 Error (NULL
, 0, 1001, "Missing options", "No input options.");
1172 return STATUS_ERROR
;
1178 if ((stricmp (argv
[0], "-h") == 0) || (stricmp (argv
[0], "--help") == 0)) {
1181 return STATUS_SUCCESS
;
1184 if (stricmp (argv
[0], "--version") == 0) {
1186 return STATUS_SUCCESS
;
1190 if ((stricmp (argv
[0], "-o") == 0) || (stricmp (argv
[0], "--outputfile") == 0)) {
1191 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
1192 Error (NULL
, 0, 1003, "Invalid option value", "Output file name is missing for -o option");
1195 OutImageName
= argv
[1];
1201 if ((stricmp (argv
[0], "-e") == 0) || (stricmp (argv
[0], "--efiImage") == 0)) {
1202 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
1203 Error (NULL
, 0, 1003, "Invalid option value", "Module Type is missing for -o option");
1206 ModuleType
= argv
[1];
1207 if (mOutImageType
!= FW_TE_IMAGE
) {
1208 mOutImageType
= FW_EFI_IMAGE
;
1215 if ((stricmp (argv
[0], "-l") == 0) || (stricmp (argv
[0], "--stripped") == 0)) {
1216 mOutImageType
= FW_RELOC_STRIPEED_IMAGE
;
1222 if ((stricmp (argv
[0], "-c") == 0) || (stricmp (argv
[0], "--acpi") == 0)) {
1223 mOutImageType
= FW_ACPI_IMAGE
;
1229 if ((stricmp (argv
[0], "-t") == 0) || (stricmp (argv
[0], "--terse") == 0)) {
1230 mOutImageType
= FW_TE_IMAGE
;
1236 if ((stricmp (argv
[0], "-u") == 0) || (stricmp (argv
[0], "--dump") == 0)) {
1237 mOutImageType
= DUMP_TE_HEADER
;
1243 if ((stricmp (argv
[0], "-b") == 0) || (stricmp (argv
[0], "--exe2bin") == 0)) {
1244 mOutImageType
= FW_BIN_IMAGE
;
1250 if ((stricmp (argv
[0], "-z") == 0) || (stricmp (argv
[0], "--zero") == 0)) {
1251 mOutImageType
= FW_ZERO_DEBUG_IMAGE
;
1257 if ((stricmp (argv
[0], "-s") == 0) || (stricmp (argv
[0], "--stamp") == 0)) {
1258 mOutImageType
= FW_SET_STAMP_IMAGE
;
1259 if (argv
[1] == NULL
|| argv
[1][0] == '-') {
1260 Error (NULL
, 0, 1003, "Invalid option value", "time stamp is missing for -s option");
1263 TimeStamp
= argv
[1];
1269 if ((stricmp (argv
[0], "-r") == 0) || (stricmp (argv
[0], "--replace") == 0)) {
1276 if (stricmp (argv
[0], "--keepexceptiontable") == 0) {
1277 KeepExceptionTableFlag
= TRUE
;
1283 if (stricmp (argv
[0], "--keepzeropending") == 0) {
1284 KeepZeroPendingFlag
= TRUE
;
1290 if ((stricmp (argv
[0], "-m") == 0) || (stricmp (argv
[0], "--mcifile") == 0)) {
1291 mOutImageType
= FW_MCI_IMAGE
;
1297 if ((stricmp (argv
[0], "-j") == 0) || (stricmp (argv
[0], "--join") == 0)) {
1298 mOutImageType
= FW_MERGE_IMAGE
;
1304 if ((stricmp (argv
[0], "-a") == 0) || (stricmp (argv
[0], "--align") == 0)) {
1305 if (AsciiStringToUint64 (argv
[1], FALSE
, &Temp64
) != EFI_SUCCESS
) {
1306 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1309 MciAlignment
= (UINT32
) Temp64
;
1315 if ((stricmp (argv
[0], "--rebase") == 0)) {
1316 if (argv
[1][0] == '-') {
1317 NegativeAddr
= TRUE
;
1318 Status
= AsciiStringToUint64 (argv
[1] + 1, FALSE
, &Temp64
);
1320 NegativeAddr
= FALSE
;
1321 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &Temp64
);
1323 if (Status
!= EFI_SUCCESS
) {
1324 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1327 mOutImageType
= FW_REBASE_IMAGE
;
1328 NewBaseAddress
= (UINT64
) Temp64
;
1334 if ((stricmp (argv
[0], "--address") == 0)) {
1335 if (argv
[1][0] == '-') {
1336 NegativeAddr
= TRUE
;
1337 Status
= AsciiStringToUint64 (argv
[1] + 1, FALSE
, &Temp64
);
1339 NegativeAddr
= FALSE
;
1340 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &Temp64
);
1342 if (Status
!= EFI_SUCCESS
) {
1343 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1346 mOutImageType
= FW_SET_ADDRESS_IMAGE
;
1347 NewBaseAddress
= (UINT64
) Temp64
;
1353 if ((stricmp (argv
[0], "-p") == 0) || (stricmp (argv
[0], "--pad") == 0)) {
1354 if (AsciiStringToUint64 (argv
[1], FALSE
, &Temp64
) != EFI_SUCCESS
) {
1355 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1358 MciPadValue
= (UINT8
) Temp64
;
1364 if ((stricmp (argv
[0], "-v") == 0) || (stricmp (argv
[0], "--verbose") == 0)) {
1365 SetPrintLevel (VERBOSE_LOG_LEVEL
);
1366 VerboseMsg ("Verbose output Mode Set!");
1372 if ((stricmp (argv
[0], "-q") == 0) || (stricmp (argv
[0], "--quiet") == 0)) {
1373 SetPrintLevel (KEY_LOG_LEVEL
);
1374 KeyMsg ("Quiet output Mode Set!");
1380 if ((stricmp (argv
[0], "-d") == 0) || (stricmp (argv
[0], "--debug") == 0)) {
1381 Status
= AsciiStringToUint64 (argv
[1], FALSE
, &LogLevel
);
1382 if (EFI_ERROR (Status
)) {
1383 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1387 Error (NULL
, 0, 1003, "Invalid option value", "Debug Level range is 0-9, currnt input level is %d", (int) LogLevel
);
1390 SetPrintLevel (LogLevel
);
1391 DebugMsg (NULL
, 0, 9, "Debug Mode Set", "Debug Output Mode Level %s is set!", argv
[1]);
1397 if ((stricmp (argv
[0], "-g") == 0) || (stricmp (argv
[0], "--hiiguid") == 0)) {
1398 Status
= StringToGuid (argv
[1], &HiiPackageListGuid
);
1399 if (EFI_ERROR (Status
)) {
1400 Error (NULL
, 0, 1003, "Invalid option value", "%s = %s", argv
[0], argv
[1]);
1408 if (stricmp (argv
[0], "--hiipackage") == 0) {
1409 mOutImageType
= FW_HII_PACKAGE_LIST_RCIMAGE
;
1415 if (stricmp (argv
[0], "--hiibinpackage") == 0) {
1416 mOutImageType
= FW_HII_PACKAGE_LIST_BINIMAGE
;
1422 if (argv
[0][0] == '-') {
1423 Error (NULL
, 0, 1000, "Unknown option", argv
[0]);
1427 // Get Input file name
1429 if ((InputFileNum
== 0) && (InputFileName
== NULL
)) {
1430 InputFileName
= (CHAR8
**) malloc (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*));
1431 if (InputFileName
== NULL
) {
1432 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1436 memset (InputFileName
, 0, (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*)));
1437 } else if (InputFileNum
% MAXIMUM_INPUT_FILE_NUM
== 0) {
1439 // InputFileName buffer too small, need to realloc
1441 InputFileName
= (CHAR8
**) realloc (
1443 (InputFileNum
+ MAXIMUM_INPUT_FILE_NUM
) * sizeof (CHAR8
*)
1446 if (InputFileName
== NULL
) {
1447 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1451 memset (&(InputFileName
[InputFileNum
]), 0, (MAXIMUM_INPUT_FILE_NUM
* sizeof (CHAR8
*)));
1454 InputFileName
[InputFileNum
++] = argv
[0];
1459 VerboseMsg ("%s tool start.", UTILITY_NAME
);
1461 if (mOutImageType
== FW_DUMMY_IMAGE
) {
1462 Error (NULL
, 0, 1001, "Missing option", "No create file action specified; pls specify -e, -c or -t option to create efi image, or acpi table or TeImage!");
1464 Error (NULL
, 0, 1001, "Missing option", "-r option is not supported as the independent option. It can be used together with other create file option specified at the above.");
1470 // check input files
1472 if (InputFileNum
== 0) {
1473 Error (NULL
, 0, 1001, "Missing option", "Input files");
1478 // Combine MciBinary files to one file
1480 if ((mOutImageType
== FW_MERGE_IMAGE
) && ReplaceFlag
) {
1481 Error (NULL
, 0, 1002, "Conflicting option", "-r replace option cannot be used with -j merge files option.");
1486 // Combine HiiBinary packages to a single package list
1488 if ((mOutImageType
== FW_HII_PACKAGE_LIST_RCIMAGE
) && ReplaceFlag
) {
1489 Error (NULL
, 0, 1002, "Conflicting option", "-r replace option cannot be used with --hiipackage merge files option.");
1493 if ((mOutImageType
== FW_HII_PACKAGE_LIST_BINIMAGE
) && ReplaceFlag
) {
1494 Error (NULL
, 0, 1002, "Conflicting option", "-r replace option cannot be used with --hiibinpackage merge files option.");
1501 mInImageName
= InputFileName
[InputFileNum
- 1];
1502 VerboseMsg ("the input file name is %s", mInImageName
);
1505 // Action will be taken for the input file.
1507 switch (mOutImageType
) {
1509 VerboseMsg ("Create efi image on module type %s based on the input PE image.", ModuleType
);
1512 VerboseMsg ("Create Te Image based on the input PE image.");
1515 VerboseMsg ("Get acpi table data from the input PE image.");
1517 case FW_RELOC_STRIPEED_IMAGE
:
1518 VerboseMsg ("Remove relocation section from Pe or Te image.");
1521 VerboseMsg ("Convert the input EXE to the output BIN file.");
1523 case FW_ZERO_DEBUG_IMAGE
:
1524 VerboseMsg ("Zero the Debug Data Fields and Time Stamp in input PE image.");
1526 case FW_SET_STAMP_IMAGE
:
1527 VerboseMsg ("Set new time stamp %s in the input PE image.", TimeStamp
);
1529 case DUMP_TE_HEADER
:
1530 VerboseMsg ("Dump the TE header information of the input TE image.");
1533 VerboseMsg ("Conver input MicroCode.txt file to MicroCode.bin file.");
1535 case FW_MERGE_IMAGE
:
1536 VerboseMsg ("Combine the input multi microcode bin files to one bin file.");
1538 case FW_HII_PACKAGE_LIST_RCIMAGE
:
1539 VerboseMsg ("Combine the input multi hii bin packages to one text pacakge list RC file.");
1541 case FW_HII_PACKAGE_LIST_BINIMAGE
:
1542 VerboseMsg ("Combine the input multi hii bin packages to one binary pacakge list file.");
1544 case FW_REBASE_IMAGE
:
1545 VerboseMsg ("Rebase the input image to new base address.");
1547 case FW_SET_ADDRESS_IMAGE
:
1548 VerboseMsg ("Set the preferred address into the section header of the input image");
1555 VerboseMsg ("Overwrite the input file with the output content.");
1559 // Open output file and Write image into the output file.
1561 if (OutImageName
!= NULL
) {
1562 fpOut
= fopen (OutImageName
, "rb");
1563 if (fpOut
!= NULL
) {
1565 // Get Output file time stamp
1567 fstat(fileno (fpOut
), &Stat_Buf
);
1568 OutputFileTime
= Stat_Buf
.st_mtime
;
1570 // Get Output file data
1572 OutputFileLength
= _filelength (fileno (fpOut
));
1573 OutputFileBuffer
= malloc (OutputFileLength
);
1574 if (OutputFileBuffer
== NULL
) {
1575 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1580 fread (OutputFileBuffer
, 1, OutputFileLength
, fpOut
);
1584 VerboseMsg ("Output file name is %s", OutImageName
);
1585 } else if (!ReplaceFlag
&& mOutImageType
!= DUMP_TE_HEADER
) {
1586 Error (NULL
, 0, 1001, "Missing option", "output file");
1591 // Open input file and read file data into file buffer.
1593 fpIn
= fopen (mInImageName
, "rb");
1595 Error (NULL
, 0, 0001, "Error opening file", mInImageName
);
1599 // Get Iutput file time stamp
1601 fstat(fileno (fpIn
), &Stat_Buf
);
1602 InputFileTime
= Stat_Buf
.st_mtime
;
1604 // Get Input file data
1606 InputFileLength
= _filelength (fileno (fpIn
));
1607 InputFileBuffer
= malloc (InputFileLength
);
1608 if (InputFileBuffer
== NULL
) {
1609 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1613 fread (InputFileBuffer
, 1, InputFileLength
, fpIn
);
1615 DebugMsg (NULL
, 0, 9, "input file info", "the input file size is %u bytes", (unsigned) InputFileLength
);
1618 // Combine multi binary HII package files.
1620 if (mOutImageType
== FW_HII_PACKAGE_LIST_RCIMAGE
|| mOutImageType
== FW_HII_PACKAGE_LIST_BINIMAGE
) {
1622 // Open output file handle.
1624 fpOut
= fopen (OutImageName
, "wb");
1626 Error (NULL
, 0, 0001, "Error opening output file", OutImageName
);
1630 // Get hii package list lenght
1632 HiiPackageListHeader
.PackageLength
= sizeof (EFI_HII_PACKAGE_LIST_HEADER
);
1633 for (Index
= 0; Index
< InputFileNum
; Index
++) {
1634 fpIn
= fopen (InputFileName
[Index
], "rb");
1636 Error (NULL
, 0, 0001, "Error opening file", InputFileName
[Index
]);
1639 FileLength
= _filelength (fileno (fpIn
));
1640 fread (&HiiPackageHeader
, 1, sizeof (HiiPackageHeader
), fpIn
);
1641 if (HiiPackageHeader
.Type
== EFI_HII_PACKAGE_FORM
) {
1642 if (HiiPackageHeader
.Length
!= FileLength
) {
1643 Error (NULL
, 0, 3000, "Invalid", "The wrong package size is in HII package file %s", InputFileName
[Index
]);
1647 if (memcmp (&HiiPackageListGuid
, &mZeroGuid
, sizeof (EFI_GUID
)) == 0) {
1648 fread (&IfrFormSet
, 1, sizeof (IfrFormSet
), fpIn
);
1649 memcpy (&HiiPackageListGuid
, &IfrFormSet
.Guid
, sizeof (EFI_GUID
));
1651 NumberOfFormPacakge
++;
1653 HiiPackageListHeader
.PackageLength
+= FileLength
;
1656 HiiPackageListHeader
.PackageLength
+= sizeof (EndPackage
);
1658 // Check whether hii packages are valid
1660 if (NumberOfFormPacakge
> 1) {
1661 Error (NULL
, 0, 3000, "Invalid", "The input hii packages contains more than one hii form package");
1664 if (memcmp (&HiiPackageListGuid
, &mZeroGuid
, sizeof (EFI_GUID
)) == 0) {
1665 Error (NULL
, 0, 3000, "Invalid", "HII pacakge list guid is not specified!");
1668 memcpy (&HiiPackageListHeader
.PackageListGuid
, &HiiPackageListGuid
, sizeof (EFI_GUID
));
1670 // read hii packages
1672 HiiPackageListBuffer
= malloc (HiiPackageListHeader
.PackageLength
);
1673 if (HiiPackageListBuffer
== NULL
) {
1674 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1677 memcpy (HiiPackageListBuffer
, &HiiPackageListHeader
, sizeof (HiiPackageListHeader
));
1678 HiiPackageDataPointer
= HiiPackageListBuffer
+ sizeof (HiiPackageListHeader
);
1679 for (Index
= 0; Index
< InputFileNum
; Index
++) {
1680 fpIn
= fopen (InputFileName
[Index
], "rb");
1682 Error (NULL
, 0, 0001, "Error opening file", InputFileName
[Index
]);
1683 free (HiiPackageListBuffer
);
1687 FileLength
= _filelength (fileno (fpIn
));
1688 fread (HiiPackageDataPointer
, 1, FileLength
, fpIn
);
1690 HiiPackageDataPointer
= HiiPackageDataPointer
+ FileLength
;
1692 memcpy (HiiPackageDataPointer
, &EndPackage
, sizeof (EndPackage
));
1695 // write the hii package into the binary package list file with the resource section header
1697 if (mOutImageType
== FW_HII_PACKAGE_LIST_BINIMAGE
) {
1699 // Create the resource section header
1701 HiiSectionHeader
= CreateHiiResouceSectionHeader (&HiiSectionHeaderSize
, HiiPackageListHeader
.PackageLength
);
1703 // Wrtie section header and HiiData into File.
1705 fwrite (HiiSectionHeader
, 1, HiiSectionHeaderSize
, fpOut
);
1706 fwrite (HiiPackageListBuffer
, 1, HiiPackageListHeader
.PackageLength
, fpOut
);
1708 // Free allocated resources.
1710 free (HiiSectionHeader
);
1711 free (HiiPackageListBuffer
);
1713 // Done successfully
1719 // write the hii package into the text package list rc file.
1721 if (mOutImageType
== FW_HII_PACKAGE_LIST_RCIMAGE
) {
1722 for (Index
= 0; gHiiPackageRCFileHeader
[Index
] != NULL
; Index
++) {
1723 fprintf (fpOut
, "%s\n", gHiiPackageRCFileHeader
[Index
]);
1725 fprintf (fpOut
, "\n%d %s\n{", HII_RESOURCE_SECTION_INDEX
, HII_RESOURCE_SECTION_NAME
);
1727 HiiPackageDataPointer
= HiiPackageListBuffer
;
1728 for (Index
= 0; Index
+ 2 < HiiPackageListHeader
.PackageLength
; Index
+= 2) {
1729 if (Index
% 16 == 0) {
1730 fprintf (fpOut
, "\n ");
1732 fprintf (fpOut
, " 0x%04X,", *(UINT16
*) HiiPackageDataPointer
);
1733 HiiPackageDataPointer
+= 2;
1736 if (Index
% 16 == 0) {
1737 fprintf (fpOut
, "\n ");
1739 if ((Index
+ 2) == HiiPackageListHeader
.PackageLength
) {
1740 fprintf (fpOut
, " 0x%04X\n}\n", *(UINT16
*) HiiPackageDataPointer
);
1742 if ((Index
+ 1) == HiiPackageListHeader
.PackageLength
) {
1743 fprintf (fpOut
, " 0x%04X\n}\n", *(UINT8
*) HiiPackageDataPointer
);
1745 free (HiiPackageListBuffer
);
1747 // Done successfully
1754 // Combine MciBinary files to one file
1756 if (mOutImageType
== FW_MERGE_IMAGE
) {
1758 // Open output file handle.
1760 fpOut
= fopen (OutImageName
, "wb");
1762 Error (NULL
, 0, 0001, "Error opening output file", OutImageName
);
1765 for (Index
= 0; Index
< InputFileNum
; Index
++) {
1766 fpIn
= fopen (InputFileName
[Index
], "rb");
1768 Error (NULL
, 0, 0001, "Error opening file", InputFileName
[Index
]);
1772 FileLength
= _filelength (fileno (fpIn
));
1773 FileBuffer
= malloc (FileLength
);
1774 if (FileBuffer
== NULL
) {
1775 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1780 fread (FileBuffer
, 1, FileLength
, fpIn
);
1783 // write input file to out file
1785 fwrite (FileBuffer
, 1, FileLength
, fpOut
);
1787 // write pad value to out file.
1789 while (FileLength
++ % MciAlignment
!= 0) {
1790 fwrite (&MciPadValue
, 1, 1, fpOut
);
1793 // free allocated memory space
1799 // Done successfully
1805 // Convert MicroCode.txt file to MicroCode.bin file
1807 if (mOutImageType
== FW_MCI_IMAGE
) {
1808 fpIn
= fopen (mInImageName
, "r");
1810 Error (NULL
, 0, 0001, "Error opening file", mInImageName
);
1815 // The first pass is to determine
1816 // how much data is in the file so we can allocate a working buffer.
1820 Status
= MicrocodeReadData (fpIn
, &Data
);
1821 if (Status
== STATUS_SUCCESS
) {
1822 FileLength
+= sizeof (Data
);
1824 if (Status
== STATUS_IGNORE
) {
1825 Status
= STATUS_SUCCESS
;
1827 } while (Status
== STATUS_SUCCESS
);
1829 // Error if no data.
1831 if (FileLength
== 0) {
1832 Error (NULL
, 0, 3000, "Invalid", "no parseable data found in file %s", mInImageName
);
1835 if (FileLength
< sizeof (MICROCODE_IMAGE_HEADER
)) {
1836 Error (NULL
, 0, 3000, "Invalid", "amount of parseable data in %s is insufficient to contain a microcode header", mInImageName
);
1841 // Allocate a buffer for the data
1843 FileBuffer
= malloc (FileLength
);
1844 if (FileBuffer
== NULL
) {
1845 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1849 // Re-read the file, storing the data into our buffer
1851 fseek (fpIn
, 0, SEEK_SET
);
1852 DataPointer
= (UINT32
*) FileBuffer
;
1853 OldDataPointer
= DataPointer
;
1855 OldDataPointer
= DataPointer
;
1856 Status
= MicrocodeReadData (fpIn
, DataPointer
++);
1857 if (Status
== STATUS_IGNORE
) {
1858 DataPointer
= OldDataPointer
;
1859 Status
= STATUS_SUCCESS
;
1861 } while (Status
== STATUS_SUCCESS
);
1863 // close input file after read data
1868 // Can't do much checking on the header because, per the spec, the
1869 // DataSize field may be 0, which means DataSize = 2000 and TotalSize = 2K,
1870 // and the TotalSize field is invalid (actually missing). Thus we can't
1871 // even verify the Reserved fields are 0.
1873 MciHeader
= (MICROCODE_IMAGE_HEADER
*) FileBuffer
;
1874 if (MciHeader
->DataSize
== 0) {
1877 Index
= MciHeader
->TotalSize
;
1880 if (Index
!= FileLength
) {
1881 Error (NULL
, 0, 3000, "Invalid", "file length of %s (0x%x) does not equal expected TotalSize: 0x%04X.", mInImageName
, (unsigned) FileLength
, (unsigned) Index
);
1886 // Checksum the contents
1888 DataPointer
= (UINT32
*) FileBuffer
;
1891 while (Index
< FileLength
) {
1892 CheckSum
+= *DataPointer
;
1894 Index
+= sizeof (*DataPointer
);
1896 if (CheckSum
!= 0) {
1897 Error (NULL
, 0, 3000, "Invalid", "checksum (0x%x) failed on file %s.", (unsigned) CheckSum
, mInImageName
);
1901 // Open the output file and write the buffer contents
1903 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
1908 // Open input file and read file data into file buffer.
1910 FileLength
= InputFileLength
;
1911 FileBuffer
= malloc (FileLength
);
1912 if (FileBuffer
== NULL
) {
1913 Error (NULL
, 0, 4001, "Resource", "memory cannot be allocated!");
1916 memcpy (FileBuffer
, InputFileBuffer
, InputFileLength
);
1919 // Dump TeImage Header into output file.
1921 if (mOutImageType
== DUMP_TE_HEADER
) {
1922 memcpy (&TEImageHeader
, FileBuffer
, sizeof (TEImageHeader
));
1923 if (TEImageHeader
.Signature
!= EFI_TE_IMAGE_HEADER_SIGNATURE
) {
1924 Error (NULL
, 0, 3000, "Invalid", "TE header signature of file %s is not correct.", mInImageName
);
1928 // Open the output file handle.
1931 fpInOut
= fopen (mInImageName
, "wb");
1932 if (fpInOut
== NULL
) {
1933 Error (NULL
, 0, 0001, "Error opening file", mInImageName
);
1937 if (OutImageName
!= NULL
) {
1938 fpOut
= fopen (OutImageName
, "wb");
1942 if (fpOut
== NULL
) {
1943 Error (NULL
, 0, 0001, "Error opening output file", OutImageName
);
1947 if (fpInOut
!= NULL
) {
1948 fprintf (fpInOut
, "Dump of file %s\n\n", mInImageName
);
1949 fprintf (fpInOut
, "TE IMAGE HEADER VALUES\n");
1950 fprintf (fpInOut
, "%17X machine\n", TEImageHeader
.Machine
);
1951 fprintf (fpInOut
, "%17X number of sections\n", TEImageHeader
.NumberOfSections
);
1952 fprintf (fpInOut
, "%17X subsystems\n", TEImageHeader
.Subsystem
);
1953 fprintf (fpInOut
, "%17X stripped size\n", TEImageHeader
.StrippedSize
);
1954 fprintf (fpInOut
, "%17X entry point\n", (unsigned) TEImageHeader
.AddressOfEntryPoint
);
1955 fprintf (fpInOut
, "%17X base of code\n", (unsigned) TEImageHeader
.BaseOfCode
);
1956 fprintf (fpInOut
, "%17llX image base\n", (unsigned long long)TEImageHeader
.ImageBase
);
1957 fprintf (fpInOut
, "%17X [%8X] RVA [size] of Base Relocation Directory\n", (unsigned) TEImageHeader
.DataDirectory
[0].VirtualAddress
, (unsigned) TEImageHeader
.DataDirectory
[0].Size
);
1958 fprintf (fpInOut
, "%17X [%8X] RVA [size] of Debug Directory\n", (unsigned) TEImageHeader
.DataDirectory
[1].VirtualAddress
, (unsigned) TEImageHeader
.DataDirectory
[1].Size
);
1960 if (fpOut
!= NULL
) {
1961 fprintf (fpOut
, "Dump of file %s\n\n", mInImageName
);
1962 fprintf (fpOut
, "TE IMAGE HEADER VALUES\n");
1963 fprintf (fpOut
, "%17X machine\n", TEImageHeader
.Machine
);
1964 fprintf (fpOut
, "%17X number of sections\n", TEImageHeader
.NumberOfSections
);
1965 fprintf (fpOut
, "%17X subsystems\n", TEImageHeader
.Subsystem
);
1966 fprintf (fpOut
, "%17X stripped size\n", TEImageHeader
.StrippedSize
);
1967 fprintf (fpOut
, "%17X entry point\n", (unsigned) TEImageHeader
.AddressOfEntryPoint
);
1968 fprintf (fpOut
, "%17X base of code\n", (unsigned) TEImageHeader
.BaseOfCode
);
1969 fprintf (fpOut
, "%17llX image base\n", (unsigned long long)TEImageHeader
.ImageBase
);
1970 fprintf (fpOut
, "%17X [%8X] RVA [size] of Base Relocation Directory\n", (unsigned) TEImageHeader
.DataDirectory
[0].VirtualAddress
, (unsigned) TEImageHeader
.DataDirectory
[0].Size
);
1971 fprintf (fpOut
, "%17X [%8X] RVA [size] of Debug Directory\n", (unsigned) TEImageHeader
.DataDirectory
[1].VirtualAddress
, (unsigned) TEImageHeader
.DataDirectory
[1].Size
);
1977 // Following code to convert dll to efi image or te image.
1978 // Get new image type
1980 if ((mOutImageType
== FW_EFI_IMAGE
) || (mOutImageType
== FW_TE_IMAGE
)) {
1981 if (ModuleType
== NULL
) {
1982 if (mOutImageType
== FW_EFI_IMAGE
) {
1983 Error (NULL
, 0, 1001, "Missing option", "EFI_FILETYPE");
1985 } else if (mOutImageType
== FW_TE_IMAGE
) {
1987 // Default TE Image Type is Boot service driver
1989 Type
= EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
1990 VerboseMsg ("Efi Image subsystem type is efi boot service driver.");
1993 if (stricmp (ModuleType
, "BASE") == 0 ||
1994 stricmp (ModuleType
, "SEC") == 0 ||
1995 stricmp (ModuleType
, "SECURITY_CORE") == 0 ||
1996 stricmp (ModuleType
, "PEI_CORE") == 0 ||
1997 stricmp (ModuleType
, "PEIM") == 0 ||
1998 stricmp (ModuleType
, "COMBINED_PEIM_DRIVER") == 0 ||
1999 stricmp (ModuleType
, "PIC_PEIM") == 0 ||
2000 stricmp (ModuleType
, "RELOCATABLE_PEIM") == 0 ||
2001 stricmp (ModuleType
, "DXE_CORE") == 0 ||
2002 stricmp (ModuleType
, "BS_DRIVER") == 0 ||
2003 stricmp (ModuleType
, "DXE_DRIVER") == 0 ||
2004 stricmp (ModuleType
, "DXE_SMM_DRIVER") == 0 ||
2005 stricmp (ModuleType
, "UEFI_DRIVER") == 0 ||
2006 stricmp (ModuleType
, "SMM_CORE") == 0) {
2007 Type
= EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
2008 VerboseMsg ("Efi Image subsystem type is efi boot service driver.");
2010 } else if (stricmp (ModuleType
, "UEFI_APPLICATION") == 0 ||
2011 stricmp (ModuleType
, "APPLICATION") == 0) {
2012 Type
= EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
;
2013 VerboseMsg ("Efi Image subsystem type is efi application.");
2015 } else if (stricmp (ModuleType
, "DXE_RUNTIME_DRIVER") == 0 ||
2016 stricmp (ModuleType
, "RT_DRIVER") == 0) {
2017 Type
= EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
2018 VerboseMsg ("Efi Image subsystem type is efi runtime driver.");
2020 } else if (stricmp (ModuleType
, "DXE_SAL_DRIVER") == 0 ||
2021 stricmp (ModuleType
, "SAL_RT_DRIVER") == 0) {
2022 Type
= EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
;
2023 VerboseMsg ("Efi Image subsystem type is efi sal runtime driver.");
2026 Error (NULL
, 0, 1003, "Invalid option value", "EFI_FILETYPE = %s", ModuleType
);
2033 // Convert ELF image to PeImage
2035 if (IsElfHeader(FileBuffer
)) {
2036 VerboseMsg ("Convert %s from ELF to PE/COFF.", mInImageName
);
2037 if (!ConvertElf(&FileBuffer
, &FileLength
)) {
2038 Error (NULL
, 0, 3000, "Invalid", "Unable to convert %s from ELF to PE/COFF.", mInImageName
);
2044 // Make sure File Offsets and Virtual Offsets are the same in the image so it is XIP
2045 // XIP == eXecute In Place
2047 PeCoffConvertImageToXip (&FileBuffer
, &FileLength
);
2050 // Remove reloc section from PE or TE image
2052 if (mOutImageType
== FW_RELOC_STRIPEED_IMAGE
) {
2056 TeHdr
= (EFI_TE_IMAGE_HEADER
*) FileBuffer
;
2057 if (TeHdr
->Signature
== EFI_TE_IMAGE_HEADER_SIGNATURE
) {
2058 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) (TeHdr
+ 1);
2059 for (Index
= 0; Index
< TeHdr
->NumberOfSections
; Index
++, SectionHeader
++) {
2060 if (strcmp ((char *)SectionHeader
->Name
, ".reloc") == 0) {
2062 // Check the reloc section is in the end of image.
2064 if ((SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
) ==
2065 (FileLength
+ TeHdr
->StrippedSize
- sizeof (EFI_TE_IMAGE_HEADER
))) {
2067 // Remove .reloc section and update TeImage Header
2069 FileLength
= FileLength
- SectionHeader
->SizeOfRawData
;
2070 SectionHeader
->SizeOfRawData
= 0;
2071 SectionHeader
->Misc
.VirtualSize
= 0;
2072 TeHdr
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= 0;
2073 TeHdr
->DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= 0;
2082 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) FileBuffer
;
2083 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2084 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
);
2085 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2086 Error (NULL
, 0, 3000, "Invalid", "TE and DOS header signatures were not found in %s image.", mInImageName
);
2091 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ DosHdr
->e_lfanew
);
2092 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2093 Error (NULL
, 0, 3000, "Invalid", "PE header signature was not found in %s image.", mInImageName
);
2097 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2098 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2099 if (strcmp ((char *)SectionHeader
->Name
, ".reloc") == 0) {
2101 // Check the reloc section is in the end of image.
2103 if ((SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
) == FileLength
) {
2105 // Remove .reloc section and update PeImage Header
2107 FileLength
= FileLength
- SectionHeader
->SizeOfRawData
;
2109 PeHdr
->Pe32
.FileHeader
.Characteristics
|= EFI_IMAGE_FILE_RELOCS_STRIPPED
;
2110 if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
2111 Optional32
= (EFI_IMAGE_OPTIONAL_HEADER32
*)&PeHdr
->Pe32
.OptionalHeader
;
2112 Optional32
->SizeOfImage
-= SectionHeader
->SizeOfRawData
;
2113 Optional32
->SizeOfInitializedData
-= SectionHeader
->SizeOfRawData
;
2114 if (Optional32
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
2115 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= 0;
2116 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= 0;
2119 if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
2120 Optional64
= (EFI_IMAGE_OPTIONAL_HEADER64
*)&PeHdr
->Pe32
.OptionalHeader
;
2121 Optional64
->SizeOfImage
-= SectionHeader
->SizeOfRawData
;
2122 Optional64
->SizeOfInitializedData
-= SectionHeader
->SizeOfRawData
;
2123 if (Optional64
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
2124 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= 0;
2125 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= 0;
2128 SectionHeader
->Misc
.VirtualSize
= 0;
2129 SectionHeader
->SizeOfRawData
= 0;
2141 // Read the dos & pe hdrs of the image
2143 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)FileBuffer
;
2144 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2145 // NO DOS header, check for PE/COFF header
2146 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
);
2147 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2148 Error (NULL
, 0, 3000, "Invalid", "DOS header signature was not found in %s image.", mInImageName
);
2154 PeHdr
= (EFI_IMAGE_OPTIONAL_HEADER_UNION
*)(FileBuffer
+ DosHdr
->e_lfanew
);
2155 if (PeHdr
->Pe32
.Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
2156 Error (NULL
, 0, 3000, "Invalid", "PE header signature was not found in %s image.", mInImageName
);
2161 if (PeHdr
->Pe32
.FileHeader
.Machine
== IMAGE_FILE_MACHINE_ARM
) {
2162 // Some tools kick out IMAGE_FILE_MACHINE_ARM (0x1c0) vs IMAGE_FILE_MACHINE_ARMT (0x1c2)
2163 // so patch back to the offical UEFI value.
2164 PeHdr
->Pe32
.FileHeader
.Machine
= IMAGE_FILE_MACHINE_ARMT
;
2168 // Set new base address into image
2170 if (mOutImageType
== FW_REBASE_IMAGE
|| mOutImageType
== FW_SET_ADDRESS_IMAGE
) {
2171 if ((PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) && (PeHdr
->Pe32
.FileHeader
.Machine
!= IMAGE_FILE_MACHINE_IA64
)) {
2172 if (NewBaseAddress
>= 0x100000000ULL
) {
2173 Error (NULL
, 0, 3000, "Invalid", "New base address is larger than 4G for 32bit PE image");
2180 // Set Base Address to a negative value.
2182 NewBaseAddress
= (UINT64
) (0 - NewBaseAddress
);
2184 if (mOutImageType
== FW_REBASE_IMAGE
) {
2185 Status
= RebaseImage (mInImageName
, FileBuffer
, NewBaseAddress
);
2187 Status
= SetAddressToSectionHeader (mInImageName
, FileBuffer
, NewBaseAddress
);
2189 if (EFI_ERROR (Status
)) {
2191 Error (NULL
, 0, 3000, "Invalid", "Rebase/Set Image %s to Base address -0x%llx can't success", mInImageName
, 0 - NewBaseAddress
);
2193 Error (NULL
, 0, 3000, "Invalid", "Rebase/Set Image %s to Base address 0x%llx can't success", mInImageName
, NewBaseAddress
);
2205 // Extract bin data from Pe image.
2207 if (mOutImageType
== FW_BIN_IMAGE
) {
2208 if (FileLength
< PeHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
) {
2209 Error (NULL
, 0, 3000, "Invalid", "FileSize of %s is not a legal size.", mInImageName
);
2213 // Output bin data from exe file
2215 FileLength
= FileLength
- PeHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
;
2216 memmove (FileBuffer
, FileBuffer
+ PeHdr
->Pe32
.OptionalHeader
.SizeOfHeaders
, FileLength
);
2217 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2222 // Zero Debug Information of Pe Image
2224 if (mOutImageType
== FW_ZERO_DEBUG_IMAGE
) {
2225 Status
= ZeroDebugData (FileBuffer
, TRUE
);
2226 if (EFI_ERROR (Status
)) {
2227 Error (NULL
, 0, 3000, "Invalid", "Zero DebugData Error status is 0x%x", (int) Status
);
2232 // Write the updated Image
2234 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2239 // Set Time Stamp of Pe Image
2241 if (mOutImageType
== FW_SET_STAMP_IMAGE
) {
2242 Status
= SetStamp (FileBuffer
, TimeStamp
);
2243 if (EFI_ERROR (Status
)) {
2248 // Write the updated Image
2250 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2255 // Extract acpi data from pe image.
2257 if (mOutImageType
== FW_ACPI_IMAGE
) {
2258 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2259 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2260 if (strcmp ((char *)SectionHeader
->Name
, ".data") == 0 || strcmp ((char *)SectionHeader
->Name
, ".sdata") == 0) {
2264 if (SectionHeader
->Misc
.VirtualSize
< SectionHeader
->SizeOfRawData
) {
2265 FileLength
= SectionHeader
->Misc
.VirtualSize
;
2267 FileLength
= SectionHeader
->SizeOfRawData
;
2270 if (CheckAcpiTable (FileBuffer
+ SectionHeader
->PointerToRawData
, FileLength
) != STATUS_SUCCESS
) {
2271 Error (NULL
, 0, 3000, "Invalid", "ACPI table check failed in %s.", mInImageName
);
2276 // Output Apci data to file
2278 memmove (FileBuffer
, FileBuffer
+ SectionHeader
->PointerToRawData
, FileLength
);
2279 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2283 Error (NULL
, 0, 3000, "Invalid", "failed to get ACPI table from %s.", mInImageName
);
2287 // Zero all unused fields of the DOS header
2289 if (DosHdr
!= NULL
) {
2290 memcpy (&BackupDosHdr
, DosHdr
, sizeof (EFI_IMAGE_DOS_HEADER
));
2291 memset (DosHdr
, 0, sizeof (EFI_IMAGE_DOS_HEADER
));
2292 DosHdr
->e_magic
= BackupDosHdr
.e_magic
;
2293 DosHdr
->e_lfanew
= BackupDosHdr
.e_lfanew
;
2295 for (Index
= sizeof (EFI_IMAGE_DOS_HEADER
); Index
< (UINT32
) DosHdr
->e_lfanew
; Index
++) {
2296 FileBuffer
[Index
] = (UINT8
) DosHdr
->e_cp
;
2301 // Initialize TeImage Header
2303 memset (&TEImageHeader
, 0, sizeof (EFI_TE_IMAGE_HEADER
));
2304 TEImageHeader
.Signature
= EFI_TE_IMAGE_HEADER_SIGNATURE
;
2305 TEImageHeader
.Machine
= PeHdr
->Pe32
.FileHeader
.Machine
;
2306 TEImageHeader
.NumberOfSections
= (UINT8
) PeHdr
->Pe32
.FileHeader
.NumberOfSections
;
2307 TEImageHeader
.StrippedSize
= (UINT16
) ((UINTN
) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
) - (UINTN
) FileBuffer
);
2308 TEImageHeader
.Subsystem
= (UINT8
) Type
;
2311 // Patch the PE header
2313 PeHdr
->Pe32
.OptionalHeader
.Subsystem
= (UINT16
) Type
;
2315 if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
2316 Optional32
= (EFI_IMAGE_OPTIONAL_HEADER32
*)&PeHdr
->Pe32
.OptionalHeader
;
2317 Optional32
->MajorOperatingSystemVersion
= 0;
2318 Optional32
->MinorOperatingSystemVersion
= 0;
2319 Optional32
->MajorImageVersion
= 0;
2320 Optional32
->MinorImageVersion
= 0;
2321 Optional32
->MajorSubsystemVersion
= 0;
2322 Optional32
->MinorSubsystemVersion
= 0;
2323 Optional32
->Win32VersionValue
= 0;
2324 Optional32
->CheckSum
= 0;
2325 Optional32
->SizeOfStackReserve
= 0;
2326 Optional32
->SizeOfStackCommit
= 0;
2327 Optional32
->SizeOfHeapReserve
= 0;
2328 Optional32
->SizeOfHeapCommit
= 0;
2330 TEImageHeader
.AddressOfEntryPoint
= Optional32
->AddressOfEntryPoint
;
2331 TEImageHeader
.BaseOfCode
= Optional32
->BaseOfCode
;
2332 TEImageHeader
.ImageBase
= (UINT64
) (Optional32
->ImageBase
);
2334 if (Optional32
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
2335 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
;
2336 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
2339 if (Optional32
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
2340 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
2341 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
2345 // Zero .pdata section data.
2347 if (!KeepExceptionTableFlag
&& Optional32
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
&&
2348 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
!= 0 &&
2349 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
!= 0) {
2350 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2351 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2352 if (SectionHeader
->VirtualAddress
== Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
) {
2354 // Zero .pdata Section data
2356 memset (FileBuffer
+ SectionHeader
->PointerToRawData
, 0, SectionHeader
->SizeOfRawData
);
2358 // Zero .pdata Section header name
2360 memset (SectionHeader
->Name
, 0, sizeof (SectionHeader
->Name
));
2362 // Zero Execption Table
2364 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
= 0;
2365 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
= 0;
2366 DebugMsg (NULL
, 0, 9, "Zero the .pdata section for PE image", NULL
);
2373 // Strip zero padding at the end of the .reloc section
2375 if (!KeepZeroPendingFlag
&& Optional32
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
2376 if (Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
!= 0) {
2377 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2378 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2380 // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory
2382 if (SectionHeader
->VirtualAddress
== Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
) {
2383 SectionHeader
->Misc
.VirtualSize
= Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
2384 AllignedRelocSize
= (Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
+ Optional32
->FileAlignment
- 1) & (~(Optional32
->FileAlignment
- 1));
2386 // Check to see if there is zero padding at the end of the base relocations
2388 if (AllignedRelocSize
< SectionHeader
->SizeOfRawData
) {
2390 // Check to see if the base relocations are at the end of the file
2392 if (SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
== Optional32
->SizeOfImage
) {
2394 // All the required conditions are met to strip the zero padding of the end of the base relocations section
2396 Optional32
->SizeOfImage
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
2397 Optional32
->SizeOfInitializedData
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
2398 SectionHeader
->SizeOfRawData
= AllignedRelocSize
;
2399 FileLength
= Optional32
->SizeOfImage
;
2400 DebugMsg (NULL
, 0, 9, "Remove the zero padding bytes at the end of the base relocations", "The size of padding bytes is %u", (unsigned) (SectionHeader
->SizeOfRawData
- AllignedRelocSize
));
2407 } else if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
2408 Optional64
= (EFI_IMAGE_OPTIONAL_HEADER64
*)&PeHdr
->Pe32
.OptionalHeader
;
2409 Optional64
->MajorOperatingSystemVersion
= 0;
2410 Optional64
->MinorOperatingSystemVersion
= 0;
2411 Optional64
->MajorImageVersion
= 0;
2412 Optional64
->MinorImageVersion
= 0;
2413 Optional64
->MajorSubsystemVersion
= 0;
2414 Optional64
->MinorSubsystemVersion
= 0;
2415 Optional64
->Win32VersionValue
= 0;
2416 Optional64
->CheckSum
= 0;
2417 Optional64
->SizeOfStackReserve
= 0;
2418 Optional64
->SizeOfStackCommit
= 0;
2419 Optional64
->SizeOfHeapReserve
= 0;
2420 Optional64
->SizeOfHeapCommit
= 0;
2422 TEImageHeader
.AddressOfEntryPoint
= Optional64
->AddressOfEntryPoint
;
2423 TEImageHeader
.BaseOfCode
= Optional64
->BaseOfCode
;
2424 TEImageHeader
.ImageBase
= (UINT64
) (Optional64
->ImageBase
);
2426 if (Optional64
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
) {
2427 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
;
2428 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
= Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
2431 if (Optional64
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
2432 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
2433 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
;
2437 // Zero the .pdata section for X64 machine and don't check the Debug Directory is empty
2438 // For Itaninum and X64 Image, remove .pdata section.
2440 if ((!KeepExceptionTableFlag
&& PeHdr
->Pe32
.FileHeader
.Machine
== IMAGE_FILE_MACHINE_X64
) || PeHdr
->Pe32
.FileHeader
.Machine
== IMAGE_FILE_MACHINE_IA64
) {
2441 if (Optional64
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
&&
2442 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
!= 0 &&
2443 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
!= 0) {
2444 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2445 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2446 if (SectionHeader
->VirtualAddress
== Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
) {
2448 // Zero .pdata Section header name
2450 memset (SectionHeader
->Name
, 0, sizeof (SectionHeader
->Name
));
2452 RuntimeFunction
= (RUNTIME_FUNCTION
*)(FileBuffer
+ SectionHeader
->PointerToRawData
);
2453 for (Index1
= 0; Index1
< Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
/ sizeof (RUNTIME_FUNCTION
); Index1
++, RuntimeFunction
++) {
2454 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2455 for (Index2
= 0; Index2
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index2
++, SectionHeader
++) {
2456 if (RuntimeFunction
->UnwindInfoAddress
>= SectionHeader
->VirtualAddress
&& RuntimeFunction
->UnwindInfoAddress
< (SectionHeader
->VirtualAddress
+ SectionHeader
->SizeOfRawData
)) {
2457 UnwindInfo
= (UNWIND_INFO
*)(FileBuffer
+ SectionHeader
->PointerToRawData
+ (RuntimeFunction
->UnwindInfoAddress
- SectionHeader
->VirtualAddress
));
2458 if (UnwindInfo
->Version
== 1) {
2459 memset (UnwindInfo
+ 1, 0, UnwindInfo
->CountOfUnwindCodes
* sizeof (UINT16
));
2460 memset (UnwindInfo
, 0, sizeof (UNWIND_INFO
));
2465 memset (RuntimeFunction
, 0, sizeof (RUNTIME_FUNCTION
));
2468 // Zero Execption Table
2470 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].Size
= 0;
2471 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION
].VirtualAddress
= 0;
2472 DebugMsg (NULL
, 0, 9, "Zero the .pdata section if the machine type is X64 for PE32+ image", NULL
);
2480 // Strip zero padding at the end of the .reloc section
2482 if (!KeepZeroPendingFlag
&& Optional64
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
) {
2483 if (Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
!= 0) {
2484 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2485 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
2487 // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory
2489 if (SectionHeader
->VirtualAddress
== Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
) {
2490 SectionHeader
->Misc
.VirtualSize
= Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
;
2491 AllignedRelocSize
= (Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
+ Optional64
->FileAlignment
- 1) & (~(Optional64
->FileAlignment
- 1));
2493 // Check to see if there is zero padding at the end of the base relocations
2495 if (AllignedRelocSize
< SectionHeader
->SizeOfRawData
) {
2497 // Check to see if the base relocations are at the end of the file
2499 if (SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
== Optional64
->SizeOfImage
) {
2501 // All the required conditions are met to strip the zero padding of the end of the base relocations section
2503 Optional64
->SizeOfImage
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
2504 Optional64
->SizeOfInitializedData
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
2505 SectionHeader
->SizeOfRawData
= AllignedRelocSize
;
2506 FileLength
= Optional64
->SizeOfImage
;
2507 DebugMsg (NULL
, 0, 9, "Remove the zero padding bytes at the end of the base relocations", "The size of padding bytes is %u", (unsigned) (SectionHeader
->SizeOfRawData
- AllignedRelocSize
));
2515 Error (NULL
, 0, 3000, "Invalid", "Magic 0x%x of PeImage %s is unknown.", PeHdr
->Pe32
.OptionalHeader
.Magic
, mInImageName
);
2519 if (((PeHdr
->Pe32
.FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) == 0) && \
2520 (TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
== 0) && \
2521 (TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].Size
== 0)) {
2523 // PeImage can be loaded into memory, but it has no relocation section.
2524 // Fix TeImage Header to set VA of relocation data directory to not zero, the size is still zero.
2526 if (Optional32
!= NULL
) {
2527 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= Optional32
->SizeOfImage
- sizeof (EFI_IMAGE_BASE_RELOCATION
);
2528 } else if (Optional64
!= NULL
) {
2529 TEImageHeader
.DataDirectory
[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC
].VirtualAddress
= Optional64
->SizeOfImage
- sizeof (EFI_IMAGE_BASE_RELOCATION
);
2534 // Fill HII section data
2536 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2537 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++) {
2538 if (stricmp ((char *)SectionHeader
[Index
].Name
, ".hii") == 0) {
2540 // Update resource section header offset
2542 SetHiiResourceHeader ((UINT8
*) FileBuffer
+ SectionHeader
[Index
].PointerToRawData
, SectionHeader
[Index
].VirtualAddress
);
2544 // Update resource section name
2546 strcpy((char *) SectionHeader
[Index
].Name
, ".rsrc");
2548 // Update resource data directory.
2550 if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
2551 Optional32
= (EFI_IMAGE_OPTIONAL_HEADER32
*)&PeHdr
->Pe32
.OptionalHeader
;
2552 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= SectionHeader
[Index
].VirtualAddress
;
2553 Optional32
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= SectionHeader
[Index
].Misc
.VirtualSize
;
2554 } else if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
2555 Optional64
= (EFI_IMAGE_OPTIONAL_HEADER64
*)&PeHdr
->Pe32
.OptionalHeader
;
2556 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
= SectionHeader
[Index
].VirtualAddress
;
2557 Optional64
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
= SectionHeader
[Index
].Misc
.VirtualSize
;
2564 // Zero ExceptionTable Xdata
2566 if (!KeepExceptionTableFlag
) {
2567 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) &(PeHdr
->Pe32
.OptionalHeader
) + PeHdr
->Pe32
.FileHeader
.SizeOfOptionalHeader
);
2568 for (Index
= 0; Index
< PeHdr
->Pe32
.FileHeader
.NumberOfSections
; Index
++) {
2569 if (stricmp ((char *)SectionHeader
[Index
].Name
, ".xdata") == 0) {
2571 // zero .xdata section
2573 memset (FileBuffer
+ SectionHeader
[Index
].PointerToRawData
, 0, SectionHeader
[Index
].SizeOfRawData
);
2574 DebugMsg (NULL
, 0, 9, NULL
, "Zero the .xdata section for PE image at Offset 0x%x and Length 0x%x", (unsigned) SectionHeader
[Index
].PointerToRawData
, (unsigned) SectionHeader
[Index
].SizeOfRawData
);
2581 // Zero Time/Data field
2583 ZeroDebugData (FileBuffer
, FALSE
);
2585 if (mOutImageType
== FW_TE_IMAGE
) {
2586 if ((PeHdr
->Pe32
.FileHeader
.NumberOfSections
&~0xFF) || (Type
&~0xFF)) {
2588 // Pack the subsystem and NumberOfSections into 1 byte. Make sure they fit both.
2590 Error (NULL
, 0, 3000, "Invalid", "Image's subsystem or NumberOfSections of PeImage %s cannot be packed into 1 byte.", mInImageName
);
2594 if ((PeHdr
->Pe32
.OptionalHeader
.SectionAlignment
!= PeHdr
->Pe32
.OptionalHeader
.FileAlignment
)) {
2596 // TeImage has the same section alignment and file alignment.
2598 Error (NULL
, 0, 3000, "Invalid", "Section-Alignment and File-Alignment of PeImage %s do not match, they must be equal for a TeImage.", mInImageName
);
2602 DebugMsg (NULL
, 0, 9, "TeImage Header Info", "Machine type is %X, Number of sections is %X, Stripped size is %X, EntryPoint is %X, BaseOfCode is %X, ImageBase is %llX",
2603 TEImageHeader
.Machine
, TEImageHeader
.NumberOfSections
, TEImageHeader
.StrippedSize
, (unsigned) TEImageHeader
.AddressOfEntryPoint
, (unsigned) TEImageHeader
.BaseOfCode
, (unsigned long long) TEImageHeader
.ImageBase
);
2605 // Update Image to TeImage
2607 FileLength
= FileLength
- TEImageHeader
.StrippedSize
;
2608 memmove (FileBuffer
+ sizeof (EFI_TE_IMAGE_HEADER
), FileBuffer
+ TEImageHeader
.StrippedSize
, FileLength
);
2609 FileLength
= FileLength
+ sizeof (EFI_TE_IMAGE_HEADER
);
2610 memcpy (FileBuffer
, &TEImageHeader
, sizeof (EFI_TE_IMAGE_HEADER
));
2611 VerboseMsg ("the size of output file is %u bytes", (unsigned) (FileLength
));
2615 // Following codes are to fix the objcopy's issue:
2616 // objcopy in binutil 2.50.18 will set PE image's charactices to "RELOC_STRIPPED" if image has no ".reloc" section
2617 // It cause issue for EFI image which has no ".reloc" sections.
2618 // Following codes will be removed when objcopy in binutil fix this problem for PE image.
2620 if ((PeHdr
->Pe32
.FileHeader
.Characteristics
& EFI_IMAGE_FILE_RELOCS_STRIPPED
) != 0) {
2621 if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
2622 Optional32
= (EFI_IMAGE_OPTIONAL_HEADER32
*)&PeHdr
->Pe32
.OptionalHeader
;
2623 if (Optional32
->ImageBase
== 0) {
2624 PeHdr
->Pe32
.FileHeader
.Characteristics
&= ~EFI_IMAGE_FILE_RELOCS_STRIPPED
;
2626 } else if (PeHdr
->Pe32
.OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
2627 Optional64
= (EFI_IMAGE_OPTIONAL_HEADER64
*)&PeHdr
->Pe32
.OptionalHeader
;
2628 if (Optional64
->ImageBase
== 0) {
2629 PeHdr
->Pe32
.FileHeader
.Characteristics
&= ~EFI_IMAGE_FILE_RELOCS_STRIPPED
;
2637 // Update Image to EfiImage or TE image
2640 if ((FileLength
!= InputFileLength
) || (memcmp (FileBuffer
, InputFileBuffer
, FileLength
) != 0)) {
2642 // Update File when File is changed.
2644 fpInOut
= fopen (mInImageName
, "wb");
2645 if (fpInOut
== NULL
) {
2646 Error (NULL
, 0, 0001, "Error opening file", mInImageName
);
2649 fwrite (FileBuffer
, 1, FileLength
, fpInOut
);
2650 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2653 if ((OutputFileTime
< InputFileTime
) || (FileLength
!= OutputFileLength
) || (memcmp (FileBuffer
, OutputFileBuffer
, FileLength
) != 0)) {
2655 // Update File when File is changed or File is old.
2657 fpOut
= fopen (OutImageName
, "wb");
2658 if (fpOut
== NULL
) {
2659 Error (NULL
, 0, 0001, "Error opening output file", OutImageName
);
2662 fwrite (FileBuffer
, 1, FileLength
, fpOut
);
2663 VerboseMsg ("the size of output file is %u bytes", (unsigned) FileLength
);
2666 mImageSize
= FileLength
;
2669 if (fpInOut
!= NULL
) {
2670 if (GetUtilityStatus () != STATUS_SUCCESS
) {
2672 // when file updates failed, original file is still recovered.
2674 fwrite (InputFileBuffer
, 1, InputFileLength
, fpInOut
);
2677 // Write converted data into fpInOut file and close input file.
2682 if (FileBuffer
!= NULL
) {
2686 if (InputFileName
!= NULL
) {
2687 free (InputFileName
);
2690 if (fpOut
!= NULL
) {
2692 // Write converted data into fpOut file and close output file.
2695 if (GetUtilityStatus () != STATUS_SUCCESS
) {
2696 if (OutputFileBuffer
== NULL
) {
2697 remove (OutImageName
);
2699 fpOut
= fopen (OutImageName
, "wb");
2700 fwrite (OutputFileBuffer
, 1, OutputFileLength
, fpOut
);
2706 if (InputFileBuffer
!= NULL
) {
2707 free (InputFileBuffer
);
2710 if (OutputFileBuffer
!= NULL
) {
2711 free (OutputFileBuffer
);
2715 // Write module size and time stamp to report file.
2717 if (OutImageName
!= NULL
) {
2718 FileLen
= strlen (OutImageName
);
2720 if (FileLen
>= 4 && strcmp (OutImageName
+ (FileLen
- 4), ".efi") == 0) {
2721 ReportFileName
= (CHAR8
*) malloc (FileLen
+ 1);
2722 if (ReportFileName
!= NULL
) {
2723 strcpy (ReportFileName
, OutImageName
);
2724 strcpy (ReportFileName
+ (FileLen
- 4), ".txt");
2725 ReportFile
= fopen (ReportFileName
, "w+");
2726 if (ReportFile
!= NULL
) {
2727 fprintf (ReportFile
, "MODULE_SIZE = %u\n", (unsigned) mImageSize
);
2728 fprintf (ReportFile
, "TIME_STAMP = %u\n", (unsigned) mImageTimeStamp
);
2731 free (ReportFileName
);
2734 VerboseMsg ("%s tool done with return code is 0x%x.", UTILITY_NAME
, GetUtilityStatus ());
2736 return GetUtilityStatus ();
2742 IN OUT UINT8
*FileBuffer
,
2743 BOOLEAN ZeroDebugFlag
2747 Routine Description:
2749 Zero debug information in PeImage.
2753 FileBuffer - Pointer to PeImage.
2754 ZeroDebugFlag - TRUE to zero Debug information, FALSE to only zero time/stamp
2758 EFI_ABORTED - PeImage is invalid.
2759 EFI_SUCCESS - Zero debug data successfully.
2764 UINT32 DebugDirectoryEntryRva
;
2765 UINT32 DebugDirectoryEntryFileOffset
;
2766 UINT32 ExportDirectoryEntryRva
;
2767 UINT32 ExportDirectoryEntryFileOffset
;
2768 UINT32 ResourceDirectoryEntryRva
;
2769 UINT32 ResourceDirectoryEntryFileOffset
;
2770 EFI_IMAGE_DOS_HEADER
*DosHdr
;
2771 EFI_IMAGE_FILE_HEADER
*FileHdr
;
2772 EFI_IMAGE_OPTIONAL_HEADER32
*Optional32Hdr
;
2773 EFI_IMAGE_OPTIONAL_HEADER64
*Optional64Hdr
;
2774 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
2775 EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*DebugEntry
;
2776 UINT32
*NewTimeStamp
;
2781 DebugDirectoryEntryRva
= 0;
2782 ExportDirectoryEntryRva
= 0;
2783 ResourceDirectoryEntryRva
= 0;
2784 DebugDirectoryEntryFileOffset
= 0;
2785 ExportDirectoryEntryFileOffset
= 0;
2786 ResourceDirectoryEntryFileOffset
= 0;
2787 DosHdr
= (EFI_IMAGE_DOS_HEADER
*) FileBuffer
;
2788 FileHdr
= (EFI_IMAGE_FILE_HEADER
*) (FileBuffer
+ DosHdr
->e_lfanew
+ sizeof (UINT32
));
2791 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)FileBuffer
;
2792 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
2793 // NO DOS header, must start with PE/COFF header
2794 FileHdr
= (EFI_IMAGE_FILE_HEADER
*)(FileBuffer
+ sizeof (UINT32
));
2796 FileHdr
= (EFI_IMAGE_FILE_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof (UINT32
));
2800 // Get Debug, Export and Resource EntryTable RVA address.
2801 // Resource Directory entry need to review.
2803 if (FileHdr
->Machine
== EFI_IMAGE_MACHINE_IA32
) {
2804 Optional32Hdr
= (EFI_IMAGE_OPTIONAL_HEADER32
*) ((UINT8
*) FileHdr
+ sizeof (EFI_IMAGE_FILE_HEADER
));
2805 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) Optional32Hdr
+ FileHdr
->SizeOfOptionalHeader
);
2806 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
&& \
2807 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
!= 0) {
2808 ExportDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
2810 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
&& \
2811 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
!= 0) {
2812 ResourceDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
;
2814 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
&& \
2815 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
!= 0) {
2816 DebugDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
2817 if (ZeroDebugFlag
) {
2818 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= 0;
2819 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= 0;
2823 Optional64Hdr
= (EFI_IMAGE_OPTIONAL_HEADER64
*) ((UINT8
*) FileHdr
+ sizeof (EFI_IMAGE_FILE_HEADER
));
2824 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) Optional64Hdr
+ FileHdr
->SizeOfOptionalHeader
);
2825 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
&& \
2826 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
!= 0) {
2827 ExportDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
2829 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
&& \
2830 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
!= 0) {
2831 ResourceDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
;
2833 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
&& \
2834 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
!= 0) {
2835 DebugDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
2836 if (ZeroDebugFlag
) {
2837 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
= 0;
2838 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
= 0;
2844 // Get DirectoryEntryTable file offset.
2846 for (Index
= 0; Index
< FileHdr
->NumberOfSections
; Index
++, SectionHeader
++) {
2847 if (DebugDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
2848 DebugDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
2849 DebugDirectoryEntryFileOffset
=
2850 DebugDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
2852 if (ExportDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
2853 ExportDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
2854 ExportDirectoryEntryFileOffset
=
2855 ExportDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
2857 if (ResourceDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
2858 ResourceDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
2859 ResourceDirectoryEntryFileOffset
=
2860 ResourceDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
2865 //Zero Debug Data and TimeStamp
2867 FileHdr
->TimeDateStamp
= 0;
2868 mImageTimeStamp
= 0;
2869 if (ExportDirectoryEntryFileOffset
!= 0) {
2870 NewTimeStamp
= (UINT32
*) (FileBuffer
+ ExportDirectoryEntryFileOffset
+ sizeof (UINT32
));
2874 if (ResourceDirectoryEntryFileOffset
!= 0) {
2875 NewTimeStamp
= (UINT32
*) (FileBuffer
+ ResourceDirectoryEntryFileOffset
+ sizeof (UINT32
));
2879 if (DebugDirectoryEntryFileOffset
!= 0) {
2880 DebugEntry
= (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
*) (FileBuffer
+ DebugDirectoryEntryFileOffset
);
2881 DebugEntry
->TimeDateStamp
= 0;
2882 mImageTimeStamp
= 0;
2883 if (ZeroDebugFlag
) {
2884 memset (FileBuffer
+ DebugEntry
->FileOffset
, 0, DebugEntry
->SizeOfData
);
2885 memset (DebugEntry
, 0, sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
));
2895 IN OUT UINT8
*FileBuffer
,
2900 Routine Description:
2902 Set new time stamp into PeImage FileHdr and Directory table:
2903 Debug, Export and Resource.
2907 FileBuffer - Pointer to PeImage.
2908 TimeStamp - Time stamp string.
2912 EFI_INVALID_PARAMETER - TimeStamp format is not recognized.
2913 EFI_SUCCESS - Set new time stamp in this image successfully.
2921 UINT32 DebugDirectoryEntryRva
;
2922 UINT32 DebugDirectoryEntryFileOffset
;
2923 UINT32 ExportDirectoryEntryRva
;
2924 UINT32 ExportDirectoryEntryFileOffset
;
2925 UINT32 ResourceDirectoryEntryRva
;
2926 UINT32 ResourceDirectoryEntryFileOffset
;
2927 EFI_IMAGE_DOS_HEADER
*DosHdr
;
2928 EFI_IMAGE_FILE_HEADER
*FileHdr
;
2929 EFI_IMAGE_OPTIONAL_HEADER32
*Optional32Hdr
;
2930 EFI_IMAGE_OPTIONAL_HEADER64
*Optional64Hdr
;
2931 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
2932 UINT32
*NewTimeStamp
;
2937 DebugDirectoryEntryRva
= 0;
2938 DebugDirectoryEntryFileOffset
= 0;
2939 ExportDirectoryEntryRva
= 0;
2940 ExportDirectoryEntryFileOffset
= 0;
2941 ResourceDirectoryEntryRva
= 0;
2942 ResourceDirectoryEntryFileOffset
= 0;
2944 // Get time and date that will be set.
2946 if (TimeStamp
== NULL
) {
2947 Error (NULL
, 0, 3000, "Invalid", "TimeStamp cannot be NULL.");
2948 return EFI_INVALID_PARAMETER
;
2951 // compare the value with "NOW", if yes, current system time is set.
2953 if (stricmp (TimeStamp
, "NOW") == 0) {
2955 // get system current time and date
2960 // Check Time Format strictly yyyy-mm-dd 00:00:00
2962 for (Index
= 0; TimeStamp
[Index
] != '\0' && Index
< 20; Index
++) {
2963 if (Index
== 4 || Index
== 7) {
2964 if (TimeStamp
[Index
] == '-') {
2967 } else if (Index
== 13 || Index
== 16) {
2968 if (TimeStamp
[Index
] == ':') {
2971 } else if (Index
== 10 && TimeStamp
[Index
] == ' ') {
2973 } else if ((TimeStamp
[Index
] < '0') || (TimeStamp
[Index
] > '9')) {
2978 if (Index
< 19 || TimeStamp
[19] != '\0') {
2979 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect Time \"%s\"\n Correct Format \"yyyy-mm-dd 00:00:00\"", TimeStamp
);
2980 return EFI_INVALID_PARAMETER
;
2984 // get the date and time from TimeStamp
2986 if (sscanf (TimeStamp
, "%d-%d-%d %d:%d:%d",
2994 Error (NULL
, 0, 1003, "Invalid option value", "Incorrect Tiem \"%s\"\n Correct Format \"yyyy-mm-dd 00:00:00\"", TimeStamp
);
2995 return EFI_INVALID_PARAMETER
;
2999 // in struct, Month (0 - 11; Jan = 0). So decrease 1 from it
3001 if (stime
.tm_mon
<= 0 || stime
.tm_mday
<=0) {
3002 Error (NULL
, 0, 3000, "Invalid", "%s Invalid date!", TimeStamp
);
3003 return EFI_INVALID_PARAMETER
;
3008 // in struct, Year (current year minus 1900)
3009 // and only the dates can be handled from Jan 1, 1970 to Jan 18, 2038
3012 // convert 0 -> 100 (2000), 1 -> 101 (2001), ..., 38 -> 138 (2038)
3014 if (stime
.tm_year
>= 1970 && stime
.tm_year
<= 2038) {
3016 // convert 1970 -> 70, 2000 -> 100, ...
3018 stime
.tm_year
-= 1900;
3020 Error (NULL
, 0, 3000, "Invalid", "%s Invalid or unsupported datetime!", TimeStamp
);
3021 return EFI_INVALID_PARAMETER
;
3025 // convert the date and time to time_t format
3027 newtime
= mktime (&stime
);
3028 if (newtime
== (time_t) - 1) {
3029 Error (NULL
, 0, 3000, "Invalid", "%s Invalid or unsupported datetime!", TimeStamp
);
3030 return EFI_INVALID_PARAMETER
;
3034 ptime
= localtime (&newtime
);
3035 DebugMsg (NULL
, 0, 9, "New Image Time Stamp", "%04d-%02d-%02d %02d:%02d:%02d",
3036 ptime
->tm_year
+ 1900, ptime
->tm_mon
+ 1, ptime
->tm_mday
, ptime
->tm_hour
, ptime
->tm_min
, ptime
->tm_sec
);
3038 // Set new time and data into PeImage.
3040 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)FileBuffer
;
3041 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
3042 // NO DOS header, must start with PE/COFF header
3043 FileHdr
= (EFI_IMAGE_FILE_HEADER
*)(FileBuffer
+ sizeof (UINT32
));
3045 FileHdr
= (EFI_IMAGE_FILE_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof (UINT32
));
3049 // Get Debug, Export and Resource EntryTable RVA address.
3050 // Resource Directory entry need to review.
3052 if (FileHdr
->Machine
== EFI_IMAGE_MACHINE_IA32
) {
3053 Optional32Hdr
= (EFI_IMAGE_OPTIONAL_HEADER32
*) ((UINT8
*) FileHdr
+ sizeof (EFI_IMAGE_FILE_HEADER
));
3054 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) Optional32Hdr
+ FileHdr
->SizeOfOptionalHeader
);
3055 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
&& \
3056 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
!= 0) {
3057 ExportDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
3059 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
&& \
3060 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
!= 0) {
3061 ResourceDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
;
3063 if (Optional32Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
&& \
3064 Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
!= 0) {
3065 DebugDirectoryEntryRva
= Optional32Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
3068 Optional64Hdr
= (EFI_IMAGE_OPTIONAL_HEADER64
*) ((UINT8
*) FileHdr
+ sizeof (EFI_IMAGE_FILE_HEADER
));
3069 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*) ((UINT8
*) Optional64Hdr
+ FileHdr
->SizeOfOptionalHeader
);
3070 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
&& \
3071 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].Size
!= 0) {
3072 ExportDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT
].VirtualAddress
;
3074 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
&& \
3075 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].Size
!= 0) {
3076 ResourceDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE
].VirtualAddress
;
3078 if (Optional64Hdr
->NumberOfRvaAndSizes
> EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
&& \
3079 Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].Size
!= 0) {
3080 DebugDirectoryEntryRva
= Optional64Hdr
->DataDirectory
[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG
].VirtualAddress
;
3085 // Get DirectoryEntryTable file offset.
3087 for (Index
= 0; Index
< FileHdr
->NumberOfSections
; Index
++, SectionHeader
++) {
3088 if (DebugDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
3089 DebugDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
3090 DebugDirectoryEntryFileOffset
=
3091 DebugDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
3093 if (ExportDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
3094 ExportDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
3095 ExportDirectoryEntryFileOffset
=
3096 ExportDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
3098 if (ResourceDirectoryEntryRva
>= SectionHeader
->VirtualAddress
&&
3099 ResourceDirectoryEntryRva
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
) {
3100 ResourceDirectoryEntryFileOffset
=
3101 ResourceDirectoryEntryRva
- SectionHeader
->VirtualAddress
+ SectionHeader
->PointerToRawData
;
3108 FileHdr
->TimeDateStamp
= (UINT32
) newtime
;
3109 mImageTimeStamp
= (UINT32
) newtime
;
3110 if (ExportDirectoryEntryRva
!= 0) {
3111 NewTimeStamp
= (UINT32
*) (FileBuffer
+ ExportDirectoryEntryFileOffset
+ sizeof (UINT32
));
3112 *NewTimeStamp
= (UINT32
) newtime
;
3115 if (ResourceDirectoryEntryRva
!= 0) {
3116 NewTimeStamp
= (UINT32
*) (FileBuffer
+ ResourceDirectoryEntryFileOffset
+ sizeof (UINT32
));
3117 *NewTimeStamp
= (UINT32
) newtime
;
3120 if (DebugDirectoryEntryRva
!= 0) {
3121 NewTimeStamp
= (UINT32
*) (FileBuffer
+ DebugDirectoryEntryFileOffset
+ sizeof (UINT32
));
3122 *NewTimeStamp
= (UINT32
) newtime
;
3136 Routine Description:
3137 Read a 32-bit microcode data value from a text file and convert to raw binary form.
3140 InFptr - file pointer to input text file
3141 Data - pointer to where to return the data parsed
3144 STATUS_SUCCESS - no errors or warnings, Data contains valid information
3145 STATUS_ERROR - errors were encountered
3149 CHAR8 Line
[MAX_LINE_LEN
];
3151 unsigned ScannedData
= 0;
3153 Line
[MAX_LINE_LEN
- 1] = 0;
3155 if (fgets (Line
, MAX_LINE_LEN
, InFptr
) == NULL
) {
3156 return STATUS_ERROR
;
3159 // If it was a binary file, then it may have overwritten our null terminator
3161 if (Line
[MAX_LINE_LEN
- 1] != 0) {
3162 return STATUS_ERROR
;
3168 for (cptr
= Line
; *cptr
&& isspace((int)*cptr
); cptr
++) {
3171 // Skip Blank Lines and Comment Lines
3172 if ((strlen(cptr
) != 0) && (*cptr
!= ';')) {
3178 // dd 000000001h ; comment
3183 if ((tolower((int)cptr
[0]) == 'd') && (tolower((int)cptr
[1]) == 'd') && isspace ((int)cptr
[2])) {
3185 // Skip blanks and look for a hex digit
3188 for (; *cptr
&& isspace((int)*cptr
); cptr
++) {
3190 if (isxdigit ((int)*cptr
)) {
3191 if (sscanf (cptr
, "%X", &ScannedData
) != 1) {
3192 return STATUS_ERROR
;
3195 *Data
= (UINT32
) ScannedData
;
3196 return STATUS_SUCCESS
;
3199 return STATUS_ERROR
;