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