SPDX: Convert all of our single license tags to Linux Kernel style
[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;
65
ecdfb419
IR
66 if (blocksize <= 0)
67 return -1;
68
a1596438 69 /* Adjust len so it we can't read past the end of the file. */
66a47ff2
SB
70 if (len + pos > filesize)
71 len = (filesize - pos);
a1596438 72
9e374e7b 73 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
a1596438 74
9e374e7b 75 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
509b498a 76 long int blknr;
9e374e7b 77 int blockoff = pos - (blocksize * i);
a1596438
US
78 int blockend = blocksize;
79 int skipfirst = 0;
80 blknr = read_allocated_block(&(node->inode), i);
715b56fe
TR
81 if (blknr < 0)
82 return -1;
a1596438 83
50ce4c07 84 blknr = blknr << log2_fs_blocksize;
a1596438
US
85
86 /* Last block. */
87 if (i == blockcnt - 1) {
9e374e7b 88 blockend = (len + pos) - (blocksize * i);
a1596438
US
89
90 /* The last portion is exactly blocksize. */
91 if (!blockend)
92 blockend = blocksize;
93 }
94
95 /* First block. */
9e374e7b 96 if (i == lldiv(pos, blocksize)) {
a1596438
US
97 skipfirst = blockoff;
98 blockend -= skipfirst;
99 }
100 if (blknr) {
101 int status;
102
103 if (previous_block_number != -1) {
104 if (delayed_next == blknr) {
105 delayed_extent += blockend;
50ce4c07 106 delayed_next += blockend >> log2blksz;
a1596438
US
107 } else { /* spill */
108 status = ext4fs_devread(delayed_start,
109 delayed_skipfirst,
110 delayed_extent,
111 delayed_buf);
715b56fe
TR
112 if (status == 0)
113 return -1;
a1596438
US
114 previous_block_number = blknr;
115 delayed_start = blknr;
116 delayed_extent = blockend;
117 delayed_skipfirst = skipfirst;
118 delayed_buf = buf;
119 delayed_next = blknr +
50ce4c07 120 (blockend >> log2blksz);
a1596438
US
121 }
122 } else {
123 previous_block_number = blknr;
124 delayed_start = blknr;
125 delayed_extent = blockend;
126 delayed_skipfirst = skipfirst;
127 delayed_buf = buf;
128 delayed_next = blknr +
50ce4c07 129 (blockend >> log2blksz);
a1596438
US
130 }
131 } else {
ecdfb419 132 int n;
a1596438
US
133 if (previous_block_number != -1) {
134 /* spill */
135 status = ext4fs_devread(delayed_start,
136 delayed_skipfirst,
137 delayed_extent,
138 delayed_buf);
715b56fe
TR
139 if (status == 0)
140 return -1;
a1596438
US
141 previous_block_number = -1;
142 }
ecdfb419
IR
143 /* Zero no more than `len' bytes. */
144 n = blocksize - skipfirst;
145 if (n > len)
146 n = len;
147 memset(buf, 0, n);
a1596438
US
148 }
149 buf += blocksize - skipfirst;
150 }
151 if (previous_block_number != -1) {
152 /* spill */
153 status = ext4fs_devread(delayed_start,
154 delayed_skipfirst, delayed_extent,
155 delayed_buf);
715b56fe
TR
156 if (status == 0)
157 return -1;
a1596438
US
158 previous_block_number = -1;
159 }
160
9f12cd0e
SR
161 *actread = len;
162 return 0;
a1596438
US
163}
164
165int ext4fs_ls(const char *dirname)
166{
167 struct ext2fs_node *dirnode;
168 int status;
169
170 if (dirname == NULL)
171 return 0;
172
173 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
174 FILETYPE_DIRECTORY);
175 if (status != 1) {
176 printf("** Can not find directory. **\n");
fa9ca8a5 177 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
a1596438
US
178 return 1;
179 }
180
181 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
182 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
183
184 return 0;
185}
186
55af5c93
SW
187int ext4fs_exists(const char *filename)
188{
9f12cd0e
SR
189 loff_t file_len;
190 int ret;
55af5c93 191
9f12cd0e
SR
192 ret = ext4fs_open(filename, &file_len);
193 return ret == 0;
55af5c93
SW
194}
195
d455d878 196int ext4fs_size(const char *filename, loff_t *size)
cf659819 197{
d455d878 198 return ext4fs_open(filename, size);
cf659819
SW
199}
200
66a47ff2 201int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
a1596438
US
202{
203 if (ext4fs_root == NULL || ext4fs_file == NULL)
66a47ff2 204 return -1;
a1596438 205
66a47ff2 206 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
a1596438 207}
e6d52415 208
4101f687 209int ext4fs_probe(struct blk_desc *fs_dev_desc,
e6d52415
SG
210 disk_partition_t *fs_partition)
211{
212 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
213
214 if (!ext4fs_mount(fs_partition->size)) {
215 ext4fs_close();
216 return -1;
217 }
218
219 return 0;
220}
221
d455d878
SR
222int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
223 loff_t *len_read)
e6d52415 224{
9f12cd0e 225 loff_t file_len;
9f12cd0e 226 int ret;
e6d52415 227
9f12cd0e
SR
228 ret = ext4fs_open(filename, &file_len);
229 if (ret < 0) {
e6d52415
SG
230 printf("** File not found %s **\n", filename);
231 return -1;
232 }
233
234 if (len == 0)
235 len = file_len;
236
66a47ff2 237 return ext4fs_read(buf, offset, len, len_read);
e6d52415 238}
59e890ef
CG
239
240int ext4fs_uuid(char *uuid_str)
241{
242 if (ext4fs_root == NULL)
243 return -1;
244
245#ifdef CONFIG_LIB_UUID
246 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
247 uuid_str, UUID_STR_FORMAT_STD);
248
249 return 0;
250#else
251 return -ENOSYS;
252#endif
253}
This page took 0.190791 seconds and 4 git commands to generate.