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