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