]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools: Autogen - modify to use standard parent/child class relationships
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / AutoGen.py
index 8ad385aac25381b0b6cd4edbf1ef4b29237dea16..aeeab9190473c7af9b85819d34c8f164e93644b8 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Generate AutoGen.h, AutoGen.c and *.depex files\r
 #\r
-# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\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
@@ -44,6 +44,7 @@ from Common.MultipleWorkspace import MultipleWorkspace as mws
 import InfSectionParser\r
 import datetime\r
 import hashlib\r
+from GenVar import VariableMgr,var_info\r
 \r
 ## Regular expression for splitting Dependency Expression string into tokens\r
 gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)")\r
@@ -158,8 +159,8 @@ ${tail_comments}
 #   This class just implements the cache mechanism of AutoGen objects.\r
 #\r
 class AutoGen(object):\r
-    # database to maintain the objects of xxxAutoGen\r
-    _CACHE_ = {}    # (BuildTarget, ToolChain) : {ARCH : {platform file: AutoGen object}}}\r
+    # database to maintain the objects in each child class\r
+    __ObjectCache = {}    # (BuildTarget, ToolChain, ARCH, platform file): AutoGen object\r
 \r
     ## Factory method\r
     #\r
@@ -173,24 +174,19 @@ class AutoGen(object):
     #   @param  *args           The specific class related parameters\r
     #   @param  **kwargs        The specific class related dict parameters\r
     #\r
-    def __new__(Class, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+    def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
         # check if the object has been created\r
-        Key = (Target, Toolchain)\r
-        if Key not in Class._CACHE_ or Arch not in Class._CACHE_[Key] \\r
-           or MetaFile not in Class._CACHE_[Key][Arch]:\r
-            AutoGenObject = super(AutoGen, Class).__new__(Class)\r
-            # call real constructor\r
-            if not AutoGenObject._Init(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
-                return None\r
-            if Key not in Class._CACHE_:\r
-                Class._CACHE_[Key] = {}\r
-            if Arch not in Class._CACHE_[Key]:\r
-                Class._CACHE_[Key][Arch] = {}\r
-            Class._CACHE_[Key][Arch][MetaFile] = AutoGenObject\r
-        else:\r
-            AutoGenObject = Class._CACHE_[Key][Arch][MetaFile]\r
+        Key = (Target, Toolchain, Arch, MetaFile)\r
+        try:\r
+            # if it exists, just return it directly\r
+            return cls.__ObjectCache[Key]\r
+        except:\r
+            # it didnt exist. create it, cache it, then return it\r
+            cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)\r
+            return cls.__ObjectCache[Key]\r
 \r
-        return AutoGenObject\r
+    def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
 \r
     ## hash() operator\r
     #\r
@@ -220,10 +216,16 @@ class AutoGen(object):
 # architecture. This class will generate top level makefile.\r
 #\r
 class WorkspaceAutoGen(AutoGen):\r
-    ## Real constructor of WorkspaceAutoGen\r
-    #\r
-    # This method behaves the same as __init__ except that it needs explicit invoke\r
-    # (in super class's __new__ method)\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(WorkspaceAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._Init = True\r
+    \r
+    ## Initialize WorkspaceAutoGen\r
     #\r
     #   @param  WorkspaceDir            Root directory of workspace\r
     #   @param  ActivePlatform          Meta-file of active platform\r
@@ -239,7 +241,7 @@ class WorkspaceAutoGen(AutoGen):
     #   @param  Caps                    Capsule list to be generated\r
     #   @param  SkuId                   SKU id from command line\r
     #\r
-    def _Init(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,\r
+    def _InitWorker(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,\r
               BuildConfig, ToolDefinition, FlashDefinitionFile='', Fds=None, Fvs=None, Caps=None, SkuId='', UniFlag=None,\r
               Progress=None, BuildModule=None):\r
         if Fds is None:\r
@@ -270,6 +272,7 @@ class WorkspaceAutoGen(AutoGen):
         self._FvDir         = None\r
         self._MakeFileDir   = None\r
         self._BuildCommand  = None\r
+        self._GuidDict = {}\r
 \r
         # there's many relative directory operations, so ...\r
         os.chdir(self.WorkspaceDir)\r
@@ -396,71 +399,10 @@ class WorkspaceAutoGen(AutoGen):
         for Arch in self.ArchList:\r
             Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]\r
 \r
-            DecPcds = {}\r
-            DecPcdsKey = set()\r
-            PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)\r
-            if GlobalData.BuildOptionPcd:\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('build', AUTOGEN_ERROR, "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 PGen.PackageList:\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
-                                                    'build',\r
-                                                     AUTOGEN_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
-                    if not FoundFlag:\r
-                        if HasTokenSpace:\r
-                            EdkLogger.error('build', AUTOGEN_ERROR, "The Pcd %s.%s is not found in the DEC file." % (TokenSpaceGuidCName, TokenCName))\r
-                        else:\r
-                            EdkLogger.error('build', AUTOGEN_ERROR, "The Pcd %s is not found in the DEC file." % (TokenCName))\r
 \r
-                    for BuildData in PGen.BuildDatabase._CACHE_.values():\r
-                        if BuildData.Arch != Arch:\r
-                            continue\r
-                        if BuildData.MetaFile.Ext == '.dec':\r
-                            continue\r
-                        for key in BuildData.Pcds:\r
-                            PcdItem = BuildData.Pcds[key]\r
-                            if (TokenSpaceGuidCName, TokenCName) == (PcdItem.TokenSpaceGuidCName, PcdItem.TokenCName):\r
-                                PcdItem.DefaultValue = NewValue\r
 \r
-                    if (TokenCName, TokenSpaceGuidCName) in PcdSet:\r
-                        PcdSet[(TokenCName, TokenSpaceGuidCName)] = NewValue\r
 \r
             SourcePcdDict = {'DynamicEx':[], 'PatchableInModule':[],'Dynamic':[],'FixedAtBuild':[]}\r
             BinaryPcdDict = {'DynamicEx':[], 'PatchableInModule':[]}\r
@@ -468,6 +410,7 @@ class WorkspaceAutoGen(AutoGen):
             BinaryPcdDict_Keys = BinaryPcdDict.keys()\r
 \r
             # generate the SourcePcdDict and BinaryPcdDict\r
+            PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)\r
             for BuildData in PGen.BuildDatabase._CACHE_.values():\r
                 if BuildData.Arch != Arch:\r
                     continue\r
