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