]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c
Updated PeiRebase to produce a map file of the relocations done by this tool
[mirror_edk2.git] / Tools / Source / TianoTools / PeiRebase / PeiRebaseExe.c
index 7dc95d60afd4182abb9a0ecb8afe680c32643e4e..cc07fa06bb98aa2b2f0fc217af961aea1f6d9a2a 100644 (file)
@@ -1,13 +1,13 @@
 /*++\r
 \r
-Copyright (c)  1999 - 2005 Intel Corporation. All rights reserved\r
-This software and associated documentation (if any) is furnished\r
-under a license and may only be used or copied in accordance\r
-with the terms of the license. Except as permitted by such\r
-license, no part of this software or documentation may be\r
-reproduced, stored in a retrieval system, or transmitted in any\r
-form or by any means without the express written consent of\r
-Intel Corporation.\r
+Copyright (c)  1999-2006 Intel Corporation. All rights reserved\r
+This program and the accompanying materials are licensed and made available \r
+under the terms and conditions of the BSD License which accompanies this \r
+distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 \r
 Module Name:\r
@@ -22,19 +22,19 @@ Abstract:
 \r
 --*/\r
 \r
-#include <UefiBaseTypes.h>\r
-#include <Base.h>\r
 #include <stdlib.h>\r
 #include <stdio.h>\r
 #include <string.h>\r
+\r
+#include <Common/UefiBaseTypes.h>\r
+#include <Common/FirmwareVolumeImageFormat.h>\r
+#include <Common/FirmwareFileSystem.h>\r
+#include <Library/PeCoffLib.h>\r
+\r
 #include "CommonLib.h"\r
 #include "ParseInf.h"\r
 #include "FvLib.h"\r
-\r
 #include "EfiUtilityMsgs.h"\r
-#include "FirmwareFileSystem.h"\r
-#include "PeCoffLib.h"\r
-\r
 #include "PeiRebaseExe.h"\r
 \r
 EFI_STATUS\r
@@ -63,11 +63,13 @@ Arguments:
                   format is a hex number preceded by 0x.\r
   InputFileName   The name of the input FV file.\r
   OutputFileName  The name of the output FV file.\r
+  MapFileName     The name of the map file of relocation info.\r
 \r
   Arguments come in pair in any order.\r
     -I InputFileName \r
     -O OutputFileName\r
     -B BaseAddress \r
+    -M MapFileName \r
 \r
 Returns:\r
 \r
@@ -83,11 +85,13 @@ Returns:
   UINT8                       Index;\r
   CHAR8                       InputFileName[_MAX_PATH];\r
   CHAR8                       OutputFileName[_MAX_PATH];\r
+  CHAR8                       MapFileName[_MAX_PATH];\r
   EFI_PHYSICAL_ADDRESS        BaseAddress;\r
   BOOLEAN                     BaseAddressSet;\r
   EFI_STATUS                  Status;\r
   FILE                        *InputFile;\r
   FILE                        *OutputFile;\r
+  FILE                        *MapFile;\r
   UINT64                      FvOffset;\r
   UINT32                      FileCount;\r
   int                         BytesRead;\r
@@ -109,11 +113,13 @@ Returns:
     PrintUsage ();\r
     return STATUS_ERROR;\r
   }\r
+\r
   //\r
   // Initialize variables\r
   //\r
   InputFileName[0]  = 0;\r
   OutputFileName[0] = 0;\r
+  MapFileName[0]    = 0;\r
   BaseAddress       = 0;\r
   BaseAddressSet    = FALSE;\r
   FvOffset          = 0;\r
@@ -121,7 +127,9 @@ Returns:
   ErasePolarity     = FALSE;\r
   InputFile         = NULL;\r
   OutputFile        = NULL;\r
+  MapFile           = NULL;\r
   FvImage           = NULL;\r
+\r
   //\r
   // Parse the command line arguments\r
   //\r
@@ -186,6 +194,17 @@ Returns:
       }\r
       break;\r
 \r
+    case 'M':\r
+    case 'm':\r
+      if (strlen (MapFileName) == 0) {\r
+        strcpy (MapFileName, argv[Index + 1]);\r
+      } else {\r
+        PrintUsage ();\r
+        Error (NULL, 0, 0, argv[Index + 1], "only one -m MapFileName may be specified");\r
+        return STATUS_ERROR;\r
+      }\r
+      break;\r
+\r
     default:\r
       PrintUsage ();\r
       Error (NULL, 0, 0, argv[Index], "unrecognized argument");\r
@@ -193,6 +212,18 @@ Returns:
       break;\r
     }\r
   }\r
