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