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