]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellProtocol.c
ShellPkg : Cache the environment variable into memory to enhance
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellProtocol.c
index 116e3f2060358525b5c0f61360891d2567e3a864..17c30029e4201fea2e1df83072b62c25cb7211fe 100644 (file)
@@ -2,7 +2,8 @@
   Member functions of EFI_SHELL_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PROTOCOL.\r
 \r
-  Copyright (c) 2009 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<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
@@ -15,6 +16,8 @@
 \r
 #include "Shell.h"\r
 \r
+#define INIT_NAME_BUFFER_SIZE  128\r
+\r
 /**\r
   Close an open file handle.\r
 \r
@@ -182,6 +185,9 @@ EfiShellSetMap(
        ){\r
           if (StringNoCaseCompare(&MapListNode->MapName, &Mapping) == 0) {\r
             RemoveEntryList(&MapListNode->Link);\r
+            SHELL_FREE_NON_NULL(MapListNode->DevicePath);\r
+            SHELL_FREE_NON_NULL(MapListNode->MapName);\r
+            SHELL_FREE_NON_NULL(MapListNode->CurrentDirectoryPath);\r
             FreePool(MapListNode);\r
             return (EFI_SUCCESS);\r
           }\r
@@ -446,37 +452,37 @@ EfiShellGetFilePathFromDevicePath(
           ; FilePath = (FILEPATH_DEVICE_PATH*)NextDevicePathNode (&FilePath->Header)\r
          ){\r
         //\r
-        // all the rest should be file path nodes\r
+        // If any node is not a file path node, then the conversion can not be completed\r
         //\r
         if ((DevicePathType(&FilePath->Header) != MEDIA_DEVICE_PATH) ||\r
             (DevicePathSubType(&FilePath->Header) != MEDIA_FILEPATH_DP)) {\r
           FreePool(PathForReturn);\r
-          PathForReturn = NULL;\r
-          ASSERT(FALSE);\r
-        } else {\r
-          //\r
-          // append the path part onto the filepath.\r
-          //\r
-          ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));\r
-\r
-          AlignedNode = AllocateCopyPool (DevicePathNodeLength(FilePath), FilePath);\r
-\r
-          // File Path Device Path Nodes 'can optionally add a "\" separator to\r
-          //  the beginning and/or the end of the Path Name string.'\r
-          // (UEFI Spec 2.4 section 9.3.6.4).\r
-          // If necessary, add a "\", but otherwise don't\r
-          // (This is specified in the above section, and also implied by the\r
-          //  UEFI Shell spec section 3.7)\r
-          if ((PathSize != 0)                        &&\r
-              (PathForReturn != NULL)                &&\r
-              (PathForReturn[PathSize - 1] != L'\\') &&\r
-              (AlignedNode->PathName[0]    != L'\\')) {\r
-            PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L"\\", 1);\r
-          }\r
+          return NULL;\r
+        }\r
 \r
-          PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, AlignedNode->PathName, 0);\r
-          FreePool(AlignedNode);\r
+        //\r
+        // append the path part onto the filepath.\r
+        //\r
+        ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));\r
+\r
+        AlignedNode = AllocateCopyPool (DevicePathNodeLength(FilePath), FilePath);\r
+        ASSERT (AlignedNode != NULL);\r
+\r
+        // File Path Device Path Nodes 'can optionally add a "\" separator to\r
+        //  the beginning and/or the end of the Path Name string.'\r
+        // (UEFI Spec 2.4 section 9.3.6.4).\r
+        // If necessary, add a "\", but otherwise don't\r
+        // (This is specified in the above section, and also implied by the\r
+        //  UEFI Shell spec section 3.7)\r
+        if ((PathSize != 0)                        &&\r
+            (PathForReturn != NULL)                &&\r
+            (PathForReturn[PathSize - 1] != L'\\') &&\r
+            (AlignedNode->PathName[0]    != L'\\')) {\r
+          PathForReturn = StrnCatGrow (&PathForReturn, &PathSize, L"\\", 1);\r
         }\r
+\r
+        PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, AlignedNode->PathName, 0);\r
+        FreePool(AlignedNode);\r
       } // for loop of remaining nodes\r
     }\r
     if (PathForReturn != NULL) {\r
@@ -528,18 +534,18 @@ EfiShellGetDevicePathFromFilePath(
     if (Cwd == NULL) {\r
       return (NULL);\r
     }\r
-    Size = StrSize(Cwd);\r
-    Size += StrSize(Path);\r
+    Size = StrSize(Cwd) + StrSize(Path);\r
     NewPath = AllocateZeroPool(Size);\r
     if (NewPath == NULL) {\r
       return (NULL);\r
     }\r
-    StrCpy(NewPath, Cwd);\r
+    StrCpyS(NewPath, Size/sizeof(CHAR16), Cwd);\r
+    StrCatS(NewPath, Size/sizeof(CHAR16), L"\\");\r
     if (*Path == L'\\') {\r
       Path++;\r
       while (PathRemoveLastItem(NewPath)) ;\r
     }\r
-    StrCat(NewPath, Path);\r
+    StrCatS(NewPath, Size/sizeof(CHAR16), Path);\r
     DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);\r
     FreePool(NewPath);\r
     return (DevicePathForReturn);\r
@@ -1144,12 +1150,39 @@ EfiShellCreateFile(
     return (EFI_NOT_FOUND);\r
   }\r
 \r
-  Status = InternalOpenFileDevicePath(DevicePath, FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, FileAttribs); // 0 = no specific file attributes\r
+  Status = InternalOpenFileDevicePath(DevicePath, FileHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, FileAttribs);\r
   FreePool(DevicePath);\r
 \r
   return(Status);\r
 }\r
 \r
+/**\r
+  Register a GUID and a localized human readable name for it.\r
+\r
+  If Guid is not assigned a name, then assign GuidName to Guid.  This list of GUID\r
+  names must be used whenever a shell command outputs GUID information.\r
+\r
+  This function is only available when the major and minor versions in the\r
+  EfiShellProtocol are greater than or equal to 2 and 1, respectively.\r
+\r
+  @param[in] Guid       A pointer to the GUID being registered.\r
+  @param[in] GuidName   A pointer to the localized name for the GUID being registered.\r
+\r
+  @retval EFI_SUCCESS             The operation was successful.\r
+  @retval EFI_INVALID_PARAMETER   Guid was NULL.\r
+  @retval EFI_INVALID_PARAMETER   GuidName was NULL.\r
+  @retval EFI_ACCESS_DENIED       Guid already is assigned a name.\r
+**/\r
+EFI_STATUS\r
+EFIAPI \r
+EfiShellRegisterGuidName(\r
+  IN CONST EFI_GUID *Guid,\r
+  IN CONST CHAR16   *GuidName\r
+  )\r
+{\r
+  return (AddNewGuidNameMapping(Guid, GuidName, NULL));\r
+}\r
+\r
 /**\r
   Opens a file or a directory by file name.\r
 \r
@@ -1317,6 +1350,8 @@ EfiShellDeleteFileByName(
   SHELL_FILE_HANDLE FileHandle;\r
   EFI_STATUS        Status;\r
 \r
+  FileHandle = NULL;\r
+\r
   //\r
   // get a handle to the file\r
   //\r
@@ -1329,6 +1364,7 @@ EfiShellDeleteFileByName(
   //\r
   // now delete the file\r
   //\r
+  ShellFileHandleRemove(FileHandle);\r
   return (ShellInfoObject.NewEfiShellProtocol->DeleteFile(FileHandle));\r
 }\r
 \r
@@ -1359,19 +1395,18 @@ EfiShellEnablePageBreak (
 /**\r
   internal worker function to load and run an image via device path.\r
 \r
-  @param ParentImageHandle  A handle of the image that is executing the specified\r
-                            command line.\r
-  @param DevicePath         device path of the file to execute\r
-  @param CommandLine        Points to the NULL-terminated UCS-2 encoded string\r
-                            containing the command line. If NULL then the command-\r
-                            line will be empty.\r
-  @param Environment        Points to a NULL-terminated array of environment\r
-                            variables with the format 'x=y', where x is the\r
-                            environment variable name and y is the value. If this\r
-                            is NULL, then the current shell environment is used.\r
-\r
-  @param[out] ExitDataSize  ExitDataSize as returned from gBS->StartImage\r
-  @param[out] ExitData      ExitData as returned from gBS->StartImage\r
+  @param ParentImageHandle      A handle of the image that is executing the specified\r
+                                command line.\r
+  @param DevicePath             device path of the file to execute\r
+  @param CommandLine            Points to the NULL-terminated UCS-2 encoded string\r
+                                containing the command line. If NULL then the command-\r
+                                line will be empty.\r
+  @param Environment            Points to a NULL-terminated array of environment\r
+                                variables with the format 'x=y', where x is the\r
+                                environment variable name and y is the value. If this\r
+                                is NULL, then the current shell environment is used.\r
+                            \r
+  @param[out] StartImageStatus  Returned status from gBS->StartImage.\r
 \r
   @retval EFI_SUCCESS       The command executed successfully. The  status code\r
                             returned by the command is pointed to by StatusCode.\r
@@ -1386,28 +1421,20 @@ InternalShellExecuteDevicePath(
   IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
   IN CONST CHAR16                   *CommandLine OPTIONAL,\r
   IN CONST CHAR16                   **Environment OPTIONAL,\r
-  OUT UINTN                         *ExitDataSize OPTIONAL,\r
-  OUT CHAR16                        **ExitData OPTIONAL\r
+  OUT EFI_STATUS                    *StartImageStatus OPTIONAL\r
   )\r
 {\r
   EFI_STATUS                    Status;\r
+  EFI_STATUS                    StartStatus;\r
   EFI_STATUS                    CleanupStatus;\r
   EFI_HANDLE                    NewHandle;\r
   EFI_LOADED_IMAGE_PROTOCOL     *LoadedImage;\r
   LIST_ENTRY                    OrigEnvs;\r
   EFI_SHELL_PARAMETERS_PROTOCOL ShellParamsProtocol;\r
-  UINTN                         InternalExitDataSize;\r
-  UINTN                         *ExitDataSizePtr;\r
   CHAR16                        *ImagePath;\r
   UINTN                         Index;\r
-\r
-  // ExitDataSize is not OPTIONAL for gBS->BootServices, provide somewhere for\r
-  // it to be dumped if the caller doesn't want it.\r
-  if (ExitData == NULL) {\r
-    ExitDataSizePtr = &InternalExitDataSize;\r
-  } else {\r
-    ExitDataSizePtr = ExitDataSize;\r
-  }\r
+  CHAR16                        *Walker;\r
+  CHAR16                        *NewCmdLine;\r
 \r
   if (ParentImageHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -1416,6 +1443,17 @@ InternalShellExecuteDevicePath(
   InitializeListHead(&OrigEnvs);\r
 \r
   NewHandle = NULL;\r
+  \r
+  NewCmdLine = AllocateCopyPool (StrSize (CommandLine), CommandLine);\r
+  if (NewCmdLine == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  for (Walker = NewCmdLine; Walker != NULL && *Walker != CHAR_NULL ; Walker++) {\r
+    if (*Walker == L'^' && *(Walker+1) == L'#') {\r
+      CopyMem(Walker, Walker+1, StrSize(Walker) - sizeof(Walker[0]));\r
+    }\r
+  }\r
 \r
   //\r
   // Load the image with:\r
@@ -1433,6 +1471,7 @@ InternalShellExecuteDevicePath(
     if (NewHandle != NULL) {\r
       gBS->UnloadImage(NewHandle);\r
     }\r
+    FreePool (NewCmdLine);\r
     return (Status);\r
   }\r
   Status = gBS->OpenProtocol(\r
@@ -1445,9 +1484,9 @@ InternalShellExecuteDevicePath(
 \r
   if (!EFI_ERROR(Status)) {\r
     ASSERT(LoadedImage->LoadOptionsSize == 0);\r
-    if (CommandLine != NULL) {\r
-      LoadedImage->LoadOptionsSize  = (UINT32)StrSize(CommandLine);\r
-      LoadedImage->LoadOptions      = (VOID*)CommandLine;\r
+    if (NewCmdLine != NULL) {\r
+      LoadedImage->LoadOptionsSize  = (UINT32)StrSize(NewCmdLine);\r
+      LoadedImage->LoadOptions      = (VOID*)NewCmdLine;\r
     }\r
 \r
     //\r
@@ -1466,7 +1505,7 @@ InternalShellExecuteDevicePath(
     ShellParamsProtocol.StdIn   = ShellInfoObject.NewShellParametersProtocol->StdIn;\r
     ShellParamsProtocol.StdOut  = ShellInfoObject.NewShellParametersProtocol->StdOut;\r
     ShellParamsProtocol.StdErr  = ShellInfoObject.NewShellParametersProtocol->StdErr;\r
-    Status = UpdateArgcArgv(&ShellParamsProtocol, CommandLine, NULL, NULL);\r
+    Status = UpdateArgcArgv(&ShellParamsProtocol, NewCmdLine, Efi_Application, NULL, NULL);\r
     ASSERT_EFI_ERROR(Status);\r
     //\r
     // Replace Argv[0] with the full path of the binary we're executing:\r
@@ -1501,14 +1540,17 @@ InternalShellExecuteDevicePath(
     ///@todo initialize and install ShellInterface protocol on the new image for compatibility if - PcdGetBool(PcdShellSupportOldProtocols)\r
 \r
     //\r
-    // now start the image, passing up exit data if the caller requested it\r
+    // now start the image and if the caller wanted the return code pass it to them...\r
     //\r
     if (!EFI_ERROR(Status)) {\r
-      Status      = gBS->StartImage(\r
+      StartStatus      = gBS->StartImage(\r
                           NewHandle,\r
-                          ExitDataSizePtr,\r
-                          ExitData\r
+                          0,\r
+                          NULL\r
                           );\r
+      if (StartImageStatus != NULL) {\r
+        *StartImageStatus = StartStatus;\r
+      }\r
 \r
       CleanupStatus = gBS->UninstallProtocolInterface(\r
                             NewHandle,\r
@@ -1542,8 +1584,113 @@ FreeAlloc:
     ASSERT_EFI_ERROR (CleanupStatus);\r
   }\r
 \r
+  FreePool (NewCmdLine);\r
+\r
   return(Status);\r
 }\r
+\r
+/**\r
+  internal worker function to load and run an image in the current shell.\r
+\r
+  @param CommandLine            Points to the NULL-terminated UCS-2 encoded string\r
+                                containing the command line. If NULL then the command-\r
+                                line will be empty.\r
+  @param Environment            Points to a NULL-terminated array of environment\r
+                                variables with the format 'x=y', where x is the\r
+                                environment variable name and y is the value. If this\r
+                                is NULL, then the current shell environment is used.\r
+                \r
+  @param[out] StartImageStatus  Returned status from the command line.\r
+\r
+  @retval EFI_SUCCESS       The command executed successfully. The  status code\r
+                            returned by the command is pointed to by StatusCode.\r
+  @retval EFI_INVALID_PARAMETER The parameters are invalid.\r
+  @retval EFI_OUT_OF_RESOURCES Out of resources.\r
+  @retval EFI_UNSUPPORTED   Nested shell invocations are not allowed.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InternalShellExecute(\r
+  IN CONST CHAR16                   *CommandLine OPTIONAL,\r
+  IN CONST CHAR16                   **Environment OPTIONAL,\r
+  OUT EFI_STATUS                    *StartImageStatus OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  EFI_STATUS                    CleanupStatus;\r
+  LIST_ENTRY                    OrigEnvs;\r
+\r
+  InitializeListHead(&OrigEnvs);\r
+\r
+  //\r
+  // Save our current environment settings for later restoration if necessary\r
+  //\r
+  if (Environment != NULL) {\r
+    Status = GetEnvironmentVariableList(&OrigEnvs);\r
+    if (!EFI_ERROR(Status)) {\r
+      Status = SetEnvironmentVariables(Environment);\r
+    } else {\r
+      return Status;\r
+    }\r
+  }\r
+\r
+  Status = RunShellCommand(CommandLine, StartImageStatus);\r
+\r
+  // Restore environment variables\r
+  if (!IsListEmpty(&OrigEnvs)) {\r
+    CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);\r
+    ASSERT_EFI_ERROR (CleanupStatus);\r
+  }\r
+\r
+  return(Status);\r
+}\r
+\r
+/**\r
+  Determine if the UEFI Shell is currently running with nesting enabled or disabled.\r
+\r
+  @retval FALSE   nesting is required\r
+  @retval other   nesting is enabled\r
+**/\r
+STATIC\r
+BOOLEAN\r
+EFIAPI\r
+NestingEnabled(\r
+)\r
+{\r
+  EFI_STATUS  Status;\r
+  CHAR16      *Temp;\r
+  CHAR16      *Temp2;\r
+  UINTN       TempSize;\r
+  BOOLEAN     RetVal;\r
+\r
+  RetVal = TRUE;\r
+  Temp   = NULL;\r
+  Temp2  = NULL;\r
+\r
+  if (ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest) {\r
+    TempSize = 0;\r
+    Temp     = NULL;\r
+    Status = SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName, &TempSize, Temp);\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      Temp = AllocateZeroPool(TempSize + sizeof(CHAR16));\r
+      if (Temp != NULL) {\r
+        Status = SHELL_GET_ENVIRONMENT_VARIABLE(mNoNestingEnvVarName, &TempSize, Temp);\r
+      }\r
+    }\r
+    Temp2 = StrnCatGrow(&Temp2, NULL, mNoNestingTrue, 0);\r
+    if (Temp != NULL && Temp2 != NULL && StringNoCaseCompare(&Temp, &Temp2) == 0) {\r
+      //\r
+      // Use the no nesting method.\r
+      //\r
+      RetVal = FALSE;\r
+    }\r
+  }\r
+\r
+  SHELL_FREE_NON_NULL(Temp);\r
+  SHELL_FREE_NON_NULL(Temp2);\r
+  return (RetVal);\r
+}\r
+\r
 /**\r
   Execute the command line.\r
 \r
@@ -1567,7 +1714,7 @@ FreeAlloc:
                             variables with the format 'x=y', where x is the\r
                             environment variable name and y is the value. If this\r
                             is NULL, then the current shell environment is used.\r
-  @param StatusCode         Points to the status code returned by the command.\r
+  @param StatusCode         Points to the status code returned by the CommandLine.\r
 \r
   @retval EFI_SUCCESS       The command executed successfully. The  status code\r
                             returned by the command is pointed to by StatusCode.\r
@@ -1591,68 +1738,48 @@ EfiShellExecute(
   CHAR16                    *Temp;\r
   EFI_DEVICE_PATH_PROTOCOL  *DevPath;\r
   UINTN                     Size;\r
-  UINTN                     ExitDataSize;\r
-  CHAR16                    *ExitData;\r
 \r
   if ((PcdGet8(PcdShellSupportLevel) < 1)) {\r
     return (EFI_UNSUPPORTED);\r
   }\r
 \r
-  DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
+  if (NestingEnabled()) {\r
+    DevPath = AppendDevicePath (ShellInfoObject.ImageDevPath, ShellInfoObject.FileDevPath);\r
 \r
-  DEBUG_CODE_BEGIN();\r
-  Temp = ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE);\r
-  FreePool(Temp);\r
-  Temp = ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE);\r
-  FreePool(Temp);\r
-  Temp = ConvertDevicePathToText(DevPath, TRUE, TRUE);\r
-  FreePool(Temp);\r
-  DEBUG_CODE_END();\r
+    DEBUG_CODE_BEGIN();\r
+    Temp = ConvertDevicePathToText(ShellInfoObject.FileDevPath, TRUE, TRUE);\r
+    FreePool(Temp);\r
+    Temp = ConvertDevicePathToText(ShellInfoObject.ImageDevPath, TRUE, TRUE);\r
+    FreePool(Temp);\r
+    Temp = ConvertDevicePathToText(DevPath, TRUE, TRUE);\r
+    FreePool(Temp);\r
+    DEBUG_CODE_END();\r
 \r
-  Temp = NULL;\r
-  Size = 0;\r
-  ASSERT((Temp == NULL && Size == 0) || (Temp != NULL));\r
-  StrnCatGrow(&Temp, &Size, L"Shell.efi -_exit ", 0);\r
-  StrnCatGrow(&Temp, &Size, CommandLine, 0);\r
-\r
-  Status = InternalShellExecuteDevicePath(\r
-    ParentImageHandle,\r
-    DevPath,\r
-    Temp,\r
-    (CONST CHAR16**)Environment,\r
-    &ExitDataSize,\r
-    &ExitData);\r
-\r
-    if (Status == EFI_ABORTED) {\r
-      // If the command exited with an error, the shell should put the exit\r
-      // status in ExitData, preceded by a null-terminated string.\r
-      ASSERT (ExitDataSize == StrSize (ExitData) + sizeof (SHELL_STATUS));\r
-\r
-      if (StatusCode != NULL) {\r
-        // Skip the null-terminated string\r
-        ExitData += StrLen (ExitData) + 1;\r
-\r
-        // Use CopyMem to avoid alignment faults\r
-        CopyMem (StatusCode, ExitData, sizeof (SHELL_STATUS));\r
-\r
-        // Convert from SHELL_STATUS to EFI_STATUS\r
-        // EFI_STATUSes have top bit set when they are errors.\r
-        // (See UEFI Spec Appendix D)\r
-        if (*StatusCode != SHELL_SUCCESS) {\r
-          *StatusCode = (EFI_STATUS) *StatusCode | MAX_BIT;\r
-        }\r
-      }\r
-      FreePool (ExitData);\r
-      Status = EFI_SUCCESS;\r
-    } else if ((StatusCode != NULL) && !EFI_ERROR(Status)) {\r
-      *StatusCode = EFI_SUCCESS;\r
-    }\r
+    Temp = NULL;\r
+    Size = 0;\r
+    ASSERT((Temp == NULL && Size == 0) || (Temp != NULL));\r
+    StrnCatGrow(&Temp, &Size, L"Shell.efi -_exit ", 0);\r
+    StrnCatGrow(&Temp, &Size, CommandLine, 0);\r
+\r
+    Status = InternalShellExecuteDevicePath(\r
+      ParentImageHandle,\r
+      DevPath,\r
+      Temp,\r
+      (CONST CHAR16**)Environment,\r
+      StatusCode);\r
+\r
+    //\r
+    // de-allocate and return\r
+    //\r
+    FreePool(DevPath);\r
+    FreePool(Temp);\r
+  } else {\r
+    Status = InternalShellExecute(\r
+      (CONST CHAR16*)CommandLine,\r
+      (CONST CHAR16**)Environment,\r
+      StatusCode);\r
+  }\r
 \r
-  //\r
-  // de-allocate and return\r
-  //\r
-  FreePool(DevPath);\r
-  FreePool(Temp);\r
   return(Status);\r
 }\r
 \r
@@ -1770,6 +1897,19 @@ EfiShellRemoveDupInFileList(
   }\r
   return (EFI_SUCCESS);\r
 }\r
+\r
+//\r
+// This is the same structure as the external version, but it has no CONST qualifiers.\r
+//\r
+typedef struct {\r
+  LIST_ENTRY        Link;       ///< Linked list members.\r
+  EFI_STATUS        Status;     ///< Status of opening the file.  Valid only if Handle != NULL.\r
+        CHAR16      *FullName;  ///< Fully qualified filename.\r
+        CHAR16      *FileName;  ///< name of this file.\r
+  SHELL_FILE_HANDLE Handle;     ///< Handle for interacting with the opened file or NULL if closed.\r
+  EFI_FILE_INFO     *Info;      ///< Pointer to the FileInfo struct for this file or NULL.\r
+} EFI_SHELL_FILE_INFO_NO_CONST;\r
+\r
 /**\r
   Allocates and duplicates a EFI_SHELL_FILE_INFO node.\r
 \r
@@ -1786,20 +1926,28 @@ InternalDuplicateShellFileInfo(
   IN BOOLEAN                   Save\r
   )\r
 {\r
-  EFI_SHELL_FILE_INFO *NewNode;\r
+  EFI_SHELL_FILE_INFO_NO_CONST *NewNode;\r
+\r
+  //\r
+  // try to confirm that the objects are in sync\r
+  //\r
+  ASSERT(sizeof(EFI_SHELL_FILE_INFO_NO_CONST) == sizeof(EFI_SHELL_FILE_INFO));\r
 \r
   NewNode = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
   if (NewNode == NULL) {\r
     return (NULL);\r
   }\r
-  NewNode->FullName = AllocateZeroPool(StrSize(Node->FullName));\r
-\r
-  NewNode->FileName = AllocateZeroPool(StrSize(Node->FileName));\r
-  NewNode->Info     = AllocateZeroPool((UINTN)Node->Info->Size);\r
+  NewNode->FullName = AllocateCopyPool(StrSize(Node->FullName), Node->FullName);\r
+  NewNode->FileName = AllocateCopyPool(StrSize(Node->FileName), Node->FileName);\r
+  NewNode->Info     = AllocateCopyPool((UINTN)Node->Info->Size, Node->Info);\r
   if ( NewNode->FullName == NULL\r
     || NewNode->FileName == NULL\r
     || NewNode->Info == NULL\r
-   ){\r
+  ){\r
+    SHELL_FREE_NON_NULL(NewNode->FullName);\r
+    SHELL_FREE_NON_NULL(NewNode->FileName);\r
+    SHELL_FREE_NON_NULL(NewNode->Info);\r
+    SHELL_FREE_NON_NULL(NewNode);\r
     return(NULL);\r
   }\r
   NewNode->Status = Node->Status;\r
@@ -1807,11 +1955,8 @@ InternalDuplicateShellFileInfo(
   if (!Save) {\r
     Node->Handle = NULL;\r
   }\r
-  StrCpy((CHAR16*)NewNode->FullName, Node->FullName);\r
-  StrCpy((CHAR16*)NewNode->FileName, Node->FileName);\r
-  CopyMem(NewNode->Info, Node->Info, (UINTN)Node->Info->Size);\r
 \r
-  return(NewNode);\r
+  return((EFI_SHELL_FILE_INFO*)NewNode);\r
 }\r
 \r
 /**\r
@@ -1876,7 +2021,7 @@ CreateAndPopulateShellFileInfo(
     TempString = StrnCatGrow(&TempString, &Size, BasePath, 0);\r
     if (TempString == NULL) {\r
       FreePool((VOID*)ShellFileListItem->FileName);\r
-      FreePool(ShellFileListItem->Info);\r
+      SHELL_FREE_NON_NULL(ShellFileListItem->Info);\r
       FreePool(ShellFileListItem);\r
       return (NULL);\r
     }\r
@@ -1892,6 +2037,8 @@ CreateAndPopulateShellFileInfo(
     }\r
   }\r
 \r
+  TempString = PathCleanUpDirectories(TempString);\r
+\r
   ShellFileListItem->FullName = TempString;\r
   ShellFileListItem->Status   = Status;\r
   ShellFileListItem->Handle   = Handle;\r
@@ -1930,6 +2077,7 @@ EfiShellFindFilesInDir(
   UINTN                     Size;\r
   CHAR16                    *TempSpot;\r
 \r
+  BasePath = NULL;\r
   Status = FileHandleGetFileName(FileDirHandle, &BasePath);\r
   if (EFI_ERROR(Status)) {\r
     return (Status);\r
@@ -1996,6 +2144,87 @@ EfiShellFindFilesInDir(
   return(Status);\r
 }\r
 \r
+/**\r
+  Get the GUID value from a human readable name.\r
+\r
+  If GuidName is a known GUID name, then update Guid to have the correct value for\r
+  that GUID.\r
+\r
+  This function is only available when the major and minor versions in the\r
+  EfiShellProtocol are greater than or equal to 2 and 1, respectively.\r
+\r
+  @param[in]  GuidName   A pointer to the localized name for the GUID being queried.\r
+  @param[out] Guid       A pointer to the GUID structure to be filled in.\r
+\r
+  @retval EFI_SUCCESS             The operation was successful.\r
+  @retval EFI_INVALID_PARAMETER   Guid was NULL.\r
+  @retval EFI_INVALID_PARAMETER   GuidName was NULL.\r
+  @retval EFI_NOT_FOUND           GuidName is not a known GUID Name.\r
+**/\r
+EFI_STATUS\r
+EFIAPI \r
+EfiShellGetGuidFromName(\r
+  IN  CONST CHAR16   *GuidName,\r
+  OUT       EFI_GUID *Guid\r
+  )\r
+{\r
+  EFI_GUID    *NewGuid;\r
+  EFI_STATUS  Status;\r
+\r
+  if (Guid == NULL || GuidName == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
\r
+  Status = GetGuidFromStringName(GuidName, NULL, &NewGuid);\r
+\r
+  if (!EFI_ERROR(Status)) {\r
+    CopyGuid(NewGuid, Guid);\r
+  }\r
+\r
+  return (Status);\r
+}\r
+\r
+/**\r
+  Get the human readable name for a GUID from the value.\r
+\r
+  If Guid is assigned a name, then update *GuidName to point to the name. The callee\r
+  should not modify the value.\r
+\r
+  This function is only available when the major and minor versions in the\r
+  EfiShellProtocol are greater than or equal to 2 and 1, respectively.\r
+\r
+  @param[in]  Guid       A pointer to the GUID being queried.\r
+  @param[out] GuidName   A pointer to a pointer the localized to name for the GUID being requested\r
+\r
+  @retval EFI_SUCCESS             The operation was successful.\r
+  @retval EFI_INVALID_PARAMETER   Guid was NULL.\r
+  @retval EFI_INVALID_PARAMETER   GuidName was NULL.\r
+  @retval EFI_NOT_FOUND           Guid is not assigned a name.\r
+**/\r
+EFI_STATUS\r
+EFIAPI \r
+EfiShellGetGuidName(\r
+  IN  CONST EFI_GUID *Guid,\r
+  OUT CONST CHAR16   **GuidName\r
+  )\r
+{\r
+  CHAR16   *Name;\r
+\r
+  if (Guid == NULL || GuidName == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
+  Name = GetStringNameFromGuid(Guid, NULL);\r
+  if (Name == NULL || StrLen(Name) == 0) {\r
+    SHELL_FREE_NON_NULL(Name);\r
+    return (EFI_NOT_FOUND);\r
+  }\r
+\r
+  *GuidName = AddBufferToFreeList(Name);\r
+\r
+  return (EFI_SUCCESS);\r
+}\r
+\r
 /**\r
   Updates a file name to be preceeded by the mapped drive name\r
 \r
@@ -2083,6 +2312,7 @@ ShellSearchHandle(
   EFI_SHELL_FILE_INFO *ShellInfo;\r
   EFI_SHELL_FILE_INFO *ShellInfoNode;\r
   EFI_SHELL_FILE_INFO *NewShellNode;\r
+  EFI_FILE_INFO       *FileInfo;\r
   BOOLEAN             Directory;\r
   CHAR16              *NewFullName;\r
   UINTN               Size;\r
@@ -2106,34 +2336,48 @@ ShellSearchHandle(
 \r
   CurrentFilePattern = AllocateZeroPool((NextFilePatternStart-FilePattern+1)*sizeof(CHAR16));\r
   ASSERT(CurrentFilePattern != NULL);\r
-  StrnCpy(CurrentFilePattern, FilePattern, NextFilePatternStart-FilePattern);\r
+  StrnCpyS(CurrentFilePattern, NextFilePatternStart-FilePattern+1, FilePattern, NextFilePatternStart-FilePattern);\r
 \r
   if (CurrentFilePattern[0]   == CHAR_NULL\r
     &&NextFilePatternStart[0] == CHAR_NULL\r
-   ){\r
+    ){\r
     //\r
-    // Add the current parameter FileHandle to the list, then end...\r
+    // we want the parent or root node (if no parent)\r
     //\r
     if (ParentNode == NULL) {\r
-      Status = EFI_INVALID_PARAMETER;\r
+      //\r
+      // We want the root node.  create the node.\r
+      //\r
+      FileInfo = FileHandleGetInfo(FileHandle);\r
+      NewShellNode = CreateAndPopulateShellFileInfo(\r
+        MapName,\r
+        EFI_SUCCESS,\r
+        L"\\",\r
+        FileHandle,\r
+        FileInfo\r
+        );\r
+      SHELL_FREE_NON_NULL(FileInfo);\r
     } else {\r
+      //\r
+      // Add the current parameter FileHandle to the list, then end...\r
+      //\r
       NewShellNode = InternalDuplicateShellFileInfo((EFI_SHELL_FILE_INFO*)ParentNode, TRUE);\r
-      if (NewShellNode == NULL) {\r
-        Status = EFI_OUT_OF_RESOURCES;\r
-      } else {\r
-        NewShellNode->Handle = NULL;\r
-        if (*FileList == NULL) {\r
-          *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
-          InitializeListHead(&((*FileList)->Link));\r
-        }\r
+    }\r
+    if (NewShellNode == NULL) {\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+    } else {\r
+      NewShellNode->Handle = NULL;\r
+      if (*FileList == NULL) {\r
+        *FileList = AllocateZeroPool(sizeof(EFI_SHELL_FILE_INFO));\r
+        InitializeListHead(&((*FileList)->Link));\r
+      }\r
 \r
-        //\r
-        // Add to the returning to use list\r
-        //\r
-        InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
+      //\r
+      // Add to the returning to use list\r
+      //\r
+      InsertTailList(&(*FileList)->Link, &NewShellNode->Link);\r
 \r
-        Status = EFI_SUCCESS;\r
-      }\r
+      Status = EFI_SUCCESS;\r
     }\r
   } else {\r
     Status = EfiShellFindFilesInDir(FileHandle, &ShellInfo);\r
@@ -2156,8 +2400,8 @@ ShellSearchHandle(
             if (NewFullName == NULL) {\r
               Status = EFI_OUT_OF_RESOURCES;\r
             } else {\r
-              StrCpy(NewFullName, MapName);\r
-              StrCat(NewFullName, ShellInfoNode->FullName+1);\r
+              StrCpyS(NewFullName, Size/sizeof(CHAR16), MapName);\r
+              StrCatS(NewFullName, Size/sizeof(CHAR16), ShellInfoNode->FullName+1);\r
               FreePool((VOID*)ShellInfoNode->FullName);\r
               ShellInfoNode->FullName = NewFullName;\r
             }\r
@@ -2188,6 +2432,8 @@ ShellSearchHandle(
               // recurse with the next part of the pattern\r
               //\r
               Status = ShellSearchHandle(NextFilePatternStart, UnicodeCollation, ShellInfoNode->Handle, FileList, ShellInfoNode, MapName);\r
+              EfiShellClose(ShellInfoNode->Handle);\r
+              ShellInfoNode->Handle = NULL;\r
             }\r
           } else if (!EFI_ERROR(Status)) {\r
             //\r
@@ -2280,11 +2526,10 @@ EfiShellFindFiles(
   RootDevicePath = NULL;\r
   RootFileHandle = NULL;\r
   MapName        = NULL;\r
-  PatternCopy = AllocateZeroPool(StrSize(FilePattern));\r
+  PatternCopy = AllocateCopyPool(StrSize(FilePattern), FilePattern);\r
   if (PatternCopy == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
-  StrCpy(PatternCopy, FilePattern);\r
 \r
   PatternCopy = PathCleanUpDirectories(PatternCopy);\r
 \r
@@ -2307,6 +2552,7 @@ EfiShellFindFiles(
             ; PatternCurrentLocation++);\r
         PatternCurrentLocation++;\r
         Status = ShellSearchHandle(PatternCurrentLocation, gUnicodeCollation, RootFileHandle, FileList, NULL, MapName);\r
+        EfiShellClose(RootFileHandle);\r
       }\r
       FreePool(RootDevicePath);\r
     }\r
@@ -2368,6 +2614,7 @@ EfiShellOpenFileList(
     CurDir = EfiShellGetCurDir(NULL);\r
     ASSERT((Path2 == NULL && Path2Size == 0) || (Path2 != NULL));\r
     StrnCatGrow(&Path2, &Path2Size, CurDir, 0);\r
+    StrnCatGrow(&Path2, &Path2Size, L"\\", 0);\r
     if (*Path == L'\\') {\r
       Path++;\r
       while (PathRemoveLastItem(Path2)) ;\r
@@ -2413,40 +2660,37 @@ EfiShellOpenFileList(
 }\r
 \r
 /**\r
-  This function updated with errata.\r
-\r
-  Gets either a single or list of environment variables.\r
-\r
-  If name is not NULL then this function returns the current value of the specified\r
-  environment variable.\r
-\r
-  If Name is NULL, then a list of all environment variable names is returned.  Each is a\r
-  NULL terminated string with a double NULL terminating the list.\r
-\r
-  @param Name                   A pointer to the environment variable name.  If\r
-                                Name is NULL, then the function will return all\r
-                                of the defined shell environment variables.  In\r
-                                the case where multiple environment variables are\r
-                                being returned, each variable will be terminated by\r
-                                a NULL, and the list will be terminated by a double\r
-                                NULL.\r
-\r
-  @return !=NULL                A pointer to the returned string.\r
-                                The returned pointer does not need to be freed by the caller.\r
-\r
-  @retval NULL                  The environment variable doesn't exist or there are\r
-                                no environment variables.\r
+  Gets the environment variable and Attributes, or list of environment variables.  Can be\r
+  used instead of GetEnv().\r
+\r
+  This function returns the current value of the specified environment variable and\r
+  the Attributes. If no variable name was specified, then all of the known\r
+  variables will be returned.\r
+\r
+  @param[in] Name               A pointer to the environment variable name. If Name is NULL,\r
+                                then the function will return all of the defined shell\r
+                                environment variables. In the case where multiple environment\r
+                                variables are being returned, each variable will be terminated\r
+                                by a NULL, and the list will be terminated by a double NULL.\r
+  @param[out] Attributes        If not NULL, a pointer to the returned attributes bitmask for\r
+                                the environment variable. In the case where Name is NULL, and\r
+                                multiple environment variables are being returned, Attributes\r
+                                is undefined.\r
+\r
+  @retval NULL                  The environment variable doesn't exist.\r
+  @return                       A non-NULL value points to the variable's value. The returned\r
+                                pointer does not need to be freed by the caller.\r
 **/\r
 CONST CHAR16 *\r
-EFIAPI\r
-EfiShellGetEnv(\r
-  IN CONST CHAR16 *Name\r
+EFIAPI \r
+EfiShellGetEnvEx(\r
+  IN  CONST CHAR16 *Name,\r
+  OUT       UINT32 *Attributes OPTIONAL\r
   )\r
 {\r
   EFI_STATUS  Status;\r
   VOID        *Buffer;\r
   UINTN       Size;\r
-  LIST_ENTRY  List;\r
   ENV_VAR_LIST *Node;\r
   CHAR16      *CurrentWriteLocation;\r
 \r
@@ -2454,21 +2698,13 @@ EfiShellGetEnv(
   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
-    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
@@ -2478,54 +2714,59 @@ EfiShellGetEnv(
 \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
-    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
-      StrCpy(CurrentWriteLocation, Node->Key);\r
+      StrCpyS( CurrentWriteLocation, \r
+                (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)), \r
+                Node->Key\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
+    Status = ShellFindEnvVarInList(Name, (CHAR16**)&Buffer, &Size, Attributes);\r
 \r
-    //\r
-    // get the size we need for this EnvVariable\r
-    //\r
-    Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, Buffer);\r
-    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    if (EFI_ERROR(Status)){\r
       //\r
-      // Allocate the space and recall the get function\r
+      // get the size we need for this EnvVariable\r
       //\r
-      Buffer = AllocateZeroPool(Size);\r
-      ASSERT(Buffer != NULL);\r
-      Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &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
+      Status = SHELL_GET_ENVIRONMENT_VARIABLE_AND_ATTRIBUTES(Name, Attributes, &Size, 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
-      return (NULL);\r
     }\r
   }\r
 \r
@@ -2535,6 +2776,38 @@ EfiShellGetEnv(
   return (AddBufferToFreeList(Buffer));\r
 }\r
 \r
+/**\r
+  Gets either a single or list of environment variables.\r
+\r
+  If name is not NULL then this function returns the current value of the specified\r
+  environment variable.\r
+\r
+  If Name is NULL, then a list of all environment variable names is returned.  Each is a\r
+  NULL terminated string with a double NULL terminating the list.\r
+\r
+  @param Name                   A pointer to the environment variable name.  If\r
+                                Name is NULL, then the function will return all\r
+                                of the defined shell environment variables.  In\r
+                                the case where multiple environment variables are\r
+                                being returned, each variable will be terminated by\r
+                                a NULL, and the list will be terminated by a double\r
+                                NULL.\r
+\r
+  @retval !=NULL                A pointer to the returned string.\r
+                                The returned pointer does not need to be freed by the caller.\r
+\r
+  @retval NULL                  The environment variable doesn't exist or there are\r
+                                no environment variables.\r
+**/\r
+CONST CHAR16 *\r
+EFIAPI\r
+EfiShellGetEnv(\r
+  IN CONST CHAR16 *Name\r
+  )\r
+{\r
+  return (EfiShellGetEnvEx(Name, NULL));\r
+}\r
+\r
 /**\r
   Internal variable setting function.  Allows for setting of the read only variables.\r
 \r
@@ -2553,14 +2826,35 @@ InternalEfiShellSetEnv(
   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
-    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
-      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
-      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
@@ -2624,6 +2918,11 @@ EfiShellSetEnv(
         gUnicodeCollation,\r
         (CHAR16*)Name,\r
         L"uefiversion") == 0\r
+    ||(!ShellInfoObject.ShellInitSettings.BitUnion.Bits.NoNest &&\r
+      gUnicodeCollation->StriColl(\r
+        gUnicodeCollation,\r
+        (CHAR16*)Name,\r
+        (CHAR16*)mNoNestingEnvVarName) == 0)\r
        ){\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
@@ -2638,6 +2937,8 @@ EfiShellSetEnv(
   FileSystemMapping. In both cases, the returned name includes the file system\r
   mapping (i.e. fs0:\current-dir).\r
 \r
+  Note that the current directory string should exclude the tailing backslash character.\r
+\r
   @param FileSystemMapping      A pointer to the file system mapping. If NULL,\r
                                 then the current working directory is returned.\r
 \r
@@ -2693,6 +2994,8 @@ EfiShellGetCurDir(
   If the current working directory or the current working file system is changed then the\r
   %cwd% environment variable will be updated\r
 \r
+  Note that the current directory string should exclude the tailing backslash character.\r
+\r
   @param FileSystem             A pointer to the file system's mapped name. If NULL, then the current working\r
                                 directory is changed.\r
   @param Dir                    Points to the NULL-terminated directory on the device specified by FileSystem.\r
@@ -2760,6 +3063,8 @@ EfiShellSetCurDir(
     }\r
 \r
     if (MapListItem == NULL) {\r
+      FreePool (DirectoryName);\r
+      SHELL_FREE_NON_NULL(MapName);\r
       return (EFI_NOT_FOUND);\r
     }\r
 \r
@@ -2776,19 +3081,23 @@ EfiShellSetCurDir(
         ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
         MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName+StrLen(MapName), 0);\r
       }\r
+      FreePool (MapName);\r
     } else {\r
       ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
       MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
     }\r
-    if ((MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') || (MapListItem->CurrentDirectoryPath == NULL)) {\r
+    if ((MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] == L'\\') || (MapListItem->CurrentDirectoryPath == NULL)) {\r
       ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
-      MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
+      if (MapListItem->CurrentDirectoryPath != NULL) {\r
+        MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;\r
+    }\r
     }\r
   } else {\r
     //\r
     // cant have a mapping in the directory...\r
     //\r
     if (StrStr(DirectoryName, L":") != NULL) {\r
+      FreePool (DirectoryName);\r
       return (EFI_INVALID_PARAMETER);\r
     }\r
     //\r
@@ -2796,6 +3105,7 @@ EfiShellSetCurDir(
     //\r
     MapListItem = ShellCommandFindMapItem(FileSystem);\r
     if (MapListItem == NULL) {\r
+      FreePool (DirectoryName);\r
       return (EFI_INVALID_PARAMETER);\r
     }\r
 //    gShellCurDir = MapListItem;\r
@@ -2814,12 +3124,13 @@ EfiShellSetCurDir(
       MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
       ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
       MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, DirectoryName, 0);\r
-      if (MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] != L'\\') {\r
+      if (MapListItem->CurrentDirectoryPath != NULL && MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] == L'\\') {\r
         ASSERT((MapListItem->CurrentDirectoryPath == NULL && Size == 0) || (MapListItem->CurrentDirectoryPath != NULL));\r
-        MapListItem->CurrentDirectoryPath = StrnCatGrow(&MapListItem->CurrentDirectoryPath, &Size, L"\\", 0);\r
+        MapListItem->CurrentDirectoryPath[StrLen(MapListItem->CurrentDirectoryPath)-1] = CHAR_NULL;\r
       }\r
     }\r
   }\r
+  FreePool (DirectoryName);\r
   //\r
   // if updated the current directory then update the environment variable\r
   //\r
@@ -2890,7 +3201,11 @@ EfiShellGetHelpText(
       FixCommand = AllocateZeroPool(StrSize(Command) - 4 * sizeof (CHAR16));\r
       ASSERT(FixCommand != NULL);\r
 \r
-      StrnCpy(FixCommand, Command, StrLen(Command)-4);\r
+      StrnCpyS( FixCommand, \r
+                (StrSize(Command) - 4 * sizeof (CHAR16))/sizeof(CHAR16), \r
+                Command, \r
+                StrLen(Command)-4\r
+                );\r
       Status = ProcessManFile(FixCommand, FixCommand, Sections, NULL, HelpText);\r
       FreePool(FixCommand);\r
       return Status;\r
@@ -2948,20 +3263,17 @@ EFIAPI
 InternalEfiShellGetListAlias(\r
   )\r
 {\r
-  UINT64            MaxStorSize;\r
-  UINT64            RemStorSize;\r
-  UINT64            MaxVarSize;\r
+  \r
   EFI_STATUS        Status;\r
   EFI_GUID          Guid;\r
   CHAR16            *VariableName;\r
   UINTN             NameSize;\r
+  UINTN             NameBufferSize;\r
   CHAR16            *RetVal;\r
   UINTN             RetSize;\r
 \r
-  Status = gRT->QueryVariableInfo(EFI_VARIABLE_NON_VOLATILE|EFI_VARIABLE_BOOTSERVICE_ACCESS, &MaxStorSize, &RemStorSize, &MaxVarSize);\r
-  ASSERT_EFI_ERROR(Status);\r
-\r
-  VariableName  = AllocateZeroPool((UINTN)MaxVarSize);\r
+  NameBufferSize = INIT_NAME_BUFFER_SIZE;\r
+  VariableName  = AllocateZeroPool(NameBufferSize);\r
   RetSize       = 0;\r
   RetVal        = NULL;\r
 \r
@@ -2972,22 +3284,38 @@ InternalEfiShellGetListAlias(
   VariableName[0] = CHAR_NULL;\r
 \r
   while (TRUE) {\r
-    NameSize = (UINTN)MaxVarSize;\r
+    NameSize = NameBufferSize;\r
     Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
     if (Status == EFI_NOT_FOUND){\r
       break;\r
-    }\r
-    ASSERT_EFI_ERROR(Status);\r
-    if (EFI_ERROR(Status)) {\r
+    } else if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      NameBufferSize = NameSize > NameBufferSize * 2 ? NameSize : NameBufferSize * 2;\r
+      SHELL_FREE_NON_NULL(VariableName);\r
+      VariableName = AllocateZeroPool(NameBufferSize);\r
+      if (VariableName == NULL) {\r
+        Status = EFI_OUT_OF_RESOURCES;\r
+        SHELL_FREE_NON_NULL(RetVal);\r
+        RetVal = NULL;\r
+        break;\r
+      }\r
+      \r
+      NameSize = NameBufferSize;\r
+      Status = gRT->GetNextVariableName(&NameSize, VariableName, &Guid);\r
+    }\r
+    \r
+    if (EFI_ERROR (Status)) {\r
+      SHELL_FREE_NON_NULL(RetVal);\r
+      RetVal = NULL;\r
       break;\r
     }\r
+    \r
     if (CompareGuid(&Guid, &gShellAliasGuid)){\r
       ASSERT((RetVal == NULL && RetSize == 0) || (RetVal != NULL));\r
       RetVal = StrnCatGrow(&RetVal, &RetSize, VariableName, 0);\r
       RetVal = StrnCatGrow(&RetVal, &RetSize, L";", 0);\r
     } // compare guid\r
   } // while\r
-  FreePool(VariableName);\r
+  SHELL_FREE_NON_NULL(VariableName);\r
 \r
   return (RetVal);\r
 }\r
@@ -2995,8 +3323,11 @@ InternalEfiShellGetListAlias(
 /**\r
   Convert a null-terminated unicode string, in-place, to all lowercase.\r
   Then return it.\r
+  \r
+  @param  Str    The null-terminated string to be converted to all lowercase.\r
+  \r
+  @return        The null-terminated string converted into all lowercase.  \r
 **/\r
-STATIC\r
 CHAR16 *\r
 ToLower (\r
   CHAR16 *Str\r
@@ -3042,6 +3373,7 @@ EfiShellGetAlias(
   UINT32      Attribs;\r
   EFI_STATUS  Status;\r
   CHAR16      *AliasLower;\r
+  CHAR16      *AliasVal;\r
 \r
   // Convert to lowercase to make aliases case-insensitive\r
   if (Alias != NULL) {\r
@@ -3050,7 +3382,9 @@ EfiShellGetAlias(
     ToLower (AliasLower);\r
 \r
     if (Volatile == NULL) {\r
-      return (AddBufferToFreeList(GetVariable(AliasLower, &gShellAliasGuid)));\r
+      GetVariable2 (AliasLower, &gShellAliasGuid, (VOID **)&AliasVal, NULL);\r
+      FreePool(AliasLower);\r
+      return (AddBufferToFreeList(AliasVal));\r
     }\r
     RetSize = 0;\r
     RetVal = NULL;\r
@@ -3063,6 +3397,7 @@ EfiShellGetAlias(
       if (RetVal != NULL) {\r
         FreePool(RetVal);\r
       }\r
+      FreePool(AliasLower);\r
       return (NULL);\r
     }\r
     if ((EFI_VARIABLE_NON_VOLATILE & Attribs) == EFI_VARIABLE_NON_VOLATILE) {\r
@@ -3232,7 +3567,13 @@ EFI_SHELL_PROTOCOL         mShellProtocol = {
   EfiShellOpenRootByHandle,\r
   NULL,\r
   SHELL_MAJOR_VERSION,\r
-  SHELL_MINOR_VERSION\r
+  SHELL_MINOR_VERSION,\r
+\r
+  // New for UEFI Shell 2.1\r
+  EfiShellRegisterGuidName,\r
+  EfiShellGetGuidName,\r
+  EfiShellGetGuidFromName,\r
+  EfiShellGetEnvEx\r
 };\r
 \r
 /**\r
@@ -3538,3 +3879,4 @@ InernalEfiShellStartMonitor(
   }\r
   return (Status);\r
 }\r
+\r