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