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