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