diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
index 2e51b4cb4d..0967bc7c52 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cd.c
@@ -1,6 +1,7 @@
/** @file
Main file for attrib shell level 2 function.
+ (C) Copyright 2016 Hewlett Packard Enterprise Development LP
(C) Copyright 2015 Hewlett-Packard Development Company, L.P.
Copyright (c) 2009 - 2016, Intel Corporation. All rights reserved.
This program and the accompanying materials
@@ -164,7 +165,16 @@ ShellCommandRunCd (
StrCpyS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, Directory);
StrCatS (Cwd, StrSize (Directory) / sizeof (CHAR16) + 1, L"\\");
Drive = GetFullyQualifiedPath (Cwd);
- while (PathRemoveLastItem (Drive));
+ while (PathRemoveLastItem (Drive)) {
+ //
+ // Check if Drive contains 'fsx:\' only or still points to a sub-directory.
+ // Don't remove trailing '\' from Drive if it points to the root directory.
+ //
+ Path = StrStr (Drive, L":\\");
+ if ((Path != NULL) && (*(Path + 2) == CHAR_NULL)) {
+ break;
+ }
+ }
FreePool (Cwd);
}
}