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