17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*18c2aff7Sartem  * Common Development and Distribution License (the "License").
6*18c2aff7Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*18c2aff7Sartem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <libdevinfo.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <dirent.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "libdiskmgt.h"
417c478bd9Sstevel@tonic-gate #include "disks_private.h"
427c478bd9Sstevel@tonic-gate #include "partition.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #ifdef sparc
457c478bd9Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
467c478bd9Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
477c478bd9Sstevel@tonic-gate 			    (les((unsigned)((val)&0xffff0000)>>16)))
487c478bd9Sstevel@tonic-gate #else
497c478bd9Sstevel@tonic-gate #define	les(val)	(val)
507c478bd9Sstevel@tonic-gate #define	lel(val)	(val)
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #define	ISIZE		FD_NUMPART * sizeof (struct ipart)
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static int	desc_ok(descriptor_t *dp);
567c478bd9Sstevel@tonic-gate static int	get_attrs(descriptor_t *dp, struct ipart *iparts,
577c478bd9Sstevel@tonic-gate 		    nvlist_t *attrs);
587c478bd9Sstevel@tonic-gate static int	get_parts(disk_t *disk, struct ipart *iparts, char *opath,
597c478bd9Sstevel@tonic-gate 		    int opath_len);
607c478bd9Sstevel@tonic-gate static int	open_disk(disk_t *diskp, char *opath, int len);
617c478bd9Sstevel@tonic-gate static int	has_slices(descriptor_t *desc, int *errp);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate descriptor_t **
647c478bd9Sstevel@tonic-gate partition_get_assoc_descriptors(descriptor_t *desc, dm_desc_type_t type,
657c478bd9Sstevel@tonic-gate     int *errp)
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate 	if (!desc_ok(desc)) {
687c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
697c478bd9Sstevel@tonic-gate 	    return (NULL);
707c478bd9Sstevel@tonic-gate 	}
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	switch (type) {
737c478bd9Sstevel@tonic-gate 	case DM_MEDIA:
747c478bd9Sstevel@tonic-gate 	    return (media_get_assocs(desc, errp));
757c478bd9Sstevel@tonic-gate 	case DM_SLICE:
767c478bd9Sstevel@tonic-gate 	    if (!has_slices(desc, errp)) {
777c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
787c478bd9Sstevel@tonic-gate 		    return (NULL);
797c478bd9Sstevel@tonic-gate 		}
807c478bd9Sstevel@tonic-gate 		return (libdiskmgt_empty_desc_array(errp));
817c478bd9Sstevel@tonic-gate 	    }
827c478bd9Sstevel@tonic-gate 	    return (slice_get_assocs(desc, errp));
837c478bd9Sstevel@tonic-gate 	}
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
867c478bd9Sstevel@tonic-gate 	return (NULL);
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * This is called by media/slice to get the associated partitions.
917c478bd9Sstevel@tonic-gate  * For a media desc. we just get all the partitions, but for a slice desc.
927c478bd9Sstevel@tonic-gate  * we just get the active solaris partition.
937c478bd9Sstevel@tonic-gate  */
947c478bd9Sstevel@tonic-gate descriptor_t **
957c478bd9Sstevel@tonic-gate partition_get_assocs(descriptor_t *desc, int *errp)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	descriptor_t	**partitions;
987c478bd9Sstevel@tonic-gate 	int		pos;
997c478bd9Sstevel@tonic-gate 	int		i;
1007c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
1017c478bd9Sstevel@tonic-gate 	char		pname[MAXPATHLEN];
1027c478bd9Sstevel@tonic-gate 	int		conv_flag = 0;
1033e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
1043e1bd7a2Ssjelinek 	int		len;
1053e1bd7a2Ssjelinek #endif
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (get_parts(desc->p.disk, iparts, pname, sizeof (pname)) != 0) {
1087c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	/* allocate the array for the descriptors */
1127c478bd9Sstevel@tonic-gate 	partitions = (descriptor_t **)calloc(FD_NUMPART + 1,
1137c478bd9Sstevel@tonic-gate 	    sizeof (descriptor_t *));
1147c478bd9Sstevel@tonic-gate 	if (partitions == NULL) {
1157c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
1167c478bd9Sstevel@tonic-gate 	    return (NULL);
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1193e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
1207c478bd9Sstevel@tonic-gate 	    /* convert part. name (e.g. c0d0p0) */
1217c478bd9Sstevel@tonic-gate 	    len = strlen(pname);
1227c478bd9Sstevel@tonic-gate 	    if (len > 1 && *(pname + (len - 2)) == 'p') {
1237c478bd9Sstevel@tonic-gate 		conv_flag = 1;
1247c478bd9Sstevel@tonic-gate 		*(pname + (len - 1)) = 0;
1257c478bd9Sstevel@tonic-gate 	    }
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/*
1297c478bd9Sstevel@tonic-gate 	 * If this is a slice desc. we need the first active solaris partition
1307c478bd9Sstevel@tonic-gate 	 * and if there isn't one then we need the first solaris partition.
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	if (desc->type == DM_SLICE) {
1337c478bd9Sstevel@tonic-gate 	    for (i = 0; i < FD_NUMPART; i++) {
1347c478bd9Sstevel@tonic-gate 		if (iparts[i].bootid == ACTIVE &&
1357c478bd9Sstevel@tonic-gate 		    (iparts[i].systid == SUNIXOS ||
1367c478bd9Sstevel@tonic-gate 		    iparts[i].systid == SUNIXOS2)) {
1377c478bd9Sstevel@tonic-gate 			break;
1387c478bd9Sstevel@tonic-gate 		}
1397c478bd9Sstevel@tonic-gate 	    }
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	    /* no active solaris part., try to get the first solaris part. */
1427c478bd9Sstevel@tonic-gate 	    if (i >= FD_NUMPART) {
1437c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
1447c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid == SUNIXOS ||
1457c478bd9Sstevel@tonic-gate 			iparts[i].systid == SUNIXOS2) {
1467c478bd9Sstevel@tonic-gate 			    break;
1477c478bd9Sstevel@tonic-gate 		    }
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 	    }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	    if (i < FD_NUMPART) {
1527c478bd9Sstevel@tonic-gate 		/* we found a solaris partition to use */
1537c478bd9Sstevel@tonic-gate 		char	part_name[MAXPATHLEN];
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 		if (conv_flag) {
1567c478bd9Sstevel@tonic-gate 		    /* convert part. name (e.g. c0d0p0) */
1577c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%s%d",
1587c478bd9Sstevel@tonic-gate 			pname, i);
1597c478bd9Sstevel@tonic-gate 		} else {
1607c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%d", i);
1617c478bd9Sstevel@tonic-gate 		}
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 		/* the media name comes from the slice desc. */
1647c478bd9Sstevel@tonic-gate 		partitions[0] = cache_get_desc(DM_PARTITION, desc->p.disk,
1657c478bd9Sstevel@tonic-gate 		    part_name, desc->secondary_name, errp);
1667c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
1677c478bd9Sstevel@tonic-gate 		    cache_free_descriptors(partitions);
1687c478bd9Sstevel@tonic-gate 		    return (NULL);
1697c478bd9Sstevel@tonic-gate 		}
1707c478bd9Sstevel@tonic-gate 		partitions[1] = NULL;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 		return (partitions);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	    }
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	    return (libdiskmgt_empty_desc_array(errp));
1777c478bd9Sstevel@tonic-gate 	}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* Must be for media, so get all the parts. */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	pos = 0;
1827c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
1837c478bd9Sstevel@tonic-gate 	    if (iparts[i].systid != 0) {
1847c478bd9Sstevel@tonic-gate 		char	part_name[MAXPATHLEN];
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		if (conv_flag) {
1877c478bd9Sstevel@tonic-gate 		    /* convert part. name (e.g. c0d0p0) */
1887c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%s%d",
1897c478bd9Sstevel@tonic-gate 			pname, i);
1907c478bd9Sstevel@tonic-gate 		} else {
1917c478bd9Sstevel@tonic-gate 		    (void) snprintf(part_name, sizeof (part_name), "%d", i);
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		/* the media name comes from the media desc. */
1957c478bd9Sstevel@tonic-gate 		partitions[pos] = cache_get_desc(DM_PARTITION, desc->p.disk,
1967c478bd9Sstevel@tonic-gate 		    part_name, desc->name, errp);
1977c478bd9Sstevel@tonic-gate 		if (*errp != 0) {
1987c478bd9Sstevel@tonic-gate 		    cache_free_descriptors(partitions);
1997c478bd9Sstevel@tonic-gate 		    return (NULL);
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		pos++;
2037c478bd9Sstevel@tonic-gate 	    }
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 	partitions[pos] = NULL;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	*errp = 0;
2087c478bd9Sstevel@tonic-gate 	return (partitions);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate nvlist_t *
2127c478bd9Sstevel@tonic-gate partition_get_attributes(descriptor_t *dp, int *errp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	nvlist_t	*attrs = NULL;
2157c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	if (!desc_ok(dp)) {
2187c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
2197c478bd9Sstevel@tonic-gate 	    return (NULL);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((*errp = get_parts(dp->p.disk, iparts, NULL, 0)) != 0) {
2237c478bd9Sstevel@tonic-gate 	    return (NULL);
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (nvlist_alloc(&attrs, NVATTRS, 0) != 0) {
2277c478bd9Sstevel@tonic-gate 	    *errp = ENOMEM;
2287c478bd9Sstevel@tonic-gate 	    return (NULL);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	if ((*errp = get_attrs(dp, iparts, attrs)) != 0) {
2327c478bd9Sstevel@tonic-gate 	    nvlist_free(attrs);
2337c478bd9Sstevel@tonic-gate 	    attrs = NULL;
2347c478bd9Sstevel@tonic-gate 	}
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	return (attrs);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * Look for the partition by the partition number (which is not too useful).
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate descriptor_t *
2437c478bd9Sstevel@tonic-gate partition_get_descriptor_by_name(char *name, int *errp)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	descriptor_t	**partitions;
2467c478bd9Sstevel@tonic-gate 	int		i;
2477c478bd9Sstevel@tonic-gate 	descriptor_t	*partition = NULL;
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	partitions = cache_get_descriptors(DM_PARTITION, errp);
2507c478bd9Sstevel@tonic-gate 	if (*errp != 0) {
2517c478bd9Sstevel@tonic-gate 	    return (NULL);
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	for (i = 0; partitions[i]; i++) {
2557c478bd9Sstevel@tonic-gate 	    if (libdiskmgt_str_eq(name, partitions[i]->name)) {
2567c478bd9Sstevel@tonic-gate 		partition = partitions[i];
2577c478bd9Sstevel@tonic-gate 	    } else {
2587c478bd9Sstevel@tonic-gate 		/* clean up the unused descriptors */
2597c478bd9Sstevel@tonic-gate 		cache_free_descriptor(partitions[i]);
2607c478bd9Sstevel@tonic-gate 	    }
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	free(partitions);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (partition == NULL) {
2657c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
2667c478bd9Sstevel@tonic-gate 	}
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	return (partition);
2697c478bd9Sstevel@tonic-gate }
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* ARGSUSED */
2727c478bd9Sstevel@tonic-gate descriptor_t **
2737c478bd9Sstevel@tonic-gate partition_get_descriptors(int filter[], int *errp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	return (cache_get_descriptors(DM_PARTITION, errp));
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate char *
2797c478bd9Sstevel@tonic-gate partition_get_name(descriptor_t *desc)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	return (desc->name);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate /* ARGSUSED */
2857c478bd9Sstevel@tonic-gate nvlist_t *
2867c478bd9Sstevel@tonic-gate partition_get_stats(descriptor_t *dp, int stat_type, int *errp)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	/* There are no stat types defined for partitions */
2897c478bd9Sstevel@tonic-gate 	*errp = EINVAL;
2907c478bd9Sstevel@tonic-gate 	return (NULL);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /* ARGSUSED */
2947c478bd9Sstevel@tonic-gate int
2957c478bd9Sstevel@tonic-gate partition_has_fdisk(disk_t *dp, int fd)
2967c478bd9Sstevel@tonic-gate {
2977c478bd9Sstevel@tonic-gate 	char		bootsect[512 * 3]; /* 3 sectors to be safe */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate #ifdef sparc
3007c478bd9Sstevel@tonic-gate 	if (dp->drv_type == DM_DT_FIXED) {
3017c478bd9Sstevel@tonic-gate 	    /* on sparc, only removable media can have fdisk parts. */
3027c478bd9Sstevel@tonic-gate 	    return (0);
3037c478bd9Sstevel@tonic-gate 	}
3047c478bd9Sstevel@tonic-gate #endif
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * We assume the caller already made sure media was inserted and
3087c478bd9Sstevel@tonic-gate 	 * spun up.
3097c478bd9Sstevel@tonic-gate 	 */
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	if ((ioctl(fd, DKIOCGMBOOT, bootsect) < 0) && (errno != ENOTTY)) {
3127c478bd9Sstevel@tonic-gate 	    return (0);
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (1);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * A partition descriptor points to a disk, the name is the partition number
3207c478bd9Sstevel@tonic-gate  * and the secondary name is the media name.
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate int
3237c478bd9Sstevel@tonic-gate partition_make_descriptors()
3247c478bd9Sstevel@tonic-gate {
3257c478bd9Sstevel@tonic-gate 	int		error;
3267c478bd9Sstevel@tonic-gate 	disk_t		*dp;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	dp = cache_get_disklist();
3297c478bd9Sstevel@tonic-gate 	while (dp != NULL) {
3307c478bd9Sstevel@tonic-gate 	    struct ipart	iparts[FD_NUMPART];
3317c478bd9Sstevel@tonic-gate 	    char		pname[MAXPATHLEN];
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	    if (get_parts(dp, iparts, pname, sizeof (pname)) == 0) {
3347c478bd9Sstevel@tonic-gate 		int	i;
3357c478bd9Sstevel@tonic-gate 		char	mname[MAXPATHLEN];
3367c478bd9Sstevel@tonic-gate 		int	conv_flag = 0;
3373e1bd7a2Ssjelinek #if defined(i386) || defined(__amd64)
3387c478bd9Sstevel@tonic-gate 		/* convert part. name (e.g. c0d0p0) */
3397c478bd9Sstevel@tonic-gate 		int	len;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		len = strlen(pname);
3427c478bd9Sstevel@tonic-gate 		if (len > 1 && *(pname + (len - 2)) == 'p') {
3437c478bd9Sstevel@tonic-gate 		    conv_flag = 1;
3447c478bd9Sstevel@tonic-gate 		    *(pname + (len - 1)) = 0;
3457c478bd9Sstevel@tonic-gate 		}
3467c478bd9Sstevel@tonic-gate #endif
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		mname[0] = 0;
3497c478bd9Sstevel@tonic-gate 		(void) media_read_name(dp, mname, sizeof (mname));
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
3527c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid != 0) {
3537c478bd9Sstevel@tonic-gate 			char	part_name[MAXPATHLEN];
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 			if (conv_flag) {
3567c478bd9Sstevel@tonic-gate 			    /* convert part. name (e.g. c0d0p0) */
3577c478bd9Sstevel@tonic-gate 			    (void) snprintf(part_name, sizeof (part_name),
3587c478bd9Sstevel@tonic-gate 				"%s%d", pname, i);
3597c478bd9Sstevel@tonic-gate 			} else {
3607c478bd9Sstevel@tonic-gate 			    (void) snprintf(part_name, sizeof (part_name),
3617c478bd9Sstevel@tonic-gate 				"%d", i);
3627c478bd9Sstevel@tonic-gate 			}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			cache_load_desc(DM_PARTITION, dp, part_name, mname,
3657c478bd9Sstevel@tonic-gate 			    &error);
3667c478bd9Sstevel@tonic-gate 			if (error != 0) {
3677c478bd9Sstevel@tonic-gate 			    return (error);
3687c478bd9Sstevel@tonic-gate 			}
3697c478bd9Sstevel@tonic-gate 		    }
3707c478bd9Sstevel@tonic-gate 		}
3717c478bd9Sstevel@tonic-gate 	    }
3727c478bd9Sstevel@tonic-gate 	    dp = dp->next;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	return (0);
3767c478bd9Sstevel@tonic-gate }
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate static int
3797c478bd9Sstevel@tonic-gate get_attrs(descriptor_t *dp, struct ipart *iparts, nvlist_t *attrs)
3807c478bd9Sstevel@tonic-gate {
3817c478bd9Sstevel@tonic-gate 	char		*p;
3827c478bd9Sstevel@tonic-gate 	int		part_num;
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	/*
3857c478bd9Sstevel@tonic-gate 	 * We already made sure the media was loaded and ready in the
3867c478bd9Sstevel@tonic-gate 	 * get_parts call within partition_get_attributes.
3877c478bd9Sstevel@tonic-gate 	 */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	p = strrchr(dp->name, 'p');
3907c478bd9Sstevel@tonic-gate 	if (p == NULL) {
3917c478bd9Sstevel@tonic-gate 	    p = dp->name;
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 	    p++;
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 	part_num = atoi(p);
3967c478bd9Sstevel@tonic-gate 	if (part_num >= FD_NUMPART || iparts[part_num].systid == 0) {
3977c478bd9Sstevel@tonic-gate 	    return (ENODEV);
3987c478bd9Sstevel@tonic-gate 	}
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	/* we found the partition */
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BOOTID,
4037c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].bootid) != 0) {
4047c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4057c478bd9Sstevel@tonic-gate 	}
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_PTYPE,
4087c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].systid) != 0) {
4097c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BHEAD,
4137c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].beghead) != 0) {
4147c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BSECT,
4187c478bd9Sstevel@tonic-gate 	    (unsigned int)((iparts[part_num].begsect) & 0x3f)) != 0) {
4197c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_BCYL, (unsigned int)
4237c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].begcyl & 0xff) |
4247c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].begsect & 0xc0) << 2))) != 0) {
4257c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4267c478bd9Sstevel@tonic-gate 	}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_EHEAD,
4297c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].endhead) != 0) {
4307c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_ESECT,
4347c478bd9Sstevel@tonic-gate 	    (unsigned int)((iparts[part_num].endsect) & 0x3f)) != 0) {
4357c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_ECYL, (unsigned int)
4397c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].endcyl & 0xff) |
4407c478bd9Sstevel@tonic-gate 	    ((iparts[part_num].endsect & 0xc0) << 2))) != 0) {
4417c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4427c478bd9Sstevel@tonic-gate 	}
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_RELSECT,
4457c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].relsect) != 0) {
4467c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (nvlist_add_uint32(attrs, DM_NSECTORS,
4507c478bd9Sstevel@tonic-gate 	    (unsigned int)iparts[part_num].numsect) != 0) {
4517c478bd9Sstevel@tonic-gate 	    return (ENOMEM);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	return (0);
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate static int
4587c478bd9Sstevel@tonic-gate get_parts(disk_t *disk, struct ipart *iparts, char *opath, int opath_len)
4597c478bd9Sstevel@tonic-gate {
4607c478bd9Sstevel@tonic-gate 	int		fd;
4617c478bd9Sstevel@tonic-gate 	struct dk_minfo	minfo;
4627c478bd9Sstevel@tonic-gate 	struct mboot	bootblk;
4637c478bd9Sstevel@tonic-gate 	char		bootsect[512];
4647c478bd9Sstevel@tonic-gate 	int		i;
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/* Can't use drive_open_disk since we need the partition dev name. */
4677c478bd9Sstevel@tonic-gate 	if ((fd = open_disk(disk, opath, opath_len)) < 0) {
4687c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/* First make sure media is inserted and spun up. */
4727c478bd9Sstevel@tonic-gate 	if (!media_read_info(fd, &minfo)) {
4737c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4747c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (!partition_has_fdisk(disk, fd)) {
4787c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4797c478bd9Sstevel@tonic-gate 	    return (ENOTTY);
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (lseek(fd, 0, 0) == -1) {
4837c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4847c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (read(fd, bootsect, 512) != 512) {
4887c478bd9Sstevel@tonic-gate 	    (void) close(fd);
4897c478bd9Sstevel@tonic-gate 	    return (ENODEV);
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 	(void) close(fd);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	(void) memcpy(&bootblk, bootsect, sizeof (bootblk));
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	if (les(bootblk.signature) != MBB_MAGIC)  {
4967c478bd9Sstevel@tonic-gate 	    return (ENOTTY);
4977c478bd9Sstevel@tonic-gate 	}
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	(void) memcpy(iparts, bootblk.parts, ISIZE);
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
5027c478bd9Sstevel@tonic-gate 	    if (iparts[i].systid != 0) {
5037c478bd9Sstevel@tonic-gate 		iparts[i].relsect = lel(iparts[i].relsect);
5047c478bd9Sstevel@tonic-gate 		iparts[i].numsect = lel(iparts[i].numsect);
5057c478bd9Sstevel@tonic-gate 	    }
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	return (0);
5097c478bd9Sstevel@tonic-gate }
5107c478bd9Sstevel@tonic-gate /* return 1 if the partition descriptor is still valid, 0 if not. */
5117c478bd9Sstevel@tonic-gate static int
5127c478bd9Sstevel@tonic-gate desc_ok(descriptor_t *dp)
5137c478bd9Sstevel@tonic-gate {
5147c478bd9Sstevel@tonic-gate 	/* First verify the media name for removable media */
5157c478bd9Sstevel@tonic-gate 	if (dp->p.disk->removable) {
5167c478bd9Sstevel@tonic-gate 	    char	mname[MAXPATHLEN];
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	    if (!media_read_name(dp->p.disk, mname, sizeof (mname))) {
5197c478bd9Sstevel@tonic-gate 		return (0);
5207c478bd9Sstevel@tonic-gate 	    }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	    if (mname[0] == 0) {
5237c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, NULL));
5247c478bd9Sstevel@tonic-gate 	    } else {
5257c478bd9Sstevel@tonic-gate 		return (libdiskmgt_str_eq(dp->secondary_name, mname));
5267c478bd9Sstevel@tonic-gate 	    }
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	/*
5307c478bd9Sstevel@tonic-gate 	 * We could verify the partition is still there but this is kind of
5317c478bd9Sstevel@tonic-gate 	 * expensive and other code down the line will do that (e.g. see
5327c478bd9Sstevel@tonic-gate 	 * get_attrs).
5337c478bd9Sstevel@tonic-gate 	 */
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	return (1);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Return 1 if partition has slices, 0 if not.
5407c478bd9Sstevel@tonic-gate  */
5417c478bd9Sstevel@tonic-gate static int
5427c478bd9Sstevel@tonic-gate has_slices(descriptor_t *desc, int *errp)
5437c478bd9Sstevel@tonic-gate {
5447c478bd9Sstevel@tonic-gate 	int		pnum;
5457c478bd9Sstevel@tonic-gate 	int		i;
5467c478bd9Sstevel@tonic-gate 	char		*p;
5477c478bd9Sstevel@tonic-gate 	struct ipart	iparts[FD_NUMPART];
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	if (get_parts(desc->p.disk, iparts, NULL, 0) != 0) {
5507c478bd9Sstevel@tonic-gate 	    *errp = ENODEV;
5517c478bd9Sstevel@tonic-gate 	    return (0);
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	p = strrchr(desc->name, 'p');
5557c478bd9Sstevel@tonic-gate 	if (p == NULL) {
5567c478bd9Sstevel@tonic-gate 	    p = desc->name;
5577c478bd9Sstevel@tonic-gate 	} else {
5587c478bd9Sstevel@tonic-gate 	    p++;
5597c478bd9Sstevel@tonic-gate 	}
5607c478bd9Sstevel@tonic-gate 	pnum = atoi(p);
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Slices are associated with the active solaris partition or if there
5647c478bd9Sstevel@tonic-gate 	 * is no active solaris partition, then the first solaris partition.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	*errp = 0;
5687c478bd9Sstevel@tonic-gate 	if (iparts[pnum].bootid == ACTIVE &&
5697c478bd9Sstevel@tonic-gate 	    (iparts[pnum].systid == SUNIXOS ||
5707c478bd9Sstevel@tonic-gate 	    iparts[pnum].systid == SUNIXOS2)) {
5717c478bd9Sstevel@tonic-gate 		return (1);
5727c478bd9Sstevel@tonic-gate 	} else {
5737c478bd9Sstevel@tonic-gate 	    int	active = 0;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	    /* Check if there are no active solaris partitions. */
5767c478bd9Sstevel@tonic-gate 	    for (i = 0; i < FD_NUMPART; i++) {
5777c478bd9Sstevel@tonic-gate 		if (iparts[i].bootid == ACTIVE &&
5787c478bd9Sstevel@tonic-gate 		    (iparts[i].systid == SUNIXOS ||
5797c478bd9Sstevel@tonic-gate 		    iparts[i].systid == SUNIXOS2)) {
5807c478bd9Sstevel@tonic-gate 			active = 1;
5817c478bd9Sstevel@tonic-gate 			break;
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 	    }
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	    if (!active) {
5867c478bd9Sstevel@tonic-gate 		/* Check if this is the first solaris partition. */
5877c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
5887c478bd9Sstevel@tonic-gate 		    if (iparts[i].systid == SUNIXOS ||
5897c478bd9Sstevel@tonic-gate 			iparts[i].systid == SUNIXOS2) {
5907c478bd9Sstevel@tonic-gate 			    break;
5917c478bd9Sstevel@tonic-gate 		    }
5927c478bd9Sstevel@tonic-gate 		}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 		if (i < FD_NUMPART && i == pnum) {
5957c478bd9Sstevel@tonic-gate 		    return (1);
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 	    }
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	return (0);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate static int
6047c478bd9Sstevel@tonic-gate open_disk(disk_t *diskp, char *opath, int len)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	/*
607*18c2aff7Sartem 	 * Just open the first devpath.
6087c478bd9Sstevel@tonic-gate 	 */
6097c478bd9Sstevel@tonic-gate 	if (diskp->aliases != NULL && diskp->aliases->devpaths != NULL) {
6107c478bd9Sstevel@tonic-gate #ifdef sparc
6117c478bd9Sstevel@tonic-gate 	    if (opath != NULL) {
6127c478bd9Sstevel@tonic-gate 		(void) strlcpy(opath, diskp->aliases->devpaths->devpath, len);
6137c478bd9Sstevel@tonic-gate 	    }
6147c478bd9Sstevel@tonic-gate 	    return (open(diskp->aliases->devpaths->devpath, O_RDONLY|O_NDELAY));
6157c478bd9Sstevel@tonic-gate #else
6167c478bd9Sstevel@tonic-gate 	    /* On intel we need to open partition device (e.g. c0d0p0). */
6177c478bd9Sstevel@tonic-gate 	    char	part_dev[MAXPATHLEN];
6187c478bd9Sstevel@tonic-gate 	    char	*p;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	    (void) strlcpy(part_dev, diskp->aliases->devpaths->devpath,
6217c478bd9Sstevel@tonic-gate 		sizeof (part_dev));
6227c478bd9Sstevel@tonic-gate 	    p = strrchr(part_dev, '/');
6237c478bd9Sstevel@tonic-gate 	    if (p == NULL) {
6247c478bd9Sstevel@tonic-gate 		p = strrchr(part_dev, 's');
6257c478bd9Sstevel@tonic-gate 		if (p != NULL) {
6267c478bd9Sstevel@tonic-gate 		    *p = 'p';
6277c478bd9Sstevel@tonic-gate 		}
6287c478bd9Sstevel@tonic-gate 	    } else {
6297c478bd9Sstevel@tonic-gate 		char *ps;
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 		*p = 0;
6327c478bd9Sstevel@tonic-gate 		ps = strrchr((p + 1), 's');
6337c478bd9Sstevel@tonic-gate 		if (ps != NULL) {
6347c478bd9Sstevel@tonic-gate 		    *ps = 'p';
6357c478bd9Sstevel@tonic-gate 		}
6367c478bd9Sstevel@tonic-gate 		*p = '/';
6377c478bd9Sstevel@tonic-gate 	    }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 	    if (opath != NULL) {
6407c478bd9Sstevel@tonic-gate 		(void) strlcpy(opath, part_dev, len);
6417c478bd9Sstevel@tonic-gate 	    }
6427c478bd9Sstevel@tonic-gate 	    return (open(part_dev, O_RDONLY|O_NDELAY));
6437c478bd9Sstevel@tonic-gate #endif
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	return (-1);
6477c478bd9Sstevel@tonic-gate }
648