]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / GenPatchPcdTable / GenPatchPcdTable.py
... / ...
CommitLineData
1## @file\r
2# Generate PCD table for 'Patchable In Module' type PCD with given .map file.\r
3# The Patch PCD table like:\r
4#\r
5# PCD Name Offset in binary\r
6# ======== ================\r
7#\r
8# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
9# SPDX-License-Identifier: BSD-2-Clause-Patent\r
10#\r
11#\r
12\r
13#====================================== External Libraries ========================================\r
14from __future__ import print_function\r
15import optparse\r
16import Common.LongFilePathOs as os\r
17import re\r
18import array\r
19\r
20from Common.BuildToolError import *\r
21import Common.EdkLogger as EdkLogger\r
22from Common.Misc import PeImageClass, startPatternGeneral, addressPatternGeneral, valuePatternGcc, pcdPatternGcc, secReGeneral\r
23from Common.BuildVersion import gBUILD_VERSION\r
24from Common.LongFilePathSupport import OpenLongFilePath as open\r
25\r
26# Version and Copyright\r
27__version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
28__version__ = "%prog Version " + __version_number__\r
29__copyright__ = "Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved."\r
30\r
31#====================================== Internal Libraries ========================================\r
32\r
33#============================================== Code ===============================================\r
34symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.\-:\\\\\w\?@\$<>]+) +([\da-fA-F]+)', re.UNICODE)\r
35\r
36def parsePcdInfoFromMapFile(mapfilepath, efifilepath):\r
37 """ Parse map file to get binary patch pcd information\r
38 @param path Map file absolution path\r
39\r
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
49\r
50 if len(lines) == 0: return None\r
51 firstline = lines[0].strip()\r
52 if (firstline.startswith("Archive member included ") and\r
53 firstline.endswith(" file (symbol)")):\r
54 return _parseForGCC(lines, efifilepath)\r
55 if firstline.startswith("# Path:"):\r
56 return _parseForXcode(lines, efifilepath)\r
57 return _parseGeneral(lines, efifilepath)\r
58\r
59def _parseForXcode(lines, efifilepath):\r
60 valuePattern = re.compile('^([\da-fA-FxX]+)([\s\S]*)([_]*_gPcd_BinaryPatch_([\w]+))')\r
61 status = 0\r
62 pcds = []\r
63 for line in lines:\r
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
70 m = valuePattern.match(line)\r
71 if m is not None:\r
72 pcds.append((m.groups(0)[3], int(m.groups(0)[0], 16)))\r
73 return pcds\r
74\r
75def _parseForGCC(lines, efifilepath):\r
76 """ Parse map file generated by GCC linker """\r
77 dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$')\r
78 status = 0\r
79 imageBase = -1\r
80 sections = []\r
81 bpcds = []\r
82 for index, line in enumerate(lines):\r
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
95 # status handler\r
96 if status == 3:\r
97 m = valuePatternGcc.match(line)\r
98 if m is not None:\r
99 sections.append(m.groups(0))\r
100 if status == 3:\r
101 m = dataPattern.match(line)\r
102 if m is not None:\r
103 if lines[index + 1]:\r
104 PcdName = m.groups(0)[0]\r
105 m = pcdPatternGcc.match(lines[index + 1].strip())\r
106 if m is not None:\r
107 bpcds.append((PcdName, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0]))\r
108\r
109 # get section information from efi file\r
110 efisecs = PeImageClass(efifilepath).SectionHeaderList\r
111 if efisecs is None or len(efisecs) == 0:\r
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
125 return pcds\r
126\r
127def _parseGeneral(lines, efifilepath):\r
128 """ For MSFT, ICC, EBC\r
129 @param lines line array for map file\r
130\r
131 @return a list which element hold (PcdName, Offset, SectionName)\r
132 """\r
133 status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
134 secs = [] # key = section name\r
135 bPcds = []\r
136 symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)')\r
137\r
138 for line in lines:\r
139 line = line.strip()\r
140 if startPatternGeneral.match(line):\r
141 status = 1\r
142 continue\r
143 if addressPatternGeneral.match(line):\r
144 status = 2\r
145 continue\r
146 if line.startswith("entry point at"):\r
147 status = 3\r
148 continue\r
149 if status == 1 and len(line) != 0:\r
150 m = secReGeneral.match(line)\r
151 assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
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
156 assert m is not None, "Fail to parse the symbol in map file, line is %s" % line\r
157 sec_no, sym_offset, sym_name, vir_addr = m.groups(0)\r
158 sec_no = int(sec_no, 16)\r
159 sym_offset = int(sym_offset, 16)\r
160 vir_addr = int(vir_addr, 16)\r
161 m2 = symPattern.match(sym_name)\r
162 if m2 is not None:\r
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
172 if efisecs is None or len(efisecs) == 0:\r
173 return None\r
174\r
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
185\r
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
193\r
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
198 #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath\r
199\r
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
210\r
211 (options, args) = parser.parse_args()\r
212\r
213 if options.mapfile is None or options.efifile is None:\r
214 print(parser.get_usage())\r
215 elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):\r
216 list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)\r
217 if list is not None:\r
218 if options.outfile is not None:\r
219 generatePcdTable(list, options.outfile)\r
220 else:\r
221 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))\r
222 else:\r
223 print('Fail to generate Patch PCD Table based on map file and efi file')\r
224 else:\r
225 print('Fail to generate Patch PCD Table for fail to find map file or efi file!')\r