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