xref: /illumos-gate/usr/src/cmd/fdisk/fdisk.c (revision 1237e847)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1237e847Slclee  * Common Development and Distribution License (the "License").
6*1237e847Slclee  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*1237e847Slclee 
227c478bd9Sstevel@tonic-gate /*
23*1237e847Slclee  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
387c478bd9Sstevel@tonic-gate  * This program reads the partition table on the specified device and
397c478bd9Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
407c478bd9Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
417c478bd9Sstevel@tonic-gate  * options are also available.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <fcntl.h>
507c478bd9Sstevel@tonic-gate #include <ctype.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <sys/param.h>
547c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
557c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
567c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
577c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
607c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
617c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	CLR_SCR ""
647c478bd9Sstevel@tonic-gate #define	CLR_LIN ""
657c478bd9Sstevel@tonic-gate #define	HOME "" \
667c478bd9Sstevel@tonic-gate 	""
677c478bd9Sstevel@tonic-gate #define	Q_LINE ""
687c478bd9Sstevel@tonic-gate #define	W_LINE ""
697c478bd9Sstevel@tonic-gate #define	E_LINE ""
707c478bd9Sstevel@tonic-gate #define	M_LINE "" \
717c478bd9Sstevel@tonic-gate 	""
727c478bd9Sstevel@tonic-gate #define	T_LINE ""
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* XXX - should be in fdisk.h, used by sd as well */
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * the MAX values are the maximum usable values for BIOS chs values
807c478bd9Sstevel@tonic-gate  * The MAX_CYL value of 1022 is the maximum usable value
817c478bd9Sstevel@tonic-gate  *   the value of 1023 is a fence value,
827c478bd9Sstevel@tonic-gate  *   indicating no CHS geometry exists for the corresponding LBA value.
837c478bd9Sstevel@tonic-gate  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
847c478bd9Sstevel@tonic-gate  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate #define	MAX_SECT	(63)
877c478bd9Sstevel@tonic-gate #define	MAX_CYL		(1022)
887c478bd9Sstevel@tonic-gate #define	MAX_HEAD	(254)
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /* for clear_vtoc() */
917c478bd9Sstevel@tonic-gate #define	OLD		0
927c478bd9Sstevel@tonic-gate #define	NEW		1
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
957c478bd9Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
967c478bd9Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
977c478bd9Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
987c478bd9Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * Support for fdisk(1M) on the SPARC platform
1027c478bd9Sstevel@tonic-gate  *	In order to convert little endian values to big endian for SPARC,
1037c478bd9Sstevel@tonic-gate  *	byte/short and long values must be swapped.
1047c478bd9Sstevel@tonic-gate  *	These swapping macros will be used to access information in the
1057c478bd9Sstevel@tonic-gate  *	mboot and ipart structures.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #ifdef sparc
1097c478bd9Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1107c478bd9Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
1117c478bd9Sstevel@tonic-gate 			    (les((unsigned)((val)&0xffff0000)>>16)))
1127c478bd9Sstevel@tonic-gate #else
1137c478bd9Sstevel@tonic-gate #define	les(val)	(val)
1147c478bd9Sstevel@tonic-gate #define	lel(val)	(val)
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1187c478bd9Sstevel@tonic-gate #define	VTOC_OFFSET	512
1197c478bd9Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1207c478bd9Sstevel@tonic-gate #define	VTOC_OFFSET	0
1217c478bd9Sstevel@tonic-gate #else
1227c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate 
125bb16350dSlclee static char Usage[] = "Usage: fdisk\n"
1267c478bd9Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1277c478bd9Sstevel@tonic-gate "[ -b masterboot ]\n"
1287c478bd9Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1297c478bd9Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1307c478bd9Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1317c478bd9Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1327c478bd9Sstevel@tonic-gate 
133bb16350dSlclee static char Usage1[] = "    Partition options:\n"
1347c478bd9Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1357c478bd9Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1367c478bd9Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1377c478bd9Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1387c478bd9Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1397c478bd9Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1407c478bd9Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1417c478bd9Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1427c478bd9Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1437c478bd9Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1447c478bd9Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1457c478bd9Sstevel@tonic-gate "			  start of partition\n"
1467c478bd9Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1477c478bd9Sstevel@tonic-gate "	-b master_boot\n"
1487c478bd9Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1497c478bd9Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1507c478bd9Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1517c478bd9Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1527c478bd9Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1537c478bd9Sstevel@tonic-gate "	-F fdisk_file\n"
1547c478bd9Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1557c478bd9Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1567c478bd9Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1577c478bd9Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1587c478bd9Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1597c478bd9Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1607c478bd9Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1617c478bd9Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1627c478bd9Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1637c478bd9Sstevel@tonic-gate "	-W fdisk_file\n"
1647c478bd9Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1657c478bd9Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1667c478bd9Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
1677c478bd9Sstevel@tonic-gate "    Diagnostic options:\n"
1687c478bd9Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
1697c478bd9Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
1707c478bd9Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
1717c478bd9Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
1727c478bd9Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
1737c478bd9Sstevel@tonic-gate "		BCYL		cylinder offset\n"
1747c478bd9Sstevel@tonic-gate "		NHEADS		number of heads\n"
1757c478bd9Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
1767c478bd9Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
1777c478bd9Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
1787c478bd9Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
1797c478bd9Sstevel@tonic-gate "	-o offset\n"
1807c478bd9Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
1817c478bd9Sstevel@tonic-gate "		-P # specified.\n"
1827c478bd9Sstevel@tonic-gate "	-P fill_patt\n"
1837c478bd9Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
1847c478bd9Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
1857c478bd9Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
1867c478bd9Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
1877c478bd9Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
1887c478bd9Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
1897c478bd9Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
1907c478bd9Sstevel@tonic-gate "	-S geom_file\n"
1917c478bd9Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
1927c478bd9Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
1937c478bd9Sstevel@tonic-gate 
194bb16350dSlclee static char Ostr[] = "Other OS";
195bb16350dSlclee static char Dstr[] = "DOS12";
196bb16350dSlclee static char D16str[] = "DOS16";
197bb16350dSlclee static char DDstr[] = "DOS-DATA";
198bb16350dSlclee static char EDstr[] = "EXT-DOS";
199bb16350dSlclee static char DBstr[] = "DOS-BIG";
200bb16350dSlclee static char PCstr[] = "PCIX";
201bb16350dSlclee static char Ustr[] = "UNIX System";
202bb16350dSlclee static char SUstr[] = "Solaris";
203bb16350dSlclee static char SU2str[] = "Solaris2";
204bb16350dSlclee static char X86str[] = "x86 Boot";
205bb16350dSlclee static char DIAGstr[] = "Diagnostic";
206bb16350dSlclee static char IFSstr[] = "IFS: NTFS";
207bb16350dSlclee static char AIXstr[] = "AIX Boot";
208bb16350dSlclee static char AIXDstr[] = "AIX Data";
209bb16350dSlclee static char OS2str[] = "OS/2 Boot";
210bb16350dSlclee static char WINstr[] = "Win95 FAT32";
211bb16350dSlclee static char EWINstr[] = "Ext Win95";
212bb16350dSlclee static char FAT95str[] = "FAT16 LBA";
213bb16350dSlclee static char EXTLstr[] = "EXT LBA";
214bb16350dSlclee static char LINUXstr[] = "Linux";
215bb16350dSlclee static char CPMstr[] = "CP/M";
216bb16350dSlclee static char NOVstr[] = "Netware 3.x+";
217bb16350dSlclee static char QNXstr[] = "QNX 4.x";
218bb16350dSlclee static char QNX2str[] = "QNX part 2";
219bb16350dSlclee static char QNX3str[] = "QNX part 3";
220bb16350dSlclee static char LINNATstr[] = "Linux native";
221bb16350dSlclee static char NTFSVOL1str[] = "NT volset 1";
222bb16350dSlclee static char NTFSVOL2str[] = "NT volset 2";
223bb16350dSlclee static char BSDstr[] = "BSD OS";
224bb16350dSlclee static char NEXTSTEPstr[] = "NeXTSTEP";
225bb16350dSlclee static char BSDIFSstr[] = "BSDI FS";
226bb16350dSlclee static char BSDISWAPstr[] = "BSDI swap";
227bb16350dSlclee static char Actvstr[] = "Active";
228bb16350dSlclee static char EFIstr[] = "EFI";
229bb16350dSlclee static char NAstr[] = "      ";
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /* All the user options and flags */
232bb16350dSlclee static char *Dfltdev;			/* name of fixed disk drive */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /* Diagnostic options */
235bb16350dSlclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
236bb16350dSlclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
237bb16350dSlclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
238bb16350dSlclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
239bb16350dSlclee static int	io_lgeom = 0;		/* get label geometry (-g) */
240bb16350dSlclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
241bb16350dSlclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
242bb16350dSlclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2457c478bd9Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
246bb16350dSlclee static int	io_offset = 0;		/* offset sector (-o offset) */
247bb16350dSlclee static int	io_size = 0;		/* size in sectors (-s size) */
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /* Partition table flags */
250bb16350dSlclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
251bb16350dSlclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
252bb16350dSlclee static int	io_fdisk = 0;		/* do fdisk operation */
253bb16350dSlclee static int	io_ifdisk = 0;		/* interactive partition */
254bb16350dSlclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
255bb16350dSlclee 
256bb16350dSlclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
257bb16350dSlclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
258bb16350dSlclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
259bb16350dSlclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
260bb16350dSlclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
261bb16350dSlclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
262bb16350dSlclee 
263bb16350dSlclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
264bb16350dSlclee 
265bb16350dSlclee static struct mboot BootCod;		/* buffer for master boot record */
266bb16350dSlclee 
267bb16350dSlclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
268bb16350dSlclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
269bb16350dSlclee static int	io_debug = 0;		/* activate verbose mode (-d) */
270bb16350dSlclee static int	io_image = 0;		/* create image using geometry (-I) */
271bb16350dSlclee 
272bb16350dSlclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
273bb16350dSlclee static char	*Bootsect;		/* pointer to sector zero buffer */
274bb16350dSlclee static char	*Nullsect;
275bb16350dSlclee static struct vtoc	disk_vtoc;	/* verify VTOC table */
276bb16350dSlclee static int	vt_inval = 0;
277bb16350dSlclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
278bb16350dSlclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
279bb16350dSlclee 
280bb16350dSlclee static struct ipart	Table[FD_NUMPART];
281bb16350dSlclee static struct ipart	Old_Table[FD_NUMPART];
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /* Disk geometry information */
284*1237e847Slclee static struct dk_minfo	minfo;
285bb16350dSlclee static struct dk_geom	disk_geom;
2867c478bd9Sstevel@tonic-gate 
287*1237e847Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
288*1237e847Slclee static diskaddr_t	chs_capacity;	/* Numcyl * heads * sectors */
289*1237e847Slclee 
290bb16350dSlclee static int Dev;			/* fd for open device */
2917c478bd9Sstevel@tonic-gate /* Physical geometry for the drive */
292bb16350dSlclee static int	Numcyl;			/* number of cylinders */
293bb16350dSlclee static int	heads;			/* number of heads */
294bb16350dSlclee static int	sectors;		/* number of sectors per track */
295bb16350dSlclee static int	acyl;			/* number of alternate sectors */
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
298bb16350dSlclee static int	hba_Numcyl;		/* number of cylinders */
299bb16350dSlclee static int	hba_heads;		/* number of heads */
300bb16350dSlclee static int	hba_sectors;		/* number of sectors per track */
3017c478bd9Sstevel@tonic-gate 
302bb16350dSlclee static int	sectsiz;		/* sector size */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /* Load functions for fdisk table modification */
3057c478bd9Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3067c478bd9Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3077c478bd9Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate #define	CBUFLEN 80
310bb16350dSlclee static char s[CBUFLEN];
311bb16350dSlclee 
312bb16350dSlclee static void update_disk_and_exit(boolean_t table_changed);
313bb16350dSlclee int main(int argc, char *argv[]);
314bb16350dSlclee static int read_geom(char *sgeom);
315bb16350dSlclee static void dev_mboot_read(void);
316bb16350dSlclee static void dev_mboot_write(int sect, char *buff, int bootsiz);
317bb16350dSlclee static void mboot_read(void);
318bb16350dSlclee static void fill_patt(void);
319bb16350dSlclee static void abs_read(void);
320bb16350dSlclee static void abs_write(void);
321bb16350dSlclee static void load(int funct, char *file);
322bb16350dSlclee static void Set_Table_CHS_Values(int ti);
323bb16350dSlclee static int insert_tbl(int id, int act,
324bb16350dSlclee     int bhead, int bsect, int bcyl,
325bb16350dSlclee     int ehead, int esect, int ecyl,
326bb16350dSlclee     int rsect, int numsect);
327bb16350dSlclee static int verify_tbl(void);
328bb16350dSlclee static int pars_fdisk(char *line,
329bb16350dSlclee     int *id, int *act,
330bb16350dSlclee     int *bhead, int *bsect, int *bcyl,
331bb16350dSlclee     int *ehead, int *esect, int *ecyl,
332bb16350dSlclee     int *rsect, int *numsect);
333bb16350dSlclee static int validate_part(int id, int rsect, int numsect);
334bb16350dSlclee static void stage0(void);
335bb16350dSlclee static int pcreate(void);
336bb16350dSlclee static int specify(uchar_t tsystid);
337bb16350dSlclee static void dispmenu(void);
338bb16350dSlclee static int pchange(void);
339bb16350dSlclee static int ppartid(void);
340bb16350dSlclee static char pdelete(void);
341bb16350dSlclee static void rm_blanks(char *s);
342bb16350dSlclee static int getcyl(void);
343bb16350dSlclee static void disptbl(void);
344bb16350dSlclee static void print_Table(void);
345bb16350dSlclee static void copy_Table_to_Old_Table(void);
346bb16350dSlclee static void nulltbl(void);
347bb16350dSlclee static void copy_Bootblk_to_Table(void);
348bb16350dSlclee static void fill_ipart(char *bootptr, struct ipart *partp);
349bb16350dSlclee #ifdef sparc
350bb16350dSlclee uchar_t getbyte(char **bp);
351bb16350dSlclee uint32_t getlong(char **bp);
352bb16350dSlclee #endif
353bb16350dSlclee static void copy_Table_to_Bootblk(void);
354bb16350dSlclee static int TableChanged(void);
355bb16350dSlclee static void ffile_write(char *file);
356bb16350dSlclee static void fix_slice(void);
357bb16350dSlclee static int yesno(void);
358bb16350dSlclee static int readvtoc(void);
359bb16350dSlclee static int writevtoc(void);
360bb16350dSlclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
361bb16350dSlclee static int clear_efi(void);
362bb16350dSlclee static void clear_vtoc(int table, int part);
363bb16350dSlclee static int lecture_and_query(char *warning, char *devname);
364bb16350dSlclee static void sanity_check_provided_device(char *devname, int fd);
365bb16350dSlclee static char *get_node(char *devname);
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static void
3687c478bd9Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	if (table_changed) {
3717c478bd9Sstevel@tonic-gate 		/*
3727c478bd9Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
3737c478bd9Sstevel@tonic-gate 		 * and write it to disk
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 		copy_Table_to_Bootblk();
3767c478bd9Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
3807c478bd9Sstevel@tonic-gate 	if (io_adjt)
3817c478bd9Sstevel@tonic-gate 		fix_slice();
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate 	exit(0);
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate /*
3897c478bd9Sstevel@tonic-gate  * main
3907c478bd9Sstevel@tonic-gate  * Process command-line options.
3917c478bd9Sstevel@tonic-gate  */
392bb16350dSlclee int
3937c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3947c478bd9Sstevel@tonic-gate {
395bb16350dSlclee 	int c, i;
3967c478bd9Sstevel@tonic-gate 	extern	int optind;
3977c478bd9Sstevel@tonic-gate 	extern	char *optarg;
3987c478bd9Sstevel@tonic-gate 	int	errflg = 0;
3997c478bd9Sstevel@tonic-gate 	int	diag_cnt = 0;
4007c478bd9Sstevel@tonic-gate 	int openmode;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
4037c478bd9Sstevel@tonic-gate 	setbuf(stdout, 0);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/* Process the options. */
4067c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
4077c478bd9Sstevel@tonic-gate 	    != EOF) {
4087c478bd9Sstevel@tonic-gate 		switch (c) {
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			case 'o':
4117c478bd9Sstevel@tonic-gate 				io_offset = strtoul(optarg, 0, 0);
4127c478bd9Sstevel@tonic-gate 				continue;
4137c478bd9Sstevel@tonic-gate 			case 's':
4147c478bd9Sstevel@tonic-gate 				io_size = strtoul(optarg, 0, 0);
4157c478bd9Sstevel@tonic-gate 				continue;
4167c478bd9Sstevel@tonic-gate 			case 'P':
4177c478bd9Sstevel@tonic-gate 				diag_cnt++;
4187c478bd9Sstevel@tonic-gate 				io_patt++;
4197c478bd9Sstevel@tonic-gate 				io_fatt = optarg;
4207c478bd9Sstevel@tonic-gate 				continue;
4217c478bd9Sstevel@tonic-gate 			case 'w':
4227c478bd9Sstevel@tonic-gate 				diag_cnt++;
4237c478bd9Sstevel@tonic-gate 				io_wrt++;
4247c478bd9Sstevel@tonic-gate 				continue;
4257c478bd9Sstevel@tonic-gate 			case 'r':
4267c478bd9Sstevel@tonic-gate 				diag_cnt++;
4277c478bd9Sstevel@tonic-gate 				io_rd++;
4287c478bd9Sstevel@tonic-gate 				continue;
4297c478bd9Sstevel@tonic-gate 			case 'd':
4307c478bd9Sstevel@tonic-gate 				io_debug++;
4317c478bd9Sstevel@tonic-gate 				continue;
4327c478bd9Sstevel@tonic-gate 			case 'I':
4337c478bd9Sstevel@tonic-gate 				io_image++;
4347c478bd9Sstevel@tonic-gate 				continue;
4357c478bd9Sstevel@tonic-gate 			case 'R':
4367c478bd9Sstevel@tonic-gate 				io_readonly++;
4377c478bd9Sstevel@tonic-gate 				continue;
4387c478bd9Sstevel@tonic-gate 			case 'S':
4397c478bd9Sstevel@tonic-gate 				diag_cnt++;
4407c478bd9Sstevel@tonic-gate 				io_sgeom = optarg;
4417c478bd9Sstevel@tonic-gate 				continue;
4427c478bd9Sstevel@tonic-gate 			case 'T':
4437c478bd9Sstevel@tonic-gate 				io_ADJT++;
444bb16350dSlclee 				/* FALLTHRU */
4457c478bd9Sstevel@tonic-gate 			case 't':
4467c478bd9Sstevel@tonic-gate 				io_adjt++;
4477c478bd9Sstevel@tonic-gate 				continue;
4487c478bd9Sstevel@tonic-gate 			case 'B':
4497c478bd9Sstevel@tonic-gate 				io_wholedisk++;
4507c478bd9Sstevel@tonic-gate 				io_fdisk++;
4517c478bd9Sstevel@tonic-gate 				continue;
4527c478bd9Sstevel@tonic-gate 			case 'E':
4537c478bd9Sstevel@tonic-gate 				io_EFIdisk++;
4547c478bd9Sstevel@tonic-gate 				io_fdisk++;
4557c478bd9Sstevel@tonic-gate 				continue;
4567c478bd9Sstevel@tonic-gate 			case 'g':
4577c478bd9Sstevel@tonic-gate 				diag_cnt++;
4587c478bd9Sstevel@tonic-gate 				io_lgeom++;
4597c478bd9Sstevel@tonic-gate 				continue;
4607c478bd9Sstevel@tonic-gate 			case 'G':
4617c478bd9Sstevel@tonic-gate 				diag_cnt++;
4627c478bd9Sstevel@tonic-gate 				io_pgeom++;
4637c478bd9Sstevel@tonic-gate 				continue;
4647c478bd9Sstevel@tonic-gate 			case 'n':
4657c478bd9Sstevel@tonic-gate 				io_nifdisk++;
4667c478bd9Sstevel@tonic-gate 				io_fdisk++;
4677c478bd9Sstevel@tonic-gate 				continue;
4687c478bd9Sstevel@tonic-gate 			case 'F':
4697c478bd9Sstevel@tonic-gate 				io_fdisk++;
4707c478bd9Sstevel@tonic-gate 				io_ffdisk = optarg;
4717c478bd9Sstevel@tonic-gate 				continue;
4727c478bd9Sstevel@tonic-gate 			case 'b':
4737c478bd9Sstevel@tonic-gate 				io_mboot = optarg;
4747c478bd9Sstevel@tonic-gate 				continue;
4757c478bd9Sstevel@tonic-gate 			case 'W':
4767c478bd9Sstevel@tonic-gate 				/*
4777c478bd9Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
4787c478bd9Sstevel@tonic-gate 				 * to standard output, otherwise write
4797c478bd9Sstevel@tonic-gate 				 * to the specified file.
4807c478bd9Sstevel@tonic-gate 				 */
4817c478bd9Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
4827c478bd9Sstevel@tonic-gate 					stdo_flag = 1;
4837c478bd9Sstevel@tonic-gate 				else
4847c478bd9Sstevel@tonic-gate 					io_Wfdisk = optarg;
4857c478bd9Sstevel@tonic-gate 				io_fdisk++;
4867c478bd9Sstevel@tonic-gate 				continue;
4877c478bd9Sstevel@tonic-gate 			case 'A':
4887c478bd9Sstevel@tonic-gate 				io_fdisk++;
4897c478bd9Sstevel@tonic-gate 				io_Afdisk = optarg;
4907c478bd9Sstevel@tonic-gate 				continue;
4917c478bd9Sstevel@tonic-gate 			case 'D':
4927c478bd9Sstevel@tonic-gate 				io_fdisk++;
4937c478bd9Sstevel@tonic-gate 				io_Dfdisk = optarg;
4947c478bd9Sstevel@tonic-gate 				continue;
4957c478bd9Sstevel@tonic-gate 			case 'h':
496bb16350dSlclee 				(void) fprintf(stderr, "%s\n", Usage);
497bb16350dSlclee 				(void) fprintf(stderr, "%s\n", Usage1);
4987c478bd9Sstevel@tonic-gate 				exit(0);
499bb16350dSlclee 				/* FALLTHRU */
5007c478bd9Sstevel@tonic-gate 			case 'v':
5017c478bd9Sstevel@tonic-gate 				v_flag = 1;
5027c478bd9Sstevel@tonic-gate 				continue;
5037c478bd9Sstevel@tonic-gate 			case '?':
5047c478bd9Sstevel@tonic-gate 				errflg++;
5057c478bd9Sstevel@tonic-gate 				break;
5067c478bd9Sstevel@tonic-gate 		}
5077c478bd9Sstevel@tonic-gate 		break;
5087c478bd9Sstevel@tonic-gate 	}
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
5117c478bd9Sstevel@tonic-gate 		diag_cnt = 0;
5127c478bd9Sstevel@tonic-gate 	}
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	/* User option checking */
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	/* By default, run in interactive mode */
5177c478bd9Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
5187c478bd9Sstevel@tonic-gate 		io_ifdisk++;
5197c478bd9Sstevel@tonic-gate 		io_fdisk++;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
5227c478bd9Sstevel@tonic-gate 		errflg++;
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* Was any error detected? */
5267c478bd9Sstevel@tonic-gate 	if (errflg || argc == optind) {
527bb16350dSlclee 		(void) fprintf(stderr, "%s\n", Usage);
528bb16350dSlclee 		(void) fprintf(stderr,
5297c478bd9Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
5307c478bd9Sstevel@tonic-gate 		exit(2);
5317c478bd9Sstevel@tonic-gate 	}
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/* Figure out the correct device node to open */
5357c478bd9Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 	if (io_readonly)
5387c478bd9Sstevel@tonic-gate 		openmode = O_RDONLY;
5397c478bd9Sstevel@tonic-gate 	else
5407c478bd9Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
543bb16350dSlclee 		(void) fprintf(stderr,
544bb16350dSlclee 		    "fdisk: Cannot open device %s.\n",
545bb16350dSlclee 		    Dfltdev);
5467c478bd9Sstevel@tonic-gate 		exit(1);
5477c478bd9Sstevel@tonic-gate 	}
548*1237e847Slclee 	/*
549*1237e847Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
550*1237e847Slclee 	 * in that case leave the minfo structure zeroed
551*1237e847Slclee 	 */
552*1237e847Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
553*1237e847Slclee 		memset(&minfo, 0, sizeof (minfo));
554*1237e847Slclee 	}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	/* Get the disk geometry */
5577c478bd9Sstevel@tonic-gate 	if (!io_image) {
5587c478bd9Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
5597c478bd9Sstevel@tonic-gate 		errno = 0;
5607c478bd9Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			/*
5637c478bd9Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
5647c478bd9Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
5657c478bd9Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
5667c478bd9Sstevel@tonic-gate 			 */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			if (errno == ENOTTY) {
5697c478bd9Sstevel@tonic-gate 				v_flag = 0;
5707c478bd9Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
5717c478bd9Sstevel@tonic-gate 			} else if (errno == EINVAL) {
5727c478bd9Sstevel@tonic-gate 				/*
5737c478bd9Sstevel@tonic-gate 				 * This means that the ioctl exists, but
5747c478bd9Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
5757c478bd9Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
5767c478bd9Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
5777c478bd9Sstevel@tonic-gate 				 */
5787c478bd9Sstevel@tonic-gate 				v_flag = 0;
5797c478bd9Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
5807c478bd9Sstevel@tonic-gate 			} else {
5817c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5827c478bd9Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
5837c478bd9Sstevel@tonic-gate 				    argv[optind]);
5847c478bd9Sstevel@tonic-gate 				exit(1);
5857c478bd9Sstevel@tonic-gate 			}
5867c478bd9Sstevel@tonic-gate 		} else {
5877c478bd9Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
5887c478bd9Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
5897c478bd9Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
5907c478bd9Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
5917c478bd9Sstevel@tonic-gate 		}
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 		errno = 0;
5947c478bd9Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
5957c478bd9Sstevel@tonic-gate 			if (errno == ENOTTY) {
5967c478bd9Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
5977c478bd9Sstevel@tonic-gate 			} else {
5987c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
5997c478bd9Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
6007c478bd9Sstevel@tonic-gate 				    argv[optind]);
6017c478bd9Sstevel@tonic-gate 				exit(1);
6027c478bd9Sstevel@tonic-gate 			}
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		/*
6067c478bd9Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
6077c478bd9Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
6087c478bd9Sstevel@tonic-gate 		 */
6097c478bd9Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
6107c478bd9Sstevel@tonic-gate 			errno = 0;
6117c478bd9Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6127c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
6137c478bd9Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
6147c478bd9Sstevel@tonic-gate 				    argv[optind]);
6157c478bd9Sstevel@tonic-gate 				exit(1);
6167c478bd9Sstevel@tonic-gate 			}
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6207c478bd9Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6217c478bd9Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6227c478bd9Sstevel@tonic-gate 		sectsiz = 512;
6237c478bd9Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		/*
6267c478bd9Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
6277c478bd9Sstevel@tonic-gate 		 * or we got an invalid hba geometry
6287c478bd9Sstevel@tonic-gate 		 * then set hba geometry based on max values
6297c478bd9Sstevel@tonic-gate 		 */
6307c478bd9Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
631bb16350dSlclee 		    disk_geom.dkg_ncyl == 0 ||
632bb16350dSlclee 		    disk_geom.dkg_nhead == 0 ||
633bb16350dSlclee 		    disk_geom.dkg_nsect == 0 ||
6347c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
6357c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
6367c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 			/*
6397c478bd9Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
6407c478bd9Sstevel@tonic-gate 			 */
6417c478bd9Sstevel@tonic-gate 			v_flag = 0;
6427c478bd9Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
6437c478bd9Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
6447c478bd9Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
6457c478bd9Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 		if (io_debug) {
649bb16350dSlclee 			(void) fprintf(stderr, "Physical Geometry:\n");
650bb16350dSlclee 			(void) fprintf(stderr,
6517c478bd9Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6527c478bd9Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6537c478bd9Sstevel@tonic-gate 			    Numcyl,
6547c478bd9Sstevel@tonic-gate 			    heads,
6557c478bd9Sstevel@tonic-gate 			    sectors,
6567c478bd9Sstevel@tonic-gate 			    sectsiz,
657bb16350dSlclee 			    Numcyl * heads * sectors,
658bb16350dSlclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
659bb16350dSlclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
660bb16350dSlclee 			(void) fprintf(stderr,
6617c478bd9Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6627c478bd9Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6637c478bd9Sstevel@tonic-gate 			    hba_Numcyl,
6647c478bd9Sstevel@tonic-gate 			    hba_heads,
6657c478bd9Sstevel@tonic-gate 			    hba_sectors,
6667c478bd9Sstevel@tonic-gate 			    sectsiz,
667bb16350dSlclee 			    hba_Numcyl * hba_heads * hba_sectors,
668bb16350dSlclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
669bb16350dSlclee 			    1048576);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
6747c478bd9Sstevel@tonic-gate 	if (io_lgeom) {
6757c478bd9Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
6777c478bd9Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
6787c478bd9Sstevel@tonic-gate 			    argv[optind]);
6797c478bd9Sstevel@tonic-gate 			exit(1);
6807c478bd9Sstevel@tonic-gate 		}
6817c478bd9Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6827c478bd9Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6837c478bd9Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6847c478bd9Sstevel@tonic-gate 		sectsiz = 512;
6857c478bd9Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
686bb16350dSlclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
687bb16350dSlclee 		(void) printf(
688bb16350dSlclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
6897c478bd9Sstevel@tonic-gate 		    " SECSIZ\n");
690bb16350dSlclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
6917c478bd9Sstevel@tonic-gate 		    Numcyl,
6927c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
6937c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
6947c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
6957c478bd9Sstevel@tonic-gate 		    heads,
6967c478bd9Sstevel@tonic-gate 		    sectors,
6977c478bd9Sstevel@tonic-gate 		    sectsiz);
6987c478bd9Sstevel@tonic-gate 		exit(0);
6997c478bd9Sstevel@tonic-gate 	} else if (io_pgeom) {
7007c478bd9Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
7017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
7027c478bd9Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
7037c478bd9Sstevel@tonic-gate 			    argv[optind]);
7047c478bd9Sstevel@tonic-gate 			exit(1);
7057c478bd9Sstevel@tonic-gate 		}
706bb16350dSlclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
707bb16350dSlclee 		(void) printf(
708bb16350dSlclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
7097c478bd9Sstevel@tonic-gate 		    " SECSIZ\n");
710bb16350dSlclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
7117c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
7127c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
7137c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
7147c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7157c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
7167c478bd9Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
7177c478bd9Sstevel@tonic-gate 		    sectsiz);
7187c478bd9Sstevel@tonic-gate 		exit(0);
7197c478bd9Sstevel@tonic-gate 	} else if (io_sgeom) {
7207c478bd9Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
7217c478bd9Sstevel@tonic-gate 			exit(1);
7227c478bd9Sstevel@tonic-gate 		} else if (!io_image) {
7237c478bd9Sstevel@tonic-gate 			exit(0);
7247c478bd9Sstevel@tonic-gate 		}
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
727*1237e847Slclee 	/*
728*1237e847Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
729*1237e847Slclee 	 * in that case use CHS
730*1237e847Slclee 	 */
731*1237e847Slclee 	chs_capacity = Numcyl * heads * sectors;
732*1237e847Slclee 	dev_capacity = chs_capacity;
733*1237e847Slclee 	if (minfo.dki_capacity > 0)
734*1237e847Slclee 		dev_capacity = minfo.dki_capacity;
735*1237e847Slclee 
7367c478bd9Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
7377c478bd9Sstevel@tonic-gate 	Bootsect = (char *)malloc(3 * sectsiz);
7387c478bd9Sstevel@tonic-gate 	if (Bootsect == NULL) {
739bb16350dSlclee 		(void) fprintf(stderr,
7407c478bd9Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
7417c478bd9Sstevel@tonic-gate 		    " (%d bytes).\n",
742bb16350dSlclee 		    3 * sectsiz);
7437c478bd9Sstevel@tonic-gate 		exit(1);
7447c478bd9Sstevel@tonic-gate 	}
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
7477c478bd9Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
7487c478bd9Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
7497c478bd9Sstevel@tonic-gate 		Nullsect[i] = 0;
7507c478bd9Sstevel@tonic-gate 	}
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	/* Find out what the user wants done */
7537c478bd9Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
7547c478bd9Sstevel@tonic-gate 		abs_read();	/* will not return */
7557c478bd9Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
7567c478bd9Sstevel@tonic-gate 		abs_write();	/* will not return */
7577c478bd9Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
7587c478bd9Sstevel@tonic-gate 		fill_patt();	/* will not return */
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
7677c478bd9Sstevel@tonic-gate 	mboot_read();
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	/* Read from disk master boot */
7707c478bd9Sstevel@tonic-gate 	dev_mboot_read();
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/*
7737c478bd9Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
7747c478bd9Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
7757c478bd9Sstevel@tonic-gate 	 */
7767c478bd9Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
7777c478bd9Sstevel@tonic-gate 	copy_Bootblk_to_Table();
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
7807c478bd9Sstevel@tonic-gate 	copy_Table_to_Old_Table();
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
7837c478bd9Sstevel@tonic-gate 	if (io_ffdisk) {
7847c478bd9Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
7857c478bd9Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
7897c478bd9Sstevel@tonic-gate 	if (io_Dfdisk) {
7907c478bd9Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 	if (io_Afdisk) {
7937c478bd9Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
7977c478bd9Sstevel@tonic-gate 		/* Check if there is no fdisk table */
7987c478bd9Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
7997c478bd9Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
800bb16350dSlclee 				(void) printf(
801bb16350dSlclee 				    "No fdisk table exists. The default"
802bb16350dSlclee 				    " partition for the disk is:\n\n"
803bb16350dSlclee 				    "  a 100%% \"SOLARIS System\" "
804bb16350dSlclee 				    "partition\n\n"
805bb16350dSlclee 				    "Type \"y\" to accept the default "
8067c478bd9Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
8077c478bd9Sstevel@tonic-gate 				    "edit the\n partition table.\n");
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 			/* Edit the partition table as directed */
8117c478bd9Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 				/* Default scenario */
8147c478bd9Sstevel@tonic-gate 				nulltbl();
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 				/* now set up UNIX System partition */
8177c478bd9Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
8187c478bd9Sstevel@tonic-gate 				Table[0].relsect = lel(heads * sectors);
819bb16350dSlclee 				Table[0].numsect = lel((long)((Numcyl - 1) *
8207c478bd9Sstevel@tonic-gate 				    heads * sectors));
8217c478bd9Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
8247c478bd9Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8277c478bd9Sstevel@tonic-gate 			} else if (io_EFIdisk) {
8287c478bd9Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
8297c478bd9Sstevel@tonic-gate 				nulltbl();
8307c478bd9Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
831*1237e847Slclee 				    dev_capacity - 1);
8327c478bd9Sstevel@tonic-gate 				if (i != 0) {
833bb16350dSlclee 					(void) fprintf(stderr,
834bb16350dSlclee 					    "Error creating EFI partition\n");
8357c478bd9Sstevel@tonic-gate 					exit(1);
8367c478bd9Sstevel@tonic-gate 				}
8377c478bd9Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 
8427c478bd9Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
8437c478bd9Sstevel@tonic-gate 	if (io_debug) {
844bb16350dSlclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
8457c478bd9Sstevel@tonic-gate 		print_Table();
8467c478bd9Sstevel@tonic-gate 		if (io_ifdisk) {
847bb16350dSlclee 			(void) fprintf(stderr, "\n");
848bb16350dSlclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
849bb16350dSlclee 			(void) gets(s);
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 	}
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	/* Interactive fdisk mode */
8547c478bd9Sstevel@tonic-gate 	if (io_ifdisk) {
855bb16350dSlclee 		(void) printf(CLR_SCR);
8567c478bd9Sstevel@tonic-gate 		disptbl();
857bb16350dSlclee 		for (;;) {
858bb16350dSlclee 			stage0();
8597c478bd9Sstevel@tonic-gate 			copy_Bootblk_to_Table();
8607c478bd9Sstevel@tonic-gate 			disptbl();
8617c478bd9Sstevel@tonic-gate 		}
8627c478bd9Sstevel@tonic-gate 	}
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
8657c478bd9Sstevel@tonic-gate 	if (io_Wfdisk)
8667c478bd9Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
8677c478bd9Sstevel@tonic-gate 	else if (stdo_flag)
8687c478bd9Sstevel@tonic-gate 		ffile_write((char *)stdout);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
871bb16350dSlclee 	return (0);
8727c478bd9Sstevel@tonic-gate }
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate  * read_geom
8767c478bd9Sstevel@tonic-gate  * Read geometry from specified file (-S).
8777c478bd9Sstevel@tonic-gate  */
8787c478bd9Sstevel@tonic-gate 
879bb16350dSlclee static int
880bb16350dSlclee read_geom(char *sgeom)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate 	char	line[256];
8837c478bd9Sstevel@tonic-gate 	FILE *fp;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 	/* open the prototype file */
8867c478bd9Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
8877c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
8887c478bd9Sstevel@tonic-gate 		    io_sgeom);
8897c478bd9Sstevel@tonic-gate 		return (1);
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	/* Read a line from the file */
8937c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
8947c478bd9Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
8957c478bd9Sstevel@tonic-gate 			continue;
8967c478bd9Sstevel@tonic-gate 		else {
8977c478bd9Sstevel@tonic-gate 			line[strlen(line)] = '\0';
898bb16350dSlclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
8997c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
9007c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
9017c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
9027c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
9037c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
9047c478bd9Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
9057c478bd9Sstevel@tonic-gate 			    &sectsiz) != 7) {
9067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
9077c478bd9Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
9087c478bd9Sstevel@tonic-gate 				    line);
9097c478bd9Sstevel@tonic-gate 				return (1);
9107c478bd9Sstevel@tonic-gate 			}
9117c478bd9Sstevel@tonic-gate 			break;
9127c478bd9Sstevel@tonic-gate 		} /* else */
9137c478bd9Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (!io_image) {
9167c478bd9Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
9177c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
9187c478bd9Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
9197c478bd9Sstevel@tonic-gate 			return (1);
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 	} else {
9227c478bd9Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
9237c478bd9Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
9247c478bd9Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
9257c478bd9Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 
928bb16350dSlclee 	(void) fclose(fp);
9297c478bd9Sstevel@tonic-gate 	return (0);
9307c478bd9Sstevel@tonic-gate }
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate /*
9337c478bd9Sstevel@tonic-gate  * dev_mboot_read
9347c478bd9Sstevel@tonic-gate  * Read the master boot sector from the device.
9357c478bd9Sstevel@tonic-gate  */
936bb16350dSlclee static void
937bb16350dSlclee dev_mboot_read(void)
9387c478bd9Sstevel@tonic-gate {
9397c478bd9Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
9407c478bd9Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
9417c478bd9Sstevel@tonic-gate 	}
9427c478bd9Sstevel@tonic-gate 	if (errno == 0)
9437c478bd9Sstevel@tonic-gate 		return;
9447c478bd9Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
945bb16350dSlclee 		(void) fprintf(stderr,
9467c478bd9Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
9477c478bd9Sstevel@tonic-gate 		    Dfltdev);
9487c478bd9Sstevel@tonic-gate 		if (!io_image)
9497c478bd9Sstevel@tonic-gate 			exit(1);
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
952bb16350dSlclee 		(void) fprintf(stderr,
9537c478bd9Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
9547c478bd9Sstevel@tonic-gate 		    Dfltdev);
9557c478bd9Sstevel@tonic-gate 		if (!io_image)
9567c478bd9Sstevel@tonic-gate 			exit(1);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate  * dev_mboot_write
9627c478bd9Sstevel@tonic-gate  * Write the master boot sector to the device.
9637c478bd9Sstevel@tonic-gate  */
964bb16350dSlclee static void
9657c478bd9Sstevel@tonic-gate dev_mboot_write(int sect, char *buff, int bootsiz)
9667c478bd9Sstevel@tonic-gate {
9677c478bd9Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
9687c478bd9Sstevel@tonic-gate 	int	clr_efi = -1, old_solaris = -1, new_solaris = -1;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (io_readonly)
971bb16350dSlclee 		return;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (io_debug) {
974bb16350dSlclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
9757c478bd9Sstevel@tonic-gate 		print_Table();
9767c478bd9Sstevel@tonic-gate 		if (io_ifdisk) {
977bb16350dSlclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
978bb16350dSlclee 			(void) gets(s);
9797c478bd9Sstevel@tonic-gate 		}
9807c478bd9Sstevel@tonic-gate 	}
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/* see if the old table had EFI or Solaris partitions */
9837c478bd9Sstevel@tonic-gate 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
9847c478bd9Sstevel@tonic-gate 		if (Old_Table[old_pt].systid == SUNIXOS ||
9857c478bd9Sstevel@tonic-gate 		    Old_Table[old_pt].systid == SUNIXOS2) {
9867c478bd9Sstevel@tonic-gate 			old_solaris = old_pt;
9877c478bd9Sstevel@tonic-gate 		} else if (Old_Table[old_pt].systid == EFI_PMBR) {
9887c478bd9Sstevel@tonic-gate 			clr_efi = old_pt;
9897c478bd9Sstevel@tonic-gate 		}
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate 	/* look to see if Solaris partition changed in relsect/numsect */
9937c478bd9Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 		/*
9967c478bd9Sstevel@tonic-gate 		 * if this is not a Solaris partition, ignore it
9977c478bd9Sstevel@tonic-gate 		 */
9987c478bd9Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
9997c478bd9Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
10007c478bd9Sstevel@tonic-gate 			continue;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		/*
10037c478bd9Sstevel@tonic-gate 		 * if there was no previous Solaris partition
10047c478bd9Sstevel@tonic-gate 		 * or if the old partition was in a different place
10057c478bd9Sstevel@tonic-gate 		 * or if the old partition was a different size
10067c478bd9Sstevel@tonic-gate 		 * then this must be a new Solaris partition
10077c478bd9Sstevel@tonic-gate 		 */
10087c478bd9Sstevel@tonic-gate 		if (old_solaris == -1 ||
10097c478bd9Sstevel@tonic-gate 		    Old_Table[old_solaris].relsect != Table[new_pt].relsect ||
10107c478bd9Sstevel@tonic-gate 		    Old_Table[old_solaris].numsect != Table[new_pt].numsect) {
10117c478bd9Sstevel@tonic-gate 			new_solaris = new_pt;
10127c478bd9Sstevel@tonic-gate 			break;
10137c478bd9Sstevel@tonic-gate 		}
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
10177c478bd9Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10187c478bd9Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
10197c478bd9Sstevel@tonic-gate 			continue;
10207c478bd9Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
1021*1237e847Slclee 			if ((Old_Table[old_pt].systid ==
1022*1237e847Slclee 			    Table[new_pt].systid) &&
1023*1237e847Slclee 			    (Old_Table[old_pt].relsect ==
1024*1237e847Slclee 			    Table[new_pt].relsect) &&
1025*1237e847Slclee 			    (Old_Table[old_pt].numsect ==
1026*1237e847Slclee 			    Table[new_pt].numsect))
1027*1237e847Slclee 				break;
10287c478bd9Sstevel@tonic-gate 		}
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate 		/*
10317c478bd9Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
10327c478bd9Sstevel@tonic-gate 		 * the EFI GPT
10337c478bd9Sstevel@tonic-gate 		 */
10347c478bd9Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
10357c478bd9Sstevel@tonic-gate 			clr_efi = 0;
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 		break;
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	/* clear labels if necessary */
10417c478bd9Sstevel@tonic-gate 	if (clr_efi >= 0) {
10427c478bd9Sstevel@tonic-gate 		if (io_debug) {
1043bb16350dSlclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
10467c478bd9Sstevel@tonic-gate 			if (io_debug) {
1047bb16350dSlclee 				(void) fprintf(stderr,
1048bb16350dSlclee 				    "\tError %d clearing EFI labels"
10497c478bd9Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
10507c478bd9Sstevel@tonic-gate 				    error);
10517c478bd9Sstevel@tonic-gate 			}
10527c478bd9Sstevel@tonic-gate 		}
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	if (new_solaris >= 0) {
10567c478bd9Sstevel@tonic-gate 		if (io_debug) {
1057bb16350dSlclee 			(void) fprintf(stderr, "Clearing VTOC labels from NEW"
10587c478bd9Sstevel@tonic-gate 			    " table\n");
10597c478bd9Sstevel@tonic-gate 		}
10607c478bd9Sstevel@tonic-gate 		clear_vtoc(NEW, new_solaris);
10617c478bd9Sstevel@tonic-gate 	}
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1064bb16350dSlclee 		(void) fprintf(stderr,
10657c478bd9Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
10667c478bd9Sstevel@tonic-gate 		    Dfltdev);
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 	if (errno == 0)
10697c478bd9Sstevel@tonic-gate 		return;
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	/* write to disk drive */
10727c478bd9Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1073bb16350dSlclee 		(void) fprintf(stderr,
10747c478bd9Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
10757c478bd9Sstevel@tonic-gate 		    Dfltdev);
10767c478bd9Sstevel@tonic-gate 		exit(1);
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1079bb16350dSlclee 		(void) fprintf(stderr,
10807c478bd9Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
10817c478bd9Sstevel@tonic-gate 		    Dfltdev);
10827c478bd9Sstevel@tonic-gate 		exit(1);
10837c478bd9Sstevel@tonic-gate 	}
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate /*
10877c478bd9Sstevel@tonic-gate  * mboot_read
10887c478bd9Sstevel@tonic-gate  * Read the prototype boot records from the files.
10897c478bd9Sstevel@tonic-gate  */
1090bb16350dSlclee static void
1091bb16350dSlclee mboot_read(void)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	int mDev, i;
10947c478bd9Sstevel@tonic-gate 	struct ipart *part;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate #if defined(i386) || defined(sparc)
10977c478bd9Sstevel@tonic-gate 	/*
10987c478bd9Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
10997c478bd9Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
11007c478bd9Sstevel@tonic-gate 	 */
11017c478bd9Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
11027c478bd9Sstevel@tonic-gate 		/*
11037c478bd9Sstevel@tonic-gate 		 * Bug ID 1249035:
11047c478bd9Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
11057c478bd9Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
11067c478bd9Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
11077c478bd9Sstevel@tonic-gate 		 */
11087c478bd9Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate 	/* First read in the master boot record */
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 	/* Open the master boot proto file */
11147c478bd9Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1115bb16350dSlclee 		(void) fprintf(stderr,
11167c478bd9Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
11177c478bd9Sstevel@tonic-gate 		    io_mboot);
11187c478bd9Sstevel@tonic-gate 		exit(1);
11197c478bd9Sstevel@tonic-gate 	}
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/* Read the master boot program */
11227c478bd9Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
11237c478bd9Sstevel@tonic-gate 	    (struct mboot)) {
1124bb16350dSlclee 		(void) fprintf(stderr,
11257c478bd9Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
11267c478bd9Sstevel@tonic-gate 		    io_mboot);
11277c478bd9Sstevel@tonic-gate 		exit(1);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	/* Is this really a master boot record? */
11317c478bd9Sstevel@tonic-gate 	if (les(BootCod.signature) != MBB_MAGIC) {
1132bb16350dSlclee 		(void) fprintf(stderr,
11337c478bd9Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1134bb16350dSlclee 		(void) fprintf(stderr,
1135bb16350dSlclee 		    "Bad magic number: is %x, but should be %x.\n",
11367c478bd9Sstevel@tonic-gate 		    les(BootCod.signature), MBB_MAGIC);
11377c478bd9Sstevel@tonic-gate 		exit(1);
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate 
1140bb16350dSlclee 	(void) close(mDev);
11417c478bd9Sstevel@tonic-gate #else
11427c478bd9Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
11437c478bd9Sstevel@tonic-gate #endif
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
11467c478bd9Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
11477c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1148bb16350dSlclee 		(void) memset(part, 0, sizeof (struct ipart));
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate /*
11547c478bd9Sstevel@tonic-gate  * fill_patt
11557c478bd9Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
11567c478bd9Sstevel@tonic-gate  */
1157bb16350dSlclee static void
1158bb16350dSlclee fill_patt(void)
11597c478bd9Sstevel@tonic-gate {
1160bb16350dSlclee 	int	*buff_ptr, i;
11617c478bd9Sstevel@tonic-gate 	int	io_fpatt = 0;
11627c478bd9Sstevel@tonic-gate 	int	io_ipatt = 0;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
11657c478bd9Sstevel@tonic-gate 		io_fpatt++;
11667c478bd9Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
11677c478bd9Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11687c478bd9Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
1169*1237e847Slclee 			*buff_ptr = io_ipatt;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	/*
11737c478bd9Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
11747c478bd9Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
11757c478bd9Sstevel@tonic-gate 	 * for io_size blocks.
11767c478bd9Sstevel@tonic-gate 	 */
11777c478bd9Sstevel@tonic-gate 	while (io_size--) {
11787c478bd9Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11797c478bd9Sstevel@tonic-gate 		if (!io_fpatt) {
11807c478bd9Sstevel@tonic-gate 			for (i = 0; i < sectsiz; i += 4, buff_ptr++)
11817c478bd9Sstevel@tonic-gate 				*buff_ptr = io_offset;
11827c478bd9Sstevel@tonic-gate 		}
11837c478bd9Sstevel@tonic-gate 		/* Write the data to disk */
11847c478bd9Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1185bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
1186*1237e847Slclee 			    Dfltdev);
11877c478bd9Sstevel@tonic-gate 			exit(1);
11887c478bd9Sstevel@tonic-gate 		}
11897c478bd9Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1190bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
1191*1237e847Slclee 			    Dfltdev);
11927c478bd9Sstevel@tonic-gate 			exit(1);
11937c478bd9Sstevel@tonic-gate 		}
11947c478bd9Sstevel@tonic-gate 	} /* while (--io_size); */
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate /*
11987c478bd9Sstevel@tonic-gate  * abs_read
11997c478bd9Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
12007c478bd9Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
12017c478bd9Sstevel@tonic-gate  */
1202bb16350dSlclee static void
1203bb16350dSlclee abs_read(void)
1204bb16350dSlclee {
12057c478bd9Sstevel@tonic-gate 	int c;
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	while (io_size--) {
12087c478bd9Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1209bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12107c478bd9Sstevel@tonic-gate 			    Dfltdev);
12117c478bd9Sstevel@tonic-gate 			exit(1);
12127c478bd9Sstevel@tonic-gate 		}
12137c478bd9Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1214bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
12157c478bd9Sstevel@tonic-gate 			    Dfltdev);
12167c478bd9Sstevel@tonic-gate 			exit(1);
12177c478bd9Sstevel@tonic-gate 		}
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		/* Write to standard ouptut */
1220bb16350dSlclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12217c478bd9Sstevel@tonic-gate 			if (c >= 0) {
12227c478bd9Sstevel@tonic-gate 				if (io_debug)
1223bb16350dSlclee 					(void) fprintf(stderr,
1224bb16350dSlclee 					    "fdisk: Output warning: %d of %d"
1225bb16350dSlclee 					    " characters written.\n",
1226bb16350dSlclee 					    c, sectsiz);
12277c478bd9Sstevel@tonic-gate 				exit(2);
12287c478bd9Sstevel@tonic-gate 			} else {
12297c478bd9Sstevel@tonic-gate 				perror("write error on output file.");
12307c478bd9Sstevel@tonic-gate 				exit(2);
12317c478bd9Sstevel@tonic-gate 			}
12327c478bd9Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
12337c478bd9Sstevel@tonic-gate 			/* != sectsiz) */
12347c478bd9Sstevel@tonic-gate 	} /* while (--io_size); */
12357c478bd9Sstevel@tonic-gate 	exit(0);
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate  * abs_write
12407c478bd9Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
12417c478bd9Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
12427c478bd9Sstevel@tonic-gate  */
1243bb16350dSlclee static void
1244bb16350dSlclee abs_write(void)
12457c478bd9Sstevel@tonic-gate {
12467c478bd9Sstevel@tonic-gate 	int c, i;
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate 	while (io_size--) {
12497c478bd9Sstevel@tonic-gate 		int part_exit = 0;
12507c478bd9Sstevel@tonic-gate 		/* Read from standard input */
12517c478bd9Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12527c478bd9Sstevel@tonic-gate 			if (c >= 0) {
12537c478bd9Sstevel@tonic-gate 				if (io_debug)
1254bb16350dSlclee 				(void) fprintf(stderr,
12557c478bd9Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
12567c478bd9Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
1257*1237e847Slclee 				    c, sectsiz);
12587c478bd9Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
12597c478bd9Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
12607c478bd9Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
12617c478bd9Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
12627c478bd9Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
12637c478bd9Sstevel@tonic-gate 					Bootsect[i++] = 0;
12647c478bd9Sstevel@tonic-gate 				}
12657c478bd9Sstevel@tonic-gate 				part_exit++;
12667c478bd9Sstevel@tonic-gate 			} else {
12677c478bd9Sstevel@tonic-gate 				perror("read error on input file.");
12687c478bd9Sstevel@tonic-gate 				exit(2);
12697c478bd9Sstevel@tonic-gate 			}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		}
12727c478bd9Sstevel@tonic-gate 		/* Write to disk drive */
12737c478bd9Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1274bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12757c478bd9Sstevel@tonic-gate 			    Dfltdev);
12767c478bd9Sstevel@tonic-gate 			exit(1);
12777c478bd9Sstevel@tonic-gate 		}
12787c478bd9Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1279bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12807c478bd9Sstevel@tonic-gate 			    Dfltdev);
12817c478bd9Sstevel@tonic-gate 			exit(1);
12827c478bd9Sstevel@tonic-gate 		}
12837c478bd9Sstevel@tonic-gate 		if (part_exit)
12847c478bd9Sstevel@tonic-gate 		exit(0);
12857c478bd9Sstevel@tonic-gate 	} /* while (--io_size); */
12867c478bd9Sstevel@tonic-gate 	exit(1);
12877c478bd9Sstevel@tonic-gate }
12887c478bd9Sstevel@tonic-gate 
1289bb16350dSlclee 
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate  * load
12927c478bd9Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
12937c478bd9Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
12947c478bd9Sstevel@tonic-gate  */
12957c478bd9Sstevel@tonic-gate 
1296bb16350dSlclee static void
1297bb16350dSlclee load(int funct, char *file)
12987c478bd9Sstevel@tonic-gate {
12997c478bd9Sstevel@tonic-gate 	int	id;
13007c478bd9Sstevel@tonic-gate 	int	act;
13017c478bd9Sstevel@tonic-gate 	int	bhead;
13027c478bd9Sstevel@tonic-gate 	int	bsect;
13037c478bd9Sstevel@tonic-gate 	int	bcyl;
13047c478bd9Sstevel@tonic-gate 	int	ehead;
13057c478bd9Sstevel@tonic-gate 	int	esect;
13067c478bd9Sstevel@tonic-gate 	int	ecyl;
13077c478bd9Sstevel@tonic-gate 	int	rsect;
13087c478bd9Sstevel@tonic-gate 	int	numsect;
13097c478bd9Sstevel@tonic-gate 	char	line[256];
13107c478bd9Sstevel@tonic-gate 	int	i = 0;
13117c478bd9Sstevel@tonic-gate 	int	j;
13127c478bd9Sstevel@tonic-gate 	FILE *fp;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	switch (funct) {
13157c478bd9Sstevel@tonic-gate 
1316*1237e847Slclee 	case LOADFILE:
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 		/*
13197c478bd9Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
13207c478bd9Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
13217c478bd9Sstevel@tonic-gate 		 * fdisk_file).
13227c478bd9Sstevel@tonic-gate 		 */
13237c478bd9Sstevel@tonic-gate 		nulltbl();
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 		/* Open the prototype file */
13267c478bd9Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
13277c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13287c478bd9Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
13297c478bd9Sstevel@tonic-gate 			    file);
13307c478bd9Sstevel@tonic-gate 			exit(1);
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 		/* Read a line from the file */
13347c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
13357c478bd9Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
13367c478bd9Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
13377c478bd9Sstevel@tonic-gate 				continue;
13387c478bd9Sstevel@tonic-gate 			}
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 			/*
13417c478bd9Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
13427c478bd9Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
13437c478bd9Sstevel@tonic-gate 			 */
13447c478bd9Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
13457c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13467c478bd9Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13477c478bd9Sstevel@tonic-gate 				    line);
13487c478bd9Sstevel@tonic-gate 				exit(1);
13497c478bd9Sstevel@tonic-gate 			}
13507c478bd9Sstevel@tonic-gate 			/*
13517c478bd9Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
13527c478bd9Sstevel@tonic-gate 			 * in table
13537c478bd9Sstevel@tonic-gate 			 */
13547c478bd9Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
13557c478bd9Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
13567c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
13577c478bd9Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13587c478bd9Sstevel@tonic-gate 				    line);
13597c478bd9Sstevel@tonic-gate 				exit(1);
13607c478bd9Sstevel@tonic-gate 			}
13617c478bd9Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1364bb16350dSlclee 			(void) fprintf(stderr,
13657c478bd9Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
13667c478bd9Sstevel@tonic-gate 			exit(1);
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 
1369bb16350dSlclee 		(void) fclose(fp);
13707c478bd9Sstevel@tonic-gate 		return;
13717c478bd9Sstevel@tonic-gate 
1372*1237e847Slclee 	case LOADDEL:
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
1375bb16350dSlclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
1376bb16350dSlclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
1377bb16350dSlclee 			(void) fprintf(stderr,
1378bb16350dSlclee 			    "fdisk: Syntax error \"%s\"\n", file);
1379bb16350dSlclee 			exit(1);
1380bb16350dSlclee 		}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 		/* Find the exact entry in the table */
13837c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
13847c478bd9Sstevel@tonic-gate 			if (Table[i].systid == id &&
13857c478bd9Sstevel@tonic-gate 			    Table[i].bootid == act &&
13867c478bd9Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
13877c478bd9Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
1388*1237e847Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
1389bb16350dSlclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
13907c478bd9Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
13917c478bd9Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
1392*1237e847Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
1393bb16350dSlclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
13947c478bd9Sstevel@tonic-gate 			    Table[i].relsect == lel(rsect) &&
13957c478bd9Sstevel@tonic-gate 			    Table[i].numsect == lel(numsect)) {
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 				/*
13987c478bd9Sstevel@tonic-gate 				 * Found the entry. Now move rest of
13997c478bd9Sstevel@tonic-gate 				 * entries up toward the top of the
14007c478bd9Sstevel@tonic-gate 				 * table, leaving available entries at
14017c478bd9Sstevel@tonic-gate 				 * the end of the fdisk table.
14027c478bd9Sstevel@tonic-gate 				 */
1403bb16350dSlclee 				for (j = i; j < FD_NUMPART - 1; j++) {
1404bb16350dSlclee 					Table[j].systid = Table[j + 1].systid;
1405bb16350dSlclee 					Table[j].bootid = Table[j + 1].bootid;
1406bb16350dSlclee 					Table[j].beghead = Table[j + 1].beghead;
1407bb16350dSlclee 					Table[j].begsect = Table[j + 1].begsect;
1408bb16350dSlclee 					Table[j].begcyl = Table[j + 1].begcyl;
1409bb16350dSlclee 					Table[j].endhead = Table[j + 1].endhead;
1410bb16350dSlclee 					Table[j].endsect = Table[j + 1].endsect;
1411bb16350dSlclee 					Table[j].endcyl = Table[j + 1].endcyl;
1412bb16350dSlclee 					Table[j].relsect = Table[j + 1].relsect;
1413bb16350dSlclee 					Table[j].numsect = Table[j + 1].numsect;
14147c478bd9Sstevel@tonic-gate 				}
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 				/*
14177c478bd9Sstevel@tonic-gate 				 * Mark the last entry as unused in case
14187c478bd9Sstevel@tonic-gate 				 * all table entries were in use prior
14197c478bd9Sstevel@tonic-gate 				 * to the deletion.
14207c478bd9Sstevel@tonic-gate 				 */
14217c478bd9Sstevel@tonic-gate 
1422bb16350dSlclee 				Table[FD_NUMPART - 1].systid = UNUSED;
1423bb16350dSlclee 				Table[FD_NUMPART - 1].bootid = 0;
14247c478bd9Sstevel@tonic-gate 				return;
14257c478bd9Sstevel@tonic-gate 			}
14267c478bd9Sstevel@tonic-gate 		}
1427bb16350dSlclee 		(void) fprintf(stderr,
14287c478bd9Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
14297c478bd9Sstevel@tonic-gate 		    "	\"%s\"\n",
14307c478bd9Sstevel@tonic-gate 		    file);
14317c478bd9Sstevel@tonic-gate 		exit(1);
1432bb16350dSlclee 		/* FALLTHRU */
14337c478bd9Sstevel@tonic-gate 
1434*1237e847Slclee 	case LOADADD:
14357c478bd9Sstevel@tonic-gate 
14367c478bd9Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
1437bb16350dSlclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
1438bb16350dSlclee 		    &esect, &ecyl, &rsect, &numsect)) {
1439bb16350dSlclee 			(void) fprintf(stderr,
1440bb16350dSlclee 			    "fdisk: Syntax error \"%s\"\n", file);
1441bb16350dSlclee 			exit(1);
1442bb16350dSlclee 		}
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
14457c478bd9Sstevel@tonic-gate 		if (rsect == 0) {
14467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14477c478bd9Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
14487c478bd9Sstevel@tonic-gate 			    "   \"%s\".\n",
14497c478bd9Sstevel@tonic-gate 			    file);
14507c478bd9Sstevel@tonic-gate 			exit(1);
14517c478bd9Sstevel@tonic-gate 		}
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 		/*
14547c478bd9Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
14557c478bd9Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
14567c478bd9Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
14577c478bd9Sstevel@tonic-gate 		 */
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 		if (id == EFI_PMBR) {
14607c478bd9Sstevel@tonic-gate 			if (rsect != 1) {
14617c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
14627c478bd9Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
14637c478bd9Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
14647c478bd9Sstevel@tonic-gate 				exit(1);
14657c478bd9Sstevel@tonic-gate 			}
14667c478bd9Sstevel@tonic-gate 
1467*1237e847Slclee 			if (numsect != dev_capacity - 1) {
14687c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
14697c478bd9Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
1470bb16350dSlclee 				    "entire disk\n"
1471*1237e847Slclee 				    "(input numsect: %d - avail: %llu)\n",
1472bb16350dSlclee 				    numsect,
1473*1237e847Slclee 				    dev_capacity - 1);
14747c478bd9Sstevel@tonic-gate 				exit(1);
14757c478bd9Sstevel@tonic-gate 			}
14767c478bd9Sstevel@tonic-gate 		}
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
14797c478bd9Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
14807c478bd9Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
14817c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
14827c478bd9Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
14837c478bd9Sstevel@tonic-gate 			    "	\"%s\"\n",
14847c478bd9Sstevel@tonic-gate 			    file);
14857c478bd9Sstevel@tonic-gate 			exit(1);
14867c478bd9Sstevel@tonic-gate 		}
14877c478bd9Sstevel@tonic-gate 
14887c478bd9Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
14897c478bd9Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1490bb16350dSlclee 			(void) fprintf(stderr,
1491bb16350dSlclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
14927c478bd9Sstevel@tonic-gate 			exit(1);
14937c478bd9Sstevel@tonic-gate 		}
14947c478bd9Sstevel@tonic-gate 	} /* switch funct */
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate /*
14987c478bd9Sstevel@tonic-gate  * Set_Table_CHS_Values
14997c478bd9Sstevel@tonic-gate  *
15007c478bd9Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
15017c478bd9Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
15027c478bd9Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
15037c478bd9Sstevel@tonic-gate  *
15047c478bd9Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
15057c478bd9Sstevel@tonic-gate  *
15067c478bd9Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
15077c478bd9Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
15087c478bd9Sstevel@tonic-gate  */
15097c478bd9Sstevel@tonic-gate static void
15107c478bd9Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
15117c478bd9Sstevel@tonic-gate {
15127c478bd9Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
15157c478bd9Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15167c478bd9Sstevel@tonic-gate 		/*
15177c478bd9Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
15187c478bd9Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
15197c478bd9Sstevel@tonic-gate 		 */
15207c478bd9Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15217c478bd9Sstevel@tonic-gate 		hd = MAX_HEAD;
15227c478bd9Sstevel@tonic-gate 		sc = MAX_SECT;
15237c478bd9Sstevel@tonic-gate 	} else {
15247c478bd9Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15257c478bd9Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15267c478bd9Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15277c478bd9Sstevel@tonic-gate 	}
15287c478bd9Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
1529bb16350dSlclee 	Table[ti].beghead = (uchar_t)hd;
1530bb16350dSlclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 	/*
15337c478bd9Sstevel@tonic-gate 	 * This code is identical to the code above
15347c478bd9Sstevel@tonic-gate 	 * except that it works on ending CHS values
15357c478bd9Sstevel@tonic-gate 	 */
15367c478bd9Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
15377c478bd9Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15387c478bd9Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15397c478bd9Sstevel@tonic-gate 		hd = MAX_HEAD;
15407c478bd9Sstevel@tonic-gate 		sc = MAX_SECT;
15417c478bd9Sstevel@tonic-gate 	} else {
15427c478bd9Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15437c478bd9Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15447c478bd9Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15457c478bd9Sstevel@tonic-gate 	}
15467c478bd9Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
1547bb16350dSlclee 	Table[ti].endhead = (uchar_t)hd;
1548bb16350dSlclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate /*
15527c478bd9Sstevel@tonic-gate  * insert_tbl
15537c478bd9Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
15547c478bd9Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
15557c478bd9Sstevel@tonic-gate  *	entries!
15567c478bd9Sstevel@tonic-gate  */
1557bb16350dSlclee static int
1558bb16350dSlclee insert_tbl(
1559bb16350dSlclee     int id, int act,
1560bb16350dSlclee     int bhead, int bsect, int bcyl,
1561bb16350dSlclee     int ehead, int esect, int ecyl,
1562bb16350dSlclee     int rsect, int numsect)
15637c478bd9Sstevel@tonic-gate {
15647c478bd9Sstevel@tonic-gate 	int	i;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 	/* validate partition size */
1567*1237e847Slclee 	if (rsect + numsect > dev_capacity) {
1568bb16350dSlclee 		(void) fprintf(stderr,
15697c478bd9Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
15707c478bd9Sstevel@tonic-gate 		return (-1);
15717c478bd9Sstevel@tonic-gate 	}
15727c478bd9Sstevel@tonic-gate 
15737c478bd9Sstevel@tonic-gate 	/* find UNUSED partition table entry */
15747c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
15757c478bd9Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
15767c478bd9Sstevel@tonic-gate 			break;
15777c478bd9Sstevel@tonic-gate 		}
15787c478bd9Sstevel@tonic-gate 	}
15797c478bd9Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
1580bb16350dSlclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
15817c478bd9Sstevel@tonic-gate 		return (-1);
15827c478bd9Sstevel@tonic-gate 	}
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 
1585bb16350dSlclee 	Table[i].systid = (uchar_t)id;
1586bb16350dSlclee 	Table[i].bootid = (uchar_t)act;
15877c478bd9Sstevel@tonic-gate 	Table[i].numsect = lel(numsect);
15887c478bd9Sstevel@tonic-gate 	Table[i].relsect = lel(rsect);
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
15927c478bd9Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
15937c478bd9Sstevel@tonic-gate 	 */
15947c478bd9Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
15957c478bd9Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
15967c478bd9Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
15977c478bd9Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
15987c478bd9Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
15997c478bd9Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
16007c478bd9Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
16017c478bd9Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
16027c478bd9Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
16037c478bd9Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
1604bb16350dSlclee 		Table[i].beghead = (uchar_t)bhead;
1605bb16350dSlclee 		Table[i].endhead = (uchar_t)ehead;
1606bb16350dSlclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
16077c478bd9Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
16087c478bd9Sstevel@tonic-gate 	} else {
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 		/*
16117c478bd9Sstevel@tonic-gate 		 * The specified values are invalid,
16127c478bd9Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
16137c478bd9Sstevel@tonic-gate 		 */
16147c478bd9Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
16157c478bd9Sstevel@tonic-gate 	}
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	/*
16187c478bd9Sstevel@tonic-gate 	 * return partition index
16197c478bd9Sstevel@tonic-gate 	 */
16207c478bd9Sstevel@tonic-gate 	return (i);
16217c478bd9Sstevel@tonic-gate }
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate /*
16247c478bd9Sstevel@tonic-gate  * verify_tbl
16257c478bd9Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
16267c478bd9Sstevel@tonic-gate  * the disk.
16277c478bd9Sstevel@tonic-gate  */
1628bb16350dSlclee static int
1629bb16350dSlclee verify_tbl(void)
16307c478bd9Sstevel@tonic-gate {
16317c478bd9Sstevel@tonic-gate 	int	i, j, rsect, numsect;
16327c478bd9Sstevel@tonic-gate 	int	noMoreParts = 0;
16337c478bd9Sstevel@tonic-gate 	int	numParts = 0;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
1636bb16350dSlclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
16377c478bd9Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
16387c478bd9Sstevel@tonic-gate 			numParts++;
16397c478bd9Sstevel@tonic-gate 			/*
16407c478bd9Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
16417c478bd9Sstevel@tonic-gate 			 * EFI_PMBR part
16427c478bd9Sstevel@tonic-gate 			 */
16437c478bd9Sstevel@tonic-gate 			if (noMoreParts) {
16447c478bd9Sstevel@tonic-gate 				return (-1);
16457c478bd9Sstevel@tonic-gate 			}
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 			/*
16487c478bd9Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
16497c478bd9Sstevel@tonic-gate 			 * and must be Table entry 0
16507c478bd9Sstevel@tonic-gate 			 */
16517c478bd9Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
16527c478bd9Sstevel@tonic-gate 				if (i == 0) {
16537c478bd9Sstevel@tonic-gate 					noMoreParts = 1;
16547c478bd9Sstevel@tonic-gate 				} else {
16557c478bd9Sstevel@tonic-gate 					return (-1);
16567c478bd9Sstevel@tonic-gate 				}
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
1659bb16350dSlclee 					(void) fprintf(stderr, "ERROR: "
16607c478bd9Sstevel@tonic-gate 					    "Invalid starting sector "
16617c478bd9Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
16627c478bd9Sstevel@tonic-gate 					    "relsect %d "
16637c478bd9Sstevel@tonic-gate 					    "(should be 1)\n",
16647c478bd9Sstevel@tonic-gate 					    Table[i].relsect);
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 					return (-1);
16677c478bd9Sstevel@tonic-gate 				}
16687c478bd9Sstevel@tonic-gate 
1669*1237e847Slclee 				if (Table[i].numsect != dev_capacity - 1) {
1670bb16350dSlclee 					(void) fprintf(stderr, "ERROR: "
16717c478bd9Sstevel@tonic-gate 					    "EFI_PMBR partition must "
16727c478bd9Sstevel@tonic-gate 					    "encompass the entire "
1673bb16350dSlclee 					    "disk.\n numsect %d - "
1674*1237e847Slclee 					    "actual %llu\n",
16757c478bd9Sstevel@tonic-gate 					    Table[i].numsect,
1676*1237e847Slclee 					    dev_capacity - 1);
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 					return (-1);
16797c478bd9Sstevel@tonic-gate 				}
16807c478bd9Sstevel@tonic-gate 			}
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
16837c478bd9Sstevel@tonic-gate 			rsect = lel(Table[i].relsect);
16847c478bd9Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
1685*1237e847Slclee 			if ((rsect + numsect) > dev_capacity) {
16867c478bd9Sstevel@tonic-gate 				return (-1);
16877c478bd9Sstevel@tonic-gate 			}
16887c478bd9Sstevel@tonic-gate 
1689bb16350dSlclee 			for (j = i + 1; j < FD_NUMPART; j++) {
16907c478bd9Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
16917c478bd9Sstevel@tonic-gate 					int t_relsect = lel(Table[j].relsect);
16927c478bd9Sstevel@tonic-gate 					int t_numsect = lel(Table[j].numsect);
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate 					if (noMoreParts) {
1695bb16350dSlclee 						(void) fprintf(stderr,
16967c478bd9Sstevel@tonic-gate 						    "Cannot add partition to "
16977c478bd9Sstevel@tonic-gate 						    "table; no more partitions "
16987c478bd9Sstevel@tonic-gate 						    "allowed\n");
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 						if (io_debug) {
1701bb16350dSlclee 							(void) fprintf(stderr,
17027c478bd9Sstevel@tonic-gate 							    "DEBUG: Current "
17037c478bd9Sstevel@tonic-gate 							    "partition:\t"
17047c478bd9Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1705bb16350dSlclee 							    "%d:%d:%d:%d:%d\n"
17067c478bd9Sstevel@tonic-gate 							    "       Next "
17077c478bd9Sstevel@tonic-gate 							    "partition:\t\t"
17087c478bd9Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1709bb16350dSlclee 							    "%d:%d:%d:%d:%d\n",
17107c478bd9Sstevel@tonic-gate 							    Table[i].systid,
17117c478bd9Sstevel@tonic-gate 							    Table[i].bootid,
17127c478bd9Sstevel@tonic-gate 							    Table[i].begcyl,
17137c478bd9Sstevel@tonic-gate 							    Table[i].beghead,
17147c478bd9Sstevel@tonic-gate 							    Table[i].begsect,
17157c478bd9Sstevel@tonic-gate 							    Table[i].endcyl,
17167c478bd9Sstevel@tonic-gate 							    Table[i].endhead,
17177c478bd9Sstevel@tonic-gate 							    Table[i].endsect,
17187c478bd9Sstevel@tonic-gate 							    Table[i].relsect,
17197c478bd9Sstevel@tonic-gate 							    Table[i].numsect,
17207c478bd9Sstevel@tonic-gate 							    Table[j].systid,
17217c478bd9Sstevel@tonic-gate 							    Table[j].bootid,
17227c478bd9Sstevel@tonic-gate 							    Table[j].begcyl,
17237c478bd9Sstevel@tonic-gate 							    Table[j].beghead,
17247c478bd9Sstevel@tonic-gate 							    Table[j].begsect,
17257c478bd9Sstevel@tonic-gate 							    Table[j].endcyl,
17267c478bd9Sstevel@tonic-gate 							    Table[j].endhead,
17277c478bd9Sstevel@tonic-gate 							    Table[j].endsect,
17287c478bd9Sstevel@tonic-gate 							    Table[j].relsect,
17297c478bd9Sstevel@tonic-gate 							    Table[j].numsect);
17307c478bd9Sstevel@tonic-gate 						}
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 						return (-1);
17337c478bd9Sstevel@tonic-gate 					}
17347c478bd9Sstevel@tonic-gate 
17357c478bd9Sstevel@tonic-gate 					if ((rsect >=
17367c478bd9Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
1737bb16350dSlclee 					    ((rsect + numsect) <= t_relsect)) {
17387c478bd9Sstevel@tonic-gate 						continue;
17397c478bd9Sstevel@tonic-gate 					} else {
1740bb16350dSlclee 						(void) fprintf(stderr, "ERROR: "
17417c478bd9Sstevel@tonic-gate 						    "current partition overlaps"
17427c478bd9Sstevel@tonic-gate 						    " following partition\n");
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate 						return (-1);
17457c478bd9Sstevel@tonic-gate 					}
17467c478bd9Sstevel@tonic-gate 				}
17477c478bd9Sstevel@tonic-gate 			}
17487c478bd9Sstevel@tonic-gate 		} else {
17497c478bd9Sstevel@tonic-gate 			noMoreParts = 1;
17507c478bd9Sstevel@tonic-gate 		}
17517c478bd9Sstevel@tonic-gate 	}
17527c478bd9Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
17537c478bd9Sstevel@tonic-gate 		if (noMoreParts ||
17547c478bd9Sstevel@tonic-gate 		    ((lel(Table[i].relsect) + lel(Table[i].numsect)) >
1755*1237e847Slclee 		    dev_capacity)) {
17567c478bd9Sstevel@tonic-gate 			return (-1);
17577c478bd9Sstevel@tonic-gate 		}
17587c478bd9Sstevel@tonic-gate 	}
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate 	return (numParts);
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate /*
17647c478bd9Sstevel@tonic-gate  * pars_fdisk
17657c478bd9Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
17667c478bd9Sstevel@tonic-gate  * (-A, -D, -F).
17677c478bd9Sstevel@tonic-gate  */
1768bb16350dSlclee static int
1769bb16350dSlclee pars_fdisk(
1770bb16350dSlclee     char *line,
1771bb16350dSlclee     int *id, int *act,
1772bb16350dSlclee     int *bhead, int *bsect, int *bcyl,
1773bb16350dSlclee     int *ehead, int *esect, int *ecyl,
1774bb16350dSlclee     int *rsect, int *numsect)
17757c478bd9Sstevel@tonic-gate {
17767c478bd9Sstevel@tonic-gate 	int	i;
17777c478bd9Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
1778*1237e847Slclee 		return (1);
17797c478bd9Sstevel@tonic-gate 	line[strlen(line)] = '\0';
17807c478bd9Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
17817c478bd9Sstevel@tonic-gate 		if (line[i] == '\0') {
17827c478bd9Sstevel@tonic-gate 			break;
17837c478bd9Sstevel@tonic-gate 		} else if (line[i] == ':') {
17847c478bd9Sstevel@tonic-gate 			line[i] = ' ';
17857c478bd9Sstevel@tonic-gate 		}
17867c478bd9Sstevel@tonic-gate 	}
1787bb16350dSlclee 	if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d",
17887c478bd9Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
17897c478bd9Sstevel@tonic-gate 	    rsect, numsect) != 10) {
17907c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
17917c478bd9Sstevel@tonic-gate 		exit(1);
17927c478bd9Sstevel@tonic-gate 	}
17937c478bd9Sstevel@tonic-gate 	return (0);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate /*
17977c478bd9Sstevel@tonic-gate  * validate_part
17987c478bd9Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
17997c478bd9Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
18007c478bd9Sstevel@tonic-gate  */
1801bb16350dSlclee static int
1802bb16350dSlclee validate_part(int id, int rsect, int numsect)
18037c478bd9Sstevel@tonic-gate {
18047c478bd9Sstevel@tonic-gate 	int i;
18057c478bd9Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
18067c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
18077c478bd9Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
18087c478bd9Sstevel@tonic-gate 			    (Old_Table[i].relsect == lel(rsect)) &&
1809*1237e847Slclee 			    (Old_Table[i].numsect == lel(numsect)))
1810*1237e847Slclee 				return (0);
18117c478bd9Sstevel@tonic-gate 		}
1812bb16350dSlclee 		(void) fprintf(stderr,
1813bb16350dSlclee 		    "New partition cannot start at sector 0\n");
18147c478bd9Sstevel@tonic-gate 		return (-1);
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 	return (0);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate /*
18207c478bd9Sstevel@tonic-gate  * stage0
18217c478bd9Sstevel@tonic-gate  * Print out interactive menu and process user input.
18227c478bd9Sstevel@tonic-gate  */
1823bb16350dSlclee static void
1824bb16350dSlclee stage0(void)
18257c478bd9Sstevel@tonic-gate {
1826bb16350dSlclee 	dispmenu();
1827bb16350dSlclee 	for (;;) {
1828bb16350dSlclee 		(void) printf(Q_LINE);
1829bb16350dSlclee 		(void) printf("Enter Selection: ");
1830bb16350dSlclee 		(void) gets(s);
18317c478bd9Sstevel@tonic-gate 		rm_blanks(s);
18327c478bd9Sstevel@tonic-gate 		while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) {
1833bb16350dSlclee 			(void) printf(E_LINE); /* Clear any previous error */
1834bb16350dSlclee 			(void) printf(
1835bb16350dSlclee 			    "Enter a one-digit number between 1 and 6.");
1836bb16350dSlclee 			(void) printf(Q_LINE);
1837bb16350dSlclee 			(void) printf("Enter Selection: ");
1838bb16350dSlclee 			(void) gets(s);
18397c478bd9Sstevel@tonic-gate 			rm_blanks(s);
18407c478bd9Sstevel@tonic-gate 		}
1841bb16350dSlclee 		(void) printf(E_LINE);
18427c478bd9Sstevel@tonic-gate 		switch (s[0]) {
18437c478bd9Sstevel@tonic-gate 			case '1':
18447c478bd9Sstevel@tonic-gate 				if (pcreate() == -1)
18457c478bd9Sstevel@tonic-gate 					return;
18467c478bd9Sstevel@tonic-gate 				break;
18477c478bd9Sstevel@tonic-gate 			case '2':
18487c478bd9Sstevel@tonic-gate 				if (pchange() == -1)
18497c478bd9Sstevel@tonic-gate 					return;
18507c478bd9Sstevel@tonic-gate 				break;
18517c478bd9Sstevel@tonic-gate 			case '3':
18527c478bd9Sstevel@tonic-gate 				if (pdelete() == -1)
18537c478bd9Sstevel@tonic-gate 					return;
18547c478bd9Sstevel@tonic-gate 				break;
18557c478bd9Sstevel@tonic-gate 			case '4':
18567c478bd9Sstevel@tonic-gate 				if (ppartid() == -1)
18577c478bd9Sstevel@tonic-gate 					return;
18587c478bd9Sstevel@tonic-gate 				break;
18597c478bd9Sstevel@tonic-gate 			case '5':
18607c478bd9Sstevel@tonic-gate 				/* update disk partition table, if changed */
18617c478bd9Sstevel@tonic-gate 				if (TableChanged() == 1) {
18627c478bd9Sstevel@tonic-gate 					copy_Table_to_Bootblk();
18637c478bd9Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
18647c478bd9Sstevel@tonic-gate 				}
18657c478bd9Sstevel@tonic-gate 				/*
18667c478bd9Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18677c478bd9Sstevel@tonic-gate 				 * (truncate only)
18687c478bd9Sstevel@tonic-gate 				 */
18697c478bd9Sstevel@tonic-gate 				if (io_adjt) {
18707c478bd9Sstevel@tonic-gate 					fix_slice();
18717c478bd9Sstevel@tonic-gate 				}
1872bb16350dSlclee 				(void) close(Dev);
18737c478bd9Sstevel@tonic-gate 				exit(0);
1874bb16350dSlclee 				/* FALLTHRU */
18757c478bd9Sstevel@tonic-gate 			case '6':
18767c478bd9Sstevel@tonic-gate 				/*
18777c478bd9Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18787c478bd9Sstevel@tonic-gate 				 * (truncate only)
18797c478bd9Sstevel@tonic-gate 				 */
18807c478bd9Sstevel@tonic-gate 				if (io_adjt) {
18817c478bd9Sstevel@tonic-gate 					fix_slice();
18827c478bd9Sstevel@tonic-gate 				}
1883bb16350dSlclee 				(void) close(Dev);
18847c478bd9Sstevel@tonic-gate 				exit(0);
1885bb16350dSlclee 				/* FALLTHRU */
18867c478bd9Sstevel@tonic-gate 			default:
18877c478bd9Sstevel@tonic-gate 				break;
18887c478bd9Sstevel@tonic-gate 		}
18897c478bd9Sstevel@tonic-gate 		copy_Table_to_Bootblk();
18907c478bd9Sstevel@tonic-gate 		disptbl();
1891bb16350dSlclee 		dispmenu();
18927c478bd9Sstevel@tonic-gate 	}
18937c478bd9Sstevel@tonic-gate }
18947c478bd9Sstevel@tonic-gate 
18957c478bd9Sstevel@tonic-gate /*
18967c478bd9Sstevel@tonic-gate  * pcreate
18977c478bd9Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
18987c478bd9Sstevel@tonic-gate  */
1899bb16350dSlclee static int
1900bb16350dSlclee pcreate(void)
19017c478bd9Sstevel@tonic-gate {
1902bb16350dSlclee 	uchar_t tsystid = 'z';
19037c478bd9Sstevel@tonic-gate 	int i, j;
19047c478bd9Sstevel@tonic-gate 	int rsect = 1;
19057c478bd9Sstevel@tonic-gate 	int retCode = 0;
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	i = 0;
1908bb16350dSlclee 	for (;;) {
19097c478bd9Sstevel@tonic-gate 		if (i == FD_NUMPART) {
1910bb16350dSlclee 			(void) printf(E_LINE);
1911bb16350dSlclee 			(void) printf(
1912bb16350dSlclee 			    "The partition table is full!\n"
1913bb16350dSlclee 			    "You must delete a partition before creating"
19147c478bd9Sstevel@tonic-gate 			    " a new one.\n");
19157c478bd9Sstevel@tonic-gate 			return (-1);
19167c478bd9Sstevel@tonic-gate 		}
19177c478bd9Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
19187c478bd9Sstevel@tonic-gate 			break;
19197c478bd9Sstevel@tonic-gate 		}
19207c478bd9Sstevel@tonic-gate 		i++;
19217c478bd9Sstevel@tonic-gate 	}
19227c478bd9Sstevel@tonic-gate 
19237c478bd9Sstevel@tonic-gate 	j = 0;
19247c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
19257c478bd9Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
19267c478bd9Sstevel@tonic-gate 			j += lel(Table[i].numsect);
19277c478bd9Sstevel@tonic-gate 		}
1928*1237e847Slclee 		if (j >= chs_capacity) {
1929bb16350dSlclee 			(void) printf(E_LINE);
1930bb16350dSlclee 			(void) printf("There is no more room on the disk for"
19317c478bd9Sstevel@tonic-gate 			    " another partition.\n");
1932bb16350dSlclee 			(void) printf(
1933bb16350dSlclee 			    "You must delete a partition before creating"
19347c478bd9Sstevel@tonic-gate 			    " a new one.\n");
19357c478bd9Sstevel@tonic-gate 			return (-1);
19367c478bd9Sstevel@tonic-gate 		}
19377c478bd9Sstevel@tonic-gate 	}
19387c478bd9Sstevel@tonic-gate 	while (tsystid == 'z') {
1939bb16350dSlclee 		(void) printf(Q_LINE);
1940bb16350dSlclee 		(void) printf(
1941bb16350dSlclee 		    "Select the partition type to create:\n"
1942bb16350dSlclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
1943bb16350dSlclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
1944bb16350dSlclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
1945bb16350dSlclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
1946bb16350dSlclee 		(void) gets(s);
19477c478bd9Sstevel@tonic-gate 		rm_blanks(s);
19487c478bd9Sstevel@tonic-gate 		if (s[1] != 0) {
1949bb16350dSlclee 			(void) printf(E_LINE);
1950bb16350dSlclee 			(void) printf("Invalid selection, try again.");
19517c478bd9Sstevel@tonic-gate 			continue;
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 		switch (s[0]) {
19547c478bd9Sstevel@tonic-gate 		case '0':		/* exit */
1955*1237e847Slclee 			(void) printf(E_LINE);
1956*1237e847Slclee 			return (-1);
19577c478bd9Sstevel@tonic-gate 		case '1':		/* Solaris partition */
1958*1237e847Slclee 			tsystid = SUNIXOS2;
1959*1237e847Slclee 			break;
19607c478bd9Sstevel@tonic-gate 		case '2':		/* UNIX partition */
1961*1237e847Slclee 			tsystid = UNIXOS;
1962*1237e847Slclee 			break;
19637c478bd9Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
1964*1237e847Slclee 			tsystid = PCIXOS;
1965*1237e847Slclee 			break;
19667c478bd9Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
1967*1237e847Slclee 			tsystid = OTHEROS;
1968*1237e847Slclee 			break;
19697c478bd9Sstevel@tonic-gate 		case '5':
1970*1237e847Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
1971*1237e847Slclee 			break;
19727c478bd9Sstevel@tonic-gate 		case '6':
1973*1237e847Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
1974*1237e847Slclee 			break;
19757c478bd9Sstevel@tonic-gate 		case '7':
1976*1237e847Slclee 			tsystid = EXTDOS;
1977*1237e847Slclee 			break;
19787c478bd9Sstevel@tonic-gate 		case '8':
1979*1237e847Slclee 			tsystid = DOSHUGE;
1980*1237e847Slclee 			break;
19817c478bd9Sstevel@tonic-gate 		case '9':
1982*1237e847Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
1983*1237e847Slclee 			break;
19847c478bd9Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
19857c478bd9Sstevel@tonic-gate 		case 'A':
1986*1237e847Slclee 			tsystid = X86BOOT;
1987*1237e847Slclee 			break;
19887c478bd9Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
19897c478bd9Sstevel@tonic-gate 		case 'B':
1990*1237e847Slclee 			tsystid = DIAGPART;
1991*1237e847Slclee 			break;
19927c478bd9Sstevel@tonic-gate 		case 'c':		/* FAT32 */
19937c478bd9Sstevel@tonic-gate 		case 'C':
1994*1237e847Slclee 			tsystid = FDISK_WINDOWS;
1995*1237e847Slclee 			break;
19967c478bd9Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
19977c478bd9Sstevel@tonic-gate 		case 'D':
1998*1237e847Slclee 			tsystid = FDISK_EXT_WIN;
1999*1237e847Slclee 			break;
20007c478bd9Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
20017c478bd9Sstevel@tonic-gate 		case 'E':
2002*1237e847Slclee 			tsystid = FDISK_EXTLBA;
2003*1237e847Slclee 			break;
20047c478bd9Sstevel@tonic-gate 		case 'f':
20057c478bd9Sstevel@tonic-gate 		case 'F':
2006*1237e847Slclee 			tsystid = EFI_PMBR;
2007*1237e847Slclee 			break;
20087c478bd9Sstevel@tonic-gate 		default:
2009*1237e847Slclee 			(void) printf(E_LINE);
2010*1237e847Slclee 			(void) printf("Invalid selection, try again.");
2011*1237e847Slclee 			continue;
20127c478bd9Sstevel@tonic-gate 		}
20137c478bd9Sstevel@tonic-gate 	}
20147c478bd9Sstevel@tonic-gate 
2015bb16350dSlclee 	(void) printf(E_LINE);
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
20187c478bd9Sstevel@tonic-gate 		/* create the new partition */
20197c478bd9Sstevel@tonic-gate 		i = specify(tsystid);
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 		if (i != -1) {
20227c478bd9Sstevel@tonic-gate 			/* see if it should be the active partition */
2023bb16350dSlclee 			(void) printf(E_LINE);
2024bb16350dSlclee 			(void) printf(Q_LINE);
20257c478bd9Sstevel@tonic-gate 
2026bb16350dSlclee 			(void) printf(
2027bb16350dSlclee 			    "Should this become the active partition? If "
2028bb16350dSlclee 			    "yes, it  will be activated\n"
2029bb16350dSlclee 			    "each time the computer is reset or turned on.\n"
2030bb16350dSlclee 			    "Please type \"y\" or \"n\". ");
20317c478bd9Sstevel@tonic-gate 
20327c478bd9Sstevel@tonic-gate 			if (yesno()) {
2033bb16350dSlclee 				(void) printf(E_LINE);
20347c478bd9Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
20357c478bd9Sstevel@tonic-gate 					if (j == i) {
20367c478bd9Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2037bb16350dSlclee 						(void) printf(E_LINE);
2038bb16350dSlclee 						(void) printf(
2039bb16350dSlclee 						    "Partition %d is now "
20407c478bd9Sstevel@tonic-gate 						    "the active partition.",
2041bb16350dSlclee 						    j + 1);
20427c478bd9Sstevel@tonic-gate 					} else {
20437c478bd9Sstevel@tonic-gate 						Table[j].bootid = 0;
20447c478bd9Sstevel@tonic-gate 					}
20457c478bd9Sstevel@tonic-gate 				}
20467c478bd9Sstevel@tonic-gate 			} else {
20477c478bd9Sstevel@tonic-gate 				Table[i].bootid = 0;
20487c478bd9Sstevel@tonic-gate 			}
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 			/* set up the return code */
20517c478bd9Sstevel@tonic-gate 			i = 1;
20527c478bd9Sstevel@tonic-gate 		}
20537c478bd9Sstevel@tonic-gate 	} else {
20547c478bd9Sstevel@tonic-gate 		/*
20557c478bd9Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
20567c478bd9Sstevel@tonic-gate 		 * the table
20577c478bd9Sstevel@tonic-gate 		 *
20587c478bd9Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
20597c478bd9Sstevel@tonic-gate 		 * empty
20607c478bd9Sstevel@tonic-gate 		 */
20617c478bd9Sstevel@tonic-gate 		retCode = verify_tbl();
20627c478bd9Sstevel@tonic-gate 
20637c478bd9Sstevel@tonic-gate 		if (retCode < 0) {
2064bb16350dSlclee 			(void) fprintf(stderr,
20657c478bd9Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
20667c478bd9Sstevel@tonic-gate 			    "current partition table is invalid.\n");
20677c478bd9Sstevel@tonic-gate 			return (-1);
20687c478bd9Sstevel@tonic-gate 		} else if (retCode > 0) {
2069bb16350dSlclee 			(void) printf(
2070bb16350dSlclee 			    "An EFI partition must be the only partition on "
2071bb16350dSlclee 			    "disk.  You may manually delete existing\n"
2072bb16350dSlclee 			    "partitions, or fdisk can do it.\n"
2073bb16350dSlclee 			    "Do you want fdisk to destroy existing "
2074bb16350dSlclee 			    "partitions?\n"
2075bb16350dSlclee 			    "Please type \"y\" or \"n\". ");
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 			if (yesno()) {
20787c478bd9Sstevel@tonic-gate 				nulltbl();
20797c478bd9Sstevel@tonic-gate 			} else {
20807c478bd9Sstevel@tonic-gate 				return (-1);
20817c478bd9Sstevel@tonic-gate 			}
20827c478bd9Sstevel@tonic-gate 		}
20837c478bd9Sstevel@tonic-gate 
20847c478bd9Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
20857c478bd9Sstevel@tonic-gate 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, rsect,
2086*1237e847Slclee 		    dev_capacity - rsect);
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate 		if (i != 0) {
2089bb16350dSlclee 			(void) printf("Error creating EFI partition!!!\n");
20907c478bd9Sstevel@tonic-gate 			i = -1;
20917c478bd9Sstevel@tonic-gate 		} else {
20927c478bd9Sstevel@tonic-gate 
20937c478bd9Sstevel@tonic-gate 			/* EFI partitions are currently never active */
20947c478bd9Sstevel@tonic-gate 			Table[i].bootid = 0;
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 			/* set up the return code */
20977c478bd9Sstevel@tonic-gate 			i = 1;
20987c478bd9Sstevel@tonic-gate 		}
20997c478bd9Sstevel@tonic-gate 	}
21007c478bd9Sstevel@tonic-gate 
21017c478bd9Sstevel@tonic-gate 	return (i);
21027c478bd9Sstevel@tonic-gate }
21037c478bd9Sstevel@tonic-gate 
21047c478bd9Sstevel@tonic-gate /*
21057c478bd9Sstevel@tonic-gate  * specify
21067c478bd9Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
21077c478bd9Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
21087c478bd9Sstevel@tonic-gate  * cylinder and length in cylinders.
21097c478bd9Sstevel@tonic-gate  */
2110bb16350dSlclee static int
2111bb16350dSlclee specify(uchar_t tsystid)
21127c478bd9Sstevel@tonic-gate {
2113*1237e847Slclee 	int	i, j, percent = -1;
21147c478bd9Sstevel@tonic-gate 	int	cyl, cylen, first_free, size_free;
2115*1237e847Slclee 	int	max_free;
2116*1237e847Slclee 	int	cyl_size;
21177c478bd9Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
21187c478bd9Sstevel@tonic-gate 
2119*1237e847Slclee 	cyl_size = heads * sectors;
2120*1237e847Slclee 
2121*1237e847Slclee 	/*
2122*1237e847Slclee 	 * make a local copy of the partition table
2123*1237e847Slclee 	 * and sort it into relsect order
2124*1237e847Slclee 	 */
2125*1237e847Slclee 	for (i = 0; i < FD_NUMPART; i++)
2126*1237e847Slclee 		partition[i] = &Table[i];
2127*1237e847Slclee 
2128*1237e847Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
2129*1237e847Slclee 		if (partition[i]->systid == UNUSED)
2130*1237e847Slclee 			break;
2131*1237e847Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
2132*1237e847Slclee 			if (partition[j]->systid == UNUSED)
2133*1237e847Slclee 				break;
2134*1237e847Slclee 			if (lel(partition[j]->relsect) <
2135*1237e847Slclee 			    lel(partition[i]->relsect)) {
2136*1237e847Slclee 				struct ipart *temp = partition[i];
2137*1237e847Slclee 				partition[i] = partition[j];
2138*1237e847Slclee 				partition[j] = temp;
2139*1237e847Slclee 			}
2140*1237e847Slclee 		}
2141*1237e847Slclee 	}
2142*1237e847Slclee 
2143bb16350dSlclee 	(void) printf(Q_LINE);
2144bb16350dSlclee 	(void) printf(
2145bb16350dSlclee 	    "Specify the percentage of disk to use for this partition\n"
2146bb16350dSlclee 	    "(or type \"c\" to specify the size in cylinders). ");
2147bb16350dSlclee 	(void) gets(s);
21487c478bd9Sstevel@tonic-gate 	rm_blanks(s);
21497c478bd9Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
2150*1237e847Slclee 		i = 0;
2151*1237e847Slclee 		while (s[i] != '\0') {
2152*1237e847Slclee 			if (s[i] < '0' || s[i] > '9') {
2153*1237e847Slclee 				(void) printf(E_LINE);
2154*1237e847Slclee 				(void) printf("Invalid percentage value "
2155*1237e847Slclee 				    "specified; retry the operation.");
2156*1237e847Slclee 				return (-1);
2157*1237e847Slclee 			}
2158*1237e847Slclee 			i++;
2159*1237e847Slclee 			if (i > 3) {
2160*1237e847Slclee 				(void) printf(E_LINE);
2161*1237e847Slclee 				(void) printf("Invalid percentage value "
2162*1237e847Slclee 				    "specified; retry the operation.");
2163*1237e847Slclee 				return (-1);
2164*1237e847Slclee 			}
21657c478bd9Sstevel@tonic-gate 		}
2166*1237e847Slclee 		if ((percent = atoi(s)) > 100) {
2167*1237e847Slclee 			(void) printf(E_LINE);
2168*1237e847Slclee 			(void) printf(
2169*1237e847Slclee 			    "Percentage value is too large. The value must be"
2170*1237e847Slclee 			    " between 1 and 100;\nretry the operation.\n");
2171*1237e847Slclee 			return (-1);
2172*1237e847Slclee 		}
2173*1237e847Slclee 		if (percent < 1) {
2174*1237e847Slclee 			(void) printf(E_LINE);
2175*1237e847Slclee 			(void) printf(
2176*1237e847Slclee 			    "Percentage value is too small. The value must be"
2177*1237e847Slclee 			    " between 1 and 100;\nretry the operation.\n");
2178*1237e847Slclee 			return (-1);
2179*1237e847Slclee 		}
2180*1237e847Slclee 
2181*1237e847Slclee 
2182*1237e847Slclee 		if (percent == 100)
2183*1237e847Slclee 			cylen = Numcyl - 1;
2184*1237e847Slclee 		else
2185*1237e847Slclee 			cylen = (Numcyl * percent) / 100;
2186*1237e847Slclee 
2187*1237e847Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
2188*1237e847Slclee 		if ((tsystid == DOSOS12) &&
2189*1237e847Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
2190*1237e847Slclee 			int n;
2191*1237e847Slclee 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl;
2192*1237e847Slclee 			(void) printf(E_LINE);
2193*1237e847Slclee 			(void) printf("Maximum size for a DOS partition "
2194*1237e847Slclee 			    "is %d%%; retry the operation.",
2195*1237e847Slclee 			    n <= 100 ? n : 100);
2196*1237e847Slclee 			return (-1);
2197*1237e847Slclee 		}
2198*1237e847Slclee 
2199*1237e847Slclee 
2200*1237e847Slclee 		max_free = 0;
2201*1237e847Slclee 		for (i = 0; i < FD_NUMPART; i++) {
2202*1237e847Slclee 
2203*1237e847Slclee 			/*
2204*1237e847Slclee 			 * check for free space before partition i
2205*1237e847Slclee 			 * where i varies from 0 to 3
2206*1237e847Slclee 			 *
2207*1237e847Slclee 			 * freespace after partition 3 is unusable
2208*1237e847Slclee 			 * because there are no free partitions
2209*1237e847Slclee 			 *
2210*1237e847Slclee 			 * freespace begins at the end of previous partition
2211*1237e847Slclee 			 * or cylinder 1
2212*1237e847Slclee 			 */
2213*1237e847Slclee 			if (i) {
2214*1237e847Slclee 				/* Not an empty table */
2215*1237e847Slclee 				first_free = lel(partition[i - 1]->relsect) +
2216*1237e847Slclee 				    lel(partition[i - 1]->numsect);
2217*1237e847Slclee 			} else {
2218*1237e847Slclee 				first_free = cyl_size;
2219*1237e847Slclee 			}
2220*1237e847Slclee 
2221*1237e847Slclee 			/*
2222*1237e847Slclee 			 * freespace ends before the current partition
2223*1237e847Slclee 			 * or the end of the disk (chs end)
2224*1237e847Slclee 			 */
2225*1237e847Slclee 			if (partition[i]->systid == UNUSED) {
2226*1237e847Slclee 				size_free = chs_capacity - first_free;
2227*1237e847Slclee 			} else {
2228*1237e847Slclee 				size_free =
2229*1237e847Slclee 				    lel(partition[i]->relsect) - first_free;
2230*1237e847Slclee 			}
2231*1237e847Slclee 
2232*1237e847Slclee 			/* save largest free space */
2233*1237e847Slclee 			if (max_free < size_free)
2234*1237e847Slclee 				max_free = size_free;
2235*1237e847Slclee 
2236*1237e847Slclee 			if ((cylen * cyl_size) <= size_free) {
2237*1237e847Slclee 				/* We found a place to use */
2238*1237e847Slclee 				break;
2239*1237e847Slclee 			}
2240*1237e847Slclee 			if (partition[i]->systid == UNUSED) {
2241*1237e847Slclee 				(void) printf(E_LINE);
2242*1237e847Slclee 				max_free /= (cyl_size);
2243*1237e847Slclee 				(void) fprintf(stderr, "fdisk: "
2244*1237e847Slclee 				    "Maximum percentage available is %d\n",
2245*1237e847Slclee 				    100 * max_free / Numcyl);
2246*1237e847Slclee 				return (-1);
2247*1237e847Slclee 			}
2248*1237e847Slclee 		}
2249*1237e847Slclee 
2250bb16350dSlclee 		(void) printf(E_LINE);
2251*1237e847Slclee 		if (i >= FD_NUMPART) {
2252*1237e847Slclee 			(void) fprintf(stderr,
2253*1237e847Slclee 			    "fdisk: Partition table is full.\n");
2254*1237e847Slclee 			return (-1);
2255*1237e847Slclee 		}
2256*1237e847Slclee 
2257*1237e847Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
2258*1237e847Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
2259*1237e847Slclee 			return (i);
2260*1237e847Slclee 		}
22617c478bd9Sstevel@tonic-gate 		return (-1);
2262*1237e847Slclee 	} else {
22637c478bd9Sstevel@tonic-gate 
2264*1237e847Slclee 		/* Specifying size in cylinders */
2265bb16350dSlclee 		(void) printf(E_LINE);
2266*1237e847Slclee 		(void) printf(Q_LINE);
2267*1237e847Slclee 		(void) printf("Enter starting cylinder number: ");
2268*1237e847Slclee 		if ((cyl = getcyl()) == -1) {
2269*1237e847Slclee 			(void) printf(E_LINE);
2270*1237e847Slclee 			(void) printf("Invalid number; retry the operation.");
2271*1237e847Slclee 			return (-1);
2272*1237e847Slclee 		}
2273*1237e847Slclee 		if (cyl == 0) {
2274*1237e847Slclee 			(void) printf(E_LINE);
2275*1237e847Slclee 			(void) printf(
2276*1237e847Slclee 			    "New partition cannot start at cylinder 0.\n");
2277*1237e847Slclee 			return (-1);
2278*1237e847Slclee 		}
2279*1237e847Slclee 		if (cyl >= (unsigned int)Numcyl) {
2280*1237e847Slclee 			(void) printf(E_LINE);
2281*1237e847Slclee 			(void) printf(
2282*1237e847Slclee 			    "Cylinder %d is out of bounds, "
2283*1237e847Slclee 			    "the maximum is %d.\n",
2284*1237e847Slclee 			    cyl, Numcyl - 1);
2285*1237e847Slclee 			return (-1);
2286*1237e847Slclee 		}
2287*1237e847Slclee 		(void) printf(Q_LINE);
2288*1237e847Slclee 		(void) printf("Enter partition size in cylinders: ");
2289*1237e847Slclee 		if ((cylen = getcyl()) == -1) {
2290*1237e847Slclee 			(void) printf(E_LINE);
2291*1237e847Slclee 			(void) printf("Invalid number, retry the operation.");
2292*1237e847Slclee 			return (-1);
2293*1237e847Slclee 		}
2294*1237e847Slclee 
2295*1237e847Slclee 		for (i = 0; i < FD_NUMPART; i++) {
2296*1237e847Slclee 			uint32_t	t_relsect, t_numsect;
2297*1237e847Slclee 
2298*1237e847Slclee 			if (partition[i]->systid == UNUSED)
2299*1237e847Slclee 				break;
2300*1237e847Slclee 			t_relsect = lel(partition[i]->relsect);
2301*1237e847Slclee 			t_numsect = lel(partition[i]->numsect);
2302*1237e847Slclee 
2303*1237e847Slclee 			if (cyl * cyl_size >= t_relsect &&
2304*1237e847Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
2305*1237e847Slclee 				(void) printf(E_LINE);
2306*1237e847Slclee 				(void) printf(
2307*1237e847Slclee 				    "Cylinder %d is already allocated"
2308*1237e847Slclee 				    "\nretry the operation.",
2309*1237e847Slclee 				    cyl);
2310*1237e847Slclee 				return (-1);
2311*1237e847Slclee 			}
2312*1237e847Slclee 
2313*1237e847Slclee 			if (cyl * cyl_size < t_relsect &&
2314*1237e847Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
2315*1237e847Slclee 				(void) printf(E_LINE);
2316*1237e847Slclee 				(void) printf(
2317*1237e847Slclee 				    "Maximum size for partition is %u cylinders"
2318*1237e847Slclee 				    "\nretry the operation.",
2319*1237e847Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
2320*1237e847Slclee 				return (-1);
2321*1237e847Slclee 			}
2322*1237e847Slclee 		}
23237c478bd9Sstevel@tonic-gate 
2324*1237e847Slclee 		/* Verify partition doesn't exceed disk size */
2325*1237e847Slclee 		if (cyl + cylen > Numcyl) {
2326*1237e847Slclee 			(void) printf(E_LINE);
2327*1237e847Slclee 			(void) printf(
2328*1237e847Slclee 			    "Maximum size for partition is %d cylinders;"
2329*1237e847Slclee 			    "\nretry the operation.",
2330*1237e847Slclee 			    Numcyl - cyl);
2331*1237e847Slclee 			return (-1);
2332*1237e847Slclee 		}
2333*1237e847Slclee 
2334*1237e847Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
2335*1237e847Slclee 		if ((tsystid == DOSOS12) &&
2336*1237e847Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
2337*1237e847Slclee 			(void) printf(E_LINE);
2338*1237e847Slclee 			(void) printf(
2339*1237e847Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
2340*1237e847Slclee 			    "\nretry the operation.",
2341*1237e847Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
2342*1237e847Slclee 			return (-1);
2343*1237e847Slclee 		}
23447c478bd9Sstevel@tonic-gate 
2345bb16350dSlclee 		(void) printf(E_LINE);
2346*1237e847Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
2347*1237e847Slclee 		    cyl * cyl_size, cylen * cyl_size);
2348*1237e847Slclee 		if (i < 0)
2349*1237e847Slclee 			return (-1);
2350*1237e847Slclee 
2351*1237e847Slclee 		if (verify_tbl() < 0) {
2352*1237e847Slclee 			(void) printf(E_LINE);
2353*1237e847Slclee 			(void) printf("fdisk: Cannot create partition table\n");
2354*1237e847Slclee 			return (-1);
2355*1237e847Slclee 		}
23567c478bd9Sstevel@tonic-gate 
2357*1237e847Slclee 		return (i);
23587c478bd9Sstevel@tonic-gate 	}
23597c478bd9Sstevel@tonic-gate }
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate /*
23627c478bd9Sstevel@tonic-gate  * dispmenu
23637c478bd9Sstevel@tonic-gate  * Display command menu (interactive mode).
23647c478bd9Sstevel@tonic-gate  */
2365bb16350dSlclee static void
2366bb16350dSlclee dispmenu(void)
23677c478bd9Sstevel@tonic-gate {
2368bb16350dSlclee 	(void) printf(M_LINE);
2369bb16350dSlclee 	(void) printf(
2370bb16350dSlclee 	    "SELECT ONE OF THE FOLLOWING:\n"
2371bb16350dSlclee 	    "   1. Create a partition\n"
2372bb16350dSlclee 	    "   2. Specify the active partition\n"
2373bb16350dSlclee 	    "   3. Delete a partition\n"
2374bb16350dSlclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
2375bb16350dSlclee 	    "   5. Exit (update disk configuration and exit)\n"
2376bb16350dSlclee 	    "   6. Cancel (exit without updating disk configuration)\n");
23777c478bd9Sstevel@tonic-gate }
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate /*
23807c478bd9Sstevel@tonic-gate  * pchange
23817c478bd9Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
23827c478bd9Sstevel@tonic-gate  */
2383bb16350dSlclee static int
2384bb16350dSlclee pchange(void)
23857c478bd9Sstevel@tonic-gate {
23867c478bd9Sstevel@tonic-gate 	char s[80];
23877c478bd9Sstevel@tonic-gate 	int i, j;
23887c478bd9Sstevel@tonic-gate 
2389bb16350dSlclee 	for (;;) {
2390bb16350dSlclee 		(void) printf(Q_LINE);
23917c478bd9Sstevel@tonic-gate 			{
2392bb16350dSlclee 			(void) printf(
2393bb16350dSlclee 			    "Specify the partition number to boot from"
23947c478bd9Sstevel@tonic-gate 			    " (or specify 0 for none): ");
23957c478bd9Sstevel@tonic-gate 			}
2396bb16350dSlclee 		(void) gets(s);
23977c478bd9Sstevel@tonic-gate 		rm_blanks(s);
23987c478bd9Sstevel@tonic-gate 		if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) {
2399bb16350dSlclee 			(void) printf(E_LINE);
2400bb16350dSlclee 			(void) printf(
2401bb16350dSlclee 			    "Invalid response, please specify a number"
24027c478bd9Sstevel@tonic-gate 			    " between 0 and 4.\n");
24037c478bd9Sstevel@tonic-gate 		} else {
24047c478bd9Sstevel@tonic-gate 			break;
24057c478bd9Sstevel@tonic-gate 		}
24067c478bd9Sstevel@tonic-gate 	}
24077c478bd9Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
24087c478bd9Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
24097c478bd9Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
24107c478bd9Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
24117c478bd9Sstevel@tonic-gate 				Table[i].bootid = 0;
24127c478bd9Sstevel@tonic-gate 		}
2413bb16350dSlclee 		(void) printf(E_LINE);
2414bb16350dSlclee 			(void) printf(
2415bb16350dSlclee 			    "No partition is currently marked as active.");
24167c478bd9Sstevel@tonic-gate 		return (0);
24177c478bd9Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
24187c478bd9Sstevel@tonic-gate 		i = s[0] - '1';
24197c478bd9Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2420bb16350dSlclee 			(void) printf(E_LINE);
2421bb16350dSlclee 			(void) printf("Partition does not exist.");
24227c478bd9Sstevel@tonic-gate 			return (-1);
24237c478bd9Sstevel@tonic-gate 		}
24247c478bd9Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
24257c478bd9Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
24267c478bd9Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
24277c478bd9Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
2428bb16350dSlclee 			(void) printf(E_LINE);
2429bb16350dSlclee 			(void) printf(
2430bb16350dSlclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
24317c478bd9Sstevel@tonic-gate 			    "cannot be made active.\n");
2432bb16350dSlclee 			(void) printf("Select another partition.");
24337c478bd9Sstevel@tonic-gate 			return (-1);
24347c478bd9Sstevel@tonic-gate 		}
24357c478bd9Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
24367c478bd9Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
24377c478bd9Sstevel@tonic-gate 			if (j != i)
24387c478bd9Sstevel@tonic-gate 			Table[j].bootid = 0;
24397c478bd9Sstevel@tonic-gate 		}
24407c478bd9Sstevel@tonic-gate 	}
2441bb16350dSlclee 	(void) printf(E_LINE);
24427c478bd9Sstevel@tonic-gate 		{
2443bb16350dSlclee 		(void) printf(
2444bb16350dSlclee 		    "Partition %d is now active. The system will start up"
2445bb16350dSlclee 		    " from this\n", i + 1);
2446bb16350dSlclee 		(void) printf("partition after the next reboot.");
24477c478bd9Sstevel@tonic-gate 		}
24487c478bd9Sstevel@tonic-gate 	return (1);
24497c478bd9Sstevel@tonic-gate }
24507c478bd9Sstevel@tonic-gate 
24517c478bd9Sstevel@tonic-gate /*
24527c478bd9Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
24537c478bd9Sstevel@tonic-gate  */
2454bb16350dSlclee static int
2455bb16350dSlclee ppartid(void)
24567c478bd9Sstevel@tonic-gate {
24577c478bd9Sstevel@tonic-gate 	char	*p, s[80];
24587c478bd9Sstevel@tonic-gate 	int	i;
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	for (;;) {
2461bb16350dSlclee 		(void) printf(Q_LINE);
2462bb16350dSlclee 		(void) printf("Specify the partition number to change"
2463*1237e847Slclee 		    " (or enter 0 to exit): ");
2464bb16350dSlclee 		if (!fgets(s, sizeof (s), stdin))
2465bb16350dSlclee 			return (1);
24667c478bd9Sstevel@tonic-gate 		i = strtol(s, &p, 10);
24677c478bd9Sstevel@tonic-gate 
24687c478bd9Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
2469bb16350dSlclee 			(void) printf(E_LINE);
2470bb16350dSlclee 			(void) printf(
2471bb16350dSlclee 			    "Invalid response, retry the operation.\n");
24727c478bd9Sstevel@tonic-gate 			continue;
24737c478bd9Sstevel@tonic-gate 		}
24747c478bd9Sstevel@tonic-gate 
24757c478bd9Sstevel@tonic-gate 		if (i == 0) {
24767c478bd9Sstevel@tonic-gate 			/* exit delete command */
2477bb16350dSlclee 			(void) printf(E_LINE); /* clear error message */
24787c478bd9Sstevel@tonic-gate 			return (1);
24797c478bd9Sstevel@tonic-gate 		}
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 		i -= 1;
24827c478bd9Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
24837c478bd9Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
24847c478bd9Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
24857c478bd9Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
24867c478bd9Sstevel@tonic-gate 		} else {
2487bb16350dSlclee 			(void) printf(E_LINE);
2488bb16350dSlclee 			(void) printf(
2489bb16350dSlclee 			    "Partition %d is not a Solaris partition.",
24907c478bd9Sstevel@tonic-gate 			    i + 1);
24917c478bd9Sstevel@tonic-gate 			continue;
24927c478bd9Sstevel@tonic-gate 		}
24937c478bd9Sstevel@tonic-gate 
2494bb16350dSlclee 		(void) printf(E_LINE);
2495bb16350dSlclee 		(void) printf("Partition %d has been changed.", i + 1);
24967c478bd9Sstevel@tonic-gate 		return (1);
24977c478bd9Sstevel@tonic-gate 	}
24987c478bd9Sstevel@tonic-gate }
24997c478bd9Sstevel@tonic-gate 
25007c478bd9Sstevel@tonic-gate /*
25017c478bd9Sstevel@tonic-gate  * pdelete
25027c478bd9Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
25037c478bd9Sstevel@tonic-gate  */
2504bb16350dSlclee static char
2505bb16350dSlclee pdelete(void)
25067c478bd9Sstevel@tonic-gate {
25077c478bd9Sstevel@tonic-gate 	char s[80];
25087c478bd9Sstevel@tonic-gate 	int i, j;
25097c478bd9Sstevel@tonic-gate 	char pactive;
25107c478bd9Sstevel@tonic-gate 
2511bb16350dSlclee DEL1:	(void) printf(Q_LINE);
2512bb16350dSlclee 	(void) printf("Specify the partition number to delete"
25137c478bd9Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
2514bb16350dSlclee 	(void) gets(s);
25157c478bd9Sstevel@tonic-gate 	rm_blanks(s);
25167c478bd9Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
2517bb16350dSlclee 		(void) printf(E_LINE);	/* clear error message */
25187c478bd9Sstevel@tonic-gate 		return (1);
25197c478bd9Sstevel@tonic-gate 	}
25207c478bd9Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
25217c478bd9Sstevel@tonic-gate 	if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) {
2522bb16350dSlclee 		(void) printf(E_LINE);
2523bb16350dSlclee 		(void) printf("Invalid response, retry the operation.\n");
25247c478bd9Sstevel@tonic-gate 		goto DEL1;
25257c478bd9Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
25267c478bd9Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
25277c478bd9Sstevel@tonic-gate 	}
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
2530bb16350dSlclee 		(void) printf(E_LINE);
2531bb16350dSlclee 		(void) printf("Partition %d does not exist.", i + 1);
25327c478bd9Sstevel@tonic-gate 		return (-1);
25337c478bd9Sstevel@tonic-gate 	}
25347c478bd9Sstevel@tonic-gate 
2535bb16350dSlclee 	(void) printf(Q_LINE);
2536bb16350dSlclee 	(void) printf("Are you sure you want to delete partition %d?"
2537bb16350dSlclee 	    " This will make all files and \n", i + 1);
2538bb16350dSlclee 	(void) printf("programs in this partition inaccessible (type"
25397c478bd9Sstevel@tonic-gate 	    " \"y\" or \"n\"). ");
25407c478bd9Sstevel@tonic-gate 
2541bb16350dSlclee 	(void) printf(E_LINE);
25427c478bd9Sstevel@tonic-gate 	if (! yesno()) {
25437c478bd9Sstevel@tonic-gate 		return (1);
25447c478bd9Sstevel@tonic-gate 	}
25457c478bd9Sstevel@tonic-gate 
25467c478bd9Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
25477c478bd9Sstevel@tonic-gate 		pactive = 1;
25487c478bd9Sstevel@tonic-gate 	} else {
25497c478bd9Sstevel@tonic-gate 		pactive = 0;
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 
25527c478bd9Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
2553*1237e847Slclee 		Table[j] = Table[j + 1];
25547c478bd9Sstevel@tonic-gate 	}
25557c478bd9Sstevel@tonic-gate 
25567c478bd9Sstevel@tonic-gate 	Table[j].systid = UNUSED;
25577c478bd9Sstevel@tonic-gate 	Table[j].numsect = 0;
25587c478bd9Sstevel@tonic-gate 	Table[j].relsect = 0;
25597c478bd9Sstevel@tonic-gate 	Table[j].bootid = 0;
2560bb16350dSlclee 	(void) printf(E_LINE);
2561bb16350dSlclee 	(void) printf("Partition %d has been deleted.", i + 1);
25627c478bd9Sstevel@tonic-gate 
25637c478bd9Sstevel@tonic-gate 	if (pactive) {
2564*1237e847Slclee 		(void) printf(" This was the active partition.");
25657c478bd9Sstevel@tonic-gate 	}
25667c478bd9Sstevel@tonic-gate 
25677c478bd9Sstevel@tonic-gate 	return (1);
25687c478bd9Sstevel@tonic-gate }
25697c478bd9Sstevel@tonic-gate 
25707c478bd9Sstevel@tonic-gate /*
25717c478bd9Sstevel@tonic-gate  * rm_blanks
25727c478bd9Sstevel@tonic-gate  * Remove blanks from strings of user responses.
25737c478bd9Sstevel@tonic-gate  */
2574bb16350dSlclee static void
2575bb16350dSlclee rm_blanks(char *s)
25767c478bd9Sstevel@tonic-gate {
25777c478bd9Sstevel@tonic-gate 	register int i, j;
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
25807c478bd9Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
25817c478bd9Sstevel@tonic-gate 			continue;
25827c478bd9Sstevel@tonic-gate 		else
25837c478bd9Sstevel@tonic-gate 			/* Found first non-blank character of the string */
25847c478bd9Sstevel@tonic-gate 			break;
25857c478bd9Sstevel@tonic-gate 	}
25867c478bd9Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
25877c478bd9Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
25887c478bd9Sstevel@tonic-gate 			/* Reached end of string */
25897c478bd9Sstevel@tonic-gate 			return;
25907c478bd9Sstevel@tonic-gate 		}
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate }
25937c478bd9Sstevel@tonic-gate 
25947c478bd9Sstevel@tonic-gate /*
25957c478bd9Sstevel@tonic-gate  * getcyl
25967c478bd9Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
25977c478bd9Sstevel@tonic-gate  * string to a decimal value.
25987c478bd9Sstevel@tonic-gate  */
2599bb16350dSlclee static int
2600bb16350dSlclee getcyl(void)
26017c478bd9Sstevel@tonic-gate {
26027c478bd9Sstevel@tonic-gate int slen, i, j;
26037c478bd9Sstevel@tonic-gate unsigned int cyl;
2604bb16350dSlclee 	(void) gets(s);
26057c478bd9Sstevel@tonic-gate 	rm_blanks(s);
26067c478bd9Sstevel@tonic-gate 	slen = strlen(s);
26077c478bd9Sstevel@tonic-gate 	j = 1;
26087c478bd9Sstevel@tonic-gate 	cyl = 0;
2609bb16350dSlclee 	for (i = slen - 1; i >= 0; i--) {
26107c478bd9Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
26117c478bd9Sstevel@tonic-gate 			return (-1);
26127c478bd9Sstevel@tonic-gate 		}
2613bb16350dSlclee 		cyl += (j * (s[i] - '0'));
26147c478bd9Sstevel@tonic-gate 		j *= 10;
26157c478bd9Sstevel@tonic-gate 	}
26167c478bd9Sstevel@tonic-gate 	return (cyl);
26177c478bd9Sstevel@tonic-gate }
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate /*
26207c478bd9Sstevel@tonic-gate  * disptbl
26217c478bd9Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
26227c478bd9Sstevel@tonic-gate  * of the disk used for each partition.
26237c478bd9Sstevel@tonic-gate  */
2624bb16350dSlclee static void
2625bb16350dSlclee disptbl(void)
26267c478bd9Sstevel@tonic-gate {
26277c478bd9Sstevel@tonic-gate 	int i;
26287c478bd9Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
26297c478bd9Sstevel@tonic-gate 	char *stat, *type;
26307c478bd9Sstevel@tonic-gate 
26317c478bd9Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
2632bb16350dSlclee 		(void) printf("WARNING: critical disk geometry information"
2633*1237e847Slclee 		    " missing!\n");
2634bb16350dSlclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
26357c478bd9Sstevel@tonic-gate 		exit(1);
26367c478bd9Sstevel@tonic-gate 	}
26377c478bd9Sstevel@tonic-gate 
2638bb16350dSlclee 	(void) printf(HOME);
2639bb16350dSlclee 	(void) printf(T_LINE);
2640bb16350dSlclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
2641bb16350dSlclee 	(void) printf("             Cylinder size is %d (512 byte) blocks\n\n",
2642bb16350dSlclee 	    heads * sectors);
2643bb16350dSlclee 	(void) printf(
2644bb16350dSlclee 	    "                                               Cylinders\n");
2645bb16350dSlclee 	(void) printf(
2646bb16350dSlclee 	    "      Partition   Status    Type          Start   End   Length"
26477c478bd9Sstevel@tonic-gate 	    "    %%\n");
2648bb16350dSlclee 	(void) printf(
2649bb16350dSlclee 	    "      =========   ======    ============  =====   ===   ======"
26507c478bd9Sstevel@tonic-gate 	    "   ===");
26517c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
26527c478bd9Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2653bb16350dSlclee 			(void) printf("\n");
2654bb16350dSlclee 			(void) printf(CLR_LIN);
26557c478bd9Sstevel@tonic-gate 			continue;
26567c478bd9Sstevel@tonic-gate 		}
26577c478bd9Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
2658*1237e847Slclee 			stat = Actvstr;
26597c478bd9Sstevel@tonic-gate 		else
2660*1237e847Slclee 			stat = NAstr;
26617c478bd9Sstevel@tonic-gate 		switch (Table[i].systid) {
26627c478bd9Sstevel@tonic-gate 		case UNIXOS:
2663*1237e847Slclee 			type = Ustr;
2664*1237e847Slclee 			break;
26657c478bd9Sstevel@tonic-gate 		case SUNIXOS:
2666*1237e847Slclee 			type = SUstr;
2667*1237e847Slclee 			break;
26687c478bd9Sstevel@tonic-gate 		case SUNIXOS2:
2669*1237e847Slclee 			type = SU2str;
2670*1237e847Slclee 			break;
26717c478bd9Sstevel@tonic-gate 		case X86BOOT:
2672*1237e847Slclee 			type = X86str;
2673*1237e847Slclee 			break;
26747c478bd9Sstevel@tonic-gate 		case DOSOS12:
2675*1237e847Slclee 			type = Dstr;
2676*1237e847Slclee 			break;
26777c478bd9Sstevel@tonic-gate 		case DOSOS16:
2678*1237e847Slclee 			type = D16str;
2679*1237e847Slclee 			break;
26807c478bd9Sstevel@tonic-gate 		case EXTDOS:
2681*1237e847Slclee 			type = EDstr;
2682*1237e847Slclee 			break;
26837c478bd9Sstevel@tonic-gate 		case DOSDATA:
2684*1237e847Slclee 			type = DDstr;
2685*1237e847Slclee 			break;
26867c478bd9Sstevel@tonic-gate 		case DOSHUGE:
2687*1237e847Slclee 			type = DBstr;
2688*1237e847Slclee 			break;
26897c478bd9Sstevel@tonic-gate 		case PCIXOS:
2690*1237e847Slclee 			type = PCstr;
2691*1237e847Slclee 			break;
26927c478bd9Sstevel@tonic-gate 		case DIAGPART:
2693*1237e847Slclee 			type = DIAGstr;
2694*1237e847Slclee 			break;
26957c478bd9Sstevel@tonic-gate 		case FDISK_IFS:
2696*1237e847Slclee 			type = IFSstr;
2697*1237e847Slclee 			break;
26987c478bd9Sstevel@tonic-gate 		case FDISK_AIXBOOT:
2699*1237e847Slclee 			type = AIXstr;
2700*1237e847Slclee 			break;
27017c478bd9Sstevel@tonic-gate 		case FDISK_AIXDATA:
2702*1237e847Slclee 			type = AIXDstr;
2703*1237e847Slclee 			break;
27047c478bd9Sstevel@tonic-gate 		case FDISK_OS2BOOT:
2705*1237e847Slclee 			type = OS2str;
2706*1237e847Slclee 			break;
27077c478bd9Sstevel@tonic-gate 		case FDISK_WINDOWS:
2708*1237e847Slclee 			type = WINstr;
2709*1237e847Slclee 			break;
27107c478bd9Sstevel@tonic-gate 		case FDISK_EXT_WIN:
2711*1237e847Slclee 			type = EWINstr;
2712*1237e847Slclee 			break;
27137c478bd9Sstevel@tonic-gate 		case FDISK_FAT95:
2714*1237e847Slclee 			type = FAT95str;
2715*1237e847Slclee 			break;
27167c478bd9Sstevel@tonic-gate 		case FDISK_EXTLBA:
2717*1237e847Slclee 			type = EXTLstr;
2718*1237e847Slclee 			break;
27197c478bd9Sstevel@tonic-gate 		case FDISK_LINUX:
2720*1237e847Slclee 			type = LINUXstr;
2721*1237e847Slclee 			break;
27227c478bd9Sstevel@tonic-gate 		case FDISK_CPM:
2723*1237e847Slclee 			type = CPMstr;
2724*1237e847Slclee 			break;
27257c478bd9Sstevel@tonic-gate 		case FDISK_NOVELL3:
2726*1237e847Slclee 			type = NOVstr;
2727*1237e847Slclee 			break;
27287c478bd9Sstevel@tonic-gate 		case FDISK_QNX4:
2729*1237e847Slclee 			type = QNXstr;
2730*1237e847Slclee 			break;
27317c478bd9Sstevel@tonic-gate 		case FDISK_QNX42:
2732*1237e847Slclee 			type = QNX2str;
2733*1237e847Slclee 			break;
27347c478bd9Sstevel@tonic-gate 		case FDISK_QNX43:
2735*1237e847Slclee 			type = QNX3str;
2736*1237e847Slclee 			break;
27377c478bd9Sstevel@tonic-gate 		case FDISK_LINUXNAT:
2738*1237e847Slclee 			type = LINNATstr;
2739*1237e847Slclee 			break;
27407c478bd9Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
2741*1237e847Slclee 			type = NTFSVOL1str;
2742*1237e847Slclee 			break;
27437c478bd9Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
2744*1237e847Slclee 			type = NTFSVOL2str;
2745*1237e847Slclee 			break;
27467c478bd9Sstevel@tonic-gate 		case FDISK_BSD:
2747*1237e847Slclee 			type = BSDstr;
2748*1237e847Slclee 			break;
27497c478bd9Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
2750*1237e847Slclee 			type = NEXTSTEPstr;
2751*1237e847Slclee 			break;
27527c478bd9Sstevel@tonic-gate 		case FDISK_BSDIFS:
2753*1237e847Slclee 			type = BSDIFSstr;
2754*1237e847Slclee 			break;
27557c478bd9Sstevel@tonic-gate 		case FDISK_BSDISWAP:
2756*1237e847Slclee 			type = BSDISWAPstr;
2757*1237e847Slclee 			break;
27587c478bd9Sstevel@tonic-gate 		case EFI_PMBR:
2759*1237e847Slclee 			type = EFIstr;
2760*1237e847Slclee 			break;
27617c478bd9Sstevel@tonic-gate 		default:
2762*1237e847Slclee 			type = Ostr;
2763*1237e847Slclee 			break;
27647c478bd9Sstevel@tonic-gate 		}
2765bb16350dSlclee 		startcyl = lel(Table[i].relsect) / (heads * sectors);
27667c478bd9Sstevel@tonic-gate 		length = lel(Table[i].numsect) / (long)(heads * sectors);
27677c478bd9Sstevel@tonic-gate 		if (lel(Table[i].numsect) % (long)(heads * sectors))
27687c478bd9Sstevel@tonic-gate 			length++;
27697c478bd9Sstevel@tonic-gate 		endcyl = startcyl + length - 1;
27707c478bd9Sstevel@tonic-gate 		percent = length * 100 / Numcyl;
2771bb16350dSlclee 		if ((remainder = (length * 100 % Numcyl)) != 0) {
27727c478bd9Sstevel@tonic-gate 			if ((remainder * 100 / Numcyl) > 50) {
27737c478bd9Sstevel@tonic-gate 				/* round up */
27747c478bd9Sstevel@tonic-gate 				percent++;
27757c478bd9Sstevel@tonic-gate 			}
27767c478bd9Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
27777c478bd9Sstevel@tonic-gate 			/* rounded down */
27787c478bd9Sstevel@tonic-gate 		}
27797c478bd9Sstevel@tonic-gate 		if (percent > 100)
27807c478bd9Sstevel@tonic-gate 			percent = 100;
2781bb16350dSlclee 		(void) printf(
2782bb16350dSlclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
2783bb16350dSlclee 		    "    %3d",
2784bb16350dSlclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
27857c478bd9Sstevel@tonic-gate 	}
27867c478bd9Sstevel@tonic-gate 	/* Print warning message if table is empty */
27877c478bd9Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
2788bb16350dSlclee 		(void) printf(W_LINE);
2789bb16350dSlclee 		(void) printf("WARNING: no partitions are defined!");
27907c478bd9Sstevel@tonic-gate 	} else {
27917c478bd9Sstevel@tonic-gate 		/* Clear the warning line */
2792bb16350dSlclee 		(void) printf(W_LINE);
27937c478bd9Sstevel@tonic-gate 	}
27947c478bd9Sstevel@tonic-gate }
27957c478bd9Sstevel@tonic-gate 
27967c478bd9Sstevel@tonic-gate /*
27977c478bd9Sstevel@tonic-gate  * print_Table
27987c478bd9Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
27997c478bd9Sstevel@tonic-gate  * the selected disk device.
28007c478bd9Sstevel@tonic-gate  */
2801bb16350dSlclee static void
2802bb16350dSlclee print_Table(void)
2803bb16350dSlclee {
28047c478bd9Sstevel@tonic-gate 	int i;
28057c478bd9Sstevel@tonic-gate 
2806bb16350dSlclee 	(void) fprintf(stderr,
28077c478bd9Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
28087c478bd9Sstevel@tonic-gate 	    "   NUMSECT\n");
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2811bb16350dSlclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
2812bb16350dSlclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
2813bb16350dSlclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
2814bb16350dSlclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
2815*1237e847Slclee 		(void) fprintf(stderr, "%-8d ",
2816*1237e847Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
28177c478bd9Sstevel@tonic-gate 
2818bb16350dSlclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
2819bb16350dSlclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
2820*1237e847Slclee 		(void) fprintf(stderr, "%-8d ",
2821*1237e847Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
2822bb16350dSlclee 		(void) fprintf(stderr, "%-9d ", lel(Table[i].relsect));
2823bb16350dSlclee 		(void) fprintf(stderr, "%-9d\n", lel(Table[i].numsect));
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 	}
28267c478bd9Sstevel@tonic-gate }
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate /*
28297c478bd9Sstevel@tonic-gate  * copy_Table_to_Old_Table
28307c478bd9Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
28317c478bd9Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
28327c478bd9Sstevel@tonic-gate  * ones compared when determining if Table has changed.
28337c478bd9Sstevel@tonic-gate  */
2834bb16350dSlclee static void
2835bb16350dSlclee copy_Table_to_Old_Table(void)
28367c478bd9Sstevel@tonic-gate {
28377c478bd9Sstevel@tonic-gate 	int i;
28387c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
2839*1237e847Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
28407c478bd9Sstevel@tonic-gate 	}
28417c478bd9Sstevel@tonic-gate }
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate /*
28447c478bd9Sstevel@tonic-gate  * nulltbl
28457c478bd9Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
28467c478bd9Sstevel@tonic-gate  * fdisk table.
28477c478bd9Sstevel@tonic-gate  */
2848bb16350dSlclee static void
2849bb16350dSlclee nulltbl(void)
28507c478bd9Sstevel@tonic-gate {
28517c478bd9Sstevel@tonic-gate 	int i;
28527c478bd9Sstevel@tonic-gate 
28537c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
2854*1237e847Slclee 		Table[i].systid = UNUSED;
2855*1237e847Slclee 		Table[i].numsect = lel(UNUSED);
2856*1237e847Slclee 		Table[i].relsect = lel(UNUSED);
2857*1237e847Slclee 		Table[i].bootid = 0;
28587c478bd9Sstevel@tonic-gate 	}
28597c478bd9Sstevel@tonic-gate }
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate /*
28627c478bd9Sstevel@tonic-gate  * copy_Bootblk_to_Table
28637c478bd9Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
28647c478bd9Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
28657c478bd9Sstevel@tonic-gate  */
2866bb16350dSlclee static void
2867bb16350dSlclee copy_Bootblk_to_Table(void)
28687c478bd9Sstevel@tonic-gate {
28697c478bd9Sstevel@tonic-gate 	int i, j;
28707c478bd9Sstevel@tonic-gate 	char *bootptr;
28717c478bd9Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
2874bb16350dSlclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
28757c478bd9Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
28767c478bd9Sstevel@tonic-gate 	if (les(Bootblk->signature) != MBB_MAGIC)  {
28777c478bd9Sstevel@tonic-gate 		/* Signature is missing */
28787c478bd9Sstevel@tonic-gate 		nulltbl();
2879bb16350dSlclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
28807c478bd9Sstevel@tonic-gate 		return;
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate 	/*
28837c478bd9Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
28847c478bd9Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
28857c478bd9Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
28867c478bd9Sstevel@tonic-gate 	 * those that are valid.
28877c478bd9Sstevel@tonic-gate 	 */
28887c478bd9Sstevel@tonic-gate 	j = 0;
28897c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28907c478bd9Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
28917c478bd9Sstevel@tonic-gate 			/* Null entry */
28927c478bd9Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
28937c478bd9Sstevel@tonic-gate 		} else {
2894bb16350dSlclee 			fill_ipart(bootptr, &Table[j]);
28957c478bd9Sstevel@tonic-gate 			j++;
28967c478bd9Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
28977c478bd9Sstevel@tonic-gate 		}
28987c478bd9Sstevel@tonic-gate 	}
28997c478bd9Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
29007c478bd9Sstevel@tonic-gate 		Table[i].systid = UNUSED;
29017c478bd9Sstevel@tonic-gate 		Table[i].numsect = lel(UNUSED);
29027c478bd9Sstevel@tonic-gate 		Table[i].relsect = lel(UNUSED);
29037c478bd9Sstevel@tonic-gate 		Table[i].bootid = 0;
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	}
29067c478bd9Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
2907bb16350dSlclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
29087c478bd9Sstevel@tonic-gate 	copy_Table_to_Bootblk();
29097c478bd9Sstevel@tonic-gate }
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate /*
29127c478bd9Sstevel@tonic-gate  * fill_ipart
29137c478bd9Sstevel@tonic-gate  * Initialize ipart structure values.
29147c478bd9Sstevel@tonic-gate  */
2915bb16350dSlclee static void
29167c478bd9Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
29177c478bd9Sstevel@tonic-gate {
29187c478bd9Sstevel@tonic-gate #ifdef sparc
29197c478bd9Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
2920bb16350dSlclee 	partp->bootid	= getbyte(&bootptr);
2921bb16350dSlclee 	partp->beghead	= getbyte(&bootptr);
2922bb16350dSlclee 	partp->begsect	= getbyte(&bootptr);
2923bb16350dSlclee 	partp->begcyl	= getbyte(&bootptr);
2924bb16350dSlclee 	partp->systid	= getbyte(&bootptr);
2925bb16350dSlclee 	partp->endhead	= getbyte(&bootptr);
2926bb16350dSlclee 	partp->endsect	= getbyte(&bootptr);
2927bb16350dSlclee 	partp->endcyl	= getbyte(&bootptr);
2928bb16350dSlclee 	partp->relsect	= (int32_t)getlong(&bootptr);
2929bb16350dSlclee 	partp->numsect	= (int32_t)getlong(&bootptr);
29307c478bd9Sstevel@tonic-gate #else
29317c478bd9Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
29327c478bd9Sstevel@tonic-gate #endif
29337c478bd9Sstevel@tonic-gate }
29347c478bd9Sstevel@tonic-gate 
29357c478bd9Sstevel@tonic-gate /*
2936bb16350dSlclee  * getbyte, getlong
29377c478bd9Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
29387c478bd9Sstevel@tonic-gate  */
29397c478bd9Sstevel@tonic-gate #ifdef sparc
2940bb16350dSlclee uchar_t
2941bb16350dSlclee getbyte(char **bp)
29427c478bd9Sstevel@tonic-gate {
2943bb16350dSlclee 	uchar_t	b;
29447c478bd9Sstevel@tonic-gate 
2945bb16350dSlclee 	b = (uchar_t)**bp;
29467c478bd9Sstevel@tonic-gate 	*bp = *bp + 1;
29477c478bd9Sstevel@tonic-gate 	return (b);
29487c478bd9Sstevel@tonic-gate }
29497c478bd9Sstevel@tonic-gate 
2950bb16350dSlclee uint32_t
2951bb16350dSlclee getlong(char **bp)
29527c478bd9Sstevel@tonic-gate {
2953bb16350dSlclee 	int32_t	b, bh, bl;
29547c478bd9Sstevel@tonic-gate 
29557c478bd9Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
29567c478bd9Sstevel@tonic-gate 	*bp += 2;
29577c478bd9Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
29587c478bd9Sstevel@tonic-gate 	*bp += 2;
29597c478bd9Sstevel@tonic-gate 
29607c478bd9Sstevel@tonic-gate 	b = (bh << 16) | bl;
2961bb16350dSlclee 	return ((uint32_t)b);
29627c478bd9Sstevel@tonic-gate }
29637c478bd9Sstevel@tonic-gate #endif
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate /*
29667c478bd9Sstevel@tonic-gate  * copy_Table_to_Bootblk
29677c478bd9Sstevel@tonic-gate  * Copy the table into the 512 boot record. Note that the unused
29687c478bd9Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
29697c478bd9Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
29707c478bd9Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
29717c478bd9Sstevel@tonic-gate  */
2972bb16350dSlclee static void
2973bb16350dSlclee copy_Table_to_Bootblk(void)
29747c478bd9Sstevel@tonic-gate {
29757c478bd9Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
29787c478bd9Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
29797c478bd9Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
29807c478bd9Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
2981*1237e847Slclee 		if (tbl_ptr->systid == UNUSED)
2982*1237e847Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
2983*1237e847Slclee 		else
2984*1237e847Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
29857c478bd9Sstevel@tonic-gate 	}
29867c478bd9Sstevel@tonic-gate 	Bootblk->signature = les(MBB_MAGIC);
29877c478bd9Sstevel@tonic-gate }
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate /*
29907c478bd9Sstevel@tonic-gate  * TableChanged
29917c478bd9Sstevel@tonic-gate  * 	Check for any changes in the partition table.
29927c478bd9Sstevel@tonic-gate  */
2993bb16350dSlclee static int
2994bb16350dSlclee TableChanged(void)
29957c478bd9Sstevel@tonic-gate {
29967c478bd9Sstevel@tonic-gate 	int i, changed;
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	changed = 0;
29997c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3000*1237e847Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
3001*1237e847Slclee 			/* Partition table changed, write back to disk */
3002*1237e847Slclee 			changed = 1;
3003*1237e847Slclee 		}
30047c478bd9Sstevel@tonic-gate 	}
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	return (changed);
30077c478bd9Sstevel@tonic-gate }
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate /*
30107c478bd9Sstevel@tonic-gate  * ffile_write
30117c478bd9Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
30127c478bd9Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
30137c478bd9Sstevel@tonic-gate  */
3014bb16350dSlclee static void
3015bb16350dSlclee ffile_write(char *file)
30167c478bd9Sstevel@tonic-gate {
30177c478bd9Sstevel@tonic-gate 	register int	i;
30187c478bd9Sstevel@tonic-gate 	FILE *fp;
30197c478bd9Sstevel@tonic-gate 
30207c478bd9Sstevel@tonic-gate 	/*
30217c478bd9Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
30227c478bd9Sstevel@tonic-gate 	 * Open file and write it.
30237c478bd9Sstevel@tonic-gate 	 */
30247c478bd9Sstevel@tonic-gate 	if (file != (char *)stdout) {
3025*1237e847Slclee 		if ((fp = fopen(file, "w")) == NULL) {
3026*1237e847Slclee 			(void) fprintf(stderr,
3027*1237e847Slclee 			    "fdisk: Cannot open output file %s.\n",
3028*1237e847Slclee 			    file);
3029*1237e847Slclee 			exit(1);
3030*1237e847Slclee 		}
30317c478bd9Sstevel@tonic-gate 	}
30327c478bd9Sstevel@tonic-gate 	else
3033*1237e847Slclee 		fp = stdout;
30347c478bd9Sstevel@tonic-gate 
30357c478bd9Sstevel@tonic-gate 	/*
30367c478bd9Sstevel@tonic-gate 	 * Write the fdisk table information
30377c478bd9Sstevel@tonic-gate 	 */
3038bb16350dSlclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
3039bb16350dSlclee 	(void) fprintf(fp, "* Dimensions:\n");
3040bb16350dSlclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3041bb16350dSlclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
3042bb16350dSlclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
3043bb16350dSlclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
3044bb16350dSlclee 	(void) fprintf(fp, "*\n");
30457c478bd9Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
30467c478bd9Sstevel@tonic-gate 	if (v_flag) {
3047bb16350dSlclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
3048bb16350dSlclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3049bb16350dSlclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
3050bb16350dSlclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
3051bb16350dSlclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
3052bb16350dSlclee 		(void) fprintf(fp, "*\n");
3053bb16350dSlclee 	}
3054bb16350dSlclee 	(void) fprintf(fp, "* systid:\n");
3055bb16350dSlclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
3056bb16350dSlclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
3057bb16350dSlclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
3058bb16350dSlclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
3059bb16350dSlclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
3060bb16350dSlclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
3061bb16350dSlclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
3062bb16350dSlclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
3063bb16350dSlclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
3064bb16350dSlclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
3065bb16350dSlclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
3066bb16350dSlclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
3067bb16350dSlclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
3068bb16350dSlclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
3069bb16350dSlclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
3070bb16350dSlclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
3071bb16350dSlclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
3072bb16350dSlclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
3073bb16350dSlclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
3074bb16350dSlclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
3075bb16350dSlclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
3076bb16350dSlclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
3077bb16350dSlclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
3078bb16350dSlclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
3079bb16350dSlclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
3080bb16350dSlclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
3081bb16350dSlclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
3082bb16350dSlclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
3083bb16350dSlclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
3084bb16350dSlclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
3085bb16350dSlclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
3086bb16350dSlclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
3087bb16350dSlclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
3088bb16350dSlclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
3089bb16350dSlclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
3090bb16350dSlclee 	(void) fprintf(fp, "*\n");
3091bb16350dSlclee 	(void) fprintf(fp,
30927c478bd9Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
30937c478bd9Sstevel@tonic-gate 	    "    Rsect    Numsect\n");
30947c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
30957c478bd9Sstevel@tonic-gate 		if (Table[i].systid != UNUSED)
3096bb16350dSlclee 			(void) fprintf(fp,
30977c478bd9Sstevel@tonic-gate 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-8d"
30987c478bd9Sstevel@tonic-gate 			    " %-8d\n",
30997c478bd9Sstevel@tonic-gate 			    Table[i].systid,
31007c478bd9Sstevel@tonic-gate 			    Table[i].bootid,
31017c478bd9Sstevel@tonic-gate 			    Table[i].beghead,
31027c478bd9Sstevel@tonic-gate 			    Table[i].begsect & 0x3f,
31037c478bd9Sstevel@tonic-gate 			    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
3104*1237e847Slclee 			    0xc0) << 2)),
31057c478bd9Sstevel@tonic-gate 			    Table[i].endhead,
31067c478bd9Sstevel@tonic-gate 			    Table[i].endsect & 0x3f,
31077c478bd9Sstevel@tonic-gate 			    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
3108*1237e847Slclee 			    0xc0) << 2)),
31097c478bd9Sstevel@tonic-gate 			    lel(Table[i].relsect),
31107c478bd9Sstevel@tonic-gate 			    lel(Table[i].numsect));
31117c478bd9Sstevel@tonic-gate 	}
31127c478bd9Sstevel@tonic-gate 	if (fp != stdout)
3113bb16350dSlclee 		(void) fclose(fp);
31147c478bd9Sstevel@tonic-gate }
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate /*
31177c478bd9Sstevel@tonic-gate  * fix_slice
31187c478bd9Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
31197c478bd9Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
31207c478bd9Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
31217c478bd9Sstevel@tonic-gate  */
3122bb16350dSlclee static void
3123bb16350dSlclee fix_slice(void)
31247c478bd9Sstevel@tonic-gate {
31257c478bd9Sstevel@tonic-gate 	int	i;
31267c478bd9Sstevel@tonic-gate 	int	numsect;
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	if (io_image) {
3129bb16350dSlclee 		return;
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate 
31327c478bd9Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31337c478bd9Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
31347c478bd9Sstevel@tonic-gate 			/*
31357c478bd9Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
31367c478bd9Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
31377c478bd9Sstevel@tonic-gate 			 * the partition.
31387c478bd9Sstevel@tonic-gate 			 */
31397c478bd9Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
31407c478bd9Sstevel@tonic-gate 			break;
31417c478bd9Sstevel@tonic-gate 		}
31427c478bd9Sstevel@tonic-gate 	}
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
31457c478bd9Sstevel@tonic-gate 		if (!io_nifdisk) {
31467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
31477c478bd9Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
31487c478bd9Sstevel@tonic-gate 			    " checked.\n");
31497c478bd9Sstevel@tonic-gate 		}
3150bb16350dSlclee 		return;
31517c478bd9Sstevel@tonic-gate 	}
31527c478bd9Sstevel@tonic-gate 
3153bb16350dSlclee 	if (readvtoc() != VTOC_OK) {
31547c478bd9Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
3155*1237e847Slclee 	}
3156*1237e847Slclee 	for (i = 0; i < V_NUMPAR; i++) {
3157*1237e847Slclee 		/* Special case for slice two (entire disk) */
3158*1237e847Slclee 		if (i == 2) {
3159*1237e847Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
3160*1237e847Slclee 				(void) fprintf(stderr,
3161*1237e847Slclee 				    "slice %d starts at %ld, is not at"
3162*1237e847Slclee 				    " start of partition",
3163*1237e847Slclee 				    i, disk_vtoc.v_part[i].p_start);
3164*1237e847Slclee 				if (!io_nifdisk) {
3165*1237e847Slclee 					(void) printf(" adjust ?:");
3166*1237e847Slclee 					if (yesno())
31677c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
3168*1237e847Slclee 				} else {
3169*1237e847Slclee 					disk_vtoc.v_part[i].p_start = 0;
3170*1237e847Slclee 					(void) fprintf(stderr, " adjusted!\n");
31717c478bd9Sstevel@tonic-gate 				}
3172*1237e847Slclee 
3173*1237e847Slclee 			}
3174*1237e847Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
3175*1237e847Slclee 				(void) fprintf(stderr,
3176*1237e847Slclee 				    "slice %d size %ld does not cover"
3177*1237e847Slclee 				    " complete partition",
3178*1237e847Slclee 				    i, disk_vtoc.v_part[i].p_size);
3179*1237e847Slclee 				if (!io_nifdisk) {
3180*1237e847Slclee 					(void) printf(" adjust ?:");
3181*1237e847Slclee 					if (yesno())
31827c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
31837c478bd9Sstevel@tonic-gate 						    numsect;
3184*1237e847Slclee 				} else {
3185*1237e847Slclee 					disk_vtoc.v_part[i].p_size = numsect;
3186*1237e847Slclee 					(void) fprintf(stderr, " adjusted!\n");
31877c478bd9Sstevel@tonic-gate 				}
3188*1237e847Slclee 			}
3189*1237e847Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
3190*1237e847Slclee 				(void) fprintf(stderr,
3191*1237e847Slclee 				    "slice %d tag was %d should be %d",
3192*1237e847Slclee 				    i, disk_vtoc.v_part[i].p_tag,
3193*1237e847Slclee 				    V_BACKUP);
3194*1237e847Slclee 				if (!io_nifdisk) {
3195bb16350dSlclee 					(void) printf(" fix ?:");
3196*1237e847Slclee 					if (yesno())
31977c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
31987c478bd9Sstevel@tonic-gate 						    V_BACKUP;
3199*1237e847Slclee 				} else {
32007c478bd9Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
32017c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
32027c478bd9Sstevel@tonic-gate 				}
3203*1237e847Slclee 			}
3204*1237e847Slclee 			continue;
3205*1237e847Slclee 		}
3206*1237e847Slclee 		if (io_ADJT) {
3207*1237e847Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
3208*1237e847Slclee 			    disk_vtoc.v_part[i].p_start +
3209*1237e847Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
3210*1237e847Slclee 				(void) fprintf(stderr,
3211*1237e847Slclee 				    "slice %d (start %ld, end %ld)"
3212*1237e847Slclee 				    " is larger than the partition",
3213*1237e847Slclee 				    i, disk_vtoc.v_part[i].p_start,
3214*1237e847Slclee 				    disk_vtoc.v_part[i].p_start +
3215*1237e847Slclee 				    disk_vtoc.v_part[i].p_size);
3216*1237e847Slclee 				if (!io_nifdisk) {
3217*1237e847Slclee 					(void) printf(" remove ?:");
3218*1237e847Slclee 					if (yesno()) {
32197c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
32207c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
32217c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
32227c478bd9Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
32237c478bd9Sstevel@tonic-gate 					}
32247c478bd9Sstevel@tonic-gate 				} else {
3225*1237e847Slclee 					disk_vtoc.v_part[i].p_size = 0;
3226*1237e847Slclee 					disk_vtoc.v_part[i].p_start = 0;
3227*1237e847Slclee 					disk_vtoc.v_part[i].p_tag = 0;
3228*1237e847Slclee 					disk_vtoc.v_part[i].p_flag = 0;
32297c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr,
3230*1237e847Slclee 					    " removed!\n");
32317c478bd9Sstevel@tonic-gate 				}
32327c478bd9Sstevel@tonic-gate 			}
3233*1237e847Slclee 			continue;
3234*1237e847Slclee 		}
3235*1237e847Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
3236*1237e847Slclee 			(void) fprintf(stderr,
3237*1237e847Slclee 			    "slice %d (start %ld) is larger than the partition",
3238*1237e847Slclee 			    i, disk_vtoc.v_part[i].p_start);
3239*1237e847Slclee 			if (!io_nifdisk) {
3240*1237e847Slclee 				(void) printf(" remove ?:");
3241*1237e847Slclee 				if (yesno()) {
3242*1237e847Slclee 					disk_vtoc.v_part[i].p_size = 0;
3243*1237e847Slclee 					disk_vtoc.v_part[i].p_start = 0;
3244*1237e847Slclee 					disk_vtoc.v_part[i].p_tag = 0;
3245*1237e847Slclee 					disk_vtoc.v_part[i].p_flag = 0;
3246*1237e847Slclee 				}
3247*1237e847Slclee 			} else {
3248*1237e847Slclee 				disk_vtoc.v_part[i].p_size = 0;
3249*1237e847Slclee 				disk_vtoc.v_part[i].p_start = 0;
3250*1237e847Slclee 				disk_vtoc.v_part[i].p_tag = 0;
3251*1237e847Slclee 				disk_vtoc.v_part[i].p_flag = 0;
3252*1237e847Slclee 				(void) fprintf(stderr,
3253*1237e847Slclee 				" removed!\n");
3254*1237e847Slclee 			}
3255*1237e847Slclee 		} else if (disk_vtoc.v_part[i].p_start
3256*1237e847Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
3257*1237e847Slclee 			(void) fprintf(stderr,
3258*1237e847Slclee 			    "slice %d (end %ld) is larger"
3259*1237e847Slclee 			    " than the partition",
3260*1237e847Slclee 			    i,
3261*1237e847Slclee 			    disk_vtoc.v_part[i].p_start +
3262*1237e847Slclee 			    disk_vtoc.v_part[i].p_size);
3263*1237e847Slclee 			if (!io_nifdisk) {
3264*1237e847Slclee 				(void) printf(" adjust ?:");
3265*1237e847Slclee 				if (yesno()) {
3266*1237e847Slclee 					disk_vtoc.v_part[i].p_size = numsect;
3267*1237e847Slclee 				}
3268*1237e847Slclee 			} else {
3269*1237e847Slclee 				disk_vtoc.v_part[i].p_size = numsect;
3270*1237e847Slclee 				(void) fprintf(stderr, " adjusted!\n");
3271*1237e847Slclee 			}
32727c478bd9Sstevel@tonic-gate 		}
32737c478bd9Sstevel@tonic-gate 	}
32747c478bd9Sstevel@tonic-gate #if 1		/* bh for now */
32757c478bd9Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
32767c478bd9Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
32777c478bd9Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
32787c478bd9Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
32797c478bd9Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
32807c478bd9Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
32817c478bd9Sstevel@tonic-gate #endif
32827c478bd9Sstevel@tonic-gate 
32837c478bd9Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
32847c478bd9Sstevel@tonic-gate 	if (!io_readonly)
3285bb16350dSlclee 		(void) writevtoc();
32867c478bd9Sstevel@tonic-gate }
32877c478bd9Sstevel@tonic-gate 
32887c478bd9Sstevel@tonic-gate /*
32897c478bd9Sstevel@tonic-gate  * yesno
32907c478bd9Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
32917c478bd9Sstevel@tonic-gate  */
32927c478bd9Sstevel@tonic-gate 
3293bb16350dSlclee static int
3294bb16350dSlclee yesno(void)
32957c478bd9Sstevel@tonic-gate {
32967c478bd9Sstevel@tonic-gate 	char	s[80];
32977c478bd9Sstevel@tonic-gate 
32987c478bd9Sstevel@tonic-gate 	for (;;) {
3299bb16350dSlclee 		(void) gets(s);
33007c478bd9Sstevel@tonic-gate 		rm_blanks(s);
33017c478bd9Sstevel@tonic-gate 		if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) {
3302bb16350dSlclee 			(void) printf(E_LINE);
3303bb16350dSlclee 			(void) printf("Please answer with \"y\" or \"n\": ");
33047c478bd9Sstevel@tonic-gate 			continue;
33057c478bd9Sstevel@tonic-gate 		}
33067c478bd9Sstevel@tonic-gate 		if (s[0] == 'y')
33077c478bd9Sstevel@tonic-gate 			return (1);
33087c478bd9Sstevel@tonic-gate 		else
33097c478bd9Sstevel@tonic-gate 			return (0);
33107c478bd9Sstevel@tonic-gate 	}
33117c478bd9Sstevel@tonic-gate }
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate /*
33147c478bd9Sstevel@tonic-gate  * readvtoc
33157c478bd9Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
33167c478bd9Sstevel@tonic-gate  */
3317bb16350dSlclee static int
3318bb16350dSlclee readvtoc(void)
33197c478bd9Sstevel@tonic-gate {
33207c478bd9Sstevel@tonic-gate 	int	i;
33217c478bd9Sstevel@tonic-gate 	int	retval = VTOC_OK;
33227c478bd9Sstevel@tonic-gate 
33237c478bd9Sstevel@tonic-gate 	if ((i = read_vtoc(Dev, &disk_vtoc)) < VTOC_OK) {
33247c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33257c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
33267c478bd9Sstevel@tonic-gate 			vt_inval++;
33277c478bd9Sstevel@tonic-gate 			retval = VTOC_INVAL;
33287c478bd9Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33297c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
3330*1237e847Slclee 			    "GPT\n");
33317c478bd9Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33327c478bd9Sstevel@tonic-gate 		} else {
33337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
33347c478bd9Sstevel@tonic-gate 			retval = VTOC_RWERR;
33357c478bd9Sstevel@tonic-gate 		}
33367c478bd9Sstevel@tonic-gate 	}
33377c478bd9Sstevel@tonic-gate 	return (retval);
33387c478bd9Sstevel@tonic-gate }
33397c478bd9Sstevel@tonic-gate 
33407c478bd9Sstevel@tonic-gate /*
33417c478bd9Sstevel@tonic-gate  * writevtoc
33427c478bd9Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
33437c478bd9Sstevel@tonic-gate  */
3344bb16350dSlclee static int
3345bb16350dSlclee writevtoc(void)
33467c478bd9Sstevel@tonic-gate {
33477c478bd9Sstevel@tonic-gate 	int	i;
33487c478bd9Sstevel@tonic-gate 	int	retval = 0;
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	if ((i = write_vtoc(Dev, &disk_vtoc)) != 0) {
33517c478bd9Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
33537c478bd9Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
33547c478bd9Sstevel@tonic-gate 			retval = VTOC_INVAL;
33557c478bd9Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33567c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
3357*1237e847Slclee 			    "GPT\n");
33587c478bd9Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33597c478bd9Sstevel@tonic-gate 		} else {
33607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
33617c478bd9Sstevel@tonic-gate 			retval = VTOC_RWERR;
33627c478bd9Sstevel@tonic-gate 		}
33637c478bd9Sstevel@tonic-gate 	}
33647c478bd9Sstevel@tonic-gate 	return (retval);
33657c478bd9Sstevel@tonic-gate }
33667c478bd9Sstevel@tonic-gate 
33677c478bd9Sstevel@tonic-gate /*
33687c478bd9Sstevel@tonic-gate  * efi_ioctl
33697c478bd9Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
33707c478bd9Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
33717c478bd9Sstevel@tonic-gate  */
33727c478bd9Sstevel@tonic-gate static int
33737c478bd9Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
33747c478bd9Sstevel@tonic-gate {
33757c478bd9Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
33767c478bd9Sstevel@tonic-gate 	int error;
33777c478bd9Sstevel@tonic-gate 
33787c478bd9Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
33797c478bd9Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 	return (error);
33827c478bd9Sstevel@tonic-gate }
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate /*
33857c478bd9Sstevel@tonic-gate  * clear_efi
33867c478bd9Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
33877c478bd9Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
33887c478bd9Sstevel@tonic-gate  */
3389bb16350dSlclee static int
3390bb16350dSlclee clear_efi(void)
33917c478bd9Sstevel@tonic-gate {
33927c478bd9Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
33937c478bd9Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 	/*
33967c478bd9Sstevel@tonic-gate 	 * see if we can read the EFI label
33977c478bd9Sstevel@tonic-gate 	 */
33987c478bd9Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
33997c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
34007c478bd9Sstevel@tonic-gate 	}
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate 	/*
34037c478bd9Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
34047c478bd9Sstevel@tonic-gate 	 */
34057c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
34067c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
34077c478bd9Sstevel@tonic-gate 
34087c478bd9Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
34097c478bd9Sstevel@tonic-gate 		return (VT_ERROR);
34107c478bd9Sstevel@tonic-gate 	}
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 	/*
34137c478bd9Sstevel@tonic-gate 	 * clear the primary label
34147c478bd9Sstevel@tonic-gate 	 */
34157c478bd9Sstevel@tonic-gate 	if (io_debug) {
3416bb16350dSlclee 		(void) fprintf(stderr,
3417bb16350dSlclee 		    "\tClearing primary EFI label at block %lld\n",
3418bb16350dSlclee 		    dk_ioc.dki_lba);
34197c478bd9Sstevel@tonic-gate 	}
34207c478bd9Sstevel@tonic-gate 
34217c478bd9Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34227c478bd9Sstevel@tonic-gate 		free(dk_ioc.dki_data);
34237c478bd9Sstevel@tonic-gate 		switch (errno) {
34247c478bd9Sstevel@tonic-gate 			case EIO:
34257c478bd9Sstevel@tonic-gate 				return (VT_EIO);
34267c478bd9Sstevel@tonic-gate 			case EINVAL:
34277c478bd9Sstevel@tonic-gate 				return (VT_EINVAL);
34287c478bd9Sstevel@tonic-gate 			default:
34297c478bd9Sstevel@tonic-gate 				return (VT_ERROR);
34307c478bd9Sstevel@tonic-gate 		}
34317c478bd9Sstevel@tonic-gate 	}
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	/*
34347c478bd9Sstevel@tonic-gate 	 * clear the backup partition table
34357c478bd9Sstevel@tonic-gate 	 */
34367c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
34377c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
34387c478bd9Sstevel@tonic-gate 	dk_ioc.dki_data++;
34397c478bd9Sstevel@tonic-gate 	if (io_debug) {
3440bb16350dSlclee 		(void) fprintf(stderr,
3441bb16350dSlclee 		    "\tClearing backup partition table at block %lld\n",
3442bb16350dSlclee 		    dk_ioc.dki_lba);
34437c478bd9Sstevel@tonic-gate 	}
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
3447*1237e847Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
3448*1237e847Slclee 		    errno);
34497c478bd9Sstevel@tonic-gate 	}
34507c478bd9Sstevel@tonic-gate 
34517c478bd9Sstevel@tonic-gate 	/*
34527c478bd9Sstevel@tonic-gate 	 * clear the backup label
34537c478bd9Sstevel@tonic-gate 	 */
34547c478bd9Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
34557c478bd9Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
34567c478bd9Sstevel@tonic-gate 	dk_ioc.dki_data--;
34577c478bd9Sstevel@tonic-gate 	if (io_debug) {
3458bb16350dSlclee 		(void) fprintf(stderr, "\tClearing backup label at block "
3459bb16350dSlclee 		    "%lld\n", dk_ioc.dki_lba);
34607c478bd9Sstevel@tonic-gate 	}
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
3463bb16350dSlclee 		(void) fprintf(stderr,
3464bb16350dSlclee 		    "\tUnable to clear backup EFI label at "
3465bb16350dSlclee 		    "block %llu; errno %d\n",
3466bb16350dSlclee 		    efi_vtoc->efi_last_lba,
3467bb16350dSlclee 		    errno);
34687c478bd9Sstevel@tonic-gate 	}
34697c478bd9Sstevel@tonic-gate 
34707c478bd9Sstevel@tonic-gate 	free(dk_ioc.dki_data);
34717c478bd9Sstevel@tonic-gate 	efi_free(efi_vtoc);
34727c478bd9Sstevel@tonic-gate 
34737c478bd9Sstevel@tonic-gate 	return (0);
34747c478bd9Sstevel@tonic-gate }
34757c478bd9Sstevel@tonic-gate 
34767c478bd9Sstevel@tonic-gate /*
34777c478bd9Sstevel@tonic-gate  * clear_vtoc
34787c478bd9Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
34797c478bd9Sstevel@tonic-gate  *      device.
34807c478bd9Sstevel@tonic-gate  */
3481bb16350dSlclee static void
34827c478bd9Sstevel@tonic-gate clear_vtoc(int table, int part)
34837c478bd9Sstevel@tonic-gate {
34847c478bd9Sstevel@tonic-gate 	struct ipart *clr_table;
34857c478bd9Sstevel@tonic-gate 	struct dk_label disk_label;
34867c478bd9Sstevel@tonic-gate 	int pcyl, ncyl, backup_block, solaris_offset, count, bytes, seek_byte;
34877c478bd9Sstevel@tonic-gate 
34887c478bd9Sstevel@tonic-gate #ifdef DEBUG
34897c478bd9Sstevel@tonic-gate 	struct dk_label	read_label;
34907c478bd9Sstevel@tonic-gate #endif /* DEBUG */
34917c478bd9Sstevel@tonic-gate 
34927c478bd9Sstevel@tonic-gate 	if (table == OLD) {
34937c478bd9Sstevel@tonic-gate 		clr_table = &Old_Table[part];
34947c478bd9Sstevel@tonic-gate 	} else {
34957c478bd9Sstevel@tonic-gate 		clr_table = &Table[part];
34967c478bd9Sstevel@tonic-gate 	}
34977c478bd9Sstevel@tonic-gate 
3498bb16350dSlclee 	(void) memset(&disk_label, 0, sizeof (struct dk_label));
34997c478bd9Sstevel@tonic-gate 
35007c478bd9Sstevel@tonic-gate 	seek_byte = (lel(clr_table->relsect) * sectsiz) + VTOC_OFFSET;
35017c478bd9Sstevel@tonic-gate 
35027c478bd9Sstevel@tonic-gate 	if (io_debug) {
3503bb16350dSlclee 		(void) fprintf(stderr, "\tClearing primary VTOC at byte %d\n",
35047c478bd9Sstevel@tonic-gate 		    seek_byte);
35057c478bd9Sstevel@tonic-gate 	}
35067c478bd9Sstevel@tonic-gate 
35077c478bd9Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3508bb16350dSlclee 		(void) fprintf(stderr,
3509bb16350dSlclee 		    "\tError seeking to primary label at byte %d\n",
35107c478bd9Sstevel@tonic-gate 		    seek_byte);
3511bb16350dSlclee 		return;
35127c478bd9Sstevel@tonic-gate 	}
35137c478bd9Sstevel@tonic-gate 
35147c478bd9Sstevel@tonic-gate 	bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35157c478bd9Sstevel@tonic-gate 
35167c478bd9Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3517bb16350dSlclee 		(void) fprintf(stderr,
3518bb16350dSlclee 		    "\tWarning: only %d bytes written to clear primary VTOC!\n",
3519bb16350dSlclee 		    bytes);
35207c478bd9Sstevel@tonic-gate 	}
35217c478bd9Sstevel@tonic-gate 
35227c478bd9Sstevel@tonic-gate #ifdef DEBUG
35237c478bd9Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3524bb16350dSlclee 		(void) fprintf(stderr,
3525bb16350dSlclee 		    "DEBUG: Error seeking to primary label at byte %d\n",
3526bb16350dSlclee 		    seek_byte);
3527bb16350dSlclee 		return;
35287c478bd9Sstevel@tonic-gate 	} else {
3529bb16350dSlclee 		(void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n",
35307c478bd9Sstevel@tonic-gate 		    seek_byte);
35317c478bd9Sstevel@tonic-gate 	}
35327c478bd9Sstevel@tonic-gate 
35337c478bd9Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
35347c478bd9Sstevel@tonic-gate 
35357c478bd9Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3536bb16350dSlclee 		(void) fprintf(stderr,
3537bb16350dSlclee 		    "DEBUG: Warning: only %d bytes read of label\n",
35387c478bd9Sstevel@tonic-gate 		    bytes);
35397c478bd9Sstevel@tonic-gate 	}
35407c478bd9Sstevel@tonic-gate 
35417c478bd9Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3542bb16350dSlclee 		(void) fprintf(stderr,
3543bb16350dSlclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
35447c478bd9Sstevel@tonic-gate 	} else {
3545bb16350dSlclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
35467c478bd9Sstevel@tonic-gate 		    "read_label\n");
35477c478bd9Sstevel@tonic-gate 	}
35487c478bd9Sstevel@tonic-gate #endif /* DEBUG */
35497c478bd9Sstevel@tonic-gate 
35507c478bd9Sstevel@tonic-gate 	/* Clear backup label */
35517c478bd9Sstevel@tonic-gate 	pcyl = lel(clr_table->numsect) / (heads * sectors);
35527c478bd9Sstevel@tonic-gate 	solaris_offset = lel(clr_table->relsect);
35537c478bd9Sstevel@tonic-gate 	ncyl = pcyl - acyl;
35547c478bd9Sstevel@tonic-gate 
35557c478bd9Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
35567c478bd9Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
35577c478bd9Sstevel@tonic-gate 
35587c478bd9Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
35597c478bd9Sstevel@tonic-gate 		seek_byte = (solaris_offset + backup_block) * 512;
35607c478bd9Sstevel@tonic-gate 
35617c478bd9Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3562bb16350dSlclee 			(void) fprintf(stderr,
35637c478bd9Sstevel@tonic-gate 			    "\tError seeking to backup label at byte %d on "
35647c478bd9Sstevel@tonic-gate 			    "%s.\n", seek_byte, Dfltdev);
3565bb16350dSlclee 			return;
35667c478bd9Sstevel@tonic-gate 		}
35677c478bd9Sstevel@tonic-gate 
35687c478bd9Sstevel@tonic-gate 		if (io_debug) {
3569bb16350dSlclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
35707c478bd9Sstevel@tonic-gate 			    " byte %d (block %d)\n",
35717c478bd9Sstevel@tonic-gate 			    (solaris_offset + backup_block) * 512,
35727c478bd9Sstevel@tonic-gate 			    (solaris_offset + backup_block));
35737c478bd9Sstevel@tonic-gate 		}
35747c478bd9Sstevel@tonic-gate 
35757c478bd9Sstevel@tonic-gate 		bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35767c478bd9Sstevel@tonic-gate 
35777c478bd9Sstevel@tonic-gate 		if (bytes != sizeof (struct dk_label)) {
3578bb16350dSlclee 			(void) fprintf(stderr,
3579bb16350dSlclee 			    "\t\tWarning: only %d bytes written to "
35807c478bd9Sstevel@tonic-gate 			    "clear backup VTOC at block %d!\n", bytes,
35817c478bd9Sstevel@tonic-gate 			    (solaris_offset + backup_block));
35827c478bd9Sstevel@tonic-gate 		}
35837c478bd9Sstevel@tonic-gate 
35847c478bd9Sstevel@tonic-gate #ifdef DEBUG
35857c478bd9Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3586bb16350dSlclee 		(void) fprintf(stderr,
3587bb16350dSlclee 		    "DEBUG: Error seeking to backup label at byte %d\n",
3588bb16350dSlclee 		    seek_byte);
3589bb16350dSlclee 		return;
35907c478bd9Sstevel@tonic-gate 	} else {
3591bb16350dSlclee 		(void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n",
35927c478bd9Sstevel@tonic-gate 		    seek_byte);
35937c478bd9Sstevel@tonic-gate 	}
35947c478bd9Sstevel@tonic-gate 
35957c478bd9Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
35967c478bd9Sstevel@tonic-gate 
35977c478bd9Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3598bb16350dSlclee 		(void) fprintf(stderr,
3599bb16350dSlclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
3600bb16350dSlclee 		    bytes);
36017c478bd9Sstevel@tonic-gate 	}
36027c478bd9Sstevel@tonic-gate 
36037c478bd9Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3604bb16350dSlclee 		(void) fprintf(stderr,
3605bb16350dSlclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
36067c478bd9Sstevel@tonic-gate 	} else {
3607bb16350dSlclee 		(void) fprintf(stderr,
3608bb16350dSlclee 		    "DEBUG: Good compare of disk_label and backup "
36097c478bd9Sstevel@tonic-gate 		    "read_label\n");
36107c478bd9Sstevel@tonic-gate 	}
36117c478bd9Sstevel@tonic-gate #endif /* DEBUG */
36127c478bd9Sstevel@tonic-gate 
36137c478bd9Sstevel@tonic-gate 		backup_block += 2;
36147c478bd9Sstevel@tonic-gate 	}
36157c478bd9Sstevel@tonic-gate }
36167c478bd9Sstevel@tonic-gate 
36177c478bd9Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
36187c478bd9Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
36197c478bd9Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
36207c478bd9Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
36217c478bd9Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
36227c478bd9Sstevel@tonic-gate 
36237c478bd9Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
36247c478bd9Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
36257c478bd9Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
36267c478bd9Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
36277c478bd9Sstevel@tonic-gate 
36287c478bd9Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
36297c478bd9Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
36307c478bd9Sstevel@tonic-gate 
36317c478bd9Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
36327c478bd9Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
36337c478bd9Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36347c478bd9Sstevel@tonic-gate 
36357c478bd9Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
36367c478bd9Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
36377c478bd9Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
36407c478bd9Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
36417c478bd9Sstevel@tonic-gate 
36427c478bd9Sstevel@tonic-gate /*
36437c478bd9Sstevel@tonic-gate  *  lecture_and_query
36447c478bd9Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
36457c478bd9Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
36467c478bd9Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
36477c478bd9Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
36487c478bd9Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
36497c478bd9Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
36507c478bd9Sstevel@tonic-gate  */
3651bb16350dSlclee static int
36527c478bd9Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
36537c478bd9Sstevel@tonic-gate {
36547c478bd9Sstevel@tonic-gate 	if (io_nifdisk)
36557c478bd9Sstevel@tonic-gate 		return (0);
36567c478bd9Sstevel@tonic-gate 
3657bb16350dSlclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
3658bb16350dSlclee 	(void) fprintf(stderr, "%s", warning);
3659bb16350dSlclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
3660bb16350dSlclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
36617c478bd9Sstevel@tonic-gate 
36627c478bd9Sstevel@tonic-gate 	return (yesno());
36637c478bd9Sstevel@tonic-gate }
36647c478bd9Sstevel@tonic-gate 
3665bb16350dSlclee static void
36667c478bd9Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
36677c478bd9Sstevel@tonic-gate {
36687c478bd9Sstevel@tonic-gate 	struct vtoc v;
36697c478bd9Sstevel@tonic-gate 	struct dk_geom d;
36707c478bd9Sstevel@tonic-gate 	struct part_info pi;
36717c478bd9Sstevel@tonic-gate 	long totsize;
36727c478bd9Sstevel@tonic-gate 	int idx = -1;
36737c478bd9Sstevel@tonic-gate 
36747c478bd9Sstevel@tonic-gate 	/*
36757c478bd9Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
36767c478bd9Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
36777c478bd9Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
36787c478bd9Sstevel@tonic-gate 	 */
36797c478bd9Sstevel@tonic-gate 	if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
36807c478bd9Sstevel@tonic-gate 		if (pi.p_start != 0) {
36817c478bd9Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
36827c478bd9Sstevel@tonic-gate 			    devname)) {
36837c478bd9Sstevel@tonic-gate 				(void) close(fd);
36847c478bd9Sstevel@tonic-gate 				exit(1);
36857c478bd9Sstevel@tonic-gate 			}
36867c478bd9Sstevel@tonic-gate 		}
36877c478bd9Sstevel@tonic-gate 	} else {
36887c478bd9Sstevel@tonic-gate 		if ((idx = read_vtoc(fd, &v)) < 0) {
36897c478bd9Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
36907c478bd9Sstevel@tonic-gate 			    devname)) {
36917c478bd9Sstevel@tonic-gate 				(void) close(fd);
36927c478bd9Sstevel@tonic-gate 				exit(1);
36937c478bd9Sstevel@tonic-gate 			}
36947c478bd9Sstevel@tonic-gate 			return;
36957c478bd9Sstevel@tonic-gate 		}
36967c478bd9Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
36977c478bd9Sstevel@tonic-gate 			perror(devname);
36987c478bd9Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
36997c478bd9Sstevel@tonic-gate 			    devname)) {
37007c478bd9Sstevel@tonic-gate 				(void) close(fd);
37017c478bd9Sstevel@tonic-gate 				exit(1);
37027c478bd9Sstevel@tonic-gate 			}
37037c478bd9Sstevel@tonic-gate 			return;
37047c478bd9Sstevel@tonic-gate 		}
37057c478bd9Sstevel@tonic-gate 		totsize = d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
37067c478bd9Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
37077c478bd9Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
37087c478bd9Sstevel@tonic-gate 			    devname)) {
37097c478bd9Sstevel@tonic-gate 				(void) close(fd);
37107c478bd9Sstevel@tonic-gate 				exit(1);
37117c478bd9Sstevel@tonic-gate 			}
37127c478bd9Sstevel@tonic-gate 		}
37137c478bd9Sstevel@tonic-gate 	}
37147c478bd9Sstevel@tonic-gate }
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate /*
37187c478bd9Sstevel@tonic-gate  * get_node
37197c478bd9Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
37207c478bd9Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
37217c478bd9Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
37227c478bd9Sstevel@tonic-gate  */
37237c478bd9Sstevel@tonic-gate static char *
37247c478bd9Sstevel@tonic-gate get_node(char *devname)
37257c478bd9Sstevel@tonic-gate {
37267c478bd9Sstevel@tonic-gate 	char *node;
37277c478bd9Sstevel@tonic-gate 	struct stat statbuf;
37287c478bd9Sstevel@tonic-gate 	size_t space;
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
37317c478bd9Sstevel@tonic-gate 	if (io_image)
37327c478bd9Sstevel@tonic-gate 		return (devname);
37337c478bd9Sstevel@tonic-gate 
37347c478bd9Sstevel@tonic-gate 	node = devname;
37357c478bd9Sstevel@tonic-gate 
37367c478bd9Sstevel@tonic-gate 	/* Try the node as provided first */
37377c478bd9Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
37387c478bd9Sstevel@tonic-gate 		/*
37397c478bd9Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
37407c478bd9Sstevel@tonic-gate 		 * default path and try again.
37417c478bd9Sstevel@tonic-gate 		 */
37427c478bd9Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
37437c478bd9Sstevel@tonic-gate 
37447c478bd9Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
3745bb16350dSlclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
37467c478bd9Sstevel@tonic-gate 			    "for device node.\n");
37477c478bd9Sstevel@tonic-gate 			exit(1);
37487c478bd9Sstevel@tonic-gate 		}
37497c478bd9Sstevel@tonic-gate 
37507c478bd9Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
37517c478bd9Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
37527c478bd9Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
37537c478bd9Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
37547c478bd9Sstevel@tonic-gate 
37557c478bd9Sstevel@tonic-gate 		/* Try to stat it again */
37567c478bd9Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
37577c478bd9Sstevel@tonic-gate 			/* Failed all options, give up */
3758bb16350dSlclee 			(void) fprintf(stderr,
3759bb16350dSlclee 			    "fdisk: Cannot stat device %s.\n",
37607c478bd9Sstevel@tonic-gate 			    devname);
37617c478bd9Sstevel@tonic-gate 			exit(1);
37627c478bd9Sstevel@tonic-gate 		}
37637c478bd9Sstevel@tonic-gate 	}
37647c478bd9Sstevel@tonic-gate 
37657c478bd9Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
37667c478bd9Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
3767bb16350dSlclee 		(void) fprintf(stderr,
3768bb16350dSlclee 		    "fdisk: %s must be a raw device.\n", node);
37697c478bd9Sstevel@tonic-gate 		exit(1);
37707c478bd9Sstevel@tonic-gate 	}
37717c478bd9Sstevel@tonic-gate 
37727c478bd9Sstevel@tonic-gate 	return (node);
37737c478bd9Sstevel@tonic-gate }
3774