FileHandleLib.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /*++
  2. Copyright (c) 2004 - 2014, Intel Corporation. All rights reserved.<BR>
  3. SPDX-License-Identifier: BSD-2-Clause-Patent
  4. **/
  5. #ifndef _FILE_HANDLE_LIBRARY_HEADER_
  6. #define _FILE_HANDLE_LIBRARY_HEADER_
  7. #include <Protocol/SimpleFileSystem.h>
  8. //
  9. // The tag for use in identifying UNICODE files.
  10. // If the file is UNICODE, the first 16 bits of the file will equal this value.
  11. //
  12. extern CONST UINT16 gUnicodeFileTag;
  13. /**
  14. This function retrieves information about the file for the handle
  15. specified and stores it in the allocated pool memory.
  16. This function allocates a buffer to store the file's information. It is the
  17. caller's responsibility to free the buffer.
  18. @param[in] FileHandle The file handle of the file for which information is
  19. being requested.
  20. @retval NULL Information could not be retrieved.
  21. @retval !NULL The information about the file.
  22. **/
  23. EFI_FILE_INFO*
  24. EFIAPI
  25. FileHandleGetInfo (
  26. IN EFI_FILE_HANDLE FileHandle
  27. );
  28. /**
  29. This function sets the information about the file for the opened handle
  30. specified.
  31. @param[in] FileHandle The file handle of the file for which information
  32. is being set.
  33. @param[in] FileInfo The information to set.
  34. @retval EFI_SUCCESS The information was set.
  35. @retval EFI_INVALID_PARAMETER A parameter was out of range or invalid.
  36. @retval EFI_UNSUPPORTED The FileHandle does not support FileInfo.
  37. @retval EFI_NO_MEDIA The device has no medium.
  38. @retval EFI_DEVICE_ERROR The device reported an error.
  39. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  40. @retval EFI_WRITE_PROTECTED The file or medium is write protected.
  41. @retval EFI_ACCESS_DENIED The file was opened read only.
  42. @retval EFI_VOLUME_FULL The volume is full.
  43. **/
  44. EFI_STATUS
  45. EFIAPI
  46. FileHandleSetInfo (
  47. IN EFI_FILE_HANDLE FileHandle,
  48. IN CONST EFI_FILE_INFO *FileInfo
  49. );
  50. /**
  51. This function reads information from an opened file.
  52. If FileHandle is not a directory, the function reads the requested number of
  53. bytes from the file at the file's current position and returns them in Buffer.
  54. If the read goes beyond the end of the file, the read length is truncated to the
  55. end of the file. The file's current position is increased by the number of bytes
  56. returned. If FileHandle is a directory, the function reads the directory entry
  57. at the file's current position and returns the entry in Buffer. If the Buffer
  58. is not large enough to hold the current directory entry, then
  59. EFI_BUFFER_TOO_SMALL is returned and the current file position is not updated.
  60. BufferSize is set to be the size of the buffer needed to read the entry. On
  61. success, the current position is updated to the next directory entry. If there
  62. are no more directory entries, the read returns a zero-length buffer.
  63. EFI_FILE_INFO is the structure returned as the directory entry.
  64. @param[in] FileHandle The opened file handle.
  65. @param[in, out] BufferSize On input, the size of buffer in bytes. On return,
  66. the number of bytes written.
  67. @param[out] Buffer The buffer to put read data into.
  68. @retval EFI_SUCCESS Data was read.
  69. @retval EFI_NO_MEDIA The device has no media.
  70. @retval EFI_DEVICE_ERROR The device reported an error.
  71. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  72. @retval EFI_BUFFER_TO_SMALL Buffer is too small. ReadSize contains required
  73. size.
  74. **/
  75. EFI_STATUS
  76. EFIAPI
  77. FileHandleRead(
  78. IN EFI_FILE_HANDLE FileHandle,
  79. IN OUT UINTN *BufferSize,
  80. OUT VOID *Buffer
  81. );
  82. /**
  83. Write data to a file.
  84. This function writes the specified number of bytes to the file at the current
  85. file position. The current file position is advanced the actual number of bytes
  86. written, which is returned in BufferSize. Partial writes only occur when there
  87. has been a data error during the write attempt (such as "volume space full").
  88. The file is automatically grown to hold the data if required. Direct writes to
  89. opened directories are not supported.
  90. @param[in] FileHandle The opened file for writing.
  91. @param[in, out] BufferSize On input, the number of bytes in Buffer. On output,
  92. the number of bytes written.
  93. @param[in] Buffer The buffer containing data to write is stored.
  94. @retval EFI_SUCCESS Data was written.
  95. @retval EFI_UNSUPPORTED Writes to an open directory are not supported.
  96. @retval EFI_NO_MEDIA The device has no media.
  97. @retval EFI_DEVICE_ERROR The device reported an error.
  98. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  99. @retval EFI_WRITE_PROTECTED The device is write-protected.
  100. @retval EFI_ACCESS_DENIED The file was opened for read only.
  101. @retval EFI_VOLUME_FULL The volume is full.
  102. **/
  103. EFI_STATUS
  104. EFIAPI
  105. FileHandleWrite(
  106. IN EFI_FILE_HANDLE FileHandle,
  107. IN OUT UINTN *BufferSize,
  108. IN VOID *Buffer
  109. );
  110. /**
  111. Close an open file handle.
  112. This function closes a specified file handle. All "dirty" cached file data is
  113. flushed to the device, and the file is closed. In all cases the handle is
  114. closed.
  115. @param[in] FileHandle The file handle to close.
  116. @retval EFI_SUCCESS The file handle was closed successfully.
  117. **/
  118. EFI_STATUS
  119. EFIAPI
  120. FileHandleClose (
  121. IN EFI_FILE_HANDLE FileHandle
  122. );
  123. /**
  124. Delete a file and close the handle.
  125. This function closes and deletes a file. In all cases the file handle is closed.
  126. If the file cannot be deleted, the warning code EFI_WARN_DELETE_FAILURE is
  127. returned, but the handle is still closed.
  128. @param[in] FileHandle The file handle to delete.
  129. @retval EFI_SUCCESS The file was closed successfully.
  130. @retval EFI_WARN_DELETE_FAILURE The handle was closed, but the file was not
  131. deleted.
  132. @retval INVALID_PARAMETER One of the parameters has an invalid value.
  133. **/
  134. EFI_STATUS
  135. EFIAPI
  136. FileHandleDelete (
  137. IN EFI_FILE_HANDLE FileHandle
  138. );
  139. /**
  140. Set the current position in a file.
  141. This function sets the current file position for the handle to the position
  142. supplied. With the exception of moving to position 0xFFFFFFFFFFFFFFFF, only
  143. absolute positioning is supported, and moving past the end of the file is
  144. allowed (a subsequent write would grow the file). Moving to position
  145. 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
  146. If FileHandle is a directory, the only position that may be set is zero. This
  147. has the effect of starting the read process of the directory entries over again.
  148. @param[in] FileHandle The file handle on which the position is being set.
  149. @param[in] Position The byte position from the begining of the file.
  150. @retval EFI_SUCCESS The operation completed sucessfully.
  151. @retval EFI_UNSUPPORTED The request for non-zero is not valid on
  152. directories.
  153. @retval INVALID_PARAMETER One of the parameters has an invalid value.
  154. **/
  155. EFI_STATUS
  156. EFIAPI
  157. FileHandleSetPosition (
  158. IN EFI_FILE_HANDLE FileHandle,
  159. IN UINT64 Position
  160. );
  161. /**
  162. Gets a file's current position.
  163. This function retrieves the current file position for the file handle. For
  164. directories, the current file position has no meaning outside of the file
  165. system driver. As such, the operation is not supported. An error is returned
  166. if FileHandle is a directory.
  167. @param[in] FileHandle The open file handle on which to get the position.
  168. @param[out] Position The byte position from begining of file.
  169. @retval EFI_SUCCESS The operation completed successfully.
  170. @retval INVALID_PARAMETER One of the parameters has an invalid value.
  171. @retval EFI_UNSUPPORTED The request is not valid on directories.
  172. **/
  173. EFI_STATUS
  174. EFIAPI
  175. FileHandleGetPosition (
  176. IN EFI_FILE_HANDLE FileHandle,
  177. OUT UINT64 *Position
  178. );
  179. /**
  180. Flushes data on a file.
  181. This function flushes all modified data associated with a file to a device.
  182. @param[in] FileHandle The file handle on which to flush data.
  183. @retval EFI_SUCCESS The data was flushed.
  184. @retval EFI_NO_MEDIA The device has no media.
  185. @retval EFI_DEVICE_ERROR The device reported an error.
  186. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  187. @retval EFI_WRITE_PROTECTED The file or medium is write protected.
  188. @retval EFI_ACCESS_DENIED The file was opened for read only.
  189. **/
  190. EFI_STATUS
  191. EFIAPI
  192. FileHandleFlush (
  193. IN EFI_FILE_HANDLE FileHandle
  194. );
  195. /**
  196. Function to determine if a given handle is a directory handle.
  197. If DirHandle is NULL, then ASSERT().
  198. Open the file information on the DirHandle, and verify that the Attribute
  199. includes EFI_FILE_DIRECTORY bit set.
  200. @param[in] DirHandle The handle to open the file.
  201. @retval EFI_SUCCESS DirHandle is a directory.
  202. @retval EFI_INVALID_PARAMETER DirHandle did not have EFI_FILE_INFO available.
  203. @retval EFI_NOT_FOUND DirHandle is not a directory.
  204. **/
  205. EFI_STATUS
  206. EFIAPI
  207. FileHandleIsDirectory (
  208. IN EFI_FILE_HANDLE DirHandle
  209. );
  210. /**
  211. Retrieve first entry from a directory.
  212. This function takes an open directory handle and gets information from the
  213. first entry in the directory. A buffer is allocated to contain
  214. the information and a pointer to the buffer is returned in *Buffer. The
  215. caller can use FileHandleFindNextFile() to get subsequent directory entries.
  216. The buffer will be freed by FileHandleFindNextFile() when the last directory
  217. entry is read. Otherwise, the caller must free the buffer, using FreePool,
  218. when finished with it.
  219. @param[in] DirHandle The file handle of the directory to search.
  220. @param[out] Buffer The pointer to pointer to buffer for file's information.
  221. @retval EFI_SUCCESS Found the first file.
  222. @retval EFI_NOT_FOUND Cannot find the directory.
  223. @retval EFI_NO_MEDIA The device has no media.
  224. @retval EFI_DEVICE_ERROR The device reported an error.
  225. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  226. @return Others The status of FileHandleGetInfo, FileHandleSetPosition,
  227. or FileHandleRead.
  228. **/
  229. EFI_STATUS
  230. EFIAPI
  231. FileHandleFindFirstFile (
  232. IN EFI_FILE_HANDLE DirHandle,
  233. OUT EFI_FILE_INFO **Buffer
  234. );
  235. /**
  236. Retrieve next entries from a directory.
  237. To use this function, the caller must first call the FileHandleFindFirstFile()
  238. function to get the first directory entry. Subsequent directory entries are
  239. retrieved by using the FileHandleFindNextFile() function. This function can
  240. be called several times to get each entry from the directory. If the call of
  241. FileHandleFindNextFile() retrieved the last directory entry, the next call of
  242. this function will set *NoFile to TRUE and free the buffer.
  243. @param[in] DirHandle The file handle of the directory.
  244. @param[out] Buffer The pointer to buffer for file's information.
  245. @param[out] NoFile The pointer to boolean when last file is found.
  246. @retval EFI_SUCCESS Found the next file, or reached last file.
  247. @retval EFI_NO_MEDIA The device has no media.
  248. @retval EFI_DEVICE_ERROR The device reported an error.
  249. @retval EFI_VOLUME_CORRUPTED The file system structures are corrupted.
  250. **/
  251. EFI_STATUS
  252. EFIAPI
  253. FileHandleFindNextFile(
  254. IN EFI_FILE_HANDLE DirHandle,
  255. OUT EFI_FILE_INFO *Buffer,
  256. OUT BOOLEAN *NoFile
  257. );
  258. /**
  259. Retrieve the size of a file.
  260. If FileHandle is NULL then ASSERT().
  261. If Size is NULL then ASSERT().
  262. This function extracts the file size info from the FileHandle's EFI_FILE_INFO
  263. data.
  264. @param[in] FileHandle The file handle from which size is retrieved.
  265. @param[out] Size The pointer to size.
  266. @retval EFI_SUCCESS The operation completed successfully.
  267. @retval EFI_DEVICE_ERROR Cannot access the file.
  268. **/
  269. EFI_STATUS
  270. EFIAPI
  271. FileHandleGetSize (
  272. IN EFI_FILE_HANDLE FileHandle,
  273. OUT UINT64 *Size
  274. );
  275. /**
  276. Set the size of a file.
  277. If FileHandle is NULL then ASSERT().
  278. This function changes the file size info from the FileHandle's EFI_FILE_INFO
  279. data.
  280. @param[in] FileHandle The file handle whose size is to be changed.
  281. @param[in] Size The new size.
  282. @retval EFI_SUCCESS The operation completed successfully.
  283. @retval EFI_DEVICE_ERROR Cannot access the file.
  284. **/
  285. EFI_STATUS
  286. EFIAPI
  287. FileHandleSetSize (
  288. IN EFI_FILE_HANDLE FileHandle,
  289. IN UINT64 Size
  290. );
  291. /**
  292. Function to get a full filename given a EFI_FILE_HANDLE somewhere lower on the
  293. directory 'stack'.
  294. @param[in] Handle Handle to the Directory or File to create path to.
  295. @param[out] FullFileName Pointer to pointer to generated full file name. It
  296. is the responsibility of the caller to free this memory
  297. with a call to FreePool().
  298. @retval EFI_SUCCESS The operation was successful and FullFileName is valid.
  299. @retval EFI_INVALID_PARAMETER Handle was NULL.
  300. @retval EFI_INVALID_PARAMETER FullFileName was NULL.
  301. @retval EFI_OUT_OF_MEMORY A memory allocation failed.
  302. **/
  303. EFI_STATUS
  304. EFIAPI
  305. FileHandleGetFileName (
  306. IN CONST EFI_FILE_HANDLE Handle,
  307. OUT CHAR16 **FullFileName
  308. );
  309. /**
  310. Function to read a single line (up to but not including the \n) from a file.
  311. If the position upon start is 0, then the Ascii Boolean will be set. This should be
  312. maintained and not changed for all operations with the same file.
  313. @param[in] Handle FileHandle to read from.
  314. @param[in, out] Buffer The pointer to buffer to read into.
  315. @param[in, out] Size The pointer to number of bytes in Buffer.
  316. @param[in] Truncate If the buffer is large enough, this has no effect.
  317. If the buffer is is too small and Truncate is TRUE,
  318. the line will be truncated.
  319. If the buffer is is too small and Truncate is FALSE,
  320. then no read will occur.
  321. @param[in, out] Ascii Boolean value for indicating whether the file is
  322. Ascii (TRUE) or UCS2 (FALSE).
  323. @retval EFI_SUCCESS The operation was successful. The line is stored in
  324. Buffer.
  325. @retval EFI_INVALID_PARAMETER Handle was NULL.
  326. @retval EFI_INVALID_PARAMETER Size was NULL.
  327. @retval EFI_BUFFER_TOO_SMALL Size was not large enough to store the line.
  328. Size was updated to the minimum space required.
  329. @sa FileHandleRead
  330. **/
  331. EFI_STATUS
  332. EFIAPI
  333. FileHandleReadLine(
  334. IN EFI_FILE_HANDLE Handle,
  335. IN OUT CHAR16 *Buffer,
  336. IN OUT UINTN *Size,
  337. IN BOOLEAN Truncate,
  338. IN OUT BOOLEAN *Ascii
  339. );
  340. /**
  341. Function to read a single line from a file. The \n is not included in the returned
  342. buffer. The returned buffer must be callee freed.
  343. If the position upon start is 0, then the Ascii Boolean will be set. This should be
  344. maintained and not changed for all operations with the same file.
  345. @param[in] Handle FileHandle to read from.
  346. @param[in, out] Ascii Boolean value for indicating whether the file is
  347. Ascii (TRUE) or UCS2 (FALSE).
  348. @return The line of text from the file.
  349. @sa FileHandleReadLine
  350. **/
  351. CHAR16*
  352. EFIAPI
  353. FileHandleReturnLine(
  354. IN EFI_FILE_HANDLE Handle,
  355. IN OUT BOOLEAN *Ascii
  356. );
  357. /**
  358. Function to write a line of unicode text to a file.
  359. If Handle is NULL, ASSERT.
  360. @param[in] Handle FileHandle to write to.
  361. @param[in] Buffer Buffer to write, if NULL the function will
  362. take no action and return EFI_SUCCESS.
  363. @retval EFI_SUCCESS The data was written.
  364. @retval other Failure.
  365. @sa FileHandleWrite
  366. **/
  367. EFI_STATUS
  368. EFIAPI
  369. FileHandleWriteLine(
  370. IN EFI_FILE_HANDLE Handle,
  371. IN CHAR16 *Buffer
  372. );
  373. /**
  374. Function to take a formatted argument and print it to a file.
  375. @param[in] Handle The file handle for the file to write to.
  376. @param[in] Format The format argument (see printlib for the format specifier).
  377. @param[in] ... The variable arguments for the format.
  378. @retval EFI_SUCCESS The operation was successful.
  379. @retval other A return value from FileHandleWriteLine.
  380. @sa FileHandleWriteLine
  381. **/
  382. EFI_STATUS
  383. EFIAPI
  384. FileHandlePrintLine(
  385. IN EFI_FILE_HANDLE Handle,
  386. IN CONST CHAR16 *Format,
  387. ...
  388. );
  389. /**
  390. Function to determine if a FILE_HANDLE is at the end of the file.
  391. This will NOT work on directories.
  392. If Handle is NULL, then ASSERT().
  393. @param[in] Handle The file handle.
  394. @retval TRUE The position is at the end of the file.
  395. @retval FALSE The position is not at the end of the file.
  396. **/
  397. BOOLEAN
  398. EFIAPI
  399. FileHandleEof(
  400. IN EFI_FILE_HANDLE Handle
  401. );
  402. #endif //_FILE_HANDLE_LIBRARY_HEADER_