blob: 8370d88cb203aa330e70a2d9fc25d839ee9625fe [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Stephen Warren045fa1e2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warren045fa1e2012-10-22 06:43:51 +00004 */
5#ifndef _FS_H
6#define _FS_H
7
8#include <common.h>
Heinrich Schuchardt13c11c62021-05-15 22:06:16 +02009#include <rtc.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000010
Simon Glass09140112020-05-10 11:40:03 -060011struct cmd_tbl;
12
Stephen Warren045fa1e2012-10-22 06:43:51 +000013#define FS_TYPE_ANY 0
14#define FS_TYPE_FAT 1
15#define FS_TYPE_EXT 2
Simon Glass92ccc962012-12-26 09:53:35 +000016#define FS_TYPE_SANDBOX 3
Hans de Goede251cee02015-09-17 18:46:58 -040017#define FS_TYPE_UBIFS 4
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020018#define FS_TYPE_BTRFS 5
Joao Marcos Costac5100612020-07-30 15:33:47 +020019#define FS_TYPE_SQUASHFS 6
Huang Jianan830613f2022-02-26 15:05:47 +080020#define FS_TYPE_EROFS 7
Sean Andersonf676b452022-03-22 16:59:20 -040021#define FS_TYPE_SEMIHOSTING 8
Stephen Warren045fa1e2012-10-22 06:43:51 +000022
Simon Glasse6f6f9e2020-05-10 11:39:58 -060023struct blk_desc;
24
Simon Glass14449982019-12-28 10:44:44 -070025/**
26 * do_fat_fsload - Run the fatload command
27 *
28 * @cmdtp: Command information for fatload
29 * @flag: Command flags (CMD_FLAG_...)
30 * @argc: Number of arguments
31 * @argv: List of arguments
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010032 * Return: result (see enum command_ret_t)
Simon Glass14449982019-12-28 10:44:44 -070033 */
Simon Glass09140112020-05-10 11:40:03 -060034int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070036
37/**
38 * do_ext2load - Run the ext2load command
39 *
40 * @cmdtp: Command information for ext2load
41 * @flag: Command flags (CMD_FLAG_...)
42 * @argc: Number of arguments
43 * @argv: List of arguments
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010044 * Return: result (see enum command_ret_t)
Simon Glass14449982019-12-28 10:44:44 -070045 */
Simon Glass09140112020-05-10 11:40:03 -060046int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070047
Stephen Warren045fa1e2012-10-22 06:43:51 +000048/*
Roger Knechtda223d82022-08-25 12:12:01 +000049 * Tell the fs layer which block device and partition to use for future
Stephen Warren045fa1e2012-10-22 06:43:51 +000050 * commands. This also internally identifies the filesystem that is present
51 * within the partition. The identification process may be limited to a
52 * specific filesystem type by passing FS_* in the fstype parameter.
53 *
54 * Returns 0 on success.
55 * Returns non-zero if there is an error accessing the disk or partition, or
56 * no known filesystem type could be recognized on it.
57 */
58int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
59
Simon Glassc24e58f2022-04-24 23:31:04 -060060/**
61 * fs_set_type() - Tell fs layer which filesystem type is used
62 *
63 * This is needed when reading from a non-block device such as sandbox. It does
64 * a similar job to fs_set_blk_dev() but just sets the filesystem type instead
65 * of detecting it and loading it on the block device
66 *
67 * @type: Filesystem type to use (FS_TYPE...)
68 */
69void fs_set_type(int type);
70
Stephen Warren045fa1e2012-10-22 06:43:51 +000071/*
Rob Clark4bbcc962017-09-09 13:15:55 -040072 * fs_set_blk_dev_with_part - Set current block device + partition
73 *
74 * Similar to fs_set_blk_dev(), but useful for cases where you already
75 * know the blk_desc and part number.
76 *
77 * Returns 0 on success.
78 * Returns non-zero if invalid partition or error accessing the disk.
79 */
80int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
81
Alex Kiernan0d488e82018-05-29 15:30:50 +000082/**
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090083 * fs_close() - Unset current block device and partition
84 *
Heinrich Schuchardte4bad9f2019-10-13 10:26:26 +020085 * fs_close() closes the connection to a file system opened with either
86 * fs_set_blk_dev() or fs_set_dev_with_part().
87 *
88 * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
89 * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
90 * fs_unlink().
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090091 */
92void fs_close(void);
93
94/**
AKASHI Takahirob7cd9562019-10-07 14:59:37 +090095 * fs_get_type() - Get type of current filesystem
96 *
97 * Return: filesystem type
98 *
99 * Returns filesystem type representing the current filesystem, or
100 * FS_TYPE_ANY for any unrecognised filesystem.
101 */
102int fs_get_type(void);
103
104/**
Alex Kiernan0d488e82018-05-29 15:30:50 +0000105 * fs_get_type_name() - Get type of current filesystem
106 *
107 * Return: Pointer to filesystem name
108 *
109 * Returns a string describing the current filesystem, or the sentinel
110 * "unsupported" for any unrecognised filesystem.
111 */
112const char *fs_get_type_name(void);
113
Rob Clark4bbcc962017-09-09 13:15:55 -0400114/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000115 * Print the list of files on the partition previously set by fs_set_blk_dev(),
116 * in directory "dirname".
117 *
118 * Returns 0 on success. Returns non-zero on error.
119 */
120int fs_ls(const char *dirname);
121
122/*
Stephen Warren61529162014-02-03 13:21:00 -0700123 * Determine whether a file exists
124 *
125 * Returns 1 if the file exists, 0 if it doesn't exist.
126 */
127int fs_exists(const char *filename);
128
129/*
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800130 * fs_size - Determine a file's size
Stephen Warrencf659812014-06-11 12:47:26 -0600131 *
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800132 * @filename: Name of the file
133 * @size: Size of file
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100134 * Return: 0 if ok with valid *size, negative on error
Stephen Warrencf659812014-06-11 12:47:26 -0600135 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800136int fs_size(const char *filename, loff_t *size);
Stephen Warrencf659812014-06-11 12:47:26 -0600137
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200138/**
139 * fs_read() - read file from the partition previously set by fs_set_blk_dev()
Stephen Warren045fa1e2012-10-22 06:43:51 +0000140 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200141 * Note that not all filesystem drivers support either or both of offset != 0
142 * and len != 0.
143 *
144 * @filename: full path of the file to read from
145 * @addr: address of the buffer to write to
146 * @offset: offset in the file from where to start reading
147 * @len: the number of bytes to read. Use 0 to read entire file.
148 * @actread: returns the actual number of bytes read
149 * Return: 0 if OK with valid *actread, -1 on error conditions
Stephen Warren045fa1e2012-10-22 06:43:51 +0000150 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800151int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
152 loff_t *actread);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000153
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200154/**
155 * fs_write() - write file to the partition previously set by fs_set_blk_dev()
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700156 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200157 * Note that not all filesystem drivers support offset != 0.
158 *
159 * @filename: full path of the file to write to
160 * @addr: address of the buffer to read from
161 * @offset: offset in the file from where to start writing
162 * @len: the number of bytes to write
163 * @actwrite: returns the actual number of bytes written
164 * Return: 0 if OK with valid *actwrite, -1 on error conditions
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700165 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800166int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
167 loff_t *actwrite);
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700168
169/*
Rob Clark4bbcc962017-09-09 13:15:55 -0400170 * Directory entry types, matches the subset of DT_x in posix readdir()
171 * which apply to u-boot.
172 */
173#define FS_DT_DIR 4 /* directory */
174#define FS_DT_REG 8 /* regular file */
175#define FS_DT_LNK 10 /* symbolic link */
176
Miquel Raynal2ac0baa2022-06-09 16:02:06 +0200177#define FS_DIRENT_NAME_LEN 256
178
Heinrich Schuchardt13c11c62021-05-15 22:06:16 +0200179/**
180 * struct fs_dirent - directory entry
181 *
182 * A directory entry, returned by fs_readdir(). Returns information
Rob Clark4bbcc962017-09-09 13:15:55 -0400183 * about the file/directory at the current directory entry position.
184 */
185struct fs_dirent {
Heinrich Schuchardt13c11c62021-05-15 22:06:16 +0200186 /** @type: one of FS_DT_x (not a mask) */
187 unsigned int type;
188 /** @size: file size */
189 loff_t size;
190 /** @flags: attribute flags (FS_ATTR_*) */
191 u32 attr;
192 /** create_time: time of creation */
193 struct rtc_time create_time;
194 /** access_time: time of last access */
195 struct rtc_time access_time;
196 /** change_time: time of last modification */
197 struct rtc_time change_time;
198 /** name: file name */
Miquel Raynal2ac0baa2022-06-09 16:02:06 +0200199 char name[FS_DIRENT_NAME_LEN];
Rob Clark4bbcc962017-09-09 13:15:55 -0400200};
201
202/* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
203struct fs_dir_stream {
204 /* private to fs. layer: */
205 struct blk_desc *desc;
206 int part;
207};
208
209/*
210 * fs_opendir - Open a directory
211 *
212 * @filename: the path to directory to open
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100213 * Return: a pointer to the directory stream or NULL on error and errno
Rob Clark4bbcc962017-09-09 13:15:55 -0400214 * set appropriately
215 */
216struct fs_dir_stream *fs_opendir(const char *filename);
217
218/*
219 * fs_readdir - Read the next directory entry in the directory stream.
220 *
221 * Works in an analogous way to posix readdir(). The previously returned
222 * directory entry is no longer valid after calling fs_readdir() again.
223 * After fs_closedir() is called, the returned directory entry is no
224 * longer valid.
225 *
226 * @dirs: the directory stream
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100227 * Return: the next directory entry (only valid until next fs_readdir() or
Rob Clark4bbcc962017-09-09 13:15:55 -0400228 * fs_closedir() call, do not attempt to free()) or NULL if the end of
229 * the directory is reached.
230 */
231struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
232
233/*
234 * fs_closedir - close a directory stream
235 *
236 * @dirs: the directory stream
237 */
238void fs_closedir(struct fs_dir_stream *dirs);
239
240/*
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900241 * fs_unlink - delete a file or directory
242 *
243 * If a given name is a directory, it will be deleted only if it's empty
244 *
245 * @filename: Name of file or directory to delete
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100246 * Return: 0 on success, -1 on error conditions
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900247 */
248int fs_unlink(const char *filename);
249
250/*
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900251 * fs_mkdir - Create a directory
252 *
253 * @filename: Name of directory to create
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100254 * Return: 0 on success, -1 on error conditions
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900255 */
256int fs_mkdir(const char *filename);
257
258/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000259 * Common implementation for various filesystem commands, optionally limited
260 * to a specific filesystem type via the fstype parameter.
261 */
Simon Glass09140112020-05-10 11:40:03 -0600262int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
263 int fstype);
264int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
265 int fstype);
266int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
267 int fstype);
Stephen Warren61529162014-02-03 13:21:00 -0700268int file_exists(const char *dev_type, const char *dev_part, const char *file,
269 int fstype);
Simon Glass09140112020-05-10 11:40:03 -0600270int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
271 int fstype);
272int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
273 int fstype);
274int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
275 int fstype);
276int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100277 int fstype);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000278
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100279/*
280 * Determine the UUID of the specified filesystem and print it. Optionally it is
281 * possible to store the UUID directly in env.
282 */
Simon Glass09140112020-05-10 11:40:03 -0600283int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
284 int fstype);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100285
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100286/*
287 * Determine the type of the specified filesystem and print it. Optionally it is
288 * possible to store the type directly in env.
289 */
Simon Glass09140112020-05-10 11:40:03 -0600290int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100291
Niel Fourie2280fa52020-03-24 16:17:04 +0100292/**
293 * do_fs_types - List supported filesystems.
294 *
295 * @cmdtp: Command information for fstypes
296 * @flag: Command flags (CMD_FLAG_...)
297 * @argc: Number of arguments
298 * @argv: List of arguments
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100299 * Return: result (see enum command_ret_t)
Niel Fourie2280fa52020-03-24 16:17:04 +0100300 */
301int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
302
Stephen Warren045fa1e2012-10-22 06:43:51 +0000303#endif /* _FS_H */