chiark / gitweb /
update file headers
[elogind.git] / extras / volume_id / lib / via_raid.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * Based on information taken from dmraid:
7  * Copyright (C) 2004-2006 Heinz Mauelshagen, Red Hat GmbH
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE 1
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <ctype.h>
33
34 #include "libvolume_id.h"
35 #include "libvolume_id-private.h"
36
37 struct via_meta {
38         uint16_t        signature;
39         uint8_t         version_number;
40         struct via_array {
41                 uint16_t        disk_bit_mask;
42                 uint8_t         disk_array_ex;
43                 uint32_t        capacity_low;
44                 uint32_t        capacity_high;
45                 uint32_t        serial_checksum;
46         } PACKED array;
47         uint32_t        serial_checksum[8];
48         uint8_t         checksum;
49 } PACKED;
50
51 #define VIA_SIGNATURE           0xAA55
52
53 /* 8 bit checksum on first 50 bytes of metadata. */
54 static uint8_t meta_checksum(struct via_meta *via)
55 {
56         uint8_t i = 50, sum = 0;
57
58         while (i--)
59                 sum += ((uint8_t*) via)[i];
60
61         return sum == via->checksum;
62 }
63
64
65 int volume_id_probe_via_raid(struct volume_id *id, uint64_t off, uint64_t size)
66 {
67         const uint8_t *buf;
68         uint64_t meta_off;
69         struct via_meta *via;
70
71         dbg("probing at offset 0x%llx, size 0x%llx\n",
72             (unsigned long long) off, (unsigned long long) size);
73
74         if (size < 0x10000)
75                 return -1;
76
77         meta_off = ((size / 0x200)-1) * 0x200;
78
79         buf = volume_id_get_buffer(id, off + meta_off, 0x200);
80         if (buf == NULL)
81                 return -1;
82
83         via = (struct via_meta *) buf;
84         if (le16_to_cpu(via->signature) !=  VIA_SIGNATURE)
85                 return -1;
86
87         if (via->version_number > 1)
88                 return -1;
89
90         if (!meta_checksum(via))
91                 return -1;
92
93         volume_id_set_usage(id, VOLUME_ID_RAID);
94         snprintf(id->type_version, sizeof(id->type_version)-1, "%u", via->version_number);
95         id->type = "via_raid_member";
96
97         return 0;
98 }