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 (Битюцкий Артём)
20 #include <dm/devres.h>
21 #include <u-boot/zlib.h>
23 #include <linux/compat.h>
24 #include <linux/err.h>
25 #include <linux/lzo.h>
27 DECLARE_GLOBAL_DATA_PTR;
32 * We need a wrapper for zunzip() because the parameters are
33 * incompatible with the lzo decompressor.
35 static int gzip_decompress(const unsigned char *in, size_t in_len,
36 unsigned char *out, size_t *out_len)
38 return zunzip(out, *out_len, (unsigned char *)in,
39 (unsigned long *)out_len, 0, 0);
42 /* Fake description object for the "none" compressor */
43 static struct ubifs_compressor none_compr = {
44 .compr_type = UBIFS_COMPR_NONE,
50 static struct ubifs_compressor lzo_compr = {
51 .compr_type = UBIFS_COMPR_LZO,
53 .comp_mutex = &lzo_mutex,
57 .decompress = lzo1x_decompress_safe,
60 static struct ubifs_compressor zlib_compr = {
61 .compr_type = UBIFS_COMPR_ZLIB,
63 .comp_mutex = &deflate_mutex,
64 .decomp_mutex = &inflate_mutex,
67 .capi_name = "deflate",
68 .decompress = gzip_decompress,
71 /* All UBIFS compressors */
72 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
81 static inline struct crypto_comp
82 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
84 struct ubifs_compressor *comp;
85 struct crypto_comp *ptr;
88 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
89 while (i < UBIFS_COMPR_TYPES_CNT) {
90 comp = ubifs_compressors[i];
95 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
101 if (i >= UBIFS_COMPR_TYPES_CNT) {
102 dbg_gen("invalid compression type %s", alg_name);
109 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
110 const u8 *src, unsigned int slen, u8 *dst,
113 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
115 size_t tmp_len = *dlen;
117 if (compr->compr_type == UBIFS_COMPR_NONE) {
118 memcpy(dst, src, slen);
123 err = compr->decompress(src, slen, dst, &tmp_len);
125 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
126 "error %d", slen, compr->name, err);
134 /* from shrinker.c */
136 /* Global clean znode counter (for all mounted UBIFS instances) */
137 atomic_long_t ubifs_clean_zn_cnt;
142 * ubifs_decompress - decompress data.
143 * @in_buf: data to decompress
144 * @in_len: length of the data to decompress
145 * @out_buf: output buffer where decompressed data should
146 * @out_len: output length is returned here
147 * @compr_type: type of compression
149 * This function decompresses data from buffer @in_buf into buffer @out_buf.
150 * The length of the uncompressed data is returned in @out_len. This functions
151 * returns %0 on success or a negative error code on failure.
153 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
154 int in_len, void *out_buf, int *out_len, int compr_type)
157 struct ubifs_compressor *compr;
159 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
160 ubifs_err(c, "invalid compression type %d", compr_type);
164 compr = ubifs_compressors[compr_type];
166 if (unlikely(!compr->capi_name)) {
167 ubifs_err(c, "%s compression is not compiled in", compr->name);
171 if (compr_type == UBIFS_COMPR_NONE) {
172 memcpy(out_buf, in_buf, in_len);
177 if (compr->decomp_mutex)
178 mutex_lock(compr->decomp_mutex);
179 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
180 (unsigned int *)out_len);
181 if (compr->decomp_mutex)
182 mutex_unlock(compr->decomp_mutex);
184 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
185 " error %d", in_len, compr->name, err);
191 * compr_init - initialize a compressor.
192 * @compr: compressor description object
194 * This function initializes the requested compressor and returns zero in case
195 * of success or a negative error code in case of failure.
197 static int __init compr_init(struct ubifs_compressor *compr)
199 ubifs_compressors[compr->compr_type] = compr;
201 #ifdef CONFIG_NEEDS_MANUAL_RELOC
202 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
203 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
204 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
207 if (compr->capi_name) {
208 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
209 if (IS_ERR(compr->cc)) {
210 dbg_gen("cannot initialize compressor %s,"
211 " error %ld", compr->name,
213 return PTR_ERR(compr->cc);
221 * ubifs_compressors_init - initialize UBIFS compressors.
223 * This function initializes the compressor which were compiled in. Returns
224 * zero in case of success and a negative error code in case of failure.
226 int __init ubifs_compressors_init(void)
230 err = compr_init(&lzo_compr);
234 err = compr_init(&zlib_compr);
238 err = compr_init(&none_compr);
249 static int filldir(struct ubifs_info *c, const char *name, int namlen,
250 u64 ino, unsigned int d_type)
256 case UBIFS_ITYPE_REG:
259 case UBIFS_ITYPE_DIR:
262 case UBIFS_ITYPE_LNK:
270 inode = ubifs_iget(c->vfs_sb, ino);
272 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
273 __func__, ino, inode);
276 ctime_r((time_t *)&inode->i_mtime, filetime);
277 printf("%9lld %24.24s ", inode->i_size, filetime);
282 printf("%s\n", name);
287 static int ubifs_printdir(struct file *file, void *dirent)
292 struct ubifs_dent_node *dent;
293 struct inode *dir = file->f_path.dentry->d_inode;
294 struct ubifs_info *c = dir->i_sb->s_fs_info;
296 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
298 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
300 * The directory was seek'ed to a senseless position or there
301 * are no more entries.
305 if (file->f_pos == 1) {
306 /* Find the first entry in TNC and save it */
307 lowest_dent_key(c, &key, dir->i_ino);
309 dent = ubifs_tnc_next_ent(c, &key, &nm);
315 file->f_pos = key_hash_flash(c, &dent->key);
316 file->private_data = dent;
319 dent = file->private_data;
322 * The directory was seek'ed to and is now readdir'ed.
323 * Find the entry corresponding to @file->f_pos or the
326 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
328 dent = ubifs_tnc_next_ent(c, &key, &nm);
333 file->f_pos = key_hash_flash(c, &dent->key);
334 file->private_data = dent;
338 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
339 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
340 key_hash_flash(c, &dent->key));
342 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
345 nm.len = le16_to_cpu(dent->nlen);
346 over = filldir(c, (char *)dent->name, nm.len,
347 le64_to_cpu(dent->inum), dent->type);
351 /* Switch to the next entry */
352 key_read(c, &dent->key, &key);
353 nm.name = (char *)dent->name;
354 dent = ubifs_tnc_next_ent(c, &key, &nm);
360 kfree(file->private_data);
361 file->f_pos = key_hash_flash(c, &dent->key);
362 file->private_data = dent;
367 if (err != -ENOENT) {
368 ubifs_err(c, "cannot find next direntry, error %d", err);
372 kfree(file->private_data);
373 file->private_data = NULL;
378 static int ubifs_finddir(struct super_block *sb, char *dirname,
379 unsigned long root_inum, unsigned long *inum)
384 struct ubifs_dent_node *dent;
385 struct ubifs_info *c;
387 struct dentry *dentry;
391 file = kzalloc(sizeof(struct file), 0);
392 dentry = kzalloc(sizeof(struct dentry), 0);
393 dir = kzalloc(sizeof(struct inode), 0);
394 if (!file || !dentry || !dir) {
395 printf("%s: Error, no memory for malloc!\n", __func__);
401 file->f_path.dentry = dentry;
402 file->f_path.dentry->d_parent = dentry;
403 file->f_path.dentry->d_inode = dir;
404 file->f_path.dentry->d_inode->i_ino = root_inum;
407 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
409 /* Find the first entry in TNC and save it */
410 lowest_dent_key(c, &key, dir->i_ino);
412 dent = ubifs_tnc_next_ent(c, &key, &nm);
418 file->f_pos = key_hash_flash(c, &dent->key);
419 file->private_data = dent;
422 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
423 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
424 key_hash_flash(c, &dent->key));
426 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
429 nm.len = le16_to_cpu(dent->nlen);
430 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
431 (strlen(dirname) == nm.len)) {
432 *inum = le64_to_cpu(dent->inum);
437 /* Switch to the next entry */
438 key_read(c, &dent->key, &key);
439 nm.name = (char *)dent->name;
440 dent = ubifs_tnc_next_ent(c, &key, &nm);
446 kfree(file->private_data);
447 file->f_pos = key_hash_flash(c, &dent->key);
448 file->private_data = dent;
454 dbg_gen("cannot find next direntry, error %d", err);
457 kfree(file->private_data);
465 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
470 char symlinkpath[128];
472 unsigned long root_inum = 1;
474 int symlink_count = 0; /* Don't allow symlink recursion */
477 strcpy(fpath, filename);
479 /* Remove all leading slashes */
484 * Handle root-direcoty ('/')
487 if (!name || *name == '\0')
492 struct ubifs_inode *ui;
494 /* Extract the actual part from the pathname. */
495 next = strchr(name, '/');
497 /* Remove all leading slashes. */
502 ret = ubifs_finddir(sb, name, root_inum, &inum);
505 inode = ubifs_iget(sb, inum);
509 ui = ubifs_inode(inode);
511 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
514 /* We have some sort of symlink recursion, bail out */
515 if (symlink_count++ > 8) {
516 printf("Symlink recursion, aborting\n");
519 memcpy(link_name, ui->data, ui->data_len);
520 link_name[ui->data_len] = '\0';
522 if (link_name[0] == '/') {
523 /* Absolute path, redo everything without
524 * the leading slash */
525 next = name = link_name + 1;
529 /* Relative to cur dir */
530 sprintf(buf, "%s/%s",
531 link_name, next == NULL ? "" : next);
532 memcpy(symlinkpath, buf, sizeof(buf));
533 next = name = symlinkpath;
538 * Check if directory with this name exists
541 /* Found the node! */
542 if (!next || *next == '\0')
552 int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
555 debug("UBIFS cannot be used with normal block devices\n");
560 * Should never happen since blk_get_device_part_str() already checks
561 * this, but better safe then sorry.
563 if (!ubifs_is_mounted()) {
564 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
571 int ubifs_ls(const char *filename)
573 struct ubifs_info *c = ubifs_sb->s_fs_info;
575 struct dentry *dentry;
581 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
582 inum = ubifs_findfile(ubifs_sb, (char *)filename);
588 file = kzalloc(sizeof(struct file), 0);
589 dentry = kzalloc(sizeof(struct dentry), 0);
590 dir = kzalloc(sizeof(struct inode), 0);
591 if (!file || !dentry || !dir) {
592 printf("%s: Error, no memory for malloc!\n", __func__);
597 dir->i_sb = ubifs_sb;
598 file->f_path.dentry = dentry;
599 file->f_path.dentry->d_parent = dentry;
600 file->f_path.dentry->d_inode = dir;
601 file->f_path.dentry->d_inode->i_ino = inum;
603 file->private_data = NULL;
604 ubifs_printdir(file, dirent);
615 ubi_close_volume(c->ubi);
619 int ubifs_exists(const char *filename)
621 struct ubifs_info *c = ubifs_sb->s_fs_info;
624 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
625 inum = ubifs_findfile(ubifs_sb, (char *)filename);
626 ubi_close_volume(c->ubi);
631 int ubifs_size(const char *filename, loff_t *size)
633 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);
640 inum = ubifs_findfile(ubifs_sb, (char *)filename);
646 inode = ubifs_iget(ubifs_sb, inum);
648 printf("%s: Error reading inode %ld!\n", __func__, inum);
649 err = PTR_ERR(inode);
653 *size = inode->i_size;
657 ubi_close_volume(c->ubi);
667 static inline void *kmap(struct page *page)
672 static int read_block(struct inode *inode, void *addr, unsigned int block,
673 struct ubifs_data_node *dn)
675 struct ubifs_info *c = inode->i_sb->s_fs_info;
676 int err, len, out_len;
680 data_key_init(c, &key, inode->i_ino, block);
681 err = ubifs_tnc_lookup(c, &key, dn);
684 /* Not found, so it must be a hole */
685 memset(addr, 0, UBIFS_BLOCK_SIZE);
689 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
691 len = le32_to_cpu(dn->size);
692 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
695 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
696 out_len = UBIFS_BLOCK_SIZE;
697 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
698 le16_to_cpu(dn->compr_type));
699 if (err || len != out_len)
703 * Data length can be less than a full block, even for blocks that are
704 * not the last in the file (e.g., as a result of making a hole and
705 * appending data). Ensure that the remainder is zeroed out.
707 if (len < UBIFS_BLOCK_SIZE)
708 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
713 ubifs_err(c, "bad data node (block %u, inode %lu)",
714 block, inode->i_ino);
715 ubifs_dump_node(c, dn);
719 static int do_readpage(struct ubifs_info *c, struct inode *inode,
720 struct page *page, int last_block_size)
724 unsigned int block, beyond;
725 struct ubifs_data_node *dn;
726 loff_t i_size = inode->i_size;
728 dbg_gen("ino %lu, pg %lu, i_size %lld",
729 inode->i_ino, page->index, i_size);
733 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
734 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
735 if (block >= beyond) {
736 /* Reading beyond inode */
737 memset(addr, 0, PAGE_CACHE_SIZE);
741 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
749 if (block >= beyond) {
750 /* Reading beyond inode */
752 memset(addr, 0, UBIFS_BLOCK_SIZE);
755 * Reading last block? Make sure to not write beyond
756 * the requested size in the destination buffer.
758 if (((block + 1) == beyond) || last_block_size) {
763 * We need to buffer the data locally for the
764 * last block. This is to not pad the
765 * destination area to a multiple of
768 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
770 printf("%s: Error, malloc fails!\n",
776 /* Read block-size into temp buffer */
777 ret = read_block(inode, buff, block, dn);
780 if (err != -ENOENT) {
787 dlen = last_block_size;
789 dlen = le32_to_cpu(dn->size);
791 /* Now copy required size back to dest */
792 memcpy(addr, buff, dlen);
796 ret = read_block(inode, addr, block, dn);
804 if (++i >= UBIFS_BLOCKS_PER_PAGE)
807 addr += UBIFS_BLOCK_SIZE;
810 if (err == -ENOENT) {
811 /* Not found, so it must be a hole */
815 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
816 page->index, inode->i_ino, err);
830 int ubifs_read(const char *filename, void *buf, loff_t offset,
831 loff_t size, loff_t *actread)
833 struct ubifs_info *c = ubifs_sb->s_fs_info;
840 int last_block_size = 0;
844 if (offset & (PAGE_SIZE - 1)) {
845 printf("ubifs: Error offset must be a multiple of %d\n",
850 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
851 /* ubifs_findfile will resolve symlinks, so we know that we get
852 * the real file here */
853 inum = ubifs_findfile(ubifs_sb, (char *)filename);
862 inode = ubifs_iget(ubifs_sb, inum);
864 printf("%s: Error reading inode %ld!\n", __func__, inum);
865 err = PTR_ERR(inode);
869 if (offset > inode->i_size) {
870 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
877 * If no size was specified or if size bigger than filesize
878 * set size to filesize
880 if ((size == 0) || (size > (inode->i_size - offset)))
881 size = inode->i_size - offset;
883 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
886 page.index = offset / PAGE_SIZE;
888 for (i = 0; i < count; i++) {
890 * Make sure to not read beyond the requested size
892 if (((i + 1) == count) && (size < inode->i_size))
893 last_block_size = size - (i * PAGE_SIZE);
895 err = do_readpage(c, inode, &page, last_block_size);
899 page.addr += PAGE_SIZE;
904 printf("Error reading file '%s'\n", filename);
905 *actread = i * PAGE_SIZE;
914 ubi_close_volume(c->ubi);
918 void ubifs_close(void)
922 /* Compat wrappers for common/cmd_ubifs.c */
923 int ubifs_load(char *filename, u32 addr, u32 size)
928 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
930 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
932 env_set_hex("filesize", actread);
939 void uboot_ubifs_umount(void)
942 printf("Unmounting UBIFS volume %s!\n",
943 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
944 ubifs_umount(ubifs_sb->s_fs_info);