BaseTools: Collect DynamicHii PCD values and assign it to VPD PCD Value
authorLiming Gao <liming.gao@intel.com>
Fri, 24 Nov 2017 06:46:58 +0000 (14:46 +0800)
committerLiming Gao <liming.gao@intel.com>
Mon, 25 Dec 2017 03:05:46 +0000 (11:05 +0800)
https://bugzilla.tianocore.org/show_bug.cgi?id=661
Collect all DynamicHii and DynamicExHii PCD value into PCD
PcdNvStoreDefaultValueBuffer, then firmware can access this PCD value
to get the variable default setting.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Feng Bob C <bob.c.feng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools/Source/Python/AutoGen/GenPcdDb.py
BaseTools/Source/Python/AutoGen/GenVar.py [new file with mode: 0644]
BaseTools/Source/Python/Common/Misc.py
BaseTools/Source/Python/Workspace/BuildClassObject.py

index 1286139..783305c 100644 (file)
@@ -44,6 +44,7 @@ from Common.MultipleWorkspace import MultipleWorkspace as mws
 import InfSectionParser\r
 import datetime\r
 import hashlib\r
+from GenVar import Variable,var_info\r
 \r
 ## Regular expression for splitting Dependency Expression string into tokens\r
 gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)")\r
@@ -1352,6 +1353,26 @@ class PlatformAutoGen(AutoGen):
                 if key in ShareFixedAtBuildPcdsSameValue and ShareFixedAtBuildPcdsSameValue[key]:                    \r
                     LibAuto.ConstPcd[key] = Pcd.DefaultValue\r
 \r
+    def CollectVariables(self, DynamicPcdSet):\r
+        VariableInfo = Variable()\r
+        Index = 0\r
+        for Pcd in DynamicPcdSet:\r
+            if not hasattr(Pcd,"DefaultStoreName"):\r
+                Pcd.DefaultStoreName = ['0']\r
+            for StorageName in Pcd.DefaultStoreName:\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
+                        if Pcd.Phase == "DXE":\r
+                            VariableInfo.append_variable(var_info(Index,pcdname,StorageName,SkuId, StringToArray(Sku.VariableName),VariableGuid, Sku.VariableAttribute , Pcd.DefaultValue,Sku.HiiDefaultValue,Pcd.DatumType))\r
+            Index += 1\r
+        return VariableInfo\r
     ## Collect dynamic PCDs\r
     #\r
     #  Gather dynamic PCDs list from each module and their settings from platform\r
@@ -1582,6 +1603,17 @@ class PlatformAutoGen(AutoGen):
                 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
+                var_info = self.CollectVariables(self._DynamicPcdList)\r
+                default_skuobj = PcdNvStoreDfBuffer.SkuInfoList.get("DEFAULT")\r
+                default_skuobj.DefaultValue = var_info.dump()\r
+                if default_skuobj:\r
+                    PcdNvStoreDfBuffer.SkuInfoList.clear()\r
+                    PcdNvStoreDfBuffer.SkuInfoList['DEFAULT'] = default_skuobj\r
+                    PcdNvStoreDfBuffer.MaxDatumSize = len(default_skuobj.DefaultValue.split(","))\r
+\r
             PlatformPcds = self._PlatformPcds.keys()\r
             PlatformPcds.sort()\r
             #\r
index ddbfe28..40743d1 100644 (file)
@@ -628,31 +628,6 @@ def GetMatchedIndex(Key1, List1, Key2, List2):
     return -1\r
 \r
 \r
