public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Yonghong Zhu <yonghong.zhu@intel.com>
To: edk2-devel@lists.01.org
Cc: Yunhua Feng <yunhuax.feng@intel.com>
Subject: [Patch 1/2] BaseTools: Support PCD flexible values format
Date: Wed, 27 Dec 2017 00:40:12 +0800	[thread overview]
Message-ID: <1514306413-5880-2-git-send-email-yonghong.zhu@intel.com> (raw)
In-Reply-To: <1514306413-5880-1-git-send-email-yonghong.zhu@intel.com>

https://bugzilla.tianocore.org/show_bug.cgi?id=541

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Signed-off-by: Yonghong Zhu <yonghong.zhu@intel.com>
---
 BaseTools/Source/Python/AutoGen/AutoGen.py         |  39 ++++
 BaseTools/Source/Python/AutoGen/GenC.py            |   2 +
 BaseTools/Source/Python/Common/Expression.py       | 246 ++++++++++++++++++++-
 BaseTools/Source/Python/Common/Misc.py             | 203 +++++++++--------
 BaseTools/Source/Python/Workspace/DscBuildData.py  |  14 +-
 BaseTools/Source/Python/Workspace/InfBuildData.py  |   7 +
 .../Source/Python/Workspace/MetaFileParser.py      |  17 +-
 .../Source/Python/Workspace/WorkspaceCommon.py     |   8 +-
 BaseTools/Source/Python/build/BuildReport.py       |   6 +
 9 files changed, 429 insertions(+), 113 deletions(-)

diff --git a/BaseTools/Source/Python/AutoGen/AutoGen.py b/BaseTools/Source/Python/AutoGen/AutoGen.py
index cacd009..8be5bfc 100644
--- a/BaseTools/Source/Python/AutoGen/AutoGen.py
+++ b/BaseTools/Source/Python/AutoGen/AutoGen.py
@@ -269,10 +269,11 @@ class WorkspaceAutoGen(AutoGen):
         self.AutoGenObjectList = []
         self._BuildDir      = None
         self._FvDir         = None
         self._MakeFileDir   = None
         self._BuildCommand  = None
+        self._GuidDict = {}
 
         # there's many relative directory operations, so ...
         os.chdir(self.WorkspaceDir)
 
         #
@@ -417,24 +418,42 @@ class WorkspaceAutoGen(AutoGen):
                     TokenSpaceGuidCNameList = []
                     FoundFlag = False
                     PcdDatumType = ''
                     NewValue = ''
                     for package in PGen.PackageList:
+                        Guids = package.Guids
+                        self._GuidDict.update(Guids)
+                    for package in PGen.PackageList:
                         for key in package.Pcds:
                             PcdItem = package.Pcds[key]
                             if HasTokenSpace:
                                 if (PcdItem.TokenCName, PcdItem.TokenSpaceGuidCName) == (TokenCName, TokenSpaceGuidCName):
                                     PcdDatumType = PcdItem.DatumType
+                                    if pcdvalue.startswith('H'):
+                                        try:
+                                            pcdvalue = ValueExpressionEx(pcdvalue[1:], PcdDatumType, self._GuidDict)(True)
+                                        except BadExpression, Value:
+                                            if Value.result > 1:
+                                                EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s",  %s' %
+                                                                (TokenSpaceGuidCName, TokenCName, pcdvalue, Value))
+                                        pcdvalue = 'H' + pcdvalue
                                     NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)
                                     FoundFlag = True
                             else:
                                 if PcdItem.TokenCName == TokenCName:
                                     if not PcdItem.TokenSpaceGuidCName in TokenSpaceGuidCNameList:
                                         if len (TokenSpaceGuidCNameList) < 1:
                                             TokenSpaceGuidCNameList.append(PcdItem.TokenSpaceGuidCName)
                                             PcdDatumType = PcdItem.DatumType
                                             TokenSpaceGuidCName = PcdItem.TokenSpaceGuidCName
