]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Map.c
ShellPkg: Refactor string manipulation in cp command
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Map.c
index 2b608a02cf620d97a02f72d998456087aae6fd47..5329be41921a59f0f8a894a125a052079070619c 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for map shell level 2 command.\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
@@ -117,108 +118,6 @@ SearchList(
   return (FALSE);\r
 }\r
 \r
-/**\r
-  Add mappings for any devices without one.  Do not change any existing maps.\r
-\r
-  @retval EFI_SUCCESS   The operation was successful.\r
-**/\r
-EFI_STATUS\r
-EFIAPI\r
-UpdateMapping (\r
-  VOID\r
-  )\r
-{\r
-  EFI_STATUS                Status;\r
-  EFI_HANDLE                *HandleList;\r
-  UINTN                     Count;\r
-  EFI_DEVICE_PATH_PROTOCOL  **DevicePathList;\r
-  CHAR16                    *NewDefaultName;\r
-  CHAR16                    *NewConsistName;\r
-  EFI_DEVICE_PATH_PROTOCOL  **ConsistMappingTable;\r
-\r
-  HandleList  = NULL;\r
-  Status      = EFI_SUCCESS;\r
-\r
-  //\r
-  // remove mappings that represent removed devices.\r
-  //\r
-\r
-  //\r
-  // Find each handle with Simple File System\r
-  //\r
-  HandleList = GetHandleListByProtocol(&gEfiSimpleFileSystemProtocolGuid);\r
-  if (HandleList != NULL) {\r
-    //\r
-    // Do a count of the handles\r
-    //\r
-    for (Count = 0 ; HandleList[Count] != NULL ; Count++);\r
-\r
-    //\r
-    // Get all Device Paths\r
-    //\r
-    DevicePathList = AllocateZeroPool(sizeof(EFI_DEVICE_PATH_PROTOCOL*) * Count);\r
-    ASSERT(DevicePathList != NULL);\r
-\r
-    for (Count = 0 ; HandleList[Count] != NULL ; Count++) {\r
-      DevicePathList[Count] = DevicePathFromHandle(HandleList[Count]);\r
-    }\r
-\r
-    //\r
-    // Sort all DevicePaths\r
-    //\r
-    PerformQuickSort(DevicePathList, Count, sizeof(EFI_DEVICE_PATH_PROTOCOL*), DevicePathCompare);\r
-\r
-    ShellCommandConsistMappingInitialize(&ConsistMappingTable);\r
-\r
-    //\r
-    // Assign new Mappings to remainders\r
-    //\r
-    for (Count = 0 ; HandleList[Count] != NULL && !EFI_ERROR(Status); Count++) {\r
-      //\r
-      // Skip ones that already have\r
-      //\r
-      if (gEfiShellProtocol->GetMapFromDevicePath(&DevicePathList[Count]) != NULL) {\r
-        continue;\r
-      }\r
-      //\r
-      // Get default name\r
-      //\r
-      NewDefaultName = ShellCommandCreateNewMappingName(MappingTypeFileSystem);\r
-      ASSERT(NewDefaultName != NULL);\r
-\r
-      //\r
-      // Call shell protocol SetMap function now...\r
-      //\r
-      Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewDefaultName);\r
-\r
-      if (!EFI_ERROR(Status)) {\r
-        //\r
-        // Now do consistent name\r
-        //\r
-        NewConsistName = ShellCommandConsistMappingGenMappingName(DevicePathList[Count], ConsistMappingTable);\r
-        if (NewConsistName != NULL) {\r
-          Status = gEfiShellProtocol->SetMap(DevicePathList[Count], NewConsistName);\r
-          FreePool(NewConsistName);\r
-        }\r
-      }\r
-\r
-      FreePool(NewDefaultName);\r
-    }\r
-    ShellCommandConsistMappingUnInitialize(ConsistMappingTable);\r
-    SHELL_FREE_NON_NULL(HandleList);\r
-    SHELL_FREE_NON_NULL(DevicePathList);\r
-\r
-    HandleList = NULL;\r
-  } else {\r
-    Count = (UINTN)-1;\r
-  }\r
-  //\r
-  // Do it all over again for gEfiBlockIoProtocolGuid\r
-  //\r
-\r
-  return (Status);\r
-}\r
-\r
 /**\r
   Determine what type of device is represented and return it's string.  The \r
   string is in allocated memory and must be callee freed.  The HII is is listed below.\r
@@ -330,6 +229,9 @@ MappingListHasType(
   //\r
   if (Specific != NULL) {\r
     NewSpecific = AllocateZeroPool(StrSize(Specific) + sizeof(CHAR16));\r
+    if (NewSpecific == NULL){\r
+      return FALSE;\r
+    }\r
     StrCpy(NewSpecific, Specific);\r
     if (NewSpecific[StrLen(NewSpecific)-1] != L':') {\r
       StrCat(NewSpecific, L":");\r
@@ -342,14 +244,16 @@ MappingListHasType(
     FreePool(NewSpecific);\r
   }\r
   if (  Consist\r
+    && Specific == NULL\r
     && (SearchList(MapList, L"HD*",  NULL, TRUE, TRUE, L";")\r
       ||SearchList(MapList, L"CD*",  NULL, TRUE, TRUE, L";")\r
-      ||SearchList(MapList, L"AnyF*",   NULL, TRUE, TRUE, L";")\r
+      ||SearchList(MapList, L"F*",   NULL, TRUE, TRUE, L";")\r
       ||SearchList(MapList, L"FP*",  NULL, TRUE, TRUE, L";"))){\r
     return (TRUE);\r
   }\r
 \r
   if (  Normal\r
+    && Specific == NULL\r
     && (SearchList(MapList, L"FS",  NULL, FALSE, TRUE, L";")\r
       ||SearchList(MapList, L"BLK", NULL, FALSE, TRUE, L";"))){\r
     return (TRUE);\r
@@ -394,10 +298,14 @@ PerformSingleMappingDisplay(
   CHAR16                    *MediaType;\r
   CHAR16                    *DevPathString;\r
   CHAR16                    *TempSpot;\r
+  CHAR16                    *Alias;\r
   UINTN                     TempLen;\r
   BOOLEAN                   Removable;\r
   CONST CHAR16              *TempSpot2;\r
 \r
+  Alias       = NULL;\r
+  TempSpot2   = NULL;\r
+  CurrentName = NULL;\r
   DevPath = DevicePathFromHandle(Handle);\r
   DevPathCopy = DevPath;\r
   MapList = gEfiShellProtocol->GetMapFromDevicePath(&DevPathCopy);\r
@@ -409,15 +317,83 @@ PerformSingleMappingDisplay(
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  CurrentName = NULL;\r
-  CurrentName = StrnCatGrow(&CurrentName, 0, MapList, 0);\r
-  TempSpot = StrStr(CurrentName, L";");\r
-  if (TempSpot != NULL) {\r
-    *TempSpot = CHAR_NULL;\r
+  if (Normal || !Consist) {\r
+    //\r
+    // need the Normal here since people can use both on command line.  otherwise unused.\r
+    //\r
+\r
+    //\r
+    // Allocate a name\r
+    //\r
+    CurrentName = NULL;\r
+    CurrentName = StrnCatGrow(&CurrentName, 0, MapList, 0);\r
+    if (CurrentName == NULL) {\r
+      return (EFI_OUT_OF_RESOURCES);\r
+    }\r
+\r
+    //\r
+    // Chop off the other names that become "Alias(s)"\r
+    // leaving just the normal name\r
+    //\r
+    TempSpot = StrStr(CurrentName, L";");\r
+    if (TempSpot != NULL) {\r
+      *TempSpot = CHAR_NULL;\r
+    }\r
+  } else {\r
+    CurrentName = NULL;\r
+\r
+    //\r
+    // Skip the first name.  This is the standard name.\r
+    //\r
+    TempSpot = StrStr(MapList, L";");\r
+    if (TempSpot != NULL) {\r
+      TempSpot++;\r
+    }\r
+    SearchList(TempSpot, L"HD*", &CurrentName, TRUE, FALSE, L";");\r
+    if (CurrentName == NULL) {\r
+      SearchList(TempSpot, L"CD*", &CurrentName, TRUE, FALSE, L";");\r
+    }\r
+    if (CurrentName == NULL) {\r
+      SearchList(TempSpot, L"FP*", &CurrentName, TRUE, FALSE, L";");\r
+    }\r
+    if (CurrentName == NULL) {\r
+      SearchList(TempSpot, L"F*",  &CurrentName, TRUE, FALSE, L";");\r
+    }\r
+    if (CurrentName == NULL) {\r
+      //\r
+      // We didnt find anything, so just the first one in the list...\r
+      //\r
+      CurrentName = StrnCatGrow(&CurrentName, 0, MapList, 0);\r
+      if (CurrentName == NULL) {\r
+        return (EFI_OUT_OF_RESOURCES);\r
+      }\r
+      TempSpot = StrStr(CurrentName, L";");\r
+      if (TempSpot != NULL) {\r
+        *TempSpot = CHAR_NULL;\r
+      }\r
+    } else {\r
+      Alias = StrnCatGrow(&Alias, 0, MapList, 0);\r
+      if (Alias == NULL) {\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
+      TempSpot = StrStr(Alias, CurrentName);\r
+      if (TempSpot != NULL) {\r
+        TempSpot2 = StrStr(TempSpot, L";");\r
+        if (TempSpot2 != NULL) {\r
+          TempSpot2++; // Move past ";" from CurrentName\r
+          CopyMem(TempSpot, TempSpot2, StrSize(TempSpot2));\r
+        } else {\r
+          *TempSpot = CHAR_NULL;\r
+        }\r
+      }\r
+      if (Alias[StrLen(Alias)-1] == L';') {\r
+        Alias[StrLen(Alias)-1] = CHAR_NULL;\r
+      }\r
+    }\r
   }\r
-  DevPathString = gDevPathToText->ConvertDevicePathToText(DevPath, TRUE, FALSE);\r
+  DevPathString = ConvertDevicePathToText(DevPath, TRUE, FALSE);\r
+  TempLen = StrLen(CurrentName);\r
   if (!SFO) {\r
-    TempLen = StrLen(CurrentName);\r
     ShellPrintHiiEx (\r
       -1,\r
       -1,\r
@@ -425,7 +401,7 @@ PerformSingleMappingDisplay(
       STRING_TOKEN (STR_MAP_ENTRY),\r
       gShellLevel2HiiHandle,\r
       CurrentName,\r
-      TempLen < StrLen(MapList)?MapList + TempLen+1:L"",\r
+      Alias!=NULL?Alias:(TempLen < StrLen(MapList)?MapList + TempLen+1:L""),\r
       DevPathString\r
      );\r
     if (Verbose) {\r
@@ -448,10 +424,9 @@ PerformSingleMappingDisplay(
           TempSpot2\r
          );\r
       }\r
-      FreePool(MediaType);\r
+      SHELL_FREE_NON_NULL(MediaType);\r
     }\r
   } else {\r
-    TempLen = StrLen(CurrentName);\r
     ShellPrintHiiEx (\r
       -1,\r
       -1,\r
@@ -460,11 +435,12 @@ PerformSingleMappingDisplay(
       gShellLevel2HiiHandle,\r
       CurrentName,\r
       DevPathString,\r
-      TempLen < StrLen(MapList)?MapList + TempLen+1:L""\r
+      Consist?L"":(TempLen < StrLen(MapList)?MapList + TempLen+1:L"")\r
      );\r
   }\r
-  FreePool(DevPathString);\r
-  FreePool(CurrentName);\r
+  SHELL_FREE_NON_NULL(DevPathString);\r
+  SHELL_FREE_NON_NULL(CurrentName);\r
+  SHELL_FREE_NON_NULL(Alias);\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -513,18 +489,19 @@ CONST CHAR16 AnyF[] = L"F*";
 /**\r
   Function to display mapping information to the user.\r
 \r
-  if Specific is specified then Consist and Normal will be ignored since information will\r
+  If Specific is specified then Consist and Normal will be ignored since information will\r
   be printed for the specific item only.\r
 \r
-  @param[in] Verbose                TRUE to display (extra) verbose information\r
-  @param[in] Consist                TRUE to display consistent mappings\r
-  @param[in] Normal                 TRUE to display normal (not consist) mappings\r
-  @param[in] TypeString             pointer to string of filter types\r
-  @param[in] SFO                    TRUE to display output in Standard Output Format\r
-  @param[in] Specific               pointer to string for specific map to display\r
+  @param[in] Verbose                TRUE to display (extra) verbose information.\r
+  @param[in] Consist                TRUE to display consistent mappings.\r
+  @param[in] Normal                 TRUE to display normal (not consist) mappings.\r
+  @param[in] TypeString             Pointer to string of filter types.\r
+  @param[in] SFO                    TRUE to display output in Standard Output Format.\r
+  @param[in] Specific               Pointer to string for specific map to display.\r
+  @param[in] Header                 TRUE to print the header block.\r
 \r
-  @retval SHELL_SUCCESS               the display was printed\r
-  @retval SHELL_INVALID_PARAMETER     one of Consist or Normal must be TRUE if no Specific\r
+  @retval SHELL_SUCCESS               The display was printed.\r
+  @retval SHELL_INVALID_PARAMETER     One of Consist or Normal must be TRUE if no Specific.\r
 \r
 **/\r
 SHELL_STATUS\r
