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>
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)
37 static inline sector_t
nr_sects(struct partition
*p
)
39 return (sector_t
)get_unaligned_le32(&p
->nr_sects
);
42 static inline sector_t
start_sect(struct partition
*p
)
44 return (sector_t
)get_unaligned_le32(&p
->start_sect
);
47 static inline int is_extended_partition(struct partition
*p
)
49 return (SYS_IND(p
) == DOS_EXTENDED_PARTITION
||
50 SYS_IND(p
) == WIN98_EXTENDED_PARTITION
||
51 SYS_IND(p
) == LINUX_EXTENDED_PARTITION
);
54 #define MSDOS_LABEL_MAGIC1 0x55
55 #define MSDOS_LABEL_MAGIC2 0xAA
58 msdos_magic_present(unsigned char *p
)
60 return (p
[0] == MSDOS_LABEL_MAGIC1
&& p
[1] == MSDOS_LABEL_MAGIC2
);
63 /* Value is EBCDIC 'IBMA' */
64 #define AIX_LABEL_MAGIC1 0xC9
65 #define AIX_LABEL_MAGIC2 0xC2
66 #define AIX_LABEL_MAGIC3 0xD4
67 #define AIX_LABEL_MAGIC4 0xC1
68 static int aix_magic_present(struct parsed_partitions
*state
, unsigned char *p
)
70 struct partition
*pt
= (struct partition
*) (p
+ 0x1be);
75 if (!(p
[0] == AIX_LABEL_MAGIC1
&&
76 p
[1] == AIX_LABEL_MAGIC2
&&
77 p
[2] == AIX_LABEL_MAGIC3
&&
78 p
[3] == AIX_LABEL_MAGIC4
))
80 /* Assume the partition table is valid if Linux partitions exists */
81 for (slot
= 1; slot
<= 4; slot
++, pt
++) {
82 if (pt
->sys_ind
== LINUX_SWAP_PARTITION
||
83 pt
->sys_ind
== LINUX_RAID_PARTITION
||
84 pt
->sys_ind
== LINUX_DATA_PARTITION
||
85 pt
->sys_ind
== LINUX_LVM_PARTITION
||
86 is_extended_partition(pt
))
89 d
= read_part_sector(state
, 7, §
);
91 if (d
[0] == '_' && d
[1] == 'L' && d
[2] == 'V' && d
[3] == 'M')
98 static void set_info(struct parsed_partitions
*state
, int slot
,
101 struct partition_meta_info
*info
= &state
->parts
[slot
].info
;
103 snprintf(info
->uuid
, sizeof(info
->uuid
), "%08x-%02x", disksig
,
105 info
->volname
[0] = 0;
106 state
->parts
[slot
].has_info
= true;
110 * Create devices for each logical partition in an extended partition.
111 * The logical partitions form a linked list, with each entry being
112 * a partition table with two entries. The first entry
113 * is the real data partition (with a start relative to the partition
114 * table start). The second is a pointer to the next logical partition
115 * (with a start relative to the entire extended partition).
116 * We do not create a Linux partition for the partition tables, but
117 * only for the actual data partitions.
120 static void parse_extended(struct parsed_partitions
*state
,
121 sector_t first_sector
, sector_t first_size
,
127 sector_t this_sector
, this_size
;
128 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
129 int loopct
= 0; /* number of links followed
130 without finding a data partition */
133 this_sector
= first_sector
;
134 this_size
= first_size
;
139 if (state
->next
== state
->limit
)
141 data
= read_part_sector(state
, this_sector
, §
);
145 if (!msdos_magic_present(data
+ 510))
148 p
= (struct partition
*) (data
+ 0x1be);
151 * Usually, the first entry is the real data partition,
152 * the 2nd entry is the next extended partition, or empty,
153 * and the 3rd and 4th entries are unused.
154 * However, DRDOS sometimes has the extended partition as
155 * the first entry (when the data partition is empty),
156 * and OS/2 seems to use all four entries.
160 * First process the data partition(s)
162 for (i
= 0; i
< 4; i
++, p
++) {
163 sector_t offs
, size
, next
;
165 if (!nr_sects(p
) || is_extended_partition(p
))
168 /* Check the 3rd and 4th entries -
169 these sometimes contain random garbage */
170 offs
= start_sect(p
)*sector_size
;
171 size
= nr_sects(p
)*sector_size
;
172 next
= this_sector
+ offs
;
174 if (offs
+ size
> this_size
)
176 if (next
< first_sector
)
178 if (next
+ size
> first_sector
+ first_size
)
182 put_partition(state
, state
->next
, next
, size
);
183 set_info(state
, state
->next
, disksig
);
184 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
185 state
->parts
[state
->next
].flags
= ADDPART_FLAG_RAID
;
187 if (++state
->next
== state
->limit
)
191 * Next, process the (first) extended partition, if present.
192 * (So far, there seems to be no reason to make
193 * parse_extended() recursive and allow a tree
194 * of extended partitions.)
195 * It should be a link to the next logical partition.
198 for (i
= 0; i
< 4; i
++, p
++)
199 if (nr_sects(p
) && is_extended_partition(p
))
202 goto done
; /* nothing left to do */
204 this_sector
= first_sector
+ start_sect(p
) * sector_size
;
205 this_size
= nr_sects(p
) * sector_size
;
206 put_dev_sector(sect
);
209 put_dev_sector(sect
);
212 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
213 indicates linux swap. Be careful before believing this is Solaris. */
215 static void parse_solaris_x86(struct parsed_partitions
*state
,
216 sector_t offset
, sector_t size
, int origin
)
218 #ifdef CONFIG_SOLARIS_X86_PARTITION
220 struct solaris_x86_vtoc
*v
;
224 v
= read_part_sector(state
, offset
+ 1, §
);
227 if (le32_to_cpu(v
->v_sanity
) != SOLARIS_X86_VTOC_SANE
) {
228 put_dev_sector(sect
);
232 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 11 + 1];
234 snprintf(tmp
, sizeof(tmp
), " %s%d: <solaris:", state
->name
, origin
);
235 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
237 if (le32_to_cpu(v
->v_version
) != 1) {
240 snprintf(tmp
, sizeof(tmp
), " cannot handle version %d vtoc>\n",
241 le32_to_cpu(v
->v_version
));
242 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
243 put_dev_sector(sect
);
246 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
247 max_nparts
= le16_to_cpu(v
->v_nparts
) > 8 ? SOLARIS_X86_NUMSLICE
: 8;
248 for (i
= 0; i
< max_nparts
&& state
->next
< state
->limit
; i
++) {
249 struct solaris_x86_slice
*s
= &v
->v_slice
[i
];
250 char tmp
[3 + 10 + 1 + 1];
254 snprintf(tmp
, sizeof(tmp
), " [s%d]", i
);
255 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
256 /* solaris partitions are relative to current MS-DOS
257 * one; must add the offset of the current partition */
258 put_partition(state
, state
->next
++,
259 le32_to_cpu(s
->s_start
)+offset
,
260 le32_to_cpu(s
->s_size
));
262 put_dev_sector(sect
);
263 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
267 #if defined(CONFIG_BSD_DISKLABEL)
269 * Create devices for BSD partitions listed in a disklabel, under a
270 * dos-like partition. See parse_extended() for more information.
272 static void parse_bsd(struct parsed_partitions
*state
,
273 sector_t offset
, sector_t size
, int origin
, char *flavour
,
277 struct bsd_disklabel
*l
;
278 struct bsd_partition
*p
;
281 l
= read_part_sector(state
, offset
+ 1, §
);
284 if (le32_to_cpu(l
->d_magic
) != BSD_DISKMAGIC
) {
285 put_dev_sector(sect
);
289 snprintf(tmp
, sizeof(tmp
), " %s%d: <%s:", state
->name
, origin
, flavour
);
290 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
292 if (le16_to_cpu(l
->d_npartitions
) < max_partitions
)
293 max_partitions
= le16_to_cpu(l
->d_npartitions
);
294 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
295 sector_t bsd_start
, bsd_size
;
297 if (state
->next
== state
->limit
)
299 if (p
->p_fstype
== BSD_FS_UNUSED
)
301 bsd_start
= le32_to_cpu(p
->p_offset
);
302 bsd_size
= le32_to_cpu(p
->p_size
);
303 if (offset
== bsd_start
&& size
== bsd_size
)
304 /* full parent partition, we have it already */
306 if (offset
> bsd_start
|| offset
+size
< bsd_start
+bsd_size
) {
307 strlcat(state
->pp_buf
, "bad subpartition - ignored\n", PAGE_SIZE
);
310 put_partition(state
, state
->next
++, bsd_start
, bsd_size
);
312 put_dev_sector(sect
);
313 if (le16_to_cpu(l
->d_npartitions
) > max_partitions
) {
314 snprintf(tmp
, sizeof(tmp
), " (ignored %d more)",
315 le16_to_cpu(l
->d_npartitions
) - max_partitions
);
316 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
318 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
322 static void parse_freebsd(struct parsed_partitions
*state
,
323 sector_t offset
, sector_t size
, int origin
)
325 #ifdef CONFIG_BSD_DISKLABEL
326 parse_bsd(state
, offset
, size
, origin
, "bsd", BSD_MAXPARTITIONS
);
330 static void parse_netbsd(struct parsed_partitions
*state
,
331 sector_t offset
, sector_t size
, int origin
)
333 #ifdef CONFIG_BSD_DISKLABEL
334 parse_bsd(state
, offset
, size
, origin
, "netbsd", BSD_MAXPARTITIONS
);
338 static void parse_openbsd(struct parsed_partitions
*state
,
339 sector_t offset
, sector_t size
, int origin
)
341 #ifdef CONFIG_BSD_DISKLABEL
342 parse_bsd(state
, offset
, size
, origin
, "openbsd",
343 OPENBSD_MAXPARTITIONS
);
348 * Create devices for Unixware partitions listed in a disklabel, under a
349 * dos-like partition. See parse_extended() for more information.
351 static void parse_unixware(struct parsed_partitions
*state
,
352 sector_t offset
, sector_t size
, int origin
)
354 #ifdef CONFIG_UNIXWARE_DISKLABEL
356 struct unixware_disklabel
*l
;
357 struct unixware_slice
*p
;
359 l
= read_part_sector(state
, offset
+ 29, §
);
362 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
363 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
364 put_dev_sector(sect
);
368 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 12 + 1];
370 snprintf(tmp
, sizeof(tmp
), " %s%d: <unixware:", state
->name
, origin
);
371 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
373 p
= &l
->vtoc
.v_slice
[1];
374 /* I omit the 0th slice as it is the same as whole disk. */
375 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
376 if (state
->next
== state
->limit
)
379 if (p
->s_label
!= UNIXWARE_FS_UNUSED
)
380 put_partition(state
, state
->next
++,
381 le32_to_cpu(p
->start_sect
),
382 le32_to_cpu(p
->nr_sects
));
385 put_dev_sector(sect
);
386 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
391 * Minix 2.0.0/2.0.2 subpartition support.
392 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
393 * Rajeev V. Pillai <rajeevvp@yahoo.com>
395 static void parse_minix(struct parsed_partitions
*state
,
396 sector_t offset
, sector_t size
, int origin
)
398 #ifdef CONFIG_MINIX_SUBPARTITION
404 data
= read_part_sector(state
, offset
, §
);
408 p
= (struct partition
*)(data
+ 0x1be);
410 /* The first sector of a Minix partition can have either
411 * a secondary MBR describing its subpartitions, or
412 * the normal boot sector. */
413 if (msdos_magic_present(data
+ 510) &&
414 SYS_IND(p
) == MINIX_PARTITION
) { /* subpartition table present */
415 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 9 + 1];
417 snprintf(tmp
, sizeof(tmp
), " %s%d: <minix:", state
->name
, origin
);
418 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
419 for (i
= 0; i
< MINIX_NR_SUBPARTITIONS
; i
++, p
++) {
420 if (state
->next
== state
->limit
)
422 /* add each partition in use */
423 if (SYS_IND(p
) == MINIX_PARTITION
)
424 put_partition(state
, state
->next
++,
425 start_sect(p
), nr_sects(p
));
427 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
429 put_dev_sector(sect
);
430 #endif /* CONFIG_MINIX_SUBPARTITION */
435 void (*parse
)(struct parsed_partitions
*, sector_t
, sector_t
, int);
437 {FREEBSD_PARTITION
, parse_freebsd
},
438 {NETBSD_PARTITION
, parse_netbsd
},
439 {OPENBSD_PARTITION
, parse_openbsd
},
440 {MINIX_PARTITION
, parse_minix
},
441 {UNIXWARE_PARTITION
, parse_unixware
},
442 {SOLARIS_X86_PARTITION
, parse_solaris_x86
},
443 {NEW_SOLARIS_X86_PARTITION
, parse_solaris_x86
},
447 int msdos_partition(struct parsed_partitions
*state
)
449 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
453 struct fat_boot_sector
*fb
;
457 data
= read_part_sector(state
, 0, §
);
462 * Note order! (some AIX disks, e.g. unbootable kind,
463 * have no MSDOS 55aa)
465 if (aix_magic_present(state
, data
)) {
466 put_dev_sector(sect
);
467 #ifdef CONFIG_AIX_PARTITION
468 return aix_partition(state
);
470 strlcat(state
->pp_buf
, " [AIX]", PAGE_SIZE
);
475 if (!msdos_magic_present(data
+ 510)) {
476 put_dev_sector(sect
);
481 * Now that the 55aa signature is present, this is probably
482 * either the boot sector of a FAT filesystem or a DOS-type
483 * partition table. Reject this in case the boot indicator
486 p
= (struct partition
*) (data
+ 0x1be);
487 for (slot
= 1; slot
<= 4; slot
++, p
++) {
488 if (p
->boot_ind
!= 0 && p
->boot_ind
!= 0x80) {
490 * Even without a valid boot inidicator value
491 * its still possible this is valid FAT filesystem
492 * without a partition table.
494 fb
= (struct fat_boot_sector
*) data
;
495 if (slot
== 1 && fb
->reserved
&& fb
->fats
496 && fat_valid_media(fb
->media
)) {
497 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
498 put_dev_sector(sect
);
501 put_dev_sector(sect
);
507 #ifdef CONFIG_EFI_PARTITION
508 p
= (struct partition
*) (data
+ 0x1be);
509 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
510 /* If this is an EFI GPT disk, msdos should ignore it. */
511 if (SYS_IND(p
) == EFI_PMBR_OSTYPE_EFI_GPT
) {
512 put_dev_sector(sect
);
517 p
= (struct partition
*) (data
+ 0x1be);
519 disksig
= le32_to_cpup((__le32
*)(data
+ 0x1b8));
522 * Look for partitions in two passes:
523 * First find the primary and DOS-type extended partitions.
524 * On the second pass look inside *BSD, Unixware and Solaris partitions.
528 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
529 sector_t start
= start_sect(p
)*sector_size
;
530 sector_t size
= nr_sects(p
)*sector_size
;
534 if (is_extended_partition(p
)) {
536 * prevent someone doing mkfs or mkswap on an
537 * extended partition, but leave room for LILO
538 * FIXME: this uses one logical sector for > 512b
539 * sector, although it may not be enough/proper.
543 n
= min(size
, max(sector_size
, n
));
544 put_partition(state
, slot
, start
, n
);
546 strlcat(state
->pp_buf
, " <", PAGE_SIZE
);
547 parse_extended(state
, start
, size
, disksig
);
548 strlcat(state
->pp_buf
, " >", PAGE_SIZE
);
551 put_partition(state
, slot
, start
, size
);
552 set_info(state
, slot
, disksig
);
553 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
554 state
->parts
[slot
].flags
= ADDPART_FLAG_RAID
;
555 if (SYS_IND(p
) == DM6_PARTITION
)
556 strlcat(state
->pp_buf
, "[DM]", PAGE_SIZE
);
557 if (SYS_IND(p
) == EZD_PARTITION
)
558 strlcat(state
->pp_buf
, "[EZD]", PAGE_SIZE
);
561 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
563 /* second pass - output for each on a separate line */
564 p
= (struct partition
*) (0x1be + data
);
565 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
566 unsigned char id
= SYS_IND(p
);
572 for (n
= 0; subtypes
[n
].parse
&& id
!= subtypes
[n
].id
; n
++)
575 if (!subtypes
[n
].parse
)
577 subtypes
[n
].parse(state
, start_sect(p
) * sector_size
,
578 nr_sects(p
) * sector_size
, slot
);
580 put_dev_sector(sect
);