2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * (C) Copyright 2008-2010
7 * Stefan Roese, DENX Software Engineering, sr@denx.de.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as published by
11 * the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc., 51
20 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Authors: Artem Bityutskiy (Битюцкий Артём)
27 #include <u-boot/zlib.h>
30 #include <linux/err.h>
31 #include <linux/lzo.h>
33 DECLARE_GLOBAL_DATA_PTR
;
38 * We need a wrapper for zunzip() because the parameters are
39 * incompatible with the lzo decompressor.
41 static int gzip_decompress(const unsigned char *in
, size_t in_len
,
42 unsigned char *out
, size_t *out_len
)
44 return zunzip(out
, *out_len
, (unsigned char *)in
,
45 (unsigned long *)out_len
, 0, 0);
48 /* Fake description object for the "none" compressor */
49 static struct ubifs_compressor none_compr
= {
50 .compr_type
= UBIFS_COMPR_NONE
,
56 static struct ubifs_compressor lzo_compr
= {
57 .compr_type
= UBIFS_COMPR_LZO
,
59 .comp_mutex
= &lzo_mutex
,
63 .decompress
= lzo1x_decompress_safe
,
66 static struct ubifs_compressor zlib_compr
= {
67 .compr_type
= UBIFS_COMPR_ZLIB
,
69 .comp_mutex
= &deflate_mutex
,
70 .decomp_mutex
= &inflate_mutex
,
73 .capi_name
= "deflate",
74 .decompress
= gzip_decompress
,
77 /* All UBIFS compressors */
78 struct ubifs_compressor
*ubifs_compressors
[UBIFS_COMPR_TYPES_CNT
];
85 * kmemdup - duplicate region of memory
87 * @src: memory region to duplicate
88 * @len: memory region length
89 * @gfp: GFP mask to use
91 void *kmemdup(const void *src
, size_t len
, gfp_t gfp
)
95 p
= kmalloc(len
, gfp
);
105 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
108 struct ubifs_compressor
*comp
;
109 struct crypto_comp
*ptr
;
112 ptr
= malloc(sizeof(struct crypto_comp
));
113 while (i
< UBIFS_COMPR_TYPES_CNT
) {
114 comp
= ubifs_compressors
[i
];
119 if (strncmp(alg_name
, comp
->capi_name
, strlen(alg_name
)) == 0) {
125 if (i
>= UBIFS_COMPR_TYPES_CNT
) {
126 ubifs_err("invalid compression type %s", alg_name
);
132 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
133 const u8
*src
, unsigned int slen
,
134 u8
*dst
, unsigned int *dlen
)
136 struct ubifs_compressor
*compr
= ubifs_compressors
[tfm
->compressor
];
139 if (compr
->compr_type
== UBIFS_COMPR_NONE
) {
140 memcpy(dst
, src
, slen
);
145 err
= compr
->decompress(src
, slen
, dst
, (size_t *)dlen
);
147 ubifs_err("cannot decompress %d bytes, compressor %s, "
148 "error %d", slen
, compr
->name
, err
);
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 void *in_buf
, int in_len
, void *out_buf
,
169 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("invalid compression type %d", compr_type
);
179 compr
= ubifs_compressors
[compr_type
];
181 if (unlikely(!compr
->capi_name
)) {
182 ubifs_err("%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(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("cannot decompress %d bytes, compressor %s, error %d",
200 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 ubifs_err("cannot initialize compressor %s, error %ld",
226 compr
->name
, PTR_ERR(compr
->cc
));
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
));
355 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) > ubifs_inode(dir
)->creat_sqnum
);
357 nm
.len
= le16_to_cpu(dent
->nlen
);
358 over
= filldir(c
, (char *)dent
->name
, nm
.len
,
359 le64_to_cpu(dent
->inum
), dent
->type
);
363 /* Switch to the next entry */
364 key_read(c
, &dent
->key
, &key
);
365 nm
.name
= (char *)dent
->name
;
366 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
372 kfree(file
->private_data
);
373 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
374 file
->private_data
= dent
;
379 if (err
!= -ENOENT
) {
380 ubifs_err("cannot find next direntry, error %d", err
);
384 kfree(file
->private_data
);
385 file
->private_data
= NULL
;
390 static int ubifs_finddir(struct super_block
*sb
, char *dirname
,
391 unsigned long root_inum
, unsigned long *inum
)
396 struct ubifs_dent_node
*dent
;
397 struct ubifs_info
*c
;
399 struct dentry
*dentry
;
403 file
= kzalloc(sizeof(struct file
), 0);
404 dentry
= kzalloc(sizeof(struct dentry
), 0);
405 dir
= kzalloc(sizeof(struct inode
), 0);
406 if (!file
|| !dentry
|| !dir
) {
407 printf("%s: Error, no memory for malloc!\n", __func__
);
413 file
->f_path
.dentry
= dentry
;
414 file
->f_path
.dentry
->d_parent
= dentry
;
415 file
->f_path
.dentry
->d_inode
= dir
;
416 file
->f_path
.dentry
->d_inode
->i_ino
= root_inum
;
419 dbg_gen("dir ino %lu, f_pos %#llx", dir
->i_ino
, file
->f_pos
);
421 /* Find the first entry in TNC and save it */
422 lowest_dent_key(c
, &key
, dir
->i_ino
);
424 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
430 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
431 file
->private_data
= dent
;
434 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
435 dent
->name
, (unsigned long long)le64_to_cpu(dent
->inum
),
436 key_hash_flash(c
, &dent
->key
));
437 ubifs_assert(le64_to_cpu(dent
->ch
.sqnum
) > ubifs_inode(dir
)->creat_sqnum
);
439 nm
.len
= le16_to_cpu(dent
->nlen
);
440 if ((strncmp(dirname
, (char *)dent
->name
, nm
.len
) == 0) &&
441 (strlen(dirname
) == nm
.len
)) {
442 *inum
= le64_to_cpu(dent
->inum
);
447 /* Switch to the next entry */
448 key_read(c
, &dent
->key
, &key
);
449 nm
.name
= (char *)dent
->name
;
450 dent
= ubifs_tnc_next_ent(c
, &key
, &nm
);
456 kfree(file
->private_data
);
457 file
->f_pos
= key_hash_flash(c
, &dent
->key
);
458 file
->private_data
= dent
;
464 ubifs_err("cannot find next direntry, error %d", err
);
467 if (file
->private_data
)
468 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_ls(char *filename
)
568 struct ubifs_info
*c
= ubifs_sb
->s_fs_info
;
570 struct dentry
*dentry
;
576 c
->ubi
= ubi_open_volume(c
->vi
.ubi_num
, c
->vi
.vol_id
, UBI_READONLY
);
577 inum
= ubifs_findfile(ubifs_sb
, filename
);
583 file
= kzalloc(sizeof(struct file
), 0);
584 dentry
= kzalloc(sizeof(struct dentry
), 0);
585 dir
= kzalloc(sizeof(struct inode
), 0);
586 if (!file
|| !dentry
|| !dir
) {
587 printf("%s: Error, no memory for malloc!\n", __func__
);
592 dir
->i_sb
= ubifs_sb
;
593 file
->f_path
.dentry
= dentry
;
594 file
->f_path
.dentry
->d_parent
= dentry
;
595 file
->f_path
.dentry
->d_inode
= dir
;
596 file
->f_path
.dentry
->d_inode
->i_ino
= inum
;
598 file
->private_data
= NULL
;
599 ubifs_printdir(file
, dirent
);
610 ubi_close_volume(c
->ubi
);
620 static inline void *kmap(struct page
*page
)
625 static int read_block(struct inode
*inode
, void *addr
, unsigned int block
,
626 struct ubifs_data_node
*dn
)
628 struct ubifs_info
*c
= inode
->i_sb
->s_fs_info
;
629 int err
, len
, out_len
;
633 data_key_init(c
, &key
, inode
->i_ino
, block
);
634 err
= ubifs_tnc_lookup(c
, &key
, dn
);
637 /* Not found, so it must be a hole */
638 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
642 ubifs_assert(le64_to_cpu(dn
->ch
.sqnum
) > ubifs_inode(inode
)->creat_sqnum
);
644 len
= le32_to_cpu(dn
->size
);
645 if (len
<= 0 || len
> UBIFS_BLOCK_SIZE
)
648 dlen
= le32_to_cpu(dn
->ch
.len
) - UBIFS_DATA_NODE_SZ
;
649 out_len
= UBIFS_BLOCK_SIZE
;
650 err
= ubifs_decompress(&dn
->data
, dlen
, addr
, &out_len
,
651 le16_to_cpu(dn
->compr_type
));
652 if (err
|| len
!= out_len
)
656 * Data length can be less than a full block, even for blocks that are
657 * not the last in the file (e.g., as a result of making a hole and
658 * appending data). Ensure that the remainder is zeroed out.
660 if (len
< UBIFS_BLOCK_SIZE
)
661 memset(addr
+ len
, 0, UBIFS_BLOCK_SIZE
- len
);
666 ubifs_err("bad data node (block %u, inode %lu)",
667 block
, inode
->i_ino
);
668 ubifs_dump_node(c
, dn
);
672 static int do_readpage(struct ubifs_info
*c
, struct inode
*inode
,
673 struct page
*page
, int last_block_size
)
677 unsigned int block
, beyond
;
678 struct ubifs_data_node
*dn
;
679 loff_t i_size
= inode
->i_size
;
681 dbg_gen("ino %lu, pg %lu, i_size %lld",
682 inode
->i_ino
, page
->index
, i_size
);
686 block
= page
->index
<< UBIFS_BLOCKS_PER_PAGE_SHIFT
;
687 beyond
= (i_size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
688 if (block
>= beyond
) {
689 /* Reading beyond inode */
690 memset(addr
, 0, PAGE_CACHE_SIZE
);
694 dn
= kmalloc(UBIFS_MAX_DATA_NODE_SZ
, GFP_NOFS
);
702 if (block
>= beyond
) {
703 /* Reading beyond inode */
705 memset(addr
, 0, UBIFS_BLOCK_SIZE
);
708 * Reading last block? Make sure to not write beyond
709 * the requested size in the destination buffer.
711 if (((block
+ 1) == beyond
) || last_block_size
) {
716 * We need to buffer the data locally for the
717 * last block. This is to not pad the
718 * destination area to a multiple of
721 buff
= malloc(UBIFS_BLOCK_SIZE
);
723 printf("%s: Error, malloc fails!\n",
729 /* Read block-size into temp buffer */
730 ret
= read_block(inode
, buff
, block
, dn
);
733 if (err
!= -ENOENT
) {
740 dlen
= last_block_size
;
742 dlen
= le32_to_cpu(dn
->size
);
744 /* Now copy required size back to dest */
745 memcpy(addr
, buff
, dlen
);
749 ret
= read_block(inode
, addr
, block
, dn
);
757 if (++i
>= UBIFS_BLOCKS_PER_PAGE
)
760 addr
+= UBIFS_BLOCK_SIZE
;
763 if (err
== -ENOENT
) {
764 /* Not found, so it must be a hole */
768 ubifs_err("cannot read page %lu of inode %lu, error %d",
769 page
->index
, inode
->i_ino
, err
);
783 int ubifs_load(char *filename
, u32 addr
, u32 size
)
785 struct ubifs_info
*c
= ubifs_sb
->s_fs_info
;
792 int last_block_size
= 0;
794 c
->ubi
= ubi_open_volume(c
->vi
.ubi_num
, c
->vi
.vol_id
, UBI_READONLY
);
795 /* ubifs_findfile will resolve symlinks, so we know that we get
796 * the real file here */
797 inum
= ubifs_findfile(ubifs_sb
, filename
);
806 inode
= ubifs_iget(ubifs_sb
, inum
);
808 printf("%s: Error reading inode %ld!\n", __func__
, inum
);
809 err
= PTR_ERR(inode
);
814 * If no size was specified or if size bigger than filesize
815 * set size to filesize
817 if ((size
== 0) || (size
> inode
->i_size
))
818 size
= inode
->i_size
;
820 count
= (size
+ UBIFS_BLOCK_SIZE
- 1) >> UBIFS_BLOCK_SHIFT
;
821 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
822 filename
, addr
, size
, size
);
824 page
.addr
= (void *)addr
;
827 for (i
= 0; i
< count
; i
++) {
829 * Make sure to not read beyond the requested size
831 if (((i
+ 1) == count
) && (size
< inode
->i_size
))
832 last_block_size
= size
- (i
* PAGE_SIZE
);
834 err
= do_readpage(c
, inode
, &page
, last_block_size
);
838 page
.addr
+= PAGE_SIZE
;
843 printf("Error reading file '%s'\n", filename
);
845 setenv_hex("filesize", size
);
852 ubi_close_volume(c
->ubi
);