]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py
BaseTools: Share RegEx between files
[mirror_edk2.git] / BaseTools / Source / Python / GenPatchPcdTable / GenPatchPcdTable.py
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 - 2018, 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 ========================================
20 import optparse
21 import Common.LongFilePathOs as 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, startPatternGeneral, addressPatternGeneral, valuePatternGcc, pcdPatternGcc, secReGeneral
28 from Common.BuildVersion import gBUILD_VERSION
29 from 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 ===============================================
39 symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.\-:\\\\\w\?@\$<>]+) +([\da-fA-F]+)', re.UNICODE)
40
41 def parsePcdInfoFromMapFile(mapfilepath, efifilepath):
42 """ Parse map file to get binary patch pcd information
43 @param path Map file absolution path
44
45 @return a list which element hold (PcdName, Offset, SectionName)
46 """
47 lines = []
48 try:
49 f = open(mapfilepath, 'r')
50 lines = f.readlines()
51 f.close()
52 except:
53 return None
54
55 if len(lines) == 0: return None
56 firstline = lines[0].strip()
57 if (firstline.startswith("Archive member included ") and
58 firstline.endswith(" file (symbol)")):
59 return _parseForGCC(lines, efifilepath)
60 if firstline.startswith("# Path:"):
61 return _parseForXcode(lines, efifilepath)
62 return _parseGeneral(lines, efifilepath)
63
64 def _parseForXcode(lines, efifilepath):
65 valuePattern = re.compile('^([\da-fA-FxX]+)([\s\S]*)([_]*_gPcd_BinaryPatch_([\w]+))')
66 status = 0
67 pcds = []
68 for line in lines:
69 line = line.strip()
70 if status == 0 and line == "# Symbols:":
71 status = 1
72 continue
73 if status == 1 and len(line) != 0:
74 if '_gPcd_BinaryPatch_' in line:
75 m = valuePattern.match(line)
76 if m is not None:
77 pcds.append((m.groups(0)[3], int(m.groups(0)[0], 16)))
78 return pcds
79
80 def _parseForGCC(lines, efifilepath):
81 """ Parse map file generated by GCC linker """
82 dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$')
83 status = 0
84 imageBase = -1
85 sections = []
86 bpcds = []
87 for index, line in enumerate(lines):
88 line = line.strip()
89 # status machine transection
90 if status == 0 and line == "Memory Configuration":
91 status = 1
92 continue
93 elif status == 1 and line == 'Linker script and memory map':
94 status = 2
95 continue
96 elif status ==2 and line == 'START GROUP':
97 status = 3
98 continue
99
100 # status handler
101 if status == 3:
102 m = valuePatternGcc.match(line)
103 if m is not None:
104 sections.append(m.groups(0))
105 if status == 3:
106 m = dataPattern.match(line)
107 if m is not None:
108 if lines[index + 1]:
109 PcdName = m.groups(0)[0]
110 m = pcdPatternGcc.match(lines[index + 1].strip())
111 if m is not None:
112 bpcds.append((PcdName, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))
113
114 # get section information from efi file
115 efisecs = PeImageClass(efifilepath).SectionHeaderList
116 if efisecs is None or len(efisecs) == 0:
117 return None
118 #redirection
119 redirection = 0
120 for efisec in efisecs:
121 for section in sections:
122 if section[0].strip() == efisec[0].strip() and section[0].strip() == '.text':
123 redirection = int(section[1], 16) - efisec[1]
124 pcds = []
125 for pcd in bpcds:
126 for efisec in efisecs:
127 if pcd[1] >= efisec[1] and pcd[1] < efisec[1]+efisec[3]:
128 #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file"
129 pcds.append([pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0]])
130 return pcds
131
132 def _parseGeneral(lines, efifilepath):
133 """ For MSFT, ICC, EBC
134 @param lines line array for map file
135
136 @return a list which element hold (PcdName, Offset, SectionName)
137 """
138 status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table
139 secs = [] # key = section name
140 bPcds = []
141 symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)')
142
143 for line in lines:
144 line = line.strip()
145 if startPatternGeneral.match(line):
146 status = 1
147 continue
148 if addressPatternGeneral.match(line):
149 status = 2
150 continue
151 if line.startswith("entry point at"):
152 status = 3
153 continue
154 if status == 1 and len(line) != 0:
155 m = secRe.match(line)
156 assert m is not None, "Fail to parse the section in map file , line is %s" % line
157 sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)
158 secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])
159 if status == 2 and len(line) != 0:
160 m = symRe.match(line)
161 assert m is not None, "Fail to parse the symbol in map file, line is %s" % line
162 sec_no, sym_offset, sym_name, vir_addr = m.groups(0)
163 sec_no = int(sec_no, 16)
164 sym_offset = int(sym_offset, 16)
165 vir_addr = int(vir_addr, 16)
166 m2 = symPattern.match(sym_name)
167 if m2 is not None:
168 # fond a binary pcd entry in map file
169 for sec in secs:
170 if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]):
171 bPcds.append([m2.groups(0)[0], sec[3], sym_offset, vir_addr, sec_no])
172
173 if len(bPcds) == 0: return None
174
175 # get section information from efi file
176 efisecs = PeImageClass(efifilepath).SectionHeaderList
177 if efisecs is None or len(efisecs) == 0:
178 return None
179
180 pcds = []
181 for pcd in bPcds:
182 index = 0
183 for efisec in efisecs:
184 index = index + 1
185 if pcd[1].strip() == efisec[0].strip():
186 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
187 elif pcd[4] == index:
188 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
189 return pcds
190
191 def generatePcdTable(list, pcdpath):
192 try:
193 f = open(pcdpath, 'w')
194 except:
195 pass
196
197 f.write('PCD Name Offset Section Name\r\n')
198
199 for pcditem in list:
200 f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2]))
201 f.close()
202
203 #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath
204
205 if __name__ == '__main__':
206 UsageString = "%prog -m <MapFile> -e <EfiFile> -o <OutFile>"
207 AdditionalNotes = "\nPCD table is generated in file name with .BinaryPcdTable.txt postfix"
208 parser = optparse.OptionParser(description=__copyright__, version=__version__, usage=UsageString)
209 parser.add_option('-m', '--mapfile', action='store', dest='mapfile',
210 help='Absolute path of module map file.')
211 parser.add_option('-e', '--efifile', action='store', dest='efifile',
212 help='Absolute path of EFI binary file.')
213 parser.add_option('-o', '--outputfile', action='store', dest='outfile',
214 help='Absolute path of output file to store the got patchable PCD table.')
215
216 (options, args) = parser.parse_args()
217
218 if options.mapfile is None or options.efifile is None:
219 print parser.get_usage()
220 elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):
221 list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)
222 if list is not None:
223 if options.outfile is not None:
224 generatePcdTable(list, options.outfile)
225 else:
226 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))
227 else:
228 print 'Fail to generate Patch PCD Table based on map file and efi file'
229 else:
230 print 'Fail to generate Patch PCD Table for fail to find map file or efi file!'