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(void)
32 printf("** Unrecognized filesystem type **\n");
36 static inline int fs_ls_unsupported(const char *dirname)
41 static inline int fs_read_unsupported(const char *filename, ulong addr,
47 static inline void fs_close_unsupported(void)
52 static int fs_probe_fat(void)
54 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
57 static void fs_close_fat(void)
61 #define fs_ls_fat file_fat_ls
63 static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
67 len_read = file_fat_read_at(filename, offset,
68 (unsigned char *)addr, len);
70 printf("** Unable to read file %s **\n", filename);
77 static inline int fs_probe_fat(void)
82 static inline void fs_close_fat(void)
86 #define fs_ls_fat fs_ls_unsupported
87 #define fs_read_fat fs_read_unsupported
91 static int fs_probe_ext(void)
93 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
95 if (!ext4fs_mount(fs_partition.size)) {
103 static void fs_close_ext(void)
108 #define fs_ls_ext ext4fs_ls
110 static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
116 printf("** Cannot support non-zero offset **\n");
120 file_len = ext4fs_open(filename);
122 printf("** File not found %s **\n", filename);
130 len_read = ext4fs_read((char *)addr, len);
133 if (len_read != len) {
134 printf("** Unable to read file %s **\n", filename);
141 static inline int fs_probe_ext(void)
146 static inline void fs_close_ext(void)
150 #define fs_ls_ext fs_ls_unsupported
151 #define fs_read_ext fs_read_unsupported
157 int (*ls)(const char *dirname);
158 int (*read)(const char *filename, ulong addr, int offset, int len);
162 static struct fstype_info fstypes[] = {
165 .fstype = FS_TYPE_FAT,
166 .probe = fs_probe_fat,
167 .close = fs_close_fat,
172 #ifdef CONFIG_FS_EXT4
174 .fstype = FS_TYPE_EXT,
175 .probe = fs_probe_ext,
176 .close = fs_close_ext,
182 .fstype = FS_TYPE_ANY,
183 .probe = fs_probe_unsupported,
184 .close = fs_close_unsupported,
185 .ls = fs_ls_unsupported,
186 .read = fs_read_unsupported,
190 static struct fstype_info *fs_get_info(int fstype)
192 struct fstype_info *info;
195 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
196 if (fstype == info->fstype)
200 /* Return the 'unsupported' sentinel */
204 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
206 struct fstype_info *info;
208 #ifdef CONFIG_NEEDS_MANUAL_RELOC
209 static int relocated;
212 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
214 info->probe += gd->reloc_off;
215 info->close += gd->reloc_off;
216 info->ls += gd->reloc_off;
217 info->read += gd->reloc_off;
223 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
228 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
229 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
230 fstype != info->fstype)
233 if (!info->probe()) {
234 fs_type = info->fstype;
242 static void fs_close(void)
244 struct fstype_info *info = fs_get_info(fs_type);
247 fs_type = FS_TYPE_ANY;
250 int fs_ls(const char *dirname)
254 struct fstype_info *info = fs_get_info(fs_type);
256 ret = info->ls(dirname);
263 int fs_read(const char *filename, ulong addr, int offset, int len)
265 struct fstype_info *info = fs_get_info(fs_type);
268 ret = info->read(filename, addr, offset, len);
270 /* If we requested a specific number of bytes, check we got it */
271 if (ret >= 0 && len && ret != len) {
272 printf("** Unable to read file %s **\n", filename);
280 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
281 int fstype, int cmdline_base)
284 const char *addr_str;
285 const char *filename;
292 return CMD_RET_USAGE;
294 return CMD_RET_USAGE;
296 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
300 addr = simple_strtoul(argv[3], NULL, cmdline_base);
302 addr_str = getenv("loadaddr");
303 if (addr_str != NULL)
304 addr = simple_strtoul(addr_str, NULL, 16);
306 addr = CONFIG_SYS_LOAD_ADDR;
311 filename = getenv("bootfile");
313 puts("** No boot file defined **\n");
318 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
322 pos = simple_strtoul(argv[6], NULL, cmdline_base);
327 len_read = fs_read(filename, addr, pos, bytes);
328 time = get_timer(time);
332 printf("%d bytes read in %lu ms", len_read, time);
335 print_size(len_read / time * 1000, "/s");
340 setenv_hex("filesize", len_read);
345 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
349 return CMD_RET_USAGE;
351 return CMD_RET_USAGE;
353 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
356 if (fs_ls(argc >= 4 ? argv[3] : "/"))