]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
ShellPkg: Update Level2 profile commands response output
[mirror_edk2.git] / ShellPkg / Library / UefiShellLevel2CommandsLib / Cp.c
index e35bf18824a69532035a767474503a4c4830f106..b4e13421255ad6dce3186f5c83f7756d8ba82314 100644 (file)
@@ -1,7 +1,8 @@
 /** @file\r
   Main file for cp shell level 2 function.\r
 \r
-  Copyright (c) 2009 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2015, Hewlett-Packard Development Company, L.P.<BR>\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
 \r
 #include "UefiShellLevel2CommandsLib.h"\r
+#include <Guid/FileSystemInfo.h>\r
+#include <Guid/FileSystemVolumeLabelInfo.h>\r
 \r
-// this is later in the file.\r
+/**\r
+  Function to take a list of files to copy and a destination location and do\r
+  the verification and copying of those files to that location.  This function\r
+  will report any errors to the user and halt.\r
+\r
+  @param[in] FileList           A LIST_ENTRY* based list of files to move.\r
+  @param[in] DestDir            The destination location.\r
+  @param[in] SilentMode         TRUE to eliminate screen output.\r
+  @param[in] RecursiveMode      TRUE to copy directories.\r
+  @param[in] Resp               The response to the overwrite query (if always).\r
+\r
+  @retval SHELL_SUCCESS             the files were all moved.\r
+  @retval SHELL_INVALID_PARAMETER   a parameter was invalid\r
+  @retval SHELL_SECURITY_VIOLATION  a security violation ocurred\r
+  @retval SHELL_WRITE_PROTECTED     the destination was write protected\r
+  @retval SHELL_OUT_OF_RESOURCES    a memory allocation failed\r
+**/\r
 SHELL_STATUS\r
 EFIAPI\r
 ValidateAndCopyFiles(\r
@@ -46,50 +65,43 @@ CopySingleFile(
   IN BOOLEAN      SilentMode\r
   )\r
 {\r
-  VOID                *Response;\r
-  UINTN               ReadSize;\r
-  SHELL_FILE_HANDLE   SourceHandle;\r
-  SHELL_FILE_HANDLE   DestHandle;\r
-  EFI_STATUS          Status;\r
-  VOID                *Buffer;\r
-  CHAR16              *TempName;\r
-  UINTN               Size;\r
-  EFI_SHELL_FILE_INFO *List;\r
-  SHELL_STATUS        ShellStatus;\r
-\r
+  VOID                  *Response;\r
+  UINTN                 ReadSize;\r
+  SHELL_FILE_HANDLE     SourceHandle;\r
+  SHELL_FILE_HANDLE     DestHandle;\r
+  EFI_STATUS            Status;\r
+  VOID                  *Buffer;\r
+  CHAR16                *TempName;\r
+  UINTN                 Size;\r
+  EFI_SHELL_FILE_INFO   *List;\r
+  SHELL_STATUS          ShellStatus;\r
+  UINT64                SourceFileSize;\r
+  UINT64                DestFileSize;\r
+  EFI_FILE_PROTOCOL     *DestVolumeFP;\r
+  EFI_FILE_SYSTEM_INFO  *DestVolumeInfo;\r
+  UINTN                 DestVolumeInfoSize;\r
 \r
   ASSERT(Resp != NULL);\r
 \r
-  SourceHandle  = NULL;\r
-  DestHandle    = NULL;\r
-  Response      = *Resp;\r
-  List          = NULL;\r
+  SourceHandle    = NULL;\r
+  DestHandle      = NULL;\r
+  Response        = *Resp;\r
+  List            = NULL;\r
+  DestVolumeInfo  = NULL;\r
+  ShellStatus     = SHELL_SUCCESS;\r
 \r
-  ReadSize = PcdGet16(PcdShellFileOperationSize);\r
+  ReadSize = PcdGet32(PcdShellFileOperationSize);\r
   // Why bother copying a file to itself\r
   if (StrCmp(Source, Dest) == 0) {\r
     return (SHELL_SUCCESS);\r
   }\r
 \r
-  //\r
-  // Open destination file without create\r
-  //\r
-  Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE, 0);\r
-\r
-  //\r
-  // close file\r
-  //\r
-  if (DestHandle != NULL) {\r
-    ShellCloseFile(&DestHandle);\r
-    DestHandle   = NULL;\r
-  }\r
-\r
   //\r
   // if the destination file existed check response and possibly prompt user\r
   //\r
-  if (!EFI_ERROR(Status)) {\r
+  if (ShellFileExists(Dest) == EFI_SUCCESS) {\r
     if (Response == NULL && !SilentMode) {\r
-      Status = ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_CP_PROMPT), gShellLevel2HiiHandle, &Response);\r
+      Status = ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);\r
     }\r
     //\r
     // possibly return based on response\r
