]> Git Repo - J-u-boot.git/blame - fs/ext4/ext4fs.c
sysreset: Fix unsupported request return values
[J-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 <[email protected]>
6 * Manjunatha C Achar <[email protected]>
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 <[email protected]>
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>
e6f6f9e6 24#include <blk.h>
a1596438
US
25#include <ext_common.h>
26#include <ext4fs.h>
a1596438 27#include "ext4_common.h"
9e374e7b 28#include <div64.h>
336d4615 29#include <malloc.h>
e6f6f9e6 30#include <part.h>
ba06b3c5 31#include <uuid.h>
a1596438
US
32
33int ext4fs_symlinknest;
94501062 34struct ext_filesystem ext_fs;
a1596438
US
35
36struct ext_filesystem *get_fs(void)
37{
94501062 38 return &ext_fs;
a1596438
US
39}
40
41void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
42{
43 if ((node != &ext4fs_root->diropen) && (node != currroot))
44 free(node);
45}
46
47/*
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
51 */
9f12cd0e
SR
52int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
53 loff_t len, char *buf, loff_t *actread)
a1596438 54{
50ce4c07 55 struct ext_filesystem *fs = get_fs();
a1596438 56 int i;
04735e9c 57 lbaint_t blockcnt;
50ce4c07
EE
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));
7f101be3 61 unsigned int filesize = le32_to_cpu(node->inode.size);
04735e9c
FL
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;
a1596438 67 char *delayed_buf = NULL;
e205896c 68 char *start_buf = buf;
a1596438 69 short status;
d5aee659
SW
70 struct ext_block_cache cache;
71
72 ext_cache_init(&cache);
a1596438
US
73
74 /* Adjust len so it we can't read past the end of the file. */
66a47ff2
SB
75 if (len + pos > filesize)
76 len = (filesize - pos);
a1596438 77
878269db
PE
78 if (blocksize <= 0 || len <= 0) {
79 ext_cache_fini(&cache);
80 return -1;
81 }
82
9e374e7b 83 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
a1596438 84
9e374e7b 85 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
509b498a 86 long int blknr;
9e374e7b 87 int blockoff = pos - (blocksize * i);
a1596438
US
88 int blockend = blocksize;
89 int skipfirst = 0;
d5aee659
SW
90 blknr = read_allocated_block(&node->inode, i, &cache);
91 if (blknr < 0) {
92 ext_cache_fini(&cache);
715b56fe 93 return -1;
d5aee659 94 }
a1596438 95
50ce4c07 96 blknr = blknr << log2_fs_blocksize;
a1596438
US
97
98 /* Last block. */
99 if (i == blockcnt - 1) {
9e374e7b 100 blockend = (len + pos) - (blocksize * i);
a1596438
US
101
102 /* The last portion is exactly blocksize. */
103 if (!blockend)
104 blockend = blocksize;
105 }
106
107 /* First block. */
9e374e7b 108 if (i == lldiv(pos, blocksize)) {
a1596438
US
109 skipfirst = blockoff;
110 blockend -= skipfirst;
111 }
112 if (blknr) {
113 int status;
114
115 if (previous_block_number != -1) {
116 if (delayed_next == blknr) {
117 delayed_extent += blockend;
50ce4c07 118 delayed_next += blockend >> log2blksz;
a1596438
US
119 } else { /* spill */
120 status = ext4fs_devread(delayed_start,
121 delayed_skipfirst,
122 delayed_extent,
123 delayed_buf);
d5aee659
SW
124 if (status == 0) {
125 ext_cache_fini(&cache);
715b56fe 126 return -1;
d5aee659 127 }
a1596438
US
128 previous_block_number = blknr;
129 delayed_start = blknr;
130 delayed_extent = blockend;
131 delayed_skipfirst = skipfirst;
132 delayed_buf = buf;
133 delayed_next = blknr +
50ce4c07 134 (blockend >> log2blksz);
a1596438
US
135 }
136 } else {
137 previous_block_number = blknr;
138 delayed_start = blknr;
139 delayed_extent = blockend;
140 delayed_skipfirst = skipfirst;
141 delayed_buf = buf;
142 delayed_next = blknr +
50ce4c07 143 (blockend >> log2blksz);
a1596438
US
144 }
145 } else {
ecdfb419 146 int n;
e205896c 147 int n_left;
a1596438
US
148 if (previous_block_number != -1) {
149 /* spill */
150 status = ext4fs_devread(delayed_start,
151 delayed_skipfirst,
152 delayed_extent,
153 delayed_buf);
d5aee659
SW
154 if (status == 0) {
155 ext_cache_fini(&cache);
715b56fe 156 return -1;
d5aee659 157 }
a1596438
US
158 previous_block_number = -1;
159 }
ecdfb419
IR
160 /* Zero no more than `len' bytes. */
161 n = blocksize - skipfirst;
e205896c
PE
162 n_left = len - ( buf - start_buf );
163 if (n > n_left)
164 n = n_left;
ecdfb419 165 memset(buf, 0, n);
a1596438
US
166 }
167 buf += blocksize - skipfirst;
168 }
169 if (previous_block_number != -1) {
170 /* spill */
171 status = ext4fs_devread(delayed_start,
172 delayed_skipfirst, delayed_extent,
173 delayed_buf);
d5aee659
SW
174 if (status == 0) {
175 ext_cache_fini(&cache);
715b56fe 176 return -1;
d5aee659 177 }
a1596438
US
178 previous_block_number = -1;
179 }
180
9f12cd0e 181 *actread = len;
d5aee659 182 ext_cache_fini(&cache);
9f12cd0e 183 return 0;
a1596438
US
184}
185
186int ext4fs_ls(const char *dirname)
187{
e71a969c 188 struct ext2fs_node *dirnode = NULL;
a1596438
US
189 int status;
190
191 if (dirname == NULL)
192 return 0;
193
194 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
195 FILETYPE_DIRECTORY);
196 if (status != 1) {
197 printf("** Can not find directory. **\n");
e71a969c
EH
198 if (dirnode)
199 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
a1596438
US
200 return 1;
201 }
202
203 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
204 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
205
206 return 0;
207}
208
55af5c93
SW
209int ext4fs_exists(const char *filename)
210{
9f12cd0e
SR
211 loff_t file_len;
212 int ret;
55af5c93 213
9f12cd0e
SR
214 ret = ext4fs_open(filename, &file_len);
215 return ret == 0;
55af5c93
SW
216}
217
d455d878 218int ext4fs_size(const char *filename, loff_t *size)
cf659819 219{
d455d878 220 return ext4fs_open(filename, size);
cf659819
SW
221}
222
66a47ff2 223int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
a1596438
US
224{
225 if (ext4fs_root == NULL || ext4fs_file == NULL)
66a47ff2 226 return -1;
a1596438 227
66a47ff2 228 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
a1596438 229}
e6d52415 230
4101f687 231int ext4fs_probe(struct blk_desc *fs_dev_desc,
0528979f 232 struct disk_partition *fs_partition)
e6d52415
SG
233{
234 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
235
236 if (!ext4fs_mount(fs_partition->size)) {
237 ext4fs_close();
238 return -1;
239 }
240
241 return 0;
242}
243
d455d878
SR
244int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
245 loff_t *len_read)
e6d52415 246{
9f12cd0e 247 loff_t file_len;
9f12cd0e 248 int ret;
e6d52415 249
9f12cd0e
SR
250 ret = ext4fs_open(filename, &file_len);
251 if (ret < 0) {
e6d52415
SG
252 printf("** File not found %s **\n", filename);
253 return -1;
254 }
255
256 if (len == 0)
257 len = file_len;
258
66a47ff2 259 return ext4fs_read(buf, offset, len, len_read);
e6d52415 260}
59e890ef
CG
261
262int ext4fs_uuid(char *uuid_str)
263{
264 if (ext4fs_root == NULL)
265 return -1;
266
267#ifdef CONFIG_LIB_UUID
268 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
269 uuid_str, UUID_STR_FORMAT_STD);
270
271 return 0;
272#else
273 return -ENOSYS;
274#endif
275}
d5aee659
SW
276
277void ext_cache_init(struct ext_block_cache *cache)
278{
279 memset(cache, 0, sizeof(*cache));
280}
281
282void ext_cache_fini(struct ext_block_cache *cache)
283{
284 free(cache->buf);
285 ext_cache_init(cache);
286}
287
288int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
289{
290 /* This could be more lenient, but this is simple and enough for now */
291 if (cache->buf && cache->block == block && cache->size == size)
292 return 1;
293 ext_cache_fini(cache);
7b83060b 294 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
d5aee659
SW
295 if (!cache->buf)
296 return 0;
297 if (!ext4fs_devread(block, 0, size, cache->buf)) {
6e5a79de 298 ext_cache_fini(cache);
d5aee659
SW
299 return 0;
300 }
301 cache->block = block;
302 cache->size = size;
303 return 1;
304}
This page took 0.422243 seconds and 4 git commands to generate.