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/>.
25 #include <sandboxfs.h>
28 #include <linux/math64.h>
30 DECLARE_GLOBAL_DATA_PTR;
32 static block_dev_desc_t *fs_dev_desc;
33 static disk_partition_t fs_partition;
34 static int fs_type = FS_TYPE_ANY;
36 static inline int fs_probe_unsupported(block_dev_desc_t *fs_dev_desc,
37 disk_partition_t *fs_partition)
39 printf("** Unrecognized filesystem type **\n");
43 static inline int fs_ls_unsupported(const char *dirname)
48 static inline int fs_exists_unsupported(const char *filename)
53 static inline int fs_size_unsupported(const char *filename, loff_t *size)
58 static inline int fs_read_unsupported(const char *filename, void *buf,
59 loff_t offset, loff_t len,
65 static inline int fs_write_unsupported(const char *filename, void *buf,
66 loff_t offset, loff_t len,
72 static inline void fs_close_unsupported(void)
76 static inline int fs_uuid_unsupported(char *uuid_str)
85 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
86 * should be false in most cases. For "virtual" filesystems which
87 * aren't based on a U-Boot block device (e.g. sandbox), this can be
88 * set to true. This should also be true for the dumm entry at the end
89 * of fstypes[], since that is essentially a "virtual" (non-existent)
92 bool null_dev_desc_ok;
93 int (*probe)(block_dev_desc_t *fs_dev_desc,
94 disk_partition_t *fs_partition);
95 int (*ls)(const char *dirname);
96 int (*exists)(const char *filename);
97 int (*size)(const char *filename, loff_t *size);
98 int (*read)(const char *filename, void *buf, loff_t offset,
99 loff_t len, loff_t *actread);
100 int (*write)(const char *filename, void *buf, loff_t offset,
101 loff_t len, loff_t *actwrite);
103 int (*uuid)(char *uuid_str);
106 static struct fstype_info fstypes[] = {
109 .fstype = FS_TYPE_FAT,
111 .null_dev_desc_ok = false,
112 .probe = fat_set_blk_dev,
115 .exists = fat_exists,
117 .read = fat_read_file,
118 #ifdef CONFIG_FAT_WRITE
119 .write = file_fat_write,
121 .write = fs_write_unsupported,
123 .uuid = fs_uuid_unsupported,
126 #ifdef CONFIG_FS_EXT4
128 .fstype = FS_TYPE_EXT,
130 .null_dev_desc_ok = false,
131 .probe = ext4fs_probe,
132 .close = ext4fs_close,
134 .exists = ext4fs_exists,
136 .read = ext4_read_file,
137 #ifdef CONFIG_CMD_EXT4_WRITE
138 .write = ext4_write_file,
140 .write = fs_write_unsupported,
145 #ifdef CONFIG_SANDBOX
147 .fstype = FS_TYPE_SANDBOX,
149 .null_dev_desc_ok = true,
150 .probe = sandbox_fs_set_blk_dev,
151 .close = sandbox_fs_close,
153 .exists = sandbox_fs_exists,
154 .size = sandbox_fs_size,
155 .read = fs_read_sandbox,
156 .write = fs_write_sandbox,
157 .uuid = fs_uuid_unsupported,
161 .fstype = FS_TYPE_ANY,
162 .name = "unsupported",
163 .null_dev_desc_ok = true,
164 .probe = fs_probe_unsupported,
165 .close = fs_close_unsupported,
166 .ls = fs_ls_unsupported,
167 .exists = fs_exists_unsupported,
168 .size = fs_size_unsupported,
169 .read = fs_read_unsupported,
170 .write = fs_write_unsupported,
171 .uuid = fs_uuid_unsupported,
175 static struct fstype_info *fs_get_info(int fstype)
177 struct fstype_info *info;
180 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
181 if (fstype == info->fstype)
185 /* Return the 'unsupported' sentinel */
189 int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
191 struct fstype_info *info;
193 #ifdef CONFIG_NEEDS_MANUAL_RELOC
194 static int relocated;
197 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
199 info->name += gd->reloc_off;
200 info->probe += gd->reloc_off;
201 info->close += gd->reloc_off;
202 info->ls += gd->reloc_off;
203 info->read += gd->reloc_off;
204 info->write += gd->reloc_off;
210 part = get_device_and_partition(ifname, dev_part_str, &fs_dev_desc,
215 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
216 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
217 fstype != info->fstype)
220 if (!fs_dev_desc && !info->null_dev_desc_ok)
223 if (!info->probe(fs_dev_desc, &fs_partition)) {
224 fs_type = info->fstype;
232 static void fs_close(void)
234 struct fstype_info *info = fs_get_info(fs_type);
238 fs_type = FS_TYPE_ANY;
241 int fs_uuid(char *uuid_str)
243 struct fstype_info *info = fs_get_info(fs_type);
245 return info->uuid(uuid_str);
248 int fs_ls(const char *dirname)
252 struct fstype_info *info = fs_get_info(fs_type);
254 ret = info->ls(dirname);
256 fs_type = FS_TYPE_ANY;
262 int fs_exists(const char *filename)
266 struct fstype_info *info = fs_get_info(fs_type);
268 ret = info->exists(filename);
275 int fs_size(const char *filename, loff_t *size)
279 struct fstype_info *info = fs_get_info(fs_type);
281 ret = info->size(filename, size);
288 int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
291 struct fstype_info *info = fs_get_info(fs_type);
296 * We don't actually know how many bytes are being read, since len==0
297 * means read the whole file.
299 buf = map_sysmem(addr, len);
300 ret = info->read(filename, buf, offset, len, actread);
303 /* If we requested a specific number of bytes, check we got it */
304 if (ret == 0 && len && *actread != len) {
305 printf("** Unable to read file %s **\n", filename);
313 int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
316 struct fstype_info *info = fs_get_info(fs_type);
320 buf = map_sysmem(addr, len);
321 ret = info->write(filename, buf, offset, len, actwrite);
324 if (ret < 0 && len != *actwrite) {
325 printf("** Unable to write file %s **\n", filename);
333 int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
339 return CMD_RET_USAGE;
341 if (fs_set_blk_dev(argv[1], argv[2], fstype))
344 if (fs_size(argv[3], &size) < 0)
345 return CMD_RET_FAILURE;
347 setenv_hex("filesize", size);
352 int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
356 const char *addr_str;
357 const char *filename;
366 return CMD_RET_USAGE;
368 return CMD_RET_USAGE;
370 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
374 addr = simple_strtoul(argv[3], &ep, 16);
375 if (ep == argv[3] || *ep != '\0')
376 return CMD_RET_USAGE;
378 addr_str = getenv("loadaddr");
379 if (addr_str != NULL)
380 addr = simple_strtoul(addr_str, NULL, 16);
382 addr = CONFIG_SYS_LOAD_ADDR;
387 filename = getenv("bootfile");
389 puts("** No boot file defined **\n");
394 bytes = simple_strtoul(argv[5], NULL, 16);
398 pos = simple_strtoul(argv[6], NULL, 16);
403 ret = fs_read(filename, addr, pos, bytes, &len_read);
404 time = get_timer(time);
408 printf("%llu bytes read in %lu ms", len_read, time);
411 print_size(div_u64(len_read, time) * 1000, "/s");
416 setenv_hex("filesize", len_read);
421 int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
425 return CMD_RET_USAGE;
427 return CMD_RET_USAGE;
429 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
432 if (fs_ls(argc >= 4 ? argv[3] : "/"))
438 int file_exists(const char *dev_type, const char *dev_part, const char *file,
441 if (fs_set_blk_dev(dev_type, dev_part, fstype))
444 return fs_exists(file);
447 int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
451 const char *filename;
458 if (argc < 6 || argc > 7)
459 return CMD_RET_USAGE;
461 if (fs_set_blk_dev(argv[1], argv[2], fstype))
464 addr = simple_strtoul(argv[3], NULL, 16);
466 bytes = simple_strtoul(argv[5], NULL, 16);
468 pos = simple_strtoul(argv[6], NULL, 16);
473 ret = fs_write(filename, addr, pos, bytes, &len);
474 time = get_timer(time);
478 printf("%llu bytes written in %lu ms", len, time);
481 print_size(div_u64(len, time) * 1000, "/s");
489 int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
494 memset(uuid, 0, sizeof(uuid));
496 if (argc < 3 || argc > 4)
497 return CMD_RET_USAGE;
499 if (fs_set_blk_dev(argv[1], argv[2], fstype))
504 return CMD_RET_FAILURE;
507 setenv(argv[3], uuid);
509 printf("%s\n", uuid);
511 return CMD_RET_SUCCESS;
514 int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
516 struct fstype_info *info;
518 if (argc < 3 || argc > 4)
519 return CMD_RET_USAGE;
521 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
524 info = fs_get_info(fs_type);
527 setenv(argv[3], info->name);
529 printf("%s\n", info->name);
531 return CMD_RET_SUCCESS;