public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch] BaseTools/Scripts: Add BinToPcd utility
@ 2016-11-08 23:45 Michael Kinney
  2016-11-10 14:36 ` Gao, Liming
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Kinney @ 2016-11-08 23:45 UTC (permalink / raw)
  To: edk2-devel; +Cc: Yonghong Zhu, Liming Gao

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.

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]]

Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
Copyright (c) 2016, Intel Corporation. All rights reserved.

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
                    <PcdTokenSpaceGuidCName>.<PcdCName>
  -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

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.

  BinToPcd.py -i test.bin

  {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

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:

  [PcdsFixedAtBuild]
  [PcdsPatchableInModule]
  [PcdsDynamicDefault]
  [PcdsDynamicExDefault]
  [PcdsDynamicVpd]
  [PcdsDynamicExVpd]
  [PcdsDynamicHii]
  [PcdsDynamicExHii]

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:

  gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
  gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer

The following example converts a public key binary file to a
[PcdsFixedAtBuild] compatible PCD statement:

  BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
    --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid

The PublicKey.pcd output file contains a single line:

  gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}

A DSC file can be updated to include the PublicKey.pcd file:

  [PcdsFixedAtBuild]
  !include PublicKey.pcd

VPD examples:
=============
  BinToPcd.py -i test.bin -p Guid.Token -t VPD
    Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
    Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
    Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  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}

HII examples:
=============
  BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
    Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}

  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}

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
---
 BaseTools/Scripts/BinToPcd.py | 179 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 179 insertions(+)
 create mode 100644 BaseTools/Scripts/BinToPcd.py

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
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 statement.
+#
+# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+# 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 IMPLIED.
+#
+
+'''
+BinToPcd
+'''
+
+import sys
+import argparse
+import re
+
+#
+# Globals for help information
+#
+__prog__        = 'BinToPcd'
+__version__     = '%s Version %s' % (__prog__, '0.9 ')
+__copyright__   = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
+__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\n'
+
+if __name__ == '__main__':
+  def ValidateUnsignedInteger (Argument):
+    try:
+      Value = int (Argument, 0)
+    except:
+      Message = '%s is not a valid integer value.' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    if Value < 0:
+      Message = '%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\_]*', Argument) <> ['','']:
+      Message = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    return Argument
+
+  def ValidateGuidName (Argument):
+    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:
+      Message = '%s is not a valid GUID C name' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    return Argument
+
+  #
+  # Create command line argument parser object
+  #
+  parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
+                                   description = __description__ + __copyright__,
+                                   conflict_handler = 'resolve')
+  parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'),
+                      help = "Input binary filename", required = True)
+  parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),
+                      help = "Output filename for PCD value or PCD statement")
+  parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
+                      help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")
+  parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],
+                      help = "PCD statement type (HII or VPD).  Default is standard.")
+  parser.add_argument("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,
+                      help = "Maximum size of the PCD.  Only used with --type VPD.")
+  parser.add_argument("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,
+                      help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.")
+  parser.add_argument("-n", "--variable-name", dest = 'VariableName',
+                      help = "UEFI variable name.  Only used with --type HII.")
+  parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',
+                      help = "UEFI variable GUID C name.  Only used with --type HII.")
+  parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",
+                      help = "Increase output messages")
+  parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
+                      help = "Reduce output messages")
+  parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range(0,10), default = 0,
+                      help = "Set debug level")
+
+  #
+  # Parse command line arguments
+  #
+  args = parser.parse_args()
+
+  #
+  # Read binary input file
+  #
+  try:
+    Buffer = 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 = ''
+    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 that is
+    # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
+    # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
+    #
+    Pcd = '  %s|' % (args.PcdName)
+    if args.Verbose:
+      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'
+      print '    [PcdsFixedAtBuild]'
+      print '    [PcdsPatchableInModule]'
+      print '    [PcdsDynamicDefault]'
+      print '    [PcdsDynamicExDefault]'
+  elif args.PcdType == '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 = 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 build
+      # tools will compute offset of PCD in VPD region.
+      #
+      Pcd = '  %s|*|%d|' % (args.PcdName, args.MaxSize)
+    else:
+      #
+      # Use the --offset value provided.
+      #
+      Pcd = '  %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
+    if args.Verbose:
+      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'
+      print '    [PcdsDynamicVpd]'
+      print '    [PcdsDynamicExVpd]'
+  elif args.PcdType == '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 = 0
+    Pcd = '  %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 with PCD sections'
+      print '    [PcdsDynamicHii]'
+      print '    [PcdsDynamicExHii]'
+
+  #
+  # Append byte array of values of the form '{0x01, 0x02, ...}'
+  #
+  Pcd = 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 write the
+    # PCD value or PCD statement to the console
+    #
+    print Pcd
-- 
2.6.3.windows.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-08 23:45 [Patch] BaseTools/Scripts: Add BinToPcd utility Michael Kinney
@ 2016-11-10 14:36 ` Gao, Liming
  2016-11-10 16:10   ` Kinney, Michael D
  0 siblings, 1 reply; 8+ messages in thread
From: Gao, Liming @ 2016-11-10 14:36 UTC (permalink / raw)
  To: Kinney, Michael D, edk2-devel@lists.01.org

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 utility to dump more one PCD values into one file. 

Thanks
Liming
-----Original Message-----
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael Kinney
Sent: Wednesday, November 9, 2016 7:45 AM
To: edk2-devel@lists.01.org
Cc: Gao, Liming <liming.gao@intel.com>
Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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.

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]]

Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
Copyright (c) 2016, Intel Corporation. All rights reserved.

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
                    <PcdTokenSpaceGuidCName>.<PcdCName>
  -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

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.

  BinToPcd.py -i test.bin

  {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

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:

  [PcdsFixedAtBuild]
  [PcdsPatchableInModule]
  [PcdsDynamicDefault]
  [PcdsDynamicExDefault]
  [PcdsDynamicVpd]
  [PcdsDynamicExVpd]
  [PcdsDynamicHii]
  [PcdsDynamicExHii]

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:

  gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
  gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer

The following example converts a public key binary file to a
[PcdsFixedAtBuild] compatible PCD statement:

  BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
    --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid

The PublicKey.pcd output file contains a single line:

  gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}

A DSC file can be updated to include the PublicKey.pcd file:

  [PcdsFixedAtBuild]
  !include PublicKey.pcd

VPD examples:
=============
  BinToPcd.py -i test.bin -p Guid.Token -t VPD
    Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
    Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
    Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}

  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}

HII examples:
=============
  BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
    Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}

  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}

Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
---
 BaseTools/Scripts/BinToPcd.py | 179 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 179 insertions(+)
 create mode 100644 BaseTools/Scripts/BinToPcd.py

diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
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 statement.
+#
+# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
+# 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 IMPLIED.
+#
+
+'''
+BinToPcd
+'''
+
+import sys
+import argparse
+import re
+
+#
+# Globals for help information
+#
+__prog__        = 'BinToPcd'
+__version__     = '%s Version %s' % (__prog__, '0.9 ')
+__copyright__   = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
+__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\n'
+
+if __name__ == '__main__':
+  def ValidateUnsignedInteger (Argument):
+    try:
+      Value = int (Argument, 0)
+    except:
+      Message = '%s is not a valid integer value.' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    if Value < 0:
+      Message = '%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\_]*', Argument) <> ['','']:
+      Message = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    return Argument
+
+  def ValidateGuidName (Argument):
+    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:
+      Message = '%s is not a valid GUID C name' % (Argument)
+      raise argparse.ArgumentTypeError(Message)
+    return Argument
+
+  #
+  # Create command line argument parser object
+  #
+  parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
+                                   description = __description__ + __copyright__,
+                                   conflict_handler = 'resolve')
+  parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'),
+                      help = "Input binary filename", required = True)
+  parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),
+                      help = "Output filename for PCD value or PCD statement")
+  parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
+                      help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")
+  parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],
+                      help = "PCD statement type (HII or VPD).  Default is standard.")
+  parser.add_argument("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,
+                      help = "Maximum size of the PCD.  Only used with --type VPD.")
+  parser.add_argument("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,
+                      help = "VPD offset if --type is VPD.  UEFI Variable offset if --type is HII.")
+  parser.add_argument("-n", "--variable-name", dest = 'VariableName',
+                      help = "UEFI variable name.  Only used with --type HII.")
+  parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',
+                      help = "UEFI variable GUID C name.  Only used with --type HII.")
+  parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",
+                      help = "Increase output messages")
+  parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
+                      help = "Reduce output messages")
+  parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range(0,10), default = 0,
+                      help = "Set debug level")
+
+  #
+  # Parse command line arguments
+  #
+  args = parser.parse_args()
+
+  #
+  # Read binary input file
+  #
+  try:
+    Buffer = 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 = ''
+    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 that is
+    # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
+    # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
+    #
+    Pcd = '  %s|' % (args.PcdName)
+    if args.Verbose:
+      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'
+      print '    [PcdsFixedAtBuild]'
+      print '    [PcdsPatchableInModule]'
+      print '    [PcdsDynamicDefault]'
+      print '    [PcdsDynamicExDefault]'
+  elif args.PcdType == '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 = 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 build
+      # tools will compute offset of PCD in VPD region.
+      #
+      Pcd = '  %s|*|%d|' % (args.PcdName, args.MaxSize)
+    else:
+      #
+      # Use the --offset value provided.
+      #
+      Pcd = '  %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
+    if args.Verbose:
+      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'
+      print '    [PcdsDynamicVpd]'
+      print '    [PcdsDynamicExVpd]'
+  elif args.PcdType == '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 = 0
+    Pcd = '  %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 with PCD sections'
+      print '    [PcdsDynamicHii]'
+      print '    [PcdsDynamicExHii]'
+
+  #
+  # Append byte array of values of the form '{0x01, 0x02, ...}'
+  #
+  Pcd = 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 write the
+    # PCD value or PCD statement to the console
+    #
+    print Pcd
-- 
2.6.3.windows.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-10 14:36 ` Gao, Liming
@ 2016-11-10 16:10   ` Kinney, Michael D
  2016-11-10 16:22     ` Gao, Liming
  0 siblings, 1 reply; 8+ messages in thread
From: Kinney, Michael D @ 2016-11-10 16:10 UTC (permalink / raw)
  To: Gao, Liming, edk2-devel@lists.01.org, Kinney, Michael D

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 
Based System Firmware Update and there are development steps in that 
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 
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 <michael.d.kinney@intel.com>; edk2-devel@lists.01.org
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
> 
> 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 utility
> to dump more one PCD values into one file.
> 
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> Kinney
> Sent: Wednesday, November 9, 2016 7:45 AM
> To: edk2-devel@lists.01.org
> Cc: Gao, Liming <liming.gao@intel.com>
> Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
> 
> 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.
> 
> 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]]
> 
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
> 
> 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
>                     <PcdTokenSpaceGuidCName>.<PcdCName>
>   -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
> 
> 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.
> 
>   BinToPcd.py -i test.bin
> 
>   {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
> 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:
> 
>   [PcdsFixedAtBuild]
>   [PcdsPatchableInModule]
>   [PcdsDynamicDefault]
>   [PcdsDynamicExDefault]
>   [PcdsDynamicVpd]
>   [PcdsDynamicExVpd]
>   [PcdsDynamicHii]
>   [PcdsDynamicExHii]
> 
> 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:
> 
>   gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
>   gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
> 
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
> 
>   BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
>     --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
> 
> The PublicKey.pcd output file contains a single line:
> 
>   gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
> 
> A DSC file can be updated to include the PublicKey.pcd file:
> 
>   [PcdsFixedAtBuild]
>   !include PublicKey.pcd
> 
> VPD examples:
> =============
>   BinToPcd.py -i test.bin -p Guid.Token -t VPD
>     Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
>   BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
>     Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
>   BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
>     Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
>   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}
> 
> HII examples:
> =============
>   BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
>     Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
> 
>   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}
> 
> Cc: Yonghong Zhu <yonghong.zhu@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Michael Kinney <michael.d.kinney@intel.com>
> ---
>  BaseTools/Scripts/BinToPcd.py | 179 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 179 insertions(+)
>  create mode 100644 BaseTools/Scripts/BinToPcd.py
> 
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__        = 'BinToPcd'
> +__version__     = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__   = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> +  def ValidateUnsignedInteger (Argument):
> +    try:
> +      Value = int (Argument, 0)
> +    except:
> +      Message = '%s is not a valid integer value.' % (Argument)
> +      raise argparse.ArgumentTypeError(Message)
> +    if Value < 0:
> +      Message = '%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\_]*', Argument)
> <> ['','']:
> +      Message = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' %
> (Argument)
> +      raise argparse.ArgumentTypeError(Message)
> +    return Argument
> +
> +  def ValidateGuidName (Argument):
> +    if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:
> +      Message = '%s is not a valid GUID C name' % (Argument)
> +      raise argparse.ArgumentTypeError(Message)
> +    return Argument
> +
> +  #
> +  # Create command line argument parser object
> +  #
> +  parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> +                                   description = __description__ +
> __copyright__,
> +                                   conflict_handler = 'resolve')
> +  parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> +                      help = "Input binary filename", required = True)
> +  parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> +                      help = "Output filename for PCD value or PCD statement")
> +  parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> +                      help = "Name of the PCD in the form
> <PcdTokenSpaceGuidCName>.<PcdCName>")
> +  parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> +                      help = "PCD statement type (HII or VPD).  Default is
> standard.")
> +  parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> +                      help = "Maximum size of the PCD.  Only used with --type
> VPD.")
> +  parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> +                      help = "VPD offset if --type is VPD.  UEFI Variable offset
> if --type is HII.")
> +  parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> +                      help = "UEFI variable name.  Only used with --type HII.")
> +  parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> +                      help = "UEFI variable GUID C name.  Only used with --type
> HII.")
> +  parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> +                      help = "Increase output messages")
> +  parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> +                      help = "Reduce output messages")
> +  parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> +                      help = "Set debug level")
> +
> +  #
> +  # Parse command line arguments
> +  #
> +  args = parser.parse_args()
> +
> +  #
> +  # Read binary input file
> +  #
> +  try:
> +    Buffer = 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 = ''
> +    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 that is
> +    # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> +    # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> +    #
> +    Pcd = '  %s|' % (args.PcdName)
> +    if args.Verbose:
> +      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> +      print '    [PcdsFixedAtBuild]'
> +      print '    [PcdsPatchableInModule]'
> +      print '    [PcdsDynamicDefault]'
> +      print '    [PcdsDynamicExDefault]'
> +  elif args.PcdType == '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 = 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 build
> +      # tools will compute offset of PCD in VPD region.
> +      #
> +      Pcd = '  %s|*|%d|' % (args.PcdName, args.MaxSize)
> +    else:
> +      #
> +      # Use the --offset value provided.
> +      #
> +      Pcd = '  %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> +    if args.Verbose:
> +      print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> +      print '    [PcdsDynamicVpd]'
> +      print '    [PcdsDynamicExVpd]'
> +  elif args.PcdType == '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 = 0
> +    Pcd = '  %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 with PCD
> sections'
> +      print '    [PcdsDynamicHii]'
> +      print '    [PcdsDynamicExHii]'
> +
> +  #
> +  # Append byte array of values of the form '{0x01, 0x02, ...}'
> +  #
> +  Pcd = 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 write the
> +    # PCD value or PCD statement to the console
> +    #
> +    print Pcd
> --
> 2.6.3.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-10 16:10   ` Kinney, Michael D
@ 2016-11-10 16:22     ` Gao, Liming
  2016-11-10 16:45       ` Kinney, Michael D
  0 siblings, 1 reply; 8+ messages in thread
From: Gao, Liming @ 2016-11-10 16:22 UTC (permalink / raw)
  To: Kinney, Michael D, edk2-devel@lists.01.org

Yes. Wiki is good enough.

For second one, I understand its complexity. Append is optional option. Users will decide to use it or not. I don't expect to add every file for every PCD.

From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:11 AM
To: Gao, Liming <liming.gao@intel.com>; edk2-devel@lists.01.org; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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
Based System Firmware Update and there are development steps in that
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
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.org<mailto:edk2-devel@lists.01.org>
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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 utility
> to dump more one PCD values into one file.
>
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> Kinney
> Sent: Wednesday, November 9, 2016 7:45 AM
> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> Cc: Gao, Liming
> Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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.
>
> 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]]
>
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
>
> 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
>
> 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.
>
> BinToPcd.py -i test.bin
>
> {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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:
>
> [PcdsFixedAtBuild]
> [PcdsPatchableInModule]
> [PcdsDynamicDefault]
> [PcdsDynamicExDefault]
> [PcdsDynamicVpd]
> [PcdsDynamicExVpd]
> [PcdsDynamicHii]
> [PcdsDynamicExHii]
>
> 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:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
>
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
>
> BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
> --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
>
> The PublicKey.pcd output file contains a single line:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
>
> A DSC file can be updated to include the PublicKey.pcd file:
>
> [PcdsFixedAtBuild]
> !include PublicKey.pcd
>
> VPD examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t VPD
> Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
> Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
> Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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}
>
> HII examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
> Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
>
> 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}
>
> 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
>
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__ = 'BinToPcd'
> +__version__ = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> + def ValidateUnsignedInteger (Argument):
> + try:
> + Value = int (Argument, 0)
> + except:
> + Message = '%s is not a valid integer value.' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + if Value < 0:
> + Message = '%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\_]*', Argument)
> <> ['','']:
> + Message = '%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 = '%s is not a valid GUID C name' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + return Argument
> +
> + #
> + # Create command line argument parser object
> + #
> + parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> + description = __description__ +
> __copyright__,
> + conflict_handler = 'resolve')
> + parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> + help = "Input binary filename", required = True)
> + parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> + help = "Output filename for PCD value or PCD statement")
> + parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> + help = "Name of the PCD in the form
> .")
> + parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> + help = "PCD statement type (HII or VPD). Default is
> standard.")
> + parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> + help = "Maximum size of the PCD. Only used with --type
> VPD.")
> + parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> + help = "VPD offset if --type is VPD. UEFI Variable offset
> if --type is HII.")
> + parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> + help = "UEFI variable name. Only used with --type HII.")
> + parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> + help = "UEFI variable GUID C name. Only used with --type
> HII.")
> + parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> + help = "Increase output messages")
> + parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> + help = "Reduce output messages")
> + parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> + help = "Set debug level")
> +
> + #
> + # Parse command line arguments
> + #
> + args = parser.parse_args()
> +
> + #
> + # Read binary input file
> + #
> + try:
> + Buffer = 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 = ''
> + 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 that is
> + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> + # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> + #
> + Pcd = ' %s|' % (args.PcdName)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> + print ' [PcdsFixedAtBuild]'
> + print ' [PcdsPatchableInModule]'
> + print ' [PcdsDynamicDefault]'
> + print ' [PcdsDynamicExDefault]'
> + elif args.PcdType == '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 = 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 build
> + # tools will compute offset of PCD in VPD region.
> + #
> + Pcd = ' %s|*|%d|' % (args.PcdName, args.MaxSize)
> + else:
> + #
> + # Use the --offset value provided.
> + #
> + Pcd = ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> + print ' [PcdsDynamicVpd]'
> + print ' [PcdsDynamicExVpd]'
> + elif args.PcdType == '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 = 0
> + Pcd = ' %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 with PCD
> sections'
> + print ' [PcdsDynamicHii]'
> + print ' [PcdsDynamicExHii]'
> +
> + #
> + # Append byte array of values of the form '{0x01, 0x02, ...}'
> + #
> + Pcd = 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 write the
> + # PCD value or PCD statement to the console
> + #
> + print Pcd
> --
> 2.6.3.windows.1
>
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-10 16:22     ` Gao, Liming
@ 2016-11-10 16:45       ` Kinney, Michael D
  2016-11-11  0:14         ` Gao, Liming
  0 siblings, 1 reply; 8+ messages in thread
