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
21 #include <linux/msdos_fs.h>
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)
36 static inline sector_t
nr_sects(struct partition
*p
)
38 return (sector_t
)get_unaligned_le32(&p
->nr_sects
);
41 static inline sector_t
start_sect(struct partition
*p
)
43 return (sector_t
)get_unaligned_le32(&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
);
62 /* Value is EBCDIC 'IBMA' */
63 #define AIX_LABEL_MAGIC1 0xC9
64 #define AIX_LABEL_MAGIC2 0xC2
65 #define AIX_LABEL_MAGIC3 0xD4
66 #define AIX_LABEL_MAGIC4 0xC1
67 static int aix_magic_present(struct parsed_partitions
*state
, unsigned char *p
)
69 struct partition
*pt
= (struct partition
*) (p
+ 0x1be);
74 if (!(p
[0] == AIX_LABEL_MAGIC1
&&
75 p
[1] == AIX_LABEL_MAGIC2
&&
76 p
[2] == AIX_LABEL_MAGIC3
&&
77 p
[3] == AIX_LABEL_MAGIC4
))
79 /* Assume the partition table is valid if Linux partitions exists */
80 for (slot
= 1; slot
<= 4; slot
++, pt
++) {
81 if (pt
->sys_ind
== LINUX_SWAP_PARTITION
||
82 pt
->sys_ind
== LINUX_RAID_PARTITION
||
83 pt
->sys_ind
== LINUX_DATA_PARTITION
||
84 pt
->sys_ind
== LINUX_LVM_PARTITION
||
85 is_extended_partition(pt
))
88 d
= read_part_sector(state
, 7, §
);
90 if (d
[0] == '_' && d
[1] == 'L' && d
[2] == 'V' && d
[3] == 'M')
98 * Create devices for each logical partition in an extended partition.
99 * The logical partitions form a linked list, with each entry being
100 * a partition table with two entries. The first entry
101 * is the real data partition (with a start relative to the partition
102 * table start). The second is a pointer to the next logical partition
103 * (with a start relative to the entire extended partition).
104 * We do not create a Linux partition for the partition tables, but
105 * only for the actual data partitions.
108 static void parse_extended(struct parsed_partitions
*state
,
109 sector_t first_sector
, sector_t first_size
)
114 sector_t this_sector
, this_size
;
115 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
116 int loopct
= 0; /* number of links followed
117 without finding a data partition */
120 this_sector
= first_sector
;
121 this_size
= first_size
;
126 if (state
->next
== state
->limit
)
128 data
= read_part_sector(state
, this_sector
, §
);
132 if (!msdos_magic_present(data
+ 510))
135 p
= (struct partition
*) (data
+ 0x1be);
138 * Usually, the first entry is the real data partition,
139 * the 2nd entry is the next extended partition, or empty,
140 * and the 3rd and 4th entries are unused.
141 * However, DRDOS sometimes has the extended partition as
142 * the first entry (when the data partition is empty),
143 * and OS/2 seems to use all four entries.
147 * First process the data partition(s)
149 for (i
=0; i
<4; i
++, p
++) {
150 sector_t offs
, size
, next
;
151 if (!nr_sects(p
) || is_extended_partition(p
))
154 /* Check the 3rd and 4th entries -
155 these sometimes contain random garbage */
156 offs
= start_sect(p
)*sector_size
;
157 size
= nr_sects(p
)*sector_size
;
158 next
= this_sector
+ offs
;
160 if (offs
+ size
> this_size
)
162 if (next
< first_sector
)
164 if (next
+ size
> first_sector
+ first_size
)
168 put_partition(state
, state
->next
, next
, size
);
169 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
170 state
->parts
[state
->next
].flags
= ADDPART_FLAG_RAID
;
172 if (++state
->next
== state
->limit
)
176 * Next, process the (first) extended partition, if present.
177 * (So far, there seems to be no reason to make
178 * parse_extended() recursive and allow a tree
179 * of extended partitions.)
180 * It should be a link to the next logical partition.
183 for (i
=0; i
<4; i
++, p
++)
184 if (nr_sects(p
) && is_extended_partition(p
))
187 goto done
; /* nothing left to do */
189 this_sector
= first_sector
+ start_sect(p
) * sector_size
;
190 this_size
= nr_sects(p
) * sector_size
;
191 put_dev_sector(sect
);
194 put_dev_sector(sect
);
197 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
198 indicates linux swap. Be careful before believing this is Solaris. */
200 static void parse_solaris_x86(struct parsed_partitions
*state
,
201 sector_t offset
, sector_t size
, int origin
)
203 #ifdef CONFIG_SOLARIS_X86_PARTITION
205 struct solaris_x86_vtoc
*v
;
209 v
= read_part_sector(state
, offset
+ 1, §
);
212 if (le32_to_cpu(v
->v_sanity
) != SOLARIS_X86_VTOC_SANE
) {
213 put_dev_sector(sect
);
217 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 11 + 1];
219 snprintf(tmp
, sizeof(tmp
), " %s%d: <solaris:", state
->name
, origin
);
220 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
222 if (le32_to_cpu(v
->v_version
) != 1) {
225 snprintf(tmp
, sizeof(tmp
), " cannot handle version %d vtoc>\n",
226 le32_to_cpu(v
->v_version
));
227 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
228 put_dev_sector(sect
);
231 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
232 max_nparts
= le16_to_cpu (v
->v_nparts
) > 8 ? SOLARIS_X86_NUMSLICE
: 8;
233 for (i
=0; i
<max_nparts
&& state
->next
<state
->limit
; i
++) {
234 struct solaris_x86_slice
*s
= &v
->v_slice
[i
];
235 char tmp
[3 + 10 + 1 + 1];
239 snprintf(tmp
, sizeof(tmp
), " [s%d]", i
);
240 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
241 /* solaris partitions are relative to current MS-DOS
242 * one; must add the offset of the current partition */
243 put_partition(state
, state
->next
++,
244 le32_to_cpu(s
->s_start
)+offset
,
245 le32_to_cpu(s
->s_size
));
247 put_dev_sector(sect
);
248 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
252 #if defined(CONFIG_BSD_DISKLABEL)
254 * Create devices for BSD partitions listed in a disklabel, under a
255 * dos-like partition. See parse_extended() for more information.
257 static void parse_bsd(struct parsed_partitions
*state
,
258 sector_t offset
, sector_t size
, int origin
, char *flavour
,
262 struct bsd_disklabel
*l
;
263 struct bsd_partition
*p
;
266 l
= read_part_sector(state
, offset
+ 1, §
);
269 if (le32_to_cpu(l
->d_magic
) != BSD_DISKMAGIC
) {
270 put_dev_sector(sect
);
274 snprintf(tmp
, sizeof(tmp
), " %s%d: <%s:", state
->name
, origin
, flavour
);
275 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
277 if (le16_to_cpu(l
->d_npartitions
) < max_partitions
)
278 max_partitions
= le16_to_cpu(l
->d_npartitions
);
279 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
280 sector_t bsd_start
, bsd_size
;
282 if (state
->next
== state
->limit
)
284 if (p
->p_fstype
== BSD_FS_UNUSED
)
286 bsd_start
= le32_to_cpu(p
->p_offset
);
287 bsd_size
= le32_to_cpu(p
->p_size
);
288 if (offset
== bsd_start
&& size
== bsd_size
)
289 /* full parent partition, we have it already */
291 if (offset
> bsd_start
|| offset
+size
< bsd_start
+bsd_size
) {
292 strlcat(state
->pp_buf
, "bad subpartition - ignored\n", PAGE_SIZE
);
295 put_partition(state
, state
->next
++, bsd_start
, bsd_size
);
297 put_dev_sector(sect
);
298 if (le16_to_cpu(l
->d_npartitions
) > max_partitions
) {
299 snprintf(tmp
, sizeof(tmp
), " (ignored %d more)",
300 le16_to_cpu(l
->d_npartitions
) - max_partitions
);
301 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
303 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
307 static void parse_freebsd(struct parsed_partitions
*state
,
308 sector_t offset
, sector_t size
, int origin
)
310 #ifdef CONFIG_BSD_DISKLABEL
311 parse_bsd(state
, offset
, size
, origin
, "bsd", BSD_MAXPARTITIONS
);
315 static void parse_netbsd(struct parsed_partitions
*state
,
316 sector_t offset
, sector_t size
, int origin
)
318 #ifdef CONFIG_BSD_DISKLABEL
319 parse_bsd(state
, offset
, size
, origin
, "netbsd", BSD_MAXPARTITIONS
);
323 static void parse_openbsd(struct parsed_partitions
*state
,
324 sector_t offset
, sector_t size
, int origin
)
326 #ifdef CONFIG_BSD_DISKLABEL
327 parse_bsd(state
, offset
, size
, origin
, "openbsd",
328 OPENBSD_MAXPARTITIONS
);
333 * Create devices for Unixware partitions listed in a disklabel, under a
334 * dos-like partition. See parse_extended() for more information.
336 static void parse_unixware(struct parsed_partitions
*state
,
337 sector_t offset
, sector_t size
, int origin
)
339 #ifdef CONFIG_UNIXWARE_DISKLABEL
341 struct unixware_disklabel
*l
;
342 struct unixware_slice
*p
;
344 l
= read_part_sector(state
, offset
+ 29, §
);
347 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
348 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
349 put_dev_sector(sect
);
353 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 12 + 1];
355 snprintf(tmp
, sizeof(tmp
), " %s%d: <unixware:", state
->name
, origin
);
356 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
358 p
= &l
->vtoc
.v_slice
[1];
359 /* I omit the 0th slice as it is the same as whole disk. */
360 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
361 if (state
->next
== state
->limit
)
364 if (p
->s_label
!= UNIXWARE_FS_UNUSED
)
365 put_partition(state
, state
->next
++,
366 le32_to_cpu(p
->start_sect
),
367 le32_to_cpu(p
->nr_sects
));
370 put_dev_sector(sect
);
371 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
376 * Minix 2.0.0/2.0.2 subpartition support.
377 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
378 * Rajeev V. Pillai <rajeevvp@yahoo.com>
380 static void parse_minix(struct parsed_partitions
*state
,
381 sector_t offset
, sector_t size
, int origin
)
383 #ifdef CONFIG_MINIX_SUBPARTITION
389 data
= read_part_sector(state
, offset
, §
);
393 p
= (struct partition
*)(data
+ 0x1be);
395 /* The first sector of a Minix partition can have either
396 * a secondary MBR describing its subpartitions, or
397 * the normal boot sector. */
398 if (msdos_magic_present (data
+ 510) &&
399 SYS_IND(p
) == MINIX_PARTITION
) { /* subpartition table present */
400 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 9 + 1];
402 snprintf(tmp
, sizeof(tmp
), " %s%d: <minix:", state
->name
, origin
);
403 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
404 for (i
= 0; i
< MINIX_NR_SUBPARTITIONS
; i
++, p
++) {
405 if (state
->next
== state
->limit
)
407 /* add each partition in use */
408 if (SYS_IND(p
) == MINIX_PARTITION
)
409 put_partition(state
, state
->next
++,
410 start_sect(p
), nr_sects(p
));
412 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
414 put_dev_sector(sect
);
415 #endif /* CONFIG_MINIX_SUBPARTITION */
420 void (*parse
)(struct parsed_partitions
*, sector_t
, sector_t
, int);
422 {FREEBSD_PARTITION
, parse_freebsd
},
423 {NETBSD_PARTITION
, parse_netbsd
},
424 {OPENBSD_PARTITION
, parse_openbsd
},
425 {MINIX_PARTITION
, parse_minix
},
426 {UNIXWARE_PARTITION
, parse_unixware
},
427 {SOLARIS_X86_PARTITION
, parse_solaris_x86
},
428 {NEW_SOLARIS_X86_PARTITION
, parse_solaris_x86
},
432 int msdos_partition(struct parsed_partitions
*state
)
434 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
438 struct fat_boot_sector
*fb
;
441 data
= read_part_sector(state
, 0, §
);
444 if (!msdos_magic_present(data
+ 510)) {
445 put_dev_sector(sect
);
449 if (aix_magic_present(state
, data
)) {
450 put_dev_sector(sect
);
451 strlcat(state
->pp_buf
, " [AIX]", PAGE_SIZE
);
456 * Now that the 55aa signature is present, this is probably
457 * either the boot sector of a FAT filesystem or a DOS-type
458 * partition table. Reject this in case the boot indicator
461 p
= (struct partition
*) (data
+ 0x1be);
462 for (slot
= 1; slot
<= 4; slot
++, p
++) {
463 if (p
->boot_ind
!= 0 && p
->boot_ind
!= 0x80) {
465 * Even without a valid boot inidicator value
466 * its still possible this is valid FAT filesystem
467 * without a partition table.
469 fb
= (struct fat_boot_sector
*) data
;
470 if (slot
== 1 && fb
->reserved
&& fb
->fats
471 && fat_valid_media(fb
->media
)) {
472 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
473 put_dev_sector(sect
);
476 put_dev_sector(sect
);
482 #ifdef CONFIG_EFI_PARTITION
483 p
= (struct partition
*) (data
+ 0x1be);
484 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
485 /* If this is an EFI GPT disk, msdos should ignore it. */
486 if (SYS_IND(p
) == EFI_PMBR_OSTYPE_EFI_GPT
) {
487 put_dev_sector(sect
);
492 p
= (struct partition
*) (data
+ 0x1be);
495 * Look for partitions in two passes:
496 * First find the primary and DOS-type extended partitions.
497 * On the second pass look inside *BSD, Unixware and Solaris partitions.
501 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
502 sector_t start
= start_sect(p
)*sector_size
;
503 sector_t size
= nr_sects(p
)*sector_size
;
506 if (is_extended_partition(p
)) {
508 * prevent someone doing mkfs or mkswap on an
509 * extended partition, but leave room for LILO
510 * FIXME: this uses one logical sector for > 512b
511 * sector, although it may not be enough/proper.
514 n
= min(size
, max(sector_size
, n
));
515 put_partition(state
, slot
, start
, n
);
517 strlcat(state
->pp_buf
, " <", PAGE_SIZE
);
518 parse_extended(state
, start
, size
);
519 strlcat(state
->pp_buf
, " >", PAGE_SIZE
);
522 put_partition(state
, slot
, start
, size
);
523 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
524 state
->parts
[slot
].flags
= ADDPART_FLAG_RAID
;
525 if (SYS_IND(p
) == DM6_PARTITION
)
526 strlcat(state
->pp_buf
, "[DM]", PAGE_SIZE
);
527 if (SYS_IND(p
) == EZD_PARTITION
)
528 strlcat(state
->pp_buf
, "[EZD]", PAGE_SIZE
);
531 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
533 /* second pass - output for each on a separate line */
534 p
= (struct partition
*) (0x1be + data
);
535 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
536 unsigned char id
= SYS_IND(p
);
542 for (n
= 0; subtypes
[n
].parse
&& id
!= subtypes
[n
].id
; n
++)
545 if (!subtypes
[n
].parse
)
547 subtypes
[n
].parse(state
, start_sect(p
) * sector_size
,
548 nr_sects(p
) * sector_size
, slot
);
550 put_dev_sector(sect
);