summaryrefslogtreecommitdiffstats
path: root/src/md.c
blob: 5d25331d81cc22596d5e71b3ac6100c8d9897220 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*

    File: md.c

    Copyright (C) 1998-2008 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 "md.h"
#include "fnctdsk.h"
#include "log.h"
static int test_MD(disk_t *disk_car, const struct mdp_superblock_s *sb, const partition_t *partition, const int dump_ind);
static int test_MD_be(disk_t *disk_car, const struct mdp_superblock_s *sb, const partition_t *partition, const int dump_ind);
static void set_MD_info(const struct mdp_superblock_s *sb, partition_t *partition, const int verbose);
static void set_MD_info_be(const struct mdp_superblock_s *sb, partition_t *partition, const int verbose);

int check_MD(disk_t *disk_car, partition_t *partition, const int verbose)
{
  unsigned char *buffer=(unsigned char*)MALLOC(MD_SB_BYTES);
  /* MD version 1.1 */
  if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset) == MD_SB_BYTES)
  {
    const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer;
    if(le32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	le32(sb1->major_version)==1 &&
	le64(sb1->super_offset)==0 &&
	test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
    {
      log_info("check_MD 1.1\n");
      set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose);
      free(buffer);
      return 0;
    }
    if(be32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	 be32(sb1->major_version)==1 &&
	 be64(sb1->super_offset)==0 &&
        test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
    {
      log_info("check_MD 1.1 (BigEndian)\n");
      set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose);
      free(buffer);
      return 0;
    }
  }
  /* MD version 1.2 */
  if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset + 4096) == MD_SB_BYTES)
  {
    const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer;
    if(le32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	le32(sb1->major_version)==1 &&
        le64(sb1->super_offset)==8 &&
        test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
    {
      log_info("check_MD 1.2\n");
      set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose);
      free(buffer);
      return 0;
    }
    if(be32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	be32(sb1->major_version)==1 &&
        be64(sb1->super_offset)==8 &&
        test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
    {
      log_info("check_MD 1.2 (BigEndian)\n");
      set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose);
      free(buffer);
      return 0;
    }
  }
  /* MD version 0.90 */
  {
    const struct mdp_superblock_s *sb=(const struct mdp_superblock_s *)buffer;
    const uint64_t offset=MD_NEW_SIZE_SECTORS(partition->part_size/512)*512;
    if(verbose>1)
    {
      log_verbose("Raid md 0.90 offset %llu\n", (long long unsigned)offset/512);
    }
    if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset + offset) == MD_SB_BYTES)
    {
      if(le32(sb->md_magic)==(unsigned int)MD_SB_MAGIC &&
	  le32(sb->major_version)==0 &&
	  test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
      {
        log_info("check_MD 0.90\n");
        set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose);
        free(buffer);
        return 0;
      }
      if(be32(sb->md_magic)==(unsigned int)MD_SB_MAGIC &&
	  be32(sb->major_version)==0 &&
	  test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
      {
        log_info("check_MD 0.90 (BigEndian)\n");
        set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose);
        free(buffer);
        return 0;
      }
    }
  }
  /* MD version 1.0 */
  if(partition->part_size > 8*2*512)
  {
    const uint64_t offset=(uint64_t)(((partition->part_size/512)-8*2) & ~(4*2-1))*512;
    if(verbose>1)
    {
      log_verbose("Raid md 1.0 offset %llu\n", (long long unsigned)offset/512);
    }
    if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset + offset) == MD_SB_BYTES)
    {
      const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer;
      if(le32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	  le32(sb1->major_version)==1 &&
          le64(sb1->super_offset)==(offset/512) &&
          test_MD(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
      {
        log_info("check_MD 1.0\n");
        set_MD_info((struct mdp_superblock_s*)buffer, partition, verbose);
        free(buffer);
        return 0;
      }
      if(be32(sb1->md_magic)==(unsigned int)MD_SB_MAGIC &&
	  be32(sb1->major_version)==1 &&
          be64(sb1->super_offset)==(offset/512) &&
          test_MD_be(disk_car, (struct mdp_superblock_s*)buffer, partition, 0)==0)
      {
        log_info("check_MD 1.0 (BigEndian)\n");
        set_MD_info_be((struct mdp_superblock_s*)buffer, partition, verbose);
        free(buffer);
        return 0;
      }
    }
  }
  free(buffer);
  return 1;
}

