]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFds.py
index dcba9f24cb6b7894f49e45128685f833a8fec401..21ae9c4d4c8f2a6c051b2c5e77faefde878a497b 100644 (file)
@@ -1,55 +1,47 @@
 ## @file\r
 # generate flash image\r
 #\r
 ## @file\r
 # generate flash image\r
 #\r
-#  Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2007 - 2019, Intel Corporation. All rights reserved.<BR>\r
 #\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
 #\r
 \r
 ##\r
 # Import Modules\r
 #\r
+from __future__ import print_function\r
+from __future__ import absolute_import\r
+from re import compile\r
 from optparse import OptionParser\r
 from optparse import OptionParser\r
-import sys\r
+from sys import exit\r
+from glob import glob\r
+from struct import unpack\r
+from linecache import getlines\r
+from io import BytesIO\r
+\r
 import Common.LongFilePathOs as os\r
 import Common.LongFilePathOs as os\r
-import linecache\r
-import FdfParser\r
-import Common.BuildToolError as BuildToolError\r
-from GenFdsGlobalVariable import GenFdsGlobalVariable\r
-from Workspace.WorkspaceDatabase import WorkspaceDatabase\r
-from Workspace.BuildClassObject import PcdClassObject\r
-from Workspace.BuildClassObject import ModuleBuildClassObject\r
-import RuleComplexFile\r
-from EfiSection import EfiSection\r
-import StringIO\r
-import Common.TargetTxtClassObject as TargetTxtClassObject\r
-import Common.ToolDefClassObject as ToolDefClassObject\r
+from Common.TargetTxtClassObject import TargetTxtClassObject\r
 from Common.DataType import *\r
 import Common.GlobalData as GlobalData\r
 from Common import EdkLogger\r
 from Common.DataType import *\r
 import Common.GlobalData as GlobalData\r
 from Common import EdkLogger\r
-from Common.String import *\r
-from Common.Misc import DirCache, PathClass\r
-from Common.Misc import SaveFileOnChange\r
-from Common.Misc import ClearDuplicatedInf\r
-from Common.Misc import GuidStructureStringToGuidString\r
-from Common.Misc import CheckPcdDatum\r
-from Common.Misc import BuildOptionPcdValueFormat\r
+from Common.StringUtils import NormPath\r
+from Common.Misc import DirCache, PathClass, GuidStructureStringToGuidString\r
+from Common.Misc import SaveFileOnChange, ClearDuplicatedInf\r
 from Common.BuildVersion import gBUILD_VERSION\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
 from Common.BuildVersion import gBUILD_VERSION\r
 from Common.MultipleWorkspace import MultipleWorkspace as mws\r
-import FfsFileStatement\r
-import glob\r
-from struct import unpack\r
+from Common.BuildToolError import FatalError, GENFDS_ERROR, CODE_ERROR, FORMAT_INVALID, RESOURCE_NOT_AVAILABLE, FILE_NOT_FOUND, OPTION_MISSING, FORMAT_NOT_SUPPORTED, OPTION_VALUE_INVALID, PARAMETER_INVALID\r
+from Workspace.WorkspaceDatabase import WorkspaceDatabase\r
+\r
+from .FdfParser import FdfParser, Warning\r
+from .GenFdsGlobalVariable import GenFdsGlobalVariable\r
+from .FfsFileStatement import FileStatement\r
+import Common.DataType as DataType\r
+from struct import Struct\r
 \r
 ## Version and Copyright\r
 versionNumber = "1.0" + ' ' + gBUILD_VERSION\r
 __version__ = "%prog Version " + versionNumber\r
 \r
 ## Version and Copyright\r
 versionNumber = "1.0" + ' ' + gBUILD_VERSION\r
 __version__ = "%prog Version " + versionNumber\r
-__copyright__ = "Copyright (c) 2007 - 2017, Intel Corporation  All rights reserved."\r
+__copyright__ = "Copyright (c) 2007 - 2018, Intel Corporation  All rights reserved."\r
 \r
 ## Tool entrance method\r
 #\r
 \r
 ## Tool entrance method\r
 #\r
@@ -63,52 +55,99 @@ __copyright__ = "Copyright (c) 2007 - 2017, Intel Corporation  All rights reserv
 def main():\r
     global Options\r
     Options = myOptionParser()\r
 def main():\r
     global Options\r
     Options = myOptionParser()\r
+    EdkLogger.Initialize()\r
+    return GenFdsApi(OptionsToCommandDict(Options))\r
+\r
+def resetFdsGlobalVariable():\r
+    GenFdsGlobalVariable.FvDir = ''\r
+    GenFdsGlobalVariable.OutputDirDict = {}\r
+    GenFdsGlobalVariable.BinDir = ''\r
+    # will be FvDir + os.sep + 'Ffs'\r
+    GenFdsGlobalVariable.FfsDir = ''\r
+    GenFdsGlobalVariable.FdfParser = None\r
+    GenFdsGlobalVariable.LibDir = ''\r
+    GenFdsGlobalVariable.WorkSpace = None\r
+    GenFdsGlobalVariable.WorkSpaceDir = ''\r
+    GenFdsGlobalVariable.ConfDir = ''\r
+    GenFdsGlobalVariable.OutputDirFromDscDict = {}\r
+    GenFdsGlobalVariable.TargetName = ''\r
+    GenFdsGlobalVariable.ToolChainTag = ''\r
+    GenFdsGlobalVariable.RuleDict = {}\r
+    GenFdsGlobalVariable.ArchList = None\r
+    GenFdsGlobalVariable.ActivePlatform = None\r
+    GenFdsGlobalVariable.FvAddressFileName = ''\r
+    GenFdsGlobalVariable.VerboseMode = False\r
+    GenFdsGlobalVariable.DebugLevel = -1\r
+    GenFdsGlobalVariable.SharpCounter = 0\r
+    GenFdsGlobalVariable.SharpNumberPerLine = 40\r
+    GenFdsGlobalVariable.FdfFile = ''\r
+    GenFdsGlobalVariable.FdfFileTimeStamp = 0\r
+    GenFdsGlobalVariable.FixedLoadAddress = False\r
+    GenFdsGlobalVariable.PlatformName = ''\r
+\r
+    GenFdsGlobalVariable.BuildRuleFamily = DataType.TAB_COMPILER_MSFT\r
+    GenFdsGlobalVariable.ToolChainFamily = DataType.TAB_COMPILER_MSFT\r
+    GenFdsGlobalVariable.__BuildRuleDatabase = None\r
+    GenFdsGlobalVariable.GuidToolDefinition = {}\r
+    GenFdsGlobalVariable.FfsCmdDict = {}\r
+    GenFdsGlobalVariable.SecCmdList = []\r
+    GenFdsGlobalVariable.CopyList   = []\r
+    GenFdsGlobalVariable.ModuleFile = ''\r
+    GenFdsGlobalVariable.EnableGenfdsMultiThread = False\r
+\r
+    GenFdsGlobalVariable.LargeFileInFvFlags = []\r
+    GenFdsGlobalVariable.EFI_FIRMWARE_FILE_SYSTEM3_GUID = '5473C07A-3DCB-4dca-BD6F-1E9689E7349A'\r
+    GenFdsGlobalVariable.LARGE_FILE_SIZE = 0x1000000\r
+\r
+    GenFdsGlobalVariable.SectionHeader = Struct("3B 1B")\r
+\r
+    # FvName, FdName, CapName in FDF, Image file name\r
+    GenFdsGlobalVariable.ImageBinDict = {}\r
 \r
 \r
+def GenFdsApi(FdsCommandDict, WorkSpaceDataBase=None):\r
     global Workspace\r
     Workspace = ""\r
     ArchList = None\r
     ReturnCode = 0\r
     global Workspace\r
     Workspace = ""\r
     ArchList = None\r
     ReturnCode = 0\r
+    resetFdsGlobalVariable()\r
 \r
 \r
-    EdkLogger.Initialize()\r
     try:\r
     try:\r
-        if Options.verbose != None:\r
+        if FdsCommandDict.get("verbose"):\r
             EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
             GenFdsGlobalVariable.VerboseMode = True\r
             EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
             GenFdsGlobalVariable.VerboseMode = True\r
