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