ubifs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. #include "ubifs.h"
  26. #include <u-boot/zlib.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /* compress.c */
  29. /*
  30. * We need a wrapper for zunzip() because the parameters are
  31. * incompatible with the lzo decompressor.
  32. */
  33. static int gzip_decompress(const unsigned char *in, size_t in_len,
  34. unsigned char *out, size_t *out_len)
  35. {
  36. unsigned long len = in_len;
  37. return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
  38. }
  39. /* Fake description object for the "none" compressor */
  40. static struct ubifs_compressor none_compr = {
  41. .compr_type = UBIFS_COMPR_NONE,
  42. .name = "no compression",
  43. .capi_name = "",
  44. .decompress = NULL,
  45. };
  46. static struct ubifs_compressor lzo_compr = {
  47. .compr_type = UBIFS_COMPR_LZO,
  48. .name = "LZO",
  49. .capi_name = "lzo",
  50. .decompress = lzo1x_decompress_safe,
  51. };
  52. static struct ubifs_compressor zlib_compr = {
  53. .compr_type = UBIFS_COMPR_ZLIB,
  54. .name = "zlib",
  55. .capi_name = "deflate",
  56. .decompress = gzip_decompress,
  57. };
  58. /* All UBIFS compressors */
  59. struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
  60. /**
  61. * ubifs_decompress - decompress data.
  62. * @in_buf: data to decompress
  63. * @in_len: length of the data to decompress
  64. * @out_buf: output buffer where decompressed data should
  65. * @out_len: output length is returned here
  66. * @compr_type: type of compression
  67. *
  68. * This function decompresses data from buffer @in_buf into buffer @out_buf.
  69. * The length of the uncompressed data is returned in @out_len. This functions
  70. * returns %0 on success or a negative error code on failure.
  71. */
  72. int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
  73. int *out_len, int compr_type)
  74. {
  75. int err;
  76. struct ubifs_compressor *compr;
  77. if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
  78. ubifs_err("invalid compression type %d", compr_type);
  79. return -EINVAL;
  80. }
  81. compr = ubifs_compressors[compr_type];
  82. if (unlikely(!compr->capi_name)) {
  83. ubifs_err("%s compression is not compiled in", compr->name);
  84. return -EINVAL;
  85. }
  86. if (compr_type == UBIFS_COMPR_NONE) {
  87. memcpy(out_buf, in_buf, in_len);
  88. *out_len = in_len;
  89. return 0;
  90. }
  91. err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
  92. if (err)
  93. ubifs_err("cannot decompress %d bytes, compressor %s, "
  94. "error %d", in_len, compr->name, err);
  95. return err;
  96. }
  97. /**
  98. * compr_init - initialize a compressor.
  99. * @compr: compressor description object
  100. *
  101. * This function initializes the requested compressor and returns zero in case
  102. * of success or a negative error code in case of failure.
  103. */
  104. static int __init compr_init(struct ubifs_compressor *compr)
  105. {
  106. ubifs_compressors[compr->compr_type] = compr;
  107. #ifdef CONFIG_NEEDS_MANUAL_RELOC
  108. ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
  109. ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
  110. ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
  111. #endif
  112. return 0;
  113. }
  114. /**
  115. * ubifs_compressors_init - initialize UBIFS compressors.
  116. *
  117. * This function initializes the compressor which were compiled in. Returns
  118. * zero in case of success and a negative error code in case of failure.
  119. */
  120. int __init ubifs_compressors_init(void)
  121. {
  122. int err;
  123. err = compr_init(&lzo_compr);
  124. if (err)
  125. return err;
  126. err = compr_init(&zlib_compr);
  127. if (err)
  128. return err;
  129. err = compr_init(&none_compr);
  130. if (err)
  131. return err;
  132. return 0;
  133. }
  134. /*
  135. * ubifsls...
  136. */
  137. static int filldir(struct ubifs_info *c, const char *name, int namlen,
  138. u64 ino, unsigned int d_type)
  139. {
  140. struct inode *inode;
  141. char filetime[32];
  142. switch (d_type) {
  143. case UBIFS_ITYPE_REG:
  144. printf("\t");
  145. break;
  146. case UBIFS_ITYPE_DIR:
  147. printf("<DIR>\t");
  148. break;
  149. case UBIFS_ITYPE_LNK:
  150. printf("<LNK>\t");
  151. break;
  152. default:
  153. printf("other\t");
  154. break;
  155. }
  156. inode = ubifs_iget(c->vfs_sb, ino);
  157. if (IS_ERR(inode)) {
  158. printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
  159. __func__, ino, inode);
  160. return -1;
  161. }
  162. ctime_r((time_t *)&inode->i_mtime, filetime);
  163. printf("%9lld %24.24s ", inode->i_size, filetime);
  164. ubifs_iput(inode);
  165. printf("%s\n", name);
  166. return 0;
  167. }
  168. static int ubifs_printdir(struct file *file, void *dirent)
  169. {
  170. int err, over = 0;
  171. struct qstr nm;
  172. union ubifs_key key;
  173. struct ubifs_dent_node *dent;
  174. struct inode *dir = file->f_path.dentry->d_inode;
  175. struct ubifs_info *c = dir->i_sb->s_fs_info;
  176. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  177. if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
  178. /*
  179. * The directory was seek'ed to a senseless position or there
  180. * are no more entries.
  181. */
  182. return 0;
  183. if (file->f_pos == 1) {
  184. /* Find the first entry in TNC and save it */
  185. lowest_dent_key(c, &key, dir->i_ino);
  186. nm.name = NULL;
  187. dent = ubifs_tnc_next_ent(c, &key, &nm);
  188. if (IS_ERR(dent)) {
  189. err = PTR_ERR(dent);
  190. goto out;
  191. }
  192. file->f_pos = key_hash_flash(c, &dent->key);
  193. file->private_data = dent;
  194. }
  195. dent = file->private_data;
  196. if (!dent) {
  197. /*
  198. * The directory was seek'ed to and is now readdir'ed.
  199. * Find the entry corresponding to @file->f_pos or the
  200. * closest one.
  201. */
  202. dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
  203. nm.name = NULL;
  204. dent = ubifs_tnc_next_ent(c, &key, &nm);
  205. if (IS_ERR(dent)) {
  206. err = PTR_ERR(dent);
  207. goto out;
  208. }
  209. file->f_pos = key_hash_flash(c, &dent->key);
  210. file->private_data = dent;
  211. }
  212. while (1) {
  213. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  214. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  215. key_hash_flash(c, &dent->key));
  216. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  217. nm.len = le16_to_cpu(dent->nlen);
  218. over = filldir(c, (char *)dent->name, nm.len,
  219. le64_to_cpu(dent->inum), dent->type);
  220. if (over)
  221. return 0;
  222. /* Switch to the next entry */
  223. key_read(c, &dent->key, &key);
  224. nm.name = (char *)dent->name;
  225. dent = ubifs_tnc_next_ent(c, &key, &nm);
  226. if (IS_ERR(dent)) {
  227. err = PTR_ERR(dent);
  228. goto out;
  229. }
  230. kfree(file->private_data);
  231. file->f_pos = key_hash_flash(c, &dent->key);
  232. file->private_data = dent;
  233. cond_resched();
  234. }
  235. out:
  236. if (err != -ENOENT) {
  237. ubifs_err("cannot find next direntry, error %d", err);
  238. return err;
  239. }
  240. kfree(file->private_data);
  241. file->private_data = NULL;
  242. file->f_pos = 2;
  243. return 0;
  244. }
  245. static int ubifs_finddir(struct super_block *sb, char *dirname,
  246. unsigned long root_inum, unsigned long *inum)
  247. {
  248. int err;
  249. struct qstr nm;
  250. union ubifs_key key;
  251. struct ubifs_dent_node *dent;
  252. struct ubifs_info *c;
  253. struct file *file;
  254. struct dentry *dentry;
  255. struct inode *dir;
  256. int ret = 0;
  257. file = kzalloc(sizeof(struct file), 0);
  258. dentry = kzalloc(sizeof(struct dentry), 0);
  259. dir = kzalloc(sizeof(struct inode), 0);
  260. if (!file || !dentry || !dir) {
  261. printf("%s: Error, no memory for malloc!\n", __func__);
  262. err = -ENOMEM;
  263. goto out;
  264. }
  265. dir->i_sb = sb;
  266. file->f_path.dentry = dentry;
  267. file->f_path.dentry->d_parent = dentry;
  268. file->f_path.dentry->d_inode = dir;
  269. file->f_path.dentry->d_inode->i_ino = root_inum;
  270. c = sb->s_fs_info;
  271. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  272. /* Find the first entry in TNC and save it */
  273. lowest_dent_key(c, &key, dir->i_ino);
  274. nm.name = NULL;
  275. dent = ubifs_tnc_next_ent(c, &key, &nm);
  276. if (IS_ERR(dent)) {
  277. err = PTR_ERR(dent);
  278. goto out;
  279. }
  280. file->f_pos = key_hash_flash(c, &dent->key);
  281. file->private_data = dent;
  282. while (1) {
  283. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  284. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  285. key_hash_flash(c, &dent->key));
  286. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  287. nm.len = le16_to_cpu(dent->nlen);
  288. if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
  289. (strlen(dirname) == nm.len)) {
  290. *inum = le64_to_cpu(dent->inum);
  291. ret = 1;
  292. goto out_free;
  293. }
  294. /* Switch to the next entry */
  295. key_read(c, &dent->key, &key);
  296. nm.name = (char *)dent->name;
  297. dent = ubifs_tnc_next_ent(c, &key, &nm);
  298. if (IS_ERR(dent)) {
  299. err = PTR_ERR(dent);
  300. goto out;
  301. }
  302. kfree(file->private_data);
  303. file->f_pos = key_hash_flash(c, &dent->key);
  304. file->private_data = dent;
  305. cond_resched();
  306. }
  307. out:
  308. if (err != -ENOENT)
  309. ubifs_err("cannot find next direntry, error %d", err);
  310. out_free:
  311. if (file->private_data)
  312. kfree(file->private_data);
  313. if (file)
  314. free(file);
  315. if (dentry)
  316. free(dentry);
  317. if (dir)
  318. free(dir);
  319. return ret;
  320. }
  321. static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
  322. {
  323. int ret;
  324. char *next;
  325. char fpath[128];
  326. char symlinkpath[128];
  327. char *name = fpath;
  328. unsigned long root_inum = 1;
  329. unsigned long inum;
  330. int symlink_count = 0; /* Don't allow symlink recursion */
  331. char link_name[64];
  332. strcpy(fpath, filename);
  333. /* Remove all leading slashes */
  334. while (*name == '/')
  335. name++;
  336. /*
  337. * Handle root-direcoty ('/')
  338. */
  339. inum = root_inum;
  340. if (!name || *name == '\0')
  341. return inum;
  342. for (;;) {
  343. struct inode *inode;
  344. struct ubifs_inode *ui;
  345. /* Extract the actual part from the pathname. */
  346. next = strchr(name, '/');
  347. if (next) {
  348. /* Remove all leading slashes. */
  349. while (*next == '/')
  350. *(next++) = '\0';
  351. }
  352. ret = ubifs_finddir(sb, name, root_inum, &inum);
  353. if (!ret)
  354. return 0;
  355. inode = ubifs_iget(sb, inum);
  356. if (!inode)
  357. return 0;
  358. ui = ubifs_inode(inode);
  359. if ((inode->i_mode & S_IFMT) == S_IFLNK) {
  360. char buf[128];
  361. /* We have some sort of symlink recursion, bail out */
  362. if (symlink_count++ > 8) {
  363. printf("Symlink recursion, aborting\n");
  364. return 0;
  365. }
  366. memcpy(link_name, ui->data, ui->data_len);
  367. link_name[ui->data_len] = '\0';
  368. if (link_name[0] == '/') {
  369. /* Absolute path, redo everything without
  370. * the leading slash */
  371. next = name = link_name + 1;
  372. root_inum = 1;
  373. continue;
  374. }
  375. /* Relative to cur dir */
  376. sprintf(buf, "%s/%s",
  377. link_name, next == NULL ? "" : next);
  378. memcpy(symlinkpath, buf, sizeof(buf));
  379. next = name = symlinkpath;
  380. continue;
  381. }
  382. /*
  383. * Check if directory with this name exists
  384. */
  385. /* Found the node! */
  386. if (!next || *next == '\0')
  387. return inum;
  388. root_inum = inum;
  389. name = next;
  390. }
  391. return 0;
  392. }
  393. int ubifs_ls(char *filename)
  394. {
  395. struct ubifs_info *c = ubifs_sb->s_fs_info;
  396. struct file *file;
  397. struct dentry *dentry;
  398. struct inode *dir;
  399. void *dirent = NULL;
  400. unsigned long inum;
  401. int ret = 0;
  402. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  403. inum = ubifs_findfile(ubifs_sb, filename);
  404. if (!inum) {
  405. ret = -1;
  406. goto out;
  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. ret = -ENOMEM;
  414. goto out_mem;
  415. }
  416. dir->i_sb = ubifs_sb;
  417. file->f_path.dentry = dentry;
  418. file->f_path.dentry->d_parent = dentry;
  419. file->f_path.dentry->d_inode = dir;
  420. file->f_path.dentry->d_inode->i_ino = inum;
  421. file->f_pos = 1;
  422. file->private_data = NULL;
  423. ubifs_printdir(file, dirent);
  424. out_mem:
  425. if (file)
  426. free(file);
  427. if (dentry)
  428. free(dentry);
  429. if (dir)
  430. free(dir);
  431. out:
  432. ubi_close_volume(c->ubi);
  433. return ret;
  434. }
  435. /*
  436. * ubifsload...
  437. */
  438. /* file.c */
  439. static inline void *kmap(struct page *page)
  440. {
  441. return page->addr;
  442. }
  443. static int read_block(struct inode *inode, void *addr, unsigned int block,
  444. struct ubifs_data_node *dn)
  445. {
  446. struct ubifs_info *c = inode->i_sb->s_fs_info;
  447. int err, len, out_len;
  448. union ubifs_key key;
  449. unsigned int dlen;
  450. data_key_init(c, &key, inode->i_ino, block);
  451. err = ubifs_tnc_lookup(c, &key, dn);
  452. if (err) {
  453. if (err == -ENOENT)
  454. /* Not found, so it must be a hole */
  455. memset(addr, 0, UBIFS_BLOCK_SIZE);
  456. return err;
  457. }
  458. ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
  459. len = le32_to_cpu(dn->size);
  460. if (len <= 0 || len > UBIFS_BLOCK_SIZE)
  461. goto dump;
  462. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  463. out_len = UBIFS_BLOCK_SIZE;
  464. err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
  465. le16_to_cpu(dn->compr_type));
  466. if (err || len != out_len)
  467. goto dump;
  468. /*
  469. * Data length can be less than a full block, even for blocks that are
  470. * not the last in the file (e.g., as a result of making a hole and
  471. * appending data). Ensure that the remainder is zeroed out.
  472. */
  473. if (len < UBIFS_BLOCK_SIZE)
  474. memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
  475. return 0;
  476. dump:
  477. ubifs_err("bad data node (block %u, inode %lu)",
  478. block, inode->i_ino);
  479. dbg_dump_node(c, dn);
  480. return -EINVAL;
  481. }
  482. static int do_readpage(struct ubifs_info *c, struct inode *inode,
  483. struct page *page, int last_block_size)
  484. {
  485. void *addr;
  486. int err = 0, i;
  487. unsigned int block, beyond;
  488. struct ubifs_data_node *dn;
  489. loff_t i_size = inode->i_size;
  490. dbg_gen("ino %lu, pg %lu, i_size %lld",
  491. inode->i_ino, page->index, i_size);
  492. addr = kmap(page);
  493. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  494. beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  495. if (block >= beyond) {
  496. /* Reading beyond inode */
  497. memset(addr, 0, PAGE_CACHE_SIZE);
  498. goto out;
  499. }
  500. dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
  501. if (!dn)
  502. return -ENOMEM;
  503. i = 0;
  504. while (1) {
  505. int ret;
  506. if (block >= beyond) {
  507. /* Reading beyond inode */
  508. err = -ENOENT;
  509. memset(addr, 0, UBIFS_BLOCK_SIZE);
  510. } else {
  511. /*
  512. * Reading last block? Make sure to not write beyond
  513. * the requested size in the destination buffer.
  514. */
  515. if (((block + 1) == beyond) || last_block_size) {
  516. void *buff;
  517. int dlen;
  518. /*
  519. * We need to buffer the data locally for the
  520. * last block. This is to not pad the
  521. * destination area to a multiple of
  522. * UBIFS_BLOCK_SIZE.
  523. */
  524. buff = malloc(UBIFS_BLOCK_SIZE);
  525. if (!buff) {
  526. printf("%s: Error, malloc fails!\n",
  527. __func__);
  528. err = -ENOMEM;
  529. break;
  530. }
  531. /* Read block-size into temp buffer */
  532. ret = read_block(inode, buff, block, dn);
  533. if (ret) {
  534. err = ret;
  535. if (err != -ENOENT) {
  536. free(buff);
  537. break;
  538. }
  539. }
  540. if (last_block_size)
  541. dlen = last_block_size;
  542. else
  543. dlen = le32_to_cpu(dn->size);
  544. /* Now copy required size back to dest */
  545. memcpy(addr, buff, dlen);
  546. free(buff);
  547. } else {
  548. ret = read_block(inode, addr, block, dn);
  549. if (ret) {
  550. err = ret;
  551. if (err != -ENOENT)
  552. break;
  553. }
  554. }
  555. }
  556. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  557. break;
  558. block += 1;
  559. addr += UBIFS_BLOCK_SIZE;
  560. }
  561. if (err) {
  562. if (err == -ENOENT) {
  563. /* Not found, so it must be a hole */
  564. dbg_gen("hole");
  565. goto out_free;
  566. }
  567. ubifs_err("cannot read page %lu of inode %lu, error %d",
  568. page->index, inode->i_ino, err);
  569. goto error;
  570. }
  571. out_free:
  572. kfree(dn);
  573. out:
  574. return 0;
  575. error:
  576. kfree(dn);
  577. return err;
  578. }
  579. int ubifs_load(char *filename, u32 addr, u32 size)
  580. {
  581. struct ubifs_info *c = ubifs_sb->s_fs_info;
  582. unsigned long inum;
  583. struct inode *inode;
  584. struct page page;
  585. int err = 0;
  586. int i;
  587. int count;
  588. int last_block_size = 0;
  589. char buf [10];
  590. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  591. /* ubifs_findfile will resolve symlinks, so we know that we get
  592. * the real file here */
  593. inum = ubifs_findfile(ubifs_sb, filename);
  594. if (!inum) {
  595. err = -1;
  596. goto out;
  597. }
  598. /*
  599. * Read file inode
  600. */
  601. inode = ubifs_iget(ubifs_sb, inum);
  602. if (IS_ERR(inode)) {
  603. printf("%s: Error reading inode %ld!\n", __func__, inum);
  604. err = PTR_ERR(inode);
  605. goto out;
  606. }
  607. /*
  608. * If no size was specified or if size bigger than filesize
  609. * set size to filesize
  610. */
  611. if ((size == 0) || (size > inode->i_size))
  612. size = inode->i_size;
  613. count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  614. printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
  615. filename, addr, size, size);
  616. page.addr = (void *)addr;
  617. page.index = 0;
  618. page.inode = inode;
  619. for (i = 0; i < count; i++) {
  620. /*
  621. * Make sure to not read beyond the requested size
  622. */
  623. if (((i + 1) == count) && (size < inode->i_size))
  624. last_block_size = size - (i * PAGE_SIZE);
  625. err = do_readpage(c, inode, &page, last_block_size);
  626. if (err)
  627. break;
  628. page.addr += PAGE_SIZE;
  629. page.index++;
  630. }
  631. if (err)
  632. printf("Error reading file '%s'\n", filename);
  633. else {
  634. sprintf(buf, "%X", size);
  635. setenv("filesize", buf);
  636. printf("Done\n");
  637. }
  638. ubifs_iput(inode);
  639. out:
  640. ubi_close_volume(c->ubi);
  641. return err;
  642. }