]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenSec/GenSec.c
Sync EDKII BaseTools to BaseTools project r1903.
[mirror_edk2.git] / BaseTools / Source / C / GenSec / GenSec.c
index 9a1d0ca48a831522b4596ac93851760bcf765d73..8c4f8743fd0259aa60673a6fa06145ddae386883 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation                                                         \r
+Copyright (c) 2004 - 2010, Intel Corporation                                                         \r
 All rights reserved. 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
@@ -27,6 +27,7 @@ Abstract:
 #include <Common/UefiBaseTypes.h>\r
 #include <Common/PiFirmwareFile.h>\r
 #include <Protocol/GuidedSectionExtraction.h>\r
+#include <IndustryStandard/PeImage.h>\r
 \r
 #include "CommonLib.h"\r
 #include "Compress.h"\r
@@ -80,6 +81,11 @@ STATIC CHAR8      *mCompressionTypeName[]    = { "PI_NONE", "PI_STD" };
 #define EFI_GUIDED_SECTION_NONE 0x80\r
 STATIC CHAR8      *mGUIDedSectionAttribue[]  = { "NONE", "PROCESSING_REQUIRED", "AUTH_STATUS_VALID"};\r
 \r
+STATIC CHAR8 *mAlignName[] = {\r
+  "1", "2", "4", "8", "16", "32", "64", "128", "256", "512",\r
+  "1K", "2K", "4K", "8K", "16K", "32K", "64K"\r
+};\r
+\r
 //\r
 // Crc32 GUID section related definitions.\r
 //\r
@@ -144,7 +150,7 @@ Returns:
   //\r
   // Copyright declaration\r
   // \r
