ubifs.c 21 KB

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