blob: 70beb848e1b0bb16d86d0a388719ad4b1c17bbdf [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 Glassf7ae49f2020-05-10 11:40:05 -060017#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <malloc.h>
Simon Glass6e295182015-09-02 17:24:57 -060019#include <memalign.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010020#include "ubifs.h"
Simon Glasse6f6f9e2020-05-10 11:39:58 -060021#include <part.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <dm/devres.h>
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020023#include <u-boot/zlib.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010024
AKASHI Takahiro4839e862019-11-13 09:44:47 +090025#include <linux/compat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020026#include <linux/err.h>
27#include <linux/lzo.h>
28
Stefan Roese9eefe2a2009-03-19 15:35:05 +010029DECLARE_GLOBAL_DATA_PTR;
30
31/* compress.c */
32
33/*
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020034 * We need a wrapper for zunzip() because the parameters are
Stefan Roese9eefe2a2009-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 Peltola8044c132012-09-05 18:05:14 +030040 return zunzip(out, *out_len, (unsigned char *)in,
41 (unsigned long *)out_len, 0, 0);
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020047 .name = "none",
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020054#ifndef __UBOOT__
55 .comp_mutex = &lzo_mutex,
56#endif
57 .name = "lzo",
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020064#ifndef __UBOOT__
65 .comp_mutex = &deflate_mutex,
66 .decomp_mutex = &inflate_mutex,
67#endif
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +020076
77#ifdef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +020078
79struct crypto_comp {
80 int compressor;
81};
82
Heiko Schocher0195a7b2015-10-22 06:19:21 +020083static inline struct crypto_comp
84*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherff94bc42014-06-24 10:10:04 +020085{
86 struct ubifs_compressor *comp;
87 struct crypto_comp *ptr;
88 int i = 0;
89
Marcel Ziswiler45196682015-08-18 13:06:37 +020090 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherff94bc42014-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 Schocher0195a7b2015-10-22 06:19:21 +0200104 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200105 free (ptr);
106 return NULL;
107 }
108 return ptr;
109}
Heiko Schocher0195a7b2015-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 Schocherff94bc42014-06-24 10:10:04 +0200114{
115 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
116 int err;
Paul Daveye4aa10b2018-11-05 18:09:29 +1300117 size_t tmp_len = *dlen;
Heiko Schocherff94bc42014-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 Daveye4aa10b2018-11-05 18:09:29 +1300125 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200126 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200127 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherff94bc42014-06-24 10:10:04 +0200128 "error %d", slen, compr->name, err);
129
Paul Daveye4aa10b2018-11-05 18:09:29 +1300130 *dlen = tmp_len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131 return err;
132
133 return 0;
134}
Anton Habeggerdc288432015-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 Schocherff94bc42014-06-24 10:10:04 +0200141#endif
142
Stefan Roese9eefe2a2009-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 Schocher0195a7b2015-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 Roese9eefe2a2009-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 Schocher0195a7b2015-10-22 06:19:21 +0200162 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100163 return -EINVAL;
164 }
165
166 compr = ubifs_compressors[compr_type];
167
168 if (unlikely(!compr->capi_name)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200169 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese9eefe2a2009-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 Schocherff94bc42014-06-24 10:10:04 +0200179 if (compr->decomp_mutex)
180 mutex_lock(compr->decomp_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200181 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200182 (unsigned int *)out_len);
183 if (compr->decomp_mutex)
184 mutex_unlock(compr->decomp_mutex);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100185 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200186 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
187 " error %d", in_len, compr->name, err);
Stefan Roese9eefe2a2009-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 Tyser521af042009-09-21 11:20:36 -0500202
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200203#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100204 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
205 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
206 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500207#endif
208
Heiko Schocherff94bc42014-06-24 10:10:04 +0200209 if (compr->capi_name) {
210 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
211 if (IS_ERR(compr->cc)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200212 dbg_gen("cannot initialize compressor %s,"
213 " error %ld", compr->name,
214 PTR_ERR(compr->cc));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200215 return PTR_ERR(compr->cc);
216 }
217 }
218
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100219 return 0;
220}
221
222/**
223 * ubifs_compressors_init - initialize UBIFS compressors.
224 *
225 * This function initializes the compressor which were compiled in. Returns
226 * zero in case of success and a negative error code in case of failure.
227 */
228int __init ubifs_compressors_init(void)
229{
230 int err;
231
232 err = compr_init(&lzo_compr);
233 if (err)
234 return err;
235
236 err = compr_init(&zlib_compr);
237 if (err)
238 return err;
239
Michael Lawnickfaac4fd2009-03-19 10:06:41 +0100240 err = compr_init(&none_compr);
241 if (err)
242 return err;
243
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100244 return 0;
245}
246
247/*
248 * ubifsls...
249 */
250
251static int filldir(struct ubifs_info *c, const char *name, int namlen,
252 u64 ino, unsigned int d_type)
253{
254 struct inode *inode;
255 char filetime[32];
256
257 switch (d_type) {
258 case UBIFS_ITYPE_REG:
259 printf("\t");
260 break;
261 case UBIFS_ITYPE_DIR:
262 printf("<DIR>\t");
263 break;
264 case UBIFS_ITYPE_LNK:
265 printf("<LNK>\t");
266 break;
267 default:
268 printf("other\t");
269 break;
270 }
271
272 inode = ubifs_iget(c->vfs_sb, ino);
273 if (IS_ERR(inode)) {
274 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
275 __func__, ino, inode);
276 return -1;
277 }
278 ctime_r((time_t *)&inode->i_mtime, filetime);
279 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200280#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100281 ubifs_iput(inode);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200282#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100283
284 printf("%s\n", name);
285
286 return 0;
287}
288
289static int ubifs_printdir(struct file *file, void *dirent)
290{
291 int err, over = 0;
292 struct qstr nm;
293 union ubifs_key key;
294 struct ubifs_dent_node *dent;
295 struct inode *dir = file->f_path.dentry->d_inode;
296 struct ubifs_info *c = dir->i_sb->s_fs_info;
297
298 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
299
300 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
301 /*
302 * The directory was seek'ed to a senseless position or there
303 * are no more entries.
304 */
305 return 0;
306
307 if (file->f_pos == 1) {
308 /* Find the first entry in TNC and save it */
309 lowest_dent_key(c, &key, dir->i_ino);
310 nm.name = NULL;
311 dent = ubifs_tnc_next_ent(c, &key, &nm);
312 if (IS_ERR(dent)) {
313 err = PTR_ERR(dent);
314 goto out;
315 }
316
317 file->f_pos = key_hash_flash(c, &dent->key);
318 file->private_data = dent;
319 }
320
321 dent = file->private_data;
322 if (!dent) {
323 /*
324 * The directory was seek'ed to and is now readdir'ed.
325 * Find the entry corresponding to @file->f_pos or the
326 * closest one.
327 */
328 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
329 nm.name = NULL;
330 dent = ubifs_tnc_next_ent(c, &key, &nm);
331 if (IS_ERR(dent)) {
332 err = PTR_ERR(dent);
333 goto out;
334 }
335 file->f_pos = key_hash_flash(c, &dent->key);
336 file->private_data = dent;
337 }
338
339 while (1) {
340 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
341 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
342 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200343#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100344 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200345#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100346
347 nm.len = le16_to_cpu(dent->nlen);
348 over = filldir(c, (char *)dent->name, nm.len,
349 le64_to_cpu(dent->inum), dent->type);
350 if (over)
351 return 0;
352
353 /* Switch to the next entry */
354 key_read(c, &dent->key, &key);
355 nm.name = (char *)dent->name;
356 dent = ubifs_tnc_next_ent(c, &key, &nm);
357 if (IS_ERR(dent)) {
358 err = PTR_ERR(dent);
359 goto out;
360 }
361
362 kfree(file->private_data);
363 file->f_pos = key_hash_flash(c, &dent->key);
364 file->private_data = dent;
365 cond_resched();
366 }
367
368out:
369 if (err != -ENOENT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200370 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100371 return err;
372 }
373
374 kfree(file->private_data);
375 file->private_data = NULL;
376 file->f_pos = 2;
377 return 0;
378}
379
380static int ubifs_finddir(struct super_block *sb, char *dirname,
381 unsigned long root_inum, unsigned long *inum)
382{
383 int err;
384 struct qstr nm;
385 union ubifs_key key;
386 struct ubifs_dent_node *dent;
387 struct ubifs_info *c;
388 struct file *file;
389 struct dentry *dentry;
390 struct inode *dir;
Stefan Roesebe739132012-08-28 14:00:24 +0200391 int ret = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100392
393 file = kzalloc(sizeof(struct file), 0);
394 dentry = kzalloc(sizeof(struct dentry), 0);
395 dir = kzalloc(sizeof(struct inode), 0);
396 if (!file || !dentry || !dir) {
397 printf("%s: Error, no memory for malloc!\n", __func__);
398 err = -ENOMEM;
399 goto out;
400 }
401
402 dir->i_sb = sb;
403 file->f_path.dentry = dentry;
404 file->f_path.dentry->d_parent = dentry;
405 file->f_path.dentry->d_inode = dir;
406 file->f_path.dentry->d_inode->i_ino = root_inum;
407 c = sb->s_fs_info;
408
409 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
410
411 /* Find the first entry in TNC and save it */
412 lowest_dent_key(c, &key, dir->i_ino);
413 nm.name = NULL;
414 dent = ubifs_tnc_next_ent(c, &key, &nm);
415 if (IS_ERR(dent)) {
416 err = PTR_ERR(dent);
417 goto out;
418 }
419
420 file->f_pos = key_hash_flash(c, &dent->key);
421 file->private_data = dent;
422
423 while (1) {
424 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
425 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
426 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200427#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100428 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200429#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100430
431 nm.len = le16_to_cpu(dent->nlen);
432 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
433 (strlen(dirname) == nm.len)) {
434 *inum = le64_to_cpu(dent->inum);
Stefan Roesebe739132012-08-28 14:00:24 +0200435 ret = 1;
436 goto out_free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100437 }
438
439 /* Switch to the next entry */
440 key_read(c, &dent->key, &key);
441 nm.name = (char *)dent->name;
442 dent = ubifs_tnc_next_ent(c, &key, &nm);
443 if (IS_ERR(dent)) {
444 err = PTR_ERR(dent);
445 goto out;
446 }
447
448 kfree(file->private_data);
449 file->f_pos = key_hash_flash(c, &dent->key);
450 file->private_data = dent;
451 cond_resched();
452 }
453
454out:
Stefan Roesebe739132012-08-28 14:00:24 +0200455 if (err != -ENOENT)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200456 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100457
Stefan Roesebe739132012-08-28 14:00:24 +0200458out_free:
Heinrich Schuchardt4b299752017-11-08 22:28:47 +0100459 kfree(file->private_data);
460 free(file);
461 free(dentry);
462 free(dir);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100463
Stefan Roesebe739132012-08-28 14:00:24 +0200464 return ret;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100465}
466
467static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
468{
469 int ret;
470 char *next;
471 char fpath[128];
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200472 char symlinkpath[128];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100473 char *name = fpath;
474 unsigned long root_inum = 1;
475 unsigned long inum;
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200476 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado64b68172010-12-02 15:02:35 +0100477 char link_name[64];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100478
479 strcpy(fpath, filename);
480
481 /* Remove all leading slashes */
482 while (*name == '/')
483 name++;
484
485 /*
486 * Handle root-direcoty ('/')
487 */
488 inum = root_inum;
489 if (!name || *name == '\0')
490 return inum;
491
492 for (;;) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200493 struct inode *inode;
494 struct ubifs_inode *ui;
495
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100496 /* Extract the actual part from the pathname. */
497 next = strchr(name, '/');
498 if (next) {
499 /* Remove all leading slashes. */
500 while (*next == '/')
501 *(next++) = '\0';
502 }
503
504 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200505 if (!ret)
506 return 0;
507 inode = ubifs_iget(sb, inum);
508
509 if (!inode)
510 return 0;
511 ui = ubifs_inode(inode);
512
513 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200514 char buf[128];
515
516 /* We have some sort of symlink recursion, bail out */
517 if (symlink_count++ > 8) {
518 printf("Symlink recursion, aborting\n");
519 return 0;
520 }
521 memcpy(link_name, ui->data, ui->data_len);
522 link_name[ui->data_len] = '\0';
523
524 if (link_name[0] == '/') {
525 /* Absolute path, redo everything without
526 * the leading slash */
527 next = name = link_name + 1;
528 root_inum = 1;
529 continue;
530 }
531 /* Relative to cur dir */
Simon Kagstromef37c682009-09-25 14:05:57 +0200532 sprintf(buf, "%s/%s",
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200533 link_name, next == NULL ? "" : next);
534 memcpy(symlinkpath, buf, sizeof(buf));
535 next = name = symlinkpath;
536 continue;
537 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100538
539 /*
540 * Check if directory with this name exists
541 */
542
543 /* Found the node! */
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200544 if (!next || *next == '\0')
545 return inum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100546
547 root_inum = inum;
548 name = next;
549 }
550
551 return 0;
552}
553
Simon Glass05289792020-05-10 11:39:57 -0600554int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400555{
556 if (rbdd) {
557 debug("UBIFS cannot be used with normal block devices\n");
558 return -1;
559 }
560
561 /*
Simon Glasse35929e2016-02-29 15:25:44 -0700562 * Should never happen since blk_get_device_part_str() already checks
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400563 * this, but better safe then sorry.
564 */
565 if (!ubifs_is_mounted()) {
566 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
567 return -1;
568 }
569
570 return 0;
571}
572
Hans de Goedead157492015-09-17 18:46:56 -0400573int ubifs_ls(const char *filename)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100574{
575 struct ubifs_info *c = ubifs_sb->s_fs_info;
576 struct file *file;
577 struct dentry *dentry;
578 struct inode *dir;
579 void *dirent = NULL;
580 unsigned long inum;
581 int ret = 0;
582
583 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedead157492015-09-17 18:46:56 -0400584 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100585 if (!inum) {
586 ret = -1;
587 goto out;
588 }
589
590 file = kzalloc(sizeof(struct file), 0);
591 dentry = kzalloc(sizeof(struct dentry), 0);
592 dir = kzalloc(sizeof(struct inode), 0);
593 if (!file || !dentry || !dir) {
594 printf("%s: Error, no memory for malloc!\n", __func__);
595 ret = -ENOMEM;
596 goto out_mem;
597 }
598
599 dir->i_sb = ubifs_sb;
600 file->f_path.dentry = dentry;
601 file->f_path.dentry->d_parent = dentry;
602 file->f_path.dentry->d_inode = dir;
603 file->f_path.dentry->d_inode->i_ino = inum;
604 file->f_pos = 1;
605 file->private_data = NULL;
606 ubifs_printdir(file, dirent);
607
608out_mem:
609 if (file)
610 free(file);
611 if (dentry)
612 free(dentry);
613 if (dir)
614 free(dir);
615
616out:
617 ubi_close_volume(c->ubi);
618 return ret;
619}
620
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400621int ubifs_exists(const char *filename)
622{
623 struct ubifs_info *c = ubifs_sb->s_fs_info;
624 unsigned long inum;
625
626 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
627 inum = ubifs_findfile(ubifs_sb, (char *)filename);
628 ubi_close_volume(c->ubi);
629
630 return inum != 0;
631}
632
633int ubifs_size(const char *filename, loff_t *size)
634{
635 struct ubifs_info *c = ubifs_sb->s_fs_info;
636 unsigned long inum;
637 struct inode *inode;
638 int err = 0;
639
640 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
641
642 inum = ubifs_findfile(ubifs_sb, (char *)filename);
643 if (!inum) {
644 err = -1;
645 goto out;
646 }
647
648 inode = ubifs_iget(ubifs_sb, inum);
649 if (IS_ERR(inode)) {
650 printf("%s: Error reading inode %ld!\n", __func__, inum);
651 err = PTR_ERR(inode);
652 goto out;
653 }
654
655 *size = inode->i_size;
656
657 ubifs_iput(inode);
658out:
659 ubi_close_volume(c->ubi);
660 return err;
661}
662
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100663/*
664 * ubifsload...
665 */
666
667/* file.c */
668
669static inline void *kmap(struct page *page)
670{
671 return page->addr;
672}
673
674static int read_block(struct inode *inode, void *addr, unsigned int block,
675 struct ubifs_data_node *dn)
676{
677 struct ubifs_info *c = inode->i_sb->s_fs_info;
678 int err, len, out_len;
679 union ubifs_key key;
680 unsigned int dlen;
681
682 data_key_init(c, &key, inode->i_ino, block);
683 err = ubifs_tnc_lookup(c, &key, dn);
684 if (err) {
685 if (err == -ENOENT)
686 /* Not found, so it must be a hole */
687 memset(addr, 0, UBIFS_BLOCK_SIZE);
688 return err;
689 }
690
691 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
692
693 len = le32_to_cpu(dn->size);
694 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
695 goto dump;
696
697 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
698 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200699 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100700 le16_to_cpu(dn->compr_type));
701 if (err || len != out_len)
702 goto dump;
703
704 /*
705 * Data length can be less than a full block, even for blocks that are
706 * not the last in the file (e.g., as a result of making a hole and
707 * appending data). Ensure that the remainder is zeroed out.
708 */
709 if (len < UBIFS_BLOCK_SIZE)
710 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
711
712 return 0;
713
714dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200715 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100716 block, inode->i_ino);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200717 ubifs_dump_node(c, dn);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100718 return -EINVAL;
719}
720
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100721static int do_readpage(struct ubifs_info *c, struct inode *inode,
722 struct page *page, int last_block_size)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100723{
724 void *addr;
725 int err = 0, i;
726 unsigned int block, beyond;
727 struct ubifs_data_node *dn;
728 loff_t i_size = inode->i_size;
729
730 dbg_gen("ino %lu, pg %lu, i_size %lld",
731 inode->i_ino, page->index, i_size);
732
733 addr = kmap(page);
734
735 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
736 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
737 if (block >= beyond) {
738 /* Reading beyond inode */
739 memset(addr, 0, PAGE_CACHE_SIZE);
740 goto out;
741 }
742
743 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mack165f9852009-06-04 19:44:12 +0200744 if (!dn)
745 return -ENOMEM;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100746
747 i = 0;
748 while (1) {
749 int ret;
750
751 if (block >= beyond) {
752 /* Reading beyond inode */
753 err = -ENOENT;
754 memset(addr, 0, UBIFS_BLOCK_SIZE);
755 } else {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100756 /*
757 * Reading last block? Make sure to not write beyond
758 * the requested size in the destination buffer.
759 */
760 if (((block + 1) == beyond) || last_block_size) {
761 void *buff;
762 int dlen;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100763
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100764 /*
765 * We need to buffer the data locally for the
766 * last block. This is to not pad the
767 * destination area to a multiple of
768 * UBIFS_BLOCK_SIZE.
769 */
Marcel Ziswiler45196682015-08-18 13:06:37 +0200770 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100771 if (!buff) {
772 printf("%s: Error, malloc fails!\n",
773 __func__);
774 err = -ENOMEM;
775 break;
776 }
777
778 /* Read block-size into temp buffer */
779 ret = read_block(inode, buff, block, dn);
780 if (ret) {
781 err = ret;
782 if (err != -ENOENT) {
783 free(buff);
784 break;
785 }
786 }
787
788 if (last_block_size)
789 dlen = last_block_size;
790 else
791 dlen = le32_to_cpu(dn->size);
792
793 /* Now copy required size back to dest */
794 memcpy(addr, buff, dlen);
795
796 free(buff);
797 } else {
798 ret = read_block(inode, addr, block, dn);
799 if (ret) {
800 err = ret;
801 if (err != -ENOENT)
802 break;
803 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100804 }
805 }
806 if (++i >= UBIFS_BLOCKS_PER_PAGE)
807 break;
808 block += 1;
809 addr += UBIFS_BLOCK_SIZE;
810 }
811 if (err) {
812 if (err == -ENOENT) {
813 /* Not found, so it must be a hole */
814 dbg_gen("hole");
815 goto out_free;
816 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200817 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100818 page->index, inode->i_ino, err);
819 goto error;
820 }
821
822out_free:
823 kfree(dn);
824out:
825 return 0;
826
827error:
828 kfree(dn);
829 return err;
830}
831
Hans de Goedead157492015-09-17 18:46:56 -0400832int ubifs_read(const char *filename, void *buf, loff_t offset,
833 loff_t size, loff_t *actread)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100834{
835 struct ubifs_info *c = ubifs_sb->s_fs_info;
836 unsigned long inum;
837 struct inode *inode;
838 struct page page;
839 int err = 0;
840 int i;
841 int count;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100842 int last_block_size = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100843
Hans de Goedead157492015-09-17 18:46:56 -0400844 *actread = 0;
845
846 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadian13819012016-10-23 20:45:16 -0700847 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedead157492015-09-17 18:46:56 -0400848 PAGE_SIZE);
849 return -1;
850 }
851
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100852 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200853 /* ubifs_findfile will resolve symlinks, so we know that we get
854 * the real file here */
Hans de Goedead157492015-09-17 18:46:56 -0400855 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100856 if (!inum) {
857 err = -1;
858 goto out;
859 }
860
861 /*
862 * Read file inode
863 */
864 inode = ubifs_iget(ubifs_sb, inum);
865 if (IS_ERR(inode)) {
866 printf("%s: Error reading inode %ld!\n", __func__, inum);
867 err = PTR_ERR(inode);
868 goto out;
869 }
870
Hans de Goedead157492015-09-17 18:46:56 -0400871 if (offset > inode->i_size) {
872 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
873 offset, size);
874 err = -1;
875 goto put_inode;
876 }
877
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100878 /*
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100879 * If no size was specified or if size bigger than filesize
880 * set size to filesize
881 */
Hans de Goedead157492015-09-17 18:46:56 -0400882 if ((size == 0) || (size > (inode->i_size - offset)))
883 size = inode->i_size - offset;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100884
885 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100886
Hans de Goedead157492015-09-17 18:46:56 -0400887 page.addr = buf;
888 page.index = offset / PAGE_SIZE;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100889 page.inode = inode;
890 for (i = 0; i < count; i++) {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100891 /*
892 * Make sure to not read beyond the requested size
893 */
894 if (((i + 1) == count) && (size < inode->i_size))
895 last_block_size = size - (i * PAGE_SIZE);
896
897 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100898 if (err)
899 break;
900
901 page.addr += PAGE_SIZE;
902 page.index++;
903 }
904
Hans de Goedead157492015-09-17 18:46:56 -0400905 if (err) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100906 printf("Error reading file '%s'\n", filename);
Hans de Goedead157492015-09-17 18:46:56 -0400907 *actread = i * PAGE_SIZE;
908 } else {
909 *actread = size;
Bastian Ruppert46d72742011-09-05 03:03:57 +0000910 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100911
Hans de Goedead157492015-09-17 18:46:56 -0400912put_inode:
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100913 ubifs_iput(inode);
914
915out:
916 ubi_close_volume(c->ubi);
917 return err;
918}
Hans de Goedead157492015-09-17 18:46:56 -0400919
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400920void ubifs_close(void)
921{
922}
923
Hans de Goedead157492015-09-17 18:46:56 -0400924/* Compat wrappers for common/cmd_ubifs.c */
925int ubifs_load(char *filename, u32 addr, u32 size)
926{
927 loff_t actread;
928 int err;
929
930 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
931
Siva Durga Prasad Paladugu34cc30a2017-05-30 14:29:06 +0200932 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedead157492015-09-17 18:46:56 -0400933 if (err == 0) {
Simon Glass018f5302017-08-03 12:22:10 -0600934 env_set_hex("filesize", actread);
Hans de Goedead157492015-09-17 18:46:56 -0400935 printf("Done\n");
936 }
937
938 return err;
939}
940
941void uboot_ubifs_umount(void)
942{
943 if (ubifs_sb) {
944 printf("Unmounting UBIFS volume %s!\n",
945 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
946 ubifs_umount(ubifs_sb->s_fs_info);
947 ubifs_sb = NULL;
948 }
949}