-            \r
-        if Options.FixedAddress != None:\r
+\r
+        if FdsCommandDict.get("FixedAddress"):\r
             GenFdsGlobalVariable.FixedLoadAddress = True\r
             GenFdsGlobalVariable.FixedLoadAddress = True\r
-            \r
-        if Options.quiet != None:\r
+\r
+        if FdsCommandDict.get("quiet"):\r
             EdkLogger.SetLevel(EdkLogger.QUIET)\r
             EdkLogger.SetLevel(EdkLogger.QUIET)\r
-        if Options.debug != None:\r
-            EdkLogger.SetLevel(Options.debug + 1)\r
-            GenFdsGlobalVariable.DebugLevel = Options.debug\r
+        if FdsCommandDict.get("debug"):\r
+            EdkLogger.SetLevel(FdsCommandDict.get("debug") + 1)\r
+            GenFdsGlobalVariable.DebugLevel = FdsCommandDict.get("debug")\r
         else:\r
             EdkLogger.SetLevel(EdkLogger.INFO)\r
 \r
         else:\r
             EdkLogger.SetLevel(EdkLogger.INFO)\r
 \r
-        if (Options.Workspace == None):\r
+        if not FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE')):\r
             EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
             EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
-        elif not os.path.exists(Options.Workspace):\r
+        elif not os.path.exists(FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE'))):\r
             EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
         else:\r
             EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",\r
                             ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")\r
         else:\r
-            Workspace = os.path.normcase(Options.Workspace)\r
+            Workspace = os.path.normcase(FdsCommandDict.get("Workspace",os.environ.get('WORKSPACE')))\r
             GenFdsGlobalVariable.WorkSpaceDir = Workspace\r
             GenFdsGlobalVariable.WorkSpaceDir = Workspace\r
-            if 'EDK_SOURCE' in os.environ.keys():\r
-                GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])\r
-            if (Options.debug):\r
+            if FdsCommandDict.get("debug"):\r
                 GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)\r
                 GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)\r
-            if Options.GenfdsMultiThread:\r
+            if FdsCommandDict.get("GenfdsMultiThread"):\r
                 GenFdsGlobalVariable.EnableGenfdsMultiThread = True\r
         os.chdir(GenFdsGlobalVariable.WorkSpaceDir)\r
                 GenFdsGlobalVariable.EnableGenfdsMultiThread = True\r
         os.chdir(GenFdsGlobalVariable.WorkSpaceDir)\r
-        \r
+\r
         # set multiple workspace\r
         PackagesPath = os.getenv("PACKAGES_PATH")\r
         mws.setWs(GenFdsGlobalVariable.WorkSpaceDir, PackagesPath)\r
 \r
         # set multiple workspace\r
         PackagesPath = os.getenv("PACKAGES_PATH")\r
         mws.setWs(GenFdsGlobalVariable.WorkSpaceDir, PackagesPath)\r
 \r
-        if (Options.filename):\r
-            FdfFilename = Options.filename\r
+        if FdsCommandDict.get("fdf_file"):\r
+            FdfFilename = FdsCommandDict.get("fdf_file")[0].Path\r
             FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)\r
 \r
             if FdfFilename[0:2] == '..':\r
             FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)\r
 \r
             if FdfFilename[0:2] == '..':\r
@@ -123,14 +162,14 @@ def main():
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")\r
 \r
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")\r
 \r
-        if (Options.BuildTarget):\r
-            GenFdsGlobalVariable.TargetName = Options.BuildTarget\r
+        if FdsCommandDict.get("build_target"):\r
+            GenFdsGlobalVariable.TargetName = FdsCommandDict.get("build_target")\r
 \r
 \r
-        if (Options.ToolChain):\r
-            GenFdsGlobalVariable.ToolChainTag = Options.ToolChain\r
+        if FdsCommandDict.get("toolchain_tag"):\r
+            GenFdsGlobalVariable.ToolChainTag = FdsCommandDict.get("toolchain_tag")\r
 \r
 \r
-        if (Options.activePlatform):\r
-            ActivePlatform = Options.activePlatform\r
+        if FdsCommandDict.get("active_platform"):\r
+            ActivePlatform = FdsCommandDict.get("active_platform")\r
             ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)\r
 \r
             if ActivePlatform[0:2] == '..':\r
             ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)\r
 \r
             if ActivePlatform[0:2] == '..':\r
@@ -139,16 +178,16 @@ def main():
             if not os.path.isabs (ActivePlatform):\r
                 ActivePlatform = mws.join(GenFdsGlobalVariable.WorkSpaceDir, ActivePlatform)\r
 \r
             if not os.path.isabs (ActivePlatform):\r
                 ActivePlatform = mws.join(GenFdsGlobalVariable.WorkSpaceDir, ActivePlatform)\r
 \r
-            if not os.path.exists(ActivePlatform)  :\r
+            if not os.path.exists(ActivePlatform):\r
                 EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")\r
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")\r
 \r
         GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))\r
 \r
                 EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")\r
         else:\r
             EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")\r
 \r
         GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))\r
 \r
-        if (Options.ConfDirectory):\r
+        if FdsCommandDict.get("conf_directory"):\r
             # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
             # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
-            ConfDirectoryPath = os.path.normpath(Options.ConfDirectory)\r
+            ConfDirectoryPath = os.path.normpath(FdsCommandDict.get("conf_directory"))\r
             if ConfDirectoryPath.startswith('"'):\r
                 ConfDirectoryPath = ConfDirectoryPath[1:]\r
             if ConfDirectoryPath.endswith('"'):\r
             if ConfDirectoryPath.startswith('"'):\r
                 ConfDirectoryPath = ConfDirectoryPath[1:]\r
             if ConfDirectoryPath.endswith('"'):\r
@@ -158,27 +197,29 @@ def main():
                 # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r
                 ConfDirectoryPath = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, ConfDirectoryPath)\r
         else:\r
                 # This also handles someone specifying the Conf directory in the workspace. Using --conf=Conf\r
                 ConfDirectoryPath = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, ConfDirectoryPath)\r
         else:\r
-            if "CONF_PATH" in os.environ.keys():\r
+            if "CONF_PATH" in os.environ:\r
                 ConfDirectoryPath = os.path.normcase(os.environ["CONF_PATH"])\r
             else:\r
                 # Get standard WORKSPACE/Conf, use the absolute path to the WORKSPACE/Conf\r
                 ConfDirectoryPath = mws.join(GenFdsGlobalVariable.WorkSpaceDir, 'Conf')\r
         GenFdsGlobalVariable.ConfDir = ConfDirectoryPath\r
                 ConfDirectoryPath = os.path.normcase(os.environ["CONF_PATH"])\r
             else:\r
                 # Get standard WORKSPACE/Conf, use the absolute path to the WORKSPACE/Conf\r
                 ConfDirectoryPath = mws.join(GenFdsGlobalVariable.WorkSpaceDir, 'Conf')\r
         GenFdsGlobalVariable.ConfDir = ConfDirectoryPath\r
+        if not GlobalData.gConfDirectory:\r
+            GlobalData.gConfDirectory = GenFdsGlobalVariable.ConfDir\r
         BuildConfigurationFile = os.path.normpath(os.path.join(ConfDirectoryPath, "target.txt"))\r
         if os.path.isfile(BuildConfigurationFile) == True:\r
         BuildConfigurationFile = os.path.normpath(os.path.join(ConfDirectoryPath, "target.txt"))\r
         if os.path.isfile(BuildConfigurationFile) == True:\r
-            TargetTxt = TargetTxtClassObject.TargetTxtClassObject()\r
+            TargetTxt = TargetTxtClassObject()\r
             TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)\r
             # if no build target given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.TargetName:\r
             TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)\r
             # if no build target given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.TargetName:\r
-                BuildTargetList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]\r
+                BuildTargetList = TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TARGET]\r
                 if len(BuildTargetList) != 1:\r
                     EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for Target.")\r
                 GenFdsGlobalVariable.TargetName = BuildTargetList[0]\r
 \r
             # if no tool chain given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.ToolChainTag:\r
                 if len(BuildTargetList) != 1:\r
                     EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for Target.")\r
                 GenFdsGlobalVariable.TargetName = BuildTargetList[0]\r
 \r
             # if no tool chain given in command line, get it from target.txt\r
             if not GenFdsGlobalVariable.ToolChainTag:\r