@@ -111,6 +123,8 @@ CopySingleFile(
           *Resp = Response;\r
         case ShellPromptResponseYes:\r
           break;\r
+        default:\r
+          return SHELL_ABORTED;\r
       }\r
     }\r
   }\r
@@ -118,6 +132,7 @@ CopySingleFile(
   if (ShellIsDirectory(Source) == EFI_SUCCESS) {\r
     Status = ShellCreateDirectory(Dest, &DestHandle);\r
     if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_DIR_FAIL), gShellLevel2HiiHandle, L"cp", Dest);  \r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -125,23 +140,28 @@ CopySingleFile(
     // Now copy all the files under the directory...\r
     //\r
     TempName    = NULL;\r
-    Size          = 0;\r
+    Size        = 0;\r
     StrnCatGrow(&TempName, &Size, Source, 0);\r
     StrnCatGrow(&TempName, &Size, L"\\*", 0);\r
-    ShellOpenFileMetaArg((CHAR16*)TempName, EFI_FILE_MODE_READ, &List);\r
-    TempName = NULL;\r
-    StrnCatGrow(&TempName, &Size, Dest, 0);\r
-    StrnCatGrow(&TempName, &Size, L"\\", 0);\r
-    ShellStatus = ValidateAndCopyFiles(List, TempName, SilentMode, TRUE, Resp);\r
-    ShellCloseFileMetaArg(&List);\r
-    FreePool(TempName);\r
-    Size = 0;\r
+    if (TempName != NULL) {\r
+      ShellOpenFileMetaArg((CHAR16*)TempName, EFI_FILE_MODE_READ, &List);\r
+      *TempName = CHAR_NULL;\r
+      StrnCatGrow(&TempName, &Size, Dest, 0);\r
+      StrnCatGrow(&TempName, &Size, L"\\", 0);\r
+      ShellStatus = ValidateAndCopyFiles(List, TempName, SilentMode, TRUE, Resp);\r
+      ShellCloseFileMetaArg(&List);\r
+      SHELL_FREE_NON_NULL(TempName);\r
+      Size = 0;\r
+    }\r
   } else {\r
+    Status = ShellDeleteFileByName(Dest);\r
+\r
     //\r
     // open file with create enabled\r
     //\r
     Status = ShellOpenFileByName(Dest, &DestHandle, EFI_FILE_MODE_READ|EFI_FILE_MODE_WRITE|EFI_FILE_MODE_CREATE, 0);\r
     if (EFI_ERROR(Status)) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_OPEN_FAIL), gShellLevel2HiiHandle, L"cp", Dest);  \r
       return (SHELL_ACCESS_DENIED);\r
     }\r
 \r
