]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
ShellPkg:?cd \? command fails to go back to the root directory of a file system
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cd.c
index 10bc185dbaf6b5c89cc4a8226d5968960b4dea38..0967bc7c521026317253734bc6f96ac18244b8fe 100644 (file)
@@ -1,8 +1,9 @@
 /** @file\r
   Main file for attrib shell level 2 function.\r
 \r
+  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>\r
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2009 - 2014, 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
@@ -31,8 +32,10 @@ ShellCommandRunCd (
   EFI_STATUS        Status;\r
   LIST_ENTRY        *Package;\r
   CONST CHAR16      *Directory;\r
+  CHAR16            *Cwd;\r
   CHAR16            *Path;\r
   CHAR16            *Drive;\r
+  UINTN             CwdSize;\r
   UINTN             DriveSize;\r
   CHAR16            *ProblemParam;\r
   SHELL_STATUS      ShellStatus;\r
@@ -121,8 +124,18 @@ ShellCommandRunCd (
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd");  \r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else {\r
-            Drive = GetFullyQualifiedPath(Directory);\r
-            PathRemoveLastItem(Drive);\r
+            CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
+            Cwd = AllocateZeroPool(CwdSize);\r
+            if (Cwd == NULL) {\r
+              ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"cd");\r
+              ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+            } else {\r
+              StrCpyS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, Directory);\r
+              StrCatS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, L"\\");\r
+              Drive = GetFullyQualifiedPath (Cwd);\r
+              PathRemoveLastItem (Drive);\r
+              FreePool (Cwd);\r
+            }\r
           }\r
           if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
             //\r
@@ -143,8 +156,27 @@ ShellCommandRunCd (
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cd");  \r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else {\r
-            Drive = GetFullyQualifiedPath(Directory);\r
-            while (PathRemoveLastItem(Drive)) ;\r
+            CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
+            Cwd = AllocateZeroPool(CwdSize);\r
+            if (Cwd == NULL) {\r
+              ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_OUT_MEM), gShellLevel2HiiHandle, L"cd");\r
+              ShellStatus = SHELL_OUT_OF_RESOURCES;\r
+            } else {\r
+              StrCpyS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, Directory);\r
+              StrCatS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, L"\\");\r
+              Drive = GetFullyQualifiedPath (Cwd);\r
+              while (PathRemoveLastItem (Drive)) {\r
+                //\r
+                // Check if Drive contains 'fsx:\' only or still points to a sub-directory.\r
+                // Don't remove trailing '\' from Drive if it points to the root directory.\r
+                //\r
+                Path = StrStr (Drive, L":\\");\r
+                if ((Path != NULL) && (*(Path + 2) == CHAR_NULL)) {\r
+                  break;\r
+                }\r
+              }\r
+              FreePool (Cwd);\r
+            }\r
           }\r
           if (ShellStatus == SHELL_SUCCESS && Drive != NULL) {\r
             //\r
@@ -166,6 +198,7 @@ ShellCommandRunCd (
           } else {\r
             ASSERT((Drive == NULL && DriveSize == 0) || (Drive != NULL));\r
             Drive = StrnCatGrow(&Drive, &DriveSize, ShellGetCurrentDir(NULL), 0);\r
+            Drive = StrnCatGrow(&Drive, &DriveSize, L"\\", 0);\r
             if (*Param1Copy == L'\\') {\r
               while (PathRemoveLastItem(Drive)) ;\r
               Drive = StrnCatGrow(&Drive, &DriveSize, Param1Copy+1, 0);\r