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 #ifdef CONFIG_CMD_EXT4_WRITE
29 #include <ext_common.h>
32 #include <asm/byteorder.h>
34 extern int ext2fs_devread (int sector, int byte_offset, int byte_len,
37 #ifndef CONFIG_CMD_EXT4_WRITE // #if 0 samsung
38 /* Magic value used to identify an ext2 filesystem. */
39 #define EXT2_MAGIC 0xEF53
40 /* Amount of indirect blocks in an inode. */
41 #define INDIRECT_BLOCKS 12
42 /* Maximum lenght of a pathname. */
43 #define EXT2_PATH_MAX 4096
44 /* Maximum nesting of symlinks, used to prevent a loop. */
45 #define EXT2_MAX_SYMLINKCNT 8
47 /* Filetype used in directory entry. */
48 #define FILETYPE_UNKNOWN 0
49 #define FILETYPE_REG 1
50 #define FILETYPE_DIRECTORY 2
51 #define FILETYPE_SYMLINK 7
53 /* Filetype information as used in inodes. */
54 #define FILETYPE_INO_MASK 0170000
55 #define FILETYPE_INO_REG 0100000
56 #define FILETYPE_INO_DIRECTORY 0040000
57 #define FILETYPE_INO_SYMLINK 0120000
59 /* Bits used as offset in sector */
60 #define DISK_SECTOR_BITS 9
62 /* Log2 size of ext2 block in 512 blocks. */
63 #define LOG2_EXT2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 1)
65 /* Log2 size of ext2 block in bytes. */
66 #define LOG2_BLOCK_SIZE(data) (__le32_to_cpu (data->sblock.log2_block_size) + 10)
68 /* The size of an ext2 block in bytes. */
69 #define EXT2_BLOCK_SIZE(data) (1 << LOG2_BLOCK_SIZE(data))
71 /* The ext2 superblock. */
73 uint32_t total_inodes;
74 uint32_t total_blocks;
75 uint32_t reserved_blocks;
78 uint32_t first_data_block;
79 uint32_t log2_block_size;
80 uint32_t log2_fragment_size;
81 uint32_t blocks_per_group;
82 uint32_t fragments_per_group;
83 uint32_t inodes_per_group;
87 uint16_t max_mnt_count;
90 uint16_t error_handling;
91 uint16_t minor_revision_level;
93 uint32_t checkinterval;
95 uint32_t revision_level;
96 uint16_t uid_reserved;
97 uint16_t gid_reserved;
100 uint16_t block_group_number;
101 uint32_t feature_compatibility;
102 uint32_t feature_incompat;
103 uint32_t feature_ro_compat;
104 uint32_t unique_id[4];
105 char volume_name[16];
106 char last_mounted_on[64];
107 uint32_t compression_info;
110 /* The ext2 blockgroup. */
111 struct ext2_block_group {
114 uint32_t inode_table_id;
115 uint16_t free_blocks;
116 uint16_t free_inodes;
117 uint16_t used_dir_cnt;
118 uint32_t reserved[3];
121 /* The ext2 inode. */
132 uint32_t blockcnt; /* Blocks of 512 bytes!! */
137 uint32_t dir_blocks[INDIRECT_BLOCKS];
138 uint32_t indir_block;
139 uint32_t double_indir_block;
140 uint32_t tripple_indir_block;
147 uint32_t fragment_addr;
151 /* The header of an ext2 directory entry. */
160 struct ext2_data *data;
161 struct ext2_inode inode;
166 /* Information about a "mounted" ext2 filesystem. */
168 struct ext2_sblock sblock;
169 struct ext2_inode *inode;
170 struct ext2fs_node diropen;
174 typedef struct ext2fs_node *ext2fs_node_t;
177 struct ext2_data *ext2fs_root = NULL;
178 #ifndef CONFIG_CMD_EXT4_WRITE
179 ext2fs_node_t ext2fs_file = NULL;
181 struct ext2fs_node *ext2fs_file;
184 uint32_t *indir1_block = NULL;
186 int indir1_blkno = -1;
187 uint32_t *indir2_block = NULL;
189 int indir2_blkno = -1;
190 static unsigned int inode_size;
193 static int ext2fs_blockgroup
194 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp) {
197 unsigned int desc_per_blk;
199 desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
201 blkno = __le32_to_cpu(data->sblock.first_data_block) + 1 +
202 group / desc_per_blk;
203 blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
205 printf ("ext2fs read %d group descriptor (blkno %d blkoff %d)\n",
206 group, blkno, blkoff);
208 return (ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE(data),
209 blkoff, sizeof(struct ext2_block_group), (char *)blkgrp));
214 static int ext2fs_read_inode
215 (struct ext2_data *data, int ino, struct ext2_inode *inode) {
216 struct ext2_block_group blkgrp;
217 struct ext2_sblock *sblock = &data->sblock;
218 int inodes_per_block;
225 printf ("ext2fs read inode %d, inode_size %d\n", ino, inode_size);
227 /* It is easier to calculate if the first inode is 0. */
229 status = ext2fs_blockgroup (data, ino / __le32_to_cpu
230 (sblock->inodes_per_group), &blkgrp);
235 inodes_per_block = EXT2_BLOCK_SIZE(data) / inode_size;
237 blkno = __le32_to_cpu (blkgrp.inode_table_id) +
238 (ino % __le32_to_cpu (sblock->inodes_per_group))
240 blkoff = (ino % inodes_per_block) * inode_size;
242 printf ("ext2fs read inode blkno %d blkoff %d\n", blkno, blkoff);
244 /* Read the inode. */
245 status = ext2fs_devread (blkno << LOG2_EXT2_BLOCK_SIZE (data), blkoff,
246 sizeof (struct ext2_inode), (char *) inode);
254 #ifndef CONFIG_CMD_EXT4_WRITE
255 void ext2fs_free_node (ext2fs_node_t node, ext2fs_node_t currroot)
257 void ext2fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
260 if ((node != &ext2fs_root->diropen) && (node != currroot)) {
266 #ifndef CONFIG_CMD_EXT4_WRITE
267 static int ext2fs_read_block (ext2fs_node_t node, int fileblock)
269 static int ext2fs_read_block(struct ext2fs_node *node, int fileblock)
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) {
281 blknr = __le32_to_cpu (inode->b.blocks.dir_blocks[fileblock]);
284 else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
285 if (indir1_block == NULL) {
286 indir1_block = (uint32_t *) malloc (blksz);
287 if (indir1_block == NULL) {
288 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
294 if (blksz != indir1_size) {
299 indir1_block = (uint32_t *) malloc (blksz);
300 if (indir1_block == NULL) {
301 printf ("** ext2fs read block (indir 1) malloc failed. **\n");
306 if ((__le32_to_cpu (inode->b.blocks.indir_block) <<
307 log2_blksz) != indir1_blkno) {
308 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.indir_block) << log2_blksz,
310 (char *) indir1_block);
312 printf ("** ext2fs read block (indir 1) failed. **\n");
316 __le32_to_cpu (inode->b.blocks.
317 indir_block) << log2_blksz;
319 blknr = __le32_to_cpu (indir1_block
320 [fileblock - INDIRECT_BLOCKS]);
322 /* Double indirect. */
324 (INDIRECT_BLOCKS + (blksz / 4 * (blksz / 4 + 1)))) {
325 unsigned int perblock = blksz / 4;
326 unsigned int rblock = fileblock - (INDIRECT_BLOCKS
329 if (indir1_block == NULL) {
330 indir1_block = (uint32_t *) malloc (blksz);
331 if (indir1_block == NULL) {
332 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
338 if (blksz != indir1_size) {
343 indir1_block = (uint32_t *) malloc (blksz);
344 if (indir1_block == NULL) {
345 printf ("** ext2fs read block (indir 2 1) malloc failed. **\n");
350 if ((__le32_to_cpu (inode->b.blocks.double_indir_block) <<
351 log2_blksz) != indir1_blkno) {
352 status = ext2fs_devread (__le32_to_cpu(inode->b.blocks.double_indir_block) << log2_blksz,
354 (char *) indir1_block);
356 printf ("** ext2fs read block (indir 2 1) failed. **\n");
360 __le32_to_cpu (inode->b.blocks.double_indir_block) << log2_blksz;
363 if (indir2_block == NULL) {
364 indir2_block = (uint32_t *) malloc (blksz);
365 if (indir2_block == NULL) {
366 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
372 if (blksz != indir2_size) {
377 indir2_block = (uint32_t *) malloc (blksz);
378 if (indir2_block == NULL) {
379 printf ("** ext2fs read block (indir 2 2) malloc failed. **\n");
384 if ((__le32_to_cpu (indir1_block[rblock / perblock]) <<
385 log2_blksz) != indir2_blkno) {
386 status = ext2fs_devread (__le32_to_cpu(indir1_block[rblock / perblock]) << log2_blksz,
388 (char *) indir2_block);
390 printf ("** ext2fs read block (indir 2 2) failed. **\n");
394 __le32_to_cpu (indir1_block[rblock / perblock]) << log2_blksz;
396 blknr = __le32_to_cpu (indir2_block[rblock % perblock]);
398 /* Tripple indirect. */
400 printf ("** ext2fs doesn't support tripple indirect blocks. **\n");
404 printf ("ext2fs_read_block %08x\n", blknr);
411 #ifndef CONFIG_CMD_EXT4_WRITE
412 (ext2fs_node_t node, int pos, unsigned int len, char *buf)
414 (struct ext2fs_node *node, int pos, unsigned int len, char *buf)
419 int log2blocksize = LOG2_EXT2_BLOCK_SIZE (node->data);
420 int blocksize = 1 << (log2blocksize + DISK_SECTOR_BITS);
421 unsigned int filesize = __le32_to_cpu(node->inode.size);
423 /* Adjust len so it we can't read past the end of the file. */
424 if (len > filesize) {
427 blockcnt = ((len + pos) + blocksize - 1) / blocksize;
429 for (i = pos / blocksize; i < blockcnt; i++) {
431 int blockoff = pos % blocksize;
432 int blockend = blocksize;
436 blknr = ext2fs_read_block (node, i);
440 blknr = blknr << log2blocksize;
443 if (i == blockcnt - 1) {
444 blockend = (len + pos) % blocksize;
446 /* The last portion is exactly blocksize. */
448 blockend = blocksize;
453 if (i == pos / blocksize) {
454 skipfirst = blockoff;
455 blockend -= skipfirst;
458 /* If the block number is 0 this block is not stored on disk but
459 is zero filled instead. */
463 status = ext2fs_devread (blknr, skipfirst, blockend, buf);
468 memset (buf, 0, blocksize - skipfirst);
470 buf += blocksize - skipfirst;
476 #ifndef CONFIG_CMD_EXT4_WRITE
477 static int ext2fs_iterate_dir (ext2fs_node_t dir, char *name, ext2fs_node_t * fnode, int *ftype)
479 int ext2fs_iterate_dir(struct ext2fs_node *dir, char *name,
480 struct ext2fs_node **fnode, int *ftype)
483 unsigned int fpos = 0;
485 struct ext2fs_node *diro = (struct ext2fs_node *) dir;
489 printf ("Iterate dir %s\n", name);
490 #endif /* of DEBUG */
491 if (!diro->inode_read) {
492 status = ext2fs_read_inode (diro->data, diro->ino,
498 /* Search the file. */
499 while (fpos < __le32_to_cpu (diro->inode.size)) {
500 struct ext2_dirent dirent;
502 status = ext2fs_read_file (diro, fpos,
503 sizeof (struct ext2_dirent),
508 if (dirent.namelen != 0) {
509 char filename[dirent.namelen + 1];
510 #ifndef CONFIG_CMD_EXT4_WRITE
513 struct ext2fs_node *fdiro;
515 int type = FILETYPE_UNKNOWN;
517 status = ext2fs_read_file (diro,
518 fpos + sizeof (struct ext2_dirent),
519 dirent.namelen, filename);
523 fdiro = malloc (sizeof (struct ext2fs_node));
528 fdiro->data = diro->data;
529 fdiro->ino = __le32_to_cpu (dirent.inode);
531 filename[dirent.namelen] = '\0';
533 if (dirent.filetype != FILETYPE_UNKNOWN) {
534 fdiro->inode_read = 0;
536 if (dirent.filetype == FILETYPE_DIRECTORY) {
537 type = FILETYPE_DIRECTORY;
538 } else if (dirent.filetype ==
540 type = FILETYPE_SYMLINK;
541 } else if (dirent.filetype == FILETYPE_REG) {
545 /* The filetype can not be read from the dirent, get it from inode */
547 status = ext2fs_read_inode (diro->data,
548 __le32_to_cpu(dirent.inode),
554 fdiro->inode_read = 1;
556 if ((__le16_to_cpu (fdiro->inode.mode) &
557 FILETYPE_INO_MASK) ==
558 FILETYPE_INO_DIRECTORY) {
559 type = FILETYPE_DIRECTORY;
560 } else if ((__le16_to_cpu (fdiro->inode.mode)
561 & FILETYPE_INO_MASK) ==
562 FILETYPE_INO_SYMLINK) {
563 type = FILETYPE_SYMLINK;
564 } else if ((__le16_to_cpu (fdiro->inode.mode)
565 & FILETYPE_INO_MASK) ==
571 printf ("iterate >%s<\n", filename);
572 #endif /* of DEBUG */
573 if ((name != NULL) && (fnode != NULL)
574 && (ftype != NULL)) {
575 if (strcmp (filename, name) == 0) {
581 if (fdiro->inode_read == 0) {
582 status = ext2fs_read_inode (diro->data,
583 __le32_to_cpu (dirent.inode),
589 fdiro->inode_read = 1;
592 case FILETYPE_DIRECTORY:
595 case FILETYPE_SYMLINK:
606 __le32_to_cpu (fdiro->inode.size),
611 fpos += __le16_to_cpu (dirent.direntlen);
617 #ifndef CONFIG_CMD_EXT4_WRITE
618 static char *ext2fs_read_symlink (ext2fs_node_t node)
620 static char *ext2fs_read_symlink(struct ext2fs_node *node)
624 struct ext2fs_node *diro = node;
627 if (!diro->inode_read) {
628 status = ext2fs_read_inode (diro->data, diro->ino,
634 symlink = malloc (__le32_to_cpu (diro->inode.size) + 1);
638 /* If the filesize of the symlink is bigger than
639 60 the symlink is stored in a separate block,
640 otherwise it is stored in the inode. */
641 if (__le32_to_cpu (diro->inode.size) <= 60) {
642 strncpy (symlink, diro->inode.b.symlink,
643 __le32_to_cpu (diro->inode.size));
645 status = ext2fs_read_file (diro, 0,
646 __le32_to_cpu (diro->inode.size),
653 symlink[__le32_to_cpu (diro->inode.size)] = '\0';
658 int ext2fs_find_file1
659 #ifndef CONFIG_CMD_EXT4_WRITE
660 (const char *currpath,
661 ext2fs_node_t currroot, ext2fs_node_t * currfound, int *foundtype)
663 (const char *currpath, struct ext2fs_node *currroot,
664 struct ext2fs_node **currfound, int *foundtype)
668 char fpath[strlen (currpath) + 1];
672 int type = FILETYPE_DIRECTORY;
673 #ifndef CONFIG_CMD_EXT4_WRITE
674 ext2fs_node_t currnode = currroot;
675 ext2fs_node_t oldnode = currroot;
677 struct ext2fs_node *currnode = currroot;
678 struct ext2fs_node *oldnode = currroot;
681 strncpy (fpath, currpath, strlen (currpath) + 1);
683 /* Remove all leading slashes. */
684 while (*name == '/') {
688 *currfound = currnode;
695 /* Extract the actual part from the pathname. */
696 next = strchr (name, '/');
698 /* Remove all leading slashes. */
699 while (*next == '/') {
704 /* At this point it is expected that the current node is a directory, check if this is true. */
705 if (type != FILETYPE_DIRECTORY) {
706 ext2fs_free_node (currnode, currroot);
712 /* Iterate over the directory. */
713 found = ext2fs_iterate_dir (currnode, name, &currnode, &type);
721 /* Read in the symlink and follow it. */
722 if (type == FILETYPE_SYMLINK) {
725 /* Test if the symlink does not loop. */
726 if (++symlinknest == 8) {
727 ext2fs_free_node (currnode, currroot);
728 ext2fs_free_node (oldnode, currroot);
732 symlink = ext2fs_read_symlink (currnode);
733 ext2fs_free_node (currnode, currroot);
736 ext2fs_free_node (oldnode, currroot);
740 printf ("Got symlink >%s<\n", symlink);
741 #endif /* of DEBUG */
742 /* The symlink is an absolute path, go back to the root inode. */
743 if (symlink[0] == '/') {
744 ext2fs_free_node (oldnode, currroot);
745 oldnode = &ext2fs_root->diropen;
748 /* Lookup the node the symlink points to. */
749 status = ext2fs_find_file1 (symlink, oldnode,
755 ext2fs_free_node (oldnode, currroot);
760 ext2fs_free_node (oldnode, currroot);
762 /* Found the node! */
763 if (!next || *next == '\0') {
764 *currfound = currnode;
775 #ifndef CONFIG_CMD_EXT4_WRITE
777 ext2fs_node_t rootnode, ext2fs_node_t * foundnode, int expecttype)
779 (const char *path, struct ext2fs_node *rootnode,
780 struct ext2fs_node **foundnode, int expecttype)
784 int foundtype = FILETYPE_DIRECTORY;
792 status = ext2fs_find_file1 (path, rootnode, foundnode, &foundtype);
796 /* Check if the node that was found was of the expected type. */
797 if ((expecttype == FILETYPE_REG) && (foundtype != expecttype)) {
799 } else if ((expecttype == FILETYPE_DIRECTORY)
800 && (foundtype != expecttype)) {
807 int ext2fs_ls (char *dirname) {
808 #ifndef CONFIG_CMD_EXT4_WRITE
809 ext2fs_node_t dirnode;
811 struct ext2fs_node *dirnode;
815 if (ext2fs_root == NULL) {
819 status = ext2fs_find_file (dirname, &ext2fs_root->diropen, &dirnode,
822 printf ("** Can not find directory. **\n");
825 ext2fs_iterate_dir (dirnode, NULL, NULL, NULL);
826 ext2fs_free_node (dirnode, &ext2fs_root->diropen);
831 int ext2fs_open (char *filename) {
832 #ifndef CONFIG_CMD_EXT4_WRITE
833 ext2fs_node_t fdiro = NULL;
835 struct ext2fs_node *fdiro = NULL;
840 if (ext2fs_root == NULL) {
844 status = ext2fs_find_file (filename, &ext2fs_root->diropen, &fdiro,
849 if (!fdiro->inode_read) {
850 status = ext2fs_read_inode (fdiro->data, fdiro->ino,
856 len = __le32_to_cpu (fdiro->inode.size);
861 ext2fs_free_node (fdiro, &ext2fs_root->diropen);
867 int ext2fs_close(void)
869 if ((ext2fs_file != NULL) && (ext2fs_root != NULL)) {
870 ext2fs_free_node (ext2fs_file, &ext2fs_root->diropen);
873 if (ext2fs_root != NULL) {
877 if (indir1_block != NULL) {
883 if (indir2_block != NULL) {
893 int ext2fs_read (char *buf, unsigned len) {
896 if (ext2fs_root == NULL) {
900 if (ext2fs_file == NULL) {
904 status = ext2fs_read_file (ext2fs_file, 0, len, buf);
909 int ext2fs_mount (unsigned part_length) {
910 struct ext2_data *data;
913 data = malloc (sizeof (struct ext2_data));
917 /* Read the superblock. */
918 status = ext2fs_devread (1 * 2, 0, sizeof (struct ext2_sblock),
919 (char *) &data->sblock);
923 /* Make sure this is an ext2 filesystem. */
924 if (__le16_to_cpu (data->sblock.magic) != EXT2_MAGIC) {
927 if (__le32_to_cpu(data->sblock.revision_level == 0)) {
930 inode_size = __le16_to_cpu(data->sblock.inode_size);
933 printf("EXT2 rev %d, inode_size %d\n",
934 __le32_to_cpu(data->sblock.revision_level), inode_size);
936 data->diropen.data = data;
937 data->diropen.ino = 2;
938 data->diropen.inode_read = 1;
939 data->inode = &data->diropen.inode;
941 status = ext2fs_read_inode (data, 2, data->inode);
951 printf ("Failed to mount ext2 filesystem...\n");