-                ToolChainList = TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]\r
-                if ToolChainList == None or len(ToolChainList) == 0:\r
+                ToolChainList = TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TOOL_CHAIN_TAG]\r
+                if ToolChainList is None or len(ToolChainList) == 0:\r
                     EdkLogger.error("GenFds", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.")\r
                 if len(ToolChainList) != 1:\r
                     EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for ToolChain.")\r
                     EdkLogger.error("GenFds", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.")\r
                 if len(ToolChainList) != 1:\r
                     EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="Only allows one instance for ToolChain.")\r
@@ -187,10 +228,10 @@ def main():
             EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)\r
 \r
         #Set global flag for build mode\r
             EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)\r
 \r
         #Set global flag for build mode\r
-        GlobalData.gIgnoreSource = Options.IgnoreSources\r
+        GlobalData.gIgnoreSource = FdsCommandDict.get("IgnoreSources")\r
 \r
 \r
-        if Options.Macros:\r
-            for Pair in Options.Macros:\r
+        if FdsCommandDict.get("macro"):\r
+            for Pair in FdsCommandDict.get("macro"):\r
                 if Pair.startswith('"'):\r
                     Pair = Pair[1:]\r
                 if Pair.endswith('"'):\r
                 if Pair.startswith('"'):\r
                     Pair = Pair[1:]\r
                 if Pair.endswith('"'):\r
@@ -199,15 +240,7 @@ def main():
                 if len(List) == 2:\r
                     if not List[1].strip():\r
                         EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="No Value given for Macro %s" %List[0])\r
                 if len(List) == 2:\r
                     if not List[1].strip():\r
                         EdkLogger.error("GenFds", OPTION_VALUE_INVALID, ExtraData="No Value given for Macro %s" %List[0])\r
-                    if List[0].strip() == "EFI_SOURCE":\r
-                        GlobalData.gEfiSource = List[1].strip()\r
-                        GlobalData.gGlobalDefines["EFI_SOURCE"] = GlobalData.gEfiSource\r
-                        continue\r
-                    elif List[0].strip() == "EDK_SOURCE":\r
-                        GlobalData.gEdkSource = List[1].strip()\r
-                        GlobalData.gGlobalDefines["EDK_SOURCE"] = GlobalData.gEdkSource\r
-                        continue\r
-                    elif List[0].strip() in ["WORKSPACE", "TARGET", "TOOLCHAIN"]:\r
+                    if List[0].strip() in ["WORKSPACE", "TARGET", "TOOLCHAIN"]:\r
                         GlobalData.gGlobalDefines[List[0].strip()] = List[1].strip()\r
                     else:\r
                         GlobalData.gCommandLineDefines[List[0].strip()] = List[1].strip()\r
                         GlobalData.gGlobalDefines[List[0].strip()] = List[1].strip()\r
                     else:\r
                         GlobalData.gCommandLineDefines[List[0].strip()] = List[1].strip()\r
@@ -216,40 +249,43 @@ def main():
         os.environ["WORKSPACE"] = Workspace\r
 \r
         # Use the -t and -b option as gGlobalDefines's TOOLCHAIN and TARGET if they are not defined\r
         os.environ["WORKSPACE"] = Workspace\r
 \r
         # Use the -t and -b option as gGlobalDefines's TOOLCHAIN and TARGET if they are not defined\r
-        if "TARGET" not in GlobalData.gGlobalDefines.keys():\r
+        if "TARGET" not in GlobalData.gGlobalDefines:\r
             GlobalData.gGlobalDefines["TARGET"] = GenFdsGlobalVariable.TargetName\r
             GlobalData.gGlobalDefines["TARGET"] = GenFdsGlobalVariable.TargetName\r
-        if "TOOLCHAIN" not in GlobalData.gGlobalDefines.keys():\r
+        if "TOOLCHAIN" not in GlobalData.gGlobalDefines:\r
             GlobalData.gGlobalDefines["TOOLCHAIN"] = GenFdsGlobalVariable.ToolChainTag\r
             GlobalData.gGlobalDefines["TOOLCHAIN"] = GenFdsGlobalVariable.ToolChainTag\r
-        if "TOOL_CHAIN_TAG" not in GlobalData.gGlobalDefines.keys():\r
+        if "TOOL_CHAIN_TAG" not in GlobalData.gGlobalDefines:\r
             GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = GenFdsGlobalVariable.ToolChainTag\r
 \r
         """call Workspace build create database"""\r
         GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))\r
             GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = GenFdsGlobalVariable.ToolChainTag\r
 \r
         """call Workspace build create database"""\r
         GlobalData.gDatabasePath = os.path.normpath(os.path.join(ConfDirectoryPath, GlobalData.gDatabasePath))\r
-        BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)\r
-        BuildWorkSpace.InitDatabase()\r
-        \r
+\r
+        if WorkSpaceDataBase:\r
+            BuildWorkSpace = WorkSpaceDataBase\r
+        else:\r
+            BuildWorkSpace = WorkspaceDatabase()\r
         #\r
         # Get files real name in workspace dir\r
         #\r
         GlobalData.gAllFiles = DirCache(Workspace)\r
         GlobalData.gWorkspace = Workspace\r
 \r
         #\r
         # Get files real name in workspace dir\r
         #\r
         GlobalData.gAllFiles = DirCache(Workspace)\r
         GlobalData.gWorkspace = Workspace\r
 \r
-        if (Options.archList) :\r
-            ArchList = Options.archList.split(',')\r
+        if FdsCommandDict.get("build_architecture_list"):\r
+            ArchList = FdsCommandDict.get("build_architecture_list").split(',')\r
         else:\r
         else:\r
-#            EdkLogger.error("GenFds", OPTION_MISSING, "Missing build ARCH")\r
-            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList\r
+            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].SupArchList\r
 \r
 \r
-        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON', Options.BuildTarget, Options.ToolChain].SupArchList) & set(ArchList)\r
+        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].SupArchList) & set(ArchList)\r
         if len(TargetArchList) == 0:\r
         if len(TargetArchList) == 0:\r
-            EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList)))\r
-        \r
+            EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON].SupArchList)))\r
+\r
         for Arch in ArchList:\r
         for Arch in ArchList:\r
-            GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].OutputDirectory)\r
-            GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, Options.BuildTarget, Options.ToolChain].PlatformName\r
+            GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].OutputDirectory)\r
+\r
+        # assign platform name based on last entry in ArchList\r
+        GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, ArchList[-1], FdsCommandDict.get("build_target"), FdsCommandDict.get("toolchain_tag")].PlatformName\r
 \r
 \r
-        if (Options.outputDir):\r
-            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)\r
+        if FdsCommandDict.get("platform_build_directory"):\r
+            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdsCommandDict.get("platform_build_directory"))\r
             if not os.path.isabs (OutputDirFromCommandLine):\r
                 OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)\r
             for Arch in ArchList:\r
             if not os.path.isabs (OutputDirFromCommandLine):\r
                 OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)\r
             for Arch in ArchList:\r
@@ -271,35 +307,38 @@ def main():
             GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir\r
 \r
         """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """\r
             GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir\r
 \r
         """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """\r
-        FdfParserObj = FdfParser.FdfParser(FdfFilename)\r
-        FdfParserObj.ParseFile()\r
+        if WorkSpaceDataBase:\r
+            FdfParserObj = GlobalData.gFdfParser\r
+        else:\r
+            FdfParserObj = FdfParser(FdfFilename)\r
+            FdfParserObj.ParseFile()\r
 \r
         if FdfParserObj.CycleReferenceCheck():\r
             EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")\r
 \r
 \r
         if FdfParserObj.CycleReferenceCheck():\r
             EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")\r
 \r
-        if (Options.uiFdName) :\r
-            if Options.uiFdName.upper() in FdfParserObj.Profile.FdDict.keys():\r
-                GenFds.OnlyGenerateThisFd = Options.uiFdName\r
+        if FdsCommandDict.get("fd"):\r
+            if FdsCommandDict.get("fd")[0].upper() in FdfParserObj.Profile.FdDict:\r
+                GenFds.OnlyGenerateThisFd = FdsCommandDict.get("fd")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such an FD in FDF file: %s" % Options.uiFdName)\r
+                                "No such an FD in FDF file: %s" % FdsCommandDict.get("fd")[0])\r
 \r
 \r
