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