blob: 5a5c739b53f5caccf1936efa7bbe93178f11494e [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 *
Stefan Roese28ea29f2010-11-01 17:28:00 +01006 * (C) Copyright 2008-2010
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 * 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
Stefan Roese2fc10f62009-03-19 15:35:05 +010029DECLARE_GLOBAL_DATA_PTR;
30
31/* compress.c */
32
33/*
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020034 * We need a wrapper for zunzip() because the parameters are
Stefan Roese2fc10f62009-03-19 15:35:05 +010035 * incompatible with the lzo decompressor.
36 */
37static int gzip_decompress(const unsigned char *in, size_t in_len,
38 unsigned char *out, size_t *out_len)
39{
40 unsigned long len = in_len;
Ricardo Ribalda Delgadoa7a743e2009-04-27 18:33:33 +020041 return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +010042}
43
44/* Fake description object for the "none" compressor */
45static struct ubifs_compressor none_compr = {
46 .compr_type = UBIFS_COMPR_NONE,
47 .name = "no compression",
48 .capi_name = "",
49 .decompress = NULL,
50};
51
52static struct ubifs_compressor lzo_compr = {
53 .compr_type = UBIFS_COMPR_LZO,
54 .name = "LZO",
55 .capi_name = "lzo",
56 .decompress = lzo1x_decompress_safe,
57};
58
59static struct ubifs_compressor zlib_compr = {
60 .compr_type = UBIFS_COMPR_ZLIB,
61 .name = "zlib",
62 .capi_name = "deflate",
63 .decompress = gzip_decompress,
64};
65
66/* All UBIFS compressors */
67struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
68
69/**
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
76 *
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.
80 */
81int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
82 int *out_len, int compr_type)
83{
84 int err;
85 struct ubifs_compressor *compr;
86
87 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
88 ubifs_err("invalid compression type %d", compr_type);
89 return -EINVAL;
90 }
91
92 compr = ubifs_compressors[compr_type];
93
94 if (unlikely(!compr->capi_name)) {
95 ubifs_err("%s compression is not compiled in", compr->name);
96 return -EINVAL;
97 }
98
99 if (compr_type == UBIFS_COMPR_NONE) {
100 memcpy(out_buf, in_buf, in_len);
101 *out_len = in_len;
102 return 0;
103 }
104
105 err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
106 if (err)
107 ubifs_err("cannot decompress %d bytes, compressor %s, "
108 "error %d", in_len, compr->name, err);
109
110 return err;
111}
112
113/**
114 * compr_init - initialize a compressor.
115 * @compr: compressor description object
116 *
117 * This function initializes the requested compressor and returns zero in case
118 * of success or a negative error code in case of failure.
119 */
120static int __init compr_init(struct ubifs_compressor *compr)
121{
122 ubifs_compressors[compr->compr_type] = compr;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500123
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200124#ifdef CONFIG_NEEDS_MANUAL_RELOC
Stefan Roese2fc10f62009-03-19 15:35:05 +0100125 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;
Peter Tyser9057cbf2009-09-21 11:20:36 -0500128#endif
129
Stefan Roese2fc10f62009-03-19 15:35:05 +0100130 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];
Simon Kagstrom877501a2009-09-15 09:53:29 +0200382 char symlinkpath[128];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100383 char *name = fpath;
384 unsigned long root_inum = 1;
385 unsigned long inum;
Simon Kagstrom877501a2009-09-15 09:53:29 +0200386 int symlink_count = 0; /* Don't allow symlink recursion */
Ricardo Ribalda Delgado4a2e69c2010-12-02 15:02:35 +0100387 char link_name[64];
Stefan Roese2fc10f62009-03-19 15:35:05 +0100388
389 strcpy(fpath, filename);
390
391 /* Remove all leading slashes */
392 while (*name == '/')
393 name++;
394
395 /*
396 * Handle root-direcoty ('/')
397 */
398 inum = root_inum;
399 if (!name || *name == '\0')
400 return inum;
401
402 for (;;) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200403 struct inode *inode;
404 struct ubifs_inode *ui;
405
Stefan Roese2fc10f62009-03-19 15:35:05 +0100406 /* Extract the actual part from the pathname. */
407 next = strchr(name, '/');
408 if (next) {
409 /* Remove all leading slashes. */
410 while (*next == '/')
411 *(next++) = '\0';
412 }
413
414 ret = ubifs_finddir(sb, name, root_inum, &inum);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200415 if (!ret)
416 return 0;
417 inode = ubifs_iget(sb, inum);
418
419 if (!inode)
420 return 0;
421 ui = ubifs_inode(inode);
422
423 if ((inode->i_mode & S_IFMT) == S_IFLNK) {
Simon Kagstrom877501a2009-09-15 09:53:29 +0200424 char buf[128];
425
426 /* We have some sort of symlink recursion, bail out */
427 if (symlink_count++ > 8) {
428 printf("Symlink recursion, aborting\n");
429 return 0;
430 }
431 memcpy(link_name, ui->data, ui->data_len);
432 link_name[ui->data_len] = '\0';
433
434 if (link_name[0] == '/') {
435 /* Absolute path, redo everything without
436 * the leading slash */
437 next = name = link_name + 1;
438 root_inum = 1;
439 continue;
440 }
441 /* Relative to cur dir */
Simon Kagstrom84167362009-09-25 14:05:57 +0200442 sprintf(buf, "%s/%s",
Simon Kagstrom877501a2009-09-15 09:53:29 +0200443 link_name, next == NULL ? "" : next);
444 memcpy(symlinkpath, buf, sizeof(buf));
445 next = name = symlinkpath;
446 continue;
447 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100448
449 /*
450 * Check if directory with this name exists
451 */
452
453 /* Found the node! */
Simon Kagstrom877501a2009-09-15 09:53:29 +0200454 if (!next || *next == '\0')
455 return inum;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100456
457 root_inum = inum;
458 name = next;
459 }
460
461 return 0;
462}
463
464int ubifs_ls(char *filename)
465{
466 struct ubifs_info *c = ubifs_sb->s_fs_info;
467 struct file *file;
468 struct dentry *dentry;
469 struct inode *dir;
470 void *dirent = NULL;
471 unsigned long inum;
472 int ret = 0;
473
474 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
475 inum = ubifs_findfile(ubifs_sb, filename);
476 if (!inum) {
477 ret = -1;
478 goto out;
479 }
480
481 file = kzalloc(sizeof(struct file), 0);
482 dentry = kzalloc(sizeof(struct dentry), 0);
483 dir = kzalloc(sizeof(struct inode), 0);
484 if (!file || !dentry || !dir) {
485 printf("%s: Error, no memory for malloc!\n", __func__);
486 ret = -ENOMEM;
487 goto out_mem;
488 }
489
490 dir->i_sb = ubifs_sb;
491 file->f_path.dentry = dentry;
492 file->f_path.dentry->d_parent = dentry;
493 file->f_path.dentry->d_inode = dir;
494 file->f_path.dentry->d_inode->i_ino = inum;
495 file->f_pos = 1;
496 file->private_data = NULL;
497 ubifs_printdir(file, dirent);
498
499out_mem:
500 if (file)
501 free(file);
502 if (dentry)
503 free(dentry);
504 if (dir)
505 free(dir);
506
507out:
508 ubi_close_volume(c->ubi);
509 return ret;
510}
511
512/*
513 * ubifsload...
514 */
515
516/* file.c */
517
518static inline void *kmap(struct page *page)
519{
520 return page->addr;
521}
522
523static int read_block(struct inode *inode, void *addr, unsigned int block,
524 struct ubifs_data_node *dn)
525{
526 struct ubifs_info *c = inode->i_sb->s_fs_info;
527 int err, len, out_len;
528 union ubifs_key key;
529 unsigned int dlen;
530
531 data_key_init(c, &key, inode->i_ino, block);
532 err = ubifs_tnc_lookup(c, &key, dn);
533 if (err) {
534 if (err == -ENOENT)
535 /* Not found, so it must be a hole */
536 memset(addr, 0, UBIFS_BLOCK_SIZE);
537 return err;
538 }
539
540 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
541
542 len = le32_to_cpu(dn->size);
543 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
544 goto dump;
545
546 dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
547 out_len = UBIFS_BLOCK_SIZE;
548 err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
549 le16_to_cpu(dn->compr_type));
550 if (err || len != out_len)
551 goto dump;
552
553 /*
554 * Data length can be less than a full block, even for blocks that are
555 * not the last in the file (e.g., as a result of making a hole and
556 * appending data). Ensure that the remainder is zeroed out.
557 */
558 if (len < UBIFS_BLOCK_SIZE)
559 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
560
561 return 0;
562
563dump:
564 ubifs_err("bad data node (block %u, inode %lu)",
565 block, inode->i_ino);
566 dbg_dump_node(c, dn);
567 return -EINVAL;
568}
569
Stefan Roese28ea29f2010-11-01 17:28:00 +0100570static int do_readpage(struct ubifs_info *c, struct inode *inode,
571 struct page *page, int last_block_size)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100572{
573 void *addr;
574 int err = 0, i;
575 unsigned int block, beyond;
576 struct ubifs_data_node *dn;
577 loff_t i_size = inode->i_size;
578
579 dbg_gen("ino %lu, pg %lu, i_size %lld",
580 inode->i_ino, page->index, i_size);
581
582 addr = kmap(page);
583
584 block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
585 beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
586 if (block >= beyond) {
587 /* Reading beyond inode */
588 memset(addr, 0, PAGE_CACHE_SIZE);
589 goto out;
590 }
591
592 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
Daniel Mackb5775802009-06-04 19:44:12 +0200593 if (!dn)
594 return -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100595
596 i = 0;
597 while (1) {
598 int ret;
599
600 if (block >= beyond) {
601 /* Reading beyond inode */
602 err = -ENOENT;
603 memset(addr, 0, UBIFS_BLOCK_SIZE);
604 } else {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100605 /*
606 * Reading last block? Make sure to not write beyond
607 * the requested size in the destination buffer.
608 */
609 if (((block + 1) == beyond) || last_block_size) {
610 void *buff;
611 int dlen;
612
613 /*
614 * We need to buffer the data locally for the
615 * last block. This is to not pad the
616 * destination area to a multiple of
617 * UBIFS_BLOCK_SIZE.
618 */
619 buff = malloc(UBIFS_BLOCK_SIZE);
620 if (!buff) {
621 printf("%s: Error, malloc fails!\n",
622 __func__);
623 err = -ENOMEM;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100624 break;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100625 }
626
627 /* Read block-size into temp buffer */
628 ret = read_block(inode, buff, block, dn);
629 if (ret) {
630 err = ret;
631 if (err != -ENOENT) {
632 free(buff);
633 break;
634 }
635 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100636
Stefan Roese28ea29f2010-11-01 17:28:00 +0100637 if (last_block_size)
638 dlen = last_block_size;
639 else
640 dlen = le32_to_cpu(dn->size);
641
642 /* Now copy required size back to dest */
643 memcpy(addr, buff, dlen);
644
645 free(buff);
646 } else {
647 ret = read_block(inode, addr, block, dn);
648 if (ret) {
649 err = ret;
650 if (err != -ENOENT)
651 break;
652 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100653 }
654 }
655 if (++i >= UBIFS_BLOCKS_PER_PAGE)
656 break;
657 block += 1;
658 addr += UBIFS_BLOCK_SIZE;
659 }
660 if (err) {
661 if (err == -ENOENT) {
662 /* Not found, so it must be a hole */
663 dbg_gen("hole");
664 goto out_free;
665 }
666 ubifs_err("cannot read page %lu of inode %lu, error %d",
667 page->index, inode->i_ino, err);
668 goto error;
669 }
670
671out_free:
672 kfree(dn);
673out:
674 return 0;
675
676error:
677 kfree(dn);
678 return err;
679}
680
681int ubifs_load(char *filename, u32 addr, u32 size)
682{
683 struct ubifs_info *c = ubifs_sb->s_fs_info;
684 unsigned long inum;
685 struct inode *inode;
686 struct page page;
687 int err = 0;
688 int i;
689 int count;
Stefan Roese28ea29f2010-11-01 17:28:00 +0100690 int last_block_size = 0;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100691
692 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
Simon Kagstrom877501a2009-09-15 09:53:29 +0200693 /* ubifs_findfile will resolve symlinks, so we know that we get
694 * the real file here */
Stefan Roese2fc10f62009-03-19 15:35:05 +0100695 inum = ubifs_findfile(ubifs_sb, filename);
696 if (!inum) {
697 err = -1;
698 goto out;
699 }
700
701 /*
702 * Read file inode
703 */
704 inode = ubifs_iget(ubifs_sb, inum);
705 if (IS_ERR(inode)) {
706 printf("%s: Error reading inode %ld!\n", __func__, inum);
707 err = PTR_ERR(inode);
708 goto out;
709 }
710
711 /*
Stefan Roese2fc10f62009-03-19 15:35:05 +0100712 * If no size was specified or if size bigger than filesize
713 * set size to filesize
714 */
715 if ((size == 0) || (size > inode->i_size))
716 size = inode->i_size;
717
718 count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
719 printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
720 filename, addr, size, size);
721
722 page.addr = (void *)addr;
723 page.index = 0;
724 page.inode = inode;
725 for (i = 0; i < count; i++) {
Stefan Roese28ea29f2010-11-01 17:28:00 +0100726 /*
727 * Make sure to not read beyond the requested size
728 */
729 if (((i + 1) == count) && (size < inode->i_size))
730 last_block_size = size - (i * PAGE_SIZE);
731
732 err = do_readpage(c, inode, &page, last_block_size);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100733 if (err)
734 break;
735
736 page.addr += PAGE_SIZE;
737 page.index++;
738 }
739
740 if (err)
741 printf("Error reading file '%s'\n", filename);
742 else
743 printf("Done\n");
744
745 ubifs_iput(inode);
746
747out:
748 ubi_close_volume(c->ubi);
749 return err;
750}