3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. 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 image type
22 #include "WinNtInclude.h"
32 #include <Common/UefiBaseTypes.h>
33 #include <Common/EfiImage.h>
35 #include "CommonLib.h"
36 #include "EfiUtilityMsgs.c"
38 #define UTILITY_NAME "FwImage"
41 typedef unsigned long ULONG
;
42 typedef unsigned char UCHAR
;
43 typedef unsigned char *PUCHAR
;
44 typedef unsigned short USHORT
;
52 printf ("Usage: " UTILITY_NAME
" {-t time-date} [BASE|SEC|PEI_CORE|PEIM|DXE_CORE|DXE_DRIVER|DXE_RUNTIME_DRIVER|DXE_SAL_DRIVER|DXE_SMM_DRIVER|TOOL|UEFI_DRIVER|UEFI_APPLICATION|USER_DEFINED] peimage [outimage]");
65 UCHAR Buffer
[8 * 1024];
67 fseek (in
, 0, SEEK_END
);
68 filesize
= ftell (in
);
70 fseek (in
, 0, SEEK_SET
);
71 fseek (out
, 0, SEEK_SET
);
74 while (offset
< filesize
) {
75 length
= sizeof (Buffer
);
76 if (filesize
- offset
< length
) {
77 length
= filesize
- offset
;
80 fread (Buffer
, length
, 1, in
);
81 fwrite (Buffer
, length
, 1, out
);
85 if ((ULONG
) ftell (out
) != filesize
) {
86 Error (NULL
, 0, 0, "write error", NULL
);
90 return STATUS_SUCCESS
;
101 fseek (in
, 0, SEEK_END
);
102 *Length
= ftell (in
);
103 *Buffer
= malloc (*Length
);
104 fseek (in
, 0, SEEK_SET
);
105 fread (*Buffer
, *Length
, 1, in
);
106 return STATUS_SUCCESS
;
117 fseek (out
, 0, SEEK_SET
);
118 fwrite (Buffer
, Length
, 1, out
);
119 if ((ULONG
) ftell (out
) != Length
) {
120 Error (NULL
, 0, 0, "write error", NULL
);
124 return STATUS_SUCCESS
;
140 argc - Number of command line parameters.
141 argv - Array of pointers to command line parameter strings.
144 STATUS_SUCCESS - Utility exits successfully.
145 STATUS_ERROR - Some error occurred during execution.
158 EFI_IMAGE_DOS_HEADER
*DosHdr
;
159 EFI_IMAGE_NT_HEADERS
*PeHdr
;
160 EFI_IMAGE_OPTIONAL_HEADER32
*Optional32
;
161 EFI_IMAGE_OPTIONAL_HEADER64
*Optional64
;
163 struct tm TimeStruct
;
164 EFI_IMAGE_DOS_HEADER BackupDosHdr
;
169 BOOLEAN TimeStampPresent
;
170 UINTN AllignedRelocSize
;
172 EFI_IMAGE_SECTION_HEADER
*SectionHeader
;
175 RUNTIME_FUNCTION
*RuntimeFunction
;
176 UNWIND_INFO
*UnwindInfo
;
178 SetUtilityName (UTILITY_NAME
);
180 // Assign to fix compile warning
186 TimeStampPresent
= FALSE
;
189 // Look for -t time-date option first. If the time is "0", then
192 if ((argc
> 2) && !strcmp (argv
[1], "-t")) {
193 TimeStampPresent
= TRUE
;
194 if (strcmp (argv
[2], "0") != 0) {
196 // Convert the string to a value
198 memset ((char *) &TimeStruct
, 0, sizeof (TimeStruct
));
200 argv
[2], "%d/%d/%d,%d:%d:%d",
201 &TimeStruct
.tm_mon
, /* months since January - [0,11] */
202 &TimeStruct
.tm_mday
, /* day of the month - [1,31] */
203 &TimeStruct
.tm_year
, /* years since 1900 */
204 &TimeStruct
.tm_hour
, /* hours since midnight - [0,23] */
205 &TimeStruct
.tm_min
, /* minutes after the hour - [0,59] */
206 &TimeStruct
.tm_sec
/* seconds after the minute - [0,59] */
208 Error (NULL
, 0, 0, argv
[2], "failed to convert to mm/dd/yyyy,hh:mm:ss format");
212 // Now fixup some of the fields
215 TimeStruct
.tm_year
-= 1900;
217 // Sanity-check values?
220 TimeStamp
= mktime (&TimeStruct
);
221 if (TimeStamp
== (time_t) - 1) {
222 Error (NULL
, 0, 0, argv
[2], "failed to convert time");
227 // Skip over the args
233 // Check for enough args
241 OutImageName
= argv
[3];
244 // Get new image type
247 if (*p
== '/' || *p
== '\\') {
251 if (stricmp (p
, "app") == 0 || stricmp (p
, "UEFI_APPLICATION") == 0) {
252 Type
= EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION
;
255 } else if (stricmp (p
, "bsdrv") == 0 || stricmp (p
, "DXE_DRIVER") == 0) {
256 Type
= EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
259 } else if (stricmp (p
, "rtdrv") == 0 || stricmp (p
, "DXE_RUNTIME_DRIVER") == 0) {
260 Type
= EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER
;
263 } else if (stricmp (p
, "rtdrv") == 0 || stricmp (p
, "DXE_SAL_DRIVER") == 0) {
264 Type
= EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER
;
266 } else if (stricmp (p
, "SEC") == 0) {
267 Type
= EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
269 } else if (stricmp (p
, "peim") == 0 ||
270 stricmp (p
, "BASE") == 0 ||
271 stricmp (p
, "PEI_CORE") == 0 ||
272 stricmp (p
, "PEIM") == 0 ||
273 stricmp (p
, "DXE_SMM_DRIVER") == 0 ||
274 stricmp (p
, "TOOL") == 0 ||
275 stricmp (p
, "UEFI_APPLICATION") == 0 ||
276 stricmp (p
, "USER_DEFINED") == 0 ||
277 stricmp (p
, "UEFI_DRIVER") == 0 ||
278 stricmp (p
, "DXE_CORE") == 0
280 Type
= EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER
;
290 fpIn
= fopen (argv
[2], "rb");
292 Error (NULL
, 0, 0, argv
[2], "failed to open input file for reading");
296 FReadFile (fpIn
, (VOID
**)&FileBuffer
, &FileLength
);
299 // Read the dos & pe hdrs of the image
301 DosHdr
= (EFI_IMAGE_DOS_HEADER
*)FileBuffer
;
302 if (DosHdr
->e_magic
!= EFI_IMAGE_DOS_SIGNATURE
) {
303 Error (NULL
, 0, 0, argv
[2], "DOS header signature not found in source image");
308 PeHdr
= (EFI_IMAGE_NT_HEADERS
*)(FileBuffer
+ DosHdr
->e_lfanew
);
309 if (PeHdr
->Signature
!= EFI_IMAGE_NT_SIGNATURE
) {
310 Error (NULL
, 0, 0, argv
[2], "PE header signature not found in source image");
318 strcpy (outname
, argv
[2]);
320 for (p
= outname
; *p
; p
++) {
333 OutImageName
= outname
;
336 fpOut
= fopen (OutImageName
, "w+b");
338 Error (NULL
, 0, 0, OutImageName
, "could not open output file for writing");
344 // Zero all unused fields of the DOS header
346 memcpy (&BackupDosHdr
, DosHdr
, sizeof (EFI_IMAGE_DOS_HEADER
));
347 memset (DosHdr
, 0, sizeof (EFI_IMAGE_DOS_HEADER
));
348 DosHdr
->e_magic
= BackupDosHdr
.e_magic
;
349 DosHdr
->e_lfanew
= BackupDosHdr
.e_lfanew
;
351 for (Index
= sizeof (EFI_IMAGE_DOS_HEADER
); Index
< (ULONG
) DosHdr
->e_lfanew
; Index
++) {
352 FileBuffer
[Index
] = DosHdr
->e_cp
;
356 // Path the PE header
358 PeHdr
->OptionalHeader
.Subsystem
= (USHORT
) Type
;
359 if (TimeStampPresent
) {
360 PeHdr
->FileHeader
.TimeDateStamp
= (UINT32
) TimeStamp
;
363 if (PeHdr
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
) {
364 Optional32
= (EFI_IMAGE_OPTIONAL_HEADER32
*)&PeHdr
->OptionalHeader
;
365 Optional32
->MajorLinkerVersion
= 0;
366 Optional32
->MinorLinkerVersion
= 0;
367 Optional32
->MajorOperatingSystemVersion
= 0;
368 Optional32
->MinorOperatingSystemVersion
= 0;
369 Optional32
->MajorImageVersion
= 0;
370 Optional32
->MinorImageVersion
= 0;
371 Optional32
->MajorSubsystemVersion
= 0;
372 Optional32
->MinorSubsystemVersion
= 0;
373 Optional32
->Win32VersionValue
= 0;
374 Optional32
->CheckSum
= 0;
375 Optional32
->SizeOfStackReserve
= 0;
376 Optional32
->SizeOfStackCommit
= 0;
377 Optional32
->SizeOfHeapReserve
= 0;
378 Optional32
->SizeOfHeapCommit
= 0;
381 // Strip zero padding at the end of the .reloc section
383 if (Optional32
->NumberOfRvaAndSizes
>= 6) {
384 if (Optional32
->DataDirectory
[5].Size
!= 0) {
385 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof(UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + PeHdr
->FileHeader
.SizeOfOptionalHeader
);
386 for (Index
= 0; Index
< PeHdr
->FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
388 // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory
390 if (SectionHeader
->VirtualAddress
== Optional32
->DataDirectory
[5].VirtualAddress
) {
391 SectionHeader
->Misc
.VirtualSize
= Optional32
->DataDirectory
[5].Size
;
392 AllignedRelocSize
= (Optional32
->DataDirectory
[5].Size
+ Optional32
->FileAlignment
- 1) & (~(Optional32
->FileAlignment
- 1));
394 // Check to see if there is zero padding at the end of the base relocations
396 if (AllignedRelocSize
< SectionHeader
->SizeOfRawData
) {
398 // Check to see if the base relocations are at the end of the file
400 if (SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
== Optional32
->SizeOfImage
) {
402 // All the required conditions are met to strip the zero padding of the end of the base relocations section
404 Optional32
->SizeOfImage
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
405 Optional32
->SizeOfInitializedData
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
406 SectionHeader
->SizeOfRawData
= AllignedRelocSize
;
407 FileLength
= Optional32
->SizeOfImage
;
415 if (PeHdr
->OptionalHeader
.Magic
== EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
) {
416 Optional64
= (EFI_IMAGE_OPTIONAL_HEADER64
*)&PeHdr
->OptionalHeader
;
417 Optional64
->MajorLinkerVersion
= 0;
418 Optional64
->MinorLinkerVersion
= 0;
419 Optional64
->MajorOperatingSystemVersion
= 0;
420 Optional64
->MinorOperatingSystemVersion
= 0;
421 Optional64
->MajorImageVersion
= 0;
422 Optional64
->MinorImageVersion
= 0;
423 Optional64
->MajorSubsystemVersion
= 0;
424 Optional64
->MinorSubsystemVersion
= 0;
425 Optional64
->Win32VersionValue
= 0;
426 Optional64
->CheckSum
= 0;
427 Optional64
->SizeOfStackReserve
= 0;
428 Optional64
->SizeOfStackCommit
= 0;
429 Optional64
->SizeOfHeapReserve
= 0;
430 Optional64
->SizeOfHeapCommit
= 0;
433 // Zero the .pdata section if the machine type is X64 and the Debug Directory is empty
435 if (PeHdr
->FileHeader
.Machine
== 0x8664) { // X64
436 if (Optional64
->NumberOfRvaAndSizes
>= 4) {
437 if (Optional64
->NumberOfRvaAndSizes
< 7 || (Optional64
->NumberOfRvaAndSizes
>= 7 && Optional64
->DataDirectory
[6].Size
== 0)) {
438 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof(UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + PeHdr
->FileHeader
.SizeOfOptionalHeader
);
439 for (Index
= 0; Index
< PeHdr
->FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
440 if (SectionHeader
->VirtualAddress
== Optional64
->DataDirectory
[3].VirtualAddress
) {
441 RuntimeFunction
= (RUNTIME_FUNCTION
*)(FileBuffer
+ SectionHeader
->PointerToRawData
);
442 for (Index1
= 0; Index1
< Optional64
->DataDirectory
[3].Size
/ sizeof (RUNTIME_FUNCTION
); Index1
++, RuntimeFunction
++) {
443 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof(UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + PeHdr
->FileHeader
.SizeOfOptionalHeader
);
444 for (Index2
= 0; Index2
< PeHdr
->FileHeader
.NumberOfSections
; Index2
++, SectionHeader
++) {
445 if (RuntimeFunction
->UnwindInfoAddress
> SectionHeader
->VirtualAddress
&& RuntimeFunction
->UnwindInfoAddress
< (SectionHeader
->VirtualAddress
+ SectionHeader
->SizeOfRawData
)) {
446 UnwindInfo
= (UNWIND_INFO
*)(FileBuffer
+ SectionHeader
->PointerToRawData
+ (RuntimeFunction
->UnwindInfoAddress
- SectionHeader
->VirtualAddress
));
447 if (UnwindInfo
->Version
== 1) {
448 memset (UnwindInfo
+ 1, 0, UnwindInfo
->CountOfUnwindCodes
* sizeof (UINT16
));
449 memset (UnwindInfo
, 0, sizeof (UNWIND_INFO
));
453 memset (RuntimeFunction
, 0, sizeof (RUNTIME_FUNCTION
));
457 Optional64
->DataDirectory
[3].Size
= 0;
458 Optional64
->DataDirectory
[3].VirtualAddress
= 0;
464 // Strip zero padding at the end of the .reloc section
466 if (Optional64
->NumberOfRvaAndSizes
>= 6) {
467 if (Optional64
->DataDirectory
[5].Size
!= 0) {
468 SectionHeader
= (EFI_IMAGE_SECTION_HEADER
*)(FileBuffer
+ DosHdr
->e_lfanew
+ sizeof(UINT32
) + sizeof (EFI_IMAGE_FILE_HEADER
) + PeHdr
->FileHeader
.SizeOfOptionalHeader
);
469 for (Index
= 0; Index
< PeHdr
->FileHeader
.NumberOfSections
; Index
++, SectionHeader
++) {
471 // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory
473 if (SectionHeader
->VirtualAddress
== Optional64
->DataDirectory
[5].VirtualAddress
) {
474 SectionHeader
->Misc
.VirtualSize
= Optional64
->DataDirectory
[5].Size
;
475 AllignedRelocSize
= (Optional64
->DataDirectory
[5].Size
+ Optional64
->FileAlignment
- 1) & (~(Optional64
->FileAlignment
- 1));
477 // Check to see if there is zero padding at the end of the base relocations
479 if (AllignedRelocSize
< SectionHeader
->SizeOfRawData
) {
481 // Check to see if the base relocations are at the end of the file
483 if (SectionHeader
->PointerToRawData
+ SectionHeader
->SizeOfRawData
== Optional64
->SizeOfImage
) {
485 // All the required conditions are met to strip the zero padding of the end of the base relocations section
487 Optional64
->SizeOfImage
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
488 Optional64
->SizeOfInitializedData
-= (SectionHeader
->SizeOfRawData
- AllignedRelocSize
);
489 SectionHeader
->SizeOfRawData
= AllignedRelocSize
;
490 FileLength
= Optional64
->SizeOfImage
;
499 FWriteFile (fpOut
, FileBuffer
, FileLength
);
507 // printf ("Created %s\n", OutImageName);
509 return STATUS_SUCCESS
;