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