@@ -610,6 +553,8 @@ class WorkspaceAutoGen(AutoGen):
                 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]\r
                 PkgSet.update(ModuleData.Packages)\r
             Pkgs = list(PkgSet) + list(PGen.PackageList)\r
+            DecPcds = {}\r
+            DecPcdsKey = set()\r
             for Pkg in Pkgs:\r
                 for Pcd in Pkg.Pcds:\r
                     DecPcds[Pcd[0], Pcd[1]] = Pkg.Pcds[Pcd]\r
@@ -1167,6 +1112,14 @@ class WorkspaceAutoGen(AutoGen):
 #  file in order to generate makefile for platform.\r
 #\r
 class PlatformAutoGen(AutoGen):\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(PlatformAutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch)\r
+            self._Init = True\r
     #\r
     # Used to store all PCDs for both PEI and DXE phase, in order to generate \r
     # correct PCD database\r
@@ -1195,11 +1148,8 @@ class PlatformAutoGen(AutoGen):
                 "0x10001"  : 2,      #  TARGET_*********_****_***********_ATTRIBUTE\r
                 "0x00001"  : 1}      #  ******_*********_****_***********_ATTRIBUTE (Lowest)\r
 \r
-    ## The real constructor of PlatformAutoGen\r
+    ## Initialize PlatformAutoGen\r
     #\r
-    #  This method is not supposed to be called by users of PlatformAutoGen. It's\r
-    #  only used by factory method __new__() to do real initialization work for an\r
-    #  object of PlatformAutoGen\r
     #\r
     #   @param      Workspace       WorkspaceAutoGen object\r
     #   @param      PlatformFile    Platform file (DSC file)\r
@@ -1207,7 +1157,7 @@ class PlatformAutoGen(AutoGen):
     #   @param      Toolchain       Name of tool chain\r
     #   @param      Arch            arch of the platform supports\r
     #\r
