blob: bb3551238033c104f2bfd10af2e8b024b7fce435 [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 Glass336d4612020-02-03 07:36:16 -070017#include <malloc.h>
Simon Glass6e295182015-09-02 17:24:57 -060018#include <memalign.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010019#include "ubifs.h"
Simon Glasse6f6f9e2020-05-10 11:39:58 -060020#include <part.h>
Simon Glass61b29b82020-02-03 07:36:15 -070021#include <dm/devres.h>
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020022#include <u-boot/zlib.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010023
AKASHI Takahiro4839e862019-11-13 09:44:47 +090024#include <linux/compat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020025#include <linux/err.h>
26#include <linux/lzo.h>
27
Stefan Roese9eefe2a2009-03-19 15:35:05 +010028DECLARE_GLOBAL_DATA_PTR;
29
30/* compress.c */
31
32/*
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020033 * We need a wrapper for zunzip() because the parameters are
Stefan Roese9eefe2a2009-03-19 15:35:05 +010034 * incompatible with the lzo decompressor.
35 */
36static int gzip_decompress(const unsigned char *in, size_t in_len,
37 unsigned char *out, size_t *out_len)
38{
Veli-Pekka Peltola8044c132012-09-05 18:05:14 +030039 return zunzip(out, *out_len, (unsigned char *)in,
40 (unsigned long *)out_len, 0, 0);
Stefan Roese9eefe2a2009-03-19 15:35:05 +010041}
42
43/* Fake description object for the "none" compressor */
44static struct ubifs_compressor none_compr = {
45 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherff94bc42014-06-24 10:10:04 +020046 .name = "none",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010047 .capi_name = "",
48 .decompress = NULL,
49};
50
51static struct ubifs_compressor lzo_compr = {
52 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherff94bc42014-06-24 10:10:04 +020053#ifndef __UBOOT__
54 .comp_mutex = &lzo_mutex,
55#endif
56 .name = "lzo",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010057 .capi_name = "lzo",
58 .decompress = lzo1x_decompress_safe,
59};
60
61static struct ubifs_compressor zlib_compr = {
62 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherff94bc42014-06-24 10:10:04 +020063#ifndef __UBOOT__
64 .comp_mutex = &deflate_mutex,
65 .decomp_mutex = &inflate_mutex,
66#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +010067 .name = "zlib",
68 .capi_name = "deflate",
69 .decompress = gzip_decompress,
70};
71
72/* All UBIFS compressors */
73struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
74
Heiko Schocherff94bc42014-06-24 10:10:04 +020075
76#ifdef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +020077
78struct crypto_comp {
79 int compressor;
80};
81
Heiko Schocher0195a7b2015-10-22 06:19:21 +020082static inline struct crypto_comp
83*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherff94bc42014-06-24 10:10:04 +020084{
85 struct ubifs_compressor *comp;
86 struct crypto_comp *ptr;
87 int i = 0;
88
Marcel Ziswiler45196682015-08-18 13:06:37 +020089 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherff94bc42014-06-24 10:10:04 +020090 while (i < UBIFS_COMPR_TYPES_CNT) {
91 comp = ubifs_compressors[i];
92 if (!comp) {
93 i++;
94 continue;
95 }
96 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
97 ptr->compressor = i;
98 return ptr;
99 }
100 i++;
101 }
102 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200103 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200104 free (ptr);
105 return NULL;
106 }
107 return ptr;
108}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200109static inline int
110crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
111 const u8 *src, unsigned int slen, u8 *dst,
112 unsigned int *dlen)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200113{
114 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
115 int err;
Paul Daveye4aa10b2018-11-05 18:09:29 +1300116 size_t tmp_len = *dlen;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200117
118 if (compr->compr_type == UBIFS_COMPR_NONE) {
119 memcpy(dst, src, slen);
120 *dlen = slen;
121 return 0;
122 }
123
Paul Daveye4aa10b2018-11-05 18:09:29 +1300124 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200125 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200126 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127 "error %d", slen, compr->name, err);
128
Paul Daveye4aa10b2018-11-05 18:09:29 +1300129 *dlen = tmp_len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200130 return err;
131
132 return 0;
133}
Anton Habeggerdc288432015-01-22 22:29:10 +0100134
135/* from shrinker.c */
136
137/* Global clean znode counter (for all mounted UBIFS instances) */
138atomic_long_t ubifs_clean_zn_cnt;
139
Heiko Schocherff94bc42014-06-24 10:10:04 +0200140#endif
141
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100142/**
143 * ubifs_decompress - decompress data.
144 * @in_buf: data to decompress
145 * @in_len: length of the data to decompress
146 * @out_buf: output buffer where decompressed data should
147 * @out_len: output length is returned here
148 * @compr_type: type of compression
149 *
150 * This function decompresses data from buffer @in_buf into buffer @out_buf.
151 * The length of the uncompressed data is returned in @out_len. This functions
152 * returns %0 on success or a negative error code on failure.
153 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200154int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
155 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100156{
157 int err;
158 struct ubifs_compressor *compr;
159
160 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200161 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100162 return -EINVAL;
163 }
164
165 compr = ubifs_compressors[compr_type];
166
167 if (unlikely(!compr->capi_name)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200168 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100169 return -EINVAL;
170 }
171
172 if (compr_type == UBIFS_COMPR_NONE) {
173 memcpy(out_buf, in_buf, in_len);
174 *out_len = in_len;
175 return 0;
176 }
177
Heiko Schocherff94bc42014-06-24 10:10:04 +0200178 if (compr->decomp_mutex)
179 mutex_lock(compr->decomp_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200180 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200181 (unsigned int *)out_len);
182 if (compr->decomp_mutex)
183 mutex_unlock(compr->decomp_mutex);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100184 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200185 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
186 " error %d", in_len, compr->name, err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100187
188 return err;
189}
190
191/**
192 * compr_init - initialize a compressor.
193 * @compr: compressor description object
194 *
195 * This function initializes the requested compressor and returns zero in case
196 * of success or a negative error code in case of failure.
197 */
198static int __init compr_init(struct ubifs_compressor *compr)
199{
200 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser521af042009-09-21 11:20:36 -0500201
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200202#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100203 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
204 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
205 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500206#endif
207
Heiko Schocherff94bc42014-06-24 10:10:04 +0200208 if (compr->capi_name) {
209 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
210 if (IS_ERR(compr->cc)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200211 dbg_gen("cannot initialize compressor %s,"
212 " error %ld", compr->name,
213 PTR_ERR(compr->cc));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200214 return PTR_ERR(compr->cc);
215 }
216 }
217
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100218 return 0;
219}
220
221/**
222 * ubifs_compressors_init - initialize UBIFS compressors.
223 *
224 * This function initializes the compressor which were compiled in. Returns
225 * zero in case of success and a negative error code in case of failure.
226 */
227int __init ubifs_compressors_init(void)
228{
229 int err;
230
231 err = compr_init(&lzo_compr);
232 if (err)
233 return err;
234
235 err = compr_init(&zlib_compr);
236 if (err)
237 return err;
238
Michael Lawnickfaac4fd2009-03-19 10:06:41 +0100239 err = compr_init(&none_compr);
240 if (err)
241 return err;
242
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100243 return 0;
244}
245
246/*
247 * ubifsls...
248 */
249
250static int filldir(struct ubifs_info *c, const char *name, int namlen,
251 u64 ino, unsigned int d_type)
252{
253 struct inode *inode;
254 char filetime[32];
255
256 switch (d_type) {
257 case UBIFS_ITYPE_REG:
258 printf("\t");
259 break;
260 case UBIFS_ITYPE_DIR:
261 printf("<DIR>\t");
262 break;
263 case UBIFS_ITYPE_LNK:
264 printf("<LNK>\t");
265 break;
266 default:
267 printf("other\t");
268 break;
269 }
270
271 inode = ubifs_iget(c->vfs_sb, ino);
272 if (IS_ERR(inode)) {
273 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
274 __func__, ino, inode);
275 return -1;
276 }
277 ctime_r((time_t *)&inode->i_mtime, filetime);
278 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200279#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100280 ubifs_iput(inode);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200281#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100282
283 printf("%s\n", name);
284
285 return 0;
286}
287
288static int ubifs_printdir(struct file *file, void *dirent)
289{
290 int err, over = 0;
291 struct qstr nm;
292 union ubifs_key key;
293 struct ubifs_dent_node *dent;
294 struct inode *dir = file->f_path.dentry->d_inode;
295 struct ubifs_info *c = dir->i_sb->s_fs_info;
296
297 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
298
299 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
300 /*
301 * The directory was seek'ed to a senseless position or there
302 * are no more entries.
303 */
304 return 0;
305
306 if (file->f_pos == 1) {
307 /* Find the first entry in TNC and save it */
308 lowest_dent_key(c, &key, dir->i_ino);
309 nm.name = NULL;
310 dent = ubifs_tnc_next_ent(c, &key, &nm);
311 if (IS_ERR(dent)) {
312 err = PTR_ERR(dent);
313 goto out;
314 }
315
316 file->f_pos = key_hash_flash(c, &dent->key);
317 file->private_data = dent;
318 }
319
320 dent = file->private_data;
321 if (!dent) {
322 /*
323 * The directory was seek'ed to and is now readdir'ed.
324 * Find the entry corresponding to @file->f_pos or the
325 * closest one.
326 */
327 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
328 nm.name = NULL;
329 dent = ubifs_tnc_next_ent(c, &key, &nm);
330 if (IS_ERR(dent)) {
331 err = PTR_ERR(dent);
332 goto out;
333 }
334 file->f_pos = key_hash_flash(c, &dent->key);
335 file->private_data = dent;
336 }
337
338 while (1) {
339 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
340 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
341 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200342#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100343 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200344#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100345
346 nm.len = le16_to_cpu(dent->nlen);
347 over = filldir(c, (char *)dent->name, nm.len,
348 le64_to_cpu(dent->inum), dent->type);
349 if (over)
350 return 0;
351
352 /* Switch to the next entry */
353 key_read(c, &dent->key, &key);
354 nm.name = (char *)dent->name;
355 dent = ubifs_tnc_next_ent(c, &key, &nm);
356 if (IS_ERR(dent)) {
357 err = PTR_ERR(dent);
358 goto out;
359 }
360
361 kfree(file->private_data);
362 file->f_pos = key_hash_flash(c, &dent->key);
363 file->private_data = dent;
364 cond_resched();
365 }
366
367out:
368 if (err != -ENOENT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200369 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100370 return err;
371 }
372
373 kfree(file->private_data);
374 file->private_data = NULL;
375 file->f_pos = 2;
376 return 0;
377}
378
379static int ubifs_finddir(struct super_block *sb, char *dirname,
380 unsigned long root_inum, unsigned long *inum)
381{
382 int err;
383 struct qstr nm;
384 union ubifs_key key;
385 struct ubifs_dent_node *dent;
386 struct ubifs_info *c;
387 struct file *file;
388 struct dentry *dentry;
389 struct inode *dir;
Stefan Roesebe739132012-08-28 14:00:24 +0200390 int ret = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100391
392 file = kzalloc(sizeof(struct file), 0);
393 dentry = kzalloc(sizeof(struct dentry), 0);
394 dir = kzalloc(sizeof(struct inode), 0);
395 if (!file || !dentry || !dir) {
396 printf("%s: Error, no memory for malloc!\n", __func__);
397 err = -ENOMEM;
398 goto out;
399 }
400
401 dir->i_sb = sb;
402 file->f_path.dentry = dentry;
403 file->f_path.dentry->d_parent = dentry;
404 file->f_path.dentry->d_inode = dir;
405 file->f_path.dentry->d_inode->i_ino = root_inum;
406 c = sb->s_fs_info;
407
408 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
409
410 /* Find the first entry in TNC and save it */
411 lowest_dent_key(c, &key, dir->i_ino);
412 nm.name = NULL;
413 dent = ubifs_tnc_next_ent(c, &key, &nm);
414 if (IS_ERR(dent)) {
415 err = PTR_ERR(dent);
416 goto out;
417 }
418
419 file->f_pos = key_hash_flash(c, &dent->key);
420 file->private_data = dent;
421
422 while (1) {
423 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
424 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
425 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200426#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100427 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200428#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100429
430 nm.len = le16_to_cpu(dent->nlen);
431 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
432 (strlen(dirname) == nm.len)) {
433 *inum = le64_to_cpu(dent->inum);
Stefan Roesebe739132012-08-28 14:00:24 +0200434 ret = 1;
435 goto out_free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100436 }
437
438 /* Switch to the next entry */
439 key_read(c, &dent->key, &key);
440 nm.name = (char *)dent->name;
441 dent = ubifs_tnc_next_ent(c, &key, &nm);
442 if (IS_ERR(dent)) {
443 err = PTR_ERR(dent);
444 goto out;
445 }
446
447 kfree(file->private_data);
448 file->f_pos = key_hash_flash(c, &dent->key);
449 file->private_data = dent;
450 cond_resched();
451 }
452
453out:
Stefan Roesebe739132012-08-28 14:00:24 +0200454 if (err != -ENOENT)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200455 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100456
Stefan Roesebe739132012-08-28 14:00:24 +0200457out_free:
Heinrich Schuchardt4b299752017-11-08 22:28:47 +0100458 kfree(file->private_data);
459 free(file);
460 free(dentry);
461 free(dir);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100462
Stefan Roesebe739132012-08-28 14:00:24 +0200463 return ret;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100464}
465
466static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
467{
468 int ret;
469 char *next;
470 char fpath[128];
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200471 char symlinkpath[128];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100472 char *name = fpath;
473 unsigned long root_inum = 1;
474 unsigned long inum;
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200475 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado64b68172010-12-02 15:02:35 +0100476 char link_name[64];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100477
478 strcpy(fpath, filename);
479
480 /* Remove all leading slashes */
481 while (*name == '/')
482 name++;
483
484 /*
485 * Handle root-direcoty ('/')
486 */
487 inum = root_inum;
488 if (!name || *name == '\0')
489 return inum;
490
491 for (;;) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200492 struct inode *inode;
493 struct ubifs_inode *ui;
494
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100495 /* Extract the actual part from the pathname. */
496 next = strchr(name, '/');
497 if (next) {
498 /* Remove all leading slashes. */
499 while (*next == '/')
500 *(next++) = '\0';
501 }
502
503 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200504 if (!ret)
505 return 0;
506 inode = ubifs_iget(sb, inum);
507
508 if (!inode)
509 return 0;
510 ui = ubifs_inode(inode);
511
512 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200513 char buf[128];
514
515 /* We have some sort of symlink recursion, bail out */
516 if (symlink_count++ > 8) {
517 printf("Symlink recursion, aborting\n");
518 return 0;
519 }
520 memcpy(link_name, ui->data, ui->data_len);
521 link_name[ui->data_len] = '\0';
522
523 if (link_name[0] == '/') {
524 /* Absolute path, redo everything without
525 * the leading slash */
526 next = name = link_name + 1;
527 root_inum = 1;
528 continue;
529 }
530 /* Relative to cur dir */
Simon Kagstromef37c682009-09-25 14:05:57 +0200531 sprintf(buf, "%s/%s",
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200532 link_name, next == NULL ? "" : next);
533 memcpy(symlinkpath, buf, sizeof(buf));
534 next = name = symlinkpath;
535 continue;
536 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100537
538 /*
539 * Check if directory with this name exists
540 */
541
542 /* Found the node! */
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200543 if (!next || *next == '\0')
544 return inum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100545
546 root_inum = inum;
547 name = next;
548 }
549
550 return 0;
551}
552
Simon Glass05289792020-05-10 11:39:57 -0600553int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400554{
555 if (rbdd) {
556 debug("UBIFS cannot be used with normal block devices\n");
557 return -1;
558 }
559
560 /*
Simon Glasse35929e2016-02-29 15:25:44 -0700561 * Should never happen since blk_get_device_part_str() already checks
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400562 * this, but better safe then sorry.
563 */
564 if (!ubifs_is_mounted()) {
565 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
566 return -1;
567 }
568
569 return 0;
570}
571
Hans de Goedead157492015-09-17 18:46:56 -0400572int ubifs_ls(const char *filename)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100573{
574 struct ubifs_info *c = ubifs_sb->s_fs_info;
575 struct file *file;
576 struct dentry *dentry;
577 struct inode *dir;
578 void *dirent = NULL;
579 unsigned long inum;
580 int ret = 0;
581
582 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedead157492015-09-17 18:46:56 -0400583 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100584 if (!inum) {
585 ret = -1;
586 goto out;
587 }
588
589 file = kzalloc(sizeof(struct file), 0);
590 dentry = kzalloc(sizeof(struct dentry), 0);
591 dir = kzalloc(sizeof(struct inode), 0);
592 if (!file || !dentry || !dir) {
593 printf("%s: Error, no memory for malloc!\n", __func__);
594 ret = -ENOMEM;
595 goto out_mem;
596 }
597
598 dir->i_sb = ubifs_sb;
599 file->f_path.dentry = dentry;
600 file->f_path.dentry->d_parent = dentry;
601 file->f_path.dentry->d_inode = dir;
602 file->f_path.dentry->d_inode->i_ino = inum;
603 file->f_pos = 1;
604 file->private_data = NULL;
605 ubifs_printdir(file, dirent);
606
607out_mem:
608 if (file)
609 free(file);
610 if (dentry)
611 free(dentry);
612 if (dir)
613 free(dir);
614
615out:
616 ubi_close_volume(c->ubi);
617 return ret;
618}
619
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400620int ubifs_exists(const char *filename)
621{
622 struct ubifs_info *c = ubifs_sb->s_fs_info;
623 unsigned long inum;
624
625 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
626 inum = ubifs_findfile(ubifs_sb, (char *)filename);
627 ubi_close_volume(c->ubi);
628
629 return inum != 0;
630}
631
632int ubifs_size(const char *filename, loff_t *size)
633{
634 struct ubifs_info *c = ubifs_sb->s_fs_info;
635 unsigned long inum;
636 struct inode *inode;
637 int err = 0;
638
639 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
640
641 inum = ubifs_findfile(ubifs_sb, (char *)filename);
642 if (!inum) {
643 err = -1;
644 goto out;
645 }
646
647 inode = ubifs_iget(ubifs_sb, inum);
648 if (IS_ERR(inode)) {
649 printf("%s: Error reading inode %ld!\n", __func__, inum);
650 err = PTR_ERR(inode);
651 goto out;
652 }
653
654 *size = inode->i_size;
655
656 ubifs_iput(inode);
657out:
658 ubi_close_volume(c->ubi);
659 return err;
660}
661
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100662/*
663 * ubifsload...
664 */
665
666/* file.c */
667
668static inline void *kmap(struct page *page)
669{
670 return page->addr;
671}
672
673static int read_block(struct inode *inode, void *addr, unsigned int block,
674 struct ubifs_data_node *dn)
675{
676 struct ubifs_info *c = inode->i_sb->s_fs_info;
677 int err, len, out_len;
678 union ubifs_key key;
679 unsigned int dlen;
680
681 data_key_init(c, &key, inode->i_ino, block);
682 err = ubifs_tnc_lookup(c, &key, dn);
683 if (err) {
684 if (err == -ENOENT)
685 /* Not found, so it must be a hole */
686 memset(addr, 0, UBIFS_BLOCK_SIZE);
687 return err;
688 }
689
690 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
691
692 len = le32_to_cpu(dn->size);
693 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
694 goto dump;
695
696 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
697 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200698 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100699 le16_to_cpu(dn->compr_type));
700 if (err || len != out_len)
701 goto dump;
702
703 /*
704 * Data length can be less than a full block, even for blocks that are
705 * not the last in the file (e.g., as a result of making a hole and
706 * appending data). Ensure that the remainder is zeroed out.
707 */
708 if (len < UBIFS_BLOCK_SIZE)
709 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
710
711 return 0;
712
713dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200714 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100715 block, inode->i_ino);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200716 ubifs_dump_node(c, dn);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100717 return -EINVAL;
718}
719
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100720static int do_readpage(struct ubifs_info *c, struct inode *inode,
721 struct page *page, int last_block_size)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100722{
723 void *addr;
724 int err = 0, i;
725 unsigned int block, beyond;
726 struct ubifs_data_node *dn;
727 loff_t i_size = inode->i_size;
728
729 dbg_gen("ino %lu, pg %lu, i_size %lld",
730 inode->i_ino, page->index, i_size);
731
732 addr = kmap(page);
733
734 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
735 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
736 if (block >= beyond) {
737 /* Reading beyond inode */
738 memset(addr, 0, PAGE_CACHE_SIZE);
739 goto out;
740 }
741
742 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mack165f9852009-06-04 19:44:12 +0200743 if (!dn)
744 return -ENOMEM;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100745
746 i = 0;
747 while (1) {
748 int ret;
749
750 if (block >= beyond) {
751 /* Reading beyond inode */
752 err = -ENOENT;
753 memset(addr, 0, UBIFS_BLOCK_SIZE);
754 } else {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100755 /*
756 * Reading last block? Make sure to not write beyond
757 * the requested size in the destination buffer.
758 */
759 if (((block + 1) == beyond) || last_block_size) {
760 void *buff;
761 int dlen;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100762
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100763 /*
764 * We need to buffer the data locally for the
765 * last block. This is to not pad the
766 * destination area to a multiple of
767 * UBIFS_BLOCK_SIZE.
768 */
Marcel Ziswiler45196682015-08-18 13:06:37 +0200769 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100770 if (!buff) {
771 printf("%s: Error, malloc fails!\n",
772 __func__);
773 err = -ENOMEM;
774 break;
775 }
776
777 /* Read block-size into temp buffer */
778 ret = read_block(inode, buff, block, dn);
779 if (ret) {
780 err = ret;
781 if (err != -ENOENT) {
782 free(buff);
783 break;
784 }
785 }
786
787 if (last_block_size)
788 dlen = last_block_size;
789 else
790 dlen = le32_to_cpu(dn->size);
791
792 /* Now copy required size back to dest */
793 memcpy(addr, buff, dlen);
794
795 free(buff);
796 } else {
797 ret = read_block(inode, addr, block, dn);
798 if (ret) {
799 err = ret;
800 if (err != -ENOENT)
801 break;
802 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100803 }
804 }
805 if (++i >= UBIFS_BLOCKS_PER_PAGE)
806 break;
807 block += 1;
808 addr += UBIFS_BLOCK_SIZE;
809 }
810 if (err) {
811 if (err == -ENOENT) {
812 /* Not found, so it must be a hole */
813 dbg_gen("hole");
814 goto out_free;
815 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200816 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100817 page->index, inode->i_ino, err);
818 goto error;
819 }
820
821out_free:
822 kfree(dn);
823out:
824 return 0;
825
826error:
827 kfree(dn);
828 return err;
829}
830
Hans de Goedead157492015-09-17 18:46:56 -0400831int ubifs_read(const char *filename, void *buf, loff_t offset,
832 loff_t size, loff_t *actread)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100833{
834 struct ubifs_info *c = ubifs_sb->s_fs_info;
835 unsigned long inum;
836 struct inode *inode;
837 struct page page;
838 int err = 0;
839 int i;
840 int count;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100841 int last_block_size = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100842
Hans de Goedead157492015-09-17 18:46:56 -0400843 *actread = 0;
844
845 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadian13819012016-10-23 20:45:16 -0700846 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedead157492015-09-17 18:46:56 -0400847 PAGE_SIZE);
848 return -1;
849 }
850
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100851 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200852 /* ubifs_findfile will resolve symlinks, so we know that we get
853 * the real file here */
Hans de Goedead157492015-09-17 18:46:56 -0400854 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100855 if (!inum) {
856 err = -1;
857 goto out;
858 }
859
860 /*
861 * Read file inode
862 */
863 inode = ubifs_iget(ubifs_sb, inum);
864 if (IS_ERR(inode)) {
865 printf("%s: Error reading inode %ld!\n", __func__, inum);
866 err = PTR_ERR(inode);
867 goto out;
868 }
869
Hans de Goedead157492015-09-17 18:46:56 -0400870 if (offset > inode->i_size) {
871 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
872 offset, size);
873 err = -1;
874 goto put_inode;
875 }
876
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100877 /*
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100878 * If no size was specified or if size bigger than filesize
879 * set size to filesize
880 */
Hans de Goedead157492015-09-17 18:46:56 -0400881 if ((size == 0) || (size > (inode->i_size - offset)))
882 size = inode->i_size - offset;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100883
884 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100885
Hans de Goedead157492015-09-17 18:46:56 -0400886 page.addr = buf;
887 page.index = offset / PAGE_SIZE;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100888 page.inode = inode;
889 for (i = 0; i < count; i++) {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100890 /*
891 * Make sure to not read beyond the requested size
892 */
893 if (((i + 1) == count) && (size < inode->i_size))
894 last_block_size = size - (i * PAGE_SIZE);
895
896 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100897 if (err)
898 break;
899
900 page.addr += PAGE_SIZE;
901 page.index++;
902 }
903
Hans de Goedead157492015-09-17 18:46:56 -0400904 if (err) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100905 printf("Error reading file '%s'\n", filename);
Hans de Goedead157492015-09-17 18:46:56 -0400906 *actread = i * PAGE_SIZE;
907 } else {
908 *actread = size;
Bastian Ruppert46d72742011-09-05 03:03:57 +0000909 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100910
Hans de Goedead157492015-09-17 18:46:56 -0400911put_inode:
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100912 ubifs_iput(inode);
913
914out:
915 ubi_close_volume(c->ubi);
916 return err;
917}
Hans de Goedead157492015-09-17 18:46:56 -0400918
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400919void ubifs_close(void)
920{
921}
922
Hans de Goedead157492015-09-17 18:46:56 -0400923/* Compat wrappers for common/cmd_ubifs.c */
924int ubifs_load(char *filename, u32 addr, u32 size)
925{
926 loff_t actread;
927 int err;
928
929 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
930
Siva Durga Prasad Paladugu34cc30a2017-05-30 14:29:06 +0200931 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedead157492015-09-17 18:46:56 -0400932 if (err == 0) {
Simon Glass018f5302017-08-03 12:22:10 -0600933 env_set_hex("filesize", actread);
Hans de Goedead157492015-09-17 18:46:56 -0400934 printf("Done\n");
935 }
936
937 return err;
938}
939
940void uboot_ubifs_umount(void)
941{
942 if (ubifs_sb) {
943 printf("Unmounting UBIFS volume %s!\n",
944 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
945 ubifs_umount(ubifs_sb->s_fs_info);
946 ubifs_sb = NULL;
947 }
948}