]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
ShellPkg: Move UpdateMapping() out of Map command and added to UefiShellCommandLib...
[mirror_edk2.git] / ShellPkg / Library / UefiShellCommandLib / UefiShellCommandLib.c
index 66a242f66ff23192d27361ad758b2af6c686bffa..18ae9f33844c5dcfa81141624f93c85875aae6e1 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Provides interface to shell internal functions for shell commands.\r
 \r
-  Copyright (c) 2009 - 2011, Intel Corporation. All rights reserved.<BR>\r
+  (C) Copyright 2013-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
   which accompanies this distribution.  The full text of the license may be found at\r
 \r
 #include "UefiShellCommandLib.h"\r
 \r
-/// The tag for use in identifying UNICODE files.\r
-/// If the file is UNICODE, the first 16 bits of the file will equal this value.\r
-enum {\r
-  gUnicodeFileTag = 0xFEFF\r
-};\r
-\r
 // STATIC local variables\r
 STATIC SHELL_COMMAND_INTERNAL_LIST_ENTRY  mCommandList;\r
 STATIC SCRIPT_FILE_LIST                   mScriptList;\r
@@ -36,7 +31,6 @@ STATIC BUFFER_LIST                        mFileHandleList;
 \r
 // global variables required by library class.\r
 EFI_UNICODE_COLLATION_PROTOCOL    *gUnicodeCollation            = NULL;\r
-EFI_DEVICE_PATH_TO_TEXT_PROTOCOL  *gDevPathToText               = NULL;\r
 SHELL_MAP_LIST                    gShellMapList;\r
 SHELL_MAP_LIST                    *gShellCurDir                 = NULL;\r
 \r
@@ -64,12 +58,6 @@ CommandInit(
       return (EFI_DEVICE_ERROR);\r
     }\r
   }\r
-  if (gDevPathToText == NULL) {\r
-    Status = gBS->LocateProtocol(&gEfiDevicePathToTextProtocolGuid, NULL, (VOID**)&gDevPathToText);\r
-    if (EFI_ERROR(Status)) {\r
-      return (EFI_DEVICE_ERROR);\r
-    }\r
-  }\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -113,6 +101,37 @@ ShellCommandLibConstructor (
   return (RETURN_SUCCESS);\r
 }\r
 \r
+/**\r
+  Frees list of file handles.\r
+\r
+  @param[in] List     The list to free.\r
+**/\r
+VOID\r
+EFIAPI\r
+FreeFileHandleList (\r
+  IN BUFFER_LIST *List\r
+  )\r
+{\r
+  BUFFER_LIST               *BufferListEntry;\r
+\r
+  if (List == NULL){\r
+    return;\r
+  }\r
+  //\r
+  // enumerate through the buffer list and free all memory\r
+  //\r
+  for ( BufferListEntry = ( BUFFER_LIST *)GetFirstNode(&List->Link)\r
+      ; !IsListEmpty (&List->Link)\r
+      ; BufferListEntry = (BUFFER_LIST *)GetFirstNode(&List->Link)\r
+     ){\r
+    RemoveEntryList(&BufferListEntry->Link);\r
+    ASSERT(BufferListEntry->Buffer != NULL);\r
+    SHELL_FREE_NON_NULL(((SHELL_COMMAND_FILE_HANDLE*)(BufferListEntry->Buffer))->Path);\r
+    SHELL_FREE_NON_NULL(BufferListEntry->Buffer);\r
+    SHELL_FREE_NON_NULL(BufferListEntry);\r
+  }\r
+}\r
+\r
 /**\r
   Destructor for the library.  free any resources.\r
 \r
@@ -182,7 +201,7 @@ ShellCommandLibDestructor (
     }\r
   }\r
   if (!IsListEmpty(&mFileHandleList.Link)){\r
-    FreeBufferList(&mFileHandleList);\r
+    FreeFileHandleList(&mFileHandleList);\r
   }\r
 \r
   if (mProfileList != NULL) {\r
@@ -190,7 +209,6 @@ ShellCommandLibDestructor (
   }\r
 \r
   gUnicodeCollation            = NULL;\r
-  gDevPathToText               = NULL;\r
   gShellCurDir                 = NULL;\r
 \r
   return (RETURN_SUCCESS);\r
@@ -1181,12 +1199,114 @@ ShellCommandCreateInitialMappingsAndPaths(
   }\r
 \r
   return (EFI_SUCCESS);\r
-}\r
-\r
-/**\r
-  Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.\r
-\r
-  @param[in] Handle     The SHELL_FILE_HANDLE to convert.\r
+}
+
+/**
+  Add mappings for any devices without one.  Do not change any existing maps.
+
+  @retval EFI_SUCCESS   The operation was successful.
+**/
+EFI_STATUS
+EFIAPI
+ShellCommandUpdateMapping (
+  VOID
+  )
+{
+  EFI_STATUS                Status;
+  EFI_HANDLE                *HandleList;
+  UINTN                     Count;
+  EFI_DEVICE_PATH_PROTOCOL  **DevicePathList;
+  CHAR16                    *NewDefaultName;
+  CHAR16                    *NewConsistName;
+  EFI_DEVICE_PATH_PROTOCOL  **ConsistMappingTable;
+
+  HandleList  = NULL;
+  Status      = EFI_SUCCESS;
+
+  //
+  // remove mappings that represent removed devices.
+  //
+
+  //
+  // Find each handle with Simple File System
+  //
+  HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);
+  if (HandleList != NULL) {
+    //
+    // Do a count of the handles
+    //
+    for (Count = 0 ; HandleList[Count] != NULL ; Count++);
+
+    //
+    // Get all Device Paths
+    //
+    DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);
+    ASSERT(DevicePathList != NULL);
+
+    for (Count = 0 ; HandleList[Count] != NULL ; Count++) {
+      DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);
+    }
+
+    //
+    // Sort all DevicePaths
+    //
+    PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);
+
+    ShellCommandConsistMappingInitialize(&ConsistMappingTable);
+
+    //
+    // Assign new Mappings to remainders
+    //
+    for (Count = 0 ; HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) {
+      //
+      // Skip ones that already have
+      //
+      if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) {
+        continue;
+      }
+      //
+      // Get default name
+      //
+      NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);
+      ASSERT(NewDefaultName != NULL);
+
+      //
+      // Call shell protocol SetMap function now...
+      //
+      Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName);
+
+      if (!EFI_ERROR(Status)) {
+        //
+        // Now do consistent name
+        //
+        NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);
+        if (NewConsistName != NULL) {
+          Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName);
+          FreePool(NewConsistName);
+        }
+      }
+
+      FreePool(NewDefaultName);
+    }
+    ShellCommandConsistMappingUnInitialize(ConsistMappingTable);
+    SHELL_FREE_NON_NULL(HandleList);
+    SHELL_FREE_NON_NULL(DevicePathList);
+
+    HandleList = NULL;
+  } else {
+    Count = (UINTN)-1;
+  }
+  //
+  // Do it all over again for gEfiBlockIoProtocolGuid
+  //
+
+  return (Status);
+}
+
+/**
+  Converts a SHELL_FILE_HANDLE to an EFI_FILE_PROTOCOL*.
+
+  @param[in] Handle     The SHELL_FILE_HANDLE to convert.
 \r
   @return a EFI_FILE_PROTOCOL* representing the same file.\r
 **/\r