+                                            if pcdvalue.startswith('H'):
+                                                try:
+                                                    pcdvalue = ValueExpressionEx(pcdvalue[1:], PcdDatumType, self._GuidDict)(True)
+                                                except BadExpression, Value:
+                                                    EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %
+                                                                    (TokenSpaceGuidCName, TokenCName, pcdvalue, Value))
+                                                pcdvalue = 'H' + pcdvalue
                                             NewValue = BuildOptionPcdValueFormat(TokenSpaceGuidCName, TokenCName, PcdDatumType, pcdvalue)
                                             FoundFlag = True
                                         else:
                                             EdkLogger.error(
                                                     'build',
@@ -2444,10 +2463,30 @@ class PlatformAutoGen(AutoGen):
                 ToPcd.MaxDatumSize = FromPcd.MaxDatumSize
             if FromPcd.DatumType not in [None, '']:
                 ToPcd.DatumType = FromPcd.DatumType
             if FromPcd.SkuInfoList not in [None, '', []]:
                 ToPcd.SkuInfoList = FromPcd.SkuInfoList
+            # Add Flexible PCD format parse
+            PcdValue = ToPcd.DefaultValue
+            if PcdValue:
+                try:
+                    ToPcd.DefaultValue = ValueExpression(PcdValue)(True)
+                except WrnExpression, Value:
+                    ToPcd.DefaultValue = Value.result
+                except BadExpression, Value:
+                    EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(ToPcd.TokenSpaceGuidCName, ToPcd.TokenCName, ToPcd.DefaultValue, Value),
+                                    File=self.MetaFile)
+            if ToPcd.DefaultValue:
+                _GuidDict = {}
+                for Pkg in self.PackageList:
+                    Guids = Pkg.Guids
+                    _GuidDict.update(Guids)
+                try:
+                    ToPcd.DefaultValue = ValueExpressionEx(ToPcd.DefaultValue, ToPcd.DatumType, _GuidDict)(True)
+                except BadExpression, Value:
+                    EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(ToPcd.TokenSpaceGuidCName, ToPcd.TokenCName, ToPcd.DefaultValue, Value),
+                                        File=self.MetaFile)
 
             # check the validation of datum
             IsValid, Cause = CheckPcdDatum(ToPcd.DatumType, ToPcd.DefaultValue)
             if not IsValid:
                 EdkLogger.error('build', FORMAT_INVALID, Cause, File=self.MetaFile,
diff --git a/BaseTools/Source/Python/AutoGen/GenC.py b/BaseTools/Source/Python/AutoGen/GenC.py
index 6b95cd4..3e98506 100644
--- a/BaseTools/Source/Python/AutoGen/GenC.py
+++ b/BaseTools/Source/Python/AutoGen/GenC.py
@@ -1031,10 +1031,12 @@ def CreateModulePcdCode(Info, AutoGenC, AutoGenH, Pcd):
             elif BoolValue == 'FALSE' or BoolValue == '0':
                 Value = '0U'
 
         if Pcd.DatumType in ['UINT64', 'UINT32', 'UINT16', 'UINT8']:
             try:
+                if Value.upper().endswith('L'):
+                    Value = Value[:-1]
                 if Value.upper().startswith('0X'):
                     ValueNumber = int (Value, 16)
                 else:
                     ValueNumber = int (Value)
             except:
diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index ba83e02..4d8bd14 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -13,11 +13,13 @@
 ## Import Modules
 #
 from Common.GlobalData import *
 from CommonDataClass.Exceptions import BadExpression
 from CommonDataClass.Exceptions import WrnExpression
-from Misc import GuidStringToGuidStructureString
+from Misc import GuidStringToGuidStructureString, ParseFieldValue
+import Common.EdkLogger as EdkLogger
+import copy
 
 ERR_STRING_EXPR         = 'This operator cannot be used in string expression: [%s].'
 ERR_SNYTAX              = 'Syntax error, the rest of expression cannot be evaluated: [%s].'
 ERR_MATCH               = 'No matching right parenthesis.'
 ERR_STRING_TOKEN        = 'Bad string token: [%s].'
@@ -112,10 +114,19 @@ def ReplaceExprMacro(String, Macros, ExceptionList = None):
             String = RetStr
             MacroStartPos = String.find('$(')
         StrList[i] = RetStr
     return ''.join(StrList)
 
+# transfer int to string for in/not in expression
+def IntToStr(Value):
+    StrList = []
+    while Value > 0:
+        StrList.append(chr(Value & 0xff))
+        Value = Value >> 8
+    Value = '"' + ''.join(StrList) + '"'
+    return Value
+
 SupportedInMacroList = ['TARGET', 'TOOL_CHAIN_TAG', 'ARCH', 'FAMILY']
 
 class ValueExpression(object):
     # Logical operator mapping
     LogicalOperators = {
@@ -144,14 +155,28 @@ class ValueExpression(object):
 
     @staticmethod
     def Eval(Operator, Oprand1, Oprand2 = None):
         WrnExp = None
 
-        if Operator not in ["==", "!=", ">=", "<=", ">", "<", "in", "not in"] and \
-            (type(Oprand1) == type('') or type(Oprand2) == type('')):
-            raise BadExpression(ERR_STRING_EXPR % Operator)
-
+        if Operator not in ["in", "not in"] and (type(Oprand1) == type('') or type(Oprand2) == type('')):
+            if type(Oprand1) == type(''):
+                if Oprand1[0] in ['"', "'"] or Oprand1.startswith('L"') or Oprand1.startswith("L'")or Oprand1.startswith('UINT'):
+                    Oprand1, Size = ParseFieldValue(Oprand1)
+                else:
+                    Oprand1,Size = ParseFieldValue('"' + Oprand1 + '"')
+            if type(Oprand2) == type(''):
+                if Oprand2[0] in ['"', "'", 'L'] or Oprand2.startswith('UINT'):
+                    Oprand2, Size = ParseFieldValue(Oprand2)
+                else:
+                    Oprand2, Size = ParseFieldValue('"' + Oprand2 + '"')
+            if type(Oprand1) == type('') or type(Oprand2) == type(''):
+                raise BadExpression(ERR_STRING_EXPR % Operator)
+        if Operator in ['in', 'not in']:
+            if type(Oprand1) != type(''):
+                Oprand1 = IntToStr(Oprand1)
+            if type(Oprand2) != type(''):
+                Oprand2 = IntToStr(Oprand2)
         TypeDict = {
             type(0)  : 0,
             type(0L) : 0,
             type('') : 1,
             type(True) : 2
@@ -224,10 +249,13 @@ class ValueExpression(object):
         self._NoProcess = False
         if type(Expression) != type(''):
             self._Expr = Expression
             self._NoProcess = True
             return
+        if Expression.strip().startswith('{') and Expression.strip().endswith('}'):
+            self._Expr = Expression
+            self._NoProcess = True
 
         self._Expr = ReplaceExprMacro(Expression.strip(),
                                   SymbolTable,
                                   SupportedInMacroList)
 
@@ -235,11 +263,11 @@ class ValueExpression(object):
             raise BadExpression(ERR_EMPTY_EXPR)
 
         #
         # The symbol table including PCD and macro mapping
         #
-        self._Symb = SymbolTable
+        self._Symb = copy.deepcopy(SymbolTable)
         self._Symb.update(self.LogicalOperators)
         self._Idx = 0
         self._Len = len(self._Expr)
         self._Token = ''
         self._WarnExcept = None
@@ -282,11 +310,11 @@ class ValueExpression(object):
             if Val == 'L""':
                 Val = False
             elif not Val:
                 Val = False
                 RealVal = '""'
-            elif not Val.startswith('L"') and not Val.startswith('{'):
+            elif not Val.startswith('L"') and not Val.startswith('{') and not Val.startswith("L'"):
                 Val = True
                 RealVal = '"' + RealVal + '"'
 
         # The expression has been parsed, but the end of expression is not reached
         # It means the rest does not comply EBNF of <Expression>
@@ -424,10 +452,19 @@ class ValueExpression(object):
     # Try to convert string to number
     def __IsNumberToken(self):
         Radix = 10
         if self._Token.lower()[0:2] == '0x' and len(self._Token) > 2:
             Radix = 16
+        if self._Token.startswith('"') or self._Token.startswith("'")\
+            or self._Token.startswith("L'") or self._Token.startswith('L"'):
+            Flag = 0
+            for Index in range(len(self._Token)):
+                if self._Token[Index] in ['"', "'"]:
+                    Flag += 1
+            if Flag == 2:
+                self._Token = ParseFieldValue(self._Token)[0]
+                return True
         try:
             self._Token = int(self._Token, Radix)
             return True
         except ValueError:
             return False
@@ -471,14 +508,16 @@ class ValueExpression(object):
 
         # Replace escape \\\", \"
         Expr = self._Expr[self._Idx:].replace('\\\\', '//').replace('\\\"', '\\\'')
         for Ch in Expr:
             self._Idx += 1
-            if Ch == '"':
+            if Ch == '"' or Ch == "'":
                 break
         self._Token = self._LiteralToken = self._Expr[Idx:self._Idx]
-        if not self._Token.endswith('"'):
+        if self._Token.startswith('"') and not self._Token.endswith('"'):
+            raise BadExpression(ERR_STRING_TOKEN % self._Token)
+        if self._Token.startswith("'") and not self._Token.endswith("'"):
             raise BadExpression(ERR_STRING_TOKEN % self._Token)
         self._Token = self._Token[1:-1]
         return self._Token
 
     # Get token that is comprised by alphanumeric, underscore or dot(used by PCD)
@@ -576,11 +615,48 @@ class ValueExpression(object):
         if Expr.startswith('L"'):
             # Skip L
             self._Idx += 1
             UStr = self.__GetString()
             self._Token = 'L"' + UStr + '"'
+            self._Token, Size = ParseFieldValue(self._Token)
+            return self._Token
+        elif Expr.startswith("L'"):
+            # Skip L
+            self._Idx += 1
+            UStr = self.__GetString()
+            self._Token = "L'" + UStr + "'"
+            self._Token, Size = ParseFieldValue(self._Token)
+            return self._Token
+        elif Expr.startswith('"'):
+            UStr = self.__GetString()
+            self._Token = '"' + UStr + '"'
+            self._Token, Size = ParseFieldValue(self._Token)
             return self._Token
+        elif Expr.startswith("'"):
+            UStr = self.__GetString()
+            self._Token = "'" + UStr + "'"
+            self._Token, Size = ParseFieldValue(self._Token)
+            return self._Token
+        elif Expr.startswith('UINT'):
+            Re = re.compile('(?:UINT8|UINT16|UINT32|UINT64)\((.+)\)')
+            try:
+                RetValue = Re.search(Expr).group(1)
+            except:
+                 raise BadExpression('Invalid Expression %s' % Expr)
+            Idx = self._Idx
+            for Ch in Expr:
+                self._Idx += 1
+                if Ch == '(':
+                    Prefix = self._Expr[Idx:self._Idx - 1]
+                    Idx = self._Idx
+                if Ch == ')':
+                    TmpValue = self._Expr[Idx :self._Idx - 1]
+                    TmpValue = ValueExpression(TmpValue)(True)
+                    TmpValue = '0x%x' % int(TmpValue) if type(TmpValue) != type('') else TmpValue
+                    break
+            self._Token, Size = ParseFieldValue(Prefix + '(' + TmpValue + ')')
+            return  self._Token
 
         self._Token = ''
         if Expr:
             Ch = Expr[0]
             Match = self.RegGuidPattern.match(Expr)
@@ -646,10 +722,162 @@ class ValueExpression(object):
                 self._Token = self.LogicalOperators[self._Token]
             return True
         self._Idx = Idx
         return False
 
+class ValueExpressionEx(ValueExpression):
+    def __init__(self, PcdValue, PcdType, SymbolTable={}):
+        ValueExpression.__init__(self, PcdValue, SymbolTable)
+        self.PcdValue = PcdValue
+        self.PcdType = PcdType
+
+    def __call__(self, RealValue=False, Depth=0):
+        PcdValue = self.PcdValue
+        try:
+            PcdValue = ValueExpression.__call__(self, RealValue, Depth)
+        except WrnExpression, Value:
+            PcdValue = Value.result
+
+        if PcdValue == 'True':
+            PcdValue = '1'
+        if PcdValue == 'False':
+            PcdValue = '0'
+        if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BOOLEAN']:
+            PcdValue = PcdValue.strip()
+            if type(PcdValue) == type('') and PcdValue.startswith('{') and PcdValue.endswith('}'):
+                PcdValue = PcdValue[1:-1].split(',')
+            if type(PcdValue) == type([]):
+                TmpValue = 0
+                Size = 0
+                for Item in PcdValue:
+                    if Item.startswith('UINT16'):
+                        ItemSize = 2
+                    elif Item.startswith('UINT32'):
+                        ItemSize = 4
+                    elif Item.startswith('UINT64'):
+                        ItemSize = 8
+                    else:
+                        ItemSize = 0
+                    Item = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)
+
+                    if ItemSize == 0:
+                        ItemValue, ItemSize = ParseFieldValue(Item)
+                    else:
+                        ItemValue = ParseFieldValue(Item)[0]
+
+                    if type(ItemValue) == type(''):
+                        ItemValue = int(ItemValue, 16) if ItemValue.startswith('0x') else int(ItemValue)
+
+                    TmpValue = (ItemValue << (Size * 8)) | TmpValue
+                    Size = Size + ItemSize
+            else:
+                TmpValue, Size = ParseFieldValue(PcdValue)
+            if type(TmpValue) == type(''):
+                TmpValue = int(TmpValue)
+            else:
+                PcdValue = '0x%0{}X'.format(Size) % (TmpValue)
+            if TmpValue < 0:
+                raise  BadExpression('Type %s PCD Value is negative' % self.PcdType)
+            if self.PcdType == 'UINT8' and Size > 1:
+                raise BadExpression('Type %s PCD Value Size is Larger than 1 byte' % self.PcdType)
+            if self.PcdType == 'UINT16' and Size > 2:
+                raise BadExpression('Type %s PCD Value Size is Larger than 2 byte' % self.PcdType)
+            if self.PcdType == 'UINT32' and Size > 4:
+                raise BadExpression('Type %s PCD Value Size is Larger than 4 byte' % self.PcdType)
+            if self.PcdType == 'UINT64' and Size > 8:
+                raise BadExpression('Type %s PCD Value Size is Larger than 8 byte' % self.PcdType)
+        if self.PcdType in ['VOID*']:
+            try:
+                TmpValue = long(PcdValue)
+                TmpList = []
+                if TmpValue.bit_length() == 0:
+                    PcdValue = '{0x00}'
+                else:
+                    for I in range((TmpValue.bit_length() + 7) / 8):
+                        TmpList.append('0x%02x' % ((TmpValue >> I * 8) & 0xff))
+                    PcdValue = '{' + ', '.join(TmpList) + '}'
+            except:
+                if PcdValue.strip().startswith('{'):
+                    PcdValue = PcdValue.strip()[1:-1].strip()
+                    Size = 0
+                    ValueStr = ''
+                    TokenSpaceGuidName = ''
+                    if PcdValue.startswith('GUID') and PcdValue.endswith(')'):
+                        try:
+                            TokenSpaceGuidName = re.search('GUID\((\w+)\)', PcdValue).group(1)
+                        except:
+                            pass
+                        if TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:
+                            PcdValue = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'
+                        elif TokenSpaceGuidName:
+                            raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)
+
+                        ListItem, Size = ParseFieldValue(PcdValue)
+                    elif PcdValue.startswith('DEVICE_PATH') and PcdValue.endswith(')'):
+                        ListItem, Size = ParseFieldValue(PcdValue)
+                    else:
+                        ListItem = PcdValue.split(',')
+
+                    if type(ListItem) == type(0) or type(ListItem) == type(0L):
+                        for Index in range(0, Size):
+                            ValueStr += '0x%02X' % (int(ListItem) & 255)
+                            ListItem >>= 8
+                            ValueStr += ', '
+                            PcdValue = '{' + ValueStr[:-2] + '}'
+                    elif type(ListItem) == type(''):
+                        if ListItem.startswith('{') and ListItem.endswith('}'):
+                            PcdValue = ListItem
+                    else:
+                        LabelDict = {}
+                        ReLabel = re.compile('LABEL\((\w+)\)')
+                        ReOffset = re.compile('OFFSET_OF\((\w+)\)')
+                        for Index, Item in enumerate(ListItem):
+                            # for LABEL parse
+                            Item = Item.strip()
+                            try:
+                                LabelList = ReLabel.findall(Item)
+                                for Label in LabelList:
+                                    if Label not in LabelDict.keys():
+                                        LabelDict[Label] = str(Index)
+                                Item = ReLabel.sub('', Item)
+                            except:
+                                pass
+                            try:
+                                OffsetList = ReOffset.findall(Item)
+                            except:
+                                pass
+                            for Offset in OffsetList:
+                                if Offset in LabelDict.keys():
+                                    Re = re.compile('OFFSET_OF\(%s\)'% Offset)
+                                    Item = Re.sub(LabelDict[Offset], Item)
+                                else:
+                                    raise BadExpression('%s not defined before use' % Offset)
+                            if Item.startswith('UINT16'):
+                                ItemSize = 2
+                            elif Item.startswith('UINT32'):
+                                ItemSize = 4
+                            elif Item.startswith('UINT64'):
+                                ItemSize = 8
+                            else:
+                                ItemSize = 0
+                            TmpValue = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)
+                            Item = '0x%x' % TmpValue if type(TmpValue) != type('') else TmpValue
+                            if ItemSize == 0:
+                                ItemValue, ItemSize = ParseFieldValue(Item)
+                            else:
+                                ItemValue = ParseFieldValue(Item)[0]
+                            for I in range(0, ItemSize):
+                                ValueStr += '0x%02X' % (int(ItemValue) & 255)
+                                ItemValue >>= 8
+                                ValueStr += ', '
+                            Size += ItemSize
+
+                        if Size > 0:
+                            PcdValue = '{' + ValueStr[:-2] + '}'
+        if RealValue:
+            return PcdValue
+
 if __name__ == '__main__':
     pass
     while True:
         input = raw_input('Input expr: ')
         if input in 'qQ':
diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py
index 0374be0..c51b685 100644
--- a/BaseTools/Source/Python/Common/Misc.py
+++ b/BaseTools/Source/Python/Common/Misc.py
@@ -35,10 +35,11 @@ from BuildToolError import *
 from CommonDataClass.DataClass import *
 from Parsing import GetSplitValueList
 from Common.LongFilePathSupport import OpenLongFilePath as open
 from Common.MultipleWorkspace import MultipleWorkspace as mws
 import uuid
+from CommonDataClass.Exceptions import BadExpression
 
 ## Regular expression used to find out place holders in string template
 gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)
 
 ## Dictionary used to store file time stamp for quick re-access
@@ -1470,103 +1471,123 @@ def AnalyzePcdExpression(Setting):
         FieldList.append(Setting[StartPos:Pos].strip())
         StartPos = Pos + 1
 
     return FieldList
 
+def ParseDevPathValue (Value):
+    pass
+
 def ParseFieldValue (Value):
-  if type(Value) == type(0):
-    return Value, (Value.bit_length() + 7) / 8
-  if type(Value) <> type(''):
-    raise ValueError
-  Value = Value.strip()
-  if Value.startswith('UINT8') and Value.endswith(')'):
-    Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
-    if Size > 1:
-      raise ValueError
+    if type(Value) == type(0):
+        return Value, (Value.bit_length() + 7) / 8
+    if type(Value) <> type(''):
+        raise BadExpression('Type %s is %s' %(Value, type(Value)))
+    Value = Value.strip()
+    if Value.startswith('UINT8') and Value.endswith(')'):
+        Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
+        if Size > 1:
+            raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))
+        return Value, 1
+    if Value.startswith('UINT16') and Value.endswith(')'):
+        Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
+        if Size > 2:
+            raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))
+        return Value, 2
+    if Value.startswith('UINT32') and Value.endswith(')'):
+        Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
+        if Size > 4:
+            raise BadExpression('Value (%s) Size larger than %d' %(Value, Size))
+        return Value, 4
+    if Value.startswith('UINT64') and Value.endswith(')'):
+        Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
+        if Size > 8:
+            raise BadExpression('Value (%s) Size larger than %d' % (Value, Size))
+        return Value, 8
+    if Value.startswith('GUID') and Value.endswith(')'):
+        Value = Value.split('(', 1)[1][:-1].strip()
+        if Value[0] == '{' and Value[-1] == '}':
+            Value = Value[1:-1].strip()
+            Value = Value.split('{', 1)
+            Value = ['%02x' % int(Item, 16) for Item in (Value[0] + Value[1][:-1]).split(',')]
+            if len(Value[0]) != 8:
+                Value[0] = '%08X' % int(Value[0], 16)
+            if len(Value[1]) != 4:
+                Value[1] = '%04X' % int(Value[1], 16)
+            if len(Value[2]) != 4:
+                Value[2] = '%04X' % int(Value[2], 16)
+            Value = '-'.join(Value[0:3]) + '-' + ''.join(Value[3:5]) + '-' + ''.join(Value[5:11])
+        if Value[0] == '"' and Value[-1] == '"':
+            Value = Value[1:-1]
+        try:
+            Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"
+        except ValueError, Message:
+            raise BadExpression('%s' % Message)
+        Value, Size = ParseFieldValue(Value)
+        return Value, 16
+    if Value.startswith('L"') and Value.endswith('"'):
+        # Unicode String
+        List = list(Value[2:-1])
+        List.reverse()
+        Value = 0
+        for Char in List:
+            Value = (Value << 16) | ord(Char)
+        return Value, (len(List) + 1) * 2
+    if Value.startswith('"') and Value.endswith('"'):
+        # ASCII String
+        List = list(Value[1:-1])
+        List.reverse()
+        Value = 0
+        for Char in List:
+            Value = (Value << 8) | ord(Char)
+        return Value, len(List) + 1
+    if Value.startswith("L'") and Value.endswith("'"):
+        # Unicode Character Constant
+        List = list(Value[2:-1])
+        List.reverse()
+        Value = 0
+        for Char in List:
+            Value = (Value << 16) | ord(Char)
+        return Value, len(List) * 2
+    if Value.startswith("'") and Value.endswith("'"):
+        # Character constant
+        List = list(Value[1:-1])
+        List.reverse()
+        Value = 0
+        for Char in List:
+            Value = (Value << 8) | ord(Char)
+        return Value, len(List)
+    if Value.startswith('{') and Value.endswith('}'):
+        # Byte array
+        Value = Value[1:-1]
+        List = [Item.strip() for Item in Value.split(',')]
+        List.reverse()
+        Value = 0
+        RetSize = 0
+        for Item in List:
+            ItemValue, Size = ParseFieldValue(Item)
+            RetSize += Size
+            for I in range(Size):
+                Value = (Value << 8) | ((ItemValue >> 8 * I) & 0xff)
+        return Value, RetSize
+    if Value.startswith('DEVICE_PATH(') and Value.endswith(')'):
+        Value = Value.split('"')[1]
+        return ParseDevPathValue(Value)
+    if Value.lower().startswith('0x'):
+        Value = int(Value, 16)
+        if Value == 0:
+            return 0, 1
+        return Value, (Value.bit_length() + 7) / 8
+    if Value[0].isdigit():
+        Value = int(Value, 10)
+        if Value == 0:
+            return 0, 1
+        return Value, (Value.bit_length() + 7) / 8
+    if Value.lower() == 'true':
+        return 1, 1
+    if Value.lower() == 'false':
+        return 0, 1
     return Value, 1
