]> git.proxmox.com Git - mirror_edk2.git/commitdiff
ShellPkg : Cache the environment variable into memory to enhance
authorQiu Shumin <shumin.qiu@intel.com>
Fri, 1 Apr 2016 01:02:20 +0000 (09:02 +0800)
committerQiu Shumin <shumin.qiu@intel.com>
Fri, 15 Apr 2016 03:10:30 +0000 (11:10 +0800)
the performance.

Currently UEFI Shell reads variable storage to get the environment
variables every time running a new command. And reading(writing)
UEFI variables is a high cost operation on most platforms. In order
to enhance the performance this patch read the variable storage once
and cache the environment variables in memory. Every further 'set'
command will save the variable not only to Shell cache, but also the
flash variable storage.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Qiu Shumin <shumin.qiu@intel.com>
Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by:Ruiyu Ni <ruiyu.ni@intel.com>

ShellPkg/Application/Shell/Shell.c
ShellPkg/Application/Shell/ShellEnvVar.c
ShellPkg/Application/Shell/ShellEnvVar.h
ShellPkg/Application/Shell/ShellProtocol.c

index bd695a45a4fea4a6aab8264062f0ca5a66895e3f..12daff91912873d57cb5d831c5eba9155303918a 100644 (file)
@@ -445,6 +445,8 @@ UefiMain (
     Status = CommandInit();\r
     ASSERT_EFI_ERROR(Status);\r
 \r
     Status = CommandInit();\r
     ASSERT_EFI_ERROR(Status);\r
 \r
+    Status = ShellInitEnvVarList ();\r
+\r
     //\r
     // Check the command line\r
     //\r
     //\r
     // Check the command line\r
     //\r
@@ -702,6 +704,8 @@ FreeResources:
     DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
   }\r
 \r
     DEBUG_CODE(ShellInfoObject.ConsoleInfo = NULL;);\r
   }\r
 \r
+  ShellFreeEnvVarList ();\r
+\r
   if (ShellCommandGetExit()) {\r
     return ((EFI_STATUS)ShellCommandGetExitCode());\r
   }\r
   if (ShellCommandGetExit()) {\r
     return ((EFI_STATUS)ShellCommandGetExitCode());\r
   }\r
index a8f177e6adae3dd7cc8100e589a6eb5bebbc3fe9..5eb382a5866f551e511848c5f892d43dd682547f 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   function declarations for shell environment functions.\r
 \r
 /** @file\r
   function declarations for shell environment functions.\r
 \r
-  Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2016, 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
   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
 #define INIT_NAME_BUFFER_SIZE  128\r
 #define INIT_DATA_BUFFER_SIZE  1024\r
 \r
 #define INIT_NAME_BUFFER_SIZE  128\r
 #define INIT_DATA_BUFFER_SIZE  1024\r
 \r
+//\r
+// The list is used to cache the environment variables.\r
+//\r
+ENV_VAR_LIST                   gShellEnvVarList;\r
+\r
 /**\r
   Reports whether an environment variable is Volatile or Non-Volatile.\r
 \r
 /**\r
   Reports whether an environment variable is Volatile or Non-Volatile.\r
 \r
@@ -379,3 +384,176 @@ SetEnvironmentVariables(
   //\r
   return (SetEnvironmentVariableList(&VarList->Link));\r
 }\r
   //\r
   return (SetEnvironmentVariableList(&VarList->Link));\r
 }\r
+\r
+/**\r
+  Find an environment variable in the gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+  @param Value      The value of the environment variable, the buffer\r
+                    shoule be freed by the caller.\r
+  @param ValueSize  The size in bytes of the environment variable\r
+                    including the tailing CHAR_NELL.\r
+  @param Atts       The attributes of the variable.\r
+\r
+  @retval EFI_SUCCESS       The command executed successfully.\r
+  @retval EFI_NOT_FOUND     The environment variable is not found in\r
+                            gShellEnvVarList.\r
+\r
+**/\r
+EFI_STATUS\r
+ShellFindEnvVarInList (\r
+  IN  CONST CHAR16    *Key,\r
+  OUT CHAR16          **Value,\r
+  OUT UINTN           *ValueSize,\r
+  OUT UINT32          *Atts OPTIONAL\r
+  )\r
+{\r
+  ENV_VAR_LIST      *Node;\r
+  \r
+  if (Key == NULL || Value == NULL || ValueSize == NULL) {\r
+    return SHELL_INVALID_PARAMETER;\r
+  }\r
+\r
+  for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
+      ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
+      ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
+     ){\r
+    if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) {\r
+      *Value      = AllocateCopyPool(StrSize(Node->Val), Node->Val);\r
+      *ValueSize  = StrSize(Node->Val);\r
+      if (Atts != NULL) {\r
+        *Atts = Node->Atts;\r
+      }\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Add an environment variable into gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+  @param Value      The value of environment variable.\r
+  @param ValueSize  The size in bytes of the environment variable\r
+                    including the tailing CHAR_NULL\r
+  @param Atts       The attributes of the variable.\r
+\r
+**/\r
+VOID\r
+ShellAddEnvVarToList (\r
+  IN CONST CHAR16     *Key,\r
+  IN CONST CHAR16     *Value,\r
+  IN UINTN            ValueSize,\r
+  IN UINT32           Atts\r
+  )\r
+{\r
+  ENV_VAR_LIST      *Node;\r
+  \r
+  if (Key == NULL || Value == NULL || ValueSize == 0) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Update the variable value if it exists in gShellEnvVarList.\r
+  //\r
+  for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
+      ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
+      ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
+     ){\r
+    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
+    }\r
+  }\r
+\r
+  //\r
+  // If the environment varialbe key doesn't exist in list just insert\r
+  // a new node.\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
+  Node->Atts = Atts;\r
+  InsertTailList(&gShellEnvVarList.Link, &Node->Link);\r
+\r
+  return;\r
+}\r
+\r
+/**\r
+  Remove a specified environment variable in gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+  \r
+  @retval EFI_SUCCESS       The command executed successfully.\r
+  @retval EFI_NOT_FOUND     The environment variable is not found in\r
+                            gShellEnvVarList.\r
+**/\r
+EFI_STATUS\r
+ShellRemvoeEnvVarFromList (\r
+  IN CONST CHAR16           *Key\r
+  )\r
+{\r
+  ENV_VAR_LIST      *Node;\r
+\r
+  if (Key == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
+      ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
+      ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
+     ){\r
+    if (Node->Key != NULL && StrCmp(Key, Node->Key) == 0) {\r
+      SHELL_FREE_NON_NULL(Node->Key);\r
+      SHELL_FREE_NON_NULL(Node->Val);\r
+      RemoveEntryList(&Node->Link);\r
+      SHELL_FREE_NON_NULL(Node);\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Initialize the gShellEnvVarList and cache all Shell-Guid-based environment\r
+  variables.\r
+  \r
+**/\r
+EFI_STATUS\r
+ShellInitEnvVarList (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS    Status;\r
+\r
+  InitializeListHead(&gShellEnvVarList.Link);\r
+  Status = GetEnvironmentVariableList (&gShellEnvVarList.Link);\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Destructe the gShellEnvVarList.\r
+\r
+**/\r
+VOID\r
+ShellFreeEnvVarList (\r
+  VOID\r
+  )\r
+{\r
+  FreeEnvironmentVariableList (&gShellEnvVarList.Link);\r
+  InitializeListHead(&gShellEnvVarList.Link);\r
+\r
+  return;\r
+}\r
+\r
index ab3d9166db4cdde8958fa670a0e32ffe887994e9..dd88b29fa040e23083219435bed8d566a66f28b1 100644 (file)
@@ -6,7 +6,7 @@
 //#include <Library/UefiRuntimeServicesTableLib.h>\r
 \r
 \r
 //#include <Library/UefiRuntimeServicesTableLib.h>\r
 \r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2009 - 2016, 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
   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
@@ -27,6 +27,12 @@ typedef struct {
   UINT32      Atts;\r
 } ENV_VAR_LIST;\r
 \r
   UINT32      Atts;\r
 } ENV_VAR_LIST;\r
 \r
+//\r
+// The list is used to cache the environment variables.\r
+//\r
+extern ENV_VAR_LIST    gShellEnvVarList;\r
+\r
+\r
 /**\r
   Reports whether an environment variable is Volatile or Non-Volatile\r
 \r
 /**\r
   Reports whether an environment variable is Volatile or Non-Volatile\r
 \r
@@ -206,5 +212,79 @@ FreeEnvironmentVariableList(
   IN LIST_ENTRY *List\r
   );\r
 \r
   IN LIST_ENTRY *List\r
   );\r
 \r
+/**\r
+  Find an environment variable in the gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+  @param Value      The value of the environment variable, the buffer\r
+                    shoule be freed by the caller.\r
+  @param ValueSize  The size in bytes of the environment variable\r
+                    including the tailing CHAR_NULL.\r
+  @param Atts       The attributes of the variable.\r
+\r
+  @retval EFI_SUCCESS       The command executed successfully.\r
+  @retval EFI_NOT_FOUND     The environment variable is not found in\r
+                            gShellEnvVarList.\r
+\r
+**/\r
+EFI_STATUS\r
+ShellFindEnvVarInList (\r
+  IN  CONST CHAR16    *Key,\r
+  OUT CHAR16          **Value,\r
+  OUT UINTN           *ValueSize,\r
+  OUT UINT32          *Atts OPTIONAL\r
+  );\r
+\r
+/**\r
+  Add an environment variable into gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+  @param Value      The value of environment variable.\r
+  @param ValueSize  The size in bytes of the environment variable\r
+                    including the tailing CHAR_NELL\r
+  @param Atts       The attributes of the variable.\r
+\r
+**/\r
+VOID\r
+ShellAddEnvVarToList (\r
+  IN CONST CHAR16     *Key,\r
+  IN CONST CHAR16     *Value,\r
+  IN UINTN            ValueSize,\r
+  IN UINT32           Atts\r
+  );\r
+\r
+/**\r
+  Remove a specified environment variable in gShellEnvVarList.\r
+\r
+  @param Key        The name of the environment variable.\r
+\r
+  @retval EFI_SUCCESS       The command executed successfully.\r
+  @retval EFI_NOT_FOUND     The environment variable is not found in\r
+                            gShellEnvVarList.\r
+**/\r
+EFI_STATUS\r
+ShellRemvoeEnvVarFromList (\r
+  IN CONST CHAR16           *Key\r
+  );\r
+\r
+/**\r
+  Initialize the gShellEnvVarList and cache all Shell-Guid-based environment \r
+  variables.\r
+  \r
+**/\r
+EFI_STATUS\r
+ShellInitEnvVarList (\r
+  VOID\r
+  );\r
+\r
+/**\r
+  Destructe the gShellEnvVarList.\r
+\r
+**/\r
+VOID\r
+ShellFreeEnvVarList (\r
+  VOID\r
+  );\r
+\r
 #endif //_SHELL_ENVIRONMENT_VARIABLE_HEADER_\r
 \r
 #endif //_SHELL_ENVIRONMENT_VARIABLE_HEADER_\r
 \r
index e55b5e9e0f22a19d92770e92dd9b44b8fe49a5c7..17c30029e4201fea2e1df83072b62c25cb7211fe 100644 (file)
@@ -2691,7 +2691,6 @@ EfiShellGetEnvEx(
   EFI_STATUS  Status;\r
   VOID        *Buffer;\r
   UINTN       Size;\r
   EFI_STATUS  Status;\r
   VOID        *Buffer;\r
   UINTN       Size;\r
-  LIST_ENTRY  List;\r
   ENV_VAR_LIST *Node;\r
   CHAR16      *CurrentWriteLocation;\r
 \r
   ENV_VAR_LIST *Node;\r
   CHAR16      *CurrentWriteLocation;\r
 \r
@@ -2699,21 +2698,13 @@ EfiShellGetEnvEx(
   Buffer = NULL;\r
 \r
   if (Name == NULL) {\r
   Buffer = NULL;\r
 \r
   if (Name == NULL) {\r
-    //\r
-    // Get all our environment variables\r
-    //\r
-    InitializeListHead(&List);\r
-    Status = GetEnvironmentVariableList(&List);\r
-    if (EFI_ERROR(Status)){\r
-      return (NULL);\r
-    }\r
 \r
     //\r
     // Build the semi-colon delimited list. (2 passes)\r
     //\r
 \r
     //\r
     // Build the semi-colon delimited list. (2 passes)\r
     //\r
-    for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)\r
-      ; !IsNull(&List, &Node->Link)\r
-      ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
+    for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
+      ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
+      ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
      ){\r
       ASSERT(Node->Key != NULL);\r
       Size += StrSize(Node->Key);\r
      ){\r
       ASSERT(Node->Key != NULL);\r
       Size += StrSize(Node->Key);\r
@@ -2723,16 +2714,13 @@ EfiShellGetEnvEx(
 \r
     Buffer = AllocateZeroPool(Size);\r
     if (Buffer == NULL) {\r
 \r
     Buffer = AllocateZeroPool(Size);\r
     if (Buffer == NULL) {\r
-      if (!IsListEmpty (&List)) {\r
-        FreeEnvironmentVariableList(&List);\r
-      }\r
       return (NULL);\r
     }\r
     CurrentWriteLocation = (CHAR16*)Buffer;\r
 \r
       return (NULL);\r
     }\r
     CurrentWriteLocation = (CHAR16*)Buffer;\r
 \r
-    for ( Node = (ENV_VAR_LIST*)GetFirstNode(&List)\r
-      ; !IsNull(&List, &Node->Link)\r
-      ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
+    for ( Node = (ENV_VAR_LIST*)GetFirstNode(&gShellEnvVarList.Link)\r
+      ; !IsNull(&gShellEnvVarList.Link, &Node->Link)\r
+      ; Node = (ENV_VAR_LIST*)GetNextNode(&gShellEnvVarList.Link, &Node->Link)\r
      ){\r
       ASSERT(Node->Key != NULL);\r
       StrCpyS( CurrentWriteLocation, \r
      ){\r
       ASSERT(Node->Key != NULL);\r
       StrCpyS( CurrentWriteLocation, \r
@@ -2742,37 +2730,43 @@ EfiShellGetEnvEx(
       CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
     }\r
 \r
       CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
     }\r
 \r
-    //\r
-    // Free the list...\r
-    //\r
-    if (!IsListEmpty (&List)) {\r
-      FreeEnvironmentVariableList(&List);\r
-    }\r
   } else {\r
     //\r
     // We are doing a specific environment variable\r
     //\r
   } else {\r
     //\r
     // We are doing a specific environment variable\r
     //\r
+    Status = ShellFindEnvVarInList(Name, (CHAR16**)&Buffer, &Size, Attributes);\r
 \r
 \r
-    //\r
-    // get the size we need for this EnvVariable\r
-    //\r
-    Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    if (EFI_ERROR(Status)){\r
       //\r
       //\r
-      // Allocate the space and recall the get function\r
+      // get the size we need for this EnvVariable\r
       //\r
       //\r
-      Buffer = AllocateZeroPool(Size);\r
       Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
       Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
-    }\r
-    //\r
-    // we didnt get it (might not exist)\r
-    // free the memory if we allocated any and return NULL\r
-    //\r
-    if (EFI_ERROR(Status)) {\r
-      if (Buffer != NULL) {\r
-        FreePool(Buffer);\r
+      if (Status == EFI_BUFFER_TOO_SMALL) {\r
+        //\r
+        // Allocate the space and recall the get function\r
+        //\r
+        Buffer = AllocateZeroPool(Size);\r
+        Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, Buffer);\r
+      }\r
+      //\r
+      // we didnt get it (might not exist)\r
+      // free the memory if we allocated any and return NULL\r
+      //\r
+      if (EFI_ERROR(Status)) {\r
+        if (Buffer != NULL) {\r
+          FreePool(Buffer);\r
+        }\r
+        return (NULL);\r
+      } else {\r
+        //\r
+        // If we did not find the environment variable in the gShellEnvVarList\r
+        // but get it from UEFI variable storage successfully then we need update\r
+        // the gShellEnvVarList.\r
+        //\r
+        ShellFreeEnvVarList ();\r
+        Status = ShellInitEnvVarList ();\r
+        ASSERT (Status == EFI_SUCCESS);\r
       }\r
       }\r
-      return (NULL);\r
     }\r
   }\r
 \r
     }\r
   }\r
 \r
@@ -2832,14 +2826,35 @@ InternalEfiShellSetEnv(
   IN BOOLEAN Volatile\r
   )\r
 {\r
   IN BOOLEAN Volatile\r
   )\r
 {\r
+  EFI_STATUS      Status;\r
+  UINT32          Atts;\r
+\r
+  Atts = 0x0;\r
+  \r
   if (Value == NULL || StrLen(Value) == 0) {\r
   if (Value == NULL || StrLen(Value) == 0) {\r
-    return (SHELL_DELETE_ENVIRONMENT_VARIABLE(Name));\r
+    Status = SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
+    if (!EFI_ERROR(Status)) {\r
+      ShellRemvoeEnvVarFromList(Name);\r
+    }\r
+    return Status;\r
   } else {\r
     SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
     if (Volatile) {\r
   } else {\r
     SHELL_DELETE_ENVIRONMENT_VARIABLE(Name);\r
     if (Volatile) {\r
-      return (SHELL_SET_ENVIRONMENT_VARIABLE_V(Name, StrSize(Value), Value));\r
+      Status = SHELL_SET_ENVIRONMENT_VARIABLE_V(Name, StrSize(Value), Value);\r
+      if (!EFI_ERROR(Status)) {\r
+        Atts   &= ~EFI_VARIABLE_NON_VOLATILE;\r
+        Atts   |= EFI_VARIABLE_BOOTSERVICE_ACCESS;\r
+        ShellAddEnvVarToList(Name, Value, StrSize(Value), Atts);\r
+      }\r
+      return Status;\r
     } else {\r
     } else {\r
-      return (SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name, StrSize(Value), Value));\r
+      Status = SHELL_SET_ENVIRONMENT_VARIABLE_NV(Name, StrSize(Value), Value);\r
+      if (!EFI_ERROR(Status)) {\r
+        Atts   |= EFI_VARIABLE_NON_VOLATILE;\r
+        Atts   |= EFI_VARIABLE_BOOTSERVICE_ACCESS;\r
+        ShellAddEnvVarToList(Name, Value, StrSize(Value), Atts);\r
+      } \r
+      return Status;\r
     }\r
   }\r
 }\r
     }\r
   }\r
 }\r