ubifs.c 15 KB

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