blob: 048730db7ff74c7a844abe6bdb705971e36f51cd [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese2fc10f62009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Stefan Roese28ea29f2010-11-01 17:28:00 +01007 * (C) Copyright 2008-2010
Stefan Roese2fc10f62009-03-19 15:35:05 +01008 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 *
Stefan Roese2fc10f62009-03-19 15:35:05 +010010 * Authors: Artem Bityutskiy (Битюцкий Артём)
11 * Adrian Hunter
12 */
13
Simon Glass313112a2019-08-01 09:46:46 -060014#include <env.h>
Simon Glass1a974af2019-08-01 09:46:36 -060015#include <gzip.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glassa87fc0a2015-09-02 17:24:57 -060018#include <memalign.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010020#include "ubifs.h"
Simon Glass655306c2020-05-10 11:39:58 -060021#include <part.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <dm/devres.h>
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020023#include <u-boot/zlib.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010024
AKASHI Takahiro1d8d34d2019-11-13 09:44:47 +090025#include <linux/compat.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020026#include <linux/err.h>
27#include <linux/lzo.h>
28
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +020029#if IS_ENABLED(CONFIG_ZSTD)
30#include <linux/zstd.h>
31#include <abuf.h>
32#endif
33
Stefan Roese2fc10f62009-03-19 15:35:05 +010034DECLARE_GLOBAL_DATA_PTR;
35
36/* compress.c */
37
38/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020039 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010040 * incompatible with the lzo decompressor.
41 */
42static int gzip_decompress(const unsigned char *in, size_t in_len,
43 unsigned char *out, size_t *out_len)
44{
Veli-Pekka Peltola7b7e1552012-09-05 18:05:14 +030045 return zunzip(out, *out_len, (unsigned char *)in,
46 (unsigned long *)out_len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010047}
48
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +020049#if IS_ENABLED(CONFIG_ZSTD)
50static int zstd_decompress_wrapper(const unsigned char *in, size_t in_len,
51 unsigned char *out, size_t *out_len)
52{
53 struct abuf abuf_in, abuf_out;
54 int ret;
55
56 abuf_init_set(&abuf_in, (void *)in, in_len);
57 abuf_init_set(&abuf_out, (void *)out, *out_len);
58
59 ret = zstd_decompress(&abuf_in, &abuf_out);
60 if (ret < 0)
61 return ret;
62
63 *out_len = ret;
64 return 0;
65}
66#endif
67
Stefan Roese2fc10f62009-03-19 15:35:05 +010068/* Fake description object for the "none" compressor */
69static struct ubifs_compressor none_compr = {
70 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherf5895d12014-06-24 10:10:04 +020071 .name = "none",
Stefan Roese2fc10f62009-03-19 15:35:05 +010072 .capi_name = "",
73 .decompress = NULL,
74};
75
76static struct ubifs_compressor lzo_compr = {
77 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherf5895d12014-06-24 10:10:04 +020078#ifndef __UBOOT__
79 .comp_mutex = &lzo_mutex,
80#endif
81 .name = "lzo",
Stefan Roese2fc10f62009-03-19 15:35:05 +010082 .capi_name = "lzo",
83 .decompress = lzo1x_decompress_safe,
84};
85
86static struct ubifs_compressor zlib_compr = {
87 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherf5895d12014-06-24 10:10:04 +020088#ifndef __UBOOT__
89 .comp_mutex = &deflate_mutex,
90 .decomp_mutex = &inflate_mutex,
91#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010092 .name = "zlib",
93 .capi_name = "deflate",
94 .decompress = gzip_decompress,
95};
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +020096
97#if IS_ENABLED(CONFIG_ZSTD)
98static struct ubifs_compressor zstd_compr = {
99 .compr_type = UBIFS_COMPR_ZSTD,
100#ifndef __UBOOT__
101 .comp_mutex = &zstd_enc_mutex,
102 .decomp_mutex = &zstd_dec_mutex,
103#endif
104 .name = "zstd",
105 .capi_name = "zstd",
106 .decompress = zstd_decompress_wrapper,
107};
108#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100109
110/* All UBIFS compressors */
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +0200111struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT] = {NULL};
Stefan Roese2fc10f62009-03-19 15:35:05 +0100112
Heiko Schocherf5895d12014-06-24 10:10:04 +0200113#ifdef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114
115struct crypto_comp {
116 int compressor;
117};
118
Heiko Schocher94b66de2015-10-22 06:19:21 +0200119static inline struct crypto_comp
120*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200121{
122 struct ubifs_compressor *comp;
123 struct crypto_comp *ptr;
124 int i = 0;
125
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200126 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200127 while (i < UBIFS_COMPR_TYPES_CNT) {
128 comp = ubifs_compressors[i];
129 if (!comp) {
130 i++;
131 continue;
132 }
133 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
134 ptr->compressor = i;
135 return ptr;
136 }
137 i++;
138 }
139 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200140 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200141 free (ptr);
142 return NULL;
143 }
144 return ptr;
145}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200146static inline int
147crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
148 const u8 *src, unsigned int slen, u8 *dst,
149 unsigned int *dlen)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200150{
151 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
152 int err;
Paul Davey7f918382018-11-05 18:09:29 +1300153 size_t tmp_len = *dlen;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200154
155 if (compr->compr_type == UBIFS_COMPR_NONE) {
156 memcpy(dst, src, slen);
157 *dlen = slen;
158 return 0;
159 }
160
Paul Davey7f918382018-11-05 18:09:29 +1300161 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200162 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200163 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherf5895d12014-06-24 10:10:04 +0200164 "error %d", slen, compr->name, err);
165
Paul Davey7f918382018-11-05 18:09:29 +1300166 *dlen = tmp_len;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200167 return err;
168
169 return 0;
170}
Anton Habegger62e79dd2015-01-22 22:29:10 +0100171
172/* from shrinker.c */
173
174/* Global clean znode counter (for all mounted UBIFS instances) */
175atomic_long_t ubifs_clean_zn_cnt;
176
Heiko Schocherf5895d12014-06-24 10:10:04 +0200177#endif
178
Stefan Roese2fc10f62009-03-19 15:35:05 +0100179/**
180 * ubifs_decompress - decompress data.
181 * @in_buf: data to decompress
182 * @in_len: length of the data to decompress
183 * @out_buf: output buffer where decompressed data should
184 * @out_len: output length is returned here
185 * @compr_type: type of compression
186 *
187 * This function decompresses data from buffer @in_buf into buffer @out_buf.
188 * The length of the uncompressed data is returned in @out_len. This functions
189 * returns %0 on success or a negative error code on failure.
190 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200191int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
192 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100193{
194 int err;
195 struct ubifs_compressor *compr;
196
197 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200198 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100199 return -EINVAL;
200 }
201
202 compr = ubifs_compressors[compr_type];
203
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +0200204 if (unlikely(!compr)) {
205 ubifs_err(c, "compression type %d is not compiled in", compr_type);
206 return -EINVAL;
207 }
208
Stefan Roese2fc10f62009-03-19 15:35:05 +0100209 if (unlikely(!compr->capi_name)) {
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +0200210 ubifs_err(c, "%s compression is not compiled in",
211 compr->name ? compr->name : "unknown");
Stefan Roese2fc10f62009-03-19 15:35:05 +0100212 return -EINVAL;
213 }
214
215 if (compr_type == UBIFS_COMPR_NONE) {
216 memcpy(out_buf, in_buf, in_len);
217 *out_len = in_len;
218 return 0;
219 }
220
Heiko Schocherf5895d12014-06-24 10:10:04 +0200221 if (compr->decomp_mutex)
222 mutex_lock(compr->decomp_mutex);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200223 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200224 (unsigned int *)out_len);
225 if (compr->decomp_mutex)
226 mutex_unlock(compr->decomp_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100227 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200228 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
229 " error %d", in_len, compr->name, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100230
231 return err;
232}
233
234/**
235 * compr_init - initialize a compressor.
236 * @compr: compressor description object
237 *
238 * This function initializes the requested compressor and returns zero in case
239 * of success or a negative error code in case of failure.
240 */
241static int __init compr_init(struct ubifs_compressor *compr)
242{
243 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500244
Heiko Schocherf5895d12014-06-24 10:10:04 +0200245 if (compr->capi_name) {
246 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
247 if (IS_ERR(compr->cc)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200248 dbg_gen("cannot initialize compressor %s,"
249 " error %ld", compr->name,
250 PTR_ERR(compr->cc));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200251 return PTR_ERR(compr->cc);
252 }
253 }
254
Stefan Roese2fc10f62009-03-19 15:35:05 +0100255 return 0;
256}
257
258/**
259 * ubifs_compressors_init - initialize UBIFS compressors.
260 *
261 * This function initializes the compressor which were compiled in. Returns
262 * zero in case of success and a negative error code in case of failure.
263 */
264int __init ubifs_compressors_init(void)
265{
266 int err;
267
268 err = compr_init(&lzo_compr);
269 if (err)
270 return err;
271
272 err = compr_init(&zlib_compr);
273 if (err)
274 return err;
275
Piotr Wojtaszczyke60ab9a2024-05-28 17:05:28 +0200276#if IS_ENABLED(CONFIG_ZSTD)
277 err = compr_init(&zstd_compr);
278 if (err)
279 return err;
280#endif
281
Michael Lawnickb0617012009-03-19 10:06:41 +0100282 err = compr_init(&none_compr);
283 if (err)
284 return err;
285
Stefan Roese2fc10f62009-03-19 15:35:05 +0100286 return 0;
287}
288
289/*
290 * ubifsls...
291 */
292
293static int filldir(struct ubifs_info *c, const char *name, int namlen,
294 u64 ino, unsigned int d_type)
295{
296 struct inode *inode;
297 char filetime[32];
298
299 switch (d_type) {
300 case UBIFS_ITYPE_REG:
301 printf("\t");
302 break;
303 case UBIFS_ITYPE_DIR:
304 printf("<DIR>\t");
305 break;
306 case UBIFS_ITYPE_LNK:
307 printf("<LNK>\t");
308 break;
309 default:
310 printf("other\t");
311 break;
312 }
313
314 inode = ubifs_iget(c->vfs_sb, ino);
315 if (IS_ERR(inode)) {
316 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
317 __func__, ino, inode);
318 return -1;
319 }
320 ctime_r((time_t *)&inode->i_mtime, filetime);
321 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200322#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100323 ubifs_iput(inode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100325
326 printf("%s\n", name);
327
328 return 0;
329}
330
331static int ubifs_printdir(struct file *file, void *dirent)
332{
333 int err, over = 0;
334 struct qstr nm;
335 union ubifs_key key;
336 struct ubifs_dent_node *dent;
337 struct inode *dir = file->f_path.dentry->d_inode;
338 struct ubifs_info *c = dir->i_sb->s_fs_info;
339
340 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
341
342 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
343 /*
344 * The directory was seek'ed to a senseless position or there
345 * are no more entries.
346 */
347 return 0;
348
349 if (file->f_pos == 1) {
350 /* Find the first entry in TNC and save it */
351 lowest_dent_key(c, &key, dir->i_ino);
352 nm.name = NULL;
353 dent = ubifs_tnc_next_ent(c, &key, &nm);
354 if (IS_ERR(dent)) {
355 err = PTR_ERR(dent);
356 goto out;
357 }
358
359 file->f_pos = key_hash_flash(c, &dent->key);
360 file->private_data = dent;
361 }
362
363 dent = file->private_data;
364 if (!dent) {
365 /*
366 * The directory was seek'ed to and is now readdir'ed.
367 * Find the entry corresponding to @file->f_pos or the
368 * closest one.
369 */
370 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
371 nm.name = NULL;
372 dent = ubifs_tnc_next_ent(c, &key, &nm);
373 if (IS_ERR(dent)) {
374 err = PTR_ERR(dent);
375 goto out;
376 }
377 file->f_pos = key_hash_flash(c, &dent->key);
378 file->private_data = dent;
379 }
380
381 while (1) {
382 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
383 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
384 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200385#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100386 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200387#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100388
389 nm.len = le16_to_cpu(dent->nlen);
390 over = filldir(c, (char *)dent->name, nm.len,
391 le64_to_cpu(dent->inum), dent->type);
392 if (over)
393 return 0;
394
395 /* Switch to the next entry */
396 key_read(c, &dent->key, &key);
397 nm.name = (char *)dent->name;
398 dent = ubifs_tnc_next_ent(c, &key, &nm);
399 if (IS_ERR(dent)) {
400 err = PTR_ERR(dent);
401 goto out;
402 }
403
404 kfree(file->private_data);
405 file->f_pos = key_hash_flash(c, &dent->key);
406 file->private_data = dent;
407 cond_resched();
408 }
409
410out:
411 if (err != -ENOENT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200412 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100413 return err;
414 }
415
416 kfree(file->private_data);
417 file->private_data = NULL;
418 file->f_pos = 2;
419 return 0;
420}
421
422static int ubifs_finddir(struct super_block *sb, char *dirname,
423 unsigned long root_inum, unsigned long *inum)
424{
425 int err;
426 struct qstr nm;
427 union ubifs_key key;
428 struct ubifs_dent_node *dent;
429 struct ubifs_info *c;
430 struct file *file;
431 struct dentry *dentry;
432 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200433 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100434
435 file = kzalloc(sizeof(struct file), 0);
436 dentry = kzalloc(sizeof(struct dentry), 0);
437 dir = kzalloc(sizeof(struct inode), 0);
438 if (!file || !dentry || !dir) {
439 printf("%s: Error, no memory for malloc!\n", __func__);
440 err = -ENOMEM;
441 goto out;
442 }
443
444 dir->i_sb = sb;
445 file->f_path.dentry = dentry;
446 file->f_path.dentry->d_parent = dentry;
447 file->f_path.dentry->d_inode = dir;
448 file->f_path.dentry->d_inode->i_ino = root_inum;
449 c = sb->s_fs_info;
450
451 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
452
453 /* Find the first entry in TNC and save it */
454 lowest_dent_key(c, &key, dir->i_ino);
455 nm.name = NULL;
456 dent = ubifs_tnc_next_ent(c, &key, &nm);
457 if (IS_ERR(dent)) {
458 err = PTR_ERR(dent);
459 goto out;
460 }
461
462 file->f_pos = key_hash_flash(c, &dent->key);
463 file->private_data = dent;
464
465 while (1) {
466 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
467 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
468 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200469#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100470 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200471#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100472
473 nm.len = le16_to_cpu(dent->nlen);
474 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
475 (strlen(dirname) == nm.len)) {
476 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200477 ret = 1;
478 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100479 }
480
481 /* Switch to the next entry */
482 key_read(c, &dent->key, &key);
483 nm.name = (char *)dent->name;
484 dent = ubifs_tnc_next_ent(c, &key, &nm);
485 if (IS_ERR(dent)) {
486 err = PTR_ERR(dent);
487 goto out;
488 }
489
490 kfree(file->private_data);
491 file->f_pos = key_hash_flash(c, &dent->key);
492 file->private_data = dent;
493 cond_resched();
494 }
495
496out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200497 if (err != -ENOENT)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200498 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100499
Stefan Roeseac3244a2012-08-28 14:00:24 +0200500out_free:
Heinrich Schuchardte9892792017-11-08 22:28:47 +0100501 kfree(file->private_data);
502 free(file);
503 free(dentry);
504 free(dir);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100505
Stefan Roeseac3244a2012-08-28 14:00:24 +0200506 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100507}
508
509static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
510{
511 int ret;
512 char *next;
513 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200514 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100515 char *name = fpath;
516 unsigned long root_inum = 1;
517 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200518 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100519 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100520
521 strcpy(fpath, filename);
522
523 /* Remove all leading slashes */
524 while (*name == '/')
525 name++;
526
527 /*
528 * Handle root-direcoty ('/')
529 */
530 inum = root_inum;
531 if (!name || *name == '\0')
532 return inum;
533
534 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200535 struct inode *inode;
536 struct ubifs_inode *ui;
537
Stefan Roese2fc10f62009-03-19 15:35:05 +0100538 /* Extract the actual part from the pathname. */
539 next = strchr(name, '/');
540 if (next) {
541 /* Remove all leading slashes. */
542 while (*next == '/')
543 *(next++) = '\0';
544 }
545
546 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200547 if (!ret)
548 return 0;
549 inode = ubifs_iget(sb, inum);
550
551 if (!inode)
552 return 0;
553 ui = ubifs_inode(inode);
554
555 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200556 char buf[128];
557
558 /* We have some sort of symlink recursion, bail out */
559 if (symlink_count++ > 8) {
560 printf("Symlink recursion, aborting\n");
561 return 0;
562 }
563 memcpy(link_name, ui->data, ui->data_len);
564 link_name[ui->data_len] = '\0';
565
566 if (link_name[0] == '/') {
567 /* Absolute path, redo everything without
568 * the leading slash */
569 next = name = link_name + 1;
570 root_inum = 1;
571 continue;
572 }
573 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200574 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200575 link_name, next == NULL ? "" : next);
576 memcpy(symlinkpath, buf, sizeof(buf));
577 next = name = symlinkpath;
578 continue;
579 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100580
581 /*
582 * Check if directory with this name exists
583 */
584
585 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200586 if (!next || *next == '\0')
587 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100588
589 root_inum = inum;
590 name = next;
591 }
592
593 return 0;
594}
595
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600596int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goedeb5030b22015-09-17 18:46:57 -0400597{
598 if (rbdd) {
599 debug("UBIFS cannot be used with normal block devices\n");
600 return -1;
601 }
602
603 /*
Simon Glasse76ee972016-02-29 15:25:44 -0700604 * Should never happen since blk_get_device_part_str() already checks
Hans de Goedeb5030b22015-09-17 18:46:57 -0400605 * this, but better safe then sorry.
606 */
607 if (!ubifs_is_mounted()) {
608 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
609 return -1;
610 }
611
612 return 0;
613}
614
Hans de Goedea1644772015-09-17 18:46:56 -0400615int ubifs_ls(const char *filename)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100616{
617 struct ubifs_info *c = ubifs_sb->s_fs_info;
618 struct file *file;
619 struct dentry *dentry;
620 struct inode *dir;
621 void *dirent = NULL;
622 unsigned long inum;
623 int ret = 0;
624
625 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedea1644772015-09-17 18:46:56 -0400626 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100627 if (!inum) {
628 ret = -1;
629 goto out;
630 }
631
632 file = kzalloc(sizeof(struct file), 0);
633 dentry = kzalloc(sizeof(struct dentry), 0);
634 dir = kzalloc(sizeof(struct inode), 0);
635 if (!file || !dentry || !dir) {
636 printf("%s: Error, no memory for malloc!\n", __func__);
637 ret = -ENOMEM;
638 goto out_mem;
639 }
640
641 dir->i_sb = ubifs_sb;
642 file->f_path.dentry = dentry;
643 file->f_path.dentry->d_parent = dentry;
644 file->f_path.dentry->d_inode = dir;
645 file->f_path.dentry->d_inode->i_ino = inum;
646 file->f_pos = 1;
647 file->private_data = NULL;
648 ubifs_printdir(file, dirent);
649
650out_mem:
651 if (file)
652 free(file);
653 if (dentry)
654 free(dentry);
655 if (dir)
656 free(dir);
657
658out:
659 ubi_close_volume(c->ubi);
660 return ret;
661}
662
Hans de Goedeb5030b22015-09-17 18:46:57 -0400663int ubifs_exists(const char *filename)
664{
665 struct ubifs_info *c = ubifs_sb->s_fs_info;
666 unsigned long inum;
667
668 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
669 inum = ubifs_findfile(ubifs_sb, (char *)filename);
670 ubi_close_volume(c->ubi);
671
672 return inum != 0;
673}
674
675int ubifs_size(const char *filename, loff_t *size)
676{
677 struct ubifs_info *c = ubifs_sb->s_fs_info;
678 unsigned long inum;
679 struct inode *inode;
680 int err = 0;
681
682 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
683
684 inum = ubifs_findfile(ubifs_sb, (char *)filename);
685 if (!inum) {
686 err = -1;
687 goto out;
688 }
689
690 inode = ubifs_iget(ubifs_sb, inum);
691 if (IS_ERR(inode)) {
692 printf("%s: Error reading inode %ld!\n", __func__, inum);
693 err = PTR_ERR(inode);
694 goto out;
695 }
696
697 *size = inode->i_size;
698
699 ubifs_iput(inode);
700out:
701 ubi_close_volume(c->ubi);
702 return err;
703}
704
Stefan Roese2fc10f62009-03-19 15:35:05 +0100705/*
706 * ubifsload...
707 */
708
709/* file.c */
710
711static inline void *kmap(struct page *page)
712{
713 return page->addr;
714}
715
716static int read_block(struct inode *inode, void *addr, unsigned int block,
717 struct ubifs_data_node *dn)
718{
719 struct ubifs_info *c = inode->i_sb->s_fs_info;
720 int err, len, out_len;
721 union ubifs_key key;
722 unsigned int dlen;
723
724 data_key_init(c, &key, inode->i_ino, block);
725 err = ubifs_tnc_lookup(c, &key, dn);
726 if (err) {
727 if (err == -ENOENT)
728 /* Not found, so it must be a hole */
729 memset(addr, 0, UBIFS_BLOCK_SIZE);
730 return err;
731 }
732
733 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
734
735 len = le32_to_cpu(dn->size);
736 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
737 goto dump;
738
739 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
740 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200741 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese2fc10f62009-03-19 15:35:05 +0100742 le16_to_cpu(dn->compr_type));
743 if (err || len != out_len)
744 goto dump;
745
746 /*
747 * Data length can be less than a full block, even for blocks that are
748 * not the last in the file (e.g., as a result of making a hole and
749 * appending data). Ensure that the remainder is zeroed out.
750 */
751 if (len < UBIFS_BLOCK_SIZE)
752 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
753
754 return 0;
755
756dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200757 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100758 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200759 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100760 return -EINVAL;
761}
762
Stefan Roese28ea29f2010-11-01 17:28:00 +0100763static int do_readpage(struct ubifs_info *c, struct inode *inode,
764 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100765{
766 void *addr;
767 int err = 0, i;
768 unsigned int block, beyond;
769 struct ubifs_data_node *dn;
770 loff_t i_size = inode->i_size;
771
772 dbg_gen("ino %lu, pg %lu, i_size %lld",
773 inode->i_ino, page->index, i_size);
774
775 addr = kmap(page);
776
777 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
778 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
779 if (block >= beyond) {
780 /* Reading beyond inode */
781 memset(addr, 0, PAGE_CACHE_SIZE);
782 goto out;
783 }
784
785 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200786 if (!dn)
787 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100788
789 i = 0;
790 while (1) {
791 int ret;
792
793 if (block >= beyond) {
794 /* Reading beyond inode */
795 err = -ENOENT;
796 memset(addr, 0, UBIFS_BLOCK_SIZE);
797 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100798 /*
799 * Reading last block? Make sure to not write beyond
800 * the requested size in the destination buffer.
801 */
802 if (((block + 1) == beyond) || last_block_size) {
803 void *buff;
804 int dlen;
805
806 /*
807 * We need to buffer the data locally for the
808 * last block. This is to not pad the
809 * destination area to a multiple of
810 * UBIFS_BLOCK_SIZE.
811 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200812 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roese28ea29f2010-11-01 17:28:00 +0100813 if (!buff) {
814 printf("%s: Error, malloc fails!\n",
815 __func__);
816 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100817 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100818 }
819
820 /* Read block-size into temp buffer */
821 ret = read_block(inode, buff, block, dn);
822 if (ret) {
823 err = ret;
824 if (err != -ENOENT) {
825 free(buff);
826 break;
827 }
828 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100829
Stefan Roese28ea29f2010-11-01 17:28:00 +0100830 if (last_block_size)
831 dlen = last_block_size;
Pali Rohár8d2176a2022-05-17 22:45:28 +0200832 else if (ret)
833 dlen = UBIFS_BLOCK_SIZE;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100834 else
835 dlen = le32_to_cpu(dn->size);
836
837 /* Now copy required size back to dest */
838 memcpy(addr, buff, dlen);
839
840 free(buff);
841 } else {
842 ret = read_block(inode, addr, block, dn);
843 if (ret) {
844 err = ret;
845 if (err != -ENOENT)
846 break;
847 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100848 }
849 }
850 if (++i >= UBIFS_BLOCKS_PER_PAGE)
851 break;
852 block += 1;
853 addr += UBIFS_BLOCK_SIZE;
854 }
855 if (err) {
856 if (err == -ENOENT) {
857 /* Not found, so it must be a hole */
858 dbg_gen("hole");
859 goto out_free;
860 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200861 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100862 page->index, inode->i_ino, err);
863 goto error;
864 }
865
866out_free:
867 kfree(dn);
868out:
869 return 0;
870
871error:
872 kfree(dn);
873 return err;
874}
875
Hans de Goedea1644772015-09-17 18:46:56 -0400876int ubifs_read(const char *filename, void *buf, loff_t offset,
877 loff_t size, loff_t *actread)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100878{
879 struct ubifs_info *c = ubifs_sb->s_fs_info;
880 unsigned long inum;
881 struct inode *inode;
882 struct page page;
883 int err = 0;
884 int i;
885 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100886 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100887
Hans de Goedea1644772015-09-17 18:46:56 -0400888 *actread = 0;
889
890 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadianbfdfd102016-10-23 20:45:16 -0700891 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedea1644772015-09-17 18:46:56 -0400892 PAGE_SIZE);
893 return -1;
894 }
895
Stefan Roese2fc10f62009-03-19 15:35:05 +0100896 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200897 /* ubifs_findfile will resolve symlinks, so we know that we get
898 * the real file here */
Hans de Goedea1644772015-09-17 18:46:56 -0400899 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100900 if (!inum) {
901 err = -1;
902 goto out;
903 }
904
905 /*
906 * Read file inode
907 */
908 inode = ubifs_iget(ubifs_sb, inum);
909 if (IS_ERR(inode)) {
910 printf("%s: Error reading inode %ld!\n", __func__, inum);
911 err = PTR_ERR(inode);
912 goto out;
913 }
914
Hans de Goedea1644772015-09-17 18:46:56 -0400915 if (offset > inode->i_size) {
916 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
917 offset, size);
918 err = -1;
919 goto put_inode;
920 }
921
Stefan Roese2fc10f62009-03-19 15:35:05 +0100922 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100923 * If no size was specified or if size bigger than filesize
924 * set size to filesize
925 */
Hans de Goedea1644772015-09-17 18:46:56 -0400926 if ((size == 0) || (size > (inode->i_size - offset)))
927 size = inode->i_size - offset;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100928
929 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100930
Hans de Goedea1644772015-09-17 18:46:56 -0400931 page.addr = buf;
932 page.index = offset / PAGE_SIZE;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100933 page.inode = inode;
934 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100935 /*
936 * Make sure to not read beyond the requested size
937 */
938 if (((i + 1) == count) && (size < inode->i_size))
939 last_block_size = size - (i * PAGE_SIZE);
940
941 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100942 if (err)
943 break;
944
945 page.addr += PAGE_SIZE;
946 page.index++;
947 }
948
Hans de Goedea1644772015-09-17 18:46:56 -0400949 if (err) {
Stefan Roese2fc10f62009-03-19 15:35:05 +0100950 printf("Error reading file '%s'\n", filename);
Hans de Goedea1644772015-09-17 18:46:56 -0400951 *actread = i * PAGE_SIZE;
952 } else {
953 *actread = size;
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000954 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100955
Hans de Goedea1644772015-09-17 18:46:56 -0400956put_inode:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100957 ubifs_iput(inode);
958
959out:
960 ubi_close_volume(c->ubi);
961 return err;
962}
Hans de Goedea1644772015-09-17 18:46:56 -0400963
Hans de Goedeb5030b22015-09-17 18:46:57 -0400964void ubifs_close(void)
965{
966}
967
Hans de Goedea1644772015-09-17 18:46:56 -0400968/* Compat wrappers for common/cmd_ubifs.c */
Ben Dooks68ebcf72023-06-06 09:23:28 +0100969int ubifs_load(char *filename, unsigned long addr, u32 size)
Hans de Goedea1644772015-09-17 18:46:56 -0400970{
971 loff_t actread;
972 int err;
973
Ben Dooks68ebcf72023-06-06 09:23:28 +0100974 printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr);
Hans de Goedea1644772015-09-17 18:46:56 -0400975
Siva Durga Prasad Paladugu67eccfb2017-05-30 14:29:06 +0200976 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400977 if (err == 0) {
Simon Glass4d949a22017-08-03 12:22:10 -0600978 env_set_hex("filesize", actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400979 printf("Done\n");
980 }
981
982 return err;
983}
984
985void uboot_ubifs_umount(void)
986{
987 if (ubifs_sb) {
988 printf("Unmounting UBIFS volume %s!\n",
989 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
990 ubifs_umount(ubifs_sb->s_fs_info);
991 ubifs_sb = NULL;
992 }
993}