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