summaryrefslogtreecommitdiffstats
path: root/src/ufs.c
blob: ebcf448356f37231504856544e8440e11dc70d73 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*

    File: ufs.c

    Copyright (C) 2005-2007 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 "ufs.h"
#include "fnctdsk.h"
#include "log.h"

static void set_ufs_info(const struct ufs_super_block *sb, partition_t *partition);
static int test_ufs(const disk_t *disk_car, const struct ufs_super_block *sb, const partition_t *partition, const int verbose);

int check_ufs(disk_t *disk_car,partition_t *partition,const int verbose)
{
  const struct ufs_super_block *sb;
  unsigned char *buffer;
  buffer=(unsigned char*)MALLOC(UFS_SUPERBLOCK_SIZE);
  sb=(const struct ufs_super_block*)buffer;
  if(disk_car->pread(disk_car, buffer, UFS_SUPERBLOCK_SIZE, partition->part_offset + UFS_SBLOCK) != UFS_SUPERBLOCK_SIZE)
  {
    free(buffer);
    return 1;
  }
  if(test_ufs(disk_car, sb, partition, verbose)!=0)
  {
    free(buffer);
    return 1;
  }
  set_ufs_info(sb, partition);
  free(buffer);
  return 0;
}

static int test_ufs(const disk_t *disk_car, const struct ufs_super_block *sb, const partition_t *partition, const int verbose)
{
  if(le32(sb->fs_magic)==UFS_MAGIC && le32(sb->fs_size) > 0 &&
    (le32(sb->fs_fsize)==512 || le32(sb->fs_fsize)==1024 || le32(sb->fs_fsize)==2048 || le32(sb->fs_fsize)==4096))
  {
    if(verbose>1)
      log_info("\nUFS Marker at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    return 0;
  }
  if(be32(sb->fs_magic)==UFS_MAGIC && be32(sb->fs_size) > 0 &&
    (be32(sb->fs_fsize)==512 || be32(sb->fs_fsize)==1024 || be32(sb->fs_fsize)==2048 || be32(sb->fs_fsize)==4096))
  {
    if(verbose>1)
      log_info("\nUFS Marker at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    return 0;
  }
  if(le32(sb->fs_magic)==UFS2_MAGIC && le64(sb->fs_u11.fs_u2.fs_size) > 0 &&
    (le32(sb->fs_fsize)==512 || le32(sb->fs_fsize)==1024 || le32(sb->fs_fsize)==2048 || le32(sb->fs_fsize)==4096))
  {
    if(verbose>1)
      log_info("\nUFS2 Marker at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    return 0;
  }
  if(be32(sb->fs_magic)==UFS2_MAGIC && be64(sb->fs_u11.fs_u2.fs_size) > 0 &&
    (be32(sb->fs_fsize)==512 || be32(sb->fs_fsize)==1024 || be32(sb->fs_fsize)==2048 || be32(sb->fs_fsize)==4096))
  {
    if(verbose>1)
      log_info("\nUFS2 Marker at %u/%u/%u\n", offset2cylinder(disk_car,partition->part_offset),offset2head(disk_car,partition->part_offset),offset2sector(disk_car,partition->part_offset));
    return 0;
  }
  return 1;
}

int recover_ufs(disk_t *disk_car, const struct ufs_super_block *sb, partition_t *partition,const int verbose, const int dump_ind)
{
  if(test_ufs(disk_car, sb, partition, verbose)!=0)
    return 1;
  if(dump_ind!=0)
  {
    log_info("recover_ufs\n");
    dump_log(sb,sizeof(*sb));
  }
  set_ufs_info(sb, partition);
  switch(partition->upart_type)
  {
    case UP_UFS_LE:
      partition->part_size = (uint64_t)le32(sb->fs_size)*le32(sb->fs_fsize);
      if(verbose>1)
      {
	log_info("fs_size %lu, fs_fsize %lu\n", (long unsigned)le32(sb->fs_size), (long unsigned)le32(sb->fs_fsize));
	log_info("fs_sblkno %lu\n", (long unsigned)le32(sb->fs_sblkno));
      }
      break;
    case UP_UFS2_LE:
      partition->part_size = (uint64_t)le64(sb->fs_u11.fs_u2.fs_size)*le32(sb->fs_fsize);
      if(verbose>1)
      {
	log_info("fs_size %lu, fs_fsize %lu\n", (long unsigned)le64(sb->fs_u11.fs_u2.fs_size), (long unsigned)le32(sb->fs_fsize));
	log_info("fs_sblkno %lu\n", (long unsigned)le32(sb->fs_sblkno));
	log_info("fs_sblockloc %llu\n", (long long unsigned)le64(sb->fs_u11.fs_u2.fs_sblockloc));
      }
      break;
    case UP_UFS:
      partition->part_size = (uint64_t)be32(sb->fs_size)*be32(sb->fs_fsize);
      if(verbose>1)
      {
	log_info("fs_size %lu, fs_fsize %lu\n",(long unsigned)be32(sb->fs_size),(long unsigned)be32(sb->fs_fsize));
	log_info("fs_sblkno %lu\n", (long unsigned)be32(sb->fs_sblkno));
      }
      break;
    case UP_UFS2:
      partition->part_size = (uint64_t)be64(sb->fs_u11.fs_u2.fs_size)*be32(sb->fs_fsize);
      if(verbose>1)
      {
	log_info("fs_size %lu, fs_fsize %lu\n", (long unsigned)be64(sb->fs_u11.fs_u2.fs_size), (long unsigned)be32(sb->fs_fsize));
	log_info("fs_sblkno %lu\n", (long unsigned)be32(sb->fs_sblkno));
	log_info("fs_sblockloc %llu\n", (long long unsigned)be64(sb->fs_u11.fs_u2.fs_sblockloc));
      }
      break;
    default: /* BUG if hit*/
      break;
  }
  if(strcmp(partition->fsname,"/")==0)
  {
    partition->part_type_sun = (unsigned char)PSUN_ROOT;
    partition->part_type_gpt=GPT_ENT_TYPE_SOLARIS_ROOT;
  }
  else if(strcmp(partition->fsname,"/var")==0)
  {
    partition->part_type_sun = (unsigned char)PSUN_VAR;
    partition->part_type_gpt=GPT_ENT_TYPE_SOLARIS_VAR;
  }
  else if(strcmp(partition->fsname,"/usr")==0)
  {
    partition->part_type_sun = (unsigned char)PSUN_USR;
    partition->part_type_gpt=GPT_ENT_TYPE_SOLARIS_USR;
  }
  else if(strcmp(partition->fsname,"/export/home")==0)
  {
    partition->part_type_sun = (unsigned char)PSUN_HOME;
    partition->part_type_gpt=GPT_ENT_TYPE_SOLARIS_HOME;
  }
  else
  {
    partition->part_type_sun = (unsigned char)PSUN_ROOT;
    partition->part_type_gpt=GPT_ENT_TYPE_SOLARIS_HOME;
  }
  return 0;
}

static void set_ufs_info(const struct ufs_super_block *sb, partition_t *partition)
{
  partition->fsname[0]='\0';
  partition->info[0]='\0';
  if(le32(sb->fs_magic)==UFS_MAGIC)
  {
    partition->upart_type = UP_UFS_LE;
    partition->blocksize=le32(sb->fs_fsize);
    set_part_name(partition,(const char*)sb->fs_u11.fs_u1.fs_fsmnt,sizeof(sb->fs_u11.fs_u1.fs_fsmnt));
    snprintf(partition->info, sizeof(partition->info),
	"UFS1 blocksize=%u", partition->blocksize);
  }
  if(be32(sb->fs_magic)==UFS_MAGIC)
  {
    partition->upart_type = UP_UFS;
    partition->blocksize=be32(sb->fs_fsize);
    set_part_name(partition,(const char*)sb->fs_u11.fs_u1.fs_fsmnt,sizeof(sb->fs_u11.fs_u1.fs_fsmnt));
    snprintf(partition->info, sizeof(partition->info),
	"UFS1 blocksize=%u", partition->blocksize);
  }
  if(le32(sb->fs_magic)==UFS2_MAGIC)
  {
    partition->blocksize=le32(sb->fs_fsize);
    partition->upart_type = UP_UFS2_LE;
    set_part_name(partition,(const char*)sb->fs_u11.fs_u2.fs_fsmnt,sizeof(sb->fs_u11.fs_u2.fs_fsmnt));
    snprintf(partition->info, sizeof(partition->info),
	"UFS2 blocksize=%u", partition->blocksize);
  }
  if(be32(sb->fs_magic)==UFS2_MAGIC)
  {
    partition->upart_type = UP_UFS2;
    partition->blocksize=be32(sb->fs_fsize);
    set_part_name(partition,(const char*)sb->fs_u11.fs_u2.fs_fsmnt,sizeof(sb->fs_u11.fs_u2.fs_fsmnt));
    snprintf(partition->info, sizeof(partition->info),
	"UFS2 blocksize=%u", partition->blocksize);
  }
}