18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  fs/partitions/msdos.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Code extracted from drivers/block/genhd.c
68c2ecf20Sopenharmony_ci *  Copyright (C) 1991-1998  Linus Torvalds
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *  Thanks to Branko Lankester, lankeste@fwi.uva.nl, who found a bug
98c2ecf20Sopenharmony_ci *  in the early extended-partition checks and added DM partitions
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  Support for DiskManager v6.0x added by Mark Lord,
128c2ecf20Sopenharmony_ci *  with information provided by OnTrack.  This now works for linux fdisk
138c2ecf20Sopenharmony_ci *  and LILO, as well as loadlin and bootln.  Note that disks other than
148c2ecf20Sopenharmony_ci *  /dev/hda *must* have a "DOS" type 0x51 partition in the first slot (hda1).
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *  More flexible handling of extended partitions - aeb, 950831
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *  Check partition table on IDE disks for common CHS translations
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *  Re-organised Feb 1998 Russell King
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci *  BSD disklabel support by Yossi Gottlieb <yogo@math.tau.ac.il>
238c2ecf20Sopenharmony_ci *  updated by Marc Espie <Marc.Espie@openbsd.org>
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci *  Unixware slices support by Andrzej Krzysztofowicz <ankry@mif.pg.gda.pl>
268c2ecf20Sopenharmony_ci *  and Krzysztof G. Baranowski <kgb@knm.org.pl>
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#include <linux/msdos_fs.h>
298c2ecf20Sopenharmony_ci#include <linux/msdos_partition.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "check.h"
328c2ecf20Sopenharmony_ci#include "efi.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * Many architectures don't like unaligned accesses, while
368c2ecf20Sopenharmony_ci * the nr_sects and start_sect partition table entries are
378c2ecf20Sopenharmony_ci * at a 2 (mod 4) address.
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic inline sector_t nr_sects(struct msdos_partition *p)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	return (sector_t)get_unaligned_le32(&p->nr_sects);
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic inline sector_t start_sect(struct msdos_partition *p)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	return (sector_t)get_unaligned_le32(&p->start_sect);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic inline int is_extended_partition(struct msdos_partition *p)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	return (p->sys_ind == DOS_EXTENDED_PARTITION ||
548c2ecf20Sopenharmony_ci		p->sys_ind == WIN98_EXTENDED_PARTITION ||
558c2ecf20Sopenharmony_ci		p->sys_ind == LINUX_EXTENDED_PARTITION);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#define MSDOS_LABEL_MAGIC1	0x55
598c2ecf20Sopenharmony_ci#define MSDOS_LABEL_MAGIC2	0xAA
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic inline int
628c2ecf20Sopenharmony_cimsdos_magic_present(unsigned char *p)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	return (p[0] == MSDOS_LABEL_MAGIC1 && p[1] == MSDOS_LABEL_MAGIC2);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* Value is EBCDIC 'IBMA' */
688c2ecf20Sopenharmony_ci#define AIX_LABEL_MAGIC1	0xC9
698c2ecf20Sopenharmony_ci#define AIX_LABEL_MAGIC2	0xC2
708c2ecf20Sopenharmony_ci#define AIX_LABEL_MAGIC3	0xD4
718c2ecf20Sopenharmony_ci#define AIX_LABEL_MAGIC4	0xC1
728c2ecf20Sopenharmony_cistatic int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct msdos_partition *pt = (struct msdos_partition *) (p + 0x1be);
758c2ecf20Sopenharmony_ci	Sector sect;
768c2ecf20Sopenharmony_ci	unsigned char *d;
778c2ecf20Sopenharmony_ci	int slot, ret = 0;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (!(p[0] == AIX_LABEL_MAGIC1 &&
808c2ecf20Sopenharmony_ci		p[1] == AIX_LABEL_MAGIC2 &&
818c2ecf20Sopenharmony_ci		p[2] == AIX_LABEL_MAGIC3 &&
828c2ecf20Sopenharmony_ci		p[3] == AIX_LABEL_MAGIC4))
838c2ecf20Sopenharmony_ci		return 0;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/*
868c2ecf20Sopenharmony_ci	 * Assume the partition table is valid if Linux partitions exists.
878c2ecf20Sopenharmony_ci	 * Note that old Solaris/x86 partitions use the same indicator as
888c2ecf20Sopenharmony_ci	 * Linux swap partitions, so we consider that a Linux partition as
898c2ecf20Sopenharmony_ci	 * well.
908c2ecf20Sopenharmony_ci	 */
918c2ecf20Sopenharmony_ci	for (slot = 1; slot <= 4; slot++, pt++) {
928c2ecf20Sopenharmony_ci		if (pt->sys_ind == SOLARIS_X86_PARTITION ||
938c2ecf20Sopenharmony_ci		    pt->sys_ind == LINUX_RAID_PARTITION ||
948c2ecf20Sopenharmony_ci		    pt->sys_ind == LINUX_DATA_PARTITION ||
958c2ecf20Sopenharmony_ci		    pt->sys_ind == LINUX_LVM_PARTITION ||
968c2ecf20Sopenharmony_ci		    is_extended_partition(pt))
978c2ecf20Sopenharmony_ci			return 0;
988c2ecf20Sopenharmony_ci	}
998c2ecf20Sopenharmony_ci	d = read_part_sector(state, 7, &sect);
1008c2ecf20Sopenharmony_ci	if (d) {
1018c2ecf20Sopenharmony_ci		if (d[0] == '_' && d[1] == 'L' && d[2] == 'V' && d[3] == 'M')
1028c2ecf20Sopenharmony_ci			ret = 1;
1038c2ecf20Sopenharmony_ci		put_dev_sector(sect);
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci	return ret;
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic void set_info(struct parsed_partitions *state, int slot,
1098c2ecf20Sopenharmony_ci		     u32 disksig)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	struct partition_meta_info *info = &state->parts[slot].info;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
1148c2ecf20Sopenharmony_ci		 slot);
1158c2ecf20Sopenharmony_ci	info->volname[0] = 0;
1168c2ecf20Sopenharmony_ci	state->parts[slot].has_info = true;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/*
1208c2ecf20Sopenharmony_ci * Create devices for each logical partition in an extended partition.
1218c2ecf20Sopenharmony_ci * The logical partitions form a linked list, with each entry being
1228c2ecf20Sopenharmony_ci * a partition table with two entries.  The first entry
1238c2ecf20Sopenharmony_ci * is the real data partition (with a start relative to the partition
1248c2ecf20Sopenharmony_ci * table start).  The second is a pointer to the next logical partition
1258c2ecf20Sopenharmony_ci * (with a start relative to the entire extended partition).
1268c2ecf20Sopenharmony_ci * We do not create a Linux partition for the partition tables, but
1278c2ecf20Sopenharmony_ci * only for the actual data partitions.
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic void parse_extended(struct parsed_partitions *state,
1318c2ecf20Sopenharmony_ci			   sector_t first_sector, sector_t first_size,
1328c2ecf20Sopenharmony_ci			   u32 disksig)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	struct msdos_partition *p;
1358c2ecf20Sopenharmony_ci	Sector sect;
1368c2ecf20Sopenharmony_ci	unsigned char *data;
1378c2ecf20Sopenharmony_ci	sector_t this_sector, this_size;
1388c2ecf20Sopenharmony_ci	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
1398c2ecf20Sopenharmony_ci	int loopct = 0;		/* number of links followed
1408c2ecf20Sopenharmony_ci				   without finding a data partition */
1418c2ecf20Sopenharmony_ci	int i;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	this_sector = first_sector;
1448c2ecf20Sopenharmony_ci	this_size = first_size;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	while (1) {
1478c2ecf20Sopenharmony_ci		if (++loopct > 100)
1488c2ecf20Sopenharmony_ci			return;
1498c2ecf20Sopenharmony_ci		if (state->next == state->limit)
1508c2ecf20Sopenharmony_ci			return;
1518c2ecf20Sopenharmony_ci		data = read_part_sector(state, this_sector, &sect);
1528c2ecf20Sopenharmony_ci		if (!data)
1538c2ecf20Sopenharmony_ci			return;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci		if (!msdos_magic_present(data + 510))
1568c2ecf20Sopenharmony_ci			goto done;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		p = (struct msdos_partition *) (data + 0x1be);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		/*
1618c2ecf20Sopenharmony_ci		 * Usually, the first entry is the real data partition,
1628c2ecf20Sopenharmony_ci		 * the 2nd entry is the next extended partition, or empty,
1638c2ecf20Sopenharmony_ci		 * and the 3rd and 4th entries are unused.
1648c2ecf20Sopenharmony_ci		 * However, DRDOS sometimes has the extended partition as
1658c2ecf20Sopenharmony_ci		 * the first entry (when the data partition is empty),
1668c2ecf20Sopenharmony_ci		 * and OS/2 seems to use all four entries.
1678c2ecf20Sopenharmony_ci		 */
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		/*
1708c2ecf20Sopenharmony_ci		 * First process the data partition(s)
1718c2ecf20Sopenharmony_ci		 */
1728c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++, p++) {
1738c2ecf20Sopenharmony_ci			sector_t offs, size, next;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci			if (!nr_sects(p) || is_extended_partition(p))
1768c2ecf20Sopenharmony_ci				continue;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci			/* Check the 3rd and 4th entries -
1798c2ecf20Sopenharmony_ci			   these sometimes contain random garbage */
1808c2ecf20Sopenharmony_ci			offs = start_sect(p)*sector_size;
1818c2ecf20Sopenharmony_ci			size = nr_sects(p)*sector_size;
1828c2ecf20Sopenharmony_ci			next = this_sector + offs;
1838c2ecf20Sopenharmony_ci			if (i >= 2) {
1848c2ecf20Sopenharmony_ci				if (offs + size > this_size)
1858c2ecf20Sopenharmony_ci					continue;
1868c2ecf20Sopenharmony_ci				if (next < first_sector)
1878c2ecf20Sopenharmony_ci					continue;
1888c2ecf20Sopenharmony_ci				if (next + size > first_sector + first_size)
1898c2ecf20Sopenharmony_ci					continue;
1908c2ecf20Sopenharmony_ci			}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci			put_partition(state, state->next, next, size);
1938c2ecf20Sopenharmony_ci			set_info(state, state->next, disksig);
1948c2ecf20Sopenharmony_ci			if (p->sys_ind == LINUX_RAID_PARTITION)
1958c2ecf20Sopenharmony_ci				state->parts[state->next].flags = ADDPART_FLAG_RAID;
1968c2ecf20Sopenharmony_ci			loopct = 0;
1978c2ecf20Sopenharmony_ci			if (++state->next == state->limit)
1988c2ecf20Sopenharmony_ci				goto done;
1998c2ecf20Sopenharmony_ci		}
2008c2ecf20Sopenharmony_ci		/*
2018c2ecf20Sopenharmony_ci		 * Next, process the (first) extended partition, if present.
2028c2ecf20Sopenharmony_ci		 * (So far, there seems to be no reason to make
2038c2ecf20Sopenharmony_ci		 *  parse_extended()  recursive and allow a tree
2048c2ecf20Sopenharmony_ci		 *  of extended partitions.)
2058c2ecf20Sopenharmony_ci		 * It should be a link to the next logical partition.
2068c2ecf20Sopenharmony_ci		 */
2078c2ecf20Sopenharmony_ci		p -= 4;
2088c2ecf20Sopenharmony_ci		for (i = 0; i < 4; i++, p++)
2098c2ecf20Sopenharmony_ci			if (nr_sects(p) && is_extended_partition(p))
2108c2ecf20Sopenharmony_ci				break;
2118c2ecf20Sopenharmony_ci		if (i == 4)
2128c2ecf20Sopenharmony_ci			goto done;	 /* nothing left to do */
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		this_sector = first_sector + start_sect(p) * sector_size;
2158c2ecf20Sopenharmony_ci		this_size = nr_sects(p) * sector_size;
2168c2ecf20Sopenharmony_ci		put_dev_sector(sect);
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_cidone:
2198c2ecf20Sopenharmony_ci	put_dev_sector(sect);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci#define SOLARIS_X86_NUMSLICE	16
2238c2ecf20Sopenharmony_ci#define SOLARIS_X86_VTOC_SANE	(0x600DDEEEUL)
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistruct solaris_x86_slice {
2268c2ecf20Sopenharmony_ci	__le16 s_tag;		/* ID tag of partition */
2278c2ecf20Sopenharmony_ci	__le16 s_flag;		/* permission flags */
2288c2ecf20Sopenharmony_ci	__le32 s_start;		/* start sector no of partition */
2298c2ecf20Sopenharmony_ci	__le32 s_size;		/* # of blocks in partition */
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistruct solaris_x86_vtoc {
2338c2ecf20Sopenharmony_ci	unsigned int v_bootinfo[3];	/* info needed by mboot */
2348c2ecf20Sopenharmony_ci	__le32 v_sanity;		/* to verify vtoc sanity */
2358c2ecf20Sopenharmony_ci	__le32 v_version;		/* layout version */
2368c2ecf20Sopenharmony_ci	char	v_volume[8];		/* volume name */
2378c2ecf20Sopenharmony_ci	__le16	v_sectorsz;		/* sector size in bytes */
2388c2ecf20Sopenharmony_ci	__le16	v_nparts;		/* number of partitions */
2398c2ecf20Sopenharmony_ci	unsigned int v_reserved[10];	/* free space */
2408c2ecf20Sopenharmony_ci	struct solaris_x86_slice
2418c2ecf20Sopenharmony_ci		v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
2428c2ecf20Sopenharmony_ci	unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp */
2438c2ecf20Sopenharmony_ci	char	v_asciilabel[128];	/* for compatibility */
2448c2ecf20Sopenharmony_ci};
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci/* james@bpgc.com: Solaris has a nasty indicator: 0x82 which also
2478c2ecf20Sopenharmony_ci   indicates linux swap.  Be careful before believing this is Solaris. */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic void parse_solaris_x86(struct parsed_partitions *state,
2508c2ecf20Sopenharmony_ci			      sector_t offset, sector_t size, int origin)
2518c2ecf20Sopenharmony_ci{
2528c2ecf20Sopenharmony_ci#ifdef CONFIG_SOLARIS_X86_PARTITION
2538c2ecf20Sopenharmony_ci	Sector sect;
2548c2ecf20Sopenharmony_ci	struct solaris_x86_vtoc *v;
2558c2ecf20Sopenharmony_ci	int i;
2568c2ecf20Sopenharmony_ci	short max_nparts;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	v = read_part_sector(state, offset + 1, &sect);
2598c2ecf20Sopenharmony_ci	if (!v)
2608c2ecf20Sopenharmony_ci		return;
2618c2ecf20Sopenharmony_ci	if (le32_to_cpu(v->v_sanity) != SOLARIS_X86_VTOC_SANE) {
2628c2ecf20Sopenharmony_ci		put_dev_sector(sect);
2638c2ecf20Sopenharmony_ci		return;
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci	{
2668c2ecf20Sopenharmony_ci		char tmp[1 + BDEVNAME_SIZE + 10 + 11 + 1];
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), " %s%d: <solaris:", state->name, origin);
2698c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci	if (le32_to_cpu(v->v_version) != 1) {
2728c2ecf20Sopenharmony_ci		char tmp[64];
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), "  cannot handle version %d vtoc>\n",
2758c2ecf20Sopenharmony_ci			 le32_to_cpu(v->v_version));
2768c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2778c2ecf20Sopenharmony_ci		put_dev_sector(sect);
2788c2ecf20Sopenharmony_ci		return;
2798c2ecf20Sopenharmony_ci	}
2808c2ecf20Sopenharmony_ci	/* Ensure we can handle previous case of VTOC with 8 entries gracefully */
2818c2ecf20Sopenharmony_ci	max_nparts = le16_to_cpu(v->v_nparts) > 8 ? SOLARIS_X86_NUMSLICE : 8;
2828c2ecf20Sopenharmony_ci	for (i = 0; i < max_nparts && state->next < state->limit; i++) {
2838c2ecf20Sopenharmony_ci		struct solaris_x86_slice *s = &v->v_slice[i];
2848c2ecf20Sopenharmony_ci		char tmp[3 + 10 + 1 + 1];
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci		if (s->s_size == 0)
2878c2ecf20Sopenharmony_ci			continue;
2888c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), " [s%d]", i);
2898c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
2908c2ecf20Sopenharmony_ci		/* solaris partitions are relative to current MS-DOS
2918c2ecf20Sopenharmony_ci		 * one; must add the offset of the current partition */
2928c2ecf20Sopenharmony_ci		put_partition(state, state->next++,
2938c2ecf20Sopenharmony_ci				 le32_to_cpu(s->s_start)+offset,
2948c2ecf20Sopenharmony_ci				 le32_to_cpu(s->s_size));
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci	put_dev_sector(sect);
2978c2ecf20Sopenharmony_ci	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
2988c2ecf20Sopenharmony_ci#endif
2998c2ecf20Sopenharmony_ci}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci/* check against BSD src/sys/sys/disklabel.h for consistency */
3028c2ecf20Sopenharmony_ci#define BSD_DISKMAGIC	(0x82564557UL)	/* The disk magic number */
3038c2ecf20Sopenharmony_ci#define BSD_MAXPARTITIONS	16
3048c2ecf20Sopenharmony_ci#define OPENBSD_MAXPARTITIONS	16
3058c2ecf20Sopenharmony_ci#define BSD_FS_UNUSED		0 /* disklabel unused partition entry ID */
3068c2ecf20Sopenharmony_cistruct bsd_disklabel {
3078c2ecf20Sopenharmony_ci	__le32	d_magic;		/* the magic number */
3088c2ecf20Sopenharmony_ci	__s16	d_type;			/* drive type */
3098c2ecf20Sopenharmony_ci	__s16	d_subtype;		/* controller/d_type specific */
3108c2ecf20Sopenharmony_ci	char	d_typename[16];		/* type name, e.g. "eagle" */
3118c2ecf20Sopenharmony_ci	char	d_packname[16];		/* pack identifier */
3128c2ecf20Sopenharmony_ci	__u32	d_secsize;		/* # of bytes per sector */
3138c2ecf20Sopenharmony_ci	__u32	d_nsectors;		/* # of data sectors per track */
3148c2ecf20Sopenharmony_ci	__u32	d_ntracks;		/* # of tracks per cylinder */
3158c2ecf20Sopenharmony_ci	__u32	d_ncylinders;		/* # of data cylinders per unit */
3168c2ecf20Sopenharmony_ci	__u32	d_secpercyl;		/* # of data sectors per cylinder */
3178c2ecf20Sopenharmony_ci	__u32	d_secperunit;		/* # of data sectors per unit */
3188c2ecf20Sopenharmony_ci	__u16	d_sparespertrack;	/* # of spare sectors per track */
3198c2ecf20Sopenharmony_ci	__u16	d_sparespercyl;		/* # of spare sectors per cylinder */
3208c2ecf20Sopenharmony_ci	__u32	d_acylinders;		/* # of alt. cylinders per unit */
3218c2ecf20Sopenharmony_ci	__u16	d_rpm;			/* rotational speed */
3228c2ecf20Sopenharmony_ci	__u16	d_interleave;		/* hardware sector interleave */
3238c2ecf20Sopenharmony_ci	__u16	d_trackskew;		/* sector 0 skew, per track */
3248c2ecf20Sopenharmony_ci	__u16	d_cylskew;		/* sector 0 skew, per cylinder */
3258c2ecf20Sopenharmony_ci	__u32	d_headswitch;		/* head switch time, usec */
3268c2ecf20Sopenharmony_ci	__u32	d_trkseek;		/* track-to-track seek, usec */
3278c2ecf20Sopenharmony_ci	__u32	d_flags;		/* generic flags */
3288c2ecf20Sopenharmony_ci#define NDDATA 5
3298c2ecf20Sopenharmony_ci	__u32	d_drivedata[NDDATA];	/* drive-type specific information */
3308c2ecf20Sopenharmony_ci#define NSPARE 5
3318c2ecf20Sopenharmony_ci	__u32	d_spare[NSPARE];	/* reserved for future use */
3328c2ecf20Sopenharmony_ci	__le32	d_magic2;		/* the magic number (again) */
3338c2ecf20Sopenharmony_ci	__le16	d_checksum;		/* xor of data incl. partitions */
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci			/* filesystem and partition information: */
3368c2ecf20Sopenharmony_ci	__le16	d_npartitions;		/* number of partitions in following */
3378c2ecf20Sopenharmony_ci	__le32	d_bbsize;		/* size of boot area at sn0, bytes */
3388c2ecf20Sopenharmony_ci	__le32	d_sbsize;		/* max size of fs superblock, bytes */
3398c2ecf20Sopenharmony_ci	struct	bsd_partition {		/* the partition table */
3408c2ecf20Sopenharmony_ci		__le32	p_size;		/* number of sectors in partition */
3418c2ecf20Sopenharmony_ci		__le32	p_offset;	/* starting sector */
3428c2ecf20Sopenharmony_ci		__le32	p_fsize;	/* filesystem basic fragment size */
3438c2ecf20Sopenharmony_ci		__u8	p_fstype;	/* filesystem type, see below */
3448c2ecf20Sopenharmony_ci		__u8	p_frag;		/* filesystem fragments per block */
3458c2ecf20Sopenharmony_ci		__le16	p_cpg;		/* filesystem cylinders per group */
3468c2ecf20Sopenharmony_ci	} d_partitions[BSD_MAXPARTITIONS];	/* actually may be more */
3478c2ecf20Sopenharmony_ci};
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci#if defined(CONFIG_BSD_DISKLABEL)
3508c2ecf20Sopenharmony_ci/*
3518c2ecf20Sopenharmony_ci * Create devices for BSD partitions listed in a disklabel, under a
3528c2ecf20Sopenharmony_ci * dos-like partition. See parse_extended() for more information.
3538c2ecf20Sopenharmony_ci */
3548c2ecf20Sopenharmony_cistatic void parse_bsd(struct parsed_partitions *state,
3558c2ecf20Sopenharmony_ci		      sector_t offset, sector_t size, int origin, char *flavour,
3568c2ecf20Sopenharmony_ci		      int max_partitions)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	Sector sect;
3598c2ecf20Sopenharmony_ci	struct bsd_disklabel *l;
3608c2ecf20Sopenharmony_ci	struct bsd_partition *p;
3618c2ecf20Sopenharmony_ci	char tmp[64];
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci	l = read_part_sector(state, offset + 1, &sect);
3648c2ecf20Sopenharmony_ci	if (!l)
3658c2ecf20Sopenharmony_ci		return;
3668c2ecf20Sopenharmony_ci	if (le32_to_cpu(l->d_magic) != BSD_DISKMAGIC) {
3678c2ecf20Sopenharmony_ci		put_dev_sector(sect);
3688c2ecf20Sopenharmony_ci		return;
3698c2ecf20Sopenharmony_ci	}
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	snprintf(tmp, sizeof(tmp), " %s%d: <%s:", state->name, origin, flavour);
3728c2ecf20Sopenharmony_ci	strlcat(state->pp_buf, tmp, PAGE_SIZE);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (le16_to_cpu(l->d_npartitions) < max_partitions)
3758c2ecf20Sopenharmony_ci		max_partitions = le16_to_cpu(l->d_npartitions);
3768c2ecf20Sopenharmony_ci	for (p = l->d_partitions; p - l->d_partitions < max_partitions; p++) {
3778c2ecf20Sopenharmony_ci		sector_t bsd_start, bsd_size;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci		if (state->next == state->limit)
3808c2ecf20Sopenharmony_ci			break;
3818c2ecf20Sopenharmony_ci		if (p->p_fstype == BSD_FS_UNUSED)
3828c2ecf20Sopenharmony_ci			continue;
3838c2ecf20Sopenharmony_ci		bsd_start = le32_to_cpu(p->p_offset);
3848c2ecf20Sopenharmony_ci		bsd_size = le32_to_cpu(p->p_size);
3858c2ecf20Sopenharmony_ci		/* FreeBSD has relative offset if C partition offset is zero */
3868c2ecf20Sopenharmony_ci		if (memcmp(flavour, "bsd\0", 4) == 0 &&
3878c2ecf20Sopenharmony_ci		    le32_to_cpu(l->d_partitions[2].p_offset) == 0)
3888c2ecf20Sopenharmony_ci			bsd_start += offset;
3898c2ecf20Sopenharmony_ci		if (offset == bsd_start && size == bsd_size)
3908c2ecf20Sopenharmony_ci			/* full parent partition, we have it already */
3918c2ecf20Sopenharmony_ci			continue;
3928c2ecf20Sopenharmony_ci		if (offset > bsd_start || offset+size < bsd_start+bsd_size) {
3938c2ecf20Sopenharmony_ci			strlcat(state->pp_buf, "bad subpartition - ignored\n", PAGE_SIZE);
3948c2ecf20Sopenharmony_ci			continue;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci		put_partition(state, state->next++, bsd_start, bsd_size);
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci	put_dev_sector(sect);
3998c2ecf20Sopenharmony_ci	if (le16_to_cpu(l->d_npartitions) > max_partitions) {
4008c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), " (ignored %d more)",
4018c2ecf20Sopenharmony_ci			 le16_to_cpu(l->d_npartitions) - max_partitions);
4028c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
4038c2ecf20Sopenharmony_ci	}
4048c2ecf20Sopenharmony_ci	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
4058c2ecf20Sopenharmony_ci}
4068c2ecf20Sopenharmony_ci#endif
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_cistatic void parse_freebsd(struct parsed_partitions *state,
4098c2ecf20Sopenharmony_ci			  sector_t offset, sector_t size, int origin)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci#ifdef CONFIG_BSD_DISKLABEL
4128c2ecf20Sopenharmony_ci	parse_bsd(state, offset, size, origin, "bsd", BSD_MAXPARTITIONS);
4138c2ecf20Sopenharmony_ci#endif
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic void parse_netbsd(struct parsed_partitions *state,
4178c2ecf20Sopenharmony_ci			 sector_t offset, sector_t size, int origin)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci#ifdef CONFIG_BSD_DISKLABEL
4208c2ecf20Sopenharmony_ci	parse_bsd(state, offset, size, origin, "netbsd", BSD_MAXPARTITIONS);
4218c2ecf20Sopenharmony_ci#endif
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic void parse_openbsd(struct parsed_partitions *state,
4258c2ecf20Sopenharmony_ci			  sector_t offset, sector_t size, int origin)
4268c2ecf20Sopenharmony_ci{
4278c2ecf20Sopenharmony_ci#ifdef CONFIG_BSD_DISKLABEL
4288c2ecf20Sopenharmony_ci	parse_bsd(state, offset, size, origin, "openbsd",
4298c2ecf20Sopenharmony_ci		  OPENBSD_MAXPARTITIONS);
4308c2ecf20Sopenharmony_ci#endif
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci#define UNIXWARE_DISKMAGIC     (0xCA5E600DUL)	/* The disk magic number */
4348c2ecf20Sopenharmony_ci#define UNIXWARE_DISKMAGIC2    (0x600DDEEEUL)	/* The slice table magic nr */
4358c2ecf20Sopenharmony_ci#define UNIXWARE_NUMSLICE      16
4368c2ecf20Sopenharmony_ci#define UNIXWARE_FS_UNUSED     0		/* Unused slice entry ID */
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_cistruct unixware_slice {
4398c2ecf20Sopenharmony_ci	__le16   s_label;	/* label */
4408c2ecf20Sopenharmony_ci	__le16   s_flags;	/* permission flags */
4418c2ecf20Sopenharmony_ci	__le32   start_sect;	/* starting sector */
4428c2ecf20Sopenharmony_ci	__le32   nr_sects;	/* number of sectors in slice */
4438c2ecf20Sopenharmony_ci};
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_cistruct unixware_disklabel {
4468c2ecf20Sopenharmony_ci	__le32	d_type;			/* drive type */
4478c2ecf20Sopenharmony_ci	__le32	d_magic;		/* the magic number */
4488c2ecf20Sopenharmony_ci	__le32	d_version;		/* version number */
4498c2ecf20Sopenharmony_ci	char	d_serial[12];		/* serial number of the device */
4508c2ecf20Sopenharmony_ci	__le32	d_ncylinders;		/* # of data cylinders per device */
4518c2ecf20Sopenharmony_ci	__le32	d_ntracks;		/* # of tracks per cylinder */
4528c2ecf20Sopenharmony_ci	__le32	d_nsectors;		/* # of data sectors per track */
4538c2ecf20Sopenharmony_ci	__le32	d_secsize;		/* # of bytes per sector */
4548c2ecf20Sopenharmony_ci	__le32	d_part_start;		/* # of first sector of this partition*/
4558c2ecf20Sopenharmony_ci	__le32	d_unknown1[12];		/* ? */
4568c2ecf20Sopenharmony_ci	__le32	d_alt_tbl;		/* byte offset of alternate table */
4578c2ecf20Sopenharmony_ci	__le32	d_alt_len;		/* byte length of alternate table */
4588c2ecf20Sopenharmony_ci	__le32	d_phys_cyl;		/* # of physical cylinders per device */
4598c2ecf20Sopenharmony_ci	__le32	d_phys_trk;		/* # of physical tracks per cylinder */
4608c2ecf20Sopenharmony_ci	__le32	d_phys_sec;		/* # of physical sectors per track */
4618c2ecf20Sopenharmony_ci	__le32	d_phys_bytes;		/* # of physical bytes per sector */
4628c2ecf20Sopenharmony_ci	__le32	d_unknown2;		/* ? */
4638c2ecf20Sopenharmony_ci	__le32	d_unknown3;		/* ? */
4648c2ecf20Sopenharmony_ci	__le32	d_pad[8];		/* pad */
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	struct unixware_vtoc {
4678c2ecf20Sopenharmony_ci		__le32	v_magic;		/* the magic number */
4688c2ecf20Sopenharmony_ci		__le32	v_version;		/* version number */
4698c2ecf20Sopenharmony_ci		char	v_name[8];		/* volume name */
4708c2ecf20Sopenharmony_ci		__le16	v_nslices;		/* # of slices */
4718c2ecf20Sopenharmony_ci		__le16	v_unknown1;		/* ? */
4728c2ecf20Sopenharmony_ci		__le32	v_reserved[10];		/* reserved */
4738c2ecf20Sopenharmony_ci		struct unixware_slice
4748c2ecf20Sopenharmony_ci			v_slice[UNIXWARE_NUMSLICE];	/* slice headers */
4758c2ecf20Sopenharmony_ci	} vtoc;
4768c2ecf20Sopenharmony_ci};  /* 408 */
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci/*
4798c2ecf20Sopenharmony_ci * Create devices for Unixware partitions listed in a disklabel, under a
4808c2ecf20Sopenharmony_ci * dos-like partition. See parse_extended() for more information.
4818c2ecf20Sopenharmony_ci */
4828c2ecf20Sopenharmony_cistatic void parse_unixware(struct parsed_partitions *state,
4838c2ecf20Sopenharmony_ci			   sector_t offset, sector_t size, int origin)
4848c2ecf20Sopenharmony_ci{
4858c2ecf20Sopenharmony_ci#ifdef CONFIG_UNIXWARE_DISKLABEL
4868c2ecf20Sopenharmony_ci	Sector sect;
4878c2ecf20Sopenharmony_ci	struct unixware_disklabel *l;
4888c2ecf20Sopenharmony_ci	struct unixware_slice *p;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	l = read_part_sector(state, offset + 29, &sect);
4918c2ecf20Sopenharmony_ci	if (!l)
4928c2ecf20Sopenharmony_ci		return;
4938c2ecf20Sopenharmony_ci	if (le32_to_cpu(l->d_magic) != UNIXWARE_DISKMAGIC ||
4948c2ecf20Sopenharmony_ci	    le32_to_cpu(l->vtoc.v_magic) != UNIXWARE_DISKMAGIC2) {
4958c2ecf20Sopenharmony_ci		put_dev_sector(sect);
4968c2ecf20Sopenharmony_ci		return;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci	{
4998c2ecf20Sopenharmony_ci		char tmp[1 + BDEVNAME_SIZE + 10 + 12 + 1];
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), " %s%d: <unixware:", state->name, origin);
5028c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
5038c2ecf20Sopenharmony_ci	}
5048c2ecf20Sopenharmony_ci	p = &l->vtoc.v_slice[1];
5058c2ecf20Sopenharmony_ci	/* I omit the 0th slice as it is the same as whole disk. */
5068c2ecf20Sopenharmony_ci	while (p - &l->vtoc.v_slice[0] < UNIXWARE_NUMSLICE) {
5078c2ecf20Sopenharmony_ci		if (state->next == state->limit)
5088c2ecf20Sopenharmony_ci			break;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci		if (p->s_label != UNIXWARE_FS_UNUSED)
5118c2ecf20Sopenharmony_ci			put_partition(state, state->next++,
5128c2ecf20Sopenharmony_ci				      le32_to_cpu(p->start_sect),
5138c2ecf20Sopenharmony_ci				      le32_to_cpu(p->nr_sects));
5148c2ecf20Sopenharmony_ci		p++;
5158c2ecf20Sopenharmony_ci	}
5168c2ecf20Sopenharmony_ci	put_dev_sector(sect);
5178c2ecf20Sopenharmony_ci	strlcat(state->pp_buf, " >\n", PAGE_SIZE);
5188c2ecf20Sopenharmony_ci#endif
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci#define MINIX_NR_SUBPARTITIONS  4
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci/*
5248c2ecf20Sopenharmony_ci * Minix 2.0.0/2.0.2 subpartition support.
5258c2ecf20Sopenharmony_ci * Anand Krishnamurthy <anandk@wiproge.med.ge.com>
5268c2ecf20Sopenharmony_ci * Rajeev V. Pillai    <rajeevvp@yahoo.com>
5278c2ecf20Sopenharmony_ci */
5288c2ecf20Sopenharmony_cistatic void parse_minix(struct parsed_partitions *state,
5298c2ecf20Sopenharmony_ci			sector_t offset, sector_t size, int origin)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci#ifdef CONFIG_MINIX_SUBPARTITION
5328c2ecf20Sopenharmony_ci	Sector sect;
5338c2ecf20Sopenharmony_ci	unsigned char *data;
5348c2ecf20Sopenharmony_ci	struct msdos_partition *p;
5358c2ecf20Sopenharmony_ci	int i;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	data = read_part_sector(state, offset, &sect);
5388c2ecf20Sopenharmony_ci	if (!data)
5398c2ecf20Sopenharmony_ci		return;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	p = (struct msdos_partition *)(data + 0x1be);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	/* The first sector of a Minix partition can have either
5448c2ecf20Sopenharmony_ci	 * a secondary MBR describing its subpartitions, or
5458c2ecf20Sopenharmony_ci	 * the normal boot sector. */
5468c2ecf20Sopenharmony_ci	if (msdos_magic_present(data + 510) &&
5478c2ecf20Sopenharmony_ci	    p->sys_ind == MINIX_PARTITION) { /* subpartition table present */
5488c2ecf20Sopenharmony_ci		char tmp[1 + BDEVNAME_SIZE + 10 + 9 + 1];
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci		snprintf(tmp, sizeof(tmp), " %s%d: <minix:", state->name, origin);
5518c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, tmp, PAGE_SIZE);
5528c2ecf20Sopenharmony_ci		for (i = 0; i < MINIX_NR_SUBPARTITIONS; i++, p++) {
5538c2ecf20Sopenharmony_ci			if (state->next == state->limit)
5548c2ecf20Sopenharmony_ci				break;
5558c2ecf20Sopenharmony_ci			/* add each partition in use */
5568c2ecf20Sopenharmony_ci			if (p->sys_ind == MINIX_PARTITION)
5578c2ecf20Sopenharmony_ci				put_partition(state, state->next++,
5588c2ecf20Sopenharmony_ci					      start_sect(p), nr_sects(p));
5598c2ecf20Sopenharmony_ci		}
5608c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, " >\n", PAGE_SIZE);
5618c2ecf20Sopenharmony_ci	}
5628c2ecf20Sopenharmony_ci	put_dev_sector(sect);
5638c2ecf20Sopenharmony_ci#endif /* CONFIG_MINIX_SUBPARTITION */
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_cistatic struct {
5678c2ecf20Sopenharmony_ci	unsigned char id;
5688c2ecf20Sopenharmony_ci	void (*parse)(struct parsed_partitions *, sector_t, sector_t, int);
5698c2ecf20Sopenharmony_ci} subtypes[] = {
5708c2ecf20Sopenharmony_ci	{FREEBSD_PARTITION, parse_freebsd},
5718c2ecf20Sopenharmony_ci	{NETBSD_PARTITION, parse_netbsd},
5728c2ecf20Sopenharmony_ci	{OPENBSD_PARTITION, parse_openbsd},
5738c2ecf20Sopenharmony_ci	{MINIX_PARTITION, parse_minix},
5748c2ecf20Sopenharmony_ci	{UNIXWARE_PARTITION, parse_unixware},
5758c2ecf20Sopenharmony_ci	{SOLARIS_X86_PARTITION, parse_solaris_x86},
5768c2ecf20Sopenharmony_ci	{NEW_SOLARIS_X86_PARTITION, parse_solaris_x86},
5778c2ecf20Sopenharmony_ci	{0, NULL},
5788c2ecf20Sopenharmony_ci};
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ciint msdos_partition(struct parsed_partitions *state)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	sector_t sector_size = bdev_logical_block_size(state->bdev) / 512;
5838c2ecf20Sopenharmony_ci	Sector sect;
5848c2ecf20Sopenharmony_ci	unsigned char *data;
5858c2ecf20Sopenharmony_ci	struct msdos_partition *p;
5868c2ecf20Sopenharmony_ci	struct fat_boot_sector *fb;
5878c2ecf20Sopenharmony_ci	int slot;
5888c2ecf20Sopenharmony_ci	u32 disksig;
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci	data = read_part_sector(state, 0, &sect);
5918c2ecf20Sopenharmony_ci	if (!data)
5928c2ecf20Sopenharmony_ci		return -1;
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	/*
5958c2ecf20Sopenharmony_ci	 * Note order! (some AIX disks, e.g. unbootable kind,
5968c2ecf20Sopenharmony_ci	 * have no MSDOS 55aa)
5978c2ecf20Sopenharmony_ci	 */
5988c2ecf20Sopenharmony_ci	if (aix_magic_present(state, data)) {
5998c2ecf20Sopenharmony_ci		put_dev_sector(sect);
6008c2ecf20Sopenharmony_ci#ifdef CONFIG_AIX_PARTITION
6018c2ecf20Sopenharmony_ci		return aix_partition(state);
6028c2ecf20Sopenharmony_ci#else
6038c2ecf20Sopenharmony_ci		strlcat(state->pp_buf, " [AIX]", PAGE_SIZE);
6048c2ecf20Sopenharmony_ci		return 0;
6058c2ecf20Sopenharmony_ci#endif
6068c2ecf20Sopenharmony_ci	}
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (!msdos_magic_present(data + 510)) {
6098c2ecf20Sopenharmony_ci		put_dev_sector(sect);
6108c2ecf20Sopenharmony_ci		return 0;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/*
6148c2ecf20Sopenharmony_ci	 * Now that the 55aa signature is present, this is probably
6158c2ecf20Sopenharmony_ci	 * either the boot sector of a FAT filesystem or a DOS-type
6168c2ecf20Sopenharmony_ci	 * partition table. Reject this in case the boot indicator
6178c2ecf20Sopenharmony_ci	 * is not 0 or 0x80.
6188c2ecf20Sopenharmony_ci	 */
6198c2ecf20Sopenharmony_ci	p = (struct msdos_partition *) (data + 0x1be);
6208c2ecf20Sopenharmony_ci	for (slot = 1; slot <= 4; slot++, p++) {
6218c2ecf20Sopenharmony_ci		if (p->boot_ind != 0 && p->boot_ind != 0x80) {
6228c2ecf20Sopenharmony_ci			/*
6238c2ecf20Sopenharmony_ci			 * Even without a valid boot inidicator value
6248c2ecf20Sopenharmony_ci			 * its still possible this is valid FAT filesystem
6258c2ecf20Sopenharmony_ci			 * without a partition table.
6268c2ecf20Sopenharmony_ci			 */
6278c2ecf20Sopenharmony_ci			fb = (struct fat_boot_sector *) data;
6288c2ecf20Sopenharmony_ci			if (slot == 1 && fb->reserved && fb->fats
6298c2ecf20Sopenharmony_ci				&& fat_valid_media(fb->media)) {
6308c2ecf20Sopenharmony_ci				strlcat(state->pp_buf, "\n", PAGE_SIZE);
6318c2ecf20Sopenharmony_ci				put_dev_sector(sect);
6328c2ecf20Sopenharmony_ci				return 1;
6338c2ecf20Sopenharmony_ci			} else {
6348c2ecf20Sopenharmony_ci				put_dev_sector(sect);
6358c2ecf20Sopenharmony_ci				return 0;
6368c2ecf20Sopenharmony_ci			}
6378c2ecf20Sopenharmony_ci		}
6388c2ecf20Sopenharmony_ci	}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI_PARTITION
6418c2ecf20Sopenharmony_ci	p = (struct msdos_partition *) (data + 0x1be);
6428c2ecf20Sopenharmony_ci	for (slot = 1 ; slot <= 4 ; slot++, p++) {
6438c2ecf20Sopenharmony_ci		/* If this is an EFI GPT disk, msdos should ignore it. */
6448c2ecf20Sopenharmony_ci		if (p->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT) {
6458c2ecf20Sopenharmony_ci			put_dev_sector(sect);
6468c2ecf20Sopenharmony_ci			return 0;
6478c2ecf20Sopenharmony_ci		}
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci#endif
6508c2ecf20Sopenharmony_ci	p = (struct msdos_partition *) (data + 0x1be);
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_ci	disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	/*
6558c2ecf20Sopenharmony_ci	 * Look for partitions in two passes:
6568c2ecf20Sopenharmony_ci	 * First find the primary and DOS-type extended partitions.
6578c2ecf20Sopenharmony_ci	 * On the second pass look inside *BSD, Unixware and Solaris partitions.
6588c2ecf20Sopenharmony_ci	 */
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	state->next = 5;
6618c2ecf20Sopenharmony_ci	for (slot = 1 ; slot <= 4 ; slot++, p++) {
6628c2ecf20Sopenharmony_ci		sector_t start = start_sect(p)*sector_size;
6638c2ecf20Sopenharmony_ci		sector_t size = nr_sects(p)*sector_size;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		if (!size)
6668c2ecf20Sopenharmony_ci			continue;
6678c2ecf20Sopenharmony_ci		if (is_extended_partition(p)) {
6688c2ecf20Sopenharmony_ci			/*
6698c2ecf20Sopenharmony_ci			 * prevent someone doing mkfs or mkswap on an
6708c2ecf20Sopenharmony_ci			 * extended partition, but leave room for LILO
6718c2ecf20Sopenharmony_ci			 * FIXME: this uses one logical sector for > 512b
6728c2ecf20Sopenharmony_ci			 * sector, although it may not be enough/proper.
6738c2ecf20Sopenharmony_ci			 */
6748c2ecf20Sopenharmony_ci			sector_t n = 2;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci			n = min(size, max(sector_size, n));
6778c2ecf20Sopenharmony_ci			put_partition(state, slot, start, n);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci			strlcat(state->pp_buf, " <", PAGE_SIZE);
6808c2ecf20Sopenharmony_ci			parse_extended(state, start, size, disksig);
6818c2ecf20Sopenharmony_ci			strlcat(state->pp_buf, " >", PAGE_SIZE);
6828c2ecf20Sopenharmony_ci			continue;
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci		put_partition(state, slot, start, size);
6858c2ecf20Sopenharmony_ci		set_info(state, slot, disksig);
6868c2ecf20Sopenharmony_ci		if (p->sys_ind == LINUX_RAID_PARTITION)
6878c2ecf20Sopenharmony_ci			state->parts[slot].flags = ADDPART_FLAG_RAID;
6888c2ecf20Sopenharmony_ci		if (p->sys_ind == DM6_PARTITION)
6898c2ecf20Sopenharmony_ci			strlcat(state->pp_buf, "[DM]", PAGE_SIZE);
6908c2ecf20Sopenharmony_ci		if (p->sys_ind == EZD_PARTITION)
6918c2ecf20Sopenharmony_ci			strlcat(state->pp_buf, "[EZD]", PAGE_SIZE);
6928c2ecf20Sopenharmony_ci	}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_ci	strlcat(state->pp_buf, "\n", PAGE_SIZE);
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_ci	/* second pass - output for each on a separate line */
6978c2ecf20Sopenharmony_ci	p = (struct msdos_partition *) (0x1be + data);
6988c2ecf20Sopenharmony_ci	for (slot = 1 ; slot <= 4 ; slot++, p++) {
6998c2ecf20Sopenharmony_ci		unsigned char id = p->sys_ind;
7008c2ecf20Sopenharmony_ci		int n;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci		if (!nr_sects(p))
7038c2ecf20Sopenharmony_ci			continue;
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		for (n = 0; subtypes[n].parse && id != subtypes[n].id; n++)
7068c2ecf20Sopenharmony_ci			;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		if (!subtypes[n].parse)
7098c2ecf20Sopenharmony_ci			continue;
7108c2ecf20Sopenharmony_ci		subtypes[n].parse(state, start_sect(p) * sector_size,
7118c2ecf20Sopenharmony_ci				  nr_sects(p) * sector_size, slot);
7128c2ecf20Sopenharmony_ci	}
7138c2ecf20Sopenharmony_ci	put_dev_sector(sect);
7148c2ecf20Sopenharmony_ci	return 1;
7158c2ecf20Sopenharmony_ci}
716