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