ubifs.c 20 KB

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