-##  Get the integer value from string like "14U" or integer like 2\r
-#\r
-#   @param      Input   The object that may be either a integer value or a string \r
-#  \r
-#   @retval     Value    The integer value that the input represents\r
-#\r
-def GetIntegerValue(Input):\r
-    if type(Input) in (int, long):\r
-        return Input\r
-    String = Input\r
-    if String.endswith("U"):\r
-        String = String[:-1]\r
-    if String.endswith("ULL"):\r
-        String = String[:-3]\r
-    if String.endswith("LL"):\r
-        String = String[:-2]\r
-    \r
-    if String.startswith("0x") or String.startswith("0X"):\r
-        return int(String, 16)\r
-    elif String == '':\r
-        return 0\r
-    else:\r
-        return int(String)\r
-\r
-\r
 ## convert StringArray like {0x36, 0x00, 0x34, 0x00, 0x21, 0x00, 0x36, 0x00, 0x34, 0x00, 0x00, 0x00}\r
 # to List like [0x36, 0x00, 0x34, 0x00, 0x21, 0x00, 0x36, 0x00, 0x34, 0x00, 0x00, 0x00]\r
 #\r
diff --git a/BaseTools/Source/Python/AutoGen/GenVar.py b/BaseTools/Source/Python/AutoGen/GenVar.py
new file mode 100644 (file)
index 0000000..9b76078
--- /dev/null
@@ -0,0 +1,297 @@
+# Copyright (c) 2017, 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
+# 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
+# This file is used to collect the Variable checking information\r
+#\r
+\r
+# #\r
+# Import Modules\r
+#\r
+from struct import pack,unpack\r
+import collections\r
+import copy\r
+from Common.VariableAttributes import VariableAttributes\r
+from Common.Misc import *\r
+import collections\r
+\r
+var_info = collections.namedtuple("uefi_var", "pcdindex,pcdname,defaultstoragename,skuname,var_name, var_guid, var_attribute,pcd_default_value, default_value, data_type")\r
+NvStorageHeaderSize = 28\r
+VariableHeaderSize = 32\r
+\r
+def StringArrayToList(StringArray):\r
+    StringArray = StringArray[1:-1]\r
+    StringArray = '[' + StringArray + ']'\r
+    return eval(StringArray)\r
+\r
+def PackGUID(Guid):\r
+    GuidBuffer = pack('=LHHBBBBBBBB',\r
+                int(Guid[0], 16),\r
+                int(Guid[1], 16),\r
+                int(Guid[2], 16),\r
+                int(Guid[3][-4:-2], 16),\r
+                int(Guid[3][-2:], 16),\r
+                int(Guid[4][-12:-10], 16),\r
+                int(Guid[4][-10:-8], 16),\r
+                int(Guid[4][-8:-6], 16),\r
+                int(Guid[4][-6:-4], 16),\r
+                int(Guid[4][-4:-2], 16),\r
+                int(Guid[4][-2:], 16)\r
+                )\r
+    return GuidBuffer\r
+\r
+class Variable(object):\r
+    def __init__(self,):\r
+        self.VarInfo = []\r
+\r
+    def append_variable(self,uefi_var):\r
+        self.VarInfo.append(uefi_var)\r
+\r
+    def process_variable_data(self):\r
+\r
+        var_data = dict()\r
+\r
+        indexedvarinfo = collections.OrderedDict()\r
+        for item in self.VarInfo:\r
+            if item.pcdindex not in indexedvarinfo:\r
+                indexedvarinfo[item.pcdindex] = dict()\r
+            indexedvarinfo[item.pcdindex][(int(item.skuname),int(item.defaultstoragename))] = item\r
+\r
+        for index in indexedvarinfo:\r
+            sku_var_info = indexedvarinfo[index]\r
+\r
+            default_data_buffer = ""\r
+            others_data_buffer = ""\r
+            tail = None\r
+            default_sku_default = indexedvarinfo.get(index).get((0,0))\r
+\r
+            if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:\r
+                var_max_len = max([len(var_item.default_value.split(",")) for var_item in sku_var_info.values()])\r
+                if len(default_sku_default.default_value.split(",")) < var_max_len:\r
+                    tail = ",".join([ "0x00" for i in range(var_max_len-len(default_sku_default.default_value.split(",")))])\r
+\r
+            default_data_buffer = self.PACK_VARIABLES_DATA(default_sku_default.default_value,default_sku_default.data_type,tail)\r
+\r
+            default_data_array = ()\r
+            for item in default_data_buffer:\r
+                default_data_array += unpack("B",item)\r
+\r
+            if (0,0) not in var_data:\r
+                var_data[(0,0)] = collections.OrderedDict()\r
+            var_data[(0,0)][index] = (default_data_buffer,sku_var_info[(0,0)])\r
+\r
+            for (skuid,defaultstoragename) in indexedvarinfo.get(index):\r
+                tail = None\r
+                if (skuid,defaultstoragename) == (0,0):\r
+                    continue\r
+                other_sku_other = indexedvarinfo.get(index).get((skuid,defaultstoragename))\r
+\r
+                if default_sku_default.data_type not in ["UINT8","UINT16","UINT32","UINT64","BOOLEAN"]:\r
+                    if len(other_sku_other.default_value.split(",")) < var_max_len:\r
+                        tail = ",".join([ "0x00" for i in range(var_max_len-len(other_sku_other.default_value.split(",")))])\r
+\r
+                others_data_buffer = self.PACK_VARIABLES_DATA(other_sku_other.default_value,other_sku_other.data_type,tail)\r
+\r
+                others_data_array = ()\r
+                for item in others_data_buffer:\r
+                    others_data_array += unpack("B",item)\r
+\r
+                data_delta = self.calculate_delta(default_data_array, others_data_array)\r
+\r
+                if (skuid,defaultstoragename) not in var_data:\r
+                    var_data[(skuid,defaultstoragename)] = collections.OrderedDict()\r
+                var_data[(skuid,defaultstoragename)][index] = (data_delta,sku_var_info[(skuid,defaultstoragename)])\r
+        return var_data\r
+\r
+    def new_process_varinfo(self):\r
+\r
+        var_data = self.process_variable_data()\r
+\r
+        pcds_default_data = var_data.get((0,0))\r
+        NvStoreDataBuffer = ""\r
+        var_data_offset = collections.OrderedDict()\r
+        offset = NvStorageHeaderSize\r
+        for default_data,default_info in pcds_default_data.values():\r
+            var_name_buffer = self.PACK_VARIABLE_NAME(default_info.var_name)\r
+\r
+            vendorguid = default_info.var_guid.split('-')\r
+\r
+            if default_info.var_attribute:\r
+                var_attr_value,_ = VariableAttributes.GetVarAttributes(default_info.var_attribute)\r
+            else:\r
+                var_attr_value = 0x07\r
+\r
+            print "default var_name_buffer"\r
+            print self.format_data(var_name_buffer)\r
+            print "default var_buffer"\r
+            print self.format_data(default_data)\r
+\r
+            DataBuffer = self.AlignData(var_name_buffer + default_data)\r
+\r
+            data_size = len(DataBuffer)\r
+            offset += VariableHeaderSize + len(default_info.var_name.split(","))\r
+            var_data_offset[default_info.pcdindex] = offset\r
+            offset += data_size - len(default_info.var_name.split(","))\r
+\r
+            var_header_buffer = self.PACK_VARIABLE_HEADER(var_attr_value, len(default_info.var_name.split(",")), data_size, vendorguid)\r
+            NvStoreDataBuffer += (var_header_buffer + DataBuffer)\r
+\r
+        variable_storage_header_buffer = self.PACK_VARIABLE_STORE_HEADER(len(NvStoreDataBuffer) + 28)\r
+\r
+        nv_default_part = self.AlignData(self.PACK_DEFAULT_DATA(0, 0, self.unpack_data(variable_storage_header_buffer+NvStoreDataBuffer)))\r
+\r
+        print "default whole data \n",self.format_data(nv_default_part)\r
+\r
+        data_delta_structure_buffer = ""\r
+        for skuid,defaultid in var_data:\r
+            if (skuid,defaultid) == (0,0):\r
+                continue\r
+            pcds_sku_data = var_data.get((skuid,defaultid))\r
+            delta_data_set = []\r
+            for pcdindex in pcds_sku_data:\r
+                offset = var_data_offset[pcdindex]\r
+                delta_data,_ = pcds_sku_data[pcdindex]\r
+                delta_data = [(item[0] + offset, item[1]) for item in delta_data]\r
+                delta_data_set.extend(delta_data)\r
+\r
+            data_delta_structure_buffer += self.AlignData(self.PACK_DELTA_DATA(defaultid,skuid,delta_data_set))\r
+            print "delta data"\r
+            print delta_data_set\r
+            print self.format_data(self.AlignData(self.PACK_DELTA_DATA(defaultid,skuid,delta_data_set)))\r
+\r
+        return self.format_data(nv_default_part + data_delta_structure_buffer)\r
+\r
+\r
+    def format_data(self,data):\r
+\r
+        return  [hex(item) for item in self.unpack_data(data)]\r
+\r
+    def unpack_data(self,data):\r
+        final_data = ()\r
+        for item in data:\r
+            final_data += unpack("B",item)\r
+        return final_data\r
+\r
+    def calculate_delta(self, default, theother):\r
+        print "default data \n", default\r
+        print "other data \n",theother\r
+        if len(default) - len(theother) != 0:\r
+            EdkLogger.error("build", FORMAT_INVALID, 'The variable data length is not the same for the same PCD.')\r
+        data_delta = []\r
+        for i in range(len(default)):\r
+            if default[i] != theother[i]:\r
+                data_delta.append((i,theother[i]))\r
+        return data_delta\r
+\r
+    def dump(self):\r
+\r
+        default_var_bin = self.new_process_varinfo()\r
+        value_str = "{"\r
+        default_var_bin_strip = [ data.strip("""'""") for data in default_var_bin]\r
+        value_str += ",".join(default_var_bin_strip)\r
+        value_str += "}"\r
+        return value_str\r
+\r
+    def PACK_VARIABLE_STORE_HEADER(self,size):\r
+        #Signature: gEfiVariableGuid\r
+        Guid = "{ 0xddcf3616, 0x3275, 0x4164, { 0x98, 0xb6, 0xfe, 0x85, 0x70, 0x7f, 0xfe, 0x7d }}"\r
+        Guid = GuidStructureStringToGuidString(Guid)\r
+        GuidBuffer = PackGUID(Guid.split('-'))\r
+\r
+        SizeBuffer = pack('=L',size)\r
+        FormatBuffer = pack('=B',0x5A)\r
+        StateBuffer = pack('=B',0xFE)\r
+        reservedBuffer = pack('=H',0)\r
+        reservedBuffer += pack('=L',0)\r
+\r
+        return GuidBuffer + SizeBuffer + FormatBuffer + StateBuffer + reservedBuffer\r
+\r
+\r
+    def PACK_VARIABLE_HEADER(self,attribute,namesize,datasize,vendorguid):\r
+\r
+        Buffer = pack('=H',0x55AA) # pack StartID\r
+        Buffer += pack('=B',0x3F)  # pack State\r
+        Buffer += pack('=B',0)     # pack reserved\r
+\r
+        Buffer += pack('=L',attribute)\r
+        Buffer += pack('=L',namesize)\r
+        Buffer += pack('=L',datasize)\r
+\r
+        Buffer += PackGUID(vendorguid)\r
+\r
+        return Buffer\r
+\r
+    def PACK_VARIABLES_DATA(self, var_value,data_type, tail = None):\r
+        Buffer = ""\r
+        data_len = 0\r
+        if data_type == "VOID*":\r
+            for value_char in var_value.strip("{").strip("}").split(","):\r
+                Buffer += pack("=B",int(value_char,16))\r
+            data_len += len(var_value.split(","))\r
+            if tail:\r
+                for value_char in tail.split(","):\r
+                    Buffer += pack("=B",int(value_char,16))\r
+                data_len += len(tail.split(","))\r
+        elif data_type == "BOOLEAN":\r
+            Buffer += pack("=B",True) if var_value.upper() == "TRUE" else pack("=B",False)\r
+            data_len += 1\r
+        elif data_type  == "UINT8":\r
+            Buffer += pack("=B",GetIntegerValue(var_value))\r
+            data_len += 1\r
+        elif data_type == "UINT16":\r
+            Buffer += pack("=H",GetIntegerValue(var_value))\r
+            data_len += 2\r
+        elif data_type == "UINT32":\r
+            Buffer += pack("=L",GetIntegerValue(var_value))\r
+            data_len += 4\r
+        elif data_type == "UINT64":\r
+            Buffer += pack("=Q",GetIntegerValue(var_value))\r
+            data_len += 8\r
+\r
+        return Buffer\r
+\r
+    def PACK_DEFAULT_DATA(self, defaultstoragename,skuid,var_value):\r
+        Buffer = ""\r
+        Buffer += pack("=H",6)\r
+        Buffer += pack("=H",int(defaultstoragename))\r
+        Buffer += pack("=H",int(skuid))\r
+\r
+        for item in var_value:\r
+            Buffer += pack("=B",item)\r
+\r
+        Buffer = pack("=L",len(Buffer)+4) + Buffer\r
+\r
+        return Buffer\r
+\r
+    def PACK_DELTA_DATA(self,defaultstoragename,skuid,delta_list):\r
+        Buffer = ""\r
+        Buffer += pack("=H",6)\r
+        Buffer += pack("=H",int(defaultstoragename))\r
+        Buffer += pack("=H",int(skuid))\r
+        for (delta_offset,value) in delta_list:\r
+            Buffer += pack("=L",delta_offset)\r
+            Buffer = Buffer[:-1] + pack("=B",value)\r
+\r
+        Buffer = pack("=L",len(Buffer) + 4) + Buffer\r
+\r
+        return Buffer\r
+\r
+    def AlignData(self,data, align = 4):\r
+        mybuffer = data\r
+        for i in range(len(data) % align):\r
+            mybuffer += pack("=B",0)\r
+\r
+        return mybuffer\r
+\r
+    def PACK_VARIABLE_NAME(self, var_name):\r
+        Buffer = ""\r
+        for name_char in var_name.strip("{").strip("}").split(","):\r
+            Buffer += pack("=B",int(name_char,16))\r
+\r
+        return Buffer\r
\ No newline at end of file
index 2bbd994..3ae2ec5 100644 (file)
@@ -2201,6 +2201,29 @@ def BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, Val
         elif Value == 'FALSE' or Value == '0':\r
             Value = '0'\r
     return  Value\r
