1 // SPDX-License-Identifier: GPL-2.0
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * (C) Copyright 2008-2010
10 * Authors: Artem Bityutskiy (Битюцкий Артём)
22 #include <dm/devres.h>
23 #include <u-boot/zlib.h>
25 #include <linux/compat.h>
26 #include <linux/err.h>
27 #include <linux/lzo.h>
29 DECLARE_GLOBAL_DATA_PTR;
34 * We need a wrapper for zunzip() because the parameters are
35 * incompatible with the lzo decompressor.
37 static int gzip_decompress(const unsigned char *in, size_t in_len,
38 unsigned char *out, size_t *out_len)
40 return zunzip(out, *out_len, (unsigned char *)in,
41 (unsigned long *)out_len, 0, 0);
44 /* Fake description object for the "none" compressor */
45 static struct ubifs_compressor none_compr = {
46 .compr_type = UBIFS_COMPR_NONE,
52 static struct ubifs_compressor lzo_compr = {
53 .compr_type = UBIFS_COMPR_LZO,
55 .comp_mutex = &lzo_mutex,
59 .decompress = lzo1x_decompress_safe,
62 static struct ubifs_compressor zlib_compr = {
63 .compr_type = UBIFS_COMPR_ZLIB,
65 .comp_mutex = &deflate_mutex,
66 .decomp_mutex = &inflate_mutex,
69 .capi_name = "deflate",
70 .decompress = gzip_decompress,
73 /* All UBIFS compressors */
74 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
83 static inline struct crypto_comp
84 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
86 struct ubifs_compressor *comp;
87 struct crypto_comp *ptr;
90 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
91 while (i < UBIFS_COMPR_TYPES_CNT) {
92 comp = ubifs_compressors[i];
97 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
103 if (i >= UBIFS_COMPR_TYPES_CNT) {
104 dbg_gen("invalid compression type %s", alg_name);
111 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
112 const u8 *src, unsigned int slen, u8 *dst,
115 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
117 size_t tmp_len = *dlen;
119 if (compr->compr_type == UBIFS_COMPR_NONE) {
120 memcpy(dst, src, slen);
125 err = compr->decompress(src, slen, dst, &tmp_len);
127 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
128 "error %d", slen, compr->name, err);
136 /* from shrinker.c */
138 /* Global clean znode counter (for all mounted UBIFS instances) */
139 atomic_long_t ubifs_clean_zn_cnt;
144 * ubifs_decompress - decompress data.
145 * @in_buf: data to decompress
146 * @in_len: length of the data to decompress
147 * @out_buf: output buffer where decompressed data should
148 * @out_len: output length is returned here
149 * @compr_type: type of compression
151 * This function decompresses data from buffer @in_buf into buffer @out_buf.
152 * The length of the uncompressed data is returned in @out_len. This functions
153 * returns %0 on success or a negative error code on failure.
155 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
156 int in_len, void *out_buf, int *out_len, int compr_type)
159 struct ubifs_compressor *compr;
161 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
162 ubifs_err(c, "invalid compression type %d", compr_type);
166 compr = ubifs_compressors[compr_type];
168 if (unlikely(!compr->capi_name)) {
169 ubifs_err(c, "%s compression is not compiled in", compr->name);
173 if (compr_type == UBIFS_COMPR_NONE) {
174 memcpy(out_buf, in_buf, in_len);
179 if (compr->decomp_mutex)
180 mutex_lock(compr->decomp_mutex);
181 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
182 (unsigned int *)out_len);
183 if (compr->decomp_mutex)
184 mutex_unlock(compr->decomp_mutex);
186 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
187 " error %d", in_len, compr->name, err);
193 * compr_init - initialize a compressor.
194 * @compr: compressor description object
196 * This function initializes the requested compressor and returns zero in case
197 * of success or a negative error code in case of failure.
199 static int __init compr_init(struct ubifs_compressor *compr)
201 ubifs_compressors[compr->compr_type] = compr;
203 #ifdef CONFIG_NEEDS_MANUAL_RELOC
204 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
205 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
206 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
209 if (compr->capi_name) {
210 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
211 if (IS_ERR(compr->cc)) {
212 dbg_gen("cannot initialize compressor %s,"
213 " error %ld", compr->name,
215 return PTR_ERR(compr->cc);
223 * ubifs_compressors_init - initialize UBIFS compressors.
225 * This function initializes the compressor which were compiled in. Returns
226 * zero in case of success and a negative error code in case of failure.
228 int __init ubifs_compressors_init(void)
232 err = compr_init(&lzo_compr);
236 err = compr_init(&zlib_compr);
240 err = compr_init(&none_compr);
251 static int filldir(struct ubifs_info *c, const char *name, int namlen,
252 u64 ino, unsigned int d_type)
258 case UBIFS_ITYPE_REG:
261 case UBIFS_ITYPE_DIR:
264 case UBIFS_ITYPE_LNK:
272 inode = ubifs_iget(c->vfs_sb, ino);
274 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
275 __func__, ino, inode);
278 ctime_r((time_t *)&inode->i_mtime, filetime);
279 printf("%9lld %24.24s ", inode->i_size, filetime);
284 printf("%s\n", name);
289 static int ubifs_printdir(struct file *file, void *dirent)
294 struct ubifs_dent_node *dent;
295 struct inode *dir = file->f_path.dentry->d_inode;
296 struct ubifs_info *c = dir->i_sb->s_fs_info;
298 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
300 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
302 * The directory was seek'ed to a senseless position or there
303 * are no more entries.
307 if (file->f_pos == 1) {
308 /* Find the first entry in TNC and save it */
309 lowest_dent_key(c, &key, dir->i_ino);
311 dent = ubifs_tnc_next_ent(c, &key, &nm);
317 file->f_pos = key_hash_flash(c, &dent->key);
318 file->private_data = dent;
321 dent = file->private_data;
324 * The directory was seek'ed to and is now readdir'ed.
325 * Find the entry corresponding to @file->f_pos or the
328 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
330 dent = ubifs_tnc_next_ent(c, &key, &nm);
335 file->f_pos = key_hash_flash(c, &dent->key);
336 file->private_data = dent;
340 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
341 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
342 key_hash_flash(c, &dent->key));
344 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
347 nm.len = le16_to_cpu(dent->nlen);
348 over = filldir(c, (char *)dent->name, nm.len,
349 le64_to_cpu(dent->inum), dent->type);
353 /* Switch to the next entry */
354 key_read(c, &dent->key, &key);
355 nm.name = (char *)dent->name;
356 dent = ubifs_tnc_next_ent(c, &key, &nm);
362 kfree(file->private_data);
363 file->f_pos = key_hash_flash(c, &dent->key);
364 file->private_data = dent;
369 if (err != -ENOENT) {
370 ubifs_err(c, "cannot find next direntry, error %d", err);
374 kfree(file->private_data);
375 file->private_data = NULL;
380 static int ubifs_finddir(struct super_block *sb, char *dirname,
381 unsigned long root_inum, unsigned long *inum)
386 struct ubifs_dent_node *dent;
387 struct ubifs_info *c;
389 struct dentry *dentry;
393 file = kzalloc(sizeof(struct file), 0);
394 dentry = kzalloc(sizeof(struct dentry), 0);
395 dir = kzalloc(sizeof(struct inode), 0);
396 if (!file || !dentry || !dir) {
397 printf("%s: Error, no memory for malloc!\n", __func__);
403 file->f_path.dentry = dentry;
404 file->f_path.dentry->d_parent = dentry;
405 file->f_path.dentry->d_inode = dir;
406 file->f_path.dentry->d_inode->i_ino = root_inum;
409 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
411 /* Find the first entry in TNC and save it */
412 lowest_dent_key(c, &key, dir->i_ino);
414 dent = ubifs_tnc_next_ent(c, &key, &nm);
420 file->f_pos = key_hash_flash(c, &dent->key);
421 file->private_data = dent;
424 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
425 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
426 key_hash_flash(c, &dent->key));
428 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
431 nm.len = le16_to_cpu(dent->nlen);
432 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
433 (strlen(dirname) == nm.len)) {
434 *inum = le64_to_cpu(dent->inum);
439 /* Switch to the next entry */
440 key_read(c, &dent->key, &key);
441 nm.name = (char *)dent->name;
442 dent = ubifs_tnc_next_ent(c, &key, &nm);
448 kfree(file->private_data);
449 file->f_pos = key_hash_flash(c, &dent->key);
450 file->private_data = dent;
456 dbg_gen("cannot find next direntry, error %d", err);
459 kfree(file->private_data);
467 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
472 char symlinkpath[128];
474 unsigned long root_inum = 1;
476 int symlink_count = 0; /* Don't allow symlink recursion */
479 strcpy(fpath, filename);
481 /* Remove all leading slashes */
486 * Handle root-direcoty ('/')
489 if (!name || *name == '\0')
494 struct ubifs_inode *ui;
496 /* Extract the actual part from the pathname. */
497 next = strchr(name, '/');
499 /* Remove all leading slashes. */
504 ret = ubifs_finddir(sb, name, root_inum, &inum);
507 inode = ubifs_iget(sb, inum);
511 ui = ubifs_inode(inode);
513 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
516 /* We have some sort of symlink recursion, bail out */
517 if (symlink_count++ > 8) {
518 printf("Symlink recursion, aborting\n");
521 memcpy(link_name, ui->data, ui->data_len);
522 link_name[ui->data_len] = '\0';
524 if (link_name[0] == '/') {
525 /* Absolute path, redo everything without
526 * the leading slash */
527 next = name = link_name + 1;
531 /* Relative to cur dir */
532 sprintf(buf, "%s/%s",
533 link_name, next == NULL ? "" : next);
534 memcpy(symlinkpath, buf, sizeof(buf));
535 next = name = symlinkpath;
540 * Check if directory with this name exists
543 /* Found the node! */
544 if (!next || *next == '\0')
554 int ubifs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
557 debug("UBIFS cannot be used with normal block devices\n");
562 * Should never happen since blk_get_device_part_str() already checks
563 * this, but better safe then sorry.
565 if (!ubifs_is_mounted()) {
566 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
573 int ubifs_ls(const char *filename)
575 struct ubifs_info *c = ubifs_sb->s_fs_info;
577 struct dentry *dentry;
583 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
584 inum = ubifs_findfile(ubifs_sb, (char *)filename);
590 file = kzalloc(sizeof(struct file), 0);
591 dentry = kzalloc(sizeof(struct dentry), 0);
592 dir = kzalloc(sizeof(struct inode), 0);
593 if (!file || !dentry || !dir) {
594 printf("%s: Error, no memory for malloc!\n", __func__);
599 dir->i_sb = ubifs_sb;
600 file->f_path.dentry = dentry;
601 file->f_path.dentry->d_parent = dentry;
602 file->f_path.dentry->d_inode = dir;
603 file->f_path.dentry->d_inode->i_ino = inum;
605 file->private_data = NULL;
606 ubifs_printdir(file, dirent);
617 ubi_close_volume(c->ubi);
621 int ubifs_exists(const char *filename)
623 struct ubifs_info *c = ubifs_sb->s_fs_info;
626 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
627 inum = ubifs_findfile(ubifs_sb, (char *)filename);
628 ubi_close_volume(c->ubi);
633 int ubifs_size(const char *filename, loff_t *size)
635 struct ubifs_info *c = ubifs_sb->s_fs_info;
640 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
642 inum = ubifs_findfile(ubifs_sb, (char *)filename);
648 inode = ubifs_iget(ubifs_sb, inum);
650 printf("%s: Error reading inode %ld!\n", __func__, inum);
651 err = PTR_ERR(inode);
655 *size = inode->i_size;
659 ubi_close_volume(c->ubi);
669 static inline void *kmap(struct page *page)
674 static int read_block(struct inode *inode, void *addr, unsigned int block,
675 struct ubifs_data_node *dn)
677 struct ubifs_info *c = inode->i_sb->s_fs_info;
678 int err, len, out_len;
682 data_key_init(c, &key, inode->i_ino, block);
683 err = ubifs_tnc_lookup(c, &key, dn);
686 /* Not found, so it must be a hole */
687 memset(addr, 0, UBIFS_BLOCK_SIZE);
691 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
693 len = le32_to_cpu(dn->size);
694 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
697 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
698 out_len = UBIFS_BLOCK_SIZE;
699 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
700 le16_to_cpu(dn->compr_type));
701 if (err || len != out_len)
705 * Data length can be less than a full block, even for blocks that are
706 * not the last in the file (e.g., as a result of making a hole and
707 * appending data). Ensure that the remainder is zeroed out.
709 if (len < UBIFS_BLOCK_SIZE)
710 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
715 ubifs_err(c, "bad data node (block %u, inode %lu)",
716 block, inode->i_ino);
717 ubifs_dump_node(c, dn);
721 static int do_readpage(struct ubifs_info *c, struct inode *inode,
722 struct page *page, int last_block_size)
726 unsigned int block, beyond;
727 struct ubifs_data_node *dn;
728 loff_t i_size = inode->i_size;
730 dbg_gen("ino %lu, pg %lu, i_size %lld",
731 inode->i_ino, page->index, i_size);
735 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
736 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
737 if (block >= beyond) {
738 /* Reading beyond inode */
739 memset(addr, 0, PAGE_CACHE_SIZE);
743 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
751 if (block >= beyond) {
752 /* Reading beyond inode */
754 memset(addr, 0, UBIFS_BLOCK_SIZE);
757 * Reading last block? Make sure to not write beyond
758 * the requested size in the destination buffer.
760 if (((block + 1) == beyond) || last_block_size) {
765 * We need to buffer the data locally for the
766 * last block. This is to not pad the
767 * destination area to a multiple of
770 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
772 printf("%s: Error, malloc fails!\n",
778 /* Read block-size into temp buffer */
779 ret = read_block(inode, buff, block, dn);
782 if (err != -ENOENT) {
789 dlen = last_block_size;
791 dlen = le32_to_cpu(dn->size);
793 /* Now copy required size back to dest */
794 memcpy(addr, buff, dlen);
798 ret = read_block(inode, addr, block, dn);
806 if (++i >= UBIFS_BLOCKS_PER_PAGE)
809 addr += UBIFS_BLOCK_SIZE;
812 if (err == -ENOENT) {
813 /* Not found, so it must be a hole */
817 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
818 page->index, inode->i_ino, err);
832 int ubifs_read(const char *filename, void *buf, loff_t offset,
833 loff_t size, loff_t *actread)
835 struct ubifs_info *c = ubifs_sb->s_fs_info;
842 int last_block_size = 0;
846 if (offset & (PAGE_SIZE - 1)) {
847 printf("ubifs: Error offset must be a multiple of %d\n",
852 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
853 /* ubifs_findfile will resolve symlinks, so we know that we get
854 * the real file here */
855 inum = ubifs_findfile(ubifs_sb, (char *)filename);
864 inode = ubifs_iget(ubifs_sb, inum);
866 printf("%s: Error reading inode %ld!\n", __func__, inum);
867 err = PTR_ERR(inode);
871 if (offset > inode->i_size) {
872 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
879 * If no size was specified or if size bigger than filesize
880 * set size to filesize
882 if ((size == 0) || (size > (inode->i_size - offset)))
883 size = inode->i_size - offset;
885 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
888 page.index = offset / PAGE_SIZE;
890 for (i = 0; i < count; i++) {
892 * Make sure to not read beyond the requested size
894 if (((i + 1) == count) && (size < inode->i_size))
895 last_block_size = size - (i * PAGE_SIZE);
897 err = do_readpage(c, inode, &page, last_block_size);
901 page.addr += PAGE_SIZE;
906 printf("Error reading file '%s'\n", filename);
907 *actread = i * PAGE_SIZE;
916 ubi_close_volume(c->ubi);
920 void ubifs_close(void)
924 /* Compat wrappers for common/cmd_ubifs.c */
925 int ubifs_load(char *filename, u32 addr, u32 size)
930 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
932 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
934 env_set_hex("filesize", actread);
941 void uboot_ubifs_umount(void)
944 printf("Unmounting UBIFS volume %s!\n",
945 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
946 ubifs_umount(ubifs_sb->s_fs_info);