Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <config.h> |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 7 | #include <errno.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 8 | #include <common.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 9 | #include <mapmem.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 10 | #include <part.h> |
| 11 | #include <ext4fs.h> |
| 12 | #include <fat.h> |
| 13 | #include <fs.h> |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 14 | #include <sandboxfs.h> |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 15 | #include <ubifs_uboot.h> |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 16 | #include <btrfs.h> |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 17 | #include <asm/io.h> |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 18 | #include <div64.h> |
| 19 | #include <linux/math64.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 20 | |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 23 | static struct blk_desc *fs_dev_desc; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 24 | static int fs_dev_part; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 25 | static disk_partition_t fs_partition; |
| 26 | static int fs_type = FS_TYPE_ANY; |
| 27 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 28 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 29 | disk_partition_t *fs_partition) |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 30 | { |
| 31 | printf("** Unrecognized filesystem type **\n"); |
| 32 | return -1; |
| 33 | } |
| 34 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 35 | static inline int fs_ls_unsupported(const char *dirname) |
| 36 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 40 | /* generic implementation of ls in terms of opendir/readdir/closedir */ |
| 41 | __maybe_unused |
| 42 | static int fs_ls_generic(const char *dirname) |
| 43 | { |
| 44 | struct fs_dir_stream *dirs; |
| 45 | struct fs_dirent *dent; |
| 46 | int nfiles = 0, ndirs = 0; |
| 47 | |
| 48 | dirs = fs_opendir(dirname); |
| 49 | if (!dirs) |
| 50 | return -errno; |
| 51 | |
| 52 | while ((dent = fs_readdir(dirs))) { |
| 53 | if (dent->type == FS_DT_DIR) { |
| 54 | printf(" %s/\n", dent->name); |
| 55 | ndirs++; |
| 56 | } else { |
| 57 | printf(" %8lld %s\n", dent->size, dent->name); |
| 58 | nfiles++; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fs_closedir(dirs); |
| 63 | |
| 64 | printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs); |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 69 | static inline int fs_exists_unsupported(const char *filename) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 74 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 75 | { |
| 76 | return -1; |
| 77 | } |
| 78 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 79 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 80 | loff_t offset, loff_t len, |
| 81 | loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 82 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 83 | return -1; |
| 84 | } |
| 85 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 86 | static inline int fs_write_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 87 | loff_t offset, loff_t len, |
| 88 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 89 | { |
| 90 | return -1; |
| 91 | } |
| 92 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 93 | static inline void fs_close_unsupported(void) |
| 94 | { |
| 95 | } |
| 96 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 97 | static inline int fs_uuid_unsupported(char *uuid_str) |
| 98 | { |
| 99 | return -1; |
| 100 | } |
| 101 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 102 | static inline int fs_opendir_unsupported(const char *filename, |
| 103 | struct fs_dir_stream **dirs) |
| 104 | { |
| 105 | return -EACCES; |
| 106 | } |
| 107 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 108 | static inline int fs_unlink_unsupported(const char *filename) |
| 109 | { |
| 110 | return -1; |
| 111 | } |
| 112 | |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 113 | static inline int fs_mkdir_unsupported(const char *dirname) |
| 114 | { |
| 115 | return -1; |
| 116 | } |
| 117 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 118 | struct fstype_info { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 119 | int fstype; |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 120 | char *name; |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 121 | /* |
| 122 | * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This |
| 123 | * should be false in most cases. For "virtual" filesystems which |
| 124 | * aren't based on a U-Boot block device (e.g. sandbox), this can be |
Heinrich Schuchardt | 99dc17d | 2018-08-11 15:52:14 +0200 | [diff] [blame] | 125 | * set to true. This should also be true for the dummy entry at the end |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 126 | * of fstypes[], since that is essentially a "virtual" (non-existent) |
| 127 | * filesystem. |
| 128 | */ |
| 129 | bool null_dev_desc_ok; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 130 | int (*probe)(struct blk_desc *fs_dev_desc, |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 131 | disk_partition_t *fs_partition); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 132 | int (*ls)(const char *dirname); |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 133 | int (*exists)(const char *filename); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 134 | int (*size)(const char *filename, loff_t *size); |
| 135 | int (*read)(const char *filename, void *buf, loff_t offset, |
| 136 | loff_t len, loff_t *actread); |
| 137 | int (*write)(const char *filename, void *buf, loff_t offset, |
| 138 | loff_t len, loff_t *actwrite); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 139 | void (*close)(void); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 140 | int (*uuid)(char *uuid_str); |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 141 | /* |
| 142 | * Open a directory stream. On success return 0 and directory |
| 143 | * stream pointer via 'dirsp'. On error, return -errno. See |
| 144 | * fs_opendir(). |
| 145 | */ |
| 146 | int (*opendir)(const char *filename, struct fs_dir_stream **dirsp); |
| 147 | /* |
| 148 | * Read next entry from directory stream. On success return 0 |
| 149 | * and directory entry pointer via 'dentp'. On error return |
| 150 | * -errno. See fs_readdir(). |
| 151 | */ |
| 152 | int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp); |
| 153 | /* see fs_closedir() */ |
| 154 | void (*closedir)(struct fs_dir_stream *dirs); |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 155 | int (*unlink)(const char *filename); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 156 | int (*mkdir)(const char *dirname); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | static struct fstype_info fstypes[] = { |
| 160 | #ifdef CONFIG_FS_FAT |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 161 | { |
| 162 | .fstype = FS_TYPE_FAT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 163 | .name = "fat", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 164 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 165 | .probe = fat_set_blk_dev, |
| 166 | .close = fat_close, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 167 | .ls = fs_ls_generic, |
Stephen Warren | 1d2f9a0 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 168 | .exists = fat_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 169 | .size = fat_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 170 | .read = fat_read_file, |
Tien Fong Chee | 87fda0c | 2019-01-23 14:20:04 +0800 | [diff] [blame] | 171 | #if CONFIG_IS_ENABLED(FAT_WRITE) |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 172 | .write = file_fat_write, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 173 | .unlink = fat_unlink, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 174 | .mkdir = fat_mkdir, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 175 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 176 | .write = fs_write_unsupported, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 177 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 178 | .mkdir = fs_mkdir_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 179 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 180 | .uuid = fs_uuid_unsupported, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 181 | .opendir = fat_opendir, |
| 182 | .readdir = fat_readdir, |
| 183 | .closedir = fat_closedir, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 184 | }, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 185 | #endif |
Tien Fong Chee | 64eab04 | 2019-01-23 14:20:06 +0800 | [diff] [blame] | 186 | |
| 187 | #if CONFIG_IS_ENABLED(FS_EXT4) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 188 | { |
| 189 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 190 | .name = "ext4", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 191 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 192 | .probe = ext4fs_probe, |
| 193 | .close = ext4fs_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 194 | .ls = ext4fs_ls, |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 195 | .exists = ext4fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 196 | .size = ext4fs_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 197 | .read = ext4_read_file, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 198 | #ifdef CONFIG_CMD_EXT4_WRITE |
| 199 | .write = ext4_write_file, |
| 200 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 201 | .write = fs_write_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 202 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 203 | .uuid = ext4fs_uuid, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 204 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 205 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 206 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 207 | }, |
| 208 | #endif |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 209 | #ifdef CONFIG_SANDBOX |
| 210 | { |
| 211 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 212 | .name = "sandbox", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 213 | .null_dev_desc_ok = true, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 214 | .probe = sandbox_fs_set_blk_dev, |
| 215 | .close = sandbox_fs_close, |
| 216 | .ls = sandbox_fs_ls, |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 217 | .exists = sandbox_fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 218 | .size = sandbox_fs_size, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 219 | .read = fs_read_sandbox, |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 220 | .write = fs_write_sandbox, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 221 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 222 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 223 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 224 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 225 | }, |
| 226 | #endif |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 227 | #ifdef CONFIG_CMD_UBIFS |
| 228 | { |
| 229 | .fstype = FS_TYPE_UBIFS, |
| 230 | .name = "ubifs", |
| 231 | .null_dev_desc_ok = true, |
| 232 | .probe = ubifs_set_blk_dev, |
| 233 | .close = ubifs_close, |
| 234 | .ls = ubifs_ls, |
| 235 | .exists = ubifs_exists, |
| 236 | .size = ubifs_size, |
| 237 | .read = ubifs_read, |
| 238 | .write = fs_write_unsupported, |
| 239 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 240 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 241 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 242 | .mkdir = fs_mkdir_unsupported, |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 243 | }, |
| 244 | #endif |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 245 | #ifdef CONFIG_FS_BTRFS |
| 246 | { |
| 247 | .fstype = FS_TYPE_BTRFS, |
| 248 | .name = "btrfs", |
| 249 | .null_dev_desc_ok = false, |
| 250 | .probe = btrfs_probe, |
| 251 | .close = btrfs_close, |
| 252 | .ls = btrfs_ls, |
| 253 | .exists = btrfs_exists, |
| 254 | .size = btrfs_size, |
| 255 | .read = btrfs_read, |
| 256 | .write = fs_write_unsupported, |
| 257 | .uuid = btrfs_uuid, |
Marek BehĂșn | 90762d9 | 2017-10-06 16:56:07 +0200 | [diff] [blame] | 258 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 259 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 260 | .mkdir = fs_mkdir_unsupported, |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 261 | }, |
| 262 | #endif |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 263 | { |
| 264 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 265 | .name = "unsupported", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 266 | .null_dev_desc_ok = true, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 267 | .probe = fs_probe_unsupported, |
| 268 | .close = fs_close_unsupported, |
| 269 | .ls = fs_ls_unsupported, |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 270 | .exists = fs_exists_unsupported, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 271 | .size = fs_size_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 272 | .read = fs_read_unsupported, |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 273 | .write = fs_write_unsupported, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 274 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 275 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 276 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 277 | .mkdir = fs_mkdir_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 278 | }, |
| 279 | }; |
| 280 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 281 | static struct fstype_info *fs_get_info(int fstype) |
| 282 | { |
| 283 | struct fstype_info *info; |
| 284 | int i; |
| 285 | |
| 286 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 287 | if (fstype == info->fstype) |
| 288 | return info; |
| 289 | } |
| 290 | |
| 291 | /* Return the 'unsupported' sentinel */ |
| 292 | return info; |
| 293 | } |
| 294 | |
Alex Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 295 | /** |
| 296 | * fs_get_type_name() - Get type of current filesystem |
| 297 | * |
| 298 | * Return: Pointer to filesystem name |
| 299 | * |
| 300 | * Returns a string describing the current filesystem, or the sentinel |
| 301 | * "unsupported" for any unrecognised filesystem. |
| 302 | */ |
| 303 | const char *fs_get_type_name(void) |
| 304 | { |
| 305 | return fs_get_info(fs_type)->name; |
| 306 | } |
| 307 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 308 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 309 | { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 310 | struct fstype_info *info; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 311 | int part, i; |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 312 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 313 | static int relocated; |
| 314 | |
| 315 | if (!relocated) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 316 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 317 | i++, info++) { |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 318 | info->name += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 319 | info->probe += gd->reloc_off; |
| 320 | info->close += gd->reloc_off; |
| 321 | info->ls += gd->reloc_off; |
| 322 | info->read += gd->reloc_off; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 323 | info->write += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 324 | } |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 325 | relocated = 1; |
| 326 | } |
| 327 | #endif |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 328 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 329 | part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 330 | &fs_partition, 1); |
| 331 | if (part < 0) |
| 332 | return -1; |
| 333 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 334 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 335 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 336 | fstype != info->fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 337 | continue; |
| 338 | |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 339 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 340 | continue; |
| 341 | |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 342 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 343 | fs_type = info->fstype; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 344 | fs_dev_part = part; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | } |
| 348 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 349 | return -1; |
| 350 | } |
| 351 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 352 | /* set current blk device w/ blk_desc + partition # */ |
| 353 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) |
| 354 | { |
| 355 | struct fstype_info *info; |
| 356 | int ret, i; |
| 357 | |
| 358 | if (part >= 1) |
| 359 | ret = part_get_info(desc, part, &fs_partition); |
| 360 | else |
| 361 | ret = part_get_info_whole_disk(desc, &fs_partition); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | fs_dev_desc = desc; |
| 365 | |
| 366 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 367 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
| 368 | fs_type = info->fstype; |
AKASHI Takahiro | 43efaa0 | 2018-10-17 16:32:02 +0900 | [diff] [blame] | 369 | fs_dev_part = part; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 370 | return 0; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return -1; |
| 375 | } |
| 376 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 377 | static void fs_close(void) |
| 378 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 379 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 380 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 381 | info->close(); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 382 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 383 | fs_type = FS_TYPE_ANY; |
| 384 | } |
| 385 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 386 | int fs_uuid(char *uuid_str) |
| 387 | { |
| 388 | struct fstype_info *info = fs_get_info(fs_type); |
| 389 | |
| 390 | return info->uuid(uuid_str); |
| 391 | } |
| 392 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 393 | int fs_ls(const char *dirname) |
| 394 | { |
| 395 | int ret; |
| 396 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 397 | struct fstype_info *info = fs_get_info(fs_type); |
| 398 | |
| 399 | ret = info->ls(dirname); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 400 | |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 401 | fs_type = FS_TYPE_ANY; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 402 | fs_close(); |
| 403 | |
| 404 | return ret; |
| 405 | } |
| 406 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 407 | int fs_exists(const char *filename) |
| 408 | { |
| 409 | int ret; |
| 410 | |
| 411 | struct fstype_info *info = fs_get_info(fs_type); |
| 412 | |
| 413 | ret = info->exists(filename); |
| 414 | |
| 415 | fs_close(); |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 420 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 421 | { |
| 422 | int ret; |
| 423 | |
| 424 | struct fstype_info *info = fs_get_info(fs_type); |
| 425 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 426 | ret = info->size(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 427 | |
| 428 | fs_close(); |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 433 | #ifdef CONFIG_LMB |
| 434 | /* Check if a file may be read to the given address */ |
| 435 | static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset, |
| 436 | loff_t len, struct fstype_info *info) |
| 437 | { |
| 438 | struct lmb lmb; |
| 439 | int ret; |
| 440 | loff_t size; |
| 441 | loff_t read_len; |
| 442 | |
| 443 | /* get the actual size of the file */ |
| 444 | ret = info->size(filename, &size); |
| 445 | if (ret) |
| 446 | return ret; |
| 447 | if (offset >= size) { |
| 448 | /* offset >= EOF, no bytes will be written */ |
| 449 | return 0; |
| 450 | } |
| 451 | read_len = size - offset; |
| 452 | |
| 453 | /* limit to 'len' if it is smaller */ |
| 454 | if (len && len < read_len) |
| 455 | read_len = len; |
| 456 | |
Simon Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 457 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 458 | lmb_dump_all(&lmb); |
| 459 | |
| 460 | if (lmb_alloc_addr(&lmb, addr, read_len) == addr) |
| 461 | return 0; |
| 462 | |
| 463 | printf("** Reading file would overwrite reserved memory **\n"); |
| 464 | return -ENOSPC; |
| 465 | } |
| 466 | #endif |
| 467 | |
| 468 | static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 469 | int do_lmb_check, loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 470 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 471 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 472 | void *buf; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 473 | int ret; |
| 474 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 475 | #ifdef CONFIG_LMB |
| 476 | if (do_lmb_check) { |
| 477 | ret = fs_read_lmb_check(filename, addr, offset, len, info); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | } |
| 481 | #endif |
| 482 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 483 | /* |
| 484 | * We don't actually know how many bytes are being read, since len==0 |
| 485 | * means read the whole file. |
| 486 | */ |
| 487 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 488 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 489 | unmap_sysmem(buf); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 490 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 491 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 56d9cdf | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 492 | if (ret == 0 && len && *actread != len) |
Heinrich Schuchardt | f02c5c5 | 2018-01-01 21:15:37 +0100 | [diff] [blame] | 493 | debug("** %s shorter than offset + len **\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 494 | fs_close(); |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 499 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 500 | loff_t *actread) |
| 501 | { |
| 502 | return _fs_read(filename, addr, offset, len, 0, actread); |
| 503 | } |
| 504 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 505 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 506 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 507 | { |
| 508 | struct fstype_info *info = fs_get_info(fs_type); |
| 509 | void *buf; |
| 510 | int ret; |
| 511 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 512 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 513 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 514 | unmap_sysmem(buf); |
| 515 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 516 | if (ret < 0 && len != *actwrite) { |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 517 | printf("** Unable to write file %s **\n", filename); |
| 518 | ret = -1; |
| 519 | } |
| 520 | fs_close(); |
| 521 | |
| 522 | return ret; |
| 523 | } |
| 524 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 525 | struct fs_dir_stream *fs_opendir(const char *filename) |
| 526 | { |
| 527 | struct fstype_info *info = fs_get_info(fs_type); |
| 528 | struct fs_dir_stream *dirs = NULL; |
| 529 | int ret; |
| 530 | |
| 531 | ret = info->opendir(filename, &dirs); |
| 532 | fs_close(); |
| 533 | if (ret) { |
| 534 | errno = -ret; |
| 535 | return NULL; |
| 536 | } |
| 537 | |
| 538 | dirs->desc = fs_dev_desc; |
| 539 | dirs->part = fs_dev_part; |
| 540 | |
| 541 | return dirs; |
| 542 | } |
| 543 | |
| 544 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) |
| 545 | { |
| 546 | struct fstype_info *info; |
| 547 | struct fs_dirent *dirent; |
| 548 | int ret; |
| 549 | |
| 550 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 551 | info = fs_get_info(fs_type); |
| 552 | |
| 553 | ret = info->readdir(dirs, &dirent); |
| 554 | fs_close(); |
| 555 | if (ret) { |
| 556 | errno = -ret; |
| 557 | return NULL; |
| 558 | } |
| 559 | |
| 560 | return dirent; |
| 561 | } |
| 562 | |
| 563 | void fs_closedir(struct fs_dir_stream *dirs) |
| 564 | { |
| 565 | struct fstype_info *info; |
| 566 | |
| 567 | if (!dirs) |
| 568 | return; |
| 569 | |
| 570 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 571 | info = fs_get_info(fs_type); |
| 572 | |
| 573 | info->closedir(dirs); |
| 574 | fs_close(); |
| 575 | } |
| 576 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 577 | int fs_unlink(const char *filename) |
| 578 | { |
| 579 | int ret; |
| 580 | |
| 581 | struct fstype_info *info = fs_get_info(fs_type); |
| 582 | |
| 583 | ret = info->unlink(filename); |
| 584 | |
| 585 | fs_type = FS_TYPE_ANY; |
| 586 | fs_close(); |
| 587 | |
| 588 | return ret; |
| 589 | } |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 590 | |
| 591 | int fs_mkdir(const char *dirname) |
| 592 | { |
| 593 | int ret; |
| 594 | |
| 595 | struct fstype_info *info = fs_get_info(fs_type); |
| 596 | |
| 597 | ret = info->mkdir(dirname); |
| 598 | |
| 599 | fs_type = FS_TYPE_ANY; |
| 600 | fs_close(); |
| 601 | |
| 602 | return ret; |
| 603 | } |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 604 | |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 605 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 606 | int fstype) |
| 607 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 608 | loff_t size; |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 609 | |
| 610 | if (argc != 4) |
| 611 | return CMD_RET_USAGE; |
| 612 | |
| 613 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 614 | return 1; |
| 615 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 616 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 617 | return CMD_RET_FAILURE; |
| 618 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 619 | env_set_hex("filesize", size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Stephen Warren | 128d3d9 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 624 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 625 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 626 | { |
| 627 | unsigned long addr; |
| 628 | const char *addr_str; |
| 629 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 630 | loff_t bytes; |
| 631 | loff_t pos; |
| 632 | loff_t len_read; |
| 633 | int ret; |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 634 | unsigned long time; |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 635 | char *ep; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 636 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 637 | if (argc < 2) |
| 638 | return CMD_RET_USAGE; |
| 639 | if (argc > 7) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 640 | return CMD_RET_USAGE; |
| 641 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 642 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 643 | return 1; |
| 644 | |
| 645 | if (argc >= 4) { |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 646 | addr = simple_strtoul(argv[3], &ep, 16); |
| 647 | if (ep == argv[3] || *ep != '\0') |
| 648 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 649 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 650 | addr_str = env_get("loadaddr"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 651 | if (addr_str != NULL) |
| 652 | addr = simple_strtoul(addr_str, NULL, 16); |
| 653 | else |
| 654 | addr = CONFIG_SYS_LOAD_ADDR; |
| 655 | } |
| 656 | if (argc >= 5) { |
| 657 | filename = argv[4]; |
| 658 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 659 | filename = env_get("bootfile"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 660 | if (!filename) { |
| 661 | puts("** No boot file defined **\n"); |
| 662 | return 1; |
| 663 | } |
| 664 | } |
| 665 | if (argc >= 6) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 666 | bytes = simple_strtoul(argv[5], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 667 | else |
| 668 | bytes = 0; |
| 669 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 670 | pos = simple_strtoul(argv[6], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 671 | else |
| 672 | pos = 0; |
| 673 | |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 674 | time = get_timer(0); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 675 | ret = _fs_read(filename, addr, pos, bytes, 1, &len_read); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 676 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 677 | if (ret < 0) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 678 | return 1; |
| 679 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 680 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 681 | if (time > 0) { |
| 682 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 683 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 684 | puts(")"); |
| 685 | } |
| 686 | puts("\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 687 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 688 | env_set_hex("fileaddr", addr); |
| 689 | env_set_hex("filesize", len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 695 | int fstype) |
| 696 | { |
| 697 | if (argc < 2) |
| 698 | return CMD_RET_USAGE; |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 699 | if (argc > 4) |
| 700 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 701 | |
| 702 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 703 | return 1; |
| 704 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 705 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 706 | return 1; |
| 707 | |
| 708 | return 0; |
| 709 | } |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 710 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 711 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 712 | int fstype) |
| 713 | { |
| 714 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) |
| 715 | return 0; |
| 716 | |
| 717 | return fs_exists(file); |
| 718 | } |
| 719 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 720 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 721 | int fstype) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 722 | { |
| 723 | unsigned long addr; |
| 724 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 725 | loff_t bytes; |
| 726 | loff_t pos; |
| 727 | loff_t len; |
| 728 | int ret; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 729 | unsigned long time; |
| 730 | |
| 731 | if (argc < 6 || argc > 7) |
| 732 | return CMD_RET_USAGE; |
| 733 | |
| 734 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 735 | return 1; |
| 736 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 737 | addr = simple_strtoul(argv[3], NULL, 16); |
| 738 | filename = argv[4]; |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 739 | bytes = simple_strtoul(argv[5], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 740 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 741 | pos = simple_strtoul(argv[6], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 742 | else |
| 743 | pos = 0; |
| 744 | |
| 745 | time = get_timer(0); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 746 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 747 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 748 | if (ret < 0) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 749 | return 1; |
| 750 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 751 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 752 | if (time > 0) { |
| 753 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 754 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 755 | puts(")"); |
| 756 | } |
| 757 | puts("\n"); |
| 758 | |
| 759 | return 0; |
| 760 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 761 | |
| 762 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 763 | int fstype) |
| 764 | { |
| 765 | int ret; |
| 766 | char uuid[37]; |
| 767 | memset(uuid, 0, sizeof(uuid)); |
| 768 | |
| 769 | if (argc < 3 || argc > 4) |
| 770 | return CMD_RET_USAGE; |
| 771 | |
| 772 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 773 | return 1; |
| 774 | |
| 775 | ret = fs_uuid(uuid); |
| 776 | if (ret) |
| 777 | return CMD_RET_FAILURE; |
| 778 | |
| 779 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 780 | env_set(argv[3], uuid); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 781 | else |
| 782 | printf("%s\n", uuid); |
| 783 | |
| 784 | return CMD_RET_SUCCESS; |
| 785 | } |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 786 | |
| 787 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 788 | { |
| 789 | struct fstype_info *info; |
| 790 | |
| 791 | if (argc < 3 || argc > 4) |
| 792 | return CMD_RET_USAGE; |
| 793 | |
| 794 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 795 | return 1; |
| 796 | |
| 797 | info = fs_get_info(fs_type); |
| 798 | |
| 799 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 800 | env_set(argv[3], info->name); |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 801 | else |
| 802 | printf("%s\n", info->name); |
| 803 | |
Marek Vasut | 08d1c42 | 2019-02-06 13:19:29 +0100 | [diff] [blame^] | 804 | fs_close(); |
| 805 | |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 806 | return CMD_RET_SUCCESS; |
| 807 | } |
| 808 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 809 | int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 810 | int fstype) |
| 811 | { |
| 812 | if (argc != 4) |
| 813 | return CMD_RET_USAGE; |
| 814 | |
| 815 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 816 | return 1; |
| 817 | |
| 818 | if (fs_unlink(argv[3])) |
| 819 | return 1; |
| 820 | |
| 821 | return 0; |
| 822 | } |
| 823 | |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 824 | int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 825 | int fstype) |
| 826 | { |
| 827 | int ret; |
| 828 | |
| 829 | if (argc != 4) |
| 830 | return CMD_RET_USAGE; |
| 831 | |
| 832 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 833 | return 1; |
| 834 | |
| 835 | ret = fs_mkdir(argv[3]); |
| 836 | if (ret) { |
| 837 | printf("** Unable to create a directory \"%s\" **\n", argv[3]); |
| 838 | return 1; |
| 839 | } |
| 840 | |
| 841 | return 0; |
| 842 | } |