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