From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) (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 C34DA81E48 for ; Thu, 10 Nov 2016 08:10:45 -0800 (PST) Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga102.fm.intel.com with ESMTP; 10 Nov 2016 08:10:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,619,1473145200"; d="scan'208";a="900013682" Received: from orsmsx102.amr.corp.intel.com ([10.22.225.129]) by orsmga003.jf.intel.com with ESMTP; 10 Nov 2016 08:10:48 -0800 Received: from orsmsx112.amr.corp.intel.com (10.22.240.13) by ORSMSX102.amr.corp.intel.com (10.22.225.129) with Microsoft SMTP Server (TLS) id 14.3.248.2; Thu, 10 Nov 2016 08:10:48 -0800 Received: from orsmsx113.amr.corp.intel.com ([169.254.9.250]) by ORSMSX112.amr.corp.intel.com ([169.254.3.58]) with mapi id 14.03.0248.002; Thu, 10 Nov 2016 08:10:48 -0800 From: "Kinney, Michael D" To: "Gao, Liming" , "edk2-devel@lists.01.org" , "Kinney, Michael D" Thread-Topic: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility Thread-Index: AQHSOhozimV2HRUFKkKpDFLbJBkveaDS0a0A//+SnfA= Date: Thu, 10 Nov 2016 16:10:48 +0000 Message-ID: References: <1478648717-20716-1-git-send-email-michael.d.kinney@intel.com> <4A89E2EF3DFEDB4C8BFDE51014F606A14B4B076D@shsmsx102.ccr.corp.intel.com> In-Reply-To: <4A89E2EF3DFEDB4C8BFDE51014F606A14B4B076D@shsmsx102.ccr.corp.intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ctpclassification: CTP_IC x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiOTk5MjExMzEtMDBiYy00OGU4LTg4ZjQtNWNjZGI0OTY4OTAyIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE1LjkuNi42IiwiVHJ1c3RlZExhYmVsSGFzaCI6InJUdVh1TWhLZm93M3g0RVBvdVwvQ1kzd3k0dklIMytvYjRubFwveTZmTmxrTT0ifQ== x-originating-ip: [10.22.254.138] MIME-Version: 1.0 Subject: Re: [Patch] BaseTools/Scripts: Add BinToPcd utility X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 10 Nov 2016 16:10:46 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Liming, This utility would not used as part of a build. I put it in BaseTools/Scripts because it is intended to be a helper tool for developers. I have been writing the Wiki on the Capsule=20 Based System Firmware Update and there are development steps in that=20 Wiki that will be simpler if this utility was available, so I plan on demonstrating real use of this utility in that Wiki. Will that address your feedback? The append concept is very complex because this utility can generate PCD statements for all the supported DSC PCD section types. So we=20 can not append 2 PCDs with different types. I prefer to do one PCD at a time and developer can choose to combine or not into one file. Thanks, Mike > -----Original Message----- > From: Gao, Liming > Sent: Thursday, November 10, 2016 6:37 AM > To: Kinney, Michael D ; edk2-devel@lists.01.o= rg > Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility >=20 > Mike: > I have two comments. > 1) Could you also provide the patch to use this utility in edk2 platform,= such as > quark platform? > 2) Suggest to support append mode as one option. If so, we can use this u= tility > to dump more one PCD values into one file. >=20 > Thanks > Liming > -----Original Message----- > From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Mi= chael > Kinney > Sent: Wednesday, November 9, 2016 7:45 AM > To: edk2-devel@lists.01.org > Cc: Gao, Liming > Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility >=20 > Add a utility that converts a binary file into a VOID* PCD value > or a full DSC file VOID* PCD statement with support for all the > DSC supported PCD sections. >=20 > usage: BinToPcd [-h] [--version] -i INPUTFILE [-o OUTPUTFILE] [-p PCDNAME= ] > [-t {VPD,HII}] [-m MAXSIZE] [-f OFFSET] [-n VARIABLENAME] > [-g VARIABLEGUID] [-v] [-q] [--debug [0-9]] >=20 > Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statemen= t. > Copyright (c) 2016, Intel Corporation. All rights reserved. >=20 > optional arguments: > -h, --help show this help message and exit > --version show program's version number and exit > -i INPUTFILE, --input INPUTFILE > Input binary filename > -o OUTPUTFILE, --output OUTPUTFILE > Output filename for PCD value or PCD statement > -p PCDNAME, --pcd PCDNAME > Name of the PCD in the form > . > -t {VPD,HII}, --type {VPD,HII} > PCD statement type (HII or VPD). Default is standard. > -m MAXSIZE, --max-size MAXSIZE > Maximum size of the PCD. Only used with --type VPD. > -f OFFSET, --offset OFFSET > VPD offset if --type is VPD. UEFI Variable offset if > --type is HII. > -n VARIABLENAME, --variable-name VARIABLENAME > UEFI variable name. Only used with --type HII. > -g VARIABLEGUID, --variable-guid VARIABLEGUID > UEFI variable GUID C name. Only used with --type HII. > -v, --verbose Increase output messages > -q, --quiet Reduce output messages > --debug [0-9] Set debug level >=20 > This utility can be used in PCD value mode to convert a binary > file into a string that can then be copied into the PCD value field > of a VOID* PCD. The following is an example of PCD value mode on > an 8 byte test.bin file. >=20 > BinToPcd.py -i test.bin >=20 > {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a} >=20 > The DSC file VOID* PCD statement mode can be used to generate a > complete PCD statement for the PCD section types that a DSC file > supports: >=20 > [PcdsFixedAtBuild] > [PcdsPatchableInModule] > [PcdsDynamicDefault] > [PcdsDynamicExDefault] > [PcdsDynamicVpd] > [PcdsDynamicExVpd] > [PcdsDynamicHii] > [PcdsDynamicExHii] >=20 > The PCD statement mode is useful when combined with a !include > statement in a DSC file. BinToPcd.py can be used to convert a > binary file to a PCD statement in an output file, and that output > file can be included into a DSC file in the matching PCD section > to set the value of the PCD to the value from the binary file > without having to copy the value into the DSC file. Updates can be > made to the included file without editing the DSC file. Some > example use cases are the setting the public key PCDs such as: >=20 > gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer > gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer >=20 > The following example converts a public key binary file to a > [PcdsFixedAtBuild] compatible PCD statement: >=20 > BinToPcd.py -i PublicKey.bin -o PublicKey.pcd > --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid >=20 > The PublicKey.pcd output file contains a single line: >=20 > gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...} >=20 > A DSC file can be updated to include the PublicKey.pcd file: >=20 > [PcdsFixedAtBuild] > !include PublicKey.pcd >=20 > VPD examples: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > BinToPcd.py -i test.bin -p Guid.Token -t VPD > Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a} >=20 > BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20 > Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a} >=20 > BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10 > Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a} >=20 > BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20 -m 10 > Guid.Name|20|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a} >=20 > HII examples: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName > Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c} >=20 > BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName -f 8 > Guid.Name|L"VarName"|VarGuid|8|{0x48, 0x65, 0x6c, 0x6c} >=20 > Cc: Yonghong Zhu > Cc: Liming Gao > Contributed-under: TianoCore Contribution Agreement 1.0 > Signed-off-by: Michael Kinney > --- > BaseTools/Scripts/BinToPcd.py | 179 ++++++++++++++++++++++++++++++++++++= ++++++ > 1 file changed, 179 insertions(+) > create mode 100644 BaseTools/Scripts/BinToPcd.py >=20 > diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.p= y > new file mode 100644 > index 0000000..3cb8000 > --- /dev/null > +++ b/BaseTools/Scripts/BinToPcd.py > @@ -0,0 +1,179 @@ > +## @file > +# Convert a binary file to a VOID* PCD value or DSC file VOID* PCD state= ment. > +# > +# Copyright (c) 2016, Intel Corporation. All rights reserved.
> +# 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 > +# > +# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, > +# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR I= MPLIED. > +# > + > +''' > +BinToPcd > +''' > + > +import sys > +import argparse > +import re > + > +# > +# Globals for help information > +# > +__prog__ =3D 'BinToPcd' > +__version__ =3D '%s Version %s' % (__prog__, '0.9 ') > +__copyright__ =3D 'Copyright (c) 2016, Intel Corporation. All rights r= eserved.' > +__description__ =3D 'Convert a binary file to a VOID* PCD value or DSC f= ile VOID* > PCD statement.\n' > + > +if __name__ =3D=3D '__main__': > + def ValidateUnsignedInteger (Argument): > + try: > + Value =3D int (Argument, 0) > + except: > + Message =3D '%s is not a valid integer value.' % (Argument) > + raise argparse.ArgumentTypeError(Message) > + if Value < 0: > + Message =3D '%s is a negative value.' % (Argument) > + raise argparse.ArgumentTypeError(Message) > + return Value > + > + def ValidatePcdName (Argument): > + if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Ar= gument) > <> ['','']: > + Message =3D '%s is not in the form .' % > (Argument) > + raise argparse.ArgumentTypeError(Message) > + return Argument > + > + def ValidateGuidName (Argument): > + if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']: > + Message =3D '%s is not a valid GUID C name' % (Argument) > + raise argparse.ArgumentTypeError(Message) > + return Argument > + > + # > + # Create command line argument parser object > + # > + parser =3D argparse.ArgumentParser(prog =3D __prog__, version =3D __ve= rsion__, > + description =3D __description__ + > __copyright__, > + conflict_handler =3D 'resolve') > + parser.add_argument("-i", "--input", dest =3D 'InputFile', type =3D > argparse.FileType('rb'), > + help =3D "Input binary filename", required =3D Tru= e) > + parser.add_argument("-o", "--output", dest =3D 'OutputFile', type =3D > argparse.FileType('wb'), > + help =3D "Output filename for PCD value or PCD sta= tement") > + parser.add_argument("-p", "--pcd", dest =3D 'PcdName', type =3D Valida= tePcdName, > + help =3D "Name of the PCD in the form > .") > + parser.add_argument("-t", "--type", dest =3D 'PcdType', default =3D No= ne, choices > =3D ['VPD','HII'], > + help =3D "PCD statement type (HII or VPD). Defaul= t is > standard.") > + parser.add_argument("-m", "--max-size", dest =3D 'MaxSize', type =3D > ValidateUnsignedInteger, > + help =3D "Maximum size of the PCD. Only used with= --type > VPD.") > + parser.add_argument("-f", "--offset", dest =3D 'Offset', type =3D > ValidateUnsignedInteger, > + help =3D "VPD offset if --type is VPD. UEFI Varia= ble offset > if --type is HII.") > + parser.add_argument("-n", "--variable-name", dest =3D 'VariableName', > + help =3D "UEFI variable name. Only used with --ty= pe HII.") > + parser.add_argument("-g", "--variable-guid", type =3D ValidateGuidName= , dest =3D > 'VariableGuid', > + help =3D "UEFI variable GUID C name. Only used wi= th --type > HII.") > + parser.add_argument("-v", "--verbose", dest =3D 'Verbose', action =3D > "store_true", > + help =3D "Increase output messages") > + parser.add_argument("-q", "--quiet", dest =3D 'Quiet', action =3D "sto= re_true", > + help =3D "Reduce output messages") > + parser.add_argument("--debug", dest =3D 'Debug', type =3D int, metavar= =3D '[0-9]', > choices =3D range(0,10), default =3D 0, > + help =3D "Set debug level") > + > + # > + # Parse command line arguments > + # > + args =3D parser.parse_args() > + > + # > + # Read binary input file > + # > + try: > + Buffer =3D args.InputFile.read() > + args.InputFile.close() > + except: > + print 'BinToPcd: error: can not read binary input file' > + sys.exit() > + > + # > + # Convert binary buffer to a DSC file PCD statement > + # > + if args.PcdName is None: > + # > + # If PcdName is None, then only a PCD value is being requested. > + Pcd =3D '' > + if args.Verbose: > + print 'PcdToBin: Convert binary file to PCD Value' > + elif args.PcdType is None: > + # > + # If --type is neither VPD nor HII, then use PCD statement syntax th= at is > + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule], > + # [PcdsDynamicDefault], and [PcdsDynamicExDefault]. > + # > + Pcd =3D ' %s|' % (args.PcdName) > + if args.Verbose: > + print 'PcdToBin: Convert binary file to PCD statement compatible w= ith PCD > sections:' > + print ' [PcdsFixedAtBuild]' > + print ' [PcdsPatchableInModule]' > + print ' [PcdsDynamicDefault]' > + print ' [PcdsDynamicExDefault]' > + elif args.PcdType =3D=3D 'VPD': > + if args.MaxSize is None: > + # > + # If --max-size is not provided, then set maximum size to the size= of the > + # binary input file > + # > + args.MaxSize =3D len(Buffer) > + if args.MaxSize < len(Buffer): > + print 'BinToPcd: error: argument --max-size is smaller than input = file.' > + sys.exit() > + if args.Offset is None: > + # > + # if --offset is not provided, then set offset field to '*' so bui= ld > + # tools will compute offset of PCD in VPD region. > + # > + Pcd =3D ' %s|*|%d|' % (args.PcdName, args.MaxSize) > + else: > + # > + # Use the --offset value provided. > + # > + Pcd =3D ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize) > + if args.Verbose: > + print 'PcdToBin: Convert binary file to PCD statement compatible w= ith PCD > sections' > + print ' [PcdsDynamicVpd]' > + print ' [PcdsDynamicExVpd]' > + elif args.PcdType =3D=3D 'HII': > + if args.VariableGuid is None: > + print 'BinToPcd: error: argument --variable-guid is required for -= -type > HII.' > + sys.exit() > + if args.VariableName is None: > + print 'BinToPcd: error: argument --variable-name is required for -= -type > HII.' > + sys.exit() > + if args.Offset is None: > + # > + # Use UEFI Variable offset of 0 if --offset is not provided > + # > + args.Offset =3D 0 > + Pcd =3D ' %s|L"%s"|%s|%d|' % (args.PcdName, args.VariableName, > args.VariableGuid, args.Offset) > + if args.Verbose: > + print 'PcdToBin: Convert binary file to PCD statement compatible w= ith PCD > sections' > + print ' [PcdsDynamicHii]' > + print ' [PcdsDynamicExHii]' > + > + # > + # Append byte array of values of the form '{0x01, 0x02, ...}' > + # > + Pcd =3D Pcd + '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in = Buffer])) > + > + # > + # Write PCD value or PCD statement to the output file > + # > + try: > + args.OutputFile.write (Pcd) > + args.OutputFile.close () > + except: > + # > + # If output file is not specified or it can not be written, then wri= te the > + # PCD value or PCD statement to the console > + # > + print Pcd > -- > 2.6.3.windows.1 >=20 > _______________________________________________ > edk2-devel mailing list > edk2-devel@lists.01.org > https://lists.01.org/mailman/listinfo/edk2-devel