@@ -152,17 +172,78 @@ CopySingleFile(
     ASSERT_EFI_ERROR(Status);\r
 \r
     //\r
-    // copy data between files\r
+    //get file size of source file and freespace available on destination volume\r
+    //\r
+    ShellGetFileSize(SourceHandle, &SourceFileSize);\r
+    ShellGetFileSize(DestHandle, &DestFileSize);\r
+\r
+    //\r
+    //if the destination file already exists then it will be replaced, meaning the sourcefile effectively needs less storage space\r
     //\r
-    Buffer = AllocateZeroPool(ReadSize);\r
-    ASSERT(Buffer != NULL);\r
-    while (ReadSize == PcdGet16(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
-      Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);\r
-      ASSERT_EFI_ERROR(Status);\r
-      Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
+    if(DestFileSize < SourceFileSize){\r
+      SourceFileSize -= DestFileSize;\r
+    } else {\r
+      SourceFileSize = 0;\r
     }\r
-  }\r
 \r
+    //\r
+    //get the system volume info to check the free space\r
+    //\r
+    DestVolumeFP = ConvertShellHandleToEfiFileProtocol(DestHandle);\r
+    DestVolumeInfo = NULL;\r
+    DestVolumeInfoSize = 0;\r
+    Status = DestVolumeFP->GetInfo(\r
+      DestVolumeFP,\r
+      &gEfiFileSystemInfoGuid,\r
+      &DestVolumeInfoSize,\r
+      DestVolumeInfo\r
+      );\r
+\r
+    if (Status == EFI_BUFFER_TOO_SMALL) {\r
+      DestVolumeInfo = AllocateZeroPool(DestVolumeInfoSize);\r
+      Status = DestVolumeFP->GetInfo(\r
+        DestVolumeFP,\r
+        &gEfiFileSystemInfoGuid,\r
+        &DestVolumeInfoSize,\r
+        DestVolumeInfo\r
+        );\r
+    }\r
+\r
+    //\r
+    //check if enough space available on destination drive to complete copy\r
+    //\r
+    if (DestVolumeInfo!= NULL && (DestVolumeInfo->FreeSpace < SourceFileSize)) {\r
+      //\r
+      //not enough space on destination directory to copy file\r
+      //\r
+      SHELL_FREE_NON_NULL(DestVolumeInfo);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_FAIL), gShellLevel2HiiHandle, L"cp");  \r
+      return(SHELL_VOLUME_FULL);\r
+    } else {\r
+      //\r
+      // copy data between files\r
+      //\r
+      Buffer = AllocateZeroPool(ReadSize);\r
+      ASSERT(Buffer != NULL);\r
+      while (ReadSize == PcdGet32(PcdShellFileOperationSize) && !EFI_ERROR(Status)) {\r
+        Status = ShellReadFile(SourceHandle, &ReadSize, Buffer);\r
+        if (!EFI_ERROR(Status)) {\r
+          Status = ShellWriteFile(DestHandle, &ReadSize, Buffer);\r
+          if (EFI_ERROR(Status)) {\r
+            ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_WRITE_ERROR), gShellLevel2HiiHandle, L"cp", Dest);   \r
+            break;\r
+          }\r
+        } else {\r
+          ShellStatus = (SHELL_STATUS) (Status & (~MAX_BIT));\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_CPY_READ_ERROR), gShellLevel2HiiHandle, L"cp", Source);  \r
+          break;\r
+        }\r
+      }\r
+    }\r
+    SHELL_FREE_NON_NULL(DestVolumeInfo);\r
+  }\r
+  \r
   //\r
   // close files\r
   //\r
