[edk2] [PATCH] BaseTools: Fix parse flexible PCD value bugs

Feng, YunhuaX posted 1 patch 6 years, 10 months ago
Failed in applying to current master (apply log)
BaseTools/Source/Python/Common/Expression.py | 157 +++++++++++++++++----------
1 file changed, 101 insertions(+), 56 deletions(-)
[edk2] [PATCH] BaseTools: Fix parse flexible PCD value bugs
Posted by Feng, YunhuaX 6 years, 10 months ago
1. UINT64 Value {"A", "B"},  the expect value 0x420041.
2. Byte  array number should less than 0xFF.
  {256, 0xff} will report error because of 256 larger than 0xFF
3. comma in GUID() and DEVICE_PATH() split issue

Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
---
 BaseTools/Source/Python/Common/Expression.py | 157 +++++++++++++++++----------
 1 file changed, 101 insertions(+), 56 deletions(-)

diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Source/Python/Common/Expression.py
index 74d1b08f76..ed86b21e54 100644
--- a/BaseTools/Source/Python/Common/Expression.py
+++ b/BaseTools/Source/Python/Common/Expression.py
@@ -66,10 +66,37 @@ def SplitString(String):
         raise BadExpression(ERR_STRING_TOKEN % Item)
     if Item:
         RetList.append(Item)
     return RetList
 
+def SplitPcdValueString(String):
+    # There might be escaped comma in GUID() or DEVICE_PATH()
+    Str = String
+    RetList = []
+    InParenthesis = 0
+    Item = ''
+    for i, ch in enumerate(Str):
+        if ch == '(':
+            InParenthesis += 1
+        if ch == ')':
+            if InParenthesis:
+                InParenthesis -= 1
+            else:
+                raise BadExpression(ERR_STRING_TOKEN % Item)
+        if ch == ',':
+            if InParenthesis:
+                Item += String[i]
+                continue
+            elif Item:
+                RetList.append(Item)
+                Item = ''
+            continue
+        Item += String[i]
+    if Item:
+        RetList.append(Item)
+    return RetList
+
 ## ReplaceExprMacro
 #
 def ReplaceExprMacro(String, Macros, ExceptionList = None):
     StrList = SplitString(String)
     for i, String in enumerate(StrList):
@@ -731,32 +758,50 @@ class ValueExpressionEx(ValueExpression):
             PcdValue = Value.result
         except BadExpression, Value:
             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(',')
+                    PcdValue = SplitPcdValueString(PcdValue[1:-1])
                 if type(PcdValue) == type([]):
                     TmpValue = 0
                     Size = 0
+                    ValueType = ''
                     for Item in PcdValue:
+                        Item = Item.strip()
                         if Item.startswith('UINT8'):
                             ItemSize = 1
-                        if Item.startswith('UINT16'):
+                            ValueType = 'UINT8'
+                        elif Item.startswith('UINT16'):
                             ItemSize = 2
+                            ValueType = 'UINT16'
                         elif Item.startswith('UINT32'):
                             ItemSize = 4
+                            ValueType = 'UINT32'
                         elif Item.startswith('UINT64'):
                             ItemSize = 8
+                            ValueType = 'UINT64'
+                        elif Item.startswith('"') or Item.startswith("'") or Item.startswith('L'):
+                            ItemSize = 0
+                            ValueType = 'VOID*'
                         else:
                             ItemSize = 0
-                        Item = ValueExpressionEx(Item, self.PcdType, self._Symb)(True)
+                            ValueType = 'UINT8'
+
+                        Item = ValueExpressionEx(Item, ValueType, self._Symb)(True)
 
                         if ItemSize == 0:
+                            try:
+                                tmpValue = int(Item, 16)  if Item.upper().startswith('0X') else int(Item, 0)
+                                if tmpValue > 255:
+                                    raise BadExpression("Byte  array number %s should less than 0xFF." % Item)
+                            except ValueError:
+                                pass
+                            except BadExpression, Value:
+                                raise BadExpression(Value)
                             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
@@ -792,64 +837,62 @@ class ValueExpressionEx(ValueExpression):
                         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(')'):
+                        PcdValueList = SplitPcdValueString(PcdValue.strip()[1:-1])
+                        LabelDict = {}
+                        NewPcdValueList = []
+                        ReLabel = re.compile('LABEL\((\w+)\)')
+                        ReOffset = re.compile('OFFSET_OF\((\w+)\)')
+                        for Index, Item in enumerate(PcdValueList):
+                            # for LABEL parse
+                            Item = Item.strip()
                             try:
-                                TokenSpaceGuidName = re.search('GUID\((\w+)\)', PcdValue).group(1)
+                                LabelList = ReLabel.findall(Item)
+                                for Label in LabelList:
+                                    if Label not in LabelDict.keys():
+                                        LabelDict[Label] = str(Index)
+                                Item = ReLabel.sub('', Item)
                             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)
+                            NewPcdValueList.append(Item)
+                        AllPcdValueList = []
+                        for Item in NewPcdValueList:
+                            Size = 0
+                            ValueStr = ''
+                            TokenSpaceGuidName = ''
+                            if Item.startswith('GUID') and Item.endswith(')'):
                                 try:
-                                    OffsetList = ReOffset.findall(Item)
+                                    TokenSpaceGuidName = re.search('GUID\((\w+)\)', Item).group(1)
                                 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 TokenSpaceGuidName and TokenSpaceGuidName in self._Symb:
+                                    Item = 'GUID(' + self._Symb[TokenSpaceGuidName] + ')'
+                                elif TokenSpaceGuidName:
+                                    raise BadExpression('%s not found in DEC file' % TokenSpaceGuidName)
+                                Item, Size = ParseFieldValue(Item)
+                                for Index in range(0, Size):
+                                    ValueStr = '0x%02X' % (int(Item) & 255)
+                                    Item >>= 8
+                                    AllPcdValueList.append(ValueStr)
+                                continue
+                            elif Item.startswith('DEVICE_PATH') and Item.endswith(')'):
+                                Item, Size = ParseFieldValue(Item)
+                                AllPcdValueList.append(Item[1:-1])
+                                continue
+                            else:
                                 ValueType = ""
                                 if Item.startswith('UINT8'):
                                     ItemSize = 1
                                     ValueType = "UINT8"
                                 elif Item.startswith('UINT16'):
@@ -868,20 +911,22 @@ class ValueExpressionEx(ValueExpression):
                                 else:
                                     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)
+                                    if not (Item.startswith('"') or Item.startswith('L') or Item.startswith('{')) and ItemSize > 1:
+                                        raise BadExpression("Byte  array number %s should less than 0xFF." % Item)
                                 else:
                                     ItemValue = ParseFieldValue(Item)[0]
                                 for I in range(0, ItemSize):
-                                    ValueStr += '0x%02X' % (int(ItemValue) & 255)
+                                    ValueStr = '0x%02X' % (int(ItemValue) & 255)
                                     ItemValue >>= 8
-                                    ValueStr += ', '
+                                    AllPcdValueList.append(ValueStr)
                                 Size += ItemSize
 
-                            if Size > 0:
-                                PcdValue = '{' + ValueStr[:-2] + '}'
+                        if Size > 0:
+                            PcdValue = '{' + ','.join(AllPcdValueList) + '}'
                     else:
                         raise  BadExpression("Type: %s, Value: %s, %s"%(self.PcdType, PcdValue, Value))
 
         if PcdValue == 'True':
             PcdValue = '1'
-- 
2.12.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel