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*b3800056SRob Clark * fs_set_blk_dev_with_part - Set current block device + partition 31*b3800056SRob Clark * 32*b3800056SRob Clark * Similar to fs_set_blk_dev(), but useful for cases where you already 33*b3800056SRob Clark * know the blk_desc and part number. 34*b3800056SRob Clark * 35*b3800056SRob Clark * Returns 0 on success. 36*b3800056SRob Clark * Returns non-zero if invalid partition or error accessing the disk. 37*b3800056SRob Clark */ 38*b3800056SRob Clark int fs_set_blk_dev_with_part(struct blk_desc *desc, int part); 39*b3800056SRob Clark 40*b3800056SRob Clark /* 41045fa1e1SStephen Warren * Print the list of files on the partition previously set by fs_set_blk_dev(), 42045fa1e1SStephen Warren * in directory "dirname". 43045fa1e1SStephen Warren * 44045fa1e1SStephen Warren * Returns 0 on success. Returns non-zero on error. 45045fa1e1SStephen Warren */ 46045fa1e1SStephen Warren int fs_ls(const char *dirname); 47045fa1e1SStephen Warren 48045fa1e1SStephen Warren /* 496152916aSStephen Warren * Determine whether a file exists 506152916aSStephen Warren * 516152916aSStephen Warren * Returns 1 if the file exists, 0 if it doesn't exist. 526152916aSStephen Warren */ 536152916aSStephen Warren int fs_exists(const char *filename); 546152916aSStephen Warren 556152916aSStephen Warren /* 56d455d878SSuriyan Ramasami * fs_size - Determine a file's size 57cf659819SStephen Warren * 58d455d878SSuriyan Ramasami * @filename: Name of the file 59d455d878SSuriyan Ramasami * @size: Size of file 60d455d878SSuriyan Ramasami * @return 0 if ok with valid *size, negative on error 61cf659819SStephen Warren */ 62d455d878SSuriyan Ramasami int fs_size(const char *filename, loff_t *size); 63cf659819SStephen Warren 64cf659819SStephen Warren /* 65d455d878SSuriyan Ramasami * fs_read - Read file from the partition previously set by fs_set_blk_dev() 66d455d878SSuriyan Ramasami * Note that not all filesystem types support either/both offset!=0 or len!=0. 67045fa1e1SStephen Warren * 68d455d878SSuriyan Ramasami * @filename: Name of file to read from 69d455d878SSuriyan Ramasami * @addr: The address to read into 70d455d878SSuriyan Ramasami * @offset: The offset in file to read from 71d455d878SSuriyan Ramasami * @len: The number of bytes to read. Maybe 0 to read entire file 72d455d878SSuriyan Ramasami * @actread: Returns the actual number of bytes read 73d455d878SSuriyan Ramasami * @return 0 if ok with valid *actread, -1 on error conditions 74045fa1e1SStephen Warren */ 75d455d878SSuriyan Ramasami int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, 76d455d878SSuriyan Ramasami loff_t *actread); 77045fa1e1SStephen Warren 78045fa1e1SStephen Warren /* 79d455d878SSuriyan Ramasami * fs_write - Write file to the partition previously set by fs_set_blk_dev() 80d455d878SSuriyan Ramasami * Note that not all filesystem types support offset!=0. 81bd6fb31fSStephen Warren * 82d455d878SSuriyan Ramasami * @filename: Name of file to read from 83d455d878SSuriyan Ramasami * @addr: The address to read into 84d455d878SSuriyan Ramasami * @offset: The offset in file to read from. Maybe 0 to write to start of file 85d455d878SSuriyan Ramasami * @len: The number of bytes to write 86d455d878SSuriyan Ramasami * @actwrite: Returns the actual number of bytes written 87d455d878SSuriyan Ramasami * @return 0 if ok with valid *actwrite, -1 on error conditions 88bd6fb31fSStephen Warren */ 89d455d878SSuriyan Ramasami int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, 90d455d878SSuriyan Ramasami loff_t *actwrite); 91bd6fb31fSStephen Warren 92bd6fb31fSStephen Warren /* 93*b3800056SRob Clark * Directory entry types, matches the subset of DT_x in posix readdir() 94*b3800056SRob Clark * which apply to u-boot. 95*b3800056SRob Clark */ 96*b3800056SRob Clark #define FS_DT_DIR 4 /* directory */ 97*b3800056SRob Clark #define FS_DT_REG 8 /* regular file */ 98*b3800056SRob Clark #define FS_DT_LNK 10 /* symbolic link */ 99*b3800056SRob Clark 100*b3800056SRob Clark /* 101*b3800056SRob Clark * A directory entry, returned by fs_readdir(). Returns information 102*b3800056SRob Clark * about the file/directory at the current directory entry position. 103*b3800056SRob Clark */ 104*b3800056SRob Clark struct fs_dirent { 105*b3800056SRob Clark unsigned type; /* one of FS_DT_x (not a mask) */ 106*b3800056SRob Clark loff_t size; /* size in bytes */ 107*b3800056SRob Clark char name[256]; 108*b3800056SRob Clark }; 109*b3800056SRob Clark 110*b3800056SRob Clark /* Note: fs_dir_stream should be treated as opaque to the user of fs layer */ 111*b3800056SRob Clark struct fs_dir_stream { 112*b3800056SRob Clark /* private to fs. layer: */ 113*b3800056SRob Clark struct blk_desc *desc; 114*b3800056SRob Clark int part; 115*b3800056SRob Clark }; 116*b3800056SRob Clark 117*b3800056SRob Clark /* 118*b3800056SRob Clark * fs_opendir - Open a directory 119*b3800056SRob Clark * 120*b3800056SRob Clark * @filename: the path to directory to open 121*b3800056SRob Clark * @return a pointer to the directory stream or NULL on error and errno 122*b3800056SRob Clark * set appropriately 123*b3800056SRob Clark */ 124*b3800056SRob Clark struct fs_dir_stream *fs_opendir(const char *filename); 125*b3800056SRob Clark 126*b3800056SRob Clark /* 127*b3800056SRob Clark * fs_readdir - Read the next directory entry in the directory stream. 128*b3800056SRob Clark * 129*b3800056SRob Clark * Works in an analogous way to posix readdir(). The previously returned 130*b3800056SRob Clark * directory entry is no longer valid after calling fs_readdir() again. 131*b3800056SRob Clark * After fs_closedir() is called, the returned directory entry is no 132*b3800056SRob Clark * longer valid. 133*b3800056SRob Clark * 134*b3800056SRob Clark * @dirs: the directory stream 135*b3800056SRob Clark * @return the next directory entry (only valid until next fs_readdir() or 136*b3800056SRob Clark * fs_closedir() call, do not attempt to free()) or NULL if the end of 137*b3800056SRob Clark * the directory is reached. 138*b3800056SRob Clark */ 139*b3800056SRob Clark struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs); 140*b3800056SRob Clark 141*b3800056SRob Clark /* 142*b3800056SRob Clark * fs_closedir - close a directory stream 143*b3800056SRob Clark * 144*b3800056SRob Clark * @dirs: the directory stream 145*b3800056SRob Clark */ 146*b3800056SRob Clark void fs_closedir(struct fs_dir_stream *dirs); 147*b3800056SRob Clark 148*b3800056SRob Clark /* 149045fa1e1SStephen Warren * Common implementation for various filesystem commands, optionally limited 150045fa1e1SStephen Warren * to a specific filesystem type via the fstype parameter. 151045fa1e1SStephen Warren */ 152cf659819SStephen Warren int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 153cf659819SStephen Warren int fstype); 154f9b55e22SStephen Warren int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 155b770e88aSWolfgang Denk int fstype); 156045fa1e1SStephen Warren int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 157045fa1e1SStephen Warren int fstype); 1586152916aSStephen Warren int file_exists(const char *dev_type, const char *dev_part, const char *file, 1596152916aSStephen Warren int fstype); 160a8f6ab52SSimon Glass int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 161b770e88aSWolfgang Denk int fstype); 162045fa1e1SStephen Warren 16359e890efSChristian Gmeiner /* 16459e890efSChristian Gmeiner * Determine the UUID of the specified filesystem and print it. Optionally it is 16559e890efSChristian Gmeiner * possible to store the UUID directly in env. 16659e890efSChristian Gmeiner */ 16759e890efSChristian Gmeiner int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], 16859e890efSChristian Gmeiner int fstype); 16959e890efSChristian Gmeiner 1701a1ad8e0SSjoerd Simons /* 1711a1ad8e0SSjoerd Simons * Determine the type of the specified filesystem and print it. Optionally it is 1721a1ad8e0SSjoerd Simons * possible to store the type directly in env. 1731a1ad8e0SSjoerd Simons */ 1741a1ad8e0SSjoerd Simons int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); 1751a1ad8e0SSjoerd Simons 176045fa1e1SStephen Warren #endif /* _FS_H */ 177