xref: /openbmc/u-boot/include/fs.h (revision 592cd5de)
183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0 */
2045fa1e1SStephen Warren /*
3045fa1e1SStephen Warren  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
4045fa1e1SStephen Warren  */
5045fa1e1SStephen Warren #ifndef _FS_H
6045fa1e1SStephen Warren #define _FS_H
7045fa1e1SStephen Warren 
8045fa1e1SStephen Warren #include <common.h>
9045fa1e1SStephen Warren 
10045fa1e1SStephen Warren #define FS_TYPE_ANY	0
11045fa1e1SStephen Warren #define FS_TYPE_FAT	1
12045fa1e1SStephen Warren #define FS_TYPE_EXT	2
1392ccc96bSSimon Glass #define FS_TYPE_SANDBOX	3
14251cee0dSHans de Goede #define FS_TYPE_UBIFS	4
150c936ee3SMarek Behún #define FS_TYPE_BTRFS	5
16045fa1e1SStephen Warren 
17045fa1e1SStephen Warren /*
18045fa1e1SStephen Warren  * Tell the fs layer which block device an partition to use for future
19045fa1e1SStephen Warren  * commands. This also internally identifies the filesystem that is present
20045fa1e1SStephen Warren  * within the partition. The identification process may be limited to a
21045fa1e1SStephen Warren  * specific filesystem type by passing FS_* in the fstype parameter.
22045fa1e1SStephen Warren  *
23045fa1e1SStephen Warren  * Returns 0 on success.
24045fa1e1SStephen Warren  * Returns non-zero if there is an error accessing the disk or partition, or
25045fa1e1SStephen Warren  * no known filesystem type could be recognized on it.
26045fa1e1SStephen Warren  */
27045fa1e1SStephen Warren int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype);
28045fa1e1SStephen Warren 
29045fa1e1SStephen Warren /*
304bbcc965SRob Clark  * fs_set_blk_dev_with_part - Set current block device + partition
314bbcc965SRob Clark  *
324bbcc965SRob Clark  * Similar to fs_set_blk_dev(), but useful for cases where you already
334bbcc965SRob Clark  * know the blk_desc and part number.
344bbcc965SRob Clark  *
354bbcc965SRob Clark  * Returns 0 on success.
364bbcc965SRob Clark  * Returns non-zero if invalid partition or error accessing the disk.
374bbcc965SRob Clark  */
384bbcc965SRob Clark int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
394bbcc965SRob Clark 
400d488e8fSAlex Kiernan /**
410d488e8fSAlex Kiernan  * fs_get_type_name() - Get type of current filesystem
420d488e8fSAlex Kiernan  *
430d488e8fSAlex Kiernan  * Return: Pointer to filesystem name
440d488e8fSAlex Kiernan  *
450d488e8fSAlex Kiernan  * Returns a string describing the current filesystem, or the sentinel
460d488e8fSAlex Kiernan  * "unsupported" for any unrecognised filesystem.
470d488e8fSAlex Kiernan  */
480d488e8fSAlex Kiernan const char *fs_get_type_name(void);
490d488e8fSAlex Kiernan 
504bbcc965SRob Clark /*
51045fa1e1SStephen Warren  * Print the list of files on the partition previously set by fs_set_blk_dev(),
52045fa1e1SStephen Warren  * in directory "dirname".
53045fa1e1SStephen Warren  *
54045fa1e1SStephen Warren  * Returns 0 on success. Returns non-zero on error.
55045fa1e1SStephen Warren  */
56045fa1e1SStephen Warren int fs_ls(const char *dirname);
57045fa1e1SStephen Warren 
58045fa1e1SStephen Warren /*
596152916aSStephen Warren  * Determine whether a file exists
606152916aSStephen Warren  *
616152916aSStephen Warren  * Returns 1 if the file exists, 0 if it doesn't exist.
626152916aSStephen Warren  */
636152916aSStephen Warren int fs_exists(const char *filename);
646152916aSStephen Warren 
656152916aSStephen Warren /*
66d455d878SSuriyan Ramasami  * fs_size - Determine a file's size
67cf659819SStephen Warren  *
68d455d878SSuriyan Ramasami  * @filename: Name of the file
69d455d878SSuriyan Ramasami  * @size: Size of file
70d455d878SSuriyan Ramasami  * @return 0 if ok with valid *size, negative on error
71cf659819SStephen Warren  */
72d455d878SSuriyan Ramasami int fs_size(const char *filename, loff_t *size);
73cf659819SStephen Warren 
74cf659819SStephen Warren /*
75d455d878SSuriyan Ramasami  * fs_read - Read file from the partition previously set by fs_set_blk_dev()
76d455d878SSuriyan Ramasami  * Note that not all filesystem types support either/both offset!=0 or len!=0.
77045fa1e1SStephen Warren  *
78d455d878SSuriyan Ramasami  * @filename: Name of file to read from
79d455d878SSuriyan Ramasami  * @addr: The address to read into
80d455d878SSuriyan Ramasami  * @offset: The offset in file to read from
81d455d878SSuriyan Ramasami  * @len: The number of bytes to read. Maybe 0 to read entire file
82d455d878SSuriyan Ramasami  * @actread: Returns the actual number of bytes read
83d455d878SSuriyan Ramasami  * @return 0 if ok with valid *actread, -1 on error conditions
84045fa1e1SStephen Warren  */
85d455d878SSuriyan Ramasami int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
86d455d878SSuriyan Ramasami 	    loff_t *actread);
87045fa1e1SStephen Warren 
88045fa1e1SStephen Warren /*
89d455d878SSuriyan Ramasami  * fs_write - Write file to the partition previously set by fs_set_blk_dev()
90d455d878SSuriyan Ramasami  * Note that not all filesystem types support offset!=0.
91bd6fb31fSStephen Warren  *
92d455d878SSuriyan Ramasami  * @filename: Name of file to read from
93d455d878SSuriyan Ramasami  * @addr: The address to read into
94d455d878SSuriyan Ramasami  * @offset: The offset in file to read from. Maybe 0 to write to start of file
95d455d878SSuriyan Ramasami  * @len: The number of bytes to write
96d455d878SSuriyan Ramasami  * @actwrite: Returns the actual number of bytes written
97d455d878SSuriyan Ramasami  * @return 0 if ok with valid *actwrite, -1 on error conditions
98bd6fb31fSStephen Warren  */
99d455d878SSuriyan Ramasami int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
100d455d878SSuriyan Ramasami 	     loff_t *actwrite);
101bd6fb31fSStephen Warren 
102bd6fb31fSStephen Warren /*
1034bbcc965SRob Clark  * Directory entry types, matches the subset of DT_x in posix readdir()
1044bbcc965SRob Clark  * which apply to u-boot.
1054bbcc965SRob Clark  */
1064bbcc965SRob Clark #define FS_DT_DIR  4         /* directory */
1074bbcc965SRob Clark #define FS_DT_REG  8         /* regular file */
1084bbcc965SRob Clark #define FS_DT_LNK  10        /* symbolic link */
1094bbcc965SRob Clark 
1104bbcc965SRob Clark /*
1114bbcc965SRob Clark  * A directory entry, returned by fs_readdir().  Returns information
1124bbcc965SRob Clark  * about the file/directory at the current directory entry position.
1134bbcc965SRob Clark  */
1144bbcc965SRob Clark struct fs_dirent {
1154bbcc965SRob Clark 	unsigned type;       /* one of FS_DT_x (not a mask) */
1164bbcc965SRob Clark 	loff_t size;         /* size in bytes */
1174bbcc965SRob Clark 	char name[256];
1184bbcc965SRob Clark };
1194bbcc965SRob Clark 
1204bbcc965SRob Clark /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */
1214bbcc965SRob Clark struct fs_dir_stream {
1224bbcc965SRob Clark 	/* private to fs. layer: */
1234bbcc965SRob Clark 	struct blk_desc *desc;
1244bbcc965SRob Clark 	int part;
1254bbcc965SRob Clark };
1264bbcc965SRob Clark 
1274bbcc965SRob Clark /*
1284bbcc965SRob Clark  * fs_opendir - Open a directory
1294bbcc965SRob Clark  *
1304bbcc965SRob Clark  * @filename: the path to directory to open
1314bbcc965SRob Clark  * @return a pointer to the directory stream or NULL on error and errno
1324bbcc965SRob Clark  *    set appropriately
1334bbcc965SRob Clark  */
1344bbcc965SRob Clark struct fs_dir_stream *fs_opendir(const char *filename);
1354bbcc965SRob Clark 
1364bbcc965SRob Clark /*
1374bbcc965SRob Clark  * fs_readdir - Read the next directory entry in the directory stream.
1384bbcc965SRob Clark  *
1394bbcc965SRob Clark  * Works in an analogous way to posix readdir().  The previously returned
1404bbcc965SRob Clark  * directory entry is no longer valid after calling fs_readdir() again.
1414bbcc965SRob Clark  * After fs_closedir() is called, the returned directory entry is no
1424bbcc965SRob Clark  * longer valid.
1434bbcc965SRob Clark  *
1444bbcc965SRob Clark  * @dirs: the directory stream
1454bbcc965SRob Clark  * @return the next directory entry (only valid until next fs_readdir() or
1464bbcc965SRob Clark  *    fs_closedir() call, do not attempt to free()) or NULL if the end of
1474bbcc965SRob Clark  *    the directory is reached.
1484bbcc965SRob Clark  */
1494bbcc965SRob Clark struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs);
1504bbcc965SRob Clark 
1514bbcc965SRob Clark /*
1524bbcc965SRob Clark  * fs_closedir - close a directory stream
1534bbcc965SRob Clark  *
1544bbcc965SRob Clark  * @dirs: the directory stream
1554bbcc965SRob Clark  */
1564bbcc965SRob Clark void fs_closedir(struct fs_dir_stream *dirs);
1574bbcc965SRob Clark 
1584bbcc965SRob Clark /*
159*e2519dafSAKASHI Takahiro  * fs_unlink - delete a file or directory
160*e2519dafSAKASHI Takahiro  *
161*e2519dafSAKASHI Takahiro  * If a given name is a directory, it will be deleted only if it's empty
162*e2519dafSAKASHI Takahiro  *
163*e2519dafSAKASHI Takahiro  * @filename: Name of file or directory to delete
164*e2519dafSAKASHI Takahiro  * @return 0 on success, -1 on error conditions
165*e2519dafSAKASHI Takahiro  */
166*e2519dafSAKASHI Takahiro int fs_unlink(const char *filename);
167*e2519dafSAKASHI Takahiro 
168*e2519dafSAKASHI Takahiro /*
169e7074cffSAKASHI Takahiro  * fs_mkdir - Create a directory
170e7074cffSAKASHI Takahiro  *
171e7074cffSAKASHI Takahiro  * @filename: Name of directory to create
172e7074cffSAKASHI Takahiro  * @return 0 on success, -1 on error conditions
173e7074cffSAKASHI Takahiro  */
174e7074cffSAKASHI Takahiro int fs_mkdir(const char *filename);
175e7074cffSAKASHI Takahiro 
176e7074cffSAKASHI Takahiro /*
177045fa1e1SStephen Warren  * Common implementation for various filesystem commands, optionally limited
178045fa1e1SStephen Warren  * to a specific filesystem type via the fstype parameter.
179045fa1e1SStephen Warren  */
180cf659819SStephen Warren int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
181cf659819SStephen Warren 		int fstype);
182f9b55e22SStephen Warren int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
183b770e88aSWolfgang Denk 		int fstype);
184045fa1e1SStephen Warren int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
185045fa1e1SStephen Warren 		int fstype);
1866152916aSStephen Warren int file_exists(const char *dev_type, const char *dev_part, const char *file,
1876152916aSStephen Warren 		int fstype);
188a8f6ab52SSimon Glass int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
189b770e88aSWolfgang Denk 		int fstype);
190*e2519dafSAKASHI Takahiro int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
191*e2519dafSAKASHI Takahiro 		int fstype);
192e7074cffSAKASHI Takahiro int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
193e7074cffSAKASHI Takahiro 		int fstype);
194045fa1e1SStephen Warren 
19559e890efSChristian Gmeiner /*
19659e890efSChristian Gmeiner  * Determine the UUID of the specified filesystem and print it. Optionally it is
19759e890efSChristian Gmeiner  * possible to store the UUID directly in env.
19859e890efSChristian Gmeiner  */
19959e890efSChristian Gmeiner int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
20059e890efSChristian Gmeiner 		int fstype);
20159e890efSChristian Gmeiner 
2021a1ad8e0SSjoerd Simons /*
2031a1ad8e0SSjoerd Simons  * Determine the type of the specified filesystem and print it. Optionally it is
2041a1ad8e0SSjoerd Simons  * possible to store the type directly in env.
2051a1ad8e0SSjoerd Simons  */
2061a1ad8e0SSjoerd Simons int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
2071a1ad8e0SSjoerd Simons 
208045fa1e1SStephen Warren #endif /* _FS_H */
209