From: Kinney, Michael D @ 2016-11-10 16:45 UTC (permalink / raw)
  To: Gao, Liming, edk2-devel@lists.01.org, Kinney, Michael D

Liming,

There are several ways that BinPcd can produce output files.

* Use -o, --output flag (e.g. -o test.pcd)
* Use output redirection >  (e.g. > test.pcd)
* Use output redirection with append >> (e.g. >> test.pcd)

Here is an example using append with current patch:

  BinToPcd.py -i test1.bin -p MyTokenSpaceGuid.MyToken1 > test.pcd
  BinToPcd.py -i test2.bin -p MyTokenSpaceGuid.MyToken2 >> test.pcd

Here is sample output:

  MyTokenSpaceGuid.MyToken1|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
  MyTokenSpaceGuid.MyToken2|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Same with -t VPD flag added

  MyTokenSpaceGuid.MyToken1|*|8|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
  MyTokenSpaceGuid.MyToken2|*|8|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Best regards,

Mike


From: Gao, Liming 
Sent: Thursday, November 10, 2016 8:23 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; edk2-devel@lists.01.org
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Yes. Wiki is good enough. 

For second one, I understand its complexity. Append is optional option. Users will decide to use it or not. I don't expect to add every file for every PCD. 

From: Kinney, Michael D 
Sent: Friday, November 11, 2016 12:11 AM
To: Gao, Liming <liming.gao@intel.com>; edk2-devel@lists.01.org; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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 
Based System Firmware Update and there are development steps in that 
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 
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.org
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
> 
> 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 utility
> to dump more one PCD values into one file.
> 
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> 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
> 
> 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.
> 
> 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]]
> 
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
> 
> 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
> 
> 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.
> 
> BinToPcd.py -i test.bin
> 
> {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
> 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:
> 
> [PcdsFixedAtBuild]
> [PcdsPatchableInModule]
> [PcdsDynamicDefault]
> [PcdsDynamicExDefault]
> [PcdsDynamicVpd]
> [PcdsDynamicExVpd]
> [PcdsDynamicHii]
> [PcdsDynamicExHii]
> 
> 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:
> 
> gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
> 
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
> 
> BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
> --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
> 
> The PublicKey.pcd output file contains a single line:
> 
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
> 
> A DSC file can be updated to include the PublicKey.pcd file:
> 
> [PcdsFixedAtBuild]
> !include PublicKey.pcd
> 
> VPD examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t VPD
> Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
> Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
> Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
> 
> 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}
> 
> HII examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
> Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
> 
> 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}
> 
> 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
> 
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__ = 'BinToPcd'
> +__version__ = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> + def ValidateUnsignedInteger (Argument):
> + try:
> + Value = int (Argument, 0)
> + except:
> + Message = '%s is not a valid integer value.' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + if Value < 0:
> + Message = '%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\_]*', Argument)
> <> ['','']:
> + Message = '%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 = '%s is not a valid GUID C name' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + return Argument
> +
> + #
> + # Create command line argument parser object
> + #
> + parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> + description = __description__ +
> __copyright__,
> + conflict_handler = 'resolve')
> + parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> + help = "Input binary filename", required = True)
> + parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> + help = "Output filename for PCD value or PCD statement")
> + parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> + help = "Name of the PCD in the form
> .")
> + parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> + help = "PCD statement type (HII or VPD). Default is
> standard.")
> + parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> + help = "Maximum size of the PCD. Only used with --type
> VPD.")
> + parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> + help = "VPD offset if --type is VPD. UEFI Variable offset
> if --type is HII.")
> + parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> + help = "UEFI variable name. Only used with --type HII.")
> + parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> + help = "UEFI variable GUID C name. Only used with --type
> HII.")
> + parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> + help = "Increase output messages")
> + parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> + help = "Reduce output messages")
> + parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> + help = "Set debug level")
> +
> + #
> + # Parse command line arguments
> + #
> + args = parser.parse_args()
> +
> + #
> + # Read binary input file
> + #
> + try:
> + Buffer = 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 = ''
> + 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 that is
> + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> + # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> + #
> + Pcd = ' %s|' % (args.PcdName)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> + print ' [PcdsFixedAtBuild]'
> + print ' [PcdsPatchableInModule]'
> + print ' [PcdsDynamicDefault]'
> + print ' [PcdsDynamicExDefault]'
> + elif args.PcdType == '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 = 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 build
> + # tools will compute offset of PCD in VPD region.
> + #
> + Pcd = ' %s|*|%d|' % (args.PcdName, args.MaxSize)
> + else:
> + #
> + # Use the --offset value provided.
> + #
> + Pcd = ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> + print ' [PcdsDynamicVpd]'
> + print ' [PcdsDynamicExVpd]'
> + elif args.PcdType == '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 = 0
> + Pcd = ' %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 with PCD
> sections'
> + print ' [PcdsDynamicHii]'
> + print ' [PcdsDynamicExHii]'
> +
> + #
> + # Append byte array of values of the form '{0x01, 0x02, ...}'
> + #
> + Pcd = 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 write the
> + # PCD value or PCD statement to the console
> + #
> + print Pcd
> --
> 2.6.3.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-10 16:45       ` Kinney, Michael D
@ 2016-11-11  0:14         ` Gao, Liming
  2016-11-14  2:57           ` Gao, Liming
  0 siblings, 1 reply; 8+ messages in thread
From: Gao, Liming @ 2016-11-11  0:14 UTC (permalink / raw)
  To: Kinney, Michael D, edk2-devel@lists.01.org

Mike:
  Thanks for your clarification. This way works for me.

Thanks
Liming
From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:46 AM
To: Gao, Liming <liming.gao@intel.com>; edk2-devel@lists.01.org; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Liming,

There are several ways that BinPcd can produce output files.

* Use -o, --output flag (e.g. -o test.pcd)
* Use output redirection > (e.g. > test.pcd)
* Use output redirection with append >> (e.g. >> test.pcd)

Here is an example using append with current patch:

BinToPcd.py -i test1.bin -p MyTokenSpaceGuid.MyToken1 > test.pcd
BinToPcd.py -i test2.bin -p MyTokenSpaceGuid.MyToken2 >> test.pcd

Here is sample output:

MyTokenSpaceGuid.MyToken1|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Same with -t VPD flag added

MyTokenSpaceGuid.MyToken1|*|8|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|*|8|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Best regards,

Mike


From: Gao, Liming
Sent: Thursday, November 10, 2016 8:23 AM
To: Kinney, Michael D ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Yes. Wiki is good enough.

For second one, I understand its complexity. Append is optional option. Users will decide to use it or not. I don't expect to add every file for every PCD.

From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:11 AM
To: Gao, Liming ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>; Kinney, Michael D
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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
Based System Firmware Update and there are development steps in that
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
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.org<mailto:edk2-devel@lists.01.org>
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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 utility
> to dump more one PCD values into one file.
>
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> Kinney
> Sent: Wednesday, November 9, 2016 7:45 AM
> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> Cc: Gao, Liming
> Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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.
>
> 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]]
>
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
>
> 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
>
> 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.
>
> BinToPcd.py -i test.bin
>
> {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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:
>
> [PcdsFixedAtBuild]
> [PcdsPatchableInModule]
> [PcdsDynamicDefault]
> [PcdsDynamicExDefault]
> [PcdsDynamicVpd]
> [PcdsDynamicExVpd]
> [PcdsDynamicHii]
> [PcdsDynamicExHii]
>
> 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:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
>
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
>
> BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
> --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
>
> The PublicKey.pcd output file contains a single line:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
>
> A DSC file can be updated to include the PublicKey.pcd file:
>
> [PcdsFixedAtBuild]
> !include PublicKey.pcd
>
> VPD examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t VPD
> Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
> Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
> Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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}
>
> HII examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
> Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
>
> 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}
>
> 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
>
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__ = 'BinToPcd'
> +__version__ = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> + def ValidateUnsignedInteger (Argument):
> + try:
> + Value = int (Argument, 0)
> + except:
> + Message = '%s is not a valid integer value.' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + if Value < 0:
> + Message = '%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\_]*', Argument)
> <> ['','']:
> + Message = '%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 = '%s is not a valid GUID C name' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + return Argument
> +
> + #
> + # Create command line argument parser object
> + #
> + parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> + description = __description__ +
> __copyright__,
> + conflict_handler = 'resolve')
> + parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> + help = "Input binary filename", required = True)
> + parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> + help = "Output filename for PCD value or PCD statement")
> + parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> + help = "Name of the PCD in the form
> .")
> + parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> + help = "PCD statement type (HII or VPD). Default is
> standard.")
> + parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> + help = "Maximum size of the PCD. Only used with --type
> VPD.")
> + parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> + help = "VPD offset if --type is VPD. UEFI Variable offset
> if --type is HII.")
> + parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> + help = "UEFI variable name. Only used with --type HII.")
> + parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> + help = "UEFI variable GUID C name. Only used with --type
> HII.")
> + parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> + help = "Increase output messages")
> + parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> + help = "Reduce output messages")
> + parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> + help = "Set debug level")
> +
> + #
> + # Parse command line arguments
> + #
> + args = parser.parse_args()
> +
> + #
> + # Read binary input file
> + #
> + try:
> + Buffer = 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 = ''
> + 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 that is
> + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> + # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> + #
> + Pcd = ' %s|' % (args.PcdName)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> + print ' [PcdsFixedAtBuild]'
> + print ' [PcdsPatchableInModule]'
> + print ' [PcdsDynamicDefault]'
> + print ' [PcdsDynamicExDefault]'
> + elif args.PcdType == '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 = 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 build
> + # tools will compute offset of PCD in VPD region.
> + #
> + Pcd = ' %s|*|%d|' % (args.PcdName, args.MaxSize)
> + else:
> + #
> + # Use the --offset value provided.
> + #
> + Pcd = ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> + print ' [PcdsDynamicVpd]'
> + print ' [PcdsDynamicExVpd]'
> + elif args.PcdType == '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 = 0
> + Pcd = ' %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 with PCD
> sections'
> + print ' [PcdsDynamicHii]'
> + print ' [PcdsDynamicExHii]'
> +
> + #
> + # Append byte array of values of the form '{0x01, 0x02, ...}'
> + #
> + Pcd = 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 write the
> + # PCD value or PCD statement to the console
> + #
> + print Pcd
> --
> 2.6.3.windows.1
>
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-11  0:14         ` Gao, Liming
@ 2016-11-14  2:57           ` Gao, Liming
  2016-11-14  3:48             ` Kinney, Michael D
  0 siblings, 1 reply; 8+ messages in thread
