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