blob: 1c79e299fdd5a100d4967da7a0f921c57b73b2c5 [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
Stephen Warren045fa1e2012-10-22 06:43:51 +000020
Simon Glasse6f6f9e2020-05-10 11:39:58 -060021struct blk_desc;
22
Simon Glass14449982019-12-28 10:44:44 -070023/**
24 * do_fat_fsload - Run the fatload command
25 *
26 * @cmdtp: Command information for fatload
27 * @flag: Command flags (CMD_FLAG_...)
28 * @argc: Number of arguments
29 * @argv: List of arguments
30 * @return result (see enum command_ret_t)
31 */
Simon Glass09140112020-05-10 11:40:03 -060032int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
33 char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070034
35/**
36 * do_ext2load - Run the ext2load command
37 *
38 * @cmdtp: Command information for ext2load
39 * @flag: Command flags (CMD_FLAG_...)
40 * @argc: Number of arguments
41 * @argv: List of arguments
42 * @return result (see enum command_ret_t)
43 */
Simon Glass09140112020-05-10 11:40:03 -060044int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070045
Stephen Warren045fa1e2012-10-22 06:43:51 +000046/*
47 * Tell the fs layer which block device an partition to use for future
48 * commands. This also internally identifies the filesystem that is present
49 * within the partition. The identification process may be limited to a
50 * specific filesystem type by passing FS_* in the fstype parameter.
51 *
52 * Returns 0 on success.
53 * Returns non-zero if there is an error accessing the disk or partition, or
54 * no known filesystem type could be recognized on it.
55 */
56int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
57
58/*
Rob Clark4bbcc962017-09-09 13:15:55 -040059 * fs_set_blk_dev_with_part - Set current block device + partition
60 *
61 * Similar to fs_set_blk_dev(), but useful for cases where you already
62 * know the blk_desc and part number.
63 *
64 * Returns 0 on success.
65 * Returns non-zero if invalid partition or error accessing the disk.
66 */
67int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
68
Alex Kiernan0d488e82018-05-29 15:30:50 +000069/**
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090070 * fs_close() - Unset current block device and partition
71 *
Heinrich Schuchardte4bad9f2019-10-13 10:26:26 +020072 * fs_close() closes the connection to a file system opened with either
73 * fs_set_blk_dev() or fs_set_dev_with_part().
74 *
75 * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
76 * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
77 * fs_unlink().
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090078 */
79void fs_close(void);
80
81/**
AKASHI Takahirob7cd9562019-10-07 14:59:37 +090082 * fs_get_type() - Get type of current filesystem
83 *
84 * Return: filesystem type
85 *
86 * Returns filesystem type representing the current filesystem, or
87 * FS_TYPE_ANY for any unrecognised filesystem.
88 */
89int fs_get_type(void);
90
91/**
Alex Kiernan0d488e82018-05-29 15:30:50 +000092 * fs_get_type_name() - Get type of current filesystem
93 *
94 * Return: Pointer to filesystem name
95 *
96 * Returns a string describing the current filesystem, or the sentinel
97 * "unsupported" for any unrecognised filesystem.
98 */
99const char *fs_get_type_name(void);
100
Rob Clark4bbcc962017-09-09 13:15:55 -0400101/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000102 * Print the list of files on the partition previously set by fs_set_blk_dev(),
103 * in directory "dirname".
104 *
105 * Returns 0 on success. Returns non-zero on error.
106 */
107int fs_ls(const char *dirname);
108
109/*
Stephen Warren61529162014-02-03 13:21:00 -0700110 * Determine whether a file exists
111 *
112 * Returns 1 if the file exists, 0 if it doesn't exist.
113 */
114int fs_exists(const char *filename);
115
116/*
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800117 * fs_size - Determine a file's size
Stephen Warrencf659812014-06-11 12:47:26 -0600118 *
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800119 * @filename: Name of the file
120 * @size: Size of file
121 * @return 0 if ok with valid *size, negative on error
Stephen Warrencf659812014-06-11 12:47:26 -0600122 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800123int fs_size(const char *filename, loff_t *size);
Stephen Warrencf659812014-06-11 12:47:26 -0600124
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200125/**
126 * fs_read() - read file from the partition previously set by fs_set_blk_dev()
Stephen Warren045fa1e2012-10-22 06:43:51 +0000127 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200128 * Note that not all filesystem drivers support either or both of offset != 0
129 * and len != 0.
130 *
131 * @filename: full path of the file to read from
132 * @addr: address of the buffer to write to
133 * @offset: offset in the file from where to start reading
134 * @len: the number of bytes to read. Use 0 to read entire file.
135 * @actread: returns the actual number of bytes read
136 * Return: 0 if OK with valid *actread, -1 on error conditions
Stephen Warren045fa1e2012-10-22 06:43:51 +0000137 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800138int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
139 loff_t *actread);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000140
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200141/**
142 * fs_write() - write file to the partition previously set by fs_set_blk_dev()
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700143 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200144 * Note that not all filesystem drivers support offset != 0.
145 *
146 * @filename: full path of the file to write to
147 * @addr: address of the buffer to read from
148 * @offset: offset in the file from where to start writing
149 * @len: the number of bytes to write
150 * @actwrite: returns the actual number of bytes written
151 * Return: 0 if OK with valid *actwrite, -1 on error conditions
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700152 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800153int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
154 loff_t *actwrite);
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700155
156/*
Rob Clark4bbcc962017-09-09 13:15:55 -0400157 * Directory entry types, matches the subset of DT_x in posix readdir()
158 * which apply to u-boot.
159 */
160#define FS_DT_DIR 4 /* directory */
161#define FS_DT_REG 8 /* regular file */
162#define FS_DT_LNK 10 /* symbolic link */
163
Heinrich Schuchardt13c11c62021-05-15 22:06:16 +0200164/**
165 * struct fs_dirent - directory entry
166 *
167 * A directory entry, returned by fs_readdir(). Returns information
Rob Clark4bbcc962017-09-09 13:15:55 -0400168 * about the file/directory at the current directory entry position.
169 */
170struct fs_dirent {
Heinrich Schuchardt13c11c62021-05-15 22:06:16 +0200171 /** @type: one of FS_DT_x (not a mask) */
172 unsigned int type;
173 /** @size: file size */
174 loff_t size;
175 /** @flags: attribute flags (FS_ATTR_*) */
176 u32 attr;
177 /** create_time: time of creation */
178 struct rtc_time create_time;
179 /** access_time: time of last access */
180 struct rtc_time access_time;
181 /** change_time: time of last modification */
182 struct rtc_time change_time;
183 /** name: file name */
Rob Clark4bbcc962017-09-09 13:15:55 -0400184 char name[256];
185};
186
187/* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
188struct fs_dir_stream {
189 /* private to fs. layer: */
190 struct blk_desc *desc;
191 int part;
192};
193
194/*
195 * fs_opendir - Open a directory
196 *
197 * @filename: the path to directory to open
198 * @return a pointer to the directory stream or NULL on error and errno
199 * set appropriately
200 */
201struct fs_dir_stream *fs_opendir(const char *filename);
202
203/*
204 * fs_readdir - Read the next directory entry in the directory stream.
205 *
206 * Works in an analogous way to posix readdir(). The previously returned
207 * directory entry is no longer valid after calling fs_readdir() again.
208 * After fs_closedir() is called, the returned directory entry is no
209 * longer valid.
210 *
211 * @dirs: the directory stream
212 * @return the next directory entry (only valid until next fs_readdir() or
213 * fs_closedir() call, do not attempt to free()) or NULL if the end of
214 * the directory is reached.
215 */
216struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
217
218/*
219 * fs_closedir - close a directory stream
220 *
221 * @dirs: the directory stream
222 */
223void fs_closedir(struct fs_dir_stream *dirs);
224
225/*
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900226 * fs_unlink - delete a file or directory
227 *
228 * If a given name is a directory, it will be deleted only if it's empty
229 *
230 * @filename: Name of file or directory to delete
231 * @return 0 on success, -1 on error conditions
232 */
233int fs_unlink(const char *filename);
234
235/*
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900236 * fs_mkdir - Create a directory
237 *
238 * @filename: Name of directory to create
239 * @return 0 on success, -1 on error conditions
240 */
241int fs_mkdir(const char *filename);
242
243/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000244 * Common implementation for various filesystem commands, optionally limited
245 * to a specific filesystem type via the fstype parameter.
246 */
Simon Glass09140112020-05-10 11:40:03 -0600247int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
248 int fstype);
249int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
250 int fstype);
251int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
252 int fstype);
Stephen Warren61529162014-02-03 13:21:00 -0700253int file_exists(const char *dev_type, const char *dev_part, const char *file,
254 int fstype);
Simon Glass09140112020-05-10 11:40:03 -0600255int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
256 int fstype);
257int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
258 int fstype);
259int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
260 int fstype);
261int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100262 int fstype);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000263
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100264/*
265 * Determine the UUID of the specified filesystem and print it. Optionally it is
266 * possible to store the UUID directly in env.
267 */
Simon Glass09140112020-05-10 11:40:03 -0600268int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
269 int fstype);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100270
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100271/*
272 * Determine the type of the specified filesystem and print it. Optionally it is
273 * possible to store the type directly in env.
274 */
Simon Glass09140112020-05-10 11:40:03 -0600275int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100276
Niel Fourie2280fa52020-03-24 16:17:04 +0100277/**
278 * do_fs_types - List supported filesystems.
279 *
280 * @cmdtp: Command information for fstypes
281 * @flag: Command flags (CMD_FLAG_...)
282 * @argc: Number of arguments
283 * @argv: List of arguments
284 * @return result (see enum command_ret_t)
285 */
286int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
287
Stephen Warren045fa1e2012-10-22 06:43:51 +0000288#endif /* _FS_H */