ext2fs.c 22 KB

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