]> git.proxmox.com Git - mirror_edk2.git/blame - 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
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 51 firstline = lines[0].strip()\r
5cef9277
ZL
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
e4ac870f
LG
54 if (firstline.startswith("Archive member included ") and\r
55 firstline.endswith(" file (symbol)")):\r
f51461c8 56 return _parseForGCC(lines, efifilepath)\r
14239ee0 57 if firstline.startswith("# Path:"):\r
5cef9277 58 return _parseForXcodeAndClang9(lines, efifilepath)\r
f51461c8
LG
59 return _parseGeneral(lines, efifilepath)\r
60\r
5cef9277 61def _parseForXcodeAndClang9(lines, efifilepath):\r
2a9aac46 62 valuePattern = re.compile('^([\da-fA-FxX]+)([\s\S]*)([_]*_gPcd_BinaryPatch_([\w]+))')\r
14239ee0
YZ
63 status = 0\r
64 pcds = []\r
e52aed0d 65 for line in lines:\r
14239ee0 66 line = line.strip()\r
5cef9277
ZL
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
14239ee0
YZ
69 status = 1\r
70 continue\r
71 if status == 1 and len(line) != 0:\r
72 if '_gPcd_BinaryPatch_' in line:\r
2a9aac46 73 m = valuePattern.match(line)\r
4231a819 74 if m is not None:\r
14239ee0
YZ
75 pcds.append((m.groups(0)[3], int(m.groups(0)[0], 16)))\r
76 return pcds\r
77\r
f51461c8
LG
78def _parseForGCC(lines, efifilepath):\r
79 """ Parse map file generated by GCC linker """\r
2a9aac46 80 dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$')\r
f51461c8
LG
81 status = 0\r
82 imageBase = -1\r
83 sections = []\r
84 bpcds = []\r
3e7e8571 85 for index, line in enumerate(lines):\r
f51461c8
LG
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
e8a47801 98 # status handler\r
3e7e8571 99 if status == 3:\r
1eb72acd 100 m = valuePatternGcc.match(line)\r
4231a819 101 if m is not None:\r
f51461c8 102 sections.append(m.groups(0))\r
3e7e8571 103 if status == 3:\r
2a9aac46 104 m = dataPattern.match(line)\r
4231a819 105 if m is not None:\r
3e7e8571
YZ
106 if lines[index + 1]:\r
107 PcdName = m.groups(0)[0]\r
1eb72acd 108 m = pcdPatternGcc.match(lines[index + 1].strip())\r
4231a819 109 if m is not None:\r
ccaa7754 110 bpcds.append((PcdName, int(m.groups(0)[0], 16), int(sections[-1][1], 16), sections[-1][0]))\r
f7496d71 111\r
e8a47801
LG
112 # get section information from efi file\r
113 efisecs = PeImageClass(efifilepath).SectionHeaderList\r
4231a819 114 if efisecs is None or len(efisecs) == 0:\r
e8a47801
LG
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
f51461c8 128 return pcds\r
f7496d71 129\r
f51461c8 130def _parseGeneral(lines, efifilepath):\r
f7496d71 131 """ For MSFT, ICC, EBC\r
f51461c8 132 @param lines line array for map file\r
f7496d71 133\r
f51461c8 134 @return a list which element hold (PcdName, Offset, SectionName)\r
47fea6af 135 """\r
f51461c8 136 status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
47fea6af 137 secs = [] # key = section name\r
f51461c8 138 bPcds = []\r
2a9aac46 139 symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)')\r
f51461c8
LG
140\r
141 for line in lines:\r
142 line = line.strip()\r
1eb72acd 143 if startPatternGeneral.match(line):\r
f51461c8
LG
144 status = 1\r
145 continue\r
1eb72acd 146 if addressPatternGeneral.match(line):\r
f51461c8
LG
147 status = 2\r
148 continue\r
2a9aac46 149 if line.startswith("entry point at"):\r
f51461c8 150 status = 3\r
47fea6af 151 continue\r
f51461c8 152 if status == 1 and len(line) != 0:\r
78e75991 153 m = secReGeneral.match(line)\r
4231a819 154 assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
f51461c8
LG
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
4231a819 159 assert m is not None, "Fail to parse the symbol in map file, line is %s" % line\r
f51461c8 160 sec_no, sym_offset, sym_name, vir_addr = m.groups(0)\r
47fea6af 161 sec_no = int(sec_no, 16)\r
f51461c8 162 sym_offset = int(sym_offset, 16)\r
47fea6af 163 vir_addr = int(vir_addr, 16)\r
2a9aac46 164 m2 = symPattern.match(sym_name)\r
4231a819 165 if m2 is not None:\r
f51461c8
LG
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
4231a819 175 if efisecs is None or len(efisecs) == 0:\r
f51461c8 176 return None\r
f7496d71 177\r
f51461c8
LG
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
f7496d71 188\r
f51461c8
LG
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
f7496d71 196\r
f51461c8
LG
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
f7496d71 201 #print 'Success to generate Binary Patch PCD table at %s!' % pcdpath\r
47fea6af 202\r
f51461c8
LG
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
f7496d71 213\r
f51461c8
LG
214 (options, args) = parser.parse_args()\r
215\r
4231a819 216 if options.mapfile is None or options.efifile is None:\r
72443dd2 217 print(parser.get_usage())\r
f51461c8 218 elif os.path.exists(options.mapfile) and os.path.exists(options.efifile):\r
47fea6af 219 list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)\r
4231a819
CJ
220 if list is not None:\r
221 if options.outfile is not None:\r
f51461c8
LG
222 generatePcdTable(list, options.outfile)\r
223 else:\r
47fea6af 224 generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))\r
f51461c8 225 else:\r
72443dd2 226 print('Fail to generate Patch PCD Table based on map file and efi file')\r
f51461c8 227 else:\r
72443dd2 228 print('Fail to generate Patch PCD Table for fail to find map file or efi file!')\r