-  if Value.startswith('UINT16') and Value.endswith(')'):
-    Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
-    if Size > 2:
-      raise ValueError
-    return Value, 2
-  if Value.startswith('UINT32') and Value.endswith(')'):
-    Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
-    if Size > 4:
-      raise ValueError
-    return Value, 4
-  if Value.startswith('UINT64') and Value.endswith(')'):
-    Value, Size = ParseFieldValue(Value.split('(', 1)[1][:-1])
-    if Size > 8:
-      raise ValueError
-    return Value, 8
-  if Value.startswith('GUID') and Value.endswith(')'):
-    Value = Value.split('(', 1)[1][:-1].strip()
-    if Value[0] == '{' and Value[-1] == '}':
-      Value = Value[1:-1].strip()
-      Value = Value.split('{', 1)
-      Value = [Item.strip()[2:] for Item in (Value[0] + Value[1][:-1]).split(',')]
-      Value = '-'.join(Value[0:3]) + '-' + ''.join(Value[3:5]) + '-' + ''.join(Value[5:11])
-    if Value[0] == '"' and Value[-1] == '"':
-      Value = Value[1:-1]
-    Value = "'" + uuid.UUID(Value).get_bytes_le() + "'"
-    Value, Size = ParseFieldValue(Value)
-    return Value, 16
-  if Value.startswith('L"') and Value.endswith('"'):
-    # Unicode String
-    List = list(Value[2:-1])
-    List.reverse()
-    Value = 0
-    for Char in List:
-      Value = (Value << 16) | ord(Char)
-    return Value, (len(List) + 1) * 2
-  if Value.startswith('"') and Value.endswith('"'):
-    # ASCII String
-    List = list(Value[1:-1])
-    List.reverse()
-    Value = 0
-    for Char in List:
-      Value = (Value << 8) | ord(Char)
-    return Value, len(List) + 1
-  if Value.startswith("L'") and Value.endswith("'"):
-    # Unicode Character Constant
-    List = list(Value[2:-1])
-    List.reverse()
-    Value = 0
-    for Char in List:
-      Value = (Value << 16) | ord(Char)
-    return Value, len(List) * 2
-  if Value.startswith("'") and Value.endswith("'"):
-    # Character constant
-    List = list(Value[1:-1])
-    List.reverse()
-    Value = 0
-    for Char in List:
-      Value = (Value << 8) | ord(Char)
-    return Value, len(List)
-  if Value.startswith('{') and Value.endswith('}'):
-    # Byte array
-    Value = Value[1:-1]
-    List = [Item.strip() for Item in Value.split(',')]
-    List.reverse()
-    Value = 0
-    for Item in List:
-      ItemValue, Size = ParseFieldValue(Item)
-      if Size > 1:
-        raise ValueError
-      Value = (Value << 8) | ItemValue
-    return Value, len(List)
-  if Value.lower().startswith('0x'):
-    Value = int(Value, 16)
-    return Value, (Value.bit_length() + 7) / 8
-  if Value[0].isdigit():
-    Value = int(Value, 10)
-    return Value, (Value.bit_length() + 7) / 8
-  if Value.lower() == 'true':
-    return 1, 1
-  if Value.lower() == 'false':
-    return 0, 1
-  return Value, 1
 
 ## AnalyzeDscPcd
 #
 #  Analyze DSC PCD value, since there is no data type info in DSC
 #  This fuction is used to match functions (AnalyzePcdData, AnalyzeHiiPcdData, AnalyzeVpdPcdData) used for retrieving PCD value from database
