ubifs.c 21 KB

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