@@ -178,7 +259,7 @@ CopySingleFile(
   //\r
   // return\r
   //\r
-  return (SHELL_SUCCESS);\r
+  return ShellStatus;\r
 }\r
 \r
 /**\r
@@ -189,8 +270,11 @@ CopySingleFile(
   The key is to have this function called ONLY once.  this allows for the parameter\r
   verification to happen correctly.\r
 \r
-  @param[in] FileList           A LIST_ENTRY* based list of files to move\r
-  @param[in] DestDir            the destination location\r
+  @param[in] FileList           A LIST_ENTRY* based list of files to move.\r
+  @param[in] DestDir            The destination location.\r
+  @param[in] SilentMode         TRUE to eliminate screen output.\r
+  @param[in] RecursiveMode      TRUE to copy directories.\r
+  @param[in] Resp               The response to the overwrite query (if always).\r
 \r
   @retval SHELL_SUCCESS             the files were all moved.\r
   @retval SHELL_INVALID_PARAMETER   a parameter was invalid\r
@@ -212,12 +296,13 @@ ValidateAndCopyFiles(
   CHAR16                    *HiiResultOk;\r
   CONST EFI_SHELL_FILE_INFO *Node;\r
   SHELL_STATUS              ShellStatus;\r
+  EFI_STATUS                Status;\r
   CHAR16                    *DestPath;\r
   VOID                      *Response;\r
-  UINTN                     PathLen;\r
+  UINTN                     PathSize;\r
   CONST CHAR16              *Cwd;\r
-  CONST CHAR16              *TempLocation;\r
   UINTN                     NewSize;\r
+  CHAR16                    *CleanFilePathStr;\r
 \r
   if (Resp == NULL) {\r
     Response = NULL;\r
@@ -225,28 +310,42 @@ ValidateAndCopyFiles(
     Response = *Resp;\r
   }\r
 \r
-  DestPath    = NULL;\r
-  ShellStatus = SHELL_SUCCESS;\r
-  PathLen     = 0;\r
-  Cwd         = ShellGetCurrentDir(NULL);\r
+  DestPath         = NULL;\r
+  ShellStatus      = SHELL_SUCCESS;\r
+  PathSize         = 0;\r
+  Cwd              = ShellGetCurrentDir(NULL);\r
+  CleanFilePathStr = NULL;\r
 \r
   ASSERT(FileList != NULL);\r
   ASSERT(DestDir  != NULL);\r
 \r
+  \r
+  Status = ShellLevel2StripQuotes (DestDir, &CleanFilePathStr);\r
+  if (EFI_ERROR (Status)) {\r
+    if (Status == EFI_OUT_OF_RESOURCES) {\r
+      return SHELL_OUT_OF_RESOURCES;\r
+    } else {\r
+      return SHELL_INVALID_PARAMETER;\r
+    }\r
+  }\r
+  \r
+  ASSERT (CleanFilePathStr != NULL);\r
+\r
   //\r
   // If we are trying to copy multiple files... make sure we got a directory for the target...\r
   //\r
-  if (EFI_ERROR(ShellIsDirectory(DestDir)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
+  if (EFI_ERROR(ShellIsDirectory(CleanFilePathStr)) && FileList->Link.ForwardLink != FileList->Link.BackLink) {\r
     //\r
     // Error for destination not a directory\r
     //\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr); \r
+    FreePool (CleanFilePathStr);\r
     return (SHELL_INVALID_PARAMETER);\r
   }\r
   for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
     ;  !IsNull(&FileList->Link, &Node->Link)\r
     ;  Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
-   ){\r
+    ){\r
     //\r
     // skip the directory traversing stuff...\r
     //\r
@@ -254,36 +353,46 @@ ValidateAndCopyFiles(
       continue;\r
     }\r
 \r
-    NewSize =  StrSize(DestDir);\r
-    NewSize += StrSize(Node->FileName);\r
-    NewSize += StrSize(Cwd);\r
-    if (NewSize > PathLen) {\r
-      PathLen = NewSize;\r
+    NewSize =  StrSize(CleanFilePathStr);\r
+    NewSize += StrSize(Node->FullName);\r
+    NewSize += (Cwd == NULL)? 0 : StrSize(Cwd);\r
+    if (NewSize > PathSize) {\r
+      PathSize = NewSize;\r
     }\r
 \r
     //\r
     // Make sure got -r if required\r
     //\r
     if (!RecursiveMode && !EFI_ERROR(ShellIsDirectory(Node->FullName))) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_REQ), gShellLevel2HiiHandle, L"cp");  \r
+      FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
 \r
     //\r
     // make sure got dest as dir if needed\r
     //\r
-    if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(DestDir))) {\r
+    if (!EFI_ERROR(ShellIsDirectory(Node->FullName)) && EFI_ERROR(ShellIsDirectory(CleanFilePathStr))) {\r
       //\r
       // Error for destination not a directory\r
       //\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, DestDir);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NOT_DIR), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+      FreePool (CleanFilePathStr);\r
       return (SHELL_INVALID_PARAMETER);\r
     }\r
   }\r
 \r
   HiiOutput   = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_CP_OUTPUT), NULL);\r
   HiiResultOk = HiiGetString (gShellLevel2HiiHandle, STRING_TOKEN (STR_GEN_RES_OK), NULL);\r
-  DestPath    = AllocatePool(PathLen);\r
+  DestPath    = AllocateZeroPool(PathSize);\r
+\r
+  if (DestPath == NULL || HiiOutput == NULL || HiiResultOk == NULL) {\r
+    SHELL_FREE_NON_NULL(DestPath);\r
+    SHELL_FREE_NON_NULL(HiiOutput);\r
+    SHELL_FREE_NON_NULL(HiiResultOk);\r
+    FreePool (CleanFilePathStr);\r
+    return (SHELL_OUT_OF_RESOURCES);\r
+  }\r
 \r
   //\r
   // Go through the list of files to copy...\r
@@ -291,7 +400,7 @@ ValidateAndCopyFiles(
   for (Node = (EFI_SHELL_FILE_INFO *)GetFirstNode(&FileList->Link)\r
     ;  !IsNull(&FileList->Link, &Node->Link)\r
     ;  Node = (EFI_SHELL_FILE_INFO *)GetNextNode(&FileList->Link, &Node->Link)\r
-   ){\r
+    ){\r
     if (ShellGetExecutionBreakFlag()) {\r
       break;\r
     }\r
@@ -306,54 +415,87 @@ ValidateAndCopyFiles(
     }\r
 \r
     if (FileList->Link.ForwardLink == FileList->Link.BackLink // 1 item\r
-      && EFI_ERROR(ShellIsDirectory(DestDir))                 // not an existing directory\r
-     ) {\r
-      ASSERT(StrStr(DestDir, L":") == NULL);\r
-      //\r
-      // simple copy of a single file\r
-      //\r
-      StrCpy(DestPath, Cwd);\r
-      if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
-        StrCat(DestPath, L"\\");\r
-      } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
-        ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
+      && EFI_ERROR(ShellIsDirectory(CleanFilePathStr))                 // not an existing directory\r
+      ) {\r
+      if (StrStr(CleanFilePathStr, L":") == NULL) {\r
+        //\r
+        // simple copy of a single file\r
+        //\r
+        if (Cwd != NULL) {\r
+          StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16)-1);\r
+        } else {\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+          FreePool (CleanFilePathStr);\r
+          return (SHELL_INVALID_PARAMETER);\r
+        }\r
+        if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
+          StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
+          ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
+        }\r
+        StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+      } else {\r
+        StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
       }\r
-      StrCat(DestPath, DestDir);\r
     } else {\r
       //\r
       // we have multiple files or a directory in the DestDir\r
       //\r
-      if (StrStr(DestDir, L":") == NULL) {\r
-        StrCpy(DestPath, Cwd);\r
-        if (DestPath[StrLen(DestPath)-1] != L'\\' && DestDir[0] != L'\\') {\r
-          StrCat(DestPath, L"\\");\r
-        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && DestDir[0] == L'\\') {\r
+      \r
+      //\r
+      // Check for leading slash\r
+      //\r
+      if (CleanFilePathStr[0] == L'\\') {\r
+         //\r
+         // Copy to the root of CWD\r
+         //\r
+        if (Cwd != NULL) {\r
+          StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
+        } else {\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp",  CleanFilePathStr); \r
+          FreePool(CleanFilePathStr);\r
+          return (SHELL_INVALID_PARAMETER);\r
+        }\r
+        while (PathRemoveLastItem(DestPath));\r
+        StrnCat(DestPath, CleanFilePathStr+1, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+      } else if (StrStr(CleanFilePathStr, L":") == NULL) {\r
+        if (Cwd != NULL) {\r
+          StrnCpy(DestPath, Cwd, PathSize/sizeof(CHAR16) -1);\r
+        } else {\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_DIR_NF), gShellLevel2HiiHandle, L"cp", CleanFilePathStr);  \r
+          FreePool(CleanFilePathStr);\r
+          return (SHELL_INVALID_PARAMETER);\r
+        }\r
+        if (DestPath[StrLen(DestPath)-1] != L'\\' && CleanFilePathStr[0] != L'\\') {\r
+          StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        } else if (DestPath[StrLen(DestPath)-1] == L'\\' && CleanFilePathStr[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, DestDir);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
-          StrCat(DestPath, L"\\");\r
-        } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
+        StrnCat(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
+          StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
           ((CHAR16*)DestPath)[StrLen(DestPath)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, Node->FileName);\r
+        StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
 \r
       } else {\r
-        StrCpy(DestPath, DestDir);\r
-        if (DestDir[StrLen(DestDir)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
-          StrCat(DestPath, L"\\");\r
-        } else if (DestDir[StrLen(DestDir)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
-          ((CHAR16*)DestDir)[StrLen(DestDir)-1] = CHAR_NULL;\r
+        StrnCpy(DestPath, CleanFilePathStr, PathSize/sizeof(CHAR16) -1);\r
+        if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] != L'\\' && Node->FileName[0] != L'\\') {\r
+          StrnCat(DestPath, L"\\", PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
+        } else if (CleanFilePathStr[StrLen(CleanFilePathStr)-1] == L'\\' && Node->FileName[0] == L'\\') {\r
+          ((CHAR16*)CleanFilePathStr)[StrLen(CleanFilePathStr)-1] = CHAR_NULL;\r
         }\r
-        StrCat(DestPath, Node->FileName);\r
+        StrnCat(DestPath, Node->FileName, PathSize/sizeof(CHAR16) - StrLen(DestPath) -1);\r
       }\r
     }\r
-\r
+    \r
     //\r
     // Make sure the path exists\r
     //\r
     if (EFI_ERROR(VerifyIntermediateDirectories(DestPath))) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DIR_WNF), gShellLevel2HiiHandle, L"cp", DestPath);  \r
       ShellStatus = SHELL_DEVICE_ERROR;\r
       break;\r
     }\r
@@ -361,28 +503,30 @@ ValidateAndCopyFiles(
     if ( !EFI_ERROR(ShellIsDirectory(Node->FullName))\r
       && !EFI_ERROR(ShellIsDirectory(DestPath))\r
       && StrniCmp(Node->FullName, DestPath, StrLen(DestPath)) == NULL\r
-     ){\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle);\r
+      ){\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_PARENT), gShellLevel2HiiHandle, L"cp");  \r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
     if (StringNoCaseCompare(&Node->FullName, &DestPath) == 0) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");  \r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
 \r
-    if ((TempLocation = StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName))) == 0\r
+    if ((StrniCmp(Node->FullName, DestPath, StrLen(Node->FullName)) == 0)\r
       && (DestPath[StrLen(Node->FullName)] == CHAR_NULL || DestPath[StrLen(Node->FullName)] == L'\\')\r
-     ) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle);\r
+      ) {\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_SD_SAME), gShellLevel2HiiHandle, L"cp");  \r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
       break;\r
     }\r
 \r
-    CleanPath(DestPath);\r
+    PathCleanUpDirectories(DestPath);\r
 \r
-    ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
+    if (!SilentMode) {\r
+      ShellPrintEx(-1, -1, HiiOutput, Node->FullName, DestPath);\r
+    }\r
 \r
     //\r
     // copy single file...\r
@@ -399,13 +543,27 @@ ValidateAndCopyFiles(
   SHELL_FREE_NON_NULL(DestPath);\r
   SHELL_FREE_NON_NULL(HiiOutput);\r
   SHELL_FREE_NON_NULL(HiiResultOk);\r
-  if (Resp != NULL) {\r
+  SHELL_FREE_NON_NULL(CleanFilePathStr);\r
+  if (Resp == NULL) {\r
     SHELL_FREE_NON_NULL(Response);\r
   }\r
 \r
   return (ShellStatus);\r
+\r
 }\r
 \r
+/**\r
+  Validate and if successful copy all the files from the list into \r
+  destination directory.\r
+\r
+  @param[in] FileList       The list of files to copy.\r
+  @param[in] DestDir        The directory to copy files to.\r
+  @param[in] SilentMode     TRUE to eliminate screen output.\r
+  @param[in] RecursiveMode  TRUE to copy directories.\r
+\r
+  @retval SHELL_INVALID_PARAMETER   A parameter was invalid.\r
+  @retval SHELL_SUCCESS             The operation was successful.\r
+**/\r
 SHELL_STATUS\r
 EFIAPI\r
 ProcessValidateAndCopyFiles(\r
@@ -417,35 +575,38 @@ ProcessValidateAndCopyFiles(
 {\r
   SHELL_STATUS        ShellStatus;\r
   EFI_SHELL_FILE_INFO *List;\r
-  EFI_STATUS          Status;\r
   EFI_FILE_INFO       *FileInfo;\r
+  CHAR16              *FullName;\r
 \r
-  List = NULL;\r
+  List      = NULL;\r
+  FullName  = NULL;\r
+  FileInfo  = NULL;\r
 \r
-  Status = ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
+  ShellOpenFileMetaArg((CHAR16*)DestDir, EFI_FILE_MODE_READ, &List);\r
   if (List != NULL && List->Link.ForwardLink != List->Link.BackLink) {\r
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, DestDir);\r
+    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_MARG_ERROR), gShellLevel2HiiHandle, L"cp", DestDir);  \r
     ShellStatus = SHELL_INVALID_PARAMETER;\r
     ShellCloseFileMetaArg(&List);\r
   } else if (List != NULL) {\r
-    ASSERT(List != NULL);\r
     ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink) != NULL);\r
     ASSERT(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName != NULL);\r