diff --git a/BaseTools/Source/Python/Workspace/DscBuildData.py b/BaseTools/Source/Python/Workspace/DscBuildData.py
index e422100..05b26a3 100644
--- a/BaseTools/Source/Python/Workspace/DscBuildData.py
+++ b/BaseTools/Source/Python/Workspace/DscBuildData.py
@@ -798,11 +798,11 @@ class DscBuildData(PlatformBuildClassObject):
                 if ModuleFile in self._Modules:
                     continue
                 ModuleData = self._Bdb[ModuleFile, self._Arch, self._Target, self._Toolchain]
                 PkgSet.update(ModuleData.Packages)
 
-            self._DecPcds = GetDeclaredPcd(self, self._Bdb, self._Arch, self._Target, self._Toolchain,PkgSet)
+            self._DecPcds, self._GuidDict = GetDeclaredPcd(self, self._Bdb, self._Arch, self._Target, self._Toolchain,PkgSet)
 
 
         if (PcdCName, TokenSpaceGuid) not in self._DecPcds:
             EdkLogger.error('build', PARSER_ERROR,
                             "Pcd (%s.%s) defined in DSC is not declared in DEC files. Arch: ['%s']" % (TokenSpaceGuid, PcdCName, self._Arch),
@@ -819,10 +819,12 @@ class DscBuildData(PlatformBuildClassObject):
         if ValueList[Index] and PcdType not in [MODEL_PCD_FEATURE_FLAG, MODEL_PCD_FIXED_AT_BUILD]:
             try:
                 ValueList[Index] = ValueExpression(ValueList[Index], GlobalData.gPlatformPcds)(True)
             except WrnExpression, Value:
                 ValueList[Index] = Value.result
+            except BadExpression, Value:
+                EdkLogger.error('Parser', FORMAT_INVALID, Value, File=self.MetaFile, Line=self._LineIndex + 1)
             except EvaluationException, Excpt:
                 if hasattr(Excpt, 'Pcd'):
                     if Excpt.Pcd in GlobalData.gPlatformOtherPcds:
                         EdkLogger.error('Parser', FORMAT_INVALID, "Cannot use this PCD (%s) in an expression as"
                                         " it must be defined in a [PcdsFixedAtBuild] or [PcdsFeatureFlag] section"
@@ -832,15 +834,17 @@ class DscBuildData(PlatformBuildClassObject):
                         EdkLogger.error('Parser', FORMAT_INVALID, "PCD (%s) is not defined in DSC file" % Excpt.Pcd,
                                         File=self.MetaFile, Line=LineNo)
                 else:
                     EdkLogger.error('Parser', FORMAT_INVALID, "Invalid expression: %s" % str(Excpt),
                                     File=self.MetaFile, Line=LineNo)
-            if ValueList[Index] == 'True':
-                ValueList[Index] = '1'
-            elif ValueList[Index] == 'False':
-                ValueList[Index] = '0'
         if ValueList[Index]:
+            DatumType = self._DecPcds[PcdCName, TokenSpaceGuid].DatumType
+            try:
+                ValueList[Index] = ValueExpressionEx(ValueList[Index], DatumType, self._GuidDict)(True)
+            except BadExpression, Value:
+                EdkLogger.error('Parser', FORMAT_INVALID, Value, File=self.MetaFile, Line=LineNo,
+                                ExtraData="PCD [%s.%s] Value \"%s\" " % (TokenSpaceGuid, PcdCName, ValueList[Index]))
             Valid, ErrStr = CheckPcdDatum(self._DecPcds[PcdCName, TokenSpaceGuid].DatumType, ValueList[Index])
             if not Valid:
                 EdkLogger.error('build', FORMAT_INVALID, ErrStr, File=self.MetaFile, Line=LineNo,
                                 ExtraData="%s.%s" % (TokenSpaceGuid, PcdCName))
             if PcdType in (MODEL_PCD_DYNAMIC_DEFAULT, MODEL_PCD_DYNAMIC_EX_DEFAULT):
diff --git a/BaseTools/Source/Python/Workspace/InfBuildData.py b/BaseTools/Source/Python/Workspace/InfBuildData.py
index fd94067..7ea9b56 100644
--- a/BaseTools/Source/Python/Workspace/InfBuildData.py
+++ b/BaseTools/Source/Python/Workspace/InfBuildData.py
@@ -14,10 +14,11 @@
 
 from Common.String import *
 from Common.DataType import *
 from Common.Misc import *
 from types import *
+from MetaFileParser import *
 
 from Workspace.BuildClassObject import ModuleBuildClassObject, LibraryClassObject, PcdClassObject
 ## Module build information from INF file
 #
 #  This class is used to retrieve information stored in database and convert them
@@ -1142,10 +1143,16 @@ class InfBuildData(ModuleBuildClassObject):
                     Pcd.DatumType = PcdInPackage.DatumType
                     Pcd.MaxDatumSize = PcdInPackage.MaxDatumSize
                     Pcd.InfDefaultValue = Pcd.DefaultValue
                     if Pcd.DefaultValue in [None, '']:
                         Pcd.DefaultValue = PcdInPackage.DefaultValue
+                    else:
+                        try:
+                            Pcd.DefaultValue = ValueExpressionEx(Pcd.DefaultValue, Pcd.DatumType, self.Guids)(True)
+                        except BadExpression, Value:
+                            EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(TokenSpaceGuid, PcdRealName, Pcd.DefaultValue, Value),
+                                            File=self.MetaFile, Line=LineNo)
                     break
             else:
                 EdkLogger.error(
                             'build',
                             FORMAT_INVALID,
diff --git a/BaseTools/Source/Python/Workspace/MetaFileParser.py b/BaseTools/Source/Python/Workspace/MetaFileParser.py
index 3038447..b2b0e28 100644
--- a/BaseTools/Source/Python/Workspace/MetaFileParser.py
+++ b/BaseTools/Source/Python/Workspace/MetaFileParser.py
@@ -25,11 +25,11 @@ import Common.EdkLogger as EdkLogger
 import Common.GlobalData as GlobalData
 
 from CommonDataClass.DataClass import *
 from Common.DataType import *
 from Common.String import *
-from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData, AnalyzeDscPcd, AnalyzePcdExpression
+from Common.Misc import GuidStructureStringToGuidString, CheckPcdDatum, PathClass, AnalyzePcdData, AnalyzeDscPcd, AnalyzePcdExpression, ParseFieldValue
 from Common.Expression import *
 from CommonDataClass.Exceptions import *
 from Common.LongFilePathSupport import OpenLongFilePath as open
 
 from MetaFileTable import MetaFileStorage
@@ -180,10 +180,11 @@ class MetaFileParser(object):
         self._Enabled = 0
         self._Finished = False
         self._PostProcessed = False
         # Different version of meta-file has different way to parse.
         self._Version = 0
+        self._GuidDict = {}  # for Parser PCD value {GUID(gTokeSpaceGuidName)}
 
     ## Store the parsed data in table
     def _Store(self, *Args):
         return self._Table.Insert(*Args)
 
@@ -1869,10 +1870,12 @@ class DecParser(MetaFileParser):
                             ExtraData=self._CurrentLine + \
                                       " (<CName> = <GuidValueInCFormat:{8,4,4,{2,2,2,2,2,2,2,2}}>)",
                             File=self.MetaFile, Line=self._LineIndex + 1)
         self._ValueList[0] = TokenList[0]
         self._ValueList[1] = TokenList[1]
+        if self._ValueList[0] not in self._GuidDict:
+            self._GuidDict[self._ValueList[0]] = self._ValueList[1]
 
     ## PCD sections parser
     #
     #   [PcdsFixedAtBuild]
     #   [PcdsPatchableInModule]
@@ -1985,16 +1988,18 @@ class DecParser(MetaFileParser):
             if PcdValue:
                 try:
                     ValueList[0] = ValueExpression(PcdValue, self._AllPcdDict)(True)
                 except WrnExpression, Value:
                     ValueList[0] = Value.result
+                except BadExpression, Value:
+                    EdkLogger.error('Parser', FORMAT_INVALID, Value, File=self.MetaFile, Line=self._LineIndex + 1)
 
-            if ValueList[0] == 'True':
-                ValueList[0] = '1'
-            if ValueList[0] == 'False':
-                ValueList[0] = '0'
-
+            if ValueList[0]:
+                try:
+                    ValueList[0] = ValueExpressionEx(ValueList[0], ValueList[1], self._GuidDict)(True)
+                except BadExpression, Value:
+                    EdkLogger.error('Parser', FORMAT_INVALID, Value, ExtraData=self._CurrentLine, File=self.MetaFile, Line=self._LineIndex + 1)
             # check format of default value against the datum type
             IsValid, Cause = CheckPcdDatum(ValueList[1], ValueList[0])
             if not IsValid:
                 EdkLogger.error('Parser', FORMAT_INVALID, Cause, ExtraData=self._CurrentLine,
                                 File=self.MetaFile, Line=self._LineIndex + 1)
diff --git a/BaseTools/Source/Python/Workspace/WorkspaceCommon.py b/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
index c289b9d..c760e57 100644
--- a/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
+++ b/BaseTools/Source/Python/Workspace/WorkspaceCommon.py
@@ -1,9 +1,9 @@
 ## @file
 # Common routines used by workspace
 #
-# Copyright (c) 2012 - 2016, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2012 - 2017, Intel Corporation. All rights reserved.<BR>
 # This program and the accompanying materials
 # are licensed and made available under the terms and conditions of the BSD License
 # which accompanies this distribution.  The full text of the license may be found at
 # http://opensource.org/licenses/bsd-license.php
 #
@@ -41,28 +41,32 @@ def GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain):
 #  @param BuildDatabase: The database saves all data for all metafiles
 #  @param Arch: Current arch
 #  @param Target: Current target
 #  @param Toolchain: Current toolchain
 #  @retval: A dictionary contains instances of PcdClassObject with key (PcdCName, TokenSpaceGuid)
