]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/BinToPcd.py
BaseTools/BinToPcd: Encode string returned from ByteArray()
[mirror_edk2.git] / BaseTools / Scripts / BinToPcd.py
CommitLineData
fd0597aa
MK
1## @file\r
2# Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\r
3#\r
aedd1559 4# Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
fd0597aa
MK
5# This program and the accompanying materials\r
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14'''\r
15BinToPcd\r
16'''\r
72443dd2 17from __future__ import print_function\r
fd0597aa
MK
18\r
19import sys\r
20import argparse\r
21import re\r
aedd1559 22import xdrlib\r
fd0597aa
MK
23\r
24#\r
25# Globals for help information\r
26#\r
27__prog__ = 'BinToPcd'\r
aedd1559
KM
28__copyright__ = 'Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.'\r
29__description__ = 'Convert one or more binary files to a VOID* PCD value or DSC file VOID* PCD statement.\n'\r
fd0597aa
MK
30\r
31if __name__ == '__main__':\r
0c805f4f
KM
32 def ValidateUnsignedInteger (Argument):\r
33 try:\r
34 Value = int (Argument, 0)\r
35 except:\r
36 Message = '{Argument} is not a valid integer value.'.format (Argument = Argument)\r
37 raise argparse.ArgumentTypeError (Message)\r
38 if Value < 0:\r
39 Message = '{Argument} is a negative value.'.format (Argument = Argument)\r
40 raise argparse.ArgumentTypeError (Message)\r
41 return Value\r
fd0597aa 42\r
0c805f4f 43 def ValidatePcdName (Argument):\r
ccaa7754 44 if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['', '']:\r
0c805f4f
KM
45 Message = '{Argument} is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>'.format (Argument = Argument)\r
46 raise argparse.ArgumentTypeError (Message)\r
47 return Argument\r
fd0597aa 48\r
0c805f4f 49 def ValidateGuidName (Argument):\r
ccaa7754 50 if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['', '']:\r
0c805f4f
KM
51 Message = '{Argument} is not a valid GUID C name'.format (Argument = Argument)\r
52 raise argparse.ArgumentTypeError (Message)\r
53 return Argument\r
54\r
55 def ByteArray (Buffer, Xdr = False):\r
56 if Xdr:\r
57 #\r
58 # If Xdr flag is set then encode data using the Variable-Length Opaque\r
59 # Data format of RFC 4506 External Data Representation Standard (XDR).\r
60 #\r
61 XdrEncoder = xdrlib.Packer ()\r
62 for Item in Buffer:\r
63 XdrEncoder.pack_bytes (Item)\r
64 Buffer = bytearray (XdrEncoder.get_buffer ())\r
65 else:\r
66 #\r
67 # If Xdr flag is not set, then concatenate all the data\r
68 #\r
76c09700 69 Buffer = bytearray (b''.join (Buffer))\r
0c805f4f
KM
70 #\r
71 # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes\r
72 #\r
83964ebc
KM
73 PcdValue = '{' + ', '.join (['0x{Byte:02X}'.format (Byte = Item) for Item in Buffer]) + '}'\r
74 return PcdValue.encode (), len (Buffer)\r
aedd1559 75\r
fd0597aa 76 #\r
0c805f4f 77 # Create command line argument parser object\r
fd0597aa 78 #\r
0c805f4f
KM
79 parser = argparse.ArgumentParser (prog = __prog__,\r
80 description = __description__ + __copyright__,\r
81 conflict_handler = 'resolve')\r
82 parser.add_argument ("-i", "--input", dest = 'InputFile', type = argparse.FileType ('rb'), action='append', required = True,\r
83 help = "Input binary filename. Multiple input files are combined into a single PCD.")\r
84 parser.add_argument ("-o", "--output", dest = 'OutputFile', type = argparse.FileType ('wb'),\r
85 help = "Output filename for PCD value or PCD statement")\r
86 parser.add_argument ("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,\r
87 help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")\r
ccaa7754 88 parser.add_argument ("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD', 'HII'],\r
0c805f4f
KM
89 help = "PCD statement type (HII or VPD). Default is standard.")\r
90 parser.add_argument ("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,\r
91 help = "Maximum size of the PCD. Ignored with --type HII.")\r
92 parser.add_argument ("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,\r
93 help = "VPD offset if --type is VPD. UEFI Variable offset if --type is HII. Must be 8-byte aligned.")\r
94 parser.add_argument ("-n", "--variable-name", dest = 'VariableName',\r
95 help = "UEFI variable name. Only used with --type HII.")\r
96 parser.add_argument ("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',\r
97 help = "UEFI variable GUID C name. Only used with --type HII.")\r
98 parser.add_argument ("-x", "--xdr", dest = 'Xdr', action = "store_true",\r
99 help = "Encode PCD using the Variable-Length Opaque Data format of RFC 4506 External Data Representation Standard (XDR)")\r
100 parser.add_argument ("-v", "--verbose", dest = 'Verbose', action = "store_true",\r
101 help = "Increase output messages")\r
102 parser.add_argument ("-q", "--quiet", dest = 'Quiet', action = "store_true",\r
103 help = "Reduce output messages")\r
104 parser.add_argument ("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range (0, 10), default = 0,\r
105 help = "Set debug level")\r
aedd1559 106\r
0c805f4f
KM
107 #\r
108 # Parse command line arguments\r
109 #\r
110 args = parser.parse_args ()\r
fd0597aa 111\r
fd0597aa 112 #\r
0c805f4f 113 # Read all binary input files\r
aedd1559 114 #\r
0c805f4f
KM
115 Buffer = []\r
116 for File in args.InputFile:\r
117 try:\r
118 Buffer.append (File.read ())\r
119 File.close ()\r
120 except:\r
121 print ('BinToPcd: error: can not read binary input file {File}'.format (File = File))\r
122 sys.exit (1)\r
123\r
fd0597aa 124 #\r
0c805f4f
KM
125 # Convert PCD to an encoded string of hex values and determine the size of\r
126 # the encoded PCD in bytes.\r
fd0597aa 127 #\r
0c805f4f 128 PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)\r
aedd1559 129\r
8e965809 130 #\r
0c805f4f 131 # Convert binary buffer to a DSC file PCD statement\r
8e965809 132 #\r
0c805f4f
KM
133 if args.PcdName is None:\r
134 #\r
135 # If PcdName is None, then only a PCD value is being requested.\r
136 #\r
137 Pcd = PcdValue\r
138 if args.Verbose:\r
139 print ('BinToPcd: Convert binary file to PCD Value')\r
140 elif args.PcdType is None:\r
141 #\r
142 # If --type is neither VPD nor HII, then use PCD statement syntax that is\r
143 # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],\r
144 # [PcdsDynamicDefault], and [PcdsDynamicExDefault].\r
145 #\r
146 if args.MaxSize is None:\r
147 #\r
148 # If --max-size is not provided, then do not generate the syntax that\r
149 # includes the maximum size.\r
150 #\r
151 Pcd = ' {Name}|{Value}'.format (Name = args.PcdName, Value = PcdValue)\r
152 elif args.MaxSize < PcdSize:\r
153 print ('BinToPcd: error: argument --max-size is smaller than input file.')\r
154 sys.exit (1)\r
155 else:\r
156 Pcd = ' {Name}|{Value}|VOID*|{Size}'.format (Name = args.PcdName, Value = PcdValue, Size = args.MaxSize)\r
157\r
158 if args.Verbose:\r
159 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections:')\r
160 print (' [PcdsFixedAtBuild]')\r
161 print (' [PcdsPatchableInModule]')\r
162 print (' [PcdsDynamicDefault]')\r
163 print (' [PcdsDynamicExDefault]')\r
164 elif args.PcdType == 'VPD':\r
165 if args.MaxSize is None:\r
166 #\r
167 # If --max-size is not provided, then set maximum size to the size of the\r
168 # binary input file\r
169 #\r
170 args.MaxSize = PcdSize\r
171 if args.MaxSize < PcdSize:\r
172 print ('BinToPcd: error: argument --max-size is smaller than input file.')\r
173 sys.exit (1)\r
174 if args.Offset is None:\r
175 #\r
176 # if --offset is not provided, then set offset field to '*' so build\r
177 # tools will compute offset of PCD in VPD region.\r
178 #\r
179 Pcd = ' {Name}|*|{Size}|{Value}'.format (Name = args.PcdName, Size = args.MaxSize, Value = PcdValue)\r
180 else:\r
181 #\r
182 # --offset value must be 8-byte aligned\r
183 #\r
184 if (args.Offset % 8) != 0:\r
185 print ('BinToPcd: error: argument --offset must be 8-byte aligned.')\r
186 sys.exit (1)\r
187 #\r
188 # Use the --offset value provided.\r
189 #\r
190 Pcd = ' {Name}|{Offset}|{Size}|{Value}'.format (Name = args.PcdName, Offset = args.Offset, Size = args.MaxSize, Value = PcdValue)\r
191 if args.Verbose:\r
192 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')\r
193 print (' [PcdsDynamicVpd]')\r
194 print (' [PcdsDynamicExVpd]')\r
195 elif args.PcdType == 'HII':\r
196 if args.VariableGuid is None or args.VariableName is None:\r
197 print ('BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.')\r
198 sys.exit (1)\r
199 if args.Offset is None:\r
200 #\r
201 # Use UEFI Variable offset of 0 if --offset is not provided\r
202 #\r
203 args.Offset = 0\r
204 #\r
205 # --offset value must be 8-byte aligned\r
206 #\r
207 if (args.Offset % 8) != 0:\r
208 print ('BinToPcd: error: argument --offset must be 8-byte aligned.')\r
209 sys.exit (1)\r
210 Pcd = ' {Name}|L"{VarName}"|{VarGuid}|{Offset}|{Value}'.format (Name = args.PcdName, VarName = args.VariableName, VarGuid = args.VariableGuid, Offset = args.Offset, Value = PcdValue)\r
211 if args.Verbose:\r
212 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')\r
213 print (' [PcdsDynamicHii]')\r
214 print (' [PcdsDynamicExHii]')\r
fd0597aa 215\r
fd0597aa 216 #\r
0c805f4f 217 # Write PCD value or PCD statement to the output file\r
fd0597aa 218 #\r
0c805f4f
KM
219 try:\r
220 args.OutputFile.write (Pcd)\r
221 args.OutputFile.close ()\r
222 except:\r
223 #\r
224 # If output file is not specified or it can not be written, then write the\r
225 # PCD value or PCD statement to the console\r
226 #\r
227 print (Pcd)\r