-    def _Init(self, Workspace, PlatformFile, Target, Toolchain, Arch):\r
+    def _InitWorker(self, Workspace, PlatformFile, Target, Toolchain, Arch):\r
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % (PlatformFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (PlatformFile, Arch, Toolchain, Target)\r
 \r
@@ -1224,6 +1174,8 @@ class PlatformAutoGen(AutoGen):
         self.AllPcdList = []\r
         # get the original module/package/platform objects\r
         self.BuildDatabase = Workspace.BuildDatabase\r
+        self.DscBuildDataObj = Workspace.Platform\r
+        self._GuidDict = Workspace._GuidDict\r
 \r
         # flag indicating if the makefile/C-code file has been created or not\r
         self.IsMakeFileCreated  = False\r
@@ -1260,6 +1212,9 @@ class PlatformAutoGen(AutoGen):
         self._BuildCommand = None\r
         self._AsBuildInfList = []\r
         self._AsBuildModuleList = []\r
+\r
+        self.VariableInfo = None\r
+\r
         if GlobalData.gFdfParser != None:\r
             self._AsBuildInfList = GlobalData.gFdfParser.Profile.InfList\r
             for Inf in self._AsBuildInfList:\r
@@ -1271,6 +1226,7 @@ class PlatformAutoGen(AutoGen):
         # get library/modules for build\r
         self.LibraryBuildDirectoryList = []\r
         self.ModuleBuildDirectoryList = []\r
+\r
         return True\r
 \r
     def __repr__(self):\r
@@ -1352,23 +1308,72 @@ class PlatformAutoGen(AutoGen):
                 if key in ShareFixedAtBuildPcdsSameValue and ShareFixedAtBuildPcdsSameValue[key]:                    \r
                     LibAuto.ConstPcd[key] = Pcd.DefaultValue\r
 \r
+    def CollectVariables(self, DynamicPcdSet):\r
+\r
+        VpdRegionSize = 0\r
+        VpdRegionBase = 0\r
+        if self.Workspace.FdfFile:\r
+            FdDict = self.Workspace.FdfProfile.FdDict[GlobalData.gFdfParser.CurrentFdName]\r
+            for FdRegion in FdDict.RegionList:\r
+                for item in FdRegion.RegionDataList:\r
+                    if self.Platform.VpdToolGuid.strip() and self.Platform.VpdToolGuid in item:\r
+                        VpdRegionSize = FdRegion.Size\r
+                        VpdRegionBase = FdRegion.Offset\r
+                        break\r
+\r
+\r
+        VariableInfo = VariableMgr(self.DscBuildDataObj._GetDefaultStores(),self.DscBuildDataObj._GetSkuIds())\r
+        VariableInfo.SetVpdRegionMaxSize(VpdRegionSize)\r
+        VariableInfo.SetVpdRegionOffset(VpdRegionBase)\r
+        Index = 0\r
+        for Pcd in DynamicPcdSet:\r
+            pcdname = ".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName))\r
+            for SkuName in Pcd.SkuInfoList:\r
+                Sku = Pcd.SkuInfoList[SkuName]\r
+                SkuId = Sku.SkuId\r
+                if SkuId == None or SkuId == '':\r
+                    continue\r
+                if len(Sku.VariableName) > 0:\r
+                    VariableGuidStructure = Sku.VariableGuidValue\r
+                    VariableGuid = GuidStructureStringToGuidString(VariableGuidStructure)\r
+                    for StorageName in Sku.DefaultStoreDict:\r
+                        VariableInfo.append_variable(var_info(Index,pcdname,StorageName,SkuName, StringToArray(Sku.VariableName),VariableGuid, Sku.VariableOffset, Sku.VariableAttribute , Sku.HiiDefaultValue,Sku.DefaultStoreDict[StorageName],Pcd.DatumType))\r
+            Index += 1\r
+        return VariableInfo\r
+\r
+    def UpdateNVStoreMaxSize(self,OrgVpdFile):\r
+        if self.VariableInfo:\r
+            VpdMapFilePath = os.path.join(self.BuildDir, "FV", "%s.map" % self.Platform.VpdToolGuid)\r
+            PcdNvStoreDfBuffer = [item for item in self._DynamicPcdList if item.TokenCName == "PcdNvStoreDefaultValueBuffer" and item.TokenSpaceGuidCName == "gEfiMdeModulePkgTokenSpaceGuid"]\r
+\r
+            if PcdNvStoreDfBuffer:\r
+                if os.path.exists(VpdMapFilePath):\r
+                    OrgVpdFile.Read(VpdMapFilePath)\r
+                    PcdItems = OrgVpdFile.GetOffset(PcdNvStoreDfBuffer[0])\r
+                    NvStoreOffset = PcdItems.values()[0].strip() if PcdItems else '0'\r
+                else:\r
+                    EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)\r
+\r
+                NvStoreOffset = int(NvStoreOffset,16) if NvStoreOffset.upper().startswith("0X") else int(NvStoreOffset)\r
+                default_skuobj = PcdNvStoreDfBuffer[0].SkuInfoList.get("DEFAULT")\r
+                maxsize = self.VariableInfo.VpdRegionSize  - NvStoreOffset if self.VariableInfo.VpdRegionSize else len(default_skuobj.DefaultValue.split(","))\r
+                var_data = self.VariableInfo.PatchNVStoreDefaultMaxSize(maxsize)\r
+\r
+                if var_data and default_skuobj:\r
+                    default_skuobj.DefaultValue = var_data\r
+                    PcdNvStoreDfBuffer[0].DefaultValue = var_data\r
+                    PcdNvStoreDfBuffer[0].SkuInfoList.clear()\r
+                    PcdNvStoreDfBuffer[0].SkuInfoList['DEFAULT'] = default_skuobj\r
+                    PcdNvStoreDfBuffer[0].MaxDatumSize = str(len(default_skuobj.DefaultValue.split(",")))\r
+\r
+        return OrgVpdFile\r
+\r
     ## Collect dynamic PCDs\r
     #\r
     #  Gather dynamic PCDs list from each module and their settings from platform\r
     #  This interface should be invoked explicitly when platform action is created.\r
     #\r
     def CollectPlatformDynamicPcds(self):\r
-        # Override the platform Pcd's value by build option\r
-        if GlobalData.BuildOptionPcd:\r
-            for key in self.Platform.Pcds:\r
-                PlatformPcd = self.Platform.Pcds[key]\r
-                for PcdItem in GlobalData.BuildOptionPcd:\r
-                    if (PlatformPcd.TokenSpaceGuidCName, PlatformPcd.TokenCName) == (PcdItem[0], PcdItem[1]):\r
-                        PlatformPcd.DefaultValue = PcdItem[2]\r
-                        if PlatformPcd.SkuInfoList:\r
-                            Sku = PlatformPcd.SkuInfoList[PlatformPcd.SkuInfoList.keys()[0]]\r
-                            Sku.DefaultValue = PcdItem[2]\r
-                        break\r
 \r
         for key in self.Platform.Pcds:\r
             for SinglePcd in GlobalData.MixedPcd:\r
@@ -1546,9 +1551,9 @@ class PlatformAutoGen(AutoGen):
         #\r
         # The reason of sorting is make sure the unicode string is in double-byte alignment in string table.\r
         #\r
