]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/FfsInfStatement.py
BaseTools:Make BaseTools support new rules to generate RAW FFS FILE
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / FfsInfStatement.py
index 9eb99d659bfd0b8b9d5328823b80fb97ff7da8d8..cd3b0f647793da01772e6fee4a98e3c576a6026d 100644 (file)
@@ -4,31 +4,27 @@
 #  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 #  Copyright (c) 2014-2016 Hewlett-Packard Development Company, L.P.<BR>\r
 #\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
-#  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
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
 ##\r
 # Import Modules\r
 #\r
-import Rule\r
+from __future__ import absolute_import\r
+from . import Rule\r
 import Common.LongFilePathOs as os\r
-import StringIO\r
+from io import BytesIO\r
 from struct import *\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
-import Ffs\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from .Ffs import SectionSuffix,FdfFvFileTypeToFileType\r
 import subprocess\r
 import sys\r
-import Section\r
-import RuleSimpleFile\r
-import RuleComplexFile\r
+from . import Section\r
+from . import RuleSimpleFile\r
+from . import RuleComplexFile\r
 from CommonDataClass.FdfClass import FfsInfStatementClassObject\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
+from Common.DataType import SUP_MODULE_USER_DEFINED\r
 from Common.StringUtils import *\r
 from Common.Misc import PathClass\r
 from Common.Misc import GuidStructureByteArrayToGuidString\r
@@ -36,15 +32,15 @@ from Common.Misc import ProcessDuplicatedInf
 from Common.Misc import GetVariableOffset\r
 from Common import EdkLogger\r
 from Common.BuildToolError import *\r
-from GuidSection import GuidSection\r
-from FvImageSection import FvImageSection\r
+from .GuidSection import GuidSection\r
+from .FvImageSection import FvImageSection\r
 from Common.Misc import PeImageClass\r
 from AutoGen.GenDepex import DependencyExpression\r
 from PatchPcdValue.PatchPcdValue import PatchBinaryFile\r
 from Common.LongFilePathSupport import CopyLongFilePath\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 import Common.GlobalData as GlobalData\r
-from DepexSection import DepexSection\r
+from .DepexSection import DepexSection\r
 from Common.Misc import SaveFileOnChange\r
 from Common.Expression import *\r
 from Common.DataType import *\r
@@ -93,7 +89,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 ModuleType = self.InfModule.ModuleType\r
                 PlatformDataBase = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, self.CurrentArch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
 \r
-                if ModuleType != DataType.SUP_MODULE_USER_DEFINED:\r
+                if ModuleType != SUP_MODULE_USER_DEFINED:\r
                     for LibraryClass in PlatformDataBase.LibraryClasses.GetKeys():\r
                         if LibraryClass.startswith("NULL") and PlatformDataBase.LibraryClasses[LibraryClass, ModuleType]:\r
                             self.InfModule.LibraryClasses[LibraryClass] = PlatformDataBase.LibraryClasses[LibraryClass, ModuleType]\r
@@ -151,7 +147,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
     #   @param  self        The object pointer\r
     #   @param  Dict        dictionary contains macro and value pair\r
     #\r
-    def __InfParse__(self, Dict = {}):\r
+    def __InfParse__(self, Dict = None, IsGenFfs=False):\r
 \r
         GenFdsGlobalVariable.VerboseLogger( " Begine parsing INf file : %s" %self.InfFileName)\r
 \r
@@ -188,7 +184,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
             Inf = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClassObj, self.CurrentArch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
             #\r
-            # Set Ffs BaseName, MdouleGuid, ModuleType, Version, OutputPath\r
+            # Set Ffs BaseName, ModuleGuid, ModuleType, Version, OutputPath\r
             #\r
             self.BaseName = Inf.BaseName\r
             self.ModuleGuid = Inf.Guid\r
