Fix the typo for the structure definition of EFI_ADAPTER_INFO_NETWORK_BOOT in Adapter...
[mirror_edk2.git] / BaseTools / Source / Python / GenPatchPcdTable / GenPatchPcdTable.py
CommitLineData
52302d4d
LG
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
e8a47801 8# Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.<BR>\r
40d841f6 9# This program and the accompanying materials\r
52302d4d
LG
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 ========================================
20import optparse
21import os
22import re
23import array
24
25from Common.BuildToolError import *
26import Common.EdkLogger as EdkLogger
27from Common.Misc import PeImageClass\r
b36d134f 28from Common.BuildVersion import gBUILD_VERSION\r
52302d4d
LG
29
30# Version and Copyright
b36d134f 31__version_number__ = ("0.10" + " " + gBUILD_VERSION)
52302d4d
LG
32__version__ = "%prog Version " + __version_number__
33__copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved."
34
35#====================================== Internal Libraries ========================================
36
37#============================================== Code ===============================================
38secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)
39symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)
40
41def 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 if lines[0].strip().find("Archive member included because of file (symbol)") != -1:
e8a47801 57 return _parseForGCC(lines, efifilepath)
52302d4d 58 return _parseGeneral(lines, efifilepath)
e8a47801
LG
59
60def _parseForGCC(lines, efifilepath):
52302d4d 61 """ Parse map file generated by GCC linker """
e8a47801
LG
62 status = 0
63 imageBase = -1
64 sections = []
65 bpcds = []
52302d4d
LG
66 for line in lines:
67 line = line.strip()
68 # status machine transection
e8a47801 69 if status == 0 and line == "Memory Configuration":
52302d4d
LG
70 status = 1
71 continue
e8a47801 72 elif status == 1 and line == 'Linker script and memory map':
52302d4d
LG
73 status = 2
74 continue
e8a47801
LG
75 elif status ==2 and line == 'START GROUP':
76 status = 3
77 continue
78
79 # status handler\r
52302d4d 80 if status == 2:
e8a47801 81 m = re.match('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$', line)
52302d4d 82 if m != None:
e8a47801 83 sections.append(m.groups(0))
52302d4d 84 if status == 2:
e8a47801 85 m = re.match("^([\da-fA-Fx]+) +[_]+gPcd_BinaryPatch_([\w_\d]+)$", line)
52302d4d 86 if m != None:
e8a47801
LG
87 bpcds.append((m.groups(0)[1], int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))
88 \r
89 # get section information from efi file\r
90 efisecs = PeImageClass(efifilepath).SectionHeaderList\r
91 if efisecs == None or len(efisecs) == 0:\r
92 return None\r
93 #redirection\r
94 redirection = 0\r
95 for efisec in efisecs:\r
96 for section in sections:\r
97 if section[0].strip() == efisec[0].strip() and section[0].strip() == '.text':\r
98 redirection = int(section[1], 16) - efisec[1]\r
99 pcds = []\r
100 for pcd in bpcds:\r
101 for efisec in efisecs:\r
102 if pcd[1] >= efisec[1] and pcd[1] < efisec[1]+efisec[3]:\r
103 #assert efisec[0].strip() == pcd[3].strip() and efisec[1] + redirection == pcd[2], "There are some differences between map file and efi file"\r
104 pcds.append([pcd[0], efisec[2] + pcd[1] - efisec[1] - redirection, efisec[0]])\r
52302d4d
LG
105 return pcds
106
107def _parseGeneral(lines, efifilepath):
108 """ For MSFT, ICC, EBC
109 @param lines line array for map file
110
111 @return a list which element hold (PcdName, Offset, SectionName)
112 """
113 status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table
114 secs = [] # key = section name
115 bPcds = []
116
117
118 for line in lines:
119 line = line.strip()
120 if re.match("^Start[' ']+Length[' ']+Name[' ']+Class", line):
121 status = 1
122 continue
123 if re.match("^Address[' ']+Publics by Value[' ']+Rva\+Base", line):
124 status = 2
125 continue
126 if re.match("^entry point at", line):
127 status = 3
128 continue
129 if status == 1 and len(line) != 0:
130 m = secRe.match(line)
131 assert m != None, "Fail to parse the section in map file , line is %s" % line
132 sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)
133 secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])
134 if status == 2 and len(line) != 0:
135 m = symRe.match(line)
136 assert m != None, "Fail to parse the symbol in map file, line is %s" % line
137 sec_no, sym_offset, sym_name, vir_addr = m.groups(0)
138 sec_no = int(sec_no, 16)
139 sym_offset = int(sym_offset, 16)
140 vir_addr = int(vir_addr, 16)
141 m2 = re.match('^[_]+gPcd_BinaryPatch_([\w]+)', sym_name)
142 if m2 != None:
143 # fond a binary pcd entry in map file
144 for sec in secs:
145 if sec[0] == sec_no and (sym_offset >= sec[1] and sym_offset < sec[1] + sec[2]):
146 bPcds.append([m2.groups(0)[0], sec[3], sym_offset, vir_addr, sec_no])
147
148 if len(bPcds) == 0: return None
149
150 # get section information from efi file
151 efisecs = PeImageClass(efifilepath).SectionHeaderList
152 if efisecs == None or len(efisecs) == 0:
153 return None
154
155 pcds = []
156 for pcd in bPcds:
157 index = 0
158 for efisec in efisecs:
159 index = index + 1
160 if pcd[1].strip() == efisec[0].strip():
161 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
162 elif pcd[4] == index:
163 pcds.append([pcd[0], efisec[2] + pcd[2], efisec[0]])
164 return pcds
165
166def generatePcdTable(list, pcdpath):
167 try:
168 f = open(pcdpath, 'w')
169 except:
170 pass
171
172 f.write('PCD Name Offset Section Name\r\n')
173
174 for pcditem in list:
175 f.write('%-30s 0x%-08X %-6s\r\n' % (pcditem[0], pcditem[1], pcditem[2]))
176 f.close()
177
178 #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath
179
180if __name__ == '__main__':
181 UsageString = "%prog -m <MapFile> -e <EfiFile> -o <OutFile>"
182 AdditionalNotes = "\nPCD table is generated in file name with .BinaryPcdTable.txt postfix"
183 parser = optparse.OptionParser(description=__copyright__, version=__version__, usage=UsageString)
184 parser.add_option('-m', '--mapfile', action='store', dest='mapfile',
185 help='Absolute path of module map file.')
186 parser.add_option('-e', '--efifile', action='store', dest='efifile',
187 help='Absolute path of EFI binary file.')
188 parser.add_option('-o', '--outputfile', action='store', dest='outfile',
189 help='Absolute path of output file to store the got patchable PCD table.')
190
191 (options, args) = parser.parse_args()
192
193 if options.mapfile == None or options.efifile == None:
194 print parser.get_usage()
195 elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):
196 list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)
197 if list != None:
198 if options.outfile != None:
199 generatePcdTable(list, options.outfile)
200 else:
201 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))
202 else:
203 print 'Fail to generate Patch PCD Table based on map file and efi file'
204 else:
205 print 'Fail to generate Patch PCD Table for fail to find map file or efi file!'