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