-        if (Options.uiFvName) :\r
-            if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict.keys():\r
-                GenFds.OnlyGenerateThisFv = Options.uiFvName\r
+        if FdsCommandDict.get("fv"):\r
+            if FdsCommandDict.get("fv")[0].upper() in FdfParserObj.Profile.FvDict:\r
+                GenFds.OnlyGenerateThisFv = FdsCommandDict.get("fv")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such an FV in FDF file: %s" % Options.uiFvName)\r
+                                "No such an FV in FDF file: %s" % FdsCommandDict.get("fv")[0])\r
 \r
 \r
-        if (Options.uiCapName) :\r
-            if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict.keys():\r
-                GenFds.OnlyGenerateThisCap = Options.uiCapName\r
+        if FdsCommandDict.get("cap"):\r
+            if FdsCommandDict.get("cap")[0].upper() in FdfParserObj.Profile.CapsuleDict:\r
+                GenFds.OnlyGenerateThisCap = FdsCommandDict.get("cap")[0]\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
             else:\r
                 EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
-                                "No such a Capsule in FDF file: %s" % Options.uiCapName)\r
+                                "No such a Capsule in FDF file: %s" % FdsCommandDict.get("cap")[0])\r
 \r
         GenFdsGlobalVariable.WorkSpace = BuildWorkSpace\r
 \r
         GenFdsGlobalVariable.WorkSpace = BuildWorkSpace\r
-        if ArchList != None:\r
+        if ArchList:\r
             GenFdsGlobalVariable.ArchList = ArchList\r
 \r
         # Dsc Build Data will handle Pcd Settings from CommandLine.\r
             GenFdsGlobalVariable.ArchList = ArchList\r
 \r
         # Dsc Build Data will handle Pcd Settings from CommandLine.\r
@@ -310,15 +349,15 @@ def main():
 \r
         # Record the FV Region info that may specific in the FD\r
         if FdfParserObj.Profile.FvDict and FdfParserObj.Profile.FdDict:\r
 \r
         # Record the FV Region info that may specific in the FD\r
         if FdfParserObj.Profile.FvDict and FdfParserObj.Profile.FdDict:\r
-            for Fv in FdfParserObj.Profile.FvDict:\r
-                FvObj = FdfParserObj.Profile.FvDict[Fv]\r
-                for Fd in FdfParserObj.Profile.FdDict:\r
-                    FdObj = FdfParserObj.Profile.FdDict[Fd]\r
+            for FvObj in FdfParserObj.Profile.FvDict.values():\r
+                for FdObj in FdfParserObj.Profile.FdDict.values():\r
                     for RegionObj in FdObj.RegionList:\r
                     for RegionObj in FdObj.RegionList:\r
-                        if RegionObj.RegionType != 'FV':\r
+                        if RegionObj.RegionType != BINARY_FILE_TYPE_FV:\r
                             continue\r
                         for RegionData in RegionObj.RegionDataList:\r
                             if FvObj.UiFvName.upper() == RegionData.upper():\r
                             continue\r
                         for RegionData in RegionObj.RegionDataList:\r
                             if FvObj.UiFvName.upper() == RegionData.upper():\r
+                                if not FvObj.BaseAddress:\r
+                                    FvObj.BaseAddress = '0x%x' % (int(FdObj.BaseAddress, 0) + RegionObj.Offset)\r
                                 if FvObj.FvRegionInFD:\r
                                     if FvObj.FvRegionInFD != RegionObj.Size:\r
                                         EdkLogger.error("GenFds", FORMAT_INVALID, "The FV %s's region is specified in multiple FD with different value." %FvObj.UiFvName)\r
                                 if FvObj.FvRegionInFD:\r
                                     if FvObj.FvRegionInFD != RegionObj.Size:\r
                                         EdkLogger.error("GenFds", FORMAT_INVALID, "The FV %s's region is specified in multiple FD with different value." %FvObj.UiFvName)\r
@@ -335,11 +374,11 @@ def main():
         """Display FV space info."""\r
         GenFds.DisplayFvSpaceInfo(FdfParserObj)\r
 \r
         """Display FV space info."""\r
         GenFds.DisplayFvSpaceInfo(FdfParserObj)\r
 \r
-    except FdfParser.Warning, X:\r
+    except Warning as X:\r
         EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)\r
         ReturnCode = FORMAT_INVALID\r
         EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)\r
         ReturnCode = FORMAT_INVALID\r
-    except FatalError, X:\r
-        if Options.debug != None:\r
+    except FatalError as X:\r
+        if FdsCommandDict.get("debug") is not None:\r
             import traceback\r
             EdkLogger.quiet(traceback.format_exc())\r
         ReturnCode = X.args[0]\r
             import traceback\r
             EdkLogger.quiet(traceback.format_exc())\r
         ReturnCode = X.args[0]\r
@@ -358,6 +397,30 @@ def main():
         ClearDuplicatedInf()\r
     return ReturnCode\r
 \r
         ClearDuplicatedInf()\r
     return ReturnCode\r
 \r
+def OptionsToCommandDict(Options):\r
+    FdsCommandDict = {}\r
+    FdsCommandDict["verbose"] = Options.verbose\r
+    FdsCommandDict["FixedAddress"] = Options.FixedAddress\r
+    FdsCommandDict["quiet"] = Options.quiet\r
+    FdsCommandDict["debug"] = Options.debug\r
+    FdsCommandDict["Workspace"] = Options.Workspace\r
+    FdsCommandDict["GenfdsMultiThread"] = Options.GenfdsMultiThread\r
+    FdsCommandDict["fdf_file"] = [PathClass(Options.filename)] if Options.filename else []\r
+    FdsCommandDict["build_target"] = Options.BuildTarget\r
+    FdsCommandDict["toolchain_tag"] = Options.ToolChain\r
+    FdsCommandDict["active_platform"] = Options.activePlatform\r
+    FdsCommandDict["OptionPcd"] = Options.OptionPcd\r
+    FdsCommandDict["conf_directory"] = Options.ConfDirectory\r
+    FdsCommandDict["IgnoreSources"] = Options.IgnoreSources\r
+    FdsCommandDict["macro"] = Options.Macros\r
+    FdsCommandDict["build_architecture_list"] = Options.archList\r
+    FdsCommandDict["platform_build_directory"] = Options.outputDir\r
+    FdsCommandDict["fd"] = [Options.uiFdName] if Options.uiFdName else []\r
+    FdsCommandDict["fv"] = [Options.uiFvName] if Options.uiFvName else []\r
+    FdsCommandDict["cap"] = [Options.uiCapName] if Options.uiCapName else []\r
+    return FdsCommandDict\r
+\r
+\r
 gParamCheck = []\r
 def SingleCheckCallback(option, opt_str, value, parser):\r
     if option not in gParamCheck:\r
 gParamCheck = []\r
 def SingleCheckCallback(option, opt_str, value, parser):\r
     if option not in gParamCheck:\r
@@ -366,151 +429,11 @@ def SingleCheckCallback(option, opt_str, value, parser):
     else:\r
         parser.error("Option %s only allows one instance in command line!" % option)\r
 \r
     else:\r
         parser.error("Option %s only allows one instance in command line!" % option)\r
 \r
