summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
c27c000)
https://bugzilla.tianocore.org/show_bug.cgi?id=890
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 <sean.brogan@microsoft.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
## @file\r
# Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\r
#\r
## @file\r
# Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\r
#\r
-# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
# This program and the accompanying materials\r
# are licensed and made available under the terms and conditions of the BSD License\r
# which accompanies this distribution. The full text of the license may be found at\r
# This program and the accompanying materials\r
# are licensed and made available under the terms and conditions of the BSD License\r
# which accompanies this distribution. The full text of the license may be found at\r
import sys\r
import argparse\r
import re\r
import sys\r
import argparse\r
import re\r
\r
#\r
# Globals for help information\r
#\r
__prog__ = 'BinToPcd'\r
\r
#\r
# Globals for help information\r
#\r
__prog__ = 'BinToPcd'\r
-__version__ = '%s Version %s' % (__prog__, '0.9 ')\r
-__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'\r
-__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\n'\r
+__version__ = '%s Version %s' % (__prog__, '0.91 ')\r
+__copyright__ = 'Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.'\r
+__description__ = 'Convert one or more binary files to a VOID* PCD value or DSC file VOID* PCD statement.\n'\r
\r
if __name__ == '__main__':\r
def ValidateUnsignedInteger (Argument):\r
\r
if __name__ == '__main__':\r
def ValidateUnsignedInteger (Argument):\r
Message = '%s is not a valid GUID C name' % (Argument)\r
raise argparse.ArgumentTypeError(Message)\r
return Argument\r
Message = '%s is not a valid GUID C name' % (Argument)\r
raise argparse.ArgumentTypeError(Message)\r
return Argument\r
- \r
- def ByteArray (Buffer):\r
+\r
+ def ByteArray (Buffer, Xdr = False):\r
+ if Xdr:\r
+ #\r
+ # If Xdr flag is set then encode data using the Variable-Length Opaque\r
+ # Data format of RFC 4506 External Data Representation Standard (XDR).\r
+ #\r
+ XdrEncoder = xdrlib.Packer()\r
+ for Item in Buffer:\r
+ XdrEncoder.pack_bytes(Item)\r
+ Buffer = XdrEncoder.get_buffer()\r
+ else:\r
+ #\r
+ # If Xdr flag is not set, then concatenate all the data\r
+ #\r
+ Buffer = ''.join(Buffer)\r
- # Append byte array of values of the form '{0x01, 0x02, ...}'\r
+ # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes\r
- return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer]))\r
- \r
+ return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer])), len (Buffer)\r
+\r
#\r
# Create command line argument parser object\r
#\r
parser = argparse.ArgumentParser(prog = __prog__, version = __version__,\r
description = __description__ + __copyright__,\r
conflict_handler = 'resolve')\r
#\r
# Create command line argument parser object\r
#\r
parser = argparse.ArgumentParser(prog = __prog__, version = __version__,\r
description = __description__ + __copyright__,\r
conflict_handler = 'resolve')\r
- parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'),\r
- help = "Input binary filename", required = True)\r
+ parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'), action='append', required = True,\r
+ help = "Input binary filename. Multiple input files are combined into a single PCD.")\r
parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),\r
help = "Output filename for PCD value or PCD statement")\r
parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,\r
parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),\r
help = "Output filename for PCD value or PCD statement")\r
parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,\r
help = "UEFI variable name. Only used with --type HII.")\r
parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',\r
help = "UEFI variable GUID C name. Only used with --type HII.")\r
help = "UEFI variable name. Only used with --type HII.")\r
parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',\r
help = "UEFI variable GUID C name. Only used with --type HII.")\r
+ parser.add_argument("-x", "--xdr", dest = 'Xdr', action = "store_true",\r
+ help = "Encode PCD using the Variable-Length Opaque Data format of RFC 4506 External Data Representation Standard (XDR)")\r
parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",\r
help = "Increase output messages")\r
parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",\r
parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",\r
help = "Increase output messages")\r
parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",\r
args = parser.parse_args()\r
\r
#\r
args = parser.parse_args()\r
\r
#\r
- # Read binary input file\r
+ # Read all binary input files\r
- try:\r
- Buffer = args.InputFile.read()\r
- args.InputFile.close()\r
- except:\r
- print 'BinToPcd: error: can not read binary input file'\r
- sys.exit()\r
+ Buffer = []\r
+ for File in args.InputFile:\r
+ try:\r
+ Buffer.append(File.read())\r
+ File.close()\r
+ except:\r
+ print 'BinToPcd: error: can not read binary input file', File\r
+ sys.exit()\r
+\r
+ #\r
+ # Convert PCD to an encoded string of hex values and determine the size of\r
+ # the encoded PCD in bytes.\r
+ #\r
+ PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)\r
\r
#\r
# Convert binary buffer to a DSC file PCD statement\r
\r
#\r
# Convert binary buffer to a DSC file PCD statement\r
if args.PcdName is None:\r
#\r
# If PcdName is None, then only a PCD value is being requested.\r
if args.PcdName is None:\r
#\r
# If PcdName is None, then only a PCD value is being requested.\r
- Pcd = ByteArray (Buffer)\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD Value'\r
elif args.PcdType is None:\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD Value'\r
elif args.PcdType is None:\r
# If --max-size is not provided, then do not generate the syntax that\r
# includes the maximum size.\r
#\r
# If --max-size is not provided, then do not generate the syntax that\r
# includes the maximum size.\r
#\r
- Pcd = ' %s|%s' % (args.PcdName, ByteArray (Buffer))\r
- elif args.MaxSize < len(Buffer):\r
+ Pcd = ' %s|%s' % (args.PcdName, PcdValue)\r
+ elif args.MaxSize < PcdSize:\r
print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
sys.exit()\r
else:\r
print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
sys.exit()\r
else:\r
- Pcd = ' %s|%s|VOID*|%d' % (args.PcdName, ByteArray (Buffer), args.MaxSize)\r
- args.MaxSize = len(Buffer)\r
- \r
+ Pcd = ' %s|%s|VOID*|%d' % (args.PcdName, PcdValue, args.MaxSize)\r
+\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'\r
print ' [PcdsFixedAtBuild]'\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'\r
print ' [PcdsFixedAtBuild]'\r
# If --max-size is not provided, then set maximum size to the size of the\r
# binary input file\r
#\r
# If --max-size is not provided, then set maximum size to the size of the\r
# binary input file\r
#\r
- args.MaxSize = len(Buffer)\r
- if args.MaxSize < len(Buffer):\r
+ args.MaxSize = PcdSize\r
+ if args.MaxSize < PcdSize:\r
print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
sys.exit()\r
if args.Offset is None:\r
print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
sys.exit()\r
if args.Offset is None:\r
# if --offset is not provided, then set offset field to '*' so build\r
# tools will compute offset of PCD in VPD region.\r
#\r
# if --offset is not provided, then set offset field to '*' so build\r
# tools will compute offset of PCD in VPD region.\r
#\r
- Pcd = ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, ByteArray (Buffer))\r
+ Pcd = ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, PcdValue)\r
else:\r
#\r
# Use the --offset value provided.\r
#\r
else:\r
#\r
# Use the --offset value provided.\r
#\r
- Pcd = ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, ByteArray (Buffer))\r
+ Pcd = ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, PcdValue)\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
print ' [PcdsDynamicVpd]'\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
print ' [PcdsDynamicVpd]'\r
# Use UEFI Variable offset of 0 if --offset is not provided\r
#\r
args.Offset = 0\r
# Use UEFI Variable offset of 0 if --offset is not provided\r
#\r
args.Offset = 0\r
- Pcd = ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, ByteArray (Buffer))\r
+ Pcd = ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, PcdValue)\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
print ' [PcdsDynamicHii]'\r
if args.Verbose:\r
print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
print ' [PcdsDynamicHii]'\r