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
21 #include "volume_id_internal.h"
23 struct msdos_partition_entry {
34 } __attribute__((packed));
36 #define MSDOS_PARTTABLE_OFFSET 0x1be
37 #define MSDOS_SIG_OFF 0x1fe
39 #define DOS_EXTENDED_PARTITION 0x05
40 #define LINUX_EXTENDED_PARTITION 0x85
41 #define WIN98_EXTENDED_PARTITION 0x0f
42 #define LINUX_RAID_PARTITION 0xfd
43 #define is_extended(type) \
44 (type == DOS_EXTENDED_PARTITION || \
45 type == WIN98_EXTENDED_PARTITION || \
46 type == LINUX_EXTENDED_PARTITION)
47 #define is_raid(type) \
48 (type == LINUX_RAID_PARTITION)
50 int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
56 uint64_t extended = 0;
61 struct msdos_partition_entry *part;
62 struct volume_id_partition *p;
64 dbg("probing at offset 0x%llx", (unsigned long long) off);
66 buf = volume_id_get_buffer(id, off, 0x200);
70 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
73 /* check flags on all entries for a valid partition table */
74 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
75 for (i = 0; i < 4; i++) {
76 if (part[i].boot_ind != 0 &&
77 part[i].boot_ind != 0x80)
80 if (part[i].nr_sects != 0)
86 if (id->partitions != NULL)
88 id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX *
89 sizeof(struct volume_id_partition));
91 for (i = 0; i < 4; i++) {
92 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
93 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
98 p = &id->partitions[i];
100 // p->pt_type_raw = part[i].sys_ind;
102 if (is_extended(part[i].sys_ind)) {
103 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
104 // volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
105 // p->type = "msdos_extended_partition";
107 extended = off + poff;
109 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
110 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
112 // if (is_raid(part[i].sys_ind))
113 // volume_id_set_usage_part(p, VOLUME_ID_RAID);
115 // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
118 // p->pt_off = off + poff;
120 id->partition_count = i+1;
127 /* follow extended partition chain and add data partitions */
130 dbg("extended chain limit reached");
134 buf = volume_id_get_buffer(id, current, 0x200);
138 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
140 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
145 for (i = 0; i < 4; i++) {
146 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
147 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
152 if (is_extended(part[i].sys_ind)) {
153 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
155 next = extended + poff;
157 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
158 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
160 /* we always start at the 5th entry */
161 // while (id->partition_count < 4)
162 // volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
163 if (id->partition_count < 4)
164 id->partition_count = 4;
166 p = &id->partitions[id->partition_count];
168 // if (is_raid(part[i].sys_ind))
169 // volume_id_set_usage_part(p, VOLUME_ID_RAID);
171 // volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
173 // p->pt_off = current + poff;
175 id->partition_count++;
177 // p->pt_type_raw = part[i].sys_ind;
179 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
180 dbg("too many partitions");
189 // volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
190 // id->type = "msdos_partition_table";