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 it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 of the License.
26 #include "volume_id.h"
30 struct msdos_partition_entry {
41 } __attribute__((packed));
43 #define MSDOS_MAGIC "\x55\xaa"
44 #define MSDOS_PARTTABLE_OFFSET 0x1be
45 #define MSDOS_SIG_OFF 0x1fe
47 #define DOS_EXTENDED_PARTITION 0x05
48 #define LINUX_EXTENDED_PARTITION 0x85
49 #define WIN98_EXTENDED_PARTITION 0x0f
50 #define LINUX_RAID_PARTITION 0xfd
51 #define is_extended(type) \
52 (type == DOS_EXTENDED_PARTITION || \
53 type == WIN98_EXTENDED_PARTITION || \
54 type == LINUX_EXTENDED_PARTITION)
55 #define is_raid(type) \
56 (type == LINUX_RAID_PARTITION)
58 int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
64 uint64_t extended = 0;
69 struct msdos_partition_entry *part;
70 struct volume_id_partition *p;
72 dbg("probing at offset 0x%llx", (unsigned long long) off);
74 buf = volume_id_get_buffer(id, off, 0x200);
78 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
81 /* check flags on all entries for a valid partition table */
82 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
83 for (i = 0; i < 4; i++) {
84 if (part[i].boot_ind != 0 &&
85 part[i].boot_ind != 0x80)
88 if (le32_to_cpu(part[i].nr_sects) != 0)
94 if (id->partitions != NULL)
96 id->partitions = malloc(VOLUME_ID_PARTITIONS_MAX *
97 sizeof(struct volume_id_partition));
98 if (id->partitions == NULL)
100 memset(id->partitions, 0x00,
101 VOLUME_ID_PARTITIONS_MAX * sizeof(struct volume_id_partition));
103 for (i = 0; i < 4; i++) {
104 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
105 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
110 p = &id->partitions[i];
112 p->partition_type_raw = part[i].sys_ind;
114 if (is_extended(part[i].sys_ind)) {
115 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
116 volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
117 p->type = "msdos_extended_partition";
119 extended = off + poff;
121 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
122 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
124 if (is_raid(part[i].sys_ind))
125 volume_id_set_usage_part(p, VOLUME_ID_RAID);
127 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
132 id->partition_count = i+1;
139 /* follow extended partition chain and add data partitions */
142 dbg("extended chain limit reached");
146 buf = volume_id_get_buffer(id, current, 0x200);
150 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
152 if (memcmp(&buf[MSDOS_SIG_OFF], MSDOS_MAGIC, 2) != 0)
157 for (i = 0; i < 4; i++) {
158 poff = (uint64_t) le32_to_cpu(part[i].start_sect) * BSIZE;
159 plen = (uint64_t) le32_to_cpu(part[i].nr_sects) * BSIZE;
164 if (is_extended(part[i].sys_ind)) {
165 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
167 next = extended + poff;
169 dbg("found 0x%x data partition at 0x%llx, len 0x%llx",
170 part[i].sys_ind, (unsigned long long) poff, (unsigned long long) plen);
172 /* we always start at the 5th entry */
173 while (id->partition_count < 4)
174 volume_id_set_usage_part(&id->partitions[id->partition_count++], VOLUME_ID_UNUSED);
176 p = &id->partitions[id->partition_count];
178 if (is_raid(part[i].sys_ind))
179 volume_id_set_usage_part(p, VOLUME_ID_RAID);
181 volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
183 p->off = current + poff;
185 id->partition_count++;
187 p->partition_type_raw = part[i].sys_ind;
189 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
190 dbg("too many partitions");
199 volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
200 id->type = "msdos_partition_table";