2 * fs/partitions/msdos.c
4 * Code extracted from drivers/block/genhd.c
5 * Copyright (C) 1991-1998 Linus Torvalds
7 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
8 * in the early extended-partition checks and added DM partitions
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).
15 * More flexible handling of extended partitions - aeb, 950831
17 * Check partition table on IDE disks for common CHS translations
19 * Re-organised Feb 1998 Russell King
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.
32 #include <asm/unaligned.h>
34 #define SYS_IND(p) (get_unaligned(&p->sys_ind))
35 #define NR_SECTS(p) ({ __typeof__(p->nr_sects) __a = \
36 get_unaligned(&p->nr_sects); \
40 #define START_SECT(p) ({ __typeof__(p->start_sect) __a = \
41 get_unaligned(&p->start_sect); \
45 static inline int is_extended_partition(struct partition *p)
47 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
48 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
49 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
52 #define MSDOS_LABEL_MAGIC1 0x55
53 #define MSDOS_LABEL_MAGIC2 0xAA
56 msdos_magic_present(unsigned char *p)
58 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
61 /* Value is EBCDIC 'IBMA' */
62 #define AIX_LABEL_MAGIC1 0xC9
63 #define AIX_LABEL_MAGIC2 0xC2
64 #define AIX_LABEL_MAGIC3 0xD4
65 #define AIX_LABEL_MAGIC4 0xC1
66 static int aix_magic_present(unsigned char *p, struct block_device *bdev)
72 if (p[0] != AIX_LABEL_MAGIC1 &&
73 p[1] != AIX_LABEL_MAGIC2 &&
74 p[2] != AIX_LABEL_MAGIC3 &&
75 p[3] != AIX_LABEL_MAGIC4)
77 d = read_dev_sector(bdev, 7, §);
79 if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
87 * Create devices for each logical partition in an extended partition.
88 * The logical partitions form a linked list, with each entry being
89 * a partition table with two entries. The first entry
90 * is the real data partition (with a start relative to the partition
91 * table start). The second is a pointer to the next logical partition
92 * (with a start relative to the entire extended partition).
93 * We do not create a Linux partition for the partition tables, but
94 * only for the actual data partitions.
98 parse_extended(struct parsed_partitions *state, struct block_device *bdev,
99 u32 first_sector, u32 first_size)
104 u32 this_sector, this_size;
105 int sector_size = bdev_hardsect_size(bdev) / 512;
106 int loopct = 0; /* number of links followed
107 without finding a data partition */
110 this_sector = first_sector;
111 this_size = first_size;
116 if (state->next == state->limit)
118 data = read_dev_sector(bdev, this_sector, §);
122 if (!msdos_magic_present(data + 510))
125 p = (struct partition *) (data + 0x1be);
128 * Usually, the first entry is the real data partition,
129 * the 2nd entry is the next extended partition, or empty,
130 * and the 3rd and 4th entries are unused.
131 * However, DRDOS sometimes has the extended partition as
132 * the first entry (when the data partition is empty),
133 * and OS/2 seems to use all four entries.
137 * First process the data partition(s)
139 for (i=0; i<4; i++, p++) {
140 u32 offs, size, next;
141 if (!NR_SECTS(p) || is_extended_partition(p))
144 /* Check the 3rd and 4th entries -
145 these sometimes contain random garbage */
146 offs = START_SECT(p)*sector_size;
147 size = NR_SECTS(p)*sector_size;
148 next = this_sector + offs;
150 if (offs + size > this_size)
152 if (next < first_sector)
154 if (next + size > first_sector + first_size)
158 put_partition(state, state->next, next, size);
159 if (SYS_IND(p) == LINUX_RAID_PARTITION)
160 state->parts[state->next].flags = 1;
162 if (++state->next == state->limit)
166 * Next, process the (first) extended partition, if present.
167 * (So far, there seems to be no reason to make
168 * parse_extended() recursive and allow a tree
169 * of extended partitions.)
170 * It should be a link to the next logical partition.
173 for (i=0; i<4; i++, p++)
174 if (NR_SECTS(p) && is_extended_partition(p))
177 goto done; /* nothing left to do */
179 this_sector = first_sector + START_SECT(p) * sector_size;
180 this_size = NR_SECTS(p) * sector_size;
181 put_dev_sector(sect);
184 put_dev_sector(sect);
187 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
188 indicates linux swap. Be careful before believing this is Solaris. */
191 parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
192 u32 offset, u32 size, int origin)
194 #ifdef CONFIG_SOLARIS_X86_PARTITION
196 struct solaris_x86_vtoc *v;
199 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, §);
202 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
203 put_dev_sector(sect);
206 printk(" %s%d: <solaris:", state->name, origin);
207 if (le32_to_cpu(v->v_version) != 1) {
208 printk(" cannot handle version %d vtoc>\n",
209 le32_to_cpu(v->v_version));
210 put_dev_sector(sect);
213 for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
214 struct solaris_x86_slice *s = &v->v_slice[i];
218 /* solaris partitions are relative to current MS-DOS
219 * one; must add the offset of the current partition */
220 put_partition(state, state->next++,
221 le32_to_cpu(s->s_start)+offset,
222 le32_to_cpu(s->s_size));
224 put_dev_sector(sect);
229 #if defined(CONFIG_BSD_DISKLABEL)
231 * Create devices for BSD partitions listed in a disklabel, under a
232 * dos-like partition. See parse_extended() for more information.
235 parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
236 u32 offset, u32 size, int origin, char *flavour,
240 struct bsd_disklabel *l;
241 struct bsd_partition *p;
243 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, §);
246 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
247 put_dev_sector(sect);
250 printk(" %s%d: <%s:", state->name, origin, flavour);
252 if (le16_to_cpu(l->d_npartitions) < max_partitions)
253 max_partitions = le16_to_cpu(l->d_npartitions);
254 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
255 u32 bsd_start, bsd_size;
257 if (state->next == state->limit)
259 if (p->p_fstype == BSD_FS_UNUSED)
261 bsd_start = le32_to_cpu(p->p_offset);
262 bsd_size = le32_to_cpu(p->p_size);
263 if (offset == bsd_start && size == bsd_size)
264 /* full parent partition, we have it already */
266 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
267 printk("bad subpartition - ignored\n");
270 put_partition(state, state->next++, bsd_start, bsd_size);
272 put_dev_sector(sect);
273 if (le16_to_cpu(l->d_npartitions) > max_partitions)
274 printk(" (ignored %d more)",
275 le16_to_cpu(l->d_npartitions) - max_partitions);
281 parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
282 u32 offset, u32 size, int origin)
284 #ifdef CONFIG_BSD_DISKLABEL
285 parse_bsd(state, bdev, offset, size, origin,
286 "bsd", BSD_MAXPARTITIONS);
291 parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
292 u32 offset, u32 size, int origin)
294 #ifdef CONFIG_BSD_DISKLABEL
295 parse_bsd(state, bdev, offset, size, origin,
296 "netbsd", BSD_MAXPARTITIONS);
301 parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
302 u32 offset, u32 size, int origin)
304 #ifdef CONFIG_BSD_DISKLABEL
305 parse_bsd(state, bdev, offset, size, origin,
306 "openbsd", OPENBSD_MAXPARTITIONS);
311 * Create devices for Unixware partitions listed in a disklabel, under a
312 * dos-like partition. See parse_extended() for more information.
315 parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
316 u32 offset, u32 size, int origin)
318 #ifdef CONFIG_UNIXWARE_DISKLABEL
320 struct unixware_disklabel *l;
321 struct unixware_slice *p;
323 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, §);
326 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
327 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
328 put_dev_sector(sect);
331 printk(" %s%d: <unixware:", state->name, origin);
332 p = &l->vtoc.v_slice[1];
333 /* I omit the 0th slice as it is the same as whole disk. */
334 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
335 if (state->next == state->limit)
338 if (p->s_label != UNIXWARE_FS_UNUSED)
339 put_partition(state, state->next++,
340 START_SECT(p), NR_SECTS(p));
343 put_dev_sector(sect);
349 * Minix 2.0.0/2.0.2 subpartition support.
350 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
351 * Rajeev V. Pillai <rajeevvp@yahoo.com>
354 parse_minix(struct parsed_partitions *state, struct block_device *bdev,
355 u32 offset, u32 size, int origin)
357 #ifdef CONFIG_MINIX_SUBPARTITION
363 data = read_dev_sector(bdev, offset, §);
367 p = (struct partition *)(data + 0x1be);
369 /* The first sector of a Minix partition can have either
370 * a secondary MBR describing its subpartitions, or
371 * the normal boot sector. */
372 if (msdos_magic_present (data + 510) &&
373 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
375 printk(" %s%d: <minix:", state->name, origin);
376 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
377 if (state->next == state->limit)
379 /* add each partition in use */
380 if (SYS_IND(p) == MINIX_PARTITION)
381 put_partition(state, state->next++,
382 START_SECT(p), NR_SECTS(p));
386 put_dev_sector(sect);
387 #endif /* CONFIG_MINIX_SUBPARTITION */
392 void (*parse)(struct parsed_partitions *, struct block_device *,
395 {FREEBSD_PARTITION, parse_freebsd},
396 {NETBSD_PARTITION, parse_netbsd},
397 {OPENBSD_PARTITION, parse_openbsd},
398 {MINIX_PARTITION, parse_minix},
399 {UNIXWARE_PARTITION, parse_unixware},
400 {SOLARIS_X86_PARTITION, parse_solaris_x86},
401 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
405 int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
407 int sector_size = bdev_hardsect_size(bdev) / 512;
413 data = read_dev_sector(bdev, 0, §);
416 if (!msdos_magic_present(data + 510)) {
417 put_dev_sector(sect);
421 if (aix_magic_present(data, bdev)) {
422 put_dev_sector(sect);
428 * Now that the 55aa signature is present, this is probably
429 * either the boot sector of a FAT filesystem or a DOS-type
430 * partition table. Reject this in case the boot indicator
433 p = (struct partition *) (data + 0x1be);
434 for (slot = 1; slot <= 4; slot++, p++) {
435 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
436 put_dev_sector(sect);
441 #ifdef CONFIG_EFI_PARTITION
442 p = (struct partition *) (data + 0x1be);
443 for (slot = 1 ; slot <= 4 ; slot++, p++) {
444 /* If this is an EFI GPT disk, msdos should ignore it. */
445 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
446 put_dev_sector(sect);
451 p = (struct partition *) (data + 0x1be);
454 * Look for partitions in two passes:
455 * First find the primary and DOS-type extended partitions.
456 * On the second pass look inside *BSD, Unixware and Solaris partitions.
460 for (slot = 1 ; slot <= 4 ; slot++, p++) {
461 u32 start = START_SECT(p)*sector_size;
462 u32 size = NR_SECTS(p)*sector_size;
465 if (is_extended_partition(p)) {
466 /* prevent someone doing mkfs or mkswap on an
467 extended partition, but leave room for LILO */
468 put_partition(state, slot, start, size == 1 ? 1 : 2);
470 parse_extended(state, bdev, start, size);
474 put_partition(state, slot, start, size);
475 if (SYS_IND(p) == LINUX_RAID_PARTITION)
476 state->parts[slot].flags = 1;
477 if (SYS_IND(p) == DM6_PARTITION)
479 if (SYS_IND(p) == EZD_PARTITION)
485 /* second pass - output for each on a separate line */
486 p = (struct partition *) (0x1be + data);
487 for (slot = 1 ; slot <= 4 ; slot++, p++) {
488 unsigned char id = SYS_IND(p);
494 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
497 if (!subtypes[n].parse)
499 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
500 NR_SECTS(p)*sector_size, slot);
502 put_dev_sector(sect);