blob: 63727567cccfb2e0f9386ea96cf1d607a7f120f3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0 */
Stephen Warreneefbc3f2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warreneefbc3f2012-10-22 06:43:51 +00004 */
5#ifndef _FS_H
6#define _FS_H
7
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02008#include <rtc.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00009
Simon Glassed38aef2020-05-10 11:40:03 -060010struct cmd_tbl;
11
Stephen Warreneefbc3f2012-10-22 06:43:51 +000012#define FS_TYPE_ANY 0
13#define FS_TYPE_FAT 1
14#define FS_TYPE_EXT 2
Simon Glass11842872012-12-26 09:53:35 +000015#define FS_TYPE_SANDBOX 3
Hans de Goede690c7962015-09-17 18:46:58 -040016#define FS_TYPE_UBIFS 4
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020017#define FS_TYPE_BTRFS 5
Joao Marcos Costa29da3742020-07-30 15:33:47 +020018#define FS_TYPE_SQUASHFS 6
Huang Jianan024fb2f2022-02-26 15:05:47 +080019#define FS_TYPE_EROFS 7
Sean Andersonc678b322022-03-22 16:59:20 -040020#define FS_TYPE_SEMIHOSTING 8
Stephen Warreneefbc3f2012-10-22 06:43:51 +000021
Simon Glass655306c2020-05-10 11:39:58 -060022struct blk_desc;
23
Simon Glass1b3f75f2019-12-28 10:44:44 -070024/**
25 * do_fat_fsload - Run the fatload command
26 *
27 * @cmdtp: Command information for fatload
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +020028 * @flag: Command flags (CMD_FLAG\_...)
Simon Glass1b3f75f2019-12-28 10:44:44 -070029 * @argc: Number of arguments
30 * @argv: List of arguments
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010031 * Return: result (see enum command_ret_t)
Simon Glass1b3f75f2019-12-28 10:44:44 -070032 */
Simon Glassed38aef2020-05-10 11:40:03 -060033int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
34 char *const argv[]);
Simon Glass1b3f75f2019-12-28 10:44:44 -070035
36/**
37 * do_ext2load - Run the ext2load command
38 *
39 * @cmdtp: Command information for ext2load
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +020040 * @flag: Command flags (CMD_FLAG\_...)
Simon Glass1b3f75f2019-12-28 10:44:44 -070041 * @argc: Number of arguments
42 * @argv: List of arguments
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010043 * Return: result (see enum command_ret_t)
Simon Glass1b3f75f2019-12-28 10:44:44 -070044 */
Simon Glassed38aef2020-05-10 11:40:03 -060045int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Simon Glass1b3f75f2019-12-28 10:44:44 -070046
Stephen Warreneefbc3f2012-10-22 06:43:51 +000047/*
Roger Knecht224d3c32022-08-25 12:12:01 +000048 * Tell the fs layer which block device and partition to use for future
Stephen Warreneefbc3f2012-10-22 06:43:51 +000049 * commands. This also internally identifies the filesystem that is present
50 * within the partition. The identification process may be limited to a
51 * specific filesystem type by passing FS_* in the fstype parameter.
52 *
53 * Returns 0 on success.
54 * Returns non-zero if there is an error accessing the disk or partition, or
55 * no known filesystem type could be recognized on it.
56 */
57int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
58
Simon Glass896ed4b2022-04-24 23:31:04 -060059/**
60 * fs_set_type() - Tell fs layer which filesystem type is used
61 *
62 * This is needed when reading from a non-block device such as sandbox. It does
63 * a similar job to fs_set_blk_dev() but just sets the filesystem type instead
64 * of detecting it and loading it on the block device
65 *
66 * @type: Filesystem type to use (FS_TYPE...)
67 */
68void fs_set_type(int type);
69
Stephen Warreneefbc3f2012-10-22 06:43:51 +000070/*
Rob Clark2b7bfd92017-09-09 13:15:55 -040071 * fs_set_blk_dev_with_part - Set current block device + partition
72 *
73 * Similar to fs_set_blk_dev(), but useful for cases where you already
74 * know the blk_desc and part number.
75 *
76 * Returns 0 on success.
77 * Returns non-zero if invalid partition or error accessing the disk.
78 */
79int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
80
Alex Kiernan3a6163a2018-05-29 15:30:50 +000081/**
AKASHI Takahiroca8e3772019-10-07 14:59:35 +090082 * fs_close() - Unset current block device and partition
83 *
Heinrich Schuchardt63a57522019-10-13 10:26:26 +020084 * fs_close() closes the connection to a file system opened with either
85 * fs_set_blk_dev() or fs_set_dev_with_part().
86 *
87 * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
88 * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
89 * fs_unlink().
AKASHI Takahiroca8e3772019-10-07 14:59:35 +090090 */
91void fs_close(void);
92
93/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +090094 * fs_get_type() - Get type of current filesystem
95 *
96 * Return: filesystem type
97 *
98 * Returns filesystem type representing the current filesystem, or
99 * FS_TYPE_ANY for any unrecognised filesystem.
100 */
101int fs_get_type(void);
102
103/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000104 * fs_get_type_name() - Get type of current filesystem
105 *
106 * Return: Pointer to filesystem name
107 *
108 * Returns a string describing the current filesystem, or the sentinel
109 * "unsupported" for any unrecognised filesystem.
110 */
111const char *fs_get_type_name(void);
112
Rob Clark2b7bfd92017-09-09 13:15:55 -0400113/*
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000114 * Print the list of files on the partition previously set by fs_set_blk_dev(),
115 * in directory "dirname".
116 *
117 * Returns 0 on success. Returns non-zero on error.
118 */
119int fs_ls(const char *dirname);
120
121/*
Stephen Warrend008fbb2014-02-03 13:21:00 -0700122 * Determine whether a file exists
123 *
124 * Returns 1 if the file exists, 0 if it doesn't exist.
125 */
126int fs_exists(const char *filename);
127
128/*
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800129 * fs_size - Determine a file's size
Stephen Warren3eb58f52014-06-11 12:47:26 -0600130 *
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800131 * @filename: Name of the file
132 * @size: Size of file
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100133 * Return: 0 if ok with valid *size, negative on error
Stephen Warren3eb58f52014-06-11 12:47:26 -0600134 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800135int fs_size(const char *filename, loff_t *size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600136
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200137/**
138 * fs_read() - read file from the partition previously set by fs_set_blk_dev()
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000139 *
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200140 * Note that not all filesystem drivers support either or both of offset != 0
141 * and len != 0.
142 *
143 * @filename: full path of the file to read from
144 * @addr: address of the buffer to write to
145 * @offset: offset in the file from where to start reading
146 * @len: the number of bytes to read. Use 0 to read entire file.
147 * @actread: returns the actual number of bytes read
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200148 * Return: 0 if OK with valid @actread, -1 on error conditions
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000149 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800150int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
151 loff_t *actread);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000152
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200153/**
154 * fs_write() - write file to the partition previously set by fs_set_blk_dev()
155 *
156 * Note that not all filesystem drivers support offset != 0.
Stephen Warren9df25802014-02-03 13:20:59 -0700157 *
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200158 * @filename: full path of the file to write to
159 * @addr: address of the buffer to read from
160 * @offset: offset in the file from where to start writing
161 * @len: the number of bytes to write
162 * @actwrite: returns the actual number of bytes written
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200163 * Return: 0 if OK with valid @actwrite, -1 on error conditions
Stephen Warren9df25802014-02-03 13:20:59 -0700164 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800165int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
166 loff_t *actwrite);
Stephen Warren9df25802014-02-03 13:20:59 -0700167
168/*
Rob Clark2b7bfd92017-09-09 13:15:55 -0400169 * Directory entry types, matches the subset of DT_x in posix readdir()
170 * which apply to u-boot.
171 */
172#define FS_DT_DIR 4 /* directory */
173#define FS_DT_REG 8 /* regular file */
174#define FS_DT_LNK 10 /* symbolic link */
175
Miquel Raynala8e95fd2022-06-09 16:02:06 +0200176#define FS_DIRENT_NAME_LEN 256
177
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200178/**
179 * struct fs_dirent - directory entry
180 *
181 * A directory entry, returned by fs_readdir(). Returns information
Rob Clark2b7bfd92017-09-09 13:15:55 -0400182 * about the file/directory at the current directory entry position.
183 */
184struct fs_dirent {
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200185 /** @type: one of FS_DT_x (not a mask) */
186 unsigned int type;
187 /** @size: file size */
188 loff_t size;
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200189 /** @attr: attribute flags (FS_ATTR_*) */
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200190 u32 attr;
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200191 /** @create_time: time of creation */
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200192 struct rtc_time create_time;
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200193 /** @access_time: time of last access */
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200194 struct rtc_time access_time;
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200195 /** @change_time: time of last modification */
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200196 struct rtc_time change_time;
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200197 /** @name: file name */
Miquel Raynala8e95fd2022-06-09 16:02:06 +0200198 char name[FS_DIRENT_NAME_LEN];
Rob Clark2b7bfd92017-09-09 13:15:55 -0400199};
200
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200201/**
202 * struct fs_dir_stream - Structure representing an opened directory
203 *
204 * Struct fs_dir_stream should be treated opaque to the user of fs layer.
205 * The fields @desc and @part are used by the fs layer.
206 * File system drivers pass additional private fields with the pointers
207 * to this structure.
208 *
209 * @desc: block device descriptor
210 * @part: partition number
211 */
Rob Clark2b7bfd92017-09-09 13:15:55 -0400212struct fs_dir_stream {
Rob Clark2b7bfd92017-09-09 13:15:55 -0400213 struct blk_desc *desc;
214 int part;
215};
216
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200217/**
Rob Clark2b7bfd92017-09-09 13:15:55 -0400218 * fs_opendir - Open a directory
219 *
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200220 * .. note::
221 * The returned struct fs_dir_stream should be treated opaque to the
222 * user of the fs layer.
223 *
224 * @filename: path to the directory to open
225 * Return:
226 * A pointer to the directory stream or NULL on error and errno set
227 * appropriately
Rob Clark2b7bfd92017-09-09 13:15:55 -0400228 */
229struct fs_dir_stream *fs_opendir(const char *filename);
230
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200231/**
Rob Clark2b7bfd92017-09-09 13:15:55 -0400232 * fs_readdir - Read the next directory entry in the directory stream.
233 *
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200234 * fs_readir works in an analogous way to posix readdir().
235 * The previously returned directory entry is no longer valid after calling
236 * fs_readdir() again.
Rob Clark2b7bfd92017-09-09 13:15:55 -0400237 * After fs_closedir() is called, the returned directory entry is no
238 * longer valid.
239 *
240 * @dirs: the directory stream
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200241 * Return:
242 * the next directory entry (only valid until next fs_readdir() or
243 * fs_closedir() call, do not attempt to free()) or NULL if the end of
244 * the directory is reached.
Rob Clark2b7bfd92017-09-09 13:15:55 -0400245 */
246struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
247
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200248/**
Rob Clark2b7bfd92017-09-09 13:15:55 -0400249 * fs_closedir - close a directory stream
250 *
251 * @dirs: the directory stream
252 */
253void fs_closedir(struct fs_dir_stream *dirs);
254
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200255/**
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900256 * fs_unlink - delete a file or directory
257 *
258 * If a given name is a directory, it will be deleted only if it's empty
259 *
260 * @filename: Name of file or directory to delete
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100261 * Return: 0 on success, -1 on error conditions
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900262 */
263int fs_unlink(const char *filename);
264
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200265/**
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900266 * fs_mkdir - Create a directory
267 *
268 * @filename: Name of directory to create
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100269 * Return: 0 on success, -1 on error conditions
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900270 */
271int fs_mkdir(const char *filename);
272
273/*
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000274 * Common implementation for various filesystem commands, optionally limited
275 * to a specific filesystem type via the fstype parameter.
276 */
Simon Glassed38aef2020-05-10 11:40:03 -0600277int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
278 int fstype);
279int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
280 int fstype);
281int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
282 int fstype);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700283int file_exists(const char *dev_type, const char *dev_part, const char *file,
284 int fstype);
Simon Glassed38aef2020-05-10 11:40:03 -0600285int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
286 int fstype);
287int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
288 int fstype);
289int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
290 int fstype);
291int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100292 int fstype);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000293
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100294/*
295 * Determine the UUID of the specified filesystem and print it. Optionally it is
296 * possible to store the UUID directly in env.
297 */
Simon Glassed38aef2020-05-10 11:40:03 -0600298int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
299 int fstype);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100300
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100301/*
302 * Determine the type of the specified filesystem and print it. Optionally it is
303 * possible to store the type directly in env.
304 */
Simon Glassed38aef2020-05-10 11:40:03 -0600305int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100306
Niel Fourie9252d702020-03-24 16:17:04 +0100307/**
308 * do_fs_types - List supported filesystems.
309 *
310 * @cmdtp: Command information for fstypes
Heinrich Schuchardt9fe85582024-10-24 11:15:28 +0200311 * @flag: Command flags (CMD_FLAG\_...)
Niel Fourie9252d702020-03-24 16:17:04 +0100312 * @argc: Number of arguments
313 * @argv: List of arguments
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100314 * Return: result (see enum command_ret_t)
Niel Fourie9252d702020-03-24 16:17:04 +0100315 */
316int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
317
Simon Glasse6c49fc2023-06-01 10:22:38 -0600318/**
319 * fs_read_alloc() - Allocate space for a file and read it
320 *
321 * You must call fs_set_blk_dev() or a similar function before calling this,
322 * since that sets up the block device to use.
323 *
324 * The file is terminated with a nul character
325 *
326 * @fname: Filename to read
327 * @size: Size of file to read (must be correct!)
328 * @align: Alignment to use for memory allocation (0 for default)
329 * @bufp: On success, returns the allocated buffer with the nul-terminated file
330 * in it
331 * Return: 0 if OK, -ENOMEM if out of memory, -EIO if read failed
332 */
333int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp);
334
335/**
336 * fs_load_alloc() - Load a file into allocated space
337 *
338 * The file is terminated with a nul character
339 *
340 * @ifname: Interface name to read from (e.g. "mmc")
341 * @dev_part_str: Device and partition string (e.g. "1:2")
342 * @fname: Filename to read
343 * @max_size: Maximum allowed size for the file (use 0 for 1GB)
344 * @align: Alignment to use for memory allocation (0 for default)
345 * @bufp: On success, returns the allocated buffer with the nul-terminated file
346 * in it
347 * @sizep: On success, returns the size of the file
348 * Return: 0 if OK, -ENOMEM if out of memory, -ENOENT if the file does not
349 * exist, -ENOMEDIUM if the device does not exist, -E2BIG if the file is too
350 * large (greater than @max_size), -EIO if read failed
351 */
352int fs_load_alloc(const char *ifname, const char *dev_part_str,
353 const char *fname, ulong max_size, ulong align, void **bufp,
354 ulong *sizep);
355
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000356#endif /* _FS_H */