]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/DxePcdLib/DxePcdLib.c
follow up the comments from code review.
[mirror_edk2.git] / MdePkg / Library / DxePcdLib / DxePcdLib.c
index c20fad23023a0474bbf0dfbea03de7d34a1ae7cc..e780bd32a24d0b3a4103388177767e3767599310 100644 (file)
@@ -11,16 +11,25 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
 \r
 \r
-Module Name: DxePcdLib.c\r
-\r
 **/\r
 \r
-static PCD_PROTOCOL  *mPcd;\r
+\r
+#include <PiDxe.h>\r
+\r
+#include <Protocol/Pcd.h>\r
+\r
+#include <Library/PcdLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/UefiBootServicesTableLib.h>\r
+#include <Library/BaseMemoryLib.h>\r
+\r
+PCD_PROTOCOL  *mPcd = NULL;\r
+\r
 \r
 /**\r
   The constructor function caches the PCD_PROTOCOL pointer.\r
 \r
-  @param[in] ImageHandle The firmware allocated handle for the EFI image.      \r
+  @param[in] ImageHandle The firmware allocated handle for the EFI image.  \r
   @param[in] SystemTable A pointer to the EFI System Table.\r
   \r
   @retval EFI_SUCCESS The constructor always return EFI_SUCCESS.\r
@@ -35,18 +44,24 @@ PcdLibConstructor (
 {\r
   EFI_STATUS  Status;\r
 \r
+  //\r
+  // PCD protocol has not been installed, but a module needs to access a\r
+  // dynamic PCD entry.\r
+  // \r
   Status = gBS->LocateProtocol (&gPcdProtocolGuid, NULL, (VOID **)&mPcd);\r
   ASSERT_EFI_ERROR (Status);\r
+  ASSERT (mPcd!= NULL);\r
 \r
-  return EFI_SUCCESS;\r
+  return Status;\r
 }\r
 \r
 \r
 /**\r
   Sets the current SKU in the PCD database to the value specified by SkuId.  SkuId is returned.\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
+  If SkuId is not less than PCD_MAX_SKU_ID, then ASSERT().\r
+  \r
+  @param[in]  SkuId     System SKU ID. The SKU value that will be used when the PCD service will retrieve and \r
+                        set values.\r
 \r
   @retval SKU_ID Return the SKU ID that just be set.\r
 \r
@@ -57,6 +72,8 @@ LibPcdSetSku (
   IN UINTN  SkuId\r
   )\r
 {\r
+  ASSERT (SkuId < PCD_MAX_SKU_ID);\r
+\r
   mPcd->SetSku (SkuId);\r
 \r
   return SkuId;\r
@@ -67,7 +84,7 @@ LibPcdSetSku (
 /**\r
   Returns the 8-bit value for the token specified by TokenNumber. \r
 \r
-  @param[in]  The PCD token number to retrieve a current value for.\r
+  @param[in]  TokenNumber   The PCD token number to retrieve a current value for.\r
 \r
   @retval UINT8 Returns the 8-bit value for the token specified by TokenNumber. \r
 \r
@@ -86,7 +103,7 @@ LibPcdGet8 (
 /**\r
   Returns the 16-bit value for the token specified by TokenNumber. \r
 \r
-  @param[in]  The PCD token number to retrieve a current value for.\r
+  @param[in]  TokenNumber   The PCD token number to retrieve a current value for.\r
 \r
   @retval UINT16 Returns the 16-bit value for the token specified by TokenNumber. \r
 \r
@@ -373,6 +390,7 @@ LibPcdGetExSize (
 /**\r
   Sets the 8-bit value for the token specified by TokenNumber \r
   to the value specified by Value.  Value is returned.\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
   @param[in]  Value The 8-bit value to set.\r
@@ -401,6 +419,7 @@ LibPcdSet8 (
 /**\r
   Sets the 16-bit value for the token specified by TokenNumber \r
   to the value specified by Value.  Value is returned.\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
   @param[in]  Value The 16-bit value to set.\r
@@ -429,6 +448,7 @@ LibPcdSet16 (
 /**\r
   Sets the 32-bit value for the token specified by TokenNumber \r
   to the value specified by Value.  Value is returned.\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
   @param[in]  Value The 32-bit value to set.\r
@@ -456,6 +476,7 @@ LibPcdSet32 (
 /**\r
   Sets the 64-bit value for the token specified by TokenNumber \r
   to the value specified by Value.  Value is returned.\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
   @param[in]  Value The 64-bit value to set.\r
@@ -483,11 +504,22 @@ LibPcdSet64 (
 \r
 /**\r
   Sets a buffer for the token specified by TokenNumber to \r
-  the value specified by Value. Value is returned.\r
-  If Value is NULL, then ASSERT().\r
+  the value specified by Buffer and SizeOfBuffer.  Buffer to\r
+  be set is returned. The content of the buffer could be \r
+  overwritten if a Callback on SET is registered with this\r
+  TokenNumber.\r
   \r
-  @param[in]  TokenNumber The PCD token number to set a current value for.\r
-  @param[in]  Value A pointer to the buffer to set.\r
+  If SizeOfBuffer is greater than the maximum \r
+  size support by TokenNumber, then set SizeOfBuffer to the \r
+  maximum size supported by TokenNumber and return NULL to \r
+  indicate that the set operation was not actually performed. \r
+  \r
+  If SizeOfValue > 0 and Buffer is NULL, then ASSERT().\r
+  \r
+  @param[in]      TokenNumber   The PCD token number to set a current value for.\r
+  @param[in, out] SizeOfBuffer  The size, in bytes, of Buffer.\r
+                                In out, returns actual size of buff is set. \r
+  @param[in]      Buffer        A pointer to the buffer to set.\r
 \r
   @retval VOID* Return the pointer for the buffer been set.\r
 \r
@@ -495,18 +527,24 @@ LibPcdSet64 (
 VOID *\r
 EFIAPI\r
 LibPcdSetPtr (\r
-  IN UINTN             TokenNumber,\r
-  IN UINTN             SizeOfBuffer,\r
-  IN VOID              *Buffer\r
+  IN      UINTN             TokenNumber,\r
+  IN OUT  UINTN             *SizeOfBuffer,\r
+  IN      VOID              *Buffer\r
   )\r
 {\r
   EFI_STATUS Status;\r
-  \r
-  ASSERT (Buffer != NULL);\r
+\r
+  ASSERT (SizeOfBuffer != NULL);\r
+\r
+  if (*SizeOfBuffer > 0) {\r
+    ASSERT (Buffer != NULL);\r
+  }\r
 \r
   Status = mPcd->SetPtr (TokenNumber, SizeOfBuffer, Buffer);\r
 \r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
 \r
   return Buffer;\r
 }\r
@@ -516,9 +554,10 @@ LibPcdSetPtr (
 /**\r
   Sets the Boolean value for the token specified by TokenNumber \r
   to the value specified by Value.  Value is returned.\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
-  @param[in]  Value The boolean value to set.\r
+  @param[in]  Value       The boolean value to set.\r
 \r
   @retval BOOLEAN Return the value been set.\r
 \r
@@ -545,6 +584,7 @@ LibPcdSetBool (
   Sets the 8-bit value for the token specified by TokenNumber and \r
   Guid to the value specified by Value. Value is returned.\r
   If Guid is NULL, then ASSERT().\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  Guid Pointer to a 128-bit unique value that \r
               designates which namespace to set a value from.\r
@@ -579,13 +619,14 @@ LibPcdSetEx8 (
   Sets the 16-bit value for the token specified by TokenNumber and \r
   Guid to the value specified by Value. Value is returned.\r
   If Guid is NULL, then ASSERT().\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  Guid Pointer to a 128-bit unique value that \r
               designates which namespace to set a value from.\r
   @param[in]  TokenNumber The PCD token number to set a current value for.\r
   @param[in]  Value The 16-bit value to set.\r
 \r
-  @retval UINT8 Return the value been set.\r
+  @retval UINT16 Return the value been set.\r
 \r
 **/\r
 UINT16\r
