1 /*
2 
3     File: gfs2.c
4 
5     Copyright (C) 2011 Christophe GRENIER <grenier@cgsecurity.org>
6 
7     This software is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write the Free Software Foundation, Inc., 51
19     Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 #include <stdio.h>
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 
31 #include "types.h"
32 #include "common.h"
33 #include "gfs2.h"
34 #include "fnctdsk.h"
35 #include "log.h"
36 
set_gfs2_info(partition_t * partition)37 static void set_gfs2_info(partition_t *partition)
38 {
39   partition->upart_type=UP_GFS2;
40   partition->info[0]='\0';
41 }
42 
test_gfs2(disk_t * disk,const struct gfs2_sb * sb,const partition_t * partition,const int dump_ind)43 static int test_gfs2(disk_t *disk, const struct gfs2_sb *sb, const partition_t *partition, const int dump_ind)
44 {
45   if(sb->sb_header.mh_magic != be32(GFS2_MAGIC))
46     return 1;
47   if(sb->sb_header.mh_format != be32(GFS2_FORMAT_SB))
48     return 1;
49   if(partition==NULL)
50     return 0;
51   if(dump_ind!=0)
52   {
53     log_info("\ngfs2 magic value at %u/%u/%u\n",
54 	offset2cylinder(disk, partition->part_offset),
55 	offset2head(disk, partition->part_offset),
56 	offset2sector(disk, partition->part_offset));
57     dump_log(sb,DEFAULT_SECTOR_SIZE);
58   }
59   return 0;
60 }
61 
check_gfs2(disk_t * disk,partition_t * partition)62 int check_gfs2(disk_t *disk, partition_t *partition)
63 {
64   unsigned char *buffer;
65   buffer=(unsigned char*)MALLOC(512);
66   if(disk->pread(disk, buffer, 512, partition->part_offset + (GFS2_SB_ADDR << GFS2_BASIC_BLOCK_SHIFT)) != 512)
67   {
68     free(buffer);
69     return 1;
70   }
71   if(test_gfs2(disk, (const struct gfs2_sb *)buffer, partition,0)!=0)
72   {
73     free(buffer);
74     return 1;
75   }
76   set_gfs2_info(partition);
77   free(buffer);
78   return 0;
79 }
80 
recover_gfs2(disk_t * disk,const struct gfs2_sb * sb,partition_t * partition,const int dump_ind)81 int recover_gfs2(disk_t *disk, const struct gfs2_sb *sb, partition_t *partition, const int dump_ind)
82 {
83   if(test_gfs2(disk,sb,partition,dump_ind)!=0)
84     return 1;
85   set_gfs2_info(partition);
86   partition->part_size=disk->sector_size;
87   partition->part_type_i386=(unsigned char)P_LINUX;
88   return 0;
89 }
90