Debian Codegraph @

libblkid/src/partitions/dos.c

raw
1 /*
2 * MS-DOS partition parsing code
3 *
4 * Copyright (C) 2009 Karel Zak <[email protected]>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 *
9 * Inspired by fdisk, partx, Linux kernel and libparted.
10 */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdint.h>
15
16 #include "partitions.h"
17 #include "superblocks/superblocks.h"
18 #include "aix.h"
19
20 /* see superblocks/vfat.c */
21 extern int blkid_probe_is_vfat(blkid_probe pr);
22 /* see superblocks/exfat.c */
23 extern int blkid_probe_is_exfat(blkid_probe pr);
24
25 static const struct dos_subtypes {
26 unsigned char type;
27 const struct blkid_idinfo *id;
28 } dos_nested[] = {
29 { MBR_FREEBSD_PARTITION, &bsd_pt_idinfo },
30 { MBR_NETBSD_PARTITION, &bsd_pt_idinfo },
31 { MBR_OPENBSD_PARTITION, &bsd_pt_idinfo },
32 { MBR_UNIXWARE_PARTITION, &unixware_pt_idinfo },
33 { MBR_SOLARIS_X86_PARTITION, &solaris_x86_pt_idinfo },
34 { MBR_MINIX_PARTITION, &minix_pt_idinfo }
35 };
36
37 static inline int is_extended(const struct dos_partition *p)
38 {
39 return (p->sys_ind == MBR_DOS_EXTENDED_PARTITION ||
40 p->sys_ind == MBR_W95_EXTENDED_PARTITION ||
41 p->sys_ind == MBR_LINUX_EXTENDED_PARTITION);
42 }
43
44 static int parse_dos_extended(blkid_probe pr, blkid_parttable tab,
45 uint32_t ex_start, uint32_t ex_size, int ssf)
46 {
47 blkid_partlist ls = blkid_probe_get_partlist(pr);
48 uint32_t cur_start = ex_start, cur_size = ex_size;
49 uint64_t ex_end = (uint64_t) ex_start + ex_size;
50 const unsigned char *data;
51 int ct_nodata = 0; /* count ext.partitions without data partitions */
52 int i;
53
54 DBG(LOWPROBE, ul_debug("parse EBR [start=%d, size=%d]", ex_start/ssf, ex_size/ssf));
55 if (ex_start == 0) {
56 DBG(LOWPROBE, ul_debug("Bad offset in primary extended partition -- ignore"));
57 return 0;
58 }
59
60 while (1) {
61 const struct dos_partition *p, *p0;
62 uint64_t start = 0, size;
63
64 if (++ct_nodata > 100)
65 return BLKID_PROBE_OK;
66 data = blkid_probe_get_sector(pr, cur_start);
67 if (!data) {
68 if (errno)
69 return -errno;
70 goto leave; /* malformed partition? */
71 }
72
73 if (!mbr_is_valid_magic(data))
74 goto leave;
75
76 p0 = mbr_get_partition(data, 0);
77
78 /* Usually, the first entry is the real data partition,
79 * the 2nd entry is the next extended partition, or empty,
80 * and the 3rd and 4th entries are unused.
81 * However, DRDOS sometimes has the extended partition as
82 * the first entry (when the data partition is empty),
83 * and OS/2 seems to use all four entries.
84 * -- Linux kernel fs/partitions/dos.c
85 *
86 * See also http://en.wikipedia.org/wiki/Extended_boot_record
87 */
88
89 /* Parse data partition */
90 for (p = p0, i = 0; i < 4; i++, p++) {
91 uint32_t abs_start;
92 uint64_t abs;
93 blkid_partition par;
94
95 /* the start is relative to the parental ext.partition */
96 start = (uint64_t) dos_partition_get_start(p) * ssf;
97 size = (uint64_t) dos_partition_get_size(p) * ssf;
98
99 if (!size || is_extended(p))
100 continue;
101
102 abs = (uint64_t) cur_start + start;
103
104 /* data partition must be within the extended area */
105 if (abs < ex_start || abs + size > ex_end) {
106 DBG(LOWPROBE, ul_debug("#%d: EBR data partition outside "
107 "extended -- ignore", i + 1));
108 continue;
109 }
110 abs_start = (uint32_t) abs;
111
112 if (i >= 2) {
113 /* extra check to detect real data on
114 * 3rd and 4th entries */
115 if (start + size > cur_size)
116 continue;
117 }
118
119 /* Avoid recursive non-empty links, see ct_nodata counter */
120 if (blkid_partlist_get_partition_by_start(ls, abs_start)) {
121 DBG(LOWPROBE, ul_debug("#%d: EBR duplicate data partition [abs start=%u] -- ignore",
122 i + 1, abs_start));
123 continue;
124 }
125
126 par = blkid_partlist_add_partition(ls, tab, abs_start, size);
127 if (!par)
128 return -ENOMEM;
129
130 blkid_partition_set_type(par, p->sys_ind);
131 blkid_partition_set_flags(par, p->boot_ind);
132 blkid_partition_gen_uuid(par);
133 ct_nodata = 0;
134 }
135 /* The first nested ext.partition should be a link to the next
136 * logical partition. Everything other (recursive ext.partitions)
137 * is junk.
138 */
139 for (p = p0, i = 0; i < 4; i++, p++) {
140 start = dos_partition_get_start(p) * ssf;
141 size = dos_partition_get_size(p) * ssf;
142
143 if (size && is_extended(p)) {
144 if (start == 0)
145 DBG(LOWPROBE, ul_debug("#%d: EBR link offset is zero -- ignore", i + 1));
146 else
147 break;
148 }
149 }
150 if (i == 4)
151 goto leave;
152
153 {
154 uint64_t next = (uint64_t) ex_start + start;
155
156 if (next + size > ex_end) {
157 DBG(LOWPROBE, ul_debug("EBR link outside "
158 "extended area -- leave"));
159 goto leave;
160 }
161 if (next <= cur_start) {
162 DBG(LOWPROBE, ul_debug("EBR link does not "
163 "advance -- leave"));
164 goto leave;
165 }
166 cur_start = (uint32_t) next;
167 cur_size = size;
168 }
169 }
170 leave:
171 return BLKID_PROBE_OK;
172 }
173
174 static inline int is_lvm(blkid_probe pr)
175 {
176 struct blkid_prval *v = __blkid_probe_lookup_value(pr, "TYPE");
177
178 return (v && v->data && strcmp((char *) v->data, "LVM2_member") == 0);
179 }
180
181 static inline int is_empty_mbr(const unsigned char *mbr)
182 {
183 const struct dos_partition *p = mbr_get_partition(mbr, 0);
184 int i, nparts = 0;
185
186 for (i = 0; i < 4; i++) {
187 if (dos_partition_get_size(p) > 0)
188 nparts++;
189 p++;
190 }
191
192 return nparts == 0;
193 }
194
195 static int probe_dos_pt(blkid_probe pr,
196 const struct blkid_idmag *mag __attribute__((__unused__)))
197 {
198 int i;
199 int ssf;
200 blkid_parttable tab = NULL;
201 blkid_partlist ls;
202 const struct dos_partition *p0, *p;
203 const unsigned char *data;
204 uint64_t start, size;
205 uint32_t id;
206 char idstr[UUID_STR_LEN];
207
208
209 data = blkid_probe_get_sector(pr, 0);
210 if (!data) {
211 if (errno)
212 return -errno;
213 goto nothing;
214 }
215
216 /* ignore disks with AIX magic number -- for more details see aix.c */
217 if (memcmp(data, BLKID_AIX_MAGIC_STRING, BLKID_AIX_MAGIC_STRLEN) == 0)
218 goto nothing;
219
220 p0 = mbr_get_partition(data, 0);
221
222 /*
223 * Reject PT where boot indicator is not 0 or 0x80.
224 */
225 for (p = p0, i = 0; i < 4; i++, p++)
226 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
227 DBG(LOWPROBE, ul_debug("missing boot indicator -- ignore"));
228 goto nothing;
229 }
230
231 /*
232 * GPT uses valid MBR
233 */
234 for (p = p0, i = 0; i < 4; i++, p++) {
235 if (p->sys_ind == MBR_GPT_PARTITION) {
236 DBG(LOWPROBE, ul_debug("probably GPT -- ignore"));
237 goto nothing;
238 }
239 }
240
241 /*
242 * Now that the 55aa signature is present, this is probably
243 * either the boot sector of a FAT filesystem or a DOS-type
244 * partition table.
245 */
246 if (blkid_probe_is_vfat(pr) == 1 || blkid_probe_is_exfat(pr) == 1) {
247 DBG(LOWPROBE, ul_debug("probably FAT -- ignore"));
248 goto nothing;
249 }
250
251 /* Another false positive is NTFS */
252 if (blkid_probe_is_ntfs(pr) == 1) {
253 DBG(LOWPROBE, ul_debug("probably NTFS -- ignore"));
254 goto nothing;
255 }
256
257 /*
258 * Ugly exception, if the device contains a valid LVM physical volume
259 * and empty MBR (=no partition defined) then it's LVM and MBR should
260 * be ignored. Crazy people use it to boot from LVM devices.
261 */
262 if (is_lvm(pr) && is_empty_mbr(data)) {
263 DBG(LOWPROBE, ul_debug("empty MBR on LVM device -- ignore"));
264 goto nothing;
265 }
266
267 blkid_probe_use_wiper(pr, MBR_PT_OFFSET, 512 - MBR_PT_OFFSET);
268
269 id = mbr_get_id(data);
270 if (id)
271 snprintf(idstr, sizeof(idstr), "%08x", id);
272
273 /*
274 * Well, all checks pass, it's MS-DOS partition table
275 */
276 if (blkid_partitions_need_typeonly(pr)) {
277 /* Non-binary interface -- caller does not ask for details
278 * about partitions, just set generic variables only. */
279 if (id)
280 blkid_partitions_strcpy_ptuuid(pr, idstr);
281 return 0;
282 }
283
284 ls = blkid_probe_get_partlist(pr);
285 if (!ls)
286 goto nothing;
287
288 /* sector size factor (the start and size are in the real sectors, but
289 * we need to convert all sizes to 512 logical sectors
290 */
291 ssf = blkid_probe_get_sectorsize(pr) / 512;
292
293 /* allocate a new partition table */
294 tab = blkid_partlist_new_parttable(ls, "dos", MBR_PT_OFFSET);
295 if (!tab)
296 return -ENOMEM;
297
298 if (id)
299 blkid_parttable_set_id(tab, (unsigned char *) idstr);
300
301 /* Parse primary partitions */
302 for (p = p0, i = 0; i < 4; i++, p++) {
303 blkid_partition par;
304
305 start = (uint64_t) dos_partition_get_start(p) * ssf;
306 size = (uint64_t) dos_partition_get_size(p) * ssf;
307
308 if (!size) {
309 /* Linux kernel ignores empty partitions, but partno for
310 * the empty primary partitions is not reused */
311 blkid_partlist_increment_partno(ls);
312 continue;
313 }
314 par = blkid_partlist_add_partition(ls, tab, start, size);
315 if (!par)
316 return -ENOMEM;
317
318 blkid_partition_set_type(par, p->sys_ind);
319 blkid_partition_set_flags(par, p->boot_ind);
320 blkid_partition_gen_uuid(par);
321 }
322
323 /* Linux uses partition numbers greater than 4
324 * for all logical partition and all nested partition tables (bsd, ..)
325 */
326 blkid_partlist_set_partno(ls, 5);
327
328 /* Parse logical partitions */
329 for (p = p0, i = 0; i < 4; i++, p++) {
330 start = (uint64_t) dos_partition_get_start(p) * ssf;
331 size = (uint64_t) dos_partition_get_size(p) * ssf;
332
333 if (!size)
334 continue;
335 if (is_extended(p) &&
336 parse_dos_extended(pr, tab, start, size, ssf) == -1)
337 goto nothing;
338 }
339
340 /* Parse subtypes (nested partitions) on large disks */
341 if (!blkid_probe_is_tiny(pr)) {
342 int nparts = blkid_partlist_numof_partitions(ls);
343
344 DBG(LOWPROBE, ul_debug("checking for subtypes"));
345
346 for (i = 0; i < nparts; i++) {
347 size_t n;
348 int type;
349 blkid_partition pa = blkid_partlist_get_partition(ls, i);
350
351 if (pa == NULL
352 || blkid_partition_get_size(pa) == 0
353 || blkid_partition_is_extended(pa)
354 || blkid_partition_is_logical(pa))
355 continue;
356
357 type = blkid_partition_get_type(pa);
358
359 for (n = 0; n < ARRAY_SIZE(dos_nested); n++) {
360 int rc;
361
362 if (dos_nested[n].type != type)
363 continue;
364
365 rc = blkid_partitions_do_subprobe(pr, pa,
366 dos_nested[n].id);
367 if (rc < 0)
368 return rc;
369 break;
370 }
371 }
372 }
373 return BLKID_PROBE_OK;
374
375 nothing:
376 return BLKID_PROBE_NONE;
377 }
378
379
380 const struct blkid_idinfo dos_pt_idinfo =
381 {
382 .name = "dos",
383 .probefunc = probe_dos_pt,
384 .magics =
385 {
386 /* DOS master boot sector:
387 *
388 * 0 | Code Area
389 * 440 | Optional Disk signature
390 * 446 | Partition table
391 * 510 | 0x55
392 * 511 | 0xAA
393 */
394 { .magic = "\x55\xAA", .len = 2, .sboff = 510 },
395 { NULL }
396 }
397 };
398