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