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