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