fs.h 7.0 KB

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