]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: use GUID identifiable section for FFS alignment padding
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Mon, 27 Jul 2015 13:50:19 +0000 (13:50 +0000)
committerabiesheuvel <abiesheuvel@Edk2>
Mon, 27 Jul 2015 13:50:19 +0000 (13:50 +0000)
Instead of using an anonymous section of type EFI_SECTION_RAW to pad
out the first aligned FFS section to its required alignment, use a
section with a dedicated GUID if the size of the padding permits it.

This allows for more flexibility when placing such FFS images in a
firmware volume, because we will now be able to remove padding rather
than add more, by shrinking the size of this section instead of
padding out the start of the FFS image to file alignment.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Yingke Liu <yingke.d.liu@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18079 6f19259b-4bc3-4df7-8a09-765794883524

BaseTools/Source/C/GenFfs/GenFfs.c
BaseTools/Source/C/Include/Guid/FfsSectionAlignmentPadding.h [new file with mode: 0644]

index 52092e36e74e36520a7cb792388798159a6d3712..433b608cb956d460f1c4115deb5641789b17c2c4 100644 (file)
@@ -19,6 +19,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Common/UefiBaseTypes.h>\r
 #include <Common/PiFirmwareFile.h>\r
 #include <IndustryStandard/PeImage.h>\r
+#include <Guid/FfsSectionAlignmentPadding.h>\r
 \r
 #include "CommonLib.h"\r
 #include "ParseInf.h"\r
@@ -58,6 +59,8 @@ STATIC UINT32 mFfsValidAlign[] = {0, 8, 16, 128, 512, 1024, 4096, 32768, 65536};
 \r
 STATIC EFI_GUID mZeroGuid = {0};\r
 \r
+STATIC EFI_GUID mEfiFfsSectionAlignmentPaddingGuid = EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID;\r
+\r
 STATIC\r
 VOID \r
 Version (\r
@@ -230,13 +233,14 @@ Returns:
 STATIC\r
 EFI_STATUS\r
 GetSectionContents (\r
-  IN  CHAR8   **InputFileName,\r
-  IN  UINT32  *InputFileAlign,\r
-  IN  UINT32  InputFileNum,\r
-  OUT UINT8   *FileBuffer,\r
-  OUT UINT32  *BufferLength,\r
-  OUT UINT32  *MaxAlignment,\r
-  OUT UINT8   *PESectionNum\r
+  IN  CHAR8                     **InputFileName,\r
+  IN  UINT32                    *InputFileAlign,\r
+  IN  UINT32                    InputFileNum,\r
+  IN  EFI_FFS_FILE_ATTRIBUTES   FfsAttrib,\r
+  OUT UINT8                     *FileBuffer,\r
+  OUT UINT32                    *BufferLength,\r
+  OUT UINT32                    *MaxAlignment,\r
+  OUT UINT8                     *PESectionNum\r
   )\r
 /*++\r
         \r
@@ -270,22 +274,24 @@ Returns:
   EFI_BUFFER_TOO_SMALL FileBuffer is not enough to contain all file data.\r
 --*/\r
 {\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_HEADER2 TempSectHeader;\r
-  EFI_TE_IMAGE_HEADER        TeHeader;\r
-  UINT32                     TeOffset;\r
-  EFI_GUID_DEFINED_SECTION   GuidSectHeader;\r
-  EFI_GUID_DEFINED_SECTION2  GuidSectHeader2;\r
-  UINT32                     HeaderSize;\r
-\r
-  Size          = 0;\r
-  Offset        = 0;\r
-  TeOffset      = 0;\r
+  UINT32                              Size;\r
+  UINT32                              Offset;\r
+  UINT32                              FileSize;\r
+  UINT32                              Index;\r
+  FILE                                *InFile;\r
+  EFI_FREEFORM_SUBTYPE_GUID_SECTION   *SectHeader;\r
+  EFI_COMMON_SECTION_HEADER2          TempSectHeader;\r
+  EFI_TE_IMAGE_HEADER                 TeHeader;\r
+  UINT32                              TeOffset;\r
+  EFI_GUID_DEFINED_SECTION            GuidSectHeader;\r
+  EFI_GUID_DEFINED_SECTION2           GuidSectHeader2;\r
+  UINT32                              HeaderSize;\r
+  UINT32                              MaxEncounteredAlignment;\r
+\r
+  Size                    = 0;\r
+  Offset                  = 0;\r
+  TeOffset                = 0;\r
+  MaxEncounteredAlignment = 1;\r
 \r
   //\r
   // Go through our array of file names and copy their contents\r
@@ -299,13 +305,6 @@ Returns:
       Size++;\r
     }\r
     \r
-    //\r
-    // Get the Max alignment of all input file datas\r
-    //\r
-    if (*MaxAlignment < InputFileAlign [Index]) {\r
-      *MaxAlignment = InputFileAlign [Index];\r
-    }\r
-\r
     // \r
     // Open file and read contents\r
     //\r
@@ -373,7 +372,7 @@ Returns:
     }\r
 \r
     //\r
