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>
29 DECLARE_GLOBAL_DATA_PTR;
34 * We need a wrapper for zunzip() because the parameters are
35 * incompatible with the lzo decompressor.
37 static int gzip_decompress(const unsigned char *in, size_t in_len,
38 unsigned char *out, size_t *out_len)
40 unsigned long len = in_len;
41 return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
44 /* Fake description object for the "none" compressor */
45 static struct ubifs_compressor none_compr = {
46 .compr_type = UBIFS_COMPR_NONE,
47 .name = "no compression",
52 static struct ubifs_compressor lzo_compr = {
53 .compr_type = UBIFS_COMPR_LZO,
56 .decompress = lzo1x_decompress_safe,
59 static struct ubifs_compressor zlib_compr = {
60 .compr_type = UBIFS_COMPR_ZLIB,
62 .capi_name = "deflate",
63 .decompress = gzip_decompress,
66 /* All UBIFS compressors */
67 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
70 * ubifs_decompress - decompress data.
71 * @in_buf: data to decompress
72 * @in_len: length of the data to decompress
73 * @out_buf: output buffer where decompressed data should
74 * @out_len: output length is returned here
75 * @compr_type: type of compression
77 * This function decompresses data from buffer @in_buf into buffer @out_buf.
78 * The length of the uncompressed data is returned in @out_len. This functions
79 * returns %0 on success or a negative error code on failure.
81 int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
82 int *out_len, int compr_type)
85 struct ubifs_compressor *compr;
87 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
88 ubifs_err("invalid compression type %d", compr_type);
92 compr = ubifs_compressors[compr_type];
94 if (unlikely(!compr->capi_name)) {
95 ubifs_err("%s compression is not compiled in", compr->name);
99 if (compr_type == UBIFS_COMPR_NONE) {
100 memcpy(out_buf, in_buf, in_len);
105 err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
107 ubifs_err("cannot decompress %d bytes, compressor %s, "
108 "error %d", in_len, compr->name, err);
114 * compr_init - initialize a compressor.
115 * @compr: compressor description object
117 * This function initializes the requested compressor and returns zero in case
118 * of success or a negative error code in case of failure.
120 static int __init compr_init(struct ubifs_compressor *compr)
122 ubifs_compressors[compr->compr_type] = compr;
124 #ifdef CONFIG_NEEDS_MANUAL_RELOC
125 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
126 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
127 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
134 * ubifs_compressors_init - initialize UBIFS compressors.
136 * This function initializes the compressor which were compiled in. Returns
137 * zero in case of success and a negative error code in case of failure.
139 int __init ubifs_compressors_init(void)
143 err = compr_init(&lzo_compr);
147 err = compr_init(&zlib_compr);
151 err = compr_init(&none_compr);
162 static int filldir(struct ubifs_info *c, const char *name, int namlen,
163 u64 ino, unsigned int d_type)
169 case UBIFS_ITYPE_REG:
172 case UBIFS_ITYPE_DIR:
175 case UBIFS_ITYPE_LNK:
183 inode = ubifs_iget(c->vfs_sb, ino);
185 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
186 __func__, ino, inode);
189 ctime_r((time_t *)&inode->i_mtime, filetime);
190 printf("%9lld %24.24s ", inode->i_size, filetime);
193 printf("%s\n", name);
198 static int ubifs_printdir(struct file *file, void *dirent)
203 struct ubifs_dent_node *dent;
204 struct inode *dir = file->f_path.dentry->d_inode;
205 struct ubifs_info *c = dir->i_sb->s_fs_info;
207 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
209 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
211 * The directory was seek'ed to a senseless position or there
212 * are no more entries.
216 if (file->f_pos == 1) {
217 /* Find the first entry in TNC and save it */
218 lowest_dent_key(c, &key, dir->i_ino);
220 dent = ubifs_tnc_next_ent(c, &key, &nm);
226 file->f_pos = key_hash_flash(c, &dent->key);
227 file->private_data = dent;
230 dent = file->private_data;
233 * The directory was seek'ed to and is now readdir'ed.
234 * Find the entry corresponding to @file->f_pos or the
237 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
239 dent = ubifs_tnc_next_ent(c, &key, &nm);
244 file->f_pos = key_hash_flash(c, &dent->key);
245 file->private_data = dent;
249 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
250 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
251 key_hash_flash(c, &dent->key));
252 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
254 nm.len = le16_to_cpu(dent->nlen);
255 over = filldir(c, (char *)dent->name, nm.len,
256 le64_to_cpu(dent->inum), dent->type);
260 /* Switch to the next entry */
261 key_read(c, &dent->key, &key);
262 nm.name = (char *)dent->name;
263 dent = ubifs_tnc_next_ent(c, &key, &nm);
269 kfree(file->private_data);
270 file->f_pos = key_hash_flash(c, &dent->key);
271 file->private_data = dent;
276 if (err != -ENOENT) {
277 ubifs_err("cannot find next direntry, error %d", err);
281 kfree(file->private_data);
282 file->private_data = NULL;
287 static int ubifs_finddir(struct super_block *sb, char *dirname,
288 unsigned long root_inum, unsigned long *inum)
293 struct ubifs_dent_node *dent;
294 struct ubifs_info *c;
296 struct dentry *dentry;
299 file = kzalloc(sizeof(struct file), 0);
300 dentry = kzalloc(sizeof(struct dentry), 0);
301 dir = kzalloc(sizeof(struct inode), 0);
302 if (!file || !dentry || !dir) {
303 printf("%s: Error, no memory for malloc!\n", __func__);
309 file->f_path.dentry = dentry;
310 file->f_path.dentry->d_parent = dentry;
311 file->f_path.dentry->d_inode = dir;
312 file->f_path.dentry->d_inode->i_ino = root_inum;
315 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
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 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
331 dent->name, (unsigned long long)le64_to_cpu(dent->inum),
332 key_hash_flash(c, &dent->key));
333 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
335 nm.len = le16_to_cpu(dent->nlen);
336 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
337 (strlen(dirname) == nm.len)) {
338 *inum = le64_to_cpu(dent->inum);
342 /* Switch to the next entry */
343 key_read(c, &dent->key, &key);
344 nm.name = (char *)dent->name;
345 dent = ubifs_tnc_next_ent(c, &key, &nm);
351 kfree(file->private_data);
352 file->f_pos = key_hash_flash(c, &dent->key);
353 file->private_data = dent;
358 if (err != -ENOENT) {
359 ubifs_err("cannot find next direntry, error %d", err);
363 if (file->private_data)
364 kfree(file->private_data);
375 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
380 char symlinkpath[128];
382 unsigned long root_inum = 1;
384 int symlink_count = 0; /* Don't allow symlink recursion */
387 strcpy(fpath, filename);
389 /* Remove all leading slashes */
394 * Handle root-direcoty ('/')
397 if (!name || *name == '\0')
402 struct ubifs_inode *ui;
404 /* Extract the actual part from the pathname. */
405 next = strchr(name, '/');
407 /* Remove all leading slashes. */
412 ret = ubifs_finddir(sb, name, root_inum, &inum);
415 inode = ubifs_iget(sb, inum);
419 ui = ubifs_inode(inode);
421 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
424 /* We have some sort of symlink recursion, bail out */
425 if (symlink_count++ > 8) {
426 printf("Symlink recursion, aborting\n");
429 memcpy(link_name, ui->data, ui->data_len);
430 link_name[ui->data_len] = '\0';
432 if (link_name[0] == '/') {
433 /* Absolute path, redo everything without
434 * the leading slash */
435 next = name = link_name + 1;
439 /* Relative to cur dir */
440 sprintf(buf, "%s/%s",
441 link_name, next == NULL ? "" : next);
442 memcpy(symlinkpath, buf, sizeof(buf));
443 next = name = symlinkpath;
448 * Check if directory with this name exists
451 /* Found the node! */
452 if (!next || *next == '\0')
462 int ubifs_ls(char *filename)
464 struct ubifs_info *c = ubifs_sb->s_fs_info;
466 struct dentry *dentry;
472 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
473 inum = ubifs_findfile(ubifs_sb, filename);
479 file = kzalloc(sizeof(struct file), 0);
480 dentry = kzalloc(sizeof(struct dentry), 0);
481 dir = kzalloc(sizeof(struct inode), 0);
482 if (!file || !dentry || !dir) {
483 printf("%s: Error, no memory for malloc!\n", __func__);
488 dir->i_sb = ubifs_sb;
489 file->f_path.dentry = dentry;
490 file->f_path.dentry->d_parent = dentry;
491 file->f_path.dentry->d_inode = dir;
492 file->f_path.dentry->d_inode->i_ino = inum;
494 file->private_data = NULL;
495 ubifs_printdir(file, dirent);
506 ubi_close_volume(c->ubi);
516 static inline void *kmap(struct page *page)
521 static int read_block(struct inode *inode, void *addr, unsigned int block,
522 struct ubifs_data_node *dn)
524 struct ubifs_info *c = inode->i_sb->s_fs_info;
525 int err, len, out_len;
529 data_key_init(c, &key, inode->i_ino, block);
530 err = ubifs_tnc_lookup(c, &key, dn);
533 /* Not found, so it must be a hole */
534 memset(addr, 0, UBIFS_BLOCK_SIZE);
538 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
540 len = le32_to_cpu(dn->size);
541 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
544 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
545 out_len = UBIFS_BLOCK_SIZE;
546 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
547 le16_to_cpu(dn->compr_type));
548 if (err || len != out_len)
552 * Data length can be less than a full block, even for blocks that are
553 * not the last in the file (e.g., as a result of making a hole and
554 * appending data). Ensure that the remainder is zeroed out.
556 if (len < UBIFS_BLOCK_SIZE)
557 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
562 ubifs_err("bad data node (block %u, inode %lu)",
563 block, inode->i_ino);
564 dbg_dump_node(c, dn);
568 static int do_readpage(struct ubifs_info *c, struct inode *inode,
569 struct page *page, int last_block_size)
573 unsigned int block, beyond;
574 struct ubifs_data_node *dn;
575 loff_t i_size = inode->i_size;
577 dbg_gen("ino %lu, pg %lu, i_size %lld",
578 inode->i_ino, page->index, i_size);
582 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
583 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
584 if (block >= beyond) {
585 /* Reading beyond inode */
586 memset(addr, 0, PAGE_CACHE_SIZE);
590 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
598 if (block >= beyond) {
599 /* Reading beyond inode */
601 memset(addr, 0, UBIFS_BLOCK_SIZE);
604 * Reading last block? Make sure to not write beyond
605 * the requested size in the destination buffer.
607 if (((block + 1) == beyond) || last_block_size) {
612 * We need to buffer the data locally for the
613 * last block. This is to not pad the
614 * destination area to a multiple of
617 buff = malloc(UBIFS_BLOCK_SIZE);
619 printf("%s: Error, malloc fails!\n",
625 /* Read block-size into temp buffer */
626 ret = read_block(inode, buff, block, dn);
629 if (err != -ENOENT) {
636 dlen = last_block_size;
638 dlen = le32_to_cpu(dn->size);
640 /* Now copy required size back to dest */
641 memcpy(addr, buff, dlen);
645 ret = read_block(inode, addr, block, dn);
653 if (++i >= UBIFS_BLOCKS_PER_PAGE)
656 addr += UBIFS_BLOCK_SIZE;
659 if (err == -ENOENT) {
660 /* Not found, so it must be a hole */
664 ubifs_err("cannot read page %lu of inode %lu, error %d",
665 page->index, inode->i_ino, err);
679 int ubifs_load(char *filename, u32 addr, u32 size)
681 struct ubifs_info *c = ubifs_sb->s_fs_info;
688 int last_block_size = 0;
691 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
692 /* ubifs_findfile will resolve symlinks, so we know that we get
693 * the real file here */
694 inum = ubifs_findfile(ubifs_sb, filename);
703 inode = ubifs_iget(ubifs_sb, inum);
705 printf("%s: Error reading inode %ld!\n", __func__, inum);
706 err = PTR_ERR(inode);
711 * If no size was specified or if size bigger than filesize
712 * set size to filesize
714 if ((size == 0) || (size > inode->i_size))
715 size = inode->i_size;
717 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
718 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
719 filename, addr, size, size);
721 page.addr = (void *)addr;
724 for (i = 0; i < count; i++) {
726 * Make sure to not read beyond the requested size
728 if (((i + 1) == count) && (size < inode->i_size))
729 last_block_size = size - (i * PAGE_SIZE);
731 err = do_readpage(c, inode, &page, last_block_size);
735 page.addr += PAGE_SIZE;
740 printf("Error reading file '%s'\n", filename);
742 sprintf(buf, "%X", size);
743 setenv("filesize", buf);
750 ubi_close_volume(c->ubi);