]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Scripts/BinToPcd.py
BaseTools/BinToPcd: Add support for multiple binary input files
[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
17\r
18import sys\r
19import argparse\r
20import re\r
aedd1559 21import xdrlib\r
fd0597aa
MK
22\r
23#\r
24# Globals for help information\r
25#\r
26__prog__ = 'BinToPcd'\r
aedd1559
KM
27__version__ = '%s Version %s' % (__prog__, '0.91 ')\r
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
32 def ValidateUnsignedInteger (Argument):\r
33 try:\r
34 Value = int (Argument, 0)\r
35 except:\r
36 Message = '%s is not a valid integer value.' % (Argument)\r
37 raise argparse.ArgumentTypeError(Message)\r
38 if Value < 0:\r
39 Message = '%s is a negative value.' % (Argument)\r
40 raise argparse.ArgumentTypeError(Message)\r
41 return Value\r
42\r
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 = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' % (Argument)\r
46 raise argparse.ArgumentTypeError(Message)\r
47 return Argument\r
48\r
49 def ValidateGuidName (Argument):\r
50 if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:\r
51 Message = '%s is not a valid GUID C name' % (Argument)\r
52 raise argparse.ArgumentTypeError(Message)\r
53 return Argument\r
aedd1559
KM
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 = 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 = ''.join(Buffer)\r
fd0597aa 70 #\r
aedd1559 71 # Return a PCD value of the form '{0x01, 0x02, ...}' along with the PCD length in bytes\r
fd0597aa 72 #\r
aedd1559
KM
73 return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer])), len (Buffer)\r
74\r
fd0597aa
MK
75 #\r
76 # Create command line argument parser object\r
77 #\r
78 parser = argparse.ArgumentParser(prog = __prog__, version = __version__,\r
79 description = __description__ + __copyright__,\r
80 conflict_handler = 'resolve')\r
aedd1559
KM
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
fd0597aa
MK
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.")\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
aedd1559
KM
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
fd0597aa
MK
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
105\r
106 #\r
107 # Parse command line arguments\r
108 #\r
109 args = parser.parse_args()\r
110\r
111 #\r
aedd1559 112 # Read all binary input files\r
fd0597aa 113 #\r
aedd1559
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\r
121 sys.exit()\r
122\r
123 #\r
124 # Convert PCD to an encoded string of hex values and determine the size of\r
125 # the encoded PCD in bytes.\r
126 #\r
127 PcdValue, PcdSize = ByteArray (Buffer, args.Xdr)\r
fd0597aa
MK
128\r
129 #\r
130 # Convert binary buffer to a DSC file PCD statement\r
131 #\r
132 if args.PcdName is None:\r
133 #\r
134 # If PcdName is None, then only a PCD value is being requested.\r
aedd1559
KM
135 #\r
136 Pcd = PcdValue\r
fd0597aa
MK
137 if args.Verbose:\r
138 print 'PcdToBin: 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
aedd1559
KM
150 Pcd = ' %s|%s' % (args.PcdName, PcdValue)\r
151 elif args.MaxSize < PcdSize:\r
fd0597aa
MK
152 print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
153 sys.exit()\r
154 else:\r
aedd1559
KM
155 Pcd = ' %s|%s|VOID*|%d' % (args.PcdName, PcdValue, args.MaxSize)\r
156\r
fd0597aa
MK
157 if args.Verbose:\r
158 print 'PcdToBin: 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
aedd1559
KM
169 args.MaxSize = PcdSize\r
170 if args.MaxSize < PcdSize:\r
fd0597aa
MK
171 print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
172 sys.exit()\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
aedd1559 178 Pcd = ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, PcdValue)\r
fd0597aa
MK
179 else:\r
180 #\r
181 # Use the --offset value provided.\r
182 #\r
aedd1559 183 Pcd = ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, PcdValue)\r
fd0597aa
MK
184 if args.Verbose:\r
185 print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
186 print ' [PcdsDynamicVpd]'\r
187 print ' [PcdsDynamicExVpd]'\r
188 elif args.PcdType == 'HII':\r
189 if args.VariableGuid is None:\r
190 print 'BinToPcd: error: argument --variable-guid is required for --type HII.'\r
191 sys.exit()\r
192 if args.VariableName is None:\r
193 print 'BinToPcd: error: argument --variable-name is required for --type HII.'\r
194 sys.exit()\r
195 if args.Offset is None:\r
196 #\r
197 # Use UEFI Variable offset of 0 if --offset is not provided\r
198 #\r
199 args.Offset = 0\r
aedd1559 200 Pcd = ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, PcdValue)\r
fd0597aa
MK
201 if args.Verbose:\r
202 print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
203 print ' [PcdsDynamicHii]'\r
204 print ' [PcdsDynamicExHii]'\r
205\r
206 #\r
207 # Write PCD value or PCD statement to the output file\r
208 #\r
209 try:\r
210 args.OutputFile.write (Pcd)\r
211 args.OutputFile.close ()\r
212 except:\r
213 #\r
214 # If output file is not specified or it can not be written, then write the\r
215 # PCD value or PCD statement to the console\r
216 #\r
217 print Pcd\r