-def CheckBuildOptionPcd():\r
-    for Arch in GenFdsGlobalVariable.ArchList:\r
-        PkgList  = GenFdsGlobalVariable.WorkSpace.GetPackageList(GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag)\r
-        for i, pcd in enumerate(GlobalData.BuildOptionPcd):\r
-            if type(pcd) is tuple:\r
-                continue\r
-            (pcdname, pcdvalue) = pcd.split('=')\r
-            if not pcdvalue:\r
-                EdkLogger.error('GenFds', OPTION_MISSING, "No Value specified for the PCD %s." % (pcdname))\r
-            if '.' in pcdname:\r
-                (TokenSpaceGuidCName, TokenCName) = pcdname.split('.')\r
-                HasTokenSpace = True\r
-            else:\r
-                TokenCName = pcdname\r
-                TokenSpaceGuidCName = ''\r
-                HasTokenSpace = False\r
-            TokenSpaceGuidCNameList = []\r
-            FoundFlag = False\r
-            PcdDatumType = ''\r
-            NewValue = ''\r
-            for package in PkgList:\r
-                for key in package.Pcds:\r
-                    PcdItem = package.Pcds[key]\r
-                    if HasTokenSpace:\r
-                        if (PcdItem.TokenCName, PcdItem.TokenSpaceGuidCName) == (TokenCName, TokenSpaceGuidCName):\r
-                            PcdDatumType = PcdItem.DatumType\r
-                            NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)\r
-                            FoundFlag = True\r
-                    else:\r
-                        if PcdItem.TokenCName == TokenCName:\r
-                            if not PcdItem.TokenSpaceGuidCName in TokenSpaceGuidCNameList:\r
-                                if len (TokenSpaceGuidCNameList) < 1:\r
-                                    TokenSpaceGuidCNameList.append(PcdItem.TokenSpaceGuidCName)\r
-                                    PcdDatumType = PcdItem.DatumType\r
-                                    TokenSpaceGuidCName = PcdItem.TokenSpaceGuidCName\r
-                                    NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)\r
-                                    FoundFlag = True\r
-                                else:\r
-                                    EdkLogger.error(\r
-                                            'GenFds',\r
-                                            PCD_VALIDATION_INFO_ERROR,\r
-                                            "The Pcd %s is found under multiple different TokenSpaceGuid: %s and %s." % (TokenCName, PcdItem.TokenSpaceGuidCName, TokenSpaceGuidCNameList[0])\r
-                                            )\r
-\r
-            GlobalData.BuildOptionPcd[i] = (TokenSpaceGuidCName, TokenCName, NewValue)\r
-\r
-\r
-## FindExtendTool()\r
-#\r
-#  Find location of tools to process data\r
-#\r
-#  @param  KeyStringList    Filter for inputs of section generation\r
-#  @param  CurrentArchList  Arch list\r
-#  @param  NameGuid         The Guid name\r
-#\r
-def FindExtendTool(KeyStringList, CurrentArchList, NameGuid):\r
-    ToolDb = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDatabase\r
-    # if user not specify filter, try to deduce it from global data.\r
-    if KeyStringList == None or KeyStringList == []:\r
-        Target = GenFdsGlobalVariable.TargetName\r
-        ToolChain = GenFdsGlobalVariable.ToolChainTag\r
-        if ToolChain not in ToolDb['TOOL_CHAIN_TAG']:\r
-            EdkLogger.error("GenFds", GENFDS_ERROR, "Can not find external tool because tool tag %s is not defined in tools_def.txt!" % ToolChain)\r
-        KeyStringList = [Target + '_' + ToolChain + '_' + CurrentArchList[0]]\r
-        for Arch in CurrentArchList:\r
-            if Target + '_' + ToolChain + '_' + Arch not in KeyStringList:\r
-                KeyStringList.append(Target + '_' + ToolChain + '_' + Arch)\r
-\r
-    if GenFdsGlobalVariable.GuidToolDefinition:\r
-        if NameGuid in GenFdsGlobalVariable.GuidToolDefinition.keys():\r
-            return GenFdsGlobalVariable.GuidToolDefinition[NameGuid]\r
-\r
-    ToolDefinition = ToolDefClassObject.ToolDefDict(GenFdsGlobalVariable.ConfDir).ToolsDefTxtDictionary\r
-    ToolPathTmp = None\r
-    ToolOption = None\r
-    ToolPathKey = None\r
-    ToolOptionKey = None\r
-    KeyList = None\r
-    for ToolDef in ToolDefinition.items():\r
-        if NameGuid == ToolDef[1]:\r
-            KeyList = ToolDef[0].split('_')\r
-            Key = KeyList[0] + \\r
-                  '_' + \\r
-                  KeyList[1] + \\r
-                  '_' + \\r
-                  KeyList[2]\r
-            if Key in KeyStringList and KeyList[4] == 'GUID':\r
-                ToolPathKey   = Key + '_' + KeyList[3] + '_PATH'\r
-                ToolOptionKey = Key + '_' + KeyList[3] + '_FLAGS'\r
-                ToolPath = ToolDefinition.get(ToolPathKey)\r
-                ToolOption = ToolDefinition.get(ToolOptionKey)\r
-                if ToolPathTmp == None:\r
-                    ToolPathTmp = ToolPath\r
-                else:\r
-                    if ToolPathTmp != ToolPath:\r
-                        EdkLogger.error("GenFds", GENFDS_ERROR, "Don't know which tool to use, %s or %s ?" % (ToolPathTmp, ToolPath))\r
-\r
-    BuildOption = {}\r
-    for Arch in CurrentArchList:\r
-        Platform = GenFdsGlobalVariable.WorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
-        # key is (ToolChainFamily, ToolChain, CodeBase)\r
-        for item in Platform.BuildOptions:\r
-            if '_PATH' in item[1] or '_FLAGS' in item[1] or '_GUID' in item[1]:\r
-                if not item[0] or (item[0] and GenFdsGlobalVariable.ToolChainFamily== item[0]):\r
-                    if item[1] not in BuildOption:\r
-                        BuildOption[item[1]] = Platform.BuildOptions[item]\r
-        if BuildOption:\r
-            ToolList = [TAB_TOD_DEFINES_TARGET, TAB_TOD_DEFINES_TOOL_CHAIN_TAG, TAB_TOD_DEFINES_TARGET_ARCH]\r
-            for Index in range(2, -1, -1):\r
-                for Key in dict(BuildOption):\r
-                    List = Key.split('_')\r
-                    if List[Index] == '*':\r
-                        for String in ToolDb[ToolList[Index]]:\r
-                            if String in [Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]:\r
-                                List[Index] = String\r
-                                NewKey = '%s_%s_%s_%s_%s' % tuple(List)\r
-                                if NewKey not in BuildOption:\r
-                                    BuildOption[NewKey] = BuildOption[Key]\r
-                                    continue\r
-                                del BuildOption[Key]\r
-                    elif List[Index] not in ToolDb[ToolList[Index]]:\r
-                        del BuildOption[Key]\r
-    if BuildOption:\r
-        if not KeyList:\r
-            for Op in BuildOption:\r
-                if NameGuid == BuildOption[Op]:\r
-                    KeyList = Op.split('_')\r
-                    Key = KeyList[0] + '_' + KeyList[1] +'_' + KeyList[2]\r
-                    if Key in KeyStringList and KeyList[4] == 'GUID':\r
-                        ToolPathKey   = Key + '_' + KeyList[3] + '_PATH'\r
-                        ToolOptionKey = Key + '_' + KeyList[3] + '_FLAGS'\r
-        if ToolPathKey in BuildOption.keys():\r
-            ToolPathTmp = BuildOption.get(ToolPathKey)\r
-        if ToolOptionKey in BuildOption.keys():\r
-            ToolOption = BuildOption.get(ToolOptionKey)\r
-\r
-    GenFdsGlobalVariable.GuidToolDefinition[NameGuid] = (ToolPathTmp, ToolOption)\r
-    return ToolPathTmp, ToolOption\r
-\r
 ## Parse command line options\r
 #\r
 # Using standard Python module optparse to parse command line option of this tool.\r
 #\r
 #   @retval Opt   A optparse.Values object containing the parsed options\r
 ## Parse command line options\r
 #\r
 # Using standard Python module optparse to parse command line option of this tool.\r
 #\r
 #   @retval Opt   A optparse.Values object containing the parsed options\r
-#   @retval Args  Target of build command\r
 #\r
 def myOptionParser():\r
     usage = "%prog [options] -f input_file -a arch_list -b build_target -p active_platform -t tool_chain_tag -D \"MacroName [= MacroValue]\""\r
 #\r
 def myOptionParser():\r
     usage = "%prog [options] -f input_file -a arch_list -b build_target -p active_platform -t tool_chain_tag -D \"MacroName [= MacroValue]\""\r