int recover_MD_from_partition(disk_t *disk_car, partition_t *partition, const int verbose)
{
  unsigned char *buffer=(unsigned char*)MALLOC(MD_SB_BYTES);
  /* MD version 0.90 */
  {
    uint64_t offset=MD_NEW_SIZE_SECTORS(partition->part_size/512)*512;
    if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset + offset) == MD_SB_BYTES)
    {
      if(recover_MD(disk_car,(struct mdp_superblock_s*)buffer,partition,verbose,0)==0)
      {
        free(buffer);
        return 0;
      }
    }
  }
  /* MD version 1.0 */
  if(partition->part_size > 8*2*512)
  {
    uint64_t offset=(((partition->part_size/512)-8*2) & ~(4*2-1))*512;
    if(disk_car->pread(disk_car, buffer, MD_SB_BYTES, partition->part_offset + offset) == MD_SB_BYTES)
    {
      const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)buffer;
      if(le32(sb1->major_version)==1 &&
          recover_MD(disk_car,(struct mdp_superblock_s*)buffer,partition,verbose,0)==0)
      {
        partition->part_offset-=le64(sb1->super_offset)*512-offset;
        free(buffer);
        return 0;
      }
    }
  }
  /* md 1.1 & 1.2 don't need special operation to be recovered */
  free(buffer);
  return 1;
}

int recover_MD(disk_t *disk_car, const struct mdp_superblock_s *sb, partition_t *partition, const int verbose, const int dump_ind)
{
  if(test_MD(disk_car, sb, partition, dump_ind)==0)
  {
    set_MD_info(sb, partition, verbose);
    partition->part_type_i386=P_RAID;
    partition->part_type_sun=PSUN_RAID;
    partition->part_type_gpt=GPT_ENT_TYPE_LINUX_RAID;
    if(le32(sb->major_version)==0)
    {
      partition->part_size=(uint64_t)(le32(sb->size)<<1)*512+MD_RESERVED_BYTES;	/* 512-byte sectors */
      memcpy(&partition->part_uuid, &sb->set_uuid0, 4);
      memcpy((char*)(&partition->part_uuid)+4, &sb->set_uuid1, 3*4);
    }
    else
    {
      const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)sb;
      partition->part_size=(uint64_t)le64(sb1->size) * 512 + 4096;	/* 512-byte sectors */
      memcpy(&partition->part_uuid, &sb1->set_uuid, 16);
    }
    return 0;
  }
  if(test_MD_be(disk_car, sb, partition, dump_ind)==0)
  {
    set_MD_info_be(sb, partition, verbose);
    partition->part_type_i386=P_RAID;
    partition->part_type_sun=PSUN_RAID;
    partition->part_type_gpt=GPT_ENT_TYPE_LINUX_RAID;
    if(be32(sb->major_version)==0)
    {
      partition->part_size=(uint64_t)(be32(sb->size)<<1)*512+MD_RESERVED_BYTES;	/* 512-byte sectors */
      memcpy(&partition->part_uuid, &sb->set_uuid0, 4);
      memcpy((char*)(&partition->part_uuid)+4, &sb->set_uuid1, 3*4);
    }
    else
    {
      const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)sb;
      partition->part_size=(uint64_t)be64(sb1->size) * 512 + 4096;	/* 512-byte sectors */
      memcpy(&partition->part_uuid, &sb1->set_uuid, 16);
    }
    return 0;
  }
  return 1;
}

static void set_MD_info(const struct mdp_superblock_s *sb, partition_t *partition, const int verbose)
{
  if(le32(sb->major_version)==0)
  {
    unsigned int i;
    partition->upart_type=UP_MD;
    sprintf(partition->fsname,"md%u",(unsigned int)le32(sb->md_minor));
    sprintf(partition->info,"md %u.%u.%u L.Endian Raid %u: devices",
        (unsigned int)le32(sb->major_version),
        (unsigned int)le32(sb->minor_version),
        (unsigned int)le32(sb->patch_version),
        (unsigned int)le32(sb->level));
    for(i=0;i<MD_SB_DISKS;i++)
    {
      if(le32(sb->disks[i].major)!=0 && le32(sb->disks[i].minor)!=0)
      {
        if(strlen(partition->info)<sizeof(partition->info)-26)
        {
          sprintf(&partition->info[strlen(partition->info)]," %u(%u,%u)",
              (unsigned int)le32(sb->disks[i].number),
              (unsigned int)le32(sb->disks[i].major),
	      (unsigned int)le32(sb->disks[i].minor));
          if(le32(sb->disks[i].major)==le32(sb->this_disk.major) &&
	    le32(sb->disks[i].minor)==le32(sb->this_disk.minor))
            sprintf(&partition->info[strlen(partition->info)],"*");
        }
      }
    }
  }
  else
  {
    const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)sb;
    partition->upart_type=UP_MD1;
    set_part_name(partition,sb1->set_name,32);
    sprintf(partition->info,"md %u.x L.Endian Raid %u - Array Slot : %lu",
	(unsigned int)le32(sb1->major_version),
	(unsigned int)le32(sb1->level),
	(long unsigned)le32(sb1->dev_number));
    if(le32(sb1->max_dev) <= 384)
    {
      unsigned int i,d;
      for (i= le32(sb1->max_dev); i> 0 ; i--)
	if (le16(sb1->dev_roles[i-1]) != 0xffff)
	  break;
      strcat(partition->info, " (");
      for (d=0; d < i && strlen(partition->info) < sizeof(partition->info) - 9; d++)
      {
	const int role = le16(sb1->dev_roles[d]);
	if (d)
	  strcat(partition->info, ", ");
	if (role == 0xffff)
	  strcat(partition->info, "empty");
	else if(role == 0xfffe)
	  strcat(partition->info, "failed");
	else
	  sprintf(&partition->info[strlen(partition->info)], "%d", role);
      }
      strcat(partition->info, ")");
    }
  }
  if(verbose>0)
    log_info("%s %s\n", partition->fsname, partition->info);
}

