BaseTools: Enable structure pcd in FDF file
[mirror_edk2.git] / BaseTools / Source / Python / Workspace / DscBuildData.py
index 5cc8141..1db201f 100644 (file)
@@ -1131,7 +1131,8 @@ class DscBuildData(PlatformBuildClassObject):
             self._Pcds.update(self._GetDynamicVpdPcd(MODEL_PCD_DYNAMIC_EX_VPD))\r
 \r
             self._Pcds = self.CompletePcdValues(self._Pcds)\r
-            self._Pcds = self.OverrideByFdfCommOverAll(self._Pcds)\r
+            self._Pcds = self.OverrideByFdfOverAll(self._Pcds)\r
+            self._Pcds = self.OverrideByCommOverAll(self._Pcds)\r
             self._Pcds = self.UpdateStructuredPcds(MODEL_PCD_TYPE_LIST, self._Pcds)\r
             self._Pcds = self.CompleteHiiPcdsDefaultStores(self._Pcds)\r
             self._Pcds = self._FilterPcdBySkuUsage(self._Pcds)\r
@@ -1191,7 +1192,34 @@ class DscBuildData(PlatformBuildClassObject):
         return structure_pcd_data\r
 \r
     @staticmethod\r
-    def OverrideByFdfComm(StruPcds):\r
+    def OverrideByFdf(StruPcds,workspace):\r
+        if GlobalData.gFdfParser is None:\r
+            return StruPcds\r
+        StructurePcdInFdf = OrderedDict()\r
+        fdfpcd = GlobalData.gFdfParser.Profile.PcdDict\r
+        fdfpcdlocation = GlobalData.gFdfParser.Profile.PcdLocalDict\r
+        for item in fdfpcd :\r
+            if len(item[2]) and (item[0],item[1]) in StruPcds:\r
+                StructurePcdInFdf[(item[1],item[0],item[2] )] = fdfpcd[item]\r
+        GlobalPcds = {(item[0],item[1]) for item in StructurePcdInFdf}\r
+        for Pcd in StruPcds.values():\r
+            if (Pcd.TokenSpaceGuidCName,Pcd.TokenCName) not in GlobalPcds:\r
+                continue\r
+            FieldValues = OrderedDict()\r
+            for item in StructurePcdInFdf:\r
+                if (Pcd.TokenSpaceGuidCName,Pcd.TokenCName) == (item[0],item[1]) and item[2]:\r
+                    FieldValues[item[2]] = StructurePcdInFdf[item]\r
+            for field in FieldValues:\r
+                if field not in Pcd.PcdFieldValueFromFdf:\r
+                    Pcd.PcdFieldValueFromFdf[field] = ["","",""]\r
+                Pcd.PcdFieldValueFromFdf[field][0] = FieldValues[field]\r
+                Pcd.PcdFieldValueFromFdf[field][1] = os.path.relpath(fdfpcdlocation[(Pcd.TokenCName,Pcd.TokenSpaceGuidCName,field)][0],workspace)\r
+                Pcd.PcdFieldValueFromFdf[field][2] = fdfpcdlocation[(Pcd.TokenCName,Pcd.TokenSpaceGuidCName,field)][1]\r
+\r
+        return StruPcds\r
+\r
+    @staticmethod\r
+    def OverrideByComm(StruPcds):\r
         StructurePcdInCom = OrderedDict()\r
         for item in GlobalData.BuildOptionPcd:\r
             if len(item) == 5 and (item[1], item[0]) in StruPcds:\r
@@ -1212,7 +1240,8 @@ class DscBuildData(PlatformBuildClassObject):
                 Pcd.PcdFieldValueFromComm[field][2] = FieldValues[field][1][1]\r
         return StruPcds\r
 \r
-    def OverrideByFdfCommOverAll(self, AllPcds):\r
+\r
+    def OverrideByCommOverAll(self,AllPcds):\r
         def CheckStructureInComm(commpcds):\r
             if not commpcds:\r
                 return False\r
@@ -1259,6 +1288,50 @@ class DscBuildData(PlatformBuildClassObject):
                         self.Pcds[Name, Guid] = copy.deepcopy(PcdInDec)\r
                         self.Pcds[Name, Guid].DefaultValue = NoFiledValues[( Guid, Name)][0]\r
         return AllPcds\r