From: Gao, Liming @ 2016-11-14  2:57 UTC (permalink / raw)
  To: Gao, Liming, Kinney, Michael D, edk2-devel@lists.01.org

Mike:
Per DSC spec,  MaxSize supports all type PCDs. Here is one example in NT32Pkg DSC file. Could you help update BinToPcd utility to remove MaxSize limitation for VPD only?

[PcdsDynamicDefault.common.DEFAULT]
  gEfiNt32PkgTokenSpaceGuid.PcdWinNtSerialPort|L"COM1!COM2"|VOID*|20
  gEfiNt32PkgTokenSpaceGuid.PcdWinNtGop|L"UGA Window 1!UGA Window 2"|VOID*|52

Thanks
Liming
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Gao, Liming
Sent: Friday, November 11, 2016 8:14 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; edk2-devel@lists.01.org
Subject: Re: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Mike:
Thanks for your clarification. This way works for me.

Thanks
Liming
From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:46 AM
To: Gao, Liming ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>; Kinney, Michael D
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Liming,

There are several ways that BinPcd can produce output files.

* Use -o, --output flag (e.g. -o test.pcd)
* Use output redirection > (e.g. > test.pcd)
* Use output redirection with append >> (e.g. >> test.pcd)

Here is an example using append with current patch:

