]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java
1, Fix EDKT141
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / action / CollectPCDAction.java
index 8c91b2fe16372b64e9a6612d6ae0cb793770809c..8242a4c87c363dca42b14fbc237c6a635f683b38 100644 (file)
@@ -23,7 +23,6 @@ import java.io.FileReader;
 import java.io.IOException;\r
 import java.math.BigInteger;\r
 import java.util.ArrayList;\r
-import java.util.Collections;\r
 import java.util.Comparator;\r
 import java.util.HashMap;\r
 import java.util.Iterator;\r
@@ -36,22 +35,15 @@ import java.util.regex.Pattern;
 \r
 import org.apache.xmlbeans.XmlException;\r
 import org.apache.xmlbeans.XmlObject;\r
-import org.tianocore.DynamicPcdBuildDefinitionsDocument;\r
 import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions;\r
-import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData;\r
-import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo;\r
 import org.tianocore.FrameworkModulesDocument;\r
-import org.tianocore.PcdDeclarationsDocument;\r
-import org.tianocore.PlatformSurfaceAreaDocument;\r
-import org.tianocore.PcdBuildDefinitionDocument;\r
-import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;\r
 import org.tianocore.ModuleSADocument;\r
-import org.tianocore.ModuleSADocument.ModuleSA;\r
-import org.tianocore.PackageSurfaceAreaDocument;\r
+import org.tianocore.PcdBuildDefinitionDocument;\r
 import org.tianocore.PcdBuildDefinitionDocument.PcdBuildDefinition;\r
+import org.tianocore.PlatformSurfaceAreaDocument;\r
 import org.tianocore.build.autogen.CommonDefinition;\r
+import org.tianocore.build.fpd.FpdParserTask;\r
 import org.tianocore.build.global.GlobalData;\r
-import org.tianocore.build.global.SurfaceAreaQuery;\r
 import org.tianocore.build.id.FpdModuleIdentification;\r
 import org.tianocore.build.pcd.action.ActionMessage;\r
 import org.tianocore.build.pcd.entity.DynamicTokenValue;\r
@@ -60,8 +52,16 @@ import org.tianocore.build.pcd.entity.SkuInstance;
 import org.tianocore.build.pcd.entity.Token;\r
 import org.tianocore.build.pcd.entity.UsageInstance;\r
 import org.tianocore.build.pcd.exception.EntityException;\r
-import org.tianocore.logger.EdkLog;\r
-import org.tianocore.ModuleTypeDef;\r
+\r
+/**\r
+    CStructTypeDeclaration   \r
+    \r
+    This class is used to store the declaration string, such as\r
+    "UINT32 PcdPlatformFlashBaseAddress", of \r
+    each memember in the C structure, which is a standard C language\r
+    feature used to implement a simple and efficient database for\r
+    dynamic(ex) type PCD entry.\r
+**/\r
 \r
 class CStructTypeDeclaration {\r
     String key;\r
@@ -77,6 +77,12 @@ class CStructTypeDeclaration {
     }\r
 }\r
 \r
+/**\r
+    StringTable   \r
+    \r
+    This class is used to store the String in a PCD database.\r
+    \r
+**/\r
 class StringTable {\r
     private ArrayList<String>   al; \r
     private ArrayList<String>   alComments;\r
@@ -101,15 +107,11 @@ class StringTable {
         return len == 0 ? 1 : len;\r
     }\r
 \r
-    public int getTableLen () {\r
-        return al.size() == 0 ? 1 : al.size();\r
-    }\r
-\r
     public String getExistanceMacro () {\r
         return String.format(PcdDatabase.StringTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
     }\r
     \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable) {\r
         final String stringTable = "StringTable";\r
         final String tab         = "\t";\r
         final String newLine     = "\r\n";\r
@@ -154,11 +156,15 @@ class StringTable {
                     stringTableName = String.format("%s_%d", stringTable, i);\r
                     cDeclCode += tab;\r
                 }\r
-                cDeclCode += String.format("%-20s%s[%d]; /* %s */", "UINT16", stringTableName, str.length() + 1, alComments.get(i)) + newLine;\r
+                cDeclCode += String.format("%-20s%s[%d]; /* %s */", "UINT16", \r
+                                           stringTableName, str.length() + 1, \r
+                                           alComments.get(i)) \r
+                             + newLine;\r
                 \r
                 if (i == 0) {\r
                     cInstCode = "/* StringTable */" + newLine;\r
                 }\r
+                \r
                 cInstCode += tab + String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i));\r
                 if (i != al.size() - 1) {\r
                     cInstCode += commaNewLine;\r
@@ -177,59 +183,6 @@ class StringTable {
         }\r
     }\r
 \r
-    public String getTypeDeclaration () {\r
-\r
-        String output;\r
-\r
-        final String stringTable = "StringTable";\r
-        final String tab = "\t";\r
-        final String newLine = ";\r\n";\r
-\r
-        output =  "/* StringTable */\r\n";\r
-\r
-        if (al.size() == 0) {\r
-            output += tab + String.format("UINT16 %s[1] /* StringTable is Empty */", stringTable) + newLine;\r
-        }\r
-\r
-        for (int i = 0; i < al.size(); i++) {\r
-            String str = al.get(i);\r
-\r
-            if (i == 0) {\r
-                //\r
-                // StringTable is a well-known name in the PCD DXE driver\r
-                //\r
-                output += tab + String.format("UINT16       %s[%d] /* %s */", stringTable, str.length() + 1, alComments.get(i)) + newLine;\r
-            } else {\r
-                output += tab + String.format("UINT16       %s_%d[%d] /* %s */", stringTable, i, str.length() + 1, alComments.get(i)) + newLine;\r
-            }\r
-        }\r
-\r
-        return output;\r
-\r
-    }\r
-\r
-    public ArrayList<String> getInstantiation () {\r
-        ArrayList<String> output = new ArrayList<String>();\r
-\r
-        output.add("/* StringTable */"); \r
-\r
-        if (al.size() == 0) {\r
-            output.add("{ 0 }");\r
-        } else {\r
-            String str;\r
-\r
-            for (int i = 0; i < al.size(); i++) {\r
-                str = String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i));\r
-                if (i != al.size() - 1) {\r
-                    str += ",";\r
-                }\r
-                output.add(str);\r
-            }\r
-        }\r
-\r
-        return output;\r
-    }\r
-\r
     public int add (String inputStr, Token token) {\r
         int i;\r
         int pos;\r
@@ -254,7 +207,6 @@ class StringTable {
                 return pos;\r
             }\r
             pos = s.length() + 1;\r
-            \r
         }\r
         \r
         i = len;\r
@@ -269,20 +221,35 @@ class StringTable {
     }\r
 }\r
 \r