+#  @retval: A dictionary contains real GUIDs of TokenSpaceGuid
 #
 def GetDeclaredPcd(Platform, BuildDatabase, Arch, Target, Toolchain,additionalPkgs):
     PkgList = GetPackageList(Platform, BuildDatabase, Arch, Target, Toolchain)
     PkgList = set(PkgList)
     PkgList |= additionalPkgs
     DecPcds = {}
+    GuidDict = {}
     for Pkg in PkgList:
+        Guids = Pkg.Guids
+        GuidDict.update(Guids)
         for Pcd in Pkg.Pcds:
             PcdCName = Pcd[0]
             PcdTokenName = Pcd[1]
             if GlobalData.MixedPcd:
                 for PcdItem in GlobalData.MixedPcd.keys():
                     if (PcdCName, PcdTokenName) in GlobalData.MixedPcd[PcdItem]:
                         PcdCName = PcdItem[0]
                         break
             if (PcdCName, PcdTokenName) not in DecPcds.keys():
                 DecPcds[PcdCName, PcdTokenName] = Pkg.Pcds[Pcd]
-    return DecPcds
+    return DecPcds, GuidDict
 
 ## Get all dependent libraries for a module
 #
 #  @param Module: InfBuildData instance
 #  @param Platform: DscBuildData instance
