blob: 91fc574a92aa8020022c3cb2b5cebc9c84834033 [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
6 * (C) Copyright 2008-2009
7 * Stefan Roese, DENX Software Engineering, sr@denx.de.
8 *
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.
12 *
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
16 * more details.
17 *
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
21 *
22 * Authors: Artem Bityutskiy (Битюцкий Артём)
23 * Adrian Hunter
24 */
25
26#include "ubifs.h"
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020027#include <u-boot/zlib.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +010028
29#if !defined(CONFIG_SYS_64BIT_VSPRINTF)
30#warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
31#endif
32
33DECLARE_GLOBAL_DATA_PTR;
34
35/* compress.c */
36
37/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020038 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010039 * incompatible with the lzo decompressor.
40 */
41static int gzip_decompress(const unsigned char *in, size_t in_len,
42 unsigned char *out, size_t *out_len)
43{
44 unsigned long len = in_len;
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020045 return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010046}
47
48/* Fake description object for the "none" compressor */
49static struct ubifs_compressor none_compr = {
50 .compr_type = UBIFS_COMPR_NONE,
51 .name = "no compression",
52 .capi_name = "",
53 .decompress = NULL,
54};
55
56static struct ubifs_compressor lzo_compr = {
57 .compr_type = UBIFS_COMPR_LZO,
58 .name = "LZO",
59 .capi_name = "lzo",
60 .decompress = lzo1x_decompress_safe,
61};
62
63static struct ubifs_compressor zlib_compr = {
64 .compr_type = UBIFS_COMPR_ZLIB,
65 .name = "zlib",
66 .capi_name = "deflate",
67 .decompress = gzip_decompress,
68};
69
70/* All UBIFS compressors */
71struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
72
73/**
74 * ubifs_decompress - decompress data.
75 * @in_buf: data to decompress
76 * @in_len: length of the data to decompress
77 * @out_buf: output buffer where decompressed data should
78 * @out_len: output length is returned here
79 * @compr_type: type of compression
80 *
81 * This function decompresses data from buffer @in_buf into buffer @out_buf.
82 * The length of the uncompressed data is returned in @out_len. This functions
83 * returns %0 on success or a negative error code on failure.
84 */
85int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
86 int *out_len, int compr_type)
87{
88 int err;
89 struct ubifs_compressor *compr;
90
91 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
92 ubifs_err("invalid compression type %d", compr_type);
93 return -EINVAL;
94 }
95
96 compr = ubifs_compressors[compr_type];
97
98 if (unlikely(!compr->capi_name)) {
99 ubifs_err("%s compression is not compiled in", compr->name);
100 return -EINVAL;
101 }
102
103 if (compr_type == UBIFS_COMPR_NONE) {
104 memcpy(out_buf, in_buf, in_len);
105 *out_len = in_len;
106 return 0;
107 }
108
109 err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
110 if (err)
111 ubifs_err("cannot decompress %d bytes, compressor %s, "
112 "error %d", in_len, compr->name, err);
113
114 return err;
115}
116
117/**
118 * compr_init - initialize a compressor.
119 * @compr: compressor description object
120 *
121 * This function initializes the requested compressor and returns zero in case
122 * of success or a negative error code in case of failure.
123 */
124static int __init compr_init(struct ubifs_compressor *compr)
125{
126 ubifs_compressors[compr->compr_type] = compr;
127 ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
128 ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
129 ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
130 return 0;
131}
132
133/**
134 * ubifs_compressors_init - initialize UBIFS compressors.
135 *
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.
138 */
139int __init ubifs_compressors_init(void)
140{
141 int err;
142
143 err = compr_init(&lzo_compr);
144 if (err)
145 return err;
146
147 err = compr_init(&zlib_compr);
148 if (err)
149 return err;
150
Michael Lawnickb0617012009-03-19 10:06:41 +0100151 err = compr_init(&none_compr);
152 if (err)
153 return err;
154
Stefan Roese2fc10f62009-03-19 15:35:05 +0100155 return 0;
156}
157
158/*
159 * ubifsls...
160 */
161
162static int filldir(struct ubifs_info *c, const char *name, int namlen,
163 u64 ino, unsigned int d_type)
164{
165 struct inode *inode;
166 char filetime[32];
167
168 switch (d_type) {
169 case UBIFS_ITYPE_REG:
170 printf("\t");
171 break;
172 case UBIFS_ITYPE_DIR:
173 printf("<DIR>\t");
174 break;
175 case UBIFS_ITYPE_LNK:
176 printf("<LNK>\t");
177 break;
178 default:
179 printf("other\t");
180 break;
181 }
182
183 inode = ubifs_iget(c->vfs_sb, ino);
184 if (IS_ERR(inode)) {
185 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
186 __func__, ino, inode);
187 return -1;
188 }
189 ctime_r((time_t *)&inode->i_mtime, filetime);
190 printf("%9lld %24.24s ", inode->i_size, filetime);
191 ubifs_iput(inode);
192
193 printf("%s\n", name);
194
195 return 0;
196}
197
198static int ubifs_printdir(struct file *file, void *dirent)
199{
200 int err, over = 0;
201 struct qstr nm;
202 union ubifs_key key;
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;
206
207 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
208
209 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
210 /*
211 * The directory was seek'ed to a senseless position or there
212 * are no more entries.
213 */
214 return 0;
215
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);
219 nm.name = NULL;
220 dent = ubifs_tnc_next_ent(c, &key, &nm);
221 if (IS_ERR(dent)) {
222 err = PTR_ERR(dent);
223 goto out;
224 }
225
226 file->f_pos = key_hash_flash(c, &dent->key);
227 file->private_data = dent;
228 }
229
230 dent = file->private_data;
231 if (!dent) {
232 /*
233 * The directory was seek'ed to and is now readdir'ed.
234 * Find the entry corresponding to @file->f_pos or the
235 * closest one.
236 */
237 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
238 nm.name = NULL;
239 dent = ubifs_tnc_next_ent(c, &key, &nm);
240 if (IS_ERR(dent)) {
241 err = PTR_ERR(dent);
242 goto out;
243 }
244 file->f_pos = key_hash_flash(c, &dent->key);
245 file->private_data = dent;
246 }
247
248 while (1) {
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);
253
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);
257 if (over)
258 return 0;
259
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);
264 if (IS_ERR(dent)) {
265 err = PTR_ERR(dent);
266 goto out;
267 }
268
269 kfree(file->private_data);
270 file->f_pos = key_hash_flash(c, &dent->key);
271 file->private_data = dent;
272 cond_resched();
273 }
274
275out:
276 if (err != -ENOENT) {
277 ubifs_err("cannot find next direntry, error %d", err);
278 return err;
279 }
280
281 kfree(file->private_data);
282 file->private_data = NULL;
283 file->f_pos = 2;
284 return 0;
285}
286
287static int ubifs_finddir(struct super_block *sb, char *dirname,
288 unsigned long root_inum, unsigned long *inum)
289{
290 int err;
291 struct qstr nm;
292 union ubifs_key key;
293 struct ubifs_dent_node *dent;
294 struct ubifs_info *c;
295 struct file *file;
296 struct dentry *dentry;
297 struct inode *dir;
298
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__);
304 err = -ENOMEM;
305 goto out;
306 }
307
308 dir->i_sb = sb;
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;
313 c = sb->s_fs_info;
314
315 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
316
317 /* Find the first entry in TNC and save it */
318 lowest_dent_key(c, &key, dir->i_ino);
319 nm.name = NULL;
320 dent = ubifs_tnc_next_ent(c, &key, &nm);
321 if (IS_ERR(dent)) {
322 err = PTR_ERR(dent);
323 goto out;
324 }
325
326 file->f_pos = key_hash_flash(c, &dent->key);
327 file->private_data = dent;
328
329 while (1) {
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);
334
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);
339 return 1;
340 }
341
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);
346 if (IS_ERR(dent)) {
347 err = PTR_ERR(dent);
348 goto out;
349 }
350
351 kfree(file->private_data);
352 file->f_pos = key_hash_flash(c, &dent->key);
353 file->private_data = dent;
354 cond_resched();
355 }
356
357out:
358 if (err != -ENOENT) {
359 ubifs_err("cannot find next direntry, error %d", err);
360 return err;
361 }
362
363 if (file)
364 free(file);
365 if (dentry)
366 free(dentry);
367 if (dir)
368 free(dir);
369
370 if (file->private_data)
371 kfree(file->private_data);
372 file->private_data = NULL;
373 file->f_pos = 2;
374 return 0;
375}
376
377static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
378{
379 int ret;
380 char *next;
381 char fpath[128];
382 char *name = fpath;
383 unsigned long root_inum = 1;
384 unsigned long inum;
385
386 strcpy(fpath, filename);
387
388 /* Remove all leading slashes */
389 while (*name == '/')
390 name++;
391
392 /*
393 * Handle root-direcoty ('/')
394 */
395 inum = root_inum;
396 if (!name || *name == '\0')
397 return inum;
398
399 for (;;) {
400 /* Extract the actual part from the pathname. */
401 next = strchr(name, '/');
402 if (next) {
403 /* Remove all leading slashes. */
404 while (*next == '/')
405 *(next++) = '\0';
406 }
407
408 ret = ubifs_finddir(sb, name, root_inum, &inum);
409
410 /*
411 * Check if directory with this name exists
412 */
413
414 /* Found the node! */
415 if (!next || *next == '\0') {
416 if (ret)
417 return inum;
418
419 break;
420 }
421
422 root_inum = inum;
423 name = next;
424 }
425
426 return 0;
427}
428
429int ubifs_ls(char *filename)
430{
431 struct ubifs_info *c = ubifs_sb->s_fs_info;
432 struct file *file;
433 struct dentry *dentry;
434 struct inode *dir;
435 void *dirent = NULL;
436 unsigned long inum;
437 int ret = 0;
438
439 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
440 inum = ubifs_findfile(ubifs_sb, filename);
441 if (!inum) {
442 ret = -1;
443 goto out;
444 }
445
446 file = kzalloc(sizeof(struct file), 0);
447 dentry = kzalloc(sizeof(struct dentry), 0);
448 dir = kzalloc(sizeof(struct inode), 0);
449 if (!file || !dentry || !dir) {
450 printf("%s: Error, no memory for malloc!\n", __func__);
451 ret = -ENOMEM;
452 goto out_mem;
453 }
454
455 dir->i_sb = ubifs_sb;
456 file->f_path.dentry = dentry;
457 file->f_path.dentry->d_parent = dentry;
458 file->f_path.dentry->d_inode = dir;
459 file->f_path.dentry->d_inode->i_ino = inum;
460 file->f_pos = 1;
461 file->private_data = NULL;
462 ubifs_printdir(file, dirent);
463
464out_mem:
465 if (file)
466 free(file);
467 if (dentry)
468 free(dentry);
469 if (dir)
470 free(dir);
471
472out:
473 ubi_close_volume(c->ubi);
474 return ret;
475}
476
477/*
478 * ubifsload...
479 */
480
481/* file.c */
482
483static inline void *kmap(struct page *page)
484{
485 return page->addr;
486}
487
488static int read_block(struct inode *inode, void *addr, unsigned int block,
489 struct ubifs_data_node *dn)
490{
491 struct ubifs_info *c = inode->i_sb->s_fs_info;
492 int err, len, out_len;
493 union ubifs_key key;
494 unsigned int dlen;
495
496 data_key_init(c, &key, inode->i_ino, block);
497 err = ubifs_tnc_lookup(c, &key, dn);
498 if (err) {
499 if (err == -ENOENT)
500 /* Not found, so it must be a hole */
501 memset(addr, 0, UBIFS_BLOCK_SIZE);
502 return err;
503 }
504
505 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
506
507 len = le32_to_cpu(dn->size);
508 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
509 goto dump;
510
511 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
512 out_len = UBIFS_BLOCK_SIZE;
513 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
514 le16_to_cpu(dn->compr_type));
515 if (err || len != out_len)
516 goto dump;
517
518 /*
519 * Data length can be less than a full block, even for blocks that are
520 * not the last in the file (e.g., as a result of making a hole and
521 * appending data). Ensure that the remainder is zeroed out.
522 */
523 if (len < UBIFS_BLOCK_SIZE)
524 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
525
526 return 0;
527
528dump:
529 ubifs_err("bad data node (block %u, inode %lu)",
530 block, inode->i_ino);
531 dbg_dump_node(c, dn);
532 return -EINVAL;
533}
534
535static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
536{
537 void *addr;
538 int err = 0, i;
539 unsigned int block, beyond;
540 struct ubifs_data_node *dn;
541 loff_t i_size = inode->i_size;
542
543 dbg_gen("ino %lu, pg %lu, i_size %lld",
544 inode->i_ino, page->index, i_size);
545
546 addr = kmap(page);
547
548 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
549 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
550 if (block >= beyond) {
551 /* Reading beyond inode */
552 memset(addr, 0, PAGE_CACHE_SIZE);
553 goto out;
554 }
555
556 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
557 if (!dn) {
558 err = -ENOMEM;
559 goto error;
560 }
561
562 i = 0;
563 while (1) {
564 int ret;
565
566 if (block >= beyond) {
567 /* Reading beyond inode */
568 err = -ENOENT;
569 memset(addr, 0, UBIFS_BLOCK_SIZE);
570 } else {
571 ret = read_block(inode, addr, block, dn);
572 if (ret) {
573 err = ret;
574 if (err != -ENOENT)
575 break;
576 } else if (block + 1 == beyond) {
577 int dlen = le32_to_cpu(dn->size);
578 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
579
580 if (ilen && ilen < dlen)
581 memset(addr + ilen, 0, dlen - ilen);
582 }
583 }
584 if (++i >= UBIFS_BLOCKS_PER_PAGE)
585 break;
586 block += 1;
587 addr += UBIFS_BLOCK_SIZE;
588 }
589 if (err) {
590 if (err == -ENOENT) {
591 /* Not found, so it must be a hole */
592 dbg_gen("hole");
593 goto out_free;
594 }
595 ubifs_err("cannot read page %lu of inode %lu, error %d",
596 page->index, inode->i_ino, err);
597 goto error;
598 }
599
600out_free:
601 kfree(dn);
602out:
603 return 0;
604
605error:
606 kfree(dn);
607 return err;
608}
609
610int ubifs_load(char *filename, u32 addr, u32 size)
611{
612 struct ubifs_info *c = ubifs_sb->s_fs_info;
613 unsigned long inum;
614 struct inode *inode;
615 struct page page;
616 int err = 0;
617 int i;
618 int count;
619 char link_name[64];
620 struct ubifs_inode *ui;
621
622 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
623 inum = ubifs_findfile(ubifs_sb, filename);
624 if (!inum) {
625 err = -1;
626 goto out;
627 }
628
629 /*
630 * Read file inode
631 */
632 inode = ubifs_iget(ubifs_sb, inum);
633 if (IS_ERR(inode)) {
634 printf("%s: Error reading inode %ld!\n", __func__, inum);
635 err = PTR_ERR(inode);
636 goto out;
637 }
638
639 /*
640 * Check for symbolic link
641 */
642 ui = ubifs_inode(inode);
643 if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
644 memcpy(link_name, ui->data, ui->data_len);
Ricardo Ribalda Delgado3ed24a22009-04-27 09:13:31 +0200645 link_name[ui->data_len] = '\0';
Stefan Roese2fc10f62009-03-19 15:35:05 +0100646 printf("%s is linked to %s!\n", filename, link_name);
647 ubifs_iput(inode);
648
649 /*
650 * Now we have the "real" filename, call ubifs_load()
651 * again (recursive call) to load this file instead
652 */
653 return ubifs_load(link_name, addr, size);
654 }
655
656 /*
657 * If no size was specified or if size bigger than filesize
658 * set size to filesize
659 */
660 if ((size == 0) || (size > inode->i_size))
661 size = inode->i_size;
662
663 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
664 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
665 filename, addr, size, size);
666
667 page.addr = (void *)addr;
668 page.index = 0;
669 page.inode = inode;
670 for (i = 0; i < count; i++) {
671 err = do_readpage(c, inode, &page);
672 if (err)
673 break;
674
675 page.addr += PAGE_SIZE;
676 page.index++;
677 }
678
679 if (err)
680 printf("Error reading file '%s'\n", filename);
681 else
682 printf("Done\n");
683
684 ubifs_iput(inode);
685
686out:
687 ubi_close_volume(c->ubi);
688 return err;
689}