blob: 29ad4d1a695f9ad255638c1dcf32f5dc54c656f5 [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
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +00006#define LOG_CATEGORY LOGC_CORE
7
Simon Glass09140112020-05-10 11:40:03 -06008#include <command.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +00009#include <config.h>
Christian Gmeiner59e890e2014-11-12 14:35:04 +010010#include <errno.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000011#include <common.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060012#include <env.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060013#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050015#include <mapmem.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000016#include <part.h>
17#include <ext4fs.h>
18#include <fat.h>
19#include <fs.h>
Simon Glass92ccc962012-12-26 09:53:35 +000020#include <sandboxfs.h>
Hans de Goede251cee02015-09-17 18:46:58 -040021#include <ubifs_uboot.h>
Marek BehĂșn0c936ee2017-09-03 17:00:29 +020022#include <btrfs.h>
Simon Glass117e0502012-12-26 09:53:32 +000023#include <asm/io.h>
Tom Rini9e374e72014-11-24 11:50:46 -050024#include <div64.h>
25#include <linux/math64.h>
Mian Yousaf Kaukabee88eac2019-06-18 15:03:44 +020026#include <efi_loader.h>
Joao Marcos Costac5100612020-07-30 15:33:47 +020027#include <squashfs.h>
Stephen Warren045fa1e2012-10-22 06:43:51 +000028
Stephen Warrena1b231c2012-10-30 07:50:47 +000029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glass4101f682016-02-29 15:25:34 -070031static struct blk_desc *fs_dev_desc;
Rob Clark4bbcc962017-09-09 13:15:55 -040032static int fs_dev_part;
Simon Glass05289792020-05-10 11:39:57 -060033static struct disk_partition fs_partition;
Stephen Warren045fa1e2012-10-22 06:43:51 +000034static int fs_type = FS_TYPE_ANY;
35
Simon Glass4101f682016-02-29 15:25:34 -070036static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -060037 struct disk_partition *fs_partition)
Simon Glass436e2b72012-12-26 09:53:29 +000038{
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +000039 log_err("** Unrecognized filesystem type **\n");
Simon Glass436e2b72012-12-26 09:53:29 +000040 return -1;
41}
42
Stephen Warren045fa1e2012-10-22 06:43:51 +000043static inline int fs_ls_unsupported(const char *dirname)
44{
Stephen Warren045fa1e2012-10-22 06:43:51 +000045 return -1;
46}
47
Rob Clark89191d62017-09-09 13:15:58 -040048/* generic implementation of ls in terms of opendir/readdir/closedir */
49__maybe_unused
50static int fs_ls_generic(const char *dirname)
51{
52 struct fs_dir_stream *dirs;
53 struct fs_dirent *dent;
54 int nfiles = 0, ndirs = 0;
55
56 dirs = fs_opendir(dirname);
57 if (!dirs)
58 return -errno;
59
60 while ((dent = fs_readdir(dirs))) {
61 if (dent->type == FS_DT_DIR) {
62 printf(" %s/\n", dent->name);
63 ndirs++;
Joao Marcos Costa02c366b2020-07-30 15:33:51 +020064 } else if (dent->type == FS_DT_LNK) {
65 printf(" <SYM> %s\n", dent->name);
66 nfiles++;
Rob Clark89191d62017-09-09 13:15:58 -040067 } else {
68 printf(" %8lld %s\n", dent->size, dent->name);
69 nfiles++;
70 }
71 }
72
73 fs_closedir(dirs);
74
75 printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs);
76
77 return 0;
78}
79
Stephen Warren61529162014-02-03 13:21:00 -070080static inline int fs_exists_unsupported(const char *filename)
81{
82 return 0;
83}
84
Suriyan Ramasamid455d872014-11-17 14:39:38 -080085static inline int fs_size_unsupported(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -060086{
87 return -1;
88}
89
Simon Glass117e0502012-12-26 09:53:32 +000090static inline int fs_read_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080091 loff_t offset, loff_t len,
92 loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +000093{
Stephen Warren045fa1e2012-10-22 06:43:51 +000094 return -1;
95}
96
Simon Glassa8f6ab52013-04-20 08:42:50 +000097static inline int fs_write_unsupported(const char *filename, void *buf,
Suriyan Ramasamid455d872014-11-17 14:39:38 -080098 loff_t offset, loff_t len,
99 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000100{
101 return -1;
102}
103
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100104static inline int fs_ln_unsupported(const char *filename, const char *target)
105{
106 return -1;
107}
108
Simon Glass436e2b72012-12-26 09:53:29 +0000109static inline void fs_close_unsupported(void)
110{
111}
112
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100113static inline int fs_uuid_unsupported(char *uuid_str)
114{
115 return -1;
116}
117
Rob Clark4bbcc962017-09-09 13:15:55 -0400118static inline int fs_opendir_unsupported(const char *filename,
119 struct fs_dir_stream **dirs)
120{
121 return -EACCES;
122}
123
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900124static inline int fs_unlink_unsupported(const char *filename)
125{
126 return -1;
127}
128
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900129static inline int fs_mkdir_unsupported(const char *dirname)
130{
131 return -1;
132}
133
Simon Glass436e2b72012-12-26 09:53:29 +0000134struct fstype_info {
Stephen Warren045fa1e2012-10-22 06:43:51 +0000135 int fstype;
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100136 char *name;
Stephen Warren377202b2014-02-03 13:21:01 -0700137 /*
138 * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This
139 * should be false in most cases. For "virtual" filesystems which
140 * aren't based on a U-Boot block device (e.g. sandbox), this can be
Heinrich Schuchardtca230b02018-08-11 15:52:14 +0200141 * set to true. This should also be true for the dummy entry at the end
Stephen Warren377202b2014-02-03 13:21:01 -0700142 * of fstypes[], since that is essentially a "virtual" (non-existent)
143 * filesystem.
144 */
145 bool null_dev_desc_ok;
Simon Glass4101f682016-02-29 15:25:34 -0700146 int (*probe)(struct blk_desc *fs_dev_desc,
Simon Glass05289792020-05-10 11:39:57 -0600147 struct disk_partition *fs_partition);
Simon Glass436e2b72012-12-26 09:53:29 +0000148 int (*ls)(const char *dirname);
Stephen Warren61529162014-02-03 13:21:00 -0700149 int (*exists)(const char *filename);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800150 int (*size)(const char *filename, loff_t *size);
151 int (*read)(const char *filename, void *buf, loff_t offset,
152 loff_t len, loff_t *actread);
153 int (*write)(const char *filename, void *buf, loff_t offset,
154 loff_t len, loff_t *actwrite);
Simon Glass436e2b72012-12-26 09:53:29 +0000155 void (*close)(void);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100156 int (*uuid)(char *uuid_str);
Rob Clark4bbcc962017-09-09 13:15:55 -0400157 /*
158 * Open a directory stream. On success return 0 and directory
159 * stream pointer via 'dirsp'. On error, return -errno. See
160 * fs_opendir().
161 */
162 int (*opendir)(const char *filename, struct fs_dir_stream **dirsp);
163 /*
164 * Read next entry from directory stream. On success return 0
165 * and directory entry pointer via 'dentp'. On error return
166 * -errno. See fs_readdir().
167 */
168 int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp);
169 /* see fs_closedir() */
170 void (*closedir)(struct fs_dir_stream *dirs);
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900171 int (*unlink)(const char *filename);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900172 int (*mkdir)(const char *dirname);
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100173 int (*ln)(const char *filename, const char *target);
Simon Glass436e2b72012-12-26 09:53:29 +0000174};
175
176static struct fstype_info fstypes[] = {
177#ifdef CONFIG_FS_FAT
Stephen Warren045fa1e2012-10-22 06:43:51 +0000178 {
179 .fstype = FS_TYPE_FAT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100180 .name = "fat",
Stephen Warren377202b2014-02-03 13:21:01 -0700181 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000182 .probe = fat_set_blk_dev,
183 .close = fat_close,
Rob Clark89191d62017-09-09 13:15:58 -0400184 .ls = fs_ls_generic,
Stephen Warrenb7b5f312014-02-03 13:21:10 -0700185 .exists = fat_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600186 .size = fat_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000187 .read = fat_read_file,
Tien Fong Cheed8c3ea92019-01-23 14:20:04 +0800188#if CONFIG_IS_ENABLED(FAT_WRITE)
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800189 .write = file_fat_write,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900190 .unlink = fat_unlink,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900191 .mkdir = fat_mkdir,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800192#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700193 .write = fs_write_unsupported,
AKASHI Takahirof8240ce2018-09-11 15:59:14 +0900194 .unlink = fs_unlink_unsupported,
AKASHI Takahiro31a18d52018-09-11 15:59:10 +0900195 .mkdir = fs_mkdir_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800196#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100197 .uuid = fs_uuid_unsupported,
Rob Clark89191d62017-09-09 13:15:58 -0400198 .opendir = fat_opendir,
199 .readdir = fat_readdir,
200 .closedir = fat_closedir,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100201 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000202 },
Simon Glass436e2b72012-12-26 09:53:29 +0000203#endif
Tien Fong Cheecafc4292019-01-23 14:20:06 +0800204
205#if CONFIG_IS_ENABLED(FS_EXT4)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000206 {
207 .fstype = FS_TYPE_EXT,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100208 .name = "ext4",
Stephen Warren377202b2014-02-03 13:21:01 -0700209 .null_dev_desc_ok = false,
Simon Glasse6d52412012-12-26 09:53:33 +0000210 .probe = ext4fs_probe,
211 .close = ext4fs_close,
Simon Glass436e2b72012-12-26 09:53:29 +0000212 .ls = ext4fs_ls,
Stephen Warren55af5c92014-02-03 13:21:09 -0700213 .exists = ext4fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600214 .size = ext4fs_size,
Simon Glasse6d52412012-12-26 09:53:33 +0000215 .read = ext4_read_file,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800216#ifdef CONFIG_CMD_EXT4_WRITE
217 .write = ext4_write_file,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100218 .ln = ext4fs_create_link,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800219#else
Stephen Warrenbd6fb312014-02-03 13:20:59 -0700220 .write = fs_write_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100221 .ln = fs_ln_unsupported,
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800222#endif
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100223 .uuid = ext4fs_uuid,
Rob Clark4bbcc962017-09-09 13:15:55 -0400224 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900225 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900226 .mkdir = fs_mkdir_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000227 },
228#endif
Simon Glass92ccc962012-12-26 09:53:35 +0000229#ifdef CONFIG_SANDBOX
230 {
231 .fstype = FS_TYPE_SANDBOX,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100232 .name = "sandbox",
Stephen Warren377202b2014-02-03 13:21:01 -0700233 .null_dev_desc_ok = true,
Simon Glass92ccc962012-12-26 09:53:35 +0000234 .probe = sandbox_fs_set_blk_dev,
235 .close = sandbox_fs_close,
236 .ls = sandbox_fs_ls,
Stephen Warren0a30aa12014-02-03 13:21:07 -0700237 .exists = sandbox_fs_exists,
Stephen Warrencf659812014-06-11 12:47:26 -0600238 .size = sandbox_fs_size,
Simon Glass92ccc962012-12-26 09:53:35 +0000239 .read = fs_read_sandbox,
Simon Glass7eb2c8d2013-04-20 08:42:51 +0000240 .write = fs_write_sandbox,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100241 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400242 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900243 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900244 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100245 .ln = fs_ln_unsupported,
Simon Glass92ccc962012-12-26 09:53:35 +0000246 },
247#endif
Hans de Goede251cee02015-09-17 18:46:58 -0400248#ifdef CONFIG_CMD_UBIFS
249 {
250 .fstype = FS_TYPE_UBIFS,
251 .name = "ubifs",
252 .null_dev_desc_ok = true,
253 .probe = ubifs_set_blk_dev,
254 .close = ubifs_close,
255 .ls = ubifs_ls,
256 .exists = ubifs_exists,
257 .size = ubifs_size,
258 .read = ubifs_read,
259 .write = fs_write_unsupported,
260 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400261 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900262 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900263 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100264 .ln = fs_ln_unsupported,
Hans de Goede251cee02015-09-17 18:46:58 -0400265 },
266#endif
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200267#ifdef CONFIG_FS_BTRFS
268 {
269 .fstype = FS_TYPE_BTRFS,
270 .name = "btrfs",
271 .null_dev_desc_ok = false,
272 .probe = btrfs_probe,
273 .close = btrfs_close,
274 .ls = btrfs_ls,
275 .exists = btrfs_exists,
276 .size = btrfs_size,
277 .read = btrfs_read,
278 .write = fs_write_unsupported,
279 .uuid = btrfs_uuid,
Marek BehĂșn38fc6832017-10-06 16:56:07 +0200280 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900281 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900282 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100283 .ln = fs_ln_unsupported,
Marek BehĂșn0c936ee2017-09-03 17:00:29 +0200284 },
285#endif
Joao Marcos Costac5100612020-07-30 15:33:47 +0200286#if IS_ENABLED(CONFIG_FS_SQUASHFS)
287 {
288 .fstype = FS_TYPE_SQUASHFS,
289 .name = "squashfs",
290 .probe = sqfs_probe,
291 .opendir = sqfs_opendir,
292 .readdir = sqfs_readdir,
293 .ls = fs_ls_generic,
294 .read = sqfs_read,
295 .size = sqfs_size,
296 .close = sqfs_close,
297 .closedir = sqfs_closedir,
298 },
299#endif
Simon Glass436e2b72012-12-26 09:53:29 +0000300 {
301 .fstype = FS_TYPE_ANY,
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100302 .name = "unsupported",
Stephen Warren377202b2014-02-03 13:21:01 -0700303 .null_dev_desc_ok = true,
Simon Glass436e2b72012-12-26 09:53:29 +0000304 .probe = fs_probe_unsupported,
305 .close = fs_close_unsupported,
306 .ls = fs_ls_unsupported,
Stephen Warren61529162014-02-03 13:21:00 -0700307 .exists = fs_exists_unsupported,
Stephen Warrencf659812014-06-11 12:47:26 -0600308 .size = fs_size_unsupported,
Simon Glass436e2b72012-12-26 09:53:29 +0000309 .read = fs_read_unsupported,
Simon Glassa8f6ab52013-04-20 08:42:50 +0000310 .write = fs_write_unsupported,
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100311 .uuid = fs_uuid_unsupported,
Rob Clark4bbcc962017-09-09 13:15:55 -0400312 .opendir = fs_opendir_unsupported,
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900313 .unlink = fs_unlink_unsupported,
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900314 .mkdir = fs_mkdir_unsupported,
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100315 .ln = fs_ln_unsupported,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000316 },
317};
318
Simon Glassc6f548d2012-12-26 09:53:30 +0000319static struct fstype_info *fs_get_info(int fstype)
320{
321 struct fstype_info *info;
322 int i;
323
324 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) {
325 if (fstype == info->fstype)
326 return info;
327 }
328
329 /* Return the 'unsupported' sentinel */
330 return info;
331}
332
Alex Kiernan0d488e82018-05-29 15:30:50 +0000333/**
AKASHI Takahirob7cd9562019-10-07 14:59:37 +0900334 * fs_get_type() - Get type of current filesystem
335 *
336 * Return: filesystem type
337 *
338 * Returns filesystem type representing the current filesystem, or
339 * FS_TYPE_ANY for any unrecognised filesystem.
340 */
341int fs_get_type(void)
342{
343 return fs_type;
344}
345
346/**
Alex Kiernan0d488e82018-05-29 15:30:50 +0000347 * fs_get_type_name() - Get type of current filesystem
348 *
349 * Return: Pointer to filesystem name
350 *
351 * Returns a string describing the current filesystem, or the sentinel
352 * "unsupported" for any unrecognised filesystem.
353 */
354const char *fs_get_type_name(void)
355{
356 return fs_get_info(fs_type)->name;
357}
358
Stephen Warren045fa1e2012-10-22 06:43:51 +0000359int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype)
360{
Simon Glass436e2b72012-12-26 09:53:29 +0000361 struct fstype_info *info;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000362 int part, i;
Stephen Warrena1b231c2012-10-30 07:50:47 +0000363#ifdef CONFIG_NEEDS_MANUAL_RELOC
364 static int relocated;
365
366 if (!relocated) {
Simon Glass436e2b72012-12-26 09:53:29 +0000367 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes);
368 i++, info++) {
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100369 info->name += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000370 info->probe += gd->reloc_off;
371 info->close += gd->reloc_off;
372 info->ls += gd->reloc_off;
373 info->read += gd->reloc_off;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000374 info->write += gd->reloc_off;
Simon Glass436e2b72012-12-26 09:53:29 +0000375 }
Stephen Warrena1b231c2012-10-30 07:50:47 +0000376 relocated = 1;
377 }
378#endif
Stephen Warren045fa1e2012-10-22 06:43:51 +0000379
Simon Glasse35929e2016-02-29 15:25:44 -0700380 part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc,
Stephen Warren045fa1e2012-10-22 06:43:51 +0000381 &fs_partition, 1);
382 if (part < 0)
383 return -1;
384
Simon Glass436e2b72012-12-26 09:53:29 +0000385 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
386 if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY &&
387 fstype != info->fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000388 continue;
389
Stephen Warren377202b2014-02-03 13:21:01 -0700390 if (!fs_dev_desc && !info->null_dev_desc_ok)
391 continue;
392
Simon Glass2ded0d42012-12-26 09:53:31 +0000393 if (!info->probe(fs_dev_desc, &fs_partition)) {
Simon Glass436e2b72012-12-26 09:53:29 +0000394 fs_type = info->fstype;
Rob Clark4bbcc962017-09-09 13:15:55 -0400395 fs_dev_part = part;
396 return 0;
397 }
398 }
399
400 return -1;
401}
402
403/* set current blk device w/ blk_desc + partition # */
404int fs_set_blk_dev_with_part(struct blk_desc *desc, int part)
405{
406 struct fstype_info *info;
407 int ret, i;
408
409 if (part >= 1)
410 ret = part_get_info(desc, part, &fs_partition);
411 else
412 ret = part_get_info_whole_disk(desc, &fs_partition);
413 if (ret)
414 return ret;
415 fs_dev_desc = desc;
416
417 for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) {
418 if (!info->probe(fs_dev_desc, &fs_partition)) {
419 fs_type = info->fstype;
AKASHI Takahirob0c78d82018-10-17 16:32:02 +0900420 fs_dev_part = part;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000421 return 0;
422 }
423 }
424
Stephen Warren045fa1e2012-10-22 06:43:51 +0000425 return -1;
426}
427
AKASHI Takahiro64f49eb2019-10-07 14:59:35 +0900428void fs_close(void)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000429{
Simon Glassc6f548d2012-12-26 09:53:30 +0000430 struct fstype_info *info = fs_get_info(fs_type);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000431
Simon Glassc6f548d2012-12-26 09:53:30 +0000432 info->close();
Simon Glasse6d52412012-12-26 09:53:33 +0000433
Stephen Warren045fa1e2012-10-22 06:43:51 +0000434 fs_type = FS_TYPE_ANY;
435}
436
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100437int fs_uuid(char *uuid_str)
438{
439 struct fstype_info *info = fs_get_info(fs_type);
440
441 return info->uuid(uuid_str);
442}
443
Stephen Warren045fa1e2012-10-22 06:43:51 +0000444int fs_ls(const char *dirname)
445{
446 int ret;
447
Simon Glassc6f548d2012-12-26 09:53:30 +0000448 struct fstype_info *info = fs_get_info(fs_type);
449
450 ret = info->ls(dirname);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000451
452 fs_close();
453
454 return ret;
455}
456
Stephen Warren61529162014-02-03 13:21:00 -0700457int fs_exists(const char *filename)
458{
459 int ret;
460
461 struct fstype_info *info = fs_get_info(fs_type);
462
463 ret = info->exists(filename);
464
465 fs_close();
466
467 return ret;
468}
469
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800470int fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600471{
472 int ret;
473
474 struct fstype_info *info = fs_get_info(fs_type);
475
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800476 ret = info->size(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600477
478 fs_close();
479
480 return ret;
481}
482
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100483#ifdef CONFIG_LMB
484/* Check if a file may be read to the given address */
485static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset,
486 loff_t len, struct fstype_info *info)
487{
488 struct lmb lmb;
489 int ret;
490 loff_t size;
491 loff_t read_len;
492
493 /* get the actual size of the file */
494 ret = info->size(filename, &size);
495 if (ret)
496 return ret;
497 if (offset >= size) {
498 /* offset >= EOF, no bytes will be written */
499 return 0;
500 }
501 read_len = size - offset;
502
503 /* limit to 'len' if it is smaller */
504 if (len && len < read_len)
505 read_len = len;
506
Simon Goldschmidt9cc23232019-01-26 22:13:04 +0100507 lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100508 lmb_dump_all(&lmb);
509
510 if (lmb_alloc_addr(&lmb, addr, read_len) == addr)
511 return 0;
512
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000513 log_err("** Reading file would overwrite reserved memory **\n");
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100514 return -ENOSPC;
515}
516#endif
517
518static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
519 int do_lmb_check, loff_t *actread)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000520{
Simon Glassc6f548d2012-12-26 09:53:30 +0000521 struct fstype_info *info = fs_get_info(fs_type);
Simon Glass117e0502012-12-26 09:53:32 +0000522 void *buf;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000523 int ret;
524
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100525#ifdef CONFIG_LMB
526 if (do_lmb_check) {
527 ret = fs_read_lmb_check(filename, addr, offset, len, info);
528 if (ret)
529 return ret;
530 }
531#endif
532
Simon Glass117e0502012-12-26 09:53:32 +0000533 /*
534 * We don't actually know how many bytes are being read, since len==0
535 * means read the whole file.
536 */
537 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800538 ret = info->read(filename, buf, offset, len, actread);
Simon Glass117e0502012-12-26 09:53:32 +0000539 unmap_sysmem(buf);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000540
Simon Glassc6f548d2012-12-26 09:53:30 +0000541 /* If we requested a specific number of bytes, check we got it */
Max Krummenacher7a3e70c2015-08-05 17:16:58 +0200542 if (ret == 0 && len && *actread != len)
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000543 log_debug("** %s shorter than offset + len **\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000544 fs_close();
545
546 return ret;
547}
548
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100549int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len,
550 loff_t *actread)
551{
552 return _fs_read(filename, addr, offset, len, 0, actread);
553}
554
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800555int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len,
556 loff_t *actwrite)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000557{
558 struct fstype_info *info = fs_get_info(fs_type);
559 void *buf;
560 int ret;
561
Simon Glassa8f6ab52013-04-20 08:42:50 +0000562 buf = map_sysmem(addr, len);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800563 ret = info->write(filename, buf, offset, len, actwrite);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000564 unmap_sysmem(buf);
565
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800566 if (ret < 0 && len != *actwrite) {
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000567 log_err("** Unable to write file %s **\n", filename);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000568 ret = -1;
569 }
570 fs_close();
571
572 return ret;
573}
574
Rob Clark4bbcc962017-09-09 13:15:55 -0400575struct fs_dir_stream *fs_opendir(const char *filename)
576{
577 struct fstype_info *info = fs_get_info(fs_type);
578 struct fs_dir_stream *dirs = NULL;
579 int ret;
580
581 ret = info->opendir(filename, &dirs);
582 fs_close();
583 if (ret) {
584 errno = -ret;
585 return NULL;
586 }
587
588 dirs->desc = fs_dev_desc;
589 dirs->part = fs_dev_part;
590
591 return dirs;
592}
593
594struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs)
595{
596 struct fstype_info *info;
597 struct fs_dirent *dirent;
598 int ret;
599
600 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
601 info = fs_get_info(fs_type);
602
603 ret = info->readdir(dirs, &dirent);
604 fs_close();
605 if (ret) {
606 errno = -ret;
607 return NULL;
608 }
609
610 return dirent;
611}
612
613void fs_closedir(struct fs_dir_stream *dirs)
614{
615 struct fstype_info *info;
616
617 if (!dirs)
618 return;
619
620 fs_set_blk_dev_with_part(dirs->desc, dirs->part);
621 info = fs_get_info(fs_type);
622
623 info->closedir(dirs);
624 fs_close();
625}
626
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900627int fs_unlink(const char *filename)
628{
629 int ret;
630
631 struct fstype_info *info = fs_get_info(fs_type);
632
633 ret = info->unlink(filename);
634
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900635 fs_close();
636
637 return ret;
638}
Rob Clark4bbcc962017-09-09 13:15:55 -0400639
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900640int fs_mkdir(const char *dirname)
641{
642 int ret;
643
644 struct fstype_info *info = fs_get_info(fs_type);
645
646 ret = info->mkdir(dirname);
647
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900648 fs_close();
649
650 return ret;
651}
652
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100653int fs_ln(const char *fname, const char *target)
654{
655 struct fstype_info *info = fs_get_info(fs_type);
656 int ret;
657
658 ret = info->ln(fname, target);
659
660 if (ret < 0) {
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000661 log_err("** Unable to create link %s -> %s **\n", fname, target);
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100662 ret = -1;
663 }
664 fs_close();
665
666 return ret;
667}
668
Simon Glass09140112020-05-10 11:40:03 -0600669int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
670 int fstype)
Stephen Warrencf659812014-06-11 12:47:26 -0600671{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800672 loff_t size;
Stephen Warrencf659812014-06-11 12:47:26 -0600673
674 if (argc != 4)
675 return CMD_RET_USAGE;
676
677 if (fs_set_blk_dev(argv[1], argv[2], fstype))
678 return 1;
679
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800680 if (fs_size(argv[3], &size) < 0)
Stephen Warrencf659812014-06-11 12:47:26 -0600681 return CMD_RET_FAILURE;
682
Simon Glass018f5302017-08-03 12:22:10 -0600683 env_set_hex("filesize", size);
Stephen Warrencf659812014-06-11 12:47:26 -0600684
685 return 0;
686}
687
Simon Glass09140112020-05-10 11:40:03 -0600688int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
689 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000690{
691 unsigned long addr;
692 const char *addr_str;
693 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800694 loff_t bytes;
695 loff_t pos;
696 loff_t len_read;
697 int ret;
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100698 unsigned long time;
Pavel Machek949bbd72014-07-09 22:42:57 +0200699 char *ep;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000700
Stephen Warrene9b0f992012-10-30 12:04:17 +0000701 if (argc < 2)
702 return CMD_RET_USAGE;
703 if (argc > 7)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000704 return CMD_RET_USAGE;
705
Stephen Warrene9b0f992012-10-30 12:04:17 +0000706 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000707 return 1;
708
709 if (argc >= 4) {
Pavel Machek949bbd72014-07-09 22:42:57 +0200710 addr = simple_strtoul(argv[3], &ep, 16);
711 if (ep == argv[3] || *ep != '\0')
712 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000713 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600714 addr_str = env_get("loadaddr");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000715 if (addr_str != NULL)
716 addr = simple_strtoul(addr_str, NULL, 16);
717 else
718 addr = CONFIG_SYS_LOAD_ADDR;
719 }
720 if (argc >= 5) {
721 filename = argv[4];
722 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600723 filename = env_get("bootfile");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000724 if (!filename) {
725 puts("** No boot file defined **\n");
726 return 1;
727 }
728 }
729 if (argc >= 6)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200730 bytes = simple_strtoul(argv[5], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000731 else
732 bytes = 0;
733 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200734 pos = simple_strtoul(argv[6], NULL, 16);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000735 else
736 pos = 0;
737
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100738 time = get_timer(0);
Simon Goldschmidtaa3c6092019-01-14 22:38:19 +0100739 ret = _fs_read(filename, addr, pos, bytes, 1, &len_read);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100740 time = get_timer(time);
Heinrich Schuchardt1244f362020-06-29 20:08:49 +0200741 if (ret < 0) {
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000742 log_err("Failed to load '%s'\n", filename);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000743 return 1;
Heinrich Schuchardt1244f362020-06-29 20:08:49 +0200744 }
745
746 if (IS_ENABLED(CONFIG_CMD_BOOTEFI))
747 efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "",
748 (argc > 4) ? argv[4] : "");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000749
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800750 printf("%llu bytes read in %lu ms", len_read, time);
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100751 if (time > 0) {
752 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500753 print_size(div_u64(len_read, time) * 1000, "/s");
Andreas Bießmannda1fd962012-11-14 13:32:37 +0100754 puts(")");
755 }
756 puts("\n");
Stephen Warren045fa1e2012-10-22 06:43:51 +0000757
Simon Glass018f5302017-08-03 12:22:10 -0600758 env_set_hex("fileaddr", addr);
759 env_set_hex("filesize", len_read);
Stephen Warren045fa1e2012-10-22 06:43:51 +0000760
761 return 0;
762}
763
Simon Glass09140112020-05-10 11:40:03 -0600764int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
765 int fstype)
Stephen Warren045fa1e2012-10-22 06:43:51 +0000766{
767 if (argc < 2)
768 return CMD_RET_USAGE;
Stephen Warrene9b0f992012-10-30 12:04:17 +0000769 if (argc > 4)
770 return CMD_RET_USAGE;
Stephen Warren045fa1e2012-10-22 06:43:51 +0000771
772 if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype))
773 return 1;
774
Stephen Warrene9b0f992012-10-30 12:04:17 +0000775 if (fs_ls(argc >= 4 ? argv[3] : "/"))
Stephen Warren045fa1e2012-10-22 06:43:51 +0000776 return 1;
777
778 return 0;
779}
Simon Glassa8f6ab52013-04-20 08:42:50 +0000780
Stephen Warren61529162014-02-03 13:21:00 -0700781int file_exists(const char *dev_type, const char *dev_part, const char *file,
782 int fstype)
783{
784 if (fs_set_blk_dev(dev_type, dev_part, fstype))
785 return 0;
786
787 return fs_exists(file);
788}
789
Simon Glass09140112020-05-10 11:40:03 -0600790int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
791 int fstype)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000792{
793 unsigned long addr;
794 const char *filename;
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800795 loff_t bytes;
796 loff_t pos;
797 loff_t len;
798 int ret;
Simon Glassa8f6ab52013-04-20 08:42:50 +0000799 unsigned long time;
800
801 if (argc < 6 || argc > 7)
802 return CMD_RET_USAGE;
803
804 if (fs_set_blk_dev(argv[1], argv[2], fstype))
805 return 1;
806
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800807 addr = simple_strtoul(argv[3], NULL, 16);
808 filename = argv[4];
Wolfgang Denkb770e882013-10-05 21:07:25 +0200809 bytes = simple_strtoul(argv[5], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000810 if (argc >= 7)
Wolfgang Denkb770e882013-10-05 21:07:25 +0200811 pos = simple_strtoul(argv[6], NULL, 16);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000812 else
813 pos = 0;
814
815 time = get_timer(0);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800816 ret = fs_write(filename, addr, pos, bytes, &len);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000817 time = get_timer(time);
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800818 if (ret < 0)
Simon Glassa8f6ab52013-04-20 08:42:50 +0000819 return 1;
820
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800821 printf("%llu bytes written in %lu ms", len, time);
Simon Glassa8f6ab52013-04-20 08:42:50 +0000822 if (time > 0) {
823 puts(" (");
Tom Rini9e374e72014-11-24 11:50:46 -0500824 print_size(div_u64(len, time) * 1000, "/s");
Simon Glassa8f6ab52013-04-20 08:42:50 +0000825 puts(")");
826 }
827 puts("\n");
828
829 return 0;
830}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100831
Simon Glass09140112020-05-10 11:40:03 -0600832int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
833 int fstype)
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100834{
835 int ret;
836 char uuid[37];
837 memset(uuid, 0, sizeof(uuid));
838
839 if (argc < 3 || argc > 4)
840 return CMD_RET_USAGE;
841
842 if (fs_set_blk_dev(argv[1], argv[2], fstype))
843 return 1;
844
845 ret = fs_uuid(uuid);
846 if (ret)
847 return CMD_RET_FAILURE;
848
849 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600850 env_set(argv[3], uuid);
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100851 else
852 printf("%s\n", uuid);
853
854 return CMD_RET_SUCCESS;
855}
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100856
Simon Glass09140112020-05-10 11:40:03 -0600857int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100858{
859 struct fstype_info *info;
860
861 if (argc < 3 || argc > 4)
862 return CMD_RET_USAGE;
863
864 if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY))
865 return 1;
866
867 info = fs_get_info(fs_type);
868
869 if (argc == 4)
Simon Glass382bee52017-08-03 12:22:09 -0600870 env_set(argv[3], info->name);
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100871 else
872 printf("%s\n", info->name);
873
Marek Vasute531c672019-02-06 13:19:29 +0100874 fs_close();
875
Sjoerd Simons1a1ad8e2015-01-05 18:13:36 +0100876 return CMD_RET_SUCCESS;
877}
878
Simon Glass09140112020-05-10 11:40:03 -0600879int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe2519da2018-09-11 15:59:13 +0900880 int fstype)
881{
882 if (argc != 4)
883 return CMD_RET_USAGE;
884
885 if (fs_set_blk_dev(argv[1], argv[2], fstype))
886 return 1;
887
888 if (fs_unlink(argv[3]))
889 return 1;
890
891 return 0;
892}
893
Simon Glass09140112020-05-10 11:40:03 -0600894int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900895 int fstype)
896{
897 int ret;
898
899 if (argc != 4)
900 return CMD_RET_USAGE;
901
902 if (fs_set_blk_dev(argv[1], argv[2], fstype))
903 return 1;
904
905 ret = fs_mkdir(argv[3]);
906 if (ret) {
Heinrich Schuchardt0d32d8c2020-08-25 17:49:16 +0000907 log_err("** Unable to create a directory \"%s\" **\n", argv[3]);
AKASHI Takahiroe7074cf2018-09-11 15:59:08 +0900908 return 1;
909 }
910
911 return 0;
912}
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100913
Simon Glass09140112020-05-10 11:40:03 -0600914int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
Jean-Jacques Hiblotaaa12152019-02-13 12:15:26 +0100915 int fstype)
916{
917 if (argc != 5)
918 return CMD_RET_USAGE;
919
920 if (fs_set_blk_dev(argv[1], argv[2], fstype))
921 return 1;
922
923 if (fs_ln(argv[3], argv[4]))
924 return 1;
925
926 return 0;
927}
Niel Fourie2280fa52020-03-24 16:17:04 +0100928
929int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
930{
931 struct fstype_info *drv = fstypes;
932 const int n_ents = ARRAY_SIZE(fstypes);
933 struct fstype_info *entry;
934 int i = 0;
935
936 puts("Supported filesystems");
937 for (entry = drv; entry != drv + n_ents; entry++) {
938 if (entry->fstype != FS_TYPE_ANY) {
939 printf("%c %s", i ? ',' : ':', entry->name);
940 i++;
941 }
942 }
943 if (!i)
944 puts(": <none>");
945 puts("\n");
946 return CMD_RET_SUCCESS;
947}