+\r
+    def OverrideByFdfOverAll(self,AllPcds):\r
+\r
+        if GlobalData.gFdfParser is None:\r
+            return AllPcds\r
+        NoFiledValues = GlobalData.gFdfParser.Profile.PcdDict\r
+        for Guid,Name,Field in NoFiledValues:\r
+            if len(Field):\r
+                continue\r
+            Value = NoFiledValues[(Guid,Name,Field)]\r
+            if (Name,Guid) in AllPcds:\r
+                Pcd = AllPcds.get((Name,Guid))\r
+                if isinstance(self._DecPcds.get((Pcd.TokenCName,Pcd.TokenSpaceGuidCName), None),StructurePcd):\r
+                    self._DecPcds.get((Pcd.TokenCName,Pcd.TokenSpaceGuidCName)).PcdValueFromComm = Value\r
+                else:\r
+                    Pcd.PcdValueFromComm = Value\r
+                    Pcd.DefaultValue = Value\r
+                    for sku in Pcd.SkuInfoList:\r
+                        SkuInfo = Pcd.SkuInfoList[sku]\r
+                        if SkuInfo.DefaultValue:\r
+                            SkuInfo.DefaultValue = Value\r
+                        else:\r
+                            SkuInfo.HiiDefaultValue = Value\r
+                            for defaultstore in SkuInfo.DefaultStoreDict:\r
+                                SkuInfo.DefaultStoreDict[defaultstore] = Value\r
+                    if Pcd.Type in [self._PCD_TYPE_STRING_[MODEL_PCD_DYNAMIC_EX_HII], self._PCD_TYPE_STRING_[MODEL_PCD_DYNAMIC_HII]]:\r
+                        if Pcd.DatumType == TAB_VOID:\r
+                            if not Pcd.MaxDatumSize:\r
+                                Pcd.MaxDatumSize = '0'\r
+                            CurrentSize = int(Pcd.MaxDatumSize,16) if Pcd.MaxDatumSize.upper().startswith("0X") else int(Pcd.MaxDatumSize)\r
+                            OptionSize = len((StringToArray(Pcd.PcdValueFromComm)).split(","))\r
+                            MaxSize = max(CurrentSize, OptionSize)\r
+                            Pcd.MaxDatumSize = str(MaxSize)\r
+            else:\r
+                PcdInDec = self.DecPcds.get((Name,Guid))\r
+                if PcdInDec:\r
+                    PcdInDec.PcdValueFromComm = Value\r
+                    if PcdInDec.Type in [self._PCD_TYPE_STRING_[MODEL_PCD_FIXED_AT_BUILD],\r
+                                        self._PCD_TYPE_STRING_[MODEL_PCD_PATCHABLE_IN_MODULE],\r
+                                        self._PCD_TYPE_STRING_[MODEL_PCD_FEATURE_FLAG]]:\r
+                        self.Pcds[Name, Guid] = copy.deepcopy(PcdInDec)\r
+                        self.Pcds[Name, Guid].DefaultValue = Value\r
+        return AllPcds\r
+\r
     def UpdateStructuredPcds(self, TypeList, AllPcds):\r
 \r
         DynamicPcdType = [self._PCD_TYPE_STRING_[MODEL_PCD_DYNAMIC_DEFAULT],\r
@@ -1368,7 +1441,8 @@ class DscBuildData(PlatformBuildClassObject):
                         if defaultstoreid not in stru_pcd.SkuOverrideValues[skuid]:\r
                             stru_pcd.SkuOverrideValues[skuid][defaultstoreid] = copy.deepcopy(stru_pcd.SkuOverrideValues[nextskuid][mindefaultstorename])\r
                             stru_pcd.ValueChain.add((skuid, defaultstoreid))\r
-        S_pcd_set = DscBuildData.OverrideByFdfComm(S_pcd_set)\r
+        S_pcd_set = DscBuildData.OverrideByFdf(S_pcd_set,self.WorkspaceDir)\r
+        S_pcd_set = DscBuildData.OverrideByComm(S_pcd_set)\r
         Str_Pcd_Values = self.GenerateByteArrayValue(S_pcd_set)\r
         if Str_Pcd_Values:\r
             for (skuname, StoreName, PcdGuid, PcdName, PcdValue) in Str_Pcd_Values:\r
@@ -1594,6 +1668,30 @@ class DscBuildData(PlatformBuildClassObject):
                             while '[' in FieldName:\r
                                 FieldName = FieldName.rsplit('[', 1)[0]\r
                                 CApp = CApp + '  __FLEXIBLE_SIZE(*Size, %s, %s, %d); // From %s Line %d Value %s \n' % (Pcd.DatumType, FieldName.strip("."), ArrayIndex + 1, FieldList[FieldName_ori][1], FieldList[FieldName_ori][2], FieldList[FieldName_ori][0])\r
+        if Pcd.PcdFieldValueFromFdf:\r
+            CApp = CApp + "// From fdf \n"\r
+        for FieldName in Pcd.PcdFieldValueFromFdf:\r
+            FieldName = "." + FieldName\r
+            IsArray = IsFieldValueAnArray(Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][0])\r
+            if IsArray and not (Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][0].startswith('{GUID') and Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][0].endswith('}')):\r
+                try:\r
+                    Value = ValueExpressionEx(Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][0], TAB_VOID, self._GuidDict)(True)\r
+                except BadExpression:\r
+                    EdkLogger.error('Build', FORMAT_INVALID, "Invalid value format for %s. From %s Line %d " %\r
+                                    (".".join((Pcd.TokenSpaceGuidCName, Pcd.TokenCName, FieldName.strip('.'))), Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][1], Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][2]))\r
+                Value, ValueSize = ParseFieldValue(Value)\r
+                CApp = CApp + '  __FLEXIBLE_SIZE(*Size, %s, %s, %d / __ARRAY_ELEMENT_SIZE(%s, %s) + ((%d %% __ARRAY_ELEMENT_SIZE(%s, %s)) ? 1 : 0)); // From %s Line %d Value %s\n' % (Pcd.DatumType, FieldName.strip("."), ValueSize, Pcd.DatumType, FieldName.strip("."), ValueSize, Pcd.DatumType, FieldName.strip("."), Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][1], Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][2], Pcd.PcdFieldValueFromFdf[FieldName.strip(".")][0]);\r
+            else:\r
+                NewFieldName = ''\r
+                FieldName_ori = FieldName.strip('.')\r
+                while '[' in  FieldName:\r
+                    NewFieldName = NewFieldName + FieldName.split('[', 1)[0] + '[0]'\r
+                    ArrayIndex = int(FieldName.split('[', 1)[1].split(']', 1)[0])\r
+                    FieldName = FieldName.split(']', 1)[1]\r
+                FieldName = NewFieldName + FieldName\r
+                while '[' in FieldName:\r
+                    FieldName = FieldName.rsplit('[', 1)[0]\r
+                    CApp = CApp + '  __FLEXIBLE_SIZE(*Size, %s, %s, %d); // From %s Line %s Value %s \n' % (Pcd.DatumType, FieldName.strip("."), ArrayIndex + 1, Pcd.PcdFieldValueFromFdf[FieldName_ori][1], Pcd.PcdFieldValueFromFdf[FieldName_ori][2], Pcd.PcdFieldValueFromFdf[FieldName_ori][0])\r
         if Pcd.PcdFieldValueFromComm:\r
             CApp = CApp + "// From Command Line \n"\r
         for FieldName in Pcd.PcdFieldValueFromComm:\r
