From 5439ccda50acf4f6e2c1cbd256a30ba555da3eda Mon Sep 17 00:00:00 2001
From: oliviermartin <oliviermartin@6f19259b-4bc3-4df7-8a09-765794883524>
Date: Thu, 22 Sep 2011 22:52:16 +0000
Subject: [PATCH] EmbeddedPkg/Ebl: Fix EBL copy file command

In the previous version, this command was not working:
cp fs0:\zImage fs1:\

This change uses the source filename is the destination
filename is not specified.



git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@12406 6f19259b-4bc3-4df7-8a09-765794883524
---
 EmbeddedPkg/Ebl/EfiDevice.c | 56 +++++++++++++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 3 deletions(-)

diff --git a/EmbeddedPkg/Ebl/EfiDevice.c b/EmbeddedPkg/Ebl/EfiDevice.c
index dfdbc3fe78..c623bd8b7e 100644
--- a/EmbeddedPkg/Ebl/EfiDevice.c
+++ b/EmbeddedPkg/Ebl/EfiDevice.c
@@ -732,19 +732,64 @@ EblFileCopyCmd (
   VOID          *Buffer      = NULL;
   UINTN         Size;
   UINTN         Offset;
-  UINTN         Chunk = FILE_COPY_CHUNK;
+  UINTN         Chunk        = FILE_COPY_CHUNK;
+  UINTN         FileNameLen;
+  CHAR8*        DestFileName;
+  CHAR8*        SrcFileName;
+  CHAR8*        SrcPtr;
 
   if (Argc < 3) {
     return EFI_INVALID_PARAMETER;
   }
   
+  DestFileName = Argv[2];
+  FileNameLen = AsciiStrLen (DestFileName);
+
+  // Check if the destination file name looks like a directory
+  if ((DestFileName[FileNameLen-1] == '\\') || (DestFileName[FileNameLen-1] == ':')) {
+    // Set the pointer after the source drive (eg: after fs1:)
+    SrcPtr = AsciiStrStr (Argv[1], ":");
+    if (SrcPtr == NULL) {
+      SrcPtr = Argv[1];
+    } else {
+      SrcPtr++;
+      if (*SrcPtr == '\\') {
+        SrcPtr++;
+      }
+    }
+
+    if (*SrcPtr == '\0') {
+      AsciiPrint("Source file incorrect.\n");
+    }
+
+    // Skip the Source Directories
+    while (1) {
+      SrcFileName = SrcPtr;
+      SrcPtr = AsciiStrStr (SrcPtr,"\\");
+      if (SrcPtr != NULL) {
+        SrcPtr++;
+      } else {
+        break;
+      }
+    }
+
+    if (*SrcFileName == '\0') {
+      AsciiPrint("Source file incorrect (Error 2).\n");
+    }
+
+    // Construct the destination filepath
+    DestFileName = (CHAR8*)AllocatePool (FileNameLen + AsciiStrLen (SrcFileName) + 1);
+    AsciiStrCpy (DestFileName, Argv[2]);
+    AsciiStrCat (DestFileName, SrcFileName);
+  }
+
   Source = EfiOpen(Argv[1], EFI_FILE_MODE_READ, 0);
   if (Source == NULL) {
     AsciiPrint("Source file open error.\n");
     return EFI_NOT_FOUND;
   }
   
-  Destination = EfiOpen(Argv[2], EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
+  Destination = EfiOpen(DestFileName, EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE, 0);
   if (Destination == NULL) {
     AsciiPrint("Destination file open error.\n");
     return EFI_NOT_FOUND;
@@ -803,6 +848,11 @@ Exit:
     if (EFI_ERROR(Status)) {
       AsciiPrint("Destination close error %r\n", Status);
     }
+
+    // Case when we have concated the filename to the destination directory
+    if (DestFileName != Argv[2]) {
+      FreePool (DestFileName);
+    }
   }
   
   if (Buffer != NULL) {
@@ -972,7 +1022,7 @@ GLOBAL_REMOVE_IF_UNREFERENCED const EBL_COMMAND_TABLE mCmdDeviceTemplate[] =
   },
   {
     "cp",
-    " file1 file2; copy file",
+    " file1 file2; copy file only.",
     NULL,
     EblFileCopyCmd
   },