1 // SPDX-License-Identifier: GPL-2.0+
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>
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>
14 * Reinhard Arlt <reinhard.arlt@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"
29 int ext4fs_symlinknest;
30 struct ext_filesystem ext_fs;
32 struct ext_filesystem *get_fs(void)
37 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
39 if ((node != &ext4fs_root->diropen) && (node != currroot))
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
48 int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
49 loff_t len, char *buf, loff_t *actread)
51 struct ext_filesystem *fs = get_fs();
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));
57 unsigned int filesize = le32_to_cpu(node->inode.size);
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;
63 char *delayed_buf = NULL;
65 struct ext_block_cache cache;
67 ext_cache_init(&cache);
69 /* Adjust len so it we can't read past the end of the file. */
70 if (len + pos > filesize)
71 len = (filesize - pos);
73 if (blocksize <= 0 || len <= 0) {
74 ext_cache_fini(&cache);
78 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
80 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
82 int blockoff = pos - (blocksize * i);
83 int blockend = blocksize;
85 blknr = read_allocated_block(&node->inode, i, &cache);
87 ext_cache_fini(&cache);
91 blknr = blknr << log2_fs_blocksize;
94 if (i == blockcnt - 1) {
95 blockend = (len + pos) - (blocksize * i);
97 /* The last portion is exactly blocksize. */
103 if (i == lldiv(pos, blocksize)) {
104 skipfirst = blockoff;
105 blockend -= skipfirst;
110 if (previous_block_number != -1) {
111 if (delayed_next == blknr) {
112 delayed_extent += blockend;
113 delayed_next += blockend >> log2blksz;
115 status = ext4fs_devread(delayed_start,
120 ext_cache_fini(&cache);
123 previous_block_number = blknr;
124 delayed_start = blknr;
125 delayed_extent = blockend;
126 delayed_skipfirst = skipfirst;
128 delayed_next = blknr +
129 (blockend >> log2blksz);
132 previous_block_number = blknr;
133 delayed_start = blknr;
134 delayed_extent = blockend;
135 delayed_skipfirst = skipfirst;
137 delayed_next = blknr +
138 (blockend >> log2blksz);
142 if (previous_block_number != -1) {
144 status = ext4fs_devread(delayed_start,
149 ext_cache_fini(&cache);
152 previous_block_number = -1;
154 /* Zero no more than `len' bytes. */
155 n = blocksize - skipfirst;
160 buf += blocksize - skipfirst;
162 if (previous_block_number != -1) {
164 status = ext4fs_devread(delayed_start,
165 delayed_skipfirst, delayed_extent,
168 ext_cache_fini(&cache);
171 previous_block_number = -1;
175 ext_cache_fini(&cache);
179 int ext4fs_ls(const char *dirname)
181 struct ext2fs_node *dirnode = NULL;
187 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
190 printf("** Can not find directory. **\n");
192 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
196 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
197 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
202 int ext4fs_exists(const char *filename)
207 ret = ext4fs_open(filename, &file_len);
211 int ext4fs_size(const char *filename, loff_t *size)
213 return ext4fs_open(filename, size);
216 int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
218 if (ext4fs_root == NULL || ext4fs_file == NULL)
221 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
224 int ext4fs_probe(struct blk_desc *fs_dev_desc,
225 disk_partition_t *fs_partition)
227 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
229 if (!ext4fs_mount(fs_partition->size)) {
237 int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
243 ret = ext4fs_open(filename, &file_len);
245 printf("** File not found %s **\n", filename);
252 return ext4fs_read(buf, offset, len, len_read);
255 int ext4fs_uuid(char *uuid_str)
257 if (ext4fs_root == NULL)
260 #ifdef CONFIG_LIB_UUID
261 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
262 uuid_str, UUID_STR_FORMAT_STD);
270 void ext_cache_init(struct ext_block_cache *cache)
272 memset(cache, 0, sizeof(*cache));
275 void ext_cache_fini(struct ext_block_cache *cache)
278 ext_cache_init(cache);
281 int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
283 /* This could be more lenient, but this is simple and enough for now */
284 if (cache->buf && cache->block == block && cache->size == size)
286 ext_cache_fini(cache);
287 cache->buf = malloc(size);
290 if (!ext4fs_devread(block, 0, size, cache->buf)) {
291 ext_cache_fini(cache);
294 cache->block = block;