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 #include <linux/math64.h>
29 DECLARE_GLOBAL_DATA_PTR;
31 static block_dev_desc_t *fs_dev_desc;
32 static disk_partition_t fs_partition;
33 static int fs_type = FS_TYPE_ANY;
35 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
36 disk_partition_t *fs_partition)
38 printf("** Unrecognized filesystem type **\n");
42 static inline int fs_ls_unsupported(const char *dirname)
47 static inline int fs_exists_unsupported(const char *filename)
52 static inline int fs_size_unsupported(const char *filename, loff_t *size)
57 static inline int fs_read_unsupported(const char *filename, void *buf,
58 loff_t offset, loff_t len,
64 static inline int fs_write_unsupported(const char *filename, void *buf,
65 loff_t offset, loff_t len,
71 static inline void fs_close_unsupported(void)
75 static inline int fs_uuid_unsupported(char *uuid_str)
84 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
85 * should be false in most cases. For "virtual" filesystems which
86 * aren't based on a U-Boot block device (e.g. sandbox), this can be
87 * set to true. This should also be true for the dumm entry at the end
88 * of fstypes[], since that is essentially a "virtual" (non-existent)
91 bool null_dev_desc_ok;
92 int (*probe)(block_dev_desc_t *fs_dev_desc,
93 disk_partition_t *fs_partition);
94 int (*ls)(const char *dirname);
95 int (*exists)(const char *filename);
96 int (*size)(const char *filename, loff_t *size);
97 int (*read)(const char *filename, void *buf, loff_t offset,
98 loff_t len, loff_t *actread);
99 int (*write)(const char *filename, void *buf, loff_t offset,
100 loff_t len, loff_t *actwrite);
102 int (*uuid)(char *uuid_str);
105 static struct fstype_info fstypes[] = {
108 .fstype = FS_TYPE_FAT,
110 .null_dev_desc_ok = false,
111 .probe = fat_set_blk_dev,
114 .exists = fat_exists,
116 .read = fat_read_file,
117 #ifdef CONFIG_FAT_WRITE
118 .write = file_fat_write,
120 .write = fs_write_unsupported,
122 .uuid = fs_uuid_unsupported,
125 #ifdef CONFIG_FS_EXT4
127 .fstype = FS_TYPE_EXT,
129 .null_dev_desc_ok = false,
130 .probe = ext4fs_probe,
131 .close = ext4fs_close,
133 .exists = ext4fs_exists,
135 .read = ext4_read_file,
136 #ifdef CONFIG_CMD_EXT4_WRITE
137 .write = ext4_write_file,
139 .write = fs_write_unsupported,
144 #ifdef CONFIG_SANDBOX
146 .fstype = FS_TYPE_SANDBOX,
148 .null_dev_desc_ok = true,
149 .probe = sandbox_fs_set_blk_dev,
150 .close = sandbox_fs_close,
152 .exists = sandbox_fs_exists,
153 .size = sandbox_fs_size,
154 .read = fs_read_sandbox,
155 .write = fs_write_sandbox,
156 .uuid = fs_uuid_unsupported,
160 .fstype = FS_TYPE_ANY,
161 .name = "unsupported",
162 .null_dev_desc_ok = true,
163 .probe = fs_probe_unsupported,
164 .close = fs_close_unsupported,
165 .ls = fs_ls_unsupported,
166 .exists = fs_exists_unsupported,
167 .size = fs_size_unsupported,
168 .read = fs_read_unsupported,
169 .write = fs_write_unsupported,
170 .uuid = fs_uuid_unsupported,
174 static struct fstype_info *fs_get_info(int fstype)
176 struct fstype_info *info;
179 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
180 if (fstype == info->fstype)
184 /* Return the 'unsupported' sentinel */
188 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
190 struct fstype_info *info;
192 #ifdef CONFIG_NEEDS_MANUAL_RELOC
193 static int relocated;
196 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
198 info->name += gd->reloc_off;
199 info->probe += gd->reloc_off;
200 info->close += gd->reloc_off;
201 info->ls += gd->reloc_off;
202 info->read += gd->reloc_off;
203 info->write += gd->reloc_off;
209 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
214 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
215 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
216 fstype != info->fstype)
219 if (!fs_dev_desc && !info->null_dev_desc_ok)
222 if (!info->probe(fs_dev_desc, &fs_partition)) {
223 fs_type = info->fstype;
231 static void fs_close(void)
233 struct fstype_info *info = fs_get_info(fs_type);
237 fs_type = FS_TYPE_ANY;
240 int fs_uuid(char *uuid_str)
242 struct fstype_info *info = fs_get_info(fs_type);
244 return info->uuid(uuid_str);
247 int fs_ls(const char *dirname)
251 struct fstype_info *info = fs_get_info(fs_type);
253 ret = info->ls(dirname);
255 fs_type = FS_TYPE_ANY;
261 int fs_exists(const char *filename)
265 struct fstype_info *info = fs_get_info(fs_type);
267 ret = info->exists(filename);
274 int fs_size(const char *filename, loff_t *size)
278 struct fstype_info *info = fs_get_info(fs_type);
280 ret = info->size(filename, size);
287 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
290 struct fstype_info *info = fs_get_info(fs_type);
295 * We don't actually know how many bytes are being read, since len==0
296 * means read the whole file.
298 buf = map_sysmem(addr, len);
299 ret = info->read(filename, buf, offset, len, actread);
302 /* If we requested a specific number of bytes, check we got it */
303 if (ret == 0 && len && *actread != len) {
304 printf("** Unable to read file %s **\n", filename);
312 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
315 struct fstype_info *info = fs_get_info(fs_type);
319 buf = map_sysmem(addr, len);
320 ret = info->write(filename, buf, offset, len, actwrite);
323 if (ret < 0 && len != *actwrite) {
324 printf("** Unable to write file %s **\n", filename);
332 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
338 return CMD_RET_USAGE;
340 if (fs_set_blk_dev(argv[1], argv[2], fstype))
343 if (fs_size(argv[3], &size) < 0)
344 return CMD_RET_FAILURE;
346 setenv_hex("filesize", size);
351 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
355 const char *addr_str;
356 const char *filename;
365 return CMD_RET_USAGE;
367 return CMD_RET_USAGE;
369 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
373 addr = simple_strtoul(argv[3], &ep, 16);
374 if (ep == argv[3] || *ep != '\0')
375 return CMD_RET_USAGE;
377 addr_str = getenv("loadaddr");
378 if (addr_str != NULL)
379 addr = simple_strtoul(addr_str, NULL, 16);
381 addr = CONFIG_SYS_LOAD_ADDR;
386 filename = getenv("bootfile");
388 puts("** No boot file defined **\n");
393 bytes = simple_strtoul(argv[5], NULL, 16);
397 pos = simple_strtoul(argv[6], NULL, 16);
402 ret = fs_read(filename, addr, pos, bytes, &len_read);
403 time = get_timer(time);
407 printf("%llu bytes read in %lu ms", len_read, time);
410 print_size(div_u64(len_read, time) * 1000, "/s");
415 setenv_hex("filesize", len_read);
420 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
424 return CMD_RET_USAGE;
426 return CMD_RET_USAGE;
428 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
431 if (fs_ls(argc >= 4 ? argv[3] : "/"))
437 int file_exists(const char *dev_type, const char *dev_part, const char *file,
440 if (fs_set_blk_dev(dev_type, dev_part, fstype))
443 return fs_exists(file);
446 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
450 const char *filename;
457 if (argc < 6 || argc > 7)
458 return CMD_RET_USAGE;
460 if (fs_set_blk_dev(argv[1], argv[2], fstype))
463 addr = simple_strtoul(argv[3], NULL, 16);
465 bytes = simple_strtoul(argv[5], NULL, 16);
467 pos = simple_strtoul(argv[6], NULL, 16);
472 ret = fs_write(filename, addr, pos, bytes, &len);
473 time = get_timer(time);
477 printf("%llu bytes written in %lu ms", len, time);
480 print_size(div_u64(len, time) * 1000, "/s");
488 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
493 memset(uuid, 0, sizeof(uuid));
495 if (argc < 3 || argc > 4)
496 return CMD_RET_USAGE;
498 if (fs_set_blk_dev(argv[1], argv[2], fstype))
503 return CMD_RET_FAILURE;
506 setenv(argv[3], uuid);
508 printf("%s\n", uuid);
510 return CMD_RET_SUCCESS;
513 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
515 struct fstype_info *info;
517 if (argc < 3 || argc > 4)
518 return CMD_RET_USAGE;
520 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
523 info = fs_get_info(fs_type);
526 setenv(argv[3], info->name);
528 printf("%s\n", info->name);
530 return CMD_RET_SUCCESS;