+\r
+  //\r
+  // Create the Map file if we need it\r
+  //\r
+  if (strlen (MapFileName) != 0) {\r
+    MapFile = fopen (MapFileName, "w");\r
+    if (MapFile == NULL) {\r
+      Error (NULL, 0, 0, MapFileName, "failed to open map file");\r
+      goto Finish;\r
+    }\r
+  } \r
+\r
   //\r
   // Open the file containing the FV\r
   //\r
@@ -247,7 +278,7 @@ Returns:
     // Rebase this file\r
     //\r
     CurrentFileBaseAddress  = BaseAddress + ((UINTN) CurrentFile - (UINTN) FvImage);\r
-    Status                  = FfsRebase (CurrentFile, CurrentFileBaseAddress);\r
+    Status                  = FfsRebase (CurrentFile, CurrentFileBaseAddress, MapFile);\r
 \r
     if (EFI_ERROR (Status)) {\r
       switch (Status) {\r
@@ -275,6 +306,7 @@ Returns:
 \r
       goto Finish;\r
     }\r
+\r
     //\r
     // Get the next file\r
     //\r
@@ -314,6 +346,10 @@ Finish:
     fclose (OutputFile);\r
   }\r
 \r
+  if (MapFile != NULL) {\r
+    fclose (MapFile);\r
+  }\r
+\r
   if (FvImage != NULL) {\r
     free (FvImage);\r
   }\r
@@ -450,20 +486,22 @@ Returns:
 --*/\r
 {\r
   printf (\r
-    "Usage: %s -I InputFileName -O OutputFileName -B BaseAddress\n",\r
+    "Usage: %s -I InputFileName -O OutputFileName -B BaseAddress [-M MapFile]\n",\r
     UTILITY_NAME\r
     );\r
   printf ("  Where:\n");\r
   printf ("    InputFileName is the name of the EFI FV file to rebase.\n");\r
   printf ("    OutputFileName is the desired output file name.\n");\r
   printf ("    BaseAddress is the FV base address to rebase agains.\n");\r
+  printf ("    MapFileName is an optional map file of the relocations\n");\r
   printf ("  Argument pair may be in any order.\n\n");\r
 }\r
 \r
 EFI_STATUS\r
 FfsRebase (\r
   IN OUT EFI_FFS_FILE_HEADER    *FfsFile,\r
-  IN EFI_PHYSICAL_ADDRESS       BaseAddress\r
+  IN EFI_PHYSICAL_ADDRESS       BaseAddress,\r
+  IN FILE                       *MapFile      OPTIONAL\r
   )\r
 /*++\r
 \r
@@ -476,6 +514,7 @@ Arguments:
 \r
   FfsFile           A pointer to Ffs file image.\r
   BaseAddress       The base address to use for rebasing the file image.\r
+  MapFile           Optional file to dump relocation information into\r
 \r
 Returns:\r
 \r
@@ -516,6 +555,7 @@ Returns:
   if (FfsFile == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+  \r
   //\r
   // Convert the GUID to a string so we can at least report which file\r
   // if we find an error.\r
@@ -526,6 +566,7 @@ Returns:
   } else {\r
     TailSize = 0;\r
   }\r
+  \r
   //\r
   // Do some cursory checks on the FFS file contents\r
   //\r
@@ -534,6 +575,9 @@ Returns:
     Error (NULL, 0, 0, "file does not appear to be a valid FFS file, cannot be rebased", FileGuidString);\r
     return EFI_INVALID_PARAMETER;\r
   }\r
+\r
+  memset (&ImageContext, 0, sizeof (ImageContext));\r
+\r
   //\r
   // Check if XIP file type. If not XIP, don't rebase.\r
   //\r
@@ -544,6 +588,7 @@ Returns:
       ) {\r
     return EFI_SUCCESS;\r
   }\r
+\r
   //\r
   // Rebase each PE32 section\r
   //\r
@@ -928,6 +973,18 @@ Returns:
     }\r
   }\r
 \r
+  //\r
+  // If a map file was selected output mapping information for any file that\r
+  // was rebased.\r
+  //\r
+  if (MapFile != NULL) {\r
+    fprintf (MapFile, "File: %s Base:%08lx", FileGuidString, BaseAddress);\r
+    if (ImageContext.PdbPointer != NULL) {\r
+      fprintf (MapFile, " FileName: %s", ImageContext.PdbPointer);\r
+    }\r
+    fprintf (MapFile, "\n");\r
+  }\r
+  \r
   return EFI_SUCCESS;\r
 }\r
 \r