BinToPcd.py -i test1.bin -p MyTokenSpaceGuid.MyToken1 > test.pcd
BinToPcd.py -i test2.bin -p MyTokenSpaceGuid.MyToken2 >> test.pcd

Here is sample output:

MyTokenSpaceGuid.MyToken1|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Same with -t VPD flag added

MyTokenSpaceGuid.MyToken1|*|8|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|*|8|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Best regards,

Mike


From: Gao, Liming
Sent: Thursday, November 10, 2016 8:23 AM
To: Kinney, Michael D ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Yes. Wiki is good enough.

For second one, I understand its complexity. Append is optional option. Users will decide to use it or not. I don't expect to add every file for every PCD.

From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:11 AM
To: Gao, Liming ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>; Kinney, Michael D
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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
Based System Firmware Update and there are development steps in that
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
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.org<mailto:edk2-devel@lists.01.org>
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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 utility
> to dump more one PCD values into one file.
>
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> Kinney
> Sent: Wednesday, November 9, 2016 7:45 AM
> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> Cc: Gao, Liming
> Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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.
>
> 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]]
>
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
>
> 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
>
> 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.
>
> BinToPcd.py -i test.bin
>
> {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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:
>
> [PcdsFixedAtBuild]
> [PcdsPatchableInModule]
> [PcdsDynamicDefault]
> [PcdsDynamicExDefault]
> [PcdsDynamicVpd]
> [PcdsDynamicExVpd]
> [PcdsDynamicHii]
> [PcdsDynamicExHii]
>
> 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:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
>
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
>
> BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
> --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
>
> The PublicKey.pcd output file contains a single line:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
>
> A DSC file can be updated to include the PublicKey.pcd file:
>
> [PcdsFixedAtBuild]
> !include PublicKey.pcd
>
> VPD examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t VPD
> Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
> Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
> Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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}
>
> HII examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
> Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
>
> 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}
>
> 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
>
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__ = 'BinToPcd'
> +__version__ = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> + def ValidateUnsignedInteger (Argument):
> + try:
> + Value = int (Argument, 0)
> + except:
> + Message = '%s is not a valid integer value.' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + if Value < 0:
> + Message = '%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\_]*', Argument)
> <> ['','']:
> + Message = '%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 = '%s is not a valid GUID C name' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + return Argument
> +
> + #
> + # Create command line argument parser object
> + #
> + parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> + description = __description__ +
> __copyright__,
> + conflict_handler = 'resolve')
> + parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> + help = "Input binary filename", required = True)
> + parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> + help = "Output filename for PCD value or PCD statement")
> + parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> + help = "Name of the PCD in the form
> .")
> + parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> + help = "PCD statement type (HII or VPD). Default is
> standard.")
> + parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> + help = "Maximum size of the PCD. Only used with --type
> VPD.")
> + parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> + help = "VPD offset if --type is VPD. UEFI Variable offset
> if --type is HII.")
> + parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> + help = "UEFI variable name. Only used with --type HII.")
> + parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> + help = "UEFI variable GUID C name. Only used with --type
> HII.")
> + parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> + help = "Increase output messages")
> + parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> + help = "Reduce output messages")
> + parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> + help = "Set debug level")
> +
> + #
> + # Parse command line arguments
> + #
> + args = parser.parse_args()
> +
> + #
> + # Read binary input file
> + #
> + try:
> + Buffer = 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 = ''
> + 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 that is
> + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> + # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> + #
> + Pcd = ' %s|' % (args.PcdName)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> + print ' [PcdsFixedAtBuild]'
> + print ' [PcdsPatchableInModule]'
> + print ' [PcdsDynamicDefault]'
> + print ' [PcdsDynamicExDefault]'
> + elif args.PcdType == '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 = 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 build
> + # tools will compute offset of PCD in VPD region.
> + #
> + Pcd = ' %s|*|%d|' % (args.PcdName, args.MaxSize)
> + else:
> + #
> + # Use the --offset value provided.
> + #
> + Pcd = ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> + print ' [PcdsDynamicVpd]'
> + print ' [PcdsDynamicExVpd]'
> + elif args.PcdType == '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 = 0
> + Pcd = ' %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 with PCD
> sections'
> + print ' [PcdsDynamicHii]'
> + print ' [PcdsDynamicExHii]'
> +
> + #
> + # Append byte array of values of the form '{0x01, 0x02, ...}'
> + #
> + Pcd = 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 write the
> + # PCD value or PCD statement to the console
> + #
> + print Pcd
> --
> 2.6.3.windows.1
>
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] BaseTools/Scripts: Add BinToPcd utility
  2016-11-14  2:57           ` Gao, Liming
@ 2016-11-14  3:48             ` Kinney, Michael D
  0 siblings, 0 replies; 8+ messages in thread
