Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 3 | * |
Tom Rini | 5b8031c | 2016-01-14 22:05:13 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 8 | #include <errno.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 9 | #include <common.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> |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 17 | #include <asm/io.h> |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 18 | #include <div64.h> |
| 19 | #include <linux/math64.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 20 | |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 23 | static struct blk_desc *fs_dev_desc; |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 24 | static int fs_dev_part; |
Stephen Warren | 045fa1e | 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 | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 28 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
Simon Glass | 2ded0d4 | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 29 | disk_partition_t *fs_partition) |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 30 | { |
| 31 | printf("** Unrecognized filesystem type **\n"); |
| 32 | return -1; |
| 33 | } |
| 34 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 35 | static inline int fs_ls_unsupported(const char *dirname) |
| 36 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 40 | static inline int fs_exists_unsupported(const char *filename) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 45 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 46 | { |
| 47 | return -1; |
| 48 | } |
| 49 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 50 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 51 | loff_t offset, loff_t len, |
| 52 | loff_t *actread) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 53 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 54 | return -1; |
| 55 | } |
| 56 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 57 | static inline int fs_write_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 58 | loff_t offset, loff_t len, |
| 59 | loff_t *actwrite) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 60 | { |
| 61 | return -1; |
| 62 | } |
| 63 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 64 | static inline void fs_close_unsupported(void) |
| 65 | { |
| 66 | } |
| 67 | |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 68 | static inline int fs_uuid_unsupported(char *uuid_str) |
| 69 | { |
| 70 | return -1; |
| 71 | } |
| 72 | |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 73 | static inline int fs_opendir_unsupported(const char *filename, |
| 74 | struct fs_dir_stream **dirs) |
| 75 | { |
| 76 | return -EACCES; |
| 77 | } |
| 78 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 79 | struct fstype_info { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 80 | int fstype; |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 81 | char *name; |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 82 | /* |
| 83 | * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This |
| 84 | * should be false in most cases. For "virtual" filesystems which |
| 85 | * aren't based on a U-Boot block device (e.g. sandbox), this can be |
| 86 | * set to true. This should also be true for the dumm entry at the end |
| 87 | * of fstypes[], since that is essentially a "virtual" (non-existent) |
| 88 | * filesystem. |
| 89 | */ |
| 90 | bool null_dev_desc_ok; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 91 | int (*probe)(struct blk_desc *fs_dev_desc, |
Simon Glass | 2ded0d4 | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 92 | disk_partition_t *fs_partition); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 93 | int (*ls)(const char *dirname); |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 94 | int (*exists)(const char *filename); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 95 | int (*size)(const char *filename, loff_t *size); |
| 96 | int (*read)(const char *filename, void *buf, loff_t offset, |
| 97 | loff_t len, loff_t *actread); |
| 98 | int (*write)(const char *filename, void *buf, loff_t offset, |
| 99 | loff_t len, loff_t *actwrite); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 100 | void (*close)(void); |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 101 | int (*uuid)(char *uuid_str); |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 102 | /* |
| 103 | * Open a directory stream. On success return 0 and directory |
| 104 | * stream pointer via 'dirsp'. On error, return -errno. See |
| 105 | * fs_opendir(). |
| 106 | */ |
| 107 | int (*opendir)(const char *filename, struct fs_dir_stream **dirsp); |
| 108 | /* |
| 109 | * Read next entry from directory stream. On success return 0 |
| 110 | * and directory entry pointer via 'dentp'. On error return |
| 111 | * -errno. See fs_readdir(). |
| 112 | */ |
| 113 | int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp); |
| 114 | /* see fs_closedir() */ |
| 115 | void (*closedir)(struct fs_dir_stream *dirs); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static struct fstype_info fstypes[] = { |
| 119 | #ifdef CONFIG_FS_FAT |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 120 | { |
| 121 | .fstype = FS_TYPE_FAT, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 122 | .name = "fat", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 123 | .null_dev_desc_ok = false, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 124 | .probe = fat_set_blk_dev, |
| 125 | .close = fat_close, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 126 | .ls = file_fat_ls, |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 127 | .exists = fat_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 128 | .size = fat_size, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 129 | .read = fat_read_file, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 130 | #ifdef CONFIG_FAT_WRITE |
| 131 | .write = file_fat_write, |
| 132 | #else |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 133 | .write = fs_write_unsupported, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 134 | #endif |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 135 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 136 | .opendir = fs_opendir_unsupported, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 137 | }, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 138 | #endif |
| 139 | #ifdef CONFIG_FS_EXT4 |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 140 | { |
| 141 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 142 | .name = "ext4", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 143 | .null_dev_desc_ok = false, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 144 | .probe = ext4fs_probe, |
| 145 | .close = ext4fs_close, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 146 | .ls = ext4fs_ls, |
Stephen Warren | 55af5c9 | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 147 | .exists = ext4fs_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 148 | .size = ext4fs_size, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 149 | .read = ext4_read_file, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 150 | #ifdef CONFIG_CMD_EXT4_WRITE |
| 151 | .write = ext4_write_file, |
| 152 | #else |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 153 | .write = fs_write_unsupported, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 154 | #endif |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 155 | .uuid = ext4fs_uuid, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 156 | .opendir = fs_opendir_unsupported, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 157 | }, |
| 158 | #endif |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 159 | #ifdef CONFIG_SANDBOX |
| 160 | { |
| 161 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 162 | .name = "sandbox", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 163 | .null_dev_desc_ok = true, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 164 | .probe = sandbox_fs_set_blk_dev, |
| 165 | .close = sandbox_fs_close, |
| 166 | .ls = sandbox_fs_ls, |
Stephen Warren | 0a30aa1 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 167 | .exists = sandbox_fs_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 168 | .size = sandbox_fs_size, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 169 | .read = fs_read_sandbox, |
Simon Glass | 7eb2c8d | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 170 | .write = fs_write_sandbox, |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 171 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 172 | .opendir = fs_opendir_unsupported, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 173 | }, |
| 174 | #endif |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 175 | #ifdef CONFIG_CMD_UBIFS |
| 176 | { |
| 177 | .fstype = FS_TYPE_UBIFS, |
| 178 | .name = "ubifs", |
| 179 | .null_dev_desc_ok = true, |
| 180 | .probe = ubifs_set_blk_dev, |
| 181 | .close = ubifs_close, |
| 182 | .ls = ubifs_ls, |
| 183 | .exists = ubifs_exists, |
| 184 | .size = ubifs_size, |
| 185 | .read = ubifs_read, |
| 186 | .write = fs_write_unsupported, |
| 187 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 188 | .opendir = fs_opendir_unsupported, |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 189 | }, |
| 190 | #endif |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 191 | { |
| 192 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 193 | .name = "unsupported", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 194 | .null_dev_desc_ok = true, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 195 | .probe = fs_probe_unsupported, |
| 196 | .close = fs_close_unsupported, |
| 197 | .ls = fs_ls_unsupported, |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 198 | .exists = fs_exists_unsupported, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 199 | .size = fs_size_unsupported, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 200 | .read = fs_read_unsupported, |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 201 | .write = fs_write_unsupported, |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 202 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 203 | .opendir = fs_opendir_unsupported, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 204 | }, |
| 205 | }; |
| 206 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 207 | static struct fstype_info *fs_get_info(int fstype) |
| 208 | { |
| 209 | struct fstype_info *info; |
| 210 | int i; |
| 211 | |
| 212 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 213 | if (fstype == info->fstype) |
| 214 | return info; |
| 215 | } |
| 216 | |
| 217 | /* Return the 'unsupported' sentinel */ |
| 218 | return info; |
| 219 | } |
| 220 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 221 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 222 | { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 223 | struct fstype_info *info; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 224 | int part, i; |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 225 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 226 | static int relocated; |
| 227 | |
| 228 | if (!relocated) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 229 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 230 | i++, info++) { |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 231 | info->name += gd->reloc_off; |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 232 | info->probe += gd->reloc_off; |
| 233 | info->close += gd->reloc_off; |
| 234 | info->ls += gd->reloc_off; |
| 235 | info->read += gd->reloc_off; |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 236 | info->write += gd->reloc_off; |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 237 | } |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 238 | relocated = 1; |
| 239 | } |
| 240 | #endif |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 241 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 242 | 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] | 243 | &fs_partition, 1); |
| 244 | if (part < 0) |
| 245 | return -1; |
| 246 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 247 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 248 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 249 | fstype != info->fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 250 | continue; |
| 251 | |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 252 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 253 | continue; |
| 254 | |
Simon Glass | 2ded0d4 | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 255 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 256 | fs_type = info->fstype; |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 257 | fs_dev_part = part; |
| 258 | return 0; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return -1; |
| 263 | } |
| 264 | |
| 265 | /* set current blk device w/ blk_desc + partition # */ |
| 266 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) |
| 267 | { |
| 268 | struct fstype_info *info; |
| 269 | int ret, i; |
| 270 | |
| 271 | if (part >= 1) |
| 272 | ret = part_get_info(desc, part, &fs_partition); |
| 273 | else |
| 274 | ret = part_get_info_whole_disk(desc, &fs_partition); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | fs_dev_desc = desc; |
| 278 | |
| 279 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 280 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
| 281 | fs_type = info->fstype; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | } |
| 285 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | static void fs_close(void) |
| 290 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 291 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 292 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 293 | info->close(); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 294 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 295 | fs_type = FS_TYPE_ANY; |
| 296 | } |
| 297 | |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 298 | int fs_uuid(char *uuid_str) |
| 299 | { |
| 300 | struct fstype_info *info = fs_get_info(fs_type); |
| 301 | |
| 302 | return info->uuid(uuid_str); |
| 303 | } |
| 304 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 305 | int fs_ls(const char *dirname) |
| 306 | { |
| 307 | int ret; |
| 308 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 309 | struct fstype_info *info = fs_get_info(fs_type); |
| 310 | |
| 311 | ret = info->ls(dirname); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 312 | |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 313 | fs_type = FS_TYPE_ANY; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 314 | fs_close(); |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 319 | int fs_exists(const char *filename) |
| 320 | { |
| 321 | int ret; |
| 322 | |
| 323 | struct fstype_info *info = fs_get_info(fs_type); |
| 324 | |
| 325 | ret = info->exists(filename); |
| 326 | |
| 327 | fs_close(); |
| 328 | |
| 329 | return ret; |
| 330 | } |
| 331 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 332 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 333 | { |
| 334 | int ret; |
| 335 | |
| 336 | struct fstype_info *info = fs_get_info(fs_type); |
| 337 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 338 | ret = info->size(filename, size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 339 | |
| 340 | fs_close(); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 345 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 346 | loff_t *actread) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 347 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 348 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 349 | void *buf; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 350 | int ret; |
| 351 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 352 | /* |
| 353 | * We don't actually know how many bytes are being read, since len==0 |
| 354 | * means read the whole file. |
| 355 | */ |
| 356 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 357 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 358 | unmap_sysmem(buf); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 359 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 360 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 7a3e70c | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 361 | if (ret == 0 && len && *actread != len) |
| 362 | printf("** %s shorter than offset + len **\n", filename); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 363 | fs_close(); |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 368 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 369 | loff_t *actwrite) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 370 | { |
| 371 | struct fstype_info *info = fs_get_info(fs_type); |
| 372 | void *buf; |
| 373 | int ret; |
| 374 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 375 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 376 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 377 | unmap_sysmem(buf); |
| 378 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 379 | if (ret < 0 && len != *actwrite) { |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 380 | printf("** Unable to write file %s **\n", filename); |
| 381 | ret = -1; |
| 382 | } |
| 383 | fs_close(); |
| 384 | |
| 385 | return ret; |
| 386 | } |
| 387 | |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame^] | 388 | struct fs_dir_stream *fs_opendir(const char *filename) |
| 389 | { |
| 390 | struct fstype_info *info = fs_get_info(fs_type); |
| 391 | struct fs_dir_stream *dirs = NULL; |
| 392 | int ret; |
| 393 | |
| 394 | ret = info->opendir(filename, &dirs); |
| 395 | fs_close(); |
| 396 | if (ret) { |
| 397 | errno = -ret; |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | dirs->desc = fs_dev_desc; |
| 402 | dirs->part = fs_dev_part; |
| 403 | |
| 404 | return dirs; |
| 405 | } |
| 406 | |
| 407 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) |
| 408 | { |
| 409 | struct fstype_info *info; |
| 410 | struct fs_dirent *dirent; |
| 411 | int ret; |
| 412 | |
| 413 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 414 | info = fs_get_info(fs_type); |
| 415 | |
| 416 | ret = info->readdir(dirs, &dirent); |
| 417 | fs_close(); |
| 418 | if (ret) { |
| 419 | errno = -ret; |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | return dirent; |
| 424 | } |
| 425 | |
| 426 | void fs_closedir(struct fs_dir_stream *dirs) |
| 427 | { |
| 428 | struct fstype_info *info; |
| 429 | |
| 430 | if (!dirs) |
| 431 | return; |
| 432 | |
| 433 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 434 | info = fs_get_info(fs_type); |
| 435 | |
| 436 | info->closedir(dirs); |
| 437 | fs_close(); |
| 438 | } |
| 439 | |
| 440 | |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 441 | int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 442 | int fstype) |
| 443 | { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 444 | loff_t size; |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 445 | |
| 446 | if (argc != 4) |
| 447 | return CMD_RET_USAGE; |
| 448 | |
| 449 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 450 | return 1; |
| 451 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 452 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 453 | return CMD_RET_FAILURE; |
| 454 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 455 | env_set_hex("filesize", size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
Stephen Warren | f9b55e2 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 460 | 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] | 461 | int fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 462 | { |
| 463 | unsigned long addr; |
| 464 | const char *addr_str; |
| 465 | const char *filename; |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 466 | loff_t bytes; |
| 467 | loff_t pos; |
| 468 | loff_t len_read; |
| 469 | int ret; |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 470 | unsigned long time; |
Pavel Machek | 949bbd7 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 471 | char *ep; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 472 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 473 | if (argc < 2) |
| 474 | return CMD_RET_USAGE; |
| 475 | if (argc > 7) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 476 | return CMD_RET_USAGE; |
| 477 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 478 | 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] | 479 | return 1; |
| 480 | |
| 481 | if (argc >= 4) { |
Pavel Machek | 949bbd7 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 482 | addr = simple_strtoul(argv[3], &ep, 16); |
| 483 | if (ep == argv[3] || *ep != '\0') |
| 484 | return CMD_RET_USAGE; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 485 | } else { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 486 | addr_str = env_get("loadaddr"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 487 | if (addr_str != NULL) |
| 488 | addr = simple_strtoul(addr_str, NULL, 16); |
| 489 | else |
| 490 | addr = CONFIG_SYS_LOAD_ADDR; |
| 491 | } |
| 492 | if (argc >= 5) { |
| 493 | filename = argv[4]; |
| 494 | } else { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 495 | filename = env_get("bootfile"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 496 | if (!filename) { |
| 497 | puts("** No boot file defined **\n"); |
| 498 | return 1; |
| 499 | } |
| 500 | } |
| 501 | if (argc >= 6) |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 502 | bytes = simple_strtoul(argv[5], NULL, 16); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 503 | else |
| 504 | bytes = 0; |
| 505 | if (argc >= 7) |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 506 | pos = simple_strtoul(argv[6], NULL, 16); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 507 | else |
| 508 | pos = 0; |
| 509 | |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 510 | time = get_timer(0); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 511 | ret = fs_read(filename, addr, pos, bytes, &len_read); |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 512 | time = get_timer(time); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 513 | if (ret < 0) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 514 | return 1; |
| 515 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 516 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 517 | if (time > 0) { |
| 518 | puts(" ("); |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 519 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 520 | puts(")"); |
| 521 | } |
| 522 | puts("\n"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 523 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 524 | env_set_hex("fileaddr", addr); |
| 525 | env_set_hex("filesize", len_read); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 531 | int fstype) |
| 532 | { |
| 533 | if (argc < 2) |
| 534 | return CMD_RET_USAGE; |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 535 | if (argc > 4) |
| 536 | return CMD_RET_USAGE; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 537 | |
| 538 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 539 | return 1; |
| 540 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 541 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 542 | return 1; |
| 543 | |
| 544 | return 0; |
| 545 | } |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 546 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 547 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 548 | int fstype) |
| 549 | { |
| 550 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) |
| 551 | return 0; |
| 552 | |
| 553 | return fs_exists(file); |
| 554 | } |
| 555 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 556 | 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] | 557 | int fstype) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 558 | { |
| 559 | unsigned long addr; |
| 560 | const char *filename; |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 561 | loff_t bytes; |
| 562 | loff_t pos; |
| 563 | loff_t len; |
| 564 | int ret; |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 565 | unsigned long time; |
| 566 | |
| 567 | if (argc < 6 || argc > 7) |
| 568 | return CMD_RET_USAGE; |
| 569 | |
| 570 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 571 | return 1; |
| 572 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 573 | addr = simple_strtoul(argv[3], NULL, 16); |
| 574 | filename = argv[4]; |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 575 | bytes = simple_strtoul(argv[5], NULL, 16); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 576 | if (argc >= 7) |
Wolfgang Denk | b770e88 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 577 | pos = simple_strtoul(argv[6], NULL, 16); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 578 | else |
| 579 | pos = 0; |
| 580 | |
| 581 | time = get_timer(0); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 582 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 583 | time = get_timer(time); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 584 | if (ret < 0) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 585 | return 1; |
| 586 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 587 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 588 | if (time > 0) { |
| 589 | puts(" ("); |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 590 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 591 | puts(")"); |
| 592 | } |
| 593 | puts("\n"); |
| 594 | |
| 595 | return 0; |
| 596 | } |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 597 | |
| 598 | int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 599 | int fstype) |
| 600 | { |
| 601 | int ret; |
| 602 | char uuid[37]; |
| 603 | memset(uuid, 0, sizeof(uuid)); |
| 604 | |
| 605 | if (argc < 3 || argc > 4) |
| 606 | return CMD_RET_USAGE; |
| 607 | |
| 608 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 609 | return 1; |
| 610 | |
| 611 | ret = fs_uuid(uuid); |
| 612 | if (ret) |
| 613 | return CMD_RET_FAILURE; |
| 614 | |
| 615 | if (argc == 4) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 616 | env_set(argv[3], uuid); |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 617 | else |
| 618 | printf("%s\n", uuid); |
| 619 | |
| 620 | return CMD_RET_SUCCESS; |
| 621 | } |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 622 | |
| 623 | int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
| 624 | { |
| 625 | struct fstype_info *info; |
| 626 | |
| 627 | if (argc < 3 || argc > 4) |
| 628 | return CMD_RET_USAGE; |
| 629 | |
| 630 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 631 | return 1; |
| 632 | |
| 633 | info = fs_get_info(fs_type); |
| 634 | |
| 635 | if (argc == 4) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 636 | env_set(argv[3], info->name); |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 637 | else |
| 638 | printf("%s\n", info->name); |
| 639 | |
| 640 | return CMD_RET_SUCCESS; |
| 641 | } |
| 642 | |