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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 26 | #include <asm/global_data.h> |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 27 | #include <asm/io.h> |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 28 | #include <div64.h> |
| 29 | #include <linux/math64.h> |
Simon Glass | e6c49fc | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 30 | #include <linux/sizes.h> |
Mian Yousaf Kaukab | 961ebe8 | 2019-06-18 15:03:44 +0200 | [diff] [blame] | 31 | #include <efi_loader.h> |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 32 | #include <squashfs.h> |
Huang Jianan | 024fb2f | 2022-02-26 15:05:47 +0800 | [diff] [blame] | 33 | #include <erofs.h> |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 34 | |
Stephen Warren | 44161af | 2012-10-30 07:50:47 +0000 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 37 | static struct blk_desc *fs_dev_desc; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 38 | static int fs_dev_part; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 39 | static struct disk_partition fs_partition; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 40 | static int fs_type = FS_TYPE_ANY; |
| 41 | |
Simon Glass | 896ed4b | 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 | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 47 | static inline int fs_probe_unsupported(struct blk_desc *fs_dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 48 | struct disk_partition *fs_partition) |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 49 | { |
Heinrich Schuchardt | 3f006d5 | 2021-09-20 14:15:03 +0200 | [diff] [blame] | 50 | log_debug("Unrecognized filesystem type\n"); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 51 | return -1; |
| 52 | } |
| 53 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 54 | static inline int fs_ls_unsupported(const char *dirname) |
| 55 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 56 | return -1; |
| 57 | } |
| 58 | |
Rob Clark | 60d74a9 | 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 | 2a7fb15 | 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 | 60d74a9 | 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 | d008fbb | 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 | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 96 | static inline int fs_size_unsupported(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 97 | { |
| 98 | return -1; |
| 99 | } |
| 100 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 101 | static inline int fs_read_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 102 | loff_t offset, loff_t len, |
| 103 | loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 104 | { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 105 | return -1; |
| 106 | } |
| 107 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 108 | static inline int fs_write_unsupported(const char *filename, void *buf, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 109 | loff_t offset, loff_t len, |
| 110 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 111 | { |
| 112 | return -1; |
| 113 | } |
| 114 | |
Jean-Jacques Hiblot | bc23748 | 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 | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 120 | static inline void fs_close_unsupported(void) |
| 121 | { |
| 122 | } |
| 123 | |
Christian Gmeiner | 9f9eec3 | 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 | 2b7bfd9 | 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 | 1ba4253 | 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 | adc8c9f | 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 | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 145 | struct fstype_info { |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 146 | int fstype; |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 147 | char *name; |
Stephen Warren | ec63d4a | 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 | 99dc17d | 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 | ec63d4a | 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 | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 157 | int (*probe)(struct blk_desc *fs_dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 158 | struct disk_partition *fs_partition); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 159 | int (*ls)(const char *dirname); |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 160 | int (*exists)(const char *filename); |
Suriyan Ramasami | 96171fb | 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 | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 166 | void (*close)(void); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 167 | int (*uuid)(char *uuid_str); |
Rob Clark | 2b7bfd9 | 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 | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 182 | int (*unlink)(const char *filename); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 183 | int (*mkdir)(const char *dirname); |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 184 | int (*ln)(const char *filename, const char *target); |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | static struct fstype_info fstypes[] = { |
Pali RohĂĄr | d291f89 | 2022-04-05 15:48:32 +0200 | [diff] [blame] | 188 | #if CONFIG_IS_ENABLED(FS_FAT) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 189 | { |
| 190 | .fstype = FS_TYPE_FAT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 191 | .name = "fat", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 192 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 193 | .probe = fat_set_blk_dev, |
| 194 | .close = fat_close, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 195 | .ls = fs_ls_generic, |
Stephen Warren | 1d2f9a0 | 2014-02-03 13:21:10 -0700 | [diff] [blame] | 196 | .exists = fat_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 197 | .size = fat_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 198 | .read = fat_read_file, |
Tien Fong Chee | 87fda0c | 2019-01-23 14:20:04 +0800 | [diff] [blame] | 199 | #if CONFIG_IS_ENABLED(FAT_WRITE) |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 200 | .write = file_fat_write, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 201 | .unlink = fat_unlink, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 202 | .mkdir = fat_mkdir, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 203 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 204 | .write = fs_write_unsupported, |
AKASHI Takahiro | 73b3497 | 2018-09-11 15:59:14 +0900 | [diff] [blame] | 205 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | 1c24b7b | 2018-09-11 15:59:10 +0900 | [diff] [blame] | 206 | .mkdir = fs_mkdir_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 207 | #endif |
Heinrich Schuchardt | fac9183 | 2020-12-31 00:38:13 +0100 | [diff] [blame] | 208 | .uuid = fat_uuid, |
Rob Clark | 60d74a9 | 2017-09-09 13:15:58 -0400 | [diff] [blame] | 209 | .opendir = fat_opendir, |
| 210 | .readdir = fat_readdir, |
| 211 | .closedir = fat_closedir, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 212 | .ln = fs_ln_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 213 | }, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 214 | #endif |
Tien Fong Chee | 64eab04 | 2019-01-23 14:20:06 +0800 | [diff] [blame] | 215 | |
| 216 | #if CONFIG_IS_ENABLED(FS_EXT4) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 217 | { |
| 218 | .fstype = FS_TYPE_EXT, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 219 | .name = "ext4", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 220 | .null_dev_desc_ok = false, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 221 | .probe = ext4fs_probe, |
| 222 | .close = ext4fs_close, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 223 | .ls = ext4fs_ls, |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 224 | .exists = ext4fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 225 | .size = ext4fs_size, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 226 | .read = ext4_read_file, |
Baruch Siach | aacc5c5 | 2024-05-15 07:37:16 +0300 | [diff] [blame] | 227 | #ifdef CONFIG_EXT4_WRITE |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 228 | .write = ext4_write_file, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 229 | .ln = ext4fs_create_link, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 230 | #else |
Stephen Warren | 9df2580 | 2014-02-03 13:20:59 -0700 | [diff] [blame] | 231 | .write = fs_write_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 232 | .ln = fs_ln_unsupported, |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 233 | #endif |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 234 | .uuid = ext4fs_uuid, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 235 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 236 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 237 | .mkdir = fs_mkdir_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 238 | }, |
| 239 | #endif |
Sean Anderson | 14fa1bd | 2023-10-14 16:47:48 -0400 | [diff] [blame] | 240 | #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_SPL_BUILD) |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 241 | { |
| 242 | .fstype = FS_TYPE_SANDBOX, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 243 | .name = "sandbox", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 244 | .null_dev_desc_ok = true, |
Simon Glass | 1184287 | 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 | 97d66f2 | 2014-02-03 13:21:07 -0700 | [diff] [blame] | 248 | .exists = sandbox_fs_exists, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 249 | .size = sandbox_fs_size, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 250 | .read = fs_read_sandbox, |
Simon Glass | ea307e8 | 2013-04-20 08:42:51 +0000 | [diff] [blame] | 251 | .write = fs_write_sandbox, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 252 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 253 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 254 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 255 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 256 | .ln = fs_ln_unsupported, |
Simon Glass | 1184287 | 2012-12-26 09:53:35 +0000 | [diff] [blame] | 257 | }, |
| 258 | #endif |
Mayuresh Chitale | 8fd5184 | 2023-11-16 22:46:12 +0530 | [diff] [blame] | 259 | #if CONFIG_IS_ENABLED(SEMIHOSTING) |
Sean Anderson | c678b32 | 2022-03-22 16:59:20 -0400 | [diff] [blame] | 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 | d3a3c12 | 2022-04-05 15:49:24 +0200 | [diff] [blame] | 278 | #ifndef CONFIG_SPL_BUILD |
Hans de Goede | 690c796 | 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 | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 292 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 293 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 294 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 295 | .ln = fs_ln_unsupported, |
Hans de Goede | 690c796 | 2015-09-17 18:46:58 -0400 | [diff] [blame] | 296 | }, |
| 297 | #endif |
Pali RohĂĄr | d3a3c12 | 2022-04-05 15:49:24 +0200 | [diff] [blame] | 298 | #endif |
Pali RohĂĄr | 849a61a | 2022-04-07 14:53:25 +0200 | [diff] [blame] | 299 | #ifndef CONFIG_SPL_BUILD |
Marek BehĂșn | 98ec1d1 | 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 | 90762d9 | 2017-10-06 16:56:07 +0200 | [diff] [blame] | 313 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 314 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 315 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 316 | .ln = fs_ln_unsupported, |
Marek BehĂșn | 98ec1d1 | 2017-09-03 17:00:29 +0200 | [diff] [blame] | 317 | }, |
| 318 | #endif |
Pali RohĂĄr | 849a61a | 2022-04-07 14:53:25 +0200 | [diff] [blame] | 319 | #endif |
Pali RohĂĄr | 1660e96 | 2022-04-06 23:34:00 +0200 | [diff] [blame] | 320 | #if CONFIG_IS_ENABLED(FS_SQUASHFS) |
Joao Marcos Costa | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 321 | { |
| 322 | .fstype = FS_TYPE_SQUASHFS, |
| 323 | .name = "squashfs", |
Richard Genoud | 654b248 | 2020-11-03 12:10:59 +0100 | [diff] [blame] | 324 | .null_dev_desc_ok = false, |
Joao Marcos Costa | 29da374 | 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 | cfca20c | 2020-11-03 12:11:26 +0100 | [diff] [blame] | 333 | .exists = sqfs_exists, |
Richard Genoud | 654b248 | 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 | 29da374 | 2020-07-30 15:33:47 +0200 | [diff] [blame] | 339 | }, |
| 340 | #endif |
Huang Jianan | 024fb2f | 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 | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 362 | { |
| 363 | .fstype = FS_TYPE_ANY, |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 364 | .name = "unsupported", |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 365 | .null_dev_desc_ok = true, |
Simon Glass | 1aede48 | 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 | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 369 | .exists = fs_exists_unsupported, |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 370 | .size = fs_size_unsupported, |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 371 | .read = fs_read_unsupported, |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 372 | .write = fs_write_unsupported, |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 373 | .uuid = fs_uuid_unsupported, |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 374 | .opendir = fs_opendir_unsupported, |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 375 | .unlink = fs_unlink_unsupported, |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 376 | .mkdir = fs_mkdir_unsupported, |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 377 | .ln = fs_ln_unsupported, |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 378 | }, |
| 379 | }; |
| 380 | |
Simon Glass | e6aad85 | 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 | 3a6163a | 2018-05-29 15:30:50 +0000 | [diff] [blame] | 395 | /** |
AKASHI Takahiro | 80ac734 | 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 | 3a6163a | 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 | eefbc3f | 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 | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 423 | struct fstype_info *info; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 424 | int part, i; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 425 | |
Sean Anderson | f476b4a | 2021-04-12 18:53:05 -0400 | [diff] [blame] | 426 | part = part_get_info_by_dev_and_name_or_num(ifname, dev_part_str, &fs_dev_desc, |
| 427 | &fs_partition, 1); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 428 | if (part < 0) |
| 429 | return -1; |
| 430 | |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 431 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 432 | if (fstype != FS_TYPE_ANY && info->fstype != FS_TYPE_ANY && |
| 433 | fstype != info->fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 434 | continue; |
| 435 | |
Stephen Warren | ec63d4a | 2014-02-03 13:21:01 -0700 | [diff] [blame] | 436 | if (!fs_dev_desc && !info->null_dev_desc_ok) |
| 437 | continue; |
| 438 | |
Simon Glass | 6a46e2f | 2012-12-26 09:53:31 +0000 | [diff] [blame] | 439 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
Simon Glass | 1aede48 | 2012-12-26 09:53:29 +0000 | [diff] [blame] | 440 | fs_type = info->fstype; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 441 | fs_dev_part = part; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | } |
| 445 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 446 | return -1; |
| 447 | } |
| 448 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 449 | /* set current blk device w/ blk_desc + partition # */ |
| 450 | int fs_set_blk_dev_with_part(struct blk_desc *desc, int part) |
| 451 | { |
| 452 | struct fstype_info *info; |
| 453 | int ret, i; |
| 454 | |
| 455 | if (part >= 1) |
| 456 | ret = part_get_info(desc, part, &fs_partition); |
| 457 | else |
| 458 | ret = part_get_info_whole_disk(desc, &fs_partition); |
| 459 | if (ret) |
| 460 | return ret; |
| 461 | fs_dev_desc = desc; |
| 462 | |
| 463 | for (i = 0, info = fstypes; i < ARRAY_SIZE(fstypes); i++, info++) { |
| 464 | if (!info->probe(fs_dev_desc, &fs_partition)) { |
| 465 | fs_type = info->fstype; |
AKASHI Takahiro | 43efaa0 | 2018-10-17 16:32:02 +0900 | [diff] [blame] | 466 | fs_dev_part = part; |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return -1; |
| 472 | } |
| 473 | |
AKASHI Takahiro | ca8e377 | 2019-10-07 14:59:35 +0900 | [diff] [blame] | 474 | void fs_close(void) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 475 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 476 | struct fstype_info *info = fs_get_info(fs_type); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 477 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 478 | info->close(); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 479 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 480 | fs_type = FS_TYPE_ANY; |
| 481 | } |
| 482 | |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 483 | int fs_uuid(char *uuid_str) |
| 484 | { |
| 485 | struct fstype_info *info = fs_get_info(fs_type); |
| 486 | |
| 487 | return info->uuid(uuid_str); |
| 488 | } |
| 489 | |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 490 | int fs_ls(const char *dirname) |
| 491 | { |
| 492 | int ret; |
| 493 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 494 | struct fstype_info *info = fs_get_info(fs_type); |
| 495 | |
| 496 | ret = info->ls(dirname); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 497 | |
| 498 | fs_close(); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 503 | int fs_exists(const char *filename) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | struct fstype_info *info = fs_get_info(fs_type); |
| 508 | |
| 509 | ret = info->exists(filename); |
| 510 | |
| 511 | fs_close(); |
| 512 | |
| 513 | return ret; |
| 514 | } |
| 515 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 516 | int fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 517 | { |
| 518 | int ret; |
| 519 | |
| 520 | struct fstype_info *info = fs_get_info(fs_type); |
| 521 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 522 | ret = info->size(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 523 | |
| 524 | fs_close(); |
| 525 | |
| 526 | return ret; |
| 527 | } |
| 528 | |
Sughosh Ganu | 0d733f0 | 2024-08-26 17:29:22 +0530 | [diff] [blame] | 529 | #if CONFIG_IS_ENABLED(LMB) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 530 | /* Check if a file may be read to the given address */ |
| 531 | static int fs_read_lmb_check(const char *filename, ulong addr, loff_t offset, |
| 532 | loff_t len, struct fstype_info *info) |
| 533 | { |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 534 | int ret; |
| 535 | loff_t size; |
| 536 | loff_t read_len; |
| 537 | |
| 538 | /* get the actual size of the file */ |
| 539 | ret = info->size(filename, &size); |
| 540 | if (ret) |
| 541 | return ret; |
| 542 | if (offset >= size) { |
| 543 | /* offset >= EOF, no bytes will be written */ |
| 544 | return 0; |
| 545 | } |
| 546 | read_len = size - offset; |
| 547 | |
| 548 | /* limit to 'len' if it is smaller */ |
| 549 | if (len && len < read_len) |
| 550 | read_len = len; |
| 551 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 552 | lmb_dump_all(); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 553 | |
Sughosh Ganu | 291bf9c | 2024-08-26 17:29:18 +0530 | [diff] [blame] | 554 | if (lmb_alloc_addr(addr, read_len) == addr) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 555 | return 0; |
| 556 | |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 557 | log_err("** Reading file would overwrite reserved memory **\n"); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 558 | return -ENOSPC; |
| 559 | } |
| 560 | #endif |
| 561 | |
| 562 | static int _fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 563 | int do_lmb_check, loff_t *actread) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 564 | { |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 565 | struct fstype_info *info = fs_get_info(fs_type); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 566 | void *buf; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 567 | int ret; |
| 568 | |
Sughosh Ganu | 0d733f0 | 2024-08-26 17:29:22 +0530 | [diff] [blame] | 569 | #if CONFIG_IS_ENABLED(LMB) |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 570 | if (do_lmb_check) { |
| 571 | ret = fs_read_lmb_check(filename, addr, offset, len, info); |
| 572 | if (ret) |
| 573 | return ret; |
| 574 | } |
| 575 | #endif |
| 576 | |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 577 | /* |
| 578 | * We don't actually know how many bytes are being read, since len==0 |
| 579 | * means read the whole file. |
| 580 | */ |
| 581 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 582 | ret = info->read(filename, buf, offset, len, actread); |
Simon Glass | cbe5d5d | 2012-12-26 09:53:32 +0000 | [diff] [blame] | 583 | unmap_sysmem(buf); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 584 | |
Simon Glass | e6aad85 | 2012-12-26 09:53:30 +0000 | [diff] [blame] | 585 | /* If we requested a specific number of bytes, check we got it */ |
Max Krummenacher | 56d9cdf | 2015-08-05 17:16:58 +0200 | [diff] [blame] | 586 | if (ret == 0 && len && *actread != len) |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 587 | log_debug("** %s shorter than offset + len **\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 588 | fs_close(); |
| 589 | |
| 590 | return ret; |
| 591 | } |
| 592 | |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 593 | int fs_read(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 594 | loff_t *actread) |
| 595 | { |
| 596 | return _fs_read(filename, addr, offset, len, 0, actread); |
| 597 | } |
| 598 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 599 | int fs_write(const char *filename, ulong addr, loff_t offset, loff_t len, |
| 600 | loff_t *actwrite) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 601 | { |
| 602 | struct fstype_info *info = fs_get_info(fs_type); |
| 603 | void *buf; |
| 604 | int ret; |
| 605 | |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 606 | buf = map_sysmem(addr, len); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 607 | ret = info->write(filename, buf, offset, len, actwrite); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 608 | unmap_sysmem(buf); |
| 609 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 610 | if (ret < 0 && len != *actwrite) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 611 | log_err("** Unable to write file %s **\n", filename); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 612 | ret = -1; |
| 613 | } |
| 614 | fs_close(); |
| 615 | |
| 616 | return ret; |
| 617 | } |
| 618 | |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 619 | struct fs_dir_stream *fs_opendir(const char *filename) |
| 620 | { |
| 621 | struct fstype_info *info = fs_get_info(fs_type); |
| 622 | struct fs_dir_stream *dirs = NULL; |
| 623 | int ret; |
| 624 | |
| 625 | ret = info->opendir(filename, &dirs); |
| 626 | fs_close(); |
| 627 | if (ret) { |
| 628 | errno = -ret; |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | dirs->desc = fs_dev_desc; |
| 633 | dirs->part = fs_dev_part; |
| 634 | |
| 635 | return dirs; |
| 636 | } |
| 637 | |
| 638 | struct fs_dirent *fs_readdir(struct fs_dir_stream *dirs) |
| 639 | { |
| 640 | struct fstype_info *info; |
| 641 | struct fs_dirent *dirent; |
| 642 | int ret; |
| 643 | |
| 644 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 645 | info = fs_get_info(fs_type); |
| 646 | |
| 647 | ret = info->readdir(dirs, &dirent); |
| 648 | fs_close(); |
| 649 | if (ret) { |
| 650 | errno = -ret; |
| 651 | return NULL; |
| 652 | } |
| 653 | |
| 654 | return dirent; |
| 655 | } |
| 656 | |
| 657 | void fs_closedir(struct fs_dir_stream *dirs) |
| 658 | { |
| 659 | struct fstype_info *info; |
| 660 | |
| 661 | if (!dirs) |
| 662 | return; |
| 663 | |
| 664 | fs_set_blk_dev_with_part(dirs->desc, dirs->part); |
| 665 | info = fs_get_info(fs_type); |
| 666 | |
| 667 | info->closedir(dirs); |
| 668 | fs_close(); |
| 669 | } |
| 670 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 671 | int fs_unlink(const char *filename) |
| 672 | { |
| 673 | int ret; |
| 674 | |
| 675 | struct fstype_info *info = fs_get_info(fs_type); |
| 676 | |
| 677 | ret = info->unlink(filename); |
| 678 | |
AKASHI Takahiro | 1ba4253 | 2018-09-11 15:59:13 +0900 | [diff] [blame] | 679 | fs_close(); |
| 680 | |
| 681 | return ret; |
| 682 | } |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 683 | |
| 684 | int fs_mkdir(const char *dirname) |
| 685 | { |
| 686 | int ret; |
| 687 | |
| 688 | struct fstype_info *info = fs_get_info(fs_type); |
| 689 | |
| 690 | ret = info->mkdir(dirname); |
| 691 | |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 692 | fs_close(); |
| 693 | |
| 694 | return ret; |
| 695 | } |
Rob Clark | 2b7bfd9 | 2017-09-09 13:15:55 -0400 | [diff] [blame] | 696 | |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 697 | int fs_ln(const char *fname, const char *target) |
| 698 | { |
| 699 | struct fstype_info *info = fs_get_info(fs_type); |
| 700 | int ret; |
| 701 | |
| 702 | ret = info->ln(fname, target); |
| 703 | |
| 704 | if (ret < 0) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 705 | log_err("** Unable to create link %s -> %s **\n", fname, target); |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 706 | ret = -1; |
| 707 | } |
| 708 | fs_close(); |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 713 | int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 714 | int fstype) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 715 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 716 | loff_t size; |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 717 | |
| 718 | if (argc != 4) |
| 719 | return CMD_RET_USAGE; |
| 720 | |
| 721 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 722 | return 1; |
| 723 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 724 | if (fs_size(argv[3], &size) < 0) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 725 | return CMD_RET_FAILURE; |
| 726 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 727 | env_set_hex("filesize", size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 732 | int do_load(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 733 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 734 | { |
| 735 | unsigned long addr; |
| 736 | const char *addr_str; |
| 737 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 738 | loff_t bytes; |
| 739 | loff_t pos; |
| 740 | loff_t len_read; |
| 741 | int ret; |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 742 | unsigned long time; |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 743 | char *ep; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 744 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 745 | if (argc < 2) |
| 746 | return CMD_RET_USAGE; |
| 747 | if (argc > 7) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 748 | return CMD_RET_USAGE; |
| 749 | |
Simon Glass | 793a98e | 2023-11-18 14:05:20 -0700 | [diff] [blame] | 750 | if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) { |
Heinrich Schuchardt | c556a45 | 2021-01-27 21:26:43 +0100 | [diff] [blame] | 751 | log_err("Can't set block device\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 752 | return 1; |
Heinrich Schuchardt | c556a45 | 2021-01-27 21:26:43 +0100 | [diff] [blame] | 753 | } |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 754 | |
| 755 | if (argc >= 4) { |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 756 | addr = hextoul(argv[3], &ep); |
Pavel Machek | 4c1a9c5 | 2014-07-09 22:42:57 +0200 | [diff] [blame] | 757 | if (ep == argv[3] || *ep != '\0') |
| 758 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 759 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 760 | addr_str = env_get("loadaddr"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 761 | if (addr_str != NULL) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 762 | addr = hextoul(addr_str, NULL); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 763 | else |
| 764 | addr = CONFIG_SYS_LOAD_ADDR; |
| 765 | } |
| 766 | if (argc >= 5) { |
| 767 | filename = argv[4]; |
| 768 | } else { |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 769 | filename = env_get("bootfile"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 770 | if (!filename) { |
| 771 | puts("** No boot file defined **\n"); |
| 772 | return 1; |
| 773 | } |
| 774 | } |
| 775 | if (argc >= 6) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 776 | bytes = hextoul(argv[5], NULL); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 777 | else |
| 778 | bytes = 0; |
| 779 | if (argc >= 7) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 780 | pos = hextoul(argv[6], NULL); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 781 | else |
| 782 | pos = 0; |
| 783 | |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 784 | time = get_timer(0); |
Simon Goldschmidt | 5b2c687 | 2019-01-14 22:38:19 +0100 | [diff] [blame] | 785 | ret = _fs_read(filename, addr, pos, bytes, 1, &len_read); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 786 | time = get_timer(time); |
Heinrich Schuchardt | ca816ce | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 787 | if (ret < 0) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 788 | log_err("Failed to load '%s'\n", filename); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 789 | return 1; |
Heinrich Schuchardt | ca816ce | 2020-06-29 20:08:49 +0200 | [diff] [blame] | 790 | } |
| 791 | |
AKASHI Takahiro | d4c9946 | 2024-01-17 13:39:44 +0900 | [diff] [blame] | 792 | efi_set_bootdev(argv[1], (argc > 2) ? argv[2] : "", |
| 793 | (argc > 4) ? argv[4] : "", map_sysmem(addr, 0), |
| 794 | len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 795 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 796 | printf("%llu bytes read in %lu ms", len_read, time); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 797 | if (time > 0) { |
| 798 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 799 | print_size(div_u64(len_read, time) * 1000, "/s"); |
Andreas BieĂmann | 1fd2d8a | 2012-11-14 13:32:37 +0100 | [diff] [blame] | 800 | puts(")"); |
| 801 | } |
| 802 | puts("\n"); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 803 | |
Simon Glass | 4d949a2 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 804 | env_set_hex("fileaddr", addr); |
| 805 | env_set_hex("filesize", len_read); |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 810 | int do_ls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 811 | int fstype) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 812 | { |
| 813 | if (argc < 2) |
| 814 | return CMD_RET_USAGE; |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 815 | if (argc > 4) |
| 816 | return CMD_RET_USAGE; |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 817 | |
Simon Glass | 793a98e | 2023-11-18 14:05:20 -0700 | [diff] [blame] | 818 | if (fs_set_blk_dev(argv[1], cmd_arg2(argc, argv), fstype)) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 819 | return 1; |
| 820 | |
Stephen Warren | b2d5cd6 | 2012-10-30 12:04:17 +0000 | [diff] [blame] | 821 | if (fs_ls(argc >= 4 ? argv[3] : "/")) |
Stephen Warren | eefbc3f | 2012-10-22 06:43:51 +0000 | [diff] [blame] | 822 | return 1; |
| 823 | |
| 824 | return 0; |
| 825 | } |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 826 | |
Stephen Warren | d008fbb | 2014-02-03 13:21:00 -0700 | [diff] [blame] | 827 | int file_exists(const char *dev_type, const char *dev_part, const char *file, |
| 828 | int fstype) |
| 829 | { |
| 830 | if (fs_set_blk_dev(dev_type, dev_part, fstype)) |
| 831 | return 0; |
| 832 | |
| 833 | return fs_exists(file); |
| 834 | } |
| 835 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 836 | int do_save(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 837 | int fstype) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 838 | { |
| 839 | unsigned long addr; |
| 840 | const char *filename; |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 841 | loff_t bytes; |
| 842 | loff_t pos; |
| 843 | loff_t len; |
| 844 | int ret; |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 845 | unsigned long time; |
| 846 | |
| 847 | if (argc < 6 || argc > 7) |
| 848 | return CMD_RET_USAGE; |
| 849 | |
| 850 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 851 | return 1; |
| 852 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 853 | addr = hextoul(argv[3], NULL); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 854 | filename = argv[4]; |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 855 | bytes = hextoul(argv[5], NULL); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 856 | if (argc >= 7) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 857 | pos = hextoul(argv[6], NULL); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 858 | else |
| 859 | pos = 0; |
| 860 | |
| 861 | time = get_timer(0); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 862 | ret = fs_write(filename, addr, pos, bytes, &len); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 863 | time = get_timer(time); |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 864 | if (ret < 0) |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 865 | return 1; |
| 866 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 867 | printf("%llu bytes written in %lu ms", len, time); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 868 | if (time > 0) { |
| 869 | puts(" ("); |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 870 | print_size(div_u64(len, time) * 1000, "/s"); |
Simon Glass | eda14ea | 2013-04-20 08:42:50 +0000 | [diff] [blame] | 871 | puts(")"); |
| 872 | } |
| 873 | puts("\n"); |
| 874 | |
| 875 | return 0; |
| 876 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 877 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 878 | int do_fs_uuid(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], |
| 879 | int fstype) |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 880 | { |
| 881 | int ret; |
| 882 | char uuid[37]; |
| 883 | memset(uuid, 0, sizeof(uuid)); |
| 884 | |
| 885 | if (argc < 3 || argc > 4) |
| 886 | return CMD_RET_USAGE; |
| 887 | |
| 888 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 889 | return 1; |
| 890 | |
| 891 | ret = fs_uuid(uuid); |
| 892 | if (ret) |
| 893 | return CMD_RET_FAILURE; |
| 894 | |
| 895 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 896 | env_set(argv[3], uuid); |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 897 | else |
| 898 | printf("%s\n", uuid); |
| 899 | |
| 900 | return CMD_RET_SUCCESS; |
| 901 | } |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 902 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 903 | 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] | 904 | { |
| 905 | struct fstype_info *info; |
| 906 | |
| 907 | if (argc < 3 || argc > 4) |
| 908 | return CMD_RET_USAGE; |
| 909 | |
| 910 | if (fs_set_blk_dev(argv[1], argv[2], FS_TYPE_ANY)) |
| 911 | return 1; |
| 912 | |
| 913 | info = fs_get_info(fs_type); |
| 914 | |
| 915 | if (argc == 4) |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 916 | env_set(argv[3], info->name); |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 917 | else |
| 918 | printf("%s\n", info->name); |
| 919 | |
Marek Vasut | 08d1c42 | 2019-02-06 13:19:29 +0100 | [diff] [blame] | 920 | fs_close(); |
| 921 | |
Sjoerd Simons | 7d9faf6 | 2015-01-05 18:13:36 +0100 | [diff] [blame] | 922 | return CMD_RET_SUCCESS; |
| 923 | } |
| 924 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 925 | 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] | 926 | int fstype) |
| 927 | { |
| 928 | if (argc != 4) |
| 929 | return CMD_RET_USAGE; |
| 930 | |
| 931 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 932 | return 1; |
| 933 | |
| 934 | if (fs_unlink(argv[3])) |
| 935 | return 1; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 940 | 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] | 941 | int fstype) |
| 942 | { |
| 943 | int ret; |
| 944 | |
| 945 | if (argc != 4) |
| 946 | return CMD_RET_USAGE; |
| 947 | |
| 948 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 949 | return 1; |
| 950 | |
| 951 | ret = fs_mkdir(argv[3]); |
| 952 | if (ret) { |
Heinrich Schuchardt | b455978 | 2020-08-25 17:49:16 +0000 | [diff] [blame] | 953 | log_err("** Unable to create a directory \"%s\" **\n", argv[3]); |
AKASHI Takahiro | adc8c9f | 2018-09-11 15:59:08 +0900 | [diff] [blame] | 954 | return 1; |
| 955 | } |
| 956 | |
| 957 | return 0; |
| 958 | } |
Jean-Jacques Hiblot | bc23748 | 2019-02-13 12:15:26 +0100 | [diff] [blame] | 959 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 960 | 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] | 961 | int fstype) |
| 962 | { |
| 963 | if (argc != 5) |
| 964 | return CMD_RET_USAGE; |
| 965 | |
| 966 | if (fs_set_blk_dev(argv[1], argv[2], fstype)) |
| 967 | return 1; |
| 968 | |
| 969 | if (fs_ln(argv[3], argv[4])) |
| 970 | return 1; |
| 971 | |
| 972 | return 0; |
| 973 | } |
Niel Fourie | 9252d70 | 2020-03-24 16:17:04 +0100 | [diff] [blame] | 974 | |
| 975 | int do_fs_types(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]) |
| 976 | { |
| 977 | struct fstype_info *drv = fstypes; |
| 978 | const int n_ents = ARRAY_SIZE(fstypes); |
| 979 | struct fstype_info *entry; |
| 980 | int i = 0; |
| 981 | |
| 982 | puts("Supported filesystems"); |
| 983 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 984 | if (entry->fstype != FS_TYPE_ANY) { |
| 985 | printf("%c %s", i ? ',' : ':', entry->name); |
| 986 | i++; |
| 987 | } |
| 988 | } |
| 989 | if (!i) |
| 990 | puts(": <none>"); |
| 991 | puts("\n"); |
| 992 | return CMD_RET_SUCCESS; |
| 993 | } |
Simon Glass | e6c49fc | 2023-06-01 10:22:38 -0600 | [diff] [blame] | 994 | |
| 995 | int fs_read_alloc(const char *fname, ulong size, uint align, void **bufp) |
| 996 | { |
| 997 | loff_t bytes_read; |
| 998 | ulong addr; |
| 999 | char *buf; |
| 1000 | int ret; |
| 1001 | |
| 1002 | buf = memalign(align, size + 1); |
| 1003 | if (!buf) |
| 1004 | return log_msg_ret("buf", -ENOMEM); |
| 1005 | addr = map_to_sysmem(buf); |
| 1006 | |
| 1007 | ret = fs_read(fname, addr, 0, size, &bytes_read); |
| 1008 | if (ret) { |
| 1009 | free(buf); |
| 1010 | return log_msg_ret("read", ret); |
| 1011 | } |
| 1012 | if (size != bytes_read) |
| 1013 | return log_msg_ret("bread", -EIO); |
| 1014 | buf[size] = '\0'; |
| 1015 | |
| 1016 | *bufp = buf; |
| 1017 | |
| 1018 | return 0; |
| 1019 | } |
| 1020 | |
| 1021 | int fs_load_alloc(const char *ifname, const char *dev_part_str, |
| 1022 | const char *fname, ulong max_size, ulong align, void **bufp, |
| 1023 | ulong *sizep) |
| 1024 | { |
| 1025 | loff_t size; |
| 1026 | void *buf; |
| 1027 | int ret; |
| 1028 | |
| 1029 | if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY)) |
| 1030 | return log_msg_ret("set", -ENOMEDIUM); |
| 1031 | |
| 1032 | ret = fs_size(fname, &size); |
| 1033 | if (ret) |
| 1034 | return log_msg_ret("sz", -ENOENT); |
| 1035 | |
| 1036 | if (size >= (max_size ?: SZ_1G)) |
| 1037 | return log_msg_ret("sz", -E2BIG); |
| 1038 | |
| 1039 | if (fs_set_blk_dev(ifname, dev_part_str, FS_TYPE_ANY)) |
| 1040 | return log_msg_ret("set", -ENOMEDIUM); |
| 1041 | |
| 1042 | ret = fs_read_alloc(fname, size, align, &buf); |
| 1043 | if (ret) |
| 1044 | return log_msg_ret("al", ret); |
| 1045 | *sizep = size; |
| 1046 | *bufp = buf; |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |