Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | #ifndef _FS_H |
| 6 | #define _FS_H |
| 7 | |
| 8 | #include <common.h> |
| 9 | |
| 10 | #define FS_TYPE_ANY 0 |
| 11 | #define FS_TYPE_FAT 1 |
| 12 | #define FS_TYPE_EXT 2 |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 13 | #define FS_TYPE_SANDBOX 3 |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 14 | #define FS_TYPE_UBIFS 4 |
Marek BehĂșn | 0c936ee | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 15 | #define FS_TYPE_BTRFS 5 |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Tell the fs layer which block device an partition to use for future |
| 19 | * commands. This also internally identifies the filesystem that is present |
| 20 | * within the partition. The identification process may be limited to a |
| 21 | * specific filesystem type by passing FS_* in the fstype parameter. |
| 22 | * |
| 23 | * Returns 0 on success. |
| 24 | * Returns non-zero if there is an error accessing the disk or partition, or |
| 25 | * no known filesystem type could be recognized on it. |
| 26 | */ |
| 27 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype); |
| 28 | |
| 29 | /* |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 30 | * fs_set_blk_dev_with_part - Set current block device + partition |
| 31 | * |
| 32 | * Similar to fs_set_blk_dev(), but useful for cases where you already |
| 33 | * know the blk_desc and part number. |
| 34 | * |
| 35 | * Returns 0 on success. |
| 36 | * Returns non-zero if invalid partition or error accessing the disk. |
| 37 | */ |
| 38 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part); |
| 39 | |
Alex Kiernan | 0d488e8 | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 40 | /** |
AKASHI Takahiro | 64f49eb | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 41 | * fs_close() - Unset current block device and partition |
| 42 | * |
Heinrich Schuchardt | e4bad9f | 2019-10-13 10:26:26 +0200 | [diff] [blame] | 43 | * fs_close() closes the connection to a file system opened with either |
| 44 | * fs_set_blk_dev() or fs_set_dev_with_part(). |
| 45 | * |
| 46 | * Many file functions implicitly call fs_close(), e.g. fs_closedir(), |
| 47 | * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(), |
| 48 | * fs_unlink(). |
AKASHI Takahiro | 64f49eb | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 49 | */ |
| 50 | void fs_close(void); |
| 51 | |
| 52 | /** |
AKASHI Takahiro | b7cd956 | 2019-10-07 14:59:37 +0900 | [diff] [blame] | 53 | * fs_get_type() - Get type of current filesystem |
| 54 | * |
| 55 | * Return: filesystem type |
| 56 | * |
| 57 | * Returns filesystem type representing the current filesystem, or |
| 58 | * FS_TYPE_ANY for any unrecognised filesystem. |
| 59 | */ |
| 60 | int fs_get_type(void); |
| 61 | |
| 62 | /** |
Alex Kiernan | 0d488e8 | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 63 | * fs_get_type_name() - Get type of current filesystem |
| 64 | * |
| 65 | * Return: Pointer to filesystem name |
| 66 | * |
| 67 | * Returns a string describing the current filesystem, or the sentinel |
| 68 | * "unsupported" for any unrecognised filesystem. |
| 69 | */ |
| 70 | const char *fs_get_type_name(void); |
| 71 | |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 72 | /* |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 73 | * Print the list of files on the partition previously set by fs_set_blk_dev(), |
| 74 | * in directory "dirname". |
| 75 | * |
| 76 | * Returns 0 on success. Returns non-zero on error. |
| 77 | */ |
| 78 | int fs_ls(const char *dirname); |
| 79 | |
| 80 | /* |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 81 | * Determine whether a file exists |
| 82 | * |
| 83 | * Returns 1 if the file exists, 0 if it doesn't exist. |
| 84 | */ |
| 85 | int fs_exists(const char *filename); |
| 86 | |
| 87 | /* |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 88 | * fs_size - Determine a file's size |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 89 | * |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 90 | * @filename: Name of the file |
| 91 | * @size: Size of file |
| 92 | * @return 0 if ok with valid *size, negative on error |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 93 | */ |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 94 | int fs_size(const char *filename, loff_t *size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 95 | |
Heinrich Schuchardt | a0e92cd | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 96 | /** |
| 97 | * fs_read() - read file from the partition previously set by fs_set_blk_dev() |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 98 | * |
Heinrich Schuchardt | a0e92cd | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 99 | * Note that not all filesystem drivers support either or both of offset != 0 |
| 100 | * and len != 0. |
| 101 | * |
| 102 | * @filename: full path of the file to read from |
| 103 | * @addr: address of the buffer to write to |
| 104 | * @offset: offset in the file from where to start reading |
| 105 | * @len: the number of bytes to read. Use 0 to read entire file. |
| 106 | * @actread: returns the actual number of bytes read |
| 107 | * Return: 0 if OK with valid *actread, -1 on error conditions |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 108 | */ |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 109 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 110 | loff_t *actread); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 111 | |
Heinrich Schuchardt | a0e92cd | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 112 | /** |
| 113 | * fs_write() - write file to the partition previously set by fs_set_blk_dev() |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 114 | * |
Heinrich Schuchardt | a0e92cd | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 115 | * Note that not all filesystem drivers support offset != 0. |
| 116 | * |
| 117 | * @filename: full path of the file to write to |
| 118 | * @addr: address of the buffer to read from |
| 119 | * @offset: offset in the file from where to start writing |
| 120 | * @len: the number of bytes to write |
| 121 | * @actwrite: returns the actual number of bytes written |
| 122 | * Return: 0 if OK with valid *actwrite, -1 on error conditions |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 123 | */ |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 124 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 125 | loff_t *actwrite); |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 126 | |
| 127 | /* |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 128 | * Directory entry types, matches the subset of DT_x in posix readdir() |
| 129 | * which apply to u-boot. |
| 130 | */ |
| 131 | #define FS_DT_DIR 4 /* directory */ |
| 132 | #define FS_DT_REG 8 /* regular file */ |
| 133 | #define FS_DT_LNK 10 /* symbolic link */ |
| 134 | |
| 135 | /* |
| 136 | * A directory entry, returned by fs_readdir(). Returns information |
| 137 | * about the file/directory at the current directory entry position. |
| 138 | */ |
| 139 | struct fs_dirent { |
| 140 | unsigned type; /* one of FS_DT_x (not a mask) */ |
| 141 | loff_t size; /* size in bytes */ |
| 142 | char name[256]; |
| 143 | }; |
| 144 | |
| 145 | /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */ |
| 146 | struct fs_dir_stream { |
| 147 | /* private to fs. layer: */ |
| 148 | struct blk_desc *desc; |
| 149 | int part; |
| 150 | }; |
| 151 | |
| 152 | /* |
| 153 | * fs_opendir - Open a directory |
| 154 | * |
| 155 | * @filename: the path to directory to open |
| 156 | * @return a pointer to the directory stream or NULL on error and errno |
| 157 | * set appropriately |
| 158 | */ |
| 159 | struct fs_dir_stream *fs_opendir(const char *filename); |
| 160 | |
| 161 | /* |
| 162 | * fs_readdir - Read the next directory entry in the directory stream. |
| 163 | * |
| 164 | * Works in an analogous way to posix readdir(). The previously returned |
| 165 | * directory entry is no longer valid after calling fs_readdir() again. |
| 166 | * After fs_closedir() is called, the returned directory entry is no |
| 167 | * longer valid. |
| 168 | * |
| 169 | * @dirs: the directory stream |
| 170 | * @return the next directory entry (only valid until next fs_readdir() or |
| 171 | * fs_closedir() call, do not attempt to free()) or NULL if the end of |
| 172 | * the directory is reached. |
| 173 | */ |
| 174 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs); |
| 175 | |
| 176 | /* |
| 177 | * fs_closedir - close a directory stream |
| 178 | * |
| 179 | * @dirs: the directory stream |
| 180 | */ |
| 181 | void fs_closedir(struct fs_dir_stream *dirs); |
| 182 | |
| 183 | /* |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 184 | * fs_unlink - delete a file or directory |
| 185 | * |
| 186 | * If a given name is a directory, it will be deleted only if it's empty |
| 187 | * |
| 188 | * @filename: Name of file or directory to delete |
| 189 | * @return 0 on success, -1 on error conditions |
| 190 | */ |
| 191 | int fs_unlink(const char *filename); |
| 192 | |
| 193 | /* |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 194 | * fs_mkdir - Create a directory |
| 195 | * |
| 196 | * @filename: Name of directory to create |
| 197 | * @return 0 on success, -1 on error conditions |
| 198 | */ |
| 199 | int fs_mkdir(const char *filename); |
| 200 | |
| 201 | /* |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 202 | * Common implementation for various filesystem commands, optionally limited |
| 203 | * to a specific filesystem type via the fstype parameter. |
| 204 | */ |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 205 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 206 | int fstype); |
Stephen Warren | f9b55e2 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 207 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 208 | int fstype); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 209 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 210 | int fstype); |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 211 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 212 | int fstype); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 213 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 214 | int fstype); |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 215 | int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 216 | int fstype); |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 217 | int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 218 | int fstype); |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 219 | int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 220 | int fstype); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 221 | |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 222 | /* |
| 223 | * Determine the UUID of the specified filesystem and print it. Optionally it is |
| 224 | * possible to store the UUID directly in env. |
| 225 | */ |
| 226 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 227 | int fstype); |
| 228 | |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 229 | /* |
| 230 | * Determine the type of the specified filesystem and print it. Optionally it is |
| 231 | * possible to store the type directly in env. |
| 232 | */ |
| 233 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 234 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 235 | #endif /* _FS_H */ |