]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
- Move global declarations from AutoGen.c to AutoGen.h
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / autogen / AutoGen.java
1 /** @file
2 AutoGen class.
3
4 This class is to generate Autogen.h and Autogen.c according to module surface area
5 or library surface area.
6
7 Copyright (c) 2006, Intel Corporation
8 All rights reserved. This program and the accompanying materials
9 are licensed and made available under the terms and conditions of the BSD License
10 which accompanies this distribution. The full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
12
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15
16 **/
17
18 package org.tianocore.build.autogen;
19
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.FileReader;
24 import java.io.FileWriter;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.LinkedHashSet;
30 import java.util.LinkedList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 import org.apache.tools.ant.BuildException;
36 import org.apache.xmlbeans.XmlObject;
37 import org.tianocore.build.exception.AutoGenException;
38 import org.tianocore.build.global.GlobalData;
39 import org.tianocore.build.global.SurfaceAreaQuery;
40 import org.tianocore.build.id.ModuleIdentification;
41 import org.tianocore.build.id.PackageIdentification;
42 import org.tianocore.build.pcd.action.PCDAutoGenAction;
43 import org.tianocore.common.definitions.ToolDefinitions;
44 import org.tianocore.common.definitions.EdkDefinitions;
45 import org.tianocore.common.exception.EdkException;
46 import org.tianocore.common.logger.EdkLog;
47
48 /**
49 This class is to generate Autogen.h and Autogen.c according to module surface
50 area or library surface area.
51 **/
52 public class AutoGen {
53 ///
54 /// The output path of Autogen.h and Autogen.c
55 ///
56 private String outputPath;
57
58 ///
59 /// The name of FV directory
60 ///
61 private String fvDir;
62
63 ///
64 /// The base name of module or library.
65 ///
66 private ModuleIdentification moduleId;
67
68 ///
69 /// The build architecture
70 ///
71 private String arch;
72
73 ///
74 /// PcdAutogen instance which is used to manage how to generate the PCD
75 /// information.
76 ///
77 private PCDAutoGenAction myPcdAutogen;
78
79 ///
80 /// the one of type : NOT_PCD_DRIVER, PEI_PCD_DRIVER, DXE_PCD_DRIVER
81 ///
82 private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;
83
84 ///
85 /// The protocl list which records in module or library surface area and
86 /// it's dependence on library instance surface area.
87 ///
88 private Set<String> mProtocolList = new HashSet<String>();
89
90 ///
91 /// The Ppi list which recorded in module or library surface area and its
92 /// dependency on library instance surface area.
93 ///
94 private Set<String> mPpiList = new HashSet<String>();
95
96 ///
97 /// The Guid list which recoreded in module or library surface area and it's
98 /// dependence on library instance surface area.
99 ///
100 private Set<String> mGuidList = new HashSet<String>();
101
102 ///
103 /// The dependence package list which recoreded in module or library surface
104 /// area and it's dependence on library instance surface area.
105 ///
106 private List<PackageIdentification> mDepPkgList = new LinkedList<PackageIdentification>();
107
108 ///
109 /// For non library module, add its library instance's construct and destructor to
110 /// list. String[0] recode LibConstructor name, String[1] recode Lib instance
111 /// module type.
112 ///
113 private List<String[]> libConstructList = new ArrayList<String[]>();
114 private List<String[]> libDestructList = new ArrayList<String[]>();
115
116 ///
117 /// List to store SetVirtalAddressMapCallBack, ExitBootServiceCallBack
118 ///
119 private List<String> setVirtalAddList = new ArrayList<String>();
120 private List<String> exitBootServiceList = new ArrayList<String>();
121
122 private StringBuffer functionDeclarations = new StringBuffer(10240);
123 private StringBuffer globalDeclarations = new StringBuffer(10240);
124
125 //
126 // flag of PcdComponentNameDisable, PcdDriverDiagnosticDisable
127 //
128 private boolean componentNamePcd = false;
129 private boolean driverDiagnostPcd = false;
130
131 //
132 // Instance of SurfaceAreaQuery
133 //
134 private SurfaceAreaQuery saq = null;
135
136 private ModuleIdentification parentId = null;
137
138 /**
139 Construct function
140
141 This function mainly initialize some member variable.
142
143 @param fvDir
144 Absolute path of FV directory.
145 @param outputPath
146 Output path of AutoGen file.
147 @param moduleId
148 Module identification.
149 @param arch
150 Target architecture.
151 **/
152 public AutoGen(String fvDir, String outputPath, ModuleIdentification moduleId, String arch, SurfaceAreaQuery saq, ModuleIdentification parentId) {
153 this.outputPath = outputPath;
154 this.moduleId = moduleId;
155 this.arch = arch;
156 this.fvDir = fvDir;
157 this.saq = saq;
158 this.parentId = parentId;
159 }
160
161 /**
162 saveFile function
163
164 This function save the content in stringBuffer to file.
165
166 @param fileName
167 The name of file.
168 @param fileBuffer
169 The content of AutoGen file in buffer.
170 @return boolean
171 "true" successful
172 "false" failed
173 **/
174 private boolean saveFile(String fileName, StringBuffer fileBuffer) {
175
176 File autoGenH = new File(fileName);
177
178 //
179 // if the file exists, compare their content
180 //
181 if (autoGenH.exists()) {
182 char[] oldFileBuffer = new char[(int) autoGenH.length()];
183 try {
184 FileReader fIn = new FileReader(autoGenH);
185 fIn.read(oldFileBuffer, 0, (int) autoGenH.length());
186 fIn.close();
187 } catch (IOException e) {
188 EdkLog.log(EdkLog.EDK_INFO, this.moduleId.getName()
189 + "'s "
190 + fileName
191 + " is exist, but can't be open!!");
192 return false;
193 }
194
195 //
196 // if we got the same file, don't re-generate it to prevent
197 // sources depending on it from re-building
198 //
199 if (fileBuffer.toString().compareTo(new String(oldFileBuffer)) == 0) {
200 return true;
201 }
202 }
203
204 try {
205 FileWriter fOut = new FileWriter(autoGenH);
206 fOut.write(fileBuffer.toString());
207 fOut.flush();
208 fOut.close();
209 } catch (IOException e) {
210 EdkLog.log(EdkLog.EDK_INFO, this.moduleId.getName()
211 + "'s "
212 + fileName
213 + " can't be create!!");
214 return false;
215 }
216 return true;
217 }
218
219 /**
220 genAutogen function
221
222 This function call libGenAutoGen or moduleGenAutogen function, which
223 dependence on generate library autogen or module autogen.
224
225 @throws BuildException
226 Failed to creat AutoGen.c & AutoGen.h.
227 **/
228 public void genAutogen() throws EdkException {
229 //
230 // If outputPath do not exist, create it.
231 //
232 File path = new File(outputPath);
233 path.mkdirs();
234
235 //
236 // Check current is library or not, then call the corresponding
237 // function.
238 //
239 if (this.moduleId.isLibrary()) {
240 libGenAutogen();
241 } else {
242 moduleGenAutogen();
243 }
244 }
245
246 /**
247 moduleGenAutogen function
248
249 This function generates AutoGen.c & AutoGen.h for module.
250
251 @throws BuildException
252 Faile to create module AutoGen.c & AutoGen.h.
253 **/
254 void moduleGenAutogen() throws EdkException {
255 setPcdComponentName();
256 setPcdDriverDiagnostic();
257 collectLibInstanceInfo();
258 moduleGenAutogenC();
259 moduleGenAutogenH();
260 }
261
262 /**
263 libGenAutogen function
264
265 This function generates AutoGen.c & AutoGen.h for library.
266
267 @throws BuildException
268 Faile to create library AutoGen.c & AutoGen.h
269 **/
270 void libGenAutogen() throws EdkException {
271 libGenAutogenC();
272 libGenAutogenH();
273 }
274
275 /**
276 moduleGenAutogenH
277
278 This function generates AutoGen.h for module.
279
280 @throws BuildException
281 Failed to generate AutoGen.h.
282 **/
283 void moduleGenAutogenH() throws EdkException {
284
285 Set<String> libClassIncludeH;
286 String moduleType;
287 // List<String> headerFileList;
288 List<String> headerFileList;
289 Iterator item;
290 StringBuffer fileBuffer = new StringBuffer(8192);
291
292 //
293 // Write Autogen.h header notation
294 //
295 fileBuffer.append(CommonDefinition.AUTOGENHNOTATION);
296
297 //
298 // Add #ifndef ${BaseName}_AUTOGENH
299 // #def ${BseeName}_AUTOGENH
300 //
301 fileBuffer.append(CommonDefinition.IFNDEF
302 + CommonDefinition.AUTOGENH
303 + this.moduleId.getGuid().replaceAll("-", "_")
304 + ToolDefinitions.LINE_SEPARATOR);
305 fileBuffer.append(CommonDefinition.DEFINE
306 + CommonDefinition.AUTOGENH
307 + this.moduleId.getGuid().replaceAll("-", "_")
308 + ToolDefinitions.LINE_SEPARATOR
309 + ToolDefinitions.LINE_SEPARATOR);
310
311 //
312 // Write the specification version and release version at the begine
313 // of autogen.h file.
314 // Note: the specification version and release version should
315 // be got from module surface area instead of hard code by it's
316 // moduleType.
317 //
318 moduleType = saq.getModuleType();
319
320 //
321 // Add "extern int __make_me_compile_correctly;" at begin of
322 // AutoGen.h.
323 //
324 fileBuffer.append(CommonDefinition.AUTOGENHBEGIN);
325
326 //
327 // Put EFI_SPECIFICATION_VERSION, and EDK_RELEASE_VERSION.
328 //
329 String[] specList = saq.getExternSpecificaiton();
330 for (int i = 0; i < specList.length; i++) {
331 fileBuffer.append(CommonDefinition.DEFINE + specList[i]
332 + "\r\n");
333 }
334 //
335 // Write consumed package's mdouleInfo related .h file to autogen.h
336 //
337 // PackageIdentification[] consumedPkgIdList = SurfaceAreaQuery
338 // .getDependencePkg(this.arch);
339 PackageIdentification[] consumedPkgIdList = saq.getDependencePkg(this.arch);
340 if (consumedPkgIdList != null) {
341 headerFileList = depPkgToAutogenH(consumedPkgIdList, moduleType);
342 item = headerFileList.iterator();
343 while (item.hasNext()) {
344 fileBuffer.append(item.next().toString());
345 }
346 }
347
348 //
349 // Write library class's related *.h file to autogen.h.
350 //
351 String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch, null);
352 if (libClassList.length > 0) {
353 libClassIncludeH = LibraryClassToAutogenH(libClassList);
354 item = libClassIncludeH.iterator();
355 while (item.hasNext()) {
356 fileBuffer.append(item.next().toString());
357 }
358 }
359
360 libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch, null);
361 if (libClassList.length > 0) {
362 libClassIncludeH = LibraryClassToAutogenH(libClassList);
363 item = libClassIncludeH.iterator();
364 while (item.hasNext()) {
365 fileBuffer.append(item.next().toString());
366 }
367 }
368 fileBuffer.append("\r\n");
369
370 //
371 // If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to
372 // {DEST_DIR_DRBUG}/FlashMap.h
373 //
374 if (saq.isHaveTianoR8FlashMap()) {
375 fileBuffer.append(CommonDefinition.INCLUDE);
376 fileBuffer.append(" <");
377 fileBuffer.append(CommonDefinition.TIANOR8PLASHMAPH + ">\r\n");
378 copyFlashMapHToDebugDir();
379 }
380
381 // Write PCD autogen information to AutoGen.h.
382 //
383 if (this.myPcdAutogen != null) {
384 fileBuffer.append("\r\n");
385 fileBuffer.append(this.myPcdAutogen.getHAutoGenString());
386 }
387
388 fileBuffer.append(globalDeclarations);
389 fileBuffer.append(functionDeclarations);
390 //
391 // Append the #endif at AutoGen.h
392 //
393 fileBuffer.append("#endif\r\n");
394
395 //
396 // Save string buffer content in AutoGen.h.
397 //
398 if (!saveFile(outputPath + File.separatorChar + "AutoGen.h", fileBuffer)) {
399 throw new AutoGenException("Failed to generate AutoGen.h !!!");
400 }
401 }
402
403 /**
404 moduleGenAutogenC
405
406 This function generates AutoGen.c for module.
407
408 @throws BuildException
409 Failed to generate AutoGen.c.
410 **/
411 void moduleGenAutogenC() throws EdkException {
412
413 StringBuffer fileBuffer = new StringBuffer(8192);
414 //
415 // Write Autogen.c header notation
416 //
417 fileBuffer.append(CommonDefinition.AUTOGENCNOTATION);
418
419 //
420 // Get the native MSA file infomation. Since before call autogen,
421 // the MSA native <Externs> information were overrided. So before
422 // process <Externs> it should be set the DOC as the Native MSA info.
423 //
424 Map<String, XmlObject> doc = GlobalData.getNativeMsa(this.moduleId);
425 saq.push(doc);
426 //
427 // Write <Extern>
428 // DriverBinding/ComponentName/DriverConfiguration/DriverDialog
429 // to AutoGen.c
430 //
431
432 ExternsDriverBindingToAutoGenC(fileBuffer);
433
434 //
435 // Write DriverExitBootServicesEvent/DriverSetVirtualAddressMapEvent
436 // to Autogen.c
437 //
438 ExternCallBackToAutoGenC(fileBuffer);
439
440 //
441 // Write EntryPoint to autgoGen.c
442 //
443 String[] entryPointList = saq.getModuleEntryPointArray();
444 String[] unloadImageList = saq.getModuleUnloadImageArray();
445 EntryPointToAutoGen(CommonDefinition.remDupString(entryPointList),
446 CommonDefinition.remDupString(unloadImageList),
447 fileBuffer);
448
449 pcdDriverType = saq.getPcdDriverType();
450
451 //
452 // Restore the DOC which include the FPD module info.
453 //
454 saq.pop();
455
456 //
457 // Write Guid to autogen.c
458 //
459 String guid = CommonDefinition.formatGuidName(saq.getModuleGuid());
460 if (this.moduleId.getModuleType().equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
461 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED GUID gEfiCallerIdGuid = {");
462 } else {
463 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID gEfiCallerIdGuid = {");
464 }
465
466 if (guid == null) {
467 throw new AutoGenException("Guid value must set!\n");
468 }
469
470 //
471 // Formate Guid as ANSI c form.Example:
472 // {0xd2b2b828, 0x826, 0x48a7,{0xb3, 0xdf, 0x98, 0x3c, 0x0, 0x60, 0x24,
473 // 0xf0}}
474 //
475
476 fileBuffer.append(guid);
477 fileBuffer.append("};\r\n");
478
479 //
480 // Generate library instance consumed protocol, guid, ppi, pcd list.
481 // Save those to this.protocolList, this.ppiList, this.pcdList,
482 // this.guidList. Write Consumed library constructor and desconstuct to
483 // autogen.c
484 //
485 LibInstanceToAutogenC(fileBuffer);
486
487 //
488 // Get module dependent Package identification.
489 //
490 PackageIdentification[] packages = saq.getDependencePkg(this.arch);
491 for (int i = 0; i < packages.length; i++) {
492 if (!this.mDepPkgList.contains(packages[i])) {
493 this.mDepPkgList.add(packages[i]);
494 }
495
496 }
497
498 //
499 // Write consumed ppi, guid, protocol to autogen.c
500 //
501 ProtocolGuidToAutogenC(fileBuffer);
502 PpiGuidToAutogenC(fileBuffer);
503 GuidGuidToAutogenC(fileBuffer);
504
505 //
506 // Call pcd autogen.
507 //
508 this.myPcdAutogen = new PCDAutoGenAction(moduleId,
509 arch,
510 false,
511 null,
512 pcdDriverType,
513 parentId);
514
515 this.myPcdAutogen.execute();
516 if (this.myPcdAutogen != null) {
517 fileBuffer.append("\r\n");
518 fileBuffer.append(this.myPcdAutogen.getCAutoGenString());
519 }
520
521 if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {
522 throw new AutoGenException("Failed to generate AutoGen.c !!!");
523 }
524
525 }
526
527 /**
528 libGenAutogenH
529
530 This function generates AutoGen.h for library.
531
532 @throws BuildException
533 Failed to generate AutoGen.c.
534 **/
535 void libGenAutogenH() throws EdkException {
536
537 Set<String> libClassIncludeH;
538 String moduleType;
539 List<String> headerFileList;
540 Iterator item;
541 StringBuffer fileBuffer = new StringBuffer(10240);
542
543 //
544 // Write Autogen.h header notation
545 //
546 fileBuffer.append(CommonDefinition.AUTOGENHNOTATION);
547
548 //
549 // Add #ifndef ${BaseName}_AUTOGENH
550 // #def ${BseeName}_AUTOGENH
551 //
552 fileBuffer.append(CommonDefinition.IFNDEF
553 + CommonDefinition.AUTOGENH
554 + this.moduleId.getGuid().replaceAll("-", "_")
555 + ToolDefinitions.LINE_SEPARATOR);
556 fileBuffer.append(CommonDefinition.DEFINE
557 + CommonDefinition.AUTOGENH
558 + this.moduleId.getGuid().replaceAll("-", "_")
559 + ToolDefinitions.LINE_SEPARATOR
560 + ToolDefinitions.LINE_SEPARATOR);
561
562 //
563 // Write EFI_SPECIFICATION_VERSION and EDK_RELEASE_VERSION
564 // to autogen.h file.
565 // Note: the specification version and release version should
566 // be get from module surface area instead of hard code.
567 //
568 fileBuffer.append(CommonDefinition.AUTOGENHBEGIN);
569 String[] specList = saq.getExternSpecificaiton();
570 for (int i = 0; i < specList.length; i++) {
571 fileBuffer.append(CommonDefinition.DEFINE + specList[i] + "\r\n");
572 }
573 // fileBuffer.append(CommonDefinition.autoGenHLine1);
574 // fileBuffer.append(CommonDefinition.autoGenHLine2);
575
576 //
577 // Write consumed package's mdouleInfo related *.h file to autogen.h.
578 //
579 moduleType = saq.getModuleType();
580 PackageIdentification[] cosumedPkglist = saq.getDependencePkg(this.arch);
581 headerFileList = depPkgToAutogenH(cosumedPkglist, moduleType);
582 item = headerFileList.iterator();
583 while (item.hasNext()) {
584 fileBuffer.append(item.next().toString());
585 }
586 //
587 // Write library class's related *.h file to autogen.h
588 //
589 String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch, null);
590 if (libClassList != null) {
591 libClassIncludeH = LibraryClassToAutogenH(libClassList);
592 item = libClassIncludeH.iterator();
593 while (item.hasNext()) {
594 fileBuffer.append(item.next().toString());
595 }
596 }
597
598 libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch, null);
599 if (libClassList != null) {
600 libClassIncludeH = LibraryClassToAutogenH(libClassList);
601 item = libClassIncludeH.iterator();
602 while (item.hasNext()) {
603 fileBuffer.append(item.next().toString());
604 }
605 }
606 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
607
608 //
609 // If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to
610 // {DEST_DIR_DRBUG}/FlashMap.h
611 //
612 if (saq.isHaveTianoR8FlashMap()) {
613 fileBuffer.append(CommonDefinition.INCLUDE);
614 fileBuffer.append(" <");
615 fileBuffer.append(CommonDefinition.TIANOR8PLASHMAPH + ">\r\n");
616 copyFlashMapHToDebugDir();
617 }
618
619 //
620 // Write PCD information to library AutoGen.h.
621 //
622 if (this.myPcdAutogen != null) {
623 fileBuffer.append("\r\n");
624 fileBuffer.append(this.myPcdAutogen.getHAutoGenString());
625 }
626 //
627 // generate function prototype for constructor and destructor
628 //
629 LibConstructorToAutogenH(moduleType);
630 LibDestructorToAutogenH(moduleType);
631 ExternCallBackToAutoGenH(moduleType);
632 fileBuffer.append(functionDeclarations);
633 //
634 // Append the #endif at AutoGen.h
635 //
636 fileBuffer.append("#endif\r\n");
637 //
638 // Save content of string buffer to AutoGen.h file.
639 //
640 if (!saveFile(outputPath + File.separatorChar + "AutoGen.h", fileBuffer)) {
641 throw new AutoGenException("Failed to generate AutoGen.h !!!");
642 }
643 }
644
645 /**
646 libGenAutogenC
647
648 This function generates AutoGen.h for library.
649
650 @throws BuildException
651 Failed to generate AutoGen.c.
652 **/
653 void libGenAutogenC() throws EdkException {
654 StringBuffer fileBuffer = new StringBuffer(10240);
655
656 //
657 // Write Autogen.c header notation
658 //
659 fileBuffer.append(CommonDefinition.AUTOGENCNOTATION);
660
661 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
662 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
663
664 //
665 // Call pcd autogen.
666 //
667 this.myPcdAutogen = new PCDAutoGenAction(moduleId,
668 arch,
669 true,
670 saq.getModulePcdEntryNameArray(this.arch),
671 pcdDriverType,
672 parentId);
673 this.myPcdAutogen.execute();
674 if (this.myPcdAutogen != null) {
675 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
676 fileBuffer.append(this.myPcdAutogen.getCAutoGenString());
677 }
678 }
679
680 /**
681 LibraryClassToAutogenH
682
683 This function returns *.h files declared by library classes which are
684 consumed or produced by current build module or library.
685
686 @param libClassList
687 List of library class which consumed or produce by current
688 build module or library.
689 @return includeStrList List of *.h file.
690 **/
691 Set<String> LibraryClassToAutogenH(String[] libClassList)
692 throws EdkException {
693 Set<String> includeStrList = new LinkedHashSet<String>();
694 String includeName[];
695 String str = "";
696
697 //
698 // Get include file from GlobalData's SPDTable according to
699 // library class name.
700 //
701 for (int i = 0; i < libClassList.length; i++) {
702 includeName = GlobalData.getLibraryClassHeaderFiles(
703 saq.getDependencePkg(this.arch),
704 libClassList[i]);
705 if (includeName == null) {
706 throw new AutoGenException("Can not find library class ["
707 + libClassList[i] + "] declaration in any SPD package. ");
708 }
709 for (int j = 0; j < includeName.length; j++) {
710 String includeNameStr = includeName[j];
711 if (includeNameStr != null) {
712 str = CommonDefinition.INCLUDE + " " + "<";
713 str = str + includeNameStr + ">\r\n";
714 includeStrList.add(str);
715 includeNameStr = null;
716 }
717 }
718 }
719 return includeStrList;
720 }
721
722 /**
723 IncludesToAutogenH
724
725 This function add include file in AutoGen.h file.
726
727 @param packageNameList
728 List of module depended package.
729 @param moduleType
730 Module type.
731 @return
732 **/
733 List<String> depPkgToAutogenH(PackageIdentification[] packageNameList,
734 String moduleType) throws AutoGenException {
735
736 List<String> includeStrList = new LinkedList<String>();
737 String pkgHeader;
738 String includeStr = "";
739
740 //
741 // Get include file from moduleInfo file
742 //
743 for (int i = 0; i < packageNameList.length; i++) {
744 pkgHeader = GlobalData.getPackageHeaderFiles(packageNameList[i],
745 moduleType);
746 if (pkgHeader == null) {
747 throw new AutoGenException("Can not find package ["
748 + packageNameList[i]
749 + "] declaration in any SPD package. ");
750 } else if (!pkgHeader.equalsIgnoreCase("")) {
751 includeStr = CommonDefinition.INCLUDE + " <" + pkgHeader + ">\r\n";
752 includeStrList.add(includeStr);
753 }
754 }
755
756 return includeStrList;
757 }
758
759 /**
760 EntryPointToAutoGen
761
762 This function convert <ModuleEntryPoint> & <ModuleUnloadImage>
763 information in mas to AutoGen.c
764
765 @param entryPointList
766 List of entry point.
767 @param fileBuffer
768 String buffer fo AutoGen.c.
769 @throws Exception
770 **/
771 void EntryPointToAutoGen(String[] entryPointList, String[] unloadImageList, StringBuffer fileBuffer)
772 throws EdkException {
773
774 String typeStr = saq.getModuleType();
775 String debugStr = "DEBUG ((EFI_D_INFO | EFI_D_LOAD, \"Module Entry Point (%s) 0x%%p\\n\", (VOID *)(UINTN)%s));\r\n";
776 int unloadImageCount = 0;
777 int entryPointCount = 0;
778
779 //
780 // The parameters and return value of entryPoint is difference
781 // for difference module type.
782 //
783 switch (CommonDefinition.getModuleType(typeStr)) {
784
785 case CommonDefinition.ModuleTypePeiCore:
786 if (entryPointList == null ||entryPointList.length != 1 ) {
787 throw new AutoGenException("Module type = 'PEI_CORE', can have only one module entry point!");
788 } else {
789 functionDeclarations.append("EFI_STATUS\r\n");
790 functionDeclarations.append("EFIAPI\r\n");
791 functionDeclarations.append(entryPointList[0]);
792 functionDeclarations.append(" (\r\n");
793 functionDeclarations.append(" IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r\n");
794 functionDeclarations.append(" IN VOID *OldCoreData\r\n");
795 functionDeclarations.append(" );\r\n\r\n");
796
797 fileBuffer.append("EFI_STATUS\r\n");
798 fileBuffer.append("EFIAPI\r\n");
799 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
800 fileBuffer.append(" IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r\n");
801 fileBuffer.append(" IN VOID *OldCoreData\r\n");
802 fileBuffer.append(" )\r\n\r\n");
803 fileBuffer.append("{\r\n");
804 fileBuffer.append(" return ");
805 fileBuffer.append(entryPointList[0]);
806 fileBuffer.append(" (PeiStartupDescriptor, OldCoreData);\r\n");
807 fileBuffer.append("}\r\n\r\n");
808 }
809 break;
810
811 case CommonDefinition.ModuleTypeDxeCore:
812 fileBuffer.append("const UINT32 _gUefiDriverRevision = 0;\r\n");
813 if (entryPointList == null || entryPointList.length != 1) {
814 throw new AutoGenException("Module type = 'DXE_CORE', can have only one module entry point!");
815 } else {
816 functionDeclarations.append("VOID\r\n");
817 functionDeclarations.append("EFIAPI\r\n");
818 functionDeclarations.append(entryPointList[0]);
819 functionDeclarations.append(" (\r\n");
820 functionDeclarations.append(" IN VOID *HobStart\r\n");
821 functionDeclarations.append(" );\r\n\r\n");
822
823 fileBuffer.append("VOID\r\n");
824 fileBuffer.append("EFIAPI\r\n");
825 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
826 fileBuffer.append(" IN VOID *HobStart\r\n");
827 fileBuffer.append(" )\r\n\r\n");
828 fileBuffer.append("{\r\n");
829 fileBuffer.append(" ");
830 fileBuffer.append(entryPointList[0]);
831 fileBuffer.append(" (HobStart);\r\n");
832 fileBuffer.append("}\r\n\r\n");
833 }
834 break;
835
836 case CommonDefinition.ModuleTypePeim:
837 entryPointCount = 0;
838 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = 0;\r\n");
839 if (entryPointList == null || entryPointList.length == 0) {
840 fileBuffer.append("EFI_STATUS\r\n");
841 fileBuffer.append("EFIAPI\r\n");
842 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
843 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
844 fileBuffer.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
845 fileBuffer.append(" )\r\n\r\n");
846 fileBuffer.append("{\r\n");
847 fileBuffer.append(" return EFI_SUCCESS;\r\n");
848 fileBuffer.append("}\r\n\r\n");
849 break;
850 }
851 for (int i = 0; i < entryPointList.length; i++) {
852 functionDeclarations.append("EFI_STATUS\r\n");
853 functionDeclarations.append("EFIAPI\r\n");
854 functionDeclarations.append(entryPointList[i]);
855 functionDeclarations.append(" (\r\n");
856 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
857 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
858 functionDeclarations.append(" );\r\n");
859 entryPointCount++;
860 }
861
862 fileBuffer.append("EFI_STATUS\r\n");
863 fileBuffer.append("EFIAPI\r\n");
864 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
865 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
866 fileBuffer.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
867 fileBuffer.append(" )\r\n\r\n");
868 fileBuffer.append("{\r\n");
869 if (entryPointCount == 1) {
870 fileBuffer.append(String.format(" " + debugStr, entryPointList[0], entryPointList[0]));
871 fileBuffer.append(" return ");
872 fileBuffer.append(entryPointList[0]);
873 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
874 } else {
875 fileBuffer.append(" EFI_STATUS Status;\r\n");
876 fileBuffer.append(" EFI_STATUS CombinedStatus;\r\n\r\n");
877 fileBuffer.append(" CombinedStatus = EFI_LOAD_ERROR;\r\n\r\n");
878 for (int i = 0; i < entryPointList.length; i++) {
879 if (!entryPointList[i].equals("")) {
880 fileBuffer.append(String.format(" " + debugStr, entryPointList[i], entryPointList[i]));
881 fileBuffer.append(" Status = ");
882 fileBuffer.append(entryPointList[i]);
883 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
884 fileBuffer.append(" if (!EFI_ERROR (Status) || EFI_ERROR (CombinedStatus)) {\r\n");
885 fileBuffer.append(" CombinedStatus = Status;\r\n");
886 fileBuffer.append(" }\r\n\r\n");
887 } else {
888 break;
889 }
890 }
891 fileBuffer.append(" return CombinedStatus;\r\n");
892 }
893 fileBuffer.append("}\r\n\r\n");
894 break;
895
896 case CommonDefinition.ModuleTypeDxeSmmDriver:
897 entryPointCount = 0;
898 //
899 // If entryPoint is null, create an empty ProcessModuleEntryPointList
900 // function.
901 //
902 if (entryPointList == null || entryPointList.length == 0) {
903 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = ");
904 fileBuffer.append(Integer.toString(entryPointCount));
905 fileBuffer.append(";\r\n");
906 fileBuffer.append("EFI_STATUS\r\n");
907 fileBuffer.append("EFIAPI\r\n");
908 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
909 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
910 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
911 fileBuffer.append(" )\r\n\r\n");
912 fileBuffer.append("{\r\n");
913 fileBuffer.append(" return EFI_SUCCESS;\r\n");
914 fileBuffer.append("}\r\n\r\n");
915
916 } else {
917 for (int i = 0; i < entryPointList.length; i++) {
918 functionDeclarations.append("EFI_STATUS\r\n");
919 functionDeclarations.append("EFIAPI\r\n");
920 functionDeclarations.append(entryPointList[i]);
921 functionDeclarations.append(" (\r\n");
922 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
923 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
924 functionDeclarations.append(" );\r\n");
925 entryPointCount++;
926 }
927 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = ");
928 fileBuffer.append(Integer.toString(entryPointCount));
929 fileBuffer.append(";\r\n");
930 fileBuffer.append("static BASE_LIBRARY_JUMP_BUFFER mJumpContext;\r\n");
931 fileBuffer.append("static EFI_STATUS mDriverEntryPointStatus = EFI_LOAD_ERROR;\r\n\r\n");
932
933 fileBuffer.append("EFI_STATUS\r\n");
934 fileBuffer.append("EFIAPI\r\n");
935 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
936 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
937 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
938 fileBuffer.append(" )\r\n\r\n");
939 fileBuffer.append("{\r\n");
940
941 for (int i = 0; i < entryPointList.length; i++) {
942 fileBuffer.append(" if (SetJump (&mJumpContext) == 0) {\r\n");
943 fileBuffer.append(String.format(" " + debugStr, entryPointList[i], entryPointList[i]));
944 fileBuffer.append(" ExitDriver (");
945 fileBuffer.append(entryPointList[i]);
946 fileBuffer.append(" (ImageHandle, SystemTable));\r\n");
947 fileBuffer.append(" ASSERT (FALSE);\r\n");
948 fileBuffer.append(" }\r\n");
949 }
950 fileBuffer.append(" return mDriverEntryPointStatus;\r\n");
951 fileBuffer.append("}\r\n\r\n");
952
953 fileBuffer.append("VOID\r\n");
954 fileBuffer.append("EFIAPI\r\n");
955 fileBuffer.append("ExitDriver (\r\n");
956 fileBuffer.append(" IN EFI_STATUS Status\r\n");
957 fileBuffer.append(" )\r\n\r\n");
958 fileBuffer.append("{\r\n");
959 fileBuffer.append(" if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");
960 fileBuffer.append(" mDriverEntryPointStatus = Status;\r\n");
961 fileBuffer.append(" }\r\n");
962 fileBuffer.append(" LongJump (&mJumpContext, (UINTN)-1);\r\n");
963 fileBuffer.append(" ASSERT (FALSE);\r\n");
964 fileBuffer.append("}\r\n\r\n");
965 }
966
967
968 //
969 // Add "ModuleUnloadImage" for DxeSmmDriver module type;
970 //
971
972 unloadImageCount = 0;
973 if (unloadImageList != null) {
974 for (int i = 0; i < unloadImageList.length; i++) {
975 functionDeclarations.append("EFI_STATUS\r\n");
976 functionDeclarations.append(unloadImageList[i]);
977 functionDeclarations.append(" (\r\n");
978 functionDeclarations.append(" IN EFI_HANDLE ImageHandle\r\n");
979 functionDeclarations.append(" );\r\n");
980 unloadImageCount++;
981 }
982 }
983
984 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ");
985 fileBuffer.append(Integer.toString(unloadImageCount));
986 fileBuffer.append(";\r\n\r\n");
987
988 fileBuffer.append("EFI_STATUS\r\n");
989 fileBuffer.append("EFIAPI\r\n");
990 fileBuffer.append("ProcessModuleUnloadList (\r\n");
991 fileBuffer.append(" IN EFI_HANDLE ImageHandle\r\n");
992 fileBuffer.append(" )\r\n");
993 fileBuffer.append("{\r\n");
994
995 if (unloadImageCount == 0) {
996 fileBuffer.append(" return EFI_SUCCESS;\r\n");
997 } else if (unloadImageCount == 1) {
998 fileBuffer.append(" return ");
999 fileBuffer.append(unloadImageList[0]);
1000 fileBuffer.append("(ImageHandle);\r\n");
1001 } else {
1002 fileBuffer.append(" EFI_STATUS Status;\r\n\r\n");
1003 fileBuffer.append(" Status = EFI_SUCCESS;\r\n\r\n");
1004 for (int i = 0; i < unloadImageList.length; i++) {
1005 if (i == 0) {
1006 fileBuffer.append(" Status = ");
1007 fileBuffer.append(unloadImageList[i]);
1008 fileBuffer.append("(ImageHandle);\r\n");
1009 } else {
1010 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1011 fileBuffer.append(" ");
1012 fileBuffer.append(unloadImageList[i]);
1013 fileBuffer.append("(ImageHandle);\r\n");
1014 fileBuffer.append(" } else {\r\n");
1015 fileBuffer.append(" Status = ");
1016 fileBuffer.append(unloadImageList[i]);
1017 fileBuffer.append("(ImageHandle);\r\n");
1018 fileBuffer.append(" }\r\n");
1019 }
1020 }
1021 fileBuffer.append(" return Status;\r\n");
1022 }
1023 fileBuffer.append("}\r\n\r\n");
1024 break;
1025
1026 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1027 case CommonDefinition.ModuleTypeDxeDriver:
1028 case CommonDefinition.ModuleTypeDxeSalDriver:
1029 case CommonDefinition.ModuleTypeUefiDriver:
1030 case CommonDefinition.ModuleTypeUefiApplication:
1031 entryPointCount = 0;
1032 fileBuffer.append("const UINT32 _gUefiDriverRevision = 0;\r\n");
1033 //
1034 // If entry point is null, create a empty ProcessModuleEntryPointList function.
1035 //
1036 if (entryPointList == null || entryPointList.length == 0) {
1037 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = 0;\r\n");
1038 fileBuffer.append("EFI_STATUS\r\n");
1039 fileBuffer.append("EFIAPI\r\n");
1040 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
1041 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1042 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1043 fileBuffer.append(" )\r\n\r\n");
1044 fileBuffer.append("{\r\n");
1045 fileBuffer.append(" return EFI_SUCCESS;\r\n");
1046 fileBuffer.append("}\r\n");
1047
1048 } else {
1049 for (int i = 0; i < entryPointList.length; i++) {
1050 functionDeclarations.append("EFI_STATUS\r\n");
1051 functionDeclarations.append("EFIAPI\r\n");
1052 functionDeclarations.append(entryPointList[i]);
1053 functionDeclarations.append(" (\r\n");
1054 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1055 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1056 functionDeclarations.append(" );\r\n");
1057 entryPointCount++;
1058 }
1059
1060 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = ");
1061 fileBuffer.append(Integer.toString(entryPointCount));
1062 fileBuffer.append(";\r\n");
1063 if (entryPointCount > 1) {
1064 fileBuffer.append("static BASE_LIBRARY_JUMP_BUFFER mJumpContext;\r\n");
1065 fileBuffer.append("static EFI_STATUS mDriverEntryPointStatus = EFI_LOAD_ERROR;\r\n");
1066 }
1067 fileBuffer.append("\r\n");
1068
1069 fileBuffer.append("EFI_STATUS\r\n");
1070 fileBuffer.append("EFIAPI\r\n");
1071 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
1072 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1073 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1074 fileBuffer.append(" )\r\n\r\n");
1075 fileBuffer.append("{\r\n");
1076
1077 if (entryPointCount == 1) {
1078 fileBuffer.append(String.format(" " + debugStr, entryPointList[0], entryPointList[0]));
1079 fileBuffer.append(" return ");
1080 fileBuffer.append(entryPointList[0]);
1081 fileBuffer.append(" (ImageHandle, SystemTable);\r\n");
1082 } else {
1083 for (int i = 0; i < entryPointList.length; i++) {
1084 if (!entryPointList[i].equals("")) {
1085 fileBuffer.append(" if (SetJump (&mJumpContext) == 0) {\r\n");
1086 fileBuffer.append(String.format(" " + debugStr, entryPointList[i], entryPointList[i]));
1087 fileBuffer.append(" ExitDriver (");
1088 fileBuffer.append(entryPointList[i]);
1089 fileBuffer.append(" (ImageHandle, SystemTable));\r\n");
1090 fileBuffer.append(" ASSERT (FALSE);\r\n");
1091 fileBuffer.append(" }\r\n");
1092 } else {
1093 break;
1094 }
1095 }
1096 fileBuffer.append(" return mDriverEntryPointStatus;\r\n");
1097 }
1098 fileBuffer.append("}\r\n\r\n");
1099
1100 fileBuffer.append("VOID\r\n");
1101 fileBuffer.append("EFIAPI\r\n");
1102 fileBuffer.append("ExitDriver (\r\n");
1103 fileBuffer.append(" IN EFI_STATUS Status\r\n");
1104 fileBuffer.append(" )\r\n\r\n");
1105 fileBuffer.append("{\r\n");
1106 if (entryPointCount <= 1) {
1107 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1108 fileBuffer.append(" ProcessLibraryDestructorList (gImageHandle, gST);\r\n");
1109 fileBuffer.append(" }\r\n");
1110 fileBuffer
1111 .append(" gBS->Exit (gImageHandle, Status, 0, NULL);\r\n");
1112 } else {
1113 fileBuffer.append(" if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");
1114 fileBuffer.append(" mDriverEntryPointStatus = Status;\r\n");
1115 fileBuffer.append(" }\r\n");
1116 fileBuffer.append(" LongJump (&mJumpContext, (UINTN)-1);\r\n");
1117 fileBuffer.append(" ASSERT (FALSE);\r\n");
1118 }
1119 fileBuffer.append("}\r\n\r\n");
1120 }
1121
1122 //
1123 // Add ModuleUnloadImage for DxeDriver and UefiDriver module type.
1124 //
1125 //entryPointList = SurfaceAreaQuery.getModuleUnloadImageArray();
1126 //
1127 // Remover duplicate unload entry point.
1128 //
1129 //entryPointList = CommonDefinition.remDupString(entryPointList);
1130 //entryPointCount = 0;
1131 unloadImageCount = 0;
1132 if (unloadImageList != null) {
1133 for (int i = 0; i < unloadImageList.length; i++) {
1134 functionDeclarations.append("EFI_STATUS\r\n");
1135 functionDeclarations.append("EFIAPI\r\n");
1136 functionDeclarations.append(unloadImageList[i]);
1137 functionDeclarations.append(" (\r\n");
1138 functionDeclarations.append(" IN EFI_HANDLE ImageHandle\r\n");
1139 functionDeclarations.append(" );\r\n");
1140 unloadImageCount++;
1141 }
1142 }
1143
1144 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ");
1145 fileBuffer.append(Integer.toString(unloadImageCount));
1146 fileBuffer.append(";\r\n\r\n");
1147
1148 fileBuffer.append("EFI_STATUS\r\n");
1149 fileBuffer.append("EFIAPI\r\n");
1150 fileBuffer.append("ProcessModuleUnloadList (\r\n");
1151 fileBuffer.append(" IN EFI_HANDLE ImageHandle\r\n");
1152 fileBuffer.append(" )\r\n");
1153 fileBuffer.append("{\r\n");
1154
1155 if (unloadImageCount == 0) {
1156 fileBuffer.append(" return EFI_SUCCESS;\r\n");
1157 } else if (unloadImageCount == 1) {
1158 fileBuffer.append(" return ");
1159 fileBuffer.append(unloadImageList[0]);
1160 fileBuffer.append("(ImageHandle);\r\n");
1161 } else {
1162 fileBuffer.append(" EFI_STATUS Status;\r\n\r\n");
1163 fileBuffer.append(" Status = EFI_SUCCESS;\r\n\r\n");
1164 for (int i = 0; i < unloadImageList.length; i++) {
1165 if (i == 0) {
1166 fileBuffer.append(" Status = ");
1167 fileBuffer.append(unloadImageList[i]);
1168 fileBuffer.append("(ImageHandle);\r\n");
1169 } else {
1170 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1171 fileBuffer.append(" ");
1172 fileBuffer.append(unloadImageList[i]);
1173 fileBuffer.append("(ImageHandle);\r\n");
1174 fileBuffer.append(" } else {\r\n");
1175 fileBuffer.append(" Status = ");
1176 fileBuffer.append(unloadImageList[i]);
1177 fileBuffer.append("(ImageHandle);\r\n");
1178 fileBuffer.append(" }\r\n");
1179 }
1180 }
1181 fileBuffer.append(" return Status;\r\n");
1182 }
1183 fileBuffer.append("}\r\n\r\n");
1184 break;
1185 }
1186 }
1187
1188 /**
1189 PpiGuidToAutogenc
1190
1191 This function gets GUIDs from SPD file accrodeing to <PPIs> information
1192 and write those GUIDs to AutoGen.c.
1193
1194 @param fileBuffer
1195 String Buffer for Autogen.c file.
1196 @throws BuildException
1197 Guid must set value!
1198 **/
1199 void PpiGuidToAutogenC(StringBuffer fileBuffer) throws AutoGenException {
1200 String[] cNameGuid = null;
1201
1202 //
1203 // Get the all PPI adn PPI Notify from MSA file,
1204 // then add those PPI ,and PPI Notify name to list.
1205 //
1206
1207 String[] ppiList = saq.getPpiArray(this.arch);
1208 for (int i = 0; i < ppiList.length; i++) {
1209 this.mPpiList.add(ppiList[i]);
1210 }
1211
1212 String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);
1213 for (int i = 0; i < ppiNotifyList.length; i++) {
1214 this.mPpiList.add(ppiNotifyList[i]);
1215 }
1216
1217 //
1218 // Find CNAME and GUID from dependence SPD file and write to Autogen.c
1219 //
1220 Iterator ppiIterator = this.mPpiList.iterator();
1221 String ppiKeyWord = null;
1222 while (ppiIterator.hasNext()) {
1223 ppiKeyWord = ppiIterator.next().toString();
1224 cNameGuid = GlobalData.getPpiGuid(this.mDepPkgList, ppiKeyWord);
1225 if (cNameGuid != null) {
1226 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");
1227 fileBuffer.append(cNameGuid[0]);
1228 fileBuffer.append(" = { ");
1229 fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));
1230 fileBuffer.append(" } ;");
1231 } else {
1232 //
1233 // If can't find Ppi GUID declaration in every package
1234 //
1235 throw new AutoGenException("Can not find Ppi GUID ["
1236 + ppiKeyWord + "] declaration in any SPD package!");
1237 }
1238 }
1239 }
1240
1241 /**
1242 ProtocolGuidToAutogenc
1243
1244 This function gets GUIDs from SPD file accrodeing to <Protocol>
1245 information and write those GUIDs to AutoGen.c.
1246
1247 @param fileBuffer
1248 String Buffer for Autogen.c file.
1249 @throws BuildException
1250 Protocol name must set.
1251 **/
1252 void ProtocolGuidToAutogenC(StringBuffer fileBuffer) throws EdkException {
1253 String[] cNameGuid = null;
1254
1255 String[] protocolList = saq.getProtocolArray(this.arch);
1256
1257 //
1258 // Add result to Autogen global list.
1259 //
1260 for (int i = 0; i < protocolList.length; i++) {
1261 this.mProtocolList.add(protocolList[i]);
1262 }
1263
1264 String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);
1265
1266 for (int i = 0; i < protocolNotifyList.length; i++) {
1267 this.mProtocolList.add(protocolNotifyList[i]);
1268 }
1269
1270 //
1271 // Get the NAME and GUID from dependence SPD and write to Autogen.c
1272 //
1273 Iterator protocolIterator = this.mProtocolList.iterator();
1274 String protocolKeyWord = null;
1275
1276
1277 while (protocolIterator.hasNext()) {
1278 protocolKeyWord = protocolIterator.next().toString();
1279 cNameGuid = GlobalData.getProtocolGuid(this.mDepPkgList, protocolKeyWord);
1280 if (cNameGuid != null) {
1281 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");
1282 fileBuffer.append(cNameGuid[0]);
1283 fileBuffer.append(" = { ");
1284 fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));
1285 fileBuffer.append(" } ;");
1286 } else {
1287 //
1288 // If can't find protocol GUID declaration in every package
1289 //
1290 throw new AutoGenException("Can not find protocol Guid ["
1291 + protocolKeyWord + "] declaration in any SPD package!");
1292 }
1293 }
1294 }
1295
1296 /**
1297 GuidGuidToAutogenc
1298
1299 This function gets GUIDs from SPD file accrodeing to <Guids> information
1300 and write those GUIDs to AutoGen.c.
1301
1302 @param fileBuffer
1303 String Buffer for Autogen.c file.
1304
1305 **/
1306 void GuidGuidToAutogenC(StringBuffer fileBuffer) throws AutoGenException {
1307 String[] cNameGuid = null;
1308 String guidKeyWord = null;
1309
1310 String[] guidList = saq.getGuidEntryArray(this.arch);
1311
1312 for (int i = 0; i < guidList.length; i++) {
1313 this.mGuidList.add(guidList[i]);
1314 }
1315
1316
1317 Iterator guidIterator = this.mGuidList.iterator();
1318 while (guidIterator.hasNext()) {
1319 guidKeyWord = guidIterator.next().toString();
1320 cNameGuid = GlobalData.getGuid(this.mDepPkgList, guidKeyWord);
1321
1322 if (cNameGuid != null) {
1323 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");
1324 fileBuffer.append(cNameGuid[0]);
1325 fileBuffer.append(" = { ");
1326 fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));
1327 fileBuffer.append("} ;");
1328 } else {
1329 //
1330 // If can't find GUID declaration in every package
1331 //
1332 throw new AutoGenException("Can not find Guid [" + guidKeyWord
1333 + "] declaration in any SPD package. ");
1334 }
1335
1336 }
1337 }
1338
1339 /**
1340 LibInstanceToAutogenC
1341
1342 This function adds dependent library instance to autogen.c,which
1343 includeing library's constructor, destructor, and library dependent ppi,
1344 protocol, guid, pcd information.
1345
1346 @param fileBuffer
1347 String buffer for AutoGen.c
1348 @throws BuildException
1349 **/
1350 void LibInstanceToAutogenC(StringBuffer fileBuffer) throws EdkException {
1351 String moduleType = this.moduleId.getModuleType();
1352 //
1353 // Add library constructor to AutoGen.c
1354 //
1355 LibConstructorToAutogenC(libConstructList, moduleType, fileBuffer);
1356 //
1357 // Add library destructor to AutoGen.c
1358 //
1359 LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer);
1360 }
1361
1362 /**
1363 LibConstructorToAutogenH
1364
1365 This function writes library constructor declarations AutoGen.h. The library
1366 constructor's parameter and return value depend on module type.
1367
1368 @param libInstanceList
1369 List of library construct name.
1370 @param moduleType
1371 Module type.
1372 @param fileBuffer
1373 String buffer for AutoGen.c
1374 @throws Exception
1375 **/
1376 void LibConstructorToAutogenH(String moduleType) throws EdkException {
1377 boolean isFirst = true;
1378
1379 //
1380 // If not yet parse this library instance's constructor
1381 // element,parse it.
1382 //
1383 String libConstructName = saq.getLibConstructorName();
1384 if (libConstructName == null) {
1385 return;
1386 }
1387
1388 //
1389 // The library constructor's parameter and return value depend on
1390 // module type.
1391 //
1392 if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1393 functionDeclarations.append("RETURN_STATUS\r\n");
1394 functionDeclarations.append("EFIAPI\r\n");
1395 functionDeclarations.append(libConstructName);
1396 functionDeclarations.append(" (\r\n");
1397 functionDeclarations.append(" VOID\r\n");
1398 functionDeclarations.append(" );\r\n");
1399 } else {
1400 switch (CommonDefinition.getModuleType(moduleType)) {
1401 case CommonDefinition.ModuleTypeBase:
1402 functionDeclarations.append("RETURN_STATUS\r\n");
1403 functionDeclarations.append("EFIAPI\r\n");
1404 functionDeclarations.append(libConstructName);
1405 functionDeclarations.append(" (\r\n");
1406 functionDeclarations.append(" VOID\r\n");
1407 functionDeclarations.append(" );\r\n");
1408 break;
1409
1410 case CommonDefinition.ModuleTypePeiCore:
1411 case CommonDefinition.ModuleTypePeim:
1412 functionDeclarations.append("EFI_STATUS\r\n");
1413 functionDeclarations.append("EFIAPI\r\n");
1414 functionDeclarations.append(libConstructName);
1415 functionDeclarations.append(" (\r\n");
1416 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1417 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1418 functionDeclarations.append(" );\r\n");
1419 break;
1420
1421 case CommonDefinition.ModuleTypeDxeCore:
1422 case CommonDefinition.ModuleTypeDxeDriver:
1423 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1424 case CommonDefinition.ModuleTypeDxeSmmDriver:
1425 case CommonDefinition.ModuleTypeDxeSalDriver:
1426 case CommonDefinition.ModuleTypeUefiDriver:
1427 case CommonDefinition.ModuleTypeUefiApplication:
1428 functionDeclarations.append("EFI_STATUS\r\n");
1429 functionDeclarations.append("EFIAPI\r\n");
1430 functionDeclarations.append(libConstructName);
1431 functionDeclarations.append(" (\r\n");
1432 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1433 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1434 functionDeclarations.append(" );\r\n");
1435 break;
1436
1437 }
1438 }
1439 }
1440
1441 /**
1442 LibDestructorToAutogenH
1443
1444 This function writes library destructor declarations AutoGen.h. The library
1445 destructor's parameter and return value depend on module type.
1446
1447 @param libInstanceList
1448 List of library destructor name.
1449 @param moduleType
1450 Module type.
1451 @param fileBuffer
1452 String buffer for AutoGen.c
1453 @throws Exception
1454 **/
1455 void LibDestructorToAutogenH(String moduleType) throws EdkException {
1456 boolean isFirst = true;
1457 String libDestructName = saq.getLibDestructorName();
1458 if (libDestructName == null) {
1459 return;
1460 }
1461
1462 if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1463 functionDeclarations.append("RETURN_STATUS\r\n");
1464 functionDeclarations.append("EFIAPI\r\n");
1465 functionDeclarations.append(libDestructName);
1466 functionDeclarations.append(" (\r\n");
1467 functionDeclarations.append(" VOID\r\n");
1468 functionDeclarations.append(" );\r\n");
1469 } else {
1470 switch (CommonDefinition.getModuleType(moduleType)) {
1471 case CommonDefinition.ModuleTypeBase:
1472 functionDeclarations.append("RETURN_STATUS\r\n");
1473 functionDeclarations.append("EFIAPI\r\n");
1474 functionDeclarations.append(libDestructName);
1475 functionDeclarations.append(" (\r\n");
1476 functionDeclarations.append(" VOID\r\n");
1477 functionDeclarations.append(" );\r\n");
1478 break;
1479 case CommonDefinition.ModuleTypePeiCore:
1480 case CommonDefinition.ModuleTypePeim:
1481 functionDeclarations.append("EFI_STATUS\r\n");
1482 functionDeclarations.append("EFIAPI\r\n");
1483 functionDeclarations.append(libDestructName);
1484 functionDeclarations.append(" (\r\n");
1485 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1486 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1487 functionDeclarations.append(" );\r\n");
1488 break;
1489 case CommonDefinition.ModuleTypeDxeCore:
1490 case CommonDefinition.ModuleTypeDxeDriver:
1491 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1492 case CommonDefinition.ModuleTypeDxeSmmDriver:
1493 case CommonDefinition.ModuleTypeDxeSalDriver:
1494 case CommonDefinition.ModuleTypeUefiDriver:
1495 case CommonDefinition.ModuleTypeUefiApplication:
1496 functionDeclarations.append("EFI_STATUS\r\n");
1497 functionDeclarations.append("EFIAPI\r\n");
1498 functionDeclarations.append(libDestructName);
1499 functionDeclarations.append(" (\r\n");
1500 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1501 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1502 functionDeclarations.append(" );\r\n");
1503 break;
1504 }
1505 }
1506 }
1507
1508 /**
1509 LibConstructorToAutogenc
1510
1511 This function writes library constructor list to AutoGen.c. The library
1512 constructor's parameter and return value depend on module type.
1513
1514 @param libInstanceList
1515 List of library construct name.
1516 @param moduleType
1517 Module type.
1518 @param fileBuffer
1519 String buffer for AutoGen.c
1520 @throws Exception
1521 **/
1522 void LibConstructorToAutogenC(List<String[]> libInstanceList,
1523 String moduleType, StringBuffer fileBuffer) throws EdkException {
1524 boolean isFirst = true;
1525
1526 //
1527 // The library constructor's parameter and return value depend on
1528 // module type.
1529 //
1530 for (int i = 0; i < libInstanceList.size(); i++) {
1531 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1532 functionDeclarations.append("RETURN_STATUS\r\n");
1533 functionDeclarations.append("EFIAPI\r\n");
1534 functionDeclarations.append(libInstanceList.get(i)[0]);
1535 functionDeclarations.append(" (\r\n");
1536 functionDeclarations.append(" VOID\r\n");
1537 functionDeclarations.append(" );\r\n");
1538 } else {
1539 switch (CommonDefinition.getModuleType(moduleType)) {
1540 case CommonDefinition.ModuleTypeBase:
1541 functionDeclarations.append("RETURN_STATUS\r\n");
1542 functionDeclarations.append("EFIAPI\r\n");
1543 functionDeclarations.append(libInstanceList.get(i)[0]);
1544 functionDeclarations.append(" (\r\n");
1545 functionDeclarations.append(" VOID\r\n");
1546 functionDeclarations.append(" );\r\n");
1547 break;
1548
1549 case CommonDefinition.ModuleTypePeiCore:
1550 case CommonDefinition.ModuleTypePeim:
1551 functionDeclarations.append("EFI_STATUS\r\n");
1552 functionDeclarations.append("EFIAPI\r\n");
1553 functionDeclarations.append(libInstanceList.get(i)[0]);
1554 functionDeclarations.append(" (\r\n");
1555 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1556 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1557 functionDeclarations.append(" );\r\n");
1558 break;
1559
1560 case CommonDefinition.ModuleTypeDxeCore:
1561 case CommonDefinition.ModuleTypeDxeDriver:
1562 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1563 case CommonDefinition.ModuleTypeDxeSmmDriver:
1564 case CommonDefinition.ModuleTypeDxeSalDriver:
1565 case CommonDefinition.ModuleTypeUefiDriver:
1566 case CommonDefinition.ModuleTypeUefiApplication:
1567 functionDeclarations.append("EFI_STATUS\r\n");
1568 functionDeclarations.append("EFIAPI\r\n");
1569 functionDeclarations.append(libInstanceList.get(i)[0]);
1570 functionDeclarations.append(" (\r\n");
1571 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1572 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1573 functionDeclarations.append(" );\r\n");
1574 break;
1575
1576 }
1577 }
1578 }
1579
1580 //
1581 // Add ProcessLibraryConstructorList in AutoGen.c
1582 //
1583 fileBuffer.append("VOID\r\n");
1584 fileBuffer.append("EFIAPI\r\n");
1585 fileBuffer.append("ProcessLibraryConstructorList (\r\n");
1586 switch (CommonDefinition.getModuleType(moduleType)) {
1587 case CommonDefinition.ModuleTypeBase:
1588 fileBuffer.append(" VOID\r\n");
1589 break;
1590
1591 case CommonDefinition.ModuleTypePeiCore:
1592 case CommonDefinition.ModuleTypePeim:
1593 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1594 fileBuffer
1595 .append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1596 break;
1597
1598 case CommonDefinition.ModuleTypeDxeCore:
1599 case CommonDefinition.ModuleTypeDxeDriver:
1600 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1601 case CommonDefinition.ModuleTypeDxeSmmDriver:
1602 case CommonDefinition.ModuleTypeDxeSalDriver:
1603 case CommonDefinition.ModuleTypeUefiDriver:
1604 case CommonDefinition.ModuleTypeUefiApplication:
1605 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1606 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1607 break;
1608 }
1609
1610 fileBuffer.append(" )\r\n");
1611 fileBuffer.append("{\r\n");
1612 //
1613 // If no constructor function, return EFI_SUCCESS.
1614 //
1615 for (int i = 0; i < libInstanceList.size(); i++) {
1616 if (isFirst) {
1617 fileBuffer.append(" EFI_STATUS Status;\r\n");
1618 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1619 fileBuffer.append("\r\n");
1620 isFirst = false;
1621 }
1622 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1623 fileBuffer.append(" Status = ");
1624 fileBuffer.append(libInstanceList.get(i)[0]);
1625 fileBuffer.append("();\r\n");
1626 } else {
1627 switch (CommonDefinition.getModuleType(moduleType)) {
1628 case CommonDefinition.ModuleTypeBase:
1629 fileBuffer.append(" Status = ");
1630 fileBuffer.append(libInstanceList.get(i)[0]);
1631 fileBuffer.append("();\r\n");
1632 break;
1633 case CommonDefinition.ModuleTypePeiCore:
1634 case CommonDefinition.ModuleTypePeim:
1635 fileBuffer.append(" Status = ");
1636 fileBuffer.append(libInstanceList.get(i)[0]);
1637 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
1638 break;
1639 case CommonDefinition.ModuleTypeDxeCore:
1640 case CommonDefinition.ModuleTypeDxeDriver:
1641 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1642 case CommonDefinition.ModuleTypeDxeSmmDriver:
1643 case CommonDefinition.ModuleTypeDxeSalDriver:
1644 case CommonDefinition.ModuleTypeUefiDriver:
1645 case CommonDefinition.ModuleTypeUefiApplication:
1646 fileBuffer.append(" Status = ");
1647 fileBuffer.append(libInstanceList.get(i)[0]);
1648 fileBuffer.append(" (ImageHandle, SystemTable);\r\n");
1649 break;
1650 default:
1651 EdkLog.log(EdkLog.EDK_INFO,"Autogen doesn't know how to deal with module type - " + moduleType + "!");
1652 }
1653
1654 }
1655 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1656 }
1657 fileBuffer.append("}\r\n");
1658 }
1659
1660 /**
1661 LibDestructorToAutogenc
1662
1663 This function writes library destructor list to AutoGen.c. The library
1664 destructor's parameter and return value depend on module type.
1665
1666 @param libInstanceList
1667 List of library destructor name.
1668 @param moduleType
1669 Module type.
1670 @param fileBuffer
1671 String buffer for AutoGen.c
1672 @throws Exception
1673 **/
1674 void LibDestructorToAutogenC(List<String[]> libInstanceList,
1675 String moduleType, StringBuffer fileBuffer) throws EdkException {
1676 boolean isFirst = true;
1677 for (int i = 0; i < libInstanceList.size(); i++) {
1678 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1679 functionDeclarations.append("RETURN_STATUS\r\n");
1680 functionDeclarations.append("EFIAPI\r\n");
1681 functionDeclarations.append(libInstanceList.get(i)[0]);
1682 functionDeclarations.append(" (\r\n");
1683 functionDeclarations.append(" VOID\r\n");
1684 functionDeclarations.append(" );\r\n");
1685 } else {
1686 switch (CommonDefinition.getModuleType(moduleType)) {
1687 case CommonDefinition.ModuleTypeBase:
1688 functionDeclarations.append("RETURN_STATUS\r\n");
1689 functionDeclarations.append("EFIAPI\r\n");
1690 functionDeclarations.append(libInstanceList.get(i)[0]);
1691 functionDeclarations.append(" (\r\n");
1692 functionDeclarations.append(" VOID\r\n");
1693 functionDeclarations.append(" );\r\n");
1694 break;
1695 case CommonDefinition.ModuleTypePeiCore:
1696 case CommonDefinition.ModuleTypePeim:
1697 functionDeclarations.append("EFI_STATUS\r\n");
1698 functionDeclarations.append("EFIAPI\r\n");
1699 functionDeclarations.append(libInstanceList.get(i)[0]);
1700 functionDeclarations.append(" (\r\n");
1701 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1702 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1703 functionDeclarations.append(" );\r\n");
1704 break;
1705 case CommonDefinition.ModuleTypeDxeCore:
1706 case CommonDefinition.ModuleTypeDxeDriver:
1707 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1708 case CommonDefinition.ModuleTypeDxeSmmDriver:
1709 case CommonDefinition.ModuleTypeDxeSalDriver:
1710 case CommonDefinition.ModuleTypeUefiDriver:
1711 case CommonDefinition.ModuleTypeUefiApplication:
1712 functionDeclarations.append("EFI_STATUS\r\n");
1713 functionDeclarations.append("EFIAPI\r\n");
1714 functionDeclarations.append(libInstanceList.get(i)[0]);
1715 functionDeclarations.append(" (\r\n");
1716 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1717 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1718 functionDeclarations.append(" );\r\n");
1719 break;
1720 }
1721 }
1722 }
1723
1724 //
1725 // Write ProcessLibraryDestructor list to autogen.c
1726 //
1727 switch (CommonDefinition.getModuleType(moduleType)) {
1728 case CommonDefinition.ModuleTypeBase:
1729 case CommonDefinition.ModuleTypePeiCore:
1730 case CommonDefinition.ModuleTypePeim:
1731 break;
1732 case CommonDefinition.ModuleTypeDxeCore:
1733 case CommonDefinition.ModuleTypeDxeDriver:
1734 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1735 case CommonDefinition.ModuleTypeDxeSmmDriver:
1736 case CommonDefinition.ModuleTypeDxeSalDriver:
1737 case CommonDefinition.ModuleTypeUefiDriver:
1738 case CommonDefinition.ModuleTypeUefiApplication:
1739 fileBuffer.append("VOID\r\n");
1740 fileBuffer.append("EFIAPI\r\n");
1741 fileBuffer.append("ProcessLibraryDestructorList (\r\n");
1742 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1743 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1744 fileBuffer.append(" )\r\n");
1745 fileBuffer.append("{\r\n");
1746 //
1747 // If no library destructor function, return EFI_SUCCESS.
1748 //
1749
1750 for (int i = 0; i < libInstanceList.size(); i++) {
1751 if (isFirst) {
1752 fileBuffer.append(" EFI_STATUS Status;\r\n");
1753 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1754 fileBuffer.append("\r\n");
1755 isFirst = false;
1756 }
1757 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1758 fileBuffer.append(" Status = ");
1759 fileBuffer.append(libInstanceList.get(i)[0]);
1760 fileBuffer.append("();\r\n");
1761 fileBuffer.append(" VOID\r\n");
1762 } else {
1763 fileBuffer.append(" Status = ");
1764 fileBuffer.append(libInstanceList.get(i)[0]);
1765 fileBuffer.append("(ImageHandle, SystemTable);\r\n");
1766 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1767 }
1768 }
1769 fileBuffer.append("}\r\n");
1770 break;
1771 }
1772 }
1773
1774 /**
1775 ExternsDriverBindingToAutoGenC
1776
1777 This function is to write DRIVER_BINDING, COMPONENT_NAME,
1778 DRIVER_CONFIGURATION, DRIVER_DIAGNOSTIC in AutoGen.c.
1779
1780 @param fileBuffer
1781 String buffer for AutoGen.c
1782 **/
1783 void ExternsDriverBindingToAutoGenC(StringBuffer fileBuffer)
1784 throws EdkException {
1785 //
1786 // Get the arry of extern. The driverBindingGroup is a 2 dimension array.
1787 // The second dimension is include following element: DriverBinding,
1788 // ComponentName, DriverConfiguration, DriverDiag;
1789 //
1790 String[][] driverBindingGroup = this.saq.getExternProtocolGroup();
1791
1792
1793 //
1794 // inital BitMask;
1795 //
1796 int BitMask = 0;
1797
1798 //
1799 // Write driver binding protocol extern to autogen.c
1800 //
1801 for (int i = 0; i < driverBindingGroup.length; i++) {
1802 if (driverBindingGroup[i][0] != null) {
1803 globalDeclarations.append("extern EFI_DRIVER_BINDING_PROTOCOL ");
1804 globalDeclarations.append(driverBindingGroup[i][0]);
1805 globalDeclarations.append(";\r\n");
1806 }
1807 }
1808
1809 //
1810 // Write component name protocol extern to autogen.c
1811 //
1812 if (!componentNamePcd) {
1813 for (int i = 0; i < driverBindingGroup.length; i++) {
1814 if (driverBindingGroup[i][1]!= null) {
1815 if (driverBindingGroup[i][0] != null) {
1816 BitMask |= 0x01;
1817 globalDeclarations.append("extern EFI_COMPONENT_NAME_PROTOCOL ");
1818 globalDeclarations.append(driverBindingGroup[i][1]);
1819 globalDeclarations.append(";\r\n");
1820 } else {
1821 throw new AutoGenException("DriverBinding can't be empty!!");
1822 }
1823 }
1824 }
1825 }
1826
1827 //
1828 // Write driver configration protocol extern to autogen.c
1829 //
1830 for (int i = 0; i < driverBindingGroup.length; i++) {
1831 if (driverBindingGroup[i][2] != null) {
1832 if (driverBindingGroup[i][0] != null) {
1833 BitMask |= 0x02;
1834 globalDeclarations.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");
1835 globalDeclarations.append(driverBindingGroup[i][2]);
1836 globalDeclarations.append(";\r\n");
1837 } else {
1838 throw new AutoGenException("DriverBinding can't be empty!!");
1839 }
1840 }
1841 }
1842
1843 //
1844 // Write driver dignastic protocol extern to autogen.c
1845 //
1846 if (!driverDiagnostPcd) {
1847 for (int i = 0; i < driverBindingGroup.length; i++) {
1848 if (driverBindingGroup[i][3] != null) {
1849 if (driverBindingGroup[i][0] != null) {
1850 BitMask |= 0x04;
1851 globalDeclarations.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");
1852 globalDeclarations.append(driverBindingGroup[i][3]);
1853 globalDeclarations.append(";\r\n");
1854 } else {
1855 throw new AutoGenException("DriverBinding can't be empty!!");
1856 }
1857 }
1858 }
1859 }
1860
1861
1862 //
1863 // Write driver module protocol bitmask.
1864 //
1865 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverModelProtocolBitmask = ");
1866 fileBuffer.append(Integer.toString(BitMask));
1867 fileBuffer.append(";\r\n");
1868
1869 //
1870 // Write driver module protocol list entry
1871 //
1872 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverModelProtocolListEntries = ");
1873
1874 fileBuffer.append(Integer.toString(driverBindingGroup.length));
1875 fileBuffer.append(";\r\n");
1876
1877 //
1878 // Write drive module protocol list to autogen.c
1879 //
1880 if (driverBindingGroup.length > 0) {
1881 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const EFI_DRIVER_MODEL_PROTOCOL_LIST _gDriverModelProtocolList[] = {");
1882 }
1883
1884
1885 for (int i = 0; i < driverBindingGroup.length; i++) {
1886 if (i != 0) {
1887 fileBuffer.append(",");
1888 }
1889 //
1890 // DriverBinding
1891 //
1892 fileBuffer.append("\r\n {\r\n");
1893 fileBuffer.append(" &");
1894 fileBuffer.append(driverBindingGroup[i][0]);
1895 fileBuffer.append(", \r\n");
1896
1897 //
1898 // ComponentName
1899 //
1900 if (driverBindingGroup[i][1] != null && componentNamePcd != true) {
1901 fileBuffer.append(" &");
1902 fileBuffer.append(driverBindingGroup[i][1]);
1903 fileBuffer.append(", \r\n");
1904 } else {
1905 fileBuffer.append(" NULL, \r\n");
1906 }
1907
1908 //
1909 // DriverConfiguration
1910 //
1911 if (driverBindingGroup[i][2] != null) {
1912 fileBuffer.append(" &");
1913 fileBuffer.append(driverBindingGroup[i][2]);
1914 fileBuffer.append(", \r\n");
1915 } else {
1916 fileBuffer.append(" NULL, \r\n");
1917 }
1918
1919 //
1920 // DriverDiagnostic
1921 //
1922 if (driverBindingGroup[i][3] != null && driverDiagnostPcd != true) {
1923 fileBuffer.append(" &");
1924 fileBuffer.append(driverBindingGroup[i][3]);
1925 fileBuffer.append(", \r\n");
1926 } else {
1927 fileBuffer.append(" NULL, \r\n");
1928 }
1929 fileBuffer.append(" }");
1930 }
1931
1932 if (driverBindingGroup.length > 0) {
1933 fileBuffer.append("\r\n};\r\n");
1934 }
1935 }
1936
1937 /**
1938 ExternCallBackToAutoGenC
1939
1940 This function adds <SetVirtualAddressMapCallBack> and
1941 <ExitBootServicesCallBack> infomation to AutoGen.c
1942
1943 @param fileBuffer
1944 String buffer for AutoGen.c
1945 @throws BuildException
1946 **/
1947 void ExternCallBackToAutoGenH(String moduleType)
1948 throws EdkException {
1949 //
1950 // Collect module's <SetVirtualAddressMapCallBack> and
1951 // <ExitBootServiceCallBack> and add to setVirtualAddList
1952 // exitBootServiceList.
1953 //
1954 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1955 String[] exitBoots = saq.getExitBootServicesCallBackArray();
1956 //
1957 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
1958 // and <ExitBootServicesCallBack>
1959 //
1960 switch (CommonDefinition.getModuleType(moduleType)) {
1961 case CommonDefinition.ModuleTypeDxeDriver:
1962 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1963 case CommonDefinition.ModuleTypeDxeSalDriver:
1964 case CommonDefinition.ModuleTypeUefiDriver:
1965 case CommonDefinition.ModuleTypeUefiApplication:
1966 //
1967 // Write SetVirtualAddressMap function definition.
1968 //
1969 for (int i = 0; setVirtuals != null && i < setVirtuals.length; i++) {
1970 if (setVirtuals[i].equalsIgnoreCase("")) {
1971 continue;
1972 }
1973 functionDeclarations.append("VOID\r\n");
1974 functionDeclarations.append("EFIAPI\r\n");
1975 functionDeclarations.append(setVirtuals[i]);
1976 functionDeclarations.append(" (\r\n");
1977 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
1978 functionDeclarations.append(" IN VOID *Context\r\n");
1979 functionDeclarations.append(" );\r\n\r\n");
1980 }
1981
1982 //
1983 // Write DriverExitBootServices function definition.
1984 //
1985 for (int i = 0; exitBoots != null && i < exitBoots.length; i++) {
1986 if (exitBoots[i].equalsIgnoreCase("")) {
1987 continue;
1988 }
1989
1990 functionDeclarations.append("VOID\r\n");
1991 functionDeclarations.append("EFIAPI\r\n");
1992 functionDeclarations.append(exitBoots[i]);
1993 functionDeclarations.append(" (\r\n");
1994 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
1995 functionDeclarations.append(" IN VOID *Context\r\n");
1996 functionDeclarations.append(" );\r\n\r\n");
1997 }
1998 break;
1999 default:
2000 break;
2001 }
2002 }
2003
2004 /**
2005 ExternCallBackToAutoGenC
2006
2007 This function adds <SetVirtualAddressMapCallBack> and
2008 <ExitBootServicesCallBack> infomation to AutoGen.c
2009
2010 @param fileBuffer
2011 String buffer for AutoGen.c
2012 @throws BuildException
2013 **/
2014 void ExternCallBackToAutoGenC(StringBuffer fileBuffer)
2015 throws EdkException {
2016 //
2017 // Collect module's <SetVirtualAddressMapCallBack> and
2018 // <ExitBootServiceCallBack> and add to setVirtualAddList
2019 // exitBootServiceList.
2020 //
2021 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
2022 String[] exitBoots = saq.getExitBootServicesCallBackArray();
2023 if (setVirtuals != null) {
2024 for (int j = 0; j < setVirtuals.length; j++) {
2025 this.setVirtalAddList.add(setVirtuals[j]);
2026 }
2027 }
2028 if (exitBoots != null) {
2029 for (int k = 0; k < exitBoots.length; k++) {
2030 this.exitBootServiceList.add(exitBoots[k]);
2031 }
2032 }
2033 //
2034 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
2035 // and <ExitBootServicesCallBack>
2036 //
2037 String moduleType = this.moduleId.getModuleType();
2038 switch (CommonDefinition.getModuleType(moduleType)) {
2039 case CommonDefinition.ModuleTypeDxeDriver:
2040 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
2041 case CommonDefinition.ModuleTypeDxeSalDriver:
2042 case CommonDefinition.ModuleTypeUefiDriver:
2043 case CommonDefinition.ModuleTypeUefiApplication:
2044 //
2045 // If moduleType is one of above, call setVirtualAddressToAutogenC,
2046 // and setExitBootServiceToAutogenC.
2047 //
2048 setVirtualAddressToAutogenC(fileBuffer);
2049 setExitBootServiceToAutogenC(fileBuffer);
2050 break;
2051 default:
2052 break;
2053 }
2054 }
2055
2056 /**
2057 copyFlashMapHToDebugDir
2058
2059 This function is to copy the falshmap.h to debug directory and change
2060 its name to TianoR8FlashMap.h
2061
2062 @param
2063 @return
2064 **/
2065 private void copyFlashMapHToDebugDir() throws AutoGenException{
2066
2067 File inFile = new File(fvDir + File.separatorChar + CommonDefinition.FLASHMAPH);
2068 int size = (int)inFile.length();
2069 byte[] buffer = new byte[size];
2070 File outFile = new File (this.outputPath + File.separatorChar + CommonDefinition.TIANOR8PLASHMAPH);
2071 //
2072 // If TianoR8FlashMap.h existed and the flashMap.h don't change,
2073 // do nothing.
2074 //
2075 if ((!outFile.exists()) ||(inFile.lastModified() - outFile.lastModified()) >= 0) {
2076 if (inFile.exists()) {
2077 try {
2078 FileInputStream fis = new FileInputStream (inFile);
2079 fis.read(buffer);
2080 FileOutputStream fos = new FileOutputStream(outFile);
2081 fos.write(buffer);
2082 fis.close();
2083 fos.close();
2084 } catch (IOException e) {
2085 throw new AutoGenException("The file, flashMap.h can't be open!");
2086 }
2087
2088 } else {
2089 throw new AutoGenException("The file, flashMap.h doesn't exist!");
2090 }
2091 }
2092 }
2093
2094 /**
2095 This function first order the library instances, then collect
2096 library instance 's PPI, Protocol, GUID,
2097 SetVirtalAddressMapCallBack, ExitBootServiceCallBack, and
2098 Destructor, Constructor.
2099
2100 @param
2101 @return
2102 **/
2103 private void collectLibInstanceInfo() throws EdkException{
2104 int index;
2105
2106 String moduleType = moduleId.getModuleType();
2107 String libConstructName = null;
2108 String libDestructName = null;
2109 String libModuleType = null;
2110 String[] setVirtuals = null;
2111 String[] exitBoots = null;
2112
2113 ModuleIdentification[] libraryIdList = saq.getLibraryInstance(this.arch);
2114 if (libraryIdList.length <= 0) {
2115 return;
2116 }
2117 //
2118 // Reorder library instance sequence.
2119 //
2120 AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList, this.arch);
2121 List<ModuleIdentification> orderList = libOrder.orderLibInstance();
2122 //
2123 // Process library instance one by one.
2124 //
2125 for (int i = 0; i < orderList.size(); i++) {
2126 //
2127 // Get library instance basename.
2128 //
2129 ModuleIdentification libInstanceId = orderList.get(i);
2130
2131 //
2132 // Get override map
2133 //
2134
2135 Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);
2136 saq.push(libDoc);
2137 //
2138 // check if the library instance support current module
2139 //
2140 String[] libraryClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED,
2141 this.arch,
2142 moduleType
2143 );
2144 if (libraryClassList.length <= 0) {
2145 throw new EdkException("Library instance " + libInstanceId.getName()
2146 + " doesn't support module type " + moduleType);
2147 }
2148 //
2149 // Get <PPis>, <Protocols>, <Guids> list of this library
2150 // instance.
2151 //
2152 String[] ppiList = saq.getPpiArray(this.arch);
2153 String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);
2154 String[] protocolList = saq.getProtocolArray(this.arch);
2155 String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);
2156 String[] guidList = saq.getGuidEntryArray(this.arch);
2157 PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);
2158
2159 //
2160 // Add those ppi, protocol, guid in global ppi,
2161 // protocol, guid
2162 // list.
2163 //
2164 for (index = 0; index < ppiList.length; index++) {
2165 this.mPpiList.add(ppiList[index]);
2166 }
2167
2168 for (index = 0; index < ppiNotifyList.length; index++) {
2169 this.mPpiList.add(ppiNotifyList[index]);
2170 }
2171
2172 for (index = 0; index < protocolList.length; index++) {
2173 this.mProtocolList.add(protocolList[index]);
2174 }
2175
2176 for (index = 0; index < protocolNotifyList.length; index++) {
2177 this.mProtocolList.add(protocolNotifyList[index]);
2178 }
2179
2180 for (index = 0; index < guidList.length; index++) {
2181 this.mGuidList.add(guidList[index]);
2182 }
2183 for (index = 0; index < pkgList.length; index++) {
2184 if (!this.mDepPkgList.contains(pkgList[index])) {
2185 this.mDepPkgList.add(pkgList[index]);
2186 }
2187 }
2188
2189 //
2190 // If not yet parse this library instance's constructor
2191 // element,parse it.
2192 //
2193 libConstructName = saq.getLibConstructorName();
2194 libDestructName = saq.getLibDestructorName();
2195 libModuleType = saq.getModuleType();
2196
2197 //
2198 // Collect SetVirtualAddressMapCallBack and
2199 // ExitBootServiceCallBack.
2200 //
2201 setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
2202 exitBoots = saq.getExitBootServicesCallBackArray();
2203 if (setVirtuals != null) {
2204 for (int j = 0; j < setVirtuals.length; j++) {
2205 this.setVirtalAddList.add(setVirtuals[j]);
2206 }
2207 }
2208 if (exitBoots != null) {
2209 for (int k = 0; k < exitBoots.length; k++) {
2210 this.exitBootServiceList.add(exitBoots[k]);
2211 }
2212 }
2213 saq.pop();
2214 //
2215 // Add dependent library instance constructor function.
2216 //
2217 if (libConstructName != null) {
2218 this.libConstructList.add(new String[] {libConstructName, libModuleType});
2219 }
2220 //
2221 // Add dependent library instance destructor fuction.
2222 //
2223 if (libDestructName != null) {
2224 this.libDestructList.add(new String[] {libDestructName, libModuleType});
2225 }
2226 }
2227 }
2228
2229 private void setVirtualAddressToAutogenC(StringBuffer fileBuffer){
2230 //
2231 // Entry point lib for these module types needs to know the count
2232 // of entryPoint.
2233 //
2234 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverSetVirtualAddressMapEventCount = ");
2235
2236 //
2237 // If the list is not valid or has no entries set count to zero else
2238 // set count to the number of valid entries
2239 //
2240 int Count = 0;
2241 int i = 0;
2242 if (this.setVirtalAddList != null) {
2243 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2244 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2245 break;
2246 }
2247 }
2248 Count = i;
2249 }
2250
2251 fileBuffer.append(Integer.toString(Count));
2252 fileBuffer.append(";\r\n\r\n");
2253 if (this.setVirtalAddList == null || this.setVirtalAddList.size() == 0) {
2254 //
2255 // No data so make a NULL list
2256 //
2257 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {\r\n");
2258 fileBuffer.append(" NULL\r\n");
2259 fileBuffer.append("};\r\n\r\n");
2260 } else {
2261 //
2262 // Write SetVirtualAddressMap function definition.
2263 //
2264 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2265 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2266 break;
2267 }
2268 functionDeclarations.append("VOID\r\n");
2269 functionDeclarations.append("EFIAPI\r\n");
2270 functionDeclarations.append(this.setVirtalAddList.get(i));
2271 functionDeclarations.append(" (\r\n");
2272 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
2273 functionDeclarations.append(" IN VOID *Context\r\n");
2274 functionDeclarations.append(" );\r\n\r\n");
2275 }
2276
2277 //
2278 // Write SetVirtualAddressMap entry point array.
2279 //
2280 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {");
2281 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2282 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2283 break;
2284 }
2285
2286 if (i == 0) {
2287 fileBuffer.append("\r\n ");
2288 } else {
2289 fileBuffer.append(",\r\n ");
2290 }
2291
2292 fileBuffer.append(this.setVirtalAddList.get(i));
2293 }
2294 //
2295 // add the NULL at the end of _gDriverSetVirtualAddressMapEvent list.
2296 //
2297 fileBuffer.append(",\r\n NULL");
2298 fileBuffer.append("\r\n};\r\n\r\n");
2299 }
2300 }
2301
2302
2303 private void setExitBootServiceToAutogenC(StringBuffer fileBuffer){
2304 //
2305 // Entry point lib for these module types needs to know the count.
2306 //
2307 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverExitBootServicesEventCount = ");
2308
2309 //
2310 // If the list is not valid or has no entries set count to zero else
2311 // set count to the number of valid entries.
2312 //
2313 int Count = 0;
2314 int i = 0;
2315 if (this.exitBootServiceList != null) {
2316 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2317 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2318 break;
2319 }
2320 }
2321 Count = i;
2322 }
2323 fileBuffer.append(Integer.toString(Count));
2324 fileBuffer.append(";\r\n\r\n");
2325
2326 if (this.exitBootServiceList == null || this.exitBootServiceList.size() == 0) {
2327 //
2328 // No data so make a NULL list.
2329 //
2330 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {\r\n");
2331 fileBuffer.append(" NULL\r\n");
2332 fileBuffer.append("};\r\n\r\n");
2333 } else {
2334 //
2335 // Write DriverExitBootServices function definition.
2336 //
2337 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2338 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2339 break;
2340 }
2341
2342 functionDeclarations.append("VOID\r\n");
2343 functionDeclarations.append("EFIAPI\r\n");
2344 functionDeclarations.append(this.exitBootServiceList.get(i));
2345 functionDeclarations.append(" (\r\n");
2346 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
2347 functionDeclarations.append(" IN VOID *Context\r\n");
2348 functionDeclarations.append(" );\r\n\r\n");
2349 }
2350
2351 //
2352 // Write DriverExitBootServices entry point array.
2353 //
2354 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {");
2355 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2356 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2357 break;
2358 }
2359
2360 if (i == 0) {
2361 fileBuffer.append("\r\n ");
2362 } else {
2363 fileBuffer.append(",\r\n ");
2364 }
2365 fileBuffer.append(this.exitBootServiceList.get(i));
2366 }
2367
2368 fileBuffer.append(",\r\n NULL");
2369 fileBuffer.append("\r\n};\r\n\r\n");
2370 }
2371 }
2372 /**
2373 setPcdComponentName
2374
2375 Get the Pcd Value of ComponentName to
2376 decide whether need to disable the componentName.
2377
2378 **/
2379 public void setPcdComponentName (){
2380 String pcdValue = null;
2381 pcdValue = saq.getPcdValueBycName("PcdComponentNameDisable");
2382 if (pcdValue != null && pcdValue.equalsIgnoreCase("true")) {
2383 this.componentNamePcd = true;
2384 }
2385 }
2386
2387 /**
2388 setPcdDriverDiagnostic
2389
2390 Get the Pcd Value of DriverDiagnostic to
2391 decide whether need to disable DriverDiagnostic.
2392
2393 **/
2394 public void setPcdDriverDiagnostic (){
2395 String pcdValue = null;
2396 pcdValue = saq.getPcdValueBycName("PcdDriverDiagnosticsDisable");
2397 if (pcdValue != null && pcdValue.equalsIgnoreCase("true")) {
2398 this.driverDiagnostPcd = true;
2399 }
2400 }
2401
2402 }