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