static void set_MD_info_be(const struct mdp_superblock_s *sb, partition_t *partition, const int verbose)
{
  if(be32(sb->major_version)==0)
  {
    unsigned int i;
    partition->upart_type=UP_MD;
    sprintf(partition->fsname,"md%u",(unsigned int)be32(sb->md_minor));
    sprintf(partition->info,"md %u.%u.%u B.Endian Raid %u: devices",
        (unsigned int)be32(sb->major_version),
        (unsigned int)be32(sb->minor_version),
        (unsigned int)be32(sb->patch_version),
        (unsigned int)be32(sb->level));
    for(i=0;i<MD_SB_DISKS;i++)
    {
      if(be32(sb->disks[i].major)!=0 && be32(sb->disks[i].minor)!=0)
      {
        if(strlen(partition->info)<sizeof(partition->info)-26)
        {
          sprintf(&partition->info[strlen(partition->info)]," %u(%u,%u)",
              (unsigned int)be32(sb->disks[i].number),
              (unsigned int)be32(sb->disks[i].major),
	      (unsigned int)be32(sb->disks[i].minor));
          if(be32(sb->disks[i].major)==be32(sb->this_disk.major) &&
	      be32(sb->disks[i].minor)==be32(sb->this_disk.minor))
            sprintf(&partition->info[strlen(partition->info)],"*");
        }
      }
    }
  }
  else
  {
    const struct mdp_superblock_1 *sb1=(const struct mdp_superblock_1 *)sb;
    partition->upart_type=UP_MD1;
    set_part_name(partition,sb1->set_name,32);
    sprintf(partition->info,"md %u.x B.Endian Raid %u - Array Slot : %lu",
	(unsigned int)be32(sb1->major_version),
	(unsigned int)be32(sb1->level),
	(long unsigned)be32(sb1->dev_number));
    if(be32(sb1->max_dev) <= 384)
    {
      unsigned int i,d;
      for (i= be32(sb1->max_dev); i> 0 ; i--)
	if (be16(sb1->dev_roles[i-1]) != 0xffff)
	  break;
      strcat(partition->info, " (");
      for (d=0; d < i && strlen(partition->info) < sizeof(partition->info) - 9; d++)
      {
	const int role = be16(sb1->dev_roles[d]);
	if (d)
	  strcat(partition->info, ", ");
	if (role == 0xffff)
	  strcat(partition->info, "empty");
	else if(role == 0xfffe)
	  strcat(partition->info, "failed");
	else
	  sprintf(&partition->info[strlen(partition->info)], "%d", role);
      }
      strcat(partition->info, ")");
    }
  }
  if(verbose>0)
    log_info("%s %s\n", partition->fsname, partition->info);
}

static int test_MD(disk_t *disk_car, const struct mdp_superblock_s *sb, const partition_t *partition, const int dump_ind)
{
  if(le32(sb->md_magic)!=(unsigned int)MD_SB_MAGIC)
    return 1;
  log_info("\nRaid magic value at %u/%u/%u\n",
      offset2cylinder(disk_car,partition->part_offset),
      offset2head(disk_car,partition->part_offset),
      offset2sector(disk_car,partition->part_offset));
  log_info("Raid apparent size: %llu sectors\n", (long long unsigned)(sb->size<<1));
  if(le32(sb->major_version)==0)
  {
    /* chunk_size may be 0 */
    log_info("Raid chunk size: %llu bytes\n", (long long unsigned)le32(sb->chunk_size));
  }
  if(le32(sb->major_version)>1)
    return 1;
  if(dump_ind!=0)
  {
    /* There is a little offset ... */
    dump_log(sb,DEFAULT_SECTOR_SIZE);
  }
  return 0;
}

static int test_MD_be(disk_t *disk_car, const struct mdp_superblock_s *sb, const partition_t *partition, const int dump_ind)
{
  if(be32(sb->md_magic)!=(unsigned int)MD_SB_MAGIC)
    return 1;
  log_info("\nRaid magic value at %u/%u/%u\n",
      offset2cylinder(disk_car,partition->part_offset),
      offset2head(disk_car,partition->part_offset),
      offset2sector(disk_car,partition->part_offset));
  log_info("Raid apparent size: %llu sectors\n", (long long unsigned)(sb->size<<1));
  if(be32(sb->major_version)==0)
  {
    /* chunk_size may be 0 */
    log_info("Raid chunk size: %llu bytes\n",(long long unsigned)be32(sb->chunk_size));
  }
  if(be32(sb->major_version)>1)
    return 1;
  if(dump_ind!=0)
  {
    /* There is a little offset ... */
    dump_log(sb,DEFAULT_SECTOR_SIZE);
  }
  return 0;
}