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