1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
36
37
38
39#include <asm/unaligned.h>
40
41static inline sector_t nr_sects(struct msdos_partition *p)
42{
43 return (sector_t)get_unaligned_le32(&p->nr_sects);
44}
45
46static inline sector_t start_sect(struct msdos_partition *p)
47{
48 return (sector_t)get_unaligned_le32(&p->start_sect);
49}
50
51static inline int is_extended_partition(struct msdos_partition *p)
52{
53 return (p->sys_ind == DOS_EXTENDED_PARTITION ||
54 p->sys_ind == WIN98_EXTENDED_PARTITION ||
55 p->sys_ind == LINUX_EXTENDED_PARTITION);
56}
57
58#define MSDOS_LABEL_MAGIC1 0x55
59#define MSDOS_LABEL_MAGIC2 0xAA
60
61static inline int
62msdos_magic_present(unsigned char *p)
63{
64 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
65}
66
67
68#define AIX_LABEL_MAGIC1 0xC9
69#define AIX_LABEL_MAGIC2 0xC2
70#define AIX_LABEL_MAGIC3 0xD4
71#define AIX_LABEL_MAGIC4 0xC1
72static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
73{
74 struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
75 Sector sect;
76 unsigned char *d;
77 int slot, ret = 0;
78
79 if (!(p[0] == AIX_LABEL_MAGIC1 &&
80 p[1] == AIX_LABEL_MAGIC2 &&
81 p[2] == AIX_LABEL_MAGIC3 &&
82 p[3] == AIX_LABEL_MAGIC4))
83 return 0;
84
85
86
87
88
89
90
91 for (slot = 1; slot <= 4; slot++, pt++) {
92 if (pt->sys_ind == SOLARIS_X86_PARTITION ||
93 pt->sys_ind == LINUX_RAID_PARTITION ||
94 pt->sys_ind == LINUX_DATA_PARTITION ||
95 pt->sys_ind == LINUX_LVM_PARTITION ||
96 is_extended_partition(pt))
97 return 0;
98 }
99 d = read_part_sector(state, 7, §);
100 if (d) {
101 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
102 ret = 1;
103 put_dev_sector(sect);
104 }
105 return ret;
106}
107
108static void set_info(struct parsed_partitions *state, int slot,
109 u32 disksig)
110{
111 struct partition_meta_info *info = &state->parts[slot].info;
112
113 snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
114 slot);
115 info->volname[0] = 0;
116 state->parts[slot].has_info = true;
117}
118
119
120
121
122
123
124
125
126
127
128
129
130static void parse_extended(struct parsed_partitions *state,
131 sector_t first_sector, sector_t first_size,
132 u32 disksig)
133{
134 struct msdos_partition *p;
135 Sector sect;
136 unsigned char *data;
137 sector_t this_sector, this_size;
138 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
139 int loopct = 0;
140
141 int i;
142
143 this_sector = first_sector;
144 this_size = first_size;
145
146 while (1) {
147 if (++loopct > 100)
148 return;
149 if (state->next == state->limit)
150 return;
151 data = read_part_sector(state, this_sector, §);
152 if (!data)
153 return;
154
155 if (!msdos_magic_present(data + 510))
156 goto done;
157
158 p = (struct msdos_partition *) (data + 0x1be);
159
160
161
162
163
164
165
166
167
168
169
170
171
172 for (i = 0; i < 4; i++, p++) {
173 sector_t offs, size, next;
174
175 if (!nr_sects(p) || is_extended_partition(p))
176 continue;
177
178
179
180 offs = start_sect(p)*sector_size;
181 size = nr_sects(p)*sector_size;
182 next = this_sector + offs;
183 if (i >= 2) {
184 if (offs + size > this_size)
185 continue;
186 if (next < first_sector)
187 continue;
188 if (next + size > first_sector + first_size)
189 continue;
190 }
191
192 put_partition(state, state->next, next, size);
193 set_info(state, state->next, disksig);
194 if (p->sys_ind == LINUX_RAID_PARTITION)
195 state->parts[state->next].flags = ADDPART_FLAG_RAID;
196 loopct = 0;
197 if (++state->next == state->limit)
198 goto done;
199 }
200
201
202
203
204
205
206
207 p -= 4;
208 for (i = 0; i < 4; i++, p++)
209 if (nr_sects(p) && is_extended_partition(p))
210 break;
211 if (i == 4)
212 goto done;
213
214 this_sector = first_sector + start_sect(p) * sector_size;
215 this_size = nr_sects(p) * sector_size;
216 put_dev_sector(sect);
217 }
218done:
219 put_dev_sector(sect);
220}
221
222#define SOLARIS_X86_NUMSLICE 16
223#define SOLARIS_X86_VTOC_SANE (0x600DDEEEUL)
224
225struct solaris_x86_slice {
226 __le16 s_tag;
227 __le16 s_flag;
228 __le32 s_start;
229 __le32 s_size;
230};
231
232struct solaris_x86_vtoc {
233 unsigned int v_bootinfo[3];
234 __le32 v_sanity;
235 __le32 v_version;
236 char v_volume[8];
237 __le16 v_sectorsz;
238 __le16 v_nparts;
239 unsigned int v_reserved[10];
240 struct solaris_x86_slice
241 v_slice[SOLARIS_X86_NUMSLICE];
242 unsigned int timestamp[SOLARIS_X86_NUMSLICE];
243 char v_asciilabel[128];
244};
245
246
247
248
249static void parse_solaris_x86(struct parsed_partitions *state,
250 sector_t offset, sector_t size, int origin)
251{
252#ifdef CONFIG_SOLARIS_X86_PARTITION
253 Sector sect;
254 struct solaris_x86_vtoc *v;
255 int i;
256 short max_nparts;
257
258 v = read_part_sector(state, offset + 1, §);
259 if (!v)
260 return;
261 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
262 put_dev_sector(sect);
263 return;
264 }
265 {
266 char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
267
268 snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
269 strlcat(state->pp_buf, tmp, PAGE_SIZE);
270 }
271 if (le32_to_cpu(v->v_version) != 1) {
272 char tmp[64];
273
274 snprintf(tmp, sizeof(tmp), " cannot handle version %d vtoc>\n",
275 le32_to_cpu(v->v_version));
276 strlcat(state->pp_buf, tmp, PAGE_SIZE);
277 put_dev_sector(sect);
278 return;
279 }
280
281 max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
282 for (i = 0; i < max_nparts && state->next < state->limit; i++) {
283 struct solaris_x86_slice *s = &v->v_slice[i];
284 char tmp[3 + 10 + 1 + 1];
285
286 if (s->s_size == 0)
287 continue;
288 snprintf(tmp, sizeof(tmp), " [s%d]", i);
289 strlcat(state->pp_buf, tmp, PAGE_SIZE);
290
291
292 put_partition(state, state->next++,
293 le32_to_cpu(s->s_start)+offset,
294 le32_to_cpu(s->s_size));
295 }
296 put_dev_sector(sect);
297 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
298#endif
299}
300
301
302#define BSD_DISKMAGIC (0x82564557UL)
303#define BSD_MAXPARTITIONS 16
304#define OPENBSD_MAXPARTITIONS 16
305#define BSD_FS_UNUSED 0
306struct bsd_disklabel {
307 __le32 d_magic;
308 __s16 d_type;
309 __s16 d_subtype;
310 char d_typename[16];
311 char d_packname[16];
312 __u32 d_secsize;
313 __u32 d_nsectors;
314 __u32 d_ntracks;
315 __u32 d_ncylinders;
316 __u32 d_secpercyl;
317 __u32 d_secperunit;
318 __u16 d_sparespertrack;
319 __u16 d_sparespercyl;
320 __u32 d_acylinders;
321 __u16 d_rpm;
322 __u16 d_interleave;
323 __u16 d_trackskew;
324 __u16 d_cylskew;
325 __u32 d_headswitch;
326 __u32 d_trkseek;
327 __u32 d_flags;
328#define NDDATA 5
329 __u32 d_drivedata[NDDATA];
330#define NSPARE 5
331 __u32 d_spare[NSPARE];
332 __le32 d_magic2;
333 __le16 d_checksum;
334
335
336 __le16 d_npartitions;
337 __le32 d_bbsize;
338 __le32 d_sbsize;
339 struct bsd_partition {
340 __le32 p_size;
341 __le32 p_offset;
342 __le32 p_fsize;
343 __u8 p_fstype;
344 __u8 p_frag;
345 __le16 p_cpg;
346 } d_partitions[BSD_MAXPARTITIONS];
347};
348
349#if defined(CONFIG_BSD_DISKLABEL)
350
351
352
353
354static void parse_bsd(struct parsed_partitions *state,
355 sector_t offset, sector_t size, int origin, char *flavour,
356 int max_partitions)
357{
358 Sector sect;
359 struct bsd_disklabel *l;
360 struct bsd_partition *p;
361 char tmp[64];
362
363 l = read_part_sector(state, offset + 1, §);
364 if (!l)
365 return;
366 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
367 put_dev_sector(sect);
368 return;
369 }
370
371 snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
372 strlcat(state->pp_buf, tmp, PAGE_SIZE);
373
374 if (le16_to_cpu(l->d_npartitions) < max_partitions)
375 max_partitions = le16_to_cpu(l->d_npartitions);
376 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
377 sector_t bsd_start, bsd_size;
378
379 if (state->next == state->limit)
380 break;
381 if (p->p_fstype == BSD_FS_UNUSED)
382 continue;
383 bsd_start = le32_to_cpu(p->p_offset);
384 bsd_size = le32_to_cpu(p->p_size);
385
386 if (memcmp(flavour, "bsd\0", 4) == 0 &&
387 le32_to_cpu(l->d_partitions[2].p_offset) == 0)
388 bsd_start += offset;
389 if (offset == bsd_start && size == bsd_size)
390
391 continue;
392 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
393 strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
394 continue;
395 }
396 put_partition(state, state->next++, bsd_start, bsd_size);
397 }
398 put_dev_sector(sect);
399 if (le16_to_cpu(l->d_npartitions) > max_partitions) {
400 snprintf(tmp, sizeof(tmp), " (ignored %d more)",
401 le16_to_cpu(l->d_npartitions) - max_partitions);
402 strlcat(state->pp_buf, tmp, PAGE_SIZE);
403 }
404 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
405}
406#endif
407
408static void parse_freebsd(struct parsed_partitions *state,
409 sector_t offset, sector_t size, int origin)
410{
411#ifdef CONFIG_BSD_DISKLABEL
412 parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
413#endif
414}
415
416static void parse_netbsd(struct parsed_partitions *state,
417 sector_t offset, sector_t size, int origin)
418{
419#ifdef CONFIG_BSD_DISKLABEL
420 parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
421#endif
422}
423
424static void parse_openbsd(struct parsed_partitions *state,
425 sector_t offset, sector_t size, int origin)
426{
427#ifdef CONFIG_BSD_DISKLABEL
428 parse_bsd(state, offset, size, origin, "openbsd",
429 OPENBSD_MAXPARTITIONS);
430#endif
431}
432
433#define UNIXWARE_DISKMAGIC (0xCA5E600DUL)
434#define UNIXWARE_DISKMAGIC2 (0x600DDEEEUL)
435#define UNIXWARE_NUMSLICE 16
436#define UNIXWARE_FS_UNUSED 0
437
438struct unixware_slice {
439 __le16 s_label;
440 __le16 s_flags;
441 __le32 start_sect;
442 __le32 nr_sects;
443};
444
445struct unixware_disklabel {
446 __le32 d_type;
447 __le32 d_magic;
448 __le32 d_version;
449 char d_serial[12];
450 __le32 d_ncylinders;
451 __le32 d_ntracks;
452 __le32 d_nsectors;
453 __le32 d_secsize;
454 __le32 d_part_start;
455 __le32 d_unknown1[12];
456 __le32 d_alt_tbl;
457 __le32 d_alt_len;
458 __le32 d_phys_cyl;
459 __le32 d_phys_trk;
460 __le32 d_phys_sec;
461 __le32 d_phys_bytes;
462 __le32 d_unknown2;
463 __le32 d_unknown3;
464 __le32 d_pad[8];
465
466 struct unixware_vtoc {
467 __le32 v_magic;
468 __le32 v_version;
469 char v_name[8];
470 __le16 v_nslices;
471 __le16 v_unknown1;
472 __le32 v_reserved[10];
473 struct unixware_slice
474 v_slice[UNIXWARE_NUMSLICE];
475 } vtoc;
476};
477
478
479
480
481
482static void parse_unixware(struct parsed_partitions *state,
483 sector_t offset, sector_t size, int origin)
484{
485#ifdef CONFIG_UNIXWARE_DISKLABEL
486 Sector sect;
487 struct unixware_disklabel *l;
488 struct unixware_slice *p;
489
490 l = read_part_sector(state, offset + 29, §);
491 if (!l)
492 return;
493 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
494 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
495 put_dev_sector(sect);
496 return;
497 }
498 {
499 char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
500
501 snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
502 strlcat(state->pp_buf, tmp, PAGE_SIZE);
503 }
504 p = &l->vtoc.v_slice[1];
505
506 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
507 if (state->next == state->limit)
508 break;
509
510 if (p->s_label != UNIXWARE_FS_UNUSED)
511 put_partition(state, state->next++,
512 le32_to_cpu(p->start_sect),
513 le32_to_cpu(p->nr_sects));
514 p++;
515 }
516 put_dev_sector(sect);
517 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
518#endif
519}
520
521#define MINIX_NR_SUBPARTITIONS 4
522
523
524
525
526
527
528static void parse_minix(struct parsed_partitions *state,
529 sector_t offset, sector_t size, int origin)
530{
531#ifdef CONFIG_MINIX_SUBPARTITION
532 Sector sect;
533 unsigned char *data;
534 struct msdos_partition *p;
535 int i;
536
537 data = read_part_sector(state, offset, §);
538 if (!data)
539 return;
540
541 p = (struct msdos_partition *)(data + 0x1be);
542
543
544
545
546 if (msdos_magic_present(data + 510) &&
547 p->sys_ind == MINIX_PARTITION) {
548 char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
549
550 snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
551 strlcat(state->pp_buf, tmp, PAGE_SIZE);
552 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
553 if (state->next == state->limit)
554 break;
555
556 if (p->sys_ind == MINIX_PARTITION)
557 put_partition(state, state->next++,
558 start_sect(p), nr_sects(p));
559 }
560 strlcat(state->pp_buf, " >\n", PAGE_SIZE);
561 }
562 put_dev_sector(sect);
563#endif
564}
565
566static struct {
567 unsigned char id;
568 void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
569} subtypes[] = {
570 {FREEBSD_PARTITION, parse_freebsd},
571 {NETBSD_PARTITION, parse_netbsd},
572 {OPENBSD_PARTITION, parse_openbsd},
573 {MINIX_PARTITION, parse_minix},
574 {UNIXWARE_PARTITION, parse_unixware},
575 {SOLARIS_X86_PARTITION, parse_solaris_x86},
576 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
577 {0, NULL},
578};
579
580int msdos_partition(struct parsed_partitions *state)
581{
582 sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
583 Sector sect;
584 unsigned char *data;
585 struct msdos_partition *p;
586 struct fat_boot_sector *fb;
587 int slot;
588 u32 disksig;
589
590 data = read_part_sector(state, 0, §);
591 if (!data)
592 return -1;
593
594
595
596
597
598 if (aix_magic_present(state, data)) {
599 put_dev_sector(sect);
600#ifdef CONFIG_AIX_PARTITION
601 return aix_partition(state);
602#else
603 strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
604 return 0;
605#endif
606 }
607
608 if (!msdos_magic_present(data + 510)) {
609 put_dev_sector(sect);
610 return 0;
611 }
612
613
614
615
616
617
618
619 p = (struct msdos_partition *) (data + 0x1be);
620 for (slot = 1; slot <= 4; slot++, p++) {
621 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
622
623
624
625
626
627 fb = (struct fat_boot_sector *) data;
628 if (slot == 1 && fb->reserved && fb->fats
629 && fat_valid_media(fb->media)) {
630 strlcat(state->pp_buf, "\n", PAGE_SIZE);
631 put_dev_sector(sect);
632 return 1;
633 } else {
634 put_dev_sector(sect);
635 return 0;
636 }
637 }
638 }
639
640#ifdef CONFIG_EFI_PARTITION
641 p = (struct msdos_partition *) (data + 0x1be);
642 for (slot = 1 ; slot <= 4 ; slot++, p++) {
643
644 if (p->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT) {
645 put_dev_sector(sect);
646 return 0;
647 }
648 }
649#endif
650 p = (struct msdos_partition *) (data + 0x1be);
651
652 disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
653
654
655
656
657
658
659
660 state->next = 5;
661 for (slot = 1 ; slot <= 4 ; slot++, p++) {
662 sector_t start = start_sect(p)*sector_size;
663 sector_t size = nr_sects(p)*sector_size;
664
665 if (!size)
666 continue;
667 if (is_extended_partition(p)) {
668
669
670
671
672
673
674 sector_t n = 2;
675
676 n = min(size, max(sector_size, n));
677 put_partition(state, slot, start, n);
678
679 strlcat(state->pp_buf, " <", PAGE_SIZE);
680 parse_extended(state, start, size, disksig);
681 strlcat(state->pp_buf, " >", PAGE_SIZE);
682 continue;
683 }
684 put_partition(state, slot, start, size);
685 set_info(state, slot, disksig);
686 if (p->sys_ind == LINUX_RAID_PARTITION)
687 state->parts[slot].flags = ADDPART_FLAG_RAID;
688 if (p->sys_ind == DM6_PARTITION)
689 strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
690 if (p->sys_ind == EZD_PARTITION)
691 strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
692 }
693
694 strlcat(state->pp_buf, "\n", PAGE_SIZE);
695
696
697 p = (struct msdos_partition *) (0x1be + data);
698 for (slot = 1 ; slot <= 4 ; slot++, p++) {
699 unsigned char id = p->sys_ind;
700 int n;
701
702 if (!nr_sects(p))
703 continue;
704
705 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
706 ;
707
708 if (!subtypes[n].parse)
709 continue;
710 subtypes[n].parse(state, start_sect(p) * sector_size,
711 nr_sects(p) * sector_size, slot);
712 }
713 put_dev_sector(sect);
714 return 1;
715}
716