blob: 8c7af1777819e2b2d5cdeabc49ef87298ca89c19 [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{
66 Sector sect;
67 unsigned char *d;
68 int ret = 0;
69
70 if (p[0] != AIX_LABEL_MAGIC1 &&
71 p[1] != AIX_LABEL_MAGIC2 &&
72 p[2] != AIX_LABEL_MAGIC3 &&
73 p[3] != AIX_LABEL_MAGIC4)
74 return 0;
75 d = read_dev_sector(bdev, 7, &sect);
76 if (d) {
77 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
78 ret = 1;
79 put_dev_sector(sect);
80 };
81 return ret;
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * Create devices for each logical partition in an extended partition.
86 * The logical partitions form a linked list, with each entry being
87 * a partition table with two entries. The first entry
88 * is the real data partition (with a start relative to the partition
89 * table start). The second is a pointer to the next logical partition
90 * (with a start relative to the entire extended partition).
91 * We do not create a Linux partition for the partition tables, but
92 * only for the actual data partitions.
93 */
94
95static void
96parse_extended(struct parsed_partitions *state, struct block_device *bdev,
97 u32 first_sector, u32 first_size)
98{
99 struct partition *p;
100 Sector sect;
101 unsigned char *data;
102 u32 this_sector, this_size;
103 int sector_size = bdev_hardsect_size(bdev) / 512;
104 int loopct = 0; /* number of links followed
105 without finding a data partition */
106 int i;
107
108 this_sector = first_sector;
109 this_size = first_size;
110
111 while (1) {
112 if (++loopct > 100)
113 return;
114 if (state->next == state->limit)
115 return;
116 data = read_dev_sector(bdev, this_sector, &sect);
117 if (!data)
118 return;
119
120 if (!msdos_magic_present(data + 510))
121 goto done;
122
123 p = (struct partition *) (data + 0x1be);
124
125 /*
126 * Usually, the first entry is the real data partition,
127 * the 2nd entry is the next extended partition, or empty,
128 * and the 3rd and 4th entries are unused.
129 * However, DRDOS sometimes has the extended partition as
130 * the first entry (when the data partition is empty),
131 * and OS/2 seems to use all four entries.
132 */
133
134 /*
135 * First process the data partition(s)
136 */
137 for (i=0; i<4; i++, p++) {
138 u32 offs, size, next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (!NR_SECTS(p) || is_extended_partition(p))
140 continue;
141
142 /* Check the 3rd and 4th entries -
143 these sometimes contain random garbage */
144 offs = START_SECT(p)*sector_size;
145 size = NR_SECTS(p)*sector_size;
146 next = this_sector + offs;
147 if (i >= 2) {
148 if (offs + size > this_size)
149 continue;
150 if (next < first_sector)
151 continue;
152 if (next + size > first_sector + first_size)
153 continue;
154 }
155
156 put_partition(state, state->next, next, size);
157 if (SYS_IND(p) == LINUX_RAID_PARTITION)
158 state->parts[state->next].flags = 1;
159 loopct = 0;
160 if (++state->next == state->limit)
161 goto done;
162 }
163 /*
164 * Next, process the (first) extended partition, if present.
165 * (So far, there seems to be no reason to make
166 * parse_extended() recursive and allow a tree
167 * of extended partitions.)
168 * It should be a link to the next logical partition.
169 */
170 p -= 4;
171 for (i=0; i<4; i++, p++)
172 if (NR_SECTS(p) && is_extended_partition(p))
173 break;
174 if (i == 4)
175 goto done; /* nothing left to do */
176
177 this_sector = first_sector + START_SECT(p) * sector_size;
178 this_size = NR_SECTS(p) * sector_size;
179 put_dev_sector(sect);
180 }
181done:
182 put_dev_sector(sect);
183}
184
185/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
186 indicates linux swap. Be careful before believing this is Solaris. */
187
188static void
189parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
190 u32 offset, u32 size, int origin)
191{
192#ifdef CONFIG_SOLARIS_X86_PARTITION
193 Sector sect;
194 struct solaris_x86_vtoc *v;
195 int i;
196
197 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, &sect);
198 if (!v)
199 return;
200 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
201 put_dev_sector(sect);
202 return;
203 }
204 printk(" %s%d: <solaris:", state->name, origin);
205 if (le32_to_cpu(v->v_version) != 1) {
206 printk(" cannot handle version %d vtoc>\n",
207 le32_to_cpu(v->v_version));
208 put_dev_sector(sect);
209 return;
210 }
211 for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
212 struct solaris_x86_slice *s = &v->v_slice[i];
213 if (s->s_size == 0)
214 continue;
215 printk(" [s%d]", i);
216 /* solaris partitions are relative to current MS-DOS
217 * one; must add the offset of the current partition */
218 put_partition(state, state->next++,
219 le32_to_cpu(s->s_start)+offset,
220 le32_to_cpu(s->s_size));
221 }
222 put_dev_sector(sect);
223 printk(" >\n");
224#endif
225}
226
Adrian Bunk486fd402005-06-25 14:58:47 -0700227#if defined(CONFIG_BSD_DISKLABEL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * Create devices for BSD partitions listed in a disklabel, under a
230 * dos-like partition. See parse_extended() for more information.
231 */
Adrian Bunk486fd402005-06-25 14:58:47 -0700232static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
234 u32 offset, u32 size, int origin, char *flavour,
235 int max_partitions)
236{
237 Sector sect;
238 struct bsd_disklabel *l;
239 struct bsd_partition *p;
240
241 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, &sect);
242 if (!l)
243 return;
244 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
245 put_dev_sector(sect);
246 return;
247 }
248 printk(" %s%d: <%s:", state->name, origin, flavour);
249
250 if (le16_to_cpu(l->d_npartitions) < max_partitions)
251 max_partitions = le16_to_cpu(l->d_npartitions);
252 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
253 u32 bsd_start, bsd_size;
254
255 if (state->next == state->limit)
256 break;
257 if (p->p_fstype == BSD_FS_UNUSED)
258 continue;
259 bsd_start = le32_to_cpu(p->p_offset);
260 bsd_size = le32_to_cpu(p->p_size);
261 if (offset == bsd_start && size == bsd_size)
262 /* full parent partition, we have it already */
263 continue;
264 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
265 printk("bad subpartition - ignored\n");
266 continue;
267 }
268 put_partition(state, state->next++, bsd_start, bsd_size);
269 }
270 put_dev_sector(sect);
271 if (le16_to_cpu(l->d_npartitions) > max_partitions)
272 printk(" (ignored %d more)",
273 le16_to_cpu(l->d_npartitions) - max_partitions);
274 printk(" >\n");
275}
276#endif
277
278static void
279parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
280 u32 offset, u32 size, int origin)
281{
282#ifdef CONFIG_BSD_DISKLABEL
283 parse_bsd(state, bdev, offset, size, origin,
284 "bsd", BSD_MAXPARTITIONS);
285#endif
286}
287
288static void
289parse_netbsd(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 "netbsd", BSD_MAXPARTITIONS);
295#endif
296}
297
298static void
299parse_openbsd(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 "openbsd", OPENBSD_MAXPARTITIONS);
305#endif
306}
307
308/*
309 * Create devices for Unixware partitions listed in a disklabel, under a
310 * dos-like partition. See parse_extended() for more information.
311 */
312static void
313parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
314 u32 offset, u32 size, int origin)
315{
316#ifdef CONFIG_UNIXWARE_DISKLABEL
317 Sector sect;
318 struct unixware_disklabel *l;
319 struct unixware_slice *p;
320
321 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, &sect);
322 if (!l)
323 return;
324 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
325 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
326 put_dev_sector(sect);
327 return;
328 }
329 printk(" %s%d: <unixware:", state->name, origin);
330 p = &l->vtoc.v_slice[1];
331 /* I omit the 0th slice as it is the same as whole disk. */
332 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
333 if (state->next == state->limit)
334 break;
335
336 if (p->s_label != UNIXWARE_FS_UNUSED)
337 put_partition(state, state->next++,
338 START_SECT(p), NR_SECTS(p));
339 p++;
340 }
341 put_dev_sector(sect);
342 printk(" >\n");
343#endif
344}
345
346/*
347 * Minix 2.0.0/2.0.2 subpartition support.
348 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
349 * Rajeev V. Pillai <rajeevvp@yahoo.com>
350 */
351static void
352parse_minix(struct parsed_partitions *state, struct block_device *bdev,
353 u32 offset, u32 size, int origin)
354{
355#ifdef CONFIG_MINIX_SUBPARTITION
356 Sector sect;
357 unsigned char *data;
358 struct partition *p;
359 int i;
360
361 data = read_dev_sector(bdev, offset, &sect);
362 if (!data)
363 return;
364
365 p = (struct partition *)(data + 0x1be);
366
367 /* The first sector of a Minix partition can have either
368 * a secondary MBR describing its subpartitions, or
369 * the normal boot sector. */
370 if (msdos_magic_present (data + 510) &&
371 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
372
373 printk(" %s%d: <minix:", state->name, origin);
374 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
375 if (state->next == state->limit)
376 break;
377 /* add each partition in use */
378 if (SYS_IND(p) == MINIX_PARTITION)
379 put_partition(state, state->next++,
380 START_SECT(p), NR_SECTS(p));
381 }
382 printk(" >\n");
383 }
384 put_dev_sector(sect);
385#endif /* CONFIG_MINIX_SUBPARTITION */
386}
387
388static struct {
389 unsigned char id;
390 void (*parse)(struct parsed_partitions *, struct block_device *,
391 u32, u32, int);
392} subtypes[] = {
393 {FREEBSD_PARTITION, parse_freebsd},
394 {NETBSD_PARTITION, parse_netbsd},
395 {OPENBSD_PARTITION, parse_openbsd},
396 {MINIX_PARTITION, parse_minix},
397 {UNIXWARE_PARTITION, parse_unixware},
398 {SOLARIS_X86_PARTITION, parse_solaris_x86},
399 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
400 {0, NULL},
401};
402
403int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
404{
405 int sector_size = bdev_hardsect_size(bdev) / 512;
406 Sector sect;
407 unsigned char *data;
408 struct partition *p;
409 int slot;
410
411 data = read_dev_sector(bdev, 0, &sect);
412 if (!data)
413 return -1;
414 if (!msdos_magic_present(data + 510)) {
415 put_dev_sector(sect);
416 return 0;
417 }
418
Olaf Heringe1dfa922006-09-29 01:59:39 -0700419 if (aix_magic_present(data, bdev)) {
420 put_dev_sector(sect);
421 printk( " [AIX]");
422 return 0;
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /*
426 * Now that the 55aa signature is present, this is probably
427 * either the boot sector of a FAT filesystem or a DOS-type
428 * partition table. Reject this in case the boot indicator
429 * is not 0 or 0x80.
430 */
431 p = (struct partition *) (data + 0x1be);
432 for (slot = 1; slot <= 4; slot++, p++) {
433 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
434 put_dev_sector(sect);
435 return 0;
436 }
437 }
438
439#ifdef CONFIG_EFI_PARTITION
440 p = (struct partition *) (data + 0x1be);
441 for (slot = 1 ; slot <= 4 ; slot++, p++) {
442 /* If this is an EFI GPT disk, msdos should ignore it. */
443 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
444 put_dev_sector(sect);
445 return 0;
446 }
447 }
448#endif
449 p = (struct partition *) (data + 0x1be);
450
451 /*
452 * Look for partitions in two passes:
453 * First find the primary and DOS-type extended partitions.
454 * On the second pass look inside *BSD, Unixware and Solaris partitions.
455 */
456
457 state->next = 5;
458 for (slot = 1 ; slot <= 4 ; slot++, p++) {
459 u32 start = START_SECT(p)*sector_size;
460 u32 size = NR_SECTS(p)*sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!size)
462 continue;
463 if (is_extended_partition(p)) {
464 /* prevent someone doing mkfs or mkswap on an
465 extended partition, but leave room for LILO */
466 put_partition(state, slot, start, size == 1 ? 1 : 2);
467 printk(" <");
468 parse_extended(state, bdev, start, size);
469 printk(" >");
470 continue;
471 }
472 put_partition(state, slot, start, size);
473 if (SYS_IND(p) == LINUX_RAID_PARTITION)
474 state->parts[slot].flags = 1;
475 if (SYS_IND(p) == DM6_PARTITION)
476 printk("[DM]");
477 if (SYS_IND(p) == EZD_PARTITION)
478 printk("[EZD]");
479 }
480
481 printk("\n");
482
483 /* second pass - output for each on a separate line */
484 p = (struct partition *) (0x1be + data);
485 for (slot = 1 ; slot <= 4 ; slot++, p++) {
486 unsigned char id = SYS_IND(p);
487 int n;
488
489 if (!NR_SECTS(p))
490 continue;
491
492 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
493 ;
494
495 if (!subtypes[n].parse)
496 continue;
497 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
498 NR_SECTS(p)*sector_size, slot);
499 }
500 put_dev_sector(sect);
501 return 1;
502}