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);
62 * Create devices for each logical partition in an extended partition.
63 * The logical partitions form a linked list, with each entry being
64 * a partition table with two entries. The first entry
65 * is the real data partition (with a start relative to the partition
66 * table start). The second is a pointer to the next logical partition
67 * (with a start relative to the entire extended partition).
68 * We do not create a Linux partition for the partition tables, but
69 * only for the actual data partitions.
73 parse_extended(struct parsed_partitions *state, struct block_device *bdev,
74 u32 first_sector, u32 first_size)
79 u32 this_sector, this_size;
80 int sector_size = bdev_hardsect_size(bdev) / 512;
81 int loopct = 0; /* number of links followed
82 without finding a data partition */
85 this_sector = first_sector;
86 this_size = first_size;
91 if (state->next == state->limit)
93 data = read_dev_sector(bdev, this_sector, §);
97 if (!msdos_magic_present(data + 510))
100 p = (struct partition *) (data + 0x1be);
103 * Usually, the first entry is the real data partition,
104 * the 2nd entry is the next extended partition, or empty,
105 * and the 3rd and 4th entries are unused.
106 * However, DRDOS sometimes has the extended partition as
107 * the first entry (when the data partition is empty),
108 * and OS/2 seems to use all four entries.
112 * First process the data partition(s)
114 for (i=0; i<4; i++, p++) {
115 u32 offs, size, next;
116 if (!NR_SECTS(p) || is_extended_partition(p))
119 /* Check the 3rd and 4th entries -
120 these sometimes contain random garbage */
121 offs = START_SECT(p)*sector_size;
122 size = NR_SECTS(p)*sector_size;
123 next = this_sector + offs;
125 if (offs + size > this_size)
127 if (next < first_sector)
129 if (next + size > first_sector + first_size)
133 put_partition(state, state->next, next, size);
134 if (SYS_IND(p) == LINUX_RAID_PARTITION)
135 state->parts[state->next].flags = 1;
137 if (++state->next == state->limit)
141 * Next, process the (first) extended partition, if present.
142 * (So far, there seems to be no reason to make
143 * parse_extended() recursive and allow a tree
144 * of extended partitions.)
145 * It should be a link to the next logical partition.
148 for (i=0; i<4; i++, p++)
149 if (NR_SECTS(p) && is_extended_partition(p))
152 goto done; /* nothing left to do */
154 this_sector = first_sector + START_SECT(p) * sector_size;
155 this_size = NR_SECTS(p) * sector_size;
156 put_dev_sector(sect);
159 put_dev_sector(sect);
162 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
163 indicates linux swap. Be careful before believing this is Solaris. */
166 parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
167 u32 offset, u32 size, int origin)
169 #ifdef CONFIG_SOLARIS_X86_PARTITION
171 struct solaris_x86_vtoc *v;
174 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, §);
177 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
178 put_dev_sector(sect);
181 printk(" %s%d: <solaris:", state->name, origin);
182 if (le32_to_cpu(v->v_version) != 1) {
183 printk(" cannot handle version %d vtoc>\n",
184 le32_to_cpu(v->v_version));
185 put_dev_sector(sect);
188 for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
189 struct solaris_x86_slice *s = &v->v_slice[i];
193 /* solaris partitions are relative to current MS-DOS
194 * one; must add the offset of the current partition */
195 put_partition(state, state->next++,
196 le32_to_cpu(s->s_start)+offset,
197 le32_to_cpu(s->s_size));
199 put_dev_sector(sect);
204 #if defined(CONFIG_BSD_DISKLABEL)
206 * Create devices for BSD partitions listed in a disklabel, under a
207 * dos-like partition. See parse_extended() for more information.
210 parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
211 u32 offset, u32 size, int origin, char *flavour,
215 struct bsd_disklabel *l;
216 struct bsd_partition *p;
218 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, §);
221 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
222 put_dev_sector(sect);
225 printk(" %s%d: <%s:", state->name, origin, flavour);
227 if (le16_to_cpu(l->d_npartitions) < max_partitions)
228 max_partitions = le16_to_cpu(l->d_npartitions);
229 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
230 u32 bsd_start, bsd_size;
232 if (state->next == state->limit)
234 if (p->p_fstype == BSD_FS_UNUSED)
236 bsd_start = le32_to_cpu(p->p_offset);
237 bsd_size = le32_to_cpu(p->p_size);
238 if (offset == bsd_start && size == bsd_size)
239 /* full parent partition, we have it already */
241 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
242 printk("bad subpartition - ignored\n");
245 put_partition(state, state->next++, bsd_start, bsd_size);
247 put_dev_sector(sect);
248 if (le16_to_cpu(l->d_npartitions) > max_partitions)
249 printk(" (ignored %d more)",
250 le16_to_cpu(l->d_npartitions) - max_partitions);
256 parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
257 u32 offset, u32 size, int origin)
259 #ifdef CONFIG_BSD_DISKLABEL
260 parse_bsd(state, bdev, offset, size, origin,
261 "bsd", BSD_MAXPARTITIONS);
266 parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
267 u32 offset, u32 size, int origin)
269 #ifdef CONFIG_BSD_DISKLABEL
270 parse_bsd(state, bdev, offset, size, origin,
271 "netbsd", BSD_MAXPARTITIONS);
276 parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
277 u32 offset, u32 size, int origin)
279 #ifdef CONFIG_BSD_DISKLABEL
280 parse_bsd(state, bdev, offset, size, origin,
281 "openbsd", OPENBSD_MAXPARTITIONS);
286 * Create devices for Unixware partitions listed in a disklabel, under a
287 * dos-like partition. See parse_extended() for more information.
290 parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
291 u32 offset, u32 size, int origin)
293 #ifdef CONFIG_UNIXWARE_DISKLABEL
295 struct unixware_disklabel *l;
296 struct unixware_slice *p;
298 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, §);
301 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
302 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
303 put_dev_sector(sect);
306 printk(" %s%d: <unixware:", state->name, origin);
307 p = &l->vtoc.v_slice[1];
308 /* I omit the 0th slice as it is the same as whole disk. */
309 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
310 if (state->next == state->limit)
313 if (p->s_label != UNIXWARE_FS_UNUSED)
314 put_partition(state, state->next++,
315 START_SECT(p), NR_SECTS(p));
318 put_dev_sector(sect);
324 * Minix 2.0.0/2.0.2 subpartition support.
325 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
326 * Rajeev V. Pillai <rajeevvp@yahoo.com>
329 parse_minix(struct parsed_partitions *state, struct block_device *bdev,
330 u32 offset, u32 size, int origin)
332 #ifdef CONFIG_MINIX_SUBPARTITION
338 data = read_dev_sector(bdev, offset, §);
342 p = (struct partition *)(data + 0x1be);
344 /* The first sector of a Minix partition can have either
345 * a secondary MBR describing its subpartitions, or
346 * the normal boot sector. */
347 if (msdos_magic_present (data + 510) &&
348 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
350 printk(" %s%d: <minix:", state->name, origin);
351 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
352 if (state->next == state->limit)
354 /* add each partition in use */
355 if (SYS_IND(p) == MINIX_PARTITION)
356 put_partition(state, state->next++,
357 START_SECT(p), NR_SECTS(p));
361 put_dev_sector(sect);
362 #endif /* CONFIG_MINIX_SUBPARTITION */
367 void (*parse)(struct parsed_partitions *, struct block_device *,
370 {FREEBSD_PARTITION, parse_freebsd},
371 {NETBSD_PARTITION, parse_netbsd},
372 {OPENBSD_PARTITION, parse_openbsd},
373 {MINIX_PARTITION, parse_minix},
374 {UNIXWARE_PARTITION, parse_unixware},
375 {SOLARIS_X86_PARTITION, parse_solaris_x86},
376 {NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
380 int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
382 int sector_size = bdev_hardsect_size(bdev) / 512;
388 data = read_dev_sector(bdev, 0, §);
391 if (!msdos_magic_present(data + 510)) {
392 put_dev_sector(sect);
397 * Now that the 55aa signature is present, this is probably
398 * either the boot sector of a FAT filesystem or a DOS-type
399 * partition table. Reject this in case the boot indicator
402 p = (struct partition *) (data + 0x1be);
403 for (slot = 1; slot <= 4; slot++, p++) {
404 if (p->boot_ind != 0 && p->boot_ind != 0x80) {
405 put_dev_sector(sect);
410 #ifdef CONFIG_EFI_PARTITION
411 p = (struct partition *) (data + 0x1be);
412 for (slot = 1 ; slot <= 4 ; slot++, p++) {
413 /* If this is an EFI GPT disk, msdos should ignore it. */
414 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
415 put_dev_sector(sect);
420 p = (struct partition *) (data + 0x1be);
423 * Look for partitions in two passes:
424 * First find the primary and DOS-type extended partitions.
425 * On the second pass look inside *BSD, Unixware and Solaris partitions.
429 for (slot = 1 ; slot <= 4 ; slot++, p++) {
430 u32 start = START_SECT(p)*sector_size;
431 u32 size = NR_SECTS(p)*sector_size;
434 if (is_extended_partition(p)) {
435 /* prevent someone doing mkfs or mkswap on an
436 extended partition, but leave room for LILO */
437 put_partition(state, slot, start, size == 1 ? 1 : 2);
439 parse_extended(state, bdev, start, size);
443 put_partition(state, slot, start, size);
444 if (SYS_IND(p) == LINUX_RAID_PARTITION)
445 state->parts[slot].flags = 1;
446 if (SYS_IND(p) == DM6_PARTITION)
448 if (SYS_IND(p) == EZD_PARTITION)
454 /* second pass - output for each on a separate line */
455 p = (struct partition *) (0x1be + data);
456 for (slot = 1 ; slot <= 4 ; slot++, p++) {
457 unsigned char id = SYS_IND(p);
463 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
466 if (!subtypes[n].parse)
468 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
469 NR_SECTS(p)*sector_size, slot);
471 put_dev_sector(sect);