]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/CCode/Source/PeiRebase/PeiRebaseExe.c
A minor mistake. Remove debug info.
[mirror_edk2.git] / Tools / CCode / Source / PeiRebase / PeiRebaseExe.c
index 27c646e486fab1b86699270498f41ec2e0fe001b..2d28a83066d7fc84b1b557d8e218a05ade7717a4 100644 (file)
@@ -100,6 +100,10 @@ Returns:
   EFI_FFS_FILE_HEADER         *CurrentFile;\r
   BOOLEAN                     ErasePolarity;\r
   EFI_PHYSICAL_ADDRESS        CurrentFileBaseAddress;\r
+  CHAR8                       InfFileName[_MAX_PATH];\r
+  CHAR8                       *InfFileImage;\r
+  UINTN                       InfFileSize;\r
+  MEMORY_FILE                 InfMemoryFile;\r
 \r
   ErasePolarity = FALSE;\r
   //\r
@@ -129,6 +133,9 @@ Returns:
   OutputFile        = NULL;\r
   MapFile           = NULL;\r
   FvImage           = NULL;\r
+  InfFileImage      = NULL;\r
+  InfFileSize       = 0;\r
+  strcpy (InfFileName, "");\r
 \r
   //\r
   // Parse the command line arguments\r
@@ -194,6 +201,52 @@ Returns:
       }\r
       break;\r
 \r
+    case 'F':\r
+    case 'f':\r
+      if (!BaseAddressSet) {\r
+        strcpy (InfFileName, argv[Index + 1]);\r
+        //\r
+        // Read the INF file image\r
+        //\r
+        Status = GetFileImage (InfFileName, &InfFileImage, &InfFileSize);\r
+        if (EFI_ERROR (Status)) {\r
+          PrintUsage ();\r
+          Error (NULL, 0, 0, argv[Index + 1], "-f FvInfFile can't be opened.");\r
+          return STATUS_ERROR;\r
+        }\r
+        //\r
+        // Initialize file structures\r
+        //\r
+        InfMemoryFile.FileImage           = InfFileImage;\r
+        InfMemoryFile.CurrentFilePointer  = InfFileImage;\r
+        InfMemoryFile.Eof                 = InfFileImage + InfFileSize;\r
+        //\r
+        // Read BaseAddress from fv.inf file.\r
+        //\r
+        FindToken (&InfMemoryFile, "[options]", "EFI_BASE_ADDRESS", 0, InfFileName);\r
+        //\r
+        // free Inf File Image\r
+        //\r
+        free (InfFileImage);\r
+        \r
+        //\r
+        // Convert string to UINT64 base address.\r
+        //\r
+        Status = AsciiStringToUint64 (InfFileName, FALSE, &BaseAddress);\r
+        if (EFI_ERROR (Status)) {\r
+          PrintUsage ();\r
+          Error (NULL, 0, 0, argv[Index + 1], "can't find the base address in the specified fv.inf file.");\r
+          return STATUS_ERROR;\r
+        }\r
+\r
+        BaseAddressSet = TRUE;\r
+      } else {\r
+        PrintUsage ();\r
+        Error (NULL, 0, 0, argv[Index + 1], "BaseAddress has been got once from fv.inf or the specified base address.");\r
+        return STATUS_ERROR;\r
+      }\r
+      break;\r
+\r
     case 'M':\r
     case 'm':\r
       if (strlen (MapFileName) == 0) {\r
@@ -212,7 +265,7 @@ Returns:
       break;\r
     }\r
   }\r
-\r
+  \r
   //\r
   // Create the Map file if we need it\r
   //\r
@@ -486,15 +539,18 @@ Returns:
 --*/\r
 {\r
   printf (\r
-    "Usage: %s -I InputFileName -O OutputFileName -B BaseAddress [-M MapFile]\n",\r
+    "Usage: %s -I InputFileName -O OutputFileName [-B BaseAddress] -F FvInfFileName -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 ("    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
+  printf ("    BaseAddress    is the FV base address to rebase agains.\n");\r
+  printf ("    FvInfFileName  is the fv.inf to be used to generate this fv image.\n");\r
+  printf ("                   BaseAddress can also be got from the fv.inf file.\n");\r
+  printf ("                   Choose only one method to input BaseAddress.\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
@@ -624,13 +680,13 @@ Returns:
     // Allocate a buffer for the image to be loaded into.\r
     //\r
     Pe32ImageSize       = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION);\r
-    MemoryImagePointer  = (UINTN) (malloc (Pe32ImageSize + 0x1000));\r
+    MemoryImagePointer  = (UINTN) (malloc (Pe32ImageSize + 0x10000));\r
     if (MemoryImagePointer == 0) {\r
       Error (NULL, 0, 0, "memory allocation failure", NULL);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
-    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000);\r
-    MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12);\r
+    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x10000);\r
+    MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFFF) & (-1 << 16);\r
     \r
 \r
     ImageContext.ImageAddress = MemoryImagePointerAligned;\r
@@ -641,6 +697,24 @@ Returns:
       free ((VOID *) MemoryImagePointer);\r
       return Status;\r
     }\r
+    \r
+    //\r
+    // Check if section-alignment and file-alignment match or not\r
+    //\r
+    if (!(ImageContext.IsTeImage)) {\r
+      PeHdr = (EFI_IMAGE_NT_HEADERS *)((UINTN)ImageContext.ImageAddress + \r
+                                              ImageContext.PeCoffHeaderOffset);\r
+      if (PeHdr->OptionalHeader.SectionAlignment != PeHdr->OptionalHeader.FileAlignment) {\r
+        Error (NULL, 0, 0, "Section-Alignment and File-Alignment does not match", FileGuidString);\r
+        free ((VOID *) MemoryImagePointer);\r
+        return EFI_ABORTED;\r
+      }\r
+    }\r
+    else {\r
+      //\r
+      // BUGBUG: TE Image Header lack section-alignment and file-alignment info\r
+      //\r
+    }\r
 \r
     ImageContext.DestinationAddress = NewPe32BaseAddress;\r
     Status                          = PeCoffLoaderRelocateImage (&ImageContext);\r
@@ -876,14 +950,14 @@ Returns:
     //\r
     // Allocate a buffer for the image to be loaded into.\r
     //\r
-    MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000));\r
+    MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x10000));\r
     if (MemoryImagePointer == 0) {\r
       Error (NULL, 0, 0, "memory allocation error on rebase of TE image", FileGuidString);\r
       free (TEBuffer);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
-    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000);\r
-    MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12);\r
+    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x10000);\r
+    MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFFF) & (-1 << 16);\r
     \r
 \r
     ImageContext.ImageAddress = MemoryImagePointerAligned;\r
@@ -894,6 +968,11 @@ Returns:
       free ((VOID *) MemoryImagePointer);\r
       return Status;\r
     }\r
+    \r
+    //\r
+    // Check if section-alignment and file-alignment match or not\r
+       // BUGBUG: TE Image Header lack section-alignment and file-alignment info\r
+    //\r
 \r
     ImageContext.DestinationAddress = NewPe32BaseAddress;\r
     Status                          = PeCoffLoaderRelocateImage (&ImageContext);\r