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