1 // SPDX-License-Identifier: GPL-2.0
3 * fs/partitions/msdos.c
5 * Code extracted from drivers/block/genhd.c
6 * Copyright (C) 1991-1998 Linus Torvalds
8 * Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
9 * in the early extended-partition checks and added DM partitions
11 * Support for DiskManager v6.0x added by Mark Lord,
12 * with information provided by OnTrack. This now works for linux fdisk
13 * and LILO, as well as loadlin and bootln. Note that disks other than
14 * /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
16 * More flexible handling of extended partitions - aeb, 950831
18 * Check partition table on IDE disks for common CHS translations
20 * Re-organised Feb 1998 Russell King
22 #include <linux/msdos_fs.h>
30 * Many architectures don't like unaligned accesses, while
31 * the nr_sects and start_sect partition table entries are
32 * at a 2 (mod 4) address.
34 #include <asm/unaligned.h>
36 #define SYS_IND(p) get_unaligned(&p->sys_ind)
38 static inline sector_t
nr_sects(struct partition
*p
)
40 return (sector_t
)get_unaligned_le32(&p
->nr_sects
);
43 static inline sector_t
start_sect(struct partition
*p
)
45 return (sector_t
)get_unaligned_le32(&p
->start_sect
);
48 static inline int is_extended_partition(struct partition
*p
)
50 return (SYS_IND(p
) == DOS_EXTENDED_PARTITION
||
51 SYS_IND(p
) == WIN98_EXTENDED_PARTITION
||
52 SYS_IND(p
) == LINUX_EXTENDED_PARTITION
);
55 #define MSDOS_LABEL_MAGIC1 0x55
56 #define MSDOS_LABEL_MAGIC2 0xAA
59 msdos_magic_present(unsigned char *p
)
61 return (p
[0] == MSDOS_LABEL_MAGIC1
&& p
[1] == MSDOS_LABEL_MAGIC2
);
64 /* Value is EBCDIC 'IBMA' */
65 #define AIX_LABEL_MAGIC1 0xC9
66 #define AIX_LABEL_MAGIC2 0xC2
67 #define AIX_LABEL_MAGIC3 0xD4
68 #define AIX_LABEL_MAGIC4 0xC1
69 static int aix_magic_present(struct parsed_partitions
*state
, unsigned char *p
)
71 struct partition
*pt
= (struct partition
*) (p
+ 0x1be);
76 if (!(p
[0] == AIX_LABEL_MAGIC1
&&
77 p
[1] == AIX_LABEL_MAGIC2
&&
78 p
[2] == AIX_LABEL_MAGIC3
&&
79 p
[3] == AIX_LABEL_MAGIC4
))
81 /* Assume the partition table is valid if Linux partitions exists */
82 for (slot
= 1; slot
<= 4; slot
++, pt
++) {
83 if (pt
->sys_ind
== LINUX_SWAP_PARTITION
||
84 pt
->sys_ind
== LINUX_RAID_PARTITION
||
85 pt
->sys_ind
== LINUX_DATA_PARTITION
||
86 pt
->sys_ind
== LINUX_LVM_PARTITION
||
87 is_extended_partition(pt
))
90 d
= read_part_sector(state
, 7, §
);
92 if (d
[0] == '_' && d
[1] == 'L' && d
[2] == 'V' && d
[3] == 'M')
99 static void set_info(struct parsed_partitions
*state
, int slot
,
102 struct partition_meta_info
*info
= &state
->parts
[slot
].info
;
104 snprintf(info
->uuid
, sizeof(info
->uuid
), "%08x-%02x", disksig
,
106 info
->volname
[0] = 0;
107 state
->parts
[slot
].has_info
= true;
111 * Create devices for each logical partition in an extended partition.
112 * The logical partitions form a linked list, with each entry being
113 * a partition table with two entries. The first entry
114 * is the real data partition (with a start relative to the partition
115 * table start). The second is a pointer to the next logical partition
116 * (with a start relative to the entire extended partition).
117 * We do not create a Linux partition for the partition tables, but
118 * only for the actual data partitions.
121 static void parse_extended(struct parsed_partitions
*state
,
122 sector_t first_sector
, sector_t first_size
,
128 sector_t this_sector
, this_size
;
129 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
130 int loopct
= 0; /* number of links followed
131 without finding a data partition */
134 this_sector
= first_sector
;
135 this_size
= first_size
;
140 if (state
->next
== state
->limit
)
142 data
= read_part_sector(state
, this_sector
, §
);
146 if (!msdos_magic_present(data
+ 510))
149 p
= (struct partition
*) (data
+ 0x1be);
152 * Usually, the first entry is the real data partition,
153 * the 2nd entry is the next extended partition, or empty,
154 * and the 3rd and 4th entries are unused.
155 * However, DRDOS sometimes has the extended partition as
156 * the first entry (when the data partition is empty),
157 * and OS/2 seems to use all four entries.
161 * First process the data partition(s)
163 for (i
= 0; i
< 4; i
++, p
++) {
164 sector_t offs
, size
, next
;
166 if (!nr_sects(p
) || is_extended_partition(p
))
169 /* Check the 3rd and 4th entries -
170 these sometimes contain random garbage */
171 offs
= start_sect(p
)*sector_size
;
172 size
= nr_sects(p
)*sector_size
;
173 next
= this_sector
+ offs
;
175 if (offs
+ size
> this_size
)
177 if (next
< first_sector
)
179 if (next
+ size
> first_sector
+ first_size
)
183 put_partition(state
, state
->next
, next
, size
);
184 set_info(state
, state
->next
, disksig
);
185 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
186 state
->parts
[state
->next
].flags
= ADDPART_FLAG_RAID
;
188 if (++state
->next
== state
->limit
)
192 * Next, process the (first) extended partition, if present.
193 * (So far, there seems to be no reason to make
194 * parse_extended() recursive and allow a tree
195 * of extended partitions.)
196 * It should be a link to the next logical partition.
199 for (i
= 0; i
< 4; i
++, p
++)
200 if (nr_sects(p
) && is_extended_partition(p
))
203 goto done
; /* nothing left to do */
205 this_sector
= first_sector
+ start_sect(p
) * sector_size
;
206 this_size
= nr_sects(p
) * sector_size
;
207 put_dev_sector(sect
);
210 put_dev_sector(sect
);
213 /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
214 indicates linux swap. Be careful before believing this is Solaris. */
216 static void parse_solaris_x86(struct parsed_partitions
*state
,
217 sector_t offset
, sector_t size
, int origin
)
219 #ifdef CONFIG_SOLARIS_X86_PARTITION
221 struct solaris_x86_vtoc
*v
;
225 v
= read_part_sector(state
, offset
+ 1, §
);
228 if (le32_to_cpu(v
->v_sanity
) != SOLARIS_X86_VTOC_SANE
) {
229 put_dev_sector(sect
);
233 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 11 + 1];
235 snprintf(tmp
, sizeof(tmp
), " %s%d: <solaris:", state
->name
, origin
);
236 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
238 if (le32_to_cpu(v
->v_version
) != 1) {
241 snprintf(tmp
, sizeof(tmp
), " cannot handle version %d vtoc>\n",
242 le32_to_cpu(v
->v_version
));
243 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
244 put_dev_sector(sect
);
247 /* Ensure we can handle previous case of VTOC with 8 entries gracefully */
248 max_nparts
= le16_to_cpu(v
->v_nparts
) > 8 ? SOLARIS_X86_NUMSLICE
: 8;
249 for (i
= 0; i
< max_nparts
&& state
->next
< state
->limit
; i
++) {
250 struct solaris_x86_slice
*s
= &v
->v_slice
[i
];
251 char tmp
[3 + 10 + 1 + 1];
255 snprintf(tmp
, sizeof(tmp
), " [s%d]", i
);
256 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
257 /* solaris partitions are relative to current MS-DOS
258 * one; must add the offset of the current partition */
259 put_partition(state
, state
->next
++,
260 le32_to_cpu(s
->s_start
)+offset
,
261 le32_to_cpu(s
->s_size
));
263 put_dev_sector(sect
);
264 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
268 #if defined(CONFIG_BSD_DISKLABEL)
270 * Create devices for BSD partitions listed in a disklabel, under a
271 * dos-like partition. See parse_extended() for more information.
273 static void parse_bsd(struct parsed_partitions
*state
,
274 sector_t offset
, sector_t size
, int origin
, char *flavour
,
278 struct bsd_disklabel
*l
;
279 struct bsd_partition
*p
;
282 l
= read_part_sector(state
, offset
+ 1, §
);
285 if (le32_to_cpu(l
->d_magic
) != BSD_DISKMAGIC
) {
286 put_dev_sector(sect
);
290 snprintf(tmp
, sizeof(tmp
), " %s%d: <%s:", state
->name
, origin
, flavour
);
291 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
293 if (le16_to_cpu(l
->d_npartitions
) < max_partitions
)
294 max_partitions
= le16_to_cpu(l
->d_npartitions
);
295 for (p
= l
->d_partitions
; p
- l
->d_partitions
< max_partitions
; p
++) {
296 sector_t bsd_start
, bsd_size
;
298 if (state
->next
== state
->limit
)
300 if (p
->p_fstype
== BSD_FS_UNUSED
)
302 bsd_start
= le32_to_cpu(p
->p_offset
);
303 bsd_size
= le32_to_cpu(p
->p_size
);
304 if (memcmp(flavour
, "bsd\0", 4) == 0)
306 if (offset
== bsd_start
&& size
== bsd_size
)
307 /* full parent partition, we have it already */
309 if (offset
> bsd_start
|| offset
+size
< bsd_start
+bsd_size
) {
310 strlcat(state
->pp_buf
, "bad subpartition - ignored\n", PAGE_SIZE
);
313 put_partition(state
, state
->next
++, bsd_start
, bsd_size
);
315 put_dev_sector(sect
);
316 if (le16_to_cpu(l
->d_npartitions
) > max_partitions
) {
317 snprintf(tmp
, sizeof(tmp
), " (ignored %d more)",
318 le16_to_cpu(l
->d_npartitions
) - max_partitions
);
319 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
321 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
325 static void parse_freebsd(struct parsed_partitions
*state
,
326 sector_t offset
, sector_t size
, int origin
)
328 #ifdef CONFIG_BSD_DISKLABEL
329 parse_bsd(state
, offset
, size
, origin
, "bsd", BSD_MAXPARTITIONS
);
333 static void parse_netbsd(struct parsed_partitions
*state
,
334 sector_t offset
, sector_t size
, int origin
)
336 #ifdef CONFIG_BSD_DISKLABEL
337 parse_bsd(state
, offset
, size
, origin
, "netbsd", BSD_MAXPARTITIONS
);
341 static void parse_openbsd(struct parsed_partitions
*state
,
342 sector_t offset
, sector_t size
, int origin
)
344 #ifdef CONFIG_BSD_DISKLABEL
345 parse_bsd(state
, offset
, size
, origin
, "openbsd",
346 OPENBSD_MAXPARTITIONS
);
351 * Create devices for Unixware partitions listed in a disklabel, under a
352 * dos-like partition. See parse_extended() for more information.
354 static void parse_unixware(struct parsed_partitions
*state
,
355 sector_t offset
, sector_t size
, int origin
)
357 #ifdef CONFIG_UNIXWARE_DISKLABEL
359 struct unixware_disklabel
*l
;
360 struct unixware_slice
*p
;
362 l
= read_part_sector(state
, offset
+ 29, §
);
365 if (le32_to_cpu(l
->d_magic
) != UNIXWARE_DISKMAGIC
||
366 le32_to_cpu(l
->vtoc
.v_magic
) != UNIXWARE_DISKMAGIC2
) {
367 put_dev_sector(sect
);
371 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 12 + 1];
373 snprintf(tmp
, sizeof(tmp
), " %s%d: <unixware:", state
->name
, origin
);
374 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
376 p
= &l
->vtoc
.v_slice
[1];
377 /* I omit the 0th slice as it is the same as whole disk. */
378 while (p
- &l
->vtoc
.v_slice
[0] < UNIXWARE_NUMSLICE
) {
379 if (state
->next
== state
->limit
)
382 if (p
->s_label
!= UNIXWARE_FS_UNUSED
)
383 put_partition(state
, state
->next
++,
384 le32_to_cpu(p
->start_sect
),
385 le32_to_cpu(p
->nr_sects
));
388 put_dev_sector(sect
);
389 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
394 * Minix 2.0.0/2.0.2 subpartition support.
395 * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
396 * Rajeev V. Pillai <rajeevvp@yahoo.com>
398 static void parse_minix(struct parsed_partitions
*state
,
399 sector_t offset
, sector_t size
, int origin
)
401 #ifdef CONFIG_MINIX_SUBPARTITION
407 data
= read_part_sector(state
, offset
, §
);
411 p
= (struct partition
*)(data
+ 0x1be);
413 /* The first sector of a Minix partition can have either
414 * a secondary MBR describing its subpartitions, or
415 * the normal boot sector. */
416 if (msdos_magic_present(data
+ 510) &&
417 SYS_IND(p
) == MINIX_PARTITION
) { /* subpartition table present */
418 char tmp
[1 + BDEVNAME_SIZE
+ 10 + 9 + 1];
420 snprintf(tmp
, sizeof(tmp
), " %s%d: <minix:", state
->name
, origin
);
421 strlcat(state
->pp_buf
, tmp
, PAGE_SIZE
);
422 for (i
= 0; i
< MINIX_NR_SUBPARTITIONS
; i
++, p
++) {
423 if (state
->next
== state
->limit
)
425 /* add each partition in use */
426 if (SYS_IND(p
) == MINIX_PARTITION
)
427 put_partition(state
, state
->next
++,
428 start_sect(p
), nr_sects(p
));
430 strlcat(state
->pp_buf
, " >\n", PAGE_SIZE
);
432 put_dev_sector(sect
);
433 #endif /* CONFIG_MINIX_SUBPARTITION */
438 void (*parse
)(struct parsed_partitions
*, sector_t
, sector_t
, int);
440 {FREEBSD_PARTITION
, parse_freebsd
},
441 {NETBSD_PARTITION
, parse_netbsd
},
442 {OPENBSD_PARTITION
, parse_openbsd
},
443 {MINIX_PARTITION
, parse_minix
},
444 {UNIXWARE_PARTITION
, parse_unixware
},
445 {SOLARIS_X86_PARTITION
, parse_solaris_x86
},
446 {NEW_SOLARIS_X86_PARTITION
, parse_solaris_x86
},
450 int msdos_partition(struct parsed_partitions
*state
)
452 sector_t sector_size
= bdev_logical_block_size(state
->bdev
) / 512;
456 struct fat_boot_sector
*fb
;
460 data
= read_part_sector(state
, 0, §
);
465 * Note order! (some AIX disks, e.g. unbootable kind,
466 * have no MSDOS 55aa)
468 if (aix_magic_present(state
, data
)) {
469 put_dev_sector(sect
);
470 #ifdef CONFIG_AIX_PARTITION
471 return aix_partition(state
);
473 strlcat(state
->pp_buf
, " [AIX]", PAGE_SIZE
);
478 if (!msdos_magic_present(data
+ 510)) {
479 put_dev_sector(sect
);
484 * Now that the 55aa signature is present, this is probably
485 * either the boot sector of a FAT filesystem or a DOS-type
486 * partition table. Reject this in case the boot indicator
489 p
= (struct partition
*) (data
+ 0x1be);
490 for (slot
= 1; slot
<= 4; slot
++, p
++) {
491 if (p
->boot_ind
!= 0 && p
->boot_ind
!= 0x80) {
493 * Even without a valid boot inidicator value
494 * its still possible this is valid FAT filesystem
495 * without a partition table.
497 fb
= (struct fat_boot_sector
*) data
;
498 if (slot
== 1 && fb
->reserved
&& fb
->fats
499 && fat_valid_media(fb
->media
)) {
500 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
501 put_dev_sector(sect
);
504 put_dev_sector(sect
);
510 #ifdef CONFIG_EFI_PARTITION
511 p
= (struct partition
*) (data
+ 0x1be);
512 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
513 /* If this is an EFI GPT disk, msdos should ignore it. */
514 if (SYS_IND(p
) == EFI_PMBR_OSTYPE_EFI_GPT
) {
515 put_dev_sector(sect
);
520 p
= (struct partition
*) (data
+ 0x1be);
522 disksig
= le32_to_cpup((__le32
*)(data
+ 0x1b8));
525 * Look for partitions in two passes:
526 * First find the primary and DOS-type extended partitions.
527 * On the second pass look inside *BSD, Unixware and Solaris partitions.
531 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
532 sector_t start
= start_sect(p
)*sector_size
;
533 sector_t size
= nr_sects(p
)*sector_size
;
537 if (is_extended_partition(p
)) {
539 * prevent someone doing mkfs or mkswap on an
540 * extended partition, but leave room for LILO
541 * FIXME: this uses one logical sector for > 512b
542 * sector, although it may not be enough/proper.
546 n
= min(size
, max(sector_size
, n
));
547 put_partition(state
, slot
, start
, n
);
549 strlcat(state
->pp_buf
, " <", PAGE_SIZE
);
550 parse_extended(state
, start
, size
, disksig
);
551 strlcat(state
->pp_buf
, " >", PAGE_SIZE
);
554 put_partition(state
, slot
, start
, size
);
555 set_info(state
, slot
, disksig
);
556 if (SYS_IND(p
) == LINUX_RAID_PARTITION
)
557 state
->parts
[slot
].flags
= ADDPART_FLAG_RAID
;
558 if (SYS_IND(p
) == DM6_PARTITION
)
559 strlcat(state
->pp_buf
, "[DM]", PAGE_SIZE
);
560 if (SYS_IND(p
) == EZD_PARTITION
)
561 strlcat(state
->pp_buf
, "[EZD]", PAGE_SIZE
);
564 strlcat(state
->pp_buf
, "\n", PAGE_SIZE
);
566 /* second pass - output for each on a separate line */
567 p
= (struct partition
*) (0x1be + data
);
568 for (slot
= 1 ; slot
<= 4 ; slot
++, p
++) {
569 unsigned char id
= SYS_IND(p
);
575 for (n
= 0; subtypes
[n
].parse
&& id
!= subtypes
[n
].id
; n
++)
578 if (!subtypes
[n
].parse
)
580 subtypes
[n
].parse(state
, start_sect(p
) * sector_size
,
581 nr_sects(p
) * sector_size
, slot
);
583 put_dev_sector(sect
);