2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "libvolume_id.h"
33 #include "libvolume_id-private.h"
35 struct mdp0_super_block {
37 uint32_t major_version;
38 uint32_t minor_version;
39 uint32_t patch_version;
40 uint32_t gvalid_words;
48 uint32_t not_persistent;
54 struct mdp1_super_block {
56 uint32_t major_version;
63 #define MD_RESERVED_BYTES 0x10000
64 #define MD_SB_MAGIC 0xa92b4efc
66 static int volume_id_probe_linux_raid0(struct volume_id *id, uint64_t off, uint64_t size)
69 struct mdp0_super_block *mdp0;
75 info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
79 buf = volume_id_get_buffer(id, off, 0x800);
82 mdp0 = (struct mdp0_super_block *) buf;
84 if (le32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
85 uuid.ints[0] = bswap_32(mdp0->set_uuid0);
86 if (le32_to_cpu(mdp0->minor_version >= 90)) {
87 uuid.ints[1] = bswap_32(mdp0->set_uuid1);
88 uuid.ints[2] = bswap_32(mdp0->set_uuid2);
89 uuid.ints[3] = bswap_32(mdp0->set_uuid3);
95 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
96 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
97 le32_to_cpu(mdp0->major_version),
98 le32_to_cpu(mdp0->minor_version),
99 le32_to_cpu(mdp0->patch_version));
100 } else if (be32_to_cpu(mdp0->md_magic) == MD_SB_MAGIC) {
101 uuid.ints[0] = mdp0->set_uuid0;
102 if (be32_to_cpu(mdp0->minor_version >= 90)) {
103 uuid.ints[1] = mdp0->set_uuid1;
104 uuid.ints[2] = mdp0->set_uuid2;
105 uuid.ints[3] = mdp0->set_uuid3;
111 volume_id_set_uuid(id, uuid.bytes, 0, UUID_MD);
112 snprintf(id->type_version, sizeof(id->type_version)-1, "%u.%u.%u",
113 be32_to_cpu(mdp0->major_version),
114 be32_to_cpu(mdp0->minor_version),
115 be32_to_cpu(mdp0->patch_version));
119 volume_id_set_usage(id, VOLUME_ID_RAID);
120 id->type = "linux_raid_member";
124 static int volume_id_probe_linux_raid1(struct volume_id *id, uint64_t off, uint64_t size)
127 struct mdp1_super_block *mdp1;
129 info("probing at offset 0x%" PRIx64 ", size 0x%" PRIx64 "\n", off, size);
131 buf = volume_id_get_buffer(id, off, 0x800);
134 mdp1 = (struct mdp1_super_block *) buf;
136 if (le32_to_cpu(mdp1->magic) != MD_SB_MAGIC)
139 if (le32_to_cpu(mdp1->major_version) != 1)
142 volume_id_set_uuid(id, mdp1->set_uuid, 0, UUID_MD);
143 volume_id_set_label_raw(id, mdp1->set_name, 32);
144 volume_id_set_label_string(id, mdp1->set_name, 32);
145 volume_id_set_usage(id, VOLUME_ID_RAID);
146 id->type = "linux_raid_member";
150 int volume_id_probe_linux_raid(struct volume_id *id, uint64_t off, uint64_t size)
154 if (size > MD_RESERVED_BYTES) {
155 /* version 0 at the end of the device */
156 sboff = (size & ~(MD_RESERVED_BYTES - 1)) - MD_RESERVED_BYTES;
157 if (volume_id_probe_linux_raid0(id, off + sboff, size) == 0)
160 /* version 1.0 at the end of the device */
161 sboff = (size & ~(0x1000 - 1)) - 0x2000;
162 if (volume_id_probe_linux_raid1(id, off + sboff, size) == 0) {
163 strcpy(id->type_version, "1.0");
168 /* version 1.1 at the start of the device */
169 if (volume_id_probe_linux_raid1(id, off, size) == 0) {
170 strcpy(id->type_version, "1.1");
174 /* version 1.2 at 4k offset from the start */
175 if (volume_id_probe_linux_raid1(id, off + 0x1000, size) == 0) {
176 strcpy(id->type_version, "1.2");