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