3 * esd gmbh <www.esd-electronics.com>
4 * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
6 * based on code from grub2 fs/ext2.c and fs/fshelp.c by
8 * GRUB -- GRand Unified Bootloader
9 * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
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.
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.
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.
28 #if (CONFIG_COMMANDS & CFG_CMD_EXT2)
31 #include <asm/byteorder.h>
33 extern int ext2fs_devread (int sector, int byte_offset, int byte_len, char *buf);
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
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
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
56 /* Bits used as offset in sector */
57 #define DISK_SECTOR_BITS 9
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)
62 /* Log2 size of ext2 block in bytes. */
63 #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10)
65 /* The size of an ext2 block in bytes. */
66 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
68 /* The ext2 superblock. */
71 uint32_t total_inodes;
72 uint32_t total_blocks;
73 uint32_t reserved_blocks;
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;
85 uint16_t max_mnt_count;
88 uint16_t error_handling;
89 uint16_t minor_revision_level;
91 uint32_t checkinterval;
93 uint32_t revision_level;
94 uint16_t uid_reserved;
95 uint16_t gid_reserved;
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;
108 /* The ext2 blockgroup. */
109 struct ext2_block_group
113 uint32_t inode_table_id;
114 uint16_t free_blocks;
115 uint16_t free_inodes;
117 uint32_t reserved[3];
120 /* The ext2 inode. */
132 uint32_t blockcnt; /* Blocks of 512 bytes!! */
139 uint32_t dir_blocks[INDIRECT_BLOCKS];
140 uint32_t indir_block;
141 uint32_t double_indir_block;
142 uint32_t tripple_indir_block;
149 uint32_t fragment_addr;
153 /* The header of an ext2 directory entry. */
164 struct ext2_data *data;
165 struct ext2_inode inode;
170 /* Information about a "mounted" ext2 filesystem. */
173 struct ext2_sblock sblock;
174 struct ext2_inode *inode;
175 struct ext2fs_node diropen;
179 typedef struct ext2fs_node *ext2fs_node_t;
181 struct ext2_data *ext2fs_root = NULL;
182 ext2fs_node_t ext2fs_file = NULL;
184 uint32_t *indir1_block = NULL;
186 int indir1_blkno = -1;
187 uint32_t *indir2_block = NULL;
189 int indir2_blkno = -1;
192 static int ext2fs_blockgroup
194 struct ext2_data *data,
196 struct ext2_block_group *blkgrp
200 printf("ext2fs read blockgroup\n");
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),
209 static int ext2fs_read_inode
211 struct ext2_data *data,
213 struct ext2_inode *inode
216 struct ext2_block_group blkgrp;
217 struct ext2_sblock *sblock = &data->sblock;
218 int inodes_per_block;
224 /* It is easier to calculate if the first inode is 0. */
227 printf("ext2fs read inode %d\n", ino);
229 status = ext2fs_blockgroup (data, ino / __le32_to_cpu(sblock->inodes_per_group), &blkgrp);
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;
238 printf("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
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),
253 void ext2fs_free_node
256 ext2fs_node_t currroot
259 if ((node != &ext2fs_root->diropen) && (node != currroot))
266 static int ext2fs_read_block
272 struct ext2_data *data = node->data;
273 struct ext2_inode *inode = &node->inode;
275 int blksz = EXT2_BLOCK_SIZE (data);
276 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
280 if (fileblock < INDIRECT_BLOCKS)
282 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
285 else if (fileblock < (INDIRECT_BLOCKS + (blksz/4)))
287 if (indir1_block == NULL)
289 indir1_block = (uint32_t *) malloc(blksz);
290 if (indir1_block == NULL)
292 printf("** ext2fs read block (indir 1) malloc failed. **\n");
298 if (blksz != indir1_size)
304 indir1_block = (uint32_t *) malloc(blksz);
305 if (indir1_block == NULL)
307 printf("** ext2fs read block (indir 1) malloc failed. **\n");
312 if ((__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz) != indir1_blkno)
314 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz, 0, blksz, (char *) indir1_block);
317 printf("** ext2fs read block (indir 1) failed. **\n");
320 indir1_blkno = __le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz;
322 blknr = __le32_to_cpu(indir1_block[fileblock - INDIRECT_BLOCKS]);
324 /* Double indirect. */
325 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1))))
327 unsigned int perblock = blksz / 4;
328 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
331 if (indir1_block == NULL)
333 indir1_block = (uint32_t *) malloc(blksz);
334 if (indir1_block == NULL)
336 printf("** ext2fs read block (indir 2 1) malloc failed. **\n");
342 if (blksz != indir1_size)
348 indir1_block = (uint32_t *) malloc(blksz);
349 if (indir1_block == NULL)
351 printf("** ext2fs read block (indir 2 1) malloc failed. **\n");
356 if ((__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz) != indir1_blkno)
358 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz, 0, blksz, (char *) indir1_block);
361 printf("** ext2fs read block (indir 2 1) failed. **\n");
364 indir1_blkno = __le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz;
367 if (indir2_block == NULL)
369 indir2_block = (uint32_t *) malloc(blksz);
370 if (indir2_block == NULL)
372 printf("** ext2fs read block (indir 2 2) malloc failed. **\n");
378 if (blksz != indir2_size)
384 indir2_block = (uint32_t *) malloc(blksz);
385 if (indir2_block == NULL)
387 printf("** ext2fs read block (indir 2 2) malloc failed. **\n");
392 if ((__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz) != indir1_blkno)
394 status = ext2fs_devread(__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz, 0, blksz, (char *) indir2_block);
397 printf("** ext2fs read block (indir 2 2) failed. **\n");
400 indir2_blkno = __le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz;
402 blknr = __le32_to_cpu(indir2_block[rblock % perblock]);
404 /* Tripple indirect. */
407 printf("** ext2fs doesn't support tripple indirect blocks. **\n");
411 printf("ext2fs_read_block %08x\n", blknr);
427 int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
428 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
429 unsigned int filesize = node->inode.size;
431 /* Adjust len so it we can't read past the end of the file. */
436 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
438 for (i = pos / blocksize; i < blockcnt; i++)
441 int blockoff = pos % blocksize;
442 int blockend = blocksize;
446 blknr = ext2fs_read_block(node, i);
451 blknr = blknr << log2blocksize;
454 if (i == blockcnt - 1)
456 blockend = (len + pos) % blocksize;
458 /* The last portion is exactly blocksize. */
461 blockend = blocksize;
466 if (i == pos / blocksize)
468 skipfirst = blockoff;
469 blockend -= skipfirst;
472 /* If the block number is 0 this block is not stored on disk but
473 is zero filled instead. */
478 status = ext2fs_devread (blknr, skipfirst, blockend, buf);
486 memset (buf, blocksize - skipfirst, 0);
488 buf += blocksize - skipfirst;
494 static int ext2fs_iterate_dir
498 ext2fs_node_t *fnode,
502 unsigned int fpos = 0;
504 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
506 if (name != NULL) printf("Iterate dir %s\n", name);
507 #endif /* of DEBUG */
508 if (!diro->inode_read)
510 status = ext2fs_read_inode (diro->data, diro->ino, &diro->inode);
516 /* Search the file. */
517 while (fpos < __le32_to_cpu (diro->inode.size))
519 struct ext2_dirent dirent;
521 status = ext2fs_read_file (diro, fpos, sizeof (struct ext2_dirent), (char *) &dirent);
526 if (dirent.namelen != 0)
528 char filename[dirent.namelen + 1];
530 int type = FILETYPE_UNKNOWN;
532 status = ext2fs_read_file (diro, fpos + sizeof (struct ext2_dirent), dirent.namelen, filename);
537 fdiro = malloc (sizeof (struct ext2fs_node));
543 fdiro->data = diro->data;
544 fdiro->ino = __le32_to_cpu(dirent.inode);
546 filename[dirent.namelen] = '\0';
548 if (dirent.filetype != FILETYPE_UNKNOWN)
550 fdiro->inode_read = 0;
552 if (dirent.filetype == FILETYPE_DIRECTORY)
554 type = FILETYPE_DIRECTORY;
556 else if (dirent.filetype == FILETYPE_SYMLINK)
558 type = FILETYPE_SYMLINK;
560 else if (dirent.filetype == FILETYPE_REG)
567 /* The filetype can not be read from the dirent, get it from inode */
569 status = ext2fs_read_inode (diro->data, __le32_to_cpu (dirent.inode), &fdiro->inode);
575 fdiro->inode_read = 1;
577 if ((__le16_to_cpu (fdiro->inode.mode) & FILETYPE_INO_MASK) == FILETYPE_INO_DIRECTORY)
579 type = FILETYPE_DIRECTORY;
581 else if ((__le16_to_cpu (fdiro->inode.mode) & FILETYPE_INO_MASK) == FILETYPE_INO_SYMLINK)
583 type = FILETYPE_SYMLINK;
585 else if ((__le16_to_cpu (fdiro->inode.mode) & FILETYPE_INO_MASK) == FILETYPE_INO_REG)
591 printf("iterate >%s<\n", filename);
592 #endif /* of DEBUG */
593 if ((name != NULL) && (fnode != NULL) && (ftype != NULL))
595 if(strcmp(filename, name) == 0)
604 if (fdiro->inode_read == 0)
606 status = ext2fs_read_inode (diro->data, __le32_to_cpu (dirent.inode), &fdiro->inode);
612 fdiro->inode_read = 1;
616 case FILETYPE_DIRECTORY:
619 case FILETYPE_SYMLINK:
629 printf("%10d %s\n", __le32_to_cpu(fdiro->inode.size), filename);
633 fpos += __le16_to_cpu (dirent.direntlen);
639 static char *ext2fs_read_symlink
645 struct ext2fs_node *diro = node;
648 if (!diro->inode_read)
650 status = ext2fs_read_inode (diro->data, diro->ino, &diro->inode);
656 symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
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)
666 strncpy (symlink, diro->inode.b.symlink, __le32_to_cpu (diro->inode.size));
670 status = ext2fs_read_file (diro, 0, __le32_to_cpu (diro->inode.size), symlink);
677 symlink[__le32_to_cpu (diro->inode.size)] = '\0';
682 int ext2fs_find_file1
684 const char *currpath,
685 ext2fs_node_t currroot,
686 ext2fs_node_t *currfound,
690 char fpath[strlen (currpath) + 1];
694 int type = FILETYPE_DIRECTORY;
695 ext2fs_node_t currnode = currroot;
696 ext2fs_node_t oldnode = currroot;
698 strncpy (fpath, currpath, strlen (currpath) + 1);
700 /* Remove all leading slashes. */
707 *currfound = currnode;
715 /* Extract the actual part from the pathname. */
716 next = strchr (name, '/');
719 /* Remove all leading slashes. */
726 /* At this point it is expected that the current node is a directory, check if this is true. */
727 if (type != FILETYPE_DIRECTORY)
729 ext2fs_free_node (currnode, currroot);
735 /* Iterate over the directory. */
736 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
746 /* Read in the symlink and follow it. */
747 if (type == FILETYPE_SYMLINK)
751 /* Test if the symlink does not loop. */
752 if (++symlinknest == 8)
754 ext2fs_free_node (currnode, currroot);
755 ext2fs_free_node (oldnode, currroot);
759 symlink = ext2fs_read_symlink (currnode);
760 ext2fs_free_node (currnode, currroot);
764 ext2fs_free_node (oldnode, currroot);
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] == '/')
773 ext2fs_free_node (oldnode, currroot);
774 oldnode = &ext2fs_root->diropen;
777 /* Lookup the node the symlink points to. */
778 status = ext2fs_find_file1 (symlink, oldnode, &currnode, &type);
784 ext2fs_free_node (oldnode, currroot);
789 ext2fs_free_node (oldnode, currroot);
791 /* Found the node! */
792 if (!next || *next == '\0')
794 *currfound = currnode;
807 ext2fs_node_t rootnode,
808 ext2fs_node_t *foundnode,
813 int foundtype = FILETYPE_DIRECTORY;
817 if (!path || path[0] != '/')
822 status = ext2fs_find_file1(path, rootnode, foundnode, &foundtype);
827 /* Check if the node that was found was of the expected type. */
828 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
832 else if ((expecttype == FILETYPE_DIRECTORY) && (foundtype != expecttype))
845 ext2fs_node_t dirnode;
848 if (ext2fs_root == NULL)
853 status = ext2fs_find_file(dirname, &ext2fs_root->diropen, &dirnode, FILETYPE_DIRECTORY);
856 printf("** Can not find directory. **\n");
859 ext2fs_iterate_dir(dirnode, NULL, NULL, NULL);
860 ext2fs_free_node(dirnode, &ext2fs_root->diropen);
870 ext2fs_node_t fdiro = NULL;
874 if (ext2fs_root == NULL)
879 status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro, FILETYPE_REG);
884 if (!fdiro->inode_read)
886 status = ext2fs_read_inode (fdiro->data, fdiro->ino, &fdiro->inode);
892 len = __le32_to_cpu (fdiro->inode.size);
897 ext2fs_free_node(fdiro, &ext2fs_root->diropen);
907 if ((ext2fs_file != NULL) && (ext2fs_root != NULL))
909 ext2fs_free_node(ext2fs_file, &ext2fs_root->diropen);
912 if (ext2fs_root != NULL)
917 if (indir1_block != NULL)
924 if (indir2_block != NULL)
943 if (ext2fs_root == NULL)
948 if (ext2fs_file == NULL)
953 status = ext2fs_read_file(ext2fs_file, 0, len, buf);
963 struct ext2_data *data;
966 data = malloc (sizeof (struct ext2_data));
971 /* Read the superblock. */
972 status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock), (char *) &data->sblock);
977 /* Make sure this is an ext2 filesystem. */
978 if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC)
982 data->diropen.data = data;
983 data->diropen.ino = 2;
984 data->diropen.inode_read = 1;
985 data->inode = &data->diropen.inode;
987 status = ext2fs_read_inode (data, 2, data->inode);
998 printf("Failed to mount ext2 filesystem...\n");
1004 #endif /* CFG_CMD_EXT2FS */