@@ -225,7 +221,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
             EdkLogger.warn("GenFds", GENFDS_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % (self.InfFileName))\r
 \r
         if self.ModuleType == SUP_MODULE_SMM_CORE and int(self.PiSpecVersion, 16) < 0x0001000A:\r
-            EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "SMM_CORE module type can't be used in the module with PI_SPECIFICATION_VERSION less than 0x0001000A", File=self.InfFileName)      \r
+            EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "SMM_CORE module type can't be used in the module with PI_SPECIFICATION_VERSION less than 0x0001000A", File=self.InfFileName)\r
 \r
         if self.ModuleType == SUP_MODULE_MM_CORE_STANDALONE and int(self.PiSpecVersion, 16) < 0x00010032:\r
             EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "MM_CORE_STANDALONE module type can't be used in the module with PI_SPECIFICATION_VERSION less than 0x00010032", File=self.InfFileName)\r
@@ -340,9 +336,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
         self.InfModule = Inf\r
         self.PcdIsDriver = Inf.PcdIsDriver\r
         self.IsBinaryModule = Inf.IsBinaryModule\r
-        Inf._GetDepex()\r
-        Inf._GetDepexExpression()\r
-        if len(Inf._Depex.data) > 0 and len(Inf._DepexExpression.data) > 0:\r
+        if len(Inf.Depex.data) > 0 and len(Inf.DepexExpression.data) > 0:\r
             self.Depex = True\r
 \r
         GenFdsGlobalVariable.VerboseLogger("BaseName : %s" % self.BaseName)\r
@@ -352,9 +346,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
         GenFdsGlobalVariable.VerboseLogger("InfFileName :%s" % self.InfFileName)\r
 \r
         #\r
-        # Set OutputPath = ${WorkSpace}\Build\Fv\Ffs\${ModuleGuid}+ ${MdouleName}\\r
+        # Set OutputPath = ${WorkSpace}\Build\Fv\Ffs\${ModuleGuid}+ ${ModuleName}\\r
         #\r
-\r
+        if IsGenFfs:\r
+            Rule = self.__GetRule__()\r
+            if GlobalData.gGuidPatternEnd.match(Rule.NameGuid):\r
+                self.ModuleGuid = Rule.NameGuid\r
         self.OutputPath = os.path.join(GenFdsGlobalVariable.FfsDir, \\r
                                        self.ModuleGuid + self.BaseName)\r
         if not os.path.exists(self.OutputPath) :\r
@@ -374,13 +371,13 @@ class FfsInfStatement(FfsInfStatementClassObject):
     def PatchEfiFile(self, EfiFile, FileType):\r
         #\r
         # If the module does not have any patches, then return path to input file\r
-        #  \r
+        #\r
         if not self.PatchPcds:\r
             return EfiFile\r
 \r
         #\r
         # Only patch file if FileType is PE32 or ModuleType is USER_DEFINED\r
-        #  \r
+        #\r
         if FileType != BINARY_FILE_TYPE_PE32 and self.ModuleType != SUP_MODULE_USER_DEFINED:\r
             return EfiFile\r
 \r
@@ -398,7 +395,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
         #\r
         # If a different file from the same module has already been patched, then generate an error\r
