blob: 398b076d783af46421bf9dcabb51886a2a3db586 [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);
322 ubifs_iput(inode);
323
324 printf("%s\n", name);
325
326 return 0;
327}
328
329static int ubifs_printdir(struct file *file, void *dirent)
330{
331 int err, over = 0;
332 struct qstr nm;
333 union ubifs_key key;
334 struct ubifs_dent_node *dent;
335 struct inode *dir = file->f_path.dentry->d_inode;
336 struct ubifs_info *c = dir->i_sb->s_fs_info;
337
338 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
339
340 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
341 /*
342 * The directory was seek'ed to a senseless position or there
343 * are no more entries.
344 */
345 return 0;
346
347 if (file->f_pos == 1) {
348 /* Find the first entry in TNC and save it */
349 lowest_dent_key(c, &key, dir->i_ino);
350 nm.name = NULL;
351 dent = ubifs_tnc_next_ent(c, &key, &nm);
352 if (IS_ERR(dent)) {
353 err = PTR_ERR(dent);
354 goto out;
355 }
356
357 file->f_pos = key_hash_flash(c, &dent->key);
358 file->private_data = dent;
359 }
360
361 dent = file->private_data;
362 if (!dent) {
363 /*
364 * The directory was seek'ed to and is now readdir'ed.
365 * Find the entry corresponding to @file->f_pos or the
366 * closest one.
367 */
368 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
369 nm.name = NULL;
370 dent = ubifs_tnc_next_ent(c, &key, &nm);
371 if (IS_ERR(dent)) {
372 err = PTR_ERR(dent);
373 goto out;
374 }
375 file->f_pos = key_hash_flash(c, &dent->key);
376 file->private_data = dent;
377 }
378
379 while (1) {
380 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
381 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
382 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200383#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100384 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200385#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100386
387 nm.len = le16_to_cpu(dent->nlen);
388 over = filldir(c, (char *)dent->name, nm.len,
389 le64_to_cpu(dent->inum), dent->type);
390 if (over)
391 return 0;
392
393 /* Switch to the next entry */
394 key_read(c, &dent->key, &key);
395 nm.name = (char *)dent->name;
396 dent = ubifs_tnc_next_ent(c, &key, &nm);
397 if (IS_ERR(dent)) {
398 err = PTR_ERR(dent);
399 goto out;
400 }
401
402 kfree(file->private_data);
403 file->f_pos = key_hash_flash(c, &dent->key);
404 file->private_data = dent;
405 cond_resched();
406 }
407
408out:
409 if (err != -ENOENT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200410 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100411 return err;
412 }
413
414 kfree(file->private_data);
415 file->private_data = NULL;
416 file->f_pos = 2;
417 return 0;
418}
419
420static int ubifs_finddir(struct super_block *sb, char *dirname,
421 unsigned long root_inum, unsigned long *inum)
422{
423 int err;
424 struct qstr nm;
425 union ubifs_key key;
426 struct ubifs_dent_node *dent;
427 struct ubifs_info *c;
428 struct file *file;
429 struct dentry *dentry;
430 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200431 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100432
433 file = kzalloc(sizeof(struct file), 0);
434 dentry = kzalloc(sizeof(struct dentry), 0);
435 dir = kzalloc(sizeof(struct inode), 0);
436 if (!file || !dentry || !dir) {
437 printf("%s: Error, no memory for malloc!\n", __func__);
438 err = -ENOMEM;
439 goto out;
440 }
441
442 dir->i_sb = sb;
443 file->f_path.dentry = dentry;
444 file->f_path.dentry->d_parent = dentry;
445 file->f_path.dentry->d_inode = dir;
446 file->f_path.dentry->d_inode->i_ino = root_inum;
447 c = sb->s_fs_info;
448
449 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
450
451 /* Find the first entry in TNC and save it */
452 lowest_dent_key(c, &key, dir->i_ino);
453 nm.name = NULL;
454 dent = ubifs_tnc_next_ent(c, &key, &nm);
455 if (IS_ERR(dent)) {
456 err = PTR_ERR(dent);
457 goto out;
458 }
459
460 file->f_pos = key_hash_flash(c, &dent->key);
461 file->private_data = dent;
462
463 while (1) {
464 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
465 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
466 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200467#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100468 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200469#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100470
471 nm.len = le16_to_cpu(dent->nlen);
472 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
473 (strlen(dirname) == nm.len)) {
474 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200475 ret = 1;
476 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100477 }
478
479 /* Switch to the next entry */
480 key_read(c, &dent->key, &key);
481 nm.name = (char *)dent->name;
482 dent = ubifs_tnc_next_ent(c, &key, &nm);
483 if (IS_ERR(dent)) {
484 err = PTR_ERR(dent);
485 goto out;
486 }
487
488 kfree(file->private_data);
489 file->f_pos = key_hash_flash(c, &dent->key);
490 file->private_data = dent;
491 cond_resched();
492 }
493
494out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200495 if (err != -ENOENT)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200496 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100497
Stefan Roeseac3244a2012-08-28 14:00:24 +0200498out_free:
Heinrich Schuchardte9892792017-11-08 22:28:47 +0100499 kfree(file->private_data);
500 free(file);
501 free(dentry);
502 free(dir);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100503
Stefan Roeseac3244a2012-08-28 14:00:24 +0200504 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100505}
506
507static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
508{
509 int ret;
510 char *next;
511 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200512 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100513 char *name = fpath;
514 unsigned long root_inum = 1;
515 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200516 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100517 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100518
519 strcpy(fpath, filename);
520
521 /* Remove all leading slashes */
522 while (*name == '/')
523 name++;
524
525 /*
526 * Handle root-direcoty ('/')
527 */
528 inum = root_inum;
529 if (!name || *name == '\0')
530 return inum;
531
532 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200533 struct inode *inode;
534 struct ubifs_inode *ui;
535
Stefan Roese2fc10f62009-03-19 15:35:05 +0100536 /* Extract the actual part from the pathname. */
537 next = strchr(name, '/');
538 if (next) {
539 /* Remove all leading slashes. */
540 while (*next == '/')
541 *(next++) = '\0';
542 }
543
544 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200545 if (!ret)
546 return 0;
547 inode = ubifs_iget(sb, inum);
548
549 if (!inode)
550 return 0;
551 ui = ubifs_inode(inode);
552
553 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200554 char buf[128];
555
556 /* We have some sort of symlink recursion, bail out */
557 if (symlink_count++ > 8) {
Michael Trimarchicaa32c92024-08-10 14:57:44 +0200558 ubifs_iput(inode);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200559 printf("Symlink recursion, aborting\n");
560 return 0;
561 }
562 memcpy(link_name, ui->data, ui->data_len);
563 link_name[ui->data_len] = '\0';
564
565 if (link_name[0] == '/') {
566 /* Absolute path, redo everything without
567 * the leading slash */
568 next = name = link_name + 1;
569 root_inum = 1;
Michael Trimarchicaa32c92024-08-10 14:57:44 +0200570 ubifs_iput(inode);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200571 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;
Michael Trimarchicaa32c92024-08-10 14:57:44 +0200578 ubifs_iput(inode);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200579 continue;
580 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100581
582 /*
583 * Check if directory with this name exists
584 */
585
586 /* Found the node! */
Michael Trimarchicaa32c92024-08-10 14:57:44 +0200587 if (!next || *next == '\0') {
588 ubifs_iput(inode);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200589 return inum;
Michael Trimarchicaa32c92024-08-10 14:57:44 +0200590 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100591
592 root_inum = inum;
593 name = next;
594 }
595
596 return 0;
597}
598
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600599int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goedeb5030b22015-09-17 18:46:57 -0400600{
601 if (rbdd) {
602 debug("UBIFS cannot be used with normal block devices\n");
603 return -1;
604 }
605
606 /*
Simon Glasse76ee972016-02-29 15:25:44 -0700607 * Should never happen since blk_get_device_part_str() already checks
Hans de Goedeb5030b22015-09-17 18:46:57 -0400608 * this, but better safe then sorry.
609 */
610 if (!ubifs_is_mounted()) {
611 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
612 return -1;
613 }
614
615 return 0;
616}
617
Hans de Goedea1644772015-09-17 18:46:56 -0400618int ubifs_ls(const char *filename)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100619{
Stefan Roese2fc10f62009-03-19 15:35:05 +0100620 struct file *file;
621 struct dentry *dentry;
622 struct inode *dir;
623 void *dirent = NULL;
624 unsigned long inum;
625 int ret = 0;
626
Alexander Dahl41ac3b62024-07-03 12:12:58 +0200627 if (!ubifs_is_mounted()) {
628 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
629 return -1;
630 }
631
Hans de Goedea1644772015-09-17 18:46:56 -0400632 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100633 if (!inum) {
634 ret = -1;
635 goto out;
636 }
637
638 file = kzalloc(sizeof(struct file), 0);
639 dentry = kzalloc(sizeof(struct dentry), 0);
640 dir = kzalloc(sizeof(struct inode), 0);
641 if (!file || !dentry || !dir) {
642 printf("%s: Error, no memory for malloc!\n", __func__);
643 ret = -ENOMEM;
644 goto out_mem;
645 }
646
647 dir->i_sb = ubifs_sb;
648 file->f_path.dentry = dentry;
649 file->f_path.dentry->d_parent = dentry;
650 file->f_path.dentry->d_inode = dir;
651 file->f_path.dentry->d_inode->i_ino = inum;
652 file->f_pos = 1;
653 file->private_data = NULL;
654 ubifs_printdir(file, dirent);
655
656out_mem:
657 if (file)
658 free(file);
659 if (dentry)
660 free(dentry);
661 if (dir)
662 free(dir);
663
664out:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100665 return ret;
666}
667
Hans de Goedeb5030b22015-09-17 18:46:57 -0400668int ubifs_exists(const char *filename)
669{
Hans de Goedeb5030b22015-09-17 18:46:57 -0400670 unsigned long inum;
671
Alexander Dahl41ac3b62024-07-03 12:12:58 +0200672 if (!ubifs_is_mounted()) {
673 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
674 return -1;
675 }
676
Hans de Goedeb5030b22015-09-17 18:46:57 -0400677 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Hans de Goedeb5030b22015-09-17 18:46:57 -0400678
679 return inum != 0;
680}
681
682int ubifs_size(const char *filename, loff_t *size)
683{
Hans de Goedeb5030b22015-09-17 18:46:57 -0400684 unsigned long inum;
685 struct inode *inode;
686 int err = 0;
687
Alexander Dahl41ac3b62024-07-03 12:12:58 +0200688 if (!ubifs_is_mounted()) {
689 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
690 return -1;
691 }
692
Hans de Goedeb5030b22015-09-17 18:46:57 -0400693 inum = ubifs_findfile(ubifs_sb, (char *)filename);
694 if (!inum) {
695 err = -1;
696 goto out;
697 }
698
699 inode = ubifs_iget(ubifs_sb, inum);
700 if (IS_ERR(inode)) {
701 printf("%s: Error reading inode %ld!\n", __func__, inum);
702 err = PTR_ERR(inode);
703 goto out;
704 }
705
706 *size = inode->i_size;
707
708 ubifs_iput(inode);
709out:
Hans de Goedeb5030b22015-09-17 18:46:57 -0400710 return err;
711}
712
Stefan Roese2fc10f62009-03-19 15:35:05 +0100713/*
714 * ubifsload...
715 */
716
717/* file.c */
718
719static inline void *kmap(struct page *page)
720{
721 return page->addr;
722}
723
724static int read_block(struct inode *inode, void *addr, unsigned int block,
725 struct ubifs_data_node *dn)
726{
727 struct ubifs_info *c = inode->i_sb->s_fs_info;
728 int err, len, out_len;
729 union ubifs_key key;
730 unsigned int dlen;
731
732 data_key_init(c, &key, inode->i_ino, block);
733 err = ubifs_tnc_lookup(c, &key, dn);
734 if (err) {
735 if (err == -ENOENT)
736 /* Not found, so it must be a hole */
737 memset(addr, 0, UBIFS_BLOCK_SIZE);
738 return err;
739 }
740
741 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
742
743 len = le32_to_cpu(dn->size);
744 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
745 goto dump;
746
747 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
748 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200749 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese2fc10f62009-03-19 15:35:05 +0100750 le16_to_cpu(dn->compr_type));
751 if (err || len != out_len)
752 goto dump;
753
754 /*
755 * Data length can be less than a full block, even for blocks that are
756 * not the last in the file (e.g., as a result of making a hole and
757 * appending data). Ensure that the remainder is zeroed out.
758 */
759 if (len < UBIFS_BLOCK_SIZE)
760 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
761
762 return 0;
763
764dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200765 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100766 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200767 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100768 return -EINVAL;
769}
770
Stefan Roese28ea29f2010-11-01 17:28:00 +0100771static int do_readpage(struct ubifs_info *c, struct inode *inode,
772 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100773{
774 void *addr;
775 int err = 0, i;
776 unsigned int block, beyond;
777 struct ubifs_data_node *dn;
778 loff_t i_size = inode->i_size;
779
780 dbg_gen("ino %lu, pg %lu, i_size %lld",
781 inode->i_ino, page->index, i_size);
782
783 addr = kmap(page);
784
785 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
786 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
787 if (block >= beyond) {
788 /* Reading beyond inode */
789 memset(addr, 0, PAGE_CACHE_SIZE);
790 goto out;
791 }
792
793 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200794 if (!dn)
795 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100796
797 i = 0;
798 while (1) {
799 int ret;
800
801 if (block >= beyond) {
802 /* Reading beyond inode */
803 err = -ENOENT;
804 memset(addr, 0, UBIFS_BLOCK_SIZE);
805 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100806 /*
807 * Reading last block? Make sure to not write beyond
808 * the requested size in the destination buffer.
809 */
810 if (((block + 1) == beyond) || last_block_size) {
811 void *buff;
812 int dlen;
813
814 /*
815 * We need to buffer the data locally for the
816 * last block. This is to not pad the
817 * destination area to a multiple of
818 * UBIFS_BLOCK_SIZE.
819 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200820 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roese28ea29f2010-11-01 17:28:00 +0100821 if (!buff) {
822 printf("%s: Error, malloc fails!\n",
823 __func__);
824 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100825 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100826 }
827
828 /* Read block-size into temp buffer */
829 ret = read_block(inode, buff, block, dn);
830 if (ret) {
831 err = ret;
832 if (err != -ENOENT) {
833 free(buff);
834 break;
835 }
836 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100837
Stefan Roese28ea29f2010-11-01 17:28:00 +0100838 if (last_block_size)
839 dlen = last_block_size;
Pali Rohár8d2176a2022-05-17 22:45:28 +0200840 else if (ret)
841 dlen = UBIFS_BLOCK_SIZE;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100842 else
843 dlen = le32_to_cpu(dn->size);
844
845 /* Now copy required size back to dest */
846 memcpy(addr, buff, dlen);
847
848 free(buff);
849 } else {
850 ret = read_block(inode, addr, block, dn);
851 if (ret) {
852 err = ret;
853 if (err != -ENOENT)
854 break;
855 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100856 }
857 }
858 if (++i >= UBIFS_BLOCKS_PER_PAGE)
859 break;
860 block += 1;
861 addr += UBIFS_BLOCK_SIZE;
862 }
863 if (err) {
864 if (err == -ENOENT) {
865 /* Not found, so it must be a hole */
866 dbg_gen("hole");
867 goto out_free;
868 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200869 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100870 page->index, inode->i_ino, err);
871 goto error;
872 }
873
874out_free:
875 kfree(dn);
876out:
877 return 0;
878
879error:
880 kfree(dn);
881 return err;
882}
883
Hans de Goedea1644772015-09-17 18:46:56 -0400884int ubifs_read(const char *filename, void *buf, loff_t offset,
885 loff_t size, loff_t *actread)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100886{
887 struct ubifs_info *c = ubifs_sb->s_fs_info;
888 unsigned long inum;
889 struct inode *inode;
890 struct page page;
891 int err = 0;
892 int i;
893 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100894 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100895
Alexander Dahl41ac3b62024-07-03 12:12:58 +0200896 if (!ubifs_is_mounted()) {
897 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
898 return -1;
899 }
900
Hans de Goedea1644772015-09-17 18:46:56 -0400901 *actread = 0;
902
903 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadianbfdfd102016-10-23 20:45:16 -0700904 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedea1644772015-09-17 18:46:56 -0400905 PAGE_SIZE);
906 return -1;
907 }
908
Simon Kagstrom877501a2009-09-15 09:53:29 +0200909 /* ubifs_findfile will resolve symlinks, so we know that we get
910 * the real file here */
Hans de Goedea1644772015-09-17 18:46:56 -0400911 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100912 if (!inum) {
913 err = -1;
914 goto out;
915 }
916
917 /*
918 * Read file inode
919 */
920 inode = ubifs_iget(ubifs_sb, inum);
921 if (IS_ERR(inode)) {
922 printf("%s: Error reading inode %ld!\n", __func__, inum);
923 err = PTR_ERR(inode);
924 goto out;
925 }
926
Hans de Goedea1644772015-09-17 18:46:56 -0400927 if (offset > inode->i_size) {
928 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
929 offset, size);
930 err = -1;
931 goto put_inode;
932 }
933
Stefan Roese2fc10f62009-03-19 15:35:05 +0100934 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100935 * If no size was specified or if size bigger than filesize
936 * set size to filesize
937 */
Hans de Goedea1644772015-09-17 18:46:56 -0400938 if ((size == 0) || (size > (inode->i_size - offset)))
939 size = inode->i_size - offset;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100940
941 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100942
Hans de Goedea1644772015-09-17 18:46:56 -0400943 page.addr = buf;
944 page.index = offset / PAGE_SIZE;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100945 page.inode = inode;
946 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100947 /*
948 * Make sure to not read beyond the requested size
949 */
950 if (((i + 1) == count) && (size < inode->i_size))
951 last_block_size = size - (i * PAGE_SIZE);
952
953 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100954 if (err)
955 break;
956
957 page.addr += PAGE_SIZE;
958 page.index++;
959 }
960
Hans de Goedea1644772015-09-17 18:46:56 -0400961 if (err) {
Stefan Roese2fc10f62009-03-19 15:35:05 +0100962 printf("Error reading file '%s'\n", filename);
Hans de Goedea1644772015-09-17 18:46:56 -0400963 *actread = i * PAGE_SIZE;
964 } else {
965 *actread = size;
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000966 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100967
Hans de Goedea1644772015-09-17 18:46:56 -0400968put_inode:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100969 ubifs_iput(inode);
970
971out:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100972 return err;
973}
Hans de Goedea1644772015-09-17 18:46:56 -0400974
Hans de Goedeb5030b22015-09-17 18:46:57 -0400975void ubifs_close(void)
976{
977}
978
Hans de Goedea1644772015-09-17 18:46:56 -0400979/* Compat wrappers for common/cmd_ubifs.c */
Ben Dooks68ebcf72023-06-06 09:23:28 +0100980int ubifs_load(char *filename, unsigned long addr, u32 size)
Hans de Goedea1644772015-09-17 18:46:56 -0400981{
982 loff_t actread;
983 int err;
984
Ben Dooks68ebcf72023-06-06 09:23:28 +0100985 printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr);
Hans de Goedea1644772015-09-17 18:46:56 -0400986
Siva Durga Prasad Paladugu67eccfb2017-05-30 14:29:06 +0200987 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400988 if (err == 0) {
Simon Glass4d949a22017-08-03 12:22:10 -0600989 env_set_hex("filesize", actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400990 printf("Done\n");
991 }
992
993 return err;
994}
995
996void uboot_ubifs_umount(void)
997{
998 if (ubifs_sb) {
999 printf("Unmounting UBIFS volume %s!\n",
1000 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
1001 ubifs_umount(ubifs_sb->s_fs_info);
Hans de Goedea1644772015-09-17 18:46:56 -04001002 }
1003}