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
22 #include <linux/config.h>
29 * Many architectures don't like unaligned accesses, while
30 * the nr_sects and start_sect partition table entries are
31 * at a 2 (mod 4) address.
33 #include <asm/unaligned.h>
35 #define SYS_IND(p) (get_unaligned(&p->sys_ind))
36 #define NR_SECTS(p) ({ __typeof__(p->nr_sects) __a = \
37 get_unaligned(&p->nr_sects); \
41 #define START_SECT(p) ({ __typeof__(p->start_sect) __a = \
42 get_unaligned(&p->start_sect); \
46 static inline int is_extended_partition(struct partition *p)
48 return (SYS_IND(p) == DOS_EXTENDED_PARTITION ||
49 SYS_IND(p) == WIN98_EXTENDED_PARTITION ||
50 SYS_IND(p) == LINUX_EXTENDED_PARTITION);
53 #define MSDOS_LABEL_MAGIC1 0x55
54 #define MSDOS_LABEL_MAGIC2 0xAA
57 msdos_magic_present(unsigned char *p)
59 return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
63 * Create devices for each logical partition in an extended partition.
64 * The logical partitions form a linked list, with each entry being
65 * a partition table with two entries. The first entry
66 * is the real data partition (with a start relative to the partition
67 * table start). The second is a pointer to the next logical partition
68 * (with a start relative to the entire extended partition).
69 * We do not create a Linux partition for the partition tables, but
70 * only for the actual data partitions.
74 parse_extended(struct parsed_partitions *state, struct block_device *bdev,
75 u32 first_sector, u32 first_size)
80 u32 this_sector, this_size;
81 int sector_size = bdev_hardsect_size(bdev) / 512;
82 int loopct = 0; /* number of links followed
83 without finding a data partition */
86 this_sector = first_sector;
87 this_size = first_size;
92 if (state->next == state->limit)
94 data = read_dev_sector(bdev, this_sector, §);
98 if (!msdos_magic_present(data + 510))
101 p = (struct partition *) (data + 0x1be);
104 * Usually, the first entry is the real data partition,
105 * the 2nd entry is the next extended partition, or empty,
106 * and the 3rd and 4th entries are unused.
107 * However, DRDOS sometimes has the extended partition as
108 * the first entry (when the data partition is empty),
109 * and OS/2 seems to use all four entries.
113 * First process the data partition(s)
115 for (i=0; i<4; i++, p++) {
116 u32 offs, size, next;
117 if (!NR_SECTS(p) || is_extended_partition(p))
120 /* Check the 3rd and 4th entries -
121 these sometimes contain random garbage */
122 offs = START_SECT(p)*sector_size;
123 size = NR_SECTS(p)*sector_size;
124 next = this_sector + offs;
126 if (offs + size > this_size)
128 if (next < first_sector)
130 if (next + size > first_sector + first_size)
134 put_partition(state, state->next, next, size);
135 if (SYS_IND(p) == LINUX_RAID_PARTITION)
136 state->parts[state->next].flags = 1;
138 if (++state->next == state->limit)
142 * Next, process the (first) extended partition, if present.
143 * (So far, there seems to be no reason to make
144 * parse_extended() recursive and allow a tree
145 * of extended partitions.)
146 * It should be a link to the next logical partition.
149 for (i=0; i<4; i++, p++)
150 if (NR_SECTS(p) && is_extended_partition(p))
153 goto done; /* nothing left to do */
155 this_sector = first_sector + START_SECT(p) * sector_size;
156 this_size = NR_SECTS(p) * sector_size;
157 put_dev_sector(sect);
160 put_dev_sector(sect);
163 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
164 indicates linux swap. Be careful before believing this is Solaris. */
167 parse_solaris_x86(struct parsed_partitions *state, struct block_device *bdev,
168 u32 offset, u32 size, int origin)
170 #ifdef CONFIG_SOLARIS_X86_PARTITION
172 struct solaris_x86_vtoc *v;
175 v = (struct solaris_x86_vtoc *)read_dev_sector(bdev, offset+1, §);
178 if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
179 put_dev_sector(sect);
182 printk(" %s%d: <solaris:", state->name, origin);
183 if (le32_to_cpu(v->v_version) != 1) {
184 printk(" cannot handle version %d vtoc>\n",
185 le32_to_cpu(v->v_version));
186 put_dev_sector(sect);
189 for (i=0; i<SOLARIS_X86_NUMSLICE && state->next<state->limit; i++) {
190 struct solaris_x86_slice *s = &v->v_slice[i];
194 /* solaris partitions are relative to current MS-DOS
195 * one; must add the offset of the current partition */
196 put_partition(state, state->next++,
197 le32_to_cpu(s->s_start)+offset,
198 le32_to_cpu(s->s_size));
200 put_dev_sector(sect);
205 #if defined(CONFIG_BSD_DISKLABEL) || defined(CONFIG_NEC98_PARTITION)
207 * Create devices for BSD partitions listed in a disklabel, under a
208 * dos-like partition. See parse_extended() for more information.
211 parse_bsd(struct parsed_partitions *state, struct block_device *bdev,
212 u32 offset, u32 size, int origin, char *flavour,
216 struct bsd_disklabel *l;
217 struct bsd_partition *p;
219 l = (struct bsd_disklabel *)read_dev_sector(bdev, offset+1, §);
222 if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
223 put_dev_sector(sect);
226 printk(" %s%d: <%s:", state->name, origin, flavour);
228 if (le16_to_cpu(l->d_npartitions) < max_partitions)
229 max_partitions = le16_to_cpu(l->d_npartitions);
230 for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
231 u32 bsd_start, bsd_size;
233 if (state->next == state->limit)
235 if (p->p_fstype == BSD_FS_UNUSED)
237 bsd_start = le32_to_cpu(p->p_offset);
238 bsd_size = le32_to_cpu(p->p_size);
239 if (offset == bsd_start && size == bsd_size)
240 /* full parent partition, we have it already */
242 if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
243 printk("bad subpartition - ignored\n");
246 put_partition(state, state->next++, bsd_start, bsd_size);
248 put_dev_sector(sect);
254 parse_freebsd(struct parsed_partitions *state, struct block_device *bdev,
255 u32 offset, u32 size, int origin)
257 #ifdef CONFIG_BSD_DISKLABEL
258 parse_bsd(state, bdev, offset, size, origin,
259 "bsd", BSD_MAXPARTITIONS);
264 parse_netbsd(struct parsed_partitions *state, struct block_device *bdev,
265 u32 offset, u32 size, int origin)
267 #ifdef CONFIG_BSD_DISKLABEL
268 parse_bsd(state, bdev, offset, size, origin,
269 "netbsd", BSD_MAXPARTITIONS);
274 parse_openbsd(struct parsed_partitions *state, struct block_device *bdev,
275 u32 offset, u32 size, int origin)
277 #ifdef CONFIG_BSD_DISKLABEL
278 parse_bsd(state, bdev, offset, size, origin,
279 "openbsd", OPENBSD_MAXPARTITIONS);
284 * Create devices for Unixware partitions listed in a disklabel, under a
285 * dos-like partition. See parse_extended() for more information.
288 parse_unixware(struct parsed_partitions *state, struct block_device *bdev,
289 u32 offset, u32 size, int origin)
291 #ifdef CONFIG_UNIXWARE_DISKLABEL
293 struct unixware_disklabel *l;
294 struct unixware_slice *p;
296 l = (struct unixware_disklabel *)read_dev_sector(bdev, offset+29, §);
299 if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
300 le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
301 put_dev_sector(sect);
304 printk(" %s%d: <unixware:", state->name, origin);
305 p = &l->vtoc.v_slice[1];
306 /* I omit the 0th slice as it is the same as whole disk. */
307 while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
308 if (state->next == state->limit)
311 if (p->s_label != UNIXWARE_FS_UNUSED)
312 put_partition(state, state->next++,
313 START_SECT(p), NR_SECTS(p));
316 put_dev_sector(sect);
322 * Minix 2.0.0/2.0.2 subpartition support.
323 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
324 * Rajeev V. Pillai <rajeevvp@yahoo.com>
327 parse_minix(struct parsed_partitions *state, struct block_device *bdev,
328 u32 offset, u32 size, int origin)
330 #ifdef CONFIG_MINIX_SUBPARTITION
336 data = read_dev_sector(bdev, offset, §);
340 p = (struct partition *)(data + 0x1be);
342 /* The first sector of a Minix partition can have either
343 * a secondary MBR describing its subpartitions, or
344 * the normal boot sector. */
345 if (msdos_magic_present (data + 510) &&
346 SYS_IND(p) == MINIX_PARTITION) { /* subpartition table present */
348 printk(" %s%d: <minix:", state->name, origin);
349 for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
350 if (state->next == state->limit)
352 /* add each partition in use */
353 if (SYS_IND(p) == MINIX_PARTITION)
354 put_partition(state, state->next++,
355 START_SECT(p), NR_SECTS(p));
359 put_dev_sector(sect);
360 #endif /* CONFIG_MINIX_SUBPARTITION */
365 void (*parse)(struct parsed_partitions *, struct block_device *,
368 {FREEBSD_PARTITION, parse_freebsd},
369 {NETBSD_PARTITION, parse_netbsd},
370 {OPENBSD_PARTITION, parse_openbsd},
371 {MINIX_PARTITION, parse_minix},
372 {UNIXWARE_PARTITION, parse_unixware},
373 {SOLARIS_X86_PARTITION, parse_solaris_x86},
377 int msdos_partition(struct parsed_partitions *state, struct block_device *bdev)
379 int sector_size = bdev_hardsect_size(bdev) / 512;
385 data = read_dev_sector(bdev, 0, §);
388 if (!msdos_magic_present(data + 510)) {
389 put_dev_sector(sect);
392 p = (struct partition *) (data + 0x1be);
393 #ifdef CONFIG_EFI_PARTITION
394 for (slot = 1 ; slot <= 4 ; slot++, p++) {
395 /* If this is an EFI GPT disk, msdos should ignore it. */
396 if (SYS_IND(p) == EFI_PMBR_OSTYPE_EFI_GPT) {
397 put_dev_sector(sect);
401 p = (struct partition *) (data + 0x1be);
405 * Look for partitions in two passes:
406 * First find the primary and DOS-type extended partitions.
407 * On the second pass look inside *BSD, Unixware and Solaris partitions.
411 for (slot = 1 ; slot <= 4 ; slot++, p++) {
412 u32 start = START_SECT(p)*sector_size;
413 u32 size = NR_SECTS(p)*sector_size;
416 if (is_extended_partition(p)) {
417 /* prevent someone doing mkfs or mkswap on an
418 extended partition, but leave room for LILO */
419 put_partition(state, slot, start, size == 1 ? 1 : 2);
421 parse_extended(state, bdev, start, size);
425 put_partition(state, slot, start, size);
426 if (SYS_IND(p) == LINUX_RAID_PARTITION)
427 state->parts[slot].flags = 1;
428 if (SYS_IND(p) == DM6_PARTITION)
430 if (SYS_IND(p) == EZD_PARTITION)
436 /* second pass - output for each on a separate line */
437 p = (struct partition *) (0x1be + data);
438 for (slot = 1 ; slot <= 4 ; slot++, p++) {
439 unsigned char id = SYS_IND(p);
445 for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
448 if (!subtypes[n].parse)
450 subtypes[n].parse(state, bdev, START_SECT(p)*sector_size,
451 NR_SECTS(p)*sector_size, slot);
453 put_dev_sector(sect);