xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 8333:de8038a7796e)
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 /*
235936Sbharding  * Copyright 2008 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  */
41*8333SSuhasini.Peddada@Sun.COM 
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <errno.h>
470Sstevel@tonic-gate #include <fcntl.h>
480Sstevel@tonic-gate #include <ctype.h>
490Sstevel@tonic-gate #include <sys/stat.h>
500Sstevel@tonic-gate #include <sys/types.h>
517563SPrasad.Singamsetty@Sun.COM #include <limits.h>
520Sstevel@tonic-gate #include <sys/param.h>
530Sstevel@tonic-gate #include <sys/systeminfo.h>
540Sstevel@tonic-gate #include <sys/efi_partition.h>
550Sstevel@tonic-gate #include <sys/byteorder.h>
560Sstevel@tonic-gate #include <sys/systeminfo.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
590Sstevel@tonic-gate #include <sys/dkio.h>
600Sstevel@tonic-gate #include <sys/vtoc.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #define	CLR_SCR ""
630Sstevel@tonic-gate #define	CLR_LIN ""
640Sstevel@tonic-gate #define	HOME "" \
650Sstevel@tonic-gate 	""
660Sstevel@tonic-gate #define	Q_LINE ""
670Sstevel@tonic-gate #define	W_LINE ""
680Sstevel@tonic-gate #define	E_LINE ""
690Sstevel@tonic-gate #define	M_LINE "" \
700Sstevel@tonic-gate 	""
710Sstevel@tonic-gate #define	T_LINE ""
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
740Sstevel@tonic-gate 
75*8333SSuhasini.Peddada@Sun.COM /* XXX - should be in fdisk.h, used by sd as well */
76*8333SSuhasini.Peddada@Sun.COM 
77*8333SSuhasini.Peddada@Sun.COM /*
78*8333SSuhasini.Peddada@Sun.COM  * the MAX values are the maximum usable values for BIOS chs values
79*8333SSuhasini.Peddada@Sun.COM  * The MAX_CYL value of 1022 is the maximum usable value
80*8333SSuhasini.Peddada@Sun.COM  *   the value of 1023 is a fence value,
81*8333SSuhasini.Peddada@Sun.COM  *   indicating no CHS geometry exists for the corresponding LBA value.
82*8333SSuhasini.Peddada@Sun.COM  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
83*8333SSuhasini.Peddada@Sun.COM  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
84*8333SSuhasini.Peddada@Sun.COM  */
85*8333SSuhasini.Peddada@Sun.COM #define	MAX_SECT	(63)
86*8333SSuhasini.Peddada@Sun.COM #define	MAX_CYL		(1022)
87*8333SSuhasini.Peddada@Sun.COM #define	MAX_HEAD	(254)
88*8333SSuhasini.Peddada@Sun.COM 
897563SPrasad.Singamsetty@Sun.COM #define	DK_MAX_2TB	UINT32_MAX	/* Max # of sectors in 2TB */
907563SPrasad.Singamsetty@Sun.COM 
910Sstevel@tonic-gate /* for clear_vtoc() */
920Sstevel@tonic-gate #define	OLD		0
930Sstevel@tonic-gate #define	NEW		1
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
960Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
970Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
980Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
990Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
1000Sstevel@tonic-gate 
101*8333SSuhasini.Peddada@Sun.COM /*
102*8333SSuhasini.Peddada@Sun.COM  * Support for fdisk(1M) on the SPARC platform
103*8333SSuhasini.Peddada@Sun.COM  *	In order to convert little endian values to big endian for SPARC,
104*8333SSuhasini.Peddada@Sun.COM  *	byte/short and long values must be swapped.
105*8333SSuhasini.Peddada@Sun.COM  *	These swapping macros will be used to access information in the
106*8333SSuhasini.Peddada@Sun.COM  *	mboot and ipart structures.
107*8333SSuhasini.Peddada@Sun.COM  */
108*8333SSuhasini.Peddada@Sun.COM 
109*8333SSuhasini.Peddada@Sun.COM #ifdef sparc
110*8333SSuhasini.Peddada@Sun.COM #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
111*8333SSuhasini.Peddada@Sun.COM #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
112*8333SSuhasini.Peddada@Sun.COM 			    (les((unsigned)((val)&0xffff0000)>>16)))
113*8333SSuhasini.Peddada@Sun.COM #else
114*8333SSuhasini.Peddada@Sun.COM #define	les(val)	(val)
115*8333SSuhasini.Peddada@Sun.COM #define	lel(val)	(val)
116*8333SSuhasini.Peddada@Sun.COM #endif
117*8333SSuhasini.Peddada@Sun.COM 
1180Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1196549Sbharding #define	VTOC_OFFSET	1
1200Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1210Sstevel@tonic-gate #define	VTOC_OFFSET	0
1220Sstevel@tonic-gate #else
1230Sstevel@tonic-gate #error No VTOC format defined.
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate 
126251Slclee static char Usage[] = "Usage: fdisk\n"
1270Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1280Sstevel@tonic-gate "[ -b masterboot ]\n"
1290Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1300Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1310Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1320Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1330Sstevel@tonic-gate 
134251Slclee static char Usage1[] = "    Partition options:\n"
1350Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1360Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1370Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1380Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1390Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1400Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1410Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1420Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1430Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1440Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1450Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1460Sstevel@tonic-gate "			  start of partition\n"
1470Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1480Sstevel@tonic-gate "	-b master_boot\n"
1490Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1500Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1510Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1520Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1530Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1540Sstevel@tonic-gate "	-F fdisk_file\n"
1550Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1560Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1570Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1580Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1590Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1600Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1610Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1620Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1630Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1640Sstevel@tonic-gate "	-W fdisk_file\n"
1650Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1660Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1670Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
1680Sstevel@tonic-gate "    Diagnostic options:\n"
1690Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
1700Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
1710Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
1720Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
1730Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
1740Sstevel@tonic-gate "		BCYL		cylinder offset\n"
1750Sstevel@tonic-gate "		NHEADS		number of heads\n"
1760Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
1770Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
1780Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
1790Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
1800Sstevel@tonic-gate "	-o offset\n"
1810Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
1820Sstevel@tonic-gate "		-P # specified.\n"
1830Sstevel@tonic-gate "	-P fill_patt\n"
1840Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
1850Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
1860Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
1870Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
1880Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
1890Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
1900Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
1910Sstevel@tonic-gate "	-S geom_file\n"
1920Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
1930Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
1940Sstevel@tonic-gate 
195251Slclee static char Ostr[] = "Other OS";
196251Slclee static char Dstr[] = "DOS12";
197251Slclee static char D16str[] = "DOS16";
198251Slclee static char DDstr[] = "DOS-DATA";
199251Slclee static char EDstr[] = "EXT-DOS";
200251Slclee static char DBstr[] = "DOS-BIG";
201251Slclee static char PCstr[] = "PCIX";
202251Slclee static char Ustr[] = "UNIX System";
203251Slclee static char SUstr[] = "Solaris";
204251Slclee static char SU2str[] = "Solaris2";
205251Slclee static char X86str[] = "x86 Boot";
206251Slclee static char DIAGstr[] = "Diagnostic";
207251Slclee static char IFSstr[] = "IFS: NTFS";
208251Slclee static char AIXstr[] = "AIX Boot";
209251Slclee static char AIXDstr[] = "AIX Data";
210251Slclee static char OS2str[] = "OS/2 Boot";
211251Slclee static char WINstr[] = "Win95 FAT32";
212251Slclee static char EWINstr[] = "Ext Win95";
213251Slclee static char FAT95str[] = "FAT16 LBA";
214251Slclee static char EXTLstr[] = "EXT LBA";
215251Slclee static char LINUXstr[] = "Linux";
216251Slclee static char CPMstr[] = "CP/M";
217251Slclee static char NOVstr[] = "Netware 3.x+";
218251Slclee static char QNXstr[] = "QNX 4.x";
219251Slclee static char QNX2str[] = "QNX part 2";
220251Slclee static char QNX3str[] = "QNX part 3";
221251Slclee static char LINNATstr[] = "Linux native";
222251Slclee static char NTFSVOL1str[] = "NT volset 1";
223251Slclee static char NTFSVOL2str[] = "NT volset 2";
224251Slclee static char BSDstr[] = "BSD OS";
225251Slclee static char NEXTSTEPstr[] = "NeXTSTEP";
226251Slclee static char BSDIFSstr[] = "BSDI FS";
227251Slclee static char BSDISWAPstr[] = "BSDI swap";
228251Slclee static char Actvstr[] = "Active";
229251Slclee static char EFIstr[] = "EFI";
230251Slclee static char NAstr[] = "      ";
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate /* All the user options and flags */
233251Slclee static char *Dfltdev;			/* name of fixed disk drive */
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate /* Diagnostic options */
236251Slclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
237251Slclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
238251Slclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
239251Slclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
240251Slclee static int	io_lgeom = 0;		/* get label geometry (-g) */
241251Slclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
242251Slclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
243251Slclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2460Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
2476549Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
2486549Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /* Partition table flags */
251251Slclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
252251Slclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
253251Slclee static int	io_fdisk = 0;		/* do fdisk operation */
254251Slclee static int	io_ifdisk = 0;		/* interactive partition */
255251Slclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
256251Slclee 
257251Slclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
258251Slclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
259251Slclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
260251Slclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
261251Slclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
262251Slclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
263251Slclee 
264251Slclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
265251Slclee 
266251Slclee static struct mboot BootCod;		/* buffer for master boot record */
267251Slclee 
268251Slclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
269251Slclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
270251Slclee static int	io_debug = 0;		/* activate verbose mode (-d) */
271251Slclee static int	io_image = 0;		/* create image using geometry (-I) */
272251Slclee 
273251Slclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
274251Slclee static char	*Bootsect;		/* pointer to sector zero buffer */
275251Slclee static char	*Nullsect;
2767563SPrasad.Singamsetty@Sun.COM static struct extvtoc	disk_vtoc;	/* verify VTOC table */
277251Slclee static int	vt_inval = 0;
278251Slclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
279251Slclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
280251Slclee 
281251Slclee static struct ipart	Table[FD_NUMPART];
282251Slclee static struct ipart	Old_Table[FD_NUMPART];
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);
332251Slclee static int verify_tbl(void);
333251Slclee static int pars_fdisk(char *line,
334251Slclee     int *id, int *act,
335251Slclee     int *bhead, int *bsect, int *bcyl,
336251Slclee     int *ehead, int *esect, int *ecyl,
3377563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect);
3387563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect);
339251Slclee static void stage0(void);
340251Slclee static int pcreate(void);
341251Slclee static int specify(uchar_t tsystid);
342251Slclee static void dispmenu(void);
343251Slclee static int pchange(void);
344251Slclee static int ppartid(void);
345251Slclee static char pdelete(void);
346251Slclee static void rm_blanks(char *s);
347251Slclee static int getcyl(void);
348251Slclee static void disptbl(void);
349251Slclee static void print_Table(void);
350251Slclee static void copy_Table_to_Old_Table(void);
351251Slclee static void nulltbl(void);
352251Slclee static void copy_Bootblk_to_Table(void);
353251Slclee static void fill_ipart(char *bootptr, struct ipart *partp);
354251Slclee #ifdef sparc
355251Slclee uchar_t getbyte(char **bp);
356251Slclee uint32_t getlong(char **bp);
357251Slclee #endif
358251Slclee static void copy_Table_to_Bootblk(void);
359251Slclee static int TableChanged(void);
360251Slclee static void ffile_write(char *file);
361251Slclee static void fix_slice(void);
362251Slclee static int yesno(void);
363251Slclee static int readvtoc(void);
364251Slclee static int writevtoc(void);
365251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
366251Slclee static int clear_efi(void);
367251Slclee static void clear_vtoc(int table, int part);
368251Slclee static int lecture_and_query(char *warning, char *devname);
369251Slclee static void sanity_check_provided_device(char *devname, int fd);
370251Slclee static char *get_node(char *devname);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate static void
3730Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	if (table_changed) {
3760Sstevel@tonic-gate 		/*
3770Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
3780Sstevel@tonic-gate 		 * and write it to disk
3790Sstevel@tonic-gate 		 */
3800Sstevel@tonic-gate 		copy_Table_to_Bootblk();
3810Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
3850Sstevel@tonic-gate 	if (io_adjt)
3860Sstevel@tonic-gate 		fix_slice();
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	exit(0);
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
391*8333SSuhasini.Peddada@Sun.COM 
392*8333SSuhasini.Peddada@Sun.COM 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * main
3950Sstevel@tonic-gate  * Process command-line options.
3960Sstevel@tonic-gate  */
397251Slclee int
3980Sstevel@tonic-gate main(int argc, char *argv[])
3990Sstevel@tonic-gate {
400251Slclee 	int c, i;
4010Sstevel@tonic-gate 	extern	int optind;
4020Sstevel@tonic-gate 	extern	char *optarg;
4030Sstevel@tonic-gate 	int	errflg = 0;
4040Sstevel@tonic-gate 	int	diag_cnt = 0;
4050Sstevel@tonic-gate 	int openmode;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
4080Sstevel@tonic-gate 	setbuf(stdout, 0);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	/* Process the options. */
4110Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
4120Sstevel@tonic-gate 	    != EOF) {
4130Sstevel@tonic-gate 		switch (c) {
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 			case 'o':
4166549Sbharding 				io_offset = (off_t)strtoull(optarg, 0, 0);
4170Sstevel@tonic-gate 				continue;
4180Sstevel@tonic-gate 			case 's':
4196549Sbharding 				io_size = (off_t)strtoull(optarg, 0, 0);
4200Sstevel@tonic-gate 				continue;
4210Sstevel@tonic-gate 			case 'P':
4220Sstevel@tonic-gate 				diag_cnt++;
4230Sstevel@tonic-gate 				io_patt++;
4240Sstevel@tonic-gate 				io_fatt = optarg;
4250Sstevel@tonic-gate 				continue;
4260Sstevel@tonic-gate 			case 'w':
4270Sstevel@tonic-gate 				diag_cnt++;
4280Sstevel@tonic-gate 				io_wrt++;
4290Sstevel@tonic-gate 				continue;
4300Sstevel@tonic-gate 			case 'r':
4310Sstevel@tonic-gate 				diag_cnt++;
4320Sstevel@tonic-gate 				io_rd++;
4330Sstevel@tonic-gate 				continue;
4340Sstevel@tonic-gate 			case 'd':
4350Sstevel@tonic-gate 				io_debug++;
4360Sstevel@tonic-gate 				continue;
4370Sstevel@tonic-gate 			case 'I':
4380Sstevel@tonic-gate 				io_image++;
4390Sstevel@tonic-gate 				continue;
4400Sstevel@tonic-gate 			case 'R':
4410Sstevel@tonic-gate 				io_readonly++;
4420Sstevel@tonic-gate 				continue;
4430Sstevel@tonic-gate 			case 'S':
4440Sstevel@tonic-gate 				diag_cnt++;
4450Sstevel@tonic-gate 				io_sgeom = optarg;
4460Sstevel@tonic-gate 				continue;
4470Sstevel@tonic-gate 			case 'T':
4480Sstevel@tonic-gate 				io_ADJT++;
449251Slclee 				/* FALLTHRU */
4500Sstevel@tonic-gate 			case 't':
4510Sstevel@tonic-gate 				io_adjt++;
4520Sstevel@tonic-gate 				continue;
4530Sstevel@tonic-gate 			case 'B':
4540Sstevel@tonic-gate 				io_wholedisk++;
4550Sstevel@tonic-gate 				io_fdisk++;
4560Sstevel@tonic-gate 				continue;
4570Sstevel@tonic-gate 			case 'E':
4580Sstevel@tonic-gate 				io_EFIdisk++;
4590Sstevel@tonic-gate 				io_fdisk++;
4600Sstevel@tonic-gate 				continue;
4610Sstevel@tonic-gate 			case 'g':
4620Sstevel@tonic-gate 				diag_cnt++;
4630Sstevel@tonic-gate 				io_lgeom++;
4640Sstevel@tonic-gate 				continue;
4650Sstevel@tonic-gate 			case 'G':
4660Sstevel@tonic-gate 				diag_cnt++;
4670Sstevel@tonic-gate 				io_pgeom++;
4680Sstevel@tonic-gate 				continue;
4690Sstevel@tonic-gate 			case 'n':
4700Sstevel@tonic-gate 				io_nifdisk++;
4710Sstevel@tonic-gate 				io_fdisk++;
4720Sstevel@tonic-gate 				continue;
4730Sstevel@tonic-gate 			case 'F':
4740Sstevel@tonic-gate 				io_fdisk++;
4750Sstevel@tonic-gate 				io_ffdisk = optarg;
4760Sstevel@tonic-gate 				continue;
4770Sstevel@tonic-gate 			case 'b':
4780Sstevel@tonic-gate 				io_mboot = optarg;
4790Sstevel@tonic-gate 				continue;
4800Sstevel@tonic-gate 			case 'W':
4810Sstevel@tonic-gate 				/*
4820Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
4830Sstevel@tonic-gate 				 * to standard output, otherwise write
4840Sstevel@tonic-gate 				 * to the specified file.
4850Sstevel@tonic-gate 				 */
4860Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
4870Sstevel@tonic-gate 					stdo_flag = 1;
4880Sstevel@tonic-gate 				else
4890Sstevel@tonic-gate 					io_Wfdisk = optarg;
4900Sstevel@tonic-gate 				io_fdisk++;
4910Sstevel@tonic-gate 				continue;
4920Sstevel@tonic-gate 			case 'A':
4930Sstevel@tonic-gate 				io_fdisk++;
4940Sstevel@tonic-gate 				io_Afdisk = optarg;
4950Sstevel@tonic-gate 				continue;
4960Sstevel@tonic-gate 			case 'D':
4970Sstevel@tonic-gate 				io_fdisk++;
4980Sstevel@tonic-gate 				io_Dfdisk = optarg;
4990Sstevel@tonic-gate 				continue;
5000Sstevel@tonic-gate 			case 'h':
501251Slclee 				(void) fprintf(stderr, "%s\n", Usage);
502251Slclee 				(void) fprintf(stderr, "%s\n", Usage1);
5030Sstevel@tonic-gate 				exit(0);
504251Slclee 				/* FALLTHRU */
5050Sstevel@tonic-gate 			case 'v':
5060Sstevel@tonic-gate 				v_flag = 1;
5070Sstevel@tonic-gate 				continue;
5080Sstevel@tonic-gate 			case '?':
5090Sstevel@tonic-gate 				errflg++;
5100Sstevel@tonic-gate 				break;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 		break;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
5160Sstevel@tonic-gate 		diag_cnt = 0;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	/* User option checking */
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	/* By default, run in interactive mode */
5220Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
5230Sstevel@tonic-gate 		io_ifdisk++;
5240Sstevel@tonic-gate 		io_fdisk++;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
5270Sstevel@tonic-gate 		errflg++;
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/* Was any error detected? */
5310Sstevel@tonic-gate 	if (errflg || argc == optind) {
532251Slclee 		(void) fprintf(stderr, "%s\n", Usage);
533251Slclee 		(void) fprintf(stderr,
5340Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
5350Sstevel@tonic-gate 		exit(2);
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 	/* Figure out the correct device node to open */
5400Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if (io_readonly)
5430Sstevel@tonic-gate 		openmode = O_RDONLY;
5440Sstevel@tonic-gate 	else
5450Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
548251Slclee 		(void) fprintf(stderr,
549251Slclee 		    "fdisk: Cannot open device %s.\n",
550251Slclee 		    Dfltdev);
5510Sstevel@tonic-gate 		exit(1);
5520Sstevel@tonic-gate 	}
5535169Slclee 	/*
5545169Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
5555169Slclee 	 * in that case leave the minfo structure zeroed
5565169Slclee 	 */
5575169Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
5585169Slclee 		memset(&minfo, 0, sizeof (minfo));
5595169Slclee 	}
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	/* Get the disk geometry */
5620Sstevel@tonic-gate 	if (!io_image) {
5630Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
5640Sstevel@tonic-gate 		errno = 0;
5650Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 			/*
5680Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
5690Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
5700Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
5710Sstevel@tonic-gate 			 */
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate 			if (errno == ENOTTY) {
5740Sstevel@tonic-gate 				v_flag = 0;
5750Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
5760Sstevel@tonic-gate 			} else if (errno == EINVAL) {
5770Sstevel@tonic-gate 				/*
5780Sstevel@tonic-gate 				 * This means that the ioctl exists, but
5790Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
5800Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
5810Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
5820Sstevel@tonic-gate 				 */
5830Sstevel@tonic-gate 				v_flag = 0;
5840Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
5850Sstevel@tonic-gate 			} else {
5860Sstevel@tonic-gate 				(void) fprintf(stderr,
5870Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
5880Sstevel@tonic-gate 				    argv[optind]);
5890Sstevel@tonic-gate 				exit(1);
5900Sstevel@tonic-gate 			}
5910Sstevel@tonic-gate 		} else {
5920Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
5930Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
5940Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
5950Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 		errno = 0;
5990Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
6000Sstevel@tonic-gate 			if (errno == ENOTTY) {
6010Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
6020Sstevel@tonic-gate 			} else {
6030Sstevel@tonic-gate 				(void) fprintf(stderr,
6040Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
6050Sstevel@tonic-gate 				    argv[optind]);
6060Sstevel@tonic-gate 				exit(1);
6070Sstevel@tonic-gate 			}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 		/*
6110Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
6120Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
6130Sstevel@tonic-gate 		 */
6140Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
6150Sstevel@tonic-gate 			errno = 0;
6160Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6170Sstevel@tonic-gate 				(void) fprintf(stderr,
6180Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
6190Sstevel@tonic-gate 				    argv[optind]);
6200Sstevel@tonic-gate 				exit(1);
6210Sstevel@tonic-gate 			}
6220Sstevel@tonic-gate 		}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6250Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6260Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6270Sstevel@tonic-gate 		sectsiz = 512;
6280Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 		/*
6310Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
6320Sstevel@tonic-gate 		 * or we got an invalid hba geometry
6330Sstevel@tonic-gate 		 * then set hba geometry based on max values
6340Sstevel@tonic-gate 		 */
6350Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
636251Slclee 		    disk_geom.dkg_ncyl == 0 ||
637251Slclee 		    disk_geom.dkg_nhead == 0 ||
638251Slclee 		    disk_geom.dkg_nsect == 0 ||
6390Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
6400Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
6410Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 			/*
6440Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
6450Sstevel@tonic-gate 			 */
6460Sstevel@tonic-gate 			v_flag = 0;
6470Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
6480Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
6490Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
6500Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 		if (io_debug) {
654251Slclee 			(void) fprintf(stderr, "Physical Geometry:\n");
655251Slclee 			(void) fprintf(stderr,
6560Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6570Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6580Sstevel@tonic-gate 			    Numcyl,
6590Sstevel@tonic-gate 			    heads,
6600Sstevel@tonic-gate 			    sectors,
6610Sstevel@tonic-gate 			    sectsiz,
662251Slclee 			    Numcyl * heads * sectors,
663251Slclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
664251Slclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
665251Slclee 			(void) fprintf(stderr,
6660Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6670Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6680Sstevel@tonic-gate 			    hba_Numcyl,
6690Sstevel@tonic-gate 			    hba_heads,
6700Sstevel@tonic-gate 			    hba_sectors,
6710Sstevel@tonic-gate 			    sectsiz,
672251Slclee 			    hba_Numcyl * hba_heads * hba_sectors,
673251Slclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
674251Slclee 			    1048576);
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
6790Sstevel@tonic-gate 	if (io_lgeom) {
6800Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6810Sstevel@tonic-gate 			(void) fprintf(stderr,
6820Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
6830Sstevel@tonic-gate 			    argv[optind]);
6840Sstevel@tonic-gate 			exit(1);
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6870Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6880Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6890Sstevel@tonic-gate 		sectsiz = 512;
6900Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
691251Slclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
692251Slclee 		(void) printf(
693251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
6940Sstevel@tonic-gate 		    " SECSIZ\n");
695251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
6960Sstevel@tonic-gate 		    Numcyl,
6970Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
6980Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
6990Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7000Sstevel@tonic-gate 		    heads,
7010Sstevel@tonic-gate 		    sectors,
7020Sstevel@tonic-gate 		    sectsiz);
7030Sstevel@tonic-gate 		exit(0);
7040Sstevel@tonic-gate 	} else if (io_pgeom) {
7050Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
7060Sstevel@tonic-gate 			(void) fprintf(stderr,
7070Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
7080Sstevel@tonic-gate 			    argv[optind]);
7090Sstevel@tonic-gate 			exit(1);
7100Sstevel@tonic-gate 		}
711251Slclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
712251Slclee 		(void) printf(
713251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
7140Sstevel@tonic-gate 		    " SECSIZ\n");
715251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
7160Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
7170Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
7180Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
7190Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7200Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
7210Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
7220Sstevel@tonic-gate 		    sectsiz);
7230Sstevel@tonic-gate 		exit(0);
7240Sstevel@tonic-gate 	} else if (io_sgeom) {
7250Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
7260Sstevel@tonic-gate 			exit(1);
7270Sstevel@tonic-gate 		} else if (!io_image) {
7280Sstevel@tonic-gate 			exit(0);
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 
7325169Slclee 	/*
7335169Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
7345169Slclee 	 * in that case use CHS
7355169Slclee 	 */
7367563SPrasad.Singamsetty@Sun.COM 	chs_capacity = (diskaddr_t)Numcyl * heads * sectors;
7375169Slclee 	dev_capacity = chs_capacity;
7387563SPrasad.Singamsetty@Sun.COM 	Numcyl_usable = Numcyl;
7397563SPrasad.Singamsetty@Sun.COM 
7407563SPrasad.Singamsetty@Sun.COM 	if (chs_capacity > DK_MAX_2TB) {
7417563SPrasad.Singamsetty@Sun.COM 		/* limit to 2TB */
7427563SPrasad.Singamsetty@Sun.COM 		Numcyl_usable = DK_MAX_2TB / (heads * sectors);
7437563SPrasad.Singamsetty@Sun.COM 		chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors;
7447563SPrasad.Singamsetty@Sun.COM 	}
7457563SPrasad.Singamsetty@Sun.COM 
7465169Slclee 	if (minfo.dki_capacity > 0)
7475169Slclee 		dev_capacity = minfo.dki_capacity;
7485169Slclee 
7490Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
7500Sstevel@tonic-gate 	Bootsect = (char *)malloc(3 * sectsiz);
7510Sstevel@tonic-gate 	if (Bootsect == NULL) {
752251Slclee 		(void) fprintf(stderr,
7530Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
7540Sstevel@tonic-gate 		    " (%d bytes).\n",
755251Slclee 		    3 * sectsiz);
7560Sstevel@tonic-gate 		exit(1);
7570Sstevel@tonic-gate 	}
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
7600Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
7610Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
7620Sstevel@tonic-gate 		Nullsect[i] = 0;
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/* Find out what the user wants done */
7660Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
7670Sstevel@tonic-gate 		abs_read();	/* will not return */
7680Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
7690Sstevel@tonic-gate 		abs_write();	/* will not return */
7700Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
7710Sstevel@tonic-gate 		fill_patt();	/* will not return */
7720Sstevel@tonic-gate 	}
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
7800Sstevel@tonic-gate 	mboot_read();
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/* Read from disk master boot */
7830Sstevel@tonic-gate 	dev_mboot_read();
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
7870Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
7880Sstevel@tonic-gate 	 */
7890Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
7900Sstevel@tonic-gate 	copy_Bootblk_to_Table();
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
7930Sstevel@tonic-gate 	copy_Table_to_Old_Table();
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
7960Sstevel@tonic-gate 	if (io_ffdisk) {
7970Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
7980Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
8020Sstevel@tonic-gate 	if (io_Dfdisk) {
8030Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	if (io_Afdisk) {
8060Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
8070Sstevel@tonic-gate 	}
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
8100Sstevel@tonic-gate 		/* Check if there is no fdisk table */
8110Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
8120Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
813251Slclee 				(void) printf(
814251Slclee 				    "No fdisk table exists. The default"
815251Slclee 				    " partition for the disk is:\n\n"
816251Slclee 				    "  a 100%% \"SOLARIS System\" "
817251Slclee 				    "partition\n\n"
818251Slclee 				    "Type \"y\" to accept the default "
8190Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
8200Sstevel@tonic-gate 				    "edit the\n partition table.\n");
8217563SPrasad.Singamsetty@Sun.COM 
8227563SPrasad.Singamsetty@Sun.COM 				if (Numcyl > Numcyl_usable)
8237563SPrasad.Singamsetty@Sun.COM 					(void) printf("WARNING: Disk is larger"
8247563SPrasad.Singamsetty@Sun.COM 					    " than 2TB. Solaris partition will"
8257563SPrasad.Singamsetty@Sun.COM 					    " be limited to 2 TB.\n");
8260Sstevel@tonic-gate 			}
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 			/* Edit the partition table as directed */
8290Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 				/* Default scenario */
8320Sstevel@tonic-gate 				nulltbl();
8330Sstevel@tonic-gate 				/* now set up UNIX System partition */
8340Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
835*8333SSuhasini.Peddada@Sun.COM 				Table[0].relsect = lel(heads * sectors);
8367563SPrasad.Singamsetty@Sun.COM 
8377563SPrasad.Singamsetty@Sun.COM 				Table[0].numsect =
838*8333SSuhasini.Peddada@Sun.COM 				    lel((ulong_t)((Numcyl_usable - 1) *
8390Sstevel@tonic-gate 				    heads * sectors));
8407563SPrasad.Singamsetty@Sun.COM 
8410Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
8440Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
8450Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8460Sstevel@tonic-gate 			} else if (io_EFIdisk) {
8470Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
8480Sstevel@tonic-gate 				nulltbl();
8490Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
8507563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB :
8517563SPrasad.Singamsetty@Sun.COM 				    (dev_capacity - 1));
8520Sstevel@tonic-gate 				if (i != 0) {
853251Slclee 					(void) fprintf(stderr,
854251Slclee 					    "Error creating EFI partition\n");
8550Sstevel@tonic-gate 					exit(1);
8560Sstevel@tonic-gate 				}
8570Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8580Sstevel@tonic-gate 			}
8590Sstevel@tonic-gate 		}
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
8630Sstevel@tonic-gate 	if (io_debug) {
864251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
8650Sstevel@tonic-gate 		print_Table();
8660Sstevel@tonic-gate 		if (io_ifdisk) {
867251Slclee 			(void) fprintf(stderr, "\n");
868251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
869251Slclee 			(void) gets(s);
8700Sstevel@tonic-gate 		}
8710Sstevel@tonic-gate 	}
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate 	/* Interactive fdisk mode */
8740Sstevel@tonic-gate 	if (io_ifdisk) {
875251Slclee 		(void) printf(CLR_SCR);
8760Sstevel@tonic-gate 		disptbl();
877251Slclee 		for (;;) {
878251Slclee 			stage0();
8790Sstevel@tonic-gate 			copy_Bootblk_to_Table();
8800Sstevel@tonic-gate 			disptbl();
8810Sstevel@tonic-gate 		}
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
8850Sstevel@tonic-gate 	if (io_Wfdisk)
8860Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
8870Sstevel@tonic-gate 	else if (stdo_flag)
8880Sstevel@tonic-gate 		ffile_write((char *)stdout);
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
891251Slclee 	return (0);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate  * read_geom
8960Sstevel@tonic-gate  * Read geometry from specified file (-S).
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate 
899251Slclee static int
900251Slclee read_geom(char *sgeom)
9010Sstevel@tonic-gate {
9020Sstevel@tonic-gate 	char	line[256];
9030Sstevel@tonic-gate 	FILE *fp;
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	/* open the prototype file */
9060Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
9070Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
9080Sstevel@tonic-gate 		    io_sgeom);
9090Sstevel@tonic-gate 		return (1);
9100Sstevel@tonic-gate 	}
9110Sstevel@tonic-gate 
9120Sstevel@tonic-gate 	/* Read a line from the file */
9130Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
9140Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
9150Sstevel@tonic-gate 			continue;
9160Sstevel@tonic-gate 		else {
9170Sstevel@tonic-gate 			line[strlen(line)] = '\0';
918251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
9190Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
9200Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
9210Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
9220Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
9230Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
9240Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
9250Sstevel@tonic-gate 			    &sectsiz) != 7) {
9260Sstevel@tonic-gate 				(void) fprintf(stderr,
9270Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
9280Sstevel@tonic-gate 				    line);
9290Sstevel@tonic-gate 				return (1);
9300Sstevel@tonic-gate 			}
9310Sstevel@tonic-gate 			break;
9320Sstevel@tonic-gate 		} /* else */
9330Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 	if (!io_image) {
9360Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
9370Sstevel@tonic-gate 			(void) fprintf(stderr,
9380Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
9390Sstevel@tonic-gate 			return (1);
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 	} else {
9420Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
9430Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
9440Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
9450Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
948251Slclee 	(void) fclose(fp);
9490Sstevel@tonic-gate 	return (0);
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * dev_mboot_read
9540Sstevel@tonic-gate  * Read the master boot sector from the device.
9550Sstevel@tonic-gate  */
956251Slclee static void
957251Slclee dev_mboot_read(void)
9580Sstevel@tonic-gate {
9590Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
9600Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate 	if (errno == 0)
9630Sstevel@tonic-gate 		return;
9640Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
965251Slclee 		(void) fprintf(stderr,
9660Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
9670Sstevel@tonic-gate 		    Dfltdev);
9680Sstevel@tonic-gate 		if (!io_image)
9690Sstevel@tonic-gate 			exit(1);
9700Sstevel@tonic-gate 	}
9710Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
972251Slclee 		(void) fprintf(stderr,
9730Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
9740Sstevel@tonic-gate 		    Dfltdev);
9750Sstevel@tonic-gate 		if (!io_image)
9760Sstevel@tonic-gate 			exit(1);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate }
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate /*
9810Sstevel@tonic-gate  * dev_mboot_write
9820Sstevel@tonic-gate  * Write the master boot sector to the device.
9830Sstevel@tonic-gate  */
984251Slclee static void
9856549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
9886478Sbharding 	int	clr_efi = -1;
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	if (io_readonly)
991251Slclee 		return;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if (io_debug) {
994251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
9950Sstevel@tonic-gate 		print_Table();
9960Sstevel@tonic-gate 		if (io_ifdisk) {
997251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
998251Slclee 			(void) gets(s);
9990Sstevel@tonic-gate 		}
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10026478Sbharding 	/*
10036478Sbharding 	 * If the new table has any Solaris partitions and the old
10046478Sbharding 	 * table does not have an entry that describes it
10056478Sbharding 	 * exactly then clear the old vtoc (if any).
10066478Sbharding 	 */
10070Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10080Sstevel@tonic-gate 
10096478Sbharding 		/* We only care about potential Solaris parts. */
10100Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
10110Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
10120Sstevel@tonic-gate 			continue;
10130Sstevel@tonic-gate 
10146478Sbharding 		/* Does the old table have an exact entry for the new entry? */
10156478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10166478Sbharding 
10176478Sbharding 			/* We only care about old Solaris partitions. */
10186478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
10196478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
10206478Sbharding 
10216478Sbharding 				/* Is this old one the same as a new one? */
10226478Sbharding 				if ((Old_Table[old_pt].relsect ==
10236478Sbharding 				    Table[new_pt].relsect) &&
10246478Sbharding 				    (Old_Table[old_pt].numsect ==
10256478Sbharding 				    Table[new_pt].numsect))
10266478Sbharding 					break; /* Yes */
10276478Sbharding 			}
10286478Sbharding 		}
10296478Sbharding 
10306478Sbharding 		/* Did a solaris partition change location or size? */
10316478Sbharding 		if (old_pt >= FD_NUMPART) {
10326478Sbharding 			/* Yes clear old vtoc */
10336478Sbharding 			if (io_debug) {
10346478Sbharding 				(void) fprintf(stderr,
10356478Sbharding 				    "Clearing VTOC labels from NEW"
10366478Sbharding 				    " table\n");
10376478Sbharding 			}
10386478Sbharding 			clear_vtoc(NEW, new_pt);
10396478Sbharding 		}
10406478Sbharding 	}
10416478Sbharding 
10426478Sbharding 
10436478Sbharding 	/* see if the old table had EFI */
10446478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10456478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
10466478Sbharding 			clr_efi = old_pt;
10470Sstevel@tonic-gate 		}
10480Sstevel@tonic-gate 	}
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
10510Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10520Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
10530Sstevel@tonic-gate 			continue;
10540Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10555169Slclee 			if ((Old_Table[old_pt].systid ==
10565169Slclee 			    Table[new_pt].systid) &&
10575169Slclee 			    (Old_Table[old_pt].relsect ==
10585169Slclee 			    Table[new_pt].relsect) &&
10595169Slclee 			    (Old_Table[old_pt].numsect ==
10605169Slclee 			    Table[new_pt].numsect))
10615169Slclee 				break;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 		/*
10650Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
10660Sstevel@tonic-gate 		 * the EFI GPT
10670Sstevel@tonic-gate 		 */
10680Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
10690Sstevel@tonic-gate 			clr_efi = 0;
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 		break;
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	/* clear labels if necessary */
10750Sstevel@tonic-gate 	if (clr_efi >= 0) {
10760Sstevel@tonic-gate 		if (io_debug) {
1077251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
10780Sstevel@tonic-gate 		}
10790Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
10800Sstevel@tonic-gate 			if (io_debug) {
1081251Slclee 				(void) fprintf(stderr,
1082251Slclee 				    "\tError %d clearing EFI labels"
10830Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
10840Sstevel@tonic-gate 				    error);
10850Sstevel@tonic-gate 			}
10860Sstevel@tonic-gate 		}
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1090251Slclee 		(void) fprintf(stderr,
10910Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
10920Sstevel@tonic-gate 		    Dfltdev);
10930Sstevel@tonic-gate 	}
10940Sstevel@tonic-gate 	if (errno == 0)
10950Sstevel@tonic-gate 		return;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 	/* write to disk drive */
10980Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1099251Slclee 		(void) fprintf(stderr,
11000Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
11010Sstevel@tonic-gate 		    Dfltdev);
11020Sstevel@tonic-gate 		exit(1);
11030Sstevel@tonic-gate 	}
11040Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1105251Slclee 		(void) fprintf(stderr,
11060Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
11070Sstevel@tonic-gate 		    Dfltdev);
11080Sstevel@tonic-gate 		exit(1);
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate /*
11130Sstevel@tonic-gate  * mboot_read
11140Sstevel@tonic-gate  * Read the prototype boot records from the files.
11150Sstevel@tonic-gate  */
1116251Slclee static void
1117251Slclee mboot_read(void)
11180Sstevel@tonic-gate {
11190Sstevel@tonic-gate 	int mDev, i;
11200Sstevel@tonic-gate 	struct ipart *part;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate #if defined(i386) || defined(sparc)
11230Sstevel@tonic-gate 	/*
11240Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
11250Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
11260Sstevel@tonic-gate 	 */
11270Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
11280Sstevel@tonic-gate 		/*
11290Sstevel@tonic-gate 		 * Bug ID 1249035:
11300Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
11310Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
11320Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
11330Sstevel@tonic-gate 		 */
11340Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
11350Sstevel@tonic-gate 	}
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate 	/* First read in the master boot record */
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate 	/* Open the master boot proto file */
11400Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1141251Slclee 		(void) fprintf(stderr,
11420Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
11430Sstevel@tonic-gate 		    io_mboot);
11440Sstevel@tonic-gate 		exit(1);
11450Sstevel@tonic-gate 	}
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* Read the master boot program */
11480Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
11490Sstevel@tonic-gate 	    (struct mboot)) {
1150251Slclee 		(void) fprintf(stderr,
11510Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
11520Sstevel@tonic-gate 		    io_mboot);
11530Sstevel@tonic-gate 		exit(1);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* Is this really a master boot record? */
1157*8333SSuhasini.Peddada@Sun.COM 	if (les(BootCod.signature) != MBB_MAGIC) {
1158251Slclee 		(void) fprintf(stderr,
11590Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1160251Slclee 		(void) fprintf(stderr,
1161251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
1162*8333SSuhasini.Peddada@Sun.COM 		    les(BootCod.signature), MBB_MAGIC);
11630Sstevel@tonic-gate 		exit(1);
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 
1166251Slclee 	(void) close(mDev);
11670Sstevel@tonic-gate #else
11680Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
11690Sstevel@tonic-gate #endif
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
11720Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
11730Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1174251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
11750Sstevel@tonic-gate 	}
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate /*
11800Sstevel@tonic-gate  * fill_patt
11810Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
11820Sstevel@tonic-gate  */
1183251Slclee static void
1184251Slclee fill_patt(void)
11850Sstevel@tonic-gate {
1186251Slclee 	int	*buff_ptr, i;
11876549Sbharding 	off_t	*off_ptr;
11880Sstevel@tonic-gate 	int	io_fpatt = 0;
11890Sstevel@tonic-gate 	int	io_ipatt = 0;
11900Sstevel@tonic-gate 
11910Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
11920Sstevel@tonic-gate 		io_fpatt++;
11930Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
11940Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11950Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
11965169Slclee 			*buff_ptr = io_ipatt;
11970Sstevel@tonic-gate 	}
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	/*
12000Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
12010Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
12020Sstevel@tonic-gate 	 * for io_size blocks.
12030Sstevel@tonic-gate 	 */
12040Sstevel@tonic-gate 	while (io_size--) {
12056549Sbharding 		off_ptr = (off_t *)Bootsect;
12060Sstevel@tonic-gate 		if (!io_fpatt) {
12076549Sbharding 			for (i = 0; i < sectsiz;
12086549Sbharding 			    i += sizeof (off_t), off_ptr++)
12096549Sbharding 				*off_ptr = io_offset;
12100Sstevel@tonic-gate 		}
12110Sstevel@tonic-gate 		/* Write the data to disk */
12126549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
12136549Sbharding 		    SEEK_SET) == -1) {
1214251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12155169Slclee 			    Dfltdev);
12160Sstevel@tonic-gate 			exit(1);
12170Sstevel@tonic-gate 		}
12180Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1219251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12205169Slclee 			    Dfltdev);
12210Sstevel@tonic-gate 			exit(1);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 	} /* while (--io_size); */
12240Sstevel@tonic-gate }
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate /*
12270Sstevel@tonic-gate  * abs_read
12280Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
12290Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
12300Sstevel@tonic-gate  */
1231251Slclee static void
1232251Slclee abs_read(void)
1233251Slclee {
12340Sstevel@tonic-gate 	int c;
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 	while (io_size--) {
12376549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
12386549Sbharding 		    SEEK_SET) == -1) {
1239251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12400Sstevel@tonic-gate 			    Dfltdev);
12410Sstevel@tonic-gate 			exit(1);
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1244251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
12450Sstevel@tonic-gate 			    Dfltdev);
12460Sstevel@tonic-gate 			exit(1);
12470Sstevel@tonic-gate 		}
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		/* Write to standard ouptut */
1250251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12510Sstevel@tonic-gate 			if (c >= 0) {
12520Sstevel@tonic-gate 				if (io_debug)
1253251Slclee 					(void) fprintf(stderr,
1254251Slclee 					    "fdisk: Output warning: %d of %d"
1255251Slclee 					    " characters written.\n",
1256251Slclee 					    c, sectsiz);
12570Sstevel@tonic-gate 				exit(2);
12580Sstevel@tonic-gate 			} else {
12590Sstevel@tonic-gate 				perror("write error on output file.");
12600Sstevel@tonic-gate 				exit(2);
12610Sstevel@tonic-gate 			}
12620Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
12630Sstevel@tonic-gate 			/* != sectsiz) */
12640Sstevel@tonic-gate 	} /* while (--io_size); */
12650Sstevel@tonic-gate 	exit(0);
12660Sstevel@tonic-gate }
12670Sstevel@tonic-gate 
12680Sstevel@tonic-gate /*
12690Sstevel@tonic-gate  * abs_write
12700Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
12710Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
12720Sstevel@tonic-gate  */
1273251Slclee static void
1274251Slclee abs_write(void)
12750Sstevel@tonic-gate {
12760Sstevel@tonic-gate 	int c, i;
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate 	while (io_size--) {
12790Sstevel@tonic-gate 		int part_exit = 0;
12800Sstevel@tonic-gate 		/* Read from standard input */
12810Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12820Sstevel@tonic-gate 			if (c >= 0) {
12830Sstevel@tonic-gate 				if (io_debug)
1284251Slclee 				(void) fprintf(stderr,
12850Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
12860Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
12875169Slclee 				    c, sectsiz);
12880Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
12890Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
12900Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
12910Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
12920Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
12930Sstevel@tonic-gate 					Bootsect[i++] = 0;
12940Sstevel@tonic-gate 				}
12950Sstevel@tonic-gate 				part_exit++;
12960Sstevel@tonic-gate 			} else {
12970Sstevel@tonic-gate 				perror("read error on input file.");
12980Sstevel@tonic-gate 				exit(2);
12990Sstevel@tonic-gate 			}
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 		}
13020Sstevel@tonic-gate 		/* Write to disk drive */
13036549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
13046549Sbharding 		    SEEK_SET) == -1) {
1305251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
13060Sstevel@tonic-gate 			    Dfltdev);
13070Sstevel@tonic-gate 			exit(1);
13080Sstevel@tonic-gate 		}
13090Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1310251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
13110Sstevel@tonic-gate 			    Dfltdev);
13120Sstevel@tonic-gate 			exit(1);
13130Sstevel@tonic-gate 		}
13140Sstevel@tonic-gate 		if (part_exit)
13150Sstevel@tonic-gate 		exit(0);
13160Sstevel@tonic-gate 	} /* while (--io_size); */
13170Sstevel@tonic-gate 	exit(1);
13180Sstevel@tonic-gate }
13190Sstevel@tonic-gate 
1320251Slclee 
13210Sstevel@tonic-gate /*
13220Sstevel@tonic-gate  * load
13230Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
13240Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
13250Sstevel@tonic-gate  */
13260Sstevel@tonic-gate 
1327251Slclee static void
1328251Slclee load(int funct, char *file)
13290Sstevel@tonic-gate {
13300Sstevel@tonic-gate 	int	id;
13310Sstevel@tonic-gate 	int	act;
13320Sstevel@tonic-gate 	int	bhead;
13330Sstevel@tonic-gate 	int	bsect;
13340Sstevel@tonic-gate 	int	bcyl;
13350Sstevel@tonic-gate 	int	ehead;
13360Sstevel@tonic-gate 	int	esect;
13370Sstevel@tonic-gate 	int	ecyl;
13387563SPrasad.Singamsetty@Sun.COM 	uint32_t	rsect;
13397563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
13400Sstevel@tonic-gate 	char	line[256];
13410Sstevel@tonic-gate 	int	i = 0;
13420Sstevel@tonic-gate 	int	j;
13430Sstevel@tonic-gate 	FILE *fp;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	switch (funct) {
13460Sstevel@tonic-gate 
13475169Slclee 	case LOADFILE:
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		/*
13500Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
13510Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
13520Sstevel@tonic-gate 		 * fdisk_file).
13530Sstevel@tonic-gate 		 */
13540Sstevel@tonic-gate 		nulltbl();
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 		/* Open the prototype file */
13570Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
13580Sstevel@tonic-gate 			(void) fprintf(stderr,
13590Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
13600Sstevel@tonic-gate 			    file);
13610Sstevel@tonic-gate 			exit(1);
13620Sstevel@tonic-gate 		}
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 		/* Read a line from the file */
13650Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
13660Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
13670Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
13680Sstevel@tonic-gate 				continue;
13690Sstevel@tonic-gate 			}
13700Sstevel@tonic-gate 
13710Sstevel@tonic-gate 			/*
13720Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
13730Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
13740Sstevel@tonic-gate 			 */
13750Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
13760Sstevel@tonic-gate 				(void) fprintf(stderr,
13770Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13780Sstevel@tonic-gate 				    line);
13790Sstevel@tonic-gate 				exit(1);
13800Sstevel@tonic-gate 			}
13810Sstevel@tonic-gate 			/*
13820Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
13830Sstevel@tonic-gate 			 * in table
13840Sstevel@tonic-gate 			 */
13850Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
13860Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
13870Sstevel@tonic-gate 				(void) fprintf(stderr,
13880Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13890Sstevel@tonic-gate 				    line);
13900Sstevel@tonic-gate 				exit(1);
13910Sstevel@tonic-gate 			}
13920Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
13930Sstevel@tonic-gate 
13940Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1395251Slclee 			(void) fprintf(stderr,
13960Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
13970Sstevel@tonic-gate 			exit(1);
13980Sstevel@tonic-gate 		}
13990Sstevel@tonic-gate 
1400251Slclee 		(void) fclose(fp);
14010Sstevel@tonic-gate 		return;
14020Sstevel@tonic-gate 
14035169Slclee 	case LOADDEL:
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
1406251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
1407251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
1408251Slclee 			(void) fprintf(stderr,
1409251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1410251Slclee 			exit(1);
1411251Slclee 		}
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate 		/* Find the exact entry in the table */
14140Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
14150Sstevel@tonic-gate 			if (Table[i].systid == id &&
14160Sstevel@tonic-gate 			    Table[i].bootid == act &&
14170Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
14180Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
14195169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
1420251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
14210Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
14220Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
14235169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
1424251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
1425*8333SSuhasini.Peddada@Sun.COM 			    Table[i].relsect == lel(rsect) &&
1426*8333SSuhasini.Peddada@Sun.COM 			    Table[i].numsect == lel(numsect)) {
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 				/*
14290Sstevel@tonic-gate 				 * Found the entry. Now move rest of
14300Sstevel@tonic-gate 				 * entries up toward the top of the
14310Sstevel@tonic-gate 				 * table, leaving available entries at
14320Sstevel@tonic-gate 				 * the end of the fdisk table.
14330Sstevel@tonic-gate 				 */
1434251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
1435251Slclee 					Table[j].systid = Table[j + 1].systid;
1436251Slclee 					Table[j].bootid = Table[j + 1].bootid;
1437251Slclee 					Table[j].beghead = Table[j + 1].beghead;
1438251Slclee 					Table[j].begsect = Table[j + 1].begsect;
1439251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
1440251Slclee 					Table[j].endhead = Table[j + 1].endhead;
1441251Slclee 					Table[j].endsect = Table[j + 1].endsect;
1442251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
1443251Slclee 					Table[j].relsect = Table[j + 1].relsect;
1444251Slclee 					Table[j].numsect = Table[j + 1].numsect;
14450Sstevel@tonic-gate 				}
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 				/*
14480Sstevel@tonic-gate 				 * Mark the last entry as unused in case
14490Sstevel@tonic-gate 				 * all table entries were in use prior
14500Sstevel@tonic-gate 				 * to the deletion.
14510Sstevel@tonic-gate 				 */
14520Sstevel@tonic-gate 
1453251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
1454251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
14550Sstevel@tonic-gate 				return;
14560Sstevel@tonic-gate 			}
14570Sstevel@tonic-gate 		}
1458251Slclee 		(void) fprintf(stderr,
14590Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
14600Sstevel@tonic-gate 		    "	\"%s\"\n",
14610Sstevel@tonic-gate 		    file);
14620Sstevel@tonic-gate 		exit(1);
1463*8333SSuhasini.Peddada@Sun.COM 		/* FALLTHRU */
14640Sstevel@tonic-gate 
14655169Slclee 	case LOADADD:
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
1468251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
1469251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
1470251Slclee 			(void) fprintf(stderr,
1471251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1472251Slclee 			exit(1);
1473251Slclee 		}
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
14760Sstevel@tonic-gate 		if (rsect == 0) {
14770Sstevel@tonic-gate 			(void) fprintf(stderr,
14780Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
14790Sstevel@tonic-gate 			    "   \"%s\".\n",
14800Sstevel@tonic-gate 			    file);
14810Sstevel@tonic-gate 			exit(1);
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		/*
14850Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
14860Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
14870Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
14880Sstevel@tonic-gate 		 */
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate 		if (id == EFI_PMBR) {
14910Sstevel@tonic-gate 			if (rsect != 1) {
14920Sstevel@tonic-gate 				(void) fprintf(stderr,
14930Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
14940Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
14950Sstevel@tonic-gate 				exit(1);
14960Sstevel@tonic-gate 			}
14970Sstevel@tonic-gate 
14987563SPrasad.Singamsetty@Sun.COM 
14997563SPrasad.Singamsetty@Sun.COM 			if (dev_capacity > DK_MAX_2TB) {
15007563SPrasad.Singamsetty@Sun.COM 				if (numsect != DK_MAX_2TB) {
15017563SPrasad.Singamsetty@Sun.COM 					(void) fprintf(stderr,
15027563SPrasad.Singamsetty@Sun.COM 					    "fdisk: EFI partitions must "
15037563SPrasad.Singamsetty@Sun.COM 					    "encompass the entire maximum 2 TB "
15047563SPrasad.Singamsetty@Sun.COM 					    "(input numsect: %u - max: %llu)\n",
15057563SPrasad.Singamsetty@Sun.COM 					    numsect, (diskaddr_t)DK_MAX_2TB);
15067563SPrasad.Singamsetty@Sun.COM 				exit(1);
15077563SPrasad.Singamsetty@Sun.COM 				}
15087563SPrasad.Singamsetty@Sun.COM 			} else if (numsect != dev_capacity - 1) {
15090Sstevel@tonic-gate 				(void) fprintf(stderr,
15100Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
1511251Slclee 				    "entire disk\n"
15127563SPrasad.Singamsetty@Sun.COM 				    "(input numsect: %u - avail: %llu)\n",
1513251Slclee 				    numsect,
15145169Slclee 				    dev_capacity - 1);
15150Sstevel@tonic-gate 				exit(1);
15160Sstevel@tonic-gate 			}
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
15200Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
15210Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
15220Sstevel@tonic-gate 			(void) fprintf(stderr,
15230Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
15240Sstevel@tonic-gate 			    "	\"%s\"\n",
15250Sstevel@tonic-gate 			    file);
15260Sstevel@tonic-gate 			exit(1);
15270Sstevel@tonic-gate 		}
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
15300Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1531251Slclee 			(void) fprintf(stderr,
1532251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
15330Sstevel@tonic-gate 			exit(1);
15340Sstevel@tonic-gate 		}
15350Sstevel@tonic-gate 	} /* switch funct */
15360Sstevel@tonic-gate }
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate /*
15390Sstevel@tonic-gate  * Set_Table_CHS_Values
15400Sstevel@tonic-gate  *
15410Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
15420Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
15430Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
15440Sstevel@tonic-gate  *
15450Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
15460Sstevel@tonic-gate  *
15470Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
15480Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
15490Sstevel@tonic-gate  */
15500Sstevel@tonic-gate static void
15510Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
15520Sstevel@tonic-gate {
15530Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
15560Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15570Sstevel@tonic-gate 		/*
15580Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
15590Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
15600Sstevel@tonic-gate 		 */
15610Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15620Sstevel@tonic-gate 		hd = MAX_HEAD;
15630Sstevel@tonic-gate 		sc = MAX_SECT;
15640Sstevel@tonic-gate 	} else {
15650Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15660Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15670Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15680Sstevel@tonic-gate 	}
15690Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
1570251Slclee 	Table[ti].beghead = (uchar_t)hd;
1571251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 	/*
15740Sstevel@tonic-gate 	 * This code is identical to the code above
15750Sstevel@tonic-gate 	 * except that it works on ending CHS values
15760Sstevel@tonic-gate 	 */
15770Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
15780Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15790Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15800Sstevel@tonic-gate 		hd = MAX_HEAD;
15810Sstevel@tonic-gate 		sc = MAX_SECT;
15820Sstevel@tonic-gate 	} else {
15830Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15840Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15850Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15860Sstevel@tonic-gate 	}
15870Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
1588251Slclee 	Table[ti].endhead = (uchar_t)hd;
1589251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate /*
15930Sstevel@tonic-gate  * insert_tbl
15940Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
15950Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
15960Sstevel@tonic-gate  *	entries!
15970Sstevel@tonic-gate  */
1598251Slclee static int
1599251Slclee insert_tbl(
1600251Slclee     int id, int act,
1601251Slclee     int bhead, int bsect, int bcyl,
1602251Slclee     int ehead, int esect, int ecyl,
16037563SPrasad.Singamsetty@Sun.COM     uint32_t rsect, uint32_t numsect)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate 	int	i;
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	/* validate partition size */
16087563SPrasad.Singamsetty@Sun.COM 	if (((diskaddr_t)rsect + numsect) > dev_capacity) {
1609251Slclee 		(void) fprintf(stderr,
16100Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
16110Sstevel@tonic-gate 		return (-1);
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 
16147563SPrasad.Singamsetty@Sun.COM 
16150Sstevel@tonic-gate 	/* find UNUSED partition table entry */
16160Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
16170Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
16180Sstevel@tonic-gate 			break;
16190Sstevel@tonic-gate 		}
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
1622251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
16230Sstevel@tonic-gate 		return (-1);
16240Sstevel@tonic-gate 	}
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate 
1627251Slclee 	Table[i].systid = (uchar_t)id;
1628251Slclee 	Table[i].bootid = (uchar_t)act;
1629*8333SSuhasini.Peddada@Sun.COM 	Table[i].numsect = lel(numsect);
1630*8333SSuhasini.Peddada@Sun.COM 	Table[i].relsect = lel(rsect);
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate 	/*
16330Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
16340Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
16350Sstevel@tonic-gate 	 */
16360Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
16370Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
16380Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
16390Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
16400Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
16410Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
16420Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
16430Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
16440Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
16450Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
1646251Slclee 		Table[i].beghead = (uchar_t)bhead;
1647251Slclee 		Table[i].endhead = (uchar_t)ehead;
1648251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
16490Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
16500Sstevel@tonic-gate 	} else {
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 		/*
16530Sstevel@tonic-gate 		 * The specified values are invalid,
16540Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
16550Sstevel@tonic-gate 		 */
16560Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	/*
16600Sstevel@tonic-gate 	 * return partition index
16610Sstevel@tonic-gate 	 */
16620Sstevel@tonic-gate 	return (i);
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate  * verify_tbl
16670Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
16680Sstevel@tonic-gate  * the disk.
16690Sstevel@tonic-gate  */
1670251Slclee static int
1671251Slclee verify_tbl(void)
16720Sstevel@tonic-gate {
16737563SPrasad.Singamsetty@Sun.COM 	uint32_t	i, j, rsect, numsect;
16740Sstevel@tonic-gate 	int	noMoreParts = 0;
16750Sstevel@tonic-gate 	int	numParts = 0;
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
1678251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
16790Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
16800Sstevel@tonic-gate 			numParts++;
16810Sstevel@tonic-gate 			/*
16820Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
16830Sstevel@tonic-gate 			 * EFI_PMBR part
16840Sstevel@tonic-gate 			 */
16850Sstevel@tonic-gate 			if (noMoreParts) {
16860Sstevel@tonic-gate 				return (-1);
16870Sstevel@tonic-gate 			}
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 			/*
16900Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
16910Sstevel@tonic-gate 			 * and must be Table entry 0
16920Sstevel@tonic-gate 			 */
16930Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
16940Sstevel@tonic-gate 				if (i == 0) {
16950Sstevel@tonic-gate 					noMoreParts = 1;
16960Sstevel@tonic-gate 				} else {
16970Sstevel@tonic-gate 					return (-1);
16980Sstevel@tonic-gate 				}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
1701251Slclee 					(void) fprintf(stderr, "ERROR: "
17020Sstevel@tonic-gate 					    "Invalid starting sector "
17030Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
17040Sstevel@tonic-gate 					    "relsect %d "
17050Sstevel@tonic-gate 					    "(should be 1)\n",
17060Sstevel@tonic-gate 					    Table[i].relsect);
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 					return (-1);
17090Sstevel@tonic-gate 				}
17100Sstevel@tonic-gate 
17115169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
1712251Slclee 					(void) fprintf(stderr, "ERROR: "
17130Sstevel@tonic-gate 					    "EFI_PMBR partition must "
17140Sstevel@tonic-gate 					    "encompass the entire "
1715251Slclee 					    "disk.\n numsect %d - "
17165169Slclee 					    "actual %llu\n",
17170Sstevel@tonic-gate 					    Table[i].numsect,
17185169Slclee 					    dev_capacity - 1);
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate 					return (-1);
17210Sstevel@tonic-gate 				}
17220Sstevel@tonic-gate 			}
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
1725*8333SSuhasini.Peddada@Sun.COM 			rsect = lel(Table[i].relsect);
1726*8333SSuhasini.Peddada@Sun.COM 			numsect = lel(Table[i].numsect);
17277563SPrasad.Singamsetty@Sun.COM 
17287563SPrasad.Singamsetty@Sun.COM 			if ((((diskaddr_t)rsect + numsect) > dev_capacity) ||
17297563SPrasad.Singamsetty@Sun.COM 			    (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) {
17300Sstevel@tonic-gate 				return (-1);
17310Sstevel@tonic-gate 			}
17320Sstevel@tonic-gate 
1733251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
17340Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
17357563SPrasad.Singamsetty@Sun.COM 					uint32_t t_relsect =
1736*8333SSuhasini.Peddada@Sun.COM 					    lel(Table[j].relsect);
17377563SPrasad.Singamsetty@Sun.COM 					uint32_t t_numsect =
1738*8333SSuhasini.Peddada@Sun.COM 					    lel(Table[j].numsect);
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 					if (noMoreParts) {
1741251Slclee 						(void) fprintf(stderr,
17420Sstevel@tonic-gate 						    "Cannot add partition to "
17430Sstevel@tonic-gate 						    "table; no more partitions "
17440Sstevel@tonic-gate 						    "allowed\n");
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 						if (io_debug) {
1747251Slclee 							(void) fprintf(stderr,
17480Sstevel@tonic-gate 							    "DEBUG: Current "
17490Sstevel@tonic-gate 							    "partition:\t"
17500Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1751251Slclee 							    "%d:%d:%d:%d:%d\n"
17520Sstevel@tonic-gate 							    "       Next "
17530Sstevel@tonic-gate 							    "partition:\t\t"
17540Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1755251Slclee 							    "%d:%d:%d:%d:%d\n",
17560Sstevel@tonic-gate 							    Table[i].systid,
17570Sstevel@tonic-gate 							    Table[i].bootid,
17580Sstevel@tonic-gate 							    Table[i].begcyl,
17590Sstevel@tonic-gate 							    Table[i].beghead,
17600Sstevel@tonic-gate 							    Table[i].begsect,
17610Sstevel@tonic-gate 							    Table[i].endcyl,
17620Sstevel@tonic-gate 							    Table[i].endhead,
17630Sstevel@tonic-gate 							    Table[i].endsect,
17640Sstevel@tonic-gate 							    Table[i].relsect,
17650Sstevel@tonic-gate 							    Table[i].numsect,
17660Sstevel@tonic-gate 							    Table[j].systid,
17670Sstevel@tonic-gate 							    Table[j].bootid,
17680Sstevel@tonic-gate 							    Table[j].begcyl,
17690Sstevel@tonic-gate 							    Table[j].beghead,
17700Sstevel@tonic-gate 							    Table[j].begsect,
17710Sstevel@tonic-gate 							    Table[j].endcyl,
17720Sstevel@tonic-gate 							    Table[j].endhead,
17730Sstevel@tonic-gate 							    Table[j].endsect,
17740Sstevel@tonic-gate 							    Table[j].relsect,
17750Sstevel@tonic-gate 							    Table[j].numsect);
17760Sstevel@tonic-gate 						}
17770Sstevel@tonic-gate 
17780Sstevel@tonic-gate 						return (-1);
17790Sstevel@tonic-gate 					}
17800Sstevel@tonic-gate 					if ((rsect >=
17810Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
1782251Slclee 					    ((rsect + numsect) <= t_relsect)) {
17830Sstevel@tonic-gate 						continue;
17840Sstevel@tonic-gate 					} else {
1785251Slclee 						(void) fprintf(stderr, "ERROR: "
17860Sstevel@tonic-gate 						    "current partition overlaps"
17870Sstevel@tonic-gate 						    " following partition\n");
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 						return (-1);
17900Sstevel@tonic-gate 					}
17910Sstevel@tonic-gate 				}
17920Sstevel@tonic-gate 			}
17930Sstevel@tonic-gate 		} else {
17940Sstevel@tonic-gate 			noMoreParts = 1;
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
17980Sstevel@tonic-gate 		if (noMoreParts ||
1799*8333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
1800*8333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > dev_capacity) ||
1801*8333SSuhasini.Peddada@Sun.COM 		    (((diskaddr_t)lel(Table[i].relsect) +
1802*8333SSuhasini.Peddada@Sun.COM 		    lel(Table[i].numsect)) > DK_MAX_2TB)) {
18030Sstevel@tonic-gate 			return (-1);
18040Sstevel@tonic-gate 		}
18050Sstevel@tonic-gate 	}
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	return (numParts);
18080Sstevel@tonic-gate }
18090Sstevel@tonic-gate 
18100Sstevel@tonic-gate /*
18110Sstevel@tonic-gate  * pars_fdisk
18120Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
18130Sstevel@tonic-gate  * (-A, -D, -F).
18140Sstevel@tonic-gate  */
1815251Slclee static int
1816251Slclee pars_fdisk(
1817251Slclee     char *line,
1818251Slclee     int *id, int *act,
1819251Slclee     int *bhead, int *bsect, int *bcyl,
1820251Slclee     int *ehead, int *esect, int *ecyl,
18217563SPrasad.Singamsetty@Sun.COM     uint32_t *rsect, uint32_t *numsect)
18220Sstevel@tonic-gate {
18230Sstevel@tonic-gate 	int	i;
18240Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
18255169Slclee 		return (1);
18260Sstevel@tonic-gate 	line[strlen(line)] = '\0';
18270Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
18280Sstevel@tonic-gate 		if (line[i] == '\0') {
18290Sstevel@tonic-gate 			break;
18300Sstevel@tonic-gate 		} else if (line[i] == ':') {
18310Sstevel@tonic-gate 			line[i] = ' ';
18320Sstevel@tonic-gate 		}
18330Sstevel@tonic-gate 	}
18347563SPrasad.Singamsetty@Sun.COM 	if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u",
18350Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
18360Sstevel@tonic-gate 	    rsect, numsect) != 10) {
18370Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
18380Sstevel@tonic-gate 		exit(1);
18390Sstevel@tonic-gate 	}
18400Sstevel@tonic-gate 	return (0);
18410Sstevel@tonic-gate }
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate /*
18440Sstevel@tonic-gate  * validate_part
18450Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
18460Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
18470Sstevel@tonic-gate  */
1848251Slclee static int
18497563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect)
18500Sstevel@tonic-gate {
18510Sstevel@tonic-gate 	int i;
18520Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
18530Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
18540Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
1855*8333SSuhasini.Peddada@Sun.COM 			    (Old_Table[i].relsect == lel(rsect)) &&
1856*8333SSuhasini.Peddada@Sun.COM 			    (Old_Table[i].numsect == lel(numsect)))
18575169Slclee 				return (0);
18580Sstevel@tonic-gate 		}
1859251Slclee 		(void) fprintf(stderr,
1860251Slclee 		    "New partition cannot start at sector 0\n");
18610Sstevel@tonic-gate 		return (-1);
18620Sstevel@tonic-gate 	}
18630Sstevel@tonic-gate 	return (0);
18640Sstevel@tonic-gate }
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate /*
18670Sstevel@tonic-gate  * stage0
18680Sstevel@tonic-gate  * Print out interactive menu and process user input.
18690Sstevel@tonic-gate  */
1870251Slclee static void
1871251Slclee stage0(void)
18720Sstevel@tonic-gate {
1873251Slclee 	dispmenu();
1874251Slclee 	for (;;) {
1875251Slclee 		(void) printf(Q_LINE);
1876251Slclee 		(void) printf("Enter Selection: ");
1877251Slclee 		(void) gets(s);
18780Sstevel@tonic-gate 		rm_blanks(s);
18790Sstevel@tonic-gate 		while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) {
1880251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
1881251Slclee 			(void) printf(
1882251Slclee 			    "Enter a one-digit number between 1 and 6.");
1883251Slclee 			(void) printf(Q_LINE);
1884251Slclee 			(void) printf("Enter Selection: ");
1885251Slclee 			(void) gets(s);
18860Sstevel@tonic-gate 			rm_blanks(s);
18870Sstevel@tonic-gate 		}
1888251Slclee 		(void) printf(E_LINE);
18890Sstevel@tonic-gate 		switch (s[0]) {
18900Sstevel@tonic-gate 			case '1':
18910Sstevel@tonic-gate 				if (pcreate() == -1)
18920Sstevel@tonic-gate 					return;
18930Sstevel@tonic-gate 				break;
18940Sstevel@tonic-gate 			case '2':
18950Sstevel@tonic-gate 				if (pchange() == -1)
18960Sstevel@tonic-gate 					return;
18970Sstevel@tonic-gate 				break;
18980Sstevel@tonic-gate 			case '3':
18990Sstevel@tonic-gate 				if (pdelete() == -1)
19000Sstevel@tonic-gate 					return;
19010Sstevel@tonic-gate 				break;
19020Sstevel@tonic-gate 			case '4':
19030Sstevel@tonic-gate 				if (ppartid() == -1)
19040Sstevel@tonic-gate 					return;
19050Sstevel@tonic-gate 				break;
19060Sstevel@tonic-gate 			case '5':
19070Sstevel@tonic-gate 				/* update disk partition table, if changed */
19080Sstevel@tonic-gate 				if (TableChanged() == 1) {
19090Sstevel@tonic-gate 					copy_Table_to_Bootblk();
19100Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
19110Sstevel@tonic-gate 				}
19120Sstevel@tonic-gate 				/*
19130Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
19140Sstevel@tonic-gate 				 * (truncate only)
19150Sstevel@tonic-gate 				 */
19160Sstevel@tonic-gate 				if (io_adjt) {
19170Sstevel@tonic-gate 					fix_slice();
19180Sstevel@tonic-gate 				}
1919251Slclee 				(void) close(Dev);
19200Sstevel@tonic-gate 				exit(0);
1921251Slclee 				/* FALLTHRU */
19220Sstevel@tonic-gate 			case '6':
19230Sstevel@tonic-gate 				/*
19240Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
19250Sstevel@tonic-gate 				 * (truncate only)
19260Sstevel@tonic-gate 				 */
19270Sstevel@tonic-gate 				if (io_adjt) {
19280Sstevel@tonic-gate 					fix_slice();
19290Sstevel@tonic-gate 				}
1930251Slclee 				(void) close(Dev);
19310Sstevel@tonic-gate 				exit(0);
1932251Slclee 				/* FALLTHRU */
19330Sstevel@tonic-gate 			default:
19340Sstevel@tonic-gate 				break;
19350Sstevel@tonic-gate 		}
19360Sstevel@tonic-gate 		copy_Table_to_Bootblk();
19370Sstevel@tonic-gate 		disptbl();
1938251Slclee 		dispmenu();
19390Sstevel@tonic-gate 	}
19400Sstevel@tonic-gate }
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate /*
19430Sstevel@tonic-gate  * pcreate
19440Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
19450Sstevel@tonic-gate  */
1946251Slclee static int
1947251Slclee pcreate(void)
19480Sstevel@tonic-gate {
1949251Slclee 	uchar_t tsystid = 'z';
19500Sstevel@tonic-gate 	int i, j;
19517563SPrasad.Singamsetty@Sun.COM 	uint32_t numsect;
19520Sstevel@tonic-gate 	int retCode = 0;
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate 	i = 0;
1955251Slclee 	for (;;) {
19560Sstevel@tonic-gate 		if (i == FD_NUMPART) {
1957251Slclee 			(void) printf(E_LINE);
1958251Slclee 			(void) printf(
1959251Slclee 			    "The partition table is full!\n"
1960251Slclee 			    "You must delete a partition before creating"
19610Sstevel@tonic-gate 			    " a new one.\n");
19620Sstevel@tonic-gate 			return (-1);
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
19650Sstevel@tonic-gate 			break;
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 		i++;
19680Sstevel@tonic-gate 	}
19690Sstevel@tonic-gate 
19707563SPrasad.Singamsetty@Sun.COM 	numsect = 0;
19710Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
19720Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
1973*8333SSuhasini.Peddada@Sun.COM 			numsect += lel(Table[i].numsect);
19740Sstevel@tonic-gate 		}
19757563SPrasad.Singamsetty@Sun.COM 		if (numsect >= chs_capacity) {
1976251Slclee 			(void) printf(E_LINE);
1977251Slclee 			(void) printf("There is no more room on the disk for"
19780Sstevel@tonic-gate 			    " another partition.\n");
1979251Slclee 			(void) printf(
1980251Slclee 			    "You must delete a partition before creating"
19810Sstevel@tonic-gate 			    " a new one.\n");
19820Sstevel@tonic-gate 			return (-1);
19830Sstevel@tonic-gate 		}
19840Sstevel@tonic-gate 	}
19850Sstevel@tonic-gate 	while (tsystid == 'z') {
19867563SPrasad.Singamsetty@Sun.COM 
19877563SPrasad.Singamsetty@Sun.COM 		/*
19887563SPrasad.Singamsetty@Sun.COM 		 * The question here is expanding to more than what is
19897563SPrasad.Singamsetty@Sun.COM 		 * allocated for question lines (Q_LINE) which garbles
19907563SPrasad.Singamsetty@Sun.COM 		 * at least warning line. Clearing warning line as workaround
19917563SPrasad.Singamsetty@Sun.COM 		 * for now.
19927563SPrasad.Singamsetty@Sun.COM 		 */
19937563SPrasad.Singamsetty@Sun.COM 
19947563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
1995251Slclee 		(void) printf(Q_LINE);
1996251Slclee 		(void) printf(
1997251Slclee 		    "Select the partition type to create:\n"
1998251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
1999251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
2000251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
2001251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
2002251Slclee 		(void) gets(s);
20030Sstevel@tonic-gate 		rm_blanks(s);
20040Sstevel@tonic-gate 		if (s[1] != 0) {
2005251Slclee 			(void) printf(E_LINE);
2006251Slclee 			(void) printf("Invalid selection, try again.");
20070Sstevel@tonic-gate 			continue;
20080Sstevel@tonic-gate 		}
20090Sstevel@tonic-gate 		switch (s[0]) {
20100Sstevel@tonic-gate 		case '0':		/* exit */
20115169Slclee 			(void) printf(E_LINE);
20125169Slclee 			return (-1);
20130Sstevel@tonic-gate 		case '1':		/* Solaris partition */
20145169Slclee 			tsystid = SUNIXOS2;
20155169Slclee 			break;
20160Sstevel@tonic-gate 		case '2':		/* UNIX partition */
20175169Slclee 			tsystid = UNIXOS;
20185169Slclee 			break;
20190Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
20205169Slclee 			tsystid = PCIXOS;
20215169Slclee 			break;
20220Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
20235169Slclee 			tsystid = OTHEROS;
20245169Slclee 			break;
20250Sstevel@tonic-gate 		case '5':
20265169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
20275169Slclee 			break;
20280Sstevel@tonic-gate 		case '6':
20295169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
20305169Slclee 			break;
20310Sstevel@tonic-gate 		case '7':
20325169Slclee 			tsystid = EXTDOS;
20335169Slclee 			break;
20340Sstevel@tonic-gate 		case '8':
20355169Slclee 			tsystid = DOSHUGE;
20365169Slclee 			break;
20370Sstevel@tonic-gate 		case '9':
20385169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
20395169Slclee 			break;
20400Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
20410Sstevel@tonic-gate 		case 'A':
20425169Slclee 			tsystid = X86BOOT;
20435169Slclee 			break;
20440Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
20450Sstevel@tonic-gate 		case 'B':
20465169Slclee 			tsystid = DIAGPART;
20475169Slclee 			break;
20480Sstevel@tonic-gate 		case 'c':		/* FAT32 */
20490Sstevel@tonic-gate 		case 'C':
20505169Slclee 			tsystid = FDISK_WINDOWS;
20515169Slclee 			break;
20520Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
20530Sstevel@tonic-gate 		case 'D':
20545169Slclee 			tsystid = FDISK_EXT_WIN;
20555169Slclee 			break;
20560Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
20570Sstevel@tonic-gate 		case 'E':
20585169Slclee 			tsystid = FDISK_EXTLBA;
20595169Slclee 			break;
20600Sstevel@tonic-gate 		case 'f':
20610Sstevel@tonic-gate 		case 'F':
20625169Slclee 			tsystid = EFI_PMBR;
20635169Slclee 			break;
20640Sstevel@tonic-gate 		default:
20655169Slclee 			(void) printf(E_LINE);
20665169Slclee 			(void) printf("Invalid selection, try again.");
20675169Slclee 			continue;
20680Sstevel@tonic-gate 		}
20690Sstevel@tonic-gate 	}
20700Sstevel@tonic-gate 
2071251Slclee 	(void) printf(E_LINE);
20720Sstevel@tonic-gate 
20730Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
20747563SPrasad.Singamsetty@Sun.COM 		(void) printf(W_LINE);
20757563SPrasad.Singamsetty@Sun.COM 		if ((dev_capacity > DK_MAX_2TB))
20767563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
20777563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
20787563SPrasad.Singamsetty@Sun.COM 
20790Sstevel@tonic-gate 		/* create the new partition */
20800Sstevel@tonic-gate 		i = specify(tsystid);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 		if (i != -1) {
20830Sstevel@tonic-gate 			/* see if it should be the active partition */
2084251Slclee 			(void) printf(E_LINE);
2085251Slclee 			(void) printf(Q_LINE);
2086251Slclee 
2087251Slclee 			(void) printf(
2088251Slclee 			    "Should this become the active partition? If "
2089251Slclee 			    "yes, it  will be activated\n"
2090251Slclee 			    "each time the computer is reset or turned on.\n"
2091251Slclee 			    "Please type \"y\" or \"n\". ");
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 			if (yesno()) {
2094251Slclee 				(void) printf(E_LINE);
20950Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
20960Sstevel@tonic-gate 					if (j == i) {
20970Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2098251Slclee 						(void) printf(E_LINE);
2099251Slclee 						(void) printf(
2100251Slclee 						    "Partition %d is now "
21010Sstevel@tonic-gate 						    "the active partition.",
2102251Slclee 						    j + 1);
21030Sstevel@tonic-gate 					} else {
21040Sstevel@tonic-gate 						Table[j].bootid = 0;
21050Sstevel@tonic-gate 					}
21060Sstevel@tonic-gate 				}
21070Sstevel@tonic-gate 			} else {
21080Sstevel@tonic-gate 				Table[i].bootid = 0;
21090Sstevel@tonic-gate 			}
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 			/* set up the return code */
21120Sstevel@tonic-gate 			i = 1;
21130Sstevel@tonic-gate 		}
21140Sstevel@tonic-gate 	} else {
21150Sstevel@tonic-gate 		/*
21160Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
21170Sstevel@tonic-gate 		 * the table
21180Sstevel@tonic-gate 		 *
21190Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
21200Sstevel@tonic-gate 		 * empty
21210Sstevel@tonic-gate 		 */
21220Sstevel@tonic-gate 		retCode = verify_tbl();
21230Sstevel@tonic-gate 
21240Sstevel@tonic-gate 		if (retCode < 0) {
2125251Slclee 			(void) fprintf(stderr,
21260Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
21270Sstevel@tonic-gate 			    "current partition table is invalid.\n");
21280Sstevel@tonic-gate 			return (-1);
21290Sstevel@tonic-gate 		} else if (retCode > 0) {
2130251Slclee 			(void) printf(
2131251Slclee 			    "An EFI partition must be the only partition on "
2132251Slclee 			    "disk.  You may manually delete existing\n"
2133251Slclee 			    "partitions, or fdisk can do it.\n"
2134251Slclee 			    "Do you want fdisk to destroy existing "
2135251Slclee 			    "partitions?\n"
2136251Slclee 			    "Please type \"y\" or \"n\". ");
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 			if (yesno()) {
21390Sstevel@tonic-gate 				nulltbl();
21400Sstevel@tonic-gate 			} else {
21410Sstevel@tonic-gate 				return (-1);
21420Sstevel@tonic-gate 			}
21430Sstevel@tonic-gate 		}
21440Sstevel@tonic-gate 
21450Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
21467563SPrasad.Singamsetty@Sun.COM 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1,
21477563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB:
21487563SPrasad.Singamsetty@Sun.COM 		    (dev_capacity - 1));
21490Sstevel@tonic-gate 
21500Sstevel@tonic-gate 		if (i != 0) {
2151251Slclee 			(void) printf("Error creating EFI partition!!!\n");
21520Sstevel@tonic-gate 			i = -1;
21530Sstevel@tonic-gate 		} else {
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 			/* EFI partitions are currently never active */
21560Sstevel@tonic-gate 			Table[i].bootid = 0;
21570Sstevel@tonic-gate 
21580Sstevel@tonic-gate 			/* set up the return code */
21590Sstevel@tonic-gate 			i = 1;
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 	}
21620Sstevel@tonic-gate 
21630Sstevel@tonic-gate 	return (i);
21640Sstevel@tonic-gate }
21650Sstevel@tonic-gate 
21660Sstevel@tonic-gate /*
21670Sstevel@tonic-gate  * specify
21680Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
21690Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
21700Sstevel@tonic-gate  * cylinder and length in cylinders.
21710Sstevel@tonic-gate  */
2172251Slclee static int
2173251Slclee specify(uchar_t tsystid)
21740Sstevel@tonic-gate {
21755169Slclee 	int	i, j, percent = -1;
21767563SPrasad.Singamsetty@Sun.COM 	int	cyl, cylen;
21777563SPrasad.Singamsetty@Sun.COM 	diskaddr_t first_free, size_free;
21787563SPrasad.Singamsetty@Sun.COM 	diskaddr_t max_free;
21795169Slclee 	int	cyl_size;
21800Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
21810Sstevel@tonic-gate 
21825169Slclee 	cyl_size = heads * sectors;
21835169Slclee 
21845169Slclee 	/*
21855169Slclee 	 * make a local copy of the partition table
21865169Slclee 	 * and sort it into relsect order
21875169Slclee 	 */
21885169Slclee 	for (i = 0; i < FD_NUMPART; i++)
21895169Slclee 		partition[i] = &Table[i];
21905169Slclee 
21915169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
21925169Slclee 		if (partition[i]->systid == UNUSED)
21935169Slclee 			break;
21945169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
21955169Slclee 			if (partition[j]->systid == UNUSED)
21965169Slclee 				break;
2197*8333SSuhasini.Peddada@Sun.COM 			if (lel(partition[j]->relsect) <
2198*8333SSuhasini.Peddada@Sun.COM 			    lel(partition[i]->relsect)) {
21995169Slclee 				struct ipart *temp = partition[i];
22005169Slclee 				partition[i] = partition[j];
22015169Slclee 				partition[j] = temp;
22025169Slclee 			}
22035169Slclee 		}
22045169Slclee 	}
22055169Slclee 
22067563SPrasad.Singamsetty@Sun.COM 
2207251Slclee 	(void) printf(Q_LINE);
2208251Slclee 	(void) printf(
2209251Slclee 	    "Specify the percentage of disk to use for this partition\n"
2210251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
2211251Slclee 	(void) gets(s);
22120Sstevel@tonic-gate 	rm_blanks(s);
22130Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
22145169Slclee 		i = 0;
22155169Slclee 		while (s[i] != '\0') {
22165169Slclee 			if (s[i] < '0' || s[i] > '9') {
22175169Slclee 				(void) printf(E_LINE);
22185169Slclee 				(void) printf("Invalid percentage value "
22195169Slclee 				    "specified; retry the operation.");
22205169Slclee 				return (-1);
22215169Slclee 			}
22225169Slclee 			i++;
22235169Slclee 			if (i > 3) {
22245169Slclee 				(void) printf(E_LINE);
22255169Slclee 				(void) printf("Invalid percentage value "
22265169Slclee 				    "specified; retry the operation.");
22275169Slclee 				return (-1);
22285169Slclee 			}
22295169Slclee 		}
22305169Slclee 		if ((percent = atoi(s)) > 100) {
22315169Slclee 			(void) printf(E_LINE);
22325169Slclee 			(void) printf(
22335169Slclee 			    "Percentage value is too large. The value must be"
22345169Slclee 			    " between 1 and 100;\nretry the operation.\n");
22355169Slclee 			return (-1);
22360Sstevel@tonic-gate 		}
22375169Slclee 		if (percent < 1) {
22385169Slclee 			(void) printf(E_LINE);
22395169Slclee 			(void) printf(
22405169Slclee 			    "Percentage value is too small. The value must be"
22415169Slclee 			    " between 1 and 100;\nretry the operation.\n");
22425169Slclee 			return (-1);
22435169Slclee 		}
22445169Slclee 
22455169Slclee 		if (percent == 100)
22467563SPrasad.Singamsetty@Sun.COM 			cylen = Numcyl_usable - 1;
22475169Slclee 		else
22487563SPrasad.Singamsetty@Sun.COM 			cylen = (Numcyl_usable * percent) / 100;
22495169Slclee 
22505169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
22515169Slclee 		if ((tsystid == DOSOS12) &&
22525169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
22535169Slclee 			int n;
22547563SPrasad.Singamsetty@Sun.COM 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable;
22555169Slclee 			(void) printf(E_LINE);
22565169Slclee 			(void) printf("Maximum size for a DOS partition "
22575169Slclee 			    "is %d%%; retry the operation.",
22585169Slclee 			    n <= 100 ? n : 100);
22595169Slclee 			return (-1);
22600Sstevel@tonic-gate 		}
22615169Slclee 
22625169Slclee 
22635169Slclee 		max_free = 0;
22645169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
22655169Slclee 
22665169Slclee 			/*
22675169Slclee 			 * check for free space before partition i
22685169Slclee 			 * where i varies from 0 to 3
22695169Slclee 			 *
22705169Slclee 			 * freespace after partition 3 is unusable
22715169Slclee 			 * because there are no free partitions
22725169Slclee 			 *
22735169Slclee 			 * freespace begins at the end of previous partition
22745169Slclee 			 * or cylinder 1
22755169Slclee 			 */
22765169Slclee 			if (i) {
22775169Slclee 				/* Not an empty table */
2278*8333SSuhasini.Peddada@Sun.COM 				first_free = lel(partition[i - 1]->relsect) +
2279*8333SSuhasini.Peddada@Sun.COM 				    lel(partition[i - 1]->numsect);
22805169Slclee 			} else {
22815169Slclee 				first_free = cyl_size;
22825169Slclee 			}
22835169Slclee 
22845169Slclee 			/*
22855169Slclee 			 * freespace ends before the current partition
22865169Slclee 			 * or the end of the disk (chs end)
22875169Slclee 			 */
22885169Slclee 			if (partition[i]->systid == UNUSED) {
22895169Slclee 				size_free = chs_capacity - first_free;
22905169Slclee 			} else {
22918148SShidokht.Yadegari@Sun.COM 				/*
22928148SShidokht.Yadegari@Sun.COM 				 * Partition might start before cylinder 1.
22938148SShidokht.Yadegari@Sun.COM 				 * Make sure free space is not negative.
22948148SShidokht.Yadegari@Sun.COM 				 */
22955169Slclee 				size_free =
2296*8333SSuhasini.Peddada@Sun.COM 				    (lel(partition[i]->relsect > first_free)) ?
2297*8333SSuhasini.Peddada@Sun.COM 				    (lel(partition[i]->relsect) - first_free) :
2298*8333SSuhasini.Peddada@Sun.COM 				    0;
22995169Slclee 			}
23005169Slclee 
23015169Slclee 			/* save largest free space */
23025169Slclee 			if (max_free < size_free)
23035169Slclee 				max_free = size_free;
23045169Slclee 
23057563SPrasad.Singamsetty@Sun.COM 			if (((uint64_t)cylen * cyl_size) <= size_free) {
23065169Slclee 				/* We found a place to use */
23075169Slclee 				break;
23085169Slclee 			}
23095169Slclee 			if (partition[i]->systid == UNUSED) {
23105169Slclee 				(void) printf(E_LINE);
23115169Slclee 				max_free /= (cyl_size);
23125169Slclee 				(void) fprintf(stderr, "fdisk: "
23137563SPrasad.Singamsetty@Sun.COM 				    "Maximum percentage available is %lld\n",
23147563SPrasad.Singamsetty@Sun.COM 				    100 * max_free / Numcyl_usable);
23155169Slclee 				return (-1);
23165169Slclee 			}
23170Sstevel@tonic-gate 		}
23185169Slclee 
23195169Slclee 		(void) printf(E_LINE);
23205169Slclee 		if (i >= FD_NUMPART) {
23215169Slclee 			(void) fprintf(stderr,
23225169Slclee 			    "fdisk: Partition table is full.\n");
23235169Slclee 			return (-1);
23245169Slclee 		}
23255169Slclee 
23265169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
23275169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
23285169Slclee 			return (i);
23295169Slclee 		}
23305169Slclee 		return (-1);
23315169Slclee 	} else {
23325169Slclee 
23335169Slclee 		/* Specifying size in cylinders */
2334251Slclee 		(void) printf(E_LINE);
23355169Slclee 		(void) printf(Q_LINE);
23365169Slclee 		(void) printf("Enter starting cylinder number: ");
23375169Slclee 		if ((cyl = getcyl()) == -1) {
23385169Slclee 			(void) printf(E_LINE);
23395169Slclee 			(void) printf("Invalid number; retry the operation.");
23405169Slclee 			return (-1);
23415169Slclee 		}
23425169Slclee 		if (cyl == 0) {
23435169Slclee 			(void) printf(E_LINE);
23445169Slclee 			(void) printf(
23455169Slclee 			    "New partition cannot start at cylinder 0.\n");
23465169Slclee 			return (-1);
23475169Slclee 		}
23487563SPrasad.Singamsetty@Sun.COM 
23497563SPrasad.Singamsetty@Sun.COM 
23507563SPrasad.Singamsetty@Sun.COM 		if (cyl >= Numcyl_usable) {
23515169Slclee 			(void) printf(E_LINE);
23525169Slclee 			(void) printf(
23535169Slclee 			    "Cylinder %d is out of bounds, "
23545169Slclee 			    "the maximum is %d.\n",
23557563SPrasad.Singamsetty@Sun.COM 			    cyl, Numcyl_usable - 1);
23565169Slclee 			return (-1);
23575169Slclee 		}
23587563SPrasad.Singamsetty@Sun.COM 
23595169Slclee 		(void) printf(Q_LINE);
23605169Slclee 		(void) printf("Enter partition size in cylinders: ");
23615169Slclee 		if ((cylen = getcyl()) == -1) {
23625169Slclee 			(void) printf(E_LINE);
23635169Slclee 			(void) printf("Invalid number, retry the operation.");
23645169Slclee 			return (-1);
23655169Slclee 		}
23665169Slclee 
23675169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
23685169Slclee 			uint32_t	t_relsect, t_numsect;
23695169Slclee 
23705169Slclee 			if (partition[i]->systid == UNUSED)
23715169Slclee 				break;
2372*8333SSuhasini.Peddada@Sun.COM 			t_relsect = lel(partition[i]->relsect);
2373*8333SSuhasini.Peddada@Sun.COM 			t_numsect = lel(partition[i]->numsect);
23745169Slclee 
23755169Slclee 			if (cyl * cyl_size >= t_relsect &&
23765169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
23775169Slclee 				(void) printf(E_LINE);
23785169Slclee 				(void) printf(
23795169Slclee 				    "Cylinder %d is already allocated"
23805169Slclee 				    "\nretry the operation.",
23815169Slclee 				    cyl);
23825169Slclee 				return (-1);
23835169Slclee 			}
23845169Slclee 
23855169Slclee 			if (cyl * cyl_size < t_relsect &&
23865169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
23875169Slclee 				(void) printf(E_LINE);
23885169Slclee 				(void) printf(
23895169Slclee 				    "Maximum size for partition is %u cylinders"
23905169Slclee 				    "\nretry the operation.",
23915169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
23925169Slclee 				return (-1);
23935169Slclee 			}
23945169Slclee 		}
23955169Slclee 
23967563SPrasad.Singamsetty@Sun.COM 		/* Verify partition doesn't exceed disk size or 2 TB */
23977563SPrasad.Singamsetty@Sun.COM 		if (cyl + cylen > Numcyl_usable) {
23985169Slclee 			(void) printf(E_LINE);
23997563SPrasad.Singamsetty@Sun.COM 			if (Numcyl > Numcyl_usable) {
24007563SPrasad.Singamsetty@Sun.COM 				(void) printf(
24017563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
24027563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
24037563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
24047563SPrasad.Singamsetty@Sun.COM 			} else {
24057563SPrasad.Singamsetty@Sun.COM 				(void) printf(
24067563SPrasad.Singamsetty@Sun.COM 				    "Maximum size for partition is %d "
24077563SPrasad.Singamsetty@Sun.COM 				    "cylinders; \nretry the operation.",
24087563SPrasad.Singamsetty@Sun.COM 				    Numcyl_usable - cyl);
24097563SPrasad.Singamsetty@Sun.COM 			}
24105169Slclee 			return (-1);
24115169Slclee 		}
24125169Slclee 
24135169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
24145169Slclee 		if ((tsystid == DOSOS12) &&
24155169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
24165169Slclee 			(void) printf(E_LINE);
24175169Slclee 			(void) printf(
24185169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
24195169Slclee 			    "\nretry the operation.",
24205169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
24215169Slclee 			return (-1);
24225169Slclee 		}
24235169Slclee 
2424251Slclee 		(void) printf(E_LINE);
24255169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
24265169Slclee 		    cyl * cyl_size, cylen * cyl_size);
24275169Slclee 		if (i < 0)
24285169Slclee 			return (-1);
24295169Slclee 
24305169Slclee 		if (verify_tbl() < 0) {
24315169Slclee 			(void) printf(E_LINE);
24325169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
24335169Slclee 			return (-1);
24345169Slclee 		}
24355169Slclee 
24365169Slclee 		return (i);
24370Sstevel@tonic-gate 	}
24380Sstevel@tonic-gate }
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate /*
24410Sstevel@tonic-gate  * dispmenu
24420Sstevel@tonic-gate  * Display command menu (interactive mode).
24430Sstevel@tonic-gate  */
2444251Slclee static void
2445251Slclee dispmenu(void)
24460Sstevel@tonic-gate {
2447251Slclee 	(void) printf(M_LINE);
2448251Slclee 	(void) printf(
2449251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
2450251Slclee 	    "   1. Create a partition\n"
2451251Slclee 	    "   2. Specify the active partition\n"
2452251Slclee 	    "   3. Delete a partition\n"
2453251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
2454251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
2455251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
24560Sstevel@tonic-gate }
24570Sstevel@tonic-gate 
24580Sstevel@tonic-gate /*
24590Sstevel@tonic-gate  * pchange
24600Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
24610Sstevel@tonic-gate  */
2462251Slclee static int
2463251Slclee pchange(void)
24640Sstevel@tonic-gate {
24650Sstevel@tonic-gate 	char s[80];
24660Sstevel@tonic-gate 	int i, j;
24670Sstevel@tonic-gate 
2468251Slclee 	for (;;) {
2469251Slclee 		(void) printf(Q_LINE);
24700Sstevel@tonic-gate 			{
2471251Slclee 			(void) printf(
2472251Slclee 			    "Specify the partition number to boot from"
24730Sstevel@tonic-gate 			    " (or specify 0 for none): ");
24740Sstevel@tonic-gate 			}
2475251Slclee 		(void) gets(s);
24760Sstevel@tonic-gate 		rm_blanks(s);
24770Sstevel@tonic-gate 		if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) {
2478251Slclee 			(void) printf(E_LINE);
2479251Slclee 			(void) printf(
2480251Slclee 			    "Invalid response, please specify a number"
24810Sstevel@tonic-gate 			    " between 0 and 4.\n");
24820Sstevel@tonic-gate 		} else {
24830Sstevel@tonic-gate 			break;
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 	}
24860Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
24870Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
24880Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
24890Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
24900Sstevel@tonic-gate 				Table[i].bootid = 0;
24910Sstevel@tonic-gate 		}
2492251Slclee 		(void) printf(E_LINE);
2493251Slclee 			(void) printf(
2494251Slclee 			    "No partition is currently marked as active.");
24950Sstevel@tonic-gate 		return (0);
24960Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
24970Sstevel@tonic-gate 		i = s[0] - '1';
24980Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2499251Slclee 			(void) printf(E_LINE);
2500251Slclee 			(void) printf("Partition does not exist.");
25010Sstevel@tonic-gate 			return (-1);
25020Sstevel@tonic-gate 		}
25030Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
25040Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
25050Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
25060Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
2507251Slclee 			(void) printf(E_LINE);
2508251Slclee 			(void) printf(
2509251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
25100Sstevel@tonic-gate 			    "cannot be made active.\n");
2511251Slclee 			(void) printf("Select another partition.");
25120Sstevel@tonic-gate 			return (-1);
25130Sstevel@tonic-gate 		}
25140Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
25150Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
25160Sstevel@tonic-gate 			if (j != i)
25170Sstevel@tonic-gate 			Table[j].bootid = 0;
25180Sstevel@tonic-gate 		}
25190Sstevel@tonic-gate 	}
2520251Slclee 	(void) printf(E_LINE);
25210Sstevel@tonic-gate 		{
2522251Slclee 		(void) printf(
2523251Slclee 		    "Partition %d is now active. The system will start up"
2524251Slclee 		    " from this\n", i + 1);
2525251Slclee 		(void) printf("partition after the next reboot.");
25260Sstevel@tonic-gate 		}
25270Sstevel@tonic-gate 	return (1);
25280Sstevel@tonic-gate }
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate /*
25310Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
25320Sstevel@tonic-gate  */
2533251Slclee static int
2534251Slclee ppartid(void)
25350Sstevel@tonic-gate {
25360Sstevel@tonic-gate 	char	*p, s[80];
25370Sstevel@tonic-gate 	int	i;
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	for (;;) {
2540251Slclee 		(void) printf(Q_LINE);
2541251Slclee 		(void) printf("Specify the partition number to change"
25425169Slclee 		    " (or enter 0 to exit): ");
2543251Slclee 		if (!fgets(s, sizeof (s), stdin))
2544251Slclee 			return (1);
25450Sstevel@tonic-gate 		i = strtol(s, &p, 10);
25460Sstevel@tonic-gate 
25470Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
2548251Slclee 			(void) printf(E_LINE);
2549251Slclee 			(void) printf(
2550251Slclee 			    "Invalid response, retry the operation.\n");
25510Sstevel@tonic-gate 			continue;
25520Sstevel@tonic-gate 		}
25530Sstevel@tonic-gate 
25540Sstevel@tonic-gate 		if (i == 0) {
25550Sstevel@tonic-gate 			/* exit delete command */
2556251Slclee 			(void) printf(E_LINE); /* clear error message */
25570Sstevel@tonic-gate 			return (1);
25580Sstevel@tonic-gate 		}
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 		i -= 1;
25610Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
25620Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
25630Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
25640Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
25650Sstevel@tonic-gate 		} else {
2566251Slclee 			(void) printf(E_LINE);
2567251Slclee 			(void) printf(
2568251Slclee 			    "Partition %d is not a Solaris partition.",
25690Sstevel@tonic-gate 			    i + 1);
25700Sstevel@tonic-gate 			continue;
25710Sstevel@tonic-gate 		}
25720Sstevel@tonic-gate 
2573251Slclee 		(void) printf(E_LINE);
2574251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
25750Sstevel@tonic-gate 		return (1);
25760Sstevel@tonic-gate 	}
25770Sstevel@tonic-gate }
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate /*
25800Sstevel@tonic-gate  * pdelete
25810Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
25820Sstevel@tonic-gate  */
2583251Slclee static char
2584251Slclee pdelete(void)
25850Sstevel@tonic-gate {
25860Sstevel@tonic-gate 	char s[80];
25870Sstevel@tonic-gate 	int i, j;
25880Sstevel@tonic-gate 	char pactive;
25890Sstevel@tonic-gate 
2590251Slclee DEL1:	(void) printf(Q_LINE);
2591251Slclee 	(void) printf("Specify the partition number to delete"
25920Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
2593251Slclee 	(void) gets(s);
25940Sstevel@tonic-gate 	rm_blanks(s);
25950Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
2596251Slclee 		(void) printf(E_LINE);	/* clear error message */
25970Sstevel@tonic-gate 		return (1);
25980Sstevel@tonic-gate 	}
25990Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
26000Sstevel@tonic-gate 	if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) {
2601251Slclee 		(void) printf(E_LINE);
2602251Slclee 		(void) printf("Invalid response, retry the operation.\n");
26030Sstevel@tonic-gate 		goto DEL1;
26040Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
26050Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
26060Sstevel@tonic-gate 	}
26070Sstevel@tonic-gate 
26080Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
2609251Slclee 		(void) printf(E_LINE);
2610251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
26110Sstevel@tonic-gate 		return (-1);
26120Sstevel@tonic-gate 	}
26130Sstevel@tonic-gate 
2614*8333SSuhasini.Peddada@Sun.COM 	(void) printf(Q_LINE);
2615*8333SSuhasini.Peddada@Sun.COM 	(void) printf("Are you sure you want to delete partition %d?"
2616*8333SSuhasini.Peddada@Sun.COM 	    " This will make all files and \n", i + 1);
2617*8333SSuhasini.Peddada@Sun.COM 	(void) printf("programs in this partition inaccessible (type"
2618*8333SSuhasini.Peddada@Sun.COM 	    " \"y\" or \"n\"). ");
2619*8333SSuhasini.Peddada@Sun.COM 
2620*8333SSuhasini.Peddada@Sun.COM 	(void) printf(E_LINE);
2621*8333SSuhasini.Peddada@Sun.COM 	if (! yesno()) {
2622*8333SSuhasini.Peddada@Sun.COM 		return (1);
26230Sstevel@tonic-gate 	}
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
26260Sstevel@tonic-gate 		pactive = 1;
26270Sstevel@tonic-gate 	} else {
26280Sstevel@tonic-gate 		pactive = 0;
26290Sstevel@tonic-gate 	}
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
26325169Slclee 		Table[j] = Table[j + 1];
26330Sstevel@tonic-gate 	}
26340Sstevel@tonic-gate 
26350Sstevel@tonic-gate 	Table[j].systid = UNUSED;
26360Sstevel@tonic-gate 	Table[j].numsect = 0;
26370Sstevel@tonic-gate 	Table[j].relsect = 0;
26380Sstevel@tonic-gate 	Table[j].bootid = 0;
2639251Slclee 	(void) printf(E_LINE);
2640251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	if (pactive) {
26435169Slclee 		(void) printf(" This was the active partition.");
26440Sstevel@tonic-gate 	}
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate 	return (1);
26470Sstevel@tonic-gate }
26480Sstevel@tonic-gate 
26490Sstevel@tonic-gate /*
26500Sstevel@tonic-gate  * rm_blanks
26510Sstevel@tonic-gate  * Remove blanks from strings of user responses.
26520Sstevel@tonic-gate  */
2653251Slclee static void
2654251Slclee rm_blanks(char *s)
26550Sstevel@tonic-gate {
26560Sstevel@tonic-gate 	register int i, j;
26570Sstevel@tonic-gate 
26580Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
26590Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
26600Sstevel@tonic-gate 			continue;
26610Sstevel@tonic-gate 		else
26620Sstevel@tonic-gate 			/* Found first non-blank character of the string */
26630Sstevel@tonic-gate 			break;
26640Sstevel@tonic-gate 	}
26650Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
26660Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
26670Sstevel@tonic-gate 			/* Reached end of string */
26680Sstevel@tonic-gate 			return;
26690Sstevel@tonic-gate 		}
26700Sstevel@tonic-gate 	}
26710Sstevel@tonic-gate }
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate /*
26740Sstevel@tonic-gate  * getcyl
26750Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
26760Sstevel@tonic-gate  * string to a decimal value.
26770Sstevel@tonic-gate  */
2678251Slclee static int
2679251Slclee getcyl(void)
26800Sstevel@tonic-gate {
26810Sstevel@tonic-gate int slen, i, j;
26820Sstevel@tonic-gate unsigned int cyl;
2683251Slclee 	(void) gets(s);
26840Sstevel@tonic-gate 	rm_blanks(s);
26850Sstevel@tonic-gate 	slen = strlen(s);
26860Sstevel@tonic-gate 	j = 1;
26870Sstevel@tonic-gate 	cyl = 0;
2688251Slclee 	for (i = slen - 1; i >= 0; i--) {
26890Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
26900Sstevel@tonic-gate 			return (-1);
26910Sstevel@tonic-gate 		}
2692251Slclee 		cyl += (j * (s[i] - '0'));
26930Sstevel@tonic-gate 		j *= 10;
26940Sstevel@tonic-gate 	}
26950Sstevel@tonic-gate 	return (cyl);
26960Sstevel@tonic-gate }
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate /*
26990Sstevel@tonic-gate  * disptbl
27000Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
27010Sstevel@tonic-gate  * of the disk used for each partition.
27020Sstevel@tonic-gate  */
2703251Slclee static void
2704251Slclee disptbl(void)
27050Sstevel@tonic-gate {
27060Sstevel@tonic-gate 	int i;
27070Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
27080Sstevel@tonic-gate 	char *stat, *type;
27097563SPrasad.Singamsetty@Sun.COM 	int is_pmbr = 0;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
2712251Slclee 		(void) printf("WARNING: critical disk geometry information"
27135169Slclee 		    " missing!\n");
2714251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
27150Sstevel@tonic-gate 		exit(1);
27160Sstevel@tonic-gate 	}
27170Sstevel@tonic-gate 
2718251Slclee 	(void) printf(HOME);
2719251Slclee 	(void) printf(T_LINE);
2720251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
2721251Slclee 	(void) printf("             Cylinder size is %d (512 byte) blocks\n\n",
2722251Slclee 	    heads * sectors);
2723251Slclee 	(void) printf(
2724251Slclee 	    "                                               Cylinders\n");
2725251Slclee 	(void) printf(
2726251Slclee 	    "      Partition   Status    Type          Start   End   Length"
27270Sstevel@tonic-gate 	    "    %%\n");
2728251Slclee 	(void) printf(
2729251Slclee 	    "      =========   ======    ============  =====   ===   ======"
27300Sstevel@tonic-gate 	    "   ===");
27310Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
27320Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2733251Slclee 			(void) printf("\n");
2734251Slclee 			(void) printf(CLR_LIN);
27350Sstevel@tonic-gate 			continue;
27360Sstevel@tonic-gate 		}
27370Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
27385169Slclee 			stat = Actvstr;
27390Sstevel@tonic-gate 		else
27405169Slclee 			stat = NAstr;
27410Sstevel@tonic-gate 		switch (Table[i].systid) {
27420Sstevel@tonic-gate 		case UNIXOS:
27435169Slclee 			type = Ustr;
27445169Slclee 			break;
27450Sstevel@tonic-gate 		case SUNIXOS:
27465169Slclee 			type = SUstr;
27475169Slclee 			break;
27480Sstevel@tonic-gate 		case SUNIXOS2:
27495169Slclee 			type = SU2str;
27505169Slclee 			break;
27510Sstevel@tonic-gate 		case X86BOOT:
27525169Slclee 			type = X86str;
27535169Slclee 			break;
27540Sstevel@tonic-gate 		case DOSOS12:
27555169Slclee 			type = Dstr;
27565169Slclee 			break;
27570Sstevel@tonic-gate 		case DOSOS16:
27585169Slclee 			type = D16str;
27595169Slclee 			break;
27600Sstevel@tonic-gate 		case EXTDOS:
27615169Slclee 			type = EDstr;
27625169Slclee 			break;
27630Sstevel@tonic-gate 		case DOSDATA:
27645169Slclee 			type = DDstr;
27655169Slclee 			break;
27660Sstevel@tonic-gate 		case DOSHUGE:
27675169Slclee 			type = DBstr;
27685169Slclee 			break;
27690Sstevel@tonic-gate 		case PCIXOS:
27705169Slclee 			type = PCstr;
27715169Slclee 			break;
27720Sstevel@tonic-gate 		case DIAGPART:
27735169Slclee 			type = DIAGstr;
27745169Slclee 			break;
27750Sstevel@tonic-gate 		case FDISK_IFS:
27765169Slclee 			type = IFSstr;
27775169Slclee 			break;
27780Sstevel@tonic-gate 		case FDISK_AIXBOOT:
27795169Slclee 			type = AIXstr;
27805169Slclee 			break;
27810Sstevel@tonic-gate 		case FDISK_AIXDATA:
27825169Slclee 			type = AIXDstr;
27835169Slclee 			break;
27840Sstevel@tonic-gate 		case FDISK_OS2BOOT:
27855169Slclee 			type = OS2str;
27865169Slclee 			break;
27870Sstevel@tonic-gate 		case FDISK_WINDOWS:
27885169Slclee 			type = WINstr;
27895169Slclee 			break;
27900Sstevel@tonic-gate 		case FDISK_EXT_WIN:
27915169Slclee 			type = EWINstr;
27925169Slclee 			break;
27930Sstevel@tonic-gate 		case FDISK_FAT95:
27945169Slclee 			type = FAT95str;
27955169Slclee 			break;
27960Sstevel@tonic-gate 		case FDISK_EXTLBA:
27975169Slclee 			type = EXTLstr;
27985169Slclee 			break;
27990Sstevel@tonic-gate 		case FDISK_LINUX:
28005169Slclee 			type = LINUXstr;
28015169Slclee 			break;
28020Sstevel@tonic-gate 		case FDISK_CPM:
28035169Slclee 			type = CPMstr;
28045169Slclee 			break;
28050Sstevel@tonic-gate 		case FDISK_NOVELL3:
28065169Slclee 			type = NOVstr;
28075169Slclee 			break;
28080Sstevel@tonic-gate 		case FDISK_QNX4:
28095169Slclee 			type = QNXstr;
28105169Slclee 			break;
28110Sstevel@tonic-gate 		case FDISK_QNX42:
28125169Slclee 			type = QNX2str;
28135169Slclee 			break;
28140Sstevel@tonic-gate 		case FDISK_QNX43:
28155169Slclee 			type = QNX3str;
28165169Slclee 			break;
28170Sstevel@tonic-gate 		case FDISK_LINUXNAT:
28185169Slclee 			type = LINNATstr;
28195169Slclee 			break;
28200Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
28215169Slclee 			type = NTFSVOL1str;
28225169Slclee 			break;
28230Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
28245169Slclee 			type = NTFSVOL2str;
28255169Slclee 			break;
28260Sstevel@tonic-gate 		case FDISK_BSD:
28275169Slclee 			type = BSDstr;
28285169Slclee 			break;
28290Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
28305169Slclee 			type = NEXTSTEPstr;
28315169Slclee 			break;
28320Sstevel@tonic-gate 		case FDISK_BSDIFS:
28335169Slclee 			type = BSDIFSstr;
28345169Slclee 			break;
28350Sstevel@tonic-gate 		case FDISK_BSDISWAP:
28365169Slclee 			type = BSDISWAPstr;
28375169Slclee 			break;
28380Sstevel@tonic-gate 		case EFI_PMBR:
28395169Slclee 			type = EFIstr;
2840*8333SSuhasini.Peddada@Sun.COM 			if (lel(Table[i].numsect) == DK_MAX_2TB)
28417563SPrasad.Singamsetty@Sun.COM 				is_pmbr = 1;
28427563SPrasad.Singamsetty@Sun.COM 
28435169Slclee 			break;
28440Sstevel@tonic-gate 		default:
28455169Slclee 			type = Ostr;
28465169Slclee 			break;
28470Sstevel@tonic-gate 		}
2848*8333SSuhasini.Peddada@Sun.COM 		startcyl = lel(Table[i].relsect) /
28495936Sbharding 		    (unsigned long)(heads * sectors);
28507563SPrasad.Singamsetty@Sun.COM 
2851*8333SSuhasini.Peddada@Sun.COM 		if (lel(Table[i].numsect) == DK_MAX_2TB) {
28527563SPrasad.Singamsetty@Sun.COM 			endcyl = Numcyl - 1;
28537563SPrasad.Singamsetty@Sun.COM 			length = endcyl - startcyl + 1;
28547563SPrasad.Singamsetty@Sun.COM 		} else {
2855*8333SSuhasini.Peddada@Sun.COM 			length = lel(Table[i].numsect) /
28567563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors);
2857*8333SSuhasini.Peddada@Sun.COM 			if (lel(Table[i].numsect) %
28587563SPrasad.Singamsetty@Sun.COM 			    (unsigned long)(heads * sectors))
28597563SPrasad.Singamsetty@Sun.COM 				length++;
28607563SPrasad.Singamsetty@Sun.COM 			endcyl = startcyl + length - 1;
28617563SPrasad.Singamsetty@Sun.COM 		}
28627563SPrasad.Singamsetty@Sun.COM 
28637563SPrasad.Singamsetty@Sun.COM 		percent = length * 100 / Numcyl_usable;
28647563SPrasad.Singamsetty@Sun.COM 		if ((remainder = (length * 100 % Numcyl_usable)) != 0) {
28657563SPrasad.Singamsetty@Sun.COM 			if ((remainder * 100 / Numcyl_usable) > 50) {
28660Sstevel@tonic-gate 				/* round up */
28670Sstevel@tonic-gate 				percent++;
28680Sstevel@tonic-gate 			}
28690Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
28700Sstevel@tonic-gate 			/* rounded down */
28710Sstevel@tonic-gate 		}
28720Sstevel@tonic-gate 		if (percent > 100)
28730Sstevel@tonic-gate 			percent = 100;
2874251Slclee 		(void) printf(
2875251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
2876251Slclee 		    "    %3d",
2877251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
28780Sstevel@tonic-gate 	}
28797563SPrasad.Singamsetty@Sun.COM 
28800Sstevel@tonic-gate 	/* Print warning message if table is empty */
28810Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
2882251Slclee 		(void) printf(W_LINE);
2883251Slclee 		(void) printf("WARNING: no partitions are defined!");
28840Sstevel@tonic-gate 	} else {
28850Sstevel@tonic-gate 		/* Clear the warning line */
2886251Slclee 		(void) printf(W_LINE);
28877563SPrasad.Singamsetty@Sun.COM 
28887563SPrasad.Singamsetty@Sun.COM 		/* Print warning if disk > 2TB and is not EFI PMBR */
28897563SPrasad.Singamsetty@Sun.COM 		if (!is_pmbr && (dev_capacity > DK_MAX_2TB))
28907563SPrasad.Singamsetty@Sun.COM 			(void) printf("WARNING: Disk is larger than 2 TB. "
28917563SPrasad.Singamsetty@Sun.COM 			    "Upper limit is 2 TB for non-EFI partition ID\n");
28920Sstevel@tonic-gate 	}
28930Sstevel@tonic-gate }
28940Sstevel@tonic-gate 
28950Sstevel@tonic-gate /*
28960Sstevel@tonic-gate  * print_Table
28970Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
28980Sstevel@tonic-gate  * the selected disk device.
28990Sstevel@tonic-gate  */
2900251Slclee static void
2901251Slclee print_Table(void)
2902251Slclee {
29030Sstevel@tonic-gate 	int i;
29040Sstevel@tonic-gate 
2905251Slclee 	(void) fprintf(stderr,
29060Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
29070Sstevel@tonic-gate 	    "   NUMSECT\n");
29080Sstevel@tonic-gate 
29090Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2910251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
2911251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
2912251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
2913251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
29145169Slclee 		(void) fprintf(stderr, "%-8d ",
29155169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
29160Sstevel@tonic-gate 
2917251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
2918251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
29195169Slclee 		(void) fprintf(stderr, "%-8d ",
29205169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
2921*8333SSuhasini.Peddada@Sun.COM 		(void) fprintf(stderr, "%-10u ", lel(Table[i].relsect));
2922*8333SSuhasini.Peddada@Sun.COM 		(void) fprintf(stderr, "%-10u\n", lel(Table[i].numsect));
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate 	}
29250Sstevel@tonic-gate }
29260Sstevel@tonic-gate 
29270Sstevel@tonic-gate /*
29280Sstevel@tonic-gate  * copy_Table_to_Old_Table
29290Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
29300Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
29310Sstevel@tonic-gate  * ones compared when determining if Table has changed.
29320Sstevel@tonic-gate  */
2933251Slclee static void
2934251Slclee copy_Table_to_Old_Table(void)
29350Sstevel@tonic-gate {
29360Sstevel@tonic-gate 	int i;
29370Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
29385169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
29390Sstevel@tonic-gate 	}
29400Sstevel@tonic-gate }
29410Sstevel@tonic-gate 
29420Sstevel@tonic-gate /*
29430Sstevel@tonic-gate  * nulltbl
29440Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
29450Sstevel@tonic-gate  * fdisk table.
29460Sstevel@tonic-gate  */
2947251Slclee static void
2948251Slclee nulltbl(void)
29490Sstevel@tonic-gate {
29500Sstevel@tonic-gate 	int i;
29510Sstevel@tonic-gate 
29520Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
29535169Slclee 		Table[i].systid = UNUSED;
2954*8333SSuhasini.Peddada@Sun.COM 		Table[i].numsect = lel(UNUSED);
2955*8333SSuhasini.Peddada@Sun.COM 		Table[i].relsect = lel(UNUSED);
29565169Slclee 		Table[i].bootid = 0;
29570Sstevel@tonic-gate 	}
29580Sstevel@tonic-gate }
29590Sstevel@tonic-gate 
29600Sstevel@tonic-gate /*
29610Sstevel@tonic-gate  * copy_Bootblk_to_Table
29620Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
29630Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
29640Sstevel@tonic-gate  */
2965251Slclee static void
2966251Slclee copy_Bootblk_to_Table(void)
29670Sstevel@tonic-gate {
29680Sstevel@tonic-gate 	int i, j;
29690Sstevel@tonic-gate 	char *bootptr;
29700Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
2973251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
29740Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
2975*8333SSuhasini.Peddada@Sun.COM 	if (les(Bootblk->signature) != MBB_MAGIC)  {
29760Sstevel@tonic-gate 		/* Signature is missing */
29770Sstevel@tonic-gate 		nulltbl();
2978251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
29790Sstevel@tonic-gate 		return;
29800Sstevel@tonic-gate 	}
29810Sstevel@tonic-gate 	/*
29820Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
29830Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
29840Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
29850Sstevel@tonic-gate 	 * those that are valid.
29860Sstevel@tonic-gate 	 */
29870Sstevel@tonic-gate 	j = 0;
29880Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
29890Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
29900Sstevel@tonic-gate 			/* Null entry */
29910Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29920Sstevel@tonic-gate 		} else {
2993251Slclee 			fill_ipart(bootptr, &Table[j]);
29940Sstevel@tonic-gate 			j++;
29950Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29960Sstevel@tonic-gate 		}
29970Sstevel@tonic-gate 	}
29980Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
29990Sstevel@tonic-gate 		Table[i].systid = UNUSED;
3000*8333SSuhasini.Peddada@Sun.COM 		Table[i].numsect = lel(UNUSED);
3001*8333SSuhasini.Peddada@Sun.COM 		Table[i].relsect = lel(UNUSED);
30020Sstevel@tonic-gate 		Table[i].bootid = 0;
30030Sstevel@tonic-gate 
30040Sstevel@tonic-gate 	}
30050Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
3006251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
30070Sstevel@tonic-gate 	copy_Table_to_Bootblk();
30080Sstevel@tonic-gate }
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate /*
30110Sstevel@tonic-gate  * fill_ipart
30120Sstevel@tonic-gate  * Initialize ipart structure values.
30130Sstevel@tonic-gate  */
3014251Slclee static void
30150Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
30160Sstevel@tonic-gate {
30170Sstevel@tonic-gate #ifdef sparc
30180Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
3019251Slclee 	partp->bootid	= getbyte(&bootptr);
3020251Slclee 	partp->beghead	= getbyte(&bootptr);
3021251Slclee 	partp->begsect	= getbyte(&bootptr);
3022251Slclee 	partp->begcyl	= getbyte(&bootptr);
3023251Slclee 	partp->systid	= getbyte(&bootptr);
3024251Slclee 	partp->endhead	= getbyte(&bootptr);
3025251Slclee 	partp->endsect	= getbyte(&bootptr);
3026251Slclee 	partp->endcyl	= getbyte(&bootptr);
3027251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
3028251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
30290Sstevel@tonic-gate #else
30300Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
30310Sstevel@tonic-gate #endif
30320Sstevel@tonic-gate }
30330Sstevel@tonic-gate 
30340Sstevel@tonic-gate /*
3035251Slclee  * getbyte, getlong
30360Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
30370Sstevel@tonic-gate  */
30380Sstevel@tonic-gate #ifdef sparc
3039251Slclee uchar_t
3040251Slclee getbyte(char **bp)
30410Sstevel@tonic-gate {
3042251Slclee 	uchar_t	b;
3043251Slclee 
3044251Slclee 	b = (uchar_t)**bp;
30450Sstevel@tonic-gate 	*bp = *bp + 1;
30460Sstevel@tonic-gate 	return (b);
30470Sstevel@tonic-gate }
30480Sstevel@tonic-gate 
3049251Slclee uint32_t
3050251Slclee getlong(char **bp)
30510Sstevel@tonic-gate {
3052251Slclee 	int32_t	b, bh, bl;
30530Sstevel@tonic-gate 
30540Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
30550Sstevel@tonic-gate 	*bp += 2;
30560Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
30570Sstevel@tonic-gate 	*bp += 2;
30580Sstevel@tonic-gate 
30590Sstevel@tonic-gate 	b = (bh << 16) | bl;
3060251Slclee 	return ((uint32_t)b);
30610Sstevel@tonic-gate }
30620Sstevel@tonic-gate #endif
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate /*
30650Sstevel@tonic-gate  * copy_Table_to_Bootblk
30660Sstevel@tonic-gate  * Copy the table into the 512 boot record. Note that the unused
30670Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
30680Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
30690Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
30700Sstevel@tonic-gate  */
3071251Slclee static void
3072251Slclee copy_Table_to_Bootblk(void)
30730Sstevel@tonic-gate {
30740Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
30750Sstevel@tonic-gate 
30760Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
30770Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
30780Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
30790Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
30805169Slclee 		if (tbl_ptr->systid == UNUSED)
30815169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
30825169Slclee 		else
30835169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
30840Sstevel@tonic-gate 	}
3085*8333SSuhasini.Peddada@Sun.COM 	Bootblk->signature = les(MBB_MAGIC);
30860Sstevel@tonic-gate }
30870Sstevel@tonic-gate 
30880Sstevel@tonic-gate /*
30890Sstevel@tonic-gate  * TableChanged
30900Sstevel@tonic-gate  * 	Check for any changes in the partition table.
30910Sstevel@tonic-gate  */
3092251Slclee static int
3093251Slclee TableChanged(void)
30940Sstevel@tonic-gate {
30950Sstevel@tonic-gate 	int i, changed;
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate 	changed = 0;
30980Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
30995169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
31005169Slclee 			/* Partition table changed, write back to disk */
31015169Slclee 			changed = 1;
31025169Slclee 		}
31030Sstevel@tonic-gate 	}
31040Sstevel@tonic-gate 
31050Sstevel@tonic-gate 	return (changed);
31060Sstevel@tonic-gate }
31070Sstevel@tonic-gate 
31080Sstevel@tonic-gate /*
31090Sstevel@tonic-gate  * ffile_write
31100Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
31110Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
31120Sstevel@tonic-gate  */
3113251Slclee static void
3114251Slclee ffile_write(char *file)
31150Sstevel@tonic-gate {
31160Sstevel@tonic-gate 	register int	i;
31170Sstevel@tonic-gate 	FILE *fp;
31180Sstevel@tonic-gate 
31190Sstevel@tonic-gate 	/*
31200Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
31210Sstevel@tonic-gate 	 * Open file and write it.
31220Sstevel@tonic-gate 	 */
31230Sstevel@tonic-gate 	if (file != (char *)stdout) {
31245169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
31255169Slclee 			(void) fprintf(stderr,
31265169Slclee 			    "fdisk: Cannot open output file %s.\n",
31275169Slclee 			    file);
31285169Slclee 			exit(1);
31295169Slclee 		}
31300Sstevel@tonic-gate 	}
31310Sstevel@tonic-gate 	else
31325169Slclee 		fp = stdout;
31330Sstevel@tonic-gate 
31340Sstevel@tonic-gate 	/*
31350Sstevel@tonic-gate 	 * Write the fdisk table information
31360Sstevel@tonic-gate 	 */
3137251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
3138251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
3139251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3140251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
3141251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
3142251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
3143251Slclee 	(void) fprintf(fp, "*\n");
31440Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
31450Sstevel@tonic-gate 	if (v_flag) {
3146251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
3147251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3148251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
3149251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
3150251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
3151251Slclee 		(void) fprintf(fp, "*\n");
31520Sstevel@tonic-gate 	}
3153251Slclee 	(void) fprintf(fp, "* systid:\n");
3154251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
3155251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
3156251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
3157251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
3158251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
3159251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
3160251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
3161251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
3162251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
3163251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
3164251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
3165251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
3166251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
3167251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
3168251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
3169251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
3170251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
3171251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
3172251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
3173251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
3174251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
3175251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
3176251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
3177251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
3178251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
3179251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
3180251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
3181251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
3182251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
3183251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
3184251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
3185251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
3186251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
3187251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
3188251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
3189251Slclee 	(void) fprintf(fp, "*\n");
3190251Slclee 	(void) fprintf(fp,
31910Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
31927563SPrasad.Singamsetty@Sun.COM 	    "    Rsect      Numsect\n");
31937563SPrasad.Singamsetty@Sun.COM 
31940Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
3195*8333SSuhasini.Peddada@Sun.COM 		if (Table[i].systid != UNUSED)
3196251Slclee 			(void) fprintf(fp,
31977563SPrasad.Singamsetty@Sun.COM 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u"
31987563SPrasad.Singamsetty@Sun.COM 			    " %-10u\n",
31990Sstevel@tonic-gate 			    Table[i].systid,
32000Sstevel@tonic-gate 			    Table[i].bootid,
32010Sstevel@tonic-gate 			    Table[i].beghead,
32020Sstevel@tonic-gate 			    Table[i].begsect & 0x3f,
32030Sstevel@tonic-gate 			    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
32045169Slclee 			    0xc0) << 2)),
32050Sstevel@tonic-gate 			    Table[i].endhead,
32060Sstevel@tonic-gate 			    Table[i].endsect & 0x3f,
32070Sstevel@tonic-gate 			    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
32085169Slclee 			    0xc0) << 2)),
3209*8333SSuhasini.Peddada@Sun.COM 			    lel(Table[i].relsect),
3210*8333SSuhasini.Peddada@Sun.COM 			    lel(Table[i].numsect));
32110Sstevel@tonic-gate 	}
32120Sstevel@tonic-gate 	if (fp != stdout)
3213251Slclee 		(void) fclose(fp);
32140Sstevel@tonic-gate }
32150Sstevel@tonic-gate 
32160Sstevel@tonic-gate /*
32170Sstevel@tonic-gate  * fix_slice
32180Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
32190Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
32200Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
32210Sstevel@tonic-gate  */
3222251Slclee static void
3223251Slclee fix_slice(void)
32240Sstevel@tonic-gate {
32250Sstevel@tonic-gate 	int	i;
32267563SPrasad.Singamsetty@Sun.COM 	uint32_t	numsect;
32270Sstevel@tonic-gate 
32280Sstevel@tonic-gate 	if (io_image) {
3229251Slclee 		return;
32300Sstevel@tonic-gate 	}
32310Sstevel@tonic-gate 
32320Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
32330Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
32340Sstevel@tonic-gate 			/*
32350Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
32360Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
32370Sstevel@tonic-gate 			 * the partition.
32380Sstevel@tonic-gate 			 */
3239*8333SSuhasini.Peddada@Sun.COM 			numsect = lel(Table[i].numsect);
32400Sstevel@tonic-gate 			break;
32410Sstevel@tonic-gate 		}
32420Sstevel@tonic-gate 	}
32430Sstevel@tonic-gate 
32440Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
32450Sstevel@tonic-gate 		if (!io_nifdisk) {
32460Sstevel@tonic-gate 			(void) fprintf(stderr,
32470Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
32480Sstevel@tonic-gate 			    " checked.\n");
32490Sstevel@tonic-gate 		}
3250251Slclee 		return;
32510Sstevel@tonic-gate 	}
32520Sstevel@tonic-gate 
3253251Slclee 	if (readvtoc() != VTOC_OK) {
32540Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
32555169Slclee 	}
32565169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
32575169Slclee 		/* Special case for slice two (entire disk) */
32585169Slclee 		if (i == 2) {
32595169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
32605169Slclee 				(void) fprintf(stderr,
32617563SPrasad.Singamsetty@Sun.COM 				    "slice %d starts at %llu, is not at"
32625169Slclee 				    " start of partition",
32635169Slclee 				    i, disk_vtoc.v_part[i].p_start);
32645169Slclee 				if (!io_nifdisk) {
32655169Slclee 					(void) printf(" adjust ?:");
32665169Slclee 					if (yesno())
32670Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
32685169Slclee 				} else {
32695169Slclee 					disk_vtoc.v_part[i].p_start = 0;
32705169Slclee 					(void) fprintf(stderr, " adjusted!\n");
32710Sstevel@tonic-gate 				}
32725169Slclee 
32735169Slclee 			}
32745169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
32755169Slclee 				(void) fprintf(stderr,
32767563SPrasad.Singamsetty@Sun.COM 				    "slice %d size %llu does not cover"
32775169Slclee 				    " complete partition",
32785169Slclee 				    i, disk_vtoc.v_part[i].p_size);
32795169Slclee 				if (!io_nifdisk) {
32805169Slclee 					(void) printf(" adjust ?:");
32815169Slclee 					if (yesno())
32820Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
32830Sstevel@tonic-gate 						    numsect;
32845169Slclee 				} else {
32855169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
32865169Slclee 					(void) fprintf(stderr, " adjusted!\n");
32870Sstevel@tonic-gate 				}
32885169Slclee 			}
32895169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
32905169Slclee 				(void) fprintf(stderr,
32915169Slclee 				    "slice %d tag was %d should be %d",
32925169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
32935169Slclee 				    V_BACKUP);
32945169Slclee 				if (!io_nifdisk) {
3295251Slclee 					(void) printf(" fix ?:");
32965169Slclee 					if (yesno())
32970Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
32980Sstevel@tonic-gate 						    V_BACKUP;
32995169Slclee 				} else {
33000Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
33010Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
33020Sstevel@tonic-gate 				}
33035169Slclee 			}
33045169Slclee 			continue;
33055169Slclee 		}
33065169Slclee 		if (io_ADJT) {
33075169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
33085169Slclee 			    disk_vtoc.v_part[i].p_start +
33095169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
33105169Slclee 				(void) fprintf(stderr,
33117563SPrasad.Singamsetty@Sun.COM 				    "slice %d (start %llu, end %llu)"
33125169Slclee 				    " is larger than the partition",
33135169Slclee 				    i, disk_vtoc.v_part[i].p_start,
33145169Slclee 				    disk_vtoc.v_part[i].p_start +
33155169Slclee 				    disk_vtoc.v_part[i].p_size);
33165169Slclee 				if (!io_nifdisk) {
33175169Slclee 					(void) printf(" remove ?:");
33185169Slclee 					if (yesno()) {
33190Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
33200Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
33210Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
33220Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
33230Sstevel@tonic-gate 					}
33240Sstevel@tonic-gate 				} else {
33255169Slclee 					disk_vtoc.v_part[i].p_size = 0;
33265169Slclee 					disk_vtoc.v_part[i].p_start = 0;
33275169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
33285169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
33290Sstevel@tonic-gate 					(void) fprintf(stderr,
33305169Slclee 					    " removed!\n");
33315169Slclee 				}
33325169Slclee 			}
33335169Slclee 			continue;
33345169Slclee 		}
33355169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
33365169Slclee 			(void) fprintf(stderr,
33377563SPrasad.Singamsetty@Sun.COM 			    "slice %d (start %llu) is larger than the "
33387563SPrasad.Singamsetty@Sun.COM 			    "partition", i, disk_vtoc.v_part[i].p_start);
33395169Slclee 			if (!io_nifdisk) {
33405169Slclee 				(void) printf(" remove ?:");
33415169Slclee 				if (yesno()) {
33425169Slclee 					disk_vtoc.v_part[i].p_size = 0;
33435169Slclee 					disk_vtoc.v_part[i].p_start = 0;
33445169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
33455169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
33460Sstevel@tonic-gate 				}
33475169Slclee 			} else {
33485169Slclee 				disk_vtoc.v_part[i].p_size = 0;
33495169Slclee 				disk_vtoc.v_part[i].p_start = 0;
33505169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
33515169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
33525169Slclee 				(void) fprintf(stderr,
33535169Slclee 				" removed!\n");
33545169Slclee 			}
33555169Slclee 		} else if (disk_vtoc.v_part[i].p_start
33565169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
33575169Slclee 			(void) fprintf(stderr,
33587563SPrasad.Singamsetty@Sun.COM 			    "slice %d (end %llu) is larger"
33595169Slclee 			    " than the partition",
33605169Slclee 			    i,
33615169Slclee 			    disk_vtoc.v_part[i].p_start +
33625169Slclee 			    disk_vtoc.v_part[i].p_size);
33635169Slclee 			if (!io_nifdisk) {
33645169Slclee 				(void) printf(" adjust ?:");
33655169Slclee 				if (yesno()) {
33665169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
33675169Slclee 				}
33685169Slclee 			} else {
33695169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
33705169Slclee 				(void) fprintf(stderr, " adjusted!\n");
33710Sstevel@tonic-gate 			}
33720Sstevel@tonic-gate 		}
33730Sstevel@tonic-gate 	}
33740Sstevel@tonic-gate #if 1		/* bh for now */
33750Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
33760Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
33770Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
33780Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
33790Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
33800Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
33810Sstevel@tonic-gate #endif
33820Sstevel@tonic-gate 
33830Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
33840Sstevel@tonic-gate 	if (!io_readonly)
3385251Slclee 		(void) writevtoc();
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate 
33880Sstevel@tonic-gate /*
33890Sstevel@tonic-gate  * yesno
33900Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
33910Sstevel@tonic-gate  */
33920Sstevel@tonic-gate 
3393251Slclee static int
3394251Slclee yesno(void)
33950Sstevel@tonic-gate {
33960Sstevel@tonic-gate 	char	s[80];
33970Sstevel@tonic-gate 
33980Sstevel@tonic-gate 	for (;;) {
3399251Slclee 		(void) gets(s);
34000Sstevel@tonic-gate 		rm_blanks(s);
34010Sstevel@tonic-gate 		if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) {
3402251Slclee 			(void) printf(E_LINE);
3403251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
34040Sstevel@tonic-gate 			continue;
34050Sstevel@tonic-gate 		}
34060Sstevel@tonic-gate 		if (s[0] == 'y')
34070Sstevel@tonic-gate 			return (1);
34080Sstevel@tonic-gate 		else
34090Sstevel@tonic-gate 			return (0);
34100Sstevel@tonic-gate 	}
34110Sstevel@tonic-gate }
34120Sstevel@tonic-gate 
34130Sstevel@tonic-gate /*
34140Sstevel@tonic-gate  * readvtoc
34150Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
34160Sstevel@tonic-gate  */
3417251Slclee static int
3418251Slclee readvtoc(void)
34190Sstevel@tonic-gate {
34200Sstevel@tonic-gate 	int	i;
34210Sstevel@tonic-gate 	int	retval = VTOC_OK;
34220Sstevel@tonic-gate 
34237563SPrasad.Singamsetty@Sun.COM 	if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) {
34240Sstevel@tonic-gate 		if (i == VT_EINVAL) {
34250Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
34260Sstevel@tonic-gate 			vt_inval++;
34270Sstevel@tonic-gate 			retval = VTOC_INVAL;
34280Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
34290Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
34305169Slclee 			    "GPT\n");
34310Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
34320Sstevel@tonic-gate 		} else {
34330Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
34340Sstevel@tonic-gate 			retval = VTOC_RWERR;
34350Sstevel@tonic-gate 		}
34360Sstevel@tonic-gate 	}
34370Sstevel@tonic-gate 	return (retval);
34380Sstevel@tonic-gate }
34390Sstevel@tonic-gate 
34400Sstevel@tonic-gate /*
34410Sstevel@tonic-gate  * writevtoc
34420Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
34430Sstevel@tonic-gate  */
3444251Slclee static int
3445251Slclee writevtoc(void)
34460Sstevel@tonic-gate {
34470Sstevel@tonic-gate 	int	i;
34480Sstevel@tonic-gate 	int	retval = 0;
34490Sstevel@tonic-gate 
34507563SPrasad.Singamsetty@Sun.COM 	if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) {
34510Sstevel@tonic-gate 		if (i == VT_EINVAL) {
34520Sstevel@tonic-gate 			(void) fprintf(stderr,
34530Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
34540Sstevel@tonic-gate 			retval = VTOC_INVAL;
34550Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
34560Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
34575169Slclee 			    "GPT\n");
34580Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
34590Sstevel@tonic-gate 		} else {
34600Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
34610Sstevel@tonic-gate 			retval = VTOC_RWERR;
34620Sstevel@tonic-gate 		}
34630Sstevel@tonic-gate 	}
34640Sstevel@tonic-gate 	return (retval);
34650Sstevel@tonic-gate }
34660Sstevel@tonic-gate 
34670Sstevel@tonic-gate /*
34680Sstevel@tonic-gate  * efi_ioctl
34690Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
34700Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
34710Sstevel@tonic-gate  */
34720Sstevel@tonic-gate static int
34730Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
34740Sstevel@tonic-gate {
34750Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
34760Sstevel@tonic-gate 	int error;
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
34790Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	return (error);
34820Sstevel@tonic-gate }
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate /*
34850Sstevel@tonic-gate  * clear_efi
34860Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
34870Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
34880Sstevel@tonic-gate  */
3489251Slclee static int
3490251Slclee clear_efi(void)
34910Sstevel@tonic-gate {
34920Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
34930Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate 	/*
34960Sstevel@tonic-gate 	 * see if we can read the EFI label
34970Sstevel@tonic-gate 	 */
34980Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
34990Sstevel@tonic-gate 		return (VT_ERROR);
35000Sstevel@tonic-gate 	}
35010Sstevel@tonic-gate 
35020Sstevel@tonic-gate 	/*
35030Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
35040Sstevel@tonic-gate 	 */
35050Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
35060Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
35090Sstevel@tonic-gate 		return (VT_ERROR);
35100Sstevel@tonic-gate 	}
35110Sstevel@tonic-gate 
35120Sstevel@tonic-gate 	/*
35130Sstevel@tonic-gate 	 * clear the primary label
35140Sstevel@tonic-gate 	 */
35150Sstevel@tonic-gate 	if (io_debug) {
3516251Slclee 		(void) fprintf(stderr,
3517251Slclee 		    "\tClearing primary EFI label at block %lld\n",
3518251Slclee 		    dk_ioc.dki_lba);
35190Sstevel@tonic-gate 	}
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
35220Sstevel@tonic-gate 		free(dk_ioc.dki_data);
35230Sstevel@tonic-gate 		switch (errno) {
35240Sstevel@tonic-gate 			case EIO:
35250Sstevel@tonic-gate 				return (VT_EIO);
35260Sstevel@tonic-gate 			case EINVAL:
35270Sstevel@tonic-gate 				return (VT_EINVAL);
35280Sstevel@tonic-gate 			default:
35290Sstevel@tonic-gate 				return (VT_ERROR);
35300Sstevel@tonic-gate 		}
35310Sstevel@tonic-gate 	}
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	/*
35340Sstevel@tonic-gate 	 * clear the backup partition table
35350Sstevel@tonic-gate 	 */
35360Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
35370Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
35380Sstevel@tonic-gate 	dk_ioc.dki_data++;
35390Sstevel@tonic-gate 	if (io_debug) {
3540251Slclee 		(void) fprintf(stderr,
3541251Slclee 		    "\tClearing backup partition table at block %lld\n",
3542251Slclee 		    dk_ioc.dki_lba);
35430Sstevel@tonic-gate 	}
35440Sstevel@tonic-gate 
35450Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
35460Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
35475169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
35485169Slclee 		    errno);
35490Sstevel@tonic-gate 	}
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 	/*
35520Sstevel@tonic-gate 	 * clear the backup label
35530Sstevel@tonic-gate 	 */
35540Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
35550Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
35560Sstevel@tonic-gate 	dk_ioc.dki_data--;
35570Sstevel@tonic-gate 	if (io_debug) {
3558251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
3559251Slclee 		    "%lld\n", dk_ioc.dki_lba);
35600Sstevel@tonic-gate 	}
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
3563251Slclee 		(void) fprintf(stderr,
3564251Slclee 		    "\tUnable to clear backup EFI label at "
3565251Slclee 		    "block %llu; errno %d\n",
3566251Slclee 		    efi_vtoc->efi_last_lba,
3567251Slclee 		    errno);
35680Sstevel@tonic-gate 	}
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 	free(dk_ioc.dki_data);
35710Sstevel@tonic-gate 	efi_free(efi_vtoc);
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 	return (0);
35740Sstevel@tonic-gate }
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate /*
35770Sstevel@tonic-gate  * clear_vtoc
35780Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
35790Sstevel@tonic-gate  *      device.
35800Sstevel@tonic-gate  */
3581251Slclee static void
35820Sstevel@tonic-gate clear_vtoc(int table, int part)
35830Sstevel@tonic-gate {
35840Sstevel@tonic-gate 	struct ipart *clr_table;
35850Sstevel@tonic-gate 	struct dk_label disk_label;
35867563SPrasad.Singamsetty@Sun.COM 	uint32_t pcyl, ncyl, count;
35877563SPrasad.Singamsetty@Sun.COM 	diskaddr_t backup_block, solaris_offset;
35887563SPrasad.Singamsetty@Sun.COM 	ssize_t bytes;
35896549Sbharding 	off_t seek_byte;
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate #ifdef DEBUG
35920Sstevel@tonic-gate 	struct dk_label	read_label;
35930Sstevel@tonic-gate #endif /* DEBUG */
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 	if (table == OLD) {
35960Sstevel@tonic-gate 		clr_table = &Old_Table[part];
35970Sstevel@tonic-gate 	} else {
35980Sstevel@tonic-gate 		clr_table = &Table[part];
35990Sstevel@tonic-gate 	}
36000Sstevel@tonic-gate 
3601251Slclee 	(void) memset(&disk_label, 0, sizeof (struct dk_label));
36020Sstevel@tonic-gate 
3603*8333SSuhasini.Peddada@Sun.COM 	seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	if (io_debug) {
36066549Sbharding 		(void) fprintf(stderr,
36076549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
36086549Sbharding 		    (uint64_t)seek_byte,
3609*8333SSuhasini.Peddada@Sun.COM 		    (uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET));
36100Sstevel@tonic-gate 	}
36110Sstevel@tonic-gate 
36120Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3613251Slclee 		(void) fprintf(stderr,
36146549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
36156549Sbharding 		    (uint64_t)seek_byte);
3616251Slclee 		return;
36170Sstevel@tonic-gate 	}
36180Sstevel@tonic-gate 
36190Sstevel@tonic-gate 	bytes = write(Dev, &disk_label, sizeof (struct dk_label));
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3622251Slclee 		(void) fprintf(stderr,
36237563SPrasad.Singamsetty@Sun.COM 		    "\tWarning: only %d bytes written to clear primary"
36247563SPrasad.Singamsetty@Sun.COM 		    " VTOC!\n", bytes);
36250Sstevel@tonic-gate 	}
36260Sstevel@tonic-gate 
36270Sstevel@tonic-gate #ifdef DEBUG
36280Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3629251Slclee 		(void) fprintf(stderr,
36306549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
36316549Sbharding 		    (uint64_t)seek_byte);
3632251Slclee 		return;
36330Sstevel@tonic-gate 	} else {
36346549Sbharding 		(void) fprintf(stderr,
36356549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
36366549Sbharding 		    (uint64_t)seek_byte);
36370Sstevel@tonic-gate 	}
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3642251Slclee 		(void) fprintf(stderr,
3643251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
36440Sstevel@tonic-gate 		    bytes);
36450Sstevel@tonic-gate 	}
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3648251Slclee 		(void) fprintf(stderr,
3649251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
36500Sstevel@tonic-gate 	} else {
3651251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
36520Sstevel@tonic-gate 		    "read_label\n");
36530Sstevel@tonic-gate 	}
36540Sstevel@tonic-gate #endif /* DEBUG */
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	/* Clear backup label */
3657*8333SSuhasini.Peddada@Sun.COM 	pcyl = lel(clr_table->numsect) / (heads * sectors);
3658*8333SSuhasini.Peddada@Sun.COM 	solaris_offset = lel(clr_table->relsect);
36590Sstevel@tonic-gate 	ncyl = pcyl - acyl;
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
36620Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
36630Sstevel@tonic-gate 
36640Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
36656549Sbharding 		seek_byte = (off_t)(solaris_offset + backup_block) * 512;
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3668251Slclee 			(void) fprintf(stderr,
36696549Sbharding 			    "\tError seeking to backup label at byte %llu on "
36706549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
3671251Slclee 			return;
36720Sstevel@tonic-gate 		}
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 		if (io_debug) {
3675251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
36766549Sbharding 			    " byte %llu (block %llu)\n",
36776549Sbharding 			    (uint64_t)seek_byte,
36786549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
36790Sstevel@tonic-gate 		}
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 		bytes = write(Dev, &disk_label, sizeof (struct dk_label));
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 		if (bytes != sizeof (struct dk_label)) {
3684251Slclee 			(void) fprintf(stderr,
3685251Slclee 			    "\t\tWarning: only %d bytes written to "
36866549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
36876549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
36880Sstevel@tonic-gate 		}
36890Sstevel@tonic-gate 
36900Sstevel@tonic-gate #ifdef DEBUG
36910Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3692251Slclee 		(void) fprintf(stderr,
36936549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
36946549Sbharding 		    (uint64_t)seek_byte);
3695251Slclee 		return;
36960Sstevel@tonic-gate 	} else {
36976549Sbharding 		(void) fprintf(stderr,
36986549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
36996549Sbharding 		    (uint64_t)seek_byte);
37000Sstevel@tonic-gate 	}
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3705251Slclee 		(void) fprintf(stderr,
3706251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
3707251Slclee 		    bytes);
37080Sstevel@tonic-gate 	}
37090Sstevel@tonic-gate 
37100Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3711251Slclee 		(void) fprintf(stderr,
3712251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
37130Sstevel@tonic-gate 	} else {
3714251Slclee 		(void) fprintf(stderr,
3715251Slclee 		    "DEBUG: Good compare of disk_label and backup "
37160Sstevel@tonic-gate 		    "read_label\n");
37170Sstevel@tonic-gate 	}
37180Sstevel@tonic-gate #endif /* DEBUG */
37190Sstevel@tonic-gate 
37200Sstevel@tonic-gate 		backup_block += 2;
37210Sstevel@tonic-gate 	}
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
37250Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
37260Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
37270Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
37280Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
37310Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
37320Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
37330Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
37360Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
37390Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
37400Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
37430Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
37440Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
37470Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate /*
37500Sstevel@tonic-gate  *  lecture_and_query
37510Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
37520Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
37530Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
37540Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
37550Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
37560Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
37570Sstevel@tonic-gate  */
3758251Slclee static int
37590Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
37600Sstevel@tonic-gate {
37610Sstevel@tonic-gate 	if (io_nifdisk)
37620Sstevel@tonic-gate 		return (0);
37630Sstevel@tonic-gate 
3764251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
3765251Slclee 	(void) fprintf(stderr, "%s", warning);
3766251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
3767251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 	return (yesno());
37700Sstevel@tonic-gate }
37710Sstevel@tonic-gate 
3772251Slclee static void
37730Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
37740Sstevel@tonic-gate {
37757563SPrasad.Singamsetty@Sun.COM 	struct extvtoc v;
37760Sstevel@tonic-gate 	struct dk_geom d;
37770Sstevel@tonic-gate 	struct part_info pi;
37787563SPrasad.Singamsetty@Sun.COM 	struct extpart_info extpi;
37797563SPrasad.Singamsetty@Sun.COM 	diskaddr_t totsize;
37800Sstevel@tonic-gate 	int idx = -1;
37810Sstevel@tonic-gate 
37820Sstevel@tonic-gate 	/*
37830Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
37840Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
37850Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
37860Sstevel@tonic-gate 	 */
37877563SPrasad.Singamsetty@Sun.COM 	if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) {
37887563SPrasad.Singamsetty@Sun.COM 		if (extpi.p_start != 0) {
37897563SPrasad.Singamsetty@Sun.COM 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
37907563SPrasad.Singamsetty@Sun.COM 			    devname)) {
37917563SPrasad.Singamsetty@Sun.COM 				(void) close(fd);
37927563SPrasad.Singamsetty@Sun.COM 				exit(1);
37937563SPrasad.Singamsetty@Sun.COM 			}
37947563SPrasad.Singamsetty@Sun.COM 		}
37957563SPrasad.Singamsetty@Sun.COM 	} else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
37960Sstevel@tonic-gate 		if (pi.p_start != 0) {
37970Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
37980Sstevel@tonic-gate 			    devname)) {
37990Sstevel@tonic-gate 				(void) close(fd);
38000Sstevel@tonic-gate 				exit(1);
38010Sstevel@tonic-gate 			}
38020Sstevel@tonic-gate 		}
38030Sstevel@tonic-gate 	} else {
38047563SPrasad.Singamsetty@Sun.COM 		if ((idx = read_extvtoc(fd, &v)) < 0) {
38050Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
38060Sstevel@tonic-gate 			    devname)) {
38070Sstevel@tonic-gate 				(void) close(fd);
38080Sstevel@tonic-gate 				exit(1);
38090Sstevel@tonic-gate 			}
38100Sstevel@tonic-gate 			return;
38110Sstevel@tonic-gate 		}
38120Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
38130Sstevel@tonic-gate 			perror(devname);
38140Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
38150Sstevel@tonic-gate 			    devname)) {
38160Sstevel@tonic-gate 				(void) close(fd);
38170Sstevel@tonic-gate 				exit(1);
38180Sstevel@tonic-gate 			}
38190Sstevel@tonic-gate 			return;
38200Sstevel@tonic-gate 		}
38217563SPrasad.Singamsetty@Sun.COM 		totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
38220Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
38230Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
38240Sstevel@tonic-gate 			    devname)) {
38250Sstevel@tonic-gate 				(void) close(fd);
38260Sstevel@tonic-gate 				exit(1);
38270Sstevel@tonic-gate 			}
38280Sstevel@tonic-gate 		}
38290Sstevel@tonic-gate 	}
38300Sstevel@tonic-gate }
38310Sstevel@tonic-gate 
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate /*
38340Sstevel@tonic-gate  * get_node
38350Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
38360Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
38370Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
38380Sstevel@tonic-gate  */
38390Sstevel@tonic-gate static char *
38400Sstevel@tonic-gate get_node(char *devname)
38410Sstevel@tonic-gate {
38420Sstevel@tonic-gate 	char *node;
38430Sstevel@tonic-gate 	struct stat statbuf;
38440Sstevel@tonic-gate 	size_t space;
38450Sstevel@tonic-gate 
38460Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
38470Sstevel@tonic-gate 	if (io_image)
38480Sstevel@tonic-gate 		return (devname);
38490Sstevel@tonic-gate 
38500Sstevel@tonic-gate 	node = devname;
38510Sstevel@tonic-gate 
38520Sstevel@tonic-gate 	/* Try the node as provided first */
38530Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
38540Sstevel@tonic-gate 		/*
38550Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
38560Sstevel@tonic-gate 		 * default path and try again.
38570Sstevel@tonic-gate 		 */
38580Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
38590Sstevel@tonic-gate 
38600Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
3861251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
38620Sstevel@tonic-gate 			    "for device node.\n");
38630Sstevel@tonic-gate 			exit(1);
38640Sstevel@tonic-gate 		}
38650Sstevel@tonic-gate 
38660Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
38670Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
38680Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
38690Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
38700Sstevel@tonic-gate 
38710Sstevel@tonic-gate 		/* Try to stat it again */
38720Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
38730Sstevel@tonic-gate 			/* Failed all options, give up */
3874251Slclee 			(void) fprintf(stderr,
3875251Slclee 			    "fdisk: Cannot stat device %s.\n",
38760Sstevel@tonic-gate 			    devname);
38770Sstevel@tonic-gate 			exit(1);
38780Sstevel@tonic-gate 		}
38790Sstevel@tonic-gate 	}
38800Sstevel@tonic-gate 
38810Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
38820Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
3883251Slclee 		(void) fprintf(stderr,
3884251Slclee 		    "fdisk: %s must be a raw device.\n", node);
38850Sstevel@tonic-gate 		exit(1);
38860Sstevel@tonic-gate 	}
38870Sstevel@tonic-gate 
38880Sstevel@tonic-gate 	return (node);
38890Sstevel@tonic-gate }
3890