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