]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Application/Shell/ShellProtocol.c
Roll back check in r15180 which caused the shell always returns EFI_ABORTED no matter...
[mirror_edk2.git] / ShellPkg / Application / Shell / ShellProtocol.c
index b67aefb71c95486ac7773e4ea04704056ef4c700..b0303962ff6939cb3771452973b6c537360aba74 100644 (file)
@@ -2,6 +2,7 @@
   Member functions of EFI_SHELL_PROTOCOL and functions for creation,\r
   manipulation, and initialization of EFI_SHELL_PROTOCOL.\r
 \r
+  (C) Copyright 2014, Hewlett-Packard Development Company, L.P.\r
   Copyright (c) 2009 - 2014, 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
@@ -458,9 +459,23 @@ EfiShellGetFilePathFromDevicePath(
           // append the path part onto the filepath.\r
           //\r
           ASSERT((PathForReturn == NULL && PathSize == 0) || (PathForReturn != NULL));\r
-          PathForReturn = StrnCatGrow(&PathForReturn, &PathSize, L"\\", 1);\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
         }\r
@@ -515,18 +530,17 @@ EfiShellGetDevicePathFromFilePath(
     if (Cwd == NULL) {\r
       return (NULL);\r
     }\r
-    Size = StrSize(Cwd);\r
-    Size += StrSize(Path);\r
+    Size = StrSize(Cwd) + StrSize(Path) - sizeof(CHAR16);\r
     NewPath = AllocateZeroPool(Size);\r
     if (NewPath == NULL) {\r
       return (NULL);\r
     }\r
-    StrCpy(NewPath, Cwd);\r
+    StrnCpy(NewPath, Cwd, Size/sizeof(CHAR16)-1);\r
     if (*Path == L'\\') {\r
       Path++;\r
       while (PathRemoveLastItem(NewPath)) ;\r
     }\r
-    StrCat(NewPath, Path);\r
+    StrnCat(NewPath, Path, Size/sizeof(CHAR16) - 1 - StrLen(NewPath));\r
     DevicePathForReturn = EfiShellGetDevicePathFromFilePath(NewPath);\r
     FreePool(NewPath);\r
     return (DevicePathForReturn);\r
@@ -1131,12 +1145,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
@@ -1304,6 +1345,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
@@ -1346,17 +1389,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
-  @param StatusCode         Points to the status code returned by the command.\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
@@ -1371,14 +1415,20 @@ InternalShellExecuteDevicePath(
   IN CONST EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
   IN CONST CHAR16                   *CommandLine OPTIONAL,\r
   IN CONST CHAR16                   **Environment OPTIONAL,\r
-  OUT EFI_STATUS                    *StatusCode 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
+  CHAR16                        *ImagePath;\r
+  UINTN                         Index;\r
+  CHAR16                        *Walker;\r
+  CHAR16                        *NewCmdLine;\r
 \r
   if (ParentImageHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -1387,6 +1437,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
@@ -1416,9 +1477,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
@@ -1437,8 +1498,35 @@ 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, NULL, NULL);\r
     ASSERT_EFI_ERROR(Status);\r
+    //\r
+    // Replace Argv[0] with the full path of the binary we're executing:\r
+    // If the command line was "foo", the binary might be called "foo.efi".\r
+    // "The first entry in [Argv] is always the full file path of the\r
+    //  executable" - UEFI Shell Spec section 2.3\r
+    //\r
+    ImagePath = EfiShellGetFilePathFromDevicePath (DevicePath);\r
+    // The image we're executing isn't necessarily in a filesystem - it might\r
+    // be memory mapped. In this case EfiShellGetFilePathFromDevicePath will\r
+    // return NULL, and we'll leave Argv[0] as UpdateArgcArgv set it.\r
+    if (ImagePath != NULL) {\r
+      if (ShellParamsProtocol.Argv == NULL) {\r
+        // Command line was empty or null.\r
+        // (UpdateArgcArgv sets Argv to NULL when CommandLine is "" or NULL)\r
+        ShellParamsProtocol.Argv = AllocatePool (sizeof (CHAR16 *));\r
+        if (ShellParamsProtocol.Argv == NULL) {\r
+          Status = EFI_OUT_OF_RESOURCES;\r
+          goto UnloadImage;\r
+        }\r
+        ShellParamsProtocol.Argc = 1;\r
+      } else {\r
+        // Free the string UpdateArgcArgv put in Argv[0];\r
+        FreePool (ShellParamsProtocol.Argv[0]);\r
+      }\r
+      ShellParamsProtocol.Argv[0] = ImagePath;\r
+    }\r
+\r
     Status = gBS->InstallProtocolInterface(&NewHandle, &gEfiShellParametersProtocolGuid, EFI_NATIVE_INTERFACE, &ShellParamsProtocol);\r
     ASSERT_EFI_ERROR(Status);\r
 \r
@@ -1448,32 +1536,49 @@ InternalShellExecuteDevicePath(
     // now start the image and if the caller wanted the return code pass it to them...\r
     //\r
     if (!EFI_ERROR(Status)) {\r
-      if (StatusCode != NULL) {\r
-        *StatusCode = gBS->StartImage(NewHandle, NULL, NULL);\r
-      } else {\r
-        Status      = gBS->StartImage(NewHandle, NULL, NULL);\r
+      StartStatus      = gBS->StartImage(\r
+                          NewHandle,\r
+                          0,\r
+                          NULL\r
+                          );\r
+      if (StartImageStatus != NULL) {\r
+        *StartImageStatus = StartStatus;\r
       }\r
+\r
+      CleanupStatus = gBS->UninstallProtocolInterface(\r
+                            NewHandle,\r
+                            &gEfiShellParametersProtocolGuid,\r
+                            &ShellParamsProtocol\r
+                            );\r
+      ASSERT_EFI_ERROR(CleanupStatus);\r
+\r
+      goto FreeAlloc;\r
     }\r
 \r
-    //\r
-    // Cleanup (and dont overwrite errors)\r
-    //\r
-    if (EFI_ERROR(Status)) {\r
-      gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);\r
-    } else {\r
-      Status = gBS->UninstallProtocolInterface(NewHandle, &gEfiShellParametersProtocolGuid, &ShellParamsProtocol);\r
-      ASSERT_EFI_ERROR(Status);\r
+UnloadImage:\r
+    // Unload image - We should only get here if we didn't call StartImage\r
+    gBS->UnloadImage (NewHandle);\r
+\r
+FreeAlloc:\r
+    // Free Argv (Allocated in UpdateArgcArgv)\r
+    if (ShellParamsProtocol.Argv != NULL) {\r
+      for (Index = 0; Index < ShellParamsProtocol.Argc; Index++) {\r
+        if (ShellParamsProtocol.Argv[Index] != NULL) {\r
+          FreePool (ShellParamsProtocol.Argv[Index]);\r
+        }\r
+      }\r
+      FreePool (ShellParamsProtocol.Argv);\r
     }\r
   }\r
 \r
+  // Restore environment variables\r
   if (!IsListEmpty(&OrigEnvs)) {\r
-    if (EFI_ERROR(Status)) {\r
-      SetEnvironmentVariableList(&OrigEnvs);\r
-    } else {\r
-      Status = SetEnvironmentVariableList(&OrigEnvs);\r
-    }\r
+    CleanupStatus = SetEnvironmentVariableList(&OrigEnvs);\r
+    ASSERT_EFI_ERROR (CleanupStatus);\r
   }\r
 \r
+  FreePool (NewCmdLine);\r
+\r
   return(Status);\r
 }\r
 /**\r
@@ -1620,6 +1725,7 @@ EfiShellFreeFileList(
     InternalFreeShellFileInfoNode(ShellFileListItem);\r
   }\r
   InternalFreeShellFileInfoNode(*FileList);\r
+  *FileList = NULL;\r
   return(EFI_SUCCESS);\r
 }\r
 \r
@@ -1641,6 +1747,7 @@ EfiShellRemoveDupInFileList(
 {\r
   EFI_SHELL_FILE_INFO *ShellFileListItem;\r
   EFI_SHELL_FILE_INFO *ShellFileListItem2;\r
+  EFI_SHELL_FILE_INFO *TempNode;\r
 \r
   if (FileList == NULL || *FileList == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -1658,13 +1765,33 @@ EfiShellRemoveDupInFileList(
             (CHAR16*)ShellFileListItem->FullName,\r
             (CHAR16*)ShellFileListItem2->FullName) == 0\r
          ){\r
+        TempNode = (EFI_SHELL_FILE_INFO *)GetPreviousNode(\r
+                                            &(*FileList)->Link,\r
+                                            &ShellFileListItem2->Link\r
+                                            );\r
         RemoveEntryList(&ShellFileListItem2->Link);\r
         InternalFreeShellFileInfoNode(ShellFileListItem2);\r
+        // Set ShellFileListItem2 to PreviousNode so we don't access Freed\r
+        // memory in GetNextNode in the loop expression above.\r
+        ShellFileListItem2 = TempNode;\r
       }\r
     }\r
   }\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
@@ -1681,20 +1808,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
@@ -1702,11 +1837,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
@@ -1771,7 +1903,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
@@ -1787,6 +1919,8 @@ CreateAndPopulateShellFileInfo(
     }\r
   }\r
 \r
+  TempString = PathCleanUpDirectories(TempString);\r
+\r
   ShellFileListItem->FullName = TempString;\r
   ShellFileListItem->Status   = Status;\r
   ShellFileListItem->Handle   = Handle;\r
@@ -1825,6 +1959,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
@@ -1891,6 +2026,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
@@ -1978,6 +2194,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
@@ -2005,30 +2222,44 @@ ShellSearchHandle(
 \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
@@ -2051,8 +2282,8 @@ ShellSearchHandle(
             if (NewFullName == NULL) {\r
               Status = EFI_OUT_OF_RESOURCES;\r
             } else {\r
-              StrCpy(NewFullName, MapName);\r
-              StrCat(NewFullName, ShellInfoNode->FullName+1);\r
+              StrnCpy(NewFullName, MapName, Size/sizeof(CHAR16)-1);\r
+              StrnCat(NewFullName, ShellInfoNode->FullName+1, (Size/sizeof(CHAR16))-StrLen(NewFullName)-1);\r
               FreePool((VOID*)ShellInfoNode->FullName);\r
               ShellInfoNode->FullName = NewFullName;\r
             }\r
@@ -2175,11 +2406,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
@@ -2308,34 +2538,32 @@ 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
@@ -2385,7 +2613,7 @@ EfiShellGetEnv(
       ; Node = (ENV_VAR_LIST*)GetNextNode(&List, &Node->Link)\r
      ){\r
       ASSERT(Node->Key != NULL);\r
-      StrCpy(CurrentWriteLocation, Node->Key);\r
+      StrnCpy(CurrentWriteLocation, Node->Key,  (Size)/sizeof(CHAR16) - (CurrentWriteLocation - ((CHAR16*)Buffer)) - 1);\r
       CurrentWriteLocation += StrLen(CurrentWriteLocation) + 1;\r
     }\r
 \r
@@ -2403,14 +2631,13 @@ EfiShellGetEnv(
     //\r
     // get the size we need for this EnvVariable\r
     //\r
-    Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &Size, 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
-      ASSERT(Buffer != NULL);\r
-      Status = SHELL_GET_ENVIRONMENT_VARIABLE(Name, &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
@@ -2430,6 +2657,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
@@ -2887,6 +3146,29 @@ InternalEfiShellGetListAlias(
   return (RetVal);\r
 }\r
 \r
+/**\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
+CHAR16 *\r
+ToLower (\r
+  CHAR16 *Str\r
+  )\r
+{\r
+  UINTN Index;\r
+\r
+  for (Index = 0; Str[Index] != L'\0'; Index++) {\r
+    if (Str[Index] >= L'A' && Str[Index] <= L'Z') {\r
+      Str[Index] -= (CHAR16)(L'A' - L'a');\r
+    }\r
+  }\r
+  return Str;\r
+}\r
+\r
 /**\r
   This function returns the command associated with a alias or a list of all\r
   alias'.\r
@@ -2916,17 +3198,23 @@ EfiShellGetAlias(
   UINTN       RetSize;\r
   UINT32      Attribs;\r
   EFI_STATUS  Status;\r
+  CHAR16      *AliasLower;\r
 \r
+  // Convert to lowercase to make aliases case-insensitive\r
   if (Alias != NULL) {\r
+    AliasLower = AllocateCopyPool (StrSize (Alias), Alias);\r
+    ASSERT (AliasLower != NULL);\r
+    ToLower (AliasLower);\r
+\r
     if (Volatile == NULL) {\r
-      return (AddBufferToFreeList(GetVariable((CHAR16*)Alias, &gShellAliasGuid)));\r
+      return (AddBufferToFreeList(GetVariable(AliasLower, &gShellAliasGuid)));\r
     }\r
     RetSize = 0;\r
     RetVal = NULL;\r
-    Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
+    Status = gRT->GetVariable(AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
     if (Status == EFI_BUFFER_TOO_SMALL) {\r
       RetVal = AllocateZeroPool(RetSize);\r
-      Status = gRT->GetVariable((CHAR16*)Alias, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
+      Status = gRT->GetVariable(AliasLower, &gShellAliasGuid, &Attribs, &RetSize, RetVal);\r
     }\r
     if (EFI_ERROR(Status)) {\r
       if (RetVal != NULL) {\r
@@ -2940,6 +3228,7 @@ EfiShellGetAlias(
       *Volatile = TRUE;\r
     }\r
 \r
+    FreePool (AliasLower);\r
     return (AddBufferToFreeList(RetVal));\r
   }\r
   return (AddBufferToFreeList(InternalEfiShellGetListAlias()));\r
@@ -2969,6 +3258,18 @@ InternalSetAlias(
   IN BOOLEAN Volatile\r
   )\r
 {\r
+  EFI_STATUS  Status;\r
+  CHAR16      *AliasLower;\r
+\r
+  // Convert to lowercase to make aliases case-insensitive\r
+  if (Alias != NULL) {\r
+    AliasLower = AllocateCopyPool (StrSize (Alias), Alias);\r
+    ASSERT (AliasLower != NULL);\r
+    ToLower (AliasLower);\r
+  } else {\r
+    AliasLower = NULL;\r
+  }\r
+\r
   //\r
   // We must be trying to remove one if Alias is NULL\r
   //\r
@@ -2976,7 +3277,7 @@ InternalSetAlias(
     //\r
     // remove an alias (but passed in COMMAND parameter)\r
     //\r
-    return (gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL));\r
+    Status = (gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL));\r
   } else {\r
     //\r
     // Add and replace are the same\r
@@ -2985,8 +3286,13 @@ InternalSetAlias(
     // We dont check the error return on purpose since the variable may not exist.\r
     gRT->SetVariable((CHAR16*)Command, &gShellAliasGuid, 0, 0, NULL);\r
 \r
-    return (gRT->SetVariable((CHAR16*)Alias, &gShellAliasGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS|(Volatile?0:EFI_VARIABLE_NON_VOLATILE), StrSize(Command), (VOID*)Command));\r
+    Status = (gRT->SetVariable((CHAR16*)Alias, &gShellAliasGuid, EFI_VARIABLE_BOOTSERVICE_ACCESS|(Volatile?0:EFI_VARIABLE_NON_VOLATILE), StrSize(Command), (VOID*)Command));\r
   }\r
+\r
+  if (Alias != NULL) {\r
+    FreePool (AliasLower);\r
+  }\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -3008,6 +3314,7 @@ InternalSetAlias(
   @retval EFI_NOT_FOUND         the Alias intended to be deleted was not found\r
   @retval EFI_ACCESS_DENIED     The alias is a built-in alias or already existed and Replace was set to\r
                                 FALSE.\r
+  @retval EFI_INVALID_PARAMETER Command is null or the empty string.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -3018,21 +3325,24 @@ EfiShellSetAlias(
   IN BOOLEAN Volatile\r
   )\r
 {\r
-  //\r
-  // cant set over a built in alias\r
-  //\r
   if (ShellCommandIsOnAliasList(Alias==NULL?Command:Alias)) {\r
+    //\r
+    // cant set over a built in alias\r
+    //\r
     return (EFI_ACCESS_DENIED);\r
-  }\r
-  if (Command == NULL || *Command == CHAR_NULL || StrLen(Command) == 0) {\r
+  } else if (Command == NULL || *Command == CHAR_NULL || StrLen(Command) == 0) {\r
+    //\r
+    // Command is null or empty\r
+    //\r
     return (EFI_INVALID_PARAMETER);\r
-  }\r
-\r
-  if (EfiShellGetAlias(Command, NULL) != NULL && !Replace) {\r
+  } else if (EfiShellGetAlias(Command, NULL) != NULL && !Replace) {\r
+    //\r
+    // Alias already exists, Replace not set\r
+    //\r
     return (EFI_ACCESS_DENIED);\r
+  } else {\r
+    return (InternalSetAlias(Command, Alias, Volatile));\r
   }\r
-\r
-  return (InternalSetAlias(Command, Alias, Volatile));\r
 }\r
 \r
 // Pure FILE_HANDLE operations are passed to FileHandleLib\r
@@ -3079,7 +3389,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
@@ -3385,3 +3701,4 @@ InernalEfiShellStartMonitor(
   }\r
   return (Status);\r
 }\r
+\r