summaryrefslogtreecommitdiffstats
path: root/src/ext2_common.c
blob: 3eaf14e10091b8f68c8e5cc6095b91035b0ffcf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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, const 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;
  switch(le32(sb->s_log_block_size))
  {
    case 0:
    case 1:
    case 2: /* block size = 4096 (default) */
    case 3: /* can be 8192 on alpha */
    case 4: /* non standard blocksize */
    case 5: /* non standard blocksize */
    case 6: /* 64 KiB */
      break;
    default:
      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;
  return 0;
}