1## @file 2# Generate PCD table for 'Patchable In Module' type PCD with given .map file. 3# The Patch PCD table like: 4# 5# PCD Name Offset in binary 6# ======== ================ 7# 8# Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR> 9# This program and the accompanying materials 10# are licensed and made available under the terms and conditions of the BSD License 11# which accompanies this distribution. The full text of the license may be found at 12# http://opensource.org/licenses/bsd-license.php 13# 14# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, 15# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. 16# 17# 18 19#====================================== External Libraries ======================================== 20import optparse 21import Common.LongFilePathOs as os 22import re 23import array 24 25from Common.BuildToolError import * 26import Common.EdkLogger as EdkLogger 27from Common.Misc import PeImageClass 28from Common.BuildVersion import gBUILD_VERSION 29from Common.LongFilePathSupport import OpenLongFilePath as open 30 31# Version and Copyright 32__version_number__ = ("0.10" + " " + gBUILD_VERSION) 33__version__ = "%prog Version " + __version_number__ 34__copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved." 35 36#====================================== Internal Libraries ======================================== 37 38#============================================== Code =============================================== 39secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE) 40symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE) 41 42def parsePcdInfoFromMapFile(mapfilepath, efifilepath): 43 """ Parse map file to get binary patch pcd information 44 @param path Map file absolution path 45 46 @return a list which element hold (PcdName, Offset, SectionName) 47 """ 48 lines = [] 49 try: 50 f = open(mapfilepath, 'r') 51 lines = f.readlines() 52 f.close() 53 except: 54 return None 55 56 if len(lines) == 0: return None 57 firstline = lines[0].strip() 58 if (firstline.startswith("Archive member included ") and 59 firstline.endswith(" file (symbol)")): 60 return _parseForGCC(lines, efifilepath) 61 return _parseGeneral(lines, efifilepath) 62 63def _parseForGCC(lines, efifilepath): 64 """ Parse map file generated by GCC linker """ 65 status = 0 66 imageBase = -1 67 sections = [] 68 bpcds = [] 69 for line in lines: 70 line = line.strip() 71 # status machine transection 72 if status == 0 and line == "Memory Configuration": 73 status = 1 74 continue 75 elif status == 1 and line == 'Linker script and memory map': 76 status = 2 77 continue 78 elif status ==2 and line == 'START GROUP': 79 status = 3 80 continue 81 82 # status handler 83 if status == 2: 84 m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line) 85 if m != None: 86 sections.append(m.groups(0)) 87 if status == 2: 88 m = re.match("^([\da-fA-Fx]+) +[_]+gPcd_BinaryPatch_([\w_\d]+)$", line) 89 if m != None: 90 bpcds.append((m.groups(0)[1], int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0])) 91 92 # get section information from efi file 93 efisecs = PeImageClass(efifilepath).SectionHeaderList 94 if efisecs == None or len(efisecs) == 0: 95 return None 96 #redirection 97 redirection = 0 98 for efisec in efisecs: 99 for section in sections: 100 if section[0].strip() == efisec[0].strip() and section[0].strip() == '.text': 101 redirection = int(section[1], 16) - efisec[1] 102 pcds = [] 103 for pcd in bpcds: 104 for efisec in efisecs: 105 if pcd[1] >= efisec[1] and pcd[1] < efisec[1]+efisec[3]: 106 #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file" 107 pcds.append([pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0]]) 108 return pcds 109 110def _parseGeneral(lines, efifilepath): 111 """ For MSFT, ICC, EBC 112 @param lines line array for map file 113 114 @return a list which element hold (PcdName, Offset, SectionName) 115 """ 116 status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table 117 secs = [] # key = section name 118 bPcds = [] 119 120 121 for line in lines: 122 line = line.strip() 123 if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line): 124 status = 1 125 continue 126 if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line): 127 status = 2 128 continue 129 if re.match("^entry point at", line): 130 status = 3 131 continue 132 if status == 1 and len(line) != 0: 133 m = secRe.match(line) 134 assert m != None, "Fail to parse the section in map file , line is %s" % line 135 sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0) 136 secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class]) 137 if status == 2 and len(line) != 0: 138 m = symRe.match(line) 139 assert m != None, "Fail to parse the symbol in map file, line is %s" % line 140 sec_no, sym_offset, sym_name, vir_addr = m.groups(0) 141 sec_no = int(sec_no, 16) 142 sym_offset = int(sym_offset, 16) 143 vir_addr = int(vir_addr, 16) 144 m2 = re.match('^[_]+gPcd_BinaryPatch_([\w]+)', sym_name) 145 if m2 != None: 146 # fond a binary pcd entry in map file 147 for sec in secs: 148 if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]): 149 bPcds.append([m2.groups(0)[0], sec[3], sym_offset, vir_addr, sec_no]) 150 151 if len(bPcds) == 0: return None 152 153 # get section information from efi file 154 efisecs = PeImageClass(efifilepath).SectionHeaderList 155 if efisecs == None or len(efisecs) == 0: 156 return None 157 158 pcds = [] 159 for pcd in bPcds: 160 index = 0 161 for efisec in efisecs: 162 index = index + 1 163 if pcd[1].strip() == efisec[0].strip(): 164 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]]) 165 elif pcd[4] == index: 166 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]]) 167 return pcds 168 169def generatePcdTable(list, pcdpath): 170 try: 171 f = open(pcdpath, 'w') 172 except: 173 pass 174 175 f.write('PCD Name Offset Section Name\r\n') 176 177 for pcditem in list: 178 f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2])) 179 f.close() 180 181 #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath 182 183if __name__ == '__main__': 184 UsageString = "%prog -m <MapFile> -e <EfiFile> -o <OutFile>" 185 AdditionalNotes = "\nPCD table is generated in file name with .BinaryPcdTable.txt postfix" 186 parser = optparse.OptionParser(description=__copyright__, version=__version__, usage=UsageString) 187 parser.add_option('-m', '--mapfile', action='store', dest='mapfile', 188 help='Absolute path of module map file.') 189 parser.add_option('-e', '--efifile', action='store', dest='efifile', 190 help='Absolute path of EFI binary file.') 191 parser.add_option('-o', '--outputfile', action='store', dest='outfile', 192 help='Absolute path of output file to store the got patchable PCD table.') 193 194 (options, args) = parser.parse_args() 195 196 if options.mapfile == None or options.efifile == None: 197 print parser.get_usage() 198 elif os.path.exists(options.mapfile) and os.path.exists(options.efifile): 199 list = parsePcdInfoFromMapFile(options.mapfile, options.efifile) 200 if list != None: 201 if options.outfile != None: 202 generatePcdTable(list, options.outfile) 203 else: 204 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt')) 205 else: 206 print 'Fail to generate Patch PCD Table based on map file and efi file' 207 else: 208 print 'Fail to generate Patch PCD Table for fail to find map file or efi file!' 209