]> 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 19e8e2ff9a79a6ee024c8f3814350119ae680d15..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 - 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
@@ -125,12 +126,16 @@ ShellCommandRunCd (
           } else {\r
             CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
             Cwd = AllocateZeroPool(CwdSize);\r
-            ASSERT(Cwd!=NULL);\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
+            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
@@ -153,12 +158,25 @@ ShellCommandRunCd (
           } else {\r
             CwdSize = StrSize(Directory) + sizeof(CHAR16);\r
             Cwd = AllocateZeroPool(CwdSize);\r
-            ASSERT(Cwd!=NULL);\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
-            FreePool(Cwd);\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