blob: adae98d021eef1b2c4640a12aef9b0affa43b9d1 [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 Takahiroe2519da2018-09-11 15:59:13 +0900108static inline int fs_unlink_unsupported(const char *filename)
109{
110 return -1;
111}
112
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900113static inline int fs_mkdir_unsupported(const char *dirname)
114{
115 return -1;
116}
117
Simon Glass436e2b72012-12-26 09:53:29 +0000118struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000119 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100120 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -0700121 /*
122 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
123 * should be false in most cases. For "virtual" filesystems which
124 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardtca230b02018-08-11 15:52:14 +0200125 * set to true. This should also be true for the dummy entry at the end
Stephen Warren377202b2014-02-03 13:21:01 -0700126 * of fstypes[], since that is essentially a "virtual" (non-existent)
127 * filesystem.
128 */
129 bool null_dev_desc_ok;
Simon Glass4101f682016-02-29 15:25:34 -0700130 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass2ded0d42012-12-26 09:53:31 +0000131 disk_partition_t *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000132 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700133 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800134 int (*size)(const char *filename, loff_t *size);
135 int (*read)(const char *filename, void *buf, loff_t offset,
136 loff_t len, loff_t *actread);
137 int (*write)(const char *filename, void *buf, loff_t offset,
138 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000139 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100140 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-09-09 13:15:55 -0400141 /*
142 * Open a directory stream. On success return 0 and directory
143 * stream pointer via 'dirsp'. On error, return -errno. See
144 * fs_opendir().
145 */
146 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
147 /*
148 * Read next entry from directory stream. On success return 0
149 * and directory entry pointer via 'dentp'. On error return
150 * -errno. See fs_readdir().
151 */
152 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
153 /* see fs_closedir() */
154 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900155 int (*unlink)(const char *filename);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900156 int (*mkdir)(const char *dirname);
Simon Glass436e2b72012-12-26 09:53:29 +0000157};
158
159static struct fstype_info fstypes[] = {
160#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000161 {
162 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100163 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700164 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000165 .probe = fat_set_blk_dev,
166 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400167 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700168 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600169 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000170 .read = fat_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800171#ifdef CONFIG_FAT_WRITE
172 .write = file_fat_write,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900173 .unlink = fat_unlink,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900174 .mkdir = fat_mkdir,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800175#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700176 .write = fs_write_unsupported,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900177 .unlink = fs_unlink_unsupported,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900178 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800179#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100180 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400181 .opendir = fat_opendir,
182 .readdir = fat_readdir,
183 .closedir = fat_closedir,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000184 },
Simon Glass436e2b72012-12-26 09:53:29 +0000185#endif
186#ifdef CONFIG_FS_EXT4
Stephen Warren045fa1e2012-10-22 06:43:51 +0000187 {
188 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100189 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700190 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000191 .probe = ext4fs_probe,
192 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000193 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700194 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600195 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000196 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800197#ifdef CONFIG_CMD_EXT4_WRITE
198 .write = ext4_write_file,
199#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700200 .write = fs_write_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800201#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100202 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400203 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900204 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900205 .mkdir = fs_mkdir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000206 },
207#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000208#ifdef CONFIG_SANDBOX
209 {
210 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100211 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700212 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000213 .probe = sandbox_fs_set_blk_dev,
214 .close = sandbox_fs_close,
215 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700216 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600217 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000218 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000219 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100220 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400221 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900222 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900223 .mkdir = fs_mkdir_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000224 },
225#endif
Hans de Goede251cee02015-09-17 18:46:58 -0400226#ifdef CONFIG_CMD_UBIFS
227 {
228 .fstype = FS_TYPE_UBIFS,
229 .name = "ubifs",
230 .null_dev_desc_ok = true,
231 .probe = ubifs_set_blk_dev,
232 .close = ubifs_close,
233 .ls = ubifs_ls,
234 .exists = ubifs_exists,
235 .size = ubifs_size,
236 .read = ubifs_read,
237 .write = fs_write_unsupported,
238 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400239 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900240 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900241 .mkdir = fs_mkdir_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400242 },
243#endif
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200244#ifdef CONFIG_FS_BTRFS
245 {
246 .fstype = FS_TYPE_BTRFS,
247 .name = "btrfs",
248 .null_dev_desc_ok = false,
249 .probe = btrfs_probe,
250 .close = btrfs_close,
251 .ls = btrfs_ls,
252 .exists = btrfs_exists,
253 .size = btrfs_size,
254 .read = btrfs_read,
255 .write = fs_write_unsupported,
256 .uuid = btrfs_uuid,
Marek BehĂșn38fc6832017-10-06 16:56:07 +0200257 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900258 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900259 .mkdir = fs_mkdir_unsupported,
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200260 },
261#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000262 {
263 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100264 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700265 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000266 .probe = fs_probe_unsupported,
267 .close = fs_close_unsupported,
268 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700269 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600270 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000271 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000272 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100273 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400274 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900275 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900276 .mkdir = fs_mkdir_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000277 },
278};
279
Simon Glassc6f548d2012-12-26 09:53:30 +0000280static struct fstype_info *fs_get_info(int fstype)
281{
282 struct fstype_info *info;
283 int i;
284
285 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
286 if (fstype == info->fstype)
287 return info;
288 }
289
290 /* Return the 'unsupported' sentinel */
291 return info;
292}
293
Alex Kiernan0d488e82018-05-29 15:30:50 +0000294/**
295 * fs_get_type_name() - Get type of current filesystem
296 *
297 * Return: Pointer to filesystem name
298 *
299 * Returns a string describing the current filesystem, or the sentinel
300 * "unsupported" for any unrecognised filesystem.
301 */
302const char *fs_get_type_name(void)
303{
304 return fs_get_info(fs_type)->name;
305}
306
Stephen Warren045fa1e2012-10-22 06:43:51 +0000307int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
308{
Simon Glass436e2b72012-12-26 09:53:29 +0000309 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000310 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000311#ifdef CONFIG_NEEDS_MANUAL_RELOC
312 static int relocated;
313
314 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000315 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
316 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100317 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000318 info->probe += gd->reloc_off;
319 info->close += gd->reloc_off;
320 info->ls += gd->reloc_off;
321 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000322 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000323 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000324 relocated = 1;
325 }
326#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000327
Simon Glasse35929e2016-02-29 15:25:44 -0700328 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000329 &fs_partition, 1);
330 if (part < 0)
331 return -1;
332
Simon Glass436e2b72012-12-26 09:53:29 +0000333 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
334 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
335 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000336 continue;
337
Stephen Warren377202b2014-02-03 13:21:01 -0700338 if (!fs_dev_desc && !info->null_dev_desc_ok)
339 continue;
340
Simon Glass2ded0d42012-12-26 09:53:31 +0000341 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000342 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400343 fs_dev_part = part;
344 return 0;
345 }
346 }
347
348 return -1;
349}
350
351/* set current blk device w/ blk_desc + partition # */
352int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
353{
354 struct fstype_info *info;
355 int ret, i;
356
357 if (part >= 1)
358 ret = part_get_info(desc, part, &fs_partition);
359 else
360 ret = part_get_info_whole_disk(desc, &fs_partition);
361 if (ret)
362 return ret;
363 fs_dev_desc = desc;
364
365 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
366 if (!info->probe(fs_dev_desc, &fs_partition)) {
367 fs_type = info->fstype;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000368 return 0;
369 }
370 }
371
Stephen Warren045fa1e2012-10-22 06:43:51 +0000372 return -1;
373}
374
375static void fs_close(void)
376{
Simon Glassc6f548d2012-12-26 09:53:30 +0000377 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000378
Simon Glassc6f548d2012-12-26 09:53:30 +0000379 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000380
Stephen Warren045fa1e2012-10-22 06:43:51 +0000381 fs_type = FS_TYPE_ANY;
382}
383
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100384int fs_uuid(char *uuid_str)
385{
386 struct fstype_info *info = fs_get_info(fs_type);
387
388 return info->uuid(uuid_str);
389}
390
Stephen Warren045fa1e2012-10-22 06:43:51 +0000391int fs_ls(const char *dirname)
392{
393 int ret;
394
Simon Glassc6f548d2012-12-26 09:53:30 +0000395 struct fstype_info *info = fs_get_info(fs_type);
396
397 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000398
Simon Glasse6d52412012-12-26 09:53:33 +0000399 fs_type = FS_TYPE_ANY;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000400 fs_close();
401
402 return ret;
403}
404
Stephen Warren61529162014-02-03 13:21:00 -0700405int fs_exists(const char *filename)
406{
407 int ret;
408
409 struct fstype_info *info = fs_get_info(fs_type);
410
411 ret = info->exists(filename);
412
413 fs_close();
414
415 return ret;
416}
417
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800418int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600419{
420 int ret;
421
422 struct fstype_info *info = fs_get_info(fs_type);
423
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800424 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600425
426 fs_close();
427
428 return ret;
429}
430
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800431int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
432 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000433{
Simon Glassc6f548d2012-12-26 09:53:30 +0000434 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000435 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000436 int ret;
437
Simon Glass117e0502012-12-26 09:53:32 +0000438 /*
439 * We don't actually know how many bytes are being read, since len==0
440 * means read the whole file.
441 */
442 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800443 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000444 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000445
Simon Glassc6f548d2012-12-26 09:53:30 +0000446 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200447 if (ret == 0 && len && *actread != len)
Heinrich Schuchardta327bde2018-01-01 21:15:37 +0100448 debug("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000449 fs_close();
450
451 return ret;
452}
453
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800454int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
455 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000456{
457 struct fstype_info *info = fs_get_info(fs_type);
458 void *buf;
459 int ret;
460
Simon Glassa8f6ab52013-04-20 08:42:50 +0000461 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800462 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000463 unmap_sysmem(buf);
464
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800465 if (ret < 0 && len != *actwrite) {
Simon Glassa8f6ab52013-04-20 08:42:50 +0000466 printf("** Unable to write file %s **\n", filename);
467 ret = -1;
468 }
469 fs_close();
470
471 return ret;
472}
473
Rob Clark4bbcc962017-09-09 13:15:55 -0400474struct fs_dir_stream *fs_opendir(const char *filename)
475{
476 struct fstype_info *info = fs_get_info(fs_type);
477 struct fs_dir_stream *dirs = NULL;
478 int ret;
479
480 ret = info->opendir(filename, &dirs);
481 fs_close();
482 if (ret) {
483 errno = -ret;
484 return NULL;
485 }
486
487 dirs->desc = fs_dev_desc;
488 dirs->part = fs_dev_part;
489
490 return dirs;
491}
492
493struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
494{
495 struct fstype_info *info;
496 struct fs_dirent *dirent;
497 int ret;
498
499 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
500 info = fs_get_info(fs_type);
501
502 ret = info->readdir(dirs, &dirent);
503 fs_close();
504 if (ret) {
505 errno = -ret;
506 return NULL;
507 }
508
509 return dirent;
510}
511
512void fs_closedir(struct fs_dir_stream *dirs)
513{
514 struct fstype_info *info;
515
516 if (!dirs)
517 return;
518
519 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
520 info = fs_get_info(fs_type);
521
522 info->closedir(dirs);
523 fs_close();
524}
525
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900526int fs_unlink(const char *filename)
527{
528 int ret;
529
530 struct fstype_info *info = fs_get_info(fs_type);
531
532 ret = info->unlink(filename);
533
534 fs_type = FS_TYPE_ANY;
535 fs_close();
536
537 return ret;
538}
Rob Clark4bbcc962017-09-09 13:15:55 -0400539
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900540int fs_mkdir(const char *dirname)
541{
542 int ret;
543
544 struct fstype_info *info = fs_get_info(fs_type);
545
546 ret = info->mkdir(dirname);
547
548 fs_type = FS_TYPE_ANY;
549 fs_close();
550
551 return ret;
552}
553
Stephen Warrencf659812014-06-11 12:47:26 -0600554int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
555 int fstype)
556{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800557 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600558
559 if (argc != 4)
560 return CMD_RET_USAGE;
561
562 if (fs_set_blk_dev(argv[1], argv[2], fstype))
563 return 1;
564
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800565 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600566 return CMD_RET_FAILURE;
567
Simon Glass018f5302017-08-03 12:22:10 -0600568 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600569
570 return 0;
571}
572
Stephen Warrenf9b55e22012-10-31 11:05:07 +0000573int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200574 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000575{
576 unsigned long addr;
577 const char *addr_str;
578 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800579 loff_t bytes;
580 loff_t pos;
581 loff_t len_read;
582 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100583 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200584 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000585
Stephen Warrene9b0f992012-10-30 12:04:17 +0000586 if (argc < 2)
587 return CMD_RET_USAGE;
588 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000589 return CMD_RET_USAGE;
590
Stephen Warrene9b0f992012-10-30 12:04:17 +0000591 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000592 return 1;
593
594 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200595 addr = simple_strtoul(argv[3], &ep, 16);
596 if (ep == argv[3] || *ep != '\0')
597 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000598 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600599 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000600 if (addr_str != NULL)
601 addr = simple_strtoul(addr_str, NULL, 16);
602 else
603 addr = CONFIG_SYS_LOAD_ADDR;
604 }
605 if (argc >= 5) {
606 filename = argv[4];
607 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600608 filename = env_get("bootfile");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000609 if (!filename) {
610 puts("** No boot file defined **\n");
611 return 1;
612 }
613 }
614 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200615 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000616 else
617 bytes = 0;
618 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200619 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000620 else
621 pos = 0;
622
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100623 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800624 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100625 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800626 if (ret < 0)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000627 return 1;
628
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800629 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100630 if (time > 0) {
631 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500632 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100633 puts(")");
634 }
635 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000636
Simon Glass018f5302017-08-03 12:22:10 -0600637 env_set_hex("fileaddr", addr);
638 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000639
640 return 0;
641}
642
643int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
644 int fstype)
645{
646 if (argc < 2)
647 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000648 if (argc > 4)
649 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000650
651 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
652 return 1;
653
Stephen Warrene9b0f992012-10-30 12:04:17 +0000654 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000655 return 1;
656
657 return 0;
658}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000659
Stephen Warren61529162014-02-03 13:21:00 -0700660int file_exists(const char *dev_type, const char *dev_part, const char *file,
661 int fstype)
662{
663 if (fs_set_blk_dev(dev_type, dev_part, fstype))
664 return 0;
665
666 return fs_exists(file);
667}
668
Simon Glassa8f6ab52013-04-20 08:42:50 +0000669int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denkb770e882013-10-05 21:07:25 +0200670 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000671{
672 unsigned long addr;
673 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800674 loff_t bytes;
675 loff_t pos;
676 loff_t len;
677 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000678 unsigned long time;
679
680 if (argc < 6 || argc > 7)
681 return CMD_RET_USAGE;
682
683 if (fs_set_blk_dev(argv[1], argv[2], fstype))
684 return 1;
685
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800686 addr = simple_strtoul(argv[3], NULL, 16);
687 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200688 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000689 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200690 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000691 else
692 pos = 0;
693
694 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800695 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000696 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800697 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000698 return 1;
699
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800700 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000701 if (time > 0) {
702 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500703 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000704 puts(")");
705 }
706 puts("\n");
707
708 return 0;
709}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100710
711int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
712 int fstype)
713{
714 int ret;
715 char uuid[37];
716 memset(uuid, 0, sizeof(uuid));
717
718 if (argc < 3 || argc > 4)
719 return CMD_RET_USAGE;
720
721 if (fs_set_blk_dev(argv[1], argv[2], fstype))
722 return 1;
723
724 ret = fs_uuid(uuid);
725 if (ret)
726 return CMD_RET_FAILURE;
727
728 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600729 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100730 else
731 printf("%s\n", uuid);
732
733 return CMD_RET_SUCCESS;
734}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100735
736int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
737{
738 struct fstype_info *info;
739
740 if (argc < 3 || argc > 4)
741 return CMD_RET_USAGE;
742
743 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
744 return 1;
745
746 info = fs_get_info(fs_type);
747
748 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600749 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100750 else
751 printf("%s\n", info->name);
752
753 return CMD_RET_SUCCESS;
754}
755
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900756int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
757 int fstype)
758{
759 if (argc != 4)
760 return CMD_RET_USAGE;
761
762 if (fs_set_blk_dev(argv[1], argv[2], fstype))
763 return 1;
764
765 if (fs_unlink(argv[3]))
766 return 1;
767
768 return 0;
769}
770
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900771int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
772 int fstype)
773{
774 int ret;
775
776 if (argc != 4)
777 return CMD_RET_USAGE;
778
779 if (fs_set_blk_dev(argv[1], argv[2], fstype))
780 return 1;
781
782 ret = fs_mkdir(argv[3]);
783 if (ret) {
784 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
785 return 1;
786 }
787
788 return 0;
789}