blob: e2b740cac4057f8115ca7749c7b99f08c91b2cf7 [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>
Uma Shankara1596432012-05-25 21:21:44 +053028
29int ext4fs_symlinknest;
Rob Herring94501062012-08-23 11:31:45 +000030struct ext_filesystem ext_fs;
Uma Shankara1596432012-05-25 21:21:44 +053031
32struct ext_filesystem *get_fs(void)
33{
Rob Herring94501062012-08-23 11:31:45 +000034 return &ext_fs;
Uma Shankara1596432012-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 */
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -080048int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
49 loff_t len, char *buf, loff_t *actread)
Uma Shankara1596432012-05-25 21:21:44 +053050{
Egbert Eich50ce4c02013-05-01 01:13:19 +000051 struct ext_filesystem *fs = get_fs();
Uma Shankara1596432012-05-25 21:21:44 +053052 int i;
Frederic Leroy04735e92013-06-26 18:11:25 +020053 lbaint_t blockcnt;
Egbert Eich50ce4c02013-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));
Michael Walle7f101be2016-08-29 10:46:44 +020057 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroy04735e92013-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 Shankara1596432012-05-25 21:21:44 +053063 char *delayed_buf = NULL;
64 short status;
Stephen Warrend5aee652019-01-30 12:58:05 -070065 struct ext_block_cache cache;
66
67 ext_cache_init(&cache);
Uma Shankara1596432012-05-25 21:21:44 +053068
69 /* Adjust len so it we can't read past the end of the file. */
Stefan Brüns66a47ff2016-11-06 18:33:57 +010070 if (len + pos > filesize)
71 len = (filesize - pos);
Uma Shankara1596432012-05-25 21:21:44 +053072
Paul Emge878269d2019-07-08 16:37:05 -070073 if (blocksize <= 0 || len <= 0) {
74 ext_cache_fini(&cache);
75 return -1;
76 }
77
Tom Rini9e374e72014-11-24 11:50:46 -050078 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankara1596432012-05-25 21:21:44 +053079
Tom Rini9e374e72014-11-24 11:50:46 -050080 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutla509b4982017-04-26 16:58:22 +053081 long int blknr;
Tom Rini9e374e72014-11-24 11:50:46 -050082 int blockoff = pos - (blocksize * i);
Uma Shankara1596432012-05-25 21:21:44 +053083 int blockend = blocksize;
84 int skipfirst = 0;
Stephen Warrend5aee652019-01-30 12:58:05 -070085 blknr = read_allocated_block(&node->inode, i, &cache);
86 if (blknr < 0) {
87 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -050088 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -070089 }
Uma Shankara1596432012-05-25 21:21:44 +053090
Egbert Eich50ce4c02013-05-01 01:13:19 +000091 blknr = blknr << log2_fs_blocksize;
Uma Shankara1596432012-05-25 21:21:44 +053092
93 /* Last block. */
94 if (i == blockcnt - 1) {
Tom Rini9e374e72014-11-24 11:50:46 -050095 blockend = (len + pos) - (blocksize * i);
Uma Shankara1596432012-05-25 21:21:44 +053096
97 /* The last portion is exactly blocksize. */
98 if (!blockend)
99 blockend = blocksize;
100 }
101
102 /* First block. */
Tom Rini9e374e72014-11-24 11:50:46 -0500103 if (i == lldiv(pos, blocksize)) {
Uma Shankara1596432012-05-25 21:21:44 +0530104 skipfirst = blockoff;
105 blockend -= skipfirst;
106 }
107 if (blknr) {
108 int status;
109
110 if (previous_block_number != -1) {
111 if (delayed_next == blknr) {
112 delayed_extent += blockend;
Egbert Eich50ce4c02013-05-01 01:13:19 +0000113 delayed_next += blockend >> log2blksz;
Uma Shankara1596432012-05-25 21:21:44 +0530114 } else { /* spill */
115 status = ext4fs_devread(delayed_start,
116 delayed_skipfirst,
117 delayed_extent,
118 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700119 if (status == 0) {
120 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500121 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700122 }
Uma Shankara1596432012-05-25 21:21:44 +0530123 previous_block_number = blknr;
124 delayed_start = blknr;
125 delayed_extent = blockend;
126 delayed_skipfirst = skipfirst;
127 delayed_buf = buf;
128 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000129 (blockend >> log2blksz);
Uma Shankara1596432012-05-25 21:21:44 +0530130 }
131 } else {
132 previous_block_number = blknr;
133 delayed_start = blknr;
134 delayed_extent = blockend;
135 delayed_skipfirst = skipfirst;
136 delayed_buf = buf;
137 delayed_next = blknr +
Egbert Eich50ce4c02013-05-01 01:13:19 +0000138 (blockend >> log2blksz);
Uma Shankara1596432012-05-25 21:21:44 +0530139 }
140 } else {
Ian Rayecdfb412017-11-08 15:35:10 +0000141 int n;
Uma Shankara1596432012-05-25 21:21:44 +0530142 if (previous_block_number != -1) {
143 /* spill */
144 status = ext4fs_devread(delayed_start,
145 delayed_skipfirst,
146 delayed_extent,
147 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700148 if (status == 0) {
149 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500150 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700151 }
Uma Shankara1596432012-05-25 21:21:44 +0530152 previous_block_number = -1;
153 }
Ian Rayecdfb412017-11-08 15:35:10 +0000154 /* Zero no more than `len' bytes. */
155 n = blocksize - skipfirst;
156 if (n > len)
157 n = len;
158 memset(buf, 0, n);
Uma Shankara1596432012-05-25 21:21:44 +0530159 }
160 buf += blocksize - skipfirst;
161 }
162 if (previous_block_number != -1) {
163 /* spill */
164 status = ext4fs_devread(delayed_start,
165 delayed_skipfirst, delayed_extent,
166 delayed_buf);
Stephen Warrend5aee652019-01-30 12:58:05 -0700167 if (status == 0) {
168 ext_cache_fini(&cache);
Tom Rini715b56f2014-02-26 08:18:58 -0500169 return -1;
Stephen Warrend5aee652019-01-30 12:58:05 -0700170 }
Uma Shankara1596432012-05-25 21:21:44 +0530171 previous_block_number = -1;
172 }
173
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800174 *actread = len;
Stephen Warrend5aee652019-01-30 12:58:05 -0700175 ext_cache_fini(&cache);
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800176 return 0;
Uma Shankara1596432012-05-25 21:21:44 +0530177}
178
179int ext4fs_ls(const char *dirname)
180{
Eugen Hristeve71a9692018-05-09 16:28:37 +0300181 struct ext2fs_node *dirnode = NULL;
Uma Shankara1596432012-05-25 21:21:44 +0530182 int status;
183
184 if (dirname == NULL)
185 return 0;
186
187 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
188 FILETYPE_DIRECTORY);
189 if (status != 1) {
190 printf("** Can not find directory. **\n");
Eugen Hristeve71a9692018-05-09 16:28:37 +0300191 if (dirnode)
192 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
Uma Shankara1596432012-05-25 21:21:44 +0530193 return 1;
194 }
195
196 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
197 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
198
199 return 0;
200}
201
Stephen Warren55af5c92014-02-03 13:21:09 -0700202int ext4fs_exists(const char *filename)
203{
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800204 loff_t file_len;
205 int ret;
Stephen Warren55af5c92014-02-03 13:21:09 -0700206
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800207 ret = ext4fs_open(filename, &file_len);
208 return ret == 0;
Stephen Warren55af5c92014-02-03 13:21:09 -0700209}
210
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800211int ext4fs_size(const char *filename, loff_t *size)
Stephen Warrencf659812014-06-11 12:47:26 -0600212{
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800213 return ext4fs_open(filename, size);
Stephen Warrencf659812014-06-11 12:47:26 -0600214}
215
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100216int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankara1596432012-05-25 21:21:44 +0530217{
218 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100219 return -1;
Uma Shankara1596432012-05-25 21:21:44 +0530220
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100221 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankara1596432012-05-25 21:21:44 +0530222}
Simon Glasse6d52412012-12-26 09:53:33 +0000223
Simon Glass4101f682016-02-29 15:25:34 -0700224int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glasse6d52412012-12-26 09:53:33 +0000225 disk_partition_t *fs_partition)
226{
227 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
228
229 if (!ext4fs_mount(fs_partition->size)) {
230 ext4fs_close();
231 return -1;
232 }
233
234 return 0;
235}
236
Suriyan Ramasamid455d872014-11-17 14:39:38 -0800237int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
238 loff_t *len_read)
Simon Glasse6d52412012-12-26 09:53:33 +0000239{
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800240 loff_t file_len;
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800241 int ret;
Simon Glasse6d52412012-12-26 09:53:33 +0000242
Suriyan Ramasami9f12cd02014-11-17 14:39:36 -0800243 ret = ext4fs_open(filename, &file_len);
244 if (ret < 0) {
Simon Glasse6d52412012-12-26 09:53:33 +0000245 printf("** File not found %s **\n", filename);
246 return -1;
247 }
248
249 if (len == 0)
250 len = file_len;
251
Stefan Brüns66a47ff2016-11-06 18:33:57 +0100252 return ext4fs_read(buf, offset, len, len_read);
Simon Glasse6d52412012-12-26 09:53:33 +0000253}
Christian Gmeiner59e890e2014-11-12 14:35:04 +0100254
255int ext4fs_uuid(char *uuid_str)
256{
257 if (ext4fs_root == NULL)
258 return -1;
259
260#ifdef CONFIG_LIB_UUID
261 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
262 uuid_str, UUID_STR_FORMAT_STD);
263
264 return 0;
265#else
266 return -ENOSYS;
267#endif
268}
Stephen Warrend5aee652019-01-30 12:58:05 -0700269
270void ext_cache_init(struct ext_block_cache *cache)
271{
272 memset(cache, 0, sizeof(*cache));
273}
274
275void ext_cache_fini(struct ext_block_cache *cache)
276{
277 free(cache->buf);
278 ext_cache_init(cache);
279}
280
281int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
282{
283 /* This could be more lenient, but this is simple and enough for now */
284 if (cache->buf && cache->block == block && cache->size == size)
285 return 1;
286 ext_cache_fini(cache);
287 cache->buf = malloc(size);
288 if (!cache->buf)
289 return 0;
290 if (!ext4fs_devread(block, 0, size, cache->buf)) {
Paul Emge6e5a79d2019-07-08 16:37:04 -0700291 ext_cache_fini(cache);
Stephen Warrend5aee652019-01-30 12:58:05 -0700292 return 0;
293 }
294 cache->block = block;
295 cache->size = size;
296 return 1;
297}