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 (Битюцкий Артём)
17 #include <u-boot/zlib.h>
19 #include <linux/err.h>
20 #include <linux/lzo.h>
22 DECLARE_GLOBAL_DATA_PTR;
27 * We need a wrapper for zunzip() because the parameters are
28 * incompatible with the lzo decompressor.
30 static int gzip_decompress(const unsigned char *in, size_t in_len,
31 unsigned char *out, size_t *out_len)
33 return zunzip(out, *out_len, (unsigned char *)in,
34 (unsigned long *)out_len, 0, 0);
37 /* Fake description object for the "none" compressor */
38 static struct ubifs_compressor none_compr = {
39 .compr_type = UBIFS_COMPR_NONE,
45 static struct ubifs_compressor lzo_compr = {
46 .compr_type = UBIFS_COMPR_LZO,
48 .comp_mutex = &lzo_mutex,
52 .decompress = lzo1x_decompress_safe,
55 static struct ubifs_compressor zlib_compr = {
56 .compr_type = UBIFS_COMPR_ZLIB,
58 .comp_mutex = &deflate_mutex,
59 .decomp_mutex = &inflate_mutex,
62 .capi_name = "deflate",
63 .decompress = gzip_decompress,
66 /* All UBIFS compressors */
67 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
74 * kmemdup - duplicate region of memory
76 * @src: memory region to duplicate
77 * @len: memory region length
78 * @gfp: GFP mask to use
80 void *kmemdup(const void *src, size_t len, gfp_t gfp)
84 p = kmalloc(len, gfp);
94 static inline struct crypto_comp
95 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
97 struct ubifs_compressor *comp;
98 struct crypto_comp *ptr;
101 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
102 while (i < UBIFS_COMPR_TYPES_CNT) {
103 comp = ubifs_compressors[i];
108 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
114 if (i >= UBIFS_COMPR_TYPES_CNT) {
115 dbg_gen("invalid compression type %s", alg_name);
122 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
123 const u8 *src, unsigned int slen, u8 *dst,
126 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
129 if (compr->compr_type == UBIFS_COMPR_NONE) {
130 memcpy(dst, src, slen);
135 err = compr->decompress(src, slen, dst, (size_t *)dlen);
137 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
138 "error %d", slen, compr->name, err);
145 /* from shrinker.c */
147 /* Global clean znode counter (for all mounted UBIFS instances) */
148 atomic_long_t ubifs_clean_zn_cnt;
153 * ubifs_decompress - decompress data.
154 * @in_buf: data to decompress
155 * @in_len: length of the data to decompress
156 * @out_buf: output buffer where decompressed data should
157 * @out_len: output length is returned here
158 * @compr_type: type of compression
160 * This function decompresses data from buffer @in_buf into buffer @out_buf.
161 * The length of the uncompressed data is returned in @out_len. This functions
162 * returns %0 on success or a negative error code on failure.
164 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
165 int in_len, void *out_buf, int *out_len, int compr_type)
168 struct ubifs_compressor *compr;
170 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
171 ubifs_err(c, "invalid compression type %d", compr_type);
175 compr = ubifs_compressors[compr_type];
177 if (unlikely(!compr->capi_name)) {
178 ubifs_err(c, "%s compression is not compiled in", compr->name);
182 if (compr_type == UBIFS_COMPR_NONE) {
183 memcpy(out_buf, in_buf, in_len);
188 if (compr->decomp_mutex)
189 mutex_lock(compr->decomp_mutex);
190 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
191 (unsigned int *)out_len);
192 if (compr->decomp_mutex)
193 mutex_unlock(compr->decomp_mutex);
195 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
196 " error %d", in_len, compr->name, err);
202 * compr_init - initialize a compressor.
203 * @compr: compressor description object
205 * This function initializes the requested compressor and returns zero in case
206 * of success or a negative error code in case of failure.
208 static int __init compr_init(struct ubifs_compressor *compr)
210 ubifs_compressors[compr->compr_type] = compr;
212 #ifdef CONFIG_NEEDS_MANUAL_RELOC
213 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
214 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
215 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
218 if (compr->capi_name) {
219 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
220 if (IS_ERR(compr->cc)) {
221 dbg_gen("cannot initialize compressor %s,"
222 " error %ld", compr->name,
224 return PTR_ERR(compr->cc);
232 * ubifs_compressors_init - initialize UBIFS compressors.
234 * This function initializes the compressor which were compiled in. Returns
235 * zero in case of success and a negative error code in case of failure.
237 int __init ubifs_compressors_init(void)
241 err = compr_init(&lzo_compr);
245 err = compr_init(&zlib_compr);
249 err = compr_init(&none_compr);
260 static int filldir(struct ubifs_info *c, const char *name, int namlen,
261 u64 ino, unsigned int d_type)
267 case UBIFS_ITYPE_REG:
270 case UBIFS_ITYPE_DIR:
273 case UBIFS_ITYPE_LNK:
281 inode = ubifs_iget(c->vfs_sb, ino);
283 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
284 __func__, ino, inode);
287 ctime_r((time_t *)&inode->i_mtime, filetime);
288 printf("%9lld %24.24s ", inode->i_size, filetime);
293 printf("%s\n", name);
298 static int ubifs_printdir(struct file *file, void *dirent)
303 struct ubifs_dent_node *dent;
304 struct inode *dir = file->f_path.dentry->d_inode;
305 struct ubifs_info *c = dir->i_sb->s_fs_info;
307 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
309 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
311 * The directory was seek'ed to a senseless position or there
312 * are no more entries.
316 if (file->f_pos == 1) {
317 /* Find the first entry in TNC and save it */
318 lowest_dent_key(c, &key, dir->i_ino);
320 dent = ubifs_tnc_next_ent(c, &key, &nm);
326 file->f_pos = key_hash_flash(c, &dent->key);
327 file->private_data = dent;
330 dent = file->private_data;
333 * The directory was seek'ed to and is now readdir'ed.
334 * Find the entry corresponding to @file->f_pos or the
337 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
339 dent = ubifs_tnc_next_ent(c, &key, &nm);
344 file->f_pos = key_hash_flash(c, &dent->key);
345 file->private_data = dent;
349 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
350 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
351 key_hash_flash(c, &dent->key));
353 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
356 nm.len = le16_to_cpu(dent->nlen);
357 over = filldir(c, (char *)dent->name, nm.len,
358 le64_to_cpu(dent->inum), dent->type);
362 /* Switch to the next entry */
363 key_read(c, &dent->key, &key);
364 nm.name = (char *)dent->name;
365 dent = ubifs_tnc_next_ent(c, &key, &nm);
371 kfree(file->private_data);
372 file->f_pos = key_hash_flash(c, &dent->key);
373 file->private_data = dent;
378 if (err != -ENOENT) {
379 ubifs_err(c, "cannot find next direntry, error %d", err);
383 kfree(file->private_data);
384 file->private_data = NULL;
389 static int ubifs_finddir(struct super_block *sb, char *dirname,
390 unsigned long root_inum, unsigned long *inum)
395 struct ubifs_dent_node *dent;
396 struct ubifs_info *c;
398 struct dentry *dentry;
402 file = kzalloc(sizeof(struct file), 0);
403 dentry = kzalloc(sizeof(struct dentry), 0);
404 dir = kzalloc(sizeof(struct inode), 0);
405 if (!file || !dentry || !dir) {
406 printf("%s: Error, no memory for malloc!\n", __func__);
412 file->f_path.dentry = dentry;
413 file->f_path.dentry->d_parent = dentry;
414 file->f_path.dentry->d_inode = dir;
415 file->f_path.dentry->d_inode->i_ino = root_inum;
418 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
420 /* Find the first entry in TNC and save it */
421 lowest_dent_key(c, &key, dir->i_ino);
423 dent = ubifs_tnc_next_ent(c, &key, &nm);
429 file->f_pos = key_hash_flash(c, &dent->key);
430 file->private_data = dent;
433 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
434 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
435 key_hash_flash(c, &dent->key));
437 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
440 nm.len = le16_to_cpu(dent->nlen);
441 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
442 (strlen(dirname) == nm.len)) {
443 *inum = le64_to_cpu(dent->inum);
448 /* Switch to the next entry */
449 key_read(c, &dent->key, &key);
450 nm.name = (char *)dent->name;
451 dent = ubifs_tnc_next_ent(c, &key, &nm);
457 kfree(file->private_data);
458 file->f_pos = key_hash_flash(c, &dent->key);
459 file->private_data = dent;
465 dbg_gen("cannot find next direntry, error %d", err);
468 kfree(file->private_data);
476 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
481 char symlinkpath[128];
483 unsigned long root_inum = 1;
485 int symlink_count = 0; /* Don't allow symlink recursion */
488 strcpy(fpath, filename);
490 /* Remove all leading slashes */
495 * Handle root-direcoty ('/')
498 if (!name || *name == '\0')
503 struct ubifs_inode *ui;
505 /* Extract the actual part from the pathname. */
506 next = strchr(name, '/');
508 /* Remove all leading slashes. */
513 ret = ubifs_finddir(sb, name, root_inum, &inum);
516 inode = ubifs_iget(sb, inum);
520 ui = ubifs_inode(inode);
522 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
525 /* We have some sort of symlink recursion, bail out */
526 if (symlink_count++ > 8) {
527 printf("Symlink recursion, aborting\n");
530 memcpy(link_name, ui->data, ui->data_len);
531 link_name[ui->data_len] = '\0';
533 if (link_name[0] == '/') {
534 /* Absolute path, redo everything without
535 * the leading slash */
536 next = name = link_name + 1;
540 /* Relative to cur dir */
541 sprintf(buf, "%s/%s",
542 link_name, next == NULL ? "" : next);
543 memcpy(symlinkpath, buf, sizeof(buf));
544 next = name = symlinkpath;
549 * Check if directory with this name exists
552 /* Found the node! */
553 if (!next || *next == '\0')
563 int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
566 debug("UBIFS cannot be used with normal block devices\n");
571 * Should never happen since blk_get_device_part_str() already checks
572 * this, but better safe then sorry.
574 if (!ubifs_is_mounted()) {
575 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
582 int ubifs_ls(const char *filename)
584 struct ubifs_info *c = ubifs_sb->s_fs_info;
586 struct dentry *dentry;
592 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
593 inum = ubifs_findfile(ubifs_sb, (char *)filename);
599 file = kzalloc(sizeof(struct file), 0);
600 dentry = kzalloc(sizeof(struct dentry), 0);
601 dir = kzalloc(sizeof(struct inode), 0);
602 if (!file || !dentry || !dir) {
603 printf("%s: Error, no memory for malloc!\n", __func__);
608 dir->i_sb = ubifs_sb;
609 file->f_path.dentry = dentry;
610 file->f_path.dentry->d_parent = dentry;
611 file->f_path.dentry->d_inode = dir;
612 file->f_path.dentry->d_inode->i_ino = inum;
614 file->private_data = NULL;
615 ubifs_printdir(file, dirent);
626 ubi_close_volume(c->ubi);
630 int ubifs_exists(const char *filename)
632 struct ubifs_info *c = ubifs_sb->s_fs_info;
635 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
636 inum = ubifs_findfile(ubifs_sb, (char *)filename);
637 ubi_close_volume(c->ubi);
642 int ubifs_size(const char *filename, loff_t *size)
644 struct ubifs_info *c = ubifs_sb->s_fs_info;
649 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
651 inum = ubifs_findfile(ubifs_sb, (char *)filename);
657 inode = ubifs_iget(ubifs_sb, inum);
659 printf("%s: Error reading inode %ld!\n", __func__, inum);
660 err = PTR_ERR(inode);
664 *size = inode->i_size;
668 ubi_close_volume(c->ubi);
678 static inline void *kmap(struct page *page)
683 static int read_block(struct inode *inode, void *addr, unsigned int block,
684 struct ubifs_data_node *dn)
686 struct ubifs_info *c = inode->i_sb->s_fs_info;
687 int err, len, out_len;
691 data_key_init(c, &key, inode->i_ino, block);
692 err = ubifs_tnc_lookup(c, &key, dn);
695 /* Not found, so it must be a hole */
696 memset(addr, 0, UBIFS_BLOCK_SIZE);
700 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
702 len = le32_to_cpu(dn->size);
703 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
706 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
707 out_len = UBIFS_BLOCK_SIZE;
708 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
709 le16_to_cpu(dn->compr_type));
710 if (err || len != out_len)
714 * Data length can be less than a full block, even for blocks that are
715 * not the last in the file (e.g., as a result of making a hole and
716 * appending data). Ensure that the remainder is zeroed out.
718 if (len < UBIFS_BLOCK_SIZE)
719 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
724 ubifs_err(c, "bad data node (block %u, inode %lu)",
725 block, inode->i_ino);
726 ubifs_dump_node(c, dn);
730 static int do_readpage(struct ubifs_info *c, struct inode *inode,
731 struct page *page, int last_block_size)
735 unsigned int block, beyond;
736 struct ubifs_data_node *dn;
737 loff_t i_size = inode->i_size;
739 dbg_gen("ino %lu, pg %lu, i_size %lld",
740 inode->i_ino, page->index, i_size);
744 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
745 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
746 if (block >= beyond) {
747 /* Reading beyond inode */
748 memset(addr, 0, PAGE_CACHE_SIZE);
752 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
760 if (block >= beyond) {
761 /* Reading beyond inode */
763 memset(addr, 0, UBIFS_BLOCK_SIZE);
766 * Reading last block? Make sure to not write beyond
767 * the requested size in the destination buffer.
769 if (((block + 1) == beyond) || last_block_size) {
774 * We need to buffer the data locally for the
775 * last block. This is to not pad the
776 * destination area to a multiple of
779 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
781 printf("%s: Error, malloc fails!\n",
787 /* Read block-size into temp buffer */
788 ret = read_block(inode, buff, block, dn);
791 if (err != -ENOENT) {
798 dlen = last_block_size;
800 dlen = le32_to_cpu(dn->size);
802 /* Now copy required size back to dest */
803 memcpy(addr, buff, dlen);
807 ret = read_block(inode, addr, block, dn);
815 if (++i >= UBIFS_BLOCKS_PER_PAGE)
818 addr += UBIFS_BLOCK_SIZE;
821 if (err == -ENOENT) {
822 /* Not found, so it must be a hole */
826 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
827 page->index, inode->i_ino, err);
841 int ubifs_read(const char *filename, void *buf, loff_t offset,
842 loff_t size, loff_t *actread)
844 struct ubifs_info *c = ubifs_sb->s_fs_info;
851 int last_block_size = 0;
855 if (offset & (PAGE_SIZE - 1)) {
856 printf("ubifs: Error offset must be a multiple of %d\n",
861 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
862 /* ubifs_findfile will resolve symlinks, so we know that we get
863 * the real file here */
864 inum = ubifs_findfile(ubifs_sb, (char *)filename);
873 inode = ubifs_iget(ubifs_sb, inum);
875 printf("%s: Error reading inode %ld!\n", __func__, inum);
876 err = PTR_ERR(inode);
880 if (offset > inode->i_size) {
881 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
888 * If no size was specified or if size bigger than filesize
889 * set size to filesize
891 if ((size == 0) || (size > (inode->i_size - offset)))
892 size = inode->i_size - offset;
894 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
897 page.index = offset / PAGE_SIZE;
899 for (i = 0; i < count; i++) {
901 * Make sure to not read beyond the requested size
903 if (((i + 1) == count) && (size < inode->i_size))
904 last_block_size = size - (i * PAGE_SIZE);
906 err = do_readpage(c, inode, &page, last_block_size);
910 page.addr += PAGE_SIZE;
915 printf("Error reading file '%s'\n", filename);
916 *actread = i * PAGE_SIZE;
925 ubi_close_volume(c->ubi);
929 void ubifs_close(void)
933 /* Compat wrappers for common/cmd_ubifs.c */
934 int ubifs_load(char *filename, u32 addr, u32 size)
939 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
941 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
943 env_set_hex("filesize", actread);
950 void uboot_ubifs_umount(void)
953 printf("Unmounting UBIFS volume %s!\n",
954 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
955 ubifs_umount(ubifs_sb->s_fs_info);