]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/PatchPcdValue/PatchPcdValue.py
BaseTools: use set instead of list for a variable to be used with in
[mirror_edk2.git] / BaseTools / Source / Python / PatchPcdValue / PatchPcdValue.py
CommitLineData
f51461c8
LG
1## @file\r
2# Patch value into the binary file.\r
3#\r
1be2ed90 4# Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>\r
f51461c8
LG
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
15# Import Modules\r
16#\r
1be2ed90
HC
17import Common.LongFilePathOs as os\r
18from Common.LongFilePathSupport import OpenLongFilePath as open\r
f51461c8
LG
19import sys\r
20import re\r
21\r
22from optparse import OptionParser\r
23from optparse import make_option\r
24from Common.BuildToolError import *\r
25import Common.EdkLogger as EdkLogger\r
26from Common.BuildVersion import gBUILD_VERSION\r
27import array\r
28\r
29# Version and Copyright\r
30__version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
31__version__ = "%prog Version " + __version_number__\r
32__copyright__ = "Copyright (c) 2010, Intel Corporation. All rights reserved."\r
33\r
34## PatchBinaryFile method\r
35#\r
36# This method mainly patches the data into binary file.\r
37# \r
38# @param FileName File path of the binary file\r
39# @param ValueOffset Offset value \r
40# @param TypeName DataType Name\r
41# @param Value Value String\r
42# @param MaxSize MaxSize value\r
43#\r
44# @retval 0 File is updated successfully.\r
45# @retval not 0 File is updated failed.\r
46#\r
47def PatchBinaryFile(FileName, ValueOffset, TypeName, ValueString, MaxSize=0):\r
48 #\r
49 # Length of Binary File\r
50 #\r
47fea6af 51 FileHandle = open(FileName, 'rb')\r
f51461c8
LG
52 FileHandle.seek (0, 2)\r
53 FileLength = FileHandle.tell()\r
54 FileHandle.close()\r
55 #\r
56 # Unify string to upper string\r
57 #\r
58 TypeName = TypeName.upper()\r
59 #\r
60 # Get PCD value data length\r
61 #\r
62 ValueLength = 0\r
63 if TypeName == 'BOOLEAN':\r
64 ValueLength = 1\r
65 elif TypeName == 'UINT8':\r
66 ValueLength = 1\r
67 elif TypeName == 'UINT16':\r
68 ValueLength = 2\r
69 elif TypeName == 'UINT32':\r
70 ValueLength = 4\r
71 elif TypeName == 'UINT64':\r
72 ValueLength = 8\r
73 elif TypeName == 'VOID*':\r
74 if MaxSize == 0:\r
75 return OPTION_MISSING, "PcdMaxSize is not specified for VOID* type PCD."\r
76 ValueLength = int(MaxSize)\r
77 else:\r
47fea6af 78 return PARAMETER_INVALID, "PCD type %s is not valid." % (CommandOptions.PcdTypeName)\r
f51461c8
LG
79 #\r
80 # Check PcdValue is in the input binary file.\r
81 #\r
82 if ValueOffset + ValueLength > FileLength:\r
83 return PARAMETER_INVALID, "PcdOffset + PcdMaxSize(DataType) is larger than the input file size."\r
84 #\r
85 # Read binary file into array\r
86 #\r
47fea6af 87 FileHandle = open(FileName, 'rb')\r
f51461c8
LG
88 ByteArray = array.array('B')\r
89 ByteArray.fromfile(FileHandle, FileLength)\r
90 FileHandle.close()\r
91 OrigByteList = ByteArray.tolist()\r
92 ByteList = ByteArray.tolist()\r
93 #\r
94 # Clear the data in file\r
95 #\r
96 for Index in range(ValueLength):\r
97 ByteList[ValueOffset + Index] = 0\r
98 #\r
99 # Patch value into offset\r
100 #\r
101 SavedStr = ValueString\r
102 ValueString = ValueString.upper()\r
103 ValueNumber = 0\r
104 if TypeName == 'BOOLEAN':\r
105 #\r
106 # Get PCD value for BOOLEAN data type\r
107 #\r
108 try:\r
109 if ValueString == 'TRUE':\r
110 ValueNumber = 1\r
111 elif ValueString == 'FALSE':\r
112 ValueNumber = 0\r
0944818a 113 ValueNumber = int (ValueString, 0)\r
f51461c8
LG
114 if ValueNumber != 0:\r
115 ValueNumber = 1\r
116 except:\r
47fea6af 117 return PARAMETER_INVALID, "PCD Value %s is not valid dec or hex string." % (ValueString)\r
f51461c8
LG
118 #\r
119 # Set PCD value into binary data\r
120 #\r
121 ByteList[ValueOffset] = ValueNumber\r
122 elif TypeName in ['UINT8', 'UINT16', 'UINT32', 'UINT64']:\r
123 #\r
124 # Get PCD value for UINT* data type\r
125 #\r
126 try:\r
0944818a 127 ValueNumber = int (ValueString, 0)\r
f51461c8 128 except:\r
47fea6af 129 return PARAMETER_INVALID, "PCD Value %s is not valid dec or hex string." % (ValueString)\r
f51461c8
LG
130 #\r
131 # Set PCD value into binary data\r
132 #\r
133 for Index in range(ValueLength):\r
134 ByteList[ValueOffset + Index] = ValueNumber % 0x100\r
135 ValueNumber = ValueNumber / 0x100\r
136 elif TypeName == 'VOID*':\r
137 ValueString = SavedStr\r
138 if ValueString.startswith('L"'):\r
139 #\r
140 # Patch Unicode String\r
141 #\r
142 Index = 0\r
143 for ByteString in ValueString[2:-1]:\r
144 #\r
145 # Reserve zero as unicode tail\r
146 #\r
147 if Index + 2 >= ValueLength:\r
148 break\r
149 #\r
150 # Set string value one by one\r
151 #\r
152 ByteList[ValueOffset + Index] = ord(ByteString)\r
153 Index = Index + 2\r
154 elif ValueString.startswith("{") and ValueString.endswith("}"):\r
155 #\r
156 # Patch {0x1, 0x2, ...} byte by byte\r
157 #\r
0537f332 158 ValueList = ValueString[1 : len(ValueString) - 1].split(',')\r
f51461c8
LG
159 Index = 0\r
160 try:\r
161 for ByteString in ValueList:\r
0537f332 162 ByteString = ByteString.strip()\r
f51461c8
LG
163 if ByteString.upper().startswith('0X'):\r
164 ByteValue = int(ByteString, 16)\r
165 else:\r
166 ByteValue = int(ByteString)\r
167 ByteList[ValueOffset + Index] = ByteValue % 0x100\r
168 Index = Index + 1\r
169 if Index >= ValueLength:\r
170 break\r
171 except:\r
47fea6af 172 return PARAMETER_INVALID, "PCD Value %s is not valid dec or hex string array." % (ValueString)\r
f51461c8
LG
173 else:\r
174 #\r
175 # Patch ascii string \r
176 #\r
177 Index = 0\r
178 for ByteString in ValueString[1:-1]:\r
179 #\r
180 # Reserve zero as string tail\r
181 #\r
182 if Index + 1 >= ValueLength:\r
183 break\r
184 #\r
185 # Set string value one by one\r
186 #\r
187 ByteList[ValueOffset + Index] = ord(ByteString)\r
188 Index = Index + 1\r
189 #\r
190 # Update new data into input file.\r
191 #\r
192 if ByteList != OrigByteList:\r
193 ByteArray = array.array('B')\r
194 ByteArray.fromlist(ByteList)\r
47fea6af 195 FileHandle = open(FileName, 'wb')\r
f51461c8
LG
196 ByteArray.tofile(FileHandle)\r
197 FileHandle.close()\r
47fea6af 198 return 0, "Patch Value into File %s successfully." % (FileName)\r
f51461c8
LG
199\r
200## Parse command line options\r
201#\r
202# Using standard Python module optparse to parse command line option of this tool.\r
203#\r
204# @retval Options A optparse.Values object containing the parsed options\r
205# @retval InputFile Path of file to be trimmed\r
206#\r
207def Options():\r
208 OptionList = [\r
209 make_option("-f", "--offset", dest="PcdOffset", action="store", type="int",\r
210 help="Start offset to the image is used to store PCD value."),\r
211 make_option("-u", "--value", dest="PcdValue", action="store",\r
212 help="PCD value will be updated into the image."),\r
213 make_option("-t", "--type", dest="PcdTypeName", action="store",\r
214 help="The name of PCD data type may be one of VOID*,BOOLEAN, UINT8, UINT16, UINT32, UINT64."),\r
215 make_option("-s", "--maxsize", dest="PcdMaxSize", action="store", type="int",\r
216 help="Max size of data buffer is taken by PCD value.It must be set when PCD type is VOID*."),\r
217 make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,\r
218 help="Run verbosely"),\r
219 make_option("-d", "--debug", dest="LogLevel", type="int",\r
220 help="Run with debug information"),\r
221 make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,\r
222 help="Run quietly"),\r
223 make_option("-?", action="help", help="show this help message and exit"),\r
224 ]\r
225\r
226 # use clearer usage to override default usage message\r
227 UsageString = "%prog -f Offset -u Value -t Type [-s MaxSize] <input_file>"\r
228\r
229 Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)\r
230 Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
231\r
232 Options, Args = Parser.parse_args()\r
233\r
234 # error check\r
235 if len(Args) == 0:\r
236 EdkLogger.error("PatchPcdValue", PARAMETER_INVALID, ExtraData=Parser.get_usage())\r
237\r
238 InputFile = Args[len(Args) - 1]\r
239 return Options, InputFile\r
240\r
241## Entrance method\r
242#\r
243# This method mainly dispatch specific methods per the command line options.\r
244# If no error found, return zero value so the caller of this tool can know\r
245# if it's executed successfully or not.\r
246#\r
247# @retval 0 Tool was successful\r
248# @retval 1 Tool failed\r
249#\r
250def Main():\r
251 try:\r
252 #\r
253 # Check input parameter\r
254 #\r
255 EdkLogger.Initialize()\r
256 CommandOptions, InputFile = Options()\r
257 if CommandOptions.LogLevel < EdkLogger.DEBUG_9:\r
258 EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
259 else:\r
260 EdkLogger.SetLevel(CommandOptions.LogLevel)\r
261 if not os.path.exists (InputFile):\r
262 EdkLogger.error("PatchPcdValue", FILE_NOT_FOUND, ExtraData=InputFile)\r
263 return 1\r
4231a819 264 if CommandOptions.PcdOffset is None or CommandOptions.PcdValue is None or CommandOptions.PcdTypeName is None:\r
f51461c8
LG
265 EdkLogger.error("PatchPcdValue", OPTION_MISSING, ExtraData="PcdOffset or PcdValue of PcdTypeName is not specified.")\r
266 return 1\r
267 if CommandOptions.PcdTypeName.upper() not in ["BOOLEAN", "UINT8", "UINT16", "UINT32", "UINT64", "VOID*"]:\r
47fea6af 268 EdkLogger.error("PatchPcdValue", PARAMETER_INVALID, ExtraData="PCD type %s is not valid." % (CommandOptions.PcdTypeName))\r
f51461c8 269 return 1\r
4231a819 270 if CommandOptions.PcdTypeName.upper() == "VOID*" and CommandOptions.PcdMaxSize is None:\r
f51461c8
LG
271 EdkLogger.error("PatchPcdValue", OPTION_MISSING, ExtraData="PcdMaxSize is not specified for VOID* type PCD.")\r
272 return 1\r
273 #\r
274 # Patch value into binary image.\r
275 #\r
276 ReturnValue, ErrorInfo = PatchBinaryFile (InputFile, CommandOptions.PcdOffset, CommandOptions.PcdTypeName, CommandOptions.PcdValue, CommandOptions.PcdMaxSize)\r
277 if ReturnValue != 0:\r
278 EdkLogger.error("PatchPcdValue", ReturnValue, ExtraData=ErrorInfo)\r
279 return 1\r
280 return 0\r
281 except:\r
282 return 1\r
283\r
284if __name__ == '__main__':\r
285 r = Main()\r
286 sys.exit(r)\r