ubifs.c 21 KB

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