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
35 #include <asm/types.h>
37 #include "../volume_id.h"
38 #include "../logging.h"
42 #define MSDOS_MAGIC "\x55\xaa"
43 #define MSDOS_PARTTABLE_OFFSET 0x1be
44 #define MSDOS_SIG_OFF 0x1fe
46 #define DOS_EXTENDED_PARTITION 0x05
47 #define LINUX_EXTENDED_PARTITION 0x85
48 #define WIN98_EXTENDED_PARTITION 0x0f
49 #define LINUX_RAID_PARTITION 0xfd
50 #define is_extended(type) \
51 (type == DOS_EXTENDED_PARTITION || \
52 type == WIN98_EXTENDED_PARTITION || \
53 type == LINUX_EXTENDED_PARTITION)
54 #define is_raid(type) \
55 (type == LINUX_RAID_PARTITION)
57 int volume_id_probe_msdos_part_table(struct volume_id *id, __u64 off)
59 struct msdos_partition_entry {
70 } __attribute__((packed)) *part;
81 struct volume_id_partition *p;
83 buf = volume_id_get_buffer(id, off, 0x200);
87 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
90 /* check flags on all entries for a valid partition table */
91 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
92 for (i = 0; i < 4; i++) {
93 if (part[i].boot_ind != 0 &&
94 part[i].boot_ind != 0x80)
97 if (le32_to_cpu(part[i].nr_sects) != 0)
103 if (id->partitions != NULL)
104 free(id->partitions);
105 id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
106 sizeof(struct volume_id_partition));
107 if (id->partitions == NULL)
109 memset(id->partitions, 0x00,
110 VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
112 for (i = 0; i < 4; i++) {
113 poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
114 plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
119 p = &id->partitions[i];
121 p->partition_type_raw = part[i].sys_ind;
123 if (is_extended(part[i].sys_ind)) {
124 dbg("found extended partition at 0x%llx", poff);
125 volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
126 p->type = "msdos_extended_partition";
128 extended = off + poff;
130 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
131 part[i].sys_ind, poff, plen);
133 if (is_raid(part[i].sys_ind))
134 volume_id_set_usage_part(p, VOLUME_ID_RAID);
136 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
141 id->partition_count = i+1;
148 /* follow extended partition chain and add data partitions */
151 dbg("extended chain limit reached");
155 buf = volume_id_get_buffer(id, current, 0x200);
159 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
161 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
166 for (i = 0; i < 4; i++) {
167 poff = (__u64) le32_to_cpu(part[i].start_sect) * BSIZE;
168 plen = (__u64) le32_to_cpu(part[i].nr_sects) * BSIZE;
173 if (is_extended(part[i].sys_ind)) {
174 dbg("found extended partition at 0x%llx", poff);
176 next = extended + poff;
178 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
179 part[i].sys_ind, poff, plen);
181 /* we always start at the 5th entry */
182 while (id->partition_count < 4)
183 volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
185 p = &id->partitions[id->partition_count];
187 if (is_raid(part[i].sys_ind))
188 volume_id_set_usage_part(p, VOLUME_ID_RAID);
190 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
192 p->off = current + poff;
194 id->partition_count++;
196 p->partition_type_raw = part[i].sys_ind;
198 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
199 dbg("too many partitions");
208 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
209 id->type = "msdos_partition_table";