blob: 75de01e95f7cb8b4cdb813b31124e3f401c11e96 [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
Stefan Roese2fc10f62009-03-19 15:35:05 +010029DECLARE_GLOBAL_DATA_PTR;
30
31/* compress.c */
32
33/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020034 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010035 * incompatible with the lzo decompressor.
36 */
37static int gzip_decompress(const unsigned char *in, size_t in_len,
38 unsigned char *out, size_t *out_len)
39{
Veli-Pekka Peltola7b7e1552012-09-05 18:05:14 +030040 return zunzip(out, *out_len, (unsigned char *)in,
41 (unsigned long *)out_len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010042}
43
44/* Fake description object for the "none" compressor */
45static struct ubifs_compressor none_compr = {
46 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherf5895d12014-06-24 10:10:04 +020047 .name = "none",
Stefan Roese2fc10f62009-03-19 15:35:05 +010048 .capi_name = "",
49 .decompress = NULL,
50};
51
52static struct ubifs_compressor lzo_compr = {
53 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherf5895d12014-06-24 10:10:04 +020054#ifndef __UBOOT__
55 .comp_mutex = &lzo_mutex,
56#endif
57 .name = "lzo",
Stefan Roese2fc10f62009-03-19 15:35:05 +010058 .capi_name = "lzo",
59 .decompress = lzo1x_decompress_safe,
60};
61
62static struct ubifs_compressor zlib_compr = {
63 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherf5895d12014-06-24 10:10:04 +020064#ifndef __UBOOT__
65 .comp_mutex = &deflate_mutex,
66 .decomp_mutex = &inflate_mutex,
67#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010068 .name = "zlib",
69 .capi_name = "deflate",
70 .decompress = gzip_decompress,
71};
72
73/* All UBIFS compressors */
74struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
75
Heiko Schocherf5895d12014-06-24 10:10:04 +020076
77#ifdef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +020078
79struct crypto_comp {
80 int compressor;
81};
82
Heiko Schocher94b66de2015-10-22 06:19:21 +020083static inline struct crypto_comp
84*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherf5895d12014-06-24 10:10:04 +020085{
86 struct ubifs_compressor *comp;
87 struct crypto_comp *ptr;
88 int i = 0;
89
Marcel Ziswilerabc574b2015-08-18 13:06:37 +020090 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherf5895d12014-06-24 10:10:04 +020091 while (i < UBIFS_COMPR_TYPES_CNT) {
92 comp = ubifs_compressors[i];
93 if (!comp) {
94 i++;
95 continue;
96 }
97 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
98 ptr->compressor = i;
99 return ptr;
100 }
101 i++;
102 }
103 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200104 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200105 free (ptr);
106 return NULL;
107 }
108 return ptr;
109}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200110static inline int
111crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
112 const u8 *src, unsigned int slen, u8 *dst,
113 unsigned int *dlen)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114{
115 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
116 int err;
Paul Davey7f918382018-11-05 18:09:29 +1300117 size_t tmp_len = *dlen;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200118
119 if (compr->compr_type == UBIFS_COMPR_NONE) {
120 memcpy(dst, src, slen);
121 *dlen = slen;
122 return 0;
123 }
124
Paul Davey7f918382018-11-05 18:09:29 +1300125 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200126 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200127 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherf5895d12014-06-24 10:10:04 +0200128 "error %d", slen, compr->name, err);
129
Paul Davey7f918382018-11-05 18:09:29 +1300130 *dlen = tmp_len;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200131 return err;
132
133 return 0;
134}
Anton Habegger62e79dd2015-01-22 22:29:10 +0100135
136/* from shrinker.c */
137
138/* Global clean znode counter (for all mounted UBIFS instances) */
139atomic_long_t ubifs_clean_zn_cnt;
140
Heiko Schocherf5895d12014-06-24 10:10:04 +0200141#endif
142
Stefan Roese2fc10f62009-03-19 15:35:05 +0100143/**
144 * ubifs_decompress - decompress data.
145 * @in_buf: data to decompress
146 * @in_len: length of the data to decompress
147 * @out_buf: output buffer where decompressed data should
148 * @out_len: output length is returned here
149 * @compr_type: type of compression
150 *
151 * This function decompresses data from buffer @in_buf into buffer @out_buf.
152 * The length of the uncompressed data is returned in @out_len. This functions
153 * returns %0 on success or a negative error code on failure.
154 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200155int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
156 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100157{
158 int err;
159 struct ubifs_compressor *compr;
160
161 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200162 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100163 return -EINVAL;
164 }
165
166 compr = ubifs_compressors[compr_type];
167
168 if (unlikely(!compr->capi_name)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200169 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100170 return -EINVAL;
171 }
172
173 if (compr_type == UBIFS_COMPR_NONE) {
174 memcpy(out_buf, in_buf, in_len);
175 *out_len = in_len;
176 return 0;
177 }
178
Heiko Schocherf5895d12014-06-24 10:10:04 +0200179 if (compr->decomp_mutex)
180 mutex_lock(compr->decomp_mutex);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200181 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200182 (unsigned int *)out_len);
183 if (compr->decomp_mutex)
184 mutex_unlock(compr->decomp_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100185 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200186 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
187 " error %d", in_len, compr->name, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100188
189 return err;
190}
191
192/**
193 * compr_init - initialize a compressor.
194 * @compr: compressor description object
195 *
196 * This function initializes the requested compressor and returns zero in case
197 * of success or a negative error code in case of failure.
198 */
199static int __init compr_init(struct ubifs_compressor *compr)
200{
201 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500202
Heiko Schocherf5895d12014-06-24 10:10:04 +0200203 if (compr->capi_name) {
204 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
205 if (IS_ERR(compr->cc)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200206 dbg_gen("cannot initialize compressor %s,"
207 " error %ld", compr->name,
208 PTR_ERR(compr->cc));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200209 return PTR_ERR(compr->cc);
210 }
211 }
212
Stefan Roese2fc10f62009-03-19 15:35:05 +0100213 return 0;
214}
215
216/**
217 * ubifs_compressors_init - initialize UBIFS compressors.
218 *
219 * This function initializes the compressor which were compiled in. Returns
220 * zero in case of success and a negative error code in case of failure.
221 */
222int __init ubifs_compressors_init(void)
223{
224 int err;
225
226 err = compr_init(&lzo_compr);
227 if (err)
228 return err;
229
230 err = compr_init(&zlib_compr);
231 if (err)
232 return err;
233
Michael Lawnickb0617012009-03-19 10:06:41 +0100234 err = compr_init(&none_compr);
235 if (err)
236 return err;
237
Stefan Roese2fc10f62009-03-19 15:35:05 +0100238 return 0;
239}
240
241/*
242 * ubifsls...
243 */
244
245static int filldir(struct ubifs_info *c, const char *name, int namlen,
246 u64 ino, unsigned int d_type)
247{
248 struct inode *inode;
249 char filetime[32];
250
251 switch (d_type) {
252 case UBIFS_ITYPE_REG:
253 printf("\t");
254 break;
255 case UBIFS_ITYPE_DIR:
256 printf("<DIR>\t");
257 break;
258 case UBIFS_ITYPE_LNK:
259 printf("<LNK>\t");
260 break;
261 default:
262 printf("other\t");
263 break;
264 }
265
266 inode = ubifs_iget(c->vfs_sb, ino);
267 if (IS_ERR(inode)) {
268 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
269 __func__, ino, inode);
270 return -1;
271 }
272 ctime_r((time_t *)&inode->i_mtime, filetime);
273 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200274#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100275 ubifs_iput(inode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200276#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100277
278 printf("%s\n", name);
279
280 return 0;
281}
282
283static int ubifs_printdir(struct file *file, void *dirent)
284{
285 int err, over = 0;
286 struct qstr nm;
287 union ubifs_key key;
288 struct ubifs_dent_node *dent;
289 struct inode *dir = file->f_path.dentry->d_inode;
290 struct ubifs_info *c = dir->i_sb->s_fs_info;
291
292 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
293
294 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
295 /*
296 * The directory was seek'ed to a senseless position or there
297 * are no more entries.
298 */
299 return 0;
300
301 if (file->f_pos == 1) {
302 /* Find the first entry in TNC and save it */
303 lowest_dent_key(c, &key, dir->i_ino);
304 nm.name = NULL;
305 dent = ubifs_tnc_next_ent(c, &key, &nm);
306 if (IS_ERR(dent)) {
307 err = PTR_ERR(dent);
308 goto out;
309 }
310
311 file->f_pos = key_hash_flash(c, &dent->key);
312 file->private_data = dent;
313 }
314
315 dent = file->private_data;
316 if (!dent) {
317 /*
318 * The directory was seek'ed to and is now readdir'ed.
319 * Find the entry corresponding to @file->f_pos or the
320 * closest one.
321 */
322 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
323 nm.name = NULL;
324 dent = ubifs_tnc_next_ent(c, &key, &nm);
325 if (IS_ERR(dent)) {
326 err = PTR_ERR(dent);
327 goto out;
328 }
329 file->f_pos = key_hash_flash(c, &dent->key);
330 file->private_data = dent;
331 }
332
333 while (1) {
334 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
335 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
336 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200337#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100338 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200339#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100340
341 nm.len = le16_to_cpu(dent->nlen);
342 over = filldir(c, (char *)dent->name, nm.len,
343 le64_to_cpu(dent->inum), dent->type);
344 if (over)
345 return 0;
346
347 /* Switch to the next entry */
348 key_read(c, &dent->key, &key);
349 nm.name = (char *)dent->name;
350 dent = ubifs_tnc_next_ent(c, &key, &nm);
351 if (IS_ERR(dent)) {
352 err = PTR_ERR(dent);
353 goto out;
354 }
355
356 kfree(file->private_data);
357 file->f_pos = key_hash_flash(c, &dent->key);
358 file->private_data = dent;
359 cond_resched();
360 }
361
362out:
363 if (err != -ENOENT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200364 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100365 return err;
366 }
367
368 kfree(file->private_data);
369 file->private_data = NULL;
370 file->f_pos = 2;
371 return 0;
372}
373
374static int ubifs_finddir(struct super_block *sb, char *dirname,
375 unsigned long root_inum, unsigned long *inum)
376{
377 int err;
378 struct qstr nm;
379 union ubifs_key key;
380 struct ubifs_dent_node *dent;
381 struct ubifs_info *c;
382 struct file *file;
383 struct dentry *dentry;
384 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200385 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100386
387 file = kzalloc(sizeof(struct file), 0);
388 dentry = kzalloc(sizeof(struct dentry), 0);
389 dir = kzalloc(sizeof(struct inode), 0);
390 if (!file || !dentry || !dir) {
391 printf("%s: Error, no memory for malloc!\n", __func__);
392 err = -ENOMEM;
393 goto out;
394 }
395
396 dir->i_sb = sb;
397 file->f_path.dentry = dentry;
398 file->f_path.dentry->d_parent = dentry;
399 file->f_path.dentry->d_inode = dir;
400 file->f_path.dentry->d_inode->i_ino = root_inum;
401 c = sb->s_fs_info;
402
403 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
404
405 /* Find the first entry in TNC and save it */
406 lowest_dent_key(c, &key, dir->i_ino);
407 nm.name = NULL;
408 dent = ubifs_tnc_next_ent(c, &key, &nm);
409 if (IS_ERR(dent)) {
410 err = PTR_ERR(dent);
411 goto out;
412 }
413
414 file->f_pos = key_hash_flash(c, &dent->key);
415 file->private_data = dent;
416
417 while (1) {
418 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
419 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
420 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200421#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100422 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200423#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100424
425 nm.len = le16_to_cpu(dent->nlen);
426 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
427 (strlen(dirname) == nm.len)) {
428 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200429 ret = 1;
430 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100431 }
432
433 /* Switch to the next entry */
434 key_read(c, &dent->key, &key);
435 nm.name = (char *)dent->name;
436 dent = ubifs_tnc_next_ent(c, &key, &nm);
437 if (IS_ERR(dent)) {
438 err = PTR_ERR(dent);
439 goto out;
440 }
441
442 kfree(file->private_data);
443 file->f_pos = key_hash_flash(c, &dent->key);
444 file->private_data = dent;
445 cond_resched();
446 }
447
448out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200449 if (err != -ENOENT)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200450 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100451
Stefan Roeseac3244a2012-08-28 14:00:24 +0200452out_free:
Heinrich Schuchardte9892792017-11-08 22:28:47 +0100453 kfree(file->private_data);
454 free(file);
455 free(dentry);
456 free(dir);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100457
Stefan Roeseac3244a2012-08-28 14:00:24 +0200458 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100459}
460
461static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
462{
463 int ret;
464 char *next;
465 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200466 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100467 char *name = fpath;
468 unsigned long root_inum = 1;
469 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200470 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100471 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100472
473 strcpy(fpath, filename);
474
475 /* Remove all leading slashes */
476 while (*name == '/')
477 name++;
478
479 /*
480 * Handle root-direcoty ('/')
481 */
482 inum = root_inum;
483 if (!name || *name == '\0')
484 return inum;
485
486 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200487 struct inode *inode;
488 struct ubifs_inode *ui;
489
Stefan Roese2fc10f62009-03-19 15:35:05 +0100490 /* Extract the actual part from the pathname. */
491 next = strchr(name, '/');
492 if (next) {
493 /* Remove all leading slashes. */
494 while (*next == '/')
495 *(next++) = '\0';
496 }
497
498 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200499 if (!ret)
500 return 0;
501 inode = ubifs_iget(sb, inum);
502
503 if (!inode)
504 return 0;
505 ui = ubifs_inode(inode);
506
507 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200508 char buf[128];
509
510 /* We have some sort of symlink recursion, bail out */
511 if (symlink_count++ > 8) {
512 printf("Symlink recursion, aborting\n");
513 return 0;
514 }
515 memcpy(link_name, ui->data, ui->data_len);
516 link_name[ui->data_len] = '\0';
517
518 if (link_name[0] == '/') {
519 /* Absolute path, redo everything without
520 * the leading slash */
521 next = name = link_name + 1;
522 root_inum = 1;
523 continue;
524 }
525 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200526 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200527 link_name, next == NULL ? "" : next);
528 memcpy(symlinkpath, buf, sizeof(buf));
529 next = name = symlinkpath;
530 continue;
531 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100532
533 /*
534 * Check if directory with this name exists
535 */
536
537 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200538 if (!next || *next == '\0')
539 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100540
541 root_inum = inum;
542 name = next;
543 }
544
545 return 0;
546}
547
Simon Glassc1c4a8f2020-05-10 11:39:57 -0600548int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goedeb5030b22015-09-17 18:46:57 -0400549{
550 if (rbdd) {
551 debug("UBIFS cannot be used with normal block devices\n");
552 return -1;
553 }
554
555 /*
Simon Glasse76ee972016-02-29 15:25:44 -0700556 * Should never happen since blk_get_device_part_str() already checks
Hans de Goedeb5030b22015-09-17 18:46:57 -0400557 * this, but better safe then sorry.
558 */
559 if (!ubifs_is_mounted()) {
560 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
561 return -1;
562 }
563
564 return 0;
565}
566
Hans de Goedea1644772015-09-17 18:46:56 -0400567int ubifs_ls(const char *filename)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100568{
569 struct ubifs_info *c = ubifs_sb->s_fs_info;
570 struct file *file;
571 struct dentry *dentry;
572 struct inode *dir;
573 void *dirent = NULL;
574 unsigned long inum;
575 int ret = 0;
576
577 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedea1644772015-09-17 18:46:56 -0400578 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100579 if (!inum) {
580 ret = -1;
581 goto out;
582 }
583
584 file = kzalloc(sizeof(struct file), 0);
585 dentry = kzalloc(sizeof(struct dentry), 0);
586 dir = kzalloc(sizeof(struct inode), 0);
587 if (!file || !dentry || !dir) {
588 printf("%s: Error, no memory for malloc!\n", __func__);
589 ret = -ENOMEM;
590 goto out_mem;
591 }
592
593 dir->i_sb = ubifs_sb;
594 file->f_path.dentry = dentry;
595 file->f_path.dentry->d_parent = dentry;
596 file->f_path.dentry->d_inode = dir;
597 file->f_path.dentry->d_inode->i_ino = inum;
598 file->f_pos = 1;
599 file->private_data = NULL;
600 ubifs_printdir(file, dirent);
601
602out_mem:
603 if (file)
604 free(file);
605 if (dentry)
606 free(dentry);
607 if (dir)
608 free(dir);
609
610out:
611 ubi_close_volume(c->ubi);
612 return ret;
613}
614
Hans de Goedeb5030b22015-09-17 18:46:57 -0400615int ubifs_exists(const char *filename)
616{
617 struct ubifs_info *c = ubifs_sb->s_fs_info;
618 unsigned long inum;
619
620 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
621 inum = ubifs_findfile(ubifs_sb, (char *)filename);
622 ubi_close_volume(c->ubi);
623
624 return inum != 0;
625}
626
627int ubifs_size(const char *filename, loff_t *size)
628{
629 struct ubifs_info *c = ubifs_sb->s_fs_info;
630 unsigned long inum;
631 struct inode *inode;
632 int err = 0;
633
634 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
635
636 inum = ubifs_findfile(ubifs_sb, (char *)filename);
637 if (!inum) {
638 err = -1;
639 goto out;
640 }
641
642 inode = ubifs_iget(ubifs_sb, inum);
643 if (IS_ERR(inode)) {
644 printf("%s: Error reading inode %ld!\n", __func__, inum);
645 err = PTR_ERR(inode);
646 goto out;
647 }
648
649 *size = inode->i_size;
650
651 ubifs_iput(inode);
652out:
653 ubi_close_volume(c->ubi);
654 return err;
655}
656
Stefan Roese2fc10f62009-03-19 15:35:05 +0100657/*
658 * ubifsload...
659 */
660
661/* file.c */
662
663static inline void *kmap(struct page *page)
664{
665 return page->addr;
666}
667
668static int read_block(struct inode *inode, void *addr, unsigned int block,
669 struct ubifs_data_node *dn)
670{
671 struct ubifs_info *c = inode->i_sb->s_fs_info;
672 int err, len, out_len;
673 union ubifs_key key;
674 unsigned int dlen;
675
676 data_key_init(c, &key, inode->i_ino, block);
677 err = ubifs_tnc_lookup(c, &key, dn);
678 if (err) {
679 if (err == -ENOENT)
680 /* Not found, so it must be a hole */
681 memset(addr, 0, UBIFS_BLOCK_SIZE);
682 return err;
683 }
684
685 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
686
687 len = le32_to_cpu(dn->size);
688 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
689 goto dump;
690
691 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
692 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200693 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese2fc10f62009-03-19 15:35:05 +0100694 le16_to_cpu(dn->compr_type));
695 if (err || len != out_len)
696 goto dump;
697
698 /*
699 * Data length can be less than a full block, even for blocks that are
700 * not the last in the file (e.g., as a result of making a hole and
701 * appending data). Ensure that the remainder is zeroed out.
702 */
703 if (len < UBIFS_BLOCK_SIZE)
704 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
705
706 return 0;
707
708dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200709 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100710 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200711 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100712 return -EINVAL;
713}
714
Stefan Roese28ea29f2010-11-01 17:28:00 +0100715static int do_readpage(struct ubifs_info *c, struct inode *inode,
716 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100717{
718 void *addr;
719 int err = 0, i;
720 unsigned int block, beyond;
721 struct ubifs_data_node *dn;
722 loff_t i_size = inode->i_size;
723
724 dbg_gen("ino %lu, pg %lu, i_size %lld",
725 inode->i_ino, page->index, i_size);
726
727 addr = kmap(page);
728
729 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
730 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
731 if (block >= beyond) {
732 /* Reading beyond inode */
733 memset(addr, 0, PAGE_CACHE_SIZE);
734 goto out;
735 }
736
737 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200738 if (!dn)
739 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100740
741 i = 0;
742 while (1) {
743 int ret;
744
745 if (block >= beyond) {
746 /* Reading beyond inode */
747 err = -ENOENT;
748 memset(addr, 0, UBIFS_BLOCK_SIZE);
749 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100750 /*
751 * Reading last block? Make sure to not write beyond
752 * the requested size in the destination buffer.
753 */
754 if (((block + 1) == beyond) || last_block_size) {
755 void *buff;
756 int dlen;
757
758 /*
759 * We need to buffer the data locally for the
760 * last block. This is to not pad the
761 * destination area to a multiple of
762 * UBIFS_BLOCK_SIZE.
763 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200764 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roese28ea29f2010-11-01 17:28:00 +0100765 if (!buff) {
766 printf("%s: Error, malloc fails!\n",
767 __func__);
768 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100769 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100770 }
771
772 /* Read block-size into temp buffer */
773 ret = read_block(inode, buff, block, dn);
774 if (ret) {
775 err = ret;
776 if (err != -ENOENT) {
777 free(buff);
778 break;
779 }
780 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100781
Stefan Roese28ea29f2010-11-01 17:28:00 +0100782 if (last_block_size)
783 dlen = last_block_size;
Pali Rohár8d2176a2022-05-17 22:45:28 +0200784 else if (ret)
785 dlen = UBIFS_BLOCK_SIZE;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100786 else
787 dlen = le32_to_cpu(dn->size);
788
789 /* Now copy required size back to dest */
790 memcpy(addr, buff, dlen);
791
792 free(buff);
793 } else {
794 ret = read_block(inode, addr, block, dn);
795 if (ret) {
796 err = ret;
797 if (err != -ENOENT)
798 break;
799 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100800 }
801 }
802 if (++i >= UBIFS_BLOCKS_PER_PAGE)
803 break;
804 block += 1;
805 addr += UBIFS_BLOCK_SIZE;
806 }
807 if (err) {
808 if (err == -ENOENT) {
809 /* Not found, so it must be a hole */
810 dbg_gen("hole");
811 goto out_free;
812 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200813 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100814 page->index, inode->i_ino, err);
815 goto error;
816 }
817
818out_free:
819 kfree(dn);
820out:
821 return 0;
822
823error:
824 kfree(dn);
825 return err;
826}
827
Hans de Goedea1644772015-09-17 18:46:56 -0400828int ubifs_read(const char *filename, void *buf, loff_t offset,
829 loff_t size, loff_t *actread)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100830{
831 struct ubifs_info *c = ubifs_sb->s_fs_info;
832 unsigned long inum;
833 struct inode *inode;
834 struct page page;
835 int err = 0;
836 int i;
837 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100838 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100839
Hans de Goedea1644772015-09-17 18:46:56 -0400840 *actread = 0;
841
842 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadianbfdfd102016-10-23 20:45:16 -0700843 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedea1644772015-09-17 18:46:56 -0400844 PAGE_SIZE);
845 return -1;
846 }
847
Stefan Roese2fc10f62009-03-19 15:35:05 +0100848 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200849 /* ubifs_findfile will resolve symlinks, so we know that we get
850 * the real file here */
Hans de Goedea1644772015-09-17 18:46:56 -0400851 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100852 if (!inum) {
853 err = -1;
854 goto out;
855 }
856
857 /*
858 * Read file inode
859 */
860 inode = ubifs_iget(ubifs_sb, inum);
861 if (IS_ERR(inode)) {
862 printf("%s: Error reading inode %ld!\n", __func__, inum);
863 err = PTR_ERR(inode);
864 goto out;
865 }
866
Hans de Goedea1644772015-09-17 18:46:56 -0400867 if (offset > inode->i_size) {
868 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
869 offset, size);
870 err = -1;
871 goto put_inode;
872 }
873
Stefan Roese2fc10f62009-03-19 15:35:05 +0100874 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100875 * If no size was specified or if size bigger than filesize
876 * set size to filesize
877 */
Hans de Goedea1644772015-09-17 18:46:56 -0400878 if ((size == 0) || (size > (inode->i_size - offset)))
879 size = inode->i_size - offset;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100880
881 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100882
Hans de Goedea1644772015-09-17 18:46:56 -0400883 page.addr = buf;
884 page.index = offset / PAGE_SIZE;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100885 page.inode = inode;
886 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100887 /*
888 * Make sure to not read beyond the requested size
889 */
890 if (((i + 1) == count) && (size < inode->i_size))
891 last_block_size = size - (i * PAGE_SIZE);
892
893 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100894 if (err)
895 break;
896
897 page.addr += PAGE_SIZE;
898 page.index++;
899 }
900
Hans de Goedea1644772015-09-17 18:46:56 -0400901 if (err) {
Stefan Roese2fc10f62009-03-19 15:35:05 +0100902 printf("Error reading file '%s'\n", filename);
Hans de Goedea1644772015-09-17 18:46:56 -0400903 *actread = i * PAGE_SIZE;
904 } else {
905 *actread = size;
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000906 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100907
Hans de Goedea1644772015-09-17 18:46:56 -0400908put_inode:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100909 ubifs_iput(inode);
910
911out:
912 ubi_close_volume(c->ubi);
913 return err;
914}
Hans de Goedea1644772015-09-17 18:46:56 -0400915
Hans de Goedeb5030b22015-09-17 18:46:57 -0400916void ubifs_close(void)
917{
918}
919
Hans de Goedea1644772015-09-17 18:46:56 -0400920/* Compat wrappers for common/cmd_ubifs.c */
Ben Dooks68ebcf72023-06-06 09:23:28 +0100921int ubifs_load(char *filename, unsigned long addr, u32 size)
Hans de Goedea1644772015-09-17 18:46:56 -0400922{
923 loff_t actread;
924 int err;
925
Ben Dooks68ebcf72023-06-06 09:23:28 +0100926 printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr);
Hans de Goedea1644772015-09-17 18:46:56 -0400927
Siva Durga Prasad Paladugu67eccfb2017-05-30 14:29:06 +0200928 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400929 if (err == 0) {
Simon Glass4d949a22017-08-03 12:22:10 -0600930 env_set_hex("filesize", actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400931 printf("Done\n");
932 }
933
934 return err;
935}
936
937void uboot_ubifs_umount(void)
938{
939 if (ubifs_sb) {
940 printf("Unmounting UBIFS volume %s!\n",
941 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
942 ubifs_umount(ubifs_sb->s_fs_info);
943 ubifs_sb = NULL;
944 }
945}