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/>.
24 DECLARE_GLOBAL_DATA_PTR;
26 static block_dev_desc_t *fs_dev_desc;
27 static disk_partition_t fs_partition;
28 static int fs_type = FS_TYPE_ANY;
30 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
31 disk_partition_t *fs_partition)
33 printf("** Unrecognized filesystem type **\n");
37 static inline int fs_ls_unsupported(const char *dirname)
42 static inline int fs_read_unsupported(const char *filename, ulong addr,
48 static inline void fs_close_unsupported(void)
53 static int fs_probe_fat(block_dev_desc_t *fs_dev_desc,
54 disk_partition_t *fs_partition)
56 return fat_set_blk_dev(fs_dev_desc, fs_partition);
59 static void fs_close_fat(void)
63 #define fs_ls_fat file_fat_ls
65 static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
69 len_read = file_fat_read_at(filename, offset,
70 (unsigned char *)addr, len);
72 printf("** Unable to read file %s **\n", filename);
79 static inline int fs_probe_fat(void)
84 static inline void fs_close_fat(void)
88 #define fs_ls_fat fs_ls_unsupported
89 #define fs_read_fat fs_read_unsupported
93 static int fs_probe_ext(block_dev_desc_t *fs_dev_desc,
94 disk_partition_t *fs_partition)
96 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
98 if (!ext4fs_mount(fs_partition->size)) {
106 static void fs_close_ext(void)
111 #define fs_ls_ext ext4fs_ls
113 static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
119 printf("** Cannot support non-zero offset **\n");
123 file_len = ext4fs_open(filename);
125 printf("** File not found %s **\n", filename);
133 len_read = ext4fs_read((char *)addr, len);
136 if (len_read != len) {
137 printf("** Unable to read file %s **\n", filename);
144 static inline int fs_probe_ext(void)
149 static inline void fs_close_ext(void)
153 #define fs_ls_ext fs_ls_unsupported
154 #define fs_read_ext fs_read_unsupported
159 int (*probe)(block_dev_desc_t *fs_dev_desc,
160 disk_partition_t *fs_partition);
161 int (*ls)(const char *dirname);
162 int (*read)(const char *filename, ulong addr, int offset, int len);
166 static struct fstype_info fstypes[] = {
169 .fstype = FS_TYPE_FAT,
170 .probe = fs_probe_fat,
171 .close = fs_close_fat,
176 #ifdef CONFIG_FS_EXT4
178 .fstype = FS_TYPE_EXT,
179 .probe = fs_probe_ext,
180 .close = fs_close_ext,
186 .fstype = FS_TYPE_ANY,
187 .probe = fs_probe_unsupported,
188 .close = fs_close_unsupported,
189 .ls = fs_ls_unsupported,
190 .read = fs_read_unsupported,
194 static struct fstype_info *fs_get_info(int fstype)
196 struct fstype_info *info;
199 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
200 if (fstype == info->fstype)
204 /* Return the 'unsupported' sentinel */
208 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
210 struct fstype_info *info;
212 #ifdef CONFIG_NEEDS_MANUAL_RELOC
213 static int relocated;
216 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
218 info->probe += gd->reloc_off;
219 info->close += gd->reloc_off;
220 info->ls += gd->reloc_off;
221 info->read += gd->reloc_off;
227 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
232 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
233 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
234 fstype != info->fstype)
237 if (!info->probe(fs_dev_desc, &fs_partition)) {
238 fs_type = info->fstype;
246 static void fs_close(void)
248 struct fstype_info *info = fs_get_info(fs_type);
251 fs_type = FS_TYPE_ANY;
254 int fs_ls(const char *dirname)
258 struct fstype_info *info = fs_get_info(fs_type);
260 ret = info->ls(dirname);
267 int fs_read(const char *filename, ulong addr, int offset, int len)
269 struct fstype_info *info = fs_get_info(fs_type);
272 ret = info->read(filename, addr, offset, len);
274 /* If we requested a specific number of bytes, check we got it */
275 if (ret >= 0 && len && ret != len) {
276 printf("** Unable to read file %s **\n", filename);
284 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
285 int fstype, int cmdline_base)
288 const char *addr_str;
289 const char *filename;
296 return CMD_RET_USAGE;
298 return CMD_RET_USAGE;
300 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
304 addr = simple_strtoul(argv[3], NULL, cmdline_base);
306 addr_str = getenv("loadaddr");
307 if (addr_str != NULL)
308 addr = simple_strtoul(addr_str, NULL, 16);
310 addr = CONFIG_SYS_LOAD_ADDR;
315 filename = getenv("bootfile");
317 puts("** No boot file defined **\n");
322 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
326 pos = simple_strtoul(argv[6], NULL, cmdline_base);
331 len_read = fs_read(filename, addr, pos, bytes);
332 time = get_timer(time);
336 printf("%d bytes read in %lu ms", len_read, time);
339 print_size(len_read / time * 1000, "/s");
344 setenv_hex("filesize", len_read);
349 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
353 return CMD_RET_USAGE;
355 return CMD_RET_USAGE;
357 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
360 if (fs_ls(argc >= 4 ? argv[3] : "/"))