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