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