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 | |
| 23 | #include <common.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 24 | #include <ext_common.h> |
| 25 | #include <ext4fs.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 26 | #include "ext4_common.h" |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 27 | #include <div64.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 28 | #include <malloc.h> |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 29 | |
| 30 | int ext4fs_symlinknest; |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 31 | struct ext_filesystem ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 32 | |
| 33 | struct ext_filesystem *get_fs(void) |
| 34 | { |
Rob Herring | 30f7fd7 | 2012-08-23 11:31:45 +0000 | [diff] [blame] | 35 | return &ext_fs; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot) |
| 39 | { |
| 40 | if ((node != &ext4fs_root->diropen) && (node != currroot)) |
| 41 | free(node); |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Taken from openmoko-kernel mailing list: By Andy green |
| 46 | * Optimized read file API : collects and defers contiguous sector |
| 47 | * reads into one potentially more efficient larger sequential read action |
| 48 | */ |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 49 | int ext4fs_read_file(struct ext2fs_node *node, loff_t pos, |
| 50 | loff_t len, char *buf, loff_t *actread) |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 51 | { |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 52 | struct ext_filesystem *fs = get_fs(); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 53 | int i; |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 54 | lbaint_t blockcnt; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 55 | int log2blksz = fs->dev_desc->log2blksz; |
| 56 | int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz; |
| 57 | int blocksize = (1 << (log2_fs_blocksize + log2blksz)); |
Michael Walle | c07cdcb | 2016-08-29 10:46:44 +0200 | [diff] [blame] | 58 | unsigned int filesize = le32_to_cpu(node->inode.size); |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 59 | lbaint_t previous_block_number = -1; |
| 60 | lbaint_t delayed_start = 0; |
| 61 | lbaint_t delayed_extent = 0; |
| 62 | lbaint_t delayed_skipfirst = 0; |
| 63 | lbaint_t delayed_next = 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 64 | char *delayed_buf = NULL; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 65 | char *start_buf = buf; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 66 | short status; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 67 | struct ext_block_cache cache; |
| 68 | |
| 69 | ext_cache_init(&cache); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 70 | |
| 71 | /* 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] | 72 | if (len + pos > filesize) |
| 73 | len = (filesize - pos); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 74 | |
Paul Emge | a4a48ef | 2019-07-08 16:37:05 -0700 | [diff] [blame] | 75 | if (blocksize <= 0 || len <= 0) { |
| 76 | ext_cache_fini(&cache); |
| 77 | return -1; |
| 78 | } |
| 79 | |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 80 | blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 81 | |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 82 | for (i = lldiv(pos, blocksize); i < blockcnt; i++) { |
Lokesh Vutla | c32ffa8 | 2017-04-26 16:58:22 +0530 | [diff] [blame] | 83 | long int blknr; |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 84 | int blockoff = pos - (blocksize * i); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 85 | int blockend = blocksize; |
| 86 | int skipfirst = 0; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 87 | blknr = read_allocated_block(&node->inode, i, &cache); |
| 88 | if (blknr < 0) { |
| 89 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 90 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 91 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 92 | |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 93 | blknr = blknr << log2_fs_blocksize; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 94 | |
| 95 | /* Last block. */ |
| 96 | if (i == blockcnt - 1) { |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 97 | blockend = (len + pos) - (blocksize * i); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 98 | |
| 99 | /* The last portion is exactly blocksize. */ |
| 100 | if (!blockend) |
| 101 | blockend = blocksize; |
| 102 | } |
| 103 | |
| 104 | /* First block. */ |
Tom Rini | a17b7bc | 2014-11-24 11:50:46 -0500 | [diff] [blame] | 105 | if (i == lldiv(pos, blocksize)) { |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 106 | skipfirst = blockoff; |
| 107 | blockend -= skipfirst; |
| 108 | } |
| 109 | if (blknr) { |
| 110 | int status; |
| 111 | |
| 112 | if (previous_block_number != -1) { |
| 113 | if (delayed_next == blknr) { |
| 114 | delayed_extent += blockend; |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 115 | delayed_next += blockend >> log2blksz; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 116 | } else { /* spill */ |
| 117 | status = ext4fs_devread(delayed_start, |
| 118 | delayed_skipfirst, |
| 119 | delayed_extent, |
| 120 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 121 | if (status == 0) { |
| 122 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 123 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 124 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 125 | previous_block_number = blknr; |
| 126 | delayed_start = blknr; |
| 127 | delayed_extent = blockend; |
| 128 | delayed_skipfirst = skipfirst; |
| 129 | delayed_buf = buf; |
| 130 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 131 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 132 | } |
| 133 | } else { |
| 134 | previous_block_number = blknr; |
| 135 | delayed_start = blknr; |
| 136 | delayed_extent = blockend; |
| 137 | delayed_skipfirst = skipfirst; |
| 138 | delayed_buf = buf; |
| 139 | delayed_next = blknr + |
Egbert Eich | 7b1b255 | 2013-05-01 01:13:19 +0000 | [diff] [blame] | 140 | (blockend >> log2blksz); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 141 | } |
| 142 | } else { |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 143 | int n; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 144 | int n_left; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 145 | if (previous_block_number != -1) { |
| 146 | /* spill */ |
| 147 | status = ext4fs_devread(delayed_start, |
| 148 | delayed_skipfirst, |
| 149 | delayed_extent, |
| 150 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 151 | if (status == 0) { |
| 152 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 153 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 154 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 155 | previous_block_number = -1; |
| 156 | } |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 157 | /* Zero no more than `len' bytes. */ |
| 158 | n = blocksize - skipfirst; |
Paul Emge | 627e3c8 | 2019-07-08 16:37:07 -0700 | [diff] [blame] | 159 | n_left = len - ( buf - start_buf ); |
| 160 | if (n > n_left) |
| 161 | n = n_left; |
Ian Ray | c45fcd5 | 2017-11-08 15:35:10 +0000 | [diff] [blame] | 162 | memset(buf, 0, n); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 163 | } |
| 164 | buf += blocksize - skipfirst; |
| 165 | } |
| 166 | if (previous_block_number != -1) { |
| 167 | /* spill */ |
| 168 | status = ext4fs_devread(delayed_start, |
| 169 | delayed_skipfirst, delayed_extent, |
| 170 | delayed_buf); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 171 | if (status == 0) { |
| 172 | ext_cache_fini(&cache); |
Tom Rini | ee03bd1 | 2014-02-26 08:18:58 -0500 | [diff] [blame] | 173 | return -1; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 174 | } |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 175 | previous_block_number = -1; |
| 176 | } |
| 177 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 178 | *actread = len; |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 179 | ext_cache_fini(&cache); |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 180 | return 0; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | int ext4fs_ls(const char *dirname) |
| 184 | { |
Eugen Hristev | d58c8e9 | 2018-05-09 16:28:37 +0300 | [diff] [blame] | 185 | struct ext2fs_node *dirnode = NULL; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 186 | int status; |
| 187 | |
| 188 | if (dirname == NULL) |
| 189 | return 0; |
| 190 | |
| 191 | status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode, |
| 192 | FILETYPE_DIRECTORY); |
| 193 | if (status != 1) { |
| 194 | printf("** Can not find directory. **\n"); |
Eugen Hristev | d58c8e9 | 2018-05-09 16:28:37 +0300 | [diff] [blame] | 195 | if (dirnode) |
| 196 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 197 | return 1; |
| 198 | } |
| 199 | |
| 200 | ext4fs_iterate_dir(dirnode, NULL, NULL, NULL); |
| 201 | ext4fs_free_node(dirnode, &ext4fs_root->diropen); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 206 | int ext4fs_exists(const char *filename) |
| 207 | { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 208 | loff_t file_len; |
| 209 | int ret; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 210 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 211 | ret = ext4fs_open(filename, &file_len); |
| 212 | return ret == 0; |
Stephen Warren | 12d6d0c | 2014-02-03 13:21:09 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 215 | int ext4fs_size(const char *filename, loff_t *size) |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 216 | { |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 217 | return ext4fs_open(filename, size); |
Stephen Warren | 3eb58f5 | 2014-06-11 12:47:26 -0600 | [diff] [blame] | 218 | } |
| 219 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 220 | 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] | 221 | { |
| 222 | if (ext4fs_root == NULL || ext4fs_file == NULL) |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 223 | return -1; |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 224 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 225 | return ext4fs_read_file(ext4fs_file, offset, len, buf, actread); |
Uma Shankar | 71014b6 | 2012-05-25 21:21:44 +0530 | [diff] [blame] | 226 | } |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 227 | |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 228 | int ext4fs_probe(struct blk_desc *fs_dev_desc, |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 229 | disk_partition_t *fs_partition) |
| 230 | { |
| 231 | ext4fs_set_blk_dev(fs_dev_desc, fs_partition); |
| 232 | |
| 233 | if (!ext4fs_mount(fs_partition->size)) { |
| 234 | ext4fs_close(); |
| 235 | return -1; |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
Suriyan Ramasami | 96171fb | 2014-11-17 14:39:38 -0800 | [diff] [blame] | 241 | int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len, |
| 242 | loff_t *len_read) |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 243 | { |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 244 | loff_t file_len; |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 245 | int ret; |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 246 | |
Suriyan Ramasami | b3a2d5a | 2014-11-17 14:39:36 -0800 | [diff] [blame] | 247 | ret = ext4fs_open(filename, &file_len); |
| 248 | if (ret < 0) { |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 249 | printf("** File not found %s **\n", filename); |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (len == 0) |
| 254 | len = file_len; |
| 255 | |
Stefan Brüns | db5862d | 2016-11-06 18:33:57 +0100 | [diff] [blame] | 256 | return ext4fs_read(buf, offset, len, len_read); |
Simon Glass | 19e3858 | 2012-12-26 09:53:33 +0000 | [diff] [blame] | 257 | } |
Christian Gmeiner | 9f9eec3 | 2014-11-12 14:35:04 +0100 | [diff] [blame] | 258 | |
| 259 | int ext4fs_uuid(char *uuid_str) |
| 260 | { |
| 261 | if (ext4fs_root == NULL) |
| 262 | return -1; |
| 263 | |
| 264 | #ifdef CONFIG_LIB_UUID |
| 265 | uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id, |
| 266 | uuid_str, UUID_STR_FORMAT_STD); |
| 267 | |
| 268 | return 0; |
| 269 | #else |
| 270 | return -ENOSYS; |
| 271 | #endif |
| 272 | } |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 273 | |
| 274 | void ext_cache_init(struct ext_block_cache *cache) |
| 275 | { |
| 276 | memset(cache, 0, sizeof(*cache)); |
| 277 | } |
| 278 | |
| 279 | void ext_cache_fini(struct ext_block_cache *cache) |
| 280 | { |
| 281 | free(cache->buf); |
| 282 | ext_cache_init(cache); |
| 283 | } |
| 284 | |
| 285 | int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size) |
| 286 | { |
| 287 | /* This could be more lenient, but this is simple and enough for now */ |
| 288 | if (cache->buf && cache->block == block && cache->size == size) |
| 289 | return 1; |
| 290 | ext_cache_fini(cache); |
| 291 | cache->buf = malloc(size); |
| 292 | if (!cache->buf) |
| 293 | return 0; |
| 294 | if (!ext4fs_devread(block, 0, size, cache->buf)) { |
Paul Emge | 955dbbc | 2019-07-08 16:37:04 -0700 | [diff] [blame] | 295 | ext_cache_fini(cache); |
Stephen Warren | 02d6ca7 | 2019-01-30 12:58:05 -0700 | [diff] [blame] | 296 | return 0; |
| 297 | } |
| 298 | cache->block = block; |
| 299 | cache->size = size; |
| 300 | return 1; |
| 301 | } |