blob: dfecfa0b4e8849ed99dc134e8ac8b73c0da999d4 [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>
Heinrich Schuchardte415b992024-10-26 08:40:46 +020024#include <div64.h>
25#include <errno.h>
Uma Shankar71014b62012-05-25 21:21:44 +053026#include <ext_common.h>
27#include <ext4fs.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>
Heinrich Schuchardte415b992024-10-26 08:40:46 +020031#include "ext4_common.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
Heinrich Schuchardte415b992024-10-26 08:40:46 +020036/**
37 * struct ext4_dir_stream - ext4 directory stream
38 *
39 * @parent: partition data used by fs layer.
40 * This field must be at the beginning of the structure.
41 * All other fields are private to the ext4 driver.
42 * @root: root directory node
43 * @dir: directory node
44 * @dirent: directory stream entry
45 * @fpos: file position in directory
46 */
47struct ext4_dir_stream {
48 struct fs_dir_stream parent;
49 char *dirname;
50 struct fs_dirent dirent;
51 unsigned int fpos;
52};
53
Uma Shankar71014b62012-05-25 21:21:44 +053054struct ext_filesystem *get_fs(void)
55{
Rob Herring30f7fd72012-08-23 11:31:45 +000056 return &ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053057}
58
59void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
60{
61 if ((node != &ext4fs_root->diropen) && (node != currroot))
62 free(node);
63}
64
65/*
66 * Taken from openmoko-kernel mailing list: By Andy green
67 * Optimized read file API : collects and defers contiguous sector
68 * reads into one potentially more efficient larger sequential read action
69 */
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -080070int ext4fs_read_file(struct ext2fs_node *node, loff_t pos,
71 loff_t len, char *buf, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +053072{
Egbert Eich7b1b2552013-05-01 01:13:19 +000073 struct ext_filesystem *fs = get_fs();
Uma Shankar71014b62012-05-25 21:21:44 +053074 int i;
Frederic Leroye7ee0282013-06-26 18:11:25 +020075 lbaint_t blockcnt;
Egbert Eich7b1b2552013-05-01 01:13:19 +000076 int log2blksz = fs->dev_desc->log2blksz;
77 int log2_fs_blocksize = LOG2_BLOCK_SIZE(node->data) - log2blksz;
78 int blocksize = (1 << (log2_fs_blocksize + log2blksz));
Michael Wallec07cdcb2016-08-29 10:46:44 +020079 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroye7ee0282013-06-26 18:11:25 +020080 lbaint_t previous_block_number = -1;
81 lbaint_t delayed_start = 0;
82 lbaint_t delayed_extent = 0;
83 lbaint_t delayed_skipfirst = 0;
84 lbaint_t delayed_next = 0;
Uma Shankar71014b62012-05-25 21:21:44 +053085 char *delayed_buf = NULL;
Paul Emge627e3c82019-07-08 16:37:07 -070086 char *start_buf = buf;
Uma Shankar71014b62012-05-25 21:21:44 +053087 short status;
Stephen Warren02d6ca72019-01-30 12:58:05 -070088 struct ext_block_cache cache;
89
90 ext_cache_init(&cache);
Uma Shankar71014b62012-05-25 21:21:44 +053091
92 /* Adjust len so it we can't read past the end of the file. */
Stefan Brünsdb5862d2016-11-06 18:33:57 +010093 if (len + pos > filesize)
94 len = (filesize - pos);
Uma Shankar71014b62012-05-25 21:21:44 +053095
Paul Emgea4a48ef2019-07-08 16:37:05 -070096 if (blocksize <= 0 || len <= 0) {
97 ext_cache_fini(&cache);
98 return -1;
99 }
100
Tom Rinia17b7bc2014-11-24 11:50:46 -0500101 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankar71014b62012-05-25 21:21:44 +0530102
Tom Rinia17b7bc2014-11-24 11:50:46 -0500103 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutlac32ffa82017-04-26 16:58:22 +0530104 long int blknr;
Tom Rinia17b7bc2014-11-24 11:50:46 -0500105 int blockoff = pos - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +0530106 int blockend = blocksize;
107 int skipfirst = 0;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700108 blknr = read_allocated_block(&node->inode, i, &cache);
109 if (blknr < 0) {
110 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500111 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700112 }
Uma Shankar71014b62012-05-25 21:21:44 +0530113
Egbert Eich7b1b2552013-05-01 01:13:19 +0000114 blknr = blknr << log2_fs_blocksize;
Uma Shankar71014b62012-05-25 21:21:44 +0530115
116 /* Last block. */
117 if (i == blockcnt - 1) {
Tom Rinia17b7bc2014-11-24 11:50:46 -0500118 blockend = (len + pos) - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +0530119
120 /* The last portion is exactly blocksize. */
121 if (!blockend)
122 blockend = blocksize;
123 }
124
125 /* First block. */
Tom Rinia17b7bc2014-11-24 11:50:46 -0500126 if (i == lldiv(pos, blocksize)) {
Uma Shankar71014b62012-05-25 21:21:44 +0530127 skipfirst = blockoff;
128 blockend -= skipfirst;
129 }
130 if (blknr) {
131 int status;
132
133 if (previous_block_number != -1) {
134 if (delayed_next == blknr) {
135 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000136 delayed_next += blockend >> log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +0530137 } else { /* spill */
138 status = ext4fs_devread(delayed_start,
139 delayed_skipfirst,
140 delayed_extent,
141 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700142 if (status == 0) {
143 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500144 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700145 }
Uma Shankar71014b62012-05-25 21:21:44 +0530146 previous_block_number = blknr;
147 delayed_start = blknr;
148 delayed_extent = blockend;
149 delayed_skipfirst = skipfirst;
150 delayed_buf = buf;
151 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000152 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530153 }
154 } else {
155 previous_block_number = blknr;
156 delayed_start = blknr;
157 delayed_extent = blockend;
158 delayed_skipfirst = skipfirst;
159 delayed_buf = buf;
160 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000161 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530162 }
163 } else {
Ian Rayc45fcd52017-11-08 15:35:10 +0000164 int n;
Paul Emge627e3c82019-07-08 16:37:07 -0700165 int n_left;
Uma Shankar71014b62012-05-25 21:21:44 +0530166 if (previous_block_number != -1) {
167 /* spill */
168 status = ext4fs_devread(delayed_start,
169 delayed_skipfirst,
170 delayed_extent,
171 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700172 if (status == 0) {
173 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500174 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700175 }
Uma Shankar71014b62012-05-25 21:21:44 +0530176 previous_block_number = -1;
177 }
Ian Rayc45fcd52017-11-08 15:35:10 +0000178 /* Zero no more than `len' bytes. */
179 n = blocksize - skipfirst;
Paul Emge627e3c82019-07-08 16:37:07 -0700180 n_left = len - ( buf - start_buf );
181 if (n > n_left)
182 n = n_left;
Ian Rayc45fcd52017-11-08 15:35:10 +0000183 memset(buf, 0, n);
Uma Shankar71014b62012-05-25 21:21:44 +0530184 }
185 buf += blocksize - skipfirst;
186 }
187 if (previous_block_number != -1) {
188 /* spill */
189 status = ext4fs_devread(delayed_start,
190 delayed_skipfirst, delayed_extent,
191 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700192 if (status == 0) {
193 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500194 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700195 }
Uma Shankar71014b62012-05-25 21:21:44 +0530196 previous_block_number = -1;
197 }
198
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800199 *actread = len;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700200 ext_cache_fini(&cache);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800201 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +0530202}
203
Heinrich Schuchardte415b992024-10-26 08:40:46 +0200204int ext4fs_opendir(const char *dirname, struct fs_dir_stream **dirsp)
205{
206 struct ext4_dir_stream *dirs;
207 struct ext2fs_node *dir = NULL;
208 int ret;
209
210 *dirsp = NULL;
211
212 dirs = calloc(1, sizeof(struct ext4_dir_stream));
213 if (!dirs)
214 return -ENOMEM;
215 dirs->dirname = strdup(dirname);
216 if (!dirs) {
217 free(dirs);
218 return -ENOMEM;
219 }
220
221 ret = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dir,
222 FILETYPE_DIRECTORY);
223 if (ret == 1) {
224 ret = 0;
225 *dirsp = (struct fs_dir_stream *)dirs;
226 } else {
227 ret = -ENOENT;
228 }
229
230 if (dir)
231 ext4fs_free_node(dir, &ext4fs_root->diropen);
232
233 return ret;
234}
235
236int ext4fs_readdir(struct fs_dir_stream *fs_dirs, struct fs_dirent **dentp)
237{
238 struct ext4_dir_stream *dirs = (struct ext4_dir_stream *)fs_dirs;
239 struct fs_dirent *dent = &dirs->dirent;
240 struct ext2fs_node *dir = NULL;
241 int ret;
242 loff_t actread;
243 struct ext2fs_node fdiro;
244 int len;
245 struct ext2_dirent dirent;
246
247 *dentp = NULL;
248
249 ret = ext4fs_find_file(dirs->dirname, &ext4fs_root->diropen, &dir,
250 FILETYPE_DIRECTORY);
251 if (ret != 1) {
252 ret = -ENOENT;
253 goto out;
254 }
255 if (!dir->inode_read) {
256 ret = ext4fs_read_inode(dir->data, dir->ino, &dir->inode);
257 if (!ret) {
258 ret = -EIO;
259 goto out;
260 }
261 }
262
263 if (dirs->fpos >= le32_to_cpu(dir->inode.size))
264 return -ENOENT;
265
266 memset(dent, 0, sizeof(struct fs_dirent));
267
268 while (dirs->fpos < le32_to_cpu(dir->inode.size)) {
269 ret = ext4fs_read_file(dir, dirs->fpos,
270 sizeof(struct ext2_dirent),
271 (char *)&dirent, &actread);
272 if (ret < 0)
273 return -ret;
274
275 if (!dirent.direntlen)
276 return -EIO;
277
278 if (dirent.namelen)
279 break;
280
281 dirs->fpos += le16_to_cpu(dirent.direntlen);
282 }
283
284 len = min(FS_DIRENT_NAME_LEN - 1, (int)dirent.namelen);
285
286 ret = ext4fs_read_file(dir, dirs->fpos + sizeof(struct ext2_dirent),
287 len, dent->name, &actread);
288 if (ret < 0)
289 goto out;
290 dent->name[len] = '\0';
291
292 fdiro.data = dir->data;
293 fdiro.ino = le32_to_cpu(dirent.inode);
294
295 ret = ext4fs_read_inode(dir->data, fdiro.ino, &fdiro.inode);
296 if (!ret) {
297 ret = -EIO;
298 goto out;
299 }
300
301 switch (le16_to_cpu(fdiro.inode.mode) & FILETYPE_INO_MASK) {
302 case FILETYPE_INO_DIRECTORY:
303 dent->type = FS_DT_DIR;
304 break;
305 case FILETYPE_INO_SYMLINK:
306 dent->type = FS_DT_LNK;
307 break;
308 case FILETYPE_INO_REG:
309 dent->type = FS_DT_REG;
310 break;
311 default:
312 dent->type = FILETYPE_UNKNOWN;
313 }
314
315 rtc_to_tm(fdiro.inode.atime, &dent->access_time);
316 rtc_to_tm(fdiro.inode.ctime, &dent->create_time);
317 rtc_to_tm(fdiro.inode.mtime, &dent->change_time);
318
319 dirs->fpos += le16_to_cpu(dirent.direntlen);
320 dent->size = fdiro.inode.size;
321 *dentp = dent;
322 ret = 0;
323
324out:
325 if (dir)
326 ext4fs_free_node(dir, &ext4fs_root->diropen);
327
328 return ret;
329}
330
331void ext4fs_closedir(struct fs_dir_stream *fs_dirs)
332{
333 struct ext4_dir_stream *dirs = (struct ext4_dir_stream *)fs_dirs;
334
335 if (!dirs)
336 return;
337
338 free(dirs->dirname);
339 free(dirs);
340}
341
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700342int ext4fs_exists(const char *filename)
343{
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100344 struct ext2fs_node *dirnode = NULL;
345 int filetype;
Heinrich Schuchardt149e3c22024-10-26 08:40:45 +0200346 int ret;
Heinrich Schuchardte2ac6382024-02-20 12:54:23 +0100347
348 if (!filename)
349 return 0;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700350
Heinrich Schuchardt149e3c22024-10-26 08:40:45 +0200351 ret = ext4fs_find_file1(filename, &ext4fs_root->diropen, &dirnode,
352 &filetype);
353 if (dirnode)
354 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
355
356 return ret;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700357}
358
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800359int ext4fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600360{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800361 return ext4fs_open(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600362}
363
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100364int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +0530365{
366 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100367 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530368
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100369 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankar71014b62012-05-25 21:21:44 +0530370}
Simon Glass19e38582012-12-26 09:53:33 +0000371
Simon Glasse3394752016-02-29 15:25:34 -0700372int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600373 struct disk_partition *fs_partition)
Simon Glass19e38582012-12-26 09:53:33 +0000374{
375 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
376
Sean Andersonab125f52023-11-08 12:51:09 -0500377 if (!ext4fs_mount()) {
Simon Glass19e38582012-12-26 09:53:33 +0000378 ext4fs_close();
379 return -1;
380 }
381
382 return 0;
383}
384
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800385int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
386 loff_t *len_read)
Simon Glass19e38582012-12-26 09:53:33 +0000387{
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800388 loff_t file_len;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800389 int ret;
Simon Glass19e38582012-12-26 09:53:33 +0000390
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800391 ret = ext4fs_open(filename, &file_len);
392 if (ret < 0) {
Simon Glass19e38582012-12-26 09:53:33 +0000393 printf("** File not found %s **\n", filename);
394 return -1;
395 }
396
397 if (len == 0)
398 len = file_len;
399
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100400 return ext4fs_read(buf, offset, len, len_read);
Simon Glass19e38582012-12-26 09:53:33 +0000401}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100402
403int ext4fs_uuid(char *uuid_str)
404{
405 if (ext4fs_root == NULL)
406 return -1;
407
408#ifdef CONFIG_LIB_UUID
409 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
410 uuid_str, UUID_STR_FORMAT_STD);
411
412 return 0;
413#else
414 return -ENOSYS;
415#endif
416}
Stephen Warren02d6ca72019-01-30 12:58:05 -0700417
418void ext_cache_init(struct ext_block_cache *cache)
419{
420 memset(cache, 0, sizeof(*cache));
421}
422
423void ext_cache_fini(struct ext_block_cache *cache)
424{
425 free(cache->buf);
426 ext_cache_init(cache);
427}
428
429int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
430{
431 /* This could be more lenient, but this is simple and enough for now */
432 if (cache->buf && cache->block == block && cache->size == size)
433 return 1;
434 ext_cache_fini(cache);
Jan Kiszkac2ecf222020-03-25 21:27:51 +0100435 cache->buf = memalign(ARCH_DMA_MINALIGN, size);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700436 if (!cache->buf)
437 return 0;
438 if (!ext4fs_devread(block, 0, size, cache->buf)) {
Paul Emge955dbbc2019-07-08 16:37:04 -0700439 ext_cache_fini(cache);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700440 return 0;
441 }
442 cache->block = block;
443 cache->size = size;
444 return 1;
445}