]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
udk2010.up2.shell initial release.
[mirror_edk2.git] / ShellPkg / Library / BaseFileHandleLib / BaseFileHandleLib.c
index fd19452cbca14d5b069a931fccbadb9234c41092..bd3a4a7b52361cfb82926aeae9fdb5c6b772cde9 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to EFI_FILE_HANDLE functionality.\r
 \r
-  Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2010, 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
 #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
+  This function will retrieve the information about the file for the handle\r
   specified and store it in allocated pool memory.\r
 \r
-  This function allocates a buffer to store the file's information. It is the \r
+  This function allocates a buffer to store the file's information. It is the\r
   caller's responsibility to free the buffer\r
 \r
-  @param  FileHandle  The file handle of the file for which information is \r
+  @param  FileHandle  The file handle of the file for which information is\r
   being requested.\r
 \r
   @retval NULL information could not be retrieved.\r
@@ -49,7 +49,7 @@ FileHandleGetInfo (
   IN EFI_FILE_HANDLE            FileHandle\r
   )\r
 {\r
-  EFI_FILE_INFO   *pFileInfo;\r
+  EFI_FILE_INFO   *FileInfo;\r
   UINTN           FileInfoSize;\r
   EFI_STATUS      Status;\r
 \r
@@ -62,51 +62,52 @@ FileHandleGetInfo (
   // Get the required size to allocate\r
   //\r
   FileInfoSize = 0;\r
-  pFileInfo = NULL;\r
-  Status = FileHandle->GetInfo(FileHandle, \r
-                               &gEfiFileInfoGuid, \r
-                               &FileInfoSize, \r
-                               pFileInfo);\r
+  FileInfo = NULL;\r
+  Status = FileHandle->GetInfo(FileHandle,\r
+                               &gEfiFileInfoGuid,\r
+                               &FileInfoSize,\r
+                               FileInfo);\r
   //\r
   // error is expected.  getting size to allocate\r
   //\r
   ASSERT (Status == EFI_BUFFER_TOO_SMALL);\r
-  pFileInfo = AllocateZeroPool(FileInfoSize);\r
-  ASSERT (pFileInfo != NULL);\r
+  FileInfo = AllocateZeroPool(FileInfoSize);\r
+  ASSERT (FileInfo != NULL);\r
   //\r
   // now get the information\r
   //\r
-  Status = FileHandle->GetInfo(FileHandle, \r
-                               &gEfiFileInfoGuid, \r
-                               &FileInfoSize, \r
-                               pFileInfo);\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)) {\r
-    FreePool(pFileInfo);\r
+    FreePool(FileInfo);\r
     return NULL;\r
   }\r
-  return (pFileInfo);\r
+  return (FileInfo);\r
 }\r
 \r
 /**\r
-  This function will set the information about the file for the opened handle \r
+  This function sets the information about the file for the opened handle\r
   specified.\r
 \r
-  @param  FileHandle            The file handle of the file for which information \r
-  is being set\r
+  @param[in]  FileHandle        The file handle of the file for which information\r
+                                is being set.\r
 \r
-  @param  FileInfo              The infotmation to set.\r
+  @param[in]  FileInfo          The information to set.\r
 \r
-  @retval EFI_SUCCESS          The information was set.\r
-  @retval EFI_UNSUPPORTED The InformationType is not known.\r
-  @retval EFI_NO_MEDIA         The device has no medium.\r
-  @retval EFI_DEVICE_ERROR     The device reported an error.\r
+  @retval EFI_SUCCESS                  The information was set.\r
+  @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.\r
+  @retval EFI_UNSUPPORTED       The FileHandle does not support FileInfo.\r
+  @retval EFI_NO_MEDIA               The device has no medium.\r
+  @retval EFI_DEVICE_ERROR         The device reported an error.\r
   @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
-  @retval EFI_WRITE_PROTECTED  The file or medium is write protected.\r
-  @retval EFI_ACCESS_DENIED    The file was opened read only.\r
-  @retval EFI_VOLUME_FULL      The volume is full.\r
+  @retval EFI_WRITE_PROTECTED    The file or medium is write protected.\r
+  @retval EFI_ACCESS_DENIED     The file was opened read only.\r
+  @retval EFI_VOLUME_FULL       The volume is full.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -115,7 +116,7 @@ FileHandleSetInfo (
   IN CONST EFI_FILE_INFO        *FileInfo\r
   )\r
 {\r
-  \r
+\r
   //\r
   // ASSERT if the FileHandle or FileInfo is NULL\r
   //\r
@@ -125,38 +126,38 @@ FileHandleSetInfo (
   //\r
   // Set the info\r
   //\r
-  return (FileHandle->SetInfo(FileHandle, \r
+  return (FileHandle->SetInfo(FileHandle,\r
                               &gEfiFileInfoGuid,\r
                               (UINTN)FileInfo->Size,\r
                               (EFI_FILE_INFO*)FileInfo));\r
-}  \r
+}\r
 \r
 /**\r
   This function reads information from an opened file.\r
 \r
-  If FileHandle is not a directory, the function reads the requested number of \r
-  bytes from the file at the file's current position and returns them in Buffer. \r
+  If FileHandle is not a directory, the function reads the requested number of\r
+  bytes from the file at the file's current position and returns them in Buffer.\r
   If the read goes beyond the end of the file, the read length is truncated to the\r
-  end of the file. The file's current position is increased by the number of bytes \r
-  returned.  If FileHandle is a directory, the function reads the directory entry \r
-  at the file's current position and returns the entry in Buffer. If the Buffer \r
-  is not large enough to hold the current directory entry, then \r
-  EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated. \r
-  BufferSize is set to be the size of the buffer needed to read the entry. On \r
-  success, the current position is updated to the next directory entry. If there \r
-  are no more directory entries, the read returns a zero-length buffer. \r
+  end of the file. The file's current position is increased by the number of bytes\r
+  returned.  If FileHandle is a directory, the function reads the directory entry\r
+  at the file's current position and returns the entry in Buffer. If the Buffer\r
+  is not large enough to hold the current directory entry, then\r
+  EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.\r
+  BufferSize is set to be the size of the buffer needed to read the entry. On\r
+  success, the current position is updated to the next directory entry. If there\r
+  are no more directory entries, the read returns a zero-length buffer.\r
   EFI_FILE_INFO is the structure returned as the directory entry.\r
 \r
   @param FileHandle             the opened file handle\r
-  @param BufferSize             on input the size of buffer in bytes.  on return \r
+  @param BufferSize             on input the size of buffer in bytes.  on return\r
                                 the number of bytes written.\r
   @param Buffer                 the buffer to put read data into.\r
 \r
-  @retval EFI_SUCCESS            Data was read.\r
-  @retval EFI_NO_MEDIA         The device has no media.\r
-  @retval EFI_DEVICE_ERROR     The device reported an error.\r
-  @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.\r
-  @retval EFI_BUFFER_TO_SMALL  Buffer is too small. ReadSize contains required \r
+  @retval EFI_SUCCESS           Data was read.\r
+  @retval EFI_NO_MEDIA          The device has no media.\r
+  @retval EFI_DEVICE_ERROR      The device reported an error.\r
+  @retval EFI_VOLUME_CORRUPTED  The file system structures are corrupted.\r
+  @retval EFI_BUFFER_TO_SMALL   Buffer is too small. ReadSize contains required\r
                                 size.\r
 \r
 **/\r
@@ -183,11 +184,11 @@ FileHandleRead(
 /**\r
   Write data to a file.\r
 \r
-  This function writes the specified number of bytes to the file at the current \r
-  file position. The current file position is advanced the actual number of bytes \r
-  written, which is returned in BufferSize. Partial writes only occur when there \r
-  has been a data error during the write attempt (such as "volume space full"). \r
-  The file is automatically grown to hold the data if required. Direct writes to \r
+  This function writes the specified number of bytes to the file at the current\r
+  file position. The current file position is advanced the actual number of bytes\r
+  written, which is returned in BufferSize. Partial writes only occur when there\r
+  has been a data error during the write attempt (such as "volume space full").\r
+  The file is automatically grown to hold the data if required. Direct writes to\r
   opened directories are not supported.\r
 \r
   @param FileHandle           The opened file for writing\r
@@ -222,11 +223,11 @@ FileHandleWrite(
   return (FileHandle->Write(FileHandle, BufferSize, Buffer));\r
 }\r
 \r
-/** \r
+/**\r
   Close an open file handle.\r
 \r
-  This function closes a specified file handle. All "dirty" cached file data is \r
-  flushed to the device, and the file is closed. In all cases the handle is \r
+  This function closes a specified file handle. All "dirty" cached file data is\r
+  flushed to the device, and the file is closed. In all cases the handle is\r
   closed.\r
 \r
 @param FileHandle               the file handle to close.\r
@@ -255,13 +256,13 @@ FileHandleClose (
   Delete a file and close the handle\r
 \r
   This function closes and deletes a file. In all cases the file handle is closed.\r
-  If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is \r
+  If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is\r
   returned, but the handle is still closed.\r
 \r
   @param FileHandle             the file handle to delete\r
 \r
   @retval EFI_SUCCESS           the file was closed sucessfully\r
-  @retval EFI_WARN_DELETE_FAILURE the handle was closed, but the file was not \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
 **/\r
@@ -286,19 +287,19 @@ FileHandleDelete (
 /**\r
   Set the current position in a file.\r
 \r
-  This function sets the current file position for the handle to the position \r
+  This function sets the current file position for the handle to the position\r
   supplied. With the exception of seeking to position 0xFFFFFFFFFFFFFFFF, only\r
-  absolute positioning is supported, and seeking past the end of the file is \r
-  allowed (a subsequent write would grow the file). Seeking to position \r
+  absolute positioning is supported, and seeking past the end of the file is\r
+  allowed (a subsequent write would grow the file). Seeking to position\r
   0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.\r
-  If FileHandle is a directory, the only position that may be set is zero. This \r
+  If FileHandle is a directory, the only position that may be set is zero. This\r
   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
 \r
   @retval EFI_SUCCESS           Operation completed sucessfully.\r
-  @retval EFI_UNSUPPORTED       the seek request for non-zero is not valid on \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
 **/\r
@@ -319,11 +320,11 @@ FileHandleSetPosition (
   return (FileHandle->SetPosition(FileHandle, Position));\r
 }\r
 \r
-/** \r
+/**\r
   Gets a file's current position\r
 \r
-  This function retrieves the current file position for the file handle. For \r
-  directories, the current file position has no meaning outside of the file \r
+  This function retrieves the current file position for the file handle. For\r
+  directories, the current file position has no meaning outside of the file\r
   system driver and as such the operation is not supported. An error is returned\r
   if FileHandle is a directory.\r
 \r
@@ -341,6 +342,9 @@ FileHandleGetPosition (
   OUT UINT64                    *Position\r
   )\r
 {\r
+  if (Position == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
   //\r
   // ASSERT if FileHandle is NULL\r
   //\r
@@ -352,7 +356,7 @@ FileHandleGetPosition (
 }\r
 /**\r
   Flushes data on a file\r
-  \r
+\r
   This function flushes all modified data associated with a file to a device.\r
 \r
   @param FileHandle             The file handle on which to flush data\r
@@ -406,12 +410,12 @@ FileHandleIsDirectory (
   // ASSERT if DirHandle is NULL\r
   //\r
   ASSERT(DirHandle != NULL);\r
-  \r
+\r
   //\r
   // get the file information for DirHandle\r
   //\r
   DirInfo = FileHandleGetInfo (DirHandle);\r
-  \r
+\r
   //\r
   // Parse DirInfo\r
   //\r
@@ -420,7 +424,7 @@ FileHandleIsDirectory (
     // We got nothing...\r
     //\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
@@ -438,8 +442,8 @@ FileHandleIsDirectory (
 /**\r
   Retrieves the first file from a directory\r
 \r
-  This function opens a directory and gets the first file's info in the \r
-  directory. Caller can use FileHandleFindNextFile() to get other files.  When \r
+  This function opens a directory and gets the first file's info in the\r
+  directory. Caller can use FileHandleFindNextFile() to get other files.  When\r
   complete the caller is responsible for calling FreePool() on Buffer.\r
 \r
   @param DirHandle              The file handle of the directory to search\r
@@ -475,15 +479,15 @@ FileHandleFindFirstFile (
   Status = FileHandleIsDirectory(DirHandle);\r
   if (EFI_ERROR(Status)) {\r
     return (Status);\r
-  } \r
+  }\r
 \r
   //\r
-  // reset to the begining of the directory \r
+  // reset to the begining of the directory\r
   //\r
   Status = FileHandleSetPosition(DirHandle, 0);\r
   if (EFI_ERROR(Status)) {\r
     return (Status);\r
-  } \r
+  }\r
 \r
   //\r
   // Allocate a buffer sized to struct size + enough for the string at the end\r
@@ -507,12 +511,12 @@ FileHandleFindFirstFile (
 /**\r
   Retrieves the next file in a directory.\r
 \r
-  To use this function, caller must call the FileHandleFindFirstFile() to get the \r
-  first file, and then use this function get other files. This function can be \r
-  called for several times to get each file's information in the directory. If \r
-  the call of FileHandleFindNextFile() got the last file in the directory, the next \r
-  call of this function has no file to get. *NoFile will be set to TRUE and the \r
-  Buffer memory will be automatically freed. \r
+  To use this function, caller must call the FileHandleFindFirstFile() to get the\r
+  first file, and then use this function get other files. This function can be\r
+  called for several times to get each file's information in the directory. If\r
+  the call of FileHandleFindNextFile() got the last file in the directory, the next\r
+  call of this function has no file to get. *NoFile will be set to TRUE and the\r
+  Buffer memory will be automatically freed.\r
 \r
   @param DirHandle              the file handle of the directory\r
   @param Buffer                                    pointer to buffer for file's information\r
@@ -565,13 +569,14 @@ FileHandleFindNextFile(
 \r
   return (EFI_SUCCESS);\r
 }\r
+\r
 /**\r
   Retrieve the size of a file.\r
 \r
   if FileHandle is NULL then ASSERT()\r
   if Size is NULL then ASSERT()\r
 \r
-  This function extracts the file size info from the FileHandle's EFI_FILE_INFO \r
+  This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
   data.\r
 \r
   @param FileHandle             file handle from which size is retrieved\r
@@ -594,7 +599,7 @@ FileHandleGetSize (
   //\r
   ASSERT (FileHandle != NULL);\r
   ASSERT (Size != NULL);\r
-  \r
+\r
   //\r
   // get the FileInfo structure\r
   //\r
@@ -607,7 +612,7 @@ FileHandleGetSize (
   // Assign the Size pointer to the correct value\r
   //\r
   *Size = FileInfo->FileSize;\r
-  \r
+\r
   //\r
   // free the FileInfo memory\r
   //\r
@@ -616,47 +621,98 @@ FileHandleGetSize (
   return (EFI_SUCCESS);\r
 }\r
 \r
+/**\r
+  Set the size of a file.\r
+\r
+  If FileHandle is NULL then ASSERT().\r
+\r
+  This function changes the file size info from the FileHandle's EFI_FILE_INFO\r
+  data.\r
+\r
+  @param FileHandle             File handle whose size is to be changed.\r
+  @param Size                   New size.\r
+\r
+  @retval EFI_SUCCESS           operation was completed sucessfully.\r
+  @retval EFI_DEVICE_ERROR      cannot access the file.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+FileHandleSetSize (\r
+  IN EFI_FILE_HANDLE            FileHandle,\r
+  IN UINT64                     Size\r
+  )\r
+{\r
+  EFI_FILE_INFO                 *FileInfo;\r
+  EFI_STATUS                    Status;\r
+\r
+  //\r
+  // ASSERT for FileHandle or Size being NULL\r
+  //\r
+  ASSERT (FileHandle != NULL);\r
+\r
+  //\r
+  // get the FileInfo structure\r
+  //\r
+  FileInfo = FileHandleGetInfo(FileHandle);\r
+  if (FileInfo == NULL) {\r
+    return (EFI_DEVICE_ERROR);\r
+  }\r
+\r
+  //\r
+  // Assign the FileSize pointer to the new value\r
+  //\r
+  FileInfo->FileSize = Size;\r
+\r
+  Status = FileHandleSetInfo(FileHandle, FileInfo);\r
+  //\r
+  // free the FileInfo memory\r
+  //\r
+  FreePool(FileInfo);\r
+\r
+  return (Status);\r
+}\r
 \r
 /**\r
-  Safely append (on the left) with automatic string resizing given length of Destination and \r
+  Safely append (on the left) with automatic string resizing given length of Destination and\r
   desired length of copy from Source.\r
 \r
-  append the first D characters of Source to the end of Destination, where D is \r
-  the lesser of Count and the StrLen() of Source. If appending those D characters \r
-  will fit within Destination (whose Size is given as CurrentSize) and \r
-  still leave room for a NULL terminator, then those characters are appended, \r
-  starting at the original terminating NULL of Destination, and a new terminating \r
+  append the first D characters of Source to the end of Destination, where D is\r
+  the lesser of Count and the StrLen() of Source. If appending those D characters\r
+  will fit within Destination (whose Size is given as CurrentSize) and\r
+  still leave room for a NULL terminator, then those characters are appended,\r
+  starting at the original terminating NULL of Destination, and a new terminating\r
   NULL is appended.\r
 \r
   If appending D characters onto Destination will result in a overflow of the size\r
   given in CurrentSize the string will be grown such that the copy can be performed\r
   and CurrentSize will be updated to the new size.\r
 \r
-  If Source is NULL, there is nothing to append, just return the current buffer in \r
+  If Source is NULL, there is nothing to append, just return the current buffer in\r
   Destination.\r
 \r
   if Destination is NULL, then ASSERT()\r
-  if Destination's current length (including NULL terminator) is already more then \r
+  if Destination's current length (including NULL terminator) is already more then\r
   CurrentSize, then ASSERT()\r
 \r
   @param[in,out] Destination   The String to append onto\r
-  @param[in,out] CurrentSize   on call the number of bytes in Destination.  On \r
+  @param[in,out] CurrentSize   on call the number of bytes in Destination.  On\r
                                 return possibly the new size (still in bytes).  if NULL\r
                                 then allocate whatever is needed.\r
   @param[in]      Source        The String to append from\r
-  @param[in]      Count         Maximum number of characters to append.  if 0 then \r
+  @param[in]      Count         Maximum number of characters to append.  if 0 then\r
                                 all are appended.\r
 \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
-  ){\r
+  )\r
+{\r
   UINTN DestinationStartSize;\r
   UINTN NewSize;\r
   UINTN CopySize;\r
@@ -713,13 +769,13 @@ StrnCatGrowLeft (
 }\r
 \r
 /**\r
-  Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the \r
+  Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the\r
   directory 'stack'.\r
 \r
   if Handle is NULL, return EFI_INVALID_PARAMETER\r
 \r
   @param[in] Handle             Handle to the Directory or File to create path to.\r
-  @param[out] FullFileName      pointer to pointer to generated full file name.  It \r
+  @param[out] FullFileName      pointer to pointer to generated full file name.  It\r
                                 is the responsibility of the caller to free this memory\r
                                 with a call to FreePool().\r
   @retval EFI_SUCCESS           the operation was sucessful and the FullFileName is valid.\r
@@ -732,7 +788,8 @@ EFIAPI
 FileHandleGetFileName (\r
   IN CONST EFI_FILE_HANDLE      Handle,\r
   OUT CHAR16                    **FullFileName\r
-  ){\r
+  )\r
+{\r
   EFI_STATUS      Status;\r
   UINTN           Size;\r
   EFI_FILE_HANDLE CurrentHandle;\r
@@ -749,6 +806,7 @@ FileHandleGetFileName (
   }\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
@@ -766,14 +824,17 @@ FileHandleGetFileName (
         //\r
         if (StrLen (FileInfo->FileName) == 0) {\r
           if (*FullFileName == NULL) {\r
+            ASSERT((*FullFileName == NULL && Size == 0) || (*FullFileName != NULL));\r
             *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\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
           }\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
@@ -790,6 +851,10 @@ FileHandleGetFileName (
       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
   }\r
 \r
   if (CurrentHandle != NULL) {\r
@@ -804,10 +869,10 @@ FileHandleGetFileName (
 }\r
 \r
 /**\r
-  Function to read a single line from a file. The \n is not included in the returned \r
+  Function to read a single line from a file. The \n is not included in the returned\r
   buffer.  The returned buffer must be callee freed.\r
 \r
-  If the position upon start is 0, then the Ascii Boolean will be set.  This should be \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
 \r
   @param[in]      Handle        FileHandle to read from.\r
@@ -845,24 +910,24 @@ FileHandleReturnLine(
 }\r
 \r
 /**\r
-  Function to read a single line (up to but not including the \n) from a file.\r
+  Function to read a single line (up to but not including the \n) from a EFI_FILE_HANDLE.\r
 \r
-  If the position upon start is 0, then the Ascii Boolean will be set.  This should be \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
 \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
+                                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
+  @retval EFI_SUCCESS           the operation was sucessful.  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
+  @retval EFI_BUFFER_TOO_SMALL  Size was not enough space to store the line.\r
                                 Size was updated to minimum space required.\r
   @sa FileHandleRead\r
 **/\r
@@ -874,7 +939,8 @@ FileHandleReadLine(
   IN OUT UINTN                  *Size,\r
   IN BOOLEAN                    Truncate,\r
   IN OUT BOOLEAN                *Ascii\r
-  ){\r
+  )\r
+{\r
   EFI_STATUS  Status;\r
   CHAR16      CharBuffer;\r
   UINTN       CharSize;\r
@@ -884,8 +950,13 @@ FileHandleReadLine(
 \r
   if (Handle == NULL\r
     ||Size   == NULL\r
-    ){\r
-  return (EFI_INVALID_PARAMETER);\r
+   ){\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+  if (Buffer == NULL) {\r
+    ASSERT(*Size == 0);\r
+  } else {\r
+    *Buffer = CHAR_NULL;\r
   }\r
   FileHandleGetPosition(Handle, &OriginalFilePosition);\r
   if (OriginalFilePosition == 0) {\r
@@ -908,17 +979,11 @@ FileHandleReadLine(
       CharSize = sizeof(CHAR16);\r
     }\r
     Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
-    if (OriginalFilePosition == 0 && *Ascii == FALSE && CountSoFar == 0) {\r
-      //\r
-      // we need to skip the unicode tag\r
-      //\r
-      continue;\r
-    }\r
-    if (  EFI_ERROR(Status) \r
-       || CharSize == 0 \r
-       || (CharBuffer == L'\n' && *Ascii == FALSE)\r
-       || (CharBuffer ==  '\n' && *Ascii != FALSE )\r
-      ){\r
+    if (  EFI_ERROR(Status)\r
+       || CharSize == 0\r
+       || (CharBuffer == L'\n' && !(*Ascii))\r
+       || (CharBuffer ==  '\n' && *Ascii)\r
+     ){\r
       break;\r
     }\r
     //\r
@@ -936,7 +1001,7 @@ FileHandleReadLine(
   //\r
   if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
     *Size = (CountSoFar+1)*sizeof(CHAR16);\r
-    if (Truncate == FALSE) {\r
+    if (!Truncate) {\r
       FileHandleSetPosition(Handle, OriginalFilePosition);\r
     } else {\r
       DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
@@ -969,7 +1034,8 @@ EFIAPI
 FileHandleWriteLine(\r
   IN EFI_FILE_HANDLE Handle,\r
   IN CHAR16          *Buffer\r
-  ){\r
+  )\r
+{\r
   EFI_STATUS Status;\r
   UINTN      Size;\r
 \r
@@ -1031,7 +1097,7 @@ FileHandlePrintLine(
   Status = FileHandleWriteLine(Handle, Buffer);\r
 \r
   //\r
-  // Cleanup and return \r
+  // Cleanup and return\r
   //\r
   FreePool(Buffer);\r
   return (Status);\r
@@ -1063,15 +1129,15 @@ FileHandleEof(
   // ASSERT if Handle is NULL\r
   //\r
   ASSERT(Handle != NULL);\r
-  \r
+\r
   FileHandleGetPosition(Handle, &Pos);\r
   Info = FileHandleGetInfo (Handle);\r
   ASSERT(Info != NULL);\r
   FileHandleSetPosition(Handle, Pos);\r
-  \r
+\r
   if (Info == NULL) {\r
     return (FALSE);\r
-  } \r
+  }\r
 \r
   if (Pos == Info->FileSize) {\r
     RetVal = TRUE;\r