2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
7 * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
8 * Ext4 read optimization taken from Open-Moko
12 * esd gmbh <www.esd-electronics.com>
15 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
16 * GRUB -- GRand Unified Bootloader
17 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
19 * ext4write : Based on generic ext4 protocol.
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 #include <ext_common.h>
39 #include "ext4_common.h"
41 int ext4fs_symlinknest;
42 struct ext_filesystem ext_fs;
44 struct ext_filesystem *get_fs(void)
49 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
51 if ((node != &ext4fs_root->diropen) && (node != currroot))
56 * Taken from openmoko-kernel mailing list: By Andy green
57 * Optimized read file API : collects and defers contiguous sector
58 * reads into one potentially more efficient larger sequential read action
60 int ext4fs_read_file(struct ext2fs_node *node, int pos,
61 unsigned int len, char *buf)
63 struct ext_filesystem *fs = get_fs();
66 int log2blksz = fs->dev_desc->log2blksz;
67 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
68 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
69 unsigned int filesize = __le32_to_cpu(node->inode.size);
70 int previous_block_number = -1;
71 int delayed_start = 0;
72 int delayed_extent = 0;
73 int delayed_skipfirst = 0;
75 char *delayed_buf = NULL;
78 /* Adjust len so it we can't read past the end of the file. */
82 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
84 for (i = pos / blocksize; i < blockcnt; i++) {
86 int blockoff = pos % blocksize;
87 int blockend = blocksize;
89 blknr = read_allocated_block(&(node->inode), i);
93 blknr = blknr << log2_fs_blocksize;
96 if (i == blockcnt - 1) {
97 blockend = (len + pos) % blocksize;
99 /* The last portion is exactly blocksize. */
101 blockend = blocksize;
105 if (i == 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,
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);
141 if (previous_block_number != -1) {
143 status = ext4fs_devread(delayed_start,
149 previous_block_number = -1;
151 memset(buf, 0, blocksize - skipfirst);
153 buf += blocksize - skipfirst;
155 if (previous_block_number != -1) {
157 status = ext4fs_devread(delayed_start,
158 delayed_skipfirst, delayed_extent,
162 previous_block_number = -1;
168 int ext4fs_ls(const char *dirname)
170 struct ext2fs_node *dirnode;
176 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
179 printf("** Can not find directory. **\n");
183 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
184 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
189 int ext4fs_read(char *buf, unsigned len)
191 if (ext4fs_root == NULL || ext4fs_file == NULL)
194 return ext4fs_read_file(ext4fs_file, 0, len, buf);
197 int ext4fs_probe(block_dev_desc_t *fs_dev_desc,
198 disk_partition_t *fs_partition)
200 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
202 if (!ext4fs_mount(fs_partition->size)) {
210 int ext4_read_file(const char *filename, void *buf, int offset, int len)
216 printf("** Cannot support non-zero offset **\n");
220 file_len = ext4fs_open(filename);
222 printf("** File not found %s **\n", filename);
229 len_read = ext4fs_read(buf, len);