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