blob: e2beba36b986ad7ccd43d11f51fddf7692d43949 [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
8#include <common.h>
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +02009#include <rtc.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000010
Simon Glassed38aef2020-05-10 11:40:03 -060011struct cmd_tbl;
12
Stephen Warreneefbc3f2012-10-22 06:43:51 +000013#define FS_TYPE_ANY 0
14#define FS_TYPE_FAT 1
15#define FS_TYPE_EXT 2
Simon Glass11842872012-12-26 09:53:35 +000016#define FS_TYPE_SANDBOX 3
Hans de Goede690c7962015-09-17 18:46:58 -040017#define FS_TYPE_UBIFS 4
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020018#define FS_TYPE_BTRFS 5
Joao Marcos Costa29da3742020-07-30 15:33:47 +020019#define FS_TYPE_SQUASHFS 6
Huang Jianan024fb2f2022-02-26 15:05:47 +080020#define FS_TYPE_EROFS 7
Sean Andersonc678b322022-03-22 16:59:20 -040021#define FS_TYPE_SEMIHOSTING 8
Stephen Warreneefbc3f2012-10-22 06:43:51 +000022
Simon Glass655306c2020-05-10 11:39:58 -060023struct blk_desc;
24
Simon Glass1b3f75f2019-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 Schuchardt47b4c022022-01-19 18:05:50 +010032 * Return: result (see enum command_ret_t)
Simon Glass1b3f75f2019-12-28 10:44:44 -070033 */
Simon Glassed38aef2020-05-10 11:40:03 -060034int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
35 char *const argv[]);
Simon Glass1b3f75f2019-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 Schuchardt47b4c022022-01-19 18:05:50 +010044 * Return: result (see enum command_ret_t)
Simon Glass1b3f75f2019-12-28 10:44:44 -070045 */
Simon Glassed38aef2020-05-10 11:40:03 -060046int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Simon Glass1b3f75f2019-12-28 10:44:44 -070047
Stephen Warreneefbc3f2012-10-22 06:43:51 +000048/*
49 * Tell the fs layer which block device an partition to use for future
50 * 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
60/*
Rob Clark2b7bfd92017-09-09 13:15:55 -040061 * fs_set_blk_dev_with_part - Set current block device + partition
62 *
63 * Similar to fs_set_blk_dev(), but useful for cases where you already
64 * know the blk_desc and part number.
65 *
66 * Returns 0 on success.
67 * Returns non-zero if invalid partition or error accessing the disk.
68 */
69int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
70
Alex Kiernan3a6163a2018-05-29 15:30:50 +000071/**
AKASHI Takahiroca8e3772019-10-07 14:59:35 +090072 * fs_close() - Unset current block device and partition
73 *
Heinrich Schuchardt63a57522019-10-13 10:26:26 +020074 * fs_close() closes the connection to a file system opened with either
75 * fs_set_blk_dev() or fs_set_dev_with_part().
76 *
77 * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
78 * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
79 * fs_unlink().
AKASHI Takahiroca8e3772019-10-07 14:59:35 +090080 */
81void fs_close(void);
82
83/**
AKASHI Takahiro80ac7342019-10-07 14:59:37 +090084 * fs_get_type() - Get type of current filesystem
85 *
86 * Return: filesystem type
87 *
88 * Returns filesystem type representing the current filesystem, or
89 * FS_TYPE_ANY for any unrecognised filesystem.
90 */
91int fs_get_type(void);
92
93/**
Alex Kiernan3a6163a2018-05-29 15:30:50 +000094 * fs_get_type_name() - Get type of current filesystem
95 *
96 * Return: Pointer to filesystem name
97 *
98 * Returns a string describing the current filesystem, or the sentinel
99 * "unsupported" for any unrecognised filesystem.
100 */
101const char *fs_get_type_name(void);
102
Rob Clark2b7bfd92017-09-09 13:15:55 -0400103/*
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000104 * Print the list of files on the partition previously set by fs_set_blk_dev(),
105 * in directory "dirname".
106 *
107 * Returns 0 on success. Returns non-zero on error.
108 */
109int fs_ls(const char *dirname);
110
111/*
Stephen Warrend008fbb2014-02-03 13:21:00 -0700112 * Determine whether a file exists
113 *
114 * Returns 1 if the file exists, 0 if it doesn't exist.
115 */
116int fs_exists(const char *filename);
117
118/*
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800119 * fs_size - Determine a file's size
Stephen Warren3eb58f52014-06-11 12:47:26 -0600120 *
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800121 * @filename: Name of the file
122 * @size: Size of file
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100123 * Return: 0 if ok with valid *size, negative on error
Stephen Warren3eb58f52014-06-11 12:47:26 -0600124 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800125int fs_size(const char *filename, loff_t *size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600126
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200127/**
128 * fs_read() - read file from the partition previously set by fs_set_blk_dev()
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000129 *
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200130 * Note that not all filesystem drivers support either or both of offset != 0
131 * and len != 0.
132 *
133 * @filename: full path of the file to read from
134 * @addr: address of the buffer to write to
135 * @offset: offset in the file from where to start reading
136 * @len: the number of bytes to read. Use 0 to read entire file.
137 * @actread: returns the actual number of bytes read
138 * Return: 0 if OK with valid *actread, -1 on error conditions
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000139 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800140int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
141 loff_t *actread);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000142
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200143/**
144 * fs_write() - write file to the partition previously set by fs_set_blk_dev()
145 *
146 * Note that not all filesystem drivers support offset != 0.
Stephen Warren9df25802014-02-03 13:20:59 -0700147 *
Heinrich Schuchardt5f1d6952019-04-25 20:36:39 +0200148 * @filename: full path of the file to write to
149 * @addr: address of the buffer to read from
150 * @offset: offset in the file from where to start writing
151 * @len: the number of bytes to write
152 * @actwrite: returns the actual number of bytes written
153 * Return: 0 if OK with valid *actwrite, -1 on error conditions
Stephen Warren9df25802014-02-03 13:20:59 -0700154 */
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800155int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
156 loff_t *actwrite);
Stephen Warren9df25802014-02-03 13:20:59 -0700157
158/*
Rob Clark2b7bfd92017-09-09 13:15:55 -0400159 * Directory entry types, matches the subset of DT_x in posix readdir()
160 * which apply to u-boot.
161 */
162#define FS_DT_DIR 4 /* directory */
163#define FS_DT_REG 8 /* regular file */
164#define FS_DT_LNK 10 /* symbolic link */
165
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200166/**
167 * struct fs_dirent - directory entry
168 *
169 * A directory entry, returned by fs_readdir(). Returns information
Rob Clark2b7bfd92017-09-09 13:15:55 -0400170 * about the file/directory at the current directory entry position.
171 */
172struct fs_dirent {
Heinrich Schuchardt236a2d12021-05-15 22:06:16 +0200173 /** @type: one of FS_DT_x (not a mask) */
174 unsigned int type;
175 /** @size: file size */
176 loff_t size;
177 /** @flags: attribute flags (FS_ATTR_*) */
178 u32 attr;
179 /** create_time: time of creation */
180 struct rtc_time create_time;
181 /** access_time: time of last access */
182 struct rtc_time access_time;
183 /** change_time: time of last modification */
184 struct rtc_time change_time;
185 /** name: file name */
Rob Clark2b7bfd92017-09-09 13:15:55 -0400186 char name[256];
187};
188
189/* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
190struct fs_dir_stream {
191 /* private to fs. layer: */
192 struct blk_desc *desc;
193 int part;
194};
195
196/*
197 * fs_opendir - Open a directory
198 *
199 * @filename: the path to directory to open
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100200 * Return: a pointer to the directory stream or NULL on error and errno
Rob Clark2b7bfd92017-09-09 13:15:55 -0400201 * set appropriately
202 */
203struct fs_dir_stream *fs_opendir(const char *filename);
204
205/*
206 * fs_readdir - Read the next directory entry in the directory stream.
207 *
208 * Works in an analogous way to posix readdir(). The previously returned
209 * directory entry is no longer valid after calling fs_readdir() again.
210 * After fs_closedir() is called, the returned directory entry is no
211 * longer valid.
212 *
213 * @dirs: the directory stream
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100214 * Return: the next directory entry (only valid until next fs_readdir() or
Rob Clark2b7bfd92017-09-09 13:15:55 -0400215 * fs_closedir() call, do not attempt to free()) or NULL if the end of
216 * the directory is reached.
217 */
218struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
219
220/*
221 * fs_closedir - close a directory stream
222 *
223 * @dirs: the directory stream
224 */
225void fs_closedir(struct fs_dir_stream *dirs);
226
227/*
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900228 * fs_unlink - delete a file or directory
229 *
230 * If a given name is a directory, it will be deleted only if it's empty
231 *
232 * @filename: Name of file or directory to delete
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100233 * Return: 0 on success, -1 on error conditions
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900234 */
235int fs_unlink(const char *filename);
236
237/*
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900238 * fs_mkdir - Create a directory
239 *
240 * @filename: Name of directory to create
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100241 * Return: 0 on success, -1 on error conditions
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900242 */
243int fs_mkdir(const char *filename);
244
245/*
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000246 * Common implementation for various filesystem commands, optionally limited
247 * to a specific filesystem type via the fstype parameter.
248 */
Simon Glassed38aef2020-05-10 11:40:03 -0600249int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
250 int fstype);
251int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
252 int fstype);
253int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
254 int fstype);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700255int file_exists(const char *dev_type, const char *dev_part, const char *file,
256 int fstype);
Simon Glassed38aef2020-05-10 11:40:03 -0600257int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
258 int fstype);
259int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
260 int fstype);
261int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
262 int fstype);
263int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotbc237482019-02-13 12:15:26 +0100264 int fstype);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000265
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100266/*
267 * Determine the UUID of the specified filesystem and print it. Optionally it is
268 * possible to store the UUID directly in env.
269 */
Simon Glassed38aef2020-05-10 11:40:03 -0600270int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
271 int fstype);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100272
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100273/*
274 * Determine the type of the specified filesystem and print it. Optionally it is
275 * possible to store the type directly in env.
276 */
Simon Glassed38aef2020-05-10 11:40:03 -0600277int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100278
Niel Fourie9252d702020-03-24 16:17:04 +0100279/**
280 * do_fs_types - List supported filesystems.
281 *
282 * @cmdtp: Command information for fstypes
283 * @flag: Command flags (CMD_FLAG_...)
284 * @argc: Number of arguments
285 * @argv: List of arguments
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100286 * Return: result (see enum command_ret_t)
Niel Fourie9252d702020-03-24 16:17:04 +0100287 */
288int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
289
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000290#endif /* _FS_H */