-        UnicodePcdArray = []\r
-        HiiPcdArray     = []\r
-        OtherPcdArray   = []\r
+        UnicodePcdArray = set()\r
+        HiiPcdArray     = set()\r
+        OtherPcdArray   = set()\r
         VpdPcdDict      = {}\r
         VpdFile               = VpdInfoFile.VpdInfoFile()\r
         NeedProcessVpdMapFile = False\r
@@ -1557,34 +1562,55 @@ class PlatformAutoGen(AutoGen):
             if pcd not in self._PlatformPcds.keys():\r
                 self._PlatformPcds[pcd] = self.Platform.Pcds[pcd]\r
 \r
+        for item in self._PlatformPcds:\r
+            if self._PlatformPcds[item].DatumType and self._PlatformPcds[item].DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:\r
+                self._PlatformPcds[item].DatumType = "VOID*"\r
+\r
         if (self.Workspace.ArchList[-1] == self.Arch): \r
             for Pcd in self._DynamicPcdList:\r
                 # just pick the a value to determine whether is unicode string type\r
                 Sku = Pcd.SkuInfoList[Pcd.SkuInfoList.keys()[0]]\r
                 Sku.VpdOffset = Sku.VpdOffset.strip()\r
 \r
-                PcdValue = Sku.DefaultValue\r
-                if Pcd.DatumType == 'VOID*' and PcdValue.startswith("L"):\r
+                if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:\r
+                    Pcd.DatumType = "VOID*"\r
+\r
                     # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex\r
-                    UnicodePcdArray.append(Pcd)\r
-                elif len(Sku.VariableName) > 0:\r
                     # if found HII type PCD then insert to right of UnicodeIndex\r
-                    HiiPcdArray.append(Pcd)\r
-                else:\r
-                    OtherPcdArray.append(Pcd)\r
                 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:\r
                     VpdPcdDict[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName)] = Pcd\r
 \r
+            #Collect DynamicHii PCD values and assign it to DynamicExVpd PCD gEfiMdeModulePkgTokenSpaceGuid.PcdNvStoreDefaultValueBuffer\r
+            PcdNvStoreDfBuffer = VpdPcdDict.get(("PcdNvStoreDefaultValueBuffer","gEfiMdeModulePkgTokenSpaceGuid"))\r
+            if PcdNvStoreDfBuffer:\r
+                self.VariableInfo = self.CollectVariables(self._DynamicPcdList)\r
+                vardump = self.VariableInfo.dump()\r
+                if vardump:\r
+                    PcdNvStoreDfBuffer.DefaultValue = vardump\r
+                    for skuname in PcdNvStoreDfBuffer.SkuInfoList:\r
+                        PcdNvStoreDfBuffer.SkuInfoList[skuname].DefaultValue = vardump\r
+                        PcdNvStoreDfBuffer.MaxDatumSize = str(len(vardump.split(",")))\r
+\r
             PlatformPcds = self._PlatformPcds.keys()\r
             PlatformPcds.sort()\r
             #\r
             # Add VPD type PCD into VpdFile and determine whether the VPD PCD need to be fixed up.\r
             #\r
+            VpdSkuMap = {}\r
             for PcdKey in PlatformPcds:\r
                 Pcd = self._PlatformPcds[PcdKey]\r
                 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD] and \\r
                    PcdKey in VpdPcdDict:\r
                     Pcd = VpdPcdDict[PcdKey]\r
+                    SkuValueMap = {}\r
+                    DefaultSku = Pcd.SkuInfoList.get('DEFAULT')\r
+                    if DefaultSku:\r
+                        PcdValue = DefaultSku.DefaultValue\r
+                        if PcdValue not in SkuValueMap:\r
+                            SkuValueMap[PcdValue] = []\r
+                            VpdFile.Add(Pcd, 'DEFAULT',DefaultSku.VpdOffset)\r
+                        SkuValueMap[PcdValue].append(DefaultSku)\r
+\r
                     for (SkuName,Sku) in Pcd.SkuInfoList.items():\r
                         Sku.VpdOffset = Sku.VpdOffset.strip()\r
                         PcdValue = Sku.DefaultValue\r
@@ -1609,7 +1635,10 @@ class PlatformAutoGen(AutoGen):
                                     EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(Pcd.TokenSpaceGuidCName, Pcd.TokenCName), File=self.MetaFile)\r
                                 else:\r
                                     EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, Alignment))\r
-                        VpdFile.Add(Pcd, Sku.VpdOffset)\r
+                        if PcdValue not in SkuValueMap:\r
+                            SkuValueMap[PcdValue] = []\r
+                            VpdFile.Add(Pcd, SkuName,Sku.VpdOffset)\r
+                        SkuValueMap[PcdValue].append(Sku)\r
                         # if the offset of a VPD is *, then it need to be fixed up by third party tool.\r
                         if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":\r
                             NeedProcessVpdMapFile = True\r
@@ -1617,7 +1646,7 @@ class PlatformAutoGen(AutoGen):
                                 EdkLogger.error("Build", FILE_NOT_FOUND, \\r
                                                 "Fail to find third-party BPDG tool to process VPD PCDs. BPDG Guid tool need to be defined in tools_def.txt and VPD_TOOL_GUID need to be provided in DSC file.")\r
 \r
-\r
+                    VpdSkuMap[PcdKey] = SkuValueMap\r
             #\r
             # Fix the PCDs define in VPD PCD section that never referenced by module.\r
             # An example is PCD for signature usage.\r
