1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include <linux/msdos_fs.h>
22
23#include "check.h"
24#include "msdos.h"
25#include "efi.h"
26
27
28
29
30
31
32#include <asm/unaligned.h>
33
34#define SYS_IND(p) (get_unaligned(&p->sys_ind))
35#define NR_SECTS(p) ({ __le32 __a = get_unaligned(&p->nr_sects); \
36 le32_to_cpu(__a); \
37 })
38
39#define START_SECT(p) ({ __le32 __a = get_unaligned(&p->start_sect); \
40 le32_to_cpu(__a); \
41 })
42
43static inline int is_extended_partition(struct partition *p)
44{
45 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
46 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
47 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
48}
49
50#define MSDOS_LABEL_MAGIC1 0x55
51#define MSDOS_LABEL_MAGIC2 0xAA
52
53static inline int
54msdos_magic_present(unsigned char *p)
55{
56 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
57}
58
59
60#define AIX_LABEL_MAGIC1 0xC9
61#define AIX_LABEL_MAGIC2 0xC2
62#define AIX_LABEL_MAGIC3 0xD4
63#define AIX_LABEL_MAGIC4 0xC1
64static int aix_magic_present(unsigned char *p, struct block_device *bdev)
65{
66 struct partition *pt = (struct partition *) (p + 0x1be);
67 Sector sect;
68 unsigned char *d;
69 int slot, ret = 0;
70
71 if (!(p[0] == AIX_LABEL_MAGIC1 &&
72 p[1] == AIX_LABEL_MAGIC2 &&
73 p[2] == AIX_LABEL_MAGIC3 &&
74 p[3] == AIX_LABEL_MAGIC4))
75 return 0;
76
77 for (slot = 1; slot <= 4; slot++, pt++) {
78 if (pt->sys_ind == LINUX_SWAP_PARTITION ||
79 pt->sys_ind == LINUX_RAID_PARTITION ||
80 pt->sys_ind == LINUX_DATA_PARTITION ||
81 pt->sys_ind == LINUX_LVM_PARTITION ||
82 is_extended_partition(pt))
83 return 0;
84 }
85 d = read_dev_sector(bdev, 7, §);
86 if (d) {
87 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
88 ret = 1;
89 put_dev_sector(sect);
90 };
91 return ret;
92}
93
94
95
96
97
98
99
100
101
102
103
104
105static void
106parse_extended(struct parsed_partitions *state, struct block_device *bdev,
107 u32 first_sector, u32 first_size)
108{
109 struct partition *p;
110 Sector sect;
111 unsigned char *data;
112 u32 this_sector, this_size;
113 int sector_size = bdev_logical_block_size(bdev) / 512;
114 int loopct = 0;
115
116 int i;
117
118 this_sector = first_sector;
119 this_size = first_size;
120
121 while (1) {
122 if (++loopct > 100)
123 return;
124 if (state->next == state->limit)
125 return;
126 data = read_dev_sector(bdev, this_sector, §);
127 if (!data)
128 return;
129
130 if (!msdos_magic_present(data + 510))
131 goto done;
132
133 p = (struct partition *) (data + 0x1be);
134
135
136
137
138
139
140
141
142
143
144
145
146
147 for (i=0; i<4; i++, p++) {
148 u32 offs, size, next;
149 if (!NR_SECTS(p) || is_extended_partition(p))
150 continue;
151
152
153
154 offs = START_SECT(p)*sector_size;
155 size = NR_SECTS(p)*sector_size;
156 next = this_sector + offs;
157 if (i >= 2) {
158 if (offs + size > this_size)
159 continue;
160 if (next < first_sector)
161 continue;
162 if (next + size > first_sector + first_size)
163 continue;
164 }
165
166 put_partition(state, state->next, next, size);
167 if (SYS_IND(p) == LINUX_RAID_PARTITION)
168 state->parts[state->next].flags = ADDPART_FLAG_RAID;
169 loopct = 0;
170 if (++state->next == state->limit)
171 goto done;
172 }
173
174
175
176
177
178
179
180 p -= 4;
181 for (i=0; i<4; i++, p++)
182 if (NR_SECTS(p) && is_extended_partition(p))
183 break;
184 if (i == 4)
185 goto done;
186
187 this_sector = first_sector + START_SECT(p) * sector_size;
188 this_size = NR_SECTS(p) * sector_size;
189 put_dev_sector(sect);
190 }
191done:
192 put_dev_sector(sect);
193}
194
195
196
197
198static void
199parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
200 u32 offset, u32 size, int origin)
201{
202#ifdef CONFIG_SOLARIS_X86_PARTITION
203 Sector sect;
204 struct solaris_x86_vtoc *v;
205 int i;
206 short max_nparts;
207
208 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, §);
209 if (!v)
210 return;
211 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
212 put_dev_sector(sect);
213 return;
214 }
215 printk(" %s%d: <solaris:", state->name, origin);
216 if (le32_to_cpu(v->v_version) != 1) {
217 printk(" cannot handle version %d vtoc>\n",
218 le32_to_cpu(v->v_version));
219 put_dev_sector(sect);
220 return;
221 }
222
223 max_nparts = le16_to_cpu (v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
224 for (i=0; i<max_nparts && state->next<state->limit; i++) {
225 struct solaris_x86_slice *s = &v->v_slice[i];
226 if (s->s_size == 0)
227 continue;
228 printk(" [s%d]", i);
229
230
231 put_partition(state, state->next++,
232 le32_to_cpu(s->s_start)+offset,
233 le32_to_cpu(s->s_size));
234 }
235 put_dev_sector(sect);
236 printk(" >\n");
237#endif
238}
239
240#if defined(CONFIG_BSD_DISKLABEL)
241
242
243
244
245static void
246parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
247 u32 offset, u32 size, int origin, char *flavour,
248 int max_partitions)
249{
250 Sector sect;
251 struct bsd_disklabel *l;
252 struct bsd_partition *p;
253
254 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, §);
255 if (!l)
256 return;
257 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
258 put_dev_sector(sect);
259 return;
260 }
261 printk(" %s%d: <%s:", state->name, origin, flavour);
262
263 if (le16_to_cpu(l->d_npartitions) < max_partitions)
264 max_partitions = le16_to_cpu(l->d_npartitions);
265 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
266 u32 bsd_start, bsd_size;
267
268 if (state->next == state->limit)
269 break;
270 if (p->p_fstype == BSD_FS_UNUSED)
271 continue;
272 bsd_start = le32_to_cpu(p->p_offset);
273 bsd_size = le32_to_cpu(p->p_size);
274 if (offset == bsd_start && size == bsd_size)
275
276 continue;
277 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
278 printk("bad subpartition - ignored\n");
279 continue;
280 }
281 put_partition(state, state->next++, bsd_start, bsd_size);
282 }
283 put_dev_sector(sect);
284 if (le16_to_cpu(l->d_npartitions) > max_partitions)
285 printk(" (ignored %d more)",
286 le16_to_cpu(l->d_npartitions) - max_partitions);
287 printk(" >\n");
288}
289#endif
290
291static void
292parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
293 u32 offset, u32 size, int origin)
294{
295#ifdef CONFIG_BSD_DISKLABEL
296 parse_bsd(state, bdev, offset, size, origin,
297 "bsd", BSD_MAXPARTITIONS);
298#endif
299}
300
301static void
302parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
303 u32 offset, u32 size, int origin)
304{
305#ifdef CONFIG_BSD_DISKLABEL
306 parse_bsd(state, bdev, offset, size, origin,
307 "netbsd", BSD_MAXPARTITIONS);
308#endif
309}
310
311static void
312parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
313 u32 offset, u32 size, int origin)
314{
315#ifdef CONFIG_BSD_DISKLABEL
316 parse_bsd(state, bdev, offset, size, origin,
317 "openbsd", OPENBSD_MAXPARTITIONS);
318#endif
319}
320
321
322
323
324
325static void
326parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
327 u32 offset, u32 size, int origin)
328{
329#ifdef CONFIG_UNIXWARE_DISKLABEL
330 Sector sect;
331 struct unixware_disklabel *l;
332 struct unixware_slice *p;
333
334 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, §);
335 if (!l)
336 return;
337 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
338 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
339 put_dev_sector(sect);
340 return;
341 }
342 printk(" %s%d: <unixware:", state->name, origin);
343 p = &l->vtoc.v_slice[1];
344
345 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
346 if (state->next == state->limit)
347 break;
348
349 if (p->s_label != UNIXWARE_FS_UNUSED)
350 put_partition(state, state->next++,
351 START_SECT(p), NR_SECTS(p));
352 p++;
353 }
354 put_dev_sector(sect);
355 printk(" >\n");
356#endif
357}
358
359
360
361
362
363
364static void
365parse_minix(struct parsed_partitions *state, struct block_device *bdev,
366 u32 offset, u32 size, int origin)
367{
368#ifdef CONFIG_MINIX_SUBPARTITION
369 Sector sect;
370 unsigned char *data;
371 struct partition *p;
372 int i;
373
374 data = read_dev_sector(bdev, offset, §);
375 if (!data)
376 return;
377
378 p = (struct partition *)(data + 0x1be);
379
380
381
382
383 if (msdos_magic_present (data + 510) &&
384 SYS_IND(p) == MINIX_PARTITION) {
385
386 printk(" %s%d: <minix:", state->name, origin);
387 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
388 if (state->next == state->limit)
389 break;
390
391 if (SYS_IND(p) == MINIX_PARTITION)
392 put_partition(state, state->next++,
393 START_SECT(p), NR_SECTS(p));
394 }
395 printk(" >\n");
396 }
397 put_dev_sector(sect);
398#endif
399}
400
401static struct {
402 unsigned char id;
403 void (*parse)(struct parsed_partitions *, struct block_device *,
404 u32, u32, int);
405} subtypes[] = {
406 {FREEBSD_PARTITION, parse_freebsd},
407 {NETBSD_PARTITION, parse_netbsd},
408 {OPENBSD_PARTITION, parse_openbsd},
409 {MINIX_PARTITION, parse_minix},
410 {UNIXWARE_PARTITION, parse_unixware},
411 {SOLARIS_X86_PARTITION, parse_solaris_x86},
412 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
413 {0, NULL},
414};
415
416int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
417{
418 int sector_size = bdev_logical_block_size(bdev) / 512;
419 Sector sect;
420 unsigned char *data;
421 struct partition *p;
422 struct fat_boot_sector *fb;
423 int slot;
424
425 data = read_dev_sector(bdev, 0, §);
426 if (!data)
427 return -1;
428 if (!msdos_magic_present(data + 510)) {
429 put_dev_sector(sect);
430 return 0;
431 }
432
433 if (aix_magic_present(data, bdev)) {
434 put_dev_sector(sect);
435 printk( " [AIX]");
436 return 0;
437 }
438
439
440
441
442
443
444
445 p = (struct partition *) (data + 0x1be);
446 for (slot = 1; slot <= 4; slot++, p++) {
447 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
448
449
450
451
452
453 fb = (struct fat_boot_sector *) data;
454 if (slot == 1 && fb->reserved && fb->fats
455 && fat_valid_media(fb->media)) {
456 printk("\n");
457 put_dev_sector(sect);
458 return 1;
459 } else {
460 put_dev_sector(sect);
461 return 0;
462 }
463 }
464 }
465
466#ifdef CONFIG_EFI_PARTITION
467 p = (struct partition *) (data + 0x1be);
468 for (slot = 1 ; slot <= 4 ; slot++, p++) {
469
470 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
471 put_dev_sector(sect);
472 return 0;
473 }
474 }
475#endif
476 p = (struct partition *) (data + 0x1be);
477
478
479
480
481
482
483
484 state->next = 5;
485 for (slot = 1 ; slot <= 4 ; slot++, p++) {
486 u32 start = START_SECT(p)*sector_size;
487 u32 size = NR_SECTS(p)*sector_size;
488 if (!size)
489 continue;
490 if (is_extended_partition(p)) {
491
492
493 put_partition(state, slot, start, size == 1 ? 1 : 2);
494 printk(" <");
495 parse_extended(state, bdev, start, size);
496 printk(" >");
497 continue;
498 }
499 put_partition(state, slot, start, size);
500 if (SYS_IND(p) == LINUX_RAID_PARTITION)
501 state->parts[slot].flags = 1;
502 if (SYS_IND(p) == DM6_PARTITION)
503 printk("[DM]");
504 if (SYS_IND(p) == EZD_PARTITION)
505 printk("[EZD]");
506 }
507
508 printk("\n");
509
510
511 p = (struct partition *) (0x1be + data);
512 for (slot = 1 ; slot <= 4 ; slot++, p++) {
513 unsigned char id = SYS_IND(p);
514 int n;
515
516 if (!NR_SECTS(p))
517 continue;
518
519 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
520 ;
521
522 if (!subtypes[n].parse)
523 continue;
524 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
525 NR_SECTS(p)*sector_size, slot);
526 }
527 put_dev_sector(sect);
528 return 1;
529}
530