]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg Variable: Update GetNextVariableName to follow UEFI 2.7
authorStar Zeng <star.zeng@intel.com>
Thu, 22 Jun 2017 09:30:39 +0000 (17:30 +0800)
committerStar Zeng <star.zeng@intel.com>
Tue, 27 Jun 2017 05:57:39 +0000 (13:57 +0800)
"The size must be large enough to fit input string supplied in
VariableName buffer" is added in the description for VariableNameSize.
And two cases of EFI_INVALID_PARAMETER are added.
1. The input values of VariableName and VendorGuid are not a name and
   GUID of an existing variable.
2. Null-terminator is not found in the first VariableNameSize bytes of
   the input VariableName buffer.

This patch is to update code to follow them.

Cc: Liming Gao <liming.gao@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Universal/Variable/EmuRuntimeDxe/EmuVariable.c
MdeModulePkg/Universal/Variable/EmuRuntimeDxe/InitVariable.c
MdeModulePkg/Universal/Variable/EmuRuntimeDxe/Variable.h
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.c
MdeModulePkg/Universal/Variable/RuntimeDxe/Variable.h

index 27ea1496a04419b874e96ce9ed7110b679b3d685..6dee2b6add4b4c4ca048a3530c705d6ef00af965 100644 (file)
@@ -3,7 +3,7 @@
   Emulation Variable services operate on the runtime volatile memory.\r
   The nonvolatile variable space doesn't exist.\r
 \r
-Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<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
@@ -1233,18 +1233,23 @@ Done:
 \r
   This code Finds the Next available variable.\r
 \r
-  @param  VariableNameSize       Size of the variable.\r
+  @param  VariableNameSize       The size of the VariableName buffer. The size must be large enough to fit input\r
+                                 string supplied in VariableName buffer.\r
   @param  VariableName           On input, supplies the last VariableName that was returned by GetNextVariableName().\r
                                  On output, returns the Null-terminated Unicode string of the current variable.\r
   @param  VendorGuid             On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
-                                 On output, returns the VendorGuid of the current variable.  \r
+                                 On output, returns the VendorGuid of the current variable.\r
   @param  Global                 Pointer to VARIABLE_GLOBAL structure.\r
 \r
-  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_SUCCESS            The function completed successfully.\r
   @retval EFI_NOT_FOUND          The next variable was not found.\r
-  @retval EFI_BUFFER_TOO_SMALL   VariableNameSize is too small for the result. \r
+  @retval EFI_BUFFER_TOO_SMALL   The VariableNameSize is too small for the result.\r
                                  VariableNameSize has been updated with the size needed to complete the request.\r
   @retval EFI_INVALID_PARAMETER  VariableNameSize or VariableName or VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER  The input values of VariableName and VendorGuid are not a name and\r
