blob: 417ce7b63bf0215122821cee7236e0062e9ee7ef [file] [log] [blame]
Uma Shankar71014b62012-05-25 21:21:44 +05301/*
2 * (C) Copyright 2011 - 2012 Samsung Electronics
3 * EXT4 filesystem implementation in Uboot by
4 * Uma Shankar <uma.shankar@samsung.com>
5 * Manjunatha C Achar <a.manjunatha@samsung.com>
6 *
7 * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
8 * Ext4 read optimization taken from Open-Moko
9 * Qi bootloader
10 *
11 * (C) Copyright 2004
12 * esd gmbh <www.esd-electronics.com>
13 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
14 *
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.
18 *
Uma Shankara74a99a2012-05-25 21:22:49 +053019 * ext4write : Based on generic ext4 protocol.
20 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +020021 * SPDX-License-Identifier: GPL-2.0+
Uma Shankar71014b62012-05-25 21:21:44 +053022 */
23
24#include <common.h>
Uma Shankar71014b62012-05-25 21:21:44 +053025#include <ext_common.h>
26#include <ext4fs.h>
Uma Shankar71014b62012-05-25 21:21:44 +053027#include "ext4_common.h"
28
29int ext4fs_symlinknest;
Rob Herring30f7fd72012-08-23 11:31:45 +000030struct ext_filesystem ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053031
32struct ext_filesystem *get_fs(void)
33{
Rob Herring30f7fd72012-08-23 11:31:45 +000034 return &ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053035}
36
37void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
38{
39 if ((node != &ext4fs_root->diropen) && (node != currroot))
40 free(node);
41}
42
43/*
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
47 */
48int ext4fs_read_file(struct ext2fs_node *node, int pos,
49 unsigned int len, char *buf)
50{
Egbert Eich7b1b2552013-05-01 01:13:19 +000051 struct ext_filesystem *fs = get_fs();
Uma Shankar71014b62012-05-25 21:21:44 +053052 int i;
Frederic Leroye7ee0282013-06-26 18:11:25 +020053 lbaint_t blockcnt;
Egbert Eich7b1b2552013-05-01 01:13:19 +000054 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));
Uma Shankar71014b62012-05-25 21:21:44 +053057 unsigned int filesize = __le32_to_cpu(node->inode.size);
Frederic Leroye7ee0282013-06-26 18:11:25 +020058 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;
Uma Shankar71014b62012-05-25 21:21:44 +053063 char *delayed_buf = NULL;
64 short status;
65
66 /* Adjust len so it we can't read past the end of the file. */
67 if (len > filesize)
68 len = filesize;
69
70 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
71
72 for (i = pos / blocksize; i < blockcnt; i++) {
Frederic Leroye7ee0282013-06-26 18:11:25 +020073 lbaint_t blknr;
Uma Shankar71014b62012-05-25 21:21:44 +053074 int blockoff = pos % blocksize;
75 int blockend = blocksize;
76 int skipfirst = 0;
77 blknr = read_allocated_block(&(node->inode), i);
Tom Riniee03bd12014-02-26 08:18:58 -050078 if (blknr < 0)
79 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +053080
Egbert Eich7b1b2552013-05-01 01:13:19 +000081 blknr = blknr << log2_fs_blocksize;
Uma Shankar71014b62012-05-25 21:21:44 +053082
83 /* Last block. */
84 if (i == blockcnt - 1) {
85 blockend = (len + pos) % blocksize;
86
87 /* The last portion is exactly blocksize. */
88 if (!blockend)
89 blockend = blocksize;
90 }
91
92 /* First block. */
93 if (i == pos / blocksize) {
94 skipfirst = blockoff;
95 blockend -= skipfirst;
96 }
97 if (blknr) {
98 int status;
99
100 if (previous_block_number != -1) {
101 if (delayed_next == blknr) {
102 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000103 delayed_next += blockend >> log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +0530104 } else { /* spill */
105 status = ext4fs_devread(delayed_start,
106 delayed_skipfirst,
107 delayed_extent,
108 delayed_buf);
Tom Riniee03bd12014-02-26 08:18:58 -0500109 if (status == 0)
110 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530111 previous_block_number = blknr;
112 delayed_start = blknr;
113 delayed_extent = blockend;
114 delayed_skipfirst = skipfirst;
115 delayed_buf = buf;
116 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000117 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530118 }
119 } else {
120 previous_block_number = blknr;
121 delayed_start = blknr;
122 delayed_extent = blockend;
123 delayed_skipfirst = skipfirst;
124 delayed_buf = buf;
125 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000126 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530127 }
128 } else {
129 if (previous_block_number != -1) {
130 /* spill */
131 status = ext4fs_devread(delayed_start,
132 delayed_skipfirst,
133 delayed_extent,
134 delayed_buf);
Tom Riniee03bd12014-02-26 08:18:58 -0500135 if (status == 0)
136 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530137 previous_block_number = -1;
138 }
139 memset(buf, 0, blocksize - skipfirst);
140 }
141 buf += blocksize - skipfirst;
142 }
143 if (previous_block_number != -1) {
144 /* spill */
145 status = ext4fs_devread(delayed_start,
146 delayed_skipfirst, delayed_extent,
147 delayed_buf);
Tom Riniee03bd12014-02-26 08:18:58 -0500148 if (status == 0)
149 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530150 previous_block_number = -1;
151 }
152
153 return len;
154}
155
156int ext4fs_ls(const char *dirname)
157{
158 struct ext2fs_node *dirnode;
159 int status;
160
161 if (dirname == NULL)
162 return 0;
163
164 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
165 FILETYPE_DIRECTORY);
166 if (status != 1) {
167 printf("** Can not find directory. **\n");
168 return 1;
169 }
170
171 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
172 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
173
174 return 0;
175}
176
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700177int ext4fs_exists(const char *filename)
178{
179 int file_len;
180
181 file_len = ext4fs_open(filename);
182 return file_len >= 0;
183}
184
Uma Shankar71014b62012-05-25 21:21:44 +0530185int ext4fs_read(char *buf, unsigned len)
186{
187 if (ext4fs_root == NULL || ext4fs_file == NULL)
188 return 0;
189
190 return ext4fs_read_file(ext4fs_file, 0, len, buf);
191}
Simon Glass19e38582012-12-26 09:53:33 +0000192
193int ext4fs_probe(block_dev_desc_t *fs_dev_desc,
194 disk_partition_t *fs_partition)
195{
196 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
197
198 if (!ext4fs_mount(fs_partition->size)) {
199 ext4fs_close();
200 return -1;
201 }
202
203 return 0;
204}
205
206int ext4_read_file(const char *filename, void *buf, int offset, int len)
207{
208 int file_len;
209 int len_read;
210
211 if (offset != 0) {
212 printf("** Cannot support non-zero offset **\n");
213 return -1;
214 }
215
216 file_len = ext4fs_open(filename);
217 if (file_len < 0) {
218 printf("** File not found %s **\n", filename);
219 return -1;
220 }
221
222 if (len == 0)
223 len = file_len;
224
225 len_read = ext4fs_read(buf, len);
226
227 return len_read;
228}