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