-    FileInfo    = NULL;\r
     FileInfo = gEfiShellProtocol->GetFileInfo(((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->Handle);\r
     ASSERT(FileInfo != NULL);\r
+    StrnCatGrow(&FullName, NULL, ((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName, 0);\r
+    ShellCloseFileMetaArg(&List);\r
     if ((FileInfo->Attribute & EFI_FILE_READ_ONLY) == 0) {\r
-      ShellStatus = ValidateAndCopyFiles(FileList, ((EFI_SHELL_FILE_INFO *)List->Link.ForwardLink)->FullName, SilentMode, RecursiveMode, NULL);\r
+      ShellStatus = ValidateAndCopyFiles(FileList, FullName, SilentMode, RecursiveMode, NULL);\r
     } else {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_CP_DEST_ERROR), gShellLevel2HiiHandle, L"cp");  \r
       ShellStatus = SHELL_ACCESS_DENIED;\r
     }\r
-    SHELL_FREE_NON_NULL(FileInfo);\r
-    ShellCloseFileMetaArg(&List);\r
   } else {\r
-      ShellStatus = ValidateAndCopyFiles(FileList, DestDir, SilentMode, RecursiveMode, NULL);\r
+    ShellCloseFileMetaArg(&List);\r
+    ShellStatus = ValidateAndCopyFiles(FileList, DestDir, SilentMode, RecursiveMode, NULL);\r
   }\r
 \r
+  SHELL_FREE_NON_NULL(FileInfo);\r
+  SHELL_FREE_NON_NULL(FullName);\r
   return (ShellStatus);\r
 }\r
 \r