@@ -1835,6 +1933,70 @@ class DscBuildData(PlatformBuildClassObject):
     def GenerateCommandLineValueStatement(Pcd):\r
         CApp = '  Assign_%s_%s_CommandLine_Value(Pcd);\n' % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName)\r
         return CApp\r
+    def GenerateFdfValue(self,Pcd):\r
+        CApp = "// Value in Fdf\n"\r
+        CApp = CApp + "void Assign_%s_%s_Fdf_Value(%s *Pcd){\n" % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName,Pcd.DatumType)\r
+        CApp = CApp + '  UINT32  FieldSize;\n'\r
+        CApp = CApp + '  CHAR8   *Value;\n'\r
+\r
+        pcddefaultvalue = Pcd.PcdValueFromFdf\r
+        for FieldList in [pcddefaultvalue,Pcd.PcdFieldValueFromFdf]:\r
+            if not FieldList:\r
+                continue\r
+            if pcddefaultvalue and FieldList == pcddefaultvalue:\r
+                IsArray = IsFieldValueAnArray(FieldList)\r
+                if IsArray:\r
+                    try:\r
+                        FieldList = ValueExpressionEx(FieldList, TAB_VOID)(True)\r
+                    except BadExpression:\r
+                        EdkLogger.error("Build", FORMAT_INVALID, "Invalid value format for %s.%s, from Fdf: %s" %\r
+                                        (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, FieldList))\r
+                Value, ValueSize = ParseFieldValue (FieldList)\r
+\r
+                if isinstance(Value, str):\r
+                    CApp = CApp + '  Pcd = %s; // From Fdf \n' % (Value)\r
+                elif IsArray:\r
+                #\r
+                # Use memcpy() to copy value into field\r
+                #\r
+                    CApp = CApp + '  Value     = %s; // From Fdf .\n' % (DscBuildData.IntToCString(Value, ValueSize))\r
+                    CApp = CApp + '  memcpy (Pcd, Value, %d);\n' % (ValueSize)\r
+                continue\r
+            for FieldName in FieldList:\r
+                IsArray = IsFieldValueAnArray(FieldList[FieldName][0])\r
+                if IsArray:\r
+                    try:\r
+                        FieldList[FieldName][0] = ValueExpressionEx(FieldList[FieldName][0], TAB_VOID, self._GuidDict)(True)\r
+                    except BadExpression:\r
+                        EdkLogger.error('Build', FORMAT_INVALID, "Invalid value format for %s. From %s Line %d " %\r
+                                        (".".join((Pcd.TokenSpaceGuidCName, Pcd.TokenCName, FieldName)), FieldList[FieldName][1], FieldList[FieldName][2]))\r
+                    except:\r
+                        print("error")\r
+                try:\r
+                    Value, ValueSize = ParseFieldValue (FieldList[FieldName][0])\r
+                except Exception:\r
+                    EdkLogger.error('Build', FORMAT_INVALID, "Invalid value format for %s. From %s Line %d " % (".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName,FieldName)),FieldList[FieldName][1], FieldList[FieldName][2]))\r
+                if isinstance(Value, str):\r
+                    CApp = CApp + '  Pcd->%s = %s; // From %s Line %d Value %s\n' % (FieldName, Value, FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])\r
+                elif IsArray:\r
+                #\r
+                # Use memcpy() to copy value into field\r
+                #\r
+                    CApp = CApp + '  FieldSize = __FIELD_SIZE(%s, %s);\n' % (Pcd.DatumType, FieldName)\r
+                    CApp = CApp + '  Value     = %s; // From %s Line %d Value %s\n' % (DscBuildData.IntToCString(Value, ValueSize), FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])\r
+                    CApp = CApp + '  memcpy (&Pcd->%s, Value, (FieldSize > 0 && FieldSize < %d) ? FieldSize : %d);\n' % (FieldName, ValueSize, ValueSize)\r
+                else:\r
+                    if ValueSize > 4:\r
+                        CApp = CApp + '  Pcd->%s = %dULL; // From %s Line %d Value %s\n' % (FieldName, Value, FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])\r
+                    else:\r
+                        CApp = CApp + '  Pcd->%s = %d; // From %s Line %s Value %s\n' % (FieldName, Value, FieldList[FieldName][1], FieldList[FieldName][2], FieldList[FieldName][0])\r
+        CApp = CApp + "}\n"\r
+        return CApp\r
+\r
+    @staticmethod\r
+    def GenerateFdfValueStatement(Pcd):\r
+        CApp = '  Assign_%s_%s_Fdf_Value(Pcd);\n' % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName)\r
+        return CApp\r
 \r
     def GenerateInitializeFunc(self, SkuName, DefaultStore, Pcd, InitByteValue, CApp):\r
         OverrideValues = {DefaultStore:""}\r
