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) ({ __le32 __a = get_unaligned(&p->nr_sects); \
39 #define START_SECT(p) ({ __le32 __a = get_unaligned(&p->start_sect); \
43 static inline int is_extended_partition(struct partition
*p
)
45 return (SYS_IND(p
) == DOS_EXTENDED_PARTITION
||
46 SYS_IND(p
) == WIN98_EXTENDED_PARTITION
||
47 SYS_IND(p
) == LINUX_EXTENDED_PARTITION
);
50 #define MSDOS_LABEL_MAGIC1 0x55
51 #define MSDOS_LABEL_MAGIC2 0xAA
54 msdos_magic_present(unsigned char *p
)
56 return (p
[0] == MSDOS_LABEL_MAGIC1
&& p
[1] == MSDOS_LABEL_MAGIC2
);
59 /* 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
64 static int aix_magic_present(unsigned char *p
, struct block_device
*bdev
)
66 struct partition
*pt
= (struct partition
*) (p
+ 0x1be);
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
))
76 /* 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
))
85 d
= read_dev_sector(bdev
, 7, §
);
87 if (d
[0] == '_' && d
[1] == 'L' && d
[2] == 'V' && d
[3] == 'M')
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.
106 parse_extended(struct parsed_partitions
*state
, struct block_device
*bdev
,
107 u32 first_sector
, u32 first_size
)
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 */
118 this_sector
= first_sector
;
119 this_size
= first_size
;
124 if (state
->next
== state
->limit
)
126 data
= read_dev_sector(bdev
, this_sector
, §
);
130 if (!msdos_magic_present(data
+ 510))
133 p
= (struct partition
*) (data
+ 0x1be);
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.
145 * First process the data partition(s)
147 for (i
=0; i
<4; i
++, p
++) {
148 u32 offs
, size
, next
;
149 if (!NR_SECTS(p
) || is_extended_partition(p
))
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
;
158 if (offs
+ size
> this_size
)
160 if (next
< first_sector
)
162 if (next
+ size
> first_sector
+ first_size
)
166 put_partition(state
, state
->next
, next
, size
);
167 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
168 state
->parts
[state
->next
].flags
= ADDPART_FLAG_RAID
;
170 if (++state
->next
== state
->limit
)
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.
181 for (i
=0; i
<4; i
++, p
++)
182 if (NR_SECTS(p
) && is_extended_partition(p
))
185 goto done
; /* nothing left to do */
187 this_sector
= first_sector
+ START_SECT(p
) * sector_size
;
188 this_size
= NR_SECTS(p
) * sector_size
;
189 put_dev_sector(sect
);
192 put_dev_sector(sect
);
195 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
196 indicates linux swap. Be careful before believing this is Solaris. */
199 parse_solaris_x86(struct parsed_partitions
*state
, struct block_device
*bdev
,
200 u32 offset
, u32 size
, int origin
)
202 #ifdef CONFIG_SOLARIS_X86_PARTITION
204 struct solaris_x86_vtoc
*v
;
208 v
= (struct solaris_x86_vtoc
*)read_dev_sector(bdev
, offset
+1, §
);
211 if (le32_to_cpu(v
->v_sanity
) != SOLARIS_X86_VTOC_SANE
) {
212 put_dev_sector(sect
);
215 printk(" %s%d: <solaris:", state
->name
, origin
);
216 if (le32_to_cpu(v
->v_version
) != 1) {
217 printk(" cannot handle version %d vtoc>\n",
218 le32_to_cpu(v
->v_version
));
219 put_dev_sector(sect
);
222 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
223 max_nparts
= le16_to_cpu (v
->v_nparts
) > 8 ? SOLARIS_X86_NUMSLICE
: 8;
224 for (i
=0; i
<max_nparts
&& state
->next
<state
->limit
; i
++) {
225 struct solaris_x86_slice
*s
= &v
->v_slice
[i
];
229 /* solaris partitions are relative to current MS-DOS
230 * one; must add the offset of the current partition */
231 put_partition(state
, state
->next
++,
232 le32_to_cpu(s
->s_start
)+offset
,
233 le32_to_cpu(s
->s_size
));
235 put_dev_sector(sect
);
240 #if defined(CONFIG_BSD_DISKLABEL)
242 * Create devices for BSD partitions listed in a disklabel, under a
243 * dos-like partition. See parse_extended() for more information.
246 parse_bsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
247 u32 offset
, u32 size
, int origin
, char *flavour
,
251 struct bsd_disklabel
*l
;
252 struct bsd_partition
*p
;
254 l
= (struct bsd_disklabel
*)read_dev_sector(bdev
, offset
+1, §
);
257 if (le32_to_cpu(l
->d_magic
) != BSD_DISKMAGIC
) {
258 put_dev_sector(sect
);
261 printk(" %s%d: <%s:", state
->name
, origin
, flavour
);
263 if (le16_to_cpu(l
->d_npartitions
) < max_partitions
)
264 max_partitions
= le16_to_cpu(l
->d_npartitions
);
265 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
266 u32 bsd_start
, bsd_size
;
268 if (state
->next
== state
->limit
)
270 if (p
->p_fstype
== BSD_FS_UNUSED
)
272 bsd_start
= le32_to_cpu(p
->p_offset
);
273 bsd_size
= le32_to_cpu(p
->p_size
);
274 if (offset
== bsd_start
&& size
== bsd_size
)
275 /* full parent partition, we have it already */
277 if (offset
> bsd_start
|| offset
+size
< bsd_start
+bsd_size
) {
278 printk("bad subpartition - ignored\n");
281 put_partition(state
, state
->next
++, bsd_start
, bsd_size
);
283 put_dev_sector(sect
);
284 if (le16_to_cpu(l
->d_npartitions
) > max_partitions
)
285 printk(" (ignored %d more)",
286 le16_to_cpu(l
->d_npartitions
) - max_partitions
);
292 parse_freebsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
293 u32 offset
, u32 size
, int origin
)
295 #ifdef CONFIG_BSD_DISKLABEL
296 parse_bsd(state
, bdev
, offset
, size
, origin
,
297 "bsd", BSD_MAXPARTITIONS
);
302 parse_netbsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
303 u32 offset
, u32 size
, int origin
)
305 #ifdef CONFIG_BSD_DISKLABEL
306 parse_bsd(state
, bdev
, offset
, size
, origin
,
307 "netbsd", BSD_MAXPARTITIONS
);
312 parse_openbsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
313 u32 offset
, u32 size
, int origin
)
315 #ifdef CONFIG_BSD_DISKLABEL
316 parse_bsd(state
, bdev
, offset
, size
, origin
,
317 "openbsd", OPENBSD_MAXPARTITIONS
);
322 * Create devices for Unixware partitions listed in a disklabel, under a
323 * dos-like partition. See parse_extended() for more information.
326 parse_unixware(struct parsed_partitions
*state
, struct block_device
*bdev
,
327 u32 offset
, u32 size
, int origin
)
329 #ifdef CONFIG_UNIXWARE_DISKLABEL
331 struct unixware_disklabel
*l
;
332 struct unixware_slice
*p
;
334 l
= (struct unixware_disklabel
*)read_dev_sector(bdev
, offset
+29, §
);
337 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
338 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
339 put_dev_sector(sect
);
342 printk(" %s%d: <unixware:", state
->name
, origin
);
343 p
= &l
->vtoc
.v_slice
[1];
344 /* I omit the 0th slice as it is the same as whole disk. */
345 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
346 if (state
->next
== state
->limit
)
349 if (p
->s_label
!= UNIXWARE_FS_UNUSED
)
350 put_partition(state
, state
->next
++,
351 START_SECT(p
), NR_SECTS(p
));
354 put_dev_sector(sect
);
360 * Minix 2.0.0/2.0.2 subpartition support.
361 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
362 * Rajeev V. Pillai <rajeevvp@yahoo.com>
365 parse_minix(struct parsed_partitions
*state
, struct block_device
*bdev
,
366 u32 offset
, u32 size
, int origin
)
368 #ifdef CONFIG_MINIX_SUBPARTITION
374 data
= read_dev_sector(bdev
, offset
, §
);
378 p
= (struct partition
*)(data
+ 0x1be);
380 /* The first sector of a Minix partition can have either
381 * a secondary MBR describing its subpartitions, or
382 * the normal boot sector. */
383 if (msdos_magic_present (data
+ 510) &&
384 SYS_IND(p
) == MINIX_PARTITION
) { /* subpartition table present */
386 printk(" %s%d: <minix:", state
->name
, origin
);
387 for (i
= 0; i
< MINIX_NR_SUBPARTITIONS
; i
++, p
++) {
388 if (state
->next
== state
->limit
)
390 /* add each partition in use */
391 if (SYS_IND(p
) == MINIX_PARTITION
)
392 put_partition(state
, state
->next
++,
393 START_SECT(p
), NR_SECTS(p
));
397 put_dev_sector(sect
);
398 #endif /* CONFIG_MINIX_SUBPARTITION */
403 void (*parse
)(struct parsed_partitions
*, struct block_device
*,
406 {FREEBSD_PARTITION
, parse_freebsd
},
407 {NETBSD_PARTITION
, parse_netbsd
},
408 {OPENBSD_PARTITION
, parse_openbsd
},
409 {MINIX_PARTITION
, parse_minix
},
410 {UNIXWARE_PARTITION
, parse_unixware
},
411 {SOLARIS_X86_PARTITION
, parse_solaris_x86
},
412 {NEW_SOLARIS_X86_PARTITION
, parse_solaris_x86
},
416 int msdos_partition(struct parsed_partitions
*state
, struct block_device
*bdev
)
418 int sector_size
= bdev_hardsect_size(bdev
) / 512;
424 data
= read_dev_sector(bdev
, 0, §
);
427 if (!msdos_magic_present(data
+ 510)) {
428 put_dev_sector(sect
);
432 if (aix_magic_present(data
, bdev
)) {
433 put_dev_sector(sect
);
439 * Now that the 55aa signature is present, this is probably
440 * either the boot sector of a FAT filesystem or a DOS-type
441 * partition table. Reject this in case the boot indicator
444 p
= (struct partition
*) (data
+ 0x1be);
445 for (slot
= 1; slot
<= 4; slot
++, p
++) {
446 if (p
->boot_ind
!= 0 && p
->boot_ind
!= 0x80) {
447 put_dev_sector(sect
);
452 #ifdef CONFIG_EFI_PARTITION
453 p
= (struct partition
*) (data
+ 0x1be);
454 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
455 /* If this is an EFI GPT disk, msdos should ignore it. */
456 if (SYS_IND(p
) == EFI_PMBR_OSTYPE_EFI_GPT
) {
457 put_dev_sector(sect
);
462 p
= (struct partition
*) (data
+ 0x1be);
465 * Look for partitions in two passes:
466 * First find the primary and DOS-type extended partitions.
467 * On the second pass look inside *BSD, Unixware and Solaris partitions.
471 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
472 u32 start
= START_SECT(p
)*sector_size
;
473 u32 size
= NR_SECTS(p
)*sector_size
;
476 if (is_extended_partition(p
)) {
477 /* prevent someone doing mkfs or mkswap on an
478 extended partition, but leave room for LILO */
479 put_partition(state
, slot
, start
, size
== 1 ? 1 : 2);
481 parse_extended(state
, bdev
, start
, size
);
485 put_partition(state
, slot
, start
, size
);
486 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
487 state
->parts
[slot
].flags
= 1;
488 if (SYS_IND(p
) == DM6_PARTITION
)
490 if (SYS_IND(p
) == EZD_PARTITION
)
496 /* second pass - output for each on a separate line */
497 p
= (struct partition
*) (0x1be + data
);
498 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
499 unsigned char id
= SYS_IND(p
);
505 for (n
= 0; subtypes
[n
].parse
&& id
!= subtypes
[n
].id
; n
++)
508 if (!subtypes
[n
].parse
)
510 subtypes
[n
].parse(state
, bdev
, START_SECT(p
)*sector_size
,
511 NR_SECTS(p
)*sector_size
, slot
);
513 put_dev_sector(sect
);