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
;
120 if (!NR_SECTS(p
) || is_extended_partition(p
))
123 /* Check the 3rd and 4th entries -
124 these sometimes contain random garbage */
125 offs
= START_SECT(p
)*sector_size
;
126 size
= NR_SECTS(p
)*sector_size
;
127 next
= this_sector
+ offs
;
129 if (offs
+ size
> this_size
)
131 if (next
< first_sector
)
133 if (next
+ size
> first_sector
+ first_size
)
137 put_partition(state
, state
->next
, next
, size
);
138 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
139 state
->parts
[state
->next
].flags
= 1;
141 if (++state
->next
== state
->limit
)
145 * Next, process the (first) extended partition, if present.
146 * (So far, there seems to be no reason to make
147 * parse_extended() recursive and allow a tree
148 * of extended partitions.)
149 * It should be a link to the next logical partition.
152 for (i
=0; i
<4; i
++, p
++)
153 if (NR_SECTS(p
) && is_extended_partition(p
))
156 goto done
; /* nothing left to do */
158 this_sector
= first_sector
+ START_SECT(p
) * sector_size
;
159 this_size
= NR_SECTS(p
) * sector_size
;
160 put_dev_sector(sect
);
163 put_dev_sector(sect
);
166 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
167 indicates linux swap. Be careful before believing this is Solaris. */
170 parse_solaris_x86(struct parsed_partitions
*state
, struct block_device
*bdev
,
171 u32 offset
, u32 size
, int origin
)
173 #ifdef CONFIG_SOLARIS_X86_PARTITION
175 struct solaris_x86_vtoc
*v
;
178 v
= (struct solaris_x86_vtoc
*)read_dev_sector(bdev
, offset
+1, §
);
181 if (le32_to_cpu(v
->v_sanity
) != SOLARIS_X86_VTOC_SANE
) {
182 put_dev_sector(sect
);
185 printk(" %s%d: <solaris:", state
->name
, origin
);
186 if (le32_to_cpu(v
->v_version
) != 1) {
187 printk(" cannot handle version %d vtoc>\n",
188 le32_to_cpu(v
->v_version
));
189 put_dev_sector(sect
);
192 for (i
=0; i
<SOLARIS_X86_NUMSLICE
&& state
->next
<state
->limit
; i
++) {
193 struct solaris_x86_slice
*s
= &v
->v_slice
[i
];
197 /* solaris partitions are relative to current MS-DOS
198 * one; must add the offset of the current partition */
199 put_partition(state
, state
->next
++,
200 le32_to_cpu(s
->s_start
)+offset
,
201 le32_to_cpu(s
->s_size
));
203 put_dev_sector(sect
);
208 #if defined(CONFIG_BSD_DISKLABEL) || defined(CONFIG_NEC98_PARTITION)
210 * Create devices for BSD partitions listed in a disklabel, under a
211 * dos-like partition. See parse_extended() for more information.
214 parse_bsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
215 u32 offset
, u32 size
, int origin
, char *flavour
,
219 struct bsd_disklabel
*l
;
220 struct bsd_partition
*p
;
222 l
= (struct bsd_disklabel
*)read_dev_sector(bdev
, offset
+1, §
);
225 if (le32_to_cpu(l
->d_magic
) != BSD_DISKMAGIC
) {
226 put_dev_sector(sect
);
229 printk(" %s%d: <%s:", state
->name
, origin
, flavour
);
231 if (le16_to_cpu(l
->d_npartitions
) < max_partitions
)
232 max_partitions
= le16_to_cpu(l
->d_npartitions
);
233 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
234 u32 bsd_start
, bsd_size
;
236 if (state
->next
== state
->limit
)
238 if (p
->p_fstype
== BSD_FS_UNUSED
)
240 bsd_start
= le32_to_cpu(p
->p_offset
);
241 bsd_size
= le32_to_cpu(p
->p_size
);
242 if (offset
== bsd_start
&& size
== bsd_size
)
243 /* full parent partition, we have it already */
245 if (offset
> bsd_start
|| offset
+size
< bsd_start
+bsd_size
) {
246 printk("bad subpartition - ignored\n");
249 put_partition(state
, state
->next
++, bsd_start
, bsd_size
);
251 put_dev_sector(sect
);
252 if (le16_to_cpu(l
->d_npartitions
) > max_partitions
)
253 printk(" (ignored %d more)",
254 le16_to_cpu(l
->d_npartitions
) - max_partitions
);
260 parse_freebsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
261 u32 offset
, u32 size
, int origin
)
263 #ifdef CONFIG_BSD_DISKLABEL
264 parse_bsd(state
, bdev
, offset
, size
, origin
,
265 "bsd", BSD_MAXPARTITIONS
);
270 parse_netbsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
271 u32 offset
, u32 size
, int origin
)
273 #ifdef CONFIG_BSD_DISKLABEL
274 parse_bsd(state
, bdev
, offset
, size
, origin
,
275 "netbsd", BSD_MAXPARTITIONS
);
280 parse_openbsd(struct parsed_partitions
*state
, struct block_device
*bdev
,
281 u32 offset
, u32 size
, int origin
)
283 #ifdef CONFIG_BSD_DISKLABEL
284 parse_bsd(state
, bdev
, offset
, size
, origin
,
285 "openbsd", OPENBSD_MAXPARTITIONS
);
290 * Create devices for Unixware partitions listed in a disklabel, under a
291 * dos-like partition. See parse_extended() for more information.
294 parse_unixware(struct parsed_partitions
*state
, struct block_device
*bdev
,
295 u32 offset
, u32 size
, int origin
)
297 #ifdef CONFIG_UNIXWARE_DISKLABEL
299 struct unixware_disklabel
*l
;
300 struct unixware_slice
*p
;
302 l
= (struct unixware_disklabel
*)read_dev_sector(bdev
, offset
+29, §
);
305 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
306 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
307 put_dev_sector(sect
);
310 printk(" %s%d: <unixware:", state
->name
, origin
);
311 p
= &l
->vtoc
.v_slice
[1];
312 /* I omit the 0th slice as it is the same as whole disk. */
313 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
314 if (state
->next
== state
->limit
)
317 if (p
->s_label
!= UNIXWARE_FS_UNUSED
)
318 put_partition(state
, state
->next
++,
319 START_SECT(p
), NR_SECTS(p
));
322 put_dev_sector(sect
);
328 * Minix 2.0.0/2.0.2 subpartition support.
329 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
330 * Rajeev V. Pillai <rajeevvp@yahoo.com>
333 parse_minix(struct parsed_partitions
*state
, struct block_device
*bdev
,
334 u32 offset
, u32 size
, int origin
)
336 #ifdef CONFIG_MINIX_SUBPARTITION
342 data
= read_dev_sector(bdev
, offset
, §
);
346 p
= (struct partition
*)(data
+ 0x1be);
348 /* The first sector of a Minix partition can have either
349 * a secondary MBR describing its subpartitions, or
350 * the normal boot sector. */
351 if (msdos_magic_present (data
+ 510) &&
352 SYS_IND(p
) == MINIX_PARTITION
) { /* subpartition table present */
354 printk(" %s%d: <minix:", state
->name
, origin
);
355 for (i
= 0; i
< MINIX_NR_SUBPARTITIONS
; i
++, p
++) {
356 if (state
->next
== state
->limit
)
358 /* add each partition in use */
359 if (SYS_IND(p
) == MINIX_PARTITION
)
360 put_partition(state
, state
->next
++,
361 START_SECT(p
), NR_SECTS(p
));
365 put_dev_sector(sect
);
366 #endif /* CONFIG_MINIX_SUBPARTITION */
371 void (*parse
)(struct parsed_partitions
*, struct block_device
*,
374 {FREEBSD_PARTITION
, parse_freebsd
},
375 {NETBSD_PARTITION
, parse_netbsd
},
376 {OPENBSD_PARTITION
, parse_openbsd
},
377 {MINIX_PARTITION
, parse_minix
},
378 {UNIXWARE_PARTITION
, parse_unixware
},
379 {SOLARIS_X86_PARTITION
, parse_solaris_x86
},
380 {NEW_SOLARIS_X86_PARTITION
, parse_solaris_x86
},
384 int msdos_partition(struct parsed_partitions
*state
, struct block_device
*bdev
)
386 int sector_size
= bdev_hardsect_size(bdev
) / 512;
392 data
= read_dev_sector(bdev
, 0, §
);
395 if (!msdos_magic_present(data
+ 510)) {
396 put_dev_sector(sect
);
401 * Now that the 55aa signature is present, this is probably
402 * either the boot sector of a FAT filesystem or a DOS-type
403 * partition table. Reject this in case the boot indicator
406 p
= (struct partition
*) (data
+ 0x1be);
407 for (slot
= 1; slot
<= 4; slot
++, p
++) {
408 if (p
->boot_ind
!= 0 && p
->boot_ind
!= 0x80) {
409 put_dev_sector(sect
);
414 #ifdef CONFIG_EFI_PARTITION
415 p
= (struct partition
*) (data
+ 0x1be);
416 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
417 /* If this is an EFI GPT disk, msdos should ignore it. */
418 if (SYS_IND(p
) == EFI_PMBR_OSTYPE_EFI_GPT
) {
419 put_dev_sector(sect
);
424 p
= (struct partition
*) (data
+ 0x1be);
427 * Look for partitions in two passes:
428 * First find the primary and DOS-type extended partitions.
429 * On the second pass look inside *BSD, Unixware and Solaris partitions.
433 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
434 u32 start
= START_SECT(p
)*sector_size
;
435 u32 size
= NR_SECTS(p
)*sector_size
;
440 if (is_extended_partition(p
)) {
441 /* prevent someone doing mkfs or mkswap on an
442 extended partition, but leave room for LILO */
443 put_partition(state
, slot
, start
, size
== 1 ? 1 : 2);
445 parse_extended(state
, bdev
, start
, size
);
449 put_partition(state
, slot
, start
, size
);
450 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
451 state
->parts
[slot
].flags
= 1;
452 if (SYS_IND(p
) == DM6_PARTITION
)
454 if (SYS_IND(p
) == EZD_PARTITION
)
460 /* second pass - output for each on a separate line */
461 p
= (struct partition
*) (0x1be + data
);
462 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
463 unsigned char id
= SYS_IND(p
);
469 for (n
= 0; subtypes
[n
].parse
&& id
!= subtypes
[n
].id
; n
++)
472 if (!subtypes
[n
].parse
)
474 subtypes
[n
].parse(state
, bdev
, START_SECT(p
)*sector_size
,
475 NR_SECTS(p
)*sector_size
, slot
);
477 put_dev_sector(sect
);