Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <config.h> |
| 18 | #include <common.h> |
| 19 | #include <part.h> |
| 20 | #include <ext4fs.h> |
| 21 | #include <fat.h> |
| 22 | #include <fs.h> |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 23 | #include <sandboxfs.h> |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 24 | #include <asm/io.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 25 | |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Stephen Warren | 045fa1e | 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 | 2ded0d4 | 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 | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 34 | { |
| 35 | printf("** Unrecognized filesystem type **\n"); |
| 36 | return -1; |
| 37 | } |
| 38 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 39 | static inline int fs_ls_unsupported(const char *dirname) |
| 40 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 44 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 45 | int offset, int len) |
| 46 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 47 | return -1; |
| 48 | } |
| 49 | |
Simon Glass | a8f6ab5 | 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 | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 56 | static inline void fs_close_unsupported(void) |
| 57 | { |
| 58 | } |
| 59 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 60 | struct fstype_info { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 61 | int fstype; |
Simon Glass | 2ded0d4 | 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 | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 64 | int (*ls)(const char *dirname); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 65 | int (*read)(const char *filename, void *buf, int offset, int len); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 66 | int (*write)(const char *filename, void *buf, int offset, int len); |
Simon Glass | 436e2b7 | 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 | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 72 | { |
| 73 | .fstype = FS_TYPE_FAT, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 74 | .probe = fat_set_blk_dev, |
| 75 | .close = fat_close, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 76 | .ls = file_fat_ls, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 77 | .read = fat_read_file, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 78 | }, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 79 | #endif |
| 80 | #ifdef CONFIG_FS_EXT4 |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 81 | { |
| 82 | .fstype = FS_TYPE_EXT, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 83 | .probe = ext4fs_probe, |
| 84 | .close = ext4fs_close, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 85 | .ls = ext4fs_ls, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 86 | .read = ext4_read_file, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 87 | }, |
| 88 | #endif |
Simon Glass | 92ccc96 | 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, |
Simon Glass | 7eb2c8d | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 96 | .write = fs_write_sandbox, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 97 | }, |
| 98 | #endif |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 99 | { |
| 100 | .fstype = FS_TYPE_ANY, |
| 101 | .probe = fs_probe_unsupported, |
| 102 | .close = fs_close_unsupported, |
| 103 | .ls = fs_ls_unsupported, |
| 104 | .read = fs_read_unsupported, |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 105 | .write = fs_write_unsupported, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 106 | }, |
| 107 | }; |
| 108 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 109 | static struct fstype_info *fs_get_info(int fstype) |
| 110 | { |
| 111 | struct fstype_info *info; |
| 112 | int i; |
| 113 | |
| 114 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 115 | if (fstype == info->fstype) |
| 116 | return info; |
| 117 | } |
| 118 | |
| 119 | /* Return the 'unsupported' sentinel */ |
| 120 | return info; |
| 121 | } |
| 122 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 123 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 124 | { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 125 | struct fstype_info *info; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 126 | int part, i; |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 127 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 128 | static int relocated; |
| 129 | |
| 130 | if (!relocated) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 131 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 132 | i++, info++) { |
| 133 | info->probe += gd->reloc_off; |
| 134 | info->close += gd->reloc_off; |
| 135 | info->ls += gd->reloc_off; |
| 136 | info->read += gd->reloc_off; |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 137 | info->write += gd->reloc_off; |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 138 | } |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 139 | relocated = 1; |
| 140 | } |
| 141 | #endif |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 142 | |
| 143 | part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc, |
| 144 | &fs_partition, 1); |
| 145 | if (part < 0) |
| 146 | return -1; |
| 147 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 148 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 149 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 150 | fstype != info->fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 151 | continue; |
| 152 | |
Simon Glass | 2ded0d4 | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 153 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 154 | fs_type = info->fstype; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | } |
| 158 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | static void fs_close(void) |
| 163 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 164 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 165 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 166 | info->close(); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 167 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 168 | fs_type = FS_TYPE_ANY; |
| 169 | } |
| 170 | |
| 171 | int fs_ls(const char *dirname) |
| 172 | { |
| 173 | int ret; |
| 174 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 175 | struct fstype_info *info = fs_get_info(fs_type); |
| 176 | |
| 177 | ret = info->ls(dirname); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 178 | |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 179 | fs_type = FS_TYPE_ANY; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 180 | fs_close(); |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | int fs_read(const char *filename, ulong addr, int offset, int len) |
| 186 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 187 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 188 | void *buf; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 189 | int ret; |
| 190 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 191 | /* |
| 192 | * We don't actually know how many bytes are being read, since len==0 |
| 193 | * means read the whole file. |
| 194 | */ |
| 195 | buf = map_sysmem(addr, len); |
| 196 | ret = info->read(filename, buf, offset, len); |
| 197 | unmap_sysmem(buf); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 198 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 199 | /* If we requested a specific number of bytes, check we got it */ |
| 200 | if (ret >= 0 && len && ret != len) { |
| 201 | printf("** Unable to read file %s **\n", filename); |
| 202 | ret = -1; |
| 203 | } |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 204 | fs_close(); |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 209 | int fs_write(const char *filename, ulong addr, int offset, int len) |
| 210 | { |
| 211 | struct fstype_info *info = fs_get_info(fs_type); |
| 212 | void *buf; |
| 213 | int ret; |
| 214 | |
| 215 | /* |
| 216 | * We don't actually know how many bytes are being read, since len==0 |
| 217 | * means read the whole file. |
| 218 | */ |
| 219 | buf = map_sysmem(addr, len); |
| 220 | ret = info->write(filename, buf, offset, len); |
| 221 | unmap_sysmem(buf); |
| 222 | |
| 223 | /* If we requested a specific number of bytes, check we got it */ |
| 224 | if (ret >= 0 && len && ret != len) { |
| 225 | printf("** Unable to write file %s **\n", filename); |
| 226 | ret = -1; |
| 227 | } |
| 228 | fs_close(); |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
Stephen Warren | f9b55e2 | 2012-10-31 11:05:07 +0000 | [diff] [blame] | 233 | int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
Stephen Warren | 3f83c87 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 234 | int fstype, int cmdline_base) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 235 | { |
| 236 | unsigned long addr; |
| 237 | const char *addr_str; |
| 238 | const char *filename; |
| 239 | unsigned long bytes; |
| 240 | unsigned long pos; |
| 241 | int len_read; |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 242 | unsigned long time; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 243 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 244 | if (argc < 2) |
| 245 | return CMD_RET_USAGE; |
| 246 | if (argc > 7) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 247 | return CMD_RET_USAGE; |
| 248 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 249 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 250 | return 1; |
| 251 | |
| 252 | if (argc >= 4) { |
Stephen Warren | 3f83c87 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 253 | addr = simple_strtoul(argv[3], NULL, cmdline_base); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 254 | } else { |
| 255 | addr_str = getenv("loadaddr"); |
| 256 | if (addr_str != NULL) |
| 257 | addr = simple_strtoul(addr_str, NULL, 16); |
| 258 | else |
| 259 | addr = CONFIG_SYS_LOAD_ADDR; |
| 260 | } |
| 261 | if (argc >= 5) { |
| 262 | filename = argv[4]; |
| 263 | } else { |
| 264 | filename = getenv("bootfile"); |
| 265 | if (!filename) { |
| 266 | puts("** No boot file defined **\n"); |
| 267 | return 1; |
| 268 | } |
| 269 | } |
| 270 | if (argc >= 6) |
Stephen Warren | 3f83c87 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 271 | bytes = simple_strtoul(argv[5], NULL, cmdline_base); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 272 | else |
| 273 | bytes = 0; |
| 274 | if (argc >= 7) |
Stephen Warren | 3f83c87 | 2012-10-30 12:04:19 +0000 | [diff] [blame] | 275 | pos = simple_strtoul(argv[6], NULL, cmdline_base); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 276 | else |
| 277 | pos = 0; |
| 278 | |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 279 | time = get_timer(0); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 280 | len_read = fs_read(filename, addr, pos, bytes); |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 281 | time = get_timer(time); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 282 | if (len_read <= 0) |
| 283 | return 1; |
| 284 | |
Andreas Bießmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 285 | printf("%d bytes read in %lu ms", len_read, time); |
| 286 | if (time > 0) { |
| 287 | puts(" ("); |
| 288 | print_size(len_read / time * 1000, "/s"); |
| 289 | puts(")"); |
| 290 | } |
| 291 | puts("\n"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 292 | |
Simon Glass | 49c4f03 | 2013-02-24 17:33:23 +0000 | [diff] [blame] | 293 | setenv_hex("filesize", len_read); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 299 | int fstype) |
| 300 | { |
| 301 | if (argc < 2) |
| 302 | return CMD_RET_USAGE; |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 303 | if (argc > 4) |
| 304 | return CMD_RET_USAGE; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 305 | |
| 306 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 307 | return 1; |
| 308 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 309 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 310 | return 1; |
| 311 | |
| 312 | return 0; |
| 313 | } |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 314 | |
| 315 | int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
| 316 | int fstype, int cmdline_base) |
| 317 | { |
| 318 | unsigned long addr; |
| 319 | const char *filename; |
| 320 | unsigned long bytes; |
| 321 | unsigned long pos; |
| 322 | int len; |
| 323 | unsigned long time; |
| 324 | |
| 325 | if (argc < 6 || argc > 7) |
| 326 | return CMD_RET_USAGE; |
| 327 | |
| 328 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 329 | return 1; |
| 330 | |
| 331 | filename = argv[3]; |
| 332 | addr = simple_strtoul(argv[4], NULL, cmdline_base); |
| 333 | bytes = simple_strtoul(argv[5], NULL, cmdline_base); |
| 334 | if (argc >= 7) |
| 335 | pos = simple_strtoul(argv[6], NULL, cmdline_base); |
| 336 | else |
| 337 | pos = 0; |
| 338 | |
| 339 | time = get_timer(0); |
| 340 | len = fs_write(filename, addr, pos, bytes); |
| 341 | time = get_timer(time); |
| 342 | if (len <= 0) |
| 343 | return 1; |
| 344 | |
| 345 | printf("%d bytes written in %lu ms", len, time); |
| 346 | if (time > 0) { |
| 347 | puts(" ("); |
| 348 | print_size(len / time * 1000, "/s"); |
| 349 | puts(")"); |
| 350 | } |
| 351 | puts("\n"); |
| 352 | |
| 353 | return 0; |
| 354 | } |