Merge https://gitlab.denx.de/u-boot/custodians/u-boot-mpc85xx
[platform/kernel/u-boot.git] / include / fs.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4  */
5 #ifndef _FS_H
6 #define _FS_H
7
8 #include <common.h>
9 #include <rtc.h>
10
11 struct cmd_tbl;
12
13 #define FS_TYPE_ANY     0
14 #define FS_TYPE_FAT     1
15 #define FS_TYPE_EXT     2
16 #define FS_TYPE_SANDBOX 3
17 #define FS_TYPE_UBIFS   4
18 #define FS_TYPE_BTRFS   5
19 #define FS_TYPE_SQUASHFS 6
20 #define FS_TYPE_EROFS   7
21 #define FS_TYPE_SEMIHOSTING 8
22
23 struct blk_desc;
24
25 /**
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
32  * Return: result (see enum command_ret_t)
33  */
34 int do_fat_fsload(struct cmd_tbl *cmdtp, int flag, int argc,
35                   char *const argv[]);
36
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
44  * Return: result (see enum command_ret_t)
45  */
46 int do_ext2load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
47
48 /*
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  */
58 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
59
60 /**
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  */
69 void fs_set_type(int type);
70
71 /*
72  * 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  */
80 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
81
82 /**
83  * fs_close() - Unset current block device and partition
84  *
85  * 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().
91  */
92 void fs_close(void);
93
94 /**
95  * 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  */
102 int fs_get_type(void);
103
104 /**
105  * 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  */
112 const char *fs_get_type_name(void);
113
114 /*
115  * 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  */
120 int fs_ls(const char *dirname);
121
122 /*
123  * Determine whether a file exists
124  *
125  * Returns 1 if the file exists, 0 if it doesn't exist.
126  */
127 int fs_exists(const char *filename);
128
129 /*
130  * fs_size - Determine a file's size
131  *
132  * @filename: Name of the file
133  * @size: Size of file
134  * Return: 0 if ok with valid *size, negative on error
135  */
136 int fs_size(const char *filename, loff_t *size);
137
138 /**
139  * fs_read() - read file from the partition previously set by fs_set_blk_dev()
140  *
141  * 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
150  */
151 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
152             loff_t *actread);
153
154 /**
155  * fs_write() - write file to the partition previously set by fs_set_blk_dev()
156  *
157  * 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
165  */
166 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
167              loff_t *actwrite);
168
169 /*
170  * 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
177 /**
178  * struct fs_dirent - directory entry
179  *
180  * A directory entry, returned by fs_readdir(). Returns information
181  * about the file/directory at the current directory entry position.
182  */
183 struct fs_dirent {
184         /** @type:              one of FS_DT_x (not a mask) */
185         unsigned int type;
186         /** @size:              file size */
187         loff_t size;
188         /** @flags:             attribute flags (FS_ATTR_*) */
189         u32 attr;
190         /** create_time:        time of creation */
191         struct rtc_time create_time;
192         /** access_time:        time of last access */
193         struct rtc_time access_time;
194         /** change_time:        time of last modification */
195         struct rtc_time change_time;
196         /** name:               file name */
197         char name[256];
198 };
199
200 /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
201 struct fs_dir_stream {
202         /* private to fs. layer: */
203         struct blk_desc *desc;
204         int part;
205 };
206
207 /*
208  * fs_opendir - Open a directory
209  *
210  * @filename: the path to directory to open
211  * Return: a pointer to the directory stream or NULL on error and errno
212  *    set appropriately
213  */
214 struct fs_dir_stream *fs_opendir(const char *filename);
215
216 /*
217  * fs_readdir - Read the next directory entry in the directory stream.
218  *
219  * Works in an analogous way to posix readdir().  The previously returned
220  * directory entry is no longer valid after calling fs_readdir() again.
221  * After fs_closedir() is called, the returned directory entry is no
222  * longer valid.
223  *
224  * @dirs: the directory stream
225  * Return: the next directory entry (only valid until next fs_readdir() or
226  *    fs_closedir() call, do not attempt to free()) or NULL if the end of
227  *    the directory is reached.
228  */
229 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
230
231 /*
232  * fs_closedir - close a directory stream
233  *
234  * @dirs: the directory stream
235  */
236 void fs_closedir(struct fs_dir_stream *dirs);
237
238 /*
239  * fs_unlink - delete a file or directory
240  *
241  * If a given name is a directory, it will be deleted only if it's empty
242  *
243  * @filename: Name of file or directory to delete
244  * Return: 0 on success, -1 on error conditions
245  */
246 int fs_unlink(const char *filename);
247
248 /*
249  * fs_mkdir - Create a directory
250  *
251  * @filename: Name of directory to create
252  * Return: 0 on success, -1 on error conditions
253  */
254 int fs_mkdir(const char *filename);
255
256 /*
257  * Common implementation for various filesystem commands, optionally limited
258  * to a specific filesystem type via the fstype parameter.
259  */
260 int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
261             int fstype);
262 int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
263             int fstype);
264 int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
265           int fstype);
266 int file_exists(const char *dev_type, const char *dev_part, const char *file,
267                 int fstype);
268 int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
269             int fstype);
270 int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
271           int fstype);
272 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
273              int fstype);
274 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
275           int fstype);
276
277 /*
278  * Determine the UUID of the specified filesystem and print it. Optionally it is
279  * possible to store the UUID directly in env.
280  */
281 int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
282                int fstype);
283
284 /*
285  * Determine the type of the specified filesystem and print it. Optionally it is
286  * possible to store the type directly in env.
287  */
288 int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
289
290 /**
291  * do_fs_types - List supported filesystems.
292  *
293  * @cmdtp: Command information for fstypes
294  * @flag: Command flags (CMD_FLAG_...)
295  * @argc: Number of arguments
296  * @argv: List of arguments
297  * Return: result (see enum command_ret_t)
298  */
299 int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
300
301 #endif /* _FS_H */