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 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 44 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 45 | int offset, int len) |
| 46 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 50 | static inline int fs_write_unsupported(const char *filename, void *buf, |
| 51 | int offset, int len) |
| 52 | { |
| 53 | return -1; |
| 54 | } |
| 55 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 56 | static inline void fs_close_unsupported(void) |
| 57 | { |
| 58 | } |
| 59 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 60 | struct fstype_info { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 61 | int fstype; |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 62 | int (*probe)(block_dev_desc_t *fs_dev_desc, |
| 63 | disk_partition_t *fs_partition); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 64 | int (*ls)(const char *dirname); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 65 | int (*read)(const char *filename, void *buf, int offset, int len); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 66 | int (*write)(const char *filename, void *buf, int offset, int len); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 67 | void (*close)(void); |
| 68 | }; |
| 69 | |
| 70 | static struct fstype_info fstypes[] = { |
| 71 | #ifdef CONFIG_FS_FAT |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 72 | { |
| 73 | .fstype = FS_TYPE_FAT, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 74 | .probe = fat_set_blk_dev, |
| 75 | .close = fat_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 76 | .ls = file_fat_ls, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 77 | .read = fat_read_file, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 78 | }, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 79 | #endif |
| 80 | #ifdef CONFIG_FS_EXT4 |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 81 | { |
| 82 | .fstype = FS_TYPE_EXT, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 83 | .probe = ext4fs_probe, |
| 84 | .close = ext4fs_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 85 | .ls = ext4fs_ls, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 86 | .read = ext4_read_file, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 87 | }, |
| 88 | #endif |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_SANDBOX |
| 90 | { |
| 91 | .fstype = FS_TYPE_SANDBOX, |
| 92 | .probe = sandbox_fs_set_blk_dev, |
| 93 | .close = sandbox_fs_close, |
| 94 | .ls = sandbox_fs_ls, |
| 95 | .read = fs_read_sandbox, |
| 96 | }, |
| 97 | #endif |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 98 | { |
| 99 | .fstype = FS_TYPE_ANY, |
| 100 | .probe = fs_probe_unsupported, |
| 101 | .close = fs_close_unsupported, |
| 102 | .ls = fs_ls_unsupported, |
| 103 | .read = fs_read_unsupported, |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 104 | .write = fs_write_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 105 | }, |
| 106 | }; |
| 107 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 108 | static struct fstype_info *fs_get_info(int fstype) |
| 109 | { |
| 110 | struct fstype_info *info; |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 114 | if (fstype == info->fstype) |
| 115 | return info; |
| 116 | } |
| 117 | |
| 118 | /* Return the 'unsupported' sentinel */ |
| 119 | return info; |
| 120 | } |
| 121 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 122 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 123 | { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 124 | struct fstype_info *info; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 125 | int part, i; |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 126 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 127 | static int relocated; |
| 128 | |
| 129 | if (!relocated) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 130 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 131 | i++, info++) { |
| 132 | info->probe += gd->reloc_off; |
| 133 | info->close += gd->reloc_off; |
| 134 | info->ls += gd->reloc_off; |
| 135 | info->read += gd->reloc_off; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 136 | info->write += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 137 | } |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 138 | relocated = 1; |
| 139 | } |
| 140 | #endif |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 141 | |
| 142 | part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc, |
| 143 | &fs_partition, 1); |
| 144 | if (part < 0) |
| 145 | return -1; |
| 146 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 147 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 148 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 149 | fstype != info->fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 152 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 153 | fs_type = info->fstype; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | |
| 161 | static void fs_close(void) |
| 162 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 163 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 164 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 165 | info->close(); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 166 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 167 | fs_type = FS_TYPE_ANY; |
| 168 | } |
| 169 | |
| 170 | int fs_ls(const char *dirname) |
| 171 | { |
| 172 | int ret; |
| 173 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 174 | struct fstype_info *info = fs_get_info(fs_type); |
| 175 | |
| 176 | ret = info->ls(dirname); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 177 | |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 178 | fs_type = FS_TYPE_ANY; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 179 | fs_close(); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | int fs_read(const char *filename, ulong addr, int offset, int len) |
| 185 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 186 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 187 | void *buf; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 188 | int ret; |
| 189 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 190 | /* |
| 191 | * We don't actually know how many bytes are being read, since len==0 |
| 192 | * means read the whole file. |
| 193 | */ |
| 194 | buf = map_sysmem(addr, len); |
| 195 | ret = info->read(filename, buf, offset, len); |
| 196 | unmap_sysmem(buf); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 197 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 198 | /* If we requested a specific number of bytes, check we got it */ |
| 199 | if (ret >= 0 && len && ret != len) { |
| 200 | printf("** Unable to read file %s **\n", filename); |
| 201 | ret = -1; |
| 202 | } |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 203 | fs_close(); |
| 204 | |
| 205 | return ret; |
| 206 | } |
| 207 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 208 | int fs_write(const char *filename, ulong addr, int offset, int len) |
| 209 | { |
| 210 | struct fstype_info *info = fs_get_info(fs_type); |
| 211 | void *buf; |
| 212 | int ret; |
| 213 | |
| 214 | /* |
| 215 | * We don't actually know how many bytes are being read, since len==0 |
| 216 | * means read the whole file. |
| 217 | */ |
| 218 | buf = map_sysmem(addr, len); |
| 219 | ret = info->write(filename, buf, offset, len); |
| 220 | unmap_sysmem(buf); |
| 221 | |
| 222 | /* If we requested a specific number of bytes, check we got it */ |
| 223 | if (ret >= 0 && len && ret != len) { |
| 224 | printf("** Unable to write file %s **\n", filename); |
| 225 | ret = -1; |
| 226 | } |
| 227 | fs_close(); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | |
Stephen Warren | 128d3d9 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 232 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Stephen Warren | 40816a0 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 233 | int fstype, int cmdline_base) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 234 | { |
| 235 | unsigned long addr; |
| 236 | const char *addr_str; |
| 237 | const char *filename; |
| 238 | unsigned long bytes; |
| 239 | unsigned long pos; |
| 240 | int len_read; |
Andreas Bießmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 241 | unsigned long time; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 242 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 243 | if (argc < 2) |
| 244 | return CMD_RET_USAGE; |
| 245 | if (argc > 7) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 246 | return CMD_RET_USAGE; |
| 247 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 248 | 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] | 249 | return 1; |
| 250 | |
| 251 | if (argc >= 4) { |
Stephen Warren | 40816a0 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 252 | addr = simple_strtoul(argv[3], NULL, cmdline_base); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 253 | } else { |
| 254 | addr_str = getenv("loadaddr"); |
| 255 | if (addr_str != NULL) |
| 256 | addr = simple_strtoul(addr_str, NULL, 16); |
| 257 | else |
| 258 | addr = CONFIG_SYS_LOAD_ADDR; |
| 259 | } |
| 260 | if (argc >= 5) { |
| 261 | filename = argv[4]; |
| 262 | } else { |
| 263 | filename = getenv("bootfile"); |
| 264 | if (!filename) { |
| 265 | puts("** No boot file defined **\n"); |
| 266 | return 1; |
| 267 | } |
| 268 | } |
| 269 | if (argc >= 6) |
Stephen Warren | 40816a0 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 270 | bytes = simple_strtoul(argv[5], NULL, cmdline_base); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 271 | else |
| 272 | bytes = 0; |
| 273 | if (argc >= 7) |
Stephen Warren | 40816a0 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 274 | pos = simple_strtoul(argv[6], NULL, cmdline_base); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 275 | else |
| 276 | pos = 0; |
| 277 | |
Andreas Bießmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 278 | time = get_timer(0); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 279 | len_read = fs_read(filename, addr, pos, bytes); |
Andreas Bießmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 280 | time = get_timer(time); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 281 | if (len_read <= 0) |
| 282 | return 1; |
| 283 | |
Andreas Bießmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 284 | printf("%d bytes read in %lu ms", len_read, time); |
| 285 | if (time > 0) { |
| 286 | puts(" ("); |
| 287 | print_size(len_read / time * 1000, "/s"); |
| 288 | puts(")"); |
| 289 | } |
| 290 | puts("\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 291 | |
Simon Glass | fff5c07 | 2013-02-24 17:33:23 +0000 | [diff] [blame] | 292 | setenv_hex("filesize", len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 298 | int fstype) |
| 299 | { |
| 300 | if (argc < 2) |
| 301 | return CMD_RET_USAGE; |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 302 | if (argc > 4) |
| 303 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 304 | |
| 305 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 306 | return 1; |
| 307 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 308 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 309 | return 1; |
| 310 | |
| 311 | return 0; |
| 312 | } |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame^] | 313 | |
| 314 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 315 | int fstype, int cmdline_base) |
| 316 | { |
| 317 | unsigned long addr; |
| 318 | const char *filename; |
| 319 | unsigned long bytes; |
| 320 | unsigned long pos; |
| 321 | int len; |
| 322 | unsigned long time; |
| 323 | |
| 324 | if (argc < 6 || argc > 7) |
| 325 | return CMD_RET_USAGE; |
| 326 | |
| 327 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 328 | return 1; |
| 329 | |
| 330 | filename = argv[3]; |
| 331 | addr = simple_strtoul(argv[4], NULL, cmdline_base); |
| 332 | bytes = simple_strtoul(argv[5], NULL, cmdline_base); |
| 333 | if (argc >= 7) |
| 334 | pos = simple_strtoul(argv[6], NULL, cmdline_base); |
| 335 | else |
| 336 | pos = 0; |
| 337 | |
| 338 | time = get_timer(0); |
| 339 | len = fs_write(filename, addr, pos, bytes); |
| 340 | time = get_timer(time); |
| 341 | if (len <= 0) |
| 342 | return 1; |
| 343 | |
| 344 | printf("%d bytes written in %lu ms", len, time); |
| 345 | if (time > 0) { |
| 346 | puts(" ("); |
| 347 | print_size(len / time * 1000, "/s"); |
| 348 | puts(")"); |
| 349 | } |
| 350 | puts("\n"); |
| 351 | |
| 352 | return 0; |
| 353 | } |