1 /* 2 * This file is part of UBIFS. 3 * 4 * Copyright (C) 2006-2008 Nokia Corporation. 5 * 6 * (C) Copyright 2008-2010 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" 27 #include <u-boot/zlib.h> 28 29 #define __UBOOT__ 30 #include <linux/err.h> 31 #include <linux/lzo.h> 32 33 DECLARE_GLOBAL_DATA_PTR; 34 35 /* compress.c */ 36 37 /* 38 * We need a wrapper for zunzip() because the parameters are 39 * incompatible with the lzo decompressor. 40 */ 41 static int gzip_decompress(const unsigned char *in, size_t in_len, 42 unsigned char *out, size_t *out_len) 43 { 44 return zunzip(out, *out_len, (unsigned char *)in, 45 (unsigned long *)out_len, 0, 0); 46 } 47 48 /* Fake description object for the "none" compressor */ 49 static struct ubifs_compressor none_compr = { 50 .compr_type = UBIFS_COMPR_NONE, 51 .name = "none", 52 .capi_name = "", 53 .decompress = NULL, 54 }; 55 56 static struct ubifs_compressor lzo_compr = { 57 .compr_type = UBIFS_COMPR_LZO, 58 #ifndef __UBOOT__ 59 .comp_mutex = &lzo_mutex, 60 #endif 61 .name = "lzo", 62 .capi_name = "lzo", 63 .decompress = lzo1x_decompress_safe, 64 }; 65 66 static struct ubifs_compressor zlib_compr = { 67 .compr_type = UBIFS_COMPR_ZLIB, 68 #ifndef __UBOOT__ 69 .comp_mutex = &deflate_mutex, 70 .decomp_mutex = &inflate_mutex, 71 #endif 72 .name = "zlib", 73 .capi_name = "deflate", 74 .decompress = gzip_decompress, 75 }; 76 77 /* All UBIFS compressors */ 78 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT]; 79 80 81 #ifdef __UBOOT__ 82 /* from mm/util.c */ 83 84 /** 85 * kmemdup - duplicate region of memory 86 * 87 * @src: memory region to duplicate 88 * @len: memory region length 89 * @gfp: GFP mask to use 90 */ 91 void *kmemdup(const void *src, size_t len, gfp_t gfp) 92 { 93 void *p; 94 95 p = kmalloc(len, gfp); 96 if (p) 97 memcpy(p, src, len); 98 return p; 99 } 100 101 struct crypto_comp { 102 int compressor; 103 }; 104 105 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 106 u32 type, u32 mask) 107 { 108 struct ubifs_compressor *comp; 109 struct crypto_comp *ptr; 110 int i = 0; 111 112 ptr = malloc(sizeof(struct crypto_comp)); 113 while (i < UBIFS_COMPR_TYPES_CNT) { 114 comp = ubifs_compressors[i]; 115 if (!comp) { 116 i++; 117 continue; 118 } 119 if (strncmp(alg_name, comp->capi_name, strlen(alg_name)) == 0) { 120 ptr->compressor = i; 121 return ptr; 122 } 123 i++; 124 } 125 if (i >= UBIFS_COMPR_TYPES_CNT) { 126 ubifs_err("invalid compression type %s", alg_name); 127 free (ptr); 128 return NULL; 129 } 130 return ptr; 131 } 132 static inline int crypto_comp_decompress(struct crypto_comp *tfm, 133 const u8 *src, unsigned int slen, 134 u8 *dst, unsigned int *dlen) 135 { 136 struct ubifs_compressor *compr = ubifs_compressors[tfm->compressor]; 137 int err; 138 139 if (compr->compr_type == UBIFS_COMPR_NONE) { 140 memcpy(dst, src, slen); 141 *dlen = slen; 142 return 0; 143 } 144 145 err = compr->decompress(src, slen, dst, (size_t *)dlen); 146 if (err) 147 ubifs_err("cannot decompress %d bytes, compressor %s, " 148 "error %d", slen, compr->name, err); 149 150 return err; 151 152 return 0; 153 } 154 #endif 155 156 /** 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 163 * 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. 167 */ 168 int ubifs_decompress(const void *in_buf, int in_len, void *out_buf, 169 int *out_len, int compr_type) 170 { 171 int err; 172 struct ubifs_compressor *compr; 173 174 if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) { 175 ubifs_err("invalid compression type %d", compr_type); 176 return -EINVAL; 177 } 178 179 compr = ubifs_compressors[compr_type]; 180 181 if (unlikely(!compr->capi_name)) { 182 ubifs_err("%s compression is not compiled in", compr->name); 183 return -EINVAL; 184 } 185 186 if (compr_type == UBIFS_COMPR_NONE) { 187 memcpy(out_buf, in_buf, in_len); 188 *out_len = in_len; 189 return 0; 190 } 191 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); 198 if (err) 199 ubifs_err("cannot decompress %d bytes, compressor %s, error %d", 200 in_len, compr->name, err); 201 202 return err; 203 } 204 205 /** 206 * compr_init - initialize a compressor. 207 * @compr: compressor description object 208 * 209 * This function initializes the requested compressor and returns zero in case 210 * of success or a negative error code in case of failure. 211 */ 212 static int __init compr_init(struct ubifs_compressor *compr) 213 { 214 ubifs_compressors[compr->compr_type] = compr; 215 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; 220 #endif 221 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); 228 } 229 } 230 231 return 0; 232 } 233 234 /** 235 * ubifs_compressors_init - initialize UBIFS compressors. 236 * 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. 239 */ 240 int __init ubifs_compressors_init(void) 241 { 242 int err; 243 244 err = compr_init(&lzo_compr); 245 if (err) 246 return err; 247 248 err = compr_init(&zlib_compr); 249 if (err) 250 return err; 251 252 err = compr_init(&none_compr); 253 if (err) 254 return err; 255 256 return 0; 257 } 258 259 /* 260 * ubifsls... 261 */ 262 263 static int filldir(struct ubifs_info *c, const char *name, int namlen, 264 u64 ino, unsigned int d_type) 265 { 266 struct inode *inode; 267 char filetime[32]; 268 269 switch (d_type) { 270 case UBIFS_ITYPE_REG: 271 printf("\t"); 272 break; 273 case UBIFS_ITYPE_DIR: 274 printf("<DIR>\t"); 275 break; 276 case UBIFS_ITYPE_LNK: 277 printf("<LNK>\t"); 278 break; 279 default: 280 printf("other\t"); 281 break; 282 } 283 284 inode = ubifs_iget(c->vfs_sb, ino); 285 if (IS_ERR(inode)) { 286 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n", 287 __func__, ino, inode); 288 return -1; 289 } 290 ctime_r((time_t *)&inode->i_mtime, filetime); 291 printf("%9lld %24.24s ", inode->i_size, filetime); 292 #ifndef __UBOOT__ 293 ubifs_iput(inode); 294 #endif 295 296 printf("%s\n", name); 297 298 return 0; 299 } 300 301 static int ubifs_printdir(struct file *file, void *dirent) 302 { 303 int err, over = 0; 304 struct qstr nm; 305 union ubifs_key key; 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; 309 310 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos); 311 312 if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2) 313 /* 314 * The directory was seek'ed to a senseless position or there 315 * are no more entries. 316 */ 317 return 0; 318 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); 322 nm.name = NULL; 323 dent = ubifs_tnc_next_ent(c, &key, &nm); 324 if (IS_ERR(dent)) { 325 err = PTR_ERR(dent); 326 goto out; 327 } 328 329 file->f_pos = key_hash_flash(c, &dent->key); 330 file->private_data = dent; 331 } 332 333 dent = file->private_data; 334 if (!dent) { 335 /* 336 * The directory was seek'ed to and is now readdir'ed. 337 * Find the entry corresponding to @file->f_pos or the 338 * closest one. 339 */ 340 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos); 341 nm.name = NULL; 342 dent = ubifs_tnc_next_ent(c, &key, &nm); 343 if (IS_ERR(dent)) { 344 err = PTR_ERR(dent); 345 goto out; 346 } 347 file->f_pos = key_hash_flash(c, &dent->key); 348 file->private_data = dent; 349 } 350 351 while (1) { 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); 356 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); 360 if (over) 361 return 0; 362 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); 367 if (IS_ERR(dent)) { 368 err = PTR_ERR(dent); 369 goto out; 370 } 371 372 kfree(file->private_data); 373 file->f_pos = key_hash_flash(c, &dent->key); 374 file->private_data = dent; 375 cond_resched(); 376 } 377 378 out: 379 if (err != -ENOENT) { 380 ubifs_err("cannot find next direntry, error %d", err); 381 return err; 382 } 383 384 kfree(file->private_data); 385 file->private_data = NULL; 386 file->f_pos = 2; 387 return 0; 388 } 389 390 static int ubifs_finddir(struct super_block *sb, char *dirname, 391 unsigned long root_inum, unsigned long *inum) 392 { 393 int err; 394 struct qstr nm; 395 union ubifs_key key; 396 struct ubifs_dent_node *dent; 397 struct ubifs_info *c; 398 struct file *file; 399 struct dentry *dentry; 400 struct inode *dir; 401 int ret = 0; 402 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__); 408 err = -ENOMEM; 409 goto out; 410 } 411 412 dir->i_sb = sb; 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; 417 c = sb->s_fs_info; 418 419 dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos); 420 421 /* Find the first entry in TNC and save it */ 422 lowest_dent_key(c, &key, dir->i_ino); 423 nm.name = NULL; 424 dent = ubifs_tnc_next_ent(c, &key, &nm); 425 if (IS_ERR(dent)) { 426 err = PTR_ERR(dent); 427 goto out; 428 } 429 430 file->f_pos = key_hash_flash(c, &dent->key); 431 file->private_data = dent; 432 433 while (1) { 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); 438 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); 443 ret = 1; 444 goto out_free; 445 } 446 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); 451 if (IS_ERR(dent)) { 452 err = PTR_ERR(dent); 453 goto out; 454 } 455 456 kfree(file->private_data); 457 file->f_pos = key_hash_flash(c, &dent->key); 458 file->private_data = dent; 459 cond_resched(); 460 } 461 462 out: 463 if (err != -ENOENT) 464 ubifs_err("cannot find next direntry, error %d", err); 465 466 out_free: 467 if (file->private_data) 468 kfree(file->private_data); 469 if (file) 470 free(file); 471 if (dentry) 472 free(dentry); 473 if (dir) 474 free(dir); 475 476 return ret; 477 } 478 479 static unsigned long ubifs_findfile(struct super_block *sb, char *filename) 480 { 481 int ret; 482 char *next; 483 char fpath[128]; 484 char symlinkpath[128]; 485 char *name = fpath; 486 unsigned long root_inum = 1; 487 unsigned long inum; 488 int symlink_count = 0; /* Don't allow symlink recursion */ 489 char link_name[64]; 490 491 strcpy(fpath, filename); 492 493 /* Remove all leading slashes */ 494 while (*name == '/') 495 name++; 496 497 /* 498 * Handle root-direcoty ('/') 499 */ 500 inum = root_inum; 501 if (!name || *name == '\0') 502 return inum; 503 504 for (;;) { 505 struct inode *inode; 506 struct ubifs_inode *ui; 507 508 /* Extract the actual part from the pathname. */ 509 next = strchr(name, '/'); 510 if (next) { 511 /* Remove all leading slashes. */ 512 while (*next == '/') 513 *(next++) = '\0'; 514 } 515 516 ret = ubifs_finddir(sb, name, root_inum, &inum); 517 if (!ret) 518 return 0; 519 inode = ubifs_iget(sb, inum); 520 521 if (!inode) 522 return 0; 523 ui = ubifs_inode(inode); 524 525 if ((inode->i_mode & S_IFMT) == S_IFLNK) { 526 char buf[128]; 527 528 /* We have some sort of symlink recursion, bail out */ 529 if (symlink_count++ > 8) { 530 printf("Symlink recursion, aborting\n"); 531 return 0; 532 } 533 memcpy(link_name, ui->data, ui->data_len); 534 link_name[ui->data_len] = '\0'; 535 536 if (link_name[0] == '/') { 537 /* Absolute path, redo everything without 538 * the leading slash */ 539 next = name = link_name + 1; 540 root_inum = 1; 541 continue; 542 } 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; 548 continue; 549 } 550 551 /* 552 * Check if directory with this name exists 553 */ 554 555 /* Found the node! */ 556 if (!next || *next == '\0') 557 return inum; 558 559 root_inum = inum; 560 name = next; 561 } 562 563 return 0; 564 } 565 566 int ubifs_ls(char *filename) 567 { 568 struct ubifs_info *c = ubifs_sb->s_fs_info; 569 struct file *file; 570 struct dentry *dentry; 571 struct inode *dir; 572 void *dirent = NULL; 573 unsigned long inum; 574 int ret = 0; 575 576 c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY); 577 inum = ubifs_findfile(ubifs_sb, filename); 578 if (!inum) { 579 ret = -1; 580 goto out; 581 } 582 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__); 588 ret = -ENOMEM; 589 goto out_mem; 590 } 591 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; 597 file->f_pos = 1; 598 file->private_data = NULL; 599 ubifs_printdir(file, dirent); 600 601 out_mem: 602 if (file) 603 free(file); 604 if (dentry) 605 free(dentry); 606 if (dir) 607 free(dir); 608 609 out: 610 ubi_close_volume(c->ubi); 611 return ret; 612 } 613 614 /* 615 * ubifsload... 616 */ 617 618 /* file.c */ 619 620 static inline void *kmap(struct page *page) 621 { 622 return page->addr; 623 } 624 625 static int read_block(struct inode *inode, void *addr, unsigned int block, 626 struct ubifs_data_node *dn) 627 { 628 struct ubifs_info *c = inode->i_sb->s_fs_info; 629 int err, len, out_len; 630 union ubifs_key key; 631 unsigned int dlen; 632 633 data_key_init(c, &key, inode->i_ino, block); 634 err = ubifs_tnc_lookup(c, &key, dn); 635 if (err) { 636 if (err == -ENOENT) 637 /* Not found, so it must be a hole */ 638 memset(addr, 0, UBIFS_BLOCK_SIZE); 639 return err; 640 } 641 642 ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum); 643 644 len = le32_to_cpu(dn->size); 645 if (len <= 0 || len > UBIFS_BLOCK_SIZE) 646 goto dump; 647 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) 653 goto dump; 654 655 /* 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. 659 */ 660 if (len < UBIFS_BLOCK_SIZE) 661 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len); 662 663 return 0; 664 665 dump: 666 ubifs_err("bad data node (block %u, inode %lu)", 667 block, inode->i_ino); 668 ubifs_dump_node(c, dn); 669 return -EINVAL; 670 } 671 672 static int do_readpage(struct ubifs_info *c, struct inode *inode, 673 struct page *page, int last_block_size) 674 { 675 void *addr; 676 int err = 0, i; 677 unsigned int block, beyond; 678 struct ubifs_data_node *dn; 679 loff_t i_size = inode->i_size; 680 681 dbg_gen("ino %lu, pg %lu, i_size %lld", 682 inode->i_ino, page->index, i_size); 683 684 addr = kmap(page); 685 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); 691 goto out; 692 } 693 694 dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS); 695 if (!dn) 696 return -ENOMEM; 697 698 i = 0; 699 while (1) { 700 int ret; 701 702 if (block >= beyond) { 703 /* Reading beyond inode */ 704 err = -ENOENT; 705 memset(addr, 0, UBIFS_BLOCK_SIZE); 706 } else { 707 /* 708 * Reading last block? Make sure to not write beyond 709 * the requested size in the destination buffer. 710 */ 711 if (((block + 1) == beyond) || last_block_size) { 712 void *buff; 713 int dlen; 714 715 /* 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 719 * UBIFS_BLOCK_SIZE. 720 */ 721 buff = malloc(UBIFS_BLOCK_SIZE); 722 if (!buff) { 723 printf("%s: Error, malloc fails!\n", 724 __func__); 725 err = -ENOMEM; 726 break; 727 } 728 729 /* Read block-size into temp buffer */ 730 ret = read_block(inode, buff, block, dn); 731 if (ret) { 732 err = ret; 733 if (err != -ENOENT) { 734 free(buff); 735 break; 736 } 737 } 738 739 if (last_block_size) 740 dlen = last_block_size; 741 else 742 dlen = le32_to_cpu(dn->size); 743 744 /* Now copy required size back to dest */ 745 memcpy(addr, buff, dlen); 746 747 free(buff); 748 } else { 749 ret = read_block(inode, addr, block, dn); 750 if (ret) { 751 err = ret; 752 if (err != -ENOENT) 753 break; 754 } 755 } 756 } 757 if (++i >= UBIFS_BLOCKS_PER_PAGE) 758 break; 759 block += 1; 760 addr += UBIFS_BLOCK_SIZE; 761 } 762 if (err) { 763 if (err == -ENOENT) { 764 /* Not found, so it must be a hole */ 765 dbg_gen("hole"); 766 goto out_free; 767 } 768 ubifs_err("cannot read page %lu of inode %lu, error %d", 769 page->index, inode->i_ino, err); 770 goto error; 771 } 772 773 out_free: 774 kfree(dn); 775 out: 776 return 0; 777 778 error: 779 kfree(dn); 780 return err; 781 } 782 783 int ubifs_load(char *filename, u32 addr, u32 size) 784 { 785 struct ubifs_info *c = ubifs_sb->s_fs_info; 786 unsigned long inum; 787 struct inode *inode; 788 struct page page; 789 int err = 0; 790 int i; 791 int count; 792 int last_block_size = 0; 793 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); 798 if (!inum) { 799 err = -1; 800 goto out; 801 } 802 803 /* 804 * Read file inode 805 */ 806 inode = ubifs_iget(ubifs_sb, inum); 807 if (IS_ERR(inode)) { 808 printf("%s: Error reading inode %ld!\n", __func__, inum); 809 err = PTR_ERR(inode); 810 goto out; 811 } 812 813 /* 814 * If no size was specified or if size bigger than filesize 815 * set size to filesize 816 */ 817 if ((size == 0) || (size > inode->i_size)) 818 size = inode->i_size; 819 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); 823 824 page.addr = (void *)addr; 825 page.index = 0; 826 page.inode = inode; 827 for (i = 0; i < count; i++) { 828 /* 829 * Make sure to not read beyond the requested size 830 */ 831 if (((i + 1) == count) && (size < inode->i_size)) 832 last_block_size = size - (i * PAGE_SIZE); 833 834 err = do_readpage(c, inode, &page, last_block_size); 835 if (err) 836 break; 837 838 page.addr += PAGE_SIZE; 839 page.index++; 840 } 841 842 if (err) 843 printf("Error reading file '%s'\n", filename); 844 else { 845 setenv_hex("filesize", size); 846 printf("Done\n"); 847 } 848 849 ubifs_iput(inode); 850 851 out: 852 ubi_close_volume(c->ubi); 853 return err; 854 } 855