blob: 96ca276839e3733b113b4e95ad09396ede64ea9a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Uma Shankara1596432012-05-25 21:21:44 +05302/*
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 *
Uma Shankared34f342012-05-25 21:22:49 +053020 * ext4write : Based on generic ext4 protocol.
Uma Shankara1596432012-05-25 21:21:44 +053021 */
22
23#include <common.h>
Uma Shankara1596432012-05-25 21:21:44 +053024#include <ext_common.h>
25#include <ext4fs.h>
Uma Shankara1596432012-05-25 21:21:44 +053026#include "ext4_common.h"
Tom Rini9e374e72014-11-24 11:50:46 -050027#include <div64.h>
Simon Glass336d4612020-02-03 07:36:16 -070028#include <malloc.h>
Simon Glassba06b3c2020-05-10 11:39:52 -060029#include <uuid.h>
Uma Shankara1596432012-05-25 21:21:44 +053030
31int ext4fs_symlinknest;
Rob Herring94501062012-08-23 11:31:45 +000032struct ext_filesystem ext_fs;
Uma Shankara1596432012-05-25 21:21:44 +053033
34struct ext_filesystem *get_fs(void)
35{
Rob Herring94501062012-08-23 11:31:45 +000036 return &ext_fs;
Uma Shankara1596432012-05-25 21:21:44 +053037}
38
39void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
40{
41 if ((node != &ext4fs_root->diropen) && (node != currroot))
42 free(node);
43}
44
45/*
46 * Taken from openmoko-kernel mailing list: By Andy green
47 * Optimized read file API : collects and defers contiguous sector
48 * reads into one potentially more efficient larger sequential read action
49 */
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080050int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
51 loff_t len, char *buf, loff_t *actread)
Uma Shankara1596432012-05-25 21:21:44 +053052{
Egbert Eich50ce4c02013-05-01 01:13:19 +000053 struct ext_filesystem *fs = get_fs();
Uma Shankara1596432012-05-25 21:21:44 +053054 int i;
Frederic Leroy04735e92013-06-26 18:11:25 +020055 lbaint_t blockcnt;
Egbert Eich50ce4c02013-05-01 01:13:19 +000056 int log2blksz = fs->dev_desc->log2blksz;
57 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
58 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
Michael Walle7f101be2016-08-29 10:46:44 +020059 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroy04735e92013-06-26 18:11:25 +020060 lbaint_t previous_block_number = -1;
61 lbaint_t delayed_start = 0;
62 lbaint_t delayed_extent = 0;
63 lbaint_t delayed_skipfirst = 0;
64 lbaint_t delayed_next = 0;
Uma Shankara1596432012-05-25 21:21:44 +053065 char *delayed_buf = NULL;
Paul Emgee2058962019-07-08 16:37:07 -070066 char *start_buf = buf;
Uma Shankara1596432012-05-25 21:21:44 +053067 short status;
Stephen Warrend5aee652019-01-30 12:58:05 -070068 struct ext_block_cache cache;
69
70 ext_cache_init(&cache);
Uma Shankara1596432012-05-25 21:21:44 +053071
72 /* Adjust len so it we can't read past the end of the file. */
Stefan Brüns66a47ff2016-11-06 18:33:57 +010073 if (len + pos > filesize)
74 len = (filesize - pos);
Uma Shankara1596432012-05-25 21:21:44 +053075
Paul Emge878269d2019-07-08 16:37:05 -070076 if (blocksize <= 0 || len <= 0) {
77 ext_cache_fini(&cache);
78 return -1;
79 }
80
Tom Rini9e374e72014-11-24 11:50:46 -050081 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankara1596432012-05-25 21:21:44 +053082
Tom Rini9e374e72014-11-24 11:50:46 -050083 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutla509b4982017-04-26 16:58:22 +053084 long int blknr;
Tom Rini9e374e72014-11-24 11:50:46 -050085 int blockoff = pos - (blocksize * i);
Uma Shankara1596432012-05-25 21:21:44 +053086 int blockend = blocksize;
87 int skipfirst = 0;
Stephen Warrend5aee652019-01-30 12:58:05 -070088 blknr = read_allocated_block(&node->inode, i, &cache);
89 if (blknr < 0) {
90 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -050091 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -070092 }
Uma Shankara1596432012-05-25 21:21:44 +053093
Egbert Eich50ce4c02013-05-01 01:13:19 +000094 blknr = blknr << log2_fs_blocksize;
Uma Shankara1596432012-05-25 21:21:44 +053095
96 /* Last block. */
97 if (i == blockcnt - 1) {
Tom Rini9e374e72014-11-24 11:50:46 -050098 blockend = (len + pos) - (blocksize * i);
Uma Shankara1596432012-05-25 21:21:44 +053099
100 /* The last portion is exactly blocksize. */
101 if (!blockend)
102 blockend = blocksize;
103 }
104
105 /* First block. */
Tom Rini9e374e72014-11-24 11:50:46 -0500106 if (i == lldiv(pos, blocksize)) {
Uma Shankara1596432012-05-25 21:21:44 +0530107 skipfirst = blockoff;
108 blockend -= skipfirst;
109 }
110 if (blknr) {
111 int status;
112
113 if (previous_block_number != -1) {
114 if (delayed_next == blknr) {
115 delayed_extent += blockend;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000116 delayed_next += blockend >> log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +0530117 } else { /* spill */
118 status = ext4fs_devread(delayed_start,
119 delayed_skipfirst,
120 delayed_extent,
121 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700122 if (status == 0) {
123 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500124 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700125 }
Uma Shankara1596432012-05-25 21:21:44 +0530126 previous_block_number = blknr;
127 delayed_start = blknr;
128 delayed_extent = blockend;
129 delayed_skipfirst = skipfirst;
130 delayed_buf = buf;
131 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000132 (blockend >> log2blksz);
Uma Shankara1596432012-05-25 21:21:44 +0530133 }
134 } else {
135 previous_block_number = blknr;
136 delayed_start = blknr;
137 delayed_extent = blockend;
138 delayed_skipfirst = skipfirst;
139 delayed_buf = buf;
140 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000141 (blockend >> log2blksz);
Uma Shankara1596432012-05-25 21:21:44 +0530142 }
143 } else {
Ian Rayecdfb412017-11-08 15:35:10 +0000144 int n;
Paul Emgee2058962019-07-08 16:37:07 -0700145 int n_left;
Uma Shankara1596432012-05-25 21:21:44 +0530146 if (previous_block_number != -1) {
147 /* spill */
148 status = ext4fs_devread(delayed_start,
149 delayed_skipfirst,
150 delayed_extent,
151 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700152 if (status == 0) {
153 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500154 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700155 }
Uma Shankara1596432012-05-25 21:21:44 +0530156 previous_block_number = -1;
157 }
Ian Rayecdfb412017-11-08 15:35:10 +0000158 /* Zero no more than `len' bytes. */
159 n = blocksize - skipfirst;
Paul Emgee2058962019-07-08 16:37:07 -0700160 n_left = len - ( buf - start_buf );
161 if (n > n_left)
162 n = n_left;
Ian Rayecdfb412017-11-08 15:35:10 +0000163 memset(buf, 0, n);
Uma Shankara1596432012-05-25 21:21:44 +0530164 }
165 buf += blocksize - skipfirst;
166 }
167 if (previous_block_number != -1) {
168 /* spill */
169 status = ext4fs_devread(delayed_start,
170 delayed_skipfirst, delayed_extent,
171 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700172 if (status == 0) {
173 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500174 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700175 }
Uma Shankara1596432012-05-25 21:21:44 +0530176 previous_block_number = -1;
177 }
178
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800179 *actread = len;
Stephen Warrend5aee652019-01-30 12:58:05 -0700180 ext_cache_fini(&cache);
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800181 return 0;
Uma Shankara1596432012-05-25 21:21:44 +0530182}
183
184int ext4fs_ls(const char *dirname)
185{
Eugen Hristeve71a9692018-05-09 16:28:37 +0300186 struct ext2fs_node *dirnode = NULL;
Uma Shankara1596432012-05-25 21:21:44 +0530187 int status;
188
189 if (dirname == NULL)
190 return 0;
191
192 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
193 FILETYPE_DIRECTORY);
194 if (status != 1) {
195 printf("** Can not find directory. **\n");
Eugen Hristeve71a9692018-05-09 16:28:37 +0300196 if (dirnode)
197 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
Uma Shankara1596432012-05-25 21:21:44 +0530198 return 1;
199 }
200
201 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
202 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
203
204 return 0;
205}
206
Stephen Warren55af5c92014-02-03 13:21:09 -0700207int ext4fs_exists(const char *filename)
208{
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800209 loff_t file_len;
210 int ret;
Stephen Warren55af5c92014-02-03 13:21:09 -0700211
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800212 ret = ext4fs_open(filename, &file_len);
213 return ret == 0;
Stephen Warren55af5c92014-02-03 13:21:09 -0700214}
215
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800216int ext4fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600217{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800218 return ext4fs_open(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600219}
220
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100221int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankara1596432012-05-25 21:21:44 +0530222{
223 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100224 return -1;
Uma Shankara1596432012-05-25 21:21:44 +0530225
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100226 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankara1596432012-05-25 21:21:44 +0530227}
Simon Glasse6d52412012-12-26 09:53:33 +0000228
Simon Glass4101f682016-02-29 15:25:34 -0700229int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glasse6d52412012-12-26 09:53:33 +0000230 disk_partition_t *fs_partition)
231{
232 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
233
234 if (!ext4fs_mount(fs_partition->size)) {
235 ext4fs_close();
236 return -1;
237 }
238
239 return 0;
240}
241
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800242int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
243 loff_t *len_read)
Simon Glasse6d52412012-12-26 09:53:33 +0000244{
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800245 loff_t file_len;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800246 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +0000247
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800248 ret = ext4fs_open(filename, &file_len);
249 if (ret < 0) {
Simon Glasse6d52412012-12-26 09:53:33 +0000250 printf("** File not found %s **\n", filename);
251 return -1;
252 }
253
254 if (len == 0)
255 len = file_len;
256
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100257 return ext4fs_read(buf, offset, len, len_read);
Simon Glasse6d52412012-12-26 09:53:33 +0000258}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100259
260int ext4fs_uuid(char *uuid_str)
261{
262 if (ext4fs_root == NULL)
263 return -1;
264
265#ifdef CONFIG_LIB_UUID
266 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
267 uuid_str, UUID_STR_FORMAT_STD);
268
269 return 0;
270#else
271 return -ENOSYS;
272#endif
273}
Stephen Warrend5aee652019-01-30 12:58:05 -0700274
275void ext_cache_init(struct ext_block_cache *cache)
276{
277 memset(cache, 0, sizeof(*cache));
278}
279
280void ext_cache_fini(struct ext_block_cache *cache)
281{
282 free(cache->buf);
283 ext_cache_init(cache);
284}
285
286int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
287{
288 /* This could be more lenient, but this is simple and enough for now */
289 if (cache->buf && cache->block == block && cache->size == size)
290 return 1;
291 ext_cache_fini(cache);
Jan Kiszka7b830602020-03-25 21:27:51 +0100292 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
Stephen Warrend5aee652019-01-30 12:58:05 -0700293 if (!cache->buf)
294 return 0;
295 if (!ext4fs_devread(block, 0, size, cache->buf)) {
Paul Emge6e5a79d2019-07-08 16:37:04 -0700296 ext_cache_fini(cache);
Stephen Warrend5aee652019-01-30 12:58:05 -0700297 return 0;
298 }
299 cache->block = block;
300 cache->size = size;
301 return 1;
302}