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"
31 int ext4fs_symlinknest;
32 struct ext_filesystem ext_fs;
34 struct ext_filesystem *get_fs(void)
39 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
41 if ((node != &ext4fs_root->diropen) && (node != currroot))
46 * Taken from openmoko-kernel mailing list: By Andy green
47 * Optimized read file API : collects and defers contiguous sector
48 * reads into one potentially more efficient larger sequential read action
50 int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
51 loff_t len, char *buf, loff_t *actread)
53 struct ext_filesystem *fs = get_fs();
56 int log2blksz = fs->dev_desc->log2blksz;
57 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
58 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
59 unsigned int filesize = le32_to_cpu(node->inode.size);
60 lbaint_t previous_block_number = -1;
61 lbaint_t delayed_start = 0;
62 lbaint_t delayed_extent = 0;
63 lbaint_t delayed_skipfirst = 0;
64 lbaint_t delayed_next = 0;
65 char *delayed_buf = NULL;
66 char *start_buf = buf;
68 struct ext_block_cache cache;
70 ext_cache_init(&cache);
72 /* Adjust len so it we can't read past the end of the file. */
73 if (len + pos > filesize)
74 len = (filesize - pos);
76 if (blocksize <= 0 || len <= 0) {
77 ext_cache_fini(&cache);
81 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
83 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
85 int blockoff = pos - (blocksize * i);
86 int blockend = blocksize;
88 blknr = read_allocated_block(&node->inode, i, &cache);
90 ext_cache_fini(&cache);
94 blknr = blknr << log2_fs_blocksize;
97 if (i == blockcnt - 1) {
98 blockend = (len + pos) - (blocksize * i);
100 /* The last portion is exactly blocksize. */
102 blockend = blocksize;
106 if (i == lldiv(pos, blocksize)) {
107 skipfirst = blockoff;
108 blockend -= skipfirst;
113 if (previous_block_number != -1) {
114 if (delayed_next == blknr) {
115 delayed_extent += blockend;
116 delayed_next += blockend >> log2blksz;
118 status = ext4fs_devread(delayed_start,
123 ext_cache_fini(&cache);
126 previous_block_number = blknr;
127 delayed_start = blknr;
128 delayed_extent = blockend;
129 delayed_skipfirst = skipfirst;
131 delayed_next = blknr +
132 (blockend >> log2blksz);
135 previous_block_number = blknr;
136 delayed_start = blknr;
137 delayed_extent = blockend;
138 delayed_skipfirst = skipfirst;
140 delayed_next = blknr +
141 (blockend >> log2blksz);
146 if (previous_block_number != -1) {
148 status = ext4fs_devread(delayed_start,
153 ext_cache_fini(&cache);
156 previous_block_number = -1;
158 /* Zero no more than `len' bytes. */
159 n = blocksize - skipfirst;
160 n_left = len - ( buf - start_buf );
165 buf += blocksize - skipfirst;
167 if (previous_block_number != -1) {
169 status = ext4fs_devread(delayed_start,
170 delayed_skipfirst, delayed_extent,
173 ext_cache_fini(&cache);
176 previous_block_number = -1;
180 ext_cache_fini(&cache);
184 int ext4fs_ls(const char *dirname)
186 struct ext2fs_node *dirnode = NULL;
192 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
195 printf("** Can not find directory. **\n");
197 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
201 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
202 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
207 int ext4fs_exists(const char *filename)
212 ret = ext4fs_open(filename, &file_len);
216 int ext4fs_size(const char *filename, loff_t *size)
218 return ext4fs_open(filename, size);
221 int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
223 if (ext4fs_root == NULL || ext4fs_file == NULL)
226 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
229 int ext4fs_probe(struct blk_desc *fs_dev_desc,
230 disk_partition_t *fs_partition)
232 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
234 if (!ext4fs_mount(fs_partition->size)) {
242 int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
248 ret = ext4fs_open(filename, &file_len);
250 printf("** File not found %s **\n", filename);
257 return ext4fs_read(buf, offset, len, len_read);
260 int ext4fs_uuid(char *uuid_str)
262 if (ext4fs_root == NULL)
265 #ifdef CONFIG_LIB_UUID
266 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
267 uuid_str, UUID_STR_FORMAT_STD);
275 void ext_cache_init(struct ext_block_cache *cache)
277 memset(cache, 0, sizeof(*cache));
280 void ext_cache_fini(struct ext_block_cache *cache)
283 ext_cache_init(cache);
286 int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
288 /* This could be more lenient, but this is simple and enough for now */
289 if (cache->buf && cache->block == block && cache->size == size)
291 ext_cache_fini(cache);
292 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
295 if (!ext4fs_devread(block, 0, size, cache->buf)) {
296 ext_cache_fini(cache);
299 cache->block = block;