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