@@ -1636,7 +1665,13 @@ class PlatformAutoGen(AutoGen):
                         # Not found, it should be signature\r
                         if not FoundFlag :\r
                             # just pick the a value to determine whether is unicode string type\r
-                            for (SkuName,Sku) in DscPcdEntry.SkuInfoList.items():\r
+                            SkuValueMap = {}\r
+                            SkuObjList = DscPcdEntry.SkuInfoList.items()\r
+                            DefaultSku = DscPcdEntry.SkuInfoList.get('DEFAULT')\r
+                            if DefaultSku:\r
+                                defaultindex = SkuObjList.index(('DEFAULT',DefaultSku))\r
+                                SkuObjList[0],SkuObjList[defaultindex] = SkuObjList[defaultindex],SkuObjList[0]\r
+                            for (SkuName,Sku) in SkuObjList:\r
                                 Sku.VpdOffset = Sku.VpdOffset.strip() \r
                                 \r
                                 # Need to iterate DEC pcd information to get the value & datumtype\r
@@ -1661,7 +1696,6 @@ class PlatformAutoGen(AutoGen):
                                                                                                                     \r
                                 if DscPcdEntry not in self._DynamicPcdList:\r
                                     self._DynamicPcdList.append(DscPcdEntry)\r
-#                                Sku = DscPcdEntry.SkuInfoList[DscPcdEntry.SkuInfoList.keys()[0]]\r
                                 Sku.VpdOffset = Sku.VpdOffset.strip()\r
                                 PcdValue = Sku.DefaultValue\r
                                 if PcdValue == "":\r
@@ -1685,49 +1719,31 @@ class PlatformAutoGen(AutoGen):
                                             EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName), File=self.MetaFile)\r
                                         else:\r
                                             EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName, Alignment))\r
-                                VpdFile.Add(DscPcdEntry, Sku.VpdOffset)\r
+                                if PcdValue not in SkuValueMap:\r
+                                    SkuValueMap[PcdValue] = []\r
+                                    VpdFile.Add(DscPcdEntry, SkuName,Sku.VpdOffset)\r
+                                SkuValueMap[PcdValue].append(Sku)\r
                                 if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":\r
                                     NeedProcessVpdMapFile = True \r
                             if DscPcdEntry.DatumType == 'VOID*' and PcdValue.startswith("L"):\r
-                                UnicodePcdArray.append(DscPcdEntry)\r
+                                UnicodePcdArray.add(DscPcdEntry)\r
                             elif len(Sku.VariableName) > 0:\r
-                                HiiPcdArray.append(DscPcdEntry)\r
+                                HiiPcdArray.add(DscPcdEntry)\r
                             else:\r
-                                OtherPcdArray.append(DscPcdEntry)\r
-                                \r
+                                OtherPcdArray.add(DscPcdEntry)\r
+\r
                                 # if the offset of a VPD is *, then it need to be fixed up by third party tool.\r
-                                                       \r
-                    \r
-                    \r
+                            VpdSkuMap[DscPcd] = SkuValueMap\r
             if (self.Platform.FlashDefinition == None or self.Platform.FlashDefinition == '') and \\r
                VpdFile.GetCount() != 0:\r
                 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, \r
                                 "Fail to get FLASH_DEFINITION definition in DSC file %s which is required when DSC contains VPD PCD." % str(self.Platform.MetaFile))\r
 \r
             if VpdFile.GetCount() != 0:\r
-                FvPath = os.path.join(self.BuildDir, "FV")\r
-                if not os.path.exists(FvPath):\r
-                    try:\r
-                        os.makedirs(FvPath)\r
-                    except:\r
-                        EdkLogger.error("build", FILE_WRITE_FAILURE, "Fail to create FV folder under %s" % self.BuildDir)\r
-\r
-                VpdFilePath = os.path.join(FvPath, "%s.txt" % self.Platform.VpdToolGuid)\r
-\r
-                if VpdFile.Write(VpdFilePath):\r
-                    # retrieve BPDG tool's path from tool_def.txt according to VPD_TOOL_GUID defined in DSC file.\r
-                    BPDGToolName = None\r
-                    for ToolDef in self.ToolDefinition.values():\r
-                        if ToolDef.has_key("GUID") and ToolDef["GUID"] == self.Platform.VpdToolGuid:\r
-                            if not ToolDef.has_key("PATH"):\r
-                                EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "PATH attribute was not provided for BPDG guid tool %s in tools_def.txt" % self.Platform.VpdToolGuid)\r
-                            BPDGToolName = ToolDef["PATH"]\r
-                            break\r
-                    # Call third party GUID BPDG tool.\r
-                    if BPDGToolName != None:\r
-                        VpdInfoFile.CallExtenalBPDGTool(BPDGToolName, VpdFilePath)\r
-                    else:\r
-                        EdkLogger.error("Build", FILE_NOT_FOUND, "Fail to find third-party BPDG tool to process VPD PCDs. BPDG Guid tool need to be defined in tools_def.txt and VPD_TOOL_GUID need to be provided in DSC file.")\r
+\r
+                self.FixVpdOffset(VpdFile)\r
+\r
+                self.FixVpdOffset(self.UpdateNVStoreMaxSize(VpdFile))\r
 \r
                 # Process VPD map file generated by third party BPDG tool\r
                 if NeedProcessVpdMapFile:\r
