]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / UefiFileHandleLib / UefiFileHandleLib.c
index e1f3e89b8c80d89786456c7cadac78c45a9d4d6e..86678e965d99bd41ab067937b23a15c2f5b87427 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   Provides interface to EFI_FILE_HANDLE functionality.\r
 \r
-  Copyright (c) 2006 - 2015, 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) 2006 - 2018, Intel Corporation. All rights reserved. <BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
 #include <Library/PcdLib.h>\r
 #include <Library/PrintLib.h>\r
 \r
-CONST UINT16 gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;\r
+CONST UINT16  gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;\r
 \r
-#define MAX_FILE_NAME_LEN 522 // (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
-#define FIND_XXXXX_FILE_BUFFER_SIZE (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
+#define MAX_FILE_NAME_LEN            522// (20 * (6+5+2))+1) unicode characters from EFI FAT spec (doubled for bytes)\r
+#define FIND_XXXXX_FILE_BUFFER_SIZE  (SIZE_OF_EFI_FILE_INFO + MAX_FILE_NAME_LEN)\r
 \r
 /**\r
   This function will retrieve the information about the file for the handle\r
@@ -46,15 +40,15 @@ CONST UINT16 gUnicodeFileTag = EFI_UNICODE_BYTE_ORDER_MARK;
 \r
   @return the information about the file\r
 **/\r
-EFI_FILE_INFO*\r
+EFI_FILE_INFO *\r
 EFIAPI\r
 FileHandleGetInfo (\r
-  IN EFI_FILE_HANDLE            FileHandle\r
+  IN EFI_FILE_HANDLE  FileHandle\r
   )\r
 {\r
-  EFI_FILE_INFO   *FileInfo;\r
-  UINTN           FileInfoSize;\r
-  EFI_STATUS      Status;\r
+  EFI_FILE_INFO  *FileInfo;\r
+  UINTN          FileInfoSize;\r
+  EFI_STATUS     Status;\r
 \r
   if (FileHandle == NULL) {\r
     return (NULL);\r
@@ -64,31 +58,38 @@ FileHandleGetInfo (
   // Get the required size to allocate\r
   //\r
   FileInfoSize = 0;\r
-  FileInfo = NULL;\r
-  Status = FileHandle->GetInfo(FileHandle,\r
+  FileInfo     = NULL;\r
+  Status       = FileHandle->GetInfo (\r
+                               FileHandle,\r
                                &gEfiFileInfoGuid,\r
                                &FileInfoSize,\r
-                               NULL);\r
-  if (Status == EFI_BUFFER_TOO_SMALL){\r
+                               NULL\r
+                               );\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
     //\r
     // error is expected.  getting size to allocate\r
     //\r
-    FileInfo = AllocateZeroPool(FileInfoSize);\r
-    //\r
-    // now get the information\r
-    //\r
-    Status = FileHandle->GetInfo(FileHandle,\r
-                                 &gEfiFileInfoGuid,\r
-                                 &FileInfoSize,\r
-                                 FileInfo);\r
-    //\r
-    // if we got an error free the memory and return NULL\r
-    //\r
-    if (EFI_ERROR(Status) && (FileInfo != NULL)) {\r
-      FreePool(FileInfo);\r
-      FileInfo = NULL;\r
+    FileInfo = AllocateZeroPool (FileInfoSize);\r
+    if (FileInfo != NULL) {\r
+      //\r
+      // now get the information\r
+      //\r
+      Status = FileHandle->GetInfo (\r
+                             FileHandle,\r
+                             &gEfiFileInfoGuid,\r
+                             &FileInfoSize,\r
+                             FileInfo\r
+                             );\r
+      //\r
+      // if we got an error free the memory and return NULL\r
+      //\r
+      if (EFI_ERROR (Status)) {\r
+        FreePool (FileInfo);\r
+        FileInfo = NULL;\r
+      }\r
     }\r
   }\r
+\r
   return (FileInfo);\r
 }\r
 \r
@@ -114,22 +115,23 @@ FileHandleGetInfo (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleSetInfo (\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  IN CONST EFI_FILE_INFO        *FileInfo\r
+  IN EFI_FILE_HANDLE      FileHandle,\r
+  IN CONST EFI_FILE_INFO  *FileInfo\r
   )\r
 {\r
-\r
-  if (FileHandle == NULL || FileInfo == NULL) {\r
+  if ((FileHandle == NULL) || (FileInfo == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
   //\r
   // Set the info\r
   //\r
-  return (FileHandle->SetInfo(FileHandle,\r
-                              &gEfiFileInfoGuid,\r
-                              (UINTN)FileInfo->Size,\r
-                              (EFI_FILE_INFO*)FileInfo));\r
+  return (FileHandle->SetInfo (\r
+                        FileHandle,\r
+                        &gEfiFileInfoGuid,\r
+                        (UINTN)FileInfo->Size,\r
+                        (EFI_FILE_INFO *)FileInfo\r
+                        ));\r
 }\r
 \r
 /**\r
@@ -163,10 +165,10 @@ FileHandleSetInfo (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandleRead(\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  IN OUT UINTN                  *BufferSize,\r
-  OUT VOID                      *Buffer\r
+FileHandleRead (\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  IN OUT UINTN        *BufferSize,\r
+  OUT VOID            *Buffer\r
   )\r
 {\r
   if (FileHandle == NULL) {\r
@@ -176,10 +178,9 @@ FileHandleRead(
   //\r
   // Perform the read based on EFI_FILE_PROTOCOL\r
   //\r
-  return (FileHandle->Read(FileHandle, BufferSize, Buffer));\r
+  return (FileHandle->Read (FileHandle, BufferSize, Buffer));\r
 }\r
 \r
-\r
 /**\r
   Write data to a file.\r
 \r
@@ -206,10 +207,10 @@ FileHandleRead(
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandleWrite(\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  IN OUT UINTN                  *BufferSize,\r
-  IN VOID                       *Buffer\r
+FileHandleWrite (\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  IN OUT UINTN        *BufferSize,\r
+  IN VOID             *Buffer\r
   )\r
 {\r
   if (FileHandle == NULL) {\r
@@ -219,7 +220,7 @@ FileHandleWrite(
   //\r
   // Perform the write based on EFI_FILE_PROTOCOL\r
   //\r
-  return (FileHandle->Write(FileHandle, BufferSize, Buffer));\r
+  return (FileHandle->Write (FileHandle, BufferSize, Buffer));\r
 }\r
 \r
 /**\r
@@ -231,15 +232,15 @@ FileHandleWrite(
 \r
 @param FileHandle               the file handle to close.\r
 \r
-@retval EFI_SUCCESS             the file handle was closed sucessfully.\r
+@retval EFI_SUCCESS             the file handle was closed successfully.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 FileHandleClose (\r
-  IN EFI_FILE_HANDLE            FileHandle\r
+  IN EFI_FILE_HANDLE  FileHandle\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   if (FileHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -248,7 +249,7 @@ FileHandleClose (
   //\r
   // Perform the Close based on EFI_FILE_PROTOCOL\r
   //\r
-  Status = FileHandle->Close(FileHandle);\r
+  Status = FileHandle->Close (FileHandle);\r
   return Status;\r
 }\r
 \r
@@ -261,7 +262,7 @@ FileHandleClose (
 \r
   @param FileHandle             the file handle to delete\r
 \r
-  @retval EFI_SUCCESS           the file was closed sucessfully\r
+  @retval EFI_SUCCESS           the file was closed successfully\r
   @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not\r
                                 deleted\r
   @retval INVALID_PARAMETER     One of the parameters has an invalid value.\r
@@ -269,10 +270,10 @@ FileHandleClose (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleDelete (\r
-  IN EFI_FILE_HANDLE    FileHandle\r
+  IN EFI_FILE_HANDLE  FileHandle\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS  Status;\r
 \r
   if (FileHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -281,7 +282,7 @@ FileHandleDelete (
   //\r
   // Perform the Delete based on EFI_FILE_PROTOCOL\r
   //\r
-  Status = FileHandle->Delete(FileHandle);\r
+  Status = FileHandle->Delete (FileHandle);\r
   return Status;\r
 }\r
 \r
@@ -297,9 +298,9 @@ FileHandleDelete (
   has the effect of starting the read process of the directory entries over.\r
 \r
   @param FileHandle             The file handle on which the position is being set\r
-  @param Position               Byte position from begining of file\r
+  @param Position               Byte position from beginning of file\r
 \r
-  @retval EFI_SUCCESS           Operation completed sucessfully.\r
+  @retval EFI_SUCCESS           Operation completed successfully.\r
   @retval EFI_UNSUPPORTED       the seek request for non-zero is not valid on\r
                                 directories.\r
   @retval INVALID_PARAMETER     One of the parameters has an invalid value.\r
@@ -307,8 +308,8 @@ FileHandleDelete (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleSetPosition (\r
-  IN EFI_FILE_HANDLE    FileHandle,\r
-  IN UINT64             Position\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  IN UINT64           Position\r
   )\r
 {\r
   if (FileHandle == NULL) {\r
@@ -318,7 +319,7 @@ FileHandleSetPosition (
   //\r
   // Perform the SetPosition based on EFI_FILE_PROTOCOL\r
   //\r
-  return (FileHandle->SetPosition(FileHandle, Position));\r
+  return (FileHandle->SetPosition (FileHandle, Position));\r
 }\r
 \r
 /**\r
@@ -330,28 +331,29 @@ FileHandleSetPosition (
   if FileHandle is a directory.\r
 \r
   @param FileHandle             The open file handle on which to get the position.\r
-  @param Position               Byte position from begining of file.\r
+  @param Position               Byte position from beginning of file.\r
 \r
-  @retval EFI_SUCCESS           the operation completed sucessfully.\r
+  @retval EFI_SUCCESS           the operation completed successfully.\r
   @retval INVALID_PARAMETER     One of the parameters has an invalid value.\r
   @retval EFI_UNSUPPORTED       the request is not valid on directories.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 FileHandleGetPosition (\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  OUT UINT64                    *Position\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  OUT UINT64          *Position\r
   )\r
 {\r
-  if (Position == NULL || FileHandle == NULL) {\r
+  if ((Position == NULL) || (FileHandle == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
   //\r
   // Perform the GetPosition based on EFI_FILE_PROTOCOL\r
   //\r
-  return (FileHandle->GetPosition(FileHandle, Position));\r
+  return (FileHandle->GetPosition (FileHandle, Position));\r
 }\r
+\r
 /**\r
   Flushes data on a file\r
 \r
@@ -369,7 +371,7 @@ FileHandleGetPosition (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleFlush (\r
-  IN EFI_FILE_HANDLE            FileHandle\r
+  IN EFI_FILE_HANDLE  FileHandle\r
   )\r
 {\r
   if (FileHandle == NULL) {\r
@@ -379,7 +381,7 @@ FileHandleFlush (
   //\r
   // Perform the Flush based on EFI_FILE_PROTOCOL\r
   //\r
-  return (FileHandle->Flush(FileHandle));\r
+  return (FileHandle->Flush (FileHandle));\r
 }\r
 \r
 /**\r
@@ -391,17 +393,17 @@ FileHandleFlush (
   @param[in] DirHandle          Handle to open file.\r
 \r
   @retval EFI_SUCCESS           DirHandle is a directory.\r
-  @retval EFI_INVALID_PARAMETER DirHandle is NULL. \r
-                                The file information returns from FileHandleGetInfo is NULL. \r
+  @retval EFI_INVALID_PARAMETER DirHandle is NULL.\r
+                                The file information returns from FileHandleGetInfo is NULL.\r
   @retval EFI_NOT_FOUND         DirHandle is not a directory.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 FileHandleIsDirectory (\r
-  IN EFI_FILE_HANDLE            DirHandle\r
+  IN EFI_FILE_HANDLE  DirHandle\r
   )\r
 {\r
-  EFI_FILE_INFO *DirInfo;\r
+  EFI_FILE_INFO  *DirInfo;\r
 \r
   if (DirHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -421,6 +423,7 @@ FileHandleIsDirectory (
     //\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
+\r
   if ((DirInfo->Attribute & EFI_FILE_DIRECTORY) == 0) {\r
     //\r
     // Attributes say this is not a directory\r
@@ -428,6 +431,7 @@ FileHandleIsDirectory (
     FreePool (DirInfo);\r
     return (EFI_NOT_FOUND);\r
   }\r
+\r
   //\r
   // all good...\r
   //\r
@@ -460,22 +464,22 @@ FileHandleIsDirectory (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleFindFirstFile (\r
-  IN EFI_FILE_HANDLE            DirHandle,\r
-  OUT EFI_FILE_INFO             **Buffer\r
+  IN EFI_FILE_HANDLE  DirHandle,\r
+  OUT EFI_FILE_INFO   **Buffer\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  UINTN         BufferSize;\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
 \r
-  if (Buffer == NULL || DirHandle == NULL) {\r
+  if ((Buffer == NULL) || (DirHandle == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
   //\r
   // verify that DirHandle is a directory\r
   //\r
-  Status = FileHandleIsDirectory(DirHandle);\r
-  if (EFI_ERROR(Status)) {\r
+  Status = FileHandleIsDirectory (DirHandle);\r
+  if (EFI_ERROR (Status)) {\r
     return (Status);\r
   }\r
 \r
@@ -483,17 +487,17 @@ FileHandleFindFirstFile (
   // Allocate a buffer sized to struct size + enough for the string at the end\r
   //\r
   BufferSize = FIND_XXXXX_FILE_BUFFER_SIZE;\r
-  *Buffer = AllocateZeroPool(BufferSize);\r
-  if (*Buffer == NULL){\r
+  *Buffer    = AllocateZeroPool (BufferSize);\r
+  if (*Buffer == NULL) {\r
     return (EFI_OUT_OF_RESOURCES);\r
   }\r
 \r
   //\r
-  // reset to the begining of the directory\r
+  // reset to the beginning of the directory\r
   //\r
-  Status = FileHandleSetPosition(DirHandle, 0);\r
-  if (EFI_ERROR(Status)) {\r
-    FreePool(*Buffer);\r
+  Status = FileHandleSetPosition (DirHandle, 0);\r
+  if (EFI_ERROR (Status)) {\r
+    FreePool (*Buffer);\r
     *Buffer = NULL;\r
     return (Status);\r
   }\r
@@ -502,15 +506,17 @@ FileHandleFindFirstFile (
   // read in the info about the first file\r
   //\r
   Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r
-  ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
-  if (EFI_ERROR(Status) || BufferSize == 0) {\r
-    FreePool(*Buffer);\r
+  ASSERT (Status != EFI_BUFFER_TOO_SMALL);\r
+  if (EFI_ERROR (Status) || (BufferSize == 0)) {\r
+    FreePool (*Buffer);\r
     *Buffer = NULL;\r
     if (BufferSize == 0) {\r
       return (EFI_NOT_FOUND);\r
     }\r
+\r
     return (Status);\r
   }\r
+\r
   return (EFI_SUCCESS);\r
 }\r
 \r
@@ -534,16 +540,16 @@ FileHandleFindFirstFile (
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandleFindNextFile(\r
-  IN EFI_FILE_HANDLE          DirHandle,\r
-  OUT EFI_FILE_INFO          *Buffer,\r
-  OUT BOOLEAN                *NoFile\r
+FileHandleFindNextFile (\r
+  IN EFI_FILE_HANDLE  DirHandle,\r
+  OUT EFI_FILE_INFO   *Buffer,\r
+  OUT BOOLEAN         *NoFile\r
   )\r
 {\r
-  EFI_STATUS    Status;\r
-  UINTN         BufferSize;\r
+  EFI_STATUS  Status;\r
+  UINTN       BufferSize;\r
 \r
-  if (DirHandle == NULL || Buffer == NULL || NoFile == NULL) {\r
+  if ((DirHandle == NULL) || (Buffer == NULL) || (NoFile == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
@@ -556,8 +562,8 @@ FileHandleFindNextFile(
   // read in the info about the next file\r
   //\r
   Status = FileHandleRead (DirHandle, &BufferSize, Buffer);\r
-  ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
-  if (EFI_ERROR(Status)) {\r
+  ASSERT (Status != EFI_BUFFER_TOO_SMALL);\r
+  if (EFI_ERROR (Status)) {\r
     return (Status);\r
   }\r
 \r
@@ -565,7 +571,7 @@ FileHandleFindNextFile(
   // If we read 0 bytes (but did not have erros) we already read in the last file.\r
   //\r
   if (BufferSize == 0) {\r
-    FreePool(Buffer);\r
+    FreePool (Buffer);\r
     *NoFile = TRUE;\r
   }\r
 \r
@@ -581,7 +587,7 @@ FileHandleFindNextFile(
   @param[in] FileHandle         The file handle from which size is retrieved.\r
   @param[out] Size              The pointer to size.\r
 \r
-  @retval EFI_SUCCESS           Operation was completed sucessfully.\r
+  @retval EFI_SUCCESS           Operation was completed successfully.\r
   @retval EFI_DEVICE_ERROR      Cannot access the file.\r
   @retval EFI_INVALID_PARAMETER FileHandle is NULL.\r
                                 Size is NULL.\r
@@ -589,20 +595,20 @@ FileHandleFindNextFile(
 EFI_STATUS\r
 EFIAPI\r
 FileHandleGetSize (\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  OUT UINT64                    *Size\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  OUT UINT64          *Size\r
   )\r
 {\r
-  EFI_FILE_INFO                 *FileInfo;\r
+  EFI_FILE_INFO  *FileInfo;\r
 \r
-  if (FileHandle == NULL || Size == NULL) {\r
+  if ((FileHandle == NULL) || (Size == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
   //\r
   // get the FileInfo structure\r
   //\r
-  FileInfo = FileHandleGetInfo(FileHandle);\r
+  FileInfo = FileHandleGetInfo (FileHandle);\r
   if (FileInfo == NULL) {\r
     return (EFI_DEVICE_ERROR);\r
   }\r
@@ -615,7 +621,7 @@ FileHandleGetSize (
   //\r
   // free the FileInfo memory\r
   //\r
-  FreePool(FileInfo);\r
+  FreePool (FileInfo);\r
 \r
   return (EFI_SUCCESS);\r
 }\r
@@ -636,12 +642,12 @@ FileHandleGetSize (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleSetSize (\r
-  IN EFI_FILE_HANDLE            FileHandle,\r
-  IN UINT64                     Size\r
+  IN EFI_FILE_HANDLE  FileHandle,\r
+  IN UINT64           Size\r
   )\r
 {\r
-  EFI_FILE_INFO                 *FileInfo;\r
-  EFI_STATUS                    Status;\r
+  EFI_FILE_INFO  *FileInfo;\r
+  EFI_STATUS     Status;\r
 \r
   if (FileHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
@@ -650,7 +656,7 @@ FileHandleSetSize (
   //\r
   // get the FileInfo structure\r
   //\r
-  FileInfo = FileHandleGetInfo(FileHandle);\r
+  FileInfo = FileHandleGetInfo (FileHandle);\r
   if (FileInfo == NULL) {\r
     return (EFI_DEVICE_ERROR);\r
   }\r
@@ -660,11 +666,11 @@ FileHandleSetSize (
   //\r
   FileInfo->FileSize = Size;\r
 \r
-  Status = FileHandleSetInfo(FileHandle, FileInfo);\r
+  Status = FileHandleSetInfo (FileHandle, FileInfo);\r
   //\r
   // free the FileInfo memory\r
   //\r
-  FreePool(FileInfo);\r
+  FreePool (FileInfo);\r
 \r
   return (Status);\r
 }\r
@@ -701,18 +707,18 @@ FileHandleSetSize (
 \r
   @return Destination           return the resultant string.\r
 **/\r
-CHAR16*\r
+CHAR16 *\r
 EFIAPI\r
 StrnCatGrowLeft (\r
-  IN OUT CHAR16           **Destination,\r
-  IN OUT UINTN            *CurrentSize,\r
-  IN     CONST CHAR16     *Source,\r
-  IN     UINTN            Count\r
+  IN OUT CHAR16        **Destination,\r
+  IN OUT UINTN         *CurrentSize,\r
+  IN     CONST CHAR16  *Source,\r
+  IN     UINTN         Count\r
   )\r
 {\r
-  UINTN DestinationStartSize;\r
-  UINTN NewSize;\r
-  UINTN CopySize;\r
+  UINTN  DestinationStartSize;\r
+  UINTN  NewSize;\r
+  UINTN  CopySize;\r
 \r
   if (Destination == NULL) {\r
     return (NULL);\r
@@ -729,19 +735,19 @@ StrnCatGrowLeft (
   // allow for NULL pointers address as Destination\r
   //\r
   if (*Destination != NULL) {\r
-    ASSERT(CurrentSize != 0);\r
-    DestinationStartSize = StrSize(*Destination);\r
-    ASSERT(DestinationStartSize <= *CurrentSize);\r
+    ASSERT (CurrentSize != 0);\r
+    DestinationStartSize = StrSize (*Destination);\r
+    ASSERT (DestinationStartSize <= *CurrentSize);\r
   } else {\r
     DestinationStartSize = 0;\r
-//    ASSERT(*CurrentSize == 0);\r
+    //    ASSERT(*CurrentSize == 0);\r
   }\r
 \r
   //\r
   // Append all of Source?\r
   //\r
   if (Count == 0) {\r
-    Count = StrSize(Source);\r
+    Count = StrSize (Source);\r
   }\r
 \r
   //\r
@@ -752,24 +758,28 @@ StrnCatGrowLeft (
     while (NewSize < (DestinationStartSize + Count)) {\r
       NewSize += 2 * Count;\r
     }\r
-    *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
+\r
+    *Destination = ReallocatePool (*CurrentSize, NewSize, *Destination);\r
     *CurrentSize = NewSize;\r
   } else {\r
-    *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r
+    *Destination = AllocateZeroPool (Count+sizeof (CHAR16));\r
   }\r
+\r
   if (*Destination == NULL) {\r
     return NULL;\r
   }\r
 \r
-  CopySize = StrSize(*Destination);\r
-  CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);\r
-  CopyMem(*Destination, Source, Count-2);\r
+  CopySize = StrSize (*Destination);\r
+  CopyMem ((*Destination)+((Count-2)/sizeof (CHAR16)), *Destination, CopySize);\r
+  CopyMem (*Destination, Source, Count-2);\r
   return (*Destination);\r
 }\r
 \r
 /**\r
   Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the\r
-  directory 'stack'.\r
+  directory 'stack'. If the file is a directory, then append the '\' char at the\r
+  end of name string. If it's not a directory, then the last '\' should not be\r
+  added.\r
 \r
   if Handle is NULL, return EFI_INVALID_PARAMETER\r
 \r
@@ -785,83 +795,105 @@ StrnCatGrowLeft (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleGetFileName (\r
-  IN CONST EFI_FILE_HANDLE      Handle,\r
-  OUT CHAR16                    **FullFileName\r
+  IN CONST EFI_FILE_HANDLE  Handle,\r
+  OUT CHAR16                **FullFileName\r
   )\r
 {\r
-  EFI_STATUS      Status;\r
-  UINTN           Size;\r
-  EFI_FILE_HANDLE CurrentHandle;\r
-  EFI_FILE_HANDLE NextHigherHandle;\r
-  EFI_FILE_INFO   *FileInfo;\r
+  EFI_STATUS       Status;\r
+  UINTN            Size;\r
+  EFI_FILE_HANDLE  CurrentHandle;\r
+  EFI_FILE_HANDLE  NextHigherHandle;\r
+  EFI_FILE_INFO    *FileInfo;\r
 \r
   Size = 0;\r
 \r
   //\r
   // Check our parameters\r
   //\r
-  if (FullFileName == NULL || Handle == NULL) {\r
+  if ((FullFileName == NULL) || (Handle == NULL)) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
   *FullFileName = NULL;\r
   CurrentHandle = NULL;\r
 \r
-  Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r
-  if (!EFI_ERROR(Status)) {\r
+  Status = Handle->Open (Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r
+  if (!EFI_ERROR (Status)) {\r
     //\r
     // Reverse out the current directory on the device\r
     //\r
-    for (;;) {\r
-      FileInfo = FileHandleGetInfo(CurrentHandle);\r
+    for ( ; ;) {\r
+      FileInfo = FileHandleGetInfo (CurrentHandle);\r
       if (FileInfo == NULL) {\r
         Status = EFI_OUT_OF_RESOURCES;\r
         break;\r
       } else {\r
         //\r
-        // We got info... do we have a name? if yes preceed the current path with it...\r
+        // Prepare to move to the parent directory.\r
+        // Also determine whether CurrentHandle refers to the Root directory.\r
+        //\r
+        Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r
+        //\r
+        // We got info... do we have a name? if yes precede the current path with it...\r
         //\r
-        if (StrLen (FileInfo->FileName) == 0) {\r
+        if ((StrLen (FileInfo->FileName) == 0) || EFI_ERROR (Status)) {\r
+          //\r
+          // Both FileInfo->FileName being '\0' and EFI_ERROR() suggest that\r
+          // CurrentHandle refers to the Root directory.  As this loop ensures\r
+          // FullFileName is starting with '\\' at all times, signal success\r
+          // and exit the loop.\r
+          // While FileInfo->FileName could theoretically be a value other than\r
+          // '\0' or '\\', '\\' is guaranteed to be supported by the\r
+          // specification and hence its value can safely be ignored.\r
+          //\r
+          Status = EFI_SUCCESS;\r
           if (*FullFileName == NULL) {\r
-            ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
-            *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+            ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
+            *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);\r
           }\r
-          FreePool(FileInfo);\r
+\r
+          FreePool (FileInfo);\r
           break;\r
         } else {\r
           if (*FullFileName == NULL) {\r
-            ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
-            *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+            ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
+            *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);\r
           }\r
-          ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
-          *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r
-          *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
-          FreePool(FileInfo);\r
+\r
+          ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
+          *FullFileName = StrnCatGrowLeft (FullFileName, &Size, FileInfo->FileName, 0);\r
+          *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);\r
+          FreePool (FileInfo);\r
         }\r
       }\r
+\r
+      FileHandleClose (CurrentHandle);\r
       //\r
       // Move to the parent directory\r
       //\r
-      Status = CurrentHandle->Open (CurrentHandle, &NextHigherHandle, L"..", EFI_FILE_MODE_READ, 0);\r
-      if (EFI_ERROR (Status)) {\r
-        break;\r
-      }\r
-\r
-      FileHandleClose(CurrentHandle);\r
       CurrentHandle = NextHigherHandle;\r
     }\r
   } else if (Status == EFI_NOT_FOUND) {\r
     Status = EFI_SUCCESS;\r
-    ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
-    *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+    ASSERT ((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
+    *FullFileName = StrnCatGrowLeft (FullFileName, &Size, L"\\", 0);\r
+  }\r
+\r
+  if ((*FullFileName != NULL) &&\r
+      ((*FullFileName)[StrLen (*FullFileName) - 1] == L'\\') &&\r
+      (StrLen (*FullFileName) > 1) &&\r
+      (FileHandleIsDirectory (Handle) == EFI_NOT_FOUND)\r
+      )\r
+  {\r
+    (*FullFileName)[StrLen (*FullFileName) - 1] = CHAR_NULL;\r
   }\r
 \r
   if (CurrentHandle != NULL) {\r
     CurrentHandle->Close (CurrentHandle);\r
   }\r
 \r
-  if (EFI_ERROR(Status) && *FullFileName != NULL) {\r
-    FreePool(*FullFileName);\r
+  if (EFI_ERROR (Status) && (*FullFileName != NULL)) {\r
+    FreePool (*FullFileName);\r
   }\r
 \r
   return (Status);\r
@@ -881,140 +913,181 @@ FileHandleGetFileName (
 \r
   @sa FileHandleReadLine\r
 **/\r
-CHAR16*\r
+CHAR16 *\r
 EFIAPI\r
-FileHandleReturnLine(\r
-  IN EFI_FILE_HANDLE            Handle,\r
-  IN OUT BOOLEAN                *Ascii\r
+FileHandleReturnLine (\r
+  IN EFI_FILE_HANDLE  Handle,\r
+  IN OUT BOOLEAN      *Ascii\r
   )\r
 {\r
-  CHAR16          *RetVal;\r
-  UINTN           Size;\r
-  EFI_STATUS      Status;\r
+  CHAR16      *RetVal;\r
+  UINTN       Size;\r
+  EFI_STATUS  Status;\r
 \r
-  Size = 0;\r
+  Size   = 0;\r
   RetVal = NULL;\r
 \r
-  Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
+  Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);\r
   if (Status == EFI_BUFFER_TOO_SMALL) {\r
-    RetVal = AllocateZeroPool(Size);\r
-    Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
+    RetVal = AllocateZeroPool (Size);\r
+    Status = FileHandleReadLine (Handle, RetVal, &Size, FALSE, Ascii);\r
   }\r
-  ASSERT_EFI_ERROR(Status);\r
-  if (EFI_ERROR(Status) && (RetVal != NULL)) {\r
-    FreePool(RetVal);\r
+\r
+  ASSERT_EFI_ERROR (Status);\r
+  if (EFI_ERROR (Status) && (RetVal != NULL)) {\r
+    FreePool (RetVal);\r
     RetVal = NULL;\r
   }\r
+\r
   return (RetVal);\r
 }\r
 \r
 /**\r
-  Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.\r
+  Function to read a single line (up to but not including the \n) from a file.\r
 \r
   If the position upon start is 0, then the Ascii Boolean will be set.  This should be\r
   maintained and not changed for all operations with the same file.\r
+  The function will not return the \r and \n character in buffer. When an empty line is\r
+  read a CHAR_NULL character will be returned in buffer.\r
 \r
-  @param[in]       Handle        FileHandle to read from\r
-  @param[in, out]  Buffer        pointer to buffer to read into\r
-  @param[in, out]  Size          pointer to number of bytes in buffer\r
-  @param[in]       Truncate      if TRUE then allows for truncation of the line to fit.\r
-                                 if FALSE will reset the position to the begining of the\r
-                                 line if the buffer is not large enough.\r
-  @param[in, out]  Ascii         Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r
-\r
-  @retval EFI_SUCCESS           the operation was sucessful.  the line is stored in\r
+  @param[in]       Handle        FileHandle to read from.\r
+  @param[in, out]  Buffer        The pointer to buffer to read into.\r
+  @param[in, out]  Size          The pointer to number of bytes in Buffer.\r
+  @param[in]       Truncate      If the buffer is large enough, this has no effect.\r
+                                 If the buffer is is too small and Truncate is TRUE,\r
+                                 the line will be truncated.\r
+                                 If the buffer is is too small and Truncate is FALSE,\r
+                                 then no read will occur.\r
+\r
+  @param[in, out]  Ascii         Boolean value for indicating whether the file is\r
+                                 Ascii (TRUE) or UCS2 (FALSE).\r
+\r
+  @retval EFI_SUCCESS           The operation was successful.  The line is stored in\r
                                 Buffer.\r
   @retval EFI_INVALID_PARAMETER Handle was NULL.\r
   @retval EFI_INVALID_PARAMETER Size was NULL.\r
-  @retval EFI_BUFFER_TOO_SMALL  Size was not enough space to store the line.\r
-                                Size was updated to minimum space required.\r
+  @retval EFI_BUFFER_TOO_SMALL  Size was not large enough to store the line.\r
+                                Size was updated to the minimum space required.\r
   @sa FileHandleRead\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandleReadLine(\r
-  IN EFI_FILE_HANDLE            Handle,\r
-  IN OUT CHAR16                 *Buffer,\r
-  IN OUT UINTN                  *Size,\r
-  IN BOOLEAN                    Truncate,\r
-  IN OUT BOOLEAN                *Ascii\r
+FileHandleReadLine (\r
+  IN EFI_FILE_HANDLE  Handle,\r
+  IN OUT CHAR16       *Buffer,\r
+  IN OUT UINTN        *Size,\r
+  IN BOOLEAN          Truncate,\r
+  IN OUT BOOLEAN      *Ascii\r
   )\r
 {\r
   EFI_STATUS  Status;\r
   CHAR16      CharBuffer;\r
+  UINT64      FileSize;\r
   UINTN       CharSize;\r
   UINTN       CountSoFar;\r
+  UINTN       CrCount;\r
+  UINTN       OldSize;\r
   UINT64      OriginalFilePosition;\r
 \r
-\r
-  if (Handle == NULL\r
-    ||Size   == NULL\r
-    ||(Buffer==NULL&&*Size!=0)\r
-   ){\r
+  if (  (Handle == NULL)\r
+     || (Size   == NULL)\r
+     || ((Buffer == NULL) && (*Size != 0))\r
+        )\r
+  {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
-  if (Buffer != NULL) {\r
+\r
+  if ((Buffer != NULL) && (*Size != 0)) {\r
     *Buffer = CHAR_NULL;\r
   }\r
-  FileHandleGetPosition(Handle, &OriginalFilePosition);\r
+\r
+  Status = FileHandleGetSize (Handle, &FileSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  } else if (FileSize == 0) {\r
+    *Ascii = TRUE;\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  FileHandleGetPosition (Handle, &OriginalFilePosition);\r
   if (OriginalFilePosition == 0) {\r
-    CharSize = sizeof(CHAR16);\r
-    Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
-    ASSERT_EFI_ERROR(Status);\r
+    CharSize = sizeof (CHAR16);\r
+    Status   = FileHandleRead (Handle, &CharSize, &CharBuffer);\r
+    ASSERT_EFI_ERROR (Status);\r
     if (CharBuffer == gUnicodeFileTag) {\r
       *Ascii = FALSE;\r
     } else {\r
       *Ascii = TRUE;\r
-      FileHandleSetPosition(Handle, OriginalFilePosition);\r
+      FileHandleSetPosition (Handle, OriginalFilePosition);\r
     }\r
   }\r
 \r
-  for (CountSoFar = 0;;CountSoFar++){\r
+  CrCount = 0;\r
+  for (CountSoFar = 0; ; CountSoFar++) {\r
     CharBuffer = 0;\r
     if (*Ascii) {\r
-      CharSize = sizeof(CHAR8);\r
+      CharSize = sizeof (CHAR8);\r
     } else {\r
-      CharSize = sizeof(CHAR16);\r
+      CharSize = sizeof (CHAR16);\r
     }\r
-    Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
-    if (  EFI_ERROR(Status)\r
-       || CharSize == 0\r
-       || (CharBuffer == L'\n' && !(*Ascii))\r
-       || (CharBuffer ==  '\n' && *Ascii)\r
-     ){\r
+\r
+    Status = FileHandleRead (Handle, &CharSize, &CharBuffer);\r
+    if (  EFI_ERROR (Status)\r
+       || (CharSize == 0)\r
+       || ((CharBuffer == L'\n') && !(*Ascii))\r
+       || ((CharBuffer ==  '\n') && *Ascii)\r
+          )\r
+    {\r
       break;\r
+    } else if (\r
+               ((CharBuffer == L'\r') && !(*Ascii)) ||\r
+               ((CharBuffer ==  '\r') && *Ascii)\r
+               )\r
+    {\r
+      CrCount++;\r
+      continue;\r
     }\r
+\r
     //\r
     // if we have space save it...\r
     //\r
-    if ((CountSoFar+1)*sizeof(CHAR16) < *Size){\r
-      ASSERT(Buffer != NULL);\r
-      ((CHAR16*)Buffer)[CountSoFar] = CharBuffer;\r
-      ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
+    if ((CountSoFar+1-CrCount)*sizeof (CHAR16) < *Size) {\r
+      ASSERT (Buffer != NULL);\r
+      ((CHAR16 *)Buffer)[CountSoFar-CrCount]   = CharBuffer;\r
+      ((CHAR16 *)Buffer)[CountSoFar+1-CrCount] = CHAR_NULL;\r
     }\r
   }\r
 \r
   //\r
   // if we ran out of space tell when...\r
   //\r
-  if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
-    *Size = (CountSoFar+1)*sizeof(CHAR16);\r
+  if ((CountSoFar+1-CrCount)*sizeof (CHAR16) > *Size) {\r
+    OldSize = *Size;\r
+    *Size   = (CountSoFar+1-CrCount)*sizeof (CHAR16);\r
     if (!Truncate) {\r
-      FileHandleSetPosition(Handle, OriginalFilePosition);\r
+      if ((Buffer != NULL) && (OldSize != 0)) {\r
+        ZeroMem (Buffer, OldSize);\r
+      }\r
+\r
+      FileHandleSetPosition (Handle, OriginalFilePosition);\r
+      return (EFI_BUFFER_TOO_SMALL);\r
     } else {\r
-      DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
+      DEBUG ((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
+      return (EFI_SUCCESS);\r
     }\r
-    return (EFI_BUFFER_TOO_SMALL);\r
-  }\r
-  while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
-    Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
   }\r
 \r
   return (Status);\r
 }\r
 \r
 /**\r
-  Function to write a line of unicode text to a file.\r
+  Function to write a line of text to a file.\r
+\r
+  If the file is a Unicode file (with UNICODE file tag) then write the unicode\r
+  text.\r
+  If the file is an ASCII file then write the ASCII text.\r
+  If the size of file is zero (without file tag at the beginning) then write\r
+  ASCII text as default.\r
 \r
   @param[in]     Handle         FileHandle to write to.\r
   @param[in]     Buffer         Buffer to write, if NULL the function will\r
@@ -1023,18 +1096,27 @@ FileHandleReadLine(
   @retval  EFI_SUCCESS            The data was written.\r
                                   Buffer is NULL.\r
   @retval  EFI_INVALID_PARAMETER  Handle is NULL.\r
+  @retval  EFI_OUT_OF_RESOURCES   Unable to allocate temporary space for ASCII\r
+                                  string due to out of resources.\r
 \r
   @sa FileHandleWrite\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandleWriteLine(\r
-  IN EFI_FILE_HANDLE Handle,\r
-  IN CHAR16          *Buffer\r
+FileHandleWriteLine (\r
+  IN EFI_FILE_HANDLE  Handle,\r
+  IN CHAR16           *Buffer\r
   )\r
 {\r
-  EFI_STATUS Status;\r
-  UINTN      Size;\r
+  EFI_STATUS  Status;\r
+  CHAR16      CharBuffer;\r
+  UINTN       Size;\r
+  UINTN       Index;\r
+  UINTN       CharSize;\r
+  UINT64      FileSize;\r
+  UINT64      OriginalFilePosition;\r
+  BOOLEAN     Ascii;\r
+  CHAR8       *AsciiBuffer;\r
 \r
   if (Buffer == NULL) {\r
     return (EFI_SUCCESS);\r
@@ -1044,13 +1126,89 @@ FileHandleWriteLine(
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
-  Size = StrSize(Buffer) - sizeof(Buffer[0]);\r
-  Status = FileHandleWrite(Handle, &Size, Buffer);\r
-  if (EFI_ERROR(Status)) {\r
-    return (Status);\r
+  Ascii       = FALSE;\r
+  AsciiBuffer = NULL;\r
+\r
+  Status = FileHandleGetPosition (Handle, &OriginalFilePosition);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = FileHandleSetPosition (Handle, 0);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  Status = FileHandleGetSize (Handle, &FileSize);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (FileSize == 0) {\r
+    Ascii = TRUE;\r
+  } else {\r
+    CharSize = sizeof (CHAR16);\r
+    Status   = FileHandleRead (Handle, &CharSize, &CharBuffer);\r
+    ASSERT_EFI_ERROR (Status);\r
+    if (CharBuffer == gUnicodeFileTag) {\r
+      Ascii = FALSE;\r
+    } else {\r
+      Ascii = TRUE;\r
+    }\r
+  }\r
+\r
+  Status = FileHandleSetPosition (Handle, OriginalFilePosition);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+\r
+  if (Ascii) {\r
+    Size        = (StrSize (Buffer) / sizeof (CHAR16)) * sizeof (CHAR8);\r
+    AsciiBuffer = (CHAR8 *)AllocateZeroPool (Size);\r
+    if (AsciiBuffer == NULL) {\r
+      return EFI_OUT_OF_RESOURCES;\r
+    }\r
+\r
+    UnicodeStrToAsciiStrS (Buffer, AsciiBuffer, Size);\r
+    for (Index = 0; Index < Size; Index++) {\r
+      if ((AsciiBuffer[Index] & BIT7) != 0) {\r
+        FreePool (AsciiBuffer);\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+    }\r
+\r
+    Size   = AsciiStrSize (AsciiBuffer) - sizeof (CHAR8);\r
+    Status = FileHandleWrite (Handle, &Size, AsciiBuffer);\r
+    if (EFI_ERROR (Status)) {\r
+      FreePool (AsciiBuffer);\r
+      return (Status);\r
+    }\r
+\r
+    Size   = AsciiStrSize ("\r\n") - sizeof (CHAR8);\r
+    Status = FileHandleWrite (Handle, &Size, "\r\n");\r
+  } else {\r
+    if (OriginalFilePosition == 0) {\r
+      Status = FileHandleSetPosition (Handle, sizeof (CHAR16));\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
+    }\r
+\r
+    Size   = StrSize (Buffer) - sizeof (CHAR16);\r
+    Status = FileHandleWrite (Handle, &Size, Buffer);\r
+    if (EFI_ERROR (Status)) {\r
+      return (Status);\r
+    }\r
+\r
+    Size   = StrSize (L"\r\n") - sizeof (CHAR16);\r
+    Status = FileHandleWrite (Handle, &Size, L"\r\n");\r
+  }\r
+\r
+  if (AsciiBuffer != NULL) {\r
+    FreePool (AsciiBuffer);\r
   }\r
-  Size = StrSize(L"\r\n") - sizeof(CHAR16);\r
-  return FileHandleWrite(Handle, &Size, L"\r\n");\r
+\r
+  return Status;\r
 }\r
 \r
 /**\r
@@ -1060,22 +1218,22 @@ FileHandleWriteLine(
   @param[in] Format   the format argument (see printlib for format specifier)\r
   @param[in] ...      the variable arguments for the format\r
 \r
-  @retval EFI_SUCCESS the operation was sucessful\r
+  @retval EFI_SUCCESS the operation was successful\r
   @return other       a return value from FileHandleWriteLine\r
 \r
   @sa FileHandleWriteLine\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
-FileHandlePrintLine(\r
+FileHandlePrintLine (\r
   IN EFI_FILE_HANDLE  Handle,\r
   IN CONST CHAR16     *Format,\r
   ...\r
   )\r
 {\r
-  VA_LIST           Marker;\r
-  CHAR16            *Buffer;\r
-  EFI_STATUS        Status;\r
+  VA_LIST     Marker;\r
+  CHAR16      *Buffer;\r
+  EFI_STATUS  Status;\r
 \r
   //\r
   // Get a buffer to print into\r
@@ -1095,12 +1253,12 @@ FileHandlePrintLine(
   //\r
   // Print buffer into file\r
   //\r
-  Status = FileHandleWriteLine(Handle, Buffer);\r
+  Status = FileHandleWriteLine (Handle, Buffer);\r
 \r
   //\r
   // Cleanup and return\r
   //\r
-  FreePool(Buffer);\r
+  FreePool (Buffer);\r
   return (Status);\r
 }\r
 \r
@@ -1118,26 +1276,26 @@ FileHandlePrintLine(
 **/\r
 BOOLEAN\r
 EFIAPI\r
-FileHandleEof(\r
-  IN EFI_FILE_HANDLE Handle\r
+FileHandleEof (\r
+  IN EFI_FILE_HANDLE  Handle\r
   )\r
 {\r
-  EFI_FILE_INFO *Info;\r
-  UINT64        Pos;\r
-  BOOLEAN       RetVal;\r
+  EFI_FILE_INFO  *Info;\r
+  UINT64         Pos;\r
+  BOOLEAN        RetVal;\r
 \r
   if (Handle == NULL) {\r
     return (FALSE);\r
   }\r
 \r
-  FileHandleGetPosition(Handle, &Pos);\r
+  FileHandleGetPosition (Handle, &Pos);\r
   Info = FileHandleGetInfo (Handle);\r
 \r
   if (Info == NULL) {\r
     return (FALSE);\r
   }\r
 \r
-  FileHandleSetPosition(Handle, Pos);\r
+  FileHandleSetPosition (Handle, Pos);\r
 \r
   if (Pos == Info->FileSize) {\r
     RetVal = TRUE;\r