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/>.
23 #include <sandboxfs.h>
26 DECLARE_GLOBAL_DATA_PTR;
28 static block_dev_desc_t *fs_dev_desc;
29 static disk_partition_t fs_partition;
30 static int fs_type = FS_TYPE_ANY;
32 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
33 disk_partition_t *fs_partition)
35 printf("** Unrecognized filesystem type **\n");
39 static inline int fs_ls_unsupported(const char *dirname)
44 static inline int fs_read_unsupported(const char *filename, void *buf,
50 static inline int fs_write_unsupported(const char *filename, void *buf,
56 static inline void fs_close_unsupported(void)
62 int (*probe)(block_dev_desc_t *fs_dev_desc,
63 disk_partition_t *fs_partition);
64 int (*ls)(const char *dirname);
65 int (*read)(const char *filename, void *buf, int offset, int len);
66 int (*write)(const char *filename, void *buf, int offset, int len);
70 static struct fstype_info fstypes[] = {
73 .fstype = FS_TYPE_FAT,
74 .probe = fat_set_blk_dev,
77 .read = fat_read_file,
78 .write = fs_write_unsupported,
83 .fstype = FS_TYPE_EXT,
84 .probe = ext4fs_probe,
85 .close = ext4fs_close,
87 .read = ext4_read_file,
88 .write = fs_write_unsupported,
93 .fstype = FS_TYPE_SANDBOX,
94 .probe = sandbox_fs_set_blk_dev,
95 .close = sandbox_fs_close,
97 .read = fs_read_sandbox,
98 .write = fs_write_sandbox,
102 .fstype = FS_TYPE_ANY,
103 .probe = fs_probe_unsupported,
104 .close = fs_close_unsupported,
105 .ls = fs_ls_unsupported,
106 .read = fs_read_unsupported,
107 .write = fs_write_unsupported,
111 static struct fstype_info *fs_get_info(int fstype)
113 struct fstype_info *info;
116 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
117 if (fstype == info->fstype)
121 /* Return the 'unsupported' sentinel */
125 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
127 struct fstype_info *info;
129 #ifdef CONFIG_NEEDS_MANUAL_RELOC
130 static int relocated;
133 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
135 info->probe += gd->reloc_off;
136 info->close += gd->reloc_off;
137 info->ls += gd->reloc_off;
138 info->read += gd->reloc_off;
139 info->write += gd->reloc_off;
145 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
150 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
151 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
152 fstype != info->fstype)
155 if (!info->probe(fs_dev_desc, &fs_partition)) {
156 fs_type = info->fstype;
164 static void fs_close(void)
166 struct fstype_info *info = fs_get_info(fs_type);
170 fs_type = FS_TYPE_ANY;
173 int fs_ls(const char *dirname)
177 struct fstype_info *info = fs_get_info(fs_type);
179 ret = info->ls(dirname);
181 fs_type = FS_TYPE_ANY;
187 int fs_read(const char *filename, ulong addr, int offset, int len)
189 struct fstype_info *info = fs_get_info(fs_type);
194 * We don't actually know how many bytes are being read, since len==0
195 * means read the whole file.
197 buf = map_sysmem(addr, len);
198 ret = info->read(filename, buf, offset, len);
201 /* If we requested a specific number of bytes, check we got it */
202 if (ret >= 0 && len && ret != len) {
203 printf("** Unable to read file %s **\n", filename);
211 int fs_write(const char *filename, ulong addr, int offset, int len)
213 struct fstype_info *info = fs_get_info(fs_type);
217 buf = map_sysmem(addr, len);
218 ret = info->write(filename, buf, offset, len);
221 if (ret >= 0 && ret != len) {
222 printf("** Unable to write file %s **\n", filename);
230 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
234 const char *addr_str;
235 const char *filename;
242 return CMD_RET_USAGE;
244 return CMD_RET_USAGE;
246 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
250 addr = simple_strtoul(argv[3], NULL, 16);
252 addr_str = getenv("loadaddr");
253 if (addr_str != NULL)
254 addr = simple_strtoul(addr_str, NULL, 16);
256 addr = CONFIG_SYS_LOAD_ADDR;
261 filename = getenv("bootfile");
263 puts("** No boot file defined **\n");
268 bytes = simple_strtoul(argv[5], NULL, 16);
272 pos = simple_strtoul(argv[6], NULL, 16);
277 len_read = fs_read(filename, addr, pos, bytes);
278 time = get_timer(time);
282 printf("%d bytes read in %lu ms", len_read, time);
285 print_size(len_read / time * 1000, "/s");
290 setenv_hex("filesize", len_read);
295 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
299 return CMD_RET_USAGE;
301 return CMD_RET_USAGE;
303 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
306 if (fs_ls(argc >= 4 ? argv[3] : "/"))
312 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
316 const char *filename;
322 if (argc < 6 || argc > 7)
323 return CMD_RET_USAGE;
325 if (fs_set_blk_dev(argv[1], argv[2], fstype))
329 addr = simple_strtoul(argv[4], NULL, 16);
330 bytes = simple_strtoul(argv[5], NULL, 16);
332 pos = simple_strtoul(argv[6], NULL, 16);
337 len = fs_write(filename, addr, pos, bytes);
338 time = get_timer(time);
342 printf("%d bytes written in %lu ms", len, time);
345 print_size(len / time * 1000, "/s");