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