blob: 465f22dc37ea22512380e9bc6fa5f5c3f392aeba [file] [log] [blame]
Denis Vlasenkode7684a2008-02-18 21:08:49 +00001/*
2 * volume_id - reads filesystem label and uuid
3 *
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "volume_id_internal.h"
22
23struct msdos_partition_entry {
24 uint8_t boot_ind;
25 uint8_t head;
26 uint8_t sector;
27 uint8_t cyl;
28 uint8_t sys_ind;
29 uint8_t end_head;
30 uint8_t end_sector;
31 uint8_t end_cyl;
32 uint32_t start_sect;
33 uint32_t nr_sects;
Denys Vlasenko8dc0e192009-09-16 00:58:11 +020034} PACKED;
Denis Vlasenkode7684a2008-02-18 21:08:49 +000035
Denis Vlasenkode7684a2008-02-18 21:08:49 +000036#define MSDOS_PARTTABLE_OFFSET 0x1be
37#define MSDOS_SIG_OFF 0x1fe
38#define BSIZE 0x200
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)
49
50int volume_id_probe_msdos_part_table(struct volume_id *id, uint64_t off)
51{
52 const uint8_t *buf;
53 int i;
54 uint64_t poff;
55 uint64_t plen;
56 uint64_t extended = 0;
57 uint64_t current;
58 uint64_t next;
59 int limit;
60 int empty = 1;
61 struct msdos_partition_entry *part;
62 struct volume_id_partition *p;
63
64 dbg("probing at offset 0x%llx", (unsigned long long) off);
65
66 buf = volume_id_get_buffer(id, off, 0x200);
67 if (buf == NULL)
68 return -1;
69
Denis Vlasenkod25c33f2008-03-17 09:25:05 +000070 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
Denis Vlasenkode7684a2008-02-18 21:08:49 +000071 return -1;
72
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)
78 return -1;
79
80 if (part[i].nr_sects != 0)
81 empty = 0;
82 }
83 if (empty == 1)
84 return -1;
85
86 if (id->partitions != NULL)
87 free(id->partitions);
Denis Vlasenkod25c33f2008-03-17 09:25:05 +000088 id->partitions = xzalloc(VOLUME_ID_PARTITIONS_MAX *
Denis Vlasenkode7684a2008-02-18 21:08:49 +000089 sizeof(struct volume_id_partition));
Denis Vlasenkode7684a2008-02-18 21:08:49 +000090
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;
94
95 if (plen == 0)
96 continue;
97
98 p = &id->partitions[i];
99
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000100// p->pt_type_raw = part[i].sys_ind;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000101
102 if (is_extended(part[i].sys_ind)) {
103 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000104// volume_id_set_usage_part(p, VOLUME_ID_PARTITIONTABLE);
105// p->type = "msdos_extended_partition";
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000106 if (extended == 0)
107 extended = off + poff;
108 } else {
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);
111
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000112// if (is_raid(part[i].sys_ind))
113// volume_id_set_usage_part(p, VOLUME_ID_RAID);
114// else
115// volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000116 }
117
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000118// p->pt_off = off + poff;
119// p->pt_len = plen;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000120 id->partition_count = i+1;
121 }
122
123 next = extended;
124 current = extended;
125 limit = 50;
126
127 /* follow extended partition chain and add data partitions */
128 while (next != 0) {
129 if (limit-- == 0) {
130 dbg("extended chain limit reached");
131 break;
132 }
133
134 buf = volume_id_get_buffer(id, current, 0x200);
135 if (buf == NULL)
136 break;
137
138 part = (struct msdos_partition_entry*) &buf[MSDOS_PARTTABLE_OFFSET];
139
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000140 if (buf[MSDOS_SIG_OFF] != 0x55 || buf[MSDOS_SIG_OFF + 1] != 0xaa)
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000141 break;
142
143 next = 0;
144
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;
148
149 if (plen == 0)
150 continue;
151
152 if (is_extended(part[i].sys_ind)) {
153 dbg("found extended partition at 0x%llx", (unsigned long long) poff);
154 if (next == 0)
155 next = extended + poff;
156 } else {
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);
159
160 /* we always start at the 5th entry */
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000161// 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;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000165
166 p = &id->partitions[id->partition_count];
167
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000168// if (is_raid(part[i].sys_ind))
169// volume_id_set_usage_part(p, VOLUME_ID_RAID);
170// else
171// volume_id_set_usage_part(p, VOLUME_ID_UNPROBED);
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000172
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000173// p->pt_off = current + poff;
174// p->pt_len = plen;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000175 id->partition_count++;
176
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000177// p->pt_type_raw = part[i].sys_ind;
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000178
179 if (id->partition_count >= VOLUME_ID_PARTITIONS_MAX) {
180 dbg("too many partitions");
181 next = 0;
182 }
183 }
184 }
185
186 current = next;
187 }
188
Denis Vlasenkod25c33f2008-03-17 09:25:05 +0000189// volume_id_set_usage(id, VOLUME_ID_PARTITIONTABLE);
190// id->type = "msdos_partition_table";
Denis Vlasenkode7684a2008-02-18 21:08:49 +0000191
192 return 0;
193}