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.
29 #include <asm/byteorder.h>
31 extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
34 /* Magic value used to identify an ext2 filesystem. */
35 #define EXT2_MAGIC 0xEF53
36 /* Amount of indirect blocks in an inode. */
37 #define INDIRECT_BLOCKS 12
38 /* Maximum lenght of a pathname. */
39 #define EXT2_PATH_MAX 4096
40 /* Maximum nesting of symlinks, used to prevent a loop. */
41 #define EXT2_MAX_SYMLINKCNT 8
43 /* Filetype used in directory entry. */
44 #define FILETYPE_UNKNOWN 0
45 #define FILETYPE_REG 1
46 #define FILETYPE_DIRECTORY 2
47 #define FILETYPE_SYMLINK 7
49 /* Filetype information as used in inodes. */
50 #define FILETYPE_INO_MASK 0170000
51 #define FILETYPE_INO_REG 0100000
52 #define FILETYPE_INO_DIRECTORY 0040000
53 #define FILETYPE_INO_SYMLINK 0120000
55 /* Bits used as offset in sector */
56 #define DISK_SECTOR_BITS 9
58 /* Log2 size of ext2 block in 512 blocks. */
59 #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1)
61 /* Log2 size of ext2 block in bytes. */
62 #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10)
64 /* The size of an ext2 block in bytes. */
65 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
67 /* The ext2 superblock. */
69 uint32_t total_inodes;
70 uint32_t total_blocks;
71 uint32_t reserved_blocks;
74 uint32_t first_data_block;
75 uint32_t log2_block_size;
76 uint32_t log2_fragment_size;
77 uint32_t blocks_per_group;
78 uint32_t fragments_per_group;
79 uint32_t inodes_per_group;
83 uint16_t max_mnt_count;
86 uint16_t error_handling;
87 uint16_t minor_revision_level;
89 uint32_t checkinterval;
91 uint32_t revision_level;
92 uint16_t uid_reserved;
93 uint16_t gid_reserved;
96 uint16_t block_group_number;
97 uint32_t feature_compatibility;
98 uint32_t feature_incompat;
99 uint32_t feature_ro_compat;
100 uint32_t unique_id[4];
101 char volume_name[16];
102 char last_mounted_on[64];
103 uint32_t compression_info;
106 /* The ext2 blockgroup. */
107 struct ext2_block_group {
110 uint32_t inode_table_id;
111 uint16_t free_blocks;
112 uint16_t free_inodes;
113 uint16_t used_dir_cnt;
114 uint32_t reserved[3];
117 /* The ext2 inode. */
128 uint32_t blockcnt; /* Blocks of 512 bytes!! */
133 uint32_t dir_blocks[INDIRECT_BLOCKS];
134 uint32_t indir_block;
135 uint32_t double_indir_block;
136 uint32_t tripple_indir_block;
143 uint32_t fragment_addr;
147 /* The header of an ext2 directory entry. */
156 struct ext2_data *data;
157 struct ext2_inode inode;
162 /* Information about a "mounted" ext2 filesystem. */
164 struct ext2_sblock sblock;
165 struct ext2_inode *inode;
166 struct ext2fs_node diropen;
170 typedef struct ext2fs_node *ext2fs_node_t;
172 struct ext2_data *ext2fs_root = NULL;
173 ext2fs_node_t ext2fs_file = NULL;
175 uint32_t *indir1_block = NULL;
177 int indir1_blkno = -1;
178 uint32_t *indir2_block = NULL;
180 int indir2_blkno = -1;
183 static int ext2fs_blockgroup
184 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
187 unsigned int desc_per_blk;
189 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
191 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
192 group / desc_per_blk;
193 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
195 printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
196 group, blkno, blkoff);
198 return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
199 blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
204 static int ext2fs_read_inode
205 (struct ext2_data *data, int ino, struct ext2_inode *inode) {
206 struct ext2_block_group blkgrp;
207 struct ext2_sblock *sblock = &data->sblock;
208 int inodes_per_block;
215 printf ("ext2fs read inode %d\n", ino);
217 /* It is easier to calculate if the first inode is 0. */
219 status = ext2fs_blockgroup (data, ino / __le32_to_cpu
220 (sblock->inodes_per_group), &blkgrp);
225 inodes_per_block = EXT2_BLOCK_SIZE(data) / __le16_to_cpu(sblock->inode_size);
228 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
231 blkno = __le32_to_cpu (blkgrp.inode_table_id) +
232 (ino % __le32_to_cpu (sblock->inodes_per_group))
234 blkoff = (ino % inodes_per_block) * __le16_to_cpu (sblock->inode_size);
236 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
238 /* Read the inode. */
239 status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
240 sizeof (struct ext2_inode), (char *) inode);
249 void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot) {
250 if ((node != &ext2fs_root->diropen) && (node != currroot)) {
256 static int ext2fs_read_block (ext2fs_node_t node, int fileblock) {
257 struct ext2_data *data = node->data;
258 struct ext2_inode *inode = &node->inode;
260 int blksz = EXT2_BLOCK_SIZE (data);
261 int log2_blksz = LOG2_EXT2_BLOCK_SIZE (data);
265 if (fileblock < INDIRECT_BLOCKS) {
266 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
269 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
270 if (indir1_block == NULL) {
271 indir1_block = (uint32_t *) malloc (blksz);
272 if (indir1_block == NULL) {
273 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
279 if (blksz != indir1_size) {
284 indir1_block = (uint32_t *) malloc (blksz);
285 if (indir1_block == NULL) {
286 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
291 if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
292 log2_blksz) != indir1_blkno) {
293 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
295 (char *) indir1_block);
297 printf ("** ext2fs read block (indir 1) failed. **\n");
301 __le32_to_cpu (inode->b.blocks.
302 indir_block) << log2_blksz;
304 blknr = __le32_to_cpu (indir1_block
305 [fileblock - INDIRECT_BLOCKS]);
307 /* Double indirect. */
309 (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
310 unsigned int perblock = blksz / 4;
311 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
314 if (indir1_block == NULL) {
315 indir1_block = (uint32_t *) malloc (blksz);
316 if (indir1_block == NULL) {
317 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
323 if (blksz != indir1_size) {
328 indir1_block = (uint32_t *) malloc (blksz);
329 if (indir1_block == NULL) {
330 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
335 if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
336 log2_blksz) != indir1_blkno) {
337 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
339 (char *) indir1_block);
341 printf ("** ext2fs read block (indir 2 1) failed. **\n");
345 __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
348 if (indir2_block == NULL) {
349 indir2_block = (uint32_t *) malloc (blksz);
350 if (indir2_block == NULL) {
351 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
357 if (blksz != indir2_size) {
362 indir2_block = (uint32_t *) malloc (blksz);
363 if (indir2_block == NULL) {
364 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
369 if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
370 log2_blksz) != indir1_blkno) {
371 status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
373 (char *) indir2_block);
375 printf ("** ext2fs read block (indir 2 2) failed. **\n");
379 __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
381 blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
383 /* Tripple indirect. */
385 printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
389 printf ("ext2fs_read_block %08x\n", blknr);
396 (ext2fs_node_t node, int pos, unsigned int len, char *buf) {
399 int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
400 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
401 unsigned int filesize = __le32_to_cpu(node->inode.size);
403 /* Adjust len so it we can't read past the end of the file. */
404 if (len > filesize) {
407 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
409 for (i = pos / blocksize; i < blockcnt; i++) {
411 int blockoff = pos % blocksize;
412 int blockend = blocksize;
416 blknr = ext2fs_read_block (node, i);
420 blknr = blknr << log2blocksize;
423 if (i == blockcnt - 1) {
424 blockend = (len + pos) % blocksize;
426 /* The last portion is exactly blocksize. */
428 blockend = blocksize;
433 if (i == pos / blocksize) {
434 skipfirst = blockoff;
435 blockend -= skipfirst;
438 /* If the block number is 0 this block is not stored on disk but
439 is zero filled instead. */
443 status = ext2fs_devread (blknr, skipfirst, blockend, buf);
448 memset (buf, 0, blocksize - skipfirst);
450 buf += blocksize - skipfirst;
456 static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
458 unsigned int fpos = 0;
460 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
464 printf ("Iterate dir %s\n", name);
465 #endif /* of DEBUG */
466 if (!diro->inode_read) {
467 status = ext2fs_read_inode (diro->data, diro->ino,
473 /* Search the file. */
474 while (fpos < __le32_to_cpu (diro->inode.size)) {
475 struct ext2_dirent dirent;
477 status = ext2fs_read_file (diro, fpos,
478 sizeof (struct ext2_dirent),
483 if (dirent.namelen != 0) {
484 char filename[dirent.namelen + 1];
486 int type = FILETYPE_UNKNOWN;
488 status = ext2fs_read_file (diro,
489 fpos + sizeof (struct ext2_dirent),
490 dirent.namelen, filename);
494 fdiro = malloc (sizeof (struct ext2fs_node));
499 fdiro->data = diro->data;
500 fdiro->ino = __le32_to_cpu (dirent.inode);
502 filename[dirent.namelen] = '\0';
504 if (dirent.filetype != FILETYPE_UNKNOWN) {
505 fdiro->inode_read = 0;
507 if (dirent.filetype == FILETYPE_DIRECTORY) {
508 type = FILETYPE_DIRECTORY;
509 } else if (dirent.filetype ==
511 type = FILETYPE_SYMLINK;
512 } else if (dirent.filetype == FILETYPE_REG) {
516 /* The filetype can not be read from the dirent, get it from inode */
518 status = ext2fs_read_inode (diro->data,
519 __le32_to_cpu(dirent.inode),
525 fdiro->inode_read = 1;
527 if ((__le16_to_cpu (fdiro->inode.mode) &
528 FILETYPE_INO_MASK) ==
529 FILETYPE_INO_DIRECTORY) {
530 type = FILETYPE_DIRECTORY;
531 } else if ((__le16_to_cpu (fdiro->inode.mode)
532 & FILETYPE_INO_MASK) ==
533 FILETYPE_INO_SYMLINK) {
534 type = FILETYPE_SYMLINK;
535 } else if ((__le16_to_cpu (fdiro->inode.mode)
536 & FILETYPE_INO_MASK) ==
542 printf ("iterate >%s<\n", filename);
543 #endif /* of DEBUG */
544 if ((name != NULL) && (fnode != NULL)
545 && (ftype != NULL)) {
546 if (strcmp (filename, name) == 0) {
552 if (fdiro->inode_read == 0) {
553 status = ext2fs_read_inode (diro->data,
554 __le32_to_cpu (dirent.inode),
560 fdiro->inode_read = 1;
563 case FILETYPE_DIRECTORY:
566 case FILETYPE_SYMLINK:
577 __le32_to_cpu (fdiro->inode.size),
582 fpos += __le16_to_cpu (dirent.direntlen);
588 static char *ext2fs_read_symlink (ext2fs_node_t node) {
590 struct ext2fs_node *diro = node;
593 if (!diro->inode_read) {
594 status = ext2fs_read_inode (diro->data, diro->ino,
600 symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
604 /* If the filesize of the symlink is bigger than
605 60 the symlink is stored in a separate block,
606 otherwise it is stored in the inode. */
607 if (__le32_to_cpu (diro->inode.size) <= 60) {
608 strncpy (symlink, diro->inode.b.symlink,
609 __le32_to_cpu (diro->inode.size));
611 status = ext2fs_read_file (diro, 0,
612 __le32_to_cpu (diro->inode.size),
619 symlink[__le32_to_cpu (diro->inode.size)] = '\0';
624 int ext2fs_find_file1
625 (const char *currpath,
626 ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype) {
627 char fpath[strlen (currpath) + 1];
631 int type = FILETYPE_DIRECTORY;
632 ext2fs_node_t currnode = currroot;
633 ext2fs_node_t oldnode = currroot;
635 strncpy (fpath, currpath, strlen (currpath) + 1);
637 /* Remove all leading slashes. */
638 while (*name == '/') {
642 *currfound = currnode;
649 /* Extract the actual part from the pathname. */
650 next = strchr (name, '/');
652 /* Remove all leading slashes. */
653 while (*next == '/') {
658 /* At this point it is expected that the current node is a directory, check if this is true. */
659 if (type != FILETYPE_DIRECTORY) {
660 ext2fs_free_node (currnode, currroot);
666 /* Iterate over the directory. */
667 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
675 /* Read in the symlink and follow it. */
676 if (type == FILETYPE_SYMLINK) {
679 /* Test if the symlink does not loop. */
680 if (++symlinknest == 8) {
681 ext2fs_free_node (currnode, currroot);
682 ext2fs_free_node (oldnode, currroot);
686 symlink = ext2fs_read_symlink (currnode);
687 ext2fs_free_node (currnode, currroot);
690 ext2fs_free_node (oldnode, currroot);
694 printf ("Got symlink >%s<\n", symlink);
695 #endif /* of DEBUG */
696 /* The symlink is an absolute path, go back to the root inode. */
697 if (symlink[0] == '/') {
698 ext2fs_free_node (oldnode, currroot);
699 oldnode = &ext2fs_root->diropen;
702 /* Lookup the node the symlink points to. */
703 status = ext2fs_find_file1 (symlink, oldnode,
709 ext2fs_free_node (oldnode, currroot);
714 ext2fs_free_node (oldnode, currroot);
716 /* Found the node! */
717 if (!next || *next == '\0') {
718 *currfound = currnode;
730 ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype) {
732 int foundtype = FILETYPE_DIRECTORY;
740 status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
744 /* Check if the node that was found was of the expected type. */
745 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
747 } else if ((expecttype == FILETYPE_DIRECTORY)
748 && (foundtype != expecttype)) {
755 int ext2fs_ls (char *dirname) {
756 ext2fs_node_t dirnode;
759 if (ext2fs_root == NULL) {
763 status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
766 printf ("** Can not find directory. **\n");
769 ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
770 ext2fs_free_node (dirnode, &ext2fs_root->diropen);
775 int ext2fs_open (char *filename) {
776 ext2fs_node_t fdiro = NULL;
780 if (ext2fs_root == NULL) {
784 status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
789 if (!fdiro->inode_read) {
790 status = ext2fs_read_inode (fdiro->data, fdiro->ino,
796 len = __le32_to_cpu (fdiro->inode.size);
801 ext2fs_free_node (fdiro, &ext2fs_root->diropen);
806 int ext2fs_close (void
808 if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
809 ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
812 if (ext2fs_root != NULL) {
816 if (indir1_block != NULL) {
822 if (indir2_block != NULL) {
832 int ext2fs_read (char *buf, unsigned len) {
835 if (ext2fs_root == NULL) {
839 if (ext2fs_file == NULL) {
843 status = ext2fs_read_file (ext2fs_file, 0, len, buf);
848 int ext2fs_mount (unsigned part_length) {
849 struct ext2_data *data;
852 data = malloc (sizeof (struct ext2_data));
856 /* Read the superblock. */
857 status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
858 (char *) &data->sblock);
862 /* Make sure this is an ext2 filesystem. */
863 if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
866 data->diropen.data = data;
867 data->diropen.ino = 2;
868 data->diropen.inode_read = 1;
869 data->inode = &data->diropen.inode;
871 status = ext2fs_read_inode (data, 2, data->inode);
881 printf ("Failed to mount ext2 filesystem...\n");