2 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
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.
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
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/>.
25 DECLARE_GLOBAL_DATA_PTR;
27 static block_dev_desc_t *fs_dev_desc;
28 static disk_partition_t fs_partition;
29 static int fs_type = FS_TYPE_ANY;
31 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
32 disk_partition_t *fs_partition)
34 printf("** Unrecognized filesystem type **\n");
38 static inline int fs_ls_unsupported(const char *dirname)
43 static inline int fs_read_unsupported(const char *filename, void *buf,
49 static inline void fs_close_unsupported(void)
55 int (*probe)(block_dev_desc_t *fs_dev_desc,
56 disk_partition_t *fs_partition);
57 int (*ls)(const char *dirname);
58 int (*read)(const char *filename, void *buf, int offset, int len);
62 static struct fstype_info fstypes[] = {
65 .fstype = FS_TYPE_FAT,
66 .probe = fat_set_blk_dev,
69 .read = fat_read_file,
74 .fstype = FS_TYPE_EXT,
75 .probe = ext4fs_probe,
76 .close = ext4fs_close,
78 .read = ext4_read_file,
82 .fstype = FS_TYPE_ANY,
83 .probe = fs_probe_unsupported,
84 .close = fs_close_unsupported,
85 .ls = fs_ls_unsupported,
86 .read = fs_read_unsupported,
90 static struct fstype_info *fs_get_info(int fstype)
92 struct fstype_info *info;
95 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
96 if (fstype == info->fstype)
100 /* Return the 'unsupported' sentinel */
104 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
106 struct fstype_info *info;
108 #ifdef CONFIG_NEEDS_MANUAL_RELOC
109 static int relocated;
112 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
114 info->probe += gd->reloc_off;
115 info->close += gd->reloc_off;
116 info->ls += gd->reloc_off;
117 info->read += gd->reloc_off;
123 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
128 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
129 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
130 fstype != info->fstype)
133 if (!info->probe(fs_dev_desc, &fs_partition)) {
134 fs_type = info->fstype;
142 static void fs_close(void)
144 struct fstype_info *info = fs_get_info(fs_type);
148 fs_type = FS_TYPE_ANY;
151 int fs_ls(const char *dirname)
155 struct fstype_info *info = fs_get_info(fs_type);
157 ret = info->ls(dirname);
159 fs_type = FS_TYPE_ANY;
165 int fs_read(const char *filename, ulong addr, int offset, int len)
167 struct fstype_info *info = fs_get_info(fs_type);
172 * We don't actually know how many bytes are being read, since len==0
173 * means read the whole file.
175 buf = map_sysmem(addr, len);
176 ret = info->read(filename, buf, offset, len);
179 /* If we requested a specific number of bytes, check we got it */
180 if (ret >= 0 && len && ret != len) {
181 printf("** Unable to read file %s **\n", filename);
189 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
190 int fstype, int cmdline_base)
193 const char *addr_str;
194 const char *filename;
201 return CMD_RET_USAGE;
203 return CMD_RET_USAGE;
205 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
209 addr = simple_strtoul(argv[3], NULL, cmdline_base);
211 addr_str = getenv("loadaddr");
212 if (addr_str != NULL)
213 addr = simple_strtoul(addr_str, NULL, 16);
215 addr = CONFIG_SYS_LOAD_ADDR;
220 filename = getenv("bootfile");
222 puts("** No boot file defined **\n");
227 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
231 pos = simple_strtoul(argv[6], NULL, cmdline_base);
236 len_read = fs_read(filename, addr, pos, bytes);
237 time = get_timer(time);
241 printf("%d bytes read in %lu ms", len_read, time);
244 print_size(len_read / time * 1000, "/s");
249 setenv_hex("filesize", len_read);
254 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
258 return CMD_RET_USAGE;
260 return CMD_RET_USAGE;
262 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
265 if (fs_ls(argc >= 4 ? argv[3] : "/"))