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>
26 #include <ubifs_uboot.h>
29 #include <linux/math64.h>
31 DECLARE_GLOBAL_DATA_PTR
;
33 static block_dev_desc_t
*fs_dev_desc
;
34 static disk_partition_t fs_partition
;
35 static int fs_type
= FS_TYPE_ANY
;
37 static inline int fs_probe_unsupported(block_dev_desc_t
*fs_dev_desc
,
38 disk_partition_t
*fs_partition
)
40 printf("** Unrecognized filesystem type **\n");
44 static inline int fs_ls_unsupported(const char *dirname
)
49 static inline int fs_exists_unsupported(const char *filename
)
54 static inline int fs_size_unsupported(const char *filename
, loff_t
*size
)
59 static inline int fs_read_unsupported(const char *filename
, void *buf
,
60 loff_t offset
, loff_t len
,
66 static inline int fs_write_unsupported(const char *filename
, void *buf
,
67 loff_t offset
, loff_t len
,
73 static inline void fs_close_unsupported(void)
77 static inline int fs_uuid_unsupported(char *uuid_str
)
86 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
87 * should be false in most cases. For "virtual" filesystems which
88 * aren't based on a U-Boot block device (e.g. sandbox), this can be
89 * set to true. This should also be true for the dumm entry at the end
90 * of fstypes[], since that is essentially a "virtual" (non-existent)
93 bool null_dev_desc_ok
;
94 int (*probe
)(block_dev_desc_t
*fs_dev_desc
,
95 disk_partition_t
*fs_partition
);
96 int (*ls
)(const char *dirname
);
97 int (*exists
)(const char *filename
);
98 int (*size
)(const char *filename
, loff_t
*size
);
99 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
100 loff_t len
, loff_t
*actread
);
101 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
102 loff_t len
, loff_t
*actwrite
);
104 int (*uuid
)(char *uuid_str
);
107 static struct fstype_info fstypes
[] = {
110 .fstype
= FS_TYPE_FAT
,
112 .null_dev_desc_ok
= false,
113 .probe
= fat_set_blk_dev
,
116 .exists
= fat_exists
,
118 .read
= fat_read_file
,
119 #ifdef CONFIG_FAT_WRITE
120 .write
= file_fat_write
,
122 .write
= fs_write_unsupported
,
124 .uuid
= fs_uuid_unsupported
,
127 #ifdef CONFIG_FS_EXT4
129 .fstype
= FS_TYPE_EXT
,
131 .null_dev_desc_ok
= false,
132 .probe
= ext4fs_probe
,
133 .close
= ext4fs_close
,
135 .exists
= ext4fs_exists
,
137 .read
= ext4_read_file
,
138 #ifdef CONFIG_CMD_EXT4_WRITE
139 .write
= ext4_write_file
,
141 .write
= fs_write_unsupported
,
146 #ifdef CONFIG_SANDBOX
148 .fstype
= FS_TYPE_SANDBOX
,
150 .null_dev_desc_ok
= true,
151 .probe
= sandbox_fs_set_blk_dev
,
152 .close
= sandbox_fs_close
,
154 .exists
= sandbox_fs_exists
,
155 .size
= sandbox_fs_size
,
156 .read
= fs_read_sandbox
,
157 .write
= fs_write_sandbox
,
158 .uuid
= fs_uuid_unsupported
,
161 #ifdef CONFIG_CMD_UBIFS
163 .fstype
= FS_TYPE_UBIFS
,
165 .null_dev_desc_ok
= true,
166 .probe
= ubifs_set_blk_dev
,
167 .close
= ubifs_close
,
169 .exists
= ubifs_exists
,
172 .write
= fs_write_unsupported
,
173 .uuid
= fs_uuid_unsupported
,
177 .fstype
= FS_TYPE_ANY
,
178 .name
= "unsupported",
179 .null_dev_desc_ok
= true,
180 .probe
= fs_probe_unsupported
,
181 .close
= fs_close_unsupported
,
182 .ls
= fs_ls_unsupported
,
183 .exists
= fs_exists_unsupported
,
184 .size
= fs_size_unsupported
,
185 .read
= fs_read_unsupported
,
186 .write
= fs_write_unsupported
,
187 .uuid
= fs_uuid_unsupported
,
191 static struct fstype_info
*fs_get_info(int fstype
)
193 struct fstype_info
*info
;
196 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
197 if (fstype
== info
->fstype
)
201 /* Return the 'unsupported' sentinel */
205 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
207 struct fstype_info
*info
;
209 #ifdef CONFIG_NEEDS_MANUAL_RELOC
210 static int relocated
;
213 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
215 info
->name
+= gd
->reloc_off
;
216 info
->probe
+= gd
->reloc_off
;
217 info
->close
+= gd
->reloc_off
;
218 info
->ls
+= gd
->reloc_off
;
219 info
->read
+= gd
->reloc_off
;
220 info
->write
+= gd
->reloc_off
;
226 part
= get_device_and_partition(ifname
, dev_part_str
, &fs_dev_desc
,
231 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
232 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
233 fstype
!= info
->fstype
)
236 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
239 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
240 fs_type
= info
->fstype
;
248 static void fs_close(void)
250 struct fstype_info
*info
= fs_get_info(fs_type
);
254 fs_type
= FS_TYPE_ANY
;
257 int fs_uuid(char *uuid_str
)
259 struct fstype_info
*info
= fs_get_info(fs_type
);
261 return info
->uuid(uuid_str
);
264 int fs_ls(const char *dirname
)
268 struct fstype_info
*info
= fs_get_info(fs_type
);
270 ret
= info
->ls(dirname
);
272 fs_type
= FS_TYPE_ANY
;
278 int fs_exists(const char *filename
)
282 struct fstype_info
*info
= fs_get_info(fs_type
);
284 ret
= info
->exists(filename
);
291 int fs_size(const char *filename
, loff_t
*size
)
295 struct fstype_info
*info
= fs_get_info(fs_type
);
297 ret
= info
->size(filename
, size
);
304 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
307 struct fstype_info
*info
= fs_get_info(fs_type
);
312 * We don't actually know how many bytes are being read, since len==0
313 * means read the whole file.
315 buf
= map_sysmem(addr
, len
);
316 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
319 /* If we requested a specific number of bytes, check we got it */
320 if (ret
== 0 && len
&& *actread
!= len
)
321 printf("** %s shorter than offset + len **\n", filename
);
327 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
330 struct fstype_info
*info
= fs_get_info(fs_type
);
334 buf
= map_sysmem(addr
, len
);
335 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
338 if (ret
< 0 && len
!= *actwrite
) {
339 printf("** Unable to write file %s **\n", filename
);
347 int do_size(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
353 return CMD_RET_USAGE
;
355 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
358 if (fs_size(argv
[3], &size
) < 0)
359 return CMD_RET_FAILURE
;
361 setenv_hex("filesize", size
);
366 int do_load(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
370 const char *addr_str
;
371 const char *filename
;
380 return CMD_RET_USAGE
;
382 return CMD_RET_USAGE
;
384 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
388 addr
= simple_strtoul(argv
[3], &ep
, 16);
389 if (ep
== argv
[3] || *ep
!= '\0')
390 return CMD_RET_USAGE
;
392 addr_str
= getenv("loadaddr");
393 if (addr_str
!= NULL
)
394 addr
= simple_strtoul(addr_str
, NULL
, 16);
396 addr
= CONFIG_SYS_LOAD_ADDR
;
401 filename
= getenv("bootfile");
403 puts("** No boot file defined **\n");
408 bytes
= simple_strtoul(argv
[5], NULL
, 16);
412 pos
= simple_strtoul(argv
[6], NULL
, 16);
417 ret
= fs_read(filename
, addr
, pos
, bytes
, &len_read
);
418 time
= get_timer(time
);
422 printf("%llu bytes read in %lu ms", len_read
, time
);
425 print_size(div_u64(len_read
, time
) * 1000, "/s");
430 setenv_hex("filesize", len_read
);
435 int do_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
439 return CMD_RET_USAGE
;
441 return CMD_RET_USAGE
;
443 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
446 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
452 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
455 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
458 return fs_exists(file
);
461 int do_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
465 const char *filename
;
472 if (argc
< 6 || argc
> 7)
473 return CMD_RET_USAGE
;
475 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
478 addr
= simple_strtoul(argv
[3], NULL
, 16);
480 bytes
= simple_strtoul(argv
[5], NULL
, 16);
482 pos
= simple_strtoul(argv
[6], NULL
, 16);
487 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
488 time
= get_timer(time
);
492 printf("%llu bytes written in %lu ms", len
, time
);
495 print_size(div_u64(len
, time
) * 1000, "/s");
503 int do_fs_uuid(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
508 memset(uuid
, 0, sizeof(uuid
));
510 if (argc
< 3 || argc
> 4)
511 return CMD_RET_USAGE
;
513 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
518 return CMD_RET_FAILURE
;
521 setenv(argv
[3], uuid
);
523 printf("%s\n", uuid
);
525 return CMD_RET_SUCCESS
;
528 int do_fs_type(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
530 struct fstype_info
*info
;
532 if (argc
< 3 || argc
> 4)
533 return CMD_RET_USAGE
;
535 if (fs_set_blk_dev(argv
[1], argv
[2], FS_TYPE_ANY
))
538 info
= fs_get_info(fs_type
);
541 setenv(argv
[3], info
->name
);
543 printf("%s\n", info
->name
);
545 return CMD_RET_SUCCESS
;