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_exists_unsupported(const char *filename)
49 static inline int fs_read_unsupported(const char *filename, void *buf,
55 static inline int fs_write_unsupported(const char *filename, void *buf,
61 static inline void fs_close_unsupported(void)
68 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
69 * should be false in most cases. For "virtual" filesystems which
70 * aren't based on a U-Boot block device (e.g. sandbox), this can be
71 * set to true. This should also be true for the dumm entry at the end
72 * of fstypes[], since that is essentially a "virtual" (non-existent)
75 bool null_dev_desc_ok;
76 int (*probe)(block_dev_desc_t *fs_dev_desc,
77 disk_partition_t *fs_partition);
78 int (*ls)(const char *dirname);
79 int (*exists)(const char *filename);
80 int (*read)(const char *filename, void *buf, int offset, int len);
81 int (*write)(const char *filename, void *buf, int offset, int len);
85 static struct fstype_info fstypes[] = {
88 .fstype = FS_TYPE_FAT,
89 .null_dev_desc_ok = false,
90 .probe = fat_set_blk_dev,
94 .read = fat_read_file,
95 .write = fs_write_unsupported,
100 .fstype = FS_TYPE_EXT,
101 .null_dev_desc_ok = false,
102 .probe = ext4fs_probe,
103 .close = ext4fs_close,
105 .exists = ext4fs_exists,
106 .read = ext4_read_file,
107 .write = fs_write_unsupported,
110 #ifdef CONFIG_SANDBOX
112 .fstype = FS_TYPE_SANDBOX,
113 .null_dev_desc_ok = true,
114 .probe = sandbox_fs_set_blk_dev,
115 .close = sandbox_fs_close,
117 .exists = sandbox_fs_exists,
118 .read = fs_read_sandbox,
119 .write = fs_write_sandbox,
123 .fstype = FS_TYPE_ANY,
124 .null_dev_desc_ok = true,
125 .probe = fs_probe_unsupported,
126 .close = fs_close_unsupported,
127 .ls = fs_ls_unsupported,
128 .exists = fs_exists_unsupported,
129 .read = fs_read_unsupported,
130 .write = fs_write_unsupported,
134 static struct fstype_info *fs_get_info(int fstype)
136 struct fstype_info *info;
139 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
140 if (fstype == info->fstype)
144 /* Return the 'unsupported' sentinel */
148 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
150 struct fstype_info *info;
152 #ifdef CONFIG_NEEDS_MANUAL_RELOC
153 static int relocated;
156 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
158 info->probe += gd->reloc_off;
159 info->close += gd->reloc_off;
160 info->ls += gd->reloc_off;
161 info->read += gd->reloc_off;
162 info->write += gd->reloc_off;
168 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
173 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
174 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
175 fstype != info->fstype)
178 if (!fs_dev_desc && !info->null_dev_desc_ok)
181 if (!info->probe(fs_dev_desc, &fs_partition)) {
182 fs_type = info->fstype;
190 static void fs_close(void)
192 struct fstype_info *info = fs_get_info(fs_type);
196 fs_type = FS_TYPE_ANY;
199 int fs_ls(const char *dirname)
203 struct fstype_info *info = fs_get_info(fs_type);
205 ret = info->ls(dirname);
207 fs_type = FS_TYPE_ANY;
213 int fs_exists(const char *filename)
217 struct fstype_info *info = fs_get_info(fs_type);
219 ret = info->exists(filename);
226 int fs_read(const char *filename, ulong addr, int offset, int len)
228 struct fstype_info *info = fs_get_info(fs_type);
233 * We don't actually know how many bytes are being read, since len==0
234 * means read the whole file.
236 buf = map_sysmem(addr, len);
237 ret = info->read(filename, buf, offset, len);
240 /* If we requested a specific number of bytes, check we got it */
241 if (ret >= 0 && len && ret != len) {
242 printf("** Unable to read file %s **\n", filename);
250 int fs_write(const char *filename, ulong addr, int offset, int len)
252 struct fstype_info *info = fs_get_info(fs_type);
256 buf = map_sysmem(addr, len);
257 ret = info->write(filename, buf, offset, len);
260 if (ret >= 0 && ret != len) {
261 printf("** Unable to write file %s **\n", filename);
269 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
273 const char *addr_str;
274 const char *filename;
282 return CMD_RET_USAGE;
284 return CMD_RET_USAGE;
286 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
290 addr = simple_strtoul(argv[3], &ep, 16);
291 if (ep == argv[3] || *ep != '\0')
292 return CMD_RET_USAGE;
294 addr_str = getenv("loadaddr");
295 if (addr_str != NULL)
296 addr = simple_strtoul(addr_str, NULL, 16);
298 addr = CONFIG_SYS_LOAD_ADDR;
303 filename = getenv("bootfile");
305 puts("** No boot file defined **\n");
310 bytes = simple_strtoul(argv[5], NULL, 16);
314 pos = simple_strtoul(argv[6], NULL, 16);
319 len_read = fs_read(filename, addr, pos, bytes);
320 time = get_timer(time);
324 printf("%d bytes read in %lu ms", len_read, time);
327 print_size(len_read / time * 1000, "/s");
332 setenv_hex("filesize", len_read);
337 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
341 return CMD_RET_USAGE;
343 return CMD_RET_USAGE;
345 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
348 if (fs_ls(argc >= 4 ? argv[3] : "/"))
354 int file_exists(const char *dev_type, const char *dev_part, const char *file,
357 if (fs_set_blk_dev(dev_type, dev_part, fstype))
360 return fs_exists(file);
363 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
367 const char *filename;
373 if (argc < 6 || argc > 7)
374 return CMD_RET_USAGE;
376 if (fs_set_blk_dev(argv[1], argv[2], fstype))
380 addr = simple_strtoul(argv[4], NULL, 16);
381 bytes = simple_strtoul(argv[5], NULL, 16);
383 pos = simple_strtoul(argv[6], NULL, 16);
388 len = fs_write(filename, addr, pos, bytes);
389 time = get_timer(time);
393 printf("%d bytes written in %lu ms", len, time);
396 print_size(len / time * 1000, "/s");