summaryrefslogtreecommitdiffstats
path: root/src/ext2_common.c
diff options
context:
space:
mode:
authorChristophe Grenier <grenier@cgsecurity.org>2013-08-28 08:29:32 +0200
committerChristophe Grenier <grenier@cgsecurity.org>2013-08-28 08:29:32 +0200
commitda8028ea3fb282cee6fd54f4ac8c8a53dacfa967 (patch)
treed52e4641fd338d51423cd1bc9f38e120d3e60ad6 /src/ext2_common.c
parented174946a0dd7c01650e43edec55d56a3373284a (diff)
TestDisk: handle ext4 with 64 bit blocks (EXT4_FEATURE_INCOMPAT_64BIT)
Diffstat (limited to 'src/ext2_common.c')
-rw-r--r--src/ext2_common.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/ext2_common.c b/src/ext2_common.c
new file mode 100644
index 0000000..021bcf0
--- /dev/null
+++ b/src/ext2_common.c
@@ -0,0 +1,92 @@
+/*
+
+ File: ext2_common.c
+
+ Copyright (C) 1998-2013 Christophe GRENIER <grenier@cgsecurity.org>
+
+ This software is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write the Free Software Foundation, Inc., 51
+ Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+ */
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
+#include "types.h"
+#include "common.h"
+#include "ext2.h"
+#include "ext2_common.h"
+
+uint64_t td_ext2fs_blocks_count(const struct ext2_super_block *super)
+{
+ return le32(super->s_blocks_count) |
+ (EXT2_HAS_INCOMPAT_FEATURE(super, EXT4_FEATURE_INCOMPAT_64BIT) ?
+ (uint64_t) le32(super->s_blocks_count_hi) << 32 : 0);
+}
+
+uint64_t td_ext2fs_free_blocks_count(const struct ext2_super_block *super)
+{
+ return le32(super->s_free_blocks_count) |
+ (EXT2_HAS_INCOMPAT_FEATURE(super, EXT4_FEATURE_INCOMPAT_64BIT) ?
+ (uint64_t) le32(super->s_free_blocks_hi) << 32 : 0);
+}
+
+int test_EXT2(const struct ext2_super_block *sb, partition_t *partition)
+{
+ /* There is a little offset ... */
+ if(le16(sb->s_magic)!=EXT2_SUPER_MAGIC)
+ return 1;
+ if (td_ext2fs_free_blocks_count(sb) > td_ext2fs_blocks_count(sb))
+ return 2;
+ if (le32(sb->s_free_inodes_count) > le32(sb->s_inodes_count))
+ return 3;
+ if (le16(sb->s_errors)!=0 &&
+ (le16(sb->s_errors) != EXT2_ERRORS_CONTINUE) &&
+ (le16(sb->s_errors) != EXT2_ERRORS_RO) &&
+ (le16(sb->s_errors) != EXT2_ERRORS_PANIC))
+ return 4;
+ if ((le16(sb->s_state) & ~(EXT2_VALID_FS | EXT2_ERROR_FS))!=0)
+ return 5;
+ if(td_ext2fs_blocks_count(sb) == 0) /* reject empty filesystem */
+ return 6;
+ if(le32(sb->s_log_block_size)>2) /* block size max = 4096, can be 8192 on alpha */
+ return 7;
+ if(le32(sb->s_blocks_per_group)==0)
+ return 8;
+ if(partition==NULL)
+ return 0;
+ if(partition->part_size!=0 &&
+ partition->part_size < td_ext2fs_blocks_count(sb) *
+ (EXT2_MIN_BLOCK_SIZE<<le32(sb->s_log_block_size)))
+ return 8;
+ if(EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT4_FEATURE_RO_COMPAT_HUGE_FILE)!=0 ||
+ EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT4_FEATURE_RO_COMPAT_GDT_CSUM)!=0 ||
+ EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT4_FEATURE_RO_COMPAT_DIR_NLINK)!=0 ||
+ EXT2_HAS_RO_COMPAT_FEATURE(sb,EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)!=0 ||
+ EXT2_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_64BIT)!=0 ||
+ EXT2_HAS_INCOMPAT_FEATURE(sb,EXT4_FEATURE_INCOMPAT_MMP)!=0)
+ partition->upart_type=UP_EXT4;
+ else if(EXT2_HAS_COMPAT_FEATURE(sb,EXT3_FEATURE_COMPAT_HAS_JOURNAL)!=0)
+ partition->upart_type=UP_EXT3;
+ else
+ partition->upart_type=UP_EXT2;
+ return 0;
+}