Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_CORE |
| 7 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 9 | #include <config.h> |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 10 | #include <errno.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <lmb.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Joe Hershberger | 65b905b | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 15 | #include <mapmem.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 16 | #include <part.h> |
| 17 | #include <ext4fs.h> |
| 18 | #include <fat.h> |
| 19 | #include <fs.h> |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 20 | #include <sandboxfs.h> |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 21 | #include <ubifs_uboot.h> |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 22 | #include <btrfs.h> |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 23 | #include <asm/io.h> |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 24 | #include <div64.h> |
| 25 | #include <linux/math64.h> |
Mian Yousaf Kaukab | 961ebe8 | 2019-06-18 15:03:44 +0200 | [diff] [blame] | 26 | #include <efi_loader.h> |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 27 | #include <squashfs.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 28 | |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 31 | static struct blk_desc *fs_dev_desc; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 32 | static int fs_dev_part; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 33 | static struct disk_partition fs_partition; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 34 | static int fs_type = FS_TYPE_ANY; |
| 35 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 36 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 37 | struct disk_partition *fs_partition) |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 38 | { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 39 | log_err("** Unrecognized filesystem type **\n"); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 40 | return -1; |
| 41 | } |
| 42 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 43 | static inline int fs_ls_unsupported(const char *dirname) |
| 44 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 45 | return -1; |
| 46 | } |
| 47 | |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 48 | /* generic implementation of ls in terms of opendir/readdir/closedir */ |
| 49 | __maybe_unused |
| 50 | static 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 Costa | 2a7fb15 | 2020-07-30 15:33:51 +0200 | [diff] [blame] | 64 | } else if (dent->type == FS_DT_LNK) { |
| 65 | printf(" <SYM> %s\n", dent->name); |
| 66 | nfiles++; |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 67 | } 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 Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 80 | static inline int fs_exists_unsupported(const char *filename) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
| 84 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 85 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 90 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 91 | loff_t offset, loff_t len, |
| 92 | loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 93 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| 96 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 97 | static inline int fs_write_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 98 | loff_t offset, loff_t len, |
| 99 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 100 | { |
| 101 | return -1; |
| 102 | } |
| 103 | |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 104 | static inline int fs_ln_unsupported(const char *filename, const char *target) |
| 105 | { |
| 106 | return -1; |
| 107 | } |
| 108 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 109 | static inline void fs_close_unsupported(void) |
| 110 | { |
| 111 | } |
| 112 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 113 | static inline int fs_uuid_unsupported(char *uuid_str) |
| 114 | { |
| 115 | return -1; |
| 116 | } |
| 117 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 118 | static inline int fs_opendir_unsupported(const char *filename, |
| 119 | struct fs_dir_stream **dirs) |
| 120 | { |
| 121 | return -EACCES; |
| 122 | } |
| 123 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 124 | static inline int fs_unlink_unsupported(const char *filename) |
| 125 | { |
| 126 | return -1; |
| 127 | } |
| 128 | |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 129 | static inline int fs_mkdir_unsupported(const char *dirname) |
| 130 | { |
| 131 | return -1; |
| 132 | } |
| 133 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 134 | struct fstype_info { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 135 | int fstype; |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 136 | char *name; |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 137 | /* |
| 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 Schuchardt | 99dc17d | 2018-08-11 15:52:14 +0200 | [diff] [blame] | 141 | * set to true. This should also be true for the dummy entry at the end |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 142 | * of fstypes[], since that is essentially a "virtual" (non-existent) |
| 143 | * filesystem. |
| 144 | */ |
| 145 | bool null_dev_desc_ok; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 146 | int (*probe)(struct blk_desc *fs_dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 147 | struct disk_partition *fs_partition); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 148 | int (*ls)(const char *dirname); |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 149 | int (*exists)(const char *filename); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 150 | 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 Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 155 | void (*close)(void); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 156 | int (*uuid)(char *uuid_str); |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 157 | /* |
| 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 Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 171 | int (*unlink)(const char *filename); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 172 | int (*mkdir)(const char *dirname); |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 173 | int (*ln)(const char *filename, const char *target); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static struct fstype_info fstypes[] = { |
| 177 | #ifdef CONFIG_FS_FAT |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 178 | { |
| 179 | .fstype = FS_TYPE_FAT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 180 | .name = "fat", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 181 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 182 | .probe = fat_set_blk_dev, |
| 183 | .close = fat_close, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 184 | .ls = fs_ls_generic, |
Stephen Warren | 1d2f9a0 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 185 | .exists = fat_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 186 | .size = fat_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 187 | .read = fat_read_file, |
Tien Fong Chee | 87fda0c | 2019-01-23 14:20:04 +0800 | [diff] [blame] | 188 | #if CONFIG_IS_ENABLED(FAT_WRITE) |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 189 | .write = file_fat_write, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 190 | .unlink = fat_unlink, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 191 | .mkdir = fat_mkdir, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 192 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 193 | .write = fs_write_unsupported, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 194 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 195 | .mkdir = fs_mkdir_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 196 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 197 | .uuid = fs_uuid_unsupported, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 198 | .opendir = fat_opendir, |
| 199 | .readdir = fat_readdir, |
| 200 | .closedir = fat_closedir, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 201 | .ln = fs_ln_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 202 | }, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 203 | #endif |
Tien Fong Chee | 64eab04 | 2019-01-23 14:20:06 +0800 | [diff] [blame] | 204 | |
| 205 | #if CONFIG_IS_ENABLED(FS_EXT4) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 206 | { |
| 207 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 208 | .name = "ext4", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 209 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 210 | .probe = ext4fs_probe, |
| 211 | .close = ext4fs_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 212 | .ls = ext4fs_ls, |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 213 | .exists = ext4fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 214 | .size = ext4fs_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 215 | .read = ext4_read_file, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 216 | #ifdef CONFIG_CMD_EXT4_WRITE |
| 217 | .write = ext4_write_file, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 218 | .ln = ext4fs_create_link, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 219 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 220 | .write = fs_write_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 221 | .ln = fs_ln_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 222 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 223 | .uuid = ext4fs_uuid, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 224 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 225 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 226 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 227 | }, |
| 228 | #endif |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 229 | #ifdef CONFIG_SANDBOX |
| 230 | { |
| 231 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 232 | .name = "sandbox", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 233 | .null_dev_desc_ok = true, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 234 | .probe = sandbox_fs_set_blk_dev, |
| 235 | .close = sandbox_fs_close, |
| 236 | .ls = sandbox_fs_ls, |
Stephen Warren | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 237 | .exists = sandbox_fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 238 | .size = sandbox_fs_size, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 239 | .read = fs_read_sandbox, |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 240 | .write = fs_write_sandbox, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 241 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 242 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 243 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 244 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 245 | .ln = fs_ln_unsupported, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 246 | }, |
| 247 | #endif |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 248 | #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 Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 261 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 262 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 263 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 264 | .ln = fs_ln_unsupported, |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 265 | }, |
| 266 | #endif |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 267 | #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Ășn | 90762d9 | 2017-10-06 16:56:07 +0200 | [diff] [blame] | 280 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 281 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 282 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 283 | .ln = fs_ln_unsupported, |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 284 | }, |
| 285 | #endif |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 286 | #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 Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 300 | { |
| 301 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 302 | .name = "unsupported", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 303 | .null_dev_desc_ok = true, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 304 | .probe = fs_probe_unsupported, |
| 305 | .close = fs_close_unsupported, |
| 306 | .ls = fs_ls_unsupported, |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 307 | .exists = fs_exists_unsupported, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 308 | .size = fs_size_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 309 | .read = fs_read_unsupported, |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 310 | .write = fs_write_unsupported, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 311 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 312 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 313 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 314 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 315 | .ln = fs_ln_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 316 | }, |
| 317 | }; |
| 318 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 319 | static 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 Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 333 | /** |
AKASHI Takahiro | 80ac734 | 2019-10-07 14:59:37 +0900 | [diff] [blame] | 334 | * 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 | */ |
| 341 | int fs_get_type(void) |
| 342 | { |
| 343 | return fs_type; |
| 344 | } |
| 345 | |
| 346 | /** |
Alex Kiernan | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 347 | * 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 | */ |
| 354 | const char *fs_get_type_name(void) |
| 355 | { |
| 356 | return fs_get_info(fs_type)->name; |
| 357 | } |
| 358 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 359 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 360 | { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 361 | struct fstype_info *info; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 362 | int part, i; |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 363 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 364 | static int relocated; |
| 365 | |
| 366 | if (!relocated) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 367 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 368 | i++, info++) { |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 369 | info->name += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 370 | info->probe += gd->reloc_off; |
| 371 | info->close += gd->reloc_off; |
| 372 | info->ls += gd->reloc_off; |
| 373 | info->read += gd->reloc_off; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 374 | info->write += gd->reloc_off; |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 375 | } |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 376 | relocated = 1; |
| 377 | } |
| 378 | #endif |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 379 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 380 | part = blk_get_device_part_str(ifname, dev_part_str, &fs_dev_desc, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 381 | &fs_partition, 1); |
| 382 | if (part < 0) |
| 383 | return -1; |
| 384 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 385 | 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 Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 388 | continue; |
| 389 | |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 390 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 391 | continue; |
| 392 | |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 393 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 394 | fs_type = info->fstype; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 395 | fs_dev_part = part; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | } |
| 399 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 400 | return -1; |
| 401 | } |
| 402 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 403 | /* set current blk device w/ blk_desc + partition # */ |
| 404 | int 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 Takahiro | 43efaa0 | 2018-10-17 16:32:02 +0900 | [diff] [blame] | 420 | fs_dev_part = part; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return -1; |
| 426 | } |
| 427 | |
AKASHI Takahiro | ca8e377 | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 428 | void fs_close(void) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 429 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 430 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 431 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 432 | info->close(); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 433 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 434 | fs_type = FS_TYPE_ANY; |
| 435 | } |
| 436 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 437 | int 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 Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 444 | int fs_ls(const char *dirname) |
| 445 | { |
| 446 | int ret; |
| 447 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 448 | struct fstype_info *info = fs_get_info(fs_type); |
| 449 | |
| 450 | ret = info->ls(dirname); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 451 | |
| 452 | fs_close(); |
| 453 | |
| 454 | return ret; |
| 455 | } |
| 456 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 457 | int 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 Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 470 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 471 | { |
| 472 | int ret; |
| 473 | |
| 474 | struct fstype_info *info = fs_get_info(fs_type); |
| 475 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 476 | ret = info->size(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 477 | |
| 478 | fs_close(); |
| 479 | |
| 480 | return ret; |
| 481 | } |
| 482 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 483 | #ifdef CONFIG_LMB |
| 484 | /* Check if a file may be read to the given address */ |
| 485 | static 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 Goldschmidt | 8890e7d | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 507 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 508 | lmb_dump_all(&lmb); |
| 509 | |
| 510 | if (lmb_alloc_addr(&lmb, addr, read_len) == addr) |
| 511 | return 0; |
| 512 | |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 513 | log_err("** Reading file would overwrite reserved memory **\n"); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 514 | return -ENOSPC; |
| 515 | } |
| 516 | #endif |
| 517 | |
| 518 | static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 519 | int do_lmb_check, loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 520 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 521 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 522 | void *buf; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 523 | int ret; |
| 524 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 525 | #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 Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 533 | /* |
| 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 Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 538 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 539 | unmap_sysmem(buf); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 540 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 541 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 56d9cdf | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 542 | if (ret == 0 && len && *actread != len) |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 543 | log_debug("** %s shorter than offset + len **\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 544 | fs_close(); |
| 545 | |
| 546 | return ret; |
| 547 | } |
| 548 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 549 | int 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 Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 555 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 556 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 557 | { |
| 558 | struct fstype_info *info = fs_get_info(fs_type); |
| 559 | void *buf; |
| 560 | int ret; |
| 561 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 562 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 563 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 564 | unmap_sysmem(buf); |
| 565 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 566 | if (ret < 0 && len != *actwrite) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 567 | log_err("** Unable to write file %s **\n", filename); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 568 | ret = -1; |
| 569 | } |
| 570 | fs_close(); |
| 571 | |
| 572 | return ret; |
| 573 | } |
| 574 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 575 | struct 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 | |
| 594 | struct 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 | |
| 613 | void 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 Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 627 | int 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 Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 635 | fs_close(); |
| 636 | |
| 637 | return ret; |
| 638 | } |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 639 | |
| 640 | int 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 Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 648 | fs_close(); |
| 649 | |
| 650 | return ret; |
| 651 | } |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 652 | |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 653 | int 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 Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 661 | log_err("** Unable to create link %s -> %s **\n", fname, target); |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 662 | ret = -1; |
| 663 | } |
| 664 | fs_close(); |
| 665 | |
| 666 | return ret; |
| 667 | } |
| 668 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 669 | int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 670 | int fstype) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 671 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 672 | loff_t size; |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 673 | |
| 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 Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 680 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 681 | return CMD_RET_FAILURE; |
| 682 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 683 | env_set_hex("filesize", size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 688 | int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 689 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 690 | { |
| 691 | unsigned long addr; |
| 692 | const char *addr_str; |
| 693 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 694 | loff_t bytes; |
| 695 | loff_t pos; |
| 696 | loff_t len_read; |
| 697 | int ret; |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 698 | unsigned long time; |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 699 | char *ep; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 700 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 701 | if (argc < 2) |
| 702 | return CMD_RET_USAGE; |
| 703 | if (argc > 7) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 704 | return CMD_RET_USAGE; |
| 705 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 706 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 707 | return 1; |
| 708 | |
| 709 | if (argc >= 4) { |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 710 | addr = simple_strtoul(argv[3], &ep, 16); |
| 711 | if (ep == argv[3] || *ep != '\0') |
| 712 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 713 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 714 | addr_str = env_get("loadaddr"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 715 | 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 Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 723 | filename = env_get("bootfile"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 724 | if (!filename) { |
| 725 | puts("** No boot file defined **\n"); |
| 726 | return 1; |
| 727 | } |
| 728 | } |
| 729 | if (argc >= 6) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 730 | bytes = simple_strtoul(argv[5], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 731 | else |
| 732 | bytes = 0; |
| 733 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 734 | pos = simple_strtoul(argv[6], NULL, 16); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 735 | else |
| 736 | pos = 0; |
| 737 | |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 738 | time = get_timer(0); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 739 | ret = _fs_read(filename, addr, pos, bytes, 1, &len_read); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 740 | time = get_timer(time); |
Heinrich Schuchardt | ca816ce | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 741 | if (ret < 0) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 742 | log_err("Failed to load '%s'\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 743 | return 1; |
Heinrich Schuchardt | ca816ce | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI)) |
| 747 | efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "", |
| 748 | (argc > 4) ? argv[4] : ""); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 749 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 750 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 751 | if (time > 0) { |
| 752 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 753 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 754 | puts(")"); |
| 755 | } |
| 756 | puts("\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 757 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 758 | env_set_hex("fileaddr", addr); |
| 759 | env_set_hex("filesize", len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 764 | int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 765 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 766 | { |
| 767 | if (argc < 2) |
| 768 | return CMD_RET_USAGE; |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 769 | if (argc > 4) |
| 770 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 771 | |
| 772 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 773 | return 1; |
| 774 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 775 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 776 | return 1; |
| 777 | |
| 778 | return 0; |
| 779 | } |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 780 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 781 | int 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 790 | int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 791 | int fstype) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 792 | { |
| 793 | unsigned long addr; |
| 794 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 795 | loff_t bytes; |
| 796 | loff_t pos; |
| 797 | loff_t len; |
| 798 | int ret; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 799 | 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 Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 807 | addr = simple_strtoul(argv[3], NULL, 16); |
| 808 | filename = argv[4]; |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 809 | bytes = simple_strtoul(argv[5], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 810 | if (argc >= 7) |
Wolfgang Denk | a10c7a5 | 2013-10-05 21:07:25 +0200 | [diff] [blame] | 811 | pos = simple_strtoul(argv[6], NULL, 16); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 812 | else |
| 813 | pos = 0; |
| 814 | |
| 815 | time = get_timer(0); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 816 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 817 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 818 | if (ret < 0) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 819 | return 1; |
| 820 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 821 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 822 | if (time > 0) { |
| 823 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 824 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 825 | puts(")"); |
| 826 | } |
| 827 | puts("\n"); |
| 828 | |
| 829 | return 0; |
| 830 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 831 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 832 | int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 833 | int fstype) |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 834 | { |
| 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 850 | env_set(argv[3], uuid); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 851 | else |
| 852 | printf("%s\n", uuid); |
| 853 | |
| 854 | return CMD_RET_SUCCESS; |
| 855 | } |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 856 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 857 | int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 858 | { |
| 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 Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 870 | env_set(argv[3], info->name); |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 871 | else |
| 872 | printf("%s\n", info->name); |
| 873 | |
Marek Vasut | 08d1c42 | 2019-02-06 13:19:29 +0100 | [diff] [blame] | 874 | fs_close(); |
| 875 | |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 876 | return CMD_RET_SUCCESS; |
| 877 | } |
| 878 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 879 | int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 880 | 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 894 | int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 895 | 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 Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 907 | log_err("** Unable to create a directory \"%s\" **\n", argv[3]); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 908 | return 1; |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 913 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 914 | int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 915 | 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 Fourie | 9252d70 | 2020-03-24 16:17:04 +0100 | [diff] [blame] | 928 | |
| 929 | int 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 | } |