@@ -499,7 +660,7 @@ ShellCommandRunCp (
   Status = ShellCommandLineParse (ParamList, &Package, &ProblemParam, TRUE);\r
   if (EFI_ERROR(Status)) {\r
     if (Status == EFI_VOLUME_CORRUPTED && ProblemParam != NULL) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, ProblemParam);\r
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellLevel2HiiHandle, L"cp", ProblemParam);  \r
       FreePool(ProblemParam);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
@@ -529,7 +690,7 @@ ShellCommandRunCp (
         //\r
         // we have insufficient parameters\r
         //\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle);\r
+        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellLevel2HiiHandle, L"cp");  \r
         ShellStatus = SHELL_INVALID_PARAMETER;\r
         break;\r
       case 2:\r
@@ -538,12 +699,12 @@ ShellCommandRunCp (
         //\r
         Cwd = ShellGetCurrentDir(NULL);\r
         if (Cwd == NULL){\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle);\r
+          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_NO_CWD), gShellLevel2HiiHandle, L"cp");  \r
           ShellStatus = SHELL_INVALID_PARAMETER;\r
         } else {\r
           Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, 1), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
           if (FileList == NULL || IsListEmpty(&FileList->Link) || EFI_ERROR(Status)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, 1));  \r
             ShellStatus = SHELL_NOT_FOUND;\r
           } else  {\r
             ShellStatus = ProcessValidateAndCopyFiles(FileList, Cwd, SilentMode, RecursiveMode);\r
@@ -561,22 +722,25 @@ ShellCommandRunCp (
           }\r
           Status = ShellOpenFileMetaArg((CHAR16*)ShellCommandLineGetRawValue(Package, LoopCounter), EFI_FILE_MODE_WRITE|EFI_FILE_MODE_READ, &FileList);\r
           if (EFI_ERROR(Status) || FileList == NULL || IsListEmpty(&FileList->Link)) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1));\r
