xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 9889:68d0fe4c716e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55169Slclee  * Common Development and Distribution License (the "License").
65169Slclee  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
215169Slclee 
220Sstevel@tonic-gate /*
238904SBarry.Harding@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
280Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
320Sstevel@tonic-gate /*	  All Rights Reserved	*/
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
360Sstevel@tonic-gate  * This program reads the partition table on the specified device and
370Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
380Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
390Sstevel@tonic-gate  * options are also available.
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate #include <stdio.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <fcntl.h>
470Sstevel@tonic-gate #include <ctype.h>
480Sstevel@tonic-gate #include <sys/stat.h>
490Sstevel@tonic-gate #include <sys/types.h>
507563SPrasad.Singamsetty@Sun.COM #include <limits.h>
510Sstevel@tonic-gate #include <sys/param.h>
520Sstevel@tonic-gate #include <sys/systeminfo.h>
530Sstevel@tonic-gate #include <sys/efi_partition.h>
540Sstevel@tonic-gate #include <sys/byteorder.h>
550Sstevel@tonic-gate #include <sys/systeminfo.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
580Sstevel@tonic-gate #include <sys/dkio.h>
590Sstevel@tonic-gate #include <sys/vtoc.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #define	CLR_SCR ""
620Sstevel@tonic-gate #define	CLR_LIN ""
630Sstevel@tonic-gate #define	HOME "" \
640Sstevel@tonic-gate 	""
650Sstevel@tonic-gate #define	Q_LINE ""
660Sstevel@tonic-gate #define	W_LINE ""
670Sstevel@tonic-gate #define	E_LINE ""
680Sstevel@tonic-gate #define	M_LINE "" \
690Sstevel@tonic-gate 	""
700Sstevel@tonic-gate #define	T_LINE ""
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
730Sstevel@tonic-gate 
748333SSuhasini.Peddada@Sun.COM /* XXX - should be in fdisk.h, used by sd as well */
758333SSuhasini.Peddada@Sun.COM 
768333SSuhasini.Peddada@Sun.COM /*
778333SSuhasini.Peddada@Sun.COM  * the MAX values are the maximum usable values for BIOS chs values
788333SSuhasini.Peddada@Sun.COM  * The MAX_CYL value of 1022 is the maximum usable value
798333SSuhasini.Peddada@Sun.COM  *   the value of 1023 is a fence value,
808333SSuhasini.Peddada@Sun.COM  *   indicating no CHS geometry exists for the corresponding LBA value.
818333SSuhasini.Peddada@Sun.COM  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
828333SSuhasini.Peddada@Sun.COM  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
838333SSuhasini.Peddada@Sun.COM  */
848333SSuhasini.Peddada@Sun.COM #define	MAX_SECT	(63)
858333SSuhasini.Peddada@Sun.COM #define	MAX_CYL		(1022)
868333SSuhasini.Peddada@Sun.COM #define	MAX_HEAD	(254)
878333SSuhasini.Peddada@Sun.COM 
887563SPrasad.Singamsetty@Sun.COM #define	DK_MAX_2TB	UINT32_MAX	/* Max # of sectors in 2TB */
897563SPrasad.Singamsetty@Sun.COM 
900Sstevel@tonic-gate /* for clear_vtoc() */
910Sstevel@tonic-gate #define	OLD		0
920Sstevel@tonic-gate #define	NEW		1
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
950Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
960Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
970Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
980Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
990Sstevel@tonic-gate 
1008333SSuhasini.Peddada@Sun.COM /*
1018333SSuhasini.Peddada@Sun.COM  * Support for fdisk(1M) on the SPARC platform
1028333SSuhasini.Peddada@Sun.COM  *	In order to convert little endian values to big endian for SPARC,
1038333SSuhasini.Peddada@Sun.COM  *	byte/short and long values must be swapped.
1048333SSuhasini.Peddada@Sun.COM  *	These swapping macros will be used to access information in the
1058333SSuhasini.Peddada@Sun.COM  *	mboot and ipart structures.
1068333SSuhasini.Peddada@Sun.COM  */
1078333SSuhasini.Peddada@Sun.COM 
1088333SSuhasini.Peddada@Sun.COM #ifdef sparc
1098333SSuhasini.Peddada@Sun.COM #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1108333SSuhasini.Peddada@Sun.COM #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
1118333SSuhasini.Peddada@Sun.COM 			    (les((unsigned)((val)&0xffff0000)>>16)))
1128333SSuhasini.Peddada@Sun.COM #else
1138333SSuhasini.Peddada@Sun.COM #define	les(val)	(val)
1148333SSuhasini.Peddada@Sun.COM #define	lel(val)	(val)
1158333SSuhasini.Peddada@Sun.COM #endif
1168333SSuhasini.Peddada@Sun.COM 
1170Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1186549Sbharding #define	VTOC_OFFSET	1
1190Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1200Sstevel@tonic-gate #define	VTOC_OFFSET	0
1210Sstevel@tonic-gate #else
1220Sstevel@tonic-gate #error No VTOC format defined.
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate 
125251Slclee static char Usage[] = "Usage: fdisk\n"
1260Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1270Sstevel@tonic-gate "[ -b masterboot ]\n"
1280Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1290Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1300Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1310Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1320Sstevel@tonic-gate 
133251Slclee static char Usage1[] = "    Partition options:\n"
1340Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1350Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1360Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1370Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1380Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1390Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1400Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1410Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1420Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1430Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1440Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1450Sstevel@tonic-gate "			  start of partition\n"
1460Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1470Sstevel@tonic-gate "	-b master_boot\n"
1480Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1490Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1500Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1510Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1520Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1530Sstevel@tonic-gate "	-F fdisk_file\n"
1540Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1550Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1560Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1570Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1580Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1590Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1600Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1610Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1620Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1630Sstevel@tonic-gate "	-W fdisk_file\n"
1640Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1650Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1660Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
1670Sstevel@tonic-gate "    Diagnostic options:\n"
1680Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
1690Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
1700Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
1710Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
1720Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
1730Sstevel@tonic-gate "		BCYL		cylinder offset\n"
1740Sstevel@tonic-gate "		NHEADS		number of heads\n"
1750Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
1760Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
1770Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
1780Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
1790Sstevel@tonic-gate "	-o offset\n"
1800Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
1810Sstevel@tonic-gate "		-P # specified.\n"
1820Sstevel@tonic-gate "	-P fill_patt\n"
1830Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
1840Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
1850Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
1860Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
1870Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
1880Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
1890Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
1900Sstevel@tonic-gate "	-S geom_file\n"
1910Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
1920Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
1930Sstevel@tonic-gate 
194251Slclee static char Ostr[] = "Other OS";
195251Slclee static char Dstr[] = "DOS12";
196251Slclee static char D16str[] = "DOS16";
197251Slclee static char DDstr[] = "DOS-DATA";
198251Slclee static char EDstr[] = "EXT-DOS";
199251Slclee static char DBstr[] = "DOS-BIG";
200251Slclee static char PCstr[] = "PCIX";
201251Slclee static char Ustr[] = "UNIX System";
202251Slclee static char SUstr[] = "Solaris";
203251Slclee static char SU2str[] = "Solaris2";
204251Slclee static char X86str[] = "x86 Boot";
205251Slclee static char DIAGstr[] = "Diagnostic";
206251Slclee static char IFSstr[] = "IFS: NTFS";
207251Slclee static char AIXstr[] = "AIX Boot";
208251Slclee static char AIXDstr[] = "AIX Data";
209251Slclee static char OS2str[] = "OS/2 Boot";
210251Slclee static char WINstr[] = "Win95 FAT32";
211251Slclee static char EWINstr[] = "Ext Win95";
212251Slclee static char FAT95str[] = "FAT16 LBA";
213251Slclee static char EXTLstr[] = "EXT LBA";
214251Slclee static char LINUXstr[] = "Linux";
215251Slclee static char CPMstr[] = "CP/M";
216251Slclee static char NOVstr[] = "Netware 3.x+";
217251Slclee static char QNXstr[] = "QNX 4.x";
218251Slclee static char QNX2str[] = "QNX part 2";
219251Slclee static char QNX3str[] = "QNX part 3";
220251Slclee static char LINNATstr[] = "Linux native";
221251Slclee static char NTFSVOL1str[] = "NT volset 1";
222251Slclee static char NTFSVOL2str[] = "NT volset 2";
223251Slclee static char BSDstr[] = "BSD OS";
224251Slclee static char NEXTSTEPstr[] = "NeXTSTEP";
225251Slclee static char BSDIFSstr[] = "BSDI FS";
226251Slclee static char BSDISWAPstr[] = "BSDI swap";
227251Slclee static char Actvstr[] = "Active";
228251Slclee static char EFIstr[] = "EFI";
229251Slclee static char NAstr[] = "      ";
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /* All the user options and flags */
232251Slclee static char *Dfltdev;			/* name of fixed disk drive */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /* Diagnostic options */
235251Slclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
236251Slclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
237251Slclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
238251Slclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
239251Slclee static int	io_lgeom = 0;		/* get label geometry (-g) */
240251Slclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
241251Slclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
242251Slclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2450Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
2466549Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
2476549Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /* Partition table flags */
250251Slclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
251251Slclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
252251Slclee static int	io_fdisk = 0;		/* do fdisk operation */
253251Slclee static int	io_ifdisk = 0;		/* interactive partition */
254251Slclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
255251Slclee 
256251Slclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
257251Slclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
258251Slclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
259251Slclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
260251Slclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
261251Slclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
262251Slclee 
263251Slclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
264251Slclee 
265251Slclee static struct mboot BootCod;		/* buffer for master boot record */
266251Slclee 
267251Slclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
268251Slclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
269251Slclee static int	io_debug = 0;		/* activate verbose mode (-d) */
270251Slclee static int	io_image = 0;		/* create image using geometry (-I) */
271251Slclee 
272251Slclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
273251Slclee static char	*Bootsect;		/* pointer to sector zero buffer */
274251Slclee static char	*Nullsect;
2757563SPrasad.Singamsetty@Sun.COM static struct extvtoc	disk_vtoc;	/* verify VTOC table */
276251Slclee static int	vt_inval = 0;
277251Slclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
278251Slclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
279251Slclee 
280251Slclee static struct ipart	Table[FD_NUMPART];
281251Slclee static struct ipart	Old_Table[FD_NUMPART];
2828904SBarry.Harding@Sun.COM static int		skip_verify[FD_NUMPART]; /* special case skip sz chk */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate /* Disk geometry information */
2855169Slclee static struct dk_minfo	minfo;
286251Slclee static struct dk_geom	disk_geom;
287251Slclee 
2887563SPrasad.Singamsetty@Sun.COM static int Dev;			/* fd for open device */
2897563SPrasad.Singamsetty@Sun.COM 
2905169Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
2917563SPrasad.Singamsetty@Sun.COM static diskaddr_t	chs_capacity;	/* Numcyl_usable * heads * sectors */
2927563SPrasad.Singamsetty@Sun.COM 
2937563SPrasad.Singamsetty@Sun.COM static int		Numcyl_usable;	/* Number of usable cylinders */
2947563SPrasad.Singamsetty@Sun.COM 					/*  used to limit fdisk to 2TB */
2957563SPrasad.Singamsetty@Sun.COM 
2960Sstevel@tonic-gate /* Physical geometry for the drive */
297251Slclee static int	Numcyl;			/* number of cylinders */
298251Slclee static int	heads;			/* number of heads */
299251Slclee static int	sectors;		/* number of sectors per track */
300251Slclee static int	acyl;			/* number of alternate sectors */
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
303251Slclee static int	hba_Numcyl;		/* number of cylinders */
304251Slclee static int	hba_heads;		/* number of heads */
305251Slclee static int	hba_sectors;		/* number of sectors per track */
306251Slclee 
307251Slclee static int	sectsiz;		/* sector size */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate /* Load functions for fdisk table modification */
3100Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3110Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3120Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate #define	CBUFLEN 80
315251Slclee static char s[CBUFLEN];
316251Slclee 
317251Slclee static void update_disk_and_exit(boolean_t table_changed);
318251Slclee int main(int argc, char *argv[]);
319251Slclee static int read_geom(char *sgeom);
320251Slclee static void dev_mboot_read(void);
3216549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz);
322251Slclee static void mboot_read(void);
323251Slclee static void fill_patt(void);
324251Slclee static void abs_read(void);
325251Slclee static void abs_write(void);
326251Slclee static void load(int funct, char *file);
327251Slclee static void Set_Table_CHS_Values(int ti);
328251Slclee static int insert_tbl(int id, int act,
329251Slclee     int bhead, int bsect, int bcyl,
330251Slclee     int ehead, int esect, int ecyl,
3317563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect);
3328904SBarry.Harding@Sun.COM static int entry_from_old_table(int id, int act,
3338904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
3348904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
3358904SBarry.Harding@Sun.COM     uint32_t rsect, uint32_t numsect);
336251Slclee static int verify_tbl(void);
337251Slclee static int pars_fdisk(char *line,
338251Slclee     int *id, int *act,
339251Slclee     int *bhead, int *bsect, int *bcyl,
340251Slclee     int *ehead, int *esect, int *ecyl,
3417563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect);
3427563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect);
343251Slclee static void stage0(void);
344251Slclee static int pcreate(void);
345251Slclee static int specify(uchar_t tsystid);
346251Slclee static void dispmenu(void);
347251Slclee static int pchange(void);
348251Slclee static int ppartid(void);
349251Slclee static char pdelete(void);
350251Slclee static void rm_blanks(char *s);
351251Slclee static int getcyl(void);
352251Slclee static void disptbl(void);
353251Slclee static void print_Table(void);
354251Slclee static void copy_Table_to_Old_Table(void);
355251Slclee static void nulltbl(void);
356251Slclee static void copy_Bootblk_to_Table(void);
357251Slclee static void fill_ipart(char *bootptr, struct ipart *partp);
358251Slclee #ifdef sparc
359251Slclee uchar_t getbyte(char **bp);
360251Slclee uint32_t getlong(char **bp);
361251Slclee #endif
362251Slclee static void copy_Table_to_Bootblk(void);
363251Slclee static int TableChanged(void);
364251Slclee static void ffile_write(char *file);
365251Slclee static void fix_slice(void);
366251Slclee static int yesno(void);
367251Slclee static int readvtoc(void);
368251Slclee static int writevtoc(void);
369251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
370251Slclee static int clear_efi(void);
371251Slclee static void clear_vtoc(int table, int part);
372251Slclee static int lecture_and_query(char *warning, char *devname);
373251Slclee static void sanity_check_provided_device(char *devname, int fd);
374251Slclee static char *get_node(char *devname);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate static void
3770Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	if (table_changed) {
3800Sstevel@tonic-gate 		/*
3810Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
3820Sstevel@tonic-gate 		 * and write it to disk
3830Sstevel@tonic-gate 		 */
3840Sstevel@tonic-gate 		copy_Table_to_Bootblk();
3850Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
3890Sstevel@tonic-gate 	if (io_adjt)
3900Sstevel@tonic-gate 		fix_slice();
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	exit(0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3958333SSuhasini.Peddada@Sun.COM 
3968333SSuhasini.Peddada@Sun.COM 
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate  * main
3990Sstevel@tonic-gate  * Process command-line options.
4000Sstevel@tonic-gate  */
401251Slclee int
4020Sstevel@tonic-gate main(int argc, char *argv[])
4030Sstevel@tonic-gate {
404251Slclee 	int c, i;
4050Sstevel@tonic-gate 	extern	int optind;
4060Sstevel@tonic-gate 	extern	char *optarg;
4070Sstevel@tonic-gate 	int	errflg = 0;
4080Sstevel@tonic-gate 	int	diag_cnt = 0;
4090Sstevel@tonic-gate 	int openmode;
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
4120Sstevel@tonic-gate 	setbuf(stdout, 0);
4130Sstevel@tonic-gate 
4140Sstevel@tonic-gate 	/* Process the options. */
4150Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
4160Sstevel@tonic-gate 	    != EOF) {
4170Sstevel@tonic-gate 		switch (c) {
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 			case 'o':
4206549Sbharding 				io_offset = (off_t)strtoull(optarg, 0, 0);
4210Sstevel@tonic-gate 				continue;
4220Sstevel@tonic-gate 			case 's':
4236549Sbharding 				io_size = (off_t)strtoull(optarg, 0, 0);
4240Sstevel@tonic-gate 				continue;
4250Sstevel@tonic-gate 			case 'P':
4260Sstevel@tonic-gate 				diag_cnt++;
4270Sstevel@tonic-gate 				io_patt++;
4280Sstevel@tonic-gate 				io_fatt = optarg;
4290Sstevel@tonic-gate 				continue;
4300Sstevel@tonic-gate 			case 'w':
4310Sstevel@tonic-gate 				diag_cnt++;
4320Sstevel@tonic-gate 				io_wrt++;
4330Sstevel@tonic-gate 				continue;
4340Sstevel@tonic-gate 			case 'r':
4350Sstevel@tonic-gate 				diag_cnt++;
4360Sstevel@tonic-gate 				io_rd++;
4370Sstevel@tonic-gate 				continue;
4380Sstevel@tonic-gate 			case 'd':
4390Sstevel@tonic-gate 				io_debug++;
4400Sstevel@tonic-gate 				continue;
4410Sstevel@tonic-gate 			case 'I':
4420Sstevel@tonic-gate 				io_image++;
4430Sstevel@tonic-gate 				continue;
4440Sstevel@tonic-gate 			case 'R':
4450Sstevel@tonic-gate 				io_readonly++;
4460Sstevel@tonic-gate 				continue;
4470Sstevel@tonic-gate 			case 'S':
4480Sstevel@tonic-gate 				diag_cnt++;
4490Sstevel@tonic-gate 				io_sgeom = optarg;
4500Sstevel@tonic-gate 				continue;
4510Sstevel@tonic-gate 			case 'T':
4520Sstevel@tonic-gate 				io_ADJT++;
453251Slclee 				/* FALLTHRU */
4540Sstevel@tonic-gate 			case 't':
4550Sstevel@tonic-gate 				io_adjt++;
4560Sstevel@tonic-gate 				continue;
4570Sstevel@tonic-gate 			case 'B':
4580Sstevel@tonic-gate 				io_wholedisk++;
4590Sstevel@tonic-gate 				io_fdisk++;
4600Sstevel@tonic-gate 				continue;
4610Sstevel@tonic-gate 			case 'E':
4620Sstevel@tonic-gate 				io_EFIdisk++;
4630Sstevel@tonic-gate 				io_fdisk++;
4640Sstevel@tonic-gate 				continue;
4650Sstevel@tonic-gate 			case 'g':
4660Sstevel@tonic-gate 				diag_cnt++;
4670Sstevel@tonic-gate 				io_lgeom++;
4680Sstevel@tonic-gate 				continue;
4690Sstevel@tonic-gate 			case 'G':
4700Sstevel@tonic-gate 				diag_cnt++;
4710Sstevel@tonic-gate 				io_pgeom++;
4720Sstevel@tonic-gate 				continue;
4730Sstevel@tonic-gate 			case 'n':
4740Sstevel@tonic-gate 				io_nifdisk++;
4750Sstevel@tonic-gate 				io_fdisk++;
4760Sstevel@tonic-gate 				continue;
4770Sstevel@tonic-gate 			case 'F':
4780Sstevel@tonic-gate 				io_fdisk++;
4790Sstevel@tonic-gate 				io_ffdisk = optarg;
4800Sstevel@tonic-gate 				continue;
4810Sstevel@tonic-gate 			case 'b':
4820Sstevel@tonic-gate 				io_mboot = optarg;
4830Sstevel@tonic-gate 				continue;
4840Sstevel@tonic-gate 			case 'W':
4850Sstevel@tonic-gate 				/*
4860Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
4870Sstevel@tonic-gate 				 * to standard output, otherwise write
4880Sstevel@tonic-gate 				 * to the specified file.
4890Sstevel@tonic-gate 				 */
4900Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
4910Sstevel@tonic-gate 					stdo_flag = 1;
4920Sstevel@tonic-gate 				else
4930Sstevel@tonic-gate 					io_Wfdisk = optarg;
4940Sstevel@tonic-gate 				io_fdisk++;
4950Sstevel@tonic-gate 				continue;
4960Sstevel@tonic-gate 			case 'A':
4970Sstevel@tonic-gate 				io_fdisk++;
4980Sstevel@tonic-gate 				io_Afdisk = optarg;
4990Sstevel@tonic-gate 				continue;
5000Sstevel@tonic-gate 			case 'D':
5010Sstevel@tonic-gate 				io_fdisk++;
5020Sstevel@tonic-gate 				io_Dfdisk = optarg;
5030Sstevel@tonic-gate 				continue;
5040Sstevel@tonic-gate 			case 'h':
505251Slclee 				(void) fprintf(stderr, "%s\n", Usage);
506251Slclee 				(void) fprintf(stderr, "%s\n", Usage1);
5070Sstevel@tonic-gate 				exit(0);
508251Slclee 				/* FALLTHRU */
5090Sstevel@tonic-gate 			case 'v':
5100Sstevel@tonic-gate 				v_flag = 1;
5110Sstevel@tonic-gate 				continue;
5120Sstevel@tonic-gate 			case '?':
5130Sstevel@tonic-gate 				errflg++;
5140Sstevel@tonic-gate 				break;
5150Sstevel@tonic-gate 		}
5160Sstevel@tonic-gate 		break;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
5200Sstevel@tonic-gate 		diag_cnt = 0;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/* User option checking */
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* By default, run in interactive mode */
5260Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
5270Sstevel@tonic-gate 		io_ifdisk++;
5280Sstevel@tonic-gate 		io_fdisk++;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
5310Sstevel@tonic-gate 		errflg++;
5320Sstevel@tonic-gate 	}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	/* Was any error detected? */
5350Sstevel@tonic-gate 	if (errflg || argc == optind) {
536251Slclee 		(void) fprintf(stderr, "%s\n", Usage);
537251Slclee 		(void) fprintf(stderr,
5380Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
5390Sstevel@tonic-gate 		exit(2);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate 	/* Figure out the correct device node to open */
5440Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
5450Sstevel@tonic-gate 
5460Sstevel@tonic-gate 	if (io_readonly)
5470Sstevel@tonic-gate 		openmode = O_RDONLY;
5480Sstevel@tonic-gate 	else
5490Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
552251Slclee 		(void) fprintf(stderr,
553251Slclee 		    "fdisk: Cannot open device %s.\n",
554251Slclee 		    Dfltdev);
5550Sstevel@tonic-gate 		exit(1);
5560Sstevel@tonic-gate 	}
5575169Slclee 	/*
5585169Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
5595169Slclee 	 * in that case leave the minfo structure zeroed
5605169Slclee 	 */
5615169Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
562*9889SLarry.Liu@Sun.COM 		(void) memset(&minfo, 0, sizeof (minfo));
5635169Slclee 	}
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/* Get the disk geometry */
5660Sstevel@tonic-gate 	if (!io_image) {
5670Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
5680Sstevel@tonic-gate 		errno = 0;
5690Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 			/*
5720Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
5730Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
5740Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
5750Sstevel@tonic-gate 			 */
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 			if (errno == ENOTTY) {
5780Sstevel@tonic-gate 				v_flag = 0;
5790Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
5800Sstevel@tonic-gate 			} else if (errno == EINVAL) {
5810Sstevel@tonic-gate 				/*
5820Sstevel@tonic-gate 				 * This means that the ioctl exists, but
5830Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
5840Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
5850Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
5860Sstevel@tonic-gate 				 */
5870Sstevel@tonic-gate 				v_flag = 0;
5880Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
5890Sstevel@tonic-gate 			} else {
5900Sstevel@tonic-gate 				(void) fprintf(stderr,
5910Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
5920Sstevel@tonic-gate 				    argv[optind]);
5930Sstevel@tonic-gate 				exit(1);
5940Sstevel@tonic-gate 			}
5950Sstevel@tonic-gate 		} else {
5960Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
5970Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
5980Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
5990Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		errno = 0;
6030Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
6040Sstevel@tonic-gate 			if (errno == ENOTTY) {
6050Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
6060Sstevel@tonic-gate 			} else {
6070Sstevel@tonic-gate 				(void) fprintf(stderr,
6080Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
6090Sstevel@tonic-gate 				    argv[optind]);
6100Sstevel@tonic-gate 				exit(1);
6110Sstevel@tonic-gate 			}
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 		}
6140Sstevel@tonic-gate 		/*
6150Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
6160Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
6170Sstevel@tonic-gate 		 */
6180Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
6190Sstevel@tonic-gate 			errno = 0;
6200Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6210Sstevel@tonic-gate 				(void) fprintf(stderr,
6220Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
6230Sstevel@tonic-gate 				    argv[optind]);
6240Sstevel@tonic-gate 				exit(1);
6250Sstevel@tonic-gate 			}
6260Sstevel@tonic-gate 		}
6270Sstevel@tonic-gate 
6280Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6290Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6300Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
631*9889SLarry.Liu@Sun.COM 
632*9889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
633*9889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
634*9889SLarry.Liu@Sun.COM 		else
635*9889SLarry.Liu@Sun.COM 			sectsiz = 512;
636*9889SLarry.Liu@Sun.COM 
6370Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		/*
6400Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
6410Sstevel@tonic-gate 		 * or we got an invalid hba geometry
6420Sstevel@tonic-gate 		 * then set hba geometry based on max values
6430Sstevel@tonic-gate 		 */
6440Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
645251Slclee 		    disk_geom.dkg_ncyl == 0 ||
646251Slclee 		    disk_geom.dkg_nhead == 0 ||
647251Slclee 		    disk_geom.dkg_nsect == 0 ||
6480Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
6490Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
6500Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 			/*
6530Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
6540Sstevel@tonic-gate 			 */
6550Sstevel@tonic-gate 			v_flag = 0;
6560Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
6570Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
6580Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
6590Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
6600Sstevel@tonic-gate 		}
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		if (io_debug) {
663251Slclee 			(void) fprintf(stderr, "Physical Geometry:\n");
664251Slclee 			(void) fprintf(stderr,
6650Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6660Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6670Sstevel@tonic-gate 			    Numcyl,
6680Sstevel@tonic-gate 			    heads,
6690Sstevel@tonic-gate 			    sectors,
6700Sstevel@tonic-gate 			    sectsiz,
671251Slclee 			    Numcyl * heads * sectors,
672251Slclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
673251Slclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
674251Slclee 			(void) fprintf(stderr,
6750Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6760Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6770Sstevel@tonic-gate 			    hba_Numcyl,
6780Sstevel@tonic-gate 			    hba_heads,
6790Sstevel@tonic-gate 			    hba_sectors,
6800Sstevel@tonic-gate 			    sectsiz,
681251Slclee 			    hba_Numcyl * hba_heads * hba_sectors,
682251Slclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
683251Slclee 			    1048576);
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
6880Sstevel@tonic-gate 	if (io_lgeom) {
6890Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6900Sstevel@tonic-gate 			(void) fprintf(stderr,
6910Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
6920Sstevel@tonic-gate 			    argv[optind]);
6930Sstevel@tonic-gate 			exit(1);
6940Sstevel@tonic-gate 		}
6950Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6960Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6970Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
698*9889SLarry.Liu@Sun.COM 		if (minfo.dki_lbsize != 0)
699*9889SLarry.Liu@Sun.COM 			sectsiz = minfo.dki_lbsize;
700*9889SLarry.Liu@Sun.COM 		else
701*9889SLarry.Liu@Sun.COM 			sectsiz = 512;
702*9889SLarry.Liu@Sun.COM 
7030Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
704251Slclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
705251Slclee 		(void) printf(
706251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
7070Sstevel@tonic-gate 		    " SECSIZ\n");
708251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
7090Sstevel@tonic-gate 		    Numcyl,
7100Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
7110Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
7120Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7130Sstevel@tonic-gate 		    heads,
7140Sstevel@tonic-gate 		    sectors,
7150Sstevel@tonic-gate 		    sectsiz);
7160Sstevel@tonic-gate 		exit(0);
7170Sstevel@tonic-gate 	} else if (io_pgeom) {
7180Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
7190Sstevel@tonic-gate 			(void) fprintf(stderr,
7200Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
7210Sstevel@tonic-gate 			    argv[optind]);
7220Sstevel@tonic-gate 			exit(1);
7230Sstevel@tonic-gate 		}
724251Slclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
725251Slclee 		(void) printf(
726251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
7270Sstevel@tonic-gate 		    " SECSIZ\n");
728251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
7290Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
7300Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
7310Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
7320Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7330Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
7340Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
7350Sstevel@tonic-gate 		    sectsiz);
7360Sstevel@tonic-gate 		exit(0);
7370Sstevel@tonic-gate 	} else if (io_sgeom) {
7380Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
7390Sstevel@tonic-gate 			exit(1);
7400Sstevel@tonic-gate 		} else if (!io_image) {
7410Sstevel@tonic-gate 			exit(0);
7420Sstevel@tonic-gate 		}
7430Sstevel@tonic-gate 	}
7440Sstevel@tonic-gate 
7455169Slclee 	/*
7465169Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
7475169Slclee 	 * in that case use CHS
7485169Slclee 	 */
7497563SPrasad.Singamsetty@Sun.COM 	chs_capacity = (diskaddr_t)Numcyl * heads * sectors;
7505169Slclee 	dev_capacity = chs_capacity;
7517563SPrasad.Singamsetty@Sun.COM 	Numcyl_usable = Numcyl;
7527563SPrasad.Singamsetty@Sun.COM 
7537563SPrasad.Singamsetty@Sun.COM 	if (chs_capacity > DK_MAX_2TB) {
7547563SPrasad.Singamsetty@Sun.COM 		/* limit to 2TB */
7557563SPrasad.Singamsetty@Sun.COM 		Numcyl_usable = DK_MAX_2TB / (heads * sectors);
7567563SPrasad.Singamsetty@Sun.COM 		chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors;
7577563SPrasad.Singamsetty@Sun.COM 	}
7587563SPrasad.Singamsetty@Sun.COM 
7595169Slclee 	if (minfo.dki_capacity > 0)
7605169Slclee 		dev_capacity = minfo.dki_capacity;
7615169Slclee 
7620Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
763*9889SLarry.Liu@Sun.COM 	Bootsect = (char *)calloc(3 * sectsiz, 1);
7640Sstevel@tonic-gate 	if (Bootsect == NULL) {
765251Slclee 		(void) fprintf(stderr,
7660Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
7670Sstevel@tonic-gate 		    " (%d bytes).\n",
768251Slclee 		    3 * sectsiz);
7690Sstevel@tonic-gate 		exit(1);
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
7730Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
7740Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
7750Sstevel@tonic-gate 		Nullsect[i] = 0;
7760Sstevel@tonic-gate 	}
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	/* Find out what the user wants done */
7790Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
7800Sstevel@tonic-gate 		abs_read();	/* will not return */
7810Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
7820Sstevel@tonic-gate 		abs_write();	/* will not return */
7830Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
7840Sstevel@tonic-gate 		fill_patt();	/* will not return */
7850Sstevel@tonic-gate 	}
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
7930Sstevel@tonic-gate 	mboot_read();
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/* Read from disk master boot */
7960Sstevel@tonic-gate 	dev_mboot_read();
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	/*
7990Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
8000Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
8010Sstevel@tonic-gate 	 */
8020Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
8030Sstevel@tonic-gate 	copy_Bootblk_to_Table();
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
8060Sstevel@tonic-gate 	copy_Table_to_Old_Table();
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
8090Sstevel@tonic-gate 	if (io_ffdisk) {
8100Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
8110Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
8120Sstevel@tonic-gate 	}
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
8150Sstevel@tonic-gate 	if (io_Dfdisk) {
8160Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 	if (io_Afdisk) {
8190Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
8230Sstevel@tonic-gate 		/* Check if there is no fdisk table */
8240Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
8250Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
826251Slclee 				(void) printf(
827251Slclee 				    "No fdisk table exists. The default"
828251Slclee 				    " partition for the disk is:\n\n"
829251Slclee 				    "  a 100%% \"SOLARIS System\" "
830251Slclee 				    "partition\n\n"
831251Slclee 				    "Type \"y\" to accept the default "
8320Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
8330Sstevel@tonic-gate 				    "edit the\n partition table.\n");
8347563SPrasad.Singamsetty@Sun.COM 
8357563SPrasad.Singamsetty@Sun.COM 				if (Numcyl > Numcyl_usable)
8367563SPrasad.Singamsetty@Sun.COM 					(void) printf("WARNING: Disk is larger"
8377563SPrasad.Singamsetty@Sun.COM 					    " than 2TB. Solaris partition will"
8387563SPrasad.Singamsetty@Sun.COM 					    " be limited to 2 TB.\n");
8390Sstevel@tonic-gate 			}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 			/* Edit the partition table as directed */
8420Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 				/* Default scenario */
8450Sstevel@tonic-gate 				nulltbl();
8460Sstevel@tonic-gate 				/* now set up UNIX System partition */
8470Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
8488333SSuhasini.Peddada@Sun.COM 				Table[0].relsect = lel(heads * sectors);
8497563SPrasad.Singamsetty@Sun.COM 
8507563SPrasad.Singamsetty@Sun.COM 				Table[0].numsect =
8518333SSuhasini.Peddada@Sun.COM 				    lel((ulong_t)((Numcyl_usable - 1) *
8520Sstevel@tonic-gate 				    heads * sectors));
8537563SPrasad.Singamsetty@Sun.COM 
8540Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
8570Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
8580Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8590Sstevel@tonic-gate 			} else if (io_EFIdisk) {
8600Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
8610Sstevel@tonic-gate 				nulltbl();
8620Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
8637563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
8647563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity - 1));
8650Sstevel@tonic-gate 				if (i != 0) {
866251Slclee 					(void) fprintf(stderr,
867251Slclee 					    "Error creating EFI partition\n");
8680Sstevel@tonic-gate 					exit(1);
8690Sstevel@tonic-gate 				}
8700Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8710Sstevel@tonic-gate 			}
8720Sstevel@tonic-gate 		}
8730Sstevel@tonic-gate 	}
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
8760Sstevel@tonic-gate 	if (io_debug) {
877251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
8780Sstevel@tonic-gate 		print_Table();
8790Sstevel@tonic-gate 		if (io_ifdisk) {
880251Slclee 			(void) fprintf(stderr, "\n");
881251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
8829786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
8830Sstevel@tonic-gate 		}
8840Sstevel@tonic-gate 	}
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 	/* Interactive fdisk mode */
8870Sstevel@tonic-gate 	if (io_ifdisk) {
888251Slclee 		(void) printf(CLR_SCR);
8890Sstevel@tonic-gate 		disptbl();
890251Slclee 		for (;;) {
891251Slclee 			stage0();
8920Sstevel@tonic-gate 			copy_Bootblk_to_Table();
8930Sstevel@tonic-gate 			disptbl();
8940Sstevel@tonic-gate 		}
8950Sstevel@tonic-gate 	}
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
8980Sstevel@tonic-gate 	if (io_Wfdisk)
8990Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
9000Sstevel@tonic-gate 	else if (stdo_flag)
9010Sstevel@tonic-gate 		ffile_write((char *)stdout);
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
904251Slclee 	return (0);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * read_geom
9090Sstevel@tonic-gate  * Read geometry from specified file (-S).
9100Sstevel@tonic-gate  */
9110Sstevel@tonic-gate 
912251Slclee static int
913251Slclee read_geom(char *sgeom)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	char	line[256];
9160Sstevel@tonic-gate 	FILE *fp;
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 	/* open the prototype file */
9190Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
9200Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
9210Sstevel@tonic-gate 		    io_sgeom);
9220Sstevel@tonic-gate 		return (1);
9230Sstevel@tonic-gate 	}
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 	/* Read a line from the file */
9260Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
9270Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
9280Sstevel@tonic-gate 			continue;
9290Sstevel@tonic-gate 		else {
9300Sstevel@tonic-gate 			line[strlen(line)] = '\0';
931251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
9320Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
9330Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
9340Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
9350Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
9360Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
9370Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
9380Sstevel@tonic-gate 			    &sectsiz) != 7) {
9390Sstevel@tonic-gate 				(void) fprintf(stderr,
9400Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
9410Sstevel@tonic-gate 				    line);
9420Sstevel@tonic-gate 				return (1);
9430Sstevel@tonic-gate 			}
9440Sstevel@tonic-gate 			break;
9450Sstevel@tonic-gate 		} /* else */
9460Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (!io_image) {
9490Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
9500Sstevel@tonic-gate 			(void) fprintf(stderr,
9510Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
9520Sstevel@tonic-gate 			return (1);
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 	} else {
9550Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
9560Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
9570Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
9580Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
9590Sstevel@tonic-gate 	}
9600Sstevel@tonic-gate 
961251Slclee 	(void) fclose(fp);
9620Sstevel@tonic-gate 	return (0);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate /*
9660Sstevel@tonic-gate  * dev_mboot_read
9670Sstevel@tonic-gate  * Read the master boot sector from the device.
9680Sstevel@tonic-gate  */
969251Slclee static void
970251Slclee dev_mboot_read(void)
9710Sstevel@tonic-gate {
9720Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
9730Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
9740Sstevel@tonic-gate 	}
9750Sstevel@tonic-gate 	if (errno == 0)
9760Sstevel@tonic-gate 		return;
9770Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
978251Slclee 		(void) fprintf(stderr,
9790Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
9800Sstevel@tonic-gate 		    Dfltdev);
9810Sstevel@tonic-gate 		if (!io_image)
9820Sstevel@tonic-gate 			exit(1);
9830Sstevel@tonic-gate 	}
9840Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
985251Slclee 		(void) fprintf(stderr,
9860Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
9870Sstevel@tonic-gate 		    Dfltdev);
9880Sstevel@tonic-gate 		if (!io_image)
9890Sstevel@tonic-gate 			exit(1);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate }
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate /*
9940Sstevel@tonic-gate  * dev_mboot_write
9950Sstevel@tonic-gate  * Write the master boot sector to the device.
9960Sstevel@tonic-gate  */
997251Slclee static void
9986549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
9990Sstevel@tonic-gate {
10000Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
10016478Sbharding 	int	clr_efi = -1;
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	if (io_readonly)
1004251Slclee 		return;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	if (io_debug) {
1007251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
10080Sstevel@tonic-gate 		print_Table();
10090Sstevel@tonic-gate 		if (io_ifdisk) {
1010251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
10119786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 
10156478Sbharding 	/*
10166478Sbharding 	 * If the new table has any Solaris partitions and the old
10176478Sbharding 	 * table does not have an entry that describes it
10186478Sbharding 	 * exactly then clear the old vtoc (if any).
10196478Sbharding 	 */
10200Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10210Sstevel@tonic-gate 
10226478Sbharding 		/* We only care about potential Solaris parts. */
10230Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
10240Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
10250Sstevel@tonic-gate 			continue;
10260Sstevel@tonic-gate 
10276478Sbharding 		/* Does the old table have an exact entry for the new entry? */
10286478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10296478Sbharding 
10306478Sbharding 			/* We only care about old Solaris partitions. */
10316478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
10326478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
10336478Sbharding 
10346478Sbharding 				/* Is this old one the same as a new one? */
10356478Sbharding 				if ((Old_Table[old_pt].relsect ==
10366478Sbharding 				    Table[new_pt].relsect) &&
10376478Sbharding 				    (Old_Table[old_pt].numsect ==
10386478Sbharding 				    Table[new_pt].numsect))
10396478Sbharding 					break; /* Yes */
10406478Sbharding 			}
10416478Sbharding 		}
10426478Sbharding 
10436478Sbharding 		/* Did a solaris partition change location or size? */
10446478Sbharding 		if (old_pt >= FD_NUMPART) {
10456478Sbharding 			/* Yes clear old vtoc */
10466478Sbharding 			if (io_debug) {
10476478Sbharding 				(void) fprintf(stderr,
10486478Sbharding 				    "Clearing VTOC labels from NEW"
10496478Sbharding 				    " table\n");
10506478Sbharding 			}
10516478Sbharding 			clear_vtoc(NEW, new_pt);
10526478Sbharding 		}
10536478Sbharding 	}
10546478Sbharding 
10556478Sbharding 
10566478Sbharding 	/* see if the old table had EFI */
10576478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10586478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
10596478Sbharding 			clr_efi = old_pt;
10600Sstevel@tonic-gate 		}
10610Sstevel@tonic-gate 	}
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
10640Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10650Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
10660Sstevel@tonic-gate 			continue;
10670Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10685169Slclee 			if ((Old_Table[old_pt].systid ==
10695169Slclee 			    Table[new_pt].systid) &&
10705169Slclee 			    (Old_Table[old_pt].relsect ==
10715169Slclee 			    Table[new_pt].relsect) &&
10725169Slclee 			    (Old_Table[old_pt].numsect ==
10735169Slclee 			    Table[new_pt].numsect))
10745169Slclee 				break;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 		/*
10780Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
10790Sstevel@tonic-gate 		 * the EFI GPT
10800Sstevel@tonic-gate 		 */
10810Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
10820Sstevel@tonic-gate 			clr_efi = 0;
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 		break;
10850Sstevel@tonic-gate 	}
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 	/* clear labels if necessary */
10880Sstevel@tonic-gate 	if (clr_efi >= 0) {
10890Sstevel@tonic-gate 		if (io_debug) {
1090251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
10910Sstevel@tonic-gate 		}
10920Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
10930Sstevel@tonic-gate 			if (io_debug) {
1094251Slclee 				(void) fprintf(stderr,
1095251Slclee 				    "\tError %d clearing EFI labels"
10960Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
10970Sstevel@tonic-gate 				    error);
10980Sstevel@tonic-gate 			}
10990Sstevel@tonic-gate 		}
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1103251Slclee 		(void) fprintf(stderr,
11040Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
11050Sstevel@tonic-gate 		    Dfltdev);
11060Sstevel@tonic-gate 	}
11070Sstevel@tonic-gate 	if (errno == 0)
11080Sstevel@tonic-gate 		return;
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	/* write to disk drive */
11110Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1112251Slclee 		(void) fprintf(stderr,
11130Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
11140Sstevel@tonic-gate 		    Dfltdev);
11150Sstevel@tonic-gate 		exit(1);
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1118251Slclee 		(void) fprintf(stderr,
11190Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
11200Sstevel@tonic-gate 		    Dfltdev);
11210Sstevel@tonic-gate 		exit(1);
11220Sstevel@tonic-gate 	}
11230Sstevel@tonic-gate }
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * mboot_read
11270Sstevel@tonic-gate  * Read the prototype boot records from the files.
11280Sstevel@tonic-gate  */
1129251Slclee static void
1130251Slclee mboot_read(void)
11310Sstevel@tonic-gate {
11320Sstevel@tonic-gate 	int mDev, i;
11330Sstevel@tonic-gate 	struct ipart *part;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate #if defined(i386) || defined(sparc)
11360Sstevel@tonic-gate 	/*
11370Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
11380Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
11390Sstevel@tonic-gate 	 */
11400Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
11410Sstevel@tonic-gate 		/*
11420Sstevel@tonic-gate 		 * Bug ID 1249035:
11430Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
11440Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
11450Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
11460Sstevel@tonic-gate 		 */
11470Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/* First read in the master boot record */
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/* Open the master boot proto file */
11530Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1154251Slclee 		(void) fprintf(stderr,
11550Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
11560Sstevel@tonic-gate 		    io_mboot);
11570Sstevel@tonic-gate 		exit(1);
11580Sstevel@tonic-gate 	}
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	/* Read the master boot program */
11610Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
11620Sstevel@tonic-gate 	    (struct mboot)) {
1163251Slclee 		(void) fprintf(stderr,
11640Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
11650Sstevel@tonic-gate 		    io_mboot);
11660Sstevel@tonic-gate 		exit(1);
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	/* Is this really a master boot record? */
11708333SSuhasini.Peddada@Sun.COM 	if (les(BootCod.signature) != MBB_MAGIC) {
1171251Slclee 		(void) fprintf(stderr,
11720Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1173251Slclee 		(void) fprintf(stderr,
1174251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
11758333SSuhasini.Peddada@Sun.COM 		    les(BootCod.signature), MBB_MAGIC);
11760Sstevel@tonic-gate 		exit(1);
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
1179251Slclee 	(void) close(mDev);
11800Sstevel@tonic-gate #else
11810Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
11820Sstevel@tonic-gate #endif
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
11850Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
11860Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1187251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
11880Sstevel@tonic-gate 	}
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * fill_patt
11940Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
11950Sstevel@tonic-gate  */
1196251Slclee static void
1197251Slclee fill_patt(void)
11980Sstevel@tonic-gate {
1199251Slclee 	int	*buff_ptr, i;
12006549Sbharding 	off_t	*off_ptr;
12010Sstevel@tonic-gate 	int	io_fpatt = 0;
12020Sstevel@tonic-gate 	int	io_ipatt = 0;
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
12050Sstevel@tonic-gate 		io_fpatt++;
12060Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
12070Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
12080Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
12095169Slclee 			*buff_ptr = io_ipatt;
12100Sstevel@tonic-gate 	}
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 	/*
12130Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
12140Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
12150Sstevel@tonic-gate 	 * for io_size blocks.
12160Sstevel@tonic-gate 	 */
12170Sstevel@tonic-gate 	while (io_size--) {
12186549Sbharding 		off_ptr = (off_t *)Bootsect;
12190Sstevel@tonic-gate 		if (!io_fpatt) {
12206549Sbharding 			for (i = 0; i < sectsiz;
12216549Sbharding 			    i += sizeof (off_t), off_ptr++)
12226549Sbharding 				*off_ptr = io_offset;
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 		/* Write the data to disk */
12256549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
12266549Sbharding 		    SEEK_SET) == -1) {
1227251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12285169Slclee 			    Dfltdev);
12290Sstevel@tonic-gate 			exit(1);
12300Sstevel@tonic-gate 		}
12310Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1232251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12335169Slclee 			    Dfltdev);
12340Sstevel@tonic-gate 			exit(1);
12350Sstevel@tonic-gate 		}
12360Sstevel@tonic-gate 	} /* while (--io_size); */
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate  * abs_read
12410Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
12420Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
12430Sstevel@tonic-gate  */
1244251Slclee static void
1245251Slclee abs_read(void)
1246251Slclee {
12470Sstevel@tonic-gate 	int c;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	while (io_size--) {
12506549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
12516549Sbharding 		    SEEK_SET) == -1) {
1252251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12530Sstevel@tonic-gate 			    Dfltdev);
12540Sstevel@tonic-gate 			exit(1);
12550Sstevel@tonic-gate 		}
12560Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1257251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
12580Sstevel@tonic-gate 			    Dfltdev);
12590Sstevel@tonic-gate 			exit(1);
12600Sstevel@tonic-gate 		}
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 		/* Write to standard ouptut */
1263251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12640Sstevel@tonic-gate 			if (c >= 0) {
12650Sstevel@tonic-gate 				if (io_debug)
1266251Slclee 					(void) fprintf(stderr,
1267251Slclee 					    "fdisk: Output warning: %d of %d"
1268251Slclee 					    " characters written.\n",
1269251Slclee 					    c, sectsiz);
12700Sstevel@tonic-gate 				exit(2);
12710Sstevel@tonic-gate 			} else {
12720Sstevel@tonic-gate 				perror("write error on output file.");
12730Sstevel@tonic-gate 				exit(2);
12740Sstevel@tonic-gate 			}
12750Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
12760Sstevel@tonic-gate 			/* != sectsiz) */
12770Sstevel@tonic-gate 	} /* while (--io_size); */
12780Sstevel@tonic-gate 	exit(0);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate /*
12820Sstevel@tonic-gate  * abs_write
12830Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
12840Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
12850Sstevel@tonic-gate  */
1286251Slclee static void
1287251Slclee abs_write(void)
12880Sstevel@tonic-gate {
12890Sstevel@tonic-gate 	int c, i;
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	while (io_size--) {
12920Sstevel@tonic-gate 		int part_exit = 0;
12930Sstevel@tonic-gate 		/* Read from standard input */
12940Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12950Sstevel@tonic-gate 			if (c >= 0) {
12960Sstevel@tonic-gate 				if (io_debug)
1297251Slclee 				(void) fprintf(stderr,
12980Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
12990Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
13005169Slclee 				    c, sectsiz);
13010Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
13020Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
13030Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
13040Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
13050Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
13060Sstevel@tonic-gate 					Bootsect[i++] = 0;
13070Sstevel@tonic-gate 				}
13080Sstevel@tonic-gate 				part_exit++;
13090Sstevel@tonic-gate 			} else {
13100Sstevel@tonic-gate 				perror("read error on input file.");
13110Sstevel@tonic-gate 				exit(2);
13120Sstevel@tonic-gate 			}
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 		}
13150Sstevel@tonic-gate 		/* Write to disk drive */
13166549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
13176549Sbharding 		    SEEK_SET) == -1) {
1318251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
13190Sstevel@tonic-gate 			    Dfltdev);
13200Sstevel@tonic-gate 			exit(1);
13210Sstevel@tonic-gate 		}
13220Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1323251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
13240Sstevel@tonic-gate 			    Dfltdev);
13250Sstevel@tonic-gate 			exit(1);
13260Sstevel@tonic-gate 		}
13270Sstevel@tonic-gate 		if (part_exit)
13280Sstevel@tonic-gate 		exit(0);
13290Sstevel@tonic-gate 	} /* while (--io_size); */
13300Sstevel@tonic-gate 	exit(1);
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
1333251Slclee 
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate  * load
13360Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
13370Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
13380Sstevel@tonic-gate  */
13390Sstevel@tonic-gate 
1340251Slclee static void
1341251Slclee load(int funct, char *file)
13420Sstevel@tonic-gate {
13430Sstevel@tonic-gate 	int	id;
13440Sstevel@tonic-gate 	int	act;
13450Sstevel@tonic-gate 	int	bhead;
13460Sstevel@tonic-gate 	int	bsect;
13470Sstevel@tonic-gate 	int	bcyl;
13480Sstevel@tonic-gate 	int	ehead;
13490Sstevel@tonic-gate 	int	esect;
13500Sstevel@tonic-gate 	int	ecyl;
13517563SPrasad.Singamsetty@Sun.COM 	uint32_t	rsect;
13527563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
13530Sstevel@tonic-gate 	char	line[256];
13540Sstevel@tonic-gate 	int	i = 0;
13550Sstevel@tonic-gate 	int	j;
13560Sstevel@tonic-gate 	FILE *fp;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 	switch (funct) {
13590Sstevel@tonic-gate 
13605169Slclee 	case LOADFILE:
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 		/*
13630Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
13640Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
13650Sstevel@tonic-gate 		 * fdisk_file).
13660Sstevel@tonic-gate 		 */
13670Sstevel@tonic-gate 		nulltbl();
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		/* Open the prototype file */
13700Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
13710Sstevel@tonic-gate 			(void) fprintf(stderr,
13720Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
13730Sstevel@tonic-gate 			    file);
13740Sstevel@tonic-gate 			exit(1);
13750Sstevel@tonic-gate 		}
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		/* Read a line from the file */
13780Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
13790Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
13800Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
13810Sstevel@tonic-gate 				continue;
13820Sstevel@tonic-gate 			}
13830Sstevel@tonic-gate 
13840Sstevel@tonic-gate 			/*
13850Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
13860Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
13870Sstevel@tonic-gate 			 */
13880Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
13890Sstevel@tonic-gate 				(void) fprintf(stderr,
13900Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13910Sstevel@tonic-gate 				    line);
13920Sstevel@tonic-gate 				exit(1);
13930Sstevel@tonic-gate 			}
13948904SBarry.Harding@Sun.COM 
13958904SBarry.Harding@Sun.COM 			if (entry_from_old_table(id, act, bhead, bsect,
13968904SBarry.Harding@Sun.COM 			    bcyl, ehead, esect, ecyl, rsect, numsect)) {
13978904SBarry.Harding@Sun.COM 				/*
13988904SBarry.Harding@Sun.COM 				 * If we got here it means we copied an
13998904SBarry.Harding@Sun.COM 				 * unmodified entry. So there is no need
14008904SBarry.Harding@Sun.COM 				 * to insert it in the table or do any
14018904SBarry.Harding@Sun.COM 				 * checks against disk size.
14028904SBarry.Harding@Sun.COM 				 *
14038904SBarry.Harding@Sun.COM 				 * This is a work around on the following
14048904SBarry.Harding@Sun.COM 				 * situation (for IDE disks, at least):
14058904SBarry.Harding@Sun.COM 				 * Different operation systems calculate
14068904SBarry.Harding@Sun.COM 				 * disk size different ways, of which there
14078904SBarry.Harding@Sun.COM 				 * are two main ways.
14088904SBarry.Harding@Sun.COM 				 *
14098904SBarry.Harding@Sun.COM 				 * The first, rounds the disk size to modulo
14108904SBarry.Harding@Sun.COM 				 * cylinder size (virtual made-up cylinder
14118904SBarry.Harding@Sun.COM 				 * usually based on maximum number of heads
14128904SBarry.Harding@Sun.COM 				 * and sectors in partition table fields).
14138904SBarry.Harding@Sun.COM 				 * Our OS's (for IDE) and most other "Unix"
14148904SBarry.Harding@Sun.COM 				 * type OS's do this.
14158904SBarry.Harding@Sun.COM 				 *
14168904SBarry.Harding@Sun.COM 				 * The second, uses every single block
14178904SBarry.Harding@Sun.COM 				 * on the disk (to maximize available space).
14188904SBarry.Harding@Sun.COM 				 * Since disk manufactures do not know about
14198904SBarry.Harding@Sun.COM 				 * "virtual cylinders", there are some number
14208904SBarry.Harding@Sun.COM 				 * of blocks that make up a partial cylinder
14218904SBarry.Harding@Sun.COM 				 * at the end of the disk.
14228904SBarry.Harding@Sun.COM 				 *
14238904SBarry.Harding@Sun.COM 				 * The difference between these two methods
14248904SBarry.Harding@Sun.COM 				 * is where the problem is. When one
14258904SBarry.Harding@Sun.COM 				 * tries to install Solaris/OpenSolaris on
14268904SBarry.Harding@Sun.COM 				 * a disk that has another OS using that
14278904SBarry.Harding@Sun.COM 				 * "partial cylinder", install fails. It fails
14288904SBarry.Harding@Sun.COM 				 * since fdisk thinks its asked to create a
14298904SBarry.Harding@Sun.COM 				 * partition with the -F option that contains
14308904SBarry.Harding@Sun.COM 				 * a partition that runs off the end of the
14318904SBarry.Harding@Sun.COM 				 * disk.
14328904SBarry.Harding@Sun.COM 				 */
14338904SBarry.Harding@Sun.COM 				continue;
14348904SBarry.Harding@Sun.COM 			}
14358904SBarry.Harding@Sun.COM 
14360Sstevel@tonic-gate 			/*
14370Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
14380Sstevel@tonic-gate 			 * in table
14390Sstevel@tonic-gate 			 */
14400Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
14410Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
14420Sstevel@tonic-gate 				(void) fprintf(stderr,
14430Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
14440Sstevel@tonic-gate 				    line);
14450Sstevel@tonic-gate 				exit(1);
14460Sstevel@tonic-gate 			}
14470Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1450251Slclee 			(void) fprintf(stderr,
14510Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
14520Sstevel@tonic-gate 			exit(1);
14530Sstevel@tonic-gate 		}
14540Sstevel@tonic-gate 
1455251Slclee 		(void) fclose(fp);
14560Sstevel@tonic-gate 		return;
14570Sstevel@tonic-gate 
14585169Slclee 	case LOADDEL:
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
1461251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
1462251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
1463251Slclee 			(void) fprintf(stderr,
1464251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1465251Slclee 			exit(1);
1466251Slclee 		}
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 		/* Find the exact entry in the table */
14690Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
14700Sstevel@tonic-gate 			if (Table[i].systid == id &&
14710Sstevel@tonic-gate 			    Table[i].bootid == act &&
14720Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
14730Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
14745169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
1475251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
14760Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
14770Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
14785169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
1479251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
14808333SSuhasini.Peddada@Sun.COM 			    Table[i].relsect == lel(rsect) &&
14818333SSuhasini.Peddada@Sun.COM 			    Table[i].numsect == lel(numsect)) {
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 				/*
14840Sstevel@tonic-gate 				 * Found the entry. Now move rest of
14850Sstevel@tonic-gate 				 * entries up toward the top of the
14860Sstevel@tonic-gate 				 * table, leaving available entries at
14870Sstevel@tonic-gate 				 * the end of the fdisk table.
14880Sstevel@tonic-gate 				 */
1489251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
1490251Slclee 					Table[j].systid = Table[j + 1].systid;
1491251Slclee 					Table[j].bootid = Table[j + 1].bootid;
1492251Slclee 					Table[j].beghead = Table[j + 1].beghead;
1493251Slclee 					Table[j].begsect = Table[j + 1].begsect;
1494251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
1495251Slclee 					Table[j].endhead = Table[j + 1].endhead;
1496251Slclee 					Table[j].endsect = Table[j + 1].endsect;
1497251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
1498251Slclee 					Table[j].relsect = Table[j + 1].relsect;
1499251Slclee 					Table[j].numsect = Table[j + 1].numsect;
15000Sstevel@tonic-gate 				}
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 				/*
15030Sstevel@tonic-gate 				 * Mark the last entry as unused in case
15040Sstevel@tonic-gate 				 * all table entries were in use prior
15050Sstevel@tonic-gate 				 * to the deletion.
15060Sstevel@tonic-gate 				 */
15070Sstevel@tonic-gate 
1508251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
1509251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
15100Sstevel@tonic-gate 				return;
15110Sstevel@tonic-gate 			}
15120Sstevel@tonic-gate 		}
1513251Slclee 		(void) fprintf(stderr,
15140Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
15150Sstevel@tonic-gate 		    "	\"%s\"\n",
15160Sstevel@tonic-gate 		    file);
15170Sstevel@tonic-gate 		exit(1);
15188333SSuhasini.Peddada@Sun.COM 		/* FALLTHRU */
15190Sstevel@tonic-gate 
15205169Slclee 	case LOADADD:
15210Sstevel@tonic-gate 
15220Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
1523251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
1524251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
1525251Slclee 			(void) fprintf(stderr,
1526251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1527251Slclee 			exit(1);
1528251Slclee 		}
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
15310Sstevel@tonic-gate 		if (rsect == 0) {
15320Sstevel@tonic-gate 			(void) fprintf(stderr,
15330Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
15340Sstevel@tonic-gate 			    "   \"%s\".\n",
15350Sstevel@tonic-gate 			    file);
15360Sstevel@tonic-gate 			exit(1);
15370Sstevel@tonic-gate 		}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 		/*
15400Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
15410Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
15420Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
15430Sstevel@tonic-gate 		 */
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 		if (id == EFI_PMBR) {
15460Sstevel@tonic-gate 			if (rsect != 1) {
15470Sstevel@tonic-gate 				(void) fprintf(stderr,
15480Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
15490Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
15500Sstevel@tonic-gate 				exit(1);
15510Sstevel@tonic-gate 			}
15520Sstevel@tonic-gate 
15537563SPrasad.Singamsetty@Sun.COM 
15547563SPrasad.Singamsetty@Sun.COM 			if (dev_capacity > DK_MAX_2TB) {
15557563SPrasad.Singamsetty@Sun.COM 				if (numsect != DK_MAX_2TB) {
15567563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr,
15577563SPrasad.Singamsetty@Sun.COM 					    "fdisk: EFI partitions must "
15587563SPrasad.Singamsetty@Sun.COM 					    "encompass the entire maximum 2 TB "
15597563SPrasad.Singamsetty@Sun.COM 					    "(input numsect: %u - max: %llu)\n",
15607563SPrasad.Singamsetty@Sun.COM 					    numsect, (diskaddr_t)DK_MAX_2TB);
15617563SPrasad.Singamsetty@Sun.COM 				exit(1);
15627563SPrasad.Singamsetty@Sun.COM 				}
15637563SPrasad.Singamsetty@Sun.COM 			} else if (numsect != dev_capacity - 1) {
15640Sstevel@tonic-gate 				(void) fprintf(stderr,
15650Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
1566251Slclee 				    "entire disk\n"
15677563SPrasad.Singamsetty@Sun.COM 				    "(input numsect: %u - avail: %llu)\n",
1568251Slclee 				    numsect,
15695169Slclee 				    dev_capacity - 1);
15700Sstevel@tonic-gate 				exit(1);
15710Sstevel@tonic-gate 			}
15720Sstevel@tonic-gate 		}
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
15750Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
15760Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
15770Sstevel@tonic-gate 			(void) fprintf(stderr,
15780Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
15790Sstevel@tonic-gate 			    "	\"%s\"\n",
15800Sstevel@tonic-gate 			    file);
15810Sstevel@tonic-gate 			exit(1);
15820Sstevel@tonic-gate 		}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
15850Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1586251Slclee 			(void) fprintf(stderr,
1587251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
15880Sstevel@tonic-gate 			exit(1);
15890Sstevel@tonic-gate 		}
15900Sstevel@tonic-gate 	} /* switch funct */
15910Sstevel@tonic-gate }
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate /*
15940Sstevel@tonic-gate  * Set_Table_CHS_Values
15950Sstevel@tonic-gate  *
15960Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
15970Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
15980Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
15990Sstevel@tonic-gate  *
16000Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
16010Sstevel@tonic-gate  *
16020Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
16030Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
16040Sstevel@tonic-gate  */
16050Sstevel@tonic-gate static void
16060Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
16070Sstevel@tonic-gate {
16080Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
16110Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
16120Sstevel@tonic-gate 		/*
16130Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
16140Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
16150Sstevel@tonic-gate 		 */
16160Sstevel@tonic-gate 		cy = MAX_CYL + 1;
16170Sstevel@tonic-gate 		hd = MAX_HEAD;
16180Sstevel@tonic-gate 		sc = MAX_SECT;
16190Sstevel@tonic-gate 	} else {
16200Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
16210Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
16220Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
16230Sstevel@tonic-gate 	}
16240Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
1625251Slclee 	Table[ti].beghead = (uchar_t)hd;
1626251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	/*
16290Sstevel@tonic-gate 	 * This code is identical to the code above
16300Sstevel@tonic-gate 	 * except that it works on ending CHS values
16310Sstevel@tonic-gate 	 */
16320Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
16330Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
16340Sstevel@tonic-gate 		cy = MAX_CYL + 1;
16350Sstevel@tonic-gate 		hd = MAX_HEAD;
16360Sstevel@tonic-gate 		sc = MAX_SECT;
16370Sstevel@tonic-gate 	} else {
16380Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
16390Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
16400Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
1643251Slclee 	Table[ti].endhead = (uchar_t)hd;
1644251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * insert_tbl
16490Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
16500Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
16510Sstevel@tonic-gate  *	entries!
16520Sstevel@tonic-gate  */
1653251Slclee static int
1654251Slclee insert_tbl(
1655251Slclee     int id, int act,
1656251Slclee     int bhead, int bsect, int bcyl,
1657251Slclee     int ehead, int esect, int ecyl,
16587563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect)
16590Sstevel@tonic-gate {
16600Sstevel@tonic-gate 	int	i;
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	/* validate partition size */
16637563SPrasad.Singamsetty@Sun.COM 	if (((diskaddr_t)rsect + numsect) > dev_capacity) {
1664251Slclee 		(void) fprintf(stderr,
16650Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
16660Sstevel@tonic-gate 		return (-1);
16670Sstevel@tonic-gate 	}
16680Sstevel@tonic-gate 
16697563SPrasad.Singamsetty@Sun.COM 
16700Sstevel@tonic-gate 	/* find UNUSED partition table entry */
16710Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
16720Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
16730Sstevel@tonic-gate 			break;
16740Sstevel@tonic-gate 		}
16750Sstevel@tonic-gate 	}
16760Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
1677251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
16780Sstevel@tonic-gate 		return (-1);
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 
1682251Slclee 	Table[i].systid = (uchar_t)id;
1683251Slclee 	Table[i].bootid = (uchar_t)act;
16848333SSuhasini.Peddada@Sun.COM 	Table[i].numsect = lel(numsect);
16858333SSuhasini.Peddada@Sun.COM 	Table[i].relsect = lel(rsect);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	/*
16880Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
16890Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
16900Sstevel@tonic-gate 	 */
16910Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
16920Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
16930Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
16940Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
16950Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
16960Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
16970Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
16980Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
16990Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
17000Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
1701251Slclee 		Table[i].beghead = (uchar_t)bhead;
1702251Slclee 		Table[i].endhead = (uchar_t)ehead;
1703251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
17040Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
17050Sstevel@tonic-gate 	} else {
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 		/*
17080Sstevel@tonic-gate 		 * The specified values are invalid,
17090Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
17100Sstevel@tonic-gate 		 */
17110Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	/*
17150Sstevel@tonic-gate 	 * return partition index
17160Sstevel@tonic-gate 	 */
17170Sstevel@tonic-gate 	return (i);
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate /*
17218904SBarry.Harding@Sun.COM  * entry_from_old_table
17228904SBarry.Harding@Sun.COM  *	If the specified entry is in the old table and is not a Solaris entry
17238904SBarry.Harding@Sun.COM  *	then insert same entry into new fdisk table. If we do this then
17248904SBarry.Harding@Sun.COM  *	all checks are skipped for that entry!
17258904SBarry.Harding@Sun.COM  */
17268904SBarry.Harding@Sun.COM static int
17278904SBarry.Harding@Sun.COM entry_from_old_table(
17288904SBarry.Harding@Sun.COM     int id, int act,
17298904SBarry.Harding@Sun.COM     int bhead, int bsect, int bcyl,
17308904SBarry.Harding@Sun.COM     int ehead, int esect, int ecyl,
17318904SBarry.Harding@Sun.COM     uint32_t rsect, uint32_t numsect)
17328904SBarry.Harding@Sun.COM {
17338904SBarry.Harding@Sun.COM 	uint32_t	i, j;
17348904SBarry.Harding@Sun.COM 
17358904SBarry.Harding@Sun.COM 	if (id == SUNIXOS || id == SUNIXOS2)
17368904SBarry.Harding@Sun.COM 		return (0);
17378904SBarry.Harding@Sun.COM 	for (i = 0; i < FD_NUMPART - 1; i++) {
17388904SBarry.Harding@Sun.COM 		if (Old_Table[i].systid == id &&
17398904SBarry.Harding@Sun.COM 		    Old_Table[i].bootid == act &&
17408904SBarry.Harding@Sun.COM 		    Old_Table[i].beghead == bhead &&
17418904SBarry.Harding@Sun.COM 		    Old_Table[i].begsect == ((bsect & 0x3f) |
17428904SBarry.Harding@Sun.COM 		    (uchar_t)((bcyl>>2) & 0xc0)) &&
17438904SBarry.Harding@Sun.COM 		    Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
17448904SBarry.Harding@Sun.COM 		    Old_Table[i].endhead == ehead &&
17458904SBarry.Harding@Sun.COM 		    Old_Table[i].endsect == ((esect & 0x3f) |
17468904SBarry.Harding@Sun.COM 		    (uchar_t)((ecyl>>2) & 0xc0)) &&
17478904SBarry.Harding@Sun.COM 		    Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
17488904SBarry.Harding@Sun.COM 		    Old_Table[i].relsect == lel(rsect) &&
17498904SBarry.Harding@Sun.COM 		    Old_Table[i].numsect == lel(numsect)) {
17508904SBarry.Harding@Sun.COM 			/* find UNUSED partition table entry */
17518904SBarry.Harding@Sun.COM 			for (j = 0; j < FD_NUMPART; j++) {
17528904SBarry.Harding@Sun.COM 				if (Table[j].systid == UNUSED) {
17538904SBarry.Harding@Sun.COM 					(void) memcpy(&Table[j], &Old_Table[i],
17548904SBarry.Harding@Sun.COM 					    sizeof (Table[0]));
17558904SBarry.Harding@Sun.COM 					skip_verify[j] = 1;
17568904SBarry.Harding@Sun.COM 					return (1);
17578904SBarry.Harding@Sun.COM 
17588904SBarry.Harding@Sun.COM 				}
17598904SBarry.Harding@Sun.COM 			}
17608904SBarry.Harding@Sun.COM 			return (0);
17618904SBarry.Harding@Sun.COM 		}
17628904SBarry.Harding@Sun.COM 
17638904SBarry.Harding@Sun.COM 	}
17648904SBarry.Harding@Sun.COM 	return (0);
17658904SBarry.Harding@Sun.COM }
17668904SBarry.Harding@Sun.COM 
17678904SBarry.Harding@Sun.COM /*
17680Sstevel@tonic-gate  * verify_tbl
17690Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
17700Sstevel@tonic-gate  * the disk.
17710Sstevel@tonic-gate  */
1772251Slclee static int
1773251Slclee verify_tbl(void)
17740Sstevel@tonic-gate {
17757563SPrasad.Singamsetty@Sun.COM 	uint32_t	i, j, rsect, numsect;
17760Sstevel@tonic-gate 	int	noMoreParts = 0;
17770Sstevel@tonic-gate 	int	numParts = 0;
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
1780251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
17810Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
17820Sstevel@tonic-gate 			numParts++;
17830Sstevel@tonic-gate 			/*
17840Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
17850Sstevel@tonic-gate 			 * EFI_PMBR part
17860Sstevel@tonic-gate 			 */
17870Sstevel@tonic-gate 			if (noMoreParts) {
17880Sstevel@tonic-gate 				return (-1);
17890Sstevel@tonic-gate 			}
17900Sstevel@tonic-gate 
17910Sstevel@tonic-gate 			/*
17920Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
17930Sstevel@tonic-gate 			 * and must be Table entry 0
17940Sstevel@tonic-gate 			 */
17950Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
17960Sstevel@tonic-gate 				if (i == 0) {
17970Sstevel@tonic-gate 					noMoreParts = 1;
17980Sstevel@tonic-gate 				} else {
17990Sstevel@tonic-gate 					return (-1);
18000Sstevel@tonic-gate 				}
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
1803251Slclee 					(void) fprintf(stderr, "ERROR: "
18040Sstevel@tonic-gate 					    "Invalid starting sector "
18050Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
18060Sstevel@tonic-gate 					    "relsect %d "
18070Sstevel@tonic-gate 					    "(should be 1)\n",
18080Sstevel@tonic-gate 					    Table[i].relsect);
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate 					return (-1);
18110Sstevel@tonic-gate 				}
18120Sstevel@tonic-gate 
18135169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
1814251Slclee 					(void) fprintf(stderr, "ERROR: "
18150Sstevel@tonic-gate 					    "EFI_PMBR partition must "
18160Sstevel@tonic-gate 					    "encompass the entire "
1817251Slclee 					    "disk.\n numsect %d - "
18185169Slclee 					    "actual %llu\n",
18190Sstevel@tonic-gate 					    Table[i].numsect,
18205169Slclee 					    dev_capacity - 1);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 					return (-1);
18230Sstevel@tonic-gate 				}
18240Sstevel@tonic-gate 			}
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
18278333SSuhasini.Peddada@Sun.COM 			rsect = lel(Table[i].relsect);
18288333SSuhasini.Peddada@Sun.COM 			numsect = lel(Table[i].numsect);
18297563SPrasad.Singamsetty@Sun.COM 
18307563SPrasad.Singamsetty@Sun.COM 			if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
18317563SPrasad.Singamsetty@Sun.COM 			    (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
18328904SBarry.Harding@Sun.COM 				if (!skip_verify[i])
18338904SBarry.Harding@Sun.COM 					return (-1);
18340Sstevel@tonic-gate 			}
18350Sstevel@tonic-gate 
1836251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
18370Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
18387563SPrasad.Singamsetty@Sun.COM 					uint32_t t_relsect =
18398333SSuhasini.Peddada@Sun.COM 					    lel(Table[j].relsect);
18407563SPrasad.Singamsetty@Sun.COM 					uint32_t t_numsect =
18418333SSuhasini.Peddada@Sun.COM 					    lel(Table[j].numsect);
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 					if (noMoreParts) {
1844251Slclee 						(void) fprintf(stderr,
18450Sstevel@tonic-gate 						    "Cannot add partition to "
18460Sstevel@tonic-gate 						    "table; no more partitions "
18470Sstevel@tonic-gate 						    "allowed\n");
18480Sstevel@tonic-gate 
18490Sstevel@tonic-gate 						if (io_debug) {
1850251Slclee 							(void) fprintf(stderr,
18510Sstevel@tonic-gate 							    "DEBUG: Current "
18520Sstevel@tonic-gate 							    "partition:\t"
18530Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1854251Slclee 							    "%d:%d:%d:%d:%d\n"
18550Sstevel@tonic-gate 							    "       Next "
18560Sstevel@tonic-gate 							    "partition:\t\t"
18570Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1858251Slclee 							    "%d:%d:%d:%d:%d\n",
18590Sstevel@tonic-gate 							    Table[i].systid,
18600Sstevel@tonic-gate 							    Table[i].bootid,
18610Sstevel@tonic-gate 							    Table[i].begcyl,
18620Sstevel@tonic-gate 							    Table[i].beghead,
18630Sstevel@tonic-gate 							    Table[i].begsect,
18640Sstevel@tonic-gate 							    Table[i].endcyl,
18650Sstevel@tonic-gate 							    Table[i].endhead,
18660Sstevel@tonic-gate 							    Table[i].endsect,
18670Sstevel@tonic-gate 							    Table[i].relsect,
18680Sstevel@tonic-gate 							    Table[i].numsect,
18690Sstevel@tonic-gate 							    Table[j].systid,
18700Sstevel@tonic-gate 							    Table[j].bootid,
18710Sstevel@tonic-gate 							    Table[j].begcyl,
18720Sstevel@tonic-gate 							    Table[j].beghead,
18730Sstevel@tonic-gate 							    Table[j].begsect,
18740Sstevel@tonic-gate 							    Table[j].endcyl,
18750Sstevel@tonic-gate 							    Table[j].endhead,
18760Sstevel@tonic-gate 							    Table[j].endsect,
18770Sstevel@tonic-gate 							    Table[j].relsect,
18780Sstevel@tonic-gate 							    Table[j].numsect);
18790Sstevel@tonic-gate 						}
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 						return (-1);
18820Sstevel@tonic-gate 					}
18830Sstevel@tonic-gate 					if ((rsect >=
18840Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
1885251Slclee 					    ((rsect + numsect) <= t_relsect)) {
18860Sstevel@tonic-gate 						continue;
18870Sstevel@tonic-gate 					} else {
1888251Slclee 						(void) fprintf(stderr, "ERROR: "
18890Sstevel@tonic-gate 						    "current partition overlaps"
18900Sstevel@tonic-gate 						    " following partition\n");
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate 						return (-1);
18930Sstevel@tonic-gate 					}
18940Sstevel@tonic-gate 				}
18950Sstevel@tonic-gate 			}
18960Sstevel@tonic-gate 		} else {
18970Sstevel@tonic-gate 			noMoreParts = 1;
18980Sstevel@tonic-gate 		}
18990Sstevel@tonic-gate 	}
19000Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
19018904SBarry.Harding@Sun.COM 		if (noMoreParts)
19028904SBarry.Harding@Sun.COM 			return (-1);
19038904SBarry.Harding@Sun.COM 		if (!skip_verify[i] &&
19048904SBarry.Harding@Sun.COM 		    ((((diskaddr_t)lel(Table[i].relsect) +
19058333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > dev_capacity) ||
19068333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
19078904SBarry.Harding@Sun.COM 		    lel(Table[i].numsect)) > DK_MAX_2TB))) {
19080Sstevel@tonic-gate 			return (-1);
19090Sstevel@tonic-gate 		}
19100Sstevel@tonic-gate 	}
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	return (numParts);
19130Sstevel@tonic-gate }
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate /*
19160Sstevel@tonic-gate  * pars_fdisk
19170Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
19180Sstevel@tonic-gate  * (-A, -D, -F).
19190Sstevel@tonic-gate  */
1920251Slclee static int
1921251Slclee pars_fdisk(
1922251Slclee     char *line,
1923251Slclee     int *id, int *act,
1924251Slclee     int *bhead, int *bsect, int *bcyl,
1925251Slclee     int *ehead, int *esect, int *ecyl,
19267563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect)
19270Sstevel@tonic-gate {
19280Sstevel@tonic-gate 	int	i;
19290Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
19305169Slclee 		return (1);
19310Sstevel@tonic-gate 	line[strlen(line)] = '\0';
19320Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
19330Sstevel@tonic-gate 		if (line[i] == '\0') {
19340Sstevel@tonic-gate 			break;
19350Sstevel@tonic-gate 		} else if (line[i] == ':') {
19360Sstevel@tonic-gate 			line[i] = ' ';
19370Sstevel@tonic-gate 		}
19380Sstevel@tonic-gate 	}
19397563SPrasad.Singamsetty@Sun.COM 	if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
19400Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
19410Sstevel@tonic-gate 	    rsect, numsect) != 10) {
19420Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
19430Sstevel@tonic-gate 		exit(1);
19440Sstevel@tonic-gate 	}
19450Sstevel@tonic-gate 	return (0);
19460Sstevel@tonic-gate }
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate /*
19490Sstevel@tonic-gate  * validate_part
19500Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
19510Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
19520Sstevel@tonic-gate  */
1953251Slclee static int
19547563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect)
19550Sstevel@tonic-gate {
19560Sstevel@tonic-gate 	int i;
19570Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
19580Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
19590Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
19608333SSuhasini.Peddada@Sun.COM 			    (Old_Table[i].relsect == lel(rsect)) &&
19618333SSuhasini.Peddada@Sun.COM 			    (Old_Table[i].numsect == lel(numsect)))
19625169Slclee 				return (0);
19630Sstevel@tonic-gate 		}
1964251Slclee 		(void) fprintf(stderr,
1965251Slclee 		    "New partition cannot start at sector 0\n");
19660Sstevel@tonic-gate 		return (-1);
19670Sstevel@tonic-gate 	}
19680Sstevel@tonic-gate 	return (0);
19690Sstevel@tonic-gate }
19700Sstevel@tonic-gate 
19710Sstevel@tonic-gate /*
19720Sstevel@tonic-gate  * stage0
19730Sstevel@tonic-gate  * Print out interactive menu and process user input.
19740Sstevel@tonic-gate  */
1975251Slclee static void
1976251Slclee stage0(void)
19770Sstevel@tonic-gate {
1978251Slclee 	dispmenu();
1979251Slclee 	for (;;) {
1980251Slclee 		(void) printf(Q_LINE);
1981251Slclee 		(void) printf("Enter Selection: ");
19829786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
19830Sstevel@tonic-gate 		rm_blanks(s);
19849786SBarry.Harding@Sun.COM 		while (!((s[0] > '0') && (s[0] < '7') &&
19859786SBarry.Harding@Sun.COM 		    ((s[1] == '\0') || (s[1] == '\n')))) {
1986251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
1987251Slclee 			(void) printf(
1988251Slclee 			    "Enter a one-digit number between 1 and 6.");
1989251Slclee 			(void) printf(Q_LINE);
1990251Slclee 			(void) printf("Enter Selection: ");
19919786SBarry.Harding@Sun.COM 			(void) fgets(s, sizeof (s), stdin);
19920Sstevel@tonic-gate 			rm_blanks(s);
19930Sstevel@tonic-gate 		}
1994251Slclee 		(void) printf(E_LINE);
19950Sstevel@tonic-gate 		switch (s[0]) {
19960Sstevel@tonic-gate 			case '1':
19970Sstevel@tonic-gate 				if (pcreate() == -1)
19980Sstevel@tonic-gate 					return;
19990Sstevel@tonic-gate 				break;
20000Sstevel@tonic-gate 			case '2':
20010Sstevel@tonic-gate 				if (pchange() == -1)
20020Sstevel@tonic-gate 					return;
20030Sstevel@tonic-gate 				break;
20040Sstevel@tonic-gate 			case '3':
20050Sstevel@tonic-gate 				if (pdelete() == -1)
20060Sstevel@tonic-gate 					return;
20070Sstevel@tonic-gate 				break;
20080Sstevel@tonic-gate 			case '4':
20090Sstevel@tonic-gate 				if (ppartid() == -1)
20100Sstevel@tonic-gate 					return;
20110Sstevel@tonic-gate 				break;
20120Sstevel@tonic-gate 			case '5':
20130Sstevel@tonic-gate 				/* update disk partition table, if changed */
20140Sstevel@tonic-gate 				if (TableChanged() == 1) {
20150Sstevel@tonic-gate 					copy_Table_to_Bootblk();
20160Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
20170Sstevel@tonic-gate 				}
20180Sstevel@tonic-gate 				/*
20190Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
20200Sstevel@tonic-gate 				 * (truncate only)
20210Sstevel@tonic-gate 				 */
20220Sstevel@tonic-gate 				if (io_adjt) {
20230Sstevel@tonic-gate 					fix_slice();
20240Sstevel@tonic-gate 				}
2025251Slclee 				(void) close(Dev);
20260Sstevel@tonic-gate 				exit(0);
2027251Slclee 				/* FALLTHRU */
20280Sstevel@tonic-gate 			case '6':
20290Sstevel@tonic-gate 				/*
20300Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
20310Sstevel@tonic-gate 				 * (truncate only)
20320Sstevel@tonic-gate 				 */
20330Sstevel@tonic-gate 				if (io_adjt) {
20340Sstevel@tonic-gate 					fix_slice();
20350Sstevel@tonic-gate 				}
2036251Slclee 				(void) close(Dev);
20370Sstevel@tonic-gate 				exit(0);
2038251Slclee 				/* FALLTHRU */
20390Sstevel@tonic-gate 			default:
20400Sstevel@tonic-gate 				break;
20410Sstevel@tonic-gate 		}
20420Sstevel@tonic-gate 		copy_Table_to_Bootblk();
20430Sstevel@tonic-gate 		disptbl();
2044251Slclee 		dispmenu();
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate }
20470Sstevel@tonic-gate 
20480Sstevel@tonic-gate /*
20490Sstevel@tonic-gate  * pcreate
20500Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
20510Sstevel@tonic-gate  */
2052251Slclee static int
2053251Slclee pcreate(void)
20540Sstevel@tonic-gate {
2055251Slclee 	uchar_t tsystid = 'z';
20560Sstevel@tonic-gate 	int i, j;
20577563SPrasad.Singamsetty@Sun.COM 	uint32_t numsect;
20580Sstevel@tonic-gate 	int retCode = 0;
20590Sstevel@tonic-gate 
20600Sstevel@tonic-gate 	i = 0;
2061251Slclee 	for (;;) {
20620Sstevel@tonic-gate 		if (i == FD_NUMPART) {
2063251Slclee 			(void) printf(E_LINE);
2064251Slclee 			(void) printf(
2065251Slclee 			    "The partition table is full!\n"
2066251Slclee 			    "You must delete a partition before creating"
20670Sstevel@tonic-gate 			    " a new one.\n");
20680Sstevel@tonic-gate 			return (-1);
20690Sstevel@tonic-gate 		}
20700Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
20710Sstevel@tonic-gate 			break;
20720Sstevel@tonic-gate 		}
20730Sstevel@tonic-gate 		i++;
20740Sstevel@tonic-gate 	}
20750Sstevel@tonic-gate 
20767563SPrasad.Singamsetty@Sun.COM 	numsect = 0;
20770Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
20780Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
20798333SSuhasini.Peddada@Sun.COM 			numsect += lel(Table[i].numsect);
20800Sstevel@tonic-gate 		}
20817563SPrasad.Singamsetty@Sun.COM 		if (numsect >= chs_capacity) {
2082251Slclee 			(void) printf(E_LINE);
2083251Slclee 			(void) printf("There is no more room on the disk for"
20840Sstevel@tonic-gate 			    " another partition.\n");
2085251Slclee 			(void) printf(
2086251Slclee 			    "You must delete a partition before creating"
20870Sstevel@tonic-gate 			    " a new one.\n");
20880Sstevel@tonic-gate 			return (-1);
20890Sstevel@tonic-gate 		}
20900Sstevel@tonic-gate 	}
20910Sstevel@tonic-gate 	while (tsystid == 'z') {
20927563SPrasad.Singamsetty@Sun.COM 
20937563SPrasad.Singamsetty@Sun.COM 		/*
20947563SPrasad.Singamsetty@Sun.COM 		 * The question here is expanding to more than what is
20957563SPrasad.Singamsetty@Sun.COM 		 * allocated for question lines (Q_LINE) which garbles
20967563SPrasad.Singamsetty@Sun.COM 		 * at least warning line. Clearing warning line as workaround
20977563SPrasad.Singamsetty@Sun.COM 		 * for now.
20987563SPrasad.Singamsetty@Sun.COM 		 */
20997563SPrasad.Singamsetty@Sun.COM 
21007563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
2101251Slclee 		(void) printf(Q_LINE);
2102251Slclee 		(void) printf(
2103251Slclee 		    "Select the partition type to create:\n"
2104251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
2105251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
2106251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
2107251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
21089786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
21090Sstevel@tonic-gate 		rm_blanks(s);
21109786SBarry.Harding@Sun.COM 		if ((s[1] != '\0') && (s[1] != '\n')) {
2111251Slclee 			(void) printf(E_LINE);
2112251Slclee 			(void) printf("Invalid selection, try again.");
21130Sstevel@tonic-gate 			continue;
21140Sstevel@tonic-gate 		}
21150Sstevel@tonic-gate 		switch (s[0]) {
21160Sstevel@tonic-gate 		case '0':		/* exit */
21175169Slclee 			(void) printf(E_LINE);
21185169Slclee 			return (-1);
21190Sstevel@tonic-gate 		case '1':		/* Solaris partition */
21205169Slclee 			tsystid = SUNIXOS2;
21215169Slclee 			break;
21220Sstevel@tonic-gate 		case '2':		/* UNIX partition */
21235169Slclee 			tsystid = UNIXOS;
21245169Slclee 			break;
21250Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
21265169Slclee 			tsystid = PCIXOS;
21275169Slclee 			break;
21280Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
21295169Slclee 			tsystid = OTHEROS;
21305169Slclee 			break;
21310Sstevel@tonic-gate 		case '5':
21325169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
21335169Slclee 			break;
21340Sstevel@tonic-gate 		case '6':
21355169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
21365169Slclee 			break;
21370Sstevel@tonic-gate 		case '7':
21385169Slclee 			tsystid = EXTDOS;
21395169Slclee 			break;
21400Sstevel@tonic-gate 		case '8':
21415169Slclee 			tsystid = DOSHUGE;
21425169Slclee 			break;
21430Sstevel@tonic-gate 		case '9':
21445169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
21455169Slclee 			break;
21460Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
21470Sstevel@tonic-gate 		case 'A':
21485169Slclee 			tsystid = X86BOOT;
21495169Slclee 			break;
21500Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
21510Sstevel@tonic-gate 		case 'B':
21525169Slclee 			tsystid = DIAGPART;
21535169Slclee 			break;
21540Sstevel@tonic-gate 		case 'c':		/* FAT32 */
21550Sstevel@tonic-gate 		case 'C':
21565169Slclee 			tsystid = FDISK_WINDOWS;
21575169Slclee 			break;
21580Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
21590Sstevel@tonic-gate 		case 'D':
21605169Slclee 			tsystid = FDISK_EXT_WIN;
21615169Slclee 			break;
21620Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
21630Sstevel@tonic-gate 		case 'E':
21645169Slclee 			tsystid = FDISK_EXTLBA;
21655169Slclee 			break;
21660Sstevel@tonic-gate 		case 'f':
21670Sstevel@tonic-gate 		case 'F':
21685169Slclee 			tsystid = EFI_PMBR;
21695169Slclee 			break;
21700Sstevel@tonic-gate 		default:
21715169Slclee 			(void) printf(E_LINE);
21725169Slclee 			(void) printf("Invalid selection, try again.");
21735169Slclee 			continue;
21740Sstevel@tonic-gate 		}
21750Sstevel@tonic-gate 	}
21760Sstevel@tonic-gate 
2177251Slclee 	(void) printf(E_LINE);
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
21807563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
21817563SPrasad.Singamsetty@Sun.COM 		if ((dev_capacity > DK_MAX_2TB))
21827563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
21837563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
21847563SPrasad.Singamsetty@Sun.COM 
21850Sstevel@tonic-gate 		/* create the new partition */
21860Sstevel@tonic-gate 		i = specify(tsystid);
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 		if (i != -1) {
21890Sstevel@tonic-gate 			/* see if it should be the active partition */
2190251Slclee 			(void) printf(E_LINE);
2191251Slclee 			(void) printf(Q_LINE);
2192251Slclee 
2193251Slclee 			(void) printf(
2194251Slclee 			    "Should this become the active partition? If "
2195251Slclee 			    "yes, it  will be activated\n"
2196251Slclee 			    "each time the computer is reset or turned on.\n"
2197251Slclee 			    "Please type \"y\" or \"n\". ");
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate 			if (yesno()) {
2200251Slclee 				(void) printf(E_LINE);
22010Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
22020Sstevel@tonic-gate 					if (j == i) {
22030Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2204251Slclee 						(void) printf(E_LINE);
2205251Slclee 						(void) printf(
2206251Slclee 						    "Partition %d is now "
22070Sstevel@tonic-gate 						    "the active partition.",
2208251Slclee 						    j + 1);
22090Sstevel@tonic-gate 					} else {
22100Sstevel@tonic-gate 						Table[j].bootid = 0;
22110Sstevel@tonic-gate 					}
22120Sstevel@tonic-gate 				}
22130Sstevel@tonic-gate 			} else {
22140Sstevel@tonic-gate 				Table[i].bootid = 0;
22150Sstevel@tonic-gate 			}
22160Sstevel@tonic-gate 
22170Sstevel@tonic-gate 			/* set up the return code */
22180Sstevel@tonic-gate 			i = 1;
22190Sstevel@tonic-gate 		}
22200Sstevel@tonic-gate 	} else {
22210Sstevel@tonic-gate 		/*
22220Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
22230Sstevel@tonic-gate 		 * the table
22240Sstevel@tonic-gate 		 *
22250Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
22260Sstevel@tonic-gate 		 * empty
22270Sstevel@tonic-gate 		 */
22280Sstevel@tonic-gate 		retCode = verify_tbl();
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 		if (retCode < 0) {
2231251Slclee 			(void) fprintf(stderr,
22320Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
22330Sstevel@tonic-gate 			    "current partition table is invalid.\n");
22340Sstevel@tonic-gate 			return (-1);
22350Sstevel@tonic-gate 		} else if (retCode > 0) {
2236251Slclee 			(void) printf(
2237251Slclee 			    "An EFI partition must be the only partition on "
2238251Slclee 			    "disk.  You may manually delete existing\n"
2239251Slclee 			    "partitions, or fdisk can do it.\n"
2240251Slclee 			    "Do you want fdisk to destroy existing "
2241251Slclee 			    "partitions?\n"
2242251Slclee 			    "Please type \"y\" or \"n\". ");
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 			if (yesno()) {
22450Sstevel@tonic-gate 				nulltbl();
22460Sstevel@tonic-gate 			} else {
22470Sstevel@tonic-gate 				return (-1);
22480Sstevel@tonic-gate 			}
22490Sstevel@tonic-gate 		}
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
22527563SPrasad.Singamsetty@Sun.COM 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
22537563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
22547563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity - 1));
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 		if (i != 0) {
2257251Slclee 			(void) printf("Error creating EFI partition!!!\n");
22580Sstevel@tonic-gate 			i = -1;
22590Sstevel@tonic-gate 		} else {
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 			/* EFI partitions are currently never active */
22620Sstevel@tonic-gate 			Table[i].bootid = 0;
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 			/* set up the return code */
22650Sstevel@tonic-gate 			i = 1;
22660Sstevel@tonic-gate 		}
22670Sstevel@tonic-gate 	}
22680Sstevel@tonic-gate 
22690Sstevel@tonic-gate 	return (i);
22700Sstevel@tonic-gate }
22710Sstevel@tonic-gate 
22720Sstevel@tonic-gate /*
22730Sstevel@tonic-gate  * specify
22740Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
22750Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
22760Sstevel@tonic-gate  * cylinder and length in cylinders.
22770Sstevel@tonic-gate  */
2278251Slclee static int
2279251Slclee specify(uchar_t tsystid)
22800Sstevel@tonic-gate {
22815169Slclee 	int	i, j, percent = -1;
22827563SPrasad.Singamsetty@Sun.COM 	int	cyl, cylen;
22837563SPrasad.Singamsetty@Sun.COM 	diskaddr_t first_free, size_free;
22847563SPrasad.Singamsetty@Sun.COM 	diskaddr_t max_free;
22855169Slclee 	int	cyl_size;
22860Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
22870Sstevel@tonic-gate 
22885169Slclee 	cyl_size = heads * sectors;
22895169Slclee 
22905169Slclee 	/*
22915169Slclee 	 * make a local copy of the partition table
22925169Slclee 	 * and sort it into relsect order
22935169Slclee 	 */
22945169Slclee 	for (i = 0; i < FD_NUMPART; i++)
22955169Slclee 		partition[i] = &Table[i];
22965169Slclee 
22975169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
22985169Slclee 		if (partition[i]->systid == UNUSED)
22995169Slclee 			break;
23005169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
23015169Slclee 			if (partition[j]->systid == UNUSED)
23025169Slclee 				break;
23038333SSuhasini.Peddada@Sun.COM 			if (lel(partition[j]->relsect) <
23048333SSuhasini.Peddada@Sun.COM 			    lel(partition[i]->relsect)) {
23055169Slclee 				struct ipart *temp = partition[i];
23065169Slclee 				partition[i] = partition[j];
23075169Slclee 				partition[j] = temp;
23085169Slclee 			}
23095169Slclee 		}
23105169Slclee 	}
23115169Slclee 
23127563SPrasad.Singamsetty@Sun.COM 
2313251Slclee 	(void) printf(Q_LINE);
2314251Slclee 	(void) printf(
2315251Slclee 	    "Specify the percentage of disk to use for this partition\n"
2316251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
23179786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
23180Sstevel@tonic-gate 	rm_blanks(s);
23190Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
23205169Slclee 		i = 0;
23219786SBarry.Harding@Sun.COM 		while ((s[i] != '\0') && (s[i] != '\n')) {
23225169Slclee 			if (s[i] < '0' || s[i] > '9') {
23235169Slclee 				(void) printf(E_LINE);
23245169Slclee 				(void) printf("Invalid percentage value "
23255169Slclee 				    "specified; retry the operation.");
23265169Slclee 				return (-1);
23275169Slclee 			}
23285169Slclee 			i++;
23295169Slclee 			if (i > 3) {
23305169Slclee 				(void) printf(E_LINE);
23315169Slclee 				(void) printf("Invalid percentage value "
23325169Slclee 				    "specified; retry the operation.");
23335169Slclee 				return (-1);
23345169Slclee 			}
23355169Slclee 		}
23365169Slclee 		if ((percent = atoi(s)) > 100) {
23375169Slclee 			(void) printf(E_LINE);
23385169Slclee 			(void) printf(
23395169Slclee 			    "Percentage value is too large. The value must be"
23405169Slclee 			    " between 1 and 100;\nretry the operation.\n");
23415169Slclee 			return (-1);
23420Sstevel@tonic-gate 		}
23435169Slclee 		if (percent < 1) {
23445169Slclee 			(void) printf(E_LINE);
23455169Slclee 			(void) printf(
23465169Slclee 			    "Percentage value is too small. The value must be"
23475169Slclee 			    " between 1 and 100;\nretry the operation.\n");
23485169Slclee 			return (-1);
23495169Slclee 		}
23505169Slclee 
23515169Slclee 		if (percent == 100)
23527563SPrasad.Singamsetty@Sun.COM 			cylen = Numcyl_usable - 1;
23535169Slclee 		else
23547563SPrasad.Singamsetty@Sun.COM 			cylen = (Numcyl_usable * percent) / 100;
23555169Slclee 
23565169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
23575169Slclee 		if ((tsystid == DOSOS12) &&
23585169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
23595169Slclee 			int n;
23607563SPrasad.Singamsetty@Sun.COM 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
23615169Slclee 			(void) printf(E_LINE);
23625169Slclee 			(void) printf("Maximum size for a DOS partition "
23635169Slclee 			    "is %d%%; retry the operation.",
23645169Slclee 			    n <= 100 ? n : 100);
23655169Slclee 			return (-1);
23660Sstevel@tonic-gate 		}
23675169Slclee 
23685169Slclee 
23695169Slclee 		max_free = 0;
23705169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
23715169Slclee 
23725169Slclee 			/*
23735169Slclee 			 * check for free space before partition i
23745169Slclee 			 * where i varies from 0 to 3
23755169Slclee 			 *
23765169Slclee 			 * freespace after partition 3 is unusable
23775169Slclee 			 * because there are no free partitions
23785169Slclee 			 *
23795169Slclee 			 * freespace begins at the end of previous partition
23805169Slclee 			 * or cylinder 1
23815169Slclee 			 */
23825169Slclee 			if (i) {
23835169Slclee 				/* Not an empty table */
23848333SSuhasini.Peddada@Sun.COM 				first_free = lel(partition[i - 1]->relsect) +
23858333SSuhasini.Peddada@Sun.COM 				    lel(partition[i - 1]->numsect);
23865169Slclee 			} else {
23875169Slclee 				first_free = cyl_size;
23885169Slclee 			}
23895169Slclee 
23905169Slclee 			/*
23915169Slclee 			 * freespace ends before the current partition
23925169Slclee 			 * or the end of the disk (chs end)
23935169Slclee 			 */
23945169Slclee 			if (partition[i]->systid == UNUSED) {
23955169Slclee 				size_free = chs_capacity - first_free;
23965169Slclee 			} else {
23978148SShidokht.Yadegari@Sun.COM 				/*
23988148SShidokht.Yadegari@Sun.COM 				 * Partition might start before cylinder 1.
23998148SShidokht.Yadegari@Sun.COM 				 * Make sure free space is not negative.
24008148SShidokht.Yadegari@Sun.COM 				 */
24015169Slclee 				size_free =
24028333SSuhasini.Peddada@Sun.COM 				    (lel(partition[i]->relsect > first_free)) ?
24038333SSuhasini.Peddada@Sun.COM 				    (lel(partition[i]->relsect) - first_free) :
24048333SSuhasini.Peddada@Sun.COM 				    0;
24055169Slclee 			}
24065169Slclee 
24075169Slclee 			/* save largest free space */
24085169Slclee 			if (max_free < size_free)
24095169Slclee 				max_free = size_free;
24105169Slclee 
24117563SPrasad.Singamsetty@Sun.COM 			if (((uint64_t)cylen * cyl_size) <= size_free) {
24125169Slclee 				/* We found a place to use */
24135169Slclee 				break;
24145169Slclee 			}
24155169Slclee 			if (partition[i]->systid == UNUSED) {
24165169Slclee 				(void) printf(E_LINE);
24175169Slclee 				max_free /= (cyl_size);
24185169Slclee 				(void) fprintf(stderr, "fdisk: "
24197563SPrasad.Singamsetty@Sun.COM 				    "Maximum percentage available is %lld\n",
24207563SPrasad.Singamsetty@Sun.COM 				    100 * max_free / Numcyl_usable);
24215169Slclee 				return (-1);
24225169Slclee 			}
24230Sstevel@tonic-gate 		}
24245169Slclee 
24255169Slclee 		(void) printf(E_LINE);
24265169Slclee 		if (i >= FD_NUMPART) {
24275169Slclee 			(void) fprintf(stderr,
24285169Slclee 			    "fdisk: Partition table is full.\n");
24295169Slclee 			return (-1);
24305169Slclee 		}
24315169Slclee 
24325169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
24335169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
24345169Slclee 			return (i);
24355169Slclee 		}
24365169Slclee 		return (-1);
24375169Slclee 	} else {
24385169Slclee 
24395169Slclee 		/* Specifying size in cylinders */
2440251Slclee 		(void) printf(E_LINE);
24415169Slclee 		(void) printf(Q_LINE);
24425169Slclee 		(void) printf("Enter starting cylinder number: ");
24435169Slclee 		if ((cyl = getcyl()) == -1) {
24445169Slclee 			(void) printf(E_LINE);
24455169Slclee 			(void) printf("Invalid number; retry the operation.");
24465169Slclee 			return (-1);
24475169Slclee 		}
24485169Slclee 		if (cyl == 0) {
24495169Slclee 			(void) printf(E_LINE);
24505169Slclee 			(void) printf(
24515169Slclee 			    "New partition cannot start at cylinder 0.\n");
24525169Slclee 			return (-1);
24535169Slclee 		}
24547563SPrasad.Singamsetty@Sun.COM 
24557563SPrasad.Singamsetty@Sun.COM 
24567563SPrasad.Singamsetty@Sun.COM 		if (cyl >= Numcyl_usable) {
24575169Slclee 			(void) printf(E_LINE);
24585169Slclee 			(void) printf(
24595169Slclee 			    "Cylinder %d is out of bounds, "
24605169Slclee 			    "the maximum is %d.\n",
24617563SPrasad.Singamsetty@Sun.COM 			    cyl, Numcyl_usable - 1);
24625169Slclee 			return (-1);
24635169Slclee 		}
24647563SPrasad.Singamsetty@Sun.COM 
24655169Slclee 		(void) printf(Q_LINE);
24665169Slclee 		(void) printf("Enter partition size in cylinders: ");
24675169Slclee 		if ((cylen = getcyl()) == -1) {
24685169Slclee 			(void) printf(E_LINE);
24695169Slclee 			(void) printf("Invalid number, retry the operation.");
24705169Slclee 			return (-1);
24715169Slclee 		}
24725169Slclee 
24735169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
24745169Slclee 			uint32_t	t_relsect, t_numsect;
24755169Slclee 
24765169Slclee 			if (partition[i]->systid == UNUSED)
24775169Slclee 				break;
24788333SSuhasini.Peddada@Sun.COM 			t_relsect = lel(partition[i]->relsect);
24798333SSuhasini.Peddada@Sun.COM 			t_numsect = lel(partition[i]->numsect);
24805169Slclee 
24815169Slclee 			if (cyl * cyl_size >= t_relsect &&
24825169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
24835169Slclee 				(void) printf(E_LINE);
24845169Slclee 				(void) printf(
24855169Slclee 				    "Cylinder %d is already allocated"
24865169Slclee 				    "\nretry the operation.",
24875169Slclee 				    cyl);
24885169Slclee 				return (-1);
24895169Slclee 			}
24905169Slclee 
24915169Slclee 			if (cyl * cyl_size < t_relsect &&
24925169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
24935169Slclee 				(void) printf(E_LINE);
24945169Slclee 				(void) printf(
24955169Slclee 				    "Maximum size for partition is %u cylinders"
24965169Slclee 				    "\nretry the operation.",
24975169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
24985169Slclee 				return (-1);
24995169Slclee 			}
25005169Slclee 		}
25015169Slclee 
25027563SPrasad.Singamsetty@Sun.COM 		/* Verify partition doesn't exceed disk size or 2 TB */
25037563SPrasad.Singamsetty@Sun.COM 		if (cyl + cylen > Numcyl_usable) {
25045169Slclee 			(void) printf(E_LINE);
25057563SPrasad.Singamsetty@Sun.COM 			if (Numcyl > Numcyl_usable) {
25067563SPrasad.Singamsetty@Sun.COM 				(void) printf(
25077563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
25087563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
25097563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
25107563SPrasad.Singamsetty@Sun.COM 			} else {
25117563SPrasad.Singamsetty@Sun.COM 				(void) printf(
25127563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
25137563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
25147563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
25157563SPrasad.Singamsetty@Sun.COM 			}
25165169Slclee 			return (-1);
25175169Slclee 		}
25185169Slclee 
25195169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
25205169Slclee 		if ((tsystid == DOSOS12) &&
25215169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
25225169Slclee 			(void) printf(E_LINE);
25235169Slclee 			(void) printf(
25245169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
25255169Slclee 			    "\nretry the operation.",
25265169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
25275169Slclee 			return (-1);
25285169Slclee 		}
25295169Slclee 
2530251Slclee 		(void) printf(E_LINE);
25315169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
25325169Slclee 		    cyl * cyl_size, cylen * cyl_size);
25335169Slclee 		if (i < 0)
25345169Slclee 			return (-1);
25355169Slclee 
25365169Slclee 		if (verify_tbl() < 0) {
25375169Slclee 			(void) printf(E_LINE);
25385169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
25395169Slclee 			return (-1);
25405169Slclee 		}
25415169Slclee 
25425169Slclee 		return (i);
25430Sstevel@tonic-gate 	}
25440Sstevel@tonic-gate }
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate /*
25470Sstevel@tonic-gate  * dispmenu
25480Sstevel@tonic-gate  * Display command menu (interactive mode).
25490Sstevel@tonic-gate  */
2550251Slclee static void
2551251Slclee dispmenu(void)
25520Sstevel@tonic-gate {
2553251Slclee 	(void) printf(M_LINE);
2554251Slclee 	(void) printf(
2555251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
2556251Slclee 	    "   1. Create a partition\n"
2557251Slclee 	    "   2. Specify the active partition\n"
2558251Slclee 	    "   3. Delete a partition\n"
2559251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
2560251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
2561251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
25620Sstevel@tonic-gate }
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate /*
25650Sstevel@tonic-gate  * pchange
25660Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
25670Sstevel@tonic-gate  */
2568251Slclee static int
2569251Slclee pchange(void)
25700Sstevel@tonic-gate {
25710Sstevel@tonic-gate 	char s[80];
25720Sstevel@tonic-gate 	int i, j;
25730Sstevel@tonic-gate 
2574251Slclee 	for (;;) {
2575251Slclee 		(void) printf(Q_LINE);
25760Sstevel@tonic-gate 			{
2577251Slclee 			(void) printf(
2578251Slclee 			    "Specify the partition number to boot from"
25790Sstevel@tonic-gate 			    " (or specify 0 for none): ");
25800Sstevel@tonic-gate 			}
25819786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
25820Sstevel@tonic-gate 		rm_blanks(s);
25839786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
25849786SBarry.Harding@Sun.COM 		    (s[0] < '0') || (s[0] > '4')) {
2585251Slclee 			(void) printf(E_LINE);
2586251Slclee 			(void) printf(
2587251Slclee 			    "Invalid response, please specify a number"
25880Sstevel@tonic-gate 			    " between 0 and 4.\n");
25890Sstevel@tonic-gate 		} else {
25900Sstevel@tonic-gate 			break;
25910Sstevel@tonic-gate 		}
25920Sstevel@tonic-gate 	}
25930Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
25940Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
25950Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
25960Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
25970Sstevel@tonic-gate 				Table[i].bootid = 0;
25980Sstevel@tonic-gate 		}
2599251Slclee 		(void) printf(E_LINE);
2600251Slclee 			(void) printf(
2601251Slclee 			    "No partition is currently marked as active.");
26020Sstevel@tonic-gate 		return (0);
26030Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
26040Sstevel@tonic-gate 		i = s[0] - '1';
26050Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2606251Slclee 			(void) printf(E_LINE);
2607251Slclee 			(void) printf("Partition does not exist.");
26080Sstevel@tonic-gate 			return (-1);
26090Sstevel@tonic-gate 		}
26100Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
26110Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
26120Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
26130Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
2614251Slclee 			(void) printf(E_LINE);
2615251Slclee 			(void) printf(
2616251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
26170Sstevel@tonic-gate 			    "cannot be made active.\n");
2618251Slclee 			(void) printf("Select another partition.");
26190Sstevel@tonic-gate 			return (-1);
26200Sstevel@tonic-gate 		}
26210Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
26220Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
26230Sstevel@tonic-gate 			if (j != i)
26240Sstevel@tonic-gate 			Table[j].bootid = 0;
26250Sstevel@tonic-gate 		}
26260Sstevel@tonic-gate 	}
2627251Slclee 	(void) printf(E_LINE);
26280Sstevel@tonic-gate 		{
2629251Slclee 		(void) printf(
2630251Slclee 		    "Partition %d is now active. The system will start up"
2631251Slclee 		    " from this\n", i + 1);
2632251Slclee 		(void) printf("partition after the next reboot.");
26330Sstevel@tonic-gate 		}
26340Sstevel@tonic-gate 	return (1);
26350Sstevel@tonic-gate }
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate /*
26380Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
26390Sstevel@tonic-gate  */
2640251Slclee static int
2641251Slclee ppartid(void)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate 	char	*p, s[80];
26440Sstevel@tonic-gate 	int	i;
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	for (;;) {
2647251Slclee 		(void) printf(Q_LINE);
2648251Slclee 		(void) printf("Specify the partition number to change"
26495169Slclee 		    " (or enter 0 to exit): ");
2650251Slclee 		if (!fgets(s, sizeof (s), stdin))
2651251Slclee 			return (1);
26520Sstevel@tonic-gate 		i = strtol(s, &p, 10);
26530Sstevel@tonic-gate 
26540Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
2655251Slclee 			(void) printf(E_LINE);
2656251Slclee 			(void) printf(
2657251Slclee 			    "Invalid response, retry the operation.\n");
26580Sstevel@tonic-gate 			continue;
26590Sstevel@tonic-gate 		}
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 		if (i == 0) {
26620Sstevel@tonic-gate 			/* exit delete command */
2663251Slclee 			(void) printf(E_LINE); /* clear error message */
26640Sstevel@tonic-gate 			return (1);
26650Sstevel@tonic-gate 		}
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 		i -= 1;
26680Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
26690Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
26700Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
26710Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
26720Sstevel@tonic-gate 		} else {
2673251Slclee 			(void) printf(E_LINE);
2674251Slclee 			(void) printf(
2675251Slclee 			    "Partition %d is not a Solaris partition.",
26760Sstevel@tonic-gate 			    i + 1);
26770Sstevel@tonic-gate 			continue;
26780Sstevel@tonic-gate 		}
26790Sstevel@tonic-gate 
2680251Slclee 		(void) printf(E_LINE);
2681251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
26820Sstevel@tonic-gate 		return (1);
26830Sstevel@tonic-gate 	}
26840Sstevel@tonic-gate }
26850Sstevel@tonic-gate 
26860Sstevel@tonic-gate /*
26870Sstevel@tonic-gate  * pdelete
26880Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
26890Sstevel@tonic-gate  */
2690251Slclee static char
2691251Slclee pdelete(void)
26920Sstevel@tonic-gate {
26930Sstevel@tonic-gate 	char s[80];
26940Sstevel@tonic-gate 	int i, j;
26950Sstevel@tonic-gate 	char pactive;
26960Sstevel@tonic-gate 
2697251Slclee DEL1:	(void) printf(Q_LINE);
2698251Slclee 	(void) printf("Specify the partition number to delete"
26990Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
27009786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
27010Sstevel@tonic-gate 	rm_blanks(s);
27020Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
2703251Slclee 		(void) printf(E_LINE);	/* clear error message */
27040Sstevel@tonic-gate 		return (1);
27050Sstevel@tonic-gate 	}
27060Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
27079786SBarry.Harding@Sun.COM 	if (((s[1] != '\0') && (s[1] != '\n')) ||
27089786SBarry.Harding@Sun.COM 	    (i = atoi(s)) < 1 || i > FD_NUMPART) {
2709251Slclee 		(void) printf(E_LINE);
2710251Slclee 		(void) printf("Invalid response, retry the operation.\n");
27110Sstevel@tonic-gate 		goto DEL1;
27120Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
27130Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
27140Sstevel@tonic-gate 	}
27150Sstevel@tonic-gate 
27160Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
2717251Slclee 		(void) printf(E_LINE);
2718251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
27190Sstevel@tonic-gate 		return (-1);
27200Sstevel@tonic-gate 	}
27210Sstevel@tonic-gate 
27228333SSuhasini.Peddada@Sun.COM 	(void) printf(Q_LINE);
27238333SSuhasini.Peddada@Sun.COM 	(void) printf("Are you sure you want to delete partition %d?"
27248333SSuhasini.Peddada@Sun.COM 	    " This will make all files and \n", i + 1);
27258333SSuhasini.Peddada@Sun.COM 	(void) printf("programs in this partition inaccessible (type"
27268333SSuhasini.Peddada@Sun.COM 	    " \"y\" or \"n\"). ");
27278333SSuhasini.Peddada@Sun.COM 
27288333SSuhasini.Peddada@Sun.COM 	(void) printf(E_LINE);
27298333SSuhasini.Peddada@Sun.COM 	if (! yesno()) {
27308333SSuhasini.Peddada@Sun.COM 		return (1);
27310Sstevel@tonic-gate 	}
27320Sstevel@tonic-gate 
27330Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
27340Sstevel@tonic-gate 		pactive = 1;
27350Sstevel@tonic-gate 	} else {
27360Sstevel@tonic-gate 		pactive = 0;
27370Sstevel@tonic-gate 	}
27380Sstevel@tonic-gate 
27390Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
27405169Slclee 		Table[j] = Table[j + 1];
27410Sstevel@tonic-gate 	}
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate 	Table[j].systid = UNUSED;
27440Sstevel@tonic-gate 	Table[j].numsect = 0;
27450Sstevel@tonic-gate 	Table[j].relsect = 0;
27460Sstevel@tonic-gate 	Table[j].bootid = 0;
2747251Slclee 	(void) printf(E_LINE);
2748251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 	if (pactive) {
27515169Slclee 		(void) printf(" This was the active partition.");
27520Sstevel@tonic-gate 	}
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	return (1);
27550Sstevel@tonic-gate }
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate /*
27580Sstevel@tonic-gate  * rm_blanks
27590Sstevel@tonic-gate  * Remove blanks from strings of user responses.
27600Sstevel@tonic-gate  */
2761251Slclee static void
2762251Slclee rm_blanks(char *s)
27630Sstevel@tonic-gate {
27640Sstevel@tonic-gate 	register int i, j;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
27670Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
27680Sstevel@tonic-gate 			continue;
27690Sstevel@tonic-gate 		else
27700Sstevel@tonic-gate 			/* Found first non-blank character of the string */
27710Sstevel@tonic-gate 			break;
27720Sstevel@tonic-gate 	}
27730Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
27740Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
27750Sstevel@tonic-gate 			/* Reached end of string */
27760Sstevel@tonic-gate 			return;
27770Sstevel@tonic-gate 		}
27780Sstevel@tonic-gate 	}
27790Sstevel@tonic-gate }
27800Sstevel@tonic-gate 
27810Sstevel@tonic-gate /*
27820Sstevel@tonic-gate  * getcyl
27830Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
27840Sstevel@tonic-gate  * string to a decimal value.
27850Sstevel@tonic-gate  */
2786251Slclee static int
2787251Slclee getcyl(void)
27880Sstevel@tonic-gate {
27890Sstevel@tonic-gate int slen, i, j;
27900Sstevel@tonic-gate unsigned int cyl;
27919786SBarry.Harding@Sun.COM 	(void) fgets(s, sizeof (s), stdin);
27920Sstevel@tonic-gate 	rm_blanks(s);
27930Sstevel@tonic-gate 	slen = strlen(s);
27949786SBarry.Harding@Sun.COM 	if (s[slen - 1] == '\n')
27959786SBarry.Harding@Sun.COM 		slen--;
27960Sstevel@tonic-gate 	j = 1;
27970Sstevel@tonic-gate 	cyl = 0;
2798251Slclee 	for (i = slen - 1; i >= 0; i--) {
27990Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
28000Sstevel@tonic-gate 			return (-1);
28010Sstevel@tonic-gate 		}
2802251Slclee 		cyl += (j * (s[i] - '0'));
28030Sstevel@tonic-gate 		j *= 10;
28040Sstevel@tonic-gate 	}
28050Sstevel@tonic-gate 	return (cyl);
28060Sstevel@tonic-gate }
28070Sstevel@tonic-gate 
28080Sstevel@tonic-gate /*
28090Sstevel@tonic-gate  * disptbl
28100Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
28110Sstevel@tonic-gate  * of the disk used for each partition.
28120Sstevel@tonic-gate  */
2813251Slclee static void
2814251Slclee disptbl(void)
28150Sstevel@tonic-gate {
28160Sstevel@tonic-gate 	int i;
28170Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
28180Sstevel@tonic-gate 	char *stat, *type;
28197563SPrasad.Singamsetty@Sun.COM 	int is_pmbr = 0;
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
2822251Slclee 		(void) printf("WARNING: critical disk geometry information"
28235169Slclee 		    " missing!\n");
2824251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
28250Sstevel@tonic-gate 		exit(1);
28260Sstevel@tonic-gate 	}
28270Sstevel@tonic-gate 
2828251Slclee 	(void) printf(HOME);
2829251Slclee 	(void) printf(T_LINE);
2830251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
2831*9889SLarry.Liu@Sun.COM 	(void) printf("             Cylinder size is %d (%d byte) blocks\n\n",
2832*9889SLarry.Liu@Sun.COM 	    heads * sectors, sectsiz);
2833251Slclee 	(void) printf(
2834251Slclee 	    "                                               Cylinders\n");
2835251Slclee 	(void) printf(
2836251Slclee 	    "      Partition   Status    Type          Start   End   Length"
28370Sstevel@tonic-gate 	    "    %%\n");
2838251Slclee 	(void) printf(
2839251Slclee 	    "      =========   ======    ============  =====   ===   ======"
28400Sstevel@tonic-gate 	    "   ===");
28410Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28420Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2843251Slclee 			(void) printf("\n");
2844251Slclee 			(void) printf(CLR_LIN);
28450Sstevel@tonic-gate 			continue;
28460Sstevel@tonic-gate 		}
28470Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
28485169Slclee 			stat = Actvstr;
28490Sstevel@tonic-gate 		else
28505169Slclee 			stat = NAstr;
28510Sstevel@tonic-gate 		switch (Table[i].systid) {
28520Sstevel@tonic-gate 		case UNIXOS:
28535169Slclee 			type = Ustr;
28545169Slclee 			break;
28550Sstevel@tonic-gate 		case SUNIXOS:
28565169Slclee 			type = SUstr;
28575169Slclee 			break;
28580Sstevel@tonic-gate 		case SUNIXOS2:
28595169Slclee 			type = SU2str;
28605169Slclee 			break;
28610Sstevel@tonic-gate 		case X86BOOT:
28625169Slclee 			type = X86str;
28635169Slclee 			break;
28640Sstevel@tonic-gate 		case DOSOS12:
28655169Slclee 			type = Dstr;
28665169Slclee 			break;
28670Sstevel@tonic-gate 		case DOSOS16:
28685169Slclee 			type = D16str;
28695169Slclee 			break;
28700Sstevel@tonic-gate 		case EXTDOS:
28715169Slclee 			type = EDstr;
28725169Slclee 			break;
28730Sstevel@tonic-gate 		case DOSDATA:
28745169Slclee 			type = DDstr;
28755169Slclee 			break;
28760Sstevel@tonic-gate 		case DOSHUGE:
28775169Slclee 			type = DBstr;
28785169Slclee 			break;
28790Sstevel@tonic-gate 		case PCIXOS:
28805169Slclee 			type = PCstr;
28815169Slclee 			break;
28820Sstevel@tonic-gate 		case DIAGPART:
28835169Slclee 			type = DIAGstr;
28845169Slclee 			break;
28850Sstevel@tonic-gate 		case FDISK_IFS:
28865169Slclee 			type = IFSstr;
28875169Slclee 			break;
28880Sstevel@tonic-gate 		case FDISK_AIXBOOT:
28895169Slclee 			type = AIXstr;
28905169Slclee 			break;
28910Sstevel@tonic-gate 		case FDISK_AIXDATA:
28925169Slclee 			type = AIXDstr;
28935169Slclee 			break;
28940Sstevel@tonic-gate 		case FDISK_OS2BOOT:
28955169Slclee 			type = OS2str;
28965169Slclee 			break;
28970Sstevel@tonic-gate 		case FDISK_WINDOWS:
28985169Slclee 			type = WINstr;
28995169Slclee 			break;
29000Sstevel@tonic-gate 		case FDISK_EXT_WIN:
29015169Slclee 			type = EWINstr;
29025169Slclee 			break;
29030Sstevel@tonic-gate 		case FDISK_FAT95:
29045169Slclee 			type = FAT95str;
29055169Slclee 			break;
29060Sstevel@tonic-gate 		case FDISK_EXTLBA:
29075169Slclee 			type = EXTLstr;
29085169Slclee 			break;
29090Sstevel@tonic-gate 		case FDISK_LINUX:
29105169Slclee 			type = LINUXstr;
29115169Slclee 			break;
29120Sstevel@tonic-gate 		case FDISK_CPM:
29135169Slclee 			type = CPMstr;
29145169Slclee 			break;
29150Sstevel@tonic-gate 		case FDISK_NOVELL3:
29165169Slclee 			type = NOVstr;
29175169Slclee 			break;
29180Sstevel@tonic-gate 		case FDISK_QNX4:
29195169Slclee 			type = QNXstr;
29205169Slclee 			break;
29210Sstevel@tonic-gate 		case FDISK_QNX42:
29225169Slclee 			type = QNX2str;
29235169Slclee 			break;
29240Sstevel@tonic-gate 		case FDISK_QNX43:
29255169Slclee 			type = QNX3str;
29265169Slclee 			break;
29270Sstevel@tonic-gate 		case FDISK_LINUXNAT:
29285169Slclee 			type = LINNATstr;
29295169Slclee 			break;
29300Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
29315169Slclee 			type = NTFSVOL1str;
29325169Slclee 			break;
29330Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
29345169Slclee 			type = NTFSVOL2str;
29355169Slclee 			break;
29360Sstevel@tonic-gate 		case FDISK_BSD:
29375169Slclee 			type = BSDstr;
29385169Slclee 			break;
29390Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
29405169Slclee 			type = NEXTSTEPstr;
29415169Slclee 			break;
29420Sstevel@tonic-gate 		case FDISK_BSDIFS:
29435169Slclee 			type = BSDIFSstr;
29445169Slclee 			break;
29450Sstevel@tonic-gate 		case FDISK_BSDISWAP:
29465169Slclee 			type = BSDISWAPstr;
29475169Slclee 			break;
29480Sstevel@tonic-gate 		case EFI_PMBR:
29495169Slclee 			type = EFIstr;
29508333SSuhasini.Peddada@Sun.COM 			if (lel(Table[i].numsect) == DK_MAX_2TB)
29517563SPrasad.Singamsetty@Sun.COM 				is_pmbr = 1;
29527563SPrasad.Singamsetty@Sun.COM 
29535169Slclee 			break;
29540Sstevel@tonic-gate 		default:
29555169Slclee 			type = Ostr;
29565169Slclee 			break;
29570Sstevel@tonic-gate 		}
29588333SSuhasini.Peddada@Sun.COM 		startcyl = lel(Table[i].relsect) /
29595936Sbharding 		    (unsigned long)(heads * sectors);
29607563SPrasad.Singamsetty@Sun.COM 
29618333SSuhasini.Peddada@Sun.COM 		if (lel(Table[i].numsect) == DK_MAX_2TB) {
29627563SPrasad.Singamsetty@Sun.COM 			endcyl = Numcyl - 1;
29637563SPrasad.Singamsetty@Sun.COM 			length = endcyl - startcyl + 1;
29647563SPrasad.Singamsetty@Sun.COM 		} else {
29658333SSuhasini.Peddada@Sun.COM 			length = lel(Table[i].numsect) /
29667563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors);
29678333SSuhasini.Peddada@Sun.COM 			if (lel(Table[i].numsect) %
29687563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors))
29697563SPrasad.Singamsetty@Sun.COM 				length++;
29707563SPrasad.Singamsetty@Sun.COM 			endcyl = startcyl + length - 1;
29717563SPrasad.Singamsetty@Sun.COM 		}
29727563SPrasad.Singamsetty@Sun.COM 
29737563SPrasad.Singamsetty@Sun.COM 		percent = length * 100 / Numcyl_usable;
29747563SPrasad.Singamsetty@Sun.COM 		if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
29757563SPrasad.Singamsetty@Sun.COM 			if ((remainder * 100 / Numcyl_usable) > 50) {
29760Sstevel@tonic-gate 				/* round up */
29770Sstevel@tonic-gate 				percent++;
29780Sstevel@tonic-gate 			}
29790Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
29800Sstevel@tonic-gate 			/* rounded down */
29810Sstevel@tonic-gate 		}
29820Sstevel@tonic-gate 		if (percent > 100)
29830Sstevel@tonic-gate 			percent = 100;
2984251Slclee 		(void) printf(
2985251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
2986251Slclee 		    "    %3d",
2987251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
29880Sstevel@tonic-gate 	}
29897563SPrasad.Singamsetty@Sun.COM 
29900Sstevel@tonic-gate 	/* Print warning message if table is empty */
29910Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
2992251Slclee 		(void) printf(W_LINE);
2993251Slclee 		(void) printf("WARNING: no partitions are defined!");
29940Sstevel@tonic-gate 	} else {
29950Sstevel@tonic-gate 		/* Clear the warning line */
2996251Slclee 		(void) printf(W_LINE);
29977563SPrasad.Singamsetty@Sun.COM 
29987563SPrasad.Singamsetty@Sun.COM 		/* Print warning if disk > 2TB and is not EFI PMBR */
29997563SPrasad.Singamsetty@Sun.COM 		if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
30007563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
30017563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
30020Sstevel@tonic-gate 	}
30030Sstevel@tonic-gate }
30040Sstevel@tonic-gate 
30050Sstevel@tonic-gate /*
30060Sstevel@tonic-gate  * print_Table
30070Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
30080Sstevel@tonic-gate  * the selected disk device.
30090Sstevel@tonic-gate  */
3010251Slclee static void
3011251Slclee print_Table(void)
3012251Slclee {
30130Sstevel@tonic-gate 	int i;
30140Sstevel@tonic-gate 
3015251Slclee 	(void) fprintf(stderr,
30160Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
30170Sstevel@tonic-gate 	    "   NUMSECT\n");
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3020251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
3021251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
3022251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
3023251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
30245169Slclee 		(void) fprintf(stderr, "%-8d ",
30255169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
30260Sstevel@tonic-gate 
3027251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
3028251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
30295169Slclee 		(void) fprintf(stderr, "%-8d ",
30305169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
30318333SSuhasini.Peddada@Sun.COM 		(void) fprintf(stderr, "%-10u ", lel(Table[i].relsect));
30328333SSuhasini.Peddada@Sun.COM 		(void) fprintf(stderr, "%-10u\n", lel(Table[i].numsect));
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate 	}
30350Sstevel@tonic-gate }
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate /*
30380Sstevel@tonic-gate  * copy_Table_to_Old_Table
30390Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
30400Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
30410Sstevel@tonic-gate  * ones compared when determining if Table has changed.
30420Sstevel@tonic-gate  */
3043251Slclee static void
3044251Slclee copy_Table_to_Old_Table(void)
30450Sstevel@tonic-gate {
30460Sstevel@tonic-gate 	int i;
30470Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
30485169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
30490Sstevel@tonic-gate 	}
30500Sstevel@tonic-gate }
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate /*
30530Sstevel@tonic-gate  * nulltbl
30540Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
30550Sstevel@tonic-gate  * fdisk table.
30560Sstevel@tonic-gate  */
3057251Slclee static void
3058251Slclee nulltbl(void)
30590Sstevel@tonic-gate {
30600Sstevel@tonic-gate 	int i;
30610Sstevel@tonic-gate 
30620Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
30635169Slclee 		Table[i].systid = UNUSED;
30648333SSuhasini.Peddada@Sun.COM 		Table[i].numsect = lel(UNUSED);
30658333SSuhasini.Peddada@Sun.COM 		Table[i].relsect = lel(UNUSED);
30665169Slclee 		Table[i].bootid = 0;
30678904SBarry.Harding@Sun.COM 		skip_verify[i] = 0;
30680Sstevel@tonic-gate 	}
30690Sstevel@tonic-gate }
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate /*
30720Sstevel@tonic-gate  * copy_Bootblk_to_Table
30730Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
30740Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
30750Sstevel@tonic-gate  */
3076251Slclee static void
3077251Slclee copy_Bootblk_to_Table(void)
30780Sstevel@tonic-gate {
30790Sstevel@tonic-gate 	int i, j;
30800Sstevel@tonic-gate 	char *bootptr;
30810Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
30820Sstevel@tonic-gate 
30830Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
3084251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
30850Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
30868333SSuhasini.Peddada@Sun.COM 	if (les(Bootblk->signature) != MBB_MAGIC)  {
30870Sstevel@tonic-gate 		/* Signature is missing */
30880Sstevel@tonic-gate 		nulltbl();
3089251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
30900Sstevel@tonic-gate 		return;
30910Sstevel@tonic-gate 	}
30920Sstevel@tonic-gate 	/*
30930Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
30940Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
30950Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
30960Sstevel@tonic-gate 	 * those that are valid.
30970Sstevel@tonic-gate 	 */
30980Sstevel@tonic-gate 	j = 0;
30990Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31000Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
31010Sstevel@tonic-gate 			/* Null entry */
31020Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
31030Sstevel@tonic-gate 		} else {
3104251Slclee 			fill_ipart(bootptr, &Table[j]);
31050Sstevel@tonic-gate 			j++;
31060Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
31070Sstevel@tonic-gate 		}
31080Sstevel@tonic-gate 	}
31090Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
31100Sstevel@tonic-gate 		Table[i].systid = UNUSED;
31118333SSuhasini.Peddada@Sun.COM 		Table[i].numsect = lel(UNUSED);
31128333SSuhasini.Peddada@Sun.COM 		Table[i].relsect = lel(UNUSED);
31130Sstevel@tonic-gate 		Table[i].bootid = 0;
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	}
31160Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
3117251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
31180Sstevel@tonic-gate 	copy_Table_to_Bootblk();
31190Sstevel@tonic-gate }
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate /*
31220Sstevel@tonic-gate  * fill_ipart
31230Sstevel@tonic-gate  * Initialize ipart structure values.
31240Sstevel@tonic-gate  */
3125251Slclee static void
31260Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
31270Sstevel@tonic-gate {
31280Sstevel@tonic-gate #ifdef sparc
31290Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
3130251Slclee 	partp->bootid	= getbyte(&bootptr);
3131251Slclee 	partp->beghead	= getbyte(&bootptr);
3132251Slclee 	partp->begsect	= getbyte(&bootptr);
3133251Slclee 	partp->begcyl	= getbyte(&bootptr);
3134251Slclee 	partp->systid	= getbyte(&bootptr);
3135251Slclee 	partp->endhead	= getbyte(&bootptr);
3136251Slclee 	partp->endsect	= getbyte(&bootptr);
3137251Slclee 	partp->endcyl	= getbyte(&bootptr);
3138251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
3139251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
31400Sstevel@tonic-gate #else
31410Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
31420Sstevel@tonic-gate #endif
31430Sstevel@tonic-gate }
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate /*
3146251Slclee  * getbyte, getlong
31470Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
31480Sstevel@tonic-gate  */
31490Sstevel@tonic-gate #ifdef sparc
3150251Slclee uchar_t
3151251Slclee getbyte(char **bp)
31520Sstevel@tonic-gate {
3153251Slclee 	uchar_t	b;
3154251Slclee 
3155251Slclee 	b = (uchar_t)**bp;
31560Sstevel@tonic-gate 	*bp = *bp + 1;
31570Sstevel@tonic-gate 	return (b);
31580Sstevel@tonic-gate }
31590Sstevel@tonic-gate 
3160251Slclee uint32_t
3161251Slclee getlong(char **bp)
31620Sstevel@tonic-gate {
3163251Slclee 	int32_t	b, bh, bl;
31640Sstevel@tonic-gate 
31650Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
31660Sstevel@tonic-gate 	*bp += 2;
31670Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
31680Sstevel@tonic-gate 	*bp += 2;
31690Sstevel@tonic-gate 
31700Sstevel@tonic-gate 	b = (bh << 16) | bl;
3171251Slclee 	return ((uint32_t)b);
31720Sstevel@tonic-gate }
31730Sstevel@tonic-gate #endif
31740Sstevel@tonic-gate 
31750Sstevel@tonic-gate /*
31760Sstevel@tonic-gate  * copy_Table_to_Bootblk
3177*9889SLarry.Liu@Sun.COM  * Copy the table into the boot record. Note that the unused
31780Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
31790Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
31800Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
31810Sstevel@tonic-gate  */
3182251Slclee static void
3183251Slclee copy_Table_to_Bootblk(void)
31840Sstevel@tonic-gate {
31850Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
31860Sstevel@tonic-gate 
31870Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
31880Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
31890Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
31900Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
31915169Slclee 		if (tbl_ptr->systid == UNUSED)
31925169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
31935169Slclee 		else
31945169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
31950Sstevel@tonic-gate 	}
31968333SSuhasini.Peddada@Sun.COM 	Bootblk->signature = les(MBB_MAGIC);
31970Sstevel@tonic-gate }
31980Sstevel@tonic-gate 
31990Sstevel@tonic-gate /*
32000Sstevel@tonic-gate  * TableChanged
32010Sstevel@tonic-gate  * 	Check for any changes in the partition table.
32020Sstevel@tonic-gate  */
3203251Slclee static int
3204251Slclee TableChanged(void)
32050Sstevel@tonic-gate {
32060Sstevel@tonic-gate 	int i, changed;
32070Sstevel@tonic-gate 
32080Sstevel@tonic-gate 	changed = 0;
32090Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
32105169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
32115169Slclee 			/* Partition table changed, write back to disk */
32125169Slclee 			changed = 1;
32135169Slclee 		}
32140Sstevel@tonic-gate 	}
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate 	return (changed);
32170Sstevel@tonic-gate }
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate /*
32200Sstevel@tonic-gate  * ffile_write
32210Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
32220Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
32230Sstevel@tonic-gate  */
3224251Slclee static void
3225251Slclee ffile_write(char *file)
32260Sstevel@tonic-gate {
32270Sstevel@tonic-gate 	register int	i;
32280Sstevel@tonic-gate 	FILE *fp;
32290Sstevel@tonic-gate 
32300Sstevel@tonic-gate 	/*
32310Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
32320Sstevel@tonic-gate 	 * Open file and write it.
32330Sstevel@tonic-gate 	 */
32340Sstevel@tonic-gate 	if (file != (char *)stdout) {
32355169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
32365169Slclee 			(void) fprintf(stderr,
32375169Slclee 			    "fdisk: Cannot open output file %s.\n",
32385169Slclee 			    file);
32395169Slclee 			exit(1);
32405169Slclee 		}
32410Sstevel@tonic-gate 	}
32420Sstevel@tonic-gate 	else
32435169Slclee 		fp = stdout;
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 	/*
32460Sstevel@tonic-gate 	 * Write the fdisk table information
32470Sstevel@tonic-gate 	 */
3248251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
3249251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
3250251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3251251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
3252251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
3253251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
3254251Slclee 	(void) fprintf(fp, "*\n");
32550Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
32560Sstevel@tonic-gate 	if (v_flag) {
3257251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
3258251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3259251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
3260251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
3261251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
3262251Slclee 		(void) fprintf(fp, "*\n");
32630Sstevel@tonic-gate 	}
3264251Slclee 	(void) fprintf(fp, "* systid:\n");
3265251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
3266251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
3267251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
3268251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
3269251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
3270251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
3271251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
3272251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
3273251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
3274251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
3275251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
3276251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
3277251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
3278251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
3279251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
3280251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
3281251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
3282251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
3283251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
3284251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
3285251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
3286251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
3287251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
3288251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
3289251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
3290251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
3291251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
3292251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
3293251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
3294251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
3295251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
3296251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
3297251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
3298251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
3299251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
3300251Slclee 	(void) fprintf(fp, "*\n");
3301251Slclee 	(void) fprintf(fp,
33020Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
33037563SPrasad.Singamsetty@Sun.COM 	    "    Rsect      Numsect\n");
33047563SPrasad.Singamsetty@Sun.COM 
33050Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
33068333SSuhasini.Peddada@Sun.COM 		if (Table[i].systid != UNUSED)
3307251Slclee 			(void) fprintf(fp,
33087563SPrasad.Singamsetty@Sun.COM 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
33097563SPrasad.Singamsetty@Sun.COM 			    " %-10u\n",
33100Sstevel@tonic-gate 			    Table[i].systid,
33110Sstevel@tonic-gate 			    Table[i].bootid,
33120Sstevel@tonic-gate 			    Table[i].beghead,
33130Sstevel@tonic-gate 			    Table[i].begsect & 0x3f,
33140Sstevel@tonic-gate 			    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
33155169Slclee 			    0xc0) << 2)),
33160Sstevel@tonic-gate 			    Table[i].endhead,
33170Sstevel@tonic-gate 			    Table[i].endsect & 0x3f,
33180Sstevel@tonic-gate 			    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
33195169Slclee 			    0xc0) << 2)),
33208333SSuhasini.Peddada@Sun.COM 			    lel(Table[i].relsect),
33218333SSuhasini.Peddada@Sun.COM 			    lel(Table[i].numsect));
33220Sstevel@tonic-gate 	}
33230Sstevel@tonic-gate 	if (fp != stdout)
3324251Slclee 		(void) fclose(fp);
33250Sstevel@tonic-gate }
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate /*
33280Sstevel@tonic-gate  * fix_slice
33290Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
33300Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
33310Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
33320Sstevel@tonic-gate  */
3333251Slclee static void
3334251Slclee fix_slice(void)
33350Sstevel@tonic-gate {
33360Sstevel@tonic-gate 	int	i;
33377563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
33380Sstevel@tonic-gate 
33390Sstevel@tonic-gate 	if (io_image) {
3340251Slclee 		return;
33410Sstevel@tonic-gate 	}
33420Sstevel@tonic-gate 
33430Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
33440Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
33450Sstevel@tonic-gate 			/*
33460Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
33470Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
33480Sstevel@tonic-gate 			 * the partition.
33490Sstevel@tonic-gate 			 */
33508333SSuhasini.Peddada@Sun.COM 			numsect = lel(Table[i].numsect);
33510Sstevel@tonic-gate 			break;
33520Sstevel@tonic-gate 		}
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
33560Sstevel@tonic-gate 		if (!io_nifdisk) {
33570Sstevel@tonic-gate 			(void) fprintf(stderr,
33580Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
33590Sstevel@tonic-gate 			    " checked.\n");
33600Sstevel@tonic-gate 		}
3361251Slclee 		return;
33620Sstevel@tonic-gate 	}
33630Sstevel@tonic-gate 
3364251Slclee 	if (readvtoc() != VTOC_OK) {
33650Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
33665169Slclee 	}
33675169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
33685169Slclee 		/* Special case for slice two (entire disk) */
33695169Slclee 		if (i == 2) {
33705169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
33715169Slclee 				(void) fprintf(stderr,
33727563SPrasad.Singamsetty@Sun.COM 				    "slice %d starts at %llu, is not at"
33735169Slclee 				    " start of partition",
33745169Slclee 				    i, disk_vtoc.v_part[i].p_start);
33755169Slclee 				if (!io_nifdisk) {
33765169Slclee 					(void) printf(" adjust ?:");
33775169Slclee 					if (yesno())
33780Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
33795169Slclee 				} else {
33805169Slclee 					disk_vtoc.v_part[i].p_start = 0;
33815169Slclee 					(void) fprintf(stderr, " adjusted!\n");
33820Sstevel@tonic-gate 				}
33835169Slclee 
33845169Slclee 			}
33855169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
33865169Slclee 				(void) fprintf(stderr,
33877563SPrasad.Singamsetty@Sun.COM 				    "slice %d size %llu does not cover"
33885169Slclee 				    " complete partition",
33895169Slclee 				    i, disk_vtoc.v_part[i].p_size);
33905169Slclee 				if (!io_nifdisk) {
33915169Slclee 					(void) printf(" adjust ?:");
33925169Slclee 					if (yesno())
33930Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
33940Sstevel@tonic-gate 						    numsect;
33955169Slclee 				} else {
33965169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
33975169Slclee 					(void) fprintf(stderr, " adjusted!\n");
33980Sstevel@tonic-gate 				}
33995169Slclee 			}
34005169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
34015169Slclee 				(void) fprintf(stderr,
34025169Slclee 				    "slice %d tag was %d should be %d",
34035169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
34045169Slclee 				    V_BACKUP);
34055169Slclee 				if (!io_nifdisk) {
3406251Slclee 					(void) printf(" fix ?:");
34075169Slclee 					if (yesno())
34080Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
34090Sstevel@tonic-gate 						    V_BACKUP;
34105169Slclee 				} else {
34110Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
34120Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
34130Sstevel@tonic-gate 				}
34145169Slclee 			}
34155169Slclee 			continue;
34165169Slclee 		}
34175169Slclee 		if (io_ADJT) {
34185169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
34195169Slclee 			    disk_vtoc.v_part[i].p_start +
34205169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
34215169Slclee 				(void) fprintf(stderr,
34227563SPrasad.Singamsetty@Sun.COM 				    "slice %d (start %llu, end %llu)"
34235169Slclee 				    " is larger than the partition",
34245169Slclee 				    i, disk_vtoc.v_part[i].p_start,
34255169Slclee 				    disk_vtoc.v_part[i].p_start +
34265169Slclee 				    disk_vtoc.v_part[i].p_size);
34275169Slclee 				if (!io_nifdisk) {
34285169Slclee 					(void) printf(" remove ?:");
34295169Slclee 					if (yesno()) {
34300Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
34310Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
34320Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
34330Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
34340Sstevel@tonic-gate 					}
34350Sstevel@tonic-gate 				} else {
34365169Slclee 					disk_vtoc.v_part[i].p_size = 0;
34375169Slclee 					disk_vtoc.v_part[i].p_start = 0;
34385169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
34395169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
34400Sstevel@tonic-gate 					(void) fprintf(stderr,
34415169Slclee 					    " removed!\n");
34425169Slclee 				}
34435169Slclee 			}
34445169Slclee 			continue;
34455169Slclee 		}
34465169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
34475169Slclee 			(void) fprintf(stderr,
34487563SPrasad.Singamsetty@Sun.COM 			    "slice %d (start %llu) is larger than the "
34497563SPrasad.Singamsetty@Sun.COM 			    "partition", i, disk_vtoc.v_part[i].p_start);
34505169Slclee 			if (!io_nifdisk) {
34515169Slclee 				(void) printf(" remove ?:");
34525169Slclee 				if (yesno()) {
34535169Slclee 					disk_vtoc.v_part[i].p_size = 0;
34545169Slclee 					disk_vtoc.v_part[i].p_start = 0;
34555169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
34565169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
34570Sstevel@tonic-gate 				}
34585169Slclee 			} else {
34595169Slclee 				disk_vtoc.v_part[i].p_size = 0;
34605169Slclee 				disk_vtoc.v_part[i].p_start = 0;
34615169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
34625169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
34635169Slclee 				(void) fprintf(stderr,
34645169Slclee 				" removed!\n");
34655169Slclee 			}
34665169Slclee 		} else if (disk_vtoc.v_part[i].p_start
34675169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
34685169Slclee 			(void) fprintf(stderr,
34697563SPrasad.Singamsetty@Sun.COM 			    "slice %d (end %llu) is larger"
34705169Slclee 			    " than the partition",
34715169Slclee 			    i,
34725169Slclee 			    disk_vtoc.v_part[i].p_start +
34735169Slclee 			    disk_vtoc.v_part[i].p_size);
34745169Slclee 			if (!io_nifdisk) {
34755169Slclee 				(void) printf(" adjust ?:");
34765169Slclee 				if (yesno()) {
34775169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
34785169Slclee 				}
34795169Slclee 			} else {
34805169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
34815169Slclee 				(void) fprintf(stderr, " adjusted!\n");
34820Sstevel@tonic-gate 			}
34830Sstevel@tonic-gate 		}
34840Sstevel@tonic-gate 	}
34850Sstevel@tonic-gate #if 1		/* bh for now */
34860Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
34870Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
34880Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
34890Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
34900Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
34910Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
34920Sstevel@tonic-gate #endif
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
34950Sstevel@tonic-gate 	if (!io_readonly)
3496251Slclee 		(void) writevtoc();
34970Sstevel@tonic-gate }
34980Sstevel@tonic-gate 
34990Sstevel@tonic-gate /*
35000Sstevel@tonic-gate  * yesno
35010Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
35020Sstevel@tonic-gate  */
35030Sstevel@tonic-gate 
3504251Slclee static int
3505251Slclee yesno(void)
35060Sstevel@tonic-gate {
35070Sstevel@tonic-gate 	char	s[80];
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	for (;;) {
35109786SBarry.Harding@Sun.COM 		(void) fgets(s, sizeof (s), stdin);
35110Sstevel@tonic-gate 		rm_blanks(s);
35129786SBarry.Harding@Sun.COM 		if (((s[1] != '\0') && (s[1] != '\n')) ||
35139786SBarry.Harding@Sun.COM 		    ((s[0] != 'y') && (s[0] != 'n'))) {
3514251Slclee 			(void) printf(E_LINE);
3515251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
35160Sstevel@tonic-gate 			continue;
35170Sstevel@tonic-gate 		}
35180Sstevel@tonic-gate 		if (s[0] == 'y')
35190Sstevel@tonic-gate 			return (1);
35200Sstevel@tonic-gate 		else
35210Sstevel@tonic-gate 			return (0);
35220Sstevel@tonic-gate 	}
35230Sstevel@tonic-gate }
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate /*
35260Sstevel@tonic-gate  * readvtoc
35270Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
35280Sstevel@tonic-gate  */
3529251Slclee static int
3530251Slclee readvtoc(void)
35310Sstevel@tonic-gate {
35320Sstevel@tonic-gate 	int	i;
35330Sstevel@tonic-gate 	int	retval = VTOC_OK;
35340Sstevel@tonic-gate 
35357563SPrasad.Singamsetty@Sun.COM 	if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
35360Sstevel@tonic-gate 		if (i == VT_EINVAL) {
35370Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
35380Sstevel@tonic-gate 			vt_inval++;
35390Sstevel@tonic-gate 			retval = VTOC_INVAL;
35400Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
35410Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
35425169Slclee 			    "GPT\n");
35430Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
35440Sstevel@tonic-gate 		} else {
35450Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
35460Sstevel@tonic-gate 			retval = VTOC_RWERR;
35470Sstevel@tonic-gate 		}
35480Sstevel@tonic-gate 	}
35490Sstevel@tonic-gate 	return (retval);
35500Sstevel@tonic-gate }
35510Sstevel@tonic-gate 
35520Sstevel@tonic-gate /*
35530Sstevel@tonic-gate  * writevtoc
35540Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
35550Sstevel@tonic-gate  */
3556251Slclee static int
3557251Slclee writevtoc(void)
35580Sstevel@tonic-gate {
35590Sstevel@tonic-gate 	int	i;
35600Sstevel@tonic-gate 	int	retval = 0;
35610Sstevel@tonic-gate 
35627563SPrasad.Singamsetty@Sun.COM 	if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
35630Sstevel@tonic-gate 		if (i == VT_EINVAL) {
35640Sstevel@tonic-gate 			(void) fprintf(stderr,
35650Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
35660Sstevel@tonic-gate 			retval = VTOC_INVAL;
35670Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
35680Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
35695169Slclee 			    "GPT\n");
35700Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
35710Sstevel@tonic-gate 		} else {
35720Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
35730Sstevel@tonic-gate 			retval = VTOC_RWERR;
35740Sstevel@tonic-gate 		}
35750Sstevel@tonic-gate 	}
35760Sstevel@tonic-gate 	return (retval);
35770Sstevel@tonic-gate }
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate /*
35800Sstevel@tonic-gate  * efi_ioctl
35810Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
35820Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
35830Sstevel@tonic-gate  */
35840Sstevel@tonic-gate static int
35850Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
35860Sstevel@tonic-gate {
35870Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
35880Sstevel@tonic-gate 	int error;
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
35910Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	return (error);
35940Sstevel@tonic-gate }
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate /*
35970Sstevel@tonic-gate  * clear_efi
35980Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
35990Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
36000Sstevel@tonic-gate  */
3601251Slclee static int
3602251Slclee clear_efi(void)
36030Sstevel@tonic-gate {
36040Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
36050Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	/*
36080Sstevel@tonic-gate 	 * see if we can read the EFI label
36090Sstevel@tonic-gate 	 */
36100Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
36110Sstevel@tonic-gate 		return (VT_ERROR);
36120Sstevel@tonic-gate 	}
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	/*
36150Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
36160Sstevel@tonic-gate 	 */
36170Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
36180Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
36190Sstevel@tonic-gate 
36200Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
36210Sstevel@tonic-gate 		return (VT_ERROR);
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate 	/*
36250Sstevel@tonic-gate 	 * clear the primary label
36260Sstevel@tonic-gate 	 */
36270Sstevel@tonic-gate 	if (io_debug) {
3628251Slclee 		(void) fprintf(stderr,
3629251Slclee 		    "\tClearing primary EFI label at block %lld\n",
3630251Slclee 		    dk_ioc.dki_lba);
36310Sstevel@tonic-gate 	}
36320Sstevel@tonic-gate 
36330Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
36340Sstevel@tonic-gate 		free(dk_ioc.dki_data);
36350Sstevel@tonic-gate 		switch (errno) {
36360Sstevel@tonic-gate 			case EIO:
36370Sstevel@tonic-gate 				return (VT_EIO);
36380Sstevel@tonic-gate 			case EINVAL:
36390Sstevel@tonic-gate 				return (VT_EINVAL);
36400Sstevel@tonic-gate 			default:
36410Sstevel@tonic-gate 				return (VT_ERROR);
36420Sstevel@tonic-gate 		}
36430Sstevel@tonic-gate 	}
36440Sstevel@tonic-gate 
36450Sstevel@tonic-gate 	/*
36460Sstevel@tonic-gate 	 * clear the backup partition table
36470Sstevel@tonic-gate 	 */
36480Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
36490Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
3650*9889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data +
3651*9889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
36520Sstevel@tonic-gate 	if (io_debug) {
3653251Slclee 		(void) fprintf(stderr,
3654251Slclee 		    "\tClearing backup partition table at block %lld\n",
3655251Slclee 		    dk_ioc.dki_lba);
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
36590Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
36605169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
36615169Slclee 		    errno);
36620Sstevel@tonic-gate 	}
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 	/*
36650Sstevel@tonic-gate 	 * clear the backup label
36660Sstevel@tonic-gate 	 */
36670Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
36680Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
3669*9889SLarry.Liu@Sun.COM 	dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data -
3670*9889SLarry.Liu@Sun.COM 	    efi_vtoc->efi_lbasize);
36710Sstevel@tonic-gate 	if (io_debug) {
3672251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
3673251Slclee 		    "%lld\n", dk_ioc.dki_lba);
36740Sstevel@tonic-gate 	}
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
3677251Slclee 		(void) fprintf(stderr,
3678251Slclee 		    "\tUnable to clear backup EFI label at "
3679251Slclee 		    "block %llu; errno %d\n",
3680251Slclee 		    efi_vtoc->efi_last_lba,
3681251Slclee 		    errno);
36820Sstevel@tonic-gate 	}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	free(dk_ioc.dki_data);
36850Sstevel@tonic-gate 	efi_free(efi_vtoc);
36860Sstevel@tonic-gate 
36870Sstevel@tonic-gate 	return (0);
36880Sstevel@tonic-gate }
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate /*
36910Sstevel@tonic-gate  * clear_vtoc
36920Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
36930Sstevel@tonic-gate  *      device.
36940Sstevel@tonic-gate  */
3695251Slclee static void
36960Sstevel@tonic-gate clear_vtoc(int table, int part)
36970Sstevel@tonic-gate {
36980Sstevel@tonic-gate 	struct ipart *clr_table;
3699*9889SLarry.Liu@Sun.COM 	char *disk_label;
37007563SPrasad.Singamsetty@Sun.COM 	uint32_t pcyl, ncyl, count;
37017563SPrasad.Singamsetty@Sun.COM 	diskaddr_t backup_block, solaris_offset;
37027563SPrasad.Singamsetty@Sun.COM 	ssize_t bytes;
37036549Sbharding 	off_t seek_byte;
37040Sstevel@tonic-gate 
37050Sstevel@tonic-gate #ifdef DEBUG
3706*9889SLarry.Liu@Sun.COM 	char *read_label;
37070Sstevel@tonic-gate #endif /* DEBUG */
37080Sstevel@tonic-gate 
37090Sstevel@tonic-gate 	if (table == OLD) {
37100Sstevel@tonic-gate 		clr_table = &Old_Table[part];
37110Sstevel@tonic-gate 	} else {
37120Sstevel@tonic-gate 		clr_table = &Table[part];
37130Sstevel@tonic-gate 	}
37140Sstevel@tonic-gate 
3715*9889SLarry.Liu@Sun.COM 	disk_label = (char *)calloc(sectsiz, 1);
3716*9889SLarry.Liu@Sun.COM 	if (disk_label == NULL) {
3717*9889SLarry.Liu@Sun.COM 		return;
3718*9889SLarry.Liu@Sun.COM 	}
37190Sstevel@tonic-gate 
37208333SSuhasini.Peddada@Sun.COM 	seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
37210Sstevel@tonic-gate 
37220Sstevel@tonic-gate 	if (io_debug) {
37236549Sbharding 		(void) fprintf(stderr,
37246549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
37256549Sbharding 		    (uint64_t)seek_byte,
37268333SSuhasini.Peddada@Sun.COM 		    (uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET));
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3730251Slclee 		(void) fprintf(stderr,
37316549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
37326549Sbharding 		    (uint64_t)seek_byte);
3733*9889SLarry.Liu@Sun.COM 		free(disk_label);
3734251Slclee 		return;
37350Sstevel@tonic-gate 	}
37360Sstevel@tonic-gate 
3737*9889SLarry.Liu@Sun.COM 	bytes = write(Dev, disk_label, sectsiz);
3738*9889SLarry.Liu@Sun.COM 
3739*9889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
3740251Slclee 		(void) fprintf(stderr,
37417563SPrasad.Singamsetty@Sun.COM 		    "\tWarning: only %d bytes written to clear primary"
37427563SPrasad.Singamsetty@Sun.COM 		    " VTOC!\n", bytes);
37430Sstevel@tonic-gate 	}
37440Sstevel@tonic-gate 
37450Sstevel@tonic-gate #ifdef DEBUG
37460Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3747251Slclee 		(void) fprintf(stderr,
37486549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
37496549Sbharding 		    (uint64_t)seek_byte);
3750*9889SLarry.Liu@Sun.COM 		free(disk_label);
3751251Slclee 		return;
37520Sstevel@tonic-gate 	} else {
37536549Sbharding 		(void) fprintf(stderr,
37546549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
37556549Sbharding 		    (uint64_t)seek_byte);
37560Sstevel@tonic-gate 	}
37570Sstevel@tonic-gate 
3758*9889SLarry.Liu@Sun.COM 	read_label = (char *)calloc(sectsiz, 1);
3759*9889SLarry.Liu@Sun.COM 	if (read_label == NULL) {
3760*9889SLarry.Liu@Sun.COM 		free(disk_label);
3761*9889SLarry.Liu@Sun.COM 		return;
3762*9889SLarry.Liu@Sun.COM 	}
3763*9889SLarry.Liu@Sun.COM 
3764*9889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
3765*9889SLarry.Liu@Sun.COM 
3766*9889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
3767251Slclee 		(void) fprintf(stderr,
3768251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
37690Sstevel@tonic-gate 		    bytes);
37700Sstevel@tonic-gate 	}
37710Sstevel@tonic-gate 
3772*9889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
3773251Slclee 		(void) fprintf(stderr,
3774251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
37750Sstevel@tonic-gate 	} else {
3776251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
37770Sstevel@tonic-gate 		    "read_label\n");
37780Sstevel@tonic-gate 	}
37790Sstevel@tonic-gate #endif /* DEBUG */
37800Sstevel@tonic-gate 
37810Sstevel@tonic-gate 	/* Clear backup label */
37828333SSuhasini.Peddada@Sun.COM 	pcyl = lel(clr_table->numsect) / (heads * sectors);
37838333SSuhasini.Peddada@Sun.COM 	solaris_offset = lel(clr_table->relsect);
37840Sstevel@tonic-gate 	ncyl = pcyl - acyl;
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
37870Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
37880Sstevel@tonic-gate 
37890Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
3790*9889SLarry.Liu@Sun.COM 		seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz;
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3793251Slclee 			(void) fprintf(stderr,
37946549Sbharding 			    "\tError seeking to backup label at byte %llu on "
37956549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
3796*9889SLarry.Liu@Sun.COM 			free(disk_label);
3797*9889SLarry.Liu@Sun.COM #ifdef DEBUG
3798*9889SLarry.Liu@Sun.COM 			free(read_label);
3799*9889SLarry.Liu@Sun.COM #endif /* DEBUG */
3800251Slclee 			return;
38010Sstevel@tonic-gate 		}
38020Sstevel@tonic-gate 
38030Sstevel@tonic-gate 		if (io_debug) {
3804251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
38056549Sbharding 			    " byte %llu (block %llu)\n",
38066549Sbharding 			    (uint64_t)seek_byte,
38076549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
38080Sstevel@tonic-gate 		}
38090Sstevel@tonic-gate 
3810*9889SLarry.Liu@Sun.COM 		bytes = write(Dev, disk_label, sectsiz);
3811*9889SLarry.Liu@Sun.COM 
3812*9889SLarry.Liu@Sun.COM 		if (bytes != sectsiz) {
3813251Slclee 			(void) fprintf(stderr,
3814251Slclee 			    "\t\tWarning: only %d bytes written to "
38156549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
38166549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
38170Sstevel@tonic-gate 		}
38180Sstevel@tonic-gate 
38190Sstevel@tonic-gate #ifdef DEBUG
38200Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3821251Slclee 		(void) fprintf(stderr,
38226549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
38236549Sbharding 		    (uint64_t)seek_byte);
3824*9889SLarry.Liu@Sun.COM 		free(disk_label);
3825*9889SLarry.Liu@Sun.COM 		free(read_label);
3826251Slclee 		return;
38270Sstevel@tonic-gate 	} else {
38286549Sbharding 		(void) fprintf(stderr,
38296549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
38306549Sbharding 		    (uint64_t)seek_byte);
38310Sstevel@tonic-gate 	}
38320Sstevel@tonic-gate 
3833*9889SLarry.Liu@Sun.COM 	bytes = read(Dev, read_label, sectsiz);
3834*9889SLarry.Liu@Sun.COM 
3835*9889SLarry.Liu@Sun.COM 	if (bytes != sectsiz) {
3836251Slclee 		(void) fprintf(stderr,
3837251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
3838251Slclee 		    bytes);
38390Sstevel@tonic-gate 	}
38400Sstevel@tonic-gate 
3841*9889SLarry.Liu@Sun.COM 	if (memcmp(disk_label, read_label, sectsiz) != 0) {
3842251Slclee 		(void) fprintf(stderr,
3843251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
38440Sstevel@tonic-gate 	} else {
3845251Slclee 		(void) fprintf(stderr,
3846251Slclee 		    "DEBUG: Good compare of disk_label and backup "
38470Sstevel@tonic-gate 		    "read_label\n");
38480Sstevel@tonic-gate 	}
3849*9889SLarry.Liu@Sun.COM 
38500Sstevel@tonic-gate #endif /* DEBUG */
38510Sstevel@tonic-gate 
38520Sstevel@tonic-gate 		backup_block += 2;
38530Sstevel@tonic-gate 	}
3854*9889SLarry.Liu@Sun.COM 
3855*9889SLarry.Liu@Sun.COM #ifdef DEBUG
3856*9889SLarry.Liu@Sun.COM 	free(read_label);
3857*9889SLarry.Liu@Sun.COM #endif /* DEBUG */
3858*9889SLarry.Liu@Sun.COM 	free(disk_label);
38590Sstevel@tonic-gate }
38600Sstevel@tonic-gate 
38610Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
38620Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
38630Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
38640Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
38650Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
38660Sstevel@tonic-gate 
38670Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
38680Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
38690Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
38700Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
38710Sstevel@tonic-gate 
38720Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
38730Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
38740Sstevel@tonic-gate 
38750Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
38760Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
38770Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
38780Sstevel@tonic-gate 
38790Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
38800Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
38810Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
38820Sstevel@tonic-gate 
38830Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
38840Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
38850Sstevel@tonic-gate 
38860Sstevel@tonic-gate /*
38870Sstevel@tonic-gate  *  lecture_and_query
38880Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
38890Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
38900Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
38910Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
38920Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
38930Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
38940Sstevel@tonic-gate  */
3895251Slclee static int
38960Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
38970Sstevel@tonic-gate {
38980Sstevel@tonic-gate 	if (io_nifdisk)
38990Sstevel@tonic-gate 		return (0);
39000Sstevel@tonic-gate 
3901251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
3902251Slclee 	(void) fprintf(stderr, "%s", warning);
3903251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
3904251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate 	return (yesno());
39070Sstevel@tonic-gate }
39080Sstevel@tonic-gate 
3909251Slclee static void
39100Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
39110Sstevel@tonic-gate {
39127563SPrasad.Singamsetty@Sun.COM 	struct extvtoc v;
39130Sstevel@tonic-gate 	struct dk_geom d;
39140Sstevel@tonic-gate 	struct part_info pi;
39157563SPrasad.Singamsetty@Sun.COM 	struct extpart_info extpi;
39167563SPrasad.Singamsetty@Sun.COM 	diskaddr_t totsize;
39170Sstevel@tonic-gate 	int idx = -1;
39180Sstevel@tonic-gate 
39190Sstevel@tonic-gate 	/*
39200Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
39210Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
39220Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
39230Sstevel@tonic-gate 	 */
39247563SPrasad.Singamsetty@Sun.COM 	if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
39257563SPrasad.Singamsetty@Sun.COM 		if (extpi.p_start != 0) {
39267563SPrasad.Singamsetty@Sun.COM 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
39277563SPrasad.Singamsetty@Sun.COM 			    devname)) {
39287563SPrasad.Singamsetty@Sun.COM 				(void) close(fd);
39297563SPrasad.Singamsetty@Sun.COM 				exit(1);
39307563SPrasad.Singamsetty@Sun.COM 			}
39317563SPrasad.Singamsetty@Sun.COM 		}
39327563SPrasad.Singamsetty@Sun.COM 	} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
39330Sstevel@tonic-gate 		if (pi.p_start != 0) {
39340Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
39350Sstevel@tonic-gate 			    devname)) {
39360Sstevel@tonic-gate 				(void) close(fd);
39370Sstevel@tonic-gate 				exit(1);
39380Sstevel@tonic-gate 			}
39390Sstevel@tonic-gate 		}
39400Sstevel@tonic-gate 	} else {
39417563SPrasad.Singamsetty@Sun.COM 		if ((idx = read_extvtoc(fd, &v)) < 0) {
39420Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
39430Sstevel@tonic-gate 			    devname)) {
39440Sstevel@tonic-gate 				(void) close(fd);
39450Sstevel@tonic-gate 				exit(1);
39460Sstevel@tonic-gate 			}
39470Sstevel@tonic-gate 			return;
39480Sstevel@tonic-gate 		}
39490Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
39500Sstevel@tonic-gate 			perror(devname);
39510Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
39520Sstevel@tonic-gate 			    devname)) {
39530Sstevel@tonic-gate 				(void) close(fd);
39540Sstevel@tonic-gate 				exit(1);
39550Sstevel@tonic-gate 			}
39560Sstevel@tonic-gate 			return;
39570Sstevel@tonic-gate 		}
39587563SPrasad.Singamsetty@Sun.COM 		totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
39590Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
39600Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
39610Sstevel@tonic-gate 			    devname)) {
39620Sstevel@tonic-gate 				(void) close(fd);
39630Sstevel@tonic-gate 				exit(1);
39640Sstevel@tonic-gate 			}
39650Sstevel@tonic-gate 		}
39660Sstevel@tonic-gate 	}
39670Sstevel@tonic-gate }
39680Sstevel@tonic-gate 
39690Sstevel@tonic-gate 
39700Sstevel@tonic-gate /*
39710Sstevel@tonic-gate  * get_node
39720Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
39730Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
39740Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
39750Sstevel@tonic-gate  */
39760Sstevel@tonic-gate static char *
39770Sstevel@tonic-gate get_node(char *devname)
39780Sstevel@tonic-gate {
39790Sstevel@tonic-gate 	char *node;
39800Sstevel@tonic-gate 	struct stat statbuf;
39810Sstevel@tonic-gate 	size_t space;
39820Sstevel@tonic-gate 
39830Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
39840Sstevel@tonic-gate 	if (io_image)
39850Sstevel@tonic-gate 		return (devname);
39860Sstevel@tonic-gate 
39870Sstevel@tonic-gate 	node = devname;
39880Sstevel@tonic-gate 
39890Sstevel@tonic-gate 	/* Try the node as provided first */
39900Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
39910Sstevel@tonic-gate 		/*
39920Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
39930Sstevel@tonic-gate 		 * default path and try again.
39940Sstevel@tonic-gate 		 */
39950Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
39960Sstevel@tonic-gate 
39970Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
3998251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
39990Sstevel@tonic-gate 			    "for device node.\n");
40000Sstevel@tonic-gate 			exit(1);
40010Sstevel@tonic-gate 		}
40020Sstevel@tonic-gate 
40030Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
40040Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
40050Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
40060Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate 		/* Try to stat it again */
40090Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
40100Sstevel@tonic-gate 			/* Failed all options, give up */
4011251Slclee 			(void) fprintf(stderr,
4012251Slclee 			    "fdisk: Cannot stat device %s.\n",
40130Sstevel@tonic-gate 			    devname);
40140Sstevel@tonic-gate 			exit(1);
40150Sstevel@tonic-gate 		}
40160Sstevel@tonic-gate 	}
40170Sstevel@tonic-gate 
40180Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
40190Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
4020251Slclee 		(void) fprintf(stderr,
4021251Slclee 		    "fdisk: %s must be a raw device.\n", node);
40220Sstevel@tonic-gate 		exit(1);
40230Sstevel@tonic-gate 	}
40240Sstevel@tonic-gate 
40250Sstevel@tonic-gate 	return (node);
40260Sstevel@tonic-gate }
4027