]> 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 0dbffffc9a151b5529197a8d8830cf292befc2c0..cd3b0f647793da01772e6fee4a98e3c576a6026d 100644 (file)
@@ -4,56 +4,51 @@
 #  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.String import *\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
 from Common.Misc import ProcessDuplicatedInf\r
 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
 \r
 ## generate FFS from INF\r
 #\r
 #\r
 class FfsInfStatement(FfsInfStatementClassObject):\r
-    ## The mapping dictionary from datum type to its maximum number.\r
-    _MAX_SIZE_TYPE = {"BOOLEAN":0x01, "UINT8":0xFF, "UINT16":0xFFFF, "UINT32":0xFFFFFFFF, "UINT64":0xFFFFFFFFFFFFFFFF}\r
     ## The constructor\r
     #\r
     #   @param  self        The object pointer\r
@@ -89,12 +84,12 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 self.FinalTargetSuffixMap.setdefault(os.path.splitext(File)[1], []).append(File)\r
 \r
             # Check if current INF module has DEPEX\r
-            if '.depex' not in self.FinalTargetSuffixMap and self.InfModule.ModuleType != "USER_DEFINED" \\r
+            if '.depex' not in self.FinalTargetSuffixMap and self.InfModule.ModuleType != SUP_MODULE_USER_DEFINED \\r
                 and not self.InfModule.DxsFile and not self.InfModule.LibraryClass:\r
                 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
@@ -152,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
@@ -189,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
@@ -205,7 +200,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 self.ShadowFromInfFile = Inf.Shadow\r
 \r
         else:\r
-            Inf = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClassObj, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
+            Inf = GenFdsGlobalVariable.WorkSpace.BuildObject[PathClassObj, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
             self.BaseName = Inf.BaseName\r
             self.ModuleGuid = Inf.Guid\r
             self.ModuleType = Inf.ModuleType\r
@@ -225,10 +220,10 @@ class FfsInfStatement(FfsInfStatementClassObject):
         if len(self.SourceFileList) != 0 and not self.InDsc:\r
             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 == '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
+        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
 \r
-        if self.ModuleType == 'MM_CORE_STANDALONE' and int(self.PiSpecVersion, 16) < 0x00010032:\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
 \r
         if Inf._Defs is not None and len(Inf._Defs) > 0:\r
@@ -250,7 +245,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
             Pcd = InfPcds[PcdKey]\r
             if not hasattr(Pcd, 'Offset'):\r
                 continue\r
-            if Pcd.Type != 'PatchableInModule':\r
+            if Pcd.Type != TAB_PCDS_PATCHABLE_IN_MODULE:\r
                 continue\r
             # Override Patchable PCD value by the value from DSC\r
             PatchPcd = None\r
@@ -294,11 +289,11 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 try:\r
                     Pcd.InfDefaultValue = ValueExpressionEx(Pcd.InfDefaultValue, Pcd.DatumType, Platform._GuidDict)(True)\r
                 except BadExpression:\r
-                    EdkLogger.error("GenFds", GENFDS_ERROR, 'PCD [%s.%s] Value "%s"' %(Pcd.TokenSpaceGuidCName, Pcd.TokenCName, Pcd.DefaultValue),File=self.InfFileName)\r
+                    EdkLogger.error("GenFds", GENFDS_ERROR, 'PCD [%s.%s] Value "%s"' %(Pcd.TokenSpaceGuidCName, Pcd.TokenCName, Pcd.DefaultValue), File=self.InfFileName)\r
 \r
             # Check value, if value are equal, no need to patch\r
-            if Pcd.DatumType == "VOID*":\r
-                if Pcd.InfDefaultValue == DefaultValue or DefaultValue in [None, '']:\r
+            if Pcd.DatumType == TAB_VOID:\r
+                if Pcd.InfDefaultValue == DefaultValue or not DefaultValue:\r
                     continue\r
                 # Get the string size from FDF or DSC\r
                 if DefaultValue[0] == 'L':\r
@@ -311,7 +306,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 if DscOverride:\r
                     Pcd.MaxDatumSize = PatchPcd.MaxDatumSize\r
                 # If no defined the maximum size in DSC, try to get current size from INF\r
-                if Pcd.MaxDatumSize in ['', None]:\r
+                if not Pcd.MaxDatumSize:\r
                     Pcd.MaxDatumSize = str(len(Pcd.InfDefaultValue.split(',')))\r
             else:\r
                 Base1 = Base2 = 10\r
@@ -327,13 +322,13 @@ class FfsInfStatement(FfsInfStatementClassObject):
                 except:\r
                     continue\r
             # Check the Pcd size and data type\r
-            if Pcd.DatumType == "VOID*":\r
+            if Pcd.DatumType == TAB_VOID:\r
                 if int(MaxDatumSize) > int(Pcd.MaxDatumSize):\r
                     EdkLogger.error("GenFds", GENFDS_ERROR, "The size of VOID* type PCD '%s.%s' exceeds its maximum size %d bytes." \\r
                                     % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, int(MaxDatumSize) - int(Pcd.MaxDatumSize)))\r
             else:\r
