blob: e097d284444959c857996b40d1b5b6bc6a515f23 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese2fc10f62009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation.
6 *
Stefan Roese28ea29f2010-11-01 17:28:00 +01007 * (C) Copyright 2008-2010
Stefan Roese2fc10f62009-03-19 15:35:05 +01008 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 *
Stefan Roese2fc10f62009-03-19 15:35:05 +010010 * Authors: Artem Bityutskiy (Битюцкий Артём)
11 * Adrian Hunter
12 */
13
Simon Glassa87fc0a2015-09-02 17:24:57 -060014#include <common.h>
Simon Glass313112a2019-08-01 09:46:46 -060015#include <env.h>
Simon Glass1a974af2019-08-01 09:46:36 -060016#include <gzip.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glassa87fc0a2015-09-02 17:24:57 -060018#include <memalign.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010019#include "ubifs.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <dm/devres.h>
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020021#include <u-boot/zlib.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010022
AKASHI Takahiro1d8d34d2019-11-13 09:44:47 +090023#include <linux/compat.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020024#include <linux/err.h>
25#include <linux/lzo.h>
26
Stefan Roese2fc10f62009-03-19 15:35:05 +010027DECLARE_GLOBAL_DATA_PTR;
28
29/* compress.c */
30
31/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020032 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010033 * incompatible with the lzo decompressor.
34 */
35static int gzip_decompress(const unsigned char *in, size_t in_len,
36 unsigned char *out, size_t *out_len)
37{
Veli-Pekka Peltola7b7e1552012-09-05 18:05:14 +030038 return zunzip(out, *out_len, (unsigned char *)in,
39 (unsigned long *)out_len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010040}
41
42/* Fake description object for the "none" compressor */
43static struct ubifs_compressor none_compr = {
44 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherf5895d12014-06-24 10:10:04 +020045 .name = "none",
Stefan Roese2fc10f62009-03-19 15:35:05 +010046 .capi_name = "",
47 .decompress = NULL,
48};
49
50static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherf5895d12014-06-24 10:10:04 +020052#ifndef __UBOOT__
53 .comp_mutex = &lzo_mutex,
54#endif
55 .name = "lzo",
Stefan Roese2fc10f62009-03-19 15:35:05 +010056 .capi_name = "lzo",
57 .decompress = lzo1x_decompress_safe,
58};
59
60static struct ubifs_compressor zlib_compr = {
61 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherf5895d12014-06-24 10:10:04 +020062#ifndef __UBOOT__
63 .comp_mutex = &deflate_mutex,
64 .decomp_mutex = &inflate_mutex,
65#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010066 .name = "zlib",
67 .capi_name = "deflate",
68 .decompress = gzip_decompress,
69};
70
71/* All UBIFS compressors */
72struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
73
Heiko Schocherf5895d12014-06-24 10:10:04 +020074
75#ifdef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +020076
77struct crypto_comp {
78 int compressor;
79};
80
Heiko Schocher94b66de2015-10-22 06:19:21 +020081static inline struct crypto_comp
82*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherf5895d12014-06-24 10:10:04 +020083{
84 struct ubifs_compressor *comp;
85 struct crypto_comp *ptr;
86 int i = 0;
87
Marcel Ziswilerabc574b2015-08-18 13:06:37 +020088 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherf5895d12014-06-24 10:10:04 +020089 while (i < UBIFS_COMPR_TYPES_CNT) {
90 comp = ubifs_compressors[i];
91 if (!comp) {
92 i++;
93 continue;
94 }
95 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
96 ptr->compressor = i;
97 return ptr;
98 }
99 i++;
100 }
101 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200102 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200103 free (ptr);
104 return NULL;
105 }
106 return ptr;
107}
Heiko Schocher94b66de2015-10-22 06:19:21 +0200108static inline int
109crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
110 const u8 *src, unsigned int slen, u8 *dst,
111 unsigned int *dlen)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200112{
113 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
114 int err;
Paul Davey7f918382018-11-05 18:09:29 +1300115 size_t tmp_len = *dlen;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200116
117 if (compr->compr_type == UBIFS_COMPR_NONE) {
118 memcpy(dst, src, slen);
119 *dlen = slen;
120 return 0;
121 }
122
Paul Davey7f918382018-11-05 18:09:29 +1300123 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200124 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200125 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherf5895d12014-06-24 10:10:04 +0200126 "error %d", slen, compr->name, err);
127
Paul Davey7f918382018-11-05 18:09:29 +1300128 *dlen = tmp_len;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200129 return err;
130
131 return 0;
132}
Anton Habegger62e79dd2015-01-22 22:29:10 +0100133
134/* from shrinker.c */
135
136/* Global clean znode counter (for all mounted UBIFS instances) */
137atomic_long_t ubifs_clean_zn_cnt;
138
Heiko Schocherf5895d12014-06-24 10:10:04 +0200139#endif
140
Stefan Roese2fc10f62009-03-19 15:35:05 +0100141/**
142 * ubifs_decompress - decompress data.
143 * @in_buf: data to decompress
144 * @in_len: length of the data to decompress
145 * @out_buf: output buffer where decompressed data should
146 * @out_len: output length is returned here
147 * @compr_type: type of compression
148 *
149 * This function decompresses data from buffer @in_buf into buffer @out_buf.
150 * The length of the uncompressed data is returned in @out_len. This functions
151 * returns %0 on success or a negative error code on failure.
152 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200153int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
154 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100155{
156 int err;
157 struct ubifs_compressor *compr;
158
159 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200160 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100161 return -EINVAL;
162 }
163
164 compr = ubifs_compressors[compr_type];
165
166 if (unlikely(!compr->capi_name)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200167 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100168 return -EINVAL;
169 }
170
171 if (compr_type == UBIFS_COMPR_NONE) {
172 memcpy(out_buf, in_buf, in_len);
173 *out_len = in_len;
174 return 0;
175 }
176
Heiko Schocherf5895d12014-06-24 10:10:04 +0200177 if (compr->decomp_mutex)
178 mutex_lock(compr->decomp_mutex);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200179 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200180 (unsigned int *)out_len);
181 if (compr->decomp_mutex)
182 mutex_unlock(compr->decomp_mutex);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100183 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200184 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
185 " error %d", in_len, compr->name, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100186
187 return err;
188}
189
190/**
191 * compr_init - initialize a compressor.
192 * @compr: compressor description object
193 *
194 * This function initializes the requested compressor and returns zero in case
195 * of success or a negative error code in case of failure.
196 */
197static int __init compr_init(struct ubifs_compressor *compr)
198{
199 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500200
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200201#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese2fc10f62009-03-19 15:35:05 +0100202 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
203 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
204 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500205#endif
206
Heiko Schocherf5895d12014-06-24 10:10:04 +0200207 if (compr->capi_name) {
208 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
209 if (IS_ERR(compr->cc)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200210 dbg_gen("cannot initialize compressor %s,"
211 " error %ld", compr->name,
212 PTR_ERR(compr->cc));
Heiko Schocherf5895d12014-06-24 10:10:04 +0200213 return PTR_ERR(compr->cc);
214 }
215 }
216
Stefan Roese2fc10f62009-03-19 15:35:05 +0100217 return 0;
218}
219
220/**
221 * ubifs_compressors_init - initialize UBIFS compressors.
222 *
223 * This function initializes the compressor which were compiled in. Returns
224 * zero in case of success and a negative error code in case of failure.
225 */
226int __init ubifs_compressors_init(void)
227{
228 int err;
229
230 err = compr_init(&lzo_compr);
231 if (err)
232 return err;
233
234 err = compr_init(&zlib_compr);
235 if (err)
236 return err;
237
Michael Lawnickb0617012009-03-19 10:06:41 +0100238 err = compr_init(&none_compr);
239 if (err)
240 return err;
241
Stefan Roese2fc10f62009-03-19 15:35:05 +0100242 return 0;
243}
244
245/*
246 * ubifsls...
247 */
248
249static int filldir(struct ubifs_info *c, const char *name, int namlen,
250 u64 ino, unsigned int d_type)
251{
252 struct inode *inode;
253 char filetime[32];
254
255 switch (d_type) {
256 case UBIFS_ITYPE_REG:
257 printf("\t");
258 break;
259 case UBIFS_ITYPE_DIR:
260 printf("<DIR>\t");
261 break;
262 case UBIFS_ITYPE_LNK:
263 printf("<LNK>\t");
264 break;
265 default:
266 printf("other\t");
267 break;
268 }
269
270 inode = ubifs_iget(c->vfs_sb, ino);
271 if (IS_ERR(inode)) {
272 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
273 __func__, ino, inode);
274 return -1;
275 }
276 ctime_r((time_t *)&inode->i_mtime, filetime);
277 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200278#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100279 ubifs_iput(inode);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100281
282 printf("%s\n", name);
283
284 return 0;
285}
286
287static int ubifs_printdir(struct file *file, void *dirent)
288{
289 int err, over = 0;
290 struct qstr nm;
291 union ubifs_key key;
292 struct ubifs_dent_node *dent;
293 struct inode *dir = file->f_path.dentry->d_inode;
294 struct ubifs_info *c = dir->i_sb->s_fs_info;
295
296 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
297
298 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
299 /*
300 * The directory was seek'ed to a senseless position or there
301 * are no more entries.
302 */
303 return 0;
304
305 if (file->f_pos == 1) {
306 /* Find the first entry in TNC and save it */
307 lowest_dent_key(c, &key, dir->i_ino);
308 nm.name = NULL;
309 dent = ubifs_tnc_next_ent(c, &key, &nm);
310 if (IS_ERR(dent)) {
311 err = PTR_ERR(dent);
312 goto out;
313 }
314
315 file->f_pos = key_hash_flash(c, &dent->key);
316 file->private_data = dent;
317 }
318
319 dent = file->private_data;
320 if (!dent) {
321 /*
322 * The directory was seek'ed to and is now readdir'ed.
323 * Find the entry corresponding to @file->f_pos or the
324 * closest one.
325 */
326 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
327 nm.name = NULL;
328 dent = ubifs_tnc_next_ent(c, &key, &nm);
329 if (IS_ERR(dent)) {
330 err = PTR_ERR(dent);
331 goto out;
332 }
333 file->f_pos = key_hash_flash(c, &dent->key);
334 file->private_data = dent;
335 }
336
337 while (1) {
338 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
339 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
340 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200341#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100342 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200343#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100344
345 nm.len = le16_to_cpu(dent->nlen);
346 over = filldir(c, (char *)dent->name, nm.len,
347 le64_to_cpu(dent->inum), dent->type);
348 if (over)
349 return 0;
350
351 /* Switch to the next entry */
352 key_read(c, &dent->key, &key);
353 nm.name = (char *)dent->name;
354 dent = ubifs_tnc_next_ent(c, &key, &nm);
355 if (IS_ERR(dent)) {
356 err = PTR_ERR(dent);
357 goto out;
358 }
359
360 kfree(file->private_data);
361 file->f_pos = key_hash_flash(c, &dent->key);
362 file->private_data = dent;
363 cond_resched();
364 }
365
366out:
367 if (err != -ENOENT) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200368 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100369 return err;
370 }
371
372 kfree(file->private_data);
373 file->private_data = NULL;
374 file->f_pos = 2;
375 return 0;
376}
377
378static int ubifs_finddir(struct super_block *sb, char *dirname,
379 unsigned long root_inum, unsigned long *inum)
380{
381 int err;
382 struct qstr nm;
383 union ubifs_key key;
384 struct ubifs_dent_node *dent;
385 struct ubifs_info *c;
386 struct file *file;
387 struct dentry *dentry;
388 struct inode *dir;
Stefan Roeseac3244a2012-08-28 14:00:24 +0200389 int ret = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100390
391 file = kzalloc(sizeof(struct file), 0);
392 dentry = kzalloc(sizeof(struct dentry), 0);
393 dir = kzalloc(sizeof(struct inode), 0);
394 if (!file || !dentry || !dir) {
395 printf("%s: Error, no memory for malloc!\n", __func__);
396 err = -ENOMEM;
397 goto out;
398 }
399
400 dir->i_sb = sb;
401 file->f_path.dentry = dentry;
402 file->f_path.dentry->d_parent = dentry;
403 file->f_path.dentry->d_inode = dir;
404 file->f_path.dentry->d_inode->i_ino = root_inum;
405 c = sb->s_fs_info;
406
407 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
408
409 /* Find the first entry in TNC and save it */
410 lowest_dent_key(c, &key, dir->i_ino);
411 nm.name = NULL;
412 dent = ubifs_tnc_next_ent(c, &key, &nm);
413 if (IS_ERR(dent)) {
414 err = PTR_ERR(dent);
415 goto out;
416 }
417
418 file->f_pos = key_hash_flash(c, &dent->key);
419 file->private_data = dent;
420
421 while (1) {
422 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
423 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
424 key_hash_flash(c, &dent->key));
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200425#ifndef __UBOOT__
Stefan Roese2fc10f62009-03-19 15:35:05 +0100426 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotardec7c77e2018-04-27 15:51:23 +0200427#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +0100428
429 nm.len = le16_to_cpu(dent->nlen);
430 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
431 (strlen(dirname) == nm.len)) {
432 *inum = le64_to_cpu(dent->inum);
Stefan Roeseac3244a2012-08-28 14:00:24 +0200433 ret = 1;
434 goto out_free;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100435 }
436
437 /* Switch to the next entry */
438 key_read(c, &dent->key, &key);
439 nm.name = (char *)dent->name;
440 dent = ubifs_tnc_next_ent(c, &key, &nm);
441 if (IS_ERR(dent)) {
442 err = PTR_ERR(dent);
443 goto out;
444 }
445
446 kfree(file->private_data);
447 file->f_pos = key_hash_flash(c, &dent->key);
448 file->private_data = dent;
449 cond_resched();
450 }
451
452out:
Stefan Roeseac3244a2012-08-28 14:00:24 +0200453 if (err != -ENOENT)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200454 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100455
Stefan Roeseac3244a2012-08-28 14:00:24 +0200456out_free:
Heinrich Schuchardte9892792017-11-08 22:28:47 +0100457 kfree(file->private_data);
458 free(file);
459 free(dentry);
460 free(dir);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100461
Stefan Roeseac3244a2012-08-28 14:00:24 +0200462 return ret;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100463}
464
465static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
466{
467 int ret;
468 char *next;
469 char fpath[128];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200470 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100471 char *name = fpath;
472 unsigned long root_inum = 1;
473 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200474 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100475 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100476
477 strcpy(fpath, filename);
478
479 /* Remove all leading slashes */
480 while (*name == '/')
481 name++;
482
483 /*
484 * Handle root-direcoty ('/')
485 */
486 inum = root_inum;
487 if (!name || *name == '\0')
488 return inum;
489
490 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200491 struct inode *inode;
492 struct ubifs_inode *ui;
493
Stefan Roese2fc10f62009-03-19 15:35:05 +0100494 /* Extract the actual part from the pathname. */
495 next = strchr(name, '/');
496 if (next) {
497 /* Remove all leading slashes. */
498 while (*next == '/')
499 *(next++) = '\0';
500 }
501
502 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200503 if (!ret)
504 return 0;
505 inode = ubifs_iget(sb, inum);
506
507 if (!inode)
508 return 0;
509 ui = ubifs_inode(inode);
510
511 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200512 char buf[128];
513
514 /* We have some sort of symlink recursion, bail out */
515 if (symlink_count++ > 8) {
516 printf("Symlink recursion, aborting\n");
517 return 0;
518 }
519 memcpy(link_name, ui->data, ui->data_len);
520 link_name[ui->data_len] = '\0';
521
522 if (link_name[0] == '/') {
523 /* Absolute path, redo everything without
524 * the leading slash */
525 next = name = link_name + 1;
526 root_inum = 1;
527 continue;
528 }
529 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200530 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200531 link_name, next == NULL ? "" : next);
532 memcpy(symlinkpath, buf, sizeof(buf));
533 next = name = symlinkpath;
534 continue;
535 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100536
537 /*
538 * Check if directory with this name exists
539 */
540
541 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200542 if (!next || *next == '\0')
543 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100544
545 root_inum = inum;
546 name = next;
547 }
548
549 return 0;
550}
551
Simon Glasse3394752016-02-29 15:25:34 -0700552int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
Hans de Goedeb5030b22015-09-17 18:46:57 -0400553{
554 if (rbdd) {
555 debug("UBIFS cannot be used with normal block devices\n");
556 return -1;
557 }
558
559 /*
Simon Glasse76ee972016-02-29 15:25:44 -0700560 * Should never happen since blk_get_device_part_str() already checks
Hans de Goedeb5030b22015-09-17 18:46:57 -0400561 * this, but better safe then sorry.
562 */
563 if (!ubifs_is_mounted()) {
564 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
565 return -1;
566 }
567
568 return 0;
569}
570
Hans de Goedea1644772015-09-17 18:46:56 -0400571int ubifs_ls(const char *filename)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100572{
573 struct ubifs_info *c = ubifs_sb->s_fs_info;
574 struct file *file;
575 struct dentry *dentry;
576 struct inode *dir;
577 void *dirent = NULL;
578 unsigned long inum;
579 int ret = 0;
580
581 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedea1644772015-09-17 18:46:56 -0400582 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100583 if (!inum) {
584 ret = -1;
585 goto out;
586 }
587
588 file = kzalloc(sizeof(struct file), 0);
589 dentry = kzalloc(sizeof(struct dentry), 0);
590 dir = kzalloc(sizeof(struct inode), 0);
591 if (!file || !dentry || !dir) {
592 printf("%s: Error, no memory for malloc!\n", __func__);
593 ret = -ENOMEM;
594 goto out_mem;
595 }
596
597 dir->i_sb = ubifs_sb;
598 file->f_path.dentry = dentry;
599 file->f_path.dentry->d_parent = dentry;
600 file->f_path.dentry->d_inode = dir;
601 file->f_path.dentry->d_inode->i_ino = inum;
602 file->f_pos = 1;
603 file->private_data = NULL;
604 ubifs_printdir(file, dirent);
605
606out_mem:
607 if (file)
608 free(file);
609 if (dentry)
610 free(dentry);
611 if (dir)
612 free(dir);
613
614out:
615 ubi_close_volume(c->ubi);
616 return ret;
617}
618
Hans de Goedeb5030b22015-09-17 18:46:57 -0400619int ubifs_exists(const char *filename)
620{
621 struct ubifs_info *c = ubifs_sb->s_fs_info;
622 unsigned long inum;
623
624 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
625 inum = ubifs_findfile(ubifs_sb, (char *)filename);
626 ubi_close_volume(c->ubi);
627
628 return inum != 0;
629}
630
631int ubifs_size(const char *filename, loff_t *size)
632{
633 struct ubifs_info *c = ubifs_sb->s_fs_info;
634 unsigned long inum;
635 struct inode *inode;
636 int err = 0;
637
638 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
639
640 inum = ubifs_findfile(ubifs_sb, (char *)filename);
641 if (!inum) {
642 err = -1;
643 goto out;
644 }
645
646 inode = ubifs_iget(ubifs_sb, inum);
647 if (IS_ERR(inode)) {
648 printf("%s: Error reading inode %ld!\n", __func__, inum);
649 err = PTR_ERR(inode);
650 goto out;
651 }
652
653 *size = inode->i_size;
654
655 ubifs_iput(inode);
656out:
657 ubi_close_volume(c->ubi);
658 return err;
659}
660
Stefan Roese2fc10f62009-03-19 15:35:05 +0100661/*
662 * ubifsload...
663 */
664
665/* file.c */
666
667static inline void *kmap(struct page *page)
668{
669 return page->addr;
670}
671
672static int read_block(struct inode *inode, void *addr, unsigned int block,
673 struct ubifs_data_node *dn)
674{
675 struct ubifs_info *c = inode->i_sb->s_fs_info;
676 int err, len, out_len;
677 union ubifs_key key;
678 unsigned int dlen;
679
680 data_key_init(c, &key, inode->i_ino, block);
681 err = ubifs_tnc_lookup(c, &key, dn);
682 if (err) {
683 if (err == -ENOENT)
684 /* Not found, so it must be a hole */
685 memset(addr, 0, UBIFS_BLOCK_SIZE);
686 return err;
687 }
688
689 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
690
691 len = le32_to_cpu(dn->size);
692 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
693 goto dump;
694
695 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
696 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200697 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese2fc10f62009-03-19 15:35:05 +0100698 le16_to_cpu(dn->compr_type));
699 if (err || len != out_len)
700 goto dump;
701
702 /*
703 * Data length can be less than a full block, even for blocks that are
704 * not the last in the file (e.g., as a result of making a hole and
705 * appending data). Ensure that the remainder is zeroed out.
706 */
707 if (len < UBIFS_BLOCK_SIZE)
708 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
709
710 return 0;
711
712dump:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200713 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100714 block, inode->i_ino);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200715 ubifs_dump_node(c, dn);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100716 return -EINVAL;
717}
718
Stefan Roese28ea29f2010-11-01 17:28:00 +0100719static int do_readpage(struct ubifs_info *c, struct inode *inode,
720 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100721{
722 void *addr;
723 int err = 0, i;
724 unsigned int block, beyond;
725 struct ubifs_data_node *dn;
726 loff_t i_size = inode->i_size;
727
728 dbg_gen("ino %lu, pg %lu, i_size %lld",
729 inode->i_ino, page->index, i_size);
730
731 addr = kmap(page);
732
733 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
734 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
735 if (block >= beyond) {
736 /* Reading beyond inode */
737 memset(addr, 0, PAGE_CACHE_SIZE);
738 goto out;
739 }
740
741 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200742 if (!dn)
743 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100744
745 i = 0;
746 while (1) {
747 int ret;
748
749 if (block >= beyond) {
750 /* Reading beyond inode */
751 err = -ENOENT;
752 memset(addr, 0, UBIFS_BLOCK_SIZE);
753 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100754 /*
755 * Reading last block? Make sure to not write beyond
756 * the requested size in the destination buffer.
757 */
758 if (((block + 1) == beyond) || last_block_size) {
759 void *buff;
760 int dlen;
761
762 /*
763 * We need to buffer the data locally for the
764 * last block. This is to not pad the
765 * destination area to a multiple of
766 * UBIFS_BLOCK_SIZE.
767 */
Marcel Ziswilerabc574b2015-08-18 13:06:37 +0200768 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roese28ea29f2010-11-01 17:28:00 +0100769 if (!buff) {
770 printf("%s: Error, malloc fails!\n",
771 __func__);
772 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100773 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100774 }
775
776 /* Read block-size into temp buffer */
777 ret = read_block(inode, buff, block, dn);
778 if (ret) {
779 err = ret;
780 if (err != -ENOENT) {
781 free(buff);
782 break;
783 }
784 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100785
Stefan Roese28ea29f2010-11-01 17:28:00 +0100786 if (last_block_size)
787 dlen = last_block_size;
788 else
789 dlen = le32_to_cpu(dn->size);
790
791 /* Now copy required size back to dest */
792 memcpy(addr, buff, dlen);
793
794 free(buff);
795 } else {
796 ret = read_block(inode, addr, block, dn);
797 if (ret) {
798 err = ret;
799 if (err != -ENOENT)
800 break;
801 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100802 }
803 }
804 if (++i >= UBIFS_BLOCKS_PER_PAGE)
805 break;
806 block += 1;
807 addr += UBIFS_BLOCK_SIZE;
808 }
809 if (err) {
810 if (err == -ENOENT) {
811 /* Not found, so it must be a hole */
812 dbg_gen("hole");
813 goto out_free;
814 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200815 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese2fc10f62009-03-19 15:35:05 +0100816 page->index, inode->i_ino, err);
817 goto error;
818 }
819
820out_free:
821 kfree(dn);
822out:
823 return 0;
824
825error:
826 kfree(dn);
827 return err;
828}
829
Hans de Goedea1644772015-09-17 18:46:56 -0400830int ubifs_read(const char *filename, void *buf, loff_t offset,
831 loff_t size, loff_t *actread)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100832{
833 struct ubifs_info *c = ubifs_sb->s_fs_info;
834 unsigned long inum;
835 struct inode *inode;
836 struct page page;
837 int err = 0;
838 int i;
839 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100840 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100841
Hans de Goedea1644772015-09-17 18:46:56 -0400842 *actread = 0;
843
844 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadianbfdfd102016-10-23 20:45:16 -0700845 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedea1644772015-09-17 18:46:56 -0400846 PAGE_SIZE);
847 return -1;
848 }
849
Stefan Roese2fc10f62009-03-19 15:35:05 +0100850 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200851 /* ubifs_findfile will resolve symlinks, so we know that we get
852 * the real file here */
Hans de Goedea1644772015-09-17 18:46:56 -0400853 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100854 if (!inum) {
855 err = -1;
856 goto out;
857 }
858
859 /*
860 * Read file inode
861 */
862 inode = ubifs_iget(ubifs_sb, inum);
863 if (IS_ERR(inode)) {
864 printf("%s: Error reading inode %ld!\n", __func__, inum);
865 err = PTR_ERR(inode);
866 goto out;
867 }
868
Hans de Goedea1644772015-09-17 18:46:56 -0400869 if (offset > inode->i_size) {
870 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
871 offset, size);
872 err = -1;
873 goto put_inode;
874 }
875
Stefan Roese2fc10f62009-03-19 15:35:05 +0100876 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100877 * If no size was specified or if size bigger than filesize
878 * set size to filesize
879 */
Hans de Goedea1644772015-09-17 18:46:56 -0400880 if ((size == 0) || (size > (inode->i_size - offset)))
881 size = inode->i_size - offset;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100882
883 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100884
Hans de Goedea1644772015-09-17 18:46:56 -0400885 page.addr = buf;
886 page.index = offset / PAGE_SIZE;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100887 page.inode = inode;
888 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100889 /*
890 * Make sure to not read beyond the requested size
891 */
892 if (((i + 1) == count) && (size < inode->i_size))
893 last_block_size = size - (i * PAGE_SIZE);
894
895 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100896 if (err)
897 break;
898
899 page.addr += PAGE_SIZE;
900 page.index++;
901 }
902
Hans de Goedea1644772015-09-17 18:46:56 -0400903 if (err) {
Stefan Roese2fc10f62009-03-19 15:35:05 +0100904 printf("Error reading file '%s'\n", filename);
Hans de Goedea1644772015-09-17 18:46:56 -0400905 *actread = i * PAGE_SIZE;
906 } else {
907 *actread = size;
Bastian Ruppertfd43edd2011-09-05 03:03:57 +0000908 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100909
Hans de Goedea1644772015-09-17 18:46:56 -0400910put_inode:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100911 ubifs_iput(inode);
912
913out:
914 ubi_close_volume(c->ubi);
915 return err;
916}
Hans de Goedea1644772015-09-17 18:46:56 -0400917
Hans de Goedeb5030b22015-09-17 18:46:57 -0400918void ubifs_close(void)
919{
920}
921
Hans de Goedea1644772015-09-17 18:46:56 -0400922/* Compat wrappers for common/cmd_ubifs.c */
923int ubifs_load(char *filename, u32 addr, u32 size)
924{
925 loff_t actread;
926 int err;
927
928 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
929
Siva Durga Prasad Paladugu67eccfb2017-05-30 14:29:06 +0200930 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400931 if (err == 0) {
Simon Glass4d949a22017-08-03 12:22:10 -0600932 env_set_hex("filesize", actread);
Hans de Goedea1644772015-09-17 18:46:56 -0400933 printf("Done\n");
934 }
935
936 return err;
937}
938
939void uboot_ubifs_umount(void)
940{
941 if (ubifs_sb) {
942 printf("Unmounting UBIFS volume %s!\n",
943 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
944 ubifs_umount(ubifs_sb->s_fs_info);
945 ubifs_sb = NULL;
946 }
947}