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_size_unsupported(const char *filename)
54 static inline int fs_read_unsupported(const char *filename, void *buf,
60 static inline int fs_write_unsupported(const char *filename, void *buf,
66 static inline void fs_close_unsupported(void)
73 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
74 * should be false in most cases. For "virtual" filesystems which
75 * aren't based on a U-Boot block device (e.g. sandbox), this can be
76 * set to true. This should also be true for the dumm entry at the end
77 * of fstypes[], since that is essentially a "virtual" (non-existent)
80 bool null_dev_desc_ok;
81 int (*probe)(block_dev_desc_t *fs_dev_desc,
82 disk_partition_t *fs_partition);
83 int (*ls)(const char *dirname);
84 int (*exists)(const char *filename);
85 int (*size)(const char *filename);
86 int (*read)(const char *filename, void *buf, int offset, int len);
87 int (*write)(const char *filename, void *buf, int offset, int len);
91 static struct fstype_info fstypes[] = {
94 .fstype = FS_TYPE_FAT,
95 .null_dev_desc_ok = false,
96 .probe = fat_set_blk_dev,
101 .read = fat_read_file,
102 .write = fs_write_unsupported,
105 #ifdef CONFIG_FS_EXT4
107 .fstype = FS_TYPE_EXT,
108 .null_dev_desc_ok = false,
109 .probe = ext4fs_probe,
110 .close = ext4fs_close,
112 .exists = ext4fs_exists,
114 .read = ext4_read_file,
115 .write = fs_write_unsupported,
118 #ifdef CONFIG_SANDBOX
120 .fstype = FS_TYPE_SANDBOX,
121 .null_dev_desc_ok = true,
122 .probe = sandbox_fs_set_blk_dev,
123 .close = sandbox_fs_close,
125 .exists = sandbox_fs_exists,
126 .size = sandbox_fs_size,
127 .read = fs_read_sandbox,
128 .write = fs_write_sandbox,
132 .fstype = FS_TYPE_ANY,
133 .null_dev_desc_ok = true,
134 .probe = fs_probe_unsupported,
135 .close = fs_close_unsupported,
136 .ls = fs_ls_unsupported,
137 .exists = fs_exists_unsupported,
138 .size = fs_size_unsupported,
139 .read = fs_read_unsupported,
140 .write = fs_write_unsupported,
144 static struct fstype_info *fs_get_info(int fstype)
146 struct fstype_info *info;
149 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
150 if (fstype == info->fstype)
154 /* Return the 'unsupported' sentinel */
158 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
160 struct fstype_info *info;
162 #ifdef CONFIG_NEEDS_MANUAL_RELOC
163 static int relocated;
166 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
168 info->probe += gd->reloc_off;
169 info->close += gd->reloc_off;
170 info->ls += gd->reloc_off;
171 info->read += gd->reloc_off;
172 info->write += gd->reloc_off;
178 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
183 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
184 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
185 fstype != info->fstype)
188 if (!fs_dev_desc && !info->null_dev_desc_ok)
191 if (!info->probe(fs_dev_desc, &fs_partition)) {
192 fs_type = info->fstype;
200 static void fs_close(void)
202 struct fstype_info *info = fs_get_info(fs_type);
206 fs_type = FS_TYPE_ANY;
209 int fs_ls(const char *dirname)
213 struct fstype_info *info = fs_get_info(fs_type);
215 ret = info->ls(dirname);
217 fs_type = FS_TYPE_ANY;
223 int fs_exists(const char *filename)
227 struct fstype_info *info = fs_get_info(fs_type);
229 ret = info->exists(filename);
236 int fs_size(const char *filename)
240 struct fstype_info *info = fs_get_info(fs_type);
242 ret = info->size(filename);
249 int fs_read(const char *filename, ulong addr, int offset, int len)
251 struct fstype_info *info = fs_get_info(fs_type);
256 * We don't actually know how many bytes are being read, since len==0
257 * means read the whole file.
259 buf = map_sysmem(addr, len);
260 ret = info->read(filename, buf, offset, len);
263 /* If we requested a specific number of bytes, check we got it */
264 if (ret >= 0 && len && ret != len) {
265 printf("** Unable to read file %s **\n", filename);
273 int fs_write(const char *filename, ulong addr, int offset, int len)
275 struct fstype_info *info = fs_get_info(fs_type);
279 buf = map_sysmem(addr, len);
280 ret = info->write(filename, buf, offset, len);
283 if (ret >= 0 && ret != len) {
284 printf("** Unable to write file %s **\n", filename);
292 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
298 return CMD_RET_USAGE;
300 if (fs_set_blk_dev(argv[1], argv[2], fstype))
303 size = fs_size(argv[3]);
305 return CMD_RET_FAILURE;
307 setenv_hex("filesize", size);
312 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
316 const char *addr_str;
317 const char *filename;
325 return CMD_RET_USAGE;
327 return CMD_RET_USAGE;
329 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
333 addr = simple_strtoul(argv[3], &ep, 16);
334 if (ep == argv[3] || *ep != '\0')
335 return CMD_RET_USAGE;
337 addr_str = getenv("loadaddr");
338 if (addr_str != NULL)
339 addr = simple_strtoul(addr_str, NULL, 16);
341 addr = CONFIG_SYS_LOAD_ADDR;
346 filename = getenv("bootfile");
348 puts("** No boot file defined **\n");
353 bytes = simple_strtoul(argv[5], NULL, 16);
357 pos = simple_strtoul(argv[6], NULL, 16);
362 len_read = fs_read(filename, addr, pos, bytes);
363 time = get_timer(time);
367 printf("%d bytes read in %lu ms", len_read, time);
370 print_size(len_read / time * 1000, "/s");
375 setenv_hex("filesize", len_read);
380 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
384 return CMD_RET_USAGE;
386 return CMD_RET_USAGE;
388 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
391 if (fs_ls(argc >= 4 ? argv[3] : "/"))
397 int file_exists(const char *dev_type, const char *dev_part, const char *file,
400 if (fs_set_blk_dev(dev_type, dev_part, fstype))
403 return fs_exists(file);
406 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
410 const char *filename;
416 if (argc < 6 || argc > 7)
417 return CMD_RET_USAGE;
419 if (fs_set_blk_dev(argv[1], argv[2], fstype))
423 addr = simple_strtoul(argv[4], NULL, 16);
424 bytes = simple_strtoul(argv[5], NULL, 16);
426 pos = simple_strtoul(argv[6], NULL, 16);
431 len = fs_write(filename, addr, pos, bytes);
432 time = get_timer(time);
436 printf("%d bytes written in %lu ms", len, time);
439 print_size(len / time * 1000, "/s");