]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFw/GenFw.c
BaseTools X64: fold PLT relocations into simple relative references
[mirror_edk2.git] / BaseTools / Source / C / GenFw / GenFw.c
index 90ba2511022ba9fd1853fb1a59b35beb9f9afb69..03bfaa1c80e860d92b21eb8ac22ac6fbf6ba43da 100644 (file)
@@ -1,6 +1,7 @@
 /** @file\r
+Converts a pe32+ image to an FW, Te image type, or other specific image.\r
 \r
-Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -9,14 +10,6 @@ http://opensource.org/licenses/bsd-license.php
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-Module Name:\r
-\r
-    GenFw.c\r
-\r
-Abstract:\r
-\r
-    Converts a pe32+ image to an FW, Te image type, or other specific image.\r
-\r
 **/\r
 \r
 #include "WinNtInclude.h"\r
@@ -66,10 +59,6 @@ Abstract:
 #define DEFAULT_MC_PAD_BYTE_VALUE  0xFF\r
 #define DEFAULT_MC_ALIGNMENT       16\r
 \r
-#ifndef _MAX_PATH\r
-#define _MAX_PATH 500\r
-#endif\r
-\r
 #define STATUS_IGNORE 0xA\r
 //\r
 // Structure definition for a microcode header\r
@@ -147,7 +136,7 @@ Returns:
 \r
 --*/\r
 {\r
-  fprintf (stdout, "%s Version %d.%d\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
+  fprintf (stdout, "%s Version %d.%d %s \n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION, __BUILD_VERSION);\r
 }\r
 \r
 STATIC\r
@@ -179,7 +168,7 @@ Returns:
   //\r
   // Copyright declaration\r
   //\r
-  fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2015, Intel Corporation. All rights reserved.\n\n");\r
 \r
   //\r
   // Details Option\r
@@ -195,9 +184,9 @@ Returns:
                         PIC_PEIM, RELOCATABLE_PEIM, BS_DRIVER, RT_DRIVER,\n\\r
                         APPLICATION, SAL_RT_DRIVER to support all module types\n\\r
                         It can only be used together with --keepexceptiontable,\n\\r
-                        --keepzeropending, -r, -o option.It is a action option.\n\\r
-                        If it is combined with other action options, the later\n\\r
-                        input action option will override the previous one.\n");\r
+                        --keepzeropending, --keepoptionalheader, -r, -o option.\n\\r
+                        It is a action option. If it is combined with other action options,\n\\r
+                        the later input action option will override the previous one.\n");\r
   fprintf (stdout, "  -c, --acpi            Create Acpi table.\n\\r
                         It can't be combined with other action options\n\\r
                         except for -o, -r option. It is a action option.\n\\r
@@ -205,9 +194,9 @@ Returns:
                         input action option will override the previous one.\n");\r
   fprintf (stdout, "  -t, --terse           Create Te Image.\n\\r
                         It can only be used together with --keepexceptiontable,\n\\r
-                        --keepzeropending, -r, -o option.It is a action option.\n\\r
-                        If it is combined with other action options, the later\n\\r
-                        input action option will override the previous one.\n");\r
+                        --keepzeropending, --keepoptionalheader, -r, -o option.\n\\r
+                        It is a action option. If it is combined with other action options,\n\\r
+                        the later input action option will override the previous one.\n");\r
   fprintf (stdout, "  -u, --dump            Dump TeImage Header.\n\\r
                         It can't be combined with other action options\n\\r
                         except for -o, -r option. It is a action option.\n\\r
@@ -257,6 +246,9 @@ Returns:
   fprintf (stdout, "  --keepexceptiontable  Don't clear exception table.\n\\r
                         This option can be used together with -e or -t.\n\\r
                         It doesn't work for other options.\n");\r
+  fprintf (stdout, "  --keepoptionalheader  Don't zero PE/COFF optional header fields.\n\\r
+                        This option can be used together with -e or -t.\n\\r
+                        It doesn't work for other options.\n");\r
   fprintf (stdout, "  --keepzeropending     Don't strip zero pending of .reloc.\n\\r
                         This option can be used together with -e or -t.\n\\r
                         It doesn't work for other options.\n");\r
@@ -658,7 +650,10 @@ PeCoffConvertImageToXip (
         SectionHeader->SizeOfRawData\r
         );\r
     }\r
-    SectionHeader->SizeOfRawData = SectionHeader->Misc.VirtualSize;\r
+    //\r
+    // Make the size of raw data in section header alignment.\r
+    //\r
+    SectionHeader->SizeOfRawData = (SectionHeader->Misc.VirtualSize + PeHdr->Pe32.OptionalHeader.FileAlignment - 1) & (~(PeHdr->Pe32.OptionalHeader.FileAlignment - 1));\r
     SectionHeader->PointerToRawData = SectionHeader->VirtualAddress;\r
   }\r
 \r
@@ -898,13 +893,6 @@ Returns:
     }\r
   }\r
 \r
-  //\r
-  // No available section header is found.\r
-  //\r
-  if (Index == ImgHdr->Pe32.FileHeader.NumberOfSections) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-  \r
   //\r
   // BaseAddress is set to section header.\r
   //\r
@@ -1087,6 +1075,7 @@ Returns:
   STATUS                           Status;\r
   BOOLEAN                          ReplaceFlag;\r
   BOOLEAN                          KeepExceptionTableFlag;\r
+  BOOLEAN                          KeepOptionalHeaderFlag;\r
   BOOLEAN                          KeepZeroPendingFlag;\r
   UINT64                           LogLevel;\r
   EFI_TE_IMAGE_HEADER              TEImageHeader;\r
@@ -1149,6 +1138,7 @@ Returns:
   Optional32        = NULL;\r
   Optional64        = NULL;\r
   KeepExceptionTableFlag = FALSE;\r
+  KeepOptionalHeaderFlag = FALSE;\r
   KeepZeroPendingFlag    = FALSE;\r
   NumberOfFormPacakge    = 0;\r
   HiiPackageListBuffer   = NULL;\r
@@ -1277,6 +1267,13 @@ Returns:
       continue;\r
     }\r
 \r
+    if (stricmp(argv[0], "--keepoptionalheader") == 0) {\r
+      KeepOptionalHeaderFlag = TRUE;\r
+      argc--;\r
+      argv++;\r
+      continue;\r
+    }\r
+\r
     if (stricmp (argv[0], "--keepzeropending") == 0) {\r
       KeepZeroPendingFlag = TRUE;\r
       argc --;\r
@@ -1556,7 +1553,7 @@ Returns:
   // Open output file and Write image into the output file.\r
   //\r
   if (OutImageName != NULL) {\r
-    fpOut = fopen (OutImageName, "rb");\r
+    fpOut = fopen (LongFilePath (OutImageName), "rb");\r
     if (fpOut != NULL) {\r
       //\r
       // Get Output file time stamp\r
@@ -1587,7 +1584,7 @@ Returns:
   //\r
   // Open input file and read file data into file buffer.\r
   //\r
-  fpIn = fopen (mInImageName, "rb");\r
+  fpIn = fopen (LongFilePath (mInImageName), "rb");\r
   if (fpIn == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
     goto Finish;\r
@@ -1618,7 +1615,7 @@ Returns:
     //\r
     // Open output file handle.\r
     //\r
-    fpOut = fopen (OutImageName, "wb");\r
+    fpOut = fopen (LongFilePath (OutImageName), "wb");\r
     if (!fpOut) {\r
       Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
       goto Finish;\r
@@ -1628,7 +1625,7 @@ Returns:
     //\r
     HiiPackageListHeader.PackageLength = sizeof (EFI_HII_PACKAGE_LIST_HEADER);\r
     for (Index = 0; Index < InputFileNum; Index ++) {\r
-      fpIn = fopen (InputFileName [Index], "rb");\r
+      fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
       if (fpIn == NULL) {\r
         Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
         goto Finish;\r
@@ -1674,7 +1671,7 @@ Returns:
     memcpy (HiiPackageListBuffer, &HiiPackageListHeader, sizeof (HiiPackageListHeader));\r
     HiiPackageDataPointer = HiiPackageListBuffer + sizeof (HiiPackageListHeader);\r
     for (Index = 0; Index < InputFileNum; Index ++) {\r
-      fpIn = fopen (InputFileName [Index], "rb");\r
+      fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
       if (fpIn == NULL) {\r
         Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
         free (HiiPackageListBuffer);\r
@@ -1754,13 +1751,13 @@ Returns:
     //\r
     // Open output file handle.\r
     //\r
-    fpOut = fopen (OutImageName, "wb");\r
+    fpOut = fopen (LongFilePath (OutImageName), "wb");\r
     if (!fpOut) {\r
       Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
       goto Finish;\r
     }\r
     for (Index = 0; Index < InputFileNum; Index ++) {\r
-      fpIn = fopen (InputFileName [Index], "rb");\r
+      fpIn = fopen (LongFilePath (InputFileName [Index]), "rb");\r
       if (!fpIn) {\r
         Error (NULL, 0, 0001, "Error opening file", InputFileName [Index]);\r
         goto Finish;\r
@@ -1802,7 +1799,7 @@ Returns:
   // Convert MicroCode.txt file to MicroCode.bin file\r
   //\r
   if (mOutImageType == FW_MCI_IMAGE) {\r
-    fpIn = fopen (mInImageName, "r");\r
+    fpIn = fopen (LongFilePath (mInImageName), "r");\r
     if (fpIn == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
       goto Finish;\r
@@ -1925,14 +1922,14 @@ Returns:
     // Open the output file handle.\r
     //\r
     if (ReplaceFlag) {\r
-      fpInOut = fopen (mInImageName, "wb");\r
+      fpInOut = fopen (LongFilePath (mInImageName), "wb");\r
       if (fpInOut == NULL) {\r
         Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
         goto Finish;\r
       }\r
     } else {\r
       if (OutImageName != NULL) {\r
-        fpOut = fopen (OutImageName, "wb");\r
+        fpOut = fopen (LongFilePath (OutImageName), "wb");\r
       } else {\r
         fpOut = stdout;\r
       }\r
@@ -2311,19 +2308,20 @@ Returns:
 \r
   if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->Pe32.OptionalHeader;\r
-    Optional32->MajorOperatingSystemVersion = 0;\r
-    Optional32->MinorOperatingSystemVersion = 0;\r
-    Optional32->MajorImageVersion           = 0;\r
-    Optional32->MinorImageVersion           = 0;\r
-    Optional32->MajorSubsystemVersion       = 0;\r
-    Optional32->MinorSubsystemVersion       = 0;\r
-    Optional32->Win32VersionValue           = 0;\r
-    Optional32->CheckSum                    = 0;\r
-    Optional32->SizeOfStackReserve = 0;\r
-    Optional32->SizeOfStackCommit  = 0;\r
-    Optional32->SizeOfHeapReserve  = 0;\r
-    Optional32->SizeOfHeapCommit   = 0;\r
-\r
+    if (!KeepOptionalHeaderFlag) {\r
+      Optional32->MajorOperatingSystemVersion = 0;\r
+      Optional32->MinorOperatingSystemVersion = 0;\r
+      Optional32->MajorImageVersion = 0;\r
+      Optional32->MinorImageVersion = 0;\r
+      Optional32->MajorSubsystemVersion = 0;\r
+      Optional32->MinorSubsystemVersion = 0;\r
+      Optional32->Win32VersionValue = 0;\r
+      Optional32->CheckSum = 0;\r
+      Optional32->SizeOfStackReserve = 0;\r
+      Optional32->SizeOfStackCommit = 0;\r
+      Optional32->SizeOfHeapReserve = 0;\r
+      Optional32->SizeOfHeapCommit = 0;\r
+    }\r
     TEImageHeader.AddressOfEntryPoint = Optional32->AddressOfEntryPoint;\r
     TEImageHeader.BaseOfCode          = Optional32->BaseOfCode;\r
     TEImageHeader.ImageBase           = (UINT64) (Optional32->ImageBase);\r
@@ -2403,19 +2401,20 @@ Returns:
     }\r
   } else if (PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
     Optional64 = (EFI_IMAGE_OPTIONAL_HEADER64 *)&PeHdr->Pe32.OptionalHeader;\r
-    Optional64->MajorOperatingSystemVersion = 0;\r
-    Optional64->MinorOperatingSystemVersion = 0;\r
-    Optional64->MajorImageVersion           = 0;\r
-    Optional64->MinorImageVersion           = 0;\r
-    Optional64->MajorSubsystemVersion       = 0;\r
-    Optional64->MinorSubsystemVersion       = 0;\r
-    Optional64->Win32VersionValue           = 0;\r
-    Optional64->CheckSum                    = 0;\r
-    Optional64->SizeOfStackReserve = 0;\r
-    Optional64->SizeOfStackCommit  = 0;\r
-    Optional64->SizeOfHeapReserve  = 0;\r
-    Optional64->SizeOfHeapCommit   = 0;\r
-\r
+    if (!KeepOptionalHeaderFlag) {\r
+      Optional64->MajorOperatingSystemVersion = 0;\r
+      Optional64->MinorOperatingSystemVersion = 0;\r
+      Optional64->MajorImageVersion = 0;\r
+      Optional64->MinorImageVersion = 0;\r
+      Optional64->MajorSubsystemVersion = 0;\r
+      Optional64->MinorSubsystemVersion = 0;\r
+      Optional64->Win32VersionValue = 0;\r
+      Optional64->CheckSum = 0;\r
+      Optional64->SizeOfStackReserve = 0;\r
+      Optional64->SizeOfStackCommit = 0;\r
+      Optional64->SizeOfHeapReserve = 0;\r
+      Optional64->SizeOfHeapCommit = 0;\r
+    }\r
     TEImageHeader.AddressOfEntryPoint = Optional64->AddressOfEntryPoint;\r
     TEImageHeader.BaseOfCode          = Optional64->BaseOfCode;\r
     TEImageHeader.ImageBase           = (UINT64) (Optional64->ImageBase);\r
@@ -2638,7 +2637,7 @@ WriteFile:
       //\r
       // Update File when File is changed.\r
       //\r
-      fpInOut = fopen (mInImageName, "wb");\r
+      fpInOut = fopen (LongFilePath (mInImageName), "wb");\r
       if (fpInOut == NULL) {\r
         Error (NULL, 0, 0001, "Error opening file", mInImageName);\r
         goto Finish;\r
@@ -2651,7 +2650,7 @@ WriteFile:
       //\r
       // Update File when File is changed or File is old.\r
       //\r
-      fpOut = fopen (OutImageName, "wb");\r
+      fpOut = fopen (LongFilePath (OutImageName), "wb");\r
       if (fpOut == NULL) {\r
         Error (NULL, 0, 0001, "Error opening output file", OutImageName);\r
         goto Finish;\r
@@ -2693,7 +2692,7 @@ Finish:
       if (OutputFileBuffer == NULL) {\r
         remove (OutImageName);\r
       } else {\r
-        fpOut = fopen (OutImageName, "wb");\r
+        fpOut = fopen (LongFilePath (OutImageName), "wb");\r
         fwrite (OutputFileBuffer, 1, OutputFileLength, fpOut);\r
         fclose (fpOut);\r
       }\r
@@ -2719,7 +2718,7 @@ Finish:
     if (ReportFileName != NULL) {\r
       strcpy (ReportFileName, OutImageName);\r
       strcpy (ReportFileName + (FileLen - 4), ".txt"); \r
-      ReportFile = fopen (ReportFileName, "w+");\r
+      ReportFile = fopen (LongFilePath (ReportFileName), "w+");\r
       if (ReportFile != NULL) {\r
         fprintf (ReportFile, "MODULE_SIZE = %u\n", (unsigned) mImageSize);\r
         fprintf (ReportFile, "TIME_STAMP = %u\n", (unsigned) mImageTimeStamp);\r
@@ -2797,8 +2796,8 @@ Returns:
   // Get Debug, Export and Resource EntryTable RVA address.\r
   // Resource Directory entry need to review.\r
   //\r
-  if (FileHdr->Machine == EFI_IMAGE_MACHINE_IA32) {\r
-    Optional32Hdr = (EFI_IMAGE_OPTIONAL_HEADER32 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));\r
+  Optional32Hdr = (EFI_IMAGE_OPTIONAL_HEADER32 *) ((UINT8*) FileHdr + sizeof (EFI_IMAGE_FILE_HEADER));\r
+  if (Optional32Hdr->Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINT8 *) Optional32Hdr +  FileHdr->SizeOfOptionalHeader);\r
     if (Optional32Hdr->NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_EXPORT && \\r
         Optional32Hdr->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_EXPORT].Size != 0) {\r