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 #include <sandboxfs.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 static block_dev_desc_t *fs_dev_desc;
30 static disk_partition_t fs_partition;
31 static int fs_type = FS_TYPE_ANY;
33 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
34 disk_partition_t *fs_partition)
36 printf("** Unrecognized filesystem type **\n");
40 static inline int fs_ls_unsupported(const char *dirname)
45 static inline int fs_exists_unsupported(const char *filename)
50 static inline int fs_size_unsupported(const char *filename)
55 static inline int fs_read_unsupported(const char *filename, void *buf,
61 static inline int fs_write_unsupported(const char *filename, void *buf,
67 static inline void fs_close_unsupported(void)
71 static inline int fs_uuid_unsupported(char *uuid_str)
79 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
80 * should be false in most cases. For "virtual" filesystems which
81 * aren't based on a U-Boot block device (e.g. sandbox), this can be
82 * set to true. This should also be true for the dumm entry at the end
83 * of fstypes[], since that is essentially a "virtual" (non-existent)
86 bool null_dev_desc_ok;
87 int (*probe)(block_dev_desc_t *fs_dev_desc,
88 disk_partition_t *fs_partition);
89 int (*ls)(const char *dirname);
90 int (*exists)(const char *filename);
91 int (*size)(const char *filename);
92 int (*read)(const char *filename, void *buf, int offset, int len);
93 int (*write)(const char *filename, void *buf, int offset, int len);
95 int (*uuid)(char *uuid_str);
98 static struct fstype_info fstypes[] = {
101 .fstype = FS_TYPE_FAT,
102 .null_dev_desc_ok = false,
103 .probe = fat_set_blk_dev,
106 .exists = fat_exists,
108 .read = fat_read_file,
109 .write = fs_write_unsupported,
110 .uuid = fs_uuid_unsupported,
113 #ifdef CONFIG_FS_EXT4
115 .fstype = FS_TYPE_EXT,
116 .null_dev_desc_ok = false,
117 .probe = ext4fs_probe,
118 .close = ext4fs_close,
120 .exists = ext4fs_exists,
122 .read = ext4_read_file,
123 .write = fs_write_unsupported,
127 #ifdef CONFIG_SANDBOX
129 .fstype = FS_TYPE_SANDBOX,
130 .null_dev_desc_ok = true,
131 .probe = sandbox_fs_set_blk_dev,
132 .close = sandbox_fs_close,
134 .exists = sandbox_fs_exists,
135 .size = sandbox_fs_size,
136 .read = fs_read_sandbox,
137 .write = fs_write_sandbox,
138 .uuid = fs_uuid_unsupported,
142 .fstype = FS_TYPE_ANY,
143 .null_dev_desc_ok = true,
144 .probe = fs_probe_unsupported,
145 .close = fs_close_unsupported,
146 .ls = fs_ls_unsupported,
147 .exists = fs_exists_unsupported,
148 .size = fs_size_unsupported,
149 .read = fs_read_unsupported,
150 .write = fs_write_unsupported,
151 .uuid = fs_uuid_unsupported,
155 static struct fstype_info *fs_get_info(int fstype)
157 struct fstype_info *info;
160 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
161 if (fstype == info->fstype)
165 /* Return the 'unsupported' sentinel */
169 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
171 struct fstype_info *info;
173 #ifdef CONFIG_NEEDS_MANUAL_RELOC
174 static int relocated;
177 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
179 info->probe += gd->reloc_off;
180 info->close += gd->reloc_off;
181 info->ls += gd->reloc_off;
182 info->read += gd->reloc_off;
183 info->write += gd->reloc_off;
189 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
194 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
195 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
196 fstype != info->fstype)
199 if (!fs_dev_desc && !info->null_dev_desc_ok)
202 if (!info->probe(fs_dev_desc, &fs_partition)) {
203 fs_type = info->fstype;
211 static void fs_close(void)
213 struct fstype_info *info = fs_get_info(fs_type);
217 fs_type = FS_TYPE_ANY;
220 int fs_uuid(char *uuid_str)
222 struct fstype_info *info = fs_get_info(fs_type);
224 return info->uuid(uuid_str);
227 int fs_ls(const char *dirname)
231 struct fstype_info *info = fs_get_info(fs_type);
233 ret = info->ls(dirname);
235 fs_type = FS_TYPE_ANY;
241 int fs_exists(const char *filename)
245 struct fstype_info *info = fs_get_info(fs_type);
247 ret = info->exists(filename);
254 int fs_size(const char *filename)
258 struct fstype_info *info = fs_get_info(fs_type);
260 ret = info->size(filename);
267 int fs_read(const char *filename, ulong addr, int offset, int len)
269 struct fstype_info *info = fs_get_info(fs_type);
274 * We don't actually know how many bytes are being read, since len==0
275 * means read the whole file.
277 buf = map_sysmem(addr, len);
278 ret = info->read(filename, buf, offset, len);
281 /* If we requested a specific number of bytes, check we got it */
282 if (ret >= 0 && len && ret != len) {
283 printf("** Unable to read file %s **\n", filename);
291 int fs_write(const char *filename, ulong addr, int offset, int len)
293 struct fstype_info *info = fs_get_info(fs_type);
297 buf = map_sysmem(addr, len);
298 ret = info->write(filename, buf, offset, len);
301 if (ret >= 0 && ret != len) {
302 printf("** Unable to write file %s **\n", filename);
310 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
316 return CMD_RET_USAGE;
318 if (fs_set_blk_dev(argv[1], argv[2], fstype))
321 size = fs_size(argv[3]);
323 return CMD_RET_FAILURE;
325 setenv_hex("filesize", size);
330 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
334 const char *addr_str;
335 const char *filename;
343 return CMD_RET_USAGE;
345 return CMD_RET_USAGE;
347 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
351 addr = simple_strtoul(argv[3], &ep, 16);
352 if (ep == argv[3] || *ep != '\0')
353 return CMD_RET_USAGE;
355 addr_str = getenv("loadaddr");
356 if (addr_str != NULL)
357 addr = simple_strtoul(addr_str, NULL, 16);
359 addr = CONFIG_SYS_LOAD_ADDR;
364 filename = getenv("bootfile");
366 puts("** No boot file defined **\n");
371 bytes = simple_strtoul(argv[5], NULL, 16);
375 pos = simple_strtoul(argv[6], NULL, 16);
380 len_read = fs_read(filename, addr, pos, bytes);
381 time = get_timer(time);
385 printf("%d bytes read in %lu ms", len_read, time);
388 print_size(len_read / time * 1000, "/s");
393 setenv_hex("filesize", len_read);
398 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
402 return CMD_RET_USAGE;
404 return CMD_RET_USAGE;
406 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
409 if (fs_ls(argc >= 4 ? argv[3] : "/"))
415 int file_exists(const char *dev_type, const char *dev_part, const char *file,
418 if (fs_set_blk_dev(dev_type, dev_part, fstype))
421 return fs_exists(file);
424 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
428 const char *filename;
434 if (argc < 6 || argc > 7)
435 return CMD_RET_USAGE;
437 if (fs_set_blk_dev(argv[1], argv[2], fstype))
441 addr = simple_strtoul(argv[4], NULL, 16);
442 bytes = simple_strtoul(argv[5], NULL, 16);
444 pos = simple_strtoul(argv[6], NULL, 16);
449 len = fs_write(filename, addr, pos, bytes);
450 time = get_timer(time);
454 printf("%d bytes written in %lu ms", len, time);
457 print_size(len / time * 1000, "/s");
465 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
470 memset(uuid, 0, sizeof(uuid));
472 if (argc < 3 || argc > 4)
473 return CMD_RET_USAGE;
475 if (fs_set_blk_dev(argv[1], argv[2], fstype))
480 return CMD_RET_FAILURE;
483 setenv(argv[3], uuid);
485 printf("%s\n", uuid);
487 return CMD_RET_SUCCESS;