@@ -609,7 +586,7 @@ PerformMappingDisplay(
   // Get the map name(s) for each one.\r
   //\r
   for ( LoopVar = 0, Found = FALSE\r
-      ; LoopVar < (BufferSize / sizeof(EFI_HANDLE))\r
+      ; LoopVar < (BufferSize / sizeof(EFI_HANDLE)) && HandleBuffer != NULL\r
       ; LoopVar ++\r
      ){\r
     Status = PerformSingleMappingDisplay(\r
@@ -635,7 +612,7 @@ PerformMappingDisplay(
     &BufferSize,\r
     HandleBuffer);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    FreePool(HandleBuffer);\r
+    SHELL_FREE_NON_NULL(HandleBuffer);\r
     HandleBuffer = AllocateZeroPool(BufferSize);\r
     if (HandleBuffer == NULL) {\r
       return (SHELL_OUT_OF_RESOURCES);\r
@@ -647,7 +624,7 @@ PerformMappingDisplay(
       &BufferSize,\r
       HandleBuffer);\r
   }\r
-  if (!EFI_ERROR(Status)) {\r
+  if (!EFI_ERROR(Status) && HandleBuffer != NULL) {\r
     //\r
     // Get the map name(s) for each one.\r
     //\r
@@ -682,7 +659,11 @@ PerformMappingDisplay(
     FreePool(HandleBuffer);\r
   }\r
   if (!Found) {\r
-    ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, Specific);\r
+    if (Specific != NULL) {\r
+      ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, Specific);\r
+    } else {\r
+      ShellPrintHiiEx(gST->ConOut->Mode->CursorColumn, gST->ConOut->Mode->CursorRow-1, NULL, STRING_TOKEN (STR_CD_NF), gShellLevel2HiiHandle);\r
+    }\r
   }\r
   return (SHELL_SUCCESS);\r
 }\r
@@ -897,6 +878,9 @@ AddMappingFromMapping(
   CHAR16                          *NewSName;\r
   \r
   NewSName = AllocateZeroPool(StrSize(SName) + sizeof(CHAR16));\r
+  if (NewSName == NULL) {\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
   StrCpy(NewSName, SName);\r
   if (NewSName[StrLen(NewSName)-1] != L':') {\r
     StrCat(NewSName, L":");\r
@@ -947,6 +931,9 @@ AddMappingFromHandle(
   CHAR16                    *NewSName;\r
   \r
   NewSName = AllocateZeroPool(StrSize(SName) + sizeof(CHAR16));\r
+  if (NewSName == NULL) {\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
   StrCpy(NewSName, SName);\r
   if (NewSName[StrLen(NewSName)-1] != L':') {\r
     StrCat(NewSName, L":");\r
@@ -1008,7 +995,6 @@ ShellCommandRunMap (
   CONST CHAR16  *SName;\r
   CONST CHAR16  *Mapping;\r
   EFI_HANDLE    MapAsHandle;\r
-  CONST EFI_DEVICE_PATH_PROTOCOL *DevPath;\r
   SHELL_STATUS  ShellStatus;\r
   BOOLEAN       SfoMode;\r
   BOOLEAN       ConstMode;\r
@@ -1020,7 +1006,6 @@ ShellCommandRunMap (
   ProblemParam  = NULL;\r
   Mapping       = NULL;\r
   SName         = NULL;\r
-  DevPath       = NULL;\r
   ShellStatus   = SHELL_SUCCESS;\r
   MapAsHandle = NULL;\r
 \r
@@ -1076,18 +1061,15 @@ ShellCommandRunMap (
           if (SName != NULL) {\r
             Status = PerformMappingDelete(SName);\r
             if (EFI_ERROR(Status)) {\r
-              switch (Status) {\r
-                case EFI_ACCESS_DENIED:\r
-                  ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle);\r
-                  ShellStatus = SHELL_ACCESS_DENIED;\r
-                  break;\r
-                case EFI_NOT_FOUND:\r
-                  ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, SName);\r
-                  ShellStatus = SHELL_INVALID_PARAMETER;\r
-                  break;\r
-                default:\r
-                  ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
-                  ShellStatus = SHELL_UNSUPPORTED;\r
+              if (Status == EFI_ACCESS_DENIED) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), gShellLevel2HiiHandle);\r
+                ShellStatus = SHELL_ACCESS_DENIED;\r
+              } else if (Status == EFI_NOT_FOUND) {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MAP_NF), gShellLevel2HiiHandle, SName);\r
+                ShellStatus = SHELL_INVALID_PARAMETER;\r
+              } else {\r
+                ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
+                ShellStatus = SHELL_UNSUPPORTED;\r
               }\r
             }\r
           } else {\r
@@ -1116,7 +1098,7 @@ ShellCommandRunMap (
           //\r
           // Do the Update\r
           //\r
-          Status = UpdateMapping();\r
+          Status = ShellCommandUpdateMapping ();\r
           if (EFI_ERROR(Status)) {\r
             ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), gShellLevel2HiiHandle, Status);\r
             ShellStatus = SHELL_UNSUPPORTED;\r