Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved. |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_CORE |
| 7 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 9 | #include <config.h> |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 10 | #include <errno.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 11 | #include <common.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 13 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 15 | #include <mapmem.h> |
Stephen Warren | 045fa1e | 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 | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 20 | #include <sandboxfs.h> |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 21 | #include <ubifs_uboot.h> |
Marek BehĂșn | 0c936ee | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 22 | #include <btrfs.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 23 | #include <asm/global_data.h> |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 24 | #include <asm/io.h> |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 25 | #include <div64.h> |
| 26 | #include <linux/math64.h> |
Mian Yousaf Kaukab | ee88eac | 2019-06-18 15:03:44 +0200 | [diff] [blame] | 27 | #include <efi_loader.h> |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 28 | #include <squashfs.h> |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 29 | |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 32 | static struct blk_desc *fs_dev_desc; |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 33 | static int fs_dev_part; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 34 | static struct disk_partition fs_partition; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 35 | static int fs_type = FS_TYPE_ANY; |
| 36 | |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 37 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 38 | struct disk_partition *fs_partition) |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 39 | { |
Heinrich Schuchardt | 3bd0e2c | 2021-09-20 14:15:03 +0200 | [diff] [blame] | 40 | log_debug("Unrecognized filesystem type\n"); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 44 | static inline int fs_ls_unsupported(const char *dirname) |
| 45 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 46 | return -1; |
| 47 | } |
| 48 | |
Rob Clark | 89191d6 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 49 | /* generic implementation of ls in terms of opendir/readdir/closedir */ |
| 50 | __maybe_unused |
| 51 | static int fs_ls_generic(const char *dirname) |
| 52 | { |
| 53 | struct fs_dir_stream *dirs; |
| 54 | struct fs_dirent *dent; |
| 55 | int nfiles = 0, ndirs = 0; |
| 56 | |
| 57 | dirs = fs_opendir(dirname); |
| 58 | if (!dirs) |
| 59 | return -errno; |
| 60 | |
| 61 | while ((dent = fs_readdir(dirs))) { |
| 62 | if (dent->type == FS_DT_DIR) { |
| 63 | printf(" %s/\n", dent->name); |
| 64 | ndirs++; |
Joao Marcos Costa | 02c366b | 2020-07-30 15:33:51 +0200 | [diff] [blame] | 65 | } else if (dent->type == FS_DT_LNK) { |
| 66 | printf(" <SYM> %s\n", dent->name); |
| 67 | nfiles++; |
Rob Clark | 89191d6 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 68 | } else { |
| 69 | printf(" %8lld %s\n", dent->size, dent->name); |
| 70 | nfiles++; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | fs_closedir(dirs); |
| 75 | |
| 76 | printf("\n%d file(s), %d dir(s)\n\n", nfiles, ndirs); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 81 | static inline int fs_exists_unsupported(const char *filename) |
| 82 | { |
| 83 | return 0; |
| 84 | } |
| 85 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 86 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 87 | { |
| 88 | return -1; |
| 89 | } |
| 90 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 91 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 92 | loff_t offset, loff_t len, |
| 93 | loff_t *actread) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 94 | { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 98 | static inline int fs_write_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 99 | loff_t offset, loff_t len, |
| 100 | loff_t *actwrite) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 101 | { |
| 102 | return -1; |
| 103 | } |
| 104 | |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 105 | static inline int fs_ln_unsupported(const char *filename, const char *target) |
| 106 | { |
| 107 | return -1; |
| 108 | } |
| 109 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 110 | static inline void fs_close_unsupported(void) |
| 111 | { |
| 112 | } |
| 113 | |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 114 | static inline int fs_uuid_unsupported(char *uuid_str) |
| 115 | { |
| 116 | return -1; |
| 117 | } |
| 118 | |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 119 | static inline int fs_opendir_unsupported(const char *filename, |
| 120 | struct fs_dir_stream **dirs) |
| 121 | { |
| 122 | return -EACCES; |
| 123 | } |
| 124 | |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 125 | static inline int fs_unlink_unsupported(const char *filename) |
| 126 | { |
| 127 | return -1; |
| 128 | } |
| 129 | |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 130 | static inline int fs_mkdir_unsupported(const char *dirname) |
| 131 | { |
| 132 | return -1; |
| 133 | } |
| 134 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 135 | struct fstype_info { |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 136 | int fstype; |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 137 | char *name; |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 138 | /* |
| 139 | * Is it legal to pass NULL as .probe()'s fs_dev_desc parameter? This |
| 140 | * should be false in most cases. For "virtual" filesystems which |
| 141 | * aren't based on a U-Boot block device (e.g. sandbox), this can be |
Heinrich Schuchardt | ca230b0 | 2018-08-11 15:52:14 +0200 | [diff] [blame] | 142 | * set to true. This should also be true for the dummy entry at the end |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 143 | * of fstypes[], since that is essentially a "virtual" (non-existent) |
| 144 | * filesystem. |
| 145 | */ |
| 146 | bool null_dev_desc_ok; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 147 | int (*probe)(struct blk_desc *fs_dev_desc, |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 148 | struct disk_partition *fs_partition); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 149 | int (*ls)(const char *dirname); |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 150 | int (*exists)(const char *filename); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 151 | int (*size)(const char *filename, loff_t *size); |
| 152 | int (*read)(const char *filename, void *buf, loff_t offset, |
| 153 | loff_t len, loff_t *actread); |
| 154 | int (*write)(const char *filename, void *buf, loff_t offset, |
| 155 | loff_t len, loff_t *actwrite); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 156 | void (*close)(void); |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 157 | int (*uuid)(char *uuid_str); |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 158 | /* |
| 159 | * Open a directory stream. On success return 0 and directory |
| 160 | * stream pointer via 'dirsp'. On error, return -errno. See |
| 161 | * fs_opendir(). |
| 162 | */ |
| 163 | int (*opendir)(const char *filename, struct fs_dir_stream **dirsp); |
| 164 | /* |
| 165 | * Read next entry from directory stream. On success return 0 |
| 166 | * and directory entry pointer via 'dentp'. On error return |
| 167 | * -errno. See fs_readdir(). |
| 168 | */ |
| 169 | int (*readdir)(struct fs_dir_stream *dirs, struct fs_dirent **dentp); |
| 170 | /* see fs_closedir() */ |
| 171 | void (*closedir)(struct fs_dir_stream *dirs); |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 172 | int (*unlink)(const char *filename); |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 173 | int (*mkdir)(const char *dirname); |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 174 | int (*ln)(const char *filename, const char *target); |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | static struct fstype_info fstypes[] = { |
| 178 | #ifdef CONFIG_FS_FAT |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 179 | { |
| 180 | .fstype = FS_TYPE_FAT, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 181 | .name = "fat", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 182 | .null_dev_desc_ok = false, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 183 | .probe = fat_set_blk_dev, |
| 184 | .close = fat_close, |
Rob Clark | 89191d6 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 185 | .ls = fs_ls_generic, |
Stephen Warren | b7b5f31 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 186 | .exists = fat_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 187 | .size = fat_size, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 188 | .read = fat_read_file, |
Tien Fong Chee | d8c3ea9 | 2019-01-23 14:20:04 +0800 | [diff] [blame] | 189 | #if CONFIG_IS_ENABLED(FAT_WRITE) |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 190 | .write = file_fat_write, |
AKASHI Takahiro | f8240ce | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 191 | .unlink = fat_unlink, |
AKASHI Takahiro | 31a18d5 | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 192 | .mkdir = fat_mkdir, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 193 | #else |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 194 | .write = fs_write_unsupported, |
AKASHI Takahiro | f8240ce | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 195 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | 31a18d5 | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 196 | .mkdir = fs_mkdir_unsupported, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 197 | #endif |
Heinrich Schuchardt | c0029e4 | 2020-12-31 00:38:13 +0100 | [diff] [blame] | 198 | .uuid = fat_uuid, |
Rob Clark | 89191d6 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 199 | .opendir = fat_opendir, |
| 200 | .readdir = fat_readdir, |
| 201 | .closedir = fat_closedir, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 202 | .ln = fs_ln_unsupported, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 203 | }, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 204 | #endif |
Tien Fong Chee | cafc429 | 2019-01-23 14:20:06 +0800 | [diff] [blame] | 205 | |
| 206 | #if CONFIG_IS_ENABLED(FS_EXT4) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 207 | { |
| 208 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 209 | .name = "ext4", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 210 | .null_dev_desc_ok = false, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 211 | .probe = ext4fs_probe, |
| 212 | .close = ext4fs_close, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 213 | .ls = ext4fs_ls, |
Stephen Warren | 55af5c9 | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 214 | .exists = ext4fs_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 215 | .size = ext4fs_size, |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 216 | .read = ext4_read_file, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 217 | #ifdef CONFIG_CMD_EXT4_WRITE |
| 218 | .write = ext4_write_file, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 219 | .ln = ext4fs_create_link, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 220 | #else |
Stephen Warren | bd6fb31 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 221 | .write = fs_write_unsupported, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 222 | .ln = fs_ln_unsupported, |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 223 | #endif |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 224 | .uuid = ext4fs_uuid, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 225 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 226 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 227 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 228 | }, |
| 229 | #endif |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 230 | #ifdef CONFIG_SANDBOX |
| 231 | { |
| 232 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 233 | .name = "sandbox", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 234 | .null_dev_desc_ok = true, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 235 | .probe = sandbox_fs_set_blk_dev, |
| 236 | .close = sandbox_fs_close, |
| 237 | .ls = sandbox_fs_ls, |
Stephen Warren | 0a30aa1 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 238 | .exists = sandbox_fs_exists, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 239 | .size = sandbox_fs_size, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 240 | .read = fs_read_sandbox, |
Simon Glass | 7eb2c8d | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 241 | .write = fs_write_sandbox, |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 242 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 243 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 244 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 245 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 246 | .ln = fs_ln_unsupported, |
Simon Glass | 92ccc96 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 247 | }, |
| 248 | #endif |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 249 | #ifdef CONFIG_CMD_UBIFS |
| 250 | { |
| 251 | .fstype = FS_TYPE_UBIFS, |
| 252 | .name = "ubifs", |
| 253 | .null_dev_desc_ok = true, |
| 254 | .probe = ubifs_set_blk_dev, |
| 255 | .close = ubifs_close, |
| 256 | .ls = ubifs_ls, |
| 257 | .exists = ubifs_exists, |
| 258 | .size = ubifs_size, |
| 259 | .read = ubifs_read, |
| 260 | .write = fs_write_unsupported, |
| 261 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 262 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 263 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 264 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 265 | .ln = fs_ln_unsupported, |
Hans de Goede | 251cee0 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 266 | }, |
| 267 | #endif |
Marek BehĂșn | 0c936ee | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 268 | #ifdef CONFIG_FS_BTRFS |
| 269 | { |
| 270 | .fstype = FS_TYPE_BTRFS, |
| 271 | .name = "btrfs", |
| 272 | .null_dev_desc_ok = false, |
| 273 | .probe = btrfs_probe, |
| 274 | .close = btrfs_close, |
| 275 | .ls = btrfs_ls, |
| 276 | .exists = btrfs_exists, |
| 277 | .size = btrfs_size, |
| 278 | .read = btrfs_read, |
| 279 | .write = fs_write_unsupported, |
| 280 | .uuid = btrfs_uuid, |
Marek BehĂșn | 38fc683 | 2017-10-06 16:56:07 +0200 | [diff] [blame] | 281 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 282 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 283 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 284 | .ln = fs_ln_unsupported, |
Marek BehĂșn | 0c936ee | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 285 | }, |
| 286 | #endif |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 287 | #if IS_ENABLED(CONFIG_FS_SQUASHFS) |
| 288 | { |
| 289 | .fstype = FS_TYPE_SQUASHFS, |
| 290 | .name = "squashfs", |
Richard Genoud | 1b1e0c0 | 2020-11-03 12:10:59 +0100 | [diff] [blame] | 291 | .null_dev_desc_ok = false, |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 292 | .probe = sqfs_probe, |
| 293 | .opendir = sqfs_opendir, |
| 294 | .readdir = sqfs_readdir, |
| 295 | .ls = fs_ls_generic, |
| 296 | .read = sqfs_read, |
| 297 | .size = sqfs_size, |
| 298 | .close = sqfs_close, |
| 299 | .closedir = sqfs_closedir, |
Richard Genoud | dd4866b | 2020-11-03 12:11:26 +0100 | [diff] [blame] | 300 | .exists = sqfs_exists, |
Richard Genoud | 1b1e0c0 | 2020-11-03 12:10:59 +0100 | [diff] [blame] | 301 | .uuid = fs_uuid_unsupported, |
| 302 | .write = fs_write_unsupported, |
| 303 | .ln = fs_ln_unsupported, |
| 304 | .unlink = fs_unlink_unsupported, |
| 305 | .mkdir = fs_mkdir_unsupported, |
Joao Marcos Costa | c510061 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 306 | }, |
| 307 | #endif |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 308 | { |
| 309 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 310 | .name = "unsupported", |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 311 | .null_dev_desc_ok = true, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 312 | .probe = fs_probe_unsupported, |
| 313 | .close = fs_close_unsupported, |
| 314 | .ls = fs_ls_unsupported, |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 315 | .exists = fs_exists_unsupported, |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 316 | .size = fs_size_unsupported, |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 317 | .read = fs_read_unsupported, |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 318 | .write = fs_write_unsupported, |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 319 | .uuid = fs_uuid_unsupported, |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 320 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 321 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 322 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 323 | .ln = fs_ln_unsupported, |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 324 | }, |
| 325 | }; |
| 326 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 327 | static struct fstype_info *fs_get_info(int fstype) |
| 328 | { |
| 329 | struct fstype_info *info; |
| 330 | int i; |
| 331 | |
| 332 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes) - 1; i++, info++) { |
| 333 | if (fstype == info->fstype) |
| 334 | return info; |
| 335 | } |
| 336 | |
| 337 | /* Return the 'unsupported' sentinel */ |
| 338 | return info; |
| 339 | } |
| 340 | |
Alex Kiernan | 0d488e8 | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 341 | /** |
AKASHI Takahiro | b7cd956 | 2019-10-07 14:59:37 +0900 | [diff] [blame] | 342 | * fs_get_type() - Get type of current filesystem |
| 343 | * |
| 344 | * Return: filesystem type |
| 345 | * |
| 346 | * Returns filesystem type representing the current filesystem, or |
| 347 | * FS_TYPE_ANY for any unrecognised filesystem. |
| 348 | */ |
| 349 | int fs_get_type(void) |
| 350 | { |
| 351 | return fs_type; |
| 352 | } |
| 353 | |
| 354 | /** |
Alex Kiernan | 0d488e8 | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 355 | * fs_get_type_name() - Get type of current filesystem |
| 356 | * |
| 357 | * Return: Pointer to filesystem name |
| 358 | * |
| 359 | * Returns a string describing the current filesystem, or the sentinel |
| 360 | * "unsupported" for any unrecognised filesystem. |
| 361 | */ |
| 362 | const char *fs_get_type_name(void) |
| 363 | { |
| 364 | return fs_get_info(fs_type)->name; |
| 365 | } |
| 366 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 367 | int fs_set_blk_dev(const char *ifname, const char *dev_part_str, int fstype) |
| 368 | { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 369 | struct fstype_info *info; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 370 | int part, i; |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 371 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
| 372 | static int relocated; |
| 373 | |
| 374 | if (!relocated) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 375 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); |
| 376 | i++, info++) { |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 377 | info->name += gd->reloc_off; |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 378 | info->probe += gd->reloc_off; |
| 379 | info->close += gd->reloc_off; |
| 380 | info->ls += gd->reloc_off; |
| 381 | info->read += gd->reloc_off; |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 382 | info->write += gd->reloc_off; |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 383 | } |
Stephen Warren | a1b231c | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 384 | relocated = 1; |
| 385 | } |
| 386 | #endif |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 387 | |
Sean Anderson | 7194527 | 2021-04-12 18:53:05 -0400 | [diff] [blame] | 388 | part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc, |
| 389 | &fs_partition, 1); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 390 | if (part < 0) |
| 391 | return -1; |
| 392 | |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 393 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 394 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 395 | fstype != info->fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 396 | continue; |
| 397 | |
Stephen Warren | 377202b | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 398 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 399 | continue; |
| 400 | |
Simon Glass | 2ded0d4 | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 401 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 436e2b7 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 402 | fs_type = info->fstype; |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 403 | fs_dev_part = part; |
| 404 | return 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | return -1; |
| 409 | } |
| 410 | |
| 411 | /* set current blk device w/ blk_desc + partition # */ |
| 412 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) |
| 413 | { |
| 414 | struct fstype_info *info; |
| 415 | int ret, i; |
| 416 | |
| 417 | if (part >= 1) |
| 418 | ret = part_get_info(desc, part, &fs_partition); |
| 419 | else |
| 420 | ret = part_get_info_whole_disk(desc, &fs_partition); |
| 421 | if (ret) |
| 422 | return ret; |
| 423 | fs_dev_desc = desc; |
| 424 | |
| 425 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 426 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
| 427 | fs_type = info->fstype; |
AKASHI Takahiro | b0c78d8 | 2018-10-17 16:32:02 +0900 | [diff] [blame] | 428 | fs_dev_part = part; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | } |
| 432 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 433 | return -1; |
| 434 | } |
| 435 | |
AKASHI Takahiro | 64f49eb | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 436 | void fs_close(void) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 437 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 438 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 439 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 440 | info->close(); |
Simon Glass | e6d5241 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 441 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 442 | fs_type = FS_TYPE_ANY; |
| 443 | } |
| 444 | |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 445 | int fs_uuid(char *uuid_str) |
| 446 | { |
| 447 | struct fstype_info *info = fs_get_info(fs_type); |
| 448 | |
| 449 | return info->uuid(uuid_str); |
| 450 | } |
| 451 | |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 452 | int fs_ls(const char *dirname) |
| 453 | { |
| 454 | int ret; |
| 455 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 456 | struct fstype_info *info = fs_get_info(fs_type); |
| 457 | |
| 458 | ret = info->ls(dirname); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 459 | |
| 460 | fs_close(); |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 465 | int fs_exists(const char *filename) |
| 466 | { |
| 467 | int ret; |
| 468 | |
| 469 | struct fstype_info *info = fs_get_info(fs_type); |
| 470 | |
| 471 | ret = info->exists(filename); |
| 472 | |
| 473 | fs_close(); |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 478 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 479 | { |
| 480 | int ret; |
| 481 | |
| 482 | struct fstype_info *info = fs_get_info(fs_type); |
| 483 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 484 | ret = info->size(filename, size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 485 | |
| 486 | fs_close(); |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 491 | #ifdef CONFIG_LMB |
| 492 | /* Check if a file may be read to the given address */ |
| 493 | static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset, |
| 494 | loff_t len, struct fstype_info *info) |
| 495 | { |
| 496 | struct lmb lmb; |
| 497 | int ret; |
| 498 | loff_t size; |
| 499 | loff_t read_len; |
| 500 | |
| 501 | /* get the actual size of the file */ |
| 502 | ret = info->size(filename, &size); |
| 503 | if (ret) |
| 504 | return ret; |
| 505 | if (offset >= size) { |
| 506 | /* offset >= EOF, no bytes will be written */ |
| 507 | return 0; |
| 508 | } |
| 509 | read_len = size - offset; |
| 510 | |
| 511 | /* limit to 'len' if it is smaller */ |
| 512 | if (len && len < read_len) |
| 513 | read_len = len; |
| 514 | |
Simon Goldschmidt | 9cc2323 | 2019-01-26 22:13:04 +0100 | [diff] [blame] | 515 | lmb_init_and_reserve(&lmb, gd->bd, (void *)gd->fdt_blob); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 516 | lmb_dump_all(&lmb); |
| 517 | |
| 518 | if (lmb_alloc_addr(&lmb, addr, read_len) == addr) |
| 519 | return 0; |
| 520 | |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 521 | log_err("** Reading file would overwrite reserved memory **\n"); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 522 | return -ENOSPC; |
| 523 | } |
| 524 | #endif |
| 525 | |
| 526 | static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 527 | int do_lmb_check, loff_t *actread) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 528 | { |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 529 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 530 | void *buf; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 531 | int ret; |
| 532 | |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 533 | #ifdef CONFIG_LMB |
| 534 | if (do_lmb_check) { |
| 535 | ret = fs_read_lmb_check(filename, addr, offset, len, info); |
| 536 | if (ret) |
| 537 | return ret; |
| 538 | } |
| 539 | #endif |
| 540 | |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 541 | /* |
| 542 | * We don't actually know how many bytes are being read, since len==0 |
| 543 | * means read the whole file. |
| 544 | */ |
| 545 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 546 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | 117e050 | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 547 | unmap_sysmem(buf); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 548 | |
Simon Glass | c6f548d | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 549 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 7a3e70c | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 550 | if (ret == 0 && len && *actread != len) |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 551 | log_debug("** %s shorter than offset + len **\n", filename); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 552 | fs_close(); |
| 553 | |
| 554 | return ret; |
| 555 | } |
| 556 | |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 557 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 558 | loff_t *actread) |
| 559 | { |
| 560 | return _fs_read(filename, addr, offset, len, 0, actread); |
| 561 | } |
| 562 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 563 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 564 | loff_t *actwrite) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 565 | { |
| 566 | struct fstype_info *info = fs_get_info(fs_type); |
| 567 | void *buf; |
| 568 | int ret; |
| 569 | |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 570 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 571 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 572 | unmap_sysmem(buf); |
| 573 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 574 | if (ret < 0 && len != *actwrite) { |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 575 | log_err("** Unable to write file %s **\n", filename); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 576 | ret = -1; |
| 577 | } |
| 578 | fs_close(); |
| 579 | |
| 580 | return ret; |
| 581 | } |
| 582 | |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 583 | struct fs_dir_stream *fs_opendir(const char *filename) |
| 584 | { |
| 585 | struct fstype_info *info = fs_get_info(fs_type); |
| 586 | struct fs_dir_stream *dirs = NULL; |
| 587 | int ret; |
| 588 | |
| 589 | ret = info->opendir(filename, &dirs); |
| 590 | fs_close(); |
| 591 | if (ret) { |
| 592 | errno = -ret; |
| 593 | return NULL; |
| 594 | } |
| 595 | |
| 596 | dirs->desc = fs_dev_desc; |
| 597 | dirs->part = fs_dev_part; |
| 598 | |
| 599 | return dirs; |
| 600 | } |
| 601 | |
| 602 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) |
| 603 | { |
| 604 | struct fstype_info *info; |
| 605 | struct fs_dirent *dirent; |
| 606 | int ret; |
| 607 | |
| 608 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 609 | info = fs_get_info(fs_type); |
| 610 | |
| 611 | ret = info->readdir(dirs, &dirent); |
| 612 | fs_close(); |
| 613 | if (ret) { |
| 614 | errno = -ret; |
| 615 | return NULL; |
| 616 | } |
| 617 | |
| 618 | return dirent; |
| 619 | } |
| 620 | |
| 621 | void fs_closedir(struct fs_dir_stream *dirs) |
| 622 | { |
| 623 | struct fstype_info *info; |
| 624 | |
| 625 | if (!dirs) |
| 626 | return; |
| 627 | |
| 628 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 629 | info = fs_get_info(fs_type); |
| 630 | |
| 631 | info->closedir(dirs); |
| 632 | fs_close(); |
| 633 | } |
| 634 | |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 635 | int fs_unlink(const char *filename) |
| 636 | { |
| 637 | int ret; |
| 638 | |
| 639 | struct fstype_info *info = fs_get_info(fs_type); |
| 640 | |
| 641 | ret = info->unlink(filename); |
| 642 | |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 643 | fs_close(); |
| 644 | |
| 645 | return ret; |
| 646 | } |
Rob Clark | 4bbcc96 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 647 | |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 648 | int fs_mkdir(const char *dirname) |
| 649 | { |
| 650 | int ret; |
| 651 | |
| 652 | struct fstype_info *info = fs_get_info(fs_type); |
| 653 | |
| 654 | ret = info->mkdir(dirname); |
| 655 | |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 656 | fs_close(); |
| 657 | |
| 658 | return ret; |
| 659 | } |
| 660 | |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 661 | int fs_ln(const char *fname, const char *target) |
| 662 | { |
| 663 | struct fstype_info *info = fs_get_info(fs_type); |
| 664 | int ret; |
| 665 | |
| 666 | ret = info->ln(fname, target); |
| 667 | |
| 668 | if (ret < 0) { |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 669 | log_err("** Unable to create link %s -> %s **\n", fname, target); |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 670 | ret = -1; |
| 671 | } |
| 672 | fs_close(); |
| 673 | |
| 674 | return ret; |
| 675 | } |
| 676 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 677 | int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 678 | int fstype) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 679 | { |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 680 | loff_t size; |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 681 | |
| 682 | if (argc != 4) |
| 683 | return CMD_RET_USAGE; |
| 684 | |
| 685 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 686 | return 1; |
| 687 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 688 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 689 | return CMD_RET_FAILURE; |
| 690 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 691 | env_set_hex("filesize", size); |
Stephen Warren | cf65981 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 696 | int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 697 | int fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 698 | { |
| 699 | unsigned long addr; |
| 700 | const char *addr_str; |
| 701 | const char *filename; |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 702 | loff_t bytes; |
| 703 | loff_t pos; |
| 704 | loff_t len_read; |
| 705 | int ret; |
Andreas BieĂmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 706 | unsigned long time; |
Pavel Machek | 949bbd7 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 707 | char *ep; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 708 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 709 | if (argc < 2) |
| 710 | return CMD_RET_USAGE; |
| 711 | if (argc > 7) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 712 | return CMD_RET_USAGE; |
| 713 | |
Heinrich Schuchardt | ec9d19b | 2021-01-27 21:26:43 +0100 | [diff] [blame] | 714 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) { |
| 715 | log_err("Can't set block device\n"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 716 | return 1; |
Heinrich Schuchardt | ec9d19b | 2021-01-27 21:26:43 +0100 | [diff] [blame] | 717 | } |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 718 | |
| 719 | if (argc >= 4) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 720 | addr = hextoul(argv[3], &ep); |
Pavel Machek | 949bbd7 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 721 | if (ep == argv[3] || *ep != '\0') |
| 722 | return CMD_RET_USAGE; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 723 | } else { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 724 | addr_str = env_get("loadaddr"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 725 | if (addr_str != NULL) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 726 | addr = hextoul(addr_str, NULL); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 727 | else |
| 728 | addr = CONFIG_SYS_LOAD_ADDR; |
| 729 | } |
| 730 | if (argc >= 5) { |
| 731 | filename = argv[4]; |
| 732 | } else { |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 733 | filename = env_get("bootfile"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 734 | if (!filename) { |
| 735 | puts("** No boot file defined **\n"); |
| 736 | return 1; |
| 737 | } |
| 738 | } |
| 739 | if (argc >= 6) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 740 | bytes = hextoul(argv[5], NULL); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 741 | else |
| 742 | bytes = 0; |
| 743 | if (argc >= 7) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 744 | pos = hextoul(argv[6], NULL); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 745 | else |
| 746 | pos = 0; |
| 747 | |
Andreas BieĂmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 748 | time = get_timer(0); |
Simon Goldschmidt | aa3c609 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 749 | ret = _fs_read(filename, addr, pos, bytes, 1, &len_read); |
Andreas BieĂmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 750 | time = get_timer(time); |
Heinrich Schuchardt | 1244f36 | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 751 | if (ret < 0) { |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 752 | log_err("Failed to load '%s'\n", filename); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 753 | return 1; |
Heinrich Schuchardt | 1244f36 | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | if (IS_ENABLED(CONFIG_CMD_BOOTEFI)) |
| 757 | efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "", |
Heinrich Schuchardt | 5f59518 | 2021-01-12 12:46:24 +0100 | [diff] [blame] | 758 | (argc > 4) ? argv[4] : "", map_sysmem(addr, 0), |
| 759 | len_read); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 760 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 761 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas BieĂmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 762 | if (time > 0) { |
| 763 | puts(" ("); |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 764 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas BieĂmann | da1fd96 | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 765 | puts(")"); |
| 766 | } |
| 767 | puts("\n"); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 768 | |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 769 | env_set_hex("fileaddr", addr); |
| 770 | env_set_hex("filesize", len_read); |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 771 | |
| 772 | return 0; |
| 773 | } |
| 774 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 775 | int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 776 | int fstype) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 777 | { |
| 778 | if (argc < 2) |
| 779 | return CMD_RET_USAGE; |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 780 | if (argc > 4) |
| 781 | return CMD_RET_USAGE; |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 782 | |
| 783 | if (fs_set_blk_dev(argv[1], (argc >= 3) ? argv[2] : NULL, fstype)) |
| 784 | return 1; |
| 785 | |
Stephen Warren | e9b0f99 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 786 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | 045fa1e | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 787 | return 1; |
| 788 | |
| 789 | return 0; |
| 790 | } |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 791 | |
Stephen Warren | 6152916 | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 792 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 793 | int fstype) |
| 794 | { |
| 795 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) |
| 796 | return 0; |
| 797 | |
| 798 | return fs_exists(file); |
| 799 | } |
| 800 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 801 | int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 802 | int fstype) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 803 | { |
| 804 | unsigned long addr; |
| 805 | const char *filename; |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 806 | loff_t bytes; |
| 807 | loff_t pos; |
| 808 | loff_t len; |
| 809 | int ret; |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 810 | unsigned long time; |
| 811 | |
| 812 | if (argc < 6 || argc > 7) |
| 813 | return CMD_RET_USAGE; |
| 814 | |
| 815 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 816 | return 1; |
| 817 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 818 | addr = hextoul(argv[3], NULL); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 819 | filename = argv[4]; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 820 | bytes = hextoul(argv[5], NULL); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 821 | if (argc >= 7) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 822 | pos = hextoul(argv[6], NULL); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 823 | else |
| 824 | pos = 0; |
| 825 | |
| 826 | time = get_timer(0); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 827 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 828 | time = get_timer(time); |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 829 | if (ret < 0) |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 830 | return 1; |
| 831 | |
Suriyan Ramasami | d455d87 | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 832 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 833 | if (time > 0) { |
| 834 | puts(" ("); |
Tom Rini | 9e374e7 | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 835 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | a8f6ab5 | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 836 | puts(")"); |
| 837 | } |
| 838 | puts("\n"); |
| 839 | |
| 840 | return 0; |
| 841 | } |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 842 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 843 | int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 844 | int fstype) |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 845 | { |
| 846 | int ret; |
| 847 | char uuid[37]; |
| 848 | memset(uuid, 0, sizeof(uuid)); |
| 849 | |
| 850 | if (argc < 3 || argc > 4) |
| 851 | return CMD_RET_USAGE; |
| 852 | |
| 853 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 854 | return 1; |
| 855 | |
| 856 | ret = fs_uuid(uuid); |
| 857 | if (ret) |
| 858 | return CMD_RET_FAILURE; |
| 859 | |
| 860 | if (argc == 4) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 861 | env_set(argv[3], uuid); |
Christian Gmeiner | 59e890e | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 862 | else |
| 863 | printf("%s\n", uuid); |
| 864 | |
| 865 | return CMD_RET_SUCCESS; |
| 866 | } |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 867 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 868 | int do_fs_type(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 869 | { |
| 870 | struct fstype_info *info; |
| 871 | |
| 872 | if (argc < 3 || argc > 4) |
| 873 | return CMD_RET_USAGE; |
| 874 | |
| 875 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 876 | return 1; |
| 877 | |
| 878 | info = fs_get_info(fs_type); |
| 879 | |
| 880 | if (argc == 4) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 881 | env_set(argv[3], info->name); |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 882 | else |
| 883 | printf("%s\n", info->name); |
| 884 | |
Marek Vasut | e531c67 | 2019-02-06 13:19:29 +0100 | [diff] [blame] | 885 | fs_close(); |
| 886 | |
Sjoerd Simons | 1a1ad8e | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 887 | return CMD_RET_SUCCESS; |
| 888 | } |
| 889 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 890 | int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
AKASHI Takahiro | e2519da | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 891 | int fstype) |
| 892 | { |
| 893 | if (argc != 4) |
| 894 | return CMD_RET_USAGE; |
| 895 | |
| 896 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 897 | return 1; |
| 898 | |
| 899 | if (fs_unlink(argv[3])) |
| 900 | return 1; |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 905 | int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 906 | int fstype) |
| 907 | { |
| 908 | int ret; |
| 909 | |
| 910 | if (argc != 4) |
| 911 | return CMD_RET_USAGE; |
| 912 | |
| 913 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 914 | return 1; |
| 915 | |
| 916 | ret = fs_mkdir(argv[3]); |
| 917 | if (ret) { |
Heinrich Schuchardt | 0d32d8c | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 918 | log_err("** Unable to create a directory \"%s\" **\n", argv[3]); |
AKASHI Takahiro | e7074cf | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 919 | return 1; |
| 920 | } |
| 921 | |
| 922 | return 0; |
| 923 | } |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 924 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 925 | int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
Jean-Jacques Hiblot | aaa1215 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 926 | int fstype) |
| 927 | { |
| 928 | if (argc != 5) |
| 929 | return CMD_RET_USAGE; |
| 930 | |
| 931 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 932 | return 1; |
| 933 | |
| 934 | if (fs_ln(argv[3], argv[4])) |
| 935 | return 1; |
| 936 | |
| 937 | return 0; |
| 938 | } |
Niel Fourie | 2280fa5 | 2020-03-24 16:17:04 +0100 | [diff] [blame] | 939 | |
| 940 | int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) |
| 941 | { |
| 942 | struct fstype_info *drv = fstypes; |
| 943 | const int n_ents = ARRAY_SIZE(fstypes); |
| 944 | struct fstype_info *entry; |
| 945 | int i = 0; |
| 946 | |
| 947 | puts("Supported filesystems"); |
| 948 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 949 | if (entry->fstype != FS_TYPE_ANY) { |
| 950 | printf("%c %s", i ? ',' : ':', entry->name); |
| 951 | i++; |
| 952 | } |
| 953 | } |
| 954 | if (!i) |
| 955 | puts(": <none>"); |
| 956 | puts("\n"); |
| 957 | return CMD_RET_SUCCESS; |
| 958 | } |