From nobody Sat May 4 19:04:59 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1519310308001613.4055747253459; Thu, 22 Feb 2018 06:38:28 -0800 (PST) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id A9A72222F4E3F; Thu, 22 Feb 2018 06:32:25 -0800 (PST) Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0ED162034D8CA for ; Thu, 22 Feb 2018 06:32:23 -0800 (PST) Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Feb 2018 06:38:23 -0800 Received: from fmsmsx103.amr.corp.intel.com ([10.18.124.201]) by fmsmga007.fm.intel.com with ESMTP; 22 Feb 2018 06:38:23 -0800 Received: from shsmsx152.ccr.corp.intel.com (10.239.6.52) by FMSMSX103.amr.corp.intel.com (10.18.124.201) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 22 Feb 2018 06:38:23 -0800 Received: from shsmsx102.ccr.corp.intel.com ([169.254.2.124]) by SHSMSX152.ccr.corp.intel.com ([169.254.6.130]) with mapi id 14.03.0319.002; Thu, 22 Feb 2018 22:38:21 +0800 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.24; helo=mga09.intel.com; envelope-from=yunhuax.feng@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,377,1515484800"; d="dat'59?scan'59,208,59";a="19466836" From: "Feng, YunhuaX" To: "edk2-devel@lists.01.org" Thread-Topic: [PATCH] BaseTools: Fix parse flexible PCD value bugs Thread-Index: AdOr6shdPpW4WnEOTDe1Tx8PY7hKxA== Date: Thu, 22 Feb 2018 14:38:20 +0000 Message-ID: <47C64442C08CCD4089DC43B6B5E46BC482B00B@shsmsx102.ccr.corp.intel.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: <47C64442C08CCD4089DC43B6B5E46BC482B00B@shsmsx102.ccr.corp.intel.com> x-originating-ip: [10.239.127.40] MIME-Version: 1.0 X-Content-Filtered-By: Mailman/MimeDel 2.1.23 Subject: [edk2] [PATCH] BaseTools: Fix parse flexible PCD value bugs X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Gao, Liming" Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng --- 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/Sourc= e/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 =20 +def SplitPcdValueString(String): + # There might be escaped comma in GUID() or DEVICE_PATH() + Str =3D String + RetList =3D [] + InParenthesis =3D 0 + Item =3D '' + for i, ch in enumerate(Str): + if ch =3D=3D '(': + InParenthesis +=3D 1 + if ch =3D=3D ')': + if InParenthesis: + InParenthesis -=3D 1 + else: + raise BadExpression(ERR_STRING_TOKEN % Item) + if ch =3D=3D ',': + if InParenthesis: + Item +=3D String[i] + continue + elif Item: + RetList.append(Item) + Item =3D '' + continue + Item +=3D String[i] + if Item: + RetList.append(Item) + return RetList + ## ReplaceExprMacro # def ReplaceExprMacro(String, Macros, ExceptionList =3D None): StrList =3D SplitString(String) for i, String in enumerate(StrList): @@ -731,32 +758,50 @@ class ValueExpressionEx(ValueExpression): PcdValue =3D Value.result except BadExpression, Value: if self.PcdType in ['UINT8', 'UINT16', 'UINT32', 'UINT64', 'BO= OLEAN']: PcdValue =3D PcdValue.strip() if type(PcdValue) =3D=3D type('') and PcdValue.startswith(= '{') and PcdValue.endswith('}'): - PcdValue =3D PcdValue[1:-1].split(',') + PcdValue =3D SplitPcdValueString(PcdValue[1:-1]) if type(PcdValue) =3D=3D type([]): TmpValue =3D 0 Size =3D 0 + ValueType =3D '' for Item in PcdValue: + Item =3D Item.strip() if Item.startswith('UINT8'): ItemSize =3D 1 - if Item.startswith('UINT16'): + ValueType =3D 'UINT8' + elif Item.startswith('UINT16'): ItemSize =3D 2 + ValueType =3D 'UINT16' elif Item.startswith('UINT32'): ItemSize =3D 4 + ValueType =3D 'UINT32' elif Item.startswith('UINT64'): ItemSize =3D 8 + ValueType =3D 'UINT64' + elif Item.startswith('"') or Item.startswith("'") = or Item.startswith('L'): + ItemSize =3D 0 + ValueType =3D 'VOID*' else: ItemSize =3D 0 - Item =3D ValueExpressionEx(Item, self.PcdType, sel= f._Symb)(True) + ValueType =3D 'UINT8' + + Item =3D ValueExpressionEx(Item, ValueType, self._= Symb)(True) =20 if ItemSize =3D=3D 0: + try: + tmpValue =3D int(Item, 16) if Item.upper(= ).startswith('0X') else int(Item, 0) + if tmpValue > 255: + raise BadExpression("Byte array numbe= r %s should less than 0xFF." % Item) + except ValueError: + pass + except BadExpression, Value: + raise BadExpression(Value) ItemValue, ItemSize =3D ParseFieldValue(Item) else: ItemValue =3D ParseFieldValue(Item)[0] - if type(ItemValue) =3D=3D type(''): ItemValue =3D int(ItemValue, 16) if ItemValue.= startswith('0x') else int(ItemValue) =20 TmpValue =3D (ItemValue << (Size * 8)) | TmpValue Size =3D 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 =3D '{' + ', '.join(TmpList) + '}' except: if PcdValue.strip().startswith('{'): - PcdValue =3D PcdValue.strip()[1:-1].strip() - Size =3D 0 - ValueStr =3D '' - TokenSpaceGuidName =3D '' - if PcdValue.startswith('GUID') and PcdValue.endswi= th(')'): + PcdValueList =3D SplitPcdValueString(PcdValue.stri= p()[1:-1]) + LabelDict =3D {} + NewPcdValueList =3D [] + ReLabel =3D re.compile('LABEL\((\w+)\)') + ReOffset =3D re.compile('OFFSET_OF\((\w+)\)') + for Index, Item in enumerate(PcdValueList): + # for LABEL parse + Item =3D Item.strip() try: - TokenSpaceGuidName =3D re.search('GUID\((\= w+)\)', PcdValue).group(1) + LabelList =3D ReLabel.findall(Item) + for Label in LabelList: + if Label not in LabelDict.keys(): + LabelDict[Label] =3D str(Index) + Item =3D ReLabel.sub('', Item) except: pass - if TokenSpaceGuidName and TokenSpaceGuidName i= n self._Symb: - PcdValue =3D 'GUID(' + self._Symb[TokenSpa= ceGuidName] + ')' - elif TokenSpaceGuidName: - raise BadExpression('%s not found in DEC f= ile' % TokenSpaceGuidName) - - ListItem, Size =3D ParseFieldValue(PcdValue) - elif PcdValue.startswith('DEVICE_PATH') and PcdVal= ue.endswith(')'): - ListItem, Size =3D ParseFieldValue(PcdValue) - else: - ListItem =3D PcdValue.split(',') - - if type(ListItem) =3D=3D type(0) or type(ListItem)= =3D=3D type(0L): - for Index in range(0, Size): - ValueStr +=3D '0x%02X' % (int(ListItem) & = 255) - ListItem >>=3D 8 - ValueStr +=3D ', ' - PcdValue =3D '{' + ValueStr[:-2] + '}' - elif type(ListItem) =3D=3D type(''): - if ListItem.startswith('{') and ListItem.endsw= ith('}'): - PcdValue =3D ListItem - else: - LabelDict =3D {} - ReLabel =3D re.compile('LABEL\((\w+)\)') - ReOffset =3D re.compile('OFFSET_OF\((\w+)\)') - for Index, Item in enumerate(ListItem): - # for LABEL parse - Item =3D Item.strip() - try: - LabelList =3D ReLabel.findall(Item) - for Label in LabelList: - if Label not in LabelDict.keys(): - LabelDict[Label] =3D str(Index) - Item =3D ReLabel.sub('', Item) - except: - pass + try: + OffsetList =3D ReOffset.findall(Item) + except: + pass + for Offset in OffsetList: + if Offset in LabelDict.keys(): + Re =3D re.compile('OFFSET_OF\(%s\)' % = Offset) + Item =3D Re.sub(LabelDict[Offset], Ite= m) + else: + raise BadExpression('%s not defined be= fore use' % Offset) + NewPcdValueList.append(Item) + AllPcdValueList =3D [] + for Item in NewPcdValueList: + Size =3D 0 + ValueStr =3D '' + TokenSpaceGuidName =3D '' + if Item.startswith('GUID') and Item.endswith('= )'): try: - OffsetList =3D ReOffset.findall(Item) + TokenSpaceGuidName =3D re.search('GUID= \((\w+)\)', Item).group(1) except: pass - for Offset in OffsetList: - if Offset in LabelDict.keys(): - Re =3D re.compile('OFFSET_OF\(%s\)= '% Offset) - Item =3D Re.sub(LabelDict[Offset],= Item) - else: - raise BadExpression('%s not define= d before use' % Offset) + if TokenSpaceGuidName and TokenSpaceGuidNa= me in self._Symb: + Item =3D 'GUID(' + self._Symb[TokenSpa= ceGuidName] + ')' + elif TokenSpaceGuidName: + raise BadExpression('%s not found in D= EC file' % TokenSpaceGuidName) + Item, Size =3D ParseFieldValue(Item) + for Index in range(0, Size): + ValueStr =3D '0x%02X' % (int(Item) & 2= 55) + Item >>=3D 8 + AllPcdValueList.append(ValueStr) + continue + elif Item.startswith('DEVICE_PATH') and Item.e= ndswith(')'): + Item, Size =3D ParseFieldValue(Item) + AllPcdValueList.append(Item[1:-1]) + continue + else: ValueType =3D "" if Item.startswith('UINT8'): ItemSize =3D 1 ValueType =3D "UINT8" elif Item.startswith('UINT16'): @@ -868,20 +911,22 @@ class ValueExpressionEx(ValueExpression): else: TmpValue =3D ValueExpressionEx(Item, s= elf.PcdType, self._Symb)(True) Item =3D '0x%x' % TmpValue if type(TmpValu= e) !=3D type('') else TmpValue if ItemSize =3D=3D 0: ItemValue, ItemSize =3D ParseFieldValu= e(Item) + if not (Item.startswith('"') or Item.s= tartswith('L') or Item.startswith('{')) and ItemSize > 1: + raise BadExpression("Byte array n= umber %s should less than 0xFF." % Item) else: ItemValue =3D ParseFieldValue(Item)[0] for I in range(0, ItemSize): - ValueStr +=3D '0x%02X' % (int(ItemValu= e) & 255) + ValueStr =3D '0x%02X' % (int(ItemValue= ) & 255) ItemValue >>=3D 8 - ValueStr +=3D ', ' + AllPcdValueList.append(ValueStr) Size +=3D ItemSize =20 - if Size > 0: - PcdValue =3D '{' + ValueStr[:-2] + '}' + if Size > 0: + PcdValue =3D '{' + ','.join(AllPcdValueList) += '}' else: raise BadExpression("Type: %s, Value: %s, %s"%(se= lf.PcdType, PcdValue, Value)) =20 if PcdValue =3D=3D 'True': PcdValue =3D '1' --=20 2.12.2.windows.2 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel