fs: export fs_close()
[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
10 #define FS_TYPE_ANY     0
11 #define FS_TYPE_FAT     1
12 #define FS_TYPE_EXT     2
13 #define FS_TYPE_SANDBOX 3
14 #define FS_TYPE_UBIFS   4
15 #define FS_TYPE_BTRFS   5
16
17 /*
18  * Tell the fs layer which block device an partition to use for future
19  * commands. This also internally identifies the filesystem that is present
20  * within the partition. The identification process may be limited to a
21  * specific filesystem type by passing FS_* in the fstype parameter.
22  *
23  * Returns 0 on success.
24  * Returns non-zero if there is an error accessing the disk or partition, or
25  * no known filesystem type could be recognized on it.
26  */
27 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
28
29 /*
30  * fs_set_blk_dev_with_part - Set current block device + partition
31  *
32  * Similar to fs_set_blk_dev(), but useful for cases where you already
33  * know the blk_desc and part number.
34  *
35  * Returns 0 on success.
36  * Returns non-zero if invalid partition or error accessing the disk.
37  */
38 int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
39
40 /**
41  * fs_close() - Unset current block device and partition
42  *
43  * Should be paired with either fs_set_blk_dev() or fs_set_dev_with_part()
44  */
45 void fs_close(void);
46
47 /**
48  * fs_get_type_name() - Get type of current filesystem
49  *
50  * Return: Pointer to filesystem name
51  *
52  * Returns a string describing the current filesystem, or the sentinel
53  * "unsupported" for any unrecognised filesystem.
54  */
55 const char *fs_get_type_name(void);
56
57 /*
58  * Print the list of files on the partition previously set by fs_set_blk_dev(),
59  * in directory "dirname".
60  *
61  * Returns 0 on success. Returns non-zero on error.
62  */
63 int fs_ls(const char *dirname);
64
65 /*
66  * Determine whether a file exists
67  *
68  * Returns 1 if the file exists, 0 if it doesn't exist.
69  */
70 int fs_exists(const char *filename);
71
72 /*
73  * fs_size - Determine a file's size
74  *
75  * @filename: Name of the file
76  * @size: Size of file
77  * @return 0 if ok with valid *size, negative on error
78  */
79 int fs_size(const char *filename, loff_t *size);
80
81 /**
82  * fs_read() - read file from the partition previously set by fs_set_blk_dev()
83  *
84  * Note that not all filesystem drivers support either or both of offset != 0
85  * and len != 0.
86  *
87  * @filename:   full path of the file to read from
88  * @addr:       address of the buffer to write to
89  * @offset:     offset in the file from where to start reading
90  * @len:        the number of bytes to read. Use 0 to read entire file.
91  * @actread:    returns the actual number of bytes read
92  * Return:      0 if OK with valid *actread, -1 on error conditions
93  */
94 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
95             loff_t *actread);
96
97 /**
98  * fs_write() - write file to the partition previously set by fs_set_blk_dev()
99  *
100  * Note that not all filesystem drivers support offset != 0.
101  *
102  * @filename:   full path of the file to write to
103  * @addr:       address of the buffer to read from
104  * @offset:     offset in the file from where to start writing
105  * @len:        the number of bytes to write
106  * @actwrite:   returns the actual number of bytes written
107  * Return:      0 if OK with valid *actwrite, -1 on error conditions
108  */
109 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
110              loff_t *actwrite);
111
112 /*
113  * Directory entry types, matches the subset of DT_x in posix readdir()
114  * which apply to u-boot.
115  */
116 #define FS_DT_DIR  4         /* directory */
117 #define FS_DT_REG  8         /* regular file */
118 #define FS_DT_LNK  10        /* symbolic link */
119
120 /*
121  * A directory entry, returned by fs_readdir().  Returns information
122  * about the file/directory at the current directory entry position.
123  */
124 struct fs_dirent {
125         unsigned type;       /* one of FS_DT_x (not a mask) */
126         loff_t size;         /* size in bytes */
127         char name[256];
128 };
129
130 /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
131 struct fs_dir_stream {
132         /* private to fs. layer: */
133         struct blk_desc *desc;
134         int part;
135 };
136
137 /*
138  * fs_opendir - Open a directory
139  *
140  * @filename: the path to directory to open
141  * @return a pointer to the directory stream or NULL on error and errno
142  *    set appropriately
143  */
144 struct fs_dir_stream *fs_opendir(const char *filename);
145
146 /*
147  * fs_readdir - Read the next directory entry in the directory stream.
148  *
149  * Works in an analogous way to posix readdir().  The previously returned
150  * directory entry is no longer valid after calling fs_readdir() again.
151  * After fs_closedir() is called, the returned directory entry is no
152  * longer valid.
153  *
154  * @dirs: the directory stream
155  * @return the next directory entry (only valid until next fs_readdir() or
156  *    fs_closedir() call, do not attempt to free()) or NULL if the end of
157  *    the directory is reached.
158  */
159 struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
160
161 /*
162  * fs_closedir - close a directory stream
163  *
164  * @dirs: the directory stream
165  */
166 void fs_closedir(struct fs_dir_stream *dirs);
167
168 /*
169  * fs_unlink - delete a file or directory
170  *
171  * If a given name is a directory, it will be deleted only if it's empty
172  *
173  * @filename: Name of file or directory to delete
174  * @return 0 on success, -1 on error conditions
175  */
176 int fs_unlink(const char *filename);
177
178 /*
179  * fs_mkdir - Create a directory
180  *
181  * @filename: Name of directory to create
182  * @return 0 on success, -1 on error conditions
183  */
184 int fs_mkdir(const char *filename);
185
186 /*
187  * Common implementation for various filesystem commands, optionally limited
188  * to a specific filesystem type via the fstype parameter.
189  */
190 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
191                 int fstype);
192 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
193                 int fstype);
194 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
195                 int fstype);
196 int file_exists(const char *dev_type, const char *dev_part, const char *file,
197                 int fstype);
198 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
199                 int fstype);
200 int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
201                 int fstype);
202 int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
203                 int fstype);
204 int do_ln(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
205           int fstype);
206
207 /*
208  * Determine the UUID of the specified filesystem and print it. Optionally it is
209  * possible to store the UUID directly in env.
210  */
211 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
212                 int fstype);
213
214 /*
215  * Determine the type of the specified filesystem and print it. Optionally it is
216  * possible to store the type directly in env.
217  */
218 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
219
220 #endif /* _FS_H */