]>
Commit | Line | Data |
---|---|---|
c8ec22a2 JY |
1 | ## @ PatchFv.py\r |
2 | #\r | |
9da59186 | 3 | # Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>\r |
16a16ea6 | 4 | # SPDX-License-Identifier: BSD-2-Clause-Patent\r |
c8ec22a2 JY |
5 | #\r |
6 | ##\r | |
7 | \r | |
8 | import os\r | |
9 | import re\r | |
10 | import sys\r | |
11 | \r | |
fb9819f1 JY |
12 | #\r |
13 | # Read data from file\r | |
14 | #\r | |
15 | # param [in] binfile Binary file\r | |
16 | # param [in] offset Offset\r | |
17 | # param [in] len Length\r | |
18 | #\r | |
19 | # retval value Value\r | |
20 | #\r | |
c8ec22a2 JY |
21 | def readDataFromFile (binfile, offset, len=1):\r |
22 | fd = open(binfile, "r+b")\r | |
23 | fsize = os.path.getsize(binfile)\r | |
24 | offval = offset & 0xFFFFFFFF\r | |
25 | if (offval & 0x80000000):\r | |
26 | offval = fsize - (0xFFFFFFFF - offval + 1)\r | |
27 | fd.seek(offval)\r | |
28 | bytearray = [ord(b) for b in fd.read(len)]\r | |
9da59186 JY |
29 | value = 0\r |
30 | idx = len - 1\r | |
c8ec22a2 JY |
31 | while idx >= 0:\r |
32 | value = value << 8 | bytearray[idx]\r | |
33 | idx = idx - 1\r | |
34 | fd.close()\r | |
35 | return value\r | |
36 | \r | |
fb9819f1 JY |
37 | #\r |
38 | # Check FSP header is valid or not\r | |
39 | #\r | |
40 | # param [in] binfile Binary file\r | |
41 | #\r | |
42 | # retval boolean True: valid; False: invalid\r | |
43 | #\r | |
9da59186 JY |
44 | def IsFspHeaderValid (binfile):\r |
45 | fd = open (binfile, "rb")\r | |
fb9819f1 | 46 | bindat = fd.read(0x200) # only read first 0x200 bytes\r |
9da59186 | 47 | fd.close()\r |
fb9819f1 | 48 | HeaderList = ['FSPH' , 'FSPP' , 'FSPE'] # Check 'FSPH', 'FSPP', and 'FSPE' in the FSP header\r |
9da59186 JY |
49 | OffsetList = []\r |
50 | for each in HeaderList:\r | |
51 | if each in bindat:\r | |
52 | idx = bindat.index(each)\r | |
53 | else:\r | |
54 | idx = 0\r | |
55 | OffsetList.append(idx)\r | |
fb9819f1 | 56 | if not OffsetList[0] or not OffsetList[1]: # If 'FSPH' or 'FSPP' is missing, it will return false\r |
9da59186 JY |
57 | return False\r |
58 | Revision = ord(bindat[OffsetList[0] + 0x0B])\r | |
fb9819f1 JY |
59 | #\r |
60 | # if revision is bigger than 1, it means it is FSP v1.1 or greater revision, which must contain 'FSPE'.\r | |
61 | #\r | |
9da59186 | 62 | if Revision > 1 and not OffsetList[2]:\r |
fb9819f1 | 63 | return False # If FSP v1.1 or greater without 'FSPE', then return false\r |
9da59186 JY |
64 | return True\r |
65 | \r | |
fb9819f1 JY |
66 | #\r |
67 | # Patch data in file\r | |
68 | #\r | |
69 | # param [in] binfile Binary file\r | |
70 | # param [in] offset Offset\r | |
71 | # param [in] value Patch value\r | |
72 | # param [in] len Length\r | |
73 | #\r | |
74 | # retval len Length\r | |
75 | #\r | |
c8ec22a2 JY |
76 | def patchDataInFile (binfile, offset, value, len=1):\r |
77 | fd = open(binfile, "r+b")\r | |
78 | fsize = os.path.getsize(binfile)\r | |
79 | offval = offset & 0xFFFFFFFF\r | |
80 | if (offval & 0x80000000):\r | |
81 | offval = fsize - (0xFFFFFFFF - offval + 1)\r | |
82 | bytearray = []\r | |
9da59186 | 83 | idx = 0\r |
c8ec22a2 JY |
84 | while idx < len:\r |
85 | bytearray.append(value & 0xFF)\r | |
86 | value = value >> 8\r | |
87 | idx = idx + 1\r | |
88 | fd.seek(offval)\r | |
89 | fd.write("".join(chr(b) for b in bytearray))\r | |
90 | fd.close()\r | |
9da59186 | 91 | return len\r |
c8ec22a2 JY |
92 | \r |
93 | \r | |
94 | class Symbols:\r | |
95 | def __init__(self):\r | |
96 | self.dictSymbolAddress = {}\r | |
97 | self.dictGuidNameXref = {}\r | |
98 | self.dictFfsOffset = {}\r | |
99 | self.dictVariable = {}\r | |
100 | self.dictModBase = {}\r | |
101 | self.fdFile = None\r | |
102 | self.string = ""\r | |
103 | self.fdBase = 0xFFFFFFFF\r | |
104 | self.fdSize = 0\r | |
105 | self.index = 0\r | |
106 | self.parenthesisOpenSet = '([{<'\r | |
107 | self.parenthesisCloseSet = ')]}>'\r | |
108 | \r | |
fb9819f1 JY |
109 | #\r |
110 | # Get FD file\r | |
111 | #\r | |
112 | # retval self.fdFile Retrieve FD file\r | |
113 | #\r | |
c8ec22a2 JY |
114 | def getFdFile (self):\r |
115 | return self.fdFile\r | |
116 | \r | |
fb9819f1 JY |
117 | #\r |
118 | # Get FD size\r | |
119 | #\r | |
120 | # retval self.fdSize Retrieve the size of FD file\r | |
121 | #\r | |
c8ec22a2 JY |
122 | def getFdSize (self):\r |
123 | return self.fdSize\r | |
124 | \r | |
fb9819f1 JY |
125 | #\r |
126 | # Create dictionaries\r | |
127 | #\r | |
128 | # param [in] fvDir FV's directory\r | |
129 | # param [in] fvNames All FV's names\r | |
130 | #\r | |
131 | # retval 0 Created dictionaries successfully\r | |
132 | #\r | |
c8ec22a2 | 133 | def createDicts (self, fvDir, fvNames):\r |
fb9819f1 JY |
134 | #\r |
135 | # If the fvDir is not a dirctory, then raise an exception\r | |
136 | #\r | |
c8ec22a2 JY |
137 | if not os.path.isdir(fvDir):\r |
138 | raise Exception ("'%s' is not a valid directory!" % FvDir)\r | |
139 | \r | |
fb9819f1 JY |
140 | #\r |
141 | # If the Guid.xref is not existing in fvDir, then raise an exception\r | |
142 | #\r | |
c8ec22a2 JY |
143 | xrefFile = os.path.join(fvDir, "Guid.xref")\r |
144 | if not os.path.exists(xrefFile):\r | |
145 | raise Exception("Cannot open GUID Xref file '%s'!" % xrefFile)\r | |
146 | \r | |
fb9819f1 JY |
147 | #\r |
148 | # Add GUID reference to dictionary\r | |
149 | #\r | |
c8ec22a2 JY |
150 | self.dictGuidNameXref = {}\r |
151 | self.parseGuidXrefFile(xrefFile)\r | |
152 | \r | |
fb9819f1 JY |
153 | #\r |
154 | # Split up each FV from fvNames and get the fdBase\r | |
155 | #\r | |
c8ec22a2 JY |
156 | fvList = fvNames.split(":")\r |
157 | fdBase = fvList.pop()\r | |
158 | if len(fvList) == 0:\r | |
159 | fvList.append(fdBase)\r | |
160 | \r | |
fb9819f1 JY |
161 | #\r |
162 | # If the FD file is not existing, then raise an exception\r | |
163 | #\r | |
c8ec22a2 JY |
164 | fdFile = os.path.join(fvDir, fdBase.strip() + ".fd")\r |
165 | if not os.path.exists(fdFile):\r | |
166 | raise Exception("Cannot open FD file '%s'!" % fdFile)\r | |
167 | \r | |
fb9819f1 JY |
168 | #\r |
169 | # Get the size of the FD file\r | |
170 | #\r | |
c8ec22a2 JY |
171 | self.fdFile = fdFile\r |
172 | self.fdSize = os.path.getsize(fdFile)\r | |
173 | \r | |
fb9819f1 JY |
174 | #\r |
175 | # If the INF file, which is the first element of fvList, is not existing, then raise an exception\r | |
176 | #\r | |
c8ec22a2 JY |
177 | infFile = os.path.join(fvDir, fvList[0].strip()) + ".inf"\r |
178 | if not os.path.exists(infFile):\r | |
179 | raise Exception("Cannot open INF file '%s'!" % infFile)\r | |
180 | \r | |
fb9819f1 JY |
181 | #\r |
182 | # Parse INF file in order to get fdBase and then assign those values to dictVariable\r | |
183 | #\r | |
c8ec22a2 | 184 | self.parseInfFile(infFile)\r |
c8ec22a2 JY |
185 | self.dictVariable = {}\r |
186 | self.dictVariable["FDSIZE"] = self.fdSize\r | |
187 | self.dictVariable["FDBASE"] = self.fdBase\r | |
188 | \r | |
fb9819f1 JY |
189 | #\r |
190 | # Collect information from FV MAP file and FV TXT file then\r | |
191 | # put them into dictionaries\r | |
192 | #\r | |
c8ec22a2 JY |
193 | self.dictSymbolAddress = {}\r |
194 | self.dictFfsOffset = {}\r | |
195 | for file in fvList:\r | |
196 | \r | |
fb9819f1 JY |
197 | #\r |
198 | # If the .Fv.map file is not existing, then raise an exception.\r | |
199 | # Otherwise, parse FV MAP file\r | |
200 | #\r | |
c8ec22a2 JY |
201 | fvFile = os.path.join(fvDir, file.strip()) + ".Fv"\r |
202 | mapFile = fvFile + ".map"\r | |
203 | if not os.path.exists(mapFile):\r | |
204 | raise Exception("Cannot open MAP file '%s'!" % mapFile)\r | |
205 | \r | |
206 | self.parseFvMapFile(mapFile)\r | |
207 | \r | |
fb9819f1 JY |
208 | #\r |
209 | # If the .Fv.txt file is not existing, then raise an exception.\r | |
210 | # Otherwise, parse FV TXT file\r | |
211 | #\r | |
c8ec22a2 JY |
212 | fvTxtFile = fvFile + ".txt"\r |
213 | if not os.path.exists(fvTxtFile):\r | |
214 | raise Exception("Cannot open FV TXT file '%s'!" % fvTxtFile)\r | |
215 | \r | |
216 | self.parseFvTxtFile(fvTxtFile)\r | |
217 | \r | |
fb9819f1 JY |
218 | #\r |
219 | # Search all MAP files in FFS directory if it exists then parse MOD MAP file\r | |
220 | #\r | |
c8ec22a2 JY |
221 | ffsDir = os.path.join(fvDir, "Ffs")\r |
222 | if (os.path.isdir(ffsDir)):\r | |
223 | for item in os.listdir(ffsDir):\r | |
224 | if len(item) <= 0x24:\r | |
225 | continue\r | |
226 | mapFile =os.path.join(ffsDir, item, "%s.map" % item[0:0x24])\r | |
227 | if not os.path.exists(mapFile):\r | |
228 | continue\r | |
229 | self.parseModMapFile(item[0x24:], mapFile)\r | |
230 | \r | |
231 | return 0\r | |
232 | \r | |
fb9819f1 JY |
233 | #\r |
234 | # Get FV offset in FD file\r | |
235 | #\r | |
236 | # param [in] fvFile FV file\r | |
237 | #\r | |
238 | # retval offset Got FV offset successfully\r | |
239 | #\r | |
c8ec22a2 | 240 | def getFvOffsetInFd(self, fvFile):\r |
fb9819f1 JY |
241 | #\r |
242 | # Check if the first 0x70 bytes of fvFile can be found in fdFile\r | |
243 | #\r | |
c8ec22a2 JY |
244 | fvHandle = open(fvFile, "r+b")\r |
245 | fdHandle = open(self.fdFile, "r+b")\r | |
246 | offset = fdHandle.read().find(fvHandle.read(0x70))\r | |
247 | fvHandle.close()\r | |
248 | fdHandle.close()\r | |
249 | if offset == -1:\r | |
250 | raise Exception("Could not locate FV file %s in FD!" % fvFile)\r | |
251 | return offset\r | |
252 | \r | |
fb9819f1 JY |
253 | #\r |
254 | # Parse INF file\r | |
255 | #\r | |
256 | # param [in] infFile INF file\r | |
257 | #\r | |
258 | # retval 0 Parsed INF file successfully\r | |
259 | #\r | |
c8ec22a2 | 260 | def parseInfFile(self, infFile):\r |
fb9819f1 JY |
261 | #\r |
262 | # Get FV offset and search EFI_BASE_ADDRESS in the FD file \r | |
263 | # then assign the value of EFI_BASE_ADDRESS to fdBase\r | |
264 | #\r | |
c8ec22a2 JY |
265 | fvOffset = self.getFvOffsetInFd(infFile[0:-4] + ".Fv")\r |
266 | fdIn = open(infFile, "r")\r | |
267 | rptLine = fdIn.readline()\r | |
268 | self.fdBase = 0xFFFFFFFF\r | |
269 | while (rptLine != "" ):\r | |
270 | #EFI_BASE_ADDRESS = 0xFFFDF400\r | |
271 | match = re.match("^EFI_BASE_ADDRESS\s*=\s*(0x[a-fA-F0-9]+)", rptLine)\r | |
272 | if match is not None:\r | |
273 | self.fdBase = int(match.group(1), 16) - fvOffset\r | |
274 | rptLine = fdIn.readline()\r | |
275 | fdIn.close()\r | |
276 | if self.fdBase == 0xFFFFFFFF:\r | |
277 | raise Exception("Could not find EFI_BASE_ADDRESS in INF file!" % fvFile)\r | |
278 | return 0\r | |
279 | \r | |
fb9819f1 JY |
280 | #\r |
281 | # Parse FV TXT file\r | |
282 | #\r | |
283 | # param [in] fvTxtFile .Fv.txt file\r | |
284 | #\r | |
285 | # retval 0 Parsed FV TXT file successfully\r | |
286 | #\r | |
c8ec22a2 | 287 | def parseFvTxtFile(self, fvTxtFile):\r |
fb9819f1 JY |
288 | #\r |
289 | # Get information from .Fv.txt in order to create a dictionary\r | |
290 | # For example,\r | |
291 | # self.dictFfsOffset[912740BE-2284-4734-B971-84B027353F0C] = 0x000D4078\r | |
292 | #\r | |
c8ec22a2 JY |
293 | fvOffset = self.getFvOffsetInFd(fvTxtFile[0:-4])\r |
294 | fdIn = open(fvTxtFile, "r")\r | |
295 | rptLine = fdIn.readline()\r | |
296 | while (rptLine != "" ):\r | |
297 | match = re.match("(0x[a-fA-F0-9]+)\s([0-9a-fA-F\-]+)", rptLine)\r | |
298 | if match is not None:\r | |
299 | self.dictFfsOffset[match.group(2)] = "0x%08X" % (int(match.group(1), 16) + fvOffset)\r | |
300 | rptLine = fdIn.readline()\r | |
301 | fdIn.close()\r | |
302 | return 0\r | |
303 | \r | |
fb9819f1 JY |
304 | #\r |
305 | # Parse FV MAP file\r | |
306 | #\r | |
307 | # param [in] mapFile .Fv.map file\r | |
308 | #\r | |
309 | # retval 0 Parsed FV MAP file successfully\r | |
310 | #\r | |
c8ec22a2 | 311 | def parseFvMapFile(self, mapFile):\r |
fb9819f1 JY |
312 | #\r |
313 | # Get information from .Fv.map in order to create dictionaries\r | |
314 | # For example,\r | |
315 | # self.dictModBase[FspSecCore:BASE] = 4294592776 (0xfffa4908)\r | |
316 | # self.dictModBase[FspSecCore:ENTRY] = 4294606552 (0xfffa7ed8)\r | |
317 | # self.dictModBase[FspSecCore:TEXT] = 4294593080 (0xfffa4a38)\r | |
318 | # self.dictModBase[FspSecCore:DATA] = 4294612280 (0xfffa9538)\r | |
319 | # self.dictSymbolAddress[FspSecCore:_SecStartup] = 0x00fffa4a38\r | |
320 | #\r | |
c8ec22a2 JY |
321 | fdIn = open(mapFile, "r")\r |
322 | rptLine = fdIn.readline()\r | |
323 | modName = ""\r | |
324 | while (rptLine != "" ):\r | |
325 | if rptLine[0] != ' ':\r | |
326 | #DxeIpl (Fixed Flash Address, BaseAddress=0x00fffb4310, EntryPoint=0x00fffb4958)\r | |
327 | #(GUID=86D70125-BAA3-4296-A62F-602BEBBB9081 .textbaseaddress=0x00fffb4398 .databaseaddress=0x00fffb4178)\r | |
328 | match = re.match("([_a-zA-Z0-9\-]+)\s\(.+BaseAddress=(0x[0-9a-fA-F]+),\s+EntryPoint=(0x[0-9a-fA-F]+)\)", rptLine)\r | |
329 | if match is not None:\r | |
330 | modName = match.group(1)\r | |
331 | if len(modName) == 36:\r | |
332 | modName = self.dictGuidNameXref[modName.upper()]\r | |
333 | self.dictModBase['%s:BASE' % modName] = int (match.group(2), 16)\r | |
334 | self.dictModBase['%s:ENTRY' % modName] = int (match.group(3), 16)\r | |
335 | match = re.match("\(GUID=([A-Z0-9\-]+)\s+\.textbaseaddress=(0x[0-9a-fA-F]+)\s+\.databaseaddress=(0x[0-9a-fA-F]+)\)", rptLine)\r | |
336 | if match is not None:\r | |
337 | modName = match.group(1)\r | |
338 | if len(modName) == 36:\r | |
339 | modName = self.dictGuidNameXref[modName.upper()]\r | |
340 | self.dictModBase['%s:TEXT' % modName] = int (match.group(2), 16)\r | |
341 | self.dictModBase['%s:DATA' % modName] = int (match.group(3), 16)\r | |
342 | else:\r | |
343 | # 0x00fff8016c __ModuleEntryPoint\r | |
344 | match = re.match("^\s+(0x[a-z0-9]+)\s+([_a-zA-Z0-9]+)", rptLine)\r | |
345 | if match is not None:\r | |
346 | self.dictSymbolAddress["%s:%s"%(modName, match.group(2))] = match.group(1)\r | |
347 | rptLine = fdIn.readline()\r | |
348 | fdIn.close()\r | |
349 | return 0\r | |
350 | \r | |
fb9819f1 JY |
351 | #\r |
352 | # Parse MOD MAP file\r | |
353 | #\r | |
354 | # param [in] moduleName Module name\r | |
355 | # param [in] mapFile .Fv.map file\r | |
356 | #\r | |
357 | # retval 0 Parsed MOD MAP file successfully\r | |
358 | # retval 1 There is no moduleEntryPoint in modSymbols\r | |
359 | #\r | |
c8ec22a2 | 360 | def parseModMapFile(self, moduleName, mapFile):\r |
fb9819f1 JY |
361 | #\r |
362 | # Get information from mapFile by moduleName in order to create a dictionary\r | |
363 | # For example,\r | |
364 | # self.dictSymbolAddress[FspSecCore:___guard_fids_count] = 0x00fffa4778\r | |
365 | #\r | |
c8ec22a2 JY |
366 | modSymbols = {}\r |
367 | fdIn = open(mapFile, "r")\r | |
fb9819f1 JY |
368 | reportLines = fdIn.readlines()\r |
369 | fdIn.close()\r | |
370 | \r | |
371 | moduleEntryPoint = "__ModuleEntryPoint"\r | |
372 | reportLine = reportLines[0]\r | |
71a6022f | 373 | if reportLine.strip().find("Archive member included") != -1:\r |
c8ec22a2 JY |
374 | #GCC\r |
375 | # 0x0000000000001d55 IoRead8\r | |
376 | patchMapFileMatchString = "\s+(0x[0-9a-fA-F]{16})\s+([^\s][^0x][_a-zA-Z0-9\-]+)\s"\r | |
377 | matchKeyGroupIndex = 2\r | |
378 | matchSymbolGroupIndex = 1\r | |
fb9819f1 | 379 | prefix = '_'\r |
c8ec22a2 JY |
380 | else:\r |
381 | #MSFT\r | |
382 | #0003:00000190 _gComBase 00007a50 SerialPo\r | |
383 | patchMapFileMatchString = "^\s[0-9a-fA-F]{4}:[0-9a-fA-F]{8}\s+(\w+)\s+([0-9a-fA-F]{8}\s+)"\r | |
384 | matchKeyGroupIndex = 1\r | |
385 | matchSymbolGroupIndex = 2\r | |
fb9819f1 JY |
386 | prefix = ''\r |
387 | \r | |
388 | for reportLine in reportLines:\r | |
c8ec22a2 JY |
389 | match = re.match(patchMapFileMatchString, reportLine)\r |
390 | if match is not None:\r | |
fb9819f1 JY |
391 | modSymbols[prefix + match.group(matchKeyGroupIndex)] = match.group(matchSymbolGroupIndex)\r |
392 | \r | |
393 | # Handle extra module patchable PCD variable in Linux map since it might have different format\r | |
394 | # .data._gPcd_BinaryPatch_PcdVpdBaseAddress\r | |
395 | # 0x0000000000003714 0x4 /tmp/ccmytayk.ltrans1.ltrans.o\r | |
396 | handleNext = False\r | |
397 | if matchSymbolGroupIndex == 1:\r | |
398 | for reportLine in reportLines:\r | |
399 | if handleNext:\r | |
400 | handleNext = False\r | |
401 | pcdName = match.group(1)\r | |
402 | match = re.match("\s+(0x[0-9a-fA-F]{16})\s+", reportLine)\r | |
403 | if match is not None:\r | |
404 | modSymbols[prefix + pcdName] = match.group(1)\r | |
405 | else:\r | |
406 | match = re.match("^\s\.data\.(_gPcd_BinaryPatch[_a-zA-Z0-9\-]+)", reportLine)\r | |
407 | if match is not None:\r | |
408 | handleNext = True\r | |
409 | continue\r | |
c8ec22a2 JY |
410 | \r |
411 | if not moduleEntryPoint in modSymbols:\r | |
412 | return 1\r | |
413 | \r | |
414 | modEntry = '%s:%s' % (moduleName,moduleEntryPoint)\r | |
415 | if not modEntry in self.dictSymbolAddress:\r | |
416 | modKey = '%s:ENTRY' % moduleName\r | |
417 | if modKey in self.dictModBase:\r | |
418 | baseOffset = self.dictModBase['%s:ENTRY' % moduleName] - int(modSymbols[moduleEntryPoint], 16)\r | |
419 | else:\r | |
420 | return 2\r | |
421 | else:\r | |
422 | baseOffset = int(self.dictSymbolAddress[modEntry], 16) - int(modSymbols[moduleEntryPoint], 16)\r | |
423 | for symbol in modSymbols:\r | |
424 | fullSym = "%s:%s" % (moduleName, symbol)\r | |
425 | if not fullSym in self.dictSymbolAddress:\r | |
426 | self.dictSymbolAddress[fullSym] = "0x00%08x" % (baseOffset+ int(modSymbols[symbol], 16))\r | |
427 | return 0\r | |
428 | \r | |
fb9819f1 JY |
429 | #\r |
430 | # Parse Guid.xref file\r | |
431 | #\r | |
432 | # param [in] xrefFile the full directory of Guid.xref file\r | |
433 | #\r | |
434 | # retval 0 Parsed Guid.xref file successfully\r | |
435 | #\r | |
c8ec22a2 | 436 | def parseGuidXrefFile(self, xrefFile):\r |
fb9819f1 JY |
437 | #\r |
438 | # Get information from Guid.xref in order to create a GuidNameXref dictionary\r | |
439 | # The dictGuidNameXref, for example, will be like\r | |
440 | # dictGuidNameXref [1BA0062E-C779-4582-8566-336AE8F78F09] = FspSecCore\r | |
441 | #\r | |
c8ec22a2 JY |
442 | fdIn = open(xrefFile, "r")\r |
443 | rptLine = fdIn.readline()\r | |
444 | while (rptLine != "" ):\r | |
445 | match = re.match("([0-9a-fA-F\-]+)\s([_a-zA-Z0-9]+)", rptLine)\r | |
446 | if match is not None:\r | |
447 | self.dictGuidNameXref[match.group(1).upper()] = match.group(2)\r | |
448 | rptLine = fdIn.readline()\r | |
449 | fdIn.close()\r | |
450 | return 0\r | |
451 | \r | |
fb9819f1 JY |
452 | #\r |
453 | # Get current character\r | |
454 | #\r | |
455 | # retval elf.string[self.index]\r | |
456 | # retval '' Exception\r | |
457 | #\r | |
c8ec22a2 JY |
458 | def getCurr(self):\r |
459 | try:\r | |
460 | return self.string[self.index]\r | |
461 | except Exception:\r | |
462 | return ''\r | |
463 | \r | |
fb9819f1 JY |
464 | #\r |
465 | # Check to see if it is last index\r | |
466 | #\r | |
467 | # retval self.index\r | |
468 | #\r | |
c8ec22a2 JY |
469 | def isLast(self):\r |
470 | return self.index == len(self.string)\r | |
471 | \r | |
fb9819f1 JY |
472 | #\r |
473 | # Move to next index\r | |
474 | #\r | |
c8ec22a2 JY |
475 | def moveNext(self):\r |
476 | self.index += 1\r | |
477 | \r | |
fb9819f1 JY |
478 | #\r |
479 | # Skip space\r | |
480 | #\r | |
c8ec22a2 JY |
481 | def skipSpace(self):\r |
482 | while not self.isLast():\r | |
483 | if self.getCurr() in ' \t':\r | |
484 | self.moveNext()\r | |
485 | else:\r | |
486 | return\r | |
487 | \r | |
fb9819f1 JY |
488 | #\r |
489 | # Parse value\r | |
490 | #\r | |
491 | # retval value\r | |
492 | #\r | |
c8ec22a2 JY |
493 | def parseValue(self):\r |
494 | self.skipSpace()\r | |
495 | var = ''\r | |
496 | while not self.isLast():\r | |
497 | char = self.getCurr()\r | |
498 | if char.lower() in '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:-':\r | |
499 | var += char\r | |
500 | self.moveNext()\r | |
501 | else:\r | |
502 | break\r | |
503 | \r | |
504 | if ':' in var:\r | |
505 | partList = var.split(':')\r | |
506 | if len(partList) != 2:\r | |
507 | raise Exception("Unrecognized expression %s" % var)\r | |
508 | modName = partList[0]\r | |
509 | modOff = partList[1]\r | |
510 | if ('-' not in modName) and (modOff[0] in '0123456789'):\r | |
511 | # MOD: OFFSET\r | |
512 | var = self.getModGuid(modName) + ":" + modOff\r | |
513 | if '-' in var: # GUID:OFFSET\r | |
514 | value = self.getGuidOff(var)\r | |
515 | else:\r | |
516 | value = self.getSymbols(var)\r | |
517 | self.synUsed = True\r | |
518 | else:\r | |
519 | if var[0] in '0123456789':\r | |
520 | value = self.getNumber(var)\r | |
521 | else:\r | |
522 | value = self.getVariable(var)\r | |
523 | return int(value)\r | |
524 | \r | |
fb9819f1 JY |
525 | #\r |
526 | # Parse single operation\r | |
527 | #\r | |
528 | # retval ~self.parseBrace() or self.parseValue()\r | |
529 | #\r | |
c8ec22a2 JY |
530 | def parseSingleOp(self):\r |
531 | self.skipSpace()\r | |
532 | char = self.getCurr()\r | |
533 | if char == '~':\r | |
534 | self.moveNext()\r | |
535 | return ~self.parseBrace()\r | |
536 | else:\r | |
537 | return self.parseValue()\r | |
538 | \r | |
fb9819f1 JY |
539 | #\r |
540 | # Parse symbol of Brace([, {, <)\r | |
541 | #\r | |
542 | # retval value or self.parseSingleOp()\r | |
543 | #\r | |
c8ec22a2 JY |
544 | def parseBrace(self):\r |
545 | self.skipSpace()\r | |
546 | char = self.getCurr()\r | |
547 | parenthesisType = self.parenthesisOpenSet.find(char)\r | |
548 | if parenthesisType >= 0:\r | |
549 | self.moveNext()\r | |
550 | value = self.parseExpr()\r | |
551 | self.skipSpace()\r | |
552 | if self.getCurr() != self.parenthesisCloseSet[parenthesisType]:\r | |
553 | raise Exception("No closing brace")\r | |
554 | self.moveNext()\r | |
555 | if parenthesisType == 1: # [ : Get content\r | |
556 | value = self.getContent(value)\r | |
557 | elif parenthesisType == 2: # { : To address\r | |
558 | value = self.toAddress(value)\r | |
559 | elif parenthesisType == 3: # < : To offset\r | |
560 | value = self.toOffset(value)\r | |
561 | return value\r | |
562 | else:\r | |
563 | return self.parseSingleOp()\r | |
564 | \r | |
fb9819f1 JY |
565 | #\r |
566 | # Parse symbol of Multiplier(*)\r | |
567 | #\r | |
568 | # retval value or self.parseSingleOp()\r | |
569 | #\r | |
c8ec22a2 JY |
570 | def parseMul(self):\r |
571 | values = [self.parseBrace()]\r | |
572 | while True:\r | |
573 | self.skipSpace()\r | |
574 | char = self.getCurr()\r | |
575 | if char == '*':\r | |
576 | self.moveNext()\r | |
577 | values.append(self.parseBrace())\r | |
578 | else:\r | |
579 | break\r | |
9da59186 | 580 | value = 1\r |
c8ec22a2 JY |
581 | for each in values:\r |
582 | value *= each\r | |
583 | return value\r | |
584 | \r | |
fb9819f1 JY |
585 | #\r |
586 | # Parse symbol of And(&) and Or(|)\r | |
587 | #\r | |
588 | # retval value\r | |
589 | #\r | |
c8ec22a2 JY |
590 | def parseAndOr(self):\r |
591 | values = [self.parseMul()]\r | |
592 | op = None\r | |
9da59186 | 593 | value = 0xFFFFFFFF\r |
c8ec22a2 JY |
594 | while True:\r |
595 | self.skipSpace()\r | |
596 | char = self.getCurr()\r | |
597 | if char == '&':\r | |
598 | self.moveNext()\r | |
599 | values.append(self.parseMul())\r | |
600 | op = char\r | |
601 | elif char == '|':\r | |
602 | div_index = self.index\r | |
603 | self.moveNext()\r | |
604 | values.append(self.parseMul())\r | |
605 | value = 0\r | |
606 | op = char\r | |
607 | else:\r | |
608 | break\r | |
609 | \r | |
610 | for each in values:\r | |
611 | if op == '|':\r | |
612 | value |= each\r | |
613 | else:\r | |
614 | value &= each\r | |
615 | \r | |
616 | return value\r | |
617 | \r | |
fb9819f1 JY |
618 | #\r |
619 | # Parse symbol of Add(+) and Minus(-)\r | |
620 | #\r | |
621 | # retval sum(values)\r | |
622 | #\r | |
c8ec22a2 JY |
623 | def parseAddMinus(self):\r |
624 | values = [self.parseAndOr()]\r | |
625 | while True:\r | |
626 | self.skipSpace()\r | |
627 | char = self.getCurr()\r | |
628 | if char == '+':\r | |
629 | self.moveNext()\r | |
630 | values.append(self.parseAndOr())\r | |
631 | elif char == '-':\r | |
632 | self.moveNext()\r | |
633 | values.append(-1 * self.parseAndOr())\r | |
634 | else:\r | |
635 | break\r | |
636 | return sum(values)\r | |
637 | \r | |
fb9819f1 JY |
638 | #\r |
639 | # Parse expression\r | |
640 | #\r | |
641 | # retval self.parseAddMinus()\r | |
642 | #\r | |
c8ec22a2 JY |
643 | def parseExpr(self):\r |
644 | return self.parseAddMinus()\r | |
645 | \r | |
fb9819f1 JY |
646 | #\r |
647 | # Get result\r | |
648 | #\r | |
649 | # retval value\r | |
650 | #\r | |
c8ec22a2 JY |
651 | def getResult(self):\r |
652 | value = self.parseExpr()\r | |
653 | self.skipSpace()\r | |
654 | if not self.isLast():\r | |
655 | raise Exception("Unexpected character found '%s'" % self.getCurr())\r | |
656 | return value\r | |
657 | \r | |
fb9819f1 JY |
658 | #\r |
659 | # Get module GUID\r | |
660 | #\r | |
661 | # retval value\r | |
662 | #\r | |
c8ec22a2 JY |
663 | def getModGuid(self, var):\r |
664 | guid = (guid for guid,name in self.dictGuidNameXref.items() if name==var)\r | |
665 | try:\r | |
666 | value = guid.next()\r | |
667 | except Exception:\r | |
668 | raise Exception("Unknown module name %s !" % var)\r | |
669 | return value\r | |
670 | \r | |
fb9819f1 JY |
671 | #\r |
672 | # Get variable\r | |
673 | #\r | |
674 | # retval value\r | |
675 | #\r | |
c8ec22a2 JY |
676 | def getVariable(self, var):\r |
677 | value = self.dictVariable.get(var, None)\r | |
678 | if value == None:\r | |
679 | raise Exception("Unrecognized variable '%s'" % var)\r | |
680 | return value\r | |
681 | \r | |
fb9819f1 JY |
682 | #\r |
683 | # Get number\r | |
684 | #\r | |
685 | # retval value\r | |
686 | #\r | |
c8ec22a2 JY |
687 | def getNumber(self, var):\r |
688 | var = var.strip()\r | |
689 | if var.startswith('0x'): # HEX\r | |
690 | value = int(var, 16)\r | |
691 | else:\r | |
692 | value = int(var, 10)\r | |
693 | return value\r | |
694 | \r | |
fb9819f1 JY |
695 | #\r |
696 | # Get content\r | |
697 | #\r | |
698 | # param [in] value\r | |
699 | #\r | |
700 | # retval value\r | |
701 | #\r | |
c8ec22a2 JY |
702 | def getContent(self, value):\r |
703 | if (value >= self.fdBase) and (value < self.fdBase + self.fdSize):\r | |
704 | value = value - self.fdBase\r | |
705 | if value >= self.fdSize:\r | |
706 | raise Exception("Invalid file offset 0x%08x !" % value)\r | |
707 | return readDataFromFile (self.fdFile, value, 4)\r | |
708 | \r | |
fb9819f1 JY |
709 | #\r |
710 | # Change value to address\r | |
711 | #\r | |
712 | # param [in] value\r | |
713 | #\r | |
714 | # retval value\r | |
715 | #\r | |
c8ec22a2 JY |
716 | def toAddress(self, value):\r |
717 | if value < self.fdSize:\r | |
718 | value = value + self.fdBase\r | |
719 | return value\r | |
720 | \r | |
fb9819f1 JY |
721 | #\r |
722 | # Change value to offset\r | |
723 | #\r | |
724 | # param [in] value\r | |
725 | #\r | |
726 | # retval value\r | |
727 | #\r | |
c8ec22a2 JY |
728 | def toOffset(self, value):\r |
729 | if value > self.fdBase:\r | |
730 | value = value - self.fdBase\r | |
731 | return value\r | |
732 | \r | |
fb9819f1 JY |
733 | #\r |
734 | # Get GUID offset\r | |
735 | #\r | |
736 | # param [in] value\r | |
737 | #\r | |
738 | # retval value\r | |
739 | #\r | |
c8ec22a2 JY |
740 | def getGuidOff(self, value):\r |
741 | # GUID:Offset\r | |
742 | symbolName = value.split(':')\r | |
743 | if len(symbolName) == 2 and self.dictFfsOffset.has_key(symbolName[0]):\r | |
744 | value = (int(self.dictFfsOffset[symbolName[0]], 16) + int(symbolName[1], 16)) & 0xFFFFFFFF\r | |
745 | else:\r | |
746 | raise Exception("Unknown GUID %s !" % value)\r | |
747 | return value\r | |
748 | \r | |
fb9819f1 JY |
749 | #\r |
750 | # Get symbols\r | |
751 | #\r | |
752 | # param [in] value\r | |
753 | #\r | |
754 | # retval ret\r | |
755 | #\r | |
c8ec22a2 JY |
756 | def getSymbols(self, value):\r |
757 | if self.dictSymbolAddress.has_key(value):\r | |
758 | # Module:Function\r | |
759 | ret = int (self.dictSymbolAddress[value], 16)\r | |
760 | else:\r | |
761 | raise Exception("Unknown symbol %s !" % value)\r | |
762 | return ret\r | |
763 | \r | |
fb9819f1 JY |
764 | #\r |
765 | # Evaluate symbols\r | |
766 | #\r | |
767 | # param [in] expression\r | |
768 | # param [in] isOffset\r | |
769 | #\r | |
770 | # retval value & 0xFFFFFFFF\r | |
771 | #\r | |
c8ec22a2 JY |
772 | def evaluate(self, expression, isOffset):\r |
773 | self.index = 0\r | |
774 | self.synUsed = False\r | |
775 | self.string = expression\r | |
776 | value = self.getResult()\r | |
777 | if isOffset:\r | |
778 | if self.synUsed:\r | |
779 | # Consider it as an address first\r | |
780 | if (value >= self.fdBase) and (value < self.fdBase + self.fdSize):\r | |
781 | value = value - self.fdBase\r | |
782 | if value & 0x80000000:\r | |
783 | # Consider it as a negative offset next\r | |
784 | offset = (~value & 0xFFFFFFFF) + 1\r | |
785 | if offset < self.fdSize:\r | |
786 | value = self.fdSize - offset\r | |
787 | if value >= self.fdSize:\r | |
788 | raise Exception("Invalid offset expression !")\r | |
789 | return value & 0xFFFFFFFF\r | |
790 | \r | |
fb9819f1 JY |
791 | #\r |
792 | # Print out the usage\r | |
793 | #\r | |
c8ec22a2 JY |
794 | def usage():\r |
795 | print "Usage: \n\tPatchFv FvBuildDir [FvFileBaseNames:]FdFileBaseNameToPatch \"Offset, Value\""\r | |
796 | \r | |
797 | def main():\r | |
798 | #\r | |
799 | # Parse the options and args\r | |
800 | #\r | |
801 | symTables = Symbols()\r | |
802 | \r | |
fb9819f1 JY |
803 | #\r |
804 | # If the arguments are less than 4, then return an error.\r | |
805 | #\r | |
c8ec22a2 JY |
806 | if len(sys.argv) < 4:\r |
807 | Usage()\r | |
808 | return 1\r | |
809 | \r | |
fb9819f1 JY |
810 | #\r |
811 | # If it fails to create dictionaries, then return an error.\r | |
812 | #\r | |
c8ec22a2 JY |
813 | if symTables.createDicts(sys.argv[1], sys.argv[2]) != 0:\r |
814 | print "ERROR: Failed to create symbol dictionary!!"\r | |
815 | return 2\r | |
816 | \r | |
fb9819f1 JY |
817 | #\r |
818 | # Get FD file and size\r | |
819 | #\r | |
c8ec22a2 JY |
820 | fdFile = symTables.getFdFile()\r |
821 | fdSize = symTables.getFdSize()\r | |
822 | \r | |
823 | try:\r | |
fb9819f1 JY |
824 | #\r |
825 | # Check to see if FSP header is valid\r | |
826 | #\r | |
9da59186 JY |
827 | ret = IsFspHeaderValid(fdFile)\r |
828 | if ret == False:\r | |
829 | raise Exception ("The FSP header is not valid. Stop patching FD.")\r | |
c8ec22a2 JY |
830 | comment = ""\r |
831 | for fvFile in sys.argv[3:]:\r | |
fb9819f1 JY |
832 | #\r |
833 | # Check to see if it has enough arguments\r | |
834 | #\r | |
c8ec22a2 JY |
835 | items = fvFile.split(",")\r |
836 | if len (items) < 2:\r | |
837 | raise Exception("Expect more arguments for '%s'!" % fvFile)\r | |
838 | \r | |
839 | comment = ""\r | |
840 | command = ""\r | |
841 | params = []\r | |
842 | for item in items:\r | |
843 | item = item.strip()\r | |
844 | if item.startswith("@"):\r | |
845 | comment = item[1:]\r | |
846 | elif item.startswith("$"):\r | |
847 | command = item[1:]\r | |
848 | else:\r | |
849 | if len(params) == 0:\r | |
850 | isOffset = True\r | |
851 | else :\r | |
852 | isOffset = False\r | |
fb9819f1 JY |
853 | #\r |
854 | # Parse symbols then append it to params\r | |
855 | #\r | |
c8ec22a2 JY |
856 | params.append (symTables.evaluate(item, isOffset))\r |
857 | \r | |
fb9819f1 JY |
858 | #\r |
859 | # Patch a new value into FD file if it is not a command\r | |
860 | #\r | |
c8ec22a2 JY |
861 | if command == "":\r |
862 | # Patch a DWORD\r | |
863 | if len (params) == 2:\r | |
864 | offset = params[0]\r | |
865 | value = params[1]\r | |
866 | oldvalue = readDataFromFile(fdFile, offset, 4)\r | |
867 | ret = patchDataInFile (fdFile, offset, value, 4) - 4\r | |
868 | else:\r | |
869 | raise Exception ("Patch command needs 2 parameters !")\r | |
870 | \r | |
871 | if ret:\r | |
872 | raise Exception ("Patch failed for offset 0x%08X" % offset)\r | |
873 | else:\r | |
874 | print "Patched offset 0x%08X:[%08X] with value 0x%08X # %s" % (offset, oldvalue, value, comment)\r | |
875 | \r | |
876 | elif command == "COPY":\r | |
fb9819f1 | 877 | #\r |
c8ec22a2 | 878 | # Copy binary block from source to destination\r |
fb9819f1 | 879 | #\r |
c8ec22a2 JY |
880 | if len (params) == 3:\r |
881 | src = symTables.toOffset(params[0])\r | |
882 | dest = symTables.toOffset(params[1])\r | |
883 | clen = symTables.toOffset(params[2])\r | |
884 | if (dest + clen <= fdSize) and (src + clen <= fdSize):\r | |
885 | oldvalue = readDataFromFile(fdFile, src, clen)\r | |
886 | ret = patchDataInFile (fdFile, dest, oldvalue, clen) - clen\r | |
887 | else:\r | |
888 | raise Exception ("Copy command OFFSET or LENGTH parameter is invalid !")\r | |
889 | else:\r | |
890 | raise Exception ("Copy command needs 3 parameters !")\r | |
891 | \r | |
892 | if ret:\r | |
893 | raise Exception ("Copy failed from offset 0x%08X to offset 0x%08X!" % (src, dest))\r | |
894 | else :\r | |
895 | print "Copied %d bytes from offset 0x%08X ~ offset 0x%08X # %s" % (clen, src, dest, comment)\r | |
896 | else:\r | |
897 | raise Exception ("Unknown command %s!" % command)\r | |
898 | return 0\r | |
899 | \r | |
900 | except Exception as (ex):\r | |
901 | print "ERROR: %s" % ex\r | |
902 | return 1\r | |
903 | \r | |
904 | if __name__ == '__main__':\r | |
905 | sys.exit(main())\r |