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