diff --git a/BaseTools/Source/Python/build/BuildReport.py b/BaseTools/Source/Python/build/BuildReport.py
index 75e8ec9..5da9914 100644
--- a/BaseTools/Source/Python/build/BuildReport.py
+++ b/BaseTools/Source/Python/build/BuildReport.py
@@ -43,10 +43,11 @@ import Common.GlobalData as GlobalData
 from AutoGen.AutoGen import ModuleAutoGen
 from Common.Misc import PathClass
 from Common.String import NormPath
 from Common.DataType import *
 import collections
+from Common.Expression import ValueExpressionEx
 
 ## Pattern to extract contents in EDK DXS files
 gDxsDependencyPattern = re.compile(r"DEPENDENCY_START(.+)DEPENDENCY_END", re.DOTALL)
 
 ## Pattern to find total FV total size, occupied size in flash report intermediate file
@@ -847,12 +848,15 @@ class PcdReport(object):
 
         #
         # Collect PCD DEC default value.
         #
         self.DecPcdDefault = {}
+        self._GuidDict = {}
         for Pa in Wa.AutoGenObjectList:
             for Package in Pa.PackageList:
+                Guids = Package.Guids
+                self._GuidDict.update(Guids)
                 for (TokenCName, TokenSpaceGuidCName, DecType) in Package.Pcds:
                     DecDefaultValue = Package.Pcds[TokenCName, TokenSpaceGuidCName, DecType].DefaultValue
                     self.DecPcdDefault.setdefault((TokenCName, TokenSpaceGuidCName, DecType), DecDefaultValue)
         #
         # Collect PCDs defined in DSC common section
