blob: 609bdbf60377316cb6b90086e3fa1ed32ea4e88f [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>
Simon Glass401d1c42020-10-30 21:38:53 -060020#include <asm/global_data.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010021#include "ubifs.h"
Simon Glasse6f6f9e2020-05-10 11:39:58 -060022#include <part.h>
Simon Glass61b29b82020-02-03 07:36:15 -070023#include <dm/devres.h>
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020024#include <u-boot/zlib.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010025
AKASHI Takahiro4839e862019-11-13 09:44:47 +090026#include <linux/compat.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020027#include <linux/err.h>
28#include <linux/lzo.h>
29
Stefan Roese9eefe2a2009-03-19 15:35:05 +010030DECLARE_GLOBAL_DATA_PTR;
31
32/* compress.c */
33
34/*
Ricardo Ribalda Delgadoc1a0fd52009-04-27 18:33:33 +020035 * We need a wrapper for zunzip() because the parameters are
Stefan Roese9eefe2a2009-03-19 15:35:05 +010036 * incompatible with the lzo decompressor.
37 */
38static int gzip_decompress(const unsigned char *in, size_t in_len,
39 unsigned char *out, size_t *out_len)
40{
Veli-Pekka Peltola8044c132012-09-05 18:05:14 +030041 return zunzip(out, *out_len, (unsigned char *)in,
42 (unsigned long *)out_len, 0, 0);
Stefan Roese9eefe2a2009-03-19 15:35:05 +010043}
44
45/* Fake description object for the "none" compressor */
46static struct ubifs_compressor none_compr = {
47 .compr_type = UBIFS_COMPR_NONE,
Heiko Schocherff94bc42014-06-24 10:10:04 +020048 .name = "none",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010049 .capi_name = "",
50 .decompress = NULL,
51};
52
53static struct ubifs_compressor lzo_compr = {
54 .compr_type = UBIFS_COMPR_LZO,
Heiko Schocherff94bc42014-06-24 10:10:04 +020055#ifndef __UBOOT__
56 .comp_mutex = &lzo_mutex,
57#endif
58 .name = "lzo",
Stefan Roese9eefe2a2009-03-19 15:35:05 +010059 .capi_name = "lzo",
60 .decompress = lzo1x_decompress_safe,
61};
62
63static struct ubifs_compressor zlib_compr = {
64 .compr_type = UBIFS_COMPR_ZLIB,
Heiko Schocherff94bc42014-06-24 10:10:04 +020065#ifndef __UBOOT__
66 .comp_mutex = &deflate_mutex,
67 .decomp_mutex = &inflate_mutex,
68#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +010069 .name = "zlib",
70 .capi_name = "deflate",
71 .decompress = gzip_decompress,
72};
73
74/* All UBIFS compressors */
75struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
76
Heiko Schocherff94bc42014-06-24 10:10:04 +020077
78#ifdef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +020079
80struct crypto_comp {
81 int compressor;
82};
83
Heiko Schocher0195a7b2015-10-22 06:19:21 +020084static inline struct crypto_comp
85*crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
Heiko Schocherff94bc42014-06-24 10:10:04 +020086{
87 struct ubifs_compressor *comp;
88 struct crypto_comp *ptr;
89 int i = 0;
90
Marcel Ziswiler45196682015-08-18 13:06:37 +020091 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
Heiko Schocherff94bc42014-06-24 10:10:04 +020092 while (i < UBIFS_COMPR_TYPES_CNT) {
93 comp = ubifs_compressors[i];
94 if (!comp) {
95 i++;
96 continue;
97 }
98 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
99 ptr->compressor = i;
100 return ptr;
101 }
102 i++;
103 }
104 if (i >= UBIFS_COMPR_TYPES_CNT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200105 dbg_gen("invalid compression type %s", alg_name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200106 free (ptr);
107 return NULL;
108 }
109 return ptr;
110}
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200111static inline int
112crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
113 const u8 *src, unsigned int slen, u8 *dst,
114 unsigned int *dlen)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200115{
116 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
117 int err;
Paul Daveye4aa10b2018-11-05 18:09:29 +1300118 size_t tmp_len = *dlen;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200119
120 if (compr->compr_type == UBIFS_COMPR_NONE) {
121 memcpy(dst, src, slen);
122 *dlen = slen;
123 return 0;
124 }
125
Paul Daveye4aa10b2018-11-05 18:09:29 +1300126 err = compr->decompress(src, slen, dst, &tmp_len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200127 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200128 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
Heiko Schocherff94bc42014-06-24 10:10:04 +0200129 "error %d", slen, compr->name, err);
130
Paul Daveye4aa10b2018-11-05 18:09:29 +1300131 *dlen = tmp_len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200132 return err;
133
134 return 0;
135}
Anton Habeggerdc288432015-01-22 22:29:10 +0100136
137/* from shrinker.c */
138
139/* Global clean znode counter (for all mounted UBIFS instances) */
140atomic_long_t ubifs_clean_zn_cnt;
141
Heiko Schocherff94bc42014-06-24 10:10:04 +0200142#endif
143
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100144/**
145 * ubifs_decompress - decompress data.
146 * @in_buf: data to decompress
147 * @in_len: length of the data to decompress
148 * @out_buf: output buffer where decompressed data should
149 * @out_len: output length is returned here
150 * @compr_type: type of compression
151 *
152 * This function decompresses data from buffer @in_buf into buffer @out_buf.
153 * The length of the uncompressed data is returned in @out_len. This functions
154 * returns %0 on success or a negative error code on failure.
155 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200156int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
157 int in_len, void *out_buf, int *out_len, int compr_type)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100158{
159 int err;
160 struct ubifs_compressor *compr;
161
162 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200163 ubifs_err(c, "invalid compression type %d", compr_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100164 return -EINVAL;
165 }
166
167 compr = ubifs_compressors[compr_type];
168
169 if (unlikely(!compr->capi_name)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200170 ubifs_err(c, "%s compression is not compiled in", compr->name);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100171 return -EINVAL;
172 }
173
174 if (compr_type == UBIFS_COMPR_NONE) {
175 memcpy(out_buf, in_buf, in_len);
176 *out_len = in_len;
177 return 0;
178 }
179
Heiko Schocherff94bc42014-06-24 10:10:04 +0200180 if (compr->decomp_mutex)
181 mutex_lock(compr->decomp_mutex);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200182 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200183 (unsigned int *)out_len);
184 if (compr->decomp_mutex)
185 mutex_unlock(compr->decomp_mutex);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100186 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200187 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
188 " error %d", in_len, compr->name, err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100189
190 return err;
191}
192
193/**
194 * compr_init - initialize a compressor.
195 * @compr: compressor description object
196 *
197 * This function initializes the requested compressor and returns zero in case
198 * of success or a negative error code in case of failure.
199 */
200static int __init compr_init(struct ubifs_compressor *compr)
201{
202 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser521af042009-09-21 11:20:36 -0500203
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200204#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100205 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
206 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
207 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
Peter Tyser521af042009-09-21 11:20:36 -0500208#endif
209
Heiko Schocherff94bc42014-06-24 10:10:04 +0200210 if (compr->capi_name) {
211 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
212 if (IS_ERR(compr->cc)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200213 dbg_gen("cannot initialize compressor %s,"
214 " error %ld", compr->name,
215 PTR_ERR(compr->cc));
Heiko Schocherff94bc42014-06-24 10:10:04 +0200216 return PTR_ERR(compr->cc);
217 }
218 }
219
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100220 return 0;
221}
222
223/**
224 * ubifs_compressors_init - initialize UBIFS compressors.
225 *
226 * This function initializes the compressor which were compiled in. Returns
227 * zero in case of success and a negative error code in case of failure.
228 */
229int __init ubifs_compressors_init(void)
230{
231 int err;
232
233 err = compr_init(&lzo_compr);
234 if (err)
235 return err;
236
237 err = compr_init(&zlib_compr);
238 if (err)
239 return err;
240
Michael Lawnickfaac4fd2009-03-19 10:06:41 +0100241 err = compr_init(&none_compr);
242 if (err)
243 return err;
244
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100245 return 0;
246}
247
248/*
249 * ubifsls...
250 */
251
252static int filldir(struct ubifs_info *c, const char *name, int namlen,
253 u64 ino, unsigned int d_type)
254{
255 struct inode *inode;
256 char filetime[32];
257
258 switch (d_type) {
259 case UBIFS_ITYPE_REG:
260 printf("\t");
261 break;
262 case UBIFS_ITYPE_DIR:
263 printf("<DIR>\t");
264 break;
265 case UBIFS_ITYPE_LNK:
266 printf("<LNK>\t");
267 break;
268 default:
269 printf("other\t");
270 break;
271 }
272
273 inode = ubifs_iget(c->vfs_sb, ino);
274 if (IS_ERR(inode)) {
275 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
276 __func__, ino, inode);
277 return -1;
278 }
279 ctime_r((time_t *)&inode->i_mtime, filetime);
280 printf("%9lld %24.24s ", inode->i_size, filetime);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200281#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100282 ubifs_iput(inode);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200283#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100284
285 printf("%s\n", name);
286
287 return 0;
288}
289
290static int ubifs_printdir(struct file *file, void *dirent)
291{
292 int err, over = 0;
293 struct qstr nm;
294 union ubifs_key key;
295 struct ubifs_dent_node *dent;
296 struct inode *dir = file->f_path.dentry->d_inode;
297 struct ubifs_info *c = dir->i_sb->s_fs_info;
298
299 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
300
301 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
302 /*
303 * The directory was seek'ed to a senseless position or there
304 * are no more entries.
305 */
306 return 0;
307
308 if (file->f_pos == 1) {
309 /* Find the first entry in TNC and save it */
310 lowest_dent_key(c, &key, dir->i_ino);
311 nm.name = NULL;
312 dent = ubifs_tnc_next_ent(c, &key, &nm);
313 if (IS_ERR(dent)) {
314 err = PTR_ERR(dent);
315 goto out;
316 }
317
318 file->f_pos = key_hash_flash(c, &dent->key);
319 file->private_data = dent;
320 }
321
322 dent = file->private_data;
323 if (!dent) {
324 /*
325 * The directory was seek'ed to and is now readdir'ed.
326 * Find the entry corresponding to @file->f_pos or the
327 * closest one.
328 */
329 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
330 nm.name = NULL;
331 dent = ubifs_tnc_next_ent(c, &key, &nm);
332 if (IS_ERR(dent)) {
333 err = PTR_ERR(dent);
334 goto out;
335 }
336 file->f_pos = key_hash_flash(c, &dent->key);
337 file->private_data = dent;
338 }
339
340 while (1) {
341 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
342 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
343 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200344#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100345 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200346#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100347
348 nm.len = le16_to_cpu(dent->nlen);
349 over = filldir(c, (char *)dent->name, nm.len,
350 le64_to_cpu(dent->inum), dent->type);
351 if (over)
352 return 0;
353
354 /* Switch to the next entry */
355 key_read(c, &dent->key, &key);
356 nm.name = (char *)dent->name;
357 dent = ubifs_tnc_next_ent(c, &key, &nm);
358 if (IS_ERR(dent)) {
359 err = PTR_ERR(dent);
360 goto out;
361 }
362
363 kfree(file->private_data);
364 file->f_pos = key_hash_flash(c, &dent->key);
365 file->private_data = dent;
366 cond_resched();
367 }
368
369out:
370 if (err != -ENOENT) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200371 ubifs_err(c, "cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100372 return err;
373 }
374
375 kfree(file->private_data);
376 file->private_data = NULL;
377 file->f_pos = 2;
378 return 0;
379}
380
381static int ubifs_finddir(struct super_block *sb, char *dirname,
382 unsigned long root_inum, unsigned long *inum)
383{
384 int err;
385 struct qstr nm;
386 union ubifs_key key;
387 struct ubifs_dent_node *dent;
388 struct ubifs_info *c;
389 struct file *file;
390 struct dentry *dentry;
391 struct inode *dir;
Stefan Roesebe739132012-08-28 14:00:24 +0200392 int ret = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100393
394 file = kzalloc(sizeof(struct file), 0);
395 dentry = kzalloc(sizeof(struct dentry), 0);
396 dir = kzalloc(sizeof(struct inode), 0);
397 if (!file || !dentry || !dir) {
398 printf("%s: Error, no memory for malloc!\n", __func__);
399 err = -ENOMEM;
400 goto out;
401 }
402
403 dir->i_sb = sb;
404 file->f_path.dentry = dentry;
405 file->f_path.dentry->d_parent = dentry;
406 file->f_path.dentry->d_inode = dir;
407 file->f_path.dentry->d_inode->i_ino = root_inum;
408 c = sb->s_fs_info;
409
410 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
411
412 /* Find the first entry in TNC and save it */
413 lowest_dent_key(c, &key, dir->i_ino);
414 nm.name = NULL;
415 dent = ubifs_tnc_next_ent(c, &key, &nm);
416 if (IS_ERR(dent)) {
417 err = PTR_ERR(dent);
418 goto out;
419 }
420
421 file->f_pos = key_hash_flash(c, &dent->key);
422 file->private_data = dent;
423
424 while (1) {
425 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
426 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
427 key_hash_flash(c, &dent->key));
Patrice Chotard87deefe2018-04-27 15:51:23 +0200428#ifndef __UBOOT__
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100429 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
Patrice Chotard87deefe2018-04-27 15:51:23 +0200430#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100431
432 nm.len = le16_to_cpu(dent->nlen);
433 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
434 (strlen(dirname) == nm.len)) {
435 *inum = le64_to_cpu(dent->inum);
Stefan Roesebe739132012-08-28 14:00:24 +0200436 ret = 1;
437 goto out_free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100438 }
439
440 /* Switch to the next entry */
441 key_read(c, &dent->key, &key);
442 nm.name = (char *)dent->name;
443 dent = ubifs_tnc_next_ent(c, &key, &nm);
444 if (IS_ERR(dent)) {
445 err = PTR_ERR(dent);
446 goto out;
447 }
448
449 kfree(file->private_data);
450 file->f_pos = key_hash_flash(c, &dent->key);
451 file->private_data = dent;
452 cond_resched();
453 }
454
455out:
Stefan Roesebe739132012-08-28 14:00:24 +0200456 if (err != -ENOENT)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200457 dbg_gen("cannot find next direntry, error %d", err);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100458
Stefan Roesebe739132012-08-28 14:00:24 +0200459out_free:
Heinrich Schuchardt4b299752017-11-08 22:28:47 +0100460 kfree(file->private_data);
461 free(file);
462 free(dentry);
463 free(dir);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100464
Stefan Roesebe739132012-08-28 14:00:24 +0200465 return ret;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100466}
467
468static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
469{
470 int ret;
471 char *next;
472 char fpath[128];
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200473 char symlinkpath[128];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100474 char *name = fpath;
475 unsigned long root_inum = 1;
476 unsigned long inum;
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200477 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado64b68172010-12-02 15:02:35 +0100478 char link_name[64];
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100479
480 strcpy(fpath, filename);
481
482 /* Remove all leading slashes */
483 while (*name == '/')
484 name++;
485
486 /*
487 * Handle root-direcoty ('/')
488 */
489 inum = root_inum;
490 if (!name || *name == '\0')
491 return inum;
492
493 for (;;) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200494 struct inode *inode;
495 struct ubifs_inode *ui;
496
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100497 /* Extract the actual part from the pathname. */
498 next = strchr(name, '/');
499 if (next) {
500 /* Remove all leading slashes. */
501 while (*next == '/')
502 *(next++) = '\0';
503 }
504
505 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200506 if (!ret)
507 return 0;
508 inode = ubifs_iget(sb, inum);
509
510 if (!inode)
511 return 0;
512 ui = ubifs_inode(inode);
513
514 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200515 char buf[128];
516
517 /* We have some sort of symlink recursion, bail out */
518 if (symlink_count++ > 8) {
519 printf("Symlink recursion, aborting\n");
520 return 0;
521 }
522 memcpy(link_name, ui->data, ui->data_len);
523 link_name[ui->data_len] = '\0';
524
525 if (link_name[0] == '/') {
526 /* Absolute path, redo everything without
527 * the leading slash */
528 next = name = link_name + 1;
529 root_inum = 1;
530 continue;
531 }
532 /* Relative to cur dir */
Simon Kagstromef37c682009-09-25 14:05:57 +0200533 sprintf(buf, "%s/%s",
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200534 link_name, next == NULL ? "" : next);
535 memcpy(symlinkpath, buf, sizeof(buf));
536 next = name = symlinkpath;
537 continue;
538 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100539
540 /*
541 * Check if directory with this name exists
542 */
543
544 /* Found the node! */
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200545 if (!next || *next == '\0')
546 return inum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100547
548 root_inum = inum;
549 name = next;
550 }
551
552 return 0;
553}
554
Simon Glass05289792020-05-10 11:39:57 -0600555int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400556{
557 if (rbdd) {
558 debug("UBIFS cannot be used with normal block devices\n");
559 return -1;
560 }
561
562 /*
Simon Glasse35929e2016-02-29 15:25:44 -0700563 * Should never happen since blk_get_device_part_str() already checks
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400564 * this, but better safe then sorry.
565 */
566 if (!ubifs_is_mounted()) {
567 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
568 return -1;
569 }
570
571 return 0;
572}
573
Hans de Goedead157492015-09-17 18:46:56 -0400574int ubifs_ls(const char *filename)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100575{
576 struct ubifs_info *c = ubifs_sb->s_fs_info;
577 struct file *file;
578 struct dentry *dentry;
579 struct inode *dir;
580 void *dirent = NULL;
581 unsigned long inum;
582 int ret = 0;
583
584 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Hans de Goedead157492015-09-17 18:46:56 -0400585 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100586 if (!inum) {
587 ret = -1;
588 goto out;
589 }
590
591 file = kzalloc(sizeof(struct file), 0);
592 dentry = kzalloc(sizeof(struct dentry), 0);
593 dir = kzalloc(sizeof(struct inode), 0);
594 if (!file || !dentry || !dir) {
595 printf("%s: Error, no memory for malloc!\n", __func__);
596 ret = -ENOMEM;
597 goto out_mem;
598 }
599
600 dir->i_sb = ubifs_sb;
601 file->f_path.dentry = dentry;
602 file->f_path.dentry->d_parent = dentry;
603 file->f_path.dentry->d_inode = dir;
604 file->f_path.dentry->d_inode->i_ino = inum;
605 file->f_pos = 1;
606 file->private_data = NULL;
607 ubifs_printdir(file, dirent);
608
609out_mem:
610 if (file)
611 free(file);
612 if (dentry)
613 free(dentry);
614 if (dir)
615 free(dir);
616
617out:
618 ubi_close_volume(c->ubi);
619 return ret;
620}
621
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400622int ubifs_exists(const char *filename)
623{
624 struct ubifs_info *c = ubifs_sb->s_fs_info;
625 unsigned long inum;
626
627 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
628 inum = ubifs_findfile(ubifs_sb, (char *)filename);
629 ubi_close_volume(c->ubi);
630
631 return inum != 0;
632}
633
634int ubifs_size(const char *filename, loff_t *size)
635{
636 struct ubifs_info *c = ubifs_sb->s_fs_info;
637 unsigned long inum;
638 struct inode *inode;
639 int err = 0;
640
641 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
642
643 inum = ubifs_findfile(ubifs_sb, (char *)filename);
644 if (!inum) {
645 err = -1;
646 goto out;
647 }
648
649 inode = ubifs_iget(ubifs_sb, inum);
650 if (IS_ERR(inode)) {
651 printf("%s: Error reading inode %ld!\n", __func__, inum);
652 err = PTR_ERR(inode);
653 goto out;
654 }
655
656 *size = inode->i_size;
657
658 ubifs_iput(inode);
659out:
660 ubi_close_volume(c->ubi);
661 return err;
662}
663
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100664/*
665 * ubifsload...
666 */
667
668/* file.c */
669
670static inline void *kmap(struct page *page)
671{
672 return page->addr;
673}
674
675static int read_block(struct inode *inode, void *addr, unsigned int block,
676 struct ubifs_data_node *dn)
677{
678 struct ubifs_info *c = inode->i_sb->s_fs_info;
679 int err, len, out_len;
680 union ubifs_key key;
681 unsigned int dlen;
682
683 data_key_init(c, &key, inode->i_ino, block);
684 err = ubifs_tnc_lookup(c, &key, dn);
685 if (err) {
686 if (err == -ENOENT)
687 /* Not found, so it must be a hole */
688 memset(addr, 0, UBIFS_BLOCK_SIZE);
689 return err;
690 }
691
692 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
693
694 len = le32_to_cpu(dn->size);
695 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
696 goto dump;
697
698 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
699 out_len = UBIFS_BLOCK_SIZE;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200700 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100701 le16_to_cpu(dn->compr_type));
702 if (err || len != out_len)
703 goto dump;
704
705 /*
706 * Data length can be less than a full block, even for blocks that are
707 * not the last in the file (e.g., as a result of making a hole and
708 * appending data). Ensure that the remainder is zeroed out.
709 */
710 if (len < UBIFS_BLOCK_SIZE)
711 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
712
713 return 0;
714
715dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200716 ubifs_err(c, "bad data node (block %u, inode %lu)",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100717 block, inode->i_ino);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200718 ubifs_dump_node(c, dn);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100719 return -EINVAL;
720}
721
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100722static int do_readpage(struct ubifs_info *c, struct inode *inode,
723 struct page *page, int last_block_size)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100724{
725 void *addr;
726 int err = 0, i;
727 unsigned int block, beyond;
728 struct ubifs_data_node *dn;
729 loff_t i_size = inode->i_size;
730
731 dbg_gen("ino %lu, pg %lu, i_size %lld",
732 inode->i_ino, page->index, i_size);
733
734 addr = kmap(page);
735
736 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
737 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
738 if (block >= beyond) {
739 /* Reading beyond inode */
740 memset(addr, 0, PAGE_CACHE_SIZE);
741 goto out;
742 }
743
744 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mack165f9852009-06-04 19:44:12 +0200745 if (!dn)
746 return -ENOMEM;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100747
748 i = 0;
749 while (1) {
750 int ret;
751
752 if (block >= beyond) {
753 /* Reading beyond inode */
754 err = -ENOENT;
755 memset(addr, 0, UBIFS_BLOCK_SIZE);
756 } else {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100757 /*
758 * Reading last block? Make sure to not write beyond
759 * the requested size in the destination buffer.
760 */
761 if (((block + 1) == beyond) || last_block_size) {
762 void *buff;
763 int dlen;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100764
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100765 /*
766 * We need to buffer the data locally for the
767 * last block. This is to not pad the
768 * destination area to a multiple of
769 * UBIFS_BLOCK_SIZE.
770 */
Marcel Ziswiler45196682015-08-18 13:06:37 +0200771 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100772 if (!buff) {
773 printf("%s: Error, malloc fails!\n",
774 __func__);
775 err = -ENOMEM;
776 break;
777 }
778
779 /* Read block-size into temp buffer */
780 ret = read_block(inode, buff, block, dn);
781 if (ret) {
782 err = ret;
783 if (err != -ENOENT) {
784 free(buff);
785 break;
786 }
787 }
788
789 if (last_block_size)
790 dlen = last_block_size;
Pali Rohár339f6522022-05-17 22:45:28 +0200791 else if (ret)
792 dlen = UBIFS_BLOCK_SIZE;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100793 else
794 dlen = le32_to_cpu(dn->size);
795
796 /* Now copy required size back to dest */
797 memcpy(addr, buff, dlen);
798
799 free(buff);
800 } else {
801 ret = read_block(inode, addr, block, dn);
802 if (ret) {
803 err = ret;
804 if (err != -ENOENT)
805 break;
806 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100807 }
808 }
809 if (++i >= UBIFS_BLOCKS_PER_PAGE)
810 break;
811 block += 1;
812 addr += UBIFS_BLOCK_SIZE;
813 }
814 if (err) {
815 if (err == -ENOENT) {
816 /* Not found, so it must be a hole */
817 dbg_gen("hole");
818 goto out_free;
819 }
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200820 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100821 page->index, inode->i_ino, err);
822 goto error;
823 }
824
825out_free:
826 kfree(dn);
827out:
828 return 0;
829
830error:
831 kfree(dn);
832 return err;
833}
834
Hans de Goedead157492015-09-17 18:46:56 -0400835int ubifs_read(const char *filename, void *buf, loff_t offset,
836 loff_t size, loff_t *actread)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100837{
838 struct ubifs_info *c = ubifs_sb->s_fs_info;
839 unsigned long inum;
840 struct inode *inode;
841 struct page page;
842 int err = 0;
843 int i;
844 int count;
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100845 int last_block_size = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100846
Hans de Goedead157492015-09-17 18:46:56 -0400847 *actread = 0;
848
849 if (offset & (PAGE_SIZE - 1)) {
Vagrant Cascadian13819012016-10-23 20:45:16 -0700850 printf("ubifs: Error offset must be a multiple of %d\n",
Hans de Goedead157492015-09-17 18:46:56 -0400851 PAGE_SIZE);
852 return -1;
853 }
854
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100855 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom9d7952e2009-09-15 09:53:29 +0200856 /* ubifs_findfile will resolve symlinks, so we know that we get
857 * the real file here */
Hans de Goedead157492015-09-17 18:46:56 -0400858 inum = ubifs_findfile(ubifs_sb, (char *)filename);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100859 if (!inum) {
860 err = -1;
861 goto out;
862 }
863
864 /*
865 * Read file inode
866 */
867 inode = ubifs_iget(ubifs_sb, inum);
868 if (IS_ERR(inode)) {
869 printf("%s: Error reading inode %ld!\n", __func__, inum);
870 err = PTR_ERR(inode);
871 goto out;
872 }
873
Hans de Goedead157492015-09-17 18:46:56 -0400874 if (offset > inode->i_size) {
875 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
876 offset, size);
877 err = -1;
878 goto put_inode;
879 }
880
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100881 /*
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100882 * If no size was specified or if size bigger than filesize
883 * set size to filesize
884 */
Hans de Goedead157492015-09-17 18:46:56 -0400885 if ((size == 0) || (size > (inode->i_size - offset)))
886 size = inode->i_size - offset;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100887
888 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100889
Hans de Goedead157492015-09-17 18:46:56 -0400890 page.addr = buf;
891 page.index = offset / PAGE_SIZE;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100892 page.inode = inode;
893 for (i = 0; i < count; i++) {
Stefan Roeseb1a14f82010-11-01 17:28:00 +0100894 /*
895 * Make sure to not read beyond the requested size
896 */
897 if (((i + 1) == count) && (size < inode->i_size))
898 last_block_size = size - (i * PAGE_SIZE);
899
900 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100901 if (err)
902 break;
903
904 page.addr += PAGE_SIZE;
905 page.index++;
906 }
907
Hans de Goedead157492015-09-17 18:46:56 -0400908 if (err) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100909 printf("Error reading file '%s'\n", filename);
Hans de Goedead157492015-09-17 18:46:56 -0400910 *actread = i * PAGE_SIZE;
911 } else {
912 *actread = size;
Bastian Ruppert46d72742011-09-05 03:03:57 +0000913 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100914
Hans de Goedead157492015-09-17 18:46:56 -0400915put_inode:
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100916 ubifs_iput(inode);
917
918out:
919 ubi_close_volume(c->ubi);
920 return err;
921}
Hans de Goedead157492015-09-17 18:46:56 -0400922
Hans de Goede29cc5bc2015-09-17 18:46:57 -0400923void ubifs_close(void)
924{
925}
926
Hans de Goedead157492015-09-17 18:46:56 -0400927/* Compat wrappers for common/cmd_ubifs.c */
Ben Dooksb46cec42023-06-06 09:23:28 +0100928int ubifs_load(char *filename, unsigned long addr, u32 size)
Hans de Goedead157492015-09-17 18:46:56 -0400929{
930 loff_t actread;
931 int err;
932
Ben Dooksb46cec42023-06-06 09:23:28 +0100933 printf("Loading file '%s' to addr 0x%08lx...\n", filename, addr);
Hans de Goedead157492015-09-17 18:46:56 -0400934
Siva Durga Prasad Paladugu34cc30a2017-05-30 14:29:06 +0200935 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
Hans de Goedead157492015-09-17 18:46:56 -0400936 if (err == 0) {
Simon Glass018f5302017-08-03 12:22:10 -0600937 env_set_hex("filesize", actread);
Hans de Goedead157492015-09-17 18:46:56 -0400938 printf("Done\n");
939 }
940
941 return err;
942}
943
944void uboot_ubifs_umount(void)
945{
946 if (ubifs_sb) {
947 printf("Unmounting UBIFS volume %s!\n",
948 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
949 ubifs_umount(ubifs_sb->s_fs_info);
950 ubifs_sb = NULL;
951 }
952}