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