fs.h 8.1 KB

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