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