]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Universal/PCD/Pei/Pcd.c
MdeModulePkg PCD: Enable Firmware to retrieve the default setting
[mirror_edk2.git] / MdeModulePkg / Universal / PCD / Pei / Pcd.c
index b018e5a95de29f2765897a9f61414ca141cd0be2..91eb9d6ccf4c67d319d71d71313ce3eb3e63a4ab 100644 (file)
@@ -1,27 +1,24 @@
-/** @file PCD PEIM\r
-\r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
-\r
+/** @file \r
+  All Pcd Ppi services are implemented here.\r
+  \r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>\r
+(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
+This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
 \r
-Module Name: Pcd.c\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
-//\r
-// Include common header file for this module.\r
-//\r
-#include "CommonHeader.h"\r
-\r
 #include "Service.h"\r
 \r
-\r
+///\r
+/// Instance of PCD_PPI protocol is EDKII native implementation.\r
+/// This protocol instance support dynamic and dynamicEx type PCDs.\r
+///\r
 PCD_PPI mPcdPpiInstance = {\r
   PeiPcdSetSku,\r
 \r
@@ -61,48 +58,363 @@ PCD_PPI mPcdPpiInstance = {
   PeiPcdGetNextTokenSpace\r
 };\r
 \r
+///\r
+/// Instance of EFI_PEI_PCD_PPI which is defined in PI 1.2 Vol 3.\r
+/// This PPI instance only support dyanmicEx type PCD.\r
+///\r
+EFI_PEI_PCD_PPI  mEfiPcdPpiInstance = {\r
+  PeiPcdSetSku,\r
+  \r
+  PeiPcdGet8Ex,\r
+  PeiPcdGet16Ex,\r
+  PeiPcdGet32Ex,\r
+  PeiPcdGet64Ex,\r
+  PeiPcdGetPtrEx,\r
+  PeiPcdGetBoolEx,\r
+  PeiPcdGetSizeEx,\r
+  PeiPcdSet8Ex,\r
+  PeiPcdSet16Ex,\r
+  PeiPcdSet32Ex,\r
+  PeiPcdSet64Ex,\r
+  PeiPcdSetPtrEx,\r
+  PeiPcdSetBoolEx,\r
+  (EFI_PEI_PCD_PPI_CALLBACK_ON_SET) PeiRegisterCallBackOnSet,\r
+  (EFI_PEI_PCD_PPI_CANCEL_CALLBACK) PcdUnRegisterCallBackOnSet,\r
+  PeiPcdGetNextToken,\r
+  PeiPcdGetNextTokenSpace\r
+};\r
+\r
+///\r
+/// Instance of GET_PCD_INFO_PPI protocol is EDKII native implementation.\r
+/// This protocol instance support dynamic and dynamicEx type PCDs.\r
+///\r
+GET_PCD_INFO_PPI mGetPcdInfoInstance = {\r
+  PeiGetPcdInfoGetInfo,\r
+  PeiGetPcdInfoGetInfoEx,\r
+  PeiGetPcdInfoGetSku\r
+};\r
+\r
+///\r
+/// Instance of EFI_GET_PCD_INFO_PPI which is defined in PI 1.2.1 Vol 3.\r
+/// This PPI instance only support dyanmicEx type PCD.\r
+///\r
+EFI_GET_PCD_INFO_PPI  mEfiGetPcdInfoInstance = {\r
+  PeiGetPcdInfoGetInfoEx,\r
+  PeiGetPcdInfoGetSku\r
+};\r
 \r
+EFI_PEI_PPI_DESCRIPTOR  mPpiList[] = {\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_PPI,\r
+    &gPcdPpiGuid,\r
+    &mPcdPpiInstance\r
+  },\r
+  {\r
+    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+    &gEfiPeiPcdPpiGuid,\r
+    &mEfiPcdPpiInstance\r
+  }\r
+};\r
 \r
-STATIC EFI_PEI_PPI_DESCRIPTOR  mPpiPCD = {\r
-  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
-  &gPcdPpiGuid,\r
-  &mPcdPpiInstance\r
+EFI_PEI_PPI_DESCRIPTOR  mPpiList2[] = {\r
+  {\r
+    EFI_PEI_PPI_DESCRIPTOR_PPI,\r
+    &gGetPcdInfoPpiGuid,\r
+    &mGetPcdInfoInstance\r
+  },\r
+  {\r
+    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+    &gEfiGetPcdInfoPpiGuid,\r
+    &mEfiGetPcdInfoInstance\r
+  }\r
 };\r
 \r
+/**\r
+  Callback on SET PcdSetNvStoreDefaultId\r
+\r
+  Once PcdSetNvStoreDefaultId is set, the default NV storage will be found from\r
+  PcdNvStoreDefaultValueBuffer, and built into VariableHob.\r
 \r
+  @param[in]      CallBackGuid  The PCD token GUID being set.\r
+  @param[in]      CallBackToken The PCD token number being set.\r
+  @param[in, out] TokenData     A pointer to the token data being set.\r
+  @param[in]      TokenDataSize The size, in bytes, of the data being set.\r
 \r
+**/\r
+VOID\r
+EFIAPI\r
+PcdSetNvStoreDefaultIdCallBack (\r
+  IN CONST EFI_GUID         *CallBackGuid, OPTIONAL\r
+  IN       UINTN            CallBackToken,\r
+  IN OUT   VOID             *TokenData,\r
+  IN       UINTN            TokenDataSize\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+  UINT16     DefaultId;\r
+  SKU_ID     SkuId;\r
+  UINTN      FullSize;\r
+  UINTN      Index;\r
+  UINT8      *DataBuffer;\r
+  UINT8      *VarStoreHobData;\r
+  UINT8      *BufferEnd;\r
+  BOOLEAN    IsFound;\r
+  VARIABLE_STORE_HEADER *NvStoreBuffer;\r
+  PCD_DEFAULT_DATA      *DataHeader;\r
+  PCD_DEFAULT_INFO      *DefaultInfo;\r
+  PCD_DATA_DELTA        *DeltaData;\r
+\r
+  DefaultId = *(UINT16 *) TokenData;\r
+  SkuId     = GetPcdDatabase()->SystemSkuId;\r
+  IsFound   = FALSE;\r
+\r
+  if (PeiPcdGetSizeEx (&gEfiMdeModulePkgTokenSpaceGuid, PcdToken (PcdNvStoreDefaultValueBuffer)) > sizeof (PCD_NV_STORE_DEFAULT_BUFFER_HEADER)) {\r
+    DataBuffer = (UINT8 *) PeiPcdGetPtrEx (&gEfiMdeModulePkgTokenSpaceGuid, PcdToken (PcdNvStoreDefaultValueBuffer));\r
+    FullSize   = ((PCD_NV_STORE_DEFAULT_BUFFER_HEADER *) DataBuffer)->Length;\r
+    DataHeader = (PCD_DEFAULT_DATA *) (DataBuffer + sizeof (PCD_NV_STORE_DEFAULT_BUFFER_HEADER));\r
+    //\r
+    // The first section data includes NV storage default setting.\r
+    //\r
+    NvStoreBuffer   = (VARIABLE_STORE_HEADER *) ((UINT8 *) DataHeader + sizeof (DataHeader->DataSize) + DataHeader->HeaderSize);\r
+    VarStoreHobData = (UINT8 *) BuildGuidHob (&NvStoreBuffer->Signature, NvStoreBuffer->Size);\r
+    ASSERT (VarStoreHobData != NULL);\r
+    CopyMem (VarStoreHobData, NvStoreBuffer, NvStoreBuffer->Size);\r
+    //\r
+    // Find the matched SkuId and DefaultId in the first section\r
+    //\r
+    DefaultInfo    = &(DataHeader->DefaultInfo[0]);\r
+    BufferEnd      = (UINT8 *) DataHeader + sizeof (DataHeader->DataSize) + DataHeader->HeaderSize;\r
+    while ((UINT8 *) DefaultInfo < BufferEnd) {\r
+      if (DefaultInfo->DefaultId == DefaultId && DefaultInfo->SkuId == SkuId) {\r
+        IsFound = TRUE;\r
+        break;\r
+      }\r
+      DefaultInfo ++;\r
+    }\r
+    //\r
+    // Find the matched SkuId and DefaultId in the remaining section\r
+    //\r
+    Index      = sizeof (PCD_NV_STORE_DEFAULT_BUFFER_HEADER) + ((DataHeader->DataSize + 7) & (~7));\r
+    DataHeader = (PCD_DEFAULT_DATA *) (DataBuffer + Index);\r
+    while (!IsFound && Index < FullSize && DataHeader->DataSize != 0xFFFFFFFF) {\r
+      DefaultInfo = &(DataHeader->DefaultInfo[0]);\r
+      BufferEnd   = (UINT8 *) DataHeader + sizeof (DataHeader->DataSize) + DataHeader->HeaderSize;\r
+      while ((UINT8 *) DefaultInfo < BufferEnd) {\r
+        if (DefaultInfo->DefaultId == DefaultId && DefaultInfo->SkuId == SkuId) {\r
+          IsFound = TRUE;\r
+          break;\r
+        }\r
+        DefaultInfo ++;\r
+      }\r
+      if (IsFound) {\r
+        DeltaData = (PCD_DATA_DELTA *) BufferEnd;\r
+        BufferEnd = (UINT8 *) DataHeader + DataHeader->DataSize;\r
+        while ((UINT8 *) DeltaData < BufferEnd) {\r
+          *(VarStoreHobData + DeltaData->Offset) = (UINT8) DeltaData->Value;\r
+          DeltaData ++;\r
+        }\r
+        break;\r
+      }\r
+      Index      = (Index + DataHeader->DataSize + 7) & (~7) ;\r
+      DataHeader = (PCD_DEFAULT_DATA *) (DataBuffer + Index);\r
+    }\r
+  }\r
+\r
+  Status = PcdUnRegisterCallBackOnSet (\r
+             &gEfiMdeModulePkgTokenSpaceGuid,\r
+             PcdToken(PcdSetNvStoreDefaultId),\r
+             PcdSetNvStoreDefaultIdCallBack\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+}\r
+\r
+/**\r
+  Main entry for PCD PEIM driver.\r
+  \r
+  This routine initialize the PCD database for PEI phase and install PCD_PPI/EFI_PEI_PCD_PPI.\r
+\r
+  @param  FileHandle  Handle of the file being invoked.\r
+  @param  PeiServices Describes the list of possible PEI Services.\r
+\r
+  @return Status of install PCD_PPI\r
+\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PcdPeimInit (\r
-  IN EFI_FFS_FILE_HEADER      *FfsHeader,\r
-  IN EFI_PEI_SERVICES         **PeiServices\r
+  IN       EFI_PEI_FILE_HANDLE  FileHandle,\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices\r
   )\r
 {\r
   EFI_STATUS Status;\r
 \r
-  BuildPcdDatabase ();\r
-  \r
-  Status = PeiServicesInstallPpi (&mPpiPCD);\r
+  BuildPcdDatabase (FileHandle);\r
 \r
+  //\r
+  // Install PCD_PPI and EFI_PEI_PCD_PPI.\r
+  //\r
+  Status = PeiServicesInstallPpi (&mPpiList[0]);\r
   ASSERT_EFI_ERROR (Status);\r
-  \r
-  return EFI_SUCCESS;\r
+\r
+  //\r
+  // Install GET_PCD_INFO_PPI and EFI_GET_PCD_INFO_PPI.\r
+  //\r
+  Status = PeiServicesInstallPpi (&mPpiList2[0]);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  Status = PeiRegisterCallBackOnSet (\r
+             &gEfiMdeModulePkgTokenSpaceGuid,\r
+             PcdToken(PcdSetNvStoreDefaultId),\r
+             PcdSetNvStoreDefaultIdCallBack\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
 }\r
 \r
+/**\r
+  Retrieve additional information associated with a PCD token in the default token space.\r
+\r
+  This includes information such as the type of value the TokenNumber is associated with as well as possible\r
+  human readable name that is associated with the token.\r
+\r
+  @param[in]    TokenNumber The PCD token number.\r
+  @param[out]   PcdInfo     The returned information associated with the requested TokenNumber.\r
+                            The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.\r
+\r
+  @retval  EFI_SUCCESS      The PCD information was returned successfully.\r
+  @retval  EFI_NOT_FOUND    The PCD service could not find the requested token number.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiGetPcdInfoGetInfo (\r
+  IN        UINTN           TokenNumber,\r
+  OUT       EFI_PCD_INFO    *PcdInfo\r
+  )\r
+{\r
+  return PeiGetPcdInfo (NULL, TokenNumber, PcdInfo);\r
+}\r
+\r
+/**\r
+  Retrieve additional information associated with a PCD token.\r
+\r
+  This includes information such as the type of value the TokenNumber is associated with as well as possible\r
+  human readable name that is associated with the token.\r
+\r
+  @param[in]    Guid        The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]    TokenNumber The PCD token number.\r
+  @param[out]   PcdInfo     The returned information associated with the requested TokenNumber.\r
+                            The caller is responsible for freeing the buffer that is allocated by callee for PcdInfo->PcdName.\r
+\r
+  @retval  EFI_SUCCESS      The PCD information was returned successfully.\r
+  @retval  EFI_NOT_FOUND    The PCD service could not find the requested token number.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+PeiGetPcdInfoGetInfoEx (\r
+  IN CONST  EFI_GUID        *Guid,\r
+  IN        UINTN           TokenNumber,\r
+  OUT       EFI_PCD_INFO    *PcdInfo\r
+  )\r
+{\r
+  return PeiGetPcdInfo (Guid, TokenNumber, PcdInfo);\r
+}\r
+\r
+/**\r
+  Retrieve the currently set SKU Id.\r
+\r
+  @return   The currently set SKU Id. If the platform has not set at a SKU Id, then the\r
+            default SKU Id value of 0 is returned. If the platform has set a SKU Id, then the currently set SKU\r
+            Id is returned.\r
+**/\r
+UINTN\r
+EFIAPI\r
+PeiGetPcdInfoGetSku (\r
+  VOID\r
+  )\r
+{\r
+  return (UINTN) GetPcdDatabase()->SystemSkuId;\r
+}\r
+\r
+/**\r
+  Sets the SKU value for subsequent calls to set or get PCD token values.\r
+\r
+  SetSku() sets the SKU Id to be used for subsequent calls to set or get PCD values. \r
+  SetSku() is normally called only once by the system.\r
+\r
+  For each item (token), the database can hold a single value that applies to all SKUs, \r
+  or multiple values, where each value is associated with a specific SKU Id. Items with multiple, \r
+  SKU-specific values are called SKU enabled. \r
+  \r
+  The SKU Id of zero is reserved as a default.\r
+  For tokens that are not SKU enabled, the system ignores any set SKU Id and works with the \r
+  single value for that token. For SKU-enabled tokens, the system will use the SKU Id set by the \r
+  last call to SetSku(). If no SKU Id is set or the currently set SKU Id isn't valid for the specified token, \r
+  the system uses the default SKU Id. If the system attempts to use the default SKU Id and no value has been \r
+  set for that Id, the results are unpredictable.\r
+\r
+  @param[in]  SkuId The SKU value that will be used when the PCD service will retrieve and \r
+              set values associated with a PCD token.\r
+\r
+**/\r
 VOID\r
 EFIAPI\r
 PeiPcdSetSku (\r
   IN  UINTN                  SkuId\r
   )\r
 {\r
+  PEI_PCD_DATABASE  *PeiPcdDb;\r
+  SKU_ID            *SkuIdTable;\r
+  UINTN             Index;\r
+\r
+  PeiPcdDb = GetPcdDatabase();\r
+\r
+  if (SkuId == PeiPcdDb->SystemSkuId) {\r
+    //\r
+    // The input SKU Id is equal to current SKU Id, return directly.\r
+    //\r
+    return;\r
+  }\r
 \r
-  GetPcdDatabase()->Init.SystemSkuId = (SKU_ID) SkuId;\r
+  if (PeiPcdDb->SystemSkuId != (SKU_ID) 0) {\r
+    DEBUG ((DEBUG_ERROR, "PcdPei - The SKU Id could be changed only once."));\r
+    DEBUG ((\r
+      DEBUG_ERROR,\r
+      "PcdPei - The SKU Id was set to 0x%lx already, it could not be set to 0x%lx any more.",\r
+      PeiPcdDb->SystemSkuId,\r
+      (SKU_ID) SkuId\r
+      ));\r
+    ASSERT (FALSE);\r
+    return;\r
+  }\r
 \r
+  SkuIdTable = (SKU_ID *) ((UINT8 *) PeiPcdDb + PeiPcdDb->SkuIdTableOffset);\r
+  for (Index = 0; Index < SkuIdTable[0]; Index++) {\r
+    if (SkuId == SkuIdTable[Index + 1]) {\r
+      DEBUG ((EFI_D_INFO, "PcdPei - Set current SKU Id to 0x%lx.\n", (SKU_ID) SkuId));\r
+      PeiPcdDb->SystemSkuId = (SKU_ID) SkuId;\r
+      return;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Invalid input SkuId, the default SKU Id will be still used for the system.\r
+  //\r
+  DEBUG ((EFI_D_INFO, "PcdPei - Invalid input SkuId, the default SKU Id will be still used.\n"));\r
   return;\r
 }\r
 \r
+/**\r
+  Retrieves an 8-bit value for a given PCD token.\r
 \r
+  Retrieves the current byte-sized value for a PCD token number.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
+  \r
+  @param[in]  TokenNumber The PCD token number. \r
 \r
+  @return The UINT8 value.\r
+  \r
+**/\r
 UINT8\r
 EFIAPI\r
 PeiPcdGet8 (\r
@@ -112,8 +424,17 @@ PeiPcdGet8 (
   return *((UINT8 *) GetWorker (TokenNumber, sizeof (UINT8)));\r
 }\r
 \r
+/**\r
+  Retrieves an 16-bit value for a given PCD token.\r
 \r
+  Retrieves the current 16-bits value for a PCD token number.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
+  \r
+  @param[in]  TokenNumber The PCD token number. \r
 \r
+  @return The UINT16 value.\r
+  \r
+**/\r
 UINT16\r
 EFIAPI\r
 PeiPcdGet16 (\r
@@ -123,8 +444,17 @@ PeiPcdGet16 (
   return ReadUnaligned16 (GetWorker (TokenNumber, sizeof (UINT16)));\r
 }\r
 \r
+/**\r
+  Retrieves an 32-bit value for a given PCD token.\r
 \r
+  Retrieves the current 32-bits value for a PCD token number.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
+  \r
+  @param[in]  TokenNumber The PCD token number. \r
 \r
+  @return The UINT32 value.\r
+  \r
+**/\r
 UINT32\r
 EFIAPI\r
 PeiPcdGet32 (\r
@@ -134,8 +464,17 @@ PeiPcdGet32 (
   return ReadUnaligned32 (GetWorker (TokenNumber, sizeof (UINT32)));\r
 }\r
 \r
+/**\r
+  Retrieves an 64-bit value for a given PCD token.\r
 \r
+  Retrieves the current 64-bits value for a PCD token number.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
+  \r
+  @param[in]  TokenNumber The PCD token number. \r
 \r
+  @return The UINT64 value.\r
+  \r
+**/\r
 UINT64\r
 EFIAPI\r
 PeiPcdGet64 (\r
@@ -145,8 +484,19 @@ PeiPcdGet64 (
   return ReadUnaligned64 (GetWorker (TokenNumber, sizeof (UINT64)));\r
 }\r
 \r
+/**\r
+  Retrieves a pointer to a value for a given PCD token.\r
 \r
+  Retrieves the current pointer to the buffer for a PCD token number.  \r
+  Do not make any assumptions about the alignment of the pointer that \r
+  is returned by this function call.  If the TokenNumber is invalid, \r
+  the results are unpredictable.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+\r
+  @return The pointer to the buffer to be retrieved.\r
+  \r
+**/\r
 VOID *\r
 EFIAPI\r
 PeiPcdGetPtr (\r
@@ -156,8 +506,19 @@ PeiPcdGetPtr (
   return GetWorker (TokenNumber, 0);\r
 }\r
 \r
+/**\r
+  Retrieves a Boolean value for a given PCD token.\r
+\r
+  Retrieves the current boolean value for a PCD token number.  \r
+  Do not make any assumptions about the alignment of the pointer that \r
+  is returned by this function call.  If the TokenNumber is invalid, \r
+  the results are unpredictable.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
 \r
+  @return The Boolean value.\r
+  \r
+**/\r
 BOOLEAN\r
 EFIAPI\r
 PeiPcdGetBool (\r
@@ -167,8 +528,17 @@ PeiPcdGetBool (
   return *((BOOLEAN *) GetWorker (TokenNumber, sizeof (BOOLEAN)));\r
 }\r
 \r
+/**\r
+  Retrieves the size of the value for a given PCD token.\r
 \r
+  Retrieves the current size of a particular PCD token.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+\r
+  @return The size of the value for the PCD token.\r
+  \r
+**/\r
 UINTN\r
 EFIAPI\r
 PeiPcdGetSize (\r
@@ -178,17 +548,10 @@ PeiPcdGetSize (
   PEI_PCD_DATABASE    *PeiPcdDb;\r
   UINTN               Size;\r
   UINTN               MaxSize;\r
+  UINT32              LocalTokenCount;\r
 \r
-  //\r
-  // If DebugAssertEnabled is TRUE, we still need to provide the GET size\r
-  // function as GetWorker and SetWoker need this function to do ASSERT.\r
-  //\r
-  if ((!FeaturePcdGet(PcdPeiPcdDatabaseGetSizeEnabled)) &&\r
-      (!DebugAssertEnabled ())) {\r
-    return 0;\r
-  }\r
-\r
-  PeiPcdDb = GetPcdDatabase ();\r
+  PeiPcdDb        = GetPcdDatabase ();\r
+  LocalTokenCount = PeiPcdDb->LocalTokenCount;\r
   //\r
   // TokenNumber Zero is reserved as PCD_INVALID_TOKEN_NUMBER.\r
   // We have to decrement TokenNumber by 1 to make it usable\r
@@ -199,9 +562,9 @@ PeiPcdGetSize (
   // EBC compiler is very choosy. It may report warning about comparison\r
   // between UINTN and 0 . So we add 1 in each size of the \r
   // comparison.\r
-  ASSERT (TokenNumber + 1 < PEI_LOCAL_TOKEN_NUMBER + 1);\r
+  ASSERT (TokenNumber + 1 < (LocalTokenCount + 1));\r
 \r
-  Size = (PeiPcdDb->Init.LocalTokenNumberTable[TokenNumber] & PCD_DATUM_TYPE_ALL_SET) >> PCD_DATUM_TYPE_SHIFT;\r
+  Size = (*((UINT32 *)((UINT8 *)PeiPcdDb + PeiPcdDb->LocalTokenNumberTableOffset) + TokenNumber) & PCD_DATUM_TYPE_ALL_SET) >> PCD_DATUM_TYPE_SHIFT;\r
 \r
   if (Size == 0) {\r
     //\r
@@ -214,8 +577,20 @@ PeiPcdGetSize (
 \r
 }\r
 \r
+/**\r
+  Retrieves an 8-bit value for a given PCD token.\r
+\r
+  Retrieves the 8-bit value of a particular PCD token.  \r
+  If the TokenNumber is invalid or the token space\r
+  specified by Guid does not exist, the results are \r
+  unpredictable.\r
 \r
+  @param[in]  Guid              The token space for the token number.\r
+  @param[in]  ExTokenNumber     The PCD token number. \r
 \r
+  @return The size 8-bit value for the PCD token.\r
+  \r
+**/\r
 UINT8\r
 EFIAPI\r
 PeiPcdGet8Ex (\r
@@ -226,8 +601,20 @@ PeiPcdGet8Ex (
   return *((UINT8 *) ExGetWorker (Guid, ExTokenNumber, sizeof (UINT8)));\r
 }\r
 \r
+/**\r
+  Retrieves an 16-bit value for a given PCD token.\r
 \r
+  Retrieves the 16-bit value of a particular PCD token.  \r
+  If the TokenNumber is invalid or the token space\r
+  specified by Guid does not exist, the results are \r
+  unpredictable.\r
 \r
+  @param[in]  Guid          The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+\r
+  @return The size 16-bit value for the PCD token.\r
+  \r
+**/\r
 UINT16\r
 EFIAPI\r
 PeiPcdGet16Ex (\r
@@ -238,8 +625,20 @@ PeiPcdGet16Ex (
   return ReadUnaligned16 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT16)));\r
 }\r
 \r
+/**\r
+  Retrieves an 32-bit value for a given PCD token.\r
+\r
+  Retrieves the 32-bit value of a particular PCD token.  \r
+  If the TokenNumber is invalid or the token space\r
+  specified by Guid does not exist, the results are \r
+  unpredictable.\r
 \r
+  @param[in]  Guid The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
 \r
+  @return The size 32-bit value for the PCD token.\r
+  \r
+**/\r
 UINT32\r
 EFIAPI\r
 PeiPcdGet32Ex (\r
@@ -250,8 +649,20 @@ PeiPcdGet32Ex (
   return ReadUnaligned32 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT32)));\r
 }\r
 \r
+/**\r
+  Retrieves an 64-bit value for a given PCD token.\r
 \r
+  Retrieves the 64-bit value of a particular PCD token.  \r
+  If the TokenNumber is invalid or the token space\r
+  specified by Guid does not exist, the results are \r
+  unpredictable.\r
 \r
+  @param[in]  Guid The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+\r
+  @return The size 64-bit value for the PCD token.\r
+  \r
+**/\r
 UINT64\r
 EFIAPI\r
 PeiPcdGet64Ex (\r
@@ -262,8 +673,20 @@ PeiPcdGet64Ex (
   return ReadUnaligned64 (ExGetWorker (Guid, ExTokenNumber, sizeof (UINT64)));\r
 }\r
 \r
+/**\r
+  Retrieves a pointer to a value for a given PCD token.\r
+\r
+  Retrieves the current pointer to the buffer for a PCD token number.  \r
+  Do not make any assumptions about the alignment of the pointer that \r
+  is returned by this function call.  If the TokenNumber is invalid, \r
+  the results are unpredictable.\r
 \r
+  @param[in]  Guid          The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
 \r
+  @return The pointer to the buffer to be retrieved.\r
+  \r
+**/\r
 VOID *\r
 EFIAPI\r
 PeiPcdGetPtrEx (\r
@@ -274,8 +697,20 @@ PeiPcdGetPtrEx (
   return ExGetWorker (Guid, ExTokenNumber, 0);\r
 }\r
 \r
+/**\r
+  Retrieves an Boolean value for a given PCD token.\r
 \r
+  Retrieves the Boolean value of a particular PCD token.  \r
+  If the TokenNumber is invalid or the token space\r
+  specified by Guid does not exist, the results are \r
+  unpredictable.\r
 \r
+  @param[in]  Guid          The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+\r
+  @return The size Boolean value for the PCD token.\r
+  \r
+**/\r
 BOOLEAN\r
 EFIAPI\r
 PeiPcdGetBoolEx (\r
@@ -286,8 +721,18 @@ PeiPcdGetBoolEx (
   return *((BOOLEAN *) ExGetWorker (Guid, ExTokenNumber, sizeof (BOOLEAN)));\r
 }\r
 \r
+/**\r
+  Retrieves the size of the value for a given PCD token.\r
+\r
+  Retrieves the current size of a particular PCD token.  \r
+  If the TokenNumber is invalid, the results are unpredictable.\r
 \r
+  @param[in]  Guid          The token space for the token number.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
 \r
+  @return The size of the value for the PCD token.\r
+  \r
+**/\r
 UINTN\r
 EFIAPI\r
 PeiPcdGetSizeEx (\r
@@ -295,15 +740,26 @@ PeiPcdGetSizeEx (
   IN UINTN                        ExTokenNumber\r
   )\r
 {\r
-  if ((!FeaturePcdGet (PcdPeiPcdDatabaseGetSizeEnabled)) ||  !FeaturePcdGet (PcdPeiPcdDatabaseExEnabled)) {\r
-    return 0;\r
-  }\r
-\r
   return PeiPcdGetSize (GetExPcdTokenNumber (Guid, ExTokenNumber));\r
 }\r
 \r
+/**\r
+  Sets an 8-bit value for a given PCD token.\r
 \r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet8 (\r
@@ -314,8 +770,23 @@ PeiPcdSet8 (
   return SetValueWorker (TokenNumber, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 16-bit value for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
 \r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet16 (\r
@@ -326,8 +797,23 @@ PeiPcdSet16 (
   return SetValueWorker (TokenNumber, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 32-bit value for a given PCD token.\r
 \r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet32 (\r
@@ -338,8 +824,23 @@ PeiPcdSet32 (
   return SetValueWorker (TokenNumber, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 64-bit value for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
 \r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet64 (\r
@@ -350,7 +851,27 @@ PeiPcdSet64 (
   return SetValueWorker (TokenNumber, &Value, sizeof (Value));\r
 }\r
 \r
-\r
+/**\r
+  Sets a value of a specified size for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
+\r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in, out] SizeOfBuffer A pointer to the length of the value being set for the PCD token.  \r
+                              On input, if the SizeOfValue is greater than the maximum size supported \r
+                              for this TokenNumber then the output value of SizeOfValue will reflect \r
+                              the maximum size supported for this TokenNumber.\r
+  @param[in]  Buffer The buffer to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSetPtr (\r
@@ -362,8 +883,23 @@ PeiPcdSetPtr (
   return SetWorker (TokenNumber, Buffer, SizeOfBuffer, TRUE);\r
 }\r
 \r
+/**\r
+  Sets an Boolean value for a given PCD token.\r
 \r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  TokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSetBool (\r
@@ -374,8 +910,24 @@ PeiPcdSetBool (
   return SetValueWorker (TokenNumber, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 8-bit value for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  Guid          The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  Value         The value to set for the PCD token.\r
 \r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet8Ex (\r
@@ -387,8 +939,24 @@ PeiPcdSet8Ex (
   return ExSetValueWorker (ExTokenNumber, Guid, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 16-bit value for a given PCD token.\r
 \r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  Guid The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  Value The value to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet16Ex (\r
@@ -400,8 +968,24 @@ PeiPcdSet16Ex (
   return ExSetValueWorker (ExTokenNumber, Guid, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 32-bit value for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  Guid          The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  Value         The value to set for the PCD token.\r
 \r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet32Ex (\r
@@ -413,8 +997,24 @@ PeiPcdSet32Ex (
   return ExSetValueWorker (ExTokenNumber, Guid, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Sets an 64-bit value for a given PCD token.\r
 \r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param[in]  Guid          The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  Value         The value to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSet64Ex (\r
@@ -426,22 +1026,58 @@ PeiPcdSet64Ex (
   return ExSetValueWorker (ExTokenNumber, Guid, &Value, sizeof (Value));\r
 }\r
 \r
-\r
-\r
+/**\r
+  Sets a value of a specified size for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
+\r
+  @param[in]        Guid            The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]        ExTokenNumber   The PCD token number. \r
+  @param[in, out]   SizeOfBuffer    A pointer to the length of the value being set for the PCD token.  \r
+                                    On input, if the SizeOfValue is greater than the maximum size supported \r
+                                    for this TokenNumber then the output value of SizeOfValue will reflect \r
+                                    the maximum size supported for this TokenNumber.\r
+  @param[in]        Value           The buffer to set for the PCD token.\r
+\r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSetPtrEx (\r
-  IN CONST EFI_GUID               *Guid,\r
-  IN UINTN                        ExTokenNumber,\r
-  IN UINTN                        *SizeOfBuffer,\r
-  IN VOID                         *Value\r
+  IN     CONST EFI_GUID               *Guid,\r
+  IN     UINTN                        ExTokenNumber,\r
+  IN OUT UINTN                        *SizeOfBuffer,\r
+  IN     VOID                         *Value\r
   )\r
 {\r
   return ExSetWorker (ExTokenNumber, Guid, Value, SizeOfBuffer, TRUE);\r
 }\r
 \r
+/**\r
+  Sets an Boolean value for a given PCD token.\r
+\r
+  When the PCD service sets a value, it will check to ensure that the \r
+  size of the value being set is compatible with the Token's existing definition.  \r
+  If it is not, an error will be returned.\r
 \r
+  @param [in]  Guid          The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param [in]  ExTokenNumber The PCD token number. \r
+  @param [in]  Value         The value to set for the PCD token.\r
 \r
+  @retval EFI_SUCCESS  Procedure returned successfully.\r
+  @retval EFI_INVALID_PARAMETER The PCD service determined that the size of the data \r
+                                  being set was incompatible with a call to this function.  \r
+                                  Use GetSize() to retrieve the size of the target data.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the requested token number.\r
+  \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdSetBoolEx (\r
@@ -453,9 +1089,18 @@ PeiPcdSetBoolEx (
   return ExSetValueWorker (ExTokenNumber, Guid, &Value, sizeof (Value));\r
 }\r
 \r
+/**\r
+  Specifies a function to be called anytime the value of a designated token is changed.\r
 \r
+  @param[in]  Guid The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  CallBackFunction The function prototype called when the value associated with the CallBackToken is set.  \r
 \r
+  @retval EFI_SUCCESS  The PCD service has successfully established a call event \r
+                        for the CallBackToken requested.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the referenced token number.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiRegisterCallBackOnSet (\r
@@ -464,17 +1109,29 @@ PeiRegisterCallBackOnSet (
   IN  PCD_PPI_CALLBACK            CallBackFunction\r
   )\r
 {\r
-  if (!FeaturePcdGet(PcdPeiPcdDatabaseCallbackOnSetEnabled)) {\r
+  if (!FeaturePcdGet(PcdPeiFullPcdDatabaseEnable)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  ASSERT (CallBackFunction != NULL);\r
-  \r
+  if (CallBackFunction == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, TRUE);\r
 }\r
 \r
+/**\r
+  Cancels a previously set callback function for a particular PCD token number.\r
+\r
+  @param[in]  Guid The 128-bit unique value that designates the namespace from which to extract the value.\r
+  @param[in]  ExTokenNumber The PCD token number. \r
+  @param[in]  CallBackFunction The function prototype called when the value associated with the CallBackToken is set.  \r
 \r
+  @retval EFI_SUCCESS  The PCD service has successfully established a call event \r
+                        for the CallBackToken requested.\r
+  @retval EFI_NOT_FOUND The PCD service could not find the referenced token number.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PcdUnRegisterCallBackOnSet (\r
@@ -483,17 +1140,42 @@ PcdUnRegisterCallBackOnSet (
   IN  PCD_PPI_CALLBACK            CallBackFunction\r
   )\r
 {\r
-  if (!FeaturePcdGet(PcdPeiPcdDatabaseCallbackOnSetEnabled)) {\r
+  if (!FeaturePcdGet(PcdPeiFullPcdDatabaseEnable)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  ASSERT (CallBackFunction != NULL);\r
-  \r
+  if (CallBackFunction == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   return PeiRegisterCallBackWorker (ExTokenNumber, Guid, CallBackFunction, FALSE);\r
 }\r
 \r
+/**\r
+  Retrieves the next valid token number in a given namespace.  \r
+  \r
+  This is useful since the PCD infrastructure contains a sparse list of token numbers, \r
+  and one cannot a priori know what token numbers are valid in the database. \r
+  \r
+  If TokenNumber is 0 and Guid is not NULL, then the first token from the token space specified by Guid is returned.  \r
+  If TokenNumber is not 0 and Guid is not NULL, then the next token in the token space specified by Guid is returned.  \r
+  If TokenNumber is 0 and Guid is NULL, then the first token in the default token space is returned.  \r
+  If TokenNumber is not 0 and Guid is NULL, then the next token in the default token space is returned.  \r
+  The token numbers in the default token space may not be related to token numbers in token spaces that are named by Guid.  \r
+  If the next token number can be retrieved, then it is returned in TokenNumber, and EFI_SUCCESS is returned.  \r
+  If TokenNumber represents the last token number in the token space specified by Guid, then EFI_NOT_FOUND is returned.  \r
+  If TokenNumber is not present in the token space specified by Guid, then EFI_NOT_FOUND is returned.\r
+\r
+\r
+  @param[in]       Guid        The 128-bit unique value that designates the namespace from which to extract the value.  \r
+                               This is an optional parameter that may be NULL.  If this parameter is NULL, then a request \r
+                               is being made to retrieve tokens from the default token space.\r
+  @param[in, out]  TokenNumber A pointer to the PCD token number to use to find the subsequent token number.\r
+                   \r
+  @retval EFI_SUCCESS   The PCD service has retrieved the next valid token number.\r
+  @retval EFI_NOT_FOUND The PCD service could not find data from the requested token number.\r
 \r
-\r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdGetNextToken (\r
@@ -504,95 +1186,116 @@ PeiPcdGetNextToken (
   UINTN               GuidTableIdx;\r
   PEI_PCD_DATABASE    *PeiPcdDb;\r
   EFI_GUID            *MatchGuid;\r
+  EFI_GUID            *GuidTable;\r
   DYNAMICEX_MAPPING   *ExMapTable;\r
-  UINTN               i;\r
+  UINTN               Index;\r
   BOOLEAN             Found;\r
   BOOLEAN             PeiExMapTableEmpty;\r
+  UINTN               PeiNexTokenNumber; \r
 \r
-  if (!FeaturePcdGet (PcdPeiPcdDatabaseTraverseEnabled)) {\r
+  if (!FeaturePcdGet (PcdPeiFullPcdDatabaseEnable)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
-  PeiExMapTableEmpty = PEI_EXMAP_TABLE_EMPTY;\r
+  PeiPcdDb          = GetPcdDatabase ();\r
+  PeiNexTokenNumber = PeiPcdDb->LocalTokenCount - PeiPcdDb->ExTokenCount;\r
+  GuidTable         = (EFI_GUID *)((UINT8 *)PeiPcdDb + PeiPcdDb->GuidTableOffset);\r
 \r
+  if (PeiPcdDb->ExTokenCount == 0) {\r
+    PeiExMapTableEmpty = TRUE;\r
+  } else {\r
+    PeiExMapTableEmpty = FALSE;\r
+  }\r
   if (Guid == NULL) {\r
-    if (*TokenNumber > PEI_NEX_TOKEN_NUMBER) {\r
+    if (*TokenNumber > PeiNexTokenNumber) {\r
       return EFI_NOT_FOUND;\r
     }\r
     (*TokenNumber)++;\r
-    if (*TokenNumber > PEI_NEX_TOKEN_NUMBER) {\r
+    if (*TokenNumber > PeiNexTokenNumber) {\r
       *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
+      return EFI_NOT_FOUND;\r
     }\r
     return EFI_SUCCESS;\r
   } else {\r
     if (PeiExMapTableEmpty) {\r
-      *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
-      return EFI_SUCCESS;\r
+      return EFI_NOT_FOUND;\r
     }\r
-    \r
-    //\r
-    // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
-    // 1) ExGuid\r
-    // 2) ExTokenNumber\r
-    //\r
-    PeiPcdDb = GetPcdDatabase ();\r
-    \r
-    MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), Guid);\r
+\r
+    MatchGuid = ScanGuid (GuidTable, PeiPcdDb->GuidTableCount * sizeof(EFI_GUID), Guid);\r
 \r
     if (MatchGuid == NULL) {\r
-      *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
       return EFI_NOT_FOUND;\r
     }\r
 \r
-    GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
+    GuidTableIdx = MatchGuid - GuidTable;\r
 \r
-    ExMapTable = PeiPcdDb->Init.ExMapTable;\r
+    ExMapTable = (DYNAMICEX_MAPPING *)((UINT8 *)PeiPcdDb + PeiPcdDb->ExMapTableOffset);\r
 \r
     Found = FALSE;\r
     //\r
     // Locate the GUID in ExMapTable first.\r
     //\r
-    for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
-      if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
+    for (Index = 0; Index < PeiPcdDb->ExTokenCount; Index++) {\r
+      if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {\r
         Found = TRUE;\r
         break;\r
       }\r
     }\r
 \r
     if (Found) {\r
+      //\r
+      // If given token number is PCD_INVALID_TOKEN_NUMBER, then return the first\r
+      // token number in found token space.\r
+      //\r
       if (*TokenNumber == PCD_INVALID_TOKEN_NUMBER) {\r
-        *TokenNumber = ExMapTable[i].ExTokenNumber;\r
+        *TokenNumber = ExMapTable[Index].ExTokenNumber;\r
          return EFI_SUCCESS;\r
       }\r
 \r
-      for ( ; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
-        if (ExMapTable[i].ExTokenNumber == *TokenNumber) {\r
-          i++;\r
-          if (i == PEI_EXMAPPING_TABLE_SIZE) {\r
-            //\r
-            // Exceed the length of ExMap Table\r
-            //\r
-            *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
-            return EFI_SUCCESS;\r
-          }\r
-          if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
-            *TokenNumber = ExMapTable[i].ExTokenNumber;\r
-            return EFI_SUCCESS;\r
-          } else {\r
-            *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
-            return EFI_SUCCESS;\r
-          }\r
+      for ( ; Index < PeiPcdDb->ExTokenCount; Index++) {\r
+        if ((ExMapTable[Index].ExTokenNumber == *TokenNumber) && (ExMapTable[Index].ExGuidIndex == GuidTableIdx)) {\r
+          break;\r
+        }\r
+      }\r
+\r
+      while (Index < PeiPcdDb->ExTokenCount) {\r
+        Index++;\r
+        if (Index == PeiPcdDb->ExTokenCount) {\r
+          //\r
+          // Exceed the length of ExMap Table\r
+          //\r
+          *TokenNumber = PCD_INVALID_TOKEN_NUMBER;\r
+          return EFI_NOT_FOUND;\r
+        } else if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {\r
+          //\r
+          // Found the next match\r
+          //\r
+          *TokenNumber = ExMapTable[Index].ExTokenNumber;\r
+          return EFI_SUCCESS;\r
         }\r
       }\r
-      return EFI_NOT_FOUND;\r
     }\r
   }\r
 \r
   return EFI_NOT_FOUND;\r
 }\r
 \r
+/**\r
+  Retrieves the next valid PCD token namespace for a given namespace.\r
+\r
+  Gets the next valid token namespace for a given namespace. This is useful to traverse the valid\r
+  token namespaces on a platform.\r
 \r
+  @param[in, out]   Guid    An indirect pointer to EFI_GUID. On input it designates a known token\r
+                            namespace from which the search will start. On output, it designates the next valid\r
+                            token namespace on the platform. If *Guid is NULL, then the GUID of the first token\r
+                            space of the current platform is returned. If the search cannot locate the next valid\r
+                            token namespace, an error is returned and the value of *Guid is undefined.\r
\r
+  @retval  EFI_SUCCESS      The PCD service retrieved the value requested.\r
+  @retval  EFI_NOT_FOUND    The PCD service could not find the next valid token namespace.\r
 \r
+**/\r
 EFI_STATUS\r
 EFIAPI\r
 PeiPcdGetNextTokenSpace (\r
@@ -603,75 +1306,97 @@ PeiPcdGetNextTokenSpace (
   EFI_GUID            *MatchGuid;\r
   PEI_PCD_DATABASE    *PeiPcdDb;\r
   DYNAMICEX_MAPPING   *ExMapTable;\r
-  UINTN               i;\r
+  UINTN               Index;\r
+  UINTN               Index2;\r
   BOOLEAN             Found;\r
   BOOLEAN             PeiExMapTableEmpty;\r
+  EFI_GUID            *GuidTable;\r
 \r
-  if (!FeaturePcdGet (PcdPeiPcdDatabaseTraverseEnabled)) {\r
+  if (!FeaturePcdGet (PcdPeiFullPcdDatabaseEnable)) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
   ASSERT (Guid != NULL);\r
 \r
-  PeiExMapTableEmpty = PEI_EXMAP_TABLE_EMPTY;\r
+  PeiPcdDb = GetPcdDatabase ();\r
 \r
+  if (PeiPcdDb->ExTokenCount == 0) {\r
+    PeiExMapTableEmpty = TRUE;\r
+  } else {\r
+    PeiExMapTableEmpty = FALSE;\r
+  }\r
+  \r
   if (PeiExMapTableEmpty) {\r
-    if (*Guid != NULL) {\r
-      return EFI_NOT_FOUND;\r
-    } else {\r
-      return EFI_SUCCESS;\r
-    }\r
+    return EFI_NOT_FOUND;\r
   }\r
 \r
-  //\r
-  // Assume PCD Database AutoGen tool is sorting the ExMap based on the following order\r
-  // 1) ExGuid\r
-  // 2) ExTokenNumber\r
-  //\r
-  PeiPcdDb = GetPcdDatabase ();\r
-\r
-  ExMapTable = PeiPcdDb->Init.ExMapTable;\r
-\r
+  ExMapTable = (DYNAMICEX_MAPPING *)((UINT8 *)PeiPcdDb + PeiPcdDb->ExMapTableOffset);\r
+  GuidTable  = (EFI_GUID *)((UINT8 *)PeiPcdDb + PeiPcdDb->GuidTableOffset);\r
+  \r
   if (*Guid == NULL) {\r
     //\r
     // return the first Token Space Guid.\r
     //\r
-    *Guid = &PeiPcdDb->Init.GuidTable[ExMapTable[0].ExGuidIndex];\r
+    *Guid = GuidTable + ExMapTable[0].ExGuidIndex;\r
     return EFI_SUCCESS;\r
   }\r
 \r
-  MatchGuid = ScanGuid (PeiPcdDb->Init.GuidTable, sizeof(PeiPcdDb->Init.GuidTable), *Guid);\r
+  MatchGuid = ScanGuid (GuidTable, PeiPcdDb->GuidTableCount * sizeof(GuidTable[0]), *Guid);\r
 \r
   if (MatchGuid == NULL) {\r
     return EFI_NOT_FOUND;\r
   }\r
   \r
-  GuidTableIdx = MatchGuid - PeiPcdDb->Init.GuidTable;\r
+  GuidTableIdx = MatchGuid - GuidTable;\r
 \r
   Found = FALSE;\r
-  for (i = 0; i < PEI_EXMAPPING_TABLE_SIZE; i++) {\r
-    if (ExMapTable[i].ExGuidIndex == GuidTableIdx) {\r
+  for (Index = 0; Index < PeiPcdDb->ExTokenCount; Index++) {\r
+    if (ExMapTable[Index].ExGuidIndex == GuidTableIdx) {\r
       Found = TRUE;\r
       break;\r
     }\r
   }\r
 \r
   if (Found) {\r
-    i++;\r
-    for ( ; i < PEI_EXMAPPING_TABLE_SIZE; i++ ) {\r
-      if (ExMapTable[i].ExGuidIndex != GuidTableIdx ) {\r
-        *Guid = &PeiPcdDb->Init.GuidTable[ExMapTable[i].ExGuidIndex];\r
-        return EFI_SUCCESS;\r
+    Index++;\r
+    for ( ; Index < PeiPcdDb->ExTokenCount; Index++ ) {\r
+      if (ExMapTable[Index].ExGuidIndex != GuidTableIdx) {\r
+        Found = FALSE;\r
+        for (Index2 = 0 ; Index2 < Index; Index2++) {\r
+          if (ExMapTable[Index2].ExGuidIndex == ExMapTable[Index].ExGuidIndex) {\r
+            //\r
+            // This token namespace should have been found and output at preceding getting.\r
+            //\r
+            Found = TRUE;\r
+            break;\r
+          }\r
+        }\r
+        if (!Found) {\r
+          *Guid = (EFI_GUID *)((UINT8 *)PeiPcdDb + PeiPcdDb->GuidTableOffset) + ExMapTable[Index].ExGuidIndex;\r
+          return EFI_SUCCESS;\r
+        }\r
       }\r
     }\r
     *Guid = NULL;\r
-    return EFI_SUCCESS;\r
   }\r
 \r
   return EFI_NOT_FOUND;\r
 \r
 }\r
 \r
+/**\r
+  Get PCD value's size for POINTER type PCD.\r
+  \r
+  The POINTER type PCD's value will be stored into a buffer in specified size.\r
+  The max size of this PCD's value is described in PCD's definition in DEC file.\r
+\r
+  @param LocalTokenNumberTableIdx Index of PCD token number in PCD token table\r
+  @param MaxSize                  Maximum size of PCD's value\r
+  @param Database                 Pcd database in PEI phase.\r
+\r
+  @return PCD value's size for POINTER type PCD.\r
+\r
+**/\r
 UINTN\r
 GetPtrTypeSize (\r
   IN    UINTN             LocalTokenNumberTableIdx,\r
@@ -683,25 +1408,26 @@ GetPtrTypeSize (
   UINTN       LocalTokenNumber;\r
   SKU_ID      *SkuIdTable;\r
   SIZE_INFO   *SizeTable;\r
-  UINTN       i;\r
+  UINTN       Index;\r
 \r
   SizeTableIdx = GetSizeTableIndex (LocalTokenNumberTableIdx, Database);\r
 \r
-  LocalTokenNumber = Database->Init.LocalTokenNumberTable[LocalTokenNumberTableIdx];\r
+  LocalTokenNumber = *((UINT32 *)((UINT8 *)Database + Database->LocalTokenNumberTableOffset) + LocalTokenNumberTableIdx);\r
 \r
   ASSERT ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER);\r
   \r
-  SizeTable = Database->Init.SizeTable;\r
+  SizeTable = (SIZE_INFO *)((UINT8 *)Database + Database->SizeTableOffset);\r
 \r
   *MaxSize = SizeTable[SizeTableIdx];\r
   //\r
   // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type \r
   // PCD entry.\r
   //\r
-  if (LocalTokenNumber & PCD_TYPE_VPD) {\r
+  if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {\r
       //\r
-      // We have only one entry for VPD enabled PCD entry:\r
+      // We have only two entry for VPD enabled PCD entry:\r
       // 1) MAX Size.\r
+      // 2) Current Size\r
       // We consider current size is equal to MAX size.\r
       //\r
       return *MaxSize;\r
@@ -720,9 +1446,9 @@ GetPtrTypeSize (
       // 2) Current Size for each SKU_ID (It is equal to MaxSku).\r
       //\r
       SkuIdTable = GetSkuIdArray (LocalTokenNumberTableIdx, Database);\r
-      for (i = 0; i < SkuIdTable[0]; i++) {\r
-        if (SkuIdTable[1 + i] == Database->Init.SystemSkuId) {\r
-          return SizeTable[SizeTableIdx + 1 + i];\r
+      for (Index = 0; Index < SkuIdTable[0]; Index++) {\r
+        if (SkuIdTable[1 + Index] == Database->SystemSkuId) {\r
+          return SizeTable[SizeTableIdx + 1 + Index];\r
         }\r
       }\r
       return SizeTable[SizeTableIdx + 1];\r
@@ -730,8 +1456,20 @@ GetPtrTypeSize (
   }\r
 }\r
 \r
+/**\r
+  Set PCD value's size for POINTER type PCD.\r
+  \r
+  The POINTER type PCD's value will be stored into a buffer in specified size.\r
+  The max size of this PCD's value is described in PCD's definition in DEC file.\r
+\r
+  @param LocalTokenNumberTableIdx Index of PCD token number in PCD token table\r
+  @param CurrentSize              Maximum size of PCD's value\r
+  @param Database                 Pcd database in PEI phase.\r
 \r
+  @retval TRUE  Success to set PCD's value size, which is not exceed maximum size\r
+  @retval FALSE Fail to set PCD's value size, which maybe exceed maximum size\r
 \r
+**/\r
 BOOLEAN\r
 SetPtrTypeSize (\r
   IN          UINTN             LocalTokenNumberTableIdx,\r
@@ -743,23 +1481,23 @@ SetPtrTypeSize (
   UINTN       LocalTokenNumber;\r
   SKU_ID      *SkuIdTable;\r
   SIZE_INFO   *SizeTable;\r
-  UINTN       i;\r
+  UINTN       Index;\r
   UINTN       MaxSize;\r
   \r
   SizeTableIdx = GetSizeTableIndex (LocalTokenNumberTableIdx, Database);\r
 \r
-  LocalTokenNumber = Database->Init.LocalTokenNumberTable[LocalTokenNumberTableIdx];\r
+  LocalTokenNumber = *((UINT32 *)((UINT8 *)Database + Database->LocalTokenNumberTableOffset) + LocalTokenNumberTableIdx);\r
 \r
   ASSERT ((LocalTokenNumber & PCD_DATUM_TYPE_ALL_SET) == PCD_DATUM_TYPE_POINTER);\r
-  \r
-  SizeTable = Database->Init.SizeTable;\r
+\r
+  SizeTable = (SIZE_INFO *)((UINT8 *)Database + Database->SizeTableOffset);\r
 \r
   MaxSize = SizeTable[SizeTableIdx];\r
   //\r
   // SizeTable only contain record for PCD_DATUM_TYPE_POINTER type \r
   // PCD entry.\r
   //\r
-  if (LocalTokenNumber & PCD_TYPE_VPD) {\r
+  if ((LocalTokenNumber & PCD_TYPE_VPD) != 0) {\r
       //\r
       // We shouldn't come here as we don't support SET for VPD\r
       //\r
@@ -770,7 +1508,7 @@ SetPtrTypeSize (
       (*CurrentSize == MAX_ADDRESS)) {\r
        *CurrentSize = MaxSize;\r
        return FALSE;\r
-    } \r
+    }\r
     \r
     if ((LocalTokenNumber & PCD_TYPE_SKU_ENABLED) == 0) {\r
       //\r
@@ -787,9 +1525,9 @@ SetPtrTypeSize (
       // 2) Current Size for each SKU_ID (It is equal to MaxSku).\r
       //\r
       SkuIdTable = GetSkuIdArray (LocalTokenNumberTableIdx, Database);\r
-      for (i = 0; i < SkuIdTable[0]; i++) {\r
-        if (SkuIdTable[1 + i] == Database->Init.SystemSkuId) {\r
-          SizeTable[SizeTableIdx + 1 + i] = (SIZE_INFO) *CurrentSize;\r
+      for (Index = 0; Index < SkuIdTable[0]; Index++) {\r
+        if (SkuIdTable[1 + Index] == Database->SystemSkuId) {\r
+          SizeTable[SizeTableIdx + 1 + Index] = (SIZE_INFO) *CurrentSize;\r
           return TRUE;\r
         }\r
       }\r