Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | eefbc3f | 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 | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 13 | #define FS_TYPE_SANDBOX 3 |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 14 | #define FS_TYPE_UBIFS 4 |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 15 | #define FS_TYPE_BTRFS 5 |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 16 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 17 | struct blk_desc; |
| 18 | |
Simon Glass | 1b3f75f | 2019-12-28 10:44:44 -0700 | [diff] [blame] | 19 | /** |
| 20 | * do_fat_fsload - Run the fatload command |
| 21 | * |
| 22 | * @cmdtp: Command information for fatload |
| 23 | * @flag: Command flags (CMD_FLAG_...) |
| 24 | * @argc: Number of arguments |
| 25 | * @argv: List of arguments |
| 26 | * @return result (see enum command_ret_t) |
| 27 | */ |
| 28 | int do_fat_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); |
| 29 | |
| 30 | /** |
| 31 | * do_ext2load - Run the ext2load command |
| 32 | * |
| 33 | * @cmdtp: Command information for ext2load |
| 34 | * @flag: Command flags (CMD_FLAG_...) |
| 35 | * @argc: Number of arguments |
| 36 | * @argv: List of arguments |
| 37 | * @return result (see enum command_ret_t) |
| 38 | */ |
| 39 | int do_ext2load(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); |
| 40 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Tell the fs layer which block device an 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 | /* |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 54 | * fs_set_blk_dev_with_part - Set current block device + partition |
| 55 | * |
| 56 | * Similar to fs_set_blk_dev(), but useful for cases where you already |
| 57 | * know the blk_desc and part number. |
| 58 | * |
| 59 | * Returns 0 on success. |
| 60 | * Returns non-zero if invalid partition or error accessing the disk. |
| 61 | */ |
| 62 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part); |
| 63 | |
Alex Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 64 | /** |
AKASHI Takahiro | ca8e377 | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 65 | * fs_close() - Unset current block device and partition |
| 66 | * |
Heinrich Schuchardt | 63a5752 | 2019-10-13 10:26:26 +0200 | [diff] [blame] | 67 | * fs_close() closes the connection to a file system opened with either |
| 68 | * fs_set_blk_dev() or fs_set_dev_with_part(). |
| 69 | * |
| 70 | * Many file functions implicitly call fs_close(), e.g. fs_closedir(), |
| 71 | * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(), |
| 72 | * fs_unlink(). |
AKASHI Takahiro | ca8e377 | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 73 | */ |
| 74 | void fs_close(void); |
| 75 | |
| 76 | /** |
AKASHI Takahiro | 80ac734 | 2019-10-07 14:59:37 +0900 | [diff] [blame] | 77 | * fs_get_type() - Get type of current filesystem |
| 78 | * |
| 79 | * Return: filesystem type |
| 80 | * |
| 81 | * Returns filesystem type representing the current filesystem, or |
| 82 | * FS_TYPE_ANY for any unrecognised filesystem. |
| 83 | */ |
| 84 | int fs_get_type(void); |
| 85 | |
| 86 | /** |
Alex Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 87 | * fs_get_type_name() - Get type of current filesystem |
| 88 | * |
| 89 | * Return: Pointer to filesystem name |
| 90 | * |
| 91 | * Returns a string describing the current filesystem, or the sentinel |
| 92 | * "unsupported" for any unrecognised filesystem. |
| 93 | */ |
| 94 | const char *fs_get_type_name(void); |
| 95 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 96 | /* |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 97 | * Print the list of files on the partition previously set by fs_set_blk_dev(), |
| 98 | * in directory "dirname". |
| 99 | * |
| 100 | * Returns 0 on success. Returns non-zero on error. |
| 101 | */ |
| 102 | int fs_ls(const char *dirname); |
| 103 | |
| 104 | /* |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 105 | * Determine whether a file exists |
| 106 | * |
| 107 | * Returns 1 if the file exists, 0 if it doesn't exist. |
| 108 | */ |
| 109 | int fs_exists(const char *filename); |
| 110 | |
| 111 | /* |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 112 | * fs_size - Determine a file's size |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 113 | * |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 114 | * @filename: Name of the file |
| 115 | * @size: Size of file |
| 116 | * @return 0 if ok with valid *size, negative on error |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 117 | */ |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 118 | int fs_size(const char *filename, loff_t *size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 119 | |
Heinrich Schuchardt | 5f1d695 | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 120 | /** |
| 121 | * fs_read() - read file from the partition previously set by fs_set_blk_dev() |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 122 | * |
Heinrich Schuchardt | 5f1d695 | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 123 | * Note that not all filesystem drivers support either or both of offset != 0 |
| 124 | * and len != 0. |
| 125 | * |
| 126 | * @filename: full path of the file to read from |
| 127 | * @addr: address of the buffer to write to |
| 128 | * @offset: offset in the file from where to start reading |
| 129 | * @len: the number of bytes to read. Use 0 to read entire file. |
| 130 | * @actread: returns the actual number of bytes read |
| 131 | * Return: 0 if OK with valid *actread, -1 on error conditions |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 132 | */ |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 133 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 134 | loff_t *actread); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 135 | |
Heinrich Schuchardt | 5f1d695 | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 136 | /** |
| 137 | * fs_write() - write file to the partition previously set by fs_set_blk_dev() |
| 138 | * |
| 139 | * Note that not all filesystem drivers support offset != 0. |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 140 | * |
Heinrich Schuchardt | 5f1d695 | 2019-04-25 20:36:39 +0200 | [diff] [blame] | 141 | * @filename: full path of the file to write to |
| 142 | * @addr: address of the buffer to read from |
| 143 | * @offset: offset in the file from where to start writing |
| 144 | * @len: the number of bytes to write |
| 145 | * @actwrite: returns the actual number of bytes written |
| 146 | * Return: 0 if OK with valid *actwrite, -1 on error conditions |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 147 | */ |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 148 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 149 | loff_t *actwrite); |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 150 | |
| 151 | /* |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 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 | |
| 159 | /* |
| 160 | * A directory entry, returned by fs_readdir(). Returns information |
| 161 | * about the file/directory at the current directory entry position. |
| 162 | */ |
| 163 | struct fs_dirent { |
| 164 | unsigned type; /* one of FS_DT_x (not a mask) */ |
| 165 | loff_t size; /* size in bytes */ |
| 166 | char name[256]; |
| 167 | }; |
| 168 | |
| 169 | /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */ |
| 170 | struct fs_dir_stream { |
| 171 | /* private to fs. layer: */ |
| 172 | struct blk_desc *desc; |
| 173 | int part; |
| 174 | }; |
| 175 | |
| 176 | /* |
| 177 | * fs_opendir - Open a directory |
| 178 | * |
| 179 | * @filename: the path to directory to open |
| 180 | * @return a pointer to the directory stream or NULL on error and errno |
| 181 | * set appropriately |
| 182 | */ |
| 183 | struct fs_dir_stream *fs_opendir(const char *filename); |
| 184 | |
| 185 | /* |
| 186 | * fs_readdir - Read the next directory entry in the directory stream. |
| 187 | * |
| 188 | * Works in an analogous way to posix readdir(). The previously returned |
| 189 | * directory entry is no longer valid after calling fs_readdir() again. |
| 190 | * After fs_closedir() is called, the returned directory entry is no |
| 191 | * longer valid. |
| 192 | * |
| 193 | * @dirs: the directory stream |
| 194 | * @return the next directory entry (only valid until next fs_readdir() or |
| 195 | * fs_closedir() call, do not attempt to free()) or NULL if the end of |
| 196 | * the directory is reached. |
| 197 | */ |
| 198 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs); |
| 199 | |
| 200 | /* |
| 201 | * fs_closedir - close a directory stream |
| 202 | * |
| 203 | * @dirs: the directory stream |
| 204 | */ |
| 205 | void fs_closedir(struct fs_dir_stream *dirs); |
| 206 | |
| 207 | /* |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 208 | * fs_unlink - delete a file or directory |
| 209 | * |
| 210 | * If a given name is a directory, it will be deleted only if it's empty |
| 211 | * |
| 212 | * @filename: Name of file or directory to delete |
| 213 | * @return 0 on success, -1 on error conditions |
| 214 | */ |
| 215 | int fs_unlink(const char *filename); |
| 216 | |
| 217 | /* |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 218 | * fs_mkdir - Create a directory |
| 219 | * |
| 220 | * @filename: Name of directory to create |
| 221 | * @return 0 on success, -1 on error conditions |
| 222 | */ |
| 223 | int fs_mkdir(const char *filename); |
| 224 | |
| 225 | /* |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 226 | * Common implementation for various filesystem commands, optionally limited |
| 227 | * to a specific filesystem type via the fstype parameter. |
| 228 | */ |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 229 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 230 | int fstype); |
Stephen Warren | 128d3d9 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 231 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 232 | int fstype); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 233 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 234 | int fstype); |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 235 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 236 | int fstype); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 237 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 238 | int fstype); |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 239 | int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 240 | int fstype); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 241 | int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 242 | int fstype); |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 243 | int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 244 | int fstype); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 245 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 246 | /* |
| 247 | * Determine the UUID of the specified filesystem and print it. Optionally it is |
| 248 | * possible to store the UUID directly in env. |
| 249 | */ |
| 250 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 251 | int fstype); |
| 252 | |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 253 | /* |
| 254 | * Determine the type of the specified filesystem and print it. Optionally it is |
| 255 | * possible to store the type directly in env. |
| 256 | */ |
| 257 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
| 258 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 259 | #endif /* _FS_H */ |