]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
Fixed grammar in messages.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / autogen / AutoGen.java
index 7de80e6c72ea4d1ed98727b6af70b2ebe7ccc73e..0704bf44119b1bec734773634e8d9b1f923f1037 100644 (file)
 \r
 package org.tianocore.build.autogen;\r
 \r
-import org.tianocore.build.global.GlobalData;\r
-import org.tianocore.build.global.Spd;\r
-import org.tianocore.build.global.SurfaceAreaQuery;\r
-import org.tianocore.build.id.ModuleIdentification;\r
-import org.tianocore.build.id.PackageIdentification;\r
-import org.tianocore.GuidsDocument;\r
-import org.tianocore.LibraryClassDocument.LibraryClass;\r
-import org.tianocore.PPIsDocument;\r
-import org.tianocore.ProtocolsDocument;\r
-import org.tianocore.build.pcd.action.PCDAutoGenAction;\r
-import org.tianocore.build.exception.*;\r
-import org.tianocore.logger.EdkLog;\r
-import org.apache.tools.ant.BuildException;\r
-import org.apache.xmlbeans.XmlObject;\r
-\r
 import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
 import java.io.FileReader;\r
 import java.io.FileWriter;\r
 import java.util.ArrayList;\r
 import java.util.HashSet;\r
 import java.util.Iterator;\r
+import java.util.LinkedHashSet;\r
 import java.util.LinkedList;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.Set;\r
 \r
+import org.apache.tools.ant.BuildException;\r
+import org.apache.xmlbeans.XmlObject;\r
+import org.tianocore.GuidsDocument;\r
+import org.tianocore.LibraryClassDocument.LibraryClass;\r
+import org.tianocore.PPIsDocument;\r
+import org.tianocore.ProtocolsDocument;\r
+import org.tianocore.build.exception.*;\r
+import org.tianocore.build.global.GlobalData;\r
+import org.tianocore.build.global.Spd;\r
+import org.tianocore.build.global.SurfaceAreaQuery;\r
+import org.tianocore.build.id.ModuleIdentification;\r
+import org.tianocore.build.id.PackageIdentification;\r
+import org.tianocore.build.pcd.action.PCDAutoGenAction;\r
+import org.tianocore.logger.EdkLog;\r
+\r
 /**\r
  * This class is to generate Autogen.h and Autogen.c according to module surface\r
  * area or library surface area.\r
@@ -52,7 +55,10 @@ public class AutoGen {
        // / The output path of Autogen.h and Autogen.c\r
        // /\r
        private String outputPath;\r
-\r
+    /// \r
+    /// The name of FV directory \r
+    /// \r
+    private String fvDir;\r
        // /\r
        // / The base name of module or library.\r
        // /\r
@@ -92,6 +98,21 @@ public class AutoGen {
     // area and it's dependence on library instance surface are.\r
     //\r
     private List<PackageIdentification> mDepPkgList = new LinkedList<PackageIdentification>();\r
+\r
+       //\r
+       //  For non library module, add its library instance's construct and destructor to \r
+       //  list.\r
+       //  \r
+       private List<String> libConstructList = new ArrayList<String>();\r
+       private List<String> libDestructList = new ArrayList<String>();\r
+\r
+       //\r
+       // List to store SetVirtalAddressMapCallBack, ExitBootServiceCallBack  \r
+       // \r
+       private List<String> setVirtalAddList = new ArrayList<String>();\r
+       private List<String> exitBootServiceList = new ArrayList<String>();\r
+\r
+       \r
        /**\r
         * Construct function\r
         * \r
@@ -104,10 +125,11 @@ public class AutoGen {
         * @param arch\r
         *            Target architecture.\r
         */\r