+            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_NF), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, LoopCounter));  \r
             ShellStatus = SHELL_NOT_FOUND;\r
           }\r
         }\r
-        //\r
-        // now copy them all...\r
-        //\r
-        if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
-          ShellStatus = ProcessValidateAndCopyFiles(FileList, ShellCommandCleanPath((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
+        if (ShellStatus != SHELL_SUCCESS) {\r
           Status = ShellCloseFileMetaArg(&FileList);\r
-          if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
-            ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, ShellCommandLineGetRawValue(Package, 1), ShellStatus|MAX_BIT);\r
-            ShellStatus = SHELL_ACCESS_DENIED;\r
+        } else {\r
+          //\r
+          // now copy them all...\r
+          //\r
+          if (FileList != NULL && !IsListEmpty(&FileList->Link)) {\r
+            ShellStatus = ProcessValidateAndCopyFiles(FileList, PathCleanUpDirectories((CHAR16*)ShellCommandLineGetRawValue(Package, ParamCount)), SilentMode, RecursiveMode);\r
+            Status = ShellCloseFileMetaArg(&FileList);\r
+            if (EFI_ERROR(Status) && ShellStatus == SHELL_SUCCESS) {\r
+              ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_FILE), gShellLevel2HiiHandle, L"cp", ShellCommandLineGetRawValue(Package, ParamCount), ShellStatus|MAX_BIT);  \r
+              ShellStatus = SHELL_ACCESS_DENIED;\r
+            }\r
           }\r
         }\r
-\r
         break;\r
     } // switch on parameter count\r
 \r