blob: 099540f38a10461a3bc2c7410293ab7c8db5a953 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren045fa1e2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warren045fa1e2012-10-22 06:43:51 +00004 */
5
6#include <config.h>
Christian Gmeiner59e890e2014-11-12 14:35:04 +01007#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00008#include <common.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -05009#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000010#include <part.h>
11#include <ext4fs.h>
12#include <fat.h>
13#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000014#include <sandboxfs.h>
Hans de Goede251cee02015-09-17 18:46:58 -040015#include <ubifs_uboot.h>
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020016#include <btrfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000017#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050018#include <div64.h>
19#include <linux/math64.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000020
Stephen Warrena1b231c2012-10-30 07:50:47 +000021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass4101f682016-02-29 15:25:34 -070023static struct blk_desc *fs_dev_desc;
Rob Clark4bbcc962017-09-09 13:15:55 -040024static int fs_dev_part;
Stephen Warren045fa1e2012-10-22 06:43:51 +000025static disk_partition_t fs_partition;
26static int fs_type = FS_TYPE_ANY;
27
Simon Glass4101f682016-02-29 15:25:34 -070028static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass2ded0d42012-12-26 09:53:31 +000029 disk_partition_t *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000030{
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33}
34
Stephen Warren045fa1e2012-10-22 06:43:51 +000035static inline int fs_ls_unsupported(const char *dirname)
36{
Stephen Warren045fa1e2012-10-22 06:43:51 +000037 return -1;
38}
39
Rob Clark89191d62017-09-09 13:15:58 -040040/* generic implementation of ls in terms of opendir/readdir/closedir */
41__maybe_unused
42static int fs_ls_generic(const char *dirname)
43{
44 struct fs_dir_stream *dirs;
45 struct fs_dirent *dent;
46 int nfiles = 0, ndirs = 0;
47
48 dirs = fs_opendir(dirname);
49 if (!dirs)
50 return -errno;
51
52 while ((dent = fs_readdir(dirs))) {
53 if (dent->type == FS_DT_DIR) {
54 printf(" %s/\n", dent->name);
55 ndirs++;
56 } else {
57 printf(" %8lld %s\n", dent->size, dent->name);
58 nfiles++;
59 }
60 }
61
62 fs_closedir(dirs);
63
64 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
65
66 return 0;
67}
68
Stephen Warren61529162014-02-03 13:21:00 -070069static inline int fs_exists_unsupported(const char *filename)
70{
71 return 0;
72}
73
Suriyan Ramasamid455d872014-11-17 14:39:38 -080074static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060075{
76 return -1;
77}
78
Simon Glass117e0502012-12-26 09:53:32 +000079static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080080 loff_t offset, loff_t len,
81 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000082{
Stephen Warren045fa1e2012-10-22 06:43:51 +000083 return -1;
84}
85
Simon Glassa8f6ab52013-04-20 08:42:50 +000086static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080087 loff_t offset, loff_t len,
88 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +000089{
90 return -1;
91}
92
Simon Glass436e2b72012-12-26 09:53:29 +000093static inline void fs_close_unsupported(void)
94{
95}
96
Christian Gmeiner59e890e2014-11-12 14:35:04 +010097static inline int fs_uuid_unsupported(char *uuid_str)
98{
99 return -1;
100}
101
Rob Clark4bbcc962017-09-09 13:15:55 -0400102static inline int fs_opendir_unsupported(const char *filename,
103 struct fs_dir_stream **dirs)
104{
105 return -EACCES;
106}
107
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900108static inline int fs_mkdir_unsupported(const char *dirname)
109{
110 return -1;
111}
112
Simon Glass436e2b72012-12-26 09:53:29 +0000113struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000114 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100115 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -0700116 /*
117 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
118 * should be false in most cases. For "virtual" filesystems which
119 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardtca230b02018-08-11 15:52:14 +0200120 * set to true. This should also be true for the dummy entry at the end
Stephen Warren377202b2014-02-03 13:21:01 -0700121 * of fstypes[], since that is essentially a "virtual" (non-existent)
122 * filesystem.
123 */
124 bool null_dev_desc_ok;
Simon Glass4101f682016-02-29 15:25:34 -0700125 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass2ded0d42012-12-26 09:53:31 +0000126 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000127 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700128 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800129 int (*size)(const char *filename, loff_t *size);
130 int (*read)(const char *filename, void *buf, loff_t offset,
131 loff_t len, loff_t *actread);
132 int (*write)(const char *filename, void *buf, loff_t offset,
133 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000134 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100135 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-09-09 13:15:55 -0400136 /*
137 * Open a directory stream. On success return 0 and directory
138 * stream pointer via 'dirsp'. On error, return -errno. See
139 * fs_opendir().
140 */
141 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
142 /*
143 * Read next entry from directory stream. On success return 0
144 * and directory entry pointer via 'dentp'. On error return
145 * -errno. See fs_readdir().
146 */
147 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
148 /* see fs_closedir() */
149 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900150 int (*mkdir)(const char *dirname);
Simon Glass436e2b72012-12-26 09:53:29 +0000151};
152
153static struct fstype_info fstypes[] = {
154#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000155 {
156 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100157 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700158 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000159 .probe = fat_set_blk_dev,
160 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400161 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700162 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600163 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000164 .read = fat_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800165#ifdef CONFIG_FAT_WRITE
166 .write = file_fat_write,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900167 .mkdir = fat_mkdir,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800168#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700169 .write = fs_write_unsupported,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900170 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800171#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100172 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400173 .opendir = fat_opendir,
174 .readdir = fat_readdir,
175 .closedir = fat_closedir,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000176 },
Simon Glass436e2b72012-12-26 09:53:29 +0000177#endif
178#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000179 {
180 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100181 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700182 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000183 .probe = ext4fs_probe,
184 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000185 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700186 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600187 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000188 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800189#ifdef CONFIG_CMD_EXT4_WRITE
190 .write = ext4_write_file,
191#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700192 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800193#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100194 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400195 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900196 .mkdir = fs_mkdir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000197 },
198#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000199#ifdef CONFIG_SANDBOX
200 {
201 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100202 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700203 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000204 .probe = sandbox_fs_set_blk_dev,
205 .close = sandbox_fs_close,
206 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700207 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600208 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000209 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000210 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100211 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400212 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900213 .mkdir = fs_mkdir_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000214 },
215#endif
Hans de Goede251cee02015-09-17 18:46:58 -0400216#ifdef CONFIG_CMD_UBIFS
217 {
218 .fstype = FS_TYPE_UBIFS,
219 .name = "ubifs",
220 .null_dev_desc_ok = true,
221 .probe = ubifs_set_blk_dev,
222 .close = ubifs_close,
223 .ls = ubifs_ls,
224 .exists = ubifs_exists,
225 .size = ubifs_size,
226 .read = ubifs_read,
227 .write = fs_write_unsupported,
228 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400229 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900230 .mkdir = fs_mkdir_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400231 },
232#endif
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200233#ifdef CONFIG_FS_BTRFS
234 {
235 .fstype = FS_TYPE_BTRFS,
236 .name = "btrfs",
237 .null_dev_desc_ok = false,
238 .probe = btrfs_probe,
239 .close = btrfs_close,
240 .ls = btrfs_ls,
241 .exists = btrfs_exists,
242 .size = btrfs_size,
243 .read = btrfs_read,
244 .write = fs_write_unsupported,
245 .uuid = btrfs_uuid,
Marek BehĂșn38fc6832017-10-06 16:56:07 +0200246 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900247 .mkdir = fs_mkdir_unsupported,
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200248 },
249#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000250 {
251 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100252 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700253 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000254 .probe = fs_probe_unsupported,
255 .close = fs_close_unsupported,
256 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700257 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600258 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000259 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000260 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100261 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400262 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900263 .mkdir = fs_mkdir_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000264 },
265};
266
Simon Glassc6f548d2012-12-26 09:53:30 +0000267static struct fstype_info *fs_get_info(int fstype)
268{
269 struct fstype_info *info;
270 int i;
271
272 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
273 if (fstype == info->fstype)
274 return info;
275 }
276
277 /* Return the 'unsupported' sentinel */
278 return info;
279}
280
Alex Kiernan0d488e82018-05-29 15:30:50 +0000281/**
282 * fs_get_type_name() - Get type of current filesystem
283 *
284 * Return: Pointer to filesystem name
285 *
286 * Returns a string describing the current filesystem, or the sentinel
287 * "unsupported" for any unrecognised filesystem.
288 */
289const char *fs_get_type_name(void)
290{
291 return fs_get_info(fs_type)->name;
292}
293
Stephen Warren045fa1e2012-10-22 06:43:51 +0000294int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
295{
Simon Glass436e2b72012-12-26 09:53:29 +0000296 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000297 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000298#ifdef CONFIG_NEEDS_MANUAL_RELOC
299 static int relocated;
300
301 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000302 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
303 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100304 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000305 info->probe += gd->reloc_off;
306 info->close += gd->reloc_off;
307 info->ls += gd->reloc_off;
308 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000309 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000310 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000311 relocated = 1;
312 }
313#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000314
Simon Glasse35929e2016-02-29 15:25:44 -0700315 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000316 &fs_partition, 1);
317 if (part < 0)
318 return -1;
319
Simon Glass436e2b72012-12-26 09:53:29 +0000320 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
321 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
322 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000323 continue;
324
Stephen Warren377202b2014-02-03 13:21:01 -0700325 if (!fs_dev_desc && !info->null_dev_desc_ok)
326 continue;
327
Simon Glass2ded0d42012-12-26 09:53:31 +0000328 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000329 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400330 fs_dev_part = part;
331 return 0;
332 }
333 }
334
335 return -1;
336}
337
338/* set current blk device w/ blk_desc + partition # */
339int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
340{
341 struct fstype_info *info;
342 int ret, i;
343
344 if (part >= 1)
345 ret = part_get_info(desc, part, &fs_partition);
346 else
347 ret = part_get_info_whole_disk(desc, &fs_partition);
348 if (ret)
349 return ret;
350 fs_dev_desc = desc;
351
352 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
353 if (!info->probe(fs_dev_desc, &fs_partition)) {
354 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000355 return 0;
356 }
357 }
358
Stephen Warren045fa1e2012-10-22 06:43:51 +0000359 return -1;
360}
361
362static void fs_close(void)
363{
Simon Glassc6f548d2012-12-26 09:53:30 +0000364 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000365
Simon Glassc6f548d2012-12-26 09:53:30 +0000366 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000367
Stephen Warren045fa1e2012-10-22 06:43:51 +0000368 fs_type = FS_TYPE_ANY;
369}
370
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100371int fs_uuid(char *uuid_str)
372{
373 struct fstype_info *info = fs_get_info(fs_type);
374
375 return info->uuid(uuid_str);
376}
377
Stephen Warren045fa1e2012-10-22 06:43:51 +0000378int fs_ls(const char *dirname)
379{
380 int ret;
381
Simon Glassc6f548d2012-12-26 09:53:30 +0000382 struct fstype_info *info = fs_get_info(fs_type);
383
384 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000385
Simon Glasse6d52412012-12-26 09:53:33 +0000386 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000387 fs_close();
388
389 return ret;
390}
391
Stephen Warren61529162014-02-03 13:21:00 -0700392int fs_exists(const char *filename)
393{
394 int ret;
395
396 struct fstype_info *info = fs_get_info(fs_type);
397
398 ret = info->exists(filename);
399
400 fs_close();
401
402 return ret;
403}
404
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800405int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600406{
407 int ret;
408
409 struct fstype_info *info = fs_get_info(fs_type);
410
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800411 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600412
413 fs_close();
414
415 return ret;
416}
417
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800418int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
419 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000420{
Simon Glassc6f548d2012-12-26 09:53:30 +0000421 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000422 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000423 int ret;
424
Simon Glass117e0502012-12-26 09:53:32 +0000425 /*
426 * We don't actually know how many bytes are being read, since len==0
427 * means read the whole file.
428 */
429 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800430 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000431 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000432
Simon Glassc6f548d2012-12-26 09:53:30 +0000433 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200434 if (ret == 0 && len && *actread != len)
Heinrich Schuchardta327bde2018-01-01 21:15:37 +0100435 debug("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000436 fs_close();
437
438 return ret;
439}
440
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800441int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
442 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000443{
444 struct fstype_info *info = fs_get_info(fs_type);
445 void *buf;
446 int ret;
447
Simon Glassa8f6ab52013-04-20 08:42:50 +0000448 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800449 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000450 unmap_sysmem(buf);
451
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800452 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000453 printf("** Unable to write file %s **\n", filename);
454 ret = -1;
455 }
456 fs_close();
457
458 return ret;
459}
460
Rob Clark4bbcc962017-09-09 13:15:55 -0400461struct fs_dir_stream *fs_opendir(const char *filename)
462{
463 struct fstype_info *info = fs_get_info(fs_type);
464 struct fs_dir_stream *dirs = NULL;
465 int ret;
466
467 ret = info->opendir(filename, &dirs);
468 fs_close();
469 if (ret) {
470 errno = -ret;
471 return NULL;
472 }
473
474 dirs->desc = fs_dev_desc;
475 dirs->part = fs_dev_part;
476
477 return dirs;
478}
479
480struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
481{
482 struct fstype_info *info;
483 struct fs_dirent *dirent;
484 int ret;
485
486 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
487 info = fs_get_info(fs_type);
488
489 ret = info->readdir(dirs, &dirent);
490 fs_close();
491 if (ret) {
492 errno = -ret;
493 return NULL;
494 }
495
496 return dirent;
497}
498
499void fs_closedir(struct fs_dir_stream *dirs)
500{
501 struct fstype_info *info;
502
503 if (!dirs)
504 return;
505
506 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
507 info = fs_get_info(fs_type);
508
509 info->closedir(dirs);
510 fs_close();
511}
512
513
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900514int fs_mkdir(const char *dirname)
515{
516 int ret;
517
518 struct fstype_info *info = fs_get_info(fs_type);
519
520 ret = info->mkdir(dirname);
521
522 fs_type = FS_TYPE_ANY;
523 fs_close();
524
525 return ret;
526}
527
Stephen Warrencf659812014-06-11 12:47:26 -0600528int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
529 int fstype)
530{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800531 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600532
533 if (argc != 4)
534 return CMD_RET_USAGE;
535
536 if (fs_set_blk_dev(argv[1], argv[2], fstype))
537 return 1;
538
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800539 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600540 return CMD_RET_FAILURE;
541
Simon Glass018f5302017-08-03 12:22:10 -0600542 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600543
544 return 0;
545}
546
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000547int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200548 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000549{
550 unsigned long addr;
551 const char *addr_str;
552 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800553 loff_t bytes;
554 loff_t pos;
555 loff_t len_read;
556 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100557 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200558 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000559
Stephen Warrene9b0f992012-10-30 12:04:17 +0000560 if (argc < 2)
561 return CMD_RET_USAGE;
562 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000563 return CMD_RET_USAGE;
564
Stephen Warrene9b0f992012-10-30 12:04:17 +0000565 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000566 return 1;
567
568 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200569 addr = simple_strtoul(argv[3], &ep, 16);
570 if (ep == argv[3] || *ep != '\0')
571 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000572 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600573 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000574 if (addr_str != NULL)
575 addr = simple_strtoul(addr_str, NULL, 16);
576 else
577 addr = CONFIG_SYS_LOAD_ADDR;
578 }
579 if (argc >= 5) {
580 filename = argv[4];
581 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600582 filename = env_get("bootfile");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000583 if (!filename) {
584 puts("** No boot file defined **\n");
585 return 1;
586 }
587 }
588 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200589 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000590 else
591 bytes = 0;
592 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200593 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000594 else
595 pos = 0;
596
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100597 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800598 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100599 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800600 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000601 return 1;
602
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800603 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100604 if (time > 0) {
605 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500606 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100607 puts(")");
608 }
609 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000610
Simon Glass018f5302017-08-03 12:22:10 -0600611 env_set_hex("fileaddr", addr);
612 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000613
614 return 0;
615}
616
617int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
618 int fstype)
619{
620 if (argc < 2)
621 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000622 if (argc > 4)
623 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000624
625 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
626 return 1;
627
Stephen Warrene9b0f992012-10-30 12:04:17 +0000628 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000629 return 1;
630
631 return 0;
632}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000633
Stephen Warren61529162014-02-03 13:21:00 -0700634int file_exists(const char *dev_type, const char *dev_part, const char *file,
635 int fstype)
636{
637 if (fs_set_blk_dev(dev_type, dev_part, fstype))
638 return 0;
639
640 return fs_exists(file);
641}
642
Simon Glassa8f6ab52013-04-20 08:42:50 +0000643int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200644 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000645{
646 unsigned long addr;
647 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800648 loff_t bytes;
649 loff_t pos;
650 loff_t len;
651 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000652 unsigned long time;
653
654 if (argc < 6 || argc > 7)
655 return CMD_RET_USAGE;
656
657 if (fs_set_blk_dev(argv[1], argv[2], fstype))
658 return 1;
659
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800660 addr = simple_strtoul(argv[3], NULL, 16);
661 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200662 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000663 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200664 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000665 else
666 pos = 0;
667
668 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800669 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000670 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800671 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000672 return 1;
673
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800674 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000675 if (time > 0) {
676 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500677 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000678 puts(")");
679 }
680 puts("\n");
681
682 return 0;
683}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100684
685int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
686 int fstype)
687{
688 int ret;
689 char uuid[37];
690 memset(uuid, 0, sizeof(uuid));
691
692 if (argc < 3 || argc > 4)
693 return CMD_RET_USAGE;
694
695 if (fs_set_blk_dev(argv[1], argv[2], fstype))
696 return 1;
697
698 ret = fs_uuid(uuid);
699 if (ret)
700 return CMD_RET_FAILURE;
701
702 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600703 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100704 else
705 printf("%s\n", uuid);
706
707 return CMD_RET_SUCCESS;
708}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100709
710int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
711{
712 struct fstype_info *info;
713
714 if (argc < 3 || argc > 4)
715 return CMD_RET_USAGE;
716
717 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
718 return 1;
719
720 info = fs_get_info(fs_type);
721
722 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600723 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100724 else
725 printf("%s\n", info->name);
726
727 return CMD_RET_SUCCESS;
728}
729
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900730int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
731 int fstype)
732{
733 int ret;
734
735 if (argc != 4)
736 return CMD_RET_USAGE;
737
738 if (fs_set_blk_dev(argv[1], argv[2], fstype))
739 return 1;
740
741 ret = fs_mkdir(argv[3]);
742 if (ret) {
743 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
744 return 1;
745 }
746
747 return 0;
748}