fs.h 10 KB

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