fs.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #ifndef _FS_H
  6. #define _FS_H
  7. #include <common.h>
  8. #define FS_TYPE_ANY 0
  9. #define FS_TYPE_FAT 1
  10. #define FS_TYPE_EXT 2
  11. #define FS_TYPE_SANDBOX 3
  12. #define FS_TYPE_UBIFS 4
  13. #define FS_TYPE_BTRFS 5
  14. /*
  15. * Tell the fs layer which block device an partition to use for future
  16. * commands. This also internally identifies the filesystem that is present
  17. * within the partition. The identification process may be limited to a
  18. * specific filesystem type by passing FS_* in the fstype parameter.
  19. *
  20. * Returns 0 on success.
  21. * Returns non-zero if there is an error accessing the disk or partition, or
  22. * no known filesystem type could be recognized on it.
  23. */
  24. int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
  25. /*
  26. * fs_set_blk_dev_with_part - Set current block device + partition
  27. *
  28. * Similar to fs_set_blk_dev(), but useful for cases where you already
  29. * know the blk_desc and part number.
  30. *
  31. * Returns 0 on success.
  32. * Returns non-zero if invalid partition or error accessing the disk.
  33. */
  34. int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
  35. /**
  36. * fs_get_type_name() - Get type of current filesystem
  37. *
  38. * Return: Pointer to filesystem name
  39. *
  40. * Returns a string describing the current filesystem, or the sentinel
  41. * "unsupported" for any unrecognised filesystem.
  42. */
  43. const char *fs_get_type_name(void);
  44. /*
  45. * Print the list of files on the partition previously set by fs_set_blk_dev(),
  46. * in directory "dirname".
  47. *
  48. * Returns 0 on success. Returns non-zero on error.
  49. */
  50. int fs_ls(const char *dirname);
  51. /*
  52. * Determine whether a file exists
  53. *
  54. * Returns 1 if the file exists, 0 if it doesn't exist.
  55. */
  56. int fs_exists(const char *filename);
  57. /*
  58. * fs_size - Determine a file's size
  59. *
  60. * @filename: Name of the file
  61. * @size: Size of file
  62. * @return 0 if ok with valid *size, negative on error
  63. */
  64. int fs_size(const char *filename, loff_t *size);
  65. /**
  66. * fs_read() - read file from the partition previously set by fs_set_blk_dev()
  67. *
  68. * Note that not all filesystem drivers support either or both of offset != 0
  69. * and len != 0.
  70. *
  71. * @filename: full path of the file to read from
  72. * @addr: address of the buffer to write to
  73. * @offset: offset in the file from where to start reading
  74. * @len: the number of bytes to read. Use 0 to read entire file.
  75. * @actread: returns the actual number of bytes read
  76. * Return: 0 if OK with valid *actread, -1 on error conditions
  77. */
  78. int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
  79. loff_t *actread);
  80. /**
  81. * fs_write() - write file to the partition previously set by fs_set_blk_dev()
  82. *
  83. * Note that not all filesystem drivers support offset != 0.
  84. *
  85. * @filename: full path of the file to write to
  86. * @addr: address of the buffer to read from
  87. * @offset: offset in the file from where to start writing
  88. * @len: the number of bytes to write
  89. * @actwrite: returns the actual number of bytes written
  90. * Return: 0 if OK with valid *actwrite, -1 on error conditions
  91. */
  92. int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
  93. loff_t *actwrite);
  94. /*
  95. * Directory entry types, matches the subset of DT_x in posix readdir()
  96. * which apply to u-boot.
  97. */
  98. #define FS_DT_DIR 4 /* directory */
  99. #define FS_DT_REG 8 /* regular file */
  100. #define FS_DT_LNK 10 /* symbolic link */
  101. /*
  102. * A directory entry, returned by fs_readdir(). Returns information
  103. * about the file/directory at the current directory entry position.
  104. */
  105. struct fs_dirent {
  106. unsigned type; /* one of FS_DT_x (not a mask) */
  107. loff_t size; /* size in bytes */
  108. char name[256];
  109. };
  110. /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
  111. struct fs_dir_stream {
  112. /* private to fs. layer: */
  113. struct blk_desc *desc;
  114. int part;
  115. };
  116. /*
  117. * fs_opendir - Open a directory
  118. *
  119. * @filename: the path to directory to open
  120. * @return a pointer to the directory stream or NULL on error and errno
  121. * set appropriately
  122. */
  123. struct fs_dir_stream *fs_opendir(const char *filename);
  124. /*
  125. * fs_readdir - Read the next directory entry in the directory stream.
  126. *
  127. * Works in an analogous way to posix readdir(). The previously returned
  128. * directory entry is no longer valid after calling fs_readdir() again.
  129. * After fs_closedir() is called, the returned directory entry is no
  130. * longer valid.
  131. *
  132. * @dirs: the directory stream
  133. * @return the next directory entry (only valid until next fs_readdir() or
  134. * fs_closedir() call, do not attempt to free()) or NULL if the end of
  135. * the directory is reached.
  136. */
  137. struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
  138. /*
  139. * fs_closedir - close a directory stream
  140. *
  141. * @dirs: the directory stream
  142. */
  143. void fs_closedir(struct fs_dir_stream *dirs);
  144. /*
  145. * fs_unlink - delete a file or directory
  146. *
  147. * If a given name is a directory, it will be deleted only if it's empty
  148. *
  149. * @filename: Name of file or directory to delete
  150. * @return 0 on success, -1 on error conditions
  151. */
  152. int fs_unlink(const char *filename);
  153. /*
  154. * fs_mkdir - Create a directory
  155. *
  156. * @filename: Name of directory to create
  157. * @return 0 on success, -1 on error conditions
  158. */
  159. int fs_mkdir(const char *filename);
  160. /*
  161. * Common implementation for various filesystem commands, optionally limited
  162. * to a specific filesystem type via the fstype parameter.
  163. */
  164. int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  165. int fstype);
  166. int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  167. int fstype);
  168. int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  169. int fstype);
  170. int file_exists(const char *dev_type, const char *dev_part, const char *file,
  171. int fstype);
  172. int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  173. int fstype);
  174. int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  175. int fstype);
  176. int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  177. int fstype);
  178. int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  179. int fstype);
  180. /*
  181. * Determine the UUID of the specified filesystem and print it. Optionally it is
  182. * possible to store the UUID directly in env.
  183. */
  184. int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
  185. int fstype);
  186. /*
  187. * Determine the type of the specified filesystem and print it. Optionally it is
  188. * possible to store the type directly in env.
  189. */
  190. int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  191. #endif /* _FS_H */