xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 11246:544fdabd3ed3)
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";
25610682SSheshadri.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 			/*
1184*11246SSharath.Srinivasan@Sun.COM 			 * FDISK_EBADLOGDRIVE, FDISK_ENOLOGDRIVE and
1185*11246SSharath.Srinivasan@Sun.COM 			 * FDISK_EBADMAGIC can be considered as
1186*11246SSharath.Srinivasan@Sun.COM 			 * soft errors and hence 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;
1192*11246SSharath.Srinivasan@Sun.COM 			case FDISK_EBADMAGIC:
1193*11246SSharath.Srinivasan@Sun.COM 				break;
119410021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOVGEOM:
119510021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get virtual"
119610021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
1197*11246SSharath.Srinivasan@Sun.COM 				libfdisk_fini(&epp);
119810021SSheshadri.Vasudevan@Sun.COM 				exit(1);
119910021SSheshadri.Vasudevan@Sun.COM 				break;
120010021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOPGEOM:
120110021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get physical"
120210021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
1203*11246SSharath.Srinivasan@Sun.COM 				libfdisk_fini(&epp);
120410021SSheshadri.Vasudevan@Sun.COM 				exit(1);
120510021SSheshadri.Vasudevan@Sun.COM 				break;
120610021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLGEOM:
120710021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Could not get label"
120810021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
1209*11246SSharath.Srinivasan@Sun.COM 				libfdisk_fini(&epp);
121010021SSheshadri.Vasudevan@Sun.COM 				exit(1);
121110021SSheshadri.Vasudevan@Sun.COM 				break;
121210021SSheshadri.Vasudevan@Sun.COM 			default:
121310021SSheshadri.Vasudevan@Sun.COM 				perror("Failed to initialise libfdisk.\n");
1214*11246SSharath.Srinivasan@Sun.COM 				libfdisk_fini(&epp);
121510021SSheshadri.Vasudevan@Sun.COM 				exit(1);
121610021SSheshadri.Vasudevan@Sun.COM 				break;
121710021SSheshadri.Vasudevan@Sun.COM 		}
121810021SSheshadri.Vasudevan@Sun.COM 	}
121910021SSheshadri.Vasudevan@Sun.COM #endif
122010021SSheshadri.Vasudevan@Sun.COM 
12210Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
12220Sstevel@tonic-gate 	if (io_ffdisk) {
12230Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
12240Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
12250Sstevel@tonic-gate 	}
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
12280Sstevel@tonic-gate 	if (io_Dfdisk) {
12290Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
12300Sstevel@tonic-gate 	}
12310Sstevel@tonic-gate 	if (io_Afdisk) {
12320Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
12360Sstevel@tonic-gate 		/* Check if there is no fdisk table */
12370Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
12380Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
1239251Slclee 				(void) printf(
1240251Slclee 				    "No fdisk table exists. The default"
1241251Slclee 				    " partition for the disk is:\n\n"
1242251Slclee 				    "  a 100%% \"SOLARIS System\" "
1243251Slclee 				    "partition\n\n"
1244251Slclee 				    "Type \"y\" to accept the default "
12450Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
12460Sstevel@tonic-gate 				    "edit the\n partition table.\n");
12477563SPrasad.Singamsetty@Sun.COM 
12487563SPrasad.Singamsetty@Sun.COM 				if (Numcyl > Numcyl_usable)
12497563SPrasad.Singamsetty@Sun.COM 					(void) printf("WARNING: Disk is larger"
12507563SPrasad.Singamsetty@Sun.COM 					    " than 2TB. Solaris partition will"
12517563SPrasad.Singamsetty@Sun.COM 					    " be limited to 2 TB.\n");
12520Sstevel@tonic-gate 			}
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 			/* Edit the partition table as directed */
12550Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 				/* Default scenario */
12580Sstevel@tonic-gate 				nulltbl();
12590Sstevel@tonic-gate 				/* now set up UNIX System partition */
12600Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
126110021SSheshadri.Vasudevan@Sun.COM 				Table[0].relsect = LE_32(heads * sectors);
12627563SPrasad.Singamsetty@Sun.COM 
12637563SPrasad.Singamsetty@Sun.COM 				Table[0].numsect =
126410021SSheshadri.Vasudevan@Sun.COM 				    LE_32((ulong_t)((Numcyl_usable - 1) *
12650Sstevel@tonic-gate 				    heads * sectors));
12667563SPrasad.Singamsetty@Sun.COM 
12670Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
12700Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
12710Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12720Sstevel@tonic-gate 			} else if (io_EFIdisk) {
12730Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
12740Sstevel@tonic-gate 				nulltbl();
12750Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
12767563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
12777563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity - 1));
12780Sstevel@tonic-gate 				if (i != 0) {
1279251Slclee 					(void) fprintf(stderr,
1280251Slclee 					    "Error creating EFI partition\n");
12810Sstevel@tonic-gate 					exit(1);
12820Sstevel@tonic-gate 				}
12830Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12840Sstevel@tonic-gate 			}
12850Sstevel@tonic-gate 		}
12860Sstevel@tonic-gate 	}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
12890Sstevel@tonic-gate 	if (io_debug) {
1290251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
12910Sstevel@tonic-gate 		print_Table();
12920Sstevel@tonic-gate 		if (io_ifdisk) {
1293251Slclee 			(void) fprintf(stderr, "\n");
1294251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
12959786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
12960Sstevel@tonic-gate 		}
12970Sstevel@tonic-gate 	}
12980Sstevel@tonic-gate 
12990Sstevel@tonic-gate 	/* Interactive fdisk mode */
13000Sstevel@tonic-gate 	if (io_ifdisk) {
1301251Slclee 		(void) printf(CLR_SCR);
13020Sstevel@tonic-gate 		disptbl();
1303251Slclee 		for (;;) {
1304251Slclee 			stage0();
13050Sstevel@tonic-gate 			copy_Bootblk_to_Table();
13060Sstevel@tonic-gate 			disptbl();
13070Sstevel@tonic-gate 		}
13080Sstevel@tonic-gate 	}
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
13110Sstevel@tonic-gate 	if (io_Wfdisk)
13120Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
13130Sstevel@tonic-gate 	else if (stdo_flag)
13140Sstevel@tonic-gate 		ffile_write((char *)stdout);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
1317251Slclee 	return (0);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate /*
13210Sstevel@tonic-gate  * read_geom
13220Sstevel@tonic-gate  * Read geometry from specified file (-S).
13230Sstevel@tonic-gate  */
13240Sstevel@tonic-gate 
1325251Slclee static int
1326251Slclee read_geom(char *sgeom)
13270Sstevel@tonic-gate {
13280Sstevel@tonic-gate 	char	line[256];
13290Sstevel@tonic-gate 	FILE *fp;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	/* open the prototype file */
13320Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
13330Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
13340Sstevel@tonic-gate 		    io_sgeom);
13350Sstevel@tonic-gate 		return (1);
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	/* Read a line from the file */
13390Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
13400Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
13410Sstevel@tonic-gate 			continue;
13420Sstevel@tonic-gate 		else {
13430Sstevel@tonic-gate 			line[strlen(line)] = '\0';
1344251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
13450Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
13460Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
13470Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
13480Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
13490Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
13500Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
13510Sstevel@tonic-gate 			    &sectsiz) != 7) {
13520Sstevel@tonic-gate 				(void) fprintf(stderr,
13530Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
13540Sstevel@tonic-gate 				    line);
13550Sstevel@tonic-gate 				return (1);
13560Sstevel@tonic-gate 			}
13570Sstevel@tonic-gate 			break;
13580Sstevel@tonic-gate 		} /* else */
13590Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	if (!io_image) {
13620Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
13630Sstevel@tonic-gate 			(void) fprintf(stderr,
13640Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
13650Sstevel@tonic-gate 			return (1);
13660Sstevel@tonic-gate 		}
13670Sstevel@tonic-gate 	} else {
13680Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
13690Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
13700Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
13710Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
13720Sstevel@tonic-gate 	}
13730Sstevel@tonic-gate 
1374251Slclee 	(void) fclose(fp);
13750Sstevel@tonic-gate 	return (0);
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate /*
13790Sstevel@tonic-gate  * dev_mboot_read
13800Sstevel@tonic-gate  * Read the master boot sector from the device.
13810Sstevel@tonic-gate  */
1382251Slclee static void
1383251Slclee dev_mboot_read(void)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
13860Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 	if (errno == 0)
13890Sstevel@tonic-gate 		return;
13900Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
1391251Slclee 		(void) fprintf(stderr,
13920Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
13930Sstevel@tonic-gate 		    Dfltdev);
13940Sstevel@tonic-gate 		if (!io_image)
13950Sstevel@tonic-gate 			exit(1);
13960Sstevel@tonic-gate 	}
13970Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1398251Slclee 		(void) fprintf(stderr,
13990Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
14000Sstevel@tonic-gate 		    Dfltdev);
14010Sstevel@tonic-gate 		if (!io_image)
14020Sstevel@tonic-gate 			exit(1);
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * dev_mboot_write
14080Sstevel@tonic-gate  * Write the master boot sector to the device.
14090Sstevel@tonic-gate  */
1410251Slclee static void
14116549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
14120Sstevel@tonic-gate {
14130Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
14146478Sbharding 	int	clr_efi = -1;
14150Sstevel@tonic-gate 
14160Sstevel@tonic-gate 	if (io_readonly)
1417251Slclee 		return;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	if (io_debug) {
1420251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
14210Sstevel@tonic-gate 		print_Table();
14220Sstevel@tonic-gate 		if (io_ifdisk) {
1423251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
14249786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
14250Sstevel@tonic-gate 		}
14260Sstevel@tonic-gate 	}
14270Sstevel@tonic-gate 
14286478Sbharding 	/*
14296478Sbharding 	 * If the new table has any Solaris partitions and the old
14306478Sbharding 	 * table does not have an entry that describes it
14316478Sbharding 	 * exactly then clear the old vtoc (if any).
14326478Sbharding 	 */
14330Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14340Sstevel@tonic-gate 
14356478Sbharding 		/* We only care about potential Solaris parts. */
14360Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
14370Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
14380Sstevel@tonic-gate 			continue;
14390Sstevel@tonic-gate 
14406478Sbharding 		/* Does the old table have an exact entry for the new entry? */
14416478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14426478Sbharding 
14436478Sbharding 			/* We only care about old Solaris partitions. */
14446478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
14456478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
14466478Sbharding 
14476478Sbharding 				/* Is this old one the same as a new one? */
14486478Sbharding 				if ((Old_Table[old_pt].relsect ==
14496478Sbharding 				    Table[new_pt].relsect) &&
14506478Sbharding 				    (Old_Table[old_pt].numsect ==
14516478Sbharding 				    Table[new_pt].numsect))
14526478Sbharding 					break; /* Yes */
14536478Sbharding 			}
14546478Sbharding 		}
14556478Sbharding 
14566478Sbharding 		/* Did a solaris partition change location or size? */
14576478Sbharding 		if (old_pt >= FD_NUMPART) {
14586478Sbharding 			/* Yes clear old vtoc */
14596478Sbharding 			if (io_debug) {
14606478Sbharding 				(void) fprintf(stderr,
14616478Sbharding 				    "Clearing VTOC labels from NEW"
14626478Sbharding 				    " table\n");
14636478Sbharding 			}
14646478Sbharding 			clear_vtoc(NEW, new_pt);
14656478Sbharding 		}
14666478Sbharding 	}
14676478Sbharding 
14686478Sbharding 
14696478Sbharding 	/* see if the old table had EFI */
14706478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14716478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
14726478Sbharding 			clr_efi = old_pt;
14730Sstevel@tonic-gate 		}
14740Sstevel@tonic-gate 	}
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
14770Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14780Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
14790Sstevel@tonic-gate 			continue;
14800Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14815169Slclee 			if ((Old_Table[old_pt].systid ==
14825169Slclee 			    Table[new_pt].systid) &&
14835169Slclee 			    (Old_Table[old_pt].relsect ==
14845169Slclee 			    Table[new_pt].relsect) &&
14855169Slclee 			    (Old_Table[old_pt].numsect ==
14865169Slclee 			    Table[new_pt].numsect))
14875169Slclee 				break;
14880Sstevel@tonic-gate 		}
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		/*
14910Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
14920Sstevel@tonic-gate 		 * the EFI GPT
14930Sstevel@tonic-gate 		 */
14940Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
14950Sstevel@tonic-gate 			clr_efi = 0;
14960Sstevel@tonic-gate 		}
14970Sstevel@tonic-gate 		break;
14980Sstevel@tonic-gate 	}
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	/* clear labels if necessary */
15010Sstevel@tonic-gate 	if (clr_efi >= 0) {
15020Sstevel@tonic-gate 		if (io_debug) {
1503251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
15060Sstevel@tonic-gate 			if (io_debug) {
1507251Slclee 				(void) fprintf(stderr,
1508251Slclee 				    "\tError %d clearing EFI labels"
15090Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
15100Sstevel@tonic-gate 				    error);
15110Sstevel@tonic-gate 			}
15120Sstevel@tonic-gate 		}
15130Sstevel@tonic-gate 	}
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1516251Slclee 		(void) fprintf(stderr,
15170Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
15180Sstevel@tonic-gate 		    Dfltdev);
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 	if (errno == 0)
15210Sstevel@tonic-gate 		return;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/* write to disk drive */
15240Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1525251Slclee 		(void) fprintf(stderr,
15260Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
15270Sstevel@tonic-gate 		    Dfltdev);
15280Sstevel@tonic-gate 		exit(1);
15290Sstevel@tonic-gate 	}
15300Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1531251Slclee 		(void) fprintf(stderr,
15320Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
15330Sstevel@tonic-gate 		    Dfltdev);
15340Sstevel@tonic-gate 		exit(1);
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /*
15390Sstevel@tonic-gate  * mboot_read
15400Sstevel@tonic-gate  * Read the prototype boot records from the files.
15410Sstevel@tonic-gate  */
1542251Slclee static void
1543251Slclee mboot_read(void)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	int mDev, i;
15460Sstevel@tonic-gate 	struct ipart *part;
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate #if defined(i386) || defined(sparc)
15490Sstevel@tonic-gate 	/*
15500Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
15510Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
15520Sstevel@tonic-gate 	 */
15530Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
15540Sstevel@tonic-gate 		/*
15550Sstevel@tonic-gate 		 * Bug ID 1249035:
15560Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
15570Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
15580Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
15590Sstevel@tonic-gate 		 */
15600Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
15610Sstevel@tonic-gate 	}
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	/* First read in the master boot record */
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 	/* Open the master boot proto file */
15660Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1567251Slclee 		(void) fprintf(stderr,
15680Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
15690Sstevel@tonic-gate 		    io_mboot);
15700Sstevel@tonic-gate 		exit(1);
15710Sstevel@tonic-gate 	}
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	/* Read the master boot program */
15740Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
15750Sstevel@tonic-gate 	    (struct mboot)) {
1576251Slclee 		(void) fprintf(stderr,
15770Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
15780Sstevel@tonic-gate 		    io_mboot);
15790Sstevel@tonic-gate 		exit(1);
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	/* Is this really a master boot record? */
158310021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(BootCod.signature) != MBB_MAGIC) {
1584251Slclee 		(void) fprintf(stderr,
15850Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1586251Slclee 		(void) fprintf(stderr,
1587251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
158810021SSheshadri.Vasudevan@Sun.COM 		    LE_16(BootCod.signature), MBB_MAGIC);
15890Sstevel@tonic-gate 		exit(1);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
1592251Slclee 	(void) close(mDev);
15930Sstevel@tonic-gate #else
15940Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
15950Sstevel@tonic-gate #endif
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
15980Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
15990Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1600251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
16010Sstevel@tonic-gate 	}
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate }
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate /*
16060Sstevel@tonic-gate  * fill_patt
16070Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
16080Sstevel@tonic-gate  */
1609251Slclee static void
1610251Slclee fill_patt(void)
16110Sstevel@tonic-gate {
1612251Slclee 	int	*buff_ptr, i;
16136549Sbharding 	off_t	*off_ptr;
16140Sstevel@tonic-gate 	int	io_fpatt = 0;
16150Sstevel@tonic-gate 	int	io_ipatt = 0;
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
16180Sstevel@tonic-gate 		io_fpatt++;
16190Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
16200Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
16210Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
16225169Slclee 			*buff_ptr = io_ipatt;
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	/*
16260Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
16270Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
16280Sstevel@tonic-gate 	 * for io_size blocks.
16290Sstevel@tonic-gate 	 */
16300Sstevel@tonic-gate 	while (io_size--) {
16316549Sbharding 		off_ptr = (off_t *)Bootsect;
16320Sstevel@tonic-gate 		if (!io_fpatt) {
16336549Sbharding 			for (i = 0; i < sectsiz;
16346549Sbharding 			    i += sizeof (off_t), off_ptr++)
16356549Sbharding 				*off_ptr = io_offset;
16360Sstevel@tonic-gate 		}
16370Sstevel@tonic-gate 		/* Write the data to disk */
16386549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16396549Sbharding 		    SEEK_SET) == -1) {
1640251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16415169Slclee 			    Dfltdev);
16420Sstevel@tonic-gate 			exit(1);
16430Sstevel@tonic-gate 		}
16440Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1645251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
16465169Slclee 			    Dfltdev);
16470Sstevel@tonic-gate 			exit(1);
16480Sstevel@tonic-gate 		}
16490Sstevel@tonic-gate 	} /* while (--io_size); */
16500Sstevel@tonic-gate }
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate  * abs_read
16540Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
16550Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
16560Sstevel@tonic-gate  */
1657251Slclee static void
1658251Slclee abs_read(void)
1659251Slclee {
16600Sstevel@tonic-gate 	int c;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	while (io_size--) {
16636549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16646549Sbharding 		    SEEK_SET) == -1) {
1665251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16660Sstevel@tonic-gate 			    Dfltdev);
16670Sstevel@tonic-gate 			exit(1);
16680Sstevel@tonic-gate 		}
16690Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1670251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
16710Sstevel@tonic-gate 			    Dfltdev);
16720Sstevel@tonic-gate 			exit(1);
16730Sstevel@tonic-gate 		}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 		/* Write to standard ouptut */
1676251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
16770Sstevel@tonic-gate 			if (c >= 0) {
16780Sstevel@tonic-gate 				if (io_debug)
1679251Slclee 					(void) fprintf(stderr,
1680251Slclee 					    "fdisk: Output warning: %d of %d"
1681251Slclee 					    " characters written.\n",
1682251Slclee 					    c, sectsiz);
16830Sstevel@tonic-gate 				exit(2);
16840Sstevel@tonic-gate 			} else {
16850Sstevel@tonic-gate 				perror("write error on output file.");
16860Sstevel@tonic-gate 				exit(2);
16870Sstevel@tonic-gate 			}
16880Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
16890Sstevel@tonic-gate 			/* != sectsiz) */
16900Sstevel@tonic-gate 	} /* while (--io_size); */
16910Sstevel@tonic-gate 	exit(0);
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate /*
16950Sstevel@tonic-gate  * abs_write
16960Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
16970Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
16980Sstevel@tonic-gate  */
1699251Slclee static void
1700251Slclee abs_write(void)
17010Sstevel@tonic-gate {
17020Sstevel@tonic-gate 	int c, i;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	while (io_size--) {
17050Sstevel@tonic-gate 		int part_exit = 0;
17060Sstevel@tonic-gate 		/* Read from standard input */
17070Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
17080Sstevel@tonic-gate 			if (c >= 0) {
17090Sstevel@tonic-gate 				if (io_debug)
1710251Slclee 				(void) fprintf(stderr,
17110Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
17120Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
17135169Slclee 				    c, sectsiz);
17140Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
17150Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
17160Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
17170Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
17180Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
17190Sstevel@tonic-gate 					Bootsect[i++] = 0;
17200Sstevel@tonic-gate 				}
17210Sstevel@tonic-gate 				part_exit++;
17220Sstevel@tonic-gate 			} else {
17230Sstevel@tonic-gate 				perror("read error on input file.");
17240Sstevel@tonic-gate 				exit(2);
17250Sstevel@tonic-gate 			}
17260Sstevel@tonic-gate 
17270Sstevel@tonic-gate 		}
17280Sstevel@tonic-gate 		/* Write to disk drive */
17296549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
17306549Sbharding 		    SEEK_SET) == -1) {
1731251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
17320Sstevel@tonic-gate 			    Dfltdev);
17330Sstevel@tonic-gate 			exit(1);
17340Sstevel@tonic-gate 		}
17350Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1736251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
17370Sstevel@tonic-gate 			    Dfltdev);
17380Sstevel@tonic-gate 			exit(1);
17390Sstevel@tonic-gate 		}
17400Sstevel@tonic-gate 		if (part_exit)
17410Sstevel@tonic-gate 		exit(0);
17420Sstevel@tonic-gate 	} /* while (--io_size); */
17430Sstevel@tonic-gate 	exit(1);
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate 
1746251Slclee 
17470Sstevel@tonic-gate /*
17480Sstevel@tonic-gate  * load
17490Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
17500Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
17510Sstevel@tonic-gate  */
17520Sstevel@tonic-gate 
1753251Slclee static void
1754251Slclee load(int funct, char *file)
17550Sstevel@tonic-gate {
17560Sstevel@tonic-gate 	int	id;
17570Sstevel@tonic-gate 	int	act;
17580Sstevel@tonic-gate 	int	bhead;
17590Sstevel@tonic-gate 	int	bsect;
17600Sstevel@tonic-gate 	int	bcyl;
17610Sstevel@tonic-gate 	int	ehead;
17620Sstevel@tonic-gate 	int	esect;
17630Sstevel@tonic-gate 	int	ecyl;
17647563SPrasad.Singamsetty@Sun.COM 	uint32_t	rsect;
17657563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
17660Sstevel@tonic-gate 	char	line[256];
17670Sstevel@tonic-gate 	int	i = 0;
17680Sstevel@tonic-gate 	int	j;
17690Sstevel@tonic-gate 	FILE *fp;
177010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
177110021SSheshadri.Vasudevan@Sun.COM 	int 	ext_part_present = 0;
177210021SSheshadri.Vasudevan@Sun.COM 	uint32_t	begsec, endsec, relsect;
177310021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
177410021SSheshadri.Vasudevan@Sun.COM 	int part_count = 0, ldcnt = 0;
177510021SSheshadri.Vasudevan@Sun.COM 	uint32_t ext_beg_sec, ext_end_sec;
177610021SSheshadri.Vasudevan@Sun.COM 	uint32_t old_ext_beg_sec = 0, old_ext_num_sec = 0;
177710021SSheshadri.Vasudevan@Sun.COM 	uint32_t new_ext_beg_sec = 0, new_ext_num_sec = 0;
177810021SSheshadri.Vasudevan@Sun.COM 	int ext_part_inited = 0;
177910021SSheshadri.Vasudevan@Sun.COM 	uchar_t	systid;
178010021SSheshadri.Vasudevan@Sun.COM #endif
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	switch (funct) {
17830Sstevel@tonic-gate 
17845169Slclee 	case LOADFILE:
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate 		/*
17870Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
17880Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
17890Sstevel@tonic-gate 		 * fdisk_file).
17900Sstevel@tonic-gate 		 */
17910Sstevel@tonic-gate 		nulltbl();
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 		/* Open the prototype file */
17940Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
17950Sstevel@tonic-gate 			(void) fprintf(stderr,
17960Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
17970Sstevel@tonic-gate 			    file);
17980Sstevel@tonic-gate 			exit(1);
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate 		/* Read a line from the file */
18020Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
18030Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
18040Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
18050Sstevel@tonic-gate 				continue;
18060Sstevel@tonic-gate 			}
180710021SSheshadri.Vasudevan@Sun.COM #ifdef i386
180810021SSheshadri.Vasudevan@Sun.COM 			part_count++;
180910021SSheshadri.Vasudevan@Sun.COM 
181010021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended((uchar_t)id)) {
181110021SSheshadri.Vasudevan@Sun.COM 				if (ext_part_present) {
181210021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Extended partition"
181310021SSheshadri.Vasudevan@Sun.COM 					    " already exists\n");
181410021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
181510021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
181610021SSheshadri.Vasudevan@Sun.COM 					exit(1);
181710021SSheshadri.Vasudevan@Sun.COM 				}
181810021SSheshadri.Vasudevan@Sun.COM 				ext_part_present = 1;
181910021SSheshadri.Vasudevan@Sun.COM 				/*
182010021SSheshadri.Vasudevan@Sun.COM 				 * If the existing extended partition's start
182110021SSheshadri.Vasudevan@Sun.COM 				 * and size matches the new one, do not
182210021SSheshadri.Vasudevan@Sun.COM 				 * initialize the extended partition EBR
182310021SSheshadri.Vasudevan@Sun.COM 				 * (Extended Boot Record) because there could
182410021SSheshadri.Vasudevan@Sun.COM 				 * be existing logical drives.
182510021SSheshadri.Vasudevan@Sun.COM 				 */
182610021SSheshadri.Vasudevan@Sun.COM 				for (i = 0; i < FD_NUMPART; i++) {
182710021SSheshadri.Vasudevan@Sun.COM 					systid = Old_Table[i].systid;
182810021SSheshadri.Vasudevan@Sun.COM 					if (fdisk_is_dos_extended(systid)) {
182910021SSheshadri.Vasudevan@Sun.COM 						old_ext_beg_sec =
183010021SSheshadri.Vasudevan@Sun.COM 						    Old_Table[i].relsect;
183110021SSheshadri.Vasudevan@Sun.COM 						old_ext_num_sec =
183210021SSheshadri.Vasudevan@Sun.COM 						    Old_Table[i].numsect;
183310021SSheshadri.Vasudevan@Sun.COM 						break;
183410021SSheshadri.Vasudevan@Sun.COM 					}
183510021SSheshadri.Vasudevan@Sun.COM 				}
183610021SSheshadri.Vasudevan@Sun.COM 				new_ext_beg_sec = rsect;
183710021SSheshadri.Vasudevan@Sun.COM 				new_ext_num_sec = numsect;
183810021SSheshadri.Vasudevan@Sun.COM 				if ((old_ext_beg_sec != new_ext_beg_sec) ||
183910021SSheshadri.Vasudevan@Sun.COM 				    (old_ext_num_sec != new_ext_num_sec)) {
184010021SSheshadri.Vasudevan@Sun.COM 					fdisk_init_ext_part(epp,
184110021SSheshadri.Vasudevan@Sun.COM 					    new_ext_beg_sec, new_ext_num_sec);
184210021SSheshadri.Vasudevan@Sun.COM 					ext_part_inited = 1;
184310021SSheshadri.Vasudevan@Sun.COM 				}
184410021SSheshadri.Vasudevan@Sun.COM 			}
184510021SSheshadri.Vasudevan@Sun.COM 
184610021SSheshadri.Vasudevan@Sun.COM 			if (part_count > FD_NUMPART) {
184710021SSheshadri.Vasudevan@Sun.COM 				/* This line should be logical drive info */
184810021SSheshadri.Vasudevan@Sun.COM 				int offset = MAX_LOGDRIVE_OFFSET;
184910021SSheshadri.Vasudevan@Sun.COM 				if (!ext_part_present) {
185010021SSheshadri.Vasudevan@Sun.COM 					/* Erroneous input file */
185110021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "More than 4 primary"
185210021SSheshadri.Vasudevan@Sun.COM 					    " partitions found in input\n");
185310021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Exiting...\n");
185410021SSheshadri.Vasudevan@Sun.COM 					exit(1);
185510021SSheshadri.Vasudevan@Sun.COM 				}
185610021SSheshadri.Vasudevan@Sun.COM 
185710021SSheshadri.Vasudevan@Sun.COM 				if (numsect == 0) {
185810021SSheshadri.Vasudevan@Sun.COM 					continue;
185910021SSheshadri.Vasudevan@Sun.COM 				}
186010021SSheshadri.Vasudevan@Sun.COM 
186110021SSheshadri.Vasudevan@Sun.COM 				/*
186210021SSheshadri.Vasudevan@Sun.COM 				 * If the start and size of the existing
186310021SSheshadri.Vasudevan@Sun.COM 				 * extended partition matches the new one and
186410021SSheshadri.Vasudevan@Sun.COM 				 * new logical drives are being defined via
186510021SSheshadri.Vasudevan@Sun.COM 				 * the input file, initialize the EBR.
186610021SSheshadri.Vasudevan@Sun.COM 				 */
186710021SSheshadri.Vasudevan@Sun.COM 				if (!ext_part_inited) {
186810021SSheshadri.Vasudevan@Sun.COM 					fdisk_init_ext_part(epp,
186910021SSheshadri.Vasudevan@Sun.COM 					    new_ext_beg_sec, new_ext_num_sec);
187010021SSheshadri.Vasudevan@Sun.COM 					ext_part_inited = 1;
187110021SSheshadri.Vasudevan@Sun.COM 				}
187210021SSheshadri.Vasudevan@Sun.COM 
187310021SSheshadri.Vasudevan@Sun.COM 				begsec = rsect - offset;
187410021SSheshadri.Vasudevan@Sun.COM 				if ((ldcnt =
187510021SSheshadri.Vasudevan@Sun.COM 				    fdisk_get_logical_drive_count(epp)) == 0) {
187610021SSheshadri.Vasudevan@Sun.COM 					/* Adding the first logical drive */
187710021SSheshadri.Vasudevan@Sun.COM 					/*
187810021SSheshadri.Vasudevan@Sun.COM 					 * Make sure that begsec doesnt wrap
187910021SSheshadri.Vasudevan@Sun.COM 					 * around. This can happen if rsect is
188010021SSheshadri.Vasudevan@Sun.COM 					 * less than offset.
188110021SSheshadri.Vasudevan@Sun.COM 					 */
188210021SSheshadri.Vasudevan@Sun.COM 					if (rsect < offset) {
188310021SSheshadri.Vasudevan@Sun.COM 						fprintf(stderr, "Minimum of "
188410021SSheshadri.Vasudevan@Sun.COM 						    "63 free sectors required "
188510021SSheshadri.Vasudevan@Sun.COM 						    "before the beginning of "
188610021SSheshadri.Vasudevan@Sun.COM 						    "a logical drive.");
188710021SSheshadri.Vasudevan@Sun.COM 						exit(1);
188810021SSheshadri.Vasudevan@Sun.COM 					}
188910021SSheshadri.Vasudevan@Sun.COM 					/*
189010021SSheshadri.Vasudevan@Sun.COM 					 * Check if the first logical drive
189110021SSheshadri.Vasudevan@Sun.COM 					 * is out of order. In that case, do
189210021SSheshadri.Vasudevan@Sun.COM 					 * not subtract MAX_LOGDRIVE_OFFSET
189310021SSheshadri.Vasudevan@Sun.COM 					 * from the given start of partition.
189410021SSheshadri.Vasudevan@Sun.COM 					 */
189510021SSheshadri.Vasudevan@Sun.COM 					if (begsec != new_ext_beg_sec) {
189610021SSheshadri.Vasudevan@Sun.COM 						begsec = rsect;
189710021SSheshadri.Vasudevan@Sun.COM 						offset = 0;
189810021SSheshadri.Vasudevan@Sun.COM 					}
189910021SSheshadri.Vasudevan@Sun.COM 				}
190010021SSheshadri.Vasudevan@Sun.COM 				if (ldcnt >= MAX_EXT_PARTS) {
190110021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "\nError : Number of "
190210021SSheshadri.Vasudevan@Sun.COM 					    "logical drives exceeds limit of "
190310021SSheshadri.Vasudevan@Sun.COM 					    "%d.\n", MAX_EXT_PARTS);
190410021SSheshadri.Vasudevan@Sun.COM 					exit(1);
190510021SSheshadri.Vasudevan@Sun.COM 				}
190610021SSheshadri.Vasudevan@Sun.COM 
190710021SSheshadri.Vasudevan@Sun.COM 				if (id > FDISK_MAX_VALID_PART_ID) {
190810021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "Invalid partition "
190910021SSheshadri.Vasudevan@Sun.COM 					    "ID\n");
191010021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
191110021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
191210021SSheshadri.Vasudevan@Sun.COM 					exit(1);
191310021SSheshadri.Vasudevan@Sun.COM 				}
191410021SSheshadri.Vasudevan@Sun.COM 
191510021SSheshadri.Vasudevan@Sun.COM 				endsec = rsect + numsect - 1;
191610021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_validate_logical_drive(epp,
191710021SSheshadri.Vasudevan@Sun.COM 				    begsec, offset, numsect) == 0) {
191810021SSheshadri.Vasudevan@Sun.COM 					if (id == EFI_PMBR) {
191910021SSheshadri.Vasudevan@Sun.COM 						fprintf(stderr, "EFI "
192010021SSheshadri.Vasudevan@Sun.COM 						    "partitions not supported "
192110021SSheshadri.Vasudevan@Sun.COM 						    "inside extended "
192210021SSheshadri.Vasudevan@Sun.COM 						    "partition\n");
192310021SSheshadri.Vasudevan@Sun.COM 						exit(1);
192410021SSheshadri.Vasudevan@Sun.COM 					}
192510021SSheshadri.Vasudevan@Sun.COM 					fdisk_add_logical_drive(epp, begsec,
192610021SSheshadri.Vasudevan@Sun.COM 					    endsec, id);
192710021SSheshadri.Vasudevan@Sun.COM 					continue;
192810021SSheshadri.Vasudevan@Sun.COM 				} else {
192910021SSheshadri.Vasudevan@Sun.COM 					fprintf(stderr, "fdisk: Error on"
193010021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
193110021SSheshadri.Vasudevan@Sun.COM 					exit(1);
193210021SSheshadri.Vasudevan@Sun.COM 				}
193310021SSheshadri.Vasudevan@Sun.COM 			}
193410021SSheshadri.Vasudevan@Sun.COM #endif
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 			/*
19370Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
19380Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
19390Sstevel@tonic-gate 			 */
19400Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
19410Sstevel@tonic-gate 				(void) fprintf(stderr,
19420Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
19430Sstevel@tonic-gate 				    line);
19440Sstevel@tonic-gate 				exit(1);
19450Sstevel@tonic-gate 			}
19468904SBarry.Harding@Sun.COM 
19478904SBarry.Harding@Sun.COM 			if (entry_from_old_table(id, act, bhead, bsect,
19488904SBarry.Harding@Sun.COM 			    bcyl, ehead, esect, ecyl, rsect, numsect)) {
19498904SBarry.Harding@Sun.COM 				/*
19508904SBarry.Harding@Sun.COM 				 * If we got here it means we copied an
19518904SBarry.Harding@Sun.COM 				 * unmodified entry. So there is no need
19528904SBarry.Harding@Sun.COM 				 * to insert it in the table or do any
19538904SBarry.Harding@Sun.COM 				 * checks against disk size.
19548904SBarry.Harding@Sun.COM 				 *
19558904SBarry.Harding@Sun.COM 				 * This is a work around on the following
19568904SBarry.Harding@Sun.COM 				 * situation (for IDE disks, at least):
19578904SBarry.Harding@Sun.COM 				 * Different operation systems calculate
19588904SBarry.Harding@Sun.COM 				 * disk size different ways, of which there
19598904SBarry.Harding@Sun.COM 				 * are two main ways.
19608904SBarry.Harding@Sun.COM 				 *
19618904SBarry.Harding@Sun.COM 				 * The first, rounds the disk size to modulo
19628904SBarry.Harding@Sun.COM 				 * cylinder size (virtual made-up cylinder
19638904SBarry.Harding@Sun.COM 				 * usually based on maximum number of heads
19648904SBarry.Harding@Sun.COM 				 * and sectors in partition table fields).
19658904SBarry.Harding@Sun.COM 				 * Our OS's (for IDE) and most other "Unix"
19668904SBarry.Harding@Sun.COM 				 * type OS's do this.
19678904SBarry.Harding@Sun.COM 				 *
19688904SBarry.Harding@Sun.COM 				 * The second, uses every single block
19698904SBarry.Harding@Sun.COM 				 * on the disk (to maximize available space).
19708904SBarry.Harding@Sun.COM 				 * Since disk manufactures do not know about
19718904SBarry.Harding@Sun.COM 				 * "virtual cylinders", there are some number
19728904SBarry.Harding@Sun.COM 				 * of blocks that make up a partial cylinder
19738904SBarry.Harding@Sun.COM 				 * at the end of the disk.
19748904SBarry.Harding@Sun.COM 				 *
19758904SBarry.Harding@Sun.COM 				 * The difference between these two methods
19768904SBarry.Harding@Sun.COM 				 * is where the problem is. When one
19778904SBarry.Harding@Sun.COM 				 * tries to install Solaris/OpenSolaris on
19788904SBarry.Harding@Sun.COM 				 * a disk that has another OS using that
19798904SBarry.Harding@Sun.COM 				 * "partial cylinder", install fails. It fails
19808904SBarry.Harding@Sun.COM 				 * since fdisk thinks its asked to create a
19818904SBarry.Harding@Sun.COM 				 * partition with the -F option that contains
19828904SBarry.Harding@Sun.COM 				 * a partition that runs off the end of the
19838904SBarry.Harding@Sun.COM 				 * disk.
19848904SBarry.Harding@Sun.COM 				 */
19858904SBarry.Harding@Sun.COM 				continue;
19868904SBarry.Harding@Sun.COM 			}
19878904SBarry.Harding@Sun.COM 
19880Sstevel@tonic-gate 			/*
19890Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
19900Sstevel@tonic-gate 			 * in table
19910Sstevel@tonic-gate 			 */
19920Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
19930Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
19940Sstevel@tonic-gate 				(void) fprintf(stderr,
19950Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
19960Sstevel@tonic-gate 				    line);
19970Sstevel@tonic-gate 				exit(1);
19980Sstevel@tonic-gate 			}
19990Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
20000Sstevel@tonic-gate 
20010Sstevel@tonic-gate 		if (verify_tbl() < 0) {
2002251Slclee 			(void) fprintf(stderr,
20030Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
20040Sstevel@tonic-gate 			exit(1);
20050Sstevel@tonic-gate 		}
20060Sstevel@tonic-gate 
2007251Slclee 		(void) fclose(fp);
20080Sstevel@tonic-gate 		return;
20090Sstevel@tonic-gate 
20105169Slclee 	case LOADDEL:
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
2013251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
2014251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
2015251Slclee 			(void) fprintf(stderr,
2016251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2017251Slclee 			exit(1);
2018251Slclee 		}
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 		/* Find the exact entry in the table */
20210Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
20220Sstevel@tonic-gate 			if (Table[i].systid == id &&
20230Sstevel@tonic-gate 			    Table[i].bootid == act &&
20240Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
20250Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
20265169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
2027251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
20280Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
20290Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
20305169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
2031251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
203210021SSheshadri.Vasudevan@Sun.COM 			    Table[i].relsect == LE_32(rsect) &&
203310021SSheshadri.Vasudevan@Sun.COM 			    Table[i].numsect == LE_32(numsect)) {
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 				/*
20360Sstevel@tonic-gate 				 * Found the entry. Now move rest of
20370Sstevel@tonic-gate 				 * entries up toward the top of the
20380Sstevel@tonic-gate 				 * table, leaving available entries at
20390Sstevel@tonic-gate 				 * the end of the fdisk table.
20400Sstevel@tonic-gate 				 */
2041251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
2042251Slclee 					Table[j].systid = Table[j + 1].systid;
2043251Slclee 					Table[j].bootid = Table[j + 1].bootid;
2044251Slclee 					Table[j].beghead = Table[j + 1].beghead;
2045251Slclee 					Table[j].begsect = Table[j + 1].begsect;
2046251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
2047251Slclee 					Table[j].endhead = Table[j + 1].endhead;
2048251Slclee 					Table[j].endsect = Table[j + 1].endsect;
2049251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
2050251Slclee 					Table[j].relsect = Table[j + 1].relsect;
2051251Slclee 					Table[j].numsect = Table[j + 1].numsect;
20520Sstevel@tonic-gate 				}
20530Sstevel@tonic-gate 
20540Sstevel@tonic-gate 				/*
20550Sstevel@tonic-gate 				 * Mark the last entry as unused in case
20560Sstevel@tonic-gate 				 * all table entries were in use prior
20570Sstevel@tonic-gate 				 * to the deletion.
20580Sstevel@tonic-gate 				 */
20590Sstevel@tonic-gate 
2060251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
2061251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
206210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
206310021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_is_dos_extended(id)) {
206410021SSheshadri.Vasudevan@Sun.COM 					fdisk_delete_ext_part(epp);
206510021SSheshadri.Vasudevan@Sun.COM 				}
206610021SSheshadri.Vasudevan@Sun.COM #endif
20670Sstevel@tonic-gate 				return;
20680Sstevel@tonic-gate 			}
20690Sstevel@tonic-gate 		}
207010021SSheshadri.Vasudevan@Sun.COM 
207110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
207210021SSheshadri.Vasudevan@Sun.COM 		ldcnt = FD_NUMPART + 1;
207310021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
207410021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
207510021SSheshadri.Vasudevan@Sun.COM 			relsect = temp->abs_secnum + temp->logdrive_offset;
207610021SSheshadri.Vasudevan@Sun.COM 			if (temp->parts[0].systid == id &&
207710021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].bootid == act &&
207810021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].beghead == bhead &&
207910021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begsect == ((bsect & 0x3f) |
208010021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
208110021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begcyl == (uchar_t)(bcyl & 0xff) &&
208210021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endhead == ehead &&
208310021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endsect == ((esect & 0x3f) |
208410021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
208510021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endcyl == (uchar_t)(ecyl & 0xff) &&
208610021SSheshadri.Vasudevan@Sun.COM 			    relsect == LE_32(rsect) &&
208710021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].numsect == LE_32(numsect)) {
208810021SSheshadri.Vasudevan@Sun.COM 				fdisk_delete_logical_drive(epp, ldcnt);
208910021SSheshadri.Vasudevan@Sun.COM 				return;
209010021SSheshadri.Vasudevan@Sun.COM 			}
209110021SSheshadri.Vasudevan@Sun.COM 			ldcnt++;
209210021SSheshadri.Vasudevan@Sun.COM 		}
209310021SSheshadri.Vasudevan@Sun.COM #endif
209410021SSheshadri.Vasudevan@Sun.COM 
2095251Slclee 		(void) fprintf(stderr,
20960Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
20970Sstevel@tonic-gate 		    "	\"%s\"\n",
20980Sstevel@tonic-gate 		    file);
20990Sstevel@tonic-gate 		exit(1);
21008333SSuhasini.Peddada@Sun.COM 		/* FALLTHRU */
21010Sstevel@tonic-gate 
21025169Slclee 	case LOADADD:
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
2105251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
2106251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
2107251Slclee 			(void) fprintf(stderr,
2108251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2109251Slclee 			exit(1);
2110251Slclee 		}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
21130Sstevel@tonic-gate 		if (rsect == 0) {
21140Sstevel@tonic-gate 			(void) fprintf(stderr,
21150Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
21160Sstevel@tonic-gate 			    "   \"%s\".\n",
21170Sstevel@tonic-gate 			    file);
21180Sstevel@tonic-gate 			exit(1);
21190Sstevel@tonic-gate 		}
21200Sstevel@tonic-gate 
21210Sstevel@tonic-gate 		/*
21220Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
21230Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
21240Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
21250Sstevel@tonic-gate 		 */
21260Sstevel@tonic-gate 
21270Sstevel@tonic-gate 		if (id == EFI_PMBR) {
21280Sstevel@tonic-gate 			if (rsect != 1) {
21290Sstevel@tonic-gate 				(void) fprintf(stderr,
21300Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
21310Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
21320Sstevel@tonic-gate 				exit(1);
21330Sstevel@tonic-gate 			}
21340Sstevel@tonic-gate 
21357563SPrasad.Singamsetty@Sun.COM 
21367563SPrasad.Singamsetty@Sun.COM 			if (dev_capacity > DK_MAX_2TB) {
21377563SPrasad.Singamsetty@Sun.COM 				if (numsect != DK_MAX_2TB) {
21387563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr,
21397563SPrasad.Singamsetty@Sun.COM 					    "fdisk: EFI partitions must "
21407563SPrasad.Singamsetty@Sun.COM 					    "encompass the entire maximum 2 TB "
21417563SPrasad.Singamsetty@Sun.COM 					    "(input numsect: %u - max: %llu)\n",
21427563SPrasad.Singamsetty@Sun.COM 					    numsect, (diskaddr_t)DK_MAX_2TB);
21437563SPrasad.Singamsetty@Sun.COM 				exit(1);
21447563SPrasad.Singamsetty@Sun.COM 				}
21457563SPrasad.Singamsetty@Sun.COM 			} else if (numsect != dev_capacity - 1) {
21460Sstevel@tonic-gate 				(void) fprintf(stderr,
21470Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
2148251Slclee 				    "entire disk\n"
21497563SPrasad.Singamsetty@Sun.COM 				    "(input numsect: %u - avail: %llu)\n",
2150251Slclee 				    numsect,
21515169Slclee 				    dev_capacity - 1);
21520Sstevel@tonic-gate 				exit(1);
21530Sstevel@tonic-gate 			}
21540Sstevel@tonic-gate 		}
21550Sstevel@tonic-gate 
215610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
215710021SSheshadri.Vasudevan@Sun.COM 		if (id > FDISK_MAX_VALID_PART_ID) {
215810021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
215910021SSheshadri.Vasudevan@Sun.COM 			exit(1);
216010021SSheshadri.Vasudevan@Sun.COM 		}
216110021SSheshadri.Vasudevan@Sun.COM 
216210021SSheshadri.Vasudevan@Sun.COM 		if ((fdisk_ext_part_exists(epp)) &&
216310021SSheshadri.Vasudevan@Sun.COM 		    (fdisk_is_dos_extended(id))) {
216410021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
216510021SSheshadri.Vasudevan@Sun.COM 			    "Extended partition already exists.\n");
216610021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
216710021SSheshadri.Vasudevan@Sun.COM 			    "fdisk: Invalid entry could not be "
216810021SSheshadri.Vasudevan@Sun.COM 			    "inserted:\n        \"%s\"\n", file);
216910021SSheshadri.Vasudevan@Sun.COM 			exit(1);
217010021SSheshadri.Vasudevan@Sun.COM 		}
217110021SSheshadri.Vasudevan@Sun.COM 
217210021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_ext_part_exists(epp) &&
217310021SSheshadri.Vasudevan@Sun.COM 		    (rsect >= (ext_beg_sec = fdisk_get_ext_beg_sec(epp))) &&
217410021SSheshadri.Vasudevan@Sun.COM 		    (rsect <= (ext_end_sec = fdisk_get_ext_end_sec(epp)))) {
217510021SSheshadri.Vasudevan@Sun.COM 			int offset = MAX_LOGDRIVE_OFFSET;
217610021SSheshadri.Vasudevan@Sun.COM 
217710021SSheshadri.Vasudevan@Sun.COM 			/*
217810021SSheshadri.Vasudevan@Sun.COM 			 * Make sure that begsec doesnt wrap around.
217910021SSheshadri.Vasudevan@Sun.COM 			 * This can happen if rsect is less than offset
218010021SSheshadri.Vasudevan@Sun.COM 			 */
218110021SSheshadri.Vasudevan@Sun.COM 			if (rsect < offset) {
218210021SSheshadri.Vasudevan@Sun.COM 				return;
218310021SSheshadri.Vasudevan@Sun.COM 			}
218410021SSheshadri.Vasudevan@Sun.COM 			begsec = rsect - offset;
218510021SSheshadri.Vasudevan@Sun.COM 			if ((ldcnt = fdisk_get_logical_drive_count(epp)) == 0) {
218610021SSheshadri.Vasudevan@Sun.COM 				/*
218710021SSheshadri.Vasudevan@Sun.COM 				 * Adding the first logical drive
218810021SSheshadri.Vasudevan@Sun.COM 				 * Check if the first logical drive
218910021SSheshadri.Vasudevan@Sun.COM 				 * is out of order. In that case, do
219010021SSheshadri.Vasudevan@Sun.COM 				 * not subtract MAX_LOGDRIVE_OFFSET
219110021SSheshadri.Vasudevan@Sun.COM 				 * from the given start of partition.
219210021SSheshadri.Vasudevan@Sun.COM 				 */
219310021SSheshadri.Vasudevan@Sun.COM 				if (begsec != ext_beg_sec) {
219410021SSheshadri.Vasudevan@Sun.COM 					begsec = rsect;
219510021SSheshadri.Vasudevan@Sun.COM 					offset = 0;
219610021SSheshadri.Vasudevan@Sun.COM 				}
219710021SSheshadri.Vasudevan@Sun.COM 			}
219810021SSheshadri.Vasudevan@Sun.COM 
219910021SSheshadri.Vasudevan@Sun.COM 			if (ldcnt >= MAX_EXT_PARTS) {
220010021SSheshadri.Vasudevan@Sun.COM 				printf("\nNumber of logical drives exceeds "
220110021SSheshadri.Vasudevan@Sun.COM 				    "limit of %d.\n", MAX_EXT_PARTS);
220210021SSheshadri.Vasudevan@Sun.COM 				printf("Failing further additions.\n");
220310021SSheshadri.Vasudevan@Sun.COM 				exit(1);
220410021SSheshadri.Vasudevan@Sun.COM 			}
220510021SSheshadri.Vasudevan@Sun.COM 
220610021SSheshadri.Vasudevan@Sun.COM 			if (numsect == 0) {
220710021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
220810021SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Partition size cannot be zero:\n"
220910021SSheshadri.Vasudevan@Sun.COM 				    "   \"%s\".\n",
221010021SSheshadri.Vasudevan@Sun.COM 				    file);
221110021SSheshadri.Vasudevan@Sun.COM 				exit(1);
221210021SSheshadri.Vasudevan@Sun.COM 			}
221310021SSheshadri.Vasudevan@Sun.COM 			endsec = rsect + numsect - 1;
221410021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_validate_logical_drive(epp, begsec,
221510021SSheshadri.Vasudevan@Sun.COM 			    offset, numsect) == 0) {
221610021SSheshadri.Vasudevan@Sun.COM 				/* Valid logical drive */
221710021SSheshadri.Vasudevan@Sun.COM 				fdisk_add_logical_drive(epp, begsec, endsec,
221810021SSheshadri.Vasudevan@Sun.COM 				    id);
221910021SSheshadri.Vasudevan@Sun.COM 				return;
222010682SSheshadri.Vasudevan@Sun.COM 			} else {
222110682SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
222210682SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Invalid entry could not be "
222310682SSheshadri.Vasudevan@Sun.COM 				    "inserted:\n        \"%s\"\n", file);
222410682SSheshadri.Vasudevan@Sun.COM 				exit(1);
222510021SSheshadri.Vasudevan@Sun.COM 			}
222610021SSheshadri.Vasudevan@Sun.COM 		}
222710021SSheshadri.Vasudevan@Sun.COM #endif
222810021SSheshadri.Vasudevan@Sun.COM 
22290Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
22300Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
22310Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
22320Sstevel@tonic-gate 			(void) fprintf(stderr,
22330Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
22340Sstevel@tonic-gate 			    "	\"%s\"\n",
22350Sstevel@tonic-gate 			    file);
22360Sstevel@tonic-gate 			exit(1);
22370Sstevel@tonic-gate 		}
22380Sstevel@tonic-gate 
22390Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
22400Sstevel@tonic-gate 		if (verify_tbl() < 0) {
2241251Slclee 			(void) fprintf(stderr,
2242251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
22430Sstevel@tonic-gate 			exit(1);
22440Sstevel@tonic-gate 		}
22450Sstevel@tonic-gate 	} /* switch funct */
22460Sstevel@tonic-gate }
22470Sstevel@tonic-gate 
22480Sstevel@tonic-gate /*
22490Sstevel@tonic-gate  * Set_Table_CHS_Values
22500Sstevel@tonic-gate  *
22510Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
22520Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
22530Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
22540Sstevel@tonic-gate  *
22550Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
22560Sstevel@tonic-gate  *
22570Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
22580Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
22590Sstevel@tonic-gate  */
22600Sstevel@tonic-gate static void
22610Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
22620Sstevel@tonic-gate {
22630Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
22660Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
22670Sstevel@tonic-gate 		/*
22680Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
22690Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
22700Sstevel@tonic-gate 		 */
22710Sstevel@tonic-gate 		cy = MAX_CYL + 1;
22720Sstevel@tonic-gate 		hd = MAX_HEAD;
22730Sstevel@tonic-gate 		sc = MAX_SECT;
22740Sstevel@tonic-gate 	} else {
22750Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
22760Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
22770Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
22780Sstevel@tonic-gate 	}
22790Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
2280251Slclee 	Table[ti].beghead = (uchar_t)hd;
2281251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
22820Sstevel@tonic-gate 
22830Sstevel@tonic-gate 	/*
22840Sstevel@tonic-gate 	 * This code is identical to the code above
22850Sstevel@tonic-gate 	 * except that it works on ending CHS values
22860Sstevel@tonic-gate 	 */
22870Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
22880Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
22890Sstevel@tonic-gate 		cy = MAX_CYL + 1;
22900Sstevel@tonic-gate 		hd = MAX_HEAD;
22910Sstevel@tonic-gate 		sc = MAX_SECT;
22920Sstevel@tonic-gate 	} else {
22930Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
22940Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
22950Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
22960Sstevel@tonic-gate 	}
22970Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
2298251Slclee 	Table[ti].endhead = (uchar_t)hd;
2299251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
23000Sstevel@tonic-gate }
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate /*
23030Sstevel@tonic-gate  * insert_tbl
23040Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
23050Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
23060Sstevel@tonic-gate  *	entries!
23070Sstevel@tonic-gate  */
2308251Slclee static int
2309251Slclee insert_tbl(
2310251Slclee     int id, int act,
2311251Slclee     int bhead, int bsect, int bcyl,
2312251Slclee     int ehead, int esect, int ecyl,
23137563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect)
23140Sstevel@tonic-gate {
23150Sstevel@tonic-gate 	int	i;
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 	/* validate partition size */
23187563SPrasad.Singamsetty@Sun.COM 	if (((diskaddr_t)rsect + numsect) > dev_capacity) {
2319251Slclee 		(void) fprintf(stderr,
23200Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
23210Sstevel@tonic-gate 		return (-1);
23220Sstevel@tonic-gate 	}
23230Sstevel@tonic-gate 
23247563SPrasad.Singamsetty@Sun.COM 
23250Sstevel@tonic-gate 	/* find UNUSED partition table entry */
23260Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
23270Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
23280Sstevel@tonic-gate 			break;
23290Sstevel@tonic-gate 		}
23300Sstevel@tonic-gate 	}
23310Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
2332251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
23330Sstevel@tonic-gate 		return (-1);
23340Sstevel@tonic-gate 	}
23350Sstevel@tonic-gate 
23360Sstevel@tonic-gate 
2337251Slclee 	Table[i].systid = (uchar_t)id;
2338251Slclee 	Table[i].bootid = (uchar_t)act;
233910021SSheshadri.Vasudevan@Sun.COM 	Table[i].numsect = LE_32(numsect);
234010021SSheshadri.Vasudevan@Sun.COM 	Table[i].relsect = LE_32(rsect);
23410Sstevel@tonic-gate 
23420Sstevel@tonic-gate 	/*
23430Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
23440Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
23450Sstevel@tonic-gate 	 */
23460Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
23470Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
23480Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
23490Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
23500Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
23510Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
23520Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
23530Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
23540Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
23550Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
2356251Slclee 		Table[i].beghead = (uchar_t)bhead;
2357251Slclee 		Table[i].endhead = (uchar_t)ehead;
2358251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
23590Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
23600Sstevel@tonic-gate 	} else {
23610Sstevel@tonic-gate 
23620Sstevel@tonic-gate 		/*
23630Sstevel@tonic-gate 		 * The specified values are invalid,
23640Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
23650Sstevel@tonic-gate 		 */
23660Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
23670Sstevel@tonic-gate 	}
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	/*
23700Sstevel@tonic-gate 	 * return partition index
23710Sstevel@tonic-gate 	 */
23720Sstevel@tonic-gate 	return (i);
23730Sstevel@tonic-gate }
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate /*
23768904SBarry.Harding@Sun.COM  * entry_from_old_table
23778904SBarry.Harding@Sun.COM  *	If the specified entry is in the old table and is not a Solaris entry
23788904SBarry.Harding@Sun.COM  *	then insert same entry into new fdisk table. If we do this then
23798904SBarry.Harding@Sun.COM  *	all checks are skipped for that entry!
23808904SBarry.Harding@Sun.COM  */
23818904SBarry.Harding@Sun.COM static int
23828904SBarry.Harding@Sun.COM entry_from_old_table(
23838904SBarry.Harding@Sun.COM     int id, int act,
23848904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
23858904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
23868904SBarry.Harding@Sun.COM     uint32_t rsect, uint32_t numsect)
23878904SBarry.Harding@Sun.COM {
23888904SBarry.Harding@Sun.COM 	uint32_t	i, j;
23898904SBarry.Harding@Sun.COM 
23908904SBarry.Harding@Sun.COM 	if (id == SUNIXOS || id == SUNIXOS2)
23918904SBarry.Harding@Sun.COM 		return (0);
239210015SBarry.Harding@Sun.COM 	for (i = 0; i < FD_NUMPART; i++) {
23938904SBarry.Harding@Sun.COM 		if (Old_Table[i].systid == id &&
23948904SBarry.Harding@Sun.COM 		    Old_Table[i].bootid == act &&
23958904SBarry.Harding@Sun.COM 		    Old_Table[i].beghead == bhead &&
23968904SBarry.Harding@Sun.COM 		    Old_Table[i].begsect == ((bsect & 0x3f) |
23978904SBarry.Harding@Sun.COM 		    (uchar_t)((bcyl>>2) & 0xc0)) &&
23988904SBarry.Harding@Sun.COM 		    Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
23998904SBarry.Harding@Sun.COM 		    Old_Table[i].endhead == ehead &&
24008904SBarry.Harding@Sun.COM 		    Old_Table[i].endsect == ((esect & 0x3f) |
24018904SBarry.Harding@Sun.COM 		    (uchar_t)((ecyl>>2) & 0xc0)) &&
24028904SBarry.Harding@Sun.COM 		    Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
24038904SBarry.Harding@Sun.COM 		    Old_Table[i].relsect == lel(rsect) &&
24048904SBarry.Harding@Sun.COM 		    Old_Table[i].numsect == lel(numsect)) {
24058904SBarry.Harding@Sun.COM 			/* find UNUSED partition table entry */
24068904SBarry.Harding@Sun.COM 			for (j = 0; j < FD_NUMPART; j++) {
24078904SBarry.Harding@Sun.COM 				if (Table[j].systid == UNUSED) {
24088904SBarry.Harding@Sun.COM 					(void) memcpy(&Table[j], &Old_Table[i],
24098904SBarry.Harding@Sun.COM 					    sizeof (Table[0]));
24108904SBarry.Harding@Sun.COM 					skip_verify[j] = 1;
24118904SBarry.Harding@Sun.COM 					return (1);
24128904SBarry.Harding@Sun.COM 
24138904SBarry.Harding@Sun.COM 				}
24148904SBarry.Harding@Sun.COM 			}
24158904SBarry.Harding@Sun.COM 			return (0);
24168904SBarry.Harding@Sun.COM 		}
24178904SBarry.Harding@Sun.COM 
24188904SBarry.Harding@Sun.COM 	}
24198904SBarry.Harding@Sun.COM 	return (0);
24208904SBarry.Harding@Sun.COM }
24218904SBarry.Harding@Sun.COM 
24228904SBarry.Harding@Sun.COM /*
24230Sstevel@tonic-gate  * verify_tbl
24240Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
24250Sstevel@tonic-gate  * the disk.
24260Sstevel@tonic-gate  */
2427251Slclee static int
2428251Slclee verify_tbl(void)
24290Sstevel@tonic-gate {
24307563SPrasad.Singamsetty@Sun.COM 	uint32_t	i, j, rsect, numsect;
24310Sstevel@tonic-gate 	int	noMoreParts = 0;
24320Sstevel@tonic-gate 	int	numParts = 0;
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
2435251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
24360Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
24370Sstevel@tonic-gate 			numParts++;
24380Sstevel@tonic-gate 			/*
24390Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
24400Sstevel@tonic-gate 			 * EFI_PMBR part
24410Sstevel@tonic-gate 			 */
24420Sstevel@tonic-gate 			if (noMoreParts) {
24430Sstevel@tonic-gate 				return (-1);
24440Sstevel@tonic-gate 			}
24450Sstevel@tonic-gate 
24460Sstevel@tonic-gate 			/*
24470Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
24480Sstevel@tonic-gate 			 * and must be Table entry 0
24490Sstevel@tonic-gate 			 */
24500Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
24510Sstevel@tonic-gate 				if (i == 0) {
24520Sstevel@tonic-gate 					noMoreParts = 1;
24530Sstevel@tonic-gate 				} else {
24540Sstevel@tonic-gate 					return (-1);
24550Sstevel@tonic-gate 				}
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
2458251Slclee 					(void) fprintf(stderr, "ERROR: "
24590Sstevel@tonic-gate 					    "Invalid starting sector "
24600Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
24610Sstevel@tonic-gate 					    "relsect %d "
24620Sstevel@tonic-gate 					    "(should be 1)\n",
24630Sstevel@tonic-gate 					    Table[i].relsect);
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 					return (-1);
24660Sstevel@tonic-gate 				}
24670Sstevel@tonic-gate 
24685169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
2469251Slclee 					(void) fprintf(stderr, "ERROR: "
24700Sstevel@tonic-gate 					    "EFI_PMBR partition must "
24710Sstevel@tonic-gate 					    "encompass the entire "
2472251Slclee 					    "disk.\n numsect %d - "
24735169Slclee 					    "actual %llu\n",
24740Sstevel@tonic-gate 					    Table[i].numsect,
24755169Slclee 					    dev_capacity - 1);
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 					return (-1);
24780Sstevel@tonic-gate 				}
24790Sstevel@tonic-gate 			}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
248210021SSheshadri.Vasudevan@Sun.COM 			rsect = LE_32(Table[i].relsect);
248310021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
24847563SPrasad.Singamsetty@Sun.COM 
24857563SPrasad.Singamsetty@Sun.COM 			if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
24867563SPrasad.Singamsetty@Sun.COM 			    (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
24878904SBarry.Harding@Sun.COM 				if (!skip_verify[i])
24888904SBarry.Harding@Sun.COM 					return (-1);
24890Sstevel@tonic-gate 			}
24900Sstevel@tonic-gate 
2491251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
24920Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
24937563SPrasad.Singamsetty@Sun.COM 					uint32_t t_relsect =
249410021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].relsect);
24957563SPrasad.Singamsetty@Sun.COM 					uint32_t t_numsect =
249610021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].numsect);
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 					if (noMoreParts) {
2499251Slclee 						(void) fprintf(stderr,
25000Sstevel@tonic-gate 						    "Cannot add partition to "
25010Sstevel@tonic-gate 						    "table; no more partitions "
25020Sstevel@tonic-gate 						    "allowed\n");
25030Sstevel@tonic-gate 
25040Sstevel@tonic-gate 						if (io_debug) {
2505251Slclee 							(void) fprintf(stderr,
25060Sstevel@tonic-gate 							    "DEBUG: Current "
25070Sstevel@tonic-gate 							    "partition:\t"
25080Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2509251Slclee 							    "%d:%d:%d:%d:%d\n"
25100Sstevel@tonic-gate 							    "       Next "
25110Sstevel@tonic-gate 							    "partition:\t\t"
25120Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2513251Slclee 							    "%d:%d:%d:%d:%d\n",
25140Sstevel@tonic-gate 							    Table[i].systid,
25150Sstevel@tonic-gate 							    Table[i].bootid,
25160Sstevel@tonic-gate 							    Table[i].begcyl,
25170Sstevel@tonic-gate 							    Table[i].beghead,
25180Sstevel@tonic-gate 							    Table[i].begsect,
25190Sstevel@tonic-gate 							    Table[i].endcyl,
25200Sstevel@tonic-gate 							    Table[i].endhead,
25210Sstevel@tonic-gate 							    Table[i].endsect,
25220Sstevel@tonic-gate 							    Table[i].relsect,
25230Sstevel@tonic-gate 							    Table[i].numsect,
25240Sstevel@tonic-gate 							    Table[j].systid,
25250Sstevel@tonic-gate 							    Table[j].bootid,
25260Sstevel@tonic-gate 							    Table[j].begcyl,
25270Sstevel@tonic-gate 							    Table[j].beghead,
25280Sstevel@tonic-gate 							    Table[j].begsect,
25290Sstevel@tonic-gate 							    Table[j].endcyl,
25300Sstevel@tonic-gate 							    Table[j].endhead,
25310Sstevel@tonic-gate 							    Table[j].endsect,
25320Sstevel@tonic-gate 							    Table[j].relsect,
25330Sstevel@tonic-gate 							    Table[j].numsect);
25340Sstevel@tonic-gate 						}
25350Sstevel@tonic-gate 
25360Sstevel@tonic-gate 						return (-1);
25370Sstevel@tonic-gate 					}
25380Sstevel@tonic-gate 					if ((rsect >=
25390Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
2540251Slclee 					    ((rsect + numsect) <= t_relsect)) {
25410Sstevel@tonic-gate 						continue;
25420Sstevel@tonic-gate 					} else {
2543251Slclee 						(void) fprintf(stderr, "ERROR: "
25440Sstevel@tonic-gate 						    "current partition overlaps"
25450Sstevel@tonic-gate 						    " following partition\n");
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 						return (-1);
25480Sstevel@tonic-gate 					}
25490Sstevel@tonic-gate 				}
25500Sstevel@tonic-gate 			}
25510Sstevel@tonic-gate 		} else {
25520Sstevel@tonic-gate 			noMoreParts = 1;
25530Sstevel@tonic-gate 		}
25540Sstevel@tonic-gate 	}
25550Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
25568904SBarry.Harding@Sun.COM 		if (noMoreParts)
25578904SBarry.Harding@Sun.COM 			return (-1);
25588904SBarry.Harding@Sun.COM 		if (!skip_verify[i] &&
25598904SBarry.Harding@Sun.COM 		    ((((diskaddr_t)lel(Table[i].relsect) +
25608333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > dev_capacity) ||
25618333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
25628904SBarry.Harding@Sun.COM 		    lel(Table[i].numsect)) > DK_MAX_2TB))) {
25630Sstevel@tonic-gate 			return (-1);
25640Sstevel@tonic-gate 		}
25650Sstevel@tonic-gate 	}
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	return (numParts);
25680Sstevel@tonic-gate }
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate /*
25710Sstevel@tonic-gate  * pars_fdisk
25720Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
25730Sstevel@tonic-gate  * (-A, -D, -F).
25740Sstevel@tonic-gate  */
2575251Slclee static int
2576251Slclee pars_fdisk(
2577251Slclee     char *line,
2578251Slclee     int *id, int *act,
2579251Slclee     int *bhead, int *bsect, int *bcyl,
2580251Slclee     int *ehead, int *esect, int *ecyl,
25817563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect)
25820Sstevel@tonic-gate {
25830Sstevel@tonic-gate 	int	i;
258410021SSheshadri.Vasudevan@Sun.COM 	int64_t test;
258510021SSheshadri.Vasudevan@Sun.COM 	char *tok, *p;
258610021SSheshadri.Vasudevan@Sun.COM 	char buf[256];
258710021SSheshadri.Vasudevan@Sun.COM 
25880Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
25895169Slclee 		return (1);
25900Sstevel@tonic-gate 	line[strlen(line)] = '\0';
25910Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
25920Sstevel@tonic-gate 		if (line[i] == '\0') {
25930Sstevel@tonic-gate 			break;
25940Sstevel@tonic-gate 		} else if (line[i] == ':') {
25950Sstevel@tonic-gate 			line[i] = ' ';
25960Sstevel@tonic-gate 		}
25970Sstevel@tonic-gate 	}
259810021SSheshadri.Vasudevan@Sun.COM 	strncpy(buf, line, 256);
259910021SSheshadri.Vasudevan@Sun.COM 	errno = 0;
260010021SSheshadri.Vasudevan@Sun.COM 	tok = strtok(buf, ": \t\n");
260110021SSheshadri.Vasudevan@Sun.COM 	while (tok != NULL) {
260210021SSheshadri.Vasudevan@Sun.COM 		for (p = tok; *p != '\0'; p++) {
260310021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(*p)) {
260410021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid input %s in line %s.\n",
260510021SSheshadri.Vasudevan@Sun.COM 				    tok, line);
260610021SSheshadri.Vasudevan@Sun.COM 				exit(1);
260710021SSheshadri.Vasudevan@Sun.COM 			}
260810021SSheshadri.Vasudevan@Sun.COM 		}
260910021SSheshadri.Vasudevan@Sun.COM 
261010021SSheshadri.Vasudevan@Sun.COM 		test = strtoll(tok, (char **)NULL, 10);
261110021SSheshadri.Vasudevan@Sun.COM 		if ((test < 0) || (test > 0xFFFFFFFF) || (errno != 0)) {
261210021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid input %s in line %s.\n", tok, line);
261310021SSheshadri.Vasudevan@Sun.COM 			exit(1);
261410021SSheshadri.Vasudevan@Sun.COM 		}
261510021SSheshadri.Vasudevan@Sun.COM 		tok = strtok(NULL, ": \t\n");
261610021SSheshadri.Vasudevan@Sun.COM 	}
26177563SPrasad.Singamsetty@Sun.COM 	if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
26180Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
26190Sstevel@tonic-gate 	    rsect, numsect) != 10) {
26200Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
26210Sstevel@tonic-gate 		exit(1);
26220Sstevel@tonic-gate 	}
26230Sstevel@tonic-gate 	return (0);
26240Sstevel@tonic-gate }
26250Sstevel@tonic-gate 
26260Sstevel@tonic-gate /*
26270Sstevel@tonic-gate  * validate_part
26280Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
26290Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
26300Sstevel@tonic-gate  */
2631251Slclee static int
26327563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect)
26330Sstevel@tonic-gate {
26340Sstevel@tonic-gate 	int i;
26350Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
26360Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
26370Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
263810021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].relsect == LE_32(rsect)) &&
263910021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].numsect == LE_32(numsect)))
26405169Slclee 				return (0);
26410Sstevel@tonic-gate 		}
2642251Slclee 		(void) fprintf(stderr,
2643251Slclee 		    "New partition cannot start at sector 0\n");
26440Sstevel@tonic-gate 		return (-1);
26450Sstevel@tonic-gate 	}
264610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
264710021SSheshadri.Vasudevan@Sun.COM 	if (id > FDISK_MAX_VALID_PART_ID) {
264810021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Invalid partition ID\n");
264910021SSheshadri.Vasudevan@Sun.COM 		return (-1);
265010021SSheshadri.Vasudevan@Sun.COM 	}
265110021SSheshadri.Vasudevan@Sun.COM #endif
26520Sstevel@tonic-gate 	return (0);
26530Sstevel@tonic-gate }
26540Sstevel@tonic-gate 
26550Sstevel@tonic-gate /*
26560Sstevel@tonic-gate  * stage0
26570Sstevel@tonic-gate  * Print out interactive menu and process user input.
26580Sstevel@tonic-gate  */
2659251Slclee static void
2660251Slclee stage0(void)
26610Sstevel@tonic-gate {
266210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
266310021SSheshadri.Vasudevan@Sun.COM 	int rval;
266410021SSheshadri.Vasudevan@Sun.COM #endif
2665251Slclee 	dispmenu();
2666251Slclee 	for (;;) {
2667251Slclee 		(void) printf(Q_LINE);
2668251Slclee 		(void) printf("Enter Selection: ");
26699786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
26700Sstevel@tonic-gate 		rm_blanks(s);
267110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
267210021SSheshadri.Vasudevan@Sun.COM 		while (!((s[0] > '0') && (s[0] < '8') &&
267310021SSheshadri.Vasudevan@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
267410021SSheshadri.Vasudevan@Sun.COM #else
26759786SBarry.Harding@Sun.COM 		while (!((s[0] > '0') && (s[0] < '7') &&
26769786SBarry.Harding@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
267710021SSheshadri.Vasudevan@Sun.COM #endif
2678251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
267910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
268010021SSheshadri.Vasudevan@Sun.COM 			(void) printf(
268110021SSheshadri.Vasudevan@Sun.COM 			    "Enter a one-digit number between 1 and 7.");
268210021SSheshadri.Vasudevan@Sun.COM #else
2683251Slclee 			(void) printf(
2684251Slclee 			    "Enter a one-digit number between 1 and 6.");
268510021SSheshadri.Vasudevan@Sun.COM #endif
2686251Slclee 			(void) printf(Q_LINE);
2687251Slclee 			(void) printf("Enter Selection: ");
26889786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
26890Sstevel@tonic-gate 			rm_blanks(s);
26900Sstevel@tonic-gate 		}
2691251Slclee 		(void) printf(E_LINE);
26920Sstevel@tonic-gate 		switch (s[0]) {
26930Sstevel@tonic-gate 			case '1':
26940Sstevel@tonic-gate 				if (pcreate() == -1)
26950Sstevel@tonic-gate 					return;
26960Sstevel@tonic-gate 				break;
26970Sstevel@tonic-gate 			case '2':
26980Sstevel@tonic-gate 				if (pchange() == -1)
26990Sstevel@tonic-gate 					return;
27000Sstevel@tonic-gate 				break;
27010Sstevel@tonic-gate 			case '3':
27020Sstevel@tonic-gate 				if (pdelete() == -1)
27030Sstevel@tonic-gate 					return;
27040Sstevel@tonic-gate 				break;
27050Sstevel@tonic-gate 			case '4':
27060Sstevel@tonic-gate 				if (ppartid() == -1)
27070Sstevel@tonic-gate 					return;
27080Sstevel@tonic-gate 				break;
270910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
271010021SSheshadri.Vasudevan@Sun.COM 			case '5':
271110021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_ext_part_exists(epp)) {
271210021SSheshadri.Vasudevan@Sun.COM 					ext_part_menu();
271310021SSheshadri.Vasudevan@Sun.COM 				} else {
271410021SSheshadri.Vasudevan@Sun.COM 					printf(Q_LINE);
271510021SSheshadri.Vasudevan@Sun.COM 					printf("\nNo extended partition found"
271610021SSheshadri.Vasudevan@Sun.COM 					    "\n");
271710021SSheshadri.Vasudevan@Sun.COM 					printf("Press enter to continue\n");
271810021SSheshadri.Vasudevan@Sun.COM 					ext_read_input(s);
271910021SSheshadri.Vasudevan@Sun.COM 				}
272010021SSheshadri.Vasudevan@Sun.COM 				break;
272110021SSheshadri.Vasudevan@Sun.COM 			case '6':
272210021SSheshadri.Vasudevan@Sun.COM 				/* update disk partition table, if changed */
272310021SSheshadri.Vasudevan@Sun.COM 				if (TableChanged() == 1) {
272410021SSheshadri.Vasudevan@Sun.COM 					copy_Table_to_Bootblk();
272510021SSheshadri.Vasudevan@Sun.COM 					dev_mboot_write(0, Bootsect, sectsiz);
272610021SSheshadri.Vasudevan@Sun.COM 				}
272710021SSheshadri.Vasudevan@Sun.COM 
272810021SSheshadri.Vasudevan@Sun.COM 				/*
272910021SSheshadri.Vasudevan@Sun.COM 				 * If the VTOC table is wrong fix it
273010021SSheshadri.Vasudevan@Sun.COM 				 * (truncate only)
273110021SSheshadri.Vasudevan@Sun.COM 				 */
273210021SSheshadri.Vasudevan@Sun.COM 				if (io_adjt) {
273310021SSheshadri.Vasudevan@Sun.COM 					fix_slice();
273410021SSheshadri.Vasudevan@Sun.COM 				}
273510021SSheshadri.Vasudevan@Sun.COM 				if (!io_readonly) {
273610021SSheshadri.Vasudevan@Sun.COM 					rval = fdisk_commit_ext_part(epp);
273710021SSheshadri.Vasudevan@Sun.COM 					switch (rval) {
273810021SSheshadri.Vasudevan@Sun.COM 						case FDISK_SUCCESS:
273910021SSheshadri.Vasudevan@Sun.COM 							/* Success */
274010021SSheshadri.Vasudevan@Sun.COM 							/* Fallthrough */
274110021SSheshadri.Vasudevan@Sun.COM 						case FDISK_ENOEXTPART:
274210021SSheshadri.Vasudevan@Sun.COM 							/* Nothing to do */
274310021SSheshadri.Vasudevan@Sun.COM 							break;
274410021SSheshadri.Vasudevan@Sun.COM 						case FDISK_EMOUNTED:
274510021SSheshadri.Vasudevan@Sun.COM 							printf(Q_LINE);
274610021SSheshadri.Vasudevan@Sun.COM 							preach_and_continue();
274710021SSheshadri.Vasudevan@Sun.COM 							continue;
274810021SSheshadri.Vasudevan@Sun.COM 						default:
274910021SSheshadri.Vasudevan@Sun.COM 							perror("Commit failed");
275010021SSheshadri.Vasudevan@Sun.COM 							exit(1);
275110021SSheshadri.Vasudevan@Sun.COM 					}
275210021SSheshadri.Vasudevan@Sun.COM 					libfdisk_fini(&epp);
275310021SSheshadri.Vasudevan@Sun.COM 				}
275410021SSheshadri.Vasudevan@Sun.COM 				(void) close(Dev);
275510021SSheshadri.Vasudevan@Sun.COM 				exit(0);
275610021SSheshadri.Vasudevan@Sun.COM #else
27570Sstevel@tonic-gate 			case '5':
27580Sstevel@tonic-gate 				/* update disk partition table, if changed */
27590Sstevel@tonic-gate 				if (TableChanged() == 1) {
27600Sstevel@tonic-gate 					copy_Table_to_Bootblk();
27610Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
27620Sstevel@tonic-gate 				}
27630Sstevel@tonic-gate 				/*
27640Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27650Sstevel@tonic-gate 				 * (truncate only)
27660Sstevel@tonic-gate 				 */
27670Sstevel@tonic-gate 				if (io_adjt) {
27680Sstevel@tonic-gate 					fix_slice();
27690Sstevel@tonic-gate 				}
2770251Slclee 				(void) close(Dev);
27710Sstevel@tonic-gate 				exit(0);
2772251Slclee 				/* FALLTHRU */
277310021SSheshadri.Vasudevan@Sun.COM #endif
277410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
277510021SSheshadri.Vasudevan@Sun.COM 			case '7':
277610021SSheshadri.Vasudevan@Sun.COM #else
27770Sstevel@tonic-gate 			case '6':
277810021SSheshadri.Vasudevan@Sun.COM #endif
27790Sstevel@tonic-gate 				/*
27800Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27810Sstevel@tonic-gate 				 * (truncate only)
27820Sstevel@tonic-gate 				 */
27830Sstevel@tonic-gate 				if (io_adjt) {
27840Sstevel@tonic-gate 					fix_slice();
27850Sstevel@tonic-gate 				}
2786251Slclee 				(void) close(Dev);
27870Sstevel@tonic-gate 				exit(0);
2788251Slclee 				/* FALLTHRU */
27890Sstevel@tonic-gate 			default:
27900Sstevel@tonic-gate 				break;
27910Sstevel@tonic-gate 		}
27920Sstevel@tonic-gate 		copy_Table_to_Bootblk();
27930Sstevel@tonic-gate 		disptbl();
2794251Slclee 		dispmenu();
27950Sstevel@tonic-gate 	}
27960Sstevel@tonic-gate }
27970Sstevel@tonic-gate 
27980Sstevel@tonic-gate /*
27990Sstevel@tonic-gate  * pcreate
28000Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
28010Sstevel@tonic-gate  */
2802251Slclee static int
2803251Slclee pcreate(void)
28040Sstevel@tonic-gate {
2805251Slclee 	uchar_t tsystid = 'z';
28060Sstevel@tonic-gate 	int i, j;
28077563SPrasad.Singamsetty@Sun.COM 	uint32_t numsect;
28080Sstevel@tonic-gate 	int retCode = 0;
280910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
281010021SSheshadri.Vasudevan@Sun.COM 	int ext_part_present = 0;
281110021SSheshadri.Vasudevan@Sun.COM #endif
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	i = 0;
2814251Slclee 	for (;;) {
28150Sstevel@tonic-gate 		if (i == FD_NUMPART) {
2816251Slclee 			(void) printf(E_LINE);
2817251Slclee 			(void) printf(
2818251Slclee 			    "The partition table is full!\n"
2819251Slclee 			    "You must delete a partition before creating"
28200Sstevel@tonic-gate 			    " a new one.\n");
28210Sstevel@tonic-gate 			return (-1);
28220Sstevel@tonic-gate 		}
28230Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
28240Sstevel@tonic-gate 			break;
28250Sstevel@tonic-gate 		}
28260Sstevel@tonic-gate 		i++;
28270Sstevel@tonic-gate 	}
28280Sstevel@tonic-gate 
28297563SPrasad.Singamsetty@Sun.COM 	numsect = 0;
28300Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28310Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
283210021SSheshadri.Vasudevan@Sun.COM 			numsect += LE_32(Table[i].numsect);
28330Sstevel@tonic-gate 		}
283410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
283510021SSheshadri.Vasudevan@Sun.COM 		/* Check if an extended partition already exists */
283610021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(Table[i].systid)) {
283710021SSheshadri.Vasudevan@Sun.COM 			ext_part_present = 1;
283810021SSheshadri.Vasudevan@Sun.COM 		}
283910021SSheshadri.Vasudevan@Sun.COM #endif
28407563SPrasad.Singamsetty@Sun.COM 		if (numsect >= chs_capacity) {
2841251Slclee 			(void) printf(E_LINE);
2842251Slclee 			(void) printf("There is no more room on the disk for"
28430Sstevel@tonic-gate 			    " another partition.\n");
2844251Slclee 			(void) printf(
2845251Slclee 			    "You must delete a partition before creating"
28460Sstevel@tonic-gate 			    " a new one.\n");
28470Sstevel@tonic-gate 			return (-1);
28480Sstevel@tonic-gate 		}
28490Sstevel@tonic-gate 	}
28500Sstevel@tonic-gate 	while (tsystid == 'z') {
28517563SPrasad.Singamsetty@Sun.COM 
28527563SPrasad.Singamsetty@Sun.COM 		/*
28537563SPrasad.Singamsetty@Sun.COM 		 * The question here is expanding to more than what is
28547563SPrasad.Singamsetty@Sun.COM 		 * allocated for question lines (Q_LINE) which garbles
28557563SPrasad.Singamsetty@Sun.COM 		 * at least warning line. Clearing warning line as workaround
28567563SPrasad.Singamsetty@Sun.COM 		 * for now.
28577563SPrasad.Singamsetty@Sun.COM 		 */
28587563SPrasad.Singamsetty@Sun.COM 
28597563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
2860251Slclee 		(void) printf(Q_LINE);
2861251Slclee 		(void) printf(
2862251Slclee 		    "Select the partition type to create:\n"
2863251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
2864251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
2865251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
2866251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
28679786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
28680Sstevel@tonic-gate 		rm_blanks(s);
28699786SBarry.Harding@Sun.COM 		if ((s[1] != '\0') && (s[1] != '\n')) {
2870251Slclee 			(void) printf(E_LINE);
2871251Slclee 			(void) printf("Invalid selection, try again.");
28720Sstevel@tonic-gate 			continue;
28730Sstevel@tonic-gate 		}
28740Sstevel@tonic-gate 		switch (s[0]) {
28750Sstevel@tonic-gate 		case '0':		/* exit */
28765169Slclee 			(void) printf(E_LINE);
28775169Slclee 			return (-1);
28780Sstevel@tonic-gate 		case '1':		/* Solaris partition */
28795169Slclee 			tsystid = SUNIXOS2;
28805169Slclee 			break;
28810Sstevel@tonic-gate 		case '2':		/* UNIX partition */
28825169Slclee 			tsystid = UNIXOS;
28835169Slclee 			break;
28840Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
28855169Slclee 			tsystid = PCIXOS;
28865169Slclee 			break;
28870Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
28885169Slclee 			tsystid = OTHEROS;
28895169Slclee 			break;
28900Sstevel@tonic-gate 		case '5':
28915169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
28925169Slclee 			break;
28930Sstevel@tonic-gate 		case '6':
28945169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
28955169Slclee 			break;
28960Sstevel@tonic-gate 		case '7':
289710021SSheshadri.Vasudevan@Sun.COM #ifdef i386
289810021SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
289910021SSheshadri.Vasudevan@Sun.COM 				printf(Q_LINE);
290010021SSheshadri.Vasudevan@Sun.COM 				printf(E_LINE);
290110021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr,
290210021SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
290310021SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Press enter to continue\n");
290410021SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
290510021SSheshadri.Vasudevan@Sun.COM 				continue;
290610021SSheshadri.Vasudevan@Sun.COM 			}
290710021SSheshadri.Vasudevan@Sun.COM #endif
29085169Slclee 			tsystid = EXTDOS;
29095169Slclee 			break;
29100Sstevel@tonic-gate 		case '8':
29115169Slclee 			tsystid = DOSHUGE;
29125169Slclee 			break;
29130Sstevel@tonic-gate 		case '9':
29145169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
29155169Slclee 			break;
29160Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
29170Sstevel@tonic-gate 		case 'A':
29185169Slclee 			tsystid = X86BOOT;
29195169Slclee 			break;
29200Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
29210Sstevel@tonic-gate 		case 'B':
29225169Slclee 			tsystid = DIAGPART;
29235169Slclee 			break;
29240Sstevel@tonic-gate 		case 'c':		/* FAT32 */
29250Sstevel@tonic-gate 		case 'C':
29265169Slclee 			tsystid = FDISK_WINDOWS;
29275169Slclee 			break;
29280Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
29290Sstevel@tonic-gate 		case 'D':
29305169Slclee 			tsystid = FDISK_EXT_WIN;
29315169Slclee 			break;
29320Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
29330Sstevel@tonic-gate 		case 'E':
293410682SSheshadri.Vasudevan@Sun.COM #ifdef i386
293510682SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
293610682SSheshadri.Vasudevan@Sun.COM 				printf(Q_LINE);
293710682SSheshadri.Vasudevan@Sun.COM 				printf(E_LINE);
293810682SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr,
293910682SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
294010682SSheshadri.Vasudevan@Sun.COM 				fprintf(stderr, "Press enter to continue\n");
294110682SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
294210682SSheshadri.Vasudevan@Sun.COM 				continue;
294310682SSheshadri.Vasudevan@Sun.COM 			}
294410682SSheshadri.Vasudevan@Sun.COM #endif
29455169Slclee 			tsystid = FDISK_EXTLBA;
29465169Slclee 			break;
29470Sstevel@tonic-gate 		case 'f':
29480Sstevel@tonic-gate 		case 'F':
29495169Slclee 			tsystid = EFI_PMBR;
29505169Slclee 			break;
29510Sstevel@tonic-gate 		default:
29525169Slclee 			(void) printf(E_LINE);
29535169Slclee 			(void) printf("Invalid selection, try again.");
29545169Slclee 			continue;
29550Sstevel@tonic-gate 		}
29560Sstevel@tonic-gate 	}
29570Sstevel@tonic-gate 
2958251Slclee 	(void) printf(E_LINE);
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
29617563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
29627563SPrasad.Singamsetty@Sun.COM 		if ((dev_capacity > DK_MAX_2TB))
29637563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
29647563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
29657563SPrasad.Singamsetty@Sun.COM 
29660Sstevel@tonic-gate 		/* create the new partition */
29670Sstevel@tonic-gate 		i = specify(tsystid);
29680Sstevel@tonic-gate 
29690Sstevel@tonic-gate 		if (i != -1) {
29700Sstevel@tonic-gate 			/* see if it should be the active partition */
2971251Slclee 			(void) printf(E_LINE);
2972251Slclee 			(void) printf(Q_LINE);
2973251Slclee 
2974251Slclee 			(void) printf(
2975251Slclee 			    "Should this become the active partition? If "
2976251Slclee 			    "yes, it  will be activated\n"
2977251Slclee 			    "each time the computer is reset or turned on.\n"
2978251Slclee 			    "Please type \"y\" or \"n\". ");
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 			if (yesno()) {
2981251Slclee 				(void) printf(E_LINE);
29820Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
29830Sstevel@tonic-gate 					if (j == i) {
29840Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2985251Slclee 						(void) printf(E_LINE);
2986251Slclee 						(void) printf(
2987251Slclee 						    "Partition %d is now "
29880Sstevel@tonic-gate 						    "the active partition.",
2989251Slclee 						    j + 1);
29900Sstevel@tonic-gate 					} else {
29910Sstevel@tonic-gate 						Table[j].bootid = 0;
29920Sstevel@tonic-gate 					}
29930Sstevel@tonic-gate 				}
29940Sstevel@tonic-gate 			} else {
29950Sstevel@tonic-gate 				Table[i].bootid = 0;
29960Sstevel@tonic-gate 			}
29970Sstevel@tonic-gate 
299810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
299910021SSheshadri.Vasudevan@Sun.COM 			/*
300010021SSheshadri.Vasudevan@Sun.COM 			 * If partition created is an extended partition, null
300110021SSheshadri.Vasudevan@Sun.COM 			 * out the first sector of the first cylinder of the
300210021SSheshadri.Vasudevan@Sun.COM 			 * extended partition
300310021SSheshadri.Vasudevan@Sun.COM 			 */
300410021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended(Table[i].systid)) {
300510021SSheshadri.Vasudevan@Sun.COM 				fdisk_init_ext_part(epp,
300610021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].relsect),
300710021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].numsect));
300810021SSheshadri.Vasudevan@Sun.COM 			}
300910021SSheshadri.Vasudevan@Sun.COM #endif
30100Sstevel@tonic-gate 			/* set up the return code */
30110Sstevel@tonic-gate 			i = 1;
30120Sstevel@tonic-gate 		}
30130Sstevel@tonic-gate 	} else {
30140Sstevel@tonic-gate 		/*
30150Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
30160Sstevel@tonic-gate 		 * the table
30170Sstevel@tonic-gate 		 *
30180Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
30190Sstevel@tonic-gate 		 * empty
30200Sstevel@tonic-gate 		 */
30210Sstevel@tonic-gate 		retCode = verify_tbl();
30220Sstevel@tonic-gate 
30230Sstevel@tonic-gate 		if (retCode < 0) {
3024251Slclee 			(void) fprintf(stderr,
30250Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
30260Sstevel@tonic-gate 			    "current partition table is invalid.\n");
30270Sstevel@tonic-gate 			return (-1);
30280Sstevel@tonic-gate 		} else if (retCode > 0) {
3029251Slclee 			(void) printf(
3030251Slclee 			    "An EFI partition must be the only partition on "
3031251Slclee 			    "disk.  You may manually delete existing\n"
3032251Slclee 			    "partitions, or fdisk can do it.\n"
3033251Slclee 			    "Do you want fdisk to destroy existing "
3034251Slclee 			    "partitions?\n"
3035251Slclee 			    "Please type \"y\" or \"n\". ");
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 			if (yesno()) {
30380Sstevel@tonic-gate 				nulltbl();
30390Sstevel@tonic-gate 			} else {
30400Sstevel@tonic-gate 				return (-1);
30410Sstevel@tonic-gate 			}
30420Sstevel@tonic-gate 		}
30430Sstevel@tonic-gate 
30440Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
30457563SPrasad.Singamsetty@Sun.COM 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
30467563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
30477563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity - 1));
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 		if (i != 0) {
3050251Slclee 			(void) printf("Error creating EFI partition!!!\n");
30510Sstevel@tonic-gate 			i = -1;
30520Sstevel@tonic-gate 		} else {
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 			/* EFI partitions are currently never active */
30550Sstevel@tonic-gate 			Table[i].bootid = 0;
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate 			/* set up the return code */
30580Sstevel@tonic-gate 			i = 1;
30590Sstevel@tonic-gate 		}
30600Sstevel@tonic-gate 	}
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	return (i);
30630Sstevel@tonic-gate }
30640Sstevel@tonic-gate 
30650Sstevel@tonic-gate /*
30660Sstevel@tonic-gate  * specify
30670Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
30680Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
30690Sstevel@tonic-gate  * cylinder and length in cylinders.
30700Sstevel@tonic-gate  */
3071251Slclee static int
3072251Slclee specify(uchar_t tsystid)
30730Sstevel@tonic-gate {
30745169Slclee 	int	i, j, percent = -1;
30757563SPrasad.Singamsetty@Sun.COM 	int	cyl, cylen;
30767563SPrasad.Singamsetty@Sun.COM 	diskaddr_t first_free, size_free;
30777563SPrasad.Singamsetty@Sun.COM 	diskaddr_t max_free;
30785169Slclee 	int	cyl_size;
30790Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
30800Sstevel@tonic-gate 
30815169Slclee 	cyl_size = heads * sectors;
30825169Slclee 
30835169Slclee 	/*
30845169Slclee 	 * make a local copy of the partition table
30855169Slclee 	 * and sort it into relsect order
30865169Slclee 	 */
30875169Slclee 	for (i = 0; i < FD_NUMPART; i++)
30885169Slclee 		partition[i] = &Table[i];
30895169Slclee 
30905169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
30915169Slclee 		if (partition[i]->systid == UNUSED)
30925169Slclee 			break;
30935169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
30945169Slclee 			if (partition[j]->systid == UNUSED)
30955169Slclee 				break;
309610021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(partition[j]->relsect) <
309710021SSheshadri.Vasudevan@Sun.COM 			    LE_32(partition[i]->relsect)) {
30985169Slclee 				struct ipart *temp = partition[i];
30995169Slclee 				partition[i] = partition[j];
31005169Slclee 				partition[j] = temp;
31015169Slclee 			}
31025169Slclee 		}
31035169Slclee 	}
31045169Slclee 
31057563SPrasad.Singamsetty@Sun.COM 
3106251Slclee 	(void) printf(Q_LINE);
3107251Slclee 	(void) printf(
3108251Slclee 	    "Specify the percentage of disk to use for this partition\n"
3109251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
31109786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
31110Sstevel@tonic-gate 	rm_blanks(s);
31120Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
31135169Slclee 		i = 0;
31149786SBarry.Harding@Sun.COM 		while ((s[i] != '\0') && (s[i] != '\n')) {
31155169Slclee 			if (s[i] < '0' || s[i] > '9') {
31165169Slclee 				(void) printf(E_LINE);
31175169Slclee 				(void) printf("Invalid percentage value "
31185169Slclee 				    "specified; retry the operation.");
31195169Slclee 				return (-1);
31205169Slclee 			}
31215169Slclee 			i++;
31225169Slclee 			if (i > 3) {
31235169Slclee 				(void) printf(E_LINE);
31245169Slclee 				(void) printf("Invalid percentage value "
31255169Slclee 				    "specified; retry the operation.");
31265169Slclee 				return (-1);
31275169Slclee 			}
31285169Slclee 		}
31295169Slclee 		if ((percent = atoi(s)) > 100) {
31305169Slclee 			(void) printf(E_LINE);
31315169Slclee 			(void) printf(
31325169Slclee 			    "Percentage value is too large. The value must be"
31335169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31345169Slclee 			return (-1);
31350Sstevel@tonic-gate 		}
31365169Slclee 		if (percent < 1) {
31375169Slclee 			(void) printf(E_LINE);
31385169Slclee 			(void) printf(
31395169Slclee 			    "Percentage value is too small. The value must be"
31405169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31415169Slclee 			return (-1);
31425169Slclee 		}
31435169Slclee 
31445169Slclee 		if (percent == 100)
31457563SPrasad.Singamsetty@Sun.COM 			cylen = Numcyl_usable - 1;
31465169Slclee 		else
31477563SPrasad.Singamsetty@Sun.COM 			cylen = (Numcyl_usable * percent) / 100;
31485169Slclee 
31495169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
31505169Slclee 		if ((tsystid == DOSOS12) &&
31515169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
31525169Slclee 			int n;
31537563SPrasad.Singamsetty@Sun.COM 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
31545169Slclee 			(void) printf(E_LINE);
31555169Slclee 			(void) printf("Maximum size for a DOS partition "
31565169Slclee 			    "is %d%%; retry the operation.",
31575169Slclee 			    n <= 100 ? n : 100);
31585169Slclee 			return (-1);
31590Sstevel@tonic-gate 		}
31605169Slclee 
31615169Slclee 
31625169Slclee 		max_free = 0;
31635169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
31645169Slclee 
31655169Slclee 			/*
31665169Slclee 			 * check for free space before partition i
31675169Slclee 			 * where i varies from 0 to 3
31685169Slclee 			 *
31695169Slclee 			 * freespace after partition 3 is unusable
31705169Slclee 			 * because there are no free partitions
31715169Slclee 			 *
31725169Slclee 			 * freespace begins at the end of previous partition
31735169Slclee 			 * or cylinder 1
31745169Slclee 			 */
31755169Slclee 			if (i) {
31765169Slclee 				/* Not an empty table */
317710021SSheshadri.Vasudevan@Sun.COM 				first_free = LE_32(partition[i - 1]->relsect) +
317810021SSheshadri.Vasudevan@Sun.COM 				    LE_32(partition[i - 1]->numsect);
31795169Slclee 			} else {
31805169Slclee 				first_free = cyl_size;
31815169Slclee 			}
31825169Slclee 
31835169Slclee 			/*
31845169Slclee 			 * freespace ends before the current partition
31855169Slclee 			 * or the end of the disk (chs end)
31865169Slclee 			 */
31875169Slclee 			if (partition[i]->systid == UNUSED) {
31885169Slclee 				size_free = chs_capacity - first_free;
31895169Slclee 			} else {
31908148SShidokht.Yadegari@Sun.COM 				/*
31918148SShidokht.Yadegari@Sun.COM 				 * Partition might start before cylinder 1.
31928148SShidokht.Yadegari@Sun.COM 				 * Make sure free space is not negative.
31938148SShidokht.Yadegari@Sun.COM 				 */
31945169Slclee 				size_free =
319510021SSheshadri.Vasudevan@Sun.COM 				    (LE_32(partition[i]->relsect > first_free))
319610021SSheshadri.Vasudevan@Sun.COM 				    ? (LE_32(partition[i]->relsect) -
319710021SSheshadri.Vasudevan@Sun.COM 				    first_free) : 0;
31985169Slclee 			}
31995169Slclee 
32005169Slclee 			/* save largest free space */
32015169Slclee 			if (max_free < size_free)
32025169Slclee 				max_free = size_free;
32035169Slclee 
32047563SPrasad.Singamsetty@Sun.COM 			if (((uint64_t)cylen * cyl_size) <= size_free) {
32055169Slclee 				/* We found a place to use */
32065169Slclee 				break;
32075169Slclee 			}
32085169Slclee 			if (partition[i]->systid == UNUSED) {
32095169Slclee 				(void) printf(E_LINE);
32105169Slclee 				max_free /= (cyl_size);
32115169Slclee 				(void) fprintf(stderr, "fdisk: "
32127563SPrasad.Singamsetty@Sun.COM 				    "Maximum percentage available is %lld\n",
32137563SPrasad.Singamsetty@Sun.COM 				    100 * max_free / Numcyl_usable);
32145169Slclee 				return (-1);
32155169Slclee 			}
32160Sstevel@tonic-gate 		}
32175169Slclee 
32185169Slclee 		(void) printf(E_LINE);
32195169Slclee 		if (i >= FD_NUMPART) {
32205169Slclee 			(void) fprintf(stderr,
32215169Slclee 			    "fdisk: Partition table is full.\n");
32225169Slclee 			return (-1);
32235169Slclee 		}
32245169Slclee 
32255169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
32265169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
32275169Slclee 			return (i);
32285169Slclee 		}
32295169Slclee 		return (-1);
32305169Slclee 	} else {
32315169Slclee 
32325169Slclee 		/* Specifying size in cylinders */
3233251Slclee 		(void) printf(E_LINE);
32345169Slclee 		(void) printf(Q_LINE);
32355169Slclee 		(void) printf("Enter starting cylinder number: ");
32365169Slclee 		if ((cyl = getcyl()) == -1) {
32375169Slclee 			(void) printf(E_LINE);
32385169Slclee 			(void) printf("Invalid number; retry the operation.");
32395169Slclee 			return (-1);
32405169Slclee 		}
32415169Slclee 		if (cyl == 0) {
32425169Slclee 			(void) printf(E_LINE);
32435169Slclee 			(void) printf(
32445169Slclee 			    "New partition cannot start at cylinder 0.\n");
32455169Slclee 			return (-1);
32465169Slclee 		}
32477563SPrasad.Singamsetty@Sun.COM 
32487563SPrasad.Singamsetty@Sun.COM 
32497563SPrasad.Singamsetty@Sun.COM 		if (cyl >= Numcyl_usable) {
32505169Slclee 			(void) printf(E_LINE);
32515169Slclee 			(void) printf(
32525169Slclee 			    "Cylinder %d is out of bounds, "
32535169Slclee 			    "the maximum is %d.\n",
32547563SPrasad.Singamsetty@Sun.COM 			    cyl, Numcyl_usable - 1);
32555169Slclee 			return (-1);
32565169Slclee 		}
32577563SPrasad.Singamsetty@Sun.COM 
32585169Slclee 		(void) printf(Q_LINE);
32595169Slclee 		(void) printf("Enter partition size in cylinders: ");
32605169Slclee 		if ((cylen = getcyl()) == -1) {
32615169Slclee 			(void) printf(E_LINE);
32625169Slclee 			(void) printf("Invalid number, retry the operation.");
32635169Slclee 			return (-1);
32645169Slclee 		}
32655169Slclee 
32665169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
32675169Slclee 			uint32_t	t_relsect, t_numsect;
32685169Slclee 
32695169Slclee 			if (partition[i]->systid == UNUSED)
32705169Slclee 				break;
327110021SSheshadri.Vasudevan@Sun.COM 			t_relsect = LE_32(partition[i]->relsect);
327210021SSheshadri.Vasudevan@Sun.COM 			t_numsect = LE_32(partition[i]->numsect);
32735169Slclee 
32745169Slclee 			if (cyl * cyl_size >= t_relsect &&
32755169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
32765169Slclee 				(void) printf(E_LINE);
32775169Slclee 				(void) printf(
32785169Slclee 				    "Cylinder %d is already allocated"
32795169Slclee 				    "\nretry the operation.",
32805169Slclee 				    cyl);
32815169Slclee 				return (-1);
32825169Slclee 			}
32835169Slclee 
32845169Slclee 			if (cyl * cyl_size < t_relsect &&
32855169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
32865169Slclee 				(void) printf(E_LINE);
32875169Slclee 				(void) printf(
32885169Slclee 				    "Maximum size for partition is %u cylinders"
32895169Slclee 				    "\nretry the operation.",
32905169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
32915169Slclee 				return (-1);
32925169Slclee 			}
32935169Slclee 		}
32945169Slclee 
32957563SPrasad.Singamsetty@Sun.COM 		/* Verify partition doesn't exceed disk size or 2 TB */
32967563SPrasad.Singamsetty@Sun.COM 		if (cyl + cylen > Numcyl_usable) {
32975169Slclee 			(void) printf(E_LINE);
32987563SPrasad.Singamsetty@Sun.COM 			if (Numcyl > Numcyl_usable) {
32997563SPrasad.Singamsetty@Sun.COM 				(void) printf(
33007563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
33017563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
33027563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
33037563SPrasad.Singamsetty@Sun.COM 			} else {
33047563SPrasad.Singamsetty@Sun.COM 				(void) printf(
33057563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
33067563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
33077563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
33087563SPrasad.Singamsetty@Sun.COM 			}
33095169Slclee 			return (-1);
33105169Slclee 		}
33115169Slclee 
33125169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
33135169Slclee 		if ((tsystid == DOSOS12) &&
33145169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
33155169Slclee 			(void) printf(E_LINE);
33165169Slclee 			(void) printf(
33175169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
33185169Slclee 			    "\nretry the operation.",
33195169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
33205169Slclee 			return (-1);
33215169Slclee 		}
33225169Slclee 
3323251Slclee 		(void) printf(E_LINE);
33245169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
33255169Slclee 		    cyl * cyl_size, cylen * cyl_size);
33265169Slclee 		if (i < 0)
33275169Slclee 			return (-1);
33285169Slclee 
33295169Slclee 		if (verify_tbl() < 0) {
33305169Slclee 			(void) printf(E_LINE);
33315169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
33325169Slclee 			return (-1);
33335169Slclee 		}
33345169Slclee 
33355169Slclee 		return (i);
33360Sstevel@tonic-gate 	}
33370Sstevel@tonic-gate }
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate /*
33400Sstevel@tonic-gate  * dispmenu
33410Sstevel@tonic-gate  * Display command menu (interactive mode).
33420Sstevel@tonic-gate  */
3343251Slclee static void
3344251Slclee dispmenu(void)
33450Sstevel@tonic-gate {
3346251Slclee 	(void) printf(M_LINE);
334710021SSheshadri.Vasudevan@Sun.COM #ifdef i386
334810021SSheshadri.Vasudevan@Sun.COM 	(void) printf(
334910021SSheshadri.Vasudevan@Sun.COM 	    "SELECT ONE OF THE FOLLOWING:\n"
335010021SSheshadri.Vasudevan@Sun.COM 	    "   1. Create a partition\n"
335110021SSheshadri.Vasudevan@Sun.COM 	    "   2. Specify the active partition\n"
335210021SSheshadri.Vasudevan@Sun.COM 	    "   3. Delete a partition\n"
335310021SSheshadri.Vasudevan@Sun.COM 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
335410021SSheshadri.Vasudevan@Sun.COM 	    "   5. Edit/View extended partitions\n"
335510021SSheshadri.Vasudevan@Sun.COM 	    "   6. Exit (update disk configuration and exit)\n"
335610021SSheshadri.Vasudevan@Sun.COM 	    "   7. Cancel (exit without updating disk configuration)\n");
335710021SSheshadri.Vasudevan@Sun.COM #else
3358251Slclee 	(void) printf(
3359251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
3360251Slclee 	    "   1. Create a partition\n"
3361251Slclee 	    "   2. Specify the active partition\n"
3362251Slclee 	    "   3. Delete a partition\n"
3363251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
3364251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
3365251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
336610021SSheshadri.Vasudevan@Sun.COM #endif
33670Sstevel@tonic-gate }
33680Sstevel@tonic-gate 
33690Sstevel@tonic-gate /*
33700Sstevel@tonic-gate  * pchange
33710Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
33720Sstevel@tonic-gate  */
3373251Slclee static int
3374251Slclee pchange(void)
33750Sstevel@tonic-gate {
33760Sstevel@tonic-gate 	char s[80];
33770Sstevel@tonic-gate 	int i, j;
33780Sstevel@tonic-gate 
3379251Slclee 	for (;;) {
3380251Slclee 		(void) printf(Q_LINE);
33810Sstevel@tonic-gate 			{
3382251Slclee 			(void) printf(
3383251Slclee 			    "Specify the partition number to boot from"
33840Sstevel@tonic-gate 			    " (or specify 0 for none): ");
33850Sstevel@tonic-gate 			}
33869786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
33870Sstevel@tonic-gate 		rm_blanks(s);
33889786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
33899786SBarry.Harding@Sun.COM 		    (s[0] < '0') || (s[0] > '4')) {
3390251Slclee 			(void) printf(E_LINE);
3391251Slclee 			(void) printf(
3392251Slclee 			    "Invalid response, please specify a number"
33930Sstevel@tonic-gate 			    " between 0 and 4.\n");
33940Sstevel@tonic-gate 		} else {
33950Sstevel@tonic-gate 			break;
33960Sstevel@tonic-gate 		}
33970Sstevel@tonic-gate 	}
33980Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
33990Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
34000Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
34010Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
34020Sstevel@tonic-gate 				Table[i].bootid = 0;
34030Sstevel@tonic-gate 		}
3404251Slclee 		(void) printf(E_LINE);
3405251Slclee 			(void) printf(
3406251Slclee 			    "No partition is currently marked as active.");
34070Sstevel@tonic-gate 		return (0);
34080Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
34090Sstevel@tonic-gate 		i = s[0] - '1';
34100Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
3411251Slclee 			(void) printf(E_LINE);
3412251Slclee 			(void) printf("Partition does not exist.");
34130Sstevel@tonic-gate 			return (-1);
34140Sstevel@tonic-gate 		}
34150Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
34160Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
34170Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
34180Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
3419251Slclee 			(void) printf(E_LINE);
3420251Slclee 			(void) printf(
3421251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
34220Sstevel@tonic-gate 			    "cannot be made active.\n");
3423251Slclee 			(void) printf("Select another partition.");
34240Sstevel@tonic-gate 			return (-1);
34250Sstevel@tonic-gate 		}
34260Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
34270Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
34280Sstevel@tonic-gate 			if (j != i)
34290Sstevel@tonic-gate 			Table[j].bootid = 0;
34300Sstevel@tonic-gate 		}
34310Sstevel@tonic-gate 	}
3432251Slclee 	(void) printf(E_LINE);
34330Sstevel@tonic-gate 		{
3434251Slclee 		(void) printf(
3435251Slclee 		    "Partition %d is now active. The system will start up"
3436251Slclee 		    " from this\n", i + 1);
3437251Slclee 		(void) printf("partition after the next reboot.");
34380Sstevel@tonic-gate 		}
34390Sstevel@tonic-gate 	return (1);
34400Sstevel@tonic-gate }
34410Sstevel@tonic-gate 
34420Sstevel@tonic-gate /*
34430Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
34440Sstevel@tonic-gate  */
3445251Slclee static int
3446251Slclee ppartid(void)
34470Sstevel@tonic-gate {
34480Sstevel@tonic-gate 	char	*p, s[80];
34490Sstevel@tonic-gate 	int	i;
34500Sstevel@tonic-gate 
34510Sstevel@tonic-gate 	for (;;) {
3452251Slclee 		(void) printf(Q_LINE);
3453251Slclee 		(void) printf("Specify the partition number to change"
34545169Slclee 		    " (or enter 0 to exit): ");
3455251Slclee 		if (!fgets(s, sizeof (s), stdin))
3456251Slclee 			return (1);
34570Sstevel@tonic-gate 		i = strtol(s, &p, 10);
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
3460251Slclee 			(void) printf(E_LINE);
3461251Slclee 			(void) printf(
3462251Slclee 			    "Invalid response, retry the operation.\n");
34630Sstevel@tonic-gate 			continue;
34640Sstevel@tonic-gate 		}
34650Sstevel@tonic-gate 
34660Sstevel@tonic-gate 		if (i == 0) {
34670Sstevel@tonic-gate 			/* exit delete command */
3468251Slclee 			(void) printf(E_LINE); /* clear error message */
34690Sstevel@tonic-gate 			return (1);
34700Sstevel@tonic-gate 		}
34710Sstevel@tonic-gate 
34720Sstevel@tonic-gate 		i -= 1;
34730Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
34740Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
34750Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
34760Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
34770Sstevel@tonic-gate 		} else {
3478251Slclee 			(void) printf(E_LINE);
3479251Slclee 			(void) printf(
3480251Slclee 			    "Partition %d is not a Solaris partition.",
34810Sstevel@tonic-gate 			    i + 1);
34820Sstevel@tonic-gate 			continue;
34830Sstevel@tonic-gate 		}
34840Sstevel@tonic-gate 
3485251Slclee 		(void) printf(E_LINE);
3486251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
34870Sstevel@tonic-gate 		return (1);
34880Sstevel@tonic-gate 	}
34890Sstevel@tonic-gate }
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate /*
34920Sstevel@tonic-gate  * pdelete
34930Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
34940Sstevel@tonic-gate  */
3495251Slclee static char
3496251Slclee pdelete(void)
34970Sstevel@tonic-gate {
34980Sstevel@tonic-gate 	char s[80];
34990Sstevel@tonic-gate 	int i, j;
35000Sstevel@tonic-gate 	char pactive;
35010Sstevel@tonic-gate 
3502251Slclee DEL1:	(void) printf(Q_LINE);
3503251Slclee 	(void) printf("Specify the partition number to delete"
35040Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
35059786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
35060Sstevel@tonic-gate 	rm_blanks(s);
35070Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
3508251Slclee 		(void) printf(E_LINE);	/* clear error message */
35090Sstevel@tonic-gate 		return (1);
35100Sstevel@tonic-gate 	}
35110Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
35129786SBarry.Harding@Sun.COM 	if (((s[1] != '\0') && (s[1] != '\n')) ||
35139786SBarry.Harding@Sun.COM 	    (i = atoi(s)) < 1 || i > FD_NUMPART) {
3514251Slclee 		(void) printf(E_LINE);
3515251Slclee 		(void) printf("Invalid response, retry the operation.\n");
35160Sstevel@tonic-gate 		goto DEL1;
35170Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
35180Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
35190Sstevel@tonic-gate 	}
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
3522251Slclee 		(void) printf(E_LINE);
3523251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
35240Sstevel@tonic-gate 		return (-1);
35250Sstevel@tonic-gate 	}
35260Sstevel@tonic-gate 
352710021SSheshadri.Vasudevan@Sun.COM #ifdef i386
352810021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_is_dos_extended(Table[i].systid) &&
352910021SSheshadri.Vasudevan@Sun.COM 	    (Table[i].relsect == fdisk_get_ext_beg_sec(epp)) &&
353010021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_logical_drive_count(epp)) {
353110021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
353210021SSheshadri.Vasudevan@Sun.COM 		(void) printf("There are logical drives inside the"
353310021SSheshadri.Vasudevan@Sun.COM 		    " extended partition\n");
353410021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure of proceeding with deletion ?"
353510021SSheshadri.Vasudevan@Sun.COM 		    " (type \"y\" or \"n\") ");
353610021SSheshadri.Vasudevan@Sun.COM 
353710021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
353810021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
353910021SSheshadri.Vasudevan@Sun.COM 			return (1);
354010021SSheshadri.Vasudevan@Sun.COM 		}
354110021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_mounted_logical_drives(epp) == FDISK_EMOUNTED) {
354210021SSheshadri.Vasudevan@Sun.COM 			(void) printf(Q_LINE);
354310021SSheshadri.Vasudevan@Sun.COM 			(void) printf("There are mounted logical drives. "
354410021SSheshadri.Vasudevan@Sun.COM 			    "Committing changes now can cause data loss or "
354510021SSheshadri.Vasudevan@Sun.COM 			    "corruption. Unmount all logical drives and then "
354610021SSheshadri.Vasudevan@Sun.COM 			    "try committing the changes again.\n");
354710021SSheshadri.Vasudevan@Sun.COM 			(void) printf("Press enter to continue.\n");
354810021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(s);
354910021SSheshadri.Vasudevan@Sun.COM 			return (1);
355010021SSheshadri.Vasudevan@Sun.COM 		}
355110021SSheshadri.Vasudevan@Sun.COM 		fdisk_delete_ext_part(epp);
355210021SSheshadri.Vasudevan@Sun.COM 	} else {
355310021SSheshadri.Vasudevan@Sun.COM #endif
355410021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
355510021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure you want to delete partition %d?"
355610021SSheshadri.Vasudevan@Sun.COM 		    " This will make all files and \n", i + 1);
355710021SSheshadri.Vasudevan@Sun.COM 		(void) printf("programs in this partition inaccessible (type"
355810021SSheshadri.Vasudevan@Sun.COM 		    " \"y\" or \"n\"). ");
355910021SSheshadri.Vasudevan@Sun.COM 
356010021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
356110021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
356210021SSheshadri.Vasudevan@Sun.COM 			return (1);
356310021SSheshadri.Vasudevan@Sun.COM 		}
356410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
35650Sstevel@tonic-gate 	}
356610021SSheshadri.Vasudevan@Sun.COM #endif
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
35690Sstevel@tonic-gate 		pactive = 1;
35700Sstevel@tonic-gate 	} else {
35710Sstevel@tonic-gate 		pactive = 0;
35720Sstevel@tonic-gate 	}
35730Sstevel@tonic-gate 
35740Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
35755169Slclee 		Table[j] = Table[j + 1];
35760Sstevel@tonic-gate 	}
35770Sstevel@tonic-gate 
35780Sstevel@tonic-gate 	Table[j].systid = UNUSED;
35790Sstevel@tonic-gate 	Table[j].numsect = 0;
35800Sstevel@tonic-gate 	Table[j].relsect = 0;
35810Sstevel@tonic-gate 	Table[j].bootid = 0;
3582251Slclee 	(void) printf(E_LINE);
3583251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 	if (pactive) {
35865169Slclee 		(void) printf(" This was the active partition.");
35870Sstevel@tonic-gate 	}
35880Sstevel@tonic-gate 
35890Sstevel@tonic-gate 	return (1);
35900Sstevel@tonic-gate }
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate /*
35930Sstevel@tonic-gate  * rm_blanks
35940Sstevel@tonic-gate  * Remove blanks from strings of user responses.
35950Sstevel@tonic-gate  */
3596251Slclee static void
3597251Slclee rm_blanks(char *s)
35980Sstevel@tonic-gate {
35990Sstevel@tonic-gate 	register int i, j;
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
36020Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
36030Sstevel@tonic-gate 			continue;
36040Sstevel@tonic-gate 		else
36050Sstevel@tonic-gate 			/* Found first non-blank character of the string */
36060Sstevel@tonic-gate 			break;
36070Sstevel@tonic-gate 	}
36080Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
36090Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
36100Sstevel@tonic-gate 			/* Reached end of string */
36110Sstevel@tonic-gate 			return;
36120Sstevel@tonic-gate 		}
36130Sstevel@tonic-gate 	}
36140Sstevel@tonic-gate }
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate /*
36170Sstevel@tonic-gate  * getcyl
36180Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
36190Sstevel@tonic-gate  * string to a decimal value.
36200Sstevel@tonic-gate  */
3621251Slclee static int
3622251Slclee getcyl(void)
36230Sstevel@tonic-gate {
36240Sstevel@tonic-gate int slen, i, j;
36250Sstevel@tonic-gate unsigned int cyl;
36269786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
36270Sstevel@tonic-gate 	rm_blanks(s);
36280Sstevel@tonic-gate 	slen = strlen(s);
36299786SBarry.Harding@Sun.COM 	if (s[slen - 1] == '\n')
36309786SBarry.Harding@Sun.COM 		slen--;
36310Sstevel@tonic-gate 	j = 1;
36320Sstevel@tonic-gate 	cyl = 0;
3633251Slclee 	for (i = slen - 1; i >= 0; i--) {
36340Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
36350Sstevel@tonic-gate 			return (-1);
36360Sstevel@tonic-gate 		}
3637251Slclee 		cyl += (j * (s[i] - '0'));
36380Sstevel@tonic-gate 		j *= 10;
36390Sstevel@tonic-gate 	}
36400Sstevel@tonic-gate 	return (cyl);
36410Sstevel@tonic-gate }
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate /*
36440Sstevel@tonic-gate  * disptbl
36450Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
36460Sstevel@tonic-gate  * of the disk used for each partition.
36470Sstevel@tonic-gate  */
3648251Slclee static void
3649251Slclee disptbl(void)
36500Sstevel@tonic-gate {
36510Sstevel@tonic-gate 	int i;
36520Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
36530Sstevel@tonic-gate 	char *stat, *type;
36547563SPrasad.Singamsetty@Sun.COM 	int is_pmbr = 0;
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
3657251Slclee 		(void) printf("WARNING: critical disk geometry information"
36585169Slclee 		    " missing!\n");
3659251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
36600Sstevel@tonic-gate 		exit(1);
36610Sstevel@tonic-gate 	}
36620Sstevel@tonic-gate 
3663251Slclee 	(void) printf(HOME);
3664251Slclee 	(void) printf(T_LINE);
3665251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
36669889SLarry.Liu@Sun.COM 	(void) printf("             Cylinder size is %d (%d byte) blocks\n\n",
36679889SLarry.Liu@Sun.COM 	    heads * sectors, sectsiz);
3668251Slclee 	(void) printf(
3669251Slclee 	    "                                               Cylinders\n");
3670251Slclee 	(void) printf(
3671251Slclee 	    "      Partition   Status    Type          Start   End   Length"
36720Sstevel@tonic-gate 	    "    %%\n");
3673251Slclee 	(void) printf(
3674251Slclee 	    "      =========   ======    ============  =====   ===   ======"
36750Sstevel@tonic-gate 	    "   ===");
36760Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
36770Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
3678251Slclee 			(void) printf("\n");
3679251Slclee 			(void) printf(CLR_LIN);
36800Sstevel@tonic-gate 			continue;
36810Sstevel@tonic-gate 		}
36820Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
36835169Slclee 			stat = Actvstr;
36840Sstevel@tonic-gate 		else
36855169Slclee 			stat = NAstr;
36860Sstevel@tonic-gate 		switch (Table[i].systid) {
36870Sstevel@tonic-gate 		case UNIXOS:
36885169Slclee 			type = Ustr;
36895169Slclee 			break;
36900Sstevel@tonic-gate 		case SUNIXOS:
36915169Slclee 			type = SUstr;
369210682SSheshadri.Vasudevan@Sun.COM #ifdef i386
369310682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, Table[i].relsect,
369410682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
369510682SSheshadri.Vasudevan@Sun.COM 				type = LINSWAPstr;
369610682SSheshadri.Vasudevan@Sun.COM #endif
36975169Slclee 			break;
36980Sstevel@tonic-gate 		case SUNIXOS2:
36995169Slclee 			type = SU2str;
37005169Slclee 			break;
37010Sstevel@tonic-gate 		case X86BOOT:
37025169Slclee 			type = X86str;
37035169Slclee 			break;
37040Sstevel@tonic-gate 		case DOSOS12:
37055169Slclee 			type = Dstr;
37065169Slclee 			break;
37070Sstevel@tonic-gate 		case DOSOS16:
37085169Slclee 			type = D16str;
37095169Slclee 			break;
37100Sstevel@tonic-gate 		case EXTDOS:
37115169Slclee 			type = EDstr;
37125169Slclee 			break;
37130Sstevel@tonic-gate 		case DOSDATA:
37145169Slclee 			type = DDstr;
37155169Slclee 			break;
37160Sstevel@tonic-gate 		case DOSHUGE:
37175169Slclee 			type = DBstr;
37185169Slclee 			break;
37190Sstevel@tonic-gate 		case PCIXOS:
37205169Slclee 			type = PCstr;
37215169Slclee 			break;
37220Sstevel@tonic-gate 		case DIAGPART:
37235169Slclee 			type = DIAGstr;
37245169Slclee 			break;
37250Sstevel@tonic-gate 		case FDISK_IFS:
37265169Slclee 			type = IFSstr;
37275169Slclee 			break;
37280Sstevel@tonic-gate 		case FDISK_AIXBOOT:
37295169Slclee 			type = AIXstr;
37305169Slclee 			break;
37310Sstevel@tonic-gate 		case FDISK_AIXDATA:
37325169Slclee 			type = AIXDstr;
37335169Slclee 			break;
37340Sstevel@tonic-gate 		case FDISK_OS2BOOT:
37355169Slclee 			type = OS2str;
37365169Slclee 			break;
37370Sstevel@tonic-gate 		case FDISK_WINDOWS:
37385169Slclee 			type = WINstr;
37395169Slclee 			break;
37400Sstevel@tonic-gate 		case FDISK_EXT_WIN:
37415169Slclee 			type = EWINstr;
37425169Slclee 			break;
37430Sstevel@tonic-gate 		case FDISK_FAT95:
37445169Slclee 			type = FAT95str;
37455169Slclee 			break;
37460Sstevel@tonic-gate 		case FDISK_EXTLBA:
37475169Slclee 			type = EXTLstr;
37485169Slclee 			break;
37490Sstevel@tonic-gate 		case FDISK_LINUX:
37505169Slclee 			type = LINUXstr;
37515169Slclee 			break;
37520Sstevel@tonic-gate 		case FDISK_CPM:
37535169Slclee 			type = CPMstr;
37545169Slclee 			break;
375510021SSheshadri.Vasudevan@Sun.COM 		case FDISK_NOVELL2:
375610021SSheshadri.Vasudevan@Sun.COM 			type = NOV2str;
375710021SSheshadri.Vasudevan@Sun.COM 			break;
37580Sstevel@tonic-gate 		case FDISK_NOVELL3:
37595169Slclee 			type = NOVstr;
37605169Slclee 			break;
37610Sstevel@tonic-gate 		case FDISK_QNX4:
37625169Slclee 			type = QNXstr;
37635169Slclee 			break;
37640Sstevel@tonic-gate 		case FDISK_QNX42:
37655169Slclee 			type = QNX2str;
37665169Slclee 			break;
37670Sstevel@tonic-gate 		case FDISK_QNX43:
37685169Slclee 			type = QNX3str;
37695169Slclee 			break;
37700Sstevel@tonic-gate 		case FDISK_LINUXNAT:
37715169Slclee 			type = LINNATstr;
37725169Slclee 			break;
37730Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
37745169Slclee 			type = NTFSVOL1str;
37755169Slclee 			break;
37760Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
37775169Slclee 			type = NTFSVOL2str;
37785169Slclee 			break;
37790Sstevel@tonic-gate 		case FDISK_BSD:
37805169Slclee 			type = BSDstr;
37815169Slclee 			break;
37820Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
37835169Slclee 			type = NEXTSTEPstr;
37845169Slclee 			break;
37850Sstevel@tonic-gate 		case FDISK_BSDIFS:
37865169Slclee 			type = BSDIFSstr;
37875169Slclee 			break;
37880Sstevel@tonic-gate 		case FDISK_BSDISWAP:
37895169Slclee 			type = BSDISWAPstr;
37905169Slclee 			break;
37910Sstevel@tonic-gate 		case EFI_PMBR:
37925169Slclee 			type = EFIstr;
379310021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) == DK_MAX_2TB)
37947563SPrasad.Singamsetty@Sun.COM 				is_pmbr = 1;
37957563SPrasad.Singamsetty@Sun.COM 
37965169Slclee 			break;
37970Sstevel@tonic-gate 		default:
37985169Slclee 			type = Ostr;
37995169Slclee 			break;
38000Sstevel@tonic-gate 		}
380110021SSheshadri.Vasudevan@Sun.COM 		startcyl = LE_32(Table[i].relsect) /
38025936Sbharding 		    (unsigned long)(heads * sectors);
38037563SPrasad.Singamsetty@Sun.COM 
380410021SSheshadri.Vasudevan@Sun.COM 		if (LE_32(Table[i].numsect) == DK_MAX_2TB) {
38057563SPrasad.Singamsetty@Sun.COM 			endcyl = Numcyl - 1;
38067563SPrasad.Singamsetty@Sun.COM 			length = endcyl - startcyl + 1;
38077563SPrasad.Singamsetty@Sun.COM 		} else {
380810021SSheshadri.Vasudevan@Sun.COM 			length = LE_32(Table[i].numsect) /
38097563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors);
381010021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) %
38117563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors))
38127563SPrasad.Singamsetty@Sun.COM 				length++;
38137563SPrasad.Singamsetty@Sun.COM 			endcyl = startcyl + length - 1;
38147563SPrasad.Singamsetty@Sun.COM 		}
38157563SPrasad.Singamsetty@Sun.COM 
38167563SPrasad.Singamsetty@Sun.COM 		percent = length * 100 / Numcyl_usable;
38177563SPrasad.Singamsetty@Sun.COM 		if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
38187563SPrasad.Singamsetty@Sun.COM 			if ((remainder * 100 / Numcyl_usable) > 50) {
38190Sstevel@tonic-gate 				/* round up */
38200Sstevel@tonic-gate 				percent++;
38210Sstevel@tonic-gate 			}
38220Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
38230Sstevel@tonic-gate 			/* rounded down */
38240Sstevel@tonic-gate 		}
38250Sstevel@tonic-gate 		if (percent > 100)
38260Sstevel@tonic-gate 			percent = 100;
3827251Slclee 		(void) printf(
3828251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
3829251Slclee 		    "    %3d",
3830251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
38310Sstevel@tonic-gate 	}
38327563SPrasad.Singamsetty@Sun.COM 
38330Sstevel@tonic-gate 	/* Print warning message if table is empty */
38340Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
3835251Slclee 		(void) printf(W_LINE);
3836251Slclee 		(void) printf("WARNING: no partitions are defined!");
38370Sstevel@tonic-gate 	} else {
38380Sstevel@tonic-gate 		/* Clear the warning line */
3839251Slclee 		(void) printf(W_LINE);
38407563SPrasad.Singamsetty@Sun.COM 
38417563SPrasad.Singamsetty@Sun.COM 		/* Print warning if disk > 2TB and is not EFI PMBR */
38427563SPrasad.Singamsetty@Sun.COM 		if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
38437563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
38447563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
38450Sstevel@tonic-gate 	}
38460Sstevel@tonic-gate }
38470Sstevel@tonic-gate 
38480Sstevel@tonic-gate /*
38490Sstevel@tonic-gate  * print_Table
38500Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
38510Sstevel@tonic-gate  * the selected disk device.
38520Sstevel@tonic-gate  */
3853251Slclee static void
3854251Slclee print_Table(void)
3855251Slclee {
38560Sstevel@tonic-gate 	int i;
38570Sstevel@tonic-gate 
3858251Slclee 	(void) fprintf(stderr,
38590Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
38600Sstevel@tonic-gate 	    "   NUMSECT\n");
38610Sstevel@tonic-gate 
38620Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3863251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
3864251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
3865251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
3866251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
38675169Slclee 		(void) fprintf(stderr, "%-8d ",
38685169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
38690Sstevel@tonic-gate 
3870251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
3871251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
38725169Slclee 		(void) fprintf(stderr, "%-8d ",
38735169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
387410021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u ", LE_32(Table[i].relsect));
387510021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u\n", LE_32(Table[i].numsect));
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate 	}
38780Sstevel@tonic-gate }
38790Sstevel@tonic-gate 
38800Sstevel@tonic-gate /*
38810Sstevel@tonic-gate  * copy_Table_to_Old_Table
38820Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
38830Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
38840Sstevel@tonic-gate  * ones compared when determining if Table has changed.
38850Sstevel@tonic-gate  */
3886251Slclee static void
3887251Slclee copy_Table_to_Old_Table(void)
38880Sstevel@tonic-gate {
38890Sstevel@tonic-gate 	int i;
38900Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
38915169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
38920Sstevel@tonic-gate 	}
38930Sstevel@tonic-gate }
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate /*
38960Sstevel@tonic-gate  * nulltbl
38970Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
38980Sstevel@tonic-gate  * fdisk table.
38990Sstevel@tonic-gate  */
3900251Slclee static void
3901251Slclee nulltbl(void)
39020Sstevel@tonic-gate {
39030Sstevel@tonic-gate 	int i;
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
39065169Slclee 		Table[i].systid = UNUSED;
390710021SSheshadri.Vasudevan@Sun.COM 		Table[i].numsect = LE_32(UNUSED);
390810021SSheshadri.Vasudevan@Sun.COM 		Table[i].relsect = LE_32(UNUSED);
39095169Slclee 		Table[i].bootid = 0;
39108904SBarry.Harding@Sun.COM 		skip_verify[i] = 0;
39110Sstevel@tonic-gate 	}
39120Sstevel@tonic-gate }
39130Sstevel@tonic-gate 
39140Sstevel@tonic-gate /*
39150Sstevel@tonic-gate  * copy_Bootblk_to_Table
39160Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
39170Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
39180Sstevel@tonic-gate  */
3919251Slclee static void
3920251Slclee copy_Bootblk_to_Table(void)
39210Sstevel@tonic-gate {
39220Sstevel@tonic-gate 	int i, j;
39230Sstevel@tonic-gate 	char *bootptr;
39240Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
3927251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
39280Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
392910021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(Bootblk->signature) != MBB_MAGIC)  {
39300Sstevel@tonic-gate 		/* Signature is missing */
39310Sstevel@tonic-gate 		nulltbl();
3932251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39330Sstevel@tonic-gate 		return;
39340Sstevel@tonic-gate 	}
39350Sstevel@tonic-gate 	/*
39360Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
39370Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
39380Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
39390Sstevel@tonic-gate 	 * those that are valid.
39400Sstevel@tonic-gate 	 */
39410Sstevel@tonic-gate 	j = 0;
39420Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
39430Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
39440Sstevel@tonic-gate 			/* Null entry */
39450Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
39460Sstevel@tonic-gate 		} else {
3947251Slclee 			fill_ipart(bootptr, &Table[j]);
39480Sstevel@tonic-gate 			j++;
39490Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
39500Sstevel@tonic-gate 		}
39510Sstevel@tonic-gate 	}
39520Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
39530Sstevel@tonic-gate 		Table[i].systid = UNUSED;
395410021SSheshadri.Vasudevan@Sun.COM 		Table[i].numsect = LE_32(UNUSED);
395510021SSheshadri.Vasudevan@Sun.COM 		Table[i].relsect = LE_32(UNUSED);
39560Sstevel@tonic-gate 		Table[i].bootid = 0;
39570Sstevel@tonic-gate 
39580Sstevel@tonic-gate 	}
39590Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
3960251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39610Sstevel@tonic-gate 	copy_Table_to_Bootblk();
39620Sstevel@tonic-gate }
39630Sstevel@tonic-gate 
39640Sstevel@tonic-gate /*
39650Sstevel@tonic-gate  * fill_ipart
39660Sstevel@tonic-gate  * Initialize ipart structure values.
39670Sstevel@tonic-gate  */
3968251Slclee static void
39690Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
39700Sstevel@tonic-gate {
39710Sstevel@tonic-gate #ifdef sparc
39720Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
3973251Slclee 	partp->bootid	= getbyte(&bootptr);
3974251Slclee 	partp->beghead	= getbyte(&bootptr);
3975251Slclee 	partp->begsect	= getbyte(&bootptr);
3976251Slclee 	partp->begcyl	= getbyte(&bootptr);
3977251Slclee 	partp->systid	= getbyte(&bootptr);
3978251Slclee 	partp->endhead	= getbyte(&bootptr);
3979251Slclee 	partp->endsect	= getbyte(&bootptr);
3980251Slclee 	partp->endcyl	= getbyte(&bootptr);
3981251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
3982251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
39830Sstevel@tonic-gate #else
39840Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
39850Sstevel@tonic-gate #endif
39860Sstevel@tonic-gate }
39870Sstevel@tonic-gate 
39880Sstevel@tonic-gate /*
3989251Slclee  * getbyte, getlong
39900Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
39910Sstevel@tonic-gate  */
39920Sstevel@tonic-gate #ifdef sparc
3993251Slclee uchar_t
3994251Slclee getbyte(char **bp)
39950Sstevel@tonic-gate {
3996251Slclee 	uchar_t	b;
3997251Slclee 
3998251Slclee 	b = (uchar_t)**bp;
39990Sstevel@tonic-gate 	*bp = *bp + 1;
40000Sstevel@tonic-gate 	return (b);
40010Sstevel@tonic-gate }
40020Sstevel@tonic-gate 
4003251Slclee uint32_t
4004251Slclee getlong(char **bp)
40050Sstevel@tonic-gate {
4006251Slclee 	int32_t	b, bh, bl;
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
40090Sstevel@tonic-gate 	*bp += 2;
40100Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
40110Sstevel@tonic-gate 	*bp += 2;
40120Sstevel@tonic-gate 
40130Sstevel@tonic-gate 	b = (bh << 16) | bl;
4014251Slclee 	return ((uint32_t)b);
40150Sstevel@tonic-gate }
40160Sstevel@tonic-gate #endif
40170Sstevel@tonic-gate 
40180Sstevel@tonic-gate /*
40190Sstevel@tonic-gate  * copy_Table_to_Bootblk
40209889SLarry.Liu@Sun.COM  * Copy the table into the boot record. Note that the unused
40210Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
40220Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
40230Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
40240Sstevel@tonic-gate  */
4025251Slclee static void
4026251Slclee copy_Table_to_Bootblk(void)
40270Sstevel@tonic-gate {
40280Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
40310Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
40320Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
40330Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
40345169Slclee 		if (tbl_ptr->systid == UNUSED)
40355169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
40365169Slclee 		else
40375169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
40380Sstevel@tonic-gate 	}
403910021SSheshadri.Vasudevan@Sun.COM 	Bootblk->signature = LE_16(MBB_MAGIC);
40400Sstevel@tonic-gate }
40410Sstevel@tonic-gate 
40420Sstevel@tonic-gate /*
40430Sstevel@tonic-gate  * TableChanged
40440Sstevel@tonic-gate  * 	Check for any changes in the partition table.
40450Sstevel@tonic-gate  */
4046251Slclee static int
4047251Slclee TableChanged(void)
40480Sstevel@tonic-gate {
40490Sstevel@tonic-gate 	int i, changed;
40500Sstevel@tonic-gate 
40510Sstevel@tonic-gate 	changed = 0;
40520Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
40535169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
40545169Slclee 			/* Partition table changed, write back to disk */
40555169Slclee 			changed = 1;
40565169Slclee 		}
40570Sstevel@tonic-gate 	}
40580Sstevel@tonic-gate 
40590Sstevel@tonic-gate 	return (changed);
40600Sstevel@tonic-gate }
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate /*
40630Sstevel@tonic-gate  * ffile_write
40640Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
40650Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
40660Sstevel@tonic-gate  */
4067251Slclee static void
4068251Slclee ffile_write(char *file)
40690Sstevel@tonic-gate {
40700Sstevel@tonic-gate 	register int	i;
40710Sstevel@tonic-gate 	FILE *fp;
40720Sstevel@tonic-gate 
40730Sstevel@tonic-gate 	/*
40740Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
40750Sstevel@tonic-gate 	 * Open file and write it.
40760Sstevel@tonic-gate 	 */
40770Sstevel@tonic-gate 	if (file != (char *)stdout) {
40785169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
40795169Slclee 			(void) fprintf(stderr,
40805169Slclee 			    "fdisk: Cannot open output file %s.\n",
40815169Slclee 			    file);
40825169Slclee 			exit(1);
40835169Slclee 		}
40840Sstevel@tonic-gate 	}
40850Sstevel@tonic-gate 	else
40865169Slclee 		fp = stdout;
40870Sstevel@tonic-gate 
40880Sstevel@tonic-gate 	/*
40890Sstevel@tonic-gate 	 * Write the fdisk table information
40900Sstevel@tonic-gate 	 */
4091251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
4092251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
4093251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4094251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
4095251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
4096251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
4097251Slclee 	(void) fprintf(fp, "*\n");
40980Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
40990Sstevel@tonic-gate 	if (v_flag) {
4100251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
4101251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4102251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
4103251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
4104251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
4105251Slclee 		(void) fprintf(fp, "*\n");
41060Sstevel@tonic-gate 	}
4107251Slclee 	(void) fprintf(fp, "* systid:\n");
4108251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
4109251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
4110251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
4111251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
4112251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
4113251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
4114251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
4115251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
4116251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
4117251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
4118251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
4119251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
4120251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
4121251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
4122251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
4123251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
4124251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
4125251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
4126251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
412710021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(fp, "*  100: FDISK_NOVELL2\n");
4128251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
4129251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
4130251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
4131251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
4132251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
4133251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
4134251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
4135251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
4136251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
4137251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
4138251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
4139251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
4140251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
4141251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
4142251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
4143251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
4144251Slclee 	(void) fprintf(fp, "*\n");
4145251Slclee 	(void) fprintf(fp,
41460Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
41477563SPrasad.Singamsetty@Sun.COM 	    "    Rsect      Numsect\n");
41487563SPrasad.Singamsetty@Sun.COM 
41490Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
415010021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(fp,
415110021SSheshadri.Vasudevan@Sun.COM 		    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
415210021SSheshadri.Vasudevan@Sun.COM 		    " %-10u\n",
415310021SSheshadri.Vasudevan@Sun.COM 		    Table[i].systid,
415410021SSheshadri.Vasudevan@Sun.COM 		    Table[i].bootid,
415510021SSheshadri.Vasudevan@Sun.COM 		    Table[i].beghead,
415610021SSheshadri.Vasudevan@Sun.COM 		    Table[i].begsect & 0x3f,
415710021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
415810021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
415910021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endhead,
416010021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endsect & 0x3f,
416110021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
416210021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
416310021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].relsect),
416410021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].numsect));
416510021SSheshadri.Vasudevan@Sun.COM 	}
416610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
416710021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_ext_part_exists(epp)) {
416810021SSheshadri.Vasudevan@Sun.COM 		struct ipart ext_tab;
416910021SSheshadri.Vasudevan@Sun.COM 		logical_drive_t *temp;
417010021SSheshadri.Vasudevan@Sun.COM 		uint32_t rsect, numsect, tempsect = 0;
417110021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
417210021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
417310021SSheshadri.Vasudevan@Sun.COM 			ext_tab = temp->parts[0];
417410021SSheshadri.Vasudevan@Sun.COM 			rsect = tempsect + LE_32(ext_tab.relsect) +
417510021SSheshadri.Vasudevan@Sun.COM 			    fdisk_get_ext_beg_sec(epp);
417610021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(ext_tab.numsect);
417710021SSheshadri.Vasudevan@Sun.COM 			tempsect = LE_32(temp->parts[1].relsect);
4178251Slclee 			(void) fprintf(fp,
417910021SSheshadri.Vasudevan@Sun.COM 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d "
418010021SSheshadri.Vasudevan@Sun.COM 			    "%-7d %-8u %-8u\n",
418110021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.systid,
418210021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.bootid,
418310021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.beghead,
418410021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.begsect & 0x3f,
418510021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begcyl & 0xff) |
418610021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begsect & 0xc0) << 2)),
418710021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endhead,
418810021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endsect & 0x3f,
418910021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endcyl & 0xff) |
419010021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endsect & 0xc0) << 2)),
419110021SSheshadri.Vasudevan@Sun.COM 			    rsect,
419210021SSheshadri.Vasudevan@Sun.COM 			    numsect);
419310021SSheshadri.Vasudevan@Sun.COM 		}
41940Sstevel@tonic-gate 	}
419510021SSheshadri.Vasudevan@Sun.COM #endif
419610021SSheshadri.Vasudevan@Sun.COM 
41970Sstevel@tonic-gate 	if (fp != stdout)
4198251Slclee 		(void) fclose(fp);
41990Sstevel@tonic-gate }
42000Sstevel@tonic-gate 
42010Sstevel@tonic-gate /*
42020Sstevel@tonic-gate  * fix_slice
42030Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
42040Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
42050Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
42060Sstevel@tonic-gate  */
4207251Slclee static void
4208251Slclee fix_slice(void)
42090Sstevel@tonic-gate {
42100Sstevel@tonic-gate 	int	i;
42117563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
42120Sstevel@tonic-gate 
42130Sstevel@tonic-gate 	if (io_image) {
4214251Slclee 		return;
42150Sstevel@tonic-gate 	}
42160Sstevel@tonic-gate 
42170Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
42180Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
42190Sstevel@tonic-gate 			/*
42200Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
42210Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
42220Sstevel@tonic-gate 			 * the partition.
42230Sstevel@tonic-gate 			 */
422410021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
42250Sstevel@tonic-gate 			break;
42260Sstevel@tonic-gate 		}
42270Sstevel@tonic-gate 	}
42280Sstevel@tonic-gate 
42290Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
42300Sstevel@tonic-gate 		if (!io_nifdisk) {
42310Sstevel@tonic-gate 			(void) fprintf(stderr,
42320Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
42330Sstevel@tonic-gate 			    " checked.\n");
42340Sstevel@tonic-gate 		}
4235251Slclee 		return;
42360Sstevel@tonic-gate 	}
42370Sstevel@tonic-gate 
4238251Slclee 	if (readvtoc() != VTOC_OK) {
42390Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
42405169Slclee 	}
42415169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
42425169Slclee 		/* Special case for slice two (entire disk) */
42435169Slclee 		if (i == 2) {
42445169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
42455169Slclee 				(void) fprintf(stderr,
42467563SPrasad.Singamsetty@Sun.COM 				    "slice %d starts at %llu, is not at"
42475169Slclee 				    " start of partition",
42485169Slclee 				    i, disk_vtoc.v_part[i].p_start);
42495169Slclee 				if (!io_nifdisk) {
42505169Slclee 					(void) printf(" adjust ?:");
42515169Slclee 					if (yesno())
42520Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
42535169Slclee 				} else {
42545169Slclee 					disk_vtoc.v_part[i].p_start = 0;
42555169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42560Sstevel@tonic-gate 				}
42575169Slclee 
42585169Slclee 			}
42595169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
42605169Slclee 				(void) fprintf(stderr,
42617563SPrasad.Singamsetty@Sun.COM 				    "slice %d size %llu does not cover"
42625169Slclee 				    " complete partition",
42635169Slclee 				    i, disk_vtoc.v_part[i].p_size);
42645169Slclee 				if (!io_nifdisk) {
42655169Slclee 					(void) printf(" adjust ?:");
42665169Slclee 					if (yesno())
42670Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
42680Sstevel@tonic-gate 						    numsect;
42695169Slclee 				} else {
42705169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
42715169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42720Sstevel@tonic-gate 				}
42735169Slclee 			}
42745169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
42755169Slclee 				(void) fprintf(stderr,
42765169Slclee 				    "slice %d tag was %d should be %d",
42775169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
42785169Slclee 				    V_BACKUP);
42795169Slclee 				if (!io_nifdisk) {
4280251Slclee 					(void) printf(" fix ?:");
42815169Slclee 					if (yesno())
42820Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
42830Sstevel@tonic-gate 						    V_BACKUP;
42845169Slclee 				} else {
42850Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
42860Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
42870Sstevel@tonic-gate 				}
42885169Slclee 			}
42895169Slclee 			continue;
42905169Slclee 		}
42915169Slclee 		if (io_ADJT) {
42925169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
42935169Slclee 			    disk_vtoc.v_part[i].p_start +
42945169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
42955169Slclee 				(void) fprintf(stderr,
42967563SPrasad.Singamsetty@Sun.COM 				    "slice %d (start %llu, end %llu)"
42975169Slclee 				    " is larger than the partition",
42985169Slclee 				    i, disk_vtoc.v_part[i].p_start,
42995169Slclee 				    disk_vtoc.v_part[i].p_start +
43005169Slclee 				    disk_vtoc.v_part[i].p_size);
43015169Slclee 				if (!io_nifdisk) {
43025169Slclee 					(void) printf(" remove ?:");
43035169Slclee 					if (yesno()) {
43040Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
43050Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
43060Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
43070Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
43080Sstevel@tonic-gate 					}
43090Sstevel@tonic-gate 				} else {
43105169Slclee 					disk_vtoc.v_part[i].p_size = 0;
43115169Slclee 					disk_vtoc.v_part[i].p_start = 0;
43125169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
43135169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
43140Sstevel@tonic-gate 					(void) fprintf(stderr,
43155169Slclee 					    " removed!\n");
43165169Slclee 				}
43175169Slclee 			}
43185169Slclee 			continue;
43195169Slclee 		}
43205169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
43215169Slclee 			(void) fprintf(stderr,
43227563SPrasad.Singamsetty@Sun.COM 			    "slice %d (start %llu) is larger than the "
43237563SPrasad.Singamsetty@Sun.COM 			    "partition", i, disk_vtoc.v_part[i].p_start);
43245169Slclee 			if (!io_nifdisk) {
43255169Slclee 				(void) printf(" remove ?:");
43265169Slclee 				if (yesno()) {
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;
43310Sstevel@tonic-gate 				}
43325169Slclee 			} else {
43335169Slclee 				disk_vtoc.v_part[i].p_size = 0;
43345169Slclee 				disk_vtoc.v_part[i].p_start = 0;
43355169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
43365169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
43375169Slclee 				(void) fprintf(stderr,
43385169Slclee 				" removed!\n");
43395169Slclee 			}
43405169Slclee 		} else if (disk_vtoc.v_part[i].p_start
43415169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
43425169Slclee 			(void) fprintf(stderr,
43437563SPrasad.Singamsetty@Sun.COM 			    "slice %d (end %llu) is larger"
43445169Slclee 			    " than the partition",
43455169Slclee 			    i,
43465169Slclee 			    disk_vtoc.v_part[i].p_start +
43475169Slclee 			    disk_vtoc.v_part[i].p_size);
43485169Slclee 			if (!io_nifdisk) {
43495169Slclee 				(void) printf(" adjust ?:");
43505169Slclee 				if (yesno()) {
43515169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
43525169Slclee 				}
43535169Slclee 			} else {
43545169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
43555169Slclee 				(void) fprintf(stderr, " adjusted!\n");
43560Sstevel@tonic-gate 			}
43570Sstevel@tonic-gate 		}
43580Sstevel@tonic-gate 	}
43590Sstevel@tonic-gate #if 1		/* bh for now */
43600Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
43610Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
43620Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
43630Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
43640Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
43650Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
43660Sstevel@tonic-gate #endif
43670Sstevel@tonic-gate 
43680Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
43690Sstevel@tonic-gate 	if (!io_readonly)
4370251Slclee 		(void) writevtoc();
43710Sstevel@tonic-gate }
43720Sstevel@tonic-gate 
43730Sstevel@tonic-gate /*
43740Sstevel@tonic-gate  * yesno
43750Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
43760Sstevel@tonic-gate  */
43770Sstevel@tonic-gate 
4378251Slclee static int
4379251Slclee yesno(void)
43800Sstevel@tonic-gate {
43810Sstevel@tonic-gate 	char	s[80];
43820Sstevel@tonic-gate 
43830Sstevel@tonic-gate 	for (;;) {
43849786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
43850Sstevel@tonic-gate 		rm_blanks(s);
43869786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
43879786SBarry.Harding@Sun.COM 		    ((s[0] != 'y') && (s[0] != 'n'))) {
4388251Slclee 			(void) printf(E_LINE);
4389251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
43900Sstevel@tonic-gate 			continue;
43910Sstevel@tonic-gate 		}
43920Sstevel@tonic-gate 		if (s[0] == 'y')
43930Sstevel@tonic-gate 			return (1);
43940Sstevel@tonic-gate 		else
43950Sstevel@tonic-gate 			return (0);
43960Sstevel@tonic-gate 	}
43970Sstevel@tonic-gate }
43980Sstevel@tonic-gate 
43990Sstevel@tonic-gate /*
44000Sstevel@tonic-gate  * readvtoc
44010Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
44020Sstevel@tonic-gate  */
4403251Slclee static int
4404251Slclee readvtoc(void)
44050Sstevel@tonic-gate {
44060Sstevel@tonic-gate 	int	i;
44070Sstevel@tonic-gate 	int	retval = VTOC_OK;
44080Sstevel@tonic-gate 
44097563SPrasad.Singamsetty@Sun.COM 	if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
44100Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44110Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
44120Sstevel@tonic-gate 			vt_inval++;
44130Sstevel@tonic-gate 			retval = VTOC_INVAL;
44140Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44150Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44165169Slclee 			    "GPT\n");
44170Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44180Sstevel@tonic-gate 		} else {
44190Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
44200Sstevel@tonic-gate 			retval = VTOC_RWERR;
44210Sstevel@tonic-gate 		}
44220Sstevel@tonic-gate 	}
44230Sstevel@tonic-gate 	return (retval);
44240Sstevel@tonic-gate }
44250Sstevel@tonic-gate 
44260Sstevel@tonic-gate /*
44270Sstevel@tonic-gate  * writevtoc
44280Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
44290Sstevel@tonic-gate  */
4430251Slclee static int
4431251Slclee writevtoc(void)
44320Sstevel@tonic-gate {
44330Sstevel@tonic-gate 	int	i;
44340Sstevel@tonic-gate 	int	retval = 0;
44350Sstevel@tonic-gate 
44367563SPrasad.Singamsetty@Sun.COM 	if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
44370Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44380Sstevel@tonic-gate 			(void) fprintf(stderr,
44390Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
44400Sstevel@tonic-gate 			retval = VTOC_INVAL;
44410Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44420Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44435169Slclee 			    "GPT\n");
44440Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44450Sstevel@tonic-gate 		} else {
44460Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
44470Sstevel@tonic-gate 			retval = VTOC_RWERR;
44480Sstevel@tonic-gate 		}
44490Sstevel@tonic-gate 	}
44500Sstevel@tonic-gate 	return (retval);
44510Sstevel@tonic-gate }
44520Sstevel@tonic-gate 
44530Sstevel@tonic-gate /*
44540Sstevel@tonic-gate  * efi_ioctl
44550Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
44560Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
44570Sstevel@tonic-gate  */
44580Sstevel@tonic-gate static int
44590Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
44600Sstevel@tonic-gate {
44610Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
44620Sstevel@tonic-gate 	int error;
44630Sstevel@tonic-gate 
44640Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
44650Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate 	return (error);
44680Sstevel@tonic-gate }
44690Sstevel@tonic-gate 
44700Sstevel@tonic-gate /*
44710Sstevel@tonic-gate  * clear_efi
44720Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
44730Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
44740Sstevel@tonic-gate  */
4475251Slclee static int
4476251Slclee clear_efi(void)
44770Sstevel@tonic-gate {
44780Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
44790Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
44800Sstevel@tonic-gate 
44810Sstevel@tonic-gate 	/*
44820Sstevel@tonic-gate 	 * see if we can read the EFI label
44830Sstevel@tonic-gate 	 */
44840Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
44850Sstevel@tonic-gate 		return (VT_ERROR);
44860Sstevel@tonic-gate 	}
44870Sstevel@tonic-gate 
44880Sstevel@tonic-gate 	/*
44890Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
44900Sstevel@tonic-gate 	 */
44910Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
44920Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
44930Sstevel@tonic-gate 
44940Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
44950Sstevel@tonic-gate 		return (VT_ERROR);
44960Sstevel@tonic-gate 	}
44970Sstevel@tonic-gate 
44980Sstevel@tonic-gate 	/*
44990Sstevel@tonic-gate 	 * clear the primary label
45000Sstevel@tonic-gate 	 */
45010Sstevel@tonic-gate 	if (io_debug) {
4502251Slclee 		(void) fprintf(stderr,
4503251Slclee 		    "\tClearing primary EFI label at block %lld\n",
4504251Slclee 		    dk_ioc.dki_lba);
45050Sstevel@tonic-gate 	}
45060Sstevel@tonic-gate 
45070Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45080Sstevel@tonic-gate 		free(dk_ioc.dki_data);
45090Sstevel@tonic-gate 		switch (errno) {
45100Sstevel@tonic-gate 			case EIO:
45110Sstevel@tonic-gate 				return (VT_EIO);
45120Sstevel@tonic-gate 			case EINVAL:
45130Sstevel@tonic-gate 				return (VT_EINVAL);
45140Sstevel@tonic-gate 			default:
45150Sstevel@tonic-gate 				return (VT_ERROR);
45160Sstevel@tonic-gate 		}
45170Sstevel@tonic-gate 	}
45180Sstevel@tonic-gate 
45190Sstevel@tonic-gate 	/*
45200Sstevel@tonic-gate 	 * clear the backup partition table
45210Sstevel@tonic-gate 	 */
45220Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
45230Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
45249889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
45259889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45260Sstevel@tonic-gate 	if (io_debug) {
4527251Slclee 		(void) fprintf(stderr,
4528251Slclee 		    "\tClearing backup partition table at block %lld\n",
4529251Slclee 		    dk_ioc.dki_lba);
45300Sstevel@tonic-gate 	}
45310Sstevel@tonic-gate 
45320Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45330Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
45345169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
45355169Slclee 		    errno);
45360Sstevel@tonic-gate 	}
45370Sstevel@tonic-gate 
45380Sstevel@tonic-gate 	/*
45390Sstevel@tonic-gate 	 * clear the backup label
45400Sstevel@tonic-gate 	 */
45410Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
45420Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
45439889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
45449889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45450Sstevel@tonic-gate 	if (io_debug) {
4546251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
4547251Slclee 		    "%lld\n", dk_ioc.dki_lba);
45480Sstevel@tonic-gate 	}
45490Sstevel@tonic-gate 
45500Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
4551251Slclee 		(void) fprintf(stderr,
4552251Slclee 		    "\tUnable to clear backup EFI label at "
4553251Slclee 		    "block %llu; errno %d\n",
4554251Slclee 		    efi_vtoc->efi_last_lba,
4555251Slclee 		    errno);
45560Sstevel@tonic-gate 	}
45570Sstevel@tonic-gate 
45580Sstevel@tonic-gate 	free(dk_ioc.dki_data);
45590Sstevel@tonic-gate 	efi_free(efi_vtoc);
45600Sstevel@tonic-gate 
45610Sstevel@tonic-gate 	return (0);
45620Sstevel@tonic-gate }
45630Sstevel@tonic-gate 
45640Sstevel@tonic-gate /*
45650Sstevel@tonic-gate  * clear_vtoc
45660Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
45670Sstevel@tonic-gate  *      device.
45680Sstevel@tonic-gate  */
4569251Slclee static void
45700Sstevel@tonic-gate clear_vtoc(int table, int part)
45710Sstevel@tonic-gate {
45720Sstevel@tonic-gate 	struct ipart *clr_table;
45739889SLarry.Liu@Sun.COM 	char *disk_label;
45747563SPrasad.Singamsetty@Sun.COM 	uint32_t pcyl, ncyl, count;
45757563SPrasad.Singamsetty@Sun.COM 	diskaddr_t backup_block, solaris_offset;
45767563SPrasad.Singamsetty@Sun.COM 	ssize_t bytes;
45776549Sbharding 	off_t seek_byte;
45780Sstevel@tonic-gate 
45790Sstevel@tonic-gate #ifdef DEBUG
45809889SLarry.Liu@Sun.COM 	char *read_label;
45810Sstevel@tonic-gate #endif /* DEBUG */
45820Sstevel@tonic-gate 
45830Sstevel@tonic-gate 	if (table == OLD) {
45840Sstevel@tonic-gate 		clr_table = &Old_Table[part];
45850Sstevel@tonic-gate 	} else {
45860Sstevel@tonic-gate 		clr_table = &Table[part];
45870Sstevel@tonic-gate 	}
45880Sstevel@tonic-gate 
45899889SLarry.Liu@Sun.COM 	disk_label = (char *)calloc(sectsiz, 1);
45909889SLarry.Liu@Sun.COM 	if (disk_label == NULL) {
45919889SLarry.Liu@Sun.COM 		return;
45929889SLarry.Liu@Sun.COM 	}
45930Sstevel@tonic-gate 
459410021SSheshadri.Vasudevan@Sun.COM 	seek_byte = (off_t)(LE_32(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
45950Sstevel@tonic-gate 
45960Sstevel@tonic-gate 	if (io_debug) {
45976549Sbharding 		(void) fprintf(stderr,
45986549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
45996549Sbharding 		    (uint64_t)seek_byte,
460010021SSheshadri.Vasudevan@Sun.COM 		    (uint64_t)(LE_32(clr_table->relsect) + VTOC_OFFSET));
46010Sstevel@tonic-gate 	}
46020Sstevel@tonic-gate 
46030Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4604251Slclee 		(void) fprintf(stderr,
46056549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
46066549Sbharding 		    (uint64_t)seek_byte);
46079889SLarry.Liu@Sun.COM 		free(disk_label);
4608251Slclee 		return;
46090Sstevel@tonic-gate 	}
46100Sstevel@tonic-gate 
46119889SLarry.Liu@Sun.COM 	bytes = write(Dev, disk_label, sectsiz);
46129889SLarry.Liu@Sun.COM 
46139889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4614251Slclee 		(void) fprintf(stderr,
46157563SPrasad.Singamsetty@Sun.COM 		    "\tWarning: only %d bytes written to clear primary"
46167563SPrasad.Singamsetty@Sun.COM 		    " VTOC!\n", bytes);
46170Sstevel@tonic-gate 	}
46180Sstevel@tonic-gate 
46190Sstevel@tonic-gate #ifdef DEBUG
46200Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4621251Slclee 		(void) fprintf(stderr,
46226549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
46236549Sbharding 		    (uint64_t)seek_byte);
46249889SLarry.Liu@Sun.COM 		free(disk_label);
4625251Slclee 		return;
46260Sstevel@tonic-gate 	} else {
46276549Sbharding 		(void) fprintf(stderr,
46286549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
46296549Sbharding 		    (uint64_t)seek_byte);
46300Sstevel@tonic-gate 	}
46310Sstevel@tonic-gate 
46329889SLarry.Liu@Sun.COM 	read_label = (char *)calloc(sectsiz, 1);
46339889SLarry.Liu@Sun.COM 	if (read_label == NULL) {
46349889SLarry.Liu@Sun.COM 		free(disk_label);
46359889SLarry.Liu@Sun.COM 		return;
46369889SLarry.Liu@Sun.COM 	}
46379889SLarry.Liu@Sun.COM 
46389889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
46399889SLarry.Liu@Sun.COM 
46409889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4641251Slclee 		(void) fprintf(stderr,
4642251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
46430Sstevel@tonic-gate 		    bytes);
46440Sstevel@tonic-gate 	}
46450Sstevel@tonic-gate 
46469889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4647251Slclee 		(void) fprintf(stderr,
4648251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
46490Sstevel@tonic-gate 	} else {
4650251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
46510Sstevel@tonic-gate 		    "read_label\n");
46520Sstevel@tonic-gate 	}
46530Sstevel@tonic-gate #endif /* DEBUG */
46540Sstevel@tonic-gate 
46550Sstevel@tonic-gate 	/* Clear backup label */
465610021SSheshadri.Vasudevan@Sun.COM 	pcyl = LE_32(clr_table->numsect) / (heads * sectors);
465710021SSheshadri.Vasudevan@Sun.COM 	solaris_offset = LE_32(clr_table->relsect);
46580Sstevel@tonic-gate 	ncyl = pcyl - acyl;
46590Sstevel@tonic-gate 
46600Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
46610Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
46620Sstevel@tonic-gate 
46630Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
46649889SLarry.Liu@Sun.COM 		seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz;
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4667251Slclee 			(void) fprintf(stderr,
46686549Sbharding 			    "\tError seeking to backup label at byte %llu on "
46696549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
46709889SLarry.Liu@Sun.COM 			free(disk_label);
46719889SLarry.Liu@Sun.COM #ifdef DEBUG
46729889SLarry.Liu@Sun.COM 			free(read_label);
46739889SLarry.Liu@Sun.COM #endif /* DEBUG */
4674251Slclee 			return;
46750Sstevel@tonic-gate 		}
46760Sstevel@tonic-gate 
46770Sstevel@tonic-gate 		if (io_debug) {
4678251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
46796549Sbharding 			    " byte %llu (block %llu)\n",
46806549Sbharding 			    (uint64_t)seek_byte,
46816549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46820Sstevel@tonic-gate 		}
46830Sstevel@tonic-gate 
46849889SLarry.Liu@Sun.COM 		bytes = write(Dev, disk_label, sectsiz);
46859889SLarry.Liu@Sun.COM 
46869889SLarry.Liu@Sun.COM 		if (bytes != sectsiz) {
4687251Slclee 			(void) fprintf(stderr,
4688251Slclee 			    "\t\tWarning: only %d bytes written to "
46896549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
46906549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46910Sstevel@tonic-gate 		}
46920Sstevel@tonic-gate 
46930Sstevel@tonic-gate #ifdef DEBUG
46940Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4695251Slclee 		(void) fprintf(stderr,
46966549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
46976549Sbharding 		    (uint64_t)seek_byte);
46989889SLarry.Liu@Sun.COM 		free(disk_label);
46999889SLarry.Liu@Sun.COM 		free(read_label);
4700251Slclee 		return;
47010Sstevel@tonic-gate 	} else {
47026549Sbharding 		(void) fprintf(stderr,
47036549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
47046549Sbharding 		    (uint64_t)seek_byte);
47050Sstevel@tonic-gate 	}
47060Sstevel@tonic-gate 
47079889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
47089889SLarry.Liu@Sun.COM 
47099889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4710251Slclee 		(void) fprintf(stderr,
4711251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
4712251Slclee 		    bytes);
47130Sstevel@tonic-gate 	}
47140Sstevel@tonic-gate 
47159889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4716251Slclee 		(void) fprintf(stderr,
4717251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
47180Sstevel@tonic-gate 	} else {
4719251Slclee 		(void) fprintf(stderr,
4720251Slclee 		    "DEBUG: Good compare of disk_label and backup "
47210Sstevel@tonic-gate 		    "read_label\n");
47220Sstevel@tonic-gate 	}
47239889SLarry.Liu@Sun.COM 
47240Sstevel@tonic-gate #endif /* DEBUG */
47250Sstevel@tonic-gate 
47260Sstevel@tonic-gate 		backup_block += 2;
47270Sstevel@tonic-gate 	}
47289889SLarry.Liu@Sun.COM 
47299889SLarry.Liu@Sun.COM #ifdef DEBUG
47309889SLarry.Liu@Sun.COM 	free(read_label);
47319889SLarry.Liu@Sun.COM #endif /* DEBUG */
47329889SLarry.Liu@Sun.COM 	free(disk_label);
47330Sstevel@tonic-gate }
47340Sstevel@tonic-gate 
47350Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
47360Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
47370Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
47380Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
47390Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
47400Sstevel@tonic-gate 
47410Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
47420Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
47430Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
47440Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
47450Sstevel@tonic-gate 
47460Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
47470Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
47480Sstevel@tonic-gate 
47490Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
47500Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
47510Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47520Sstevel@tonic-gate 
47530Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
47540Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
47550Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47560Sstevel@tonic-gate 
47570Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
47580Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate /*
47610Sstevel@tonic-gate  *  lecture_and_query
47620Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
47630Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
47640Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
47650Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
47660Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
47670Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
47680Sstevel@tonic-gate  */
4769251Slclee static int
47700Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
47710Sstevel@tonic-gate {
47720Sstevel@tonic-gate 	if (io_nifdisk)
47730Sstevel@tonic-gate 		return (0);
47740Sstevel@tonic-gate 
4775251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
4776251Slclee 	(void) fprintf(stderr, "%s", warning);
4777251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
4778251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
47790Sstevel@tonic-gate 
47800Sstevel@tonic-gate 	return (yesno());
47810Sstevel@tonic-gate }
47820Sstevel@tonic-gate 
4783251Slclee static void
47840Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
47850Sstevel@tonic-gate {
47867563SPrasad.Singamsetty@Sun.COM 	struct extvtoc v;
47870Sstevel@tonic-gate 	struct dk_geom d;
47880Sstevel@tonic-gate 	struct part_info pi;
47897563SPrasad.Singamsetty@Sun.COM 	struct extpart_info extpi;
47907563SPrasad.Singamsetty@Sun.COM 	diskaddr_t totsize;
47910Sstevel@tonic-gate 	int idx = -1;
47920Sstevel@tonic-gate 
47930Sstevel@tonic-gate 	/*
47940Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
47950Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
47960Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
47970Sstevel@tonic-gate 	 */
47987563SPrasad.Singamsetty@Sun.COM 	if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
47997563SPrasad.Singamsetty@Sun.COM 		if (extpi.p_start != 0) {
48007563SPrasad.Singamsetty@Sun.COM 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
48017563SPrasad.Singamsetty@Sun.COM 			    devname)) {
48027563SPrasad.Singamsetty@Sun.COM 				(void) close(fd);
48037563SPrasad.Singamsetty@Sun.COM 				exit(1);
48047563SPrasad.Singamsetty@Sun.COM 			}
48057563SPrasad.Singamsetty@Sun.COM 		}
48067563SPrasad.Singamsetty@Sun.COM 	} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
48070Sstevel@tonic-gate 		if (pi.p_start != 0) {
48080Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
48090Sstevel@tonic-gate 			    devname)) {
48100Sstevel@tonic-gate 				(void) close(fd);
48110Sstevel@tonic-gate 				exit(1);
48120Sstevel@tonic-gate 			}
48130Sstevel@tonic-gate 		}
48140Sstevel@tonic-gate 	} else {
48157563SPrasad.Singamsetty@Sun.COM 		if ((idx = read_extvtoc(fd, &v)) < 0) {
48160Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
48170Sstevel@tonic-gate 			    devname)) {
48180Sstevel@tonic-gate 				(void) close(fd);
48190Sstevel@tonic-gate 				exit(1);
48200Sstevel@tonic-gate 			}
48210Sstevel@tonic-gate 			return;
48220Sstevel@tonic-gate 		}
48230Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
48240Sstevel@tonic-gate 			perror(devname);
48250Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
48260Sstevel@tonic-gate 			    devname)) {
48270Sstevel@tonic-gate 				(void) close(fd);
48280Sstevel@tonic-gate 				exit(1);
48290Sstevel@tonic-gate 			}
48300Sstevel@tonic-gate 			return;
48310Sstevel@tonic-gate 		}
48327563SPrasad.Singamsetty@Sun.COM 		totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
48330Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
48340Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
48350Sstevel@tonic-gate 			    devname)) {
48360Sstevel@tonic-gate 				(void) close(fd);
48370Sstevel@tonic-gate 				exit(1);
48380Sstevel@tonic-gate 			}
48390Sstevel@tonic-gate 		}
48400Sstevel@tonic-gate 	}
48410Sstevel@tonic-gate }
48420Sstevel@tonic-gate 
48430Sstevel@tonic-gate 
48440Sstevel@tonic-gate /*
48450Sstevel@tonic-gate  * get_node
48460Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
48470Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
48480Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
48490Sstevel@tonic-gate  */
48500Sstevel@tonic-gate static char *
48510Sstevel@tonic-gate get_node(char *devname)
48520Sstevel@tonic-gate {
48530Sstevel@tonic-gate 	char *node;
48540Sstevel@tonic-gate 	struct stat statbuf;
48550Sstevel@tonic-gate 	size_t space;
48560Sstevel@tonic-gate 
48570Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
48580Sstevel@tonic-gate 	if (io_image)
48590Sstevel@tonic-gate 		return (devname);
48600Sstevel@tonic-gate 
48610Sstevel@tonic-gate 	node = devname;
48620Sstevel@tonic-gate 
48630Sstevel@tonic-gate 	/* Try the node as provided first */
48640Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
48650Sstevel@tonic-gate 		/*
48660Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
48670Sstevel@tonic-gate 		 * default path and try again.
48680Sstevel@tonic-gate 		 */
48690Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
4872251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
48730Sstevel@tonic-gate 			    "for device node.\n");
48740Sstevel@tonic-gate 			exit(1);
48750Sstevel@tonic-gate 		}
48760Sstevel@tonic-gate 
48770Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
48780Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
48790Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
48800Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
48810Sstevel@tonic-gate 
48820Sstevel@tonic-gate 		/* Try to stat it again */
48830Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
48840Sstevel@tonic-gate 			/* Failed all options, give up */
4885251Slclee 			(void) fprintf(stderr,
4886251Slclee 			    "fdisk: Cannot stat device %s.\n",
48870Sstevel@tonic-gate 			    devname);
48880Sstevel@tonic-gate 			exit(1);
48890Sstevel@tonic-gate 		}
48900Sstevel@tonic-gate 	}
48910Sstevel@tonic-gate 
48920Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
48930Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
4894251Slclee 		(void) fprintf(stderr,
4895251Slclee 		    "fdisk: %s must be a raw device.\n", node);
48960Sstevel@tonic-gate 		exit(1);
48970Sstevel@tonic-gate 	}
48980Sstevel@tonic-gate 
48990Sstevel@tonic-gate 	return (node);
49000Sstevel@tonic-gate }
490110021SSheshadri.Vasudevan@Sun.COM 
490210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
490310021SSheshadri.Vasudevan@Sun.COM static void
490410021SSheshadri.Vasudevan@Sun.COM preach_and_continue()
490510021SSheshadri.Vasudevan@Sun.COM {
490610021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(stderr, "There are mounted logical drives. Committing "
490710021SSheshadri.Vasudevan@Sun.COM 	    "changes now can lead to inconsistancy in internal system state "
490810021SSheshadri.Vasudevan@Sun.COM 	    "which can eventually cause data loss or corruption. Unmount all "
490910021SSheshadri.Vasudevan@Sun.COM 	    "logical drives and try committing the changes again.\n");
491010021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(s);
491110021SSheshadri.Vasudevan@Sun.COM }
491210021SSheshadri.Vasudevan@Sun.COM 
491310021SSheshadri.Vasudevan@Sun.COM /*
491410021SSheshadri.Vasudevan@Sun.COM  * Convert a given partition ID to an descriptive string.
491510021SSheshadri.Vasudevan@Sun.COM  * Just an index into the partition types table.
491610021SSheshadri.Vasudevan@Sun.COM  */
491710021SSheshadri.Vasudevan@Sun.COM void
491810021SSheshadri.Vasudevan@Sun.COM id_to_name(uchar_t sysid, char *buffer)
491910021SSheshadri.Vasudevan@Sun.COM {
492010021SSheshadri.Vasudevan@Sun.COM 	strcpy(buffer, fdisk_part_types[sysid]);
492110021SSheshadri.Vasudevan@Sun.COM }
492210021SSheshadri.Vasudevan@Sun.COM 
492310021SSheshadri.Vasudevan@Sun.COM /*
492410021SSheshadri.Vasudevan@Sun.COM  * Procedure to check the validity of the extended partition menu option
492510021SSheshadri.Vasudevan@Sun.COM  * entered by the user
492610021SSheshadri.Vasudevan@Sun.COM  */
492710021SSheshadri.Vasudevan@Sun.COM static int
492810021SSheshadri.Vasudevan@Sun.COM ext_invalid_option(char ch)
492910021SSheshadri.Vasudevan@Sun.COM {
493010021SSheshadri.Vasudevan@Sun.COM 	char *p;
493110021SSheshadri.Vasudevan@Sun.COM 
493210021SSheshadri.Vasudevan@Sun.COM 	p = strchr(ext_part_menu_opts, tolower(ch));
493310021SSheshadri.Vasudevan@Sun.COM 
493410021SSheshadri.Vasudevan@Sun.COM 	if (p == NULL) {
493510021SSheshadri.Vasudevan@Sun.COM 		return (1);
493610021SSheshadri.Vasudevan@Sun.COM 	}
493710021SSheshadri.Vasudevan@Sun.COM 	return (0);
493810021SSheshadri.Vasudevan@Sun.COM }
493910021SSheshadri.Vasudevan@Sun.COM 
494010021SSheshadri.Vasudevan@Sun.COM /*
494110021SSheshadri.Vasudevan@Sun.COM  * Read 16 bytes of the input (assuming that no valid user input spans more
494210021SSheshadri.Vasudevan@Sun.COM  * than that). Flush the input stream, so that the next read does not reap
494310021SSheshadri.Vasudevan@Sun.COM  * stale data from the previous input that was not processed.
494410021SSheshadri.Vasudevan@Sun.COM  * Note that fgets also reads the trailing '\n'
494510021SSheshadri.Vasudevan@Sun.COM  */
494610021SSheshadri.Vasudevan@Sun.COM static void
494710021SSheshadri.Vasudevan@Sun.COM ext_read_input(char *buf)
494810021SSheshadri.Vasudevan@Sun.COM {
494910021SSheshadri.Vasudevan@Sun.COM 	fgets(buf, 16, stdin);
495010021SSheshadri.Vasudevan@Sun.COM 	fflush(stdin);
495110021SSheshadri.Vasudevan@Sun.COM }
495210021SSheshadri.Vasudevan@Sun.COM 
495310021SSheshadri.Vasudevan@Sun.COM /*
495410021SSheshadri.Vasudevan@Sun.COM  * Procedure to read and validate the user option at the extended partition menu
495510021SSheshadri.Vasudevan@Sun.COM  */
495610021SSheshadri.Vasudevan@Sun.COM static int
495710021SSheshadri.Vasudevan@Sun.COM ext_read_options(char *buf)
495810021SSheshadri.Vasudevan@Sun.COM {
495910021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(buf);
496010021SSheshadri.Vasudevan@Sun.COM 	if ((strlen(buf) != 2) || (ext_invalid_option(buf[0]))) {
496110021SSheshadri.Vasudevan@Sun.COM 		printf("\nUnknown Command\n");
496210021SSheshadri.Vasudevan@Sun.COM 		return (-1);
496310021SSheshadri.Vasudevan@Sun.COM 	}
496410021SSheshadri.Vasudevan@Sun.COM 	return (0);
496510021SSheshadri.Vasudevan@Sun.COM }
496610021SSheshadri.Vasudevan@Sun.COM 
496710021SSheshadri.Vasudevan@Sun.COM /*
496810021SSheshadri.Vasudevan@Sun.COM  * Procedure to print the list of known partition types and their IDs
496910021SSheshadri.Vasudevan@Sun.COM  */
497010021SSheshadri.Vasudevan@Sun.COM static void
497110021SSheshadri.Vasudevan@Sun.COM ext_print_part_types()
497210021SSheshadri.Vasudevan@Sun.COM {
497310021SSheshadri.Vasudevan@Sun.COM 	int i, rowmax, rowcount = 1;
497410021SSheshadri.Vasudevan@Sun.COM 	struct winsize ws;
497510021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
497610021SSheshadri.Vasudevan@Sun.COM 
497710021SSheshadri.Vasudevan@Sun.COM 	/* Get the current window dimensions */
497810021SSheshadri.Vasudevan@Sun.COM 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) {
497910021SSheshadri.Vasudevan@Sun.COM 		perror("ioctl");
498010021SSheshadri.Vasudevan@Sun.COM 		rowmax = 20;
498110021SSheshadri.Vasudevan@Sun.COM 	} else {
498210021SSheshadri.Vasudevan@Sun.COM 		/*
498310021SSheshadri.Vasudevan@Sun.COM 		 * Accommodate the initial headings by reducing the number of
498410021SSheshadri.Vasudevan@Sun.COM 		 * partition IDs being printed.
498510021SSheshadri.Vasudevan@Sun.COM 		 */
498610021SSheshadri.Vasudevan@Sun.COM 		rowmax = ws.ws_row - 5;
498710021SSheshadri.Vasudevan@Sun.COM 	}
498810021SSheshadri.Vasudevan@Sun.COM 
498910021SSheshadri.Vasudevan@Sun.COM 	if (rowmax < 3) {
499010021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Window size too small."
499110021SSheshadri.Vasudevan@Sun.COM 		    " Try resizing the window\n");
499210021SSheshadri.Vasudevan@Sun.COM 		return;
499310021SSheshadri.Vasudevan@Sun.COM 	}
499410021SSheshadri.Vasudevan@Sun.COM 
499510021SSheshadri.Vasudevan@Sun.COM 	printf("List of known partition types : \n");
499610021SSheshadri.Vasudevan@Sun.COM 	printf("PartID          Partition Type\n");
499710021SSheshadri.Vasudevan@Sun.COM 	printf("======          ==============\n");
499810021SSheshadri.Vasudevan@Sun.COM 	for (i = 0; i <= FDISK_MAX_VALID_PART_ID; i++) {
499910021SSheshadri.Vasudevan@Sun.COM 		printf("%-3d          %s\n", i, fdisk_part_types[i]);
500010021SSheshadri.Vasudevan@Sun.COM 		rowcount++;
500110021SSheshadri.Vasudevan@Sun.COM 		if (rowcount == rowmax) {
500210021SSheshadri.Vasudevan@Sun.COM 			/*
500310021SSheshadri.Vasudevan@Sun.COM 			 * After the initial screen, use all the rows for
500410021SSheshadri.Vasudevan@Sun.COM 			 * printing the partition IDs, but one.
500510021SSheshadri.Vasudevan@Sun.COM 			 */
500610021SSheshadri.Vasudevan@Sun.COM 			rowmax = ws.ws_row - 1;
500710021SSheshadri.Vasudevan@Sun.COM 			fprintf(stderr, "\nPress enter to see next page or 'q'"
500810021SSheshadri.Vasudevan@Sun.COM 			    " to quit : ");
500910021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
501010021SSheshadri.Vasudevan@Sun.COM 			if ((strlen(buf) == 2) && (tolower(buf[0]) == 'q')) {
501110021SSheshadri.Vasudevan@Sun.COM 				return;
501210021SSheshadri.Vasudevan@Sun.COM 			}
501310021SSheshadri.Vasudevan@Sun.COM 			rowcount = 1;
501410021SSheshadri.Vasudevan@Sun.COM 		}
501510021SSheshadri.Vasudevan@Sun.COM 	}
501610021SSheshadri.Vasudevan@Sun.COM }
501710021SSheshadri.Vasudevan@Sun.COM 
501810021SSheshadri.Vasudevan@Sun.COM static void
501910021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(int *pno)
502010021SSheshadri.Vasudevan@Sun.COM {
502110021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
502210021SSheshadri.Vasudevan@Sun.COM 	int len, i;
502310021SSheshadri.Vasudevan@Sun.COM 
502410021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
502510021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the partition number : ");
502610021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
502710021SSheshadri.Vasudevan@Sun.COM 
502810021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
502910021SSheshadri.Vasudevan@Sun.COM 
503010021SSheshadri.Vasudevan@Sun.COM 		/* Check length of the input */
503110021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_NUM_DIGITS+1))) {
503210021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
503310021SSheshadri.Vasudevan@Sun.COM 		}
503410021SSheshadri.Vasudevan@Sun.COM 
503510021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
503610021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
503710021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
503810021SSheshadri.Vasudevan@Sun.COM 				goto print_error_and_continue;
503910021SSheshadri.Vasudevan@Sun.COM 			}
504010021SSheshadri.Vasudevan@Sun.COM 		}
504110021SSheshadri.Vasudevan@Sun.COM 
504210021SSheshadri.Vasudevan@Sun.COM 		*pno = atoi(buf);
504310021SSheshadri.Vasudevan@Sun.COM 
504410021SSheshadri.Vasudevan@Sun.COM 		if ((*pno <= FD_NUMPART) ||
504510021SSheshadri.Vasudevan@Sun.COM 		    *pno > (fdisk_get_logical_drive_count(epp) + FD_NUMPART)) {
504610021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
504710021SSheshadri.Vasudevan@Sun.COM 		}
504810021SSheshadri.Vasudevan@Sun.COM 
504910021SSheshadri.Vasudevan@Sun.COM 		break;
505010021SSheshadri.Vasudevan@Sun.COM print_error_and_continue:
505110021SSheshadri.Vasudevan@Sun.COM 		printf("Invalid partition number\n");
505210021SSheshadri.Vasudevan@Sun.COM 		continue;
505310021SSheshadri.Vasudevan@Sun.COM 	}
505410021SSheshadri.Vasudevan@Sun.COM }
505510021SSheshadri.Vasudevan@Sun.COM 
505610021SSheshadri.Vasudevan@Sun.COM static void
505710021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(uchar_t *partid)
505810021SSheshadri.Vasudevan@Sun.COM {
505910021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
506010021SSheshadri.Vasudevan@Sun.COM 	int len, i, id;
506110021SSheshadri.Vasudevan@Sun.COM 
506210021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
506310021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the ID ( Type I for list of partition IDs ) : ");
506410021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
506510021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
506610021SSheshadri.Vasudevan@Sun.COM 
506710021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_ID_DIGITS + 1))) {
506810021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
506910021SSheshadri.Vasudevan@Sun.COM 			continue;
507010021SSheshadri.Vasudevan@Sun.COM 		}
507110021SSheshadri.Vasudevan@Sun.COM 
507210021SSheshadri.Vasudevan@Sun.COM 		if ((len == 2) && (toupper(buf[0]) == 'I')) {
507310021SSheshadri.Vasudevan@Sun.COM 			ext_print_part_types();
507410021SSheshadri.Vasudevan@Sun.COM 			continue;
507510021SSheshadri.Vasudevan@Sun.COM 		}
507610021SSheshadri.Vasudevan@Sun.COM 
507710021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
507810021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
507910021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
508010021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid partition ID\n");
508110021SSheshadri.Vasudevan@Sun.COM 				break;
508210021SSheshadri.Vasudevan@Sun.COM 			}
508310021SSheshadri.Vasudevan@Sun.COM 		}
508410021SSheshadri.Vasudevan@Sun.COM 
508510021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
508610021SSheshadri.Vasudevan@Sun.COM 			continue;
508710021SSheshadri.Vasudevan@Sun.COM 		}
508810021SSheshadri.Vasudevan@Sun.COM 
508910021SSheshadri.Vasudevan@Sun.COM 		/* Check if the (now) valid number is greater than the limit */
509010021SSheshadri.Vasudevan@Sun.COM 		if ((id = atoi(buf)) > FDISK_MAX_VALID_PART_ID) {
509110021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition ID\n");
509210021SSheshadri.Vasudevan@Sun.COM 			continue;
509310021SSheshadri.Vasudevan@Sun.COM 		}
509410021SSheshadri.Vasudevan@Sun.COM 
509510021SSheshadri.Vasudevan@Sun.COM 		*partid = (uchar_t)id;
509610021SSheshadri.Vasudevan@Sun.COM 
509710021SSheshadri.Vasudevan@Sun.COM 		/* Disallow multiple extended partitions */
509810021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(*partid)) {
509910021SSheshadri.Vasudevan@Sun.COM 			printf("Multiple extended partitions not allowed\n");
510010021SSheshadri.Vasudevan@Sun.COM 			continue;
510110021SSheshadri.Vasudevan@Sun.COM 		}
510210021SSheshadri.Vasudevan@Sun.COM 
510310021SSheshadri.Vasudevan@Sun.COM 		/* Disallow EFI partitions within extended partition */
510410021SSheshadri.Vasudevan@Sun.COM 		if (*partid == EFI_PMBR) {
510510021SSheshadri.Vasudevan@Sun.COM 			printf("EFI partitions within an extended partition"
510610021SSheshadri.Vasudevan@Sun.COM 			    " is not allowed\n");
510710021SSheshadri.Vasudevan@Sun.COM 			continue;
510810021SSheshadri.Vasudevan@Sun.COM 		}
510910021SSheshadri.Vasudevan@Sun.COM 
511010021SSheshadri.Vasudevan@Sun.COM 		return; /* Valid partition ID is in partid */
511110021SSheshadri.Vasudevan@Sun.COM 	}
511210021SSheshadri.Vasudevan@Sun.COM }
511310021SSheshadri.Vasudevan@Sun.COM 
511410021SSheshadri.Vasudevan@Sun.COM static void
511510021SSheshadri.Vasudevan@Sun.COM delete_logical_drive()
511610021SSheshadri.Vasudevan@Sun.COM {
511710021SSheshadri.Vasudevan@Sun.COM 	int pno;
511810021SSheshadri.Vasudevan@Sun.COM 
511910021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
512010021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
512110021SSheshadri.Vasudevan@Sun.COM 		return;
512210021SSheshadri.Vasudevan@Sun.COM 	}
512310021SSheshadri.Vasudevan@Sun.COM 
512410021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
512510021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
512610021SSheshadri.Vasudevan@Sun.COM 	fdisk_delete_logical_drive(epp, pno);
512710021SSheshadri.Vasudevan@Sun.COM 	printf("Partition %d deleted\n", pno);
512810021SSheshadri.Vasudevan@Sun.COM }
512910021SSheshadri.Vasudevan@Sun.COM 
513010021SSheshadri.Vasudevan@Sun.COM static int
513110021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_start(uint32_t *begsec)
513210021SSheshadri.Vasudevan@Sun.COM {
513310021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
513410021SSheshadri.Vasudevan@Sun.COM 	int ret, len, i;
513510021SSheshadri.Vasudevan@Sun.COM 	uint32_t begcyl;
513610021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_cyl;
513710021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_sec;
513810021SSheshadri.Vasudevan@Sun.COM 
513910021SSheshadri.Vasudevan@Sun.COM 	ret = fdisk_ext_find_first_free_sec(epp, &first_free_sec);
514010021SSheshadri.Vasudevan@Sun.COM 	if (ret != FDISK_SUCCESS) {
514110021SSheshadri.Vasudevan@Sun.COM 		return (ret);
514210021SSheshadri.Vasudevan@Sun.COM 	}
514310021SSheshadri.Vasudevan@Sun.COM 
514410021SSheshadri.Vasudevan@Sun.COM 	first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec);
514510021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
514610021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the beginning cylinder (Default - %d) : ",
514710021SSheshadri.Vasudevan@Sun.COM 		    first_free_cyl);
514810021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
514910021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
515010021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
515110021SSheshadri.Vasudevan@Sun.COM 			*begsec = first_free_sec;
515210021SSheshadri.Vasudevan@Sun.COM 			return (FDISK_SUCCESS);
515310021SSheshadri.Vasudevan@Sun.COM 		}
515410021SSheshadri.Vasudevan@Sun.COM 
515510021SSheshadri.Vasudevan@Sun.COM 		if (len > (FDISK_MAX_VALID_CYL_NUM_DIGITS + 1)) {
515610021SSheshadri.Vasudevan@Sun.COM 			printf("Input too long\n");
515710021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid beginning cylinder number\n");
515810021SSheshadri.Vasudevan@Sun.COM 			continue;
515910021SSheshadri.Vasudevan@Sun.COM 		}
516010021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
516110021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len - 1; i++) {
516210021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
516310021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
516410021SSheshadri.Vasudevan@Sun.COM 				break;
516510021SSheshadri.Vasudevan@Sun.COM 			}
516610021SSheshadri.Vasudevan@Sun.COM 		}
516710021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
516810021SSheshadri.Vasudevan@Sun.COM 			continue;
516910021SSheshadri.Vasudevan@Sun.COM 		}
517010021SSheshadri.Vasudevan@Sun.COM 
517110021SSheshadri.Vasudevan@Sun.COM 		begcyl = atoi(buf);
517210021SSheshadri.Vasudevan@Sun.COM 		ret = fdisk_ext_validate_part_start(epp, begcyl, begsec);
517310021SSheshadri.Vasudevan@Sun.COM 		switch (ret) {
517410021SSheshadri.Vasudevan@Sun.COM 			case FDISK_SUCCESS:
517510021SSheshadri.Vasudevan@Sun.COM 				/*
517610021SSheshadri.Vasudevan@Sun.COM 				 * Success.
517710021SSheshadri.Vasudevan@Sun.COM 				 * Valid beginning sector is in begsec
517810021SSheshadri.Vasudevan@Sun.COM 				 */
517910021SSheshadri.Vasudevan@Sun.COM 				break;
518010021SSheshadri.Vasudevan@Sun.COM 
518110021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOVERLAP:
518210021SSheshadri.Vasudevan@Sun.COM 				printf("Partition boundary overlaps with ");
518310021SSheshadri.Vasudevan@Sun.COM 				printf("existing partitions\n");
518410021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
518510021SSheshadri.Vasudevan@Sun.COM 				continue;
518610021SSheshadri.Vasudevan@Sun.COM 
518710021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOOBOUND:
518810021SSheshadri.Vasudevan@Sun.COM 				printf("Cylinder boundary beyond the limits\n");
518910021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid beginning cylinder number\n");
519010021SSheshadri.Vasudevan@Sun.COM 				continue;
519110021SSheshadri.Vasudevan@Sun.COM 		}
519210021SSheshadri.Vasudevan@Sun.COM 		return (FDISK_SUCCESS);
519310021SSheshadri.Vasudevan@Sun.COM 	}
519410021SSheshadri.Vasudevan@Sun.COM }
519510021SSheshadri.Vasudevan@Sun.COM 
519610021SSheshadri.Vasudevan@Sun.COM /*
519710021SSheshadri.Vasudevan@Sun.COM  * Algorithm :
519810021SSheshadri.Vasudevan@Sun.COM  * 1. Check if the first character is a +
519910021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, check if the last character is 'k', 'm' or 'g'
520010021SSheshadri.Vasudevan@Sun.COM  * 2. If not, check if there are any non-digits
520110021SSheshadri.Vasudevan@Sun.COM  * 3. Check for the length of the numeral string
520210021SSheshadri.Vasudevan@Sun.COM  * 4. atoi the numeral string
520310021SSheshadri.Vasudevan@Sun.COM  * 5. In case of data entered in KB, MB or GB, convert it to number of cylinders
520410021SSheshadri.Vasudevan@Sun.COM  *	a) Adjust size to be cylinder boundary aligned
520510021SSheshadri.Vasudevan@Sun.COM  * 6. If size specifies is zero, flag error
520610021SSheshadri.Vasudevan@Sun.COM  * 7. Check if the size is less than 1 cylinder
520710021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, default the size FDISK_MIN_PART_SIZE
520810021SSheshadri.Vasudevan@Sun.COM  * 	b) If no, Check if the size is within endcyl - begcyl
520910021SSheshadri.Vasudevan@Sun.COM  */
521010021SSheshadri.Vasudevan@Sun.COM static void
521110021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec)
521210021SSheshadri.Vasudevan@Sun.COM {
521310021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
521410021SSheshadri.Vasudevan@Sun.COM 	uint32_t tempcyl;
521510021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_sec;
521610021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_cyl;
521710021SSheshadri.Vasudevan@Sun.COM 	int i, len, ch, mbgb = 0, scale = FDISK_SECTS_PER_CYL(epp);
521810021SSheshadri.Vasudevan@Sun.COM 	uint64_t size = 0;
521910021SSheshadri.Vasudevan@Sun.COM 	int copy_len;
522010021SSheshadri.Vasudevan@Sun.COM 	char numbuf[FDISK_MAX_VALID_CYL_NUM_DIGITS + 1];
522110021SSheshadri.Vasudevan@Sun.COM 	int sectsize = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE);
522210021SSheshadri.Vasudevan@Sun.COM 	uint32_t remdr, spc, poss_end;
522310021SSheshadri.Vasudevan@Sun.COM 
522410021SSheshadri.Vasudevan@Sun.COM 	if (sectsize == EINVAL) {
522510021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Unsupported geometry statistics.\n");
522610021SSheshadri.Vasudevan@Sun.COM 		exit(1);
522710021SSheshadri.Vasudevan@Sun.COM 	}
522810021SSheshadri.Vasudevan@Sun.COM 
522910021SSheshadri.Vasudevan@Sun.COM 	last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec);
523010021SSheshadri.Vasudevan@Sun.COM 	last_free_cyl = FDISK_SECT_TO_CYL(epp, last_free_sec);
523110021SSheshadri.Vasudevan@Sun.COM 
523210021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
523310021SSheshadri.Vasudevan@Sun.COM 		printf("Enter the size in cylinders (Default End Cylinder -");
523410021SSheshadri.Vasudevan@Sun.COM 		printf(" %u)\n", last_free_cyl);
523510021SSheshadri.Vasudevan@Sun.COM 		printf("Type +<size>K, +<size>M or +<size>G to enter size in");
523610021SSheshadri.Vasudevan@Sun.COM 		printf("KB, MB or GB : ");
523710021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
523810021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
523910021SSheshadri.Vasudevan@Sun.COM 		mbgb = 0;
524010021SSheshadri.Vasudevan@Sun.COM 		scale = FDISK_SECTS_PER_CYL(epp);
524110021SSheshadri.Vasudevan@Sun.COM 
524210021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
524310021SSheshadri.Vasudevan@Sun.COM 			*endsec = last_free_sec;
524410021SSheshadri.Vasudevan@Sun.COM 			return;
524510021SSheshadri.Vasudevan@Sun.COM 		}
524610021SSheshadri.Vasudevan@Sun.COM 
524710021SSheshadri.Vasudevan@Sun.COM 		copy_len = len - 1;
524810021SSheshadri.Vasudevan@Sun.COM 
524910021SSheshadri.Vasudevan@Sun.COM 		if ((buf[0] == '+') && (isdigit(buf[1]))) {
525010021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
525110021SSheshadri.Vasudevan@Sun.COM 			if ((ch = toupper(buf[len - 2])) == 'B') {
525210021SSheshadri.Vasudevan@Sun.COM 				ch = toupper(buf[len - 3]);
525310021SSheshadri.Vasudevan@Sun.COM 				copy_len--;
525410021SSheshadri.Vasudevan@Sun.COM 			}
525510021SSheshadri.Vasudevan@Sun.COM 
525610021SSheshadri.Vasudevan@Sun.COM 			if (!((ch == 'K') || (ch == 'M') || (ch == 'G'))) {
525710021SSheshadri.Vasudevan@Sun.COM 				printf("Invalid partition size\n");
525810021SSheshadri.Vasudevan@Sun.COM 				continue;
525910021SSheshadri.Vasudevan@Sun.COM 			}
526010021SSheshadri.Vasudevan@Sun.COM 
526110021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
526210021SSheshadri.Vasudevan@Sun.COM 			mbgb = 1;
526310021SSheshadri.Vasudevan@Sun.COM 			scale = ((ch == 'K') ? FDISK_KB :
526410021SSheshadri.Vasudevan@Sun.COM 			    ((ch == 'M') ? FDISK_MB : FDISK_GB));
526510021SSheshadri.Vasudevan@Sun.COM 		}
526610021SSheshadri.Vasudevan@Sun.COM 
526710021SSheshadri.Vasudevan@Sun.COM 		if (copy_len > FDISK_MAX_VALID_CYL_NUM_DIGITS) {
526810021SSheshadri.Vasudevan@Sun.COM 			printf("Input too long\n");
526910021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
527010021SSheshadri.Vasudevan@Sun.COM 			continue;
527110021SSheshadri.Vasudevan@Sun.COM 		}
527210021SSheshadri.Vasudevan@Sun.COM 
527310021SSheshadri.Vasudevan@Sun.COM 		strncpy(numbuf, &buf[mbgb], copy_len);
527410021SSheshadri.Vasudevan@Sun.COM 		numbuf[copy_len] = '\0';
527510021SSheshadri.Vasudevan@Sun.COM 
527610021SSheshadri.Vasudevan@Sun.COM 		for (i = mbgb; i < copy_len + mbgb; i++) {
527710021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
527810021SSheshadri.Vasudevan@Sun.COM 				break;
527910021SSheshadri.Vasudevan@Sun.COM 			}
528010021SSheshadri.Vasudevan@Sun.COM 		}
528110021SSheshadri.Vasudevan@Sun.COM 
528210021SSheshadri.Vasudevan@Sun.COM 		if (i < copy_len + mbgb) {
528310021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
528410021SSheshadri.Vasudevan@Sun.COM 			continue;
528510021SSheshadri.Vasudevan@Sun.COM 		}
528610021SSheshadri.Vasudevan@Sun.COM 
528710021SSheshadri.Vasudevan@Sun.COM 		size = (atoll(numbuf) * (scale));
528810021SSheshadri.Vasudevan@Sun.COM 
528910021SSheshadri.Vasudevan@Sun.COM 		if (size == 0) {
529010021SSheshadri.Vasudevan@Sun.COM 			printf("Zero size is invalid\n");
529110021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
529210021SSheshadri.Vasudevan@Sun.COM 			continue;
529310021SSheshadri.Vasudevan@Sun.COM 		}
529410021SSheshadri.Vasudevan@Sun.COM 
529510021SSheshadri.Vasudevan@Sun.COM 		if (mbgb) {
529610021SSheshadri.Vasudevan@Sun.COM 			size /= sectsize;
529710021SSheshadri.Vasudevan@Sun.COM 		}
529810021SSheshadri.Vasudevan@Sun.COM 
529910021SSheshadri.Vasudevan@Sun.COM 		if (size > (last_free_sec - begsec + 1)) {
530010021SSheshadri.Vasudevan@Sun.COM 			printf("Cylinder boundary beyond the limits");
530110021SSheshadri.Vasudevan@Sun.COM 			printf(" or overlaps with existing");
530210021SSheshadri.Vasudevan@Sun.COM 			printf(" partitions\n");
530310021SSheshadri.Vasudevan@Sun.COM 			printf("Invalid partition size\n");
530410021SSheshadri.Vasudevan@Sun.COM 			continue;
530510021SSheshadri.Vasudevan@Sun.COM 		}
530610021SSheshadri.Vasudevan@Sun.COM 
530710021SSheshadri.Vasudevan@Sun.COM 		/*
530810021SSheshadri.Vasudevan@Sun.COM 		 * Adjust the ending sector such that there are no partial
530910021SSheshadri.Vasudevan@Sun.COM 		 * cylinders allocated. But at the same time, make sure it
531010021SSheshadri.Vasudevan@Sun.COM 		 * doesn't over shoot boundaries.
531110021SSheshadri.Vasudevan@Sun.COM 		 */
531210021SSheshadri.Vasudevan@Sun.COM 		spc = FDISK_SECTS_PER_CYL(epp);
531310021SSheshadri.Vasudevan@Sun.COM 		poss_end = begsec + size - 1;
531410021SSheshadri.Vasudevan@Sun.COM 		if (remdr = (poss_end % spc)) {
531510021SSheshadri.Vasudevan@Sun.COM 			poss_end += spc - remdr - 1;
531610021SSheshadri.Vasudevan@Sun.COM 		}
531710021SSheshadri.Vasudevan@Sun.COM 		*endsec = (poss_end > last_free_sec) ? last_free_sec :
531810021SSheshadri.Vasudevan@Sun.COM 		    poss_end;
531910021SSheshadri.Vasudevan@Sun.COM 
532010021SSheshadri.Vasudevan@Sun.COM 		return;
532110021SSheshadri.Vasudevan@Sun.COM 	}
532210021SSheshadri.Vasudevan@Sun.COM }
532310021SSheshadri.Vasudevan@Sun.COM 
532410021SSheshadri.Vasudevan@Sun.COM /*
532510021SSheshadri.Vasudevan@Sun.COM  * ALGORITHM:
532610021SSheshadri.Vasudevan@Sun.COM  * 1. Get the starting and ending sectors/cylinder of the extended partition.
532710021SSheshadri.Vasudevan@Sun.COM  * 2. Keep track of the first free sector/cylinder
532810021SSheshadri.Vasudevan@Sun.COM  * 3. Allow the user to specify the beginning cylinder of the new partition
532910021SSheshadri.Vasudevan@Sun.COM  * 4. Check for the validity of the entered data
533010021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
533110021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
533210021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
533310021SSheshadri.Vasudevan@Sun.COM  * 5. Allow the user to specify the size in cylinders/ human readable form
533410021SSheshadri.Vasudevan@Sun.COM  * 6. Check for the validity of the entered data
533510021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
533610021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
533710021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
533810021SSheshadri.Vasudevan@Sun.COM  *	d) If it is a number lesser than the starting cylinder
533910021SSheshadri.Vasudevan@Sun.COM  * 7. Request partition ID for the new partition.
534010021SSheshadri.Vasudevan@Sun.COM  * 8. Update the first free cylinder available
534110021SSheshadri.Vasudevan@Sun.COM  * 9. Display Success message
534210021SSheshadri.Vasudevan@Sun.COM  */
534310021SSheshadri.Vasudevan@Sun.COM 
534410021SSheshadri.Vasudevan@Sun.COM static void
534510021SSheshadri.Vasudevan@Sun.COM add_logical_drive()
534610021SSheshadri.Vasudevan@Sun.COM {
534710021SSheshadri.Vasudevan@Sun.COM 	uint32_t begsec, endsec;
534810021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
534910021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
535010021SSheshadri.Vasudevan@Sun.COM 	int rval;
535110021SSheshadri.Vasudevan@Sun.COM 
535210021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_get_logical_drive_count(epp) >= MAX_EXT_PARTS) {
535310021SSheshadri.Vasudevan@Sun.COM 		printf("\nNumber of logical drives exceeds limit of %d.\n",
535410021SSheshadri.Vasudevan@Sun.COM 		    MAX_EXT_PARTS);
535510021SSheshadri.Vasudevan@Sun.COM 		printf("Command did not succeed. Press enter to continue\n");
535610021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
535710021SSheshadri.Vasudevan@Sun.COM 		return;
535810021SSheshadri.Vasudevan@Sun.COM 	}
535910021SSheshadri.Vasudevan@Sun.COM 
536010021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
536110021SSheshadri.Vasudevan@Sun.COM 	rval = ext_read_valid_partition_start(&begsec);
536210021SSheshadri.Vasudevan@Sun.COM 	switch (rval) {
536310021SSheshadri.Vasudevan@Sun.COM 		case FDISK_SUCCESS:
536410021SSheshadri.Vasudevan@Sun.COM 			break;
536510021SSheshadri.Vasudevan@Sun.COM 
536610021SSheshadri.Vasudevan@Sun.COM 		case FDISK_EOOBOUND:
536710021SSheshadri.Vasudevan@Sun.COM 			printf("\nNo space left in the extended partition\n");
536810021SSheshadri.Vasudevan@Sun.COM 			printf("Press enter to continue\n");
536910021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
537010021SSheshadri.Vasudevan@Sun.COM 			return;
537110021SSheshadri.Vasudevan@Sun.COM 	}
537210021SSheshadri.Vasudevan@Sun.COM 
537310021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_partition_size(begsec, &endsec);
537410021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
537510021SSheshadri.Vasudevan@Sun.COM 	fdisk_add_logical_drive(epp, begsec, endsec, partid);
537610021SSheshadri.Vasudevan@Sun.COM 
537710021SSheshadri.Vasudevan@Sun.COM 	printf("New partition with ID %d added\n", partid);
537810021SSheshadri.Vasudevan@Sun.COM }
537910021SSheshadri.Vasudevan@Sun.COM 
538010021SSheshadri.Vasudevan@Sun.COM static void
538110021SSheshadri.Vasudevan@Sun.COM ext_change_logical_drive_id()
538210021SSheshadri.Vasudevan@Sun.COM {
538310021SSheshadri.Vasudevan@Sun.COM 	int pno;
538410021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
538510021SSheshadri.Vasudevan@Sun.COM 
538610021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
538710021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
538810021SSheshadri.Vasudevan@Sun.COM 		return;
538910021SSheshadri.Vasudevan@Sun.COM 	}
539010021SSheshadri.Vasudevan@Sun.COM 
539110021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
539210021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
539310021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
539410021SSheshadri.Vasudevan@Sun.COM 	fdisk_change_logical_drive_id(epp, pno, partid);
539510021SSheshadri.Vasudevan@Sun.COM 
539610021SSheshadri.Vasudevan@Sun.COM 	printf("Partition ID of partition %d changed to %d\n", pno, partid);
539710021SSheshadri.Vasudevan@Sun.COM }
539810021SSheshadri.Vasudevan@Sun.COM 
539910021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
540010021SSheshadri.Vasudevan@Sun.COM static void
540110021SSheshadri.Vasudevan@Sun.COM ext_print_logdrive_layout_debug()
540210021SSheshadri.Vasudevan@Sun.COM {
540310021SSheshadri.Vasudevan@Sun.COM 	int pno;
540410021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
540510021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *head = fdisk_get_ld_head(epp);
540610021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
540710021SSheshadri.Vasudevan@Sun.COM 
540810021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
540910021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
541010021SSheshadri.Vasudevan@Sun.COM 		return;
541110021SSheshadri.Vasudevan@Sun.COM 	}
541210021SSheshadri.Vasudevan@Sun.COM 
541310021SSheshadri.Vasudevan@Sun.COM 	printf("\n\n");
541410021SSheshadri.Vasudevan@Sun.COM 	puts("#  start block  end block    abs start    abs end      OSType");
541510021SSheshadri.Vasudevan@Sun.COM 	for (temp = head, pno = 5; temp != NULL; temp = temp->next, pno++) {
541610021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
541710021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[0].systid, namebuff);
541810021SSheshadri.Vasudevan@Sun.COM 		printf("%d: %.10u   %.10u   %.10u   %.10u",
541910021SSheshadri.Vasudevan@Sun.COM 		    pno,
542010021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].relsect),
542110021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].numsect),
542210021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum,
542310021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum + temp->numsect - 1 +
542410021SSheshadri.Vasudevan@Sun.COM 		    MAX_LOGDRIVE_OFFSET);
542510021SSheshadri.Vasudevan@Sun.COM 		printf("   %s\n", namebuff);
542610021SSheshadri.Vasudevan@Sun.COM 		/*
542710021SSheshadri.Vasudevan@Sun.COM 		 * Print the second entry in the EBR which is information
542810021SSheshadri.Vasudevan@Sun.COM 		 * about the location and the size of the next extended
542910021SSheshadri.Vasudevan@Sun.COM 		 * partition.
543010021SSheshadri.Vasudevan@Sun.COM 		 */
543110021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[1].systid, namebuff);
543210021SSheshadri.Vasudevan@Sun.COM 		printf("%d: %.10u   %.10u   %.10s   %.10s",
543310021SSheshadri.Vasudevan@Sun.COM 		    pno,
543410021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].relsect),
543510021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].numsect),
543610021SSheshadri.Vasudevan@Sun.COM 		    "          ", "          ");
543710021SSheshadri.Vasudevan@Sun.COM 		printf("   %s\n", namebuff);
543810021SSheshadri.Vasudevan@Sun.COM 	}
543910021SSheshadri.Vasudevan@Sun.COM }
544010021SSheshadri.Vasudevan@Sun.COM #endif
544110021SSheshadri.Vasudevan@Sun.COM 
544210021SSheshadri.Vasudevan@Sun.COM static void
544310021SSheshadri.Vasudevan@Sun.COM ext_print_logical_drive_layout()
544410021SSheshadri.Vasudevan@Sun.COM {
544510021SSheshadri.Vasudevan@Sun.COM 	int sysid;
544610021SSheshadri.Vasudevan@Sun.COM 	unsigned int startcyl, endcyl, length, percent, remainder;
544710021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
544810682SSheshadri.Vasudevan@Sun.COM 	uint32_t part_start;
544910021SSheshadri.Vasudevan@Sun.COM 	struct ipart *fpart;
545010021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
545110021SSheshadri.Vasudevan@Sun.COM 	int numcyl = fdisk_get_disk_geom(epp, PHYSGEOM, NCYL);
545210021SSheshadri.Vasudevan@Sun.COM 	int pno;
545310021SSheshadri.Vasudevan@Sun.COM 
545410021SSheshadri.Vasudevan@Sun.COM 	if (numcyl == EINVAL) {
545510021SSheshadri.Vasudevan@Sun.COM 		fprintf(stderr, "Unsupported geometry statistics.\n");
545610021SSheshadri.Vasudevan@Sun.COM 		exit(1);
545710021SSheshadri.Vasudevan@Sun.COM 	}
545810021SSheshadri.Vasudevan@Sun.COM 
545910021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
546010021SSheshadri.Vasudevan@Sun.COM 		printf("\nNo logical drives defined.\n");
546110021SSheshadri.Vasudevan@Sun.COM 		return;
546210021SSheshadri.Vasudevan@Sun.COM 	}
546310021SSheshadri.Vasudevan@Sun.COM 
546410021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
546510021SSheshadri.Vasudevan@Sun.COM 	printf("Number of cylinders in disk              : %u\n", numcyl);
546610021SSheshadri.Vasudevan@Sun.COM 	printf("Beginning cylinder of extended partition : %u\n",
546710021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_beg_cyl(epp));
546810021SSheshadri.Vasudevan@Sun.COM 	printf("Ending cylinder of extended partition    : %u\n",
546910021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_end_cyl(epp));
547010021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
547110021SSheshadri.Vasudevan@Sun.COM 	printf("Part#   StartCyl   EndCyl     Length    %%     "
547210021SSheshadri.Vasudevan@Sun.COM 	"Part ID (Type)\n");
547310021SSheshadri.Vasudevan@Sun.COM 	printf("=====   ========   ========   =======   ==="
547410021SSheshadri.Vasudevan@Sun.COM 	"   ==============\n");
547510021SSheshadri.Vasudevan@Sun.COM 	for (temp = fdisk_get_ld_head(epp), pno = 5; temp != NULL;
547610021SSheshadri.Vasudevan@Sun.COM 	    temp = temp->next, pno++) {
547710021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
547810021SSheshadri.Vasudevan@Sun.COM 		fpart = &temp->parts[0];
547910021SSheshadri.Vasudevan@Sun.COM 		sysid = fpart->systid;
548010682SSheshadri.Vasudevan@Sun.COM 		/*
548110682SSheshadri.Vasudevan@Sun.COM 		 * Check if partition id 0x82 is Solaris
548210682SSheshadri.Vasudevan@Sun.COM 		 * or a Linux swap. Print the string
548310682SSheshadri.Vasudevan@Sun.COM 		 * accordingly.
548410682SSheshadri.Vasudevan@Sun.COM 		 */
548510682SSheshadri.Vasudevan@Sun.COM 		if (sysid == SUNIXOS) {
548610682SSheshadri.Vasudevan@Sun.COM 			part_start = temp->abs_secnum +
548710682SSheshadri.Vasudevan@Sun.COM 			    temp->logdrive_offset;
548810682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, part_start,
548910682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
549010682SSheshadri.Vasudevan@Sun.COM 				strcpy(namebuff, LINSWAPstr);
549110682SSheshadri.Vasudevan@Sun.COM 			else
549210682SSheshadri.Vasudevan@Sun.COM 				strcpy(namebuff, SUstr);
549310682SSheshadri.Vasudevan@Sun.COM 		} else {
549410682SSheshadri.Vasudevan@Sun.COM 			id_to_name(sysid, namebuff);
549510682SSheshadri.Vasudevan@Sun.COM 		}
549610021SSheshadri.Vasudevan@Sun.COM 		startcyl = temp->begcyl;
549710021SSheshadri.Vasudevan@Sun.COM 		endcyl = temp->endcyl;
549810021SSheshadri.Vasudevan@Sun.COM 		if (startcyl == endcyl) {
549910021SSheshadri.Vasudevan@Sun.COM 			length = 1;
550010021SSheshadri.Vasudevan@Sun.COM 		} else {
550110021SSheshadri.Vasudevan@Sun.COM 			length = endcyl - startcyl + 1;
550210021SSheshadri.Vasudevan@Sun.COM 		}
550310021SSheshadri.Vasudevan@Sun.COM 		percent = length * 100 / numcyl;
550410021SSheshadri.Vasudevan@Sun.COM 		if ((remainder = (length * 100 % numcyl)) != 0) {
550510021SSheshadri.Vasudevan@Sun.COM 			if ((remainder * 100 / numcyl) > 50) {
550610021SSheshadri.Vasudevan@Sun.COM 				/* round up */
550710021SSheshadri.Vasudevan@Sun.COM 				percent++;
550810021SSheshadri.Vasudevan@Sun.COM 			}
550910021SSheshadri.Vasudevan@Sun.COM 			/* Else leave the percent as is since it's already */
551010021SSheshadri.Vasudevan@Sun.COM 			/* rounded down */
551110021SSheshadri.Vasudevan@Sun.COM 		}
551210021SSheshadri.Vasudevan@Sun.COM 		if (percent > 100) {
551310021SSheshadri.Vasudevan@Sun.COM 			percent = 100;
551410021SSheshadri.Vasudevan@Sun.COM 		}
551510021SSheshadri.Vasudevan@Sun.COM 		printf("%-5d   %-8u   %-8u   %-7u   %-3d   %-3d (%-.28s)\n",
551610021SSheshadri.Vasudevan@Sun.COM 		    pno, startcyl, endcyl, length, percent, sysid, namebuff);
551710021SSheshadri.Vasudevan@Sun.COM 	}
551810021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
551910021SSheshadri.Vasudevan@Sun.COM 	ext_print_logdrive_layout_debug();
552010021SSheshadri.Vasudevan@Sun.COM #endif
552110021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
552210021SSheshadri.Vasudevan@Sun.COM }
552310021SSheshadri.Vasudevan@Sun.COM 
552410021SSheshadri.Vasudevan@Sun.COM static void
552510021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu()
552610021SSheshadri.Vasudevan@Sun.COM {
552710021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
552810021SSheshadri.Vasudevan@Sun.COM 	printf("a	Add a logical drive\n");
552910021SSheshadri.Vasudevan@Sun.COM 	printf("d	Delete a logical drive\n");
553010021SSheshadri.Vasudevan@Sun.COM 	printf("h	Print this help menu\n");
553110021SSheshadri.Vasudevan@Sun.COM 	printf("i	Change the id of the logical drive\n");
553210021SSheshadri.Vasudevan@Sun.COM 	printf("p	Print the logical drive layout\n");
553310021SSheshadri.Vasudevan@Sun.COM 	printf("r	Return to the main fdisk menu\n");
553410021SSheshadri.Vasudevan@Sun.COM 	printf("        (To commit or cancel the changes)\n");
553510021SSheshadri.Vasudevan@Sun.COM 	printf("\n");
553610021SSheshadri.Vasudevan@Sun.COM }
553710021SSheshadri.Vasudevan@Sun.COM 
553810021SSheshadri.Vasudevan@Sun.COM static void
553910021SSheshadri.Vasudevan@Sun.COM ext_part_menu()
554010021SSheshadri.Vasudevan@Sun.COM {
554110021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
554210021SSheshadri.Vasudevan@Sun.COM 	uchar_t *bbsigp;
554310021SSheshadri.Vasudevan@Sun.COM 	static int bbsig_disp_flag = 1;
554410021SSheshadri.Vasudevan@Sun.COM 
554510021SSheshadri.Vasudevan@Sun.COM 	int i;
554610021SSheshadri.Vasudevan@Sun.COM 
554710021SSheshadri.Vasudevan@Sun.COM 	printf(CLR_SCR);
554810021SSheshadri.Vasudevan@Sun.COM 
554910021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_corrupt_logical_drives(epp)) {
555010021SSheshadri.Vasudevan@Sun.COM 		printf("One or more logical drives seem to be corrupt.\n");
555110021SSheshadri.Vasudevan@Sun.COM 		printf("Displaying only sane logical drives.\n");
555210021SSheshadri.Vasudevan@Sun.COM 	}
555310021SSheshadri.Vasudevan@Sun.COM 
555410021SSheshadri.Vasudevan@Sun.COM 	if (bbsig_disp_flag && fdisk_invalid_bb_sig(epp, &bbsigp)) {
555510021SSheshadri.Vasudevan@Sun.COM 		printf("The following logical drives have a wrong boot block"
555610021SSheshadri.Vasudevan@Sun.COM 		    " signature :\n\n");
555710021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; bbsigp[i]; i++) {
555810021SSheshadri.Vasudevan@Sun.COM 			printf("%d ", bbsigp[i]);
555910021SSheshadri.Vasudevan@Sun.COM 		}
556010021SSheshadri.Vasudevan@Sun.COM 		printf("\n\n");
556110021SSheshadri.Vasudevan@Sun.COM 		printf("They will be corrected when you choose to commit\n");
556210021SSheshadri.Vasudevan@Sun.COM 		bbsig_disp_flag = 0;
556310021SSheshadri.Vasudevan@Sun.COM 	}
556410021SSheshadri.Vasudevan@Sun.COM 
556510021SSheshadri.Vasudevan@Sun.COM 	printf("Extended partition menu\n");
556610021SSheshadri.Vasudevan@Sun.COM 
556710021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
556810021SSheshadri.Vasudevan@Sun.COM 		printf("\nEnter Command (Type h for help) : ");
556910021SSheshadri.Vasudevan@Sun.COM 		if ((ext_read_options(buf)) < 0) {
557010021SSheshadri.Vasudevan@Sun.COM 			printf("\nCommand Options : \n");
557110021SSheshadri.Vasudevan@Sun.COM 			ext_print_help_menu();
557210021SSheshadri.Vasudevan@Sun.COM 			continue;
557310021SSheshadri.Vasudevan@Sun.COM 		}
557410021SSheshadri.Vasudevan@Sun.COM 		switch (buf[0]) {
557510021SSheshadri.Vasudevan@Sun.COM 			case 'a':
557610021SSheshadri.Vasudevan@Sun.COM 				add_logical_drive();
557710021SSheshadri.Vasudevan@Sun.COM 				break;
557810021SSheshadri.Vasudevan@Sun.COM 			case 'd':
557910021SSheshadri.Vasudevan@Sun.COM 				delete_logical_drive();
558010021SSheshadri.Vasudevan@Sun.COM 				break;
558110021SSheshadri.Vasudevan@Sun.COM 			case 'h':
558210021SSheshadri.Vasudevan@Sun.COM 				ext_print_help_menu();
558310021SSheshadri.Vasudevan@Sun.COM 				break;
558410021SSheshadri.Vasudevan@Sun.COM 			case 'i':
558510021SSheshadri.Vasudevan@Sun.COM 				ext_change_logical_drive_id();
558610021SSheshadri.Vasudevan@Sun.COM 				break;
558710021SSheshadri.Vasudevan@Sun.COM 			case 'p':
558810021SSheshadri.Vasudevan@Sun.COM 				ext_print_logical_drive_layout();
558910021SSheshadri.Vasudevan@Sun.COM 				break;
559010021SSheshadri.Vasudevan@Sun.COM 			case 'r':
559110021SSheshadri.Vasudevan@Sun.COM 				printf(CLR_SCR);
559210021SSheshadri.Vasudevan@Sun.COM 				return;
559310021SSheshadri.Vasudevan@Sun.COM 			default : /* NOTREACHED */
559410021SSheshadri.Vasudevan@Sun.COM 				break;
559510021SSheshadri.Vasudevan@Sun.COM 		}
559610021SSheshadri.Vasudevan@Sun.COM 	}
559710021SSheshadri.Vasudevan@Sun.COM }
559810021SSheshadri.Vasudevan@Sun.COM #endif
5599