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