xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 10682:4c75f60a2704)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55169Slclee  * Common Development and Distribution License (the "License").
65169Slclee  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215169Slclee 
220Sstevel@tonic-gate /*
238904SBarry.Harding@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
320Sstevel@tonic-gate /*	  All Rights Reserved	*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
360Sstevel@tonic-gate  * This program reads the partition table on the specified device and
370Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
380Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
390Sstevel@tonic-gate  * options are also available.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <ctype.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/types.h>
507563SPrasad.Singamsetty@Sun.COM #include <limits.h>
510Sstevel@tonic-gate #include <sys/param.h>
520Sstevel@tonic-gate #include <sys/systeminfo.h>
530Sstevel@tonic-gate #include <sys/efi_partition.h>
540Sstevel@tonic-gate #include <sys/byteorder.h>
550Sstevel@tonic-gate #include <sys/systeminfo.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
580Sstevel@tonic-gate #include <sys/dkio.h>
590Sstevel@tonic-gate #include <sys/vtoc.h>
6010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
6110021SSheshadri.Vasudevan@Sun.COM #include <sys/tty.h>
6210021SSheshadri.Vasudevan@Sun.COM #include <libfdisk.h>
6310021SSheshadri.Vasudevan@Sun.COM #endif
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #define	CLR_SCR ""
660Sstevel@tonic-gate #define	CLR_LIN ""
670Sstevel@tonic-gate #define	HOME "" \
680Sstevel@tonic-gate 	""
690Sstevel@tonic-gate #define	Q_LINE ""
7010021SSheshadri.Vasudevan@Sun.COM 
7110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
7210021SSheshadri.Vasudevan@Sun.COM #define	W_LINE ""
7310021SSheshadri.Vasudevan@Sun.COM #else
740Sstevel@tonic-gate #define	W_LINE ""
7510021SSheshadri.Vasudevan@Sun.COM #endif
7610021SSheshadri.Vasudevan@Sun.COM 
770Sstevel@tonic-gate #define	E_LINE ""
7810021SSheshadri.Vasudevan@Sun.COM 
7910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
8010021SSheshadri.Vasudevan@Sun.COM #define	M_LINE "" \
8110021SSheshadri.Vasudevan@Sun.COM 	""
8210021SSheshadri.Vasudevan@Sun.COM #else
830Sstevel@tonic-gate #define	M_LINE "" \
840Sstevel@tonic-gate 	""
8510021SSheshadri.Vasudevan@Sun.COM #endif
8610021SSheshadri.Vasudevan@Sun.COM 
870Sstevel@tonic-gate #define	T_LINE ""
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
900Sstevel@tonic-gate 
918333SSuhasini.Peddada@Sun.COM /* XXX - should be in fdisk.h, used by sd as well */
928333SSuhasini.Peddada@Sun.COM 
938333SSuhasini.Peddada@Sun.COM /*
948333SSuhasini.Peddada@Sun.COM  * the MAX values are the maximum usable values for BIOS chs values
958333SSuhasini.Peddada@Sun.COM  * The MAX_CYL value of 1022 is the maximum usable value
968333SSuhasini.Peddada@Sun.COM  *   the value of 1023 is a fence value,
978333SSuhasini.Peddada@Sun.COM  *   indicating no CHS geometry exists for the corresponding LBA value.
988333SSuhasini.Peddada@Sun.COM  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
998333SSuhasini.Peddada@Sun.COM  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
1008333SSuhasini.Peddada@Sun.COM  */
1018333SSuhasini.Peddada@Sun.COM #define	MAX_SECT	(63)
1028333SSuhasini.Peddada@Sun.COM #define	MAX_CYL		(1022)
1038333SSuhasini.Peddada@Sun.COM #define	MAX_HEAD	(254)
1048333SSuhasini.Peddada@Sun.COM 
1057563SPrasad.Singamsetty@Sun.COM #define	DK_MAX_2TB	UINT32_MAX	/* Max # of sectors in 2TB */
1067563SPrasad.Singamsetty@Sun.COM 
1070Sstevel@tonic-gate /* for clear_vtoc() */
1080Sstevel@tonic-gate #define	OLD		0
1090Sstevel@tonic-gate #define	NEW		1
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
1120Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
1130Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
1140Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
1150Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
1160Sstevel@tonic-gate 
1178333SSuhasini.Peddada@Sun.COM /*
1188333SSuhasini.Peddada@Sun.COM  * Support for fdisk(1M) on the SPARC platform
1198333SSuhasini.Peddada@Sun.COM  *	In order to convert little endian values to big endian for SPARC,
1208333SSuhasini.Peddada@Sun.COM  *	byte/short and long values must be swapped.
1218333SSuhasini.Peddada@Sun.COM  *	These swapping macros will be used to access information in the
1228333SSuhasini.Peddada@Sun.COM  *	mboot and ipart structures.
1238333SSuhasini.Peddada@Sun.COM  */
1248333SSuhasini.Peddada@Sun.COM 
1258333SSuhasini.Peddada@Sun.COM #ifdef sparc
1268333SSuhasini.Peddada@Sun.COM #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1278333SSuhasini.Peddada@Sun.COM #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
1288333SSuhasini.Peddada@Sun.COM 			    (les((unsigned)((val)&0xffff0000)>>16)))
1298333SSuhasini.Peddada@Sun.COM #else
1308333SSuhasini.Peddada@Sun.COM #define	les(val)	(val)
1318333SSuhasini.Peddada@Sun.COM #define	lel(val)	(val)
1328333SSuhasini.Peddada@Sun.COM #endif
1338333SSuhasini.Peddada@Sun.COM 
1340Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1356549Sbharding #define	VTOC_OFFSET	1
1360Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1370Sstevel@tonic-gate #define	VTOC_OFFSET	0
1380Sstevel@tonic-gate #else
1390Sstevel@tonic-gate #error No VTOC format defined.
1400Sstevel@tonic-gate #endif
1410Sstevel@tonic-gate 
14210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
14310021SSheshadri.Vasudevan@Sun.COM #define	FDISK_KB	(1024)
14410021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MB	(FDISK_KB * 1024)
14510021SSheshadri.Vasudevan@Sun.COM #define	FDISK_GB	(FDISK_MB * 1024)
14610021SSheshadri.Vasudevan@Sun.COM #define	TRUE	1
14710021SSheshadri.Vasudevan@Sun.COM 
14810021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_ID	255
14910021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_NUM_DIGITS	2
15010021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_ID_DIGITS	3
15110021SSheshadri.Vasudevan@Sun.COM 
15210021SSheshadri.Vasudevan@Sun.COM /* Maximum number of digits for a valid partition size */
15310021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_CYL_NUM_DIGITS	10
15410021SSheshadri.Vasudevan@Sun.COM 
15510021SSheshadri.Vasudevan@Sun.COM /* Minimum partition size in cylinders */
15610021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MIN_PART_SIZE	1
15710021SSheshadri.Vasudevan@Sun.COM #endif
15810021SSheshadri.Vasudevan@Sun.COM 
159251Slclee static char Usage[] = "Usage: fdisk\n"
1600Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1610Sstevel@tonic-gate "[ -b masterboot ]\n"
1620Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1630Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1640Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1650Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1660Sstevel@tonic-gate 
167251Slclee static char Usage1[] = "    Partition options:\n"
1680Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1690Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1700Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1710Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1720Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1730Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1740Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1750Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1760Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1770Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1780Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1790Sstevel@tonic-gate "			  start of partition\n"
1800Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1810Sstevel@tonic-gate "	-b master_boot\n"
1820Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1830Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1840Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1850Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1860Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1870Sstevel@tonic-gate "	-F fdisk_file\n"
1880Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1890Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1900Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1910Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1920Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1930Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1940Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1950Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1960Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1970Sstevel@tonic-gate "	-W fdisk_file\n"
1980Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1990Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
2000Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
2010Sstevel@tonic-gate "    Diagnostic options:\n"
2020Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
2030Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
2040Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
2050Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
2060Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
2070Sstevel@tonic-gate "		BCYL		cylinder offset\n"
2080Sstevel@tonic-gate "		NHEADS		number of heads\n"
2090Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
2100Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
2110Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
2120Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
2130Sstevel@tonic-gate "	-o offset\n"
2140Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
2150Sstevel@tonic-gate "		-P # specified.\n"
2160Sstevel@tonic-gate "	-P fill_patt\n"
2170Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
2180Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
2190Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
2200Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
2210Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
2220Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
2230Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
2240Sstevel@tonic-gate "	-S geom_file\n"
2250Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
2260Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
2270Sstevel@tonic-gate 
228251Slclee static char Ostr[] = "Other OS";
229251Slclee static char Dstr[] = "DOS12";
230251Slclee static char D16str[] = "DOS16";
231251Slclee static char DDstr[] = "DOS-DATA";
232251Slclee static char EDstr[] = "EXT-DOS";
233251Slclee static char DBstr[] = "DOS-BIG";
234251Slclee static char PCstr[] = "PCIX";
235251Slclee static char Ustr[] = "UNIX System";
236251Slclee static char SUstr[] = "Solaris";
237251Slclee static char SU2str[] = "Solaris2";
238251Slclee static char X86str[] = "x86 Boot";
239251Slclee static char DIAGstr[] = "Diagnostic";
240251Slclee static char IFSstr[] = "IFS: NTFS";
241251Slclee static char AIXstr[] = "AIX Boot";
242251Slclee static char AIXDstr[] = "AIX Data";
243251Slclee static char OS2str[] = "OS/2 Boot";
244251Slclee static char WINstr[] = "Win95 FAT32";
245251Slclee static char EWINstr[] = "Ext Win95";
246251Slclee static char FAT95str[] = "FAT16 LBA";
247251Slclee static char EXTLstr[] = "EXT LBA";
248251Slclee static char LINUXstr[] = "Linux";
249251Slclee static char CPMstr[] = "CP/M";
25010021SSheshadri.Vasudevan@Sun.COM static char NOV2str[] = "Netware 286";
251251Slclee static char NOVstr[] = "Netware 3.x+";
252251Slclee static char QNXstr[] = "QNX 4.x";
253251Slclee static char QNX2str[] = "QNX part 2";
254251Slclee static char QNX3str[] = "QNX part 3";
255251Slclee static char LINNATstr[] = "Linux native";
256*10682SSheshadri.Vasudevan@Sun.COM static char LINSWAPstr[] = "Linux swap";
257251Slclee static char NTFSVOL1str[] = "NT volset 1";
258251Slclee static char NTFSVOL2str[] = "NT volset 2";
259251Slclee static char BSDstr[] = "BSD OS";
260251Slclee static char NEXTSTEPstr[] = "NeXTSTEP";
261251Slclee static char BSDIFSstr[] = "BSDI FS";
262251Slclee static char BSDISWAPstr[] = "BSDI swap";
263251Slclee static char Actvstr[] = "Active";
264251Slclee static char EFIstr[] = "EFI";
265251Slclee static char NAstr[] = "      ";
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /* All the user options and flags */
268251Slclee static char *Dfltdev;			/* name of fixed disk drive */
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate /* Diagnostic options */
271251Slclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
272251Slclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
273251Slclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
274251Slclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
275251Slclee static int	io_lgeom = 0;		/* get label geometry (-g) */
276251Slclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
277251Slclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
278251Slclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2810Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
2826549Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
2836549Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /* Partition table flags */
286251Slclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
287251Slclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
288251Slclee static int	io_fdisk = 0;		/* do fdisk operation */
289251Slclee static int	io_ifdisk = 0;		/* interactive partition */
290251Slclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
291251Slclee 
292251Slclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
293251Slclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
294251Slclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
295251Slclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
296251Slclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
297251Slclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
298251Slclee 
299251Slclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
300251Slclee 
301251Slclee static struct mboot BootCod;		/* buffer for master boot record */
302251Slclee 
303251Slclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
304251Slclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
305251Slclee static int	io_debug = 0;		/* activate verbose mode (-d) */
306251Slclee static int	io_image = 0;		/* create image using geometry (-I) */
307251Slclee 
308251Slclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
309251Slclee static char	*Bootsect;		/* pointer to sector zero buffer */
310251Slclee static char	*Nullsect;
3117563SPrasad.Singamsetty@Sun.COM static struct extvtoc	disk_vtoc;	/* verify VTOC table */
312251Slclee static int	vt_inval = 0;
313251Slclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
314251Slclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
315251Slclee 
316251Slclee static struct ipart	Table[FD_NUMPART];
317251Slclee static struct ipart	Old_Table[FD_NUMPART];
3188904SBarry.Harding@Sun.COM static int		skip_verify[FD_NUMPART]; /* special case skip sz chk */
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /* Disk geometry information */
3215169Slclee static struct dk_minfo	minfo;
322251Slclee static struct dk_geom	disk_geom;
323251Slclee 
3247563SPrasad.Singamsetty@Sun.COM static int Dev;			/* fd for open device */
3257563SPrasad.Singamsetty@Sun.COM 
3265169Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
3277563SPrasad.Singamsetty@Sun.COM static diskaddr_t	chs_capacity;	/* Numcyl_usable * heads * sectors */
3287563SPrasad.Singamsetty@Sun.COM 
3297563SPrasad.Singamsetty@Sun.COM static int		Numcyl_usable;	/* Number of usable cylinders */
3307563SPrasad.Singamsetty@Sun.COM 					/*  used to limit fdisk to 2TB */
3317563SPrasad.Singamsetty@Sun.COM 
3320Sstevel@tonic-gate /* Physical geometry for the drive */
333251Slclee static int	Numcyl;			/* number of cylinders */
334251Slclee static int	heads;			/* number of heads */
335251Slclee static int	sectors;		/* number of sectors per track */
336251Slclee static int	acyl;			/* number of alternate sectors */
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
339251Slclee static int	hba_Numcyl;		/* number of cylinders */
340251Slclee static int	hba_heads;		/* number of heads */
341251Slclee static int	hba_sectors;		/* number of sectors per track */
342251Slclee 
343251Slclee static int	sectsiz;		/* sector size */
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate /* Load functions for fdisk table modification */
3460Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3470Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3480Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate #define	CBUFLEN 80
351251Slclee static char s[CBUFLEN];
352251Slclee 
35310021SSheshadri.Vasudevan@Sun.COM #ifdef i386
35410021SSheshadri.Vasudevan@Sun.COM /*
35510021SSheshadri.Vasudevan@Sun.COM  * Complete list of all the 255 partition types. Some are unknown types
35610021SSheshadri.Vasudevan@Sun.COM  * and some entries are known to be unused.
35710021SSheshadri.Vasudevan@Sun.COM  *
35810021SSheshadri.Vasudevan@Sun.COM  * Courtesy of http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
35910021SSheshadri.Vasudevan@Sun.COM  */
36010021SSheshadri.Vasudevan@Sun.COM char *fdisk_part_types[] = {
36110021SSheshadri.Vasudevan@Sun.COM 	"Empty",				/* 0 */
36210021SSheshadri.Vasudevan@Sun.COM 	"FAT12",				/* 1 */
36310021SSheshadri.Vasudevan@Sun.COM 	"XENIX /",				/* 2 */
36410021SSheshadri.Vasudevan@Sun.COM 	"XENIX /usr",				/* 3 */
36510021SSheshadri.Vasudevan@Sun.COM 	"FAT16 (Upto 32M)",			/* 4 */
36610021SSheshadri.Vasudevan@Sun.COM 	"DOS Extended",				/* 5 */
36710021SSheshadri.Vasudevan@Sun.COM 	"FAT16 (>32M, HUGEDOS)",		/* 6 */
36810021SSheshadri.Vasudevan@Sun.COM 	"IFS: NTFS",				/* 7 */
36910021SSheshadri.Vasudevan@Sun.COM 	"AIX Boot/QNX(qny)",			/* 8 */
37010021SSheshadri.Vasudevan@Sun.COM 	"AIX Data/QNX(qnz)",			/* 9 */
37110021SSheshadri.Vasudevan@Sun.COM 	"OS/2 Boot/Coherent swap",		/* 10 */
37210021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT32(Upto 2047GB)",		/* 11 */
37310021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT32(LBA)",			/* 12 */
37410021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 13 */
37510021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT16(LBA)",			/* 14 */
37610021SSheshadri.Vasudevan@Sun.COM 	"WIN95 Extended(LBA)",			/* 15 */
37710021SSheshadri.Vasudevan@Sun.COM 	"OPUS",					/* 16 */
37810021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT12",				/* 17 */
37910021SSheshadri.Vasudevan@Sun.COM 	"Diagnostic",				/* 18 */
38010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 19 */
38110021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(Upto 32M)",		/* 20 */
38210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 21 */
38310021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(>=32M)",			/* 22 */
38410021SSheshadri.Vasudevan@Sun.COM 	"Hidden IFS: HPFS",			/* 23 */
38510021SSheshadri.Vasudevan@Sun.COM 	"AST SmartSleep Partition",		/* 24 */
38610021SSheshadri.Vasudevan@Sun.COM 	"Unused/Willowtech Photon",		/* 25 */
38710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 26 */
38810021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT32",				/* 27 */
38910021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT32(LBA)",			/* 28 */
39010021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 29 */
39110021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(LBA)",			/* 30 */
39210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 31 */
39310021SSheshadri.Vasudevan@Sun.COM 	"Unused/OSF1",				/* 32 */
39410021SSheshadri.Vasudevan@Sun.COM 	"Reserved/FSo2(Oxygen FS)",		/* 33 */
39510021SSheshadri.Vasudevan@Sun.COM 	"Unused/(Oxygen EXT)",			/* 34 */
39610021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 35 */
39710021SSheshadri.Vasudevan@Sun.COM 	"NEC DOS 3.x",				/* 36 */
39810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 37 */
39910021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 38 */
40010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 39 */
40110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 40 */
40210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 41 */
40310021SSheshadri.Vasudevan@Sun.COM 	"AtheOS File System",			/* 42 */
40410021SSheshadri.Vasudevan@Sun.COM 	"SyllableSecure",			/* 43 */
40510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 44 */
40610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 45 */
40710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 46 */
40810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 47 */
40910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 48 */
41010021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 49 */
41110021SSheshadri.Vasudevan@Sun.COM 	"NOS",					/* 50 */
41210021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 51 */
41310021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 52 */
41410021SSheshadri.Vasudevan@Sun.COM 	"JFS on OS/2",				/* 53 */
41510021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 54 */
41610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 55 */
41710021SSheshadri.Vasudevan@Sun.COM 	"THEOS 3.2 2GB",			/* 56 */
41810021SSheshadri.Vasudevan@Sun.COM 	"Plan9/THEOS 4",			/* 57 */
41910021SSheshadri.Vasudevan@Sun.COM 	"THEOS 4 4GB",				/* 58 */
42010021SSheshadri.Vasudevan@Sun.COM 	"THEOS 4 Extended",			/* 59 */
42110021SSheshadri.Vasudevan@Sun.COM 	"PartitionMagic Recovery",		/* 60 */
42210021SSheshadri.Vasudevan@Sun.COM 	"Hidden NetWare",			/* 61 */
42310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 62 */
42410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 63 */
42510021SSheshadri.Vasudevan@Sun.COM 	"Venix 80286",				/* 64 */
42610021SSheshadri.Vasudevan@Sun.COM 	"MINIX/PPC PReP Boot",			/* 65 */
42710021SSheshadri.Vasudevan@Sun.COM 	"Win2K Dynamic Disk/SFS(DOS)",		/* 66 */
42810021SSheshadri.Vasudevan@Sun.COM 	"Linux+DRDOS shared",			/* 67 */
42910021SSheshadri.Vasudevan@Sun.COM 	"GoBack partition",			/* 68 */
43010021SSheshadri.Vasudevan@Sun.COM 	"Boot-US boot manager",			/* 69 */
43110021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 70 */
43210021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 71 */
43310021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 72 */
43410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 73 */
43510021SSheshadri.Vasudevan@Sun.COM 	"ALFS/THIN FS for DOS",			/* 74 */
43610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 75 */
43710021SSheshadri.Vasudevan@Sun.COM 	"Oberon partition",			/* 76 */
43810021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x",				/* 77 */
43910021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x 2nd Part",			/* 78 */
44010021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x 3rd Part",			/* 79 */
44110021SSheshadri.Vasudevan@Sun.COM 	"OnTrack DM R/O, Lynx RTOS",		/* 80 */
44210021SSheshadri.Vasudevan@Sun.COM 	"OnTrack DM R/W, Novell",		/* 81 */
44310021SSheshadri.Vasudevan@Sun.COM 	"CP/M",					/* 82 */
44410021SSheshadri.Vasudevan@Sun.COM 	"Disk Manager 6.0 Aux3",		/* 83 */
44510021SSheshadri.Vasudevan@Sun.COM 	"Disk Manager 6.0 DDO",			/* 84 */
44610021SSheshadri.Vasudevan@Sun.COM 	"EZ-Drive",				/* 85 */
44710021SSheshadri.Vasudevan@Sun.COM 	"Golden Bow VFeature/AT&T MS-DOS",	/* 86 */
44810021SSheshadri.Vasudevan@Sun.COM 	"DrivePro",				/* 87 */
44910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 88 */
45010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 89 */
45110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 90 */
45210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 91 */
45310021SSheshadri.Vasudevan@Sun.COM 	"Priam EDisk",				/* 92 */
45410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 93 */
45510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 94 */
45610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 95 */
45710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 96 */
45810021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 97 */
45910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 98 */
46010021SSheshadri.Vasudevan@Sun.COM 	"Unix SysV, Mach, GNU Hurd",		/* 99 */
46110021SSheshadri.Vasudevan@Sun.COM 	"PC-ARMOUR, Netware 286",		/* 100 */
46210021SSheshadri.Vasudevan@Sun.COM 	"Netware 386",				/* 101 */
46310021SSheshadri.Vasudevan@Sun.COM 	"Netware SMS",				/* 102 */
46410021SSheshadri.Vasudevan@Sun.COM 	"Novell",				/* 103 */
46510021SSheshadri.Vasudevan@Sun.COM 	"Novell",				/* 104 */
46610021SSheshadri.Vasudevan@Sun.COM 	"Netware NSS",				/* 105 */
46710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 106 */
46810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 107 */
46910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 108 */
47010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 109 */
47110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 110 */
47210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 111 */
47310021SSheshadri.Vasudevan@Sun.COM 	"DiskSecure Multi-Boot",		/* 112 */
47410021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 113 */
47510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 114 */
47610021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 115 */
47710021SSheshadri.Vasudevan@Sun.COM 	"Scramdisk partition",			/* 116 */
47810021SSheshadri.Vasudevan@Sun.COM 	"IBM PC/IX",				/* 117 */
47910021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 118 */
48010021SSheshadri.Vasudevan@Sun.COM 	"M2FS/M2CS,Netware VNDI",		/* 119 */
48110021SSheshadri.Vasudevan@Sun.COM 	"XOSL FS",				/* 120 */
48210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 121 */
48310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 122 */
48410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 123 */
48510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 124 */
48610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 125 */
48710021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 126 */
48810021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 127 */
48910021SSheshadri.Vasudevan@Sun.COM 	"MINIX until 1.4a",			/* 128 */
49010021SSheshadri.Vasudevan@Sun.COM 	"MINIX since 1.4b, early Linux",	/* 129 */
49110021SSheshadri.Vasudevan@Sun.COM 	"Solaris/Linux swap",			/* 130 */
49210021SSheshadri.Vasudevan@Sun.COM 	"Linux native",				/* 131 */
49310021SSheshadri.Vasudevan@Sun.COM 	"OS/2 hidden,Win Hibernation",		/* 132 */
49410021SSheshadri.Vasudevan@Sun.COM 	"Linux extended",			/* 133 */
49510021SSheshadri.Vasudevan@Sun.COM 	"Old Linux RAID,NT FAT16 RAID",		/* 134 */
49610021SSheshadri.Vasudevan@Sun.COM 	"NTFS volume set",			/* 135 */
49710021SSheshadri.Vasudevan@Sun.COM 	"Linux plaintext part table",		/* 136 */
49810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 137 */
49910021SSheshadri.Vasudevan@Sun.COM 	"Linux Kernel Partition",		/* 138 */
50010021SSheshadri.Vasudevan@Sun.COM 	"Fault Tolerant FAT32 volume",		/* 139 */
50110021SSheshadri.Vasudevan@Sun.COM 	"Fault Tolerant FAT32 volume",		/* 140 */
50210021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT12",		/* 141 */
50310021SSheshadri.Vasudevan@Sun.COM 	"Linux LVM partition",			/* 142 */
50410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 143 */
50510021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT16",		/* 144 */
50610021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden DOS EXT",		/* 145 */
50710021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT16 Large",	/* 146 */
50810021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux native, Amoeba",		/* 147 */
50910021SSheshadri.Vasudevan@Sun.COM 	"Amoeba Bad Block Table",		/* 148 */
51010021SSheshadri.Vasudevan@Sun.COM 	"MIT EXOPC Native",			/* 149 */
51110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 150 */
51210021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT32",		/* 151 */
51310021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT32 LBA",		/* 152 */
51410021SSheshadri.Vasudevan@Sun.COM 	"DCE376 logical drive",			/* 153 */
51510021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT16 LBA",		/* 154 */
51610021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden DOS EXT",		/* 155 */
51710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 156 */
51810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 157 */
51910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 158 */
52010021SSheshadri.Vasudevan@Sun.COM 	"BSD/OS",				/* 159 */
52110021SSheshadri.Vasudevan@Sun.COM 	"Laptop hibernation",			/* 160 */
52210021SSheshadri.Vasudevan@Sun.COM 	"Laptop hibernate,HP SpeedStor",	/* 161 */
52310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 162 */
52410021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 163 */
52510021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 164 */
52610021SSheshadri.Vasudevan@Sun.COM 	"BSD/386,386BSD,NetBSD,FreeBSD",	/* 165 */
52710021SSheshadri.Vasudevan@Sun.COM 	"OpenBSD,HP SpeedStor",			/* 166 */
52810021SSheshadri.Vasudevan@Sun.COM 	"NeXTStep",				/* 167 */
52910021SSheshadri.Vasudevan@Sun.COM 	"Mac OS-X",				/* 168 */
53010021SSheshadri.Vasudevan@Sun.COM 	"NetBSD",				/* 169 */
53110021SSheshadri.Vasudevan@Sun.COM 	"Olivetti FAT12 1.44MB Service",	/* 170 */
53210021SSheshadri.Vasudevan@Sun.COM 	"Mac OS-X Boot",			/* 171 */
53310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 172 */
53410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 173 */
53510021SSheshadri.Vasudevan@Sun.COM 	"ShagOS filesystem",			/* 174 */
53610021SSheshadri.Vasudevan@Sun.COM 	"ShagOS swap",				/* 175 */
53710021SSheshadri.Vasudevan@Sun.COM 	"BootStar Dummy",			/* 176 */
53810021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 177 */
53910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 178 */
54010021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 179 */
54110021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 180 */
54210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 181 */
54310021SSheshadri.Vasudevan@Sun.COM 	"Corrupted FAT16 NT Mirror Set",	/* 182 */
54410021SSheshadri.Vasudevan@Sun.COM 	"Corrupted NTFS NT Mirror Set",		/* 183 */
54510021SSheshadri.Vasudevan@Sun.COM 	"Old BSDI BSD/386 swap",		/* 184 */
54610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 185 */
54710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 186 */
54810021SSheshadri.Vasudevan@Sun.COM 	"Boot Wizard hidden",			/* 187 */
54910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 188 */
55010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 189 */
55110021SSheshadri.Vasudevan@Sun.COM 	"Solaris x86 boot",			/* 190 */
55210021SSheshadri.Vasudevan@Sun.COM 	"Solaris2",				/* 191 */
55310021SSheshadri.Vasudevan@Sun.COM 	"REAL/32 or Novell DOS secured",	/* 192 */
55410021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(FAT12)",			/* 193 */
55510021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux",				/* 194 */
55610021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux swap",			/* 195 */
55710021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(FAT16,< 32M)",		/* 196 */
55810021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(Extended)",		/* 197 */
55910021SSheshadri.Vasudevan@Sun.COM 	"NT corrupted FAT16 volume",		/* 198 */
56010021SSheshadri.Vasudevan@Sun.COM 	"NT corrupted NTFS volume",		/* 199 */
56110021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 200 */
56210021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 201 */
56310021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 202 */
56410021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secured FAT32(CHS)",	/* 203 */
56510021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secured FAT32(LBA)",	/* 204 */
56610021SSheshadri.Vasudevan@Sun.COM 	"CTOS Memdump",				/* 205 */
56710021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ FAT16X(LBA)",		/* 206 */
56810021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secure EXT DOS(LBA)",	/* 207 */
56910021SSheshadri.Vasudevan@Sun.COM 	"REAL/32 secure big, MDOS",		/* 208 */
57010021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT12",		/* 209 */
57110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 210 */
57210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 211 */
57310021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT16 <32M",		/* 212 */
57410021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure EXT",			/* 213 */
57510021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT16 >=32M",		/* 214 */
57610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 215 */
57710021SSheshadri.Vasudevan@Sun.COM 	"CP/M-86",				/* 216 */
57810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 217 */
57910021SSheshadri.Vasudevan@Sun.COM 	"Non-FS Data",				/* 218 */
58010021SSheshadri.Vasudevan@Sun.COM 	"CP/M,Concurrent DOS,CTOS",		/* 219 */
58110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 220 */
58210021SSheshadri.Vasudevan@Sun.COM 	"Hidden CTOS memdump",			/* 221 */
58310021SSheshadri.Vasudevan@Sun.COM 	"Dell PowerEdge utilities(FAT)",	/* 222 */
58410021SSheshadri.Vasudevan@Sun.COM 	"DG/UX virtual disk manager",		/* 223 */
58510021SSheshadri.Vasudevan@Sun.COM 	"ST AVFS(STMicroelectronics)",		/* 224 */
58610021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor 12-bit FAT EXT",		/* 225 */
58710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 226 */
58810021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 227 */
58910021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor 16-bit FAT EXT",		/* 228 */
59010021SSheshadri.Vasudevan@Sun.COM 	"Tandy MSDOS",				/* 229 */
59110021SSheshadri.Vasudevan@Sun.COM 	"Storage Dimensions SpeedStor",		/* 230 */
59210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 231 */
59310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 232 */
59410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 233 */
59510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 234 */
59610021SSheshadri.Vasudevan@Sun.COM 	"BeOS BFS",				/* 235 */
59710021SSheshadri.Vasudevan@Sun.COM 	"SkyOS SkyFS",				/* 236 */
59810021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 237 */
59910021SSheshadri.Vasudevan@Sun.COM 	"EFI Header Indicator",			/* 238 */
60010021SSheshadri.Vasudevan@Sun.COM 	"EFI Filesystem",			/* 239 */
60110021SSheshadri.Vasudevan@Sun.COM 	"Linux/PA-RISC boot loader",		/* 240 */
60210021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 241 */
60310021SSheshadri.Vasudevan@Sun.COM 	"DOS 3.3+ secondary",			/* 242 */
60410021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor Reserved",			/* 243 */
60510021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor Large",			/* 244 */
60610021SSheshadri.Vasudevan@Sun.COM 	"Prologue multi-volume",		/* 245 */
60710021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 246 */
60810021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 247 */
60910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 248 */
61010021SSheshadri.Vasudevan@Sun.COM 	"pCache",				/* 249 */
61110021SSheshadri.Vasudevan@Sun.COM 	"Bochs",				/* 250 */
61210021SSheshadri.Vasudevan@Sun.COM 	"VMware File System",			/* 251 */
61310021SSheshadri.Vasudevan@Sun.COM 	"VMware swap",				/* 252 */
61410021SSheshadri.Vasudevan@Sun.COM 	"Linux raid autodetect",		/* 253 */
61510021SSheshadri.Vasudevan@Sun.COM 	"NT Disk Administrator hidden",		/* 254 */
61610021SSheshadri.Vasudevan@Sun.COM 	"Xenix Bad Block Table"			/* 255 */
61710021SSheshadri.Vasudevan@Sun.COM };
61810021SSheshadri.Vasudevan@Sun.COM 
61910021SSheshadri.Vasudevan@Sun.COM /* Allowed extended partition menu options */
62010021SSheshadri.Vasudevan@Sun.COM static char ext_part_menu_opts[] = "adhipr";
62110021SSheshadri.Vasudevan@Sun.COM 
62210021SSheshadri.Vasudevan@Sun.COM /*
62310021SSheshadri.Vasudevan@Sun.COM  * Structure holding all information about the extended partition
62410021SSheshadri.Vasudevan@Sun.COM  * NOTE : As of now, there will be just one instance of ext_part_t, since most
62510021SSheshadri.Vasudevan@Sun.COM  * known systems allow only one extended dos partition per disk.
62610021SSheshadri.Vasudevan@Sun.COM  */
62710021SSheshadri.Vasudevan@Sun.COM static ext_part_t *epp;
62810021SSheshadri.Vasudevan@Sun.COM #endif
62910021SSheshadri.Vasudevan@Sun.COM 
630251Slclee static void update_disk_and_exit(boolean_t table_changed);
631251Slclee int main(int argc, char *argv[]);
632251Slclee static int read_geom(char *sgeom);
633251Slclee static void dev_mboot_read(void);
6346549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz);
635251Slclee static void mboot_read(void);
636251Slclee static void fill_patt(void);
637251Slclee static void abs_read(void);
638251Slclee static void abs_write(void);
639251Slclee static void load(int funct, char *file);
640251Slclee static void Set_Table_CHS_Values(int ti);
641251Slclee static int insert_tbl(int id, int act,
642251Slclee     int bhead, int bsect, int bcyl,
643251Slclee     int ehead, int esect, int ecyl,
6447563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect);
6458904SBarry.Harding@Sun.COM static int entry_from_old_table(int id, int act,
6468904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
6478904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
6488904SBarry.Harding@Sun.COM     uint32_t rsect, uint32_t numsect);
649251Slclee static int verify_tbl(void);
650251Slclee static int pars_fdisk(char *line,
651251Slclee     int *id, int *act,
652251Slclee     int *bhead, int *bsect, int *bcyl,
653251Slclee     int *ehead, int *esect, int *ecyl,
6547563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect);
6557563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect);
656251Slclee static void stage0(void);
657251Slclee static int pcreate(void);
658251Slclee static int specify(uchar_t tsystid);
659251Slclee static void dispmenu(void);
660251Slclee static int pchange(void);
661251Slclee static int ppartid(void);
662251Slclee static char pdelete(void);
663251Slclee static void rm_blanks(char *s);
664251Slclee static int getcyl(void);
665251Slclee static void disptbl(void);
666251Slclee static void print_Table(void);
667251Slclee static void copy_Table_to_Old_Table(void);
668251Slclee static void nulltbl(void);
669251Slclee static void copy_Bootblk_to_Table(void);
670251Slclee static void fill_ipart(char *bootptr, struct ipart *partp);
671251Slclee #ifdef sparc
672251Slclee uchar_t getbyte(char **bp);
673251Slclee uint32_t getlong(char **bp);
674251Slclee #endif
675251Slclee static void copy_Table_to_Bootblk(void);
676251Slclee static int TableChanged(void);
677251Slclee static void ffile_write(char *file);
678251Slclee static void fix_slice(void);
679251Slclee static int yesno(void);
680251Slclee static int readvtoc(void);
681251Slclee static int writevtoc(void);
682251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
683251Slclee static int clear_efi(void);
684251Slclee static void clear_vtoc(int table, int part);
685251Slclee static int lecture_and_query(char *warning, char *devname);
686251Slclee static void sanity_check_provided_device(char *devname, int fd);
687251Slclee static char *get_node(char *devname);
6880Sstevel@tonic-gate 
68910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
69010021SSheshadri.Vasudevan@Sun.COM static void id_to_name(uchar_t sysid, char *buffer);
69110021SSheshadri.Vasudevan@Sun.COM static void ext_read_input(char *buf);
69210021SSheshadri.Vasudevan@Sun.COM static int ext_read_options(char *buf);
69310021SSheshadri.Vasudevan@Sun.COM static int ext_invalid_option(char ch);
69410021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_num(int *pno);
69510021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_id(uchar_t *partid);
69610021SSheshadri.Vasudevan@Sun.COM static int ext_read_valid_partition_start(uint32_t *begsec);
69710021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec);
69810021SSheshadri.Vasudevan@Sun.COM static void ext_part_menu();
69910021SSheshadri.Vasudevan@Sun.COM static void add_logical_drive();
70010021SSheshadri.Vasudevan@Sun.COM static void delete_logical_drive();
70110021SSheshadri.Vasudevan@Sun.COM static void ext_print_help_menu();
70210021SSheshadri.Vasudevan@Sun.COM static void ext_change_logical_drive_id();
70310021SSheshadri.Vasudevan@Sun.COM static void ext_print_part_types();
70410021SSheshadri.Vasudevan@Sun.COM static void ext_print_logical_drive_layout();
70510021SSheshadri.Vasudevan@Sun.COM static void preach_and_continue();
70610021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
70710021SSheshadri.Vasudevan@Sun.COM static void ext_print_logdrive_layout_debug();
70810021SSheshadri.Vasudevan@Sun.COM #endif	/* DEBUG */
70910021SSheshadri.Vasudevan@Sun.COM #endif	/* i386 */
71010021SSheshadri.Vasudevan@Sun.COM 
71110021SSheshadri.Vasudevan@Sun.COM /*
71210021SSheshadri.Vasudevan@Sun.COM  * This function is called only during the non-interactive mode.
71310021SSheshadri.Vasudevan@Sun.COM  * It is touchy and does not tolerate any errors. If there are
71410021SSheshadri.Vasudevan@Sun.COM  * mounted logical drives, changes to the partition table
71510021SSheshadri.Vasudevan@Sun.COM  * is disallowed.
71610021SSheshadri.Vasudevan@Sun.COM  */
7170Sstevel@tonic-gate static void
7180Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
7190Sstevel@tonic-gate {
72010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
72110021SSheshadri.Vasudevan@Sun.COM 	int rval;
72210021SSheshadri.Vasudevan@Sun.COM #endif
7230Sstevel@tonic-gate 	if (table_changed) {
7240Sstevel@tonic-gate 		/*
7250Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
7260Sstevel@tonic-gate 		 * and write it to disk
7270Sstevel@tonic-gate 		 */
7280Sstevel@tonic-gate 		copy_Table_to_Bootblk();
7290Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
7330Sstevel@tonic-gate 	if (io_adjt)
7340Sstevel@tonic-gate 		fix_slice();
7350Sstevel@tonic-gate 
73610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
73710021SSheshadri.Vasudevan@Sun.COM 	if (!io_readonly) {
73810021SSheshadri.Vasudevan@Sun.COM 		rval = fdisk_commit_ext_part(epp);
73910021SSheshadri.Vasudevan@Sun.COM 		switch (rval) {
74010021SSheshadri.Vasudevan@Sun.COM 			case FDISK_SUCCESS:
74110021SSheshadri.Vasudevan@Sun.COM 				/* Success */
74210021SSheshadri.Vasudevan@Sun.COM 				break;
74310021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOEXTPART:
74410021SSheshadri.Vasudevan@Sun.COM 				/* Nothing to do */
74510021SSheshadri.Vasudevan@Sun.COM 				break;
74610021SSheshadri.Vasudevan@Sun.COM 			default:
74710021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Error in"
74810021SSheshadri.Vasudevan@Sun.COM 				    " fdisk_commit_ext_part\n");
74910021SSheshadri.Vasudevan@Sun.COM 				exit(rval);
75010021SSheshadri.Vasudevan@Sun.COM 		}
75110021SSheshadri.Vasudevan@Sun.COM 	}
75210021SSheshadri.Vasudevan@Sun.COM 	libfdisk_fini(&epp);
75310021SSheshadri.Vasudevan@Sun.COM #endif
7540Sstevel@tonic-gate 	exit(0);
7550Sstevel@tonic-gate }
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate /*
7580Sstevel@tonic-gate  * main
7590Sstevel@tonic-gate  * Process command-line options.
7600Sstevel@tonic-gate  */
761251Slclee int
7620Sstevel@tonic-gate main(int argc, char *argv[])
7630Sstevel@tonic-gate {
764251Slclee 	int c, i;
7650Sstevel@tonic-gate 	extern	int optind;
7660Sstevel@tonic-gate 	extern	char *optarg;
7670Sstevel@tonic-gate 	int	errflg = 0;
7680Sstevel@tonic-gate 	int	diag_cnt = 0;
7690Sstevel@tonic-gate 	int openmode;
77010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
77110021SSheshadri.Vasudevan@Sun.COM 	int rval;
77210021SSheshadri.Vasudevan@Sun.COM 	int lf_op_flag = 0;
77310021SSheshadri.Vasudevan@Sun.COM #endif
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
7760Sstevel@tonic-gate 	setbuf(stdout, 0);
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/* Process the options. */
7790Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
7800Sstevel@tonic-gate 	    != EOF) {
7810Sstevel@tonic-gate 		switch (c) {
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 			case 'o':
7846549Sbharding 				io_offset = (off_t)strtoull(optarg, 0, 0);
7850Sstevel@tonic-gate 				continue;
7860Sstevel@tonic-gate 			case 's':
7876549Sbharding 				io_size = (off_t)strtoull(optarg, 0, 0);
7880Sstevel@tonic-gate 				continue;
7890Sstevel@tonic-gate 			case 'P':
7900Sstevel@tonic-gate 				diag_cnt++;
7910Sstevel@tonic-gate 				io_patt++;
7920Sstevel@tonic-gate 				io_fatt = optarg;
7930Sstevel@tonic-gate 				continue;
7940Sstevel@tonic-gate 			case 'w':
7950Sstevel@tonic-gate 				diag_cnt++;
7960Sstevel@tonic-gate 				io_wrt++;
7970Sstevel@tonic-gate 				continue;
7980Sstevel@tonic-gate 			case 'r':
7990Sstevel@tonic-gate 				diag_cnt++;
8000Sstevel@tonic-gate 				io_rd++;
8010Sstevel@tonic-gate 				continue;
8020Sstevel@tonic-gate 			case 'd':
8030Sstevel@tonic-gate 				io_debug++;
8040Sstevel@tonic-gate 				continue;
8050Sstevel@tonic-gate 			case 'I':
8060Sstevel@tonic-gate 				io_image++;
8070Sstevel@tonic-gate 				continue;
8080Sstevel@tonic-gate 			case 'R':
8090Sstevel@tonic-gate 				io_readonly++;
8100Sstevel@tonic-gate 				continue;
8110Sstevel@tonic-gate 			case 'S':
8120Sstevel@tonic-gate 				diag_cnt++;
8130Sstevel@tonic-gate 				io_sgeom = optarg;
8140Sstevel@tonic-gate 				continue;
8150Sstevel@tonic-gate 			case 'T':
8160Sstevel@tonic-gate 				io_ADJT++;
817251Slclee 				/* FALLTHRU */
8180Sstevel@tonic-gate 			case 't':
8190Sstevel@tonic-gate 				io_adjt++;
8200Sstevel@tonic-gate 				continue;
8210Sstevel@tonic-gate 			case 'B':
8220Sstevel@tonic-gate 				io_wholedisk++;
8230Sstevel@tonic-gate 				io_fdisk++;
8240Sstevel@tonic-gate 				continue;
8250Sstevel@tonic-gate 			case 'E':
8260Sstevel@tonic-gate 				io_EFIdisk++;
8270Sstevel@tonic-gate 				io_fdisk++;
8280Sstevel@tonic-gate 				continue;
8290Sstevel@tonic-gate 			case 'g':
8300Sstevel@tonic-gate 				diag_cnt++;
8310Sstevel@tonic-gate 				io_lgeom++;
8320Sstevel@tonic-gate 				continue;
8330Sstevel@tonic-gate 			case 'G':
8340Sstevel@tonic-gate 				diag_cnt++;
8350Sstevel@tonic-gate 				io_pgeom++;
8360Sstevel@tonic-gate 				continue;
8370Sstevel@tonic-gate 			case 'n':
8380Sstevel@tonic-gate 				io_nifdisk++;
8390Sstevel@tonic-gate 				io_fdisk++;
8400Sstevel@tonic-gate 				continue;
8410Sstevel@tonic-gate 			case 'F':
8420Sstevel@tonic-gate 				io_fdisk++;
8430Sstevel@tonic-gate 				io_ffdisk = optarg;
8440Sstevel@tonic-gate 				continue;
8450Sstevel@tonic-gate 			case 'b':
8460Sstevel@tonic-gate 				io_mboot = optarg;
8470Sstevel@tonic-gate 				continue;
8480Sstevel@tonic-gate 			case 'W':
8490Sstevel@tonic-gate 				/*
8500Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
8510Sstevel@tonic-gate 				 * to standard output, otherwise write
8520Sstevel@tonic-gate 				 * to the specified file.
8530Sstevel@tonic-gate 				 */
8540Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
8550Sstevel@tonic-gate 					stdo_flag = 1;
8560Sstevel@tonic-gate 				else
8570Sstevel@tonic-gate 					io_Wfdisk = optarg;
8580Sstevel@tonic-gate 				io_fdisk++;
8590Sstevel@tonic-gate 				continue;
8600Sstevel@tonic-gate 			case 'A':
8610Sstevel@tonic-gate 				io_fdisk++;
8620Sstevel@tonic-gate 				io_Afdisk = optarg;
8630Sstevel@tonic-gate 				continue;
8640Sstevel@tonic-gate 			case 'D':
8650Sstevel@tonic-gate 				io_fdisk++;
8660Sstevel@tonic-gate 				io_Dfdisk = optarg;
8670Sstevel@tonic-gate 				continue;
8680Sstevel@tonic-gate 			case 'h':
869251Slclee 				(void) fprintf(stderr, "%s\n", Usage);
870251Slclee 				(void) fprintf(stderr, "%s\n", Usage1);
8710Sstevel@tonic-gate 				exit(0);
872251Slclee 				/* FALLTHRU */
8730Sstevel@tonic-gate 			case 'v':
8740Sstevel@tonic-gate 				v_flag = 1;
8750Sstevel@tonic-gate 				continue;
8760Sstevel@tonic-gate 			case '?':
8770Sstevel@tonic-gate 				errflg++;
8780Sstevel@tonic-gate 				break;
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 		break;
8810Sstevel@tonic-gate 	}
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
8840Sstevel@tonic-gate 		diag_cnt = 0;
8850Sstevel@tonic-gate 	}
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	/* User option checking */
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/* By default, run in interactive mode */
8900Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
8910Sstevel@tonic-gate 		io_ifdisk++;
8920Sstevel@tonic-gate 		io_fdisk++;
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
8950Sstevel@tonic-gate 		errflg++;
8960Sstevel@tonic-gate 	}
8970Sstevel@tonic-gate 
8980Sstevel@tonic-gate 	/* Was any error detected? */
8990Sstevel@tonic-gate 	if (errflg || argc == optind) {
900251Slclee 		(void) fprintf(stderr, "%s\n", Usage);
901251Slclee 		(void) fprintf(stderr,
9020Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
9030Sstevel@tonic-gate 		exit(2);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	/* Figure out the correct device node to open */
9080Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (io_readonly)
9110Sstevel@tonic-gate 		openmode = O_RDONLY;
9120Sstevel@tonic-gate 	else
9130Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
916251Slclee 		(void) fprintf(stderr,
917251Slclee 		    "fdisk: Cannot open device %s.\n",
918251Slclee 		    Dfltdev);
9190Sstevel@tonic-gate 		exit(1);
9200Sstevel@tonic-gate 	}
9215169Slclee 	/*
9225169Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
9235169Slclee 	 * in that case leave the minfo structure zeroed
9245169Slclee 	 */
9255169Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
9269889SLarry.Liu@Sun.COM 		(void) memset(&minfo, 0, sizeof (minfo));
9275169Slclee 	}
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate 	/* Get the disk geometry */
9300Sstevel@tonic-gate 	if (!io_image) {
9310Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
9320Sstevel@tonic-gate 		errno = 0;
9330Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 			/*
9360Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
9370Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
9380Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
9390Sstevel@tonic-gate 			 */
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 			if (errno == ENOTTY) {
9420Sstevel@tonic-gate 				v_flag = 0;
9430Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
9440Sstevel@tonic-gate 			} else if (errno == EINVAL) {
9450Sstevel@tonic-gate 				/*
9460Sstevel@tonic-gate 				 * This means that the ioctl exists, but
9470Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
9480Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
9490Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
9500Sstevel@tonic-gate 				 */
9510Sstevel@tonic-gate 				v_flag = 0;
9520Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
9530Sstevel@tonic-gate 			} else {
9540Sstevel@tonic-gate 				(void) fprintf(stderr,
9550Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
9560Sstevel@tonic-gate 				    argv[optind]);
9570Sstevel@tonic-gate 				exit(1);
9580Sstevel@tonic-gate 			}
9590Sstevel@tonic-gate 		} else {
9600Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
9610Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
9620Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
9630Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
9640Sstevel@tonic-gate 		}
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 		errno = 0;
9670Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
9680Sstevel@tonic-gate 			if (errno == ENOTTY) {
9690Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
9700Sstevel@tonic-gate 			} else {
9710Sstevel@tonic-gate 				(void) fprintf(stderr,
9720Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
9730Sstevel@tonic-gate 				    argv[optind]);
9740Sstevel@tonic-gate 				exit(1);
9750Sstevel@tonic-gate 			}
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 		}
9780Sstevel@tonic-gate 		/*
9790Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
9800Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
9810Sstevel@tonic-gate 		 */
9820Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
9830Sstevel@tonic-gate 			errno = 0;
9840Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
9850Sstevel@tonic-gate 				(void) fprintf(stderr,
9860Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
9870Sstevel@tonic-gate 				    argv[optind]);
9880Sstevel@tonic-gate 				exit(1);
9890Sstevel@tonic-gate 			}
9900Sstevel@tonic-gate 		}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
9930Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
9940Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
9959889SLarry.Liu@Sun.COM 
9969889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
9979889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
9989889SLarry.Liu@Sun.COM 		else
9999889SLarry.Liu@Sun.COM 			sectsiz = 512;
10009889SLarry.Liu@Sun.COM 
10010Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 		/*
10040Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
10050Sstevel@tonic-gate 		 * or we got an invalid hba geometry
10060Sstevel@tonic-gate 		 * then set hba geometry based on max values
10070Sstevel@tonic-gate 		 */
10080Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
1009251Slclee 		    disk_geom.dkg_ncyl == 0 ||
1010251Slclee 		    disk_geom.dkg_nhead == 0 ||
1011251Slclee 		    disk_geom.dkg_nsect == 0 ||
10120Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
10130Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
10140Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate 			/*
10170Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
10180Sstevel@tonic-gate 			 */
10190Sstevel@tonic-gate 			v_flag = 0;
10200Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
10210Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
10220Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
10230Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 
10260Sstevel@tonic-gate 		if (io_debug) {
1027251Slclee 			(void) fprintf(stderr, "Physical Geometry:\n");
1028251Slclee 			(void) fprintf(stderr,
10290Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
10300Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
10310Sstevel@tonic-gate 			    Numcyl,
10320Sstevel@tonic-gate 			    heads,
10330Sstevel@tonic-gate 			    sectors,
10340Sstevel@tonic-gate 			    sectsiz,
1035251Slclee 			    Numcyl * heads * sectors,
1036251Slclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
1037251Slclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
1038251Slclee 			(void) fprintf(stderr,
10390Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
10400Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
10410Sstevel@tonic-gate 			    hba_Numcyl,
10420Sstevel@tonic-gate 			    hba_heads,
10430Sstevel@tonic-gate 			    hba_sectors,
10440Sstevel@tonic-gate 			    sectsiz,
1045251Slclee 			    hba_Numcyl * hba_heads * hba_sectors,
1046251Slclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
1047251Slclee 			    1048576);
10480Sstevel@tonic-gate 		}
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
10520Sstevel@tonic-gate 	if (io_lgeom) {
10530Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
10540Sstevel@tonic-gate 			(void) fprintf(stderr,
10550Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
10560Sstevel@tonic-gate 			    argv[optind]);
10570Sstevel@tonic-gate 			exit(1);
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
10600Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
10610Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
10629889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
10639889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
10649889SLarry.Liu@Sun.COM 		else
10659889SLarry.Liu@Sun.COM 			sectsiz = 512;
10669889SLarry.Liu@Sun.COM 
10670Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
1068251Slclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
1069251Slclee 		(void) printf(
1070251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
10710Sstevel@tonic-gate 		    " SECSIZ\n");
1072251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
10730Sstevel@tonic-gate 		    Numcyl,
10740Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
10750Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
10760Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
10770Sstevel@tonic-gate 		    heads,
10780Sstevel@tonic-gate 		    sectors,
10790Sstevel@tonic-gate 		    sectsiz);
10800Sstevel@tonic-gate 		exit(0);
10810Sstevel@tonic-gate 	} else if (io_pgeom) {
10820Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
10830Sstevel@tonic-gate 			(void) fprintf(stderr,
10840Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
10850Sstevel@tonic-gate 			    argv[optind]);
10860Sstevel@tonic-gate 			exit(1);
10870Sstevel@tonic-gate 		}
1088251Slclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
1089251Slclee 		(void) printf(
1090251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
10910Sstevel@tonic-gate 		    " SECSIZ\n");
1092251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
10930Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
10940Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
10950Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
10960Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
10970Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
10980Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
10990Sstevel@tonic-gate 		    sectsiz);
11000Sstevel@tonic-gate 		exit(0);
11010Sstevel@tonic-gate 	} else if (io_sgeom) {
11020Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
11030Sstevel@tonic-gate 			exit(1);
11040Sstevel@tonic-gate 		} else if (!io_image) {
11050Sstevel@tonic-gate 			exit(0);
11060Sstevel@tonic-gate 		}
11070Sstevel@tonic-gate 	}
11080Sstevel@tonic-gate 
11095169Slclee 	/*
11105169Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
11115169Slclee 	 * in that case use CHS
11125169Slclee 	 */
11137563SPrasad.Singamsetty@Sun.COM 	chs_capacity = (diskaddr_t)Numcyl * heads * sectors;
11145169Slclee 	dev_capacity = chs_capacity;
11157563SPrasad.Singamsetty@Sun.COM 	Numcyl_usable = Numcyl;
11167563SPrasad.Singamsetty@Sun.COM 
11177563SPrasad.Singamsetty@Sun.COM 	if (chs_capacity > DK_MAX_2TB) {
11187563SPrasad.Singamsetty@Sun.COM 		/* limit to 2TB */
11197563SPrasad.Singamsetty@Sun.COM 		Numcyl_usable = DK_MAX_2TB / (heads * sectors);
11207563SPrasad.Singamsetty@Sun.COM 		chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors;
11217563SPrasad.Singamsetty@Sun.COM 	}
11227563SPrasad.Singamsetty@Sun.COM 
11235169Slclee 	if (minfo.dki_capacity > 0)
11245169Slclee 		dev_capacity = minfo.dki_capacity;
11255169Slclee 
11260Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
11279889SLarry.Liu@Sun.COM 	Bootsect = (char *)calloc(3 * sectsiz, 1);
11280Sstevel@tonic-gate 	if (Bootsect == NULL) {
1129251Slclee 		(void) fprintf(stderr,
11300Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
11310Sstevel@tonic-gate 		    " (%d bytes).\n",
1132251Slclee 		    3 * sectsiz);
11330Sstevel@tonic-gate 		exit(1);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
11370Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
11380Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
11390Sstevel@tonic-gate 		Nullsect[i] = 0;
11400Sstevel@tonic-gate 	}
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate 	/* Find out what the user wants done */
11430Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
11440Sstevel@tonic-gate 		abs_read();	/* will not return */
11450Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
11460Sstevel@tonic-gate 		abs_write();	/* will not return */
11470Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
11480Sstevel@tonic-gate 		fill_patt();	/* will not return */
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
11570Sstevel@tonic-gate 	mboot_read();
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	/* Read from disk master boot */
11600Sstevel@tonic-gate 	dev_mboot_read();
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/*
11630Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
11640Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
11650Sstevel@tonic-gate 	 */
11660Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
11670Sstevel@tonic-gate 	copy_Bootblk_to_Table();
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
11700Sstevel@tonic-gate 	copy_Table_to_Old_Table();
11710Sstevel@tonic-gate 
117210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
117310021SSheshadri.Vasudevan@Sun.COM 	/*
117410021SSheshadri.Vasudevan@Sun.COM 	 * Read extended partition only when the fdisk table is not
117510021SSheshadri.Vasudevan@Sun.COM 	 * supplied from a file
117610021SSheshadri.Vasudevan@Sun.COM 	 */
117710021SSheshadri.Vasudevan@Sun.COM 	if (!io_ffdisk) {
117810021SSheshadri.Vasudevan@Sun.COM 		lf_op_flag |= FDISK_READ_DISK;
117910021SSheshadri.Vasudevan@Sun.COM 	}
118010021SSheshadri.Vasudevan@Sun.COM 	if ((rval = libfdisk_init(&epp, Dfltdev, &Table[0], lf_op_flag))
118110021SSheshadri.Vasudevan@Sun.COM 	    != FDISK_SUCCESS) {
118210021SSheshadri.Vasudevan@Sun.COM 		switch (rval) {
118310021SSheshadri.Vasudevan@Sun.COM 			/*
118410021SSheshadri.Vasudevan@Sun.COM 			 * FDISK_EBADLOGDRIVE and FDISK_ENOLOGDRIVE can
118510021SSheshadri.Vasudevan@Sun.COM 			 * be considered as soft errors and hence
118610021SSheshadri.Vasudevan@Sun.COM 			 * we do not exit
118710021SSheshadri.Vasudevan@Sun.COM 			 */
118810021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EBADLOGDRIVE:
118910021SSheshadri.Vasudevan@Sun.COM 				break;
119010021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLOGDRIVE:
119110021SSheshadri.Vasudevan@Sun.COM 				break;
119210021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOVGEOM:
119310021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get virtual"
119410021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
119510021SSheshadri.Vasudevan@Sun.COM 				exit(1);
119610021SSheshadri.Vasudevan@Sun.COM 				break;
119710021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOPGEOM:
119810021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get physical"
119910021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
120010021SSheshadri.Vasudevan@Sun.COM 				exit(1);
120110021SSheshadri.Vasudevan@Sun.COM 				break;
120210021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLGEOM:
120310021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get label"
120410021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
120510021SSheshadri.Vasudevan@Sun.COM 				exit(1);
120610021SSheshadri.Vasudevan@Sun.COM 				break;
120710021SSheshadri.Vasudevan@Sun.COM 			default:
120810021SSheshadri.Vasudevan@Sun.COM 				perror("Failed to initialise libfdisk.\n");
120910021SSheshadri.Vasudevan@Sun.COM 				exit(1);
121010021SSheshadri.Vasudevan@Sun.COM 				break;
121110021SSheshadri.Vasudevan@Sun.COM 		}
121210021SSheshadri.Vasudevan@Sun.COM 	}
121310021SSheshadri.Vasudevan@Sun.COM #endif
121410021SSheshadri.Vasudevan@Sun.COM 
12150Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
12160Sstevel@tonic-gate 	if (io_ffdisk) {
12170Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
12180Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
12190Sstevel@tonic-gate 	}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
12220Sstevel@tonic-gate 	if (io_Dfdisk) {
12230Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 	if (io_Afdisk) {
12260Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
12270Sstevel@tonic-gate 	}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
12300Sstevel@tonic-gate 		/* Check if there is no fdisk table */
12310Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
12320Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
1233251Slclee 				(void) printf(
1234251Slclee 				    "No fdisk table exists. The default"
1235251Slclee 				    " partition for the disk is:\n\n"
1236251Slclee 				    "  a 100%% \"SOLARIS System\" "
1237251Slclee 				    "partition\n\n"
1238251Slclee 				    "Type \"y\" to accept the default "
12390Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
12400Sstevel@tonic-gate 				    "edit the\n partition table.\n");
12417563SPrasad.Singamsetty@Sun.COM 
12427563SPrasad.Singamsetty@Sun.COM 				if (Numcyl > Numcyl_usable)
12437563SPrasad.Singamsetty@Sun.COM 					(void) printf("WARNING: Disk is larger"
12447563SPrasad.Singamsetty@Sun.COM 					    " than 2TB. Solaris partition will"
12457563SPrasad.Singamsetty@Sun.COM 					    " be limited to 2 TB.\n");
12460Sstevel@tonic-gate 			}
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 			/* Edit the partition table as directed */
12490Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 				/* Default scenario */
12520Sstevel@tonic-gate 				nulltbl();
12530Sstevel@tonic-gate 				/* now set up UNIX System partition */
12540Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
125510021SSheshadri.Vasudevan@Sun.COM 				Table[0].relsect = LE_32(heads * sectors);
12567563SPrasad.Singamsetty@Sun.COM 
12577563SPrasad.Singamsetty@Sun.COM 				Table[0].numsect =
125810021SSheshadri.Vasudevan@Sun.COM 				    LE_32((ulong_t)((Numcyl_usable - 1) *
12590Sstevel@tonic-gate 				    heads * sectors));
12607563SPrasad.Singamsetty@Sun.COM 
12610Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
12640Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
12650Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12660Sstevel@tonic-gate 			} else if (io_EFIdisk) {
12670Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
12680Sstevel@tonic-gate 				nulltbl();
12690Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
12707563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
12717563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity - 1));
12720Sstevel@tonic-gate 				if (i != 0) {
1273251Slclee 					(void) fprintf(stderr,
1274251Slclee 					    "Error creating EFI partition\n");
12750Sstevel@tonic-gate 					exit(1);
12760Sstevel@tonic-gate 				}
12770Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12780Sstevel@tonic-gate 			}
12790Sstevel@tonic-gate 		}
12800Sstevel@tonic-gate 	}
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
12830Sstevel@tonic-gate 	if (io_debug) {
1284251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
12850Sstevel@tonic-gate 		print_Table();
12860Sstevel@tonic-gate 		if (io_ifdisk) {
1287251Slclee 			(void) fprintf(stderr, "\n");
1288251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
12899786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 	}
12920Sstevel@tonic-gate 
12930Sstevel@tonic-gate 	/* Interactive fdisk mode */
12940Sstevel@tonic-gate 	if (io_ifdisk) {
1295251Slclee 		(void) printf(CLR_SCR);
12960Sstevel@tonic-gate 		disptbl();
1297251Slclee 		for (;;) {
1298251Slclee 			stage0();
12990Sstevel@tonic-gate 			copy_Bootblk_to_Table();
13000Sstevel@tonic-gate 			disptbl();
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 	}
13030Sstevel@tonic-gate 
13040Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
13050Sstevel@tonic-gate 	if (io_Wfdisk)
13060Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
13070Sstevel@tonic-gate 	else if (stdo_flag)
13080Sstevel@tonic-gate 		ffile_write((char *)stdout);
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
1311251Slclee 	return (0);
13120Sstevel@tonic-gate }
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate  * read_geom
13160Sstevel@tonic-gate  * Read geometry from specified file (-S).
13170Sstevel@tonic-gate  */
13180Sstevel@tonic-gate 
1319251Slclee static int
1320251Slclee read_geom(char *sgeom)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate 	char	line[256];
13230Sstevel@tonic-gate 	FILE *fp;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	/* open the prototype file */
13260Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
13270Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
13280Sstevel@tonic-gate 		    io_sgeom);
13290Sstevel@tonic-gate 		return (1);
13300Sstevel@tonic-gate 	}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 	/* Read a line from the file */
13330Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
13340Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
13350Sstevel@tonic-gate 			continue;
13360Sstevel@tonic-gate 		else {
13370Sstevel@tonic-gate 			line[strlen(line)] = '\0';
1338251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
13390Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
13400Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
13410Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
13420Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
13430Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
13440Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
13450Sstevel@tonic-gate 			    &sectsiz) != 7) {
13460Sstevel@tonic-gate 				(void) fprintf(stderr,
13470Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
13480Sstevel@tonic-gate 				    line);
13490Sstevel@tonic-gate 				return (1);
13500Sstevel@tonic-gate 			}
13510Sstevel@tonic-gate 			break;
13520Sstevel@tonic-gate 		} /* else */
13530Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 	if (!io_image) {
13560Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
13570Sstevel@tonic-gate 			(void) fprintf(stderr,
13580Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
13590Sstevel@tonic-gate 			return (1);
13600Sstevel@tonic-gate 		}
13610Sstevel@tonic-gate 	} else {
13620Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
13630Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
13640Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
13650Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
13660Sstevel@tonic-gate 	}
13670Sstevel@tonic-gate 
1368251Slclee 	(void) fclose(fp);
13690Sstevel@tonic-gate 	return (0);
13700Sstevel@tonic-gate }
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate /*
13730Sstevel@tonic-gate  * dev_mboot_read
13740Sstevel@tonic-gate  * Read the master boot sector from the device.
13750Sstevel@tonic-gate  */
1376251Slclee static void
1377251Slclee dev_mboot_read(void)
13780Sstevel@tonic-gate {
13790Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
13800Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
13810Sstevel@tonic-gate 	}
13820Sstevel@tonic-gate 	if (errno == 0)
13830Sstevel@tonic-gate 		return;
13840Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
1385251Slclee 		(void) fprintf(stderr,
13860Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
13870Sstevel@tonic-gate 		    Dfltdev);
13880Sstevel@tonic-gate 		if (!io_image)
13890Sstevel@tonic-gate 			exit(1);
13900Sstevel@tonic-gate 	}
13910Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1392251Slclee 		(void) fprintf(stderr,
13930Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
13940Sstevel@tonic-gate 		    Dfltdev);
13950Sstevel@tonic-gate 		if (!io_image)
13960Sstevel@tonic-gate 			exit(1);
13970Sstevel@tonic-gate 	}
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate /*
14010Sstevel@tonic-gate  * dev_mboot_write
14020Sstevel@tonic-gate  * Write the master boot sector to the device.
14030Sstevel@tonic-gate  */
1404251Slclee static void
14056549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
14060Sstevel@tonic-gate {
14070Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
14086478Sbharding 	int	clr_efi = -1;
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	if (io_readonly)
1411251Slclee 		return;
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 	if (io_debug) {
1414251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
14150Sstevel@tonic-gate 		print_Table();
14160Sstevel@tonic-gate 		if (io_ifdisk) {
1417251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
14189786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
14190Sstevel@tonic-gate 		}
14200Sstevel@tonic-gate 	}
14210Sstevel@tonic-gate 
14226478Sbharding 	/*
14236478Sbharding 	 * If the new table has any Solaris partitions and the old
14246478Sbharding 	 * table does not have an entry that describes it
14256478Sbharding 	 * exactly then clear the old vtoc (if any).
14266478Sbharding 	 */
14270Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14280Sstevel@tonic-gate 
14296478Sbharding 		/* We only care about potential Solaris parts. */
14300Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
14310Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
14320Sstevel@tonic-gate 			continue;
14330Sstevel@tonic-gate 
14346478Sbharding 		/* Does the old table have an exact entry for the new entry? */
14356478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14366478Sbharding 
14376478Sbharding 			/* We only care about old Solaris partitions. */
14386478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
14396478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
14406478Sbharding 
14416478Sbharding 				/* Is this old one the same as a new one? */
14426478Sbharding 				if ((Old_Table[old_pt].relsect ==
14436478Sbharding 				    Table[new_pt].relsect) &&
14446478Sbharding 				    (Old_Table[old_pt].numsect ==
14456478Sbharding 				    Table[new_pt].numsect))
14466478Sbharding 					break; /* Yes */
14476478Sbharding 			}
14486478Sbharding 		}
14496478Sbharding 
14506478Sbharding 		/* Did a solaris partition change location or size? */
14516478Sbharding 		if (old_pt >= FD_NUMPART) {
14526478Sbharding 			/* Yes clear old vtoc */
14536478Sbharding 			if (io_debug) {
14546478Sbharding 				(void) fprintf(stderr,
14556478Sbharding 				    "Clearing VTOC labels from NEW"
14566478Sbharding 				    " table\n");
14576478Sbharding 			}
14586478Sbharding 			clear_vtoc(NEW, new_pt);
14596478Sbharding 		}
14606478Sbharding 	}
14616478Sbharding 
14626478Sbharding 
14636478Sbharding 	/* see if the old table had EFI */
14646478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14656478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
14666478Sbharding 			clr_efi = old_pt;
14670Sstevel@tonic-gate 		}
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
14710Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14720Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
14730Sstevel@tonic-gate 			continue;
14740Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14755169Slclee 			if ((Old_Table[old_pt].systid ==
14765169Slclee 			    Table[new_pt].systid) &&
14775169Slclee 			    (Old_Table[old_pt].relsect ==
14785169Slclee 			    Table[new_pt].relsect) &&
14795169Slclee 			    (Old_Table[old_pt].numsect ==
14805169Slclee 			    Table[new_pt].numsect))
14815169Slclee 				break;
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		/*
14850Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
14860Sstevel@tonic-gate 		 * the EFI GPT
14870Sstevel@tonic-gate 		 */
14880Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
14890Sstevel@tonic-gate 			clr_efi = 0;
14900Sstevel@tonic-gate 		}
14910Sstevel@tonic-gate 		break;
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	/* clear labels if necessary */
14950Sstevel@tonic-gate 	if (clr_efi >= 0) {
14960Sstevel@tonic-gate 		if (io_debug) {
1497251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
14980Sstevel@tonic-gate 		}
14990Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
15000Sstevel@tonic-gate 			if (io_debug) {
1501251Slclee 				(void) fprintf(stderr,
1502251Slclee 				    "\tError %d clearing EFI labels"
15030Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
15040Sstevel@tonic-gate 				    error);
15050Sstevel@tonic-gate 			}
15060Sstevel@tonic-gate 		}
15070Sstevel@tonic-gate 	}
15080Sstevel@tonic-gate 
15090Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1510251Slclee 		(void) fprintf(stderr,
15110Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
15120Sstevel@tonic-gate 		    Dfltdev);
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 	if (errno == 0)
15150Sstevel@tonic-gate 		return;
15160Sstevel@tonic-gate 
15170Sstevel@tonic-gate 	/* write to disk drive */
15180Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1519251Slclee 		(void) fprintf(stderr,
15200Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
15210Sstevel@tonic-gate 		    Dfltdev);
15220Sstevel@tonic-gate 		exit(1);
15230Sstevel@tonic-gate 	}
15240Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1525251Slclee 		(void) fprintf(stderr,
15260Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
15270Sstevel@tonic-gate 		    Dfltdev);
15280Sstevel@tonic-gate 		exit(1);
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate }
15310Sstevel@tonic-gate 
15320Sstevel@tonic-gate /*
15330Sstevel@tonic-gate  * mboot_read
15340Sstevel@tonic-gate  * Read the prototype boot records from the files.
15350Sstevel@tonic-gate  */
1536251Slclee static void
1537251Slclee mboot_read(void)
15380Sstevel@tonic-gate {
15390Sstevel@tonic-gate 	int mDev, i;
15400Sstevel@tonic-gate 	struct ipart *part;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate #if defined(i386) || defined(sparc)
15430Sstevel@tonic-gate 	/*
15440Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
15450Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
15460Sstevel@tonic-gate 	 */
15470Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
15480Sstevel@tonic-gate 		/*
15490Sstevel@tonic-gate 		 * Bug ID 1249035:
15500Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
15510Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
15520Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
15530Sstevel@tonic-gate 		 */
15540Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
15550Sstevel@tonic-gate 	}
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	/* First read in the master boot record */
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 	/* Open the master boot proto file */
15600Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1561251Slclee 		(void) fprintf(stderr,
15620Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
15630Sstevel@tonic-gate 		    io_mboot);
15640Sstevel@tonic-gate 		exit(1);
15650Sstevel@tonic-gate 	}
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	/* Read the master boot program */
15680Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
15690Sstevel@tonic-gate 	    (struct mboot)) {
1570251Slclee 		(void) fprintf(stderr,
15710Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
15720Sstevel@tonic-gate 		    io_mboot);
15730Sstevel@tonic-gate 		exit(1);
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	/* Is this really a master boot record? */
157710021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(BootCod.signature) != MBB_MAGIC) {
1578251Slclee 		(void) fprintf(stderr,
15790Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1580251Slclee 		(void) fprintf(stderr,
1581251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
158210021SSheshadri.Vasudevan@Sun.COM 		    LE_16(BootCod.signature), MBB_MAGIC);
15830Sstevel@tonic-gate 		exit(1);
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 
1586251Slclee 	(void) close(mDev);
15870Sstevel@tonic-gate #else
15880Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
15890Sstevel@tonic-gate #endif
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
15920Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
15930Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1594251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
15950Sstevel@tonic-gate 	}
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate }
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate /*
16000Sstevel@tonic-gate  * fill_patt
16010Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
16020Sstevel@tonic-gate  */
1603251Slclee static void
1604251Slclee fill_patt(void)
16050Sstevel@tonic-gate {
1606251Slclee 	int	*buff_ptr, i;
16076549Sbharding 	off_t	*off_ptr;
16080Sstevel@tonic-gate 	int	io_fpatt = 0;
16090Sstevel@tonic-gate 	int	io_ipatt = 0;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
16120Sstevel@tonic-gate 		io_fpatt++;
16130Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
16140Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
16150Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
16165169Slclee 			*buff_ptr = io_ipatt;
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	/*
16200Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
16210Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
16220Sstevel@tonic-gate 	 * for io_size blocks.
16230Sstevel@tonic-gate 	 */
16240Sstevel@tonic-gate 	while (io_size--) {
16256549Sbharding 		off_ptr = (off_t *)Bootsect;
16260Sstevel@tonic-gate 		if (!io_fpatt) {
16276549Sbharding 			for (i = 0; i < sectsiz;
16286549Sbharding 			    i += sizeof (off_t), off_ptr++)
16296549Sbharding 				*off_ptr = io_offset;
16300Sstevel@tonic-gate 		}
16310Sstevel@tonic-gate 		/* Write the data to disk */
16326549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16336549Sbharding 		    SEEK_SET) == -1) {
1634251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16355169Slclee 			    Dfltdev);
16360Sstevel@tonic-gate 			exit(1);
16370Sstevel@tonic-gate 		}
16380Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1639251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
16405169Slclee 			    Dfltdev);
16410Sstevel@tonic-gate 			exit(1);
16420Sstevel@tonic-gate 		}
16430Sstevel@tonic-gate 	} /* while (--io_size); */
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate  * abs_read
16480Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
16490Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
16500Sstevel@tonic-gate  */
1651251Slclee static void
1652251Slclee abs_read(void)
1653251Slclee {
16540Sstevel@tonic-gate 	int c;
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate 	while (io_size--) {
16576549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16586549Sbharding 		    SEEK_SET) == -1) {
1659251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16600Sstevel@tonic-gate 			    Dfltdev);
16610Sstevel@tonic-gate 			exit(1);
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1664251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
16650Sstevel@tonic-gate 			    Dfltdev);
16660Sstevel@tonic-gate 			exit(1);
16670Sstevel@tonic-gate 		}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 		/* Write to standard ouptut */
1670251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
16710Sstevel@tonic-gate 			if (c >= 0) {
16720Sstevel@tonic-gate 				if (io_debug)
1673251Slclee 					(void) fprintf(stderr,
1674251Slclee 					    "fdisk: Output warning: %d of %d"
1675251Slclee 					    " characters written.\n",
1676251Slclee 					    c, sectsiz);
16770Sstevel@tonic-gate 				exit(2);
16780Sstevel@tonic-gate 			} else {
16790Sstevel@tonic-gate 				perror("write error on output file.");
16800Sstevel@tonic-gate 				exit(2);
16810Sstevel@tonic-gate 			}
16820Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
16830Sstevel@tonic-gate 			/* != sectsiz) */
16840Sstevel@tonic-gate 	} /* while (--io_size); */
16850Sstevel@tonic-gate 	exit(0);
16860Sstevel@tonic-gate }
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate /*
16890Sstevel@tonic-gate  * abs_write
16900Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
16910Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
16920Sstevel@tonic-gate  */
1693251Slclee static void
1694251Slclee abs_write(void)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate 	int c, i;
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate 	while (io_size--) {
16990Sstevel@tonic-gate 		int part_exit = 0;
17000Sstevel@tonic-gate 		/* Read from standard input */
17010Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
17020Sstevel@tonic-gate 			if (c >= 0) {
17030Sstevel@tonic-gate 				if (io_debug)
1704251Slclee 				(void) fprintf(stderr,
17050Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
17060Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
17075169Slclee 				    c, sectsiz);
17080Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
17090Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
17100Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
17110Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
17120Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
17130Sstevel@tonic-gate 					Bootsect[i++] = 0;
17140Sstevel@tonic-gate 				}
17150Sstevel@tonic-gate 				part_exit++;
17160Sstevel@tonic-gate 			} else {
17170Sstevel@tonic-gate 				perror("read error on input file.");
17180Sstevel@tonic-gate 				exit(2);
17190Sstevel@tonic-gate 			}
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 		}
17220Sstevel@tonic-gate 		/* Write to disk drive */
17236549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
17246549Sbharding 		    SEEK_SET) == -1) {
1725251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
17260Sstevel@tonic-gate 			    Dfltdev);
17270Sstevel@tonic-gate 			exit(1);
17280Sstevel@tonic-gate 		}
17290Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1730251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
17310Sstevel@tonic-gate 			    Dfltdev);
17320Sstevel@tonic-gate 			exit(1);
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 		if (part_exit)
17350Sstevel@tonic-gate 		exit(0);
17360Sstevel@tonic-gate 	} /* while (--io_size); */
17370Sstevel@tonic-gate 	exit(1);
17380Sstevel@tonic-gate }
17390Sstevel@tonic-gate 
1740251Slclee 
17410Sstevel@tonic-gate /*
17420Sstevel@tonic-gate  * load
17430Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
17440Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
17450Sstevel@tonic-gate  */
17460Sstevel@tonic-gate 
1747251Slclee static void
1748251Slclee load(int funct, char *file)
17490Sstevel@tonic-gate {
17500Sstevel@tonic-gate 	int	id;
17510Sstevel@tonic-gate 	int	act;
17520Sstevel@tonic-gate 	int	bhead;
17530Sstevel@tonic-gate 	int	bsect;
17540Sstevel@tonic-gate 	int	bcyl;
17550Sstevel@tonic-gate 	int	ehead;
17560Sstevel@tonic-gate 	int	esect;
17570Sstevel@tonic-gate 	int	ecyl;
17587563SPrasad.Singamsetty@Sun.COM 	uint32_t	rsect;
17597563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
17600Sstevel@tonic-gate 	char	line[256];
17610Sstevel@tonic-gate 	int	i = 0;
17620Sstevel@tonic-gate 	int	j;
17630Sstevel@tonic-gate 	FILE *fp;
176410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
176510021SSheshadri.Vasudevan@Sun.COM 	int 	ext_part_present = 0;
176610021SSheshadri.Vasudevan@Sun.COM 	uint32_t	begsec, endsec, relsect;
176710021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
176810021SSheshadri.Vasudevan@Sun.COM 	int part_count = 0, ldcnt = 0;
176910021SSheshadri.Vasudevan@Sun.COM 	uint32_t ext_beg_sec, ext_end_sec;
177010021SSheshadri.Vasudevan@Sun.COM 	uint32_t old_ext_beg_sec = 0, old_ext_num_sec = 0;
177110021SSheshadri.Vasudevan@Sun.COM 	uint32_t new_ext_beg_sec = 0, new_ext_num_sec = 0;
177210021SSheshadri.Vasudevan@Sun.COM 	int ext_part_inited = 0;
177310021SSheshadri.Vasudevan@Sun.COM 	uchar_t	systid;
177410021SSheshadri.Vasudevan@Sun.COM #endif
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	switch (funct) {
17770Sstevel@tonic-gate 
17785169Slclee 	case LOADFILE:
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 		/*
17810Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
17820Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
17830Sstevel@tonic-gate 		 * fdisk_file).
17840Sstevel@tonic-gate 		 */
17850Sstevel@tonic-gate 		nulltbl();
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 		/* Open the prototype file */
17880Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
17890Sstevel@tonic-gate 			(void) fprintf(stderr,
17900Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
17910Sstevel@tonic-gate 			    file);
17920Sstevel@tonic-gate 			exit(1);
17930Sstevel@tonic-gate 		}
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		/* Read a line from the file */
17960Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
17970Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
17980Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
17990Sstevel@tonic-gate 				continue;
18000Sstevel@tonic-gate 			}
180110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
180210021SSheshadri.Vasudevan@Sun.COM 			part_count++;
180310021SSheshadri.Vasudevan@Sun.COM 
180410021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended((uchar_t)id)) {
180510021SSheshadri.Vasudevan@Sun.COM 				if (ext_part_present) {
180610021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Extended partition"
180710021SSheshadri.Vasudevan@Sun.COM 					    " already exists\n");
180810021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
180910021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
181010021SSheshadri.Vasudevan@Sun.COM 					exit(1);
181110021SSheshadri.Vasudevan@Sun.COM 				}
181210021SSheshadri.Vasudevan@Sun.COM 				ext_part_present = 1;
181310021SSheshadri.Vasudevan@Sun.COM 				/*
181410021SSheshadri.Vasudevan@Sun.COM 				 * If the existing extended partition's start
181510021SSheshadri.Vasudevan@Sun.COM 				 * and size matches the new one, do not
181610021SSheshadri.Vasudevan@Sun.COM 				 * initialize the extended partition EBR
181710021SSheshadri.Vasudevan@Sun.COM 				 * (Extended Boot Record) because there could
181810021SSheshadri.Vasudevan@Sun.COM 				 * be existing logical drives.
181910021SSheshadri.Vasudevan@Sun.COM 				 */
182010021SSheshadri.Vasudevan@Sun.COM 				for (i = 0; i < FD_NUMPART; i++) {
182110021SSheshadri.Vasudevan@Sun.COM 					systid = Old_Table[i].systid;
182210021SSheshadri.Vasudevan@Sun.COM 					if (fdisk_is_dos_extended(systid)) {
182310021SSheshadri.Vasudevan@Sun.COM 						old_ext_beg_sec =
182410021SSheshadri.Vasudevan@Sun.COM 						    Old_Table[i].relsect;
182510021SSheshadri.Vasudevan@Sun.COM 						old_ext_num_sec =
182610021SSheshadri.Vasudevan@Sun.COM 						    Old_Table[i].numsect;
182710021SSheshadri.Vasudevan@Sun.COM 						break;
182810021SSheshadri.Vasudevan@Sun.COM 					}
182910021SSheshadri.Vasudevan@Sun.COM 				}
183010021SSheshadri.Vasudevan@Sun.COM 				new_ext_beg_sec = rsect;
183110021SSheshadri.Vasudevan@Sun.COM 				new_ext_num_sec = numsect;
183210021SSheshadri.Vasudevan@Sun.COM 				if ((old_ext_beg_sec != new_ext_beg_sec) ||
183310021SSheshadri.Vasudevan@Sun.COM 				    (old_ext_num_sec != new_ext_num_sec)) {
183410021SSheshadri.Vasudevan@Sun.COM 					fdisk_init_ext_part(epp,
183510021SSheshadri.Vasudevan@Sun.COM 					    new_ext_beg_sec, new_ext_num_sec);
183610021SSheshadri.Vasudevan@Sun.COM 					ext_part_inited = 1;
183710021SSheshadri.Vasudevan@Sun.COM 				}
183810021SSheshadri.Vasudevan@Sun.COM 			}
183910021SSheshadri.Vasudevan@Sun.COM 
184010021SSheshadri.Vasudevan@Sun.COM 			if (part_count > FD_NUMPART) {
184110021SSheshadri.Vasudevan@Sun.COM 				/* This line should be logical drive info */
184210021SSheshadri.Vasudevan@Sun.COM 				int offset = MAX_LOGDRIVE_OFFSET;
184310021SSheshadri.Vasudevan@Sun.COM 				if (!ext_part_present) {
184410021SSheshadri.Vasudevan@Sun.COM 					/* Erroneous input file */
184510021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "More than 4 primary"
184610021SSheshadri.Vasudevan@Sun.COM 					    " partitions found in input\n");
184710021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Exiting...\n");
184810021SSheshadri.Vasudevan@Sun.COM 					exit(1);
184910021SSheshadri.Vasudevan@Sun.COM 				}
185010021SSheshadri.Vasudevan@Sun.COM 
185110021SSheshadri.Vasudevan@Sun.COM 				if (numsect == 0) {
185210021SSheshadri.Vasudevan@Sun.COM 					continue;
185310021SSheshadri.Vasudevan@Sun.COM 				}
185410021SSheshadri.Vasudevan@Sun.COM 
185510021SSheshadri.Vasudevan@Sun.COM 				/*
185610021SSheshadri.Vasudevan@Sun.COM 				 * If the start and size of the existing
185710021SSheshadri.Vasudevan@Sun.COM 				 * extended partition matches the new one and
185810021SSheshadri.Vasudevan@Sun.COM 				 * new logical drives are being defined via
185910021SSheshadri.Vasudevan@Sun.COM 				 * the input file, initialize the EBR.
186010021SSheshadri.Vasudevan@Sun.COM 				 */
186110021SSheshadri.Vasudevan@Sun.COM 				if (!ext_part_inited) {
186210021SSheshadri.Vasudevan@Sun.COM 					fdisk_init_ext_part(epp,
186310021SSheshadri.Vasudevan@Sun.COM 					    new_ext_beg_sec, new_ext_num_sec);
186410021SSheshadri.Vasudevan@Sun.COM 					ext_part_inited = 1;
186510021SSheshadri.Vasudevan@Sun.COM 				}
186610021SSheshadri.Vasudevan@Sun.COM 
186710021SSheshadri.Vasudevan@Sun.COM 				begsec = rsect - offset;
186810021SSheshadri.Vasudevan@Sun.COM 				if ((ldcnt =
186910021SSheshadri.Vasudevan@Sun.COM 				    fdisk_get_logical_drive_count(epp)) == 0) {
187010021SSheshadri.Vasudevan@Sun.COM 					/* Adding the first logical drive */
187110021SSheshadri.Vasudevan@Sun.COM 					/*
187210021SSheshadri.Vasudevan@Sun.COM 					 * Make sure that begsec doesnt wrap
187310021SSheshadri.Vasudevan@Sun.COM 					 * around. This can happen if rsect is
187410021SSheshadri.Vasudevan@Sun.COM 					 * less than offset.
187510021SSheshadri.Vasudevan@Sun.COM 					 */
187610021SSheshadri.Vasudevan@Sun.COM 					if (rsect < offset) {
187710021SSheshadri.Vasudevan@Sun.COM 						fprintf(stderr, "Minimum of "
187810021SSheshadri.Vasudevan@Sun.COM 						    "63 free sectors required "
187910021SSheshadri.Vasudevan@Sun.COM 						    "before the beginning of "
188010021SSheshadri.Vasudevan@Sun.COM 						    "a logical drive.");
188110021SSheshadri.Vasudevan@Sun.COM 						exit(1);
188210021SSheshadri.Vasudevan@Sun.COM 					}
188310021SSheshadri.Vasudevan@Sun.COM 					/*
188410021SSheshadri.Vasudevan@Sun.COM 					 * Check if the first logical drive
188510021SSheshadri.Vasudevan@Sun.COM 					 * is out of order. In that case, do
188610021SSheshadri.Vasudevan@Sun.COM 					 * not subtract MAX_LOGDRIVE_OFFSET
188710021SSheshadri.Vasudevan@Sun.COM 					 * from the given start of partition.
188810021SSheshadri.Vasudevan@Sun.COM 					 */
188910021SSheshadri.Vasudevan@Sun.COM 					if (begsec != new_ext_beg_sec) {
189010021SSheshadri.Vasudevan@Sun.COM 						begsec = rsect;
189110021SSheshadri.Vasudevan@Sun.COM 						offset = 0;
189210021SSheshadri.Vasudevan@Sun.COM 					}
189310021SSheshadri.Vasudevan@Sun.COM 				}
189410021SSheshadri.Vasudevan@Sun.COM 				if (ldcnt >= MAX_EXT_PARTS) {
189510021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "\nError : Number of "
189610021SSheshadri.Vasudevan@Sun.COM 					    "logical drives exceeds limit of "
189710021SSheshadri.Vasudevan@Sun.COM 					    "%d.\n", MAX_EXT_PARTS);
189810021SSheshadri.Vasudevan@Sun.COM 					exit(1);
189910021SSheshadri.Vasudevan@Sun.COM 				}
190010021SSheshadri.Vasudevan@Sun.COM 
190110021SSheshadri.Vasudevan@Sun.COM 				if (id > FDISK_MAX_VALID_PART_ID) {
190210021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Invalid partition "
190310021SSheshadri.Vasudevan@Sun.COM 					    "ID\n");
190410021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
190510021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
190610021SSheshadri.Vasudevan@Sun.COM 					exit(1);
190710021SSheshadri.Vasudevan@Sun.COM 				}
190810021SSheshadri.Vasudevan@Sun.COM 
190910021SSheshadri.Vasudevan@Sun.COM 				endsec = rsect + numsect - 1;
191010021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_validate_logical_drive(epp,
191110021SSheshadri.Vasudevan@Sun.COM 				    begsec, offset, numsect) == 0) {
191210021SSheshadri.Vasudevan@Sun.COM 					if (id == EFI_PMBR) {
191310021SSheshadri.Vasudevan@Sun.COM 						fprintf(stderr, "EFI "
191410021SSheshadri.Vasudevan@Sun.COM 						    "partitions not supported "
191510021SSheshadri.Vasudevan@Sun.COM 						    "inside extended "
191610021SSheshadri.Vasudevan@Sun.COM 						    "partition\n");
191710021SSheshadri.Vasudevan@Sun.COM 						exit(1);
191810021SSheshadri.Vasudevan@Sun.COM 					}
191910021SSheshadri.Vasudevan@Sun.COM 					fdisk_add_logical_drive(epp, begsec,
192010021SSheshadri.Vasudevan@Sun.COM 					    endsec, id);
192110021SSheshadri.Vasudevan@Sun.COM 					continue;
192210021SSheshadri.Vasudevan@Sun.COM 				} else {
192310021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
192410021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
192510021SSheshadri.Vasudevan@Sun.COM 					exit(1);
192610021SSheshadri.Vasudevan@Sun.COM 				}
192710021SSheshadri.Vasudevan@Sun.COM 			}
192810021SSheshadri.Vasudevan@Sun.COM #endif
19290Sstevel@tonic-gate 
19300Sstevel@tonic-gate 			/*
19310Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
19320Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
19330Sstevel@tonic-gate 			 */
19340Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
19350Sstevel@tonic-gate 				(void) fprintf(stderr,
19360Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
19370Sstevel@tonic-gate 				    line);
19380Sstevel@tonic-gate 				exit(1);
19390Sstevel@tonic-gate 			}
19408904SBarry.Harding@Sun.COM 
19418904SBarry.Harding@Sun.COM 			if (entry_from_old_table(id, act, bhead, bsect,
19428904SBarry.Harding@Sun.COM 			    bcyl, ehead, esect, ecyl, rsect, numsect)) {
19438904SBarry.Harding@Sun.COM 				/*
19448904SBarry.Harding@Sun.COM 				 * If we got here it means we copied an
19458904SBarry.Harding@Sun.COM 				 * unmodified entry. So there is no need
19468904SBarry.Harding@Sun.COM 				 * to insert it in the table or do any
19478904SBarry.Harding@Sun.COM 				 * checks against disk size.
19488904SBarry.Harding@Sun.COM 				 *
19498904SBarry.Harding@Sun.COM 				 * This is a work around on the following
19508904SBarry.Harding@Sun.COM 				 * situation (for IDE disks, at least):
19518904SBarry.Harding@Sun.COM 				 * Different operation systems calculate
19528904SBarry.Harding@Sun.COM 				 * disk size different ways, of which there
19538904SBarry.Harding@Sun.COM 				 * are two main ways.
19548904SBarry.Harding@Sun.COM 				 *
19558904SBarry.Harding@Sun.COM 				 * The first, rounds the disk size to modulo
19568904SBarry.Harding@Sun.COM 				 * cylinder size (virtual made-up cylinder
19578904SBarry.Harding@Sun.COM 				 * usually based on maximum number of heads
19588904SBarry.Harding@Sun.COM 				 * and sectors in partition table fields).
19598904SBarry.Harding@Sun.COM 				 * Our OS's (for IDE) and most other "Unix"
19608904SBarry.Harding@Sun.COM 				 * type OS's do this.
19618904SBarry.Harding@Sun.COM 				 *
19628904SBarry.Harding@Sun.COM 				 * The second, uses every single block
19638904SBarry.Harding@Sun.COM 				 * on the disk (to maximize available space).
19648904SBarry.Harding@Sun.COM 				 * Since disk manufactures do not know about
19658904SBarry.Harding@Sun.COM 				 * "virtual cylinders", there are some number
19668904SBarry.Harding@Sun.COM 				 * of blocks that make up a partial cylinder
19678904SBarry.Harding@Sun.COM 				 * at the end of the disk.
19688904SBarry.Harding@Sun.COM 				 *
19698904SBarry.Harding@Sun.COM 				 * The difference between these two methods
19708904SBarry.Harding@Sun.COM 				 * is where the problem is. When one
19718904SBarry.Harding@Sun.COM 				 * tries to install Solaris/OpenSolaris on
19728904SBarry.Harding@Sun.COM 				 * a disk that has another OS using that
19738904SBarry.Harding@Sun.COM 				 * "partial cylinder", install fails. It fails
19748904SBarry.Harding@Sun.COM 				 * since fdisk thinks its asked to create a
19758904SBarry.Harding@Sun.COM 				 * partition with the -F option that contains
19768904SBarry.Harding@Sun.COM 				 * a partition that runs off the end of the
19778904SBarry.Harding@Sun.COM 				 * disk.
19788904SBarry.Harding@Sun.COM 				 */
19798904SBarry.Harding@Sun.COM 				continue;
19808904SBarry.Harding@Sun.COM 			}
19818904SBarry.Harding@Sun.COM 
19820Sstevel@tonic-gate 			/*
19830Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
19840Sstevel@tonic-gate 			 * in table
19850Sstevel@tonic-gate 			 */
19860Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
19870Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
19880Sstevel@tonic-gate 				(void) fprintf(stderr,
19890Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
19900Sstevel@tonic-gate 				    line);
19910Sstevel@tonic-gate 				exit(1);
19920Sstevel@tonic-gate 			}
19930Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1996251Slclee 			(void) fprintf(stderr,
19970Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
19980Sstevel@tonic-gate 			exit(1);
19990Sstevel@tonic-gate 		}
20000Sstevel@tonic-gate 
2001251Slclee 		(void) fclose(fp);
20020Sstevel@tonic-gate 		return;
20030Sstevel@tonic-gate 
20045169Slclee 	case LOADDEL:
20050Sstevel@tonic-gate 
20060Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
2007251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
2008251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
2009251Slclee 			(void) fprintf(stderr,
2010251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2011251Slclee 			exit(1);
2012251Slclee 		}
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate 		/* Find the exact entry in the table */
20150Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
20160Sstevel@tonic-gate 			if (Table[i].systid == id &&
20170Sstevel@tonic-gate 			    Table[i].bootid == act &&
20180Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
20190Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
20205169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
2021251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
20220Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
20230Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
20245169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
2025251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
202610021SSheshadri.Vasudevan@Sun.COM 			    Table[i].relsect == LE_32(rsect) &&
202710021SSheshadri.Vasudevan@Sun.COM 			    Table[i].numsect == LE_32(numsect)) {
20280Sstevel@tonic-gate 
20290Sstevel@tonic-gate 				/*
20300Sstevel@tonic-gate 				 * Found the entry. Now move rest of
20310Sstevel@tonic-gate 				 * entries up toward the top of the
20320Sstevel@tonic-gate 				 * table, leaving available entries at
20330Sstevel@tonic-gate 				 * the end of the fdisk table.
20340Sstevel@tonic-gate 				 */
2035251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
2036251Slclee 					Table[j].systid = Table[j + 1].systid;
2037251Slclee 					Table[j].bootid = Table[j + 1].bootid;
2038251Slclee 					Table[j].beghead = Table[j + 1].beghead;
2039251Slclee 					Table[j].begsect = Table[j + 1].begsect;
2040251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
2041251Slclee 					Table[j].endhead = Table[j + 1].endhead;
2042251Slclee 					Table[j].endsect = Table[j + 1].endsect;
2043251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
2044251Slclee 					Table[j].relsect = Table[j + 1].relsect;
2045251Slclee 					Table[j].numsect = Table[j + 1].numsect;
20460Sstevel@tonic-gate 				}
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate 				/*
20490Sstevel@tonic-gate 				 * Mark the last entry as unused in case
20500Sstevel@tonic-gate 				 * all table entries were in use prior
20510Sstevel@tonic-gate 				 * to the deletion.
20520Sstevel@tonic-gate 				 */
20530Sstevel@tonic-gate 
2054251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
2055251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
205610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
205710021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_is_dos_extended(id)) {
205810021SSheshadri.Vasudevan@Sun.COM 					fdisk_delete_ext_part(epp);
205910021SSheshadri.Vasudevan@Sun.COM 				}
206010021SSheshadri.Vasudevan@Sun.COM #endif
20610Sstevel@tonic-gate 				return;
20620Sstevel@tonic-gate 			}
20630Sstevel@tonic-gate 		}
206410021SSheshadri.Vasudevan@Sun.COM 
206510021SSheshadri.Vasudevan@Sun.COM #ifdef i386
206610021SSheshadri.Vasudevan@Sun.COM 		ldcnt = FD_NUMPART + 1;
206710021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
206810021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
206910021SSheshadri.Vasudevan@Sun.COM 			relsect = temp->abs_secnum + temp->logdrive_offset;
207010021SSheshadri.Vasudevan@Sun.COM 			if (temp->parts[0].systid == id &&
207110021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].bootid == act &&
207210021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].beghead == bhead &&
207310021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begsect == ((bsect & 0x3f) |
207410021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
207510021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begcyl == (uchar_t)(bcyl & 0xff) &&
207610021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endhead == ehead &&
207710021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endsect == ((esect & 0x3f) |
207810021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
207910021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endcyl == (uchar_t)(ecyl & 0xff) &&
208010021SSheshadri.Vasudevan@Sun.COM 			    relsect == LE_32(rsect) &&
208110021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].numsect == LE_32(numsect)) {
208210021SSheshadri.Vasudevan@Sun.COM 				fdisk_delete_logical_drive(epp, ldcnt);
208310021SSheshadri.Vasudevan@Sun.COM 				return;
208410021SSheshadri.Vasudevan@Sun.COM 			}
208510021SSheshadri.Vasudevan@Sun.COM 			ldcnt++;
208610021SSheshadri.Vasudevan@Sun.COM 		}
208710021SSheshadri.Vasudevan@Sun.COM #endif
208810021SSheshadri.Vasudevan@Sun.COM 
2089251Slclee 		(void) fprintf(stderr,
20900Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
20910Sstevel@tonic-gate 		    "	\"%s\"\n",
20920Sstevel@tonic-gate 		    file);
20930Sstevel@tonic-gate 		exit(1);
20948333SSuhasini.Peddada@Sun.COM 		/* FALLTHRU */
20950Sstevel@tonic-gate 
20965169Slclee 	case LOADADD:
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
2099251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
2100251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
2101251Slclee 			(void) fprintf(stderr,
2102251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2103251Slclee 			exit(1);
2104251Slclee 		}
21050Sstevel@tonic-gate 
21060Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
21070Sstevel@tonic-gate 		if (rsect == 0) {
21080Sstevel@tonic-gate 			(void) fprintf(stderr,
21090Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
21100Sstevel@tonic-gate 			    "   \"%s\".\n",
21110Sstevel@tonic-gate 			    file);
21120Sstevel@tonic-gate 			exit(1);
21130Sstevel@tonic-gate 		}
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 		/*
21160Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
21170Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
21180Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
21190Sstevel@tonic-gate 		 */
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 		if (id == EFI_PMBR) {
21220Sstevel@tonic-gate 			if (rsect != 1) {
21230Sstevel@tonic-gate 				(void) fprintf(stderr,
21240Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
21250Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
21260Sstevel@tonic-gate 				exit(1);
21270Sstevel@tonic-gate 			}
21280Sstevel@tonic-gate 
21297563SPrasad.Singamsetty@Sun.COM 
21307563SPrasad.Singamsetty@Sun.COM 			if (dev_capacity > DK_MAX_2TB) {
21317563SPrasad.Singamsetty@Sun.COM 				if (numsect != DK_MAX_2TB) {
21327563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr,
21337563SPrasad.Singamsetty@Sun.COM 					    "fdisk: EFI partitions must "
21347563SPrasad.Singamsetty@Sun.COM 					    "encompass the entire maximum 2 TB "
21357563SPrasad.Singamsetty@Sun.COM 					    "(input numsect: %u - max: %llu)\n",
21367563SPrasad.Singamsetty@Sun.COM 					    numsect, (diskaddr_t)DK_MAX_2TB);
21377563SPrasad.Singamsetty@Sun.COM 				exit(1);
21387563SPrasad.Singamsetty@Sun.COM 				}
21397563SPrasad.Singamsetty@Sun.COM 			} else if (numsect != dev_capacity - 1) {
21400Sstevel@tonic-gate 				(void) fprintf(stderr,
21410Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
2142251Slclee 				    "entire disk\n"
21437563SPrasad.Singamsetty@Sun.COM 				    "(input numsect: %u - avail: %llu)\n",
2144251Slclee 				    numsect,
21455169Slclee 				    dev_capacity - 1);
21460Sstevel@tonic-gate 				exit(1);
21470Sstevel@tonic-gate 			}
21480Sstevel@tonic-gate 		}
21490Sstevel@tonic-gate 
215010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
215110021SSheshadri.Vasudevan@Sun.COM 		if (id > FDISK_MAX_VALID_PART_ID) {
215210021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
215310021SSheshadri.Vasudevan@Sun.COM 			exit(1);
215410021SSheshadri.Vasudevan@Sun.COM 		}
215510021SSheshadri.Vasudevan@Sun.COM 
215610021SSheshadri.Vasudevan@Sun.COM 		if ((fdisk_ext_part_exists(epp)) &&
215710021SSheshadri.Vasudevan@Sun.COM 		    (fdisk_is_dos_extended(id))) {
215810021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
215910021SSheshadri.Vasudevan@Sun.COM 			    "Extended partition already exists.\n");
216010021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
216110021SSheshadri.Vasudevan@Sun.COM 			    "fdisk: Invalid entry could not be "
216210021SSheshadri.Vasudevan@Sun.COM 			    "inserted:\n        \"%s\"\n", file);
216310021SSheshadri.Vasudevan@Sun.COM 			exit(1);
216410021SSheshadri.Vasudevan@Sun.COM 		}
216510021SSheshadri.Vasudevan@Sun.COM 
216610021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_ext_part_exists(epp) &&
216710021SSheshadri.Vasudevan@Sun.COM 		    (rsect >= (ext_beg_sec = fdisk_get_ext_beg_sec(epp))) &&
216810021SSheshadri.Vasudevan@Sun.COM 		    (rsect <= (ext_end_sec = fdisk_get_ext_end_sec(epp)))) {
216910021SSheshadri.Vasudevan@Sun.COM 			int offset = MAX_LOGDRIVE_OFFSET;
217010021SSheshadri.Vasudevan@Sun.COM 
217110021SSheshadri.Vasudevan@Sun.COM 			/*
217210021SSheshadri.Vasudevan@Sun.COM 			 * Make sure that begsec doesnt wrap around.
217310021SSheshadri.Vasudevan@Sun.COM 			 * This can happen if rsect is less than offset
217410021SSheshadri.Vasudevan@Sun.COM 			 */
217510021SSheshadri.Vasudevan@Sun.COM 			if (rsect < offset) {
217610021SSheshadri.Vasudevan@Sun.COM 				return;
217710021SSheshadri.Vasudevan@Sun.COM 			}
217810021SSheshadri.Vasudevan@Sun.COM 			begsec = rsect - offset;
217910021SSheshadri.Vasudevan@Sun.COM 			if ((ldcnt = fdisk_get_logical_drive_count(epp)) == 0) {
218010021SSheshadri.Vasudevan@Sun.COM 				/*
218110021SSheshadri.Vasudevan@Sun.COM 				 * Adding the first logical drive
218210021SSheshadri.Vasudevan@Sun.COM 				 * Check if the first logical drive
218310021SSheshadri.Vasudevan@Sun.COM 				 * is out of order. In that case, do
218410021SSheshadri.Vasudevan@Sun.COM 				 * not subtract MAX_LOGDRIVE_OFFSET
218510021SSheshadri.Vasudevan@Sun.COM 				 * from the given start of partition.
218610021SSheshadri.Vasudevan@Sun.COM 				 */
218710021SSheshadri.Vasudevan@Sun.COM 				if (begsec != ext_beg_sec) {
218810021SSheshadri.Vasudevan@Sun.COM 					begsec = rsect;
218910021SSheshadri.Vasudevan@Sun.COM 					offset = 0;
219010021SSheshadri.Vasudevan@Sun.COM 				}
219110021SSheshadri.Vasudevan@Sun.COM 			}
219210021SSheshadri.Vasudevan@Sun.COM 
219310021SSheshadri.Vasudevan@Sun.COM 			if (ldcnt >= MAX_EXT_PARTS) {
219410021SSheshadri.Vasudevan@Sun.COM 				printf("\nNumber of logical drives exceeds "
219510021SSheshadri.Vasudevan@Sun.COM 				    "limit of %d.\n", MAX_EXT_PARTS);
219610021SSheshadri.Vasudevan@Sun.COM 				printf("Failing further additions.\n");
219710021SSheshadri.Vasudevan@Sun.COM 				exit(1);
219810021SSheshadri.Vasudevan@Sun.COM 			}
219910021SSheshadri.Vasudevan@Sun.COM 
220010021SSheshadri.Vasudevan@Sun.COM 			if (numsect == 0) {
220110021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
220210021SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Partition size cannot be zero:\n"
220310021SSheshadri.Vasudevan@Sun.COM 				    "   \"%s\".\n",
220410021SSheshadri.Vasudevan@Sun.COM 				    file);
220510021SSheshadri.Vasudevan@Sun.COM 				exit(1);
220610021SSheshadri.Vasudevan@Sun.COM 			}
220710021SSheshadri.Vasudevan@Sun.COM 			endsec = rsect + numsect - 1;
220810021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_validate_logical_drive(epp, begsec,
220910021SSheshadri.Vasudevan@Sun.COM 			    offset, numsect) == 0) {
221010021SSheshadri.Vasudevan@Sun.COM 				/* Valid logical drive */
221110021SSheshadri.Vasudevan@Sun.COM 				fdisk_add_logical_drive(epp, begsec, endsec,
221210021SSheshadri.Vasudevan@Sun.COM 				    id);
221310021SSheshadri.Vasudevan@Sun.COM 				return;
2214*10682SSheshadri.Vasudevan@Sun.COM 			} else {
2215*10682SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
2216*10682SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Invalid entry could not be "
2217*10682SSheshadri.Vasudevan@Sun.COM 				    "inserted:\n        \"%s\"\n", file);
2218*10682SSheshadri.Vasudevan@Sun.COM 				exit(1);
221910021SSheshadri.Vasudevan@Sun.COM 			}
222010021SSheshadri.Vasudevan@Sun.COM 		}
222110021SSheshadri.Vasudevan@Sun.COM #endif
222210021SSheshadri.Vasudevan@Sun.COM 
22230Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
22240Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
22250Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
22260Sstevel@tonic-gate 			(void) fprintf(stderr,
22270Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
22280Sstevel@tonic-gate 			    "	\"%s\"\n",
22290Sstevel@tonic-gate 			    file);
22300Sstevel@tonic-gate 			exit(1);
22310Sstevel@tonic-gate 		}
22320Sstevel@tonic-gate 
22330Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
22340Sstevel@tonic-gate 		if (verify_tbl() < 0) {
2235251Slclee 			(void) fprintf(stderr,
2236251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
22370Sstevel@tonic-gate 			exit(1);
22380Sstevel@tonic-gate 		}
22390Sstevel@tonic-gate 	} /* switch funct */
22400Sstevel@tonic-gate }
22410Sstevel@tonic-gate 
22420Sstevel@tonic-gate /*
22430Sstevel@tonic-gate  * Set_Table_CHS_Values
22440Sstevel@tonic-gate  *
22450Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
22460Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
22470Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
22480Sstevel@tonic-gate  *
22490Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
22500Sstevel@tonic-gate  *
22510Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
22520Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
22530Sstevel@tonic-gate  */
22540Sstevel@tonic-gate static void
22550Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
22560Sstevel@tonic-gate {
22570Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
22600Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
22610Sstevel@tonic-gate 		/*
22620Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
22630Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
22640Sstevel@tonic-gate 		 */
22650Sstevel@tonic-gate 		cy = MAX_CYL + 1;
22660Sstevel@tonic-gate 		hd = MAX_HEAD;
22670Sstevel@tonic-gate 		sc = MAX_SECT;
22680Sstevel@tonic-gate 	} else {
22690Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
22700Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
22710Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
22720Sstevel@tonic-gate 	}
22730Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
2274251Slclee 	Table[ti].beghead = (uchar_t)hd;
2275251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
22760Sstevel@tonic-gate 
22770Sstevel@tonic-gate 	/*
22780Sstevel@tonic-gate 	 * This code is identical to the code above
22790Sstevel@tonic-gate 	 * except that it works on ending CHS values
22800Sstevel@tonic-gate 	 */
22810Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
22820Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
22830Sstevel@tonic-gate 		cy = MAX_CYL + 1;
22840Sstevel@tonic-gate 		hd = MAX_HEAD;
22850Sstevel@tonic-gate 		sc = MAX_SECT;
22860Sstevel@tonic-gate 	} else {
22870Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
22880Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
22890Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
22900Sstevel@tonic-gate 	}
22910Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
2292251Slclee 	Table[ti].endhead = (uchar_t)hd;
2293251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
22940Sstevel@tonic-gate }
22950Sstevel@tonic-gate 
22960Sstevel@tonic-gate /*
22970Sstevel@tonic-gate  * insert_tbl
22980Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
22990Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
23000Sstevel@tonic-gate  *	entries!
23010Sstevel@tonic-gate  */
2302251Slclee static int
2303251Slclee insert_tbl(
2304251Slclee     int id, int act,
2305251Slclee     int bhead, int bsect, int bcyl,
2306251Slclee     int ehead, int esect, int ecyl,
23077563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect)
23080Sstevel@tonic-gate {
23090Sstevel@tonic-gate 	int	i;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 	/* validate partition size */
23127563SPrasad.Singamsetty@Sun.COM 	if (((diskaddr_t)rsect + numsect) > dev_capacity) {
2313251Slclee 		(void) fprintf(stderr,
23140Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
23150Sstevel@tonic-gate 		return (-1);
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 
23187563SPrasad.Singamsetty@Sun.COM 
23190Sstevel@tonic-gate 	/* find UNUSED partition table entry */
23200Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
23210Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
23220Sstevel@tonic-gate 			break;
23230Sstevel@tonic-gate 		}
23240Sstevel@tonic-gate 	}
23250Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
2326251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
23270Sstevel@tonic-gate 		return (-1);
23280Sstevel@tonic-gate 	}
23290Sstevel@tonic-gate 
23300Sstevel@tonic-gate 
2331251Slclee 	Table[i].systid = (uchar_t)id;
2332251Slclee 	Table[i].bootid = (uchar_t)act;
233310021SSheshadri.Vasudevan@Sun.COM 	Table[i].numsect = LE_32(numsect);
233410021SSheshadri.Vasudevan@Sun.COM 	Table[i].relsect = LE_32(rsect);
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 	/*
23370Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
23380Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
23390Sstevel@tonic-gate 	 */
23400Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
23410Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
23420Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
23430Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
23440Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
23450Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
23460Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
23470Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
23480Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
23490Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
2350251Slclee 		Table[i].beghead = (uchar_t)bhead;
2351251Slclee 		Table[i].endhead = (uchar_t)ehead;
2352251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
23530Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
23540Sstevel@tonic-gate 	} else {
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 		/*
23570Sstevel@tonic-gate 		 * The specified values are invalid,
23580Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
23590Sstevel@tonic-gate 		 */
23600Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
23610Sstevel@tonic-gate 	}
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	/*
23640Sstevel@tonic-gate 	 * return partition index
23650Sstevel@tonic-gate 	 */
23660Sstevel@tonic-gate 	return (i);
23670Sstevel@tonic-gate }
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate /*
23708904SBarry.Harding@Sun.COM  * entry_from_old_table
23718904SBarry.Harding@Sun.COM  *	If the specified entry is in the old table and is not a Solaris entry
23728904SBarry.Harding@Sun.COM  *	then insert same entry into new fdisk table. If we do this then
23738904SBarry.Harding@Sun.COM  *	all checks are skipped for that entry!
23748904SBarry.Harding@Sun.COM  */
23758904SBarry.Harding@Sun.COM static int
23768904SBarry.Harding@Sun.COM entry_from_old_table(
23778904SBarry.Harding@Sun.COM     int id, int act,
23788904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
23798904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
23808904SBarry.Harding@Sun.COM     uint32_t rsect, uint32_t numsect)
23818904SBarry.Harding@Sun.COM {
23828904SBarry.Harding@Sun.COM 	uint32_t	i, j;
23838904SBarry.Harding@Sun.COM 
23848904SBarry.Harding@Sun.COM 	if (id == SUNIXOS || id == SUNIXOS2)
23858904SBarry.Harding@Sun.COM 		return (0);
238610015SBarry.Harding@Sun.COM 	for (i = 0; i < FD_NUMPART; i++) {
23878904SBarry.Harding@Sun.COM 		if (Old_Table[i].systid == id &&
23888904SBarry.Harding@Sun.COM 		    Old_Table[i].bootid == act &&
23898904SBarry.Harding@Sun.COM 		    Old_Table[i].beghead == bhead &&
23908904SBarry.Harding@Sun.COM 		    Old_Table[i].begsect == ((bsect & 0x3f) |
23918904SBarry.Harding@Sun.COM 		    (uchar_t)((bcyl>>2) & 0xc0)) &&
23928904SBarry.Harding@Sun.COM 		    Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
23938904SBarry.Harding@Sun.COM 		    Old_Table[i].endhead == ehead &&
23948904SBarry.Harding@Sun.COM 		    Old_Table[i].endsect == ((esect & 0x3f) |
23958904SBarry.Harding@Sun.COM 		    (uchar_t)((ecyl>>2) & 0xc0)) &&
23968904SBarry.Harding@Sun.COM 		    Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
23978904SBarry.Harding@Sun.COM 		    Old_Table[i].relsect == lel(rsect) &&
23988904SBarry.Harding@Sun.COM 		    Old_Table[i].numsect == lel(numsect)) {
23998904SBarry.Harding@Sun.COM 			/* find UNUSED partition table entry */
24008904SBarry.Harding@Sun.COM 			for (j = 0; j < FD_NUMPART; j++) {
24018904SBarry.Harding@Sun.COM 				if (Table[j].systid == UNUSED) {
24028904SBarry.Harding@Sun.COM 					(void) memcpy(&Table[j], &Old_Table[i],
24038904SBarry.Harding@Sun.COM 					    sizeof (Table[0]));
24048904SBarry.Harding@Sun.COM 					skip_verify[j] = 1;
24058904SBarry.Harding@Sun.COM 					return (1);
24068904SBarry.Harding@Sun.COM 
24078904SBarry.Harding@Sun.COM 				}
24088904SBarry.Harding@Sun.COM 			}
24098904SBarry.Harding@Sun.COM 			return (0);
24108904SBarry.Harding@Sun.COM 		}
24118904SBarry.Harding@Sun.COM 
24128904SBarry.Harding@Sun.COM 	}
24138904SBarry.Harding@Sun.COM 	return (0);
24148904SBarry.Harding@Sun.COM }
24158904SBarry.Harding@Sun.COM 
24168904SBarry.Harding@Sun.COM /*
24170Sstevel@tonic-gate  * verify_tbl
24180Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
24190Sstevel@tonic-gate  * the disk.
24200Sstevel@tonic-gate  */
2421251Slclee static int
2422251Slclee verify_tbl(void)
24230Sstevel@tonic-gate {
24247563SPrasad.Singamsetty@Sun.COM 	uint32_t	i, j, rsect, numsect;
24250Sstevel@tonic-gate 	int	noMoreParts = 0;
24260Sstevel@tonic-gate 	int	numParts = 0;
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
2429251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
24300Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
24310Sstevel@tonic-gate 			numParts++;
24320Sstevel@tonic-gate 			/*
24330Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
24340Sstevel@tonic-gate 			 * EFI_PMBR part
24350Sstevel@tonic-gate 			 */
24360Sstevel@tonic-gate 			if (noMoreParts) {
24370Sstevel@tonic-gate 				return (-1);
24380Sstevel@tonic-gate 			}
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate 			/*
24410Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
24420Sstevel@tonic-gate 			 * and must be Table entry 0
24430Sstevel@tonic-gate 			 */
24440Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
24450Sstevel@tonic-gate 				if (i == 0) {
24460Sstevel@tonic-gate 					noMoreParts = 1;
24470Sstevel@tonic-gate 				} else {
24480Sstevel@tonic-gate 					return (-1);
24490Sstevel@tonic-gate 				}
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
2452251Slclee 					(void) fprintf(stderr, "ERROR: "
24530Sstevel@tonic-gate 					    "Invalid starting sector "
24540Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
24550Sstevel@tonic-gate 					    "relsect %d "
24560Sstevel@tonic-gate 					    "(should be 1)\n",
24570Sstevel@tonic-gate 					    Table[i].relsect);
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 					return (-1);
24600Sstevel@tonic-gate 				}
24610Sstevel@tonic-gate 
24625169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
2463251Slclee 					(void) fprintf(stderr, "ERROR: "
24640Sstevel@tonic-gate 					    "EFI_PMBR partition must "
24650Sstevel@tonic-gate 					    "encompass the entire "
2466251Slclee 					    "disk.\n numsect %d - "
24675169Slclee 					    "actual %llu\n",
24680Sstevel@tonic-gate 					    Table[i].numsect,
24695169Slclee 					    dev_capacity - 1);
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 					return (-1);
24720Sstevel@tonic-gate 				}
24730Sstevel@tonic-gate 			}
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
247610021SSheshadri.Vasudevan@Sun.COM 			rsect = LE_32(Table[i].relsect);
247710021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
24787563SPrasad.Singamsetty@Sun.COM 
24797563SPrasad.Singamsetty@Sun.COM 			if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
24807563SPrasad.Singamsetty@Sun.COM 			    (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
24818904SBarry.Harding@Sun.COM 				if (!skip_verify[i])
24828904SBarry.Harding@Sun.COM 					return (-1);
24830Sstevel@tonic-gate 			}
24840Sstevel@tonic-gate 
2485251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
24860Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
24877563SPrasad.Singamsetty@Sun.COM 					uint32_t t_relsect =
248810021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].relsect);
24897563SPrasad.Singamsetty@Sun.COM 					uint32_t t_numsect =
249010021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].numsect);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 					if (noMoreParts) {
2493251Slclee 						(void) fprintf(stderr,
24940Sstevel@tonic-gate 						    "Cannot add partition to "
24950Sstevel@tonic-gate 						    "table; no more partitions "
24960Sstevel@tonic-gate 						    "allowed\n");
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 						if (io_debug) {
2499251Slclee 							(void) fprintf(stderr,
25000Sstevel@tonic-gate 							    "DEBUG: Current "
25010Sstevel@tonic-gate 							    "partition:\t"
25020Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2503251Slclee 							    "%d:%d:%d:%d:%d\n"
25040Sstevel@tonic-gate 							    "       Next "
25050Sstevel@tonic-gate 							    "partition:\t\t"
25060Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2507251Slclee 							    "%d:%d:%d:%d:%d\n",
25080Sstevel@tonic-gate 							    Table[i].systid,
25090Sstevel@tonic-gate 							    Table[i].bootid,
25100Sstevel@tonic-gate 							    Table[i].begcyl,
25110Sstevel@tonic-gate 							    Table[i].beghead,
25120Sstevel@tonic-gate 							    Table[i].begsect,
25130Sstevel@tonic-gate 							    Table[i].endcyl,
25140Sstevel@tonic-gate 							    Table[i].endhead,
25150Sstevel@tonic-gate 							    Table[i].endsect,
25160Sstevel@tonic-gate 							    Table[i].relsect,
25170Sstevel@tonic-gate 							    Table[i].numsect,
25180Sstevel@tonic-gate 							    Table[j].systid,
25190Sstevel@tonic-gate 							    Table[j].bootid,
25200Sstevel@tonic-gate 							    Table[j].begcyl,
25210Sstevel@tonic-gate 							    Table[j].beghead,
25220Sstevel@tonic-gate 							    Table[j].begsect,
25230Sstevel@tonic-gate 							    Table[j].endcyl,
25240Sstevel@tonic-gate 							    Table[j].endhead,
25250Sstevel@tonic-gate 							    Table[j].endsect,
25260Sstevel@tonic-gate 							    Table[j].relsect,
25270Sstevel@tonic-gate 							    Table[j].numsect);
25280Sstevel@tonic-gate 						}
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 						return (-1);
25310Sstevel@tonic-gate 					}
25320Sstevel@tonic-gate 					if ((rsect >=
25330Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
2534251Slclee 					    ((rsect + numsect) <= t_relsect)) {
25350Sstevel@tonic-gate 						continue;
25360Sstevel@tonic-gate 					} else {
2537251Slclee 						(void) fprintf(stderr, "ERROR: "
25380Sstevel@tonic-gate 						    "current partition overlaps"
25390Sstevel@tonic-gate 						    " following partition\n");
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 						return (-1);
25420Sstevel@tonic-gate 					}
25430Sstevel@tonic-gate 				}
25440Sstevel@tonic-gate 			}
25450Sstevel@tonic-gate 		} else {
25460Sstevel@tonic-gate 			noMoreParts = 1;
25470Sstevel@tonic-gate 		}
25480Sstevel@tonic-gate 	}
25490Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
25508904SBarry.Harding@Sun.COM 		if (noMoreParts)
25518904SBarry.Harding@Sun.COM 			return (-1);
25528904SBarry.Harding@Sun.COM 		if (!skip_verify[i] &&
25538904SBarry.Harding@Sun.COM 		    ((((diskaddr_t)lel(Table[i].relsect) +
25548333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > dev_capacity) ||
25558333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
25568904SBarry.Harding@Sun.COM 		    lel(Table[i].numsect)) > DK_MAX_2TB))) {
25570Sstevel@tonic-gate 			return (-1);
25580Sstevel@tonic-gate 		}
25590Sstevel@tonic-gate 	}
25600Sstevel@tonic-gate 
25610Sstevel@tonic-gate 	return (numParts);
25620Sstevel@tonic-gate }
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate /*
25650Sstevel@tonic-gate  * pars_fdisk
25660Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
25670Sstevel@tonic-gate  * (-A, -D, -F).
25680Sstevel@tonic-gate  */
2569251Slclee static int
2570251Slclee pars_fdisk(
2571251Slclee     char *line,
2572251Slclee     int *id, int *act,
2573251Slclee     int *bhead, int *bsect, int *bcyl,
2574251Slclee     int *ehead, int *esect, int *ecyl,
25757563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect)
25760Sstevel@tonic-gate {
25770Sstevel@tonic-gate 	int	i;
257810021SSheshadri.Vasudevan@Sun.COM 	int64_t test;
257910021SSheshadri.Vasudevan@Sun.COM 	char *tok, *p;
258010021SSheshadri.Vasudevan@Sun.COM 	char buf[256];
258110021SSheshadri.Vasudevan@Sun.COM 
25820Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
25835169Slclee 		return (1);
25840Sstevel@tonic-gate 	line[strlen(line)] = '\0';
25850Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
25860Sstevel@tonic-gate 		if (line[i] == '\0') {
25870Sstevel@tonic-gate 			break;
25880Sstevel@tonic-gate 		} else if (line[i] == ':') {
25890Sstevel@tonic-gate 			line[i] = ' ';
25900Sstevel@tonic-gate 		}
25910Sstevel@tonic-gate 	}
259210021SSheshadri.Vasudevan@Sun.COM 	strncpy(buf, line, 256);
259310021SSheshadri.Vasudevan@Sun.COM 	errno = 0;
259410021SSheshadri.Vasudevan@Sun.COM 	tok = strtok(buf, ": \t\n");
259510021SSheshadri.Vasudevan@Sun.COM 	while (tok != NULL) {
259610021SSheshadri.Vasudevan@Sun.COM 		for (p = tok; *p != '\0'; p++) {
259710021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(*p)) {
259810021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid input %s in line %s.\n",
259910021SSheshadri.Vasudevan@Sun.COM 				    tok, line);
260010021SSheshadri.Vasudevan@Sun.COM 				exit(1);
260110021SSheshadri.Vasudevan@Sun.COM 			}
260210021SSheshadri.Vasudevan@Sun.COM 		}
260310021SSheshadri.Vasudevan@Sun.COM 
260410021SSheshadri.Vasudevan@Sun.COM 		test = strtoll(tok, (char **)NULL, 10);
260510021SSheshadri.Vasudevan@Sun.COM 		if ((test < 0) || (test > 0xFFFFFFFF) || (errno != 0)) {
260610021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid input %s in line %s.\n", tok, line);
260710021SSheshadri.Vasudevan@Sun.COM 			exit(1);
260810021SSheshadri.Vasudevan@Sun.COM 		}
260910021SSheshadri.Vasudevan@Sun.COM 		tok = strtok(NULL, ": \t\n");
261010021SSheshadri.Vasudevan@Sun.COM 	}
26117563SPrasad.Singamsetty@Sun.COM 	if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
26120Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
26130Sstevel@tonic-gate 	    rsect, numsect) != 10) {
26140Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
26150Sstevel@tonic-gate 		exit(1);
26160Sstevel@tonic-gate 	}
26170Sstevel@tonic-gate 	return (0);
26180Sstevel@tonic-gate }
26190Sstevel@tonic-gate 
26200Sstevel@tonic-gate /*
26210Sstevel@tonic-gate  * validate_part
26220Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
26230Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
26240Sstevel@tonic-gate  */
2625251Slclee static int
26267563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect)
26270Sstevel@tonic-gate {
26280Sstevel@tonic-gate 	int i;
26290Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
26300Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
26310Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
263210021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].relsect == LE_32(rsect)) &&
263310021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].numsect == LE_32(numsect)))
26345169Slclee 				return (0);
26350Sstevel@tonic-gate 		}
2636251Slclee 		(void) fprintf(stderr,
2637251Slclee 		    "New partition cannot start at sector 0\n");
26380Sstevel@tonic-gate 		return (-1);
26390Sstevel@tonic-gate 	}
264010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
264110021SSheshadri.Vasudevan@Sun.COM 	if (id > FDISK_MAX_VALID_PART_ID) {
264210021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Invalid partition ID\n");
264310021SSheshadri.Vasudevan@Sun.COM 		return (-1);
264410021SSheshadri.Vasudevan@Sun.COM 	}
264510021SSheshadri.Vasudevan@Sun.COM #endif
26460Sstevel@tonic-gate 	return (0);
26470Sstevel@tonic-gate }
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate /*
26500Sstevel@tonic-gate  * stage0
26510Sstevel@tonic-gate  * Print out interactive menu and process user input.
26520Sstevel@tonic-gate  */
2653251Slclee static void
2654251Slclee stage0(void)
26550Sstevel@tonic-gate {
265610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
265710021SSheshadri.Vasudevan@Sun.COM 	int rval;
265810021SSheshadri.Vasudevan@Sun.COM #endif
2659251Slclee 	dispmenu();
2660251Slclee 	for (;;) {
2661251Slclee 		(void) printf(Q_LINE);
2662251Slclee 		(void) printf("Enter Selection: ");
26639786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
26640Sstevel@tonic-gate 		rm_blanks(s);
266510021SSheshadri.Vasudevan@Sun.COM #ifdef i386
266610021SSheshadri.Vasudevan@Sun.COM 		while (!((s[0] > '0') && (s[0] < '8') &&
266710021SSheshadri.Vasudevan@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
266810021SSheshadri.Vasudevan@Sun.COM #else
26699786SBarry.Harding@Sun.COM 		while (!((s[0] > '0') && (s[0] < '7') &&
26709786SBarry.Harding@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
267110021SSheshadri.Vasudevan@Sun.COM #endif
2672251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
267310021SSheshadri.Vasudevan@Sun.COM #ifdef i386
267410021SSheshadri.Vasudevan@Sun.COM 			(void) printf(
267510021SSheshadri.Vasudevan@Sun.COM 			    "Enter a one-digit number between 1 and 7.");
267610021SSheshadri.Vasudevan@Sun.COM #else
2677251Slclee 			(void) printf(
2678251Slclee 			    "Enter a one-digit number between 1 and 6.");
267910021SSheshadri.Vasudevan@Sun.COM #endif
2680251Slclee 			(void) printf(Q_LINE);
2681251Slclee 			(void) printf("Enter Selection: ");
26829786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
26830Sstevel@tonic-gate 			rm_blanks(s);
26840Sstevel@tonic-gate 		}
2685251Slclee 		(void) printf(E_LINE);
26860Sstevel@tonic-gate 		switch (s[0]) {
26870Sstevel@tonic-gate 			case '1':
26880Sstevel@tonic-gate 				if (pcreate() == -1)
26890Sstevel@tonic-gate 					return;
26900Sstevel@tonic-gate 				break;
26910Sstevel@tonic-gate 			case '2':
26920Sstevel@tonic-gate 				if (pchange() == -1)
26930Sstevel@tonic-gate 					return;
26940Sstevel@tonic-gate 				break;
26950Sstevel@tonic-gate 			case '3':
26960Sstevel@tonic-gate 				if (pdelete() == -1)
26970Sstevel@tonic-gate 					return;
26980Sstevel@tonic-gate 				break;
26990Sstevel@tonic-gate 			case '4':
27000Sstevel@tonic-gate 				if (ppartid() == -1)
27010Sstevel@tonic-gate 					return;
27020Sstevel@tonic-gate 				break;
270310021SSheshadri.Vasudevan@Sun.COM #ifdef i386
270410021SSheshadri.Vasudevan@Sun.COM 			case '5':
270510021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_ext_part_exists(epp)) {
270610021SSheshadri.Vasudevan@Sun.COM 					ext_part_menu();
270710021SSheshadri.Vasudevan@Sun.COM 				} else {
270810021SSheshadri.Vasudevan@Sun.COM 					printf(Q_LINE);
270910021SSheshadri.Vasudevan@Sun.COM 					printf("\nNo extended partition found"
271010021SSheshadri.Vasudevan@Sun.COM 					    "\n");
271110021SSheshadri.Vasudevan@Sun.COM 					printf("Press enter to continue\n");
271210021SSheshadri.Vasudevan@Sun.COM 					ext_read_input(s);
271310021SSheshadri.Vasudevan@Sun.COM 				}
271410021SSheshadri.Vasudevan@Sun.COM 				break;
271510021SSheshadri.Vasudevan@Sun.COM 			case '6':
271610021SSheshadri.Vasudevan@Sun.COM 				/* update disk partition table, if changed */
271710021SSheshadri.Vasudevan@Sun.COM 				if (TableChanged() == 1) {
271810021SSheshadri.Vasudevan@Sun.COM 					copy_Table_to_Bootblk();
271910021SSheshadri.Vasudevan@Sun.COM 					dev_mboot_write(0, Bootsect, sectsiz);
272010021SSheshadri.Vasudevan@Sun.COM 				}
272110021SSheshadri.Vasudevan@Sun.COM 
272210021SSheshadri.Vasudevan@Sun.COM 				/*
272310021SSheshadri.Vasudevan@Sun.COM 				 * If the VTOC table is wrong fix it
272410021SSheshadri.Vasudevan@Sun.COM 				 * (truncate only)
272510021SSheshadri.Vasudevan@Sun.COM 				 */
272610021SSheshadri.Vasudevan@Sun.COM 				if (io_adjt) {
272710021SSheshadri.Vasudevan@Sun.COM 					fix_slice();
272810021SSheshadri.Vasudevan@Sun.COM 				}
272910021SSheshadri.Vasudevan@Sun.COM 				if (!io_readonly) {
273010021SSheshadri.Vasudevan@Sun.COM 					rval = fdisk_commit_ext_part(epp);
273110021SSheshadri.Vasudevan@Sun.COM 					switch (rval) {
273210021SSheshadri.Vasudevan@Sun.COM 						case FDISK_SUCCESS:
273310021SSheshadri.Vasudevan@Sun.COM 							/* Success */
273410021SSheshadri.Vasudevan@Sun.COM 							/* Fallthrough */
273510021SSheshadri.Vasudevan@Sun.COM 						case FDISK_ENOEXTPART:
273610021SSheshadri.Vasudevan@Sun.COM 							/* Nothing to do */
273710021SSheshadri.Vasudevan@Sun.COM 							break;
273810021SSheshadri.Vasudevan@Sun.COM 						case FDISK_EMOUNTED:
273910021SSheshadri.Vasudevan@Sun.COM 							printf(Q_LINE);
274010021SSheshadri.Vasudevan@Sun.COM 							preach_and_continue();
274110021SSheshadri.Vasudevan@Sun.COM 							continue;
274210021SSheshadri.Vasudevan@Sun.COM 						default:
274310021SSheshadri.Vasudevan@Sun.COM 							perror("Commit failed");
274410021SSheshadri.Vasudevan@Sun.COM 							exit(1);
274510021SSheshadri.Vasudevan@Sun.COM 					}
274610021SSheshadri.Vasudevan@Sun.COM 					libfdisk_fini(&epp);
274710021SSheshadri.Vasudevan@Sun.COM 				}
274810021SSheshadri.Vasudevan@Sun.COM 				(void) close(Dev);
274910021SSheshadri.Vasudevan@Sun.COM 				exit(0);
275010021SSheshadri.Vasudevan@Sun.COM #else
27510Sstevel@tonic-gate 			case '5':
27520Sstevel@tonic-gate 				/* update disk partition table, if changed */
27530Sstevel@tonic-gate 				if (TableChanged() == 1) {
27540Sstevel@tonic-gate 					copy_Table_to_Bootblk();
27550Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
27560Sstevel@tonic-gate 				}
27570Sstevel@tonic-gate 				/*
27580Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27590Sstevel@tonic-gate 				 * (truncate only)
27600Sstevel@tonic-gate 				 */
27610Sstevel@tonic-gate 				if (io_adjt) {
27620Sstevel@tonic-gate 					fix_slice();
27630Sstevel@tonic-gate 				}
2764251Slclee 				(void) close(Dev);
27650Sstevel@tonic-gate 				exit(0);
2766251Slclee 				/* FALLTHRU */
276710021SSheshadri.Vasudevan@Sun.COM #endif
276810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
276910021SSheshadri.Vasudevan@Sun.COM 			case '7':
277010021SSheshadri.Vasudevan@Sun.COM #else
27710Sstevel@tonic-gate 			case '6':
277210021SSheshadri.Vasudevan@Sun.COM #endif
27730Sstevel@tonic-gate 				/*
27740Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27750Sstevel@tonic-gate 				 * (truncate only)
27760Sstevel@tonic-gate 				 */
27770Sstevel@tonic-gate 				if (io_adjt) {
27780Sstevel@tonic-gate 					fix_slice();
27790Sstevel@tonic-gate 				}
2780251Slclee 				(void) close(Dev);
27810Sstevel@tonic-gate 				exit(0);
2782251Slclee 				/* FALLTHRU */
27830Sstevel@tonic-gate 			default:
27840Sstevel@tonic-gate 				break;
27850Sstevel@tonic-gate 		}
27860Sstevel@tonic-gate 		copy_Table_to_Bootblk();
27870Sstevel@tonic-gate 		disptbl();
2788251Slclee 		dispmenu();
27890Sstevel@tonic-gate 	}
27900Sstevel@tonic-gate }
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate /*
27930Sstevel@tonic-gate  * pcreate
27940Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
27950Sstevel@tonic-gate  */
2796251Slclee static int
2797251Slclee pcreate(void)
27980Sstevel@tonic-gate {
2799251Slclee 	uchar_t tsystid = 'z';
28000Sstevel@tonic-gate 	int i, j;
28017563SPrasad.Singamsetty@Sun.COM 	uint32_t numsect;
28020Sstevel@tonic-gate 	int retCode = 0;
280310021SSheshadri.Vasudevan@Sun.COM #ifdef i386
280410021SSheshadri.Vasudevan@Sun.COM 	int ext_part_present = 0;
280510021SSheshadri.Vasudevan@Sun.COM #endif
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 	i = 0;
2808251Slclee 	for (;;) {
28090Sstevel@tonic-gate 		if (i == FD_NUMPART) {
2810251Slclee 			(void) printf(E_LINE);
2811251Slclee 			(void) printf(
2812251Slclee 			    "The partition table is full!\n"
2813251Slclee 			    "You must delete a partition before creating"
28140Sstevel@tonic-gate 			    " a new one.\n");
28150Sstevel@tonic-gate 			return (-1);
28160Sstevel@tonic-gate 		}
28170Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
28180Sstevel@tonic-gate 			break;
28190Sstevel@tonic-gate 		}
28200Sstevel@tonic-gate 		i++;
28210Sstevel@tonic-gate 	}
28220Sstevel@tonic-gate 
28237563SPrasad.Singamsetty@Sun.COM 	numsect = 0;
28240Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28250Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
282610021SSheshadri.Vasudevan@Sun.COM 			numsect += LE_32(Table[i].numsect);
28270Sstevel@tonic-gate 		}
282810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
282910021SSheshadri.Vasudevan@Sun.COM 		/* Check if an extended partition already exists */
283010021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(Table[i].systid)) {
283110021SSheshadri.Vasudevan@Sun.COM 			ext_part_present = 1;
283210021SSheshadri.Vasudevan@Sun.COM 		}
283310021SSheshadri.Vasudevan@Sun.COM #endif
28347563SPrasad.Singamsetty@Sun.COM 		if (numsect >= chs_capacity) {
2835251Slclee 			(void) printf(E_LINE);
2836251Slclee 			(void) printf("There is no more room on the disk for"
28370Sstevel@tonic-gate 			    " another partition.\n");
2838251Slclee 			(void) printf(
2839251Slclee 			    "You must delete a partition before creating"
28400Sstevel@tonic-gate 			    " a new one.\n");
28410Sstevel@tonic-gate 			return (-1);
28420Sstevel@tonic-gate 		}
28430Sstevel@tonic-gate 	}
28440Sstevel@tonic-gate 	while (tsystid == 'z') {
28457563SPrasad.Singamsetty@Sun.COM 
28467563SPrasad.Singamsetty@Sun.COM 		/*
28477563SPrasad.Singamsetty@Sun.COM 		 * The question here is expanding to more than what is
28487563SPrasad.Singamsetty@Sun.COM 		 * allocated for question lines (Q_LINE) which garbles
28497563SPrasad.Singamsetty@Sun.COM 		 * at least warning line. Clearing warning line as workaround
28507563SPrasad.Singamsetty@Sun.COM 		 * for now.
28517563SPrasad.Singamsetty@Sun.COM 		 */
28527563SPrasad.Singamsetty@Sun.COM 
28537563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
2854251Slclee 		(void) printf(Q_LINE);
2855251Slclee 		(void) printf(
2856251Slclee 		    "Select the partition type to create:\n"
2857251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
2858251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
2859251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
2860251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
28619786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
28620Sstevel@tonic-gate 		rm_blanks(s);
28639786SBarry.Harding@Sun.COM 		if ((s[1] != '\0') && (s[1] != '\n')) {
2864251Slclee 			(void) printf(E_LINE);
2865251Slclee 			(void) printf("Invalid selection, try again.");
28660Sstevel@tonic-gate 			continue;
28670Sstevel@tonic-gate 		}
28680Sstevel@tonic-gate 		switch (s[0]) {
28690Sstevel@tonic-gate 		case '0':		/* exit */
28705169Slclee 			(void) printf(E_LINE);
28715169Slclee 			return (-1);
28720Sstevel@tonic-gate 		case '1':		/* Solaris partition */
28735169Slclee 			tsystid = SUNIXOS2;
28745169Slclee 			break;
28750Sstevel@tonic-gate 		case '2':		/* UNIX partition */
28765169Slclee 			tsystid = UNIXOS;
28775169Slclee 			break;
28780Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
28795169Slclee 			tsystid = PCIXOS;
28805169Slclee 			break;
28810Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
28825169Slclee 			tsystid = OTHEROS;
28835169Slclee 			break;
28840Sstevel@tonic-gate 		case '5':
28855169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
28865169Slclee 			break;
28870Sstevel@tonic-gate 		case '6':
28885169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
28895169Slclee 			break;
28900Sstevel@tonic-gate 		case '7':
289110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
289210021SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
289310021SSheshadri.Vasudevan@Sun.COM 				printf(Q_LINE);
289410021SSheshadri.Vasudevan@Sun.COM 				printf(E_LINE);
289510021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr,
289610021SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
289710021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Press enter to continue\n");
289810021SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
289910021SSheshadri.Vasudevan@Sun.COM 				continue;
290010021SSheshadri.Vasudevan@Sun.COM 			}
290110021SSheshadri.Vasudevan@Sun.COM #endif
29025169Slclee 			tsystid = EXTDOS;
29035169Slclee 			break;
29040Sstevel@tonic-gate 		case '8':
29055169Slclee 			tsystid = DOSHUGE;
29065169Slclee 			break;
29070Sstevel@tonic-gate 		case '9':
29085169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
29095169Slclee 			break;
29100Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
29110Sstevel@tonic-gate 		case 'A':
29125169Slclee 			tsystid = X86BOOT;
29135169Slclee 			break;
29140Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
29150Sstevel@tonic-gate 		case 'B':
29165169Slclee 			tsystid = DIAGPART;
29175169Slclee 			break;
29180Sstevel@tonic-gate 		case 'c':		/* FAT32 */
29190Sstevel@tonic-gate 		case 'C':
29205169Slclee 			tsystid = FDISK_WINDOWS;
29215169Slclee 			break;
29220Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
29230Sstevel@tonic-gate 		case 'D':
29245169Slclee 			tsystid = FDISK_EXT_WIN;
29255169Slclee 			break;
29260Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
29270Sstevel@tonic-gate 		case 'E':
2928*10682SSheshadri.Vasudevan@Sun.COM #ifdef i386
2929*10682SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
2930*10682SSheshadri.Vasudevan@Sun.COM 				printf(Q_LINE);
2931*10682SSheshadri.Vasudevan@Sun.COM 				printf(E_LINE);
2932*10682SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr,
2933*10682SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
2934*10682SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Press enter to continue\n");
2935*10682SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
2936*10682SSheshadri.Vasudevan@Sun.COM 				continue;
2937*10682SSheshadri.Vasudevan@Sun.COM 			}
2938*10682SSheshadri.Vasudevan@Sun.COM #endif
29395169Slclee 			tsystid = FDISK_EXTLBA;
29405169Slclee 			break;
29410Sstevel@tonic-gate 		case 'f':
29420Sstevel@tonic-gate 		case 'F':
29435169Slclee 			tsystid = EFI_PMBR;
29445169Slclee 			break;
29450Sstevel@tonic-gate 		default:
29465169Slclee 			(void) printf(E_LINE);
29475169Slclee 			(void) printf("Invalid selection, try again.");
29485169Slclee 			continue;
29490Sstevel@tonic-gate 		}
29500Sstevel@tonic-gate 	}
29510Sstevel@tonic-gate 
2952251Slclee 	(void) printf(E_LINE);
29530Sstevel@tonic-gate 
29540Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
29557563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
29567563SPrasad.Singamsetty@Sun.COM 		if ((dev_capacity > DK_MAX_2TB))
29577563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
29587563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
29597563SPrasad.Singamsetty@Sun.COM 
29600Sstevel@tonic-gate 		/* create the new partition */
29610Sstevel@tonic-gate 		i = specify(tsystid);
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 		if (i != -1) {
29640Sstevel@tonic-gate 			/* see if it should be the active partition */
2965251Slclee 			(void) printf(E_LINE);
2966251Slclee 			(void) printf(Q_LINE);
2967251Slclee 
2968251Slclee 			(void) printf(
2969251Slclee 			    "Should this become the active partition? If "
2970251Slclee 			    "yes, it  will be activated\n"
2971251Slclee 			    "each time the computer is reset or turned on.\n"
2972251Slclee 			    "Please type \"y\" or \"n\". ");
29730Sstevel@tonic-gate 
29740Sstevel@tonic-gate 			if (yesno()) {
2975251Slclee 				(void) printf(E_LINE);
29760Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
29770Sstevel@tonic-gate 					if (j == i) {
29780Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2979251Slclee 						(void) printf(E_LINE);
2980251Slclee 						(void) printf(
2981251Slclee 						    "Partition %d is now "
29820Sstevel@tonic-gate 						    "the active partition.",
2983251Slclee 						    j + 1);
29840Sstevel@tonic-gate 					} else {
29850Sstevel@tonic-gate 						Table[j].bootid = 0;
29860Sstevel@tonic-gate 					}
29870Sstevel@tonic-gate 				}
29880Sstevel@tonic-gate 			} else {
29890Sstevel@tonic-gate 				Table[i].bootid = 0;
29900Sstevel@tonic-gate 			}
29910Sstevel@tonic-gate 
299210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
299310021SSheshadri.Vasudevan@Sun.COM 			/*
299410021SSheshadri.Vasudevan@Sun.COM 			 * If partition created is an extended partition, null
299510021SSheshadri.Vasudevan@Sun.COM 			 * out the first sector of the first cylinder of the
299610021SSheshadri.Vasudevan@Sun.COM 			 * extended partition
299710021SSheshadri.Vasudevan@Sun.COM 			 */
299810021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended(Table[i].systid)) {
299910021SSheshadri.Vasudevan@Sun.COM 				fdisk_init_ext_part(epp,
300010021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].relsect),
300110021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].numsect));
300210021SSheshadri.Vasudevan@Sun.COM 			}
300310021SSheshadri.Vasudevan@Sun.COM #endif
30040Sstevel@tonic-gate 			/* set up the return code */
30050Sstevel@tonic-gate 			i = 1;
30060Sstevel@tonic-gate 		}
30070Sstevel@tonic-gate 	} else {
30080Sstevel@tonic-gate 		/*
30090Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
30100Sstevel@tonic-gate 		 * the table
30110Sstevel@tonic-gate 		 *
30120Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
30130Sstevel@tonic-gate 		 * empty
30140Sstevel@tonic-gate 		 */
30150Sstevel@tonic-gate 		retCode = verify_tbl();
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 		if (retCode < 0) {
3018251Slclee 			(void) fprintf(stderr,
30190Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
30200Sstevel@tonic-gate 			    "current partition table is invalid.\n");
30210Sstevel@tonic-gate 			return (-1);
30220Sstevel@tonic-gate 		} else if (retCode > 0) {
3023251Slclee 			(void) printf(
3024251Slclee 			    "An EFI partition must be the only partition on "
3025251Slclee 			    "disk.  You may manually delete existing\n"
3026251Slclee 			    "partitions, or fdisk can do it.\n"
3027251Slclee 			    "Do you want fdisk to destroy existing "
3028251Slclee 			    "partitions?\n"
3029251Slclee 			    "Please type \"y\" or \"n\". ");
30300Sstevel@tonic-gate 
30310Sstevel@tonic-gate 			if (yesno()) {
30320Sstevel@tonic-gate 				nulltbl();
30330Sstevel@tonic-gate 			} else {
30340Sstevel@tonic-gate 				return (-1);
30350Sstevel@tonic-gate 			}
30360Sstevel@tonic-gate 		}
30370Sstevel@tonic-gate 
30380Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
30397563SPrasad.Singamsetty@Sun.COM 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
30407563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
30417563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity - 1));
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 		if (i != 0) {
3044251Slclee 			(void) printf("Error creating EFI partition!!!\n");
30450Sstevel@tonic-gate 			i = -1;
30460Sstevel@tonic-gate 		} else {
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 			/* EFI partitions are currently never active */
30490Sstevel@tonic-gate 			Table[i].bootid = 0;
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 			/* set up the return code */
30520Sstevel@tonic-gate 			i = 1;
30530Sstevel@tonic-gate 		}
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	return (i);
30570Sstevel@tonic-gate }
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate /*
30600Sstevel@tonic-gate  * specify
30610Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
30620Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
30630Sstevel@tonic-gate  * cylinder and length in cylinders.
30640Sstevel@tonic-gate  */
3065251Slclee static int
3066251Slclee specify(uchar_t tsystid)
30670Sstevel@tonic-gate {
30685169Slclee 	int	i, j, percent = -1;
30697563SPrasad.Singamsetty@Sun.COM 	int	cyl, cylen;
30707563SPrasad.Singamsetty@Sun.COM 	diskaddr_t first_free, size_free;
30717563SPrasad.Singamsetty@Sun.COM 	diskaddr_t max_free;
30725169Slclee 	int	cyl_size;
30730Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
30740Sstevel@tonic-gate 
30755169Slclee 	cyl_size = heads * sectors;
30765169Slclee 
30775169Slclee 	/*
30785169Slclee 	 * make a local copy of the partition table
30795169Slclee 	 * and sort it into relsect order
30805169Slclee 	 */
30815169Slclee 	for (i = 0; i < FD_NUMPART; i++)
30825169Slclee 		partition[i] = &Table[i];
30835169Slclee 
30845169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
30855169Slclee 		if (partition[i]->systid == UNUSED)
30865169Slclee 			break;
30875169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
30885169Slclee 			if (partition[j]->systid == UNUSED)
30895169Slclee 				break;
309010021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(partition[j]->relsect) <
309110021SSheshadri.Vasudevan@Sun.COM 			    LE_32(partition[i]->relsect)) {
30925169Slclee 				struct ipart *temp = partition[i];
30935169Slclee 				partition[i] = partition[j];
30945169Slclee 				partition[j] = temp;
30955169Slclee 			}
30965169Slclee 		}
30975169Slclee 	}
30985169Slclee 
30997563SPrasad.Singamsetty@Sun.COM 
3100251Slclee 	(void) printf(Q_LINE);
3101251Slclee 	(void) printf(
3102251Slclee 	    "Specify the percentage of disk to use for this partition\n"
3103251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
31049786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
31050Sstevel@tonic-gate 	rm_blanks(s);
31060Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
31075169Slclee 		i = 0;
31089786SBarry.Harding@Sun.COM 		while ((s[i] != '\0') && (s[i] != '\n')) {
31095169Slclee 			if (s[i] < '0' || s[i] > '9') {
31105169Slclee 				(void) printf(E_LINE);
31115169Slclee 				(void) printf("Invalid percentage value "
31125169Slclee 				    "specified; retry the operation.");
31135169Slclee 				return (-1);
31145169Slclee 			}
31155169Slclee 			i++;
31165169Slclee 			if (i > 3) {
31175169Slclee 				(void) printf(E_LINE);
31185169Slclee 				(void) printf("Invalid percentage value "
31195169Slclee 				    "specified; retry the operation.");
31205169Slclee 				return (-1);
31215169Slclee 			}
31225169Slclee 		}
31235169Slclee 		if ((percent = atoi(s)) > 100) {
31245169Slclee 			(void) printf(E_LINE);
31255169Slclee 			(void) printf(
31265169Slclee 			    "Percentage value is too large. The value must be"
31275169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31285169Slclee 			return (-1);
31290Sstevel@tonic-gate 		}
31305169Slclee 		if (percent < 1) {
31315169Slclee 			(void) printf(E_LINE);
31325169Slclee 			(void) printf(
31335169Slclee 			    "Percentage value is too small. The value must be"
31345169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31355169Slclee 			return (-1);
31365169Slclee 		}
31375169Slclee 
31385169Slclee 		if (percent == 100)
31397563SPrasad.Singamsetty@Sun.COM 			cylen = Numcyl_usable - 1;
31405169Slclee 		else
31417563SPrasad.Singamsetty@Sun.COM 			cylen = (Numcyl_usable * percent) / 100;
31425169Slclee 
31435169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
31445169Slclee 		if ((tsystid == DOSOS12) &&
31455169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
31465169Slclee 			int n;
31477563SPrasad.Singamsetty@Sun.COM 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
31485169Slclee 			(void) printf(E_LINE);
31495169Slclee 			(void) printf("Maximum size for a DOS partition "
31505169Slclee 			    "is %d%%; retry the operation.",
31515169Slclee 			    n <= 100 ? n : 100);
31525169Slclee 			return (-1);
31530Sstevel@tonic-gate 		}
31545169Slclee 
31555169Slclee 
31565169Slclee 		max_free = 0;
31575169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
31585169Slclee 
31595169Slclee 			/*
31605169Slclee 			 * check for free space before partition i
31615169Slclee 			 * where i varies from 0 to 3
31625169Slclee 			 *
31635169Slclee 			 * freespace after partition 3 is unusable
31645169Slclee 			 * because there are no free partitions
31655169Slclee 			 *
31665169Slclee 			 * freespace begins at the end of previous partition
31675169Slclee 			 * or cylinder 1
31685169Slclee 			 */
31695169Slclee 			if (i) {
31705169Slclee 				/* Not an empty table */
317110021SSheshadri.Vasudevan@Sun.COM 				first_free = LE_32(partition[i - 1]->relsect) +
317210021SSheshadri.Vasudevan@Sun.COM 				    LE_32(partition[i - 1]->numsect);
31735169Slclee 			} else {
31745169Slclee 				first_free = cyl_size;
31755169Slclee 			}
31765169Slclee 
31775169Slclee 			/*
31785169Slclee 			 * freespace ends before the current partition
31795169Slclee 			 * or the end of the disk (chs end)
31805169Slclee 			 */
31815169Slclee 			if (partition[i]->systid == UNUSED) {
31825169Slclee 				size_free = chs_capacity - first_free;
31835169Slclee 			} else {
31848148SShidokht.Yadegari@Sun.COM 				/*
31858148SShidokht.Yadegari@Sun.COM 				 * Partition might start before cylinder 1.
31868148SShidokht.Yadegari@Sun.COM 				 * Make sure free space is not negative.
31878148SShidokht.Yadegari@Sun.COM 				 */
31885169Slclee 				size_free =
318910021SSheshadri.Vasudevan@Sun.COM 				    (LE_32(partition[i]->relsect > first_free))
319010021SSheshadri.Vasudevan@Sun.COM 				    ? (LE_32(partition[i]->relsect) -
319110021SSheshadri.Vasudevan@Sun.COM 				    first_free) : 0;
31925169Slclee 			}
31935169Slclee 
31945169Slclee 			/* save largest free space */
31955169Slclee 			if (max_free < size_free)
31965169Slclee 				max_free = size_free;
31975169Slclee 
31987563SPrasad.Singamsetty@Sun.COM 			if (((uint64_t)cylen * cyl_size) <= size_free) {
31995169Slclee 				/* We found a place to use */
32005169Slclee 				break;
32015169Slclee 			}
32025169Slclee 			if (partition[i]->systid == UNUSED) {
32035169Slclee 				(void) printf(E_LINE);
32045169Slclee 				max_free /= (cyl_size);
32055169Slclee 				(void) fprintf(stderr, "fdisk: "
32067563SPrasad.Singamsetty@Sun.COM 				    "Maximum percentage available is %lld\n",
32077563SPrasad.Singamsetty@Sun.COM 				    100 * max_free / Numcyl_usable);
32085169Slclee 				return (-1);
32095169Slclee 			}
32100Sstevel@tonic-gate 		}
32115169Slclee 
32125169Slclee 		(void) printf(E_LINE);
32135169Slclee 		if (i >= FD_NUMPART) {
32145169Slclee 			(void) fprintf(stderr,
32155169Slclee 			    "fdisk: Partition table is full.\n");
32165169Slclee 			return (-1);
32175169Slclee 		}
32185169Slclee 
32195169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
32205169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
32215169Slclee 			return (i);
32225169Slclee 		}
32235169Slclee 		return (-1);
32245169Slclee 	} else {
32255169Slclee 
32265169Slclee 		/* Specifying size in cylinders */
3227251Slclee 		(void) printf(E_LINE);
32285169Slclee 		(void) printf(Q_LINE);
32295169Slclee 		(void) printf("Enter starting cylinder number: ");
32305169Slclee 		if ((cyl = getcyl()) == -1) {
32315169Slclee 			(void) printf(E_LINE);
32325169Slclee 			(void) printf("Invalid number; retry the operation.");
32335169Slclee 			return (-1);
32345169Slclee 		}
32355169Slclee 		if (cyl == 0) {
32365169Slclee 			(void) printf(E_LINE);
32375169Slclee 			(void) printf(
32385169Slclee 			    "New partition cannot start at cylinder 0.\n");
32395169Slclee 			return (-1);
32405169Slclee 		}
32417563SPrasad.Singamsetty@Sun.COM 
32427563SPrasad.Singamsetty@Sun.COM 
32437563SPrasad.Singamsetty@Sun.COM 		if (cyl >= Numcyl_usable) {
32445169Slclee 			(void) printf(E_LINE);
32455169Slclee 			(void) printf(
32465169Slclee 			    "Cylinder %d is out of bounds, "
32475169Slclee 			    "the maximum is %d.\n",
32487563SPrasad.Singamsetty@Sun.COM 			    cyl, Numcyl_usable - 1);
32495169Slclee 			return (-1);
32505169Slclee 		}
32517563SPrasad.Singamsetty@Sun.COM 
32525169Slclee 		(void) printf(Q_LINE);
32535169Slclee 		(void) printf("Enter partition size in cylinders: ");
32545169Slclee 		if ((cylen = getcyl()) == -1) {
32555169Slclee 			(void) printf(E_LINE);
32565169Slclee 			(void) printf("Invalid number, retry the operation.");
32575169Slclee 			return (-1);
32585169Slclee 		}
32595169Slclee 
32605169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
32615169Slclee 			uint32_t	t_relsect, t_numsect;
32625169Slclee 
32635169Slclee 			if (partition[i]->systid == UNUSED)
32645169Slclee 				break;
326510021SSheshadri.Vasudevan@Sun.COM 			t_relsect = LE_32(partition[i]->relsect);
326610021SSheshadri.Vasudevan@Sun.COM 			t_numsect = LE_32(partition[i]->numsect);
32675169Slclee 
32685169Slclee 			if (cyl * cyl_size >= t_relsect &&
32695169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
32705169Slclee 				(void) printf(E_LINE);
32715169Slclee 				(void) printf(
32725169Slclee 				    "Cylinder %d is already allocated"
32735169Slclee 				    "\nretry the operation.",
32745169Slclee 				    cyl);
32755169Slclee 				return (-1);
32765169Slclee 			}
32775169Slclee 
32785169Slclee 			if (cyl * cyl_size < t_relsect &&
32795169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
32805169Slclee 				(void) printf(E_LINE);
32815169Slclee 				(void) printf(
32825169Slclee 				    "Maximum size for partition is %u cylinders"
32835169Slclee 				    "\nretry the operation.",
32845169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
32855169Slclee 				return (-1);
32865169Slclee 			}
32875169Slclee 		}
32885169Slclee 
32897563SPrasad.Singamsetty@Sun.COM 		/* Verify partition doesn't exceed disk size or 2 TB */
32907563SPrasad.Singamsetty@Sun.COM 		if (cyl + cylen > Numcyl_usable) {
32915169Slclee 			(void) printf(E_LINE);
32927563SPrasad.Singamsetty@Sun.COM 			if (Numcyl > Numcyl_usable) {
32937563SPrasad.Singamsetty@Sun.COM 				(void) printf(
32947563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
32957563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
32967563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
32977563SPrasad.Singamsetty@Sun.COM 			} else {
32987563SPrasad.Singamsetty@Sun.COM 				(void) printf(
32997563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
33007563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
33017563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
33027563SPrasad.Singamsetty@Sun.COM 			}
33035169Slclee 			return (-1);
33045169Slclee 		}
33055169Slclee 
33065169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
33075169Slclee 		if ((tsystid == DOSOS12) &&
33085169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
33095169Slclee 			(void) printf(E_LINE);
33105169Slclee 			(void) printf(
33115169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
33125169Slclee 			    "\nretry the operation.",
33135169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
33145169Slclee 			return (-1);
33155169Slclee 		}
33165169Slclee 
3317251Slclee 		(void) printf(E_LINE);
33185169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
33195169Slclee 		    cyl * cyl_size, cylen * cyl_size);
33205169Slclee 		if (i < 0)
33215169Slclee 			return (-1);
33225169Slclee 
33235169Slclee 		if (verify_tbl() < 0) {
33245169Slclee 			(void) printf(E_LINE);
33255169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
33265169Slclee 			return (-1);
33275169Slclee 		}
33285169Slclee 
33295169Slclee 		return (i);
33300Sstevel@tonic-gate 	}
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate 
33330Sstevel@tonic-gate /*
33340Sstevel@tonic-gate  * dispmenu
33350Sstevel@tonic-gate  * Display command menu (interactive mode).
33360Sstevel@tonic-gate  */
3337251Slclee static void
3338251Slclee dispmenu(void)
33390Sstevel@tonic-gate {
3340251Slclee 	(void) printf(M_LINE);
334110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
334210021SSheshadri.Vasudevan@Sun.COM 	(void) printf(
334310021SSheshadri.Vasudevan@Sun.COM 	    "SELECT ONE OF THE FOLLOWING:\n"
334410021SSheshadri.Vasudevan@Sun.COM 	    "   1. Create a partition\n"
334510021SSheshadri.Vasudevan@Sun.COM 	    "   2. Specify the active partition\n"
334610021SSheshadri.Vasudevan@Sun.COM 	    "   3. Delete a partition\n"
334710021SSheshadri.Vasudevan@Sun.COM 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
334810021SSheshadri.Vasudevan@Sun.COM 	    "   5. Edit/View extended partitions\n"
334910021SSheshadri.Vasudevan@Sun.COM 	    "   6. Exit (update disk configuration and exit)\n"
335010021SSheshadri.Vasudevan@Sun.COM 	    "   7. Cancel (exit without updating disk configuration)\n");
335110021SSheshadri.Vasudevan@Sun.COM #else
3352251Slclee 	(void) printf(
3353251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
3354251Slclee 	    "   1. Create a partition\n"
3355251Slclee 	    "   2. Specify the active partition\n"
3356251Slclee 	    "   3. Delete a partition\n"
3357251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
3358251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
3359251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
336010021SSheshadri.Vasudevan@Sun.COM #endif
33610Sstevel@tonic-gate }
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate /*
33640Sstevel@tonic-gate  * pchange
33650Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
33660Sstevel@tonic-gate  */
3367251Slclee static int
3368251Slclee pchange(void)
33690Sstevel@tonic-gate {
33700Sstevel@tonic-gate 	char s[80];
33710Sstevel@tonic-gate 	int i, j;
33720Sstevel@tonic-gate 
3373251Slclee 	for (;;) {
3374251Slclee 		(void) printf(Q_LINE);
33750Sstevel@tonic-gate 			{
3376251Slclee 			(void) printf(
3377251Slclee 			    "Specify the partition number to boot from"
33780Sstevel@tonic-gate 			    " (or specify 0 for none): ");
33790Sstevel@tonic-gate 			}
33809786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
33810Sstevel@tonic-gate 		rm_blanks(s);
33829786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
33839786SBarry.Harding@Sun.COM 		    (s[0] < '0') || (s[0] > '4')) {
3384251Slclee 			(void) printf(E_LINE);
3385251Slclee 			(void) printf(
3386251Slclee 			    "Invalid response, please specify a number"
33870Sstevel@tonic-gate 			    " between 0 and 4.\n");
33880Sstevel@tonic-gate 		} else {
33890Sstevel@tonic-gate 			break;
33900Sstevel@tonic-gate 		}
33910Sstevel@tonic-gate 	}
33920Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
33930Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
33940Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
33950Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
33960Sstevel@tonic-gate 				Table[i].bootid = 0;
33970Sstevel@tonic-gate 		}
3398251Slclee 		(void) printf(E_LINE);
3399251Slclee 			(void) printf(
3400251Slclee 			    "No partition is currently marked as active.");
34010Sstevel@tonic-gate 		return (0);
34020Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
34030Sstevel@tonic-gate 		i = s[0] - '1';
34040Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
3405251Slclee 			(void) printf(E_LINE);
3406251Slclee 			(void) printf("Partition does not exist.");
34070Sstevel@tonic-gate 			return (-1);
34080Sstevel@tonic-gate 		}
34090Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
34100Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
34110Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
34120Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
3413251Slclee 			(void) printf(E_LINE);
3414251Slclee 			(void) printf(
3415251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
34160Sstevel@tonic-gate 			    "cannot be made active.\n");
3417251Slclee 			(void) printf("Select another partition.");
34180Sstevel@tonic-gate 			return (-1);
34190Sstevel@tonic-gate 		}
34200Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
34210Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
34220Sstevel@tonic-gate 			if (j != i)
34230Sstevel@tonic-gate 			Table[j].bootid = 0;
34240Sstevel@tonic-gate 		}
34250Sstevel@tonic-gate 	}
3426251Slclee 	(void) printf(E_LINE);
34270Sstevel@tonic-gate 		{
3428251Slclee 		(void) printf(
3429251Slclee 		    "Partition %d is now active. The system will start up"
3430251Slclee 		    " from this\n", i + 1);
3431251Slclee 		(void) printf("partition after the next reboot.");
34320Sstevel@tonic-gate 		}
34330Sstevel@tonic-gate 	return (1);
34340Sstevel@tonic-gate }
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate /*
34370Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
34380Sstevel@tonic-gate  */
3439251Slclee static int
3440251Slclee ppartid(void)
34410Sstevel@tonic-gate {
34420Sstevel@tonic-gate 	char	*p, s[80];
34430Sstevel@tonic-gate 	int	i;
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 	for (;;) {
3446251Slclee 		(void) printf(Q_LINE);
3447251Slclee 		(void) printf("Specify the partition number to change"
34485169Slclee 		    " (or enter 0 to exit): ");
3449251Slclee 		if (!fgets(s, sizeof (s), stdin))
3450251Slclee 			return (1);
34510Sstevel@tonic-gate 		i = strtol(s, &p, 10);
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
3454251Slclee 			(void) printf(E_LINE);
3455251Slclee 			(void) printf(
3456251Slclee 			    "Invalid response, retry the operation.\n");
34570Sstevel@tonic-gate 			continue;
34580Sstevel@tonic-gate 		}
34590Sstevel@tonic-gate 
34600Sstevel@tonic-gate 		if (i == 0) {
34610Sstevel@tonic-gate 			/* exit delete command */
3462251Slclee 			(void) printf(E_LINE); /* clear error message */
34630Sstevel@tonic-gate 			return (1);
34640Sstevel@tonic-gate 		}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 		i -= 1;
34670Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
34680Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
34690Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
34700Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
34710Sstevel@tonic-gate 		} else {
3472251Slclee 			(void) printf(E_LINE);
3473251Slclee 			(void) printf(
3474251Slclee 			    "Partition %d is not a Solaris partition.",
34750Sstevel@tonic-gate 			    i + 1);
34760Sstevel@tonic-gate 			continue;
34770Sstevel@tonic-gate 		}
34780Sstevel@tonic-gate 
3479251Slclee 		(void) printf(E_LINE);
3480251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
34810Sstevel@tonic-gate 		return (1);
34820Sstevel@tonic-gate 	}
34830Sstevel@tonic-gate }
34840Sstevel@tonic-gate 
34850Sstevel@tonic-gate /*
34860Sstevel@tonic-gate  * pdelete
34870Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
34880Sstevel@tonic-gate  */
3489251Slclee static char
3490251Slclee pdelete(void)
34910Sstevel@tonic-gate {
34920Sstevel@tonic-gate 	char s[80];
34930Sstevel@tonic-gate 	int i, j;
34940Sstevel@tonic-gate 	char pactive;
34950Sstevel@tonic-gate 
3496251Slclee DEL1:	(void) printf(Q_LINE);
3497251Slclee 	(void) printf("Specify the partition number to delete"
34980Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
34999786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
35000Sstevel@tonic-gate 	rm_blanks(s);
35010Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
3502251Slclee 		(void) printf(E_LINE);	/* clear error message */
35030Sstevel@tonic-gate 		return (1);
35040Sstevel@tonic-gate 	}
35050Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
35069786SBarry.Harding@Sun.COM 	if (((s[1] != '\0') && (s[1] != '\n')) ||
35079786SBarry.Harding@Sun.COM 	    (i = atoi(s)) < 1 || i > FD_NUMPART) {
3508251Slclee 		(void) printf(E_LINE);
3509251Slclee 		(void) printf("Invalid response, retry the operation.\n");
35100Sstevel@tonic-gate 		goto DEL1;
35110Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
35120Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
35130Sstevel@tonic-gate 	}
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
3516251Slclee 		(void) printf(E_LINE);
3517251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
35180Sstevel@tonic-gate 		return (-1);
35190Sstevel@tonic-gate 	}
35200Sstevel@tonic-gate 
352110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
352210021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_is_dos_extended(Table[i].systid) &&
352310021SSheshadri.Vasudevan@Sun.COM 	    (Table[i].relsect == fdisk_get_ext_beg_sec(epp)) &&
352410021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_logical_drive_count(epp)) {
352510021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
352610021SSheshadri.Vasudevan@Sun.COM 		(void) printf("There are logical drives inside the"
352710021SSheshadri.Vasudevan@Sun.COM 		    " extended partition\n");
352810021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure of proceeding with deletion ?"
352910021SSheshadri.Vasudevan@Sun.COM 		    " (type \"y\" or \"n\") ");
353010021SSheshadri.Vasudevan@Sun.COM 
353110021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
353210021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
353310021SSheshadri.Vasudevan@Sun.COM 			return (1);
353410021SSheshadri.Vasudevan@Sun.COM 		}
353510021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_mounted_logical_drives(epp) == FDISK_EMOUNTED) {
353610021SSheshadri.Vasudevan@Sun.COM 			(void) printf(Q_LINE);
353710021SSheshadri.Vasudevan@Sun.COM 			(void) printf("There are mounted logical drives. "
353810021SSheshadri.Vasudevan@Sun.COM 			    "Committing changes now can cause data loss or "
353910021SSheshadri.Vasudevan@Sun.COM 			    "corruption. Unmount all logical drives and then "
354010021SSheshadri.Vasudevan@Sun.COM 			    "try committing the changes again.\n");
354110021SSheshadri.Vasudevan@Sun.COM 			(void) printf("Press enter to continue.\n");
354210021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(s);
354310021SSheshadri.Vasudevan@Sun.COM 			return (1);
354410021SSheshadri.Vasudevan@Sun.COM 		}
354510021SSheshadri.Vasudevan@Sun.COM 		fdisk_delete_ext_part(epp);
354610021SSheshadri.Vasudevan@Sun.COM 	} else {
354710021SSheshadri.Vasudevan@Sun.COM #endif
354810021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
354910021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure you want to delete partition %d?"
355010021SSheshadri.Vasudevan@Sun.COM 		    " This will make all files and \n", i + 1);
355110021SSheshadri.Vasudevan@Sun.COM 		(void) printf("programs in this partition inaccessible (type"
355210021SSheshadri.Vasudevan@Sun.COM 		    " \"y\" or \"n\"). ");
355310021SSheshadri.Vasudevan@Sun.COM 
355410021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
355510021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
355610021SSheshadri.Vasudevan@Sun.COM 			return (1);
355710021SSheshadri.Vasudevan@Sun.COM 		}
355810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
35590Sstevel@tonic-gate 	}
356010021SSheshadri.Vasudevan@Sun.COM #endif
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
35630Sstevel@tonic-gate 		pactive = 1;
35640Sstevel@tonic-gate 	} else {
35650Sstevel@tonic-gate 		pactive = 0;
35660Sstevel@tonic-gate 	}
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
35695169Slclee 		Table[j] = Table[j + 1];
35700Sstevel@tonic-gate 	}
35710Sstevel@tonic-gate 
35720Sstevel@tonic-gate 	Table[j].systid = UNUSED;
35730Sstevel@tonic-gate 	Table[j].numsect = 0;
35740Sstevel@tonic-gate 	Table[j].relsect = 0;
35750Sstevel@tonic-gate 	Table[j].bootid = 0;
3576251Slclee 	(void) printf(E_LINE);
3577251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 	if (pactive) {
35805169Slclee 		(void) printf(" This was the active partition.");
35810Sstevel@tonic-gate 	}
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 	return (1);
35840Sstevel@tonic-gate }
35850Sstevel@tonic-gate 
35860Sstevel@tonic-gate /*
35870Sstevel@tonic-gate  * rm_blanks
35880Sstevel@tonic-gate  * Remove blanks from strings of user responses.
35890Sstevel@tonic-gate  */
3590251Slclee static void
3591251Slclee rm_blanks(char *s)
35920Sstevel@tonic-gate {
35930Sstevel@tonic-gate 	register int i, j;
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
35960Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
35970Sstevel@tonic-gate 			continue;
35980Sstevel@tonic-gate 		else
35990Sstevel@tonic-gate 			/* Found first non-blank character of the string */
36000Sstevel@tonic-gate 			break;
36010Sstevel@tonic-gate 	}
36020Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
36030Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
36040Sstevel@tonic-gate 			/* Reached end of string */
36050Sstevel@tonic-gate 			return;
36060Sstevel@tonic-gate 		}
36070Sstevel@tonic-gate 	}
36080Sstevel@tonic-gate }
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate /*
36110Sstevel@tonic-gate  * getcyl
36120Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
36130Sstevel@tonic-gate  * string to a decimal value.
36140Sstevel@tonic-gate  */
3615251Slclee static int
3616251Slclee getcyl(void)
36170Sstevel@tonic-gate {
36180Sstevel@tonic-gate int slen, i, j;
36190Sstevel@tonic-gate unsigned int cyl;
36209786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
36210Sstevel@tonic-gate 	rm_blanks(s);
36220Sstevel@tonic-gate 	slen = strlen(s);
36239786SBarry.Harding@Sun.COM 	if (s[slen - 1] == '\n')
36249786SBarry.Harding@Sun.COM 		slen--;
36250Sstevel@tonic-gate 	j = 1;
36260Sstevel@tonic-gate 	cyl = 0;
3627251Slclee 	for (i = slen - 1; i >= 0; i--) {
36280Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
36290Sstevel@tonic-gate 			return (-1);
36300Sstevel@tonic-gate 		}
3631251Slclee 		cyl += (j * (s[i] - '0'));
36320Sstevel@tonic-gate 		j *= 10;
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate 	return (cyl);
36350Sstevel@tonic-gate }
36360Sstevel@tonic-gate 
36370Sstevel@tonic-gate /*
36380Sstevel@tonic-gate  * disptbl
36390Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
36400Sstevel@tonic-gate  * of the disk used for each partition.
36410Sstevel@tonic-gate  */
3642251Slclee static void
3643251Slclee disptbl(void)
36440Sstevel@tonic-gate {
36450Sstevel@tonic-gate 	int i;
36460Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
36470Sstevel@tonic-gate 	char *stat, *type;
36487563SPrasad.Singamsetty@Sun.COM 	int is_pmbr = 0;
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
3651251Slclee 		(void) printf("WARNING: critical disk geometry information"
36525169Slclee 		    " missing!\n");
3653251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
36540Sstevel@tonic-gate 		exit(1);
36550Sstevel@tonic-gate 	}
36560Sstevel@tonic-gate 
3657251Slclee 	(void) printf(HOME);
3658251Slclee 	(void) printf(T_LINE);
3659251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
36609889SLarry.Liu@Sun.COM 	(void) printf("             Cylinder size is %d (%d byte) blocks\n\n",
36619889SLarry.Liu@Sun.COM 	    heads * sectors, sectsiz);
3662251Slclee 	(void) printf(
3663251Slclee 	    "                                               Cylinders\n");
3664251Slclee 	(void) printf(
3665251Slclee 	    "      Partition   Status    Type          Start   End   Length"
36660Sstevel@tonic-gate 	    "    %%\n");
3667251Slclee 	(void) printf(
3668251Slclee 	    "      =========   ======    ============  =====   ===   ======"
36690Sstevel@tonic-gate 	    "   ===");
36700Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
36710Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
3672251Slclee 			(void) printf("\n");
3673251Slclee 			(void) printf(CLR_LIN);
36740Sstevel@tonic-gate 			continue;
36750Sstevel@tonic-gate 		}
36760Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
36775169Slclee 			stat = Actvstr;
36780Sstevel@tonic-gate 		else
36795169Slclee 			stat = NAstr;
36800Sstevel@tonic-gate 		switch (Table[i].systid) {
36810Sstevel@tonic-gate 		case UNIXOS:
36825169Slclee 			type = Ustr;
36835169Slclee 			break;
36840Sstevel@tonic-gate 		case SUNIXOS:
36855169Slclee 			type = SUstr;
3686*10682SSheshadri.Vasudevan@Sun.COM #ifdef i386
3687*10682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, Table[i].relsect,
3688*10682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
3689*10682SSheshadri.Vasudevan@Sun.COM 				type = LINSWAPstr;
3690*10682SSheshadri.Vasudevan@Sun.COM #endif
36915169Slclee 			break;
36920Sstevel@tonic-gate 		case SUNIXOS2:
36935169Slclee 			type = SU2str;
36945169Slclee 			break;
36950Sstevel@tonic-gate 		case X86BOOT:
36965169Slclee 			type = X86str;
36975169Slclee 			break;
36980Sstevel@tonic-gate 		case DOSOS12:
36995169Slclee 			type = Dstr;
37005169Slclee 			break;
37010Sstevel@tonic-gate 		case DOSOS16:
37025169Slclee 			type = D16str;
37035169Slclee 			break;
37040Sstevel@tonic-gate 		case EXTDOS:
37055169Slclee 			type = EDstr;
37065169Slclee 			break;
37070Sstevel@tonic-gate 		case DOSDATA:
37085169Slclee 			type = DDstr;
37095169Slclee 			break;
37100Sstevel@tonic-gate 		case DOSHUGE:
37115169Slclee 			type = DBstr;
37125169Slclee 			break;
37130Sstevel@tonic-gate 		case PCIXOS:
37145169Slclee 			type = PCstr;
37155169Slclee 			break;
37160Sstevel@tonic-gate 		case DIAGPART:
37175169Slclee 			type = DIAGstr;
37185169Slclee 			break;
37190Sstevel@tonic-gate 		case FDISK_IFS:
37205169Slclee 			type = IFSstr;
37215169Slclee 			break;
37220Sstevel@tonic-gate 		case FDISK_AIXBOOT:
37235169Slclee 			type = AIXstr;
37245169Slclee 			break;
37250Sstevel@tonic-gate 		case FDISK_AIXDATA:
37265169Slclee 			type = AIXDstr;
37275169Slclee 			break;
37280Sstevel@tonic-gate 		case FDISK_OS2BOOT:
37295169Slclee 			type = OS2str;
37305169Slclee 			break;
37310Sstevel@tonic-gate 		case FDISK_WINDOWS:
37325169Slclee 			type = WINstr;
37335169Slclee 			break;
37340Sstevel@tonic-gate 		case FDISK_EXT_WIN:
37355169Slclee 			type = EWINstr;
37365169Slclee 			break;
37370Sstevel@tonic-gate 		case FDISK_FAT95:
37385169Slclee 			type = FAT95str;
37395169Slclee 			break;
37400Sstevel@tonic-gate 		case FDISK_EXTLBA:
37415169Slclee 			type = EXTLstr;
37425169Slclee 			break;
37430Sstevel@tonic-gate 		case FDISK_LINUX:
37445169Slclee 			type = LINUXstr;
37455169Slclee 			break;
37460Sstevel@tonic-gate 		case FDISK_CPM:
37475169Slclee 			type = CPMstr;
37485169Slclee 			break;
374910021SSheshadri.Vasudevan@Sun.COM 		case FDISK_NOVELL2:
375010021SSheshadri.Vasudevan@Sun.COM 			type = NOV2str;
375110021SSheshadri.Vasudevan@Sun.COM 			break;
37520Sstevel@tonic-gate 		case FDISK_NOVELL3:
37535169Slclee 			type = NOVstr;
37545169Slclee 			break;
37550Sstevel@tonic-gate 		case FDISK_QNX4:
37565169Slclee 			type = QNXstr;
37575169Slclee 			break;
37580Sstevel@tonic-gate 		case FDISK_QNX42:
37595169Slclee 			type = QNX2str;
37605169Slclee 			break;
37610Sstevel@tonic-gate 		case FDISK_QNX43:
37625169Slclee 			type = QNX3str;
37635169Slclee 			break;
37640Sstevel@tonic-gate 		case FDISK_LINUXNAT:
37655169Slclee 			type = LINNATstr;
37665169Slclee 			break;
37670Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
37685169Slclee 			type = NTFSVOL1str;
37695169Slclee 			break;
37700Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
37715169Slclee 			type = NTFSVOL2str;
37725169Slclee 			break;
37730Sstevel@tonic-gate 		case FDISK_BSD:
37745169Slclee 			type = BSDstr;
37755169Slclee 			break;
37760Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
37775169Slclee 			type = NEXTSTEPstr;
37785169Slclee 			break;
37790Sstevel@tonic-gate 		case FDISK_BSDIFS:
37805169Slclee 			type = BSDIFSstr;
37815169Slclee 			break;
37820Sstevel@tonic-gate 		case FDISK_BSDISWAP:
37835169Slclee 			type = BSDISWAPstr;
37845169Slclee 			break;
37850Sstevel@tonic-gate 		case EFI_PMBR:
37865169Slclee 			type = EFIstr;
378710021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) == DK_MAX_2TB)
37887563SPrasad.Singamsetty@Sun.COM 				is_pmbr = 1;
37897563SPrasad.Singamsetty@Sun.COM 
37905169Slclee 			break;
37910Sstevel@tonic-gate 		default:
37925169Slclee 			type = Ostr;
37935169Slclee 			break;
37940Sstevel@tonic-gate 		}
379510021SSheshadri.Vasudevan@Sun.COM 		startcyl = LE_32(Table[i].relsect) /
37965936Sbharding 		    (unsigned long)(heads * sectors);
37977563SPrasad.Singamsetty@Sun.COM 
379810021SSheshadri.Vasudevan@Sun.COM 		if (LE_32(Table[i].numsect) == DK_MAX_2TB) {
37997563SPrasad.Singamsetty@Sun.COM 			endcyl = Numcyl - 1;
38007563SPrasad.Singamsetty@Sun.COM 			length = endcyl - startcyl + 1;
38017563SPrasad.Singamsetty@Sun.COM 		} else {
380210021SSheshadri.Vasudevan@Sun.COM 			length = LE_32(Table[i].numsect) /
38037563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors);
380410021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) %
38057563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors))
38067563SPrasad.Singamsetty@Sun.COM 				length++;
38077563SPrasad.Singamsetty@Sun.COM 			endcyl = startcyl + length - 1;
38087563SPrasad.Singamsetty@Sun.COM 		}
38097563SPrasad.Singamsetty@Sun.COM 
38107563SPrasad.Singamsetty@Sun.COM 		percent = length * 100 / Numcyl_usable;
38117563SPrasad.Singamsetty@Sun.COM 		if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
38127563SPrasad.Singamsetty@Sun.COM 			if ((remainder * 100 / Numcyl_usable) > 50) {
38130Sstevel@tonic-gate 				/* round up */
38140Sstevel@tonic-gate 				percent++;
38150Sstevel@tonic-gate 			}
38160Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
38170Sstevel@tonic-gate 			/* rounded down */
38180Sstevel@tonic-gate 		}
38190Sstevel@tonic-gate 		if (percent > 100)
38200Sstevel@tonic-gate 			percent = 100;
3821251Slclee 		(void) printf(
3822251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
3823251Slclee 		    "    %3d",
3824251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
38250Sstevel@tonic-gate 	}
38267563SPrasad.Singamsetty@Sun.COM 
38270Sstevel@tonic-gate 	/* Print warning message if table is empty */
38280Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
3829251Slclee 		(void) printf(W_LINE);
3830251Slclee 		(void) printf("WARNING: no partitions are defined!");
38310Sstevel@tonic-gate 	} else {
38320Sstevel@tonic-gate 		/* Clear the warning line */
3833251Slclee 		(void) printf(W_LINE);
38347563SPrasad.Singamsetty@Sun.COM 
38357563SPrasad.Singamsetty@Sun.COM 		/* Print warning if disk > 2TB and is not EFI PMBR */
38367563SPrasad.Singamsetty@Sun.COM 		if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
38377563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
38387563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate }
38410Sstevel@tonic-gate 
38420Sstevel@tonic-gate /*
38430Sstevel@tonic-gate  * print_Table
38440Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
38450Sstevel@tonic-gate  * the selected disk device.
38460Sstevel@tonic-gate  */
3847251Slclee static void
3848251Slclee print_Table(void)
3849251Slclee {
38500Sstevel@tonic-gate 	int i;
38510Sstevel@tonic-gate 
3852251Slclee 	(void) fprintf(stderr,
38530Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
38540Sstevel@tonic-gate 	    "   NUMSECT\n");
38550Sstevel@tonic-gate 
38560Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3857251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
3858251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
3859251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
3860251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
38615169Slclee 		(void) fprintf(stderr, "%-8d ",
38625169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
38630Sstevel@tonic-gate 
3864251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
3865251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
38665169Slclee 		(void) fprintf(stderr, "%-8d ",
38675169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
386810021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u ", LE_32(Table[i].relsect));
386910021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u\n", LE_32(Table[i].numsect));
38700Sstevel@tonic-gate 
38710Sstevel@tonic-gate 	}
38720Sstevel@tonic-gate }
38730Sstevel@tonic-gate 
38740Sstevel@tonic-gate /*
38750Sstevel@tonic-gate  * copy_Table_to_Old_Table
38760Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
38770Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
38780Sstevel@tonic-gate  * ones compared when determining if Table has changed.
38790Sstevel@tonic-gate  */
3880251Slclee static void
3881251Slclee copy_Table_to_Old_Table(void)
38820Sstevel@tonic-gate {
38830Sstevel@tonic-gate 	int i;
38840Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
38855169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
38860Sstevel@tonic-gate 	}
38870Sstevel@tonic-gate }
38880Sstevel@tonic-gate 
38890Sstevel@tonic-gate /*
38900Sstevel@tonic-gate  * nulltbl
38910Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
38920Sstevel@tonic-gate  * fdisk table.
38930Sstevel@tonic-gate  */
3894251Slclee static void
3895251Slclee nulltbl(void)
38960Sstevel@tonic-gate {
38970Sstevel@tonic-gate 	int i;
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
39005169Slclee 		Table[i].systid = UNUSED;
390110021SSheshadri.Vasudevan@Sun.COM 		Table[i].numsect = LE_32(UNUSED);
390210021SSheshadri.Vasudevan@Sun.COM 		Table[i].relsect = LE_32(UNUSED);
39035169Slclee 		Table[i].bootid = 0;
39048904SBarry.Harding@Sun.COM 		skip_verify[i] = 0;
39050Sstevel@tonic-gate 	}
39060Sstevel@tonic-gate }
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate /*
39090Sstevel@tonic-gate  * copy_Bootblk_to_Table
39100Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
39110Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
39120Sstevel@tonic-gate  */
3913251Slclee static void
3914251Slclee copy_Bootblk_to_Table(void)
39150Sstevel@tonic-gate {
39160Sstevel@tonic-gate 	int i, j;
39170Sstevel@tonic-gate 	char *bootptr;
39180Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
39190Sstevel@tonic-gate 
39200Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
3921251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
39220Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
392310021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(Bootblk->signature) != MBB_MAGIC)  {
39240Sstevel@tonic-gate 		/* Signature is missing */
39250Sstevel@tonic-gate 		nulltbl();
3926251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39270Sstevel@tonic-gate 		return;
39280Sstevel@tonic-gate 	}
39290Sstevel@tonic-gate 	/*
39300Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
39310Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
39320Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
39330Sstevel@tonic-gate 	 * those that are valid.
39340Sstevel@tonic-gate 	 */
39350Sstevel@tonic-gate 	j = 0;
39360Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
39370Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
39380Sstevel@tonic-gate 			/* Null entry */
39390Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
39400Sstevel@tonic-gate 		} else {
3941251Slclee 			fill_ipart(bootptr, &Table[j]);
39420Sstevel@tonic-gate 			j++;
39430Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
39440Sstevel@tonic-gate 		}
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
39470Sstevel@tonic-gate 		Table[i].systid = UNUSED;
394810021SSheshadri.Vasudevan@Sun.COM 		Table[i].numsect = LE_32(UNUSED);
394910021SSheshadri.Vasudevan@Sun.COM 		Table[i].relsect = LE_32(UNUSED);
39500Sstevel@tonic-gate 		Table[i].bootid = 0;
39510Sstevel@tonic-gate 
39520Sstevel@tonic-gate 	}
39530Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
3954251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39550Sstevel@tonic-gate 	copy_Table_to_Bootblk();
39560Sstevel@tonic-gate }
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate /*
39590Sstevel@tonic-gate  * fill_ipart
39600Sstevel@tonic-gate  * Initialize ipart structure values.
39610Sstevel@tonic-gate  */
3962251Slclee static void
39630Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
39640Sstevel@tonic-gate {
39650Sstevel@tonic-gate #ifdef sparc
39660Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
3967251Slclee 	partp->bootid	= getbyte(&bootptr);
3968251Slclee 	partp->beghead	= getbyte(&bootptr);
3969251Slclee 	partp->begsect	= getbyte(&bootptr);
3970251Slclee 	partp->begcyl	= getbyte(&bootptr);
3971251Slclee 	partp->systid	= getbyte(&bootptr);
3972251Slclee 	partp->endhead	= getbyte(&bootptr);
3973251Slclee 	partp->endsect	= getbyte(&bootptr);
3974251Slclee 	partp->endcyl	= getbyte(&bootptr);
3975251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
3976251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
39770Sstevel@tonic-gate #else
39780Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
39790Sstevel@tonic-gate #endif
39800Sstevel@tonic-gate }
39810Sstevel@tonic-gate 
39820Sstevel@tonic-gate /*
3983251Slclee  * getbyte, getlong
39840Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
39850Sstevel@tonic-gate  */
39860Sstevel@tonic-gate #ifdef sparc
3987251Slclee uchar_t
3988251Slclee getbyte(char **bp)
39890Sstevel@tonic-gate {
3990251Slclee 	uchar_t	b;
3991251Slclee 
3992251Slclee 	b = (uchar_t)**bp;
39930Sstevel@tonic-gate 	*bp = *bp + 1;
39940Sstevel@tonic-gate 	return (b);
39950Sstevel@tonic-gate }
39960Sstevel@tonic-gate 
3997251Slclee uint32_t
3998251Slclee getlong(char **bp)
39990Sstevel@tonic-gate {
4000251Slclee 	int32_t	b, bh, bl;
40010Sstevel@tonic-gate 
40020Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
40030Sstevel@tonic-gate 	*bp += 2;
40040Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
40050Sstevel@tonic-gate 	*bp += 2;
40060Sstevel@tonic-gate 
40070Sstevel@tonic-gate 	b = (bh << 16) | bl;
4008251Slclee 	return ((uint32_t)b);
40090Sstevel@tonic-gate }
40100Sstevel@tonic-gate #endif
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate /*
40130Sstevel@tonic-gate  * copy_Table_to_Bootblk
40149889SLarry.Liu@Sun.COM  * Copy the table into the boot record. Note that the unused
40150Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
40160Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
40170Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
40180Sstevel@tonic-gate  */
4019251Slclee static void
4020251Slclee copy_Table_to_Bootblk(void)
40210Sstevel@tonic-gate {
40220Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
40230Sstevel@tonic-gate 
40240Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
40250Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
40260Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
40270Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
40285169Slclee 		if (tbl_ptr->systid == UNUSED)
40295169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
40305169Slclee 		else
40315169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
40320Sstevel@tonic-gate 	}
403310021SSheshadri.Vasudevan@Sun.COM 	Bootblk->signature = LE_16(MBB_MAGIC);
40340Sstevel@tonic-gate }
40350Sstevel@tonic-gate 
40360Sstevel@tonic-gate /*
40370Sstevel@tonic-gate  * TableChanged
40380Sstevel@tonic-gate  * 	Check for any changes in the partition table.
40390Sstevel@tonic-gate  */
4040251Slclee static int
4041251Slclee TableChanged(void)
40420Sstevel@tonic-gate {
40430Sstevel@tonic-gate 	int i, changed;
40440Sstevel@tonic-gate 
40450Sstevel@tonic-gate 	changed = 0;
40460Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
40475169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
40485169Slclee 			/* Partition table changed, write back to disk */
40495169Slclee 			changed = 1;
40505169Slclee 		}
40510Sstevel@tonic-gate 	}
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate 	return (changed);
40540Sstevel@tonic-gate }
40550Sstevel@tonic-gate 
40560Sstevel@tonic-gate /*
40570Sstevel@tonic-gate  * ffile_write
40580Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
40590Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
40600Sstevel@tonic-gate  */
4061251Slclee static void
4062251Slclee ffile_write(char *file)
40630Sstevel@tonic-gate {
40640Sstevel@tonic-gate 	register int	i;
40650Sstevel@tonic-gate 	FILE *fp;
40660Sstevel@tonic-gate 
40670Sstevel@tonic-gate 	/*
40680Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
40690Sstevel@tonic-gate 	 * Open file and write it.
40700Sstevel@tonic-gate 	 */
40710Sstevel@tonic-gate 	if (file != (char *)stdout) {
40725169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
40735169Slclee 			(void) fprintf(stderr,
40745169Slclee 			    "fdisk: Cannot open output file %s.\n",
40755169Slclee 			    file);
40765169Slclee 			exit(1);
40775169Slclee 		}
40780Sstevel@tonic-gate 	}
40790Sstevel@tonic-gate 	else
40805169Slclee 		fp = stdout;
40810Sstevel@tonic-gate 
40820Sstevel@tonic-gate 	/*
40830Sstevel@tonic-gate 	 * Write the fdisk table information
40840Sstevel@tonic-gate 	 */
4085251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
4086251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
4087251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4088251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
4089251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
4090251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
4091251Slclee 	(void) fprintf(fp, "*\n");
40920Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
40930Sstevel@tonic-gate 	if (v_flag) {
4094251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
4095251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4096251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
4097251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
4098251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
4099251Slclee 		(void) fprintf(fp, "*\n");
41000Sstevel@tonic-gate 	}
4101251Slclee 	(void) fprintf(fp, "* systid:\n");
4102251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
4103251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
4104251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
4105251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
4106251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
4107251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
4108251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
4109251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
4110251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
4111251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
4112251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
4113251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
4114251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
4115251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
4116251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
4117251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
4118251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
4119251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
4120251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
412110021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(fp, "*  100: FDISK_NOVELL2\n");
4122251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
4123251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
4124251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
4125251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
4126251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
4127251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
4128251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
4129251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
4130251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
4131251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
4132251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
4133251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
4134251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
4135251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
4136251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
4137251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
4138251Slclee 	(void) fprintf(fp, "*\n");
4139251Slclee 	(void) fprintf(fp,
41400Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
41417563SPrasad.Singamsetty@Sun.COM 	    "    Rsect      Numsect\n");
41427563SPrasad.Singamsetty@Sun.COM 
41430Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
414410021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(fp,
414510021SSheshadri.Vasudevan@Sun.COM 		    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
414610021SSheshadri.Vasudevan@Sun.COM 		    " %-10u\n",
414710021SSheshadri.Vasudevan@Sun.COM 		    Table[i].systid,
414810021SSheshadri.Vasudevan@Sun.COM 		    Table[i].bootid,
414910021SSheshadri.Vasudevan@Sun.COM 		    Table[i].beghead,
415010021SSheshadri.Vasudevan@Sun.COM 		    Table[i].begsect & 0x3f,
415110021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
415210021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
415310021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endhead,
415410021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endsect & 0x3f,
415510021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
415610021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
415710021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].relsect),
415810021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].numsect));
415910021SSheshadri.Vasudevan@Sun.COM 	}
416010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
416110021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_ext_part_exists(epp)) {
416210021SSheshadri.Vasudevan@Sun.COM 		struct ipart ext_tab;
416310021SSheshadri.Vasudevan@Sun.COM 		logical_drive_t *temp;
416410021SSheshadri.Vasudevan@Sun.COM 		uint32_t rsect, numsect, tempsect = 0;
416510021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
416610021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
416710021SSheshadri.Vasudevan@Sun.COM 			ext_tab = temp->parts[0];
416810021SSheshadri.Vasudevan@Sun.COM 			rsect = tempsect + LE_32(ext_tab.relsect) +
416910021SSheshadri.Vasudevan@Sun.COM 			    fdisk_get_ext_beg_sec(epp);
417010021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(ext_tab.numsect);
417110021SSheshadri.Vasudevan@Sun.COM 			tempsect = LE_32(temp->parts[1].relsect);
4172251Slclee 			(void) fprintf(fp,
417310021SSheshadri.Vasudevan@Sun.COM 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d "
417410021SSheshadri.Vasudevan@Sun.COM 			    "%-7d %-8u %-8u\n",
417510021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.systid,
417610021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.bootid,
417710021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.beghead,
417810021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.begsect & 0x3f,
417910021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begcyl & 0xff) |
418010021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begsect & 0xc0) << 2)),
418110021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endhead,
418210021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endsect & 0x3f,
418310021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endcyl & 0xff) |
418410021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endsect & 0xc0) << 2)),
418510021SSheshadri.Vasudevan@Sun.COM 			    rsect,
418610021SSheshadri.Vasudevan@Sun.COM 			    numsect);
418710021SSheshadri.Vasudevan@Sun.COM 		}
41880Sstevel@tonic-gate 	}
418910021SSheshadri.Vasudevan@Sun.COM #endif
419010021SSheshadri.Vasudevan@Sun.COM 
41910Sstevel@tonic-gate 	if (fp != stdout)
4192251Slclee 		(void) fclose(fp);
41930Sstevel@tonic-gate }
41940Sstevel@tonic-gate 
41950Sstevel@tonic-gate /*
41960Sstevel@tonic-gate  * fix_slice
41970Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
41980Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
41990Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
42000Sstevel@tonic-gate  */
4201251Slclee static void
4202251Slclee fix_slice(void)
42030Sstevel@tonic-gate {
42040Sstevel@tonic-gate 	int	i;
42057563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 	if (io_image) {
4208251Slclee 		return;
42090Sstevel@tonic-gate 	}
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
42120Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
42130Sstevel@tonic-gate 			/*
42140Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
42150Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
42160Sstevel@tonic-gate 			 * the partition.
42170Sstevel@tonic-gate 			 */
421810021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
42190Sstevel@tonic-gate 			break;
42200Sstevel@tonic-gate 		}
42210Sstevel@tonic-gate 	}
42220Sstevel@tonic-gate 
42230Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
42240Sstevel@tonic-gate 		if (!io_nifdisk) {
42250Sstevel@tonic-gate 			(void) fprintf(stderr,
42260Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
42270Sstevel@tonic-gate 			    " checked.\n");
42280Sstevel@tonic-gate 		}
4229251Slclee 		return;
42300Sstevel@tonic-gate 	}
42310Sstevel@tonic-gate 
4232251Slclee 	if (readvtoc() != VTOC_OK) {
42330Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
42345169Slclee 	}
42355169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
42365169Slclee 		/* Special case for slice two (entire disk) */
42375169Slclee 		if (i == 2) {
42385169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
42395169Slclee 				(void) fprintf(stderr,
42407563SPrasad.Singamsetty@Sun.COM 				    "slice %d starts at %llu, is not at"
42415169Slclee 				    " start of partition",
42425169Slclee 				    i, disk_vtoc.v_part[i].p_start);
42435169Slclee 				if (!io_nifdisk) {
42445169Slclee 					(void) printf(" adjust ?:");
42455169Slclee 					if (yesno())
42460Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
42475169Slclee 				} else {
42485169Slclee 					disk_vtoc.v_part[i].p_start = 0;
42495169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42500Sstevel@tonic-gate 				}
42515169Slclee 
42525169Slclee 			}
42535169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
42545169Slclee 				(void) fprintf(stderr,
42557563SPrasad.Singamsetty@Sun.COM 				    "slice %d size %llu does not cover"
42565169Slclee 				    " complete partition",
42575169Slclee 				    i, disk_vtoc.v_part[i].p_size);
42585169Slclee 				if (!io_nifdisk) {
42595169Slclee 					(void) printf(" adjust ?:");
42605169Slclee 					if (yesno())
42610Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
42620Sstevel@tonic-gate 						    numsect;
42635169Slclee 				} else {
42645169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
42655169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42660Sstevel@tonic-gate 				}
42675169Slclee 			}
42685169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
42695169Slclee 				(void) fprintf(stderr,
42705169Slclee 				    "slice %d tag was %d should be %d",
42715169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
42725169Slclee 				    V_BACKUP);
42735169Slclee 				if (!io_nifdisk) {
4274251Slclee 					(void) printf(" fix ?:");
42755169Slclee 					if (yesno())
42760Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
42770Sstevel@tonic-gate 						    V_BACKUP;
42785169Slclee 				} else {
42790Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
42800Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
42810Sstevel@tonic-gate 				}
42825169Slclee 			}
42835169Slclee 			continue;
42845169Slclee 		}
42855169Slclee 		if (io_ADJT) {
42865169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
42875169Slclee 			    disk_vtoc.v_part[i].p_start +
42885169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
42895169Slclee 				(void) fprintf(stderr,
42907563SPrasad.Singamsetty@Sun.COM 				    "slice %d (start %llu, end %llu)"
42915169Slclee 				    " is larger than the partition",
42925169Slclee 				    i, disk_vtoc.v_part[i].p_start,
42935169Slclee 				    disk_vtoc.v_part[i].p_start +
42945169Slclee 				    disk_vtoc.v_part[i].p_size);
42955169Slclee 				if (!io_nifdisk) {
42965169Slclee 					(void) printf(" remove ?:");
42975169Slclee 					if (yesno()) {
42980Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
42990Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
43000Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
43010Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
43020Sstevel@tonic-gate 					}
43030Sstevel@tonic-gate 				} else {
43045169Slclee 					disk_vtoc.v_part[i].p_size = 0;
43055169Slclee 					disk_vtoc.v_part[i].p_start = 0;
43065169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
43075169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
43080Sstevel@tonic-gate 					(void) fprintf(stderr,
43095169Slclee 					    " removed!\n");
43105169Slclee 				}
43115169Slclee 			}
43125169Slclee 			continue;
43135169Slclee 		}
43145169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
43155169Slclee 			(void) fprintf(stderr,
43167563SPrasad.Singamsetty@Sun.COM 			    "slice %d (start %llu) is larger than the "
43177563SPrasad.Singamsetty@Sun.COM 			    "partition", i, disk_vtoc.v_part[i].p_start);
43185169Slclee 			if (!io_nifdisk) {
43195169Slclee 				(void) printf(" remove ?:");
43205169Slclee 				if (yesno()) {
43215169Slclee 					disk_vtoc.v_part[i].p_size = 0;
43225169Slclee 					disk_vtoc.v_part[i].p_start = 0;
43235169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
43245169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
43250Sstevel@tonic-gate 				}
43265169Slclee 			} else {
43275169Slclee 				disk_vtoc.v_part[i].p_size = 0;
43285169Slclee 				disk_vtoc.v_part[i].p_start = 0;
43295169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
43305169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
43315169Slclee 				(void) fprintf(stderr,
43325169Slclee 				" removed!\n");
43335169Slclee 			}
43345169Slclee 		} else if (disk_vtoc.v_part[i].p_start
43355169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
43365169Slclee 			(void) fprintf(stderr,
43377563SPrasad.Singamsetty@Sun.COM 			    "slice %d (end %llu) is larger"
43385169Slclee 			    " than the partition",
43395169Slclee 			    i,
43405169Slclee 			    disk_vtoc.v_part[i].p_start +
43415169Slclee 			    disk_vtoc.v_part[i].p_size);
43425169Slclee 			if (!io_nifdisk) {
43435169Slclee 				(void) printf(" adjust ?:");
43445169Slclee 				if (yesno()) {
43455169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
43465169Slclee 				}
43475169Slclee 			} else {
43485169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
43495169Slclee 				(void) fprintf(stderr, " adjusted!\n");
43500Sstevel@tonic-gate 			}
43510Sstevel@tonic-gate 		}
43520Sstevel@tonic-gate 	}
43530Sstevel@tonic-gate #if 1		/* bh for now */
43540Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
43550Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
43560Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
43570Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
43580Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
43590Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
43600Sstevel@tonic-gate #endif
43610Sstevel@tonic-gate 
43620Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
43630Sstevel@tonic-gate 	if (!io_readonly)
4364251Slclee 		(void) writevtoc();
43650Sstevel@tonic-gate }
43660Sstevel@tonic-gate 
43670Sstevel@tonic-gate /*
43680Sstevel@tonic-gate  * yesno
43690Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
43700Sstevel@tonic-gate  */
43710Sstevel@tonic-gate 
4372251Slclee static int
4373251Slclee yesno(void)
43740Sstevel@tonic-gate {
43750Sstevel@tonic-gate 	char	s[80];
43760Sstevel@tonic-gate 
43770Sstevel@tonic-gate 	for (;;) {
43789786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
43790Sstevel@tonic-gate 		rm_blanks(s);
43809786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
43819786SBarry.Harding@Sun.COM 		    ((s[0] != 'y') && (s[0] != 'n'))) {
4382251Slclee 			(void) printf(E_LINE);
4383251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
43840Sstevel@tonic-gate 			continue;
43850Sstevel@tonic-gate 		}
43860Sstevel@tonic-gate 		if (s[0] == 'y')
43870Sstevel@tonic-gate 			return (1);
43880Sstevel@tonic-gate 		else
43890Sstevel@tonic-gate 			return (0);
43900Sstevel@tonic-gate 	}
43910Sstevel@tonic-gate }
43920Sstevel@tonic-gate 
43930Sstevel@tonic-gate /*
43940Sstevel@tonic-gate  * readvtoc
43950Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
43960Sstevel@tonic-gate  */
4397251Slclee static int
4398251Slclee readvtoc(void)
43990Sstevel@tonic-gate {
44000Sstevel@tonic-gate 	int	i;
44010Sstevel@tonic-gate 	int	retval = VTOC_OK;
44020Sstevel@tonic-gate 
44037563SPrasad.Singamsetty@Sun.COM 	if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
44040Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44050Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
44060Sstevel@tonic-gate 			vt_inval++;
44070Sstevel@tonic-gate 			retval = VTOC_INVAL;
44080Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44090Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44105169Slclee 			    "GPT\n");
44110Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44120Sstevel@tonic-gate 		} else {
44130Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
44140Sstevel@tonic-gate 			retval = VTOC_RWERR;
44150Sstevel@tonic-gate 		}
44160Sstevel@tonic-gate 	}
44170Sstevel@tonic-gate 	return (retval);
44180Sstevel@tonic-gate }
44190Sstevel@tonic-gate 
44200Sstevel@tonic-gate /*
44210Sstevel@tonic-gate  * writevtoc
44220Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
44230Sstevel@tonic-gate  */
4424251Slclee static int
4425251Slclee writevtoc(void)
44260Sstevel@tonic-gate {
44270Sstevel@tonic-gate 	int	i;
44280Sstevel@tonic-gate 	int	retval = 0;
44290Sstevel@tonic-gate 
44307563SPrasad.Singamsetty@Sun.COM 	if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
44310Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44320Sstevel@tonic-gate 			(void) fprintf(stderr,
44330Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
44340Sstevel@tonic-gate 			retval = VTOC_INVAL;
44350Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44360Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44375169Slclee 			    "GPT\n");
44380Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44390Sstevel@tonic-gate 		} else {
44400Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
44410Sstevel@tonic-gate 			retval = VTOC_RWERR;
44420Sstevel@tonic-gate 		}
44430Sstevel@tonic-gate 	}
44440Sstevel@tonic-gate 	return (retval);
44450Sstevel@tonic-gate }
44460Sstevel@tonic-gate 
44470Sstevel@tonic-gate /*
44480Sstevel@tonic-gate  * efi_ioctl
44490Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
44500Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
44510Sstevel@tonic-gate  */
44520Sstevel@tonic-gate static int
44530Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
44540Sstevel@tonic-gate {
44550Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
44560Sstevel@tonic-gate 	int error;
44570Sstevel@tonic-gate 
44580Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
44590Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
44600Sstevel@tonic-gate 
44610Sstevel@tonic-gate 	return (error);
44620Sstevel@tonic-gate }
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate /*
44650Sstevel@tonic-gate  * clear_efi
44660Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
44670Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
44680Sstevel@tonic-gate  */
4469251Slclee static int
4470251Slclee clear_efi(void)
44710Sstevel@tonic-gate {
44720Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
44730Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
44740Sstevel@tonic-gate 
44750Sstevel@tonic-gate 	/*
44760Sstevel@tonic-gate 	 * see if we can read the EFI label
44770Sstevel@tonic-gate 	 */
44780Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
44790Sstevel@tonic-gate 		return (VT_ERROR);
44800Sstevel@tonic-gate 	}
44810Sstevel@tonic-gate 
44820Sstevel@tonic-gate 	/*
44830Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
44840Sstevel@tonic-gate 	 */
44850Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
44860Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
44870Sstevel@tonic-gate 
44880Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
44890Sstevel@tonic-gate 		return (VT_ERROR);
44900Sstevel@tonic-gate 	}
44910Sstevel@tonic-gate 
44920Sstevel@tonic-gate 	/*
44930Sstevel@tonic-gate 	 * clear the primary label
44940Sstevel@tonic-gate 	 */
44950Sstevel@tonic-gate 	if (io_debug) {
4496251Slclee 		(void) fprintf(stderr,
4497251Slclee 		    "\tClearing primary EFI label at block %lld\n",
4498251Slclee 		    dk_ioc.dki_lba);
44990Sstevel@tonic-gate 	}
45000Sstevel@tonic-gate 
45010Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45020Sstevel@tonic-gate 		free(dk_ioc.dki_data);
45030Sstevel@tonic-gate 		switch (errno) {
45040Sstevel@tonic-gate 			case EIO:
45050Sstevel@tonic-gate 				return (VT_EIO);
45060Sstevel@tonic-gate 			case EINVAL:
45070Sstevel@tonic-gate 				return (VT_EINVAL);
45080Sstevel@tonic-gate 			default:
45090Sstevel@tonic-gate 				return (VT_ERROR);
45100Sstevel@tonic-gate 		}
45110Sstevel@tonic-gate 	}
45120Sstevel@tonic-gate 
45130Sstevel@tonic-gate 	/*
45140Sstevel@tonic-gate 	 * clear the backup partition table
45150Sstevel@tonic-gate 	 */
45160Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
45170Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
45189889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
45199889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45200Sstevel@tonic-gate 	if (io_debug) {
4521251Slclee 		(void) fprintf(stderr,
4522251Slclee 		    "\tClearing backup partition table at block %lld\n",
4523251Slclee 		    dk_ioc.dki_lba);
45240Sstevel@tonic-gate 	}
45250Sstevel@tonic-gate 
45260Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45270Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
45285169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
45295169Slclee 		    errno);
45300Sstevel@tonic-gate 	}
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	/*
45330Sstevel@tonic-gate 	 * clear the backup label
45340Sstevel@tonic-gate 	 */
45350Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
45360Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
45379889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
45389889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45390Sstevel@tonic-gate 	if (io_debug) {
4540251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
4541251Slclee 		    "%lld\n", dk_ioc.dki_lba);
45420Sstevel@tonic-gate 	}
45430Sstevel@tonic-gate 
45440Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
4545251Slclee 		(void) fprintf(stderr,
4546251Slclee 		    "\tUnable to clear backup EFI label at "
4547251Slclee 		    "block %llu; errno %d\n",
4548251Slclee 		    efi_vtoc->efi_last_lba,
4549251Slclee 		    errno);
45500Sstevel@tonic-gate 	}
45510Sstevel@tonic-gate 
45520Sstevel@tonic-gate 	free(dk_ioc.dki_data);
45530Sstevel@tonic-gate 	efi_free(efi_vtoc);
45540Sstevel@tonic-gate 
45550Sstevel@tonic-gate 	return (0);
45560Sstevel@tonic-gate }
45570Sstevel@tonic-gate 
45580Sstevel@tonic-gate /*
45590Sstevel@tonic-gate  * clear_vtoc
45600Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
45610Sstevel@tonic-gate  *      device.
45620Sstevel@tonic-gate  */
4563251Slclee static void
45640Sstevel@tonic-gate clear_vtoc(int table, int part)
45650Sstevel@tonic-gate {
45660Sstevel@tonic-gate 	struct ipart *clr_table;
45679889SLarry.Liu@Sun.COM 	char *disk_label;
45687563SPrasad.Singamsetty@Sun.COM 	uint32_t pcyl, ncyl, count;
45697563SPrasad.Singamsetty@Sun.COM 	diskaddr_t backup_block, solaris_offset;
45707563SPrasad.Singamsetty@Sun.COM 	ssize_t bytes;
45716549Sbharding 	off_t seek_byte;
45720Sstevel@tonic-gate 
45730Sstevel@tonic-gate #ifdef DEBUG
45749889SLarry.Liu@Sun.COM 	char *read_label;
45750Sstevel@tonic-gate #endif /* DEBUG */
45760Sstevel@tonic-gate 
45770Sstevel@tonic-gate 	if (table == OLD) {
45780Sstevel@tonic-gate 		clr_table = &Old_Table[part];
45790Sstevel@tonic-gate 	} else {
45800Sstevel@tonic-gate 		clr_table = &Table[part];
45810Sstevel@tonic-gate 	}
45820Sstevel@tonic-gate 
45839889SLarry.Liu@Sun.COM 	disk_label = (char *)calloc(sectsiz, 1);
45849889SLarry.Liu@Sun.COM 	if (disk_label == NULL) {
45859889SLarry.Liu@Sun.COM 		return;
45869889SLarry.Liu@Sun.COM 	}
45870Sstevel@tonic-gate 
458810021SSheshadri.Vasudevan@Sun.COM 	seek_byte = (off_t)(LE_32(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
45890Sstevel@tonic-gate 
45900Sstevel@tonic-gate 	if (io_debug) {
45916549Sbharding 		(void) fprintf(stderr,
45926549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
45936549Sbharding 		    (uint64_t)seek_byte,
459410021SSheshadri.Vasudevan@Sun.COM 		    (uint64_t)(LE_32(clr_table->relsect) + VTOC_OFFSET));
45950Sstevel@tonic-gate 	}
45960Sstevel@tonic-gate 
45970Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4598251Slclee 		(void) fprintf(stderr,
45996549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
46006549Sbharding 		    (uint64_t)seek_byte);
46019889SLarry.Liu@Sun.COM 		free(disk_label);
4602251Slclee 		return;
46030Sstevel@tonic-gate 	}
46040Sstevel@tonic-gate 
46059889SLarry.Liu@Sun.COM 	bytes = write(Dev, disk_label, sectsiz);
46069889SLarry.Liu@Sun.COM 
46079889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4608251Slclee 		(void) fprintf(stderr,
46097563SPrasad.Singamsetty@Sun.COM 		    "\tWarning: only %d bytes written to clear primary"
46107563SPrasad.Singamsetty@Sun.COM 		    " VTOC!\n", bytes);
46110Sstevel@tonic-gate 	}
46120Sstevel@tonic-gate 
46130Sstevel@tonic-gate #ifdef DEBUG
46140Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4615251Slclee 		(void) fprintf(stderr,
46166549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
46176549Sbharding 		    (uint64_t)seek_byte);
46189889SLarry.Liu@Sun.COM 		free(disk_label);
4619251Slclee 		return;
46200Sstevel@tonic-gate 	} else {
46216549Sbharding 		(void) fprintf(stderr,
46226549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
46236549Sbharding 		    (uint64_t)seek_byte);
46240Sstevel@tonic-gate 	}
46250Sstevel@tonic-gate 
46269889SLarry.Liu@Sun.COM 	read_label = (char *)calloc(sectsiz, 1);
46279889SLarry.Liu@Sun.COM 	if (read_label == NULL) {
46289889SLarry.Liu@Sun.COM 		free(disk_label);
46299889SLarry.Liu@Sun.COM 		return;
46309889SLarry.Liu@Sun.COM 	}
46319889SLarry.Liu@Sun.COM 
46329889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
46339889SLarry.Liu@Sun.COM 
46349889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4635251Slclee 		(void) fprintf(stderr,
4636251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
46370Sstevel@tonic-gate 		    bytes);
46380Sstevel@tonic-gate 	}
46390Sstevel@tonic-gate 
46409889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4641251Slclee 		(void) fprintf(stderr,
4642251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
46430Sstevel@tonic-gate 	} else {
4644251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
46450Sstevel@tonic-gate 		    "read_label\n");
46460Sstevel@tonic-gate 	}
46470Sstevel@tonic-gate #endif /* DEBUG */
46480Sstevel@tonic-gate 
46490Sstevel@tonic-gate 	/* Clear backup label */
465010021SSheshadri.Vasudevan@Sun.COM 	pcyl = LE_32(clr_table->numsect) / (heads * sectors);
465110021SSheshadri.Vasudevan@Sun.COM 	solaris_offset = LE_32(clr_table->relsect);
46520Sstevel@tonic-gate 	ncyl = pcyl - acyl;
46530Sstevel@tonic-gate 
46540Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
46550Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
46560Sstevel@tonic-gate 
46570Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
46589889SLarry.Liu@Sun.COM 		seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz;
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4661251Slclee 			(void) fprintf(stderr,
46626549Sbharding 			    "\tError seeking to backup label at byte %llu on "
46636549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
46649889SLarry.Liu@Sun.COM 			free(disk_label);
46659889SLarry.Liu@Sun.COM #ifdef DEBUG
46669889SLarry.Liu@Sun.COM 			free(read_label);
46679889SLarry.Liu@Sun.COM #endif /* DEBUG */
4668251Slclee 			return;
46690Sstevel@tonic-gate 		}
46700Sstevel@tonic-gate 
46710Sstevel@tonic-gate 		if (io_debug) {
4672251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
46736549Sbharding 			    " byte %llu (block %llu)\n",
46746549Sbharding 			    (uint64_t)seek_byte,
46756549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46760Sstevel@tonic-gate 		}
46770Sstevel@tonic-gate 
46789889SLarry.Liu@Sun.COM 		bytes = write(Dev, disk_label, sectsiz);
46799889SLarry.Liu@Sun.COM 
46809889SLarry.Liu@Sun.COM 		if (bytes != sectsiz) {
4681251Slclee 			(void) fprintf(stderr,
4682251Slclee 			    "\t\tWarning: only %d bytes written to "
46836549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
46846549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46850Sstevel@tonic-gate 		}
46860Sstevel@tonic-gate 
46870Sstevel@tonic-gate #ifdef DEBUG
46880Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4689251Slclee 		(void) fprintf(stderr,
46906549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
46916549Sbharding 		    (uint64_t)seek_byte);
46929889SLarry.Liu@Sun.COM 		free(disk_label);
46939889SLarry.Liu@Sun.COM 		free(read_label);
4694251Slclee 		return;
46950Sstevel@tonic-gate 	} else {
46966549Sbharding 		(void) fprintf(stderr,
46976549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
46986549Sbharding 		    (uint64_t)seek_byte);
46990Sstevel@tonic-gate 	}
47000Sstevel@tonic-gate 
47019889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
47029889SLarry.Liu@Sun.COM 
47039889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4704251Slclee 		(void) fprintf(stderr,
4705251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
4706251Slclee 		    bytes);
47070Sstevel@tonic-gate 	}
47080Sstevel@tonic-gate 
47099889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4710251Slclee 		(void) fprintf(stderr,
4711251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
47120Sstevel@tonic-gate 	} else {
4713251Slclee 		(void) fprintf(stderr,
4714251Slclee 		    "DEBUG: Good compare of disk_label and backup "
47150Sstevel@tonic-gate 		    "read_label\n");
47160Sstevel@tonic-gate 	}
47179889SLarry.Liu@Sun.COM 
47180Sstevel@tonic-gate #endif /* DEBUG */
47190Sstevel@tonic-gate 
47200Sstevel@tonic-gate 		backup_block += 2;
47210Sstevel@tonic-gate 	}
47229889SLarry.Liu@Sun.COM 
47239889SLarry.Liu@Sun.COM #ifdef DEBUG
47249889SLarry.Liu@Sun.COM 	free(read_label);
47259889SLarry.Liu@Sun.COM #endif /* DEBUG */
47269889SLarry.Liu@Sun.COM 	free(disk_label);
47270Sstevel@tonic-gate }
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
47300Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
47310Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
47320Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
47330Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
47360Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
47370Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
47380Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
47390Sstevel@tonic-gate 
47400Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
47410Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
47420Sstevel@tonic-gate 
47430Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
47440Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
47450Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47460Sstevel@tonic-gate 
47470Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
47480Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
47490Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47500Sstevel@tonic-gate 
47510Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
47520Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
47530Sstevel@tonic-gate 
47540Sstevel@tonic-gate /*
47550Sstevel@tonic-gate  *  lecture_and_query
47560Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
47570Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
47580Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
47590Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
47600Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
47610Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
47620Sstevel@tonic-gate  */
4763251Slclee static int
47640Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
47650Sstevel@tonic-gate {
47660Sstevel@tonic-gate 	if (io_nifdisk)
47670Sstevel@tonic-gate 		return (0);
47680Sstevel@tonic-gate 
4769251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
4770251Slclee 	(void) fprintf(stderr, "%s", warning);
4771251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
4772251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
47730Sstevel@tonic-gate 
47740Sstevel@tonic-gate 	return (yesno());
47750Sstevel@tonic-gate }
47760Sstevel@tonic-gate 
4777251Slclee static void
47780Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
47790Sstevel@tonic-gate {
47807563SPrasad.Singamsetty@Sun.COM 	struct extvtoc v;
47810Sstevel@tonic-gate 	struct dk_geom d;
47820Sstevel@tonic-gate 	struct part_info pi;
47837563SPrasad.Singamsetty@Sun.COM 	struct extpart_info extpi;
47847563SPrasad.Singamsetty@Sun.COM 	diskaddr_t totsize;
47850Sstevel@tonic-gate 	int idx = -1;
47860Sstevel@tonic-gate 
47870Sstevel@tonic-gate 	/*
47880Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
47890Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
47900Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
47910Sstevel@tonic-gate 	 */
47927563SPrasad.Singamsetty@Sun.COM 	if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
47937563SPrasad.Singamsetty@Sun.COM 		if (extpi.p_start != 0) {
47947563SPrasad.Singamsetty@Sun.COM 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
47957563SPrasad.Singamsetty@Sun.COM 			    devname)) {
47967563SPrasad.Singamsetty@Sun.COM 				(void) close(fd);
47977563SPrasad.Singamsetty@Sun.COM 				exit(1);
47987563SPrasad.Singamsetty@Sun.COM 			}
47997563SPrasad.Singamsetty@Sun.COM 		}
48007563SPrasad.Singamsetty@Sun.COM 	} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
48010Sstevel@tonic-gate 		if (pi.p_start != 0) {
48020Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
48030Sstevel@tonic-gate 			    devname)) {
48040Sstevel@tonic-gate 				(void) close(fd);
48050Sstevel@tonic-gate 				exit(1);
48060Sstevel@tonic-gate 			}
48070Sstevel@tonic-gate 		}
48080Sstevel@tonic-gate 	} else {
48097563SPrasad.Singamsetty@Sun.COM 		if ((idx = read_extvtoc(fd, &v)) < 0) {
48100Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
48110Sstevel@tonic-gate 			    devname)) {
48120Sstevel@tonic-gate 				(void) close(fd);
48130Sstevel@tonic-gate 				exit(1);
48140Sstevel@tonic-gate 			}
48150Sstevel@tonic-gate 			return;
48160Sstevel@tonic-gate 		}
48170Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
48180Sstevel@tonic-gate 			perror(devname);
48190Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
48200Sstevel@tonic-gate 			    devname)) {
48210Sstevel@tonic-gate 				(void) close(fd);
48220Sstevel@tonic-gate 				exit(1);
48230Sstevel@tonic-gate 			}
48240Sstevel@tonic-gate 			return;
48250Sstevel@tonic-gate 		}
48267563SPrasad.Singamsetty@Sun.COM 		totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
48270Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
48280Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
48290Sstevel@tonic-gate 			    devname)) {
48300Sstevel@tonic-gate 				(void) close(fd);
48310Sstevel@tonic-gate 				exit(1);
48320Sstevel@tonic-gate 			}
48330Sstevel@tonic-gate 		}
48340Sstevel@tonic-gate 	}
48350Sstevel@tonic-gate }
48360Sstevel@tonic-gate 
48370Sstevel@tonic-gate 
48380Sstevel@tonic-gate /*
48390Sstevel@tonic-gate  * get_node
48400Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
48410Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
48420Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
48430Sstevel@tonic-gate  */
48440Sstevel@tonic-gate static char *
48450Sstevel@tonic-gate get_node(char *devname)
48460Sstevel@tonic-gate {
48470Sstevel@tonic-gate 	char *node;
48480Sstevel@tonic-gate 	struct stat statbuf;
48490Sstevel@tonic-gate 	size_t space;
48500Sstevel@tonic-gate 
48510Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
48520Sstevel@tonic-gate 	if (io_image)
48530Sstevel@tonic-gate 		return (devname);
48540Sstevel@tonic-gate 
48550Sstevel@tonic-gate 	node = devname;
48560Sstevel@tonic-gate 
48570Sstevel@tonic-gate 	/* Try the node as provided first */
48580Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
48590Sstevel@tonic-gate 		/*
48600Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
48610Sstevel@tonic-gate 		 * default path and try again.
48620Sstevel@tonic-gate 		 */
48630Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
48640Sstevel@tonic-gate 
48650Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
4866251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
48670Sstevel@tonic-gate 			    "for device node.\n");
48680Sstevel@tonic-gate 			exit(1);
48690Sstevel@tonic-gate 		}
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
48720Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
48730Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
48740Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
48750Sstevel@tonic-gate 
48760Sstevel@tonic-gate 		/* Try to stat it again */
48770Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
48780Sstevel@tonic-gate 			/* Failed all options, give up */
4879251Slclee 			(void) fprintf(stderr,
4880251Slclee 			    "fdisk: Cannot stat device %s.\n",
48810Sstevel@tonic-gate 			    devname);
48820Sstevel@tonic-gate 			exit(1);
48830Sstevel@tonic-gate 		}
48840Sstevel@tonic-gate 	}
48850Sstevel@tonic-gate 
48860Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
48870Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
4888251Slclee 		(void) fprintf(stderr,
4889251Slclee 		    "fdisk: %s must be a raw device.\n", node);
48900Sstevel@tonic-gate 		exit(1);
48910Sstevel@tonic-gate 	}
48920Sstevel@tonic-gate 
48930Sstevel@tonic-gate 	return (node);
48940Sstevel@tonic-gate }
489510021SSheshadri.Vasudevan@Sun.COM 
489610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
489710021SSheshadri.Vasudevan@Sun.COM static void
489810021SSheshadri.Vasudevan@Sun.COM preach_and_continue()
489910021SSheshadri.Vasudevan@Sun.COM {
490010021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(stderr, "There are mounted logical drives. Committing "
490110021SSheshadri.Vasudevan@Sun.COM 	    "changes now can lead to inconsistancy in internal system state "
490210021SSheshadri.Vasudevan@Sun.COM 	    "which can eventually cause data loss or corruption. Unmount all "
490310021SSheshadri.Vasudevan@Sun.COM 	    "logical drives and try committing the changes again.\n");
490410021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(s);
490510021SSheshadri.Vasudevan@Sun.COM }
490610021SSheshadri.Vasudevan@Sun.COM 
490710021SSheshadri.Vasudevan@Sun.COM /*
490810021SSheshadri.Vasudevan@Sun.COM  * Convert a given partition ID to an descriptive string.
490910021SSheshadri.Vasudevan@Sun.COM  * Just an index into the partition types table.
491010021SSheshadri.Vasudevan@Sun.COM  */
491110021SSheshadri.Vasudevan@Sun.COM void
491210021SSheshadri.Vasudevan@Sun.COM id_to_name(uchar_t sysid, char *buffer)
491310021SSheshadri.Vasudevan@Sun.COM {
491410021SSheshadri.Vasudevan@Sun.COM 	strcpy(buffer, fdisk_part_types[sysid]);
491510021SSheshadri.Vasudevan@Sun.COM }
491610021SSheshadri.Vasudevan@Sun.COM 
491710021SSheshadri.Vasudevan@Sun.COM /*
491810021SSheshadri.Vasudevan@Sun.COM  * Procedure to check the validity of the extended partition menu option
491910021SSheshadri.Vasudevan@Sun.COM  * entered by the user
492010021SSheshadri.Vasudevan@Sun.COM  */
492110021SSheshadri.Vasudevan@Sun.COM static int
492210021SSheshadri.Vasudevan@Sun.COM ext_invalid_option(char ch)
492310021SSheshadri.Vasudevan@Sun.COM {
492410021SSheshadri.Vasudevan@Sun.COM 	char *p;
492510021SSheshadri.Vasudevan@Sun.COM 
492610021SSheshadri.Vasudevan@Sun.COM 	p = strchr(ext_part_menu_opts, tolower(ch));
492710021SSheshadri.Vasudevan@Sun.COM 
492810021SSheshadri.Vasudevan@Sun.COM 	if (p == NULL) {
492910021SSheshadri.Vasudevan@Sun.COM 		return (1);
493010021SSheshadri.Vasudevan@Sun.COM 	}
493110021SSheshadri.Vasudevan@Sun.COM 	return (0);
493210021SSheshadri.Vasudevan@Sun.COM }
493310021SSheshadri.Vasudevan@Sun.COM 
493410021SSheshadri.Vasudevan@Sun.COM /*
493510021SSheshadri.Vasudevan@Sun.COM  * Read 16 bytes of the input (assuming that no valid user input spans more
493610021SSheshadri.Vasudevan@Sun.COM  * than that). Flush the input stream, so that the next read does not reap
493710021SSheshadri.Vasudevan@Sun.COM  * stale data from the previous input that was not processed.
493810021SSheshadri.Vasudevan@Sun.COM  * Note that fgets also reads the trailing '\n'
493910021SSheshadri.Vasudevan@Sun.COM  */
494010021SSheshadri.Vasudevan@Sun.COM static void
494110021SSheshadri.Vasudevan@Sun.COM ext_read_input(char *buf)
494210021SSheshadri.Vasudevan@Sun.COM {
494310021SSheshadri.Vasudevan@Sun.COM 	fgets(buf, 16, stdin);
494410021SSheshadri.Vasudevan@Sun.COM 	fflush(stdin);
494510021SSheshadri.Vasudevan@Sun.COM }
494610021SSheshadri.Vasudevan@Sun.COM 
494710021SSheshadri.Vasudevan@Sun.COM /*
494810021SSheshadri.Vasudevan@Sun.COM  * Procedure to read and validate the user option at the extended partition menu
494910021SSheshadri.Vasudevan@Sun.COM  */
495010021SSheshadri.Vasudevan@Sun.COM static int
495110021SSheshadri.Vasudevan@Sun.COM ext_read_options(char *buf)
495210021SSheshadri.Vasudevan@Sun.COM {
495310021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(buf);
495410021SSheshadri.Vasudevan@Sun.COM 	if ((strlen(buf) != 2) || (ext_invalid_option(buf[0]))) {
495510021SSheshadri.Vasudevan@Sun.COM 		printf("\nUnknown Command\n");
495610021SSheshadri.Vasudevan@Sun.COM 		return (-1);
495710021SSheshadri.Vasudevan@Sun.COM 	}
495810021SSheshadri.Vasudevan@Sun.COM 	return (0);
495910021SSheshadri.Vasudevan@Sun.COM }
496010021SSheshadri.Vasudevan@Sun.COM 
496110021SSheshadri.Vasudevan@Sun.COM /*
496210021SSheshadri.Vasudevan@Sun.COM  * Procedure to print the list of known partition types and their IDs
496310021SSheshadri.Vasudevan@Sun.COM  */
496410021SSheshadri.Vasudevan@Sun.COM static void
496510021SSheshadri.Vasudevan@Sun.COM ext_print_part_types()
496610021SSheshadri.Vasudevan@Sun.COM {
496710021SSheshadri.Vasudevan@Sun.COM 	int i, rowmax, rowcount = 1;
496810021SSheshadri.Vasudevan@Sun.COM 	struct winsize ws;
496910021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
497010021SSheshadri.Vasudevan@Sun.COM 
497110021SSheshadri.Vasudevan@Sun.COM 	/* Get the current window dimensions */
497210021SSheshadri.Vasudevan@Sun.COM 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) {
497310021SSheshadri.Vasudevan@Sun.COM 		perror("ioctl");
497410021SSheshadri.Vasudevan@Sun.COM 		rowmax = 20;
497510021SSheshadri.Vasudevan@Sun.COM 	} else {
497610021SSheshadri.Vasudevan@Sun.COM 		/*
497710021SSheshadri.Vasudevan@Sun.COM 		 * Accommodate the initial headings by reducing the number of
497810021SSheshadri.Vasudevan@Sun.COM 		 * partition IDs being printed.
497910021SSheshadri.Vasudevan@Sun.COM 		 */
498010021SSheshadri.Vasudevan@Sun.COM 		rowmax = ws.ws_row - 5;
498110021SSheshadri.Vasudevan@Sun.COM 	}
498210021SSheshadri.Vasudevan@Sun.COM 
498310021SSheshadri.Vasudevan@Sun.COM 	if (rowmax < 3) {
498410021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Window size too small."
498510021SSheshadri.Vasudevan@Sun.COM 		    " Try resizing the window\n");
498610021SSheshadri.Vasudevan@Sun.COM 		return;
498710021SSheshadri.Vasudevan@Sun.COM 	}
498810021SSheshadri.Vasudevan@Sun.COM 
498910021SSheshadri.Vasudevan@Sun.COM 	printf("List of known partition types : \n");
499010021SSheshadri.Vasudevan@Sun.COM 	printf("PartID          Partition Type\n");
499110021SSheshadri.Vasudevan@Sun.COM 	printf("======          ==============\n");
499210021SSheshadri.Vasudevan@Sun.COM 	for (i = 0; i <= FDISK_MAX_VALID_PART_ID; i++) {
499310021SSheshadri.Vasudevan@Sun.COM 		printf("%-3d          %s\n", i, fdisk_part_types[i]);
499410021SSheshadri.Vasudevan@Sun.COM 		rowcount++;
499510021SSheshadri.Vasudevan@Sun.COM 		if (rowcount == rowmax) {
499610021SSheshadri.Vasudevan@Sun.COM 			/*
499710021SSheshadri.Vasudevan@Sun.COM 			 * After the initial screen, use all the rows for
499810021SSheshadri.Vasudevan@Sun.COM 			 * printing the partition IDs, but one.
499910021SSheshadri.Vasudevan@Sun.COM 			 */
500010021SSheshadri.Vasudevan@Sun.COM 			rowmax = ws.ws_row - 1;
500110021SSheshadri.Vasudevan@Sun.COM 			fprintf(stderr, "\nPress enter to see next page or 'q'"
500210021SSheshadri.Vasudevan@Sun.COM 			    " to quit : ");
500310021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
500410021SSheshadri.Vasudevan@Sun.COM 			if ((strlen(buf) == 2) && (tolower(buf[0]) == 'q')) {
500510021SSheshadri.Vasudevan@Sun.COM 				return;
500610021SSheshadri.Vasudevan@Sun.COM 			}
500710021SSheshadri.Vasudevan@Sun.COM 			rowcount = 1;
500810021SSheshadri.Vasudevan@Sun.COM 		}
500910021SSheshadri.Vasudevan@Sun.COM 	}
501010021SSheshadri.Vasudevan@Sun.COM }
501110021SSheshadri.Vasudevan@Sun.COM 
501210021SSheshadri.Vasudevan@Sun.COM static void
501310021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(int *pno)
501410021SSheshadri.Vasudevan@Sun.COM {
501510021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
501610021SSheshadri.Vasudevan@Sun.COM 	int len, i;
501710021SSheshadri.Vasudevan@Sun.COM 
501810021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
501910021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the partition number : ");
502010021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
502110021SSheshadri.Vasudevan@Sun.COM 
502210021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
502310021SSheshadri.Vasudevan@Sun.COM 
502410021SSheshadri.Vasudevan@Sun.COM 		/* Check length of the input */
502510021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_NUM_DIGITS+1))) {
502610021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
502710021SSheshadri.Vasudevan@Sun.COM 		}
502810021SSheshadri.Vasudevan@Sun.COM 
502910021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
503010021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
503110021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
503210021SSheshadri.Vasudevan@Sun.COM 				goto print_error_and_continue;
503310021SSheshadri.Vasudevan@Sun.COM 			}
503410021SSheshadri.Vasudevan@Sun.COM 		}
503510021SSheshadri.Vasudevan@Sun.COM 
503610021SSheshadri.Vasudevan@Sun.COM 		*pno = atoi(buf);
503710021SSheshadri.Vasudevan@Sun.COM 
503810021SSheshadri.Vasudevan@Sun.COM 		if ((*pno <= FD_NUMPART) ||
503910021SSheshadri.Vasudevan@Sun.COM 		    *pno > (fdisk_get_logical_drive_count(epp) + FD_NUMPART)) {
504010021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
504110021SSheshadri.Vasudevan@Sun.COM 		}
504210021SSheshadri.Vasudevan@Sun.COM 
504310021SSheshadri.Vasudevan@Sun.COM 		break;
504410021SSheshadri.Vasudevan@Sun.COM print_error_and_continue:
504510021SSheshadri.Vasudevan@Sun.COM 		printf("Invalid partition number\n");
504610021SSheshadri.Vasudevan@Sun.COM 		continue;
504710021SSheshadri.Vasudevan@Sun.COM 	}
504810021SSheshadri.Vasudevan@Sun.COM }
504910021SSheshadri.Vasudevan@Sun.COM 
505010021SSheshadri.Vasudevan@Sun.COM static void
505110021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(uchar_t *partid)
505210021SSheshadri.Vasudevan@Sun.COM {
505310021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
505410021SSheshadri.Vasudevan@Sun.COM 	int len, i, id;
505510021SSheshadri.Vasudevan@Sun.COM 
505610021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
505710021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the ID ( Type I for list of partition IDs ) : ");
505810021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
505910021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
506010021SSheshadri.Vasudevan@Sun.COM 
506110021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_ID_DIGITS + 1))) {
506210021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
506310021SSheshadri.Vasudevan@Sun.COM 			continue;
506410021SSheshadri.Vasudevan@Sun.COM 		}
506510021SSheshadri.Vasudevan@Sun.COM 
506610021SSheshadri.Vasudevan@Sun.COM 		if ((len == 2) && (toupper(buf[0]) == 'I')) {
506710021SSheshadri.Vasudevan@Sun.COM 			ext_print_part_types();
506810021SSheshadri.Vasudevan@Sun.COM 			continue;
506910021SSheshadri.Vasudevan@Sun.COM 		}
507010021SSheshadri.Vasudevan@Sun.COM 
507110021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
507210021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
507310021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
507410021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid partition ID\n");
507510021SSheshadri.Vasudevan@Sun.COM 				break;
507610021SSheshadri.Vasudevan@Sun.COM 			}
507710021SSheshadri.Vasudevan@Sun.COM 		}
507810021SSheshadri.Vasudevan@Sun.COM 
507910021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
508010021SSheshadri.Vasudevan@Sun.COM 			continue;
508110021SSheshadri.Vasudevan@Sun.COM 		}
508210021SSheshadri.Vasudevan@Sun.COM 
508310021SSheshadri.Vasudevan@Sun.COM 		/* Check if the (now) valid number is greater than the limit */
508410021SSheshadri.Vasudevan@Sun.COM 		if ((id = atoi(buf)) > FDISK_MAX_VALID_PART_ID) {
508510021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
508610021SSheshadri.Vasudevan@Sun.COM 			continue;
508710021SSheshadri.Vasudevan@Sun.COM 		}
508810021SSheshadri.Vasudevan@Sun.COM 
508910021SSheshadri.Vasudevan@Sun.COM 		*partid = (uchar_t)id;
509010021SSheshadri.Vasudevan@Sun.COM 
509110021SSheshadri.Vasudevan@Sun.COM 		/* Disallow multiple extended partitions */
509210021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(*partid)) {
509310021SSheshadri.Vasudevan@Sun.COM 			printf("Multiple extended partitions not allowed\n");
509410021SSheshadri.Vasudevan@Sun.COM 			continue;
509510021SSheshadri.Vasudevan@Sun.COM 		}
509610021SSheshadri.Vasudevan@Sun.COM 
509710021SSheshadri.Vasudevan@Sun.COM 		/* Disallow EFI partitions within extended partition */
509810021SSheshadri.Vasudevan@Sun.COM 		if (*partid == EFI_PMBR) {
509910021SSheshadri.Vasudevan@Sun.COM 			printf("EFI partitions within an extended partition"
510010021SSheshadri.Vasudevan@Sun.COM 			    " is not allowed\n");
510110021SSheshadri.Vasudevan@Sun.COM 			continue;
510210021SSheshadri.Vasudevan@Sun.COM 		}
510310021SSheshadri.Vasudevan@Sun.COM 
510410021SSheshadri.Vasudevan@Sun.COM 		return; /* Valid partition ID is in partid */
510510021SSheshadri.Vasudevan@Sun.COM 	}
510610021SSheshadri.Vasudevan@Sun.COM }
510710021SSheshadri.Vasudevan@Sun.COM 
510810021SSheshadri.Vasudevan@Sun.COM static void
510910021SSheshadri.Vasudevan@Sun.COM delete_logical_drive()
511010021SSheshadri.Vasudevan@Sun.COM {
511110021SSheshadri.Vasudevan@Sun.COM 	int pno;
511210021SSheshadri.Vasudevan@Sun.COM 
511310021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
511410021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
511510021SSheshadri.Vasudevan@Sun.COM 		return;
511610021SSheshadri.Vasudevan@Sun.COM 	}
511710021SSheshadri.Vasudevan@Sun.COM 
511810021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
511910021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
512010021SSheshadri.Vasudevan@Sun.COM 	fdisk_delete_logical_drive(epp, pno);
512110021SSheshadri.Vasudevan@Sun.COM 	printf("Partition %d deleted\n", pno);
512210021SSheshadri.Vasudevan@Sun.COM }
512310021SSheshadri.Vasudevan@Sun.COM 
512410021SSheshadri.Vasudevan@Sun.COM static int
512510021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_start(uint32_t *begsec)
512610021SSheshadri.Vasudevan@Sun.COM {
512710021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
512810021SSheshadri.Vasudevan@Sun.COM 	int ret, len, i;
512910021SSheshadri.Vasudevan@Sun.COM 	uint32_t begcyl;
513010021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_cyl;
513110021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_sec;
513210021SSheshadri.Vasudevan@Sun.COM 
513310021SSheshadri.Vasudevan@Sun.COM 	ret = fdisk_ext_find_first_free_sec(epp, &first_free_sec);
513410021SSheshadri.Vasudevan@Sun.COM 	if (ret != FDISK_SUCCESS) {
513510021SSheshadri.Vasudevan@Sun.COM 		return (ret);
513610021SSheshadri.Vasudevan@Sun.COM 	}
513710021SSheshadri.Vasudevan@Sun.COM 
513810021SSheshadri.Vasudevan@Sun.COM 	first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec);
513910021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
514010021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the beginning cylinder (Default - %d) : ",
514110021SSheshadri.Vasudevan@Sun.COM 		    first_free_cyl);
514210021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
514310021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
514410021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
514510021SSheshadri.Vasudevan@Sun.COM 			*begsec = first_free_sec;
514610021SSheshadri.Vasudevan@Sun.COM 			return (FDISK_SUCCESS);
514710021SSheshadri.Vasudevan@Sun.COM 		}
514810021SSheshadri.Vasudevan@Sun.COM 
514910021SSheshadri.Vasudevan@Sun.COM 		if (len > (FDISK_MAX_VALID_CYL_NUM_DIGITS + 1)) {
515010021SSheshadri.Vasudevan@Sun.COM 			printf("Input too long\n");
515110021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid beginning cylinder number\n");
515210021SSheshadri.Vasudevan@Sun.COM 			continue;
515310021SSheshadri.Vasudevan@Sun.COM 		}
515410021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
515510021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len - 1; i++) {
515610021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
515710021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
515810021SSheshadri.Vasudevan@Sun.COM 				break;
515910021SSheshadri.Vasudevan@Sun.COM 			}
516010021SSheshadri.Vasudevan@Sun.COM 		}
516110021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
516210021SSheshadri.Vasudevan@Sun.COM 			continue;
516310021SSheshadri.Vasudevan@Sun.COM 		}
516410021SSheshadri.Vasudevan@Sun.COM 
516510021SSheshadri.Vasudevan@Sun.COM 		begcyl = atoi(buf);
516610021SSheshadri.Vasudevan@Sun.COM 		ret = fdisk_ext_validate_part_start(epp, begcyl, begsec);
516710021SSheshadri.Vasudevan@Sun.COM 		switch (ret) {
516810021SSheshadri.Vasudevan@Sun.COM 			case FDISK_SUCCESS:
516910021SSheshadri.Vasudevan@Sun.COM 				/*
517010021SSheshadri.Vasudevan@Sun.COM 				 * Success.
517110021SSheshadri.Vasudevan@Sun.COM 				 * Valid beginning sector is in begsec
517210021SSheshadri.Vasudevan@Sun.COM 				 */
517310021SSheshadri.Vasudevan@Sun.COM 				break;
517410021SSheshadri.Vasudevan@Sun.COM 
517510021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOVERLAP:
517610021SSheshadri.Vasudevan@Sun.COM 				printf("Partition boundary overlaps with ");
517710021SSheshadri.Vasudevan@Sun.COM 				printf("existing partitions\n");
517810021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
517910021SSheshadri.Vasudevan@Sun.COM 				continue;
518010021SSheshadri.Vasudevan@Sun.COM 
518110021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOOBOUND:
518210021SSheshadri.Vasudevan@Sun.COM 				printf("Cylinder boundary beyond the limits\n");
518310021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
518410021SSheshadri.Vasudevan@Sun.COM 				continue;
518510021SSheshadri.Vasudevan@Sun.COM 		}
518610021SSheshadri.Vasudevan@Sun.COM 		return (FDISK_SUCCESS);
518710021SSheshadri.Vasudevan@Sun.COM 	}
518810021SSheshadri.Vasudevan@Sun.COM }
518910021SSheshadri.Vasudevan@Sun.COM 
519010021SSheshadri.Vasudevan@Sun.COM /*
519110021SSheshadri.Vasudevan@Sun.COM  * Algorithm :
519210021SSheshadri.Vasudevan@Sun.COM  * 1. Check if the first character is a +
519310021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, check if the last character is 'k', 'm' or 'g'
519410021SSheshadri.Vasudevan@Sun.COM  * 2. If not, check if there are any non-digits
519510021SSheshadri.Vasudevan@Sun.COM  * 3. Check for the length of the numeral string
519610021SSheshadri.Vasudevan@Sun.COM  * 4. atoi the numeral string
519710021SSheshadri.Vasudevan@Sun.COM  * 5. In case of data entered in KB, MB or GB, convert it to number of cylinders
519810021SSheshadri.Vasudevan@Sun.COM  *	a) Adjust size to be cylinder boundary aligned
519910021SSheshadri.Vasudevan@Sun.COM  * 6. If size specifies is zero, flag error
520010021SSheshadri.Vasudevan@Sun.COM  * 7. Check if the size is less than 1 cylinder
520110021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, default the size FDISK_MIN_PART_SIZE
520210021SSheshadri.Vasudevan@Sun.COM  * 	b) If no, Check if the size is within endcyl - begcyl
520310021SSheshadri.Vasudevan@Sun.COM  */
520410021SSheshadri.Vasudevan@Sun.COM static void
520510021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec)
520610021SSheshadri.Vasudevan@Sun.COM {
520710021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
520810021SSheshadri.Vasudevan@Sun.COM 	uint32_t tempcyl;
520910021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_sec;
521010021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_cyl;
521110021SSheshadri.Vasudevan@Sun.COM 	int i, len, ch, mbgb = 0, scale = FDISK_SECTS_PER_CYL(epp);
521210021SSheshadri.Vasudevan@Sun.COM 	uint64_t size = 0;
521310021SSheshadri.Vasudevan@Sun.COM 	int copy_len;
521410021SSheshadri.Vasudevan@Sun.COM 	char numbuf[FDISK_MAX_VALID_CYL_NUM_DIGITS + 1];
521510021SSheshadri.Vasudevan@Sun.COM 	int sectsize = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE);
521610021SSheshadri.Vasudevan@Sun.COM 	uint32_t remdr, spc, poss_end;
521710021SSheshadri.Vasudevan@Sun.COM 
521810021SSheshadri.Vasudevan@Sun.COM 	if (sectsize == EINVAL) {
521910021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Unsupported geometry statistics.\n");
522010021SSheshadri.Vasudevan@Sun.COM 		exit(1);
522110021SSheshadri.Vasudevan@Sun.COM 	}
522210021SSheshadri.Vasudevan@Sun.COM 
522310021SSheshadri.Vasudevan@Sun.COM 	last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec);
522410021SSheshadri.Vasudevan@Sun.COM 	last_free_cyl = FDISK_SECT_TO_CYL(epp, last_free_sec);
522510021SSheshadri.Vasudevan@Sun.COM 
522610021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
522710021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the size in cylinders (Default End Cylinder -");
522810021SSheshadri.Vasudevan@Sun.COM 		printf(" %u)\n", last_free_cyl);
522910021SSheshadri.Vasudevan@Sun.COM 		printf("Type +<size>K, +<size>M or +<size>G to enter size in");
523010021SSheshadri.Vasudevan@Sun.COM 		printf("KB, MB or GB : ");
523110021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
523210021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
523310021SSheshadri.Vasudevan@Sun.COM 		mbgb = 0;
523410021SSheshadri.Vasudevan@Sun.COM 		scale = FDISK_SECTS_PER_CYL(epp);
523510021SSheshadri.Vasudevan@Sun.COM 
523610021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
523710021SSheshadri.Vasudevan@Sun.COM 			*endsec = last_free_sec;
523810021SSheshadri.Vasudevan@Sun.COM 			return;
523910021SSheshadri.Vasudevan@Sun.COM 		}
524010021SSheshadri.Vasudevan@Sun.COM 
524110021SSheshadri.Vasudevan@Sun.COM 		copy_len = len - 1;
524210021SSheshadri.Vasudevan@Sun.COM 
524310021SSheshadri.Vasudevan@Sun.COM 		if ((buf[0] == '+') && (isdigit(buf[1]))) {
524410021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
524510021SSheshadri.Vasudevan@Sun.COM 			if ((ch = toupper(buf[len - 2])) == 'B') {
524610021SSheshadri.Vasudevan@Sun.COM 				ch = toupper(buf[len - 3]);
524710021SSheshadri.Vasudevan@Sun.COM 				copy_len--;
524810021SSheshadri.Vasudevan@Sun.COM 			}
524910021SSheshadri.Vasudevan@Sun.COM 
525010021SSheshadri.Vasudevan@Sun.COM 			if (!((ch == 'K') || (ch == 'M') || (ch == 'G'))) {
525110021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid partition size\n");
525210021SSheshadri.Vasudevan@Sun.COM 				continue;
525310021SSheshadri.Vasudevan@Sun.COM 			}
525410021SSheshadri.Vasudevan@Sun.COM 
525510021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
525610021SSheshadri.Vasudevan@Sun.COM 			mbgb = 1;
525710021SSheshadri.Vasudevan@Sun.COM 			scale = ((ch == 'K') ? FDISK_KB :
525810021SSheshadri.Vasudevan@Sun.COM 			    ((ch == 'M') ? FDISK_MB : FDISK_GB));
525910021SSheshadri.Vasudevan@Sun.COM 		}
526010021SSheshadri.Vasudevan@Sun.COM 
526110021SSheshadri.Vasudevan@Sun.COM 		if (copy_len > FDISK_MAX_VALID_CYL_NUM_DIGITS) {
526210021SSheshadri.Vasudevan@Sun.COM 			printf("Input too long\n");
526310021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
526410021SSheshadri.Vasudevan@Sun.COM 			continue;
526510021SSheshadri.Vasudevan@Sun.COM 		}
526610021SSheshadri.Vasudevan@Sun.COM 
526710021SSheshadri.Vasudevan@Sun.COM 		strncpy(numbuf, &buf[mbgb], copy_len);
526810021SSheshadri.Vasudevan@Sun.COM 		numbuf[copy_len] = '\0';
526910021SSheshadri.Vasudevan@Sun.COM 
527010021SSheshadri.Vasudevan@Sun.COM 		for (i = mbgb; i < copy_len + mbgb; i++) {
527110021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
527210021SSheshadri.Vasudevan@Sun.COM 				break;
527310021SSheshadri.Vasudevan@Sun.COM 			}
527410021SSheshadri.Vasudevan@Sun.COM 		}
527510021SSheshadri.Vasudevan@Sun.COM 
527610021SSheshadri.Vasudevan@Sun.COM 		if (i < copy_len + mbgb) {
527710021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
527810021SSheshadri.Vasudevan@Sun.COM 			continue;
527910021SSheshadri.Vasudevan@Sun.COM 		}
528010021SSheshadri.Vasudevan@Sun.COM 
528110021SSheshadri.Vasudevan@Sun.COM 		size = (atoll(numbuf) * (scale));
528210021SSheshadri.Vasudevan@Sun.COM 
528310021SSheshadri.Vasudevan@Sun.COM 		if (size == 0) {
528410021SSheshadri.Vasudevan@Sun.COM 			printf("Zero size is invalid\n");
528510021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
528610021SSheshadri.Vasudevan@Sun.COM 			continue;
528710021SSheshadri.Vasudevan@Sun.COM 		}
528810021SSheshadri.Vasudevan@Sun.COM 
528910021SSheshadri.Vasudevan@Sun.COM 		if (mbgb) {
529010021SSheshadri.Vasudevan@Sun.COM 			size /= sectsize;
529110021SSheshadri.Vasudevan@Sun.COM 		}
529210021SSheshadri.Vasudevan@Sun.COM 
529310021SSheshadri.Vasudevan@Sun.COM 		if (size > (last_free_sec - begsec + 1)) {
529410021SSheshadri.Vasudevan@Sun.COM 			printf("Cylinder boundary beyond the limits");
529510021SSheshadri.Vasudevan@Sun.COM 			printf(" or overlaps with existing");
529610021SSheshadri.Vasudevan@Sun.COM 			printf(" partitions\n");
529710021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
529810021SSheshadri.Vasudevan@Sun.COM 			continue;
529910021SSheshadri.Vasudevan@Sun.COM 		}
530010021SSheshadri.Vasudevan@Sun.COM 
530110021SSheshadri.Vasudevan@Sun.COM 		/*
530210021SSheshadri.Vasudevan@Sun.COM 		 * Adjust the ending sector such that there are no partial
530310021SSheshadri.Vasudevan@Sun.COM 		 * cylinders allocated. But at the same time, make sure it
530410021SSheshadri.Vasudevan@Sun.COM 		 * doesn't over shoot boundaries.
530510021SSheshadri.Vasudevan@Sun.COM 		 */
530610021SSheshadri.Vasudevan@Sun.COM 		spc = FDISK_SECTS_PER_CYL(epp);
530710021SSheshadri.Vasudevan@Sun.COM 		poss_end = begsec + size - 1;
530810021SSheshadri.Vasudevan@Sun.COM 		if (remdr = (poss_end % spc)) {
530910021SSheshadri.Vasudevan@Sun.COM 			poss_end += spc - remdr - 1;
531010021SSheshadri.Vasudevan@Sun.COM 		}
531110021SSheshadri.Vasudevan@Sun.COM 		*endsec = (poss_end > last_free_sec) ? last_free_sec :
531210021SSheshadri.Vasudevan@Sun.COM 		    poss_end;
531310021SSheshadri.Vasudevan@Sun.COM 
531410021SSheshadri.Vasudevan@Sun.COM 		return;
531510021SSheshadri.Vasudevan@Sun.COM 	}
531610021SSheshadri.Vasudevan@Sun.COM }
531710021SSheshadri.Vasudevan@Sun.COM 
531810021SSheshadri.Vasudevan@Sun.COM /*
531910021SSheshadri.Vasudevan@Sun.COM  * ALGORITHM:
532010021SSheshadri.Vasudevan@Sun.COM  * 1. Get the starting and ending sectors/cylinder of the extended partition.
532110021SSheshadri.Vasudevan@Sun.COM  * 2. Keep track of the first free sector/cylinder
532210021SSheshadri.Vasudevan@Sun.COM  * 3. Allow the user to specify the beginning cylinder of the new partition
532310021SSheshadri.Vasudevan@Sun.COM  * 4. Check for the validity of the entered data
532410021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
532510021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
532610021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
532710021SSheshadri.Vasudevan@Sun.COM  * 5. Allow the user to specify the size in cylinders/ human readable form
532810021SSheshadri.Vasudevan@Sun.COM  * 6. Check for the validity of the entered data
532910021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
533010021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
533110021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
533210021SSheshadri.Vasudevan@Sun.COM  *	d) If it is a number lesser than the starting cylinder
533310021SSheshadri.Vasudevan@Sun.COM  * 7. Request partition ID for the new partition.
533410021SSheshadri.Vasudevan@Sun.COM  * 8. Update the first free cylinder available
533510021SSheshadri.Vasudevan@Sun.COM  * 9. Display Success message
533610021SSheshadri.Vasudevan@Sun.COM  */
533710021SSheshadri.Vasudevan@Sun.COM 
533810021SSheshadri.Vasudevan@Sun.COM static void
533910021SSheshadri.Vasudevan@Sun.COM add_logical_drive()
534010021SSheshadri.Vasudevan@Sun.COM {
534110021SSheshadri.Vasudevan@Sun.COM 	uint32_t begsec, endsec;
534210021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
534310021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
534410021SSheshadri.Vasudevan@Sun.COM 	int rval;
534510021SSheshadri.Vasudevan@Sun.COM 
534610021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_get_logical_drive_count(epp) >= MAX_EXT_PARTS) {
534710021SSheshadri.Vasudevan@Sun.COM 		printf("\nNumber of logical drives exceeds limit of %d.\n",
534810021SSheshadri.Vasudevan@Sun.COM 		    MAX_EXT_PARTS);
534910021SSheshadri.Vasudevan@Sun.COM 		printf("Command did not succeed. Press enter to continue\n");
535010021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
535110021SSheshadri.Vasudevan@Sun.COM 		return;
535210021SSheshadri.Vasudevan@Sun.COM 	}
535310021SSheshadri.Vasudevan@Sun.COM 
535410021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
535510021SSheshadri.Vasudevan@Sun.COM 	rval = ext_read_valid_partition_start(&begsec);
535610021SSheshadri.Vasudevan@Sun.COM 	switch (rval) {
535710021SSheshadri.Vasudevan@Sun.COM 		case FDISK_SUCCESS:
535810021SSheshadri.Vasudevan@Sun.COM 			break;
535910021SSheshadri.Vasudevan@Sun.COM 
536010021SSheshadri.Vasudevan@Sun.COM 		case FDISK_EOOBOUND:
536110021SSheshadri.Vasudevan@Sun.COM 			printf("\nNo space left in the extended partition\n");
536210021SSheshadri.Vasudevan@Sun.COM 			printf("Press enter to continue\n");
536310021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
536410021SSheshadri.Vasudevan@Sun.COM 			return;
536510021SSheshadri.Vasudevan@Sun.COM 	}
536610021SSheshadri.Vasudevan@Sun.COM 
536710021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_partition_size(begsec, &endsec);
536810021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
536910021SSheshadri.Vasudevan@Sun.COM 	fdisk_add_logical_drive(epp, begsec, endsec, partid);
537010021SSheshadri.Vasudevan@Sun.COM 
537110021SSheshadri.Vasudevan@Sun.COM 	printf("New partition with ID %d added\n", partid);
537210021SSheshadri.Vasudevan@Sun.COM }
537310021SSheshadri.Vasudevan@Sun.COM 
537410021SSheshadri.Vasudevan@Sun.COM static void
537510021SSheshadri.Vasudevan@Sun.COM ext_change_logical_drive_id()
537610021SSheshadri.Vasudevan@Sun.COM {
537710021SSheshadri.Vasudevan@Sun.COM 	int pno;
537810021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
537910021SSheshadri.Vasudevan@Sun.COM 
538010021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
538110021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
538210021SSheshadri.Vasudevan@Sun.COM 		return;
538310021SSheshadri.Vasudevan@Sun.COM 	}
538410021SSheshadri.Vasudevan@Sun.COM 
538510021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
538610021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
538710021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
538810021SSheshadri.Vasudevan@Sun.COM 	fdisk_change_logical_drive_id(epp, pno, partid);
538910021SSheshadri.Vasudevan@Sun.COM 
539010021SSheshadri.Vasudevan@Sun.COM 	printf("Partition ID of partition %d changed to %d\n", pno, partid);
539110021SSheshadri.Vasudevan@Sun.COM }
539210021SSheshadri.Vasudevan@Sun.COM 
539310021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
539410021SSheshadri.Vasudevan@Sun.COM static void
539510021SSheshadri.Vasudevan@Sun.COM ext_print_logdrive_layout_debug()
539610021SSheshadri.Vasudevan@Sun.COM {
539710021SSheshadri.Vasudevan@Sun.COM 	int pno;
539810021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
539910021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *head = fdisk_get_ld_head(epp);
540010021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
540110021SSheshadri.Vasudevan@Sun.COM 
540210021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
540310021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
540410021SSheshadri.Vasudevan@Sun.COM 		return;
540510021SSheshadri.Vasudevan@Sun.COM 	}
540610021SSheshadri.Vasudevan@Sun.COM 
540710021SSheshadri.Vasudevan@Sun.COM 	printf("\n\n");
540810021SSheshadri.Vasudevan@Sun.COM 	puts("#  start block  end block    abs start    abs end      OSType");
540910021SSheshadri.Vasudevan@Sun.COM 	for (temp = head, pno = 5; temp != NULL; temp = temp->next, pno++) {
541010021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
541110021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[0].systid, namebuff);
541210021SSheshadri.Vasudevan@Sun.COM 		printf("%d: %.10u   %.10u   %.10u   %.10u",
541310021SSheshadri.Vasudevan@Sun.COM 		    pno,
541410021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].relsect),
541510021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].numsect),
541610021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum,
541710021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum + temp->numsect - 1 +
541810021SSheshadri.Vasudevan@Sun.COM 		    MAX_LOGDRIVE_OFFSET);
541910021SSheshadri.Vasudevan@Sun.COM 		printf("   %s\n", namebuff);
542010021SSheshadri.Vasudevan@Sun.COM 		/*
542110021SSheshadri.Vasudevan@Sun.COM 		 * Print the second entry in the EBR which is information
542210021SSheshadri.Vasudevan@Sun.COM 		 * about the location and the size of the next extended
542310021SSheshadri.Vasudevan@Sun.COM 		 * partition.
542410021SSheshadri.Vasudevan@Sun.COM 		 */
542510021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[1].systid, namebuff);
542610021SSheshadri.Vasudevan@Sun.COM 		printf("%d: %.10u   %.10u   %.10s   %.10s",
542710021SSheshadri.Vasudevan@Sun.COM 		    pno,
542810021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].relsect),
542910021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].numsect),
543010021SSheshadri.Vasudevan@Sun.COM 		    "          ", "          ");
543110021SSheshadri.Vasudevan@Sun.COM 		printf("   %s\n", namebuff);
543210021SSheshadri.Vasudevan@Sun.COM 	}
543310021SSheshadri.Vasudevan@Sun.COM }
543410021SSheshadri.Vasudevan@Sun.COM #endif
543510021SSheshadri.Vasudevan@Sun.COM 
543610021SSheshadri.Vasudevan@Sun.COM static void
543710021SSheshadri.Vasudevan@Sun.COM ext_print_logical_drive_layout()
543810021SSheshadri.Vasudevan@Sun.COM {
543910021SSheshadri.Vasudevan@Sun.COM 	int sysid;
544010021SSheshadri.Vasudevan@Sun.COM 	unsigned int startcyl, endcyl, length, percent, remainder;
544110021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
5442*10682SSheshadri.Vasudevan@Sun.COM 	uint32_t part_start;
544310021SSheshadri.Vasudevan@Sun.COM 	struct ipart *fpart;
544410021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
544510021SSheshadri.Vasudevan@Sun.COM 	int numcyl = fdisk_get_disk_geom(epp, PHYSGEOM, NCYL);
544610021SSheshadri.Vasudevan@Sun.COM 	int pno;
544710021SSheshadri.Vasudevan@Sun.COM 
544810021SSheshadri.Vasudevan@Sun.COM 	if (numcyl == EINVAL) {
544910021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Unsupported geometry statistics.\n");
545010021SSheshadri.Vasudevan@Sun.COM 		exit(1);
545110021SSheshadri.Vasudevan@Sun.COM 	}
545210021SSheshadri.Vasudevan@Sun.COM 
545310021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
545410021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
545510021SSheshadri.Vasudevan@Sun.COM 		return;
545610021SSheshadri.Vasudevan@Sun.COM 	}
545710021SSheshadri.Vasudevan@Sun.COM 
545810021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
545910021SSheshadri.Vasudevan@Sun.COM 	printf("Number of cylinders in disk              : %u\n", numcyl);
546010021SSheshadri.Vasudevan@Sun.COM 	printf("Beginning cylinder of extended partition : %u\n",
546110021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_beg_cyl(epp));
546210021SSheshadri.Vasudevan@Sun.COM 	printf("Ending cylinder of extended partition    : %u\n",
546310021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_end_cyl(epp));
546410021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
546510021SSheshadri.Vasudevan@Sun.COM 	printf("Part#   StartCyl   EndCyl     Length    %%     "
546610021SSheshadri.Vasudevan@Sun.COM 	"Part ID (Type)\n");
546710021SSheshadri.Vasudevan@Sun.COM 	printf("=====   ========   ========   =======   ==="
546810021SSheshadri.Vasudevan@Sun.COM 	"   ==============\n");
546910021SSheshadri.Vasudevan@Sun.COM 	for (temp = fdisk_get_ld_head(epp), pno = 5; temp != NULL;
547010021SSheshadri.Vasudevan@Sun.COM 	    temp = temp->next, pno++) {
547110021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
547210021SSheshadri.Vasudevan@Sun.COM 		fpart = &temp->parts[0];
547310021SSheshadri.Vasudevan@Sun.COM 		sysid = fpart->systid;
5474*10682SSheshadri.Vasudevan@Sun.COM 		/*
5475*10682SSheshadri.Vasudevan@Sun.COM 		 * Check if partition id 0x82 is Solaris
5476*10682SSheshadri.Vasudevan@Sun.COM 		 * or a Linux swap. Print the string
5477*10682SSheshadri.Vasudevan@Sun.COM 		 * accordingly.
5478*10682SSheshadri.Vasudevan@Sun.COM 		 */
5479*10682SSheshadri.Vasudevan@Sun.COM 		if (sysid == SUNIXOS) {
5480*10682SSheshadri.Vasudevan@Sun.COM 			part_start = temp->abs_secnum +
5481*10682SSheshadri.Vasudevan@Sun.COM 			    temp->logdrive_offset;
5482*10682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, part_start,
5483*10682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
5484*10682SSheshadri.Vasudevan@Sun.COM 				strcpy(namebuff, LINSWAPstr);
5485*10682SSheshadri.Vasudevan@Sun.COM 			else
5486*10682SSheshadri.Vasudevan@Sun.COM 				strcpy(namebuff, SUstr);
5487*10682SSheshadri.Vasudevan@Sun.COM 		} else {
5488*10682SSheshadri.Vasudevan@Sun.COM 			id_to_name(sysid, namebuff);
5489*10682SSheshadri.Vasudevan@Sun.COM 		}
549010021SSheshadri.Vasudevan@Sun.COM 		startcyl = temp->begcyl;
549110021SSheshadri.Vasudevan@Sun.COM 		endcyl = temp->endcyl;
549210021SSheshadri.Vasudevan@Sun.COM 		if (startcyl == endcyl) {
549310021SSheshadri.Vasudevan@Sun.COM 			length = 1;
549410021SSheshadri.Vasudevan@Sun.COM 		} else {
549510021SSheshadri.Vasudevan@Sun.COM 			length = endcyl - startcyl + 1;
549610021SSheshadri.Vasudevan@Sun.COM 		}
549710021SSheshadri.Vasudevan@Sun.COM 		percent = length * 100 / numcyl;
549810021SSheshadri.Vasudevan@Sun.COM 		if ((remainder = (length * 100 % numcyl)) != 0) {
549910021SSheshadri.Vasudevan@Sun.COM 			if ((remainder * 100 / numcyl) > 50) {
550010021SSheshadri.Vasudevan@Sun.COM 				/* round up */
550110021SSheshadri.Vasudevan@Sun.COM 				percent++;
550210021SSheshadri.Vasudevan@Sun.COM 			}
550310021SSheshadri.Vasudevan@Sun.COM 			/* Else leave the percent as is since it's already */
550410021SSheshadri.Vasudevan@Sun.COM 			/* rounded down */
550510021SSheshadri.Vasudevan@Sun.COM 		}
550610021SSheshadri.Vasudevan@Sun.COM 		if (percent > 100) {
550710021SSheshadri.Vasudevan@Sun.COM 			percent = 100;
550810021SSheshadri.Vasudevan@Sun.COM 		}
550910021SSheshadri.Vasudevan@Sun.COM 		printf("%-5d   %-8u   %-8u   %-7u   %-3d   %-3d (%-.28s)\n",
551010021SSheshadri.Vasudevan@Sun.COM 		    pno, startcyl, endcyl, length, percent, sysid, namebuff);
551110021SSheshadri.Vasudevan@Sun.COM 	}
551210021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
551310021SSheshadri.Vasudevan@Sun.COM 	ext_print_logdrive_layout_debug();
551410021SSheshadri.Vasudevan@Sun.COM #endif
551510021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
551610021SSheshadri.Vasudevan@Sun.COM }
551710021SSheshadri.Vasudevan@Sun.COM 
551810021SSheshadri.Vasudevan@Sun.COM static void
551910021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu()
552010021SSheshadri.Vasudevan@Sun.COM {
552110021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
552210021SSheshadri.Vasudevan@Sun.COM 	printf("a	Add a logical drive\n");
552310021SSheshadri.Vasudevan@Sun.COM 	printf("d	Delete a logical drive\n");
552410021SSheshadri.Vasudevan@Sun.COM 	printf("h	Print this help menu\n");
552510021SSheshadri.Vasudevan@Sun.COM 	printf("i	Change the id of the logical drive\n");
552610021SSheshadri.Vasudevan@Sun.COM 	printf("p	Print the logical drive layout\n");
552710021SSheshadri.Vasudevan@Sun.COM 	printf("r	Return to the main fdisk menu\n");
552810021SSheshadri.Vasudevan@Sun.COM 	printf("        (To commit or cancel the changes)\n");
552910021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
553010021SSheshadri.Vasudevan@Sun.COM }
553110021SSheshadri.Vasudevan@Sun.COM 
553210021SSheshadri.Vasudevan@Sun.COM static void
553310021SSheshadri.Vasudevan@Sun.COM ext_part_menu()
553410021SSheshadri.Vasudevan@Sun.COM {
553510021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
553610021SSheshadri.Vasudevan@Sun.COM 	uchar_t *bbsigp;
553710021SSheshadri.Vasudevan@Sun.COM 	static int bbsig_disp_flag = 1;
553810021SSheshadri.Vasudevan@Sun.COM 
553910021SSheshadri.Vasudevan@Sun.COM 	int i;
554010021SSheshadri.Vasudevan@Sun.COM 
554110021SSheshadri.Vasudevan@Sun.COM 	printf(CLR_SCR);
554210021SSheshadri.Vasudevan@Sun.COM 
554310021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_corrupt_logical_drives(epp)) {
554410021SSheshadri.Vasudevan@Sun.COM 		printf("One or more logical drives seem to be corrupt.\n");
554510021SSheshadri.Vasudevan@Sun.COM 		printf("Displaying only sane logical drives.\n");
554610021SSheshadri.Vasudevan@Sun.COM 	}
554710021SSheshadri.Vasudevan@Sun.COM 
554810021SSheshadri.Vasudevan@Sun.COM 	if (bbsig_disp_flag && fdisk_invalid_bb_sig(epp, &bbsigp)) {
554910021SSheshadri.Vasudevan@Sun.COM 		printf("The following logical drives have a wrong boot block"
555010021SSheshadri.Vasudevan@Sun.COM 		    " signature :\n\n");
555110021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; bbsigp[i]; i++) {
555210021SSheshadri.Vasudevan@Sun.COM 			printf("%d ", bbsigp[i]);
555310021SSheshadri.Vasudevan@Sun.COM 		}
555410021SSheshadri.Vasudevan@Sun.COM 		printf("\n\n");
555510021SSheshadri.Vasudevan@Sun.COM 		printf("They will be corrected when you choose to commit\n");
555610021SSheshadri.Vasudevan@Sun.COM 		bbsig_disp_flag = 0;
555710021SSheshadri.Vasudevan@Sun.COM 	}
555810021SSheshadri.Vasudevan@Sun.COM 
555910021SSheshadri.Vasudevan@Sun.COM 	printf("Extended partition menu\n");
556010021SSheshadri.Vasudevan@Sun.COM 
556110021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
556210021SSheshadri.Vasudevan@Sun.COM 		printf("\nEnter Command (Type h for help) : ");
556310021SSheshadri.Vasudevan@Sun.COM 		if ((ext_read_options(buf)) < 0) {
556410021SSheshadri.Vasudevan@Sun.COM 			printf("\nCommand Options : \n");
556510021SSheshadri.Vasudevan@Sun.COM 			ext_print_help_menu();
556610021SSheshadri.Vasudevan@Sun.COM 			continue;
556710021SSheshadri.Vasudevan@Sun.COM 		}
556810021SSheshadri.Vasudevan@Sun.COM 		switch (buf[0]) {
556910021SSheshadri.Vasudevan@Sun.COM 			case 'a':
557010021SSheshadri.Vasudevan@Sun.COM 				add_logical_drive();
557110021SSheshadri.Vasudevan@Sun.COM 				break;
557210021SSheshadri.Vasudevan@Sun.COM 			case 'd':
557310021SSheshadri.Vasudevan@Sun.COM 				delete_logical_drive();
557410021SSheshadri.Vasudevan@Sun.COM 				break;
557510021SSheshadri.Vasudevan@Sun.COM 			case 'h':
557610021SSheshadri.Vasudevan@Sun.COM 				ext_print_help_menu();
557710021SSheshadri.Vasudevan@Sun.COM 				break;
557810021SSheshadri.Vasudevan@Sun.COM 			case 'i':
557910021SSheshadri.Vasudevan@Sun.COM 				ext_change_logical_drive_id();
558010021SSheshadri.Vasudevan@Sun.COM 				break;
558110021SSheshadri.Vasudevan@Sun.COM 			case 'p':
558210021SSheshadri.Vasudevan@Sun.COM 				ext_print_logical_drive_layout();
558310021SSheshadri.Vasudevan@Sun.COM 				break;
558410021SSheshadri.Vasudevan@Sun.COM 			case 'r':
558510021SSheshadri.Vasudevan@Sun.COM 				printf(CLR_SCR);
558610021SSheshadri.Vasudevan@Sun.COM 				return;
558710021SSheshadri.Vasudevan@Sun.COM 			default : /* NOTREACHED */
558810021SSheshadri.Vasudevan@Sun.COM 				break;
558910021SSheshadri.Vasudevan@Sun.COM 		}
559010021SSheshadri.Vasudevan@Sun.COM 	}
559110021SSheshadri.Vasudevan@Sun.COM }
559210021SSheshadri.Vasudevan@Sun.COM #endif
5593