]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFsp2Pkg/Tools/GenCfgOpt.py
IntelFsp2Pkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / IntelFsp2Pkg / Tools / GenCfgOpt.py
index 7658b7fc5cb0782d65a302140df290ef34764586..450c4e3eb9ecb6528fa8f9ae0640a138b18d069d 100644 (file)
@@ -1,13 +1,7 @@
 ## @ GenCfgOpt.py\r
 #\r
-# Copyright (c) 2014 - 2016, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials are licensed and made available under\r
-# the terms and conditions of the BSD License that accompanies this distribution.\r
-# 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
+# Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 ##\r
 \r
@@ -88,6 +82,8 @@ are permitted provided that the following conditions are met:
 **/\r
 """\r
 \r
+BuildOptionPcd = []\r
+\r
 class CLogicalExpression:\r
     def __init__(self):\r
         self.index    = 0\r
@@ -289,7 +285,6 @@ class CGenCfgOpt:
     def __init__(self):\r
         self.Debug          = False\r
         self.Error          = ''\r
-        self.ReleaseMode    = True\r
 \r
         self._GlobalDataDef = """\r
 GlobalDataDef\r
@@ -318,13 +313,6 @@ EndList
         self._FvDir       = ''\r
         self._MapVer      = 0\r
 \r
-    def ParseBuildMode (self, OutputStr):\r
-        if "RELEASE_" in OutputStr:\r
-            self.ReleaseMode = True\r
-        if "DEBUG_" in OutputStr:\r
-            self.ReleaseMode = False\r
-        return\r
-\r
     def ParseMacros (self, MacroDefStr):\r
         # ['-DABC=1', '-D', 'CFG_DEBUG=1', '-D', 'CFG_OUTDIR=Build']\r
         self._MacroDict = {}\r
@@ -426,6 +414,8 @@ EndList
         return ""\r
 \r
     def ParseDscFile (self, DscFile, FvDir):\r
+        Hardcode = False\r
+        AutoAlign = False\r
         self._CfgItemList = []\r
         self._CfgPageDict = {}\r
         self._CfgBlkDict  = {}\r
@@ -446,6 +436,8 @@ EndList
         DscLines     = DscFd.readlines()\r
         DscFd.close()\r
 \r
+        MaxAlign = 32   #Default align to 32, but if there are 64 bit unit, align to 64\r
+        SizeAlign = 0   #record the struct max align\r
         while len(DscLines):\r
             DscLine  = DscLines.pop(0).strip()\r
             Handle   = False\r
@@ -457,7 +449,7 @@ EndList
                 IsUpdSect = False\r
                 if  Match.group(1).lower() == "Defines".lower():\r
                     IsDefSect = True\r
-                if  Match.group(1).lower() == "PcdsFeatureFlag".lower():\r
+                if  (Match.group(1).lower() == "PcdsFeatureFlag".lower() or Match.group(1).lower() == "PcdsFixedAtBuild".lower()):\r
                     IsPcdSect = True\r
                 elif Match.group(1).lower() == "PcdsDynamicVpd.Upd".lower():\r
                     ConfigDict = {}\r
@@ -472,6 +464,7 @@ EndList
                     ConfigDict['comment'] = ''\r
                     ConfigDict['subreg']  = []\r
                     IsUpdSect = True\r
+                    Offset    = 0\r
             else:\r
                 if IsDefSect or IsPcdSect or IsUpdSect or IsVpdSect:\r
                     if re.match("^!else($|\s+#.+)", DscLine):\r
@@ -499,7 +492,7 @@ EndList
                             IfStack.append(Result)\r
                             ElifStack.append(0)\r
                         else:\r
-                            Match  = re.match("!(if|elseif)\s+(.+)", DscLine)\r
+                            Match  = re.match("!(if|elseif)\s+(.+)", DscLine.split("#")[0])\r
                             if Match:\r
                                 Result = self.EvaluateExpress(Match.group(2))\r
                                 if Match.group(1) == "if":\r
@@ -538,6 +531,7 @@ EndList
                                         NewDscLines = IncludeDsc.readlines()\r
                                         IncludeDsc.close()\r
                                         DscLines = NewDscLines + DscLines\r
+                                        Offset = 0\r
                                     else:\r
                                         if DscLine.startswith('!'):\r
                                             print("ERROR: Unrecoginized directive for line '%s'" % DscLine)\r
@@ -563,6 +557,12 @@ EndList
                     self._PcdsDict[Match.group(1)] = Match.group(2)\r
                     if self.Debug:\r
                         print "INFO : PCD %s = [ %s ]" % (Match.group(1), Match.group(2))\r
