1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2011 - 2012 Samsung Electronics
4 * EXT4 filesystem implementation in Uboot by
8 * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
9 * Ext4 read optimization taken from Open-Moko
13 * esd gmbh <www.esd-electronics.com>
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.
20 * ext4write : Based on generic ext4 protocol.
24 #include <ext_common.h>
26 #include "ext4_common.h"
30 int ext4fs_symlinknest;
31 struct ext_filesystem ext_fs;
33 struct ext_filesystem *get_fs(void)
38 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
40 if ((node != &ext4fs_root->diropen) && (node != currroot))
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
49 int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
50 loff_t len, char *buf, loff_t *actread)
52 struct ext_filesystem *fs = get_fs();
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));
58 unsigned int filesize = le32_to_cpu(node->inode.size);
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;
64 char *delayed_buf = NULL;
65 char *start_buf = buf;
67 struct ext_block_cache cache;
69 ext_cache_init(&cache);
71 /* Adjust len so it we can't read past the end of the file. */
72 if (len + pos > filesize)
73 len = (filesize - pos);
75 if (blocksize <= 0 || len <= 0) {
76 ext_cache_fini(&cache);
80 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
82 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
84 int blockoff = pos - (blocksize * i);
85 int blockend = blocksize;
87 blknr = read_allocated_block(&node->inode, i, &cache);
89 ext_cache_fini(&cache);
93 blknr = blknr << log2_fs_blocksize;
96 if (i == blockcnt - 1) {
97 blockend = (len + pos) - (blocksize * i);
99 /* The last portion is exactly blocksize. */
101 blockend = blocksize;
105 if (i == lldiv(pos, blocksize)) {
106 skipfirst = blockoff;
107 blockend -= skipfirst;
112 if (previous_block_number != -1) {
113 if (delayed_next == blknr) {
114 delayed_extent += blockend;
115 delayed_next += blockend >> log2blksz;
117 status = ext4fs_devread(delayed_start,
122 ext_cache_fini(&cache);
125 previous_block_number = blknr;
126 delayed_start = blknr;
127 delayed_extent = blockend;
128 delayed_skipfirst = skipfirst;
130 delayed_next = blknr +
131 (blockend >> log2blksz);
134 previous_block_number = blknr;
135 delayed_start = blknr;
136 delayed_extent = blockend;
137 delayed_skipfirst = skipfirst;
139 delayed_next = blknr +
140 (blockend >> log2blksz);
145 if (previous_block_number != -1) {
147 status = ext4fs_devread(delayed_start,
152 ext_cache_fini(&cache);
155 previous_block_number = -1;
157 /* Zero no more than `len' bytes. */
158 n = blocksize - skipfirst;
159 n_left = len - ( buf - start_buf );
164 buf += blocksize - skipfirst;
166 if (previous_block_number != -1) {
168 status = ext4fs_devread(delayed_start,
169 delayed_skipfirst, delayed_extent,
172 ext_cache_fini(&cache);
175 previous_block_number = -1;
179 ext_cache_fini(&cache);
183 int ext4fs_ls(const char *dirname)
185 struct ext2fs_node *dirnode = NULL;
191 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
194 printf("** Can not find directory. **\n");
196 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
200 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
201 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
206 int ext4fs_exists(const char *filename)
211 ret = ext4fs_open(filename, &file_len);
215 int ext4fs_size(const char *filename, loff_t *size)
217 return ext4fs_open(filename, size);
220 int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
222 if (ext4fs_root == NULL || ext4fs_file == NULL)
225 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
228 int ext4fs_probe(struct blk_desc *fs_dev_desc,
229 disk_partition_t *fs_partition)
231 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
233 if (!ext4fs_mount(fs_partition->size)) {
241 int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
247 ret = ext4fs_open(filename, &file_len);
249 printf("** File not found %s **\n", filename);
256 return ext4fs_read(buf, offset, len, len_read);
259 int ext4fs_uuid(char *uuid_str)
261 if (ext4fs_root == NULL)
264 #ifdef CONFIG_LIB_UUID
265 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
266 uuid_str, UUID_STR_FORMAT_STD);
274 void ext_cache_init(struct ext_block_cache *cache)
276 memset(cache, 0, sizeof(*cache));
279 void ext_cache_fini(struct ext_block_cache *cache)
282 ext_cache_init(cache);
285 int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
287 /* This could be more lenient, but this is simple and enough for now */
288 if (cache->buf && cache->block == block && cache->size == size)
290 ext_cache_fini(cache);
291 cache->buf = malloc(size);
294 if (!ext4fs_devread(block, 0, size, cache->buf)) {
295 ext_cache_fini(cache);
298 cache->block = block;