From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=134.134.136.20; helo=mga02.intel.com; envelope-from=yonghong.zhu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) (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 BEFDF21F6A6E1 for ; Fri, 23 Feb 2018 23:15:15 -0800 (PST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 23 Feb 2018 23:21:16 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.47,386,1515484800"; d="scan'208";a="29441001" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by FMSMGA003.fm.intel.com with ESMTP; 23 Feb 2018 23:21:16 -0800 Received: from fmsmsx115.amr.corp.intel.com (10.18.116.19) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 23 Feb 2018 23:21:15 -0800 Received: from shsmsx152.ccr.corp.intel.com (10.239.6.52) by fmsmsx115.amr.corp.intel.com (10.18.116.19) with Microsoft SMTP Server (TLS) id 14.3.319.2; Fri, 23 Feb 2018 23:21:15 -0800 Received: from shsmsx103.ccr.corp.intel.com ([169.254.4.116]) by SHSMSX152.ccr.corp.intel.com ([169.254.6.130]) with mapi id 14.03.0319.002; Sat, 24 Feb 2018 15:21:13 +0800 From: "Zhu, Yonghong" To: "Feng, YunhuaX" , "edk2-devel@lists.01.org" CC: "Gao, Liming" , "Zhu, Yonghong" Thread-Topic: [PATCH] BaseTools: Update ValueExpressionEx for flexible PCD Thread-Index: AdOsnBUsn+duQDfEQI+eCHmr4Gh8MwAo+aQA Date: Sat, 24 Feb 2018 07:21:12 +0000 Message-ID: References: <47C64442C08CCD4089DC43B6B5E46BC482B186@shsmsx102.ccr.corp.intel.com> In-Reply-To: <47C64442C08CCD4089DC43B6B5E46BC482B186@shsmsx102.ccr.corp.intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiYzMxZTVjNjgtZmFjNi00ODkzLTg3NTktZTM4OTgyZDgzZWE3IiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX05UIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE2LjUuOS4zIiwiVHJ1c3RlZExhYmVsSGFzaCI6IjU4aFNoVlJvT29UWG5ieHhOXC9TV3p4K1wvWFRNNWIwVzduVzgzUmZIek1XVT0ifQ== x-ctpclassification: CTP_NT dlp-product: dlpe-windows dlp-version: 11.0.0.116 dlp-reaction: no-action x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Subject: Re: [PATCH] BaseTools: Update ValueExpressionEx for flexible PCD 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: , X-List-Received-Date: Sat, 24 Feb 2018 07:15:16 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Yonghong Zhu =20 Best Regards, Zhu Yonghong -----Original Message----- From: Feng, YunhuaX=20 Sent: Friday, February 23, 2018 7:48 PM To: edk2-devel@lists.01.org Cc: Zhu, Yonghong ; Gao, Liming Subject: [PATCH] BaseTools: Update ValueExpressionEx for flexible PCD 1. Byte array number should less than 0xFF. 2. Add SplitPcdValueString for PCD split Cc: Liming Gao Cc: Yonghong Zhu Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Yunhua Feng --- BaseTools/Source/Python/Common/Expression.py | 205 +++++++++++++++++------= ---- 1 file changed, 130 insertions(+), 75 deletions(-) diff --git a/BaseTools/Source/Python/Common/Expression.py b/BaseTools/Sourc= e/Python/Common/Expression.py index 28320d78a9..edb0a60de6 100644 --- a/BaseTools/Source/Python/Common/Expression.py +++ b/BaseTools/Source/Python/Common/Expression.py @@ -66,10 +66,48 @@ 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() or " " + # or ' ' or L' ' or L" " + Str =3D String + RetList =3D [] + InParenthesis =3D 0 + InSingleQuote =3D False + InDoubleQuote =3D False + 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 '"' and not InSingleQuote: + if String[i-1] !=3D '\\': + InDoubleQuote =3D not InDoubleQuote + if ch =3D=3D "'" and not InDoubleQuote: + if String[i-1] !=3D '\\': + InSingleQuote =3D not InSingleQuote + if ch =3D=3D ',': + if InParenthesis or InSingleQuote or InDoubleQuote: + Item +=3D String[i] + continue + elif Item: + RetList.append(Item) + Item =3D '' + continue + Item +=3D String[i] + if InSingleQuote or InDoubleQuote or InParenthesis: + raise BadExpression(ERR_STRING_TOKEN % Item) + 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,28 +769,46 @@ 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 BadExpression, Value: + raise BadExpression(Value) + except ValueError: + pass ItemValue, ItemSize =3D ParseFieldValue(Item) else: ItemValue =3D ParseFieldValue(Item)[0] =20 if type(ItemValue) =3D=3D type(''): @@ -792,88 +848,85 @@ 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+)\)') + LabelOffset =3D 0 + for Index, Item in enumerate(PcdValueList): + # compute byte offset of every LABEL + 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(LabelOffs= et) 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+)\)') - LabelOffset =3D 0 - for Index, Item in enumerate(ListItem): - # compute byte offset of every LABEL - Item =3D Item.strip() + if Item.startswith('UINT8'): + LabelOffset =3D LabelOffset + 1 + elif Item.startswith('UINT16'): + LabelOffset =3D LabelOffset + 2 + elif Item.startswith('UINT32'): + LabelOffset =3D LabelOffset + 4 + elif Item.startswith('UINT64'): + LabelOffset =3D LabelOffset + 8 + else: try: - LabelList =3D ReLabel.findall(Item) - for Label in LabelList: - if Label not in LabelDict.keys(): - LabelDict[Label] =3D str(Label= Offset) - Item =3D ReLabel.sub('', Item) - except: - pass - if Item.startswith('UINT8'): - LabelOffset =3D LabelOffset + 1 - elif Item.startswith('UINT16'): - LabelOffset =3D LabelOffset + 2 - elif Item.startswith('UINT32'): - LabelOffset =3D LabelOffset + 4 - elif Item.startswith('UINT64'): - LabelOffset =3D LabelOffset + 8 - else: ItemValue, ItemSize =3D ParseFieldValu= e(Item) LabelOffset =3D LabelOffset + ItemSize - - 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 + LabelOffset =3D LabelOffset + 1 + + for Index, Item in enumerate(PcdValueList): + # for LABEL parse + Item =3D Item.strip() + try: + 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' %= 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' % 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'): @@ -892,20 +945,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