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