blob: 33e200ffa3c5e1c7c5bc50aaeff1b8ec6a326d29 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Uma Shankar71014b62012-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 Shankara74a99a2012-05-25 21:22:49 +053020 * ext4write : Based on generic ext4 protocol.
Uma Shankar71014b62012-05-25 21:21:44 +053021 */
22
23#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060024#include <blk.h>
Uma Shankar71014b62012-05-25 21:21:44 +053025#include <ext_common.h>
26#include <ext4fs.h>
Uma Shankar71014b62012-05-25 21:21:44 +053027#include "ext4_common.h"
Tom Rinia17b7bc2014-11-24 11:50:46 -050028#include <div64.h>
Simon Glass9bc15642020-02-03 07:36:16 -070029#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060030#include <part.h>
Simon Glass6f044982020-05-10 11:39:52 -060031#include <uuid.h>
Uma Shankar71014b62012-05-25 21:21:44 +053032
33int ext4fs_symlinknest;
Rob Herring30f7fd72012-08-23 11:31:45 +000034struct ext_filesystem ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053035
36struct ext_filesystem *get_fs(void)
37{
Rob Herring30f7fd72012-08-23 11:31:45 +000038 return &ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053039}
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 */
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080052int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
53 loff_t len, char *buf, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +053054{
Egbert Eich7b1b2552013-05-01 01:13:19 +000055 struct ext_filesystem *fs = get_fs();
Uma Shankar71014b62012-05-25 21:21:44 +053056 int i;
Frederic Leroye7ee0282013-06-26 18:11:25 +020057 lbaint_t blockcnt;
Egbert Eich7b1b2552013-05-01 01:13:19 +000058 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));
Michael Wallec07cdcb2016-08-29 10:46:44 +020061 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroye7ee0282013-06-26 18:11:25 +020062 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;
Uma Shankar71014b62012-05-25 21:21:44 +053067 char *delayed_buf = NULL;
Paul Emge627e3c82019-07-08 16:37:07 -070068 char *start_buf = buf;
Uma Shankar71014b62012-05-25 21:21:44 +053069 short status;
Stephen Warren02d6ca72019-01-30 12:58:05 -070070 struct ext_block_cache cache;
71
72 ext_cache_init(&cache);
Uma Shankar71014b62012-05-25 21:21:44 +053073
74 /* Adjust len so it we can't read past the end of the file. */
Stefan Brünsdb5862d2016-11-06 18:33:57 +010075 if (len + pos > filesize)
76 len = (filesize - pos);
Uma Shankar71014b62012-05-25 21:21:44 +053077
Paul Emgea4a48ef2019-07-08 16:37:05 -070078 if (blocksize <= 0 || len <= 0) {
79 ext_cache_fini(&cache);
80 return -1;
81 }
82
Tom Rinia17b7bc2014-11-24 11:50:46 -050083 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankar71014b62012-05-25 21:21:44 +053084
Tom Rinia17b7bc2014-11-24 11:50:46 -050085 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutlac32ffa82017-04-26 16:58:22 +053086 long int blknr;
Tom Rinia17b7bc2014-11-24 11:50:46 -050087 int blockoff = pos - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +053088 int blockend = blocksize;
89 int skipfirst = 0;
Stephen Warren02d6ca72019-01-30 12:58:05 -070090 blknr = read_allocated_block(&node->inode, i, &cache);
91 if (blknr < 0) {
92 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -050093 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -070094 }
Uma Shankar71014b62012-05-25 21:21:44 +053095
Egbert Eich7b1b2552013-05-01 01:13:19 +000096 blknr = blknr << log2_fs_blocksize;
Uma Shankar71014b62012-05-25 21:21:44 +053097
98 /* Last block. */
99 if (i == blockcnt - 1) {
Tom Rinia17b7bc2014-11-24 11:50:46 -0500100 blockend = (len + pos) - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +0530101
102 /* The last portion is exactly blocksize. */
103 if (!blockend)
104 blockend = blocksize;
105 }
106
107 /* First block. */
Tom Rinia17b7bc2014-11-24 11:50:46 -0500108 if (i == lldiv(pos, blocksize)) {
Uma Shankar71014b62012-05-25 21:21:44 +0530109 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;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000118 delayed_next += blockend >> log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +0530119 } else { /* spill */
120 status = ext4fs_devread(delayed_start,
121 delayed_skipfirst,
122 delayed_extent,
123 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700124 if (status == 0) {
125 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500126 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700127 }
Uma Shankar71014b62012-05-25 21:21:44 +0530128 previous_block_number = blknr;
129 delayed_start = blknr;
130 delayed_extent = blockend;
131 delayed_skipfirst = skipfirst;
132 delayed_buf = buf;
133 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000134 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530135 }
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 +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000143 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530144 }
145 } else {
Ian Rayc45fcd52017-11-08 15:35:10 +0000146 int n;
Paul Emge627e3c82019-07-08 16:37:07 -0700147 int n_left;
Uma Shankar71014b62012-05-25 21:21:44 +0530148 if (previous_block_number != -1) {
149 /* spill */
150 status = ext4fs_devread(delayed_start,
151 delayed_skipfirst,
152 delayed_extent,
153 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700154 if (status == 0) {
155 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500156 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700157 }
Uma Shankar71014b62012-05-25 21:21:44 +0530158 previous_block_number = -1;
159 }
Ian Rayc45fcd52017-11-08 15:35:10 +0000160 /* Zero no more than `len' bytes. */
161 n = blocksize - skipfirst;
Paul Emge627e3c82019-07-08 16:37:07 -0700162 n_left = len - ( buf - start_buf );
163 if (n > n_left)
164 n = n_left;
Ian Rayc45fcd52017-11-08 15:35:10 +0000165 memset(buf, 0, n);
Uma Shankar71014b62012-05-25 21:21:44 +0530166 }
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);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700174 if (status == 0) {
175 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500176 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700177 }
Uma Shankar71014b62012-05-25 21:21:44 +0530178 previous_block_number = -1;
179 }
180
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800181 *actread = len;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700182 ext_cache_fini(&cache);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800183 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +0530184}
185
186int ext4fs_ls(const char *dirname)
187{
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300188 struct ext2fs_node *dirnode = NULL;
Uma Shankar71014b62012-05-25 21:21:44 +0530189 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");
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300198 if (dirnode)
199 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
Uma Shankar71014b62012-05-25 21:21:44 +0530200 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
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700209int ext4fs_exists(const char *filename)
210{
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100211 struct ext2fs_node *dirnode = NULL;
212 int filetype;
213
214 if (!filename)
215 return 0;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700216
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100217 return ext4fs_find_file1(filename, &ext4fs_root->diropen, &dirnode,
218 &filetype);
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700219}
220
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800221int ext4fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600222{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800223 return ext4fs_open(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600224}
225
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100226int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +0530227{
228 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100229 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530230
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100231 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankar71014b62012-05-25 21:21:44 +0530232}
Simon Glass19e38582012-12-26 09:53:33 +0000233
Simon Glasse3394752016-02-29 15:25:34 -0700234int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600235 struct disk_partition *fs_partition)
Simon Glass19e38582012-12-26 09:53:33 +0000236{
237 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
238
Sean Andersonab125f52023-11-08 12:51:09 -0500239 if (!ext4fs_mount()) {
Simon Glass19e38582012-12-26 09:53:33 +0000240 ext4fs_close();
241 return -1;
242 }
243
244 return 0;
245}
246
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800247int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
248 loff_t *len_read)
Simon Glass19e38582012-12-26 09:53:33 +0000249{
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800250 loff_t file_len;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800251 int ret;
Simon Glass19e38582012-12-26 09:53:33 +0000252
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800253 ret = ext4fs_open(filename, &file_len);
254 if (ret < 0) {
Simon Glass19e38582012-12-26 09:53:33 +0000255 printf("** File not found %s **\n", filename);
256 return -1;
257 }
258
259 if (len == 0)
260 len = file_len;
261
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100262 return ext4fs_read(buf, offset, len, len_read);
Simon Glass19e38582012-12-26 09:53:33 +0000263}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100264
265int ext4fs_uuid(char *uuid_str)
266{
267 if (ext4fs_root == NULL)
268 return -1;
269
270#ifdef CONFIG_LIB_UUID
271 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
272 uuid_str, UUID_STR_FORMAT_STD);
273
274 return 0;
275#else
276 return -ENOSYS;
277#endif
278}
Stephen Warren02d6ca72019-01-30 12:58:05 -0700279
280void ext_cache_init(struct ext_block_cache *cache)
281{
282 memset(cache, 0, sizeof(*cache));
283}
284
285void ext_cache_fini(struct ext_block_cache *cache)
286{
287 free(cache->buf);
288 ext_cache_init(cache);
289}
290
291int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
292{
293 /* This could be more lenient, but this is simple and enough for now */
294 if (cache->buf && cache->block == block && cache->size == size)
295 return 1;
296 ext_cache_fini(cache);
Jan Kiszkac2ecf222020-03-25 21:27:51 +0100297 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700298 if (!cache->buf)
299 return 0;
300 if (!ext4fs_devread(block, 0, size, cache->buf)) {
Paul Emge955dbbc2019-07-08 16:37:04 -0700301 ext_cache_fini(cache);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700302 return 0;
303 }
304 cache->block = block;
305 cache->size = size;
306 return 1;
307}