fs.h 7.6 KB

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