X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=BaseTools%2FSource%2FPython%2FGenPatchPcdTable%2FGenPatchPcdTable.py;h=d962ab0adda73f9e0e3a5edaff32384dd3c3fcb3;hp=36e539f67458b73889ed71bebf489d990733c251;hb=HEAD;hpb=47fea6afd74af76c7e2a2b03d319b7ac035ac26a;ds=sidebyside diff --git a/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py b/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py index 36e539f674..d962ab0add 100644 --- a/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py +++ b/BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py @@ -1,22 +1,17 @@ ## @file # Generate PCD table for 'Patchable In Module' type PCD with given .map file. # The Patch PCD table like: -# +# # PCD Name Offset in binary # ======== ================ # -# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.
-# This program and the accompanying materials -# are licensed and made available under the terms and conditions of the BSD License -# which accompanies this distribution. The full text of the license may be found at -# http://opensource.org/licenses/bsd-license.php -# -# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, -# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. +# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent # # #====================================== External Libraries ======================================== +from __future__ import print_function import optparse import Common.LongFilePathOs as os import re @@ -24,25 +19,24 @@ import array from Common.BuildToolError import * import Common.EdkLogger as EdkLogger -from Common.Misc import PeImageClass +from Common.Misc import PeImageClass, startPatternGeneral, addressPatternGeneral, valuePatternGcc, pcdPatternGcc, secReGeneral from Common.BuildVersion import gBUILD_VERSION from Common.LongFilePathSupport import OpenLongFilePath as open # Version and Copyright __version_number__ = ("0.10" + " " + gBUILD_VERSION) __version__ = "%prog Version " + __version_number__ -__copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved." +__copyright__ = "Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved." #====================================== Internal Libraries ======================================== #============================================== Code =============================================== -secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE) -symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE) +symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.\-:\\\\\w\?@\$<>]+) +([\da-fA-F]+)', re.UNICODE) def parsePcdInfoFromMapFile(mapfilepath, efifilepath): - """ Parse map file to get binary patch pcd information + """ Parse map file to get binary patch pcd information @param path Map file absolution path - + @return a list which element hold (PcdName, Offset, SectionName) """ lines = [] @@ -52,21 +46,43 @@ def parsePcdInfoFromMapFile(mapfilepath, efifilepath): f.close() except: return None - + if len(lines) == 0: return None firstline = lines[0].strip() + if re.match('^\s*Address\s*Size\s*Align\s*Out\s*In\s*Symbol\s*$', firstline): + return _parseForXcodeAndClang9(lines, efifilepath) if (firstline.startswith("Archive member included ") and firstline.endswith(" file (symbol)")): return _parseForGCC(lines, efifilepath) + if firstline.startswith("# Path:"): + return _parseForXcodeAndClang9(lines, efifilepath) return _parseGeneral(lines, efifilepath) +def _parseForXcodeAndClang9(lines, efifilepath): + valuePattern = re.compile('^([\da-fA-FxX]+)([\s\S]*)([_]*_gPcd_BinaryPatch_([\w]+))') + status = 0 + pcds = [] + for line in lines: + line = line.strip() + if status == 0 and (re.match('^\s*Address\s*Size\s*Align\s*Out\s*In\s*Symbol\s*$', line) \ + or line == "# Symbols:"): + status = 1 + continue + if status == 1 and len(line) != 0: + if '_gPcd_BinaryPatch_' in line: + m = valuePattern.match(line) + if m is not None: + pcds.append((m.groups(0)[3], int(m.groups(0)[0], 16))) + return pcds + def _parseForGCC(lines, efifilepath): """ Parse map file generated by GCC linker """ + dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$') status = 0 imageBase = -1 sections = [] bpcds = [] - for line in lines: + for index, line in enumerate(lines): line = line.strip() # status machine transection if status == 0 and line == "Memory Configuration": @@ -80,18 +96,22 @@ def _parseForGCC(lines, efifilepath): continue # status handler - if status == 2: - m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line) - if m != None: + if status == 3: + m = valuePatternGcc.match(line) + if m is not None: sections.append(m.groups(0)) - if status == 2: - m = re.match("^([\da-fA-Fx]+) +[_]+gPcd_BinaryPatch_([\w_\d]+)$", line) - if m != None: - bpcds.append((m.groups(0)[1], int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0])) - + if status == 3: + m = dataPattern.match(line) + if m is not None: + if lines[index + 1]: + PcdName = m.groups(0)[0] + m = pcdPatternGcc.match(lines[index + 1].strip()) + if m is not None: + bpcds.append((PcdName, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0])) + # get section information from efi file efisecs = PeImageClass(efifilepath).SectionHeaderList - if efisecs == None or len(efisecs) == 0: + if efisecs is None or len(efisecs) == 0: return None #redirection redirection = 0 @@ -106,43 +126,43 @@ def _parseForGCC(lines, efifilepath): #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file" pcds.append([pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0]]) return pcds - + def _parseGeneral(lines, efifilepath): - """ For MSFT, ICC, EBC + """ For MSFT, ICC, EBC @param lines line array for map file - + @return a list which element hold (PcdName, Offset, SectionName) """ status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table secs = [] # key = section name bPcds = [] - + symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)') for line in lines: line = line.strip() - if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line): + if startPatternGeneral.match(line): status = 1 continue - if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line): + if addressPatternGeneral.match(line): status = 2 continue - if re.match("^entry point at", line): + if line.startswith("entry point at"): status = 3 continue if status == 1 and len(line) != 0: - m = secRe.match(line) - assert m != None, "Fail to parse the section in map file , line is %s" % line + m = secReGeneral.match(line) + assert m is not None, "Fail to parse the section in map file , line is %s" % line sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0) secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class]) if status == 2 and len(line) != 0: m = symRe.match(line) - assert m != None, "Fail to parse the symbol in map file, line is %s" % line + assert m is not None, "Fail to parse the symbol in map file, line is %s" % line sec_no, sym_offset, sym_name, vir_addr = m.groups(0) sec_no = int(sec_no, 16) sym_offset = int(sym_offset, 16) vir_addr = int(vir_addr, 16) - m2 = re.match('^[_]+gPcd_BinaryPatch_([\w]+)', sym_name) - if m2 != None: + m2 = symPattern.match(sym_name) + if m2 is not None: # fond a binary pcd entry in map file for sec in secs: if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]): @@ -152,9 +172,9 @@ def _parseGeneral(lines, efifilepath): # get section information from efi file efisecs = PeImageClass(efifilepath).SectionHeaderList - if efisecs == None or len(efisecs) == 0: + if efisecs is None or len(efisecs) == 0: return None - + pcds = [] for pcd in bPcds: index = 0 @@ -165,7 +185,7 @@ def _parseGeneral(lines, efifilepath): elif pcd[4] == index: pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]]) return pcds - + def generatePcdTable(list, pcdpath): try: f = open(pcdpath, 'w') @@ -173,12 +193,12 @@ def generatePcdTable(list, pcdpath): pass f.write('PCD Name Offset Section Name\r\n') - + for pcditem in list: f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2])) f.close() - #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath + #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath if __name__ == '__main__': UsageString = "%prog -m -e -o " @@ -190,19 +210,19 @@ if __name__ == '__main__': help='Absolute path of EFI binary file.') parser.add_option('-o', '--outputfile', action='store', dest='outfile', help='Absolute path of output file to store the got patchable PCD table.') - + (options, args) = parser.parse_args() - if options.mapfile == None or options.efifile == None: - print parser.get_usage() + if options.mapfile is None or options.efifile is None: + print(parser.get_usage()) elif os.path.exists(options.mapfile) and os.path.exists(options.efifile): list = parsePcdInfoFromMapFile(options.mapfile, options.efifile) - if list != None: - if options.outfile != None: + if list is not None: + if options.outfile is not None: generatePcdTable(list, options.outfile) else: generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt')) else: - print 'Fail to generate Patch PCD Table based on map file and efi file' + print('Fail to generate Patch PCD Table based on map file and efi file') else: - print 'Fail to generate Patch PCD Table for fail to find map file or efi file!' + print('Fail to generate Patch PCD Table for fail to find map file or efi file!')