-                if PcdValueInDscOrFdf > FfsInfStatement._MAX_SIZE_TYPE[Pcd.DatumType] \\r
-                    or PcdValueInImg > FfsInfStatement._MAX_SIZE_TYPE[Pcd.DatumType]:\r
+                if PcdValueInDscOrFdf > MAX_VAL_TYPE[Pcd.DatumType] \\r
+                    or PcdValueInImg > MAX_VAL_TYPE[Pcd.DatumType]:\r
                     EdkLogger.error("GenFds", GENFDS_ERROR, "The size of %s type PCD '%s.%s' doesn't match its data type." \\r
                                     % (Pcd.DatumType, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))\r
             self.PatchPcds.append((Pcd, DefaultValue))\r
@@ -341,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
@@ -353,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
@@ -375,14 +371,14 @@ 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
-        if FileType != 'PE32' and self.ModuleType != "USER_DEFINED":\r
+        #\r
+        if FileType != BINARY_FILE_TYPE_PE32 and self.ModuleType != SUP_MODULE_USER_DEFINED:\r
             return EfiFile\r
 \r
         #\r
@@ -399,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
@@ -409,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
@@ -422,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
@@ -445,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
+        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
@@ -474,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
@@ -489,22 +485,24 @@ class FfsInfStatement(FfsInfStatementClassObject):
         #\r
         # Convert Fv File Type for PI1.1 SMM driver.\r
         #\r
-        if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
+        if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
             if Rule.FvFileType == 'DRIVER':\r
                 Rule.FvFileType = 'SMM'\r
         #\r
         # Framework SMM Driver has no SMM FV file type\r
         #\r
-        if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
-            if Rule.FvFileType == 'SMM' or Rule.FvFileType == 'SMM_CORE':\r
+        if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) < 0x0001000A:\r
+            if Rule.FvFileType == 'SMM' or Rule.FvFileType == SUP_MODULE_SMM_CORE:\r
                 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM or SMM_CORE FV file type", File=self.InfFileName)\r
         #\r
         # For the rule only has simpleFile\r
         #\r
         MakefilePath = None\r
+        if self.IsBinaryModule:\r
+            IsMakefile = False\r
         if IsMakefile:\r
             MakefilePath = self.InfFileName, Arch\r
-        if isinstance (Rule, RuleSimpleFile.RuleSimpleFile) :\r
+        if isinstance (Rule, RuleSimpleFile.RuleSimpleFile):\r
             SectionOutputList = self.__GenSimpleFileSection__(Rule, IsMakefile=IsMakefile)\r
             FfsOutput = self.__GenSimpleFileFfs__(Rule, SectionOutputList, MakefilePath=MakefilePath)\r
             return FfsOutput\r
@@ -533,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
@@ -568,7 +566,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
         RuleName = 'RULE'      + \\r
                    '.'         + \\r
-                   'COMMON'    + \\r
+                   TAB_COMMON    + \\r
                    '.'         + \\r
                    self.ModuleType.upper()\r
 \r
@@ -610,7 +608,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                     # the file name FILE_GUIDmodule.inf, then PlatformDataBase.Modules use FILE_GUIDmodule.inf as key,\r
                     # but the path (self.MetaFile.Path) is the real path\r
                     #\r
