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