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