BaseTools/Scripts: Add BinToPcd utility
[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
4# Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
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
21\r
22#\r
23# Globals for help information\r
24#\r
25__prog__ = 'BinToPcd'\r
26__version__ = '%s Version %s' % (__prog__, '0.9 ')\r
27__copyright__ = 'Copyright (c) 2016, Intel Corporation. All rights reserved.'\r
28__description__ = 'Convert a binary file to a VOID* PCD value or DSC file VOID* PCD statement.\n'\r
29\r
30if __name__ == '__main__':\r
31 def ValidateUnsignedInteger (Argument):\r
32 try:\r
33 Value = int (Argument, 0)\r
34 except:\r
35 Message = '%s is not a valid integer value.' % (Argument)\r
36 raise argparse.ArgumentTypeError(Message)\r
37 if Value < 0:\r
38 Message = '%s is a negative value.' % (Argument)\r
39 raise argparse.ArgumentTypeError(Message)\r
40 return Value\r
41\r
42 def ValidatePcdName (Argument):\r
43 if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*\.[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:\r
44 Message = '%s is not in the form <PcdTokenSpaceGuidCName>.<PcdCName>' % (Argument)\r
45 raise argparse.ArgumentTypeError(Message)\r
46 return Argument\r
47\r
48 def ValidateGuidName (Argument):\r
49 if re.split('[a-zA-Z\_][a-zA-Z0-9\_]*', Argument) <> ['','']:\r
50 Message = '%s is not a valid GUID C name' % (Argument)\r
51 raise argparse.ArgumentTypeError(Message)\r
52 return Argument\r
53 \r
54 def ByteArray (Buffer):\r
55 #\r
56 # Append byte array of values of the form '{0x01, 0x02, ...}'\r
57 #\r
58 return '{%s}' % (', '.join(['0x%02x' % (ord(Item)) for Item in Buffer]))\r
59 \r
60 #\r
61 # Create command line argument parser object\r
62 #\r
63 parser = argparse.ArgumentParser(prog = __prog__, version = __version__,\r
64 description = __description__ + __copyright__,\r
65 conflict_handler = 'resolve')\r
66 parser.add_argument("-i", "--input", dest = 'InputFile', type = argparse.FileType('rb'),\r
67 help = "Input binary filename", required = True)\r
68 parser.add_argument("-o", "--output", dest = 'OutputFile', type = argparse.FileType('wb'),\r
69 help = "Output filename for PCD value or PCD statement")\r
70 parser.add_argument("-p", "--pcd", dest = 'PcdName', type = ValidatePcdName,\r
71 help = "Name of the PCD in the form <PcdTokenSpaceGuidCName>.<PcdCName>")\r
72 parser.add_argument("-t", "--type", dest = 'PcdType', default = None, choices = ['VPD','HII'],\r
73 help = "PCD statement type (HII or VPD). Default is standard.")\r
74 parser.add_argument("-m", "--max-size", dest = 'MaxSize', type = ValidateUnsignedInteger,\r
75 help = "Maximum size of the PCD. Ignored with --type HII.")\r
76 parser.add_argument("-f", "--offset", dest = 'Offset', type = ValidateUnsignedInteger,\r
77 help = "VPD offset if --type is VPD. UEFI Variable offset if --type is HII.")\r
78 parser.add_argument("-n", "--variable-name", dest = 'VariableName',\r
79 help = "UEFI variable name. Only used with --type HII.")\r
80 parser.add_argument("-g", "--variable-guid", type = ValidateGuidName, dest = 'VariableGuid',\r
81 help = "UEFI variable GUID C name. Only used with --type HII.")\r
82 parser.add_argument("-v", "--verbose", dest = 'Verbose', action = "store_true",\r
83 help = "Increase output messages")\r
84 parser.add_argument("-q", "--quiet", dest = 'Quiet', action = "store_true",\r
85 help = "Reduce output messages")\r
86 parser.add_argument("--debug", dest = 'Debug', type = int, metavar = '[0-9]', choices = range(0,10), default = 0,\r
87 help = "Set debug level")\r
88\r
89 #\r
90 # Parse command line arguments\r
91 #\r
92 args = parser.parse_args()\r
93\r
94 #\r
95 # Read binary input file\r
96 #\r
97 try:\r
98 Buffer = args.InputFile.read()\r
99 args.InputFile.close()\r
100 except:\r
101 print 'BinToPcd: error: can not read binary input file'\r
102 sys.exit()\r
103\r
104 #\r
105 # Convert binary buffer to a DSC file PCD statement\r
106 #\r
107 if args.PcdName is None:\r
108 #\r
109 # If PcdName is None, then only a PCD value is being requested.\r
110 Pcd = ByteArray (Buffer)\r
111 if args.Verbose:\r
112 print 'PcdToBin: Convert binary file to PCD Value'\r
113 elif args.PcdType is None:\r
114 #\r
115 # If --type is neither VPD nor HII, then use PCD statement syntax that is\r
116 # compatible with [PcdsFixedAtBuild], [PcdsPatchableInModule],\r
117 # [PcdsDynamicDefault], and [PcdsDynamicExDefault].\r
118 #\r
119 if args.MaxSize is None:\r
120 #\r
121 # If --max-size is not provided, then do not generate the syntax that\r
122 # includes the maximum size.\r
123 #\r
124 Pcd = ' %s|%s' % (args.PcdName, ByteArray (Buffer))\r
125 elif args.MaxSize < len(Buffer):\r
126 print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
127 sys.exit()\r
128 else:\r
129 Pcd = ' %s|%s|VOID*|%d' % (args.PcdName, ByteArray (Buffer), args.MaxSize)\r
130 args.MaxSize = len(Buffer)\r
131 \r
132 if args.Verbose:\r
133 print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections:'\r
134 print ' [PcdsFixedAtBuild]'\r
135 print ' [PcdsPatchableInModule]'\r
136 print ' [PcdsDynamicDefault]'\r
137 print ' [PcdsDynamicExDefault]'\r
138 elif args.PcdType == 'VPD':\r
139 if args.MaxSize is None:\r
140 #\r
141 # If --max-size is not provided, then set maximum size to the size of the\r
142 # binary input file\r
143 #\r
144 args.MaxSize = len(Buffer)\r
145 if args.MaxSize < len(Buffer):\r
146 print 'BinToPcd: error: argument --max-size is smaller than input file.'\r
147 sys.exit()\r
148 if args.Offset is None:\r
149 #\r
150 # if --offset is not provided, then set offset field to '*' so build\r
151 # tools will compute offset of PCD in VPD region.\r
152 #\r
153 Pcd = ' %s|*|%d|%s' % (args.PcdName, args.MaxSize, ByteArray (Buffer))\r
154 else:\r
155 #\r
156 # Use the --offset value provided.\r
157 #\r
158 Pcd = ' %s|%d|%d|%s' % (args.PcdName, args.Offset, args.MaxSize, ByteArray (Buffer))\r
159 if args.Verbose:\r
160 print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
161 print ' [PcdsDynamicVpd]'\r
162 print ' [PcdsDynamicExVpd]'\r
163 elif args.PcdType == 'HII':\r
164 if args.VariableGuid is None:\r
165 print 'BinToPcd: error: argument --variable-guid is required for --type HII.'\r
166 sys.exit()\r
167 if args.VariableName is None:\r
168 print 'BinToPcd: error: argument --variable-name is required for --type HII.'\r
169 sys.exit()\r
170 if args.Offset is None:\r
171 #\r
172 # Use UEFI Variable offset of 0 if --offset is not provided\r
173 #\r
174 args.Offset = 0\r
175 Pcd = ' %s|L"%s"|%s|%d|%s' % (args.PcdName, args.VariableName, args.VariableGuid, args.Offset, ByteArray (Buffer))\r
176 if args.Verbose:\r
177 print 'PcdToBin: Convert binary file to PCD statement compatible with PCD sections'\r
178 print ' [PcdsDynamicHii]'\r
179 print ' [PcdsDynamicExHii]'\r
180\r
181 #\r
182 # Write PCD value or PCD statement to the output file\r
183 #\r
184 try:\r
185 args.OutputFile.write (Pcd)\r
186 args.OutputFile.close ()\r
187 except:\r
188 #\r
189 # If output file is not specified or it can not be written, then write the\r
190 # PCD value or PCD statement to the console\r
191 #\r
192 print Pcd\r