-                    for key in PlatformDataBase.Modules.keys():\r
+                    for key in PlatformDataBase.Modules:\r
                         if InfFileKey == str((PlatformDataBase.Modules[key]).MetaFile.Path):\r
                             DscArchList.append (Arch)\r
                             break\r
@@ -693,13 +691,13 @@ class FfsInfStatement(FfsInfStatementClassObject):
             Arch = self.CurrentArch\r
 \r
         OutputPath = os.path.join(GenFdsGlobalVariable.OutputDirDict[Arch],\r
-                                  Arch ,\r
+                                  Arch,\r
                                   ModulePath,\r
                                   FileName,\r
                                   'OUTPUT'\r
                                   )\r
         DebugPath = os.path.join(GenFdsGlobalVariable.OutputDirDict[Arch],\r
-                                  Arch ,\r
+                                  Arch,\r
                                   ModulePath,\r
                                   FileName,\r
                                   'DEBUG'\r
@@ -737,17 +735,17 @@ class FfsInfStatement(FfsInfStatementClassObject):
         #\r
         # Convert Fv Section Type for PI1.1 SMM driver.\r
         #\r
-        if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
-            if SectionType == 'DXE_DEPEX':\r
-                SectionType = 'SMM_DEPEX'\r
+        if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
+            if SectionType == BINARY_FILE_TYPE_DXE_DEPEX:\r
+                SectionType = BINARY_FILE_TYPE_SMM_DEPEX\r
         #\r
         # Framework SMM Driver has no SMM_DEPEX section type\r
         #\r
-        if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
-            if SectionType == 'SMM_DEPEX':\r
+        if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) < 0x0001000A:\r
+            if SectionType == BINARY_FILE_TYPE_SMM_DEPEX:\r
                 EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM_DEPEX section type", File=self.InfFileName)\r
         NoStrip = True\r
-        if self.ModuleType in ('SEC', 'PEI_CORE', 'PEIM'):\r
+        if self.ModuleType in (SUP_MODULE_SEC, SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM):\r
             if self.KeepReloc is not None:\r
                 NoStrip = self.KeepReloc\r
             elif Rule.KeepReloc is not None:\r
@@ -760,20 +758,20 @@ class FfsInfStatement(FfsInfStatementClassObject):
 \r
                 SecNum = '%d' %Index\r
                 GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
-                              Ffs.Ffs.SectionSuffix[SectionType] + '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
 \r
                 #Get PE Section alignment when align is set to AUTO\r
-                if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):\r
+                if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE):\r
                     ImageObj = PeImageClass (File)\r
                     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
@@ -789,7 +787,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                         )\r
                     File = StrippedFile\r
 \r
-                if SectionType == 'TE':\r
+                if SectionType == BINARY_FILE_TYPE_TE:\r
                     TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw')\r
                     GenFdsGlobalVariable.GenerateFirmwareImage(\r
                             TeFile,\r
@@ -803,19 +801,19 @@ class FfsInfStatement(FfsInfStatementClassObject):
         else:\r
             SecNum = '%d' %Index\r
             GenSecOutputFile= self.__ExtendMacro__(Rule.NameGuid) + \\r
-                              Ffs.Ffs.SectionSuffix[SectionType] + '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
             #Get PE Section alignment when align is set to AUTO\r
-            if self.Alignment == 'Auto' and (SectionType == 'PE32' or SectionType == 'TE'):\r
+            if self.Alignment == 'Auto' and (SectionType == BINARY_FILE_TYPE_PE32 or SectionType == BINARY_FILE_TYPE_TE):\r
                 ImageObj = PeImageClass (GenSecInputFile)\r
                 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
@@ -832,7 +830,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                     )\r
                 GenSecInputFile = StrippedFile\r
 \r