@@ -613,6 +654,7 @@ LibPcdSetEx16 (
   Sets the 32-bit value for the token specified by TokenNumber and \r
   Guid to the value specified by Value. Value is returned.\r
   If Guid is NULL, then ASSERT().\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  Guid Pointer to a 128-bit unique value that \r
               designates which namespace to set a value from.\r
@@ -678,36 +720,48 @@ LibPcdSetEx64 (
 \r
 \r
 /**\r
-  Sets a buffer for the token specified by TokenNumber and \r
-  Guid to the value specified by Value. Value is returned.\r
-  If Guid is NULL, then ASSERT().\r
-  If Value is NULL, then ASSERT().\r
+  Sets a buffer for the token specified by TokenNumber to the value specified by \r
+  Buffer and SizeOfBuffer.  Buffer is returned.  If SizeOfBuffer is greater than \r
+  the maximum size support by TokenNumber, then set SizeOfBuffer to the maximum size \r
+  supported by TokenNumber and return NULL to indicate that the set operation \r
+  was not actually performed. \r
   \r
-  @param[in]  Guid Pointer to a 128-bit unique value that \r
-              designates which namespace to set a value from.\r
-  @param[in]  TokenNumber The PCD token number to set a current value for.\r
-  @param[in]  Value The 8-bit value to set.\r
+  If SizeOfBuffer > 0 and Buffer is NULL, then ASSERT().\r
+  \r
+  @param[in]        Guid Pointer to a 128-bit unique value that \r
+                    designates which namespace to set a value from.\r
+  @param[in]        TokenNumber The PCD token number to set a current value for.\r
+  @param[in, out]   SizeOfBuffer The size, in bytes, of Buffer.\r
+                    In out, returns actual size of buffer is set.\r
+  @param[in]        Buffer A pointer to the buffer to set.\r
 \r
-  @retval VOID * Return the value been set.\r
+  @retval VOID * Return the pinter to the buffer been set.\r
 \r
 **/\r
 VOID *\r
 EFIAPI\r
 LibPcdSetExPtr (\r
-  IN CONST GUID        *Guid,\r
-  IN UINTN             TokenNumber,\r
-  IN UINTN             SizeOfBuffer,\r
-  IN VOID              *Buffer\r
+  IN      CONST GUID        *Guid,\r
+  IN      UINTN             TokenNumber,\r
+  IN OUT  UINTN             *SizeOfBuffer,\r
+  IN      VOID              *Buffer\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   ASSERT (Guid != NULL);\r
-  ASSERT (Buffer != NULL);\r
+\r
+  ASSERT (SizeOfBuffer != NULL);\r
+\r
+  if (*SizeOfBuffer > 0) {\r
+    ASSERT (Buffer != NULL);\r
+  }\r
 \r
   Status = mPcd->SetPtrEx (Guid, TokenNumber, SizeOfBuffer, Buffer);\r
 \r
-  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status)) {\r
+    return NULL;\r
+  }\r
 \r
   return Buffer;\r
 }\r
