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