]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/BinToPcd.py
BaseTools: Refactor python print statements
[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
KM
43 def ValidatePcdName (Argument):\r
44 if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:\r
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
KM
49 def ValidateGuidName (Argument):\r
50 if re.split ('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) != ['','']:\r
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
69 Buffer = b''.join (Buffer)\r
70 #\r
71 # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes\r
72 #\r
73 return '{' + (', '.join (['0x{Byte:02X}'.format (Byte = Item) for Item in Buffer])) + '}', len (Buffer)\r
aedd1559 74\r
fd0597aa 75 #\r
0c805f4f 76 # Create command line argument parser object\r
fd0597aa 77 #\r
0c805f4f
KM
78 parser = argparse.ArgumentParser (prog = __prog__,\r
79 description = __description__ + __copyright__,\r
80 conflict_handler = 'resolve')\r
81 parser.add_argument ("-i", "--input", dest = 'InputFile', type = argparse.FileType ('rb'), action='append', required = True,\r
82 help = "Input binary filename. Multiple input files are combined into a single PCD.")\r
83 parser.add_argument ("-o", "--output", dest = 'OutputFile', type = argparse.FileType ('wb'),\r
84 help = "Output filename for PCD value or PCD statement")\r
85 parser.add_argument ("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,\r
86 help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")\r
87 parser.add_argument ("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],\r
88 help = "PCD statement type (HII or VPD). Default is standard.")\r
89 parser.add_argument ("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,\r
90 help = "Maximum size of the PCD. Ignored with --type HII.")\r
91 parser.add_argument ("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,\r
92 help = "VPD offset if --type is VPD. UEFI Variable offset if --type is HII. Must be 8-byte aligned.")\r
93 parser.add_argument ("-n", "--variable-name", dest = 'VariableName',\r
94 help = "UEFI variable name. Only used with --type HII.")\r
95 parser.add_argument ("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',\r
96 help = "UEFI variable GUID C name. Only used with --type HII.")\r
97 parser.add_argument ("-x", "--xdr", dest = 'Xdr', action = "store_true",\r
98 help = "Encode PCD using the Variable-Length Opaque Data format of RFC 4506 External Data Representation Standard (XDR)")\r
99 parser.add_argument ("-v", "--verbose", dest = 'Verbose', action = "store_true",\r
100 help = "Increase output messages")\r
101 parser.add_argument ("-q", "--quiet", dest = 'Quiet', action = "store_true",\r
102 help = "Reduce output messages")\r
103 parser.add_argument ("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range (0, 10), default = 0,\r
104 help = "Set debug level")\r
aedd1559 105\r
0c805f4f
KM
106 #\r
107 # Parse command line arguments\r
108 #\r
109 args = parser.parse_args ()\r
fd0597aa 110\r
fd0597aa 111 #\r
0c805f4f 112 # Read all binary input files\r
aedd1559 113 #\r
0c805f4f
KM
114 Buffer = []\r
115 for File in args.InputFile:\r
116 try:\r
117 Buffer.append (File.read ())\r
118 File.close ()\r
119 except:\r
120 print ('BinToPcd: error: can not read binary input file {File}'.format (File = File))\r
121 sys.exit (1)\r
122\r
fd0597aa 123 #\r
0c805f4f
KM
124 # Convert PCD to an encoded string of hex values and determine the size of\r
125 # the encoded PCD in bytes.\r
fd0597aa 126 #\r
0c805f4f 127 PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)\r
aedd1559 128\r
8e965809 129 #\r
0c805f4f 130 # Convert binary buffer to a DSC file PCD statement\r
8e965809 131 #\r
0c805f4f
KM
132 if args.PcdName is None:\r
133 #\r
134 # If PcdName is None, then only a PCD value is being requested.\r
135 #\r
136 Pcd = PcdValue\r
137 if args.Verbose:\r
138 print ('BinToPcd: Convert binary file to PCD Value')\r
139 elif args.PcdType is None:\r
140 #\r
141 # If --type is neither VPD nor HII, then use PCD statement syntax that is\r
142 # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],\r
143 # [PcdsDynamicDefault], and [PcdsDynamicExDefault].\r
144 #\r
145 if args.MaxSize is None:\r
146 #\r
147 # If --max-size is not provided, then do not generate the syntax that\r
148 # includes the maximum size.\r
149 #\r
150 Pcd = ' {Name}|{Value}'.format (Name = args.PcdName, Value = PcdValue)\r
151 elif args.MaxSize < PcdSize:\r
152 print ('BinToPcd: error: argument --max-size is smaller than input file.')\r
153 sys.exit (1)\r
154 else:\r
155 Pcd = ' {Name}|{Value}|VOID*|{Size}'.format (Name = args.PcdName, Value = PcdValue, Size = args.MaxSize)\r
156\r
157 if args.Verbose:\r
158 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections:')\r
159 print (' [PcdsFixedAtBuild]')\r
160 print (' [PcdsPatchableInModule]')\r
161 print (' [PcdsDynamicDefault]')\r
162 print (' [PcdsDynamicExDefault]')\r
163 elif args.PcdType == 'VPD':\r
164 if args.MaxSize is None:\r
165 #\r
166 # If --max-size is not provided, then set maximum size to the size of the\r
167 # binary input file\r
168 #\r
169 args.MaxSize = PcdSize\r
170 if args.MaxSize < PcdSize:\r
171 print ('BinToPcd: error: argument --max-size is smaller than input file.')\r
172 sys.exit (1)\r
173 if args.Offset is None:\r
174 #\r
175 # if --offset is not provided, then set offset field to '*' so build\r
176 # tools will compute offset of PCD in VPD region.\r
177 #\r
178 Pcd = ' {Name}|*|{Size}|{Value}'.format (Name = args.PcdName, Size = args.MaxSize, Value = PcdValue)\r
179 else:\r
180 #\r
181 # --offset value must be 8-byte aligned\r
182 #\r
183 if (args.Offset % 8) != 0:\r
184 print ('BinToPcd: error: argument --offset must be 8-byte aligned.')\r
185 sys.exit (1)\r
186 #\r
187 # Use the --offset value provided.\r
188 #\r
189 Pcd = ' {Name}|{Offset}|{Size}|{Value}'.format (Name = args.PcdName, Offset = args.Offset, Size = args.MaxSize, Value = PcdValue)\r
190 if args.Verbose:\r
191 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')\r
192 print (' [PcdsDynamicVpd]')\r
193 print (' [PcdsDynamicExVpd]')\r
194 elif args.PcdType == 'HII':\r
195 if args.VariableGuid is None or args.VariableName is None:\r
196 print ('BinToPcd: error: arguments --variable-guid and --variable-name are required for --type HII.')\r
197 sys.exit (1)\r
198 if args.Offset is None:\r
199 #\r
200 # Use UEFI Variable offset of 0 if --offset is not provided\r
201 #\r
202 args.Offset = 0\r
203 #\r
204 # --offset value must be 8-byte aligned\r
205 #\r
206 if (args.Offset % 8) != 0:\r
207 print ('BinToPcd: error: argument --offset must be 8-byte aligned.')\r
208 sys.exit (1)\r
209 Pcd = ' {Name}|L"{VarName}"|{VarGuid}|{Offset}|{Value}'.format (Name = args.PcdName, VarName = args.VariableName, VarGuid = args.VariableGuid, Offset = args.Offset, Value = PcdValue)\r
210 if args.Verbose:\r
211 print ('BinToPcd: Convert binary file to PCD statement compatible with PCD sections')\r
212 print (' [PcdsDynamicHii]')\r
213 print (' [PcdsDynamicExHii]')\r
fd0597aa 214\r
fd0597aa 215 #\r
0c805f4f 216 # Write PCD value or PCD statement to the output file\r
fd0597aa 217 #\r
0c805f4f
KM
218 try:\r
219 args.OutputFile.write (Pcd)\r
220 args.OutputFile.close ()\r
221 except:\r
222 #\r
223 # If output file is not specified or it can not be written, then write the\r
224 # PCD value or PCD statement to the console\r
225 #\r
226 print (Pcd)\r