From: Kinney, Michael D @ 2016-11-14  3:48 UTC (permalink / raw)
  To: Gao, Liming, edk2-devel@lists.01.org, Kinney, Michael D

Liming,

Thanks for catching this case.  I will send a V2 patch

Mike

From: Gao, Liming
Sent: Sunday, November 13, 2016 6:58 PM
To: Gao, Liming <liming.gao@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>; edk2-devel@lists.01.org
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Mike:
Per DSC spec,  MaxSize supports all type PCDs. Here is one example in NT32Pkg DSC file. Could you help update BinToPcd utility to remove MaxSize limitation for VPD only?

[PcdsDynamicDefault.common.DEFAULT]
  gEfiNt32PkgTokenSpaceGuid.PcdWinNtSerialPort|L"COM1!COM2"|VOID*|20
  gEfiNt32PkgTokenSpaceGuid.PcdWinNtGop|L"UGA Window 1!UGA Window 2"|VOID*|52

Thanks
Liming
From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Gao, Liming
Sent: Friday, November 11, 2016 8:14 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
Subject: Re: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Mike:
Thanks for your clarification. This way works for me.

Thanks
Liming
From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:46 AM
To: Gao, Liming ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>; Kinney, Michael D
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Liming,

There are several ways that BinPcd can produce output files.

