fs.h 7.6 KB

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