2 # Patch value into the binary file.
4 # Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 import Common
.LongFilePathOs
as os
12 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
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
22 from Common
.DataType
import *
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."
29 ## PatchBinaryFile method
31 # This method mainly patches the data into binary file.
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
39 # @retval 0 File is updated successfully.
40 # @retval not 0 File is updated failed.
42 def PatchBinaryFile(FileName
, ValueOffset
, TypeName
, ValueString
, MaxSize
=0):
44 # Length of Binary File
46 FileHandle
= open(FileName
, 'rb')
47 FileHandle
.seek (0, 2)
48 FileLength
= FileHandle
.tell()
51 # Unify string to upper string
53 TypeName
= TypeName
.upper()
55 # Get PCD value data length
58 if TypeName
== 'BOOLEAN':
60 elif TypeName
== TAB_UINT8
:
62 elif TypeName
== TAB_UINT16
:
64 elif TypeName
== TAB_UINT32
:
66 elif TypeName
== TAB_UINT64
:
68 elif TypeName
== TAB_VOID
:
70 return OPTION_MISSING
, "PcdMaxSize is not specified for VOID* type PCD."
71 ValueLength
= int(MaxSize
)
73 return PARAMETER_INVALID
, "PCD type %s is not valid." % (CommandOptions
.PcdTypeName
)
75 # Check PcdValue is in the input binary file.
77 if ValueOffset
+ ValueLength
> FileLength
:
78 return PARAMETER_INVALID
, "PcdOffset + PcdMaxSize(DataType) is larger than the input file size."
80 # Read binary file into array
82 FileHandle
= open(FileName
, 'rb')
83 ByteArray
= array
.array('B')
84 ByteArray
.fromfile(FileHandle
, FileLength
)
86 OrigByteList
= ByteArray
.tolist()
87 ByteList
= ByteArray
.tolist()
89 # Clear the data in file
91 for Index
in range(ValueLength
):
92 ByteList
[ValueOffset
+ Index
] = 0
94 # Patch value into offset
96 SavedStr
= ValueString
97 ValueString
= ValueString
.upper()
99 if TypeName
== 'BOOLEAN':
101 # Get PCD value for BOOLEAN data type
104 if ValueString
== 'TRUE':
106 elif ValueString
== 'FALSE':
108 ValueNumber
= int (ValueString
, 0)
112 return PARAMETER_INVALID
, "PCD Value %s is not valid dec or hex string." % (ValueString
)
114 # Set PCD value into binary data
116 ByteList
[ValueOffset
] = ValueNumber
117 elif TypeName
in TAB_PCD_CLEAN_NUMERIC_TYPES
:
119 # Get PCD value for UINT* data type
122 ValueNumber
= int (ValueString
, 0)
124 return PARAMETER_INVALID
, "PCD Value %s is not valid dec or hex string." % (ValueString
)
126 # Set PCD value into binary data
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"'):
135 # Patch Unicode String
138 for ByteString
in ValueString
[2:-1]:
140 # Reserve zero as unicode tail
142 if Index
+ 2 >= ValueLength
:
145 # Set string value one by one/ 0x100
147 ByteList
[ValueOffset
+ Index
] = ord(ByteString
)
149 elif ValueString
.startswith("{") and ValueString
.endswith("}"):
151 # Patch {0x1, 0x2, ...} byte by byte
153 ValueList
= ValueString
[1 : len(ValueString
) - 1].split(',')
156 for ByteString
in ValueList
:
157 ByteString
= ByteString
.strip()
158 if ByteString
.upper().startswith('0X'):
159 ByteValue
= int(ByteString
, 16)
161 ByteValue
= int(ByteString
)
162 ByteList
[ValueOffset
+ Index
] = ByteValue
% 0x100
164 if Index
>= ValueLength
:
167 return PARAMETER_INVALID
, "PCD Value %s is not valid dec or hex string array." % (ValueString
)
173 for ByteString
in ValueString
[1:-1]:
175 # Reserve zero as string tail
177 if Index
+ 1 >= ValueLength
:
180 # Set string value one by one
182 ByteList
[ValueOffset
+ Index
] = ord(ByteString
)
185 # Update new data into input file.
187 if ByteList
!= OrigByteList
:
188 ByteArray
= array
.array('B')
189 ByteArray
.fromlist(ByteList
)
190 FileHandle
= open(FileName
, 'wb')
191 ByteArray
.tofile(FileHandle
)
193 return 0, "Patch Value into File %s successfully." % (FileName
)
195 ## Parse command line options
197 # Using standard Python module optparse to parse command line option of this tool.
199 # @retval Options A optparse.Values object containing the parsed options
200 # @retval InputFile Path of file to be trimmed
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
,
218 make_option("-?", action
="help", help="show this help message and exit"),
221 # use clearer usage to override default usage message
222 UsageString
= "%prog -f Offset -u Value -t Type [-s MaxSize] <input_file>"
224 Parser
= OptionParser(description
=__copyright__
, version
=__version__
, option_list
=OptionList
, usage
=UsageString
)
225 Parser
.set_defaults(LogLevel
=EdkLogger
.INFO
)
227 Options
, Args
= Parser
.parse_args()
231 EdkLogger
.error("PatchPcdValue", PARAMETER_INVALID
, ExtraData
=Parser
.get_usage())
233 InputFile
= Args
[len(Args
) - 1]
234 return Options
, InputFile
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.
242 # @retval 0 Tool was successful
243 # @retval 1 Tool failed
248 # Check input parameter
250 EdkLogger
.Initialize()
251 CommandOptions
, InputFile
= Options()
252 if CommandOptions
.LogLevel
< EdkLogger
.DEBUG_9
:
253 EdkLogger
.SetLevel(CommandOptions
.LogLevel
+ 1)
255 EdkLogger
.SetLevel(CommandOptions
.LogLevel
)
256 if not os
.path
.exists (InputFile
):
257 EdkLogger
.error("PatchPcdValue", FILE_NOT_FOUND
, ExtraData
=InputFile
)
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.")
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
))
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.")
269 # Patch value into binary image.
271 ReturnValue
, ErrorInfo
= PatchBinaryFile (InputFile
, CommandOptions
.PcdOffset
, CommandOptions
.PcdTypeName
, CommandOptions
.PcdValue
, CommandOptions
.PcdMaxSize
)
273 EdkLogger
.error("PatchPcdValue", ReturnValue
, ExtraData
=ErrorInfo
)
279 if __name__
== '__main__':