+##  Get the integer value from string like "14U" or integer like 2\r
+#\r
+#   @param      Input   The object that may be either a integer value or a string\r
+#\r
+#   @retval     Value    The integer value that the input represents\r
+#\r
+def GetIntegerValue(Input):\r
+    if type(Input) in (int, long):\r
+        return Input\r
+    String = Input\r
+    if String.endswith("U"):\r
+        String = String[:-1]\r
+    if String.endswith("ULL"):\r
+        String = String[:-3]\r
+    if String.endswith("LL"):\r
+        String = String[:-2]\r
+\r
+    if String.startswith("0x") or String.startswith("0X"):\r
+        return int(String, 16)\r
+    elif String == '':\r
+        return 0\r
+    else:\r
+        return int(String)\r
 \r
 ##\r
 #\r
index da265b6..6150557 100644 (file)
@@ -122,12 +122,16 @@ class StructurePcd(PcdClassObject):
         return self.TypeName\r
 \r
     def AddDefaultValue (self, FieldName, Value, FileName="", LineNo=0):\r
+        if FieldName in self.DefaultValues:\r
+            del self.DefaultValues[FieldName]\r
         self.DefaultValues[FieldName] = [Value.strip(), FileName, LineNo]\r
         return self.DefaultValues[FieldName]\r
 \r
     def AddOverrideValue (self, FieldName, Value, SkuName, FileName="", LineNo=0):\r
         if SkuName not in self.SkuOverrideValues:\r
             self.SkuOverrideValues[SkuName] = collections.OrderedDict({})\r
+        if FieldName in self.SkuOverrideValues[SkuName]:\r
+            del self.SkuOverrideValues[SkuName][FieldName]\r
         self.SkuOverrideValues[SkuName][FieldName] = [Value.strip(), FileName, LineNo]\r
         return self.SkuOverrideValues[SkuName][FieldName]\r
 \r