blob: dafd3b6b2dc3301be1c03946bfaf0dfd5e6a9f0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include "check.h"
24#include "msdos.h"
25#include "efi.h"
26
27/*
28 * Many architectures don't like unaligned accesses, while
29 * the nr_sects and start_sect partition table entries are
30 * at a 2 (mod 4) address.
31 */
32#include <asm/unaligned.h>
33
34#define SYS_IND(p) (get_unaligned(&p->sys_ind))
Al Viro3524de12005-12-24 14:39:13 -050035#define NR_SECTS(p) ({ __le32 __a = get_unaligned(&p->nr_sects); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 le32_to_cpu(__a); \
37 })
38
Al Viro3524de12005-12-24 14:39:13 -050039#define START_SECT(p) ({ __le32 __a = get_unaligned(&p->start_sect); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 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
Olaf Heringe1dfa922006-09-29 01:59:39 -070059/* Value is EBCDIC 'IBMA' */
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{
Olaf Hering4419d1a2007-02-10 01:45:47 -080066 struct partition *pt = (struct partition *) (p + 0x1be);
Olaf Heringe1dfa922006-09-29 01:59:39 -070067 Sector sect;
68 unsigned char *d;
Olaf Hering4419d1a2007-02-10 01:45:47 -080069 int slot, ret = 0;
Olaf Heringe1dfa922006-09-29 01:59:39 -070070
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;
Olaf Hering4419d1a2007-02-10 01:45:47 -080076 /* Assume the partition table is valid if Linux partitions exists */
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 }
Olaf Heringe1dfa922006-09-29 01:59:39 -070085 d = read_dev_sector(bdev, 7, &sect);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/*
95 * Create devices for each logical partition in an extended partition.
96 * The logical partitions form a linked list, with each entry being
97 * a partition table with two entries. The first entry
98 * is the real data partition (with a start relative to the partition
99 * table start). The second is a pointer to the next logical partition
100 * (with a start relative to the entire extended partition).
101 * We do not create a Linux partition for the partition tables, but
102 * only for the actual data partitions.
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_hardsect_size(bdev) / 512;
114 int loopct = 0; /* number of links followed
115 without finding a data partition */
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, &sect);
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 * Usually, the first entry is the real data partition,
137 * the 2nd entry is the next extended partition, or empty,
138 * and the 3rd and 4th entries are unused.
139 * However, DRDOS sometimes has the extended partition as
140 * the first entry (when the data partition is empty),
141 * and OS/2 seems to use all four entries.
142 */
143
144 /*
145 * First process the data partition(s)
146 */
147 for (i=0; i<4; i++, p++) {
148 u32 offs, size, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (!NR_SECTS(p) || is_extended_partition(p))
150 continue;
151
152 /* Check the 3rd and 4th entries -
153 these sometimes contain random garbage */
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 = 1;
169 loopct = 0;
170 if (++state->next == state->limit)
171 goto done;
172 }
173 /*
174 * Next, process the (first) extended partition, if present.
175 * (So far, there seems to be no reason to make
176 * parse_extended() recursive and allow a tree
177 * of extended partitions.)
178 * It should be a link to the next logical partition.
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; /* nothing left to do */
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/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
196 indicates linux swap. Be careful before believing this is Solaris. */
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
207 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, &sect);
208 if (!v)
209 return;
210 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
211 put_dev_sector(sect);
212 return;
213 }
214 printk(" %s%d: <solaris:", state->name, origin);
215 if (le32_to_cpu(v->v_version) != 1) {
216 printk(" cannot handle version %d vtoc>\n",
217 le32_to_cpu(v->v_version));
218 put_dev_sector(sect);
219 return;
220 }
221 for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
222 struct solaris_x86_slice *s = &v->v_slice[i];
223 if (s->s_size == 0)
224 continue;
225 printk(" [s%d]", i);
226 /* solaris partitions are relative to current MS-DOS
227 * one; must add the offset of the current partition */
228 put_partition(state, state->next++,
229 le32_to_cpu(s->s_start)+offset,
230 le32_to_cpu(s->s_size));
231 }
232 put_dev_sector(sect);
233 printk(" >\n");
234#endif
235}
236
Adrian Bunk486fd402005-06-25 14:58:47 -0700237#if defined(CONFIG_BSD_DISKLABEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
239 * Create devices for BSD partitions listed in a disklabel, under a
240 * dos-like partition. See parse_extended() for more information.
241 */
Adrian Bunk486fd402005-06-25 14:58:47 -0700242static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
244 u32 offset, u32 size, int origin, char *flavour,
245 int max_partitions)
246{
247 Sector sect;
248 struct bsd_disklabel *l;
249 struct bsd_partition *p;
250
251 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, &sect);
252 if (!l)
253 return;
254 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
255 put_dev_sector(sect);
256 return;
257 }
258 printk(" %s%d: <%s:", state->name, origin, flavour);
259
260 if (le16_to_cpu(l->d_npartitions) < max_partitions)
261 max_partitions = le16_to_cpu(l->d_npartitions);
262 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
263 u32 bsd_start, bsd_size;
264
265 if (state->next == state->limit)
266 break;
267 if (p->p_fstype == BSD_FS_UNUSED)
268 continue;
269 bsd_start = le32_to_cpu(p->p_offset);
270 bsd_size = le32_to_cpu(p->p_size);
271 if (offset == bsd_start && size == bsd_size)
272 /* full parent partition, we have it already */
273 continue;
274 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
275 printk("bad subpartition - ignored\n");
276 continue;
277 }
278 put_partition(state, state->next++, bsd_start, bsd_size);
279 }
280 put_dev_sector(sect);
281 if (le16_to_cpu(l->d_npartitions) > max_partitions)
282 printk(" (ignored %d more)",
283 le16_to_cpu(l->d_npartitions) - max_partitions);
284 printk(" >\n");
285}
286#endif
287
288static void
289parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
290 u32 offset, u32 size, int origin)
291{
292#ifdef CONFIG_BSD_DISKLABEL
293 parse_bsd(state, bdev, offset, size, origin,
294 "bsd", BSD_MAXPARTITIONS);
295#endif
296}
297
298static void
299parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
300 u32 offset, u32 size, int origin)
301{
302#ifdef CONFIG_BSD_DISKLABEL
303 parse_bsd(state, bdev, offset, size, origin,
304 "netbsd", BSD_MAXPARTITIONS);
305#endif
306}
307
308static void
309parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
310 u32 offset, u32 size, int origin)
311{
312#ifdef CONFIG_BSD_DISKLABEL
313 parse_bsd(state, bdev, offset, size, origin,
314 "openbsd", OPENBSD_MAXPARTITIONS);
315#endif
316}
317
318/*
319 * Create devices for Unixware partitions listed in a disklabel, under a
320 * dos-like partition. See parse_extended() for more information.
321 */
322static void
323parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
324 u32 offset, u32 size, int origin)
325{
326#ifdef CONFIG_UNIXWARE_DISKLABEL
327 Sector sect;
328 struct unixware_disklabel *l;
329 struct unixware_slice *p;
330
331 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, &sect);
332 if (!l)
333 return;
334 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
335 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
336 put_dev_sector(sect);
337 return;
338 }
339 printk(" %s%d: <unixware:", state->name, origin);
340 p = &l->vtoc.v_slice[1];
341 /* I omit the 0th slice as it is the same as whole disk. */
342 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
343 if (state->next == state->limit)
344 break;
345
346 if (p->s_label != UNIXWARE_FS_UNUSED)
347 put_partition(state, state->next++,
348 START_SECT(p), NR_SECTS(p));
349 p++;
350 }
351 put_dev_sector(sect);
352 printk(" >\n");
353#endif
354}
355
356/*
357 * Minix 2.0.0/2.0.2 subpartition support.
358 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
359 * Rajeev V. Pillai <rajeevvp@yahoo.com>
360 */
361static void
362parse_minix(struct parsed_partitions *state, struct block_device *bdev,
363 u32 offset, u32 size, int origin)
364{
365#ifdef CONFIG_MINIX_SUBPARTITION
366 Sector sect;
367 unsigned char *data;
368 struct partition *p;
369 int i;
370
371 data = read_dev_sector(bdev, offset, &sect);
372 if (!data)
373 return;
374
375 p = (struct partition *)(data + 0x1be);
376
377 /* The first sector of a Minix partition can have either
378 * a secondary MBR describing its subpartitions, or
379 * the normal boot sector. */
380 if (msdos_magic_present (data + 510) &&
381 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
382
383 printk(" %s%d: <minix:", state->name, origin);
384 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
385 if (state->next == state->limit)
386 break;
387 /* add each partition in use */
388 if (SYS_IND(p) == MINIX_PARTITION)
389 put_partition(state, state->next++,
390 START_SECT(p), NR_SECTS(p));
391 }
392 printk(" >\n");
393 }
394 put_dev_sector(sect);
395#endif /* CONFIG_MINIX_SUBPARTITION */
396}
397
398static struct {
399 unsigned char id;
400 void (*parse)(struct parsed_partitions *, struct block_device *,
401 u32, u32, int);
402} subtypes[] = {
403 {FREEBSD_PARTITION, parse_freebsd},
404 {NETBSD_PARTITION, parse_netbsd},
405 {OPENBSD_PARTITION, parse_openbsd},
406 {MINIX_PARTITION, parse_minix},
407 {UNIXWARE_PARTITION, parse_unixware},
408 {SOLARIS_X86_PARTITION, parse_solaris_x86},
409 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
410 {0, NULL},
411};
412
413int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
414{
415 int sector_size = bdev_hardsect_size(bdev) / 512;
416 Sector sect;
417 unsigned char *data;
418 struct partition *p;
419 int slot;
420
421 data = read_dev_sector(bdev, 0, &sect);
422 if (!data)
423 return -1;
424 if (!msdos_magic_present(data + 510)) {
425 put_dev_sector(sect);
426 return 0;
427 }
428
Olaf Heringe1dfa922006-09-29 01:59:39 -0700429 if (aix_magic_present(data, bdev)) {
430 put_dev_sector(sect);
431 printk( " [AIX]");
432 return 0;
433 }
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 /*
436 * Now that the 55aa signature is present, this is probably
437 * either the boot sector of a FAT filesystem or a DOS-type
438 * partition table. Reject this in case the boot indicator
439 * is not 0 or 0x80.
440 */
441 p = (struct partition *) (data + 0x1be);
442 for (slot = 1; slot <= 4; slot++, p++) {
443 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
444 put_dev_sector(sect);
445 return 0;
446 }
447 }
448
449#ifdef CONFIG_EFI_PARTITION
450 p = (struct partition *) (data + 0x1be);
451 for (slot = 1 ; slot <= 4 ; slot++, p++) {
452 /* If this is an EFI GPT disk, msdos should ignore it. */
453 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
454 put_dev_sector(sect);
455 return 0;
456 }
457 }
458#endif
459 p = (struct partition *) (data + 0x1be);
460
461 /*
462 * Look for partitions in two passes:
463 * First find the primary and DOS-type extended partitions.
464 * On the second pass look inside *BSD, Unixware and Solaris partitions.
465 */
466
467 state->next = 5;
468 for (slot = 1 ; slot <= 4 ; slot++, p++) {
469 u32 start = START_SECT(p)*sector_size;
470 u32 size = NR_SECTS(p)*sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (!size)
472 continue;
473 if (is_extended_partition(p)) {
474 /* prevent someone doing mkfs or mkswap on an
475 extended partition, but leave room for LILO */
476 put_partition(state, slot, start, size == 1 ? 1 : 2);
477 printk(" <");
478 parse_extended(state, bdev, start, size);
479 printk(" >");
480 continue;
481 }
482 put_partition(state, slot, start, size);
483 if (SYS_IND(p) == LINUX_RAID_PARTITION)
484 state->parts[slot].flags = 1;
485 if (SYS_IND(p) == DM6_PARTITION)
486 printk("[DM]");
487 if (SYS_IND(p) == EZD_PARTITION)
488 printk("[EZD]");
489 }
490
491 printk("\n");
492
493 /* second pass - output for each on a separate line */
494 p = (struct partition *) (0x1be + data);
495 for (slot = 1 ; slot <= 4 ; slot++, p++) {
496 unsigned char id = SYS_IND(p);
497 int n;
498
499 if (!NR_SECTS(p))
500 continue;
501
502 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
503 ;
504
505 if (!subtypes[n].parse)
506 continue;
507 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
508 NR_SECTS(p)*sector_size, slot);
509 }
510 put_dev_sector(sect);
511 return 1;
512}