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
, loff_t
*size
)
55 static inline int fs_read_unsupported(const char *filename
, void *buf
,
56 loff_t offset
, loff_t len
,
62 static inline int fs_write_unsupported(const char *filename
, void *buf
,
63 loff_t offset
, loff_t len
,
69 static inline void fs_close_unsupported(void)
73 static inline int fs_uuid_unsupported(char *uuid_str
)
81 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
82 * should be false in most cases. For "virtual" filesystems which
83 * aren't based on a U-Boot block device (e.g. sandbox), this can be
84 * set to true. This should also be true for the dumm entry at the end
85 * of fstypes[], since that is essentially a "virtual" (non-existent)
88 bool null_dev_desc_ok
;
89 int (*probe
)(block_dev_desc_t
*fs_dev_desc
,
90 disk_partition_t
*fs_partition
);
91 int (*ls
)(const char *dirname
);
92 int (*exists
)(const char *filename
);
93 int (*size
)(const char *filename
, loff_t
*size
);
94 int (*read
)(const char *filename
, void *buf
, loff_t offset
,
95 loff_t len
, loff_t
*actread
);
96 int (*write
)(const char *filename
, void *buf
, loff_t offset
,
97 loff_t len
, loff_t
*actwrite
);
99 int (*uuid
)(char *uuid_str
);
102 static struct fstype_info fstypes
[] = {
105 .fstype
= FS_TYPE_FAT
,
106 .null_dev_desc_ok
= false,
107 .probe
= fat_set_blk_dev
,
110 .exists
= fat_exists
,
112 .read
= fat_read_file
,
113 #ifdef CONFIG_FAT_WRITE
114 .write
= file_fat_write
,
116 .write
= fs_write_unsupported
,
118 .uuid
= fs_uuid_unsupported
,
121 #ifdef CONFIG_FS_EXT4
123 .fstype
= FS_TYPE_EXT
,
124 .null_dev_desc_ok
= false,
125 .probe
= ext4fs_probe
,
126 .close
= ext4fs_close
,
128 .exists
= ext4fs_exists
,
130 .read
= ext4_read_file
,
131 #ifdef CONFIG_CMD_EXT4_WRITE
132 .write
= ext4_write_file
,
134 .write
= fs_write_unsupported
,
139 #ifdef CONFIG_SANDBOX
141 .fstype
= FS_TYPE_SANDBOX
,
142 .null_dev_desc_ok
= true,
143 .probe
= sandbox_fs_set_blk_dev
,
144 .close
= sandbox_fs_close
,
146 .exists
= sandbox_fs_exists
,
147 .size
= sandbox_fs_size
,
148 .read
= fs_read_sandbox
,
149 .write
= fs_write_sandbox
,
150 .uuid
= fs_uuid_unsupported
,
154 .fstype
= FS_TYPE_ANY
,
155 .null_dev_desc_ok
= true,
156 .probe
= fs_probe_unsupported
,
157 .close
= fs_close_unsupported
,
158 .ls
= fs_ls_unsupported
,
159 .exists
= fs_exists_unsupported
,
160 .size
= fs_size_unsupported
,
161 .read
= fs_read_unsupported
,
162 .write
= fs_write_unsupported
,
163 .uuid
= fs_uuid_unsupported
,
167 static struct fstype_info
*fs_get_info(int fstype
)
169 struct fstype_info
*info
;
172 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
) - 1; i
++, info
++) {
173 if (fstype
== info
->fstype
)
177 /* Return the 'unsupported' sentinel */
181 int fs_set_blk_dev(const char *ifname
, const char *dev_part_str
, int fstype
)
183 struct fstype_info
*info
;
185 #ifdef CONFIG_NEEDS_MANUAL_RELOC
186 static int relocated
;
189 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
);
191 info
->probe
+= gd
->reloc_off
;
192 info
->close
+= gd
->reloc_off
;
193 info
->ls
+= gd
->reloc_off
;
194 info
->read
+= gd
->reloc_off
;
195 info
->write
+= gd
->reloc_off
;
201 part
= get_device_and_partition(ifname
, dev_part_str
, &fs_dev_desc
,
206 for (i
= 0, info
= fstypes
; i
< ARRAY_SIZE(fstypes
); i
++, info
++) {
207 if (fstype
!= FS_TYPE_ANY
&& info
->fstype
!= FS_TYPE_ANY
&&
208 fstype
!= info
->fstype
)
211 if (!fs_dev_desc
&& !info
->null_dev_desc_ok
)
214 if (!info
->probe(fs_dev_desc
, &fs_partition
)) {
215 fs_type
= info
->fstype
;
223 static void fs_close(void)
225 struct fstype_info
*info
= fs_get_info(fs_type
);
229 fs_type
= FS_TYPE_ANY
;
232 int fs_uuid(char *uuid_str
)
234 struct fstype_info
*info
= fs_get_info(fs_type
);
236 return info
->uuid(uuid_str
);
239 int fs_ls(const char *dirname
)
243 struct fstype_info
*info
= fs_get_info(fs_type
);
245 ret
= info
->ls(dirname
);
247 fs_type
= FS_TYPE_ANY
;
253 int fs_exists(const char *filename
)
257 struct fstype_info
*info
= fs_get_info(fs_type
);
259 ret
= info
->exists(filename
);
266 int fs_size(const char *filename
, loff_t
*size
)
270 struct fstype_info
*info
= fs_get_info(fs_type
);
272 ret
= info
->size(filename
, size
);
279 int fs_read(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
282 struct fstype_info
*info
= fs_get_info(fs_type
);
287 * We don't actually know how many bytes are being read, since len==0
288 * means read the whole file.
290 buf
= map_sysmem(addr
, len
);
291 ret
= info
->read(filename
, buf
, offset
, len
, actread
);
294 /* If we requested a specific number of bytes, check we got it */
295 if (ret
== 0 && len
&& *actread
!= len
) {
296 printf("** Unable to read file %s **\n", filename
);
304 int fs_write(const char *filename
, ulong addr
, loff_t offset
, loff_t len
,
307 struct fstype_info
*info
= fs_get_info(fs_type
);
311 buf
= map_sysmem(addr
, len
);
312 ret
= info
->write(filename
, buf
, offset
, len
, actwrite
);
315 if (ret
< 0 && len
!= *actwrite
) {
316 printf("** Unable to write file %s **\n", filename
);
324 int do_size(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
330 return CMD_RET_USAGE
;
332 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
335 if (fs_size(argv
[3], &size
) < 0)
336 return CMD_RET_FAILURE
;
338 setenv_hex("filesize", size
);
343 int do_load(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
347 const char *addr_str
;
348 const char *filename
;
357 return CMD_RET_USAGE
;
359 return CMD_RET_USAGE
;
361 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
365 addr
= simple_strtoul(argv
[3], &ep
, 16);
366 if (ep
== argv
[3] || *ep
!= '\0')
367 return CMD_RET_USAGE
;
369 addr_str
= getenv("loadaddr");
370 if (addr_str
!= NULL
)
371 addr
= simple_strtoul(addr_str
, NULL
, 16);
373 addr
= CONFIG_SYS_LOAD_ADDR
;
378 filename
= getenv("bootfile");
380 puts("** No boot file defined **\n");
385 bytes
= simple_strtoul(argv
[5], NULL
, 16);
389 pos
= simple_strtoul(argv
[6], NULL
, 16);
394 ret
= fs_read(filename
, addr
, pos
, bytes
, &len_read
);
395 time
= get_timer(time
);
399 printf("%llu bytes read in %lu ms", len_read
, time
);
402 print_size(len_read
/ time
* 1000, "/s");
407 setenv_hex("filesize", len_read
);
412 int do_ls(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
416 return CMD_RET_USAGE
;
418 return CMD_RET_USAGE
;
420 if (fs_set_blk_dev(argv
[1], (argc
>= 3) ? argv
[2] : NULL
, fstype
))
423 if (fs_ls(argc
>= 4 ? argv
[3] : "/"))
429 int file_exists(const char *dev_type
, const char *dev_part
, const char *file
,
432 if (fs_set_blk_dev(dev_type
, dev_part
, fstype
))
435 return fs_exists(file
);
438 int do_save(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
442 const char *filename
;
449 if (argc
< 6 || argc
> 7)
450 return CMD_RET_USAGE
;
452 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
455 addr
= simple_strtoul(argv
[3], NULL
, 16);
457 bytes
= simple_strtoul(argv
[5], NULL
, 16);
459 pos
= simple_strtoul(argv
[6], NULL
, 16);
464 ret
= fs_write(filename
, addr
, pos
, bytes
, &len
);
465 time
= get_timer(time
);
469 printf("%llu bytes written in %lu ms", len
, time
);
472 print_size(len
/ time
* 1000, "/s");
480 int do_fs_uuid(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[],
485 memset(uuid
, 0, sizeof(uuid
));
487 if (argc
< 3 || argc
> 4)
488 return CMD_RET_USAGE
;
490 if (fs_set_blk_dev(argv
[1], argv
[2], fstype
))
495 return CMD_RET_FAILURE
;
498 setenv(argv
[3], uuid
);
500 printf("%s\n", uuid
);
502 return CMD_RET_SUCCESS
;