ext2fs.c 21 KB

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