blob: ba9a65166c705b5bbad609bc09bf2c338087bd4c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warreneefbc3f2012-10-22 06:43:51 +00002/*
3 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Stephen Warreneefbc3f2012-10-22 06:43:51 +00004 */
5
6#include <config.h>
Christian Gmeiner9f9eec32014-11-12 14:35:04 +01007#include <errno.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +00008#include <common.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -05009#include <mapmem.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000010#include <part.h>
11#include <ext4fs.h>
12#include <fat.h>
13#include <fs.h>
Simon Glass11842872012-12-26 09:53:35 +000014#include <sandboxfs.h>
Hans de Goede690c7962015-09-17 18:46:58 -040015#include <ubifs_uboot.h>
Marek BehĂșn98ec1d12017-09-03 17:00:29 +020016#include <btrfs.h>
Simon Glasscbe5d5d2012-12-26 09:53:32 +000017#include <asm/io.h>
Tom Rinia17b7bc2014-11-24 11:50:46 -050018#include <div64.h>
19#include <linux/math64.h>
Stephen Warreneefbc3f2012-10-22 06:43:51 +000020
Stephen Warren44161af2012-10-30 07:50:47 +000021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glasse3394752016-02-29 15:25:34 -070023static struct blk_desc *fs_dev_desc;
Rob Clark2b7bfd92017-09-09 13:15:55 -040024static int fs_dev_part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +000025static disk_partition_t fs_partition;
26static int fs_type = FS_TYPE_ANY;
27
Simon Glasse3394752016-02-29 15:25:34 -070028static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +000029 disk_partition_t *fs_partition)
Simon Glass1aede482012-12-26 09:53:29 +000030{
31 printf("** Unrecognized filesystem type **\n");
32 return -1;
33}
34
Stephen Warreneefbc3f2012-10-22 06:43:51 +000035static inline int fs_ls_unsupported(const char *dirname)
36{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000037 return -1;
38}
39
Rob Clark60d74a92017-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 Warrend008fbb2014-02-03 13:21:00 -070069static inline int fs_exists_unsupported(const char *filename)
70{
71 return 0;
72}
73
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080074static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -060075{
76 return -1;
77}
78
Simon Glasscbe5d5d2012-12-26 09:53:32 +000079static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080080 loff_t offset, loff_t len,
81 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +000082{
Stephen Warreneefbc3f2012-10-22 06:43:51 +000083 return -1;
84}
85
Simon Glasseda14ea2013-04-20 08:42:50 +000086static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -080087 loff_t offset, loff_t len,
88 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +000089{
90 return -1;
91}
92
Simon Glass1aede482012-12-26 09:53:29 +000093static inline void fs_close_unsupported(void)
94{
95}
96
Christian Gmeiner9f9eec32014-11-12 14:35:04 +010097static inline int fs_uuid_unsupported(char *uuid_str)
98{
99 return -1;
100}
101
Rob Clark2b7bfd92017-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 Takahiro1ba42532018-09-11 15:59:13 +0900108static inline int fs_unlink_unsupported(const char *filename)
109{
110 return -1;
111}
112
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900113static inline int fs_mkdir_unsupported(const char *dirname)
114{
115 return -1;
116}
117
Simon Glass1aede482012-12-26 09:53:29 +0000118struct fstype_info {
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000119 int fstype;
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100120 char *name;
Stephen Warrenec63d4a2014-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 Schuchardt99dc17d2018-08-11 15:52:14 +0200125 * set to true. This should also be true for the dummy entry at the end
Stephen Warrenec63d4a2014-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 Glasse3394752016-02-29 15:25:34 -0700130 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass6a46e2f2012-12-26 09:53:31 +0000131 disk_partition_t *fs_partition);
Simon Glass1aede482012-12-26 09:53:29 +0000132 int (*ls)(const char *dirname);
Stephen Warrend008fbb2014-02-03 13:21:00 -0700133 int (*exists)(const char *filename);
Suriyan Ramasami96171fb2014-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 Glass1aede482012-12-26 09:53:29 +0000139 void (*close)(void);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100140 int (*uuid)(char *uuid_str);
Rob Clark2b7bfd92017-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 Takahiro1ba42532018-09-11 15:59:13 +0900155 int (*unlink)(const char *filename);
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900156 int (*mkdir)(const char *dirname);
Simon Glass1aede482012-12-26 09:53:29 +0000157};
158
159static struct fstype_info fstypes[] = {
160#ifdef CONFIG_FS_FAT
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000161 {
162 .fstype = FS_TYPE_FAT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100163 .name = "fat",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700164 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000165 .probe = fat_set_blk_dev,
166 .close = fat_close,
Rob Clark60d74a92017-09-09 13:15:58 -0400167 .ls = fs_ls_generic,
Stephen Warren1d2f9a02014-02-03 13:21:10 -0700168 .exists = fat_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600169 .size = fat_size,
Simon Glass19e38582012-12-26 09:53:33 +0000170 .read = fat_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800171#ifdef CONFIG_FAT_WRITE
172 .write = file_fat_write,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900173 .mkdir = fat_mkdir,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800174#else
Stephen Warren9df25802014-02-03 13:20:59 -0700175 .write = fs_write_unsupported,
AKASHI Takahiro1c24b7b2018-09-11 15:59:10 +0900176 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800177#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100178 .uuid = fs_uuid_unsupported,
Rob Clark60d74a92017-09-09 13:15:58 -0400179 .opendir = fat_opendir,
180 .readdir = fat_readdir,
181 .closedir = fat_closedir,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900182 .unlink = fs_unlink_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000183 },
Simon Glass1aede482012-12-26 09:53:29 +0000184#endif
185#ifdef CONFIG_FS_EXT4
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000186 {
187 .fstype = FS_TYPE_EXT,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100188 .name = "ext4",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700189 .null_dev_desc_ok = false,
Simon Glass19e38582012-12-26 09:53:33 +0000190 .probe = ext4fs_probe,
191 .close = ext4fs_close,
Simon Glass1aede482012-12-26 09:53:29 +0000192 .ls = ext4fs_ls,
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700193 .exists = ext4fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600194 .size = ext4fs_size,
Simon Glass19e38582012-12-26 09:53:33 +0000195 .read = ext4_read_file,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800196#ifdef CONFIG_CMD_EXT4_WRITE
197 .write = ext4_write_file,
198#else
Stephen Warren9df25802014-02-03 13:20:59 -0700199 .write = fs_write_unsupported,
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800200#endif
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100201 .uuid = ext4fs_uuid,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400202 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900203 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900204 .mkdir = fs_mkdir_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000205 },
206#endif
Simon Glass11842872012-12-26 09:53:35 +0000207#ifdef CONFIG_SANDBOX
208 {
209 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100210 .name = "sandbox",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700211 .null_dev_desc_ok = true,
Simon Glass11842872012-12-26 09:53:35 +0000212 .probe = sandbox_fs_set_blk_dev,
213 .close = sandbox_fs_close,
214 .ls = sandbox_fs_ls,
Stephen Warren97d66f22014-02-03 13:21:07 -0700215 .exists = sandbox_fs_exists,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600216 .size = sandbox_fs_size,
Simon Glass11842872012-12-26 09:53:35 +0000217 .read = fs_read_sandbox,
Simon Glassea307e82013-04-20 08:42:51 +0000218 .write = fs_write_sandbox,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100219 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400220 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900221 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900222 .mkdir = fs_mkdir_unsupported,
Simon Glass11842872012-12-26 09:53:35 +0000223 },
224#endif
Hans de Goede690c7962015-09-17 18:46:58 -0400225#ifdef CONFIG_CMD_UBIFS
226 {
227 .fstype = FS_TYPE_UBIFS,
228 .name = "ubifs",
229 .null_dev_desc_ok = true,
230 .probe = ubifs_set_blk_dev,
231 .close = ubifs_close,
232 .ls = ubifs_ls,
233 .exists = ubifs_exists,
234 .size = ubifs_size,
235 .read = ubifs_read,
236 .write = fs_write_unsupported,
237 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400238 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900239 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900240 .mkdir = fs_mkdir_unsupported,
Hans de Goede690c7962015-09-17 18:46:58 -0400241 },
242#endif
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200243#ifdef CONFIG_FS_BTRFS
244 {
245 .fstype = FS_TYPE_BTRFS,
246 .name = "btrfs",
247 .null_dev_desc_ok = false,
248 .probe = btrfs_probe,
249 .close = btrfs_close,
250 .ls = btrfs_ls,
251 .exists = btrfs_exists,
252 .size = btrfs_size,
253 .read = btrfs_read,
254 .write = fs_write_unsupported,
255 .uuid = btrfs_uuid,
Marek BehĂșn90762d92017-10-06 16:56:07 +0200256 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900257 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900258 .mkdir = fs_mkdir_unsupported,
Marek BehĂșn98ec1d12017-09-03 17:00:29 +0200259 },
260#endif
Simon Glass1aede482012-12-26 09:53:29 +0000261 {
262 .fstype = FS_TYPE_ANY,
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100263 .name = "unsupported",
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700264 .null_dev_desc_ok = true,
Simon Glass1aede482012-12-26 09:53:29 +0000265 .probe = fs_probe_unsupported,
266 .close = fs_close_unsupported,
267 .ls = fs_ls_unsupported,
Stephen Warrend008fbb2014-02-03 13:21:00 -0700268 .exists = fs_exists_unsupported,
Stephen Warren3eb58f52014-06-11 12:47:26 -0600269 .size = fs_size_unsupported,
Simon Glass1aede482012-12-26 09:53:29 +0000270 .read = fs_read_unsupported,
Simon Glasseda14ea2013-04-20 08:42:50 +0000271 .write = fs_write_unsupported,
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100272 .uuid = fs_uuid_unsupported,
Rob Clark2b7bfd92017-09-09 13:15:55 -0400273 .opendir = fs_opendir_unsupported,
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900274 .unlink = fs_unlink_unsupported,
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900275 .mkdir = fs_mkdir_unsupported,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000276 },
277};
278
Simon Glasse6aad852012-12-26 09:53:30 +0000279static struct fstype_info *fs_get_info(int fstype)
280{
281 struct fstype_info *info;
282 int i;
283
284 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
285 if (fstype == info->fstype)
286 return info;
287 }
288
289 /* Return the 'unsupported' sentinel */
290 return info;
291}
292
Alex Kiernan3a6163a2018-05-29 15:30:50 +0000293/**
294 * fs_get_type_name() - Get type of current filesystem
295 *
296 * Return: Pointer to filesystem name
297 *
298 * Returns a string describing the current filesystem, or the sentinel
299 * "unsupported" for any unrecognised filesystem.
300 */
301const char *fs_get_type_name(void)
302{
303 return fs_get_info(fs_type)->name;
304}
305
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000306int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
307{
Simon Glass1aede482012-12-26 09:53:29 +0000308 struct fstype_info *info;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000309 int part, i;
Stephen Warren44161af2012-10-30 07:50:47 +0000310#ifdef CONFIG_NEEDS_MANUAL_RELOC
311 static int relocated;
312
313 if (!relocated) {
Simon Glass1aede482012-12-26 09:53:29 +0000314 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
315 i++, info++) {
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100316 info->name += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000317 info->probe += gd->reloc_off;
318 info->close += gd->reloc_off;
319 info->ls += gd->reloc_off;
320 info->read += gd->reloc_off;
Simon Glasseda14ea2013-04-20 08:42:50 +0000321 info->write += gd->reloc_off;
Simon Glass1aede482012-12-26 09:53:29 +0000322 }
Stephen Warren44161af2012-10-30 07:50:47 +0000323 relocated = 1;
324 }
325#endif
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000326
Simon Glasse76ee972016-02-29 15:25:44 -0700327 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000328 &fs_partition, 1);
329 if (part < 0)
330 return -1;
331
Simon Glass1aede482012-12-26 09:53:29 +0000332 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
333 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
334 fstype != info->fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000335 continue;
336
Stephen Warrenec63d4a2014-02-03 13:21:01 -0700337 if (!fs_dev_desc && !info->null_dev_desc_ok)
338 continue;
339
Simon Glass6a46e2f2012-12-26 09:53:31 +0000340 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass1aede482012-12-26 09:53:29 +0000341 fs_type = info->fstype;
Rob Clark2b7bfd92017-09-09 13:15:55 -0400342 fs_dev_part = part;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000343 return 0;
344 }
345 }
346
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000347 return -1;
348}
349
Rob Clark2b7bfd92017-09-09 13:15:55 -0400350/* set current blk device w/ blk_desc + partition # */
351int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
352{
353 struct fstype_info *info;
354 int ret, i;
355
356 if (part >= 1)
357 ret = part_get_info(desc, part, &fs_partition);
358 else
359 ret = part_get_info_whole_disk(desc, &fs_partition);
360 if (ret)
361 return ret;
362 fs_dev_desc = desc;
363
364 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
365 if (!info->probe(fs_dev_desc, &fs_partition)) {
366 fs_type = info->fstype;
367 return 0;
368 }
369 }
370
371 return -1;
372}
373
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000374static void fs_close(void)
375{
Simon Glasse6aad852012-12-26 09:53:30 +0000376 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000377
Simon Glasse6aad852012-12-26 09:53:30 +0000378 info->close();
Simon Glass19e38582012-12-26 09:53:33 +0000379
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000380 fs_type = FS_TYPE_ANY;
381}
382
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100383int fs_uuid(char *uuid_str)
384{
385 struct fstype_info *info = fs_get_info(fs_type);
386
387 return info->uuid(uuid_str);
388}
389
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000390int fs_ls(const char *dirname)
391{
392 int ret;
393
Simon Glasse6aad852012-12-26 09:53:30 +0000394 struct fstype_info *info = fs_get_info(fs_type);
395
396 ret = info->ls(dirname);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000397
Simon Glass19e38582012-12-26 09:53:33 +0000398 fs_type = FS_TYPE_ANY;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000399 fs_close();
400
401 return ret;
402}
403
Stephen Warrend008fbb2014-02-03 13:21:00 -0700404int fs_exists(const char *filename)
405{
406 int ret;
407
408 struct fstype_info *info = fs_get_info(fs_type);
409
410 ret = info->exists(filename);
411
412 fs_close();
413
414 return ret;
415}
416
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800417int fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600418{
419 int ret;
420
421 struct fstype_info *info = fs_get_info(fs_type);
422
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800423 ret = info->size(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600424
425 fs_close();
426
427 return ret;
428}
429
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800430int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
431 loff_t *actread)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000432{
Simon Glasse6aad852012-12-26 09:53:30 +0000433 struct fstype_info *info = fs_get_info(fs_type);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000434 void *buf;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000435 int ret;
436
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000437 /*
438 * We don't actually know how many bytes are being read, since len==0
439 * means read the whole file.
440 */
441 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800442 ret = info->read(filename, buf, offset, len, actread);
Simon Glasscbe5d5d2012-12-26 09:53:32 +0000443 unmap_sysmem(buf);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000444
Simon Glasse6aad852012-12-26 09:53:30 +0000445 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher56d9cdf2015-08-05 17:16:58 +0200446 if (ret == 0 && len && *actread != len)
Heinrich Schuchardtf02c5c52018-01-01 21:15:37 +0100447 debug("** %s shorter than offset + len **\n", filename);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000448 fs_close();
449
450 return ret;
451}
452
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800453int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
454 loff_t *actwrite)
Simon Glasseda14ea2013-04-20 08:42:50 +0000455{
456 struct fstype_info *info = fs_get_info(fs_type);
457 void *buf;
458 int ret;
459
Simon Glasseda14ea2013-04-20 08:42:50 +0000460 buf = map_sysmem(addr, len);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800461 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glasseda14ea2013-04-20 08:42:50 +0000462 unmap_sysmem(buf);
463
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800464 if (ret < 0 && len != *actwrite) {
Simon Glasseda14ea2013-04-20 08:42:50 +0000465 printf("** Unable to write file %s **\n", filename);
466 ret = -1;
467 }
468 fs_close();
469
470 return ret;
471}
472
Rob Clark2b7bfd92017-09-09 13:15:55 -0400473struct fs_dir_stream *fs_opendir(const char *filename)
474{
475 struct fstype_info *info = fs_get_info(fs_type);
476 struct fs_dir_stream *dirs = NULL;
477 int ret;
478
479 ret = info->opendir(filename, &dirs);
480 fs_close();
481 if (ret) {
482 errno = -ret;
483 return NULL;
484 }
485
486 dirs->desc = fs_dev_desc;
487 dirs->part = fs_dev_part;
488
489 return dirs;
490}
491
492struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
493{
494 struct fstype_info *info;
495 struct fs_dirent *dirent;
496 int ret;
497
498 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
499 info = fs_get_info(fs_type);
500
501 ret = info->readdir(dirs, &dirent);
502 fs_close();
503 if (ret) {
504 errno = -ret;
505 return NULL;
506 }
507
508 return dirent;
509}
510
511void fs_closedir(struct fs_dir_stream *dirs)
512{
513 struct fstype_info *info;
514
515 if (!dirs)
516 return;
517
518 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
519 info = fs_get_info(fs_type);
520
521 info->closedir(dirs);
522 fs_close();
523}
524
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900525int fs_unlink(const char *filename)
526{
527 int ret;
528
529 struct fstype_info *info = fs_get_info(fs_type);
530
531 ret = info->unlink(filename);
532
533 fs_type = FS_TYPE_ANY;
534 fs_close();
535
536 return ret;
537}
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900538
539int fs_mkdir(const char *dirname)
540{
541 int ret;
542
543 struct fstype_info *info = fs_get_info(fs_type);
544
545 ret = info->mkdir(dirname);
546
547 fs_type = FS_TYPE_ANY;
548 fs_close();
549
550 return ret;
551}
Rob Clark2b7bfd92017-09-09 13:15:55 -0400552
Stephen Warren3eb58f52014-06-11 12:47:26 -0600553int do_size(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
554 int fstype)
555{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800556 loff_t size;
Stephen Warren3eb58f52014-06-11 12:47:26 -0600557
558 if (argc != 4)
559 return CMD_RET_USAGE;
560
561 if (fs_set_blk_dev(argv[1], argv[2], fstype))
562 return 1;
563
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800564 if (fs_size(argv[3], &size) < 0)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600565 return CMD_RET_FAILURE;
566
Simon Glass4d949a22017-08-03 12:22:10 -0600567 env_set_hex("filesize", size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600568
569 return 0;
570}
571
Stephen Warren128d3d92012-10-31 11:05:07 +0000572int do_load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200573 int fstype)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000574{
575 unsigned long addr;
576 const char *addr_str;
577 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800578 loff_t bytes;
579 loff_t pos;
580 loff_t len_read;
581 int ret;
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100582 unsigned long time;
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200583 char *ep;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000584
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000585 if (argc < 2)
586 return CMD_RET_USAGE;
587 if (argc > 7)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000588 return CMD_RET_USAGE;
589
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000590 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000591 return 1;
592
593 if (argc >= 4) {
Pavel Machek4c1a9c52014-07-09 22:42:57 +0200594 addr = simple_strtoul(argv[3], &ep, 16);
595 if (ep == argv[3] || *ep != '\0')
596 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000597 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600598 addr_str = env_get("loadaddr");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000599 if (addr_str != NULL)
600 addr = simple_strtoul(addr_str, NULL, 16);
601 else
602 addr = CONFIG_SYS_LOAD_ADDR;
603 }
604 if (argc >= 5) {
605 filename = argv[4];
606 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600607 filename = env_get("bootfile");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000608 if (!filename) {
609 puts("** No boot file defined **\n");
610 return 1;
611 }
612 }
613 if (argc >= 6)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200614 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000615 else
616 bytes = 0;
617 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200618 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000619 else
620 pos = 0;
621
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100622 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800623 ret = fs_read(filename, addr, pos, bytes, &len_read);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100624 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800625 if (ret < 0)
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000626 return 1;
627
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800628 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100629 if (time > 0) {
630 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500631 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmann1fd2d8a2012-11-14 13:32:37 +0100632 puts(")");
633 }
634 puts("\n");
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000635
Simon Glass4d949a22017-08-03 12:22:10 -0600636 env_set_hex("fileaddr", addr);
637 env_set_hex("filesize", len_read);
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000638
639 return 0;
640}
641
642int do_ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
643 int fstype)
644{
645 if (argc < 2)
646 return CMD_RET_USAGE;
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000647 if (argc > 4)
648 return CMD_RET_USAGE;
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000649
650 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
651 return 1;
652
Stephen Warrenb2d5cd62012-10-30 12:04:17 +0000653 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warreneefbc3f2012-10-22 06:43:51 +0000654 return 1;
655
656 return 0;
657}
Simon Glasseda14ea2013-04-20 08:42:50 +0000658
Stephen Warrend008fbb2014-02-03 13:21:00 -0700659int file_exists(const char *dev_type, const char *dev_part, const char *file,
660 int fstype)
661{
662 if (fs_set_blk_dev(dev_type, dev_part, fstype))
663 return 0;
664
665 return fs_exists(file);
666}
667
Simon Glasseda14ea2013-04-20 08:42:50 +0000668int do_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200669 int fstype)
Simon Glasseda14ea2013-04-20 08:42:50 +0000670{
671 unsigned long addr;
672 const char *filename;
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800673 loff_t bytes;
674 loff_t pos;
675 loff_t len;
676 int ret;
Simon Glasseda14ea2013-04-20 08:42:50 +0000677 unsigned long time;
678
679 if (argc < 6 || argc > 7)
680 return CMD_RET_USAGE;
681
682 if (fs_set_blk_dev(argv[1], argv[2], fstype))
683 return 1;
684
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800685 addr = simple_strtoul(argv[3], NULL, 16);
686 filename = argv[4];
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200687 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000688 if (argc >= 7)
Wolfgang Denka10c7a52013-10-05 21:07:25 +0200689 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glasseda14ea2013-04-20 08:42:50 +0000690 else
691 pos = 0;
692
693 time = get_timer(0);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800694 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glasseda14ea2013-04-20 08:42:50 +0000695 time = get_timer(time);
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800696 if (ret < 0)
Simon Glasseda14ea2013-04-20 08:42:50 +0000697 return 1;
698
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800699 printf("%llu bytes written in %lu ms", len, time);
Simon Glasseda14ea2013-04-20 08:42:50 +0000700 if (time > 0) {
701 puts(" (");
Tom Rinia17b7bc2014-11-24 11:50:46 -0500702 print_size(div_u64(len, time) * 1000, "/s");
Simon Glasseda14ea2013-04-20 08:42:50 +0000703 puts(")");
704 }
705 puts("\n");
706
707 return 0;
708}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100709
710int do_fs_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
711 int fstype)
712{
713 int ret;
714 char uuid[37];
715 memset(uuid, 0, sizeof(uuid));
716
717 if (argc < 3 || argc > 4)
718 return CMD_RET_USAGE;
719
720 if (fs_set_blk_dev(argv[1], argv[2], fstype))
721 return 1;
722
723 ret = fs_uuid(uuid);
724 if (ret)
725 return CMD_RET_FAILURE;
726
727 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600728 env_set(argv[3], uuid);
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100729 else
730 printf("%s\n", uuid);
731
732 return CMD_RET_SUCCESS;
733}
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100734
735int do_fs_type(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
736{
737 struct fstype_info *info;
738
739 if (argc < 3 || argc > 4)
740 return CMD_RET_USAGE;
741
742 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
743 return 1;
744
745 info = fs_get_info(fs_type);
746
747 if (argc == 4)
Simon Glass6a38e412017-08-03 12:22:09 -0600748 env_set(argv[3], info->name);
Sjoerd Simons7d9faf62015-01-05 18:13:36 +0100749 else
750 printf("%s\n", info->name);
751
752 return CMD_RET_SUCCESS;
753}
754
AKASHI Takahiro1ba42532018-09-11 15:59:13 +0900755int do_rm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
756 int fstype)
757{
758 if (argc != 4)
759 return CMD_RET_USAGE;
760
761 if (fs_set_blk_dev(argv[1], argv[2], fstype))
762 return 1;
763
764 if (fs_unlink(argv[3]))
765 return 1;
766
767 return 0;
768}
769
AKASHI Takahiroadc8c9f2018-09-11 15:59:08 +0900770int do_mkdir(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
771 int fstype)
772{
773 int ret;
774
775 if (argc != 4)
776 return CMD_RET_USAGE;
777
778 if (fs_set_blk_dev(argv[1], argv[2], fstype))
779 return 1;
780
781 ret = fs_mkdir(argv[3]);
782 if (ret) {
783 printf("** Unable to create a directory \"%s\" **\n", argv[3]);
784 return 1;
785 }
786
787 return 0;
788}