+                    i = 0\r
+                    while i < len(BuildOptionPcd):\r
+                        Match = re.match("\s*([\w\.]+)\s*\=\s*(\w+)", BuildOptionPcd[i])\r
+                        if Match:\r
+                            self._PcdsDict[Match.group(1)] = Match.group(2)\r
+                        i += 1\r
             else:\r
                 Match = re.match("^\s*#\s+(!BSF|@Bsf|!HDR)\s+(.+)", DscLine)\r
                 if Match:\r
@@ -628,13 +628,22 @@ EndList
 \r
                 # Check VPD/UPD\r
                 if IsUpdSect:\r
-                    Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+)\s*\|\s*(\d+|0x[0-9a-fA-F]+)\s*\|\s*(.+)",DscLine)\r
+                    Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+|\*)\s*\|\s*(\d+|0x[0-9a-fA-F]+)\s*\|\s*(.+)",DscLine)\r
                 else:\r
                     Match = re.match("^([_a-zA-Z0-9]+).([_a-zA-Z0-9]+)\s*\|\s*(0x[0-9A-F]+)(?:\s*\|\s*(.+))?",  DscLine)\r
                 if Match:\r
                     ConfigDict['space']  = Match.group(1)\r
                     ConfigDict['cname']  = Match.group(2)\r
-                    ConfigDict['offset'] = int (Match.group(3), 16)\r
+                    if Match.group(3) != '*':\r
+                        Hardcode = True\r
+                        Offset =  int (Match.group(3), 16)\r
+                    else:\r
+                        AutoAlign = True\r
+\r
+                    if Hardcode and AutoAlign:\r
+                        print("Hardcode and auto-align mixed mode is not supported by GenCfgOpt")\r
+                        raise SystemExit\r
+                    ConfigDict['offset'] = Offset\r
                     if ConfigDict['order'] == -1:\r
                         ConfigDict['order'] = ConfigDict['offset'] << 8\r
                     else:\r
@@ -646,6 +655,7 @@ EndList
                             Length  = int (Match.group(4), 16)\r
                         else :\r
                             Length  = int (Match.group(4))\r
+                        Offset += Length\r
                     else:\r
                         Value = Match.group(4)\r
                         if Value is None:\r
@@ -673,6 +683,52 @@ EndList
                         ConfigDict['help']   = ''\r
                         ConfigDict['type']   = ''\r
                         ConfigDict['option'] = ''\r
+                    if IsUpdSect and AutoAlign:\r
+                        ItemLength = int(ConfigDict['length'])\r
+                        ItemOffset = int(ConfigDict['offset'])\r
+                        ItemStruct = ConfigDict['struct']\r
+                        Unit = 1\r
+                        if ItemLength in [1, 2, 4, 8] and not ConfigDict['value'].startswith('{'):\r
+                            Unit = ItemLength\r
+                            # If there are 64 bit unit, align to 64\r
+                            if Unit == 8:\r
+                                MaxAlign = 64\r
+                                SizeAlign = 8\r
+                        if ItemStruct != '':\r
+                            UnitDict = {'UINT8':1, 'UINT16':2, 'UINT32':4, 'UINT64':8}\r
+                            if ItemStruct in ['UINT8', 'UINT16', 'UINT32', 'UINT64']:\r
+                                Unit = UnitDict[ItemStruct]\r
+                                # If there are 64 bit unit, align to 64\r
+                                if Unit == 8:\r
+                                    MaxAlign = 64\r
+                                SizeAlign = max(SizeAlign, Unit)\r
+                        if (ConfigDict['embed'].find(':START') != -1):\r
+                            Base = ItemOffset\r
+                        SubOffset = ItemOffset - Base\r
+                        SubRemainder = SubOffset % Unit\r
+                        if SubRemainder:\r
+                            Diff = Unit - SubRemainder\r
+                            Offset = Offset + Diff\r
+                            ItemOffset = ItemOffset + Diff\r
+\r
+                        if (ConfigDict['embed'].find(':END') != -1):\r
+                            Remainder = Offset % (MaxAlign/8)   # MaxAlign is either 32 or 64\r
+                            if Remainder:\r
+                                Diff = (MaxAlign/8) - Remainder\r
+                                Offset = Offset + Diff\r
+                                ItemOffset = ItemOffset + Diff\r
+                            MaxAlign = 32                       # Reset to default 32 align when struct end\r
+                        if (ConfigDict['cname'] == 'UpdTerminator'):\r
+                            # ItemLength is the size of UpdTerminator\r
+                            # Itemlength might be 16, 32, or 64\r
+                            # Struct align to 64 if UpdTerminator\r
+                            # or struct size is 64 bit, else align to 32\r
+                            Remainder = Offset % max(ItemLength/8, 4, SizeAlign)\r
+                            Offset = Offset + ItemLength\r
+                            if Remainder:\r
+                                Diff = max(ItemLength/8, 4, SizeAlign) - Remainder\r
+                                ItemOffset = ItemOffset + Diff\r
+                        ConfigDict['offset'] = ItemOffset\r
 \r
                     self._CfgItemList.append(ConfigDict.copy())\r
                     ConfigDict['name']   = ''\r
