]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellEnvVar.c
ShellPkg: Clean up source files
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellEnvVar.c
index 5eb382a5866f551e511848c5f892d43dd682547f..663276454273d7cb239e898134fa68ddbd85264b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   function declarations for shell environment functions.\r
 \r
-  Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2018, 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
@@ -26,14 +26,15 @@ ENV_VAR_LIST                   gShellEnvVarList;
   Reports whether an environment variable is Volatile or Non-Volatile.\r
 \r
   @param EnvVarName             The name of the environment variable in question\r
+  @param Volatile               Return TRUE if the environment variable is volatile\r
 \r
-  @retval TRUE                  This environment variable is Volatile\r
-  @retval FALSE                 This environment variable is NON-Volatile\r
+  @retval EFI_SUCCESS           The volatile attribute is returned successfully\r
+  @retval others                Some errors happened.\r
 **/\r
-BOOLEAN\r
-EFIAPI\r
+EFI_STATUS\r
 IsVolatileEnv (\r
-  IN CONST CHAR16 *EnvVarName\r
+  IN CONST CHAR16 *EnvVarName,\r
+  OUT BOOLEAN     *Volatile\r
   )\r
 {\r
   EFI_STATUS  Status;\r
@@ -41,6 +42,8 @@ IsVolatileEnv (
   VOID        *Buffer;\r
   UINT32      Attribs;\r
 \r
+  ASSERT (Volatile != NULL);\r
+\r
   Size = 0;\r
   Buffer = NULL;\r
 \r
@@ -54,7 +57,9 @@ IsVolatileEnv (
                             Buffer);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
     Buffer = AllocateZeroPool(Size);\r
-    ASSERT(Buffer != NULL);\r
+    if (Buffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
     Status = gRT->GetVariable((CHAR16*)EnvVarName,\r
                               &gShellVariableGuid,\r
                               &Attribs,\r
@@ -66,21 +71,18 @@ IsVolatileEnv (
   // not found means volatile\r
   //\r
   if (Status == EFI_NOT_FOUND) {\r
-    return (TRUE);\r
+    *Volatile = TRUE;\r
+    return EFI_SUCCESS;\r
   }\r
-  ASSERT_EFI_ERROR(Status);\r
-\r
-  //\r
-  // check for the Non Volatile bit\r
-  //\r
-  if ((Attribs & EFI_VARIABLE_NON_VOLATILE) == EFI_VARIABLE_NON_VOLATILE) {\r
-    return (FALSE);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
   }\r
 \r
   //\r
-  // everything else is volatile\r
+  // check for the Non Volatile bit\r
   //\r
-  return (TRUE);\r
+  *Volatile = !(BOOLEAN) ((Attribs & EFI_VARIABLE_NON_VOLATILE) == EFI_VARIABLE_NON_VOLATILE);\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
@@ -89,7 +91,6 @@ IsVolatileEnv (
   @param[in] List               The pointer to pointer to list.\r
 **/\r
 VOID\r
-EFIAPI\r
 FreeEnvironmentVariableList(\r
   IN LIST_ENTRY *List\r
   )\r
@@ -126,7 +127,6 @@ FreeEnvironmentVariableList(
   @retval EFI_SUCCESS           the list was created sucessfully.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 GetEnvironmentVariableList(\r
   IN OUT LIST_ENTRY *ListHead\r
   )\r
@@ -143,9 +143,9 @@ GetEnvironmentVariableList(
   if (ListHead == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
-  \r
+\r
   Status = EFI_SUCCESS;\r
-  \r
+\r
   ValBufferSize = INIT_DATA_BUFFER_SIZE;\r
   NameBufferSize = INIT_NAME_BUFFER_SIZE;\r
   VariableName = AllocateZeroPool(NameBufferSize);\r
@@ -171,14 +171,17 @@ GetEnvironmentVariableList(
       NameSize = NameBufferSize;\r
       Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
     }\r
-    \r
+\r
     if (!EFI_ERROR(Status) && CompareGuid(&Guid, &gShellVariableGuid)){\r
       VarList = AllocateZeroPool(sizeof(ENV_VAR_LIST));\r
       if (VarList == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
       } else {\r
         ValSize = ValBufferSize;\r
-        VarList->Val = AllocateZeroPool(ValSize);\r
+        //\r
+        // We need another CHAR16 to save '\0' in VarList->Val.\r
+        //\r
+        VarList->Val = AllocateZeroPool (ValSize + sizeof (CHAR16));\r
         if (VarList->Val == NULL) {\r
             SHELL_FREE_NON_NULL(VarList);\r
             Status = EFI_OUT_OF_RESOURCES;\r
@@ -188,13 +191,16 @@ GetEnvironmentVariableList(
         if (Status == EFI_BUFFER_TOO_SMALL){\r
           ValBufferSize = ValSize > ValBufferSize * 2 ? ValSize : ValBufferSize * 2;\r
           SHELL_FREE_NON_NULL (VarList->Val);\r
-          VarList->Val = AllocateZeroPool(ValBufferSize);\r
+          //\r
+          // We need another CHAR16 to save '\0' in VarList->Val.\r
+          //\r
+          VarList->Val = AllocateZeroPool (ValBufferSize + sizeof (CHAR16));\r
           if (VarList->Val == NULL) {\r
             SHELL_FREE_NON_NULL(VarList);\r
             Status = EFI_OUT_OF_RESOURCES;\r
             break;\r
           }\r
-          \r
+\r
           ValSize = ValBufferSize;\r
           Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(VariableName, &VarList->Atts, &ValSize, VarList->Val);\r
         }\r
@@ -236,7 +242,6 @@ GetEnvironmentVariableList(
   @retval EFI_SUCCESS           the list was Set sucessfully.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 SetEnvironmentVariableList(\r
   IN LIST_ENTRY *ListHead\r
   )\r
@@ -273,7 +278,7 @@ SetEnvironmentVariableList(
       ; !IsNull(ListHead, &Node->Link)\r
       ; Node = (ENV_VAR_LIST*)GetNextNode(ListHead, &Node->Link)\r
      ){\r
-    Size = StrSize(Node->Val);\r
+    Size = StrSize (Node->Val) - sizeof (CHAR16);\r
     if (Node->Atts & EFI_VARIABLE_NON_VOLATILE) {\r
       Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(Node->Key, Size, Node->Val);\r
     } else {\r
@@ -300,7 +305,6 @@ SetEnvironmentVariableList(
   @sa SetEnvironmentVariableList\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
 SetEnvironmentVariables(\r
   IN CONST CHAR16 **Environment\r
   )\r
@@ -344,8 +348,8 @@ SetEnvironmentVariables(
     //\r
     // Copy the string into the Key, leaving the last character allocated as NULL to terminate\r
     //\r
-    StrnCpyS( Node->Key, \r
-              StrStr(CurrentString, L"=") - CurrentString + 1, \r
+    StrnCpyS( Node->Key,\r
+              StrStr(CurrentString, L"=") - CurrentString + 1,\r
               CurrentString,\r
               StrStr(CurrentString, L"=") - CurrentString\r
               );\r
@@ -409,7 +413,7 @@ ShellFindEnvVarInList (
   )\r
 {\r
   ENV_VAR_LIST      *Node;\r
-  \r
+\r
   if (Key == NULL || Value == NULL || ValueSize == NULL) {\r
     return SHELL_INVALID_PARAMETER;\r
   }\r
@@ -440,8 +444,11 @@ ShellFindEnvVarInList (
                     including the tailing CHAR_NULL\r
   @param Atts       The attributes of the variable.\r
 \r
+  @retval EFI_SUCCESS  The environment variable was added to list successfully.\r
+  @retval others       Some errors happened.\r
+\r
 **/\r
-VOID\r
+EFI_STATUS\r
 ShellAddEnvVarToList (\r
   IN CONST CHAR16     *Key,\r
   IN CONST CHAR16     *Value,\r
@@ -450,9 +457,16 @@ ShellAddEnvVarToList (
   )\r
 {\r
   ENV_VAR_LIST      *Node;\r
-  \r
+  CHAR16            *LocalKey;\r
+  CHAR16            *LocalValue;\r
+\r
   if (Key == NULL || Value == NULL || ValueSize == 0) {\r
-    return;\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  LocalValue = AllocateCopyPool (ValueSize, Value);\r
+  if (LocalValue == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
   }\r
 \r
   //\r
@@ -465,10 +479,8 @@ ShellAddEnvVarToList (
     if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) {\r
       Node->Atts = Atts;\r
       SHELL_FREE_NON_NULL(Node->Val);\r
-      Node->Val  = AllocateZeroPool (ValueSize);\r
-      ASSERT (Node->Val != NULL);\r
-      CopyMem(Node->Val, Value, ValueSize);\r
-      return;\r
+      Node->Val  = LocalValue;\r
+      return EFI_SUCCESS;\r
     }\r
   }\r
 \r
@@ -476,23 +488,30 @@ ShellAddEnvVarToList (
   // If the environment varialbe key doesn't exist in list just insert\r
   // a new node.\r
   //\r
+  LocalKey = AllocateCopyPool (StrSize(Key), Key);\r
+  if (LocalKey == NULL) {\r
+    FreePool (LocalValue);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   Node = (ENV_VAR_LIST*)AllocateZeroPool (sizeof(ENV_VAR_LIST));\r
-  ASSERT (Node != NULL);\r
-  Node->Key = AllocateCopyPool(StrSize(Key), Key);\r
-  ASSERT (Node->Key != NULL);\r
-  Node->Val = AllocateCopyPool(ValueSize, Value);\r
-  ASSERT (Node->Val != NULL);\r
+  if (Node == NULL) {\r
+    FreePool (LocalKey);\r
+    FreePool (LocalValue);\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  Node->Key = LocalKey;\r
+  Node->Val = LocalValue;\r
   Node->Atts = Atts;\r
   InsertTailList(&gShellEnvVarList.Link, &Node->Link);\r
 \r
-  return;\r
+  return EFI_SUCCESS;\r
 }\r
 \r
 /**\r
   Remove a specified environment variable in gShellEnvVarList.\r
 \r
   @param Key        The name of the environment variable.\r
-  \r
+\r
   @retval EFI_SUCCESS       The command executed successfully.\r
   @retval EFI_NOT_FOUND     The environment variable is not found in\r
                             gShellEnvVarList.\r
@@ -527,7 +546,7 @@ ShellRemvoeEnvVarFromList (
 /**\r
   Initialize the gShellEnvVarList and cache all Shell-Guid-based environment\r
   variables.\r
-  \r
+\r
 **/\r
 EFI_STATUS\r
 ShellInitEnvVarList (\r