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