-    // make sure section data meet its alignment requirement by adding one raw pad section.\r
+    // make sure section data meet its alignment requirement by adding one pad section.\r
     // But the different sections have the different section header. Necessary or not?\r
     // Based on section type to adjust offset? Todo\r
     //\r
@@ -386,11 +385,26 @@ Returns:
         // The maximal alignment is 64K, the raw section size must be less than 0xffffff\r
         //\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
+        SectHeader                        = (EFI_FREEFORM_SUBTYPE_GUID_SECTION *) (FileBuffer + Size);\r
+        SectHeader->CommonHeader.Size[0]  = (UINT8) (Offset & 0xff);\r
+        SectHeader->CommonHeader.Size[1]  = (UINT8) ((Offset & 0xff00) >> 8);\r
+        SectHeader->CommonHeader.Size[2]  = (UINT8) ((Offset & 0xff0000) >> 16);\r
+\r
+        //\r
+        // Only add a special reducible padding section if\r
+        // - this FFS has the FFS_ATTRIB_FIXED attribute,\r
+        // - none of the preceding sections have alignment requirements,\r
+        // - the size of the padding is sufficient for the\r
+        //   EFI_SECTION_FREEFORM_SUBTYPE_GUID header.\r
+        //\r
+        if ((FfsAttrib & FFS_ATTRIB_FIXED) != 0 &&\r
+            MaxEncounteredAlignment <= 1 &&\r
+            Offset >= sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION)) {\r
+          SectHeader->CommonHeader.Type   = EFI_SECTION_FREEFORM_SUBTYPE_GUID;\r
+          SectHeader->SubTypeGuid         = mEfiFfsSectionAlignmentPaddingGuid;\r
+        } else {\r
+          SectHeader->CommonHeader.Type   = EFI_SECTION_RAW;\r
+        }\r
       }\r
       DebugMsg (NULL, 0, 9, "Pad raw section for section data alignment", \r
                 "Pad Raw section size is %u", (unsigned) Offset);\r
@@ -398,6 +412,13 @@ Returns:
       Size = Size + Offset;\r
     }\r
 \r
+    //\r
+    // Get the Max alignment of all input file datas\r
+    //\r
+    if (MaxEncounteredAlignment < InputFileAlign [Index]) {\r
+      MaxEncounteredAlignment = InputFileAlign [Index];\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
@@ -413,7 +434,9 @@ Returns:
     fclose (InFile);\r
     Size += FileSize;\r
   }\r
-  \r
+\r
+  *MaxAlignment = MaxEncounteredAlignment;\r
+\r
   //\r
   // Set the actual length of the data.\r
   //\r
@@ -774,6 +797,7 @@ Returns:
              InputFileName,\r
              InputFileAlign,\r
              InputFileNum,\r
+             FfsAttrib,\r
              FileBuffer,\r
              &FileSize,\r
              &MaxAlignment,\r
@@ -810,6 +834,7 @@ Returns:
                InputFileName,\r
                InputFileAlign,\r
                InputFileNum,\r
+               FfsAttrib,\r
                FileBuffer,\r
                &FileSize,\r
                &MaxAlignment,\r
diff --git a/BaseTools/Source/C/Include/Guid/FfsSectionAlignmentPadding.h b/BaseTools/Source/C/Include/Guid/FfsSectionAlignmentPadding.h
new file mode 100644 (file)
index 0000000..0ad9d8c
--- /dev/null
@@ -0,0 +1,22 @@
+/** @file\r
+  Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>\r
+\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
+\r
+#ifndef __FFS_SECTION_ALIGNMENT_PADDING_GUID_H__\r
+#define __FFS_SECTION_ALIGNMENT_PADDING_GUID_H__\r
+\r
+#define EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID \\r
+  { \\r
+    0x04132C8D, 0x0A22, 0x4FA8, {0x82, 0x6E, 0x8B, 0xBF, 0xEF, 0xDB, 0x83, 0x6C } \\r
+  }\r
+\r
+#endif\r