ext2fs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. * (C) Copyright 2004
  3. * esd gmbh <www.esd-electronics.com>
  4. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  5. *
  6. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  7. *
  8. * GRUB -- GRand Unified Bootloader
  9. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. #include <common.h>
  26. #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
  27. #include <ext2fs.h>
  28. #include <malloc.h>
  29. #include <asm/byteorder.h>
  30. extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
  31. char *buf);
  32. /* Magic value used to identify an ext2 filesystem. */
  33. #define EXT2_MAGIC 0xEF53
  34. /* Amount of indirect blocks in an inode. */
  35. #define INDIRECT_BLOCKS 12
  36. /* Maximum lenght of a pathname. */
  37. #define EXT2_PATH_MAX 4096
  38. /* Maximum nesting of symlinks, used to prevent a loop. */
  39. #define EXT2_MAX_SYMLINKCNT 8
  40. /* Filetype used in directory entry. */
  41. #define FILETYPE_UNKNOWN 0
  42. #define FILETYPE_REG 1
  43. #define FILETYPE_DIRECTORY 2
  44. #define FILETYPE_SYMLINK 7
  45. /* Filetype information as used in inodes. */
  46. #define FILETYPE_INO_MASK 0170000
  47. #define FILETYPE_INO_REG 0100000
  48. #define FILETYPE_INO_DIRECTORY 0040000
  49. #define FILETYPE_INO_SYMLINK 0120000
  50. /* Bits used as offset in sector */
  51. #define DISK_SECTOR_BITS 9
  52. /* Log2 size of ext2 block in 512 blocks. */
  53. #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1)
  54. /* Log2 size of ext2 block in bytes. */
  55. #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10)
  56. /* The size of an ext2 block in bytes. */
  57. #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
  58. /* The ext2 superblock. */
  59. struct ext2_sblock {
  60. uint32_t total_inodes;
  61. uint32_t total_blocks;
  62. uint32_t reserved_blocks;
  63. uint32_t free_blocks;
  64. uint32_t free_inodes;
  65. uint32_t first_data_block;
  66. uint32_t log2_block_size;
  67. uint32_t log2_fragment_size;
  68. uint32_t blocks_per_group;
  69. uint32_t fragments_per_group;
  70. uint32_t inodes_per_group;
  71. uint32_t mtime;
  72. uint32_t utime;
  73. uint16_t mnt_count;
  74. uint16_t max_mnt_count;
  75. uint16_t magic;
  76. uint16_t fs_state;
  77. uint16_t error_handling;
  78. uint16_t minor_revision_level;
  79. uint32_t lastcheck;
  80. uint32_t checkinterval;
  81. uint32_t creator_os;
  82. uint32_t revision_level;
  83. uint16_t uid_reserved;
  84. uint16_t gid_reserved;
  85. uint32_t first_inode;
  86. uint16_t inode_size;
  87. uint16_t block_group_number;
  88. uint32_t feature_compatibility;
  89. uint32_t feature_incompat;
  90. uint32_t feature_ro_compat;
  91. uint32_t unique_id[4];
  92. char volume_name[16];
  93. char last_mounted_on[64];
  94. uint32_t compression_info;
  95. };
  96. /* The ext2 blockgroup. */
  97. struct ext2_block_group {
  98. uint32_t block_id;
  99. uint32_t inode_id;
  100. uint32_t inode_table_id;
  101. uint16_t free_blocks;
  102. uint16_t free_inodes;
  103. uint16_t pad;
  104. uint32_t reserved[3];
  105. };
  106. /* The ext2 inode. */
  107. struct ext2_inode {
  108. uint16_t mode;
  109. uint16_t uid;
  110. uint32_t size;
  111. uint32_t atime;
  112. uint32_t ctime;
  113. uint32_t mtime;
  114. uint32_t dtime;
  115. uint16_t gid;
  116. uint16_t nlinks;
  117. uint32_t blockcnt; /* Blocks of 512 bytes!! */
  118. uint32_t flags;
  119. uint32_t osd1;
  120. union {
  121. struct datablocks {
  122. uint32_t dir_blocks[INDIRECT_BLOCKS];
  123. uint32_t indir_block;
  124. uint32_t double_indir_block;
  125. uint32_t tripple_indir_block;
  126. } blocks;
  127. char symlink[60];
  128. } b;
  129. uint32_t version;
  130. uint32_t acl;
  131. uint32_t dir_acl;
  132. uint32_t fragment_addr;
  133. uint32_t osd2[3];
  134. };
  135. /* The header of an ext2 directory entry. */
  136. struct ext2_dirent {
  137. uint32_t inode;
  138. uint16_t direntlen;
  139. uint8_t namelen;
  140. uint8_t filetype;
  141. };
  142. struct ext2fs_node {
  143. struct ext2_data *data;
  144. struct ext2_inode inode;
  145. int ino;
  146. int inode_read;
  147. };
  148. /* Information about a "mounted" ext2 filesystem. */
  149. struct ext2_data {
  150. struct ext2_sblock sblock;
  151. struct ext2_inode *inode;
  152. struct ext2fs_node diropen;
  153. };
  154. typedef struct ext2fs_node *ext2fs_node_t;
  155. struct ext2_data *ext2fs_root = NULL;
  156. ext2fs_node_t ext2fs_file = NULL;
  157. int symlinknest = 0;
  158. uint32_t *indir1_block = NULL;
  159. int indir1_size = 0;
  160. int indir1_blkno = -1;
  161. uint32_t *indir2_block = NULL;
  162. int indir2_size = 0;
  163. int indir2_blkno = -1;
  164. static int ext2fs_blockgroup
  165. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
  166. #ifdef DEBUG
  167. printf ("ext2fs read blockgroup\n");
  168. #endif
  169. return (ext2fs_devread
  170. (((__le32_to_cpu (data->sblock.first_data_block) +
  171. 1) << LOG2_EXT2_BLOCK_SIZE (data)),
  172. group * sizeof (struct ext2_block_group),
  173. sizeof (struct ext2_block_group), (char *) blkgrp));
  174. }
  175. static int ext2fs_read_inode
  176. (struct ext2_data *data, int ino, struct ext2_inode *inode) {
  177. struct ext2_block_group blkgrp;
  178. struct ext2_sblock *sblock = &data->sblock;
  179. int inodes_per_block;
  180. int status;
  181. unsigned int blkno;
  182. unsigned int blkoff;
  183. /* It is easier to calculate if the first inode is 0. */
  184. ino--;
  185. #ifdef DEBUG
  186. printf ("ext2fs read inode %d\n", ino);
  187. #endif
  188. status = ext2fs_blockgroup (data,
  189. ino /
  190. __le32_to_cpu (sblock->inodes_per_group),
  191. &blkgrp);
  192. if (status == 0) {
  193. return (0);
  194. }
  195. inodes_per_block = EXT2_BLOCK_SIZE (data) / 128;
  196. blkno = (ino % __le32_to_cpu (sblock->inodes_per_group)) /
  197. inodes_per_block;
  198. blkoff = (ino % __le32_to_cpu (sblock->inodes_per_group)) %
  199. inodes_per_block;
  200. #ifdef DEBUG
  201. printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
  202. #endif
  203. /* Read the inode. */
  204. status = ext2fs_devread (((__le32_to_cpu (blkgrp.inode_table_id) +
  205. blkno) << LOG2_EXT2_BLOCK_SIZE (data)),
  206. sizeof (struct ext2_inode) * blkoff,
  207. sizeof (struct ext2_inode), (char *) inode);
  208. if (status == 0) {
  209. return (0);
  210. }
  211. return (1);
  212. }
  213. void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
  214. if ((node != &ext2fs_root->diropen) && (node != currroot)) {
  215. free (node);
  216. }
  217. }
  218. static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
  219. struct ext2_data *data = node->data;
  220. struct ext2_inode *inode = &node->inode;
  221. int blknr;
  222. int blksz = EXT2_BLOCK_SIZE (data);
  223. int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
  224. int status;
  225. /* Direct blocks. */
  226. if (fileblock < INDIRECT_BLOCKS) {
  227. blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
  228. }
  229. /* Indirect. */
  230. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  231. if (indir1_block == NULL) {
  232. indir1_block = (uint32_t *) malloc (blksz);
  233. if (indir1_block == NULL) {
  234. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  235. return (-1);
  236. }
  237. indir1_size = blksz;
  238. indir1_blkno = -1;
  239. }
  240. if (blksz != indir1_size) {
  241. free (indir1_block);
  242. indir1_block = NULL;
  243. indir1_size = 0;
  244. indir1_blkno = -1;
  245. indir1_block = (uint32_t *) malloc (blksz);
  246. if (indir1_block == NULL) {
  247. printf ("** ext2fs read block (indir 1) malloc failed. **\n");
  248. return (-1);
  249. }
  250. indir1_size = blksz;
  251. }
  252. if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
  253. log2_blksz) != indir1_blkno) {
  254. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
  255. 0, blksz,
  256. (char *) indir1_block);
  257. if (status == 0) {
  258. printf ("** ext2fs read block (indir 1) failed. **\n");
  259. return (0);
  260. }
  261. indir1_blkno =
  262. __le32_to_cpu (inode->b.blocks.
  263. indir_block) << log2_blksz;
  264. }
  265. blknr = __le32_to_cpu (indir1_block
  266. [fileblock - INDIRECT_BLOCKS]);
  267. }
  268. /* Double indirect. */
  269. else if (fileblock <
  270. (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
  271. unsigned int perblock = blksz / 4;
  272. unsigned int rblock = fileblock - (INDIRECT_BLOCKS
  273. + blksz / 4);
  274. if (indir1_block == NULL) {
  275. indir1_block = (uint32_t *) malloc (blksz);
  276. if (indir1_block == NULL) {
  277. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  278. return (-1);
  279. }
  280. indir1_size = blksz;
  281. indir1_blkno = -1;
  282. }
  283. if (blksz != indir1_size) {
  284. free (indir1_block);
  285. indir1_block = NULL;
  286. indir1_size = 0;
  287. indir1_blkno = -1;
  288. indir1_block = (uint32_t *) malloc (blksz);
  289. if (indir1_block == NULL) {
  290. printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
  291. return (-1);
  292. }
  293. indir1_size = blksz;
  294. }
  295. if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
  296. log2_blksz) != indir1_blkno) {
  297. status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
  298. 0, blksz,
  299. (char *) indir1_block);
  300. if (status == 0) {
  301. printf ("** ext2fs read block (indir 2 1) failed. **\n");
  302. return (-1);
  303. }
  304. indir1_blkno =
  305. __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
  306. }
  307. if (indir2_block == NULL) {
  308. indir2_block = (uint32_t *) malloc (blksz);
  309. if (indir2_block == NULL) {
  310. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  311. return (-1);
  312. }
  313. indir2_size = blksz;
  314. indir2_blkno = -1;
  315. }
  316. if (blksz != indir2_size) {
  317. free (indir2_block);
  318. indir2_block = NULL;
  319. indir2_size = 0;
  320. indir2_blkno = -1;
  321. indir2_block = (uint32_t *) malloc (blksz);
  322. if (indir2_block == NULL) {
  323. printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
  324. return (-1);
  325. }
  326. indir2_size = blksz;
  327. }
  328. if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
  329. log2_blksz) != indir1_blkno) {
  330. status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
  331. 0, blksz,
  332. (char *) indir2_block);
  333. if (status == 0) {
  334. printf ("** ext2fs read block (indir 2 2) failed. **\n");
  335. return (-1);
  336. }
  337. indir2_blkno =
  338. __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
  339. }
  340. blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
  341. }
  342. /* Tripple indirect. */
  343. else {
  344. printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
  345. return (-1);
  346. }
  347. #ifdef DEBUG
  348. printf ("ext2fs_read_block %08x\n", blknr);
  349. #endif
  350. return (blknr);
  351. }
  352. int ext2fs_read_file
  353. (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
  354. int i;
  355. int blockcnt;
  356. int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
  357. int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
  358. unsigned int filesize = node->inode.size;
  359. /* Adjust len so it we can't read past the end of the file. */
  360. if (len > filesize) {
  361. len = filesize;
  362. }
  363. blockcnt = ((len + pos) + blocksize - 1) / blocksize;
  364. for (i = pos / blocksize; i < blockcnt; i++) {
  365. int blknr;
  366. int blockoff = pos % blocksize;
  367. int blockend = blocksize;
  368. int skipfirst = 0;
  369. blknr = ext2fs_read_block (node, i);
  370. if (blknr < 0) {
  371. return (-1);
  372. }
  373. blknr = blknr << log2blocksize;
  374. /* Last block. */
  375. if (i == blockcnt - 1) {
  376. blockend = (len + pos) % blocksize;
  377. /* The last portion is exactly blocksize. */
  378. if (!blockend) {
  379. blockend = blocksize;
  380. }
  381. }
  382. /* First block. */
  383. if (i == pos / blocksize) {
  384. skipfirst = blockoff;
  385. blockend -= skipfirst;
  386. }
  387. /* If the block number is 0 this block is not stored on disk but
  388. is zero filled instead. */
  389. if (blknr) {
  390. int status;
  391. status = ext2fs_devread (blknr, skipfirst, blockend, buf);
  392. if (status == 0) {
  393. return (-1);
  394. }
  395. } else {
  396. memset (buf, blocksize - skipfirst, 0);
  397. }
  398. buf += blocksize - skipfirst;
  399. }
  400. return (len);
  401. }
  402. static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
  403. {
  404. unsigned int fpos = 0;
  405. int status;
  406. struct ext2fs_node *diro = (struct ext2fs_node *) dir;
  407. #ifdef DEBUG
  408. if (name != NULL)
  409. printf ("Iterate dir %s\n", name);
  410. #endif /* of DEBUG */
  411. if (!diro->inode_read) {
  412. status = ext2fs_read_inode (diro->data, diro->ino,
  413. &diro->inode);
  414. if (status == 0) {
  415. return (0);
  416. }
  417. }
  418. /* Search the file. */
  419. while (fpos < __le32_to_cpu (diro->inode.size)) {
  420. struct ext2_dirent dirent;
  421. status = ext2fs_read_file (diro, fpos,
  422. sizeof (struct ext2_dirent),
  423. (char *) &dirent);
  424. if (status < 1) {
  425. return (0);
  426. }
  427. if (dirent.namelen != 0) {
  428. char filename[dirent.namelen + 1];
  429. ext2fs_node_t fdiro;
  430. int type = FILETYPE_UNKNOWN;
  431. status = ext2fs_read_file (diro,
  432. fpos + sizeof (struct ext2_dirent),
  433. dirent.namelen, filename);
  434. if (status < 1) {
  435. return (0);
  436. }
  437. fdiro = malloc (sizeof (struct ext2fs_node));
  438. if (!fdiro) {
  439. return (0);
  440. }
  441. fdiro->data = diro->data;
  442. fdiro->ino = __le32_to_cpu (dirent.inode);
  443. filename[dirent.namelen] = '\0';
  444. if (dirent.filetype != FILETYPE_UNKNOWN) {
  445. fdiro->inode_read = 0;
  446. if (dirent.filetype == FILETYPE_DIRECTORY) {
  447. type = FILETYPE_DIRECTORY;
  448. } else if (dirent.filetype ==
  449. FILETYPE_SYMLINK) {
  450. type = FILETYPE_SYMLINK;
  451. } else if (dirent.filetype == FILETYPE_REG) {
  452. type = FILETYPE_REG;
  453. }
  454. } else {
  455. /* The filetype can not be read from the dirent, get it from inode */
  456. status = ext2fs_read_inode (diro->data,
  457. __le32_to_cpu(dirent.inode),
  458. &fdiro->inode);
  459. if (status == 0) {
  460. free (fdiro);
  461. return (0);
  462. }
  463. fdiro->inode_read = 1;
  464. if ((__le16_to_cpu (fdiro->inode.mode) &
  465. FILETYPE_INO_MASK) ==
  466. FILETYPE_INO_DIRECTORY) {
  467. type = FILETYPE_DIRECTORY;
  468. } else if ((__le16_to_cpu (fdiro->inode.mode)
  469. & FILETYPE_INO_MASK) ==
  470. FILETYPE_INO_SYMLINK) {
  471. type = FILETYPE_SYMLINK;
  472. } else if ((__le16_to_cpu (fdiro->inode.mode)
  473. & FILETYPE_INO_MASK) ==
  474. FILETYPE_INO_REG) {
  475. type = FILETYPE_REG;
  476. }
  477. }
  478. #ifdef DEBUG
  479. printf ("iterate >%s<\n", filename);
  480. #endif /* of DEBUG */
  481. if ((name != NULL) && (fnode != NULL)
  482. && (ftype != NULL)) {
  483. if (strcmp (filename, name) == 0) {
  484. *ftype = type;
  485. *fnode = fdiro;
  486. return (1);
  487. }
  488. } else {
  489. if (fdiro->inode_read == 0) {
  490. status = ext2fs_read_inode (diro->data,
  491. __le32_to_cpu (dirent.inode),
  492. &fdiro->inode);
  493. if (status == 0) {
  494. free (fdiro);
  495. return (0);
  496. }
  497. fdiro->inode_read = 1;
  498. }
  499. switch (type) {
  500. case FILETYPE_DIRECTORY:
  501. printf ("<DIR> ");
  502. break;
  503. case FILETYPE_SYMLINK:
  504. printf ("<SYM> ");
  505. break;
  506. case FILETYPE_REG:
  507. printf (" ");
  508. break;
  509. default:
  510. printf ("<???> ");
  511. break;
  512. }
  513. printf ("%10d %s\n",
  514. __le32_to_cpu (fdiro->inode.size),
  515. filename);
  516. }
  517. free (fdiro);
  518. }
  519. fpos += __le16_to_cpu (dirent.direntlen);
  520. }
  521. return (0);
  522. }
  523. static char *ext2fs_read_symlink (ext2fs_node_t node) {
  524. char *symlink;
  525. struct ext2fs_node *diro = node;
  526. int status;
  527. if (!diro->inode_read) {
  528. status = ext2fs_read_inode (diro->data, diro->ino,
  529. &diro->inode);
  530. if (status == 0) {
  531. return (0);
  532. }
  533. }
  534. symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
  535. if (!symlink) {
  536. return (0);
  537. }
  538. /* If the filesize of the symlink is bigger than
  539. 60 the symlink is stored in a separate block,
  540. otherwise it is stored in the inode. */
  541. if (__le32_to_cpu (diro->inode.size) <= 60) {
  542. strncpy (symlink, diro->inode.b.symlink,
  543. __le32_to_cpu (diro->inode.size));
  544. } else {
  545. status = ext2fs_read_file (diro, 0,
  546. __le32_to_cpu (diro->inode.size),
  547. symlink);
  548. if (status == 0) {
  549. free (symlink);
  550. return (0);
  551. }
  552. }
  553. symlink[__le32_to_cpu (diro->inode.size)] = '\0';
  554. return (symlink);
  555. }
  556. int ext2fs_find_file1
  557. (const char *currpath,
  558. ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
  559. char fpath[strlen (currpath) + 1];
  560. char *name = fpath;
  561. char *next;
  562. int status;
  563. int type = FILETYPE_DIRECTORY;
  564. ext2fs_node_t currnode = currroot;
  565. ext2fs_node_t oldnode = currroot;
  566. strncpy (fpath, currpath, strlen (currpath) + 1);
  567. /* Remove all leading slashes. */
  568. while (*name == '/') {
  569. name++;
  570. }
  571. if (!*name) {
  572. *currfound = currnode;
  573. return (1);
  574. }
  575. for (;;) {
  576. int found;
  577. /* Extract the actual part from the pathname. */
  578. next = strchr (name, '/');
  579. if (next) {
  580. /* Remove all leading slashes. */
  581. while (*next == '/') {
  582. *(next++) = '\0';
  583. }
  584. }
  585. /* At this point it is expected that the current node is a directory, check if this is true. */
  586. if (type != FILETYPE_DIRECTORY) {
  587. ext2fs_free_node (currnode, currroot);
  588. return (0);
  589. }
  590. oldnode = currnode;
  591. /* Iterate over the directory. */
  592. found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
  593. if (found == 0) {
  594. return (0);
  595. }
  596. if (found == -1) {
  597. break;
  598. }
  599. /* Read in the symlink and follow it. */
  600. if (type == FILETYPE_SYMLINK) {
  601. char *symlink;
  602. /* Test if the symlink does not loop. */
  603. if (++symlinknest == 8) {
  604. ext2fs_free_node (currnode, currroot);
  605. ext2fs_free_node (oldnode, currroot);
  606. return (0);
  607. }
  608. symlink = ext2fs_read_symlink (currnode);
  609. ext2fs_free_node (currnode, currroot);
  610. if (!symlink) {
  611. ext2fs_free_node (oldnode, currroot);
  612. return (0);
  613. }
  614. #ifdef DEBUG
  615. printf ("Got symlink >%s<\n", symlink);
  616. #endif /* of DEBUG */
  617. /* The symlink is an absolute path, go back to the root inode. */
  618. if (symlink[0] == '/') {
  619. ext2fs_free_node (oldnode, currroot);
  620. oldnode = &ext2fs_root->diropen;
  621. }
  622. /* Lookup the node the symlink points to. */
  623. status = ext2fs_find_file1 (symlink, oldnode,
  624. &currnode, &type);
  625. free (symlink);
  626. if (status == 0) {
  627. ext2fs_free_node (oldnode, currroot);
  628. return (0);
  629. }
  630. }
  631. ext2fs_free_node (oldnode, currroot);
  632. /* Found the node! */
  633. if (!next || *next == '\0') {
  634. *currfound = currnode;
  635. *foundtype = type;
  636. return (1);
  637. }
  638. name = next;
  639. }
  640. return (-1);
  641. }
  642. int ext2fs_find_file
  643. (const char *path,
  644. ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
  645. int status;
  646. int foundtype = FILETYPE_DIRECTORY;
  647. symlinknest = 0;
  648. if (!path || path[0] != '/') {
  649. return (0);
  650. }
  651. status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
  652. if (status == 0) {
  653. return (0);
  654. }
  655. /* Check if the node that was found was of the expected type. */
  656. if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
  657. return (0);
  658. } else if ((expecttype == FILETYPE_DIRECTORY)
  659. && (foundtype != expecttype)) {
  660. return (0);
  661. }
  662. return (1);
  663. }
  664. int ext2fs_ls (char *dirname) {
  665. ext2fs_node_t dirnode;
  666. int status;
  667. if (ext2fs_root == NULL) {
  668. return (0);
  669. }
  670. status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
  671. FILETYPE_DIRECTORY);
  672. if (status != 1) {
  673. printf ("** Can not find directory. **\n");
  674. return (1);
  675. }
  676. ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
  677. ext2fs_free_node (dirnode, &ext2fs_root->diropen);
  678. return (0);
  679. }
  680. int ext2fs_open (char *filename) {
  681. ext2fs_node_t fdiro = NULL;
  682. int status;
  683. int len;
  684. if (ext2fs_root == NULL) {
  685. return (0);
  686. }
  687. ext2fs_file = NULL;
  688. status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
  689. FILETYPE_REG);
  690. if (status == 0) {
  691. goto fail;
  692. }
  693. if (!fdiro->inode_read) {
  694. status = ext2fs_read_inode (fdiro->data, fdiro->ino,
  695. &fdiro->inode);
  696. if (status == 0) {
  697. goto fail;
  698. }
  699. }
  700. len = __le32_to_cpu (fdiro->inode.size);
  701. ext2fs_file = fdiro;
  702. return (len);
  703. fail:
  704. ext2fs_free_node (fdiro, &ext2fs_root->diropen);
  705. return (0);
  706. }
  707. int ext2fs_close (void
  708. ) {
  709. if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
  710. ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
  711. ext2fs_file = NULL;
  712. }
  713. if (ext2fs_root != NULL) {
  714. free (ext2fs_root);
  715. ext2fs_root = NULL;
  716. }
  717. if (indir1_block != NULL) {
  718. free (indir1_block);
  719. indir1_block = NULL;
  720. indir1_size = 0;
  721. indir1_blkno = -1;
  722. }
  723. if (indir2_block != NULL) {
  724. free (indir2_block);
  725. indir2_block = NULL;
  726. indir2_size = 0;
  727. indir2_blkno = -1;
  728. }
  729. return (0);
  730. }
  731. int ext2fs_read (char *buf, unsigned len) {
  732. int status;
  733. if (ext2fs_root == NULL) {
  734. return (0);
  735. }
  736. if (ext2fs_file == NULL) {
  737. return (0);
  738. }
  739. status = ext2fs_read_file (ext2fs_file, 0, len, buf);
  740. return (status);
  741. }
  742. int ext2fs_mount (unsigned part_length) {
  743. struct ext2_data *data;
  744. int status;
  745. data = malloc (sizeof (struct ext2_data));
  746. if (!data) {
  747. return (0);
  748. }
  749. /* Read the superblock. */
  750. status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
  751. (char *) &data->sblock);
  752. if (status == 0) {
  753. goto fail;
  754. }
  755. /* Make sure this is an ext2 filesystem. */
  756. if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
  757. goto fail;
  758. }
  759. data->diropen.data = data;
  760. data->diropen.ino = 2;
  761. data->diropen.inode_read = 1;
  762. data->inode = &data->diropen.inode;
  763. status = ext2fs_read_inode (data, 2, data->inode);
  764. if (status == 0) {
  765. goto fail;
  766. }
  767. ext2fs_root = data;
  768. return (1);
  769. fail:
  770. printf ("Failed to mount ext2 filesystem...\n");
  771. free (data);
  772. ext2fs_root = NULL;
  773. return (0);
  774. }
  775. #endif /* CFG_CMD_EXT2FS */