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_ls_unsupported(const char *dirname)
32 printf("** Unrecognized filesystem type **\n");
36 static inline int fs_read_unsupported(const char *filename, ulong addr,
39 printf("** Unrecognized filesystem type **\n");
44 static int fs_probe_fat(void)
46 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
49 static void fs_close_fat(void)
53 #define fs_ls_fat file_fat_ls
55 static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
59 len_read = file_fat_read_at(filename, offset,
60 (unsigned char *)addr, len);
62 printf("** Unable to read file %s **\n", filename);
69 static inline int fs_probe_fat(void)
74 static inline void fs_close_fat(void)
78 #define fs_ls_fat fs_ls_unsupported
79 #define fs_read_fat fs_read_unsupported
83 static int fs_probe_ext(void)
85 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
87 if (!ext4fs_mount(fs_partition.size)) {
95 static void fs_close_ext(void)
100 #define fs_ls_ext ext4fs_ls
102 static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
108 printf("** Cannot support non-zero offset **\n");
112 file_len = ext4fs_open(filename);
114 printf("** File not found %s **\n", filename);
122 len_read = ext4fs_read((char *)addr, len);
125 if (len_read != len) {
126 printf("** Unable to read file %s **\n", filename);
133 static inline int fs_probe_ext(void)
138 static inline void fs_close_ext(void)
142 #define fs_ls_ext fs_ls_unsupported
143 #define fs_read_ext fs_read_unsupported
151 .fstype = FS_TYPE_FAT,
152 .probe = fs_probe_fat,
155 .fstype = FS_TYPE_EXT,
156 .probe = fs_probe_ext,
160 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
163 #ifdef CONFIG_NEEDS_MANUAL_RELOC
164 static int relocated;
167 for (i = 0; i < ARRAY_SIZE(fstypes); i++)
168 fstypes[i].probe += gd->reloc_off;
173 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
178 for (i = 0; i < ARRAY_SIZE(fstypes); i++) {
179 if ((fstype != FS_TYPE_ANY) && (fstype != fstypes[i].fstype))
182 if (!fstypes[i].probe()) {
183 fs_type = fstypes[i].fstype;
188 printf("** Unrecognized filesystem type **\n");
192 static void fs_close(void)
205 fs_type = FS_TYPE_ANY;
208 int fs_ls(const char *dirname)
214 ret = fs_ls_fat(dirname);
217 ret = fs_ls_ext(dirname);
220 ret = fs_ls_unsupported(dirname);
229 int fs_read(const char *filename, ulong addr, int offset, int len)
235 ret = fs_read_fat(filename, addr, offset, len);
238 ret = fs_read_ext(filename, addr, offset, len);
241 ret = fs_read_unsupported(filename, addr, offset, len);
250 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
251 int fstype, int cmdline_base)
254 const char *addr_str;
255 const char *filename;
262 return CMD_RET_USAGE;
264 return CMD_RET_USAGE;
266 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
270 addr = simple_strtoul(argv[3], NULL, cmdline_base);
272 addr_str = getenv("loadaddr");
273 if (addr_str != NULL)
274 addr = simple_strtoul(addr_str, NULL, 16);
276 addr = CONFIG_SYS_LOAD_ADDR;
281 filename = getenv("bootfile");
283 puts("** No boot file defined **\n");
288 bytes = simple_strtoul(argv[5], NULL, cmdline_base);
292 pos = simple_strtoul(argv[6], NULL, cmdline_base);
296 len_read = fs_read(filename, addr, pos, bytes);
300 printf("%d bytes read\n", len_read);
302 sprintf(buf, "0x%x", len_read);
303 setenv("filesize", buf);
308 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
312 return CMD_RET_USAGE;
314 return CMD_RET_USAGE;
316 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
319 if (fs_ls(argc >= 4 ? argv[3] : "/"))