-        #  \r
+        #\r
         if self.PatchedBinFile:\r
             EdkLogger.error("GenFds", GENFDS_ERROR,\r
                             'Only one binary file can be patched:\n'\r
@@ -408,12 +405,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
         #\r
         # Copy unpatched file contents to output file location to perform patching\r
-        #  \r
+        #\r
         CopyLongFilePath(EfiFile, Output)\r
 \r
         #\r
         # Apply patches to patched output file\r
-        #  \r
+        #\r
         for Pcd, Value in self.PatchPcds:\r
             RetVal, RetStr = PatchBinaryFile(Output, int(Pcd.Offset, 0), Pcd.DatumType, Value, Pcd.MaxDatumSize)\r
             if RetVal:\r
@@ -421,12 +418,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
         #\r
         # Save the path of the patched output file\r
-        #  \r
+        #\r
         self.PatchedBinFile = Output\r
 \r
         #\r
         # Return path to patched output file\r
-        #  \r
+        #\r
         return Output\r
 \r
     ## GenFfs() method\r
@@ -444,18 +441,18 @@ class FfsInfStatement(FfsInfStatementClassObject):
         # Parse Inf file get Module related information\r
         #\r
 \r
-        self.__InfParse__(Dict)\r
+        self.__InfParse__(Dict, IsGenFfs=True)\r
         Arch = self.GetCurrentArch()\r
         SrcFile = mws.join( GenFdsGlobalVariable.WorkSpaceDir, self.InfFileName);\r
         DestFile = os.path.join( self.OutputPath, self.ModuleGuid + '.ffs')\r
-        \r
+\r
         SrcFileDir = "."\r
         SrcPath = os.path.dirname(SrcFile)\r
         SrcFileName = os.path.basename(SrcFile)\r
-        SrcFileBase, SrcFileExt = os.path.splitext(SrcFileName)   \r
+        SrcFileBase, SrcFileExt = os.path.splitext(SrcFileName)\r
         DestPath = os.path.dirname(DestFile)\r
         DestFileName = os.path.basename(DestFile)\r
-        DestFileBase, DestFileExt = os.path.splitext(DestFileName)   \r
+        DestFileBase, DestFileExt = os.path.splitext(DestFileName)\r
         self.MacroDict = {\r
             # source file\r
             "${src}"      :   SrcFile,\r
@@ -473,7 +470,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
         }\r
         #\r
         # Allow binary type module not specify override rule in FDF file.\r
-        # \r
+        #\r
         if len(self.BinFileList) > 0:\r
             if self.Rule is None or self.Rule == "":\r
                 self.Rule = "BINARY"\r
@@ -534,7 +531,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
             '$(NAMED_GUID)'  : self.ModuleGuid\r
         }\r
         String = GenFdsGlobalVariable.MacroExtend(String, MacroDict)\r
-        String = GenFdsGlobalVariable.MacroExtend(String, self.MacroDict)        \r
+        String = GenFdsGlobalVariable.MacroExtend(String, self.MacroDict)\r
         return String\r
 \r
     ## __GetRule__() method\r
@@ -761,7 +758,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
                 SecNum = '%d' %Index\r
                 GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
-                              Ffs.Ffs.SectionSuffix[SectionType] + SUP_MODULE_SEC + SecNum\r
+                              SectionSuffix[SectionType] + SUP_MODULE_SEC + SecNum\r
                 Index = Index + 1\r
                 OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
                 File = GenFdsGlobalVariable.MacroExtend(File, Dict, self.CurrentArch)\r
@@ -772,9 +769,9 @@ class FfsInfStatement(FfsInfStatementClassObject):
                     if ImageObj.SectionAlignment < 0x400:\r
                         self.Alignment = str (ImageObj.SectionAlignment)\r
                     elif ImageObj.SectionAlignment < 0x100000:\r