@@ -1736,23 +1752,76 @@ class PlatformAutoGen(AutoGen):
                         VpdFile.Read(VpdMapFilePath)\r
 \r
                         # Fixup "*" offset\r
-                        for Pcd in self._DynamicPcdList:\r
+                        for pcd in VpdSkuMap:\r
+                            vpdinfo = VpdFile.GetVpdInfo(pcd)\r
+                            if vpdinfo is None:\r
                             # just pick the a value to determine whether is unicode string type\r
-                            i = 0\r
-                            for (SkuName,Sku) in Pcd.SkuInfoList.items():                        \r
-                                if Sku.VpdOffset == "*":\r
-                                    Sku.VpdOffset = VpdFile.GetOffset(Pcd)[i].strip()\r
-                                i += 1\r
+                                continue\r
+                            for pcdvalue in VpdSkuMap[pcd]:\r
+                                for sku in VpdSkuMap[pcd][pcdvalue]:\r
+                                    for item in vpdinfo:\r
+                                        if item[2] == pcdvalue:\r
+                                            sku.VpdOffset = item[1]\r
                     else:\r
                         EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)\r
 \r
-            # Delete the DynamicPcdList At the last time enter into this function \r
+            # Delete the DynamicPcdList At the last time enter into this function\r
+            for Pcd in self._DynamicPcdList:\r
+                # just pick the a value to determine whether is unicode string type\r
+                Sku = Pcd.SkuInfoList[Pcd.SkuInfoList.keys()[0]]\r
+                Sku.VpdOffset = Sku.VpdOffset.strip()\r
+\r
+                if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:\r
+                    Pcd.DatumType = "VOID*"\r
+\r
+                PcdValue = Sku.DefaultValue\r
+                if Pcd.DatumType == 'VOID*' and PcdValue.startswith("L"):\r
+                    # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex\r
+                    UnicodePcdArray.add(Pcd)\r
+                elif len(Sku.VariableName) > 0:\r
+                    # if found HII type PCD then insert to right of UnicodeIndex\r
+                    HiiPcdArray.add(Pcd)\r
+                else:\r
+                    OtherPcdArray.add(Pcd)\r
             del self._DynamicPcdList[:]\r
-        self._DynamicPcdList.extend(UnicodePcdArray)\r
-        self._DynamicPcdList.extend(HiiPcdArray)\r
-        self._DynamicPcdList.extend(OtherPcdArray)\r
+        self._DynamicPcdList.extend(list(UnicodePcdArray))\r
+        self._DynamicPcdList.extend(list(HiiPcdArray))\r
+        self._DynamicPcdList.extend(list(OtherPcdArray))\r
+        allskuset = [(SkuName,Sku.SkuId) for pcd in self._DynamicPcdList for (SkuName,Sku) in pcd.SkuInfoList.items()]\r
+        for pcd in self._DynamicPcdList:\r
+            if len(pcd.SkuInfoList) == 1:\r
+                for (SkuName,SkuId) in allskuset:\r
+                    if type(SkuId) in (str,unicode) and eval(SkuId) == 0 or SkuId == 0:\r
+                        continue\r
+                    pcd.SkuInfoList[SkuName] = copy.deepcopy(pcd.SkuInfoList['DEFAULT'])\r
+                    pcd.SkuInfoList[SkuName].SkuId = SkuId\r
         self.AllPcdList = self._NonDynamicPcdList + self._DynamicPcdList\r
-        \r
+\r
+    def FixVpdOffset(self,VpdFile ):\r
+        FvPath = os.path.join(self.BuildDir, "FV")\r
+        if not os.path.exists(FvPath):\r
+            try:\r
+                os.makedirs(FvPath)\r
+            except:\r
+                EdkLogger.error("build", FILE_WRITE_FAILURE, "Fail to create FV folder under %s" % self.BuildDir)\r
+\r
+        VpdFilePath = os.path.join(FvPath, "%s.txt" % self.Platform.VpdToolGuid)\r
+\r
+        if VpdFile.Write(VpdFilePath):\r
+            # retrieve BPDG tool's path from tool_def.txt according to VPD_TOOL_GUID defined in DSC file.\r
+            BPDGToolName = None\r
+            for ToolDef in self.ToolDefinition.values():\r
+                if ToolDef.has_key("GUID") and ToolDef["GUID"] == self.Platform.VpdToolGuid:\r
+                    if not ToolDef.has_key("PATH"):\r
+                        EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "PATH attribute was not provided for BPDG guid tool %s in tools_def.txt" % self.Platform.VpdToolGuid)\r
+                    BPDGToolName = ToolDef["PATH"]\r
+                    break\r
+            # Call third party GUID BPDG tool.\r
+            if BPDGToolName != None:\r
+                VpdInfoFile.CallExtenalBPDGTool(BPDGToolName, VpdFilePath)\r
+            else:\r
+                EdkLogger.error("Build", FILE_NOT_FOUND, "Fail to find third-party BPDG tool to process VPD PCDs. BPDG Guid tool need to be defined in tools_def.txt and VPD_TOOL_GUID need to be provided in DSC file.")\r
+\r
     ## Return the platform build data object\r
     def _GetPlatform(self):\r
         if self._Platform == None:\r
