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, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 171 | #ifdef CONFIG_FAT_WRITE |
| 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 |
| 186 | #ifdef CONFIG_FS_EXT4 |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 187 | { |
| 188 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 189 | .name = "ext4", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 190 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 191 | .probe = ext4fs_probe, |
| 192 | .close = ext4fs_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 193 | .ls = ext4fs_ls, |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 194 | .exists = ext4fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 195 | .size = ext4fs_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 196 | .read = ext4_read_file, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 197 | #ifdef CONFIG_CMD_EXT4_WRITE |
| 198 | .write = ext4_write_file, |
| 199 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 200 | .write = fs_write_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 201 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 202 | .uuid = ext4fs_uuid, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 203 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 204 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 205 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 206 | }, |
| 207 | #endif |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 208 | #ifdef CONFIG_SANDBOX |
| 209 | { |
| 210 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 211 | .name = "sandbox", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 212 | .null_dev_desc_ok = true, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 213 | .probe = sandbox_fs_set_blk_dev, |
| 214 | .close = sandbox_fs_close, |
| 215 | .ls = sandbox_fs_ls, |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 216 | .exists = sandbox_fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 217 | .size = sandbox_fs_size, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 218 | .read = fs_read_sandbox, |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 219 | .write = fs_write_sandbox, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 220 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 221 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 222 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 223 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 224 | }, |
| 225 | #endif |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 226 | #ifdef CONFIG_CMD_UBIFS |
| 227 | { |
| 228 | .fstype = FS_TYPE_UBIFS, |
| 229 | .name = "ubifs", |
| 230 | .null_dev_desc_ok = true, |
| 231 | .probe = ubifs_set_blk_dev, |
| 232 | .close = ubifs_close, |
| 233 | .ls = ubifs_ls, |
| 234 | .exists = ubifs_exists, |
| 235 | .size = ubifs_size, |
| 236 | .read = ubifs_read, |
| 237 | .write = fs_write_unsupported, |
| 238 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 239 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 240 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 241 | .mkdir = fs_mkdir_unsupported, |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 242 | }, |
| 243 | #endif |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 244 | #ifdef CONFIG_FS_BTRFS |
| 245 | { |
| 246 | .fstype = FS_TYPE_BTRFS, |
| 247 | .name = "btrfs", |
| 248 | .null_dev_desc_ok = false, |
| 249 | .probe = btrfs_probe, |
| 250 | .close = btrfs_close, |
| 251 | .ls = btrfs_ls, |
| 252 | .exists = btrfs_exists, |
| 253 | .size = btrfs_size, |
| 254 | .read = btrfs_read, |
| 255 | .write = fs_write_unsupported, |
| 256 | .uuid = btrfs_uuid, |
Marek BehĂșn | 90762d9 | 2017-10-06 16:56:07 +0200 | [diff] [blame] | 257 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 258 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 259 | .mkdir = fs_mkdir_unsupported, |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 260 | }, |
| 261 | #endif |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 262 | { |
| 263 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 264 | .name = "unsupported", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 265 | .null_dev_desc_ok = true, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 266 | .probe = fs_probe_unsupported, |
| 267 | .close = fs_close_unsupported, |
| 268 | .ls = fs_ls_unsupported, |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 269 | .exists = fs_exists_unsupported, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 270 | .size = fs_size_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 271 | .read = fs_read_unsupported, |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 272 | .write = fs_write_unsupported, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 273 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 274 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 275 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 276 | .mkdir = fs_mkdir_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 277 | }, |
| 278 | }; |
| 279 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 280 | static struct fstype_info *fs_get_info(int fstype) |
| 281 | { |
| 282 | struct fstype_info *info; |
| 283 | int i; |
| 284 | |
| 285 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 286 | if (fstype == info->fstype) |
| 287 | return info; |
| 288 | } |
| 289 | |
| 290 | /* Return the 'unsupported' sentinel */ |
| 291 | return info; |
| 292 | } |
| 293 | |
Alex Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 294 | /** |
| 295 | * fs_get_type_name() - Get type of current filesystem |
| 296 | * |
| 297 | * Return: Pointer to filesystem name |
| 298 | * |
| 299 | * Returns a string describing the current filesystem, or the sentinel |
| 300 | * "unsupported" for any unrecognised filesystem. |
| 301 | */ |
| 302 | const char *fs_get_type_name(void) |
| 303 | { |
| 304 | return fs_get_info(fs_type)->name; |
| 305 | } |
| 306 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 307 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 308 | { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 309 | struct fstype_info *info; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 310 | int part, i; |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 311 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 312 | static int relocated; |
| 313 | |
| 314 | if (!relocated) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 315 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 316 | i++, info++) { |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 317 | info->name += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 318 | info->probe += gd->reloc_off; |
| 319 | info->close += gd->reloc_off; |
| 320 | info->ls += gd->reloc_off; |
| 321 | info->read += gd->reloc_off; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 322 | info->write += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 323 | } |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 324 | relocated = 1; |
| 325 | } |
| 326 | #endif |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 327 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 328 | 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] | 329 | &fs_partition, 1); |
| 330 | if (part < 0) |
| 331 | return -1; |
| 332 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 333 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 334 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 335 | fstype != info->fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 336 | continue; |
| 337 | |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 338 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 339 | continue; |
| 340 | |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 341 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 342 | fs_type = info->fstype; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 343 | fs_dev_part = part; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | } |
| 347 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 351 | /* set current blk device w/ blk_desc + partition # */ |
| 352 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) |
| 353 | { |
| 354 | struct fstype_info *info; |
| 355 | int ret, i; |
| 356 | |
| 357 | if (part >= 1) |
| 358 | ret = part_get_info(desc, part, &fs_partition); |
| 359 | else |
| 360 | ret = part_get_info_whole_disk(desc, &fs_partition); |
| 361 | if (ret) |
| 362 | return ret; |
| 363 | fs_dev_desc = desc; |
| 364 | |
| 365 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 366 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
| 367 | fs_type = info->fstype; |
AKASHI Takahiro | 43efaa0 | 2018-10-17 16:32:02 +0900 | [diff] [blame] | 368 | fs_dev_part = part; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return -1; |
| 374 | } |
| 375 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 376 | static void fs_close(void) |
| 377 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 378 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 379 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 380 | info->close(); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 381 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 382 | fs_type = FS_TYPE_ANY; |
| 383 | } |
| 384 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 385 | int fs_uuid(char *uuid_str) |
| 386 | { |
| 387 | struct fstype_info *info = fs_get_info(fs_type); |
| 388 | |
| 389 | return info->uuid(uuid_str); |
| 390 | } |
| 391 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 392 | int fs_ls(const char *dirname) |
| 393 | { |
| 394 | int ret; |
| 395 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 396 | struct fstype_info *info = fs_get_info(fs_type); |
| 397 | |
| 398 | ret = info->ls(dirname); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 399 | |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 400 | fs_type = FS_TYPE_ANY; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 401 | fs_close(); |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 406 | int fs_exists(const char *filename) |
| 407 | { |
| 408 | int ret; |
| 409 | |
| 410 | struct fstype_info *info = fs_get_info(fs_type); |
| 411 | |
| 412 | ret = info->exists(filename); |
| 413 | |
| 414 | fs_close(); |
| 415 | |
| 416 | return ret; |
| 417 | } |
| 418 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 419 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 420 | { |
| 421 | int ret; |
| 422 | |
| 423 | struct fstype_info *info = fs_get_info(fs_type); |
| 424 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 425 | ret = info->size(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 426 | |
| 427 | fs_close(); |
| 428 | |
| 429 | return ret; |
| 430 | } |
| 431 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 432 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 433 | loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 434 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 435 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 436 | void *buf; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 437 | int ret; |
| 438 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 439 | /* |
| 440 | * We don't actually know how many bytes are being read, since len==0 |
| 441 | * means read the whole file. |
| 442 | */ |
| 443 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 444 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 445 | unmap_sysmem(buf); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 446 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 447 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 56d9cdf | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 448 | if (ret == 0 && len && *actread != len) |
Heinrich Schuchardt | f02c5c5 | 2018-01-01 21:15:37 +0100 | [diff] [blame] | 449 | debug("** %s shorter than offset + len **\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 450 | fs_close(); |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 455 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 456 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 457 | { |
| 458 | struct fstype_info *info = fs_get_info(fs_type); |
| 459 | void *buf; |
| 460 | int ret; |
| 461 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 462 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 463 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 464 | unmap_sysmem(buf); |
| 465 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 466 | if (ret < 0 && len != *actwrite) { |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 467 | printf("** Unable to write file %s **\n", filename); |
| 468 | ret = -1; |
| 469 | } |
| 470 | fs_close(); |
| 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 475 | struct fs_dir_stream *fs_opendir(const char *filename) |
| 476 | { |
| 477 | struct fstype_info *info = fs_get_info(fs_type); |
| 478 | struct fs_dir_stream *dirs = NULL; |
| 479 | int ret; |
| 480 | |
| 481 | ret = info->opendir(filename, &dirs); |
| 482 | fs_close(); |
| 483 | if (ret) { |
| 484 | errno = -ret; |
| 485 | return NULL; |
| 486 | } |
| 487 | |
| 488 | dirs->desc = fs_dev_desc; |
| 489 | dirs->part = fs_dev_part; |
| 490 | |
| 491 | return dirs; |
| 492 | } |
| 493 | |
| 494 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) |
| 495 | { |
| 496 | struct fstype_info *info; |
| 497 | struct fs_dirent *dirent; |
| 498 | int ret; |
| 499 | |
| 500 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 501 | info = fs_get_info(fs_type); |
| 502 | |
| 503 | ret = info->readdir(dirs, &dirent); |
| 504 | fs_close(); |
| 505 | if (ret) { |
| 506 | errno = -ret; |
| 507 | return NULL; |
| 508 | } |
| 509 | |
| 510 | return dirent; |
| 511 | } |
| 512 | |
| 513 | void fs_closedir(struct fs_dir_stream *dirs) |
| 514 | { |
| 515 | struct fstype_info *info; |
| 516 | |
| 517 | if (!dirs) |
| 518 | return; |
| 519 | |
| 520 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 521 | info = fs_get_info(fs_type); |
| 522 | |
| 523 | info->closedir(dirs); |
| 524 | fs_close(); |
| 525 | } |
| 526 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 527 | int fs_unlink(const char *filename) |
| 528 | { |
| 529 | int ret; |
| 530 | |
| 531 | struct fstype_info *info = fs_get_info(fs_type); |
| 532 | |
| 533 | ret = info->unlink(filename); |
| 534 | |
| 535 | fs_type = FS_TYPE_ANY; |
| 536 | fs_close(); |
| 537 | |
| 538 | return ret; |
| 539 | } |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 540 | |
| 541 | int fs_mkdir(const char *dirname) |
| 542 | { |
| 543 | int ret; |
| 544 | |
| 545 | struct fstype_info *info = fs_get_info(fs_type); |
| 546 | |
| 547 | ret = info->mkdir(dirname); |
| 548 | |
| 549 | fs_type = FS_TYPE_ANY; |
| 550 | fs_close(); |
| 551 | |
| 552 | return ret; |
| 553 | } |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 554 | |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 555 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 556 | int fstype) |
| 557 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 558 | loff_t size; |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 559 | |
| 560 | if (argc != 4) |
| 561 | return CMD_RET_USAGE; |
| 562 | |
| 563 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 564 | return 1; |
| 565 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 566 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 567 | return CMD_RET_FAILURE; |
| 568 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 569 | env_set_hex("filesize", size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
Stephen Warren | 128d3d9 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 574 | 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] | 575 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 576 | { |
| 577 | unsigned long addr; |
| 578 | const char *addr_str; |
| 579 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 580 | loff_t bytes; |
| 581 | loff_t pos; |
| 582 | loff_t len_read; |
| 583 | int ret; |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 584 | unsigned long time; |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 585 | char *ep; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 586 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 587 | if (argc < 2) |
| 588 | return CMD_RET_USAGE; |
| 589 | if (argc > 7) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 590 | return CMD_RET_USAGE; |
| 591 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 592 | 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] | 593 | return 1; |
| 594 | |
| 595 | if (argc >= 4) { |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 596 | addr = simple_strtoul(argv[3], &ep, 16); |
| 597 | if (ep == argv[3] || *ep != '\0') |
| 598 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 599 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 600 | addr_str = env_get("loadaddr"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 601 | if (addr_str != NULL) |
| 602 | addr = simple_strtoul(addr_str, NULL, 16); |
| 603 | else |
| 604 | addr = CONFIG_SYS_LOAD_ADDR; |
| 605 | } |
| 606 | if (argc >= 5) { |
| 607 | filename = argv[4]; |
| 608 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 609 | filename = env_get("bootfile"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 610 | if (!filename) { |
| 611 | puts("** No boot file defined **\n"); |
| 612 | return 1; |
| 613 | } |
| 614 | } |
| 615 | if (argc >= 6) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 616 | bytes = simple_strtoul(argv[5], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 617 | else |
| 618 | bytes = 0; |
| 619 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 620 | pos = simple_strtoul(argv[6], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 621 | else |
| 622 | pos = 0; |
| 623 | |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 624 | time = get_timer(0); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 625 | ret = fs_read(filename, addr, pos, bytes, &len_read); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 626 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 627 | if (ret < 0) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 628 | return 1; |
| 629 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 630 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 631 | if (time > 0) { |
| 632 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 633 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 634 | puts(")"); |
| 635 | } |
| 636 | puts("\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 637 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 638 | env_set_hex("fileaddr", addr); |
| 639 | env_set_hex("filesize", len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |
| 644 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 645 | int fstype) |
| 646 | { |
| 647 | if (argc < 2) |
| 648 | return CMD_RET_USAGE; |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 649 | if (argc > 4) |
| 650 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 651 | |
| 652 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 653 | return 1; |
| 654 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 655 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 656 | return 1; |
| 657 | |
| 658 | return 0; |
| 659 | } |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 660 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 661 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 662 | int fstype) |
| 663 | { |
| 664 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) |
| 665 | return 0; |
| 666 | |
| 667 | return fs_exists(file); |
| 668 | } |
| 669 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 670 | 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] | 671 | int fstype) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 672 | { |
| 673 | unsigned long addr; |
| 674 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 675 | loff_t bytes; |
| 676 | loff_t pos; |
| 677 | loff_t len; |
| 678 | int ret; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 679 | unsigned long time; |
| 680 | |
| 681 | if (argc < 6 || argc > 7) |
| 682 | return CMD_RET_USAGE; |
| 683 | |
| 684 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 685 | return 1; |
| 686 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 687 | addr = simple_strtoul(argv[3], NULL, 16); |
| 688 | filename = argv[4]; |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 689 | bytes = simple_strtoul(argv[5], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 690 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 691 | pos = simple_strtoul(argv[6], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 692 | else |
| 693 | pos = 0; |
| 694 | |
| 695 | time = get_timer(0); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 696 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 697 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 698 | if (ret < 0) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 699 | return 1; |
| 700 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 701 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 702 | if (time > 0) { |
| 703 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 704 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 705 | puts(")"); |
| 706 | } |
| 707 | puts("\n"); |
| 708 | |
| 709 | return 0; |
| 710 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 711 | |
| 712 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 713 | int fstype) |
| 714 | { |
| 715 | int ret; |
| 716 | char uuid[37]; |
| 717 | memset(uuid, 0, sizeof(uuid)); |
| 718 | |
| 719 | if (argc < 3 || argc > 4) |
| 720 | return CMD_RET_USAGE; |
| 721 | |
| 722 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 723 | return 1; |
| 724 | |
| 725 | ret = fs_uuid(uuid); |
| 726 | if (ret) |
| 727 | return CMD_RET_FAILURE; |
| 728 | |
| 729 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 730 | env_set(argv[3], uuid); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 731 | else |
| 732 | printf("%s\n", uuid); |
| 733 | |
| 734 | return CMD_RET_SUCCESS; |
| 735 | } |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 736 | |
| 737 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 738 | { |
| 739 | struct fstype_info *info; |
| 740 | |
| 741 | if (argc < 3 || argc > 4) |
| 742 | return CMD_RET_USAGE; |
| 743 | |
| 744 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 745 | return 1; |
| 746 | |
| 747 | info = fs_get_info(fs_type); |
| 748 | |
| 749 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 750 | env_set(argv[3], info->name); |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 751 | else |
| 752 | printf("%s\n", info->name); |
| 753 | |
| 754 | return CMD_RET_SUCCESS; |
| 755 | } |
| 756 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 757 | int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 758 | int fstype) |
| 759 | { |
| 760 | if (argc != 4) |
| 761 | return CMD_RET_USAGE; |
| 762 | |
| 763 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 764 | return 1; |
| 765 | |
| 766 | if (fs_unlink(argv[3])) |
| 767 | return 1; |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 772 | int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 773 | int fstype) |
| 774 | { |
| 775 | int ret; |
| 776 | |
| 777 | if (argc != 4) |
| 778 | return CMD_RET_USAGE; |
| 779 | |
| 780 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 781 | return 1; |
| 782 | |
| 783 | ret = fs_mkdir(argv[3]); |
| 784 | if (ret) { |
| 785 | printf("** Unable to create a directory \"%s\" **\n", argv[3]); |
| 786 | return 1; |
| 787 | } |
| 788 | |
| 789 | return 0; |
| 790 | } |