-  fprintf (stdout, "Copyright (c) 2007, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
 \r
   //\r
   // Details Option\r
@@ -180,6 +186,10 @@ Returns:
   fprintf (stdout, "  -j Number, --buildnumber Number\n\\r
                         Number is an integer value between 0000 and 9999\n\\r
                         used in Ver section.\n");\r
+  fprintf (stdout, "  --sectionalign SectionAlign\n\\r
+                        SectionAlign points to section alignment, which support\n\\r
+                        the alignment scope 1~64K. It is specified in same\n\\r
+                        order that the section file is input.\n");\r
   fprintf (stdout, "  -v, --verbose         Turn on verbose output with informational messages.\n");\r
   fprintf (stdout, "  -q, --quiet           Disable all messages except key message and fatal error\n");\r
   fprintf (stdout, "  -d, --debug level     Enable debug messages, at input debug level.\n");\r
@@ -329,9 +339,50 @@ Done:
   return Status;\r
 }\r
 \r
+STATIC\r
+EFI_STATUS\r
+StringtoAlignment (\r
+  IN  CHAR8  *AlignBuffer,\r
+  OUT UINT32 *AlignNumber\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Converts Align String to align value (1~64K). \r
+\r
+Arguments:\r
+\r
+  AlignBuffer    - Pointer to Align string.\r
+  AlignNumber    - Pointer to Align value.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             Successfully convert align string to align value.\r
+  EFI_INVALID_PARAMETER   Align string is invalid or align value is not in scope.\r
+\r
+--*/\r
+{\r
+  UINT32 Index = 0;\r
+  //\r
+  // Check AlignBuffer\r
+  //\r
+  if (AlignBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  for (Index = 0; Index < sizeof (mAlignName) / sizeof (CHAR8 *); Index ++) {\r
+    if (stricmp (AlignBuffer, mAlignName [Index]) == 0) {\r
+      *AlignNumber = 1 << Index;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  return EFI_INVALID_PARAMETER;\r
+}\r
+\r
 EFI_STATUS\r
 GetSectionContents (\r
   CHAR8   **InputFileName,\r
+  UINT32  *InputFileAlign,\r
   UINT32  InputFileNum,\r
   UINT8   *FileBuffer,\r
   UINT32  *BufferLength\r
@@ -346,7 +397,9 @@ Routine Description:
 Arguments:\r
                \r
   InputFileName  - Name of the input file.\r
-                \r
+\r
+  InputFileAlign - Alignment required by the input file data.\r
+\r
   InputFileNum   - Number of input files. Should be at least 1.\r
 \r
   FileBuffer     - Output buffer to contain data\r
@@ -362,10 +415,17 @@ Returns:
   EFI_BUFFER_TOO_SMALL FileBuffer is not enough to contain all file data.\r
 --*/\r
 {\r
-  UINT32   Size;\r
-  UINT32   FileSize;\r
-  UINT32   Index;\r
-  FILE    *InFile;\r
+  UINT32                     Size;\r
+  UINT32                     Offset;\r
+  UINT32                     FileSize;\r
+  UINT32                     Index;\r
+  FILE                       *InFile;\r
+  EFI_COMMON_SECTION_HEADER  *SectHeader;\r
+  EFI_COMMON_SECTION_HEADER  TempSectHeader;\r
+  EFI_TE_IMAGE_HEADER        TeHeader;\r
+  UINT32                     TeOffset;\r
+  EFI_GUID_DEFINED_SECTION   GuidSectHeader;\r
+  UINT32                     HeaderSize;\r
 \r
   if (InputFileNum < 1) {\r
     Error (NULL, 0, 2000, "Invalid paramter", "must specify at least one input file");\r
@@ -377,7 +437,9 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  Size = 0;\r
+  Size          = 0;\r
+  Offset        = 0;\r
+  TeOffset      = 0;\r
   //\r
   // Go through our array of file names and copy their contents\r
   // to the output buffer.\r
@@ -406,11 +468,66 @@ Returns:
     FileSize = ftell (InFile);\r
     fseek (InFile, 0, SEEK_SET);\r
     DebugMsg (NULL, 0, 9, "Input files", "the input file name is %s and the size is %u bytes", InputFileName[Index], (unsigned) FileSize); \r
+    //\r
+    // Adjust section buffer when section alignment is required.\r
+    //\r
+    if (InputFileAlign != NULL) {\r
+      //\r
+      // Check this section is Te/Pe section, and Calculate the numbers of Te/Pe section.\r
+      //\r
+      TeOffset = 0;\r
+      HeaderSize = sizeof (EFI_COMMON_SECTION_HEADER);\r
+      fread (&TempSectHeader, 1, sizeof (TempSectHeader), InFile);\r
+      if (TempSectHeader.Type == EFI_SECTION_TE) {\r
+        fread (&TeHeader, 1, sizeof (TeHeader), InFile);\r
+        if (TeHeader.Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {\r
+          TeOffset = TeHeader.StrippedSize - sizeof (TeHeader);\r
+        }\r
+      } else if (TempSectHeader.Type == EFI_SECTION_GUID_DEFINED) {\r
+        fseek (InFile, 0, SEEK_SET);\r
+        fread (&GuidSectHeader, 1, sizeof (GuidSectHeader), InFile);\r
+        if ((GuidSectHeader.Attributes & EFI_GUIDED_SECTION_PROCESSING_REQUIRED) == 0) {\r
+          HeaderSize = GuidSectHeader.DataOffset;\r
+        }\r
+      } \r
+\r
+      fseek (InFile, 0, SEEK_SET);\r
+\r
+      //\r
+      // Revert TeOffset to the converse value relative to Alignment\r
+      // This is to assure the original PeImage Header at Alignment.\r
+      //\r
+      if (TeOffset != 0) {\r
+        TeOffset = InputFileAlign [Index] - (TeOffset % InputFileAlign [Index]);\r
+        TeOffset = TeOffset % InputFileAlign [Index];\r
+      }\r
+\r
+      //\r
+      // make sure section data meet its alignment requirement by adding one raw pad section.\r
+      //\r
+      if ((InputFileAlign [Index] != 0) && (((Size + HeaderSize + TeOffset) % InputFileAlign [Index]) != 0)) {\r
+        Offset = (Size + sizeof (EFI_COMMON_SECTION_HEADER) + HeaderSize + TeOffset + InputFileAlign [Index] - 1) & ~(InputFileAlign [Index] - 1);\r
+        Offset = Offset - Size - HeaderSize - TeOffset;\r
+         \r
+        if (FileBuffer != NULL && ((Size + Offset) < *BufferLength)) {\r
+          memset (FileBuffer + Size, 0, Offset);\r
+          SectHeader          = (EFI_COMMON_SECTION_HEADER *) (FileBuffer + Size);\r
+          SectHeader->Type    = EFI_SECTION_RAW;\r
+          SectHeader->Size[0] = (UINT8) (Offset & 0xff);\r
+          SectHeader->Size[1] = (UINT8) ((Offset & 0xff00) >> 8);\r
+          SectHeader->Size[2] = (UINT8) ((Offset & 0xff0000) >> 16);\r
+        }\r
+        DebugMsg (NULL, 0, 9, "Pad raw section for section data alignment", "Pad Raw section size is %u", (unsigned) Offset);\r
+\r
+        Size = Size + Offset;\r
+      }\r
+    }\r
+\r
     //\r
     // Now read the contents of the file into the buffer\r
     // Buffer must be enough to contain the file content.\r
     //\r
-    if (FileSize > 0 && FileBuffer != NULL && (Size + FileSize) <= *BufferLength) {\r
+    if ((FileSize > 0) && (FileBuffer != NULL) && ((Size + FileSize) <= *BufferLength)) {\r
       if (fread (FileBuffer + Size, (size_t) FileSize, 1, InFile) != 1) {\r
         Error (NULL, 0, 0004, "Error reading file", InputFileName[Index]);\r
         fclose (InFile);\r
@@ -437,6 +554,7 @@ Returns:
 EFI_STATUS\r
 GenSectionCompressionSection (\r
   CHAR8   **InputFileName,\r
+  UINT32  *InputFileAlign,\r
   UINT32  InputFileNum,\r
   UINT8   SectCompSubType,\r
   UINT8   **OutFileBuffer\r
@@ -453,7 +571,9 @@ Routine Description:
 Arguments:\r
                \r
   InputFileName  - Name of the input file.\r
-                \r
+\r
+  InputFileAlign - Alignment required by the input file data.\r
+\r
   InputFileNum   - Number of input files. Should be at least 1.\r
 \r
   SectCompSubType - Specify the compression algorithm requested. \r
@@ -487,6 +607,7 @@ Returns:
   //\r
   Status = GetSectionContents (\r
             InputFileName,\r
+            InputFileAlign,\r
             InputFileNum,\r
             FileBuffer,\r
             &InputLength\r
@@ -503,6 +624,7 @@ Returns:
     //\r
     Status = GetSectionContents (\r
               InputFileName,\r
+              InputFileAlign,\r
               InputFileNum,\r
               FileBuffer,\r
               &InputLength\r
@@ -524,6 +646,16 @@ Returns:
   switch (SectCompSubType) {\r
   case EFI_NOT_COMPRESSED:\r
     CompressedLength = InputLength;\r
+    //\r
+    // Copy file buffer to the none compressed data.\r
+    //\r
+    OutputBuffer = malloc (CompressedLength + sizeof (EFI_COMPRESSION_SECTION));\r
+    if (OutputBuffer == NULL) {\r
+      free (FileBuffer);\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+    memcpy (OutputBuffer + sizeof (EFI_COMPRESSION_SECTION), FileBuffer, CompressedLength);\r
+    FileBuffer = OutputBuffer;\r
     break;\r
 \r
   case EFI_STANDARD_COMPRESSION:\r
@@ -599,6 +731,7 @@ Returns:
 EFI_STATUS\r
 GenSectionGuidDefinedSection (\r
   CHAR8    **InputFileName,\r
+  UINT32   *InputFileAlign,\r
   UINT32   InputFileNum,\r
   EFI_GUID *VendorGuid,\r
   UINT16   DataAttribute,\r
@@ -618,6 +751,8 @@ Arguments:
                \r
   InputFileName - Name of the input file.\r
                 \r
+  InputFileAlign - Alignment required by the input file data.\r
+\r
   InputFileNum  - Number of input files. Should be at least 1.\r
 \r
   VendorGuid    - Specify vendor guid value.\r
@@ -662,6 +797,7 @@ Returns:
   //\r
   Status = GetSectionContents (\r
             InputFileName,\r
+            InputFileAlign,\r
             InputFileNum,\r
             FileBuffer,\r
             &InputLength\r
@@ -678,6 +814,7 @@ Returns:
     //\r
     Status = GetSectionContents (\r
               InputFileName,\r
+              InputFileAlign,\r
               InputFileNum,\r
               FileBuffer + Offset,\r
               &InputLength\r
@@ -797,7 +934,11 @@ Returns:
   UINT8                     *OutFileBuffer;\r
   EFI_STATUS                Status;\r
   UINT64                    LogLevel;\r
-  \r
+  UINT32                    *InputFileAlign;\r
+  UINT32                    InputFileAlignNum;\r
+\r
+  InputFileAlign        = NULL;\r
+  InputFileAlignNum     = 0;\r
   InputFileName         = NULL;\r
   OutputFileName        = NULL;\r
   SectionName           = NULL;\r
@@ -809,7 +950,7 @@ Returns:
   InputFileNum          = 0;\r
   SectType              = EFI_SECTION_ALL;\r
   SectCompSubType       = 0;\r
-  SectGuidAttribute     = 0;\r
+  SectGuidAttribute     = EFI_GUIDED_SECTION_NONE;\r
   OutFileBuffer         = NULL;\r
   InputLength           = 0;\r
   Status                = STATUS_SUCCESS;\r
@@ -983,6 +1124,41 @@ Returns:
       continue;\r
     }\r
 \r
+    //\r
+    // Section File alignment requirement\r
+    //\r
+    if (stricmp (argv[0], "--sectionalign") == 0) {\r
+      if (InputFileAlignNum == 0) {\r
+        InputFileAlign = (UINT32 *) malloc (MAXIMUM_INPUT_FILE_NUM * sizeof (UINT32));\r
+        if (InputFileAlign == NULL) {\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return 1;\r
+        }\r
+        memset (InputFileAlign, 1, MAXIMUM_INPUT_FILE_NUM * sizeof (UINT32));\r
+      } else if (InputFileAlignNum % MAXIMUM_INPUT_FILE_NUM == 0) {\r
+        InputFileAlign = (UINT32 *) realloc (\r
+          InputFileAlign,\r
+          (InputFileNum + MAXIMUM_INPUT_FILE_NUM) * sizeof (UINT32)\r
+          );\r
+\r
+        if (InputFileAlign == NULL) {\r
+          Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+          return 1;\r
+        }\r
+        memset (&(InputFileAlign[InputFileNum]), 1, (MAXIMUM_INPUT_FILE_NUM * sizeof (UINT32)));\r
+      }\r
+      \r
+      Status = StringtoAlignment (argv[1], &(InputFileAlign[InputFileAlignNum]));\r
+      if (EFI_ERROR (Status)) {\r
+        Error (NULL, 0, 1003, "Invalid option value", "%s = %s", argv[0], argv[1]);\r
+        goto Finish;\r
+      }\r
+      argc -= 2;\r
+      argv += 2;\r
+      InputFileAlignNum ++;\r
+      continue; \r
+    }\r
+\r
     //\r
     // Get Input file name\r
     //\r
@@ -992,7 +1168,6 @@ Returns:
         Error (NULL, 0, 4001, "Resource", "memory cannot be allcoated");\r
         return 1;\r
       }\r
-\r
       memset (InputFileName, 0, (MAXIMUM_INPUT_FILE_NUM * sizeof (CHAR8 *)));\r
     } else if (InputFileNum % MAXIMUM_INPUT_FILE_NUM == 0) {\r
       //\r
@@ -1007,7 +1182,6 @@ Returns:
         Error (NULL, 0, 4001, "Resource", "memory cannot be allcoated");\r
         return 1;\r
       }\r
-\r
       memset (&(InputFileName[InputFileNum]), 0, (MAXIMUM_INPUT_FILE_NUM * sizeof (CHAR8 *)));\r
     }\r
 \r
@@ -1016,6 +1190,11 @@ Returns:
     argv ++;\r
   }\r
 \r
+  if (InputFileAlignNum > 0 && InputFileAlignNum != InputFileNum) {\r
+    Error (NULL, 0, 1003, "Invalid option", "section alignment must be set for each section");\r
+    goto Finish;\r
+  }\r
+\r
   VerboseMsg ("%s tool start.", UTILITY_NAME);\r
 \r
   //\r
@@ -1050,14 +1229,11 @@ Returns:
       memcpy (&VendorGuid, &mEfiCrc32SectionGuid, sizeof (EFI_GUID));\r
     }\r
     \r
-    if (SectGuidAttribute == 0) {\r
-      SectGuidAttribute = EFI_GUIDED_SECTION_PROCESSING_REQUIRED;\r
-    }\r
     if ((SectGuidAttribute & EFI_GUIDED_SECTION_NONE) != 0) {\r
       //\r
       // NONE attribute, clear attribute value.\r
       //\r
-      SectGuidAttribute = 0;\r
+      SectGuidAttribute = SectGuidAttribute & ~EFI_GUIDED_SECTION_NONE;\r
     }\r
     VerboseMsg ("Vendor Guid is %08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", \r
                 (unsigned) VendorGuid.Data1,\r
@@ -1161,8 +1337,13 @@ Returns:
   //\r
   switch (SectType) {\r
   case EFI_SECTION_COMPRESSION:\r
+    if (InputFileAlign != NULL) {\r
+      free (InputFileAlign);\r
+      InputFileAlign = NULL;\r
+    }\r
     Status = GenSectionCompressionSection (\r
               InputFileName,\r
+              InputFileAlign,\r
               InputFileNum,\r
               SectCompSubType,\r
               &OutFileBuffer\r
@@ -1170,8 +1351,17 @@ Returns:
     break;\r
 \r
   case EFI_SECTION_GUID_DEFINED:\r
+    if (InputFileAlign != NULL && (CompareGuid (&VendorGuid, &mEfiCrc32SectionGuid) != 0)) {\r
+      //\r
+      // Only process alignment for the default known CRC32 guided section.\r
+      // For the unknown guided section, the alignment is processed when the dummy all section (EFI_SECTION_ALL) is generated.\r
+      //\r
+      free (InputFileAlign);\r
+      InputFileAlign = NULL;\r
+    }\r
     Status = GenSectionGuidDefinedSection (\r
               InputFileName,\r
+              InputFileAlign,\r
               InputFileNum,\r
               &VendorGuid,\r
               SectGuidAttribute,\r
@@ -1232,6 +1422,7 @@ Returns:
     //\r
     Status = GetSectionContents (\r
               InputFileName,\r
+              InputFileAlign,\r
               InputFileNum,\r
               OutFileBuffer,\r
               &InputLength\r
@@ -1248,6 +1439,7 @@ Returns:
       //\r
       Status = GetSectionContents (\r
                 InputFileName,\r
+                InputFileAlign,\r
                 InputFileNum,\r
                 OutFileBuffer,\r
                 &InputLength\r
@@ -1296,6 +1488,10 @@ Finish:
     free (InputFileName);\r
   }\r
 \r
+  if (InputFileAlign != NULL) {\r
+    free (InputFileAlign);\r
+  }\r
+\r
   if (OutFileBuffer != NULL) {\r
     free (OutFileBuffer);\r
   }\r