xref: /illumos-gate/usr/src/cmd/format/menu_fdisk.c (revision a11d78d6)
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
50972747aSyl  * Common Development and Distribution License (the "License").
60972747aSyl  * 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.
23406fc510SToomas 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[];
64*a11d78d6SToomas Soome struct mboot boot_sec;
65*a11d78d6SToomas Soome uint_t	xstart;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Byte swapping macros for accessing struct ipart
697c478bd9Sstevel@tonic-gate  *	to resolve little endian on Sparc.
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate #if defined(sparc)
727c478bd9Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
737c478bd9Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
747c478bd9Sstevel@tonic-gate 			(les((unsigned)((val)&0xffff0000)>>16)))
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #elif	defined(i386)
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	les(val)	(val)
797c478bd9Sstevel@tonic-gate #define	lel(val)	(val)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate #else	/* defined(sparc) */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #error	No Platform defined
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate /* Function prototypes */
897c478bd9Sstevel@tonic-gate #ifdef	__STDC__
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #if	defined(sparc)
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static int getbyte(uchar_t **);
947c478bd9Sstevel@tonic-gate static int getlong(uchar_t **);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static int get_solaris_part(int fd, struct ipart *ipart);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #else	/* __STDC__ */
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #if	defined(sparc)
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate static int getbyte();
1057c478bd9Sstevel@tonic-gate static int getlong();
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate #endif	/* defined(sparc) */
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static int get_solaris_part();
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #endif	/* __STDC__ */
1127c478bd9Sstevel@tonic-gate 
113aa1b14e7SSheshadri Vasudevan #ifdef i386
114aa1b14e7SSheshadri Vasudevan int extpart_init(ext_part_t **epp);
115aa1b14e7SSheshadri Vasudevan #endif
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Handling the alignment problem of struct ipart.
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate static void
fill_ipart(char * bootptr,struct ipart * partp)1207c478bd9Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate #if defined(sparc)
1237c478bd9Sstevel@tonic-gate 	/*
1247c478bd9Sstevel@tonic-gate 	 * Sparc platform:
1257c478bd9Sstevel@tonic-gate 	 *
1267c478bd9Sstevel@tonic-gate 	 * Packing short/word for struct ipart to resolve
1277c478bd9Sstevel@tonic-gate 	 *	little endian on Sparc since it is not
1287c478bd9Sstevel@tonic-gate 	 *	properly aligned on Sparc.
1297c478bd9Sstevel@tonic-gate 	 */
1307c478bd9Sstevel@tonic-gate 	partp->bootid = getbyte((uchar_t **)&bootptr);
1317c478bd9Sstevel@tonic-gate 	partp->beghead = getbyte((uchar_t **)&bootptr);
1327c478bd9Sstevel@tonic-gate 	partp->begsect = getbyte((uchar_t **)&bootptr);
1337c478bd9Sstevel@tonic-gate 	partp->begcyl = getbyte((uchar_t **)&bootptr);
1347c478bd9Sstevel@tonic-gate 	partp->systid = getbyte((uchar_t **)&bootptr);
1357c478bd9Sstevel@tonic-gate 	partp->endhead = getbyte((uchar_t **)&bootptr);
1367c478bd9Sstevel@tonic-gate 	partp->endsect = getbyte((uchar_t **)&bootptr);
1377c478bd9Sstevel@tonic-gate 	partp->endcyl = getbyte((uchar_t **)&bootptr);
1387c478bd9Sstevel@tonic-gate 	partp->relsect = getlong((uchar_t **)&bootptr);
1397c478bd9Sstevel@tonic-gate 	partp->numsect = getlong((uchar_t **)&bootptr);
1407c478bd9Sstevel@tonic-gate #elif defined(i386)
1417c478bd9Sstevel@tonic-gate 	/*
1427c478bd9Sstevel@tonic-gate 	 * i386 platform:
1437c478bd9Sstevel@tonic-gate 	 *
1447c478bd9Sstevel@tonic-gate 	 * The fdisk table does not begin on a 4-byte boundary within
1457c478bd9Sstevel@tonic-gate 	 * the master boot record; so, we need to recopy its contents
1467c478bd9Sstevel@tonic-gate 	 * to another data structure to avoid an alignment exception.
1477c478bd9Sstevel@tonic-gate 	 */
1487c478bd9Sstevel@tonic-gate 	(void) bcopy(bootptr, partp, sizeof (struct ipart));
1497c478bd9Sstevel@tonic-gate #else
1507c478bd9Sstevel@tonic-gate #error  No Platform defined
1517c478bd9Sstevel@tonic-gate #endif /* defined(sparc) */
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /*
1557c478bd9Sstevel@tonic-gate  * Get a correct byte/short/word routines for Sparc platform.
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate #if defined(sparc)
1587c478bd9Sstevel@tonic-gate static int
getbyte(uchar_t ** bp)1597c478bd9Sstevel@tonic-gate getbyte(uchar_t **bp)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	int	b;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	b = **bp;
1647c478bd9Sstevel@tonic-gate 	*bp = *bp + 1;
1657c478bd9Sstevel@tonic-gate 	return (b);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #ifdef DEADCODE
1697c478bd9Sstevel@tonic-gate static int
getshort(uchar_t ** bp)1707c478bd9Sstevel@tonic-gate getshort(uchar_t **bp)
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate 	int	b;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	b = ((**bp) << 8) | *(*bp + 1);
1757c478bd9Sstevel@tonic-gate 	*bp += 2;
1767c478bd9Sstevel@tonic-gate 	return (b);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate #endif /* DEADCODE */
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static int
getlong(uchar_t ** bp)1817c478bd9Sstevel@tonic-gate getlong(uchar_t **bp)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int	b, bh, bl;
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
1867c478bd9Sstevel@tonic-gate 	*bp += 2;
1877c478bd9Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
1887c478bd9Sstevel@tonic-gate 	*bp += 2;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	b = (bh << 16) | bl;
1917c478bd9Sstevel@tonic-gate 	return (b);
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate #endif /* defined(sparc) */
1947c478bd9Sstevel@tonic-gate 
195589271a4SSheng-Liang Eric Zhang #ifdef i386
196589271a4SSheng-Liang Eric Zhang /*
197589271a4SSheng-Liang Eric Zhang  * Convert emcpowerN[a-p,p0,p1,p2,p3,p4] to emcpowerNp0 path,
198589271a4SSheng-Liang Eric Zhang  * this is specific for emc powerpath driver.
199589271a4SSheng-Liang Eric Zhang  */
200589271a4SSheng-Liang Eric Zhang static void
get_emcpower_pname(char * name,char * devname)201589271a4SSheng-Liang Eric Zhang get_emcpower_pname(char *name, char *devname)
202589271a4SSheng-Liang Eric Zhang {
203589271a4SSheng-Liang Eric Zhang 	char	*emcp = "emcpower";
204589271a4SSheng-Liang Eric Zhang 	char	*npt = NULL;
205589271a4SSheng-Liang Eric Zhang 	char	np[MAXNAMELEN];
206589271a4SSheng-Liang Eric Zhang 	int	i = strlen(emcp);
207589271a4SSheng-Liang Eric Zhang 
208589271a4SSheng-Liang Eric Zhang 	(void) strcpy(np, devname);
209589271a4SSheng-Liang Eric Zhang 	npt = strstr(np, emcp);
210589271a4SSheng-Liang Eric Zhang 	while ((i < strlen(npt)) && (isdigit(npt[i])))
211589271a4SSheng-Liang Eric Zhang 		i++;
212589271a4SSheng-Liang Eric Zhang 	npt[i] = '\0';
213589271a4SSheng-Liang Eric Zhang 	(void) snprintf(name, MAXNAMELEN, "/dev/rdsk/%sp0", npt);
214589271a4SSheng-Liang Eric Zhang }
215589271a4SSheng-Liang Eric Zhang #endif
216589271a4SSheng-Liang Eric Zhang 
217895142e0Syl /*
218895142e0Syl  * Convert cn[tn]dn to cn[tn]dns2 path
219895142e0Syl  */
220895142e0Syl static void
get_sname(char * name)221895142e0Syl get_sname(char *name)
222895142e0Syl {
223895142e0Syl 	char		buf[MAXPATHLEN];
224895142e0Syl 	char		*devp = "/dev/dsk";
225895142e0Syl 	char		*rdevp = "/dev/rdsk";
226895142e0Syl 	char		np[MAXNAMELEN];
227895142e0Syl 	char		*npt;
228895142e0Syl 
229589271a4SSheng-Liang Eric Zhang #ifdef i386
230589271a4SSheng-Liang Eric Zhang 	if (emcpower_name(cur_disk->disk_name)) {
231589271a4SSheng-Liang Eric Zhang 		get_emcpower_pname(name, cur_disk->disk_name);
232589271a4SSheng-Liang Eric Zhang 		return;
233589271a4SSheng-Liang Eric Zhang 	}
234589271a4SSheng-Liang Eric Zhang #endif
235589271a4SSheng-Liang Eric Zhang 
236895142e0Syl 	/*
237895142e0Syl 	 * If it is a full path /dev/[r]dsk/cn[tn]dn, use this path
238895142e0Syl 	 */
239895142e0Syl 	(void) strcpy(np, cur_disk->disk_name);
240895142e0Syl 	if (strncmp(rdevp, cur_disk->disk_name, strlen(rdevp)) == 0 ||
241895142e0Syl 	    strncmp(devp, cur_disk->disk_name, strlen(devp)) == 0) {
242895142e0Syl 		/*
243895142e0Syl 		 * Skip if the path is already included with sN
244895142e0Syl 		 */
245895142e0Syl 		if (strchr(np, 's') == strrchr(np, 's')) {
246895142e0Syl 			npt = strrchr(np, 'p');
247895142e0Syl 			/* If pN is found, do not include it */
24876871191Syl 			if (npt != NULL) {
24976871191Syl 				*npt = '\0';
250895142e0Syl 			}
251895142e0Syl 			(void) snprintf(buf, sizeof (buf), "%ss2", np);
252895142e0Syl 		} else {
253895142e0Syl 			(void) snprintf(buf, sizeof (buf), "%s", np);
254895142e0Syl 		}
255895142e0Syl 	} else {
256895142e0Syl 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%ss2", np);
257895142e0Syl 	}
258895142e0Syl 	(void) strcpy(name, buf);
259895142e0Syl }
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate  * Convert cn[tn]dnsn to cn[tn]dnp0 path
2637c478bd9Sstevel@tonic-gate  */
2647c478bd9Sstevel@tonic-gate static void
get_pname(char * name)2657c478bd9Sstevel@tonic-gate get_pname(char *name)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
2687c478bd9Sstevel@tonic-gate 	char		*devp = "/dev/dsk";
2697c478bd9Sstevel@tonic-gate 	char		*rdevp = "/dev/rdsk";
2707c478bd9Sstevel@tonic-gate 	char		np[MAXNAMELEN];
2717c478bd9Sstevel@tonic-gate 	char		*npt;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * If it is a full path /dev/[r]dsk/cn[tn]dnsn, use this path
2757c478bd9Sstevel@tonic-gate 	 */
2760972747aSyl 	if (cur_disk == NULL) {
2770972747aSyl 		(void) strcpy(np, x86_devname);
2780972747aSyl 	} else {
2790972747aSyl 		(void) strcpy(np, cur_disk->disk_name);
2800972747aSyl 	}
2810972747aSyl 
282589271a4SSheng-Liang Eric Zhang #ifdef i386
283589271a4SSheng-Liang Eric Zhang 	if (emcpower_name(np)) {
284589271a4SSheng-Liang Eric Zhang 		get_emcpower_pname(name, np);
285589271a4SSheng-Liang Eric Zhang 		return;
286589271a4SSheng-Liang Eric Zhang 	}
287589271a4SSheng-Liang Eric Zhang #endif
288589271a4SSheng-Liang Eric Zhang 
2890972747aSyl 	if (strncmp(rdevp, np, strlen(rdevp)) == 0 ||
2900972747aSyl 	    strncmp(devp, np, strlen(devp)) == 0) {
2917c478bd9Sstevel@tonic-gate 		/*
2927c478bd9Sstevel@tonic-gate 		 * Skip if the path is already included with pN
2937c478bd9Sstevel@tonic-gate 		 */
2947c478bd9Sstevel@tonic-gate 		if (strchr(np, 'p') == NULL) {
2957c478bd9Sstevel@tonic-gate 			npt = strrchr(np, 's');
2967c478bd9Sstevel@tonic-gate 			/* If sN is found, do not include it */
2977c478bd9Sstevel@tonic-gate 			if (isdigit(*++npt)) {
2987c478bd9Sstevel@tonic-gate 				*--npt = '\0';
2997c478bd9Sstevel@tonic-gate 			}
3007c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%sp0", np);
3017c478bd9Sstevel@tonic-gate 		} else {
3027c478bd9Sstevel@tonic-gate 			(void) snprintf(buf, sizeof (buf), "%s", np);
3037c478bd9Sstevel@tonic-gate 		}
3047c478bd9Sstevel@tonic-gate 	} else {
3057c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "/dev/rdsk/%sp0", np);
3067c478bd9Sstevel@tonic-gate 	}
3077c478bd9Sstevel@tonic-gate 	(void) strcpy(name, buf);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * Open file descriptor for current disk (cur_file)
3127c478bd9Sstevel@tonic-gate  *	with "p0" path or cur_disk->disk_path
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate void
open_cur_file(int mode)3157c478bd9Sstevel@tonic-gate open_cur_file(int mode)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	char	*dkpath;
3187c478bd9Sstevel@tonic-gate 	char	pbuf[MAXPATHLEN];
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	switch (mode) {
321aa1b14e7SSheshadri Vasudevan 		case FD_USE_P0_PATH:
322aa1b14e7SSheshadri Vasudevan 			(void) get_pname(&pbuf[0]);
323895142e0Syl 			dkpath = pbuf;
324aa1b14e7SSheshadri Vasudevan 			break;
325aa1b14e7SSheshadri Vasudevan 		case FD_USE_CUR_DISK_PATH:
326aa1b14e7SSheshadri Vasudevan 			if (cur_disk->fdisk_part.systid == SUNIXOS ||
327aa1b14e7SSheshadri Vasudevan 			    cur_disk->fdisk_part.systid == SUNIXOS2) {
328aa1b14e7SSheshadri Vasudevan 				(void) get_sname(&pbuf[0]);
329aa1b14e7SSheshadri Vasudevan 				dkpath = pbuf;
330aa1b14e7SSheshadri Vasudevan 			} else {
331aa1b14e7SSheshadri Vasudevan 				dkpath = cur_disk->disk_path;
332aa1b14e7SSheshadri Vasudevan 			}
333aa1b14e7SSheshadri Vasudevan 			break;
334aa1b14e7SSheshadri Vasudevan 		default:
335aa1b14e7SSheshadri Vasudevan 			err_print("Error: Invalid mode option for opening "
336aa1b14e7SSheshadri Vasudevan 			    "cur_file\n");
337aa1b14e7SSheshadri Vasudevan 			fullabort();
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 	/* Close previous cur_file */
3417c478bd9Sstevel@tonic-gate 	(void) close(cur_file);
3427c478bd9Sstevel@tonic-gate 	/* Open cur_file with the required path dkpath */
3437c478bd9Sstevel@tonic-gate 	if ((cur_file = open_disk(dkpath, O_RDWR | O_NDELAY)) < 0) {
3447c478bd9Sstevel@tonic-gate 		err_print(
3457c478bd9Sstevel@tonic-gate 		    "Error: can't open selected disk '%s'.\n", dkpath);
3467c478bd9Sstevel@tonic-gate 		fullabort();
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate  * This routine implements the 'fdisk' command.  It simply runs
3537c478bd9Sstevel@tonic-gate  * the fdisk command on the current disk.
3547c478bd9Sstevel@tonic-gate  * Use of this is restricted to interactive mode only.
3557c478bd9Sstevel@tonic-gate  */
3567c478bd9Sstevel@tonic-gate int
c_fdisk()3577c478bd9Sstevel@tonic-gate c_fdisk()
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
3617c478bd9Sstevel@tonic-gate 	char		pbuf[MAXPATHLEN];
3627c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * We must be in interactive mode to use the fdisk command
3667c478bd9Sstevel@tonic-gate 	 */
3677c478bd9Sstevel@tonic-gate 	if (option_f != (char *)NULL || isatty(0) != 1 || isatty(1) != 1) {
3687c478bd9Sstevel@tonic-gate 		err_print("Fdisk command is for interactive use only!\n");
3697c478bd9Sstevel@tonic-gate 		return (-1);
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/*
3737c478bd9Sstevel@tonic-gate 	 * There must be a current disk type and a current disk
3747c478bd9Sstevel@tonic-gate 	 */
3757c478bd9Sstevel@tonic-gate 	if (cur_dtype == NULL) {
3767c478bd9Sstevel@tonic-gate 		err_print("Current Disk Type is not set.\n");
3777c478bd9Sstevel@tonic-gate 		return (-1);
3787c478bd9Sstevel@tonic-gate 	}
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	/*
3817c478bd9Sstevel@tonic-gate 	 * Before running the fdisk command, get file status of
3827c478bd9Sstevel@tonic-gate 	 *	/dev/rdsk/cn[tn]dnp0 path to see if this disk
3837c478bd9Sstevel@tonic-gate 	 *	supports fixed disk partition table.
3847c478bd9Sstevel@tonic-gate 	 */
3857c478bd9Sstevel@tonic-gate 	(void) get_pname(&pbuf[0]);
3867c478bd9Sstevel@tonic-gate 	if (stat(pbuf, (struct stat *)&statbuf) == -1 ||
3877c478bd9Sstevel@tonic-gate 	    !S_ISCHR(statbuf.st_mode)) {
3887c478bd9Sstevel@tonic-gate 		err_print(
3897c478bd9Sstevel@tonic-gate 		"Disk does not support fixed disk partition table\n");
3907c478bd9Sstevel@tonic-gate 		return (0);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 	/*
3947c478bd9Sstevel@tonic-gate 	 * Run the fdisk program.
3957c478bd9Sstevel@tonic-gate 	 */
3967c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "fdisk %s\n", pbuf);
3977c478bd9Sstevel@tonic-gate 	(void) system(buf);
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	/*
4007c478bd9Sstevel@tonic-gate 	 * Open cur_file with "p0" path for accessing the fdisk table
4017c478bd9Sstevel@tonic-gate 	 */
4027c478bd9Sstevel@tonic-gate 	(void) open_cur_file(FD_USE_P0_PATH);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	/*
4057c478bd9Sstevel@tonic-gate 	 * Get solaris partition information in the fdisk partition table
4067c478bd9Sstevel@tonic-gate 	 */
4077c478bd9Sstevel@tonic-gate 	if (get_solaris_part(cur_file, &cur_disk->fdisk_part) == -1) {
4087c478bd9Sstevel@tonic-gate 		err_print("No fdisk solaris partition found\n");
4097c478bd9Sstevel@tonic-gate 		cur_disk->fdisk_part.numsect = 0;  /* No Solaris */
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	/*
4137c478bd9Sstevel@tonic-gate 	 * Restore cur_file with cur_disk->disk_path
4147c478bd9Sstevel@tonic-gate 	 */
4157c478bd9Sstevel@tonic-gate 	(void) open_cur_file(FD_USE_CUR_DISK_PATH);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	return (0);
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate /*
4217c478bd9Sstevel@tonic-gate  * Read MBR on the disk
4227c478bd9Sstevel@tonic-gate  * if the Solaris partition has changed,
4237c478bd9Sstevel@tonic-gate  *	reread the vtoc
4247c478bd9Sstevel@tonic-gate  */
4257c478bd9Sstevel@tonic-gate #ifdef DEADCODE
4267c478bd9Sstevel@tonic-gate static void
update_cur_parts()4277c478bd9Sstevel@tonic-gate update_cur_parts()
4287c478bd9Sstevel@tonic-gate {
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	int i;
4317c478bd9Sstevel@tonic-gate 	register struct partition_info *parts;
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate 	for (i = 0; i < NDKMAP; i++) {
4347c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4357c478bd9Sstevel@tonic-gate 		if (cur_parts->vtoc.v_part[i].p_tag &&
43665908c77Syu, larry liu - Sun Microsystems - Beijing China 		    cur_parts->vtoc.v_part[i].p_tag != V_ALTSCTR) {
4377c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_start = 0;
4387c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_size = 0;
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate #endif
4417c478bd9Sstevel@tonic-gate 			cur_parts->pinfo_map[i].dkl_nblk = 0;
4427c478bd9Sstevel@tonic-gate 			cur_parts->pinfo_map[i].dkl_cylno = 0;
4437c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_tag =
44465908c77Syu, larry liu - Sun Microsystems - Beijing China 			    default_vtoc_map[i].p_tag;
4457c478bd9Sstevel@tonic-gate 			cur_parts->vtoc.v_part[i].p_flag =
44665908c77Syu, larry liu - Sun Microsystems - Beijing China 			    default_vtoc_map[i].p_flag;
4477c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[C_PARTITION].dkl_nblk = ncyl * spc();
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
4547c478bd9Sstevel@tonic-gate 	/*
4557c478bd9Sstevel@tonic-gate 	 * Adjust for the boot partitions
4567c478bd9Sstevel@tonic-gate 	 */
4577c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[I_PARTITION].dkl_nblk = spc();
4587c478bd9Sstevel@tonic-gate 	cur_parts->pinfo_map[I_PARTITION].dkl_cylno = 0;
4597c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[C_PARTITION].p_start =
46065908c77Syu, larry liu - Sun Microsystems - Beijing China 	    cur_parts->pinfo_map[C_PARTITION].dkl_cylno * nhead * nsect;
4617c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[C_PARTITION].p_size =
46265908c77Syu, larry liu - Sun Microsystems - Beijing China 	    cur_parts->pinfo_map[C_PARTITION].dkl_nblk;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[I_PARTITION].p_start =
46565908c77Syu, larry liu - Sun Microsystems - Beijing China 	    cur_parts->pinfo_map[I_PARTITION].dkl_cylno;
4667c478bd9Sstevel@tonic-gate 	cur_parts->vtoc.v_part[I_PARTITION].p_size =
46765908c77Syu, larry liu - Sun Microsystems - Beijing China 	    cur_parts->pinfo_map[I_PARTITION].dkl_nblk;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate #endif	/* defined(_SUNOS_VTOC_16) */
4707c478bd9Sstevel@tonic-gate 	parts = cur_dtype->dtype_plist;
4717c478bd9Sstevel@tonic-gate 	cur_dtype->dtype_ncyl = ncyl;
4727c478bd9Sstevel@tonic-gate 	cur_dtype->dtype_plist = cur_parts;
4737c478bd9Sstevel@tonic-gate 	parts->pinfo_name = cur_parts->pinfo_name;
4747c478bd9Sstevel@tonic-gate 	cur_disk->disk_parts = cur_parts;
4757c478bd9Sstevel@tonic-gate 	cur_ctype->ctype_dlist = cur_dtype;
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate }
4787c478bd9Sstevel@tonic-gate #endif /* DEADCODE */
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate static int
get_solaris_part(int fd,struct ipart * ipart)4817c478bd9Sstevel@tonic-gate get_solaris_part(int fd, struct ipart *ipart)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	int		i;
4847c478bd9Sstevel@tonic-gate 	struct ipart	ip;
4857c478bd9Sstevel@tonic-gate 	int		status;
48665908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
4877c478bd9Sstevel@tonic-gate 	char		*bootptr;
48841ebd847Syl 	struct dk_label	update_label;
489aa1b14e7SSheshadri Vasudevan 	ushort_t	found = 0;
490aa1b14e7SSheshadri Vasudevan #ifdef i386
491aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
492aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
493aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
494aa1b14e7SSheshadri Vasudevan #endif
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	(void) lseek(fd, 0, 0);
4977c478bd9Sstevel@tonic-gate 
49865908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
49965908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
50065908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
50165908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
50265908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
50365908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
50465908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
50565908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
50665908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
50765908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
50865908c77Syu, larry liu - Sun Microsystems - Beijing China 
50965908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
5107c478bd9Sstevel@tonic-gate 		err_print("Bad read of fdisk partition. Status = %x\n", status);
5117c478bd9Sstevel@tonic-gate 		err_print("Cannot read fdisk partition information.\n");
51265908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
5137c478bd9Sstevel@tonic-gate 		return (-1);
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
51665908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&boot_sec, mbr, sizeof (struct mboot));
51765908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(mbr);
51865908c77Syu, larry liu - Sun Microsystems - Beijing China 
519e998e519SSheshadri Vasudevan #ifdef i386
520e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
521e998e519SSheshadri Vasudevan #endif
5227c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
5237c478bd9Sstevel@tonic-gate 		int	ipc;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
5287c478bd9Sstevel@tonic-gate 		bootptr = &boot_sec.parts[ipc];
5297c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
5307c478bd9Sstevel@tonic-gate 
531aa1b14e7SSheshadri Vasudevan #ifdef i386
532aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
533aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
534aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
535aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
536aa1b14e7SSheshadri Vasudevan 			    &numsec);
537aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
538aa1b14e7SSheshadri Vasudevan 				/*
539aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
540aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
541aa1b14e7SSheshadri Vasudevan 				 */
542aa1b14e7SSheshadri Vasudevan 				if (nhead != 0 && nsect != 0) {
543aa1b14e7SSheshadri Vasudevan 					pcyl = numsec / (nhead * nsect);
544aa1b14e7SSheshadri Vasudevan 					xstart = relsec / (nhead * nsect);
545aa1b14e7SSheshadri Vasudevan 					ncyl = pcyl - acyl;
546aa1b14e7SSheshadri Vasudevan 				}
547aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
548aa1b14e7SSheshadri Vasudevan 				found = 2;
549aa1b14e7SSheshadri Vasudevan 				ip.bootid = 0;
550aa1b14e7SSheshadri Vasudevan 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
551aa1b14e7SSheshadri Vasudevan 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
552aa1b14e7SSheshadri Vasudevan 				ip.systid = SUNIXOS2;
553aa1b14e7SSheshadri Vasudevan 				ip.relsect = relsec;
554aa1b14e7SSheshadri Vasudevan 				ip.numsect = numsec;
555aa1b14e7SSheshadri Vasudevan 				ipart->bootid = ip.bootid;
556aa1b14e7SSheshadri Vasudevan 				status = bcmp(&ip, ipart,
557aa1b14e7SSheshadri Vasudevan 				    sizeof (struct ipart));
558aa1b14e7SSheshadri Vasudevan 				bcopy(&ip, ipart, sizeof (struct ipart));
559aa1b14e7SSheshadri Vasudevan 			}
560aa1b14e7SSheshadri Vasudevan 			continue;
561aa1b14e7SSheshadri Vasudevan 		}
562aa1b14e7SSheshadri Vasudevan #endif
563aa1b14e7SSheshadri Vasudevan 
5647c478bd9Sstevel@tonic-gate 		/*
5657c478bd9Sstevel@tonic-gate 		 * we are interested in Solaris and EFI partition types
5667c478bd9Sstevel@tonic-gate 		 */
567e998e519SSheshadri Vasudevan #ifdef i386
568e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
569e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
570e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
571e998e519SSheshadri Vasudevan 		    ip.systid == EFI_PMBR) {
572e998e519SSheshadri Vasudevan #else
5737c478bd9Sstevel@tonic-gate 		if (ip.systid == SUNIXOS ||
5747c478bd9Sstevel@tonic-gate 		    ip.systid == SUNIXOS2 ||
5757c478bd9Sstevel@tonic-gate 		    ip.systid == EFI_PMBR) {
576e998e519SSheshadri Vasudevan #endif
5777c478bd9Sstevel@tonic-gate 			/*
5787c478bd9Sstevel@tonic-gate 			 * if the disk has an EFI label, nhead and nsect may
5797c478bd9Sstevel@tonic-gate 			 * be zero.  This test protects us from FPE's, and
5807c478bd9Sstevel@tonic-gate 			 * format still seems to work fine
5817c478bd9Sstevel@tonic-gate 			 */
5827c478bd9Sstevel@tonic-gate 			if (nhead != 0 && nsect != 0) {
5837c478bd9Sstevel@tonic-gate 				pcyl = lel(ip.numsect) / (nhead * nsect);
5847c478bd9Sstevel@tonic-gate 				xstart = lel(ip.relsect) / (nhead * nsect);
5857c478bd9Sstevel@tonic-gate 				ncyl = pcyl - acyl;
5867c478bd9Sstevel@tonic-gate 			}
5877c478bd9Sstevel@tonic-gate #ifdef DEBUG
5887c478bd9Sstevel@tonic-gate 			else {
5897c478bd9Sstevel@tonic-gate 				err_print("Critical geometry values are zero:\n"
590aa1b14e7SSheshadri Vasudevan 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
5917c478bd9Sstevel@tonic-gate 			}
5927c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5937c478bd9Sstevel@tonic-gate 
594342440ecSPrasad Singamsetty 			solaris_offset = (uint_t)lel(ip.relsect);
595aa1b14e7SSheshadri Vasudevan 			found = 1;
5967c478bd9Sstevel@tonic-gate 			break;
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
600e998e519SSheshadri Vasudevan #ifdef i386
601e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
602e998e519SSheshadri Vasudevan #endif
603e998e519SSheshadri Vasudevan 
604aa1b14e7SSheshadri Vasudevan 	if (!found) {
6057c478bd9Sstevel@tonic-gate 		err_print("Solaris fdisk partition not found\n");
6067c478bd9Sstevel@tonic-gate 		return (-1);
607aa1b14e7SSheshadri Vasudevan 	} else if (found == 1) {
608aa1b14e7SSheshadri Vasudevan 		/*
609aa1b14e7SSheshadri Vasudevan 		 * Found a primary solaris partition.
610aa1b14e7SSheshadri Vasudevan 		 * compare the previous and current Solaris partition
611aa1b14e7SSheshadri Vasudevan 		 * but don't use bootid in determination of Solaris partition
612aa1b14e7SSheshadri Vasudevan 		 * changes
613aa1b14e7SSheshadri Vasudevan 		 */
614aa1b14e7SSheshadri Vasudevan 		ipart->bootid = ip.bootid;
615aa1b14e7SSheshadri Vasudevan 		status = bcmp(&ip, ipart, sizeof (struct ipart));
616f85c7842SSuhasini Peddada 
617aa1b14e7SSheshadri Vasudevan 		bcopy(&ip, ipart, sizeof (struct ipart));
618aa1b14e7SSheshadri Vasudevan 	}
619f85c7842SSuhasini Peddada 
6209773101cSdduvall 	/* if the disk partitioning has changed - get the VTOC */
6217c478bd9Sstevel@tonic-gate 	if (status) {
622342440ecSPrasad Singamsetty 		struct extvtoc exvtoc;
623342440ecSPrasad Singamsetty 		struct vtoc vtoc;
624342440ecSPrasad Singamsetty 
625342440ecSPrasad Singamsetty 		status = ioctl(fd, DKIOCGEXTVTOC, &exvtoc);
6267c478bd9Sstevel@tonic-gate 		if (status == -1) {
6277c478bd9Sstevel@tonic-gate 			i = errno;
628342440ecSPrasad Singamsetty 			/* Try the old ioctl DKIOCGVTOC */
629342440ecSPrasad Singamsetty 			status = ioctl(fd, DKIOCGVTOC, &vtoc);
630342440ecSPrasad Singamsetty 			if (status == -1) {
631342440ecSPrasad Singamsetty 				err_print("Bad ioctl DKIOCGEXTVTOC.\n");
632342440ecSPrasad Singamsetty 				err_print("errno=%d %s\n", i, strerror(i));
633342440ecSPrasad Singamsetty 				err_print("Cannot read vtoc information.\n");
634342440ecSPrasad Singamsetty 				return (-1);
635342440ecSPrasad Singamsetty 			}
6367c478bd9Sstevel@tonic-gate 		}
63741ebd847Syl 
63841ebd847Syl 		status = read_label(fd, &update_label);
63941ebd847Syl 		if (status == -1) {
64041ebd847Syl 			err_print("Cannot read label information.\n");
64141ebd847Syl 			return (-1);
64241ebd847Syl 		}
64341ebd847Syl 
644342440ecSPrasad Singamsetty 		/* copy vtoc information */
645342440ecSPrasad Singamsetty 		cur_parts->vtoc = update_label.dkl_vtoc;
646342440ecSPrasad Singamsetty 
64741ebd847Syl #if defined(_SUNOS_VTOC_16)
64841ebd847Syl 		/*
64941ebd847Syl 		 * this is to update the slice table on x86
65041ebd847Syl 		 * we don't care about VTOC8 here
65141ebd847Syl 		 */
65241ebd847Syl 		for (i = 0; i < NDKMAP; i ++) {
65341ebd847Syl 			cur_parts->pinfo_map[i].dkl_cylno =
65441ebd847Syl 			    update_label.dkl_vtoc.v_part[i].p_start /
65541ebd847Syl 			    ((int)(update_label.dkl_nhead *
65641ebd847Syl 			    update_label.dkl_nsect));
65741ebd847Syl 			cur_parts->pinfo_map[i].dkl_nblk =
65841ebd847Syl 			    update_label.dkl_vtoc.v_part[i].p_size;
65941ebd847Syl 		}
66041ebd847Syl #endif /* defined(_SUNOS_VTOC_16) */
66141ebd847Syl 
66241ebd847Syl 		cur_dtype->dtype_ncyl = update_label.dkl_ncyl;
66341ebd847Syl 		cur_dtype->dtype_pcyl = update_label.dkl_pcyl;
66441ebd847Syl 		cur_dtype->dtype_acyl = update_label.dkl_acyl;
66541ebd847Syl 		cur_dtype->dtype_nhead = update_label.dkl_nhead;
66641ebd847Syl 		cur_dtype->dtype_nsect = update_label.dkl_nsect;
66741ebd847Syl 		ncyl = cur_dtype->dtype_ncyl;
66841ebd847Syl 		acyl = cur_dtype->dtype_acyl;
66941ebd847Syl 		pcyl = cur_dtype->dtype_pcyl;
67041ebd847Syl 		nsect = cur_dtype->dtype_nsect;
67141ebd847Syl 		nhead = cur_dtype->dtype_nhead;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 	return (0);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate int
6787c478bd9Sstevel@tonic-gate copy_solaris_part(struct ipart *ipart)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	int		status, i, fd;
6827c478bd9Sstevel@tonic-gate 	struct mboot	mboot;
68365908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
6847c478bd9Sstevel@tonic-gate 	struct ipart	ip;
6857c478bd9Sstevel@tonic-gate 	char		buf[MAXPATHLEN];
6867c478bd9Sstevel@tonic-gate 	char		*bootptr;
6877c478bd9Sstevel@tonic-gate 	struct stat	statbuf;
688aa1b14e7SSheshadri Vasudevan #ifdef i386
689aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
690aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
691aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
692aa1b14e7SSheshadri Vasudevan #endif
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	(void) get_pname(&buf[0]);
6957c478bd9Sstevel@tonic-gate 	if (stat(buf, &statbuf) == -1 ||
6967c478bd9Sstevel@tonic-gate 	    !S_ISCHR(statbuf.st_mode) ||
6977c478bd9Sstevel@tonic-gate 	    ((cur_label == L_TYPE_EFI) &&
69865908c77Syu, larry liu - Sun Microsystems - Beijing China 	    (cur_disk->disk_flags & DSK_LABEL_DIRTY))) {
6997c478bd9Sstevel@tonic-gate 		/*
7007c478bd9Sstevel@tonic-gate 		 * Make sure to reset solaris_offset to zero if it is
7017c478bd9Sstevel@tonic-gate 		 *	previously set by a selected disk that
7027c478bd9Sstevel@tonic-gate 		 *	supports the fdisk table.
7037c478bd9Sstevel@tonic-gate 		 */
7047c478bd9Sstevel@tonic-gate 		solaris_offset = 0;
7057c478bd9Sstevel@tonic-gate 		/*
7067c478bd9Sstevel@tonic-gate 		 * Return if this disk does not support fdisk table or
7077c478bd9Sstevel@tonic-gate 		 * if it uses an EFI label but has not yet been labelled.
7087c478bd9Sstevel@tonic-gate 		 * If the EFI label has not been written then the open
7097c478bd9Sstevel@tonic-gate 		 * on the partition will fail.
7107c478bd9Sstevel@tonic-gate 		 */
7117c478bd9Sstevel@tonic-gate 		return (0);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if ((fd = open(buf, O_RDONLY)) < 0) {
715406fc510SToomas Soome 		/*
716406fc510SToomas Soome 		 * Labeled device support in lofi provides p0 partition on
717406fc510SToomas Soome 		 * both x86 and sparc. However, sparc does not implement fdisk
718406fc510SToomas Soome 		 * partitioning. This workaround will allow format
719406fc510SToomas Soome 		 * to ignore fdisk errors in case of lofi devices.
720406fc510SToomas Soome 		 */
721406fc510SToomas Soome 		if (strcmp(cur_disk->disk_dkinfo.dki_cname, "lofi") == 0) {
722406fc510SToomas Soome 			solaris_offset = 0;
723406fc510SToomas Soome 			return (0);
724406fc510SToomas Soome 		}
7257c478bd9Sstevel@tonic-gate 		err_print("Error: can't open disk '%s'.\n", buf);
7267c478bd9Sstevel@tonic-gate 		return (-1);
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
72965908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
73065908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
73165908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
73265908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
73365908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
73465908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
73565908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
73665908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
73765908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
73865908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
7397c478bd9Sstevel@tonic-gate 
74065908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
7417c478bd9Sstevel@tonic-gate 		err_print("Bad read of fdisk partition.\n");
7427c478bd9Sstevel@tonic-gate 		(void) close(fd);
74365908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
7447c478bd9Sstevel@tonic-gate 		return (-1);
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
74765908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
74865908c77Syu, larry liu - Sun Microsystems - Beijing China 
749e998e519SSheshadri Vasudevan #ifdef i386
750e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
751e998e519SSheshadri Vasudevan #endif
7527c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
7537c478bd9Sstevel@tonic-gate 		int	ipc;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
7587c478bd9Sstevel@tonic-gate 		bootptr = &mboot.parts[ipc];
7597c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
7607c478bd9Sstevel@tonic-gate 
761aa1b14e7SSheshadri Vasudevan #ifdef i386
762aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
763aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
764aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
765aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
766aa1b14e7SSheshadri Vasudevan 			    &numsec);
767aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
768aa1b14e7SSheshadri Vasudevan 				/*
769aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
770aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
771aa1b14e7SSheshadri Vasudevan 				 */
772aa1b14e7SSheshadri Vasudevan 				if (nhead != 0 && nsect != 0) {
773aa1b14e7SSheshadri Vasudevan 					pcyl = numsec / (nhead * nsect);
774aa1b14e7SSheshadri Vasudevan 					ncyl = pcyl - acyl;
775aa1b14e7SSheshadri Vasudevan 				}
776aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
777aa1b14e7SSheshadri Vasudevan 				ip.bootid = 0;
778aa1b14e7SSheshadri Vasudevan 				ip.beghead = ip.begsect = ip.begcyl = 0xff;
779aa1b14e7SSheshadri Vasudevan 				ip.endhead = ip.endsect = ip.endcyl = 0xff;
780aa1b14e7SSheshadri Vasudevan 				ip.systid = SUNIXOS2;
781aa1b14e7SSheshadri Vasudevan 				ip.relsect = relsec;
782aa1b14e7SSheshadri Vasudevan 				ip.numsect = numsec;
783aa1b14e7SSheshadri Vasudevan 				bcopy(&ip, ipart, sizeof (struct ipart));
784aa1b14e7SSheshadri Vasudevan 			}
785aa1b14e7SSheshadri Vasudevan 			continue;
786aa1b14e7SSheshadri Vasudevan 		}
787aa1b14e7SSheshadri Vasudevan #endif
788aa1b14e7SSheshadri Vasudevan 
789e998e519SSheshadri Vasudevan 
790e998e519SSheshadri Vasudevan #ifdef i386
791e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
792e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
793e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
794e998e519SSheshadri Vasudevan 		    ip.systid == EFI_PMBR) {
795e998e519SSheshadri Vasudevan #else
7967c478bd9Sstevel@tonic-gate 		if (ip.systid == SUNIXOS ||
7977c478bd9Sstevel@tonic-gate 		    ip.systid == SUNIXOS2 ||
7987c478bd9Sstevel@tonic-gate 		    ip.systid == EFI_PMBR) {
799e998e519SSheshadri Vasudevan #endif
8007c478bd9Sstevel@tonic-gate 			solaris_offset = lel(ip.relsect);
8017c478bd9Sstevel@tonic-gate 			bcopy(&ip, ipart, sizeof (struct ipart));
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 			/*
8047c478bd9Sstevel@tonic-gate 			 * if the disk has an EFI label, we typically won't
8057c478bd9Sstevel@tonic-gate 			 * have values for nhead and nsect.  format seems to
8067c478bd9Sstevel@tonic-gate 			 * work without them, and we need to protect ourselves
8077c478bd9Sstevel@tonic-gate 			 * from FPE's
8087c478bd9Sstevel@tonic-gate 			 */
8097c478bd9Sstevel@tonic-gate 			if (nhead != 0 && nsect != 0) {
8107c478bd9Sstevel@tonic-gate 				pcyl = lel(ip.numsect) / (nhead * nsect);
8117c478bd9Sstevel@tonic-gate 				ncyl = pcyl - acyl;
8127c478bd9Sstevel@tonic-gate 			}
8137c478bd9Sstevel@tonic-gate #ifdef DEBUG
8147c478bd9Sstevel@tonic-gate 			else {
8157c478bd9Sstevel@tonic-gate 				err_print("Critical geometry values are zero:\n"
816aa1b14e7SSheshadri Vasudevan 				    "\tnhead = %d; nsect = %d\n", nhead, nsect);
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 			break;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 	}
823e998e519SSheshadri Vasudevan #ifdef i386
824e998e519SSheshadri Vasudevan 	libfdisk_fini(&epp);
825e998e519SSheshadri Vasudevan #endif
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	(void) close(fd);
82865908c77Syu, larry liu - Sun Microsystems - Beijing China 	free(mbr);
8297c478bd9Sstevel@tonic-gate 	return (0);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate #if defined(_FIRMWARE_NEEDS_FDISK)
8337c478bd9Sstevel@tonic-gate int
8347c478bd9Sstevel@tonic-gate auto_solaris_part(struct dk_label *label)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 	int		status, i, fd;
8387c478bd9Sstevel@tonic-gate 	struct mboot	mboot;
83965908c77Syu, larry liu - Sun Microsystems - Beijing China 	char		*mbr;
8407c478bd9Sstevel@tonic-gate 	struct ipart	ip;
8417c478bd9Sstevel@tonic-gate 	char		*bootptr;
8423ccda647Slclee 	char		pbuf[MAXPATHLEN];
843aa1b14e7SSheshadri Vasudevan #ifdef i386
844aa1b14e7SSheshadri Vasudevan 	uint32_t	relsec, numsec;
845aa1b14e7SSheshadri Vasudevan 	int		pno, rval, ext_part_found = 0;
846aa1b14e7SSheshadri Vasudevan 	ext_part_t	*epp;
847aa1b14e7SSheshadri Vasudevan #endif
848f85c7842SSuhasini Peddada 
8490972747aSyl 	(void) get_pname(&pbuf[0]);
8503ccda647Slclee 	if ((fd = open_disk(pbuf, O_RDONLY)) < 0) {
8513ccda647Slclee 		err_print("Error: can't open selected disk '%s'.\n", pbuf);
8527c478bd9Sstevel@tonic-gate 		return (-1);
8537c478bd9Sstevel@tonic-gate 	}
8543ccda647Slclee 
85565908c77Syu, larry liu - Sun Microsystems - Beijing China 	/*
85665908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * We may get mbr of different size, but the first 512 bytes
85765908c77Syu, larry liu - Sun Microsystems - Beijing China 	 * are valid information.
85865908c77Syu, larry liu - Sun Microsystems - Beijing China 	 */
85965908c77Syu, larry liu - Sun Microsystems - Beijing China 	mbr = malloc(cur_blksz);
86065908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (mbr == NULL) {
86165908c77Syu, larry liu - Sun Microsystems - Beijing China 		err_print("No memory available.\n");
86265908c77Syu, larry liu - Sun Microsystems - Beijing China 		return (-1);
86365908c77Syu, larry liu - Sun Microsystems - Beijing China 	}
86465908c77Syu, larry liu - Sun Microsystems - Beijing China 	status = read(fd, mbr, cur_blksz);
8657c478bd9Sstevel@tonic-gate 
86665908c77Syu, larry liu - Sun Microsystems - Beijing China 	if (status != cur_blksz) {
8677c478bd9Sstevel@tonic-gate 		err_print("Bad read of fdisk partition.\n");
86865908c77Syu, larry liu - Sun Microsystems - Beijing China 		free(mbr);
8697c478bd9Sstevel@tonic-gate 		return (-1);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
87265908c77Syu, larry liu - Sun Microsystems - Beijing China 	(void) memcpy(&mboot, mbr, sizeof (struct mboot));
87365908c77Syu, larry liu - Sun Microsystems - Beijing China 
874e998e519SSheshadri Vasudevan #ifdef i386
875e998e519SSheshadri Vasudevan 	(void) extpart_init(&epp);
876e998e519SSheshadri Vasudevan #endif
8777c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
8787c478bd9Sstevel@tonic-gate 		int	ipc;
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 		ipc = i * sizeof (struct ipart);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		/* Handling the alignment problem of struct ipart */
8837c478bd9Sstevel@tonic-gate 		bootptr = &mboot.parts[ipc];
8847c478bd9Sstevel@tonic-gate 		(void) fill_ipart(bootptr, &ip);
8857c478bd9Sstevel@tonic-gate 
886aa1b14e7SSheshadri Vasudevan #ifdef i386
887aa1b14e7SSheshadri Vasudevan 		if (fdisk_is_dos_extended(ip.systid) && (ext_part_found == 0)) {
888aa1b14e7SSheshadri Vasudevan 			/* We support only one extended partition per disk */
889aa1b14e7SSheshadri Vasudevan 			ext_part_found = 1;
890aa1b14e7SSheshadri Vasudevan 			rval = fdisk_get_solaris_part(epp, &pno, &relsec,
891aa1b14e7SSheshadri Vasudevan 			    &numsec);
892aa1b14e7SSheshadri Vasudevan 			if (rval == FDISK_SUCCESS) {
893aa1b14e7SSheshadri Vasudevan 				/*
894aa1b14e7SSheshadri Vasudevan 				 * Found a solaris partition inside the
895aa1b14e7SSheshadri Vasudevan 				 * extended partition. Update the statistics.
896aa1b14e7SSheshadri Vasudevan 				 */
897aa1b14e7SSheshadri Vasudevan 				if ((label->dkl_nhead != 0) &&
898aa1b14e7SSheshadri Vasudevan 				    (label->dkl_nsect != 0)) {
899aa1b14e7SSheshadri Vasudevan 					label->dkl_pcyl =
900aa1b14e7SSheshadri Vasudevan 					    numsec / (label->dkl_nhead *
901aa1b14e7SSheshadri Vasudevan 					    label->dkl_nsect);
902aa1b14e7SSheshadri Vasudevan 					label->dkl_ncyl = label->dkl_pcyl -
903aa1b14e7SSheshadri Vasudevan 					    label->dkl_acyl;
904aa1b14e7SSheshadri Vasudevan 				}
905aa1b14e7SSheshadri Vasudevan 				solaris_offset = relsec;
906aa1b14e7SSheshadri Vasudevan 			}
907aa1b14e7SSheshadri Vasudevan 			continue;
908aa1b14e7SSheshadri Vasudevan 		}
909aa1b14e7SSheshadri Vasudevan #endif
910aa1b14e7SSheshadri Vasudevan 
9117c478bd9Sstevel@tonic-gate 		/*
9127c478bd9Sstevel@tonic-gate 		 * if the disk has an EFI label, the nhead and nsect fields
9137c478bd9Sstevel@tonic-gate 		 * the label may be zero.  This protects us from FPE's, and
9147c478bd9Sstevel@tonic-gate 		 * format still seems to work happily
9157c478bd9Sstevel@tonic-gate 		 */
916e998e519SSheshadri Vasudevan 
917e998e519SSheshadri Vasudevan 
918e998e519SSheshadri Vasudevan #ifdef i386
919e998e519SSheshadri Vasudevan 		if ((ip.systid == SUNIXOS &&
920e998e519SSheshadri Vasudevan 		    (fdisk_is_linux_swap(epp, lel(ip.relsect), NULL) != 0)) ||
921e998e519SSheshadri Vasudevan 		    ip.systid == SUNIXOS2 ||
922e998e519SSheshadri Vasudevan