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