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