Commit | Line | Data |
---|---|---|
52302d4d LG |
1 | ## @file\r |
2 | # Generate PCD table for 'Patchable In Module' type PCD with given .map file.\r | |
f51461c8 LG |
3 | # The Patch PCD table like:\r |
4 | # \r | |
5 | # PCD Name Offset in binary\r | |
52302d4d LG |
6 | # ======== ================\r |
7 | #\r | |
7e7a8116 | 8 | # Copyright (c) 2008 - 2016, Intel Corporation. All rights reserved.<BR>\r |
40d841f6 | 9 | # This program and the accompanying materials\r |
52302d4d LG |
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 | |
f51461c8 LG |
17 | #\r |
18 | \r | |
19 | #====================================== External Libraries ========================================\r | |
20 | import optparse\r | |
1be2ed90 | 21 | import Common.LongFilePathOs as os\r |
f51461c8 LG |
22 | import re\r |
23 | import array\r | |
24 | \r | |
25 | from Common.BuildToolError import *\r | |
26 | import Common.EdkLogger as EdkLogger\r | |
52302d4d | 27 | from Common.Misc import PeImageClass\r |
b36d134f | 28 | from Common.BuildVersion import gBUILD_VERSION\r |
1be2ed90 | 29 | from Common.LongFilePathSupport import OpenLongFilePath as open\r |
f51461c8 LG |
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 | |
39 | secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r | |
7e7a8116 | 40 | symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.\-:\\\\\w\?@\$<>]+) +([\da-fA-F]+)', re.UNICODE)\r |
f51461c8 LG |
41 | \r |
42 | def 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 | |
e4ac870f LG |
57 | firstline = lines[0].strip()\r |
58 | if (firstline.startswith("Archive member included ") and\r | |
59 | firstline.endswith(" file (symbol)")):\r | |
f51461c8 LG |
60 | return _parseForGCC(lines, efifilepath)\r |
61 | return _parseGeneral(lines, efifilepath)\r | |
62 | \r | |
63 | def _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 | |
3e7e8571 | 69 | for index, line in enumerate(lines):\r |
f51461c8 LG |
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 | |
e8a47801 | 82 | # status handler\r |
3e7e8571 | 83 | if status == 3:\r |
f51461c8 LG |
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 | |
3e7e8571 YZ |
87 | if status == 3:\r |
88 | m = re.match('^.data._gPcd_BinaryPatch_([\w_\d]+)$', line)\r | |
f51461c8 | 89 | if m != None:\r |
3e7e8571 YZ |
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 | |
e8a47801 LG |
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 | |
f51461c8 LG |
112 | return pcds\r |
113 | \r | |
114 | def _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 | |
47fea6af | 119 | """\r |
f51461c8 | 120 | status = 0 #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r |
47fea6af | 121 | secs = [] # key = section name\r |
f51461c8 | 122 | bPcds = []\r |
47fea6af | 123 | \r |
f51461c8 LG |
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 | |
47fea6af | 135 | continue\r |
f51461c8 | 136 | if status == 1 and len(line) != 0:\r |
47fea6af | 137 | m = secRe.match(line)\r |
f51461c8 LG |
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 | |
47fea6af | 145 | sec_no = int(sec_no, 16)\r |
f51461c8 | 146 | sym_offset = int(sym_offset, 16)\r |
47fea6af | 147 | vir_addr = int(vir_addr, 16)\r |
f51461c8 LG |
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 | |
173 | def 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 | |
47fea6af | 186 | \r |
f51461c8 LG |
187 | if __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 | |
47fea6af | 203 | list = parsePcdInfoFromMapFile(options.mapfile, options.efifile)\r |
f51461c8 LG |
204 | if list != None:\r |
205 | if options.outfile != None:\r | |
206 | generatePcdTable(list, options.outfile)\r | |
207 | else:\r | |
47fea6af | 208 | generatePcdTable(list, options.mapfile.replace('.map', '.BinaryPcdTable.txt'))\r |
f51461c8 LG |
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 |