blob: 67a0e8caae709759a88c6b93657d1803e0aca5a4 [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>
Simon Glassc7694dd2019-08-01 09:46:46 -060015#include <env.h>
Simon Glass0c670fc2019-08-01 09:46:36 -060016#include <gzip.h>
Simon Glass6e295182015-09-02 17:24:57 -060017#include <memalign.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010018#include "ubifs.h"
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020019#include <u-boot/zlib.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010020
Heiko Schocherff94bc42014-06-24 10:10:04 +020021#include <linux/err.h>
22#include <linux/lzo.h>
23
Stefan Roese9eefe2a2009-03-19 15:35:05 +010024DECLARE_GLOBAL_DATA_PTR;
25
26/* compress.c */
27
28/*
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020029 * We need a wrapper for zunzip() because the parameters are
Stefan Roese9eefe2a2009-03-19 15:35:05 +010030 * incompatible with the lzo decompressor.
31 */
32static int gzip_decompress(const unsigned char *in, size_t in_len,
33 unsigned char *out, size_t *out_len)
34{
Veli-Pekka Peltola8044c132012-09-05 18:05:14 +030035 return zunzip(out, *out_len, (unsigned char *)in,
36 (unsigned long *)out_len, 0, 0);
Stefan Roese9eefe2a2009-03-19 15:35:05 +010037}
38
39/* Fake description object for the "none" compressor */
40static struct ubifs_compressor none_compr = {
41 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherff94bc42014-06-24 10:10:04 +020042 .name = "none",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010043 .capi_name = "",
44 .decompress = NULL,
45};
46
47static struct ubifs_compressor lzo_compr = {
48 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherff94bc42014-06-24 10:10:04 +020049#ifndef __UBOOT__
50 .comp_mutex = &lzo_mutex,
51#endif
52 .name = "lzo",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010053 .capi_name = "lzo",
54 .decompress = lzo1x_decompress_safe,
55};
56
57static struct ubifs_compressor zlib_compr = {
58 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherff94bc42014-06-24 10:10:04 +020059#ifndef __UBOOT__
60 .comp_mutex = &deflate_mutex,
61 .decomp_mutex = &inflate_mutex,
62#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +010063 .name = "zlib",
64 .capi_name = "deflate",
65 .decompress = gzip_decompress,
66};
67
68/* All UBIFS compressors */
69struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
70
Heiko Schocherff94bc42014-06-24 10:10:04 +020071
72#ifdef __UBOOT__
73/* from mm/util.c */
74
75/**
76 * kmemdup - duplicate region of memory
77 *
78 * @src: memory region to duplicate
79 * @len: memory region length
80 * @gfp: GFP mask to use
81 */
82void *kmemdup(const void *src, size_t len, gfp_t gfp)
83{
84 void *p;
85
86 p = kmalloc(len, gfp);
87 if (p)
88 memcpy(p, src, len);
89 return p;
90}
91
92struct crypto_comp {
93 int compressor;
94};
95
Heiko Schocher0195a7b2015-10-22 06:19:21 +020096static inline struct crypto_comp
97*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherff94bc42014-06-24 10:10:04 +020098{
99 struct ubifs_compressor *comp;
100 struct crypto_comp *ptr;
101 int i = 0;
102
Marcel Ziswiler45196682015-08-18 13:06:37 +0200103 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200104 while (i < UBIFS_COMPR_TYPES_CNT) {
105 comp = ubifs_compressors[i];
106 if (!comp) {
107 i++;
108 continue;
109 }
110 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
111 ptr->compressor = i;
112 return ptr;
113 }
114 i++;
115 }
116 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200117 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200118 free (ptr);
119 return NULL;
120 }
121 return ptr;
122}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200123static inline int
124crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
125 const u8 *src, unsigned int slen, u8 *dst,
126 unsigned int *dlen)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127{
128 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
129 int err;
Paul Daveye4aa10b2018-11-05 18:09:29 +1300130 size_t tmp_len = *dlen;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131
132 if (compr->compr_type == UBIFS_COMPR_NONE) {
133 memcpy(dst, src, slen);
134 *dlen = slen;
135 return 0;
136 }
137
Paul Daveye4aa10b2018-11-05 18:09:29 +1300138 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200139 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200140 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherff94bc42014-06-24 10:10:04 +0200141 "error %d", slen, compr->name, err);
142
Paul Daveye4aa10b2018-11-05 18:09:29 +1300143 *dlen = tmp_len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200144 return err;
145
146 return 0;
147}
Anton Habeggerdc288432015-01-22 22:29:10 +0100148
149/* from shrinker.c */
150
151/* Global clean znode counter (for all mounted UBIFS instances) */
152atomic_long_t ubifs_clean_zn_cnt;
153
Heiko Schocherff94bc42014-06-24 10:10:04 +0200154#endif
155
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100156/**
157 * ubifs_decompress - decompress data.
158 * @in_buf: data to decompress
159 * @in_len: length of the data to decompress
160 * @out_buf: output buffer where decompressed data should
161 * @out_len: output length is returned here
162 * @compr_type: type of compression
163 *
164 * This function decompresses data from buffer @in_buf into buffer @out_buf.
165 * The length of the uncompressed data is returned in @out_len. This functions
166 * returns %0 on success or a negative error code on failure.
167 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200168int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
169 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100170{
171 int err;
172 struct ubifs_compressor *compr;
173
174 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200175 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100176 return -EINVAL;
177 }
178
179 compr = ubifs_compressors[compr_type];
180
181 if (unlikely(!compr->capi_name)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200182 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100183 return -EINVAL;
184 }
185
186 if (compr_type == UBIFS_COMPR_NONE) {
187 memcpy(out_buf, in_buf, in_len);
188 *out_len = in_len;
189 return 0;
190 }
191
Heiko Schocherff94bc42014-06-24 10:10:04 +0200192 if (compr->decomp_mutex)
193 mutex_lock(compr->decomp_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200194 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200195 (unsigned int *)out_len);
196 if (compr->decomp_mutex)
197 mutex_unlock(compr->decomp_mutex);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100198 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200199 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
200 " error %d", in_len, compr->name, err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100201
202 return err;
203}
204
205/**
206 * compr_init - initialize a compressor.
207 * @compr: compressor description object
208 *
209 * This function initializes the requested compressor and returns zero in case
210 * of success or a negative error code in case of failure.
211 */
212static int __init compr_init(struct ubifs_compressor *compr)
213{
214 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser521af042009-09-21 11:20:36 -0500215
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200216#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100217 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
218 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
219 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500220#endif
221
Heiko Schocherff94bc42014-06-24 10:10:04 +0200222 if (compr->capi_name) {
223 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
224 if (IS_ERR(compr->cc)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200225 dbg_gen("cannot initialize compressor %s,"
226 " error %ld", compr->name,
227 PTR_ERR(compr->cc));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200228 return PTR_ERR(compr->cc);
229 }
230 }
231
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100232 return 0;
233}
234
235/**
236 * ubifs_compressors_init - initialize UBIFS compressors.
237 *
238 * This function initializes the compressor which were compiled in. Returns
239 * zero in case of success and a negative error code in case of failure.
240 */
241int __init ubifs_compressors_init(void)
242{
243 int err;
244
245 err = compr_init(&lzo_compr);
246 if (err)
247 return err;
248
249 err = compr_init(&zlib_compr);
250 if (err)
251 return err;
252
Michael Lawnickfaac4fd2009-03-19 10:06:41 +0100253 err = compr_init(&none_compr);
254 if (err)
255 return err;
256
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100257 return 0;
258}
259
260/*
261 * ubifsls...
262 */
263
264static int filldir(struct ubifs_info *c, const char *name, int namlen,
265 u64 ino, unsigned int d_type)
266{
267 struct inode *inode;
268 char filetime[32];
269
270 switch (d_type) {
271 case UBIFS_ITYPE_REG:
272 printf("\t");
273 break;
274 case UBIFS_ITYPE_DIR:
275 printf("<DIR>\t");
276 break;
277 case UBIFS_ITYPE_LNK:
278 printf("<LNK>\t");
279 break;
280 default:
281 printf("other\t");
282 break;
283 }
284
285 inode = ubifs_iget(c->vfs_sb, ino);
286 if (IS_ERR(inode)) {
287 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
288 __func__, ino, inode);
289 return -1;
290 }
291 ctime_r((time_t *)&inode->i_mtime, filetime);
292 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200293#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100294 ubifs_iput(inode);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200295#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100296
297 printf("%s\n", name);
298
299 return 0;
300}
301
302static int ubifs_printdir(struct file *file, void *dirent)
303{
304 int err, over = 0;
305 struct qstr nm;
306 union ubifs_key key;
307 struct ubifs_dent_node *dent;
308 struct inode *dir = file->f_path.dentry->d_inode;
309 struct ubifs_info *c = dir->i_sb->s_fs_info;
310
311 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
312
313 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
314 /*
315 * The directory was seek'ed to a senseless position or there
316 * are no more entries.
317 */
318 return 0;
319
320 if (file->f_pos == 1) {
321 /* Find the first entry in TNC and save it */
322 lowest_dent_key(c, &key, dir->i_ino);
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
330 file->f_pos = key_hash_flash(c, &dent->key);
331 file->private_data = dent;
332 }
333
334 dent = file->private_data;
335 if (!dent) {
336 /*
337 * The directory was seek'ed to and is now readdir'ed.
338 * Find the entry corresponding to @file->f_pos or the
339 * closest one.
340 */
341 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
342 nm.name = NULL;
343 dent = ubifs_tnc_next_ent(c, &key, &nm);
344 if (IS_ERR(dent)) {
345 err = PTR_ERR(dent);
346 goto out;
347 }
348 file->f_pos = key_hash_flash(c, &dent->key);
349 file->private_data = dent;
350 }
351
352 while (1) {
353 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
354 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
355 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200356#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100357 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200358#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100359
360 nm.len = le16_to_cpu(dent->nlen);
361 over = filldir(c, (char *)dent->name, nm.len,
362 le64_to_cpu(dent->inum), dent->type);
363 if (over)
364 return 0;
365
366 /* Switch to the next entry */
367 key_read(c, &dent->key, &key);
368 nm.name = (char *)dent->name;
369 dent = ubifs_tnc_next_ent(c, &key, &nm);
370 if (IS_ERR(dent)) {
371 err = PTR_ERR(dent);
372 goto out;
373 }
374
375 kfree(file->private_data);
376 file->f_pos = key_hash_flash(c, &dent->key);
377 file->private_data = dent;
378 cond_resched();
379 }
380
381out:
382 if (err != -ENOENT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200383 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100384 return err;
385 }
386
387 kfree(file->private_data);
388 file->private_data = NULL;
389 file->f_pos = 2;
390 return 0;
391}
392
393static int ubifs_finddir(struct super_block *sb, char *dirname,
394 unsigned long root_inum, unsigned long *inum)
395{
396 int err;
397 struct qstr nm;
398 union ubifs_key key;
399 struct ubifs_dent_node *dent;
400 struct ubifs_info *c;
401 struct file *file;
402 struct dentry *dentry;
403 struct inode *dir;
Stefan Roesebe739132012-08-28 14:00:24 +0200404 int ret = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100405
406 file = kzalloc(sizeof(struct file), 0);
407 dentry = kzalloc(sizeof(struct dentry), 0);
408 dir = kzalloc(sizeof(struct inode), 0);
409 if (!file || !dentry || !dir) {
410 printf("%s: Error, no memory for malloc!\n", __func__);
411 err = -ENOMEM;
412 goto out;
413 }
414
415 dir->i_sb = sb;
416 file->f_path.dentry = dentry;
417 file->f_path.dentry->d_parent = dentry;
418 file->f_path.dentry->d_inode = dir;
419 file->f_path.dentry->d_inode->i_ino = root_inum;
420 c = sb->s_fs_info;
421
422 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
423
424 /* Find the first entry in TNC and save it */
425 lowest_dent_key(c, &key, dir->i_ino);
426 nm.name = NULL;
427 dent = ubifs_tnc_next_ent(c, &key, &nm);
428 if (IS_ERR(dent)) {
429 err = PTR_ERR(dent);
430 goto out;
431 }
432
433 file->f_pos = key_hash_flash(c, &dent->key);
434 file->private_data = dent;
435
436 while (1) {
437 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
438 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
439 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200440#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100441 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200442#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100443
444 nm.len = le16_to_cpu(dent->nlen);
445 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
446 (strlen(dirname) == nm.len)) {
447 *inum = le64_to_cpu(dent->inum);
Stefan Roesebe739132012-08-28 14:00:24 +0200448 ret = 1;
449 goto out_free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100450 }
451
452 /* Switch to the next entry */
453 key_read(c, &dent->key, &key);
454 nm.name = (char *)dent->name;
455 dent = ubifs_tnc_next_ent(c, &key, &nm);
456 if (IS_ERR(dent)) {
457 err = PTR_ERR(dent);
458 goto out;
459 }
460
461 kfree(file->private_data);
462 file->f_pos = key_hash_flash(c, &dent->key);
463 file->private_data = dent;
464 cond_resched();
465 }
466
467out:
Stefan Roesebe739132012-08-28 14:00:24 +0200468 if (err != -ENOENT)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200469 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100470
Stefan Roesebe739132012-08-28 14:00:24 +0200471out_free:
Heinrich Schuchardt4b299752017-11-08 22:28:47 +0100472 kfree(file->private_data);
473 free(file);
474 free(dentry);
475 free(dir);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100476
Stefan Roesebe739132012-08-28 14:00:24 +0200477 return ret;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100478}
479
480static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
481{
482 int ret;
483 char *next;
484 char fpath[128];
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200485 char symlinkpath[128];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100486 char *name = fpath;
487 unsigned long root_inum = 1;
488 unsigned long inum;
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200489 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado64b68172010-12-02 15:02:35 +0100490 char link_name[64];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100491
492 strcpy(fpath, filename);
493
494 /* Remove all leading slashes */
495 while (*name == '/')
496 name++;
497
498 /*
499 * Handle root-direcoty ('/')
500 */
501 inum = root_inum;
502 if (!name || *name == '\0')
503 return inum;
504
505 for (;;) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200506 struct inode *inode;
507 struct ubifs_inode *ui;
508
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100509 /* Extract the actual part from the pathname. */
510 next = strchr(name, '/');
511 if (next) {
512 /* Remove all leading slashes. */
513 while (*next == '/')
514 *(next++) = '\0';
515 }
516
517 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200518 if (!ret)
519 return 0;
520 inode = ubifs_iget(sb, inum);
521
522 if (!inode)
523 return 0;
524 ui = ubifs_inode(inode);
525
526 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200527 char buf[128];
528
529 /* We have some sort of symlink recursion, bail out */
530 if (symlink_count++ > 8) {
531 printf("Symlink recursion, aborting\n");
532 return 0;
533 }
534 memcpy(link_name, ui->data, ui->data_len);
535 link_name[ui->data_len] = '\0';
536
537 if (link_name[0] == '/') {
538 /* Absolute path, redo everything without
539 * the leading slash */
540 next = name = link_name + 1;
541 root_inum = 1;
542 continue;
543 }
544 /* Relative to cur dir */
Simon Kagstromef37c682009-09-25 14:05:57 +0200545 sprintf(buf, "%s/%s",
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200546 link_name, next == NULL ? "" : next);
547 memcpy(symlinkpath, buf, sizeof(buf));
548 next = name = symlinkpath;
549 continue;
550 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100551
552 /*
553 * Check if directory with this name exists
554 */
555
556 /* Found the node! */
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200557 if (!next || *next == '\0')
558 return inum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100559
560 root_inum = inum;
561 name = next;
562 }
563
564 return 0;
565}
566
Simon Glass4101f682016-02-29 15:25:34 -0700567int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400568{
569 if (rbdd) {
570 debug("UBIFS cannot be used with normal block devices\n");
571 return -1;
572 }
573
574 /*
Simon Glasse35929e2016-02-29 15:25:44 -0700575 * Should never happen since blk_get_device_part_str() already checks
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400576 * this, but better safe then sorry.
577 */
578 if (!ubifs_is_mounted()) {
579 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
580 return -1;
581 }
582
583 return 0;
584}
585
Hans de Goedead157492015-09-17 18:46:56 -0400586int ubifs_ls(const char *filename)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100587{
588 struct ubifs_info *c = ubifs_sb->s_fs_info;
589 struct file *file;
590 struct dentry *dentry;
591 struct inode *dir;
592 void *dirent = NULL;
593 unsigned long inum;
594 int ret = 0;
595
596 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedead157492015-09-17 18:46:56 -0400597 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100598 if (!inum) {
599 ret = -1;
600 goto out;
601 }
602
603 file = kzalloc(sizeof(struct file), 0);
604 dentry = kzalloc(sizeof(struct dentry), 0);
605 dir = kzalloc(sizeof(struct inode), 0);
606 if (!file || !dentry || !dir) {
607 printf("%s: Error, no memory for malloc!\n", __func__);
608 ret = -ENOMEM;
609 goto out_mem;
610 }
611
612 dir->i_sb = ubifs_sb;
613 file->f_path.dentry = dentry;
614 file->f_path.dentry->d_parent = dentry;
615 file->f_path.dentry->d_inode = dir;
616 file->f_path.dentry->d_inode->i_ino = inum;
617 file->f_pos = 1;
618 file->private_data = NULL;
619 ubifs_printdir(file, dirent);
620
621out_mem:
622 if (file)
623 free(file);
624 if (dentry)
625 free(dentry);
626 if (dir)
627 free(dir);
628
629out:
630 ubi_close_volume(c->ubi);
631 return ret;
632}
633
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400634int ubifs_exists(const char *filename)
635{
636 struct ubifs_info *c = ubifs_sb->s_fs_info;
637 unsigned long inum;
638
639 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
640 inum = ubifs_findfile(ubifs_sb, (char *)filename);
641 ubi_close_volume(c->ubi);
642
643 return inum != 0;
644}
645
646int ubifs_size(const char *filename, loff_t *size)
647{
648 struct ubifs_info *c = ubifs_sb->s_fs_info;
649 unsigned long inum;
650 struct inode *inode;
651 int err = 0;
652
653 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
654
655 inum = ubifs_findfile(ubifs_sb, (char *)filename);
656 if (!inum) {
657 err = -1;
658 goto out;
659 }
660
661 inode = ubifs_iget(ubifs_sb, inum);
662 if (IS_ERR(inode)) {
663 printf("%s: Error reading inode %ld!\n", __func__, inum);
664 err = PTR_ERR(inode);
665 goto out;
666 }
667
668 *size = inode->i_size;
669
670 ubifs_iput(inode);
671out:
672 ubi_close_volume(c->ubi);
673 return err;
674}
675
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100676/*
677 * ubifsload...
678 */
679
680/* file.c */
681
682static inline void *kmap(struct page *page)
683{
684 return page->addr;
685}
686
687static int read_block(struct inode *inode, void *addr, unsigned int block,
688 struct ubifs_data_node *dn)
689{
690 struct ubifs_info *c = inode->i_sb->s_fs_info;
691 int err, len, out_len;
692 union ubifs_key key;
693 unsigned int dlen;
694
695 data_key_init(c, &key, inode->i_ino, block);
696 err = ubifs_tnc_lookup(c, &key, dn);
697 if (err) {
698 if (err == -ENOENT)
699 /* Not found, so it must be a hole */
700 memset(addr, 0, UBIFS_BLOCK_SIZE);
701 return err;
702 }
703
704 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
705
706 len = le32_to_cpu(dn->size);
707 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
708 goto dump;
709
710 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
711 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200712 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100713 le16_to_cpu(dn->compr_type));
714 if (err || len != out_len)
715 goto dump;
716
717 /*
718 * Data length can be less than a full block, even for blocks that are
719 * not the last in the file (e.g., as a result of making a hole and
720 * appending data). Ensure that the remainder is zeroed out.
721 */
722 if (len < UBIFS_BLOCK_SIZE)
723 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
724
725 return 0;
726
727dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200728 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100729 block, inode->i_ino);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200730 ubifs_dump_node(c, dn);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100731 return -EINVAL;
732}
733
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100734static int do_readpage(struct ubifs_info *c, struct inode *inode,
735 struct page *page, int last_block_size)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100736{
737 void *addr;
738 int err = 0, i;
739 unsigned int block, beyond;
740 struct ubifs_data_node *dn;
741 loff_t i_size = inode->i_size;
742
743 dbg_gen("ino %lu, pg %lu, i_size %lld",
744 inode->i_ino, page->index, i_size);
745
746 addr = kmap(page);
747
748 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
749 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
750 if (block >= beyond) {
751 /* Reading beyond inode */
752 memset(addr, 0, PAGE_CACHE_SIZE);
753 goto out;
754 }
755
756 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mack165f9852009-06-04 19:44:12 +0200757 if (!dn)
758 return -ENOMEM;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100759
760 i = 0;
761 while (1) {
762 int ret;
763
764 if (block >= beyond) {
765 /* Reading beyond inode */
766 err = -ENOENT;
767 memset(addr, 0, UBIFS_BLOCK_SIZE);
768 } else {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100769 /*
770 * Reading last block? Make sure to not write beyond
771 * the requested size in the destination buffer.
772 */
773 if (((block + 1) == beyond) || last_block_size) {
774 void *buff;
775 int dlen;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100776
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100777 /*
778 * We need to buffer the data locally for the
779 * last block. This is to not pad the
780 * destination area to a multiple of
781 * UBIFS_BLOCK_SIZE.
782 */
Marcel Ziswiler45196682015-08-18 13:06:37 +0200783 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100784 if (!buff) {
785 printf("%s: Error, malloc fails!\n",
786 __func__);
787 err = -ENOMEM;
788 break;
789 }
790
791 /* Read block-size into temp buffer */
792 ret = read_block(inode, buff, block, dn);
793 if (ret) {
794 err = ret;
795 if (err != -ENOENT) {
796 free(buff);
797 break;
798 }
799 }
800
801 if (last_block_size)
802 dlen = last_block_size;
803 else
804 dlen = le32_to_cpu(dn->size);
805
806 /* Now copy required size back to dest */
807 memcpy(addr, buff, dlen);
808
809 free(buff);
810 } else {
811 ret = read_block(inode, addr, block, dn);
812 if (ret) {
813 err = ret;
814 if (err != -ENOENT)
815 break;
816 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100817 }
818 }
819 if (++i >= UBIFS_BLOCKS_PER_PAGE)
820 break;
821 block += 1;
822 addr += UBIFS_BLOCK_SIZE;
823 }
824 if (err) {
825 if (err == -ENOENT) {
826 /* Not found, so it must be a hole */
827 dbg_gen("hole");
828 goto out_free;
829 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200830 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100831 page->index, inode->i_ino, err);
832 goto error;
833 }
834
835out_free:
836 kfree(dn);
837out:
838 return 0;
839
840error:
841 kfree(dn);
842 return err;
843}
844
Hans de Goedead157492015-09-17 18:46:56 -0400845int ubifs_read(const char *filename, void *buf, loff_t offset,
846 loff_t size, loff_t *actread)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100847{
848 struct ubifs_info *c = ubifs_sb->s_fs_info;
849 unsigned long inum;
850 struct inode *inode;
851 struct page page;
852 int err = 0;
853 int i;
854 int count;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100855 int last_block_size = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100856
Hans de Goedead157492015-09-17 18:46:56 -0400857 *actread = 0;
858
859 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadian13819012016-10-23 20:45:16 -0700860 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedead157492015-09-17 18:46:56 -0400861 PAGE_SIZE);
862 return -1;
863 }
864
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100865 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200866 /* ubifs_findfile will resolve symlinks, so we know that we get
867 * the real file here */
Hans de Goedead157492015-09-17 18:46:56 -0400868 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100869 if (!inum) {
870 err = -1;
871 goto out;
872 }
873
874 /*
875 * Read file inode
876 */
877 inode = ubifs_iget(ubifs_sb, inum);
878 if (IS_ERR(inode)) {
879 printf("%s: Error reading inode %ld!\n", __func__, inum);
880 err = PTR_ERR(inode);
881 goto out;
882 }
883
Hans de Goedead157492015-09-17 18:46:56 -0400884 if (offset > inode->i_size) {
885 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
886 offset, size);
887 err = -1;
888 goto put_inode;
889 }
890
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100891 /*
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100892 * If no size was specified or if size bigger than filesize
893 * set size to filesize
894 */
Hans de Goedead157492015-09-17 18:46:56 -0400895 if ((size == 0) || (size > (inode->i_size - offset)))
896 size = inode->i_size - offset;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100897
898 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100899
Hans de Goedead157492015-09-17 18:46:56 -0400900 page.addr = buf;
901 page.index = offset / PAGE_SIZE;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100902 page.inode = inode;
903 for (i = 0; i < count; i++) {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100904 /*
905 * Make sure to not read beyond the requested size
906 */
907 if (((i + 1) == count) && (size < inode->i_size))
908 last_block_size = size - (i * PAGE_SIZE);
909
910 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100911 if (err)
912 break;
913
914 page.addr += PAGE_SIZE;
915 page.index++;
916 }
917
Hans de Goedead157492015-09-17 18:46:56 -0400918 if (err) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100919 printf("Error reading file '%s'\n", filename);
Hans de Goedead157492015-09-17 18:46:56 -0400920 *actread = i * PAGE_SIZE;
921 } else {
922 *actread = size;
Bastian Ruppert46d72742011-09-05 03:03:57 +0000923 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100924
Hans de Goedead157492015-09-17 18:46:56 -0400925put_inode:
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100926 ubifs_iput(inode);
927
928out:
929 ubi_close_volume(c->ubi);
930 return err;
931}
Hans de Goedead157492015-09-17 18:46:56 -0400932
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400933void ubifs_close(void)
934{
935}
936
Hans de Goedead157492015-09-17 18:46:56 -0400937/* Compat wrappers for common/cmd_ubifs.c */
938int ubifs_load(char *filename, u32 addr, u32 size)
939{
940 loff_t actread;
941 int err;
942
943 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
944
Siva Durga Prasad Paladugu34cc30a2017-05-30 14:29:06 +0200945 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedead157492015-09-17 18:46:56 -0400946 if (err == 0) {
Simon Glass018f5302017-08-03 12:22:10 -0600947 env_set_hex("filesize", actread);
Hans de Goedead157492015-09-17 18:46:56 -0400948 printf("Done\n");
949 }
950
951 return err;
952}
953
954void uboot_ubifs_umount(void)
955{
956 if (ubifs_sb) {
957 printf("Unmounting UBIFS volume %s!\n",
958 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
959 ubifs_umount(ubifs_sb->s_fs_info);
960 ubifs_sb = NULL;
961 }
962}