blob: 26db677a1f172bde1f10ac5297bf7226b6f6ba15 [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>
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>
Uma Shankar71014b62012-05-25 21:21:44 +053028
29int ext4fs_symlinknest;
Rob Herring30f7fd72012-08-23 11:31:45 +000030struct ext_filesystem ext_fs;
Uma Shankar71014b62012-05-25 21:21:44 +053031
32struct ext_filesystem *get_fs(void)
33{
Rob Herring30f7fd72012-08-23 11:31:45 +000034 return &ext_fs;
Uma Shankar71014b62012-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 Ramasamib3a2d5a2014-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 Shankar71014b62012-05-25 21:21:44 +053050{
Egbert Eich7b1b2552013-05-01 01:13:19 +000051 struct ext_filesystem *fs = get_fs();
Uma Shankar71014b62012-05-25 21:21:44 +053052 int i;
Frederic Leroye7ee0282013-06-26 18:11:25 +020053 lbaint_t blockcnt;
Egbert Eich7b1b2552013-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 Wallec07cdcb2016-08-29 10:46:44 +020057 unsigned int filesize = le32_to_cpu(node->inode.size);
Frederic Leroye7ee0282013-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 Shankar71014b62012-05-25 21:21:44 +053063 char *delayed_buf = NULL;
64 short status;
Stephen Warren02d6ca72019-01-30 12:58:05 -070065 struct ext_block_cache cache;
66
67 ext_cache_init(&cache);
Uma Shankar71014b62012-05-25 21:21:44 +053068
Ian Rayc45fcd52017-11-08 15:35:10 +000069 if (blocksize <= 0)
70 return -1;
71
Uma Shankar71014b62012-05-25 21:21:44 +053072 /* Adjust len so it we can't read past the end of the file. */
Stefan Brünsdb5862d2016-11-06 18:33:57 +010073 if (len + pos > filesize)
74 len = (filesize - pos);
Uma Shankar71014b62012-05-25 21:21:44 +053075
Tom Rinia17b7bc2014-11-24 11:50:46 -050076 blockcnt = lldiv(((len + pos) + blocksize - 1), blocksize);
Uma Shankar71014b62012-05-25 21:21:44 +053077
Tom Rinia17b7bc2014-11-24 11:50:46 -050078 for (i = lldiv(pos, blocksize); i < blockcnt; i++) {
Lokesh Vutlac32ffa82017-04-26 16:58:22 +053079 long int blknr;
Tom Rinia17b7bc2014-11-24 11:50:46 -050080 int blockoff = pos - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +053081 int blockend = blocksize;
82 int skipfirst = 0;
Stephen Warren02d6ca72019-01-30 12:58:05 -070083 blknr = read_allocated_block(&node->inode, i, &cache);
84 if (blknr < 0) {
85 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -050086 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -070087 }
Uma Shankar71014b62012-05-25 21:21:44 +053088
Egbert Eich7b1b2552013-05-01 01:13:19 +000089 blknr = blknr << log2_fs_blocksize;
Uma Shankar71014b62012-05-25 21:21:44 +053090
91 /* Last block. */
92 if (i == blockcnt - 1) {
Tom Rinia17b7bc2014-11-24 11:50:46 -050093 blockend = (len + pos) - (blocksize * i);
Uma Shankar71014b62012-05-25 21:21:44 +053094
95 /* The last portion is exactly blocksize. */
96 if (!blockend)
97 blockend = blocksize;
98 }
99
100 /* First block. */
Tom Rinia17b7bc2014-11-24 11:50:46 -0500101 if (i == lldiv(pos, blocksize)) {
Uma Shankar71014b62012-05-25 21:21:44 +0530102 skipfirst = blockoff;
103 blockend -= skipfirst;
104 }
105 if (blknr) {
106 int status;
107
108 if (previous_block_number != -1) {
109 if (delayed_next == blknr) {
110 delayed_extent += blockend;
Egbert Eich7b1b2552013-05-01 01:13:19 +0000111 delayed_next += blockend >> log2blksz;
Uma Shankar71014b62012-05-25 21:21:44 +0530112 } else { /* spill */
113 status = ext4fs_devread(delayed_start,
114 delayed_skipfirst,
115 delayed_extent,
116 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700117 if (status == 0) {
118 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500119 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700120 }
Uma Shankar71014b62012-05-25 21:21:44 +0530121 previous_block_number = blknr;
122 delayed_start = blknr;
123 delayed_extent = blockend;
124 delayed_skipfirst = skipfirst;
125 delayed_buf = buf;
126 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000127 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530128 }
129 } else {
130 previous_block_number = blknr;
131 delayed_start = blknr;
132 delayed_extent = blockend;
133 delayed_skipfirst = skipfirst;
134 delayed_buf = buf;
135 delayed_next = blknr +
Egbert Eich7b1b2552013-05-01 01:13:19 +0000136 (blockend >> log2blksz);
Uma Shankar71014b62012-05-25 21:21:44 +0530137 }
138 } else {
Ian Rayc45fcd52017-11-08 15:35:10 +0000139 int n;
Uma Shankar71014b62012-05-25 21:21:44 +0530140 if (previous_block_number != -1) {
141 /* spill */
142 status = ext4fs_devread(delayed_start,
143 delayed_skipfirst,
144 delayed_extent,
145 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700146 if (status == 0) {
147 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500148 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700149 }
Uma Shankar71014b62012-05-25 21:21:44 +0530150 previous_block_number = -1;
151 }
Ian Rayc45fcd52017-11-08 15:35:10 +0000152 /* Zero no more than `len' bytes. */
153 n = blocksize - skipfirst;
154 if (n > len)
155 n = len;
156 memset(buf, 0, n);
Uma Shankar71014b62012-05-25 21:21:44 +0530157 }
158 buf += blocksize - skipfirst;
159 }
160 if (previous_block_number != -1) {
161 /* spill */
162 status = ext4fs_devread(delayed_start,
163 delayed_skipfirst, delayed_extent,
164 delayed_buf);
Stephen Warren02d6ca72019-01-30 12:58:05 -0700165 if (status == 0) {
166 ext_cache_fini(&cache);
Tom Riniee03bd12014-02-26 08:18:58 -0500167 return -1;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700168 }
Uma Shankar71014b62012-05-25 21:21:44 +0530169 previous_block_number = -1;
170 }
171
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800172 *actread = len;
Stephen Warren02d6ca72019-01-30 12:58:05 -0700173 ext_cache_fini(&cache);
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800174 return 0;
Uma Shankar71014b62012-05-25 21:21:44 +0530175}
176
177int ext4fs_ls(const char *dirname)
178{
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300179 struct ext2fs_node *dirnode = NULL;
Uma Shankar71014b62012-05-25 21:21:44 +0530180 int status;
181
182 if (dirname == NULL)
183 return 0;
184
185 status = ext4fs_find_file(dirname, &ext4fs_root->diropen, &dirnode,
186 FILETYPE_DIRECTORY);
187 if (status != 1) {
188 printf("** Can not find directory. **\n");
Eugen Hristevd58c8e92018-05-09 16:28:37 +0300189 if (dirnode)
190 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
Uma Shankar71014b62012-05-25 21:21:44 +0530191 return 1;
192 }
193
194 ext4fs_iterate_dir(dirnode, NULL, NULL, NULL);
195 ext4fs_free_node(dirnode, &ext4fs_root->diropen);
196
197 return 0;
198}
199
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700200int ext4fs_exists(const char *filename)
201{
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800202 loff_t file_len;
203 int ret;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700204
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800205 ret = ext4fs_open(filename, &file_len);
206 return ret == 0;
Stephen Warren12d6d0c2014-02-03 13:21:09 -0700207}
208
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800209int ext4fs_size(const char *filename, loff_t *size)
Stephen Warren3eb58f52014-06-11 12:47:26 -0600210{
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800211 return ext4fs_open(filename, size);
Stephen Warren3eb58f52014-06-11 12:47:26 -0600212}
213
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100214int ext4fs_read(char *buf, loff_t offset, loff_t len, loff_t *actread)
Uma Shankar71014b62012-05-25 21:21:44 +0530215{
216 if (ext4fs_root == NULL || ext4fs_file == NULL)
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100217 return -1;
Uma Shankar71014b62012-05-25 21:21:44 +0530218
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100219 return ext4fs_read_file(ext4fs_file, offset, len, buf, actread);
Uma Shankar71014b62012-05-25 21:21:44 +0530220}
Simon Glass19e38582012-12-26 09:53:33 +0000221
Simon Glasse3394752016-02-29 15:25:34 -0700222int ext4fs_probe(struct blk_desc *fs_dev_desc,
Simon Glass19e38582012-12-26 09:53:33 +0000223 disk_partition_t *fs_partition)
224{
225 ext4fs_set_blk_dev(fs_dev_desc, fs_partition);
226
227 if (!ext4fs_mount(fs_partition->size)) {
228 ext4fs_close();
229 return -1;
230 }
231
232 return 0;
233}
234
Suriyan Ramasami96171fb2014-11-17 14:39:38 -0800235int ext4_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
236 loff_t *len_read)
Simon Glass19e38582012-12-26 09:53:33 +0000237{
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800238 loff_t file_len;
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800239 int ret;
Simon Glass19e38582012-12-26 09:53:33 +0000240
Suriyan Ramasamib3a2d5a2014-11-17 14:39:36 -0800241 ret = ext4fs_open(filename, &file_len);
242 if (ret < 0) {
Simon Glass19e38582012-12-26 09:53:33 +0000243 printf("** File not found %s **\n", filename);
244 return -1;
245 }
246
247 if (len == 0)
248 len = file_len;
249
Stefan Brünsdb5862d2016-11-06 18:33:57 +0100250 return ext4fs_read(buf, offset, len, len_read);
Simon Glass19e38582012-12-26 09:53:33 +0000251}
Christian Gmeiner9f9eec32014-11-12 14:35:04 +0100252
253int ext4fs_uuid(char *uuid_str)
254{
255 if (ext4fs_root == NULL)
256 return -1;
257
258#ifdef CONFIG_LIB_UUID
259 uuid_bin_to_str((unsigned char *)ext4fs_root->sblock.unique_id,
260 uuid_str, UUID_STR_FORMAT_STD);
261
262 return 0;
263#else
264 return -ENOSYS;
265#endif
266}
Stephen Warren02d6ca72019-01-30 12:58:05 -0700267
268void ext_cache_init(struct ext_block_cache *cache)
269{
270 memset(cache, 0, sizeof(*cache));
271}
272
273void ext_cache_fini(struct ext_block_cache *cache)
274{
275 free(cache->buf);
276 ext_cache_init(cache);
277}
278
279int ext_cache_read(struct ext_block_cache *cache, lbaint_t block, int size)
280{
281 /* This could be more lenient, but this is simple and enough for now */
282 if (cache->buf && cache->block == block && cache->size == size)
283 return 1;
284 ext_cache_fini(cache);
285 cache->buf = malloc(size);
286 if (!cache->buf)
287 return 0;
288 if (!ext4fs_devread(block, 0, size, cache->buf)) {
289 free(cache->buf);
290 return 0;
291 }
292 cache->block = block;
293 cache->size = size;
294 return 1;
295}