@@ -1907,6 +2069,7 @@ class DscBuildData(PlatformBuildClassObject):
             else:\r
                 CApp = CApp + "// SkuName: %s,  DefaultStoreName: STANDARD \n" % self.SkuIdMgr.SystemSkuId\r
                 CApp = CApp + DscBuildData.GenerateInitValueStatement(Pcd, self.SkuIdMgr.SystemSkuId, TAB_DEFAULT_STORES_DEFAULT)\r
+            CApp = CApp + DscBuildData.GenerateFdfValueStatement(Pcd)\r
             CApp = CApp + DscBuildData.GenerateCommandLineValueStatement(Pcd)\r
             #\r
             # Set new PCD value and size\r
@@ -1943,6 +2106,7 @@ class DscBuildData(PlatformBuildClassObject):
             Pcd = StructuredPcds[PcdName]\r
             CApp = CApp + self.GenerateSizeFunction(Pcd)\r
             CApp = CApp + self.GenerateDefaultValueAssignFunction(Pcd)\r
+            CApp = CApp + self.GenerateFdfValue(Pcd)\r
             CApp = CApp + self.GenerateCommandLineValue(Pcd)\r
             if not Pcd.SkuOverrideValues or Pcd.Type in [self._PCD_TYPE_STRING_[MODEL_PCD_FIXED_AT_BUILD],\r
                         self._PCD_TYPE_STRING_[MODEL_PCD_PATCHABLE_IN_MODULE]]:\r