ubifs.c 19 KB

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