1045fa1e1SStephen Warren /* 2045fa1e1SStephen Warren * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. 3045fa1e1SStephen Warren * 45b8031ccSTom Rini * SPDX-License-Identifier: GPL-2.0 5045fa1e1SStephen Warren */ 6045fa1e1SStephen Warren #ifndef _FS_H 7045fa1e1SStephen Warren #define _FS_H 8045fa1e1SStephen Warren 9045fa1e1SStephen Warren #include <common.h> 10045fa1e1SStephen Warren 11045fa1e1SStephen Warren #define FS_TYPE_ANY 0 12045fa1e1SStephen Warren #define FS_TYPE_FAT 1 13045fa1e1SStephen Warren #define FS_TYPE_EXT 2 1492ccc96bSSimon Glass #define FS_TYPE_SANDBOX 3 15251cee0dSHans de Goede #define FS_TYPE_UBIFS 4 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 /* 30*e17ddceaSJason Zhu * fs_get_fstype - Get filesystem type on the partition previously 31*e17ddceaSJason Zhu * set by fs_set_blk_dev() 32*e17ddceaSJason Zhu * 33*e17ddceaSJason Zhu * @fstype_name: The return the name of filesystem type 34*e17ddceaSJason Zhu * @return 0 if ok with valid *fstype_name, -1 on error conditions 35*e17ddceaSJason Zhu */ 36*e17ddceaSJason Zhu int fs_get_fstype(const char **fstype_name); 37*e17ddceaSJason Zhu 38*e17ddceaSJason Zhu /* 39b3800056SRob Clark * fs_set_blk_dev_with_part - Set current block device + partition 40b3800056SRob Clark * 41b3800056SRob Clark * Similar to fs_set_blk_dev(), but useful for cases where you already 42b3800056SRob Clark * know the blk_desc and part number. 43b3800056SRob Clark * 44b3800056SRob Clark * Returns 0 on success. 45b3800056SRob Clark * Returns non-zero if invalid partition or error accessing the disk. 46b3800056SRob Clark */ 47b3800056SRob Clark int fs_set_blk_dev_with_part(struct blk_desc *desc, int part); 48b3800056SRob Clark 49b3800056SRob Clark /* 50045fa1e1SStephen Warren * Print the list of files on the partition previously set by fs_set_blk_dev(), 51045fa1e1SStephen Warren * in directory "dirname". 52045fa1e1SStephen Warren * 53045fa1e1SStephen Warren * Returns 0 on success. Returns non-zero on error. 54045fa1e1SStephen Warren */ 55045fa1e1SStephen Warren int fs_ls(const char *dirname); 56045fa1e1SStephen Warren 57045fa1e1SStephen Warren /* 586152916aSStephen Warren * Determine whether a file exists 596152916aSStephen Warren * 606152916aSStephen Warren * Returns 1 if the file exists, 0 if it doesn't exist. 616152916aSStephen Warren */ 626152916aSStephen Warren int fs_exists(const char *filename); 636152916aSStephen Warren 646152916aSStephen Warren /* 65d455d878SSuriyan Ramasami * fs_size - Determine a file's size 66cf659819SStephen Warren * 67d455d878SSuriyan Ramasami * @filename: Name of the file 68d455d878SSuriyan Ramasami * @size: Size of file 69d455d878SSuriyan Ramasami * @return 0 if ok with valid *size, negative on error 70cf659819SStephen Warren */ 71d455d878SSuriyan Ramasami int fs_size(const char *filename, loff_t *size); 72cf659819SStephen Warren 73cf659819SStephen Warren /* 74d455d878SSuriyan Ramasami * fs_read - Read file from the partition previously set by fs_set_blk_dev() 75d455d878SSuriyan Ramasami * Note that not all filesystem types support either/both offset!=0 or len!=0. 76045fa1e1SStephen Warren * 77d455d878SSuriyan Ramasami * @filename: Name of file to read from 78d455d878SSuriyan Ramasami * @addr: The address to read into 79d455d878SSuriyan Ramasami * @offset: The offset in file to read from 80d455d878SSuriyan Ramasami * @len: The number of bytes to read. Maybe 0 to read entire file 81d455d878SSuriyan Ramasami * @actread: Returns the actual number of bytes read 82d455d878SSuriyan Ramasami * @return 0 if ok with valid *actread, -1 on error conditions 83045fa1e1SStephen Warren */ 84d455d878SSuriyan Ramasami int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, 85d455d878SSuriyan Ramasami loff_t *actread); 86045fa1e1SStephen Warren 87045fa1e1SStephen Warren /* 88d455d878SSuriyan Ramasami * fs_write - Write file to the partition previously set by fs_set_blk_dev() 89d455d878SSuriyan Ramasami * Note that not all filesystem types support offset!=0. 90bd6fb31fSStephen Warren * 91d455d878SSuriyan Ramasami * @filename: Name of file to read from 92d455d878SSuriyan Ramasami * @addr: The address to read into 93d455d878SSuriyan Ramasami * @offset: The offset in file to read from. Maybe 0 to write to start of file 94d455d878SSuriyan Ramasami * @len: The number of bytes to write 95d455d878SSuriyan Ramasami * @actwrite: Returns the actual number of bytes written 96d455d878SSuriyan Ramasami * @return 0 if ok with valid *actwrite, -1 on error conditions 97bd6fb31fSStephen Warren */ 98d455d878SSuriyan Ramasami int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, 99d455d878SSuriyan Ramasami loff_t *actwrite); 100bd6fb31fSStephen Warren 101bd6fb31fSStephen Warren /* 102b3800056SRob Clark * Directory entry types, matches the subset of DT_x in posix readdir() 103b3800056SRob Clark * which apply to u-boot. 104b3800056SRob Clark */ 105b3800056SRob Clark #define FS_DT_DIR 4 /* directory */ 106b3800056SRob Clark #define FS_DT_REG 8 /* regular file */ 107b3800056SRob Clark #define FS_DT_LNK 10 /* symbolic link */ 108b3800056SRob Clark 109b3800056SRob Clark /* 110b3800056SRob Clark * A directory entry, returned by fs_readdir(). Returns information 111b3800056SRob Clark * about the file/directory at the current directory entry position. 112b3800056SRob Clark */ 113b3800056SRob Clark struct fs_dirent { 114b3800056SRob Clark unsigned type; /* one of FS_DT_x (not a mask) */ 115b3800056SRob Clark loff_t size; /* size in bytes */ 116b3800056SRob Clark char name[256]; 117b3800056SRob Clark }; 118b3800056SRob Clark 119b3800056SRob Clark /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */ 120b3800056SRob Clark struct fs_dir_stream { 121b3800056SRob Clark /* private to fs. layer: */ 122b3800056SRob Clark struct blk_desc *desc; 123b3800056SRob Clark int part; 124b3800056SRob Clark }; 125b3800056SRob Clark 126b3800056SRob Clark /* 127b3800056SRob Clark * fs_opendir - Open a directory 128b3800056SRob Clark * 129b3800056SRob Clark * @filename: the path to directory to open 130b3800056SRob Clark * @return a pointer to the directory stream or NULL on error and errno 131b3800056SRob Clark * set appropriately 132b3800056SRob Clark */ 133b3800056SRob Clark struct fs_dir_stream *fs_opendir(const char *filename); 134b3800056SRob Clark 135b3800056SRob Clark /* 136b3800056SRob Clark * fs_readdir - Read the next directory entry in the directory stream. 137b3800056SRob Clark * 138b3800056SRob Clark * Works in an analogous way to posix readdir(). The previously returned 139b3800056SRob Clark * directory entry is no longer valid after calling fs_readdir() again. 140b3800056SRob Clark * After fs_closedir() is called, the returned directory entry is no 141b3800056SRob Clark * longer valid. 142b3800056SRob Clark * 143b3800056SRob Clark * @dirs: the directory stream 144b3800056SRob Clark * @return the next directory entry (only valid until next fs_readdir() or 145b3800056SRob Clark * fs_closedir() call, do not attempt to free()) or NULL if the end of 146b3800056SRob Clark * the directory is reached. 147b3800056SRob Clark */ 148b3800056SRob Clark struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs); 149b3800056SRob Clark 150b3800056SRob Clark /* 151b3800056SRob Clark * fs_closedir - close a directory stream 152b3800056SRob Clark * 153b3800056SRob Clark * @dirs: the directory stream 154b3800056SRob Clark */ 155b3800056SRob Clark void fs_closedir(struct fs_dir_stream *dirs); 156b3800056SRob Clark 157b3800056SRob Clark /* 158045fa1e1SStephen Warren * Common implementation for various filesystem commands, optionally limited 159045fa1e1SStephen Warren * to a specific filesystem type via the fstype parameter. 160045fa1e1SStephen Warren */ 161cf659819SStephen Warren int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 162cf659819SStephen Warren int fstype); 163f9b55e22SStephen Warren int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 164b770e88aSWolfgang Denk int fstype); 165045fa1e1SStephen Warren int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 166045fa1e1SStephen Warren int fstype); 1676152916aSStephen Warren int file_exists(const char *dev_type, const char *dev_part, const char *file, 1686152916aSStephen Warren int fstype); 169a8f6ab52SSimon Glass int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 170b770e88aSWolfgang Denk int fstype); 171045fa1e1SStephen Warren 17259e890efSChristian Gmeiner /* 17359e890efSChristian Gmeiner * Determine the UUID of the specified filesystem and print it. Optionally it is 17459e890efSChristian Gmeiner * possible to store the UUID directly in env. 17559e890efSChristian Gmeiner */ 17659e890efSChristian Gmeiner int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 17759e890efSChristian Gmeiner int fstype); 17859e890efSChristian Gmeiner 1791a1ad8e0SSjoerd Simons /* 1801a1ad8e0SSjoerd Simons * Determine the type of the specified filesystem and print it. Optionally it is 1811a1ad8e0SSjoerd Simons * possible to store the type directly in env. 1821a1ad8e0SSjoerd Simons */ 1831a1ad8e0SSjoerd Simons int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 1841a1ad8e0SSjoerd Simons 185045fa1e1SStephen Warren #endif /* _FS_H */ 186