]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/BaseFileHandleLib/BaseFileHandleLib.c
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@9720 6f19259b...
[mirror_edk2.git] / ShellPkg / Library / BaseFileHandleLib / BaseFileHandleLib.c
index 023210e8e0f0dc484a24b830d9f35e2d130d7a72..8c4cc03fc1845c7091607d10986f51a304a28d6b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to EFI_FILE_HANDLE functionality.\r
 \r
-Copyright (c) 2006 - 2009, Intel Corporation\r
+Copyright (c) 2006 - 2009, Intel Corporation<BR>\r
 All rights reserved. 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
@@ -22,6 +22,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/MemoryAllocationLib.h>\r
 #include <Library/BaseLib.h>\r
 #include <Library/BaseMemoryLib.h>\r
+#include <Library/FileHandleLib.h>\r
+#include <Library/PcdLib.h>\r
+#include <Library/PrintLib.h>\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
@@ -46,7 +49,6 @@ FileHandleGetInfo (
   IN EFI_FILE_HANDLE            FileHandle\r
   )\r
 {\r
-  EFI_GUID        FileInfoGuid;\r
   EFI_FILE_INFO   *pFileInfo;\r
   UINTN           FileInfoSize;\r
   EFI_STATUS      Status;\r
@@ -59,11 +61,10 @@ FileHandleGetInfo (
   //\r
   // Get the required size to allocate\r
   //\r
-  FileInfoGuid = gEfiFileInfoGuid;\r
   FileInfoSize = 0;\r
   pFileInfo = NULL;\r
   Status = FileHandle->GetInfo(FileHandle, \r
-                               &FileInfoGuid, \r
+                               &gEfiFileInfoGuid, \r
                                &FileInfoSize, \r
                                pFileInfo);\r
   //\r
@@ -76,7 +77,7 @@ FileHandleGetInfo (
   // now get the information\r
   //\r
   Status = FileHandle->GetInfo(FileHandle, \r
-                               &FileInfoGuid, \r
+                               &gEfiFileInfoGuid, \r
                                &FileInfoSize, \r
                                pFileInfo);\r
   //\r
@@ -114,7 +115,6 @@ FileHandleSetInfo (
   IN CONST EFI_FILE_INFO        *FileInfo\r
   )\r
 {\r
-  EFI_GUID        FileInfoGuid;\r
   \r
   //\r
   // ASSERT if the FileHandle or FileInfo is NULL\r
@@ -122,12 +122,11 @@ FileHandleSetInfo (
   ASSERT (FileHandle != NULL);\r
   ASSERT (FileInfo   != NULL);\r
 \r
-  FileInfoGuid = gEfiFileInfoGuid;\r
   //\r
   // Set the info\r
   //\r
   return (FileHandle->SetInfo(FileHandle, \r
-                              &FileInfoGuid,\r
+                              &gEfiFileInfoGuid,\r
                               (UINTN)FileInfo->Size,\r
                               (EFI_FILE_INFO*)FileInfo));\r
 }  \r
@@ -541,14 +540,6 @@ FileHandleFindNextFile(
   ASSERT (DirHandle != NULL);\r
   ASSERT (Buffer    != NULL);\r
   ASSERT (NoFile    != NULL);\r
-  \r
-  //\r
-  // verify that DirHandle is a directory\r
-  //\r
-  Status = FileHandleIsDirectory(DirHandle);\r
-  if (EFI_ERROR(Status)) {\r
-    return (Status);\r
-  } \r
 \r
   //\r
   // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r
@@ -648,8 +639,8 @@ FileHandleGetSize (
   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] Destination   The String to append onto\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
@@ -668,6 +659,7 @@ StrnCatGrowLeft (
   ){\r
   UINTN DestinationStartSize;\r
   UINTN NewSize;\r
+  UINTN CopySize;\r
 \r
   //\r
   // ASSERTs\r
@@ -697,7 +689,7 @@ StrnCatGrowLeft (
   // Append all of Source?\r
   //\r
   if (Count == 0) {\r
-    Count = StrLen(Source);\r
+    Count = StrSize(Source);\r
   }\r
 \r
   //\r
@@ -705,17 +697,18 @@ StrnCatGrowLeft (
   //\r
   if (CurrentSize != NULL) {\r
     NewSize = *CurrentSize;\r
-    while (NewSize < (DestinationStartSize + (Count*sizeof(CHAR16)))) {\r
-      NewSize += 2 * Count * sizeof(CHAR16);\r
+    while (NewSize < (DestinationStartSize + Count)) {\r
+      NewSize += 2 * Count;\r
     }\r
     *Destination = ReallocatePool(*CurrentSize, NewSize, *Destination);\r
     *CurrentSize = NewSize;\r
   } else {\r
-    *Destination = AllocateZeroPool((Count+1)*sizeof(CHAR16));\r
+    *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r
   }\r
 \r
-  *Destination = CopyMem(*Destination+StrLen(Source), *Destination, StrSize(*Destination));\r
-  *Destination = CopyMem(*Destination, Source, StrLen(Source));\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
@@ -747,7 +740,6 @@ FileHandleGetFileName (
   EFI_FILE_INFO   *FileInfo;\r
 \r
   Size = 0;\r
-  *FullFileName = NULL;\r
 \r
   //\r
   // Check our parameters\r
@@ -756,6 +748,8 @@ FileHandleGetFileName (
     return (EFI_INVALID_PARAMETER);\r
   }\r
 \r
+  *FullFileName = NULL;\r
+\r
   Status = Handle->Open(Handle, &CurrentHandle, L".", EFI_FILE_MODE_READ, 0);\r
   if (!EFI_ERROR(Status)) {\r
     //\r
@@ -771,10 +765,15 @@ FileHandleGetFileName (
         // We got info... do we have a name? if yes preceed the current path with it...\r
         //\r
         if (StrLen (FileInfo->FileName) == 0) {\r
-          *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+          if (*FullFileName == NULL) {\r
+            *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+          }\r
           FreePool(FileInfo);\r
           break;\r
         } else {\r
+          if (*FullFileName == NULL) {\r
+            *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
+          }\r
           *FullFileName = StrnCatGrowLeft(FullFileName, &Size, FileInfo->FileName, 0);\r
           *FullFileName = StrnCatGrowLeft(FullFileName, &Size, L"\\", 0);\r
           FreePool(FileInfo);\r
@@ -798,26 +797,70 @@ FileHandleGetFileName (
   }\r
 \r
   if (EFI_ERROR(Status) && *FullFileName != NULL) {\r
-    FreePool(FullFileName);\r
+    FreePool(*FullFileName);\r
   }\r
 \r
   return (Status);\r
 }\r
 \r
+/**\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
+  maintained and not changed for all operations with the same file.\r
+\r
+  @param[in]      Handle        FileHandle to read from.\r
+  @param[in,out]  Ascii         Boolean value for indicating whether the file is Ascii (TRUE) or UCS2 (FALSE);\r
+\r
+  @return                       The line of text from the file.\r
+\r
+  @sa FileHandleReadLine\r
+**/\r
+CHAR16*\r
+EFIAPI\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
+\r
+  Size = 0;\r
+  RetVal = NULL;\r
+\r
+  Status = FileHandleReadLine(Handle, RetVal, &Size, FALSE, Ascii);\r
+  if (Status == EFI_BUFFER_TOO_SMALL) {\r
+    RetVal = AllocatePool(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
+    RetVal = NULL;\r
+  }\r
+  return (RetVal);\r
+}\r
+\r
 /**\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
+\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
+  @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
                                 Buffer.\r
   @retval EFI_INVALID_PARAMETER Handle was NULL.\r
-  @retval EFI_INVALID_PARAMETER Buffer 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
@@ -827,31 +870,54 @@ EFI_STATUS
 EFIAPI\r
 FileHandleReadLine(\r
   IN EFI_FILE_HANDLE            Handle,\r
-  IN OUT VOID                   *Buffer,\r
+  IN OUT CHAR16                 *Buffer,\r
   IN OUT UINTN                  *Size,\r
-  IN BOOLEAN                    Truncate\r
+  IN BOOLEAN                    Truncate,\r
+  IN OUT BOOLEAN                *Ascii\r
   ){\r
   EFI_STATUS  Status;\r
   CHAR16      CharBuffer;\r
   UINTN       CharSize;\r
   UINTN       CountSoFar;\r
-  UINT64      Position;\r
+  UINT64      OriginalFilePosition;\r
 \r
 \r
   if (Handle == NULL\r
-    ||Buffer == NULL\r
     ||Size   == NULL\r
     ){\r
   return (EFI_INVALID_PARAMETER);\r
   }\r
-  FileHandleGetPosition(Handle, &Position);\r
+  FileHandleGetPosition(Handle, &OriginalFilePosition);\r
+  if (OriginalFilePosition == 0) {\r
+    CharSize = sizeof(CHAR16);\r
+    Status = FileHandleRead(Handle, &CharSize, &CharBuffer);\r
+    ASSERT_EFI_ERROR(Status);\r
+    if (CharBuffer == UnicodeFileTag) {\r
+      *Ascii = FALSE;\r
+    } else {\r
+      *Ascii = TRUE;\r
+      FileHandleSetPosition(Handle, OriginalFilePosition);\r
+    }\r
+  }\r
 \r
   for (CountSoFar = 0;;CountSoFar++){\r
-    CharSize = sizeof(CharBuffer);\r
+    CharBuffer = 0;\r
+    if (*Ascii) {\r
+      CharSize = sizeof(CHAR8);\r
+    } else {\r
+      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 == '\n'\r
+       || (CharBuffer == L'\n' && *Ascii == FALSE)\r
+       || (CharBuffer ==  '\n' && *Ascii != FALSE )\r
       ){\r
       break;\r
     }\r
@@ -859,8 +925,9 @@ FileHandleReadLine(
     // 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] = '\0';\r
+      ((CHAR16*)Buffer)[CountSoFar+1] = CHAR_NULL;\r
     }\r
   }\r
 \r
@@ -870,13 +937,16 @@ FileHandleReadLine(
   if ((CountSoFar+1)*sizeof(CHAR16) > *Size){\r
     *Size = (CountSoFar+1)*sizeof(CHAR16);\r
     if (Truncate == FALSE) {\r
-      FileHandleSetPosition(Handle, Position);\r
+      FileHandleSetPosition(Handle, OriginalFilePosition);\r
     } else {\r
-      DEBUG((DEBUG_WARN, "The line was truncated in ReadLine"));\r
+      DEBUG((DEBUG_WARN, "The line was truncated in FileHandleReadLine"));\r
     }\r
     return (EFI_BUFFER_TOO_SMALL);\r
   }\r
-  *Size = (CountSoFar+1)*sizeof(CHAR16);\r
+  while(Buffer[StrLen(Buffer)-1] == L'\r') {\r
+    Buffer[StrLen(Buffer)-1] = CHAR_NULL;\r
+  }\r
+\r
   return (Status);\r
 }\r
 \r
@@ -917,3 +987,99 @@ FileHandleWriteLine(
   Size = StrLen(L"\r\n");\r
   return FileHandleWrite(Handle, &Size, L"\r\n");\r
 }\r
+\r
+/**\r
+  function to take a formatted argument and print it to a file.\r
+\r
+  @param[in] Handle   the file handle for the file to write to\r
+  @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
+  @return other       a return value from FileHandleWriteLine\r
+\r
+  @sa FileHandleWriteLine\r
+**/\r
+EFI_STATUS\r
+EFIAPI\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
+\r
+  VA_START (Marker, Format);\r
+\r
+  //\r
+  // Get a buffer to print into\r
+  //\r
+  Buffer = AllocateZeroPool (PcdGet16 (PcdShellPrintBufferSize));\r
+  ASSERT (Buffer != NULL);\r
+\r
+  //\r
+  // Print into our buffer\r
+  //\r
+  UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker);\r
+\r
+  //\r
+  // Print buffer into file\r
+  //\r
+  Status = FileHandleWriteLine(Handle, Buffer);\r
+\r
+  //\r
+  // Cleanup and return \r
+  //\r
+  FreePool(Buffer);\r
+  return (Status);\r
+}\r
+\r
+/**\r
+  Function to determine if a FILE_HANDLE is at the end of the file.\r
+\r
+  This will NOT work on directories.\r
+\r
+  If Handle is NULL, then ASSERT.\r
+\r
+  @param[in] Handle     the file handle\r
+\r
+  @retval TRUE          the position is at the end of the file\r
+  @retval FALSE         the position is not at the end of the file\r
+**/\r
+BOOLEAN\r
+EFIAPI\r
+FileHandleEof(\r
+  IN EFI_FILE_HANDLE Handle\r
+  )\r
+{\r
+  EFI_FILE_INFO *Info;\r
+  UINT64        Pos;\r
+  BOOLEAN       RetVal;\r
+\r
+  //\r
+  // ASSERT if Handle is NULL\r
+  //\r
+  ASSERT(Handle != NULL);\r
+  \r
+  FileHandleGetPosition(Handle, &Pos);\r
+  Info = FileHandleGetInfo (Handle);\r
+  ASSERT(Info != NULL);\r
+  FileHandleSetPosition(Handle, Pos);\r
+  \r
+  if (Info == NULL) {\r
+    return (FALSE);\r
+  } \r
+\r
+  if (Pos == Info->FileSize) {\r
+    RetVal = TRUE;\r
+  } else {\r
+    RetVal = FALSE;\r
+  }\r
+\r
+  FreePool (Info);\r
+\r
+  return (RetVal);\r
+}
\ No newline at end of file