diff --git a/EdkModulePkg/Universal/PCD/Dxe/Pcd.c b/EdkModulePkg/Universal/PCD/Dxe/Pcd.c index 45a6c5932a..55040fe159 100644 --- a/EdkModulePkg/Universal/PCD/Dxe/Pcd.c +++ b/EdkModulePkg/Universal/PCD/Dxe/Pcd.c @@ -183,7 +183,13 @@ DxePcdGetSize ( ) { UINT16 * SizeTable; - + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + SizeTable = (TokenNumber < PEI_LOCAL_TOKEN_NUMBER) ? mPcdDatabase->PeiDb.Init.SizeTable : mPcdDatabase->DxeDb.Init.SizeTable; @@ -493,7 +499,7 @@ DxeUnRegisterCallBackOnSet ( { ASSERT (CallBackFunction != NULL); - return DxeRegisterCallBackWorker (TokenNumber, Guid, CallBackFunction); + return DxeUnRegisterCallBackWorker (TokenNumber, Guid, CallBackFunction); } diff --git a/EdkModulePkg/Universal/PCD/Dxe/Service.c b/EdkModulePkg/Universal/PCD/Dxe/Service.c index 36e859c779..9513143f26 100644 --- a/EdkModulePkg/Universal/PCD/Dxe/Service.c +++ b/EdkModulePkg/Universal/PCD/Dxe/Service.c @@ -50,10 +50,16 @@ GetWorker ( UINT16 StringTableIdx; UINT32 LocalTokenNumber; - + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ASSERT (TokenNumber < PCD_TOTAL_TOKEN_NUMBER); - Size = DxePcdGetSize (TokenNumber); + Size = DxePcdGetSize (TokenNumber + 1); ASSERT (GetSize == Size || GetSize == 0); @@ -100,11 +106,10 @@ GetWorker ( return (UINT8 *) Data + VariableHead->Offset; } else { // - // BugBug: Need to support default value. The current implementation - // will return a memory buffer with ALL ZERO. - // - return AllocateZeroPool (Size); - } + // Return the default value specified by Platform Integrator + // + return (VOID *) ((UINT8 *) PcdDb + VariableHead->DefaultValueOffset); + } case PCD_TYPE_STRING: StringTableIdx = (UINT16) *((UINT8 *) PcdDb + Offset); @@ -143,6 +148,13 @@ DxeRegisterCallBackWorker ( TokenNumber = GetExPcdTokenNumber (Guid, (UINT32) TokenNumber); } + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ListHead = &mCallbackFnTable[TokenNumber]; ListNode = GetFirstNode (ListHead); @@ -186,6 +198,13 @@ DxeUnRegisterCallBackWorker ( TokenNumber = GetExPcdTokenNumber (Guid, (UINT32) TokenNumber); } + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ListHead = &mCallbackFnTable[TokenNumber]; ListNode = GetFirstNode (ListHead); @@ -342,13 +361,17 @@ GetHiiVariable ( EFI_STATUS Status; VOID *Buffer; + Size = 0; + Buffer = NULL; + Status = EfiGetVariable ( (UINT16 *)VariableName, VariableGuid, NULL, &Size, - NULL + Buffer ); + if (Status == EFI_BUFFER_TOO_SMALL) { Buffer = AllocatePool (Size); @@ -366,6 +389,9 @@ GetHiiVariable ( ASSERT (Status == EFI_SUCCESS); } + *VariableData = Buffer; + *VariableSize = Size; + return Status; } @@ -444,6 +470,13 @@ InvokeCallbackOnSet ( LIST_ENTRY *ListHead; LIST_ENTRY *ListNode; + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ListHead = &mCallbackFnTable[TokenNumber]; ListNode = GetFirstNode (ListHead); @@ -485,13 +518,19 @@ SetWorker ( UINTN Offset; UINT8 *PcdDb; - + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ASSERT (TokenNumber < PCD_TOTAL_TOKEN_NUMBER); if (PtrType) { - ASSERT (Size <= DxePcdGetSize (TokenNumber)); + ASSERT (Size <= DxePcdGetSize (TokenNumber + 1)); } else { - ASSERT (Size == DxePcdGetSize (TokenNumber)); + ASSERT (Size == DxePcdGetSize (TokenNumber + 1)); } IsPeiDb = (TokenNumber < PEI_LOCAL_TOKEN_NUMBER) ? TRUE : FALSE; @@ -501,7 +540,7 @@ SetWorker ( if ((TokenNumber < PEI_NEX_TOKEN_NUMBER) || (TokenNumber >= PEI_LOCAL_TOKEN_NUMBER || TokenNumber < (PEI_LOCAL_TOKEN_NUMBER + DXE_NEX_TOKEN_NUMBER))) { - InvokeCallbackOnSet (0, NULL, TokenNumber, Data, Size); + InvokeCallbackOnSet (0, NULL, TokenNumber + 1, Data, Size); } TokenNumber = IsPeiDb ? TokenNumber diff --git a/EdkModulePkg/Universal/PCD/Dxe/Service.h b/EdkModulePkg/Universal/PCD/Dxe/Service.h index a1757ed056..c8bec08539 100644 --- a/EdkModulePkg/Universal/PCD/Dxe/Service.h +++ b/EdkModulePkg/Universal/PCD/Dxe/Service.h @@ -22,7 +22,7 @@ Module Name: Service.h // Please make sure the PCD Serivce PEIM Version is consistent with // the version of PCD Database generation tool // -#define PCD_DXE_SERVICE_DRIVER_VERSION 1 +#define PCD_DXE_SERVICE_DRIVER_VERSION 2 // // PCD_DXE_DATABASE_GENTOOL_VERSION is defined in Autogen.h diff --git a/EdkModulePkg/Universal/PCD/Pei/Pcd.c b/EdkModulePkg/Universal/PCD/Pei/Pcd.c index b41d3ed09b..34af220f66 100644 --- a/EdkModulePkg/Universal/PCD/Pei/Pcd.c +++ b/EdkModulePkg/Universal/PCD/Pei/Pcd.c @@ -169,6 +169,13 @@ PeiPcdGetSize ( IN UINTN TokenNumber ) { + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER); return GetPcdDatabase()->Init.SizeTable[TokenNumber]; @@ -559,10 +566,10 @@ PeiPcdGetNextToken ( return EFI_SUCCESS; } -EFI_GUID * +EFI_STATUS EFIAPI PeiPcdGetNextTokenSpaceGuid ( - IN CONST EFI_GUID *Guid + IN OUT CONST EFI_GUID **Guid ) { UINTN GuidTableIdx; @@ -572,9 +579,17 @@ PeiPcdGetNextTokenSpaceGuid ( UINTN i; BOOLEAN Found; - if (PEI_EXMAP_TABLE_EMPTY) { - return NULL; - } + if (*Guid == NULL) { + if (PEI_EXMAP_TABLE_EMPTY) { + return EFI_SUCCESS; + } else { + // + // return the first Token Space Guid. + // + *Guid = &PeiPcdDb->Init.GuidTable[ExMapTable[0].ExGuidIndex]; + return EFI_SUCCESS; + } + } // // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order @@ -583,10 +598,10 @@ PeiPcdGetNextTokenSpaceGuid ( // PeiPcdDb = GetPcdDatabase (); - MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid); + MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), *Guid); if (MatchGuid == NULL) { - return NULL; + return EFI_NOT_FOUND; } GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable; @@ -604,16 +619,13 @@ PeiPcdGetNextTokenSpaceGuid ( if (Found) { for ( ; i < PEI_EXMAPPING_TABLE_SIZE; i++ ) { if (ExMapTable[i].ExGuidIndex != GuidTableIdx ) { - if (i < PEI_EXMAPPING_TABLE_SIZE) { - return &PeiPcdDb->Init.GuidTable[ExMapTable[i].ExGuidIndex]; - } else { - return NULL; - } + *Guid = &PeiPcdDb->Init.GuidTable[ExMapTable[i].ExGuidIndex]; + return EFI_SUCCESS; } } } - return NULL; + return EFI_NOT_FOUND; } diff --git a/EdkModulePkg/Universal/PCD/Pei/Service.c b/EdkModulePkg/Universal/PCD/Pei/Service.c index 01fe0151bf..8d03d9c2b1 100644 --- a/EdkModulePkg/Universal/PCD/Pei/Service.c +++ b/EdkModulePkg/Universal/PCD/Pei/Service.c @@ -46,12 +46,27 @@ PeiRegisterCallBackWorker ( if (Guid == NULL) { TokenNumber = ExTokenNumber; + + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; ASSERT (TokenNumber < PEI_NEX_TOKEN_NUMBER); } else { TokenNumber = GetExPcdTokenNumber (Guid, ExTokenNumber); + + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER); } + LocalTokenNumber = GetPcdDatabase()->Init.LocalTokenNumberTable[TokenNumber]; ASSERT ((LocalTokenNumber & PCD_TYPE_HII) == 0); @@ -248,6 +263,13 @@ InvokeCallbackOnSet ( PCD_PPI_CALLBACK *CallbackTable; UINTN Idx; + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + if (Guid == NULL) ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER); @@ -287,6 +309,13 @@ SetWorker ( UINTN Offset; VOID *InternalData; + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER); PeiPcdDb = GetPcdDatabase (); @@ -305,7 +334,7 @@ SetWorker ( // type PCD entry in ExSetWorker. // if (TokenNumber < PEI_NEX_TOKEN_NUMBER) { - InvokeCallbackOnSet (0, NULL, TokenNumber, Data, Size); + InvokeCallbackOnSet (0, NULL, TokenNumber + 1, Data, Size); } if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == PCD_TYPE_SKU_ENABLED) { @@ -425,9 +454,16 @@ GetWorker ( UINT32 LocalTokenNumber; UINTN Size; + // + // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER. + // We have to decrement TokenNumber by 1 to make it usable + // as the array index. + // + TokenNumber--; + ASSERT (TokenNumber < PEI_LOCAL_TOKEN_NUMBER); - Size = PeiPcdGetSize(TokenNumber); + Size = PeiPcdGetSize(TokenNumber + 1); ASSERT (GetSize == Size || GetSize == 0); @@ -464,16 +500,14 @@ GetWorker ( return (VOID *) ((UINT8 *) Data + VariableHead->Offset); } else { // - // BugBug: Need to support default value. The current implementation - // will return a memory buffer with ALL ZERO. - // - return AllocateZeroPool (Size); + // Return the default value specified by Platform Integrator + // + return (VOID *) ((UINT8 *) PeiPcdDb + VariableHead->DefaultValueOffset); } } case PCD_TYPE_DATA: return (VOID *) ((UINT8 *)PeiPcdDb + Offset); - break; case PCD_TYPE_STRING: StringTableIdx = (UINT16) *((UINT8 *) PeiPcdDb + Offset); diff --git a/EdkModulePkg/Universal/PCD/Pei/Service.h b/EdkModulePkg/Universal/PCD/Pei/Service.h index e62d0307a7..563e82ae22 100644 --- a/EdkModulePkg/Universal/PCD/Pei/Service.h +++ b/EdkModulePkg/Universal/PCD/Pei/Service.h @@ -22,7 +22,7 @@ Module Name: Service.h // Please make sure the PCD Serivce PEIM Version is consistent with // the version of PCD Database generation tool // -#define PCD_PEI_SERVICE_DRIVER_VERSION 1 +#define PCD_PEI_SERVICE_DRIVER_VERSION 2 // // PCD_PEI_DATABASE_GENTOOL_VERSION is defined in Autogen.h diff --git a/EdkNt32Pkg/Dxe/WinNtThunk/Bus/WinNtBusDriver/WinNtBusDriver.c b/EdkNt32Pkg/Dxe/WinNtThunk/Bus/WinNtBusDriver/WinNtBusDriver.c index eb071575e0..73a345a431 100644 --- a/EdkNt32Pkg/Dxe/WinNtThunk/Bus/WinNtBusDriver/WinNtBusDriver.c +++ b/EdkNt32Pkg/Dxe/WinNtThunk/Bus/WinNtBusDriver/WinNtBusDriver.c @@ -317,26 +317,6 @@ Returns: CHAR16 *PcdTempStr; UINTN TempStrSize; - // - // Test Feature Set and Binary Patchable Case - // - if (FeaturePcdGet (PcdWinNtFeatureFlag1)) { - TempStrSize = PatchPcdGet32(PcdWinNtBinaryPatch1) + PatchPcdGet32(PcdWinNtBinaryPatch2); - } - - if (0) { - // - // Test Dynamic and DynamicEx - // (Please add PcdWinNtConsole in "WinNtBusDriver.inf" before enable this code!!!) - // - PcdTempStr = PcdGetPtr (PcdWinNtConsole); - } - - // - // Test Dynamic Set and Dynamic Set Ex - // - PcdSet32 (PcdWinNtDynamicUINT32, 2006); - Status = EFI_UNSUPPORTED; // diff --git a/MdePkg/Include/Library/PcdLib.h b/MdePkg/Include/Library/PcdLib.h index b513a61e86..90e19ba2a9 100644 --- a/MdePkg/Include/Library/PcdLib.h +++ b/MdePkg/Include/Library/PcdLib.h @@ -18,7 +18,7 @@ Module Name: PcdLib.h #ifndef __PCD_LIB_H__ #define __PCD_LIB_H__ -#define PCD_INVALID_TOKEN_NUMBER ((UINTN) -1) +#define PCD_INVALID_TOKEN_NUMBER ((UINTN) 0) #define PcdToken(TokenName) _PCD_TOKEN_##TokenName diff --git a/MdePkg/Include/Ppi/Pcd.h b/MdePkg/Include/Ppi/Pcd.h index 13b56406dc..360eea3beb 100644 --- a/MdePkg/Include/Ppi/Pcd.h +++ b/MdePkg/Include/Ppi/Pcd.h @@ -22,7 +22,7 @@ extern EFI_GUID gPcdPpiGuid; #define PCD_PPI_GUID \ { 0x6e81c58, 0x4ad7, 0x44bc, { 0x83, 0x90, 0xf1, 0x2, 0x65, 0xf7, 0x24, 0x80 } } -#define PCD_INVALID_TOKEN_NUMBER ((UINTN) -1) +#define PCD_INVALID_TOKEN_NUMBER ((UINTN) 0) typedef VOID diff --git a/MdePkg/Include/Protocol/Pcd.h b/MdePkg/Include/Protocol/Pcd.h index 323c16ccf1..07517ffbb1 100644 --- a/MdePkg/Include/Protocol/Pcd.h +++ b/MdePkg/Include/Protocol/Pcd.h @@ -22,7 +22,7 @@ extern EFI_GUID gPcdProtocolGuid; #define PCD_PROTOCOL_GUID \ { 0x11b34006, 0xd85b, 0x4d0a, { 0xa2, 0x90, 0xd5, 0xa5, 0x71, 0x31, 0xe, 0xf7 } } -#define PCD_INVALID_TOKEN_NUMBER ((UINTN) -1) +#define PCD_INVALID_TOKEN_NUMBER ((UINTN) 0) typedef VOID diff --git a/Tools/Conf/Pcd/PcdDatabaseCommonDefinitions.sample b/Tools/Conf/Pcd/PcdDatabaseCommonDefinitions.sample index 011d3df603..eaa692d25e 100644 --- a/Tools/Conf/Pcd/PcdDatabaseCommonDefinitions.sample +++ b/Tools/Conf/Pcd/PcdDatabaseCommonDefinitions.sample @@ -33,9 +33,10 @@ typedef struct { typedef struct { - UINT16 GuidTableIndex; // Offset in Guid Table in units of GUID. - UINT16 StringIndex; // Offset in String Table in units of UINT16. - UINT16 Offset; // Offset in Variable + UINT16 GuidTableIndex; // Offset in Guid Table in units of GUID. + UINT16 StringIndex; // Offset in String Table in units of UINT16. + UINT16 Offset; // Offset in Variable + UINT16 DefaultValueOffset; // Offset of the Default Value } VARIABLE_HEAD ; @@ -43,6 +44,8 @@ typedef struct { UINT32 Offset; } VPD_HEAD; +typedef UINT16 STRING_HEAD; + typedef struct { UINT32 LocalTokenNumber; UINT16 TokenNumber; diff --git a/Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java b/Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java index 3edadc6959..b54f68c815 100644 --- a/Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java +++ b/Tools/Source/GenBuild/org/tianocore/build/pcd/action/CollectPCDAction.java @@ -23,25 +23,17 @@ import java.io.FileReader; import java.io.IOException; import java.math.BigInteger; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.UUID; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; -import org.tianocore.DynamicPcdBuildDefinitionsDocument; import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions; -import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData; -import org.tianocore.DynamicPcdBuildDefinitionsDocument.DynamicPcdBuildDefinitions.PcdBuildData.SkuInfo; import org.tianocore.FrameworkModulesDocument; import org.tianocore.FrameworkPlatformDescriptionDocument; -import org.tianocore.FrameworkPlatformDescriptionDocument.FrameworkPlatformDescription; import org.tianocore.ModuleSADocument; -import org.tianocore.ModuleSADocument.ModuleSA; -import org.tianocore.PackageSurfaceAreaDocument; import org.tianocore.PcdBuildDefinitionDocument.PcdBuildDefinition; import org.tianocore.build.global.GlobalData; import org.tianocore.build.global.SurfaceAreaQuery; @@ -54,21 +46,31 @@ import org.tianocore.build.pcd.entity.UsageInstance; import org.tianocore.build.pcd.exception.EntityException; import org.tianocore.ModuleTypeDef; +class CStructTypeDeclaration { + String key; + int alignmentSize; + String cCode; + boolean initTable; + + public CStructTypeDeclaration (String key, int alignmentSize, String cCode, boolean initTable) { + this.key = key; + this.alignmentSize = alignmentSize; + this.cCode = cCode; + this.initTable = initTable; + } +} + class StringTable { private ArrayList al; private ArrayList alComments; private String phase; int len; - int bodyStart; - int bodyLineNum; public StringTable (String phase) { this.phase = phase; al = new ArrayList(); alComments = new ArrayList(); len = 0; - bodyStart = 0; - bodyLineNum = 0; } public String getSizeMacro () { @@ -89,6 +91,74 @@ class StringTable { public String getExistanceMacro () { return String.format(PcdDatabase.StringTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE"); } + + public void genCodeNew (ArrayList declaList, HashMap instTable) { + final String stringTable = "StringTable"; + final String tab = "\t"; + final String newLine = "\r\n"; + final String commaNewLine = ",\r\n"; + + CStructTypeDeclaration decl; + + String cDeclCode = ""; + String cInstCode = ""; + + // + // If we have a empty StringTable + // + if (al.size() == 0) { + cDeclCode += tab + String.format("UINT16 %s[1]; /* StringTable is Empty */", stringTable) + newLine; + decl = new CStructTypeDeclaration ( + stringTable, + 2, + cDeclCode, + true + ); + declaList.add(decl); + + cInstCode = tab + " { 0 } " + String.format("/* %s */", stringTable) + commaNewLine; + instTable.put(stringTable, cInstCode); + } else { + + // + // If there is any String in the StringTable + // + for (int i = 0; i < al.size(); i++) { + String str = al.get(i); + String stringTableName; + + if (i == 0) { + // + // StringTable is a well-known name in the PCD DXE driver + // + stringTableName = stringTable; + + } else { + stringTableName = String.format("%s_%d", stringTable, i); + cDeclCode += tab; + } + cDeclCode += String.format("UINT16 %s[%d]; /* %s */", stringTableName, str.length() + 1, alComments.get(i)) + newLine; + + if (i == 0) { + cInstCode = "/* StringTable */" + newLine; + } + cInstCode += tab + String.format("L\"%s\" /* %s */", al.get(i), alComments.get(i)); + if (i != al.size() - 1) { + cInstCode += commaNewLine; + } + } + + decl = new CStructTypeDeclaration ( + stringTable, + 2, + cDeclCode, + true + ); + declaList.add(decl); + + instTable.put(stringTable, cInstCode); + } + } public String getTypeDeclaration () { @@ -143,9 +213,33 @@ class StringTable { return output; } - public int add (String str, Token token) { + public int add (String inputStr, Token token) { int i; + int pos; + String str = inputStr; + + // + // The input can be two types: + // "L\"Bootmode\"" or "Bootmode". + // We drop the L\" and \" for the first type. + if (str.startsWith("L\"") && str.endsWith("\"")) { + str = str.substring(2, str.length() - 1); + } + // + // Check if StringTable has this String already. + // If so, return the current pos. + // + for (i = 0, pos = 0; i < al.size(); i++) { + String s = al.get(i);; + + if (str.equals(s)) { + return pos; + } + pos = s.length() + 1; + + } + i = len; // // Include the NULL character at the end of String @@ -163,16 +257,32 @@ class SizeTable { private ArrayList alComments; private String phase; private int len; - private int bodyStart; - private int bodyLineNum; - + public SizeTable (String phase) { this.phase = phase; al = new ArrayList(); alComments = new ArrayList(); len = 0; - bodyStart = 0; - bodyLineNum = 0; + } + + public void genCodeNew (ArrayList declaList, HashMap instTable, String phase) { + final String name = "SizeTable"; + + CStructTypeDeclaration decl; + String cCode; + + cCode = String.format(PcdDatabase.SizeTableDeclaration, phase); + decl = new CStructTypeDeclaration ( + name, + 2, + cCode, + true + ); + declaList.add(decl); + + + cCode = PcdDatabase.genInstantiationStr(getInstantiation()); + instTable.put(name, cCode); } public String getTypeDeclaration () { @@ -184,14 +294,12 @@ class SizeTable { Output.add("/* SizeTable */"); Output.add("{"); - bodyStart = 2; - if (al.size() == 0) { Output.add("0"); } else { for (int index = 0; index < al.size(); index++) { Integer n = al.get(index); - String str = n.toString(); + String str = "\t" + n.toString(); if (index != (al.size() - 1)) { str += ","; @@ -199,7 +307,6 @@ class SizeTable { str += " /* " + alComments.get(index) + " */"; Output.add(str); - bodyLineNum++; } } @@ -208,14 +315,6 @@ class SizeTable { return Output; } - public int getBodyStart() { - return bodyStart; - } - - public int getBodyLineNum () { - return bodyLineNum; - } - public int add (Token token) { int index = len; @@ -226,18 +325,6 @@ class SizeTable { return index; } - private int getDatumSize(Token token) { - /* - switch (token.datumType) { - case Token.DATUM_TYPE.UINT8: - return 1; - default: - return 0; - } - */ - return 0; - } - public int getTableLen () { return al.size() == 0 ? 1 : al.size(); } @@ -249,7 +336,6 @@ class GuidTable { private ArrayList alComments; private String phase; private int len; - private int bodyStart; private int bodyLineNum; public GuidTable (String phase) { @@ -257,7 +343,6 @@ class GuidTable { al = new ArrayList(); alComments = new ArrayList(); len = 0; - bodyStart = 0; bodyLineNum = 0; } @@ -273,6 +358,26 @@ class GuidTable { return String.format(PcdDatabase.GuidTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE"); } + public void genCodeNew (ArrayList declaList, HashMap instTable, String phase) { + final String name = "GuidTable"; + + CStructTypeDeclaration decl; + String cCode = ""; + + cCode += String.format(PcdDatabase.GuidTableDeclaration, phase); + decl = new CStructTypeDeclaration ( + name, + 8, + cCode, + true + ); + declaList.add(decl); + + + cCode = PcdDatabase.genInstantiationStr(getInstantiation()); + instTable.put(name, cCode); + } + public String getTypeDeclaration () { return String.format(PcdDatabase.GuidTableDeclaration, phase); } @@ -302,17 +407,15 @@ class GuidTable { Output.add("/* GuidTable */"); Output.add("{"); - bodyStart = 2; - if (al.size() == 0) { Output.add(getUuidCString(new UUID(0, 0))); } - for (Object u : al) { - UUID uuid = (UUID)u; - String str = getUuidCString(uuid); + for (int i = 0; i < al.size(); i++) { + String str = "\t" + getUuidCString(al.get(i)); - if (al.indexOf(u) != (al.size() - 1)) { + str += "/* " + alComments.get(i) + " */"; + if (i != (al.size() - 1)) { str += ","; } Output.add(str); @@ -324,23 +427,25 @@ class GuidTable { return Output; } - public int getBodyStart() { - return bodyStart; - } - - public int getBodyLineNum () { - return bodyLineNum; - } - public int add (UUID uuid, String name) { - int index = len; // - // Include the NULL character at the end of String + // Check if GuidTable has this entry already. + // If so, return the GuidTable index. // + for (int i = 0; i < al.size(); i++) { + if (al.get(i).equals(uuid)) { + return i; + } + } + len++; al.add(uuid); + alComments.add(name); - return index; + // + // Return the previous Table Index + // + return len - 1; } public int getTableLen () { @@ -354,15 +459,11 @@ class SkuIdTable { private ArrayList alComment; private String phase; private int len; - private int bodyStart; - private int bodyLineNum; public SkuIdTable (String phase) { this.phase = phase; al = new ArrayList(); alComment = new ArrayList(); - bodyStart = 0; - bodyLineNum = 0; len = 0; } @@ -371,13 +472,49 @@ class SkuIdTable { } private int getSize () { - return (al.size() == 0)? 1 : al.size(); + return (len == 0)? 1 : len; } public String getExistanceMacro () { return String.format(PcdDatabase.SkuTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE"); } + public void genCodeNew (ArrayList declaList, HashMap instTable, String phase) { + final String name = "SkuIdTable"; + + CStructTypeDeclaration decl; + String cCode = ""; + + cCode += String.format(PcdDatabase.SkuIdTableDeclaration, phase); + decl = new CStructTypeDeclaration ( + name, + 1, + cCode, + true + ); + declaList.add(decl); + + + cCode = PcdDatabase.genInstantiationStr(getInstantiation()); + instTable.put(name, cCode); + + // + // SystemSkuId is in PEI phase PCD Database + // + if (phase.equalsIgnoreCase("PEI")) { + decl = new CStructTypeDeclaration ( + "SystemSkuId", + 1, + "SKU_ID SystemSkuId;\r\n", + true + ); + declaList.add(decl); + + instTable.put("SystemSkuId", "0"); + } + + } + public String getTypeDeclaration () { return String.format(PcdDatabase.SkuIdTableDeclaration, phase); } @@ -387,10 +524,9 @@ class SkuIdTable { Output.add("/* SkuIdTable */"); Output.add("{"); - bodyStart = 2; if (al.size() == 0) { - Output.add("0"); + Output.add("\t0"); } for (int index = 0; index < al.size(); index++) { @@ -402,16 +538,15 @@ class SkuIdTable { Integer[] ia = al.get(index); - str += ia[0].toString() + ", "; + str += "\t" + ia[0].toString() + ", "; for (int index2 = 1; index2 < ia.length; index2++) { str += ia[index2].toString(); - if (index != al.size() - 1) { + if (!((index2 == ia.length - 1) && (index == al.size() - 1))) { str += ", "; } } Output.add(str); - bodyLineNum++; } @@ -423,6 +558,31 @@ class SkuIdTable { public int add (Token token) { int index; + int pos; + + // + // Check if this SKU_ID Array is already in the table + // + pos = 0; + for (Object o: al) { + Integer [] s = (Integer[]) o; + boolean different = false; + if (s[0] == token.getSkuIdCount()) { + for (index = 1; index < s.length; index++) { + if (s[index] != token.skuData.get(index-1).id) { + different = true; + break; + } + } + } else { + different = true; + } + if (different) { + pos += s[0] + 1; + } else { + return pos; + } + } Integer [] skuIds = new Integer[token.skuData.size() + 1]; skuIds[0] = new Integer(token.skuData.size()); @@ -472,6 +632,25 @@ class LocalTokenNumberTable { return String.format(PcdDatabase.DatabaseExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE"); } + public void genCodeNew (ArrayList declaList, HashMap instTable, String phase) { + final String name = "LocalTokenNumberTable"; + + CStructTypeDeclaration decl; + String cCode = ""; + + cCode += String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase); + decl = new CStructTypeDeclaration ( + name, + 4, + cCode, + true + ); + declaList.add(decl); + + cCode = PcdDatabase.genInstantiationStr(getInstantiation()); + instTable.put(name, cCode); + } + public String getTypeDeclaration () { return String.format(PcdDatabase.LocalTokenNumberTableDeclaration, phase); } @@ -489,7 +668,7 @@ class LocalTokenNumberTable { for (int index = 0; index < al.size(); index++) { String str; - str = (String)al.get(index); + str = "\t" + (String)al.get(index); str += " /* " + alComment.get(index) + " */ "; @@ -556,15 +735,12 @@ class ExMapTable { private ArrayList alComment; private String phase; private int len; - private int bodyStart; private int bodyLineNum; - private int base; - + public ExMapTable (String phase) { this.phase = phase; al = new ArrayList(); alComment = new ArrayList(); - bodyStart = 0; bodyLineNum = 0; len = 0; } @@ -574,14 +750,32 @@ class ExMapTable { + String.format(PcdDatabase.ExTokenNumber, phase, al.size()); } - private int getSize () { - return (al.size() == 0)? 1 : al.size(); - } - public String getExistanceMacro () { return String.format(PcdDatabase.ExMapTableExistenceMacro, phase, (al.size() == 0)? "TRUE":"FALSE"); } + public void genCodeNew (ArrayList declaList, HashMap instTable, String phase) { + final String exMapTableName = "ExMapTable"; + + sortTable(); + + CStructTypeDeclaration decl; + String cCode = ""; + + cCode += String.format(PcdDatabase.ExMapTableDeclaration, phase); + decl = new CStructTypeDeclaration ( + exMapTableName, + 4, + cCode, + true + ); + declaList.add(decl); + + + cCode = PcdDatabase.genInstantiationStr(getInstantiation()); + instTable.put(exMapTableName, cCode); + } + public String getTypeDeclaration () { return String.format(PcdDatabase.ExMapTableDeclaration, phase); } @@ -591,10 +785,8 @@ class ExMapTable { Output.add("/* ExMapTable */"); Output.add("{"); - bodyStart = 2; - if (al.size() == 0) { - Output.add("{0, 0, 0}"); + Output.add("\t{0, 0, 0}"); } int index; @@ -603,11 +795,11 @@ class ExMapTable { ExTriplet e = (ExTriplet)al.get(index); - str = "{ " + e.exTokenNumber.toString() + ", "; + str = "\t" + "{ " + String.format("0x%08X", e.exTokenNumber) + ", "; str += e.localTokenIdx.toString() + ", "; str += e.guidTableIdx.toString(); - str += " /* " + alComment.get(index) + " */"; + str += "}" + " /* " + alComment.get(index) + " */" ; if (index != al.size() - 1) { str += ","; @@ -637,21 +829,52 @@ class ExMapTable { return al.size() == 0 ? 1 : al.size(); } + // + // To simplify the algorithm for GetNextToken and GetNextTokenSpace in + // PCD PEIM/Driver, we need to sort the ExMapTable according to the + // following order: + // 1) ExGuid + // 2) ExTokenNumber + // + class ExTripletComp implements Comparator { + public int compare (ExTriplet a, ExTriplet b) { + if (a.guidTableIdx == b.guidTableIdx ) { + if (a.exTokenNumber > b.exTokenNumber) { + return 1; + } else if (a.exTokenNumber > b.exTokenNumber) { + return 1; + } else { + return 0; + } + } + + return a.guidTableIdx - b.guidTableIdx; + } + } + + private void sortTable () { + java.util.Comparator comparator = new ExTripletComp(); + java.util.Collections.sort(al, comparator); + } } class PcdDatabase { + private final static int SkuHeadAlignmentSize = 4; + private final String newLine = "\r\n"; + private final String commaNewLine = ",\r\n"; + private final String tab = "\t"; public final static String ExMapTableDeclaration = "DYNAMICEX_MAPPING ExMapTable[%s_EXMAPPING_TABLE_SIZE];\r\n"; public final static String GuidTableDeclaration = "EFI_GUID GuidTable[%s_GUID_TABLE_SIZE];\r\n"; public final static String LocalTokenNumberTableDeclaration = "UINT32 LocalTokenNumberTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n"; public final static String StringTableDeclaration = "UINT16 StringTable[%s_STRING_TABLE_SIZE];\r\n"; public final static String SizeTableDeclaration = "UINT16 SizeTable[%s_LOCAL_TOKEN_NUMBER_TABLE_SIZE];\r\n"; - public final static String SkuIdTableDeclaration = "UINT8 SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n"; + public final static String SkuIdTableDeclaration = "UINT8 SkuIdTable[%s_SKUID_TABLE_SIZE];\r\n"; public final static String ExMapTableSizeMacro = "#define %s_EXMAPPING_TABLE_SIZE %d\r\n"; public final static String ExTokenNumber = "#define %s_EX_TOKEN_NUMBER %d\r\n"; - public final static String GuidTableSizeMacro = "#define %s_GUID_TABLE_SIZE %d\r\n"; + public final static String GuidTableSizeMacro = "#define %s_GUID_TABLE_SIZE %d\r\n"; public final static String LocalTokenNumberTableSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER_TABLE_SIZE %d\r\n"; public final static String LocalTokenNumberSizeMacro = "#define %s_LOCAL_TOKEN_NUMBER %d\r\n"; public final static String StringTableSizeMacro = "#define %s_STRING_TABLE_SIZE %d\r\n"; @@ -665,7 +888,11 @@ class PcdDatabase { public final static String SkuTableExistenceMacro = "#define %s_SKUID_TABLE_EMPTY %s\r\n"; public final static String offsetOfSkuHeadStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s_SkuDataTable)"; + public final static String offsetOfVariableEnabledDefault = "offsetof(%s_PCD_DATABASE, %s.%s_VariableDefault_%d)"; public final static String offsetOfStrTemplate = "offsetof(%s_PCD_DATABASE, %s.%s)"; + + private final static String skuDataTableTemplate = "SkuDataTable"; + private StringTable stringTable; private GuidTable guidTable; @@ -678,6 +905,12 @@ class PcdDatabase { private String phase; private int assignedTokenNumber; + // + // Use two class global variable to store + // temperary + // + private String privateGlobalName; + private String privateGlobalCCode; // // After Major changes done to the PCD // database generation class PcdDatabase @@ -685,7 +918,7 @@ class PcdDatabase { // also update the version number in PCD // service PEIM and DXE driver accordingly. // - private final int version = 1; + private final int version = 2; private String hString; private String cString; @@ -697,7 +930,7 @@ class PcdDatabase { - getAlignmentSize(a); } } - + public PcdDatabase (ArrayList alTokens, String exePhase, int startLen) { phase = exePhase; @@ -708,10 +941,23 @@ class PcdDatabase { sizeTable = new SizeTable(phase); exMapTable = new ExMapTable(phase); - assignedTokenNumber = startLen; + assignedTokenNumber = startLen + 1; this.alTokens = alTokens; } + private void getNonExAndExTokens (ArrayList alTokens, List nexTokens, List exTokens) { + for (int i = 0; i < alTokens.size(); i++) { + Token t = (Token)alTokens.get(i); + if (t.isDynamicEx()) { + exTokens.add(t); + } else { + nexTokens.add(t); + } + } + + return; + } + private void getTwoGroupsOfTokens (ArrayList alTokens, List initTokens, List uninitTokens) { for (int i = 0; i < alTokens.size(); i++) { Token t = (Token)alTokens.get(i); @@ -725,6 +971,25 @@ class PcdDatabase { return; } + private int getDataTypeAlignmentSize (Token token) { + switch (token.datumType) { + case UINT8: + return 1; + case UINT16: + return 2; + case UINT32: + return 4; + case UINT64: + return 8; + case POINTER: + return 1; + case BOOLEAN: + return 1; + default: + return 1; + } + } + private int getAlignmentSize (Token token) { if (token.getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) { return 2; @@ -737,22 +1002,8 @@ class PcdDatabase { if (token.isUnicodeStringType()) { return 2; } - - switch (token.datumType) { - case UINT8: - return 1; - case UINT16: - return 2; - case UINT32: - return 4; - case UINT64: - return 8; - case POINTER: - return 1; - case BOOLEAN: - return 1; - } - return 1; + + return getDataTypeAlignmentSize(token); } public String getCString () { @@ -762,6 +1013,224 @@ class PcdDatabase { public String getHString () { return hString; } + + private void genCodeWorker(Token t, + ArrayList declaList, + HashMap instTable, String phase) + throws EntityException { + + CStructTypeDeclaration decl; + + // + // Insert SKU_HEAD if isSkuEnable is true + // + if (t.isSkuEnable()) { + int tableIdx; + tableIdx = skuIdTable.add(t); + decl = new CStructTypeDeclaration(t.getPrimaryKeyString(), + SkuHeadAlignmentSize, getSkuEnabledTypeDeclaration(t), true); + declaList.add(decl); + instTable.put(t.getPrimaryKeyString(), + getSkuEnabledTypeInstantiaion(t, tableIdx)); + } + + // + // Insert PCD_ENTRY declaration and instantiation + // + getCDeclarationString(t); + + decl = new CStructTypeDeclaration(privateGlobalName, + getAlignmentSize(t), privateGlobalCCode, t.hasDefaultValue()); + declaList.add(decl); + + if (t.hasDefaultValue()) { + instTable.put(privateGlobalName, + getTypeInstantiation(t, declaList, instTable, phase) + ); + } + + } + + private void ProcessTokensNew (List tokens, + ArrayList cStructDeclList, + HashMap cStructInstTable, + String phase + ) + throws EntityException { + + for (int idx = 0; idx < tokens.size(); idx++) { + Token t = tokens.get(idx); + + genCodeWorker (t, cStructDeclList, cStructInstTable, phase); + + sizeTable.add(t); + localTokenNumberTable.add(t); + t.tokenNumber = assignedTokenNumber++; + + // + // Add a mapping if this dynamic PCD entry is a EX type + // + if (t.isDynamicEx()) { + exMapTable.add(t.tokenNumber, + t.dynamicExTokenNumber, + guidTable.add(t.tokenSpaceName, t.getPrimaryKeyString()), + t.getPrimaryKeyString() + ); + } + } + + } + + public void genCodeNew () throws EntityException { + + ArrayList cStructDeclList = new ArrayList(); + HashMap cStructInstTable = new HashMap(); + + List nexTokens = new ArrayList (); + List exTokens = new ArrayList (); + + getNonExAndExTokens (alTokens, nexTokens, exTokens); + + // + // We have to process Non-Ex type PCD entry first. The reason is + // that our optimization assumes that the Token Number of Non-Ex + // PCD entry start from 1 (for PEI phase) and grows continously upwards. + // + // EX type token number starts from the last Non-EX PCD entry and + // grows continously upwards. + // + ProcessTokensNew (nexTokens, cStructDeclList, cStructInstTable, phase); + ProcessTokensNew (exTokens, cStructDeclList, cStructInstTable, phase); + + stringTable.genCodeNew(cStructDeclList, cStructInstTable); + skuIdTable.genCodeNew(cStructDeclList, cStructInstTable, phase); + exMapTable.genCodeNew(cStructDeclList, cStructInstTable, phase); + localTokenNumberTable.genCodeNew(cStructDeclList, cStructInstTable, phase); + sizeTable.genCodeNew(cStructDeclList, cStructInstTable, phase); + guidTable.genCodeNew(cStructDeclList, cStructInstTable, phase); + + hString = genCMacroCode (); + + HashMap result; + + result = genCStructCode(cStructDeclList, + cStructInstTable, + phase + ); + + hString += result.get("initDeclStr"); + hString += result.get("uninitDeclStr"); + + hString += String.format("#define PCD_%s_SERVICE_DRIVER_VERSION %d", phase, version); + + cString = newLine + newLine + result.get("initInstStr"); + + } + + private String genCMacroCode () { + String macroStr = ""; + + // + // Generate size info Macro for all Tables + // + macroStr += guidTable.getSizeMacro(); + macroStr += stringTable.getSizeMacro(); + macroStr += skuIdTable.getSizeMacro(); + macroStr += localTokenNumberTable.getSizeMacro(); + macroStr += exMapTable.getSizeMacro(); + + // + // Generate existance info Macro for all Tables + // + macroStr += guidTable.getExistanceMacro(); + macroStr += stringTable.getExistanceMacro(); + macroStr += skuIdTable.getExistanceMacro(); + macroStr += localTokenNumberTable.getExistanceMacro(); + macroStr += exMapTable.getExistanceMacro(); + + macroStr += newLine; + + return macroStr; + } + + private HashMap genCStructCode( + ArrayList declaList, + HashMap instTable, + String phase + ) { + + int i; + HashMap result = new HashMap(); + HashMap > alignmentInitDecl = new HashMap>(); + HashMap > alignmentUninitDecl = new HashMap>(); + HashMap > alignmentInitInst = new HashMap>(); + + // + // Initialize the storage for each alignment + // + for (i = 8; i > 0; i>>=1) { + alignmentInitDecl.put(new Integer(i), new ArrayList()); + alignmentInitInst.put(new Integer(i), new ArrayList()); + alignmentUninitDecl.put(new Integer(i), new ArrayList()); + } + + String initDeclStr = "typedef struct {" + newLine; + String initInstStr = String.format("%s_PCD_DATABASE_INIT g%sPcdDbInit = { ", phase.toUpperCase(), phase.toUpperCase()) + newLine; + String uninitDeclStr = "typedef struct {" + newLine; + + // + // Sort all C declaration and instantiation base on Alignment Size + // + for (Object d : declaList) { + CStructTypeDeclaration decl = (CStructTypeDeclaration) d; + + if (decl.initTable) { + alignmentInitDecl.get(new Integer(decl.alignmentSize)).add(decl.cCode); + alignmentInitInst.get(new Integer(decl.alignmentSize)).add(instTable.get(decl.key)); + } else { + alignmentUninitDecl.get(new Integer(decl.alignmentSize)).add(decl.cCode); + } + } + + // + // Generate code for every alignment size + // + for (int align = 8; align > 0; align >>= 1) { + ArrayList declaListBasedOnAlignment = alignmentInitDecl.get(new Integer(align)); + ArrayList instListBasedOnAlignment = alignmentInitInst.get(new Integer(align)); + for (i = 0; i < declaListBasedOnAlignment.size(); i++) { + initDeclStr += tab + declaListBasedOnAlignment.get(i); + initInstStr += tab + instListBasedOnAlignment.get(i); + + // + // We made a assumption that both PEI_PCD_DATABASE and DXE_PCD_DATABASE + // has a least one data memember with alignment size of 1. So we can + // remove the last "," in the C structure instantiation string. + // + if ((align == 1) && (i == declaListBasedOnAlignment.size() - 1)) { + initInstStr += newLine; + } else { + initInstStr += commaNewLine; + } + } + + declaListBasedOnAlignment = alignmentUninitDecl.get(new Integer(align)); + for (Object d : declaListBasedOnAlignment) { + String s = (String)d; + uninitDeclStr += tab + s; + } + } + + initDeclStr += String.format("} %s_PCD_DATABASE_INIT;", phase) + newLine + newLine; + initInstStr += "};"; + uninitDeclStr += String.format("} %s_PCD_DATABASE_UNINIT;", phase) + newLine + newLine; + + result.put("initDeclStr", initDeclStr); + result.put("initInstStr", initInstStr); + result.put("uninitDeclStr", uninitDeclStr); + + return result; + } public void genCode () throws EntityException { @@ -916,10 +1385,13 @@ class PcdDatabase { } - private String genInstantiationStr (ArrayList alStr) { + public static String genInstantiationStr (ArrayList alStr) { String str = ""; for (int i = 0; i< alStr.size(); i++) { - str += "\t" + alStr.get(i); + if (i != 0) { + str += "\t"; + } + str += alStr.get(i); if (i != alStr.size() - 1) { str += "\r\n"; } @@ -999,7 +1471,7 @@ class PcdDatabase { private String getSkuEnabledTypeInstantiaion (Token token, int SkuTableIdx) { String offsetof = String.format(PcdDatabase.offsetOfSkuHeadStrTemplate, phase, token.hasDefaultValue()? "Init" : "Uninit", token.getPrimaryKeyString()); - return String.format("{ %s, %d }", offsetof, SkuTableIdx); + return String.format("{ %s, %d } /* SKU_ENABLED: %s */", offsetof, SkuTableIdx, token.getPrimaryKeyString()); } private String getDataTypeDeclarationForSkuEnabled (Token token) { @@ -1043,6 +1515,10 @@ class PcdDatabase { } + private String getDataTypeInstantiationForVariableDefault_new (Token token, String cName, int skuId) { + return String.format("%s /* %s */", token.skuData.get(skuId).value.hiiDefaultValue, cName); + } + private String getDataTypeInstantiation (Token token) { if (token.datumType == Token.DATUM_TYPE.POINTER) { @@ -1052,7 +1528,82 @@ class PcdDatabase { } } + private String getCType (Token t) + throws EntityException { + + if (t.isHiiEnable()) { + return "VARIABLE_HEAD"; + } + + if (t.isVpdEnable()) { + return "VPD_HEAD"; + } + + if (t.isUnicodeStringType()) { + return "STRING_HEAD"; + } + + switch (t.datumType) { + case UINT64: + return "UINT64"; + case UINT32: + return "UINT32"; + case UINT16: + return "UINT16"; + case UINT8: + return "UINT8"; + case BOOLEAN: + return "BOOLEAN"; + case POINTER: + return "UINT8"; + default: + throw new EntityException("Unknown type in getDataTypeCDeclaration"); + } + } + + private void getCDeclarationString(Token t) + throws EntityException { + + if (t.isSkuEnable()) { + privateGlobalName = String.format("%s_%s", t.getPrimaryKeyString(), skuDataTableTemplate); + } else { + privateGlobalName = t.getPrimaryKeyString(); + } + if (t.isUnicodeStringType()) { + privateGlobalCCode = String.format("STRING_HEAD %s[%d];\r\n", t.getPrimaryKeyString(), t.getSkuIdCount()); + } else { + String type = getCType(t); + if (t.datumType == Token.DATUM_TYPE.POINTER) { + privateGlobalCCode = String.format("%s %s[%d][%d];\r\n", type, privateGlobalName, t.getSkuIdCount(), t.datumSize); + } else { + privateGlobalCCode = String.format("%s %s[%d];\r\n", type, privateGlobalName, t.getSkuIdCount()); + } + } + } + + private String getDataTypeDeclarationForVariableDefault_new (Token token, String cName, int skuId) { + + String typeStr = ""; + + if (token.datumType == Token.DATUM_TYPE.UINT8) { + typeStr = "UINT8"; + } else if (token.datumType == Token.DATUM_TYPE.UINT16) { + typeStr = "UINT16"; + } else if (token.datumType == Token.DATUM_TYPE.UINT32) { + typeStr = "UINT32"; + } else if (token.datumType == Token.DATUM_TYPE.UINT64) { + typeStr = "UINT64"; + } else if (token.datumType == Token.DATUM_TYPE.BOOLEAN) { + typeStr = "BOOLEAN"; + } else if (token.datumType == Token.DATUM_TYPE.POINTER) { + return String.format("UINT8 %s[%d]", cName, token.datumSize); + } else { + } + + return String.format("%s %s;\r\n", typeStr, cName); + } + private String getDataTypeDeclaration (Token token) { String typeStr = ""; @@ -1078,7 +1629,67 @@ class PcdDatabase { private String getVpdEnableTypeDeclaration (Token token) { return String.format("VPD_HEAD %s", token.getPrimaryKeyString()); } + + private String getTypeInstantiation (Token t, ArrayList declaList, HashMap instTable, String phase) throws EntityException { + + int i; + String s; + s = String.format("/* %s */", t.getPrimaryKeyString()) + newLine; + s += tab + "{" + newLine; + + for (i = 0; i < t.skuData.size(); i++) { + if (t.isUnicodeStringType() && !t.isHiiEnable()) { + s += tab + tab + String.format("{ %d }", stringTable.add(t.skuData.get(i).value.value, t)); + } else if (t.isHiiEnable()) { + /* VPD_HEAD definition + typedef struct { + UINT16 GuidTableIndex; // Offset in Guid Table in units of GUID. + UINT16 StringIndex; // Offset in String Table in units of UINT16. + UINT16 Offset; // Offset in Variable + } VARIABLE_HEAD ; + */ + String variableDefaultName = String.format("%s_VariableDefault_%d", t.getPrimaryKeyString(), i); + + s += tab + tab + String.format("{ %d, %d, %s, %s }", guidTable.add(t.skuData.get(i).value.variableGuid, t.getPrimaryKeyString()), + stringTable.add(t.skuData.get(i).value.getStringOfVariableName(), t), + t.skuData.get(i).value.variableOffset, + String.format("offsetof(%s_PCD_DATABASE, Init.%s)", phase, variableDefaultName) + ); + // + // We need to support the default value, so we add the declaration and + // the instantiation for the default value. + // + CStructTypeDeclaration decl = new CStructTypeDeclaration (variableDefaultName, + getDataTypeAlignmentSize(t), + getDataTypeDeclarationForVariableDefault_new(t, variableDefaultName, i), + true + ); + declaList.add(decl); + instTable.put(variableDefaultName, getDataTypeInstantiationForVariableDefault_new (t, variableDefaultName, i)); + } else if (t.isVpdEnable()) { + /* typedef struct { + UINT32 Offset; + } VPD_HEAD; + */ + s += tab + tab + String.format("{ %s }", t.skuData.get(i).value.vpdOffset); + } else { + s += tab + tab + String.format("{ %s }", t.skuData.get(i).value.value); + } + + if (i != t.skuData.size() - 1) { + s += commaNewLine; + } else { + s += newLine; + } + + } + + s += tab + "}"; + + return s; + } + private String getVpdEnableTypeInstantiation (Token token) { return String.format("{ %s } /* %s */", token.getDefaultSku().vpdOffset, token.getPrimaryKeyString()); @@ -1321,19 +1932,19 @@ public class CollectPCDAction { dbManager.getTwoPhaseDynamicRecordArray(alPei, alDxe); PcdDatabase pcdPeiDatabase = new PcdDatabase (alPei, "PEI", 0); - pcdPeiDatabase.genCode(); - dbManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString() + pcdPeiDatabase.genCodeNew(); + MemoryDatabaseManager.PcdPeimHString = PcdCommonHeaderString + pcdPeiDatabase.getHString() + PcdDatabase.getPcdPeiDatabaseDefinitions(); - dbManager.PcdPeimCString = pcdPeiDatabase.getCString(); + MemoryDatabaseManager.PcdPeimCString = pcdPeiDatabase.getCString(); PcdDatabase pcdDxeDatabase = new PcdDatabase (alDxe, "DXE", alPei.size() ); - pcdDxeDatabase.genCode(); - dbManager.PcdDxeHString = dbManager.PcdPeimHString + pcdDxeDatabase.getHString() + pcdDxeDatabase.genCodeNew(); + MemoryDatabaseManager.PcdDxeHString = MemoryDatabaseManager.PcdPeimHString + pcdDxeDatabase.getHString() + PcdDatabase.getPcdDxeDatabaseDefinitions(); - dbManager.PcdDxeCString = pcdDxeDatabase.getCString(); + MemoryDatabaseManager.PcdDxeCString = pcdDxeDatabase.getCString(); } /** diff --git a/Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java b/Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java index fdf90b620c..c363aab6a5 100644 --- a/Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java +++ b/Tools/Source/GenBuild/org/tianocore/build/pcd/action/PCDAutoGenAction.java @@ -19,15 +19,11 @@ package org.tianocore.build.pcd.action; import java.io.File; import java.util.ArrayList; import java.util.List; -import java.util.Map; -import java.util.Set; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.xmlbeans.XmlObject; import org.tianocore.build.global.GlobalData; -import org.tianocore.build.global.SurfaceAreaQuery; import org.tianocore.build.pcd.entity.MemoryDatabaseManager; import org.tianocore.build.pcd.entity.Token; import org.tianocore.build.pcd.entity.UsageInstance; @@ -398,7 +394,7 @@ public class PCDAutoGenAction extends BuildAction { **/ public static void main(String argv[]) { - String WorkSpace = "M:/tianocore/edk2"; + String WorkSpace = "X:/edk2"; String logFilePath = WorkSpace + "/EdkNt32Pkg/Nt32.fpd"; String[] nameArray = null; @@ -421,13 +417,13 @@ public class PCDAutoGenAction extends BuildAction { // // Then execute the PCDAuotoGenAction to get generated Autogen.h and Autogen.c // - PCDAutoGenAction autogenAction = new PCDAutoGenAction("MonoStatusCode", + PCDAutoGenAction autogenAction = new PCDAutoGenAction("PcdPeim", null, null, null, "IA32", null, - false, + true, nameArray); autogenAction.execute(); diff --git a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java index 0ff5b8e338..0439c11d7c 100644 --- a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java +++ b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java @@ -15,20 +15,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. **/ package org.tianocore.build.pcd.entity; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; -import java.util.Hashtable; import java.util.List; import java.util.Map; import java.util.UUID; -import org.tianocore.build.pcd.action.ActionMessage; +import org.tianocore.build.pcd.exception.EntityException; /** Database hold all PCD information comes from SPD, MSA, FPD file in memory. **/ @@ -141,7 +134,6 @@ public class MemoryDatabaseManager { private ArrayList getDynamicRecordArray() { Token[] tokenArray = getRecordArray(); int index = 0; - int count = 0; ArrayList al = new ArrayList(); for (index = 0; index < tokenArray.length; index++) { @@ -159,8 +151,10 @@ public class MemoryDatabaseManager { The output array is sorted based on descending order of the size of alignment for each feilds. @return the token record array contained all PCD token referenced in PEI phase. + * @throws EntityException **/ - public void getTwoPhaseDynamicRecordArray(ArrayList pei, ArrayList dxe) { + public void getTwoPhaseDynamicRecordArray(ArrayList pei, ArrayList dxe) + throws EntityException { int usageInstanceIndex = 0; int index = 0; ArrayList tokenArrayList = getDynamicRecordArray(); @@ -185,10 +179,30 @@ public class MemoryDatabaseManager { } } - // If no PEI components reference the PCD entry, we insert it to DXE list + // + // If no PEI components reference the PCD entry, + // we check if it is referenced in DXE driver. // if (!found) { - dxe.add(token); + if (token.consumers != null) { + usageInstanceArray = token.consumers.entrySet().toArray(); + for (usageInstanceIndex = 0; usageInstanceIndex < token.consumers.size(); usageInstanceIndex ++) { + usageInstance =(UsageInstance) (((Map.Entry)usageInstanceArray[usageInstanceIndex]).getValue()); + if (usageInstance.isDxePhaseComponent()) { + dxe.add(token); + found = true; + break; + } + } + } + + if (!found) { + // + // We only support Dynamice(EX) type for PEI and DXE phase. + // If it is not referenced in either PEI or DXE, throw exception now. + // + throw new EntityException("Dynamic(EX) PCD Entries are referenced in module that is not in PEI phase nor in DXE phase."); + } } } diff --git a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/Token.java b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/Token.java index 9a54a3e579..7d1e0191db 100644 --- a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/Token.java +++ b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/Token.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Map; import java.util.UUID; -import org.tianocore.build.pcd.action.ActionMessage; import org.tianocore.build.pcd.exception.EntityException; /** This class is to descript a PCD token object. The information of a token mainly @@ -164,6 +163,17 @@ public class Token { return false; } + public boolean isDynamicEx() { + + for (int i = 0; i < supportedPcdType.size(); i++) { + if (supportedPcdType.get(i) == PCD_TYPE.DYNAMIC_EX) { + return true; + } + } + + return false; + } + /** Use "TokencName + "-" + SpaceTokenName" as primary key when adding token into database @@ -194,6 +204,20 @@ public class Token { } return false; } + + public boolean isHiiEnable() { + if (getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.HII_TYPE) { + return true; + } + return false; + } + + public boolean isVpdEnable() { + if (getDefaultSku().type == DynamicTokenValue.VALUE_TYPE.VPD_TYPE) { + return true; + } + return false; + } /** Get the token primary key in token database. @@ -471,6 +495,7 @@ public class Token { @return string of datum type. **/ + public static String getAutogendatumTypeString(DATUM_TYPE datumType) { switch (datumType) { case UINT8: @@ -545,6 +570,11 @@ public class Token { return null; } + + public int getSkuIdCount () { + return this.skuData.size(); + } + /** Get default value for a token, For HII type, HiiDefaultValue of default @@ -576,13 +606,17 @@ public class Token { boolean isInteger = true; DynamicTokenValue dynamicValue = null; + if (isSkuEnable()) { + return true; + } + if (this.isDynamicPCD) { dynamicValue = getDefaultSku(); switch (dynamicValue.type) { case HII_TYPE: - return !isValidNullValue(dynamicValue.hiiDefaultValue); + return true; case VPD_TYPE: - return false; + return true; case DEFAULT_TYPE: return !isValidNullValue(dynamicValue.value); } diff --git a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java index 20f0b9b14d..85806b4289 100644 --- a/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java +++ b/Tools/Source/GenBuild/org/tianocore/build/pcd/entity/UsageInstance.java @@ -20,8 +20,6 @@ package org.tianocore.build.pcd.entity; import java.util.UUID; import org.tianocore.ModuleTypeDef; -import org.tianocore.build.autogen.CommonDefinition; -import org.tianocore.build.pcd.action.ActionMessage; import org.tianocore.build.pcd.exception.EntityException; /** @@ -186,6 +184,23 @@ public class UsageInstance { } return false; } + + public boolean isDxePhaseComponent() { + // + // BugBug: May need confirmation on which type of module can + // make use of Dynamic(EX) PCD entry. + // + if ((moduleType == ModuleTypeDef.DXE_DRIVER) || + (moduleType == ModuleTypeDef.DXE_RUNTIME_DRIVER) || + (moduleType == ModuleTypeDef.DXE_SAL_DRIVER) || + (moduleType == ModuleTypeDef.DXE_SMM_DRIVER) || + (moduleType == ModuleTypeDef.UEFI_DRIVER) || + (moduleType == ModuleTypeDef.UEFI_APPLICATION) + ) { + return true; + } + return false; + } /** Generate autogen string for header file and C code file.