@@ -540,7 +463,7 @@ def myOptionParser():
     Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ")\r
     Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.")\r
 \r
     Parser.add_option("--pcd", action="append", dest="OptionPcd", help="Set PCD value by command line. Format: \"PcdName=Value\" ")\r
     Parser.add_option("--genfds-multi-thread", action="store_true", dest="GenfdsMultiThread", default=False, help="Enable GenFds multi thread to generate ffs file.")\r
 \r
-    (Options, args) = Parser.parse_args()\r
+    Options, _ = Parser.parse_args()\r
     return Options\r
 \r
 ## The class implementing the EDK2 flash image generation process\r
     return Options\r
 \r
 ## The class implementing the EDK2 flash image generation process\r
@@ -550,10 +473,8 @@ def myOptionParser():
 #       2. Call methods of Fd class to generate FD\r
 #       3. Call methods of Fv class to generate FV that not belong to FD\r
 #\r
 #       2. Call methods of Fd class to generate FD\r
 #       3. Call methods of Fv class to generate FV that not belong to FD\r
 #\r
-class GenFds :\r
+class GenFds(object):\r
     FdfParsef = None\r
     FdfParsef = None\r
-    # FvName, FdName, CapName in FDF, Image file name\r
-    ImageBinDict = {}\r
     OnlyGenerateThisFd = None\r
     OnlyGenerateThisFv = None\r
     OnlyGenerateThisCap = None\r
     OnlyGenerateThisFd = None\r
     OnlyGenerateThisFv = None\r
     OnlyGenerateThisCap = None\r
@@ -561,71 +482,66 @@ class GenFds :
     ## GenFd()\r
     #\r
     #   @param  OutputDir           Output directory\r
     ## GenFd()\r
     #\r
     #   @param  OutputDir           Output directory\r
-    #   @param  FdfParser           FDF contents parser\r
+    #   @param  FdfParserObject     FDF contents parser\r
     #   @param  Workspace           The directory of workspace\r
     #   @param  ArchList            The Arch list of platform\r
     #\r
     #   @param  Workspace           The directory of workspace\r
     #   @param  ArchList            The Arch list of platform\r
     #\r
-    def GenFd (OutputDir, FdfParser, WorkSpace, ArchList):\r
-        GenFdsGlobalVariable.SetDir ('', FdfParser, WorkSpace, ArchList)\r
+    @staticmethod\r
+    def GenFd (OutputDir, FdfParserObject, WorkSpace, ArchList):\r
+        GenFdsGlobalVariable.SetDir ('', FdfParserObject, WorkSpace, ArchList)\r
 \r
         GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")\r
 \r
         GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")\r
-        if GenFds.OnlyGenerateThisCap != None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
-            CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.get(GenFds.OnlyGenerateThisCap.upper())\r
-            if CapsuleObj != None:\r
+        if GenFds.OnlyGenerateThisCap is not None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict:\r
+            CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[GenFds.OnlyGenerateThisCap.upper()]\r
+            if CapsuleObj is not None:\r
                 CapsuleObj.GenCapsule()\r
                 return\r
 \r
                 CapsuleObj.GenCapsule()\r
                 return\r
 \r
-        if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
-            FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(GenFds.OnlyGenerateThisFd.upper())\r
-            if FdObj != None:\r
+        if GenFds.OnlyGenerateThisFd is not None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
+            FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
+            if FdObj is not None:\r
                 FdObj.GenFd()\r
                 return\r
                 FdObj.GenFd()\r
                 return\r
-        elif GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisFv == None:\r
-            for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
-                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
+        elif GenFds.OnlyGenerateThisFd is None and GenFds.OnlyGenerateThisFv is None:\r
+            for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
                 FdObj.GenFd()\r
 \r
         GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")\r
                 FdObj.GenFd()\r
 \r
         GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")\r
-        if GenFds.OnlyGenerateThisFv != None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-            FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(GenFds.OnlyGenerateThisFv.upper())\r
-            if FvObj != None:\r
-                Buffer = StringIO.StringIO()\r
+        if GenFds.OnlyGenerateThisFv is not None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict:\r
+            FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[GenFds.OnlyGenerateThisFv.upper()]\r
+            if FvObj is not None:\r
+                Buffer = BytesIO()\r
                 FvObj.AddToBuffer(Buffer)\r
                 Buffer.close()\r
                 return\r
                 FvObj.AddToBuffer(Buffer)\r
                 Buffer.close()\r
                 return\r
-        elif GenFds.OnlyGenerateThisFv == None:\r
-            for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-                Buffer = StringIO.StringIO('')\r
-                FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]\r
+        elif GenFds.OnlyGenerateThisFv is None:\r
+            for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
+                Buffer = BytesIO()\r
                 FvObj.AddToBuffer(Buffer)\r
                 Buffer.close()\r
                 FvObj.AddToBuffer(Buffer)\r
                 Buffer.close()\r
-        \r
-        if GenFds.OnlyGenerateThisFv == None and GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisCap == None:\r
+\r
+        if GenFds.OnlyGenerateThisFv is None and GenFds.OnlyGenerateThisFd is None and GenFds.OnlyGenerateThisCap is None:\r
             if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:\r
                 GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")\r
             if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:\r
                 GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")\r
-                for CapsuleName in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():\r
-                    CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[CapsuleName]\r
+                for CapsuleObj in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.values():\r
                     CapsuleObj.GenCapsule()\r
 \r
             if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
                 GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")\r
                     CapsuleObj.GenCapsule()\r
 \r
             if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
                 GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")\r
-                for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():\r
-                    OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]\r
+                for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
                     OptRomObj.AddToBuffer(None)\r
                     OptRomObj.AddToBuffer(None)\r
+\r
     @staticmethod\r
     @staticmethod\r
-    def GenFfsMakefile(OutputDir, FdfParser, WorkSpace, ArchList, GlobalData):\r
-        GenFdsGlobalVariable.SetEnv(FdfParser, WorkSpace, ArchList, GlobalData)\r
-        for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
-            FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]\r
+    def GenFfsMakefile(OutputDir, FdfParserObject, WorkSpace, ArchList, GlobalData):\r
+        GenFdsGlobalVariable.SetEnv(FdfParserObject, WorkSpace, ArchList, GlobalData)\r
+        for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
             FdObj.GenFd(Flag=True)\r
 \r
             FdObj.GenFd(Flag=True)\r
 \r
-        for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():\r
-            FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]\r
+        for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
             FvObj.AddToBuffer(Buffer=None, Flag=True)\r
 \r
         if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
             FvObj.AddToBuffer(Buffer=None, Flag=True)\r
 \r
         if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
-            for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():\r
-                OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]\r
+            for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
                 OptRomObj.AddToBuffer(Buffer=None, Flag=True)\r
 \r
         return GenFdsGlobalVariable.FfsCmdDict\r
                 OptRomObj.AddToBuffer(Buffer=None, Flag=True)\r
 \r
         return GenFdsGlobalVariable.FfsCmdDict\r
@@ -635,17 +551,18 @@ class GenFds :
     #   @param  FvObj           Whose block size to get\r
     #   @retval int             Block size value\r
     #\r
     #   @param  FvObj           Whose block size to get\r
     #   @retval int             Block size value\r
     #\r
+    @staticmethod\r
     def GetFvBlockSize(FvObj):\r
         DefaultBlockSize = 0x1\r
         FdObj = None\r
     def GetFvBlockSize(FvObj):\r
         DefaultBlockSize = 0x1\r
         FdObj = None\r
-        if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():\r
+        if GenFds.OnlyGenerateThisFd is not None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict:\r
             FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
             FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]\r
-        if FdObj == None:\r
+        if FdObj is None:\r
             for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
                 for ElementRegion in ElementFd.RegionList:\r
             for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
                 for ElementRegion in ElementFd.RegionList:\r
-                    if ElementRegion.RegionType == 'FV':\r
+                    if ElementRegion.RegionType == BINARY_FILE_TYPE_FV:\r
                         for ElementRegionData in ElementRegion.RegionDataList:\r
                         for ElementRegionData in ElementRegion.RegionDataList:\r