@@ -1823,6 +1892,13 @@ class PlatformAutoGen(AutoGen):
                     NewOption = self.ToolDefinition["MAKE"]["FLAGS"].strip()\r
                     if NewOption != '':\r
                         self._BuildCommand += SplitOption(NewOption)\r
+                if "MAKE" in self.EdkIIBuildOption:\r
+                    if "FLAGS" in self.EdkIIBuildOption["MAKE"]:\r
+                        Flags = self.EdkIIBuildOption["MAKE"]["FLAGS"]\r
+                        if Flags.startswith('='):\r
+                            self._BuildCommand = [self._BuildCommand[0]] + [Flags[1:]]\r
+                        else:\r
+                            self._BuildCommand += [Flags]\r
         return self._BuildCommand\r
 \r
     ## Get tool chain definition\r
@@ -2282,11 +2358,6 @@ class PlatformAutoGen(AutoGen):
                 TokenCName = PcdItem[0]\r
                 break\r
         if FromPcd != None:\r
-            if GlobalData.BuildOptionPcd:\r
-                for pcd in GlobalData.BuildOptionPcd:\r
-                    if (FromPcd.TokenSpaceGuidCName, FromPcd.TokenCName) == (pcd[0], pcd[1]):\r
-                        FromPcd.DefaultValue = pcd[2]\r
-                        break\r
             if ToPcd.Pending and FromPcd.Type not in [None, '']:\r
                 ToPcd.Type = FromPcd.Type\r
             elif (ToPcd.Type not in [None, '']) and (FromPcd.Type not in [None, ''])\\r
@@ -2313,6 +2384,13 @@ class PlatformAutoGen(AutoGen):
                 ToPcd.DatumType = FromPcd.DatumType\r
             if FromPcd.SkuInfoList not in [None, '', []]:\r
                 ToPcd.SkuInfoList = FromPcd.SkuInfoList\r
+            # Add Flexible PCD format parse\r
+            if ToPcd.DefaultValue:\r
+                try:\r
+                    ToPcd.DefaultValue = ValueExpressionEx(ToPcd.DefaultValue, ToPcd.DatumType, self._GuidDict)(True)\r
+                except BadExpression, Value:\r
+                    EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(ToPcd.TokenSpaceGuidCName, ToPcd.TokenCName, ToPcd.DefaultValue, Value),\r
+                                        File=self.MetaFile)\r
 \r
             # check the validation of datum\r
             IsValid, Cause = CheckPcdDatum(ToPcd.DatumType, ToPcd.DefaultValue)\r
@@ -2323,7 +2401,7 @@ class PlatformAutoGen(AutoGen):
             ToPcd.validlists = FromPcd.validlists\r
             ToPcd.expressions = FromPcd.expressions\r
 \r
-        if ToPcd.DatumType == "VOID*" and ToPcd.MaxDatumSize in ['', None]:\r
+        if FromPcd != None and ToPcd.DatumType == "VOID*" and ToPcd.MaxDatumSize in ['', None]:\r
             EdkLogger.debug(EdkLogger.DEBUG_9, "No MaxDatumSize specified for PCD %s.%s" \\r
                             % (ToPcd.TokenSpaceGuidCName, TokenCName))\r
             Value = ToPcd.DefaultValue\r
@@ -2344,7 +2422,7 @@ class PlatformAutoGen(AutoGen):
             else:\r
                 SkuName = 'DEFAULT'\r
             ToPcd.SkuInfoList = {\r
-                SkuName : SkuInfoClass(SkuName, self.Platform.SkuIds[SkuName], '', '', '', '', '', ToPcd.DefaultValue)\r
+                SkuName : SkuInfoClass(SkuName, self.Platform.SkuIds[SkuName][0], '', '', '', '', '', ToPcd.DefaultValue)\r
             }\r
 \r
     ## Apply PCD setting defined platform to a module\r
@@ -2396,6 +2474,19 @@ class PlatformAutoGen(AutoGen):
                             break\r
                 if Flag:\r
                     self._OverridePcd(ToPcd, PlatformModule.Pcds[Key], Module)\r
+        # use PCD value to calculate the MaxDatumSize when it is not specified\r
+        for Name, Guid in Pcds:\r
+            Pcd = Pcds[Name, Guid]\r
+            if Pcd.DatumType == "VOID*" and Pcd.MaxDatumSize in ['', None]:\r
+                Value = Pcd.DefaultValue\r
+                if Value in [None, '']:\r
+                    Pcd.MaxDatumSize = '1'\r
+                elif Value[0] == 'L':\r
+                    Pcd.MaxDatumSize = str((len(Value) - 2) * 2)\r
+                elif Value[0] == '{':\r
+                    Pcd.MaxDatumSize = str(len(Value.split(',')))\r
+                else:\r
+                    Pcd.MaxDatumSize = str(len(Value) - 1)\r
         return Pcds.values()\r
 \r
     ## Resolve library names to library modules\r
@@ -2691,15 +2782,29 @@ class PlatformAutoGen(AutoGen):
 # to the [depex] section in module's inf file.\r
 #\r
 class ModuleAutoGen(AutoGen):\r