-            if SectionType == 'TE':\r
+            if SectionType == BINARY_FILE_TYPE_TE:\r
                 TeFile = os.path.join( self.OutputPath, self.ModuleGuid + 'Te.raw')\r
                 GenFdsGlobalVariable.GenerateFirmwareImage(\r
                         TeFile,\r
@@ -882,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
@@ -901,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 ('SEC', 'PEI_CORE', '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
@@ -940,14 +938,14 @@ class FfsInfStatement(FfsInfStatementClassObject):
             #\r
             # Convert Fv Section Type for PI1.1 SMM driver.\r
             #\r
-            if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
-                if Sect.SectionType == 'DXE_DEPEX':\r
-                    Sect.SectionType = 'SMM_DEPEX'\r
+            if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) >= 0x0001000A:\r
+                if Sect.SectionType == BINARY_FILE_TYPE_DXE_DEPEX:\r
+                    Sect.SectionType = BINARY_FILE_TYPE_SMM_DEPEX\r
             #\r
             # Framework SMM Driver has no SMM_DEPEX section type\r
             #\r
-            if self.ModuleType == 'DXE_SMM_DRIVER' and int(self.PiSpecVersion, 16) < 0x0001000A:\r
-                if Sect.SectionType == 'SMM_DEPEX':\r
+            if self.ModuleType == SUP_MODULE_DXE_SMM_DRIVER and int(self.PiSpecVersion, 16) < 0x0001000A:\r
+                if Sect.SectionType == BINARY_FILE_TYPE_SMM_DEPEX:\r
                     EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Framework SMM module doesn't support SMM_DEPEX section type", File=self.InfFileName)\r
             #\r
             # process the inside FvImage from FvSection or GuidSection\r
@@ -959,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
+                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
+                SectList, Align = Sect.GenSection(self.OutputPath, self.ModuleGuid, SecIndex, self.KeyStringList, self, IsMakefile = IsMakefile)\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
@@ -977,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
@@ -1009,7 +1007,7 @@ class FfsInfStatement(FfsInfStatementClassObject):
                         if VfrUniOffsetList:\r
                             UniVfrOffsetFileName = os.path.join(self.OutputPath, self.BaseName + '.offset')\r
                             UniVfrOffsetFileSection = os.path.join(self.OutputPath, self.BaseName + 'Offset' + '.raw')\r
-                            self.__GenUniVfrOffsetFile (VfrUniOffsetList, UniVfrOffsetFileName)\r
+                            FfsInfStatement.__GenUniVfrOffsetFile (VfrUniOffsetList, UniVfrOffsetFileName)\r
                             UniVfrOffsetFileNameList = []\r
                             UniVfrOffsetFileNameList.append(UniVfrOffsetFileName)\r
                             """Call GenSection"""\r
@@ -1022,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
@@ -1055,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
@@ -1063,27 +1061,6 @@ class FfsInfStatement(FfsInfStatementClassObject):
                                              )\r
         return FfsOutput\r
 \r
-    ## __GetGenFfsCmdParameter__() method\r
-    #\r
-    #   Create parameter string for GenFfs\r
-    #\r
-    #   @param  self        The object pointer\r
-    #   @param  Rule        The rule object used to generate section\r
-    #   @retval tuple       (FileType, Fixed, CheckSum, Alignment)\r
-    #\r
-    def __GetGenFfsCmdParameter__(self, Rule):\r
-        result = tuple()\r
-        result += ('-t', Ffs.Ffs.FdfFvFileTypeToFileType[Rule.FvFileType])\r
-        if Rule.Fixed != False:\r
-            result += ('-x',)\r
-        if Rule.CheckSum != False:\r
-            result += ('-s',)\r
-\r
-        if Rule.Alignment is not None and Rule.Alignment != '':\r
-            result += ('-a', Rule.Alignment)\r
-\r
-        return result\r
\r
     ## __GetBuildOutputMapFileVfrUniInfo() method\r
     #\r
     #   Find the offset of UNI/INF object offset in the EFI image file.\r
@@ -1091,25 +1068,25 @@ 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
     #\r
-    #   @param  self                    The object pointer\r
     #   @param  VfrUniOffsetList        A list contain the VFR/UNI offsets in the EFI image file.\r
     #   @param  UniVfrOffsetFileName    The output offset file name.\r
     #\r
-    def __GenUniVfrOffsetFile(self, VfrUniOffsetList, UniVfrOffsetFileName):\r
+    @staticmethod\r
+    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
@@ -1117,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
@@ -1128,21 +1104,20 @@ 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
         try :\r
             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
+            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
         fStringIO.close ()\r
 \r
-        \r
+\r