Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2011 - 2012 Samsung Electronics |
| 4 | * EXT4 filesystem implementation in Uboot by |
| 5 | * Uma Shankar <uma.shankar@samsung.com> |
| 6 | * Manjunatha C Achar <a.manjunatha@samsung.com> |
| 7 | * |
| 8 | * ext4ls and ext4load : Based on ext2 ls and load support in Uboot. |
| 9 | * Ext4 read optimization taken from Open-Moko |
| 10 | * Qi bootloader |
| 11 | * |
| 12 | * (C) Copyright 2004 |
| 13 | * esd gmbh <www.esd-electronics.com> |
| 14 | * Reinhard Arlt <reinhard.arlt@esd-electronics.com> |
| 15 | * |
| 16 | * based on code from grub2 fs/ext2.c and fs/fshelp.c by |
| 17 | * GRUB -- GRand Unified Bootloader |
| 18 | * Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
| 19 | * |
Uma Shankar | a74a99a | 2012-05-25 21:22:49 +0530 | [diff] [blame] | 20 | * ext4write : Based on generic ext4 protocol. |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 21 | */ |
| 22 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 23 | #include <blk.h> |
Heinrich Schuchardt | e415b99 | 2024-10-26 08:40:46 +0200 | [diff] [blame] | 24 | #include <div64.h> |
| 25 | #include <errno.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 26 | #include <ext_common.h> |
| 27 | #include <ext4fs.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 28 | #include <malloc.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 29 | #include <part.h> |
Caleb Connolly | 29cab7c | 2024-08-30 13:34:37 +0100 | [diff] [blame] | 30 | #include <u-boot/uuid.h> |
Heinrich Schuchardt | e415b99 | 2024-10-26 08:40:46 +0200 | [diff] [blame] | 31 | #include "ext4_common.h" |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 32 | |
| 33 | int ext4fs_symlinknest; |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 34 | struct ext_filesystem ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 35 | |
Heinrich Schuchardt | e415b99 | 2024-10-26 08:40:46 +0200 | [diff] [blame] | 36 | /** |
| 37 | * struct ext4_dir_stream - ext4 directory stream |
| 38 | * |
| 39 | * @parent: partition data used by fs layer. |
| 40 | * This field must be at the beginning of the structure. |
| 41 | * All other fields are private to the ext4 driver. |
| 42 | * @root: root directory node |
| 43 | * @dir: directory node |
| 44 | * @dirent: directory stream entry |
| 45 | * @fpos: file position in directory |
| 46 | */ |
| 47 | struct ext4_dir_stream { |
| 48 | struct fs_dir_stream parent; |
| 49 | char *dirname; |
| 50 | struct fs_dirent dirent; |
| 51 | unsigned int fpos; |
| 52 | }; |
| 53 | |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 54 | struct ext_filesystem *get_fs(void) |
| 55 | { |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 56 | return &ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot) |
| 60 | { |
| 61 | if ((node != &ext4fs_root->diropen) && (node != currroot)) |
| 62 | free(node); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Taken from openmoko-kernel mailing list: By Andy green |
| 67 | * Optimized read file API : collects and defers contiguous sector |
| 68 | * reads into one potentially more efficient larger sequential read action |
| 69 | */ |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 70 | int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, |
| 71 | loff_t len, char *buf, loff_t *actread) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 72 | { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 73 | struct ext_filesystem *fs = get_fs(); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 74 | int i; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 75 | lbaint_t blockcnt; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 76 | int log2blksz = fs->dev_desc->log2blksz; |
| 77 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz; |
| 78 | int blocksize = (1 << (log2_fs_blocksize + log2blksz)); |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 79 | unsigned int filesize = le32_to_cpu(node->inode.size); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 80 | lbaint_t previous_block_number = -1; |
| 81 | lbaint_t delayed_start = 0; |
| 82 | lbaint_t delayed_extent = 0; |
| 83 | lbaint_t delayed_skipfirst = 0; |
| 84 | lbaint_t delayed_next = 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 85 | char *delayed_buf = NULL; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 86 | char *start_buf = buf; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 87 | short status; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 88 | struct ext_block_cache cache; |
| 89 | |
| 90 | ext_cache_init(&cache); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 91 | |
| 92 | /* Adjust len so it we can't read past the end of the file. */ |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 93 | if (len + pos > filesize) |
| 94 | len = (filesize - pos); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 95 | |
Paul Emge | a4a48ef | 2019-07-08 16:37:05 -0700 | [diff] [blame] | 96 | if (blocksize <= 0 || len <= 0) { |
| 97 | ext_cache_fini(&cache); |
| 98 | return -1; |
| 99 | } |
| 100 | |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 101 | blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 102 | |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 103 | for (i = lldiv(pos, blocksize); i < blockcnt; i++) { |
Lokesh Vutla | c32ffa8 | 2017-04-26 16:58:22 +0530 | [diff] [blame] | 104 | long int blknr; |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 105 | int blockoff = pos - (blocksize * i); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 106 | int blockend = blocksize; |
| 107 | int skipfirst = 0; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 108 | blknr = read_allocated_block(&node->inode, i, &cache); |
| 109 | if (blknr < 0) { |
| 110 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 111 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 112 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 113 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 114 | blknr = blknr << log2_fs_blocksize; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 115 | |
| 116 | /* Last block. */ |
| 117 | if (i == blockcnt - 1) { |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 118 | blockend = (len + pos) - (blocksize * i); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 119 | |
| 120 | /* The last portion is exactly blocksize. */ |
| 121 | if (!blockend) |
| 122 | blockend = blocksize; |
| 123 | } |
| 124 | |
| 125 | /* First block. */ |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 126 | if (i == lldiv(pos, blocksize)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 127 | skipfirst = blockoff; |
| 128 | blockend -= skipfirst; |
| 129 | } |
| 130 | if (blknr) { |
| 131 | int status; |
| 132 | |
| 133 | if (previous_block_number != -1) { |
| 134 | if (delayed_next == blknr) { |
| 135 | delayed_extent += blockend; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 136 | delayed_next += blockend >> log2blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 137 | } else { /* spill */ |
| 138 | status = ext4fs_devread(delayed_start, |
| 139 | delayed_skipfirst, |
| 140 | delayed_extent, |
| 141 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 142 | if (status == 0) { |
| 143 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 144 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 145 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 146 | previous_block_number = blknr; |
| 147 | delayed_start = blknr; |
| 148 | delayed_extent = blockend; |
| 149 | delayed_skipfirst = skipfirst; |
| 150 | delayed_buf = buf; |
| 151 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 152 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 153 | } |
| 154 | } else { |
| 155 | previous_block_number = blknr; |
| 156 | delayed_start = blknr; |
| 157 | delayed_extent = blockend; |
| 158 | delayed_skipfirst = skipfirst; |
| 159 | delayed_buf = buf; |
| 160 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 161 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 162 | } |
| 163 | } else { |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 164 | int n; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 165 | int n_left; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 166 | if (previous_block_number != -1) { |
| 167 | /* spill */ |
| 168 | status = ext4fs_devread(delayed_start, |
| 169 | delayed_skipfirst, |
| 170 | delayed_extent, |
| 171 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 172 | if (status == 0) { |
| 173 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 174 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 175 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 176 | previous_block_number = -1; |
| 177 | } |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 178 | /* Zero no more than `len' bytes. */ |
| 179 | n = blocksize - skipfirst; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 180 | n_left = len - ( buf - start_buf ); |
| 181 | if (n > n_left) |
| 182 | n = n_left; |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 183 | memset(buf, 0, n); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 184 | } |
| 185 | buf += blocksize - skipfirst; |
| 186 | } |
| 187 | if (previous_block_number != -1) { |
| 188 | /* spill */ |
| 189 | status = ext4fs_devread(delayed_start, |
| 190 | delayed_skipfirst, delayed_extent, |
| 191 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 192 | if (status == 0) { |
| 193 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 194 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 195 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 196 | previous_block_number = -1; |
| 197 | } |
| 198 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 199 | *actread = len; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 200 | ext_cache_fini(&cache); |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 201 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 202 | } |
| 203 | |
Heinrich Schuchardt | e415b99 | 2024-10-26 08:40:46 +0200 | [diff] [blame] | 204 | int ext4fs_opendir(const char *dirname, struct fs_dir_stream **dirsp) |
| 205 | { |
| 206 | struct ext4_dir_stream *dirs; |
| 207 | struct ext2fs_node *dir = NULL; |
| 208 | int ret; |
| 209 | |
| 210 | *dirsp = NULL; |
| 211 | |
| 212 | dirs = calloc(1, sizeof(struct ext4_dir_stream)); |
| 213 | if (!dirs) |
| 214 | return -ENOMEM; |
| 215 | dirs->dirname = strdup(dirname); |
| 216 | if (!dirs) { |
| 217 | free(dirs); |
| 218 | return -ENOMEM; |
| 219 | } |
| 220 | |
| 221 | ret = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dir, |
| 222 | FILETYPE_DIRECTORY); |
| 223 | if (ret == 1) { |
| 224 | ret = 0; |
| 225 | *dirsp = (struct fs_dir_stream *)dirs; |
| 226 | } else { |
| 227 | ret = -ENOENT; |
| 228 | } |
| 229 | |
| 230 | if (dir) |
| 231 | ext4fs_free_node(dir, &ext4fs_root->diropen); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | int ext4fs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp) |
| 237 | { |
| 238 | struct ext4_dir_stream *dirs = (struct ext4_dir_stream *)fs_dirs; |
| 239 | struct fs_dirent *dent = &dirs->dirent; |
| 240 | struct ext2fs_node *dir = NULL; |
| 241 | int ret; |
| 242 | loff_t actread; |
| 243 | struct ext2fs_node fdiro; |
| 244 | int len; |
| 245 | struct ext2_dirent dirent; |
| 246 | |
| 247 | *dentp = NULL; |
| 248 | |
| 249 | ret = ext4fs_find_file(dirs->dirname, &ext4fs_root->diropen, &dir, |
| 250 | FILETYPE_DIRECTORY); |
| 251 | if (ret != 1) { |
| 252 | ret = -ENOENT; |
| 253 | goto out; |
| 254 | } |
| 255 | if (!dir->inode_read) { |
| 256 | ret = ext4fs_read_inode(dir->data, dir->ino, &dir->inode); |
| 257 | if (!ret) { |
| 258 | ret = -EIO; |
| 259 | goto out; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | if (dirs->fpos >= le32_to_cpu(dir->inode.size)) |
| 264 | return -ENOENT; |
| 265 | |
| 266 | memset(dent, 0, sizeof(struct fs_dirent)); |
| 267 | |
| 268 | while (dirs->fpos < le32_to_cpu(dir->inode.size)) { |
| 269 | ret = ext4fs_read_file(dir, dirs->fpos, |
| 270 | sizeof(struct ext2_dirent), |
| 271 | (char *)&dirent, &actread); |
| 272 | if (ret < 0) |
| 273 | return -ret; |
| 274 | |
| 275 | if (!dirent.direntlen) |
| 276 | return -EIO; |
| 277 | |
| 278 | if (dirent.namelen) |
| 279 | break; |
| 280 | |
| 281 | dirs->fpos += le16_to_cpu(dirent.direntlen); |
| 282 | } |
| 283 | |
| 284 | len = min(FS_DIRENT_NAME_LEN - 1, (int)dirent.namelen); |
| 285 | |
| 286 | ret = ext4fs_read_file(dir, dirs->fpos + sizeof(struct ext2_dirent), |
| 287 | len, dent->name, &actread); |
| 288 | if (ret < 0) |
| 289 | goto out; |
| 290 | dent->name[len] = '\0'; |
| 291 | |
| 292 | fdiro.data = dir->data; |
| 293 | fdiro.ino = le32_to_cpu(dirent.inode); |
| 294 | |
| 295 | ret = ext4fs_read_inode(dir->data, fdiro.ino, &fdiro.inode); |
| 296 | if (!ret) { |
| 297 | ret = -EIO; |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | switch (le16_to_cpu(fdiro.inode.mode) & FILETYPE_INO_MASK) { |
| 302 | case FILETYPE_INO_DIRECTORY: |
| 303 | dent->type = FS_DT_DIR; |
| 304 | break; |
| 305 | case FILETYPE_INO_SYMLINK: |
| 306 | dent->type = FS_DT_LNK; |
| 307 | break; |
| 308 | case FILETYPE_INO_REG: |
| 309 | dent->type = FS_DT_REG; |
| 310 | break; |
| 311 | default: |
| 312 | dent->type = FILETYPE_UNKNOWN; |
| 313 | } |
| 314 | |
| 315 | rtc_to_tm(fdiro.inode.atime, &dent->access_time); |
| 316 | rtc_to_tm(fdiro.inode.ctime, &dent->create_time); |
| 317 | rtc_to_tm(fdiro.inode.mtime, &dent->change_time); |
| 318 | |
| 319 | dirs->fpos += le16_to_cpu(dirent.direntlen); |
| 320 | dent->size = fdiro.inode.size; |
| 321 | *dentp = dent; |
| 322 | ret = 0; |
| 323 | |
| 324 | out: |
| 325 | if (dir) |
| 326 | ext4fs_free_node(dir, &ext4fs_root->diropen); |
| 327 | |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | void ext4fs_closedir(struct fs_dir_stream *fs_dirs) |
| 332 | { |
| 333 | struct ext4_dir_stream *dirs = (struct ext4_dir_stream *)fs_dirs; |
| 334 | |
| 335 | if (!dirs) |
| 336 | return; |
| 337 | |
| 338 | free(dirs->dirname); |
| 339 | free(dirs); |
| 340 | } |
| 341 | |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 342 | int ext4fs_exists(const char *filename) |
| 343 | { |
Heinrich Schuchardt | e2ac638 | 2024-02-20 12:54:23 +0100 | [diff] [blame] | 344 | struct ext2fs_node *dirnode = NULL; |
| 345 | int filetype; |
Heinrich Schuchardt | 149e3c2 | 2024-10-26 08:40:45 +0200 | [diff] [blame] | 346 | int ret; |
Heinrich Schuchardt | e2ac638 | 2024-02-20 12:54:23 +0100 | [diff] [blame] | 347 | |
| 348 | if (!filename) |
| 349 | return 0; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 350 | |
Heinrich Schuchardt | 149e3c2 | 2024-10-26 08:40:45 +0200 | [diff] [blame] | 351 | ret = ext4fs_find_file1(filename, &ext4fs_root->diropen, &dirnode, |
| 352 | &filetype); |
| 353 | if (dirnode) |
| 354 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
| 355 | |
| 356 | return ret; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 359 | int ext4fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 360 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 361 | return ext4fs_open(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 362 | } |
| 363 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 364 | int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 365 | { |
| 366 | if (ext4fs_root == NULL || ext4fs_file == NULL) |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 367 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 368 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 369 | return ext4fs_read_file(ext4fs_file, offset, len, buf, actread); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 370 | } |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 371 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 372 | int ext4fs_probe(struct blk_desc *fs_dev_desc, |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 373 | struct disk_partition *fs_partition) |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 374 | { |
| 375 | ext4fs_set_blk_dev(fs_dev_desc, fs_partition); |
| 376 | |
Sean Anderson | ab125f5 | 2023-11-08 12:51:09 -0500 | [diff] [blame] | 377 | if (!ext4fs_mount()) { |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 378 | ext4fs_close(); |
| 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | return 0; |
| 383 | } |
| 384 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 385 | int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 386 | loff_t *len_read) |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 387 | { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 388 | loff_t file_len; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 389 | int ret; |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 390 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 391 | ret = ext4fs_open(filename, &file_len); |
| 392 | if (ret < 0) { |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 393 | printf("** File not found %s **\n", filename); |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | if (len == 0) |
| 398 | len = file_len; |
| 399 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 400 | return ext4fs_read(buf, offset, len, len_read); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 401 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 402 | |
| 403 | int ext4fs_uuid(char *uuid_str) |
| 404 | { |
| 405 | if (ext4fs_root == NULL) |
| 406 | return -1; |
| 407 | |
| 408 | #ifdef CONFIG_LIB_UUID |
| 409 | uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id, |
| 410 | uuid_str, UUID_STR_FORMAT_STD); |
| 411 | |
| 412 | return 0; |
| 413 | #else |
| 414 | return -ENOSYS; |
| 415 | #endif |
| 416 | } |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 417 | |
| 418 | void ext_cache_init(struct ext_block_cache *cache) |
| 419 | { |
| 420 | memset(cache, 0, sizeof(*cache)); |
| 421 | } |
| 422 | |
| 423 | void ext_cache_fini(struct ext_block_cache *cache) |
| 424 | { |
| 425 | free(cache->buf); |
| 426 | ext_cache_init(cache); |
| 427 | } |
| 428 | |
| 429 | int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size) |
| 430 | { |
| 431 | /* This could be more lenient, but this is simple and enough for now */ |
| 432 | if (cache->buf && cache->block == block && cache->size == size) |
| 433 | return 1; |
| 434 | ext_cache_fini(cache); |
Jan Kiszka | c2ecf22 | 2020-03-25 21:27:51 +0100 | [diff] [blame] | 435 | cache->buf = memalign(ARCH_DMA_MINALIGN, size); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 436 | if (!cache->buf) |
| 437 | return 0; |
| 438 | if (!ext4fs_devread(block, 0, size, cache->buf)) { |
Paul Emge | 955dbbc | 2019-07-08 16:37:04 -0700 | [diff] [blame] | 439 | ext_cache_fini(cache); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | cache->block = block; |
| 443 | cache->size = size; |
| 444 | return 1; |
| 445 | } |