fs.h 8.4 KB

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