-                        self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
+                        self.Alignment = str (ImageObj.SectionAlignment // 0x400) + 'K'\r
                     else:\r
-                        self.Alignment = str (ImageObj.SectionAlignment / 0x100000) + 'M'\r
+                        self.Alignment = str (ImageObj.SectionAlignment // 0x100000) + 'M'\r
 \r
                 if not NoStrip:\r
                     FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc')\r
@@ -804,7 +801,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
         else:\r
             SecNum = '%d' %Index\r
             GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
-                              Ffs.Ffs.SectionSuffix[SectionType] + SUP_MODULE_SEC + SecNum\r
+                              SectionSuffix[SectionType] + SUP_MODULE_SEC + SecNum\r
             OutputFile = os.path.join(self.OutputPath, GenSecOutputFile)\r
             GenSecInputFile = GenFdsGlobalVariable.MacroExtend(GenSecInputFile, Dict, self.CurrentArch)\r
 \r
@@ -814,9 +811,9 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 if ImageObj.SectionAlignment < 0x400:\r
                     self.Alignment = str (ImageObj.SectionAlignment)\r
                 elif ImageObj.SectionAlignment < 0x100000:\r
-                    self.Alignment = str (ImageObj.SectionAlignment / 0x400) + 'K'\r
+                    self.Alignment = str (ImageObj.SectionAlignment // 0x400) + 'K'\r
                 else:\r
-                    self.Alignment = str (ImageObj.SectionAlignment / 0x100000) + 'M'\r
+                    self.Alignment = str (ImageObj.SectionAlignment // 0x100000) + 'M'\r
 \r
             if not NoStrip:\r
                 FileBeforeStrip = os.path.join(self.OutputPath, ModuleName + '.reloc')\r
@@ -883,7 +880,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
             self.ModuleGuid = RegistryGuidStr\r
 \r
             GenFdsGlobalVariable.GenerateFfs(FfsOutput, InputSection,\r
-                                             Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType],\r
+                                             FdfFvFileTypeToFileType[Rule.FvFileType],\r
                                              self.ModuleGuid, Fixed=Rule.Fixed,\r
                                              CheckSum=Rule.CheckSum, Align=Rule.Alignment,\r
                                              SectionAlign=SectionAlignments,\r
@@ -902,7 +899,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
     #   @retval string       File name of the generated section file\r
     #\r
     def __GenComplexFileSection__(self, Rule, FvChildAddr, FvParentAddr, IsMakefile = False):\r
-        if self.ModuleType in (SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM):\r
+        if self.ModuleType in (SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM, SUP_MODULE_MM_CORE_STANDALONE):\r
             if Rule.KeepReloc is not None:\r
                 self.KeepRelocFromRule = Rule.KeepReloc\r
         SectFiles = []\r
@@ -960,14 +957,14 @@ class FfsInfStatement(FfsInfStatementClassObject):
                     Sect.FvAddr = FvChildAddr\r
             if FvParentAddr is not None and isinstance(Sect, GuidSection):\r
                 Sect.FvParentAddr = FvParentAddr\r
-            \r
+\r
             if Rule.KeyStringList != []:\r
                 SectList, Align = Sect.GenSection(self.OutputPath, self.ModuleGuid, SecIndex, Rule.KeyStringList, self, IsMakefile = IsMakefile)\r
             else :\r
                 SectList, Align = Sect.GenSection(self.OutputPath, self.ModuleGuid, SecIndex, self.KeyStringList, self, IsMakefile = IsMakefile)\r
-            \r
+\r
             if not HasGeneratedFlag:\r
-                UniVfrOffsetFileSection = ""    \r
+                UniVfrOffsetFileSection = ""\r
                 ModuleFileName = mws.join(GenFdsGlobalVariable.WorkSpaceDir, self.InfFileName)\r
                 InfData = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClass(ModuleFileName), self.CurrentArch]\r
                 #\r
@@ -978,16 +975,16 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 for SourceFile in InfData.Sources:\r
                     if SourceFile.Type.upper() == ".VFR" :\r
                         #\r
-                        # search the .map file to find the offset of vfr binary in the PE32+/TE file. \r
+                        # search the .map file to find the offset of vfr binary in the PE32+/TE file.\r
                         #\r
                         VfrUniBaseName[SourceFile.BaseName] = (SourceFile.BaseName + "Bin")\r
                     if SourceFile.Type.upper() == ".UNI" :\r
                         #\r
-                        # search the .map file to find the offset of Uni strings binary in the PE32+/TE file. \r
+                        # search the .map file to find the offset of Uni strings binary in the PE32+/TE file.\r
                         #\r
                         VfrUniBaseName["UniOffsetName"] = (self.BaseName + "Strings")\r
-                    \r
-                \r
+\r
+\r
                 if len(VfrUniBaseName) > 0:\r
                     if IsMakefile:\r
                         if InfData.BuildType != 'UEFI_HII':\r
@@ -1023,7 +1020,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                     if UniVfrOffsetFileSection:\r
                         SectList.append(UniVfrOffsetFileSection)\r
                         HasGeneratedFlag = True\r
-                \r
+\r
             for SecName in  SectList :\r
                 SectFiles.append(SecName)\r
                 SectAlignments.append(Align)\r
@@ -1056,7 +1053,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
         FfsOutput = os.path.join( self.OutputPath, self.ModuleGuid + '.ffs')\r
         GenFdsGlobalVariable.GenerateFfs(FfsOutput, InputFile,\r
-                                             Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType],\r
+                                             FdfFvFileTypeToFileType[Rule.FvFileType],\r
                                              self.ModuleGuid, Fixed=Rule.Fixed,\r
                                              CheckSum=Rule.CheckSum, Align=Rule.Alignment,\r
                                              SectionAlign=Alignments,\r
@@ -1071,12 +1068,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
     #   @param  self                  The object pointer\r
     #   @param  VfrUniBaseName        A name list contain the UNI/INF object name.\r
     #   @retval RetValue              A list contain offset of UNI/INF object.\r
-    #    \r
+    #\r
     def __GetBuildOutputMapFileVfrUniInfo(self, VfrUniBaseName):\r
         MapFileName = os.path.join(self.EfiOutputPath, self.BaseName + ".map")\r
         EfiFileName = os.path.join(self.EfiOutputPath, self.BaseName + ".efi")\r
-        return GetVariableOffset(MapFileName, EfiFileName, VfrUniBaseName.values())\r
-    \r
+        return GetVariableOffset(MapFileName, EfiFileName, list(VfrUniBaseName.values()))\r
+\r
     ## __GenUniVfrOffsetFile() method\r
     #\r
     #   Generate the offset file for the module which contain VFR or UNI file.\r
@@ -1088,8 +1085,8 @@ class FfsInfStatement(FfsInfStatementClassObject):
     def __GenUniVfrOffsetFile(VfrUniOffsetList, UniVfrOffsetFileName):\r
 \r
         # Use a instance of StringIO to cache data\r
-        fStringIO = StringIO.StringIO('')  \r
-        \r
+        fStringIO = BytesIO()\r
+\r
         for Item in VfrUniOffsetList:\r
             if (Item[0].find("Strings") != -1):\r
                 #\r
@@ -1097,9 +1094,8 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 # GUID + Offset\r
                 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
                 #\r
-                UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
-                UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
-                fStringIO.write(''.join(UniGuid))            \r
+                UniGuid = b'\xe0\xc5\x13\x89\xf63\x86M\x9b\xf1C\xef\x89\xfc\x06f'\r
+                fStringIO.write(UniGuid)\r
                 UniValue = pack ('Q', int (Item[1], 16))\r
                 fStringIO.write (UniValue)\r
             else:\r
@@ -1108,13 +1104,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 # GUID + Offset\r
                 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
                 #\r
-                VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
-                VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
-                fStringIO.write(''.join(VfrGuid))                   \r
-                type (Item[1]) \r
+                VfrGuid = b'\xb4|\xbc\xd0Gj_I\xaa\x11q\x07F\xda\x06\xa2'\r
+                fStringIO.write(VfrGuid)\r
+                type (Item[1])\r
                 VfrValue = pack ('Q', int (Item[1], 16))\r
                 fStringIO.write (VfrValue)\r
-            \r
+\r
         #\r
         # write data into file.\r
         #\r
@@ -1122,7 +1117,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
             SaveFileOnChange(UniVfrOffsetFileName, fStringIO.getvalue())\r
         except:\r
             EdkLogger.error("GenFds", FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." %UniVfrOffsetFileName, None)\r
-        \r
+\r
         fStringIO.close ()\r
 \r
-        \r
+\r