ubifs.c 20 KB

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