ubifs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * (C) Copyright 2008-2009
  7. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License version 2 as published by
  11. * the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 51
  20. * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Authors: Artem Bityutskiy (Битюцкий Артём)
  23. * Adrian Hunter
  24. */
  25. #include "ubifs.h"
  26. #include <u-boot/zlib.h>
  27. DECLARE_GLOBAL_DATA_PTR;
  28. /* compress.c */
  29. /*
  30. * We need a wrapper for zunzip() because the parameters are
  31. * incompatible with the lzo decompressor.
  32. */
  33. static int gzip_decompress(const unsigned char *in, size_t in_len,
  34. unsigned char *out, size_t *out_len)
  35. {
  36. unsigned long len = in_len;
  37. return zunzip(out, *out_len, (unsigned char *)in, &len, 0, 0);
  38. }
  39. /* Fake description object for the "none" compressor */
  40. static struct ubifs_compressor none_compr = {
  41. .compr_type = UBIFS_COMPR_NONE,
  42. .name = "no compression",
  43. .capi_name = "",
  44. .decompress = NULL,
  45. };
  46. static struct ubifs_compressor lzo_compr = {
  47. .compr_type = UBIFS_COMPR_LZO,
  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. .name = "zlib",
  55. .capi_name = "deflate",
  56. .decompress = gzip_decompress,
  57. };
  58. /* All UBIFS compressors */
  59. struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
  60. /**
  61. * ubifs_decompress - decompress data.
  62. * @in_buf: data to decompress
  63. * @in_len: length of the data to decompress
  64. * @out_buf: output buffer where decompressed data should
  65. * @out_len: output length is returned here
  66. * @compr_type: type of compression
  67. *
  68. * This function decompresses data from buffer @in_buf into buffer @out_buf.
  69. * The length of the uncompressed data is returned in @out_len. This functions
  70. * returns %0 on success or a negative error code on failure.
  71. */
  72. int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
  73. int *out_len, int compr_type)
  74. {
  75. int err;
  76. struct ubifs_compressor *compr;
  77. if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
  78. ubifs_err("invalid compression type %d", compr_type);
  79. return -EINVAL;
  80. }
  81. compr = ubifs_compressors[compr_type];
  82. if (unlikely(!compr->capi_name)) {
  83. ubifs_err("%s compression is not compiled in", compr->name);
  84. return -EINVAL;
  85. }
  86. if (compr_type == UBIFS_COMPR_NONE) {
  87. memcpy(out_buf, in_buf, in_len);
  88. *out_len = in_len;
  89. return 0;
  90. }
  91. err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
  92. if (err)
  93. ubifs_err("cannot decompress %d bytes, compressor %s, "
  94. "error %d", in_len, compr->name, err);
  95. return err;
  96. }
  97. /**
  98. * compr_init - initialize a compressor.
  99. * @compr: compressor description object
  100. *
  101. * This function initializes the requested compressor and returns zero in case
  102. * of success or a negative error code in case of failure.
  103. */
  104. static int __init compr_init(struct ubifs_compressor *compr)
  105. {
  106. ubifs_compressors[compr->compr_type] = compr;
  107. #ifndef CONFIG_RELOC_FIXUP_WORKS
  108. ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
  109. ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
  110. ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
  111. #endif
  112. return 0;
  113. }
  114. /**
  115. * ubifs_compressors_init - initialize UBIFS compressors.
  116. *
  117. * This function initializes the compressor which were compiled in. Returns
  118. * zero in case of success and a negative error code in case of failure.
  119. */
  120. int __init ubifs_compressors_init(void)
  121. {
  122. int err;
  123. err = compr_init(&lzo_compr);
  124. if (err)
  125. return err;
  126. err = compr_init(&zlib_compr);
  127. if (err)
  128. return err;
  129. err = compr_init(&none_compr);
  130. if (err)
  131. return err;
  132. return 0;
  133. }
  134. /*
  135. * ubifsls...
  136. */
  137. static int filldir(struct ubifs_info *c, const char *name, int namlen,
  138. u64 ino, unsigned int d_type)
  139. {
  140. struct inode *inode;
  141. char filetime[32];
  142. switch (d_type) {
  143. case UBIFS_ITYPE_REG:
  144. printf("\t");
  145. break;
  146. case UBIFS_ITYPE_DIR:
  147. printf("<DIR>\t");
  148. break;
  149. case UBIFS_ITYPE_LNK:
  150. printf("<LNK>\t");
  151. break;
  152. default:
  153. printf("other\t");
  154. break;
  155. }
  156. inode = ubifs_iget(c->vfs_sb, ino);
  157. if (IS_ERR(inode)) {
  158. printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
  159. __func__, ino, inode);
  160. return -1;
  161. }
  162. ctime_r((time_t *)&inode->i_mtime, filetime);
  163. printf("%9lld %24.24s ", inode->i_size, filetime);
  164. ubifs_iput(inode);
  165. printf("%s\n", name);
  166. return 0;
  167. }
  168. static int ubifs_printdir(struct file *file, void *dirent)
  169. {
  170. int err, over = 0;
  171. struct qstr nm;
  172. union ubifs_key key;
  173. struct ubifs_dent_node *dent;
  174. struct inode *dir = file->f_path.dentry->d_inode;
  175. struct ubifs_info *c = dir->i_sb->s_fs_info;
  176. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  177. if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
  178. /*
  179. * The directory was seek'ed to a senseless position or there
  180. * are no more entries.
  181. */
  182. return 0;
  183. if (file->f_pos == 1) {
  184. /* Find the first entry in TNC and save it */
  185. lowest_dent_key(c, &key, dir->i_ino);
  186. nm.name = NULL;
  187. dent = ubifs_tnc_next_ent(c, &key, &nm);
  188. if (IS_ERR(dent)) {
  189. err = PTR_ERR(dent);
  190. goto out;
  191. }
  192. file->f_pos = key_hash_flash(c, &dent->key);
  193. file->private_data = dent;
  194. }
  195. dent = file->private_data;
  196. if (!dent) {
  197. /*
  198. * The directory was seek'ed to and is now readdir'ed.
  199. * Find the entry corresponding to @file->f_pos or the
  200. * closest one.
  201. */
  202. dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
  203. nm.name = NULL;
  204. dent = ubifs_tnc_next_ent(c, &key, &nm);
  205. if (IS_ERR(dent)) {
  206. err = PTR_ERR(dent);
  207. goto out;
  208. }
  209. file->f_pos = key_hash_flash(c, &dent->key);
  210. file->private_data = dent;
  211. }
  212. while (1) {
  213. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  214. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  215. key_hash_flash(c, &dent->key));
  216. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  217. nm.len = le16_to_cpu(dent->nlen);
  218. over = filldir(c, (char *)dent->name, nm.len,
  219. le64_to_cpu(dent->inum), dent->type);
  220. if (over)
  221. return 0;
  222. /* Switch to the next entry */
  223. key_read(c, &dent->key, &key);
  224. nm.name = (char *)dent->name;
  225. dent = ubifs_tnc_next_ent(c, &key, &nm);
  226. if (IS_ERR(dent)) {
  227. err = PTR_ERR(dent);
  228. goto out;
  229. }
  230. kfree(file->private_data);
  231. file->f_pos = key_hash_flash(c, &dent->key);
  232. file->private_data = dent;
  233. cond_resched();
  234. }
  235. out:
  236. if (err != -ENOENT) {
  237. ubifs_err("cannot find next direntry, error %d", err);
  238. return err;
  239. }
  240. kfree(file->private_data);
  241. file->private_data = NULL;
  242. file->f_pos = 2;
  243. return 0;
  244. }
  245. static int ubifs_finddir(struct super_block *sb, char *dirname,
  246. unsigned long root_inum, unsigned long *inum)
  247. {
  248. int err;
  249. struct qstr nm;
  250. union ubifs_key key;
  251. struct ubifs_dent_node *dent;
  252. struct ubifs_info *c;
  253. struct file *file;
  254. struct dentry *dentry;
  255. struct inode *dir;
  256. file = kzalloc(sizeof(struct file), 0);
  257. dentry = kzalloc(sizeof(struct dentry), 0);
  258. dir = kzalloc(sizeof(struct inode), 0);
  259. if (!file || !dentry || !dir) {
  260. printf("%s: Error, no memory for malloc!\n", __func__);
  261. err = -ENOMEM;
  262. goto out;
  263. }
  264. dir->i_sb = sb;
  265. file->f_path.dentry = dentry;
  266. file->f_path.dentry->d_parent = dentry;
  267. file->f_path.dentry->d_inode = dir;
  268. file->f_path.dentry->d_inode->i_ino = root_inum;
  269. c = sb->s_fs_info;
  270. dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
  271. /* Find the first entry in TNC and save it */
  272. lowest_dent_key(c, &key, dir->i_ino);
  273. nm.name = NULL;
  274. dent = ubifs_tnc_next_ent(c, &key, &nm);
  275. if (IS_ERR(dent)) {
  276. err = PTR_ERR(dent);
  277. goto out;
  278. }
  279. file->f_pos = key_hash_flash(c, &dent->key);
  280. file->private_data = dent;
  281. while (1) {
  282. dbg_gen("feed '%s', ino %llu, new f_pos %#x",
  283. dent->name, (unsigned long long)le64_to_cpu(dent->inum),
  284. key_hash_flash(c, &dent->key));
  285. ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
  286. nm.len = le16_to_cpu(dent->nlen);
  287. if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
  288. (strlen(dirname) == nm.len)) {
  289. *inum = le64_to_cpu(dent->inum);
  290. return 1;
  291. }
  292. /* Switch to the next entry */
  293. key_read(c, &dent->key, &key);
  294. nm.name = (char *)dent->name;
  295. dent = ubifs_tnc_next_ent(c, &key, &nm);
  296. if (IS_ERR(dent)) {
  297. err = PTR_ERR(dent);
  298. goto out;
  299. }
  300. kfree(file->private_data);
  301. file->f_pos = key_hash_flash(c, &dent->key);
  302. file->private_data = dent;
  303. cond_resched();
  304. }
  305. out:
  306. if (err != -ENOENT) {
  307. ubifs_err("cannot find next direntry, error %d", err);
  308. return err;
  309. }
  310. if (file)
  311. free(file);
  312. if (dentry)
  313. free(dentry);
  314. if (dir)
  315. free(dir);
  316. if (file->private_data)
  317. kfree(file->private_data);
  318. file->private_data = NULL;
  319. file->f_pos = 2;
  320. return 0;
  321. }
  322. static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
  323. {
  324. int ret;
  325. char *next;
  326. char fpath[128];
  327. char symlinkpath[128];
  328. char *name = fpath;
  329. unsigned long root_inum = 1;
  330. unsigned long inum;
  331. int symlink_count = 0; /* Don't allow symlink recursion */
  332. strcpy(fpath, filename);
  333. /* Remove all leading slashes */
  334. while (*name == '/')
  335. name++;
  336. /*
  337. * Handle root-direcoty ('/')
  338. */
  339. inum = root_inum;
  340. if (!name || *name == '\0')
  341. return inum;
  342. for (;;) {
  343. struct inode *inode;
  344. struct ubifs_inode *ui;
  345. /* Extract the actual part from the pathname. */
  346. next = strchr(name, '/');
  347. if (next) {
  348. /* Remove all leading slashes. */
  349. while (*next == '/')
  350. *(next++) = '\0';
  351. }
  352. ret = ubifs_finddir(sb, name, root_inum, &inum);
  353. if (!ret)
  354. return 0;
  355. inode = ubifs_iget(sb, inum);
  356. if (!inode)
  357. return 0;
  358. ui = ubifs_inode(inode);
  359. if ((inode->i_mode & S_IFMT) == S_IFLNK) {
  360. char link_name[64];
  361. char buf[128];
  362. /* We have some sort of symlink recursion, bail out */
  363. if (symlink_count++ > 8) {
  364. printf("Symlink recursion, aborting\n");
  365. return 0;
  366. }
  367. memcpy(link_name, ui->data, ui->data_len);
  368. link_name[ui->data_len] = '\0';
  369. if (link_name[0] == '/') {
  370. /* Absolute path, redo everything without
  371. * the leading slash */
  372. next = name = link_name + 1;
  373. root_inum = 1;
  374. continue;
  375. }
  376. /* Relative to cur dir */
  377. sprintf(buf, "%s/%s",
  378. link_name, next == NULL ? "" : next);
  379. memcpy(symlinkpath, buf, sizeof(buf));
  380. next = name = symlinkpath;
  381. continue;
  382. }
  383. /*
  384. * Check if directory with this name exists
  385. */
  386. /* Found the node! */
  387. if (!next || *next == '\0')
  388. return inum;
  389. root_inum = inum;
  390. name = next;
  391. }
  392. return 0;
  393. }
  394. int ubifs_ls(char *filename)
  395. {
  396. struct ubifs_info *c = ubifs_sb->s_fs_info;
  397. struct file *file;
  398. struct dentry *dentry;
  399. struct inode *dir;
  400. void *dirent = NULL;
  401. unsigned long inum;
  402. int ret = 0;
  403. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  404. inum = ubifs_findfile(ubifs_sb, filename);
  405. if (!inum) {
  406. ret = -1;
  407. goto out;
  408. }
  409. file = kzalloc(sizeof(struct file), 0);
  410. dentry = kzalloc(sizeof(struct dentry), 0);
  411. dir = kzalloc(sizeof(struct inode), 0);
  412. if (!file || !dentry || !dir) {
  413. printf("%s: Error, no memory for malloc!\n", __func__);
  414. ret = -ENOMEM;
  415. goto out_mem;
  416. }
  417. dir->i_sb = ubifs_sb;
  418. file->f_path.dentry = dentry;
  419. file->f_path.dentry->d_parent = dentry;
  420. file->f_path.dentry->d_inode = dir;
  421. file->f_path.dentry->d_inode->i_ino = inum;
  422. file->f_pos = 1;
  423. file->private_data = NULL;
  424. ubifs_printdir(file, dirent);
  425. out_mem:
  426. if (file)
  427. free(file);
  428. if (dentry)
  429. free(dentry);
  430. if (dir)
  431. free(dir);
  432. out:
  433. ubi_close_volume(c->ubi);
  434. return ret;
  435. }
  436. /*
  437. * ubifsload...
  438. */
  439. /* file.c */
  440. static inline void *kmap(struct page *page)
  441. {
  442. return page->addr;
  443. }
  444. static int read_block(struct inode *inode, void *addr, unsigned int block,
  445. struct ubifs_data_node *dn)
  446. {
  447. struct ubifs_info *c = inode->i_sb->s_fs_info;
  448. int err, len, out_len;
  449. union ubifs_key key;
  450. unsigned int dlen;
  451. data_key_init(c, &key, inode->i_ino, block);
  452. err = ubifs_tnc_lookup(c, &key, dn);
  453. if (err) {
  454. if (err == -ENOENT)
  455. /* Not found, so it must be a hole */
  456. memset(addr, 0, UBIFS_BLOCK_SIZE);
  457. return err;
  458. }
  459. ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
  460. len = le32_to_cpu(dn->size);
  461. if (len <= 0 || len > UBIFS_BLOCK_SIZE)
  462. goto dump;
  463. dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
  464. out_len = UBIFS_BLOCK_SIZE;
  465. err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
  466. le16_to_cpu(dn->compr_type));
  467. if (err || len != out_len)
  468. goto dump;
  469. /*
  470. * Data length can be less than a full block, even for blocks that are
  471. * not the last in the file (e.g., as a result of making a hole and
  472. * appending data). Ensure that the remainder is zeroed out.
  473. */
  474. if (len < UBIFS_BLOCK_SIZE)
  475. memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
  476. return 0;
  477. dump:
  478. ubifs_err("bad data node (block %u, inode %lu)",
  479. block, inode->i_ino);
  480. dbg_dump_node(c, dn);
  481. return -EINVAL;
  482. }
  483. static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
  484. {
  485. void *addr;
  486. int err = 0, i;
  487. unsigned int block, beyond;
  488. struct ubifs_data_node *dn;
  489. loff_t i_size = inode->i_size;
  490. dbg_gen("ino %lu, pg %lu, i_size %lld",
  491. inode->i_ino, page->index, i_size);
  492. addr = kmap(page);
  493. block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
  494. beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  495. if (block >= beyond) {
  496. /* Reading beyond inode */
  497. memset(addr, 0, PAGE_CACHE_SIZE);
  498. goto out;
  499. }
  500. dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
  501. if (!dn)
  502. return -ENOMEM;
  503. i = 0;
  504. while (1) {
  505. int ret;
  506. if (block >= beyond) {
  507. /* Reading beyond inode */
  508. err = -ENOENT;
  509. memset(addr, 0, UBIFS_BLOCK_SIZE);
  510. } else {
  511. ret = read_block(inode, addr, block, dn);
  512. if (ret) {
  513. err = ret;
  514. if (err != -ENOENT)
  515. break;
  516. } else if (block + 1 == beyond) {
  517. int dlen = le32_to_cpu(dn->size);
  518. int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
  519. if (ilen && ilen < dlen)
  520. memset(addr + ilen, 0, dlen - ilen);
  521. }
  522. }
  523. if (++i >= UBIFS_BLOCKS_PER_PAGE)
  524. break;
  525. block += 1;
  526. addr += UBIFS_BLOCK_SIZE;
  527. }
  528. if (err) {
  529. if (err == -ENOENT) {
  530. /* Not found, so it must be a hole */
  531. dbg_gen("hole");
  532. goto out_free;
  533. }
  534. ubifs_err("cannot read page %lu of inode %lu, error %d",
  535. page->index, inode->i_ino, err);
  536. goto error;
  537. }
  538. out_free:
  539. kfree(dn);
  540. out:
  541. return 0;
  542. error:
  543. kfree(dn);
  544. return err;
  545. }
  546. int ubifs_load(char *filename, u32 addr, u32 size)
  547. {
  548. struct ubifs_info *c = ubifs_sb->s_fs_info;
  549. unsigned long inum;
  550. struct inode *inode;
  551. struct page page;
  552. int err = 0;
  553. int i;
  554. int count;
  555. c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
  556. /* ubifs_findfile will resolve symlinks, so we know that we get
  557. * the real file here */
  558. inum = ubifs_findfile(ubifs_sb, filename);
  559. if (!inum) {
  560. err = -1;
  561. goto out;
  562. }
  563. /*
  564. * Read file inode
  565. */
  566. inode = ubifs_iget(ubifs_sb, inum);
  567. if (IS_ERR(inode)) {
  568. printf("%s: Error reading inode %ld!\n", __func__, inum);
  569. err = PTR_ERR(inode);
  570. goto out;
  571. }
  572. /*
  573. * If no size was specified or if size bigger than filesize
  574. * set size to filesize
  575. */
  576. if ((size == 0) || (size > inode->i_size))
  577. size = inode->i_size;
  578. count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
  579. printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
  580. filename, addr, size, size);
  581. page.addr = (void *)addr;
  582. page.index = 0;
  583. page.inode = inode;
  584. for (i = 0; i < count; i++) {
  585. err = do_readpage(c, inode, &page);
  586. if (err)
  587. break;
  588. page.addr += PAGE_SIZE;
  589. page.index++;
  590. }
  591. if (err)
  592. printf("Error reading file '%s'\n", filename);
  593. else
  594. printf("Done\n");
  595. ubifs_iput(inode);
  596. out:
  597. ubi_close_volume(c->ubi);
  598. return err;
  599. }