+    # call super().__init__ then call the worker function with different parameter count\r
+    def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        try:\r
+            self._Init\r
+        except:\r
+            super(ModuleAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+            self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args)\r
+            self._Init = True\r
+\r
     ## Cache the timestamps of metafiles of every module in a class variable\r
     #\r
     TimeDict = {}\r
 \r
-    ## The real constructor of ModuleAutoGen\r
-    #\r
-    #  This method is not supposed to be called by users of ModuleAutoGen. It's\r
-    #  only used by factory method __new__() to do real initialization work for an\r
-    #  object of ModuleAutoGen\r
+    def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):\r
+        obj = super(ModuleAutoGen, cls).__new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)\r
+        # check if this module is employed by active platform\r
+        if not PlatformAutoGen(Workspace, args[0], Target, Toolchain, Arch).ValidModule(MetaFile):\r
+            EdkLogger.verbose("Module [%s] for [%s] is not employed by active platform\n" \\r
+                              % (MetaFile, Arch))\r
+            return None\r
+        return obj\r
+            \r
+    ## Initialize ModuleAutoGen\r
     #\r
     #   @param      Workspace           EdkIIWorkspaceBuild object\r
     #   @param      ModuleFile          The path of module file\r
@@ -2708,7 +2813,7 @@ class ModuleAutoGen(AutoGen):
     #   @param      Arch                The arch the module supports\r
     #   @param      PlatformFile        Platform meta-file\r
     #\r
-    def _Init(self, Workspace, ModuleFile, Target, Toolchain, Arch, PlatformFile):\r
+    def _InitWorker(self, Workspace, ModuleFile, Target, Toolchain, Arch, PlatformFile):\r
         EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen module [%s] [%s]" % (ModuleFile, Arch))\r
         GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (ModuleFile, Arch, Toolchain, Target)\r
 \r
@@ -2717,11 +2822,6 @@ class ModuleAutoGen(AutoGen):
 \r
         self.MetaFile = ModuleFile\r
         self.PlatformInfo = PlatformAutoGen(Workspace, PlatformFile, Target, Toolchain, Arch)\r
-        # check if this module is employed by active platform\r
-        if not self.PlatformInfo.ValidModule(self.MetaFile):\r
-            EdkLogger.verbose("Module [%s] for [%s] is not employed by active platform\n" \\r
-                              % (self.MetaFile, Arch))\r
-            return False\r
 \r
         self.SourceDir = self.MetaFile.SubDir\r
         self.SourceDir = mws.relpath(self.SourceDir, self.WorkspaceDir)\r
@@ -3838,7 +3938,13 @@ class ModuleAutoGen(AutoGen):
 \r
     ## Create AsBuilt INF file the module\r
     #\r
-    def CreateAsBuiltInf(self):\r
+    def CreateAsBuiltInf(self, IsOnlyCopy = False):\r
+        self.OutputFile = []\r
+        if IsOnlyCopy:\r
+            if GlobalData.gBinCacheDest:\r
+                self.CopyModuleToCache()\r
+                return\r
+\r
         if self.IsAsBuiltInfCreated:\r
             return\r
             \r
@@ -3971,11 +4077,13 @@ class ModuleAutoGen(AutoGen):
             AsBuiltInfDict['module_pi_specification_version'] += [self.Specification['PI_SPECIFICATION_VERSION']]\r
 \r
         OutputDir = self.OutputDir.replace('\\', '/').strip('/')\r
-        self.OutputFile = []\r
+        DebugDir = self.DebugDir.replace('\\', '/').strip('/')\r
         for Item in self.CodaTargetList:\r
-            File = Item.Target.Path.replace('\\', '/').strip('/').replace(OutputDir, '').strip('/')\r
+            File = Item.Target.Path.replace('\\', '/').strip('/').replace(DebugDir, '').replace(OutputDir, '').strip('/')\r
             if File not in self.OutputFile:\r
                 self.OutputFile.append(File)\r
+            if os.path.isabs(File):\r
+                File = File.replace('\\', '/').strip('/').replace(OutputDir, '').strip('/')\r
             if Item.Target.Ext.lower() == '.aml':\r
                 AsBuiltInfDict['binary_item'] += ['ASL|' + File]\r
             elif Item.Target.Ext.lower() == '.acpi':\r
@@ -4054,7 +4162,7 @@ class ModuleAutoGen(AutoGen):
                     elif BoolValue == 'FALSE':\r
                         Pcd.DefaultValue = '0'\r
 \r
-                if Pcd.DatumType != 'VOID*':\r
+                if Pcd.DatumType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:\r
                     HexFormat = '0x%02x'\r
                     if Pcd.DatumType == 'UINT16':\r
                         HexFormat = '0x%04x'\r
@@ -4198,8 +4306,12 @@ class ModuleAutoGen(AutoGen):
             shutil.copy2(HashFile, FileDir)\r
         if os.path.exists(ModuleFile):\r
             shutil.copy2(ModuleFile, FileDir)\r
+        if not self.OutputFile:\r
+            Ma = self.Workspace.BuildDatabase[PathClass(ModuleFile), self.Arch, self.BuildTarget, self.ToolChain]\r
+            self.OutputFile = Ma.Binaries\r
         if self.OutputFile:\r
             for File in self.OutputFile:\r
+                File = str(File)\r
                 if not os.path.isabs(File):\r
                     File = os.path.join(self.OutputDir, File)\r
                 if os.path.exists(File):\r