-                            if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:\r
+                            if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
                                 if FvObj.BlockSizeList != []:\r
                                     return FvObj.BlockSizeList[0][0]\r
                                 else:\r
                                 if FvObj.BlockSizeList != []:\r
                                     return FvObj.BlockSizeList[0][0]\r
                                 else:\r
@@ -655,9 +572,9 @@ class GenFds :
             return DefaultBlockSize\r
         else:\r
             for ElementRegion in FdObj.RegionList:\r
             return DefaultBlockSize\r
         else:\r
             for ElementRegion in FdObj.RegionList:\r
-                    if ElementRegion.RegionType == 'FV':\r
+                    if ElementRegion.RegionType == BINARY_FILE_TYPE_FV:\r
                         for ElementRegionData in ElementRegion.RegionDataList:\r
                         for ElementRegionData in ElementRegion.RegionDataList:\r
-                            if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:\r
+                            if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
                                 if FvObj.BlockSizeList != []:\r
                                     return FvObj.BlockSizeList[0][0]\r
                                 else:\r
                                 if FvObj.BlockSizeList != []:\r
                                     return FvObj.BlockSizeList[0][0]\r
                                 else:\r
@@ -669,16 +586,17 @@ class GenFds :
     #   @param  FvObj           Whose block size to get\r
     #   @retval None\r
     #\r
     #   @param  FvObj           Whose block size to get\r
     #   @retval None\r
     #\r
-    def DisplayFvSpaceInfo(FdfParser):\r
-        \r
+    @staticmethod\r
+    def DisplayFvSpaceInfo(FdfParserObject):\r
+\r
         FvSpaceInfoList = []\r
         MaxFvNameLength = 0\r
         FvSpaceInfoList = []\r
         MaxFvNameLength = 0\r
-        for FvName in FdfParser.Profile.FvDict:\r
+        for FvName in FdfParserObject.Profile.FvDict:\r
             if len(FvName) > MaxFvNameLength:\r
                 MaxFvNameLength = len(FvName)\r
             FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')\r
             if os.path.exists(FvSpaceInfoFileName):\r
             if len(FvName) > MaxFvNameLength:\r
                 MaxFvNameLength = len(FvName)\r
             FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')\r
             if os.path.exists(FvSpaceInfoFileName):\r
-                FileLinesList = linecache.getlines(FvSpaceInfoFileName)\r
+                FileLinesList = getlines(FvSpaceInfoFileName)\r
                 TotalFound = False\r
                 Total = ''\r
                 UsedFound = False\r
                 TotalFound = False\r
                 Total = ''\r
                 UsedFound = False\r
@@ -697,16 +615,16 @@ class GenFds :
                         if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':\r
                             FreeFound = True\r
                             Free = NameValue[1].strip()\r
                         if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':\r
                             FreeFound = True\r
                             Free = NameValue[1].strip()\r
-                \r
+\r
                 if TotalFound and UsedFound and FreeFound:\r
                     FvSpaceInfoList.append((FvName, Total, Used, Free))\r
                 if TotalFound and UsedFound and FreeFound:\r
                     FvSpaceInfoList.append((FvName, Total, Used, Free))\r
-                \r
+\r
         GenFdsGlobalVariable.InfLogger('\nFV Space Information')\r
         for FvSpaceInfo in FvSpaceInfoList:\r
             Name = FvSpaceInfo[0]\r
         GenFdsGlobalVariable.InfLogger('\nFV Space Information')\r
         for FvSpaceInfo in FvSpaceInfoList:\r
             Name = FvSpaceInfo[0]\r
-            TotalSizeValue = long(FvSpaceInfo[1], 0)\r
-            UsedSizeValue = long(FvSpaceInfo[2], 0)\r
-            FreeSizeValue = long(FvSpaceInfo[3], 0)\r
+            TotalSizeValue = int(FvSpaceInfo[1], 0)\r
+            UsedSizeValue = int(FvSpaceInfo[2], 0)\r
+            FreeSizeValue = int(FvSpaceInfo[3], 0)\r
             if UsedSizeValue == TotalSizeValue:\r
                 Percentage = '100'\r
             else:\r
             if UsedSizeValue == TotalSizeValue:\r
                 Percentage = '100'\r
             else:\r
@@ -720,68 +638,81 @@ class GenFds :
     #   @param  DscFile         modules from dsc file will be preprocessed\r
     #   @retval None\r
     #\r
     #   @param  DscFile         modules from dsc file will be preprocessed\r
     #   @retval None\r
     #\r
+    @staticmethod\r
     def PreprocessImage(BuildDb, DscFile):\r
     def PreprocessImage(BuildDb, DscFile):\r
