blob: 47fa41ad1dd329fdc5ee595e0436fd192779d6c6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese9eefe2a2009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Stefan Roeseb1a14f82010-11-01 17:28:00 +01007 * (C) Copyright 2008-2010
Stefan Roese9eefe2a2009-03-19 15:35:05 +01008 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 *
Stefan Roese9eefe2a2009-03-19 15:35:05 +010010 * Authors: Artem Bityutskiy (Битюцкий Артём)
11 * Adrian Hunter
12 */
13
Simon Glass6e295182015-09-02 17:24:57 -060014#include <common.h>
15#include <memalign.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010016#include "ubifs.h"
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020017#include <u-boot/zlib.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010018
Heiko Schocherff94bc42014-06-24 10:10:04 +020019#include <linux/err.h>
20#include <linux/lzo.h>
21
Stefan Roese9eefe2a2009-03-19 15:35:05 +010022DECLARE_GLOBAL_DATA_PTR;
23
24/* compress.c */
25
26/*
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020027 * We need a wrapper for zunzip() because the parameters are
Stefan Roese9eefe2a2009-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 Peltola8044c132012-09-05 18:05:14 +030033 return zunzip(out, *out_len, (unsigned char *)in,
34 (unsigned long *)out_len, 0, 0);
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020040 .name = "none",
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020047#ifndef __UBOOT__
48 .comp_mutex = &lzo_mutex,
49#endif
50 .name = "lzo",
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020057#ifndef __UBOOT__
58 .comp_mutex = &deflate_mutex,
59 .decomp_mutex = &inflate_mutex,
60#endif
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-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 Schocher0195a7b2015-10-22 06:19:21 +020094static inline struct crypto_comp
95*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherff94bc42014-06-24 10:10:04 +020096{
97 struct ubifs_compressor *comp;
98 struct crypto_comp *ptr;
99 int i = 0;
100
Marcel Ziswiler45196682015-08-18 13:06:37 +0200101 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherff94bc42014-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 Schocher0195a7b2015-10-22 06:19:21 +0200115 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200116 free (ptr);
117 return NULL;
118 }
119 return ptr;
120}
Heiko Schocher0195a7b2015-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 Schocherff94bc42014-06-24 10:10:04 +0200125{
126 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
127 int err;
128
129 if (compr->compr_type == UBIFS_COMPR_NONE) {
130 memcpy(dst, src, slen);
131 *dlen = slen;
132 return 0;
133 }
134
135 err = compr->decompress(src, slen, dst, (size_t *)dlen);
136 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200137 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherff94bc42014-06-24 10:10:04 +0200138 "error %d", slen, compr->name, err);
139
140 return err;
141
142 return 0;
143}
Anton Habeggerdc288432015-01-22 22:29:10 +0100144
145/* from shrinker.c */
146
147/* Global clean znode counter (for all mounted UBIFS instances) */
148atomic_long_t ubifs_clean_zn_cnt;
149
Heiko Schocherff94bc42014-06-24 10:10:04 +0200150#endif
151
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100152/**
153 * ubifs_decompress - decompress data.
154 * @in_buf: data to decompress
155 * @in_len: length of the data to decompress
156 * @out_buf: output buffer where decompressed data should
157 * @out_len: output length is returned here
158 * @compr_type: type of compression
159 *
160 * This function decompresses data from buffer @in_buf into buffer @out_buf.
161 * The length of the uncompressed data is returned in @out_len. This functions
162 * returns %0 on success or a negative error code on failure.
163 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200164int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
165 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100166{
167 int err;
168 struct ubifs_compressor *compr;
169
170 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200171 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100172 return -EINVAL;
173 }
174
175 compr = ubifs_compressors[compr_type];
176
177 if (unlikely(!compr->capi_name)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200178 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100179 return -EINVAL;
180 }
181
182 if (compr_type == UBIFS_COMPR_NONE) {
183 memcpy(out_buf, in_buf, in_len);
184 *out_len = in_len;
185 return 0;
186 }
187
Heiko Schocherff94bc42014-06-24 10:10:04 +0200188 if (compr->decomp_mutex)
189 mutex_lock(compr->decomp_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200190 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200191 (unsigned int *)out_len);
192 if (compr->decomp_mutex)
193 mutex_unlock(compr->decomp_mutex);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100194 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200195 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
196 " error %d", in_len, compr->name, err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100197
198 return err;
199}
200
201/**
202 * compr_init - initialize a compressor.
203 * @compr: compressor description object
204 *
205 * This function initializes the requested compressor and returns zero in case
206 * of success or a negative error code in case of failure.
207 */
208static int __init compr_init(struct ubifs_compressor *compr)
209{
210 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser521af042009-09-21 11:20:36 -0500211
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200212#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100213 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
214 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
215 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500216#endif
217
Heiko Schocherff94bc42014-06-24 10:10:04 +0200218 if (compr->capi_name) {
219 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
220 if (IS_ERR(compr->cc)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200221 dbg_gen("cannot initialize compressor %s,"
222 " error %ld", compr->name,
223 PTR_ERR(compr->cc));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200224 return PTR_ERR(compr->cc);
225 }
226 }
227
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100228 return 0;
229}
230
231/**
232 * ubifs_compressors_init - initialize UBIFS compressors.
233 *
234 * This function initializes the compressor which were compiled in. Returns
235 * zero in case of success and a negative error code in case of failure.
236 */
237int __init ubifs_compressors_init(void)
238{
239 int err;
240
241 err = compr_init(&lzo_compr);
242 if (err)
243 return err;
244
245 err = compr_init(&zlib_compr);
246 if (err)
247 return err;
248
Michael Lawnickfaac4fd2009-03-19 10:06:41 +0100249 err = compr_init(&none_compr);
250 if (err)
251 return err;
252
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100253 return 0;
254}
255
256/*
257 * ubifsls...
258 */
259
260static int filldir(struct ubifs_info *c, const char *name, int namlen,
261 u64 ino, unsigned int d_type)
262{
263 struct inode *inode;
264 char filetime[32];
265
266 switch (d_type) {
267 case UBIFS_ITYPE_REG:
268 printf("\t");
269 break;
270 case UBIFS_ITYPE_DIR:
271 printf("<DIR>\t");
272 break;
273 case UBIFS_ITYPE_LNK:
274 printf("<LNK>\t");
275 break;
276 default:
277 printf("other\t");
278 break;
279 }
280
281 inode = ubifs_iget(c->vfs_sb, ino);
282 if (IS_ERR(inode)) {
283 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
284 __func__, ino, inode);
285 return -1;
286 }
287 ctime_r((time_t *)&inode->i_mtime, filetime);
288 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200289#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100290 ubifs_iput(inode);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200291#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100292
293 printf("%s\n", name);
294
295 return 0;
296}
297
298static int ubifs_printdir(struct file *file, void *dirent)
299{
300 int err, over = 0;
301 struct qstr nm;
302 union ubifs_key key;
303 struct ubifs_dent_node *dent;
304 struct inode *dir = file->f_path.dentry->d_inode;
305 struct ubifs_info *c = dir->i_sb->s_fs_info;
306
307 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
308
309 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
310 /*
311 * The directory was seek'ed to a senseless position or there
312 * are no more entries.
313 */
314 return 0;
315
316 if (file->f_pos == 1) {
317 /* Find the first entry in TNC and save it */
318 lowest_dent_key(c, &key, dir->i_ino);
319 nm.name = NULL;
320 dent = ubifs_tnc_next_ent(c, &key, &nm);
321 if (IS_ERR(dent)) {
322 err = PTR_ERR(dent);
323 goto out;
324 }
325
326 file->f_pos = key_hash_flash(c, &dent->key);
327 file->private_data = dent;
328 }
329
330 dent = file->private_data;
331 if (!dent) {
332 /*
333 * The directory was seek'ed to and is now readdir'ed.
334 * Find the entry corresponding to @file->f_pos or the
335 * closest one.
336 */
337 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
338 nm.name = NULL;
339 dent = ubifs_tnc_next_ent(c, &key, &nm);
340 if (IS_ERR(dent)) {
341 err = PTR_ERR(dent);
342 goto out;
343 }
344 file->f_pos = key_hash_flash(c, &dent->key);
345 file->private_data = dent;
346 }
347
348 while (1) {
349 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
350 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
351 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200352#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100353 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200354#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100355
356 nm.len = le16_to_cpu(dent->nlen);
357 over = filldir(c, (char *)dent->name, nm.len,
358 le64_to_cpu(dent->inum), dent->type);
359 if (over)
360 return 0;
361
362 /* Switch to the next entry */
363 key_read(c, &dent->key, &key);
364 nm.name = (char *)dent->name;
365 dent = ubifs_tnc_next_ent(c, &key, &nm);
366 if (IS_ERR(dent)) {
367 err = PTR_ERR(dent);
368 goto out;
369 }
370
371 kfree(file->private_data);
372 file->f_pos = key_hash_flash(c, &dent->key);
373 file->private_data = dent;
374 cond_resched();
375 }
376
377out:
378 if (err != -ENOENT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200379 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100380 return err;
381 }
382
383 kfree(file->private_data);
384 file->private_data = NULL;
385 file->f_pos = 2;
386 return 0;
387}
388
389static int ubifs_finddir(struct super_block *sb, char *dirname,
390 unsigned long root_inum, unsigned long *inum)
391{
392 int err;
393 struct qstr nm;
394 union ubifs_key key;
395 struct ubifs_dent_node *dent;
396 struct ubifs_info *c;
397 struct file *file;
398 struct dentry *dentry;
399 struct inode *dir;
Stefan Roesebe739132012-08-28 14:00:24 +0200400 int ret = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100401
402 file = kzalloc(sizeof(struct file), 0);
403 dentry = kzalloc(sizeof(struct dentry), 0);
404 dir = kzalloc(sizeof(struct inode), 0);
405 if (!file || !dentry || !dir) {
406 printf("%s: Error, no memory for malloc!\n", __func__);
407 err = -ENOMEM;
408 goto out;
409 }
410
411 dir->i_sb = sb;
412 file->f_path.dentry = dentry;
413 file->f_path.dentry->d_parent = dentry;
414 file->f_path.dentry->d_inode = dir;
415 file->f_path.dentry->d_inode->i_ino = root_inum;
416 c = sb->s_fs_info;
417
418 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
419
420 /* Find the first entry in TNC and save it */
421 lowest_dent_key(c, &key, dir->i_ino);
422 nm.name = NULL;
423 dent = ubifs_tnc_next_ent(c, &key, &nm);
424 if (IS_ERR(dent)) {
425 err = PTR_ERR(dent);
426 goto out;
427 }
428
429 file->f_pos = key_hash_flash(c, &dent->key);
430 file->private_data = dent;
431
432 while (1) {
433 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
434 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
435 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200436#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100437 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200438#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100439
440 nm.len = le16_to_cpu(dent->nlen);
441 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
442 (strlen(dirname) == nm.len)) {
443 *inum = le64_to_cpu(dent->inum);
Stefan Roesebe739132012-08-28 14:00:24 +0200444 ret = 1;
445 goto out_free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100446 }
447
448 /* Switch to the next entry */
449 key_read(c, &dent->key, &key);
450 nm.name = (char *)dent->name;
451 dent = ubifs_tnc_next_ent(c, &key, &nm);
452 if (IS_ERR(dent)) {
453 err = PTR_ERR(dent);
454 goto out;
455 }
456
457 kfree(file->private_data);
458 file->f_pos = key_hash_flash(c, &dent->key);
459 file->private_data = dent;
460 cond_resched();
461 }
462
463out:
Stefan Roesebe739132012-08-28 14:00:24 +0200464 if (err != -ENOENT)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200465 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100466
Stefan Roesebe739132012-08-28 14:00:24 +0200467out_free:
Heinrich Schuchardt4b299752017-11-08 22:28:47 +0100468 kfree(file->private_data);
469 free(file);
470 free(dentry);
471 free(dir);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100472
Stefan Roesebe739132012-08-28 14:00:24 +0200473 return ret;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100474}
475
476static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
477{
478 int ret;
479 char *next;
480 char fpath[128];
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200481 char symlinkpath[128];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100482 char *name = fpath;
483 unsigned long root_inum = 1;
484 unsigned long inum;
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200485 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado64b68172010-12-02 15:02:35 +0100486 char link_name[64];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100487
488 strcpy(fpath, filename);
489
490 /* Remove all leading slashes */
491 while (*name == '/')
492 name++;
493
494 /*
495 * Handle root-direcoty ('/')
496 */
497 inum = root_inum;
498 if (!name || *name == '\0')
499 return inum;
500
501 for (;;) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200502 struct inode *inode;
503 struct ubifs_inode *ui;
504
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100505 /* Extract the actual part from the pathname. */
506 next = strchr(name, '/');
507 if (next) {
508 /* Remove all leading slashes. */
509 while (*next == '/')
510 *(next++) = '\0';
511 }
512
513 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200514 if (!ret)
515 return 0;
516 inode = ubifs_iget(sb, inum);
517
518 if (!inode)
519 return 0;
520 ui = ubifs_inode(inode);
521
522 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200523 char buf[128];
524
525 /* We have some sort of symlink recursion, bail out */
526 if (symlink_count++ > 8) {
527 printf("Symlink recursion, aborting\n");
528 return 0;
529 }
530 memcpy(link_name, ui->data, ui->data_len);
531 link_name[ui->data_len] = '\0';
532
533 if (link_name[0] == '/') {
534 /* Absolute path, redo everything without
535 * the leading slash */
536 next = name = link_name + 1;
537 root_inum = 1;
538 continue;
539 }
540 /* Relative to cur dir */
Simon Kagstromef37c682009-09-25 14:05:57 +0200541 sprintf(buf, "%s/%s",
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200542 link_name, next == NULL ? "" : next);
543 memcpy(symlinkpath, buf, sizeof(buf));
544 next = name = symlinkpath;
545 continue;
546 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100547
548 /*
549 * Check if directory with this name exists
550 */
551
552 /* Found the node! */
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200553 if (!next || *next == '\0')
554 return inum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100555
556 root_inum = inum;
557 name = next;
558 }
559
560 return 0;
561}
562
Simon Glass4101f682016-02-29 15:25:34 -0700563int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400564{
565 if (rbdd) {
566 debug("UBIFS cannot be used with normal block devices\n");
567 return -1;
568 }
569
570 /*
Simon Glasse35929e2016-02-29 15:25:44 -0700571 * Should never happen since blk_get_device_part_str() already checks
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400572 * this, but better safe then sorry.
573 */
574 if (!ubifs_is_mounted()) {
575 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
576 return -1;
577 }
578
579 return 0;
580}
581
Hans de Goedead157492015-09-17 18:46:56 -0400582int ubifs_ls(const char *filename)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100583{
584 struct ubifs_info *c = ubifs_sb->s_fs_info;
585 struct file *file;
586 struct dentry *dentry;
587 struct inode *dir;
588 void *dirent = NULL;
589 unsigned long inum;
590 int ret = 0;
591
592 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedead157492015-09-17 18:46:56 -0400593 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100594 if (!inum) {
595 ret = -1;
596 goto out;
597 }
598
599 file = kzalloc(sizeof(struct file), 0);
600 dentry = kzalloc(sizeof(struct dentry), 0);
601 dir = kzalloc(sizeof(struct inode), 0);
602 if (!file || !dentry || !dir) {
603 printf("%s: Error, no memory for malloc!\n", __func__);
604 ret = -ENOMEM;
605 goto out_mem;
606 }
607
608 dir->i_sb = ubifs_sb;
609 file->f_path.dentry = dentry;
610 file->f_path.dentry->d_parent = dentry;
611 file->f_path.dentry->d_inode = dir;
612 file->f_path.dentry->d_inode->i_ino = inum;
613 file->f_pos = 1;
614 file->private_data = NULL;
615 ubifs_printdir(file, dirent);
616
617out_mem:
618 if (file)
619 free(file);
620 if (dentry)
621 free(dentry);
622 if (dir)
623 free(dir);
624
625out:
626 ubi_close_volume(c->ubi);
627 return ret;
628}
629
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400630int ubifs_exists(const char *filename)
631{
632 struct ubifs_info *c = ubifs_sb->s_fs_info;
633 unsigned long inum;
634
635 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
636 inum = ubifs_findfile(ubifs_sb, (char *)filename);
637 ubi_close_volume(c->ubi);
638
639 return inum != 0;
640}
641
642int ubifs_size(const char *filename, loff_t *size)
643{
644 struct ubifs_info *c = ubifs_sb->s_fs_info;
645 unsigned long inum;
646 struct inode *inode;
647 int err = 0;
648
649 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
650
651 inum = ubifs_findfile(ubifs_sb, (char *)filename);
652 if (!inum) {
653 err = -1;
654 goto out;
655 }
656
657 inode = ubifs_iget(ubifs_sb, inum);
658 if (IS_ERR(inode)) {
659 printf("%s: Error reading inode %ld!\n", __func__, inum);
660 err = PTR_ERR(inode);
661 goto out;
662 }
663
664 *size = inode->i_size;
665
666 ubifs_iput(inode);
667out:
668 ubi_close_volume(c->ubi);
669 return err;
670}
671
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100672/*
673 * ubifsload...
674 */
675
676/* file.c */
677
678static inline void *kmap(struct page *page)
679{
680 return page->addr;
681}
682
683static int read_block(struct inode *inode, void *addr, unsigned int block,
684 struct ubifs_data_node *dn)
685{
686 struct ubifs_info *c = inode->i_sb->s_fs_info;
687 int err, len, out_len;
688 union ubifs_key key;
689 unsigned int dlen;
690
691 data_key_init(c, &key, inode->i_ino, block);
692 err = ubifs_tnc_lookup(c, &key, dn);
693 if (err) {
694 if (err == -ENOENT)
695 /* Not found, so it must be a hole */
696 memset(addr, 0, UBIFS_BLOCK_SIZE);
697 return err;
698 }
699
700 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
701
702 len = le32_to_cpu(dn->size);
703 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
704 goto dump;
705
706 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
707 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200708 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100709 le16_to_cpu(dn->compr_type));
710 if (err || len != out_len)
711 goto dump;
712
713 /*
714 * Data length can be less than a full block, even for blocks that are
715 * not the last in the file (e.g., as a result of making a hole and
716 * appending data). Ensure that the remainder is zeroed out.
717 */
718 if (len < UBIFS_BLOCK_SIZE)
719 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
720
721 return 0;
722
723dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200724 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100725 block, inode->i_ino);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200726 ubifs_dump_node(c, dn);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100727 return -EINVAL;
728}
729
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100730static int do_readpage(struct ubifs_info *c, struct inode *inode,
731 struct page *page, int last_block_size)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100732{
733 void *addr;
734 int err = 0, i;
735 unsigned int block, beyond;
736 struct ubifs_data_node *dn;
737 loff_t i_size = inode->i_size;
738
739 dbg_gen("ino %lu, pg %lu, i_size %lld",
740 inode->i_ino, page->index, i_size);
741
742 addr = kmap(page);
743
744 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
745 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
746 if (block >= beyond) {
747 /* Reading beyond inode */
748 memset(addr, 0, PAGE_CACHE_SIZE);
749 goto out;
750 }
751
752 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mack165f9852009-06-04 19:44:12 +0200753 if (!dn)
754 return -ENOMEM;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100755
756 i = 0;
757 while (1) {
758 int ret;
759
760 if (block >= beyond) {
761 /* Reading beyond inode */
762 err = -ENOENT;
763 memset(addr, 0, UBIFS_BLOCK_SIZE);
764 } else {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100765 /*
766 * Reading last block? Make sure to not write beyond
767 * the requested size in the destination buffer.
768 */
769 if (((block + 1) == beyond) || last_block_size) {
770 void *buff;
771 int dlen;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100772
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100773 /*
774 * We need to buffer the data locally for the
775 * last block. This is to not pad the
776 * destination area to a multiple of
777 * UBIFS_BLOCK_SIZE.
778 */
Marcel Ziswiler45196682015-08-18 13:06:37 +0200779 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100780 if (!buff) {
781 printf("%s: Error, malloc fails!\n",
782 __func__);
783 err = -ENOMEM;
784 break;
785 }
786
787 /* Read block-size into temp buffer */
788 ret = read_block(inode, buff, block, dn);
789 if (ret) {
790 err = ret;
791 if (err != -ENOENT) {
792 free(buff);
793 break;
794 }
795 }
796
797 if (last_block_size)
798 dlen = last_block_size;
799 else
800 dlen = le32_to_cpu(dn->size);
801
802 /* Now copy required size back to dest */
803 memcpy(addr, buff, dlen);
804
805 free(buff);
806 } else {
807 ret = read_block(inode, addr, block, dn);
808 if (ret) {
809 err = ret;
810 if (err != -ENOENT)
811 break;
812 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100813 }
814 }
815 if (++i >= UBIFS_BLOCKS_PER_PAGE)
816 break;
817 block += 1;
818 addr += UBIFS_BLOCK_SIZE;
819 }
820 if (err) {
821 if (err == -ENOENT) {
822 /* Not found, so it must be a hole */
823 dbg_gen("hole");
824 goto out_free;
825 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200826 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100827 page->index, inode->i_ino, err);
828 goto error;
829 }
830
831out_free:
832 kfree(dn);
833out:
834 return 0;
835
836error:
837 kfree(dn);
838 return err;
839}
840
Hans de Goedead157492015-09-17 18:46:56 -0400841int ubifs_read(const char *filename, void *buf, loff_t offset,
842 loff_t size, loff_t *actread)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100843{
844 struct ubifs_info *c = ubifs_sb->s_fs_info;
845 unsigned long inum;
846 struct inode *inode;
847 struct page page;
848 int err = 0;
849 int i;
850 int count;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100851 int last_block_size = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100852
Hans de Goedead157492015-09-17 18:46:56 -0400853 *actread = 0;
854
855 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadian13819012016-10-23 20:45:16 -0700856 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedead157492015-09-17 18:46:56 -0400857 PAGE_SIZE);
858 return -1;
859 }
860
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100861 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200862 /* ubifs_findfile will resolve symlinks, so we know that we get
863 * the real file here */
Hans de Goedead157492015-09-17 18:46:56 -0400864 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100865 if (!inum) {
866 err = -1;
867 goto out;
868 }
869
870 /*
871 * Read file inode
872 */
873 inode = ubifs_iget(ubifs_sb, inum);
874 if (IS_ERR(inode)) {
875 printf("%s: Error reading inode %ld!\n", __func__, inum);
876 err = PTR_ERR(inode);
877 goto out;
878 }
879
Hans de Goedead157492015-09-17 18:46:56 -0400880 if (offset > inode->i_size) {
881 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
882 offset, size);
883 err = -1;
884 goto put_inode;
885 }
886
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100887 /*
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100888 * If no size was specified or if size bigger than filesize
889 * set size to filesize
890 */
Hans de Goedead157492015-09-17 18:46:56 -0400891 if ((size == 0) || (size > (inode->i_size - offset)))
892 size = inode->i_size - offset;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100893
894 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100895
Hans de Goedead157492015-09-17 18:46:56 -0400896 page.addr = buf;
897 page.index = offset / PAGE_SIZE;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100898 page.inode = inode;
899 for (i = 0; i < count; i++) {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100900 /*
901 * Make sure to not read beyond the requested size
902 */
903 if (((i + 1) == count) && (size < inode->i_size))
904 last_block_size = size - (i * PAGE_SIZE);
905
906 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100907 if (err)
908 break;
909
910 page.addr += PAGE_SIZE;
911 page.index++;
912 }
913
Hans de Goedead157492015-09-17 18:46:56 -0400914 if (err) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100915 printf("Error reading file '%s'\n", filename);
Hans de Goedead157492015-09-17 18:46:56 -0400916 *actread = i * PAGE_SIZE;
917 } else {
918 *actread = size;
Bastian Ruppert46d72742011-09-05 03:03:57 +0000919 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100920
Hans de Goedead157492015-09-17 18:46:56 -0400921put_inode:
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100922 ubifs_iput(inode);
923
924out:
925 ubi_close_volume(c->ubi);
926 return err;
927}
Hans de Goedead157492015-09-17 18:46:56 -0400928
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400929void ubifs_close(void)
930{
931}
932
Hans de Goedead157492015-09-17 18:46:56 -0400933/* Compat wrappers for common/cmd_ubifs.c */
934int ubifs_load(char *filename, u32 addr, u32 size)
935{
936 loff_t actread;
937 int err;
938
939 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
940
Siva Durga Prasad Paladugu34cc30a2017-05-30 14:29:06 +0200941 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedead157492015-09-17 18:46:56 -0400942 if (err == 0) {
Simon Glass018f5302017-08-03 12:22:10 -0600943 env_set_hex("filesize", actread);
Hans de Goedead157492015-09-17 18:46:56 -0400944 printf("Done\n");
945 }
946
947 return err;
948}
949
950void uboot_ubifs_umount(void)
951{
952 if (ubifs_sb) {
953 printf("Unmounting UBIFS volume %s!\n",
954 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
955 ubifs_umount(ubifs_sb->s_fs_info);
956 ubifs_sb = NULL;
957 }
958}