Loading...
1/*
2 * fs/partitions/msdos.c
3 *
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
6 *
7 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
8 * in the early extended-partition checks and added DM partitions
9 *
10 * Support for DiskManager v6.0x added by Mark Lord,
11 * with information provided by OnTrack. This now works for linux fdisk
12 * and LILO, as well as loadlin and bootln. Note that disks other than
13 * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
14 *
15 * More flexible handling of extended partitions - aeb, 950831
16 *
17 * Check partition table on IDE disks for common CHS translations
18 *
19 * Re-organised Feb 1998 Russell King
20 */
21#include <linux/msdos_fs.h>
22
23#include "check.h"
24#include "msdos.h"
25#include "efi.h"
26#include "aix.h"
27
28/*
29 * Many architectures don't like unaligned accesses, while
30 * the nr_sects and start_sect partition table entries are
31 * at a 2 (mod 4) address.
32 */
33#include <asm/unaligned.h>
34
35#define SYS_IND(p) get_unaligned(&p->sys_ind)
36
37static inline sector_t nr_sects(struct partition *p)
38{
39 return (sector_t)get_unaligned_le32(&p->nr_sects);
40}
41
42static inline sector_t start_sect(struct partition *p)
43{
44 return (sector_t)get_unaligned_le32(&p->start_sect);
45}
46
47static inline int is_extended_partition(struct partition *p)
48{
49 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
50 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
51 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
52}
53
54#define MSDOS_LABEL_MAGIC1 0x55
55#define MSDOS_LABEL_MAGIC2 0xAA
56
57static inline int
58msdos_magic_present(unsigned char *p)
59{
60 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
61}
62
63/* Value is EBCDIC 'IBMA' */
64#define AIX_LABEL_MAGIC1 0xC9
65#define AIX_LABEL_MAGIC2 0xC2
66#define AIX_LABEL_MAGIC3 0xD4
67#define AIX_LABEL_MAGIC4 0xC1
68static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
69{
70 struct partition *pt = (struct partition *) (p + 0x1be);
71 Sector sect;
72 unsigned char *d;
73 int slot, ret = 0;
74
75 if (!(p[0] == AIX_LABEL_MAGIC1 &&
76 p[1] == AIX_LABEL_MAGIC2 &&
77 p[2] == AIX_LABEL_MAGIC3 &&
78 p[3] == AIX_LABEL_MAGIC4))
79 return 0;
80 /* Assume the partition table is valid if Linux partitions exists */
81 for (slot = 1; slot <= 4; slot++, pt++) {
82 if (pt->sys_ind == LINUX_SWAP_PARTITION ||
83 pt->sys_ind == LINUX_RAID_PARTITION ||
84 pt->sys_ind == LINUX_DATA_PARTITION ||
85 pt->sys_ind == LINUX_LVM_PARTITION ||
86 is_extended_partition(pt))
87 return 0;
88 }
89 d = read_part_sector(state, 7, §);
90 if (d) {
91 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
92 ret = 1;
93 put_dev_sector(sect);
94 }
95 return ret;
96}
97
98static void set_info(struct parsed_partitions *state, int slot,
99 u32 disksig)
100{
101 struct partition_meta_info *info = &state->parts[slot].info;
102
103 snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
104 slot);
105 info->volname[0] = 0;
106 state->parts[slot].has_info = true;
107}
108
109/*
110 * Create devices for each logical partition in an extended partition.
111 * The logical partitions form a linked list, with each entry being
112 * a partition table with two entries. The first entry
113 * is the real data partition (with a start relative to the partition
114 * table start). The second is a pointer to the next logical partition
115 * (with a start relative to the entire extended partition).
116 * We do not create a Linux partition for the partition tables, but
117 * only for the actual data partitions.
118 */
119
120static void parse_extended(struct parsed_partitions *state,
121 sector_t first_sector, sector_t first_size,
122 u32 disksig)
123{
124 struct partition *p;
125 Sector sect;
126 unsigned char *data;
127 sector_t this_sector, this_size;
128 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
129 int loopct = 0; /* number of links followed
130 without finding a data partition */
131 int i;
132
133 this_sector = first_sector;
134 this_size = first_size;
135
136 while (1) {
137 if (++loopct > 100)
138 return;
139 if (state->next == state->limit)
140 return;
141 data = read_part_sector(state, this_sector, §);
142 if (!data)
143 return;
144
145 if (!msdos_magic_present(data + 510))
146 goto done;
147
148 p = (struct partition *) (data + 0x1be);
149
150 /*
151 * Usually, the first entry is the real data partition,
152 * the 2nd entry is the next extended partition, or empty,
153 * and the 3rd and 4th entries are unused.
154 * However, DRDOS sometimes has the extended partition as
155 * the first entry (when the data partition is empty),
156 * and OS/2 seems to use all four entries.
157 */
158
159 /*
160 * First process the data partition(s)
161 */
162 for (i = 0; i < 4; i++, p++) {
163 sector_t offs, size, next;
164
165 if (!nr_sects(p) || is_extended_partition(p))
166 continue;
167
168 /* Check the 3rd and 4th entries -
169 these sometimes contain random garbage */
170 offs = start_sect(p)*sector_size;
171 size = nr_sects(p)*sector_size;
172 next = this_sector + offs;
173 if (i >= 2) {
174 if (offs + size > this_size)
175 continue;
176 if (next < first_sector)
177 continue;
178 if (next + size > first_sector + first_size)
179 continue;
180 }
181
182 put_partition(state, state->next, next, size);
183 set_info(state, state->next, disksig);
184 if (SYS_IND(p) == LINUX_RAID_PARTITION)
185 state->parts[state->next].flags = ADDPART_FLAG_RAID;
186 loopct = 0;
187 if (++state->next == state->limit)
188 goto done;
189 }
190 /*
191 * Next, process the (first) extended partition, if present.
192 * (So far, there seems to be no reason to make
193 * parse_extended() recursive and allow a tree
194 * of extended partitions.)
195 * It should be a link to the next logical partition.
196 */
197 p -= 4;
198 for (i = 0; i < 4; i++, p++)
199 if (nr_sects(p) && is_extended_partition(p))
200 break;
201 if (i == 4)
202 goto done; /* nothing left to do */
203
204 this_sector = first_sector + start_sect(p) * sector_size;
205 this_size = nr_sects(p) * sector_size;
206 put_dev_sector(sect);
207 }
208done:
209 put_dev_sector(sect);
210}
211
212/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
213 indicates linux swap. Be careful before believing this is Solaris. */
214
215static void parse_solaris_x86(struct parsed_partitions *state,
216 sector_t offset, sector_t size, int origin)
217{
218#ifdef CONFIG_SOLARIS_X86_PARTITION
219 Sector sect;
220 struct solaris_x86_vtoc *v;
221 int i;
222 short max_nparts;
223
224 v = read_part_sector(state, offset + 1, §);
225 if (!v)
226 return;
227 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
228 put_dev_sector(sect);
229 return;
230 }
231 {
232 char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
233
234 snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
235 strlcat(state->pp_buf, tmp, PAGE_SIZE);
236 }
237 if (le32_to_cpu(v->v_version) != 1) {
238 char tmp[64];
239
240 snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
241 le32_to_cpu(v->v_version));
242 strlcat(state->pp_buf, tmp, PAGE_SIZE);
243 put_dev_sector(sect);
244 return;
245 }
246 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
247 max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
248 for (i = 0; i < max_nparts && state->next < state->limit; i++) {
249 struct solaris_x86_slice *s = &v->v_slice[i];
250 char tmp[3 + 10 + 1 + 1];
251
252 if (s->s_size == 0)
253 continue;
254 snprintf(tmp, sizeof(tmp), " [s%d]", i);
255 strlcat(state->pp_buf, tmp, PAGE_SIZE);
256 /* solaris partitions are relative to current MS-DOS
257 * one; must add the offset of the current partition */
258 put_partition(state, state->next++,
259 le32_to_cpu(s->s_start)+offset,
260 le32_to_cpu(s->s_size));
261 }
262 put_dev_sector(sect);
263 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
264#endif
265}
266
267#if defined(CONFIG_BSD_DISKLABEL)
268/*
269 * Create devices for BSD partitions listed in a disklabel, under a
270 * dos-like partition. See parse_extended() for more information.
271 */
272static void parse_bsd(struct parsed_partitions *state,
273 sector_t offset, sector_t size, int origin, char *flavour,
274 int max_partitions)
275{
276 Sector sect;
277 struct bsd_disklabel *l;
278 struct bsd_partition *p;
279 char tmp[64];
280
281 l = read_part_sector(state, offset + 1, §);
282 if (!l)
283 return;
284 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
285 put_dev_sector(sect);
286 return;
287 }
288
289 snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
290 strlcat(state->pp_buf, tmp, PAGE_SIZE);
291
292 if (le16_to_cpu(l->d_npartitions) < max_partitions)
293 max_partitions = le16_to_cpu(l->d_npartitions);
294 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
295 sector_t bsd_start, bsd_size;
296
297 if (state->next == state->limit)
298 break;
299 if (p->p_fstype == BSD_FS_UNUSED)
300 continue;
301 bsd_start = le32_to_cpu(p->p_offset);
302 bsd_size = le32_to_cpu(p->p_size);
303 if (offset == bsd_start && size == bsd_size)
304 /* full parent partition, we have it already */
305 continue;
306 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
307 strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
308 continue;
309 }
310 put_partition(state, state->next++, bsd_start, bsd_size);
311 }
312 put_dev_sector(sect);
313 if (le16_to_cpu(l->d_npartitions) > max_partitions) {
314 snprintf(tmp, sizeof(tmp), " (ignored %d more)",
315 le16_to_cpu(l->d_npartitions) - max_partitions);
316 strlcat(state->pp_buf, tmp, PAGE_SIZE);
317 }
318 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
319}
320#endif
321
322static void parse_freebsd(struct parsed_partitions *state,
323 sector_t offset, sector_t size, int origin)
324{
325#ifdef CONFIG_BSD_DISKLABEL
326 parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
327#endif
328}
329
330static void parse_netbsd(struct parsed_partitions *state,
331 sector_t offset, sector_t size, int origin)
332{
333#ifdef CONFIG_BSD_DISKLABEL
334 parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
335#endif
336}
337
338static void parse_openbsd(struct parsed_partitions *state,
339 sector_t offset, sector_t size, int origin)
340{
341#ifdef CONFIG_BSD_DISKLABEL
342 parse_bsd(state, offset, size, origin, "openbsd",
343 OPENBSD_MAXPARTITIONS);
344#endif
345}
346
347/*
348 * Create devices for Unixware partitions listed in a disklabel, under a
349 * dos-like partition. See parse_extended() for more information.
350 */
351static void parse_unixware(struct parsed_partitions *state,
352 sector_t offset, sector_t size, int origin)
353{
354#ifdef CONFIG_UNIXWARE_DISKLABEL
355 Sector sect;
356 struct unixware_disklabel *l;
357 struct unixware_slice *p;
358
359 l = read_part_sector(state, offset + 29, §);
360 if (!l)
361 return;
362 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
363 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
364 put_dev_sector(sect);
365 return;
366 }
367 {
368 char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
369
370 snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
371 strlcat(state->pp_buf, tmp, PAGE_SIZE);
372 }
373 p = &l->vtoc.v_slice[1];
374 /* I omit the 0th slice as it is the same as whole disk. */
375 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
376 if (state->next == state->limit)
377 break;
378
379 if (p->s_label != UNIXWARE_FS_UNUSED)
380 put_partition(state, state->next++,
381 le32_to_cpu(p->start_sect),
382 le32_to_cpu(p->nr_sects));
383 p++;
384 }
385 put_dev_sector(sect);
386 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
387#endif
388}
389
390/*
391 * Minix 2.0.0/2.0.2 subpartition support.
392 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
393 * Rajeev V. Pillai <rajeevvp@yahoo.com>
394 */
395static void parse_minix(struct parsed_partitions *state,
396 sector_t offset, sector_t size, int origin)
397{
398#ifdef CONFIG_MINIX_SUBPARTITION
399 Sector sect;
400 unsigned char *data;
401 struct partition *p;
402 int i;
403
404 data = read_part_sector(state, offset, §);
405 if (!data)
406 return;
407
408 p = (struct partition *)(data + 0x1be);
409
410 /* The first sector of a Minix partition can have either
411 * a secondary MBR describing its subpartitions, or
412 * the normal boot sector. */
413 if (msdos_magic_present(data + 510) &&
414 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
415 char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
416
417 snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
418 strlcat(state->pp_buf, tmp, PAGE_SIZE);
419 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
420 if (state->next == state->limit)
421 break;
422 /* add each partition in use */
423 if (SYS_IND(p) == MINIX_PARTITION)
424 put_partition(state, state->next++,
425 start_sect(p), nr_sects(p));
426 }
427 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
428 }
429 put_dev_sector(sect);
430#endif /* CONFIG_MINIX_SUBPARTITION */
431}
432
433static struct {
434 unsigned char id;
435 void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
436} subtypes[] = {
437 {FREEBSD_PARTITION, parse_freebsd},
438 {NETBSD_PARTITION, parse_netbsd},
439 {OPENBSD_PARTITION, parse_openbsd},
440 {MINIX_PARTITION, parse_minix},
441 {UNIXWARE_PARTITION, parse_unixware},
442 {SOLARIS_X86_PARTITION, parse_solaris_x86},
443 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
444 {0, NULL},
445};
446
447int msdos_partition(struct parsed_partitions *state)
448{
449 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
450 Sector sect;
451 unsigned char *data;
452 struct partition *p;
453 struct fat_boot_sector *fb;
454 int slot;
455 u32 disksig;
456
457 data = read_part_sector(state, 0, §);
458 if (!data)
459 return -1;
460
461 /*
462 * Note order! (some AIX disks, e.g. unbootable kind,
463 * have no MSDOS 55aa)
464 */
465 if (aix_magic_present(state, data)) {
466 put_dev_sector(sect);
467#ifdef CONFIG_AIX_PARTITION
468 return aix_partition(state);
469#else
470 strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
471 return 0;
472#endif
473 }
474
475 if (!msdos_magic_present(data + 510)) {
476 put_dev_sector(sect);
477 return 0;
478 }
479
480 /*
481 * Now that the 55aa signature is present, this is probably
482 * either the boot sector of a FAT filesystem or a DOS-type
483 * partition table. Reject this in case the boot indicator
484 * is not 0 or 0x80.
485 */
486 p = (struct partition *) (data + 0x1be);
487 for (slot = 1; slot <= 4; slot++, p++) {
488 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
489 /*
490 * Even without a valid boot inidicator value
491 * its still possible this is valid FAT filesystem
492 * without a partition table.
493 */
494 fb = (struct fat_boot_sector *) data;
495 if (slot == 1 && fb->reserved && fb->fats
496 && fat_valid_media(fb->media)) {
497 strlcat(state->pp_buf, "\n", PAGE_SIZE);
498 put_dev_sector(sect);
499 return 1;
500 } else {
501 put_dev_sector(sect);
502 return 0;
503 }
504 }
505 }
506
507#ifdef CONFIG_EFI_PARTITION
508 p = (struct partition *) (data + 0x1be);
509 for (slot = 1 ; slot <= 4 ; slot++, p++) {
510 /* If this is an EFI GPT disk, msdos should ignore it. */
511 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
512 put_dev_sector(sect);
513 return 0;
514 }
515 }
516#endif
517 p = (struct partition *) (data + 0x1be);
518
519 disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
520
521 /*
522 * Look for partitions in two passes:
523 * First find the primary and DOS-type extended partitions.
524 * On the second pass look inside *BSD, Unixware and Solaris partitions.
525 */
526
527 state->next = 5;
528 for (slot = 1 ; slot <= 4 ; slot++, p++) {
529 sector_t start = start_sect(p)*sector_size;
530 sector_t size = nr_sects(p)*sector_size;
531
532 if (!size)
533 continue;
534 if (is_extended_partition(p)) {
535 /*
536 * prevent someone doing mkfs or mkswap on an
537 * extended partition, but leave room for LILO
538 * FIXME: this uses one logical sector for > 512b
539 * sector, although it may not be enough/proper.
540 */
541 sector_t n = 2;
542
543 n = min(size, max(sector_size, n));
544 put_partition(state, slot, start, n);
545
546 strlcat(state->pp_buf, " <", PAGE_SIZE);
547 parse_extended(state, start, size, disksig);
548 strlcat(state->pp_buf, " >", PAGE_SIZE);
549 continue;
550 }
551 put_partition(state, slot, start, size);
552 set_info(state, slot, disksig);
553 if (SYS_IND(p) == LINUX_RAID_PARTITION)
554 state->parts[slot].flags = ADDPART_FLAG_RAID;
555 if (SYS_IND(p) == DM6_PARTITION)
556 strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
557 if (SYS_IND(p) == EZD_PARTITION)
558 strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
559 }
560
561 strlcat(state->pp_buf, "\n", PAGE_SIZE);
562
563 /* second pass - output for each on a separate line */
564 p = (struct partition *) (0x1be + data);
565 for (slot = 1 ; slot <= 4 ; slot++, p++) {
566 unsigned char id = SYS_IND(p);
567 int n;
568
569 if (!nr_sects(p))
570 continue;
571
572 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
573 ;
574
575 if (!subtypes[n].parse)
576 continue;
577 subtypes[n].parse(state, start_sect(p) * sector_size,
578 nr_sects(p) * sector_size, slot);
579 }
580 put_dev_sector(sect);
581 return 1;
582}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * fs/partitions/msdos.c
4 *
5 * Code extracted from drivers/block/genhd.c
6 * Copyright (C) 1991-1998 Linus Torvalds
7 *
8 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
9 * in the early extended-partition checks and added DM partitions
10 *
11 * Support for DiskManager v6.0x added by Mark Lord,
12 * with information provided by OnTrack. This now works for linux fdisk
13 * and LILO, as well as loadlin and bootln. Note that disks other than
14 * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
15 *
16 * More flexible handling of extended partitions - aeb, 950831
17 *
18 * Check partition table on IDE disks for common CHS translations
19 *
20 * Re-organised Feb 1998 Russell King
21 *
22 * BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
23 * updated by Marc Espie <Marc.Espie@openbsd.org>
24 *
25 * Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
26 * and Krzysztof G. Baranowski <kgb@knm.org.pl>
27 */
28#include <linux/msdos_fs.h>
29#include <linux/msdos_partition.h>
30
31#include "check.h"
32#include "efi.h"
33
34/*
35 * Many architectures don't like unaligned accesses, while
36 * the nr_sects and start_sect partition table entries are
37 * at a 2 (mod 4) address.
38 */
39#include <asm/unaligned.h>
40
41#define SYS_IND(p) get_unaligned(&p->sys_ind)
42
43static inline sector_t nr_sects(struct msdos_partition *p)
44{
45 return (sector_t)get_unaligned_le32(&p->nr_sects);
46}
47
48static inline sector_t start_sect(struct msdos_partition *p)
49{
50 return (sector_t)get_unaligned_le32(&p->start_sect);
51}
52
53static inline int is_extended_partition(struct msdos_partition *p)
54{
55 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
56 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
57 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
58}
59
60#define MSDOS_LABEL_MAGIC1 0x55
61#define MSDOS_LABEL_MAGIC2 0xAA
62
63static inline int
64msdos_magic_present(unsigned char *p)
65{
66 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
67}
68
69/* Value is EBCDIC 'IBMA' */
70#define AIX_LABEL_MAGIC1 0xC9
71#define AIX_LABEL_MAGIC2 0xC2
72#define AIX_LABEL_MAGIC3 0xD4
73#define AIX_LABEL_MAGIC4 0xC1
74static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
75{
76 struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
77 Sector sect;
78 unsigned char *d;
79 int slot, ret = 0;
80
81 if (!(p[0] == AIX_LABEL_MAGIC1 &&
82 p[1] == AIX_LABEL_MAGIC2 &&
83 p[2] == AIX_LABEL_MAGIC3 &&
84 p[3] == AIX_LABEL_MAGIC4))
85 return 0;
86
87 /*
88 * Assume the partition table is valid if Linux partitions exists.
89 * Note that old Solaris/x86 partitions use the same indicator as
90 * Linux swap partitions, so we consider that a Linux partition as
91 * well.
92 */
93 for (slot = 1; slot <= 4; slot++, pt++) {
94 if (pt->sys_ind == SOLARIS_X86_PARTITION ||
95 pt->sys_ind == LINUX_RAID_PARTITION ||
96 pt->sys_ind == LINUX_DATA_PARTITION ||
97 pt->sys_ind == LINUX_LVM_PARTITION ||
98 is_extended_partition(pt))
99 return 0;
100 }
101 d = read_part_sector(state, 7, §);
102 if (d) {
103 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
104 ret = 1;
105 put_dev_sector(sect);
106 }
107 return ret;
108}
109
110static void set_info(struct parsed_partitions *state, int slot,
111 u32 disksig)
112{
113 struct partition_meta_info *info = &state->parts[slot].info;
114
115 snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
116 slot);
117 info->volname[0] = 0;
118 state->parts[slot].has_info = true;
119}
120
121/*
122 * Create devices for each logical partition in an extended partition.
123 * The logical partitions form a linked list, with each entry being
124 * a partition table with two entries. The first entry
125 * is the real data partition (with a start relative to the partition
126 * table start). The second is a pointer to the next logical partition
127 * (with a start relative to the entire extended partition).
128 * We do not create a Linux partition for the partition tables, but
129 * only for the actual data partitions.
130 */
131
132static void parse_extended(struct parsed_partitions *state,
133 sector_t first_sector, sector_t first_size,
134 u32 disksig)
135{
136 struct msdos_partition *p;
137 Sector sect;
138 unsigned char *data;
139 sector_t this_sector, this_size;
140 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
141 int loopct = 0; /* number of links followed
142 without finding a data partition */
143 int i;
144
145 this_sector = first_sector;
146 this_size = first_size;
147
148 while (1) {
149 if (++loopct > 100)
150 return;
151 if (state->next == state->limit)
152 return;
153 data = read_part_sector(state, this_sector, §);
154 if (!data)
155 return;
156
157 if (!msdos_magic_present(data + 510))
158 goto done;
159
160 p = (struct msdos_partition *) (data + 0x1be);
161
162 /*
163 * Usually, the first entry is the real data partition,
164 * the 2nd entry is the next extended partition, or empty,
165 * and the 3rd and 4th entries are unused.
166 * However, DRDOS sometimes has the extended partition as
167 * the first entry (when the data partition is empty),
168 * and OS/2 seems to use all four entries.
169 */
170
171 /*
172 * First process the data partition(s)
173 */
174 for (i = 0; i < 4; i++, p++) {
175 sector_t offs, size, next;
176
177 if (!nr_sects(p) || is_extended_partition(p))
178 continue;
179
180 /* Check the 3rd and 4th entries -
181 these sometimes contain random garbage */
182 offs = start_sect(p)*sector_size;
183 size = nr_sects(p)*sector_size;
184 next = this_sector + offs;
185 if (i >= 2) {
186 if (offs + size > this_size)
187 continue;
188 if (next < first_sector)
189 continue;
190 if (next + size > first_sector + first_size)
191 continue;
192 }
193
194 put_partition(state, state->next, next, size);
195 set_info(state, state->next, disksig);
196 if (SYS_IND(p) == LINUX_RAID_PARTITION)
197 state->parts[state->next].flags = ADDPART_FLAG_RAID;
198 loopct = 0;
199 if (++state->next == state->limit)
200 goto done;
201 }
202 /*
203 * Next, process the (first) extended partition, if present.
204 * (So far, there seems to be no reason to make
205 * parse_extended() recursive and allow a tree
206 * of extended partitions.)
207 * It should be a link to the next logical partition.
208 */
209 p -= 4;
210 for (i = 0; i < 4; i++, p++)
211 if (nr_sects(p) && is_extended_partition(p))
212 break;
213 if (i == 4)
214 goto done; /* nothing left to do */
215
216 this_sector = first_sector + start_sect(p) * sector_size;
217 this_size = nr_sects(p) * sector_size;
218 put_dev_sector(sect);
219 }
220done:
221 put_dev_sector(sect);
222}
223
224#define SOLARIS_X86_NUMSLICE 16
225#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
226
227struct solaris_x86_slice {
228 __le16 s_tag; /* ID tag of partition */
229 __le16 s_flag; /* permission flags */
230 __le32 s_start; /* start sector no of partition */
231 __le32 s_size; /* # of blocks in partition */
232};
233
234struct solaris_x86_vtoc {
235 unsigned int v_bootinfo[3]; /* info needed by mboot */
236 __le32 v_sanity; /* to verify vtoc sanity */
237 __le32 v_version; /* layout version */
238 char v_volume[8]; /* volume name */
239 __le16 v_sectorsz; /* sector size in bytes */
240 __le16 v_nparts; /* number of partitions */
241 unsigned int v_reserved[10]; /* free space */
242 struct solaris_x86_slice
243 v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
244 unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */
245 char v_asciilabel[128]; /* for compatibility */
246};
247
248/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
249 indicates linux swap. Be careful before believing this is Solaris. */
250
251static void parse_solaris_x86(struct parsed_partitions *state,
252 sector_t offset, sector_t size, int origin)
253{
254#ifdef CONFIG_SOLARIS_X86_PARTITION
255 Sector sect;
256 struct solaris_x86_vtoc *v;
257 int i;
258 short max_nparts;
259
260 v = read_part_sector(state, offset + 1, §);
261 if (!v)
262 return;
263 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
264 put_dev_sector(sect);
265 return;
266 }
267 {
268 char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
269
270 snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
271 strlcat(state->pp_buf, tmp, PAGE_SIZE);
272 }
273 if (le32_to_cpu(v->v_version) != 1) {
274 char tmp[64];
275
276 snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
277 le32_to_cpu(v->v_version));
278 strlcat(state->pp_buf, tmp, PAGE_SIZE);
279 put_dev_sector(sect);
280 return;
281 }
282 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
283 max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
284 for (i = 0; i < max_nparts && state->next < state->limit; i++) {
285 struct solaris_x86_slice *s = &v->v_slice[i];
286 char tmp[3 + 10 + 1 + 1];
287
288 if (s->s_size == 0)
289 continue;
290 snprintf(tmp, sizeof(tmp), " [s%d]", i);
291 strlcat(state->pp_buf, tmp, PAGE_SIZE);
292 /* solaris partitions are relative to current MS-DOS
293 * one; must add the offset of the current partition */
294 put_partition(state, state->next++,
295 le32_to_cpu(s->s_start)+offset,
296 le32_to_cpu(s->s_size));
297 }
298 put_dev_sector(sect);
299 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
300#endif
301}
302
303/* check against BSD src/sys/sys/disklabel.h for consistency */
304#define BSD_DISKMAGIC (0x82564557UL) /* The disk magic number */
305#define BSD_MAXPARTITIONS 16
306#define OPENBSD_MAXPARTITIONS 16
307#define BSD_FS_UNUSED 0 /* disklabel unused partition entry ID */
308struct bsd_disklabel {
309 __le32 d_magic; /* the magic number */
310 __s16 d_type; /* drive type */
311 __s16 d_subtype; /* controller/d_type specific */
312 char d_typename[16]; /* type name, e.g. "eagle" */
313 char d_packname[16]; /* pack identifier */
314 __u32 d_secsize; /* # of bytes per sector */
315 __u32 d_nsectors; /* # of data sectors per track */
316 __u32 d_ntracks; /* # of tracks per cylinder */
317 __u32 d_ncylinders; /* # of data cylinders per unit */
318 __u32 d_secpercyl; /* # of data sectors per cylinder */
319 __u32 d_secperunit; /* # of data sectors per unit */
320 __u16 d_sparespertrack; /* # of spare sectors per track */
321 __u16 d_sparespercyl; /* # of spare sectors per cylinder */
322 __u32 d_acylinders; /* # of alt. cylinders per unit */
323 __u16 d_rpm; /* rotational speed */
324 __u16 d_interleave; /* hardware sector interleave */
325 __u16 d_trackskew; /* sector 0 skew, per track */
326 __u16 d_cylskew; /* sector 0 skew, per cylinder */
327 __u32 d_headswitch; /* head switch time, usec */
328 __u32 d_trkseek; /* track-to-track seek, usec */
329 __u32 d_flags; /* generic flags */
330#define NDDATA 5
331 __u32 d_drivedata[NDDATA]; /* drive-type specific information */
332#define NSPARE 5
333 __u32 d_spare[NSPARE]; /* reserved for future use */
334 __le32 d_magic2; /* the magic number (again) */
335 __le16 d_checksum; /* xor of data incl. partitions */
336
337 /* filesystem and partition information: */
338 __le16 d_npartitions; /* number of partitions in following */
339 __le32 d_bbsize; /* size of boot area at sn0, bytes */
340 __le32 d_sbsize; /* max size of fs superblock, bytes */
341 struct bsd_partition { /* the partition table */
342 __le32 p_size; /* number of sectors in partition */
343 __le32 p_offset; /* starting sector */
344 __le32 p_fsize; /* filesystem basic fragment size */
345 __u8 p_fstype; /* filesystem type, see below */
346 __u8 p_frag; /* filesystem fragments per block */
347 __le16 p_cpg; /* filesystem cylinders per group */
348 } d_partitions[BSD_MAXPARTITIONS]; /* actually may be more */
349};
350
351#if defined(CONFIG_BSD_DISKLABEL)
352/*
353 * Create devices for BSD partitions listed in a disklabel, under a
354 * dos-like partition. See parse_extended() for more information.
355 */
356static void parse_bsd(struct parsed_partitions *state,
357 sector_t offset, sector_t size, int origin, char *flavour,
358 int max_partitions)
359{
360 Sector sect;
361 struct bsd_disklabel *l;
362 struct bsd_partition *p;
363 char tmp[64];
364
365 l = read_part_sector(state, offset + 1, §);
366 if (!l)
367 return;
368 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
369 put_dev_sector(sect);
370 return;
371 }
372
373 snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
374 strlcat(state->pp_buf, tmp, PAGE_SIZE);
375
376 if (le16_to_cpu(l->d_npartitions) < max_partitions)
377 max_partitions = le16_to_cpu(l->d_npartitions);
378 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
379 sector_t bsd_start, bsd_size;
380
381 if (state->next == state->limit)
382 break;
383 if (p->p_fstype == BSD_FS_UNUSED)
384 continue;
385 bsd_start = le32_to_cpu(p->p_offset);
386 bsd_size = le32_to_cpu(p->p_size);
387 /* FreeBSD has relative offset if C partition offset is zero */
388 if (memcmp(flavour, "bsd\0", 4) == 0 &&
389 le32_to_cpu(l->d_partitions[2].p_offset) == 0)
390 bsd_start += offset;
391 if (offset == bsd_start && size == bsd_size)
392 /* full parent partition, we have it already */
393 continue;
394 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
395 strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
396 continue;
397 }
398 put_partition(state, state->next++, bsd_start, bsd_size);
399 }
400 put_dev_sector(sect);
401 if (le16_to_cpu(l->d_npartitions) > max_partitions) {
402 snprintf(tmp, sizeof(tmp), " (ignored %d more)",
403 le16_to_cpu(l->d_npartitions) - max_partitions);
404 strlcat(state->pp_buf, tmp, PAGE_SIZE);
405 }
406 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
407}
408#endif
409
410static void parse_freebsd(struct parsed_partitions *state,
411 sector_t offset, sector_t size, int origin)
412{
413#ifdef CONFIG_BSD_DISKLABEL
414 parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
415#endif
416}
417
418static void parse_netbsd(struct parsed_partitions *state,
419 sector_t offset, sector_t size, int origin)
420{
421#ifdef CONFIG_BSD_DISKLABEL
422 parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
423#endif
424}
425
426static void parse_openbsd(struct parsed_partitions *state,
427 sector_t offset, sector_t size, int origin)
428{
429#ifdef CONFIG_BSD_DISKLABEL
430 parse_bsd(state, offset, size, origin, "openbsd",
431 OPENBSD_MAXPARTITIONS);
432#endif
433}
434
435#define UNIXWARE_DISKMAGIC (0xCA5E600DUL) /* The disk magic number */
436#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL) /* The slice table magic nr */
437#define UNIXWARE_NUMSLICE 16
438#define UNIXWARE_FS_UNUSED 0 /* Unused slice entry ID */
439
440struct unixware_slice {
441 __le16 s_label; /* label */
442 __le16 s_flags; /* permission flags */
443 __le32 start_sect; /* starting sector */
444 __le32 nr_sects; /* number of sectors in slice */
445};
446
447struct unixware_disklabel {
448 __le32 d_type; /* drive type */
449 __le32 d_magic; /* the magic number */
450 __le32 d_version; /* version number */
451 char d_serial[12]; /* serial number of the device */
452 __le32 d_ncylinders; /* # of data cylinders per device */
453 __le32 d_ntracks; /* # of tracks per cylinder */
454 __le32 d_nsectors; /* # of data sectors per track */
455 __le32 d_secsize; /* # of bytes per sector */
456 __le32 d_part_start; /* # of first sector of this partition*/
457 __le32 d_unknown1[12]; /* ? */
458 __le32 d_alt_tbl; /* byte offset of alternate table */
459 __le32 d_alt_len; /* byte length of alternate table */
460 __le32 d_phys_cyl; /* # of physical cylinders per device */
461 __le32 d_phys_trk; /* # of physical tracks per cylinder */
462 __le32 d_phys_sec; /* # of physical sectors per track */
463 __le32 d_phys_bytes; /* # of physical bytes per sector */
464 __le32 d_unknown2; /* ? */
465 __le32 d_unknown3; /* ? */
466 __le32 d_pad[8]; /* pad */
467
468 struct unixware_vtoc {
469 __le32 v_magic; /* the magic number */
470 __le32 v_version; /* version number */
471 char v_name[8]; /* volume name */
472 __le16 v_nslices; /* # of slices */
473 __le16 v_unknown1; /* ? */
474 __le32 v_reserved[10]; /* reserved */
475 struct unixware_slice
476 v_slice[UNIXWARE_NUMSLICE]; /* slice headers */
477 } vtoc;
478}; /* 408 */
479
480/*
481 * Create devices for Unixware partitions listed in a disklabel, under a
482 * dos-like partition. See parse_extended() for more information.
483 */
484static void parse_unixware(struct parsed_partitions *state,
485 sector_t offset, sector_t size, int origin)
486{
487#ifdef CONFIG_UNIXWARE_DISKLABEL
488 Sector sect;
489 struct unixware_disklabel *l;
490 struct unixware_slice *p;
491
492 l = read_part_sector(state, offset + 29, §);
493 if (!l)
494 return;
495 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
496 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
497 put_dev_sector(sect);
498 return;
499 }
500 {
501 char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
502
503 snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
504 strlcat(state->pp_buf, tmp, PAGE_SIZE);
505 }
506 p = &l->vtoc.v_slice[1];
507 /* I omit the 0th slice as it is the same as whole disk. */
508 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
509 if (state->next == state->limit)
510 break;
511
512 if (p->s_label != UNIXWARE_FS_UNUSED)
513 put_partition(state, state->next++,
514 le32_to_cpu(p->start_sect),
515 le32_to_cpu(p->nr_sects));
516 p++;
517 }
518 put_dev_sector(sect);
519 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
520#endif
521}
522
523#define MINIX_NR_SUBPARTITIONS 4
524
525/*
526 * Minix 2.0.0/2.0.2 subpartition support.
527 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
528 * Rajeev V. Pillai <rajeevvp@yahoo.com>
529 */
530static void parse_minix(struct parsed_partitions *state,
531 sector_t offset, sector_t size, int origin)
532{
533#ifdef CONFIG_MINIX_SUBPARTITION
534 Sector sect;
535 unsigned char *data;
536 struct msdos_partition *p;
537 int i;
538
539 data = read_part_sector(state, offset, §);
540 if (!data)
541 return;
542
543 p = (struct msdos_partition *)(data + 0x1be);
544
545 /* The first sector of a Minix partition can have either
546 * a secondary MBR describing its subpartitions, or
547 * the normal boot sector. */
548 if (msdos_magic_present(data + 510) &&
549 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
550 char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
551
552 snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
553 strlcat(state->pp_buf, tmp, PAGE_SIZE);
554 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
555 if (state->next == state->limit)
556 break;
557 /* add each partition in use */
558 if (SYS_IND(p) == MINIX_PARTITION)
559 put_partition(state, state->next++,
560 start_sect(p), nr_sects(p));
561 }
562 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
563 }
564 put_dev_sector(sect);
565#endif /* CONFIG_MINIX_SUBPARTITION */
566}
567
568static struct {
569 unsigned char id;
570 void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
571} subtypes[] = {
572 {FREEBSD_PARTITION, parse_freebsd},
573 {NETBSD_PARTITION, parse_netbsd},
574 {OPENBSD_PARTITION, parse_openbsd},
575 {MINIX_PARTITION, parse_minix},
576 {UNIXWARE_PARTITION, parse_unixware},
577 {SOLARIS_X86_PARTITION, parse_solaris_x86},
578 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
579 {0, NULL},
580};
581
582int msdos_partition(struct parsed_partitions *state)
583{
584 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
585 Sector sect;
586 unsigned char *data;
587 struct msdos_partition *p;
588 struct fat_boot_sector *fb;
589 int slot;
590 u32 disksig;
591
592 data = read_part_sector(state, 0, §);
593 if (!data)
594 return -1;
595
596 /*
597 * Note order! (some AIX disks, e.g. unbootable kind,
598 * have no MSDOS 55aa)
599 */
600 if (aix_magic_present(state, data)) {
601 put_dev_sector(sect);
602#ifdef CONFIG_AIX_PARTITION
603 return aix_partition(state);
604#else
605 strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
606 return 0;
607#endif
608 }
609
610 if (!msdos_magic_present(data + 510)) {
611 put_dev_sector(sect);
612 return 0;
613 }
614
615 /*
616 * Now that the 55aa signature is present, this is probably
617 * either the boot sector of a FAT filesystem or a DOS-type
618 * partition table. Reject this in case the boot indicator
619 * is not 0 or 0x80.
620 */
621 p = (struct msdos_partition *) (data + 0x1be);
622 for (slot = 1; slot <= 4; slot++, p++) {
623 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
624 /*
625 * Even without a valid boot inidicator value
626 * its still possible this is valid FAT filesystem
627 * without a partition table.
628 */
629 fb = (struct fat_boot_sector *) data;
630 if (slot == 1 && fb->reserved && fb->fats
631 && fat_valid_media(fb->media)) {
632 strlcat(state->pp_buf, "\n", PAGE_SIZE);
633 put_dev_sector(sect);
634 return 1;
635 } else {
636 put_dev_sector(sect);
637 return 0;
638 }
639 }
640 }
641
642#ifdef CONFIG_EFI_PARTITION
643 p = (struct msdos_partition *) (data + 0x1be);
644 for (slot = 1 ; slot <= 4 ; slot++, p++) {
645 /* If this is an EFI GPT disk, msdos should ignore it. */
646 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
647 put_dev_sector(sect);
648 return 0;
649 }
650 }
651#endif
652 p = (struct msdos_partition *) (data + 0x1be);
653
654 disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
655
656 /*
657 * Look for partitions in two passes:
658 * First find the primary and DOS-type extended partitions.
659 * On the second pass look inside *BSD, Unixware and Solaris partitions.
660 */
661
662 state->next = 5;
663 for (slot = 1 ; slot <= 4 ; slot++, p++) {
664 sector_t start = start_sect(p)*sector_size;
665 sector_t size = nr_sects(p)*sector_size;
666
667 if (!size)
668 continue;
669 if (is_extended_partition(p)) {
670 /*
671 * prevent someone doing mkfs or mkswap on an
672 * extended partition, but leave room for LILO
673 * FIXME: this uses one logical sector for > 512b
674 * sector, although it may not be enough/proper.
675 */
676 sector_t n = 2;
677
678 n = min(size, max(sector_size, n));
679 put_partition(state, slot, start, n);
680
681 strlcat(state->pp_buf, " <", PAGE_SIZE);
682 parse_extended(state, start, size, disksig);
683 strlcat(state->pp_buf, " >", PAGE_SIZE);
684 continue;
685 }
686 put_partition(state, slot, start, size);
687 set_info(state, slot, disksig);
688 if (SYS_IND(p) == LINUX_RAID_PARTITION)
689 state->parts[slot].flags = ADDPART_FLAG_RAID;
690 if (SYS_IND(p) == DM6_PARTITION)
691 strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
692 if (SYS_IND(p) == EZD_PARTITION)
693 strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
694 }
695
696 strlcat(state->pp_buf, "\n", PAGE_SIZE);
697
698 /* second pass - output for each on a separate line */
699 p = (struct msdos_partition *) (0x1be + data);
700 for (slot = 1 ; slot <= 4 ; slot++, p++) {
701 unsigned char id = SYS_IND(p);
702 int n;
703
704 if (!nr_sects(p))
705 continue;
706
707 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
708 ;
709
710 if (!subtypes[n].parse)
711 continue;
712 subtypes[n].parse(state, start_sect(p) * sector_size,
713 nr_sects(p) * sector_size, slot);
714 }
715 put_dev_sector(sect);
716 return 1;
717}