ubifs.c 20 KB

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