MdeModulePkg/ScsiDisk: Set correct value to the Media->IoAlign field of BlockIo proto...
[mirror_edk2.git] / IntelFspPkg / Tools / PatchFv.py
CommitLineData
c8ec22a2
JY
1## @ PatchFv.py\r
2#\r
3# Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
4# This program and the accompanying materials are licensed and made available under\r
5# the terms and conditions of the BSD License that accompanies this distribution.\r
6# The full text of the license may be found at\r
7# http://opensource.org/licenses/bsd-license.php.\r
8#\r
9# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11#\r
12##\r
13\r
14import os\r
15import re\r
16import sys\r
17\r
18def readDataFromFile (binfile, offset, len=1):\r
19 fd = open(binfile, "r+b")\r
20 fsize = os.path.getsize(binfile)\r
21 offval = offset & 0xFFFFFFFF\r
22 if (offval & 0x80000000):\r
23 offval = fsize - (0xFFFFFFFF - offval + 1)\r
24 fd.seek(offval)\r
25 bytearray = [ord(b) for b in fd.read(len)]\r
26 value = 0;\r
27 idx = len - 1;\r
28 while idx >= 0:\r
29 value = value << 8 | bytearray[idx]\r
30 idx = idx - 1\r
31 fd.close()\r
32 return value\r
33\r
34def patchDataInFile (binfile, offset, value, len=1):\r
35 fd = open(binfile, "r+b")\r
36 fsize = os.path.getsize(binfile)\r
37 offval = offset & 0xFFFFFFFF\r
38 if (offval & 0x80000000):\r
39 offval = fsize - (0xFFFFFFFF - offval + 1)\r
40 bytearray = []\r
41 idx = 0;\r
42 while idx < len:\r
43 bytearray.append(value & 0xFF)\r
44 value = value >> 8\r
45 idx = idx + 1\r
46 fd.seek(offval)\r
47 fd.write("".join(chr(b) for b in bytearray))\r
48 fd.close()\r
49 return len;\r
50\r
51\r
52class Symbols:\r
53 def __init__(self):\r
54 self.dictSymbolAddress = {}\r
55 self.dictGuidNameXref = {}\r
56 self.dictFfsOffset = {}\r
57 self.dictVariable = {}\r
58 self.dictModBase = {}\r
59 self.fdFile = None\r
60 self.string = ""\r
61 self.fdBase = 0xFFFFFFFF\r
62 self.fdSize = 0\r
63 self.index = 0\r
64 self.parenthesisOpenSet = '([{<'\r
65 self.parenthesisCloseSet = ')]}>'\r
66\r
67 def getFdFile (self):\r
68 return self.fdFile\r
69\r
70 def getFdSize (self):\r
71 return self.fdSize\r
72\r
73 def createDicts (self, fvDir, fvNames):\r
74 if not os.path.isdir(fvDir):\r
75 raise Exception ("'%s' is not a valid directory!" % FvDir)\r
76\r
77 xrefFile = os.path.join(fvDir, "Guid.xref")\r
78 if not os.path.exists(xrefFile):\r
79 raise Exception("Cannot open GUID Xref file '%s'!" % xrefFile)\r
80\r
81 self.dictGuidNameXref = {}\r
82 self.parseGuidXrefFile(xrefFile)\r
83\r
84 fvList = fvNames.split(":")\r
85 fdBase = fvList.pop()\r
86 if len(fvList) == 0:\r
87 fvList.append(fdBase)\r
88\r
89 fdFile = os.path.join(fvDir, fdBase.strip() + ".fd")\r
90 if not os.path.exists(fdFile):\r
91 raise Exception("Cannot open FD file '%s'!" % fdFile)\r
92\r
93 self.fdFile = fdFile\r
94 self.fdSize = os.path.getsize(fdFile)\r
95\r
96 infFile = os.path.join(fvDir, fvList[0].strip()) + ".inf"\r
97 if not os.path.exists(infFile):\r
98 raise Exception("Cannot open INF file '%s'!" % infFile)\r
99\r
100 self.parseInfFile(infFile)\r
101\r
102 self.dictVariable = {}\r
103 self.dictVariable["FDSIZE"] = self.fdSize\r
104 self.dictVariable["FDBASE"] = self.fdBase\r
105\r
106 self.dictSymbolAddress = {}\r
107 self.dictFfsOffset = {}\r
108 for file in fvList:\r
109\r
110 fvFile = os.path.join(fvDir, file.strip()) + ".Fv"\r
111 mapFile = fvFile + ".map"\r
112 if not os.path.exists(mapFile):\r
113 raise Exception("Cannot open MAP file '%s'!" % mapFile)\r
114\r
115 self.parseFvMapFile(mapFile)\r
116\r
117 fvTxtFile = fvFile + ".txt"\r
118 if not os.path.exists(fvTxtFile):\r
119 raise Exception("Cannot open FV TXT file '%s'!" % fvTxtFile)\r
120\r
121 self.parseFvTxtFile(fvTxtFile)\r
122\r
123 ffsDir = os.path.join(fvDir, "Ffs")\r
124 if (os.path.isdir(ffsDir)):\r
125 for item in os.listdir(ffsDir):\r
126 if len(item) <= 0x24:\r
127 continue\r
128 mapFile =os.path.join(ffsDir, item, "%s.map" % item[0:0x24])\r
129 if not os.path.exists(mapFile):\r
130 continue\r
131 self.parseModMapFile(item[0x24:], mapFile)\r
132\r
133 return 0\r
134\r
135 def getFvOffsetInFd(self, fvFile):\r
136 fvHandle = open(fvFile, "r+b")\r
137 fdHandle = open(self.fdFile, "r+b")\r
138 offset = fdHandle.read().find(fvHandle.read(0x70))\r
139 fvHandle.close()\r
140 fdHandle.close()\r
141 if offset == -1:\r
142 raise Exception("Could not locate FV file %s in FD!" % fvFile)\r
143 return offset\r
144\r
145 def parseInfFile(self, infFile):\r
146 fvOffset = self.getFvOffsetInFd(infFile[0:-4] + ".Fv")\r
147 fdIn = open(infFile, "r")\r
148 rptLine = fdIn.readline()\r
149 self.fdBase = 0xFFFFFFFF\r
150 while (rptLine != "" ):\r
151 #EFI_BASE_ADDRESS = 0xFFFDF400\r
152 match = re.match("^EFI_BASE_ADDRESS\s*=\s*(0x[a-fA-F0-9]+)", rptLine)\r
153 if match is not None:\r
154 self.fdBase = int(match.group(1), 16) - fvOffset\r
155 rptLine = fdIn.readline()\r
156 fdIn.close()\r
157 if self.fdBase == 0xFFFFFFFF:\r
158 raise Exception("Could not find EFI_BASE_ADDRESS in INF file!" % fvFile)\r
159 return 0\r
160\r
161 def parseFvTxtFile(self, fvTxtFile):\r
162 fvOffset = self.getFvOffsetInFd(fvTxtFile[0:-4])\r
163 fdIn = open(fvTxtFile, "r")\r
164 rptLine = fdIn.readline()\r
165 while (rptLine != "" ):\r
166 match = re.match("(0x[a-fA-F0-9]+)\s([0-9a-fA-F\-]+)", rptLine)\r
167 if match is not None:\r
168 self.dictFfsOffset[match.group(2)] = "0x%08X" % (int(match.group(1), 16) + fvOffset)\r
169 rptLine = fdIn.readline()\r
170 fdIn.close()\r
171 return 0\r
172\r
173 def parseFvMapFile(self, mapFile):\r
174 fdIn = open(mapFile, "r")\r
175 rptLine = fdIn.readline()\r
176 modName = ""\r
177 while (rptLine != "" ):\r
178 if rptLine[0] != ' ':\r
179 #DxeIpl (Fixed Flash Address, BaseAddress=0x00fffb4310, EntryPoint=0x00fffb4958)\r
180 #(GUID=86D70125-BAA3-4296-A62F-602BEBBB9081 .textbaseaddress=0x00fffb4398 .databaseaddress=0x00fffb4178)\r
181 match = re.match("([_a-zA-Z0-9\-]+)\s\(.+BaseAddress=(0x[0-9a-fA-F]+),\s+EntryPoint=(0x[0-9a-fA-F]+)\)", rptLine)\r
182 if match is not None:\r
183 modName = match.group(1)\r
184 if len(modName) == 36:\r
185 modName = self.dictGuidNameXref[modName.upper()]\r
186 self.dictModBase['%s:BASE' % modName] = int (match.group(2), 16)\r
187 self.dictModBase['%s:ENTRY' % modName] = int (match.group(3), 16)\r
188 match = re.match("\(GUID=([A-Z0-9\-]+)\s+\.textbaseaddress=(0x[0-9a-fA-F]+)\s+\.databaseaddress=(0x[0-9a-fA-F]+)\)", rptLine)\r
189 if match is not None:\r
190 modName = match.group(1)\r
191 if len(modName) == 36:\r
192 modName = self.dictGuidNameXref[modName.upper()]\r
193 self.dictModBase['%s:TEXT' % modName] = int (match.group(2), 16)\r
194 self.dictModBase['%s:DATA' % modName] = int (match.group(3), 16)\r
195 else:\r
196 # 0x00fff8016c __ModuleEntryPoint\r
197 match = re.match("^\s+(0x[a-z0-9]+)\s+([_a-zA-Z0-9]+)", rptLine)\r
198 if match is not None:\r
199 self.dictSymbolAddress["%s:%s"%(modName, match.group(2))] = match.group(1)\r
200 rptLine = fdIn.readline()\r
201 fdIn.close()\r
202 return 0\r
203\r
204 def parseModMapFile(self, moduleName, mapFile):\r
205 modSymbols = {}\r
206 fdIn = open(mapFile, "r")\r
207 reportLine = fdIn.readline()\r
208 if reportLine.strip().find("Archive member included because of file (symbol)") != -1:\r
209 #GCC\r
210 # 0x0000000000001d55 IoRead8\r
211 patchMapFileMatchString = "\s+(0x[0-9a-fA-F]{16})\s+([^\s][^0x][_a-zA-Z0-9\-]+)\s"\r
212 matchKeyGroupIndex = 2\r
213 matchSymbolGroupIndex = 1\r
214 moduleEntryPoint = "_ModuleEntryPoint"\r
215 else:\r
216 #MSFT\r
217 #0003:00000190 _gComBase 00007a50 SerialPo\r
218 patchMapFileMatchString = "^\s[0-9a-fA-F]{4}:[0-9a-fA-F]{8}\s+(\w+)\s+([0-9a-fA-F]{8}\s+)"\r
219 matchKeyGroupIndex = 1\r
220 matchSymbolGroupIndex = 2\r
221 moduleEntryPoint = "__ModuleEntryPoint"\r
222 while (reportLine != "" ):\r
223 match = re.match(patchMapFileMatchString, reportLine)\r
224 if match is not None:\r
225 modSymbols[match.group(matchKeyGroupIndex)] = match.group(matchSymbolGroupIndex)\r
226 reportLine = fdIn.readline()\r
227 fdIn.close()\r
228\r
229 if not moduleEntryPoint in modSymbols:\r
230 return 1\r
231\r
232 modEntry = '%s:%s' % (moduleName,moduleEntryPoint)\r
233 if not modEntry in self.dictSymbolAddress:\r
234 modKey = '%s:ENTRY' % moduleName\r
235 if modKey in self.dictModBase:\r
236 baseOffset = self.dictModBase['%s:ENTRY' % moduleName] - int(modSymbols[moduleEntryPoint], 16)\r
237 else:\r
238 return 2\r
239 else:\r
240 baseOffset = int(self.dictSymbolAddress[modEntry], 16) - int(modSymbols[moduleEntryPoint], 16)\r
241 for symbol in modSymbols:\r
242 fullSym = "%s:%s" % (moduleName, symbol)\r
243 if not fullSym in self.dictSymbolAddress:\r
244 self.dictSymbolAddress[fullSym] = "0x00%08x" % (baseOffset+ int(modSymbols[symbol], 16))\r
245 return 0\r
246\r
247 def parseGuidXrefFile(self, xrefFile):\r
248 fdIn = open(xrefFile, "r")\r
249 rptLine = fdIn.readline()\r
250 while (rptLine != "" ):\r
251 match = re.match("([0-9a-fA-F\-]+)\s([_a-zA-Z0-9]+)", rptLine)\r
252 if match is not None:\r
253 self.dictGuidNameXref[match.group(1).upper()] = match.group(2)\r
254 rptLine = fdIn.readline()\r
255 fdIn.close()\r
256 return 0\r
257\r
258 def getCurr(self):\r
259 try:\r
260 return self.string[self.index]\r
261 except Exception:\r
262 return ''\r
263\r
264 def isLast(self):\r
265 return self.index == len(self.string)\r
266\r
267 def moveNext(self):\r
268 self.index += 1\r
269\r
270 def skipSpace(self):\r
271 while not self.isLast():\r
272 if self.getCurr() in ' \t':\r
273 self.moveNext()\r
274 else:\r
275 return\r
276\r
277 def parseValue(self):\r
278 self.skipSpace()\r
279 var = ''\r
280 while not self.isLast():\r
281 char = self.getCurr()\r
282 if char.lower() in '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:-':\r
283 var += char\r
284 self.moveNext()\r
285 else:\r
286 break\r
287\r
288 if ':' in var:\r
289 partList = var.split(':')\r
290 if len(partList) != 2:\r
291 raise Exception("Unrecognized expression %s" % var)\r
292 modName = partList[0]\r
293 modOff = partList[1]\r
294 if ('-' not in modName) and (modOff[0] in '0123456789'):\r
295 # MOD: OFFSET\r
296 var = self.getModGuid(modName) + ":" + modOff\r
297 if '-' in var: # GUID:OFFSET\r
298 value = self.getGuidOff(var)\r
299 else:\r
300 value = self.getSymbols(var)\r
301 self.synUsed = True\r
302 else:\r
303 if var[0] in '0123456789':\r
304 value = self.getNumber(var)\r
305 else:\r
306 value = self.getVariable(var)\r
307 return int(value)\r
308\r
309 def parseSingleOp(self):\r
310 self.skipSpace()\r
311 char = self.getCurr()\r
312 if char == '~':\r
313 self.moveNext()\r
314 return ~self.parseBrace()\r
315 else:\r
316 return self.parseValue()\r
317\r
318 def parseBrace(self):\r
319 self.skipSpace()\r
320 char = self.getCurr()\r
321 parenthesisType = self.parenthesisOpenSet.find(char)\r
322 if parenthesisType >= 0:\r
323 self.moveNext()\r
324 value = self.parseExpr()\r
325 self.skipSpace()\r
326 if self.getCurr() != self.parenthesisCloseSet[parenthesisType]:\r
327 raise Exception("No closing brace")\r
328 self.moveNext()\r
329 if parenthesisType == 1: # [ : Get content\r
330 value = self.getContent(value)\r
331 elif parenthesisType == 2: # { : To address\r
332 value = self.toAddress(value)\r
333 elif parenthesisType == 3: # < : To offset\r
334 value = self.toOffset(value)\r
335 return value\r
336 else:\r
337 return self.parseSingleOp()\r
338\r
339 def parseMul(self):\r
340 values = [self.parseBrace()]\r
341 while True:\r
342 self.skipSpace()\r
343 char = self.getCurr()\r
344 if char == '*':\r
345 self.moveNext()\r
346 values.append(self.parseBrace())\r
347 else:\r
348 break\r
349 value = 1;\r
350 for each in values:\r
351 value *= each\r
352 return value\r
353\r
354 def parseAndOr(self):\r
355 values = [self.parseMul()]\r
356 op = None\r
357 value = 0xFFFFFFFF;\r
358 while True:\r
359 self.skipSpace()\r
360 char = self.getCurr()\r
361 if char == '&':\r
362 self.moveNext()\r
363 values.append(self.parseMul())\r
364 op = char\r
365 elif char == '|':\r
366 div_index = self.index\r
367 self.moveNext()\r
368 values.append(self.parseMul())\r
369 value = 0\r
370 op = char\r
371 else:\r
372 break\r
373\r
374 for each in values:\r
375 if op == '|':\r
376 value |= each\r
377 else:\r
378 value &= each\r
379\r
380 return value\r
381\r
382 def parseAddMinus(self):\r
383 values = [self.parseAndOr()]\r
384 while True:\r
385 self.skipSpace()\r
386 char = self.getCurr()\r
387 if char == '+':\r
388 self.moveNext()\r
389 values.append(self.parseAndOr())\r
390 elif char == '-':\r
391 self.moveNext()\r
392 values.append(-1 * self.parseAndOr())\r
393 else:\r
394 break\r
395 return sum(values)\r
396\r
397 def parseExpr(self):\r
398 return self.parseAddMinus()\r
399\r
400 def getResult(self):\r
401 value = self.parseExpr()\r
402 self.skipSpace()\r
403 if not self.isLast():\r
404 raise Exception("Unexpected character found '%s'" % self.getCurr())\r
405 return value\r
406\r
407 def getModGuid(self, var):\r
408 guid = (guid for guid,name in self.dictGuidNameXref.items() if name==var)\r
409 try:\r
410 value = guid.next()\r
411 except Exception:\r
412 raise Exception("Unknown module name %s !" % var)\r
413 return value\r
414\r
415 def getVariable(self, var):\r
416 value = self.dictVariable.get(var, None)\r
417 if value == None:\r
418 raise Exception("Unrecognized variable '%s'" % var)\r
419 return value\r
420\r
421 def getNumber(self, var):\r
422 var = var.strip()\r
423 if var.startswith('0x'): # HEX\r
424 value = int(var, 16)\r
425 else:\r
426 value = int(var, 10)\r
427 return value\r
428\r
429 def getContent(self, value):\r
430 if (value >= self.fdBase) and (value < self.fdBase + self.fdSize):\r
431 value = value - self.fdBase\r
432 if value >= self.fdSize:\r
433 raise Exception("Invalid file offset 0x%08x !" % value)\r
434 return readDataFromFile (self.fdFile, value, 4)\r
435\r
436 def toAddress(self, value):\r
437 if value < self.fdSize:\r
438 value = value + self.fdBase\r
439 return value\r
440\r
441 def toOffset(self, value):\r
442 if value > self.fdBase:\r
443 value = value - self.fdBase\r
444 return value\r
445\r
446 def getGuidOff(self, value):\r
447 # GUID:Offset\r
448 symbolName = value.split(':')\r
449 if len(symbolName) == 2 and self.dictFfsOffset.has_key(symbolName[0]):\r
450 value = (int(self.dictFfsOffset[symbolName[0]], 16) + int(symbolName[1], 16)) & 0xFFFFFFFF\r
451 else:\r
452 raise Exception("Unknown GUID %s !" % value)\r
453 return value\r
454\r
455 def getSymbols(self, value):\r
456 if self.dictSymbolAddress.has_key(value):\r
457 # Module:Function\r
458 ret = int (self.dictSymbolAddress[value], 16)\r
459 else:\r
460 raise Exception("Unknown symbol %s !" % value)\r
461 return ret\r
462\r
463 def evaluate(self, expression, isOffset):\r
464 self.index = 0\r
465 self.synUsed = False\r
466 self.string = expression\r
467 value = self.getResult()\r
468 if isOffset:\r
469 if self.synUsed:\r
470 # Consider it as an address first\r
471 if (value >= self.fdBase) and (value < self.fdBase + self.fdSize):\r
472 value = value - self.fdBase\r
473 if value & 0x80000000:\r
474 # Consider it as a negative offset next\r
475 offset = (~value & 0xFFFFFFFF) + 1\r
476 if offset < self.fdSize:\r
477 value = self.fdSize - offset\r
478 if value >= self.fdSize:\r
479 raise Exception("Invalid offset expression !")\r
480 return value & 0xFFFFFFFF\r
481\r
482def usage():\r
483 print "Usage: \n\tPatchFv FvBuildDir [FvFileBaseNames:]FdFileBaseNameToPatch \"Offset, Value\""\r
484\r
485def main():\r
486 #\r
487 # Parse the options and args\r
488 #\r
489 symTables = Symbols()\r
490\r
491 if len(sys.argv) < 4:\r
492 Usage()\r
493 return 1\r
494\r
495 if symTables.createDicts(sys.argv[1], sys.argv[2]) != 0:\r
496 print "ERROR: Failed to create symbol dictionary!!"\r
497 return 2\r
498\r
499 fdFile = symTables.getFdFile()\r
500 fdSize = symTables.getFdSize()\r
501\r
502 try:\r
503 comment = ""\r
504 for fvFile in sys.argv[3:]:\r
505 items = fvFile.split(",")\r
506 if len (items) < 2:\r
507 raise Exception("Expect more arguments for '%s'!" % fvFile)\r
508\r
509 comment = ""\r
510 command = ""\r
511 params = []\r
512 for item in items:\r
513 item = item.strip()\r
514 if item.startswith("@"):\r
515 comment = item[1:]\r
516 elif item.startswith("$"):\r
517 command = item[1:]\r
518 else:\r
519 if len(params) == 0:\r
520 isOffset = True\r
521 else :\r
522 isOffset = False\r
523 params.append (symTables.evaluate(item, isOffset))\r
524\r
525 if command == "":\r
526 # Patch a DWORD\r
527 if len (params) == 2:\r
528 offset = params[0]\r
529 value = params[1]\r
530 oldvalue = readDataFromFile(fdFile, offset, 4)\r
531 ret = patchDataInFile (fdFile, offset, value, 4) - 4\r
532 else:\r
533 raise Exception ("Patch command needs 2 parameters !")\r
534\r
535 if ret:\r
536 raise Exception ("Patch failed for offset 0x%08X" % offset)\r
537 else:\r
538 print "Patched offset 0x%08X:[%08X] with value 0x%08X # %s" % (offset, oldvalue, value, comment)\r
539\r
540 elif command == "COPY":\r
541 # Copy binary block from source to destination\r
542 if len (params) == 3:\r
543 src = symTables.toOffset(params[0])\r
544 dest = symTables.toOffset(params[1])\r
545 clen = symTables.toOffset(params[2])\r
546 if (dest + clen <= fdSize) and (src + clen <= fdSize):\r
547 oldvalue = readDataFromFile(fdFile, src, clen)\r
548 ret = patchDataInFile (fdFile, dest, oldvalue, clen) - clen\r
549 else:\r
550 raise Exception ("Copy command OFFSET or LENGTH parameter is invalid !")\r
551 else:\r
552 raise Exception ("Copy command needs 3 parameters !")\r
553\r
554 if ret:\r
555 raise Exception ("Copy failed from offset 0x%08X to offset 0x%08X!" % (src, dest))\r
556 else :\r
557 print "Copied %d bytes from offset 0x%08X ~ offset 0x%08X # %s" % (clen, src, dest, comment)\r
558 else:\r
559 raise Exception ("Unknown command %s!" % command)\r
560 return 0\r
561\r
562 except Exception as (ex):\r
563 print "ERROR: %s" % ex\r
564 return 1\r
565\r
566if __name__ == '__main__':\r
567 sys.exit(main())\r