@@ -941,10 +945,12 @@ class PcdReport(object):
                     #
                     DecDefaultValue = self.DecPcdDefault.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, DecType))
                     DscDefaultValue = self.DscPcdDefault.get((Pcd.TokenCName, Pcd.TokenSpaceGuidCName))
                     DscDefaultValBak= DscDefaultValue
                     DscDefaultValue = self.FdfPcdSet.get((Pcd.TokenCName, Key), DscDefaultValue)
+                    if DscDefaultValue:
+                        DscDefaultValue = ValueExpressionEx(DscDefaultValue, Pcd.DatumType, self._GuidDict)(True)
                     InfDefaultValue = None
                     
                     PcdValue = DecDefaultValue
                     if DscDefaultValue:
                         PcdValue = DscDefaultValue
-- 
2.6.1.windows.1



  reply	other threads:[~2017-12-26 16:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-26 16:40 [Patch 0/2] BaseTools: Add Flexible PCD Value support Yonghong Zhu
2017-12-26 16:40 ` Yonghong Zhu [this message]
2017-12-26 16:40 ` [Patch 2/2] BaseTools: Add DevicePath support for PCD values Yonghong Zhu
2017-12-27  2:40   ` Gao, Liming

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1514306413-5880-2-git-send-email-yonghong.zhu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox