blob: 15587e92e3e72d02206ddffe9c60733dc1df475b [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
Simon Glass655306c2020-05-10 11:39:58 -060023#include <blk.h>
Uma Shankar71014b62012-05-25 21:21:44 +053024#include <ext_common.h>
25#include <ext4fs.h>
Uma Shankar71014b62012-05-25 21:21:44 +053026#include "ext4_common.h"
Tom Rinia17b7bc2014-11-24 11:50:46 -050027#include <div64.h>
Simon Glass9bc15642020-02-03 07:36:16 -070028#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060029#include <part.h>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010030#include <u-boot/uuid.h>
Uma Shankar71014b62012-05-25 21:21:44 +053031
32int ext4fs_symlinknest;
Rob Herring30f7fd72012-08-23 11:31:45 +000033struct ext_filesystem ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053034
35struct ext_filesystem *get_fs(void)
36{
Rob Herring30f7fd72012-08-23 11:31:45 +000037 return &ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053038}
39
40void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
41{
42 if ((node != &ext4fs_root->diropen) && (node != currroot))
43 free(node);
44}
45
46/*
47 * Taken from openmoko-kernel mailing list: By Andy green
48 * Optimized read file API : collects and defers contiguous sector
49 * reads into one potentially more efficient larger sequential read action
50 */
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080051int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
52 loff_t len, char *buf, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +053053{
Egbert Eich7b1b2552013-05-01 01:13:19 +000054 struct ext_filesystem *fs = get_fs();
Uma Shankar71014b62012-05-25 21:21:44 +053055 int i;
Frederic Leroye7ee0282013-06-26 18:11:25 +020056 lbaint_t blockcnt;
Egbert Eich7b1b2552013-05-01 01:13:19 +000057 int log2blksz = fs->dev_desc->log2blksz;
58 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
59 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
Michael Wallec07cdcb2016-08-29 10:46:44 +020060 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroye7ee0282013-06-26 18:11:25 +020061 lbaint_t previous_block_number = -1;
62 lbaint_t delayed_start = 0;
63 lbaint_t delayed_extent = 0;
64 lbaint_t delayed_skipfirst = 0;
65 lbaint_t delayed_next = 0;
Uma Shankar71014b62012-05-25 21:21:44 +053066 char *delayed_buf = NULL;
Paul Emge627e3c82019-07-08 16:37:07 -070067 char *start_buf = buf;
Uma Shankar71014b62012-05-25 21:21:44 +053068 short status;
Stephen Warren02d6ca72019-01-30 12:58:05 -070069 struct ext_block_cache cache;
70
71 ext_cache_init(&cache);
Uma Shankar71014b62012-05-25 21:21:44 +053072
73 /* Adjust len so it we can't read past the end of the file. */
Stefan Brünsdb5862d2016-11-06 18:33:57 +010074 if (len + pos > filesize)
75 len = (filesize - pos);
Uma Shankar71014b62012-05-25 21:21:44 +053076
Paul Emgea4a48ef2019-07-08 16:37:05 -070077 if (blocksize <= 0 || len <= 0) {
78 ext_cache_fini(&cache);
79 return -1;
80 }
81
Tom Rinia17b7bc2014-11-24 11:50:46 -050082 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankar71014b62012-05-25 21:21:44 +053083
Tom Rinia17b7bc2014-11-24 11:50:46 -050084 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutlac32ffa82017-04-26 16:58:22 +053085 long int blknr;
Tom Rinia17b7bc2014-11-24 11:50:46 -050086 int blockoff = pos - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +053087 int blockend = blocksize;
88 int skipfirst = 0;
Stephen Warren02d6ca72019-01-30 12:58:05 -070089 blknr = read_allocated_block(&node->inode, i, &cache);
90 if (blknr < 0) {
91 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -050092 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -070093 }
Uma Shankar71014b62012-05-25 21:21:44 +053094
Egbert Eich7b1b2552013-05-01 01:13:19 +000095 blknr = blknr << log2_fs_blocksize;
Uma Shankar71014b62012-05-25 21:21:44 +053096
97 /* Last block. */
98 if (i == blockcnt - 1) {
Tom Rinia17b7bc2014-11-24 11:50:46 -050099 blockend = (len + pos) - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +0530100
101 /* The last portion is exactly blocksize. */
102 if (!blockend)
103 blockend = blocksize;
104 }
105
106 /* First block. */
Tom Rinia17b7bc2014-11-24 11:50:46 -0500107 if (i == lldiv(pos, blocksize)) {
Uma Shankar71014b62012-05-25 21:21:44 +0530108 skipfirst = blockoff;
109 blockend -= skipfirst;
110 }
111 if (blknr) {
112 int status;
113
114 if (previous_block_number != -1) {
115 if (delayed_next == blknr) {
116 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000117 delayed_next += blockend >> log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +0530118 } else { /* spill */
119 status = ext4fs_devread(delayed_start,
120 delayed_skipfirst,
121 delayed_extent,
122 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700123 if (status == 0) {
124 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500125 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700126 }
Uma Shankar71014b62012-05-25 21:21:44 +0530127 previous_block_number = blknr;
128 delayed_start = blknr;
129 delayed_extent = blockend;
130 delayed_skipfirst = skipfirst;
131 delayed_buf = buf;
132 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000133 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530134 }
135 } else {
136 previous_block_number = blknr;
137 delayed_start = blknr;
138 delayed_extent = blockend;
139 delayed_skipfirst = skipfirst;
140 delayed_buf = buf;
141 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000142 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530143 }
144 } else {
Ian Rayc45fcd52017-11-08 15:35:10 +0000145 int n;
Paul Emge627e3c82019-07-08 16:37:07 -0700146 int n_left;
Uma Shankar71014b62012-05-25 21:21:44 +0530147 if (previous_block_number != -1) {
148 /* spill */
149 status = ext4fs_devread(delayed_start,
150 delayed_skipfirst,
151 delayed_extent,
152 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700153 if (status == 0) {
154 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500155 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700156 }
Uma Shankar71014b62012-05-25 21:21:44 +0530157 previous_block_number = -1;
158 }
Ian Rayc45fcd52017-11-08 15:35:10 +0000159 /* Zero no more than `len' bytes. */
160 n = blocksize - skipfirst;
Paul Emge627e3c82019-07-08 16:37:07 -0700161 n_left = len - ( buf - start_buf );
162 if (n > n_left)
163 n = n_left;
Ian Rayc45fcd52017-11-08 15:35:10 +0000164 memset(buf, 0, n);
Uma Shankar71014b62012-05-25 21:21:44 +0530165 }
166 buf += blocksize - skipfirst;
167 }
168 if (previous_block_number != -1) {
169 /* spill */
170 status = ext4fs_devread(delayed_start,
171 delayed_skipfirst, delayed_extent,
172 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700173 if (status == 0) {
174 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500175 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700176 }
Uma Shankar71014b62012-05-25 21:21:44 +0530177 previous_block_number = -1;
178 }
179
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800180 *actread = len;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700181 ext_cache_fini(&cache);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800182 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +0530183}
184
185int ext4fs_ls(const char *dirname)
186{
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300187 struct ext2fs_node *dirnode = NULL;
Uma Shankar71014b62012-05-25 21:21:44 +0530188 int status;
189
190 if (dirname == NULL)
191 return 0;
192
193 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
194 FILETYPE_DIRECTORY);
195 if (status != 1) {
196 printf("** Can not find directory. **\n");
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300197 if (dirnode)
198 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
Uma Shankar71014b62012-05-25 21:21:44 +0530199 return 1;
200 }
201
202 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
203 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
204
205 return 0;
206}
207
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700208int ext4fs_exists(const char *filename)
209{
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100210 struct ext2fs_node *dirnode = NULL;
211 int filetype;
212
213 if (!filename)
214 return 0;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700215
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100216 return ext4fs_find_file1(filename, &ext4fs_root->diropen, &dirnode,
217 &filetype);
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700218}
219
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800220int ext4fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600221{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800222 return ext4fs_open(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600223}
224
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100225int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +0530226{
227 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100228 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530229
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100230 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankar71014b62012-05-25 21:21:44 +0530231}
Simon Glass19e38582012-12-26 09:53:33 +0000232
Simon Glasse3394752016-02-29 15:25:34 -0700233int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600234 struct disk_partition *fs_partition)
Simon Glass19e38582012-12-26 09:53:33 +0000235{
236 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
237
Sean Andersonab125f52023-11-08 12:51:09 -0500238 if (!ext4fs_mount()) {
Simon Glass19e38582012-12-26 09:53:33 +0000239 ext4fs_close();
240 return -1;
241 }
242
243 return 0;
244}
245
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800246int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
247 loff_t *len_read)
Simon Glass19e38582012-12-26 09:53:33 +0000248{
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800249 loff_t file_len;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800250 int ret;
Simon Glass19e38582012-12-26 09:53:33 +0000251
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800252 ret = ext4fs_open(filename, &file_len);
253 if (ret < 0) {
Simon Glass19e38582012-12-26 09:53:33 +0000254 printf("** File not found %s **\n", filename);
255 return -1;
256 }
257
258 if (len == 0)
259 len = file_len;
260
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100261 return ext4fs_read(buf, offset, len, len_read);
Simon Glass19e38582012-12-26 09:53:33 +0000262}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100263
264int ext4fs_uuid(char *uuid_str)
265{
266 if (ext4fs_root == NULL)
267 return -1;
268
269#ifdef CONFIG_LIB_UUID
270 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
271 uuid_str, UUID_STR_FORMAT_STD);
272
273 return 0;
274#else
275 return -ENOSYS;
276#endif
277}
Stephen Warren02d6ca72019-01-30 12:58:05 -0700278
279void ext_cache_init(struct ext_block_cache *cache)
280{
281 memset(cache, 0, sizeof(*cache));
282}
283
284void ext_cache_fini(struct ext_block_cache *cache)
285{
286 free(cache->buf);
287 ext_cache_init(cache);
288}
289
290int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
291{
292 /* This could be more lenient, but this is simple and enough for now */
293 if (cache->buf && cache->block == block && cache->size == size)
294 return 1;
295 ext_cache_fini(cache);
Jan Kiszkac2ecf222020-03-25 21:27:51 +0100296 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700297 if (!cache->buf)
298 return 0;
299 if (!ext4fs_devread(block, 0, size, cache->buf)) {
Paul Emge955dbbc2019-07-08 16:37:04 -0700300 ext_cache_fini(cache);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700301 return 0;
302 }
303 cache->block = block;
304 cache->size = size;
305 return 1;
306}