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.
25 #include <ext_common.h>
27 #include "ext4_common.h"
33 int ext4fs_symlinknest;
34 struct ext_filesystem ext_fs;
36 struct ext_filesystem *get_fs(void)
41 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
43 if ((node != &ext4fs_root->diropen) && (node != currroot))
48 * Taken from openmoko-kernel mailing list: By Andy green
49 * Optimized read file API : collects and defers contiguous sector
50 * reads into one potentially more efficient larger sequential read action
52 int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
53 loff_t len, char *buf, loff_t *actread)
55 struct ext_filesystem *fs = get_fs();
58 int log2blksz = fs->dev_desc->log2blksz;
59 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
60 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
61 unsigned int filesize = le32_to_cpu(node->inode.size);
62 lbaint_t previous_block_number = -1;
63 lbaint_t delayed_start = 0;
64 lbaint_t delayed_extent = 0;
65 lbaint_t delayed_skipfirst = 0;
66 lbaint_t delayed_next = 0;
67 char *delayed_buf = NULL;
68 char *start_buf = buf;
70 struct ext_block_cache cache;
72 ext_cache_init(&cache);
74 /* Adjust len so it we can't read past the end of the file. */
75 if (len + pos > filesize)
76 len = (filesize - pos);
78 if (blocksize <= 0 || len <= 0) {
79 ext_cache_fini(&cache);
83 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
85 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
87 int blockoff = pos - (blocksize * i);
88 int blockend = blocksize;
90 blknr = read_allocated_block(&node->inode, i, &cache);
92 ext_cache_fini(&cache);
96 blknr = blknr << log2_fs_blocksize;
99 if (i == blockcnt - 1) {
100 blockend = (len + pos) - (blocksize * i);
102 /* The last portion is exactly blocksize. */
104 blockend = blocksize;
108 if (i == lldiv(pos, blocksize)) {
109 skipfirst = blockoff;
110 blockend -= skipfirst;
115 if (previous_block_number != -1) {
116 if (delayed_next == blknr) {
117 delayed_extent += blockend;
118 delayed_next += blockend >> log2blksz;
120 status = ext4fs_devread(delayed_start,
125 ext_cache_fini(&cache);
128 previous_block_number = blknr;
129 delayed_start = blknr;
130 delayed_extent = blockend;
131 delayed_skipfirst = skipfirst;
133 delayed_next = blknr +
134 (blockend >> log2blksz);
137 previous_block_number = blknr;
138 delayed_start = blknr;
139 delayed_extent = blockend;
140 delayed_skipfirst = skipfirst;
142 delayed_next = blknr +
143 (blockend >> log2blksz);
148 if (previous_block_number != -1) {
150 status = ext4fs_devread(delayed_start,
155 ext_cache_fini(&cache);
158 previous_block_number = -1;
160 /* Zero no more than `len' bytes. */
161 n = blocksize - skipfirst;
162 n_left = len - ( buf - start_buf );
167 buf += blocksize - skipfirst;
169 if (previous_block_number != -1) {
171 status = ext4fs_devread(delayed_start,
172 delayed_skipfirst, delayed_extent,
175 ext_cache_fini(&cache);
178 previous_block_number = -1;
182 ext_cache_fini(&cache);
186 int ext4fs_ls(const char *dirname)
188 struct ext2fs_node *dirnode = NULL;
194 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
197 printf("** Can not find directory. **\n");
199 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
203 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
204 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
209 int ext4fs_exists(const char *filename)
214 ret = ext4fs_open(filename, &file_len);
218 int ext4fs_size(const char *filename, loff_t *size)
220 return ext4fs_open(filename, size);
223 int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
225 if (ext4fs_root == NULL || ext4fs_file == NULL)
228 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
231 int ext4fs_probe(struct blk_desc *fs_dev_desc,
232 struct disk_partition *fs_partition)
234 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
236 if (!ext4fs_mount(fs_partition->size)) {
244 int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
250 ret = ext4fs_open(filename, &file_len);
252 printf("** File not found %s **\n", filename);
259 return ext4fs_read(buf, offset, len, len_read);
262 int ext4fs_uuid(char *uuid_str)
264 if (ext4fs_root == NULL)
267 #ifdef CONFIG_LIB_UUID
268 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
269 uuid_str, UUID_STR_FORMAT_STD);
277 void ext_cache_init(struct ext_block_cache *cache)
279 memset(cache, 0, sizeof(*cache));
282 void ext_cache_fini(struct ext_block_cache *cache)
285 ext_cache_init(cache);
288 int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
290 /* This could be more lenient, but this is simple and enough for now */
291 if (cache->buf && cache->block == block && cache->size == size)
293 ext_cache_fini(cache);
294 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
297 if (!ext4fs_devread(block, 0, size, cache->buf)) {
298 ext_cache_fini(cache);
301 cache->block = block;