]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
Replace "system.out.print" with "EdkLog.Log".
[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 //
1674 // Get the arry of extern. The driverBindingGroup is a 2 dimension array.
1675 // The second dimension is include following element: DriverBinding,
1676 // ComponentName, DriverConfiguration, DriverDiag;
1677 //
1678 String[][] driverBindingGroup = this.saq.getExternProtocolGroup();
1679
1680 //
1681 // inital BitMask;
1682 //
1683 int BitMask = 0;
1684
1685 //
1686 // Write driver binding protocol extern to autogen.c
1687 //
1688 for (int i = 0; i < driverBindingGroup.length; i++) {
1689 if (driverBindingGroup[i][0] != null) {
1690 fileBuffer.append("extern EFI_DRIVER_BINDING_PROTOCOL ");
1691 fileBuffer.append(driverBindingGroup[i][0]);
1692 fileBuffer.append(";\r\n");
1693 }
1694 }
1695
1696 //
1697 // Write component name protocol extern to autogen.c
1698 //
1699 for (int i = 0; i < driverBindingGroup.length; i++) {
1700 if (driverBindingGroup[i][1]!= null) {
1701 if (driverBindingGroup[i][0] != null) {
1702 BitMask |= 0x01;
1703 fileBuffer.append("extern EFI_COMPONENT_NAME_PROTOCOL ");
1704 fileBuffer.append(driverBindingGroup[i][1]);
1705 fileBuffer.append(";\r\n");
1706 } else {
1707 throw new AutoGenException("DriverBinding can't be empty!!");
1708 }
1709 }
1710 }
1711
1712 //
1713 // Write driver configration protocol extern to autogen.c
1714 //
1715 for (int i = 0; i < driverBindingGroup.length; i++) {
1716 if (driverBindingGroup[i][2] != null) {
1717 if (driverBindingGroup[i][0] != null) {
1718 BitMask |= 0x02;
1719 fileBuffer.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");
1720 fileBuffer.append(driverBindingGroup[i][2]);
1721 fileBuffer.append(";\r\n");
1722 } else {
1723 throw new AutoGenException("DriverBinding can't be empty!!");
1724 }
1725 }
1726 }
1727
1728 //
1729 // Write driver dignastic protocol extern to autogen.c
1730 //
1731 for (int i = 0; i < driverBindingGroup.length; i++) {
1732 if (driverBindingGroup[i][3] != null) {
1733 if (driverBindingGroup[i][0] != null) {
1734 BitMask |= 0x04;
1735 fileBuffer.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");
1736 fileBuffer.append(driverBindingGroup[i][3]);
1737 fileBuffer.append(";\r\n");
1738 } else {
1739 throw new AutoGenException("DriverBinding can't be empty!!");
1740 }
1741 }
1742 }
1743
1744 //
1745 // Write driver module protocol bitmask.
1746 //
1747 fileBuffer
1748 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverModelProtocolBitmask = ");
1749 fileBuffer.append(Integer.toString(BitMask));
1750 fileBuffer.append(";\r\n");
1751
1752 //
1753 // Write driver module protocol list entry
1754 //
1755 fileBuffer
1756 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverModelProtocolListEntries = ");
1757
1758 fileBuffer.append(Integer.toString(driverBindingGroup.length));
1759 fileBuffer.append(";\r\n");
1760
1761 //
1762 // Write drive module protocol list to autogen.c
1763 //
1764 if (driverBindingGroup.length > 0) {
1765 fileBuffer
1766 .append("GLOBAL_REMOVE_IF_UNREFERENCED const EFI_DRIVER_MODEL_PROTOCOL_LIST _gDriverModelProtocolList[] = {");
1767 }
1768
1769
1770 for (int i = 0; i < driverBindingGroup.length; i++) {
1771 if (i != 0) {
1772 fileBuffer.append(",");
1773 }
1774
1775 fileBuffer.append("\r\n {\r\n");
1776 fileBuffer.append(" &");
1777 fileBuffer.append(driverBindingGroup[i][0]);
1778 fileBuffer.append(", \r\n");
1779
1780
1781 if (driverBindingGroup[i][1] != null) {
1782 fileBuffer.append(" &");
1783 fileBuffer.append(driverBindingGroup[i][1]);
1784 fileBuffer.append(", \r\n");
1785 } else {
1786 fileBuffer.append(" NULL, \r\n");
1787 }
1788
1789 if (driverBindingGroup[i][2] != null) {
1790 fileBuffer.append(" &");
1791 fileBuffer.append(driverBindingGroup[i][2]);
1792 fileBuffer.append(", \r\n");
1793 } else {
1794 fileBuffer.append(" NULL, \r\n");
1795 }
1796
1797 if (driverBindingGroup[i][3] != null) {
1798 fileBuffer.append(" &");
1799 fileBuffer.append(driverBindingGroup[i][3]);
1800 fileBuffer.append(", \r\n");
1801 } else {
1802 fileBuffer.append(" NULL, \r\n");
1803 }
1804 fileBuffer.append(" }");
1805 }
1806
1807 if (driverBindingGroup.length > 0) {
1808 fileBuffer.append("\r\n};\r\n");
1809 }
1810 }
1811
1812 /**
1813 ExternCallBackToAutoGenC
1814
1815 This function adds <SetVirtualAddressMapCallBack> and
1816 <ExitBootServicesCallBack> infomation to AutoGen.c
1817
1818 @param fileBuffer
1819 String buffer for AutoGen.c
1820 @throws BuildException
1821 **/
1822 void ExternCallBackToAutoGenC(StringBuffer fileBuffer)
1823 throws EdkException {
1824 //
1825 // Collect module's <SetVirtualAddressMapCallBack> and
1826 // <ExitBootServiceCallBack> and add to setVirtualAddList
1827 // exitBootServiceList.
1828 //
1829 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1830 String[] exitBoots = saq.getExitBootServicesCallBackArray();
1831 if (setVirtuals != null) {
1832 for (int j = 0; j < setVirtuals.length; j++) {
1833 this.setVirtalAddList.add(setVirtuals[j]);
1834 }
1835 }
1836 if (exitBoots != null) {
1837 for (int k = 0; k < exitBoots.length; k++) {
1838 this.exitBootServiceList.add(exitBoots[k]);
1839 }
1840 }
1841 //
1842 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
1843 // and <ExitBootServicesCallBack>
1844 //
1845 String moduleType = this.moduleId.getModuleType();
1846 switch (CommonDefinition.getModuleType(moduleType)) {
1847 case CommonDefinition.ModuleTypeDxeDriver:
1848 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1849 case CommonDefinition.ModuleTypeDxeSalDriver:
1850 case CommonDefinition.ModuleTypeUefiDriver:
1851 case CommonDefinition.ModuleTypeUefiApplication:
1852 //
1853 // If moduleType is one of above, call setVirtualAddressToAutogenC,
1854 // and setExitBootServiceToAutogenC.
1855 //
1856 setVirtualAddressToAutogenC(fileBuffer);
1857 setExitBootServiceToAutogenC(fileBuffer);
1858 break;
1859 default:
1860 break;
1861 }
1862 }
1863
1864 /**
1865 copyFlashMapHToDebugDir
1866
1867 This function is to copy the falshmap.h to debug directory and change
1868 its name to TianoR8FlashMap.h
1869
1870 @param
1871 @return
1872 **/
1873 private void copyFlashMapHToDebugDir() throws AutoGenException{
1874
1875 File inFile = new File(fvDir + File.separatorChar + CommonDefinition.FLASHMAPH);
1876 int size = (int)inFile.length();
1877 byte[] buffer = new byte[size];
1878 File outFile = new File (this.outputPath + File.separatorChar + CommonDefinition.TIANOR8PLASHMAPH);
1879 //
1880 // If TianoR8FlashMap.h existed and the flashMap.h don't change,
1881 // do nothing.
1882 //
1883 if ((!outFile.exists()) ||(inFile.lastModified() - outFile.lastModified()) >= 0) {
1884 if (inFile.exists()) {
1885 try{
1886 FileInputStream fis = new FileInputStream (inFile);
1887 fis.read(buffer);
1888 FileOutputStream fos = new FileOutputStream(outFile);
1889 fos.write(buffer);
1890 fis.close();
1891 fos.close();
1892 } catch (IOException e){
1893 throw new AutoGenException("The file, flashMap.h can't be open!");
1894 }
1895
1896 } else {
1897 throw new AutoGenException("The file, flashMap.h doesn't exist!");
1898 }
1899 }
1900 }
1901
1902 /**
1903 This function first order the library instances, then collect
1904 library instance 's PPI, Protocol, GUID,
1905 SetVirtalAddressMapCallBack, ExitBootServiceCallBack, and
1906 Destructor, Constructor.
1907
1908 @param
1909 @return
1910 **/
1911 private void collectLibInstanceInfo() throws EdkException{
1912 int index;
1913
1914 String libConstructName = null;
1915 String libDestructName = null;
1916 String libModuleType = null;
1917 String[] setVirtuals = null;
1918 String[] exitBoots = null;
1919
1920 ModuleIdentification[] libraryIdList = saq.getLibraryInstance(this.arch);
1921
1922 if (libraryIdList != null) {
1923 //
1924 // Reorder library instance sequence.
1925 //
1926 AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList,
1927 this.arch);
1928 List<ModuleIdentification> orderList = libOrder
1929 .orderLibInstance();
1930
1931 if (orderList != null) {
1932 //
1933 // Process library instance one by one.
1934 //
1935 for (int i = 0; i < orderList.size(); i++) {
1936
1937 //
1938 // Get library instance basename.
1939 //
1940 ModuleIdentification libInstanceId = orderList.get(i);
1941
1942 //
1943 // Get override map
1944 //
1945
1946 Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);
1947 saq.push(libDoc);
1948 //
1949 // Get <PPis>, <Protocols>, <Guids> list of this library
1950 // instance.
1951 //
1952 String[] ppiList = saq.getPpiArray(this.arch);
1953 String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);
1954 String[] protocolList = saq.getProtocolArray(this.arch);
1955 String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);
1956 String[] guidList = saq.getGuidEntryArray(this.arch);
1957 PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);
1958
1959 //
1960 // Add those ppi, protocol, guid in global ppi,
1961 // protocol, guid
1962 // list.
1963 //
1964 for (index = 0; index < ppiList.length; index++) {
1965 this.mPpiList.add(ppiList[index]);
1966 }
1967
1968 for (index = 0; index < ppiNotifyList.length; index++) {
1969 this.mPpiList.add(ppiNotifyList[index]);
1970 }
1971
1972 for (index = 0; index < protocolList.length; index++) {
1973 this.mProtocolList.add(protocolList[index]);
1974 }
1975
1976 for (index = 0; index < protocolNotifyList.length; index++) {
1977 this.mProtocolList.add(protocolNotifyList[index]);
1978 }
1979
1980 for (index = 0; index < guidList.length; index++) {
1981 this.mGuidList.add(guidList[index]);
1982 }
1983 for (index = 0; index < pkgList.length; index++) {
1984 if (!this.mDepPkgList.contains(pkgList[index])) {
1985 this.mDepPkgList.add(pkgList[index]);
1986 }
1987 }
1988
1989 //
1990 // If not yet parse this library instance's constructor
1991 // element,parse it.
1992 //
1993 libConstructName = saq.getLibConstructorName();
1994 libDestructName = saq.getLibDestructorName();
1995 libModuleType = saq.getModuleType();
1996
1997 //
1998 // Collect SetVirtualAddressMapCallBack and
1999 // ExitBootServiceCallBack.
2000 //
2001 setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
2002 exitBoots = saq.getExitBootServicesCallBackArray();
2003 if (setVirtuals != null) {
2004 for (int j = 0; j < setVirtuals.length; j++) {
2005 this.setVirtalAddList.add(setVirtuals[j]);
2006 }
2007 }
2008 if (exitBoots != null) {
2009 for (int k = 0; k < exitBoots.length; k++) {
2010 this.exitBootServiceList.add(exitBoots[k]);
2011 }
2012 }
2013 saq.pop();
2014 //
2015 // Add dependent library instance constructor function.
2016 //
2017 if (libConstructName != null) {
2018 this.libConstructList.add(new String[] {libConstructName, libModuleType});
2019 }
2020 //
2021 // Add dependent library instance destructor fuction.
2022 //
2023 if (libDestructName != null) {
2024 this.libDestructList.add(new String[] {libDestructName, libModuleType});
2025 }
2026 }
2027 }
2028
2029 }
2030 }
2031 private void setVirtualAddressToAutogenC(StringBuffer fileBuffer){
2032 //
2033 // Entry point lib for these module types needs to know the count
2034 // of entryPoint.
2035 //
2036 fileBuffer
2037 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverSetVirtualAddressMapEventCount = ");
2038
2039 //
2040 // If the list is not valid or has no entries set count to zero else
2041 // set count to the number of valid entries
2042 //
2043 int Count = 0;
2044 int i = 0;
2045 if (this.setVirtalAddList != null) {
2046 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2047 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2048 break;
2049 }
2050 }
2051 Count = i;
2052 }
2053
2054 fileBuffer.append(Integer.toString(Count));
2055 fileBuffer.append(";\r\n\r\n");
2056 if (this.setVirtalAddList == null || this.setVirtalAddList.size() == 0) {
2057 //
2058 // No data so make a NULL list
2059 //
2060 fileBuffer
2061 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {\r\n");
2062 fileBuffer.append(" NULL\r\n");
2063 fileBuffer.append("};\r\n\r\n");
2064 } else {
2065 //
2066 // Write SetVirtualAddressMap function definition.
2067 //
2068 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2069 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2070 break;
2071 }
2072 fileBuffer.append("VOID\r\n");
2073 fileBuffer.append("EFIAPI\r\n");
2074 fileBuffer.append(this.setVirtalAddList.get(i));
2075 fileBuffer.append(" (\r\n");
2076 fileBuffer.append(" IN EFI_EVENT Event,\r\n");
2077 fileBuffer.append(" IN VOID *Context\r\n");
2078 fileBuffer.append(" );\r\n\r\n");
2079 }
2080
2081 //
2082 // Write SetVirtualAddressMap entry point array.
2083 //
2084 fileBuffer
2085 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {");
2086 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2087 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2088 break;
2089 }
2090
2091 if (i == 0) {
2092 fileBuffer.append("\r\n ");
2093 } else {
2094 fileBuffer.append(",\r\n ");
2095 }
2096
2097 fileBuffer.append(this.setVirtalAddList.get(i));
2098 }
2099 //
2100 // add the NULL at the end of _gDriverSetVirtualAddressMapEvent list.
2101 //
2102 fileBuffer.append(",\r\n NULL");
2103 fileBuffer.append("\r\n};\r\n\r\n");
2104 }
2105 }
2106
2107
2108 private void setExitBootServiceToAutogenC(StringBuffer fileBuffer){
2109 //
2110 // Entry point lib for these module types needs to know the count.
2111 //
2112 fileBuffer
2113 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverExitBootServicesEventCount = ");
2114
2115 //
2116 // If the list is not valid or has no entries set count to zero else
2117 // set count to the number of valid entries.
2118 //
2119 int Count = 0;
2120 int i = 0;
2121 if (this.exitBootServiceList != null) {
2122 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2123 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2124 break;
2125 }
2126 }
2127 Count = i;
2128 }
2129 fileBuffer.append(Integer.toString(Count));
2130 fileBuffer.append(";\r\n\r\n");
2131
2132 if (this.exitBootServiceList == null || this.exitBootServiceList.size() == 0) {
2133 //
2134 // No data so make a NULL list.
2135 //
2136 fileBuffer
2137 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {\r\n");
2138 fileBuffer.append(" NULL\r\n");
2139 fileBuffer.append("};\r\n\r\n");
2140 } else {
2141 //
2142 // Write DriverExitBootServices function definition.
2143 //
2144 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2145 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2146 break;
2147 }
2148
2149 fileBuffer.append("VOID\r\n");
2150 fileBuffer.append("EFIAPI\r\n");
2151 fileBuffer.append(this.exitBootServiceList.get(i));
2152 fileBuffer.append(" (\r\n");
2153 fileBuffer.append(" IN EFI_EVENT Event,\r\n");
2154 fileBuffer.append(" IN VOID *Context\r\n");
2155 fileBuffer.append(" );\r\n\r\n");
2156 }
2157
2158 //
2159 // Write DriverExitBootServices entry point array.
2160 //
2161 fileBuffer
2162 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {");
2163 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2164 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2165 break;
2166 }
2167
2168 if (i == 0) {
2169 fileBuffer.append("\r\n ");
2170 } else {
2171 fileBuffer.append(",\r\n ");
2172 }
2173 fileBuffer.append(this.exitBootServiceList.get(i));
2174 }
2175
2176 fileBuffer.append(",\r\n NULL");
2177 fileBuffer.append("\r\n};\r\n\r\n");
2178 }
2179
2180 }
2181
2182 }