]> git.ipfire.org Git - thirdparty/u-boot.git/blame - fs/ext4/ext4fs.c
ARC: dwmmc: Adding DesignWare MMC driver support for ARC devboards
[thirdparty/u-boot.git] / fs / ext4 / ext4fs.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
a1596438
US
2/*
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>
7 *
8 * ext4ls and ext4load : Based on ext2 ls and load support in Uboot.
9 * Ext4 read optimization taken from Open-Moko
10 * Qi bootloader
11 *
12 * (C) Copyright 2004
13 * esd gmbh <www.esd-electronics.com>
14 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
15 *
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.
19 *
ed34f34d 20 * ext4write : Based on generic ext4 protocol.
a1596438
US
21 */
22
23#include <common.h>
a1596438
US
24#include <ext_common.h>
25#include <ext4fs.h>
a1596438 26#include "ext4_common.h"
9e374e7b 27#include <div64.h>
a1596438
US
28
29int ext4fs_symlinknest;
94501062 30struct ext_filesystem ext_fs;
a1596438
US
31
32struct ext_filesystem *get_fs(void)
33{
94501062 34 return &ext_fs;
a1596438
US
35}
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 */
9f12cd0e
SR
48int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
49 loff_t len, char *buf, loff_t *actread)
a1596438 50{
50ce4c07 51 struct ext_filesystem *fs = get_fs();
a1596438 52 int i;
04735e9c 53 lbaint_t blockcnt;
50ce4c07
EE
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));
7f101be3 57 unsigned int filesize = le32_to_cpu(node->inode.size);
04735e9c
FL
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;
a1596438
US
63 char *delayed_buf = NULL;
64 short status;
d5aee659
SW
65 struct ext_block_cache cache;
66
67 ext_cache_init(&cache);
a1596438 68
ecdfb419
IR
69 if (blocksize <= 0)
70 return -1;
71
a1596438 72 /* Adjust len so it we can't read past the end of the file. */
66a47ff2
SB
73 if (len + pos > filesize)
74 len = (filesize - pos);
a1596438 75
9e374e7b 76 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
a1596438 77
9e374e7b 78 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
509b498a 79 long int blknr;
9e374e7b 80 int blockoff = pos - (blocksize * i);
a1596438
US
81 int blockend = blocksize;
82 int skipfirst = 0;
d5aee659
SW
83 blknr = read_allocated_block(&node->inode, i, &cache);
84 if (blknr < 0) {
85 ext_cache_fini(&cache);
715b56fe 86 return -1;
d5aee659 87 }
a1596438 88
50ce4c07 89 blknr = blknr << log2_fs_blocksize;
a1596438
US
90
91 /* Last block. */
92 if (i == blockcnt - 1) {
9e374e7b 93 blockend = (len + pos) - (blocksize * i);
a1596438
US
94
95 /* The last portion is exactly blocksize. */
96 if (!blockend)
97 blockend = blocksize;
98 }
99
100 /* First block. */
9e374e7b 101 if (i == lldiv(pos, blocksize)) {
a1596438
US
102 skipfirst = blockoff;
103 blockend -= skipfirst;
104 }
105 if (blknr) {
106 int status;
107
108 if (previous_block_number != -1) {
109 if (delayed_next == blknr) {
110 delayed_extent += blockend;
50ce4c07 111 delayed_next += blockend >> log2blksz;
a1596438
US
112 } else { /* spill */
113 status = ext4fs_devread(delayed_start,
114 delayed_skipfirst,
115 delayed_extent,
116 delayed_buf);
d5aee659
SW
117 if (status == 0) {
118 ext_cache_fini(&cache);
715b56fe 119 return -1;
d5aee659 120 }
a1596438
US
121 previous_block_number = blknr;
122 delayed_start = blknr;
123 delayed_extent = blockend;
124 delayed_skipfirst = skipfirst;
125 delayed_buf = buf;
126 delayed_next = blknr +
50ce4c07 127 (blockend >> log2blksz);
a1596438
US
128 }
129 } else {
130 previous_block_number = blknr;
131 delayed_start = blknr;
132 delayed_extent = blockend;
133 delayed_skipfirst = skipfirst;
134 delayed_buf = buf;
135 delayed_next = blknr +
50ce4c07 136 (blockend >> log2blksz);
a1596438
US
137 }
138 } else {
ecdfb419 139 int n;
a1596438
US
140 if (previous_block_number != -1) {
141 /* spill */
142 status = ext4fs_devread(delayed_start,
143 delayed_skipfirst,
144 delayed_extent,
145 delayed_buf);
d5aee659
SW
146 if (status == 0) {
147 ext_cache_fini(&cache);
715b56fe 148 return -1;
d5aee659 149 }
a1596438
US
150 previous_block_number = -1;
151 }
ecdfb419
IR
152 /* Zero no more than `len' bytes. */
153 n = blocksize - skipfirst;
154 if (n > len)
155 n = len;
156 memset(buf, 0, n);
a1596438
US
157 }
158 buf += blocksize - skipfirst;
159 }
160 if (previous_block_number != -1) {
161 /* spill */
162 status = ext4fs_devread(delayed_start,
163 delayed_skipfirst, delayed_extent,
164 delayed_buf);
d5aee659
SW
165 if (status == 0) {
166 ext_cache_fini(&cache);
715b56fe 167 return -1;
d5aee659 168 }
a1596438
US
169 previous_block_number = -1;
170 }
171
9f12cd0e 172 *actread = len;
d5aee659 173 ext_cache_fini(&cache);
9f12cd0e 174 return 0;
a1596438
US
175}
176
177int ext4fs_ls(const char *dirname)
178{
e71a969c 179 struct ext2fs_node *dirnode = NULL;
a1596438
US
180 int status;
181
182 if (dirname == NULL)
183 return 0;
184
185 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
186 FILETYPE_DIRECTORY);
187 if (status != 1) {
188 printf("** Can not find directory. **\n");
e71a969c
EH
189 if (dirnode)
190 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
a1596438
US
191 return 1;
192 }
193
194 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
195 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
196
197 return 0;
198}
199
55af5c93
SW
200int ext4fs_exists(const char *filename)
201{
9f12cd0e
SR
202 loff_t file_len;
203 int ret;
55af5c93 204
9f12cd0e
SR
205 ret = ext4fs_open(filename, &file_len);
206 return ret == 0;
55af5c93
SW
207}
208
d455d878 209int ext4fs_size(const char *filename, loff_t *size)
cf659819 210{
d455d878 211 return ext4fs_open(filename, size);
cf659819
SW
212}
213
66a47ff2 214int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
a1596438
US
215{
216 if (ext4fs_root == NULL || ext4fs_file == NULL)
66a47ff2 217 return -1;
a1596438 218
66a47ff2 219 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
a1596438 220}
e6d52415 221
4101f687 222int ext4fs_probe(struct blk_desc *fs_dev_desc,
e6d52415
SG
223 disk_partition_t *fs_partition)
224{
225 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
226
227 if (!ext4fs_mount(fs_partition->size)) {
228 ext4fs_close();
229 return -1;
230 }
231
232 return 0;
233}
234
d455d878
SR
235int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
236 loff_t *len_read)
e6d52415 237{
9f12cd0e 238 loff_t file_len;
9f12cd0e 239 int ret;
e6d52415 240
9f12cd0e
SR
241 ret = ext4fs_open(filename, &file_len);
242 if (ret < 0) {
e6d52415
SG
243 printf("** File not found %s **\n", filename);
244 return -1;
245 }
246
247 if (len == 0)
248 len = file_len;
249
66a47ff2 250 return ext4fs_read(buf, offset, len, len_read);
e6d52415 251}
59e890ef
CG
252
253int ext4fs_uuid(char *uuid_str)
254{
255 if (ext4fs_root == NULL)
256 return -1;
257
258#ifdef CONFIG_LIB_UUID
259 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
260 uuid_str, UUID_STR_FORMAT_STD);
261
262 return 0;
263#else
264 return -ENOSYS;
265#endif
266}
d5aee659
SW
267
268void ext_cache_init(struct ext_block_cache *cache)
269{
270 memset(cache, 0, sizeof(*cache));
271}
272
273void ext_cache_fini(struct ext_block_cache *cache)
274{
275 free(cache->buf);
276 ext_cache_init(cache);
277}
278
279int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
280{
281 /* This could be more lenient, but this is simple and enough for now */
282 if (cache->buf && cache->block == block && cache->size == size)
283 return 1;
284 ext_cache_fini(cache);
285 cache->buf = malloc(size);
286 if (!cache->buf)
287 return 0;
288 if (!ext4fs_devread(block, 0, size, cache->buf)) {
289 free(cache->buf);
290 return 0;
291 }
292 cache->block = block;
293 cache->size = size;
294 return 1;
295}