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