From nobody Tue May 7 11:37:09 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 1521672849332125.10935811225409; Wed, 21 Mar 2018 15:54:09 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 47DAD22468025; Wed, 21 Mar 2018 15:47:36 -0700 (PDT) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (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 DD39320954B87 for ; Wed, 21 Mar 2018 15:47:34 -0700 (PDT) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 21 Mar 2018 15:54:05 -0700 Received: from mdkinney-mobl2.amr.corp.intel.com ([10.241.98.49]) by fmsmga006.fm.intel.com with ESMTP; 21 Mar 2018 15:54:05 -0700 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=192.55.52.43; helo=mga05.intel.com; envelope-from=michael.d.kinney@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,341,1517904000"; d="scan'208";a="213501485" From: Michael D Kinney To: edk2-devel@lists.01.org Date: Wed, 21 Mar 2018 15:54:03 -0700 Message-Id: <20180321225403.4056-1-michael.d.kinney@intel.com> X-Mailer: git-send-email 2.14.2.windows.3 Subject: [edk2] [Patch V2] BaseTools/BinToPcd: Add support for multiple binary input files 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: "Kinney, Michael D" , Liming Gao MIME-Version: 1.0 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" From: "Kinney, Michael D" https://bugzilla.tianocore.org/show_bug.cgi?id=3D890 There are use cases where a VOID * PCD needs to be generated from multiple binary input files. This can be in the form of an array of fixed size elements or a set of variable sized elements. Update BinToPcd to support multiple one or more -i INPUTFILE arguments. By default, the contents of each binary input file are concatenated in the order provided. This supports generating a PCD that is an array of fixed size elements Add -x, --xdr flags to BinToPcd to encodes the PCD using the Variable-Length Opaque Data of RFC 4506 External Data Representation Standard (XDR). https://tools.ietf.org/html/rfc4506 https://tools.ietf.org/html/rfc4506#section-4.10 The data format from RFC 4506 meets the requirements for a PCD that is a set of variable sized elements in the Variable-Length Opaque Data format. The overhead of this format is a 32-bit length and 0 to 3 bytes of padding to align the next element at a 32-bit boundary. Cc: Sean Brogan Cc: Yonghong Zhu Cc: Liming Gao Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Michael D Kinney Reviewed-by: Yonghong Zhu =20 --- BaseTools/Scripts/BinToPcd.py | 83 ++++++++++++++++++++++++++++-----------= ---- 1 file changed, 54 insertions(+), 29 deletions(-) diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py index 68a7ac652d..f2485a27fa 100644 --- a/BaseTools/Scripts/BinToPcd.py +++ b/BaseTools/Scripts/BinToPcd.py @@ -1,7 +1,7 @@ ## @file # Convert a binary file to a VOID* PCD value or DSC file VOID* PCD stateme= nt. # -# Copyright (c) 2016, Intel Corporation. All rights reserved.
+# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.
# This program and the accompanying materials # are licensed and made available under the terms and conditions of the BS= D License # which accompanies this distribution. The full text of the license may b= e found at @@ -18,14 +18,15 @@ BinToPcd import sys import argparse import re +import xdrlib =20 # # Globals for help information # __prog__ =3D 'BinToPcd' -__version__ =3D '%s Version %s' % (__prog__, '0.9 ') -__copyright__ =3D 'Copyright (c) 2016, Intel Corporation. All rights res= erved.' -__description__ =3D 'Convert a binary file to a VOID* PCD value or DSC fil= e VOID* PCD statement.\n' +__version__ =3D '%s Version %s' % (__prog__, '0.91 ') +__copyright__ =3D 'Copyright (c) 2016 - 2018, Intel Corporation. All rig= hts reserved.' +__description__ =3D 'Convert one or more binary files to a VOID* PCD value= or DSC file VOID* PCD statement.\n' =20 if __name__ =3D=3D '__main__': def ValidateUnsignedInteger (Argument): @@ -50,21 +51,35 @@ if __name__ =3D=3D '__main__': Message =3D '%s is not a valid GUID C name' % (Argument) raise argparse.ArgumentTypeError(Message) return Argument - =20 - def ByteArray (Buffer): + + def ByteArray (Buffer, Xdr =3D False): + if Xdr: + # + # If Xdr flag is set then encode data using the Variable-Length Opaq= ue + # Data format of RFC 4506 External Data Representation Standard (XDR= ). + # + XdrEncoder =3D xdrlib.Packer() + for Item in Buffer: + XdrEncoder.pack_bytes(Item) + Buffer =3D XdrEncoder.get_buffer() + else: + # + # If Xdr flag is not set, then concatenate all the data + # + Buffer =3D ''.join(Buffer) # - # Append byte array of values of the form '{0x01, 0x02, ...}' + # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PC= D length in bytes # - return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer]= )) - =20 + return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer]= )), len (Buffer) + # # Create command line argument parser object # parser =3D argparse.ArgumentParser(prog =3D __prog__, version =3D __vers= ion__, description =3D __description__ + __cop= yright__, conflict_handler =3D 'resolve') - parser.add_argument("-i", "--input", dest =3D 'InputFile', type =3D argp= arse.FileType('rb'), - help =3D "Input binary filename", required =3D True) + parser.add_argument("-i", "--input", dest =3D 'InputFile', type =3D argp= arse.FileType('rb'), action=3D'append', required =3D True, + help =3D "Input binary filename. Multiple input fil= es are combined into a single PCD.") parser.add_argument("-o", "--output", dest =3D 'OutputFile', type =3D ar= gparse.FileType('wb'), help =3D "Output filename for PCD value or PCD state= ment") parser.add_argument("-p", "--pcd", dest =3D 'PcdName', type =3D Validate= PcdName, @@ -79,6 +94,8 @@ if __name__ =3D=3D '__main__': help =3D "UEFI variable name. Only used with --type= HII.") parser.add_argument("-g", "--variable-guid", type =3D ValidateGuidName, = dest =3D 'VariableGuid', help =3D "UEFI variable GUID C name. Only used with= --type HII.") + parser.add_argument("-x", "--xdr", dest =3D 'Xdr', action =3D "store_tru= e", + help =3D "Encode PCD using the Variable-Length Opaqu= e Data format of RFC 4506 External Data Representation Standard (XDR)") parser.add_argument("-v", "--verbose", dest =3D 'Verbose', action =3D "s= tore_true", help =3D "Increase output messages") parser.add_argument("-q", "--quiet", dest =3D 'Quiet', action =3D "store= _true", @@ -92,14 +109,22 @@ if __name__ =3D=3D '__main__': args =3D parser.parse_args() =20 # - # Read binary input file + # Read all binary input files # - try: - Buffer =3D args.InputFile.read() - args.InputFile.close() - except: - print 'BinToPcd: error: can not read binary input file' - sys.exit() + Buffer =3D [] + for File in args.InputFile: + try: + Buffer.append(File.read()) + File.close() + except: + print 'BinToPcd: error: can not read binary input file', File + sys.exit() + + # + # Convert PCD to an encoded string of hex values and determine the size = of + # the encoded PCD in bytes. + # + PcdValue, PcdSize =3D ByteArray (Buffer, args.Xdr) =20 # # Convert binary buffer to a DSC file PCD statement @@ -107,7 +132,8 @@ if __name__ =3D=3D '__main__': if args.PcdName is None: # # If PcdName is None, then only a PCD value is being requested. - Pcd =3D ByteArray (Buffer) + # + Pcd =3D PcdValue if args.Verbose: print 'PcdToBin: Convert binary file to PCD Value' elif args.PcdType is None: @@ -121,14 +147,13 @@ if __name__ =3D=3D '__main__': # If --max-size is not provided, then do not generate the syntax that # includes the maximum size. # - Pcd =3D ' %s|%s' % (args.PcdName, ByteArray (Buffer)) - elif args.MaxSize < len(Buffer): + Pcd =3D ' %s|%s' % (args.PcdName, PcdValue) + elif args.MaxSize < PcdSize: print 'BinToPcd: error: argument --max-size is smaller than input fi= le.' sys.exit() else: - Pcd =3D ' %s|%s|VOID*|%d' % (args.PcdName, ByteArray (Buffer), args= .MaxSize) - args.MaxSize =3D len(Buffer) - =20 + Pcd =3D ' %s|%s|VOID*|%d' % (args.PcdName, PcdValue, args.MaxSize) + if args.Verbose: print 'PcdToBin: Convert binary file to PCD statement compatible wit= h PCD sections:' print ' [PcdsFixedAtBuild]' @@ -141,8 +166,8 @@ if __name__ =3D=3D '__main__': # If --max-size is not provided, then set maximum size to the size o= f the # binary input file # - args.MaxSize =3D len(Buffer) - if args.MaxSize < len(Buffer): + args.MaxSize =3D PcdSize + if args.MaxSize < PcdSize: print 'BinToPcd: error: argument --max-size is smaller than input fi= le.' sys.exit() if args.Offset is None: @@ -150,12 +175,12 @@ if __name__ =3D=3D '__main__': # if --offset is not provided, then set offset field to '*' so build # tools will compute offset of PCD in VPD region. # - Pcd =3D ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, ByteArray (Buf= fer)) + Pcd =3D ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, PcdValue) else: # # Use the --offset value provided. # - Pcd =3D ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, = ByteArray (Buffer)) + Pcd =3D ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, = PcdValue) if args.Verbose: print 'PcdToBin: Convert binary file to PCD statement compatible wit= h PCD sections' print ' [PcdsDynamicVpd]' @@ -172,7 +197,7 @@ if __name__ =3D=3D '__main__': # Use UEFI Variable offset of 0 if --offset is not provided # args.Offset =3D 0 - Pcd =3D ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args= .VariableGuid, args.Offset, ByteArray (Buffer)) + Pcd =3D ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args= .VariableGuid, args.Offset, PcdValue) if args.Verbose: print 'PcdToBin: Convert binary file to PCD statement compatible wit= h PCD sections' print ' [PcdsDynamicHii]' --=20 2.14.2.windows.3 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel