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
8 * Stefan Roese, DENX Software Engineering, sr@denx.de.
10 * Authors: Artem Bityutskiy (Битюцкий Артём)
18 #include <u-boot/zlib.h>
20 #include <linux/err.h>
21 #include <linux/lzo.h>
23 DECLARE_GLOBAL_DATA_PTR;
28 * We need a wrapper for zunzip() because the parameters are
29 * incompatible with the lzo decompressor.
31 static int gzip_decompress(const unsigned char *in, size_t in_len,
32 unsigned char *out, size_t *out_len)
34 return zunzip(out, *out_len, (unsigned char *)in,
35 (unsigned long *)out_len, 0, 0);
38 /* Fake description object for the "none" compressor */
39 static struct ubifs_compressor none_compr = {
40 .compr_type = UBIFS_COMPR_NONE,
46 static struct ubifs_compressor lzo_compr = {
47 .compr_type = UBIFS_COMPR_LZO,
49 .comp_mutex = &lzo_mutex,
53 .decompress = lzo1x_decompress_safe,
56 static struct ubifs_compressor zlib_compr = {
57 .compr_type = UBIFS_COMPR_ZLIB,
59 .comp_mutex = &deflate_mutex,
60 .decomp_mutex = &inflate_mutex,
63 .capi_name = "deflate",
64 .decompress = gzip_decompress,
67 /* All UBIFS compressors */
68 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
75 * kmemdup - duplicate region of memory
77 * @src: memory region to duplicate
78 * @len: memory region length
79 * @gfp: GFP mask to use
81 void *kmemdup(const void *src, size_t len, gfp_t gfp)
85 p = kmalloc(len, gfp);
95 static inline struct crypto_comp
96 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
98 struct ubifs_compressor *comp;
99 struct crypto_comp *ptr;
102 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
103 while (i < UBIFS_COMPR_TYPES_CNT) {
104 comp = ubifs_compressors[i];
109 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
115 if (i >= UBIFS_COMPR_TYPES_CNT) {
116 dbg_gen("invalid compression type %s", alg_name);
123 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
124 const u8 *src, unsigned int slen, u8 *dst,
127 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
129 size_t tmp_len = *dlen;
131 if (compr->compr_type == UBIFS_COMPR_NONE) {
132 memcpy(dst, src, slen);
137 err = compr->decompress(src, slen, dst, &tmp_len);
139 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
140 "error %d", slen, compr->name, err);
148 /* from shrinker.c */
150 /* Global clean znode counter (for all mounted UBIFS instances) */
151 atomic_long_t ubifs_clean_zn_cnt;
156 * ubifs_decompress - decompress data.
157 * @in_buf: data to decompress
158 * @in_len: length of the data to decompress
159 * @out_buf: output buffer where decompressed data should
160 * @out_len: output length is returned here
161 * @compr_type: type of compression
163 * This function decompresses data from buffer @in_buf into buffer @out_buf.
164 * The length of the uncompressed data is returned in @out_len. This functions
165 * returns %0 on success or a negative error code on failure.
167 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
168 int in_len, void *out_buf, int *out_len, int compr_type)
171 struct ubifs_compressor *compr;
173 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
174 ubifs_err(c, "invalid compression type %d", compr_type);
178 compr = ubifs_compressors[compr_type];
180 if (unlikely(!compr->capi_name)) {
181 ubifs_err(c, "%s compression is not compiled in", compr->name);
185 if (compr_type == UBIFS_COMPR_NONE) {
186 memcpy(out_buf, in_buf, in_len);
191 if (compr->decomp_mutex)
192 mutex_lock(compr->decomp_mutex);
193 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
194 (unsigned int *)out_len);
195 if (compr->decomp_mutex)
196 mutex_unlock(compr->decomp_mutex);
198 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
199 " error %d", in_len, compr->name, err);
205 * compr_init - initialize a compressor.
206 * @compr: compressor description object
208 * This function initializes the requested compressor and returns zero in case
209 * of success or a negative error code in case of failure.
211 static int __init compr_init(struct ubifs_compressor *compr)
213 ubifs_compressors[compr->compr_type] = compr;
215 #ifdef CONFIG_NEEDS_MANUAL_RELOC
216 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
217 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
218 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
221 if (compr->capi_name) {
222 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
223 if (IS_ERR(compr->cc)) {
224 dbg_gen("cannot initialize compressor %s,"
225 " error %ld", compr->name,
227 return PTR_ERR(compr->cc);
235 * ubifs_compressors_init - initialize UBIFS compressors.
237 * This function initializes the compressor which were compiled in. Returns
238 * zero in case of success and a negative error code in case of failure.
240 int __init ubifs_compressors_init(void)
244 err = compr_init(&lzo_compr);
248 err = compr_init(&zlib_compr);
252 err = compr_init(&none_compr);
263 static int filldir(struct ubifs_info *c, const char *name, int namlen,
264 u64 ino, unsigned int d_type)
270 case UBIFS_ITYPE_REG:
273 case UBIFS_ITYPE_DIR:
276 case UBIFS_ITYPE_LNK:
284 inode = ubifs_iget(c->vfs_sb, ino);
286 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
287 __func__, ino, inode);
290 ctime_r((time_t *)&inode->i_mtime, filetime);
291 printf("%9lld %24.24s ", inode->i_size, filetime);
296 printf("%s\n", name);
301 static int ubifs_printdir(struct file *file, void *dirent)
306 struct ubifs_dent_node *dent;
307 struct inode *dir = file->f_path.dentry->d_inode;
308 struct ubifs_info *c = dir->i_sb->s_fs_info;
310 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
312 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
314 * The directory was seek'ed to a senseless position or there
315 * are no more entries.
319 if (file->f_pos == 1) {
320 /* Find the first entry in TNC and save it */
321 lowest_dent_key(c, &key, dir->i_ino);
323 dent = ubifs_tnc_next_ent(c, &key, &nm);
329 file->f_pos = key_hash_flash(c, &dent->key);
330 file->private_data = dent;
333 dent = file->private_data;
336 * The directory was seek'ed to and is now readdir'ed.
337 * Find the entry corresponding to @file->f_pos or the
340 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
342 dent = ubifs_tnc_next_ent(c, &key, &nm);
347 file->f_pos = key_hash_flash(c, &dent->key);
348 file->private_data = dent;
352 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
353 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
354 key_hash_flash(c, &dent->key));
356 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
359 nm.len = le16_to_cpu(dent->nlen);
360 over = filldir(c, (char *)dent->name, nm.len,
361 le64_to_cpu(dent->inum), dent->type);
365 /* Switch to the next entry */
366 key_read(c, &dent->key, &key);
367 nm.name = (char *)dent->name;
368 dent = ubifs_tnc_next_ent(c, &key, &nm);
374 kfree(file->private_data);
375 file->f_pos = key_hash_flash(c, &dent->key);
376 file->private_data = dent;
381 if (err != -ENOENT) {
382 ubifs_err(c, "cannot find next direntry, error %d", err);
386 kfree(file->private_data);
387 file->private_data = NULL;
392 static int ubifs_finddir(struct super_block *sb, char *dirname,
393 unsigned long root_inum, unsigned long *inum)
398 struct ubifs_dent_node *dent;
399 struct ubifs_info *c;
401 struct dentry *dentry;
405 file = kzalloc(sizeof(struct file), 0);
406 dentry = kzalloc(sizeof(struct dentry), 0);
407 dir = kzalloc(sizeof(struct inode), 0);
408 if (!file || !dentry || !dir) {
409 printf("%s: Error, no memory for malloc!\n", __func__);
415 file->f_path.dentry = dentry;
416 file->f_path.dentry->d_parent = dentry;
417 file->f_path.dentry->d_inode = dir;
418 file->f_path.dentry->d_inode->i_ino = root_inum;
421 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
423 /* Find the first entry in TNC and save it */
424 lowest_dent_key(c, &key, dir->i_ino);
426 dent = ubifs_tnc_next_ent(c, &key, &nm);
432 file->f_pos = key_hash_flash(c, &dent->key);
433 file->private_data = dent;
436 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
437 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
438 key_hash_flash(c, &dent->key));
440 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
443 nm.len = le16_to_cpu(dent->nlen);
444 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
445 (strlen(dirname) == nm.len)) {
446 *inum = le64_to_cpu(dent->inum);
451 /* Switch to the next entry */
452 key_read(c, &dent->key, &key);
453 nm.name = (char *)dent->name;
454 dent = ubifs_tnc_next_ent(c, &key, &nm);
460 kfree(file->private_data);
461 file->f_pos = key_hash_flash(c, &dent->key);
462 file->private_data = dent;
468 dbg_gen("cannot find next direntry, error %d", err);
471 kfree(file->private_data);
479 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
484 char symlinkpath[128];
486 unsigned long root_inum = 1;
488 int symlink_count = 0; /* Don't allow symlink recursion */
491 strcpy(fpath, filename);
493 /* Remove all leading slashes */
498 * Handle root-direcoty ('/')
501 if (!name || *name == '\0')
506 struct ubifs_inode *ui;
508 /* Extract the actual part from the pathname. */
509 next = strchr(name, '/');
511 /* Remove all leading slashes. */
516 ret = ubifs_finddir(sb, name, root_inum, &inum);
519 inode = ubifs_iget(sb, inum);
523 ui = ubifs_inode(inode);
525 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
528 /* We have some sort of symlink recursion, bail out */
529 if (symlink_count++ > 8) {
530 printf("Symlink recursion, aborting\n");
533 memcpy(link_name, ui->data, ui->data_len);
534 link_name[ui->data_len] = '\0';
536 if (link_name[0] == '/') {
537 /* Absolute path, redo everything without
538 * the leading slash */
539 next = name = link_name + 1;
543 /* Relative to cur dir */
544 sprintf(buf, "%s/%s",
545 link_name, next == NULL ? "" : next);
546 memcpy(symlinkpath, buf, sizeof(buf));
547 next = name = symlinkpath;
552 * Check if directory with this name exists
555 /* Found the node! */
556 if (!next || *next == '\0')
566 int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
569 debug("UBIFS cannot be used with normal block devices\n");
574 * Should never happen since blk_get_device_part_str() already checks
575 * this, but better safe then sorry.
577 if (!ubifs_is_mounted()) {
578 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
585 int ubifs_ls(const char *filename)
587 struct ubifs_info *c = ubifs_sb->s_fs_info;
589 struct dentry *dentry;
595 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
596 inum = ubifs_findfile(ubifs_sb, (char *)filename);
602 file = kzalloc(sizeof(struct file), 0);
603 dentry = kzalloc(sizeof(struct dentry), 0);
604 dir = kzalloc(sizeof(struct inode), 0);
605 if (!file || !dentry || !dir) {
606 printf("%s: Error, no memory for malloc!\n", __func__);
611 dir->i_sb = ubifs_sb;
612 file->f_path.dentry = dentry;
613 file->f_path.dentry->d_parent = dentry;
614 file->f_path.dentry->d_inode = dir;
615 file->f_path.dentry->d_inode->i_ino = inum;
617 file->private_data = NULL;
618 ubifs_printdir(file, dirent);
629 ubi_close_volume(c->ubi);
633 int ubifs_exists(const char *filename)
635 struct ubifs_info *c = ubifs_sb->s_fs_info;
638 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
639 inum = ubifs_findfile(ubifs_sb, (char *)filename);
640 ubi_close_volume(c->ubi);
645 int ubifs_size(const char *filename, loff_t *size)
647 struct ubifs_info *c = ubifs_sb->s_fs_info;
652 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
654 inum = ubifs_findfile(ubifs_sb, (char *)filename);
660 inode = ubifs_iget(ubifs_sb, inum);
662 printf("%s: Error reading inode %ld!\n", __func__, inum);
663 err = PTR_ERR(inode);
667 *size = inode->i_size;
671 ubi_close_volume(c->ubi);
681 static inline void *kmap(struct page *page)
686 static int read_block(struct inode *inode, void *addr, unsigned int block,
687 struct ubifs_data_node *dn)
689 struct ubifs_info *c = inode->i_sb->s_fs_info;
690 int err, len, out_len;
694 data_key_init(c, &key, inode->i_ino, block);
695 err = ubifs_tnc_lookup(c, &key, dn);
698 /* Not found, so it must be a hole */
699 memset(addr, 0, UBIFS_BLOCK_SIZE);
703 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
705 len = le32_to_cpu(dn->size);
706 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
709 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
710 out_len = UBIFS_BLOCK_SIZE;
711 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
712 le16_to_cpu(dn->compr_type));
713 if (err || len != out_len)
717 * Data length can be less than a full block, even for blocks that are
718 * not the last in the file (e.g., as a result of making a hole and
719 * appending data). Ensure that the remainder is zeroed out.
721 if (len < UBIFS_BLOCK_SIZE)
722 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
727 ubifs_err(c, "bad data node (block %u, inode %lu)",
728 block, inode->i_ino);
729 ubifs_dump_node(c, dn);
733 static int do_readpage(struct ubifs_info *c, struct inode *inode,
734 struct page *page, int last_block_size)
738 unsigned int block, beyond;
739 struct ubifs_data_node *dn;
740 loff_t i_size = inode->i_size;
742 dbg_gen("ino %lu, pg %lu, i_size %lld",
743 inode->i_ino, page->index, i_size);
747 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
748 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
749 if (block >= beyond) {
750 /* Reading beyond inode */
751 memset(addr, 0, PAGE_CACHE_SIZE);
755 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
763 if (block >= beyond) {
764 /* Reading beyond inode */
766 memset(addr, 0, UBIFS_BLOCK_SIZE);
769 * Reading last block? Make sure to not write beyond
770 * the requested size in the destination buffer.
772 if (((block + 1) == beyond) || last_block_size) {
777 * We need to buffer the data locally for the
778 * last block. This is to not pad the
779 * destination area to a multiple of
782 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
784 printf("%s: Error, malloc fails!\n",
790 /* Read block-size into temp buffer */
791 ret = read_block(inode, buff, block, dn);
794 if (err != -ENOENT) {
801 dlen = last_block_size;
803 dlen = le32_to_cpu(dn->size);
805 /* Now copy required size back to dest */
806 memcpy(addr, buff, dlen);
810 ret = read_block(inode, addr, block, dn);
818 if (++i >= UBIFS_BLOCKS_PER_PAGE)
821 addr += UBIFS_BLOCK_SIZE;
824 if (err == -ENOENT) {
825 /* Not found, so it must be a hole */
829 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
830 page->index, inode->i_ino, err);
844 int ubifs_read(const char *filename, void *buf, loff_t offset,
845 loff_t size, loff_t *actread)
847 struct ubifs_info *c = ubifs_sb->s_fs_info;
854 int last_block_size = 0;
858 if (offset & (PAGE_SIZE - 1)) {
859 printf("ubifs: Error offset must be a multiple of %d\n",
864 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
865 /* ubifs_findfile will resolve symlinks, so we know that we get
866 * the real file here */
867 inum = ubifs_findfile(ubifs_sb, (char *)filename);
876 inode = ubifs_iget(ubifs_sb, inum);
878 printf("%s: Error reading inode %ld!\n", __func__, inum);
879 err = PTR_ERR(inode);
883 if (offset > inode->i_size) {
884 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
891 * If no size was specified or if size bigger than filesize
892 * set size to filesize
894 if ((size == 0) || (size > (inode->i_size - offset)))
895 size = inode->i_size - offset;
897 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
900 page.index = offset / PAGE_SIZE;
902 for (i = 0; i < count; i++) {
904 * Make sure to not read beyond the requested size
906 if (((i + 1) == count) && (size < inode->i_size))
907 last_block_size = size - (i * PAGE_SIZE);
909 err = do_readpage(c, inode, &page, last_block_size);
913 page.addr += PAGE_SIZE;
918 printf("Error reading file '%s'\n", filename);
919 *actread = i * PAGE_SIZE;
928 ubi_close_volume(c->ubi);
932 void ubifs_close(void)
936 /* Compat wrappers for common/cmd_ubifs.c */
937 int ubifs_load(char *filename, u32 addr, u32 size)
942 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
944 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
946 env_set_hex("filesize", actread);
953 void uboot_ubifs_umount(void)
956 printf("Unmounting UBIFS volume %s!\n",
957 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
958 ubifs_umount(ubifs_sb->s_fs_info);