ubifs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. file = kzalloc(sizeof(struct file), 0);
  257. dentry = kzalloc(sizeof(struct dentry), 0);
  258. dir = kzalloc(sizeof(struct inode), 0);
  259. if (!file || !dentry || !dir) {
  260. printf("%s: Error, no memory for malloc!\n", __func__);
  261. err = -ENOMEM;
  262. goto out;
  263. }
  264. dir->i_sb = sb;
  265. file->f_path.dentry = dentry;
  266. file->f_path.dentry->d_parent = dentry;
  267. file->f_path.dentry->d_inode = dir;
  268. file->f_path.dentry->d_inode->i_ino = root_inum;
  269. c = sb->s_fs_info;
  270. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  271. /* Find the first entry in TNC and save it */
  272. lowest_dent_key(c, &key, dir->i_ino);
  273. nm.name = NULL;
  274. dent = ubifs_tnc_next_ent(c, &key, &nm);
  275. if (IS_ERR(dent)) {
  276. err = PTR_ERR(dent);
  277. goto out;
  278. }
  279. file->f_pos = key_hash_flash(c, &dent->key);
  280. file->private_data = dent;
  281. while (1) {
  282. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  283. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  284. key_hash_flash(c, &dent->key));
  285. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  286. nm.len = le16_to_cpu(dent->nlen);
  287. if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
  288. (strlen(dirname) == nm.len)) {
  289. *inum = le64_to_cpu(dent->inum);
  290. return 1;
  291. }
  292. /* Switch to the next entry */
  293. key_read(c, &dent->key, &key);
  294. nm.name = (char *)dent->name;
  295. dent = ubifs_tnc_next_ent(c, &key, &nm);
  296. if (IS_ERR(dent)) {
  297. err = PTR_ERR(dent);
  298. goto out;
  299. }
  300. kfree(file->private_data);
  301. file->f_pos = key_hash_flash(c, &dent->key);
  302. file->private_data = dent;
  303. cond_resched();
  304. }
  305. out:
  306. if (err != -ENOENT) {
  307. ubifs_err("cannot find next direntry, error %d", err);
  308. return err;
  309. }
  310. if (file->private_data)
  311. kfree(file->private_data);
  312. if (file)
  313. free(file);
  314. if (dentry)
  315. free(dentry);
  316. if (dir)
  317. free(dir);
  318. return 0;
  319. }
  320. static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
  321. {
  322. int ret;
  323. char *next;
  324. char fpath[128];
  325. char symlinkpath[128];
  326. char *name = fpath;
  327. unsigned long root_inum = 1;
  328. unsigned long inum;
  329. int symlink_count = 0; /* Don't allow symlink recursion */
  330. char link_name[64];
  331. strcpy(fpath, filename);
  332. /* Remove all leading slashes */
  333. while (*name == '/')
  334. name++;
  335. /*
  336. * Handle root-direcoty ('/')
  337. */
  338. inum = root_inum;
  339. if (!name || *name == '\0')
  340. return inum;
  341. for (;;) {
  342. struct inode *inode;
  343. struct ubifs_inode *ui;
  344. /* Extract the actual part from the pathname. */
  345. next = strchr(name, '/');
  346. if (next) {
  347. /* Remove all leading slashes. */
  348. while (*next == '/')
  349. *(next++) = '\0';
  350. }
  351. ret = ubifs_finddir(sb, name, root_inum, &inum);
  352. if (!ret)
  353. return 0;
  354. inode = ubifs_iget(sb, inum);
  355. if (!inode)
  356. return 0;
  357. ui = ubifs_inode(inode);
  358. if ((inode->i_mode & S_IFMT) == S_IFLNK) {
  359. char buf[128];
  360. /* We have some sort of symlink recursion, bail out */
  361. if (symlink_count++ > 8) {
  362. printf("Symlink recursion, aborting\n");
  363. return 0;
  364. }
  365. memcpy(link_name, ui->data, ui->data_len);
  366. link_name[ui->data_len] = '\0';
  367. if (link_name[0] == '/') {
  368. /* Absolute path, redo everything without
  369. * the leading slash */
  370. next = name = link_name + 1;
  371. root_inum = 1;
  372. continue;
  373. }
  374. /* Relative to cur dir */
  375. sprintf(buf, "%s/%s",
  376. link_name, next == NULL ? "" : next);
  377. memcpy(symlinkpath, buf, sizeof(buf));
  378. next = name = symlinkpath;
  379. continue;
  380. }
  381. /*
  382. * Check if directory with this name exists
  383. */
  384. /* Found the node! */
  385. if (!next || *next == '\0')
  386. return inum;
  387. root_inum = inum;
  388. name = next;
  389. }
  390. return 0;
  391. }
  392. int ubifs_ls(char *filename)
  393. {
  394. struct ubifs_info *c = ubifs_sb->s_fs_info;
  395. struct file *file;
  396. struct dentry *dentry;
  397. struct inode *dir;
  398. void *dirent = NULL;
  399. unsigned long inum;
  400. int ret = 0;
  401. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  402. inum = ubifs_findfile(ubifs_sb, filename);
  403. if (!inum) {
  404. ret = -1;
  405. goto out;
  406. }
  407. file = kzalloc(sizeof(struct file), 0);
  408. dentry = kzalloc(sizeof(struct dentry), 0);
  409. dir = kzalloc(sizeof(struct inode), 0);
  410. if (!file || !dentry || !dir) {
  411. printf("%s: Error, no memory for malloc!\n", __func__);
  412. ret = -ENOMEM;
  413. goto out_mem;
  414. }
  415. dir->i_sb = ubifs_sb;
  416. file->f_path.dentry = dentry;
  417. file->f_path.dentry->d_parent = dentry;
  418. file->f_path.dentry->d_inode = dir;
  419. file->f_path.dentry->d_inode->i_ino = inum;
  420. file->f_pos = 1;
  421. file->private_data = NULL;
  422. ubifs_printdir(file, dirent);
  423. out_mem:
  424. if (file)
  425. free(file);
  426. if (dentry)
  427. free(dentry);
  428. if (dir)
  429. free(dir);
  430. out:
  431. ubi_close_volume(c->ubi);
  432. return ret;
  433. }
  434. /*
  435. * ubifsload...
  436. */
  437. /* file.c */
  438. static inline void *kmap(struct page *page)
  439. {
  440. return page->addr;
  441. }
  442. static int read_block(struct inode *inode, void *addr, unsigned int block,
  443. struct ubifs_data_node *dn)
  444. {
  445. struct ubifs_info *c = inode->i_sb->s_fs_info;
  446. int err, len, out_len;
  447. union ubifs_key key;
  448. unsigned int dlen;
  449. data_key_init(c, &key, inode->i_ino, block);
  450. err = ubifs_tnc_lookup(c, &key, dn);
  451. if (err) {
  452. if (err == -ENOENT)
  453. /* Not found, so it must be a hole */
  454. memset(addr, 0, UBIFS_BLOCK_SIZE);
  455. return err;
  456. }
  457. ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
  458. len = le32_to_cpu(dn->size);
  459. if (len <= 0 || len > UBIFS_BLOCK_SIZE)
  460. goto dump;
  461. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  462. out_len = UBIFS_BLOCK_SIZE;
  463. err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
  464. le16_to_cpu(dn->compr_type));
  465. if (err || len != out_len)
  466. goto dump;
  467. /*
  468. * Data length can be less than a full block, even for blocks that are
  469. * not the last in the file (e.g., as a result of making a hole and
  470. * appending data). Ensure that the remainder is zeroed out.
  471. */
  472. if (len < UBIFS_BLOCK_SIZE)
  473. memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
  474. return 0;
  475. dump:
  476. ubifs_err("bad data node (block %u, inode %lu)",
  477. block, inode->i_ino);
  478. dbg_dump_node(c, dn);
  479. return -EINVAL;
  480. }
  481. static int do_readpage(struct ubifs_info *c, struct inode *inode,
  482. struct page *page, int last_block_size)
  483. {
  484. void *addr;
  485. int err = 0, i;
  486. unsigned int block, beyond;
  487. struct ubifs_data_node *dn;
  488. loff_t i_size = inode->i_size;
  489. dbg_gen("ino %lu, pg %lu, i_size %lld",
  490. inode->i_ino, page->index, i_size);
  491. addr = kmap(page);
  492. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  493. beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  494. if (block >= beyond) {
  495. /* Reading beyond inode */
  496. memset(addr, 0, PAGE_CACHE_SIZE);
  497. goto out;
  498. }
  499. dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
  500. if (!dn)
  501. return -ENOMEM;
  502. i = 0;
  503. while (1) {
  504. int ret;
  505. if (block >= beyond) {
  506. /* Reading beyond inode */
  507. err = -ENOENT;
  508. memset(addr, 0, UBIFS_BLOCK_SIZE);
  509. } else {
  510. /*
  511. * Reading last block? Make sure to not write beyond
  512. * the requested size in the destination buffer.
  513. */
  514. if (((block + 1) == beyond) || last_block_size) {
  515. void *buff;
  516. int dlen;
  517. /*
  518. * We need to buffer the data locally for the
  519. * last block. This is to not pad the
  520. * destination area to a multiple of
  521. * UBIFS_BLOCK_SIZE.
  522. */
  523. buff = malloc(UBIFS_BLOCK_SIZE);
  524. if (!buff) {
  525. printf("%s: Error, malloc fails!\n",
  526. __func__);
  527. err = -ENOMEM;
  528. break;
  529. }
  530. /* Read block-size into temp buffer */
  531. ret = read_block(inode, buff, block, dn);
  532. if (ret) {
  533. err = ret;
  534. if (err != -ENOENT) {
  535. free(buff);
  536. break;
  537. }
  538. }
  539. if (last_block_size)
  540. dlen = last_block_size;
  541. else
  542. dlen = le32_to_cpu(dn->size);
  543. /* Now copy required size back to dest */
  544. memcpy(addr, buff, dlen);
  545. free(buff);
  546. } else {
  547. ret = read_block(inode, addr, block, dn);
  548. if (ret) {
  549. err = ret;
  550. if (err != -ENOENT)
  551. break;
  552. }
  553. }
  554. }
  555. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  556. break;
  557. block += 1;
  558. addr += UBIFS_BLOCK_SIZE;
  559. }
  560. if (err) {
  561. if (err == -ENOENT) {
  562. /* Not found, so it must be a hole */
  563. dbg_gen("hole");
  564. goto out_free;
  565. }
  566. ubifs_err("cannot read page %lu of inode %lu, error %d",
  567. page->index, inode->i_ino, err);
  568. goto error;
  569. }
  570. out_free:
  571. kfree(dn);
  572. out:
  573. return 0;
  574. error:
  575. kfree(dn);
  576. return err;
  577. }
  578. int ubifs_load(char *filename, u32 addr, u32 size)
  579. {
  580. struct ubifs_info *c = ubifs_sb->s_fs_info;
  581. unsigned long inum;
  582. struct inode *inode;
  583. struct page page;
  584. int err = 0;
  585. int i;
  586. int count;
  587. int last_block_size = 0;
  588. char buf [10];
  589. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  590. /* ubifs_findfile will resolve symlinks, so we know that we get
  591. * the real file here */
  592. inum = ubifs_findfile(ubifs_sb, filename);
  593. if (!inum) {
  594. err = -1;
  595. goto out;
  596. }
  597. /*
  598. * Read file inode
  599. */
  600. inode = ubifs_iget(ubifs_sb, inum);
  601. if (IS_ERR(inode)) {
  602. printf("%s: Error reading inode %ld!\n", __func__, inum);
  603. err = PTR_ERR(inode);
  604. goto out;
  605. }
  606. /*
  607. * If no size was specified or if size bigger than filesize
  608. * set size to filesize
  609. */
  610. if ((size == 0) || (size > inode->i_size))
  611. size = inode->i_size;
  612. count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  613. printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
  614. filename, addr, size, size);
  615. page.addr = (void *)addr;
  616. page.index = 0;
  617. page.inode = inode;
  618. for (i = 0; i < count; i++) {
  619. /*
  620. * Make sure to not read beyond the requested size
  621. */
  622. if (((i + 1) == count) && (size < inode->i_size))
  623. last_block_size = size - (i * PAGE_SIZE);
  624. err = do_readpage(c, inode, &page, last_block_size);
  625. if (err)
  626. break;
  627. page.addr += PAGE_SIZE;
  628. page.index++;
  629. }
  630. if (err)
  631. printf("Error reading file '%s'\n", filename);
  632. else {
  633. sprintf(buf, "%X", size);
  634. setenv("filesize", buf);
  635. printf("Done\n");
  636. }
  637. ubifs_iput(inode);
  638. out:
  639. ubi_close_volume(c->ubi);
  640. return err;
  641. }