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