-       public AutoGen(String outputPath, ModuleIdentification moduleId, String arch) {\r
+       public AutoGen(String fvDir, String outputPath, ModuleIdentification moduleId, String arch) {\r
                this.outputPath = outputPath;\r
                this.moduleId = moduleId;\r
                this.arch = arch;\r
+        this.fvDir = fvDir;\r
 \r
        }\r
 \r
@@ -197,6 +219,7 @@ public class AutoGen {
        void moduleGenAutogen() throws BuildException {\r
 \r
                try {\r
+                       collectLibInstanceInfo();\r
                        moduleGenAutogenC();\r
                        moduleGenAutogenH();\r
                } catch (Exception e) {\r
@@ -220,7 +243,7 @@ public class AutoGen {
                        libGenAutogenH();\r
                } catch (Exception e) {\r
                        throw new BuildException(\r
-                                       "Faile to create library AutoGen.c & AutoGen.h!\n"\r
+                                       "Failed to create library AutoGen.c & AutoGen.h!\n"\r
                                                        + e.getMessage());\r
                }\r
        }\r
@@ -251,10 +274,8 @@ public class AutoGen {
                // Add #ifndef ${BaseName}_AUTOGENH\r
                // #def ${BseeName}_AUTOGENH\r
                //\r
-               fileBuffer.append("#ifndef    " + this.moduleId.getName().toUpperCase()\r
-                               + "_AUTOGENH\r\n");\r
-               fileBuffer.append("#define    " + this.moduleId.getName().toUpperCase()\r
-                               + "_AUTOGENH\r\n\r\n");\r
+               fileBuffer.append("#ifndef    " + "_AUTOGENH_" + this.moduleId.getGuid().replaceAll("-", "_") +"\r\n");\r
+               fileBuffer.append("#define    " + "_AUTOGENH_" + this.moduleId.getGuid().replaceAll("-", "_") +"\r\n\r\n");\r
 \r
                //\r
                // Write the specification version and release version at the begine\r
@@ -264,29 +285,6 @@ public class AutoGen {
                // moduleType.\r
                //\r
                moduleType = SurfaceAreaQuery.getModuleType();\r
-//             switch (CommonDefinition.getModuleType(moduleType)) {\r
-//             case CommonDefinition.ModuleTypeDxeCore:\r
-//             case CommonDefinition.ModuleTypeDxeDriver:\r
-//             case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
-//             case CommonDefinition.ModuleTypeDxeSmmDriver:\r
-//             case CommonDefinition.ModuleTypeDxeSalDriver:\r
-//             case CommonDefinition.ModuleTypeUefiDriver:\r
-//             case CommonDefinition.ModuleTypeUefiApplication:\r
-//                     fileBuffer.append(CommonDefinition.autoGenHLine1);\r
-//                     break;\r
-//             default:\r
-//                     fileBuffer.append(CommonDefinition.autoGenHVersionDefault);\r
-//                     break;\r
-//             }\r
-//             switch (CommonDefinition.getModuleType(moduleType)) {\r
-//             case CommonDefinition.ModuleTypeUefiDriver:\r
-//             case CommonDefinition.ModuleTypeUefiApplication:\r
-//                     fileBuffer.append(CommonDefinition.autoGenHReleaseDefault);\r
-//                     break;\r
-//             default:\r
-//                     fileBuffer.append(CommonDefinition.autoGenHLine2);\r
-//                     break;\r
-//             }\r
 \r
                //\r
                // Add "extern int __make_me_compile_correctly;" at begin of\r
@@ -308,7 +306,7 @@ public class AutoGen {
                // PackageIdentification[] consumedPkgIdList = SurfaceAreaQuery\r
                // .getDependencePkg(this.arch);\r
                PackageIdentification[] consumedPkgIdList = SurfaceAreaQuery\r
-                               .getDependencePkg(null);\r
+                               .getDependencePkg(this.arch);\r
                if (consumedPkgIdList != null) {\r
                        headerFileList = depPkgToAutogenH(consumedPkgIdList, moduleType);\r
                        item = headerFileList.iterator();\r
@@ -321,7 +319,7 @@ public class AutoGen {
                // Write library class's related *.h file to autogen.h.\r
                //\r
                String[] libClassList = SurfaceAreaQuery\r
-                               .getLibraryClasses(CommonDefinition.AlwaysConsumed);\r
+                               .getLibraryClasses(CommonDefinition.AlwaysConsumed,this.arch);\r
                if (libClassList != null) {\r
                        libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
                        item = libClassIncludeH.iterator();\r
@@ -331,7 +329,7 @@ public class AutoGen {
                }\r
 \r
                libClassList = SurfaceAreaQuery\r
-                               .getLibraryClasses(CommonDefinition.AlwaysProduced);\r
+                               .getLibraryClasses(CommonDefinition.AlwaysProduced, this.arch);\r
                if (libClassList != null) {\r
                        libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
                        item = libClassIncludeH.iterator();\r
@@ -341,7 +339,17 @@ public class AutoGen {
                }\r
                fileBuffer.append("\r\n");\r
 \r
-               //\r
+        //\r
+        //  If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to \r
+        // {DEST_DIR_DRBUG}/FlashMap.h\r
+        // \r
+        if (SurfaceAreaQuery.isHaveTianoR8FlashMap()) {\r
+            fileBuffer.append(CommonDefinition.include);\r
+            fileBuffer.append("  <");\r
+            fileBuffer.append(CommonDefinition.tianoR8FlashMapH + ">\r\n");\r
+            copyFlashMapHToDebugDir();\r
+        }\r
+\r
                // Write PCD autogen information to AutoGen.h.\r
                //\r
                if (this.myPcdAutogen != null) {\r
@@ -464,25 +472,18 @@ public class AutoGen {
                GuidGuidToAutogenC(fileBuffer);\r
 \r
                //\r
-               // Call pcd autogen. PCDAutoGenAction tool only need module name and\r
-               // isPcdEmulatedDriver as parameter. Library inherits PCD and module's\r
-               // PCD information has been collected in FPDParser task by\r
-               // CollectPCDAction.\r
-               // Note : when PCD image tool ready,\r
-               // isPCDEmulatedDriver parameter will be removed.\r
-               //\r
-                try {\r
-//              this.myPcdAutogen = new PCDAutoGenAction(moduleId.getName(),\r
-//              moduleId.getGuid(), moduleId.getPackage().getName(), moduleId.getPackage().getGuid(),this.arch,moduleId.getVersion(),false, null);\r
-         this.myPcdAutogen = new PCDAutoGenAction(moduleId.getName(),null,null,null, this.arch,null,false, null);\r
-                this.myPcdAutogen.execute();\r
-                } catch (Exception e) {\r
-                throw new BuildException("PCD Autogen failed:" + e.getMessage());\r
-                }\r
+               // Call pcd autogen. \r
+               //\r
+                this.myPcdAutogen = new PCDAutoGenAction(moduleId, this.arch, false, null);\r
+                try {\r
+                    this.myPcdAutogen.execute();\r
+                } catch (Exception exp) {\r
+                    throw new PcdAutogenException (exp.getMessage());\r
+                }\r
                \r
                if (this.myPcdAutogen != null) {\r
-            fileBuffer.append("\r\n");\r
-                       fileBuffer.append(this.myPcdAutogen.OutputC());\r
+                    fileBuffer.append("\r\n");\r
+                    fileBuffer.append(this.myPcdAutogen.OutputC());\r
                }\r
 \r
                if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {\r
@@ -516,10 +517,8 @@ public class AutoGen {
                // Add #ifndef ${BaseName}_AUTOGENH\r
                // #def ${BseeName}_AUTOGENH\r
                //\r
-               fileBuffer.append("#ifndef    " + this.moduleId.getName().toUpperCase()\r
-                               + "_AUTOGENH\r\n");\r
-               fileBuffer.append("#define    " + this.moduleId.getName().toUpperCase()\r
-                               + "_AUTOGENH\r\n\r\n");\r
+               fileBuffer.append("#ifndef    " + "_AUTOGENH_" + this.moduleId.getGuid().replaceAll("-", "_") + "\r\n");\r
+               fileBuffer.append("#define    " + "_AUTOGENH_" + this.moduleId.getGuid().replaceAll("-", "_") + "\r\n\r\n");\r
 \r
                //\r
                // Write EFI_SPECIFICATION_VERSION and EDK_RELEASE_VERSION\r
@@ -551,7 +550,7 @@ public class AutoGen {
                // Write library class's related *.h file to autogen.h\r
                //\r
                String[] libClassList = SurfaceAreaQuery\r
-                               .getLibraryClasses(CommonDefinition.AlwaysConsumed);\r
+                               .getLibraryClasses(CommonDefinition.AlwaysConsumed, this.arch);\r
                if (libClassList != null) {\r
                        libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
                        item = libClassIncludeH.iterator();\r
@@ -561,7 +560,7 @@ public class AutoGen {
                }\r
 \r
                libClassList = SurfaceAreaQuery\r
-                               .getLibraryClasses(CommonDefinition.AlwaysProduced);\r
+                               .getLibraryClasses(CommonDefinition.AlwaysProduced, this.arch);\r
                if (libClassList != null) {\r
                        libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
                        item = libClassIncludeH.iterator();\r
@@ -571,6 +570,17 @@ public class AutoGen {
                }\r
                fileBuffer.append("\r\n");\r
 \r
+        //\r
+        //  If is TianoR8FlashMap, copy {Fv_DIR}/FlashMap.h to \r
+        // {DEST_DIR_DRBUG}/FlashMap.h\r
+        // \r
+        if (SurfaceAreaQuery.isHaveTianoR8FlashMap()) {\r
+            fileBuffer.append(CommonDefinition.include);\r
+            fileBuffer.append("  <");\r
+            fileBuffer.append(CommonDefinition.tianoR8FlashMapH + ">\r\n");\r
+            copyFlashMapHToDebugDir();\r
+        }\r
+\r
                //\r
                // Write PCD information to library AutoGen.h.\r
                //\r
@@ -600,7 +610,7 @@ public class AutoGen {
         * @throws BuildException\r
         *             Failed to generate AutoGen.c.\r
         */\r
-       void libGenAutogenC() throws BuildException {\r
+       void libGenAutogenC() throws BuildException, PcdAutogenException {\r
                StringBuffer fileBuffer = new StringBuffer(10240);\r
 \r
                //\r
@@ -612,33 +622,21 @@ public class AutoGen {
                fileBuffer.append("\r\n");\r
 \r
                //\r
-               // Call pcd autogen. PCDAutoGenAction tool only need module name and\r
-               // isPcdEmulatedDriver as parameter. Library inherit PCD and module's\r
-               // PCD information has been collected in FPDParser task by\r
-               // CollectPCDAction.\r
-               // Note : when PCD image tool ready,\r
-               // isPCDEmulatedDriver parameter will be removed.\r
+               // Call pcd autogen. \r
                //\r
+                this.myPcdAutogen = new PCDAutoGenAction(this.moduleId,\r
+                                                         this.arch,\r
+                                                         true, \r
+                                                         SurfaceAreaQuery.getModulePcdEntryNameArray());\r
                try {\r
-//                      this.myPcdAutogen = new PCDAutoGenAction(this.moduleId.getName(),\r
-//                      this.moduleId.getGuid(),moduleId.getPackage().getName(),moduleId.getPackage().getGuid(), this.arch, moduleId.getVersion(),true, SurfaceAreaQuery.getModulePcdEntryNameArray());\r
-            this.myPcdAutogen = new PCDAutoGenAction(this.moduleId.getName(),\r
-                                                     null,\r
-                                                     null,\r
-                                                     null,\r
-                                                     this.arch,\r
-                                                     null,\r
-                                                     true, \r
-                                                     SurfaceAreaQuery.getModulePcdEntryNameArray());\r
-                     \r
-                       this.myPcdAutogen.execute();\r
+                    this.myPcdAutogen.execute();\r
                } catch (Exception e) {\r
-                       throw new BuildException(e.getMessage());\r
+                    throw new PcdAutogenException(e.getMessage());\r
                }\r
 \r
                if (this.myPcdAutogen != null) {\r
-            fileBuffer.append("\r\n");\r
-                       fileBuffer.append(this.myPcdAutogen.OutputC());\r
+                    fileBuffer.append("\r\n");\r
+                    fileBuffer.append(this.myPcdAutogen.OutputC());\r
                }\r
 \r
                if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {\r
@@ -659,7 +657,7 @@ public class AutoGen {
         */\r
        Set<String> LibraryClassToAutogenH(String[] libClassList)\r
                        throws AutoGenException {\r
-               Set<String> includStrList = new HashSet<String>();\r
+               Set<String> includStrList = new LinkedHashSet<String>();\r
                String includerName[];\r
                String str = "";\r
 \r
@@ -674,7 +672,7 @@ public class AutoGen {
                                        libClassList[i]);\r
                        if (includerName == null) {\r
                                throw new AutoGenException("Can not find library class ["\r
-                                               + libClassList[i] + "] declaration in every packages. ");\r
+                                               + libClassList[i] + "] declaration in any SPD package. ");\r
                        }\r
                        for (int j = 0; j < includerName.length; j++) {\r
                                String includeNameStr = includerName[j];\r
@@ -716,7 +714,7 @@ public class AutoGen {
                        if (pkgHeader == null) {\r
                                throw new AutoGenException("Can not find package ["\r
                                                + packageNameList[i]\r
-                                               + "] declaration in every packages. ");\r
+                                               + "] declaration in any SPD package. ");\r
                        } else if (!pkgHeader.equalsIgnoreCase("")) {\r
                                includeStr = CommonDefinition.include + " <" + pkgHeader\r
                                                + ">\r\n";\r
@@ -753,7 +751,7 @@ public class AutoGen {
                case CommonDefinition.ModuleTypePeiCore:\r
                        if (entryPointList == null ||entryPointList.length != 1 ) {\r
                                throw new BuildException(\r
-                                               "Module type = 'PEI_CORE', only have one module entry point!");\r
+                                               "Module type = 'PEI_CORE', can have only one module entry point!");\r
                        } else {\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
@@ -785,7 +783,7 @@ public class AutoGen {
                        fileBuffer.append("const UINT32 _gUefiDriverRevision = 0;\r\n");\r
                        if (entryPointList == null || entryPointList.length != 1) {\r
                                throw new BuildException(\r
-                                               "Module type = 'DXE_CORE', only have one module entry point!");\r
+                                               "Module type = 'DXE_CORE', can have only one module entry point!");\r
                        } else {\r
 \r
                                fileBuffer.append("VOID\r\n");\r
@@ -812,7 +810,7 @@ public class AutoGen {
                        int entryPointCount = 0;\r
                        fileBuffer\r
                                        .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT32 _gPeimRevision = 0;\r\n");\r
-                       if (entryPointList == null) {\r
+                       if (entryPointList == null || entryPointList.length == 0) {\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append("ProcessModuleEntryPointList (\r\n");\r
@@ -882,35 +880,26 @@ public class AutoGen {
                                .append("GLOBAL_REMOVE_IF_UNREFERENCED  const UINT8  _gDriverEntryPointCount = ");\r
                        fileBuffer.append(Integer.toString(entryPointCount));\r
                        fileBuffer.append(";\r\n");\r
-                       fileBuffer\r
-                               .append("static BASE_LIBRARY_JUMP_BUFFER  mJumpContext;\r\n");\r
-                               fileBuffer\r
-                                               .append("static EFI_STATUS  mDriverEntryPointStatus = EFI_LOAD_ERROR;\r\n\r\n");\r
-               \r
-                               fileBuffer.append("EFI_STATUS\r\n");\r
+                fileBuffer.append("EFI_STATUS\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append("ProcessModuleEntryPointList (\r\n");\r
-                               fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                               fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                               fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                               fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
                                fileBuffer.append("  )\r\n\r\n");\r
                                fileBuffer.append("{\r\n");\r
                                fileBuffer.append("  return EFI_SUCCESS;\r\n");\r
                                fileBuffer.append("}\r\n\r\n");\r
-                               break;\r
+\r
                        } else {\r
                 for (int i = 0; i < entryPointList.length; i++) {\r
-                    if (!entryPointList[i].equals("")) {\r
-                        fileBuffer.append("EFI_STATUS\r\n");\r
-                        fileBuffer.append("EFIAPI\r\n");\r
-                        fileBuffer.append(entryPointList[i]);\r
-                        fileBuffer.append(" (\r\n");\r
-                        fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                        fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
-                        fileBuffer.append("  );\r\n");\r
-                        entryPointCount++;\r
-                    } else {\r
-                        break;\r
-                    }\r
+                    fileBuffer.append("EFI_STATUS\r\n");\r
+                    fileBuffer.append("EFIAPI\r\n");\r
+                    fileBuffer.append(entryPointList[i]);\r
+                    fileBuffer.append(" (\r\n");\r
+                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    fileBuffer.append("  );\r\n");\r
+                    entryPointCount++;\r
                 }\r
                 fileBuffer\r
                         .append("GLOBAL_REMOVE_IF_UNREFERENCED  const UINT8  _gDriverEntryPointCount = ");\r
@@ -924,8 +913,8 @@ public class AutoGen {
                 fileBuffer.append("EFI_STATUS\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
                 fileBuffer.append("ProcessModuleEntryPointList (\r\n");\r
-                fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
 \r
@@ -967,31 +956,28 @@ public class AutoGen {
             entryPointList = CommonDefinition.remDupString(entryPointList);\r
                        entryPointCount = 0;\r
 \r
-                       fileBuffer\r
-                                       .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8  _gDriverUnloadImageCount = ");\r
-                       fileBuffer.append(Integer.toString(entryPointCount));\r
-                       fileBuffer.append(";\r\n\r\n");\r
-\r
                        if (entryPointList != null) {\r
                                for (int i = 0; i < entryPointList.length; i++) {\r
-                                       if (!entryPointList[i].equals("")) {\r
-                                               fileBuffer.append("EFI_STATUS\r\n");\r
-                                               fileBuffer.append("EFIAPI\r\n");\r
-                                               fileBuffer.append(entryPointList[i]);\r
-                                               fileBuffer.append(" (\r\n");\r
-                                               fileBuffer\r
-                                                               .append("  EFI_HANDLE        ImageHandle\r\n");\r
-                                               fileBuffer.append("  );\r\n");\r
-                                       } else {\r
-                                               break;\r
-                                       }\r
-                               }\r
+                    fileBuffer.append("EFI_STATUS\r\n");\r
+                    fileBuffer.append("EFIAPI\r\n");\r
+                    fileBuffer.append(entryPointList[i]);\r
+                    fileBuffer.append(" (\r\n");\r
+                    fileBuffer\r
+                            .append("  IN EFI_HANDLE        ImageHandle\r\n");\r
+                    fileBuffer.append("  );\r\n");\r
+                    entryPointCount++;\r
+                }\r
                        }\r
 \r
+            fileBuffer\r
+                                       .append("GLOBAL_REMOVE_IF_UNREFERENCED const UINT8  _gDriverUnloadImageCount = ");\r
+                       fileBuffer.append(Integer.toString(entryPointCount));\r
+                       fileBuffer.append(";\r\n\r\n");\r
+\r
                        fileBuffer.append("EFI_STATUS\r\n");\r
                        fileBuffer.append("EFIAPI\r\n");\r
                        fileBuffer.append("ProcessModuleUnloadList (\r\n");\r
-                       fileBuffer.append("  EFI_HANDLE  ImageHandle\r\n");\r
+                       fileBuffer.append("  IN EFI_HANDLE  ImageHandle\r\n");\r
                        fileBuffer.append("  )\r\n");\r
                        fileBuffer.append("{\r\n");\r
 \r
@@ -1042,8 +1028,8 @@ public class AutoGen {
                                fileBuffer.append("EFI_STATUS\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append("ProcessModuleEntryPointList (\r\n");\r
-                               fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                               fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                               fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                               fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
                                fileBuffer.append("  )\r\n\r\n");\r
                                fileBuffer.append("{\r\n");\r
                                fileBuffer.append("  return EFI_SUCCESS;\r\n");\r
@@ -1056,8 +1042,8 @@ public class AutoGen {
                     fileBuffer.append("EFIAPI\r\n");\r
                     fileBuffer.append(entryPointList[i]);\r
                     fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                    fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
                     fileBuffer.append("  );\r\n");\r
                     entryPointCount++;\r
                 }\r
@@ -1077,8 +1063,8 @@ public class AutoGen {
                 fileBuffer.append("EFI_STATUS\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
                 fileBuffer.append("ProcessModuleEntryPointList (\r\n");\r
-                fileBuffer.append("  EFI_HANDLE        ImageHandle,\r\n");\r
-                fileBuffer.append("  EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
 \r
@@ -1104,10 +1090,10 @@ public class AutoGen {
                 }\r
                 fileBuffer.append("}\r\n\r\n");\r
 \r
-                fileBuffer.append("VOID\n");\r
-                fileBuffer.append("EFIAPI\n");\r
+                fileBuffer.append("VOID\r\n");\r
+                fileBuffer.append("EFIAPI\r\n");\r
                 fileBuffer.append("ExitDriver (\r\n");\r
-                fileBuffer.append("  IN EFI_STATUS  Status\n");\r
+                fileBuffer.append("  IN EFI_STATUS  Status\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
                 if (entryPointCount <= 1) {\r
@@ -1140,18 +1126,14 @@ public class AutoGen {
                        entryPointCount = 0;\r
                        if (entryPointList != null) {\r
                                for (int i = 0; i < entryPointList.length; i++) {\r
-                                       if (!entryPointList[i].equals("")) {\r
-                                               fileBuffer.append("EFI_STATUS\r\n");\r
-                                               fileBuffer.append("EFIAPI\r\n");\r
-                                               fileBuffer.append(entryPointList[i]);\r
-                                               fileBuffer.append(" (\r\n");\r
-                                               fileBuffer\r
-                                                               .append("  EFI_HANDLE        ImageHandle\r\n");\r
-                                               fileBuffer.append("  );\r\n");\r
-                                               entryPointCount++;\r
-                                       } else {\r
-                                               break;\r
-                                       }\r
+                    fileBuffer.append("EFI_STATUS\r\n");\r
+                    fileBuffer.append("EFIAPI\r\n");\r
+                    fileBuffer.append(entryPointList[i]);\r
+                    fileBuffer.append(" (\r\n");\r
+                    fileBuffer\r
+                            .append("  IN EFI_HANDLE        ImageHandle\r\n");\r
+                    fileBuffer.append("  );\r\n");\r
+                    entryPointCount++;\r
                                }\r
                        }\r
 \r
@@ -1160,26 +1142,10 @@ public class AutoGen {
                        fileBuffer.append(Integer.toString(entryPointCount));\r
                        fileBuffer.append(";\r\n\r\n");\r
 \r
-                       if (entryPointList != null) {\r
-                               for (int i = 0; i < entryPointList.length; i++) {\r
-                                       if (!entryPointList[i].equals("")) {\r
-                                               fileBuffer.append("EFI_STATUS\r\n");\r
-                                               fileBuffer.append("EFIAPI\r\n");\r
-                                               fileBuffer.append(entryPointList[i]);\r
-                                               fileBuffer.append(" (\r\n");\r
-                                               fileBuffer\r
-                                                               .append("  EFI_HANDLE        ImageHandle\r\n");\r
-                                               fileBuffer.append("  );\r\n");\r
-                                       } else {\r
-                                               break;\r
-                                       }\r
-                               }\r
-                       }\r
-\r
                        fileBuffer.append("EFI_STATUS\n");\r
                        fileBuffer.append("EFIAPI\r\n");\r
                        fileBuffer.append("ProcessModuleUnloadList (\r\n");\r
-                       fileBuffer.append("  EFI_HANDLE  ImageHandle\r\n");\r
+                       fileBuffer.append("  IN EFI_HANDLE  ImageHandle\r\n");\r
                        fileBuffer.append("  )\r\n");\r
                        fileBuffer.append("{\r\n");\r
 \r
@@ -1193,8 +1159,12 @@ public class AutoGen {
                                fileBuffer.append("  EFI_STATUS  Status;\r\n\r\n");\r
                                fileBuffer.append("  Status = EFI_SUCCESS;\r\n\r\n");\r
                                for (int i = 0; i < entryPointList.length; i++) {\r
-                                       if (!entryPointList[i].equals("")) {\r
-                                               fileBuffer.append("  if (EFI_ERROR (Status)) {\r\n");\r
+                    if (i == 0) {\r
+                        fileBuffer.append("  Status = ");\r
+                        fileBuffer.append(entryPointList[i]);\r
+                        fileBuffer.append("(ImageHandle);\r\n");\r
+                    }else{\r
+                        fileBuffer.append("  if (EFI_ERROR (Status)) {\r\n");\r
                                                fileBuffer.append("    ");\r
                                                fileBuffer.append(entryPointList[i]);\r
                                                fileBuffer.append("(ImageHandle);\r\n");\r
@@ -1203,9 +1173,7 @@ public class AutoGen {
                                                fileBuffer.append(entryPointList[i]);\r
                                                fileBuffer.append("(ImageHandle);\r\n");\r
                                                fileBuffer.append("  }\r\n");\r
-                                       } else {\r
-                                               break;\r
-                                       }\r
+                    }\r
                                }\r
                                fileBuffer.append("  return Status;\r\n");\r
                        }\r
@@ -1263,7 +1231,7 @@ public class AutoGen {
                                // If can't find Ppi GUID declaration in every package\r
                                //\r
                                throw new AutoGenException("Can not find Ppi GUID ["\r
-                                               + ppiKeyWord + "] declaration in every packages. ");\r
+                                               + ppiKeyWord + "] declaration in any SPD package!");\r
                        }\r
                }\r
        }\r
@@ -1320,7 +1288,7 @@ public class AutoGen {
                                // If can't find protocol GUID declaration in every package\r
                                //\r
                                throw new BuildException("Can not find protocol Guid ["\r
-                                               + protocolKeyWord + "] declaration in every packages. ");\r
+                                               + protocolKeyWord + "] declaration in any SPD package!");\r
                        }\r
                }\r
        }\r
@@ -1363,7 +1331,7 @@ public class AutoGen {
                                // If can't find GUID declaration in every package\r
                                //\r
                                throw new AutoGenException("Can not find Guid [" + guidKeyWord\r
-                                               + "] declaration in every packages. ");\r
+                                               + "] declaration in any SPD package. ");\r
                        }\r
 \r
                }\r
@@ -1381,128 +1349,17 @@ public class AutoGen {
         * @throws BuildException\r
         */\r
        void LibInstanceToAutogenC(StringBuffer fileBuffer) throws BuildException {\r
-               int index;\r
-\r
-               String moduleType = SurfaceAreaQuery.getModuleType();\r
-               List<String> libConstructList = new ArrayList<String>();\r
-               List<String> libDestructList = new ArrayList<String>();\r
-\r
-               String libConstructName = null;\r
-               String libDestructName = null;\r
-               ModuleIdentification[] libraryIdList = SurfaceAreaQuery\r
-                               .getLibraryInstance(this.arch);\r
-\r
                try {\r
-                       if (libraryIdList != null) {\r
-                               //\r
-                               // Reorder library instance sequence.\r
-                               //\r
-                               AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList,\r
-                                               this.arch);\r
-                               List<ModuleIdentification> orderList = libOrder\r
-                                               .orderLibInstance();\r
-\r
-                               if (orderList != null) {\r
-                                       //\r
-                                       // Process library instance one by one.\r
-                                       //\r
-                                       for (int i = 0; i < orderList.size(); i++) {\r
-\r
-                                               //\r
-                                               // Get library instance basename.\r
-                                               //\r
-                                               ModuleIdentification libInstanceId = orderList.get(i);\r
-\r
-                                               //\r
-                                               // Get override map\r
-                                               //\r
-                                                \r
-                                               Map<String, XmlObject> libDoc = GlobalData.getDoc(\r
-                                                               libInstanceId, this.arch);\r
-                                               SurfaceAreaQuery.push(libDoc);\r
-\r
-                                               //\r
-                                               // Get <PPis>, <Protocols>, <Guids> list of this library\r
-                                               // instance.\r
-                                               //\r
-                                               String[] ppiList = SurfaceAreaQuery.getPpiArray(this.arch);\r
-                                               String[] ppiNotifyList = SurfaceAreaQuery\r
-                                                               .getPpiNotifyArray(this.arch);\r
-                                               String[] protocolList = SurfaceAreaQuery\r
-                                                               .getProtocolArray(this.arch);\r
-                                               String[] protocolNotifyList = SurfaceAreaQuery\r
-                                                               .getProtocolNotifyArray(this.arch);\r
-                                               String[] guidList = SurfaceAreaQuery\r
-                                                               .getGuidEntryArray(this.arch);\r
-                        PackageIdentification[] pkgList = SurfaceAreaQuery.getDependencePkg(this.arch);\r
-\r
-                                               //\r
-                                               // Add those ppi, protocol, guid in global ppi,\r
-                                               // protocol, guid\r
-                                               // list.\r
-                                               //\r
-                                               for (index = 0; index < ppiList.length; index++) {\r
-                                                       this.mPpiList.add(ppiList[index]);\r
-                                               }\r
-\r
-                                               for (index = 0; index < ppiNotifyList.length; index++) {\r
-                                                       this.mPpiList.add(ppiNotifyList[index]);\r
-                                               }\r
-\r
-                                               for (index = 0; index < protocolList.length; index++) {\r
-                                                       this.mProtocolList.add(protocolList[index]);\r
-                                               }\r
-\r
-                                               for (index = 0; index < protocolNotifyList.length; index++) {\r
-                                                       this.mProtocolList.add(protocolNotifyList[index]);\r
-                                               }\r
-\r
-                                               for (index = 0; index < guidList.length; index++) {\r
-                                                       this.mGuidList.add(guidList[index]);\r
-                                               }\r
-                        for (index = 0; index < pkgList.length; index++){\r
-                            if (!this.mDepPkgList.contains(pkgList[index])){\r
-                                this.mDepPkgList.add(pkgList[index]);\r
-                            }\r
-                        }\r
-\r
-                                               //\r
-                                               // If not yet parse this library instance's constructor\r
-                                               // element,parse it.\r
-                                               //\r
-                                               libConstructName = SurfaceAreaQuery\r
-                                                               .getLibConstructorName();\r
-                                               libDestructName = SurfaceAreaQuery\r
-                                                               .getLibDestructorName();\r
-\r
-                                               SurfaceAreaQuery.pop();\r
-                                               //\r
-                                               // Add dependent library instance constructor function.\r
-                                               //\r
-                                               if (libConstructName != null) {\r
-                                                       libConstructList.add(libConstructName);\r
-                                               }\r
-                                               //\r
-                                               // Add dependent library instance destructor fuction.\r
-                                               //\r
-                                               if (libDestructName != null) {\r
-                                                       libDestructList.add(libDestructName);\r
-                                               }\r
-                                       }\r
-\r
-                               }\r
-\r
-                               //\r
-                               // Add library constructor to AutoGen.c\r
-                               //\r
-                               LibConstructorToAutogenC(libConstructList, moduleType,\r
-                                               fileBuffer/* autogenC */);\r
-                               //\r
-                               // Add library destructor to AutoGen.c\r
-                               //\r
-                               LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer/* autogenC */);\r
-                       }\r
-\r
+            String moduleType = this.moduleId.getModuleType();\r
+                   //\r
+                       // Add library constructor to AutoGen.c\r
+                       //\r
+                       LibConstructorToAutogenC(libConstructList, moduleType,\r
+                               fileBuffer/* autogenC */);\r
+                       //\r
+                       // Add library destructor to AutoGen.c\r
+                       //\r
+                       LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer/* autogenC */);\r
                } catch (Exception e) {\r
                        throw new BuildException(e.getMessage());\r
                }\r
@@ -1534,6 +1391,7 @@ public class AutoGen {
                        switch (CommonDefinition.getModuleType(moduleType)) {\r
                        case CommonDefinition.ModuleTypeBase:\r
                                fileBuffer.append("RETURN_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  VOID\r\n");\r
@@ -1543,6 +1401,7 @@ public class AutoGen {
                        case CommonDefinition.ModuleTypePeiCore:\r
                        case CommonDefinition.ModuleTypePeim:\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer\r
@@ -1560,6 +1419,7 @@ public class AutoGen {
                        case CommonDefinition.ModuleTypeUefiDriver:\r
                        case CommonDefinition.ModuleTypeUefiApplication:\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
@@ -1573,6 +1433,7 @@ public class AutoGen {
                // Add ProcessLibraryConstructorList in AutoGen.c\r
                //\r
                fileBuffer.append("VOID\r\n");\r
+               fileBuffer.append("EFIAPI\r\n");\r
                fileBuffer.append("ProcessLibraryConstructorList (\r\n");\r
                switch (CommonDefinition.getModuleType(moduleType)) {\r
                case CommonDefinition.ModuleTypeBase:\r
@@ -1638,7 +1499,7 @@ public class AutoGen {
                                fileBuffer.append(" (ImageHandle, SystemTable);\r\n");\r
                                break;\r
             default:\r
-                EdkLog.log(EdkLog.EDK_INFO,"Autogen don't know how to deal with module type -"+ moduleType + " !");\r
+                EdkLog.log(EdkLog.EDK_INFO,"Autogen doesn't know how to deal with module type - " + moduleType + "!");\r
                        }\r
                        fileBuffer.append("  ASSERT_EFI_ERROR (Status);\r\n");\r
                }\r
@@ -1665,7 +1526,8 @@ public class AutoGen {
                for (int i = 0; i < libInstanceList.size(); i++) {\r
                        switch (CommonDefinition.getModuleType(moduleType)) {\r
                        case CommonDefinition.ModuleTypeBase:\r
-                               fileBuffer.append("RETURN_STATUS\n");\r
+                               fileBuffer.append("RETURN_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  VOID\r\n");\r
@@ -1674,6 +1536,7 @@ public class AutoGen {
                        case CommonDefinition.ModuleTypePeiCore:\r
                        case CommonDefinition.ModuleTypePeim:\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer\r
@@ -1690,6 +1553,7 @@ public class AutoGen {
                        case CommonDefinition.ModuleTypeUefiDriver:\r
                        case CommonDefinition.ModuleTypeUefiApplication:\r
                                fileBuffer.append("EFI_STATUS\r\n");\r
+                               fileBuffer.append("EFIAPI\r\n");\r
                                fileBuffer.append(libInstanceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
@@ -1715,6 +1579,7 @@ public class AutoGen {
                case CommonDefinition.ModuleTypeUefiDriver:\r
                case CommonDefinition.ModuleTypeUefiApplication:\r
                        fileBuffer.append("VOID\r\n");\r
+                       fileBuffer.append("EFIAPI\r\n");\r
                        fileBuffer.append("ProcessLibraryDestructorList (\r\n");\r
                        fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
                        fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
@@ -1825,7 +1690,7 @@ public class AutoGen {
                if (compDiagList != null && compDiagList.length != 0) {\r
                        if (drvBindList.length != compDiagList.length) {\r
                                throw new BuildException(\r
-                                               "Different number of Driver Binding and Driver Configuration protocols!");\r
+                                               "Different number of Driver Binding and Driver Diagnosis protocols!");\r
                        }\r
 \r
                        BitMask |= 0x04;\r
@@ -1907,15 +1772,31 @@ public class AutoGen {
         */\r
        void ExternCallBackToAutoGenC(StringBuffer fileBuffer)\r
                        throws BuildException {\r
-               String[] setVirtualList = SurfaceAreaQuery\r
-                               .getSetVirtualAddressMapCallBackArray();\r
-               String[] exitBootList = SurfaceAreaQuery\r
-                               .getExitBootServicesCallBackArray();\r
-               String moduleType = SurfaceAreaQuery.getModuleType();\r
+               //\r
+               // Collect module's <SetVirtualAddressMapCallBack> and \r
+               // <ExitBootServiceCallBack> and add to setVirtualAddList \r
+               //  exitBootServiceList.\r
+               // \r
+        String[] setVirtuals = SurfaceAreaQuery.getSetVirtualAddressMapCallBackArray();\r
+        String[] exitBoots = SurfaceAreaQuery.getExitBootServicesCallBackArray();\r
+        if (setVirtuals != null) {\r
+            for (int j = 0; j < setVirtuals.length; j++) {\r
+                           this.setVirtalAddList.add(setVirtuals[j]);\r
+                       }\r
+               }\r
+               if (exitBoots != null) {\r
+                   for (int k = 0; k < exitBoots.length; k++) {\r
+                           this.exitBootServiceList.add(exitBoots[k]);\r
+                       }\r
+               }\r
+               //\r
+               //  Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>\r
+               //  and <ExitBootServicesCallBack> \r
+               // \r
+               String moduleType = this.moduleId.getModuleType();\r
                boolean UefiOrDxeModule = false;\r
                int Count = 0;\r
                int i;\r
-\r
                switch (CommonDefinition.getModuleType(moduleType)) {\r
                case CommonDefinition.ModuleTypeDxeDriver:\r
                case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
@@ -1935,9 +1816,9 @@ public class AutoGen {
                        // set count to the number of valid entries\r
                        //\r
                        Count = 0;\r
-                       if (setVirtualList != null) {\r
-                               for (i = 0; i < setVirtualList.length; i++) {\r
-                                       if (setVirtualList[i].equalsIgnoreCase("")) {\r
+                       if (this.setVirtalAddList != null) {\r
+                               for (i = 0; i < this.setVirtalAddList.size(); i++) {\r
+                                       if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {\r
                                                break;\r
                                        }\r
                                }\r
@@ -1951,7 +1832,7 @@ public class AutoGen {
                        break;\r
                }\r
 \r
-               if (setVirtualList == null) {\r
+               if (this.setVirtalAddList == null || this.setVirtalAddList.size() == 0) {\r
                        if (UefiOrDxeModule) {\r
                                //\r
                                // No data so make a NULL list\r
@@ -1965,13 +1846,13 @@ public class AutoGen {
                        //\r
                        // Write SetVirtualAddressMap function definition.\r
                        //\r
-                       for (i = 0; i < setVirtualList.length; i++) {\r
-                               if (setVirtualList[i].equalsIgnoreCase("")) {\r
+                       for (i = 0; i < this.setVirtalAddList.size(); i++) {\r
+                               if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {\r
                                        break;\r
                                }\r
                                fileBuffer.append("VOID\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
-                               fileBuffer.append(setVirtualList[i]);\r
+                               fileBuffer.append(this.setVirtalAddList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  IN EFI_EVENT  Event,\r\n");\r
                                fileBuffer.append("  IN VOID       *Context\r\n");\r
@@ -1983,8 +1864,8 @@ public class AutoGen {
                        //\r
                        fileBuffer\r
                                        .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverSetVirtualAddressMapEvent[] = {");\r
-                       for (i = 0; i < setVirtualList.length; i++) {\r
-                               if (setVirtualList[i].equalsIgnoreCase("")) {\r
+                       for (i = 0; i < this.setVirtalAddList.size(); i++) {\r
+                               if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {\r
                                        break;\r
                                }\r
 \r
@@ -1994,7 +1875,7 @@ public class AutoGen {
                                        fileBuffer.append(",\r\n  ");\r
                                }\r
 \r
-                               fileBuffer.append(setVirtualList[i]);\r
+                               fileBuffer.append(this.setVirtalAddList.get(i));\r
                        }\r
                        //\r
                        //  If module is not DXE_DRIVER, DXE_RUNTIME_DIRVER, UEFI_DRIVER\r
@@ -2019,21 +1900,19 @@ public class AutoGen {
                        // set count to the number of valid entries.\r
                        //\r
                        Count = 0;\r
-                       if (exitBootList != null) {\r
-                               if (setVirtualList != null) {\r
-                                       for (i = 0; i < exitBootList.length; i++) {\r
-                                               if (exitBootList[i].equalsIgnoreCase("")) {\r
-                                                       break;\r
-                                               }\r
+                       if (this.exitBootServiceList != null) {\r
+                               for (i = 0; i < this.exitBootServiceList.size(); i++) {\r
+                                       if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {\r
+                                               break;\r
                                        }\r
-                                       Count = i;\r
                                }\r
+                               Count = i;\r
                        }\r
                        fileBuffer.append(Integer.toString(Count));\r
                        fileBuffer.append(";\r\n\r\n");\r
                }\r
 \r
-               if (exitBootList == null) {\r
+               if (this.exitBootServiceList == null || this.exitBootServiceList.size() == 0) {\r
                        if (UefiOrDxeModule) {\r
                                //\r
                                // No data so make a NULL list.\r
@@ -2047,14 +1926,14 @@ public class AutoGen {
                        //\r
                        // Write DriverExitBootServices function definition.\r
                        //\r
-                       for (i = 0; i < exitBootList.length; i++) {\r
-                               if (exitBootList[i].equalsIgnoreCase("")) {\r
+                       for (i = 0; i < this.exitBootServiceList.size(); i++) {\r
+                               if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {\r
                                        break;\r
                                }\r
 \r
                                fileBuffer.append("VOID\r\n");\r
                                fileBuffer.append("EFIAPI\r\n");\r
-                               fileBuffer.append(exitBootList[i]);\r
+                               fileBuffer.append(this.exitBootServiceList.get(i));\r
                                fileBuffer.append(" (\r\n");\r
                                fileBuffer.append("  IN EFI_EVENT  Event,\r\n");\r
                                fileBuffer.append("  IN VOID       *Context\r\n");\r
@@ -2066,8 +1945,8 @@ public class AutoGen {
                        //\r
                        fileBuffer\r
                                        .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED const EFI_EVENT_NOTIFY _gDriverExitBootServicesEvent[] = {");\r
-                       for (i = 0; i < exitBootList.length; i++) {\r
-                               if (exitBootList[i].equalsIgnoreCase("")) {\r
+                       for (i = 0; i < this.exitBootServiceList.size(); i++) {\r
+                               if (this.exitBootServiceList.get(i).equalsIgnoreCase("")) {\r
                                        break;\r
                                }\r
 \r
@@ -2076,7 +1955,7 @@ public class AutoGen {
                                } else {\r
                                        fileBuffer.append(",\r\n  ");\r
                                }\r
-                               fileBuffer.append(exitBootList[i]);\r
+                               fileBuffer.append(this.exitBootServiceList.get(i));\r
                        }\r
                        if (!UefiOrDxeModule) {\r
                                fileBuffer.append(",\r\n  NULL");\r
@@ -2086,4 +1965,171 @@ public class AutoGen {
 \r
        }\r
 \r
-}
\ No newline at end of file
+    private void copyFlashMapHToDebugDir() throws  AutoGenException{\r
+        \r
+        File inFile = new File(fvDir + File.separatorChar + CommonDefinition.flashMapH);\r
+        int size = (int)inFile.length();\r
+        byte[] buffer = new byte[size];\r
+        File outFile = new File (this.outputPath + File.separatorChar + CommonDefinition.tianoR8FlashMapH);\r
+               //\r
+               //  If TianoR8FlashMap.h existed and the flashMap.h don't change, \r
+               //  do nothing.\r
+               // \r
+        if ((!outFile.exists()) ||(inFile.lastModified() - outFile.lastModified()) >= 0) {\r
+                   try{\r
+                if (inFile.exists()) {\r
+                    FileInputStream fis = new FileInputStream (inFile);\r
+                    fis.read(buffer);\r
+                    FileOutputStream fos = new FileOutputStream(outFile);\r
+                    fos.write(buffer);\r
+                    fis.close();\r
+                    fos.close();\r
+                }else {\r
+                    throw new AutoGenException("The file, flashMap.h doesn't exist!");\r
+                }\r
+            } catch (Exception e){\r
+                throw new AutoGenException(e.getMessage());\r
+            }\r
+               }\r
+    }\r
+    \r
+    /**\r
+    *This function first order the library instances, then collect\r
+    *library instance 's PPI, Protocol, GUID,\r
+    *SetVirtalAddressMapCallBack, ExitBootServiceCallBack, and\r
+    *Destructor, Constructor.\r
+    *\r
+       **/\r
+       private void collectLibInstanceInfo(){\r
+               int index;\r
+\r
+               String moduleType = SurfaceAreaQuery.getModuleType();\r
+               String libConstructName = null;\r
+               String libDestructName = null;\r
+               String[] setVirtuals = null;\r
+               String[] exitBoots = null;\r
+\r
+               ModuleIdentification[] libraryIdList = SurfaceAreaQuery\r
+                               .getLibraryInstance(this.arch);\r
+               try {\r
+                       if (libraryIdList != null) {\r
+                               //\r
+                               // Reorder library instance sequence.\r
+                               //\r
+                               AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList,\r
+                                               this.arch);\r
+                               List<ModuleIdentification> orderList = libOrder\r
+                                               .orderLibInstance();\r
+\r
+                               if (orderList != null) {\r
+                                       //\r
+                                       // Process library instance one by one.\r
+                                       //\r
+                                       for (int i = 0; i < orderList.size(); i++) {\r
+\r
+                                               //\r
+                                               // Get library instance basename.\r
+                                               //\r
+                                               ModuleIdentification libInstanceId = orderList.get(i);\r
+\r
+                                               //\r
+                                               // Get override map\r
+                                               //\r
+\r
+                                               Map<String, XmlObject> libDoc = GlobalData.getDoc(\r
+                                                               libInstanceId, this.arch);\r
+                                               SurfaceAreaQuery.push(libDoc);\r
+                                               //\r
+                                               // Get <PPis>, <Protocols>, <Guids> list of this library\r
+                                               // instance.\r
+                                               //\r
+                                               String[] ppiList = SurfaceAreaQuery.getPpiArray(this.arch);\r
+                                               String[] ppiNotifyList = SurfaceAreaQuery\r
+                                                               .getPpiNotifyArray(this.arch);\r
+                                               String[] protocolList = SurfaceAreaQuery\r
+                                                               .getProtocolArray(this.arch);\r
+                                               String[] protocolNotifyList = SurfaceAreaQuery\r
+                                                               .getProtocolNotifyArray(this.arch);\r
+                                               String[] guidList = SurfaceAreaQuery\r
+                                                               .getGuidEntryArray(this.arch);\r
+                                               PackageIdentification[] pkgList = SurfaceAreaQuery.getDependencePkg(this.arch);\r
+\r
+                                               //\r
+                                               // Add those ppi, protocol, guid in global ppi,\r
+                                               // protocol, guid\r
+                                               // list.\r
+                                               //\r
+                                               for (index = 0; index < ppiList.length; index++) {\r
+                                                       this.mPpiList.add(ppiList[index]);\r
+                                               }\r
+\r
+                                               for (index = 0; index < ppiNotifyList.length; index++) {\r
+                                                       this.mPpiList.add(ppiNotifyList[index]);\r
+                                               }\r
+\r
+                                               for (index = 0; index < protocolList.length; index++) {\r
+                                                       this.mProtocolList.add(protocolList[index]);\r
+                                               }\r
+\r
+                                               for (index = 0; index < protocolNotifyList.length; index++) {\r
+                                                       this.mProtocolList.add(protocolNotifyList[index]);\r
+                                               }\r
+\r
+                                               for (index = 0; index < guidList.length; index++) {\r
+                                                       this.mGuidList.add(guidList[index]);\r
+                                               }\r
+                                               for (index = 0; index < pkgList.length; index++){\r
+                                                       if (!this.mDepPkgList.contains(pkgList[index])){\r
+                                                               this.mDepPkgList.add(pkgList[index]);\r
+                                                       }\r
+                                               }\r
+\r
+                                               //\r
+                                               // If not yet parse this library instance's constructor\r
+                                               // element,parse it.\r
+                                               //\r
+                                               libConstructName = SurfaceAreaQuery\r
+                                                               .getLibConstructorName();\r
+                                               libDestructName = SurfaceAreaQuery\r
+                                                               .getLibDestructorName();\r
+\r
+                                               //\r
+                                               // Collect SetVirtualAddressMapCallBack and \r
+                                               // ExitBootServiceCallBack.\r
+                                               // \r
+                        setVirtuals = SurfaceAreaQuery.getSetVirtualAddressMapCallBackArray();\r
+                        exitBoots = SurfaceAreaQuery.getExitBootServicesCallBackArray();\r
+                        if (setVirtuals != null) {\r
+                            for (int j = 0; j < setVirtuals.length; j++) {\r
+                                                           this.setVirtalAddList.add(setVirtuals[j]);\r
+                                                   }\r
+                                               }\r
+                                               if (exitBoots != null) {\r
+                                                       for (int k = 0; k < exitBoots.length; k++) {\r
+                                                           this.exitBootServiceList.add(exitBoots[k]);\r
+                                                   }\r
+                                               }\r
+                                               SurfaceAreaQuery.pop();\r
+                                               //\r
+                                               // Add dependent library instance constructor function.\r
+                                               //\r
+                                               if (libConstructName != null) {\r
+                                                       this.libConstructList.add(libConstructName);\r
+                                               }\r
+                                               //\r
+                                               // Add dependent library instance destructor fuction.\r
+                                               //\r
+                                               if (libDestructName != null) {\r
+                                                       this.libDestructList.add(libDestructName);\r
+                                               }\r
+                                       }\r
+                               }\r
+\r
+                       }\r
+\r
+       }catch (Exception e){\r
+       System.out.println(e.getMessage());\r
+       System.out.println("Collect library instance failed!");\r
+}\r
+}\r
+}\r