]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/GenFds/GenFds.py
BaseTools: refactor and remove un-needed use of .keys() on dictionaries
[mirror_edk2.git] / BaseTools / Source / Python / GenFds / GenFds.py
index 9088a876e4c9d889753622e1b6224a7b94732cb1..54c7d828305f7c58a42378280e622daf9c47909f 100644 (file)
-## @file
-# generate flash image
-#
-#  Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
-#
-#  This program and the accompanying materials
-#  are licensed and made available under the terms and conditions of the BSD License
-#  which accompanies this distribution.  The full text of the license may be found at
-#  http://opensource.org/licenses/bsd-license.php
-#
-#  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-#  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-
-##
-# Import Modules
-#
-from optparse import OptionParser
-import sys
-import os
-import linecache
-import FdfParser
-import Common.BuildToolError as BuildToolError
-from GenFdsGlobalVariable import GenFdsGlobalVariable
-from Workspace.WorkspaceDatabase import WorkspaceDatabase
-from Workspace.BuildClassObject import PcdClassObject
-from Workspace.BuildClassObject import ModuleBuildClassObject
-import RuleComplexFile
-from EfiSection import EfiSection
-import StringIO
-import Common.TargetTxtClassObject as TargetTxtClassObject
-import Common.ToolDefClassObject as ToolDefClassObject
-import Common.DataType
-import Common.GlobalData as GlobalData
-from Common import EdkLogger
-from Common.String import *
-from Common.Misc import DirCache,PathClass
-from Common.Misc import SaveFileOnChange
-
-## Version and Copyright
-versionNumber = "1.0"
-__version__ = "%prog Version " + versionNumber
-__copyright__ = "Copyright (c) 2007 - 2010, Intel Corporation  All rights reserved."
-
-## Tool entrance method
-#
-# This method mainly dispatch specific methods per the command line options.
-# If no error found, return zero value so the caller of this tool can know
-# if it's executed successfully or not.
-#
-#   @retval 0     Tool was successful
-#   @retval 1     Tool failed
-#
-def main():
-    global Options
-    Options = myOptionParser()
-
-    global Workspace
-    Workspace = ""
-    ArchList = None
-    ReturnCode = 0
-
-    EdkLogger.Initialize()
-    try:
-        if Options.verbose != None:
-            EdkLogger.SetLevel(EdkLogger.VERBOSE)
-            GenFdsGlobalVariable.VerboseMode = True
-            
-        if Options.FixedAddress != None:
-            GenFdsGlobalVariable.FixedLoadAddress = True
-            
-        if Options.quiet != None:
-            EdkLogger.SetLevel(EdkLogger.QUIET)
-        if Options.debug != None:
-            EdkLogger.SetLevel(Options.debug + 1)
-            GenFdsGlobalVariable.DebugLevel = Options.debug
-        else:
-            EdkLogger.SetLevel(EdkLogger.INFO)
-
-        if (Options.Workspace == None):
-            EdkLogger.error("GenFds", OPTION_MISSING, "WORKSPACE not defined",
-                            ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")
-        elif not os.path.exists(Options.Workspace):
-            EdkLogger.error("GenFds", PARAMETER_INVALID, "WORKSPACE is invalid",
-                            ExtraData="Please use '-w' switch to pass it or set the WORKSPACE environment variable.")
-        else:
-            Workspace = os.path.normcase(Options.Workspace)
-            GenFdsGlobalVariable.WorkSpaceDir = Workspace
-            if 'EDK_SOURCE' in os.environ.keys():
-                GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])
-            if (Options.debug):
-                GenFdsGlobalVariable.VerboseLogger( "Using Workspace:" + Workspace)
-        os.chdir(GenFdsGlobalVariable.WorkSpaceDir)
-
-        if (Options.filename):
-            FdfFilename = Options.filename
-            FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)
-
-            if FdfFilename[0:2] == '..':
-                FdfFilename = os.path.realpath(FdfFilename)
-            if not os.path.isabs (FdfFilename):
-                FdfFilename = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, FdfFilename)
-            if not os.path.exists(FdfFilename):
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=FdfFilename)
-            if os.path.normcase (FdfFilename).find(Workspace) != 0:
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, "FdfFile doesn't exist in Workspace!")
-
-            GenFdsGlobalVariable.FdfFile = FdfFilename
-            GenFdsGlobalVariable.FdfFileTimeStamp = os.path.getmtime(FdfFilename)
-        else:
-            EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")
-
-        if (Options.BuildTarget):
-            GenFdsGlobalVariable.TargetName = Options.BuildTarget
-        else:
-            EdkLogger.error("GenFds", OPTION_MISSING, "Missing build target")
-
-        if (Options.ToolChain):
-            GenFdsGlobalVariable.ToolChainTag = Options.ToolChain
-        else:
-            EdkLogger.error("GenFds", OPTION_MISSING, "Missing tool chain tag")
-
-        if (Options.activePlatform):
-            ActivePlatform = Options.activePlatform
-            ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)
-
-            if ActivePlatform[0:2] == '..':
-                ActivePlatform = os.path.realpath(ActivePlatform)
-
-            if not os.path.isabs (ActivePlatform):
-                ActivePlatform = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, ActivePlatform)
-
-            if not os.path.exists(ActivePlatform)  :
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")
-
-            if os.path.normcase (ActivePlatform).find(Workspace) != 0:
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist in Workspace!")
-
-            ActivePlatform = ActivePlatform[len(Workspace):]
-            if len(ActivePlatform) > 0 :
-                if ActivePlatform[0] == '\\' or ActivePlatform[0] == '/':
-                    ActivePlatform = ActivePlatform[1:]
-            else:
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")
-        else:
-            EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")
-
-        GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform), Workspace)
-
-        BuildConfigurationFile = os.path.normpath(os.path.join(GenFdsGlobalVariable.WorkSpaceDir, "Conf/target.txt"))
-        if os.path.isfile(BuildConfigurationFile) == True:
-            TargetTxtClassObject.TargetTxtClassObject(BuildConfigurationFile)
-        else:
-            EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)
-
-        if Options.Macros:
-            for Pair in Options.Macros:
-                Pair.strip('"')
-                List = Pair.split('=')
-                if len(List) == 2:
-                    if List[0].strip() == "EFI_SOURCE":
-                        GlobalData.gEfiSource = List[1].strip()
-                        continue
-                    elif List[0].strip() == "EDK_SOURCE":
-                        GlobalData.gEdkSource = List[1].strip()
-                        continue
-                    else:
-                        GlobalData.gEdkGlobal[List[0].strip()] = List[1].strip()
-                        FdfParser.InputMacroDict[List[0].strip()] = List[1].strip()
-                else:
-                    FdfParser.InputMacroDict[List[0].strip()] = ""
-
-        """call Workspace build create database"""
-        os.environ["WORKSPACE"] = Workspace
-        FdfParser.InputMacroDict["WORKSPACE"] = Workspace
-        BuildWorkSpace = WorkspaceDatabase(':memory:', FdfParser.InputMacroDict)
-        BuildWorkSpace.InitDatabase()
-        
-        #
-        # Get files real name in workspace dir
-        #
-        GlobalData.gAllFiles = DirCache(Workspace)
-        GlobalData.gWorkspace = Workspace
-
-        if (Options.archList) :
-            ArchList = Options.archList.split(',')
-        else:
-#            EdkLogger.error("GenFds", OPTION_MISSING, "Missing build ARCH")
-            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList
-
-        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList) & set(ArchList)
-        if len(TargetArchList) == 0:
-            EdkLogger.error("GenFds", GENFDS_ERROR, "Target ARCH %s not in platform supported ARCH %s" % (str(ArchList), str(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, 'COMMON'].SupArchList)))
-        
-        for Arch in ArchList:
-            GenFdsGlobalVariable.OutputDirFromDscDict[Arch] = NormPath(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch].OutputDirectory)
-            GenFdsGlobalVariable.PlatformName = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch].PlatformName
-
-        if (Options.outputDir):
-            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)
-            if not os.path.isabs (OutputDirFromCommandLine):
-                OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)
-            for Arch in ArchList:
-                GenFdsGlobalVariable.OutputDirDict[Arch] = OutputDirFromCommandLine
-        else:
-            for Arch in ArchList:
-                GenFdsGlobalVariable.OutputDirDict[Arch] = os.path.join(GenFdsGlobalVariable.OutputDirFromDscDict[Arch], GenFdsGlobalVariable.TargetName + '_' + GenFdsGlobalVariable.ToolChainTag)
-
-        for Key in GenFdsGlobalVariable.OutputDirDict:
-            OutputDir = GenFdsGlobalVariable.OutputDirDict[Key]
-            if OutputDir[0:2] == '..':
-                OutputDir = os.path.realpath(OutputDir)
-
-            if OutputDir[1] != ':':
-                OutputDir = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, OutputDir)
-
-            if not os.path.exists(OutputDir):
-                EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=OutputDir)
-            GenFdsGlobalVariable.OutputDirDict[Key] = OutputDir
-
-        """ Parse Fdf file, has to place after build Workspace as FDF may contain macros from DSC file """
-        FdfParserObj = FdfParser.FdfParser(FdfFilename)
-        FdfParserObj.ParseFile()
-
-        if FdfParserObj.CycleReferenceCheck():
-            EdkLogger.error("GenFds", FORMAT_NOT_SUPPORTED, "Cycle Reference Detected in FDF file")
-
-        if (Options.uiFdName) :
-            if Options.uiFdName.upper() in FdfParserObj.Profile.FdDict.keys():
-                GenFds.OnlyGenerateThisFd = Options.uiFdName
-            else:
-                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
-                                "No such an FD in FDF file: %s" % Options.uiFdName)
-
-        if (Options.uiFvName) :
-            if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict.keys():
-                GenFds.OnlyGenerateThisFv = Options.uiFvName
-            else:
-                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
-                                "No such an FV in FDF file: %s" % Options.uiFvName)
-
-        if (Options.uiCapName) :
-            if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict.keys():
-                GenFds.OnlyGenerateThisCap = Options.uiCapName
-            else:
-                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,
-                                "No such a Capsule in FDF file: %s" % Options.uiCapName)
-
-        """Modify images from build output if the feature of loading driver at fixed address is on."""
-        if GenFdsGlobalVariable.FixedLoadAddress:
-            GenFds.PreprocessImage(BuildWorkSpace, GenFdsGlobalVariable.ActivePlatform)
-        """Call GenFds"""
-        GenFds.GenFd('', FdfParserObj, BuildWorkSpace, ArchList)
-
-        """Generate GUID cross reference file"""
-        GenFds.GenerateGuidXRefFile(BuildWorkSpace, ArchList)
-
-        """Display FV space info."""
-        GenFds.DisplayFvSpaceInfo(FdfParserObj)
-
-    except FdfParser.Warning, X:
-        EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError = False)
-        ReturnCode = FORMAT_INVALID
-    except FatalError, X:
-        if Options.debug != None:
-            import traceback
-            EdkLogger.quiet(traceback.format_exc())
-        ReturnCode = X.args[0]
-    except:
-        import traceback
-        EdkLogger.error(
-                    "\nPython",
-                    CODE_ERROR,
-                    "Tools code failure",
-                    ExtraData="Please send email to edk2-buildtools-devel@lists.sourceforge.net for help, attaching following call stack trace!\n",
-                    RaiseError=False
-                    )
-        EdkLogger.quiet(traceback.format_exc())
-        ReturnCode = CODE_ERROR
-    return ReturnCode
-
-gParamCheck = []
-def SingleCheckCallback(option, opt_str, value, parser):
-    if option not in gParamCheck:
-        setattr(parser.values, option.dest, value)
-        gParamCheck.append(option)
-    else:
-        parser.error("Option %s only allows one instance in command line!" % option)
-        
-## Parse command line options
-#
-# Using standard Python module optparse to parse command line option of this tool.
-#
-#   @retval Opt   A optparse.Values object containing the parsed options
-#   @retval Args  Target of build command
-#
-def myOptionParser():
-    usage = "%prog [options] -f input_file -a arch_list -b build_target -p active_platform -t tool_chain_tag -D \"MacroName [= MacroValue]\""
-    Parser = OptionParser(usage=usage,description=__copyright__,version="%prog " + str(versionNumber))
-    Parser.add_option("-f", "--file", dest="filename", type="string", help="Name of FDF file to convert", action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-a", "--arch", dest="archList", help="comma separated list containing one or more of: IA32, X64, IPF, ARM or EBC which should be built, overrides target.txt?s TARGET_ARCH")
-    Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")
-    Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed.")
-    Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")
-    Parser.add_option("-p", "--platform", type="string", dest="activePlatform", help="Set the ACTIVE_PLATFORM, overrides target.txt ACTIVE_PLATFORM setting.",
-                      action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-w", "--workspace", type="string", dest="Workspace", default=os.environ.get('WORKSPACE'), help="Set the WORKSPACE",
-                      action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-o", "--outputDir", type="string", dest="outputDir", help="Name of Build Output directory",
-                      action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-r", "--rom_image", dest="uiFdName", help="Build the image using the [FD] section named by FdUiName.")
-    Parser.add_option("-i", "--FvImage", dest="uiFvName", help="Build the FV image using the [FV] section named by UiFvName")
-    Parser.add_option("-C", "--CapsuleImage", dest="uiCapName", help="Build the Capsule image using the [Capsule] section named by UiCapName")
-    Parser.add_option("-b", "--buildtarget", type="choice", choices=['DEBUG','RELEASE'], dest="BuildTarget", help="Build TARGET is one of list: DEBUG, RELEASE.",
-                      action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-t", "--tagname", type="string", dest="ToolChain", help="Using the tools: TOOL_CHAIN_TAG name to build the platform.",
-                      action="callback", callback=SingleCheckCallback)
-    Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")
-    Parser.add_option("-s", "--specifyaddress", dest="FixedAddress", action="store_true", type=None, help="Specify driver load address.")
-    (Options, args) = Parser.parse_args()
-    return Options
-
-## The class implementing the EDK2 flash image generation process
-#
-#   This process includes:
-#       1. Collect workspace information, includes platform and module information
-#       2. Call methods of Fd class to generate FD
-#       3. Call methods of Fv class to generate FV that not belong to FD
-#
-class GenFds :
-    FdfParsef = None
-    # FvName, FdName, CapName in FDF, Image file name
-    ImageBinDict = {}
-    OnlyGenerateThisFd = None
-    OnlyGenerateThisFv = None
-    OnlyGenerateThisCap = None
-
-    ## GenFd()
-    #
-    #   @param  OutputDir           Output directory
-    #   @param  FdfParser           FDF contents parser
-    #   @param  Workspace           The directory of workspace
-    #   @param  ArchList            The Arch list of platform
-    #
-    def GenFd (OutputDir, FdfParser, WorkSpace, ArchList):
-        GenFdsGlobalVariable.SetDir ('', FdfParser, WorkSpace, ArchList)
-
-        GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")
-        if GenFds.OnlyGenerateThisCap != None and GenFds.OnlyGenerateThisCap.upper() in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
-            CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.get(GenFds.OnlyGenerateThisCap.upper())
-            if CapsuleObj != None:
-                CapsuleObj.GenCapsule()
-                return
-
-        if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
-            FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict.get(GenFds.OnlyGenerateThisFd.upper())
-            if FdObj != None:
-                FdObj.GenFd()
-                return
-        elif GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisFv == None:
-            for FdName in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
-                FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[FdName]
-                FdObj.GenFd()
-
-        GenFdsGlobalVariable.VerboseLogger("\n Generate other FV images! ")
-        if GenFds.OnlyGenerateThisFv != None and GenFds.OnlyGenerateThisFv.upper() in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
-            FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict.get(GenFds.OnlyGenerateThisFv.upper())
-            if FvObj != None:
-                Buffer = StringIO.StringIO()
-                FvObj.AddToBuffer(Buffer)
-                Buffer.close()
-                return
-        elif GenFds.OnlyGenerateThisFv == None:
-            for FvName in GenFdsGlobalVariable.FdfParser.Profile.FvDict.keys():
-                Buffer = StringIO.StringIO('')
-                FvObj = GenFdsGlobalVariable.FdfParser.Profile.FvDict[FvName]
-                FvObj.AddToBuffer(Buffer)
-                Buffer.close()
-        
-        if GenFds.OnlyGenerateThisFv == None and GenFds.OnlyGenerateThisFd == None and GenFds.OnlyGenerateThisCap == None:
-            if GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict != {}:
-                GenFdsGlobalVariable.VerboseLogger("\n Generate other Capsule images!")
-                for CapsuleName in GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict.keys():
-                    CapsuleObj = GenFdsGlobalVariable.FdfParser.Profile.CapsuleDict[CapsuleName]
-                    CapsuleObj.GenCapsule()
-
-            if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:
-                GenFdsGlobalVariable.VerboseLogger("\n Generate all Option ROM!")
-                for DriverName in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.keys():
-                    OptRomObj = GenFdsGlobalVariable.FdfParser.Profile.OptRomDict[DriverName]
-                    OptRomObj.AddToBuffer(None)
-
-    ## GetFvBlockSize()
-    #
-    #   @param  FvObj           Whose block size to get
-    #   @retval int             Block size value
-    #
-    def GetFvBlockSize(FvObj):
-        DefaultBlockSize = 0x1
-        FdObj = None
-        if GenFds.OnlyGenerateThisFd != None and GenFds.OnlyGenerateThisFd.upper() in GenFdsGlobalVariable.FdfParser.Profile.FdDict.keys():
-            FdObj = GenFdsGlobalVariable.FdfParser.Profile.FdDict[GenFds.OnlyGenerateThisFd.upper()]
-        if FdObj == None:
-            for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():
-                for ElementRegion in ElementFd.RegionList:
-                    if ElementRegion.RegionType == 'FV':
-                        for ElementRegionData in ElementRegion.RegionDataList:
-                            if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:
-                                if FvObj.BlockSizeList != []:
-                                    return FvObj.BlockSizeList[0][0]
-                                else:
-                                    return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)
-            if FvObj.BlockSizeList != []:
-                return FvObj.BlockSizeList[0][0]
-            return DefaultBlockSize
-        else:
-            for ElementRegion in FdObj.RegionList:
-                    if ElementRegion.RegionType == 'FV':
-                        for ElementRegionData in ElementRegion.RegionDataList:
-                            if ElementRegionData != None and ElementRegionData.upper() == FvObj.UiFvName:
-                                if FvObj.BlockSizeList != []:
-                                    return FvObj.BlockSizeList[0][0]
-                                else:
-                                    return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)
-            return DefaultBlockSize
-
-    ## DisplayFvSpaceInfo()
-    #
-    #   @param  FvObj           Whose block size to get
-    #   @retval None
-    #
-    def DisplayFvSpaceInfo(FdfParser):
-        
-        FvSpaceInfoList = []
-        MaxFvNameLength = 0
-        for FvName in FdfParser.Profile.FvDict:
-            if len(FvName) > MaxFvNameLength:
-                MaxFvNameLength = len(FvName)
-            FvSpaceInfoFileName = os.path.join(GenFdsGlobalVariable.FvDir, FvName.upper() + '.Fv.map')
-            if os.path.exists(FvSpaceInfoFileName):
-                FileLinesList = linecache.getlines(FvSpaceInfoFileName)
-                TotalFound = False
-                Total = ''
-                UsedFound = False
-                Used = ''
-                FreeFound = False
-                Free = ''
-                for Line in FileLinesList:
-                    NameValue = Line.split('=')
-                    if len(NameValue) == 2:
-                        if NameValue[0].strip() == 'EFI_FV_TOTAL_SIZE':
-                            TotalFound = True
-                            Total = NameValue[1].strip()
-                        if NameValue[0].strip() == 'EFI_FV_TAKEN_SIZE':
-                            UsedFound = True
-                            Used = NameValue[1].strip()
-                        if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':
-                            FreeFound = True
-                            Free = NameValue[1].strip()
-                
-                if TotalFound and UsedFound and FreeFound:
-                    FvSpaceInfoList.append((FvName, Total, Used, Free))
-                
-        GenFdsGlobalVariable.InfLogger('\nFV Space Information')
-        for FvSpaceInfo in FvSpaceInfoList:
-            Name = FvSpaceInfo[0]
-            TotalSizeValue = long(FvSpaceInfo[1], 0)
-            UsedSizeValue = long(FvSpaceInfo[2], 0)
-            FreeSizeValue = long(FvSpaceInfo[3], 0)
-            if UsedSizeValue == TotalSizeValue:
-                Percentage = '100'
-            else:
-                Percentage = str((UsedSizeValue+0.0)/TotalSizeValue)[0:4].lstrip('0.') 
-            
-            GenFdsGlobalVariable.InfLogger(Name + ' ' + '[' + Percentage + '%Full] ' + str(TotalSizeValue) + ' total, ' + str(UsedSizeValue) + ' used, ' + str(FreeSizeValue) + ' free')
-
-    ## PreprocessImage()
-    #
-    #   @param  BuildDb         Database from build meta data files
-    #   @param  DscFile         modules from dsc file will be preprocessed
-    #   @retval None
-    #
-    def PreprocessImage(BuildDb, DscFile):
-        PcdDict = BuildDb.BuildObject[DscFile, 'COMMON'].Pcds
-        PcdValue = ''
-        for Key in PcdDict:
-            PcdObj = PcdDict[Key]
-            if PcdObj.TokenCName == 'PcdBsBaseAddress':
-                PcdValue = PcdObj.DefaultValue
-                break
-        
-        if PcdValue == '':
-            return
-        
-        Int64PcdValue = long(PcdValue, 0)
-        if Int64PcdValue == 0 or Int64PcdValue < -1:    
-            return
-                
-        TopAddress = 0
-        if Int64PcdValue > 0:
-            TopAddress = Int64PcdValue
-            
-        ModuleDict = BuildDb.BuildObject[DscFile, 'COMMON'].Modules
-        for Key in ModuleDict:
-            ModuleObj = BuildDb.BuildObject[Key, 'COMMON']
-            print ModuleObj.BaseName + ' ' + ModuleObj.ModuleType
-
-    def GenerateGuidXRefFile(BuildDb, ArchList):
-        GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")
-        GuidXRefFile = StringIO.StringIO('')
-        for Arch in ArchList:
-            PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch]
-            for ModuleFile in PlatformDataBase.Modules:
-                Module = BuildDb.BuildObject[ModuleFile, Arch]
-                GuidXRefFile.write("%s %s\n" % (Module.Guid, Module.BaseName))
-        SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), False)\r
-        GuidXRefFile.close()
-        GenFdsGlobalVariable.InfLogger("\nGUID cross reference file can be found at %s" % GuidXRefFileName)
-        
-    ##Define GenFd as static function
-    GenFd = staticmethod(GenFd)
-    GetFvBlockSize = staticmethod(GetFvBlockSize)
-    DisplayFvSpaceInfo = staticmethod(DisplayFvSpaceInfo)
-    PreprocessImage = staticmethod(PreprocessImage)
-    GenerateGuidXRefFile = staticmethod(GenerateGuidXRefFile)
-
-if __name__ == '__main__':
-    r = main()
-    ## 0-127 is a safe return range, and 1 is a standard default error
-    if r < 0 or r > 127: r = 1
-    sys.exit(r)
-
+## @file\r
+# generate flash image\r
+#\r
+#  Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<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
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+from optparse import OptionParser\r
+import sys\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
+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.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.BuildVersion import gBUILD_VERSION\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
+import FfsFileStatement\r
+import glob\r
+from struct import unpack\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
+\r
+## Tool entrance method\r
+#\r
+# This method mainly dispatch specific methods per the command line options.\r
+# If no error found, return zero value so the caller of this tool can know\r
+# if it's executed successfully or not.\r
+#\r
+#   @retval 0     Tool was successful\r
+#   @retval 1     Tool failed\r
+#\r
+def main():\r
+    global Options\r
+    Options = myOptionParser()\r
+\r
+    global Workspace\r
+    Workspace = ""\r
+    ArchList = None\r
+    ReturnCode = 0\r
+\r
+    EdkLogger.Initialize()\r
+    try:\r
+        if Options.verbose is not None:\r
+            EdkLogger.SetLevel(EdkLogger.VERBOSE)\r
+            GenFdsGlobalVariable.VerboseMode = True\r
+            \r
+        if Options.FixedAddress is not None:\r
+            GenFdsGlobalVariable.FixedLoadAddress = True\r
+            \r
+        if Options.quiet is not None:\r
+            EdkLogger.SetLevel(EdkLogger.QUIET)\r
+        if Options.debug is not None:\r
+            EdkLogger.SetLevel(Options.debug + 1)\r
+            GenFdsGlobalVariable.DebugLevel = Options.debug\r
+        else:\r
+            EdkLogger.SetLevel(EdkLogger.INFO)\r
+\r
+        if (Options.Workspace is None):\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
+            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
+            GenFdsGlobalVariable.WorkSpaceDir = Workspace\r
+            if 'EDK_SOURCE' in os.environ:\r
+                GenFdsGlobalVariable.EdkSourceDir = os.path.normcase(os.environ['EDK_SOURCE'])\r
+            if (Options.debug):\r
+                GenFdsGlobalVariable.VerboseLogger("Using Workspace:" + Workspace)\r
+            if Options.GenfdsMultiThread:\r
+                GenFdsGlobalVariable.EnableGenfdsMultiThread = True\r
+        os.chdir(GenFdsGlobalVariable.WorkSpaceDir)\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
+            FdfFilename = GenFdsGlobalVariable.ReplaceWorkspaceMacro(FdfFilename)\r
+\r
+            if FdfFilename[0:2] == '..':\r
+                FdfFilename = os.path.realpath(FdfFilename)\r
+            if not os.path.isabs(FdfFilename):\r
+                FdfFilename = mws.join(GenFdsGlobalVariable.WorkSpaceDir, FdfFilename)\r
+            if not os.path.exists(FdfFilename):\r
+                EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=FdfFilename)\r
+\r
+            GenFdsGlobalVariable.FdfFile = FdfFilename\r
+            GenFdsGlobalVariable.FdfFileTimeStamp = os.path.getmtime(FdfFilename)\r
+        else:\r
+            EdkLogger.error("GenFds", OPTION_MISSING, "Missing FDF filename")\r
+\r
+        if (Options.BuildTarget):\r
+            GenFdsGlobalVariable.TargetName = Options.BuildTarget\r
+\r
+        if (Options.ToolChain):\r
+            GenFdsGlobalVariable.ToolChainTag = Options.ToolChain\r
+\r
+        if (Options.activePlatform):\r
+            ActivePlatform = Options.activePlatform\r
+            ActivePlatform = GenFdsGlobalVariable.ReplaceWorkspaceMacro(ActivePlatform)\r
+\r
+            if ActivePlatform[0:2] == '..':\r
+                ActivePlatform = os.path.realpath(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
+                EdkLogger.error("GenFds", FILE_NOT_FOUND, "ActivePlatform doesn't exist!")\r
+        else:\r
+            EdkLogger.error("GenFds", OPTION_MISSING, "Missing active platform")\r
+\r
+        GlobalData.BuildOptionPcd     = Options.OptionPcd if Options.OptionPcd else {}\r
+        GenFdsGlobalVariable.ActivePlatform = PathClass(NormPath(ActivePlatform))\r
+\r
+        if (Options.ConfDirectory):\r
+            # Get alternate Conf location, if it is absolute, then just use the absolute directory name\r
+            ConfDirectoryPath = os.path.normpath(Options.ConfDirectory)\r
+            if ConfDirectoryPath.startswith('"'):\r
+                ConfDirectoryPath = ConfDirectoryPath[1:]\r
+            if ConfDirectoryPath.endswith('"'):\r
+                ConfDirectoryPath = ConfDirectoryPath[:-1]\r
+            if not os.path.isabs(ConfDirectoryPath):\r
+                # Since alternate directory name is not absolute, the alternate directory is located within the WORKSPACE\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:\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
+            TargetTxt = TargetTxtClassObject.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
+                BuildTargetList = TargetTxt.TargetTxtDictionary[DataType.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
+                ToolChainList = TargetTxt.TargetTxtDictionary[DataType.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
+                GenFdsGlobalVariable.ToolChainTag = ToolChainList[0]\r
+        else:\r
+            EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)\r
+\r
+        #Set global flag for build mode\r
+        GlobalData.gIgnoreSource = Options.IgnoreSources\r
+\r
+        if Options.Macros:\r
+            for Pair in Options.Macros:\r
+                if Pair.startswith('"'):\r
+                    Pair = Pair[1:]\r
+                if Pair.endswith('"'):\r
+                    Pair = Pair[:-1]\r
+                List = Pair.split('=')\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
+                        GlobalData.gGlobalDefines[List[0].strip()] = List[1].strip()\r
+                    else:\r
+                        GlobalData.gCommandLineDefines[List[0].strip()] = List[1].strip()\r
+                else:\r
+                    GlobalData.gCommandLineDefines[List[0].strip()] = "TRUE"\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:\r
+            GlobalData.gGlobalDefines["TARGET"] = GenFdsGlobalVariable.TargetName\r
+        if "TOOLCHAIN" not in GlobalData.gGlobalDefines:\r
+            GlobalData.gGlobalDefines["TOOLCHAIN"] = GenFdsGlobalVariable.ToolChainTag\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
+        BuildWorkSpace = WorkspaceDatabase(GlobalData.gDatabasePath)\r
+        BuildWorkSpace.InitDatabase()\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
+        else:\r
+#            EdkLogger.error("GenFds", OPTION_MISSING, "Missing build ARCH")\r
+            ArchList = BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, Options.BuildTarget, Options.ToolChain].SupArchList\r
+\r
+        TargetArchList = set(BuildWorkSpace.BuildObject[GenFdsGlobalVariable.ActivePlatform, TAB_COMMON, Options.BuildTarget, Options.ToolChain].SupArchList) & set(ArchList)\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, TAB_COMMON].SupArchList)))\r
+        \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
+\r
+        if (Options.outputDir):\r
+            OutputDirFromCommandLine = GenFdsGlobalVariable.ReplaceWorkspaceMacro(Options.outputDir)\r
+            if not os.path.isabs (OutputDirFromCommandLine):\r
+                OutputDirFromCommandLine = os.path.join(GenFdsGlobalVariable.WorkSpaceDir, OutputDirFromCommandLine)\r
+            for Arch in ArchList:\r
+                GenFdsGlobalVariable.OutputDirDict[Arch] = OutputDirFromCommandLine\r
+        else:\r
+            for Arch in ArchList:\r
+                GenFdsGlobalVariable.OutputDirDict[Arch] = os.path.join(GenFdsGlobalVariable.OutputDirFromDscDict[Arch], GenFdsGlobalVariable.TargetName + '_' + GenFdsGlobalVariable.ToolChainTag)\r
+\r
+        for Key in GenFdsGlobalVariable.OutputDirDict:\r
+            OutputDir = GenFdsGlobalVariable.OutputDirDict[Key]\r
+            if OutputDir[0:2] == '..':\r
+                OutputDir = os.path.realpath(OutputDir)\r
+\r
+            if OutputDir[1] != ':':\r
+                OutputDir = os.path.join (GenFdsGlobalVariable.WorkSpaceDir, OutputDir)\r
+\r
+            if not os.path.exists(OutputDir):\r
+                EdkLogger.error("GenFds", FILE_NOT_FOUND, ExtraData=OutputDir)\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
+\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:\r
+                GenFds.OnlyGenerateThisFd = Options.uiFdName\r
+            else:\r
+                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
+                                "No such an FD in FDF file: %s" % Options.uiFdName)\r
+\r
+        if (Options.uiFvName) :\r
+            if Options.uiFvName.upper() in FdfParserObj.Profile.FvDict:\r
+                GenFds.OnlyGenerateThisFv = Options.uiFvName\r
+            else:\r
+                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
+                                "No such an FV in FDF file: %s" % Options.uiFvName)\r
+\r
+        if (Options.uiCapName) :\r
+            if Options.uiCapName.upper() in FdfParserObj.Profile.CapsuleDict:\r
+                GenFds.OnlyGenerateThisCap = Options.uiCapName\r
+            else:\r
+                EdkLogger.error("GenFds", OPTION_VALUE_INVALID,\r
+                                "No such a Capsule in FDF file: %s" % Options.uiCapName)\r
+\r
+        GenFdsGlobalVariable.WorkSpace = BuildWorkSpace\r
+        if ArchList is not None:\r
+            GenFdsGlobalVariable.ArchList = ArchList\r
+\r
+        # Dsc Build Data will handle Pcd Settings from CommandLine.\r
+\r
+        """Modify images from build output if the feature of loading driver at fixed address is on."""\r
+        if GenFdsGlobalVariable.FixedLoadAddress:\r
+            GenFds.PreprocessImage(BuildWorkSpace, GenFdsGlobalVariable.ActivePlatform)\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 RegionObj in FdObj.RegionList:\r
+                        if RegionObj.RegionType != 'FV':\r
+                            continue\r
+                        for RegionData in RegionObj.RegionDataList:\r
+                            if FvObj.UiFvName.upper() == RegionData.upper():\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
+                                else:\r
+                                    FvObj.FvRegionInFD = RegionObj.Size\r
+                                    RegionObj.BlockInfoOfRegion(FdObj.BlockSizeList, FvObj)\r
+\r
+        """Call GenFds"""\r
+        GenFds.GenFd('', FdfParserObj, BuildWorkSpace, ArchList)\r
+\r
+        """Generate GUID cross reference file"""\r
+        GenFds.GenerateGuidXRefFile(BuildWorkSpace, ArchList, FdfParserObj)\r
+\r
+        """Display FV space info."""\r
+        GenFds.DisplayFvSpaceInfo(FdfParserObj)\r
+\r
+    except FdfParser.Warning, X:\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 is not None:\r
+            import traceback\r
+            EdkLogger.quiet(traceback.format_exc())\r
+        ReturnCode = X.args[0]\r
+    except:\r
+        import traceback\r
+        EdkLogger.error(\r
+                    "\nPython",\r
+                    CODE_ERROR,\r
+                    "Tools code failure",\r
+                    ExtraData="Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!\n",\r
+                    RaiseError=False\r
+                    )\r
+        EdkLogger.quiet(traceback.format_exc())\r
+        ReturnCode = CODE_ERROR\r
+    finally:\r
+        ClearDuplicatedInf()\r
+    return ReturnCode\r
+\r
+gParamCheck = []\r
+def SingleCheckCallback(option, opt_str, value, parser):\r
+    if option not in gParamCheck:\r
+        setattr(parser.values, option.dest, value)\r
+        gParamCheck.append(option)\r
+    else:\r
+        parser.error("Option %s only allows one instance in command line!" % option)\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 is 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:\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.lower() == ToolDef[1].lower() :\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 is 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:\r
+            ToolPathTmp = BuildOption[ToolPathKey]\r
+        if ToolOptionKey in BuildOption:\r
+            ToolOption = BuildOption[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
+#   @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
+    Parser = OptionParser(usage=usage, description=__copyright__, version="%prog " + str(versionNumber))\r
+    Parser.add_option("-f", "--file", dest="filename", type="string", help="Name of FDF file to convert", action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-a", "--arch", dest="archList", help="comma separated list containing one or more of: IA32, X64, IPF, ARM, AARCH64 or EBC which should be built, overrides target.txt?s TARGET_ARCH")\r
+    Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")\r
+    Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed.")\r
+    Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")\r
+    Parser.add_option("-p", "--platform", type="string", dest="activePlatform", help="Set the ACTIVE_PLATFORM, overrides target.txt ACTIVE_PLATFORM setting.",\r
+                      action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-w", "--workspace", type="string", dest="Workspace", default=os.environ.get('WORKSPACE'), help="Set the WORKSPACE",\r
+                      action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-o", "--outputDir", type="string", dest="outputDir", help="Name of Build Output directory",\r
+                      action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-r", "--rom_image", dest="uiFdName", help="Build the image using the [FD] section named by FdUiName.")\r
+    Parser.add_option("-i", "--FvImage", dest="uiFvName", help="Build the FV image using the [FV] section named by UiFvName")\r
+    Parser.add_option("-C", "--CapsuleImage", dest="uiCapName", help="Build the Capsule image using the [Capsule] section named by UiCapName")\r
+    Parser.add_option("-b", "--buildtarget", type="string", dest="BuildTarget", help="Set the build TARGET, overrides target.txt TARGET setting.",\r
+                      action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-t", "--tagname", type="string", dest="ToolChain", help="Using the tools: TOOL_CHAIN_TAG name to build the platform.",\r
+                      action="callback", callback=SingleCheckCallback)\r
+    Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")\r
+    Parser.add_option("-s", "--specifyaddress", dest="FixedAddress", action="store_true", type=None, help="Specify driver load address.")\r
+    Parser.add_option("--conf", action="store", type="string", dest="ConfDirectory", help="Specify the customized Conf directory.")\r
+    Parser.add_option("--ignore-sources", action="store_true", dest="IgnoreSources", default=False, help="Focus to a binary build and ignore all source files")\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
+    return Options\r
+\r
+## The class implementing the EDK2 flash image generation process\r
+#\r
+#   This process includes:\r
+#       1. Collect workspace information, includes platform and module information\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
+    FdfParsef = None\r
+    # FvName, FdName, CapName in FDF, Image file name\r
+    ImageBinDict = {}\r
+    OnlyGenerateThisFd = None\r
+    OnlyGenerateThisFv = None\r
+    OnlyGenerateThisCap = None\r
+\r
+    ## GenFd()\r
+    #\r
+    #   @param  OutputDir           Output directory\r
+    #   @param  FdfParser           FDF contents parser\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
+\r
+        GenFdsGlobalVariable.VerboseLogger(" Generate all Fd images and their required FV and Capsule images!")\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
+        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
+        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
+        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 = StringIO.StringIO()\r
+                FvObj.AddToBuffer(Buffer)\r
+                Buffer.close()\r
+                return\r
+        elif GenFds.OnlyGenerateThisFv is None:\r
+            for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
+                Buffer = StringIO.StringIO('')\r
+                FvObj.AddToBuffer(Buffer)\r
+                Buffer.close()\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
+                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
+                for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
+                    OptRomObj.AddToBuffer(None)\r
+    @staticmethod\r
+    def GenFfsMakefile(OutputDir, FdfParser, WorkSpace, ArchList, GlobalData):\r
+        GenFdsGlobalVariable.SetEnv(FdfParser, WorkSpace, ArchList, GlobalData)\r
+        for FdObj in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
+            FdObj.GenFd(Flag=True)\r
+\r
+        for FvObj in GenFdsGlobalVariable.FdfParser.Profile.FvDict.values():\r
+            FvObj.AddToBuffer(Buffer=None, Flag=True)\r
+\r
+        if GenFdsGlobalVariable.FdfParser.Profile.OptRomDict != {}:\r
+            for OptRomObj in GenFdsGlobalVariable.FdfParser.Profile.OptRomDict.values():\r
+                OptRomObj.AddToBuffer(Buffer=None, Flag=True)\r
+\r
+        return GenFdsGlobalVariable.FfsCmdDict\r
+\r
+    ## GetFvBlockSize()\r
+    #\r
+    #   @param  FvObj           Whose block size to get\r
+    #   @retval int             Block size value\r
+    #\r
+    def GetFvBlockSize(FvObj):\r
+        DefaultBlockSize = 0x1\r
+        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 None:\r
+            for ElementFd in GenFdsGlobalVariable.FdfParser.Profile.FdDict.values():\r
+                for ElementRegion in ElementFd.RegionList:\r
+                    if ElementRegion.RegionType == 'FV':\r
+                        for ElementRegionData in ElementRegion.RegionDataList:\r
+                            if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
+                                if FvObj.BlockSizeList != []:\r
+                                    return FvObj.BlockSizeList[0][0]\r
+                                else:\r
+                                    return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
+            if FvObj.BlockSizeList != []:\r
+                return FvObj.BlockSizeList[0][0]\r
+            return DefaultBlockSize\r
+        else:\r
+            for ElementRegion in FdObj.RegionList:\r
+                    if ElementRegion.RegionType == 'FV':\r
+                        for ElementRegionData in ElementRegion.RegionDataList:\r
+                            if ElementRegionData is not None and ElementRegionData.upper() == FvObj.UiFvName:\r
+                                if FvObj.BlockSizeList != []:\r
+                                    return FvObj.BlockSizeList[0][0]\r
+                                else:\r
+                                    return ElementRegion.BlockSizeOfRegion(ElementFd.BlockSizeList)\r
+            return DefaultBlockSize\r
+\r
+    ## DisplayFvSpaceInfo()\r
+    #\r
+    #   @param  FvObj           Whose block size to get\r
+    #   @retval None\r
+    #\r
+    def DisplayFvSpaceInfo(FdfParser):\r
+        \r
+        FvSpaceInfoList = []\r
+        MaxFvNameLength = 0\r
+        for FvName in FdfParser.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
+                FileLinesList = linecache.getlines(FvSpaceInfoFileName)\r
+                TotalFound = False\r
+                Total = ''\r
+                UsedFound = False\r
+                Used = ''\r
+                FreeFound = False\r
+                Free = ''\r
+                for Line in FileLinesList:\r
+                    NameValue = Line.split('=')\r
+                    if len(NameValue) == 2:\r
+                        if NameValue[0].strip() == 'EFI_FV_TOTAL_SIZE':\r
+                            TotalFound = True\r
+                            Total = NameValue[1].strip()\r
+                        if NameValue[0].strip() == 'EFI_FV_TAKEN_SIZE':\r
+                            UsedFound = True\r
+                            Used = NameValue[1].strip()\r
+                        if NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':\r
+                            FreeFound = True\r
+                            Free = NameValue[1].strip()\r
+                \r
+                if TotalFound and UsedFound and FreeFound:\r
+                    FvSpaceInfoList.append((FvName, Total, Used, Free))\r
+                \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
+            if UsedSizeValue == TotalSizeValue:\r
+                Percentage = '100'\r
+            else:\r
+                Percentage = str((UsedSizeValue + 0.0) / TotalSizeValue)[0:4].lstrip('0.')\r
+\r
+            GenFdsGlobalVariable.InfLogger(Name + ' ' + '[' + Percentage + '%Full] ' + str(TotalSizeValue) + ' total, ' + str(UsedSizeValue) + ' used, ' + str(FreeSizeValue) + ' free')\r
+\r
+    ## PreprocessImage()\r
+    #\r
+    #   @param  BuildDb         Database from build meta data files\r
+    #   @param  DscFile         modules from dsc file will be preprocessed\r
+    #   @retval None\r
+    #\r
+    def PreprocessImage(BuildDb, DscFile):\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
+        \r
+        if PcdValue == '':\r
+            return\r
+        \r
+        Int64PcdValue = long(PcdValue, 0)\r
+        if Int64PcdValue == 0 or Int64PcdValue < -1:    \r
+            return\r
+                \r
+        TopAddress = 0\r
+        if Int64PcdValue > 0:\r
+            TopAddress = Int64PcdValue\r
+            \r
+        ModuleDict = BuildDb.BuildObject[DscFile, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag].Modules\r
+        for Key in ModuleDict:\r
+            ModuleObj = BuildDb.BuildObject[Key, TAB_COMMON, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\r
+            print ModuleObj.BaseName + ' ' + ModuleObj.ModuleType\r
+\r
+    def GenerateGuidXRefFile(BuildDb, ArchList, FdfParserObj):\r
+        GuidXRefFileName = os.path.join(GenFdsGlobalVariable.FvDir, "Guid.xref")\r
+        GuidXRefFile = StringIO.StringIO('')\r
+        GuidDict = {}\r
+        ModuleList = []\r
+        FileGuidList = []\r
+        for Arch in ArchList:\r
+            PlatformDataBase = BuildDb.BuildObject[GenFdsGlobalVariable.ActivePlatform, Arch, GenFdsGlobalVariable.TargetName, GenFdsGlobalVariable.ToolChainTag]\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
+            for FvName in FdfParserObj.Profile.FvDict:\r
+                for FfsObj in FdfParserObj.Profile.FvDict[FvName].FfsList:\r
+                    if not isinstance(FfsObj, FfsFileStatement.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
+                        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
+                    else:\r
+                        FileStatementGuid = FfsObj.NameGuid\r
+                        if FileStatementGuid in FileGuidList:\r
+                            continue\r
+                        else:\r
+                            FileGuidList.append(FileStatementGuid)\r
+                        Name = []\r
+                        FfsPath = os.path.join(GenFdsGlobalVariable.FvDir, 'Ffs')\r
+                        FfsPath = glob.glob(os.path.join(FfsPath, FileStatementGuid) + '*')\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
+                        FileList = os.listdir(FfsPath[0])\r
+                        for File in FileList:\r
+                            Match = ReFileEnds.search(File)\r
+                            if Match:\r
+                                for Index in range(1, 8):\r
+                                    if Match.group(Index) and Match.group(Index) in MatchDict:\r
+                                        MatchDict[Match.group(Index)].append(File)\r
+                                    elif Match.group(Index):\r
+                                        MatchDict[Match.group(Index)] = [File]\r
+                        if not MatchDict:\r
+                            continue\r
+                        if '.ui' in MatchDict:\r
+                            for File in MatchDict['.ui']:\r
+                                with open(os.path.join(FfsPath[0], File), 'rb') as F:\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
+                        else:\r
+                            FileList = []\r
+                            if 'fv.sec.txt' in MatchDict:\r
+                                FileList = MatchDict['fv.sec.txt']\r
+                            elif '.pe32.txt' in MatchDict:\r
+                                FileList = MatchDict['.pe32.txt']\r
+                            elif '.te.txt' in MatchDict:\r
+                                FileList = MatchDict['.te.txt']\r
+                            elif '.pic.txt' in MatchDict:\r
+                                FileList = MatchDict['.pic.txt']\r
+                            elif '.raw.txt' in MatchDict:\r
+                                FileList = MatchDict['.raw.txt']\r
+                            elif '.ffs.txt' in MatchDict:\r
+                                FileList = MatchDict['.ffs.txt']\r
+                            else:\r
+                                pass\r
+                            for File in FileList:\r
+                                with open(os.path.join(FfsPath[0], File), 'r') as F:\r
+                                    Name.append((F.read().split()[-1]))\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
+\r
+       # Append GUIDs, Protocols, and PPIs to the Xref file\r
+        GuidXRefFile.write("\n")\r
+        for key, item in GuidDict.items():\r
+            GuidXRefFile.write("%s %s\n" % (GuidStructureStringToGuidString(item).upper(), key))\r
+\r
+        if GuidXRefFile.getvalue():\r
+            SaveFileOnChange(GuidXRefFileName, GuidXRefFile.getvalue(), 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
+        GuidXRefFile.close()\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
+    if r < 0 or r > 127: r = 1\r
+    sys.exit(r)\r
+\r