-        PcdDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Pcds\r
+        PcdDict = BuildDb.BuildObject[DscFile, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Pcds\r
         PcdValue = ''\r
         for Key in PcdDict:\r
             PcdObj = PcdDict[Key]\r
             if PcdObj.TokenCName == 'PcdBsBaseAddress':\r
                 PcdValue = PcdObj.DefaultValue\r
                 break\r
         PcdValue = ''\r
         for Key in PcdDict:\r
             PcdObj = PcdDict[Key]\r
             if PcdObj.TokenCName == 'PcdBsBaseAddress':\r
                 PcdValue = PcdObj.DefaultValue\r
                 break\r
-        \r
+\r
         if PcdValue == '':\r
             return\r
         if PcdValue == '':\r
             return\r
-        \r
-        Int64PcdValue = long(PcdValue, 0)\r
-        if Int64PcdValue == 0 or Int64PcdValue < -1:    \r
+\r
+        Int64PcdValue = int(PcdValue, 0)\r
+        if Int64PcdValue == 0 or Int64PcdValue < -1:\r
             return\r
             return\r
-                \r
+\r
         TopAddress = 0\r
         if Int64PcdValue > 0:\r
             TopAddress = Int64PcdValue\r
         TopAddress = 0\r
         if Int64PcdValue > 0:\r
             TopAddress = Int64PcdValue\r
-            \r
-        ModuleDict = BuildDb.BuildObject[DscFile, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules\r
+\r
+        ModuleDict = BuildDb.BuildObject[DscFile, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules\r
         for Key in ModuleDict:\r
         for Key in ModuleDict:\r
-            ModuleObj = BuildDb.BuildObject[Key, 'COMMON', GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
-            print ModuleObj.BaseName + ' ' + ModuleObj.ModuleType\r
+            ModuleObj = BuildDb.BuildObject[Key, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
+            print(ModuleObj.BaseName + ' ' + ModuleObj.ModuleType)\r
 \r
 \r
+    @staticmethod\r
     def GenerateGuidXRefFile(BuildDb, ArchList, FdfParserObj):\r
         GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")\r
     def GenerateGuidXRefFile(BuildDb, ArchList, FdfParserObj):\r
         GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")\r
-        GuidXRefFile = StringIO.StringIO('')\r
+        GuidXRefFile = []\r
+        PkgGuidDict = {}\r
         GuidDict = {}\r
         ModuleList = []\r
         FileGuidList = []\r
         GuidDict = {}\r
         ModuleList = []\r
         FileGuidList = []\r
+        VariableGuidSet = set()\r
         for Arch in ArchList:\r
             PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
         for Arch in ArchList:\r
             PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
+            PkgList = GenFdsGlobalVariable.WorkSpace.GetPackageList(GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag)\r
+            for P in PkgList:\r
+                PkgGuidDict.update(P.Guids)\r
+            for Name, Guid in PlatformDataBase.Pcds:\r
+                Pcd = PlatformDataBase.Pcds[Name, Guid]\r
+                if Pcd.Type in [TAB_PCDS_DYNAMIC_HII, TAB_PCDS_DYNAMIC_EX_HII]:\r
+                    for SkuId in Pcd.SkuInfoList:\r
+                        Sku = Pcd.SkuInfoList[SkuId]\r
+                        if Sku.VariableGuid in VariableGuidSet:continue\r
+                        VariableGuidSet.add(Sku.VariableGuid)\r
+                        if Sku.VariableGuid and Sku.VariableGuid in PkgGuidDict.keys():\r
+                            GuidDict[Sku.VariableGuid] = PkgGuidDict[Sku.VariableGuid]\r
             for ModuleFile in PlatformDataBase.Modules:\r
                 Module = BuildDb.BuildObject[ModuleFile, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
                 if Module in ModuleList:\r
                     continue\r
                 else:\r
                     ModuleList.append(Module)\r
             for ModuleFile in PlatformDataBase.Modules:\r
                 Module = BuildDb.BuildObject[ModuleFile, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
                 if Module in ModuleList:\r
                     continue\r
                 else:\r
                     ModuleList.append(Module)\r
-                GuidXRefFile.write("%s %s\n" % (Module.Guid, Module.BaseName))\r
-                for key, item in Module.Protocols.items():\r
-                    GuidDict[key] = item\r
-                for key, item in Module.Guids.items():\r
-                    GuidDict[key] = item\r
-                for key, item in Module.Ppis.items():\r
-                    GuidDict[key] = item\r
+                if GlobalData.gGuidPattern.match(ModuleFile.BaseName):\r
+                    GuidXRefFile.append("%s %s\n" % (ModuleFile.BaseName, Module.BaseName))\r
+                else:\r
+                    GuidXRefFile.append("%s %s\n" % (Module.Guid, Module.BaseName))\r
+                GuidDict.update(Module.Protocols)\r
+                GuidDict.update(Module.Guids)\r
+                GuidDict.update(Module.Ppis)\r
             for FvName in FdfParserObj.Profile.FvDict:\r
                 for FfsObj in FdfParserObj.Profile.FvDict[FvName].FfsList:\r
             for FvName in FdfParserObj.Profile.FvDict:\r
                 for FfsObj in FdfParserObj.Profile.FvDict[FvName].FfsList:\r
-                    if not isinstance(FfsObj, FfsFileStatement.FileStatement):\r
+                    if not isinstance(FfsObj, FileStatement):\r
                         InfPath = PathClass(NormPath(mws.join(GenFdsGlobalVariable.WorkSpaceDir, FfsObj.InfFileName)))\r
                         FdfModule = BuildDb.BuildObject[InfPath, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
                         if FdfModule in ModuleList:\r
                             continue\r
                         else:\r
                             ModuleList.append(FdfModule)\r
                         InfPath = PathClass(NormPath(mws.join(GenFdsGlobalVariable.WorkSpaceDir, FfsObj.InfFileName)))\r
                         FdfModule = BuildDb.BuildObject[InfPath, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
                         if FdfModule in ModuleList:\r
                             continue\r
                         else:\r
                             ModuleList.append(FdfModule)\r
-                        GuidXRefFile.write("%s %s\n" % (FdfModule.Guid, FdfModule.BaseName))\r
-                        for key, item in FdfModule.Protocols.items():\r
-                            GuidDict[key] = item\r
-                        for key, item in FdfModule.Guids.items():\r
-                            GuidDict[key] = item\r
-                        for key, item in FdfModule.Ppis.items():\r
-                            GuidDict[key] = item\r
+                        GuidXRefFile.append("%s %s\n" % (FdfModule.Guid, FdfModule.BaseName))\r
+                        GuidDict.update(FdfModule.Protocols)\r
+                        GuidDict.update(FdfModule.Guids)\r
+                        GuidDict.update(FdfModule.Ppis)\r
                     else:\r
                         FileStatementGuid = FfsObj.NameGuid\r
                         if FileStatementGuid in FileGuidList:\r
                     else:\r
                         FileStatementGuid = FfsObj.NameGuid\r
                         if FileStatementGuid in FileGuidList:\r
@@ -790,13 +721,13 @@ class GenFds :
                             FileGuidList.append(FileStatementGuid)\r
                         Name = []\r
                         FfsPath = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')\r
                             FileGuidList.append(FileStatementGuid)\r
                         Name = []\r
                         FfsPath = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')\r
-                        FfsPath = glob.glob(os.path.join(FfsPath, FileStatementGuid) + '*')\r
+                        FfsPath = glob(os.path.join(FfsPath, FileStatementGuid) + TAB_STAR)\r
                         if not FfsPath:\r
                             continue\r
                         if not os.path.exists(FfsPath[0]):\r
                             continue\r
                         MatchDict = {}\r
                         if not FfsPath:\r
                             continue\r
                         if not os.path.exists(FfsPath[0]):\r
                             continue\r
                         MatchDict = {}\r
-                        ReFileEnds = re.compile('\S+(.ui)$|\S+(fv.sec.txt)$|\S+(.pe32.txt)$|\S+(.te.txt)$|\S+(.pic.txt)$|\S+(.raw.txt)$|\S+(.ffs.txt)$')\r
+                        ReFileEnds = compile('\S+(.ui)$|\S+(fv.sec.txt)$|\S+(.pe32.txt)$|\S+(.te.txt)$|\S+(.pic.txt)$|\S+(.raw.txt)$|\S+(.ffs.txt)$')\r
                         FileList = os.listdir(FfsPath[0])\r
                         for File in FileList:\r
                             Match = ReFileEnds.search(File)\r
                         FileList = os.listdir(FfsPath[0])\r
                         for File in FileList:\r
                             Match = ReFileEnds.search(File)\r
@@ -814,8 +745,8 @@ class GenFds :
                                     F.read()\r
                                     length = F.tell()\r
                                     F.seek(4)\r
                                     F.read()\r
                                     length = F.tell()\r
                                     F.seek(4)\r
-                                    TmpStr = unpack('%dh' % ((length - 4) / 2), F.read())\r
-                                    Name = ''.join([chr(c) for c in TmpStr[:-1]])\r
+                                    TmpStr = unpack('%dh' % ((length - 4) // 2), F.read())\r
+                                    Name = ''.join(chr(c) for c in TmpStr[:-1])\r
                         else:\r
                             FileList = []\r
                             if 'fv.sec.txt' in MatchDict:\r
                         else:\r
                             FileList = []\r
                             if 'fv.sec.txt' in MatchDict:\r
@@ -838,31 +769,26 @@ class GenFds :
                         if not Name:\r
                             continue\r
 \r
                         if not Name:\r
                             continue\r
 \r
-                        Name = ' '.join(Name) if type(Name) == type([]) else Name\r
-                        GuidXRefFile.write("%s %s\n" %(FileStatementGuid, Name))\r
+                        Name = ' '.join(Name) if isinstance(Name, type([])) else Name\r
+                        GuidXRefFile.append("%s %s\n" %(FileStatementGuid, Name))\r
 \r
        # Append GUIDs, Protocols, and PPIs to the Xref file\r
 \r
        # Append GUIDs, Protocols, and PPIs to the Xref file\r
-        GuidXRefFile.write("\n")\r
+        GuidXRefFile.append("\n")\r
         for key, item in GuidDict.items():\r
         for key, item in GuidDict.items():\r
-            GuidXRefFile.write("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))\r
+            GuidXRefFile.append("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))\r
 \r
 \r
-        if GuidXRefFile.getvalue():\r
-            SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), False)\r
+        if GuidXRefFile:\r
+            GuidXRefFile = ''.join(GuidXRefFile)\r
+            SaveFileOnChange(GuidXRefFileName, GuidXRefFile, False)\r
             GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)\r
         elif os.path.exists(GuidXRefFileName):\r
             os.remove(GuidXRefFileName)\r
             GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)\r
         elif os.path.exists(GuidXRefFileName):\r
             os.remove(GuidXRefFileName)\r
-        GuidXRefFile.close()\r
 \r
 \r
-    ##Define GenFd as static function\r
-    GenFd = staticmethod(GenFd)\r
-    GetFvBlockSize = staticmethod(GetFvBlockSize)\r
-    DisplayFvSpaceInfo = staticmethod(DisplayFvSpaceInfo)\r
-    PreprocessImage = staticmethod(PreprocessImage)\r
-    GenerateGuidXRefFile = staticmethod(GenerateGuidXRefFile)\r
 \r
 if __name__ == '__main__':\r
     r = main()\r
     ## 0-127 is a safe return range, and 1 is a standard default error\r
 \r
 if __name__ == '__main__':\r
     r = main()\r
     ## 0-127 is a safe return range, and 1 is a standard default error\r
-    if r < 0 or r > 127: r = 1\r
-    sys.exit(r)\r
+    if r < 0 or r > 127:\r
+        r = 1\r
+    exit(r)\r
 \r
 \r