]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | #!/usr/bin/env python\r |
2 | #\r | |
3 | #\r | |
4 | # Copyright (c) 2007, Intel Corporation\r | |
5 | # All rights reserved. This program and the accompanying materials\r | |
6 | # are licensed and made available under the terms and conditions of the BSD License\r | |
7 | # which accompanies this distribution. The full text of the license may be found at\r | |
8 | # http://opensource.org/licenses/bsd-license.php\r | |
9 | # \r | |
10 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
13 | \r | |
14 | #\r | |
15 | # Import Modules\r | |
16 | #\r | |
17 | import re, os, glob\r | |
18 | from Common.XmlRoutines import *\r | |
19 | \r | |
20 | #"ModuleType"=>(PackageGuid, headerFileName) List\r | |
21 | HeaderFiles = {}\r | |
22 | GuidList = []\r | |
23 | GuidMap = {}\r | |
24 | HeaderFileContents = {}\r | |
25 | gTest = {}\r | |
26 | GuidMacro2CName = {}\r | |
27 | GuidAliasList = []\r | |
28 | \r | |
29 | def collectIncludeFolder(pkgDirName, guidType, pkgName):\r | |
30 | includeFolder = os.path.join(pkgDirName, "Include", guidType)\r | |
31 | if os.path.exists(includeFolder) and os.path.isdir(includeFolder):\r | |
32 | for headerFileName in os.listdir(includeFolder):\r | |
33 | if headerFileName[-2:] == ".h":\r | |
34 | headerFile = open(os.path.join(includeFolder, headerFileName))\r | |
35 | HeaderFileContents[(guidType, headerFileName, pkgName)] = headerFile.read()\r | |
36 | headerFile.close()\r | |
37 | \r | |
38 | GuidMacroReg = re.compile(r"\b(?!EFI_GUID\b)[A-Z0-9_]+_GUID\b")\r | |
39 | GuidCNameReg = re.compile(r"\bg\w+Guid\b")\r | |
40 | GuidAliasReg = re.compile(r"#define\s+([A-Z0-9_]+_GUID)\s+([A-Z0-9_]+_GUID)\b")\r | |
41 | \r | |
42 | def collectPackageInfo(spdFileName):\r | |
43 | pkgDirName = os.path.dirname(spdFileName)\r | |
44 | \r | |
45 | spd = XmlParseFile(spdFileName)\r | |
46 | \r | |
47 | pkgName = XmlElement(spd, "/PackageSurfaceArea/SpdHeader/PackageName")\r | |
48 | pkgGuid = XmlElement(spd, "/PackageSurfaceArea/SpdHeader/GuidValue")\r | |
49 | \r | |
50 | \r | |
51 | for IncludePkgHeader in XmlList(spd, "/PackageSurfaceArea/PackageHeaders/IncludePkgHeader"):\r | |
52 | moduleType = XmlAttribute(IncludePkgHeader, "ModuleType")\r | |
53 | headerFilePath = XmlElementData(IncludePkgHeader)\r | |
54 | headerFilePath = re.sub("Include/", "", headerFilePath, 1)\r | |
55 | \r | |
56 | headerTuple = HeaderFiles.get(moduleType, [])\r | |
57 | headerTuple.append((pkgGuid, headerFilePath))\r | |
58 | HeaderFiles[moduleType] = headerTuple\r | |
59 | \r | |
60 | guidTypes = ["Guid", "Protocol", "Ppi"]\r | |
61 | \r | |
62 | for guidType in guidTypes:\r | |
63 | for guidEntry in XmlList(spd, "/PackageSurfaceArea/" + guidType + "Declarations/Entry"):\r | |
64 | guidCName = XmlElement(guidEntry, "Entry/C_Name")\r | |
65 | GuidList.append(guidCName)\r | |
66 | \r | |
67 | collectIncludeFolder(pkgDirName, guidType, pkgName) \r | |
68 | \r | |
69 | for DecFile in glob.glob(os.path.join(pkgDirName, "*.dec")):\r | |
70 | fileContents = open(DecFile).read()\r | |
71 | for GuidCNameMatch in GuidCNameReg.finditer(fileContents):\r | |
72 | GuidCName = GuidCNameMatch.group(0)\r | |
73 | if GuidCName not in GuidList:\r | |
74 | GuidList.append(GuidCName)\r | |
75 | \r | |
76 | def AddGuidMacro2GuidCName(GuidMacros, GuidCNames):\r | |
77 | for GuidMacro in GuidMacros:\r | |
78 | GuessGuidCName = "g" + GuidMacro.lower().title().replace("_", "")\r | |
79 | if GuessGuidCName in GuidCNames:\r | |
80 | GuidMacro2CName[GuidMacro] = GuessGuidCName\r | |
81 | elif len(GuidCNames) == 1:\r | |
82 | GuidMacro2CName[GuidMacro] = GuidCNames[0]\r | |
83 | else:\r | |
84 | for GuidCName in GuidCNames:\r | |
85 | if GuidCName.lower() == GuessGuidCName.lower():\r | |
86 | GuidMacro2CName[GuidMacro] = GuidCName\r | |
87 | break\r | |
88 | else:\r | |
89 | pass\r | |
90 | #print "No matching GuidMacro %s" % GuidMacro\r | |
91 | \r | |
92 | \r | |
93 | def TranslateGuid(GuidMacroMatch):\r | |
94 | GuidMacro = GuidMacroMatch.group(0)\r | |
95 | return GuidMacro2CName.get(GuidMacro, GuidMacro)\r | |
96 | \r | |
97 | DepexReg = re.compile(r"DEPENDENCY_START(.*?)DEPENDENCY_END", re.DOTALL)\r | |
98 | \r | |
99 | def TranslateDpxSection(fileContents):\r | |
100 | DepexMatch = DepexReg.search(fileContents)\r | |
101 | if not DepexMatch:\r | |
102 | return "", []\r | |
103 | \r | |
104 | fileContents = DepexMatch.group(1)\r | |
105 | fileContents = re.sub(r"\s+", " ", fileContents).strip()\r | |
106 | fileContents = GuidMacroReg.sub(TranslateGuid, fileContents)\r | |
107 | return fileContents, GuidMacroReg.findall(fileContents)\r | |
108 | \r | |
109 | def InitializeAutoGen(workspace, db):\r | |
110 | \r | |
111 | \r | |
112 | for spdFile in XmlList(db, "/FrameworkDatabase/PackageList/Filename"):\r | |
113 | spdFileName = XmlElementData(spdFile)\r | |
114 | collectPackageInfo(os.path.join(workspace, spdFileName))\r | |
115 | \r | |
116 | \r | |
117 | BlockCommentReg = re.compile(r"/\*.*?\*/", re.DOTALL)\r | |
118 | LineCommentReg = re.compile(r"//.*")\r | |
119 | GuidReg = re.compile(r"\b(" + '|'.join(GuidList) + r")\b")\r | |
120 | \r | |
121 | for headerFile in HeaderFileContents:\r | |
122 | Contents = HeaderFileContents[headerFile]\r | |
123 | Contents = BlockCommentReg.sub("", Contents)\r | |
124 | Contents = LineCommentReg.sub("", Contents)\r | |
125 | \r | |
126 | FoundGuids = GuidReg.findall(Contents)\r | |
127 | for FoundGuid in FoundGuids:\r | |
128 | GuidMap[FoundGuid] = "%s/%s" % (headerFile[0], headerFile[1])\r | |
129 | #print "%-40s %s/%s" % (FoundGuid, headerFile[0], headerFile[1])\r | |
130 | \r | |
131 | GuidMacros = GuidMacroReg.findall(Contents)\r | |
132 | GuidCNames = GuidCNameReg.findall(Contents)\r | |
133 | \r | |
134 | for GuidAliasMatch in GuidAliasReg.finditer(Contents):\r | |
135 | Name1, Name2 = GuidAliasMatch.group(1), GuidAliasMatch.group(2)\r | |
136 | GuidAliasList.append((Name1, Name2))\r | |
137 | \r | |
138 | AddGuidMacro2GuidCName(GuidMacros, GuidCNames)\r | |
139 | \r | |
140 | def AddSystemIncludeStatement(moduleType, PackageList):\r | |
141 | IncludeStatement = "\n"\r | |
142 | \r | |
143 | headerList = HeaderFiles.get(moduleType, [])\r | |
144 | \r | |
145 | for pkgGuid in PackageList:\r | |
146 | \r | |
147 | for pkgTuple in headerList:\r | |
148 | if pkgTuple[0] == pkgGuid:\r | |
149 | IncludeStatement += "#include <%s>\n" % pkgTuple[1]\r | |
150 | \r | |
151 | return IncludeStatement\r | |
152 | \r | |
153 | \r | |
154 | def AddLibraryClassStatement(LibraryClassList):\r | |
155 | IncludeStatement = "\n"\r | |
156 | for LibraryClass in LibraryClassList:\r | |
157 | IncludeStatement += "#include <Library/%s.h>\n" % LibraryClass\r | |
158 | \r | |
159 | return IncludeStatement\r | |
160 | \r | |
161 | def AddGuidStatement(GuidList):\r | |
162 | IncludeStatement = "\n"\r | |
163 | GuidIncludeSet = {}\r | |
164 | for Guid in GuidList:\r | |
165 | if Guid in GuidMap:\r | |
166 | GuidIncludeSet[GuidMap[Guid]] = 1\r | |
167 | else:\r | |
168 | print "GUID CName: %s cannot be found in any public header file" % Guid\r | |
169 | \r | |
170 | for GuidInclude in GuidIncludeSet:\r | |
171 | IncludeStatement += "#include <%s>\n" % GuidInclude\r | |
172 | \r | |
173 | return IncludeStatement\r | |
174 | \r | |
175 | DriverBindingMap = {\r | |
176 | "gEfiDriverBindingProtocolGuid" : "EFI_DRIVER_BINDING_PROTOCOL",\r | |
177 | "gEfiComponentNameProtocolGuid" : "EFI_COMPONENT_NAME_PROTOCOL",\r | |
178 | "gEfiDriverConfigurationProtocolGuid" : "EFI_DRIVER_CONFIGURATION_PROTOCOL",\r | |
179 | "gEfiDriverDiagnosticProtocolGuid" : "EFI_DRIVER_CONFIGURATION_PROTOCOL"\r | |
180 | }\r | |
181 | \r | |
182 | def AddDriverBindingProtocolStatement(AutoGenDriverModel):\r | |
183 | InstallStatement = "\n"\r | |
184 | DBindingHandle = "ImageHandle"\r | |
185 | GlobalDeclaration = "\n"\r | |
186 | \r | |
187 | \r | |
188 | for DriverModelItem in AutoGenDriverModel:\r | |
189 | \r | |
190 | if DriverModelItem[1] == "NULL" and DriverModelItem[2] == "NULL" and DriverModelItem[3] == "NULL":\r | |
191 | InstallStatement += " Status = EfiLibInstallDriverBinding (\n"\r | |
192 | InstallStatement += " ImageHandle,\n"\r | |
193 | InstallStatement += " SystemTable,\n"\r | |
194 | InstallStatement += " %s,\n" % DriverModelItem[0]\r | |
195 | InstallStatement += " %s\n" % DBindingHandle\r | |
196 | InstallStatement += " );\n"\r | |
197 | else:\r | |
198 | InstallStatement += " Status = EfiLibInstallAllDriverProtocols (\n"\r | |
199 | InstallStatement += " ImageHandle,\n"\r | |
200 | InstallStatement += " SystemTable,\n"\r | |
201 | InstallStatement += " %s,\n" % DriverModelItem[0]\r | |
202 | InstallStatement += " %s,\n" % DBindingHandle\r | |
203 | InstallStatement += " %s,\n" % DriverModelItem[1]\r | |
204 | InstallStatement += " %s,\n" % DriverModelItem[2]\r | |
205 | InstallStatement += " %s\n" % DriverModelItem[3]\r | |
206 | InstallStatement += " );\n"\r | |
207 | \r | |
208 | InstallStatement += " ASSERT_EFI_ERROR (Status);\n\n"\r | |
209 | \r | |
210 | GlobalDeclaration += "extern EFI_DRIVER_BINDING_PROTOCOL %s;\n" % DriverModelItem[0][1:]\r | |
211 | if (DriverModelItem[1] != "NULL"):\r | |
212 | GlobalDeclaration += "extern EFI_COMPONENT_NAME_PROTOCOL %s;\n" % DriverModelItem[1][1:]\r | |
213 | if (DriverModelItem[2] != "NULL"):\r | |
214 | GlobalDeclaration += "extern EFI_DRIVER_CONFIGURATION_PROTOCOL %s;\n" % DriverModelItem[2][1:]\r | |
215 | if (DriverModelItem[3] != "NULL"):\r | |
216 | GlobalDeclaration += "extern EFI_DRIVER_CONFIGURATION_PROTOCOL %s;\n" % DriverModelItem[3][1:]\r | |
217 | \r | |
218 | DBindingHandle = "NULL"\r | |
219 | \r | |
220 | return (InstallStatement, "", "", GlobalDeclaration)\r | |
221 | \r | |
222 | EventDeclarationTemplate = """\r | |
223 | //\r | |
224 | // Declaration for callback Event.\r | |
225 | //\r | |
226 | VOID\r | |
227 | EFIAPI\r | |
228 | %s (\r | |
229 | IN EFI_EVENT Event,\r | |
230 | IN VOID *Context\r | |
231 | );\r | |
232 | """\r | |
233 | \r | |
234 | def AddBootServiceEventStatement(EventList):\r | |
235 | FinalEvent = ""\r | |
236 | if len(EventList) > 1:\r | |
237 | \r | |
238 | print "Current prototype does not support multi boot service event"\r | |
239 | else:\r | |
240 | FinalEvent = EventList[0]\r | |
241 | \r | |
242 | CreateStatement = "\n"\r | |
243 | CreateStatement += " Status = gBS->CreateEvent (\n"\r | |
244 | CreateStatement += " EVT_SIGNAL_EXIT_BOOT_SERVICES,\n"\r | |
245 | CreateStatement += " EFI_TPL_NOTIFY,\n"\r | |
246 | CreateStatement += " " + FinalEvent + ",\n"\r | |
247 | CreateStatement += " NULL,\n"\r | |
248 | CreateStatement += " &mExitBootServicesEvent\n"\r | |
249 | CreateStatement += " );\n"\r | |
250 | CreateStatement += " ASSERT_EFI_ERROR (Status);\n"\r | |
251 | \r | |
252 | GlobalDefinition = "\n"\r | |
253 | GlobalDefinition += "STATIC EFI_EVENT mExitBootServicesEvent = NULL;\n"\r | |
254 | \r | |
255 | GlobalDeclaration = EventDeclarationTemplate % FinalEvent\r | |
256 | \r | |
257 | DestroyStatement = "\n"\r | |
258 | DestroyStatement += " Status = gBS->CloseEvent (mExitBootServicesEvent);\n"\r | |
259 | DestroyStatement += " ASSERT_EFI_ERROR (Status);\n"\r | |
260 | return (CreateStatement, "", GlobalDefinition, GlobalDeclaration)\r | |
261 | \r | |
262 | def AddVirtualAddressEventStatement(EventList):\r | |
263 | FinalEvent = ""\r | |
264 | if len(EventList) > 1:\r | |
265 | print "Current prototype does not support multi virtual address change event"\r | |
266 | else:\r | |
267 | FinalEvent = EventList[0]\r | |
268 | \r | |
269 | CreateStatement = "\n"\r | |
270 | \r | |
271 | CreateStatement += " Status = gBS->CreateEvent (\n"\r | |
272 | CreateStatement += " EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE,\n"\r | |
273 | CreateStatement += " TPL_NOTIFY,\n"\r | |
274 | CreateStatement += " " + FinalEvent + ",\n"\r | |
275 | CreateStatement += " NULL,\n"\r | |
276 | CreateStatement += " &mVirtualAddressChangedEvent\n"\r | |
277 | CreateStatement += " );\n"\r | |
278 | CreateStatement += " ASSERT_EFI_ERROR (Status);\n"\r | |
279 | \r | |
280 | GlobalDefinition = "\n"\r | |
281 | GlobalDefinition += "STATIC EFI_EVENT mVirtualAddressChangedEvent = NULL;\n"\r | |
282 | \r | |
283 | GlobalDeclaration = EventDeclarationTemplate % FinalEvent\r | |
284 | \r | |
285 | DestroyStatement = "\n"\r | |
286 | DestroyStatement += " Status = gBS->CloseEvent (mVirtualAddressChangedEvent);\n"\r | |
287 | DestroyStatement += " ASSERT_EFI_ERROR (Status);\n"\r | |
288 | \r | |
289 | return (CreateStatement, "", GlobalDefinition, GlobalDeclaration)\r | |
290 | \r | |
291 | \r | |
292 | EntryPointDeclarationTemplate = """\r | |
293 | //\r | |
294 | // Declaration for original Entry Point. \r | |
295 | //\r | |
296 | EFI_STATUS\r | |
297 | EFIAPI\r | |
298 | %s (\r | |
299 | IN EFI_HANDLE ImageHandle,\r | |
300 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
301 | );\r | |
302 | """\r | |
303 | \r | |
304 | EntryPointHeader = r"""\r | |
305 | /**\r | |
306 | The user Entry Point for module %s. The user code starts with this function.\r | |
307 | \r | |
308 | @param[in] ImageHandle The firmware allocated handle for the EFI image. \r | |
309 | @param[in] SystemTable A pointer to the EFI System Table.\r | |
310 | \r | |
311 | @retval EFI_SUCCESS The entry point is executed successfully.\r | |
312 | @retval other Some error occurs when executing this entry point.\r | |
313 | \r | |
314 | **/\r | |
315 | """\r | |
316 | def AddNewEntryPointContentsStatement (moduleName, EntryPoint, InstallStatement = ""):\r | |
317 | if EntryPoint != "Initialize%s" % moduleName:\r | |
318 | NewEntryPoint = "Initialize%s" % moduleName\r | |
319 | else:\r | |
320 | NewEntryPoint = "NewInitialize%s" % moduleName\r | |
321 | \r | |
322 | EntryPointContents = EntryPointHeader % moduleName\r | |
323 | EntryPointContents += "EFI_STATUS\n"\r | |
324 | EntryPointContents += "EFIAPI\n"\r | |
325 | EntryPointContents += NewEntryPoint + "(\n"\r | |
326 | EntryPointContents += " IN EFI_HANDLE ImageHandle,\n"\r | |
327 | EntryPointContents += " IN EFI_SYSTEM_TABLE *SystemTable\n"\r | |
328 | EntryPointContents += " )\n"\r | |
329 | EntryPointContents += "{\n"\r | |
330 | EntryPointContents += " EFI_STATUS Status;\n"\r | |
331 | EntryPointContents += InstallStatement + "\n"\r | |
332 | GlobalDeclaration = ""\r | |
333 | \r | |
334 | if EntryPoint != "":\r | |
335 | EntryPointContents += " //\n // Call the original Entry Point\n //\n"\r | |
336 | EntryPointContents += " Status = %s (ImageHandle, SystemTable);\n\n" % EntryPoint\r | |
337 | GlobalDeclaration += EntryPointDeclarationTemplate % EntryPoint\r | |
338 | \r | |
339 | EntryPointContents += " return Status;\n"\r | |
340 | EntryPointContents += "}\n"\r | |
341 | \r | |
342 | return (NewEntryPoint, EntryPointContents, GlobalDeclaration)\r | |
343 | \r | |
344 | reFileHeader = re.compile(r"^\s*/\*.*?\*/\s*", re.DOTALL)\r | |
345 | reNext = re.compile(r"#ifndef\s*(\w+)\s*#define\s*\1\s*")\r | |
346 | \r | |
347 | def AddCommonInclusionStatement(fileContents, includeStatement):\r | |
348 | if includeStatement in fileContents:\r | |
349 | return fileContents\r | |
350 | \r | |
351 | insertPos = 0\r | |
352 | matchFileHeader = reFileHeader.search(fileContents)\r | |
353 | if matchFileHeader:\r | |
354 | insertPos = matchFileHeader.end()\r | |
355 | \r | |
356 | matchFileHeader = reNext.search(fileContents, insertPos)\r | |
357 | if matchFileHeader:\r | |
358 | insertPos = matchFileHeader.end()\r | |
359 | \r | |
360 | includeStatement = "\n%s\n\n" % includeStatement \r | |
361 | fileContents = fileContents[0:insertPos] + includeStatement + fileContents[insertPos:] \r | |
362 | return fileContents \r | |
363 | \r | |
364 | # This acts like the main() function for the script, unless it is 'import'ed into another\r | |
365 | # script.\r | |
366 | if __name__ == '__main__':\r | |
367 | \r | |
368 | pass\r | |
369 | \r |