xref: /titanic_51/usr/src/cmd/format/menu_fdisk.c (revision a29e56d91db891741f1af9f6bbd3e3c3cac5f19b)
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
50972747aSyl194034  * Common Development and Distribution License (the "License").
60972747aSyl194034  * 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 /*
22589271a4SSheng-Liang Eric Zhang  * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved.
23*a29e56d9SToomas Soome  * Copyright 2016 Toomas Soome <tsoome@me.com>
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This file contains functions that implement the fdisk menu commands.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include "global.h"
307c478bd9Sstevel@tonic-gate #include <errno.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/resource.h>
337c478bd9Sstevel@tonic-gate #include <sys/wait.h>
347c478bd9Sstevel@tonic-gate #include <signal.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <sys/dklabel.h>
41aa1b14e7SSheshadri Vasudevan #ifdef i386
42aa1b14e7SSheshadri Vasudevan #include <libfdisk.h>
43aa1b14e7SSheshadri Vasudevan #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "main.h"
467c478bd9Sstevel@tonic-gate #include "analyze.h"
477c478bd9Sstevel@tonic-gate #include "menu.h"
487c478bd9Sstevel@tonic-gate #include "menu_command.h"
497c478bd9Sstevel@tonic-gate #include "menu_defect.h"
507c478bd9Sstevel@tonic-gate #include "menu_partition.h"
517c478bd9Sstevel@tonic-gate #include "menu_fdisk.h"
527c478bd9Sstevel@tonic-gate #include "param.h"
537c478bd9Sstevel@tonic-gate #include "misc.h"
547c478bd9Sstevel@tonic-gate #include "label.h"
557c478bd9Sstevel@tonic-gate #include "startup.h"
567c478bd9Sstevel@tonic-gate #include "partition.h"
577c478bd9Sstevel@tonic-gate #include "prompts.h"
583e1bd7a2Ssjelinek #include "checkdev.h"
597c478bd9Sstevel@tonic-gate #include "io.h"
607c478bd9Sstevel@tonic-gate #include "ctlr_scsi.h"
617c478bd9Sstevel@tonic-gate #include "auto_sense.h"
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate extern	struct menu_item menu_fdisk[];
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Byte swapping macros for accessing struct ipart
677c478bd9Sstevel@tonic-gate  *	to resolve little endian on Sparc.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate #if defined(sparc)
707c478bd9Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
717c478bd9Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
727c478bd9Sstevel@tonic-gate 			(les((unsigned)((val)&0xffff0000)>>16)))
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #elif	defined(i386)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	les(val)	(val)
777c478bd9Sstevel@tonic-gate #define	lel(val)	(val)
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #else	/* defined(sparc) */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #error	No Platform defined
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* Function prototypes */
877c478bd9Sstevel@tonic-gate #ifdef	__STDC__
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #if	defined(sparc)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate static int getbyte(uchar_t **);
927c478bd9Sstevel@tonic-gate static int getlong(uchar_t **);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static int get_solaris_part(int fd, struct ipart *ipart);
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #if	defined(sparc)
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static int getbyte();
1037c478bd9Sstevel@tonic-gate static int getlong();
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate static int get_solaris_part();
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
1107c478bd9Sstevel@tonic-gate 
111aa1b14e7SSheshadri Vasudevan #ifdef i386
112aa1b14e7SSheshadri Vasudevan int extpart_init(ext_part_t **epp);
113aa1b14e7SSheshadri Vasudevan #endif
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * Handling the alignment problem of struct ipart.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate static void
1187c478bd9Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate #if defined(sparc)
1217c478bd9Sstevel@tonic-gate 	/*
1227c478bd9Sstevel@tonic-gate 	 * Sparc platform:
1237c478bd9Sstevel@tonic-gate 	 *
1247c478bd9Sstevel@tonic-gate 	 * Packing short/word for struct ipart to resolve
1257c478bd9Sstevel@tonic-gate 	 *	little endian on Sparc since it is not
1267c478bd9Sstevel@tonic-gate 	 *	properly aligned on Sparc.
1277c478bd9Sstevel@tonic-gate 	 */
1287c478bd9Sstevel@tonic-gate 	partp->bootid = getbyte((uchar_t **)&bootptr);
1297c478bd9Sstevel@tonic-gate 	partp->beghead = getbyte((uchar_t **)&bootptr);
1307c478bd9Sstevel@tonic-gate 	partp->begsect = getbyte((uchar_t **)&bootptr);
1317c478bd9Sstevel@tonic-gate 	partp->begcyl = getbyte((uchar_t **)&bootptr);
1327c478bd9Sstevel@tonic-gate 	partp->systid = getbyte((uchar_t **)&bootptr);
1337c478bd9Sstevel@tonic-gate 	partp->endhead = getbyte((uchar_t **)&bootptr);
1347c478bd9Sstevel@tonic-gate 	partp->endsect = getbyte((uchar_t **)&bootptr);
1357c478bd9Sstevel@tonic-gate 	partp->endcyl = getbyte((uchar_t **)&bootptr);
1367c478bd9Sstevel@tonic-gate 	partp->relsect = getlong((uchar_t **)&bootptr);
1377c478bd9Sstevel@tonic-gate 	partp->numsect = getlong((uchar_t **)&bootptr);
1387c478bd9Sstevel@tonic-gate #elif defined(i386)
1397c478bd9Sstevel@tonic-gate 	/*
1407c478bd9Sstevel@tonic-gate 	 * i386 platform:
1417c478bd9Sstevel@tonic-gate 	 *
1427c478bd9Sstevel@tonic-gate 	 * The fdisk table does not begin on a 4-byte boundary within
1437c478bd9Sstevel@tonic-gate 	 * the master boot record; so, we need to recopy its contents
1447c478bd9Sstevel@tonic-gate 	 * to another data structure to avoid an alignment exception.
1457c478bd9Sstevel@tonic-gate 	 */
1467c478bd9Sstevel@tonic-gate 	(void) bcopy(bootptr, partp, sizeof (struct ipart));
1477c478bd9Sstevel@tonic-gate #else
1487c478bd9Sstevel@tonic-gate #error  No Platform defined
1497c478bd9Sstevel@tonic-gate #endif /* defined(sparc) */
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Get a correct byte/short/word routines for Sparc platform.
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate #if defined(sparc)
1567c478bd9Sstevel@tonic-gate static int
1577c478bd9Sstevel@tonic-gate getbyte(uchar_t **bp)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	int	b;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	b = **bp;
1627c478bd9Sstevel@tonic-gate 	*bp = *bp + 1;
1637c478bd9Sstevel@tonic-gate 	return (b);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate #ifdef DEADCODE
1677c478bd9Sstevel@tonic-gate static int
1687c478bd9Sstevel@tonic-gate getshort(uchar_t **bp)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int	b;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	b = ((**bp) << 8) | *(*bp + 1);
1737c478bd9Sstevel@tonic-gate 	*bp += 2;
1747c478bd9Sstevel@tonic-gate 	return (b);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate #endif /* DEADCODE */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int
1797c478bd9Sstevel@tonic-gate getlong(uchar_t **bp)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	int	b, bh, bl;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
1847c478bd9Sstevel@tonic-gate 	*bp += 2;
1857c478bd9Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
1867c478bd9Sstevel@tonic-gate 	*bp += 2;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	b = (bh << 16) | bl;
1897c478bd9Sstevel@tonic-gate 	return (b);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate #endif /* defined(sparc) */
1927c478bd9Sstevel@tonic-gate 
193589271a4SSheng-Liang Eric Zhang #ifdef i386
194589271a4SSheng-Liang Eric Zhang /*
195589271a4SSheng-Liang Eric Zhang  * Convert emcpowerN[a-p,p0,p1,p2,p3,p4] to emcpowerNp0 path,
196589271a4SSheng-Liang Eric Zhang  * this is specific for emc powerpath driver.
197589271a4SSheng-Liang Eric Zhang  */
198589271a4SSheng-Liang Eric Zhang static void
199589271a4SSheng-Liang Eric Zhang get_emcpower_pname(char *name, char *devname)
200589271a4SSheng-Liang Eric Zhang {
201589271a4SSheng-Liang Eric Zhang 	char	*emcp = "emcpower";
202589271a4SSheng-Liang Eric Zhang 	char	*npt = NULL;
203589271a4SSheng-Liang Eric Zhang 	char	np[MAXNAMELEN];
204589271a4SSheng-Liang Eric Zhang 	int	i = strlen(emcp);
205589271a4SSheng-Liang Eric Zhang 
206589271a4SSheng-Liang Eric Zhang 	(void) strcpy(np, devname);
207589271a4SSheng-Liang Eric Zhang 	npt = strstr(np, emcp);
208589271a4SSheng-Liang Eric Zhang 	while ((i < strlen(npt)) && (isdigit(npt[i])))
209589271a4SSheng-Liang Eric Zhang 		i++;
210589271a4SSheng-Liang Eric Zhang 	npt[i] = '\0';
211589271a4SSheng-Liang Eric Zhang 	(void) snprintf(name, MAXNAMELEN, "/dev/rdsk/%sp0", npt);
212589271a4SSheng-Liang Eric Zhang }
213589271a4SSheng-Liang Eric Zhang #endif
214589271a4SSheng-Liang Eric Zhang 
215895142e0Syl194034 /*
216895142e0Syl194034  * Convert cn[tn]dn to cn[tn]dns2 path
217895142e0Syl194034  */
218895142e0Syl194034 static void
219895142e0Syl194034 get_sname(char *name)
220895142e0Syl194034 {
221895142e0Syl194034 	char		buf[MAXPATHLEN];
222895142e0Syl194034 	char		*devp = "/dev/dsk";
223895142e0Syl194034 	char		*rdevp = "/dev/rdsk";
224895142e0Syl194034 	char		np[MAXNAMELEN];
225895142e0Syl194034 	char		*npt;
226895142e0Syl194034 
227589271a4SSheng-Liang Eric Zhang #ifdef i386
228589271a4SSheng-Liang Eric Zhang 	if (emcpower_name(cur_disk->disk_name)) {
229589271a4SSheng-Liang Eric Zhang 		get_emcpower_pname(name, cur_disk->disk_name);
230589271a4SSheng-Liang Eric Zhang 		return;
231589271a4SSheng-Liang Eric Zhang 	}
232589271a4SSheng-Liang Eric Zhang #endif
233589271a4SSheng-Liang Eric Zhang 
234895142e0Syl194034 	/*
235895142e0Syl194034 	 * If it is a full path /dev/[r]dsk/cn[tn]dn, use this path
236895142e0Syl194034 	 */
237895142e0Syl194034 	(void) strcpy(np, cur_disk->disk_name);
238895142e0Syl194034 	if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 ||
239895142e0Syl194034 	    strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) {
240895142e0Syl194034 		/*
241895142e0Syl194034 		 * Skip if the path is already included with sN
242895142e0Syl194034 		 */
243895142e0Syl194034 		if (strchr(np, 's') == strrchr(np, 's')) {
244895142e0Syl194034 			npt = strrchr(np, 'p');
245895142e0Syl194034 			/* If pN is found, do not include it */
24676871191Syl194034 			if (npt != NULL) {
24776871191Syl194034 				*npt = '\0';
248895142e0Syl194034 			}
249895142e0Syl194034 			(void) snprintf(buf, sizeof (buf), "%ss2", np);
250895142e0Syl194034 		} else {
251895142e0Syl194034 			(void) snprintf(buf, sizeof (buf), "%s", np);
252895142e0Syl194034 		}
253895142e0Syl194034 	} else {
254895142e0Syl194034 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%ss2", np);
255895142e0Syl194034 	}
256895142e0Syl194034 	(void) strcpy(name, buf);
257895142e0Syl194034 }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
2607c478bd9Sstevel@tonic-gate  * Convert cn[tn]dnsn to cn[tn]dnp0 path
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate static void
2637c478bd9Sstevel@tonic-gate get_pname(char *name)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
2667c478bd9Sstevel@tonic-gate 	char		*devp = "/dev/dsk";
2677c478bd9Sstevel@tonic-gate 	char		*rdevp = "/dev/rdsk";
2687c478bd9Sstevel@tonic-gate 	char		np[MAXNAMELEN];
2697c478bd9Sstevel@tonic-gate 	char		*npt;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path
2737c478bd9Sstevel@tonic-gate 	 */
2740972747aSyl194034 	if (cur_disk == NULL) {
2750972747aSyl194034 		(void) strcpy(np, x86_devname);
2760972747aSyl194034 	} else {
2777c478bd9Sstevel@tonic-gate 		(void) strcpy(np, cur_disk->disk_name);
2780972747aSyl194034 	}
2790972747aSyl194034 
280589271a4SSheng-Liang Eric Zhang #ifdef i386
281589271a4SSheng-Liang Eric Zhang 	if (emcpower_name(np)) {
282589271a4SSheng-Liang Eric Zhang 		get_emcpower_pname(name, np);
283589271a4SSheng-Liang Eric Zhang 		return;
284589271a4SSheng-Liang Eric Zhang 	}
285589271a4SSheng-Liang Eric Zhang #endif
286589271a4SSheng-Liang Eric Zhang 
2870972747aSyl194034 	if (strncmp(rdevp, np, strlen(rdevp)) == 0 ||
2880972747aSyl194034 	    strncmp(devp, np, strlen(devp)) == 0) {
2897c478bd9Sstevel@tonic-gate 		/*
2907c478bd9Sstevel@tonic-gate 		 * Skip if the path is already included with pN
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		if (strchr(np, 'p') == NULL) {
2937c478bd9Sstevel@tonic-gate 			npt = strrchr(np, 's');
2947c478bd9Sstevel@tonic-gate 			/* If sN is found, do not include it */
2957c478bd9Sstevel@tonic-gate 			if (isdigit(*++npt)) {
2967c478bd9Sstevel@tonic-gate 				*--npt = '\0';
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%sp0", np);
2997c478bd9Sstevel@tonic-gate 		} else {
3007c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", np);
3017c478bd9Sstevel@tonic-gate 		}
3027c478bd9Sstevel@tonic-gate 	} else {
3037c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	(void) strcpy(name, buf);
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Open file descriptor for current disk (cur_file)
3107c478bd9Sstevel@tonic-gate  *	with "p0" path or cur_disk->disk_path
3117c478bd9Sstevel@tonic-gate  */
3127c478bd9Sstevel@tonic-gate void
3137c478bd9Sstevel@tonic-gate open_cur_file(int mode)
3147c478bd9Sstevel@tonic-gate {
3157c478bd9Sstevel@tonic-gate 	char	*dkpath;
3167c478bd9Sstevel@tonic-gate 	char	pbuf[MAXPATHLEN];
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	switch (mode) {
3197c478bd9Sstevel@tonic-gate 		case FD_USE_P0_PATH:
3207c478bd9Sstevel@tonic-gate 			(void) get_pname(&pbuf[0]);
3217c478bd9Sstevel@tonic-gate 			dkpath = pbuf;
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		case FD_USE_CUR_DISK_PATH:
324895142e0Syl194034 			if (cur_disk->fdisk_part.systid == SUNIXOS ||
325895142e0Syl194034 			    cur_disk->fdisk_part.systid == SUNIXOS2) {
326895142e0Syl194034 				(void) get_sname(&pbuf[0]);
327895142e0Syl194034 				dkpath = pbuf;
328895142e0Syl194034 			} else {
3297c478bd9Sstevel@tonic-gate 				dkpath = cur_disk->disk_path;
330895142e0Syl194034 			}
3317c478bd9Sstevel@tonic-gate 			break;
3327c478bd9Sstevel@tonic-gate 		default:
333aa1b14e7SSheshadri Vasudevan 			err_print("Error: Invalid mode option for opening "
334aa1b14e7SSheshadri Vasudevan 			    "cur_file\n");
3357c478bd9Sstevel@tonic-gate 			fullabort();
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	/* Close previous cur_file */
3397c478bd9Sstevel@tonic-gate 	(void) close(cur_file);
3407c478bd9Sstevel@tonic-gate 	/* Open cur_file with the required path dkpath */
3417c478bd9Sstevel@tonic-gate 	if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) {
3427c478bd9Sstevel@tonic-gate 		err_print(
3437c478bd9Sstevel@tonic-gate 		    "Error: can't open selected disk '%s'.\n", dkpath);
3447c478bd9Sstevel@tonic-gate 		fullabort();
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /*
3507c478bd9Sstevel@tonic-gate  * This routine implements the 'fdisk' command.  It simply runs
3517c478bd9Sstevel@tonic-gate  * the fdisk command on the current disk.
3527c478bd9Sstevel@tonic-gate  * Use of this is restricted to interactive mode only.
3537c478bd9Sstevel@tonic-gate  */
3547c478bd9Sstevel@tonic-gate int
3557c478bd9Sstevel@tonic-gate c_fdisk()
3567c478bd9Sstevel@tonic-gate {
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
3597c478bd9Sstevel@tonic-gate 	char		pbuf[MAXPATHLEN];
3607c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	/*
3637c478bd9Sstevel@tonic-gate 	 * We must be in interactive mode to use the fdisk command
3647c478bd9Sstevel@tonic-gate 	 */
3657c478bd9Sstevel@tonic-gate 	if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) {
3667c478bd9Sstevel@tonic-gate 		err_print("Fdisk command is for interactive use only!\n");
3677c478bd9Sstevel@tonic-gate 		return (-1);
3687c478bd9Sstevel@tonic-gate 	}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	/*
3717c478bd9Sstevel@tonic-gate 	 * There must be a current disk type and a current disk
3727c478bd9Sstevel@tonic-gate 	 */
3737c478bd9Sstevel@tonic-gate 	if (cur_dtype == NULL) {
3747c478bd9Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
3757c478bd9Sstevel@tonic-gate 		return (-1);
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	/*
3797c478bd9Sstevel@tonic-gate 	 * Before running the fdisk command, get file status of
3807c478bd9Sstevel@tonic-gate 	 *	/dev/rdsk/cn[tn]dnp0 path to see if this disk
3817c478bd9Sstevel@tonic-gate 	 *	supports fixed disk partition table.
3827c478bd9Sstevel@tonic-gate 	 */
3837c478bd9Sstevel@tonic-gate 	(void) get_pname(&pbuf[0]);
3847c478bd9Sstevel@tonic-gate 	if (stat(pbuf, (struct stat *)&statbuf) == -1 ||
3857c478bd9Sstevel@tonic-gate 	    !S_ISCHR(statbuf.st_mode)) {
3867c478bd9Sstevel@tonic-gate 		err_print(
3877c478bd9Sstevel@tonic-gate 		"Disk does not support fixed disk partition table\n");
3887c478bd9Sstevel@tonic-gate 		return (0);
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	 * Run the fdisk program.
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf);
3957c478bd9Sstevel@tonic-gate 	(void) system(buf);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/*
3987c478bd9Sstevel@tonic-gate 	 * Open cur_file with "p0" path for accessing the fdisk table
3997c478bd9Sstevel@tonic-gate 	 */
4007c478bd9Sstevel@tonic-gate 	(void) open_cur_file(FD_USE_P0_PATH);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	/*
4037c478bd9Sstevel@tonic-gate 	 * Get solaris partition information in the fdisk partition table
4047c478bd9Sstevel@tonic-gate 	 */
4057c478bd9Sstevel@tonic-gate 	if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) {
4067c478bd9Sstevel@tonic-gate 		err_print("No fdisk solaris partition found\n");
4077c478bd9Sstevel@tonic-gate 		cur_disk->fdisk_part.numsect = 0;  /* No Solaris */
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * Restore cur_file with cur_disk->disk_path
4127c478bd9Sstevel@tonic-gate 	 */
4137c478bd9Sstevel@tonic-gate 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Read MBR on the disk
4207c478bd9Sstevel@tonic-gate  * if the Solaris partition has changed,
4217c478bd9Sstevel@tonic-gate  *	reread the vtoc
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate #ifdef DEADCODE
4247c478bd9Sstevel@tonic-gate static void
4257c478bd9Sstevel@tonic-gate update_cur_parts()
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	int i;
4297c478bd9Sstevel@tonic-gate 	register struct partition_info *parts;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
4327c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4337c478bd9Sstevel@tonic-gate 		if (cur_parts->vtoc.v_part[i].p_tag &&
4347c478bd9Sstevel@tonic-gate 		    cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) {
4357c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_start = 0;
4367c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_size = 0;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate #endif
4397c478bd9Sstevel@tonic-gate 			cur_parts->pinfo_map[i].dkl_nblk = 0;
4407c478bd9Sstevel@tonic-gate 			cur_parts->pinfo_map[i].dkl_cylno = 0;
4417c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_tag =
4427c478bd9Sstevel@tonic-gate 			    default_vtoc_map[i].p_tag;
4437c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_flag =
4447c478bd9Sstevel@tonic-gate 			    default_vtoc_map[i].p_flag;
4457c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4467c478bd9Sstevel@tonic-gate 		}
4477c478bd9Sstevel@tonic-gate #endif
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc();
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4527c478bd9Sstevel@tonic-gate 	/*
4537c478bd9Sstevel@tonic-gate 	 * Adjust for the boot partitions
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc();
4567c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0;
4577c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[C_PARTITION].p_start =
4587c478bd9Sstevel@tonic-gate 	    cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect;
4597c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[C_PARTITION].p_size =
4607c478bd9Sstevel@tonic-gate 	    cur_parts->pinfo_map[C_PARTITION].dkl_nblk;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[I_PARTITION].p_start =
4637c478bd9Sstevel@tonic-gate 	    cur_parts->pinfo_map[I_PARTITION].dkl_cylno;
4647c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[I_PARTITION].p_size =
4657c478bd9Sstevel@tonic-gate 	    cur_parts->pinfo_map[I_PARTITION].dkl_nblk;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate #endif	/* defined(_SUNOS_VTOC_16) */
4687c478bd9Sstevel@tonic-gate 	parts = cur_dtype->dtype_plist;
4697c478bd9Sstevel@tonic-gate 	cur_dtype->dtype_ncyl = ncyl;
4707c478bd9Sstevel@tonic-gate 	cur_dtype->dtype_plist = cur_parts;
4717c478bd9Sstevel@tonic-gate 	parts->pinfo_name = cur_parts->pinfo_name;
4727c478bd9Sstevel@tonic-gate 	cur_disk->disk_parts = cur_parts;
4737c478bd9Sstevel@tonic-gate 	cur_ctype->ctype_dlist = cur_dtype;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate }
4767c478bd9Sstevel@tonic-gate #endif /* DEADCODE */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate static int
4797c478bd9Sstevel@tonic-gate get_solaris_part(int fd, struct ipart *ipart)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	int		i;
4827c478bd9Sstevel@tonic-gate 	struct ipart	ip;
4837c478bd9Sstevel@tonic-gate 	int		status;
48465908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
4857c478bd9Sstevel@tonic-gate 	char		*bootptr;
48641ebd847Syl194034 	struct dk_label	update_label;
487aa1b14e7SSheshadri Vasudevan 	ushort_t	found = 0;
488aa1b14e7SSheshadri Vasudevan #ifdef i386
489aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
490aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
491aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
492aa1b14e7SSheshadri Vasudevan #endif
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	(void) lseek(fd, 0, 0);
4957c478bd9Sstevel@tonic-gate 
49665908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
49765908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
49865908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
49965908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
50065908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
50165908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
50265908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
5037c478bd9Sstevel@tonic-gate 		return (-1);
5047c478bd9Sstevel@tonic-gate 	}
50565908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
50665908c77Syu, larry liu - Sun Microsystems - Beijing China 
50765908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
50865908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("Bad read of fdisk partition. Status = %x\n", status);
50965908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("Cannot read fdisk partition information.\n");
51065908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
51165908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
51265908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
51365908c77Syu, larry liu - Sun Microsystems - Beijing China 
51465908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&boot_sec, mbr, sizeof (struct mboot));
51565908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(mbr);
5167c478bd9Sstevel@tonic-gate 
517e998e519SSheshadri Vasudevan #ifdef i386
518e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
519e998e519SSheshadri Vasudevan #endif
5207c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
5217c478bd9Sstevel@tonic-gate 		int	ipc;
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
5267c478bd9Sstevel@tonic-gate 		bootptr = &boot_sec.parts[ipc];
5277c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
5287c478bd9Sstevel@tonic-gate 
529aa1b14e7SSheshadri Vasudevan #ifdef i386
530aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
531aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
532aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
533aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
534aa1b14e7SSheshadri Vasudevan 			    &numsec);
535aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
536aa1b14e7SSheshadri Vasudevan 				/*
537aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
538aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
539aa1b14e7SSheshadri Vasudevan 				 */
540aa1b14e7SSheshadri Vasudevan 				if (nhead != 0 && nsect != 0) {
541aa1b14e7SSheshadri Vasudevan 					pcyl = numsec / (nhead * nsect);
542aa1b14e7SSheshadri Vasudevan 					xstart = relsec / (nhead * nsect);
543aa1b14e7SSheshadri Vasudevan 					ncyl = pcyl - acyl;
544aa1b14e7SSheshadri Vasudevan 				}
545aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
546aa1b14e7SSheshadri Vasudevan 				found = 2;
547aa1b14e7SSheshadri Vasudevan 				ip.bootid = 0;
548aa1b14e7SSheshadri Vasudevan 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
549aa1b14e7SSheshadri Vasudevan 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
550aa1b14e7SSheshadri Vasudevan 				ip.systid = SUNIXOS2;
551aa1b14e7SSheshadri Vasudevan 				ip.relsect = relsec;
552aa1b14e7SSheshadri Vasudevan 				ip.numsect = numsec;
553aa1b14e7SSheshadri Vasudevan 				ipart->bootid = ip.bootid;
554aa1b14e7SSheshadri Vasudevan 				status = bcmp(&ip, ipart,
555aa1b14e7SSheshadri Vasudevan 				    sizeof (struct ipart));
556aa1b14e7SSheshadri Vasudevan 				bcopy(&ip, ipart, sizeof (struct ipart));
557aa1b14e7SSheshadri Vasudevan 			}
558aa1b14e7SSheshadri Vasudevan 			continue;
559aa1b14e7SSheshadri Vasudevan 		}
560aa1b14e7SSheshadri Vasudevan #endif
561aa1b14e7SSheshadri Vasudevan 
5627c478bd9Sstevel@tonic-gate 		/*
5637c478bd9Sstevel@tonic-gate 		 * we are interested in Solaris and EFI partition types
5647c478bd9Sstevel@tonic-gate 		 */
565e998e519SSheshadri Vasudevan #ifdef i386
566e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
567e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
568e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
569e998e519SSheshadri Vasudevan 		    ip.systid == EFI_PMBR) {
570e998e519SSheshadri Vasudevan #else
5717c478bd9Sstevel@tonic-gate 		if (ip.systid == SUNIXOS ||
5727c478bd9Sstevel@tonic-gate 		    ip.systid == SUNIXOS2 ||
5737c478bd9Sstevel@tonic-gate 		    ip.systid == EFI_PMBR) {
574e998e519SSheshadri Vasudevan #endif
5757c478bd9Sstevel@tonic-gate 			/*
5767c478bd9Sstevel@tonic-gate 			 * if the disk has an EFI label, nhead and nsect may
5777c478bd9Sstevel@tonic-gate 			 * be zero.  This test protects us from FPE's, and
5787c478bd9Sstevel@tonic-gate 			 * format still seems to work fine
5797c478bd9Sstevel@tonic-gate 			 */
5807c478bd9Sstevel@tonic-gate 			if (nhead != 0 && nsect != 0) {
5817c478bd9Sstevel@tonic-gate 				pcyl = lel(ip.numsect) / (nhead * nsect);
5827c478bd9Sstevel@tonic-gate 				xstart = lel(ip.relsect) / (nhead * nsect);
5837c478bd9Sstevel@tonic-gate 				ncyl = pcyl - acyl;
5847c478bd9Sstevel@tonic-gate 			}
5857c478bd9Sstevel@tonic-gate #ifdef DEBUG
5867c478bd9Sstevel@tonic-gate 			else {
5877c478bd9Sstevel@tonic-gate 				err_print("Critical geometry values are zero:\n"
588aa1b14e7SSheshadri Vasudevan 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5917c478bd9Sstevel@tonic-gate 
592342440ecSPrasad Singamsetty 			solaris_offset = (uint_t)lel(ip.relsect);
593aa1b14e7SSheshadri Vasudevan 			found = 1;
5947c478bd9Sstevel@tonic-gate 			break;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
598e998e519SSheshadri Vasudevan #ifdef i386
599e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
600e998e519SSheshadri Vasudevan #endif
601e998e519SSheshadri Vasudevan 
602aa1b14e7SSheshadri Vasudevan 	if (!found) {
6037c478bd9Sstevel@tonic-gate 		err_print("Solaris fdisk partition not found\n");
6047c478bd9Sstevel@tonic-gate 		return (-1);
605aa1b14e7SSheshadri Vasudevan 	} else if (found == 1) {
6067c478bd9Sstevel@tonic-gate 		/*
607aa1b14e7SSheshadri Vasudevan 		 * Found a primary solaris partition.
6087c478bd9Sstevel@tonic-gate 		 * compare the previous and current Solaris partition
609aa1b14e7SSheshadri Vasudevan 		 * but don't use bootid in determination of Solaris partition
610aa1b14e7SSheshadri Vasudevan 		 * changes
6117c478bd9Sstevel@tonic-gate 		 */
6127c478bd9Sstevel@tonic-gate 		ipart->bootid = ip.bootid;
6137c478bd9Sstevel@tonic-gate 		status = bcmp(&ip, ipart, sizeof (struct ipart));
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 		bcopy(&ip, ipart, sizeof (struct ipart));
616aa1b14e7SSheshadri Vasudevan 	}
6177c478bd9Sstevel@tonic-gate 
6189773101cSdduvall 	/* if the disk partitioning has changed - get the VTOC */
6197c478bd9Sstevel@tonic-gate 	if (status) {
620342440ecSPrasad Singamsetty 		struct extvtoc exvtoc;
621342440ecSPrasad Singamsetty 		struct vtoc vtoc;
622342440ecSPrasad Singamsetty 
623342440ecSPrasad Singamsetty 		status = ioctl(fd, DKIOCGEXTVTOC, &exvtoc);
6247c478bd9Sstevel@tonic-gate 		if (status == -1) {
6257c478bd9Sstevel@tonic-gate 			i = errno;
626342440ecSPrasad Singamsetty 			/* Try the old ioctl DKIOCGVTOC */
627342440ecSPrasad Singamsetty 			status = ioctl(fd, DKIOCGVTOC, &vtoc);
628342440ecSPrasad Singamsetty 			if (status == -1) {
629342440ecSPrasad Singamsetty 				err_print("Bad ioctl DKIOCGEXTVTOC.\n");
6307c478bd9Sstevel@tonic-gate 				err_print("errno=%d %s\n", i, strerror(i));
6317c478bd9Sstevel@tonic-gate 				err_print("Cannot read vtoc information.\n");
6327c478bd9Sstevel@tonic-gate 				return (-1);
6337c478bd9Sstevel@tonic-gate 			}
634342440ecSPrasad Singamsetty 		}
63541ebd847Syl194034 
63641ebd847Syl194034 		status = read_label(fd, &update_label);
63741ebd847Syl194034 		if (status == -1) {
63841ebd847Syl194034 			err_print("Cannot read label information.\n");
63941ebd847Syl194034 			return (-1);
64041ebd847Syl194034 		}
64141ebd847Syl194034 
642342440ecSPrasad Singamsetty 		/* copy vtoc information */
643342440ecSPrasad Singamsetty 		cur_parts->vtoc = update_label.dkl_vtoc;
644342440ecSPrasad Singamsetty 
64541ebd847Syl194034 #if defined(_SUNOS_VTOC_16)
64641ebd847Syl194034 		/*
64741ebd847Syl194034 		 * this is to update the slice table on x86
64841ebd847Syl194034 		 * we don't care about VTOC8 here
64941ebd847Syl194034 		 */
65041ebd847Syl194034 		for (i = 0; i < NDKMAP; i ++) {
65141ebd847Syl194034 			cur_parts->pinfo_map[i].dkl_cylno =
65241ebd847Syl194034 			    update_label.dkl_vtoc.v_part[i].p_start /
65341ebd847Syl194034 			    ((int)(update_label.dkl_nhead *
65441ebd847Syl194034 			    update_label.dkl_nsect));
65541ebd847Syl194034 			cur_parts->pinfo_map[i].dkl_nblk =
65641ebd847Syl194034 			    update_label.dkl_vtoc.v_part[i].p_size;
65741ebd847Syl194034 		}
65841ebd847Syl194034 #endif /* defined(_SUNOS_VTOC_16) */
65941ebd847Syl194034 
66041ebd847Syl194034 		cur_dtype->dtype_ncyl = update_label.dkl_ncyl;
66141ebd847Syl194034 		cur_dtype->dtype_pcyl = update_label.dkl_pcyl;
66241ebd847Syl194034 		cur_dtype->dtype_acyl = update_label.dkl_acyl;
66341ebd847Syl194034 		cur_dtype->dtype_nhead = update_label.dkl_nhead;
66441ebd847Syl194034 		cur_dtype->dtype_nsect = update_label.dkl_nsect;
66541ebd847Syl194034 		ncyl = cur_dtype->dtype_ncyl;
66641ebd847Syl194034 		acyl = cur_dtype->dtype_acyl;
66741ebd847Syl194034 		pcyl = cur_dtype->dtype_pcyl;
66841ebd847Syl194034 		nsect = cur_dtype->dtype_nsect;
66941ebd847Syl194034 		nhead = cur_dtype->dtype_nhead;
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	return (0);
6727c478bd9Sstevel@tonic-gate }
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 
6757c478bd9Sstevel@tonic-gate int
6767c478bd9Sstevel@tonic-gate copy_solaris_part(struct ipart *ipart)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	int		status, i, fd;
6807c478bd9Sstevel@tonic-gate 	struct mboot	mboot;
68165908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
6827c478bd9Sstevel@tonic-gate 	struct ipart	ip;
6837c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
6847c478bd9Sstevel@tonic-gate 	char		*bootptr;
6857c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
686aa1b14e7SSheshadri Vasudevan #ifdef i386
687aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
688aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
689aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
690aa1b14e7SSheshadri Vasudevan #endif
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	(void) get_pname(&buf[0]);
6937c478bd9Sstevel@tonic-gate 	if (stat(buf, &statbuf) == -1 ||
6947c478bd9Sstevel@tonic-gate 	    !S_ISCHR(statbuf.st_mode) ||
6957c478bd9Sstevel@tonic-gate 	    ((cur_label == L_TYPE_EFI) &&
6967c478bd9Sstevel@tonic-gate 	    (cur_disk->disk_flags & DSK_LABEL_DIRTY))) {
6977c478bd9Sstevel@tonic-gate 		/*
6987c478bd9Sstevel@tonic-gate 		 * Make sure to reset solaris_offset to zero if it is
6997c478bd9Sstevel@tonic-gate 		 *	previously set by a selected disk that
7007c478bd9Sstevel@tonic-gate 		 *	supports the fdisk table.
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		solaris_offset = 0;
7037c478bd9Sstevel@tonic-gate 		/*
7047c478bd9Sstevel@tonic-gate 		 * Return if this disk does not support fdisk table or
7057c478bd9Sstevel@tonic-gate 		 * if it uses an EFI label but has not yet been labelled.
7067c478bd9Sstevel@tonic-gate 		 * If the EFI label has not been written then the open
7077c478bd9Sstevel@tonic-gate 		 * on the partition will fail.
7087c478bd9Sstevel@tonic-gate 		 */
7097c478bd9Sstevel@tonic-gate 		return (0);
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	if ((fd = open(buf, O_RDONLY)) < 0) {
713*a29e56d9SToomas Soome 		/*
714*a29e56d9SToomas Soome 		 * Labeled device support in lofi provides p0 partition on
715*a29e56d9SToomas Soome 		 * both x86 and sparc. However, sparc does not implement fdisk
716*a29e56d9SToomas Soome 		 * partitioning. This workaround will allow format
717*a29e56d9SToomas Soome 		 * to ignore fdisk errors in case of lofi devices.
718*a29e56d9SToomas Soome 		 */
719*a29e56d9SToomas Soome 		if (strcmp(cur_disk->disk_dkinfo.dki_cname, "lofi") == 0) {
720*a29e56d9SToomas Soome 			solaris_offset = 0;
721*a29e56d9SToomas Soome 			return (0);
722*a29e56d9SToomas Soome 		}
7237c478bd9Sstevel@tonic-gate 		err_print("Error: can't open disk '%s'.\n", buf);
7247c478bd9Sstevel@tonic-gate 		return (-1);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
72765908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
72865908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
72965908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
73065908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
73165908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
73265908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
73365908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
7347c478bd9Sstevel@tonic-gate 		return (-1);
7357c478bd9Sstevel@tonic-gate 	}
73665908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
73765908c77Syu, larry liu - Sun Microsystems - Beijing China 
73865908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
73965908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("Bad read of fdisk partition.\n");
74065908c77Syu, larry liu - Sun Microsystems - Beijing China 		(void) close(fd);
74165908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
74265908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
74365908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
74465908c77Syu, larry liu - Sun Microsystems - Beijing China 
74565908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
7467c478bd9Sstevel@tonic-gate 
747e998e519SSheshadri Vasudevan #ifdef i386
748e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
749e998e519SSheshadri Vasudevan #endif
7507c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
7517c478bd9Sstevel@tonic-gate 		int	ipc;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
7567c478bd9Sstevel@tonic-gate 		bootptr = &mboot.parts[ipc];
7577c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
7587c478bd9Sstevel@tonic-gate 
759aa1b14e7SSheshadri Vasudevan #ifdef i386
760aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
761aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
762aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
763aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
764aa1b14e7SSheshadri Vasudevan 			    &numsec);
765aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
766aa1b14e7SSheshadri Vasudevan 				/*
767aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
768aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
769aa1b14e7SSheshadri Vasudevan 				 */
770aa1b14e7SSheshadri Vasudevan 				if (nhead != 0 && nsect != 0) {
771aa1b14e7SSheshadri Vasudevan 					pcyl = numsec / (nhead * nsect);
772aa1b14e7SSheshadri Vasudevan 					ncyl = pcyl - acyl;
773aa1b14e7SSheshadri Vasudevan 				}
774aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
775aa1b14e7SSheshadri Vasudevan 				ip.bootid = 0;
776aa1b14e7SSheshadri Vasudevan 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
777aa1b14e7SSheshadri Vasudevan 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
778aa1b14e7SSheshadri Vasudevan 				ip.systid = SUNIXOS2;
779aa1b14e7SSheshadri Vasudevan 				ip.relsect = relsec;
780aa1b14e7SSheshadri Vasudevan 				ip.numsect = numsec;
781aa1b14e7SSheshadri Vasudevan 				bcopy(&ip, ipart, sizeof (struct ipart));
782aa1b14e7SSheshadri Vasudevan 			}
783aa1b14e7SSheshadri Vasudevan 			continue;
784aa1b14e7SSheshadri Vasudevan 		}
785aa1b14e7SSheshadri Vasudevan #endif
786aa1b14e7SSheshadri Vasudevan 
787e998e519SSheshadri Vasudevan 
788e998e519SSheshadri Vasudevan #ifdef i386
789e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
790e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
791e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
792e998e519SSheshadri Vasudevan 		    ip.systid == EFI_PMBR) {
793e998e519SSheshadri Vasudevan #else
7947c478bd9Sstevel@tonic-gate 		if (ip.systid == SUNIXOS ||
7957c478bd9Sstevel@tonic-gate 		    ip.systid == SUNIXOS2 ||
7967c478bd9Sstevel@tonic-gate 		    ip.systid == EFI_PMBR) {
797e998e519SSheshadri Vasudevan #endif
7987c478bd9Sstevel@tonic-gate 			solaris_offset = lel(ip.relsect);
7997c478bd9Sstevel@tonic-gate 			bcopy(&ip, ipart, sizeof (struct ipart));
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 			/*
8027c478bd9Sstevel@tonic-gate 			 * if the disk has an EFI label, we typically won't
8037c478bd9Sstevel@tonic-gate 			 * have values for nhead and nsect.  format seems to
8047c478bd9Sstevel@tonic-gate 			 * work without them, and we need to protect ourselves
8057c478bd9Sstevel@tonic-gate 			 * from FPE's
8067c478bd9Sstevel@tonic-gate 			 */
8077c478bd9Sstevel@tonic-gate 			if (nhead != 0 && nsect != 0) {
8087c478bd9Sstevel@tonic-gate 				pcyl = lel(ip.numsect) / (nhead * nsect);
8097c478bd9Sstevel@tonic-gate 				ncyl = pcyl - acyl;
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate #ifdef DEBUG
8127c478bd9Sstevel@tonic-gate 			else {
8137c478bd9Sstevel@tonic-gate 				err_print("Critical geometry values are zero:\n"
814aa1b14e7SSheshadri Vasudevan 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 	}
821e998e519SSheshadri Vasudevan #ifdef i386
822e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
823e998e519SSheshadri Vasudevan #endif
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	(void) close(fd);
82665908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(mbr);
8277c478bd9Sstevel@tonic-gate 	return (0);
8287c478bd9Sstevel@tonic-gate }
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate #if defined(_FIRMWARE_NEEDS_FDISK)
8317c478bd9Sstevel@tonic-gate int
8327c478bd9Sstevel@tonic-gate auto_solaris_part(struct dk_label *label)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	int		status, i, fd;
8367c478bd9Sstevel@tonic-gate 	struct mboot	mboot;
83765908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
8387c478bd9Sstevel@tonic-gate 	struct ipart	ip;
8397c478bd9Sstevel@tonic-gate 	char		*bootptr;
8403ccda647Slclee 	char		pbuf[MAXPATHLEN];
841aa1b14e7SSheshadri Vasudevan #ifdef i386
842aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
843aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
844aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
845aa1b14e7SSheshadri Vasudevan #endif
846f85c7842SSuhasini Peddada 
8470972747aSyl194034 	(void) get_pname(&pbuf[0]);
8483ccda647Slclee 	if ((fd = open_disk(pbuf, O_RDONLY)) < 0) {
8493ccda647Slclee 		err_print("Error: can't open selected disk '%s'.\n", pbuf);
8507c478bd9Sstevel@tonic-gate 		return (-1);
8517c478bd9Sstevel@tonic-gate 	}
8523ccda647Slclee 
85365908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
85465908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
85565908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
85665908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
85765908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
85865908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
85965908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
8607c478bd9Sstevel@tonic-gate 		return (-1);
8617c478bd9Sstevel@tonic-gate 	}
86265908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
86365908c77Syu, larry liu - Sun Microsystems - Beijing China 
86465908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
86565908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("Bad read of fdisk partition.\n");
86665908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
86765908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
86865908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
86965908c77Syu, larry liu - Sun Microsystems - Beijing China 
87065908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
8717c478bd9Sstevel@tonic-gate 
872e998e519SSheshadri Vasudevan #ifdef i386
873e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
874e998e519SSheshadri Vasudevan #endif
8757c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
8767c478bd9Sstevel@tonic-gate 		int	ipc;
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
8817c478bd9Sstevel@tonic-gate 		bootptr = &mboot.parts[ipc];
8827c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
8837c478bd9Sstevel@tonic-gate 
884aa1b14e7SSheshadri Vasudevan #ifdef i386
885aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
886aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
887aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
888aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
889aa1b14e7SSheshadri Vasudevan 			    &numsec);
890aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
891aa1b14e7SSheshadri Vasudevan 				/*
892aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
893aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
894aa1b14e7SSheshadri Vasudevan 				 */
895aa1b14e7SSheshadri Vasudevan 				if ((label->dkl_nhead != 0) &&
896aa1b14e7SSheshadri Vasudevan 				    (label->dkl_nsect != 0)) {
897aa1b14e7SSheshadri Vasudevan 					label->dkl_pcyl =
898aa1b14e7SSheshadri Vasudevan 					    numsec / (label->dkl_nhead *
899aa1b14e7SSheshadri Vasudevan 					    label->dkl_nsect);
900aa1b14e7SSheshadri Vasudevan 					label->dkl_ncyl = label->dkl_pcyl -
901aa1b14e7SSheshadri Vasudevan 					    label->dkl_acyl;
902aa1b14e7SSheshadri Vasudevan 				}
903aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
904aa1b14e7SSheshadri Vasudevan 			}
905aa1b14e7SSheshadri Vasudevan 			continue;
906aa1b14e7SSheshadri Vasudevan 		}
907aa1b14e7SSheshadri Vasudevan #endif
908aa1b14e7SSheshadri Vasudevan 
9097c478bd9Sstevel@tonic-gate 		/*
9107c478bd9Sstevel@tonic-gate 		 * if the disk has an EFI label, the nhead and nsect fields
9117c478bd9Sstevel@tonic-gate 		 * the label may be zero.  This protects us from FPE's, and
9127c478bd9Sstevel@tonic-gate 		 * format still seems to work happily
9137c478bd9Sstevel@tonic-gate 		 */
914e998e519SSheshadri Vasudevan 
915e998e519SSheshadri Vasudevan 
916e998e519SSheshadri Vasudevan #ifdef i386
917e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
918e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
919e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
920e998e519SSheshadri Vasudevan 		    ip.systid == EFI_PMBR) {
921e998e519SSheshadri Vasudevan #else
9227c478bd9Sstevel@tonic-gate 		if (ip.systid == SUNIXOS ||
9237c478bd9Sstevel@tonic-gate 		    ip.systid == SUNIXOS2 ||
9247c478bd9Sstevel@tonic-gate 		    ip.systid == EFI_PMBR) {
925e998e519SSheshadri Vasudevan #endif
9267c478bd9Sstevel@tonic-gate 			if ((label->dkl_nhead != 0) &&
9277c478bd9Sstevel@tonic-gate 			    (label->dkl_nsect != 0)) {
9287c478bd9Sstevel@tonic-gate 				label->dkl_pcyl = lel(ip.numsect) /
9297c478bd9Sstevel@tonic-gate 				    (label->dkl_nhead * label->dkl_nsect);
9307c478bd9Sstevel@tonic-gate 				label->dkl_ncyl = label->dkl_pcyl -
9317c478bd9Sstevel@tonic-gate 				    label->dkl_acyl;
9327c478bd9Sstevel@tonic-gate 			}
9337c478bd9Sstevel@tonic-gate #ifdef DEBUG
9347c478bd9Sstevel@tonic-gate 			else {
9357c478bd9Sstevel@tonic-gate 				err_print("Critical label fields aren't "
9367c478bd9Sstevel@tonic-gate 				    "non-zero:\n"
9377c478bd9Sstevel@tonic-gate 				    "\tlabel->dkl_nhead = %d; "
9387c478bd9Sstevel@tonic-gate 				    "label->dkl_nsect = "
9397c478bd9Sstevel@tonic-gate 				    "%d\n", label->dkl_nhead,
9407c478bd9Sstevel@tonic-gate 				    label->dkl_nsect);
9417c478bd9Sstevel@tonic-gate 			}
9427c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 		solaris_offset = lel(ip.relsect);
9457c478bd9Sstevel@tonic-gate 		break;
9467c478bd9Sstevel@tonic-gate 		}
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
949e998e519SSheshadri Vasudevan #ifdef i386
950e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
951e998e519SSheshadri Vasudevan #endif
9527c478bd9Sstevel@tonic-gate 	(void) close(fd);
95365908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(mbr);
9547c478bd9Sstevel@tonic-gate 	return (0);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate #endif	/* defined(_FIRMWARE_NEEDS_FDISK) */
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate int
9607c478bd9Sstevel@tonic-gate good_fdisk()
9617c478bd9Sstevel@tonic-gate {
9627c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
9637c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	(void) get_pname(&buf[0]);
9667c478bd9Sstevel@tonic-gate 	if (stat(buf, &statbuf) == -1 ||
9677c478bd9Sstevel@tonic-gate 	    !S_ISCHR(statbuf.st_mode) ||
9687c478bd9Sstevel@tonic-gate 	    cur_label == L_TYPE_EFI) {
9697c478bd9Sstevel@tonic-gate 		/*
9707c478bd9Sstevel@tonic-gate 		 * Return if this disk does not support fdisk table or
9717c478bd9Sstevel@tonic-gate 		 * if the disk is labeled with EFI.
9727c478bd9Sstevel@tonic-gate 		 */
9737c478bd9Sstevel@tonic-gate 		return (1);
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 
976cc0efcfeSqd208687 	if (lel(cur_disk->fdisk_part.numsect) > 0) {
9777c478bd9Sstevel@tonic-gate 		return (1);
978cc0efcfeSqd208687 	} else {
979*a29e56d9SToomas Soome 		/*
980*a29e56d9SToomas Soome 		 * Labeled device support in lofi provides p0 partition on
981*a29e56d9SToomas Soome 		 * both x86 and sparc. However, sparc does not implement fdisk
982*a29e56d9SToomas Soome 		 * partitioning. This workaround will allow format
983*a29e56d9SToomas Soome 		 * to ignore fdisk errors in case of lofi devices.
984*a29e56d9SToomas Soome 		 */
985*a29e56d9SToomas Soome 		if (strcmp(cur_disk->disk_dkinfo.dki_cname, "lofi") == 0) {
986*a29e56d9SToomas Soome 			return (1);
987*a29e56d9SToomas Soome 		}
988cc0efcfeSqd208687 		err_print("WARNING - ");
989cc0efcfeSqd208687 		err_print("This disk may be in use by an application "
990cc0efcfeSqd208687 		    "that has\n\t  modified the fdisk table. Ensure "
991cc0efcfeSqd208687 		    "that this disk is\n\t  not currently in use "
992cc0efcfeSqd208687 		    "before proceeding to use fdisk.\n");
9937c478bd9Sstevel@tonic-gate 		return (0);
9947c478bd9Sstevel@tonic-gate 	}
995cc0efcfeSqd208687 }
996aa1b14e7SSheshadri Vasudevan 
997aa1b14e7SSheshadri Vasudevan #ifdef i386
998aa1b14e7SSheshadri Vasudevan int
999aa1b14e7SSheshadri Vasudevan extpart_init(ext_part_t **epp)
1000aa1b14e7SSheshadri Vasudevan {
1001aa1b14e7SSheshadri Vasudevan 	int		rval, lf_op_flag = 0;
1002aa1b14e7SSheshadri Vasudevan 	char		p0_path[MAXPATHLEN];
1003aa1b14e7SSheshadri Vasudevan 
1004aa1b14e7SSheshadri Vasudevan 	get_pname(&p0_path[0]);
1005aa1b14e7SSheshadri Vasudevan 	lf_op_flag |= FDISK_READ_DISK;
1006aa1b14e7SSheshadri Vasudevan 	if ((rval = libfdisk_init(epp, p0_path, NULL, lf_op_flag)) !=
1007aa1b14e7SSheshadri Vasudevan 	    FDISK_SUCCESS) {
1008aa1b14e7SSheshadri Vasudevan 		switch (rval) {
1009aa1b14e7SSheshadri Vasudevan 			/*
10106cb5747bSSharath M Srinivasan 			 * FDISK_EBADLOGDRIVE, FDISK_ENOLOGDRIVE
10116cb5747bSSharath M Srinivasan 			 * and FDISK_EBADMAGIC can be considered
10126cb5747bSSharath M Srinivasan 			 * as soft errors and hence we do not exit.
1013aa1b14e7SSheshadri Vasudevan 			 */
1014aa1b14e7SSheshadri Vasudevan 			case FDISK_EBADLOGDRIVE:
1015aa1b14e7SSheshadri Vasudevan 				break;
1016aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOLOGDRIVE:
1017aa1b14e7SSheshadri Vasudevan 				break;
10186cb5747bSSharath M Srinivasan 			case FDISK_EBADMAGIC:
10196cb5747bSSharath M Srinivasan 				break;
1020aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOVGEOM:
1021aa1b14e7SSheshadri Vasudevan 				err_print("Could not get virtual geometry for"
1022aa1b14e7SSheshadri Vasudevan 				    " this device\n");
1023aa1b14e7SSheshadri Vasudevan 				fullabort();
1024aa1b14e7SSheshadri Vasudevan 				break;
1025aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOPGEOM:
1026aa1b14e7SSheshadri Vasudevan 				err_print("Could not get physical geometry for"
1027aa1b14e7SSheshadri Vasudevan 				    " this device\n");
1028aa1b14e7SSheshadri Vasudevan 				fullabort();
1029aa1b14e7SSheshadri Vasudevan 				break;
1030aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOLGEOM:
1031aa1b14e7SSheshadri Vasudevan 				err_print("Could not get label geometry for "
1032aa1b14e7SSheshadri Vasudevan 				    " this device\n");
1033aa1b14e7SSheshadri Vasudevan 				fullabort();
1034aa1b14e7SSheshadri Vasudevan 				break;
1035aa1b14e7SSheshadri Vasudevan 			default:
1036aa1b14e7SSheshadri Vasudevan 				err_print("Failed to initialise libfdisk.\n");
1037aa1b14e7SSheshadri Vasudevan 				fullabort();
1038aa1b14e7SSheshadri Vasudevan 				break;
1039aa1b14e7SSheshadri Vasudevan 		}
1040aa1b14e7SSheshadri Vasudevan 	}
1041aa1b14e7SSheshadri Vasudevan 	return (0);
1042aa1b14e7SSheshadri Vasudevan }
1043aa1b14e7SSheshadri Vasudevan #endif
1044