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