]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
Restored the SetVirtualAddress and SetExitBootService related code generation for...
[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 if (CommonDefinition.getModuleType(typeStr) == CommonDefinition.ModuleTypeUefiApplication) {
1104 fileBuffer.append(" Exit (");
1105 } else {
1106 fileBuffer.append(" ExitDriver (");
1107 }
1108 fileBuffer.append(entryPointList[i]);
1109 fileBuffer.append(" (ImageHandle, SystemTable));\r\n");
1110 fileBuffer.append(" ASSERT (FALSE);\r\n");
1111 fileBuffer.append(" }\r\n");
1112 } else {
1113 break;
1114 }
1115 }
1116 fileBuffer.append(" return mDriverEntryPointStatus;\r\n");
1117 }
1118 fileBuffer.append("}\r\n\r\n");
1119
1120 if (CommonDefinition.getModuleType(typeStr) != CommonDefinition.ModuleTypeUefiApplication) {
1121 fileBuffer.append("VOID\r\n");
1122 fileBuffer.append("EFIAPI\r\n");
1123 fileBuffer.append("ExitDriver (\r\n");
1124 fileBuffer.append(" IN EFI_STATUS Status\r\n");
1125 fileBuffer.append(" )\r\n\r\n");
1126 fileBuffer.append("{\r\n");
1127 if (entryPointCount <= 1) {
1128 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1129 fileBuffer.append(" ProcessLibraryDestructorList (gImageHandle, gST);\r\n");
1130 fileBuffer.append(" }\r\n");
1131 fileBuffer.append(" gBS->Exit (gImageHandle, Status, 0, NULL);\r\n");
1132 } else {
1133 fileBuffer.append(" if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");
1134 fileBuffer.append(" mDriverEntryPointStatus = Status;\r\n");
1135 fileBuffer.append(" }\r\n");
1136 fileBuffer.append(" LongJump (&mJumpContext, (UINTN)-1);\r\n");
1137 fileBuffer.append(" ASSERT (FALSE);\r\n");
1138 }
1139 fileBuffer.append("}\r\n\r\n");
1140 }
1141 }
1142
1143 if (CommonDefinition.getModuleType(typeStr) == CommonDefinition.ModuleTypeUefiApplication) {
1144 break;
1145 }
1146 //
1147 // Add ModuleUnloadImage for DxeDriver and UefiDriver module type.
1148 //
1149 //entryPointList = SurfaceAreaQuery.getModuleUnloadImageArray();
1150 //
1151 // Remover duplicate unload entry point.
1152 //
1153 //entryPointList = CommonDefinition.remDupString(entryPointList);
1154 //entryPointCount = 0;
1155 unloadImageCount = 0;
1156 if (unloadImageList != null) {
1157 for (int i = 0; i < unloadImageList.length; i++) {
1158 functionDeclarations.append("EFI_STATUS\r\n");
1159 functionDeclarations.append("EFIAPI\r\n");
1160 functionDeclarations.append(unloadImageList[i]);
1161 functionDeclarations.append(" (\r\n");
1162 functionDeclarations.append(" IN EFI_HANDLE ImageHandle\r\n");
1163 functionDeclarations.append(" );\r\n");
1164 unloadImageCount++;
1165 }
1166 }
1167
1168 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverUnloadImageCount = ");
1169 fileBuffer.append(Integer.toString(unloadImageCount));
1170 fileBuffer.append(";\r\n\r\n");
1171
1172 fileBuffer.append("EFI_STATUS\r\n");
1173 fileBuffer.append("EFIAPI\r\n");
1174 fileBuffer.append("ProcessModuleUnloadList (\r\n");
1175 fileBuffer.append(" IN EFI_HANDLE ImageHandle\r\n");
1176 fileBuffer.append(" )\r\n");
1177 fileBuffer.append("{\r\n");
1178
1179 if (unloadImageCount == 0) {
1180 fileBuffer.append(" return EFI_SUCCESS;\r\n");
1181 } else if (unloadImageCount == 1) {
1182 fileBuffer.append(" return ");
1183 fileBuffer.append(unloadImageList[0]);
1184 fileBuffer.append("(ImageHandle);\r\n");
1185 } else {
1186 fileBuffer.append(" EFI_STATUS Status;\r\n\r\n");
1187 fileBuffer.append(" Status = EFI_SUCCESS;\r\n\r\n");
1188 for (int i = 0; i < unloadImageList.length; i++) {
1189 if (i == 0) {
1190 fileBuffer.append(" Status = ");
1191 fileBuffer.append(unloadImageList[i]);
1192 fileBuffer.append("(ImageHandle);\r\n");
1193 } else {
1194 fileBuffer.append(" if (EFI_ERROR (Status)) {\r\n");
1195 fileBuffer.append(" ");
1196 fileBuffer.append(unloadImageList[i]);
1197 fileBuffer.append("(ImageHandle);\r\n");
1198 fileBuffer.append(" } else {\r\n");
1199 fileBuffer.append(" Status = ");
1200 fileBuffer.append(unloadImageList[i]);
1201 fileBuffer.append("(ImageHandle);\r\n");
1202 fileBuffer.append(" }\r\n");
1203 }
1204 }
1205 fileBuffer.append(" return Status;\r\n");
1206 }
1207 fileBuffer.append("}\r\n\r\n");
1208 break;
1209 }
1210 }
1211
1212 /**
1213 CNameToAutogenc
1214
1215 This function gets GUIDs from SPD file accrodeing to <Protocols> <Ppis>
1216 <Guids> information and write those GUIDs to AutoGen.c.
1217
1218 @param fileBuffer
1219 String Buffer for Autogen.c file.
1220
1221 **/
1222 void CNameToAutogenC(StringBuffer fileBuffer) throws AutoGenException {
1223 String[] cNameGuid = null;
1224 String guidKeyWord = null;
1225
1226 String[] cnameList = saq.getCNameArray(this.arch);
1227 for (int i = 0; i < cnameList.length; i++) {
1228 this.mGuidCNameList.add(cnameList[i]);
1229 }
1230
1231
1232 Iterator guidIterator = this.mGuidCNameList.iterator();
1233 while (guidIterator.hasNext()) {
1234 guidKeyWord = guidIterator.next().toString();
1235 cNameGuid = GlobalData.getGuid(this.mDepPkgList, guidKeyWord);
1236 if (cNameGuid == null) {
1237 cNameGuid = GlobalData.getProtocolGuid(this.mDepPkgList, guidKeyWord);
1238 if (cNameGuid == null) {
1239 cNameGuid = GlobalData.getPpiGuid(this.mDepPkgList, guidKeyWord);
1240 if (cNameGuid == null) {
1241 //
1242 // If can't find GUID declaration in every package, stop the build
1243 //
1244 EdkLog.log(EdkLog.EDK_INFO,"WARN: Can not find Guid [" + guidKeyWord + "] declaration in any SPD file.");
1245 continue;
1246 //throw new AutoGenException("Can not find Guid [" + guidKeyWord
1247 // + "] declaration in any SPD package. ");
1248 }
1249 }
1250 }
1251
1252 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");
1253 fileBuffer.append(cNameGuid[0]);
1254 fileBuffer.append(" = { ");
1255 fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));
1256 fileBuffer.append("} ;");
1257 }
1258 }
1259
1260 /**
1261 LibInstanceToAutogenC
1262
1263 This function adds dependent library instance to autogen.c,which
1264 includeing library's constructor, destructor, and library dependent ppi,
1265 protocol, guid, pcd information.
1266
1267 @param fileBuffer
1268 String buffer for AutoGen.c
1269 @throws BuildException
1270 **/
1271 void LibInstanceToAutogenC(StringBuffer fileBuffer) throws EdkException {
1272 String moduleType = this.moduleId.getModuleType();
1273 //
1274 // Add library constructor to AutoGen.c
1275 //
1276 LibConstructorToAutogenC(libConstructList, moduleType, fileBuffer);
1277 //
1278 // Add library destructor to AutoGen.c
1279 //
1280 LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer);
1281 }
1282
1283 /**
1284 LibConstructorToAutogenH
1285
1286 This function writes library constructor declarations AutoGen.h. The library
1287 constructor's parameter and return value depend on module type.
1288
1289 @param libInstanceList
1290 List of library construct name.
1291 @param moduleType
1292 Module type.
1293 @param fileBuffer
1294 String buffer for AutoGen.c
1295 @throws Exception
1296 **/
1297 void LibConstructorToAutogenH(String moduleType) throws EdkException {
1298 boolean isFirst = true;
1299
1300 //
1301 // If not yet parse this library instance's constructor
1302 // element,parse it.
1303 //
1304 String libConstructName = saq.getLibConstructorName();
1305 if (libConstructName == null) {
1306 return;
1307 }
1308
1309 //
1310 // The library constructor's parameter and return value depend on
1311 // module type.
1312 //
1313 if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1314 functionDeclarations.append("RETURN_STATUS\r\n");
1315 functionDeclarations.append("EFIAPI\r\n");
1316 functionDeclarations.append(libConstructName);
1317 functionDeclarations.append(" (\r\n");
1318 functionDeclarations.append(" VOID\r\n");
1319 functionDeclarations.append(" );\r\n");
1320 } else {
1321 switch (CommonDefinition.getModuleType(moduleType)) {
1322 case CommonDefinition.ModuleTypeBase:
1323 functionDeclarations.append("RETURN_STATUS\r\n");
1324 functionDeclarations.append("EFIAPI\r\n");
1325 functionDeclarations.append(libConstructName);
1326 functionDeclarations.append(" (\r\n");
1327 functionDeclarations.append(" VOID\r\n");
1328 functionDeclarations.append(" );\r\n");
1329 break;
1330
1331 case CommonDefinition.ModuleTypePeiCore:
1332 case CommonDefinition.ModuleTypePeim:
1333 functionDeclarations.append("EFI_STATUS\r\n");
1334 functionDeclarations.append("EFIAPI\r\n");
1335 functionDeclarations.append(libConstructName);
1336 functionDeclarations.append(" (\r\n");
1337 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1338 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1339 functionDeclarations.append(" );\r\n");
1340 break;
1341
1342 case CommonDefinition.ModuleTypeDxeCore:
1343 case CommonDefinition.ModuleTypeDxeDriver:
1344 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1345 case CommonDefinition.ModuleTypeDxeSmmDriver:
1346 case CommonDefinition.ModuleTypeDxeSalDriver:
1347 case CommonDefinition.ModuleTypeUefiDriver:
1348 case CommonDefinition.ModuleTypeUefiApplication:
1349 functionDeclarations.append("EFI_STATUS\r\n");
1350 functionDeclarations.append("EFIAPI\r\n");
1351 functionDeclarations.append(libConstructName);
1352 functionDeclarations.append(" (\r\n");
1353 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1354 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1355 functionDeclarations.append(" );\r\n");
1356 break;
1357
1358 }
1359 }
1360 }
1361
1362 /**
1363 LibDestructorToAutogenH
1364
1365 This function writes library destructor declarations AutoGen.h. The library
1366 destructor's parameter and return value depend on module type.
1367
1368 @param libInstanceList
1369 List of library destructor name.
1370 @param moduleType
1371 Module type.
1372 @param fileBuffer
1373 String buffer for AutoGen.c
1374 @throws Exception
1375 **/
1376 void LibDestructorToAutogenH(String moduleType) throws EdkException {
1377 boolean isFirst = true;
1378 String libDestructName = saq.getLibDestructorName();
1379 if (libDestructName == null) {
1380 return;
1381 }
1382
1383 if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1384 functionDeclarations.append("RETURN_STATUS\r\n");
1385 functionDeclarations.append("EFIAPI\r\n");
1386 functionDeclarations.append(libDestructName);
1387 functionDeclarations.append(" (\r\n");
1388 functionDeclarations.append(" VOID\r\n");
1389 functionDeclarations.append(" );\r\n");
1390 } else {
1391 switch (CommonDefinition.getModuleType(moduleType)) {
1392 case CommonDefinition.ModuleTypeBase:
1393 functionDeclarations.append("RETURN_STATUS\r\n");
1394 functionDeclarations.append("EFIAPI\r\n");
1395 functionDeclarations.append(libDestructName);
1396 functionDeclarations.append(" (\r\n");
1397 functionDeclarations.append(" VOID\r\n");
1398 functionDeclarations.append(" );\r\n");
1399 break;
1400 case CommonDefinition.ModuleTypePeiCore:
1401 case CommonDefinition.ModuleTypePeim:
1402 functionDeclarations.append("EFI_STATUS\r\n");
1403 functionDeclarations.append("EFIAPI\r\n");
1404 functionDeclarations.append(libDestructName);
1405 functionDeclarations.append(" (\r\n");
1406 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1407 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1408 functionDeclarations.append(" );\r\n");
1409 break;
1410 case CommonDefinition.ModuleTypeDxeCore:
1411 case CommonDefinition.ModuleTypeDxeDriver:
1412 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1413 case CommonDefinition.ModuleTypeDxeSmmDriver:
1414 case CommonDefinition.ModuleTypeDxeSalDriver:
1415 case CommonDefinition.ModuleTypeUefiDriver:
1416 case CommonDefinition.ModuleTypeUefiApplication:
1417 functionDeclarations.append("EFI_STATUS\r\n");
1418 functionDeclarations.append("EFIAPI\r\n");
1419 functionDeclarations.append(libDestructName);
1420 functionDeclarations.append(" (\r\n");
1421 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1422 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1423 functionDeclarations.append(" );\r\n");
1424 break;
1425 }
1426 }
1427 }
1428
1429 /**
1430 LibConstructorToAutogenc
1431
1432 This function writes library constructor list to AutoGen.c. The library
1433 constructor's parameter and return value depend on module type.
1434
1435 @param libInstanceList
1436 List of library construct name.
1437 @param moduleType
1438 Module type.
1439 @param fileBuffer
1440 String buffer for AutoGen.c
1441 @throws Exception
1442 **/
1443 void LibConstructorToAutogenC(List<String[]> libInstanceList,
1444 String moduleType, StringBuffer fileBuffer) throws EdkException {
1445 boolean isFirst = true;
1446
1447 //
1448 // The library constructor's parameter and return value depend on
1449 // module type.
1450 //
1451 for (int i = 0; i < libInstanceList.size(); i++) {
1452 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1453 functionDeclarations.append("RETURN_STATUS\r\n");
1454 functionDeclarations.append("EFIAPI\r\n");
1455 functionDeclarations.append(libInstanceList.get(i)[0]);
1456 functionDeclarations.append(" (\r\n");
1457 functionDeclarations.append(" VOID\r\n");
1458 functionDeclarations.append(" );\r\n");
1459 } else {
1460 switch (CommonDefinition.getModuleType(moduleType)) {
1461 case CommonDefinition.ModuleTypeBase:
1462 functionDeclarations.append("RETURN_STATUS\r\n");
1463 functionDeclarations.append("EFIAPI\r\n");
1464 functionDeclarations.append(libInstanceList.get(i)[0]);
1465 functionDeclarations.append(" (\r\n");
1466 functionDeclarations.append(" VOID\r\n");
1467 functionDeclarations.append(" );\r\n");
1468 break;
1469
1470 case CommonDefinition.ModuleTypePeiCore:
1471 case CommonDefinition.ModuleTypePeim:
1472 functionDeclarations.append("EFI_STATUS\r\n");
1473 functionDeclarations.append("EFIAPI\r\n");
1474 functionDeclarations.append(libInstanceList.get(i)[0]);
1475 functionDeclarations.append(" (\r\n");
1476 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1477 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1478 functionDeclarations.append(" );\r\n");
1479 break;
1480
1481 case CommonDefinition.ModuleTypeDxeCore:
1482 case CommonDefinition.ModuleTypeDxeDriver:
1483 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1484 case CommonDefinition.ModuleTypeDxeSmmDriver:
1485 case CommonDefinition.ModuleTypeDxeSalDriver:
1486 case CommonDefinition.ModuleTypeUefiDriver:
1487 case CommonDefinition.ModuleTypeUefiApplication:
1488 functionDeclarations.append("EFI_STATUS\r\n");
1489 functionDeclarations.append("EFIAPI\r\n");
1490 functionDeclarations.append(libInstanceList.get(i)[0]);
1491 functionDeclarations.append(" (\r\n");
1492 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1493 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1494 functionDeclarations.append(" );\r\n");
1495 break;
1496
1497 }
1498 }
1499 }
1500
1501 //
1502 // Add ProcessLibraryConstructorList in AutoGen.c
1503 //
1504 fileBuffer.append("VOID\r\n");
1505 fileBuffer.append("EFIAPI\r\n");
1506 fileBuffer.append("ProcessLibraryConstructorList (\r\n");
1507 switch (CommonDefinition.getModuleType(moduleType)) {
1508 case CommonDefinition.ModuleTypeBase:
1509 fileBuffer.append(" VOID\r\n");
1510 break;
1511
1512 case CommonDefinition.ModuleTypePeiCore:
1513 case CommonDefinition.ModuleTypePeim:
1514 fileBuffer.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1515 fileBuffer.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1516 break;
1517
1518 case CommonDefinition.ModuleTypeDxeCore:
1519 case CommonDefinition.ModuleTypeDxeDriver:
1520 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1521 case CommonDefinition.ModuleTypeDxeSmmDriver:
1522 case CommonDefinition.ModuleTypeDxeSalDriver:
1523 case CommonDefinition.ModuleTypeUefiDriver:
1524 case CommonDefinition.ModuleTypeUefiApplication:
1525 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1526 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1527 break;
1528 }
1529
1530 fileBuffer.append(" )\r\n");
1531 fileBuffer.append("{\r\n");
1532 //
1533 // If no constructor function, return EFI_SUCCESS.
1534 //
1535 for (int i = 0; i < libInstanceList.size(); i++) {
1536 if (isFirst) {
1537 fileBuffer.append(" EFI_STATUS Status;\r\n");
1538 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1539 fileBuffer.append("\r\n");
1540 isFirst = false;
1541 }
1542 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1543 fileBuffer.append(" Status = ");
1544 fileBuffer.append(libInstanceList.get(i)[0]);
1545 fileBuffer.append("();\r\n");
1546 } else {
1547 switch (CommonDefinition.getModuleType(moduleType)) {
1548 case CommonDefinition.ModuleTypeBase:
1549 fileBuffer.append(" Status = ");
1550 fileBuffer.append(libInstanceList.get(i)[0]);
1551 fileBuffer.append("();\r\n");
1552 break;
1553 case CommonDefinition.ModuleTypePeiCore:
1554 case CommonDefinition.ModuleTypePeim:
1555 fileBuffer.append(" Status = ");
1556 fileBuffer.append(libInstanceList.get(i)[0]);
1557 fileBuffer.append(" (FfsHeader, PeiServices);\r\n");
1558 break;
1559 case CommonDefinition.ModuleTypeDxeCore:
1560 case CommonDefinition.ModuleTypeDxeDriver:
1561 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1562 case CommonDefinition.ModuleTypeDxeSmmDriver:
1563 case CommonDefinition.ModuleTypeDxeSalDriver:
1564 case CommonDefinition.ModuleTypeUefiDriver:
1565 case CommonDefinition.ModuleTypeUefiApplication:
1566 fileBuffer.append(" Status = ");
1567 fileBuffer.append(libInstanceList.get(i)[0]);
1568 fileBuffer.append(" (ImageHandle, SystemTable);\r\n");
1569 break;
1570 default:
1571 EdkLog.log(EdkLog.EDK_INFO,"Autogen doesn't know how to deal with module type - " + moduleType + "!");
1572 }
1573
1574 }
1575 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1576 }
1577 fileBuffer.append("}\r\n");
1578 }
1579
1580 /**
1581 LibDestructorToAutogenc
1582
1583 This function writes library destructor list to AutoGen.c. The library
1584 destructor's parameter and return value depend on module type.
1585
1586 @param libInstanceList
1587 List of library destructor name.
1588 @param moduleType
1589 Module type.
1590 @param fileBuffer
1591 String buffer for AutoGen.c
1592 @throws Exception
1593 **/
1594 void LibDestructorToAutogenC(List<String[]> libInstanceList,
1595 String moduleType, StringBuffer fileBuffer) throws EdkException {
1596 boolean isFirst = true;
1597 for (int i = 0; i < libInstanceList.size(); i++) {
1598 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1599 functionDeclarations.append("RETURN_STATUS\r\n");
1600 functionDeclarations.append("EFIAPI\r\n");
1601 functionDeclarations.append(libInstanceList.get(i)[0]);
1602 functionDeclarations.append(" (\r\n");
1603 functionDeclarations.append(" VOID\r\n");
1604 functionDeclarations.append(" );\r\n");
1605 } else {
1606 switch (CommonDefinition.getModuleType(moduleType)) {
1607 case CommonDefinition.ModuleTypeBase:
1608 functionDeclarations.append("RETURN_STATUS\r\n");
1609 functionDeclarations.append("EFIAPI\r\n");
1610 functionDeclarations.append(libInstanceList.get(i)[0]);
1611 functionDeclarations.append(" (\r\n");
1612 functionDeclarations.append(" VOID\r\n");
1613 functionDeclarations.append(" );\r\n");
1614 break;
1615 case CommonDefinition.ModuleTypePeiCore:
1616 case CommonDefinition.ModuleTypePeim:
1617 functionDeclarations.append("EFI_STATUS\r\n");
1618 functionDeclarations.append("EFIAPI\r\n");
1619 functionDeclarations.append(libInstanceList.get(i)[0]);
1620 functionDeclarations.append(" (\r\n");
1621 functionDeclarations.append(" IN EFI_FFS_FILE_HEADER *FfsHeader,\r\n");
1622 functionDeclarations.append(" IN EFI_PEI_SERVICES **PeiServices\r\n");
1623 functionDeclarations.append(" );\r\n");
1624 break;
1625 case CommonDefinition.ModuleTypeDxeCore:
1626 case CommonDefinition.ModuleTypeDxeDriver:
1627 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1628 case CommonDefinition.ModuleTypeDxeSmmDriver:
1629 case CommonDefinition.ModuleTypeDxeSalDriver:
1630 case CommonDefinition.ModuleTypeUefiDriver:
1631 case CommonDefinition.ModuleTypeUefiApplication:
1632 functionDeclarations.append("EFI_STATUS\r\n");
1633 functionDeclarations.append("EFIAPI\r\n");
1634 functionDeclarations.append(libInstanceList.get(i)[0]);
1635 functionDeclarations.append(" (\r\n");
1636 functionDeclarations.append(" IN EFI_HANDLE ImageHandle,\r\n");
1637 functionDeclarations.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1638 functionDeclarations.append(" );\r\n");
1639 break;
1640 }
1641 }
1642 }
1643
1644 //
1645 // Write ProcessLibraryDestructor list to autogen.c
1646 //
1647 switch (CommonDefinition.getModuleType(moduleType)) {
1648 case CommonDefinition.ModuleTypeBase:
1649 case CommonDefinition.ModuleTypePeiCore:
1650 case CommonDefinition.ModuleTypePeim:
1651 break;
1652 case CommonDefinition.ModuleTypeDxeCore:
1653 case CommonDefinition.ModuleTypeDxeDriver:
1654 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1655 case CommonDefinition.ModuleTypeDxeSmmDriver:
1656 case CommonDefinition.ModuleTypeDxeSalDriver:
1657 case CommonDefinition.ModuleTypeUefiDriver:
1658 case CommonDefinition.ModuleTypeUefiApplication:
1659 fileBuffer.append("VOID\r\n");
1660 fileBuffer.append("EFIAPI\r\n");
1661 fileBuffer.append("ProcessLibraryDestructorList (\r\n");
1662 fileBuffer.append(" IN EFI_HANDLE ImageHandle,\r\n");
1663 fileBuffer.append(" IN EFI_SYSTEM_TABLE *SystemTable\r\n");
1664 fileBuffer.append(" )\r\n");
1665 fileBuffer.append("{\r\n");
1666 //
1667 // If no library destructor function, return EFI_SUCCESS.
1668 //
1669
1670 for (int i = 0; i < libInstanceList.size(); i++) {
1671 if (isFirst) {
1672 fileBuffer.append(" EFI_STATUS Status;\r\n");
1673 fileBuffer.append(" Status = EFI_SUCCESS;\r\n");
1674 fileBuffer.append("\r\n");
1675 isFirst = false;
1676 }
1677 if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {
1678 fileBuffer.append(" Status = ");
1679 fileBuffer.append(libInstanceList.get(i)[0]);
1680 fileBuffer.append("();\r\n");
1681 fileBuffer.append(" VOID\r\n");
1682 } else {
1683 fileBuffer.append(" Status = ");
1684 fileBuffer.append(libInstanceList.get(i)[0]);
1685 fileBuffer.append("(ImageHandle, SystemTable);\r\n");
1686 fileBuffer.append(" ASSERT_EFI_ERROR (Status);\r\n");
1687 }
1688 }
1689 fileBuffer.append("}\r\n");
1690 break;
1691 }
1692 }
1693
1694 /**
1695 ExternsDriverBindingToAutoGenC
1696
1697 This function is to write DRIVER_BINDING, COMPONENT_NAME,
1698 DRIVER_CONFIGURATION, DRIVER_DIAGNOSTIC in AutoGen.c.
1699
1700 @param fileBuffer
1701 String buffer for AutoGen.c
1702 **/
1703 void ExternsDriverBindingToAutoGenC(StringBuffer fileBuffer)
1704 throws EdkException {
1705 //
1706 // Get the arry of extern. The driverBindingGroup is a 2 dimension array.
1707 // The second dimension is include following element: DriverBinding,
1708 // ComponentName, DriverConfiguration, DriverDiag;
1709 //
1710 String[][] driverBindingGroup = this.saq.getExternProtocolGroup();
1711
1712
1713 //
1714 // inital BitMask;
1715 //
1716 int BitMask = 0;
1717
1718 //
1719 // Write driver binding protocol extern to autogen.c
1720 //
1721 for (int i = 0; i < driverBindingGroup.length; i++) {
1722 if (driverBindingGroup[i][0] != null) {
1723 globalDeclarations.append("extern EFI_DRIVER_BINDING_PROTOCOL ");
1724 globalDeclarations.append(driverBindingGroup[i][0]);
1725 globalDeclarations.append(";\r\n");
1726 }
1727 }
1728
1729 //
1730 // Write component name protocol extern to autogen.c
1731 //
1732 if (!componentNamePcd) {
1733 for (int i = 0; i < driverBindingGroup.length; i++) {
1734 if (driverBindingGroup[i][1]!= null) {
1735 if (driverBindingGroup[i][0] != null) {
1736 BitMask |= 0x01;
1737 globalDeclarations.append("extern EFI_COMPONENT_NAME_PROTOCOL ");
1738 globalDeclarations.append(driverBindingGroup[i][1]);
1739 globalDeclarations.append(";\r\n");
1740 } else {
1741 throw new AutoGenException("DriverBinding can't be empty!!");
1742 }
1743 }
1744 }
1745 }
1746
1747 //
1748 // Write driver configration protocol extern to autogen.c
1749 //
1750 for (int i = 0; i < driverBindingGroup.length; i++) {
1751 if (driverBindingGroup[i][2] != null) {
1752 if (driverBindingGroup[i][0] != null) {
1753 BitMask |= 0x02;
1754 globalDeclarations.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");
1755 globalDeclarations.append(driverBindingGroup[i][2]);
1756 globalDeclarations.append(";\r\n");
1757 } else {
1758 throw new AutoGenException("DriverBinding can't be empty!!");
1759 }
1760 }
1761 }
1762
1763 //
1764 // Write driver dignastic protocol extern to autogen.c
1765 //
1766 if (!driverDiagnostPcd) {
1767 for (int i = 0; i < driverBindingGroup.length; i++) {
1768 if (driverBindingGroup[i][3] != null) {
1769 if (driverBindingGroup[i][0] != null) {
1770 BitMask |= 0x04;
1771 globalDeclarations.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");
1772 globalDeclarations.append(driverBindingGroup[i][3]);
1773 globalDeclarations.append(";\r\n");
1774 } else {
1775 throw new AutoGenException("DriverBinding can't be empty!!");
1776 }
1777 }
1778 }
1779 }
1780
1781
1782 //
1783 // Write driver module protocol bitmask.
1784 //
1785 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8 _gDriverModelProtocolBitmask = ");
1786 fileBuffer.append(Integer.toString(BitMask));
1787 fileBuffer.append(";\r\n");
1788
1789 //
1790 // Write driver module protocol list entry
1791 //
1792 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverModelProtocolListEntries = ");
1793
1794 fileBuffer.append(Integer.toString(driverBindingGroup.length));
1795 fileBuffer.append(";\r\n");
1796
1797 //
1798 // Write drive module protocol list to autogen.c
1799 //
1800 if (driverBindingGroup.length > 0) {
1801 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED const EFI_DRIVER_MODEL_PROTOCOL_LIST _gDriverModelProtocolList[] = {");
1802 }
1803
1804
1805 for (int i = 0; i < driverBindingGroup.length; i++) {
1806 if (i != 0) {
1807 fileBuffer.append(",");
1808 }
1809 //
1810 // DriverBinding
1811 //
1812 fileBuffer.append("\r\n {\r\n");
1813 fileBuffer.append(" &");
1814 fileBuffer.append(driverBindingGroup[i][0]);
1815 fileBuffer.append(", \r\n");
1816
1817 //
1818 // ComponentName
1819 //
1820 if (driverBindingGroup[i][1] != null && componentNamePcd != true) {
1821 fileBuffer.append(" &");
1822 fileBuffer.append(driverBindingGroup[i][1]);
1823 fileBuffer.append(", \r\n");
1824 } else {
1825 fileBuffer.append(" NULL, \r\n");
1826 }
1827
1828 //
1829 // DriverConfiguration
1830 //
1831 if (driverBindingGroup[i][2] != null) {
1832 fileBuffer.append(" &");
1833 fileBuffer.append(driverBindingGroup[i][2]);
1834 fileBuffer.append(", \r\n");
1835 } else {
1836 fileBuffer.append(" NULL, \r\n");
1837 }
1838
1839 //
1840 // DriverDiagnostic
1841 //
1842 if (driverBindingGroup[i][3] != null && driverDiagnostPcd != true) {
1843 fileBuffer.append(" &");
1844 fileBuffer.append(driverBindingGroup[i][3]);
1845 fileBuffer.append(", \r\n");
1846 } else {
1847 fileBuffer.append(" NULL, \r\n");
1848 }
1849 fileBuffer.append(" }");
1850 }
1851
1852 if (driverBindingGroup.length > 0) {
1853 fileBuffer.append("\r\n};\r\n");
1854 }
1855 }
1856
1857 /**
1858 ExternCallBackToAutoGenC
1859
1860 This function adds <SetVirtualAddressMapCallBack> and
1861 <ExitBootServicesCallBack> infomation to AutoGen.c
1862
1863 @param fileBuffer
1864 String buffer for AutoGen.c
1865 @throws BuildException
1866 **/
1867 void ExternCallBackToAutoGenH(String moduleType)
1868 throws EdkException {
1869 //
1870 // Collect module's <SetVirtualAddressMapCallBack> and
1871 // <ExitBootServiceCallBack> and add to setVirtualAddList
1872 // exitBootServiceList.
1873 //
1874 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1875 String[] exitBoots = saq.getExitBootServicesCallBackArray();
1876 //
1877 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
1878 // and <ExitBootServicesCallBack>
1879 //
1880 switch (CommonDefinition.getModuleType(moduleType)) {
1881 case CommonDefinition.ModuleTypeDxeDriver:
1882 case CommonDefinition.ModuleTypeDxeRuntimeDriver:
1883 case CommonDefinition.ModuleTypeDxeSalDriver:
1884 case CommonDefinition.ModuleTypeUefiDriver:
1885 case CommonDefinition.ModuleTypeUefiApplication:
1886 //
1887 // Write SetVirtualAddressMap function definition.
1888 //
1889 for (int i = 0; setVirtuals != null && i < setVirtuals.length; i++) {
1890 if (setVirtuals[i].equalsIgnoreCase("")) {
1891 continue;
1892 }
1893 functionDeclarations.append("VOID\r\n");
1894 functionDeclarations.append("EFIAPI\r\n");
1895 functionDeclarations.append(setVirtuals[i]);
1896 functionDeclarations.append(" (\r\n");
1897 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
1898 functionDeclarations.append(" IN VOID *Context\r\n");
1899 functionDeclarations.append(" );\r\n\r\n");
1900 }
1901
1902 //
1903 // Write DriverExitBootServices function definition.
1904 //
1905 for (int i = 0; exitBoots != null && i < exitBoots.length; i++) {
1906 if (exitBoots[i].equalsIgnoreCase("")) {
1907 continue;
1908 }
1909
1910 functionDeclarations.append("VOID\r\n");
1911 functionDeclarations.append("EFIAPI\r\n");
1912 functionDeclarations.append(exitBoots[i]);
1913 functionDeclarations.append(" (\r\n");
1914 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
1915 functionDeclarations.append(" IN VOID *Context\r\n");
1916 functionDeclarations.append(" );\r\n\r\n");
1917 }
1918 break;
1919 default:
1920 break;
1921 }
1922 }
1923
1924 /**
1925 ExternCallBackToAutoGenC
1926
1927 This function adds <SetVirtualAddressMapCallBack> and
1928 <ExitBootServicesCallBack> infomation to AutoGen.c
1929
1930 @param fileBuffer
1931 String buffer for AutoGen.c
1932 @throws BuildException
1933 **/
1934 void ExternCallBackToAutoGenC(StringBuffer fileBuffer)
1935 throws EdkException {
1936 //
1937 // Collect module's <SetVirtualAddressMapCallBack> and
1938 // <ExitBootServiceCallBack> and add to setVirtualAddList
1939 // exitBootServiceList.
1940 //
1941 String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
1942 String[] exitBoots = saq.getExitBootServicesCallBackArray();
1943 if (setVirtuals != null) {
1944 for (int j = 0; j < setVirtuals.length; j++) {
1945 this.setVirtalAddList.add(setVirtuals[j]);
1946 }
1947 }
1948 if (exitBoots != null) {
1949 for (int k = 0; k < exitBoots.length; k++) {
1950 this.exitBootServiceList.add(exitBoots[k]);
1951 }
1952 }
1953 //
1954 // Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>
1955 // and <ExitBootServicesCallBack>
1956 //
1957 String moduleType = this.moduleId.getModuleType();
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 // If moduleType is one of above, call setVirtualAddressToAutogenC,
1966 // and setExitBootServiceToAutogenC.
1967 //
1968 setVirtualAddressToAutogenC(fileBuffer);
1969 setExitBootServiceToAutogenC(fileBuffer);
1970 break;
1971 default:
1972 break;
1973 }
1974 }
1975
1976 /**
1977 copyFlashMapHToDebugDir
1978
1979 This function is to copy the falshmap.h to debug directory and change
1980 its name to TianoR8FlashMap.h
1981
1982 @param
1983 @return
1984 **/
1985 private void copyFlashMapHToDebugDir() throws AutoGenException{
1986
1987 File inFile = new File(fvDir + File.separatorChar + CommonDefinition.FLASHMAPH);
1988 int size = (int)inFile.length();
1989 byte[] buffer = new byte[size];
1990 File outFile = new File (this.outputPath + File.separatorChar + CommonDefinition.TIANOR8PLASHMAPH);
1991 //
1992 // If TianoR8FlashMap.h existed and the flashMap.h don't change,
1993 // do nothing.
1994 //
1995 if ((!outFile.exists()) ||(inFile.lastModified() - outFile.lastModified()) >= 0) {
1996 if (inFile.exists()) {
1997 try {
1998 FileInputStream fis = new FileInputStream (inFile);
1999 fis.read(buffer);
2000 FileOutputStream fos = new FileOutputStream(outFile);
2001 fos.write(buffer);
2002 fis.close();
2003 fos.close();
2004 } catch (IOException e) {
2005 throw new AutoGenException("The file, flashMap.h can't be open!");
2006 }
2007
2008 } else {
2009 throw new AutoGenException("The file, flashMap.h doesn't exist!");
2010 }
2011 }
2012 }
2013
2014 /**
2015 This function first order the library instances, then collect
2016 library instance 's PPI, Protocol, GUID,
2017 SetVirtalAddressMapCallBack, ExitBootServiceCallBack, and
2018 Destructor, Constructor.
2019
2020 @param
2021 @return
2022 **/
2023 private void collectLibInstanceInfo() throws EdkException{
2024 int index;
2025
2026 String moduleType = moduleId.getModuleType();
2027 String libConstructName = null;
2028 String libDestructName = null;
2029 String libModuleType = null;
2030 String[] setVirtuals = null;
2031 String[] exitBoots = null;
2032
2033 ModuleIdentification[] libraryIdList = saq.getLibraryInstance(this.arch);
2034 if (libraryIdList.length <= 0) {
2035 return;
2036 }
2037 //
2038 // Reorder library instance sequence.
2039 //
2040 AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList, this.arch);
2041 List<ModuleIdentification> orderList = libOrder.orderLibInstance();
2042 //
2043 // Process library instance one by one.
2044 //
2045 for (int i = 0; i < orderList.size(); i++) {
2046 //
2047 // Get library instance basename.
2048 //
2049 ModuleIdentification libInstanceId = orderList.get(i);
2050
2051 //
2052 // Get override map
2053 //
2054
2055 Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);
2056 saq.push(libDoc);
2057 //
2058 // check if the library instance support current module
2059 //
2060 String[] libraryClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED,
2061 this.arch,
2062 moduleType
2063 );
2064 if (libraryClassList.length <= 0) {
2065 throw new EdkException("Library instance " + libInstanceId.getName()
2066 + " doesn't support module type " + moduleType);
2067 }
2068 //
2069 // Get CName list from <PPis>, <Protocols>, <Guids>, etc. of this library
2070 // instance.
2071 //
2072 String[] guidCNameList = saq.getCNameArray(this.arch);
2073 PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);
2074
2075 //
2076 // Add those ppi, protocol, guid in global ppi,
2077 // protocol, guid
2078 // list.
2079 //
2080 for (index = 0; index < guidCNameList.length; index++) {
2081 this.mGuidCNameList.add(guidCNameList[index]);
2082 }
2083
2084 for (index = 0; index < pkgList.length; index++) {
2085 if (!this.mDepPkgList.contains(pkgList[index])) {
2086 this.mDepPkgList.add(pkgList[index]);
2087 }
2088 }
2089
2090 //
2091 // If not yet parse this library instance's constructor
2092 // element,parse it.
2093 //
2094 libConstructName = saq.getLibConstructorName();
2095 libDestructName = saq.getLibDestructorName();
2096 libModuleType = saq.getModuleType();
2097
2098 //
2099 // Collect SetVirtualAddressMapCallBack and
2100 // ExitBootServiceCallBack.
2101 //
2102 setVirtuals = saq.getSetVirtualAddressMapCallBackArray();
2103 exitBoots = saq.getExitBootServicesCallBackArray();
2104 if (setVirtuals != null) {
2105 for (int j = 0; j < setVirtuals.length; j++) {
2106 this.setVirtalAddList.add(setVirtuals[j]);
2107 }
2108 }
2109 if (exitBoots != null) {
2110 for (int k = 0; k < exitBoots.length; k++) {
2111 this.exitBootServiceList.add(exitBoots[k]);
2112 }
2113 }
2114 saq.pop();
2115 //
2116 // Add dependent library instance constructor function.
2117 //
2118 if (libConstructName != null) {
2119 this.libConstructList.add(new String[] {libConstructName, libModuleType});
2120 }
2121 //
2122 // Add dependent library instance destructor fuction.
2123 //
2124 if (libDestructName != null) {
2125 this.libDestructList.add(new String[] {libDestructName, libModuleType});
2126 }
2127 }
2128 }
2129
2130 private void setVirtualAddressToAutogenC(StringBuffer fileBuffer){
2131 //
2132 // Entry point lib for these module types needs to know the count
2133 // of entryPoint.
2134 //
2135 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverSetVirtualAddressMapEventCount = ");
2136
2137 //
2138 // If the list is not valid or has no entries set count to zero else
2139 // set count to the number of valid entries
2140 //
2141 int Count = 0;
2142 int i = 0;
2143 if (this.setVirtalAddList != null) {
2144 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2145 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2146 break;
2147 }
2148 }
2149 Count = i;
2150 }
2151
2152 fileBuffer.append(Integer.toString(Count));
2153 fileBuffer.append(";\r\n\r\n");
2154 if (this.setVirtalAddList == null || this.setVirtalAddList.size() == 0) {
2155 //
2156 // No data so make a NULL list
2157 //
2158 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {\r\n");
2159 fileBuffer.append(" NULL\r\n");
2160 fileBuffer.append("};\r\n\r\n");
2161 } else {
2162 //
2163 // Write SetVirtualAddressMap function definition.
2164 //
2165 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2166 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2167 break;
2168 }
2169 functionDeclarations.append("VOID\r\n");
2170 functionDeclarations.append("EFIAPI\r\n");
2171 functionDeclarations.append(this.setVirtalAddList.get(i));
2172 functionDeclarations.append(" (\r\n");
2173 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
2174 functionDeclarations.append(" IN VOID *Context\r\n");
2175 functionDeclarations.append(" );\r\n\r\n");
2176 }
2177
2178 //
2179 // Write SetVirtualAddressMap entry point array.
2180 //
2181 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {");
2182 for (i = 0; i < this.setVirtalAddList.size(); i++) {
2183 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {
2184 break;
2185 }
2186
2187 if (i == 0) {
2188 fileBuffer.append("\r\n ");
2189 } else {
2190 fileBuffer.append(",\r\n ");
2191 }
2192
2193 fileBuffer.append(this.setVirtalAddList.get(i));
2194 }
2195 //
2196 // add the NULL at the end of _gDriverSetVirtualAddressMapEvent list.
2197 //
2198 fileBuffer.append(",\r\n NULL");
2199 fileBuffer.append("\r\n};\r\n\r\n");
2200 }
2201 }
2202
2203
2204 private void setExitBootServiceToAutogenC(StringBuffer fileBuffer){
2205 //
2206 // Entry point lib for these module types needs to know the count.
2207 //
2208 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const UINTN _gDriverExitBootServicesEventCount = ");
2209
2210 //
2211 // If the list is not valid or has no entries set count to zero else
2212 // set count to the number of valid entries.
2213 //
2214 int Count = 0;
2215 int i = 0;
2216 if (this.exitBootServiceList != null) {
2217 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2218 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2219 break;
2220 }
2221 }
2222 Count = i;
2223 }
2224 fileBuffer.append(Integer.toString(Count));
2225 fileBuffer.append(";\r\n\r\n");
2226
2227 if (this.exitBootServiceList == null || this.exitBootServiceList.size() == 0) {
2228 //
2229 // No data so make a NULL list.
2230 //
2231 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {\r\n");
2232 fileBuffer.append(" NULL\r\n");
2233 fileBuffer.append("};\r\n\r\n");
2234 } else {
2235 //
2236 // Write DriverExitBootServices function definition.
2237 //
2238 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2239 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2240 break;
2241 }
2242
2243 functionDeclarations.append("VOID\r\n");
2244 functionDeclarations.append("EFIAPI\r\n");
2245 functionDeclarations.append(this.exitBootServiceList.get(i));
2246 functionDeclarations.append(" (\r\n");
2247 functionDeclarations.append(" IN EFI_EVENT Event,\r\n");
2248 functionDeclarations.append(" IN VOID *Context\r\n");
2249 functionDeclarations.append(" );\r\n\r\n");
2250 }
2251
2252 //
2253 // Write DriverExitBootServices entry point array.
2254 //
2255 fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {");
2256 for (i = 0; i < this.exitBootServiceList.size(); i++) {
2257 if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {
2258 break;
2259 }
2260
2261 if (i == 0) {
2262 fileBuffer.append("\r\n ");
2263 } else {
2264 fileBuffer.append(",\r\n ");
2265 }
2266 fileBuffer.append(this.exitBootServiceList.get(i));
2267 }
2268
2269 fileBuffer.append(",\r\n NULL");
2270 fileBuffer.append("\r\n};\r\n\r\n");
2271 }
2272 }
2273 /**
2274 setPcdComponentName
2275
2276 Get the Pcd Value of ComponentName to
2277 decide whether need to disable the componentName.
2278
2279 **/
2280 public void setPcdComponentName (){
2281 String pcdValue = null;
2282 pcdValue = saq.getPcdValueBycName("PcdComponentNameDisable");
2283 if (pcdValue != null && pcdValue.equalsIgnoreCase("true")) {
2284 this.componentNamePcd = true;
2285 }
2286 }
2287
2288 /**
2289 setPcdDriverDiagnostic
2290
2291 Get the Pcd Value of DriverDiagnostic to
2292 decide whether need to disable DriverDiagnostic.
2293
2294 **/
2295 public void setPcdDriverDiagnostic (){
2296 String pcdValue = null;
2297 pcdValue = saq.getPcdValueBycName("PcdDriverDiagnosticsDisable");
2298 if (pcdValue != null && pcdValue.equalsIgnoreCase("true")) {
2299 this.driverDiagnostPcd = true;
2300 }
2301 }
2302
2303 }