* Use -o, --output flag (e.g. -o test.pcd)
* Use output redirection > (e.g. > test.pcd)
* Use output redirection with append >> (e.g. >> test.pcd)

Here is an example using append with current patch:

BinToPcd.py -i test1.bin -p MyTokenSpaceGuid.MyToken1 > test.pcd
BinToPcd.py -i test2.bin -p MyTokenSpaceGuid.MyToken2 >> test.pcd

Here is sample output:

MyTokenSpaceGuid.MyToken1|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Same with -t VPD flag added

MyTokenSpaceGuid.MyToken1|*|8|{0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
MyTokenSpaceGuid.MyToken2|*|8|{0x57, 0x6f, 0x72, 0x6c, 0x64, 0x20, 0x0d, 0x0a}

Best regards,

Mike


From: Gao, Liming
Sent: Thursday, November 10, 2016 8:23 AM
To: Kinney, Michael D ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

Yes. Wiki is good enough.

For second one, I understand its complexity. Append is optional option. Users will decide to use it or not. I don't expect to add every file for every PCD.

From: Kinney, Michael D
Sent: Friday, November 11, 2016 12:11 AM
To: Gao, Liming ; edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>; Kinney, Michael D
Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility

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
Based System Firmware Update and there are development steps in that
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
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.org<mailto:edk2-devel@lists.01.org>
> Subject: RE: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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 utility
> to dump more one PCD values into one file.
>
> Thanks
> Liming
> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Michael
> Kinney
> Sent: Wednesday, November 9, 2016 7:45 AM
> To: edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> Cc: Gao, Liming
> Subject: [edk2] [Patch] BaseTools/Scripts: Add BinToPcd utility
>
> 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.
>
> 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]]
>
> Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.
> Copyright (c) 2016, Intel Corporation. All rights reserved.
>
> 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
>
> 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.
>
> BinToPcd.py -i test.bin
>
> {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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:
>
> [PcdsFixedAtBuild]
> [PcdsPatchableInModule]
> [PcdsDynamicDefault]
> [PcdsDynamicExDefault]
> [PcdsDynamicVpd]
> [PcdsDynamicExVpd]
> [PcdsDynamicHii]
> [PcdsDynamicExHii]
>
> 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:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdRsa2048Sha256PublicKeyBuffer
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer
>
> The following example converts a public key binary file to a
> [PcdsFixedAtBuild] compatible PCD statement:
>
> BinToPcd.py -i PublicKey.bin -o PublicKey.pcd
> --pcd gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBufferkenSpaceGuid
>
> The PublicKey.pcd output file contains a single line:
>
> gEfiSecurityPkgTokenSpaceGuid.PcdPkcs7CertBuffer|{0x48, ...}
>
> A DSC file can be updated to include the PublicKey.pcd file:
>
> [PcdsFixedAtBuild]
> !include PublicKey.pcd
>
> VPD examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t VPD
> Guid.Name|*|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -f 20
> Guid.Name|20|8|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> BinToPcd.py -i test.bin -p Guid.Token -t VPD -m 10
> Guid.Name|*|10|{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x0d, 0x0a}
>
> 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}
>
> HII examples:
> =============
> BinToPcd.py -i test.bin -p Guid.Token -t HII -g VarGuid -n VarName
> Guid.Name|L"VarName"|VarGuid|0|{0x48, 0x65, 0x6c, 0x6c}
>
> 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}
>
> 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
>
> diff --git a/BaseTools/Scripts/BinToPcd.py b/BaseTools/Scripts/BinToPcd.py
> 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 statement.
> +#
> +# 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 IMPLIED.
> +#
> +
> +'''
> +BinToPcd
> +'''
> +
> +import sys
> +import argparse
> +import re
> +
> +#
> +# Globals for help information
> +#
> +__prog__ = 'BinToPcd'
> +__version__ = '%s Version %s' % (__prog__, '0.9 ')
> +__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'
> +__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID*
> PCD statement.\n'
> +
> +if __name__ == '__main__':
> + def ValidateUnsignedInteger (Argument):
> + try:
> + Value = int (Argument, 0)
> + except:
> + Message = '%s is not a valid integer value.' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + if Value < 0:
> + Message = '%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\_]*', Argument)
> <> ['','']:
> + Message = '%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 = '%s is not a valid GUID C name' % (Argument)
> + raise argparse.ArgumentTypeError(Message)
> + return Argument
> +
> + #
> + # Create command line argument parser object
> + #
> + parser = argparse.ArgumentParser(prog = __prog__, version = __version__,
> + description = __description__ +
> __copyright__,
> + conflict_handler = 'resolve')
> + parser.add_argument("-i", "--input", dest = 'InputFile', type =
> argparse.FileType('rb'),
> + help = "Input binary filename", required = True)
> + parser.add_argument("-o", "--output", dest = 'OutputFile', type =
> argparse.FileType('wb'),
> + help = "Output filename for PCD value or PCD statement")
> + parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,
> + help = "Name of the PCD in the form
> .")
> + parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices
> = ['VPD','HII'],
> + help = "PCD statement type (HII or VPD). Default is
> standard.")
> + parser.add_argument("-m", "--max-size", dest = 'MaxSize', type =
> ValidateUnsignedInteger,
> + help = "Maximum size of the PCD. Only used with --type
> VPD.")
> + parser.add_argument("-f", "--offset", dest = 'Offset', type =
> ValidateUnsignedInteger,
> + help = "VPD offset if --type is VPD. UEFI Variable offset
> if --type is HII.")
> + parser.add_argument("-n", "--variable-name", dest = 'VariableName',
> + help = "UEFI variable name. Only used with --type HII.")
> + parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest =
> 'VariableGuid',
> + help = "UEFI variable GUID C name. Only used with --type
> HII.")
> + parser.add_argument("-v", "--verbose", dest = 'Verbose', action =
> "store_true",
> + help = "Increase output messages")
> + parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",
> + help = "Reduce output messages")
> + parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]',
> choices = range(0,10), default = 0,
> + help = "Set debug level")
> +
> + #
> + # Parse command line arguments
> + #
> + args = parser.parse_args()
> +
> + #
> + # Read binary input file
> + #
> + try:
> + Buffer = 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 = ''
> + 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 that is
> + # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],
> + # [PcdsDynamicDefault], and [PcdsDynamicExDefault].
> + #
> + Pcd = ' %s|' % (args.PcdName)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections:'
> + print ' [PcdsFixedAtBuild]'
> + print ' [PcdsPatchableInModule]'
> + print ' [PcdsDynamicDefault]'
> + print ' [PcdsDynamicExDefault]'
> + elif args.PcdType == '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 = 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 build
> + # tools will compute offset of PCD in VPD region.
> + #
> + Pcd = ' %s|*|%d|' % (args.PcdName, args.MaxSize)
> + else:
> + #
> + # Use the --offset value provided.
> + #
> + Pcd = ' %s|%d|%d|' % (args.PcdName, args.Offset, args.MaxSize)
> + if args.Verbose:
> + print 'PcdToBin: Convert binary file to PCD statement compatible with PCD
> sections'
> + print ' [PcdsDynamicVpd]'
> + print ' [PcdsDynamicExVpd]'
> + elif args.PcdType == '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 = 0
> + Pcd = ' %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 with PCD
> sections'
> + print ' [PcdsDynamicHii]'
> + print ' [PcdsDynamicExHii]'
> +
> + #
> + # Append byte array of values of the form '{0x01, 0x02, ...}'
> + #
> + Pcd = 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 write the
> + # PCD value or PCD statement to the console
> + #
> + print Pcd
> --
> 2.6.3.windows.1
>
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
> https://lists.01.org/mailman/listinfo/edk2-devel
_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org<mailto:edk2-devel@lists.01.org>
https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2016-11-14  3:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-08 23:45 [Patch] BaseTools/Scripts: Add BinToPcd utility Michael Kinney
2016-11-10 14:36 ` Gao, Liming
2016-11-10 16:10   ` Kinney, Michael D
2016-11-10 16:22     ` Gao, Liming
2016-11-10 16:45       ` Kinney, Michael D
2016-11-11  0:14         ` Gao, Liming
2016-11-14  2:57           ` Gao, Liming
2016-11-14  3:48             ` Kinney, Michael D

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox