]> git.proxmox.com Git - mirror_edk2.git/blobdiff - ShellPkg/Library/UefiFileHandleLib/UefiFileHandleLib.c
MdeModulePkg/PartitionDxe: Fixed El Torito support when the medium is not a CDROM
[mirror_edk2.git] / ShellPkg / Library / UefiFileHandleLib / UefiFileHandleLib.c
index a4820c9970e6db7f8c95db0d3700a1b0c9f2a424..5af75c5adb3996861b54aaf3b28ab6095d113400 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Provides interface to EFI_FILE_HANDLE functionality.\r
 \r
-  Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved. <BR>\r
+  Copyright (c) 2006 - 2014, 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
@@ -84,9 +84,9 @@ FileHandleGetInfo (
     //\r
     // if we got an error free the memory and return NULL\r
     //\r
-    if (EFI_ERROR(Status)) {\r
+    if (EFI_ERROR(Status) && (FileInfo != NULL)) {\r
       FreePool(FileInfo);\r
-      return NULL;\r
+      FileInfo = NULL;\r
     }\r
   }\r
   return (FileInfo);\r
@@ -101,29 +101,27 @@ FileHandleGetInfo (
 \r
   @param[in]  FileInfo          The information to set.\r
 \r
-  @retval EFI_SUCCESS                  The information was set.\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_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
 **/\r
 EFI_STATUS\r
 EFIAPI\r
 FileHandleSetInfo (\r
-  IN EFI_FILE_HANDLE           FileHandle,\r
+  IN EFI_FILE_HANDLE            FileHandle,\r
   IN CONST EFI_FILE_INFO        *FileInfo\r
   )\r
 {\r
 \r
-  //\r
-  // ASSERT if the FileHandle or FileInfo is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
-  ASSERT (FileInfo   != NULL);\r
+  if (FileHandle == NULL || FileInfo == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // Set the info\r
@@ -171,10 +169,9 @@ FileHandleRead(
   OUT VOID                      *Buffer\r
   )\r
 {\r
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // Perform the read based on EFI_FILE_PROTOCOL\r
@@ -198,14 +195,14 @@ FileHandleRead(
                               the number of bytes written.\r
   @param Buffer               the buffer containing data to write is stored.\r
 \r
- @retval EFI_SUCCESS           Data was written.\r
- @retval EFI_UNSUPPORTED           Writes to an open directory are not supported.\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_WRITE_PROTECTED   The device is write-protected.\r
- @retval EFI_ACCESS_DENIED       The file was open for read only.\r
- @retval EFI_VOLUME_FULL           The volume is full.\r
+ @retval EFI_SUCCESS          Data was written.\r
+ @retval EFI_UNSUPPORTED      Writes to an open directory are not supported.\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_WRITE_PROTECTED  The device is write-protected.\r
+ @retval EFI_ACCESS_DENIED    The file was open for read only.\r
+ @retval EFI_VOLUME_FULL      The volume is full.\r
 **/\r
 EFI_STATUS\r
 EFIAPI\r
@@ -215,10 +212,10 @@ FileHandleWrite(
   IN VOID                       *Buffer\r
   )\r
 {\r
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // Perform the write based on EFI_FILE_PROTOCOL\r
   //\r
@@ -243,10 +240,11 @@ FileHandleClose (
   )\r
 {\r
   EFI_STATUS Status;\r
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // Perform the Close based on EFI_FILE_PROTOCOL\r
   //\r
@@ -266,19 +264,20 @@ FileHandleClose (
   @retval EFI_SUCCESS           the file was closed sucessfully\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
+  @retval INVALID_PARAMETER     One of the parameters has an invalid value.\r
 **/\r
 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
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // Perform the Delete based on EFI_FILE_PROTOCOL\r
   //\r
@@ -308,14 +307,14 @@ 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
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // Perform the SetPosition based on EFI_FILE_PROTOCOL\r
   //\r
@@ -344,13 +343,10 @@ FileHandleGetPosition (
   OUT UINT64                    *Position\r
   )\r
 {\r
-  if (Position == NULL) {\r
+  if (Position == NULL || FileHandle == NULL) {\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+\r
   //\r
   // Perform the GetPosition based on EFI_FILE_PROTOCOL\r
   //\r
@@ -376,10 +372,10 @@ FileHandleFlush (
   IN EFI_FILE_HANDLE            FileHandle\r
   )\r
 {\r
-  //\r
-  // ASSERT if FileHandle is NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
+\r
   //\r
   // Perform the Flush based on EFI_FILE_PROTOCOL\r
   //\r
@@ -389,7 +385,7 @@ FileHandleFlush (
 /**\r
   function to determine if a given handle is a directory handle\r
 \r
-  if DirHandle is NULL then ASSERT()\r
+  if DirHandle is NULL then return error\r
 \r
   open the file information on the DirHandle and verify that the Attribute\r
   includes EFI_FILE_DIRECTORY bit set.\r
@@ -408,10 +404,9 @@ FileHandleIsDirectory (
 {\r
   EFI_FILE_INFO *DirInfo;\r
 \r
-  //\r
-  // ASSERT if DirHandle is NULL\r
-  //\r
-  ASSERT(DirHandle != NULL);\r
+  if (DirHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // get the file information for DirHandle\r
@@ -441,15 +436,19 @@ FileHandleIsDirectory (
   return (EFI_SUCCESS);\r
 }\r
 \r
-/**\r
-  Retrieves the first file from a directory\r
+/** Retrieve first entry 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
-  complete the caller is responsible for calling FreePool() on Buffer.\r
+  This function takes an open directory handle and gets information from the\r
+  first entry in the directory.  A buffer is allocated to contain\r
+  the information and a pointer to the buffer is returned in *Buffer.  The\r
+  caller can use FileHandleFindNextFile() to get subsequent directory entries.\r
 \r
-  @param DirHandle              The file handle of the directory to search\r
-  @param Buffer                 Pointer to buffer for file's information\r
+  The buffer will be freed by FileHandleFindNextFile() when the last directory\r
+  entry is read.  Otherwise, the caller must free the buffer, using FreePool,\r
+  when finished with it.\r
+\r
+  @param[in]  DirHandle         The file handle of the directory to search.\r
+  @param[out] Buffer            The pointer to pointer to buffer for file's information.\r
 \r
   @retval EFI_SUCCESS           Found the first file.\r
   @retval EFI_NOT_FOUND         Cannot find the directory.\r
@@ -505,26 +504,29 @@ FileHandleFindFirstFile (
   //\r
   Status = FileHandleRead (DirHandle, &BufferSize, *Buffer);\r
   ASSERT(Status != EFI_BUFFER_TOO_SMALL);\r
-  if (EFI_ERROR(Status)) {\r
+  if (EFI_ERROR(Status) || BufferSize == 0) {\r
     FreePool(*Buffer);\r
     *Buffer = NULL;\r
+    if (BufferSize == 0) {\r
+      return (EFI_NOT_FOUND);\r
+    }\r
     return (Status);\r
   }\r
   return (EFI_SUCCESS);\r
 }\r
-/**\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
+/** Retrieve next entries from a directory.\r
+\r
+  To use this function, the caller must first call the FileHandleFindFirstFile()\r
+  function to get the first directory entry.  Subsequent directory entries are\r
+  retrieved by using the FileHandleFindNextFile() function.  This function can\r
+  be called several times to get each entry from the directory.  If the call of\r
+  FileHandleFindNextFile() retrieved the last directory entry, the next call of\r
+  this function will set *NoFile to TRUE and free the buffer.\r
 \r
-  @param DirHandle              the file handle of the directory\r
-  @param Buffer                                    pointer to buffer for file's information\r
-  @param NoFile                                    pointer to boolean when last file is found\r
+  @param[in]  DirHandle         The file handle of the directory.\r
+  @param[out] Buffer            The pointer to buffer for file's information.\r
+  @param[out] NoFile            The pointer to boolean when last file is found.\r
 \r
   @retval EFI_SUCCESS           Found the next file, or reached last file\r
   @retval EFI_NO_MEDIA          The device has no media.\r
@@ -534,20 +536,17 @@ FileHandleFindFirstFile (
 EFI_STATUS\r
 EFIAPI\r
 FileHandleFindNextFile(\r
-  IN EFI_FILE_HANDLE             DirHandle,\r
-  OUT EFI_FILE_INFO              *Buffer,\r
-  OUT BOOLEAN                    *NoFile\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
 \r
-  //\r
-  // ASSERTs for DirHandle or Buffer or NoFile poitners being NULL\r
-  //\r
-  ASSERT (DirHandle != NULL);\r
-  ASSERT (Buffer    != NULL);\r
-  ASSERT (NoFile    != NULL);\r
+  if (DirHandle == NULL || Buffer == NULL || NoFile == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // This BufferSize MUST stay equal to the originally allocated one in GetFirstFile\r
@@ -577,8 +576,8 @@ FileHandleFindNextFile(
 /**\r
   Retrieve the size of a file.\r
 \r
-  if FileHandle is NULL then ASSERT()\r
-  if Size is NULL then ASSERT()\r
+  if FileHandle is NULL then return error\r
+  if Size is NULL then return error\r
 \r
   This function extracts the file size info from the FileHandle's EFI_FILE_INFO\r
   data.\r
@@ -598,11 +597,9 @@ FileHandleGetSize (
 {\r
   EFI_FILE_INFO                 *FileInfo;\r
 \r
-  //\r
-  // ASSERT for FileHandle or Size being NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
-  ASSERT (Size != NULL);\r
+  if (FileHandle == NULL || Size == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // get the FileInfo structure\r
@@ -628,7 +625,7 @@ FileHandleGetSize (
 /**\r
   Set the size of a file.\r
 \r
-  If FileHandle is NULL then ASSERT().\r
+  If FileHandle is NULL then return error.\r
 \r
   This function changes the file size info from the FileHandle's EFI_FILE_INFO\r
   data.\r
@@ -649,10 +646,9 @@ FileHandleSetSize (
   EFI_FILE_INFO                 *FileInfo;\r
   EFI_STATUS                    Status;\r
 \r
-  //\r
-  // ASSERT for FileHandle or Size being NULL\r
-  //\r
-  ASSERT (FileHandle != NULL);\r
+  if (FileHandle == NULL) {\r
+    return (EFI_INVALID_PARAMETER);\r
+  }\r
 \r
   //\r
   // get the FileInfo structure\r
@@ -694,7 +690,7 @@ FileHandleSetSize (
   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 is NULL, then return error\r
   if Destination's current length (including NULL terminator) is already more then\r
   CurrentSize, then ASSERT()\r
 \r
@@ -721,10 +717,9 @@ StrnCatGrowLeft (
   UINTN NewSize;\r
   UINTN CopySize;\r
 \r
-  //\r
-  // ASSERTs\r
-  //\r
-  ASSERT(Destination != NULL);\r
+  if (Destination == NULL) {\r
+    return (NULL);\r
+  }\r
 \r
   //\r
   // If there's nothing to do then just return Destination\r
@@ -765,6 +760,9 @@ StrnCatGrowLeft (
   } else {\r
     *Destination = AllocateZeroPool(Count+sizeof(CHAR16));\r
   }\r
+  if (*Destination == NULL) {\r
+    return NULL;\r
+  }\r
 \r
   CopySize = StrSize(*Destination);\r
   CopyMem((*Destination)+((Count-2)/sizeof(CHAR16)), *Destination, CopySize);\r
@@ -954,12 +952,11 @@ FileHandleReadLine(
 \r
   if (Handle == NULL\r
     ||Size   == NULL\r
+    ||(Buffer==NULL&&*Size!=0)\r
    ){\r
     return (EFI_INVALID_PARAMETER);\r
   }\r
-  if (Buffer == NULL) {\r
-    ASSERT(*Size == 0);\r
-  } else {\r
+  if (Buffer != NULL) {\r
     *Buffer = CHAR_NULL;\r
   }\r
   FileHandleGetPosition(Handle, &OriginalFilePosition);\r
@@ -1022,7 +1019,7 @@ FileHandleReadLine(
 /**\r
   function to write a line of unicode text to a file.\r
 \r
-  if Handle is NULL, ASSERT.\r
+  if Handle is NULL, return error.\r
   if Buffer is NULL, do nothing.  (return SUCCESS)\r
 \r
   @param[in]     Handle         FileHandle to write to\r
@@ -1043,12 +1040,14 @@ FileHandleWriteLine(
   EFI_STATUS Status;\r
   UINTN      Size;\r
 \r
-  ASSERT(Handle != NULL);\r
-\r
   if (Buffer == NULL) {\r
     return (EFI_SUCCESS);\r
   }\r
 \r
+  if (Handle == NULL) {\r
+    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
@@ -1082,18 +1081,20 @@ FileHandlePrintLine(
   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
+  if (Buffer == NULL) {\r
+    return (EFI_OUT_OF_RESOURCES);\r
+  }\r
 \r
   //\r
   // Print into our buffer\r
   //\r
+  VA_START (Marker, Format);\r
   UnicodeVSPrint (Buffer, PcdGet16 (PcdShellPrintBufferSize), Format, Marker);\r
+  VA_END (Marker);\r
 \r
   //\r
   // Print buffer into file\r
@@ -1112,7 +1113,7 @@ FileHandlePrintLine(
 \r
   This will NOT work on directories.\r
 \r
-  If Handle is NULL, then ASSERT.\r
+  If Handle is NULL, then return False.\r
 \r
   @param[in] Handle     the file handle\r
 \r
@@ -1129,20 +1130,19 @@ FileHandleEof(
   UINT64        Pos;\r
   BOOLEAN       RetVal;\r
 \r
-  //\r
-  // ASSERT if Handle is NULL\r
-  //\r
-  ASSERT(Handle != NULL);\r
+  if (Handle == NULL) {\r
+    return (FALSE);\r
+  }\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
+  FileHandleSetPosition(Handle, Pos);\r
+\r
   if (Pos == Info->FileSize) {\r
     RetVal = TRUE;\r
   } else {\r