2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include "volume_id.h"
41 struct msdos_partition_entry {
52 } __attribute__((packed));
54 #define MSDOS_MAGIC "\x55\xaa"
55 #define MSDOS_PARTTABLE_OFFSET 0x1be
56 #define MSDOS_SIG_OFF 0x1fe
58 #define DOS_EXTENDED_PARTITION 0x05
59 #define LINUX_EXTENDED_PARTITION 0x85
60 #define WIN98_EXTENDED_PARTITION 0x0f
61 #define LINUX_RAID_PARTITION 0xfd
62 #define is_extended(type) \
63 (type == DOS_EXTENDED_PARTITION || \
64 type == WIN98_EXTENDED_PARTITION || \
65 type == LINUX_EXTENDED_PARTITION)
66 #define is_raid(type) \
67 (type == LINUX_RAID_PARTITION)
69 int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
75 uint64_t extended = 0;
80 struct msdos_partition_entry *part;
81 struct volume_id_partition *p;
83 dbg("probing at offset 0x%llx", (unsigned long long) off);
85 buf = volume_id_get_buffer(id, off, 0x200);
89 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
92 /* check flags on all entries for a valid partition table */
93 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
94 for (i = 0; i < 4; i++) {
95 if (part[i].boot_ind != 0 &&
96 part[i].boot_ind != 0x80)
99 if (le32_to_cpu(part[i].nr_sects) != 0)
105 if (id->partitions != NULL)
106 free(id->partitions);
107 id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
108 sizeof(struct volume_id_partition));
109 if (id->partitions == NULL)
111 memset(id->partitions, 0x00,
112 VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
114 for (i = 0; i < 4; i++) {
115 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
116 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
121 p = &id->partitions[i];
123 p->partition_type_raw = part[i].sys_ind;
125 if (is_extended(part[i].sys_ind)) {
126 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
127 volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
128 p->type = "msdos_extended_partition";
130 extended = off + poff;
132 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
133 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
135 if (is_raid(part[i].sys_ind))
136 volume_id_set_usage_part(p, VOLUME_ID_RAID);
138 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
143 id->partition_count = i+1;
150 /* follow extended partition chain and add data partitions */
153 dbg("extended chain limit reached");
157 buf = volume_id_get_buffer(id, current, 0x200);
161 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
163 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
168 for (i = 0; i < 4; i++) {
169 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
170 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
175 if (is_extended(part[i].sys_ind)) {
176 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
178 next = extended + poff;
180 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
181 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
183 /* we always start at the 5th entry */
184 while (id->partition_count < 4)
185 volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
187 p = &id->partitions[id->partition_count];
189 if (is_raid(part[i].sys_ind))
190 volume_id_set_usage_part(p, VOLUME_ID_RAID);
192 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
194 p->off = current + poff;
196 id->partition_count++;
198 p->partition_type_raw = part[i].sys_ind;
200 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
201 dbg("too many partitions");
210 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
211 id->type = "msdos_partition_table";