xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 12505:0d402bc0353c)
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 /*
23*12505SShidokht.Yadegari@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
310Sstevel@tonic-gate /*	  All Rights Reserved	*/
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
350Sstevel@tonic-gate  * This program reads the partition table on the specified device and
360Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
370Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
380Sstevel@tonic-gate  * options are also available.
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate #include <stdio.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <string.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <errno.h>
450Sstevel@tonic-gate #include <fcntl.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate #include <sys/types.h>
497563SPrasad.Singamsetty@Sun.COM #include <limits.h>
500Sstevel@tonic-gate #include <sys/param.h>
510Sstevel@tonic-gate #include <sys/systeminfo.h>
520Sstevel@tonic-gate #include <sys/efi_partition.h>
530Sstevel@tonic-gate #include <sys/byteorder.h>
540Sstevel@tonic-gate #include <sys/systeminfo.h>
550Sstevel@tonic-gate 
560Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
570Sstevel@tonic-gate #include <sys/dkio.h>
580Sstevel@tonic-gate #include <sys/vtoc.h>
5910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
6010021SSheshadri.Vasudevan@Sun.COM #include <sys/tty.h>
6110021SSheshadri.Vasudevan@Sun.COM #include <libfdisk.h>
6210021SSheshadri.Vasudevan@Sun.COM #endif
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #define	CLR_SCR ""
650Sstevel@tonic-gate #define	CLR_LIN ""
660Sstevel@tonic-gate #define	HOME "" \
670Sstevel@tonic-gate 	""
680Sstevel@tonic-gate #define	Q_LINE ""
6910021SSheshadri.Vasudevan@Sun.COM 
7010021SSheshadri.Vasudevan@Sun.COM #ifdef i386
7110021SSheshadri.Vasudevan@Sun.COM #define	W_LINE ""
7210021SSheshadri.Vasudevan@Sun.COM #else
730Sstevel@tonic-gate #define	W_LINE ""
7410021SSheshadri.Vasudevan@Sun.COM #endif
7510021SSheshadri.Vasudevan@Sun.COM 
760Sstevel@tonic-gate #define	E_LINE ""
7710021SSheshadri.Vasudevan@Sun.COM 
7810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
7910021SSheshadri.Vasudevan@Sun.COM #define	M_LINE "" \
8010021SSheshadri.Vasudevan@Sun.COM 	""
8110021SSheshadri.Vasudevan@Sun.COM #else
820Sstevel@tonic-gate #define	M_LINE "" \
830Sstevel@tonic-gate 	""
8410021SSheshadri.Vasudevan@Sun.COM #endif
8510021SSheshadri.Vasudevan@Sun.COM 
860Sstevel@tonic-gate #define	T_LINE ""
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
890Sstevel@tonic-gate 
908333SSuhasini.Peddada@Sun.COM /* XXX - should be in fdisk.h, used by sd as well */
918333SSuhasini.Peddada@Sun.COM 
928333SSuhasini.Peddada@Sun.COM /*
938333SSuhasini.Peddada@Sun.COM  * the MAX values are the maximum usable values for BIOS chs values
948333SSuhasini.Peddada@Sun.COM  * The MAX_CYL value of 1022 is the maximum usable value
958333SSuhasini.Peddada@Sun.COM  *   the value of 1023 is a fence value,
968333SSuhasini.Peddada@Sun.COM  *   indicating no CHS geometry exists for the corresponding LBA value.
978333SSuhasini.Peddada@Sun.COM  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
988333SSuhasini.Peddada@Sun.COM  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
998333SSuhasini.Peddada@Sun.COM  */
1008333SSuhasini.Peddada@Sun.COM #define	MAX_SECT	(63)
1018333SSuhasini.Peddada@Sun.COM #define	MAX_CYL		(1022)
1028333SSuhasini.Peddada@Sun.COM #define	MAX_HEAD	(254)
1038333SSuhasini.Peddada@Sun.COM 
1047563SPrasad.Singamsetty@Sun.COM #define	DK_MAX_2TB	UINT32_MAX	/* Max # of sectors in 2TB */
1057563SPrasad.Singamsetty@Sun.COM 
1060Sstevel@tonic-gate /* for clear_vtoc() */
1070Sstevel@tonic-gate #define	OLD		0
1080Sstevel@tonic-gate #define	NEW		1
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
1110Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
1120Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
1130Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
1140Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
1150Sstevel@tonic-gate 
1168333SSuhasini.Peddada@Sun.COM /*
1178333SSuhasini.Peddada@Sun.COM  * Support for fdisk(1M) on the SPARC platform
1188333SSuhasini.Peddada@Sun.COM  *	In order to convert little endian values to big endian for SPARC,
1198333SSuhasini.Peddada@Sun.COM  *	byte/short and long values must be swapped.
1208333SSuhasini.Peddada@Sun.COM  *	These swapping macros will be used to access information in the
1218333SSuhasini.Peddada@Sun.COM  *	mboot and ipart structures.
1228333SSuhasini.Peddada@Sun.COM  */
1238333SSuhasini.Peddada@Sun.COM 
1248333SSuhasini.Peddada@Sun.COM #ifdef sparc
1258333SSuhasini.Peddada@Sun.COM #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1268333SSuhasini.Peddada@Sun.COM #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
1278333SSuhasini.Peddada@Sun.COM 			    (les((unsigned)((val)&0xffff0000)>>16)))
1288333SSuhasini.Peddada@Sun.COM #else
1298333SSuhasini.Peddada@Sun.COM #define	les(val)	(val)
1308333SSuhasini.Peddada@Sun.COM #define	lel(val)	(val)
1318333SSuhasini.Peddada@Sun.COM #endif
1328333SSuhasini.Peddada@Sun.COM 
1330Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1346549Sbharding #define	VTOC_OFFSET	1
1350Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1360Sstevel@tonic-gate #define	VTOC_OFFSET	0
1370Sstevel@tonic-gate #else
1380Sstevel@tonic-gate #error No VTOC format defined.
1390Sstevel@tonic-gate #endif
1400Sstevel@tonic-gate 
14110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
14210021SSheshadri.Vasudevan@Sun.COM #define	FDISK_KB	(1024)
14310021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MB	(FDISK_KB * 1024)
14410021SSheshadri.Vasudevan@Sun.COM #define	FDISK_GB	(FDISK_MB * 1024)
14510021SSheshadri.Vasudevan@Sun.COM #define	TRUE	1
14610021SSheshadri.Vasudevan@Sun.COM 
14710021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_ID	255
14810021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_NUM_DIGITS	2
14910021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_PART_ID_DIGITS	3
15010021SSheshadri.Vasudevan@Sun.COM 
15110021SSheshadri.Vasudevan@Sun.COM /* Maximum number of digits for a valid partition size */
15210021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MAX_VALID_CYL_NUM_DIGITS	10
15310021SSheshadri.Vasudevan@Sun.COM 
15410021SSheshadri.Vasudevan@Sun.COM /* Minimum partition size in cylinders */
15510021SSheshadri.Vasudevan@Sun.COM #define	FDISK_MIN_PART_SIZE	1
15610021SSheshadri.Vasudevan@Sun.COM #endif
15710021SSheshadri.Vasudevan@Sun.COM 
158251Slclee static char Usage[] = "Usage: fdisk\n"
1590Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1600Sstevel@tonic-gate "[ -b masterboot ]\n"
1610Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1620Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1630Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1640Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1650Sstevel@tonic-gate 
166251Slclee static char Usage1[] = "    Partition options:\n"
1670Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1680Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1690Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1700Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1710Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1720Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1730Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1740Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1750Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1760Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1770Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1780Sstevel@tonic-gate "			  start of partition\n"
1790Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1800Sstevel@tonic-gate "	-b master_boot\n"
1810Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1820Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1830Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1840Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1850Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1860Sstevel@tonic-gate "	-F fdisk_file\n"
1870Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1880Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1890Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1900Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1910Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1920Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1930Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1940Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1950Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1960Sstevel@tonic-gate "	-W fdisk_file\n"
1970Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1980Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1990Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
2000Sstevel@tonic-gate "    Diagnostic options:\n"
2010Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
2020Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
2030Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
2040Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
2050Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
2060Sstevel@tonic-gate "		BCYL		cylinder offset\n"
2070Sstevel@tonic-gate "		NHEADS		number of heads\n"
2080Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
2090Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
2100Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
2110Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
2120Sstevel@tonic-gate "	-o offset\n"
2130Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
2140Sstevel@tonic-gate "		-P # specified.\n"
2150Sstevel@tonic-gate "	-P fill_patt\n"
2160Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
2170Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
2180Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
2190Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
2200Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
2210Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
2220Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
2230Sstevel@tonic-gate "	-S geom_file\n"
2240Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
2250Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
2260Sstevel@tonic-gate 
227251Slclee static char Ostr[] = "Other OS";
228251Slclee static char Dstr[] = "DOS12";
229251Slclee static char D16str[] = "DOS16";
230251Slclee static char DDstr[] = "DOS-DATA";
231251Slclee static char EDstr[] = "EXT-DOS";
232251Slclee static char DBstr[] = "DOS-BIG";
233251Slclee static char PCstr[] = "PCIX";
234251Slclee static char Ustr[] = "UNIX System";
235251Slclee static char SUstr[] = "Solaris";
236251Slclee static char SU2str[] = "Solaris2";
237251Slclee static char X86str[] = "x86 Boot";
238251Slclee static char DIAGstr[] = "Diagnostic";
239251Slclee static char IFSstr[] = "IFS: NTFS";
240251Slclee static char AIXstr[] = "AIX Boot";
241251Slclee static char AIXDstr[] = "AIX Data";
242251Slclee static char OS2str[] = "OS/2 Boot";
243251Slclee static char WINstr[] = "Win95 FAT32";
244251Slclee static char EWINstr[] = "Ext Win95";
245251Slclee static char FAT95str[] = "FAT16 LBA";
246251Slclee static char EXTLstr[] = "EXT LBA";
247251Slclee static char LINUXstr[] = "Linux";
248251Slclee static char CPMstr[] = "CP/M";
24910021SSheshadri.Vasudevan@Sun.COM static char NOV2str[] = "Netware 286";
250251Slclee static char NOVstr[] = "Netware 3.x+";
251251Slclee static char QNXstr[] = "QNX 4.x";
252251Slclee static char QNX2str[] = "QNX part 2";
253251Slclee static char QNX3str[] = "QNX part 3";
254251Slclee static char LINNATstr[] = "Linux native";
255*12505SShidokht.Yadegari@Sun.COM #ifdef i386
25610682SSheshadri.Vasudevan@Sun.COM static char LINSWAPstr[] = "Linux swap";
257*12505SShidokht.Yadegari@Sun.COM #endif
258251Slclee static char NTFSVOL1str[] = "NT volset 1";
259251Slclee static char NTFSVOL2str[] = "NT volset 2";
260251Slclee static char BSDstr[] = "BSD OS";
261251Slclee static char NEXTSTEPstr[] = "NeXTSTEP";
262251Slclee static char BSDIFSstr[] = "BSDI FS";
263251Slclee static char BSDISWAPstr[] = "BSDI swap";
264251Slclee static char Actvstr[] = "Active";
265251Slclee static char EFIstr[] = "EFI";
266251Slclee static char NAstr[] = "      ";
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /* All the user options and flags */
269251Slclee static char *Dfltdev;			/* name of fixed disk drive */
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate /* Diagnostic options */
272251Slclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
273251Slclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
274251Slclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
275251Slclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
276251Slclee static int	io_lgeom = 0;		/* get label geometry (-g) */
277251Slclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
278251Slclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
279251Slclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2820Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
2836549Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
2846549Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate /* Partition table flags */
287251Slclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
288251Slclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
289251Slclee static int	io_fdisk = 0;		/* do fdisk operation */
290251Slclee static int	io_ifdisk = 0;		/* interactive partition */
291251Slclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
292251Slclee 
293251Slclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
294251Slclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
295251Slclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
296251Slclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
297251Slclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
298251Slclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
299251Slclee 
300251Slclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
301251Slclee 
302251Slclee static struct mboot BootCod;		/* buffer for master boot record */
303251Slclee 
304251Slclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
305251Slclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
306251Slclee static int	io_debug = 0;		/* activate verbose mode (-d) */
307251Slclee static int	io_image = 0;		/* create image using geometry (-I) */
308251Slclee 
309251Slclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
310251Slclee static char	*Bootsect;		/* pointer to sector zero buffer */
311251Slclee static char	*Nullsect;
3127563SPrasad.Singamsetty@Sun.COM static struct extvtoc	disk_vtoc;	/* verify VTOC table */
313251Slclee static int	vt_inval = 0;
314251Slclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
315251Slclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
316251Slclee 
317251Slclee static struct ipart	Table[FD_NUMPART];
318251Slclee static struct ipart	Old_Table[FD_NUMPART];
3198904SBarry.Harding@Sun.COM static int		skip_verify[FD_NUMPART]; /* special case skip sz chk */
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /* Disk geometry information */
3225169Slclee static struct dk_minfo	minfo;
323251Slclee static struct dk_geom	disk_geom;
324251Slclee 
3257563SPrasad.Singamsetty@Sun.COM static int Dev;			/* fd for open device */
3267563SPrasad.Singamsetty@Sun.COM 
3275169Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
3287563SPrasad.Singamsetty@Sun.COM static diskaddr_t	chs_capacity;	/* Numcyl_usable * heads * sectors */
3297563SPrasad.Singamsetty@Sun.COM 
3307563SPrasad.Singamsetty@Sun.COM static int		Numcyl_usable;	/* Number of usable cylinders */
3317563SPrasad.Singamsetty@Sun.COM 					/*  used to limit fdisk to 2TB */
3327563SPrasad.Singamsetty@Sun.COM 
3330Sstevel@tonic-gate /* Physical geometry for the drive */
334251Slclee static int	Numcyl;			/* number of cylinders */
335251Slclee static int	heads;			/* number of heads */
336251Slclee static int	sectors;		/* number of sectors per track */
337251Slclee static int	acyl;			/* number of alternate sectors */
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
340251Slclee static int	hba_Numcyl;		/* number of cylinders */
341251Slclee static int	hba_heads;		/* number of heads */
342251Slclee static int	hba_sectors;		/* number of sectors per track */
343251Slclee 
344251Slclee static int	sectsiz;		/* sector size */
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /* Load functions for fdisk table modification */
3470Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3480Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3490Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate #define	CBUFLEN 80
352251Slclee static char s[CBUFLEN];
353251Slclee 
35410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
35510021SSheshadri.Vasudevan@Sun.COM /*
35610021SSheshadri.Vasudevan@Sun.COM  * Complete list of all the 255 partition types. Some are unknown types
35710021SSheshadri.Vasudevan@Sun.COM  * and some entries are known to be unused.
35810021SSheshadri.Vasudevan@Sun.COM  *
35910021SSheshadri.Vasudevan@Sun.COM  * Courtesy of http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
36010021SSheshadri.Vasudevan@Sun.COM  */
36110021SSheshadri.Vasudevan@Sun.COM char *fdisk_part_types[] = {
36210021SSheshadri.Vasudevan@Sun.COM 	"Empty",				/* 0 */
36310021SSheshadri.Vasudevan@Sun.COM 	"FAT12",				/* 1 */
36410021SSheshadri.Vasudevan@Sun.COM 	"XENIX /",				/* 2 */
36510021SSheshadri.Vasudevan@Sun.COM 	"XENIX /usr",				/* 3 */
36610021SSheshadri.Vasudevan@Sun.COM 	"FAT16 (Upto 32M)",			/* 4 */
36710021SSheshadri.Vasudevan@Sun.COM 	"DOS Extended",				/* 5 */
36810021SSheshadri.Vasudevan@Sun.COM 	"FAT16 (>32M, HUGEDOS)",		/* 6 */
36910021SSheshadri.Vasudevan@Sun.COM 	"IFS: NTFS",				/* 7 */
37010021SSheshadri.Vasudevan@Sun.COM 	"AIX Boot/QNX(qny)",			/* 8 */
37110021SSheshadri.Vasudevan@Sun.COM 	"AIX Data/QNX(qnz)",			/* 9 */
37210021SSheshadri.Vasudevan@Sun.COM 	"OS/2 Boot/Coherent swap",		/* 10 */
37310021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT32(Upto 2047GB)",		/* 11 */
37410021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT32(LBA)",			/* 12 */
37510021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 13 */
37610021SSheshadri.Vasudevan@Sun.COM 	"WIN95 FAT16(LBA)",			/* 14 */
37710021SSheshadri.Vasudevan@Sun.COM 	"WIN95 Extended(LBA)",			/* 15 */
37810021SSheshadri.Vasudevan@Sun.COM 	"OPUS",					/* 16 */
37910021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT12",				/* 17 */
38010021SSheshadri.Vasudevan@Sun.COM 	"Diagnostic",				/* 18 */
38110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 19 */
38210021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(Upto 32M)",		/* 20 */
38310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 21 */
38410021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(>=32M)",			/* 22 */
38510021SSheshadri.Vasudevan@Sun.COM 	"Hidden IFS: HPFS",			/* 23 */
38610021SSheshadri.Vasudevan@Sun.COM 	"AST SmartSleep Partition",		/* 24 */
38710021SSheshadri.Vasudevan@Sun.COM 	"Unused/Willowtech Photon",		/* 25 */
38810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 26 */
38910021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT32",				/* 27 */
39010021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT32(LBA)",			/* 28 */
39110021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 29 */
39210021SSheshadri.Vasudevan@Sun.COM 	"Hidden FAT16(LBA)",			/* 30 */
39310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 31 */
39410021SSheshadri.Vasudevan@Sun.COM 	"Unused/OSF1",				/* 32 */
39510021SSheshadri.Vasudevan@Sun.COM 	"Reserved/FSo2(Oxygen FS)",		/* 33 */
39610021SSheshadri.Vasudevan@Sun.COM 	"Unused/(Oxygen EXT)",			/* 34 */
39710021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 35 */
39810021SSheshadri.Vasudevan@Sun.COM 	"NEC DOS 3.x",				/* 36 */
39910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 37 */
40010021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 38 */
40110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 39 */
40210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 40 */
40310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 41 */
40410021SSheshadri.Vasudevan@Sun.COM 	"AtheOS File System",			/* 42 */
40510021SSheshadri.Vasudevan@Sun.COM 	"SyllableSecure",			/* 43 */
40610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 44 */
40710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 45 */
40810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 46 */
40910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 47 */
41010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 48 */
41110021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 49 */
41210021SSheshadri.Vasudevan@Sun.COM 	"NOS",					/* 50 */
41310021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 51 */
41410021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 52 */
41510021SSheshadri.Vasudevan@Sun.COM 	"JFS on OS/2",				/* 53 */
41610021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 54 */
41710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 55 */
41810021SSheshadri.Vasudevan@Sun.COM 	"THEOS 3.2 2GB",			/* 56 */
41910021SSheshadri.Vasudevan@Sun.COM 	"Plan9/THEOS 4",			/* 57 */
42010021SSheshadri.Vasudevan@Sun.COM 	"THEOS 4 4GB",				/* 58 */
42110021SSheshadri.Vasudevan@Sun.COM 	"THEOS 4 Extended",			/* 59 */
42210021SSheshadri.Vasudevan@Sun.COM 	"PartitionMagic Recovery",		/* 60 */
42310021SSheshadri.Vasudevan@Sun.COM 	"Hidden NetWare",			/* 61 */
42410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 62 */
42510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 63 */
42610021SSheshadri.Vasudevan@Sun.COM 	"Venix 80286",				/* 64 */
42710021SSheshadri.Vasudevan@Sun.COM 	"MINIX/PPC PReP Boot",			/* 65 */
42810021SSheshadri.Vasudevan@Sun.COM 	"Win2K Dynamic Disk/SFS(DOS)",		/* 66 */
42910021SSheshadri.Vasudevan@Sun.COM 	"Linux+DRDOS shared",			/* 67 */
43010021SSheshadri.Vasudevan@Sun.COM 	"GoBack partition",			/* 68 */
43110021SSheshadri.Vasudevan@Sun.COM 	"Boot-US boot manager",			/* 69 */
43210021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 70 */
43310021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 71 */
43410021SSheshadri.Vasudevan@Sun.COM 	"EUMEL/Elan",				/* 72 */
43510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 73 */
43610021SSheshadri.Vasudevan@Sun.COM 	"ALFS/THIN FS for DOS",			/* 74 */
43710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 75 */
43810021SSheshadri.Vasudevan@Sun.COM 	"Oberon partition",			/* 76 */
43910021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x",				/* 77 */
44010021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x 2nd Part",			/* 78 */
44110021SSheshadri.Vasudevan@Sun.COM 	"QNX 4,x 3rd Part",			/* 79 */
44210021SSheshadri.Vasudevan@Sun.COM 	"OnTrack DM R/O, Lynx RTOS",		/* 80 */
44310021SSheshadri.Vasudevan@Sun.COM 	"OnTrack DM R/W, Novell",		/* 81 */
44410021SSheshadri.Vasudevan@Sun.COM 	"CP/M",					/* 82 */
44510021SSheshadri.Vasudevan@Sun.COM 	"Disk Manager 6.0 Aux3",		/* 83 */
44610021SSheshadri.Vasudevan@Sun.COM 	"Disk Manager 6.0 DDO",			/* 84 */
44710021SSheshadri.Vasudevan@Sun.COM 	"EZ-Drive",				/* 85 */
44810021SSheshadri.Vasudevan@Sun.COM 	"Golden Bow VFeature/AT&T MS-DOS",	/* 86 */
44910021SSheshadri.Vasudevan@Sun.COM 	"DrivePro",				/* 87 */
45010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 88 */
45110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 89 */
45210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 90 */
45310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 91 */
45410021SSheshadri.Vasudevan@Sun.COM 	"Priam EDisk",				/* 92 */
45510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 93 */
45610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 94 */
45710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 95 */
45810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 96 */
45910021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 97 */
46010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 98 */
46110021SSheshadri.Vasudevan@Sun.COM 	"Unix SysV, Mach, GNU Hurd",		/* 99 */
46210021SSheshadri.Vasudevan@Sun.COM 	"PC-ARMOUR, Netware 286",		/* 100 */
46310021SSheshadri.Vasudevan@Sun.COM 	"Netware 386",				/* 101 */
46410021SSheshadri.Vasudevan@Sun.COM 	"Netware SMS",				/* 102 */
46510021SSheshadri.Vasudevan@Sun.COM 	"Novell",				/* 103 */
46610021SSheshadri.Vasudevan@Sun.COM 	"Novell",				/* 104 */
46710021SSheshadri.Vasudevan@Sun.COM 	"Netware NSS",				/* 105 */
46810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 106 */
46910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 107 */
47010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 108 */
47110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 109 */
47210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 110 */
47310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 111 */
47410021SSheshadri.Vasudevan@Sun.COM 	"DiskSecure Multi-Boot",		/* 112 */
47510021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 113 */
47610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 114 */
47710021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 115 */
47810021SSheshadri.Vasudevan@Sun.COM 	"Scramdisk partition",			/* 116 */
47910021SSheshadri.Vasudevan@Sun.COM 	"IBM PC/IX",				/* 117 */
48010021SSheshadri.Vasudevan@Sun.COM 	"Reserved",				/* 118 */
48110021SSheshadri.Vasudevan@Sun.COM 	"M2FS/M2CS,Netware VNDI",		/* 119 */
48210021SSheshadri.Vasudevan@Sun.COM 	"XOSL FS",				/* 120 */
48310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 121 */
48410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 122 */
48510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 123 */
48610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 124 */
48710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 125 */
48810021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 126 */
48910021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 127 */
49010021SSheshadri.Vasudevan@Sun.COM 	"MINIX until 1.4a",			/* 128 */
49110021SSheshadri.Vasudevan@Sun.COM 	"MINIX since 1.4b, early Linux",	/* 129 */
49210021SSheshadri.Vasudevan@Sun.COM 	"Solaris/Linux swap",			/* 130 */
49310021SSheshadri.Vasudevan@Sun.COM 	"Linux native",				/* 131 */
49410021SSheshadri.Vasudevan@Sun.COM 	"OS/2 hidden,Win Hibernation",		/* 132 */
49510021SSheshadri.Vasudevan@Sun.COM 	"Linux extended",			/* 133 */
49610021SSheshadri.Vasudevan@Sun.COM 	"Old Linux RAID,NT FAT16 RAID",		/* 134 */
49710021SSheshadri.Vasudevan@Sun.COM 	"NTFS volume set",			/* 135 */
49810021SSheshadri.Vasudevan@Sun.COM 	"Linux plaintext part table",		/* 136 */
49910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 137 */
50010021SSheshadri.Vasudevan@Sun.COM 	"Linux Kernel Partition",		/* 138 */
50110021SSheshadri.Vasudevan@Sun.COM 	"Fault Tolerant FAT32 volume",		/* 139 */
50210021SSheshadri.Vasudevan@Sun.COM 	"Fault Tolerant FAT32 volume",		/* 140 */
50310021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT12",		/* 141 */
50410021SSheshadri.Vasudevan@Sun.COM 	"Linux LVM partition",			/* 142 */
50510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 143 */
50610021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT16",		/* 144 */
50710021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden DOS EXT",		/* 145 */
50810021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT16 Large",	/* 146 */
50910021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux native, Amoeba",		/* 147 */
51010021SSheshadri.Vasudevan@Sun.COM 	"Amoeba Bad Block Table",		/* 148 */
51110021SSheshadri.Vasudevan@Sun.COM 	"MIT EXOPC Native",			/* 149 */
51210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 150 */
51310021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden PDOS FAT32",		/* 151 */
51410021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT32 LBA",		/* 152 */
51510021SSheshadri.Vasudevan@Sun.COM 	"DCE376 logical drive",			/* 153 */
51610021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden FAT16 LBA",		/* 154 */
51710021SSheshadri.Vasudevan@Sun.COM 	"Free FDISK hidden DOS EXT",		/* 155 */
51810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 156 */
51910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 157 */
52010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 158 */
52110021SSheshadri.Vasudevan@Sun.COM 	"BSD/OS",				/* 159 */
52210021SSheshadri.Vasudevan@Sun.COM 	"Laptop hibernation",			/* 160 */
52310021SSheshadri.Vasudevan@Sun.COM 	"Laptop hibernate,HP SpeedStor",	/* 161 */
52410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 162 */
52510021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 163 */
52610021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 164 */
52710021SSheshadri.Vasudevan@Sun.COM 	"BSD/386,386BSD,NetBSD,FreeBSD",	/* 165 */
52810021SSheshadri.Vasudevan@Sun.COM 	"OpenBSD,HP SpeedStor",			/* 166 */
52910021SSheshadri.Vasudevan@Sun.COM 	"NeXTStep",				/* 167 */
53010021SSheshadri.Vasudevan@Sun.COM 	"Mac OS-X",				/* 168 */
53110021SSheshadri.Vasudevan@Sun.COM 	"NetBSD",				/* 169 */
53210021SSheshadri.Vasudevan@Sun.COM 	"Olivetti FAT12 1.44MB Service",	/* 170 */
53310021SSheshadri.Vasudevan@Sun.COM 	"Mac OS-X Boot",			/* 171 */
53410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 172 */
53510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 173 */
53610021SSheshadri.Vasudevan@Sun.COM 	"ShagOS filesystem",			/* 174 */
53710021SSheshadri.Vasudevan@Sun.COM 	"ShagOS swap",				/* 175 */
53810021SSheshadri.Vasudevan@Sun.COM 	"BootStar Dummy",			/* 176 */
53910021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 177 */
54010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 178 */
54110021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 179 */
54210021SSheshadri.Vasudevan@Sun.COM 	"HP SpeedStor",				/* 180 */
54310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 181 */
54410021SSheshadri.Vasudevan@Sun.COM 	"Corrupted FAT16 NT Mirror Set",	/* 182 */
54510021SSheshadri.Vasudevan@Sun.COM 	"Corrupted NTFS NT Mirror Set",		/* 183 */
54610021SSheshadri.Vasudevan@Sun.COM 	"Old BSDI BSD/386 swap",		/* 184 */
54710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 185 */
54810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 186 */
54910021SSheshadri.Vasudevan@Sun.COM 	"Boot Wizard hidden",			/* 187 */
55010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 188 */
55110021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 189 */
55210021SSheshadri.Vasudevan@Sun.COM 	"Solaris x86 boot",			/* 190 */
55310021SSheshadri.Vasudevan@Sun.COM 	"Solaris2",				/* 191 */
55410021SSheshadri.Vasudevan@Sun.COM 	"REAL/32 or Novell DOS secured",	/* 192 */
55510021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(FAT12)",			/* 193 */
55610021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux",				/* 194 */
55710021SSheshadri.Vasudevan@Sun.COM 	"Hidden Linux swap",			/* 195 */
55810021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(FAT16,< 32M)",		/* 196 */
55910021SSheshadri.Vasudevan@Sun.COM 	"DRDOS/secured(Extended)",		/* 197 */
56010021SSheshadri.Vasudevan@Sun.COM 	"NT corrupted FAT16 volume",		/* 198 */
56110021SSheshadri.Vasudevan@Sun.COM 	"NT corrupted NTFS volume",		/* 199 */
56210021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 200 */
56310021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 201 */
56410021SSheshadri.Vasudevan@Sun.COM 	"DRDOS8.0+",				/* 202 */
56510021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secured FAT32(CHS)",	/* 203 */
56610021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secured FAT32(LBA)",	/* 204 */
56710021SSheshadri.Vasudevan@Sun.COM 	"CTOS Memdump",				/* 205 */
56810021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ FAT16X(LBA)",		/* 206 */
56910021SSheshadri.Vasudevan@Sun.COM 	"DRDOS7.04+ secure EXT DOS(LBA)",	/* 207 */
57010021SSheshadri.Vasudevan@Sun.COM 	"REAL/32 secure big, MDOS",		/* 208 */
57110021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT12",		/* 209 */
57210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 210 */
57310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 211 */
57410021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT16 <32M",		/* 212 */
57510021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure EXT",			/* 213 */
57610021SSheshadri.Vasudevan@Sun.COM 	"Old MDOS secure FAT16 >=32M",		/* 214 */
57710021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 215 */
57810021SSheshadri.Vasudevan@Sun.COM 	"CP/M-86",				/* 216 */
57910021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 217 */
58010021SSheshadri.Vasudevan@Sun.COM 	"Non-FS Data",				/* 218 */
58110021SSheshadri.Vasudevan@Sun.COM 	"CP/M,Concurrent DOS,CTOS",		/* 219 */
58210021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 220 */
58310021SSheshadri.Vasudevan@Sun.COM 	"Hidden CTOS memdump",			/* 221 */
58410021SSheshadri.Vasudevan@Sun.COM 	"Dell PowerEdge utilities(FAT)",	/* 222 */
58510021SSheshadri.Vasudevan@Sun.COM 	"DG/UX virtual disk manager",		/* 223 */
58610021SSheshadri.Vasudevan@Sun.COM 	"ST AVFS(STMicroelectronics)",		/* 224 */
58710021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor 12-bit FAT EXT",		/* 225 */
58810021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 226 */
58910021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 227 */
59010021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor 16-bit FAT EXT",		/* 228 */
59110021SSheshadri.Vasudevan@Sun.COM 	"Tandy MSDOS",				/* 229 */
59210021SSheshadri.Vasudevan@Sun.COM 	"Storage Dimensions SpeedStor",		/* 230 */
59310021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 231 */
59410021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 232 */
59510021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 233 */
59610021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 234 */
59710021SSheshadri.Vasudevan@Sun.COM 	"BeOS BFS",				/* 235 */
59810021SSheshadri.Vasudevan@Sun.COM 	"SkyOS SkyFS",				/* 236 */
59910021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 237 */
60010021SSheshadri.Vasudevan@Sun.COM 	"EFI Header Indicator",			/* 238 */
60110021SSheshadri.Vasudevan@Sun.COM 	"EFI Filesystem",			/* 239 */
60210021SSheshadri.Vasudevan@Sun.COM 	"Linux/PA-RISC boot loader",		/* 240 */
60310021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 241 */
60410021SSheshadri.Vasudevan@Sun.COM 	"DOS 3.3+ secondary",			/* 242 */
60510021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor Reserved",			/* 243 */
60610021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor Large",			/* 244 */
60710021SSheshadri.Vasudevan@Sun.COM 	"Prologue multi-volume",		/* 245 */
60810021SSheshadri.Vasudevan@Sun.COM 	"SpeedStor",				/* 246 */
60910021SSheshadri.Vasudevan@Sun.COM 	"Unused",				/* 247 */
61010021SSheshadri.Vasudevan@Sun.COM 	"Unknown",				/* 248 */
61110021SSheshadri.Vasudevan@Sun.COM 	"pCache",				/* 249 */
61210021SSheshadri.Vasudevan@Sun.COM 	"Bochs",				/* 250 */
61310021SSheshadri.Vasudevan@Sun.COM 	"VMware File System",			/* 251 */
61410021SSheshadri.Vasudevan@Sun.COM 	"VMware swap",				/* 252 */
61510021SSheshadri.Vasudevan@Sun.COM 	"Linux raid autodetect",		/* 253 */
61610021SSheshadri.Vasudevan@Sun.COM 	"NT Disk Administrator hidden",		/* 254 */
61710021SSheshadri.Vasudevan@Sun.COM 	"Xenix Bad Block Table"			/* 255 */
61810021SSheshadri.Vasudevan@Sun.COM };
61910021SSheshadri.Vasudevan@Sun.COM 
62010021SSheshadri.Vasudevan@Sun.COM /* Allowed extended partition menu options */
62110021SSheshadri.Vasudevan@Sun.COM static char ext_part_menu_opts[] = "adhipr";
62210021SSheshadri.Vasudevan@Sun.COM 
62310021SSheshadri.Vasudevan@Sun.COM /*
62410021SSheshadri.Vasudevan@Sun.COM  * Structure holding all information about the extended partition
62510021SSheshadri.Vasudevan@Sun.COM  * NOTE : As of now, there will be just one instance of ext_part_t, since most
62610021SSheshadri.Vasudevan@Sun.COM  * known systems allow only one extended dos partition per disk.
62710021SSheshadri.Vasudevan@Sun.COM  */
62810021SSheshadri.Vasudevan@Sun.COM static ext_part_t *epp;
62910021SSheshadri.Vasudevan@Sun.COM #endif
63010021SSheshadri.Vasudevan@Sun.COM 
631251Slclee static void update_disk_and_exit(boolean_t table_changed);
632251Slclee int main(int argc, char *argv[]);
633251Slclee static int read_geom(char *sgeom);
634251Slclee static void dev_mboot_read(void);
6356549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz);
636251Slclee static void mboot_read(void);
637251Slclee static void fill_patt(void);
638251Slclee static void abs_read(void);
639251Slclee static void abs_write(void);
640251Slclee static void load(int funct, char *file);
641251Slclee static void Set_Table_CHS_Values(int ti);
64211382SShidokht.Yadegari@Sun.COM static int nopartdefined();
643251Slclee static int insert_tbl(int id, int act,
644251Slclee     int bhead, int bsect, int bcyl,
645251Slclee     int ehead, int esect, int ecyl,
64611382SShidokht.Yadegari@Sun.COM     uint32_t rsect, uint32_t numsect, int startindex);
6478904SBarry.Harding@Sun.COM static int entry_from_old_table(int id, int act,
6488904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
6498904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
65011382SShidokht.Yadegari@Sun.COM     uint32_t rsect, uint32_t numsect, int startindex);
651251Slclee static int verify_tbl(void);
652251Slclee static int pars_fdisk(char *line,
653251Slclee     int *id, int *act,
654251Slclee     int *bhead, int *bsect, int *bcyl,
655251Slclee     int *ehead, int *esect, int *ecyl,
6567563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect);
6577563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect);
658251Slclee static void stage0(void);
659251Slclee static int pcreate(void);
660251Slclee static int specify(uchar_t tsystid);
661251Slclee static void dispmenu(void);
662251Slclee static int pchange(void);
663251Slclee static int ppartid(void);
664251Slclee static char pdelete(void);
665251Slclee static void rm_blanks(char *s);
666251Slclee static int getcyl(void);
667251Slclee static void disptbl(void);
668251Slclee static void print_Table(void);
669251Slclee static void copy_Table_to_Old_Table(void);
670251Slclee static void nulltbl(void);
671251Slclee static void copy_Bootblk_to_Table(void);
672251Slclee static void fill_ipart(char *bootptr, struct ipart *partp);
673251Slclee #ifdef sparc
674251Slclee uchar_t getbyte(char **bp);
675251Slclee uint32_t getlong(char **bp);
676251Slclee #endif
677251Slclee static void copy_Table_to_Bootblk(void);
678251Slclee static int TableChanged(void);
679251Slclee static void ffile_write(char *file);
680251Slclee static void fix_slice(void);
681251Slclee static int yesno(void);
682251Slclee static int readvtoc(void);
683251Slclee static int writevtoc(void);
684251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
685251Slclee static int clear_efi(void);
686251Slclee static void clear_vtoc(int table, int part);
687251Slclee static int lecture_and_query(char *warning, char *devname);
688251Slclee static void sanity_check_provided_device(char *devname, int fd);
689251Slclee static char *get_node(char *devname);
6900Sstevel@tonic-gate 
69110021SSheshadri.Vasudevan@Sun.COM #ifdef i386
69210021SSheshadri.Vasudevan@Sun.COM static void id_to_name(uchar_t sysid, char *buffer);
69310021SSheshadri.Vasudevan@Sun.COM static void ext_read_input(char *buf);
69410021SSheshadri.Vasudevan@Sun.COM static int ext_read_options(char *buf);
69510021SSheshadri.Vasudevan@Sun.COM static int ext_invalid_option(char ch);
69610021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_num(int *pno);
69710021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_id(uchar_t *partid);
69810021SSheshadri.Vasudevan@Sun.COM static int ext_read_valid_partition_start(uint32_t *begsec);
69910021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec);
70010021SSheshadri.Vasudevan@Sun.COM static void ext_part_menu();
70110021SSheshadri.Vasudevan@Sun.COM static void add_logical_drive();
70210021SSheshadri.Vasudevan@Sun.COM static void delete_logical_drive();
70310021SSheshadri.Vasudevan@Sun.COM static void ext_print_help_menu();
70410021SSheshadri.Vasudevan@Sun.COM static void ext_change_logical_drive_id();
70510021SSheshadri.Vasudevan@Sun.COM static void ext_print_part_types();
70610021SSheshadri.Vasudevan@Sun.COM static void ext_print_logical_drive_layout();
70710021SSheshadri.Vasudevan@Sun.COM static void preach_and_continue();
70810021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
70910021SSheshadri.Vasudevan@Sun.COM static void ext_print_logdrive_layout_debug();
71010021SSheshadri.Vasudevan@Sun.COM #endif	/* DEBUG */
71110021SSheshadri.Vasudevan@Sun.COM #endif	/* i386 */
71210021SSheshadri.Vasudevan@Sun.COM 
71310021SSheshadri.Vasudevan@Sun.COM /*
71410021SSheshadri.Vasudevan@Sun.COM  * This function is called only during the non-interactive mode.
71510021SSheshadri.Vasudevan@Sun.COM  * It is touchy and does not tolerate any errors. If there are
71610021SSheshadri.Vasudevan@Sun.COM  * mounted logical drives, changes to the partition table
71710021SSheshadri.Vasudevan@Sun.COM  * is disallowed.
71810021SSheshadri.Vasudevan@Sun.COM  */
7190Sstevel@tonic-gate static void
update_disk_and_exit(boolean_t table_changed)7200Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
7210Sstevel@tonic-gate {
72210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
72310021SSheshadri.Vasudevan@Sun.COM 	int rval;
72410021SSheshadri.Vasudevan@Sun.COM #endif
7250Sstevel@tonic-gate 	if (table_changed) {
7260Sstevel@tonic-gate 		/*
7270Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
7280Sstevel@tonic-gate 		 * and write it to disk
7290Sstevel@tonic-gate 		 */
7300Sstevel@tonic-gate 		copy_Table_to_Bootblk();
7310Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
7350Sstevel@tonic-gate 	if (io_adjt)
7360Sstevel@tonic-gate 		fix_slice();
7370Sstevel@tonic-gate 
73810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
73910021SSheshadri.Vasudevan@Sun.COM 	if (!io_readonly) {
74010021SSheshadri.Vasudevan@Sun.COM 		rval = fdisk_commit_ext_part(epp);
74110021SSheshadri.Vasudevan@Sun.COM 		switch (rval) {
74210021SSheshadri.Vasudevan@Sun.COM 			case FDISK_SUCCESS:
74310021SSheshadri.Vasudevan@Sun.COM 				/* Success */
74410021SSheshadri.Vasudevan@Sun.COM 				break;
74510021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOEXTPART:
74610021SSheshadri.Vasudevan@Sun.COM 				/* Nothing to do */
74710021SSheshadri.Vasudevan@Sun.COM 				break;
74810021SSheshadri.Vasudevan@Sun.COM 			default:
749*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr, "Error in"
75010021SSheshadri.Vasudevan@Sun.COM 				    " fdisk_commit_ext_part\n");
75110021SSheshadri.Vasudevan@Sun.COM 				exit(rval);
75210021SSheshadri.Vasudevan@Sun.COM 		}
75310021SSheshadri.Vasudevan@Sun.COM 	}
75410021SSheshadri.Vasudevan@Sun.COM 	libfdisk_fini(&epp);
75510021SSheshadri.Vasudevan@Sun.COM #endif
7560Sstevel@tonic-gate 	exit(0);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * main
7610Sstevel@tonic-gate  * Process command-line options.
7620Sstevel@tonic-gate  */
763251Slclee int
main(int argc,char * argv[])7640Sstevel@tonic-gate main(int argc, char *argv[])
7650Sstevel@tonic-gate {
766251Slclee 	int c, i;
7670Sstevel@tonic-gate 	extern	int optind;
7680Sstevel@tonic-gate 	extern	char *optarg;
7690Sstevel@tonic-gate 	int	errflg = 0;
7700Sstevel@tonic-gate 	int	diag_cnt = 0;
7710Sstevel@tonic-gate 	int openmode;
77210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
77310021SSheshadri.Vasudevan@Sun.COM 	int rval;
77410021SSheshadri.Vasudevan@Sun.COM 	int lf_op_flag = 0;
77510021SSheshadri.Vasudevan@Sun.COM #endif
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
7780Sstevel@tonic-gate 	setbuf(stdout, 0);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	/* Process the options. */
7810Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
7820Sstevel@tonic-gate 	    != EOF) {
7830Sstevel@tonic-gate 		switch (c) {
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 			case 'o':
7866549Sbharding 				io_offset = (off_t)strtoull(optarg, 0, 0);
7870Sstevel@tonic-gate 				continue;
7880Sstevel@tonic-gate 			case 's':
7896549Sbharding 				io_size = (off_t)strtoull(optarg, 0, 0);
7900Sstevel@tonic-gate 				continue;
7910Sstevel@tonic-gate 			case 'P':
7920Sstevel@tonic-gate 				diag_cnt++;
7930Sstevel@tonic-gate 				io_patt++;
7940Sstevel@tonic-gate 				io_fatt = optarg;
7950Sstevel@tonic-gate 				continue;
7960Sstevel@tonic-gate 			case 'w':
7970Sstevel@tonic-gate 				diag_cnt++;
7980Sstevel@tonic-gate 				io_wrt++;
7990Sstevel@tonic-gate 				continue;
8000Sstevel@tonic-gate 			case 'r':
8010Sstevel@tonic-gate 				diag_cnt++;
8020Sstevel@tonic-gate 				io_rd++;
8030Sstevel@tonic-gate 				continue;
8040Sstevel@tonic-gate 			case 'd':
8050Sstevel@tonic-gate 				io_debug++;
8060Sstevel@tonic-gate 				continue;
8070Sstevel@tonic-gate 			case 'I':
8080Sstevel@tonic-gate 				io_image++;
8090Sstevel@tonic-gate 				continue;
8100Sstevel@tonic-gate 			case 'R':
8110Sstevel@tonic-gate 				io_readonly++;
8120Sstevel@tonic-gate 				continue;
8130Sstevel@tonic-gate 			case 'S':
8140Sstevel@tonic-gate 				diag_cnt++;
8150Sstevel@tonic-gate 				io_sgeom = optarg;
8160Sstevel@tonic-gate 				continue;
8170Sstevel@tonic-gate 			case 'T':
8180Sstevel@tonic-gate 				io_ADJT++;
819251Slclee 				/* FALLTHRU */
8200Sstevel@tonic-gate 			case 't':
8210Sstevel@tonic-gate 				io_adjt++;
8220Sstevel@tonic-gate 				continue;
8230Sstevel@tonic-gate 			case 'B':
8240Sstevel@tonic-gate 				io_wholedisk++;
8250Sstevel@tonic-gate 				io_fdisk++;
8260Sstevel@tonic-gate 				continue;
8270Sstevel@tonic-gate 			case 'E':
8280Sstevel@tonic-gate 				io_EFIdisk++;
8290Sstevel@tonic-gate 				io_fdisk++;
8300Sstevel@tonic-gate 				continue;
8310Sstevel@tonic-gate 			case 'g':
8320Sstevel@tonic-gate 				diag_cnt++;
8330Sstevel@tonic-gate 				io_lgeom++;
8340Sstevel@tonic-gate 				continue;
8350Sstevel@tonic-gate 			case 'G':
8360Sstevel@tonic-gate 				diag_cnt++;
8370Sstevel@tonic-gate 				io_pgeom++;
8380Sstevel@tonic-gate 				continue;
8390Sstevel@tonic-gate 			case 'n':
8400Sstevel@tonic-gate 				io_nifdisk++;
8410Sstevel@tonic-gate 				io_fdisk++;
8420Sstevel@tonic-gate 				continue;
8430Sstevel@tonic-gate 			case 'F':
8440Sstevel@tonic-gate 				io_fdisk++;
8450Sstevel@tonic-gate 				io_ffdisk = optarg;
8460Sstevel@tonic-gate 				continue;
8470Sstevel@tonic-gate 			case 'b':
8480Sstevel@tonic-gate 				io_mboot = optarg;
8490Sstevel@tonic-gate 				continue;
8500Sstevel@tonic-gate 			case 'W':
8510Sstevel@tonic-gate 				/*
8520Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
8530Sstevel@tonic-gate 				 * to standard output, otherwise write
8540Sstevel@tonic-gate 				 * to the specified file.
8550Sstevel@tonic-gate 				 */
8560Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
8570Sstevel@tonic-gate 					stdo_flag = 1;
8580Sstevel@tonic-gate 				else
8590Sstevel@tonic-gate 					io_Wfdisk = optarg;
8600Sstevel@tonic-gate 				io_fdisk++;
8610Sstevel@tonic-gate 				continue;
8620Sstevel@tonic-gate 			case 'A':
8630Sstevel@tonic-gate 				io_fdisk++;
8640Sstevel@tonic-gate 				io_Afdisk = optarg;
8650Sstevel@tonic-gate 				continue;
8660Sstevel@tonic-gate 			case 'D':
8670Sstevel@tonic-gate 				io_fdisk++;
8680Sstevel@tonic-gate 				io_Dfdisk = optarg;
8690Sstevel@tonic-gate 				continue;
8700Sstevel@tonic-gate 			case 'h':
871251Slclee 				(void) fprintf(stderr, "%s\n", Usage);
872251Slclee 				(void) fprintf(stderr, "%s\n", Usage1);
8730Sstevel@tonic-gate 				exit(0);
874251Slclee 				/* FALLTHRU */
8750Sstevel@tonic-gate 			case 'v':
8760Sstevel@tonic-gate 				v_flag = 1;
8770Sstevel@tonic-gate 				continue;
8780Sstevel@tonic-gate 			case '?':
8790Sstevel@tonic-gate 				errflg++;
8800Sstevel@tonic-gate 				break;
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 		break;
8830Sstevel@tonic-gate 	}
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
8860Sstevel@tonic-gate 		diag_cnt = 0;
8870Sstevel@tonic-gate 	}
8880Sstevel@tonic-gate 
8890Sstevel@tonic-gate 	/* User option checking */
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 	/* By default, run in interactive mode */
8920Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
8930Sstevel@tonic-gate 		io_ifdisk++;
8940Sstevel@tonic-gate 		io_fdisk++;
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
8970Sstevel@tonic-gate 		errflg++;
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	/* Was any error detected? */
9010Sstevel@tonic-gate 	if (errflg || argc == optind) {
902251Slclee 		(void) fprintf(stderr, "%s\n", Usage);
903251Slclee 		(void) fprintf(stderr,
9040Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
9050Sstevel@tonic-gate 		exit(2);
9060Sstevel@tonic-gate 	}
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 	/* Figure out the correct device node to open */
9100Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	if (io_readonly)
9130Sstevel@tonic-gate 		openmode = O_RDONLY;
9140Sstevel@tonic-gate 	else
9150Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
918251Slclee 		(void) fprintf(stderr,
919251Slclee 		    "fdisk: Cannot open device %s.\n",
920251Slclee 		    Dfltdev);
9210Sstevel@tonic-gate 		exit(1);
9220Sstevel@tonic-gate 	}
9235169Slclee 	/*
9245169Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
9255169Slclee 	 * in that case leave the minfo structure zeroed
9265169Slclee 	 */
9275169Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
9289889SLarry.Liu@Sun.COM 		(void) memset(&minfo, 0, sizeof (minfo));
9295169Slclee 	}
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate 	/* Get the disk geometry */
9320Sstevel@tonic-gate 	if (!io_image) {
9330Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
9340Sstevel@tonic-gate 		errno = 0;
9350Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 			/*
9380Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
9390Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
9400Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
9410Sstevel@tonic-gate 			 */
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 			if (errno == ENOTTY) {
9440Sstevel@tonic-gate 				v_flag = 0;
9450Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
9460Sstevel@tonic-gate 			} else if (errno == EINVAL) {
9470Sstevel@tonic-gate 				/*
9480Sstevel@tonic-gate 				 * This means that the ioctl exists, but
9490Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
9500Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
9510Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
9520Sstevel@tonic-gate 				 */
9530Sstevel@tonic-gate 				v_flag = 0;
9540Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
9550Sstevel@tonic-gate 			} else {
9560Sstevel@tonic-gate 				(void) fprintf(stderr,
9570Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
9580Sstevel@tonic-gate 				    argv[optind]);
9590Sstevel@tonic-gate 				exit(1);
9600Sstevel@tonic-gate 			}
9610Sstevel@tonic-gate 		} else {
9620Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
9630Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
9640Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
9650Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		errno = 0;
9690Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
9700Sstevel@tonic-gate 			if (errno == ENOTTY) {
9710Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
9720Sstevel@tonic-gate 			} else {
9730Sstevel@tonic-gate 				(void) fprintf(stderr,
9740Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
9750Sstevel@tonic-gate 				    argv[optind]);
9760Sstevel@tonic-gate 				exit(1);
9770Sstevel@tonic-gate 			}
9780Sstevel@tonic-gate 
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 		/*
9810Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
9820Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
9830Sstevel@tonic-gate 		 */
9840Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
9850Sstevel@tonic-gate 			errno = 0;
9860Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
9870Sstevel@tonic-gate 				(void) fprintf(stderr,
9880Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
9890Sstevel@tonic-gate 				    argv[optind]);
9900Sstevel@tonic-gate 				exit(1);
9910Sstevel@tonic-gate 			}
9920Sstevel@tonic-gate 		}
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
9950Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
9960Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
9979889SLarry.Liu@Sun.COM 
9989889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
9999889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
10009889SLarry.Liu@Sun.COM 		else
10019889SLarry.Liu@Sun.COM 			sectsiz = 512;
10029889SLarry.Liu@Sun.COM 
10030Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		/*
10060Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
10070Sstevel@tonic-gate 		 * or we got an invalid hba geometry
10080Sstevel@tonic-gate 		 * then set hba geometry based on max values
10090Sstevel@tonic-gate 		 */
10100Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
1011251Slclee 		    disk_geom.dkg_ncyl == 0 ||
1012251Slclee 		    disk_geom.dkg_nhead == 0 ||
1013251Slclee 		    disk_geom.dkg_nsect == 0 ||
10140Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
10150Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
10160Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate 			/*
10190Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
10200Sstevel@tonic-gate 			 */
10210Sstevel@tonic-gate 			v_flag = 0;
10220Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
10230Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
10240Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
10250Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
10260Sstevel@tonic-gate 		}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 		if (io_debug) {
1029251Slclee 			(void) fprintf(stderr, "Physical Geometry:\n");
1030251Slclee 			(void) fprintf(stderr,
10310Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
10320Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
10330Sstevel@tonic-gate 			    Numcyl,
10340Sstevel@tonic-gate 			    heads,
10350Sstevel@tonic-gate 			    sectors,
10360Sstevel@tonic-gate 			    sectsiz,
1037251Slclee 			    Numcyl * heads * sectors,
1038251Slclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
1039251Slclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
1040251Slclee 			(void) fprintf(stderr,
10410Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
10420Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
10430Sstevel@tonic-gate 			    hba_Numcyl,
10440Sstevel@tonic-gate 			    hba_heads,
10450Sstevel@tonic-gate 			    hba_sectors,
10460Sstevel@tonic-gate 			    sectsiz,
1047251Slclee 			    hba_Numcyl * hba_heads * hba_sectors,
1048251Slclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
1049251Slclee 			    1048576);
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 	}
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
10540Sstevel@tonic-gate 	if (io_lgeom) {
10550Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
10560Sstevel@tonic-gate 			(void) fprintf(stderr,
10570Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
10580Sstevel@tonic-gate 			    argv[optind]);
10590Sstevel@tonic-gate 			exit(1);
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
10620Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
10630Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
10649889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
10659889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
10669889SLarry.Liu@Sun.COM 		else
10679889SLarry.Liu@Sun.COM 			sectsiz = 512;
10689889SLarry.Liu@Sun.COM 
10690Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
1070251Slclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
1071251Slclee 		(void) printf(
1072251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
10730Sstevel@tonic-gate 		    " SECSIZ\n");
1074251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
10750Sstevel@tonic-gate 		    Numcyl,
10760Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
10770Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
10780Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
10790Sstevel@tonic-gate 		    heads,
10800Sstevel@tonic-gate 		    sectors,
10810Sstevel@tonic-gate 		    sectsiz);
10820Sstevel@tonic-gate 		exit(0);
10830Sstevel@tonic-gate 	} else if (io_pgeom) {
10840Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
10850Sstevel@tonic-gate 			(void) fprintf(stderr,
10860Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
10870Sstevel@tonic-gate 			    argv[optind]);
10880Sstevel@tonic-gate 			exit(1);
10890Sstevel@tonic-gate 		}
1090251Slclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
1091251Slclee 		(void) printf(
1092251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
10930Sstevel@tonic-gate 		    " SECSIZ\n");
1094251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
10950Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
10960Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
10970Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
10980Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
10990Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
11000Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
11010Sstevel@tonic-gate 		    sectsiz);
11020Sstevel@tonic-gate 		exit(0);
11030Sstevel@tonic-gate 	} else if (io_sgeom) {
11040Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
11050Sstevel@tonic-gate 			exit(1);
11060Sstevel@tonic-gate 		} else if (!io_image) {
11070Sstevel@tonic-gate 			exit(0);
11080Sstevel@tonic-gate 		}
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 
11115169Slclee 	/*
11125169Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
11135169Slclee 	 * in that case use CHS
11145169Slclee 	 */
11157563SPrasad.Singamsetty@Sun.COM 	chs_capacity = (diskaddr_t)Numcyl * heads * sectors;
11165169Slclee 	dev_capacity = chs_capacity;
11177563SPrasad.Singamsetty@Sun.COM 	Numcyl_usable = Numcyl;
11187563SPrasad.Singamsetty@Sun.COM 
11197563SPrasad.Singamsetty@Sun.COM 	if (chs_capacity > DK_MAX_2TB) {
11207563SPrasad.Singamsetty@Sun.COM 		/* limit to 2TB */
11217563SPrasad.Singamsetty@Sun.COM 		Numcyl_usable = DK_MAX_2TB / (heads * sectors);
11227563SPrasad.Singamsetty@Sun.COM 		chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors;
11237563SPrasad.Singamsetty@Sun.COM 	}
11247563SPrasad.Singamsetty@Sun.COM 
11255169Slclee 	if (minfo.dki_capacity > 0)
11265169Slclee 		dev_capacity = minfo.dki_capacity;
11275169Slclee 
11280Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
11299889SLarry.Liu@Sun.COM 	Bootsect = (char *)calloc(3 * sectsiz, 1);
11300Sstevel@tonic-gate 	if (Bootsect == NULL) {
1131251Slclee 		(void) fprintf(stderr,
11320Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
11330Sstevel@tonic-gate 		    " (%d bytes).\n",
1134251Slclee 		    3 * sectsiz);
11350Sstevel@tonic-gate 		exit(1);
11360Sstevel@tonic-gate 	}
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
11390Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
11400Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
11410Sstevel@tonic-gate 		Nullsect[i] = 0;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	/* Find out what the user wants done */
11450Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
11460Sstevel@tonic-gate 		abs_read();	/* will not return */
11470Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
11480Sstevel@tonic-gate 		abs_write();	/* will not return */
11490Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
11500Sstevel@tonic-gate 		fill_patt();	/* will not return */
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
11590Sstevel@tonic-gate 	mboot_read();
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	/* Read from disk master boot */
11620Sstevel@tonic-gate 	dev_mboot_read();
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate 	/*
11650Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
11660Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
11670Sstevel@tonic-gate 	 */
11680Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
11690Sstevel@tonic-gate 	copy_Bootblk_to_Table();
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
11720Sstevel@tonic-gate 	copy_Table_to_Old_Table();
11730Sstevel@tonic-gate 
117410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
117510021SSheshadri.Vasudevan@Sun.COM 	/*
117610021SSheshadri.Vasudevan@Sun.COM 	 * Read extended partition only when the fdisk table is not
117710021SSheshadri.Vasudevan@Sun.COM 	 * supplied from a file
117810021SSheshadri.Vasudevan@Sun.COM 	 */
117910021SSheshadri.Vasudevan@Sun.COM 	if (!io_ffdisk) {
118010021SSheshadri.Vasudevan@Sun.COM 		lf_op_flag |= FDISK_READ_DISK;
118110021SSheshadri.Vasudevan@Sun.COM 	}
118210021SSheshadri.Vasudevan@Sun.COM 	if ((rval = libfdisk_init(&epp, Dfltdev, &Table[0], lf_op_flag))
118310021SSheshadri.Vasudevan@Sun.COM 	    != FDISK_SUCCESS) {
118410021SSheshadri.Vasudevan@Sun.COM 		switch (rval) {
118510021SSheshadri.Vasudevan@Sun.COM 			/*
118611246SSharath.Srinivasan@Sun.COM 			 * FDISK_EBADLOGDRIVE, FDISK_ENOLOGDRIVE and
118711246SSharath.Srinivasan@Sun.COM 			 * FDISK_EBADMAGIC can be considered as
118811246SSharath.Srinivasan@Sun.COM 			 * soft errors and hence we do not exit
118910021SSheshadri.Vasudevan@Sun.COM 			 */
119010021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EBADLOGDRIVE:
119110021SSheshadri.Vasudevan@Sun.COM 				break;
119210021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLOGDRIVE:
119310021SSheshadri.Vasudevan@Sun.COM 				break;
119411246SSharath.Srinivasan@Sun.COM 			case FDISK_EBADMAGIC:
119511246SSharath.Srinivasan@Sun.COM 				break;
119610021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOVGEOM:
1197*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr, "Could not get virtual"
119810021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
119910021SSheshadri.Vasudevan@Sun.COM 				exit(1);
120010021SSheshadri.Vasudevan@Sun.COM 				break;
120110021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOPGEOM:
1202*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr, "Could not get physical"
120310021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
120410021SSheshadri.Vasudevan@Sun.COM 				exit(1);
120510021SSheshadri.Vasudevan@Sun.COM 				break;
120610021SSheshadri.Vasudevan@Sun.COM 			case FDISK_ENOLGEOM:
1207*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr, "Could not get label"
120810021SSheshadri.Vasudevan@Sun.COM 				    " geometry for this device\n");
120910021SSheshadri.Vasudevan@Sun.COM 				exit(1);
121010021SSheshadri.Vasudevan@Sun.COM 				break;
121110021SSheshadri.Vasudevan@Sun.COM 			default:
121210021SSheshadri.Vasudevan@Sun.COM 				perror("Failed to initialise libfdisk.\n");
121310021SSheshadri.Vasudevan@Sun.COM 				exit(1);
121410021SSheshadri.Vasudevan@Sun.COM 				break;
121510021SSheshadri.Vasudevan@Sun.COM 		}
121610021SSheshadri.Vasudevan@Sun.COM 	}
121710021SSheshadri.Vasudevan@Sun.COM #endif
121810021SSheshadri.Vasudevan@Sun.COM 
12190Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
12200Sstevel@tonic-gate 	if (io_ffdisk) {
12210Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
12220Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
12260Sstevel@tonic-gate 	if (io_Dfdisk) {
12270Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
12280Sstevel@tonic-gate 	}
12290Sstevel@tonic-gate 	if (io_Afdisk) {
12300Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
12310Sstevel@tonic-gate 	}
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
12340Sstevel@tonic-gate 		/* Check if there is no fdisk table */
123511382SShidokht.Yadegari@Sun.COM 		if (nopartdefined() || io_wholedisk || io_EFIdisk) {
12360Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
1237251Slclee 				(void) printf(
1238251Slclee 				    "No fdisk table exists. The default"
1239251Slclee 				    " partition for the disk is:\n\n"
1240251Slclee 				    "  a 100%% \"SOLARIS System\" "
1241251Slclee 				    "partition\n\n"
1242251Slclee 				    "Type \"y\" to accept the default "
12430Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
12440Sstevel@tonic-gate 				    "edit the\n partition table.\n");
12457563SPrasad.Singamsetty@Sun.COM 
12467563SPrasad.Singamsetty@Sun.COM 				if (Numcyl > Numcyl_usable)
12477563SPrasad.Singamsetty@Sun.COM 					(void) printf("WARNING: Disk is larger"
12487563SPrasad.Singamsetty@Sun.COM 					    " than 2TB. Solaris partition will"
12497563SPrasad.Singamsetty@Sun.COM 					    " be limited to 2 TB.\n");
12500Sstevel@tonic-gate 			}
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 			/* Edit the partition table as directed */
12530Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 				/* Default scenario */
12560Sstevel@tonic-gate 				nulltbl();
12570Sstevel@tonic-gate 				/* now set up UNIX System partition */
12580Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
125910021SSheshadri.Vasudevan@Sun.COM 				Table[0].relsect = LE_32(heads * sectors);
12607563SPrasad.Singamsetty@Sun.COM 
12617563SPrasad.Singamsetty@Sun.COM 				Table[0].numsect =
126210021SSheshadri.Vasudevan@Sun.COM 				    LE_32((ulong_t)((Numcyl_usable - 1) *
12630Sstevel@tonic-gate 				    heads * sectors));
12647563SPrasad.Singamsetty@Sun.COM 
12650Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
12680Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
12690Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12700Sstevel@tonic-gate 			} else if (io_EFIdisk) {
12710Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
12720Sstevel@tonic-gate 				nulltbl();
12730Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
12747563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
127511382SShidokht.Yadegari@Sun.COM 				    (dev_capacity - 1), 0);
12760Sstevel@tonic-gate 				if (i != 0) {
1277251Slclee 					(void) fprintf(stderr,
1278251Slclee 					    "Error creating EFI partition\n");
12790Sstevel@tonic-gate 					exit(1);
12800Sstevel@tonic-gate 				}
12810Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
12820Sstevel@tonic-gate 			}
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 	}
12850Sstevel@tonic-gate 
12860Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
12870Sstevel@tonic-gate 	if (io_debug) {
1288251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
12890Sstevel@tonic-gate 		print_Table();
12900Sstevel@tonic-gate 		if (io_ifdisk) {
1291251Slclee 			(void) fprintf(stderr, "\n");
1292251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
12939786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
12940Sstevel@tonic-gate 		}
12950Sstevel@tonic-gate 	}
12960Sstevel@tonic-gate 
12970Sstevel@tonic-gate 	/* Interactive fdisk mode */
12980Sstevel@tonic-gate 	if (io_ifdisk) {
1299251Slclee 		(void) printf(CLR_SCR);
13000Sstevel@tonic-gate 		disptbl();
1301251Slclee 		for (;;) {
1302251Slclee 			stage0();
13030Sstevel@tonic-gate 			copy_Bootblk_to_Table();
13040Sstevel@tonic-gate 			disptbl();
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
13090Sstevel@tonic-gate 	if (io_Wfdisk)
13100Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
13110Sstevel@tonic-gate 	else if (stdo_flag)
13120Sstevel@tonic-gate 		ffile_write((char *)stdout);
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
1315251Slclee 	return (0);
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate /*
13190Sstevel@tonic-gate  * read_geom
13200Sstevel@tonic-gate  * Read geometry from specified file (-S).
13210Sstevel@tonic-gate  */
13220Sstevel@tonic-gate 
1323251Slclee static int
read_geom(char * sgeom)1324251Slclee read_geom(char *sgeom)
13250Sstevel@tonic-gate {
13260Sstevel@tonic-gate 	char	line[256];
13270Sstevel@tonic-gate 	FILE *fp;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 	/* open the prototype file */
13300Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
13310Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
13320Sstevel@tonic-gate 		    io_sgeom);
13330Sstevel@tonic-gate 		return (1);
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	/* Read a line from the file */
13370Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
13380Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
13390Sstevel@tonic-gate 			continue;
13400Sstevel@tonic-gate 		else {
13410Sstevel@tonic-gate 			line[strlen(line)] = '\0';
1342251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
13430Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
13440Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
13450Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
13460Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
13470Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
13480Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
13490Sstevel@tonic-gate 			    &sectsiz) != 7) {
13500Sstevel@tonic-gate 				(void) fprintf(stderr,
13510Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
13520Sstevel@tonic-gate 				    line);
13530Sstevel@tonic-gate 				return (1);
13540Sstevel@tonic-gate 			}
13550Sstevel@tonic-gate 			break;
13560Sstevel@tonic-gate 		} /* else */
13570Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	if (!io_image) {
13600Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
13610Sstevel@tonic-gate 			(void) fprintf(stderr,
13620Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
13630Sstevel@tonic-gate 			return (1);
13640Sstevel@tonic-gate 		}
13650Sstevel@tonic-gate 	} else {
13660Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
13670Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
13680Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
13690Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
13700Sstevel@tonic-gate 	}
13710Sstevel@tonic-gate 
1372251Slclee 	(void) fclose(fp);
13730Sstevel@tonic-gate 	return (0);
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate 
13760Sstevel@tonic-gate /*
13770Sstevel@tonic-gate  * dev_mboot_read
13780Sstevel@tonic-gate  * Read the master boot sector from the device.
13790Sstevel@tonic-gate  */
1380251Slclee static void
dev_mboot_read(void)1381251Slclee dev_mboot_read(void)
13820Sstevel@tonic-gate {
13830Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
13840Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 	if (errno == 0)
13870Sstevel@tonic-gate 		return;
13880Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
1389251Slclee 		(void) fprintf(stderr,
13900Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
13910Sstevel@tonic-gate 		    Dfltdev);
13920Sstevel@tonic-gate 		if (!io_image)
13930Sstevel@tonic-gate 			exit(1);
13940Sstevel@tonic-gate 	}
13950Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1396251Slclee 		(void) fprintf(stderr,
13970Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
13980Sstevel@tonic-gate 		    Dfltdev);
13990Sstevel@tonic-gate 		if (!io_image)
14000Sstevel@tonic-gate 			exit(1);
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate /*
14050Sstevel@tonic-gate  * dev_mboot_write
14060Sstevel@tonic-gate  * Write the master boot sector to the device.
14070Sstevel@tonic-gate  */
1408251Slclee static void
dev_mboot_write(off_t sect,char * buff,int bootsiz)14096549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
14100Sstevel@tonic-gate {
14110Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
14126478Sbharding 	int	clr_efi = -1;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	if (io_readonly)
1415251Slclee 		return;
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate 	if (io_debug) {
1418251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
14190Sstevel@tonic-gate 		print_Table();
14200Sstevel@tonic-gate 		if (io_ifdisk) {
1421251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
14229786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
14230Sstevel@tonic-gate 		}
14240Sstevel@tonic-gate 	}
14250Sstevel@tonic-gate 
14266478Sbharding 	/*
14276478Sbharding 	 * If the new table has any Solaris partitions and the old
14286478Sbharding 	 * table does not have an entry that describes it
14296478Sbharding 	 * exactly then clear the old vtoc (if any).
14306478Sbharding 	 */
14310Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14320Sstevel@tonic-gate 
14336478Sbharding 		/* We only care about potential Solaris parts. */
14340Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
14350Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
14360Sstevel@tonic-gate 			continue;
14370Sstevel@tonic-gate 
14386478Sbharding 		/* Does the old table have an exact entry for the new entry? */
14396478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14406478Sbharding 
14416478Sbharding 			/* We only care about old Solaris partitions. */
14426478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
14436478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
14446478Sbharding 
14456478Sbharding 				/* Is this old one the same as a new one? */
14466478Sbharding 				if ((Old_Table[old_pt].relsect ==
14476478Sbharding 				    Table[new_pt].relsect) &&
14486478Sbharding 				    (Old_Table[old_pt].numsect ==
14496478Sbharding 				    Table[new_pt].numsect))
14506478Sbharding 					break; /* Yes */
14516478Sbharding 			}
14526478Sbharding 		}
14536478Sbharding 
14546478Sbharding 		/* Did a solaris partition change location or size? */
14556478Sbharding 		if (old_pt >= FD_NUMPART) {
14566478Sbharding 			/* Yes clear old vtoc */
14576478Sbharding 			if (io_debug) {
14586478Sbharding 				(void) fprintf(stderr,
14596478Sbharding 				    "Clearing VTOC labels from NEW"
14606478Sbharding 				    " table\n");
14616478Sbharding 			}
14626478Sbharding 			clear_vtoc(NEW, new_pt);
14636478Sbharding 		}
14646478Sbharding 	}
14656478Sbharding 
14666478Sbharding 
14676478Sbharding 	/* see if the old table had EFI */
14686478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14696478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
14706478Sbharding 			clr_efi = old_pt;
14710Sstevel@tonic-gate 		}
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
14750Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
14760Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
14770Sstevel@tonic-gate 			continue;
14780Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
14795169Slclee 			if ((Old_Table[old_pt].systid ==
14805169Slclee 			    Table[new_pt].systid) &&
14815169Slclee 			    (Old_Table[old_pt].relsect ==
14825169Slclee 			    Table[new_pt].relsect) &&
14835169Slclee 			    (Old_Table[old_pt].numsect ==
14845169Slclee 			    Table[new_pt].numsect))
14855169Slclee 				break;
14860Sstevel@tonic-gate 		}
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 		/*
14890Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
14900Sstevel@tonic-gate 		 * the EFI GPT
14910Sstevel@tonic-gate 		 */
14920Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
14930Sstevel@tonic-gate 			clr_efi = 0;
14940Sstevel@tonic-gate 		}
14950Sstevel@tonic-gate 		break;
14960Sstevel@tonic-gate 	}
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	/* clear labels if necessary */
14990Sstevel@tonic-gate 	if (clr_efi >= 0) {
15000Sstevel@tonic-gate 		if (io_debug) {
1501251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
15040Sstevel@tonic-gate 			if (io_debug) {
1505251Slclee 				(void) fprintf(stderr,
1506251Slclee 				    "\tError %d clearing EFI labels"
15070Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
15080Sstevel@tonic-gate 				    error);
15090Sstevel@tonic-gate 			}
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 	}
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1514251Slclee 		(void) fprintf(stderr,
15150Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
15160Sstevel@tonic-gate 		    Dfltdev);
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate 	if (errno == 0)
15190Sstevel@tonic-gate 		return;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate 	/* write to disk drive */
15220Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1523251Slclee 		(void) fprintf(stderr,
15240Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
15250Sstevel@tonic-gate 		    Dfltdev);
15260Sstevel@tonic-gate 		exit(1);
15270Sstevel@tonic-gate 	}
15280Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1529251Slclee 		(void) fprintf(stderr,
15300Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
15310Sstevel@tonic-gate 		    Dfltdev);
15320Sstevel@tonic-gate 		exit(1);
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate }
15350Sstevel@tonic-gate 
15360Sstevel@tonic-gate /*
15370Sstevel@tonic-gate  * mboot_read
15380Sstevel@tonic-gate  * Read the prototype boot records from the files.
15390Sstevel@tonic-gate  */
1540251Slclee static void
mboot_read(void)1541251Slclee mboot_read(void)
15420Sstevel@tonic-gate {
15430Sstevel@tonic-gate 	int mDev, i;
15440Sstevel@tonic-gate 	struct ipart *part;
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate #if defined(i386) || defined(sparc)
15470Sstevel@tonic-gate 	/*
15480Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
15490Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
15500Sstevel@tonic-gate 	 */
15510Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
15520Sstevel@tonic-gate 		/*
15530Sstevel@tonic-gate 		 * Bug ID 1249035:
15540Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
15550Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
15560Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
15570Sstevel@tonic-gate 		 */
15580Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	/* First read in the master boot record */
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	/* Open the master boot proto file */
15640Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1565251Slclee 		(void) fprintf(stderr,
15660Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
15670Sstevel@tonic-gate 		    io_mboot);
15680Sstevel@tonic-gate 		exit(1);
15690Sstevel@tonic-gate 	}
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	/* Read the master boot program */
15720Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
15730Sstevel@tonic-gate 	    (struct mboot)) {
1574251Slclee 		(void) fprintf(stderr,
15750Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
15760Sstevel@tonic-gate 		    io_mboot);
15770Sstevel@tonic-gate 		exit(1);
15780Sstevel@tonic-gate 	}
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	/* Is this really a master boot record? */
158110021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(BootCod.signature) != MBB_MAGIC) {
1582251Slclee 		(void) fprintf(stderr,
15830Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1584251Slclee 		(void) fprintf(stderr,
1585251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
158610021SSheshadri.Vasudevan@Sun.COM 		    LE_16(BootCod.signature), MBB_MAGIC);
15870Sstevel@tonic-gate 		exit(1);
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
1590251Slclee 	(void) close(mDev);
15910Sstevel@tonic-gate #else
15920Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
15930Sstevel@tonic-gate #endif
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
15960Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
15970Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1598251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
15990Sstevel@tonic-gate 	}
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate }
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate /*
16040Sstevel@tonic-gate  * fill_patt
16050Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
16060Sstevel@tonic-gate  */
1607251Slclee static void
fill_patt(void)1608251Slclee fill_patt(void)
16090Sstevel@tonic-gate {
1610251Slclee 	int	*buff_ptr, i;
16116549Sbharding 	off_t	*off_ptr;
16120Sstevel@tonic-gate 	int	io_fpatt = 0;
16130Sstevel@tonic-gate 	int	io_ipatt = 0;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
16160Sstevel@tonic-gate 		io_fpatt++;
16170Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
16180Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
16190Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
16205169Slclee 			*buff_ptr = io_ipatt;
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	/*
16240Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
16250Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
16260Sstevel@tonic-gate 	 * for io_size blocks.
16270Sstevel@tonic-gate 	 */
16280Sstevel@tonic-gate 	while (io_size--) {
16296549Sbharding 		off_ptr = (off_t *)Bootsect;
16300Sstevel@tonic-gate 		if (!io_fpatt) {
16316549Sbharding 			for (i = 0; i < sectsiz;
16326549Sbharding 			    i += sizeof (off_t), off_ptr++)
16336549Sbharding 				*off_ptr = io_offset;
16340Sstevel@tonic-gate 		}
16350Sstevel@tonic-gate 		/* Write the data to disk */
16366549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16376549Sbharding 		    SEEK_SET) == -1) {
1638251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16395169Slclee 			    Dfltdev);
16400Sstevel@tonic-gate 			exit(1);
16410Sstevel@tonic-gate 		}
16420Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1643251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
16445169Slclee 			    Dfltdev);
16450Sstevel@tonic-gate 			exit(1);
16460Sstevel@tonic-gate 		}
16470Sstevel@tonic-gate 	} /* while (--io_size); */
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate /*
16510Sstevel@tonic-gate  * abs_read
16520Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
16530Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
16540Sstevel@tonic-gate  */
1655251Slclee static void
abs_read(void)1656251Slclee abs_read(void)
1657251Slclee {
16580Sstevel@tonic-gate 	int c;
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	while (io_size--) {
16616549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
16626549Sbharding 		    SEEK_SET) == -1) {
1663251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
16640Sstevel@tonic-gate 			    Dfltdev);
16650Sstevel@tonic-gate 			exit(1);
16660Sstevel@tonic-gate 		}
16670Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1668251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
16690Sstevel@tonic-gate 			    Dfltdev);
16700Sstevel@tonic-gate 			exit(1);
16710Sstevel@tonic-gate 		}
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 		/* Write to standard ouptut */
1674251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
16750Sstevel@tonic-gate 			if (c >= 0) {
16760Sstevel@tonic-gate 				if (io_debug)
1677251Slclee 					(void) fprintf(stderr,
1678251Slclee 					    "fdisk: Output warning: %d of %d"
1679251Slclee 					    " characters written.\n",
1680251Slclee 					    c, sectsiz);
16810Sstevel@tonic-gate 				exit(2);
16820Sstevel@tonic-gate 			} else {
16830Sstevel@tonic-gate 				perror("write error on output file.");
16840Sstevel@tonic-gate 				exit(2);
16850Sstevel@tonic-gate 			}
16860Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
16870Sstevel@tonic-gate 			/* != sectsiz) */
16880Sstevel@tonic-gate 	} /* while (--io_size); */
16890Sstevel@tonic-gate 	exit(0);
16900Sstevel@tonic-gate }
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate /*
16930Sstevel@tonic-gate  * abs_write
16940Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
16950Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
16960Sstevel@tonic-gate  */
1697251Slclee static void
abs_write(void)1698251Slclee abs_write(void)
16990Sstevel@tonic-gate {
17000Sstevel@tonic-gate 	int c, i;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	while (io_size--) {
17030Sstevel@tonic-gate 		int part_exit = 0;
17040Sstevel@tonic-gate 		/* Read from standard input */
17050Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
17060Sstevel@tonic-gate 			if (c >= 0) {
17070Sstevel@tonic-gate 				if (io_debug)
1708251Slclee 				(void) fprintf(stderr,
17090Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
17100Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
17115169Slclee 				    c, sectsiz);
17120Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
17130Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
17140Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
17150Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
17160Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
17170Sstevel@tonic-gate 					Bootsect[i++] = 0;
17180Sstevel@tonic-gate 				}
17190Sstevel@tonic-gate 				part_exit++;
17200Sstevel@tonic-gate 			} else {
17210Sstevel@tonic-gate 				perror("read error on input file.");
17220Sstevel@tonic-gate 				exit(2);
17230Sstevel@tonic-gate 			}
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate 		/* Write to disk drive */
17276549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
17286549Sbharding 		    SEEK_SET) == -1) {
1729251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
17300Sstevel@tonic-gate 			    Dfltdev);
17310Sstevel@tonic-gate 			exit(1);
17320Sstevel@tonic-gate 		}
17330Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1734251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
17350Sstevel@tonic-gate 			    Dfltdev);
17360Sstevel@tonic-gate 			exit(1);
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		if (part_exit)
17390Sstevel@tonic-gate 		exit(0);
17400Sstevel@tonic-gate 	} /* while (--io_size); */
17410Sstevel@tonic-gate 	exit(1);
17420Sstevel@tonic-gate }
17430Sstevel@tonic-gate 
1744251Slclee 
17450Sstevel@tonic-gate /*
17460Sstevel@tonic-gate  * load
17470Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
17480Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
17490Sstevel@tonic-gate  */
17500Sstevel@tonic-gate 
1751251Slclee static void
load(int funct,char * file)1752251Slclee load(int funct, char *file)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	int	id;
17550Sstevel@tonic-gate 	int	act;
17560Sstevel@tonic-gate 	int	bhead;
17570Sstevel@tonic-gate 	int	bsect;
17580Sstevel@tonic-gate 	int	bcyl;
17590Sstevel@tonic-gate 	int	ehead;
17600Sstevel@tonic-gate 	int	esect;
17610Sstevel@tonic-gate 	int	ecyl;
17627563SPrasad.Singamsetty@Sun.COM 	uint32_t	rsect;
17637563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
17640Sstevel@tonic-gate 	char	line[256];
17650Sstevel@tonic-gate 	int	i = 0;
17660Sstevel@tonic-gate 	FILE *fp;
176711382SShidokht.Yadegari@Sun.COM 	int	startindex = 0;
176811382SShidokht.Yadegari@Sun.COM 	int	tmpindex = 0;
176910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
177010021SSheshadri.Vasudevan@Sun.COM 	int 	ext_part_present = 0;
177110021SSheshadri.Vasudevan@Sun.COM 	uint32_t	begsec, endsec, relsect;
177210021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
177310021SSheshadri.Vasudevan@Sun.COM 	int part_count = 0, ldcnt = 0;
1774*12505SShidokht.Yadegari@Sun.COM 	uint32_t ext_beg_sec;
177510021SSheshadri.Vasudevan@Sun.COM 	uint32_t old_ext_beg_sec = 0, old_ext_num_sec = 0;
177610021SSheshadri.Vasudevan@Sun.COM 	uint32_t new_ext_beg_sec = 0, new_ext_num_sec = 0;
177710021SSheshadri.Vasudevan@Sun.COM 	int ext_part_inited = 0;
177810021SSheshadri.Vasudevan@Sun.COM 	uchar_t	systid;
177910021SSheshadri.Vasudevan@Sun.COM #endif
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	switch (funct) {
17820Sstevel@tonic-gate 
17835169Slclee 	case LOADFILE:
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 		/*
17860Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
17870Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
17880Sstevel@tonic-gate 		 * fdisk_file).
17890Sstevel@tonic-gate 		 */
17900Sstevel@tonic-gate 		nulltbl();
17910Sstevel@tonic-gate 
17920Sstevel@tonic-gate 		/* Open the prototype file */
17930Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
17940Sstevel@tonic-gate 			(void) fprintf(stderr,
17950Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
17960Sstevel@tonic-gate 			    file);
17970Sstevel@tonic-gate 			exit(1);
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		/* Read a line from the file */
18010Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
18020Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
18030Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
18040Sstevel@tonic-gate 				continue;
18050Sstevel@tonic-gate 			}
180610021SSheshadri.Vasudevan@Sun.COM #ifdef i386
180710021SSheshadri.Vasudevan@Sun.COM 			part_count++;
180810021SSheshadri.Vasudevan@Sun.COM 
180910021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended((uchar_t)id)) {
181010021SSheshadri.Vasudevan@Sun.COM 				if (ext_part_present) {
1811*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr,
1812*12505SShidokht.Yadegari@Sun.COM 					    "Extended partition"
181310021SSheshadri.Vasudevan@Sun.COM 					    " already exists\n");
1814*12505SShidokht.Yadegari@Sun.COM 					(void) 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)) {
1840*12505SShidokht.Yadegari@Sun.COM 					(void) 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 */
1851*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr,
1852*12505SShidokht.Yadegari@Sun.COM 					    "More than 4 primary"
185310021SSheshadri.Vasudevan@Sun.COM 					    " partitions found in input\n");
1854*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr, "Exiting...\n");
185510021SSheshadri.Vasudevan@Sun.COM 					exit(1);
185610021SSheshadri.Vasudevan@Sun.COM 				}
185710021SSheshadri.Vasudevan@Sun.COM 
185810021SSheshadri.Vasudevan@Sun.COM 				if (numsect == 0) {
185910021SSheshadri.Vasudevan@Sun.COM 					continue;
186010021SSheshadri.Vasudevan@Sun.COM 				}
186110021SSheshadri.Vasudevan@Sun.COM 
186210021SSheshadri.Vasudevan@Sun.COM 				/*
186310021SSheshadri.Vasudevan@Sun.COM 				 * If the start and size of the existing
186410021SSheshadri.Vasudevan@Sun.COM 				 * extended partition matches the new one and
186510021SSheshadri.Vasudevan@Sun.COM 				 * new logical drives are being defined via
186610021SSheshadri.Vasudevan@Sun.COM 				 * the input file, initialize the EBR.
186710021SSheshadri.Vasudevan@Sun.COM 				 */
186810021SSheshadri.Vasudevan@Sun.COM 				if (!ext_part_inited) {
1869*12505SShidokht.Yadegari@Sun.COM 					(void) fdisk_init_ext_part(epp,
187010021SSheshadri.Vasudevan@Sun.COM 					    new_ext_beg_sec, new_ext_num_sec);
187110021SSheshadri.Vasudevan@Sun.COM 					ext_part_inited = 1;
187210021SSheshadri.Vasudevan@Sun.COM 				}
187310021SSheshadri.Vasudevan@Sun.COM 
187410021SSheshadri.Vasudevan@Sun.COM 				begsec = rsect - offset;
187510021SSheshadri.Vasudevan@Sun.COM 				if ((ldcnt =
187610021SSheshadri.Vasudevan@Sun.COM 				    fdisk_get_logical_drive_count(epp)) == 0) {
187710021SSheshadri.Vasudevan@Sun.COM 					/* Adding the first logical drive */
187810021SSheshadri.Vasudevan@Sun.COM 					/*
187910021SSheshadri.Vasudevan@Sun.COM 					 * Make sure that begsec doesnt wrap
188010021SSheshadri.Vasudevan@Sun.COM 					 * around. This can happen if rsect is
188110021SSheshadri.Vasudevan@Sun.COM 					 * less than offset.
188210021SSheshadri.Vasudevan@Sun.COM 					 */
188310021SSheshadri.Vasudevan@Sun.COM 					if (rsect < offset) {
1884*12505SShidokht.Yadegari@Sun.COM 						(void) fprintf(stderr,
1885*12505SShidokht.Yadegari@Sun.COM 						    "Minimum of "
188610021SSheshadri.Vasudevan@Sun.COM 						    "63 free sectors required "
188710021SSheshadri.Vasudevan@Sun.COM 						    "before the beginning of "
188810021SSheshadri.Vasudevan@Sun.COM 						    "a logical drive.");
188910021SSheshadri.Vasudevan@Sun.COM 						exit(1);
189010021SSheshadri.Vasudevan@Sun.COM 					}
189110021SSheshadri.Vasudevan@Sun.COM 					/*
189210021SSheshadri.Vasudevan@Sun.COM 					 * Check if the first logical drive
189310021SSheshadri.Vasudevan@Sun.COM 					 * is out of order. In that case, do
189410021SSheshadri.Vasudevan@Sun.COM 					 * not subtract MAX_LOGDRIVE_OFFSET
189510021SSheshadri.Vasudevan@Sun.COM 					 * from the given start of partition.
189610021SSheshadri.Vasudevan@Sun.COM 					 */
189710021SSheshadri.Vasudevan@Sun.COM 					if (begsec != new_ext_beg_sec) {
189810021SSheshadri.Vasudevan@Sun.COM 						begsec = rsect;
189910021SSheshadri.Vasudevan@Sun.COM 						offset = 0;
190010021SSheshadri.Vasudevan@Sun.COM 					}
190110021SSheshadri.Vasudevan@Sun.COM 				}
190210021SSheshadri.Vasudevan@Sun.COM 				if (ldcnt >= MAX_EXT_PARTS) {
1903*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr,
1904*12505SShidokht.Yadegari@Sun.COM 					    "\nError : Number of "
190510021SSheshadri.Vasudevan@Sun.COM 					    "logical drives exceeds limit of "
190610021SSheshadri.Vasudevan@Sun.COM 					    "%d.\n", MAX_EXT_PARTS);
190710021SSheshadri.Vasudevan@Sun.COM 					exit(1);
190810021SSheshadri.Vasudevan@Sun.COM 				}
190910021SSheshadri.Vasudevan@Sun.COM 
191010021SSheshadri.Vasudevan@Sun.COM 				if (id > FDISK_MAX_VALID_PART_ID) {
1911*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr,
1912*12505SShidokht.Yadegari@Sun.COM 					    "Invalid partition ID\n");
1913*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr, "fdisk: Error on"
191410021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
191510021SSheshadri.Vasudevan@Sun.COM 					exit(1);
191610021SSheshadri.Vasudevan@Sun.COM 				}
191710021SSheshadri.Vasudevan@Sun.COM 
191810021SSheshadri.Vasudevan@Sun.COM 				endsec = rsect + numsect - 1;
191910021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_validate_logical_drive(epp,
192010021SSheshadri.Vasudevan@Sun.COM 				    begsec, offset, numsect) == 0) {
192110021SSheshadri.Vasudevan@Sun.COM 					if (id == EFI_PMBR) {
1922*12505SShidokht.Yadegari@Sun.COM 						(void) fprintf(stderr, "EFI "
192310021SSheshadri.Vasudevan@Sun.COM 						    "partitions not supported "
192410021SSheshadri.Vasudevan@Sun.COM 						    "inside extended "
192510021SSheshadri.Vasudevan@Sun.COM 						    "partition\n");
192610021SSheshadri.Vasudevan@Sun.COM 						exit(1);
192710021SSheshadri.Vasudevan@Sun.COM 					}
192810021SSheshadri.Vasudevan@Sun.COM 					fdisk_add_logical_drive(epp, begsec,
192910021SSheshadri.Vasudevan@Sun.COM 					    endsec, id);
193010021SSheshadri.Vasudevan@Sun.COM 					continue;
193110021SSheshadri.Vasudevan@Sun.COM 				} else {
1932*12505SShidokht.Yadegari@Sun.COM 					(void) fprintf(stderr, "fdisk: Error on"
193310021SSheshadri.Vasudevan@Sun.COM 					    " entry \"%s\".\n", line);
193410021SSheshadri.Vasudevan@Sun.COM 					exit(1);
193510021SSheshadri.Vasudevan@Sun.COM 				}
193610021SSheshadri.Vasudevan@Sun.COM 			}
193710021SSheshadri.Vasudevan@Sun.COM #endif
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 			/*
19400Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
19410Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
19420Sstevel@tonic-gate 			 */
19430Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
19440Sstevel@tonic-gate 				(void) fprintf(stderr,
19450Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
19460Sstevel@tonic-gate 				    line);
19470Sstevel@tonic-gate 				exit(1);
19480Sstevel@tonic-gate 			}
19498904SBarry.Harding@Sun.COM 
195011382SShidokht.Yadegari@Sun.COM 			if ((tmpindex = entry_from_old_table(id, act, bhead,
195111382SShidokht.Yadegari@Sun.COM 			    bsect, bcyl, ehead, esect, ecyl, rsect, numsect,
195211382SShidokht.Yadegari@Sun.COM 			    startindex)) != -1) {
19538904SBarry.Harding@Sun.COM 				/*
19548904SBarry.Harding@Sun.COM 				 * If we got here it means we copied an
19558904SBarry.Harding@Sun.COM 				 * unmodified entry. So there is no need
19568904SBarry.Harding@Sun.COM 				 * to insert it in the table or do any
19578904SBarry.Harding@Sun.COM 				 * checks against disk size.
19588904SBarry.Harding@Sun.COM 				 *
19598904SBarry.Harding@Sun.COM 				 * This is a work around on the following
19608904SBarry.Harding@Sun.COM 				 * situation (for IDE disks, at least):
19618904SBarry.Harding@Sun.COM 				 * Different operation systems calculate
19628904SBarry.Harding@Sun.COM 				 * disk size different ways, of which there
19638904SBarry.Harding@Sun.COM 				 * are two main ways.
19648904SBarry.Harding@Sun.COM 				 *
19658904SBarry.Harding@Sun.COM 				 * The first, rounds the disk size to modulo
19668904SBarry.Harding@Sun.COM 				 * cylinder size (virtual made-up cylinder
19678904SBarry.Harding@Sun.COM 				 * usually based on maximum number of heads
19688904SBarry.Harding@Sun.COM 				 * and sectors in partition table fields).
19698904SBarry.Harding@Sun.COM 				 * Our OS's (for IDE) and most other "Unix"
19708904SBarry.Harding@Sun.COM 				 * type OS's do this.
19718904SBarry.Harding@Sun.COM 				 *
19728904SBarry.Harding@Sun.COM 				 * The second, uses every single block
19738904SBarry.Harding@Sun.COM 				 * on the disk (to maximize available space).
19748904SBarry.Harding@Sun.COM 				 * Since disk manufactures do not know about
19758904SBarry.Harding@Sun.COM 				 * "virtual cylinders", there are some number
19768904SBarry.Harding@Sun.COM 				 * of blocks that make up a partial cylinder
19778904SBarry.Harding@Sun.COM 				 * at the end of the disk.
19788904SBarry.Harding@Sun.COM 				 *
19798904SBarry.Harding@Sun.COM 				 * The difference between these two methods
19808904SBarry.Harding@Sun.COM 				 * is where the problem is. When one
19818904SBarry.Harding@Sun.COM 				 * tries to install Solaris/OpenSolaris on
19828904SBarry.Harding@Sun.COM 				 * a disk that has another OS using that
19838904SBarry.Harding@Sun.COM 				 * "partial cylinder", install fails. It fails
19848904SBarry.Harding@Sun.COM 				 * since fdisk thinks its asked to create a
19858904SBarry.Harding@Sun.COM 				 * partition with the -F option that contains
19868904SBarry.Harding@Sun.COM 				 * a partition that runs off the end of the
19878904SBarry.Harding@Sun.COM 				 * disk.
19888904SBarry.Harding@Sun.COM 				 */
198911382SShidokht.Yadegari@Sun.COM 				startindex = tmpindex + 1;
19908904SBarry.Harding@Sun.COM 				continue;
19918904SBarry.Harding@Sun.COM 			}
19928904SBarry.Harding@Sun.COM 
19930Sstevel@tonic-gate 			/*
19940Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
19950Sstevel@tonic-gate 			 * in table
19960Sstevel@tonic-gate 			 */
199711382SShidokht.Yadegari@Sun.COM 			if ((startindex = insert_tbl(id, act, bhead, bsect,
199811382SShidokht.Yadegari@Sun.COM 			    bcyl, ehead, esect, ecyl, rsect, numsect,
199911382SShidokht.Yadegari@Sun.COM 			    startindex)) < 0) {
20000Sstevel@tonic-gate 				(void) fprintf(stderr,
20010Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
20020Sstevel@tonic-gate 				    line);
20030Sstevel@tonic-gate 				exit(1);
20040Sstevel@tonic-gate 			}
200511382SShidokht.Yadegari@Sun.COM 			startindex++;
20060Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
20070Sstevel@tonic-gate 
20080Sstevel@tonic-gate 		if (verify_tbl() < 0) {
2009251Slclee 			(void) fprintf(stderr,
20100Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
20110Sstevel@tonic-gate 			exit(1);
20120Sstevel@tonic-gate 		}
20130Sstevel@tonic-gate 
2014251Slclee 		(void) fclose(fp);
20150Sstevel@tonic-gate 		return;
20160Sstevel@tonic-gate 
20175169Slclee 	case LOADDEL:
20180Sstevel@tonic-gate 
20190Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
2020251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
2021251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
2022251Slclee 			(void) fprintf(stderr,
2023251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2024251Slclee 			exit(1);
2025251Slclee 		}
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 		/* Find the exact entry in the table */
20280Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
20290Sstevel@tonic-gate 			if (Table[i].systid == id &&
20300Sstevel@tonic-gate 			    Table[i].bootid == act &&
20310Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
20320Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
20335169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
2034251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
20350Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
20360Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
20375169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
2038251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
203910021SSheshadri.Vasudevan@Sun.COM 			    Table[i].relsect == LE_32(rsect) &&
204010021SSheshadri.Vasudevan@Sun.COM 			    Table[i].numsect == LE_32(numsect)) {
20410Sstevel@tonic-gate 
204211382SShidokht.Yadegari@Sun.COM 				(void) memset(&Table[i], 0,
204311382SShidokht.Yadegari@Sun.COM 				    sizeof (struct ipart));
204410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
204510021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_is_dos_extended(id)) {
2046*12505SShidokht.Yadegari@Sun.COM 					(void) fdisk_delete_ext_part(epp);
204710021SSheshadri.Vasudevan@Sun.COM 				}
204810021SSheshadri.Vasudevan@Sun.COM #endif
20490Sstevel@tonic-gate 				return;
20500Sstevel@tonic-gate 			}
20510Sstevel@tonic-gate 		}
205210021SSheshadri.Vasudevan@Sun.COM 
205310021SSheshadri.Vasudevan@Sun.COM #ifdef i386
205410021SSheshadri.Vasudevan@Sun.COM 		ldcnt = FD_NUMPART + 1;
205510021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
205610021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
205710021SSheshadri.Vasudevan@Sun.COM 			relsect = temp->abs_secnum + temp->logdrive_offset;
205810021SSheshadri.Vasudevan@Sun.COM 			if (temp->parts[0].systid == id &&
205910021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].bootid == act &&
206010021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].beghead == bhead &&
206110021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begsect == ((bsect & 0x3f) |
206210021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
206310021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].begcyl == (uchar_t)(bcyl & 0xff) &&
206410021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endhead == ehead &&
206510021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endsect == ((esect & 0x3f) |
206610021SSheshadri.Vasudevan@Sun.COM 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
206710021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].endcyl == (uchar_t)(ecyl & 0xff) &&
206810021SSheshadri.Vasudevan@Sun.COM 			    relsect == LE_32(rsect) &&
206910021SSheshadri.Vasudevan@Sun.COM 			    temp->parts[0].numsect == LE_32(numsect)) {
207010021SSheshadri.Vasudevan@Sun.COM 				fdisk_delete_logical_drive(epp, ldcnt);
207110021SSheshadri.Vasudevan@Sun.COM 				return;
207210021SSheshadri.Vasudevan@Sun.COM 			}
207310021SSheshadri.Vasudevan@Sun.COM 			ldcnt++;
207410021SSheshadri.Vasudevan@Sun.COM 		}
207510021SSheshadri.Vasudevan@Sun.COM #endif
207610021SSheshadri.Vasudevan@Sun.COM 
2077251Slclee 		(void) fprintf(stderr,
20780Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
20790Sstevel@tonic-gate 		    "	\"%s\"\n",
20800Sstevel@tonic-gate 		    file);
20810Sstevel@tonic-gate 		exit(1);
20828333SSuhasini.Peddada@Sun.COM 		/* FALLTHRU */
20830Sstevel@tonic-gate 
20845169Slclee 	case LOADADD:
20850Sstevel@tonic-gate 
20860Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
2087251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
2088251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
2089251Slclee 			(void) fprintf(stderr,
2090251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
2091251Slclee 			exit(1);
2092251Slclee 		}
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
20950Sstevel@tonic-gate 		if (rsect == 0) {
20960Sstevel@tonic-gate 			(void) fprintf(stderr,
20970Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
20980Sstevel@tonic-gate 			    "   \"%s\".\n",
20990Sstevel@tonic-gate 			    file);
21000Sstevel@tonic-gate 			exit(1);
21010Sstevel@tonic-gate 		}
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 		/*
21040Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
21050Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
21060Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
21070Sstevel@tonic-gate 		 */
21080Sstevel@tonic-gate 
21090Sstevel@tonic-gate 		if (id == EFI_PMBR) {
21100Sstevel@tonic-gate 			if (rsect != 1) {
21110Sstevel@tonic-gate 				(void) fprintf(stderr,
21120Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
21130Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
21140Sstevel@tonic-gate 				exit(1);
21150Sstevel@tonic-gate 			}
21160Sstevel@tonic-gate 
21177563SPrasad.Singamsetty@Sun.COM 
21187563SPrasad.Singamsetty@Sun.COM 			if (dev_capacity > DK_MAX_2TB) {
21197563SPrasad.Singamsetty@Sun.COM 				if (numsect != DK_MAX_2TB) {
21207563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr,
21217563SPrasad.Singamsetty@Sun.COM 					    "fdisk: EFI partitions must "
21227563SPrasad.Singamsetty@Sun.COM 					    "encompass the entire maximum 2 TB "
21237563SPrasad.Singamsetty@Sun.COM 					    "(input numsect: %u - max: %llu)\n",
21247563SPrasad.Singamsetty@Sun.COM 					    numsect, (diskaddr_t)DK_MAX_2TB);
21257563SPrasad.Singamsetty@Sun.COM 				exit(1);
21267563SPrasad.Singamsetty@Sun.COM 				}
21277563SPrasad.Singamsetty@Sun.COM 			} else if (numsect != dev_capacity - 1) {
21280Sstevel@tonic-gate 				(void) fprintf(stderr,
21290Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
2130251Slclee 				    "entire disk\n"
21317563SPrasad.Singamsetty@Sun.COM 				    "(input numsect: %u - avail: %llu)\n",
2132251Slclee 				    numsect,
21335169Slclee 				    dev_capacity - 1);
21340Sstevel@tonic-gate 				exit(1);
21350Sstevel@tonic-gate 			}
21360Sstevel@tonic-gate 		}
21370Sstevel@tonic-gate 
213810021SSheshadri.Vasudevan@Sun.COM #ifdef i386
213910021SSheshadri.Vasudevan@Sun.COM 		if (id > FDISK_MAX_VALID_PART_ID) {
2140*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition ID\n");
214110021SSheshadri.Vasudevan@Sun.COM 			exit(1);
214210021SSheshadri.Vasudevan@Sun.COM 		}
214310021SSheshadri.Vasudevan@Sun.COM 
214410021SSheshadri.Vasudevan@Sun.COM 		if ((fdisk_ext_part_exists(epp)) &&
214510021SSheshadri.Vasudevan@Sun.COM 		    (fdisk_is_dos_extended(id))) {
214610021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
214710021SSheshadri.Vasudevan@Sun.COM 			    "Extended partition already exists.\n");
214810021SSheshadri.Vasudevan@Sun.COM 			(void) fprintf(stderr,
214910021SSheshadri.Vasudevan@Sun.COM 			    "fdisk: Invalid entry could not be "
215010021SSheshadri.Vasudevan@Sun.COM 			    "inserted:\n        \"%s\"\n", file);
215110021SSheshadri.Vasudevan@Sun.COM 			exit(1);
215210021SSheshadri.Vasudevan@Sun.COM 		}
215310021SSheshadri.Vasudevan@Sun.COM 
215410021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_ext_part_exists(epp) &&
215510021SSheshadri.Vasudevan@Sun.COM 		    (rsect >= (ext_beg_sec = fdisk_get_ext_beg_sec(epp))) &&
2156*12505SShidokht.Yadegari@Sun.COM 		    (rsect <= (fdisk_get_ext_end_sec(epp)))) {
215710021SSheshadri.Vasudevan@Sun.COM 			int offset = MAX_LOGDRIVE_OFFSET;
215810021SSheshadri.Vasudevan@Sun.COM 
215910021SSheshadri.Vasudevan@Sun.COM 			/*
216010021SSheshadri.Vasudevan@Sun.COM 			 * Make sure that begsec doesnt wrap around.
216110021SSheshadri.Vasudevan@Sun.COM 			 * This can happen if rsect is less than offset
216210021SSheshadri.Vasudevan@Sun.COM 			 */
216310021SSheshadri.Vasudevan@Sun.COM 			if (rsect < offset) {
216410021SSheshadri.Vasudevan@Sun.COM 				return;
216510021SSheshadri.Vasudevan@Sun.COM 			}
216610021SSheshadri.Vasudevan@Sun.COM 			begsec = rsect - offset;
216710021SSheshadri.Vasudevan@Sun.COM 			if ((ldcnt = fdisk_get_logical_drive_count(epp)) == 0) {
216810021SSheshadri.Vasudevan@Sun.COM 				/*
216910021SSheshadri.Vasudevan@Sun.COM 				 * Adding the first logical drive
217010021SSheshadri.Vasudevan@Sun.COM 				 * Check if the first logical drive
217110021SSheshadri.Vasudevan@Sun.COM 				 * is out of order. In that case, do
217210021SSheshadri.Vasudevan@Sun.COM 				 * not subtract MAX_LOGDRIVE_OFFSET
217310021SSheshadri.Vasudevan@Sun.COM 				 * from the given start of partition.
217410021SSheshadri.Vasudevan@Sun.COM 				 */
217510021SSheshadri.Vasudevan@Sun.COM 				if (begsec != ext_beg_sec) {
217610021SSheshadri.Vasudevan@Sun.COM 					begsec = rsect;
217710021SSheshadri.Vasudevan@Sun.COM 					offset = 0;
217810021SSheshadri.Vasudevan@Sun.COM 				}
217910021SSheshadri.Vasudevan@Sun.COM 			}
218010021SSheshadri.Vasudevan@Sun.COM 
218110021SSheshadri.Vasudevan@Sun.COM 			if (ldcnt >= MAX_EXT_PARTS) {
2182*12505SShidokht.Yadegari@Sun.COM 				(void) printf("\nNumber of logical drives "
2183*12505SShidokht.Yadegari@Sun.COM 				    "exceeds limit of %d.\n", MAX_EXT_PARTS);
2184*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Failing further additions.\n");
218510021SSheshadri.Vasudevan@Sun.COM 				exit(1);
218610021SSheshadri.Vasudevan@Sun.COM 			}
218710021SSheshadri.Vasudevan@Sun.COM 
218810021SSheshadri.Vasudevan@Sun.COM 			if (numsect == 0) {
218910021SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
219010021SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Partition size cannot be zero:\n"
219110021SSheshadri.Vasudevan@Sun.COM 				    "   \"%s\".\n",
219210021SSheshadri.Vasudevan@Sun.COM 				    file);
219310021SSheshadri.Vasudevan@Sun.COM 				exit(1);
219410021SSheshadri.Vasudevan@Sun.COM 			}
219510021SSheshadri.Vasudevan@Sun.COM 			endsec = rsect + numsect - 1;
219610021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_validate_logical_drive(epp, begsec,
219710021SSheshadri.Vasudevan@Sun.COM 			    offset, numsect) == 0) {
219810021SSheshadri.Vasudevan@Sun.COM 				/* Valid logical drive */
219910021SSheshadri.Vasudevan@Sun.COM 				fdisk_add_logical_drive(epp, begsec, endsec,
220010021SSheshadri.Vasudevan@Sun.COM 				    id);
220110021SSheshadri.Vasudevan@Sun.COM 				return;
220210682SSheshadri.Vasudevan@Sun.COM 			} else {
220310682SSheshadri.Vasudevan@Sun.COM 				(void) fprintf(stderr,
220410682SSheshadri.Vasudevan@Sun.COM 				    "fdisk: Invalid entry could not be "
220510682SSheshadri.Vasudevan@Sun.COM 				    "inserted:\n        \"%s\"\n", file);
220610682SSheshadri.Vasudevan@Sun.COM 				exit(1);
220710021SSheshadri.Vasudevan@Sun.COM 			}
220810021SSheshadri.Vasudevan@Sun.COM 		}
220910021SSheshadri.Vasudevan@Sun.COM #endif
221010021SSheshadri.Vasudevan@Sun.COM 
22110Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
22120Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
221311382SShidokht.Yadegari@Sun.COM 		    ecyl, rsect, numsect, 0) < 0) {
22140Sstevel@tonic-gate 			(void) fprintf(stderr,
22150Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
22160Sstevel@tonic-gate 			    "	\"%s\"\n",
22170Sstevel@tonic-gate 			    file);
22180Sstevel@tonic-gate 			exit(1);
22190Sstevel@tonic-gate 		}
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
22220Sstevel@tonic-gate 		if (verify_tbl() < 0) {
2223251Slclee 			(void) fprintf(stderr,
2224251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
22250Sstevel@tonic-gate 			exit(1);
22260Sstevel@tonic-gate 		}
22270Sstevel@tonic-gate 	} /* switch funct */
22280Sstevel@tonic-gate }
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate /*
22310Sstevel@tonic-gate  * Set_Table_CHS_Values
22320Sstevel@tonic-gate  *
22330Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
22340Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
22350Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
22360Sstevel@tonic-gate  *
22370Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
22380Sstevel@tonic-gate  *
22390Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
22400Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
22410Sstevel@tonic-gate  */
22420Sstevel@tonic-gate static void
Set_Table_CHS_Values(int ti)22430Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
22440Sstevel@tonic-gate {
22450Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
22480Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
22490Sstevel@tonic-gate 		/*
22500Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
22510Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
22520Sstevel@tonic-gate 		 */
22530Sstevel@tonic-gate 		cy = MAX_CYL + 1;
22540Sstevel@tonic-gate 		hd = MAX_HEAD;
22550Sstevel@tonic-gate 		sc = MAX_SECT;
22560Sstevel@tonic-gate 	} else {
22570Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
22580Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
22590Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
22600Sstevel@tonic-gate 	}
22610Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
2262251Slclee 	Table[ti].beghead = (uchar_t)hd;
2263251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 	/*
22660Sstevel@tonic-gate 	 * This code is identical to the code above
22670Sstevel@tonic-gate 	 * except that it works on ending CHS values
22680Sstevel@tonic-gate 	 */
22690Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
22700Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
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].endcyl = cy & 0xff;
2280251Slclee 	Table[ti].endhead = (uchar_t)hd;
2281251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
22820Sstevel@tonic-gate }
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate /*
22850Sstevel@tonic-gate  * insert_tbl
22860Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
22870Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
22880Sstevel@tonic-gate  *	entries!
22890Sstevel@tonic-gate  */
2290251Slclee static int
insert_tbl(int id,int act,int bhead,int bsect,int bcyl,int ehead,int esect,int ecyl,uint32_t rsect,uint32_t numsect,int startindex)2291251Slclee insert_tbl(
2292251Slclee     int id, int act,
2293251Slclee     int bhead, int bsect, int bcyl,
2294251Slclee     int ehead, int esect, int ecyl,
229511382SShidokht.Yadegari@Sun.COM     uint32_t rsect, uint32_t numsect, int startindex)
22960Sstevel@tonic-gate {
22970Sstevel@tonic-gate 	int	i;
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	/* validate partition size */
23007563SPrasad.Singamsetty@Sun.COM 	if (((diskaddr_t)rsect + numsect) > dev_capacity) {
2301251Slclee 		(void) fprintf(stderr,
23020Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
23030Sstevel@tonic-gate 		return (-1);
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate 
23067563SPrasad.Singamsetty@Sun.COM 
23070Sstevel@tonic-gate 	/* find UNUSED partition table entry */
230811382SShidokht.Yadegari@Sun.COM 	for (i = startindex; i < FD_NUMPART; i++) {
23090Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
23100Sstevel@tonic-gate 			break;
23110Sstevel@tonic-gate 		}
23120Sstevel@tonic-gate 	}
23130Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
2314251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
23150Sstevel@tonic-gate 		return (-1);
23160Sstevel@tonic-gate 	}
23170Sstevel@tonic-gate 
23180Sstevel@tonic-gate 
2319251Slclee 	Table[i].systid = (uchar_t)id;
2320251Slclee 	Table[i].bootid = (uchar_t)act;
232110021SSheshadri.Vasudevan@Sun.COM 	Table[i].numsect = LE_32(numsect);
232210021SSheshadri.Vasudevan@Sun.COM 	Table[i].relsect = LE_32(rsect);
23230Sstevel@tonic-gate 
232411382SShidokht.Yadegari@Sun.COM 	if (id == UNUSED) {
232511382SShidokht.Yadegari@Sun.COM 		(void) memset(&Table[i], 0, sizeof (struct ipart));
232611382SShidokht.Yadegari@Sun.COM 	} else if (0 < bsect && bsect <= MAX_SECT &&
23270Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
23280Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
23290Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
233011382SShidokht.Yadegari@Sun.COM 
233111382SShidokht.Yadegari@Sun.COM 		/*
233211382SShidokht.Yadegari@Sun.COM 		 * If we have been called with a valid geometry, use it
233311382SShidokht.Yadegari@Sun.COM 		 * valid means non-zero values that fit in the BIOS fields
233411382SShidokht.Yadegari@Sun.COM 		 */
23350Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
23360Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
23370Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
23380Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
23390Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
23400Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
2341251Slclee 		Table[i].beghead = (uchar_t)bhead;
2342251Slclee 		Table[i].endhead = (uchar_t)ehead;
2343251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
23440Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
23450Sstevel@tonic-gate 	} else {
23460Sstevel@tonic-gate 
23470Sstevel@tonic-gate 		/*
23480Sstevel@tonic-gate 		 * The specified values are invalid,
23490Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
23500Sstevel@tonic-gate 		 */
23510Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
23520Sstevel@tonic-gate 	}
23530Sstevel@tonic-gate 
23540Sstevel@tonic-gate 	/*
23550Sstevel@tonic-gate 	 * return partition index
23560Sstevel@tonic-gate 	 */
23570Sstevel@tonic-gate 	return (i);
23580Sstevel@tonic-gate }
23590Sstevel@tonic-gate 
23600Sstevel@tonic-gate /*
23618904SBarry.Harding@Sun.COM  * entry_from_old_table
23628904SBarry.Harding@Sun.COM  *	If the specified entry is in the old table and is not a Solaris entry
23638904SBarry.Harding@Sun.COM  *	then insert same entry into new fdisk table. If we do this then
23648904SBarry.Harding@Sun.COM  *	all checks are skipped for that entry!
23658904SBarry.Harding@Sun.COM  */
23668904SBarry.Harding@Sun.COM static int
entry_from_old_table(int id,int act,int bhead,int bsect,int bcyl,int ehead,int esect,int ecyl,uint32_t rsect,uint32_t numsect,int startindex)23678904SBarry.Harding@Sun.COM entry_from_old_table(
23688904SBarry.Harding@Sun.COM     int id, int act,
23698904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
23708904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
237111382SShidokht.Yadegari@Sun.COM     uint32_t rsect, uint32_t numsect, int startindex)
23728904SBarry.Harding@Sun.COM {
23738904SBarry.Harding@Sun.COM 	uint32_t	i, j;
23748904SBarry.Harding@Sun.COM 
237511382SShidokht.Yadegari@Sun.COM 	if (id == SUNIXOS || id == SUNIXOS2 || id == UNUSED)
237611382SShidokht.Yadegari@Sun.COM 		return (-1);
237710015SBarry.Harding@Sun.COM 	for (i = 0; i < FD_NUMPART; i++) {
23788904SBarry.Harding@Sun.COM 		if (Old_Table[i].systid == id &&
23798904SBarry.Harding@Sun.COM 		    Old_Table[i].bootid == act &&
23808904SBarry.Harding@Sun.COM 		    Old_Table[i].beghead == bhead &&
23818904SBarry.Harding@Sun.COM 		    Old_Table[i].begsect == ((bsect & 0x3f) |
23828904SBarry.Harding@Sun.COM 		    (uchar_t)((bcyl>>2) & 0xc0)) &&
23838904SBarry.Harding@Sun.COM 		    Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
23848904SBarry.Harding@Sun.COM 		    Old_Table[i].endhead == ehead &&
23858904SBarry.Harding@Sun.COM 		    Old_Table[i].endsect == ((esect & 0x3f) |
23868904SBarry.Harding@Sun.COM 		    (uchar_t)((ecyl>>2) & 0xc0)) &&
23878904SBarry.Harding@Sun.COM 		    Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
23888904SBarry.Harding@Sun.COM 		    Old_Table[i].relsect == lel(rsect) &&
23898904SBarry.Harding@Sun.COM 		    Old_Table[i].numsect == lel(numsect)) {
23908904SBarry.Harding@Sun.COM 			/* find UNUSED partition table entry */
239111382SShidokht.Yadegari@Sun.COM 			for (j = startindex; j < FD_NUMPART; j++) {
23928904SBarry.Harding@Sun.COM 				if (Table[j].systid == UNUSED) {
23938904SBarry.Harding@Sun.COM 					(void) memcpy(&Table[j], &Old_Table[i],
23948904SBarry.Harding@Sun.COM 					    sizeof (Table[0]));
23958904SBarry.Harding@Sun.COM 					skip_verify[j] = 1;
239611382SShidokht.Yadegari@Sun.COM 					return (j);
23978904SBarry.Harding@Sun.COM 
23988904SBarry.Harding@Sun.COM 				}
23998904SBarry.Harding@Sun.COM 			}
240011382SShidokht.Yadegari@Sun.COM 			return (-1);
24018904SBarry.Harding@Sun.COM 		}
24028904SBarry.Harding@Sun.COM 
24038904SBarry.Harding@Sun.COM 	}
240411382SShidokht.Yadegari@Sun.COM 	return (-1);
24058904SBarry.Harding@Sun.COM }
24068904SBarry.Harding@Sun.COM 
24078904SBarry.Harding@Sun.COM /*
24080Sstevel@tonic-gate  * verify_tbl
24090Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
24100Sstevel@tonic-gate  * the disk.
24110Sstevel@tonic-gate  */
2412251Slclee static int
verify_tbl(void)2413251Slclee verify_tbl(void)
24140Sstevel@tonic-gate {
24157563SPrasad.Singamsetty@Sun.COM 	uint32_t	i, j, rsect, numsect;
24160Sstevel@tonic-gate 	int	noMoreParts = 0;
24170Sstevel@tonic-gate 	int	numParts = 0;
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
2420251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
24210Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
24220Sstevel@tonic-gate 			numParts++;
24230Sstevel@tonic-gate 			/*
242411382SShidokht.Yadegari@Sun.COM 			 * No valid partitions allowed after EFI_PMBR part
24250Sstevel@tonic-gate 			 */
24260Sstevel@tonic-gate 			if (noMoreParts) {
24270Sstevel@tonic-gate 				return (-1);
24280Sstevel@tonic-gate 			}
24290Sstevel@tonic-gate 
24300Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
243111382SShidokht.Yadegari@Sun.COM 				/*
243211382SShidokht.Yadegari@Sun.COM 				 * EFI_PMBR partition must be the only
243311382SShidokht.Yadegari@Sun.COM 				 * partition
243411382SShidokht.Yadegari@Sun.COM 				 */
243511382SShidokht.Yadegari@Sun.COM 				noMoreParts = 1;
24360Sstevel@tonic-gate 
24370Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
2438251Slclee 					(void) fprintf(stderr, "ERROR: "
24390Sstevel@tonic-gate 					    "Invalid starting sector "
24400Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
24410Sstevel@tonic-gate 					    "relsect %d "
24420Sstevel@tonic-gate 					    "(should be 1)\n",
24430Sstevel@tonic-gate 					    Table[i].relsect);
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 					return (-1);
24460Sstevel@tonic-gate 				}
24470Sstevel@tonic-gate 
244811382SShidokht.Yadegari@Sun.COM 				if (Table[i].numsect !=
244911382SShidokht.Yadegari@Sun.COM 				    ((dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
245011382SShidokht.Yadegari@Sun.COM 				    (dev_capacity - 1))) {
245111382SShidokht.Yadegari@Sun.COM 
2452251Slclee 					(void) fprintf(stderr, "ERROR: "
24530Sstevel@tonic-gate 					    "EFI_PMBR partition must "
245411382SShidokht.Yadegari@Sun.COM 					    "encompass the entire");
245511382SShidokht.Yadegari@Sun.COM 
245611382SShidokht.Yadegari@Sun.COM 					if (dev_capacity > DK_MAX_2TB)
245711382SShidokht.Yadegari@Sun.COM 						(void) fprintf(stderr,
245811382SShidokht.Yadegari@Sun.COM 						    "maximum 2 TB.\n "
245911382SShidokht.Yadegari@Sun.COM 						    "numsect %u - "
246011382SShidokht.Yadegari@Sun.COM 						    "actual %llu\n",
246111382SShidokht.Yadegari@Sun.COM 						    Table[i].numsect,
246211382SShidokht.Yadegari@Sun.COM 						    (diskaddr_t)DK_MAX_2TB);
246311382SShidokht.Yadegari@Sun.COM 
246411382SShidokht.Yadegari@Sun.COM 					else
246511382SShidokht.Yadegari@Sun.COM 						(void) fprintf(stderr,
246611382SShidokht.Yadegari@Sun.COM 						    "disk.\n numsect %u - "
246711382SShidokht.Yadegari@Sun.COM 						    "actual %llu\n",
246811382SShidokht.Yadegari@Sun.COM 						    Table[i].numsect,
246911382SShidokht.Yadegari@Sun.COM 						    dev_capacity - 1);
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 					return (-1);
24720Sstevel@tonic-gate 				}
24730Sstevel@tonic-gate 			}
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
247610021SSheshadri.Vasudevan@Sun.COM 			rsect = LE_32(Table[i].relsect);
247710021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
24787563SPrasad.Singamsetty@Sun.COM 
24797563SPrasad.Singamsetty@Sun.COM 			if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
24807563SPrasad.Singamsetty@Sun.COM 			    (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
24818904SBarry.Harding@Sun.COM 				if (!skip_verify[i])
24828904SBarry.Harding@Sun.COM 					return (-1);
24830Sstevel@tonic-gate 			}
24840Sstevel@tonic-gate 
2485251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
24860Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
24877563SPrasad.Singamsetty@Sun.COM 					uint32_t t_relsect =
248810021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].relsect);
24897563SPrasad.Singamsetty@Sun.COM 					uint32_t t_numsect =
249010021SSheshadri.Vasudevan@Sun.COM 					    LE_32(Table[j].numsect);
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 					if (noMoreParts) {
2493251Slclee 						(void) fprintf(stderr,
24940Sstevel@tonic-gate 						    "Cannot add partition to "
24950Sstevel@tonic-gate 						    "table; no more partitions "
24960Sstevel@tonic-gate 						    "allowed\n");
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 						if (io_debug) {
2499251Slclee 							(void) fprintf(stderr,
25000Sstevel@tonic-gate 							    "DEBUG: Current "
25010Sstevel@tonic-gate 							    "partition:\t"
25020Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2503251Slclee 							    "%d:%d:%d:%d:%d\n"
25040Sstevel@tonic-gate 							    "       Next "
25050Sstevel@tonic-gate 							    "partition:\t\t"
25060Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
2507251Slclee 							    "%d:%d:%d:%d:%d\n",
25080Sstevel@tonic-gate 							    Table[i].systid,
25090Sstevel@tonic-gate 							    Table[i].bootid,
25100Sstevel@tonic-gate 							    Table[i].begcyl,
25110Sstevel@tonic-gate 							    Table[i].beghead,
25120Sstevel@tonic-gate 							    Table[i].begsect,
25130Sstevel@tonic-gate 							    Table[i].endcyl,
25140Sstevel@tonic-gate 							    Table[i].endhead,
25150Sstevel@tonic-gate 							    Table[i].endsect,
25160Sstevel@tonic-gate 							    Table[i].relsect,
25170Sstevel@tonic-gate 							    Table[i].numsect,
25180Sstevel@tonic-gate 							    Table[j].systid,
25190Sstevel@tonic-gate 							    Table[j].bootid,
25200Sstevel@tonic-gate 							    Table[j].begcyl,
25210Sstevel@tonic-gate 							    Table[j].beghead,
25220Sstevel@tonic-gate 							    Table[j].begsect,
25230Sstevel@tonic-gate 							    Table[j].endcyl,
25240Sstevel@tonic-gate 							    Table[j].endhead,
25250Sstevel@tonic-gate 							    Table[j].endsect,
25260Sstevel@tonic-gate 							    Table[j].relsect,
25270Sstevel@tonic-gate 							    Table[j].numsect);
25280Sstevel@tonic-gate 						}
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 						return (-1);
25310Sstevel@tonic-gate 					}
25320Sstevel@tonic-gate 					if ((rsect >=
25330Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
2534251Slclee 					    ((rsect + numsect) <= t_relsect)) {
25350Sstevel@tonic-gate 						continue;
25360Sstevel@tonic-gate 					} else {
2537251Slclee 						(void) fprintf(stderr, "ERROR: "
25380Sstevel@tonic-gate 						    "current partition overlaps"
25390Sstevel@tonic-gate 						    " following partition\n");
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 						return (-1);
25420Sstevel@tonic-gate 					}
25430Sstevel@tonic-gate 				}
25440Sstevel@tonic-gate 			}
25450Sstevel@tonic-gate 		}
25460Sstevel@tonic-gate 	}
25470Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
25488904SBarry.Harding@Sun.COM 		if (noMoreParts)
25498904SBarry.Harding@Sun.COM 			return (-1);
25508904SBarry.Harding@Sun.COM 		if (!skip_verify[i] &&
25518904SBarry.Harding@Sun.COM 		    ((((diskaddr_t)lel(Table[i].relsect) +
25528333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > dev_capacity) ||
25538333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
25548904SBarry.Harding@Sun.COM 		    lel(Table[i].numsect)) > DK_MAX_2TB))) {
25550Sstevel@tonic-gate 			return (-1);
25560Sstevel@tonic-gate 		}
25570Sstevel@tonic-gate 	}
25580Sstevel@tonic-gate 
25590Sstevel@tonic-gate 	return (numParts);
25600Sstevel@tonic-gate }
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate /*
25630Sstevel@tonic-gate  * pars_fdisk
25640Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
25650Sstevel@tonic-gate  * (-A, -D, -F).
25660Sstevel@tonic-gate  */
2567251Slclee static int
pars_fdisk(char * line,int * id,int * act,int * bhead,int * bsect,int * bcyl,int * ehead,int * esect,int * ecyl,uint32_t * rsect,uint32_t * numsect)2568251Slclee pars_fdisk(
2569251Slclee     char *line,
2570251Slclee     int *id, int *act,
2571251Slclee     int *bhead, int *bsect, int *bcyl,
2572251Slclee     int *ehead, int *esect, int *ecyl,
25737563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect)
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	int	i;
257610021SSheshadri.Vasudevan@Sun.COM 	int64_t test;
257710021SSheshadri.Vasudevan@Sun.COM 	char *tok, *p;
257810021SSheshadri.Vasudevan@Sun.COM 	char buf[256];
257910021SSheshadri.Vasudevan@Sun.COM 
25800Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
25815169Slclee 		return (1);
25820Sstevel@tonic-gate 	line[strlen(line)] = '\0';
25830Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
25840Sstevel@tonic-gate 		if (line[i] == '\0') {
25850Sstevel@tonic-gate 			break;
25860Sstevel@tonic-gate 		} else if (line[i] == ':') {
25870Sstevel@tonic-gate 			line[i] = ' ';
25880Sstevel@tonic-gate 		}
25890Sstevel@tonic-gate 	}
2590*12505SShidokht.Yadegari@Sun.COM 	(void) strncpy(buf, line, 256);
259110021SSheshadri.Vasudevan@Sun.COM 	errno = 0;
259210021SSheshadri.Vasudevan@Sun.COM 	tok = strtok(buf, ": \t\n");
259310021SSheshadri.Vasudevan@Sun.COM 	while (tok != NULL) {
259410021SSheshadri.Vasudevan@Sun.COM 		for (p = tok; *p != '\0'; p++) {
259510021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(*p)) {
2596*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid input %s in line %s.\n",
259710021SSheshadri.Vasudevan@Sun.COM 				    tok, line);
259810021SSheshadri.Vasudevan@Sun.COM 				exit(1);
259910021SSheshadri.Vasudevan@Sun.COM 			}
260010021SSheshadri.Vasudevan@Sun.COM 		}
260110021SSheshadri.Vasudevan@Sun.COM 
260210021SSheshadri.Vasudevan@Sun.COM 		test = strtoll(tok, (char **)NULL, 10);
260310021SSheshadri.Vasudevan@Sun.COM 		if ((test < 0) || (test > 0xFFFFFFFF) || (errno != 0)) {
2604*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid input %s in line %s.\n", tok,
2605*12505SShidokht.Yadegari@Sun.COM 			    line);
260610021SSheshadri.Vasudevan@Sun.COM 			exit(1);
260710021SSheshadri.Vasudevan@Sun.COM 		}
260810021SSheshadri.Vasudevan@Sun.COM 		tok = strtok(NULL, ": \t\n");
260910021SSheshadri.Vasudevan@Sun.COM 	}
26107563SPrasad.Singamsetty@Sun.COM 	if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
26110Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
26120Sstevel@tonic-gate 	    rsect, numsect) != 10) {
26130Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
26140Sstevel@tonic-gate 		exit(1);
26150Sstevel@tonic-gate 	}
26160Sstevel@tonic-gate 	return (0);
26170Sstevel@tonic-gate }
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate /*
26200Sstevel@tonic-gate  * validate_part
26210Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
26220Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
26230Sstevel@tonic-gate  */
2624251Slclee static int
validate_part(int id,uint32_t rsect,uint32_t numsect)26257563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect)
26260Sstevel@tonic-gate {
26270Sstevel@tonic-gate 	int i;
26280Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
26290Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
26300Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
263110021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].relsect == LE_32(rsect)) &&
263210021SSheshadri.Vasudevan@Sun.COM 			    (Old_Table[i].numsect == LE_32(numsect)))
26335169Slclee 				return (0);
26340Sstevel@tonic-gate 		}
2635251Slclee 		(void) fprintf(stderr,
2636251Slclee 		    "New partition cannot start at sector 0\n");
26370Sstevel@tonic-gate 		return (-1);
26380Sstevel@tonic-gate 	}
263910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
264010021SSheshadri.Vasudevan@Sun.COM 	if (id > FDISK_MAX_VALID_PART_ID) {
2641*12505SShidokht.Yadegari@Sun.COM 		(void) fprintf(stderr, "Invalid partition ID\n");
264210021SSheshadri.Vasudevan@Sun.COM 		return (-1);
264310021SSheshadri.Vasudevan@Sun.COM 	}
264410021SSheshadri.Vasudevan@Sun.COM #endif
26450Sstevel@tonic-gate 	return (0);
26460Sstevel@tonic-gate }
26470Sstevel@tonic-gate 
26480Sstevel@tonic-gate /*
26490Sstevel@tonic-gate  * stage0
26500Sstevel@tonic-gate  * Print out interactive menu and process user input.
26510Sstevel@tonic-gate  */
2652251Slclee static void
stage0(void)2653251Slclee stage0(void)
26540Sstevel@tonic-gate {
265510021SSheshadri.Vasudevan@Sun.COM #ifdef i386
265610021SSheshadri.Vasudevan@Sun.COM 	int rval;
265710021SSheshadri.Vasudevan@Sun.COM #endif
2658251Slclee 	dispmenu();
2659251Slclee 	for (;;) {
2660251Slclee 		(void) printf(Q_LINE);
2661251Slclee 		(void) printf("Enter Selection: ");
26629786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
26630Sstevel@tonic-gate 		rm_blanks(s);
266410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
266510021SSheshadri.Vasudevan@Sun.COM 		while (!((s[0] > '0') && (s[0] < '8') &&
266610021SSheshadri.Vasudevan@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
266710021SSheshadri.Vasudevan@Sun.COM #else
26689786SBarry.Harding@Sun.COM 		while (!((s[0] > '0') && (s[0] < '7') &&
26699786SBarry.Harding@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
267010021SSheshadri.Vasudevan@Sun.COM #endif
2671251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
267210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
267310021SSheshadri.Vasudevan@Sun.COM 			(void) printf(
267410021SSheshadri.Vasudevan@Sun.COM 			    "Enter a one-digit number between 1 and 7.");
267510021SSheshadri.Vasudevan@Sun.COM #else
2676251Slclee 			(void) printf(
2677251Slclee 			    "Enter a one-digit number between 1 and 6.");
267810021SSheshadri.Vasudevan@Sun.COM #endif
2679251Slclee 			(void) printf(Q_LINE);
2680251Slclee 			(void) printf("Enter Selection: ");
26819786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
26820Sstevel@tonic-gate 			rm_blanks(s);
26830Sstevel@tonic-gate 		}
2684251Slclee 		(void) printf(E_LINE);
26850Sstevel@tonic-gate 		switch (s[0]) {
26860Sstevel@tonic-gate 			case '1':
26870Sstevel@tonic-gate 				if (pcreate() == -1)
26880Sstevel@tonic-gate 					return;
26890Sstevel@tonic-gate 				break;
26900Sstevel@tonic-gate 			case '2':
26910Sstevel@tonic-gate 				if (pchange() == -1)
26920Sstevel@tonic-gate 					return;
26930Sstevel@tonic-gate 				break;
26940Sstevel@tonic-gate 			case '3':
26950Sstevel@tonic-gate 				if (pdelete() == -1)
26960Sstevel@tonic-gate 					return;
26970Sstevel@tonic-gate 				break;
26980Sstevel@tonic-gate 			case '4':
26990Sstevel@tonic-gate 				if (ppartid() == -1)
27000Sstevel@tonic-gate 					return;
27010Sstevel@tonic-gate 				break;
270210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
270310021SSheshadri.Vasudevan@Sun.COM 			case '5':
270410021SSheshadri.Vasudevan@Sun.COM 				if (fdisk_ext_part_exists(epp)) {
270510021SSheshadri.Vasudevan@Sun.COM 					ext_part_menu();
270610021SSheshadri.Vasudevan@Sun.COM 				} else {
2707*12505SShidokht.Yadegari@Sun.COM 					(void) printf(Q_LINE);
2708*12505SShidokht.Yadegari@Sun.COM 					(void) printf("\nNo extended partition"
2709*12505SShidokht.Yadegari@Sun.COM 					    " found\n");
2710*12505SShidokht.Yadegari@Sun.COM 					(void) printf("Press enter to "
2711*12505SShidokht.Yadegari@Sun.COM 					    "continue\n");
271210021SSheshadri.Vasudevan@Sun.COM 					ext_read_input(s);
271310021SSheshadri.Vasudevan@Sun.COM 				}
271410021SSheshadri.Vasudevan@Sun.COM 				break;
271510021SSheshadri.Vasudevan@Sun.COM 			case '6':
271610021SSheshadri.Vasudevan@Sun.COM 				/* update disk partition table, if changed */
271710021SSheshadri.Vasudevan@Sun.COM 				if (TableChanged() == 1) {
271810021SSheshadri.Vasudevan@Sun.COM 					copy_Table_to_Bootblk();
271910021SSheshadri.Vasudevan@Sun.COM 					dev_mboot_write(0, Bootsect, sectsiz);
272010021SSheshadri.Vasudevan@Sun.COM 				}
272110021SSheshadri.Vasudevan@Sun.COM 
272210021SSheshadri.Vasudevan@Sun.COM 				/*
272310021SSheshadri.Vasudevan@Sun.COM 				 * If the VTOC table is wrong fix it
272410021SSheshadri.Vasudevan@Sun.COM 				 * (truncate only)
272510021SSheshadri.Vasudevan@Sun.COM 				 */
272610021SSheshadri.Vasudevan@Sun.COM 				if (io_adjt) {
272710021SSheshadri.Vasudevan@Sun.COM 					fix_slice();
272810021SSheshadri.Vasudevan@Sun.COM 				}
272910021SSheshadri.Vasudevan@Sun.COM 				if (!io_readonly) {
273010021SSheshadri.Vasudevan@Sun.COM 					rval = fdisk_commit_ext_part(epp);
273110021SSheshadri.Vasudevan@Sun.COM 					switch (rval) {
273210021SSheshadri.Vasudevan@Sun.COM 						case FDISK_SUCCESS:
273310021SSheshadri.Vasudevan@Sun.COM 							/* Success */
273410021SSheshadri.Vasudevan@Sun.COM 							/* Fallthrough */
273510021SSheshadri.Vasudevan@Sun.COM 						case FDISK_ENOEXTPART:
273610021SSheshadri.Vasudevan@Sun.COM 							/* Nothing to do */
273710021SSheshadri.Vasudevan@Sun.COM 							break;
273810021SSheshadri.Vasudevan@Sun.COM 						case FDISK_EMOUNTED:
2739*12505SShidokht.Yadegari@Sun.COM 							(void) printf(Q_LINE);
274010021SSheshadri.Vasudevan@Sun.COM 							preach_and_continue();
274110021SSheshadri.Vasudevan@Sun.COM 							continue;
274210021SSheshadri.Vasudevan@Sun.COM 						default:
274310021SSheshadri.Vasudevan@Sun.COM 							perror("Commit failed");
274410021SSheshadri.Vasudevan@Sun.COM 							exit(1);
274510021SSheshadri.Vasudevan@Sun.COM 					}
274610021SSheshadri.Vasudevan@Sun.COM 					libfdisk_fini(&epp);
274710021SSheshadri.Vasudevan@Sun.COM 				}
274810021SSheshadri.Vasudevan@Sun.COM 				(void) close(Dev);
274910021SSheshadri.Vasudevan@Sun.COM 				exit(0);
2750*12505SShidokht.Yadegari@Sun.COM 				/* NOTREACHED */
275110021SSheshadri.Vasudevan@Sun.COM #else
27520Sstevel@tonic-gate 			case '5':
27530Sstevel@tonic-gate 				/* update disk partition table, if changed */
27540Sstevel@tonic-gate 				if (TableChanged() == 1) {
27550Sstevel@tonic-gate 					copy_Table_to_Bootblk();
27560Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
27570Sstevel@tonic-gate 				}
27580Sstevel@tonic-gate 				/*
27590Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27600Sstevel@tonic-gate 				 * (truncate only)
27610Sstevel@tonic-gate 				 */
27620Sstevel@tonic-gate 				if (io_adjt) {
27630Sstevel@tonic-gate 					fix_slice();
27640Sstevel@tonic-gate 				}
2765251Slclee 				(void) close(Dev);
27660Sstevel@tonic-gate 				exit(0);
2767251Slclee 				/* FALLTHRU */
276810021SSheshadri.Vasudevan@Sun.COM #endif
276910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
277010021SSheshadri.Vasudevan@Sun.COM 			case '7':
277110021SSheshadri.Vasudevan@Sun.COM #else
27720Sstevel@tonic-gate 			case '6':
277310021SSheshadri.Vasudevan@Sun.COM #endif
27740Sstevel@tonic-gate 				/*
27750Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
27760Sstevel@tonic-gate 				 * (truncate only)
27770Sstevel@tonic-gate 				 */
27780Sstevel@tonic-gate 				if (io_adjt) {
27790Sstevel@tonic-gate 					fix_slice();
27800Sstevel@tonic-gate 				}
2781251Slclee 				(void) close(Dev);
27820Sstevel@tonic-gate 				exit(0);
2783251Slclee 				/* FALLTHRU */
27840Sstevel@tonic-gate 			default:
27850Sstevel@tonic-gate 				break;
27860Sstevel@tonic-gate 		}
27870Sstevel@tonic-gate 		copy_Table_to_Bootblk();
27880Sstevel@tonic-gate 		disptbl();
2789251Slclee 		dispmenu();
27900Sstevel@tonic-gate 	}
27910Sstevel@tonic-gate }
27920Sstevel@tonic-gate 
27930Sstevel@tonic-gate /*
27940Sstevel@tonic-gate  * pcreate
27950Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
27960Sstevel@tonic-gate  */
2797251Slclee static int
2798251Slclee pcreate(void)
27990Sstevel@tonic-gate {
2800251Slclee 	uchar_t tsystid = 'z';
28010Sstevel@tonic-gate 	int i, j;
28027563SPrasad.Singamsetty@Sun.COM 	uint32_t numsect;
28030Sstevel@tonic-gate 	int retCode = 0;
280410021SSheshadri.Vasudevan@Sun.COM #ifdef i386
280510021SSheshadri.Vasudevan@Sun.COM 	int ext_part_present = 0;
280610021SSheshadri.Vasudevan@Sun.COM #endif
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate 	i = 0;
2809251Slclee 	for (;;) {
28100Sstevel@tonic-gate 		if (i == FD_NUMPART) {
2811251Slclee 			(void) printf(E_LINE);
2812251Slclee 			(void) printf(
2813251Slclee 			    "The partition table is full!\n"
2814251Slclee 			    "You must delete a partition before creating"
28150Sstevel@tonic-gate 			    " a new one.\n");
28160Sstevel@tonic-gate 			return (-1);
28170Sstevel@tonic-gate 		}
28180Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
28190Sstevel@tonic-gate 			break;
28200Sstevel@tonic-gate 		}
28210Sstevel@tonic-gate 		i++;
28220Sstevel@tonic-gate 	}
28230Sstevel@tonic-gate 
28247563SPrasad.Singamsetty@Sun.COM 	numsect = 0;
28250Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28260Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
282710021SSheshadri.Vasudevan@Sun.COM 			numsect += LE_32(Table[i].numsect);
28280Sstevel@tonic-gate 		}
282910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
283010021SSheshadri.Vasudevan@Sun.COM 		/* Check if an extended partition already exists */
283110021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(Table[i].systid)) {
283210021SSheshadri.Vasudevan@Sun.COM 			ext_part_present = 1;
283310021SSheshadri.Vasudevan@Sun.COM 		}
283410021SSheshadri.Vasudevan@Sun.COM #endif
28357563SPrasad.Singamsetty@Sun.COM 		if (numsect >= chs_capacity) {
2836251Slclee 			(void) printf(E_LINE);
2837251Slclee 			(void) printf("There is no more room on the disk for"
28380Sstevel@tonic-gate 			    " another partition.\n");
2839251Slclee 			(void) printf(
2840251Slclee 			    "You must delete a partition before creating"
28410Sstevel@tonic-gate 			    " a new one.\n");
28420Sstevel@tonic-gate 			return (-1);
28430Sstevel@tonic-gate 		}
28440Sstevel@tonic-gate 	}
28450Sstevel@tonic-gate 	while (tsystid == 'z') {
28467563SPrasad.Singamsetty@Sun.COM 
28477563SPrasad.Singamsetty@Sun.COM 		/*
28487563SPrasad.Singamsetty@Sun.COM 		 * The question here is expanding to more than what is
28497563SPrasad.Singamsetty@Sun.COM 		 * allocated for question lines (Q_LINE) which garbles
28507563SPrasad.Singamsetty@Sun.COM 		 * at least warning line. Clearing warning line as workaround
28517563SPrasad.Singamsetty@Sun.COM 		 * for now.
28527563SPrasad.Singamsetty@Sun.COM 		 */
28537563SPrasad.Singamsetty@Sun.COM 
28547563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
2855251Slclee 		(void) printf(Q_LINE);
2856251Slclee 		(void) printf(
2857251Slclee 		    "Select the partition type to create:\n"
2858251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
2859251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
2860251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
2861251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
28629786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
28630Sstevel@tonic-gate 		rm_blanks(s);
28649786SBarry.Harding@Sun.COM 		if ((s[1] != '\0') && (s[1] != '\n')) {
2865251Slclee 			(void) printf(E_LINE);
2866251Slclee 			(void) printf("Invalid selection, try again.");
28670Sstevel@tonic-gate 			continue;
28680Sstevel@tonic-gate 		}
28690Sstevel@tonic-gate 		switch (s[0]) {
28700Sstevel@tonic-gate 		case '0':		/* exit */
28715169Slclee 			(void) printf(E_LINE);
28725169Slclee 			return (-1);
28730Sstevel@tonic-gate 		case '1':		/* Solaris partition */
28745169Slclee 			tsystid = SUNIXOS2;
28755169Slclee 			break;
28760Sstevel@tonic-gate 		case '2':		/* UNIX partition */
28775169Slclee 			tsystid = UNIXOS;
28785169Slclee 			break;
28790Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
28805169Slclee 			tsystid = PCIXOS;
28815169Slclee 			break;
28820Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
28835169Slclee 			tsystid = OTHEROS;
28845169Slclee 			break;
28850Sstevel@tonic-gate 		case '5':
28865169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
28875169Slclee 			break;
28880Sstevel@tonic-gate 		case '6':
28895169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
28905169Slclee 			break;
28910Sstevel@tonic-gate 		case '7':
289210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
289310021SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
2894*12505SShidokht.Yadegari@Sun.COM 				(void) printf(Q_LINE);
2895*12505SShidokht.Yadegari@Sun.COM 				(void) printf(E_LINE);
2896*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr,
289710021SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
2898*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr,
2899*12505SShidokht.Yadegari@Sun.COM 				    "Press enter to continue\n");
290010021SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
290110021SSheshadri.Vasudevan@Sun.COM 				continue;
290210021SSheshadri.Vasudevan@Sun.COM 			}
290310021SSheshadri.Vasudevan@Sun.COM #endif
29045169Slclee 			tsystid = EXTDOS;
29055169Slclee 			break;
29060Sstevel@tonic-gate 		case '8':
29075169Slclee 			tsystid = DOSHUGE;
29085169Slclee 			break;
29090Sstevel@tonic-gate 		case '9':
29105169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
29115169Slclee 			break;
29120Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
29130Sstevel@tonic-gate 		case 'A':
29145169Slclee 			tsystid = X86BOOT;
29155169Slclee 			break;
29160Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
29170Sstevel@tonic-gate 		case 'B':
29185169Slclee 			tsystid = DIAGPART;
29195169Slclee 			break;
29200Sstevel@tonic-gate 		case 'c':		/* FAT32 */
29210Sstevel@tonic-gate 		case 'C':
29225169Slclee 			tsystid = FDISK_WINDOWS;
29235169Slclee 			break;
29240Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
29250Sstevel@tonic-gate 		case 'D':
29265169Slclee 			tsystid = FDISK_EXT_WIN;
29275169Slclee 			break;
29280Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
29290Sstevel@tonic-gate 		case 'E':
293010682SSheshadri.Vasudevan@Sun.COM #ifdef i386
293110682SSheshadri.Vasudevan@Sun.COM 			if (ext_part_present) {
2932*12505SShidokht.Yadegari@Sun.COM 				(void) printf(Q_LINE);
2933*12505SShidokht.Yadegari@Sun.COM 				(void) printf(E_LINE);
2934*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr,
293510682SSheshadri.Vasudevan@Sun.COM 				    "Extended partition already exists\n");
2936*12505SShidokht.Yadegari@Sun.COM 				(void) fprintf(stderr,
2937*12505SShidokht.Yadegari@Sun.COM 				    "Press enter to continue\n");
293810682SSheshadri.Vasudevan@Sun.COM 				ext_read_input(s);
293910682SSheshadri.Vasudevan@Sun.COM 				continue;
294010682SSheshadri.Vasudevan@Sun.COM 			}
294110682SSheshadri.Vasudevan@Sun.COM #endif
29425169Slclee 			tsystid = FDISK_EXTLBA;
29435169Slclee 			break;
29440Sstevel@tonic-gate 		case 'f':
29450Sstevel@tonic-gate 		case 'F':
29465169Slclee 			tsystid = EFI_PMBR;
29475169Slclee 			break;
29480Sstevel@tonic-gate 		default:
29495169Slclee 			(void) printf(E_LINE);
29505169Slclee 			(void) printf("Invalid selection, try again.");
29515169Slclee 			continue;
29520Sstevel@tonic-gate 		}
29530Sstevel@tonic-gate 	}
29540Sstevel@tonic-gate 
2955251Slclee 	(void) printf(E_LINE);
29560Sstevel@tonic-gate 
29570Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
29587563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
29597563SPrasad.Singamsetty@Sun.COM 		if ((dev_capacity > DK_MAX_2TB))
29607563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
29617563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
29627563SPrasad.Singamsetty@Sun.COM 
29630Sstevel@tonic-gate 		/* create the new partition */
29640Sstevel@tonic-gate 		i = specify(tsystid);
29650Sstevel@tonic-gate 
29660Sstevel@tonic-gate 		if (i != -1) {
29670Sstevel@tonic-gate 			/* see if it should be the active partition */
2968251Slclee 			(void) printf(E_LINE);
2969251Slclee 			(void) printf(Q_LINE);
2970251Slclee 
2971251Slclee 			(void) printf(
2972251Slclee 			    "Should this become the active partition? If "
2973251Slclee 			    "yes, it  will be activated\n"
2974251Slclee 			    "each time the computer is reset or turned on.\n"
2975251Slclee 			    "Please type \"y\" or \"n\". ");
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 			if (yesno()) {
2978251Slclee 				(void) printf(E_LINE);
29790Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
29800Sstevel@tonic-gate 					if (j == i) {
29810Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2982251Slclee 						(void) printf(E_LINE);
2983251Slclee 						(void) printf(
2984251Slclee 						    "Partition %d is now "
29850Sstevel@tonic-gate 						    "the active partition.",
2986251Slclee 						    j + 1);
29870Sstevel@tonic-gate 					} else {
29880Sstevel@tonic-gate 						Table[j].bootid = 0;
29890Sstevel@tonic-gate 					}
29900Sstevel@tonic-gate 				}
29910Sstevel@tonic-gate 			} else {
29920Sstevel@tonic-gate 				Table[i].bootid = 0;
29930Sstevel@tonic-gate 			}
29940Sstevel@tonic-gate 
299510021SSheshadri.Vasudevan@Sun.COM #ifdef i386
299610021SSheshadri.Vasudevan@Sun.COM 			/*
299710021SSheshadri.Vasudevan@Sun.COM 			 * If partition created is an extended partition, null
299810021SSheshadri.Vasudevan@Sun.COM 			 * out the first sector of the first cylinder of the
299910021SSheshadri.Vasudevan@Sun.COM 			 * extended partition
300010021SSheshadri.Vasudevan@Sun.COM 			 */
300110021SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_dos_extended(Table[i].systid)) {
3002*12505SShidokht.Yadegari@Sun.COM 				(void) fdisk_init_ext_part(epp,
300310021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].relsect),
300410021SSheshadri.Vasudevan@Sun.COM 				    LE_32(Table[i].numsect));
300510021SSheshadri.Vasudevan@Sun.COM 			}
300610021SSheshadri.Vasudevan@Sun.COM #endif
30070Sstevel@tonic-gate 			/* set up the return code */
30080Sstevel@tonic-gate 			i = 1;
30090Sstevel@tonic-gate 		}
30100Sstevel@tonic-gate 	} else {
30110Sstevel@tonic-gate 		/*
30120Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
30130Sstevel@tonic-gate 		 * the table
30140Sstevel@tonic-gate 		 *
30150Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
30160Sstevel@tonic-gate 		 * empty
30170Sstevel@tonic-gate 		 */
30180Sstevel@tonic-gate 		retCode = verify_tbl();
30190Sstevel@tonic-gate 
30200Sstevel@tonic-gate 		if (retCode < 0) {
3021251Slclee 			(void) fprintf(stderr,
30220Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
30230Sstevel@tonic-gate 			    "current partition table is invalid.\n");
30240Sstevel@tonic-gate 			return (-1);
30250Sstevel@tonic-gate 		} else if (retCode > 0) {
3026251Slclee 			(void) printf(
3027251Slclee 			    "An EFI partition must be the only partition on "
3028251Slclee 			    "disk.  You may manually delete existing\n"
3029251Slclee 			    "partitions, or fdisk can do it.\n"
3030251Slclee 			    "Do you want fdisk to destroy existing "
3031251Slclee 			    "partitions?\n"
3032251Slclee 			    "Please type \"y\" or \"n\". ");
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 			if (yesno()) {
30350Sstevel@tonic-gate 				nulltbl();
30360Sstevel@tonic-gate 			} else {
30370Sstevel@tonic-gate 				return (-1);
30380Sstevel@tonic-gate 			}
30390Sstevel@tonic-gate 		}
30400Sstevel@tonic-gate 
30410Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
30427563SPrasad.Singamsetty@Sun.COM 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
30437563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
304411382SShidokht.Yadegari@Sun.COM 		    (dev_capacity - 1), 0);
30450Sstevel@tonic-gate 
30460Sstevel@tonic-gate 		if (i != 0) {
3047251Slclee 			(void) printf("Error creating EFI partition!!!\n");
30480Sstevel@tonic-gate 			i = -1;
30490Sstevel@tonic-gate 		} else {
30500Sstevel@tonic-gate 
30510Sstevel@tonic-gate 			/* EFI partitions are currently never active */
30520Sstevel@tonic-gate 			Table[i].bootid = 0;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 			/* set up the return code */
30550Sstevel@tonic-gate 			i = 1;
30560Sstevel@tonic-gate 		}
30570Sstevel@tonic-gate 	}
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	return (i);
30600Sstevel@tonic-gate }
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate /*
30630Sstevel@tonic-gate  * specify
30640Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
30650Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
30660Sstevel@tonic-gate  * cylinder and length in cylinders.
30670Sstevel@tonic-gate  */
3068251Slclee static int
3069251Slclee specify(uchar_t tsystid)
30700Sstevel@tonic-gate {
30715169Slclee 	int	i, j, percent = -1;
30727563SPrasad.Singamsetty@Sun.COM 	int	cyl, cylen;
30737563SPrasad.Singamsetty@Sun.COM 	diskaddr_t first_free, size_free;
30747563SPrasad.Singamsetty@Sun.COM 	diskaddr_t max_free;
30755169Slclee 	int	cyl_size;
30760Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
307711382SShidokht.Yadegari@Sun.COM 	struct ipart localpart[FD_NUMPART];
30780Sstevel@tonic-gate 
30795169Slclee 	cyl_size = heads * sectors;
30805169Slclee 
30815169Slclee 	/*
30825169Slclee 	 * make a local copy of the partition table
30835169Slclee 	 * and sort it into relsect order
30845169Slclee 	 */
308511382SShidokht.Yadegari@Sun.COM 
308611382SShidokht.Yadegari@Sun.COM 
308711382SShidokht.Yadegari@Sun.COM 	for (i = 0, j = 0; i < FD_NUMPART; i++) {
308811382SShidokht.Yadegari@Sun.COM 		if (Table[i].systid != UNUSED) {
308911382SShidokht.Yadegari@Sun.COM 			localpart[j] = Table[i];
309011382SShidokht.Yadegari@Sun.COM 			j++;
309111382SShidokht.Yadegari@Sun.COM 		}
309211382SShidokht.Yadegari@Sun.COM 	}
309311382SShidokht.Yadegari@Sun.COM 
309411382SShidokht.Yadegari@Sun.COM 	while (j < FD_NUMPART) {
309511382SShidokht.Yadegari@Sun.COM 		(void) memset(&localpart[j], 0, sizeof (struct ipart));
309611382SShidokht.Yadegari@Sun.COM 		j++;
309711382SShidokht.Yadegari@Sun.COM 	}
309811382SShidokht.Yadegari@Sun.COM 
30995169Slclee 	for (i = 0; i < FD_NUMPART; i++)
310011382SShidokht.Yadegari@Sun.COM 		partition[i] = &localpart[i];
31015169Slclee 
31025169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
31035169Slclee 		if (partition[i]->systid == UNUSED)
31045169Slclee 			break;
31055169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
31065169Slclee 			if (partition[j]->systid == UNUSED)
31075169Slclee 				break;
310810021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(partition[j]->relsect) <
310910021SSheshadri.Vasudevan@Sun.COM 			    LE_32(partition[i]->relsect)) {
31105169Slclee 				struct ipart *temp = partition[i];
31115169Slclee 				partition[i] = partition[j];
31125169Slclee 				partition[j] = temp;
31135169Slclee 			}
31145169Slclee 		}
31155169Slclee 	}
31165169Slclee 
31177563SPrasad.Singamsetty@Sun.COM 
3118251Slclee 	(void) printf(Q_LINE);
3119251Slclee 	(void) printf(
3120251Slclee 	    "Specify the percentage of disk to use for this partition\n"
3121251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
31229786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
31230Sstevel@tonic-gate 	rm_blanks(s);
31240Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
31255169Slclee 		i = 0;
31269786SBarry.Harding@Sun.COM 		while ((s[i] != '\0') && (s[i] != '\n')) {
31275169Slclee 			if (s[i] < '0' || s[i] > '9') {
31285169Slclee 				(void) printf(E_LINE);
31295169Slclee 				(void) printf("Invalid percentage value "
31305169Slclee 				    "specified; retry the operation.");
31315169Slclee 				return (-1);
31325169Slclee 			}
31335169Slclee 			i++;
31345169Slclee 			if (i > 3) {
31355169Slclee 				(void) printf(E_LINE);
31365169Slclee 				(void) printf("Invalid percentage value "
31375169Slclee 				    "specified; retry the operation.");
31385169Slclee 				return (-1);
31395169Slclee 			}
31405169Slclee 		}
31415169Slclee 		if ((percent = atoi(s)) > 100) {
31425169Slclee 			(void) printf(E_LINE);
31435169Slclee 			(void) printf(
31445169Slclee 			    "Percentage value is too large. The value must be"
31455169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31465169Slclee 			return (-1);
31470Sstevel@tonic-gate 		}
31485169Slclee 		if (percent < 1) {
31495169Slclee 			(void) printf(E_LINE);
31505169Slclee 			(void) printf(
31515169Slclee 			    "Percentage value is too small. The value must be"
31525169Slclee 			    " between 1 and 100;\nretry the operation.\n");
31535169Slclee 			return (-1);
31545169Slclee 		}
31555169Slclee 
31565169Slclee 		if (percent == 100)
31577563SPrasad.Singamsetty@Sun.COM 			cylen = Numcyl_usable - 1;
31585169Slclee 		else
31597563SPrasad.Singamsetty@Sun.COM 			cylen = (Numcyl_usable * percent) / 100;
31605169Slclee 
31615169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
31625169Slclee 		if ((tsystid == DOSOS12) &&
31635169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
31645169Slclee 			int n;
31657563SPrasad.Singamsetty@Sun.COM 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
31665169Slclee 			(void) printf(E_LINE);
31675169Slclee 			(void) printf("Maximum size for a DOS partition "
31685169Slclee 			    "is %d%%; retry the operation.",
31695169Slclee 			    n <= 100 ? n : 100);
31705169Slclee 			return (-1);
31710Sstevel@tonic-gate 		}
31725169Slclee 
31735169Slclee 
31745169Slclee 		max_free = 0;
31755169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
31765169Slclee 
31775169Slclee 			/*
31785169Slclee 			 * check for free space before partition i
31795169Slclee 			 * where i varies from 0 to 3
31805169Slclee 			 *
31815169Slclee 			 * freespace after partition 3 is unusable
31825169Slclee 			 * because there are no free partitions
31835169Slclee 			 *
31845169Slclee 			 * freespace begins at the end of previous partition
31855169Slclee 			 * or cylinder 1
31865169Slclee 			 */
31875169Slclee 			if (i) {
31885169Slclee 				/* Not an empty table */
318910021SSheshadri.Vasudevan@Sun.COM 				first_free = LE_32(partition[i - 1]->relsect) +
319010021SSheshadri.Vasudevan@Sun.COM 				    LE_32(partition[i - 1]->numsect);
31915169Slclee 			} else {
31925169Slclee 				first_free = cyl_size;
31935169Slclee 			}
31945169Slclee 
31955169Slclee 			/*
31965169Slclee 			 * freespace ends before the current partition
31975169Slclee 			 * or the end of the disk (chs end)
31985169Slclee 			 */
31995169Slclee 			if (partition[i]->systid == UNUSED) {
32005169Slclee 				size_free = chs_capacity - first_free;
32015169Slclee 			} else {
32028148SShidokht.Yadegari@Sun.COM 				/*
32038148SShidokht.Yadegari@Sun.COM 				 * Partition might start before cylinder 1.
32048148SShidokht.Yadegari@Sun.COM 				 * Make sure free space is not negative.
32058148SShidokht.Yadegari@Sun.COM 				 */
32065169Slclee 				size_free =
320710021SSheshadri.Vasudevan@Sun.COM 				    (LE_32(partition[i]->relsect > first_free))
320810021SSheshadri.Vasudevan@Sun.COM 				    ? (LE_32(partition[i]->relsect) -
320910021SSheshadri.Vasudevan@Sun.COM 				    first_free) : 0;
32105169Slclee 			}
32115169Slclee 
32125169Slclee 			/* save largest free space */
32135169Slclee 			if (max_free < size_free)
32145169Slclee 				max_free = size_free;
32155169Slclee 
32167563SPrasad.Singamsetty@Sun.COM 			if (((uint64_t)cylen * cyl_size) <= size_free) {
32175169Slclee 				/* We found a place to use */
32185169Slclee 				break;
32195169Slclee 			}
32205169Slclee 			if (partition[i]->systid == UNUSED) {
32215169Slclee 				(void) printf(E_LINE);
32225169Slclee 				max_free /= (cyl_size);
32235169Slclee 				(void) fprintf(stderr, "fdisk: "
32247563SPrasad.Singamsetty@Sun.COM 				    "Maximum percentage available is %lld\n",
32257563SPrasad.Singamsetty@Sun.COM 				    100 * max_free / Numcyl_usable);
32265169Slclee 				return (-1);
32275169Slclee 			}
32280Sstevel@tonic-gate 		}
32295169Slclee 
32305169Slclee 		(void) printf(E_LINE);
32315169Slclee 		if (i >= FD_NUMPART) {
32325169Slclee 			(void) fprintf(stderr,
32335169Slclee 			    "fdisk: Partition table is full.\n");
32345169Slclee 			return (-1);
32355169Slclee 		}
32365169Slclee 
32375169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
323811382SShidokht.Yadegari@Sun.COM 		    first_free, cylen * cyl_size, 0)) >= 0)  {
32395169Slclee 			return (i);
32405169Slclee 		}
32415169Slclee 		return (-1);
32425169Slclee 	} else {
32435169Slclee 
32445169Slclee 		/* Specifying size in cylinders */
3245251Slclee 		(void) printf(E_LINE);
32465169Slclee 		(void) printf(Q_LINE);
32475169Slclee 		(void) printf("Enter starting cylinder number: ");
32485169Slclee 		if ((cyl = getcyl()) == -1) {
32495169Slclee 			(void) printf(E_LINE);
32505169Slclee 			(void) printf("Invalid number; retry the operation.");
32515169Slclee 			return (-1);
32525169Slclee 		}
32535169Slclee 		if (cyl == 0) {
32545169Slclee 			(void) printf(E_LINE);
32555169Slclee 			(void) printf(
32565169Slclee 			    "New partition cannot start at cylinder 0.\n");
32575169Slclee 			return (-1);
32585169Slclee 		}
32597563SPrasad.Singamsetty@Sun.COM 
32607563SPrasad.Singamsetty@Sun.COM 
32617563SPrasad.Singamsetty@Sun.COM 		if (cyl >= Numcyl_usable) {
32625169Slclee 			(void) printf(E_LINE);
32635169Slclee 			(void) printf(
32645169Slclee 			    "Cylinder %d is out of bounds, "
32655169Slclee 			    "the maximum is %d.\n",
32667563SPrasad.Singamsetty@Sun.COM 			    cyl, Numcyl_usable - 1);
32675169Slclee 			return (-1);
32685169Slclee 		}
32697563SPrasad.Singamsetty@Sun.COM 
32705169Slclee 		(void) printf(Q_LINE);
32715169Slclee 		(void) printf("Enter partition size in cylinders: ");
32725169Slclee 		if ((cylen = getcyl()) == -1) {
32735169Slclee 			(void) printf(E_LINE);
32745169Slclee 			(void) printf("Invalid number, retry the operation.");
32755169Slclee 			return (-1);
32765169Slclee 		}
32775169Slclee 
32785169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
32795169Slclee 			uint32_t	t_relsect, t_numsect;
32805169Slclee 
32815169Slclee 			if (partition[i]->systid == UNUSED)
32825169Slclee 				break;
328310021SSheshadri.Vasudevan@Sun.COM 			t_relsect = LE_32(partition[i]->relsect);
328410021SSheshadri.Vasudevan@Sun.COM 			t_numsect = LE_32(partition[i]->numsect);
32855169Slclee 
32865169Slclee 			if (cyl * cyl_size >= t_relsect &&
32875169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
32885169Slclee 				(void) printf(E_LINE);
32895169Slclee 				(void) printf(
32905169Slclee 				    "Cylinder %d is already allocated"
32915169Slclee 				    "\nretry the operation.",
32925169Slclee 				    cyl);
32935169Slclee 				return (-1);
32945169Slclee 			}
32955169Slclee 
32965169Slclee 			if (cyl * cyl_size < t_relsect &&
32975169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
32985169Slclee 				(void) printf(E_LINE);
32995169Slclee 				(void) printf(
33005169Slclee 				    "Maximum size for partition is %u cylinders"
33015169Slclee 				    "\nretry the operation.",
33025169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
33035169Slclee 				return (-1);
33045169Slclee 			}
33055169Slclee 		}
33065169Slclee 
33077563SPrasad.Singamsetty@Sun.COM 		/* Verify partition doesn't exceed disk size or 2 TB */
33087563SPrasad.Singamsetty@Sun.COM 		if (cyl + cylen > Numcyl_usable) {
33095169Slclee 			(void) printf(E_LINE);
33107563SPrasad.Singamsetty@Sun.COM 			if (Numcyl > Numcyl_usable) {
33117563SPrasad.Singamsetty@Sun.COM 				(void) printf(
33127563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
33137563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
33147563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
33157563SPrasad.Singamsetty@Sun.COM 			} else {
33167563SPrasad.Singamsetty@Sun.COM 				(void) printf(
33177563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
33187563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
33197563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
33207563SPrasad.Singamsetty@Sun.COM 			}
33215169Slclee 			return (-1);
33225169Slclee 		}
33235169Slclee 
33245169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
33255169Slclee 		if ((tsystid == DOSOS12) &&
33265169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
33275169Slclee 			(void) printf(E_LINE);
33285169Slclee 			(void) printf(
33295169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
33305169Slclee 			    "\nretry the operation.",
33315169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
33325169Slclee 			return (-1);
33335169Slclee 		}
33345169Slclee 
3335251Slclee 		(void) printf(E_LINE);
33365169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
333711382SShidokht.Yadegari@Sun.COM 		    cyl * cyl_size, cylen * cyl_size, 0);
33385169Slclee 		if (i < 0)
33395169Slclee 			return (-1);
33405169Slclee 
33415169Slclee 		if (verify_tbl() < 0) {
33425169Slclee 			(void) printf(E_LINE);
33435169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
33445169Slclee 			return (-1);
33455169Slclee 		}
33465169Slclee 
33475169Slclee 		return (i);
33480Sstevel@tonic-gate 	}
33490Sstevel@tonic-gate }
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate /*
33520Sstevel@tonic-gate  * dispmenu
33530Sstevel@tonic-gate  * Display command menu (interactive mode).
33540Sstevel@tonic-gate  */
3355251Slclee static void
3356251Slclee dispmenu(void)
33570Sstevel@tonic-gate {
3358251Slclee 	(void) printf(M_LINE);
335910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
336010021SSheshadri.Vasudevan@Sun.COM 	(void) printf(
336110021SSheshadri.Vasudevan@Sun.COM 	    "SELECT ONE OF THE FOLLOWING:\n"
336210021SSheshadri.Vasudevan@Sun.COM 	    "   1. Create a partition\n"
336310021SSheshadri.Vasudevan@Sun.COM 	    "   2. Specify the active partition\n"
336410021SSheshadri.Vasudevan@Sun.COM 	    "   3. Delete a partition\n"
336510021SSheshadri.Vasudevan@Sun.COM 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
336610021SSheshadri.Vasudevan@Sun.COM 	    "   5. Edit/View extended partitions\n"
336710021SSheshadri.Vasudevan@Sun.COM 	    "   6. Exit (update disk configuration and exit)\n"
336810021SSheshadri.Vasudevan@Sun.COM 	    "   7. Cancel (exit without updating disk configuration)\n");
336910021SSheshadri.Vasudevan@Sun.COM #else
3370251Slclee 	(void) printf(
3371251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
3372251Slclee 	    "   1. Create a partition\n"
3373251Slclee 	    "   2. Specify the active partition\n"
3374251Slclee 	    "   3. Delete a partition\n"
3375251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
3376251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
3377251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
337810021SSheshadri.Vasudevan@Sun.COM #endif
33790Sstevel@tonic-gate }
33800Sstevel@tonic-gate 
33810Sstevel@tonic-gate /*
33820Sstevel@tonic-gate  * pchange
33830Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
33840Sstevel@tonic-gate  */
3385251Slclee static int
3386251Slclee pchange(void)
33870Sstevel@tonic-gate {
33880Sstevel@tonic-gate 	char s[80];
33890Sstevel@tonic-gate 	int i, j;
33900Sstevel@tonic-gate 
3391251Slclee 	for (;;) {
3392251Slclee 		(void) printf(Q_LINE);
33930Sstevel@tonic-gate 			{
3394251Slclee 			(void) printf(
3395251Slclee 			    "Specify the partition number to boot from"
33960Sstevel@tonic-gate 			    " (or specify 0 for none): ");
33970Sstevel@tonic-gate 			}
33989786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
33990Sstevel@tonic-gate 		rm_blanks(s);
34009786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
34019786SBarry.Harding@Sun.COM 		    (s[0] < '0') || (s[0] > '4')) {
3402251Slclee 			(void) printf(E_LINE);
3403251Slclee 			(void) printf(
3404251Slclee 			    "Invalid response, please specify a number"
34050Sstevel@tonic-gate 			    " between 0 and 4.\n");
34060Sstevel@tonic-gate 		} else {
34070Sstevel@tonic-gate 			break;
34080Sstevel@tonic-gate 		}
34090Sstevel@tonic-gate 	}
34100Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
34110Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
34120Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
34130Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
34140Sstevel@tonic-gate 				Table[i].bootid = 0;
34150Sstevel@tonic-gate 		}
3416251Slclee 		(void) printf(E_LINE);
3417251Slclee 			(void) printf(
3418251Slclee 			    "No partition is currently marked as active.");
34190Sstevel@tonic-gate 		return (0);
34200Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
342111382SShidokht.Yadegari@Sun.COM 
34220Sstevel@tonic-gate 		i = s[0] - '1';
342311382SShidokht.Yadegari@Sun.COM 
34240Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
3425251Slclee 			(void) printf(E_LINE);
3426251Slclee 			(void) printf("Partition does not exist.");
34270Sstevel@tonic-gate 			return (-1);
34280Sstevel@tonic-gate 		}
34290Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
34300Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
34310Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
34320Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
3433251Slclee 			(void) printf(E_LINE);
3434251Slclee 			(void) printf(
3435251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
34360Sstevel@tonic-gate 			    "cannot be made active.\n");
3437251Slclee 			(void) printf("Select another partition.");
34380Sstevel@tonic-gate 			return (-1);
34390Sstevel@tonic-gate 		}
34400Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
34410Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
34420Sstevel@tonic-gate 			if (j != i)
34430Sstevel@tonic-gate 			Table[j].bootid = 0;
34440Sstevel@tonic-gate 		}
34450Sstevel@tonic-gate 	}
3446251Slclee 	(void) printf(E_LINE);
34470Sstevel@tonic-gate 		{
3448251Slclee 		(void) printf(
3449251Slclee 		    "Partition %d is now active. The system will start up"
3450251Slclee 		    " from this\n", i + 1);
3451251Slclee 		(void) printf("partition after the next reboot.");
34520Sstevel@tonic-gate 		}
34530Sstevel@tonic-gate 	return (1);
34540Sstevel@tonic-gate }
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate /*
34570Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
34580Sstevel@tonic-gate  */
3459251Slclee static int
3460251Slclee ppartid(void)
34610Sstevel@tonic-gate {
34620Sstevel@tonic-gate 	char	*p, s[80];
34630Sstevel@tonic-gate 	int	i;
34640Sstevel@tonic-gate 
34650Sstevel@tonic-gate 	for (;;) {
3466251Slclee 		(void) printf(Q_LINE);
3467251Slclee 		(void) printf("Specify the partition number to change"
34685169Slclee 		    " (or enter 0 to exit): ");
3469251Slclee 		if (!fgets(s, sizeof (s), stdin))
3470251Slclee 			return (1);
34710Sstevel@tonic-gate 		i = strtol(s, &p, 10);
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
3474251Slclee 			(void) printf(E_LINE);
3475251Slclee 			(void) printf(
3476251Slclee 			    "Invalid response, retry the operation.\n");
34770Sstevel@tonic-gate 			continue;
34780Sstevel@tonic-gate 		}
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 		if (i == 0) {
34810Sstevel@tonic-gate 			/* exit delete command */
3482251Slclee 			(void) printf(E_LINE); /* clear error message */
34830Sstevel@tonic-gate 			return (1);
34840Sstevel@tonic-gate 		}
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 		i -= 1;
348711382SShidokht.Yadegari@Sun.COM 
34880Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
34890Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
34900Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
34910Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
34920Sstevel@tonic-gate 		} else {
3493251Slclee 			(void) printf(E_LINE);
3494251Slclee 			(void) printf(
3495251Slclee 			    "Partition %d is not a Solaris partition.",
34960Sstevel@tonic-gate 			    i + 1);
34970Sstevel@tonic-gate 			continue;
34980Sstevel@tonic-gate 		}
34990Sstevel@tonic-gate 
3500251Slclee 		(void) printf(E_LINE);
3501251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
35020Sstevel@tonic-gate 		return (1);
35030Sstevel@tonic-gate 	}
35040Sstevel@tonic-gate }
35050Sstevel@tonic-gate 
35060Sstevel@tonic-gate /*
35070Sstevel@tonic-gate  * pdelete
35080Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
35090Sstevel@tonic-gate  */
3510251Slclee static char
3511251Slclee pdelete(void)
35120Sstevel@tonic-gate {
35130Sstevel@tonic-gate 	char s[80];
351411382SShidokht.Yadegari@Sun.COM 	int i;
35150Sstevel@tonic-gate 	char pactive;
35160Sstevel@tonic-gate 
3517251Slclee DEL1:	(void) printf(Q_LINE);
3518251Slclee 	(void) printf("Specify the partition number to delete"
35190Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
35209786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
35210Sstevel@tonic-gate 	rm_blanks(s);
35220Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
3523251Slclee 		(void) printf(E_LINE);	/* clear error message */
35240Sstevel@tonic-gate 		return (1);
35250Sstevel@tonic-gate 	}
35260Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
35279786SBarry.Harding@Sun.COM 	if (((s[1] != '\0') && (s[1] != '\n')) ||
35289786SBarry.Harding@Sun.COM 	    (i = atoi(s)) < 1 || i > FD_NUMPART) {
3529251Slclee 		(void) printf(E_LINE);
3530251Slclee 		(void) printf("Invalid response, retry the operation.\n");
35310Sstevel@tonic-gate 		goto DEL1;
35320Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
35330Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
35340Sstevel@tonic-gate 	}
35350Sstevel@tonic-gate 
35360Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
3537251Slclee 		(void) printf(E_LINE);
3538251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
35390Sstevel@tonic-gate 		return (-1);
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 
354210021SSheshadri.Vasudevan@Sun.COM #ifdef i386
354310021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_is_dos_extended(Table[i].systid) &&
354410021SSheshadri.Vasudevan@Sun.COM 	    (Table[i].relsect == fdisk_get_ext_beg_sec(epp)) &&
354510021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_logical_drive_count(epp)) {
354610021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
354710021SSheshadri.Vasudevan@Sun.COM 		(void) printf("There are logical drives inside the"
354810021SSheshadri.Vasudevan@Sun.COM 		    " extended partition\n");
354910021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure of proceeding with deletion ?"
355010021SSheshadri.Vasudevan@Sun.COM 		    " (type \"y\" or \"n\") ");
355110021SSheshadri.Vasudevan@Sun.COM 
355210021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
355310021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
355410021SSheshadri.Vasudevan@Sun.COM 			return (1);
355510021SSheshadri.Vasudevan@Sun.COM 		}
355610021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_mounted_logical_drives(epp) == FDISK_EMOUNTED) {
355710021SSheshadri.Vasudevan@Sun.COM 			(void) printf(Q_LINE);
355810021SSheshadri.Vasudevan@Sun.COM 			(void) printf("There are mounted logical drives. "
355910021SSheshadri.Vasudevan@Sun.COM 			    "Committing changes now can cause data loss or "
356010021SSheshadri.Vasudevan@Sun.COM 			    "corruption. Unmount all logical drives and then "
356110021SSheshadri.Vasudevan@Sun.COM 			    "try committing the changes again.\n");
356210021SSheshadri.Vasudevan@Sun.COM 			(void) printf("Press enter to continue.\n");
356310021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(s);
356410021SSheshadri.Vasudevan@Sun.COM 			return (1);
356510021SSheshadri.Vasudevan@Sun.COM 		}
3566*12505SShidokht.Yadegari@Sun.COM 		(void) fdisk_delete_ext_part(epp);
356710021SSheshadri.Vasudevan@Sun.COM 	} else {
356810021SSheshadri.Vasudevan@Sun.COM #endif
356910021SSheshadri.Vasudevan@Sun.COM 		(void) printf(Q_LINE);
357010021SSheshadri.Vasudevan@Sun.COM 		(void) printf("Are you sure you want to delete partition %d?"
357110021SSheshadri.Vasudevan@Sun.COM 		    " This will make all files and \n", i + 1);
357210021SSheshadri.Vasudevan@Sun.COM 		(void) printf("programs in this partition inaccessible (type"
357310021SSheshadri.Vasudevan@Sun.COM 		    " \"y\" or \"n\"). ");
357410021SSheshadri.Vasudevan@Sun.COM 
357510021SSheshadri.Vasudevan@Sun.COM 		(void) printf(E_LINE);
357610021SSheshadri.Vasudevan@Sun.COM 		if (! yesno()) {
357710021SSheshadri.Vasudevan@Sun.COM 			return (1);
357810021SSheshadri.Vasudevan@Sun.COM 		}
357910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
35800Sstevel@tonic-gate 	}
358110021SSheshadri.Vasudevan@Sun.COM #endif
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
35840Sstevel@tonic-gate 		pactive = 1;
35850Sstevel@tonic-gate 	} else {
35860Sstevel@tonic-gate 		pactive = 0;
35870Sstevel@tonic-gate 	}
35880Sstevel@tonic-gate 
358911382SShidokht.Yadegari@Sun.COM 	(void) memset(&Table[i], 0, sizeof (struct ipart));
359011382SShidokht.Yadegari@Sun.COM 
3591251Slclee 	(void) printf(E_LINE);
3592251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate 	if (pactive) {
35955169Slclee 		(void) printf(" This was the active partition.");
35960Sstevel@tonic-gate 	}
35970Sstevel@tonic-gate 
35980Sstevel@tonic-gate 	return (1);
35990Sstevel@tonic-gate }
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate /*
36020Sstevel@tonic-gate  * rm_blanks
36030Sstevel@tonic-gate  * Remove blanks from strings of user responses.
36040Sstevel@tonic-gate  */
3605251Slclee static void
3606251Slclee rm_blanks(char *s)
36070Sstevel@tonic-gate {
36080Sstevel@tonic-gate 	register int i, j;
36090Sstevel@tonic-gate 
36100Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
36110Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
36120Sstevel@tonic-gate 			continue;
36130Sstevel@tonic-gate 		else
36140Sstevel@tonic-gate 			/* Found first non-blank character of the string */
36150Sstevel@tonic-gate 			break;
36160Sstevel@tonic-gate 	}
36170Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
36180Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
36190Sstevel@tonic-gate 			/* Reached end of string */
36200Sstevel@tonic-gate 			return;
36210Sstevel@tonic-gate 		}
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate }
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate /*
36260Sstevel@tonic-gate  * getcyl
36270Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
36280Sstevel@tonic-gate  * string to a decimal value.
36290Sstevel@tonic-gate  */
3630251Slclee static int
3631251Slclee getcyl(void)
36320Sstevel@tonic-gate {
36330Sstevel@tonic-gate int slen, i, j;
36340Sstevel@tonic-gate unsigned int cyl;
36359786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
36360Sstevel@tonic-gate 	rm_blanks(s);
36370Sstevel@tonic-gate 	slen = strlen(s);
36389786SBarry.Harding@Sun.COM 	if (s[slen - 1] == '\n')
36399786SBarry.Harding@Sun.COM 		slen--;
36400Sstevel@tonic-gate 	j = 1;
36410Sstevel@tonic-gate 	cyl = 0;
3642251Slclee 	for (i = slen - 1; i >= 0; i--) {
36430Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
36440Sstevel@tonic-gate 			return (-1);
36450Sstevel@tonic-gate 		}
3646251Slclee 		cyl += (j * (s[i] - '0'));
36470Sstevel@tonic-gate 		j *= 10;
36480Sstevel@tonic-gate 	}
36490Sstevel@tonic-gate 	return (cyl);
36500Sstevel@tonic-gate }
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate /*
36530Sstevel@tonic-gate  * disptbl
36540Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
36550Sstevel@tonic-gate  * of the disk used for each partition.
36560Sstevel@tonic-gate  */
3657251Slclee static void
3658251Slclee disptbl(void)
36590Sstevel@tonic-gate {
36600Sstevel@tonic-gate 	int i;
36610Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
36620Sstevel@tonic-gate 	char *stat, *type;
36637563SPrasad.Singamsetty@Sun.COM 	int is_pmbr = 0;
36640Sstevel@tonic-gate 
36650Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
3666251Slclee 		(void) printf("WARNING: critical disk geometry information"
36675169Slclee 		    " missing!\n");
3668251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
36690Sstevel@tonic-gate 		exit(1);
36700Sstevel@tonic-gate 	}
36710Sstevel@tonic-gate 
3672251Slclee 	(void) printf(HOME);
3673251Slclee 	(void) printf(T_LINE);
3674251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
36759889SLarry.Liu@Sun.COM 	(void) printf("             Cylinder size is %d (%d byte) blocks\n\n",
36769889SLarry.Liu@Sun.COM 	    heads * sectors, sectsiz);
3677251Slclee 	(void) printf(
3678251Slclee 	    "                                               Cylinders\n");
3679251Slclee 	(void) printf(
3680251Slclee 	    "      Partition   Status    Type          Start   End   Length"
36810Sstevel@tonic-gate 	    "    %%\n");
3682251Slclee 	(void) printf(
3683251Slclee 	    "      =========   ======    ============  =====   ===   ======"
36840Sstevel@tonic-gate 	    "   ===");
368511382SShidokht.Yadegari@Sun.COM 
368611382SShidokht.Yadegari@Sun.COM 
36870Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
368811382SShidokht.Yadegari@Sun.COM 
36890Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
36900Sstevel@tonic-gate 			continue;
36910Sstevel@tonic-gate 		}
369211382SShidokht.Yadegari@Sun.COM 
36930Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
36945169Slclee 			stat = Actvstr;
36950Sstevel@tonic-gate 		else
36965169Slclee 			stat = NAstr;
36970Sstevel@tonic-gate 		switch (Table[i].systid) {
36980Sstevel@tonic-gate 		case UNIXOS:
36995169Slclee 			type = Ustr;
37005169Slclee 			break;
37010Sstevel@tonic-gate 		case SUNIXOS:
37025169Slclee 			type = SUstr;
370310682SSheshadri.Vasudevan@Sun.COM #ifdef i386
370410682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, Table[i].relsect,
370510682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
370610682SSheshadri.Vasudevan@Sun.COM 				type = LINSWAPstr;
370710682SSheshadri.Vasudevan@Sun.COM #endif
37085169Slclee 			break;
37090Sstevel@tonic-gate 		case SUNIXOS2:
37105169Slclee 			type = SU2str;
37115169Slclee 			break;
37120Sstevel@tonic-gate 		case X86BOOT:
37135169Slclee 			type = X86str;
37145169Slclee 			break;
37150Sstevel@tonic-gate 		case DOSOS12:
37165169Slclee 			type = Dstr;
37175169Slclee 			break;
37180Sstevel@tonic-gate 		case DOSOS16:
37195169Slclee 			type = D16str;
37205169Slclee 			break;
37210Sstevel@tonic-gate 		case EXTDOS:
37225169Slclee 			type = EDstr;
37235169Slclee 			break;
37240Sstevel@tonic-gate 		case DOSDATA:
37255169Slclee 			type = DDstr;
37265169Slclee 			break;
37270Sstevel@tonic-gate 		case DOSHUGE:
37285169Slclee 			type = DBstr;
37295169Slclee 			break;
37300Sstevel@tonic-gate 		case PCIXOS:
37315169Slclee 			type = PCstr;
37325169Slclee 			break;
37330Sstevel@tonic-gate 		case DIAGPART:
37345169Slclee 			type = DIAGstr;
37355169Slclee 			break;
37360Sstevel@tonic-gate 		case FDISK_IFS:
37375169Slclee 			type = IFSstr;
37385169Slclee 			break;
37390Sstevel@tonic-gate 		case FDISK_AIXBOOT:
37405169Slclee 			type = AIXstr;
37415169Slclee 			break;
37420Sstevel@tonic-gate 		case FDISK_AIXDATA:
37435169Slclee 			type = AIXDstr;
37445169Slclee 			break;
37450Sstevel@tonic-gate 		case FDISK_OS2BOOT:
37465169Slclee 			type = OS2str;
37475169Slclee 			break;
37480Sstevel@tonic-gate 		case FDISK_WINDOWS:
37495169Slclee 			type = WINstr;
37505169Slclee 			break;
37510Sstevel@tonic-gate 		case FDISK_EXT_WIN:
37525169Slclee 			type = EWINstr;
37535169Slclee 			break;
37540Sstevel@tonic-gate 		case FDISK_FAT95:
37555169Slclee 			type = FAT95str;
37565169Slclee 			break;
37570Sstevel@tonic-gate 		case FDISK_EXTLBA:
37585169Slclee 			type = EXTLstr;
37595169Slclee 			break;
37600Sstevel@tonic-gate 		case FDISK_LINUX:
37615169Slclee 			type = LINUXstr;
37625169Slclee 			break;
37630Sstevel@tonic-gate 		case FDISK_CPM:
37645169Slclee 			type = CPMstr;
37655169Slclee 			break;
376610021SSheshadri.Vasudevan@Sun.COM 		case FDISK_NOVELL2:
376710021SSheshadri.Vasudevan@Sun.COM 			type = NOV2str;
376810021SSheshadri.Vasudevan@Sun.COM 			break;
37690Sstevel@tonic-gate 		case FDISK_NOVELL3:
37705169Slclee 			type = NOVstr;
37715169Slclee 			break;
37720Sstevel@tonic-gate 		case FDISK_QNX4:
37735169Slclee 			type = QNXstr;
37745169Slclee 			break;
37750Sstevel@tonic-gate 		case FDISK_QNX42:
37765169Slclee 			type = QNX2str;
37775169Slclee 			break;
37780Sstevel@tonic-gate 		case FDISK_QNX43:
37795169Slclee 			type = QNX3str;
37805169Slclee 			break;
37810Sstevel@tonic-gate 		case FDISK_LINUXNAT:
37825169Slclee 			type = LINNATstr;
37835169Slclee 			break;
37840Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
37855169Slclee 			type = NTFSVOL1str;
37865169Slclee 			break;
37870Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
37885169Slclee 			type = NTFSVOL2str;
37895169Slclee 			break;
37900Sstevel@tonic-gate 		case FDISK_BSD:
37915169Slclee 			type = BSDstr;
37925169Slclee 			break;
37930Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
37945169Slclee 			type = NEXTSTEPstr;
37955169Slclee 			break;
37960Sstevel@tonic-gate 		case FDISK_BSDIFS:
37975169Slclee 			type = BSDIFSstr;
37985169Slclee 			break;
37990Sstevel@tonic-gate 		case FDISK_BSDISWAP:
38005169Slclee 			type = BSDISWAPstr;
38015169Slclee 			break;
38020Sstevel@tonic-gate 		case EFI_PMBR:
38035169Slclee 			type = EFIstr;
380410021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) == DK_MAX_2TB)
38057563SPrasad.Singamsetty@Sun.COM 				is_pmbr = 1;
38067563SPrasad.Singamsetty@Sun.COM 
38075169Slclee 			break;
38080Sstevel@tonic-gate 		default:
38095169Slclee 			type = Ostr;
38105169Slclee 			break;
38110Sstevel@tonic-gate 		}
381210021SSheshadri.Vasudevan@Sun.COM 		startcyl = LE_32(Table[i].relsect) /
38135936Sbharding 		    (unsigned long)(heads * sectors);
38147563SPrasad.Singamsetty@Sun.COM 
381510021SSheshadri.Vasudevan@Sun.COM 		if (LE_32(Table[i].numsect) == DK_MAX_2TB) {
38167563SPrasad.Singamsetty@Sun.COM 			endcyl = Numcyl - 1;
38177563SPrasad.Singamsetty@Sun.COM 			length = endcyl - startcyl + 1;
38187563SPrasad.Singamsetty@Sun.COM 		} else {
381910021SSheshadri.Vasudevan@Sun.COM 			length = LE_32(Table[i].numsect) /
38207563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors);
382110021SSheshadri.Vasudevan@Sun.COM 			if (LE_32(Table[i].numsect) %
38227563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors))
38237563SPrasad.Singamsetty@Sun.COM 				length++;
38247563SPrasad.Singamsetty@Sun.COM 			endcyl = startcyl + length - 1;
38257563SPrasad.Singamsetty@Sun.COM 		}
38267563SPrasad.Singamsetty@Sun.COM 
38277563SPrasad.Singamsetty@Sun.COM 		percent = length * 100 / Numcyl_usable;
38287563SPrasad.Singamsetty@Sun.COM 		if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
38297563SPrasad.Singamsetty@Sun.COM 			if ((remainder * 100 / Numcyl_usable) > 50) {
38300Sstevel@tonic-gate 				/* round up */
38310Sstevel@tonic-gate 				percent++;
38320Sstevel@tonic-gate 			}
38330Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
38340Sstevel@tonic-gate 			/* rounded down */
38350Sstevel@tonic-gate 		}
38360Sstevel@tonic-gate 		if (percent > 100)
38370Sstevel@tonic-gate 			percent = 100;
3838251Slclee 		(void) printf(
3839251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
3840251Slclee 		    "    %3d",
3841251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
38420Sstevel@tonic-gate 	}
38437563SPrasad.Singamsetty@Sun.COM 
38440Sstevel@tonic-gate 	/* Print warning message if table is empty */
384511382SShidokht.Yadegari@Sun.COM 	if (nopartdefined()) {
3846251Slclee 		(void) printf(W_LINE);
3847251Slclee 		(void) printf("WARNING: no partitions are defined!");
38480Sstevel@tonic-gate 	} else {
38490Sstevel@tonic-gate 		/* Clear the warning line */
3850251Slclee 		(void) printf(W_LINE);
38517563SPrasad.Singamsetty@Sun.COM 
38527563SPrasad.Singamsetty@Sun.COM 		/* Print warning if disk > 2TB and is not EFI PMBR */
38537563SPrasad.Singamsetty@Sun.COM 		if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
38547563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
38557563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
38560Sstevel@tonic-gate 	}
38570Sstevel@tonic-gate }
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate /*
38600Sstevel@tonic-gate  * print_Table
38610Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
38620Sstevel@tonic-gate  * the selected disk device.
38630Sstevel@tonic-gate  */
3864251Slclee static void
3865251Slclee print_Table(void)
3866251Slclee {
38670Sstevel@tonic-gate 	int i;
38680Sstevel@tonic-gate 
3869251Slclee 	(void) fprintf(stderr,
38700Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
38710Sstevel@tonic-gate 	    "   NUMSECT\n");
38720Sstevel@tonic-gate 
38730Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3874251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
3875251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
3876251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
3877251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
38785169Slclee 		(void) fprintf(stderr, "%-8d ",
38795169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
38800Sstevel@tonic-gate 
3881251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
3882251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
38835169Slclee 		(void) fprintf(stderr, "%-8d ",
38845169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
388510021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u ", LE_32(Table[i].relsect));
388610021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(stderr, "%-10u\n", LE_32(Table[i].numsect));
38870Sstevel@tonic-gate 
38880Sstevel@tonic-gate 	}
38890Sstevel@tonic-gate }
38900Sstevel@tonic-gate 
38910Sstevel@tonic-gate /*
38920Sstevel@tonic-gate  * copy_Table_to_Old_Table
38930Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
38940Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
38950Sstevel@tonic-gate  * ones compared when determining if Table has changed.
38960Sstevel@tonic-gate  */
3897251Slclee static void
3898251Slclee copy_Table_to_Old_Table(void)
38990Sstevel@tonic-gate {
39000Sstevel@tonic-gate 	int i;
39010Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
39025169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
39030Sstevel@tonic-gate 	}
39040Sstevel@tonic-gate }
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate /*
39070Sstevel@tonic-gate  * nulltbl
39080Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
39090Sstevel@tonic-gate  * fdisk table.
39100Sstevel@tonic-gate  */
3911251Slclee static void
3912251Slclee nulltbl(void)
39130Sstevel@tonic-gate {
39140Sstevel@tonic-gate 	int i;
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
39175169Slclee 		Table[i].systid = UNUSED;
391810021SSheshadri.Vasudevan@Sun.COM 		Table[i].numsect = LE_32(UNUSED);
391910021SSheshadri.Vasudevan@Sun.COM 		Table[i].relsect = LE_32(UNUSED);
39205169Slclee 		Table[i].bootid = 0;
39218904SBarry.Harding@Sun.COM 		skip_verify[i] = 0;
39220Sstevel@tonic-gate 	}
39230Sstevel@tonic-gate }
39240Sstevel@tonic-gate 
39250Sstevel@tonic-gate /*
39260Sstevel@tonic-gate  * copy_Bootblk_to_Table
39270Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
39280Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
39290Sstevel@tonic-gate  */
3930251Slclee static void
3931251Slclee copy_Bootblk_to_Table(void)
39320Sstevel@tonic-gate {
393311382SShidokht.Yadegari@Sun.COM 	int i;
39340Sstevel@tonic-gate 	char *bootptr;
39350Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
3938251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
39390Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
394010021SSheshadri.Vasudevan@Sun.COM 	if (LE_16(Bootblk->signature) != MBB_MAGIC)  {
39410Sstevel@tonic-gate 		/* Signature is missing */
39420Sstevel@tonic-gate 		nulltbl();
3943251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39440Sstevel@tonic-gate 		return;
39450Sstevel@tonic-gate 	}
39460Sstevel@tonic-gate 	/*
39470Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
39480Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
39490Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
39500Sstevel@tonic-gate 	 * those that are valid.
39510Sstevel@tonic-gate 	 */
39520Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
39530Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
39540Sstevel@tonic-gate 			/* Null entry */
395511382SShidokht.Yadegari@Sun.COM 			(void) memset(&Table[i], 0, sizeof (struct ipart));
39560Sstevel@tonic-gate 		} else {
395711382SShidokht.Yadegari@Sun.COM 			fill_ipart(bootptr, &Table[i]);
39580Sstevel@tonic-gate 		}
395911382SShidokht.Yadegari@Sun.COM 		bootptr += sizeof (struct ipart);
39600Sstevel@tonic-gate 	}
396111382SShidokht.Yadegari@Sun.COM 
39620Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
3963251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
39640Sstevel@tonic-gate 	copy_Table_to_Bootblk();
39650Sstevel@tonic-gate }
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate /*
39680Sstevel@tonic-gate  * fill_ipart
39690Sstevel@tonic-gate  * Initialize ipart structure values.
39700Sstevel@tonic-gate  */
3971251Slclee static void
39720Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
39730Sstevel@tonic-gate {
39740Sstevel@tonic-gate #ifdef sparc
39750Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
3976251Slclee 	partp->bootid	= getbyte(&bootptr);
3977251Slclee 	partp->beghead	= getbyte(&bootptr);
3978251Slclee 	partp->begsect	= getbyte(&bootptr);
3979251Slclee 	partp->begcyl	= getbyte(&bootptr);
3980251Slclee 	partp->systid	= getbyte(&bootptr);
3981251Slclee 	partp->endhead	= getbyte(&bootptr);
3982251Slclee 	partp->endsect	= getbyte(&bootptr);
3983251Slclee 	partp->endcyl	= getbyte(&bootptr);
3984251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
3985251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
39860Sstevel@tonic-gate #else
39870Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
39880Sstevel@tonic-gate #endif
39890Sstevel@tonic-gate }
39900Sstevel@tonic-gate 
39910Sstevel@tonic-gate /*
3992251Slclee  * getbyte, getlong
39930Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
39940Sstevel@tonic-gate  */
39950Sstevel@tonic-gate #ifdef sparc
3996251Slclee uchar_t
3997251Slclee getbyte(char **bp)
39980Sstevel@tonic-gate {
3999251Slclee 	uchar_t	b;
4000251Slclee 
4001251Slclee 	b = (uchar_t)**bp;
40020Sstevel@tonic-gate 	*bp = *bp + 1;
40030Sstevel@tonic-gate 	return (b);
40040Sstevel@tonic-gate }
40050Sstevel@tonic-gate 
4006251Slclee uint32_t
4007251Slclee getlong(char **bp)
40080Sstevel@tonic-gate {
4009251Slclee 	int32_t	b, bh, bl;
40100Sstevel@tonic-gate 
40110Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
40120Sstevel@tonic-gate 	*bp += 2;
40130Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
40140Sstevel@tonic-gate 	*bp += 2;
40150Sstevel@tonic-gate 
40160Sstevel@tonic-gate 	b = (bh << 16) | bl;
4017251Slclee 	return ((uint32_t)b);
40180Sstevel@tonic-gate }
40190Sstevel@tonic-gate #endif
40200Sstevel@tonic-gate 
40210Sstevel@tonic-gate /*
40220Sstevel@tonic-gate  * copy_Table_to_Bootblk
40239889SLarry.Liu@Sun.COM  * Copy the table into the boot record. Note that the unused
40240Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
40250Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
40260Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
40270Sstevel@tonic-gate  */
4028251Slclee static void
4029251Slclee copy_Table_to_Bootblk(void)
40300Sstevel@tonic-gate {
40310Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
40320Sstevel@tonic-gate 
40330Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
40340Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
40350Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
40360Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
40375169Slclee 		if (tbl_ptr->systid == UNUSED)
40385169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
40395169Slclee 		else
40405169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
40410Sstevel@tonic-gate 	}
404210021SSheshadri.Vasudevan@Sun.COM 	Bootblk->signature = LE_16(MBB_MAGIC);
40430Sstevel@tonic-gate }
40440Sstevel@tonic-gate 
40450Sstevel@tonic-gate /*
40460Sstevel@tonic-gate  * TableChanged
40470Sstevel@tonic-gate  * 	Check for any changes in the partition table.
40480Sstevel@tonic-gate  */
4049251Slclee static int
4050251Slclee TableChanged(void)
40510Sstevel@tonic-gate {
40520Sstevel@tonic-gate 	int i, changed;
40530Sstevel@tonic-gate 
40540Sstevel@tonic-gate 	changed = 0;
40550Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
40565169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
40575169Slclee 			/* Partition table changed, write back to disk */
40585169Slclee 			changed = 1;
40595169Slclee 		}
40600Sstevel@tonic-gate 	}
40610Sstevel@tonic-gate 
40620Sstevel@tonic-gate 	return (changed);
40630Sstevel@tonic-gate }
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate /*
40660Sstevel@tonic-gate  * ffile_write
40670Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
40680Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
40690Sstevel@tonic-gate  */
4070251Slclee static void
4071251Slclee ffile_write(char *file)
40720Sstevel@tonic-gate {
40730Sstevel@tonic-gate 	register int	i;
40740Sstevel@tonic-gate 	FILE *fp;
40750Sstevel@tonic-gate 
40760Sstevel@tonic-gate 	/*
40770Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
40780Sstevel@tonic-gate 	 * Open file and write it.
40790Sstevel@tonic-gate 	 */
40800Sstevel@tonic-gate 	if (file != (char *)stdout) {
40815169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
40825169Slclee 			(void) fprintf(stderr,
40835169Slclee 			    "fdisk: Cannot open output file %s.\n",
40845169Slclee 			    file);
40855169Slclee 			exit(1);
40865169Slclee 		}
40870Sstevel@tonic-gate 	}
40880Sstevel@tonic-gate 	else
40895169Slclee 		fp = stdout;
40900Sstevel@tonic-gate 
40910Sstevel@tonic-gate 	/*
40920Sstevel@tonic-gate 	 * Write the fdisk table information
40930Sstevel@tonic-gate 	 */
4094251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
4095251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
4096251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4097251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
4098251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
4099251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
4100251Slclee 	(void) fprintf(fp, "*\n");
41010Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
41020Sstevel@tonic-gate 	if (v_flag) {
4103251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
4104251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
4105251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
4106251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
4107251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
4108251Slclee 		(void) fprintf(fp, "*\n");
41090Sstevel@tonic-gate 	}
4110251Slclee 	(void) fprintf(fp, "* systid:\n");
4111251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
4112251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
4113251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
4114251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
4115251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
4116251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
4117251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
4118251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
4119251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
4120251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
4121251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
4122251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
4123251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
4124251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
4125251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
4126251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
4127251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
4128251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
4129251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
413010021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(fp, "*  100: FDISK_NOVELL2\n");
4131251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
4132251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
4133251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
4134251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
4135251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
4136251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
4137251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
4138251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
4139251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
4140251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
4141251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
4142251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
4143251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
4144251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
4145251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
4146251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
4147251Slclee 	(void) fprintf(fp, "*\n");
4148251Slclee 	(void) fprintf(fp,
41490Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
41507563SPrasad.Singamsetty@Sun.COM 	    "    Rsect      Numsect\n");
41517563SPrasad.Singamsetty@Sun.COM 
41520Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
415310021SSheshadri.Vasudevan@Sun.COM 		(void) fprintf(fp,
415410021SSheshadri.Vasudevan@Sun.COM 		    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
415510021SSheshadri.Vasudevan@Sun.COM 		    " %-10u\n",
415610021SSheshadri.Vasudevan@Sun.COM 		    Table[i].systid,
415710021SSheshadri.Vasudevan@Sun.COM 		    Table[i].bootid,
415810021SSheshadri.Vasudevan@Sun.COM 		    Table[i].beghead,
415910021SSheshadri.Vasudevan@Sun.COM 		    Table[i].begsect & 0x3f,
416010021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
416110021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
416210021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endhead,
416310021SSheshadri.Vasudevan@Sun.COM 		    Table[i].endsect & 0x3f,
416410021SSheshadri.Vasudevan@Sun.COM 		    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
416510021SSheshadri.Vasudevan@Sun.COM 		    0xc0) << 2)),
416610021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].relsect),
416710021SSheshadri.Vasudevan@Sun.COM 		    LE_32(Table[i].numsect));
416810021SSheshadri.Vasudevan@Sun.COM 	}
416910021SSheshadri.Vasudevan@Sun.COM #ifdef i386
417010021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_ext_part_exists(epp)) {
417110021SSheshadri.Vasudevan@Sun.COM 		struct ipart ext_tab;
417210021SSheshadri.Vasudevan@Sun.COM 		logical_drive_t *temp;
417310021SSheshadri.Vasudevan@Sun.COM 		uint32_t rsect, numsect, tempsect = 0;
417410021SSheshadri.Vasudevan@Sun.COM 		for (temp = fdisk_get_ld_head(epp); temp != NULL;
417510021SSheshadri.Vasudevan@Sun.COM 		    temp = temp->next) {
417610021SSheshadri.Vasudevan@Sun.COM 			ext_tab = temp->parts[0];
417710021SSheshadri.Vasudevan@Sun.COM 			rsect = tempsect + LE_32(ext_tab.relsect) +
417810021SSheshadri.Vasudevan@Sun.COM 			    fdisk_get_ext_beg_sec(epp);
417910021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(ext_tab.numsect);
418010021SSheshadri.Vasudevan@Sun.COM 			tempsect = LE_32(temp->parts[1].relsect);
4181251Slclee 			(void) fprintf(fp,
418210021SSheshadri.Vasudevan@Sun.COM 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d "
418310021SSheshadri.Vasudevan@Sun.COM 			    "%-7d %-8u %-8u\n",
418410021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.systid,
418510021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.bootid,
418610021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.beghead,
418710021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.begsect & 0x3f,
418810021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begcyl & 0xff) |
418910021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.begsect & 0xc0) << 2)),
419010021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endhead,
419110021SSheshadri.Vasudevan@Sun.COM 			    ext_tab.endsect & 0x3f,
419210021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endcyl & 0xff) |
419310021SSheshadri.Vasudevan@Sun.COM 			    ((ext_tab.endsect & 0xc0) << 2)),
419410021SSheshadri.Vasudevan@Sun.COM 			    rsect,
419510021SSheshadri.Vasudevan@Sun.COM 			    numsect);
419610021SSheshadri.Vasudevan@Sun.COM 		}
41970Sstevel@tonic-gate 	}
419810021SSheshadri.Vasudevan@Sun.COM #endif
419910021SSheshadri.Vasudevan@Sun.COM 
42000Sstevel@tonic-gate 	if (fp != stdout)
4201251Slclee 		(void) fclose(fp);
42020Sstevel@tonic-gate }
42030Sstevel@tonic-gate 
42040Sstevel@tonic-gate /*
42050Sstevel@tonic-gate  * fix_slice
42060Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
42070Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
42080Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
42090Sstevel@tonic-gate  */
4210251Slclee static void
4211251Slclee fix_slice(void)
42120Sstevel@tonic-gate {
42130Sstevel@tonic-gate 	int	i;
42147563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
42150Sstevel@tonic-gate 
42160Sstevel@tonic-gate 	if (io_image) {
4217251Slclee 		return;
42180Sstevel@tonic-gate 	}
42190Sstevel@tonic-gate 
42200Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
42210Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
42220Sstevel@tonic-gate 			/*
42230Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
42240Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
42250Sstevel@tonic-gate 			 * the partition.
42260Sstevel@tonic-gate 			 */
422710021SSheshadri.Vasudevan@Sun.COM 			numsect = LE_32(Table[i].numsect);
42280Sstevel@tonic-gate 			break;
42290Sstevel@tonic-gate 		}
42300Sstevel@tonic-gate 	}
42310Sstevel@tonic-gate 
42320Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
42330Sstevel@tonic-gate 		if (!io_nifdisk) {
42340Sstevel@tonic-gate 			(void) fprintf(stderr,
42350Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
42360Sstevel@tonic-gate 			    " checked.\n");
42370Sstevel@tonic-gate 		}
4238251Slclee 		return;
42390Sstevel@tonic-gate 	}
42400Sstevel@tonic-gate 
4241251Slclee 	if (readvtoc() != VTOC_OK) {
42420Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
42435169Slclee 	}
42445169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
42455169Slclee 		/* Special case for slice two (entire disk) */
42465169Slclee 		if (i == 2) {
42475169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
42485169Slclee 				(void) fprintf(stderr,
42497563SPrasad.Singamsetty@Sun.COM 				    "slice %d starts at %llu, is not at"
42505169Slclee 				    " start of partition",
42515169Slclee 				    i, disk_vtoc.v_part[i].p_start);
42525169Slclee 				if (!io_nifdisk) {
42535169Slclee 					(void) printf(" adjust ?:");
42545169Slclee 					if (yesno())
42550Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
42565169Slclee 				} else {
42575169Slclee 					disk_vtoc.v_part[i].p_start = 0;
42585169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42590Sstevel@tonic-gate 				}
42605169Slclee 
42615169Slclee 			}
42625169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
42635169Slclee 				(void) fprintf(stderr,
42647563SPrasad.Singamsetty@Sun.COM 				    "slice %d size %llu does not cover"
42655169Slclee 				    " complete partition",
42665169Slclee 				    i, disk_vtoc.v_part[i].p_size);
42675169Slclee 				if (!io_nifdisk) {
42685169Slclee 					(void) printf(" adjust ?:");
42695169Slclee 					if (yesno())
42700Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
42710Sstevel@tonic-gate 						    numsect;
42725169Slclee 				} else {
42735169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
42745169Slclee 					(void) fprintf(stderr, " adjusted!\n");
42750Sstevel@tonic-gate 				}
42765169Slclee 			}
42775169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
42785169Slclee 				(void) fprintf(stderr,
42795169Slclee 				    "slice %d tag was %d should be %d",
42805169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
42815169Slclee 				    V_BACKUP);
42825169Slclee 				if (!io_nifdisk) {
4283251Slclee 					(void) printf(" fix ?:");
42845169Slclee 					if (yesno())
42850Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
42860Sstevel@tonic-gate 						    V_BACKUP;
42875169Slclee 				} else {
42880Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
42890Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
42900Sstevel@tonic-gate 				}
42915169Slclee 			}
42925169Slclee 			continue;
42935169Slclee 		}
42945169Slclee 		if (io_ADJT) {
42955169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
42965169Slclee 			    disk_vtoc.v_part[i].p_start +
42975169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
42985169Slclee 				(void) fprintf(stderr,
42997563SPrasad.Singamsetty@Sun.COM 				    "slice %d (start %llu, end %llu)"
43005169Slclee 				    " is larger than the partition",
43015169Slclee 				    i, disk_vtoc.v_part[i].p_start,
43025169Slclee 				    disk_vtoc.v_part[i].p_start +
43035169Slclee 				    disk_vtoc.v_part[i].p_size);
43045169Slclee 				if (!io_nifdisk) {
43055169Slclee 					(void) printf(" remove ?:");
43065169Slclee 					if (yesno()) {
43070Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
43080Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
43090Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
43100Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
43110Sstevel@tonic-gate 					}
43120Sstevel@tonic-gate 				} else {
43135169Slclee 					disk_vtoc.v_part[i].p_size = 0;
43145169Slclee 					disk_vtoc.v_part[i].p_start = 0;
43155169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
43165169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
43170Sstevel@tonic-gate 					(void) fprintf(stderr,
43185169Slclee 					    " removed!\n");
43195169Slclee 				}
43205169Slclee 			}
43215169Slclee 			continue;
43225169Slclee 		}
43235169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
43245169Slclee 			(void) fprintf(stderr,
43257563SPrasad.Singamsetty@Sun.COM 			    "slice %d (start %llu) is larger than the "
43267563SPrasad.Singamsetty@Sun.COM 			    "partition", i, disk_vtoc.v_part[i].p_start);
43275169Slclee 			if (!io_nifdisk) {
43285169Slclee 				(void) printf(" remove ?:");
43295169Slclee 				if (yesno()) {
43305169Slclee 					disk_vtoc.v_part[i].p_size = 0;
43315169Slclee 					disk_vtoc.v_part[i].p_start = 0;
43325169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
43335169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
43340Sstevel@tonic-gate 				}
43355169Slclee 			} else {
43365169Slclee 				disk_vtoc.v_part[i].p_size = 0;
43375169Slclee 				disk_vtoc.v_part[i].p_start = 0;
43385169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
43395169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
43405169Slclee 				(void) fprintf(stderr,
43415169Slclee 				" removed!\n");
43425169Slclee 			}
43435169Slclee 		} else if (disk_vtoc.v_part[i].p_start
43445169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
43455169Slclee 			(void) fprintf(stderr,
43467563SPrasad.Singamsetty@Sun.COM 			    "slice %d (end %llu) is larger"
43475169Slclee 			    " than the partition",
43485169Slclee 			    i,
43495169Slclee 			    disk_vtoc.v_part[i].p_start +
43505169Slclee 			    disk_vtoc.v_part[i].p_size);
43515169Slclee 			if (!io_nifdisk) {
43525169Slclee 				(void) printf(" adjust ?:");
43535169Slclee 				if (yesno()) {
43545169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
43555169Slclee 				}
43565169Slclee 			} else {
43575169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
43585169Slclee 				(void) fprintf(stderr, " adjusted!\n");
43590Sstevel@tonic-gate 			}
43600Sstevel@tonic-gate 		}
43610Sstevel@tonic-gate 	}
43620Sstevel@tonic-gate #if 1		/* bh for now */
43630Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
43640Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
43650Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
43660Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
43670Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
43680Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
43690Sstevel@tonic-gate #endif
43700Sstevel@tonic-gate 
43710Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
43720Sstevel@tonic-gate 	if (!io_readonly)
4373251Slclee 		(void) writevtoc();
43740Sstevel@tonic-gate }
43750Sstevel@tonic-gate 
43760Sstevel@tonic-gate /*
43770Sstevel@tonic-gate  * yesno
43780Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
43790Sstevel@tonic-gate  */
43800Sstevel@tonic-gate 
4381251Slclee static int
4382251Slclee yesno(void)
43830Sstevel@tonic-gate {
43840Sstevel@tonic-gate 	char	s[80];
43850Sstevel@tonic-gate 
43860Sstevel@tonic-gate 	for (;;) {
43879786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
43880Sstevel@tonic-gate 		rm_blanks(s);
43899786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
43909786SBarry.Harding@Sun.COM 		    ((s[0] != 'y') && (s[0] != 'n'))) {
4391251Slclee 			(void) printf(E_LINE);
4392251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
43930Sstevel@tonic-gate 			continue;
43940Sstevel@tonic-gate 		}
43950Sstevel@tonic-gate 		if (s[0] == 'y')
43960Sstevel@tonic-gate 			return (1);
43970Sstevel@tonic-gate 		else
43980Sstevel@tonic-gate 			return (0);
43990Sstevel@tonic-gate 	}
44000Sstevel@tonic-gate }
44010Sstevel@tonic-gate 
44020Sstevel@tonic-gate /*
44030Sstevel@tonic-gate  * readvtoc
44040Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
44050Sstevel@tonic-gate  */
4406251Slclee static int
4407251Slclee readvtoc(void)
44080Sstevel@tonic-gate {
44090Sstevel@tonic-gate 	int	i;
44100Sstevel@tonic-gate 	int	retval = VTOC_OK;
44110Sstevel@tonic-gate 
44127563SPrasad.Singamsetty@Sun.COM 	if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
44130Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44140Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
44150Sstevel@tonic-gate 			vt_inval++;
44160Sstevel@tonic-gate 			retval = VTOC_INVAL;
44170Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44180Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44195169Slclee 			    "GPT\n");
44200Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44210Sstevel@tonic-gate 		} else {
44220Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
44230Sstevel@tonic-gate 			retval = VTOC_RWERR;
44240Sstevel@tonic-gate 		}
44250Sstevel@tonic-gate 	}
44260Sstevel@tonic-gate 	return (retval);
44270Sstevel@tonic-gate }
44280Sstevel@tonic-gate 
44290Sstevel@tonic-gate /*
44300Sstevel@tonic-gate  * writevtoc
44310Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
44320Sstevel@tonic-gate  */
4433251Slclee static int
4434251Slclee writevtoc(void)
44350Sstevel@tonic-gate {
44360Sstevel@tonic-gate 	int	i;
44370Sstevel@tonic-gate 	int	retval = 0;
44380Sstevel@tonic-gate 
44397563SPrasad.Singamsetty@Sun.COM 	if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
44400Sstevel@tonic-gate 		if (i == VT_EINVAL) {
44410Sstevel@tonic-gate 			(void) fprintf(stderr,
44420Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
44430Sstevel@tonic-gate 			retval = VTOC_INVAL;
44440Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
44450Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
44465169Slclee 			    "GPT\n");
44470Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
44480Sstevel@tonic-gate 		} else {
44490Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
44500Sstevel@tonic-gate 			retval = VTOC_RWERR;
44510Sstevel@tonic-gate 		}
44520Sstevel@tonic-gate 	}
44530Sstevel@tonic-gate 	return (retval);
44540Sstevel@tonic-gate }
44550Sstevel@tonic-gate 
44560Sstevel@tonic-gate /*
44570Sstevel@tonic-gate  * efi_ioctl
44580Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
44590Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
44600Sstevel@tonic-gate  */
44610Sstevel@tonic-gate static int
44620Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
44630Sstevel@tonic-gate {
44640Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
44650Sstevel@tonic-gate 	int error;
44660Sstevel@tonic-gate 
44670Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
44680Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
44690Sstevel@tonic-gate 
44700Sstevel@tonic-gate 	return (error);
44710Sstevel@tonic-gate }
44720Sstevel@tonic-gate 
44730Sstevel@tonic-gate /*
44740Sstevel@tonic-gate  * clear_efi
44750Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
44760Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
44770Sstevel@tonic-gate  */
4478251Slclee static int
4479251Slclee clear_efi(void)
44800Sstevel@tonic-gate {
44810Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
44820Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
44830Sstevel@tonic-gate 
44840Sstevel@tonic-gate 	/*
44850Sstevel@tonic-gate 	 * see if we can read the EFI label
44860Sstevel@tonic-gate 	 */
44870Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
44880Sstevel@tonic-gate 		return (VT_ERROR);
44890Sstevel@tonic-gate 	}
44900Sstevel@tonic-gate 
44910Sstevel@tonic-gate 	/*
44920Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
44930Sstevel@tonic-gate 	 */
44940Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
44950Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
44960Sstevel@tonic-gate 
44970Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
44980Sstevel@tonic-gate 		return (VT_ERROR);
44990Sstevel@tonic-gate 	}
45000Sstevel@tonic-gate 
45010Sstevel@tonic-gate 	/*
45020Sstevel@tonic-gate 	 * clear the primary label
45030Sstevel@tonic-gate 	 */
45040Sstevel@tonic-gate 	if (io_debug) {
4505251Slclee 		(void) fprintf(stderr,
4506251Slclee 		    "\tClearing primary EFI label at block %lld\n",
4507251Slclee 		    dk_ioc.dki_lba);
45080Sstevel@tonic-gate 	}
45090Sstevel@tonic-gate 
45100Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45110Sstevel@tonic-gate 		free(dk_ioc.dki_data);
45120Sstevel@tonic-gate 		switch (errno) {
45130Sstevel@tonic-gate 			case EIO:
45140Sstevel@tonic-gate 				return (VT_EIO);
45150Sstevel@tonic-gate 			case EINVAL:
45160Sstevel@tonic-gate 				return (VT_EINVAL);
45170Sstevel@tonic-gate 			default:
45180Sstevel@tonic-gate 				return (VT_ERROR);
45190Sstevel@tonic-gate 		}
45200Sstevel@tonic-gate 	}
45210Sstevel@tonic-gate 
45220Sstevel@tonic-gate 	/*
45230Sstevel@tonic-gate 	 * clear the backup partition table
45240Sstevel@tonic-gate 	 */
45250Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
45260Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
45279889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
45289889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45290Sstevel@tonic-gate 	if (io_debug) {
4530251Slclee 		(void) fprintf(stderr,
4531251Slclee 		    "\tClearing backup partition table at block %lld\n",
4532251Slclee 		    dk_ioc.dki_lba);
45330Sstevel@tonic-gate 	}
45340Sstevel@tonic-gate 
45350Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
45360Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
45375169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
45385169Slclee 		    errno);
45390Sstevel@tonic-gate 	}
45400Sstevel@tonic-gate 
45410Sstevel@tonic-gate 	/*
45420Sstevel@tonic-gate 	 * clear the backup label
45430Sstevel@tonic-gate 	 */
45440Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
45450Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
45469889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
45479889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
45480Sstevel@tonic-gate 	if (io_debug) {
4549251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
4550251Slclee 		    "%lld\n", dk_ioc.dki_lba);
45510Sstevel@tonic-gate 	}
45520Sstevel@tonic-gate 
45530Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
4554251Slclee 		(void) fprintf(stderr,
4555251Slclee 		    "\tUnable to clear backup EFI label at "
4556251Slclee 		    "block %llu; errno %d\n",
4557251Slclee 		    efi_vtoc->efi_last_lba,
4558251Slclee 		    errno);
45590Sstevel@tonic-gate 	}
45600Sstevel@tonic-gate 
45610Sstevel@tonic-gate 	free(dk_ioc.dki_data);
45620Sstevel@tonic-gate 	efi_free(efi_vtoc);
45630Sstevel@tonic-gate 
45640Sstevel@tonic-gate 	return (0);
45650Sstevel@tonic-gate }
45660Sstevel@tonic-gate 
45670Sstevel@tonic-gate /*
45680Sstevel@tonic-gate  * clear_vtoc
45690Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
45700Sstevel@tonic-gate  *      device.
45710Sstevel@tonic-gate  */
4572251Slclee static void
45730Sstevel@tonic-gate clear_vtoc(int table, int part)
45740Sstevel@tonic-gate {
45750Sstevel@tonic-gate 	struct ipart *clr_table;
45769889SLarry.Liu@Sun.COM 	char *disk_label;
45777563SPrasad.Singamsetty@Sun.COM 	uint32_t pcyl, ncyl, count;
45787563SPrasad.Singamsetty@Sun.COM 	diskaddr_t backup_block, solaris_offset;
45797563SPrasad.Singamsetty@Sun.COM 	ssize_t bytes;
45806549Sbharding 	off_t seek_byte;
45810Sstevel@tonic-gate 
45820Sstevel@tonic-gate #ifdef DEBUG
45839889SLarry.Liu@Sun.COM 	char *read_label;
45840Sstevel@tonic-gate #endif /* DEBUG */
45850Sstevel@tonic-gate 
45860Sstevel@tonic-gate 	if (table == OLD) {
45870Sstevel@tonic-gate 		clr_table = &Old_Table[part];
45880Sstevel@tonic-gate 	} else {
45890Sstevel@tonic-gate 		clr_table = &Table[part];
45900Sstevel@tonic-gate 	}
45910Sstevel@tonic-gate 
45929889SLarry.Liu@Sun.COM 	disk_label = (char *)calloc(sectsiz, 1);
45939889SLarry.Liu@Sun.COM 	if (disk_label == NULL) {
45949889SLarry.Liu@Sun.COM 		return;
45959889SLarry.Liu@Sun.COM 	}
45960Sstevel@tonic-gate 
459710021SSheshadri.Vasudevan@Sun.COM 	seek_byte = (off_t)(LE_32(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
45980Sstevel@tonic-gate 
45990Sstevel@tonic-gate 	if (io_debug) {
46006549Sbharding 		(void) fprintf(stderr,
46016549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
46026549Sbharding 		    (uint64_t)seek_byte,
460310021SSheshadri.Vasudevan@Sun.COM 		    (uint64_t)(LE_32(clr_table->relsect) + VTOC_OFFSET));
46040Sstevel@tonic-gate 	}
46050Sstevel@tonic-gate 
46060Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4607251Slclee 		(void) fprintf(stderr,
46086549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
46096549Sbharding 		    (uint64_t)seek_byte);
46109889SLarry.Liu@Sun.COM 		free(disk_label);
4611251Slclee 		return;
46120Sstevel@tonic-gate 	}
46130Sstevel@tonic-gate 
46149889SLarry.Liu@Sun.COM 	bytes = write(Dev, disk_label, sectsiz);
46159889SLarry.Liu@Sun.COM 
46169889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4617251Slclee 		(void) fprintf(stderr,
46187563SPrasad.Singamsetty@Sun.COM 		    "\tWarning: only %d bytes written to clear primary"
46197563SPrasad.Singamsetty@Sun.COM 		    " VTOC!\n", bytes);
46200Sstevel@tonic-gate 	}
46210Sstevel@tonic-gate 
46220Sstevel@tonic-gate #ifdef DEBUG
46230Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4624251Slclee 		(void) fprintf(stderr,
46256549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
46266549Sbharding 		    (uint64_t)seek_byte);
46279889SLarry.Liu@Sun.COM 		free(disk_label);
4628251Slclee 		return;
46290Sstevel@tonic-gate 	} else {
46306549Sbharding 		(void) fprintf(stderr,
46316549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
46326549Sbharding 		    (uint64_t)seek_byte);
46330Sstevel@tonic-gate 	}
46340Sstevel@tonic-gate 
46359889SLarry.Liu@Sun.COM 	read_label = (char *)calloc(sectsiz, 1);
46369889SLarry.Liu@Sun.COM 	if (read_label == NULL) {
46379889SLarry.Liu@Sun.COM 		free(disk_label);
46389889SLarry.Liu@Sun.COM 		return;
46399889SLarry.Liu@Sun.COM 	}
46409889SLarry.Liu@Sun.COM 
46419889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
46429889SLarry.Liu@Sun.COM 
46439889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4644251Slclee 		(void) fprintf(stderr,
4645251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
46460Sstevel@tonic-gate 		    bytes);
46470Sstevel@tonic-gate 	}
46480Sstevel@tonic-gate 
46499889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4650251Slclee 		(void) fprintf(stderr,
4651251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
46520Sstevel@tonic-gate 	} else {
4653251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
46540Sstevel@tonic-gate 		    "read_label\n");
46550Sstevel@tonic-gate 	}
46560Sstevel@tonic-gate #endif /* DEBUG */
46570Sstevel@tonic-gate 
46580Sstevel@tonic-gate 	/* Clear backup label */
465910021SSheshadri.Vasudevan@Sun.COM 	pcyl = LE_32(clr_table->numsect) / (heads * sectors);
466010021SSheshadri.Vasudevan@Sun.COM 	solaris_offset = LE_32(clr_table->relsect);
46610Sstevel@tonic-gate 	ncyl = pcyl - acyl;
46620Sstevel@tonic-gate 
46630Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
46640Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
46679889SLarry.Liu@Sun.COM 		seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz;
46680Sstevel@tonic-gate 
46690Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4670251Slclee 			(void) fprintf(stderr,
46716549Sbharding 			    "\tError seeking to backup label at byte %llu on "
46726549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
46739889SLarry.Liu@Sun.COM 			free(disk_label);
46749889SLarry.Liu@Sun.COM #ifdef DEBUG
46759889SLarry.Liu@Sun.COM 			free(read_label);
46769889SLarry.Liu@Sun.COM #endif /* DEBUG */
4677251Slclee 			return;
46780Sstevel@tonic-gate 		}
46790Sstevel@tonic-gate 
46800Sstevel@tonic-gate 		if (io_debug) {
4681251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
46826549Sbharding 			    " byte %llu (block %llu)\n",
46836549Sbharding 			    (uint64_t)seek_byte,
46846549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46850Sstevel@tonic-gate 		}
46860Sstevel@tonic-gate 
46879889SLarry.Liu@Sun.COM 		bytes = write(Dev, disk_label, sectsiz);
46889889SLarry.Liu@Sun.COM 
46899889SLarry.Liu@Sun.COM 		if (bytes != sectsiz) {
4690251Slclee 			(void) fprintf(stderr,
4691251Slclee 			    "\t\tWarning: only %d bytes written to "
46926549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
46936549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
46940Sstevel@tonic-gate 		}
46950Sstevel@tonic-gate 
46960Sstevel@tonic-gate #ifdef DEBUG
46970Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
4698251Slclee 		(void) fprintf(stderr,
46996549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
47006549Sbharding 		    (uint64_t)seek_byte);
47019889SLarry.Liu@Sun.COM 		free(disk_label);
47029889SLarry.Liu@Sun.COM 		free(read_label);
4703251Slclee 		return;
47040Sstevel@tonic-gate 	} else {
47056549Sbharding 		(void) fprintf(stderr,
47066549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
47076549Sbharding 		    (uint64_t)seek_byte);
47080Sstevel@tonic-gate 	}
47090Sstevel@tonic-gate 
47109889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
47119889SLarry.Liu@Sun.COM 
47129889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
4713251Slclee 		(void) fprintf(stderr,
4714251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
4715251Slclee 		    bytes);
47160Sstevel@tonic-gate 	}
47170Sstevel@tonic-gate 
47189889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
4719251Slclee 		(void) fprintf(stderr,
4720251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
47210Sstevel@tonic-gate 	} else {
4722251Slclee 		(void) fprintf(stderr,
4723251Slclee 		    "DEBUG: Good compare of disk_label and backup "
47240Sstevel@tonic-gate 		    "read_label\n");
47250Sstevel@tonic-gate 	}
47269889SLarry.Liu@Sun.COM 
47270Sstevel@tonic-gate #endif /* DEBUG */
47280Sstevel@tonic-gate 
47290Sstevel@tonic-gate 		backup_block += 2;
47300Sstevel@tonic-gate 	}
47319889SLarry.Liu@Sun.COM 
47329889SLarry.Liu@Sun.COM #ifdef DEBUG
47339889SLarry.Liu@Sun.COM 	free(read_label);
47349889SLarry.Liu@Sun.COM #endif /* DEBUG */
47359889SLarry.Liu@Sun.COM 	free(disk_label);
47360Sstevel@tonic-gate }
47370Sstevel@tonic-gate 
47380Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
47390Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
47400Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
47410Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
47420Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
47430Sstevel@tonic-gate 
47440Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
47450Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
47460Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
47470Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
47480Sstevel@tonic-gate 
47490Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
47500Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
47510Sstevel@tonic-gate 
47520Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
47530Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
47540Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47550Sstevel@tonic-gate 
47560Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
47570Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
47580Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
47590Sstevel@tonic-gate 
47600Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
47610Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
47620Sstevel@tonic-gate 
47630Sstevel@tonic-gate /*
47640Sstevel@tonic-gate  *  lecture_and_query
47650Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
47660Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
47670Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
47680Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
47690Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
47700Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
47710Sstevel@tonic-gate  */
4772251Slclee static int
47730Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
47740Sstevel@tonic-gate {
47750Sstevel@tonic-gate 	if (io_nifdisk)
47760Sstevel@tonic-gate 		return (0);
47770Sstevel@tonic-gate 
4778251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
4779251Slclee 	(void) fprintf(stderr, "%s", warning);
4780251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
4781251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
47820Sstevel@tonic-gate 
47830Sstevel@tonic-gate 	return (yesno());
47840Sstevel@tonic-gate }
47850Sstevel@tonic-gate 
4786251Slclee static void
47870Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
47880Sstevel@tonic-gate {
47897563SPrasad.Singamsetty@Sun.COM 	struct extvtoc v;
47900Sstevel@tonic-gate 	struct dk_geom d;
47910Sstevel@tonic-gate 	struct part_info pi;
47927563SPrasad.Singamsetty@Sun.COM 	struct extpart_info extpi;
47937563SPrasad.Singamsetty@Sun.COM 	diskaddr_t totsize;
47940Sstevel@tonic-gate 	int idx = -1;
47950Sstevel@tonic-gate 
47960Sstevel@tonic-gate 	/*
47970Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
47980Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
47990Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
48000Sstevel@tonic-gate 	 */
48017563SPrasad.Singamsetty@Sun.COM 	if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
48027563SPrasad.Singamsetty@Sun.COM 		if (extpi.p_start != 0) {
48037563SPrasad.Singamsetty@Sun.COM 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
48047563SPrasad.Singamsetty@Sun.COM 			    devname)) {
48057563SPrasad.Singamsetty@Sun.COM 				(void) close(fd);
48067563SPrasad.Singamsetty@Sun.COM 				exit(1);
48077563SPrasad.Singamsetty@Sun.COM 			}
48087563SPrasad.Singamsetty@Sun.COM 		}
48097563SPrasad.Singamsetty@Sun.COM 	} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
48100Sstevel@tonic-gate 		if (pi.p_start != 0) {
48110Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
48120Sstevel@tonic-gate 			    devname)) {
48130Sstevel@tonic-gate 				(void) close(fd);
48140Sstevel@tonic-gate 				exit(1);
48150Sstevel@tonic-gate 			}
48160Sstevel@tonic-gate 		}
48170Sstevel@tonic-gate 	} else {
48187563SPrasad.Singamsetty@Sun.COM 		if ((idx = read_extvtoc(fd, &v)) < 0) {
48190Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
48200Sstevel@tonic-gate 			    devname)) {
48210Sstevel@tonic-gate 				(void) close(fd);
48220Sstevel@tonic-gate 				exit(1);
48230Sstevel@tonic-gate 			}
48240Sstevel@tonic-gate 			return;
48250Sstevel@tonic-gate 		}
48260Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
48270Sstevel@tonic-gate 			perror(devname);
48280Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
48290Sstevel@tonic-gate 			    devname)) {
48300Sstevel@tonic-gate 				(void) close(fd);
48310Sstevel@tonic-gate 				exit(1);
48320Sstevel@tonic-gate 			}
48330Sstevel@tonic-gate 			return;
48340Sstevel@tonic-gate 		}
48357563SPrasad.Singamsetty@Sun.COM 		totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
48360Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
48370Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
48380Sstevel@tonic-gate 			    devname)) {
48390Sstevel@tonic-gate 				(void) close(fd);
48400Sstevel@tonic-gate 				exit(1);
48410Sstevel@tonic-gate 			}
48420Sstevel@tonic-gate 		}
48430Sstevel@tonic-gate 	}
48440Sstevel@tonic-gate }
48450Sstevel@tonic-gate 
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate /*
48480Sstevel@tonic-gate  * get_node
48490Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
48500Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
48510Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
48520Sstevel@tonic-gate  */
48530Sstevel@tonic-gate static char *
48540Sstevel@tonic-gate get_node(char *devname)
48550Sstevel@tonic-gate {
48560Sstevel@tonic-gate 	char *node;
48570Sstevel@tonic-gate 	struct stat statbuf;
48580Sstevel@tonic-gate 	size_t space;
48590Sstevel@tonic-gate 
48600Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
48610Sstevel@tonic-gate 	if (io_image)
48620Sstevel@tonic-gate 		return (devname);
48630Sstevel@tonic-gate 
48640Sstevel@tonic-gate 	node = devname;
48650Sstevel@tonic-gate 
48660Sstevel@tonic-gate 	/* Try the node as provided first */
48670Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
48680Sstevel@tonic-gate 		/*
48690Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
48700Sstevel@tonic-gate 		 * default path and try again.
48710Sstevel@tonic-gate 		 */
48720Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
48730Sstevel@tonic-gate 
48740Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
4875251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
48760Sstevel@tonic-gate 			    "for device node.\n");
48770Sstevel@tonic-gate 			exit(1);
48780Sstevel@tonic-gate 		}
48790Sstevel@tonic-gate 
48800Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
48810Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
48820Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
48830Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
48840Sstevel@tonic-gate 
48850Sstevel@tonic-gate 		/* Try to stat it again */
48860Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
48870Sstevel@tonic-gate 			/* Failed all options, give up */
4888251Slclee 			(void) fprintf(stderr,
4889251Slclee 			    "fdisk: Cannot stat device %s.\n",
48900Sstevel@tonic-gate 			    devname);
48910Sstevel@tonic-gate 			exit(1);
48920Sstevel@tonic-gate 		}
48930Sstevel@tonic-gate 	}
48940Sstevel@tonic-gate 
48950Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
48960Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
4897251Slclee 		(void) fprintf(stderr,
4898251Slclee 		    "fdisk: %s must be a raw device.\n", node);
48990Sstevel@tonic-gate 		exit(1);
49000Sstevel@tonic-gate 	}
49010Sstevel@tonic-gate 
49020Sstevel@tonic-gate 	return (node);
49030Sstevel@tonic-gate }
490410021SSheshadri.Vasudevan@Sun.COM 
490510021SSheshadri.Vasudevan@Sun.COM #ifdef i386
490610021SSheshadri.Vasudevan@Sun.COM static void
490710021SSheshadri.Vasudevan@Sun.COM preach_and_continue()
490810021SSheshadri.Vasudevan@Sun.COM {
490910021SSheshadri.Vasudevan@Sun.COM 	(void) fprintf(stderr, "There are mounted logical drives. Committing "
491010021SSheshadri.Vasudevan@Sun.COM 	    "changes now can lead to inconsistancy in internal system state "
491110021SSheshadri.Vasudevan@Sun.COM 	    "which can eventually cause data loss or corruption. Unmount all "
491210021SSheshadri.Vasudevan@Sun.COM 	    "logical drives and try committing the changes again.\n");
491310021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(s);
491410021SSheshadri.Vasudevan@Sun.COM }
491510021SSheshadri.Vasudevan@Sun.COM 
491610021SSheshadri.Vasudevan@Sun.COM /*
491710021SSheshadri.Vasudevan@Sun.COM  * Convert a given partition ID to an descriptive string.
491810021SSheshadri.Vasudevan@Sun.COM  * Just an index into the partition types table.
491910021SSheshadri.Vasudevan@Sun.COM  */
492010021SSheshadri.Vasudevan@Sun.COM void
492110021SSheshadri.Vasudevan@Sun.COM id_to_name(uchar_t sysid, char *buffer)
492210021SSheshadri.Vasudevan@Sun.COM {
4923*12505SShidokht.Yadegari@Sun.COM 	(void) strcpy(buffer, fdisk_part_types[sysid]);
492410021SSheshadri.Vasudevan@Sun.COM }
492510021SSheshadri.Vasudevan@Sun.COM 
492610021SSheshadri.Vasudevan@Sun.COM /*
492710021SSheshadri.Vasudevan@Sun.COM  * Procedure to check the validity of the extended partition menu option
492810021SSheshadri.Vasudevan@Sun.COM  * entered by the user
492910021SSheshadri.Vasudevan@Sun.COM  */
493010021SSheshadri.Vasudevan@Sun.COM static int
493110021SSheshadri.Vasudevan@Sun.COM ext_invalid_option(char ch)
493210021SSheshadri.Vasudevan@Sun.COM {
493310021SSheshadri.Vasudevan@Sun.COM 	char *p;
493410021SSheshadri.Vasudevan@Sun.COM 
493510021SSheshadri.Vasudevan@Sun.COM 	p = strchr(ext_part_menu_opts, tolower(ch));
493610021SSheshadri.Vasudevan@Sun.COM 
493710021SSheshadri.Vasudevan@Sun.COM 	if (p == NULL) {
493810021SSheshadri.Vasudevan@Sun.COM 		return (1);
493910021SSheshadri.Vasudevan@Sun.COM 	}
494010021SSheshadri.Vasudevan@Sun.COM 	return (0);
494110021SSheshadri.Vasudevan@Sun.COM }
494210021SSheshadri.Vasudevan@Sun.COM 
494310021SSheshadri.Vasudevan@Sun.COM /*
494410021SSheshadri.Vasudevan@Sun.COM  * Read 16 bytes of the input (assuming that no valid user input spans more
494510021SSheshadri.Vasudevan@Sun.COM  * than that). Flush the input stream, so that the next read does not reap
494610021SSheshadri.Vasudevan@Sun.COM  * stale data from the previous input that was not processed.
494710021SSheshadri.Vasudevan@Sun.COM  * Note that fgets also reads the trailing '\n'
494810021SSheshadri.Vasudevan@Sun.COM  */
494910021SSheshadri.Vasudevan@Sun.COM static void
495010021SSheshadri.Vasudevan@Sun.COM ext_read_input(char *buf)
495110021SSheshadri.Vasudevan@Sun.COM {
4952*12505SShidokht.Yadegari@Sun.COM 	(void) fgets(buf, 16, stdin);
4953*12505SShidokht.Yadegari@Sun.COM 	(void) fflush(stdin);
495410021SSheshadri.Vasudevan@Sun.COM }
495510021SSheshadri.Vasudevan@Sun.COM 
495610021SSheshadri.Vasudevan@Sun.COM /*
495710021SSheshadri.Vasudevan@Sun.COM  * Procedure to read and validate the user option at the extended partition menu
495810021SSheshadri.Vasudevan@Sun.COM  */
495910021SSheshadri.Vasudevan@Sun.COM static int
496010021SSheshadri.Vasudevan@Sun.COM ext_read_options(char *buf)
496110021SSheshadri.Vasudevan@Sun.COM {
496210021SSheshadri.Vasudevan@Sun.COM 	ext_read_input(buf);
496310021SSheshadri.Vasudevan@Sun.COM 	if ((strlen(buf) != 2) || (ext_invalid_option(buf[0]))) {
4964*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nUnknown Command\n");
496510021SSheshadri.Vasudevan@Sun.COM 		return (-1);
496610021SSheshadri.Vasudevan@Sun.COM 	}
496710021SSheshadri.Vasudevan@Sun.COM 	return (0);
496810021SSheshadri.Vasudevan@Sun.COM }
496910021SSheshadri.Vasudevan@Sun.COM 
497010021SSheshadri.Vasudevan@Sun.COM /*
497110021SSheshadri.Vasudevan@Sun.COM  * Procedure to print the list of known partition types and their IDs
497210021SSheshadri.Vasudevan@Sun.COM  */
497310021SSheshadri.Vasudevan@Sun.COM static void
497410021SSheshadri.Vasudevan@Sun.COM ext_print_part_types()
497510021SSheshadri.Vasudevan@Sun.COM {
497610021SSheshadri.Vasudevan@Sun.COM 	int i, rowmax, rowcount = 1;
497710021SSheshadri.Vasudevan@Sun.COM 	struct winsize ws;
497810021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
497910021SSheshadri.Vasudevan@Sun.COM 
498010021SSheshadri.Vasudevan@Sun.COM 	/* Get the current window dimensions */
498110021SSheshadri.Vasudevan@Sun.COM 	if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) {
498210021SSheshadri.Vasudevan@Sun.COM 		perror("ioctl");
498310021SSheshadri.Vasudevan@Sun.COM 		rowmax = 20;
498410021SSheshadri.Vasudevan@Sun.COM 	} else {
498510021SSheshadri.Vasudevan@Sun.COM 		/*
498610021SSheshadri.Vasudevan@Sun.COM 		 * Accommodate the initial headings by reducing the number of
498710021SSheshadri.Vasudevan@Sun.COM 		 * partition IDs being printed.
498810021SSheshadri.Vasudevan@Sun.COM 		 */
498910021SSheshadri.Vasudevan@Sun.COM 		rowmax = ws.ws_row - 5;
499010021SSheshadri.Vasudevan@Sun.COM 	}
499110021SSheshadri.Vasudevan@Sun.COM 
499210021SSheshadri.Vasudevan@Sun.COM 	if (rowmax < 3) {
4993*12505SShidokht.Yadegari@Sun.COM 		(void) fprintf(stderr, "Window size too small."
499410021SSheshadri.Vasudevan@Sun.COM 		    " Try resizing the window\n");
499510021SSheshadri.Vasudevan@Sun.COM 		return;
499610021SSheshadri.Vasudevan@Sun.COM 	}
499710021SSheshadri.Vasudevan@Sun.COM 
4998*12505SShidokht.Yadegari@Sun.COM 	(void) printf("List of known partition types : \n");
4999*12505SShidokht.Yadegari@Sun.COM 	(void) printf("PartID          Partition Type\n");
5000*12505SShidokht.Yadegari@Sun.COM 	(void) printf("======          ==============\n");
500110021SSheshadri.Vasudevan@Sun.COM 	for (i = 0; i <= FDISK_MAX_VALID_PART_ID; i++) {
5002*12505SShidokht.Yadegari@Sun.COM 		(void) printf("%-3d          %s\n", i, fdisk_part_types[i]);
500310021SSheshadri.Vasudevan@Sun.COM 		rowcount++;
500410021SSheshadri.Vasudevan@Sun.COM 		if (rowcount == rowmax) {
500510021SSheshadri.Vasudevan@Sun.COM 			/*
500610021SSheshadri.Vasudevan@Sun.COM 			 * After the initial screen, use all the rows for
500710021SSheshadri.Vasudevan@Sun.COM 			 * printing the partition IDs, but one.
500810021SSheshadri.Vasudevan@Sun.COM 			 */
500910021SSheshadri.Vasudevan@Sun.COM 			rowmax = ws.ws_row - 1;
5010*12505SShidokht.Yadegari@Sun.COM 			(void) fprintf(stderr, "\nPress enter to see next "
5011*12505SShidokht.Yadegari@Sun.COM 			    "page or 'q' to quit : ");
501210021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
501310021SSheshadri.Vasudevan@Sun.COM 			if ((strlen(buf) == 2) && (tolower(buf[0]) == 'q')) {
501410021SSheshadri.Vasudevan@Sun.COM 				return;
501510021SSheshadri.Vasudevan@Sun.COM 			}
501610021SSheshadri.Vasudevan@Sun.COM 			rowcount = 1;
501710021SSheshadri.Vasudevan@Sun.COM 		}
501810021SSheshadri.Vasudevan@Sun.COM 	}
501910021SSheshadri.Vasudevan@Sun.COM }
502010021SSheshadri.Vasudevan@Sun.COM 
502110021SSheshadri.Vasudevan@Sun.COM static void
502210021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(int *pno)
502310021SSheshadri.Vasudevan@Sun.COM {
502410021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
502510021SSheshadri.Vasudevan@Sun.COM 	int len, i;
502610021SSheshadri.Vasudevan@Sun.COM 
502710021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
5028*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Enter the partition number : ");
502910021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
503010021SSheshadri.Vasudevan@Sun.COM 
503110021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
503210021SSheshadri.Vasudevan@Sun.COM 
503310021SSheshadri.Vasudevan@Sun.COM 		/* Check length of the input */
503410021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_NUM_DIGITS+1))) {
503510021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
503610021SSheshadri.Vasudevan@Sun.COM 		}
503710021SSheshadri.Vasudevan@Sun.COM 
503810021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
503910021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
504010021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
504110021SSheshadri.Vasudevan@Sun.COM 				goto print_error_and_continue;
504210021SSheshadri.Vasudevan@Sun.COM 			}
504310021SSheshadri.Vasudevan@Sun.COM 		}
504410021SSheshadri.Vasudevan@Sun.COM 
504510021SSheshadri.Vasudevan@Sun.COM 		*pno = atoi(buf);
504610021SSheshadri.Vasudevan@Sun.COM 
504710021SSheshadri.Vasudevan@Sun.COM 		if ((*pno <= FD_NUMPART) ||
504810021SSheshadri.Vasudevan@Sun.COM 		    *pno > (fdisk_get_logical_drive_count(epp) + FD_NUMPART)) {
504910021SSheshadri.Vasudevan@Sun.COM 			goto print_error_and_continue;
505010021SSheshadri.Vasudevan@Sun.COM 		}
505110021SSheshadri.Vasudevan@Sun.COM 
505210021SSheshadri.Vasudevan@Sun.COM 		break;
505310021SSheshadri.Vasudevan@Sun.COM print_error_and_continue:
5054*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Invalid partition number\n");
505510021SSheshadri.Vasudevan@Sun.COM 		continue;
505610021SSheshadri.Vasudevan@Sun.COM 	}
505710021SSheshadri.Vasudevan@Sun.COM }
505810021SSheshadri.Vasudevan@Sun.COM 
505910021SSheshadri.Vasudevan@Sun.COM static void
506010021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(uchar_t *partid)
506110021SSheshadri.Vasudevan@Sun.COM {
506210021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
506310021SSheshadri.Vasudevan@Sun.COM 	int len, i, id;
506410021SSheshadri.Vasudevan@Sun.COM 
506510021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
5066*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Enter the ID ( Type I for list of "
5067*12505SShidokht.Yadegari@Sun.COM 		    "partition IDs ) : ");
506810021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
506910021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
507010021SSheshadri.Vasudevan@Sun.COM 
507110021SSheshadri.Vasudevan@Sun.COM 		if ((len < 2) || (len > (FDISK_MAX_VALID_PART_ID_DIGITS + 1))) {
5072*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition ID\n");
507310021SSheshadri.Vasudevan@Sun.COM 			continue;
507410021SSheshadri.Vasudevan@Sun.COM 		}
507510021SSheshadri.Vasudevan@Sun.COM 
507610021SSheshadri.Vasudevan@Sun.COM 		if ((len == 2) && (toupper(buf[0]) == 'I')) {
507710021SSheshadri.Vasudevan@Sun.COM 			ext_print_part_types();
507810021SSheshadri.Vasudevan@Sun.COM 			continue;
507910021SSheshadri.Vasudevan@Sun.COM 		}
508010021SSheshadri.Vasudevan@Sun.COM 
508110021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
508210021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len-1; i++) {
508310021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
5084*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid partition ID\n");
508510021SSheshadri.Vasudevan@Sun.COM 				break;
508610021SSheshadri.Vasudevan@Sun.COM 			}
508710021SSheshadri.Vasudevan@Sun.COM 		}
508810021SSheshadri.Vasudevan@Sun.COM 
508910021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
509010021SSheshadri.Vasudevan@Sun.COM 			continue;
509110021SSheshadri.Vasudevan@Sun.COM 		}
509210021SSheshadri.Vasudevan@Sun.COM 
509310021SSheshadri.Vasudevan@Sun.COM 		/* Check if the (now) valid number is greater than the limit */
509410021SSheshadri.Vasudevan@Sun.COM 		if ((id = atoi(buf)) > FDISK_MAX_VALID_PART_ID) {
5095*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition ID\n");
509610021SSheshadri.Vasudevan@Sun.COM 			continue;
509710021SSheshadri.Vasudevan@Sun.COM 		}
509810021SSheshadri.Vasudevan@Sun.COM 
509910021SSheshadri.Vasudevan@Sun.COM 		*partid = (uchar_t)id;
510010021SSheshadri.Vasudevan@Sun.COM 
510110021SSheshadri.Vasudevan@Sun.COM 		/* Disallow multiple extended partitions */
510210021SSheshadri.Vasudevan@Sun.COM 		if (fdisk_is_dos_extended(*partid)) {
5103*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Multiple extended partitions not "
5104*12505SShidokht.Yadegari@Sun.COM 			    "allowed\n");
510510021SSheshadri.Vasudevan@Sun.COM 			continue;
510610021SSheshadri.Vasudevan@Sun.COM 		}
510710021SSheshadri.Vasudevan@Sun.COM 
510810021SSheshadri.Vasudevan@Sun.COM 		/* Disallow EFI partitions within extended partition */
510910021SSheshadri.Vasudevan@Sun.COM 		if (*partid == EFI_PMBR) {
5110*12505SShidokht.Yadegari@Sun.COM 			(void) printf("EFI partitions within an extended "
5111*12505SShidokht.Yadegari@Sun.COM 			    "partition is not allowed\n");
511210021SSheshadri.Vasudevan@Sun.COM 			continue;
511310021SSheshadri.Vasudevan@Sun.COM 		}
511410021SSheshadri.Vasudevan@Sun.COM 
511510021SSheshadri.Vasudevan@Sun.COM 		return; /* Valid partition ID is in partid */
511610021SSheshadri.Vasudevan@Sun.COM 	}
511710021SSheshadri.Vasudevan@Sun.COM }
511810021SSheshadri.Vasudevan@Sun.COM 
511910021SSheshadri.Vasudevan@Sun.COM static void
512010021SSheshadri.Vasudevan@Sun.COM delete_logical_drive()
512110021SSheshadri.Vasudevan@Sun.COM {
512210021SSheshadri.Vasudevan@Sun.COM 	int pno;
512310021SSheshadri.Vasudevan@Sun.COM 
512410021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
5125*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nNo logical drives defined.\n");
512610021SSheshadri.Vasudevan@Sun.COM 		return;
512710021SSheshadri.Vasudevan@Sun.COM 	}
512810021SSheshadri.Vasudevan@Sun.COM 
5129*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
513010021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
513110021SSheshadri.Vasudevan@Sun.COM 	fdisk_delete_logical_drive(epp, pno);
5132*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Partition %d deleted\n", pno);
513310021SSheshadri.Vasudevan@Sun.COM }
513410021SSheshadri.Vasudevan@Sun.COM 
513510021SSheshadri.Vasudevan@Sun.COM static int
513610021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_start(uint32_t *begsec)
513710021SSheshadri.Vasudevan@Sun.COM {
513810021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
513910021SSheshadri.Vasudevan@Sun.COM 	int ret, len, i;
514010021SSheshadri.Vasudevan@Sun.COM 	uint32_t begcyl;
514110021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_cyl;
514210021SSheshadri.Vasudevan@Sun.COM 	uint32_t first_free_sec;
514310021SSheshadri.Vasudevan@Sun.COM 
514410021SSheshadri.Vasudevan@Sun.COM 	ret = fdisk_ext_find_first_free_sec(epp, &first_free_sec);
514510021SSheshadri.Vasudevan@Sun.COM 	if (ret != FDISK_SUCCESS) {
514610021SSheshadri.Vasudevan@Sun.COM 		return (ret);
514710021SSheshadri.Vasudevan@Sun.COM 	}
514810021SSheshadri.Vasudevan@Sun.COM 
514910021SSheshadri.Vasudevan@Sun.COM 	first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec);
515010021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
5151*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Enter the beginning cylinder (Default - %d) : ",
515210021SSheshadri.Vasudevan@Sun.COM 		    first_free_cyl);
515310021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
515410021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
515510021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
515610021SSheshadri.Vasudevan@Sun.COM 			*begsec = first_free_sec;
515710021SSheshadri.Vasudevan@Sun.COM 			return (FDISK_SUCCESS);
515810021SSheshadri.Vasudevan@Sun.COM 		}
515910021SSheshadri.Vasudevan@Sun.COM 
516010021SSheshadri.Vasudevan@Sun.COM 		if (len > (FDISK_MAX_VALID_CYL_NUM_DIGITS + 1)) {
5161*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Input too long\n");
5162*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid beginning cylinder number\n");
516310021SSheshadri.Vasudevan@Sun.COM 			continue;
516410021SSheshadri.Vasudevan@Sun.COM 		}
516510021SSheshadri.Vasudevan@Sun.COM 		/* Check if there is a non-digit in the input */
516610021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; i < len - 1; i++) {
516710021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
5168*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid beginning cylinder "
5169*12505SShidokht.Yadegari@Sun.COM 				    "number\n");
517010021SSheshadri.Vasudevan@Sun.COM 				break;
517110021SSheshadri.Vasudevan@Sun.COM 			}
517210021SSheshadri.Vasudevan@Sun.COM 		}
517310021SSheshadri.Vasudevan@Sun.COM 		if (i < len - 1) {
517410021SSheshadri.Vasudevan@Sun.COM 			continue;
517510021SSheshadri.Vasudevan@Sun.COM 		}
517610021SSheshadri.Vasudevan@Sun.COM 
517710021SSheshadri.Vasudevan@Sun.COM 		begcyl = atoi(buf);
517810021SSheshadri.Vasudevan@Sun.COM 		ret = fdisk_ext_validate_part_start(epp, begcyl, begsec);
517910021SSheshadri.Vasudevan@Sun.COM 		switch (ret) {
518010021SSheshadri.Vasudevan@Sun.COM 			case FDISK_SUCCESS:
518110021SSheshadri.Vasudevan@Sun.COM 				/*
518210021SSheshadri.Vasudevan@Sun.COM 				 * Success.
518310021SSheshadri.Vasudevan@Sun.COM 				 * Valid beginning sector is in begsec
518410021SSheshadri.Vasudevan@Sun.COM 				 */
518510021SSheshadri.Vasudevan@Sun.COM 				break;
518610021SSheshadri.Vasudevan@Sun.COM 
518710021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOVERLAP:
5188*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Partition boundary overlaps "
5189*12505SShidokht.Yadegari@Sun.COM 				    "with ");
5190*12505SShidokht.Yadegari@Sun.COM 				(void) printf("existing partitions\n");
5191*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid beginning cylinder "
5192*12505SShidokht.Yadegari@Sun.COM 				    "number\n");
519310021SSheshadri.Vasudevan@Sun.COM 				continue;
519410021SSheshadri.Vasudevan@Sun.COM 
519510021SSheshadri.Vasudevan@Sun.COM 			case FDISK_EOOBOUND:
5196*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Cylinder boundary beyond the "
5197*12505SShidokht.Yadegari@Sun.COM 				    "limits\n");
5198*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid beginning cylinder "
5199*12505SShidokht.Yadegari@Sun.COM 				    "number\n");
520010021SSheshadri.Vasudevan@Sun.COM 				continue;
520110021SSheshadri.Vasudevan@Sun.COM 		}
520210021SSheshadri.Vasudevan@Sun.COM 		return (FDISK_SUCCESS);
520310021SSheshadri.Vasudevan@Sun.COM 	}
520410021SSheshadri.Vasudevan@Sun.COM }
520510021SSheshadri.Vasudevan@Sun.COM 
520610021SSheshadri.Vasudevan@Sun.COM /*
520710021SSheshadri.Vasudevan@Sun.COM  * Algorithm :
520810021SSheshadri.Vasudevan@Sun.COM  * 1. Check if the first character is a +
520910021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, check if the last character is 'k', 'm' or 'g'
521010021SSheshadri.Vasudevan@Sun.COM  * 2. If not, check if there are any non-digits
521110021SSheshadri.Vasudevan@Sun.COM  * 3. Check for the length of the numeral string
521210021SSheshadri.Vasudevan@Sun.COM  * 4. atoi the numeral string
521310021SSheshadri.Vasudevan@Sun.COM  * 5. In case of data entered in KB, MB or GB, convert it to number of cylinders
521410021SSheshadri.Vasudevan@Sun.COM  *	a) Adjust size to be cylinder boundary aligned
521510021SSheshadri.Vasudevan@Sun.COM  * 6. If size specifies is zero, flag error
521610021SSheshadri.Vasudevan@Sun.COM  * 7. Check if the size is less than 1 cylinder
521710021SSheshadri.Vasudevan@Sun.COM  *	a) If yes, default the size FDISK_MIN_PART_SIZE
521810021SSheshadri.Vasudevan@Sun.COM  * 	b) If no, Check if the size is within endcyl - begcyl
521910021SSheshadri.Vasudevan@Sun.COM  */
522010021SSheshadri.Vasudevan@Sun.COM static void
522110021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec)
522210021SSheshadri.Vasudevan@Sun.COM {
522310021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
522410021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_sec;
522510021SSheshadri.Vasudevan@Sun.COM 	uint32_t last_free_cyl;
522610021SSheshadri.Vasudevan@Sun.COM 	int i, len, ch, mbgb = 0, scale = FDISK_SECTS_PER_CYL(epp);
522710021SSheshadri.Vasudevan@Sun.COM 	uint64_t size = 0;
522810021SSheshadri.Vasudevan@Sun.COM 	int copy_len;
522910021SSheshadri.Vasudevan@Sun.COM 	char numbuf[FDISK_MAX_VALID_CYL_NUM_DIGITS + 1];
523010021SSheshadri.Vasudevan@Sun.COM 	int sectsize = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE);
523110021SSheshadri.Vasudevan@Sun.COM 	uint32_t remdr, spc, poss_end;
523210021SSheshadri.Vasudevan@Sun.COM 
523310021SSheshadri.Vasudevan@Sun.COM 	if (sectsize == EINVAL) {
5234*12505SShidokht.Yadegari@Sun.COM 		(void) fprintf(stderr, "Unsupported geometry statistics.\n");
523510021SSheshadri.Vasudevan@Sun.COM 		exit(1);
523610021SSheshadri.Vasudevan@Sun.COM 	}
523710021SSheshadri.Vasudevan@Sun.COM 
523810021SSheshadri.Vasudevan@Sun.COM 	last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec);
523910021SSheshadri.Vasudevan@Sun.COM 	last_free_cyl = FDISK_SECT_TO_CYL(epp, last_free_sec);
524010021SSheshadri.Vasudevan@Sun.COM 
524110021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
5242*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Enter the size in cylinders (Default End "
5243*12505SShidokht.Yadegari@Sun.COM 		    "Cylinder -");
5244*12505SShidokht.Yadegari@Sun.COM 		(void) printf(" %u)\n", last_free_cyl);
5245*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Type +<size>K, +<size>M or +<size>G to enter "
5246*12505SShidokht.Yadegari@Sun.COM 		    "size in");
5247*12505SShidokht.Yadegari@Sun.COM 		(void) printf("KB, MB or GB : ");
524810021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
524910021SSheshadri.Vasudevan@Sun.COM 		len = strlen(buf);
525010021SSheshadri.Vasudevan@Sun.COM 		mbgb = 0;
525110021SSheshadri.Vasudevan@Sun.COM 		scale = FDISK_SECTS_PER_CYL(epp);
525210021SSheshadri.Vasudevan@Sun.COM 
525310021SSheshadri.Vasudevan@Sun.COM 		if (len == 1) { /* User accepted the default value */
525410021SSheshadri.Vasudevan@Sun.COM 			*endsec = last_free_sec;
525510021SSheshadri.Vasudevan@Sun.COM 			return;
525610021SSheshadri.Vasudevan@Sun.COM 		}
525710021SSheshadri.Vasudevan@Sun.COM 
525810021SSheshadri.Vasudevan@Sun.COM 		copy_len = len - 1;
525910021SSheshadri.Vasudevan@Sun.COM 
526010021SSheshadri.Vasudevan@Sun.COM 		if ((buf[0] == '+') && (isdigit(buf[1]))) {
526110021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
526210021SSheshadri.Vasudevan@Sun.COM 			if ((ch = toupper(buf[len - 2])) == 'B') {
526310021SSheshadri.Vasudevan@Sun.COM 				ch = toupper(buf[len - 3]);
526410021SSheshadri.Vasudevan@Sun.COM 				copy_len--;
526510021SSheshadri.Vasudevan@Sun.COM 			}
526610021SSheshadri.Vasudevan@Sun.COM 
526710021SSheshadri.Vasudevan@Sun.COM 			if (!((ch == 'K') || (ch == 'M') || (ch == 'G'))) {
5268*12505SShidokht.Yadegari@Sun.COM 				(void) printf("Invalid partition size\n");
526910021SSheshadri.Vasudevan@Sun.COM 				continue;
527010021SSheshadri.Vasudevan@Sun.COM 			}
527110021SSheshadri.Vasudevan@Sun.COM 
527210021SSheshadri.Vasudevan@Sun.COM 			copy_len--;
527310021SSheshadri.Vasudevan@Sun.COM 			mbgb = 1;
527410021SSheshadri.Vasudevan@Sun.COM 			scale = ((ch == 'K') ? FDISK_KB :
527510021SSheshadri.Vasudevan@Sun.COM 			    ((ch == 'M') ? FDISK_MB : FDISK_GB));
527610021SSheshadri.Vasudevan@Sun.COM 		}
527710021SSheshadri.Vasudevan@Sun.COM 
527810021SSheshadri.Vasudevan@Sun.COM 		if (copy_len > FDISK_MAX_VALID_CYL_NUM_DIGITS) {
5279*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Input too long\n");
5280*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition size\n");
528110021SSheshadri.Vasudevan@Sun.COM 			continue;
528210021SSheshadri.Vasudevan@Sun.COM 		}
528310021SSheshadri.Vasudevan@Sun.COM 
5284*12505SShidokht.Yadegari@Sun.COM 		(void) strncpy(numbuf, &buf[mbgb], copy_len);
528510021SSheshadri.Vasudevan@Sun.COM 		numbuf[copy_len] = '\0';
528610021SSheshadri.Vasudevan@Sun.COM 
528710021SSheshadri.Vasudevan@Sun.COM 		for (i = mbgb; i < copy_len + mbgb; i++) {
528810021SSheshadri.Vasudevan@Sun.COM 			if (!isdigit(buf[i])) {
528910021SSheshadri.Vasudevan@Sun.COM 				break;
529010021SSheshadri.Vasudevan@Sun.COM 			}
529110021SSheshadri.Vasudevan@Sun.COM 		}
529210021SSheshadri.Vasudevan@Sun.COM 
529310021SSheshadri.Vasudevan@Sun.COM 		if (i < copy_len + mbgb) {
5294*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition size\n");
529510021SSheshadri.Vasudevan@Sun.COM 			continue;
529610021SSheshadri.Vasudevan@Sun.COM 		}
529710021SSheshadri.Vasudevan@Sun.COM 
529810021SSheshadri.Vasudevan@Sun.COM 		size = (atoll(numbuf) * (scale));
529910021SSheshadri.Vasudevan@Sun.COM 
530010021SSheshadri.Vasudevan@Sun.COM 		if (size == 0) {
5301*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Zero size is invalid\n");
5302*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition size\n");
530310021SSheshadri.Vasudevan@Sun.COM 			continue;
530410021SSheshadri.Vasudevan@Sun.COM 		}
530510021SSheshadri.Vasudevan@Sun.COM 
530610021SSheshadri.Vasudevan@Sun.COM 		if (mbgb) {
530710021SSheshadri.Vasudevan@Sun.COM 			size /= sectsize;
530810021SSheshadri.Vasudevan@Sun.COM 		}
530910021SSheshadri.Vasudevan@Sun.COM 
531010021SSheshadri.Vasudevan@Sun.COM 		if (size > (last_free_sec - begsec + 1)) {
5311*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Cylinder boundary beyond the limits");
5312*12505SShidokht.Yadegari@Sun.COM 			(void) printf(" or overlaps with existing");
5313*12505SShidokht.Yadegari@Sun.COM 			(void) printf(" partitions\n");
5314*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Invalid partition size\n");
531510021SSheshadri.Vasudevan@Sun.COM 			continue;
531610021SSheshadri.Vasudevan@Sun.COM 		}
531710021SSheshadri.Vasudevan@Sun.COM 
531810021SSheshadri.Vasudevan@Sun.COM 		/*
531910021SSheshadri.Vasudevan@Sun.COM 		 * Adjust the ending sector such that there are no partial
532010021SSheshadri.Vasudevan@Sun.COM 		 * cylinders allocated. But at the same time, make sure it
532110021SSheshadri.Vasudevan@Sun.COM 		 * doesn't over shoot boundaries.
532210021SSheshadri.Vasudevan@Sun.COM 		 */
532310021SSheshadri.Vasudevan@Sun.COM 		spc = FDISK_SECTS_PER_CYL(epp);
532410021SSheshadri.Vasudevan@Sun.COM 		poss_end = begsec + size - 1;
5325*12505SShidokht.Yadegari@Sun.COM 		if ((remdr = (poss_end % spc)) != 0) {
532610021SSheshadri.Vasudevan@Sun.COM 			poss_end += spc - remdr - 1;
532710021SSheshadri.Vasudevan@Sun.COM 		}
532810021SSheshadri.Vasudevan@Sun.COM 		*endsec = (poss_end > last_free_sec) ? last_free_sec :
532910021SSheshadri.Vasudevan@Sun.COM 		    poss_end;
533010021SSheshadri.Vasudevan@Sun.COM 
533110021SSheshadri.Vasudevan@Sun.COM 		return;
533210021SSheshadri.Vasudevan@Sun.COM 	}
533310021SSheshadri.Vasudevan@Sun.COM }
533410021SSheshadri.Vasudevan@Sun.COM 
533510021SSheshadri.Vasudevan@Sun.COM /*
533610021SSheshadri.Vasudevan@Sun.COM  * ALGORITHM:
533710021SSheshadri.Vasudevan@Sun.COM  * 1. Get the starting and ending sectors/cylinder of the extended partition.
533810021SSheshadri.Vasudevan@Sun.COM  * 2. Keep track of the first free sector/cylinder
533910021SSheshadri.Vasudevan@Sun.COM  * 3. Allow the user to specify the beginning cylinder of the new partition
534010021SSheshadri.Vasudevan@Sun.COM  * 4. Check for the validity of the entered data
534110021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
534210021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
534310021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
534410021SSheshadri.Vasudevan@Sun.COM  * 5. Allow the user to specify the size in cylinders/ human readable form
534510021SSheshadri.Vasudevan@Sun.COM  * 6. Check for the validity of the entered data
534610021SSheshadri.Vasudevan@Sun.COM  *	a) If it is non-numeric
534710021SSheshadri.Vasudevan@Sun.COM  *	b) If it is beyond the extended partition limits
534810021SSheshadri.Vasudevan@Sun.COM  *	c) If it overlaps with the current logical drives
534910021SSheshadri.Vasudevan@Sun.COM  *	d) If it is a number lesser than the starting cylinder
535010021SSheshadri.Vasudevan@Sun.COM  * 7. Request partition ID for the new partition.
535110021SSheshadri.Vasudevan@Sun.COM  * 8. Update the first free cylinder available
535210021SSheshadri.Vasudevan@Sun.COM  * 9. Display Success message
535310021SSheshadri.Vasudevan@Sun.COM  */
535410021SSheshadri.Vasudevan@Sun.COM 
535510021SSheshadri.Vasudevan@Sun.COM static void
535610021SSheshadri.Vasudevan@Sun.COM add_logical_drive()
535710021SSheshadri.Vasudevan@Sun.COM {
535810021SSheshadri.Vasudevan@Sun.COM 	uint32_t begsec, endsec;
535910021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
536010021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
536110021SSheshadri.Vasudevan@Sun.COM 	int rval;
536210021SSheshadri.Vasudevan@Sun.COM 
536310021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_get_logical_drive_count(epp) >= MAX_EXT_PARTS) {
5364*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nNumber of logical drives exceeds limit of "
5365*12505SShidokht.Yadegari@Sun.COM 		    "%d.\n", MAX_EXT_PARTS);
5366*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Command did not succeed. Press enter to "
5367*12505SShidokht.Yadegari@Sun.COM 		    "continue\n");
536810021SSheshadri.Vasudevan@Sun.COM 		ext_read_input(buf);
536910021SSheshadri.Vasudevan@Sun.COM 		return;
537010021SSheshadri.Vasudevan@Sun.COM 	}
537110021SSheshadri.Vasudevan@Sun.COM 
5372*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
537310021SSheshadri.Vasudevan@Sun.COM 	rval = ext_read_valid_partition_start(&begsec);
537410021SSheshadri.Vasudevan@Sun.COM 	switch (rval) {
537510021SSheshadri.Vasudevan@Sun.COM 		case FDISK_SUCCESS:
537610021SSheshadri.Vasudevan@Sun.COM 			break;
537710021SSheshadri.Vasudevan@Sun.COM 
537810021SSheshadri.Vasudevan@Sun.COM 		case FDISK_EOOBOUND:
5379*12505SShidokht.Yadegari@Sun.COM 			(void) printf("\nNo space left in the extended "
5380*12505SShidokht.Yadegari@Sun.COM 			    "partition\n");
5381*12505SShidokht.Yadegari@Sun.COM 			(void) printf("Press enter to continue\n");
538210021SSheshadri.Vasudevan@Sun.COM 			ext_read_input(buf);
538310021SSheshadri.Vasudevan@Sun.COM 			return;
538410021SSheshadri.Vasudevan@Sun.COM 	}
538510021SSheshadri.Vasudevan@Sun.COM 
538610021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_partition_size(begsec, &endsec);
538710021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
538810021SSheshadri.Vasudevan@Sun.COM 	fdisk_add_logical_drive(epp, begsec, endsec, partid);
538910021SSheshadri.Vasudevan@Sun.COM 
5390*12505SShidokht.Yadegari@Sun.COM 	(void) printf("New partition with ID %d added\n", partid);
539110021SSheshadri.Vasudevan@Sun.COM }
539210021SSheshadri.Vasudevan@Sun.COM 
539310021SSheshadri.Vasudevan@Sun.COM static void
539410021SSheshadri.Vasudevan@Sun.COM ext_change_logical_drive_id()
539510021SSheshadri.Vasudevan@Sun.COM {
539610021SSheshadri.Vasudevan@Sun.COM 	int pno;
539710021SSheshadri.Vasudevan@Sun.COM 	uchar_t partid;
539810021SSheshadri.Vasudevan@Sun.COM 
539910021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
5400*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nNo logical drives defined.\n");
540110021SSheshadri.Vasudevan@Sun.COM 		return;
540210021SSheshadri.Vasudevan@Sun.COM 	}
540310021SSheshadri.Vasudevan@Sun.COM 
5404*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
540510021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_num(&pno);
540610021SSheshadri.Vasudevan@Sun.COM 	ext_read_valid_part_id(&partid);
540710021SSheshadri.Vasudevan@Sun.COM 	fdisk_change_logical_drive_id(epp, pno, partid);
540810021SSheshadri.Vasudevan@Sun.COM 
5409*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Partition ID of partition %d changed to %d\n", pno,
5410*12505SShidokht.Yadegari@Sun.COM 	    partid);
541110021SSheshadri.Vasudevan@Sun.COM }
541210021SSheshadri.Vasudevan@Sun.COM 
541310021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
541410021SSheshadri.Vasudevan@Sun.COM static void
541510021SSheshadri.Vasudevan@Sun.COM ext_print_logdrive_layout_debug()
541610021SSheshadri.Vasudevan@Sun.COM {
541710021SSheshadri.Vasudevan@Sun.COM 	int pno;
541810021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
541910021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *head = fdisk_get_ld_head(epp);
542010021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
542110021SSheshadri.Vasudevan@Sun.COM 
542210021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
5423*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nNo logical drives defined.\n");
542410021SSheshadri.Vasudevan@Sun.COM 		return;
542510021SSheshadri.Vasudevan@Sun.COM 	}
542610021SSheshadri.Vasudevan@Sun.COM 
5427*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n\n");
542810021SSheshadri.Vasudevan@Sun.COM 	puts("#  start block  end block    abs start    abs end      OSType");
542910021SSheshadri.Vasudevan@Sun.COM 	for (temp = head, pno = 5; temp != NULL; temp = temp->next, pno++) {
543010021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
543110021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[0].systid, namebuff);
5432*12505SShidokht.Yadegari@Sun.COM 		(void) printf("%d: %.10u   %.10u   %.10u   %.10u",
543310021SSheshadri.Vasudevan@Sun.COM 		    pno,
543410021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].relsect),
543510021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[0].numsect),
543610021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum,
543710021SSheshadri.Vasudevan@Sun.COM 		    temp->abs_secnum + temp->numsect - 1 +
543810021SSheshadri.Vasudevan@Sun.COM 		    MAX_LOGDRIVE_OFFSET);
5439*12505SShidokht.Yadegari@Sun.COM 		(void) printf("   %s\n", namebuff);
544010021SSheshadri.Vasudevan@Sun.COM 		/*
544110021SSheshadri.Vasudevan@Sun.COM 		 * Print the second entry in the EBR which is information
544210021SSheshadri.Vasudevan@Sun.COM 		 * about the location and the size of the next extended
544310021SSheshadri.Vasudevan@Sun.COM 		 * partition.
544410021SSheshadri.Vasudevan@Sun.COM 		 */
544510021SSheshadri.Vasudevan@Sun.COM 		id_to_name(temp->parts[1].systid, namebuff);
5446*12505SShidokht.Yadegari@Sun.COM 		(void) printf("%d: %.10u   %.10u   %.10s   %.10s",
544710021SSheshadri.Vasudevan@Sun.COM 		    pno,
544810021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].relsect),
544910021SSheshadri.Vasudevan@Sun.COM 		    LE_32(temp->parts[1].numsect),
545010021SSheshadri.Vasudevan@Sun.COM 		    "          ", "          ");
5451*12505SShidokht.Yadegari@Sun.COM 		(void) printf("   %s\n", namebuff);
545210021SSheshadri.Vasudevan@Sun.COM 	}
545310021SSheshadri.Vasudevan@Sun.COM }
545410021SSheshadri.Vasudevan@Sun.COM #endif
545510021SSheshadri.Vasudevan@Sun.COM 
545610021SSheshadri.Vasudevan@Sun.COM static void
545710021SSheshadri.Vasudevan@Sun.COM ext_print_logical_drive_layout()
545810021SSheshadri.Vasudevan@Sun.COM {
545910021SSheshadri.Vasudevan@Sun.COM 	int sysid;
546010021SSheshadri.Vasudevan@Sun.COM 	unsigned int startcyl, endcyl, length, percent, remainder;
546110021SSheshadri.Vasudevan@Sun.COM 	logical_drive_t *temp;
546210682SSheshadri.Vasudevan@Sun.COM 	uint32_t part_start;
546310021SSheshadri.Vasudevan@Sun.COM 	struct ipart *fpart;
546410021SSheshadri.Vasudevan@Sun.COM 	char namebuff[255];
546510021SSheshadri.Vasudevan@Sun.COM 	int numcyl = fdisk_get_disk_geom(epp, PHYSGEOM, NCYL);
546610021SSheshadri.Vasudevan@Sun.COM 	int pno;
546710021SSheshadri.Vasudevan@Sun.COM 
546810021SSheshadri.Vasudevan@Sun.COM 	if (numcyl == EINVAL) {
5469*12505SShidokht.Yadegari@Sun.COM 		(void) fprintf(stderr, "Unsupported geometry statistics.\n");
547010021SSheshadri.Vasudevan@Sun.COM 		exit(1);
547110021SSheshadri.Vasudevan@Sun.COM 	}
547210021SSheshadri.Vasudevan@Sun.COM 
547310021SSheshadri.Vasudevan@Sun.COM 	if (!fdisk_get_logical_drive_count(epp)) {
5474*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nNo logical drives defined.\n");
547510021SSheshadri.Vasudevan@Sun.COM 		return;
547610021SSheshadri.Vasudevan@Sun.COM 	}
547710021SSheshadri.Vasudevan@Sun.COM 
5478*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
5479*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Number of cylinders in disk              : %u\n",
5480*12505SShidokht.Yadegari@Sun.COM 	    numcyl);
5481*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Beginning cylinder of extended partition : %u\n",
548210021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_beg_cyl(epp));
5483*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Ending cylinder of extended partition    : %u\n",
548410021SSheshadri.Vasudevan@Sun.COM 	    fdisk_get_ext_end_cyl(epp));
5485*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
5486*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Part#   StartCyl   EndCyl     Length    %%     "
548710021SSheshadri.Vasudevan@Sun.COM 	"Part ID (Type)\n");
5488*12505SShidokht.Yadegari@Sun.COM 	(void) printf("=====   ========   ========   =======   ==="
548910021SSheshadri.Vasudevan@Sun.COM 	"   ==============\n");
549010021SSheshadri.Vasudevan@Sun.COM 	for (temp = fdisk_get_ld_head(epp), pno = 5; temp != NULL;
549110021SSheshadri.Vasudevan@Sun.COM 	    temp = temp->next, pno++) {
549210021SSheshadri.Vasudevan@Sun.COM 		/* Print the logical drive details */
549310021SSheshadri.Vasudevan@Sun.COM 		fpart = &temp->parts[0];
549410021SSheshadri.Vasudevan@Sun.COM 		sysid = fpart->systid;
549510682SSheshadri.Vasudevan@Sun.COM 		/*
549610682SSheshadri.Vasudevan@Sun.COM 		 * Check if partition id 0x82 is Solaris
549710682SSheshadri.Vasudevan@Sun.COM 		 * or a Linux swap. Print the string
549810682SSheshadri.Vasudevan@Sun.COM 		 * accordingly.
549910682SSheshadri.Vasudevan@Sun.COM 		 */
550010682SSheshadri.Vasudevan@Sun.COM 		if (sysid == SUNIXOS) {
550110682SSheshadri.Vasudevan@Sun.COM 			part_start = temp->abs_secnum +
550210682SSheshadri.Vasudevan@Sun.COM 			    temp->logdrive_offset;
550310682SSheshadri.Vasudevan@Sun.COM 			if (fdisk_is_linux_swap(epp, part_start,
550410682SSheshadri.Vasudevan@Sun.COM 			    NULL) == 0)
5505*12505SShidokht.Yadegari@Sun.COM 				(void) strcpy(namebuff, LINSWAPstr);
550610682SSheshadri.Vasudevan@Sun.COM 			else
5507*12505SShidokht.Yadegari@Sun.COM 				(void) strcpy(namebuff, SUstr);
550810682SSheshadri.Vasudevan@Sun.COM 		} else {
550910682SSheshadri.Vasudevan@Sun.COM 			id_to_name(sysid, namebuff);
551010682SSheshadri.Vasudevan@Sun.COM 		}
551110021SSheshadri.Vasudevan@Sun.COM 		startcyl = temp->begcyl;
551210021SSheshadri.Vasudevan@Sun.COM 		endcyl = temp->endcyl;
551310021SSheshadri.Vasudevan@Sun.COM 		if (startcyl == endcyl) {
551410021SSheshadri.Vasudevan@Sun.COM 			length = 1;
551510021SSheshadri.Vasudevan@Sun.COM 		} else {
551610021SSheshadri.Vasudevan@Sun.COM 			length = endcyl - startcyl + 1;
551710021SSheshadri.Vasudevan@Sun.COM 		}
551810021SSheshadri.Vasudevan@Sun.COM 		percent = length * 100 / numcyl;
551910021SSheshadri.Vasudevan@Sun.COM 		if ((remainder = (length * 100 % numcyl)) != 0) {
552010021SSheshadri.Vasudevan@Sun.COM 			if ((remainder * 100 / numcyl) > 50) {
552110021SSheshadri.Vasudevan@Sun.COM 				/* round up */
552210021SSheshadri.Vasudevan@Sun.COM 				percent++;
552310021SSheshadri.Vasudevan@Sun.COM 			}
552410021SSheshadri.Vasudevan@Sun.COM 			/* Else leave the percent as is since it's already */
552510021SSheshadri.Vasudevan@Sun.COM 			/* rounded down */
552610021SSheshadri.Vasudevan@Sun.COM 		}
552710021SSheshadri.Vasudevan@Sun.COM 		if (percent > 100) {
552810021SSheshadri.Vasudevan@Sun.COM 			percent = 100;
552910021SSheshadri.Vasudevan@Sun.COM 		}
5530*12505SShidokht.Yadegari@Sun.COM 		(void) printf("%-5d   %-8u   %-8u   %-7u   %-3d   "
5531*12505SShidokht.Yadegari@Sun.COM 		    "%-3d (%-.28s)\n",
553210021SSheshadri.Vasudevan@Sun.COM 		    pno, startcyl, endcyl, length, percent, sysid, namebuff);
553310021SSheshadri.Vasudevan@Sun.COM 	}
553410021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG
553510021SSheshadri.Vasudevan@Sun.COM 	ext_print_logdrive_layout_debug();
553610021SSheshadri.Vasudevan@Sun.COM #endif
5537*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
553810021SSheshadri.Vasudevan@Sun.COM }
553910021SSheshadri.Vasudevan@Sun.COM 
554010021SSheshadri.Vasudevan@Sun.COM static void
554110021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu()
554210021SSheshadri.Vasudevan@Sun.COM {
5543*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
5544*12505SShidokht.Yadegari@Sun.COM 	(void) printf("a	Add a logical drive\n");
5545*12505SShidokht.Yadegari@Sun.COM 	(void) printf("d	Delete a logical drive\n");
5546*12505SShidokht.Yadegari@Sun.COM 	(void) printf("h	Print this help menu\n");
5547*12505SShidokht.Yadegari@Sun.COM 	(void) printf("i	Change the id of the logical drive\n");
5548*12505SShidokht.Yadegari@Sun.COM 	(void) printf("p	Print the logical drive layout\n");
5549*12505SShidokht.Yadegari@Sun.COM 	(void) printf("r	Return to the main fdisk menu\n");
5550*12505SShidokht.Yadegari@Sun.COM 	(void) printf("        (To commit or cancel the changes)\n");
5551*12505SShidokht.Yadegari@Sun.COM 	(void) printf("\n");
555210021SSheshadri.Vasudevan@Sun.COM }
555310021SSheshadri.Vasudevan@Sun.COM 
555410021SSheshadri.Vasudevan@Sun.COM static void
555510021SSheshadri.Vasudevan@Sun.COM ext_part_menu()
555610021SSheshadri.Vasudevan@Sun.COM {
555710021SSheshadri.Vasudevan@Sun.COM 	char buf[80];
555810021SSheshadri.Vasudevan@Sun.COM 	uchar_t *bbsigp;
555910021SSheshadri.Vasudevan@Sun.COM 	static int bbsig_disp_flag = 1;
556010021SSheshadri.Vasudevan@Sun.COM 
556110021SSheshadri.Vasudevan@Sun.COM 	int i;
556210021SSheshadri.Vasudevan@Sun.COM 
5563*12505SShidokht.Yadegari@Sun.COM 	(void) printf(CLR_SCR);
556410021SSheshadri.Vasudevan@Sun.COM 
556510021SSheshadri.Vasudevan@Sun.COM 	if (fdisk_corrupt_logical_drives(epp)) {
5566*12505SShidokht.Yadegari@Sun.COM 		(void) printf("One or more logical drives seem to be "
5567*12505SShidokht.Yadegari@Sun.COM 		    "corrupt.\n");
5568*12505SShidokht.Yadegari@Sun.COM 		(void) printf("Displaying only sane logical drives.\n");
556910021SSheshadri.Vasudevan@Sun.COM 	}
557010021SSheshadri.Vasudevan@Sun.COM 
557110021SSheshadri.Vasudevan@Sun.COM 	if (bbsig_disp_flag && fdisk_invalid_bb_sig(epp, &bbsigp)) {
5572*12505SShidokht.Yadegari@Sun.COM 		(void) printf("The following logical drives have a wrong "
5573*12505SShidokht.Yadegari@Sun.COM 		    "boot block signature :\n\n");
557410021SSheshadri.Vasudevan@Sun.COM 		for (i = 0; bbsigp[i]; i++) {
5575*12505SShidokht.Yadegari@Sun.COM 			(void) printf("%d ", bbsigp[i]);
557610021SSheshadri.Vasudevan@Sun.COM 		}
5577*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\n\n");
5578*12505SShidokht.Yadegari@Sun.COM 		(void) printf("They will be corrected when you choose to "
5579*12505SShidokht.Yadegari@Sun.COM 		    "commit\n");
558010021SSheshadri.Vasudevan@Sun.COM 		bbsig_disp_flag = 0;
558110021SSheshadri.Vasudevan@Sun.COM 	}
558210021SSheshadri.Vasudevan@Sun.COM 
5583*12505SShidokht.Yadegari@Sun.COM 	(void) printf("Extended partition menu\n");
558410021SSheshadri.Vasudevan@Sun.COM 
558510021SSheshadri.Vasudevan@Sun.COM 	for (;;) {
5586*12505SShidokht.Yadegari@Sun.COM 		(void) printf("\nEnter Command (Type h for help) : ");
558710021SSheshadri.Vasudevan@Sun.COM 		if ((ext_read_options(buf)) < 0) {
5588*12505SShidokht.Yadegari@Sun.COM 			(void) printf("\nCommand Options : \n");
558910021SSheshadri.Vasudevan@Sun.COM 			ext_print_help_menu();
559010021SSheshadri.Vasudevan@Sun.COM 			continue;
559110021SSheshadri.Vasudevan@Sun.COM 		}
559210021SSheshadri.Vasudevan@Sun.COM 		switch (buf[0]) {
559310021SSheshadri.Vasudevan@Sun.COM 			case 'a':
559410021SSheshadri.Vasudevan@Sun.COM 				add_logical_drive();
559510021SSheshadri.Vasudevan@Sun.COM 				break;
559610021SSheshadri.Vasudevan@Sun.COM 			case 'd':
559710021SSheshadri.Vasudevan@Sun.COM 				delete_logical_drive();
559810021SSheshadri.Vasudevan@Sun.COM 				break;
559910021SSheshadri.Vasudevan@Sun.COM 			case 'h':
560010021SSheshadri.Vasudevan@Sun.COM 				ext_print_help_menu();
560110021SSheshadri.Vasudevan@Sun.COM 				break;
560210021SSheshadri.Vasudevan@Sun.COM 			case 'i':
560310021SSheshadri.Vasudevan@Sun.COM 				ext_change_logical_drive_id();
560410021SSheshadri.Vasudevan@Sun.COM 				break;
560510021SSheshadri.Vasudevan@Sun.COM 			case 'p':
560610021SSheshadri.Vasudevan@Sun.COM 				ext_print_logical_drive_layout();
560710021SSheshadri.Vasudevan@Sun.COM 				break;
560810021SSheshadri.Vasudevan@Sun.COM 			case 'r':
5609*12505SShidokht.Yadegari@Sun.COM 				(void) printf(CLR_SCR);
561010021SSheshadri.Vasudevan@Sun.COM 				return;
561110021SSheshadri.Vasudevan@Sun.COM 			default : /* NOTREACHED */
561210021SSheshadri.Vasudevan@Sun.COM 				break;
561310021SSheshadri.Vasudevan@Sun.COM 		}
561410021SSheshadri.Vasudevan@Sun.COM 	}
561510021SSheshadri.Vasudevan@Sun.COM }
5616*12505SShidokht.Yadegari@Sun.COM #endif /* i386 */
561711382SShidokht.Yadegari@Sun.COM 
561811382SShidokht.Yadegari@Sun.COM static int
561911382SShidokht.Yadegari@Sun.COM nopartdefined()
562011382SShidokht.Yadegari@Sun.COM {
562111382SShidokht.Yadegari@Sun.COM 	int i;
562211382SShidokht.Yadegari@Sun.COM 
562311382SShidokht.Yadegari@Sun.COM 	for (i = 0; i < FD_NUMPART; i++)
562411382SShidokht.Yadegari@Sun.COM 		if (Table[i].systid != UNUSED)
562511382SShidokht.Yadegari@Sun.COM 			return (0);
562611382SShidokht.Yadegari@Sun.COM 	return (1);
562711382SShidokht.Yadegari@Sun.COM }
5628