@@ -718,6 +772,7 @@ LibPcdSetExPtr (
   Sets the Boolean value for the token specified by TokenNumber and \r
   Guid to the value specified by Value. Value is returned.\r
   If Guid is NULL, then ASSERT().\r
+  If fail to set pcd value, then ASSERT_EFI_ERROR().\r
   \r
   @param[in]  Guid Pointer to a 128-bit unique value that \r
               designates which namespace to set a value from.\r
@@ -753,7 +808,8 @@ LibPcdSetExBool (
   then notification function specified by NotificationFunction is called.  \r
   If Guid is NULL, then the default token space is used. \r
   If NotificationFunction is NULL, then ASSERT().\r
-\r
+  If fail to set callback function, then ASSERT_EFI_ERROR().\r
+  \r
   @param[in]  Guid Pointer to a 128-bit unique value that designates which \r
               namespace to set a value from.  If NULL, then the default \r
               token space is used.\r
@@ -776,7 +832,7 @@ LibPcdCallbackOnSet (
 \r
   ASSERT (NotificationFunction != NULL);\r
 \r
-  Status = mPcd->CallbackOnSet (TokenNumber, Guid, NotificationFunction);\r
+  Status = mPcd->CallbackOnSet (Guid, TokenNumber, NotificationFunction);\r
 \r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -788,7 +844,8 @@ LibPcdCallbackOnSet (
 /**\r
   Disable a notification function that was established with LibPcdCallbackonSet().\r
   If NotificationFunction is NULL, then ASSERT().\r
-\r
+  If fail to cancel callback function, then ASSERT_EFI_ERROR().\r
+  \r
   @param[in]  Guid Specify the GUID token space.\r
   @param[in]  TokenNumber Specify the token number.\r
   @param[in]  NotificationFunction The callback function to be unregistered.\r
@@ -808,7 +865,7 @@ LibPcdCancelCallback (
 \r
   ASSERT (NotificationFunction != NULL);\r
     \r
-  Status = mPcd->CancelCallback (TokenNumber, Guid, NotificationFunction);\r
+  Status = mPcd->CancelCallback (Guid, TokenNumber, NotificationFunction);\r
 \r
   ASSERT_EFI_ERROR (Status);\r
 \r
@@ -824,11 +881,12 @@ LibPcdCancelCallback (
   follows TokenNumber in the token space is returned.  If TokenNumber is the last \r
   token number in the token space, then 0 is returned.  If TokenNumber is not 0 and \r
   is not in the token space specified by Guid, then ASSERT().\r
+  If Fail to get next token, then ASSERT_EFI_ERROR().\r
 \r
-  @param[in]  Pointer to a 128-bit unique value that designates which namespace \r
-              to set a value from.  If NULL, then the default token space is used.\r
-  @param[in]  The previous PCD token number.  If 0, then retrieves the first PCD \r
-              token number.\r
+  @param[in]  Guid        Pointer to a 128-bit unique value that designates which namespace \r
+                          to set a value from.  If NULL, then the default token space is used.\r
+  @param[in]  TokenNumber The previous PCD token number.  If 0, then retrieves the first PCD \r
+                          token number.\r
 \r
   @retval UINTN            The next valid token number.\r
 \r
@@ -849,3 +907,86 @@ LibPcdGetNextToken (
   return TokenNumber;\r
 }\r
 \r
+\r
+\r
+/**\r
+  Retrieves the next PCD token space from a token space specified by Guid.\r
+  Guid of NULL is reserved to mark the default local token namespace on the current\r
+  platform. If Guid is NULL, then the GUID of the first non-local token space of the \r
+  current platform is returned. If Guid is the last non-local token space, \r
+  then NULL is returned. \r
+\r
+  If Guid is not NULL and is not a valid token space in the current platform, then ASSERT().\r
+  If fail to get next token space, then ASSERT_EFI_ERROR().\r
+  \r
+  @param[in]  Guid  Pointer to a 128-bit unique value that designates from which namespace \r
+                    to start the search.\r
+\r
+  @retval CONST GUID *  The next valid token namespace.\r
+\r
+**/\r
+GUID *           \r
+EFIAPI\r
+LibPcdGetNextTokenSpace (\r
+  IN CONST GUID  *Guid\r
+  )\r
+{\r
+  EFI_STATUS Status;\r
+\r
+  Status = mPcd->GetNextTokenSpace (&Guid);\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return (GUID *) Guid;\r
+}\r
+\r
+\r
+/**\r
+  Sets the PCD entry specified by PatchVariable to the value specified by Buffer \r
+  and SizeOfBuffer.  Buffer is returned.  If SizeOfBuffer is greater than \r
+  MaximumDatumSize, then set SizeOfBuffer to MaximumDatumSize and return \r
+  NULL to indicate that the set operation was not actually performed.  \r
+  If SizeOfBuffer is set to MAX_ADDRESS, then SizeOfBuffer must be set to \r
+  MaximumDatumSize and NULL must be returned.\r
+  \r
+  If PatchVariable is NULL, then ASSERT().\r
+  If SizeOfBuffer is NULL, then ASSERT().\r
+  If SizeOfBuffer > 0 and Buffer is NULL, then ASSERT().\r
+\r
+  @param[in] PatchVariable      A pointer to the global variable in a module that is \r
+                                the target of the set operation.\r
+  @param[in] MaximumDatumSize   The maximum size allowed for the PCD entry specified by PatchVariable.\r
+  @param[in, out] SizeOfBuffer  A pointer to the size, in bytes, of Buffer.\r
+                                In out, returns actual size of buffer is set.\r
+  @param[in] Buffer             A pointer to the buffer to used to set the target variable.\r
+\r
+**/\r
+VOID *\r
+EFIAPI\r
+LibPatchPcdSetPtr (\r
+  IN        VOID        *PatchVariable,\r
+  IN        UINTN       MaximumDatumSize,\r
+  IN OUT    UINTN       *SizeOfBuffer,\r
+  IN CONST  VOID        *Buffer\r
+  )\r
+{\r
+  ASSERT (PatchVariable != NULL);\r
+  ASSERT (SizeOfBuffer  != NULL);\r
+  \r
+  if (*SizeOfBuffer > 0) {\r
+    ASSERT (Buffer != NULL);\r
+  }\r
+\r
+  if ((*SizeOfBuffer > MaximumDatumSize) ||\r
+      (*SizeOfBuffer == MAX_ADDRESS)) {\r
+    *SizeOfBuffer = MaximumDatumSize;\r
+    return NULL;\r
+  }\r
+    \r
+  CopyMem (PatchVariable, Buffer, *SizeOfBuffer);\r
+  \r
+  return (VOID *) Buffer;\r
+}\r
+\r
+\r
+\r