blob: 29f737b8c2198b1dbd6595b44f1a3f63596ed82c [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>
9
Simon Glass09140112020-05-10 11:40:03 -060010struct cmd_tbl;
11
Stephen Warren045fa1e2012-10-22 06:43:51 +000012#define FS_TYPE_ANY 0
13#define FS_TYPE_FAT 1
14#define FS_TYPE_EXT 2
Simon Glass92ccc962012-12-26 09:53:35 +000015#define FS_TYPE_SANDBOX 3
Hans de Goede251cee02015-09-17 18:46:58 -040016#define FS_TYPE_UBIFS 4
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020017#define FS_TYPE_BTRFS 5
Stephen Warren045fa1e2012-10-22 06:43:51 +000018
Simon Glasse6f6f9e2020-05-10 11:39:58 -060019struct blk_desc;
20
Simon Glass14449982019-12-28 10:44:44 -070021/**
22 * do_fat_fsload - Run the fatload command
23 *
24 * @cmdtp: Command information for fatload
25 * @flag: Command flags (CMD_FLAG_...)
26 * @argc: Number of arguments
27 * @argv: List of arguments
28 * @return result (see enum command_ret_t)
29 */
Simon Glass09140112020-05-10 11:40:03 -060030int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
31 char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070032
33/**
34 * do_ext2load - Run the ext2load command
35 *
36 * @cmdtp: Command information for ext2load
37 * @flag: Command flags (CMD_FLAG_...)
38 * @argc: Number of arguments
39 * @argv: List of arguments
40 * @return result (see enum command_ret_t)
41 */
Simon Glass09140112020-05-10 11:40:03 -060042int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Simon Glass14449982019-12-28 10:44:44 -070043
Stephen Warren045fa1e2012-10-22 06:43:51 +000044/*
45 * Tell the fs layer which block device an partition to use for future
46 * commands. This also internally identifies the filesystem that is present
47 * within the partition. The identification process may be limited to a
48 * specific filesystem type by passing FS_* in the fstype parameter.
49 *
50 * Returns 0 on success.
51 * Returns non-zero if there is an error accessing the disk or partition, or
52 * no known filesystem type could be recognized on it.
53 */
54int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
55
56/*
Rob Clark4bbcc962017-09-09 13:15:55 -040057 * fs_set_blk_dev_with_part - Set current block device + partition
58 *
59 * Similar to fs_set_blk_dev(), but useful for cases where you already
60 * know the blk_desc and part number.
61 *
62 * Returns 0 on success.
63 * Returns non-zero if invalid partition or error accessing the disk.
64 */
65int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
66
Alex Kiernan0d488e82018-05-29 15:30:50 +000067/**
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090068 * fs_close() - Unset current block device and partition
69 *
Heinrich Schuchardte4bad9f2019-10-13 10:26:26 +020070 * fs_close() closes the connection to a file system opened with either
71 * fs_set_blk_dev() or fs_set_dev_with_part().
72 *
73 * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
74 * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
75 * fs_unlink().
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +090076 */
77void fs_close(void);
78
79/**
AKASHI Takahirob7cd9562019-10-07 14:59:37 +090080 * fs_get_type() - Get type of current filesystem
81 *
82 * Return: filesystem type
83 *
84 * Returns filesystem type representing the current filesystem, or
85 * FS_TYPE_ANY for any unrecognised filesystem.
86 */
87int fs_get_type(void);
88
89/**
Alex Kiernan0d488e82018-05-29 15:30:50 +000090 * fs_get_type_name() - Get type of current filesystem
91 *
92 * Return: Pointer to filesystem name
93 *
94 * Returns a string describing the current filesystem, or the sentinel
95 * "unsupported" for any unrecognised filesystem.
96 */
97const char *fs_get_type_name(void);
98
Rob Clark4bbcc962017-09-09 13:15:55 -040099/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000100 * Print the list of files on the partition previously set by fs_set_blk_dev(),
101 * in directory "dirname".
102 *
103 * Returns 0 on success. Returns non-zero on error.
104 */
105int fs_ls(const char *dirname);
106
107/*
Stephen Warren61529162014-02-03 13:21:00 -0700108 * Determine whether a file exists
109 *
110 * Returns 1 if the file exists, 0 if it doesn't exist.
111 */
112int fs_exists(const char *filename);
113
114/*
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800115 * fs_size - Determine a file's size
Stephen Warrencf659812014-06-11 12:47:26 -0600116 *
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800117 * @filename: Name of the file
118 * @size: Size of file
119 * @return 0 if ok with valid *size, negative on error
Stephen Warrencf659812014-06-11 12:47:26 -0600120 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800121int fs_size(const char *filename, loff_t *size);
Stephen Warrencf659812014-06-11 12:47:26 -0600122
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200123/**
124 * fs_read() - read file from the partition previously set by fs_set_blk_dev()
Stephen Warren045fa1e2012-10-22 06:43:51 +0000125 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200126 * Note that not all filesystem drivers support either or both of offset != 0
127 * and len != 0.
128 *
129 * @filename: full path of the file to read from
130 * @addr: address of the buffer to write to
131 * @offset: offset in the file from where to start reading
132 * @len: the number of bytes to read. Use 0 to read entire file.
133 * @actread: returns the actual number of bytes read
134 * Return: 0 if OK with valid *actread, -1 on error conditions
Stephen Warren045fa1e2012-10-22 06:43:51 +0000135 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800136int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
137 loff_t *actread);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000138
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200139/**
140 * fs_write() - write file to the partition previously set by fs_set_blk_dev()
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700141 *
Heinrich Schuchardta0e92cd2019-04-25 20:36:39 +0200142 * Note that not all filesystem drivers support offset != 0.
143 *
144 * @filename: full path of the file to write to
145 * @addr: address of the buffer to read from
146 * @offset: offset in the file from where to start writing
147 * @len: the number of bytes to write
148 * @actwrite: returns the actual number of bytes written
149 * Return: 0 if OK with valid *actwrite, -1 on error conditions
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700150 */
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800151int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
152 loff_t *actwrite);
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700153
154/*
Rob Clark4bbcc962017-09-09 13:15:55 -0400155 * Directory entry types, matches the subset of DT_x in posix readdir()
156 * which apply to u-boot.
157 */
158#define FS_DT_DIR 4 /* directory */
159#define FS_DT_REG 8 /* regular file */
160#define FS_DT_LNK 10 /* symbolic link */
161
162/*
163 * A directory entry, returned by fs_readdir(). Returns information
164 * about the file/directory at the current directory entry position.
165 */
166struct fs_dirent {
167 unsigned type; /* one of FS_DT_x (not a mask) */
168 loff_t size; /* size in bytes */
169 char name[256];
170};
171
172/* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
173struct fs_dir_stream {
174 /* private to fs. layer: */
175 struct blk_desc *desc;
176 int part;
177};
178
179/*
180 * fs_opendir - Open a directory
181 *
182 * @filename: the path to directory to open
183 * @return a pointer to the directory stream or NULL on error and errno
184 * set appropriately
185 */
186struct fs_dir_stream *fs_opendir(const char *filename);
187
188/*
189 * fs_readdir - Read the next directory entry in the directory stream.
190 *
191 * Works in an analogous way to posix readdir(). The previously returned
192 * directory entry is no longer valid after calling fs_readdir() again.
193 * After fs_closedir() is called, the returned directory entry is no
194 * longer valid.
195 *
196 * @dirs: the directory stream
197 * @return the next directory entry (only valid until next fs_readdir() or
198 * fs_closedir() call, do not attempt to free()) or NULL if the end of
199 * the directory is reached.
200 */
201struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
202
203/*
204 * fs_closedir - close a directory stream
205 *
206 * @dirs: the directory stream
207 */
208void fs_closedir(struct fs_dir_stream *dirs);
209
210/*
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900211 * fs_unlink - delete a file or directory
212 *
213 * If a given name is a directory, it will be deleted only if it's empty
214 *
215 * @filename: Name of file or directory to delete
216 * @return 0 on success, -1 on error conditions
217 */
218int fs_unlink(const char *filename);
219
220/*
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900221 * fs_mkdir - Create a directory
222 *
223 * @filename: Name of directory to create
224 * @return 0 on success, -1 on error conditions
225 */
226int fs_mkdir(const char *filename);
227
228/*
Stephen Warren045fa1e2012-10-22 06:43:51 +0000229 * Common implementation for various filesystem commands, optionally limited
230 * to a specific filesystem type via the fstype parameter.
231 */
Simon Glass09140112020-05-10 11:40:03 -0600232int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
233 int fstype);
234int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
235 int fstype);
236int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
237 int fstype);
Stephen Warren61529162014-02-03 13:21:00 -0700238int file_exists(const char *dev_type, const char *dev_part, const char *file,
239 int fstype);
Simon Glass09140112020-05-10 11:40:03 -0600240int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
241 int fstype);
242int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
243 int fstype);
244int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
245 int fstype);
246int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100247 int fstype);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000248
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100249/*
250 * Determine the UUID of the specified filesystem and print it. Optionally it is
251 * possible to store the UUID directly in env.
252 */
Simon Glass09140112020-05-10 11:40:03 -0600253int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
254 int fstype);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100255
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100256/*
257 * Determine the type of the specified filesystem and print it. Optionally it is
258 * possible to store the type directly in env.
259 */
Simon Glass09140112020-05-10 11:40:03 -0600260int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100261
Stephen Warren045fa1e2012-10-22 06:43:51 +0000262#endif /* _FS_H */