blob: d5101d3c4594beec93e9caf8b9c15e8359182678 [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 Glassa87fc0a2015-09-02 17:24:57 -060014#include <common.h>
15#include <memalign.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010016#include "ubifs.h"
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020017#include <u-boot/zlib.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010018
Heiko Schocherf5895d12014-06-24 10:10:04 +020019#include <linux/err.h>
20#include <linux/lzo.h>
21
Stefan Roese2fc10f62009-03-19 15:35:05 +010022DECLARE_GLOBAL_DATA_PTR;
23
24/* compress.c */
25
26/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020027 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010028 * incompatible with the lzo decompressor.
29 */
30static int gzip_decompress(const unsigned char *in, size_t in_len,
31 unsigned char *out, size_t *out_len)
32{
Veli-Pekka Peltola7b7e1552012-09-05 18:05:14 +030033 return zunzip(out, *out_len, (unsigned char *)in,
34 (unsigned long *)out_len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010035}
36
37/* Fake description object for the "none" compressor */
38static struct ubifs_compressor none_compr = {
39 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherf5895d12014-06-24 10:10:04 +020040 .name = "none",
Stefan Roese2fc10f62009-03-19 15:35:05 +010041 .capi_name = "",
42 .decompress = NULL,
43};
44
45static struct ubifs_compressor lzo_compr = {
46 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherf5895d12014-06-24 10:10:04 +020047#ifndef __UBOOT__
48 .comp_mutex = &lzo_mutex,
49#endif
50 .name = "lzo",
Stefan Roese2fc10f62009-03-19 15:35:05 +010051 .capi_name = "lzo",
52 .decompress = lzo1x_decompress_safe,
53};
54
55static struct ubifs_compressor zlib_compr = {
56 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherf5895d12014-06-24 10:10:04 +020057#ifndef __UBOOT__
58 .comp_mutex = &deflate_mutex,
59 .decomp_mutex = &inflate_mutex,
60#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010061 .name = "zlib",
62 .capi_name = "deflate",
63 .decompress = gzip_decompress,
64};
65
66/* All UBIFS compressors */
67struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
68
Heiko Schocherf5895d12014-06-24 10:10:04 +020069
70#ifdef __UBOOT__
71/* from mm/util.c */
72
73/**
74 * kmemdup - duplicate region of memory
75 *
76 * @src: memory region to duplicate
77 * @len: memory region length
78 * @gfp: GFP mask to use
79 */
80void *kmemdup(const void *src, size_t len, gfp_t gfp)
81{
82 void *p;
83
84 p = kmalloc(len, gfp);
85 if (p)
86 memcpy(p, src, len);
87 return p;
88}
89
90struct crypto_comp {
91 int compressor;
92};
93
Heiko Schocher94b66de2015-10-22 06:19:21 +020094static inline struct crypto_comp
95*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherf5895d12014-06-24 10:10:04 +020096{
97 struct ubifs_compressor *comp;
98 struct crypto_comp *ptr;
99 int i = 0;
100
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200101 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200102 while (i < UBIFS_COMPR_TYPES_CNT) {
103 comp = ubifs_compressors[i];
104 if (!comp) {
105 i++;
106 continue;
107 }
108 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
109 ptr->compressor = i;
110 return ptr;
111 }
112 i++;
113 }
114 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200115 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200116 free (ptr);
117 return NULL;
118 }
119 return ptr;
120}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200121static inline int
122crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
123 const u8 *src, unsigned int slen, u8 *dst,
124 unsigned int *dlen)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200125{
126 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
127 int err;
Paul Davey7f918382018-11-05 18:09:29 +1300128 size_t tmp_len = *dlen;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129
130 if (compr->compr_type == UBIFS_COMPR_NONE) {
131 memcpy(dst, src, slen);
132 *dlen = slen;
133 return 0;
134 }
135
Paul Davey7f918382018-11-05 18:09:29 +1300136 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200137 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200138 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherf5895d12014-06-24 10:10:04 +0200139 "error %d", slen, compr->name, err);
140
Paul Davey7f918382018-11-05 18:09:29 +1300141 *dlen = tmp_len;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200142 return err;
143
144 return 0;
145}
Anton Habegger62e79dd2015-01-22 22:29:10 +0100146
147/* from shrinker.c */
148
149/* Global clean znode counter (for all mounted UBIFS instances) */
150atomic_long_t ubifs_clean_zn_cnt;
151
Heiko Schocherf5895d12014-06-24 10:10:04 +0200152#endif
153
Stefan Roese2fc10f62009-03-19 15:35:05 +0100154/**
155 * ubifs_decompress - decompress data.
156 * @in_buf: data to decompress
157 * @in_len: length of the data to decompress
158 * @out_buf: output buffer where decompressed data should
159 * @out_len: output length is returned here
160 * @compr_type: type of compression
161 *
162 * This function decompresses data from buffer @in_buf into buffer @out_buf.
163 * The length of the uncompressed data is returned in @out_len. This functions
164 * returns %0 on success or a negative error code on failure.
165 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200166int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
167 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100168{
169 int err;
170 struct ubifs_compressor *compr;
171
172 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200173 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100174 return -EINVAL;
175 }
176
177 compr = ubifs_compressors[compr_type];
178
179 if (unlikely(!compr->capi_name)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200180 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100181 return -EINVAL;
182 }
183
184 if (compr_type == UBIFS_COMPR_NONE) {
185 memcpy(out_buf, in_buf, in_len);
186 *out_len = in_len;
187 return 0;
188 }
189
Heiko Schocherf5895d12014-06-24 10:10:04 +0200190 if (compr->decomp_mutex)
191 mutex_lock(compr->decomp_mutex);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200192 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200193 (unsigned int *)out_len);
194 if (compr->decomp_mutex)
195 mutex_unlock(compr->decomp_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100196 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200197 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
198 " error %d", in_len, compr->name, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100199
200 return err;
201}
202
203/**
204 * compr_init - initialize a compressor.
205 * @compr: compressor description object
206 *
207 * This function initializes the requested compressor and returns zero in case
208 * of success or a negative error code in case of failure.
209 */
210static int __init compr_init(struct ubifs_compressor *compr)
211{
212 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500213
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200214#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese2fc10f62009-03-19 15:35:05 +0100215 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
216 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
217 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500218#endif
219
Heiko Schocherf5895d12014-06-24 10:10:04 +0200220 if (compr->capi_name) {
221 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
222 if (IS_ERR(compr->cc)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200223 dbg_gen("cannot initialize compressor %s,"
224 " error %ld", compr->name,
225 PTR_ERR(compr->cc));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200226 return PTR_ERR(compr->cc);
227 }
228 }
229
Stefan Roese2fc10f62009-03-19 15:35:05 +0100230 return 0;
231}
232
233/**
234 * ubifs_compressors_init - initialize UBIFS compressors.
235 *
236 * This function initializes the compressor which were compiled in. Returns
237 * zero in case of success and a negative error code in case of failure.
238 */
239int __init ubifs_compressors_init(void)
240{
241 int err;
242
243 err = compr_init(&lzo_compr);
244 if (err)
245 return err;
246
247 err = compr_init(&zlib_compr);
248 if (err)
249 return err;
250
Michael Lawnickb0617012009-03-19 10:06:41 +0100251 err = compr_init(&none_compr);
252 if (err)
253 return err;
254
Stefan Roese2fc10f62009-03-19 15:35:05 +0100255 return 0;
256}
257
258/*
259 * ubifsls...
260 */
261
262static int filldir(struct ubifs_info *c, const char *name, int namlen,
263 u64 ino, unsigned int d_type)
264{
265 struct inode *inode;
266 char filetime[32];
267
268 switch (d_type) {
269 case UBIFS_ITYPE_REG:
270 printf("\t");
271 break;
272 case UBIFS_ITYPE_DIR:
273 printf("<DIR>\t");
274 break;
275 case UBIFS_ITYPE_LNK:
276 printf("<LNK>\t");
277 break;
278 default:
279 printf("other\t");
280 break;
281 }
282
283 inode = ubifs_iget(c->vfs_sb, ino);
284 if (IS_ERR(inode)) {
285 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
286 __func__, ino, inode);
287 return -1;
288 }
289 ctime_r((time_t *)&inode->i_mtime, filetime);
290 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200291#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100292 ubifs_iput(inode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200293#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100294
295 printf("%s\n", name);
296
297 return 0;
298}
299
300static int ubifs_printdir(struct file *file, void *dirent)
301{
302 int err, over = 0;
303 struct qstr nm;
304 union ubifs_key key;
305 struct ubifs_dent_node *dent;
306 struct inode *dir = file->f_path.dentry->d_inode;
307 struct ubifs_info *c = dir->i_sb->s_fs_info;
308
309 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
310
311 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
312 /*
313 * The directory was seek'ed to a senseless position or there
314 * are no more entries.
315 */
316 return 0;
317
318 if (file->f_pos == 1) {
319 /* Find the first entry in TNC and save it */
320 lowest_dent_key(c, &key, dir->i_ino);
321 nm.name = NULL;
322 dent = ubifs_tnc_next_ent(c, &key, &nm);
323 if (IS_ERR(dent)) {
324 err = PTR_ERR(dent);
325 goto out;
326 }
327
328 file->f_pos = key_hash_flash(c, &dent->key);
329 file->private_data = dent;
330 }
331
332 dent = file->private_data;
333 if (!dent) {
334 /*
335 * The directory was seek'ed to and is now readdir'ed.
336 * Find the entry corresponding to @file->f_pos or the
337 * closest one.
338 */
339 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
340 nm.name = NULL;
341 dent = ubifs_tnc_next_ent(c, &key, &nm);
342 if (IS_ERR(dent)) {
343 err = PTR_ERR(dent);
344 goto out;
345 }
346 file->f_pos = key_hash_flash(c, &dent->key);
347 file->private_data = dent;
348 }
349
350 while (1) {
351 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
352 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
353 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200354#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100355 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200356#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100357
358 nm.len = le16_to_cpu(dent->nlen);
359 over = filldir(c, (char *)dent->name, nm.len,
360 le64_to_cpu(dent->inum), dent->type);
361 if (over)
362 return 0;
363
364 /* Switch to the next entry */
365 key_read(c, &dent->key, &key);
366 nm.name = (char *)dent->name;
367 dent = ubifs_tnc_next_ent(c, &key, &nm);
368 if (IS_ERR(dent)) {
369 err = PTR_ERR(dent);
370 goto out;
371 }
372
373 kfree(file->private_data);
374 file->f_pos = key_hash_flash(c, &dent->key);
375 file->private_data = dent;
376 cond_resched();
377 }
378
379out:
380 if (err != -ENOENT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200381 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100382 return err;
383 }
384
385 kfree(file->private_data);
386 file->private_data = NULL;
387 file->f_pos = 2;
388 return 0;
389}
390
391static int ubifs_finddir(struct super_block *sb, char *dirname,
392 unsigned long root_inum, unsigned long *inum)
393{
394 int err;
395 struct qstr nm;
396 union ubifs_key key;
397 struct ubifs_dent_node *dent;
398 struct ubifs_info *c;
399 struct file *file;
400 struct dentry *dentry;
401 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200402 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100403
404 file = kzalloc(sizeof(struct file), 0);
405 dentry = kzalloc(sizeof(struct dentry), 0);
406 dir = kzalloc(sizeof(struct inode), 0);
407 if (!file || !dentry || !dir) {
408 printf("%s: Error, no memory for malloc!\n", __func__);
409 err = -ENOMEM;
410 goto out;
411 }
412
413 dir->i_sb = sb;
414 file->f_path.dentry = dentry;
415 file->f_path.dentry->d_parent = dentry;
416 file->f_path.dentry->d_inode = dir;
417 file->f_path.dentry->d_inode->i_ino = root_inum;
418 c = sb->s_fs_info;
419
420 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
421
422 /* Find the first entry in TNC and save it */
423 lowest_dent_key(c, &key, dir->i_ino);
424 nm.name = NULL;
425 dent = ubifs_tnc_next_ent(c, &key, &nm);
426 if (IS_ERR(dent)) {
427 err = PTR_ERR(dent);
428 goto out;
429 }
430
431 file->f_pos = key_hash_flash(c, &dent->key);
432 file->private_data = dent;
433
434 while (1) {
435 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
436 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
437 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200438#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100439 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200440#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100441
442 nm.len = le16_to_cpu(dent->nlen);
443 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
444 (strlen(dirname) == nm.len)) {
445 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200446 ret = 1;
447 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100448 }
449
450 /* Switch to the next entry */
451 key_read(c, &dent->key, &key);
452 nm.name = (char *)dent->name;
453 dent = ubifs_tnc_next_ent(c, &key, &nm);
454 if (IS_ERR(dent)) {
455 err = PTR_ERR(dent);
456 goto out;
457 }
458
459 kfree(file->private_data);
460 file->f_pos = key_hash_flash(c, &dent->key);
461 file->private_data = dent;
462 cond_resched();
463 }
464
465out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200466 if (err != -ENOENT)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200467 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100468
Stefan Roeseac3244a2012-08-28 14:00:24 +0200469out_free:
Heinrich Schuchardte9892792017-11-08 22:28:47 +0100470 kfree(file->private_data);
471 free(file);
472 free(dentry);
473 free(dir);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100474
Stefan Roeseac3244a2012-08-28 14:00:24 +0200475 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100476}
477
478static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
479{
480 int ret;
481 char *next;
482 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200483 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100484 char *name = fpath;
485 unsigned long root_inum = 1;
486 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200487 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100488 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100489
490 strcpy(fpath, filename);
491
492 /* Remove all leading slashes */
493 while (*name == '/')
494 name++;
495
496 /*
497 * Handle root-direcoty ('/')
498 */
499 inum = root_inum;
500 if (!name || *name == '\0')
501 return inum;
502
503 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200504 struct inode *inode;
505 struct ubifs_inode *ui;
506
Stefan Roese2fc10f62009-03-19 15:35:05 +0100507 /* Extract the actual part from the pathname. */
508 next = strchr(name, '/');
509 if (next) {
510 /* Remove all leading slashes. */
511 while (*next == '/')
512 *(next++) = '\0';
513 }
514
515 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200516 if (!ret)
517 return 0;
518 inode = ubifs_iget(sb, inum);
519
520 if (!inode)
521 return 0;
522 ui = ubifs_inode(inode);
523
524 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200525 char buf[128];
526
527 /* We have some sort of symlink recursion, bail out */
528 if (symlink_count++ > 8) {
529 printf("Symlink recursion, aborting\n");
530 return 0;
531 }
532 memcpy(link_name, ui->data, ui->data_len);
533 link_name[ui->data_len] = '\0';
534
535 if (link_name[0] == '/') {
536 /* Absolute path, redo everything without
537 * the leading slash */
538 next = name = link_name + 1;
539 root_inum = 1;
540 continue;
541 }
542 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200543 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200544 link_name, next == NULL ? "" : next);
545 memcpy(symlinkpath, buf, sizeof(buf));
546 next = name = symlinkpath;
547 continue;
548 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100549
550 /*
551 * Check if directory with this name exists
552 */
553
554 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200555 if (!next || *next == '\0')
556 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100557
558 root_inum = inum;
559 name = next;
560 }
561
562 return 0;
563}
564
Simon Glasse3394752016-02-29 15:25:34 -0700565int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Hans de Goedeb5030b22015-09-17 18:46:57 -0400566{
567 if (rbdd) {
568 debug("UBIFS cannot be used with normal block devices\n");
569 return -1;
570 }
571
572 /*
Simon Glasse76ee972016-02-29 15:25:44 -0700573 * Should never happen since blk_get_device_part_str() already checks
Hans de Goedeb5030b22015-09-17 18:46:57 -0400574 * this, but better safe then sorry.
575 */
576 if (!ubifs_is_mounted()) {
577 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
578 return -1;
579 }
580
581 return 0;
582}
583
Hans de Goedea1644772015-09-17 18:46:56 -0400584int ubifs_ls(const char *filename)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100585{
586 struct ubifs_info *c = ubifs_sb->s_fs_info;
587 struct file *file;
588 struct dentry *dentry;
589 struct inode *dir;
590 void *dirent = NULL;
591 unsigned long inum;
592 int ret = 0;
593
594 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedea1644772015-09-17 18:46:56 -0400595 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100596 if (!inum) {
597 ret = -1;
598 goto out;
599 }
600
601 file = kzalloc(sizeof(struct file), 0);
602 dentry = kzalloc(sizeof(struct dentry), 0);
603 dir = kzalloc(sizeof(struct inode), 0);
604 if (!file || !dentry || !dir) {
605 printf("%s: Error, no memory for malloc!\n", __func__);
606 ret = -ENOMEM;
607 goto out_mem;
608 }
609
610 dir->i_sb = ubifs_sb;
611 file->f_path.dentry = dentry;
612 file->f_path.dentry->d_parent = dentry;
613 file->f_path.dentry->d_inode = dir;
614 file->f_path.dentry->d_inode->i_ino = inum;
615 file->f_pos = 1;
616 file->private_data = NULL;
617 ubifs_printdir(file, dirent);
618
619out_mem:
620 if (file)
621 free(file);
622 if (dentry)
623 free(dentry);
624 if (dir)
625 free(dir);
626
627out:
628 ubi_close_volume(c->ubi);
629 return ret;
630}
631
Hans de Goedeb5030b22015-09-17 18:46:57 -0400632int ubifs_exists(const char *filename)
633{
634 struct ubifs_info *c = ubifs_sb->s_fs_info;
635 unsigned long inum;
636
637 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
638 inum = ubifs_findfile(ubifs_sb, (char *)filename);
639 ubi_close_volume(c->ubi);
640
641 return inum != 0;
642}
643
644int ubifs_size(const char *filename, loff_t *size)
645{
646 struct ubifs_info *c = ubifs_sb->s_fs_info;
647 unsigned long inum;
648 struct inode *inode;
649 int err = 0;
650
651 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
652
653 inum = ubifs_findfile(ubifs_sb, (char *)filename);
654 if (!inum) {
655 err = -1;
656 goto out;
657 }
658
659 inode = ubifs_iget(ubifs_sb, inum);
660 if (IS_ERR(inode)) {
661 printf("%s: Error reading inode %ld!\n", __func__, inum);
662 err = PTR_ERR(inode);
663 goto out;
664 }
665
666 *size = inode->i_size;
667
668 ubifs_iput(inode);
669out:
670 ubi_close_volume(c->ubi);
671 return err;
672}
673
Stefan Roese2fc10f62009-03-19 15:35:05 +0100674/*
675 * ubifsload...
676 */
677
678/* file.c */
679
680static inline void *kmap(struct page *page)
681{
682 return page->addr;
683}
684
685static int read_block(struct inode *inode, void *addr, unsigned int block,
686 struct ubifs_data_node *dn)
687{
688 struct ubifs_info *c = inode->i_sb->s_fs_info;
689 int err, len, out_len;
690 union ubifs_key key;
691 unsigned int dlen;
692
693 data_key_init(c, &key, inode->i_ino, block);
694 err = ubifs_tnc_lookup(c, &key, dn);
695 if (err) {
696 if (err == -ENOENT)
697 /* Not found, so it must be a hole */
698 memset(addr, 0, UBIFS_BLOCK_SIZE);
699 return err;
700 }
701
702 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
703
704 len = le32_to_cpu(dn->size);
705 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
706 goto dump;
707
708 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
709 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200710 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese2fc10f62009-03-19 15:35:05 +0100711 le16_to_cpu(dn->compr_type));
712 if (err || len != out_len)
713 goto dump;
714
715 /*
716 * Data length can be less than a full block, even for blocks that are
717 * not the last in the file (e.g., as a result of making a hole and
718 * appending data). Ensure that the remainder is zeroed out.
719 */
720 if (len < UBIFS_BLOCK_SIZE)
721 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
722
723 return 0;
724
725dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200726 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100727 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200728 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100729 return -EINVAL;
730}
731
Stefan Roese28ea29f2010-11-01 17:28:00 +0100732static int do_readpage(struct ubifs_info *c, struct inode *inode,
733 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100734{
735 void *addr;
736 int err = 0, i;
737 unsigned int block, beyond;
738 struct ubifs_data_node *dn;
739 loff_t i_size = inode->i_size;
740
741 dbg_gen("ino %lu, pg %lu, i_size %lld",
742 inode->i_ino, page->index, i_size);
743
744 addr = kmap(page);
745
746 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
747 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
748 if (block >= beyond) {
749 /* Reading beyond inode */
750 memset(addr, 0, PAGE_CACHE_SIZE);
751 goto out;
752 }
753
754 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200755 if (!dn)
756 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100757
758 i = 0;
759 while (1) {
760 int ret;
761
762 if (block >= beyond) {
763 /* Reading beyond inode */
764 err = -ENOENT;
765 memset(addr, 0, UBIFS_BLOCK_SIZE);
766 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100767 /*
768 * Reading last block? Make sure to not write beyond
769 * the requested size in the destination buffer.
770 */
771 if (((block + 1) == beyond) || last_block_size) {
772 void *buff;
773 int dlen;
774
775 /*
776 * We need to buffer the data locally for the
777 * last block. This is to not pad the
778 * destination area to a multiple of
779 * UBIFS_BLOCK_SIZE.
780 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200781 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roese28ea29f2010-11-01 17:28:00 +0100782 if (!buff) {
783 printf("%s: Error, malloc fails!\n",
784 __func__);
785 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100786 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100787 }
788
789 /* Read block-size into temp buffer */
790 ret = read_block(inode, buff, block, dn);
791 if (ret) {
792 err = ret;
793 if (err != -ENOENT) {
794 free(buff);
795 break;
796 }
797 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100798
Stefan Roese28ea29f2010-11-01 17:28:00 +0100799 if (last_block_size)
800 dlen = last_block_size;
801 else
802 dlen = le32_to_cpu(dn->size);
803
804 /* Now copy required size back to dest */
805 memcpy(addr, buff, dlen);
806
807 free(buff);
808 } else {
809 ret = read_block(inode, addr, block, dn);
810 if (ret) {
811 err = ret;
812 if (err != -ENOENT)
813 break;
814 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100815 }
816 }
817 if (++i >= UBIFS_BLOCKS_PER_PAGE)
818 break;
819 block += 1;
820 addr += UBIFS_BLOCK_SIZE;
821 }
822 if (err) {
823 if (err == -ENOENT) {
824 /* Not found, so it must be a hole */
825 dbg_gen("hole");
826 goto out_free;
827 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200828 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100829 page->index, inode->i_ino, err);
830 goto error;
831 }
832
833out_free:
834 kfree(dn);
835out:
836 return 0;
837
838error:
839 kfree(dn);
840 return err;
841}
842
Hans de Goedea1644772015-09-17 18:46:56 -0400843int ubifs_read(const char *filename, void *buf, loff_t offset,
844 loff_t size, loff_t *actread)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100845{
846 struct ubifs_info *c = ubifs_sb->s_fs_info;
847 unsigned long inum;
848 struct inode *inode;
849 struct page page;
850 int err = 0;
851 int i;
852 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100853 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100854
Hans de Goedea1644772015-09-17 18:46:56 -0400855 *actread = 0;
856
857 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadianbfdfd102016-10-23 20:45:16 -0700858 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedea1644772015-09-17 18:46:56 -0400859 PAGE_SIZE);
860 return -1;
861 }
862
Stefan Roese2fc10f62009-03-19 15:35:05 +0100863 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200864 /* ubifs_findfile will resolve symlinks, so we know that we get
865 * the real file here */
Hans de Goedea1644772015-09-17 18:46:56 -0400866 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100867 if (!inum) {
868 err = -1;
869 goto out;
870 }
871
872 /*
873 * Read file inode
874 */
875 inode = ubifs_iget(ubifs_sb, inum);
876 if (IS_ERR(inode)) {
877 printf("%s: Error reading inode %ld!\n", __func__, inum);
878 err = PTR_ERR(inode);
879 goto out;
880 }
881
Hans de Goedea1644772015-09-17 18:46:56 -0400882 if (offset > inode->i_size) {
883 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
884 offset, size);
885 err = -1;
886 goto put_inode;
887 }
888
Stefan Roese2fc10f62009-03-19 15:35:05 +0100889 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100890 * If no size was specified or if size bigger than filesize
891 * set size to filesize
892 */
Hans de Goedea1644772015-09-17 18:46:56 -0400893 if ((size == 0) || (size > (inode->i_size - offset)))
894 size = inode->i_size - offset;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100895
896 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100897
Hans de Goedea1644772015-09-17 18:46:56 -0400898 page.addr = buf;
899 page.index = offset / PAGE_SIZE;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100900 page.inode = inode;
901 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100902 /*
903 * Make sure to not read beyond the requested size
904 */
905 if (((i + 1) == count) && (size < inode->i_size))
906 last_block_size = size - (i * PAGE_SIZE);
907
908 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100909 if (err)
910 break;
911
912 page.addr += PAGE_SIZE;
913 page.index++;
914 }
915
Hans de Goedea1644772015-09-17 18:46:56 -0400916 if (err) {
Stefan Roese2fc10f62009-03-19 15:35:05 +0100917 printf("Error reading file '%s'\n", filename);
Hans de Goedea1644772015-09-17 18:46:56 -0400918 *actread = i * PAGE_SIZE;
919 } else {
920 *actread = size;
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000921 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100922
Hans de Goedea1644772015-09-17 18:46:56 -0400923put_inode:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100924 ubifs_iput(inode);
925
926out:
927 ubi_close_volume(c->ubi);
928 return err;
929}
Hans de Goedea1644772015-09-17 18:46:56 -0400930
Hans de Goedeb5030b22015-09-17 18:46:57 -0400931void ubifs_close(void)
932{
933}
934
Hans de Goedea1644772015-09-17 18:46:56 -0400935/* Compat wrappers for common/cmd_ubifs.c */
936int ubifs_load(char *filename, u32 addr, u32 size)
937{
938 loff_t actread;
939 int err;
940
941 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
942
Siva Durga Prasad Paladugu67eccfb2017-05-30 14:29:06 +0200943 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400944 if (err == 0) {
Simon Glass4d949a22017-08-03 12:22:10 -0600945 env_set_hex("filesize", actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400946 printf("Done\n");
947 }
948
949 return err;
950}
951
952void uboot_ubifs_umount(void)
953{
954 if (ubifs_sb) {
955 printf("Unmounting UBIFS volume %s!\n",
956 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
957 ubifs_umount(ubifs_sb->s_fs_info);
958 ubifs_sb = NULL;
959 }
960}