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