xref: /openbmc/linux/block/partitions/msdos.c (revision a08aa9bc)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29be96f3fSAl Viro /*
39be96f3fSAl Viro  *  fs/partitions/msdos.c
49be96f3fSAl Viro  *
59be96f3fSAl Viro  *  Code extracted from drivers/block/genhd.c
69be96f3fSAl Viro  *  Copyright (C) 1991-1998  Linus Torvalds
79be96f3fSAl Viro  *
89be96f3fSAl Viro  *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
99be96f3fSAl Viro  *  in the early extended-partition checks and added DM partitions
109be96f3fSAl Viro  *
119be96f3fSAl Viro  *  Support for DiskManager v6.0x added by Mark Lord,
129be96f3fSAl Viro  *  with information provided by OnTrack.  This now works for linux fdisk
139be96f3fSAl Viro  *  and LILO, as well as loadlin and bootln.  Note that disks other than
149be96f3fSAl Viro  *  /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
159be96f3fSAl Viro  *
169be96f3fSAl Viro  *  More flexible handling of extended partitions - aeb, 950831
179be96f3fSAl Viro  *
189be96f3fSAl Viro  *  Check partition table on IDE disks for common CHS translations
199be96f3fSAl Viro  *
209be96f3fSAl Viro  *  Re-organised Feb 1998 Russell King
213f4fc59cSChristoph Hellwig  *
223f4fc59cSChristoph Hellwig  *  BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
233f4fc59cSChristoph Hellwig  *  updated by Marc Espie <Marc.Espie@openbsd.org>
243f4fc59cSChristoph Hellwig  *
253f4fc59cSChristoph Hellwig  *  Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
263f4fc59cSChristoph Hellwig  *  and Krzysztof G. Baranowski <kgb@knm.org.pl>
279be96f3fSAl Viro  */
289be96f3fSAl Viro #include <linux/msdos_fs.h>
291442f76dSChristoph Hellwig #include <linux/msdos_partition.h>
309be96f3fSAl Viro 
319be96f3fSAl Viro #include "check.h"
329be96f3fSAl Viro #include "efi.h"
339be96f3fSAl Viro 
349be96f3fSAl Viro /*
359be96f3fSAl Viro  * Many architectures don't like unaligned accesses, while
369be96f3fSAl Viro  * the nr_sects and start_sect partition table entries are
379be96f3fSAl Viro  * at a 2 (mod 4) address.
389be96f3fSAl Viro  */
399be96f3fSAl Viro #include <asm/unaligned.h>
409be96f3fSAl Viro 
nr_sects(struct msdos_partition * p)411442f76dSChristoph Hellwig static inline sector_t nr_sects(struct msdos_partition *p)
429be96f3fSAl Viro {
439be96f3fSAl Viro 	return (sector_t)get_unaligned_le32(&p->nr_sects);
449be96f3fSAl Viro }
459be96f3fSAl Viro 
start_sect(struct msdos_partition * p)461442f76dSChristoph Hellwig static inline sector_t start_sect(struct msdos_partition *p)
479be96f3fSAl Viro {
489be96f3fSAl Viro 	return (sector_t)get_unaligned_le32(&p->start_sect);
499be96f3fSAl Viro }
509be96f3fSAl Viro 
is_extended_partition(struct msdos_partition * p)511442f76dSChristoph Hellwig static inline int is_extended_partition(struct msdos_partition *p)
529be96f3fSAl Viro {
531b177499SArnd Bergmann 	return (p->sys_ind == DOS_EXTENDED_PARTITION ||
541b177499SArnd Bergmann 		p->sys_ind == WIN98_EXTENDED_PARTITION ||
551b177499SArnd Bergmann 		p->sys_ind == LINUX_EXTENDED_PARTITION);
569be96f3fSAl Viro }
579be96f3fSAl Viro 
589be96f3fSAl Viro #define MSDOS_LABEL_MAGIC1	0x55
599be96f3fSAl Viro #define MSDOS_LABEL_MAGIC2	0xAA
609be96f3fSAl Viro 
619be96f3fSAl Viro static inline int
msdos_magic_present(unsigned char * p)629be96f3fSAl Viro msdos_magic_present(unsigned char *p)
639be96f3fSAl Viro {
649be96f3fSAl Viro 	return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
659be96f3fSAl Viro }
669be96f3fSAl Viro 
679be96f3fSAl Viro /* Value is EBCDIC 'IBMA' */
689be96f3fSAl Viro #define AIX_LABEL_MAGIC1	0xC9
699be96f3fSAl Viro #define AIX_LABEL_MAGIC2	0xC2
709be96f3fSAl Viro #define AIX_LABEL_MAGIC3	0xD4
719be96f3fSAl Viro #define AIX_LABEL_MAGIC4	0xC1
aix_magic_present(struct parsed_partitions * state,unsigned char * p)729be96f3fSAl Viro static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
739be96f3fSAl Viro {
741442f76dSChristoph Hellwig 	struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
759be96f3fSAl Viro 	Sector sect;
769be96f3fSAl Viro 	unsigned char *d;
779be96f3fSAl Viro 	int slot, ret = 0;
789be96f3fSAl Viro 
799be96f3fSAl Viro 	if (!(p[0] == AIX_LABEL_MAGIC1 &&
809be96f3fSAl Viro 		p[1] == AIX_LABEL_MAGIC2 &&
819be96f3fSAl Viro 		p[2] == AIX_LABEL_MAGIC3 &&
829be96f3fSAl Viro 		p[3] == AIX_LABEL_MAGIC4))
839be96f3fSAl Viro 		return 0;
84cb0ab526SChristoph Hellwig 
85cb0ab526SChristoph Hellwig 	/*
86cb0ab526SChristoph Hellwig 	 * Assume the partition table is valid if Linux partitions exists.
87cb0ab526SChristoph Hellwig 	 * Note that old Solaris/x86 partitions use the same indicator as
88cb0ab526SChristoph Hellwig 	 * Linux swap partitions, so we consider that a Linux partition as
89cb0ab526SChristoph Hellwig 	 * well.
90cb0ab526SChristoph Hellwig 	 */
919be96f3fSAl Viro 	for (slot = 1; slot <= 4; slot++, pt++) {
92cb0ab526SChristoph Hellwig 		if (pt->sys_ind == SOLARIS_X86_PARTITION ||
939be96f3fSAl Viro 		    pt->sys_ind == LINUX_RAID_PARTITION ||
949be96f3fSAl Viro 		    pt->sys_ind == LINUX_DATA_PARTITION ||
959be96f3fSAl Viro 		    pt->sys_ind == LINUX_LVM_PARTITION ||
969be96f3fSAl Viro 		    is_extended_partition(pt))
979be96f3fSAl Viro 			return 0;
989be96f3fSAl Viro 	}
999be96f3fSAl Viro 	d = read_part_sector(state, 7, &sect);
1009be96f3fSAl Viro 	if (d) {
1019be96f3fSAl Viro 		if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
1029be96f3fSAl Viro 			ret = 1;
1039be96f3fSAl Viro 		put_dev_sector(sect);
1041d04f3c6SPhilippe De Muyter 	}
1059be96f3fSAl Viro 	return ret;
1069be96f3fSAl Viro }
1079be96f3fSAl Viro 
set_info(struct parsed_partitions * state,int slot,u32 disksig)108d33b98fcSStephen Warren static void set_info(struct parsed_partitions *state, int slot,
109d33b98fcSStephen Warren 		     u32 disksig)
110d33b98fcSStephen Warren {
111d33b98fcSStephen Warren 	struct partition_meta_info *info = &state->parts[slot].info;
112d33b98fcSStephen Warren 
113d33b98fcSStephen Warren 	snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
114d33b98fcSStephen Warren 		 slot);
115d33b98fcSStephen Warren 	info->volname[0] = 0;
116d33b98fcSStephen Warren 	state->parts[slot].has_info = true;
117d33b98fcSStephen Warren }
118d33b98fcSStephen Warren 
1199be96f3fSAl Viro /*
1209be96f3fSAl Viro  * Create devices for each logical partition in an extended partition.
1219be96f3fSAl Viro  * The logical partitions form a linked list, with each entry being
1229be96f3fSAl Viro  * a partition table with two entries.  The first entry
1239be96f3fSAl Viro  * is the real data partition (with a start relative to the partition
1249be96f3fSAl Viro  * table start).  The second is a pointer to the next logical partition
1259be96f3fSAl Viro  * (with a start relative to the entire extended partition).
1269be96f3fSAl Viro  * We do not create a Linux partition for the partition tables, but
1279be96f3fSAl Viro  * only for the actual data partitions.
1289be96f3fSAl Viro  */
1299be96f3fSAl Viro 
parse_extended(struct parsed_partitions * state,sector_t first_sector,sector_t first_size,u32 disksig)1309be96f3fSAl Viro static void parse_extended(struct parsed_partitions *state,
131d33b98fcSStephen Warren 			   sector_t first_sector, sector_t first_size,
132d33b98fcSStephen Warren 			   u32 disksig)
1339be96f3fSAl Viro {
1341442f76dSChristoph Hellwig 	struct msdos_partition *p;
1359be96f3fSAl Viro 	Sector sect;
1369be96f3fSAl Viro 	unsigned char *data;
1379be96f3fSAl Viro 	sector_t this_sector, this_size;
138*a08aa9bcSChristoph Hellwig 	sector_t sector_size;
1399be96f3fSAl Viro 	int loopct = 0;		/* number of links followed
1409be96f3fSAl Viro 				   without finding a data partition */
1419be96f3fSAl Viro 	int i;
1429be96f3fSAl Viro 
143*a08aa9bcSChristoph Hellwig 	sector_size = queue_logical_block_size(state->disk->queue) / 512;
1449be96f3fSAl Viro 	this_sector = first_sector;
1459be96f3fSAl Viro 	this_size = first_size;
1469be96f3fSAl Viro 
1479be96f3fSAl Viro 	while (1) {
1489be96f3fSAl Viro 		if (++loopct > 100)
1499be96f3fSAl Viro 			return;
1509be96f3fSAl Viro 		if (state->next == state->limit)
1519be96f3fSAl Viro 			return;
1529be96f3fSAl Viro 		data = read_part_sector(state, this_sector, &sect);
1539be96f3fSAl Viro 		if (!data)
1549be96f3fSAl Viro 			return;
1559be96f3fSAl Viro 
1569be96f3fSAl Viro 		if (!msdos_magic_present(data + 510))
1579be96f3fSAl Viro 			goto done;
1589be96f3fSAl Viro 
1591442f76dSChristoph Hellwig 		p = (struct msdos_partition *) (data + 0x1be);
1609be96f3fSAl Viro 
1619be96f3fSAl Viro 		/*
1629be96f3fSAl Viro 		 * Usually, the first entry is the real data partition,
1639be96f3fSAl Viro 		 * the 2nd entry is the next extended partition, or empty,
1649be96f3fSAl Viro 		 * and the 3rd and 4th entries are unused.
1659be96f3fSAl Viro 		 * However, DRDOS sometimes has the extended partition as
1669be96f3fSAl Viro 		 * the first entry (when the data partition is empty),
1679be96f3fSAl Viro 		 * and OS/2 seems to use all four entries.
1689be96f3fSAl Viro 		 */
1699be96f3fSAl Viro 
1709be96f3fSAl Viro 		/*
1719be96f3fSAl Viro 		 * First process the data partition(s)
1729be96f3fSAl Viro 		 */
1739be96f3fSAl Viro 		for (i = 0; i < 4; i++, p++) {
1749be96f3fSAl Viro 			sector_t offs, size, next;
175dce14c23SFabian Frederick 
1769be96f3fSAl Viro 			if (!nr_sects(p) || is_extended_partition(p))
1779be96f3fSAl Viro 				continue;
1789be96f3fSAl Viro 
1799be96f3fSAl Viro 			/* Check the 3rd and 4th entries -
1809be96f3fSAl Viro 			   these sometimes contain random garbage */
1819be96f3fSAl Viro 			offs = start_sect(p)*sector_size;
1829be96f3fSAl Viro 			size = nr_sects(p)*sector_size;
1839be96f3fSAl Viro 			next = this_sector + offs;
1849be96f3fSAl Viro 			if (i >= 2) {
1859be96f3fSAl Viro 				if (offs + size > this_size)
1869be96f3fSAl Viro 					continue;
1879be96f3fSAl Viro 				if (next < first_sector)
1889be96f3fSAl Viro 					continue;
1899be96f3fSAl Viro 				if (next + size > first_sector + first_size)
1909be96f3fSAl Viro 					continue;
1919be96f3fSAl Viro 			}
1929be96f3fSAl Viro 
1939be96f3fSAl Viro 			put_partition(state, state->next, next, size);
194d33b98fcSStephen Warren 			set_info(state, state->next, disksig);
1951b177499SArnd Bergmann 			if (p->sys_ind == LINUX_RAID_PARTITION)
1969be96f3fSAl Viro 				state->parts[state->next].flags = ADDPART_FLAG_RAID;
1979be96f3fSAl Viro 			loopct = 0;
1989be96f3fSAl Viro 			if (++state->next == state->limit)
1999be96f3fSAl Viro 				goto done;
2009be96f3fSAl Viro 		}
2019be96f3fSAl Viro 		/*
2029be96f3fSAl Viro 		 * Next, process the (first) extended partition, if present.
2039be96f3fSAl Viro 		 * (So far, there seems to be no reason to make
2049be96f3fSAl Viro 		 *  parse_extended()  recursive and allow a tree
2059be96f3fSAl Viro 		 *  of extended partitions.)
2069be96f3fSAl Viro 		 * It should be a link to the next logical partition.
2079be96f3fSAl Viro 		 */
2089be96f3fSAl Viro 		p -= 4;
2099be96f3fSAl Viro 		for (i = 0; i < 4; i++, p++)
2109be96f3fSAl Viro 			if (nr_sects(p) && is_extended_partition(p))
2119be96f3fSAl Viro 				break;
2129be96f3fSAl Viro 		if (i == 4)
2139be96f3fSAl Viro 			goto done;	 /* nothing left to do */
2149be96f3fSAl Viro 
2159be96f3fSAl Viro 		this_sector = first_sector + start_sect(p) * sector_size;
2169be96f3fSAl Viro 		this_size = nr_sects(p) * sector_size;
2179be96f3fSAl Viro 		put_dev_sector(sect);
2189be96f3fSAl Viro 	}
2199be96f3fSAl Viro done:
2209be96f3fSAl Viro 	put_dev_sector(sect);
2219be96f3fSAl Viro }
2229be96f3fSAl Viro 
2233f4fc59cSChristoph Hellwig #define SOLARIS_X86_NUMSLICE	16
2243f4fc59cSChristoph Hellwig #define SOLARIS_X86_VTOC_SANE	(0x600DDEEEUL)
2253f4fc59cSChristoph Hellwig 
2263f4fc59cSChristoph Hellwig struct solaris_x86_slice {
2273f4fc59cSChristoph Hellwig 	__le16 s_tag;		/* ID tag of partition */
2283f4fc59cSChristoph Hellwig 	__le16 s_flag;		/* permission flags */
2293f4fc59cSChristoph Hellwig 	__le32 s_start;		/* start sector no of partition */
2303f4fc59cSChristoph Hellwig 	__le32 s_size;		/* # of blocks in partition */
2313f4fc59cSChristoph Hellwig };
2323f4fc59cSChristoph Hellwig 
2333f4fc59cSChristoph Hellwig struct solaris_x86_vtoc {
2343f4fc59cSChristoph Hellwig 	unsigned int v_bootinfo[3];	/* info needed by mboot */
2353f4fc59cSChristoph Hellwig 	__le32 v_sanity;		/* to verify vtoc sanity */
2363f4fc59cSChristoph Hellwig 	__le32 v_version;		/* layout version */
2373f4fc59cSChristoph Hellwig 	char	v_volume[8];		/* volume name */
2383f4fc59cSChristoph Hellwig 	__le16	v_sectorsz;		/* sector size in bytes */
2393f4fc59cSChristoph Hellwig 	__le16	v_nparts;		/* number of partitions */
2403f4fc59cSChristoph Hellwig 	unsigned int v_reserved[10];	/* free space */
2413f4fc59cSChristoph Hellwig 	struct solaris_x86_slice
2423f4fc59cSChristoph Hellwig 		v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
2433f4fc59cSChristoph Hellwig 	unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */
2443f4fc59cSChristoph Hellwig 	char	v_asciilabel[128];	/* for compatibility */
2453f4fc59cSChristoph Hellwig };
2463f4fc59cSChristoph Hellwig 
2479be96f3fSAl Viro /* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
2489be96f3fSAl Viro    indicates linux swap.  Be careful before believing this is Solaris. */
2499be96f3fSAl Viro 
parse_solaris_x86(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)2509be96f3fSAl Viro static void parse_solaris_x86(struct parsed_partitions *state,
2519be96f3fSAl Viro 			      sector_t offset, sector_t size, int origin)
2529be96f3fSAl Viro {
2539be96f3fSAl Viro #ifdef CONFIG_SOLARIS_X86_PARTITION
2549be96f3fSAl Viro 	Sector sect;
2559be96f3fSAl Viro 	struct solaris_x86_vtoc *v;
2569be96f3fSAl Viro 	int i;
2579be96f3fSAl Viro 	short max_nparts;
2589be96f3fSAl Viro 
2599be96f3fSAl Viro 	v = read_part_sector(state, offset + 1, &sect);
2609be96f3fSAl Viro 	if (!v)
2619be96f3fSAl Viro 		return;
2629be96f3fSAl Viro 	if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
2639be96f3fSAl Viro 		put_dev_sector(sect);
2649be96f3fSAl Viro 		return;
2659be96f3fSAl Viro 	}
2669be96f3fSAl Viro 	{
2679be96f3fSAl Viro 		char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
2689be96f3fSAl Viro 
2699be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
2709be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2719be96f3fSAl Viro 	}
2729be96f3fSAl Viro 	if (le32_to_cpu(v->v_version) != 1) {
2739be96f3fSAl Viro 		char tmp[64];
2749be96f3fSAl Viro 
2759be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), "  cannot handle version %d vtoc>\n",
2769be96f3fSAl Viro 			 le32_to_cpu(v->v_version));
2779be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2789be96f3fSAl Viro 		put_dev_sector(sect);
2799be96f3fSAl Viro 		return;
2809be96f3fSAl Viro 	}
2819be96f3fSAl Viro 	/* Ensure we can handle previous case of VTOC with 8 entries gracefully */
2829be96f3fSAl Viro 	max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
2839be96f3fSAl Viro 	for (i = 0; i < max_nparts && state->next < state->limit; i++) {
2849be96f3fSAl Viro 		struct solaris_x86_slice *s = &v->v_slice[i];
2859be96f3fSAl Viro 		char tmp[3 + 10 + 1 + 1];
2869be96f3fSAl Viro 
2879be96f3fSAl Viro 		if (s->s_size == 0)
2889be96f3fSAl Viro 			continue;
2899be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), " [s%d]", i);
2909be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2919be96f3fSAl Viro 		/* solaris partitions are relative to current MS-DOS
2929be96f3fSAl Viro 		 * one; must add the offset of the current partition */
2939be96f3fSAl Viro 		put_partition(state, state->next++,
2949be96f3fSAl Viro 				 le32_to_cpu(s->s_start)+offset,
2959be96f3fSAl Viro 				 le32_to_cpu(s->s_size));
2969be96f3fSAl Viro 	}
2979be96f3fSAl Viro 	put_dev_sector(sect);
2989be96f3fSAl Viro 	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
2999be96f3fSAl Viro #endif
3009be96f3fSAl Viro }
3019be96f3fSAl Viro 
3023f4fc59cSChristoph Hellwig /* check against BSD src/sys/sys/disklabel.h for consistency */
3033f4fc59cSChristoph Hellwig #define BSD_DISKMAGIC	(0x82564557UL)	/* The disk magic number */
3043f4fc59cSChristoph Hellwig #define BSD_MAXPARTITIONS	16
3053f4fc59cSChristoph Hellwig #define OPENBSD_MAXPARTITIONS	16
3063f4fc59cSChristoph Hellwig #define BSD_FS_UNUSED		0 /* disklabel unused partition entry ID */
3073f4fc59cSChristoph Hellwig struct bsd_disklabel {
3083f4fc59cSChristoph Hellwig 	__le32	d_magic;		/* the magic number */
3093f4fc59cSChristoph Hellwig 	__s16	d_type;			/* drive type */
3103f4fc59cSChristoph Hellwig 	__s16	d_subtype;		/* controller/d_type specific */
3113f4fc59cSChristoph Hellwig 	char	d_typename[16];		/* type name, e.g. "eagle" */
3123f4fc59cSChristoph Hellwig 	char	d_packname[16];		/* pack identifier */
3133f4fc59cSChristoph Hellwig 	__u32	d_secsize;		/* # of bytes per sector */
3143f4fc59cSChristoph Hellwig 	__u32	d_nsectors;		/* # of data sectors per track */
3153f4fc59cSChristoph Hellwig 	__u32	d_ntracks;		/* # of tracks per cylinder */
3163f4fc59cSChristoph Hellwig 	__u32	d_ncylinders;		/* # of data cylinders per unit */
3173f4fc59cSChristoph Hellwig 	__u32	d_secpercyl;		/* # of data sectors per cylinder */
3183f4fc59cSChristoph Hellwig 	__u32	d_secperunit;		/* # of data sectors per unit */
3193f4fc59cSChristoph Hellwig 	__u16	d_sparespertrack;	/* # of spare sectors per track */
3203f4fc59cSChristoph Hellwig 	__u16	d_sparespercyl;		/* # of spare sectors per cylinder */
3213f4fc59cSChristoph Hellwig 	__u32	d_acylinders;		/* # of alt. cylinders per unit */
3223f4fc59cSChristoph Hellwig 	__u16	d_rpm;			/* rotational speed */
3233f4fc59cSChristoph Hellwig 	__u16	d_interleave;		/* hardware sector interleave */
3243f4fc59cSChristoph Hellwig 	__u16	d_trackskew;		/* sector 0 skew, per track */
3253f4fc59cSChristoph Hellwig 	__u16	d_cylskew;		/* sector 0 skew, per cylinder */
3263f4fc59cSChristoph Hellwig 	__u32	d_headswitch;		/* head switch time, usec */
3273f4fc59cSChristoph Hellwig 	__u32	d_trkseek;		/* track-to-track seek, usec */
3283f4fc59cSChristoph Hellwig 	__u32	d_flags;		/* generic flags */
3293f4fc59cSChristoph Hellwig #define NDDATA 5
3303f4fc59cSChristoph Hellwig 	__u32	d_drivedata[NDDATA];	/* drive-type specific information */
3313f4fc59cSChristoph Hellwig #define NSPARE 5
3323f4fc59cSChristoph Hellwig 	__u32	d_spare[NSPARE];	/* reserved for future use */
3333f4fc59cSChristoph Hellwig 	__le32	d_magic2;		/* the magic number (again) */
3343f4fc59cSChristoph Hellwig 	__le16	d_checksum;		/* xor of data incl. partitions */
3353f4fc59cSChristoph Hellwig 
3363f4fc59cSChristoph Hellwig 			/* filesystem and partition information: */
3373f4fc59cSChristoph Hellwig 	__le16	d_npartitions;		/* number of partitions in following */
3383f4fc59cSChristoph Hellwig 	__le32	d_bbsize;		/* size of boot area at sn0, bytes */
3393f4fc59cSChristoph Hellwig 	__le32	d_sbsize;		/* max size of fs superblock, bytes */
3403f4fc59cSChristoph Hellwig 	struct	bsd_partition {		/* the partition table */
3413f4fc59cSChristoph Hellwig 		__le32	p_size;		/* number of sectors in partition */
3423f4fc59cSChristoph Hellwig 		__le32	p_offset;	/* starting sector */
3433f4fc59cSChristoph Hellwig 		__le32	p_fsize;	/* filesystem basic fragment size */
3443f4fc59cSChristoph Hellwig 		__u8	p_fstype;	/* filesystem type, see below */
3453f4fc59cSChristoph Hellwig 		__u8	p_frag;		/* filesystem fragments per block */
3463f4fc59cSChristoph Hellwig 		__le16	p_cpg;		/* filesystem cylinders per group */
3473f4fc59cSChristoph Hellwig 	} d_partitions[BSD_MAXPARTITIONS];	/* actually may be more */
3483f4fc59cSChristoph Hellwig };
3493f4fc59cSChristoph Hellwig 
3509be96f3fSAl Viro #if defined(CONFIG_BSD_DISKLABEL)
3519be96f3fSAl Viro /*
3529be96f3fSAl Viro  * Create devices for BSD partitions listed in a disklabel, under a
3539be96f3fSAl Viro  * dos-like partition. See parse_extended() for more information.
3549be96f3fSAl Viro  */
parse_bsd(struct parsed_partitions * state,sector_t offset,sector_t size,int origin,char * flavour,int max_partitions)3559be96f3fSAl Viro static void parse_bsd(struct parsed_partitions *state,
3569be96f3fSAl Viro 		      sector_t offset, sector_t size, int origin, char *flavour,
3579be96f3fSAl Viro 		      int max_partitions)
3589be96f3fSAl Viro {
3599be96f3fSAl Viro 	Sector sect;
3609be96f3fSAl Viro 	struct bsd_disklabel *l;
3619be96f3fSAl Viro 	struct bsd_partition *p;
3629be96f3fSAl Viro 	char tmp[64];
3639be96f3fSAl Viro 
3649be96f3fSAl Viro 	l = read_part_sector(state, offset + 1, &sect);
3659be96f3fSAl Viro 	if (!l)
3669be96f3fSAl Viro 		return;
3679be96f3fSAl Viro 	if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
3689be96f3fSAl Viro 		put_dev_sector(sect);
3699be96f3fSAl Viro 		return;
3709be96f3fSAl Viro 	}
3719be96f3fSAl Viro 
3729be96f3fSAl Viro 	snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
3739be96f3fSAl Viro 	strlcat(state->pp_buf, tmp, PAGE_SIZE);
3749be96f3fSAl Viro 
3759be96f3fSAl Viro 	if (le16_to_cpu(l->d_npartitions) < max_partitions)
3769be96f3fSAl Viro 		max_partitions = le16_to_cpu(l->d_npartitions);
3779be96f3fSAl Viro 	for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
3789be96f3fSAl Viro 		sector_t bsd_start, bsd_size;
3799be96f3fSAl Viro 
3809be96f3fSAl Viro 		if (state->next == state->limit)
3819be96f3fSAl Viro 			break;
3829be96f3fSAl Viro 		if (p->p_fstype == BSD_FS_UNUSED)
3839be96f3fSAl Viro 			continue;
3849be96f3fSAl Viro 		bsd_start = le32_to_cpu(p->p_offset);
3859be96f3fSAl Viro 		bsd_size = le32_to_cpu(p->p_size);
3865f15684bSRichard Narron 		/* FreeBSD has relative offset if C partition offset is zero */
3875f15684bSRichard Narron 		if (memcmp(flavour, "bsd\0", 4) == 0 &&
3885f15684bSRichard Narron 		    le32_to_cpu(l->d_partitions[2].p_offset) == 0)
38922322035SRichard 			bsd_start += offset;
3909be96f3fSAl Viro 		if (offset == bsd_start && size == bsd_size)
3919be96f3fSAl Viro 			/* full parent partition, we have it already */
3929be96f3fSAl Viro 			continue;
3939be96f3fSAl Viro 		if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
3949be96f3fSAl Viro 			strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
3959be96f3fSAl Viro 			continue;
3969be96f3fSAl Viro 		}
3979be96f3fSAl Viro 		put_partition(state, state->next++, bsd_start, bsd_size);
3989be96f3fSAl Viro 	}
3999be96f3fSAl Viro 	put_dev_sector(sect);
4009be96f3fSAl Viro 	if (le16_to_cpu(l->d_npartitions) > max_partitions) {
4019be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), " (ignored %d more)",
4029be96f3fSAl Viro 			 le16_to_cpu(l->d_npartitions) - max_partitions);
4039be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
4049be96f3fSAl Viro 	}
4059be96f3fSAl Viro 	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
4069be96f3fSAl Viro }
4079be96f3fSAl Viro #endif
4089be96f3fSAl Viro 
parse_freebsd(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)4099be96f3fSAl Viro static void parse_freebsd(struct parsed_partitions *state,
4109be96f3fSAl Viro 			  sector_t offset, sector_t size, int origin)
4119be96f3fSAl Viro {
4129be96f3fSAl Viro #ifdef CONFIG_BSD_DISKLABEL
4139be96f3fSAl Viro 	parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
4149be96f3fSAl Viro #endif
4159be96f3fSAl Viro }
4169be96f3fSAl Viro 
parse_netbsd(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)4179be96f3fSAl Viro static void parse_netbsd(struct parsed_partitions *state,
4189be96f3fSAl Viro 			 sector_t offset, sector_t size, int origin)
4199be96f3fSAl Viro {
4209be96f3fSAl Viro #ifdef CONFIG_BSD_DISKLABEL
4219be96f3fSAl Viro 	parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
4229be96f3fSAl Viro #endif
4239be96f3fSAl Viro }
4249be96f3fSAl Viro 
parse_openbsd(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)4259be96f3fSAl Viro static void parse_openbsd(struct parsed_partitions *state,
4269be96f3fSAl Viro 			  sector_t offset, sector_t size, int origin)
4279be96f3fSAl Viro {
4289be96f3fSAl Viro #ifdef CONFIG_BSD_DISKLABEL
4299be96f3fSAl Viro 	parse_bsd(state, offset, size, origin, "openbsd",
4309be96f3fSAl Viro 		  OPENBSD_MAXPARTITIONS);
4319be96f3fSAl Viro #endif
4329be96f3fSAl Viro }
4339be96f3fSAl Viro 
4343f4fc59cSChristoph Hellwig #define UNIXWARE_DISKMAGIC     (0xCA5E600DUL)	/* The disk magic number */
4353f4fc59cSChristoph Hellwig #define UNIXWARE_DISKMAGIC2    (0x600DDEEEUL)	/* The slice table magic nr */
4363f4fc59cSChristoph Hellwig #define UNIXWARE_NUMSLICE      16
4373f4fc59cSChristoph Hellwig #define UNIXWARE_FS_UNUSED     0		/* Unused slice entry ID */
4383f4fc59cSChristoph Hellwig 
4393f4fc59cSChristoph Hellwig struct unixware_slice {
4403f4fc59cSChristoph Hellwig 	__le16   s_label;	/* label */
4413f4fc59cSChristoph Hellwig 	__le16   s_flags;	/* permission flags */
4423f4fc59cSChristoph Hellwig 	__le32   start_sect;	/* starting sector */
4433f4fc59cSChristoph Hellwig 	__le32   nr_sects;	/* number of sectors in slice */
4443f4fc59cSChristoph Hellwig };
4453f4fc59cSChristoph Hellwig 
4463f4fc59cSChristoph Hellwig struct unixware_disklabel {
4473f4fc59cSChristoph Hellwig 	__le32	d_type;			/* drive type */
4483f4fc59cSChristoph Hellwig 	__le32	d_magic;		/* the magic number */
4493f4fc59cSChristoph Hellwig 	__le32	d_version;		/* version number */
4503f4fc59cSChristoph Hellwig 	char	d_serial[12];		/* serial number of the device */
4513f4fc59cSChristoph Hellwig 	__le32	d_ncylinders;		/* # of data cylinders per device */
4523f4fc59cSChristoph Hellwig 	__le32	d_ntracks;		/* # of tracks per cylinder */
4533f4fc59cSChristoph Hellwig 	__le32	d_nsectors;		/* # of data sectors per track */
4543f4fc59cSChristoph Hellwig 	__le32	d_secsize;		/* # of bytes per sector */
4553f4fc59cSChristoph Hellwig 	__le32	d_part_start;		/* # of first sector of this partition*/
4563f4fc59cSChristoph Hellwig 	__le32	d_unknown1[12];		/* ? */
4573f4fc59cSChristoph Hellwig 	__le32	d_alt_tbl;		/* byte offset of alternate table */
4583f4fc59cSChristoph Hellwig 	__le32	d_alt_len;		/* byte length of alternate table */
4593f4fc59cSChristoph Hellwig 	__le32	d_phys_cyl;		/* # of physical cylinders per device */
4603f4fc59cSChristoph Hellwig 	__le32	d_phys_trk;		/* # of physical tracks per cylinder */
4613f4fc59cSChristoph Hellwig 	__le32	d_phys_sec;		/* # of physical sectors per track */
4623f4fc59cSChristoph Hellwig 	__le32	d_phys_bytes;		/* # of physical bytes per sector */
4633f4fc59cSChristoph Hellwig 	__le32	d_unknown2;		/* ? */
4643f4fc59cSChristoph Hellwig 	__le32	d_unknown3;		/* ? */
4653f4fc59cSChristoph Hellwig 	__le32	d_pad[8];		/* pad */
4663f4fc59cSChristoph Hellwig 
4673f4fc59cSChristoph Hellwig 	struct unixware_vtoc {
4683f4fc59cSChristoph Hellwig 		__le32	v_magic;		/* the magic number */
4693f4fc59cSChristoph Hellwig 		__le32	v_version;		/* version number */
4703f4fc59cSChristoph Hellwig 		char	v_name[8];		/* volume name */
4713f4fc59cSChristoph Hellwig 		__le16	v_nslices;		/* # of slices */
4723f4fc59cSChristoph Hellwig 		__le16	v_unknown1;		/* ? */
4733f4fc59cSChristoph Hellwig 		__le32	v_reserved[10];		/* reserved */
4743f4fc59cSChristoph Hellwig 		struct unixware_slice
4753f4fc59cSChristoph Hellwig 			v_slice[UNIXWARE_NUMSLICE];	/* slice headers */
4763f4fc59cSChristoph Hellwig 	} vtoc;
4773f4fc59cSChristoph Hellwig };  /* 408 */
4783f4fc59cSChristoph Hellwig 
4799be96f3fSAl Viro /*
4809be96f3fSAl Viro  * Create devices for Unixware partitions listed in a disklabel, under a
4819be96f3fSAl Viro  * dos-like partition. See parse_extended() for more information.
4829be96f3fSAl Viro  */
parse_unixware(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)4839be96f3fSAl Viro static void parse_unixware(struct parsed_partitions *state,
4849be96f3fSAl Viro 			   sector_t offset, sector_t size, int origin)
4859be96f3fSAl Viro {
4869be96f3fSAl Viro #ifdef CONFIG_UNIXWARE_DISKLABEL
4879be96f3fSAl Viro 	Sector sect;
4889be96f3fSAl Viro 	struct unixware_disklabel *l;
4899be96f3fSAl Viro 	struct unixware_slice *p;
4909be96f3fSAl Viro 
4919be96f3fSAl Viro 	l = read_part_sector(state, offset + 29, &sect);
4929be96f3fSAl Viro 	if (!l)
4939be96f3fSAl Viro 		return;
4949be96f3fSAl Viro 	if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
4959be96f3fSAl Viro 	    le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
4969be96f3fSAl Viro 		put_dev_sector(sect);
4979be96f3fSAl Viro 		return;
4989be96f3fSAl Viro 	}
4999be96f3fSAl Viro 	{
5009be96f3fSAl Viro 		char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
5019be96f3fSAl Viro 
5029be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
5039be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
5049be96f3fSAl Viro 	}
5059be96f3fSAl Viro 	p = &l->vtoc.v_slice[1];
5069be96f3fSAl Viro 	/* I omit the 0th slice as it is the same as whole disk. */
5079be96f3fSAl Viro 	while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
5089be96f3fSAl Viro 		if (state->next == state->limit)
5099be96f3fSAl Viro 			break;
5109be96f3fSAl Viro 
5119be96f3fSAl Viro 		if (p->s_label != UNIXWARE_FS_UNUSED)
5129be96f3fSAl Viro 			put_partition(state, state->next++,
5139be96f3fSAl Viro 				      le32_to_cpu(p->start_sect),
5149be96f3fSAl Viro 				      le32_to_cpu(p->nr_sects));
5159be96f3fSAl Viro 		p++;
5169be96f3fSAl Viro 	}
5179be96f3fSAl Viro 	put_dev_sector(sect);
5189be96f3fSAl Viro 	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
5199be96f3fSAl Viro #endif
5209be96f3fSAl Viro }
5219be96f3fSAl Viro 
5223f4fc59cSChristoph Hellwig #define MINIX_NR_SUBPARTITIONS  4
5233f4fc59cSChristoph Hellwig 
5249be96f3fSAl Viro /*
5259be96f3fSAl Viro  * Minix 2.0.0/2.0.2 subpartition support.
5269be96f3fSAl Viro  * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
5279be96f3fSAl Viro  * Rajeev V. Pillai    <rajeevvp@yahoo.com>
5289be96f3fSAl Viro  */
parse_minix(struct parsed_partitions * state,sector_t offset,sector_t size,int origin)5299be96f3fSAl Viro static void parse_minix(struct parsed_partitions *state,
5309be96f3fSAl Viro 			sector_t offset, sector_t size, int origin)
5319be96f3fSAl Viro {
5329be96f3fSAl Viro #ifdef CONFIG_MINIX_SUBPARTITION
5339be96f3fSAl Viro 	Sector sect;
5349be96f3fSAl Viro 	unsigned char *data;
5351442f76dSChristoph Hellwig 	struct msdos_partition *p;
5369be96f3fSAl Viro 	int i;
5379be96f3fSAl Viro 
5389be96f3fSAl Viro 	data = read_part_sector(state, offset, &sect);
5399be96f3fSAl Viro 	if (!data)
5409be96f3fSAl Viro 		return;
5419be96f3fSAl Viro 
5421442f76dSChristoph Hellwig 	p = (struct msdos_partition *)(data + 0x1be);
5439be96f3fSAl Viro 
5449be96f3fSAl Viro 	/* The first sector of a Minix partition can have either
5459be96f3fSAl Viro 	 * a secondary MBR describing its subpartitions, or
5469be96f3fSAl Viro 	 * the normal boot sector. */
5479be96f3fSAl Viro 	if (msdos_magic_present(data + 510) &&
5481b177499SArnd Bergmann 	    p->sys_ind == MINIX_PARTITION) { /* subpartition table present */
5499be96f3fSAl Viro 		char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
5509be96f3fSAl Viro 
5519be96f3fSAl Viro 		snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
5529be96f3fSAl Viro 		strlcat(state->pp_buf, tmp, PAGE_SIZE);
5539be96f3fSAl Viro 		for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
5549be96f3fSAl Viro 			if (state->next == state->limit)
5559be96f3fSAl Viro 				break;
5569be96f3fSAl Viro 			/* add each partition in use */
5571b177499SArnd Bergmann 			if (p->sys_ind == MINIX_PARTITION)
5589be96f3fSAl Viro 				put_partition(state, state->next++,
5599be96f3fSAl Viro 					      start_sect(p), nr_sects(p));
5609be96f3fSAl Viro 		}
5619be96f3fSAl Viro 		strlcat(state->pp_buf, " >\n", PAGE_SIZE);
5629be96f3fSAl Viro 	}
5639be96f3fSAl Viro 	put_dev_sector(sect);
5649be96f3fSAl Viro #endif /* CONFIG_MINIX_SUBPARTITION */
5659be96f3fSAl Viro }
5669be96f3fSAl Viro 
5679be96f3fSAl Viro static struct {
5689be96f3fSAl Viro 	unsigned char id;
5699be96f3fSAl Viro 	void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
5709be96f3fSAl Viro } subtypes[] = {
5719be96f3fSAl Viro 	{FREEBSD_PARTITION, parse_freebsd},
5729be96f3fSAl Viro 	{NETBSD_PARTITION, parse_netbsd},
5739be96f3fSAl Viro 	{OPENBSD_PARTITION, parse_openbsd},
5749be96f3fSAl Viro 	{MINIX_PARTITION, parse_minix},
5759be96f3fSAl Viro 	{UNIXWARE_PARTITION, parse_unixware},
5769be96f3fSAl Viro 	{SOLARIS_X86_PARTITION, parse_solaris_x86},
5779be96f3fSAl Viro 	{NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
5789be96f3fSAl Viro 	{0, NULL},
5799be96f3fSAl Viro };
5809be96f3fSAl Viro 
msdos_partition(struct parsed_partitions * state)5819be96f3fSAl Viro int msdos_partition(struct parsed_partitions *state)
5829be96f3fSAl Viro {
583*a08aa9bcSChristoph Hellwig 	sector_t sector_size;
5849be96f3fSAl Viro 	Sector sect;
5859be96f3fSAl Viro 	unsigned char *data;
5861442f76dSChristoph Hellwig 	struct msdos_partition *p;
5879be96f3fSAl Viro 	struct fat_boot_sector *fb;
5889be96f3fSAl Viro 	int slot;
589d33b98fcSStephen Warren 	u32 disksig;
5909be96f3fSAl Viro 
591*a08aa9bcSChristoph Hellwig 	sector_size = queue_logical_block_size(state->disk->queue) / 512;
5929be96f3fSAl Viro 	data = read_part_sector(state, 0, &sect);
5939be96f3fSAl Viro 	if (!data)
5949be96f3fSAl Viro 		return -1;
5959be96f3fSAl Viro 
59686ee8ba6SPhilippe De Muyter 	/*
59786ee8ba6SPhilippe De Muyter 	 * Note order! (some AIX disks, e.g. unbootable kind,
59886ee8ba6SPhilippe De Muyter 	 * have no MSDOS 55aa)
59986ee8ba6SPhilippe De Muyter 	 */
6009be96f3fSAl Viro 	if (aix_magic_present(state, data)) {
6019be96f3fSAl Viro 		put_dev_sector(sect);
602f8f06603SPhilippe De Muyter #ifdef CONFIG_AIX_PARTITION
603f8f06603SPhilippe De Muyter 		return aix_partition(state);
604f8f06603SPhilippe De Muyter #else
6059be96f3fSAl Viro 		strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
6069be96f3fSAl Viro 		return 0;
607f8f06603SPhilippe De Muyter #endif
6089be96f3fSAl Viro 	}
6099be96f3fSAl Viro 
61086ee8ba6SPhilippe De Muyter 	if (!msdos_magic_present(data + 510)) {
61186ee8ba6SPhilippe De Muyter 		put_dev_sector(sect);
61286ee8ba6SPhilippe De Muyter 		return 0;
61386ee8ba6SPhilippe De Muyter 	}
61486ee8ba6SPhilippe De Muyter 
6159be96f3fSAl Viro 	/*
6169be96f3fSAl Viro 	 * Now that the 55aa signature is present, this is probably
6179be96f3fSAl Viro 	 * either the boot sector of a FAT filesystem or a DOS-type
6189be96f3fSAl Viro 	 * partition table. Reject this in case the boot indicator
6199be96f3fSAl Viro 	 * is not 0 or 0x80.
6209be96f3fSAl Viro 	 */
6211442f76dSChristoph Hellwig 	p = (struct msdos_partition *) (data + 0x1be);
6229be96f3fSAl Viro 	for (slot = 1; slot <= 4; slot++, p++) {
6239be96f3fSAl Viro 		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
6249be96f3fSAl Viro 			/*
625ddcc5c54SThomas Bracht Laumann Jespersen 			 * Even without a valid boot indicator value
6269be96f3fSAl Viro 			 * its still possible this is valid FAT filesystem
6279be96f3fSAl Viro 			 * without a partition table.
6289be96f3fSAl Viro 			 */
6299be96f3fSAl Viro 			fb = (struct fat_boot_sector *) data;
6309be96f3fSAl Viro 			if (slot == 1 && fb->reserved && fb->fats
6319be96f3fSAl Viro 				&& fat_valid_media(fb->media)) {
6329be96f3fSAl Viro 				strlcat(state->pp_buf, "\n", PAGE_SIZE);
6339be96f3fSAl Viro 				put_dev_sector(sect);
6349be96f3fSAl Viro 				return 1;
6359be96f3fSAl Viro 			} else {
6369be96f3fSAl Viro 				put_dev_sector(sect);
6379be96f3fSAl Viro 				return 0;
6389be96f3fSAl Viro 			}
6399be96f3fSAl Viro 		}
6409be96f3fSAl Viro 	}
6419be96f3fSAl Viro 
6429be96f3fSAl Viro #ifdef CONFIG_EFI_PARTITION
6431442f76dSChristoph Hellwig 	p = (struct msdos_partition *) (data + 0x1be);
6449be96f3fSAl Viro 	for (slot = 1 ; slot <= 4 ; slot++, p++) {
6459be96f3fSAl Viro 		/* If this is an EFI GPT disk, msdos should ignore it. */
6461b177499SArnd Bergmann 		if (p->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT) {
6479be96f3fSAl Viro 			put_dev_sector(sect);
6489be96f3fSAl Viro 			return 0;
6499be96f3fSAl Viro 		}
6509be96f3fSAl Viro 	}
6519be96f3fSAl Viro #endif
6521442f76dSChristoph Hellwig 	p = (struct msdos_partition *) (data + 0x1be);
6539be96f3fSAl Viro 
654d33b98fcSStephen Warren 	disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
655d33b98fcSStephen Warren 
6569be96f3fSAl Viro 	/*
6579be96f3fSAl Viro 	 * Look for partitions in two passes:
6589be96f3fSAl Viro 	 * First find the primary and DOS-type extended partitions.
6599be96f3fSAl Viro 	 * On the second pass look inside *BSD, Unixware and Solaris partitions.
6609be96f3fSAl Viro 	 */
6619be96f3fSAl Viro 
6629be96f3fSAl Viro 	state->next = 5;
6639be96f3fSAl Viro 	for (slot = 1 ; slot <= 4 ; slot++, p++) {
6649be96f3fSAl Viro 		sector_t start = start_sect(p)*sector_size;
6659be96f3fSAl Viro 		sector_t size = nr_sects(p)*sector_size;
666dce14c23SFabian Frederick 
6679be96f3fSAl Viro 		if (!size)
6689be96f3fSAl Viro 			continue;
6699be96f3fSAl Viro 		if (is_extended_partition(p)) {
6709be96f3fSAl Viro 			/*
6719be96f3fSAl Viro 			 * prevent someone doing mkfs or mkswap on an
6729be96f3fSAl Viro 			 * extended partition, but leave room for LILO
6739be96f3fSAl Viro 			 * FIXME: this uses one logical sector for > 512b
6749be96f3fSAl Viro 			 * sector, although it may not be enough/proper.
6759be96f3fSAl Viro 			 */
6769be96f3fSAl Viro 			sector_t n = 2;
677dce14c23SFabian Frederick 
6789be96f3fSAl Viro 			n = min(size, max(sector_size, n));
6799be96f3fSAl Viro 			put_partition(state, slot, start, n);
6809be96f3fSAl Viro 
6819be96f3fSAl Viro 			strlcat(state->pp_buf, " <", PAGE_SIZE);
682d33b98fcSStephen Warren 			parse_extended(state, start, size, disksig);
6839be96f3fSAl Viro 			strlcat(state->pp_buf, " >", PAGE_SIZE);
6849be96f3fSAl Viro 			continue;
6859be96f3fSAl Viro 		}
6869be96f3fSAl Viro 		put_partition(state, slot, start, size);
687d33b98fcSStephen Warren 		set_info(state, slot, disksig);
6881b177499SArnd Bergmann 		if (p->sys_ind == LINUX_RAID_PARTITION)
6899be96f3fSAl Viro 			state->parts[slot].flags = ADDPART_FLAG_RAID;
6901b177499SArnd Bergmann 		if (p->sys_ind == DM6_PARTITION)
6919be96f3fSAl Viro 			strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
6921b177499SArnd Bergmann 		if (p->sys_ind == EZD_PARTITION)
6939be96f3fSAl Viro 			strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
6949be96f3fSAl Viro 	}
6959be96f3fSAl Viro 
6969be96f3fSAl Viro 	strlcat(state->pp_buf, "\n", PAGE_SIZE);
6979be96f3fSAl Viro 
6989be96f3fSAl Viro 	/* second pass - output for each on a separate line */
6991442f76dSChristoph Hellwig 	p = (struct msdos_partition *) (0x1be + data);
7009be96f3fSAl Viro 	for (slot = 1 ; slot <= 4 ; slot++, p++) {
7011b177499SArnd Bergmann 		unsigned char id = p->sys_ind;
7029be96f3fSAl Viro 		int n;
7039be96f3fSAl Viro 
7049be96f3fSAl Viro 		if (!nr_sects(p))
7059be96f3fSAl Viro 			continue;
7069be96f3fSAl Viro 
7079be96f3fSAl Viro 		for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
7089be96f3fSAl Viro 			;
7099be96f3fSAl Viro 
7109be96f3fSAl Viro 		if (!subtypes[n].parse)
7119be96f3fSAl Viro 			continue;
7129be96f3fSAl Viro 		subtypes[n].parse(state, start_sect(p) * sector_size,
7139be96f3fSAl Viro 				  nr_sects(p) * sector_size, slot);
7149be96f3fSAl Viro 	}
7159be96f3fSAl Viro 	put_dev_sector(sect);
7169be96f3fSAl Viro 	return 1;
7179be96f3fSAl Viro }
718