]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
Unify the logger and exception in AutoGen.
[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.*;
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.logger.EdkLog;
44 import org.tianocore.common.definitions.ToolDefinitions;
45 import org.tianocore.common.exception.EdkException;
46
47 /**
48 This class is to generate Autogen.h and Autogen.c according to module surface
49 area or library surface area.
50 **/
51 public class AutoGen {
52 ///
53 /// The output path of Autogen.h and Autogen.c
54 ///
55 private String outputPath;
56
57 ///
58 /// The name of FV directory
59 ///
60 private String fvDir;
61
62 ///
63 /// The base name of module or library.
64 ///
65 private ModuleIdentification moduleId;
66
67 ///
68 /// The build architecture
69 ///
70 private String arch;
71
72 ///
73 /// PcdAutogen instance which is used to manage how to generate the PCD
74 /// information.
75 ///
76 private PCDAutoGenAction myPcdAutogen;
77
78 ///
79 /// the one of type : NOT_PCD_DRIVER, PEI_PCD_DRIVER, DXE_PCD_DRIVER
80 ///
81 private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;
82
83 ///
84 /// The protocl list which records in module or library surface area and
85 /// it's dependence on library instance surface area.
86 ///
87 private Set<String> mProtocolList = new HashSet<String>();
88
89 ///
90 /// The Ppi list which recorded in module or library surface area and its
91 /// dependency on library instance surface area.
92 ///
93 private Set<String> mPpiList = new HashSet<String>();
94
95 ///
96 /// The Guid list which recoreded in module or library surface area and it's
97 /// dependence on library instance surface area.
98 ///
99 private Set<String> mGuidList = new HashSet<String>();
100
101 ///
102 /// The dependence package list which recoreded in module or library surface
103 /// area and it's dependence on library instance surface area.
104 ///
105 private List<PackageIdentification> mDepPkgList = new LinkedList<PackageIdentification>();
106
107 ///
108 /// For non library module, add its library instance's construct and destructor to
109 /// list.
110 ///
111 private List<String> libConstructList = new ArrayList<String>();
112 private List<String> libDestructList = new ArrayList<String>();
113
114 ///
115 /// List to store SetVirtalAddressMapCallBack, ExitBootServiceCallBack
116 ///
117 private List<String> setVirtalAddList = new ArrayList<String>();
118 private List<String> exitBootServiceList = new ArrayList<String>();
119
120 private SurfaceAreaQuery saq = null;
121
122 /**
123 Construct function
124
125 This function mainly initialize some member variable.
126
127 @param fvDir
128 Absolute path of FV directory.
129 @param outputPath
130 Output path of AutoGen file.
131 @param moduleId
132 Module identification.
133 @param arch
134 Target architecture.
135 **/
136 public AutoGen(String fvDir, String outputPath, ModuleIdentification moduleId, String arch, SurfaceAreaQuery saq) {
137 this.outputPath = outputPath;
138 this.moduleId = moduleId;
139 this.arch = arch;
140 this.fvDir = fvDir;
141 this.saq = saq;
142 }
143
144 /**
145 saveFile function
146
147 This function save the content in stringBuffer to file.
148
149 @param fileName
150 The name of file.
151 @param fileBuffer
152 The content of AutoGen file in buffer.
153 @return boolean
154 "true" successful
155 "false" failed
156 **/
157 private boolean saveFile(String fileName, StringBuffer fileBuffer) {
158
159 File autoGenH = new File(fileName);
160
161 //
162 // if the file exists, compare their content
163 //
164 if (autoGenH.exists()) {
165 char[] oldFileBuffer = new char[(int) autoGenH.length()];
166 try {
167 FileReader fIn = new FileReader(autoGenH);
168 fIn.read(oldFileBuffer, 0, (int) autoGenH.length());
169 fIn.close();
170 } catch (IOException e) {
171 EdkLog.log(EdkLog.EDK_INFO, this.moduleId.getName()
172 + "'s "
173 + fileName
174 + " is exist, but can't be open!!");
175 return false;
176 }
177
178 //
179 // if we got the same file, don't re-generate it to prevent
180 // sources depending on it from re-building
181 //
182 if (fileBuffer.toString().compareTo(new String(oldFileBuffer)) == 0) {
183 return true;
184 }
185 }
186
187 try {
188 FileWriter fOut = new FileWriter(autoGenH);
189 fOut.write(fileBuffer.toString());
190 fOut.flush();
191 fOut.close();
192 } catch (IOException e) {
193 EdkLog.log(EdkLog.EDK_INFO, this.moduleId.getName()
194 + "'s "
195 + fileName
196 + " can't be create!!");
197 return false;
198 }
199 return true;
200 }
201
202 /**
203 genAutogen function
204
205 This function call libGenAutoGen or moduleGenAutogen function, which
206 dependence on generate library autogen or module autogen.
207
208 @throws BuildException
209 Failed to creat AutoGen.c & AutoGen.h.
210 **/
211 public void genAutogen() throws EdkException {
212 try {
213 //
214 // If outputPath do not exist, create it.
215 //
216 File path = new File(outputPath);
217 path.mkdirs();
218 } catch (Exception e) {
219 throw new AutoGenException(
220 "Failed to create "
221 + outputPath + " directory");
222 }
223
224 //
225 // Check current is library or not, then call the corresponding
226 // function.
227 //
228 if (this.moduleId.isLibrary()) {
229 libGenAutogen();
230 } else {
231 moduleGenAutogen();
232 }
233 }
234
235 /**
236 moduleGenAutogen function
237
238 This function generates AutoGen.c & AutoGen.h for module.
239
240 @throws BuildException
241 Faile to create module AutoGen.c & AutoGen.h.
242 **/
243 void moduleGenAutogen() throws EdkException {
244 collectLibInstanceInfo();
245 moduleGenAutogenC();
246 moduleGenAutogenH();
247 }
248
249 /**
250 libGenAutogen function
251
252 This function generates AutoGen.c & AutoGen.h for library.
253
254 @throws BuildException
255 Faile to create library AutoGen.c & AutoGen.h
256 **/
257 void libGenAutogen() throws EdkException {
258 libGenAutogenC();
259 libGenAutogenH();
260 }
261
262 /**
263 moduleGenAutogenH
264
265 This function generates AutoGen.h for module.
266
267 @throws BuildException
268 Failed to generate AutoGen.h.
269 **/
270 void moduleGenAutogenH() throws EdkException {
271
272 Set<String> libClassIncludeH;
273 String moduleType;
274 // List<String> headerFileList;
275 List<String> headerFileList;
276 Iterator item;
277 StringBuffer fileBuffer = new StringBuffer(8192);
278
279 //
280 // Write Autogen.h header notation
281 //
282 fileBuffer.append(CommonDefinition.AUTOGENHNOTATION);
283
284 //
285 // Add #ifndef ${BaseName}_AUTOGENH
286 // #def ${BseeName}_AUTOGENH
287 //
288 fileBuffer.append(CommonDefinition.IFNDEF
289 + CommonDefinition.AUTOGENH
290 + this.moduleId.getGuid().replaceAll("-", "_")
291 + ToolDefinitions.LINE_SEPARATOR);
292 fileBuffer.append(CommonDefinition.DEFINE
293 + CommonDefinition.AUTOGENH
294 + this.moduleId.getGuid().replaceAll("-", "_")
295 + ToolDefinitions.LINE_SEPARATOR
296 + ToolDefinitions.LINE_SEPARATOR);
297
298 //
299 // Write the specification version and release version at the begine
300 // of autogen.h file.
301 // Note: the specification version and release version should
302 // be got from module surface area instead of hard code by it's
303 // moduleType.
304 //
305 moduleType = saq.getModuleType();
306
307 //
308 // Add "extern int __make_me_compile_correctly;" at begin of
309 // AutoGen.h.
310 //
311 fileBuffer.append(CommonDefinition.AUTOGENHBEGIN);
312
313 //
314 // Put EFI_SPECIFICATION_VERSION, and EDK_RELEASE_VERSION.
315 //
316 String[] specList = saq.getExternSpecificaiton();
317 for (int i = 0; i < specList.length; i++) {
318 fileBuffer.append(CommonDefinition.DEFINE + specList[i]
319 + "\r\n");
320 }
321 //
322 // Write consumed package's mdouleInfo related .h file to autogen.h
323 //
324 // PackageIdentification[] consumedPkgIdList = SurfaceAreaQuery
325 // .getDependencePkg(this.arch);
326 PackageIdentification[] consumedPkgIdList = saq.getDependencePkg(this.arch);
327 if (consumedPkgIdList != null) {
328 headerFileList = depPkgToAutogenH(consumedPkgIdList, moduleType);
329 item = headerFileList.iterator();
330 while (item.hasNext()) {
331 fileBuffer.append(item.next().toString());
332 }
333 }
334
335 //
336 // Write library class's related *.h file to autogen.h.
337 //
338 String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED,this.arch);
339 if (libClassList != null) {
340 libClassIncludeH = LibraryClassToAutogenH(libClassList);
341 item = libClassIncludeH.iterator();
342 while (item.hasNext()) {
343 fileBuffer.append(item.next().toString());
344 }
345 }
346
347 libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch);
348 if (libClassList != null) {
349 libClassIncludeH = LibraryClassToAutogenH(libClassList);
350 item = libClassIncludeH.iterator();
351 while (item.hasNext()) {
352 fileBuffer.append(item.next().toString());
353 }
354 }
355 fileBuffer.append("\r\n");
356
357 //
358 // If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to
359 // {DEST_DIR_DRBUG}/FlashMap.h
360 //
361 if (saq.isHaveTianoR8FlashMap()) {
362 fileBuffer.append(CommonDefinition.INCLUDE);
363 fileBuffer.append(" <");
364 fileBuffer.append(CommonDefinition.TIANOR8PLASHMAPH + ">\r\n");
365 copyFlashMapHToDebugDir();
366 }
367
368 // Write PCD autogen information to AutoGen.h.
369 //
370 if (this.myPcdAutogen != null) {
371 fileBuffer.append("\r\n");
372 fileBuffer.append(this.myPcdAutogen.getHAutoGenString());
373 }
374
375 //
376 // Append the #endif at AutoGen.h
377 //
378 fileBuffer.append("#endif\r\n");
379
380 //
381 // Save string buffer content in AutoGen.h.
382 //
383 if (!saveFile(outputPath + File.separatorChar + "AutoGen.h", fileBuffer)) {
384 throw new AutoGenException("Failed to generate AutoGen.h !!!");
385 }
386 }
387
388 /**
389 moduleGenAutogenC
390
391 This function generates AutoGen.c for module.
392
393 @throws BuildException
394 Failed to generate AutoGen.c.
395 **/
396 void moduleGenAutogenC() throws EdkException {
397
398 StringBuffer fileBuffer = new StringBuffer(8192);
399 //
400 // Write Autogen.c header notation
401 //
402 fileBuffer.append(CommonDefinition.AUTOGENCNOTATION);
403
404 //
405 // Write #include <AutoGen.h> at beginning of AutoGen.c
406 //
407 fileBuffer.append(CommonDefinition.INCLUDEAUTOGENH);
408
409 //
410 // Get the native MSA file infomation. Since before call autogen,
411 // the MSA native <Externs> information were overrided. So before
412 // process <Externs> it should be set the DOC as the Native MSA info.
413 //
414 Map<String, XmlObject> doc = GlobalData.getNativeMsa(this.moduleId);
415 saq.push(doc);
416 //
417 // Write <Extern>
418 // DriverBinding/ComponentName/DriverConfiguration/DriverDialog
419 // to AutoGen.c
420 //
421
422 ExternsDriverBindingToAutoGenC(fileBuffer);
423
424 //
425 // Write DriverExitBootServicesEvent/DriverSetVirtualAddressMapEvent
426 // to Autogen.c
427 //
428 ExternCallBackToAutoGenC(fileBuffer);
429
430 //
431 // Write EntryPoint to autgoGen.c
432 //
433 String[] entryPointList = saq.getModuleEntryPointArray();
434 String[] unloadImageList = saq.getModuleUnloadImageArray();
435 EntryPointToAutoGen(CommonDefinition.remDupString(entryPointList),
436 CommonDefinition.remDupString(unloadImageList),
437 fileBuffer);
438
439 pcdDriverType = saq.getPcdDriverType();
440
441 //
442 // Restore the DOC which include the FPD module info.
443 //
444 saq.pop();
445
446 //
447 // Write Guid to autogen.c
448 //
449 String guid = CommonDefinition.formatGuidName(saq.getModuleGuid());
450
451 fileBuffer
452 .append("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID gEfiCallerIdGuid = {");
453 if (guid == null) {
454 throw new AutoGenException("Guid value must set!\n");
455 }
456
457 //
458 // Formate Guid as ANSI c form.Example:
459 // {0xd2b2b828, 0x826, 0x48a7,{0xb3, 0xdf, 0x98, 0x3c, 0x0, 0x60, 0x24,
460 // 0xf0}}
461 //
462
463 fileBuffer.append(guid);
464 fileBuffer.append("};\r\n");
465
466 //
467 // Generate library instance consumed protocol, guid, ppi, pcd list.
468 // Save those to this.protocolList, this.ppiList, this.pcdList,
469 // this.guidList. Write Consumed library constructor and desconstuct to
470 // autogen.c
471 //
472 LibInstanceToAutogenC(fileBuffer);
473
474 //
475 // Get module dependent Package identification.
476 //
477 PackageIdentification[] packages = saq.getDependencePkg(this.arch);
478 for (int i = 0; i < packages.length; i++) {
479 if (!this.mDepPkgList.contains(packages[i])) {
480 this.mDepPkgList.add(packages[i]);
481 }
482
483 }
484
485 //
486 // Write consumed ppi, guid, protocol to autogen.c
487 //
488 ProtocolGuidToAutogenC(fileBuffer);
489 PpiGuidToAutogenC(fileBuffer);
490 GuidGuidToAutogenC(fileBuffer);
491
492 //
493 // Call pcd autogen.
494 //
495 this.myPcdAutogen = new PCDAutoGenAction(moduleId,
496 arch,
497 false,
498 null,
499 pcdDriverType);
500
501 this.myPcdAutogen.execute();
502 if (this.myPcdAutogen != null) {
503 fileBuffer.append("\r\n");
504 fileBuffer.append(this.myPcdAutogen.getCAutoGenString());
505 }
506
507 if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {
508 throw new AutoGenException("Failed to generate AutoGen.c !!!");
509 }
510
511 }
512
513 /**
514 libGenAutogenH
515
516 This function generates AutoGen.h for library.
517
518 @throws BuildException
519 Failed to generate AutoGen.c.
520 **/
521 void libGenAutogenH() throws EdkException {
522
523 Set<String> libClassIncludeH;
524 String moduleType;
525 List<String> headerFileList;
526 Iterator item;
527 StringBuffer fileBuffer = new StringBuffer(10240);
528
529 //
530 // Write Autogen.h header notation
531 //
532 fileBuffer.append(CommonDefinition.AUTOGENHNOTATION);
533
534 //
535 // Add #ifndef ${BaseName}_AUTOGENH
536 // #def ${BseeName}_AUTOGENH
537 //
538 fileBuffer.append(CommonDefinition.IFNDEF
539 + CommonDefinition.AUTOGENH
540 + this.moduleId.getGuid().replaceAll("-", "_")
541 + ToolDefinitions.LINE_SEPARATOR);
542 fileBuffer.append(CommonDefinition.DEFINE
543 + CommonDefinition.AUTOGENH
544 + this.moduleId.getGuid().replaceAll("-", "_")
545 + ToolDefinitions.LINE_SEPARATOR
546 + ToolDefinitions.LINE_SEPARATOR);
547
548 //
549 // Write EFI_SPECIFICATION_VERSION and EDK_RELEASE_VERSION
550 // to autogen.h file.
551 // Note: the specification version and release version should
552 // be get from module surface area instead of hard code.
553 //
554 fileBuffer.append(CommonDefinition.AUTOGENHBEGIN);
555 String[] specList = saq.getExternSpecificaiton();
556 for (int i = 0; i < specList.length; i++) {
557 fileBuffer.append(CommonDefinition.DEFINE + specList[i]
558 + "\r\n");
559 }
560 // fileBuffer.append(CommonDefinition.autoGenHLine1);
561 // fileBuffer.append(CommonDefinition.autoGenHLine2);
562
563 //
564 // Write consumed package's mdouleInfo related *.h file to autogen.h.
565 //
566 moduleType = saq.getModuleType();
567 PackageIdentification[] cosumedPkglist = saq
568 .getDependencePkg(this.arch);
569 headerFileList = depPkgToAutogenH(cosumedPkglist, moduleType);
570 item = headerFileList.iterator();
571 while (item.hasNext()) {
572 fileBuffer.append(item.next().toString());
573 }
574 //
575 // Write library class's related *.h file to autogen.h
576 //
577 String[] libClassList = saq
578 .getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch);
579 if (libClassList != null) {
580 libClassIncludeH = LibraryClassToAutogenH(libClassList);
581 item = libClassIncludeH.iterator();
582 while (item.hasNext()) {
583 fileBuffer.append(item.next().toString());
584 }
585 }
586
587 libClassList = saq
588 .getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch);
589 if (libClassList != null) {
590 libClassIncludeH = LibraryClassToAutogenH(libClassList);
591 item = libClassIncludeH.iterator();
592 while (item.hasNext()) {
593 fileBuffer.append(item.next().toString());
594 }
595 }
596 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
597
598 //
599 // If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to
600 // {DEST_DIR_DRBUG}/FlashMap.h
601 //
602 if (saq.isHaveTianoR8FlashMap()) {
603 fileBuffer.append(CommonDefinition.INCLUDE);
604 fileBuffer.append(" <");
605 fileBuffer.append(CommonDefinition.TIANOR8PLASHMAPH + ">\r\n");
606 copyFlashMapHToDebugDir();
607 }
608
609 //
610 // Write PCD information to library AutoGen.h.
611 //
612 if (this.myPcdAutogen != null) {
613 fileBuffer.append("\r\n");
614 fileBuffer.append(this.myPcdAutogen.getHAutoGenString());
615 }
616
617 //
618 // Append the #endif at AutoGen.h
619 //
620 fileBuffer.append("#endif\r\n");
621
622 //
623 // Save content of string buffer to AutoGen.h file.
624 //
625 if (!saveFile(outputPath + File.separatorChar + "AutoGen.h", fileBuffer)) {
626 throw new AutoGenException("Failed to generate AutoGen.h !!!");
627 }
628 }
629
630 /**
631 libGenAutogenC
632
633 This function generates AutoGen.h for library.
634
635 @throws BuildException
636 Failed to generate AutoGen.c.
637 **/
638 void libGenAutogenC() throws EdkException {
639 StringBuffer fileBuffer = new StringBuffer(10240);
640
641 //
642 // Write Autogen.c header notation
643 //
644 fileBuffer.append(CommonDefinition.AUTOGENCNOTATION);
645
646 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
647 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
648
649 //
650 // Call pcd autogen.
651 //
652 this.myPcdAutogen = new PCDAutoGenAction(moduleId,
653 arch,
654 true,
655 saq.getModulePcdEntryNameArray(),
656 pcdDriverType);
657 this.myPcdAutogen.execute();
658 if (this.myPcdAutogen != null) {
659 fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);
660 fileBuffer.append(this.myPcdAutogen.getCAutoGenString());
661 }
662
663 if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {
664 throw new AutoGenException("Failed to generate AutoGen.c !!!");
665 }
666 }
667
668 /**
669 LibraryClassToAutogenH
670
671 This function returns *.h files declared by library classes which are
672 consumed or produced by current build module or library.
673
674 @param libClassList
675 List of library class which consumed or produce by current
676 build module or library.
677 @return includeStrList List of *.h file.
678 **/
679 Set<String> LibraryClassToAutogenH(String[] libClassList)
680 throws EdkException {
681 Set<String> includeStrList = new LinkedHashSet<String>();
682 String includeName[];
683 String str = "";
684
685 //
686 // Get include file from GlobalData's SPDTable according to
687 // library class name.
688 //
689 for (int i = 0; i < libClassList.length; i++) {
690 includeName = GlobalData.getLibraryClassHeaderFiles(
691 saq.getDependencePkg(this.arch),
692 libClassList[i]);
693 if (includeName == null) {
694 throw new AutoGenException("Can not find library class ["
695 + libClassList[i] + "] declaration in any SPD package. ");
696 }
697 for (int j = 0; j < includeName.length; j++) {
698 String includeNameStr = includeName[j];
699 if (includeNameStr != null) {
700 str = CommonDefinition.INCLUDE + " " + "<";
701 str = str + includeNameStr + ">\r\n";
702 includeStrList.add(str);
703 includeNameStr = null;
704 }
705 }
706 }
707 return includeStrList;
708 }
709
710 /**
711 IncludesToAutogenH
712
713 This function add include file in AutoGen.h file.
714
715 @param packageNameList
716 List of module depended package.
717 @param moduleType
718 Module type.
719 @return
720 **/
721 List<String> depPkgToAutogenH(PackageIdentification[] packageNameList,
722 String moduleType) throws AutoGenException {
723
724 List<String> includeStrList = new LinkedList<String>();
725 String pkgHeader;
726 String includeStr = "";
727
728 //
729 // Get include file from moduleInfo file
730 //
731 for (int i = 0; i < packageNameList.length; i++) {
732 pkgHeader = GlobalData.getPackageHeaderFiles(packageNameList[i],
733 moduleType);
734 if (pkgHeader == null) {
735 throw new AutoGenException("Can not find package ["
736 + packageNameList[i]
737 + "] declaration in any SPD package. ");
738 } else if (!pkgHeader.equalsIgnoreCase("")) {
739 includeStr = CommonDefinition.INCLUDE + " <" + pkgHeader
740 + ">\r\n";
741 includeStrList.add(includeStr);
742 }
743 }
744
745 return includeStrList;
746 }
747
748 /**
749 EntryPointToAutoGen
750
751 This function convert <ModuleEntryPoint> & <ModuleUnloadImage>
752 information in mas to AutoGen.c
753
754 @param entryPointList
755 List of entry point.
756 @param fileBuffer
757 String buffer fo AutoGen.c.
758 @throws Exception
759 **/
760 void EntryPointToAutoGen(String[] entryPointList, String[] unloadImageList, StringBuffer fileBuffer)
761 throws EdkException {
762
763 String typeStr = saq.getModuleType();
764 int unloadImageCount = 0;
765 int entryPointCount = 0;
766
767 //
768 // The parameters and return value of entryPoint is difference
769 // for difference module type.
770 //
771 switch (CommonDefinition.getModuleType(typeStr)) {
772
773 case CommonDefinition.ModuleTypePeiCore:
774 if (entryPointList == null ||entryPointList.length != 1 ) {
775 throw new AutoGenException(
776 "Module type = 'PEI_CORE', can have only one module entry point!");
777 } else {
778 fileBuffer.append("EFI_STATUS\r\n");
779 fileBuffer.append("EFIAPI\r\n");
780 fileBuffer.append(entryPointList[0]);
781 fileBuffer.append(" (\r\n");
782 fileBuffer
783 .append(" IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r\n");
784 fileBuffer
785 .append(" IN VOID *OldCoreData\r\n");
786 fileBuffer.append(" );\r\n\r\n");
787
788 fileBuffer.append("EFI_STATUS\r\n");
789 fileBuffer.append("EFIAPI\r\n");
790 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
791 fileBuffer
792 .append(" IN EFI_PEI_STARTUP_DESCRIPTOR *PeiStartupDescriptor,\r\n");
793 fileBuffer
794 .append(" IN VOID *OldCoreData\r\n");
795 fileBuffer.append(" )\r\n\r\n");
796 fileBuffer.append("{\r\n");
797 fileBuffer.append(" return ");
798 fileBuffer.append(entryPointList[0]);
799 fileBuffer.append(" (PeiStartupDescriptor, OldCoreData);\r\n");
800 fileBuffer.append("}\r\n\r\n");
801 }
802 break;
803
804 case CommonDefinition.ModuleTypeDxeCore:
805 fileBuffer.append("const UINT32 _gUefiDriverRevision = 0;\r\n");
806 if (entryPointList == null || entryPointList.length != 1) {
807 throw new AutoGenException(
808 "Module type = 'DXE_CORE', can have only one module entry point!");
809 } else {
810
811 fileBuffer.append("VOID\r\n");
812 fileBuffer.append("EFIAPI\r\n");
813 fileBuffer.append(entryPointList[0]);
814 fileBuffer.append(" (\n");
815 fileBuffer.append(" IN VOID *HobStart\r\n");
816 fileBuffer.append(" );\r\n\r\n");
817
818 fileBuffer.append("VOID\r\n");
819 fileBuffer.append("EFIAPI\r\n");
820 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
821 fileBuffer.append(" IN VOID *HobStart\r\n");
822 fileBuffer.append(" )\r\n\r\n");
823 fileBuffer.append("{\r\n");
824 fileBuffer.append(" ");
825 fileBuffer.append(entryPointList[0]);
826 fileBuffer.append(" (HobStart);\r\n");
827 fileBuffer.append("}\r\n\r\n");
828 }
829 break;
830
831 case CommonDefinition.ModuleTypePeim:
832 entryPointCount = 0;
833 fileBuffer
834 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = 0;\r\n");
835 if (entryPointList == null || entryPointList.length == 0) {
836 fileBuffer.append("EFI_STATUS\r\n");
837 fileBuffer.append("EFIAPI\r\n");
838 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
839 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
840 fileBuffer.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
841 fileBuffer.append(" )\r\n\r\n");
842 fileBuffer.append("{\r\n");
843 fileBuffer.append(" return EFI_SUCCESS;\r\n");
844 fileBuffer.append("}\r\n\r\n");
845 break;
846 }
847 for (int i = 0; i < entryPointList.length; i++) {
848 fileBuffer.append("EFI_STATUS\r\n");
849 fileBuffer.append("EFIAPI\r\n");
850 fileBuffer.append(entryPointList[i]);
851 fileBuffer.append(" (\r\n");
852 fileBuffer
853 .append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
854 fileBuffer
855 .append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
856 fileBuffer.append(" );\r\n");
857 entryPointCount++;
858
859 }
860
861 fileBuffer.append("EFI_STATUS\r\n");
862 fileBuffer.append("EFIAPI\r\n");
863 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
864 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
865 fileBuffer.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
866 fileBuffer.append(" )\r\n\r\n");
867 fileBuffer.append("{\r\n");
868 if (entryPointCount == 1) {
869 fileBuffer.append(" return ");
870 fileBuffer.append(entryPointList[0]);
871 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
872 } else {
873 fileBuffer.append(" EFI_STATUS Status;\r\n");
874 fileBuffer.append(" EFI_STATUS CombinedStatus;\r\n\r\n");
875 fileBuffer.append(" CombinedStatus = EFI_LOAD_ERROR;\r\n\r\n");
876 for (int i = 0; i < entryPointList.length; i++) {
877 if (!entryPointList[i].equals("")) {
878 fileBuffer.append(" Status = ");
879 fileBuffer.append(entryPointList[i]);
880 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
881 fileBuffer
882 .append(" if (!EFI_ERROR (Status) || EFI_ERROR (CombinedStatus)) {\r\n");
883 fileBuffer.append(" CombinedStatus = Status;\r\n");
884 fileBuffer.append(" }\r\n\r\n");
885 } else {
886 break;
887 }
888 }
889 fileBuffer.append(" return CombinedStatus;\r\n");
890 }
891 fileBuffer.append("}\r\n\r\n");
892 break;
893
894 case CommonDefinition.ModuleTypeDxeSmmDriver:
895 entryPointCount = 0;
896 //
897 // If entryPoint is null, create an empty ProcessModuleEntryPointList
898 // function.
899 //
900 if (entryPointList == null || entryPointList.length == 0) {
901 fileBuffer
902 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = ");
903 fileBuffer.append(Integer.toString(entryPointCount));
904 fileBuffer.append(";\r\n");
905 fileBuffer.append("EFI_STATUS\r\n");
906 fileBuffer.append("EFIAPI\r\n");
907 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
908 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
909 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
910 fileBuffer.append(" )\r\n\r\n");
911 fileBuffer.append("{\r\n");
912 fileBuffer.append(" return EFI_SUCCESS;\r\n");
913 fileBuffer.append("}\r\n\r\n");
914
915 } else {
916 for (int i = 0; i < entryPointList.length; i++) {
917 fileBuffer.append("EFI_STATUS\r\n");
918 fileBuffer.append("EFIAPI\r\n");
919 fileBuffer.append(entryPointList[i]);
920 fileBuffer.append(" (\r\n");
921 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
922 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
923 fileBuffer.append(" );\r\n");
924 entryPointCount++;
925 }
926 fileBuffer
927 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = ");
928 fileBuffer.append(Integer.toString(entryPointCount));
929 fileBuffer.append(";\r\n");
930 fileBuffer
931 .append("static BASE_LIBRARY_JUMP_BUFFER mJumpContext;\r\n");
932 fileBuffer
933 .append("static EFI_STATUS mDriverEntryPointStatus = EFI_LOAD_ERROR;\r\n\r\n");
934
935 fileBuffer.append("EFI_STATUS\r\n");
936 fileBuffer.append("EFIAPI\r\n");
937 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
938 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
939 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
940 fileBuffer.append(" )\r\n\r\n");
941 fileBuffer.append("{\r\n");
942
943
944 for (int i = 0; i < entryPointList.length; i++) {
945 fileBuffer
946 .append(" if (SetJump (&mJumpContext) == 0) {\r\n");
947 fileBuffer.append(" ExitDriver (");
948 fileBuffer.append(entryPointList[i]);
949 fileBuffer.append(" (ImageHandle, SystemTable));\r\n");
950 fileBuffer.append(" ASSERT (FALSE);\r\n");
951 fileBuffer.append(" }\r\n");
952
953 }
954 fileBuffer.append(" return mDriverEntryPointStatus;\r\n");
955 fileBuffer.append("}\r\n\r\n");
956
957 fileBuffer.append("VOID\r\n");
958 fileBuffer.append("EFIAPI\r\n");
959 fileBuffer.append("ExitDriver (\r\n");
960 fileBuffer.append(" IN EFI_STATUS Status\n");
961 fileBuffer.append(" )\r\n\r\n");
962 fileBuffer.append("{\r\n");
963 fileBuffer
964 .append(" if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");
965 fileBuffer.append(" mDriverEntryPointStatus = Status;\r\n");
966 fileBuffer.append(" }\r\n");
967 fileBuffer.append(" LongJump (&mJumpContext, (UINTN)-1);\r\n");
968 fileBuffer.append(" ASSERT (FALSE);\r\n");
969 fileBuffer.append("}\r\n\r\n");
970
971 }
972
973
974 //
975 // Add "ModuleUnloadImage" for DxeSmmDriver module type;
976 //
977 //entryPointList = SurfaceAreaQuery.getModuleUnloadImageArray();
978 //entryPointList = CommonDefinition.remDupString(entryPointList);
979 //entryPointCount = 0;
980
981 unloadImageCount = 0;
982 if (unloadImageList != null) {
983 for (int i = 0; i < unloadImageList.length; i++) {
984 fileBuffer.append("EFI_STATUS\r\n");
985 fileBuffer.append("EFIAPI\r\n");
986 fileBuffer.append(unloadImageList[i]);
987 fileBuffer.append(" (\r\n");
988 fileBuffer
989 .append(" IN EFI_HANDLE ImageHandle\r\n");
990 fileBuffer.append(" );\r\n");
991 unloadImageCount++;
992 }
993 }
994
995 fileBuffer
996 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ");
997 fileBuffer.append(Integer.toString(unloadImageCount));
998 fileBuffer.append(";\r\n\r\n");
999
1000 fileBuffer.append("EFI_STATUS\r\n");
1001 fileBuffer.append("EFIAPI\r\n");
1002 fileBuffer.append("ProcessModuleUnloadList (\r\n");
1003 fileBuffer.append(" IN EFI_HANDLE ImageHandle\r\n");
1004 fileBuffer.append(" )\r\n");
1005 fileBuffer.append("{\r\n");
1006
1007 if (unloadImageCount == 0) {
1008 fileBuffer.append(" return EFI_SUCCESS;\r\n");
1009 } else if (unloadImageCount == 1) {
1010 fileBuffer.append(" return ");
1011 fileBuffer.append(unloadImageList[0]);
1012 fileBuffer.append("(ImageHandle);\r\n");
1013 } else {
1014 fileBuffer.append(" EFI_STATUS Status;\r\n\r\n");
1015 fileBuffer.append(" Status = EFI_SUCCESS;\r\n\r\n");
1016 for (int i = 0; i < unloadImageList.length; i++) {
1017 if (i == 0) {
1018 fileBuffer.append(" Status = ");
1019 fileBuffer.append(unloadImageList[i]);
1020 fileBuffer.append("(ImageHandle);\r\n");
1021 } else {
1022 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1023 fileBuffer.append(" ");
1024 fileBuffer.append(unloadImageList[i]);
1025 fileBuffer.append("(ImageHandle);\r\n");
1026 fileBuffer.append(" } else {\r\n");
1027 fileBuffer.append(" Status = ");
1028 fileBuffer.append(unloadImageList[i]);
1029 fileBuffer.append("(ImageHandle);\r\n");
1030 fileBuffer.append(" }\r\n");
1031 }
1032 }
1033 fileBuffer.append(" return Status;\r\n");
1034 }
1035 fileBuffer.append("}\r\n\r\n");
1036 break;
1037
1038 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1039 case CommonDefinition.ModuleTypeDxeDriver:
1040 case CommonDefinition.ModuleTypeDxeSalDriver:
1041 case CommonDefinition.ModuleTypeUefiDriver:
1042 case CommonDefinition.ModuleTypeUefiApplication:
1043 entryPointCount = 0;
1044 fileBuffer.append("const UINT32 _gUefiDriverRevision = 0;\r\n");
1045 //
1046 // If entry point is null, create a empty ProcessModuleEntryPointList function.
1047 //
1048 if (entryPointList == null || entryPointList.length == 0) {
1049 fileBuffer
1050 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverEntryPointCount = 0;\r\n");
1051 fileBuffer.append("EFI_STATUS\r\n");
1052 fileBuffer.append("EFIAPI\r\n");
1053 fileBuffer.append("ProcessModuleEntryPointList (\r\n");
1054 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1055 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1056 fileBuffer.append(" )\r\n\r\n");
1057 fileBuffer.append("{\r\n");
1058 fileBuffer.append(" return EFI_SUCCESS;\r\n");
1059 fileBuffer.append("}\r\n");
1060
1061 } else {
1062 for (int i = 0; i < entryPointList.length; i++) {
1063
1064 fileBuffer.append("EFI_STATUS\r\n");
1065 fileBuffer.append("EFIAPI\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 switch (CommonDefinition.getModuleType(moduleType)) {
1412 case CommonDefinition.ModuleTypeBase:
1413 fileBuffer.append("RETURN_STATUS\r\n");
1414 fileBuffer.append("EFIAPI\r\n");
1415 fileBuffer.append(libInstanceList.get(i));
1416 fileBuffer.append(" (\r\n");
1417 fileBuffer.append(" VOID\r\n");
1418 fileBuffer.append(" );\r\n");
1419 break;
1420
1421 case CommonDefinition.ModuleTypePeiCore:
1422 case CommonDefinition.ModuleTypePeim:
1423 fileBuffer.append("EFI_STATUS\r\n");
1424 fileBuffer.append("EFIAPI\r\n");
1425 fileBuffer.append(libInstanceList.get(i));
1426 fileBuffer.append(" (\r\n");
1427 fileBuffer
1428 .append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1429 fileBuffer
1430 .append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1431 fileBuffer.append(" );\r\n");
1432 break;
1433
1434 case CommonDefinition.ModuleTypeDxeCore:
1435 case CommonDefinition.ModuleTypeDxeDriver:
1436 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1437 case CommonDefinition.ModuleTypeDxeSmmDriver:
1438 case CommonDefinition.ModuleTypeDxeSalDriver:
1439 case CommonDefinition.ModuleTypeUefiDriver:
1440 case CommonDefinition.ModuleTypeUefiApplication:
1441 fileBuffer.append("EFI_STATUS\r\n");
1442 fileBuffer.append("EFIAPI\r\n");
1443 fileBuffer.append(libInstanceList.get(i));
1444 fileBuffer.append(" (\r\n");
1445 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1446 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1447 fileBuffer.append(" );\r\n");
1448 break;
1449 }
1450 }
1451
1452 //
1453 // Add ProcessLibraryConstructorList in AutoGen.c
1454 //
1455 fileBuffer.append("VOID\r\n");
1456 fileBuffer.append("EFIAPI\r\n");
1457 fileBuffer.append("ProcessLibraryConstructorList (\r\n");
1458 switch (CommonDefinition.getModuleType(moduleType)) {
1459 case CommonDefinition.ModuleTypeBase:
1460 fileBuffer.append(" VOID\r\n");
1461 break;
1462
1463 case CommonDefinition.ModuleTypePeiCore:
1464 case CommonDefinition.ModuleTypePeim:
1465 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1466 fileBuffer
1467 .append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1468 break;
1469
1470 case CommonDefinition.ModuleTypeDxeCore:
1471 case CommonDefinition.ModuleTypeDxeDriver:
1472 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1473 case CommonDefinition.ModuleTypeDxeSmmDriver:
1474 case CommonDefinition.ModuleTypeDxeSalDriver:
1475 case CommonDefinition.ModuleTypeUefiDriver:
1476 case CommonDefinition.ModuleTypeUefiApplication:
1477 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1478 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1479 break;
1480 }
1481
1482 fileBuffer.append(" )\r\n");
1483 fileBuffer.append("{\r\n");
1484 //
1485 // If no constructor function, return EFI_SUCCESS.
1486 //
1487 //if (libInstanceList.size() == 0){
1488 // fileBuffer.append(" return EFI_SUCCESS;\r\n");
1489 //}
1490 for (int i = 0; i < libInstanceList.size(); i++) {
1491 if (isFirst) {
1492 fileBuffer.append(" EFI_STATUS Status;\r\n");
1493 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1494 fileBuffer.append("\r\n");
1495 isFirst = false;
1496 }
1497 switch (CommonDefinition.getModuleType(moduleType)) {
1498 case CommonDefinition.ModuleTypeBase:
1499 fileBuffer.append(" Status = ");
1500 fileBuffer.append(libInstanceList.get(i));
1501 fileBuffer.append("();\r\n");
1502 fileBuffer.append(" VOID\r\n");
1503 break;
1504 case CommonDefinition.ModuleTypePeiCore:
1505 case CommonDefinition.ModuleTypePeim:
1506 fileBuffer.append(" Status = ");
1507 fileBuffer.append(libInstanceList.get(i));
1508 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
1509 break;
1510 case CommonDefinition.ModuleTypeDxeCore:
1511 case CommonDefinition.ModuleTypeDxeDriver:
1512 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1513 case CommonDefinition.ModuleTypeDxeSmmDriver:
1514 case CommonDefinition.ModuleTypeDxeSalDriver:
1515 case CommonDefinition.ModuleTypeUefiDriver:
1516 case CommonDefinition.ModuleTypeUefiApplication:
1517 fileBuffer.append(" Status = ");
1518 fileBuffer.append(libInstanceList.get(i));
1519 fileBuffer.append(" (ImageHandle, SystemTable);\r\n");
1520 break;
1521 default:
1522 EdkLog.log(EdkLog.EDK_INFO,"Autogen doesn't know how to deal with module type - " + moduleType + "!");
1523 }
1524 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1525 }
1526 fileBuffer.append("}\r\n");
1527 }
1528
1529 /**
1530 LibDestructorToAutogenc
1531
1532 This function writes library destructor list to AutoGen.c. The library
1533 destructor's parameter and return value depend on module type.
1534
1535 @param libInstanceList
1536 List of library destructor name.
1537 @param moduleType
1538 Module type.
1539 @param fileBuffer
1540 String buffer for AutoGen.c
1541 @throws Exception
1542 **/
1543 void LibDestructorToAutogenC(List<String> libInstanceList,
1544 String moduleType, StringBuffer fileBuffer) throws EdkException {
1545 boolean isFirst = true;
1546 for (int i = 0; i < libInstanceList.size(); i++) {
1547 switch (CommonDefinition.getModuleType(moduleType)) {
1548 case CommonDefinition.ModuleTypeBase:
1549 fileBuffer.append("RETURN_STATUS\r\n");
1550 fileBuffer.append("EFIAPI\r\n");
1551 fileBuffer.append(libInstanceList.get(i));
1552 fileBuffer.append(" (\r\n");
1553 fileBuffer.append(" VOID\r\n");
1554 fileBuffer.append(" );\r\n");
1555 break;
1556 case CommonDefinition.ModuleTypePeiCore:
1557 case CommonDefinition.ModuleTypePeim:
1558 fileBuffer.append("EFI_STATUS\r\n");
1559 fileBuffer.append("EFIAPI\r\n");
1560 fileBuffer.append(libInstanceList.get(i));
1561 fileBuffer.append(" (\r\n");
1562 fileBuffer
1563 .append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1564 fileBuffer
1565 .append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1566 fileBuffer.append(" );\r\n");
1567 break;
1568 case CommonDefinition.ModuleTypeDxeCore:
1569 case CommonDefinition.ModuleTypeDxeDriver:
1570 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1571 case CommonDefinition.ModuleTypeDxeSmmDriver:
1572 case CommonDefinition.ModuleTypeDxeSalDriver:
1573 case CommonDefinition.ModuleTypeUefiDriver:
1574 case CommonDefinition.ModuleTypeUefiApplication:
1575 fileBuffer.append("EFI_STATUS\r\n");
1576 fileBuffer.append("EFIAPI\r\n");
1577 fileBuffer.append(libInstanceList.get(i));
1578 fileBuffer.append(" (\r\n");
1579 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1580 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1581 fileBuffer.append(" );\r\n");
1582 break;
1583 }
1584 }
1585
1586 //
1587 // Write ProcessLibraryDestructor list to autogen.c
1588 //
1589 switch (CommonDefinition.getModuleType(moduleType)) {
1590 case CommonDefinition.ModuleTypeBase:
1591 case CommonDefinition.ModuleTypePeiCore:
1592 case CommonDefinition.ModuleTypePeim:
1593 break;
1594 case CommonDefinition.ModuleTypeDxeCore:
1595 case CommonDefinition.ModuleTypeDxeDriver:
1596 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1597 case CommonDefinition.ModuleTypeDxeSmmDriver:
1598 case CommonDefinition.ModuleTypeDxeSalDriver:
1599 case CommonDefinition.ModuleTypeUefiDriver:
1600 case CommonDefinition.ModuleTypeUefiApplication:
1601 fileBuffer.append("VOID\r\n");
1602 fileBuffer.append("EFIAPI\r\n");
1603 fileBuffer.append("ProcessLibraryDestructorList (\r\n");
1604 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1605 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1606 fileBuffer.append(" )\r\n");
1607 fileBuffer.append("{\r\n");
1608 //
1609 // If no library destructor function, return EFI_SUCCESS.
1610 //
1611
1612 for (int i = 0; i < libInstanceList.size(); i++) {
1613 if (isFirst) {
1614 fileBuffer.append(" EFI_STATUS Status;\r\n");
1615 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1616 fileBuffer.append("\r\n");
1617 isFirst = false;
1618 }
1619 fileBuffer.append(" Status = ");
1620 fileBuffer.append(libInstanceList.get(i));
1621 fileBuffer.append("(ImageHandle, SystemTable);\r\n");
1622 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1623 }
1624 fileBuffer.append("}\r\n");
1625 break;
1626 }
1627 }
1628
1629 /**
1630 ExternsDriverBindingToAutoGenC
1631
1632 This function is to write DRIVER_BINDING, COMPONENT_NAME,
1633 DRIVER_CONFIGURATION, DRIVER_DIAGNOSTIC in AutoGen.c.
1634
1635 @param fileBuffer
1636 String buffer for AutoGen.c
1637 **/
1638 void ExternsDriverBindingToAutoGenC(StringBuffer fileBuffer)
1639 throws EdkException {
1640
1641 //
1642 // Check what <extern> contains. And the number of following elements
1643 // under <extern> should be same. 1. DRIVER_BINDING 2. COMPONENT_NAME
1644 // 3.DRIVER_CONFIGURATION 4. DRIVER_DIAGNOSTIC
1645 //
1646
1647 String[] drvBindList = saq.getDriverBindingArray();
1648
1649 //
1650 // If component name protocol,component configuration protocol,
1651 // component diagnostic protocol is not null or empty, check
1652 // if every one have the same number of the driver binding protocol.
1653 //
1654 if (drvBindList == null || drvBindList.length == 0) {
1655 return;
1656 }
1657
1658 String[] compNamList = saq.getComponentNameArray();
1659 String[] compConfList = saq.getDriverConfigArray();
1660 String[] compDiagList = saq.getDriverDiagArray();
1661
1662 int BitMask = 0;
1663
1664 //
1665 // Write driver binding protocol extern to autogen.c
1666 //
1667 for (int i = 0; i < drvBindList.length; i++) {
1668 fileBuffer.append("extern EFI_DRIVER_BINDING_PROTOCOL ");
1669 fileBuffer.append(drvBindList[i]);
1670 fileBuffer.append(";\r\n");
1671 }
1672
1673 //
1674 // Write component name protocol extern to autogen.c
1675 //
1676 if (compNamList != null && compNamList.length != 0) {
1677 if (drvBindList.length != compNamList.length) {
1678 throw new AutoGenException(
1679 "Different number of Driver Binding and Component Name protocols!");
1680 }
1681
1682 BitMask |= 0x01;
1683 for (int i = 0; i < compNamList.length; i++) {
1684 fileBuffer.append("extern EFI_COMPONENT_NAME_PROTOCOL ");
1685 fileBuffer.append(compNamList[i]);
1686 fileBuffer.append(";\r\n");
1687 }
1688 }
1689
1690 //
1691 // Write driver configration protocol extern to autogen.c
1692 //
1693 if (compConfList != null && compConfList.length != 0) {
1694 if (drvBindList.length != compConfList.length) {
1695 throw new AutoGenException(
1696 "Different number of Driver Binding and Driver Configuration protocols!");
1697 }
1698
1699 BitMask |= 0x02;
1700 for (int i = 0; i < compConfList.length; i++) {
1701 fileBuffer.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");
1702 fileBuffer.append(compConfList[i]);
1703 fileBuffer.append(";\r\n");
1704 }
1705 }
1706
1707 //
1708 // Write driver dignastic protocol extern to autogen.c
1709 //
1710 if (compDiagList != null && compDiagList.length != 0) {
1711 if (drvBindList.length != compDiagList.length) {
1712 throw new AutoGenException(
1713 "Different number of Driver Binding and Driver Diagnosis protocols!");
1714 }
1715
1716 BitMask |= 0x04;
1717 for (int i = 0; i < compDiagList.length; i++) {
1718 fileBuffer.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");
1719 fileBuffer.append(compDiagList[i]);
1720 fileBuffer.append(";\r\n");
1721 }
1722 }
1723
1724 //
1725 // Write driver module protocol bitmask.
1726 //
1727 fileBuffer
1728 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverModelProtocolBitmask = ");
1729 fileBuffer.append(Integer.toString(BitMask));
1730 fileBuffer.append(";\r\n");
1731
1732 //
1733 // Write driver module protocol list entry
1734 //
1735 fileBuffer
1736 .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverModelProtocolListEntries = ");
1737
1738 fileBuffer.append(Integer.toString(drvBindList.length));
1739 fileBuffer.append(";\r\n");
1740
1741 //
1742 // Write drive module protocol list to autogen.c
1743 //
1744 fileBuffer
1745 .append("GLOBAL_REMOVE_IF_UNREFERENCED const EFI_DRIVER_MODEL_PROTOCOL_LIST _gDriverModelProtocolList[] = {");
1746 for (int i = 0; i < drvBindList.length; i++) {
1747 if (i != 0) {
1748 fileBuffer.append(",");
1749 }
1750 fileBuffer.append("\r\n {\r\n");
1751 fileBuffer.append(" &");
1752 fileBuffer.append(drvBindList[i]);
1753 fileBuffer.append(", \r\n");
1754
1755 if (compNamList != null) {
1756 fileBuffer.append(" &");
1757 fileBuffer.append(compNamList[i]);
1758 fileBuffer.append(", \r\n");
1759 } else {
1760 fileBuffer.append(" NULL, \r\n");
1761 }
1762
1763 if (compConfList != null) {
1764 fileBuffer.append(" &");
1765 fileBuffer.append(compConfList[i]);
1766 fileBuffer.append(", \r\n");
1767 } else {
1768 fileBuffer.append(" NULL, \r\n");
1769 }
1770
1771 if (compDiagList != null) {
1772 fileBuffer.append(" &");
1773 fileBuffer.append(compDiagList[i]);
1774 fileBuffer.append(", \r\n");
1775 } else {
1776 fileBuffer.append(" NULL, \r\n");
1777 }
1778 fileBuffer.append(" }");
1779 }
1780 fileBuffer.append("\r\n};\r\n");
1781 }
1782
1783 /**
1784 ExternCallBackToAutoGenC
1785
1786 This function adds <SetVirtualAddressMapCallBack> and
1787 <ExitBootServicesCallBack> infomation to AutoGen.c
1788
1789 @param fileBuffer
1790 String buffer for AutoGen.c
1791 @throws BuildException
1792 **/
1793 void ExternCallBackToAutoGenC(StringBuffer fileBuffer)
1794 throws EdkException {
1795 //
1796 // Collect module's <SetVirtualAddressMapCallBack> and
1797 // <ExitBootServiceCallBack> and add to setVirtualAddList
1798 // exitBootServiceList.
1799 //
1800 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1801 String[] exitBoots = saq.getExitBootServicesCallBackArray();
1802 if (setVirtuals != null) {
1803 for (int j = 0; j < setVirtuals.length; j++) {
1804 this.setVirtalAddList.add(setVirtuals[j]);
1805 }
1806 }
1807 if (exitBoots != null) {
1808 for (int k = 0; k < exitBoots.length; k++) {
1809 this.exitBootServiceList.add(exitBoots[k]);
1810 }
1811 }
1812 //
1813 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
1814 // and <ExitBootServicesCallBack>
1815 //
1816 String moduleType = this.moduleId.getModuleType();
1817 switch (CommonDefinition.getModuleType(moduleType)) {
1818 case CommonDefinition.ModuleTypeDxeDriver:
1819 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1820 case CommonDefinition.ModuleTypeDxeSalDriver:
1821 case CommonDefinition.ModuleTypeUefiDriver:
1822 case CommonDefinition.ModuleTypeUefiApplication:
1823 //
1824 // If moduleType is one of above, call setVirtualAddressToAutogenC,
1825 // and setExitBootServiceToAutogenC.
1826 //
1827 setVirtualAddressToAutogenC(fileBuffer);
1828 setExitBootServiceToAutogenC(fileBuffer);
1829 break;
1830 default:
1831 break;
1832 }
1833 }
1834
1835 /**
1836 copyFlashMapHToDebugDir
1837
1838 This function is to copy the falshmap.h to debug directory and change
1839 its name to TianoR8FlashMap.h
1840
1841 @param
1842 @return
1843 **/
1844 private void copyFlashMapHToDebugDir() throws AutoGenException{
1845
1846 File inFile = new File(fvDir + File.separatorChar + CommonDefinition.FLASHMAPH);
1847 int size = (int)inFile.length();
1848 byte[] buffer = new byte[size];
1849 File outFile = new File (this.outputPath + File.separatorChar + CommonDefinition.TIANOR8PLASHMAPH);
1850 //
1851 // If TianoR8FlashMap.h existed and the flashMap.h don't change,
1852 // do nothing.
1853 //
1854 if ((!outFile.exists()) ||(inFile.lastModified() - outFile.lastModified()) >= 0) {
1855 if (inFile.exists()) {
1856 try{
1857 FileInputStream fis = new FileInputStream (inFile);
1858 fis.read(buffer);
1859 FileOutputStream fos = new FileOutputStream(outFile);
1860 fos.write(buffer);
1861 fis.close();
1862 fos.close();
1863 } catch (IOException e){
1864 throw new AutoGenException("The file, flashMap.h can't be open!");
1865 }
1866
1867 } else {
1868 throw new AutoGenException("The file, flashMap.h doesn't exist!");
1869 }
1870 }
1871 }
1872
1873 /**
1874 This function first order the library instances, then collect
1875 library instance 's PPI, Protocol, GUID,
1876 SetVirtalAddressMapCallBack, ExitBootServiceCallBack, and
1877 Destructor, Constructor.
1878
1879 @param
1880 @return
1881 **/
1882 private void collectLibInstanceInfo() throws EdkException{
1883 int index;
1884
1885 String libConstructName = null;
1886 String libDestructName = null;
1887 String[] setVirtuals = null;
1888 String[] exitBoots = null;
1889
1890 ModuleIdentification[] libraryIdList = saq.getLibraryInstance(this.arch);
1891
1892 if (libraryIdList != null) {
1893 //
1894 // Reorder library instance sequence.
1895 //
1896 AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList,
1897 this.arch);
1898 List<ModuleIdentification> orderList = libOrder
1899 .orderLibInstance();
1900
1901 if (orderList != null) {
1902 //
1903 // Process library instance one by one.
1904 //
1905 for (int i = 0; i < orderList.size(); i++) {
1906
1907 //
1908 // Get library instance basename.
1909 //
1910 ModuleIdentification libInstanceId = orderList.get(i);
1911
1912 //
1913 // Get override map
1914 //
1915
1916 Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);
1917 saq.push(libDoc);
1918 //
1919 // Get <PPis>, <Protocols>, <Guids> list of this library
1920 // instance.
1921 //
1922 String[] ppiList = saq.getPpiArray(this.arch);
1923 String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);
1924 String[] protocolList = saq.getProtocolArray(this.arch);
1925 String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);
1926 String[] guidList = saq.getGuidEntryArray(this.arch);
1927 PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);
1928
1929 //
1930 // Add those ppi, protocol, guid in global ppi,
1931 // protocol, guid
1932 // list.
1933 //
1934 for (index = 0; index < ppiList.length; index++) {
1935 this.mPpiList.add(ppiList[index]);
1936 }
1937
1938 for (index = 0; index < ppiNotifyList.length; index++) {
1939 this.mPpiList.add(ppiNotifyList[index]);
1940 }
1941
1942 for (index = 0; index < protocolList.length; index++) {
1943 this.mProtocolList.add(protocolList[index]);
1944 }
1945
1946 for (index = 0; index < protocolNotifyList.length; index++) {
1947 this.mProtocolList.add(protocolNotifyList[index]);
1948 }
1949
1950 for (index = 0; index < guidList.length; index++) {
1951 this.mGuidList.add(guidList[index]);
1952 }
1953 for (index = 0; index < pkgList.length; index++) {
1954 if (!this.mDepPkgList.contains(pkgList[index])) {
1955 this.mDepPkgList.add(pkgList[index]);
1956 }
1957 }
1958
1959 //
1960 // If not yet parse this library instance's constructor
1961 // element,parse it.
1962 //
1963 libConstructName = saq.getLibConstructorName();
1964 libDestructName = saq.getLibDestructorName();
1965
1966 //
1967 // Collect SetVirtualAddressMapCallBack and
1968 // ExitBootServiceCallBack.
1969 //
1970 setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1971 exitBoots = saq.getExitBootServicesCallBackArray();
1972 if (setVirtuals != null) {
1973 for (int j = 0; j < setVirtuals.length; j++) {
1974 this.setVirtalAddList.add(setVirtuals[j]);
1975 }
1976 }
1977 if (exitBoots != null) {
1978 for (int k = 0; k < exitBoots.length; k++) {
1979 this.exitBootServiceList.add(exitBoots[k]);
1980 }
1981 }
1982 saq.pop();
1983 //
1984 // Add dependent library instance constructor function.
1985 //
1986 if (libConstructName != null) {
1987 this.libConstructList.add(libConstructName);
1988 }
1989 //
1990 // Add dependent library instance destructor fuction.
1991 //
1992 if (libDestructName != null) {
1993 this.libDestructList.add(libDestructName);
1994 }
1995 }
1996 }
1997
1998 }
1999 }
2000 private void setVirtualAddressToAutogenC(StringBuffer fileBuffer){
2001 //
2002 // Entry point lib for these module types needs to know the count
2003 // of entryPoint.
2004 //
2005 fileBuffer
2006 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverSetVirtualAddressMapEventCount = ");
2007
2008 //
2009 // If the list is not valid or has no entries set count to zero else
2010 // set count to the number of valid entries
2011 //
2012 int Count = 0;
2013 int i = 0;
2014 if (this.setVirtalAddList != null) {
2015 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2016 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2017 break;
2018 }
2019 }
2020 Count = i;
2021 }
2022
2023 fileBuffer.append(Integer.toString(Count));
2024 fileBuffer.append(";\r\n\r\n");
2025 if (this.setVirtalAddList == null || this.setVirtalAddList.size() == 0) {
2026 //
2027 // No data so make a NULL list
2028 //
2029 fileBuffer
2030 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {\r\n");
2031 fileBuffer.append(" NULL\r\n");
2032 fileBuffer.append("};\r\n\r\n");
2033 } else {
2034 //
2035 // Write SetVirtualAddressMap function definition.
2036 //
2037 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2038 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2039 break;
2040 }
2041 fileBuffer.append("VOID\r\n");
2042 fileBuffer.append("EFIAPI\r\n");
2043 fileBuffer.append(this.setVirtalAddList.get(i));
2044 fileBuffer.append(" (\r\n");
2045 fileBuffer.append(" IN EFI_EVENT Event,\r\n");
2046 fileBuffer.append(" IN VOID *Context\r\n");
2047 fileBuffer.append(" );\r\n\r\n");
2048 }
2049
2050 //
2051 // Write SetVirtualAddressMap entry point array.
2052 //
2053 fileBuffer
2054 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {");
2055 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2056 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2057 break;
2058 }
2059
2060 if (i == 0) {
2061 fileBuffer.append("\r\n ");
2062 } else {
2063 fileBuffer.append(",\r\n ");
2064 }
2065
2066 fileBuffer.append(this.setVirtalAddList.get(i));
2067 }
2068 //
2069 // add the NULL at the end of _gDriverSetVirtualAddressMapEvent list.
2070 //
2071 fileBuffer.append(",\r\n NULL");
2072 fileBuffer.append("\r\n};\r\n\r\n");
2073 }
2074 }
2075
2076
2077 private void setExitBootServiceToAutogenC(StringBuffer fileBuffer){
2078 //
2079 // Entry point lib for these module types needs to know the count.
2080 //
2081 fileBuffer
2082 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverExitBootServicesEventCount = ");
2083
2084 //
2085 // If the list is not valid or has no entries set count to zero else
2086 // set count to the number of valid entries.
2087 //
2088 int Count = 0;
2089 int i = 0;
2090 if (this.exitBootServiceList != null) {
2091 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2092 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2093 break;
2094 }
2095 }
2096 Count = i;
2097 }
2098 fileBuffer.append(Integer.toString(Count));
2099 fileBuffer.append(";\r\n\r\n");
2100
2101 if (this.exitBootServiceList == null || this.exitBootServiceList.size() == 0) {
2102 //
2103 // No data so make a NULL list.
2104 //
2105 fileBuffer
2106 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {\r\n");
2107 fileBuffer.append(" NULL\r\n");
2108 fileBuffer.append("};\r\n\r\n");
2109 } else {
2110 //
2111 // Write DriverExitBootServices function definition.
2112 //
2113 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2114 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2115 break;
2116 }
2117
2118 fileBuffer.append("VOID\r\n");
2119 fileBuffer.append("EFIAPI\r\n");
2120 fileBuffer.append(this.exitBootServiceList.get(i));
2121 fileBuffer.append(" (\r\n");
2122 fileBuffer.append(" IN EFI_EVENT Event,\r\n");
2123 fileBuffer.append(" IN VOID *Context\r\n");
2124 fileBuffer.append(" );\r\n\r\n");
2125 }
2126
2127 //
2128 // Write DriverExitBootServices entry point array.
2129 //
2130 fileBuffer
2131 .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {");
2132 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2133 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2134 break;
2135 }
2136
2137 if (i == 0) {
2138 fileBuffer.append("\r\n ");
2139 } else {
2140 fileBuffer.append(",\r\n ");
2141 }
2142 fileBuffer.append(this.exitBootServiceList.get(i));
2143 }
2144
2145 fileBuffer.append(",\r\n NULL");
2146 fileBuffer.append("\r\n};\r\n\r\n");
2147 }
2148
2149 }
2150
2151 }