+/**\r
+    SizeTable   \r
+    \r
+    This class is used to store the Size information for\r
+    POINTER TYPE PCD entry in a PCD database.\r
+\r
+**/\r
 class SizeTable {\r
-    private ArrayList<Integer>  al;\r
+    private ArrayList<ArrayList<Integer>>  al;\r
     private ArrayList<String>   alComments;\r
-    private String              phase;\r
     private int                 len;\r
+    private String              phase;\r
     \r
     public SizeTable (String phase) {\r
-        this.phase = phase;\r
-        al = new ArrayList<Integer>();\r
+        al = new ArrayList<ArrayList<Integer>>();\r
         alComments = new ArrayList<String>();\r
         len = 0;\r
+        this.phase = phase;\r
+    }\r
+\r
+    public String getSizeMacro () {\r
+        return String.format(PcdDatabase.SizeTableSizeMacro, phase, getSize());\r
+    }\r
+    \r
+    private int getSize() {\r
+        return len == 0 ? 1 : len;\r
     }\r
 \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
         final String name = "SizeTable";\r
         \r
         CStructTypeDeclaration decl;\r
@@ -302,11 +269,8 @@ class SizeTable {
         instTable.put(name, cCode);\r
     }\r
 \r
-    public String getTypeDeclaration () {\r
-        return String.format(PcdDatabase.SizeTableDeclaration, phase);\r
-    }\r
-\r
-    public ArrayList<String> getInstantiation () {\r
+    private ArrayList<String> getInstantiation () {\r
+        final String comma   = ",";\r
         ArrayList<String> Output = new ArrayList<String>();\r
 \r
         Output.add("/* SizeTable */");\r
@@ -315,14 +279,23 @@ class SizeTable {
             Output.add("\t0");\r
         } else {\r
             for (int index = 0; index < al.size(); index++) {\r
-                Integer n = al.get(index);\r
-                String str = "\t" + n.toString();\r
+                ArrayList<Integer> ial = al.get(index);\r
+                \r
+                String str = "\t";\r
+                \r
+                for (int index2 = 0; index2 < ial.size(); index2++) {\r
+                    str += " " + ial.get(index2).toString();\r
+                    if (index2 != ial.size() - 1) {\r
+                        str += comma;\r
+                    }\r
+                }\r
 \r
+                str += " /* " + alComments.get(index) + " */"; \r
+                \r
                 if (index != (al.size() - 1)) {\r
-                    str += ",";\r
+                    str += comma;\r
                 }\r
 \r
-                str += " /* " + alComments.get(index) + " */"; \r
                 Output.add(str);\r
     \r
             }\r
@@ -332,22 +305,32 @@ class SizeTable {
         return Output;\r
     }\r
 \r
-    public int add (Token token) {\r
-        int index = len;\r
+    public void add (Token token) {\r
 \r
-        len++; \r
-        al.add(token.datumSize);\r
+        //\r
+        // We only have size information for POINTER type PCD entry.\r
+        //\r
+        if (token.datumType != Token.DATUM_TYPE.POINTER) {\r
+            return;\r
+        }\r
+        \r
+        ArrayList<Integer> ial = token.getPointerTypeSize();\r
+        \r
+        len+= ial.size(); \r
+\r
+        al.add(ial);\r
         alComments.add(token.getPrimaryKeyString());\r
 \r
-        return index;\r
+        return;\r
     }\r
     \r
-    public int getTableLen () {\r
-        return al.size() == 0 ? 1 : al.size();\r
-    }\r
-\r
 }\r
 \r
+/**\r
+    GuidTable   \r
+    \r
+    This class is used to store the GUIDs in a PCD database.\r
+**/\r
 class GuidTable {\r
     private ArrayList<UUID> al;\r
     private ArrayList<String> alComments;\r
@@ -375,7 +358,7 @@ class GuidTable {
         return String.format(PcdDatabase.GuidTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
     }\r
 \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
         final String name = "GuidTable";\r
         \r
         CStructTypeDeclaration decl;\r
@@ -384,7 +367,7 @@ class GuidTable {
         cCode += String.format(PcdDatabase.GuidTableDeclaration, phase); \r
         decl = new CStructTypeDeclaration (\r
                                             name,\r
-                                            8,\r
+                                            4,\r
                                             cCode,\r
                                             true\r
                                            );  \r
@@ -395,10 +378,6 @@ class GuidTable {
         instTable.put(name, cCode);\r
     }\r
 \r
-    public String getTypeDeclaration () {\r
-        return String.format(PcdDatabase.GuidTableDeclaration, phase);\r
-    }\r
-\r
     private String getUuidCString (UUID uuid) {\r
         String[]  guidStrArray;\r
 \r
@@ -419,7 +398,7 @@ class GuidTable {
                                         );\r
     }\r
 \r
-    public ArrayList<String> getInstantiation () {\r
+    private ArrayList<String> getInstantiation () {\r
         ArrayList<String> Output = new ArrayList<String>();\r
 \r
         Output.add("/* GuidTable */");\r
@@ -451,7 +430,7 @@ class GuidTable {
         // If so, return the GuidTable index.\r
         //\r
         for (int i = 0; i < al.size(); i++) {\r
-            if (al.get(i).equals(uuid)) {\r
+            if (al.get(i).compareTo(uuid) == 0) {\r
                 return i;\r
             }\r
         }\r
@@ -466,12 +445,14 @@ class GuidTable {
         return len - 1;\r
     }\r
 \r
-    public int getTableLen () {\r
-        return al.size() == 0 ? 0 : al.size();\r
-    }\r
-\r
 }\r
 \r
+/**\r
+    SkuIdTable   \r
+    \r
+    This class is used to store the SKU IDs in a PCD database.\r
+\r
+**/\r
 class SkuIdTable {\r
     private ArrayList<Integer[]> al;\r
     private ArrayList<String>    alComment;\r
@@ -497,7 +478,7 @@ class SkuIdTable {
         return String.format(PcdDatabase.SkuTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
     }\r
 \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
         final String name = "SkuIdTable";\r
         \r
         CStructTypeDeclaration decl;\r
@@ -533,11 +514,7 @@ class SkuIdTable {
 \r
     }\r
 \r
-    public String getTypeDeclaration () {\r
-        return String.format(PcdDatabase.SkuIdTableDeclaration, phase);\r
-    }\r
-\r
-    public ArrayList<String> getInstantiation () {\r
+    private ArrayList<String> getInstantiation () {\r
         ArrayList<String> Output = new ArrayList<String> ();\r
 \r
         Output.add("/* SkuIdTable */");\r
@@ -617,10 +594,6 @@ class SkuIdTable {
         return index;\r
     }\r
 \r
-    public int getTableLen () {\r
-        return al.size() == 0 ? 1 : al.size();\r
-    }\r
-\r
 }\r
 \r
 class LocalTokenNumberTable {\r
@@ -650,7 +623,7 @@ class LocalTokenNumberTable {
         return String.format(PcdDatabase.DatabaseExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
     }\r
 \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
         final String name = "LocalTokenNumberTable";\r
         \r
         CStructTypeDeclaration decl;\r
@@ -669,11 +642,7 @@ class LocalTokenNumberTable {
         instTable.put(name, cCode);\r
     }\r
 \r
-    public String getTypeDeclaration () {\r
-        return String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase);\r
-    }\r
-\r
-    public ArrayList<String> getInstantiation () {\r
+    private ArrayList<String> getInstantiation () {\r
         ArrayList<String> output = new ArrayList<String>();\r
 \r
         output.add("/* LocalTokenNumberTable */");\r
@@ -728,6 +697,25 @@ class LocalTokenNumberTable {
             str += " | PCD_TYPE_VPD";\r
         }\r
         \r
+        switch (token.datumType) {\r
+        case UINT8:\r
+        case BOOLEAN:\r
+            str += " | PCD_DATUM_TYPE_UINT8";\r
+            break;\r
+        case UINT16:\r
+            str += " | PCD_DATUM_TYPE_UINT16";\r
+            break;\r
+        case UINT32:\r
+            str += " | PCD_DATUM_TYPE_UINT32";\r
+            break;\r
+        case UINT64:\r
+            str += " | PCD_DATUM_TYPE_UINT64";\r
+            break;\r
+        case POINTER:\r
+            str += " | PCD_DATUM_TYPE_POINTER";\r
+            break;\r
+        }\r
+        \r
         al.add(str);\r
         alComment.add(token.getPrimaryKeyString());\r
 \r
@@ -735,8 +723,22 @@ class LocalTokenNumberTable {
     }\r
 }\r
 \r
+/**\r
+    ExMapTable   \r
+    \r
+    This class is used to store the table of mapping information\r
+    between DynamicEX ID pair(Guid, TokenNumber) and\r
+    the local token number assigned by PcdDatabase class.\r
+**/\r
 class ExMapTable {\r
 \r
+    /**\r
+        ExTriplet   \r
+        \r
+        This class is used to store the mapping information\r
+        between DynamicEX ID pair(Guid, TokenNumber) and\r
+        the local token number assigned by PcdDatabase class.\r
+    **/\r
     class ExTriplet {\r
         public Integer guidTableIdx;\r
         public Long exTokenNumber;\r
@@ -750,7 +752,7 @@ class ExMapTable {
     }\r
 \r
     private ArrayList<ExTriplet> al;\r
-    private ArrayList<String>    alComment;\r
+    private Map<ExTriplet, String> alComment;\r
     private String               phase;\r
     private int                  len;\r
     private int                   bodyLineNum;\r
@@ -758,7 +760,7 @@ class ExMapTable {
     public ExMapTable (String phase) {\r
         this.phase = phase;\r
         al = new ArrayList<ExTriplet>();\r
-        alComment = new ArrayList<String>();\r
+        alComment = new HashMap<ExTriplet, String>();\r
         bodyLineNum = 0;\r
         len = 0;\r
     }\r
@@ -772,7 +774,7 @@ class ExMapTable {
         return String.format(PcdDatabase.ExMapTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE");\r
     }\r
 \r
-    public void genCodeNew (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
+    public void genCode (ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) {\r
         final String exMapTableName = "ExMapTable";\r
         \r
         sortTable();\r
@@ -794,11 +796,7 @@ class ExMapTable {
         instTable.put(exMapTableName, cCode);\r
     }\r
     \r
-    public String getTypeDeclaration () {\r
-        return String.format(PcdDatabase.ExMapTableDeclaration, phase);\r
-    }\r
-\r
-    public ArrayList<String> getInstantiation () {\r
+    private ArrayList<String> getInstantiation () {\r
         ArrayList<String> Output = new ArrayList<String>();\r
 \r
         Output.add("/* ExMapTable */");\r
@@ -817,7 +815,7 @@ class ExMapTable {
             str += e.localTokenIdx.toString() + ", ";\r
             str += e.guidTableIdx.toString();\r
 \r
-            str += "}" + " /* " + alComment.get(index) + " */" ;\r
+            str += "}" + " /* " + alComment.get(e) + " */" ;\r
 \r
             if (index != al.size() - 1) {\r
                 str += ",";\r
@@ -836,14 +834,16 @@ class ExMapTable {
     public int add (int localTokenIdx, long exTokenNum, int guidTableIdx, String name) {\r
         int index = len;\r
 \r
-        len++; \r
-        al.add(new ExTriplet(guidTableIdx, exTokenNum, localTokenIdx));\r
-        alComment.add(name);\r
+        len++;\r
+        ExTriplet et = new ExTriplet(guidTableIdx, exTokenNum, localTokenIdx); \r
+\r
+        al.add(et);\r
+        alComment.put(et, name);\r
 \r
         return index;\r
     }\r
 \r
-    public int getTableLen () {\r
+    private int getTableLen () {\r
         return al.size() == 0 ? 1 : al.size();\r
     }\r
 \r
@@ -857,12 +857,15 @@ class ExMapTable {
     class ExTripletComp implements Comparator<ExTriplet> {\r
         public int compare (ExTriplet a, ExTriplet b) {\r
             if (a.guidTableIdx == b.guidTableIdx ) {\r
+                //\r
+                // exTokenNumber is long, we can't use simple substraction.\r
+                //\r
                 if (a.exTokenNumber > b.exTokenNumber) {\r
                     return 1;\r
-                } else if (a.exTokenNumber > b.exTokenNumber) {\r
-                    return 1;\r
-                } else {\r
+                } else if (a.exTokenNumber == b.exTokenNumber) {\r
                     return 0;\r
+                } else {\r
+                    return -1;\r
                 }\r
             }\r
             \r
@@ -876,6 +879,12 @@ class ExMapTable {
     }\r
 }\r
 \r
+/**\r
+    PcdDatabase   \r
+    \r
+    This class is used to generate C code for Autogen.h and Autogen.c of\r
+    a PCD service DXE driver and PCD service PEIM.\r
+**/\r
 class PcdDatabase {\r
 \r
     private final static int    SkuHeadAlignmentSize             = 4;\r
@@ -886,7 +895,7 @@ class PcdDatabase {
     public final static String GuidTableDeclaration             = "EFI_GUID            GuidTable[%s_GUID_TABLE_SIZE];\r\n";\r
     public final static String LocalTokenNumberTableDeclaration = "UINT32              LocalTokenNumberTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";\r
     public final static String StringTableDeclaration           = "UINT16              StringTable[%s_STRING_TABLE_SIZE];\r\n";\r
-    public final static String SizeTableDeclaration             = "UINT16              SizeTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n";\r
+    public final static String SizeTableDeclaration             = "SIZE_INFO           SizeTable[%s_SIZE_TABLE_SIZE];\r\n";\r
     public final static String SkuIdTableDeclaration            = "UINT8               SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n";\r
 \r
 \r
@@ -895,6 +904,7 @@ class PcdDatabase {
     public final static String GuidTableSizeMacro               = "#define %s_GUID_TABLE_SIZE         %d\r\n"; \r
     public final static String LocalTokenNumberTableSizeMacro   = "#define %s_LOCAL_TOKEN_NUMBER_TABLE_SIZE            %d\r\n";\r
     public final static String LocalTokenNumberSizeMacro               = "#define %s_LOCAL_TOKEN_NUMBER            %d\r\n";\r
+    public final static String SizeTableSizeMacro               = "#define %s_SIZE_TABLE_SIZE            %d\r\n";\r
     public final static String StringTableSizeMacro             = "#define %s_STRING_TABLE_SIZE       %d\r\n";\r
     public final static String SkuIdTableSizeMacro              = "#define %s_SKUID_TABLE_SIZE        %d\r\n";\r
 \r
@@ -941,14 +951,21 @@ class PcdDatabase {
     private String hString;\r
     private String cString;\r
 \r
-\r
-    class AlignmentSizeComp implements Comparator<Token> {\r
-        public int compare (Token a, Token b) {\r
-            return getAlignmentSize(b) \r
-                    - getAlignmentSize(a);\r
-        }\r
-    }\r
-    \r
+    /**\r
+        Constructor for PcdDatabase class. \r
+        \r
+        <p>We have two PCD dynamic(ex) database for the Framework implementation. One\r
+        for PEI phase and the other for DXE phase.  </p>\r
+        \r
+        @param alTokens A ArrayList of Dynamic(EX) PCD entry.\r
+        @param exePhase The phase to generate PCD database for: valid input\r
+                        is "PEI" or "DXE".\r
+        @param startLen The starting Local Token Number for the PCD database. For\r
+                        PEI phase, the starting Local Token Number starts from 0.\r
+                        For DXE phase, the starting Local Token Number starts\r
+                        from the total number of PCD entry of PEI phase.\r
+        @return void\r
+    **/\r
     public PcdDatabase (ArrayList<Token> alTokens, String exePhase, int startLen) {\r
        phase = exePhase;\r
 \r
@@ -959,6 +976,11 @@ class PcdDatabase {
        sizeTable = new SizeTable(phase);\r
        exMapTable = new ExMapTable(phase); \r
 \r
+       //\r
+       // Local token number 0 is reserved for INVALID_TOKEN_NUMBER.\r
+       // So we will increment 1 for the startLen passed from the \r
+       // constructor.\r
+       //\r
        assignedTokenNumber = startLen + 1;\r
        this.alTokens = alTokens;\r
     }\r
@@ -976,20 +998,26 @@ class PcdDatabase {
         return;\r
     }\r
 \r
-    private void getTwoGroupsOfTokens (ArrayList<Token> alTokens, List<Token> initTokens, List<Token> uninitTokens) {\r
-        for (int i = 0; i < alTokens.size(); i++) {\r
-            Token t = (Token)alTokens.get(i);\r
-            if (t.hasDefaultValue()) {\r
-                initTokens.add(t);\r
-            } else {\r
-                uninitTokens.add(t);\r
-            }\r
+    private int getDataTypeAlignmentSize (Token token) {\r
+        switch (token.datumType) {\r
+        case UINT8:\r
+            return 1;\r
+        case UINT16:\r
+            return 2;\r
+        case UINT32:\r
+            return 4;\r
+        case UINT64:\r
+            return 8;\r
+        case POINTER:\r
+            return 1;\r
+        case BOOLEAN:\r
+            return 1;\r
+        default:\r
+            return 1;\r
         }\r
-\r
-        return;\r
     }\r
-\r
-    private int getDataTypeAlignmentSize (Token token) {\r
+    \r
+    private int getHiiPtrTypeAlignmentSize(Token token) {\r
         switch (token.datumType) {\r
         case UINT8:\r
             return 1;\r
@@ -1000,6 +1028,11 @@ class PcdDatabase {
         case UINT64:\r
             return 8;\r
         case POINTER:\r
+            if (token.isHiiEnable()) {\r
+                if (token.isHiiDefaultValueUnicodeStringType()) {\r
+                    return 2;\r
+                }\r
+            }\r
             return 1;\r
         case BOOLEAN:\r
             return 1;\r
@@ -1069,7 +1102,7 @@ class PcdDatabase {
 \r
     }\r
 \r
-    private void ProcessTokensNew (List<Token> tokens, \r
+    private void ProcessTokens (List<Token> tokens, \r
                                    ArrayList<CStructTypeDeclaration> cStructDeclList,\r
                                    HashMap<String, String> cStructInstTable,\r
                                    String phase\r
@@ -1091,7 +1124,7 @@ class PcdDatabase {
             if (t.isDynamicEx()) {\r
                 exMapTable.add((int)t.tokenNumber, \r
                                 t.dynamicExTokenNumber, \r
-                                guidTable.add(t.tokenSpaceName, t.getPrimaryKeyString()), \r
+                                guidTable.add(translateSchemaStringToUUID(t.tokenSpaceName), t.getPrimaryKeyString()), \r
                                 t.getPrimaryKeyString()\r
                                 );\r
             }\r
@@ -1099,7 +1132,7 @@ class PcdDatabase {
 \r
     }\r
     \r
-    public void genCodeNew () throws EntityException {\r
+    public void genCode () throws EntityException {\r
         \r
         ArrayList<CStructTypeDeclaration> cStructDeclList = new ArrayList<CStructTypeDeclaration>();\r
         HashMap<String, String> cStructInstTable = new HashMap<String, String>();\r
@@ -1117,15 +1150,15 @@ class PcdDatabase {
         // EX type token number starts from the last Non-EX PCD entry and\r
         // grows continously upwards.\r
         //\r
-        ProcessTokensNew (nexTokens, cStructDeclList, cStructInstTable, phase);\r
-        ProcessTokensNew (exTokens, cStructDeclList, cStructInstTable, phase);\r
+        ProcessTokens (nexTokens, cStructDeclList, cStructInstTable, phase);\r
+        ProcessTokens (exTokens, cStructDeclList, cStructInstTable, phase);\r
         \r
-        stringTable.genCodeNew(cStructDeclList, cStructInstTable);\r
-        skuIdTable.genCodeNew(cStructDeclList, cStructInstTable, phase);\r
-        exMapTable.genCodeNew(cStructDeclList, cStructInstTable, phase);\r
-        localTokenNumberTable.genCodeNew(cStructDeclList, cStructInstTable, phase);\r
-        sizeTable.genCodeNew(cStructDeclList, cStructInstTable, phase);\r
-        guidTable.genCodeNew(cStructDeclList, cStructInstTable, phase);\r
+        stringTable.genCode(cStructDeclList, cStructInstTable);\r
+        skuIdTable.genCode(cStructDeclList, cStructInstTable, phase);\r
+        exMapTable.genCode(cStructDeclList, cStructInstTable, phase);\r
+        localTokenNumberTable.genCode(cStructDeclList, cStructInstTable, phase);\r
+        sizeTable.genCode(cStructDeclList, cStructInstTable, phase);\r
+        guidTable.genCode(cStructDeclList, cStructInstTable, phase);\r
         \r
         hString = genCMacroCode ();\r
         \r
@@ -1156,6 +1189,7 @@ class PcdDatabase {
         macroStr += skuIdTable.getSizeMacro();\r
         macroStr += localTokenNumberTable.getSizeMacro();\r
         macroStr += exMapTable.getSizeMacro();\r
+        macroStr += sizeTable.getSizeMacro();\r
 \r
         //\r
         // Generate existance info Macro for all Tables\r
@@ -1261,159 +1295,6 @@ class PcdDatabase {
         return result;\r
     }\r
 \r
-     public void genCode () \r
-        throws EntityException {\r
-\r
-        final String newLine                        = "\r\n";\r
-        final String declNewLine                    = ";\r\n";\r
-        final String tab                            = "\t";\r
-        final String commaNewLine                   = ", \r\n";\r
-\r
-        int i;\r
-        ArrayList<String> decla;\r
-        ArrayList<String> inst;\r
-\r
-        String macroStr   = "";\r
-        String initDeclStr = "";\r
-        String initInstStr = "";\r
-        String uninitDeclStr = "";\r
-\r
-        List<Token> initTokens = new ArrayList<Token> ();\r
-        List<Token> uninitTokens = new ArrayList<Token> ();\r
-        \r
-        HashMap <String, ArrayList<String>> initCode = new HashMap<String, ArrayList<String>> ();\r
-        HashMap <String, ArrayList<String>> uninitCode = new HashMap<String, ArrayList<String>> ();\r
-\r
-        getTwoGroupsOfTokens (alTokens, initTokens, uninitTokens);\r
-\r
-        //\r
-        // Generate Structure Declaration for PcdTokens without Default Value\r
-        // PEI_PCD_DATABASE_INIT\r
-        //\r
-        java.util.Comparator<Token> comparator = new AlignmentSizeComp();\r
-        java.util.Collections.sort(initTokens, comparator);\r
-        initCode = processTokens(initTokens);\r
-\r
-        //\r
-        // Generate Structure Declaration for PcdTokens without Default Value\r
-        // PEI_PCD_DATABASE_UNINIT\r
-        //\r
-        java.util.Collections.sort(uninitTokens, comparator);\r
-        uninitCode = processTokens(uninitTokens);\r
-\r
-        //\r
-        // Generate size info Macro for all Tables\r
-        //\r
-        macroStr += guidTable.getSizeMacro();\r
-        macroStr += stringTable.getSizeMacro();\r
-        macroStr += skuIdTable.getSizeMacro();\r
-        macroStr += localTokenNumberTable.getSizeMacro();\r
-        macroStr += exMapTable.getSizeMacro();\r
-\r
-        //\r
-        // Generate existance info Macro for all Tables\r
-        //\r
-        macroStr += guidTable.getExistanceMacro();\r
-        macroStr += stringTable.getExistanceMacro();\r
-        macroStr += skuIdTable.getExistanceMacro();\r
-        macroStr += localTokenNumberTable.getExistanceMacro();\r
-        macroStr += exMapTable.getExistanceMacro();\r
-\r
-        //\r
-        // Generate Structure Declaration for PcdTokens with Default Value\r
-        // for example PEI_PCD_DATABASE_INIT\r
-        //\r
-        initDeclStr += "typedef struct {" + newLine;\r
-            {\r
-                initDeclStr += tab + exMapTable.getTypeDeclaration();\r
-                initDeclStr += tab + guidTable.getTypeDeclaration();\r
-                initDeclStr += tab + localTokenNumberTable.getTypeDeclaration();\r
-                initDeclStr += tab + stringTable.getTypeDeclaration();\r
-                initDeclStr += tab + sizeTable.getTypeDeclaration();\r
-                initDeclStr += tab + skuIdTable.getTypeDeclaration();\r
-                if (phase.equalsIgnoreCase("PEI")) {\r
-                    initDeclStr += tab + "SKU_ID            SystemSkuId;" + newLine;\r
-                }\r
-\r
-                decla = initCode.get(new String("Declaration"));\r
-                for (i = 0; i < decla.size(); i++)  {\r
-                    initDeclStr += tab + decla.get(i) + declNewLine;\r
-                }\r
-\r
-                //\r
-                // Generate Structure Declaration for PcdToken with SkuEnabled\r
-                //\r
-                decla = initCode.get("DeclarationForSku");\r
-\r
-                for (i = 0; i < decla.size(); i++) {\r
-                    initDeclStr += tab + decla.get(i) + declNewLine;\r
-                }\r
-            }\r
-        initDeclStr += String.format("} %s_PCD_DATABASE_INIT;\r\n\r\n", phase);\r
-\r
-        //\r
-        // Generate MACRO for structure intialization of PCDTokens with Default Value\r
-        // The sequence must match the sequence of declaration of the memembers in the structure\r
-        String tmp = String.format("%s_PCD_DATABASE_INIT g%sPcdDbInit = { ", phase.toUpperCase(), phase.toUpperCase());\r
-        initInstStr +=  tmp + newLine;\r
-        initInstStr += tab + genInstantiationStr(exMapTable.getInstantiation()) + commaNewLine;\r
-        initInstStr += tab + genInstantiationStr(guidTable.getInstantiation()) + commaNewLine;\r
-        initInstStr += tab + genInstantiationStr(localTokenNumberTable.getInstantiation()) + commaNewLine; \r
-        initInstStr += tab + genInstantiationStr(stringTable.getInstantiation()) + commaNewLine;\r
-        initInstStr += tab + genInstantiationStr(sizeTable.getInstantiation()) + commaNewLine;\r
-        initInstStr += tab + genInstantiationStr(skuIdTable.getInstantiation()) + commaNewLine;\r
-        //\r
-        // For SystemSkuId\r
-        //\r
-        if (phase.equalsIgnoreCase("PEI")) {\r
-            initInstStr += tab + "0" + tab + "/* SystemSkuId */" + commaNewLine;\r
-        }\r
-\r
-        inst = initCode.get("Instantiation");\r
-        for (i = 0; i < inst.size(); i++) {\r
-            initInstStr += tab + inst.get(i) + commaNewLine;\r
-        }\r
-\r
-        inst = initCode.get("InstantiationForSku");\r
-        for (i = 0; i < inst.size(); i++) {\r
-            initInstStr += tab + inst.get(i);\r
-            if (i != inst.size() - 1) {\r
-                initInstStr += commaNewLine;\r
-            }\r
-        }\r
-\r
-        initInstStr += "};";\r
-\r
-        uninitDeclStr += "typedef struct {" + newLine;\r
-            {\r
-                decla = uninitCode.get("Declaration");\r
-                if (decla.size() == 0) {\r
-                    uninitDeclStr += "UINT8 dummy /* The UINT struct is empty */" + declNewLine;\r
-                } else {\r
-    \r
-                    for (i = 0; i < decla.size(); i++) {\r
-                        uninitDeclStr += tab + decla.get(i) + declNewLine;\r
-                    }\r
-    \r
-                    decla = uninitCode.get("DeclarationForSku");\r
-    \r
-                    for (i = 0; i < decla.size(); i++) {\r
-                        uninitDeclStr += tab + decla.get(i) + declNewLine;\r
-                    }\r
-                }\r
-            }\r
-        uninitDeclStr += String.format("} %s_PCD_DATABASE_UNINIT;\r\n\r\n", phase);\r
-\r
-        cString = initInstStr + newLine;\r
-        hString = macroStr      + newLine  \r
-              + initDeclStr   + newLine\r
-              + uninitDeclStr + newLine\r
-              + newLine;\r
-        \r
-        hString += String.format("#define PCD_%s_SERVICE_DRIVER_VERSION                        %d", phase, version);\r
-\r
-    }\r
-\r
     public static String genInstantiationStr (ArrayList<String> alStr) {\r
         String str = "";\r
         for (int i = 0; i< alStr.size(); i++) {\r
@@ -1429,133 +1310,19 @@ class PcdDatabase {
         return str;\r
     }\r
 \r
-    private HashMap<String, ArrayList<String>> processTokens (List<Token> alToken) \r
-        throws EntityException {\r
-\r
-        HashMap <String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();\r
-\r
-        ArrayList<String> decl = new ArrayList<String>();\r
-        ArrayList<String> declForSkuEnableType = new ArrayList<String>();\r
-        ArrayList<String> inst = new ArrayList<String>();\r
-        ArrayList<String> instForSkuEnableType = new ArrayList<String>();\r
+    private String getSkuEnabledTypeDeclaration (Token token) {\r
+        return String.format("%-20s%s;\r\n", "SKU_HEAD", token.getPrimaryKeyString());\r
+    }\r
 \r
-        for (int index = 0; index < alToken.size(); index++) {\r
-            Token token = alToken.get(index);\r
+    private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) {\r
 \r
-            if (token.isSkuEnable()) {\r
-                //\r
-                // BugBug: Schema only support Data type now\r
-                //\r
-                int tableIdx;\r
+        String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString());\r
+        return String.format("{ %s, %d } /* SKU_ENABLED: %s */", offsetof, SkuTableIdx, token.getPrimaryKeyString());\r
+    }\r
 \r
-                tableIdx = skuIdTable.add(token);\r
-\r
-                decl.add(getSkuEnabledTypeDeclaration(token));\r
-                if (token.hasDefaultValue()) {\r
-                    inst.add(getSkuEnabledTypeInstantiaion(token, tableIdx)); \r
-                }\r
-\r
-                declForSkuEnableType.add(getDataTypeDeclarationForSkuEnabled(token));\r
-                if (token.hasDefaultValue()) {\r
-                    instForSkuEnableType.add(getDataTypeInstantiationForSkuEnabled(token));\r
-                }\r
-\r
-            } else {\r
-                if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) {\r
-                    decl.add(getVariableEnableTypeDeclaration(token));\r
-                    inst.add(getVariableEnableInstantiation(token));\r
-                } else if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) {\r
-                    decl.add(getVpdEnableTypeDeclaration(token));\r
-                    inst.add(getVpdEnableTypeInstantiation(token));\r
-                } else if (token.isUnicodeStringType()) {\r
-                    decl.add(getStringTypeDeclaration(token));\r
-                    inst.add(getStringTypeInstantiation(stringTable.add(token.getStringTypeString(), token), token));\r
-                }\r
-                else {\r
-                    decl.add(getDataTypeDeclaration(token));\r
-                    if (token.hasDefaultValue()) {\r
-                        inst.add(getDataTypeInstantiation(token));\r
-                    }\r
-                }\r
-            }\r
-\r
-            sizeTable.add(token);\r
-            localTokenNumberTable.add(token);\r
-            token.tokenNumber = assignedTokenNumber++;\r
-\r
-        }\r
-\r
-        map.put("Declaration",  decl);\r
-        map.put("DeclarationForSku", declForSkuEnableType);\r
-        map.put("Instantiation", inst);\r
-        map.put("InstantiationForSku", instForSkuEnableType);\r
-\r
-        return map;\r
-    }\r
-\r
-    private String getSkuEnabledTypeDeclaration (Token token) {\r
-        return String.format("%-20s%s;\r\n", "SKU_HEAD", token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) {\r
-\r
-        String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString());\r
-        return String.format("{ %s, %d } /* SKU_ENABLED: %s */", offsetof, SkuTableIdx, token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getDataTypeDeclarationForSkuEnabled (Token token) {\r
-        String typeStr = "";\r
-\r
-        if (token.datumType == Token.DATUM_TYPE.UINT8) {\r
-            typeStr = "UINT8 %s_%s[%d];\r\n";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT16) {\r
-            typeStr = "UINT16 %s_%s[%d];\r\n";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT32) {\r
-            typeStr = "UINT32 %s_%s[%d];\r\n";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT64) {\r
-            typeStr = "UINT64 %s_%s[%d];\r\n";\r
-        } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {\r
-            typeStr = "BOOLEAN %s_%s[%d];\r\n";\r
-        } else if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
-            return String.format("UINT8 %s_%s[%d];\r\n", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.skuData.size());\r
-        } \r
-\r
-        return String.format(typeStr, token.getPrimaryKeyString(), "SkuDataTable", token.skuData.size());\r
-\r
-    }\r
-\r
-    private String getDataTypeInstantiationForSkuEnabled (Token token) {\r
-        String str = "";\r
-\r
-        if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
-            return String.format("UINT8 %s_%s[%d]", token.getPrimaryKeyString(), "SkuDataTable", token.datumSize * token.skuData.size());\r
-        } else {\r
-            str = "{ ";\r
-            for (int idx = 0; idx < token.skuData.size(); idx++) {\r
-                str += token.skuData.get(idx).toString();\r
-                if (idx != token.skuData.size() - 1) {\r
-                    str += ", ";\r
-                }\r
-            }\r
-            str += "}";\r
-\r
-            return str;\r
-        }\r
-\r
-    }\r
-\r
-    private String getDataTypeInstantiationForVariableDefault_new (Token token, String cName, int skuId) {\r
-        return String.format("%s /* %s */", token.skuData.get(skuId).value.hiiDefaultValue, cName);\r
-    }\r
-\r
-    private String getDataTypeInstantiation (Token token) {\r
-\r
-        if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
-            return String.format("%s /* %s */", token.getDefaultSku().value, token.getPrimaryKeyString());\r
-        } else {\r
-            return String.format("%s /* %s */", token.getDefaultSku().value, token.getPrimaryKeyString());\r
-        }\r
-    }\r
+    private String getDataTypeInstantiationForVariableDefault (Token token, String cName, int skuId) {\r
+        return String.format("%s /* %s */", token.skuData.get(skuId).value.hiiDefaultValue, cName);\r
+    }\r
 \r
     private String getCType (Token t) \r
         throws EntityException {\r
@@ -1590,6 +1357,9 @@ class PcdDatabase {
         }\r
     }\r
     \r
+    //\r
+    // privateGlobalName and privateGlobalCCode is used to pass output to caller of getCDeclarationString\r
+    //\r
     private void getCDeclarationString(Token t) \r
         throws EntityException {\r
         \r
@@ -1599,28 +1369,25 @@ class PcdDatabase {
             privateGlobalName = t.getPrimaryKeyString();\r
         }\r
 \r
-        if (t.isUnicodeStringType()) {\r
-            privateGlobalCCode = String.format("%-20s%s[%d];\r\n", "STRING_HEAD", t.getPrimaryKeyString(), t.getSkuIdCount());\r
-        } else {\r
-            String type = getCType(t);\r
-            if (t.datumType == Token.DATUM_TYPE.POINTER) {\r
-                int bufferSize;\r
-                if (t.isASCIIStringType()) {\r
-                    //\r
-                    // Build tool will add a NULL string at the end of the ASCII string\r
-                    //\r
-                    bufferSize = t.datumSize + 1;\r
-                } else {\r
-                    bufferSize = t.datumSize;\r
-                }\r
-                privateGlobalCCode = String.format("%-20s%s[%d][%d];\r\n", type, privateGlobalName, t.getSkuIdCount(), bufferSize);\r
+        String type = getCType(t);\r
+        if ((t.datumType == Token.DATUM_TYPE.POINTER) && (!t.isHiiEnable()) && (!t.isUnicodeStringType())) {\r
+            int bufferSize;\r
+            if (t.isASCIIStringType()) {\r
+                //\r
+                // Build tool will add a NULL string at the end of the ASCII string\r
+                //\r
+                bufferSize = t.datumSize + 1;\r
             } else {\r
-                privateGlobalCCode = String.format("%-20s%s[%d];\r\n", type, privateGlobalName, t.getSkuIdCount());\r
+                bufferSize = t.datumSize;\r
             }\r
+            privateGlobalCCode = String.format("%-20s%s[%d][%d];\r\n", type, privateGlobalName, t.getSkuIdCount(), bufferSize);\r
+        } else {\r
+            privateGlobalCCode = String.format("%-20s%s[%d];\r\n", type, privateGlobalName, t.getSkuIdCount());\r
         }\r
     }\r
     \r
-    private String getDataTypeDeclarationForVariableDefault_new (Token token, String cName, int skuId) {\r
+    private String getDataTypeDeclarationForVariableDefault (Token token, String cName, int skuId) \r
+        throws EntityException {\r
 \r
         String typeStr;\r
 \r
@@ -1635,40 +1402,32 @@ class PcdDatabase {
         } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {\r
             typeStr = "BOOLEAN";\r
         } else if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
-            return String.format("%-20s%s[%d];\r\n", cName, token.datumSize);\r
+            int size;\r
+            if (token.isHiiDefaultValueUnicodeStringType()) {\r
+                typeStr = "UINT16";\r
+                //\r
+                // Include the NULL charactor\r
+                //\r
+                size = token.datumSize / 2 + 1;\r
+            } else {\r
+                typeStr = "UINT8";\r
+                if (token.isHiiDefaultValueASCIIStringType()) {\r
+                    //\r
+                    // Include the NULL charactor\r
+                    //\r
+                    size = token.datumSize + 1;\r
+                } else {\r
+                    size = token.datumSize;\r
+                }\r
+            }\r
+            return String.format("%-20s%s[%d];\r\n", typeStr, cName, size);\r
         } else {\r
-            typeStr = "";\r
+            throw new EntityException("Unknown DATUM_TYPE type in when generating code for VARIABLE_ENABLED PCD entry");\r
         }\r
 \r
         return String.format("%-20s%s;\r\n", typeStr, cName);\r
     }\r
     \r
-    private String getDataTypeDeclaration (Token token) {\r
-\r
-        String typeStr = "";\r
-\r
-        if (token.datumType == Token.DATUM_TYPE.UINT8) {\r
-            typeStr = "UINT8";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT16) {\r
-            typeStr = "UINT16";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT32) {\r
-            typeStr = "UINT32";\r
-        } else if (token.datumType == Token.DATUM_TYPE.UINT64) {\r
-            typeStr = "UINT64";\r
-        } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) {\r
-            typeStr = "BOOLEAN";\r
-        } else if (token.datumType == Token.DATUM_TYPE.POINTER) {\r
-            return String.format("UINT8 %s[%d]", token.getPrimaryKeyString(), token.datumSize);\r
-        } else {\r
-        }\r
-\r
-        return String.format("%s    %s", typeStr, token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getVpdEnableTypeDeclaration (Token token) {\r
-        return String.format("VPD_HEAD %s", token.getPrimaryKeyString());\r
-    }\r
-    \r
     private String getTypeInstantiation (Token t, ArrayList<CStructTypeDeclaration> declaList, HashMap<String, String> instTable, String phase) throws EntityException {\r
       \r
         int     i;\r
@@ -1678,7 +1437,7 @@ class PcdDatabase {
         s += tab + "{" + newLine;\r
 \r
         for (i = 0; i < t.skuData.size(); i++) {\r
-            if (t.isUnicodeStringType() && !t.isHiiEnable()) {\r
+            if (t.isUnicodeStringType()) {\r
                 s += tab + tab + String.format("{ %d }", stringTable.add(t.skuData.get(i).value.value, t));\r
             } else if (t.isHiiEnable()) {\r
                 /* VPD_HEAD definition\r
@@ -1686,6 +1445,7 @@ class PcdDatabase {
                       UINT16  GuidTableIndex;   // Offset in Guid Table in units of GUID.\r
                       UINT16  StringIndex;      // Offset in String Table in units of UINT16.\r
                       UINT16  Offset;           // Offset in Variable\r
+                      UINT16  DefaultValueOffset; // Offset of the Default Value\r
                     } VARIABLE_HEAD  ;\r
                  */\r
                 String variableDefaultName = String.format("%s_VariableDefault_%d", t.getPrimaryKeyString(), i); \r
@@ -1700,12 +1460,12 @@ class PcdDatabase {
                 // the instantiation for the default value.\r
                 //\r
                 CStructTypeDeclaration decl = new CStructTypeDeclaration (variableDefaultName,\r
-                                                        getDataTypeAlignmentSize(t),\r
-                                                        getDataTypeDeclarationForVariableDefault_new(t, variableDefaultName, i),\r
+                                                        getHiiPtrTypeAlignmentSize(t),\r
+                                                        getDataTypeDeclarationForVariableDefault(t, variableDefaultName, i),\r
                                                         true\r
                                                         ); \r
                 declaList.add(decl);\r
-                instTable.put(variableDefaultName, getDataTypeInstantiationForVariableDefault_new (t, variableDefaultName, i));\r
+                instTable.put(variableDefaultName, getDataTypeInstantiationForVariableDefault (t, variableDefaultName, i));\r
             } else if (t.isVpdEnable()) {\r
                     /* typedef  struct {\r
                         UINT32  Offset;\r
@@ -1736,40 +1496,6 @@ class PcdDatabase {
         return s;\r
     }\r
     \r
-    private String getVpdEnableTypeInstantiation (Token token) {\r
-        return String.format("{ %s } /* %s */", token.getDefaultSku().vpdOffset,\r
-                                                token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getStringTypeDeclaration (Token token) {\r
-        return String.format("UINT16  %s", token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getStringTypeInstantiation (int StringTableIdx, Token token) {\r
-        return String.format ("%d /* %s */", StringTableIdx,\r
-                                             token.getPrimaryKeyString()); \r
-    }\r
-\r
-\r
-    private String getVariableEnableTypeDeclaration (Token token) {\r
-      return String.format("VARIABLE_HEAD  %s", token.getPrimaryKeyString());\r
-    }\r
-\r
-    private String getVariableEnableInstantiation (Token token) \r
-        throws EntityException {\r
-        //\r
-        // Need scott fix\r
-        // \r
-        return String.format("{ %d, %d, %s } /* %s */", guidTable.add(token.getDefaultSku().variableGuid, token.getPrimaryKeyString()),\r
-                                                        stringTable.add(token.getDefaultSku().getStringOfVariableName(), token),\r
-                                                        token.getDefaultSku().variableOffset, \r
-                                                        token.getPrimaryKeyString());\r
-    }\r
-\r
-    public int getTotalTokenNumber () {\r
-        return sizeTable.getTableLen();\r
-    }\r
-\r
     public static String getPcdDatabaseCommonDefinitions () \r
         throws EntityException {\r
 \r
@@ -1839,28 +1565,119 @@ class PcdDatabase {
         return retStr;\r
     }\r
 \r
+    /**\r
+       Translate the schema string to UUID instance.\r
+       \r
+       In schema, the string of UUID is defined as following two types string:\r
+        1) GuidArrayType: pattern = 0x[a-fA-F0-9]{1,8},( )*0x[a-fA-F0-9]{1,4},(\r
+        )*0x[a-fA-F0-9]{1,4}(,( )*\{)?(,?( )*0x[a-fA-F0-9]{1,2}){8}( )*(\})?\r
+       \r
+        2) GuidNamingConvention: pattern =\r
+        [a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}\r
+       \r
+       This function will convert string and create uuid instance.\r
+       \r
+       @param uuidString    UUID string in XML file\r
+       \r
+       @return UUID         UUID instance\r
+    **/\r
+    private UUID translateSchemaStringToUUID(String uuidString) \r
+        throws EntityException {\r
+        String      temp;\r
+        String[]    splitStringArray;\r
+        int         index;\r
+        int         chIndex;\r
+        int         chLen;\r
+\r
+        if (uuidString == null) {\r
+            return null;\r
+        }\r
+\r
+        if (uuidString.length() == 0) {\r
+            return null;\r
+        }\r
+\r
+        if (uuidString.equals("0") ||\r
+            uuidString.equalsIgnoreCase("0x0")) {\r
+            return new UUID(0, 0);\r
+        }\r
+\r
+        uuidString = uuidString.replaceAll("\\{", "");\r
+        uuidString = uuidString.replaceAll("\\}", "");\r
+\r
+        //\r
+        // If the UUID schema string is GuidArrayType type then need translate \r
+        // to GuidNamingConvention type at first.\r
+        // \r
+        if ((uuidString.charAt(0) == '0') && ((uuidString.charAt(1) == 'x') || (uuidString.charAt(1) == 'X'))) {\r
+            splitStringArray = uuidString.split("," );\r
+            if (splitStringArray.length != 11) {\r
+                throw new EntityException ("[FPD file error] Wrong format for UUID string: " + uuidString);\r
+            }\r
+\r
+            //\r
+            // Remove blank space from these string and remove header string "0x"\r
+            // \r
+            for (index = 0; index < 11; index ++) {\r
+                splitStringArray[index] = splitStringArray[index].trim();\r
+                splitStringArray[index] = splitStringArray[index].substring(2, splitStringArray[index].length());\r
+            }\r
+\r
+            //\r
+            // Add heading '0' to normalize the string length\r
+            // \r
+            for (index = 3; index < 11; index ++) {\r
+                chLen = splitStringArray[index].length();\r
+                for (chIndex = 0; chIndex < 2 - chLen; chIndex ++) {\r
+                    splitStringArray[index] = "0" + splitStringArray[index];\r
+                }\r
+            }\r
+\r
+            //\r
+            // construct the final GuidNamingConvention string\r
+            // \r
+            temp = String.format("%s-%s-%s-%s%s-%s%s%s%s%s%s",\r
+                                 splitStringArray[0],\r
+                                 splitStringArray[1],\r
+                                 splitStringArray[2],\r
+                                 splitStringArray[3],\r
+                                 splitStringArray[4],\r
+                                 splitStringArray[5],\r
+                                 splitStringArray[6],\r
+                                 splitStringArray[7],\r
+                                 splitStringArray[8],\r
+                                 splitStringArray[9],\r
+                                 splitStringArray[10]);\r
+            uuidString = temp;\r
+        }\r
+\r
+        return UUID.fromString(uuidString);\r
+    }\r
 }\r
 \r
+/** Module Info class is the data structure to hold information got from GlobalData.\r
+*/\r
 class ModuleInfo {\r
-    private String                  type;\r
-    private FpdModuleIdentification moduleId;\r
+    ///\r
+    /// Module's ID for a <ModuleSA>\r
+    /// \r
+    private FpdModuleIdentification                       moduleId;\r
+    ///\r
+    /// <PcdBuildDefinition> xmlobject in FPD file for a <ModuleSA>\r
+    /// \r
     private PcdBuildDefinitionDocument.PcdBuildDefinition pcdBuildDef;\r
-    \r
-    \r
 \r
-    public ModuleInfo (FpdModuleIdentification moduleId, String type, XmlObject pcdDef) {\r
-        this.moduleId = moduleId;\r
-        this.type   = type;\r
-        this.pcdBuildDef = ((PcdBuildDefinitionDocument)pcdDef).getPcdBuildDefinition();\r
-    }\r
-    public String getModuleType (){\r
-       return this.type;\r
+    public ModuleInfo (FpdModuleIdentification moduleId, XmlObject pcdDef) {\r
+        this.moduleId       = moduleId;\r
+        this.pcdBuildDef    = ((PcdBuildDefinitionDocument)pcdDef).getPcdBuildDefinition();\r
     }\r
+\r
     public FpdModuleIdentification getModuleId (){\r
-       return this.moduleId;\r
+       return moduleId;\r
     }\r
+\r
     public PcdBuildDefinitionDocument.PcdBuildDefinition getPcdBuildDef(){\r
-       return this.pcdBuildDef;\r
+       return pcdBuildDef;\r
     }\r
 }\r
 \r
@@ -1869,22 +1686,29 @@ class ModuleInfo {
     from buildAction or UIAction.\r
 **/\r
 public class CollectPCDAction {\r
+    ///\r
     /// memoryDatabase hold all PCD information collected from SPD, MSA, FPD.\r
+    /// \r
     private MemoryDatabaseManager dbManager;\r
-\r
+    ///\r
     /// Workspacepath hold the workspace information.\r
+    /// \r
     private String                workspacePath;\r
-\r
+    ///\r
     /// FPD file is the root file. \r
+    /// \r
     private String                fpdFilePath;\r
-\r
+    ///\r
     /// Message level for CollectPCDAction.\r
+    /// \r
     private int                   originalMessageLevel;\r
-\r
+    ///\r
     /// Cache the fpd docment instance for private usage.\r
+    /// \r
     private PlatformSurfaceAreaDocument fpdDocInstance;\r
-    \r
+    ///\r
     /// xmlObject name\r
+    /// \r
     private static String xmlObjectName = "PcdBuildDefinition"; \r
        \r
     /**\r
@@ -1971,8 +1795,7 @@ public class CollectPCDAction {
         createTokenInDBFromFPD();\r
         \r
         //\r
-        // Call Private function genPcdDatabaseSourceCode (void); ComponentTypeBsDriver\r
-        // 1) Generate for PEI, DXE PCD DATABASE's definition and initialization.\r
+        // Generate for PEI, DXE PCD DATABASE's definition and initialization.\r
         //\r
         genPcdDatabaseSourceCode ();\r
         \r
@@ -1987,25 +1810,22 @@ public class CollectPCDAction {
     **/\r
     private void genPcdDatabaseSourceCode()\r
         throws EntityException {\r
-        String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions ();\r
+        String PcdCommonHeaderString = PcdDatabase.getPcdDatabaseCommonDefinitions();\r
 \r
         ArrayList<Token> alPei = new ArrayList<Token> ();\r
         ArrayList<Token> alDxe = new ArrayList<Token> ();\r
 \r
         dbManager.getTwoPhaseDynamicRecordArray(alPei, alDxe);\r
         PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0);\r
-        pcdPeiDatabase.genCodeNew();\r
-        MemoryDatabaseManager.PcdPeimHString        = PcdCommonHeaderString + pcdPeiDatabase.getHString()\r
-                                            + PcdDatabase.getPcdPeiDatabaseDefinitions();\r
+        pcdPeiDatabase.genCode();\r
+        MemoryDatabaseManager.PcdPeimHString        = PcdCommonHeaderString + pcdPeiDatabase.getHString() + \r
+                                                      PcdDatabase.getPcdPeiDatabaseDefinitions();\r
         MemoryDatabaseManager.PcdPeimCString        = pcdPeiDatabase.getCString();\r
 \r
-        PcdDatabase pcdDxeDatabase = new PcdDatabase (alDxe, \r
-                                                      "DXE",\r
-                                                      alPei.size()\r
-                                                      );\r
-        pcdDxeDatabase.genCodeNew();\r
-        MemoryDatabaseManager.PcdDxeHString   = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString()\r
-                                      + PcdDatabase.getPcdDxeDatabaseDefinitions();\r
+        PcdDatabase pcdDxeDatabase = new PcdDatabase(alDxe, "DXE", alPei.size());\r
+        pcdDxeDatabase.genCode();\r
+        MemoryDatabaseManager.PcdDxeHString   = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString() + \r
+                                                PcdDatabase.getPcdDxeDatabaseDefinitions();\r
         MemoryDatabaseManager.PcdDxeCString   = pcdDxeDatabase.getCString();\r
     }\r
 \r
@@ -2019,46 +1839,24 @@ public class CollectPCDAction {
      */\r
     private List<ModuleInfo> getComponentsFromFPD() \r
         throws EntityException {\r
-        List<ModuleInfo>            allModules  = new ArrayList<ModuleInfo>();\r
-        ModuleInfo                  current     = null;\r
-        int                         index       = 0;\r
-        FrameworkModulesDocument.FrameworkModules fModules = null;\r
-        ModuleSADocument.ModuleSA[]               modules  = null;\r
-        HashMap<String, XmlObject>                map      = new HashMap<String, XmlObject>();\r
-\r
-        if (fpdDocInstance == null) {\r
-            try {\r
-                fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
-            } catch(IOException ioE) {\r
-                throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
-            } catch(XmlException xmlE) {\r
-                throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
-            }\r
+        List<ModuleInfo>                            allModules          = new ArrayList<ModuleInfo>();\r
+        FrameworkModulesDocument.FrameworkModules   fModules            = null;\r
+        ModuleSADocument.ModuleSA[]                 modules             = null;\r
+        Map<FpdModuleIdentification, XmlObject>     pcdBuildDefinitions = null;\r
 \r
+        pcdBuildDefinitions = GlobalData.getFpdPcdBuildDefinitions();\r
+        if (pcdBuildDefinitions == null) {\r
+            return null;\r
         }\r
 \r
-        //map.put("FrameworkPlatformDescription", fpdDocInstance);\r
-        //SurfaceAreaQuery.setDoc(map); \r
-        Map<FpdModuleIdentification,XmlObject>pcdBuildDef = GlobalData.getFpdModuleSaXmlObject(CollectPCDAction.xmlObjectName);\r
-        Set<FpdModuleIdentification> pcdBuildKeySet = pcdBuildDef.keySet();\r
-        Iterator item = pcdBuildKeySet.iterator();\r
+        //\r
+        // Loop map to retrieve all PCD build definition and Module id \r
+        // \r
+        Iterator item = pcdBuildDefinitions.keySet().iterator();\r
         while (item.hasNext()){\r
-               FpdModuleIdentification id = (FpdModuleIdentification)item.next();\r
-            try {\r
-                allModules.add(new ModuleInfo(id, id.getModule().getModuleType(),pcdBuildDef.get(id)));    \r
-            } catch (Exception e){\r
-                System.out.println(e.getMessage());\r
-                //EdkLog.log(EdkLog.EDK_INFO,e.getMessage());\r
-            }\r
-               \r
-               \r
-        }\r
-//        //modules = SurfaceAreaQuery.getFpdModuleSAs();\r
-//        for (index = 0; index < modules.length; index ++) {\r
-//            //SurfaceAreaQuery.setDoc(GlobalData.getDoc(modules[index].getModuleName()));\r
-//            allModules.add(new ModuleInfo(modules[index], \r
-//                                          ModuleTypeDef.Enum.forString(SurfaceAreaQuery.getModuleType())));\r
-//        }\r
+            FpdModuleIdentification id = (FpdModuleIdentification) item.next();\r
+            allModules.add(new ModuleInfo(id, pcdBuildDefinitions.get(id)));    \r
+        }\r
         \r
         return allModules;\r
     }\r
@@ -2081,8 +1879,6 @@ public class CollectPCDAction {
         List<PcdBuildDefinition.PcdData>    pcdBuildDataArray = new ArrayList<PcdBuildDefinition.PcdData>();\r
         PcdBuildDefinition.PcdData          pcdBuildData      = null;\r
         Token                               token             = null;\r
-        SkuInstance                         skuInstance       = null;\r
-        int                                 skuIndex          = 0;\r
         List<ModuleInfo>                    modules           = null;\r
         String                              primaryKey        = null;\r
         String                              exceptionString   = null;\r
@@ -2092,12 +1888,12 @@ public class CollectPCDAction {
         boolean                             isDuplicate       = false;\r
         Token.PCD_TYPE                      pcdType           = Token.PCD_TYPE.UNKNOWN;\r
         Token.DATUM_TYPE                    datumType         = Token.DATUM_TYPE.UNKNOWN;\r
-        long                                 tokenNumber       = 0;\r
+        long                                tokenNumber       = 0;\r
         String                              moduleName        = null;\r
         String                              datum             = null;\r
         int                                 maxDatumSize      = 0;\r
-        String                                                         tokenSpaceGuidString = null;\r
-        \r
+        String[]                            tokenSpaceStrRet  = null;\r
+\r
         //\r
         // ----------------------------------------------\r
         // 1), Get all <ModuleSA> from FPD file.\r
@@ -2115,34 +1911,6 @@ public class CollectPCDAction {
         // -------------------------------------------------------------------\r
         // \r
         for (index = 0; index < modules.size(); index ++) {\r
-            isDuplicate =  false;\r
-            for (index2 = 0; index2 < index; index2 ++) {\r
-                //\r
-                // BUGBUG: For transition schema, we can *not* get module's version from \r
-                // <ModuleSAs>, It is work around code.\r
-                // \r
-                primaryKey1 = UsageInstance.getPrimaryKey(modules.get(index).getModuleId().getModule().getName(), \r
-                                                          null,\r
-                                                          null,\r
-                                                          null, \r
-                                                          modules.get(index).getModuleId().getArch(),\r
-                                                          null);\r
-                primaryKey2 = UsageInstance.getPrimaryKey(modules.get(index2).getModuleId().getModule().getName(), \r
-                                                          null, \r
-                                                          null, \r
-                                                          null, \r
-                                                          modules.get(index2).getModuleId().getArch(), \r
-                                                          null);\r
-                if (primaryKey1.equalsIgnoreCase(primaryKey2)) {\r
-                    isDuplicate = true;\r
-                    break;\r
-                }\r
-            }\r
-\r
-            if (isDuplicate) {\r
-                continue;\r
-            }\r
-\r
            //\r
            // It is legal for a module does not contains ANY pcd build definitions.\r
            // \r
@@ -2160,11 +1928,19 @@ public class CollectPCDAction {
             // ----------------------------------------------------------------------\r
             // \r
             for (pcdIndex = 0; pcdIndex < pcdBuildDataArray.size(); pcdIndex ++) {\r
-               //tokenSpaceGuidString = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName())[1];\r
-                tokenSpaceGuidString = null;\r
                 pcdBuildData = pcdBuildDataArray.get(pcdIndex);\r
-                primaryKey   = Token.getPrimaryKeyString(pcdBuildData.getCName(),\r
-                                                         translateSchemaStringToUUID(tokenSpaceGuidString));\r
+                \r
+                try {\r
+                    tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName());\r
+                } catch ( Exception e ) {\r
+                    throw new EntityException ("Faile get Guid for token " + pcdBuildData.getCName() + ":" + e.getMessage());\r
+                }\r
+\r
+                if (tokenSpaceStrRet == null) {\r
+                    throw new EntityException ("Fail to get Token space guid for token" + pcdBuildData.getCName());\r
+                } \r
+\r
+                primaryKey   = Token.getPrimaryKeyString(pcdBuildData.getCName(), tokenSpaceStrRet[1]);\r
                 pcdType      = Token.getpcdTypeFromString(pcdBuildData.getItemType().toString());\r
                 datumType    = Token.getdatumTypeFromString(pcdBuildData.getDatumType().toString());\r
                 tokenNumber  = Long.decode(pcdBuildData.getToken().toString());\r
@@ -2293,10 +2069,17 @@ public class CollectPCDAction {
                     // If the token is not in database, create a new token instance and add\r
                     // a usage instance into this token in database.\r
                     // \r
-                       //String tokenSpaceString = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName())[1];\r
-                    String tokenSpaceString = null;\r
-                    token = new Token(pcdBuildData.getCName(), \r
-                                      translateSchemaStringToUUID(tokenSpaceString));\r
+                    try {\r
+                        tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName());\r
+                    } catch (Exception e) {\r
+                        throw new EntityException("Fail to get token space guid for token " + token.cName);\r
+                    }\r
+\r
+                    if (tokenSpaceStrRet == null) {\r
+                        throw new EntityException("Fail to get token space guid for token " + token.cName);\r
+                    }\r
+\r
+                    token = new Token(pcdBuildData.getCName(), tokenSpaceStrRet[1]);\r
     \r
                     token.datumType     = datumType;\r
                     token.tokenNumber   = tokenNumber;\r
@@ -2330,19 +2113,253 @@ public class CollectPCDAction {
                 // ------------------------------------------------\r
                 // \r
                 usageInstance = new UsageInstance(token, \r
-                                                  moduleName, \r
-                                                  null,\r
-                                                  null,\r
-                                                  null,\r
-                                                  CommonDefinition.getModuleType(modules.get(index).getModuleType()), \r
+                                                  modules.get(index).getModuleId().getModule(), \r
                                                   pcdType,\r
                                                   modules.get(index).getModuleId().getArch(), \r
-                                                  null,\r
                                                   datum,\r
                                                   maxDatumSize);\r
                 token.addUsageInstance(usageInstance);\r
             }\r
         }\r
+\r
+        //\r
+        // ------------------------------------------------\r
+        // 3), Add unreference dynamic_Ex pcd token into Pcd database.\r
+        // ------------------------------------------------\r
+        // \r
+        List<Token> tokenArray = getUnreferencedDynamicPcd();\r
+        if (tokenArray != null) {\r
+            for (index = 0; index < tokenArray.size(); index ++) {\r
+                dbManager.addTokenToDatabase(tokenArray.get(index).getPrimaryKeyString(), \r
+                                             tokenArray.get(index));\r
+            }\r
+        }\r
+    }\r
+\r
+    private List<Token> getUnreferencedDynamicPcd () throws EntityException {\r
+        List<Token>                                   tokenArray                 = new ArrayList<Token>();\r
+        Token                                         token                      = null;\r
+        DynamicPcdBuildDefinitions                    dynamicPcdBuildDefinitions = null;\r
+        List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray   = null;\r
+        DynamicPcdBuildDefinitions.PcdBuildData       pcdBuildData               = null;\r
+        List<DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo>   skuInfoList      = null;\r
+        Token.PCD_TYPE                                pcdType;\r
+        SkuInstance                                   skuInstance                = null;\r
+        String  primaryKey = null;\r
+        boolean hasSkuId0  = false;\r
+        int     index, offset, index2;\r
+        String  temp;\r
+        String  exceptionString;\r
+        String  hiiDefaultValue;\r
+        String  tokenSpaceStrRet[];\r
+        String  variableGuidString[];\r
+\r
+        //\r
+        // Open fpd document to get <DynamicPcdBuildDefinition> Section.\r
+        // BUGBUG: the function should be move GlobalData in furture.\r
+        // \r
+        if (fpdDocInstance == null) {\r
+            try {\r
+                fpdDocInstance = (PlatformSurfaceAreaDocument)XmlObject.Factory.parse(new File(fpdFilePath));\r
+            } catch(IOException ioE) {\r
+                throw new EntityException("File IO error for xml file:" + fpdFilePath + "\n" + ioE.getMessage());\r
+            } catch(XmlException xmlE) {\r
+                throw new EntityException("Can't parse the FPD xml fle:" + fpdFilePath + "\n" + xmlE.getMessage());\r
+            }\r
+        }\r
+\r
+        dynamicPcdBuildDefinitions = fpdDocInstance.getPlatformSurfaceArea().getDynamicPcdBuildDefinitions();\r
+        if (dynamicPcdBuildDefinitions == null) {\r
+            return null;\r
+        }\r
+\r
+        dynamicPcdBuildDataArray = dynamicPcdBuildDefinitions.getPcdBuildDataList();\r
+        for (index2 = 0; index2 < dynamicPcdBuildDataArray.size(); index2 ++) {\r
+            pcdBuildData = dynamicPcdBuildDataArray.get(index2);\r
+            try {\r
+                tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(pcdBuildData.getTokenSpaceGuidCName());\r
+            } catch ( Exception e ) {\r
+                throw new EntityException ("Faile get Guid for token " + pcdBuildData.getCName() + ":" + e.getMessage());\r
+            }\r
+\r
+            if (tokenSpaceStrRet == null) {\r
+                throw new EntityException ("Fail to get Token space guid for token" + pcdBuildData.getCName());\r
+            } \r
+\r
+            primaryKey = Token.getPrimaryKeyString(pcdBuildData.getCName(),\r
+                                                   tokenSpaceStrRet[1]);\r
+\r
+            if (dbManager.isTokenInDatabase(primaryKey)) {\r
+                continue;\r
+            }\r
+\r
+            pcdType = Token.getpcdTypeFromString(pcdBuildData.getItemType().toString());\r
+            if (pcdType != Token.PCD_TYPE.DYNAMIC_EX) {\r
+                throw new EntityException (String.format("[FPD file error] It not allowed for DYNAMIC PCD %s who is no used by any module",\r
+                                                         pcdBuildData.getCName()));\r
+            }\r
+\r
+            //\r
+            // Create new token for unreference dynamic PCD token\r
+            // \r
+            token           = new Token(pcdBuildData.getCName(), tokenSpaceStrRet[1]);\r
+            token.datumSize = pcdBuildData.getMaxDatumSize();\r
+            \r
+\r
+            token.datumType     = Token.getdatumTypeFromString(pcdBuildData.getDatumType().toString());\r
+            token.tokenNumber   = Long.decode(pcdBuildData.getToken().toString());\r
+            token.dynamicExTokenNumber = token.tokenNumber;\r
+            token.isDynamicPCD  = true; \r
+            token.updateSupportPcdType(pcdType);\r
+\r
+            exceptionString = verifyDatum(token.cName, \r
+                                          null,\r
+                                          null, \r
+                                          token.datumType, \r
+                                          token.datumSize);\r
+            if (exceptionString != null) {\r
+                throw new EntityException(exceptionString);\r
+            }\r
+\r
+            skuInfoList = pcdBuildData.getSkuInfoList();\r
+\r
+            //\r
+            // Loop all sku data \r
+            // \r
+            for (index = 0; index < skuInfoList.size(); index ++) {\r
+                skuInstance = new SkuInstance();\r
+                //\r
+                // Although SkuId in schema is BigInteger, but in fact, sku id is 32 bit value.\r
+                // \r
+                temp = skuInfoList.get(index).getSkuId().toString();\r
+                skuInstance.id = Integer.decode(temp);\r
+                if (skuInstance.id == 0) {\r
+                    hasSkuId0 = true;\r
+                }\r
+                //\r
+                // Judge whether is DefaultGroup at first, because most case is DefautlGroup.\r
+                // \r
+                if (skuInfoList.get(index).getValue() != null) {\r
+                    skuInstance.value.setValue(skuInfoList.get(index).getValue().toString());\r
+                    if ((exceptionString = verifyDatum(token.cName, \r
+                                                       null, \r
+                                                       skuInfoList.get(index).getValue().toString(), \r
+                                                       token.datumType, \r
+                                                       token.datumSize)) != null) {\r
+                        throw new EntityException(exceptionString);\r
+                    }\r
+\r
+                    token.skuData.add(skuInstance);\r
+\r
+                    continue;\r
+                }\r
+\r
+                //\r
+                // Judge whether is HII group case.\r
+                // \r
+                if (skuInfoList.get(index).getVariableName() != null) {\r
+                    exceptionString = null;\r
+                    if (skuInfoList.get(index).getVariableGuid() == null) {\r
+                        exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
+                                                        "file, who use HII, but there is no <VariableGuid> defined for Sku %d data!",\r
+                                                        token.cName,\r
+                                                        index);\r
+                        if (exceptionString != null) {\r
+                            throw new EntityException(exceptionString);\r
+                        }                                                    \r
+                    }\r
+\r
+                    if (skuInfoList.get(index).getVariableOffset() == null) {\r
+                        exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
+                                                        "file, who use HII, but there is no <VariableOffset> defined for Sku %d data!",\r
+                                                        token.cName,\r
+                                                        index);\r
+                        if (exceptionString != null) {\r
+                            throw new EntityException(exceptionString);\r
+                        }\r
+                    }\r
+\r
+                    if (skuInfoList.get(index).getHiiDefaultValue() == null) {\r
+                        exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions> section in FPD "+\r
+                                                        "file, who use HII, but there is no <HiiDefaultValue> defined for Sku %d data!",\r
+                                                        token.cName,\r
+                                                        index);\r
+                        if (exceptionString != null) {\r
+                            throw new EntityException(exceptionString);\r
+                        }\r
+                    }\r
+\r
+                    if (skuInfoList.get(index).getHiiDefaultValue() != null) {\r
+                        hiiDefaultValue = skuInfoList.get(index).getHiiDefaultValue().toString();\r
+                    } else {\r
+                        hiiDefaultValue = null;\r
+                    }\r
+\r
+                    if ((exceptionString = verifyDatum(token.cName, \r
+                                                       null, \r
+                                                       hiiDefaultValue, \r
+                                                       token.datumType, \r
+                                                       token.datumSize)) != null) {\r
+                        throw new EntityException(exceptionString);\r
+                    }\r
+\r
+                    offset = Integer.decode(skuInfoList.get(index).getVariableOffset());\r
+                    if (offset > 0xFFFF) {\r
+                        throw new EntityException(String.format("[FPD file error] For dynamic PCD %s ,  the variable offset defined in sku %d data "+\r
+                                                                "exceed 64K, it is not allowed!",\r
+                                                                token.cName,\r
+                                                                index));\r
+                    }\r
+\r
+                    //\r
+                    // Get variable guid string according to the name of guid which will be mapped into a GUID in SPD file.\r
+                    // \r
+                    variableGuidString = GlobalData.getGuidInfoFromCname(skuInfoList.get(index).getVariableGuid().toString());\r
+                    if (variableGuidString == null) {\r
+                        throw new EntityException(String.format("[GUID Error] For dynamic PCD %s,  the variable guid %s can be found in all SPD file!",\r
+                                                                token.cName, \r
+                                                                skuInfoList.get(index).getVariableGuid().toString()));\r
+                    }\r
+                    String variableStr = skuInfoList.get(index).getVariableName();\r
+                    Pattern pattern = Pattern.compile("0x([a-fA-F0-9]){4}");\r
+                    Matcher matcher = pattern.matcher(variableStr);\r
+                    List<String> varNameList = new ArrayList<String>();\r
+                    while (matcher.find()){\r
+                            String str = variableStr.substring(matcher.start(),matcher.end());\r
+                            varNameList.add(str);\r
+                    }\r
+\r
+                    skuInstance.value.setHiiData(varNameList,\r
+                                                 translateSchemaStringToUUID(variableGuidString[1]),\r
+                                                 skuInfoList.get(index).getVariableOffset(),\r
+                                                 skuInfoList.get(index).getHiiDefaultValue().toString());\r
+                    token.skuData.add(skuInstance);\r
+                    continue;\r
+                }\r
+\r
+                if (skuInfoList.get(index).getVpdOffset() != null) {\r
+                    skuInstance.value.setVpdData(skuInfoList.get(index).getVpdOffset());\r
+                    token.skuData.add(skuInstance);\r
+                    continue;\r
+                }\r
+\r
+                exceptionString = String.format("[FPD file error] For dynamic PCD %s, the dynamic info must "+\r
+                                                "be one of 'DefaultGroup', 'HIIGroup', 'VpdGroup'.",\r
+                                                token.cName);\r
+                throw new EntityException(exceptionString);\r
+            }\r
+\r
+            if (!hasSkuId0) {\r
+                exceptionString = String.format("[FPD file error] For dynamic PCD %s in <DynamicPcdBuildDefinitions>, there are "+\r
+                                                "no sku id = 0 data, which is required for every dynamic PCD",\r
+                                                token.cName);\r
+                throw new EntityException(exceptionString);\r
+            }\r
+\r
+            tokenArray.add(token);\r
+        }\r
+\r
+        return tokenArray;\r
     }\r
 \r
     /**\r
@@ -2615,7 +2632,12 @@ public class CollectPCDAction {
                 strValue        = datum.substring(start + 1, end);\r
                 strValue        = strValue.trim();\r
                 if (strValue.length() == 0) {\r
-                    break;\r
+                    exceptionString = String.format ("[FPD file error] The datum type of PCD %s in %s is VOID*, and "+\r
+                                                     "it is byte array in fact, but '{}' is not valid for NULL datam but"+\r
+                                                     " need use '{0}'",\r
+                                                     cName,\r
+                                                     moduleName);\r
+                    return exceptionString;\r
                 }\r
                 strValueArray   = strValue.split(",");\r
                 for (index = 0; index < strValueArray.length; index ++) {\r
@@ -2689,9 +2711,11 @@ public class CollectPCDAction {
         String dynamicPrimaryKey = null;\r
         DynamicPcdBuildDefinitions                    dynamicPcdBuildDefinitions = null;\r
         List<DynamicPcdBuildDefinitions.PcdBuildData> dynamicPcdBuildDataArray   = null;\r
+        String[]                                      tokenSpaceStrRet           = null;\r
 \r
         //\r
         // If FPD document is not be opened, open and initialize it.\r
+        // BUGBUG: The code should be moved into GlobalData in future.\r
         // \r
         if (fpdDocInstance == null) {\r
             try {\r
@@ -2716,8 +2740,18 @@ public class CollectPCDAction {
         for (index = 0; index < dynamicPcdBuildDataArray.size(); index ++) {\r
             //String tokenSpaceGuidString = GlobalData.getGuidInfoFromCname(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName())[1];\r
             String tokenSpaceGuidString = null;\r
+            try {\r
+                tokenSpaceStrRet = GlobalData.getGuidInfoFromCname(dynamicPcdBuildDataArray.get(index).getTokenSpaceGuidCName());\r
+            } catch (Exception e) {\r
+                throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName());\r
+            }\r
+            \r
+            if (tokenSpaceStrRet == null) {\r
+                throw new EntityException ("Fail to get token space guid for token " + dynamicPcdBuildDataArray.get(index).getCName());\r
+            }\r
+\r
             dynamicPrimaryKey = Token.getPrimaryKeyString(dynamicPcdBuildDataArray.get(index).getCName(),\r
-                                                          translateSchemaStringToUUID(tokenSpaceGuidString));\r
+                                                          tokenSpaceStrRet[1]);\r
             if (dynamicPrimaryKey.equalsIgnoreCase(token.getPrimaryKeyString())) {\r
                 return dynamicPcdBuildDataArray.get(index);\r
             }\r
@@ -2802,9 +2836,7 @@ public class CollectPCDAction {
         }\r
 \r
         pcdType = Token.getpcdTypeFromString(dynamicInfo.getItemType().toString());\r
-        if (pcdType == Token.PCD_TYPE.DYNAMIC_EX) {\r
-            token.dynamicExTokenNumber = tokenNumber;\r
-        }\r
+        token.dynamicExTokenNumber = tokenNumber;\r
 \r
         skuInfoList = dynamicInfo.getSkuInfoList();\r
 \r
@@ -3081,11 +3113,16 @@ public class CollectPCDAction {
     **/\r
     public static void main(String argv[]) throws EntityException {\r
         CollectPCDAction ca = new CollectPCDAction();\r
-        ca.setWorkspacePath("m:/tianocore/edk2");\r
-        ca.setFPDFilePath("m:/tianocore/edk2/EdkNt32Pkg/Nt32.fpd");\r
+        String projectDir = "x:/edk2";\r
+        ca.setWorkspacePath(projectDir);\r
+        ca.setFPDFilePath(projectDir + "/EdkNt32Pkg/Nt32.fpd");\r
         ca.setActionMessageLevel(ActionMessage.MAX_MESSAGE_LEVEL);\r
-//        GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
-//                            "m:/tianocore/edk2");\r
-//        ca.execute();\r
+        GlobalData.initInfo("Tools" + File.separator + "Conf" + File.separator + "FrameworkDatabase.db",\r
+                            projectDir,\r
+                            "tools_def.txt");\r
+        System.out.println("After initInfo!");\r
+        FpdParserTask fpt = new FpdParserTask();\r
+        fpt.parseFpdFile(new File(projectDir + "/EdkNt32Pkg/Nt32.fpd"));\r
+        ca.execute();\r
     }\r
 }\r