ubifs.c 20 KB

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