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 static block_dev_desc_t *fs_dev_desc;
25 static disk_partition_t fs_partition;
26 static int fs_type = FS_TYPE_ANY;
28 static inline int fs_ls_unsupported(const char *dirname)
30 printf("** Unrecognized filesystem type **\n");
34 static inline int fs_read_unsupported(const char *filename, ulong addr,
37 printf("** Unrecognized filesystem type **\n");
42 static int fs_probe_fat(void)
44 return fat_set_blk_dev(fs_dev_desc, &fs_partition);
47 static void fs_close_fat(void)
51 #define fs_ls_fat file_fat_ls
53 static int fs_read_fat(const char *filename, ulong addr, int offset, int len)
57 len_read = file_fat_read_at(filename, offset,
58 (unsigned char *)addr, len);
60 printf("** Unable to read file %s **\n", filename);
67 static inline int fs_probe_fat(void)
72 static inline void fs_close_fat(void)
76 #define fs_ls_fat fs_ls_unsupported
77 #define fs_read_fat fs_read_unsupported
81 static int fs_probe_ext(void)
83 ext4fs_set_blk_dev(fs_dev_desc, &fs_partition);
85 if (!ext4fs_mount(fs_partition.size)) {
93 static void fs_close_ext(void)
98 #define fs_ls_ext ext4fs_ls
100 static int fs_read_ext(const char *filename, ulong addr, int offset, int len)
106 printf("** Cannot support non-zero offset **\n");
110 file_len = ext4fs_open(filename);
112 printf("** File not found %s **\n", filename);
120 len_read = ext4fs_read((char *)addr, len);
123 if (len_read != len) {
124 printf("** Unable to read file %s **\n", filename);
131 static inline int fs_probe_ext(void)
136 static inline void fs_close_ext(void)
140 #define fs_ls_ext fs_ls_unsupported
141 #define fs_read_ext fs_read_unsupported
144 static const struct {
149 .fstype = FS_TYPE_FAT,
150 .probe = fs_probe_fat,
153 .fstype = FS_TYPE_EXT,
154 .probe = fs_probe_ext,
158 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
162 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
167 for (i = 0; i < ARRAY_SIZE(fstypes); i++) {
168 if ((fstype != FS_TYPE_ANY) && (fstype != fstypes[i].fstype))
171 if (!fstypes[i].probe()) {
172 fs_type = fstypes[i].fstype;
177 printf("** Unrecognized filesystem type **\n");
181 static void fs_close(void)
194 fs_type = FS_TYPE_ANY;
197 int fs_ls(const char *dirname)
203 ret = fs_ls_fat(dirname);
206 ret = fs_ls_ext(dirname);
209 ret = fs_ls_unsupported(dirname);
218 int fs_read(const char *filename, ulong addr, int offset, int len)
224 ret = fs_read_fat(filename, addr, offset, len);
227 ret = fs_read_ext(filename, addr, offset, len);
230 ret = fs_read_unsupported(filename, addr, offset, len);
239 int do_fsload(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
243 const char *addr_str;
244 const char *filename;
251 return CMD_RET_USAGE;
253 if (fs_set_blk_dev(argv[1], argv[2], fstype))
257 addr = simple_strtoul(argv[3], NULL, 0);
259 addr_str = getenv("loadaddr");
260 if (addr_str != NULL)
261 addr = simple_strtoul(addr_str, NULL, 16);
263 addr = CONFIG_SYS_LOAD_ADDR;
268 filename = getenv("bootfile");
270 puts("** No boot file defined **\n");
275 bytes = simple_strtoul(argv[5], NULL, 0);
279 pos = simple_strtoul(argv[6], NULL, 0);
283 len_read = fs_read(filename, addr, pos, bytes);
287 printf("%d bytes read\n", len_read);
289 sprintf(buf, "0x%x", len_read);
290 setenv("filesize", buf);
295 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
299 return CMD_RET_USAGE;
301 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
304 if (fs_ls(argc == 4 ? argv[3] : "/"))