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 (Битюцкий Артём)
19 #include <u-boot/zlib.h>
21 #include <linux/err.h>
22 #include <linux/lzo.h>
24 DECLARE_GLOBAL_DATA_PTR;
29 * We need a wrapper for zunzip() because the parameters are
30 * incompatible with the lzo decompressor.
32 static int gzip_decompress(const unsigned char *in, size_t in_len,
33 unsigned char *out, size_t *out_len)
35 return zunzip(out, *out_len, (unsigned char *)in,
36 (unsigned long *)out_len, 0, 0);
39 /* Fake description object for the "none" compressor */
40 static struct ubifs_compressor none_compr = {
41 .compr_type = UBIFS_COMPR_NONE,
47 static struct ubifs_compressor lzo_compr = {
48 .compr_type = UBIFS_COMPR_LZO,
50 .comp_mutex = &lzo_mutex,
54 .decompress = lzo1x_decompress_safe,
57 static struct ubifs_compressor zlib_compr = {
58 .compr_type = UBIFS_COMPR_ZLIB,
60 .comp_mutex = &deflate_mutex,
61 .decomp_mutex = &inflate_mutex,
64 .capi_name = "deflate",
65 .decompress = gzip_decompress,
68 /* All UBIFS compressors */
69 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
76 * kmemdup - duplicate region of memory
78 * @src: memory region to duplicate
79 * @len: memory region length
80 * @gfp: GFP mask to use
82 void *kmemdup(const void *src, size_t len, gfp_t gfp)
86 p = kmalloc(len, gfp);
96 static inline struct crypto_comp
97 *crypto_alloc_comp(const char *alg_name, u32 type, u32 mask)
99 struct ubifs_compressor *comp;
100 struct crypto_comp *ptr;
103 ptr = malloc_cache_aligned(sizeof(struct crypto_comp));
104 while (i < UBIFS_COMPR_TYPES_CNT) {
105 comp = ubifs_compressors[i];
110 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) {
116 if (i >= UBIFS_COMPR_TYPES_CNT) {
117 dbg_gen("invalid compression type %s", alg_name);
124 crypto_comp_decompress(const struct ubifs_info *c, struct crypto_comp *tfm,
125 const u8 *src, unsigned int slen, u8 *dst,
128 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor];
130 size_t tmp_len = *dlen;
132 if (compr->compr_type == UBIFS_COMPR_NONE) {
133 memcpy(dst, src, slen);
138 err = compr->decompress(src, slen, dst, &tmp_len);
140 ubifs_err(c, "cannot decompress %d bytes, compressor %s, "
141 "error %d", slen, compr->name, err);
149 /* from shrinker.c */
151 /* Global clean znode counter (for all mounted UBIFS instances) */
152 atomic_long_t ubifs_clean_zn_cnt;
157 * ubifs_decompress - decompress data.
158 * @in_buf: data to decompress
159 * @in_len: length of the data to decompress
160 * @out_buf: output buffer where decompressed data should
161 * @out_len: output length is returned here
162 * @compr_type: type of compression
164 * This function decompresses data from buffer @in_buf into buffer @out_buf.
165 * The length of the uncompressed data is returned in @out_len. This functions
166 * returns %0 on success or a negative error code on failure.
168 int ubifs_decompress(const struct ubifs_info *c, const void *in_buf,
169 int in_len, void *out_buf, int *out_len, int compr_type)
172 struct ubifs_compressor *compr;
174 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
175 ubifs_err(c, "invalid compression type %d", compr_type);
179 compr = ubifs_compressors[compr_type];
181 if (unlikely(!compr->capi_name)) {
182 ubifs_err(c, "%s compression is not compiled in", compr->name);
186 if (compr_type == UBIFS_COMPR_NONE) {
187 memcpy(out_buf, in_buf, in_len);
192 if (compr->decomp_mutex)
193 mutex_lock(compr->decomp_mutex);
194 err = crypto_comp_decompress(c, compr->cc, in_buf, in_len, out_buf,
195 (unsigned int *)out_len);
196 if (compr->decomp_mutex)
197 mutex_unlock(compr->decomp_mutex);
199 ubifs_err(c, "cannot decompress %d bytes, compressor %s,"
200 " error %d", in_len, compr->name, err);
206 * compr_init - initialize a compressor.
207 * @compr: compressor description object
209 * This function initializes the requested compressor and returns zero in case
210 * of success or a negative error code in case of failure.
212 static int __init compr_init(struct ubifs_compressor *compr)
214 ubifs_compressors[compr->compr_type] = compr;
216 #ifdef CONFIG_NEEDS_MANUAL_RELOC
217 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
218 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
219 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
222 if (compr->capi_name) {
223 compr->cc = crypto_alloc_comp(compr->capi_name, 0, 0);
224 if (IS_ERR(compr->cc)) {
225 dbg_gen("cannot initialize compressor %s,"
226 " error %ld", compr->name,
228 return PTR_ERR(compr->cc);
236 * ubifs_compressors_init - initialize UBIFS compressors.
238 * This function initializes the compressor which were compiled in. Returns
239 * zero in case of success and a negative error code in case of failure.
241 int __init ubifs_compressors_init(void)
245 err = compr_init(&lzo_compr);
249 err = compr_init(&zlib_compr);
253 err = compr_init(&none_compr);
264 static int filldir(struct ubifs_info *c, const char *name, int namlen,
265 u64 ino, unsigned int d_type)
271 case UBIFS_ITYPE_REG:
274 case UBIFS_ITYPE_DIR:
277 case UBIFS_ITYPE_LNK:
285 inode = ubifs_iget(c->vfs_sb, ino);
287 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
288 __func__, ino, inode);
291 ctime_r((time_t *)&inode->i_mtime, filetime);
292 printf("%9lld %24.24s ", inode->i_size, filetime);
297 printf("%s\n", name);
302 static int ubifs_printdir(struct file *file, void *dirent)
307 struct ubifs_dent_node *dent;
308 struct inode *dir = file->f_path.dentry->d_inode;
309 struct ubifs_info *c = dir->i_sb->s_fs_info;
311 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
313 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
315 * The directory was seek'ed to a senseless position or there
316 * are no more entries.
320 if (file->f_pos == 1) {
321 /* Find the first entry in TNC and save it */
322 lowest_dent_key(c, &key, dir->i_ino);
324 dent = ubifs_tnc_next_ent(c, &key, &nm);
330 file->f_pos = key_hash_flash(c, &dent->key);
331 file->private_data = dent;
334 dent = file->private_data;
337 * The directory was seek'ed to and is now readdir'ed.
338 * Find the entry corresponding to @file->f_pos or the
341 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
343 dent = ubifs_tnc_next_ent(c, &key, &nm);
348 file->f_pos = key_hash_flash(c, &dent->key);
349 file->private_data = dent;
353 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
354 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
355 key_hash_flash(c, &dent->key));
357 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
360 nm.len = le16_to_cpu(dent->nlen);
361 over = filldir(c, (char *)dent->name, nm.len,
362 le64_to_cpu(dent->inum), dent->type);
366 /* Switch to the next entry */
367 key_read(c, &dent->key, &key);
368 nm.name = (char *)dent->name;
369 dent = ubifs_tnc_next_ent(c, &key, &nm);
375 kfree(file->private_data);
376 file->f_pos = key_hash_flash(c, &dent->key);
377 file->private_data = dent;
382 if (err != -ENOENT) {
383 ubifs_err(c, "cannot find next direntry, error %d", err);
387 kfree(file->private_data);
388 file->private_data = NULL;
393 static int ubifs_finddir(struct super_block *sb, char *dirname,
394 unsigned long root_inum, unsigned long *inum)
399 struct ubifs_dent_node *dent;
400 struct ubifs_info *c;
402 struct dentry *dentry;
406 file = kzalloc(sizeof(struct file), 0);
407 dentry = kzalloc(sizeof(struct dentry), 0);
408 dir = kzalloc(sizeof(struct inode), 0);
409 if (!file || !dentry || !dir) {
410 printf("%s: Error, no memory for malloc!\n", __func__);
416 file->f_path.dentry = dentry;
417 file->f_path.dentry->d_parent = dentry;
418 file->f_path.dentry->d_inode = dir;
419 file->f_path.dentry->d_inode->i_ino = root_inum;
422 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
424 /* Find the first entry in TNC and save it */
425 lowest_dent_key(c, &key, dir->i_ino);
427 dent = ubifs_tnc_next_ent(c, &key, &nm);
433 file->f_pos = key_hash_flash(c, &dent->key);
434 file->private_data = dent;
437 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
438 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
439 key_hash_flash(c, &dent->key));
441 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
444 nm.len = le16_to_cpu(dent->nlen);
445 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
446 (strlen(dirname) == nm.len)) {
447 *inum = le64_to_cpu(dent->inum);
452 /* Switch to the next entry */
453 key_read(c, &dent->key, &key);
454 nm.name = (char *)dent->name;
455 dent = ubifs_tnc_next_ent(c, &key, &nm);
461 kfree(file->private_data);
462 file->f_pos = key_hash_flash(c, &dent->key);
463 file->private_data = dent;
469 dbg_gen("cannot find next direntry, error %d", err);
472 kfree(file->private_data);
480 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
485 char symlinkpath[128];
487 unsigned long root_inum = 1;
489 int symlink_count = 0; /* Don't allow symlink recursion */
492 strcpy(fpath, filename);
494 /* Remove all leading slashes */
499 * Handle root-direcoty ('/')
502 if (!name || *name == '\0')
507 struct ubifs_inode *ui;
509 /* Extract the actual part from the pathname. */
510 next = strchr(name, '/');
512 /* Remove all leading slashes. */
517 ret = ubifs_finddir(sb, name, root_inum, &inum);
520 inode = ubifs_iget(sb, inum);
524 ui = ubifs_inode(inode);
526 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
529 /* We have some sort of symlink recursion, bail out */
530 if (symlink_count++ > 8) {
531 printf("Symlink recursion, aborting\n");
534 memcpy(link_name, ui->data, ui->data_len);
535 link_name[ui->data_len] = '\0';
537 if (link_name[0] == '/') {
538 /* Absolute path, redo everything without
539 * the leading slash */
540 next = name = link_name + 1;
544 /* Relative to cur dir */
545 sprintf(buf, "%s/%s",
546 link_name, next == NULL ? "" : next);
547 memcpy(symlinkpath, buf, sizeof(buf));
548 next = name = symlinkpath;
553 * Check if directory with this name exists
556 /* Found the node! */
557 if (!next || *next == '\0')
567 int ubifs_set_blk_dev(struct blk_desc *rbdd, disk_partition_t *info)
570 debug("UBIFS cannot be used with normal block devices\n");
575 * Should never happen since blk_get_device_part_str() already checks
576 * this, but better safe then sorry.
578 if (!ubifs_is_mounted()) {
579 debug("UBIFS not mounted, use ubifsmount to mount volume first!\n");
586 int ubifs_ls(const char *filename)
588 struct ubifs_info *c = ubifs_sb->s_fs_info;
590 struct dentry *dentry;
596 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
597 inum = ubifs_findfile(ubifs_sb, (char *)filename);
603 file = kzalloc(sizeof(struct file), 0);
604 dentry = kzalloc(sizeof(struct dentry), 0);
605 dir = kzalloc(sizeof(struct inode), 0);
606 if (!file || !dentry || !dir) {
607 printf("%s: Error, no memory for malloc!\n", __func__);
612 dir->i_sb = ubifs_sb;
613 file->f_path.dentry = dentry;
614 file->f_path.dentry->d_parent = dentry;
615 file->f_path.dentry->d_inode = dir;
616 file->f_path.dentry->d_inode->i_ino = inum;
618 file->private_data = NULL;
619 ubifs_printdir(file, dirent);
630 ubi_close_volume(c->ubi);
634 int ubifs_exists(const char *filename)
636 struct ubifs_info *c = ubifs_sb->s_fs_info;
639 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
640 inum = ubifs_findfile(ubifs_sb, (char *)filename);
641 ubi_close_volume(c->ubi);
646 int ubifs_size(const char *filename, loff_t *size)
648 struct ubifs_info *c = ubifs_sb->s_fs_info;
653 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
655 inum = ubifs_findfile(ubifs_sb, (char *)filename);
661 inode = ubifs_iget(ubifs_sb, inum);
663 printf("%s: Error reading inode %ld!\n", __func__, inum);
664 err = PTR_ERR(inode);
668 *size = inode->i_size;
672 ubi_close_volume(c->ubi);
682 static inline void *kmap(struct page *page)
687 static int read_block(struct inode *inode, void *addr, unsigned int block,
688 struct ubifs_data_node *dn)
690 struct ubifs_info *c = inode->i_sb->s_fs_info;
691 int err, len, out_len;
695 data_key_init(c, &key, inode->i_ino, block);
696 err = ubifs_tnc_lookup(c, &key, dn);
699 /* Not found, so it must be a hole */
700 memset(addr, 0, UBIFS_BLOCK_SIZE);
704 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
706 len = le32_to_cpu(dn->size);
707 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
710 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
711 out_len = UBIFS_BLOCK_SIZE;
712 err = ubifs_decompress(c, &dn->data, dlen, addr, &out_len,
713 le16_to_cpu(dn->compr_type));
714 if (err || len != out_len)
718 * Data length can be less than a full block, even for blocks that are
719 * not the last in the file (e.g., as a result of making a hole and
720 * appending data). Ensure that the remainder is zeroed out.
722 if (len < UBIFS_BLOCK_SIZE)
723 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
728 ubifs_err(c, "bad data node (block %u, inode %lu)",
729 block, inode->i_ino);
730 ubifs_dump_node(c, dn);
734 static int do_readpage(struct ubifs_info *c, struct inode *inode,
735 struct page *page, int last_block_size)
739 unsigned int block, beyond;
740 struct ubifs_data_node *dn;
741 loff_t i_size = inode->i_size;
743 dbg_gen("ino %lu, pg %lu, i_size %lld",
744 inode->i_ino, page->index, i_size);
748 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
749 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
750 if (block >= beyond) {
751 /* Reading beyond inode */
752 memset(addr, 0, PAGE_CACHE_SIZE);
756 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
764 if (block >= beyond) {
765 /* Reading beyond inode */
767 memset(addr, 0, UBIFS_BLOCK_SIZE);
770 * Reading last block? Make sure to not write beyond
771 * the requested size in the destination buffer.
773 if (((block + 1) == beyond) || last_block_size) {
778 * We need to buffer the data locally for the
779 * last block. This is to not pad the
780 * destination area to a multiple of
783 buff = malloc_cache_aligned(UBIFS_BLOCK_SIZE);
785 printf("%s: Error, malloc fails!\n",
791 /* Read block-size into temp buffer */
792 ret = read_block(inode, buff, block, dn);
795 if (err != -ENOENT) {
802 dlen = last_block_size;
804 dlen = le32_to_cpu(dn->size);
806 /* Now copy required size back to dest */
807 memcpy(addr, buff, dlen);
811 ret = read_block(inode, addr, block, dn);
819 if (++i >= UBIFS_BLOCKS_PER_PAGE)
822 addr += UBIFS_BLOCK_SIZE;
825 if (err == -ENOENT) {
826 /* Not found, so it must be a hole */
830 ubifs_err(c, "cannot read page %lu of inode %lu, error %d",
831 page->index, inode->i_ino, err);
845 int ubifs_read(const char *filename, void *buf, loff_t offset,
846 loff_t size, loff_t *actread)
848 struct ubifs_info *c = ubifs_sb->s_fs_info;
855 int last_block_size = 0;
859 if (offset & (PAGE_SIZE - 1)) {
860 printf("ubifs: Error offset must be a multiple of %d\n",
865 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
866 /* ubifs_findfile will resolve symlinks, so we know that we get
867 * the real file here */
868 inum = ubifs_findfile(ubifs_sb, (char *)filename);
877 inode = ubifs_iget(ubifs_sb, inum);
879 printf("%s: Error reading inode %ld!\n", __func__, inum);
880 err = PTR_ERR(inode);
884 if (offset > inode->i_size) {
885 printf("ubifs: Error offset (%lld) > file-size (%lld)\n",
892 * If no size was specified or if size bigger than filesize
893 * set size to filesize
895 if ((size == 0) || (size > (inode->i_size - offset)))
896 size = inode->i_size - offset;
898 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
901 page.index = offset / PAGE_SIZE;
903 for (i = 0; i < count; i++) {
905 * Make sure to not read beyond the requested size
907 if (((i + 1) == count) && (size < inode->i_size))
908 last_block_size = size - (i * PAGE_SIZE);
910 err = do_readpage(c, inode, &page, last_block_size);
914 page.addr += PAGE_SIZE;
919 printf("Error reading file '%s'\n", filename);
920 *actread = i * PAGE_SIZE;
929 ubi_close_volume(c->ubi);
933 void ubifs_close(void)
937 /* Compat wrappers for common/cmd_ubifs.c */
938 int ubifs_load(char *filename, u32 addr, u32 size)
943 printf("Loading file '%s' to addr 0x%08x...\n", filename, addr);
945 err = ubifs_read(filename, (void *)(uintptr_t)addr, 0, size, &actread);
947 env_set_hex("filesize", actread);
954 void uboot_ubifs_umount(void)
957 printf("Unmounting UBIFS volume %s!\n",
958 ((struct ubifs_info *)(ubifs_sb->s_fs_info))->vi.name);
959 ubifs_umount(ubifs_sb->s_fs_info);