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