+                                 GUID of an existing variable.\r
+  @retval EFI_INVALID_PARAMETER  Null-terminator is not found in the first VariableNameSize bytes of\r
+                                 the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -1259,16 +1264,42 @@ EmuGetNextVariableName (
   VARIABLE_POINTER_TRACK  Variable;\r
   UINTN                   VarNameSize;\r
   EFI_STATUS              Status;\r
+  UINTN                   MaxLen;\r
 \r
   if (VariableNameSize == NULL || VariableName == NULL || VendorGuid == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Calculate the possible maximum length of name string, including the Null terminator.\r
+  //\r
+  MaxLen = *VariableNameSize / sizeof (CHAR16);\r
+  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
+    //\r
+    // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
+    // follow spec to return EFI_INVALID_PARAMETER.\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   AcquireLockOnlyAtBootTime(&Global->VariableServicesLock);\r
 \r
   Status = FindVariable (VariableName, VendorGuid, &Variable, Global);\r
 \r
   if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
+    //\r
+    // For VariableName is an empty string, FindVariable() will try to find and return\r
+    // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
+    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
+    //\r
+    if (VariableName[0] != 0) {\r
+      //\r
+      // For VariableName is not an empty string, and FindVariable() returns error as\r
+      // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
+      // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
+      //\r
+      Status = EFI_INVALID_PARAMETER;\r
+    }\r
     goto Done;\r
   }\r
 \r
index 1feedc07c332eb0180a3b8e635d1a52f8f8e8102..309a4b8dbfeb61e8b2e599b0a7dcd7aa1c6dc826 100644 (file)
@@ -60,14 +60,22 @@ RuntimeServiceGetVariable (
 \r
   This code Finds the Next available variable.\r
 \r
-  @param VariableNameSize           Size of the variable name\r
-  @param VariableName               Pointer to variable name\r
-  @param VendorGuid                 Variable Vendor Guid\r
-\r
-  @return EFI_INVALID_PARAMETER     Invalid parameter\r
-  @return EFI_SUCCESS               Find the specified variable\r
-  @return EFI_NOT_FOUND             Not found\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result\r
+  @param  VariableNameSize       The size of the VariableName buffer. The size must be large enough to fit input\r
+                                 string supplied in VariableName buffer.\r
+  @param  VariableName           On input, supplies the last VariableName that was returned by GetNextVariableName().\r
+                                 On output, returns the Null-terminated Unicode string of the current variable.\r
+  @param  VendorGuid             On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
+                                 On output, returns the VendorGuid of the current variable.\r
+\r
+  @retval EFI_SUCCESS            The function completed successfully.\r
+  @retval EFI_NOT_FOUND          The next variable was not found.\r
+  @retval EFI_BUFFER_TOO_SMALL   The VariableNameSize is too small for the result.\r
+                                 VariableNameSize has been updated with the size needed to complete the request.\r
+  @retval EFI_INVALID_PARAMETER  VariableNameSize or VariableName or VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER  The input values of VariableName and VendorGuid are not a name and\r
+                                 GUID of an existing variable.\r
+  @retval EFI_INVALID_PARAMETER  Null-terminator is not found in the first VariableNameSize bytes of\r
+                                 the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r
index 81a45681a2316a130255f1d7473b55c78d79afe1..985f56791909137dd26f8a1c53a631f90e81d468 100644 (file)
@@ -3,7 +3,7 @@
   The internal header file includes the common header files, defines\r
   internal structure and functions used by EmuVariable module.\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<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
@@ -173,18 +173,23 @@ EmuGetVariable (
 \r
   This code finds the next available variable.\r
 \r
-  @param  VariableNameSize       Size of the variable.\r
+  @param  VariableNameSize       The size of the VariableName buffer. The size must be large enough to fit input\r
+                                 string supplied in VariableName buffer.\r
   @param  VariableName           On input, supplies the last VariableName that was returned by GetNextVariableName().\r
                                  On output, returns the Null-terminated Unicode string of the current variable.\r
   @param  VendorGuid             On input, supplies the last VendorGuid that was returned by GetNextVariableName().\r
-                                 On output, returns the VendorGuid of the current variable.  \r
+                                 On output, returns the VendorGuid of the current variable.\r
   @param  Global                 Pointer to VARIABLE_GLOBAL structure.\r
 \r
-  @retval EFI_SUCCESS            The function completed successfully. \r
+  @retval EFI_SUCCESS            The function completed successfully.\r
   @retval EFI_NOT_FOUND          The next variable was not found.\r
-  @retval EFI_BUFFER_TOO_SMALL   VariableNameSize is too small for the result. \r
+  @retval EFI_BUFFER_TOO_SMALL   The VariableNameSize is too small for the result.\r
                                  VariableNameSize has been updated with the size needed to complete the request.\r
   @retval EFI_INVALID_PARAMETER  VariableNameSize or VariableName or VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER  The input values of VariableName and VendorGuid are not a name and\r
+                                 GUID of an existing variable.\r
+  @retval EFI_INVALID_PARAMETER  Null-terminator is not found in the first VariableNameSize bytes of\r
+                                 the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r
index 0a325de1659d3eaf2d35f193b065212ea697fd80..71a6fd20936401d45c616df0e56facd42b1e2c84 100644 (file)
@@ -2905,8 +2905,11 @@ Done:
   @param[in]  VendorGuid    Variable Vendor Guid.\r
   @param[out] VariablePtr   Pointer to variable header address.\r
 \r
-  @return EFI_SUCCESS       Find the specified variable.\r
-  @return EFI_NOT_FOUND     Not found.\r
+  @retval EFI_SUCCESS           The function completed successfully.\r
+  @retval EFI_NOT_FOUND         The next variable was not found.\r
+  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
+                                GUID of an existing variable.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -2926,6 +2929,19 @@ VariableServiceGetNextVariableInternal (
 \r
   Status = FindVariable (VariableName, VendorGuid, &Variable, &mVariableModuleGlobal->VariableGlobal, FALSE);\r
   if (Variable.CurrPtr == NULL || EFI_ERROR (Status)) {\r
+    //\r
+    // For VariableName is an empty string, FindVariable() will try to find and return\r
+    // the first qualified variable, and if FindVariable() returns error (EFI_NOT_FOUND)\r
+    // as no any variable is found, still go to return the error (EFI_NOT_FOUND).\r
+    //\r
+    if (VariableName[0] != 0) {\r
+      //\r
+      // For VariableName is not an empty string, and FindVariable() returns error as\r
+      // VariableName and VendorGuid are not a name and GUID of an existing variable,\r
+      // there is no way to get next variable, follow spec to return EFI_INVALID_PARAMETER.\r
+      //\r
+      Status = EFI_INVALID_PARAMETER;\r
+    }\r
     goto Done;\r
   }\r
 \r
@@ -3046,14 +3062,22 @@ Done:
   Caution: This function may receive untrusted input.\r
   This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
 \r
-  @param VariableNameSize           Size of the variable name.\r
+  @param VariableNameSize           The size of the VariableName buffer. The size must be large\r
+                                    enough to fit input string supplied in VariableName buffer.\r
   @param VariableName               Pointer to variable name.\r
   @param VendorGuid                 Variable Vendor Guid.\r
 \r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
+  @retval EFI_SUCCESS               The function completed successfully.\r
+  @retval EFI_NOT_FOUND             The next variable was not found.\r
+  @retval EFI_BUFFER_TOO_SMALL      The VariableNameSize is too small for the result.\r
+                                    VariableNameSize has been updated with the size needed to complete the request.\r
+  @retval EFI_INVALID_PARAMETER     VariableNameSize is NULL.\r
+  @retval EFI_INVALID_PARAMETER     VariableName is NULL.\r
+  @retval EFI_INVALID_PARAMETER     VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER     The input values of VariableName and VendorGuid are not a name and\r
+                                    GUID of an existing variable.\r
+  @retval EFI_INVALID_PARAMETER     Null-terminator is not found in the first VariableNameSize bytes of\r
+                                    the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -3065,6 +3089,7 @@ VariableServiceGetNextVariableName (
   )\r
 {\r
   EFI_STATUS              Status;\r
+  UINTN                   MaxLen;\r
   UINTN                   VarNameSize;\r
   VARIABLE_HEADER         *VariablePtr;\r
 \r
@@ -3072,6 +3097,18 @@ VariableServiceGetNextVariableName (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // Calculate the possible maximum length of name string, including the Null terminator.\r
+  //\r
+  MaxLen = *VariableNameSize / sizeof (CHAR16);\r
+  if ((MaxLen == 0) || (StrnLenS (VariableName, MaxLen) == MaxLen)) {\r
+    //\r
+    // Null-terminator is not found in the first VariableNameSize bytes of the input VariableName buffer,\r
+    // follow spec to return EFI_INVALID_PARAMETER.\r
+    //\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   AcquireLockOnlyAtBootTime(&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);\r
 \r
   Status = VariableServiceGetNextVariableInternal (VariableName, VendorGuid, &VariablePtr);\r
index cd0d9568158a1d59594eba7f484db5b08d2036e9..8b1b1332b3da881206ebaf9e229cfb56b0038d1b 100644 (file)
@@ -543,8 +543,11 @@ VariableServiceGetVariable (
   @param[in] VendorGuid     Variable Vendor Guid.\r
   @param[out] VariablePtr   Pointer to variable header address.\r
 \r
-  @return EFI_SUCCESS       Find the specified variable.\r
-  @return EFI_NOT_FOUND     Not found.\r
+  @retval EFI_SUCCESS           The function completed successfully.\r
+  @retval EFI_NOT_FOUND         The next variable was not found.\r
+  @retval EFI_INVALID_PARAMETER If VariableName is not an empty string, while VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER The input values of VariableName and VendorGuid are not a name and\r
+                                GUID of an existing variable.\r
 \r
 **/\r
 EFI_STATUS\r
@@ -562,14 +565,22 @@ VariableServiceGetNextVariableInternal (
   Caution: This function may receive untrusted input.\r
   This function may be invoked in SMM mode. This function will do basic validation, before parse the data.\r
 \r
-  @param VariableNameSize           Size of the variable name.\r
+  @param VariableNameSize           The size of the VariableName buffer. The size must be large\r
+                                    enough to fit input string supplied in VariableName buffer.\r
   @param VariableName               Pointer to variable name.\r
   @param VendorGuid                 Variable Vendor Guid.\r
 \r
-  @return EFI_INVALID_PARAMETER     Invalid parameter.\r
-  @return EFI_SUCCESS               Find the specified variable.\r
-  @return EFI_NOT_FOUND             Not found.\r
-  @return EFI_BUFFER_TO_SMALL       DataSize is too small for the result.\r
+  @retval EFI_SUCCESS               The function completed successfully.\r
+  @retval EFI_NOT_FOUND             The next variable was not found.\r
+  @retval EFI_BUFFER_TOO_SMALL      The VariableNameSize is too small for the result.\r
+                                    VariableNameSize has been updated with the size needed to complete the request.\r
+  @retval EFI_INVALID_PARAMETER     VariableNameSize is NULL.\r
+  @retval EFI_INVALID_PARAMETER     VariableName is NULL.\r
+  @retval EFI_INVALID_PARAMETER     VendorGuid is NULL.\r
+  @retval EFI_INVALID_PARAMETER     The input values of VariableName and VendorGuid are not a name and\r
+                                    GUID of an existing variable.\r
+  @retval EFI_INVALID_PARAMETER     Null-terminator is not found in the first VariableNameSize bytes of\r
+                                    the input VariableName buffer.\r
 \r
 **/\r
 EFI_STATUS\r