]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiShellDebug1CommandsLib/Comp.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / ShellPkg / Library / UefiShellDebug1CommandsLib / Comp.c
index 5b91327efcb7d219ceb9b514b565057641439eed..e94236d0b9eff94f08c6a72967257f37ebd30737 100644 (file)
@@ -2,24 +2,18 @@
   Main file for Comp shell Debug1 function.\r
 \r
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>\r
-  Copyright (c) 2010 - 2017, 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
-  http://opensource.org/licenses/bsd-license.php\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include "UefiShellDebug1CommandsLib.h"\r
 \r
-STATIC CONST SHELL_PARAM_ITEM ParamList[] = {\r
-  {L"-n", TypeValue},\r
-  {L"-s", TypeValue},\r
-  {NULL,  TypeMax}\r
-  };\r
+STATIC CONST SHELL_PARAM_ITEM  ParamList[] = {\r
+  { L"-n", TypeValue },\r
+  { L"-s", TypeValue },\r
+  { NULL,  TypeMax   }\r
+};\r
 \r
 typedef enum {\r
   OutOfDiffPoint,\r
@@ -27,10 +21,21 @@ typedef enum {
   InPrevDiffPoint\r
 } READ_STATUS;\r
 \r
+//\r
+// Buffer type, for reading both file operands in chunks.\r
+//\r
+typedef struct {\r
+  UINT8    *Data;     // dynamically allocated buffer\r
+  UINTN    Allocated; // the allocated size of Data\r
+  UINTN    Next;      // next position in Data to fetch a byte at\r
+  UINTN    Left;      // number of bytes left in Data for fetching at Next\r
+} FILE_BUFFER;\r
+\r
 /**\r
   Function to print differnt point data.\r
 \r
-  @param[in]  FileName        File name\r
+  @param[in]  FileName        File name.\r
+  @param[in]  FileTag         File tag name.\r
   @param[in]  Buffer          Data buffer to be printed.\r
   @param[in]  BufferSize      Size of the data to be printed.\r
   @param[in]  Address         Address of the differnt point.\r
@@ -38,26 +43,27 @@ typedef enum {
 \r
 **/\r
 VOID\r
-PrintDifferentPoint(\r
+PrintDifferentPoint (\r
   CONST CHAR16  *FileName,\r
+  CHAR16        *FileTag,\r
   UINT8         *Buffer,\r
-  UINT64        DataSize,\r
+  UINT64        BufferSize,\r
   UINTN         Address,\r
-  UINT64        BufferSize\r
+  UINT64        DifferentBytes\r
   )\r
 {\r
-  UINTN Index;\r
+  UINTN  Index;\r
 \r
-  ShellPrintEx (-1, -1, L"%s: %s\r\n  %08x:", L"File1", FileName, Address);\r
+  ShellPrintEx (-1, -1, L"%s: %s\r\n  %08x:", FileTag, FileName, Address);\r
 \r
   //\r
   // Print data in hex-format.\r
   //\r
-  for (Index = 0; Index < DataSize; Index++) {\r
+  for (Index = 0; Index < BufferSize; Index++) {\r
     ShellPrintEx (-1, -1, L" %02x", Buffer[Index]);\r
   }\r
 \r
-  if (DataSize < BufferSize) {\r
+  if (BufferSize < DifferentBytes) {\r
     ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_END_OF_FILE), gShellDebug1HiiHandle);\r
   }\r
 \r
@@ -66,8 +72,8 @@ PrintDifferentPoint(
   //\r
   // Print data in char-format.\r
   //\r
-  for (Index = 0; Index < DataSize; Index++) {\r
-    if (Buffer[Index] >= 0x20 && Buffer[Index] <= 0x7E) {\r
+  for (Index = 0; Index < BufferSize; Index++) {\r
+    if ((Buffer[Index] >= 0x20) && (Buffer[Index] <= 0x7E)) {\r
       ShellPrintEx (-1, -1, L"%c", Buffer[Index]);\r
     } else {\r
       //\r
@@ -80,6 +86,111 @@ PrintDifferentPoint(
   ShellPrintEx (-1, -1, L"*\r\n");\r
 }\r
 \r
+/**\r
+  Initialize a FILE_BUFFER.\r
+\r
+  @param[out] FileBuffer  The FILE_BUFFER to initialize. On return, the caller\r
+                          is responsible for checking FileBuffer->Data: if\r
+                          FileBuffer->Data is NULL on output, then memory\r
+                          allocation failed.\r
+**/\r
+STATIC\r
+VOID\r
+FileBufferInit (\r
+  OUT FILE_BUFFER  *FileBuffer\r
+  )\r
+{\r
+  FileBuffer->Allocated = PcdGet32 (PcdShellFileOperationSize);\r
+  FileBuffer->Data      = AllocatePool (FileBuffer->Allocated);\r
+  FileBuffer->Left      = 0;\r
+}\r
+\r
+/**\r
+  Uninitialize a FILE_BUFFER.\r
+\r
+  @param[in,out] FileBuffer  The FILE_BUFFER to uninitialize. The caller is\r
+                             responsible for making sure FileBuffer was first\r
+                             initialized with FileBufferInit(), successfully or\r
+                             unsuccessfully.\r
+**/\r
+STATIC\r
+VOID\r
+FileBufferUninit (\r
+  IN OUT FILE_BUFFER  *FileBuffer\r
+  )\r
+{\r
+  SHELL_FREE_NON_NULL (FileBuffer->Data);\r
+}\r
+\r
+/**\r
+  Read a byte from a SHELL_FILE_HANDLE, buffered with a FILE_BUFFER.\r
+\r
+  @param[in] FileHandle      The SHELL_FILE_HANDLE to replenish FileBuffer\r
+                             from, if needed.\r
+\r
+  @param[in,out] FileBuffer  The FILE_BUFFER to read a byte from. If FileBuffer\r
+                             is empty on entry, then FileBuffer is refilled\r
+                             from FileHandle, before outputting a byte from\r
+                             FileBuffer to Byte. The caller is responsible for\r
+                             ensuring that FileBuffer was successfully\r
+                             initialized with FileBufferInit().\r
+\r
+  @param[out] BytesRead      On successful return, BytesRead is set to 1 if the\r
+                             next byte from FileBuffer has been stored to Byte.\r
+                             On successful return, BytesRead is set to 0 if\r
+                             FileBuffer is empty, and FileHandle is at EOF.\r
+                             When an error is returned, BytesRead is not set.\r
+\r
+  @param[out] Byte           On output, the next byte from FileBuffer. Only set\r
+                             if (a) EFI_SUCCESS is returned and (b) BytesRead\r
+                             is set to 1 on output.\r
+\r
+  @retval EFI_SUCCESS  BytesRead has been set to 0 or 1. In the latter case,\r
+                       Byte has been set as well.\r
+\r
+  @return              Error codes propagated from\r
+                       gEfiShellProtocol->ReadFile().\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+FileBufferReadByte (\r
+  IN     SHELL_FILE_HANDLE  FileHandle,\r
+  IN OUT FILE_BUFFER        *FileBuffer,\r
+  OUT UINTN                 *BytesRead,\r
+  OUT UINT8                 *Byte\r
+  )\r
+{\r
+  UINTN       ReadSize;\r
+  EFI_STATUS  Status;\r
+\r
+  if (FileBuffer->Left == 0) {\r
+    ReadSize = FileBuffer->Allocated;\r
+    Status   = gEfiShellProtocol->ReadFile (\r
+                                    FileHandle,\r
+                                    &ReadSize,\r
+                                    FileBuffer->Data\r
+                                    );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    if (ReadSize == 0) {\r
+      *BytesRead = 0;\r
+      return EFI_SUCCESS;\r
+    }\r
+\r
+    FileBuffer->Next = 0;\r
+    FileBuffer->Left = ReadSize;\r
+  }\r
+\r
+  *BytesRead = 1;\r
+  *Byte      = FileBuffer->Data[FileBuffer->Next];\r
+\r
+  FileBuffer->Next++;\r
+  FileBuffer->Left--;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
 /**\r
   Function for 'comp' command.\r
 \r
@@ -93,110 +204,114 @@ ShellCommandRunComp (
   IN EFI_SYSTEM_TABLE  *SystemTable\r
   )\r
 {\r
-  EFI_STATUS          Status;\r
-  LIST_ENTRY          *Package;\r
-  CHAR16              *ProblemParam;\r
-  CHAR16              *FileName1;\r
-  CHAR16              *FileName2;\r
-  CONST CHAR16        *TempParam;\r
-  SHELL_STATUS        ShellStatus;\r
-  SHELL_FILE_HANDLE   FileHandle1;\r
-  SHELL_FILE_HANDLE   FileHandle2;\r
-  UINT64              Size1;\r
-  UINT64              Size2;\r
-  UINT64              DifferentBytes;\r
-  UINT64              DifferentCount;\r
-  UINT8               DiffPointNumber;\r
-  UINT8               OneByteFromFile1;\r
-  UINT8               OneByteFromFile2;\r
-  UINT8               *DataFromFile1;\r
-  UINT8               *DataFromFile2;\r
-  UINTN               InsertPosition1;\r
-  UINTN               InsertPosition2;\r
-  UINTN               DataSizeFromFile1;\r
-  UINTN               DataSizeFromFile2;\r
-  UINTN               TempAddress;\r
-  UINTN               Index;\r
-  UINTN               DiffPointAddress;\r
-  READ_STATUS         ReadStatus;\r
-\r
-  ShellStatus         = SHELL_SUCCESS;\r
-  Status              = EFI_SUCCESS;\r
-  FileName1           = NULL;\r
-  FileName2           = NULL;\r
-  FileHandle1         = NULL;\r
-  FileHandle2         = NULL;\r
-  DataFromFile1       = NULL;\r
-  DataFromFile2       = NULL;\r
-  ReadStatus          = OutOfDiffPoint;\r
-  DifferentCount      = 10;\r
-  DifferentBytes      = 4;\r
-  DiffPointNumber     = 0;\r
-  InsertPosition1     = 0;\r
-  InsertPosition2     = 0;\r
-  TempAddress         = 0;\r
-  DiffPointAddress    = 0;\r
+  EFI_STATUS         Status;\r
+  LIST_ENTRY         *Package;\r
+  CHAR16             *ProblemParam;\r
+  CHAR16             *FileName1;\r
+  CHAR16             *FileName2;\r
+  CONST CHAR16       *TempParam;\r
+  SHELL_STATUS       ShellStatus;\r
+  SHELL_FILE_HANDLE  FileHandle1;\r
+  SHELL_FILE_HANDLE  FileHandle2;\r
+  UINT64             Size1;\r
+  UINT64             Size2;\r
+  UINT64             DifferentBytes;\r
+  UINT64             DifferentCount;\r
+  UINT8              DiffPointNumber;\r
+  UINT8              OneByteFromFile1;\r
+  UINT8              OneByteFromFile2;\r
+  UINT8              *DataFromFile1;\r
+  UINT8              *DataFromFile2;\r
+  FILE_BUFFER        FileBuffer1;\r
+  FILE_BUFFER        FileBuffer2;\r
+  UINTN              InsertPosition1;\r
+  UINTN              InsertPosition2;\r
+  UINTN              DataSizeFromFile1;\r
+  UINTN              DataSizeFromFile2;\r
+  UINTN              TempAddress;\r
+  UINTN              Index;\r
+  UINTN              DiffPointAddress;\r
+  READ_STATUS        ReadStatus;\r
+\r
+  ShellStatus      = SHELL_SUCCESS;\r
+  Status           = EFI_SUCCESS;\r
+  FileName1        = NULL;\r
+  FileName2        = NULL;\r
+  FileHandle1      = NULL;\r
+  FileHandle2      = NULL;\r
+  DataFromFile1    = NULL;\r
+  DataFromFile2    = NULL;\r
+  ReadStatus       = OutOfDiffPoint;\r
+  DifferentCount   = 10;\r
+  DifferentBytes   = 4;\r
+  DiffPointNumber  = 0;\r
+  InsertPosition1  = 0;\r
+  InsertPosition2  = 0;\r
+  TempAddress      = 0;\r
+  DiffPointAddress = 0;\r
 \r
   //\r
   // initialize the shell lib (we must be in non-auto-init...)\r
   //\r
-  Status = ShellInitialize();\r
-  ASSERT_EFI_ERROR(Status);\r
+  Status = ShellInitialize ();\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-  Status = CommandInit();\r
-  ASSERT_EFI_ERROR(Status);\r
+  Status = CommandInit ();\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
   //\r
   // parse the command line\r
   //\r
   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), gShellDebug1HiiHandle, L"comp", ProblemParam);  \r
-      FreePool(ProblemParam);\r
+  if (EFI_ERROR (Status)) {\r
+    if ((Status == EFI_VOLUME_CORRUPTED) && (ProblemParam != NULL)) {\r
+      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM), gShellDebug1HiiHandle, L"comp", ProblemParam);\r
+      FreePool (ProblemParam);\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
-      ASSERT(FALSE);\r
+      ASSERT (FALSE);\r
     }\r
   } else {\r
-    if (ShellCommandLineGetCount(Package) > 3) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"comp");  \r
+    if (ShellCommandLineGetCount (Package) > 3) {\r
+      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), gShellDebug1HiiHandle, L"comp");\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
-    } else if (ShellCommandLineGetCount(Package) < 3) {\r
-      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"comp");  \r
+    } else if (ShellCommandLineGetCount (Package) < 3) {\r
+      ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), gShellDebug1HiiHandle, L"comp");\r
       ShellStatus = SHELL_INVALID_PARAMETER;\r
     } else {\r
-      TempParam = ShellCommandLineGetRawValue(Package, 1);\r
-      ASSERT(TempParam != NULL);\r
-      FileName1 = ShellFindFilePath(TempParam);\r
+      TempParam = ShellCommandLineGetRawValue (Package, 1);\r
+      ASSERT (TempParam != NULL);\r
+      FileName1 = ShellFindFilePath (TempParam);\r
       if (FileName1 == NULL) {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  \r
+        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);\r
         ShellStatus = SHELL_NOT_FOUND;\r
       } else {\r
-        Status = ShellOpenFileByName(FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0);\r
-        if (EFI_ERROR(Status)) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  \r
+        Status = ShellOpenFileByName (FileName1, &FileHandle1, EFI_FILE_MODE_READ, 0);\r
+        if (EFI_ERROR (Status)) {\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);\r
           ShellStatus = SHELL_NOT_FOUND;\r
         }\r
       }\r
-      TempParam = ShellCommandLineGetRawValue(Package, 2);\r
-      ASSERT(TempParam != NULL);\r
-      FileName2 = ShellFindFilePath(TempParam);\r
+\r
+      TempParam = ShellCommandLineGetRawValue (Package, 2);\r
+      ASSERT (TempParam != NULL);\r
+      FileName2 = ShellFindFilePath (TempParam);\r
       if (FileName2 == NULL) {\r
-        ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  \r
+        ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_FILE_FIND_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);\r
         ShellStatus = SHELL_NOT_FOUND;\r
       } else {\r
-        Status = ShellOpenFileByName(FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0);\r
-        if (EFI_ERROR(Status)) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);  \r
+        Status = ShellOpenFileByName (FileName2, &FileHandle2, EFI_FILE_MODE_READ, 0);\r
+        if (EFI_ERROR (Status)) {\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_FILE_OPEN_FAIL), gShellDebug1HiiHandle, L"comp", TempParam);\r
           ShellStatus = SHELL_NOT_FOUND;\r
         }\r
       }\r
+\r
       if (ShellStatus == SHELL_SUCCESS) {\r
-        Status = gEfiShellProtocol->GetFileSize(FileHandle1, &Size1);\r
-        ASSERT_EFI_ERROR(Status);\r
-        Status = gEfiShellProtocol->GetFileSize(FileHandle2, &Size2);\r
-        ASSERT_EFI_ERROR(Status);\r
+        Status = gEfiShellProtocol->GetFileSize (FileHandle1, &Size1);\r
+        ASSERT_EFI_ERROR (Status);\r
+        Status = gEfiShellProtocol->GetFileSize (FileHandle2, &Size2);\r
+        ASSERT_EFI_ERROR (Status);\r
 \r
         if (ShellCommandLineGetFlag (Package, L"-n")) {\r
           TempParam = ShellCommandLineGetValue (Package, L"-n");\r
@@ -208,7 +323,7 @@ ShellCommandRunComp (
               DifferentCount = MAX_UINTN;\r
             } else {\r
               Status = ShellConvertStringToUint64 (TempParam, &DifferentCount, FALSE, TRUE);\r
-              if (EFI_ERROR(Status) || DifferentCount == 0) {\r
+              if (EFI_ERROR (Status) || (DifferentCount == 0)) {\r
                 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-n");\r
                 ShellStatus = SHELL_INVALID_PARAMETER;\r
               }\r
@@ -223,7 +338,7 @@ ShellCommandRunComp (
             ShellStatus = SHELL_INVALID_PARAMETER;\r
           } else {\r
             Status = ShellConvertStringToUint64 (TempParam, &DifferentBytes, FALSE, TRUE);\r
-            if (EFI_ERROR(Status) || DifferentBytes == 0) {\r
+            if (EFI_ERROR (Status) || (DifferentBytes == 0)) {\r
               ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PROBLEM_VAL), gShellDebug1HiiHandle, L"comp", TempParam, L"-s");\r
               ShellStatus = SHELL_INVALID_PARAMETER;\r
             } else {\r
@@ -238,10 +353,16 @@ ShellCommandRunComp (
       if (ShellStatus == SHELL_SUCCESS) {\r
         DataFromFile1 = AllocateZeroPool ((UINTN)DifferentBytes);\r
         DataFromFile2 = AllocateZeroPool ((UINTN)DifferentBytes);\r
-        if (DataFromFile1 == NULL || DataFromFile2 == NULL) {\r
+        FileBufferInit (&FileBuffer1);\r
+        FileBufferInit (&FileBuffer2);\r
+        if ((DataFromFile1 == NULL) || (DataFromFile2 == NULL) ||\r
+            (FileBuffer1.Data == NULL) || (FileBuffer2.Data == NULL))\r
+        {\r
           ShellStatus = SHELL_OUT_OF_RESOURCES;\r
           SHELL_FREE_NON_NULL (DataFromFile1);\r
           SHELL_FREE_NON_NULL (DataFromFile2);\r
+          FileBufferUninit (&FileBuffer1);\r
+          FileBufferUninit (&FileBuffer2);\r
         }\r
       }\r
 \r
@@ -249,11 +370,21 @@ ShellCommandRunComp (
         while (DiffPointNumber < DifferentCount) {\r
           DataSizeFromFile1 = 1;\r
           DataSizeFromFile2 = 1;\r
-          OneByteFromFile1 = 0;\r
-          OneByteFromFile2 = 0;\r
-          Status = gEfiShellProtocol->ReadFile (FileHandle1, &DataSizeFromFile1, &OneByteFromFile1);\r
+          OneByteFromFile1  = 0;\r
+          OneByteFromFile2  = 0;\r
+          Status            = FileBufferReadByte (\r
+                                FileHandle1,\r
+                                &FileBuffer1,\r
+                                &DataSizeFromFile1,\r
+                                &OneByteFromFile1\r
+                                );\r
           ASSERT_EFI_ERROR (Status);\r
-          Status = gEfiShellProtocol->ReadFile (FileHandle2, &DataSizeFromFile2, &OneByteFromFile2);\r
+          Status = FileBufferReadByte (\r
+                     FileHandle2,\r
+                     &FileBuffer2,\r
+                     &DataSizeFromFile2,\r
+                     &OneByteFromFile2\r
+                     );\r
           ASSERT_EFI_ERROR (Status);\r
 \r
           TempAddress++;\r
@@ -263,19 +394,21 @@ ShellCommandRunComp (
           // 2.If no more char in File1 or File2, The ReadStatus is InPrevDiffPoint forever.\r
           //   So the previous different point is the last one, then break the while block.\r
           //\r
-          if ( (DataSizeFromFile1 == 0 && InsertPosition1 == 0 && DataSizeFromFile2 == 0 && InsertPosition2 == 0) ||\r
-               (ReadStatus == InPrevDiffPoint && (DataSizeFromFile1 == 0 || DataSizeFromFile2 == 0))\r
-             ) {\r
+          if (((DataSizeFromFile1 == 0) && (InsertPosition1 == 0) && (DataSizeFromFile2 == 0) && (InsertPosition2 == 0)) ||\r
+              ((ReadStatus == InPrevDiffPoint) && ((DataSizeFromFile1 == 0) || (DataSizeFromFile2 == 0)))\r
+              )\r
+          {\r
             break;\r
           }\r
 \r
           if (ReadStatus == OutOfDiffPoint) {\r
             if (OneByteFromFile1 != OneByteFromFile2) {\r
-              ReadStatus = InDiffPoint;\r
+              ReadStatus       = InDiffPoint;\r
               DiffPointAddress = TempAddress;\r
               if (DataSizeFromFile1 == 1) {\r
                 DataFromFile1[InsertPosition1++] = OneByteFromFile1;\r
               }\r
+\r
               if (DataSizeFromFile2 == 1) {\r
                 DataFromFile2[InsertPosition2++] = OneByteFromFile2;\r
               }\r
@@ -284,6 +417,7 @@ ShellCommandRunComp (
             if (DataSizeFromFile1 == 1) {\r
               DataFromFile1[InsertPosition1++] = OneByteFromFile1;\r
             }\r
+\r
             if (DataSizeFromFile2 == 1) {\r
               DataFromFile2[InsertPosition2++] = OneByteFromFile2;\r
             }\r
@@ -296,19 +430,19 @@ ShellCommandRunComp (
           //\r
           // ReadStatus should be always equal InDiffPoint.\r
           //\r
-          if ( InsertPosition1 == DifferentBytes ||\r
-               InsertPosition2 == DifferentBytes ||\r
-               (DataSizeFromFile1 == 0 && DataSizeFromFile2 == 0)\r
-             ) {\r
-\r
+          if ((InsertPosition1 == DifferentBytes) ||\r
+              (InsertPosition2 == DifferentBytes) ||\r
+              ((DataSizeFromFile1 == 0) && (DataSizeFromFile2 == 0))\r
+              )\r
+          {\r
             ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_DIFFERENCE_POINT), gShellDebug1HiiHandle, ++DiffPointNumber);\r
-            PrintDifferentPoint (FileName1, DataFromFile1, InsertPosition1, DiffPointAddress, DifferentBytes);\r
-            PrintDifferentPoint (FileName2, DataFromFile2, InsertPosition2, DiffPointAddress, DifferentBytes);\r
+            PrintDifferentPoint (FileName1, L"File1", DataFromFile1, InsertPosition1, DiffPointAddress, DifferentBytes);\r
+            PrintDifferentPoint (FileName2, L"File2", DataFromFile2, InsertPosition2, DiffPointAddress, DifferentBytes);\r
 \r
             //\r
             // One of two buffuers is empty, it means this is the last different point.\r
             //\r
-            if (InsertPosition1 == 0 || InsertPosition2 == 0) {\r
+            if ((InsertPosition1 == 0) || (InsertPosition2 == 0)) {\r
               break;\r
             }\r
 \r
@@ -323,9 +457,9 @@ ShellCommandRunComp (
               //\r
               // Try to find a new different point in the rest of DataFromFile.\r
               //\r
-              for (; Index < MAX (InsertPosition1,InsertPosition2); Index++) {\r
+              for ( ; Index < MAX (InsertPosition1, InsertPosition2); Index++) {\r
                 if (DataFromFile1[Index] != DataFromFile2[Index]) {\r
-                  ReadStatus = InDiffPoint;\r
+                  ReadStatus        = InDiffPoint;\r
                   DiffPointAddress += Index;\r
                   break;\r
                 }\r
@@ -350,25 +484,30 @@ ShellCommandRunComp (
 \r
         SHELL_FREE_NON_NULL (DataFromFile1);\r
         SHELL_FREE_NON_NULL (DataFromFile2);\r
+        FileBufferUninit (&FileBuffer1);\r
+        FileBufferUninit (&FileBuffer2);\r
 \r
         if (DiffPointNumber == 0) {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_PASS), gShellDebug1HiiHandle);\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_PASS), gShellDebug1HiiHandle);\r
         } else {\r
-          ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_FAIL), gShellDebug1HiiHandle);\r
+          ShellStatus = SHELL_NOT_EQUAL;\r
+          ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_COMP_FOOTER_FAIL), gShellDebug1HiiHandle);\r
         }\r
       }\r
     }\r
 \r
     ShellCommandLineFreeVarList (Package);\r
   }\r
-  SHELL_FREE_NON_NULL(FileName1);\r
-  SHELL_FREE_NON_NULL(FileName2);\r
+\r
+  SHELL_FREE_NON_NULL (FileName1);\r
+  SHELL_FREE_NON_NULL (FileName2);\r
 \r
   if (FileHandle1 != NULL) {\r
-    gEfiShellProtocol->CloseFile(FileHandle1);\r
+    gEfiShellProtocol->CloseFile (FileHandle1);\r
   }\r
+\r
   if (FileHandle2 != NULL) {\r
-    gEfiShellProtocol->CloseFile(FileHandle2);\r
+    gEfiShellProtocol->CloseFile (FileHandle2);\r
   }\r
 \r
   return (ShellStatus);\r