@@ -815,9 +871,6 @@ EndList
                     TxtFd.write("%s.UnusedUpdSpace%d|%s0x%04X|0x%04X|{0}\n" % (Item['space'], SpaceIdx, Default, NextOffset - StartAddr, Offset - NextOffset))\r
                     SpaceIdx = SpaceIdx + 1\r
                 NextOffset = Offset + Item['length']\r
-                if Item['cname'] == 'PcdSerialIoUartDebugEnable':\r
-                    if self.ReleaseMode == False:\r
-                        Item['value'] = 0x01\r
                 TxtFd.write("%s.%s|%s0x%04X|%s|%s\n" % (Item['space'],Item['cname'],Default,Item['offset'] - StartAddr,Item['length'],Item['value']))\r
             TxtFd.close()\r
         return 0\r
@@ -987,6 +1040,13 @@ EndList
         NewTextBody.extend(OldTextBody)\r
         return NewTextBody\r
 \r
+    def WriteLinesWithoutTailingSpace (self, HeaderFd, Line):\r
+        TxtBody2 = Line.splitlines(True)\r
+        for Line2 in TxtBody2:\r
+            Line2 = Line2.rstrip()\r
+            Line2 += '\n'\r
+            HeaderFd.write (Line2)\r
+        return 0\r
     def CreateHeaderFile (self, InputHeaderFile):\r
         FvDir = self._FvDir\r
 \r
@@ -1186,7 +1246,7 @@ EndList
                 Index += 1\r
                 for Item in range(len(StructStart)):\r
                     if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:\r
-                        HeaderFd.write (Line)\r
+                        self.WriteLinesWithoutTailingSpace(HeaderFd, Line)\r
             HeaderFd.write("#pragma pack()\n\n")\r
             HeaderFd.write("#endif\n")\r
             HeaderFd.close()\r
@@ -1231,7 +1291,7 @@ EndList
                 Index += 1\r
                 for Item in range(len(StructStart)):\r
                     if Index >= StructStartWithComment[Item] and Index <= StructEnd[Item]:\r
-                        HeaderFd.write (Line)\r
+                        self.WriteLinesWithoutTailingSpace(HeaderFd, Line)\r
         HeaderFd.write("#pragma pack()\n\n")\r
         HeaderFd.write("#endif\n")\r
         HeaderFd.close()\r
@@ -1239,6 +1299,7 @@ EndList
         return 0\r
 \r
     def WriteBsfStruct  (self, BsfFd, Item):\r
+        LogExpr = CLogicalExpression()\r
         if Item['type'] == "None":\r
             Space = "gPlatformFspPkgTokenSpaceGuid"\r
         else:\r
@@ -1260,6 +1321,9 @@ EndList
                 for Option in OptList:\r
                     Option = Option.strip()\r
                     (OpVal, OpStr) = Option.split(':')\r
+                    test = LogExpr.getNumber (OpVal)\r
+                    if test is None:\r
+                        raise Exception("Selection Index '%s' is not a number" % OpVal)\r
                     TmpList.append((OpVal, OpStr))\r
         return  TmpList\r
 \r
@@ -1400,7 +1464,7 @@ EndList
 \r
 \r
 def Usage():\r
-    print "GenCfgOpt Version 0.52"\r
+    print "GenCfgOpt Version 0.53"\r
     print "Usage:"\r
     print "    GenCfgOpt  UPDTXT  PlatformDscFile BuildFvDir                 [-D Macros]"\r
     print "    GenCfgOpt  HEADER  PlatformDscFile BuildFvDir  InputHFile     [-D Macros]"\r
@@ -1410,7 +1474,14 @@ def Main():
     #\r
     # Parse the options and args\r
     #\r
+    i = 1\r
+\r
     GenCfgOpt = CGenCfgOpt()\r
+    while i < len(sys.argv):\r
+        if sys.argv[i].strip().lower() == "--pcd":\r
+            BuildOptionPcd.append(sys.argv[i+1])\r
+            i += 1\r
+        i += 1\r
     argc = len(sys.argv)\r
     if argc < 4:\r
         Usage()\r
@@ -1428,10 +1499,10 @@ def Main():
             else:\r
                 OutFile = sys.argv[4]\r
                 Start = 5\r
-            GenCfgOpt.ParseBuildMode(sys.argv[3])\r
-            if GenCfgOpt.ParseMacros(sys.argv[Start:]) != 0:\r
-                print "ERROR: Macro parsing failed !"\r
-                return 3\r
+            if argc > Start:\r
+                if GenCfgOpt.ParseMacros(sys.argv[Start:]) != 0:\r
+                    print "ERROR: Macro parsing failed !"\r
+                    return 3\r
 \r
         FvDir = sys.argv[3]\r
         if not os.path.exists(FvDir):\r