[edk2] [PATCH 2/3] ShellPkg: Fix bug that fails to change CWD after "map -r".

Ruiyu Ni posted 3 patches 7 years, 3 months ago
[edk2] [PATCH 2/3] ShellPkg: Fix bug that fails to change CWD after "map -r".
Posted by Ruiyu Ni 7 years, 3 months ago
From: Huajing Li <huajing.li@intel.com>

When "map -r" runs, the mapping list is re-created but
gShellCurMapping still points to the old mapping list which
is already destroyed.
The patch updates the gShellCurMapping to point to the correct
location in the new mapping list.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Huajing Li <huajing.li@intel.com>
Reviewed-by: Ruiyu Ni <ruiyu.ni@intel.com>
---
 .../UefiShellCommandLib/UefiShellCommandLib.c      | 36 +++++++++++++++++++++-
 1 file changed, 35 insertions(+), 1 deletion(-)

diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
index b9158d1243..c7984f11b2 100644
--- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
+++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
@@ -1268,7 +1268,14 @@ ShellCommandCreateInitialMappingsAndPaths(
   CHAR16                    *NewConsistName;
   EFI_DEVICE_PATH_PROTOCOL  **ConsistMappingTable;
   SHELL_MAP_LIST            *MapListNode;
-
+  CONST CHAR16              *CurDir;
+  CHAR16                    *SplitCurDir;
+  CHAR16                    *MapName;
+  SHELL_MAP_LIST            *MapListItem;
+
+  SplitCurDir = NULL;
+  MapName     = NULL;
+  MapListItem = NULL;
   HandleList  = NULL;
 
   //
@@ -1354,6 +1361,33 @@ ShellCommandCreateInitialMappingsAndPaths(
     SHELL_FREE_NON_NULL(DevicePathList);
 
     HandleList = NULL;
+
+    //
+    //gShellCurMapping point to node of current file system in the gShellMapList. When reset all mappings,
+    //all nodes in the gShellMapList will be free. Then gShellCurMapping will be a dangling pointer, So,
+    //after created new mappings, we should reset the gShellCurMapping pointer back to node of current file system.
+    //
+    if (gShellCurMapping != NULL) {
+      gShellCurMapping = NULL;
+      CurDir = gEfiShellProtocol->GetEnv(L"cwd");
+      if (CurDir != NULL) {
+        MapName = AllocateCopyPool (StrSize(CurDir), CurDir);
+        if (MapName == NULL) {
+          return EFI_OUT_OF_RESOURCES;
+        }
+        SplitCurDir = StrStr (MapName, L":");
+        if (SplitCurDir == NULL) {
+          SHELL_FREE_NON_NULL (MapName);
+          return EFI_UNSUPPORTED;
+        }
+        *(SplitCurDir + 1) = CHAR_NULL;
+        MapListItem = ShellCommandFindMapItem (MapName);
+        if (MapListItem != NULL) {
+          gShellCurMapping = MapListItem;
+        }
+        SHELL_FREE_NON_NULL (MapName);
+      }
+    }
   } else {
     Count = (UINTN)-1;
   }
-- 
2.12.2.windows.2

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel