xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 6549:763ed4e8708e)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
380Sstevel@tonic-gate  * This program reads the partition table on the specified device and
390Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
400Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
410Sstevel@tonic-gate  * options are also available.
420Sstevel@tonic-gate  */
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <stdlib.h>
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <errno.h>
490Sstevel@tonic-gate #include <fcntl.h>
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <sys/stat.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/param.h>
540Sstevel@tonic-gate #include <sys/systeminfo.h>
550Sstevel@tonic-gate #include <sys/efi_partition.h>
560Sstevel@tonic-gate #include <sys/byteorder.h>
570Sstevel@tonic-gate #include <sys/systeminfo.h>
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
600Sstevel@tonic-gate #include <sys/dkio.h>
610Sstevel@tonic-gate #include <sys/vtoc.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #define	CLR_SCR ""
640Sstevel@tonic-gate #define	CLR_LIN ""
650Sstevel@tonic-gate #define	HOME "" \
660Sstevel@tonic-gate 	""
670Sstevel@tonic-gate #define	Q_LINE ""
680Sstevel@tonic-gate #define	W_LINE ""
690Sstevel@tonic-gate #define	E_LINE ""
700Sstevel@tonic-gate #define	M_LINE "" \
710Sstevel@tonic-gate 	""
720Sstevel@tonic-gate #define	T_LINE ""
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #define	DEFAULT_PATH	"/dev/rdsk/"
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /* XXX - should be in fdisk.h, used by sd as well */
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /*
790Sstevel@tonic-gate  * the MAX values are the maximum usable values for BIOS chs values
800Sstevel@tonic-gate  * The MAX_CYL value of 1022 is the maximum usable value
810Sstevel@tonic-gate  *   the value of 1023 is a fence value,
820Sstevel@tonic-gate  *   indicating no CHS geometry exists for the corresponding LBA value.
830Sstevel@tonic-gate  * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1)
840Sstevel@tonic-gate  * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT)
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate #define	MAX_SECT	(63)
870Sstevel@tonic-gate #define	MAX_CYL		(1022)
880Sstevel@tonic-gate #define	MAX_HEAD	(254)
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* for clear_vtoc() */
910Sstevel@tonic-gate #define	OLD		0
920Sstevel@tonic-gate #define	NEW		1
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
950Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
960Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
970Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
980Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * Support for fdisk(1M) on the SPARC platform
1020Sstevel@tonic-gate  *	In order to convert little endian values to big endian for SPARC,
1030Sstevel@tonic-gate  *	byte/short and long values must be swapped.
1040Sstevel@tonic-gate  *	These swapping macros will be used to access information in the
1050Sstevel@tonic-gate  *	mboot and ipart structures.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate #ifdef sparc
1090Sstevel@tonic-gate #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
1100Sstevel@tonic-gate #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
1110Sstevel@tonic-gate 			    (les((unsigned)((val)&0xffff0000)>>16)))
1120Sstevel@tonic-gate #else
1130Sstevel@tonic-gate #define	les(val)	(val)
1140Sstevel@tonic-gate #define	lel(val)	(val)
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
118*6549Sbharding #define	VTOC_OFFSET	1
1190Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1200Sstevel@tonic-gate #define	VTOC_OFFSET	0
1210Sstevel@tonic-gate #else
1220Sstevel@tonic-gate #error No VTOC format defined.
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate 
125251Slclee static char Usage[] = "Usage: fdisk\n"
1260Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1270Sstevel@tonic-gate "[ -b masterboot ]\n"
1280Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1290Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1300Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1310Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice";
1320Sstevel@tonic-gate 
133251Slclee static char Usage1[] = "    Partition options:\n"
1340Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1350Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1360Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1370Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1380Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1390Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1400Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1410Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1420Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1430Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1440Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1450Sstevel@tonic-gate "			  start of partition\n"
1460Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1470Sstevel@tonic-gate "	-b master_boot\n"
1480Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1490Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1500Sstevel@tonic-gate "	-E	Create one EFI partition that uses the entire disk.\n"
1510Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1520Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1530Sstevel@tonic-gate "	-F fdisk_file\n"
1540Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1550Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1560Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1570Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1580Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1590Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1600Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1610Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1620Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1630Sstevel@tonic-gate "	-W fdisk_file\n"
1640Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1650Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1660Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
1670Sstevel@tonic-gate "    Diagnostic options:\n"
1680Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
1690Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
1700Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
1710Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
1720Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
1730Sstevel@tonic-gate "		BCYL		cylinder offset\n"
1740Sstevel@tonic-gate "		NHEADS		number of heads\n"
1750Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
1760Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
1770Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
1780Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
1790Sstevel@tonic-gate "	-o offset\n"
1800Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
1810Sstevel@tonic-gate "		-P # specified.\n"
1820Sstevel@tonic-gate "	-P fill_patt\n"
1830Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
1840Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
1850Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
1860Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
1870Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
1880Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
1890Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
1900Sstevel@tonic-gate "	-S geom_file\n"
1910Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
1920Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
1930Sstevel@tonic-gate 
194251Slclee static char Ostr[] = "Other OS";
195251Slclee static char Dstr[] = "DOS12";
196251Slclee static char D16str[] = "DOS16";
197251Slclee static char DDstr[] = "DOS-DATA";
198251Slclee static char EDstr[] = "EXT-DOS";
199251Slclee static char DBstr[] = "DOS-BIG";
200251Slclee static char PCstr[] = "PCIX";
201251Slclee static char Ustr[] = "UNIX System";
202251Slclee static char SUstr[] = "Solaris";
203251Slclee static char SU2str[] = "Solaris2";
204251Slclee static char X86str[] = "x86 Boot";
205251Slclee static char DIAGstr[] = "Diagnostic";
206251Slclee static char IFSstr[] = "IFS: NTFS";
207251Slclee static char AIXstr[] = "AIX Boot";
208251Slclee static char AIXDstr[] = "AIX Data";
209251Slclee static char OS2str[] = "OS/2 Boot";
210251Slclee static char WINstr[] = "Win95 FAT32";
211251Slclee static char EWINstr[] = "Ext Win95";
212251Slclee static char FAT95str[] = "FAT16 LBA";
213251Slclee static char EXTLstr[] = "EXT LBA";
214251Slclee static char LINUXstr[] = "Linux";
215251Slclee static char CPMstr[] = "CP/M";
216251Slclee static char NOVstr[] = "Netware 3.x+";
217251Slclee static char QNXstr[] = "QNX 4.x";
218251Slclee static char QNX2str[] = "QNX part 2";
219251Slclee static char QNX3str[] = "QNX part 3";
220251Slclee static char LINNATstr[] = "Linux native";
221251Slclee static char NTFSVOL1str[] = "NT volset 1";
222251Slclee static char NTFSVOL2str[] = "NT volset 2";
223251Slclee static char BSDstr[] = "BSD OS";
224251Slclee static char NEXTSTEPstr[] = "NeXTSTEP";
225251Slclee static char BSDIFSstr[] = "BSDI FS";
226251Slclee static char BSDISWAPstr[] = "BSDI swap";
227251Slclee static char Actvstr[] = "Active";
228251Slclee static char EFIstr[] = "EFI";
229251Slclee static char NAstr[] = "      ";
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /* All the user options and flags */
232251Slclee static char *Dfltdev;			/* name of fixed disk drive */
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate /* Diagnostic options */
235251Slclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
236251Slclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
237251Slclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
238251Slclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
239251Slclee static int	io_lgeom = 0;		/* get label geometry (-g) */
240251Slclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
241251Slclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
242251Slclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2450Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
246*6549Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
247*6549Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /* Partition table flags */
250251Slclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
251251Slclee static int 	stdo_flag = 0;		/* stdout flag (-W -) */
252251Slclee static int	io_fdisk = 0;		/* do fdisk operation */
253251Slclee static int	io_ifdisk = 0;		/* interactive partition */
254251Slclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
255251Slclee 
256251Slclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
257251Slclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
258251Slclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
259251Slclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
260251Slclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
261251Slclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
262251Slclee 
263251Slclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
264251Slclee 
265251Slclee static struct mboot BootCod;		/* buffer for master boot record */
266251Slclee 
267251Slclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
268251Slclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
269251Slclee static int	io_debug = 0;		/* activate verbose mode (-d) */
270251Slclee static int	io_image = 0;		/* create image using geometry (-I) */
271251Slclee 
272251Slclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
273251Slclee static char	*Bootsect;		/* pointer to sector zero buffer */
274251Slclee static char	*Nullsect;
275251Slclee static struct vtoc	disk_vtoc;	/* verify VTOC table */
276251Slclee static int	vt_inval = 0;
277251Slclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
278251Slclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
279251Slclee 
280251Slclee static struct ipart	Table[FD_NUMPART];
281251Slclee static struct ipart	Old_Table[FD_NUMPART];
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /* Disk geometry information */
2845169Slclee static struct dk_minfo	minfo;
285251Slclee static struct dk_geom	disk_geom;
286251Slclee 
2875169Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
2885169Slclee static diskaddr_t	chs_capacity;	/* Numcyl * heads * sectors */
2895169Slclee 
290251Slclee static int Dev;			/* fd for open device */
2910Sstevel@tonic-gate /* Physical geometry for the drive */
292251Slclee static int	Numcyl;			/* number of cylinders */
293251Slclee static int	heads;			/* number of heads */
294251Slclee static int	sectors;		/* number of sectors per track */
295251Slclee static int	acyl;			/* number of alternate sectors */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
298251Slclee static int	hba_Numcyl;		/* number of cylinders */
299251Slclee static int	hba_heads;		/* number of heads */
300251Slclee static int	hba_sectors;		/* number of sectors per track */
301251Slclee 
302251Slclee static int	sectsiz;		/* sector size */
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate /* Load functions for fdisk table modification */
3050Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3060Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3070Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate #define	CBUFLEN 80
310251Slclee static char s[CBUFLEN];
311251Slclee 
312251Slclee static void update_disk_and_exit(boolean_t table_changed);
313251Slclee int main(int argc, char *argv[]);
314251Slclee static int read_geom(char *sgeom);
315251Slclee static void dev_mboot_read(void);
316*6549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz);
317251Slclee static void mboot_read(void);
318251Slclee static void fill_patt(void);
319251Slclee static void abs_read(void);
320251Slclee static void abs_write(void);
321251Slclee static void load(int funct, char *file);
322251Slclee static void Set_Table_CHS_Values(int ti);
323251Slclee static int insert_tbl(int id, int act,
324251Slclee     int bhead, int bsect, int bcyl,
325251Slclee     int ehead, int esect, int ecyl,
326251Slclee     int rsect, int numsect);
327251Slclee static int verify_tbl(void);
328251Slclee static int pars_fdisk(char *line,
329251Slclee     int *id, int *act,
330251Slclee     int *bhead, int *bsect, int *bcyl,
331251Slclee     int *ehead, int *esect, int *ecyl,
332251Slclee     int *rsect, int *numsect);
333251Slclee static int validate_part(int id, int rsect, int numsect);
334251Slclee static void stage0(void);
335251Slclee static int pcreate(void);
336251Slclee static int specify(uchar_t tsystid);
337251Slclee static void dispmenu(void);
338251Slclee static int pchange(void);
339251Slclee static int ppartid(void);
340251Slclee static char pdelete(void);
341251Slclee static void rm_blanks(char *s);
342251Slclee static int getcyl(void);
343251Slclee static void disptbl(void);
344251Slclee static void print_Table(void);
345251Slclee static void copy_Table_to_Old_Table(void);
346251Slclee static void nulltbl(void);
347251Slclee static void copy_Bootblk_to_Table(void);
348251Slclee static void fill_ipart(char *bootptr, struct ipart *partp);
349251Slclee #ifdef sparc
350251Slclee uchar_t getbyte(char **bp);
351251Slclee uint32_t getlong(char **bp);
352251Slclee #endif
353251Slclee static void copy_Table_to_Bootblk(void);
354251Slclee static int TableChanged(void);
355251Slclee static void ffile_write(char *file);
356251Slclee static void fix_slice(void);
357251Slclee static int yesno(void);
358251Slclee static int readvtoc(void);
359251Slclee static int writevtoc(void);
360251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
361251Slclee static int clear_efi(void);
362251Slclee static void clear_vtoc(int table, int part);
363251Slclee static int lecture_and_query(char *warning, char *devname);
364251Slclee static void sanity_check_provided_device(char *devname, int fd);
365251Slclee static char *get_node(char *devname);
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate static void
3680Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	if (table_changed) {
3710Sstevel@tonic-gate 		/*
3720Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
3730Sstevel@tonic-gate 		 * and write it to disk
3740Sstevel@tonic-gate 		 */
3750Sstevel@tonic-gate 		copy_Table_to_Bootblk();
3760Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
3770Sstevel@tonic-gate 	}
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
3800Sstevel@tonic-gate 	if (io_adjt)
3810Sstevel@tonic-gate 		fix_slice();
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	exit(0);
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate /*
3890Sstevel@tonic-gate  * main
3900Sstevel@tonic-gate  * Process command-line options.
3910Sstevel@tonic-gate  */
392251Slclee int
3930Sstevel@tonic-gate main(int argc, char *argv[])
3940Sstevel@tonic-gate {
395251Slclee 	int c, i;
3960Sstevel@tonic-gate 	extern	int optind;
3970Sstevel@tonic-gate 	extern	char *optarg;
3980Sstevel@tonic-gate 	int	errflg = 0;
3990Sstevel@tonic-gate 	int	diag_cnt = 0;
4000Sstevel@tonic-gate 	int openmode;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	setbuf(stderr, 0);	/* so all output gets out on exit */
4030Sstevel@tonic-gate 	setbuf(stdout, 0);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	/* Process the options. */
4060Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE"))
4070Sstevel@tonic-gate 	    != EOF) {
4080Sstevel@tonic-gate 		switch (c) {
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			case 'o':
411*6549Sbharding 				io_offset = (off_t)strtoull(optarg, 0, 0);
4120Sstevel@tonic-gate 				continue;
4130Sstevel@tonic-gate 			case 's':
414*6549Sbharding 				io_size = (off_t)strtoull(optarg, 0, 0);
4150Sstevel@tonic-gate 				continue;
4160Sstevel@tonic-gate 			case 'P':
4170Sstevel@tonic-gate 				diag_cnt++;
4180Sstevel@tonic-gate 				io_patt++;
4190Sstevel@tonic-gate 				io_fatt = optarg;
4200Sstevel@tonic-gate 				continue;
4210Sstevel@tonic-gate 			case 'w':
4220Sstevel@tonic-gate 				diag_cnt++;
4230Sstevel@tonic-gate 				io_wrt++;
4240Sstevel@tonic-gate 				continue;
4250Sstevel@tonic-gate 			case 'r':
4260Sstevel@tonic-gate 				diag_cnt++;
4270Sstevel@tonic-gate 				io_rd++;
4280Sstevel@tonic-gate 				continue;
4290Sstevel@tonic-gate 			case 'd':
4300Sstevel@tonic-gate 				io_debug++;
4310Sstevel@tonic-gate 				continue;
4320Sstevel@tonic-gate 			case 'I':
4330Sstevel@tonic-gate 				io_image++;
4340Sstevel@tonic-gate 				continue;
4350Sstevel@tonic-gate 			case 'R':
4360Sstevel@tonic-gate 				io_readonly++;
4370Sstevel@tonic-gate 				continue;
4380Sstevel@tonic-gate 			case 'S':
4390Sstevel@tonic-gate 				diag_cnt++;
4400Sstevel@tonic-gate 				io_sgeom = optarg;
4410Sstevel@tonic-gate 				continue;
4420Sstevel@tonic-gate 			case 'T':
4430Sstevel@tonic-gate 				io_ADJT++;
444251Slclee 				/* FALLTHRU */
4450Sstevel@tonic-gate 			case 't':
4460Sstevel@tonic-gate 				io_adjt++;
4470Sstevel@tonic-gate 				continue;
4480Sstevel@tonic-gate 			case 'B':
4490Sstevel@tonic-gate 				io_wholedisk++;
4500Sstevel@tonic-gate 				io_fdisk++;
4510Sstevel@tonic-gate 				continue;
4520Sstevel@tonic-gate 			case 'E':
4530Sstevel@tonic-gate 				io_EFIdisk++;
4540Sstevel@tonic-gate 				io_fdisk++;
4550Sstevel@tonic-gate 				continue;
4560Sstevel@tonic-gate 			case 'g':
4570Sstevel@tonic-gate 				diag_cnt++;
4580Sstevel@tonic-gate 				io_lgeom++;
4590Sstevel@tonic-gate 				continue;
4600Sstevel@tonic-gate 			case 'G':
4610Sstevel@tonic-gate 				diag_cnt++;
4620Sstevel@tonic-gate 				io_pgeom++;
4630Sstevel@tonic-gate 				continue;
4640Sstevel@tonic-gate 			case 'n':
4650Sstevel@tonic-gate 				io_nifdisk++;
4660Sstevel@tonic-gate 				io_fdisk++;
4670Sstevel@tonic-gate 				continue;
4680Sstevel@tonic-gate 			case 'F':
4690Sstevel@tonic-gate 				io_fdisk++;
4700Sstevel@tonic-gate 				io_ffdisk = optarg;
4710Sstevel@tonic-gate 				continue;
4720Sstevel@tonic-gate 			case 'b':
4730Sstevel@tonic-gate 				io_mboot = optarg;
4740Sstevel@tonic-gate 				continue;
4750Sstevel@tonic-gate 			case 'W':
4760Sstevel@tonic-gate 				/*
4770Sstevel@tonic-gate 				 * If '-' is the -W argument, then write
4780Sstevel@tonic-gate 				 * to standard output, otherwise write
4790Sstevel@tonic-gate 				 * to the specified file.
4800Sstevel@tonic-gate 				 */
4810Sstevel@tonic-gate 				if (strncmp(optarg, "-", 1) == 0)
4820Sstevel@tonic-gate 					stdo_flag = 1;
4830Sstevel@tonic-gate 				else
4840Sstevel@tonic-gate 					io_Wfdisk = optarg;
4850Sstevel@tonic-gate 				io_fdisk++;
4860Sstevel@tonic-gate 				continue;
4870Sstevel@tonic-gate 			case 'A':
4880Sstevel@tonic-gate 				io_fdisk++;
4890Sstevel@tonic-gate 				io_Afdisk = optarg;
4900Sstevel@tonic-gate 				continue;
4910Sstevel@tonic-gate 			case 'D':
4920Sstevel@tonic-gate 				io_fdisk++;
4930Sstevel@tonic-gate 				io_Dfdisk = optarg;
4940Sstevel@tonic-gate 				continue;
4950Sstevel@tonic-gate 			case 'h':
496251Slclee 				(void) fprintf(stderr, "%s\n", Usage);
497251Slclee 				(void) fprintf(stderr, "%s\n", Usage1);
4980Sstevel@tonic-gate 				exit(0);
499251Slclee 				/* FALLTHRU */
5000Sstevel@tonic-gate 			case 'v':
5010Sstevel@tonic-gate 				v_flag = 1;
5020Sstevel@tonic-gate 				continue;
5030Sstevel@tonic-gate 			case '?':
5040Sstevel@tonic-gate 				errflg++;
5050Sstevel@tonic-gate 				break;
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		break;
5080Sstevel@tonic-gate 	}
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (io_image && io_sgeom && diag_cnt == 1) {
5110Sstevel@tonic-gate 		diag_cnt = 0;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	/* User option checking */
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	/* By default, run in interactive mode */
5170Sstevel@tonic-gate 	if (!io_fdisk && !diag_cnt && !io_nifdisk) {
5180Sstevel@tonic-gate 		io_ifdisk++;
5190Sstevel@tonic-gate 		io_fdisk++;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 	if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) {
5220Sstevel@tonic-gate 		errflg++;
5230Sstevel@tonic-gate 	}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	/* Was any error detected? */
5260Sstevel@tonic-gate 	if (errflg || argc == optind) {
527251Slclee 		(void) fprintf(stderr, "%s\n", Usage);
528251Slclee 		(void) fprintf(stderr,
5290Sstevel@tonic-gate 		    "\nDetailed help is available with the -h option.\n");
5300Sstevel@tonic-gate 		exit(2);
5310Sstevel@tonic-gate 	}
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	/* Figure out the correct device node to open */
5350Sstevel@tonic-gate 	Dfltdev = get_node(argv[optind]);
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (io_readonly)
5380Sstevel@tonic-gate 		openmode = O_RDONLY;
5390Sstevel@tonic-gate 	else
5400Sstevel@tonic-gate 		openmode = O_RDWR|O_CREAT;
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	if ((Dev = open(Dfltdev, openmode, 0666)) == -1) {
543251Slclee 		(void) fprintf(stderr,
544251Slclee 		    "fdisk: Cannot open device %s.\n",
545251Slclee 		    Dfltdev);
5460Sstevel@tonic-gate 		exit(1);
5470Sstevel@tonic-gate 	}
5485169Slclee 	/*
5495169Slclee 	 * not all disk (or disklike) drivers support DKIOCGMEDIAINFO
5505169Slclee 	 * in that case leave the minfo structure zeroed
5515169Slclee 	 */
5525169Slclee 	if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) {
5535169Slclee 		memset(&minfo, 0, sizeof (minfo));
5545169Slclee 	}
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 	/* Get the disk geometry */
5570Sstevel@tonic-gate 	if (!io_image) {
5580Sstevel@tonic-gate 		/* Get disk's HBA (virtual) geometry */
5590Sstevel@tonic-gate 		errno = 0;
5600Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) {
5610Sstevel@tonic-gate 
5620Sstevel@tonic-gate 			/*
5630Sstevel@tonic-gate 			 * If ioctl isn't implemented on this platform, then
5640Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v),
5650Sstevel@tonic-gate 			 * otherwise use the virtual geometry.
5660Sstevel@tonic-gate 			 */
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 			if (errno == ENOTTY) {
5690Sstevel@tonic-gate 				v_flag = 0;
5700Sstevel@tonic-gate 				no_virtgeom_ioctl = 1;
5710Sstevel@tonic-gate 			} else if (errno == EINVAL) {
5720Sstevel@tonic-gate 				/*
5730Sstevel@tonic-gate 				 * This means that the ioctl exists, but
5740Sstevel@tonic-gate 				 * is invalid for this disk, meaning the
5750Sstevel@tonic-gate 				 * disk doesn't have an HBA geometry
5760Sstevel@tonic-gate 				 * (like, say, it's larger than 8GB).
5770Sstevel@tonic-gate 				 */
5780Sstevel@tonic-gate 				v_flag = 0;
5790Sstevel@tonic-gate 				hba_Numcyl = hba_heads = hba_sectors = 0;
5800Sstevel@tonic-gate 			} else {
5810Sstevel@tonic-gate 				(void) fprintf(stderr,
5820Sstevel@tonic-gate 				    "%s: Cannot get virtual disk geometry.\n",
5830Sstevel@tonic-gate 				    argv[optind]);
5840Sstevel@tonic-gate 				exit(1);
5850Sstevel@tonic-gate 			}
5860Sstevel@tonic-gate 		} else {
5870Sstevel@tonic-gate 			/* save virtual geometry values obtained by ioctl */
5880Sstevel@tonic-gate 			hba_Numcyl = disk_geom.dkg_ncyl;
5890Sstevel@tonic-gate 			hba_heads = disk_geom.dkg_nhead;
5900Sstevel@tonic-gate 			hba_sectors = disk_geom.dkg_nsect;
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 		errno = 0;
5940Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
5950Sstevel@tonic-gate 			if (errno == ENOTTY) {
5960Sstevel@tonic-gate 				no_physgeom_ioctl = 1;
5970Sstevel@tonic-gate 			} else {
5980Sstevel@tonic-gate 				(void) fprintf(stderr,
5990Sstevel@tonic-gate 				    "%s: Cannot get physical disk geometry.\n",
6000Sstevel@tonic-gate 				    argv[optind]);
6010Sstevel@tonic-gate 				exit(1);
6020Sstevel@tonic-gate 			}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		/*
6060Sstevel@tonic-gate 		 * Call DKIOCGGEOM if the ioctls for physical and virtual
6070Sstevel@tonic-gate 		 * geometry fail. Get both from this generic call.
6080Sstevel@tonic-gate 		 */
6090Sstevel@tonic-gate 		if (no_virtgeom_ioctl && no_physgeom_ioctl) {
6100Sstevel@tonic-gate 			errno = 0;
6110Sstevel@tonic-gate 			if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6120Sstevel@tonic-gate 				(void) fprintf(stderr,
6130Sstevel@tonic-gate 				    "%s: Cannot get disk label geometry.\n",
6140Sstevel@tonic-gate 				    argv[optind]);
6150Sstevel@tonic-gate 				exit(1);
6160Sstevel@tonic-gate 			}
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6200Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6210Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6220Sstevel@tonic-gate 		sectsiz = 512;
6230Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * if hba geometry was not set by DKIOC_VIRTGEOM
6270Sstevel@tonic-gate 		 * or we got an invalid hba geometry
6280Sstevel@tonic-gate 		 * then set hba geometry based on max values
6290Sstevel@tonic-gate 		 */
6300Sstevel@tonic-gate 		if (no_virtgeom_ioctl ||
631251Slclee 		    disk_geom.dkg_ncyl == 0 ||
632251Slclee 		    disk_geom.dkg_nhead == 0 ||
633251Slclee 		    disk_geom.dkg_nsect == 0 ||
6340Sstevel@tonic-gate 		    disk_geom.dkg_ncyl > MAX_CYL ||
6350Sstevel@tonic-gate 		    disk_geom.dkg_nhead > MAX_HEAD ||
6360Sstevel@tonic-gate 		    disk_geom.dkg_nsect > MAX_SECT) {
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 			/*
6390Sstevel@tonic-gate 			 * turn off flag to print out virtual geometry (-v)
6400Sstevel@tonic-gate 			 */
6410Sstevel@tonic-gate 			v_flag = 0;
6420Sstevel@tonic-gate 			hba_sectors	= MAX_SECT;
6430Sstevel@tonic-gate 			hba_heads	= MAX_HEAD + 1;
6440Sstevel@tonic-gate 			hba_Numcyl	= (Numcyl * heads * sectors) /
6450Sstevel@tonic-gate 			    (hba_sectors * hba_heads);
6460Sstevel@tonic-gate 		}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 		if (io_debug) {
649251Slclee 			(void) fprintf(stderr, "Physical Geometry:\n");
650251Slclee 			(void) fprintf(stderr,
6510Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6520Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6530Sstevel@tonic-gate 			    Numcyl,
6540Sstevel@tonic-gate 			    heads,
6550Sstevel@tonic-gate 			    sectors,
6560Sstevel@tonic-gate 			    sectsiz,
657251Slclee 			    Numcyl * heads * sectors,
658251Slclee 			    (Numcyl * heads * sectors * sectsiz) / 1048576);
659251Slclee 			(void) fprintf(stderr, "Virtual (HBA) Geometry:\n");
660251Slclee 			(void) fprintf(stderr,
6610Sstevel@tonic-gate 			    "  cylinders[%d] heads[%d] sectors[%d]\n"
6620Sstevel@tonic-gate 			    "  sector size[%d] blocks[%d] mbytes[%d]\n",
6630Sstevel@tonic-gate 			    hba_Numcyl,
6640Sstevel@tonic-gate 			    hba_heads,
6650Sstevel@tonic-gate 			    hba_sectors,
6660Sstevel@tonic-gate 			    sectsiz,
667251Slclee 			    hba_Numcyl * hba_heads * hba_sectors,
668251Slclee 			    (hba_Numcyl * hba_heads * hba_sectors * sectsiz) /
669251Slclee 			    1048576);
6700Sstevel@tonic-gate 		}
6710Sstevel@tonic-gate 	}
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate 	/* If user has requested a geometry report just do it and exit */
6740Sstevel@tonic-gate 	if (io_lgeom) {
6750Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) {
6760Sstevel@tonic-gate 			(void) fprintf(stderr,
6770Sstevel@tonic-gate 			    "%s: Cannot get disk label geometry.\n",
6780Sstevel@tonic-gate 			    argv[optind]);
6790Sstevel@tonic-gate 			exit(1);
6800Sstevel@tonic-gate 		}
6810Sstevel@tonic-gate 		Numcyl = disk_geom.dkg_ncyl;
6820Sstevel@tonic-gate 		heads = disk_geom.dkg_nhead;
6830Sstevel@tonic-gate 		sectors = disk_geom.dkg_nsect;
6840Sstevel@tonic-gate 		sectsiz = 512;
6850Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
686251Slclee 		(void) printf("* Label geometry for device %s\n", Dfltdev);
687251Slclee 		(void) printf(
688251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
6890Sstevel@tonic-gate 		    " SECSIZ\n");
690251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
6910Sstevel@tonic-gate 		    Numcyl,
6920Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
6930Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
6940Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
6950Sstevel@tonic-gate 		    heads,
6960Sstevel@tonic-gate 		    sectors,
6970Sstevel@tonic-gate 		    sectsiz);
6980Sstevel@tonic-gate 		exit(0);
6990Sstevel@tonic-gate 	} else if (io_pgeom) {
7000Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) {
7010Sstevel@tonic-gate 			(void) fprintf(stderr,
7020Sstevel@tonic-gate 			    "%s: Cannot get physical disk geometry.\n",
7030Sstevel@tonic-gate 			    argv[optind]);
7040Sstevel@tonic-gate 			exit(1);
7050Sstevel@tonic-gate 		}
706251Slclee 		(void) printf("* Physical geometry for device %s\n", Dfltdev);
707251Slclee 		(void) printf(
708251Slclee 		    "* PCYL     NCYL     ACYL     BCYL     NHEAD NSECT"
7090Sstevel@tonic-gate 		    " SECSIZ\n");
710251Slclee 		(void) printf("  %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n",
7110Sstevel@tonic-gate 		    disk_geom.dkg_pcyl,
7120Sstevel@tonic-gate 		    disk_geom.dkg_ncyl,
7130Sstevel@tonic-gate 		    disk_geom.dkg_acyl,
7140Sstevel@tonic-gate 		    disk_geom.dkg_bcyl,
7150Sstevel@tonic-gate 		    disk_geom.dkg_nhead,
7160Sstevel@tonic-gate 		    disk_geom.dkg_nsect,
7170Sstevel@tonic-gate 		    sectsiz);
7180Sstevel@tonic-gate 		exit(0);
7190Sstevel@tonic-gate 	} else if (io_sgeom) {
7200Sstevel@tonic-gate 		if (read_geom(io_sgeom)) {
7210Sstevel@tonic-gate 			exit(1);
7220Sstevel@tonic-gate 		} else if (!io_image) {
7230Sstevel@tonic-gate 			exit(0);
7240Sstevel@tonic-gate 		}
7250Sstevel@tonic-gate 	}
7260Sstevel@tonic-gate 
7275169Slclee 	/*
7285169Slclee 	 * some drivers may not support DKIOCGMEDIAINFO
7295169Slclee 	 * in that case use CHS
7305169Slclee 	 */
7315169Slclee 	chs_capacity = Numcyl * heads * sectors;
7325169Slclee 	dev_capacity = chs_capacity;
7335169Slclee 	if (minfo.dki_capacity > 0)
7345169Slclee 		dev_capacity = minfo.dki_capacity;
7355169Slclee 
7360Sstevel@tonic-gate 	/* Allocate memory to hold three complete sectors */
7370Sstevel@tonic-gate 	Bootsect = (char *)malloc(3 * sectsiz);
7380Sstevel@tonic-gate 	if (Bootsect == NULL) {
739251Slclee 		(void) fprintf(stderr,
7400Sstevel@tonic-gate 		    "fdisk: Unable to obtain enough buffer memory"
7410Sstevel@tonic-gate 		    " (%d bytes).\n",
742251Slclee 		    3 * sectsiz);
7430Sstevel@tonic-gate 		exit(1);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	Nullsect = Bootsect + sectsiz;
7470Sstevel@tonic-gate 	/* Zero out the "NULL" sector */
7480Sstevel@tonic-gate 	for (i = 0; i < sectsiz; i++) {
7490Sstevel@tonic-gate 		Nullsect[i] = 0;
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	/* Find out what the user wants done */
7530Sstevel@tonic-gate 	if (io_rd) {		/* abs disk read */
7540Sstevel@tonic-gate 		abs_read();	/* will not return */
7550Sstevel@tonic-gate 	} else if (io_wrt && !io_readonly) {
7560Sstevel@tonic-gate 		abs_write();	/* will not return */
7570Sstevel@tonic-gate 	} else if (io_patt && !io_readonly) {
7580Sstevel@tonic-gate 		fill_patt();	/* will not return */
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate 
7620Sstevel@tonic-gate 	/* This is the fdisk edit, the real reason for the program.	*/
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	sanity_check_provided_device(Dfltdev, Dev);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/* Get the new BOOT program in case we write a new fdisk table */
7670Sstevel@tonic-gate 	mboot_read();
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 	/* Read from disk master boot */
7700Sstevel@tonic-gate 	dev_mboot_read();
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/*
7730Sstevel@tonic-gate 	 * Verify and copy the device's fdisk table. This will be used
7740Sstevel@tonic-gate 	 * as the prototype mboot if the device's mboot looks invalid.
7750Sstevel@tonic-gate 	 */
7760Sstevel@tonic-gate 	Bootblk = (struct mboot *)Bootsect;
7770Sstevel@tonic-gate 	copy_Bootblk_to_Table();
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate 	/* save away a copy of Table in Old_Table for sensing changes */
7800Sstevel@tonic-gate 	copy_Table_to_Old_Table();
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	/* Load fdisk table from specified file (-F fdisk_file) */
7830Sstevel@tonic-gate 	if (io_ffdisk) {
7840Sstevel@tonic-gate 		/* Load and verify user-specified table parameters */
7850Sstevel@tonic-gate 		load(LOADFILE, io_ffdisk);
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/* Does user want to delete or add an entry? */
7890Sstevel@tonic-gate 	if (io_Dfdisk) {
7900Sstevel@tonic-gate 		load(LOADDEL, io_Dfdisk);
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 	if (io_Afdisk) {
7930Sstevel@tonic-gate 		load(LOADADD, io_Afdisk);
7940Sstevel@tonic-gate 	}
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) {
7970Sstevel@tonic-gate 		/* Check if there is no fdisk table */
7980Sstevel@tonic-gate 		if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) {
7990Sstevel@tonic-gate 			if (io_ifdisk && !io_wholedisk && !io_EFIdisk) {
800251Slclee 				(void) printf(
801251Slclee 				    "No fdisk table exists. The default"
802251Slclee 				    " partition for the disk is:\n\n"
803251Slclee 				    "  a 100%% \"SOLARIS System\" "
804251Slclee 				    "partition\n\n"
805251Slclee 				    "Type \"y\" to accept the default "
8060Sstevel@tonic-gate 				    "partition,  otherwise type \"n\" to "
8070Sstevel@tonic-gate 				    "edit the\n partition table.\n");
8080Sstevel@tonic-gate 			}
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 			/* Edit the partition table as directed */
8110Sstevel@tonic-gate 			if (io_wholedisk ||(io_ifdisk && yesno())) {
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate 				/* Default scenario */
8140Sstevel@tonic-gate 				nulltbl();
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 				/* now set up UNIX System partition */
8170Sstevel@tonic-gate 				Table[0].bootid = ACTIVE;
8180Sstevel@tonic-gate 				Table[0].relsect = lel(heads * sectors);
819251Slclee 				Table[0].numsect = lel((long)((Numcyl - 1) *
8200Sstevel@tonic-gate 				    heads * sectors));
8210Sstevel@tonic-gate 				Table[0].systid = SUNIXOS2;   /* Solaris */
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 				/* calculate CHS values for table entry 0 */
8240Sstevel@tonic-gate 				Set_Table_CHS_Values(0);
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8270Sstevel@tonic-gate 			} else if (io_EFIdisk) {
8280Sstevel@tonic-gate 				/* create an EFI partition for the whole disk */
8290Sstevel@tonic-gate 				nulltbl();
8300Sstevel@tonic-gate 				i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1,
8315169Slclee 				    dev_capacity - 1);
8320Sstevel@tonic-gate 				if (i != 0) {
833251Slclee 					(void) fprintf(stderr,
834251Slclee 					    "Error creating EFI partition\n");
8350Sstevel@tonic-gate 					exit(1);
8360Sstevel@tonic-gate 				}
8370Sstevel@tonic-gate 				update_disk_and_exit(B_TRUE);
8380Sstevel@tonic-gate 			}
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 	/* Display complete fdisk table entries for debugging purposes */
8430Sstevel@tonic-gate 	if (io_debug) {
844251Slclee 		(void) fprintf(stderr, "Partition Table Entry Values:\n");
8450Sstevel@tonic-gate 		print_Table();
8460Sstevel@tonic-gate 		if (io_ifdisk) {
847251Slclee 			(void) fprintf(stderr, "\n");
848251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
849251Slclee 			(void) gets(s);
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	/* Interactive fdisk mode */
8540Sstevel@tonic-gate 	if (io_ifdisk) {
855251Slclee 		(void) printf(CLR_SCR);
8560Sstevel@tonic-gate 		disptbl();
857251Slclee 		for (;;) {
858251Slclee 			stage0();
8590Sstevel@tonic-gate 			copy_Bootblk_to_Table();
8600Sstevel@tonic-gate 			disptbl();
8610Sstevel@tonic-gate 		}
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/* If user wants to write the table to a file, do it */
8650Sstevel@tonic-gate 	if (io_Wfdisk)
8660Sstevel@tonic-gate 		ffile_write(io_Wfdisk);
8670Sstevel@tonic-gate 	else if (stdo_flag)
8680Sstevel@tonic-gate 		ffile_write((char *)stdout);
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate 	update_disk_and_exit(TableChanged() == 1);
871251Slclee 	return (0);
8720Sstevel@tonic-gate }
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate /*
8750Sstevel@tonic-gate  * read_geom
8760Sstevel@tonic-gate  * Read geometry from specified file (-S).
8770Sstevel@tonic-gate  */
8780Sstevel@tonic-gate 
879251Slclee static int
880251Slclee read_geom(char *sgeom)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate 	char	line[256];
8830Sstevel@tonic-gate 	FILE *fp;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/* open the prototype file */
8860Sstevel@tonic-gate 	if ((fp = fopen(sgeom, "r")) == NULL) {
8870Sstevel@tonic-gate 		(void) fprintf(stderr, "fdisk: Cannot open file %s.\n",
8880Sstevel@tonic-gate 		    io_sgeom);
8890Sstevel@tonic-gate 		return (1);
8900Sstevel@tonic-gate 	}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 	/* Read a line from the file */
8930Sstevel@tonic-gate 	while (fgets(line, sizeof (line) - 1, fp)) {
8940Sstevel@tonic-gate 		if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
8950Sstevel@tonic-gate 			continue;
8960Sstevel@tonic-gate 		else {
8970Sstevel@tonic-gate 			line[strlen(line)] = '\0';
898251Slclee 			if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d",
8990Sstevel@tonic-gate 			    &disk_geom.dkg_pcyl,
9000Sstevel@tonic-gate 			    &disk_geom.dkg_ncyl,
9010Sstevel@tonic-gate 			    &disk_geom.dkg_acyl,
9020Sstevel@tonic-gate 			    &disk_geom.dkg_bcyl,
9030Sstevel@tonic-gate 			    &disk_geom.dkg_nhead,
9040Sstevel@tonic-gate 			    &disk_geom.dkg_nsect,
9050Sstevel@tonic-gate 			    &sectsiz) != 7) {
9060Sstevel@tonic-gate 				(void) fprintf(stderr,
9070Sstevel@tonic-gate 				    "Syntax error:\n	\"%s\".\n",
9080Sstevel@tonic-gate 				    line);
9090Sstevel@tonic-gate 				return (1);
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 			break;
9120Sstevel@tonic-gate 		} /* else */
9130Sstevel@tonic-gate 	} /* while (fgets(line, sizeof (line) - 1, fp)) */
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 	if (!io_image) {
9160Sstevel@tonic-gate 		if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) {
9170Sstevel@tonic-gate 			(void) fprintf(stderr,
9180Sstevel@tonic-gate 			    "fdisk: Cannot set label geometry.\n");
9190Sstevel@tonic-gate 			return (1);
9200Sstevel@tonic-gate 		}
9210Sstevel@tonic-gate 	} else {
9220Sstevel@tonic-gate 		Numcyl = hba_Numcyl = disk_geom.dkg_ncyl;
9230Sstevel@tonic-gate 		heads = hba_heads = disk_geom.dkg_nhead;
9240Sstevel@tonic-gate 		sectors = hba_sectors = disk_geom.dkg_nsect;
9250Sstevel@tonic-gate 		acyl = disk_geom.dkg_acyl;
9260Sstevel@tonic-gate 	}
9270Sstevel@tonic-gate 
928251Slclee 	(void) fclose(fp);
9290Sstevel@tonic-gate 	return (0);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate /*
9330Sstevel@tonic-gate  * dev_mboot_read
9340Sstevel@tonic-gate  * Read the master boot sector from the device.
9350Sstevel@tonic-gate  */
936251Slclee static void
937251Slclee dev_mboot_read(void)
9380Sstevel@tonic-gate {
9390Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) {
9400Sstevel@tonic-gate 		perror("Error in ioctl DKIOCGMBOOT");
9410Sstevel@tonic-gate 	}
9420Sstevel@tonic-gate 	if (errno == 0)
9430Sstevel@tonic-gate 		return;
9440Sstevel@tonic-gate 	if (lseek(Dev, 0, SEEK_SET) == -1) {
945251Slclee 		(void) fprintf(stderr,
9460Sstevel@tonic-gate 		    "fdisk: Error seeking to partition table on %s.\n",
9470Sstevel@tonic-gate 		    Dfltdev);
9480Sstevel@tonic-gate 		if (!io_image)
9490Sstevel@tonic-gate 			exit(1);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 	if (read(Dev, Bootsect, sectsiz) != sectsiz) {
952251Slclee 		(void) fprintf(stderr,
9530Sstevel@tonic-gate 		    "fdisk: Error reading partition table from %s.\n",
9540Sstevel@tonic-gate 		    Dfltdev);
9550Sstevel@tonic-gate 		if (!io_image)
9560Sstevel@tonic-gate 			exit(1);
9570Sstevel@tonic-gate 	}
9580Sstevel@tonic-gate }
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /*
9610Sstevel@tonic-gate  * dev_mboot_write
9620Sstevel@tonic-gate  * Write the master boot sector to the device.
9630Sstevel@tonic-gate  */
964251Slclee static void
965*6549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
9686478Sbharding 	int	clr_efi = -1;
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate 	if (io_readonly)
971251Slclee 		return;
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 	if (io_debug) {
974251Slclee 		(void) fprintf(stderr, "About to write fdisk table:\n");
9750Sstevel@tonic-gate 		print_Table();
9760Sstevel@tonic-gate 		if (io_ifdisk) {
977251Slclee 			(void) fprintf(stderr, "Press Enter to continue.\n");
978251Slclee 			(void) gets(s);
9790Sstevel@tonic-gate 		}
9800Sstevel@tonic-gate 	}
9810Sstevel@tonic-gate 
9826478Sbharding 	/*
9836478Sbharding 	 * If the new table has any Solaris partitions and the old
9846478Sbharding 	 * table does not have an entry that describes it
9856478Sbharding 	 * exactly then clear the old vtoc (if any).
9866478Sbharding 	 */
9870Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
9880Sstevel@tonic-gate 
9896478Sbharding 		/* We only care about potential Solaris parts. */
9900Sstevel@tonic-gate 		if (Table[new_pt].systid != SUNIXOS &&
9910Sstevel@tonic-gate 		    Table[new_pt].systid != SUNIXOS2)
9920Sstevel@tonic-gate 			continue;
9930Sstevel@tonic-gate 
9946478Sbharding 		/* Does the old table have an exact entry for the new entry? */
9956478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
9966478Sbharding 
9976478Sbharding 			/* We only care about old Solaris partitions. */
9986478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
9996478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
10006478Sbharding 
10016478Sbharding 				/* Is this old one the same as a new one? */
10026478Sbharding 				if ((Old_Table[old_pt].relsect ==
10036478Sbharding 				    Table[new_pt].relsect) &&
10046478Sbharding 				    (Old_Table[old_pt].numsect ==
10056478Sbharding 				    Table[new_pt].numsect))
10066478Sbharding 					break; /* Yes */
10076478Sbharding 			}
10086478Sbharding 		}
10096478Sbharding 
10106478Sbharding 		/* Did a solaris partition change location or size? */
10116478Sbharding 		if (old_pt >= FD_NUMPART) {
10126478Sbharding 			/* Yes clear old vtoc */
10136478Sbharding 			if (io_debug) {
10146478Sbharding 				(void) fprintf(stderr,
10156478Sbharding 				    "Clearing VTOC labels from NEW"
10166478Sbharding 				    " table\n");
10176478Sbharding 			}
10186478Sbharding 			clear_vtoc(NEW, new_pt);
10196478Sbharding 		}
10206478Sbharding 	}
10216478Sbharding 
10226478Sbharding 
10236478Sbharding 	/* see if the old table had EFI */
10246478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10256478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
10266478Sbharding 			clr_efi = old_pt;
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 	}
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 	/* look to see if a EFI partition changed in relsect/numsect */
10310Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
10320Sstevel@tonic-gate 		if (Table[new_pt].systid != EFI_PMBR)
10330Sstevel@tonic-gate 			continue;
10340Sstevel@tonic-gate 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
10355169Slclee 			if ((Old_Table[old_pt].systid ==
10365169Slclee 			    Table[new_pt].systid) &&
10375169Slclee 			    (Old_Table[old_pt].relsect ==
10385169Slclee 			    Table[new_pt].relsect) &&
10395169Slclee 			    (Old_Table[old_pt].numsect ==
10405169Slclee 			    Table[new_pt].numsect))
10415169Slclee 				break;
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 		/*
10450Sstevel@tonic-gate 		 * if EFI partition changed, set the flag to clear
10460Sstevel@tonic-gate 		 * the EFI GPT
10470Sstevel@tonic-gate 		 */
10480Sstevel@tonic-gate 		if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) {
10490Sstevel@tonic-gate 			clr_efi = 0;
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 		break;
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	/* clear labels if necessary */
10550Sstevel@tonic-gate 	if (clr_efi >= 0) {
10560Sstevel@tonic-gate 		if (io_debug) {
1057251Slclee 			(void) fprintf(stderr, "Clearing EFI labels\n");
10580Sstevel@tonic-gate 		}
10590Sstevel@tonic-gate 		if ((error = clear_efi()) != 0) {
10600Sstevel@tonic-gate 			if (io_debug) {
1061251Slclee 				(void) fprintf(stderr,
1062251Slclee 				    "\tError %d clearing EFI labels"
10630Sstevel@tonic-gate 				    " (probably no EFI labels exist)\n",
10640Sstevel@tonic-gate 				    error);
10650Sstevel@tonic-gate 			}
10660Sstevel@tonic-gate 		}
10670Sstevel@tonic-gate 	}
10680Sstevel@tonic-gate 
10690Sstevel@tonic-gate 	if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) {
1070251Slclee 		(void) fprintf(stderr,
10710Sstevel@tonic-gate 		    "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n",
10720Sstevel@tonic-gate 		    Dfltdev);
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 	if (errno == 0)
10750Sstevel@tonic-gate 		return;
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	/* write to disk drive */
10780Sstevel@tonic-gate 	if (lseek(Dev, sect, SEEK_SET) == -1) {
1079251Slclee 		(void) fprintf(stderr,
10800Sstevel@tonic-gate 		    "fdisk: Error seeking to master boot record on %s.\n",
10810Sstevel@tonic-gate 		    Dfltdev);
10820Sstevel@tonic-gate 		exit(1);
10830Sstevel@tonic-gate 	}
10840Sstevel@tonic-gate 	if (write(Dev, buff, bootsiz) != bootsiz) {
1085251Slclee 		(void) fprintf(stderr,
10860Sstevel@tonic-gate 		    "fdisk: Error writing master boot record to %s.\n",
10870Sstevel@tonic-gate 		    Dfltdev);
10880Sstevel@tonic-gate 		exit(1);
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate }
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate /*
10930Sstevel@tonic-gate  * mboot_read
10940Sstevel@tonic-gate  * Read the prototype boot records from the files.
10950Sstevel@tonic-gate  */
1096251Slclee static void
1097251Slclee mboot_read(void)
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	int mDev, i;
11000Sstevel@tonic-gate 	struct ipart *part;
11010Sstevel@tonic-gate 
11020Sstevel@tonic-gate #if defined(i386) || defined(sparc)
11030Sstevel@tonic-gate 	/*
11040Sstevel@tonic-gate 	 * If the master boot file hasn't been specified, use the
11050Sstevel@tonic-gate 	 * implementation architecture name to generate the default one.
11060Sstevel@tonic-gate 	 */
11070Sstevel@tonic-gate 	if (io_mboot == (char *)0) {
11080Sstevel@tonic-gate 		/*
11090Sstevel@tonic-gate 		 * Bug ID 1249035:
11100Sstevel@tonic-gate 		 *	The mboot file must be delivered on all platforms
11110Sstevel@tonic-gate 		 *	and installed in a non-platform-dependent
11120Sstevel@tonic-gate 		 *	directory; i.e., /usr/lib/fs/ufs.
11130Sstevel@tonic-gate 		 */
11140Sstevel@tonic-gate 		io_mboot = "/usr/lib/fs/ufs/mboot";
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate 	/* First read in the master boot record */
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	/* Open the master boot proto file */
11200Sstevel@tonic-gate 	if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) {
1121251Slclee 		(void) fprintf(stderr,
11220Sstevel@tonic-gate 		    "fdisk: Cannot open master boot file %s.\n",
11230Sstevel@tonic-gate 		    io_mboot);
11240Sstevel@tonic-gate 		exit(1);
11250Sstevel@tonic-gate 	}
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 	/* Read the master boot program */
11280Sstevel@tonic-gate 	if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof
11290Sstevel@tonic-gate 	    (struct mboot)) {
1130251Slclee 		(void) fprintf(stderr,
11310Sstevel@tonic-gate 		    "fdisk: Cannot read master boot file %s.\n",
11320Sstevel@tonic-gate 		    io_mboot);
11330Sstevel@tonic-gate 		exit(1);
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 
11360Sstevel@tonic-gate 	/* Is this really a master boot record? */
11370Sstevel@tonic-gate 	if (les(BootCod.signature) != MBB_MAGIC) {
1138251Slclee 		(void) fprintf(stderr,
11390Sstevel@tonic-gate 		    "fdisk: Invalid master boot file %s.\n", io_mboot);
1140251Slclee 		(void) fprintf(stderr,
1141251Slclee 		    "Bad magic number: is %x, but should be %x.\n",
11420Sstevel@tonic-gate 		    les(BootCod.signature), MBB_MAGIC);
11430Sstevel@tonic-gate 		exit(1);
11440Sstevel@tonic-gate 	}
11450Sstevel@tonic-gate 
1146251Slclee 	(void) close(mDev);
11470Sstevel@tonic-gate #else
11480Sstevel@tonic-gate #error	fdisk needs to be ported to new architecture
11490Sstevel@tonic-gate #endif
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	/* Zero out the partitions part of this record */
11520Sstevel@tonic-gate 	part = (struct ipart *)BootCod.parts;
11530Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++, part++) {
1154251Slclee 		(void) memset(part, 0, sizeof (struct ipart));
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * fill_patt
11610Sstevel@tonic-gate  * Fill the disk with user/sector number pattern.
11620Sstevel@tonic-gate  */
1163251Slclee static void
1164251Slclee fill_patt(void)
11650Sstevel@tonic-gate {
1166251Slclee 	int	*buff_ptr, i;
1167*6549Sbharding 	off_t	*off_ptr;
11680Sstevel@tonic-gate 	int	io_fpatt = 0;
11690Sstevel@tonic-gate 	int	io_ipatt = 0;
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
11720Sstevel@tonic-gate 		io_fpatt++;
11730Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
11740Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11750Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
11765169Slclee 			*buff_ptr = io_ipatt;
11770Sstevel@tonic-gate 	}
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 	/*
11800Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
11810Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
11820Sstevel@tonic-gate 	 * for io_size blocks.
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	while (io_size--) {
1185*6549Sbharding 		off_ptr = (off_t *)Bootsect;
11860Sstevel@tonic-gate 		if (!io_fpatt) {
1187*6549Sbharding 			for (i = 0; i < sectsiz;
1188*6549Sbharding 			    i += sizeof (off_t), off_ptr++)
1189*6549Sbharding 				*off_ptr = io_offset;
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 		/* Write the data to disk */
1192*6549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
1193*6549Sbharding 		    SEEK_SET) == -1) {
1194251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
11955169Slclee 			    Dfltdev);
11960Sstevel@tonic-gate 			exit(1);
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1199251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12005169Slclee 			    Dfltdev);
12010Sstevel@tonic-gate 			exit(1);
12020Sstevel@tonic-gate 		}
12030Sstevel@tonic-gate 	} /* while (--io_size); */
12040Sstevel@tonic-gate }
12050Sstevel@tonic-gate 
12060Sstevel@tonic-gate /*
12070Sstevel@tonic-gate  * abs_read
12080Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
12090Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
12100Sstevel@tonic-gate  */
1211251Slclee static void
1212251Slclee abs_read(void)
1213251Slclee {
12140Sstevel@tonic-gate 	int c;
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 	while (io_size--) {
1217*6549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
1218*6549Sbharding 		    SEEK_SET) == -1) {
1219251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12200Sstevel@tonic-gate 			    Dfltdev);
12210Sstevel@tonic-gate 			exit(1);
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1224251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
12250Sstevel@tonic-gate 			    Dfltdev);
12260Sstevel@tonic-gate 			exit(1);
12270Sstevel@tonic-gate 		}
12280Sstevel@tonic-gate 
12290Sstevel@tonic-gate 		/* Write to standard ouptut */
1230251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12310Sstevel@tonic-gate 			if (c >= 0) {
12320Sstevel@tonic-gate 				if (io_debug)
1233251Slclee 					(void) fprintf(stderr,
1234251Slclee 					    "fdisk: Output warning: %d of %d"
1235251Slclee 					    " characters written.\n",
1236251Slclee 					    c, sectsiz);
12370Sstevel@tonic-gate 				exit(2);
12380Sstevel@tonic-gate 			} else {
12390Sstevel@tonic-gate 				perror("write error on output file.");
12400Sstevel@tonic-gate 				exit(2);
12410Sstevel@tonic-gate 			}
12420Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
12430Sstevel@tonic-gate 			/* != sectsiz) */
12440Sstevel@tonic-gate 	} /* while (--io_size); */
12450Sstevel@tonic-gate 	exit(0);
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate /*
12490Sstevel@tonic-gate  * abs_write
12500Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
12510Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
12520Sstevel@tonic-gate  */
1253251Slclee static void
1254251Slclee abs_write(void)
12550Sstevel@tonic-gate {
12560Sstevel@tonic-gate 	int c, i;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 	while (io_size--) {
12590Sstevel@tonic-gate 		int part_exit = 0;
12600Sstevel@tonic-gate 		/* Read from standard input */
12610Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12620Sstevel@tonic-gate 			if (c >= 0) {
12630Sstevel@tonic-gate 				if (io_debug)
1264251Slclee 				(void) fprintf(stderr,
12650Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
12660Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
12675169Slclee 				    c, sectsiz);
12680Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
12690Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
12700Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
12710Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
12720Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
12730Sstevel@tonic-gate 					Bootsect[i++] = 0;
12740Sstevel@tonic-gate 				}
12750Sstevel@tonic-gate 				part_exit++;
12760Sstevel@tonic-gate 			} else {
12770Sstevel@tonic-gate 				perror("read error on input file.");
12780Sstevel@tonic-gate 				exit(2);
12790Sstevel@tonic-gate 			}
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		}
12820Sstevel@tonic-gate 		/* Write to disk drive */
1283*6549Sbharding 		if (lseek(Dev, (off_t)(sectsiz * io_offset++),
1284*6549Sbharding 		    SEEK_SET) == -1) {
1285251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12860Sstevel@tonic-gate 			    Dfltdev);
12870Sstevel@tonic-gate 			exit(1);
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1290251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12910Sstevel@tonic-gate 			    Dfltdev);
12920Sstevel@tonic-gate 			exit(1);
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 		if (part_exit)
12950Sstevel@tonic-gate 		exit(0);
12960Sstevel@tonic-gate 	} /* while (--io_size); */
12970Sstevel@tonic-gate 	exit(1);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
1300251Slclee 
13010Sstevel@tonic-gate /*
13020Sstevel@tonic-gate  * load
13030Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
13040Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
13050Sstevel@tonic-gate  */
13060Sstevel@tonic-gate 
1307251Slclee static void
1308251Slclee load(int funct, char *file)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	int	id;
13110Sstevel@tonic-gate 	int	act;
13120Sstevel@tonic-gate 	int	bhead;
13130Sstevel@tonic-gate 	int	bsect;
13140Sstevel@tonic-gate 	int	bcyl;
13150Sstevel@tonic-gate 	int	ehead;
13160Sstevel@tonic-gate 	int	esect;
13170Sstevel@tonic-gate 	int	ecyl;
13180Sstevel@tonic-gate 	int	rsect;
13190Sstevel@tonic-gate 	int	numsect;
13200Sstevel@tonic-gate 	char	line[256];
13210Sstevel@tonic-gate 	int	i = 0;
13220Sstevel@tonic-gate 	int	j;
13230Sstevel@tonic-gate 	FILE *fp;
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	switch (funct) {
13260Sstevel@tonic-gate 
13275169Slclee 	case LOADFILE:
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		/*
13300Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
13310Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
13320Sstevel@tonic-gate 		 * fdisk_file).
13330Sstevel@tonic-gate 		 */
13340Sstevel@tonic-gate 		nulltbl();
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		/* Open the prototype file */
13370Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
13380Sstevel@tonic-gate 			(void) fprintf(stderr,
13390Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
13400Sstevel@tonic-gate 			    file);
13410Sstevel@tonic-gate 			exit(1);
13420Sstevel@tonic-gate 		}
13430Sstevel@tonic-gate 
13440Sstevel@tonic-gate 		/* Read a line from the file */
13450Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
13460Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
13470Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
13480Sstevel@tonic-gate 				continue;
13490Sstevel@tonic-gate 			}
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate 			/*
13520Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
13530Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
13540Sstevel@tonic-gate 			 */
13550Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
13560Sstevel@tonic-gate 				(void) fprintf(stderr,
13570Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13580Sstevel@tonic-gate 				    line);
13590Sstevel@tonic-gate 				exit(1);
13600Sstevel@tonic-gate 			}
13610Sstevel@tonic-gate 			/*
13620Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
13630Sstevel@tonic-gate 			 * in table
13640Sstevel@tonic-gate 			 */
13650Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
13660Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
13670Sstevel@tonic-gate 				(void) fprintf(stderr,
13680Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13690Sstevel@tonic-gate 				    line);
13700Sstevel@tonic-gate 				exit(1);
13710Sstevel@tonic-gate 			}
13720Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1375251Slclee 			(void) fprintf(stderr,
13760Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
13770Sstevel@tonic-gate 			exit(1);
13780Sstevel@tonic-gate 		}
13790Sstevel@tonic-gate 
1380251Slclee 		(void) fclose(fp);
13810Sstevel@tonic-gate 		return;
13820Sstevel@tonic-gate 
13835169Slclee 	case LOADDEL:
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
1386251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
1387251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
1388251Slclee 			(void) fprintf(stderr,
1389251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1390251Slclee 			exit(1);
1391251Slclee 		}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 		/* Find the exact entry in the table */
13940Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
13950Sstevel@tonic-gate 			if (Table[i].systid == id &&
13960Sstevel@tonic-gate 			    Table[i].bootid == act &&
13970Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
13980Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
13995169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
1400251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
14010Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
14020Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
14035169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
1404251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
14050Sstevel@tonic-gate 			    Table[i].relsect == lel(rsect) &&
14060Sstevel@tonic-gate 			    Table[i].numsect == lel(numsect)) {
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 				/*
14090Sstevel@tonic-gate 				 * Found the entry. Now move rest of
14100Sstevel@tonic-gate 				 * entries up toward the top of the
14110Sstevel@tonic-gate 				 * table, leaving available entries at
14120Sstevel@tonic-gate 				 * the end of the fdisk table.
14130Sstevel@tonic-gate 				 */
1414251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
1415251Slclee 					Table[j].systid = Table[j + 1].systid;
1416251Slclee 					Table[j].bootid = Table[j + 1].bootid;
1417251Slclee 					Table[j].beghead = Table[j + 1].beghead;
1418251Slclee 					Table[j].begsect = Table[j + 1].begsect;
1419251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
1420251Slclee 					Table[j].endhead = Table[j + 1].endhead;
1421251Slclee 					Table[j].endsect = Table[j + 1].endsect;
1422251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
1423251Slclee 					Table[j].relsect = Table[j + 1].relsect;
1424251Slclee 					Table[j].numsect = Table[j + 1].numsect;
14250Sstevel@tonic-gate 				}
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 				/*
14280Sstevel@tonic-gate 				 * Mark the last entry as unused in case
14290Sstevel@tonic-gate 				 * all table entries were in use prior
14300Sstevel@tonic-gate 				 * to the deletion.
14310Sstevel@tonic-gate 				 */
14320Sstevel@tonic-gate 
1433251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
1434251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
14350Sstevel@tonic-gate 				return;
14360Sstevel@tonic-gate 			}
14370Sstevel@tonic-gate 		}
1438251Slclee 		(void) fprintf(stderr,
14390Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
14400Sstevel@tonic-gate 		    "	\"%s\"\n",
14410Sstevel@tonic-gate 		    file);
14420Sstevel@tonic-gate 		exit(1);
1443251Slclee 		/* FALLTHRU */
14440Sstevel@tonic-gate 
14455169Slclee 	case LOADADD:
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
1448251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
1449251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
1450251Slclee 			(void) fprintf(stderr,
1451251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1452251Slclee 			exit(1);
1453251Slclee 		}
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
14560Sstevel@tonic-gate 		if (rsect == 0) {
14570Sstevel@tonic-gate 			(void) fprintf(stderr,
14580Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
14590Sstevel@tonic-gate 			    "   \"%s\".\n",
14600Sstevel@tonic-gate 			    file);
14610Sstevel@tonic-gate 			exit(1);
14620Sstevel@tonic-gate 		}
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate 		/*
14650Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
14660Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
14670Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
14680Sstevel@tonic-gate 		 */
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 		if (id == EFI_PMBR) {
14710Sstevel@tonic-gate 			if (rsect != 1) {
14720Sstevel@tonic-gate 				(void) fprintf(stderr,
14730Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
14740Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
14750Sstevel@tonic-gate 				exit(1);
14760Sstevel@tonic-gate 			}
14770Sstevel@tonic-gate 
14785169Slclee 			if (numsect != dev_capacity - 1) {
14790Sstevel@tonic-gate 				(void) fprintf(stderr,
14800Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
1481251Slclee 				    "entire disk\n"
14825169Slclee 				    "(input numsect: %d - avail: %llu)\n",
1483251Slclee 				    numsect,
14845169Slclee 				    dev_capacity - 1);
14850Sstevel@tonic-gate 				exit(1);
14860Sstevel@tonic-gate 			}
14870Sstevel@tonic-gate 		}
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
14900Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
14910Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
14920Sstevel@tonic-gate 			(void) fprintf(stderr,
14930Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
14940Sstevel@tonic-gate 			    "	\"%s\"\n",
14950Sstevel@tonic-gate 			    file);
14960Sstevel@tonic-gate 			exit(1);
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
15000Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1501251Slclee 			(void) fprintf(stderr,
1502251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
15030Sstevel@tonic-gate 			exit(1);
15040Sstevel@tonic-gate 		}
15050Sstevel@tonic-gate 	} /* switch funct */
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate /*
15090Sstevel@tonic-gate  * Set_Table_CHS_Values
15100Sstevel@tonic-gate  *
15110Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
15120Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
15130Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
15140Sstevel@tonic-gate  *
15150Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
15160Sstevel@tonic-gate  *
15170Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
15180Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
15190Sstevel@tonic-gate  */
15200Sstevel@tonic-gate static void
15210Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
15260Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15270Sstevel@tonic-gate 		/*
15280Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
15290Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
15300Sstevel@tonic-gate 		 */
15310Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15320Sstevel@tonic-gate 		hd = MAX_HEAD;
15330Sstevel@tonic-gate 		sc = MAX_SECT;
15340Sstevel@tonic-gate 	} else {
15350Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15360Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15370Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15380Sstevel@tonic-gate 	}
15390Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
1540251Slclee 	Table[ti].beghead = (uchar_t)hd;
1541251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 	/*
15440Sstevel@tonic-gate 	 * This code is identical to the code above
15450Sstevel@tonic-gate 	 * except that it works on ending CHS values
15460Sstevel@tonic-gate 	 */
15470Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
15480Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15490Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15500Sstevel@tonic-gate 		hd = MAX_HEAD;
15510Sstevel@tonic-gate 		sc = MAX_SECT;
15520Sstevel@tonic-gate 	} else {
15530Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15540Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15550Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15560Sstevel@tonic-gate 	}
15570Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
1558251Slclee 	Table[ti].endhead = (uchar_t)hd;
1559251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate /*
15630Sstevel@tonic-gate  * insert_tbl
15640Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
15650Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
15660Sstevel@tonic-gate  *	entries!
15670Sstevel@tonic-gate  */
1568251Slclee static int
1569251Slclee insert_tbl(
1570251Slclee     int id, int act,
1571251Slclee     int bhead, int bsect, int bcyl,
1572251Slclee     int ehead, int esect, int ecyl,
1573251Slclee     int rsect, int numsect)
15740Sstevel@tonic-gate {
15750Sstevel@tonic-gate 	int	i;
15760Sstevel@tonic-gate 
15770Sstevel@tonic-gate 	/* validate partition size */
15785169Slclee 	if (rsect + numsect > dev_capacity) {
1579251Slclee 		(void) fprintf(stderr,
15800Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
15810Sstevel@tonic-gate 		return (-1);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/* find UNUSED partition table entry */
15850Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
15860Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
15870Sstevel@tonic-gate 			break;
15880Sstevel@tonic-gate 		}
15890Sstevel@tonic-gate 	}
15900Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
1591251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
15920Sstevel@tonic-gate 		return (-1);
15930Sstevel@tonic-gate 	}
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 
1596251Slclee 	Table[i].systid = (uchar_t)id;
1597251Slclee 	Table[i].bootid = (uchar_t)act;
15980Sstevel@tonic-gate 	Table[i].numsect = lel(numsect);
15990Sstevel@tonic-gate 	Table[i].relsect = lel(rsect);
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate 	/*
16020Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
16030Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
16060Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
16070Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
16080Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
16090Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
16100Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
16110Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
16120Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
16130Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
16140Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
1615251Slclee 		Table[i].beghead = (uchar_t)bhead;
1616251Slclee 		Table[i].endhead = (uchar_t)ehead;
1617251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
16180Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
16190Sstevel@tonic-gate 	} else {
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 		/*
16220Sstevel@tonic-gate 		 * The specified values are invalid,
16230Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
16240Sstevel@tonic-gate 		 */
16250Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
16260Sstevel@tonic-gate 	}
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	/*
16290Sstevel@tonic-gate 	 * return partition index
16300Sstevel@tonic-gate 	 */
16310Sstevel@tonic-gate 	return (i);
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate  * verify_tbl
16360Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
16370Sstevel@tonic-gate  * the disk.
16380Sstevel@tonic-gate  */
1639251Slclee static int
1640251Slclee verify_tbl(void)
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	int	i, j, rsect, numsect;
16430Sstevel@tonic-gate 	int	noMoreParts = 0;
16440Sstevel@tonic-gate 	int	numParts = 0;
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
1647251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
16480Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
16490Sstevel@tonic-gate 			numParts++;
16500Sstevel@tonic-gate 			/*
16510Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
16520Sstevel@tonic-gate 			 * EFI_PMBR part
16530Sstevel@tonic-gate 			 */
16540Sstevel@tonic-gate 			if (noMoreParts) {
16550Sstevel@tonic-gate 				return (-1);
16560Sstevel@tonic-gate 			}
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 			/*
16590Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
16600Sstevel@tonic-gate 			 * and must be Table entry 0
16610Sstevel@tonic-gate 			 */
16620Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
16630Sstevel@tonic-gate 				if (i == 0) {
16640Sstevel@tonic-gate 					noMoreParts = 1;
16650Sstevel@tonic-gate 				} else {
16660Sstevel@tonic-gate 					return (-1);
16670Sstevel@tonic-gate 				}
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
1670251Slclee 					(void) fprintf(stderr, "ERROR: "
16710Sstevel@tonic-gate 					    "Invalid starting sector "
16720Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
16730Sstevel@tonic-gate 					    "relsect %d "
16740Sstevel@tonic-gate 					    "(should be 1)\n",
16750Sstevel@tonic-gate 					    Table[i].relsect);
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate 					return (-1);
16780Sstevel@tonic-gate 				}
16790Sstevel@tonic-gate 
16805169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
1681251Slclee 					(void) fprintf(stderr, "ERROR: "
16820Sstevel@tonic-gate 					    "EFI_PMBR partition must "
16830Sstevel@tonic-gate 					    "encompass the entire "
1684251Slclee 					    "disk.\n numsect %d - "
16855169Slclee 					    "actual %llu\n",
16860Sstevel@tonic-gate 					    Table[i].numsect,
16875169Slclee 					    dev_capacity - 1);
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 					return (-1);
16900Sstevel@tonic-gate 				}
16910Sstevel@tonic-gate 			}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
16940Sstevel@tonic-gate 			rsect = lel(Table[i].relsect);
16950Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
16965169Slclee 			if ((rsect + numsect) > dev_capacity) {
16970Sstevel@tonic-gate 				return (-1);
16980Sstevel@tonic-gate 			}
16990Sstevel@tonic-gate 
1700251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
17010Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
17020Sstevel@tonic-gate 					int t_relsect = lel(Table[j].relsect);
17030Sstevel@tonic-gate 					int t_numsect = lel(Table[j].numsect);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 					if (noMoreParts) {
1706251Slclee 						(void) fprintf(stderr,
17070Sstevel@tonic-gate 						    "Cannot add partition to "
17080Sstevel@tonic-gate 						    "table; no more partitions "
17090Sstevel@tonic-gate 						    "allowed\n");
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 						if (io_debug) {
1712251Slclee 							(void) fprintf(stderr,
17130Sstevel@tonic-gate 							    "DEBUG: Current "
17140Sstevel@tonic-gate 							    "partition:\t"
17150Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1716251Slclee 							    "%d:%d:%d:%d:%d\n"
17170Sstevel@tonic-gate 							    "       Next "
17180Sstevel@tonic-gate 							    "partition:\t\t"
17190Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1720251Slclee 							    "%d:%d:%d:%d:%d\n",
17210Sstevel@tonic-gate 							    Table[i].systid,
17220Sstevel@tonic-gate 							    Table[i].bootid,
17230Sstevel@tonic-gate 							    Table[i].begcyl,
17240Sstevel@tonic-gate 							    Table[i].beghead,
17250Sstevel@tonic-gate 							    Table[i].begsect,
17260Sstevel@tonic-gate 							    Table[i].endcyl,
17270Sstevel@tonic-gate 							    Table[i].endhead,
17280Sstevel@tonic-gate 							    Table[i].endsect,
17290Sstevel@tonic-gate 							    Table[i].relsect,
17300Sstevel@tonic-gate 							    Table[i].numsect,
17310Sstevel@tonic-gate 							    Table[j].systid,
17320Sstevel@tonic-gate 							    Table[j].bootid,
17330Sstevel@tonic-gate 							    Table[j].begcyl,
17340Sstevel@tonic-gate 							    Table[j].beghead,
17350Sstevel@tonic-gate 							    Table[j].begsect,
17360Sstevel@tonic-gate 							    Table[j].endcyl,
17370Sstevel@tonic-gate 							    Table[j].endhead,
17380Sstevel@tonic-gate 							    Table[j].endsect,
17390Sstevel@tonic-gate 							    Table[j].relsect,
17400Sstevel@tonic-gate 							    Table[j].numsect);
17410Sstevel@tonic-gate 						}
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 						return (-1);
17440Sstevel@tonic-gate 					}
17450Sstevel@tonic-gate 
17460Sstevel@tonic-gate 					if ((rsect >=
17470Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
1748251Slclee 					    ((rsect + numsect) <= t_relsect)) {
17490Sstevel@tonic-gate 						continue;
17500Sstevel@tonic-gate 					} else {
1751251Slclee 						(void) fprintf(stderr, "ERROR: "
17520Sstevel@tonic-gate 						    "current partition overlaps"
17530Sstevel@tonic-gate 						    " following partition\n");
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 						return (-1);
17560Sstevel@tonic-gate 					}
17570Sstevel@tonic-gate 				}
17580Sstevel@tonic-gate 			}
17590Sstevel@tonic-gate 		} else {
17600Sstevel@tonic-gate 			noMoreParts = 1;
17610Sstevel@tonic-gate 		}
17620Sstevel@tonic-gate 	}
17630Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
17640Sstevel@tonic-gate 		if (noMoreParts ||
17650Sstevel@tonic-gate 		    ((lel(Table[i].relsect) + lel(Table[i].numsect)) >
17665169Slclee 		    dev_capacity)) {
17670Sstevel@tonic-gate 			return (-1);
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 	}
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate 	return (numParts);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate  * pars_fdisk
17760Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
17770Sstevel@tonic-gate  * (-A, -D, -F).
17780Sstevel@tonic-gate  */
1779251Slclee static int
1780251Slclee pars_fdisk(
1781251Slclee     char *line,
1782251Slclee     int *id, int *act,
1783251Slclee     int *bhead, int *bsect, int *bcyl,
1784251Slclee     int *ehead, int *esect, int *ecyl,
1785251Slclee     int *rsect, int *numsect)
17860Sstevel@tonic-gate {
17870Sstevel@tonic-gate 	int	i;
17880Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
17895169Slclee 		return (1);
17900Sstevel@tonic-gate 	line[strlen(line)] = '\0';
17910Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
17920Sstevel@tonic-gate 		if (line[i] == '\0') {
17930Sstevel@tonic-gate 			break;
17940Sstevel@tonic-gate 		} else if (line[i] == ':') {
17950Sstevel@tonic-gate 			line[i] = ' ';
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 	}
1798251Slclee 	if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d",
17990Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
18000Sstevel@tonic-gate 	    rsect, numsect) != 10) {
18010Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
18020Sstevel@tonic-gate 		exit(1);
18030Sstevel@tonic-gate 	}
18040Sstevel@tonic-gate 	return (0);
18050Sstevel@tonic-gate }
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate /*
18080Sstevel@tonic-gate  * validate_part
18090Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
18100Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
18110Sstevel@tonic-gate  */
1812251Slclee static int
1813251Slclee validate_part(int id, int rsect, int numsect)
18140Sstevel@tonic-gate {
18150Sstevel@tonic-gate 	int i;
18160Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
18170Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
18180Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
18190Sstevel@tonic-gate 			    (Old_Table[i].relsect == lel(rsect)) &&
18205169Slclee 			    (Old_Table[i].numsect == lel(numsect)))
18215169Slclee 				return (0);
18220Sstevel@tonic-gate 		}
1823251Slclee 		(void) fprintf(stderr,
1824251Slclee 		    "New partition cannot start at sector 0\n");
18250Sstevel@tonic-gate 		return (-1);
18260Sstevel@tonic-gate 	}
18270Sstevel@tonic-gate 	return (0);
18280Sstevel@tonic-gate }
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate /*
18310Sstevel@tonic-gate  * stage0
18320Sstevel@tonic-gate  * Print out interactive menu and process user input.
18330Sstevel@tonic-gate  */
1834251Slclee static void
1835251Slclee stage0(void)
18360Sstevel@tonic-gate {
1837251Slclee 	dispmenu();
1838251Slclee 	for (;;) {
1839251Slclee 		(void) printf(Q_LINE);
1840251Slclee 		(void) printf("Enter Selection: ");
1841251Slclee 		(void) gets(s);
18420Sstevel@tonic-gate 		rm_blanks(s);
18430Sstevel@tonic-gate 		while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) {
1844251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
1845251Slclee 			(void) printf(
1846251Slclee 			    "Enter a one-digit number between 1 and 6.");
1847251Slclee 			(void) printf(Q_LINE);
1848251Slclee 			(void) printf("Enter Selection: ");
1849251Slclee 			(void) gets(s);
18500Sstevel@tonic-gate 			rm_blanks(s);
18510Sstevel@tonic-gate 		}
1852251Slclee 		(void) printf(E_LINE);
18530Sstevel@tonic-gate 		switch (s[0]) {
18540Sstevel@tonic-gate 			case '1':
18550Sstevel@tonic-gate 				if (pcreate() == -1)
18560Sstevel@tonic-gate 					return;
18570Sstevel@tonic-gate 				break;
18580Sstevel@tonic-gate 			case '2':
18590Sstevel@tonic-gate 				if (pchange() == -1)
18600Sstevel@tonic-gate 					return;
18610Sstevel@tonic-gate 				break;
18620Sstevel@tonic-gate 			case '3':
18630Sstevel@tonic-gate 				if (pdelete() == -1)
18640Sstevel@tonic-gate 					return;
18650Sstevel@tonic-gate 				break;
18660Sstevel@tonic-gate 			case '4':
18670Sstevel@tonic-gate 				if (ppartid() == -1)
18680Sstevel@tonic-gate 					return;
18690Sstevel@tonic-gate 				break;
18700Sstevel@tonic-gate 			case '5':
18710Sstevel@tonic-gate 				/* update disk partition table, if changed */
18720Sstevel@tonic-gate 				if (TableChanged() == 1) {
18730Sstevel@tonic-gate 					copy_Table_to_Bootblk();
18740Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
18750Sstevel@tonic-gate 				}
18760Sstevel@tonic-gate 				/*
18770Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18780Sstevel@tonic-gate 				 * (truncate only)
18790Sstevel@tonic-gate 				 */
18800Sstevel@tonic-gate 				if (io_adjt) {
18810Sstevel@tonic-gate 					fix_slice();
18820Sstevel@tonic-gate 				}
1883251Slclee 				(void) close(Dev);
18840Sstevel@tonic-gate 				exit(0);
1885251Slclee 				/* FALLTHRU */
18860Sstevel@tonic-gate 			case '6':
18870Sstevel@tonic-gate 				/*
18880Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18890Sstevel@tonic-gate 				 * (truncate only)
18900Sstevel@tonic-gate 				 */
18910Sstevel@tonic-gate 				if (io_adjt) {
18920Sstevel@tonic-gate 					fix_slice();
18930Sstevel@tonic-gate 				}
1894251Slclee 				(void) close(Dev);
18950Sstevel@tonic-gate 				exit(0);
1896251Slclee 				/* FALLTHRU */
18970Sstevel@tonic-gate 			default:
18980Sstevel@tonic-gate 				break;
18990Sstevel@tonic-gate 		}
19000Sstevel@tonic-gate 		copy_Table_to_Bootblk();
19010Sstevel@tonic-gate 		disptbl();
1902251Slclee 		dispmenu();
19030Sstevel@tonic-gate 	}
19040Sstevel@tonic-gate }
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate /*
19070Sstevel@tonic-gate  * pcreate
19080Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
19090Sstevel@tonic-gate  */
1910251Slclee static int
1911251Slclee pcreate(void)
19120Sstevel@tonic-gate {
1913251Slclee 	uchar_t tsystid = 'z';
19140Sstevel@tonic-gate 	int i, j;
19150Sstevel@tonic-gate 	int rsect = 1;
19160Sstevel@tonic-gate 	int retCode = 0;
19170Sstevel@tonic-gate 
19180Sstevel@tonic-gate 	i = 0;
1919251Slclee 	for (;;) {
19200Sstevel@tonic-gate 		if (i == FD_NUMPART) {
1921251Slclee 			(void) printf(E_LINE);
1922251Slclee 			(void) printf(
1923251Slclee 			    "The partition table is full!\n"
1924251Slclee 			    "You must delete a partition before creating"
19250Sstevel@tonic-gate 			    " a new one.\n");
19260Sstevel@tonic-gate 			return (-1);
19270Sstevel@tonic-gate 		}
19280Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
19290Sstevel@tonic-gate 			break;
19300Sstevel@tonic-gate 		}
19310Sstevel@tonic-gate 		i++;
19320Sstevel@tonic-gate 	}
19330Sstevel@tonic-gate 
19340Sstevel@tonic-gate 	j = 0;
19350Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
19360Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
19370Sstevel@tonic-gate 			j += lel(Table[i].numsect);
19380Sstevel@tonic-gate 		}
19395169Slclee 		if (j >= chs_capacity) {
1940251Slclee 			(void) printf(E_LINE);
1941251Slclee 			(void) printf("There is no more room on the disk for"
19420Sstevel@tonic-gate 			    " another partition.\n");
1943251Slclee 			(void) printf(
1944251Slclee 			    "You must delete a partition before creating"
19450Sstevel@tonic-gate 			    " a new one.\n");
19460Sstevel@tonic-gate 			return (-1);
19470Sstevel@tonic-gate 		}
19480Sstevel@tonic-gate 	}
19490Sstevel@tonic-gate 	while (tsystid == 'z') {
1950251Slclee 		(void) printf(Q_LINE);
1951251Slclee 		(void) printf(
1952251Slclee 		    "Select the partition type to create:\n"
1953251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
1954251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
1955251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
1956251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
1957251Slclee 		(void) gets(s);
19580Sstevel@tonic-gate 		rm_blanks(s);
19590Sstevel@tonic-gate 		if (s[1] != 0) {
1960251Slclee 			(void) printf(E_LINE);
1961251Slclee 			(void) printf("Invalid selection, try again.");
19620Sstevel@tonic-gate 			continue;
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		switch (s[0]) {
19650Sstevel@tonic-gate 		case '0':		/* exit */
19665169Slclee 			(void) printf(E_LINE);
19675169Slclee 			return (-1);
19680Sstevel@tonic-gate 		case '1':		/* Solaris partition */
19695169Slclee 			tsystid = SUNIXOS2;
19705169Slclee 			break;
19710Sstevel@tonic-gate 		case '2':		/* UNIX partition */
19725169Slclee 			tsystid = UNIXOS;
19735169Slclee 			break;
19740Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
19755169Slclee 			tsystid = PCIXOS;
19765169Slclee 			break;
19770Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
19785169Slclee 			tsystid = OTHEROS;
19795169Slclee 			break;
19800Sstevel@tonic-gate 		case '5':
19815169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
19825169Slclee 			break;
19830Sstevel@tonic-gate 		case '6':
19845169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
19855169Slclee 			break;
19860Sstevel@tonic-gate 		case '7':
19875169Slclee 			tsystid = EXTDOS;
19885169Slclee 			break;
19890Sstevel@tonic-gate 		case '8':
19905169Slclee 			tsystid = DOSHUGE;
19915169Slclee 			break;
19920Sstevel@tonic-gate 		case '9':
19935169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
19945169Slclee 			break;
19950Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
19960Sstevel@tonic-gate 		case 'A':
19975169Slclee 			tsystid = X86BOOT;
19985169Slclee 			break;
19990Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
20000Sstevel@tonic-gate 		case 'B':
20015169Slclee 			tsystid = DIAGPART;
20025169Slclee 			break;
20030Sstevel@tonic-gate 		case 'c':		/* FAT32 */
20040Sstevel@tonic-gate 		case 'C':
20055169Slclee 			tsystid = FDISK_WINDOWS;
20065169Slclee 			break;
20070Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
20080Sstevel@tonic-gate 		case 'D':
20095169Slclee 			tsystid = FDISK_EXT_WIN;
20105169Slclee 			break;
20110Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
20120Sstevel@tonic-gate 		case 'E':
20135169Slclee 			tsystid = FDISK_EXTLBA;
20145169Slclee 			break;
20150Sstevel@tonic-gate 		case 'f':
20160Sstevel@tonic-gate 		case 'F':
20175169Slclee 			tsystid = EFI_PMBR;
20185169Slclee 			break;
20190Sstevel@tonic-gate 		default:
20205169Slclee 			(void) printf(E_LINE);
20215169Slclee 			(void) printf("Invalid selection, try again.");
20225169Slclee 			continue;
20230Sstevel@tonic-gate 		}
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate 
2026251Slclee 	(void) printf(E_LINE);
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
20290Sstevel@tonic-gate 		/* create the new partition */
20300Sstevel@tonic-gate 		i = specify(tsystid);
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		if (i != -1) {
20330Sstevel@tonic-gate 			/* see if it should be the active partition */
2034251Slclee 			(void) printf(E_LINE);
2035251Slclee 			(void) printf(Q_LINE);
2036251Slclee 
2037251Slclee 			(void) printf(
2038251Slclee 			    "Should this become the active partition? If "
2039251Slclee 			    "yes, it  will be activated\n"
2040251Slclee 			    "each time the computer is reset or turned on.\n"
2041251Slclee 			    "Please type \"y\" or \"n\". ");
20420Sstevel@tonic-gate 
20430Sstevel@tonic-gate 			if (yesno()) {
2044251Slclee 				(void) printf(E_LINE);
20450Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
20460Sstevel@tonic-gate 					if (j == i) {
20470Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2048251Slclee 						(void) printf(E_LINE);
2049251Slclee 						(void) printf(
2050251Slclee 						    "Partition %d is now "
20510Sstevel@tonic-gate 						    "the active partition.",
2052251Slclee 						    j + 1);
20530Sstevel@tonic-gate 					} else {
20540Sstevel@tonic-gate 						Table[j].bootid = 0;
20550Sstevel@tonic-gate 					}
20560Sstevel@tonic-gate 				}
20570Sstevel@tonic-gate 			} else {
20580Sstevel@tonic-gate 				Table[i].bootid = 0;
20590Sstevel@tonic-gate 			}
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 			/* set up the return code */
20620Sstevel@tonic-gate 			i = 1;
20630Sstevel@tonic-gate 		}
20640Sstevel@tonic-gate 	} else {
20650Sstevel@tonic-gate 		/*
20660Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
20670Sstevel@tonic-gate 		 * the table
20680Sstevel@tonic-gate 		 *
20690Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
20700Sstevel@tonic-gate 		 * empty
20710Sstevel@tonic-gate 		 */
20720Sstevel@tonic-gate 		retCode = verify_tbl();
20730Sstevel@tonic-gate 
20740Sstevel@tonic-gate 		if (retCode < 0) {
2075251Slclee 			(void) fprintf(stderr,
20760Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
20770Sstevel@tonic-gate 			    "current partition table is invalid.\n");
20780Sstevel@tonic-gate 			return (-1);
20790Sstevel@tonic-gate 		} else if (retCode > 0) {
2080251Slclee 			(void) printf(
2081251Slclee 			    "An EFI partition must be the only partition on "
2082251Slclee 			    "disk.  You may manually delete existing\n"
2083251Slclee 			    "partitions, or fdisk can do it.\n"
2084251Slclee 			    "Do you want fdisk to destroy existing "
2085251Slclee 			    "partitions?\n"
2086251Slclee 			    "Please type \"y\" or \"n\". ");
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 			if (yesno()) {
20890Sstevel@tonic-gate 				nulltbl();
20900Sstevel@tonic-gate 			} else {
20910Sstevel@tonic-gate 				return (-1);
20920Sstevel@tonic-gate 			}
20930Sstevel@tonic-gate 		}
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
20960Sstevel@tonic-gate 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, rsect,
20975169Slclee 		    dev_capacity - rsect);
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 		if (i != 0) {
2100251Slclee 			(void) printf("Error creating EFI partition!!!\n");
21010Sstevel@tonic-gate 			i = -1;
21020Sstevel@tonic-gate 		} else {
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 			/* EFI partitions are currently never active */
21050Sstevel@tonic-gate 			Table[i].bootid = 0;
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 			/* set up the return code */
21080Sstevel@tonic-gate 			i = 1;
21090Sstevel@tonic-gate 		}
21100Sstevel@tonic-gate 	}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 	return (i);
21130Sstevel@tonic-gate }
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate /*
21160Sstevel@tonic-gate  * specify
21170Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
21180Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
21190Sstevel@tonic-gate  * cylinder and length in cylinders.
21200Sstevel@tonic-gate  */
2121251Slclee static int
2122251Slclee specify(uchar_t tsystid)
21230Sstevel@tonic-gate {
21245169Slclee 	int	i, j, percent = -1;
21250Sstevel@tonic-gate 	int	cyl, cylen, first_free, size_free;
21265169Slclee 	int	max_free;
21275169Slclee 	int	cyl_size;
21280Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
21290Sstevel@tonic-gate 
21305169Slclee 	cyl_size = heads * sectors;
21315169Slclee 
21325169Slclee 	/*
21335169Slclee 	 * make a local copy of the partition table
21345169Slclee 	 * and sort it into relsect order
21355169Slclee 	 */
21365169Slclee 	for (i = 0; i < FD_NUMPART; i++)
21375169Slclee 		partition[i] = &Table[i];
21385169Slclee 
21395169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
21405169Slclee 		if (partition[i]->systid == UNUSED)
21415169Slclee 			break;
21425169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
21435169Slclee 			if (partition[j]->systid == UNUSED)
21445169Slclee 				break;
21455169Slclee 			if (lel(partition[j]->relsect) <
21465169Slclee 			    lel(partition[i]->relsect)) {
21475169Slclee 				struct ipart *temp = partition[i];
21485169Slclee 				partition[i] = partition[j];
21495169Slclee 				partition[j] = temp;
21505169Slclee 			}
21515169Slclee 		}
21525169Slclee 	}
21535169Slclee 
2154251Slclee 	(void) printf(Q_LINE);
2155251Slclee 	(void) printf(
2156251Slclee 	    "Specify the percentage of disk to use for this partition\n"
2157251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
2158251Slclee 	(void) gets(s);
21590Sstevel@tonic-gate 	rm_blanks(s);
21600Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
21615169Slclee 		i = 0;
21625169Slclee 		while (s[i] != '\0') {
21635169Slclee 			if (s[i] < '0' || s[i] > '9') {
21645169Slclee 				(void) printf(E_LINE);
21655169Slclee 				(void) printf("Invalid percentage value "
21665169Slclee 				    "specified; retry the operation.");
21675169Slclee 				return (-1);
21685169Slclee 			}
21695169Slclee 			i++;
21705169Slclee 			if (i > 3) {
21715169Slclee 				(void) printf(E_LINE);
21725169Slclee 				(void) printf("Invalid percentage value "
21735169Slclee 				    "specified; retry the operation.");
21745169Slclee 				return (-1);
21755169Slclee 			}
21765169Slclee 		}
21775169Slclee 		if ((percent = atoi(s)) > 100) {
21785169Slclee 			(void) printf(E_LINE);
21795169Slclee 			(void) printf(
21805169Slclee 			    "Percentage value is too large. The value must be"
21815169Slclee 			    " between 1 and 100;\nretry the operation.\n");
21825169Slclee 			return (-1);
21830Sstevel@tonic-gate 		}
21845169Slclee 		if (percent < 1) {
21855169Slclee 			(void) printf(E_LINE);
21865169Slclee 			(void) printf(
21875169Slclee 			    "Percentage value is too small. The value must be"
21885169Slclee 			    " between 1 and 100;\nretry the operation.\n");
21895169Slclee 			return (-1);
21905169Slclee 		}
21915169Slclee 
21925169Slclee 
21935169Slclee 		if (percent == 100)
21945169Slclee 			cylen = Numcyl - 1;
21955169Slclee 		else
21965169Slclee 			cylen = (Numcyl * percent) / 100;
21975169Slclee 
21985169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
21995169Slclee 		if ((tsystid == DOSOS12) &&
22005169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
22015169Slclee 			int n;
22025169Slclee 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl;
22035169Slclee 			(void) printf(E_LINE);
22045169Slclee 			(void) printf("Maximum size for a DOS partition "
22055169Slclee 			    "is %d%%; retry the operation.",
22065169Slclee 			    n <= 100 ? n : 100);
22075169Slclee 			return (-1);
22080Sstevel@tonic-gate 		}
22095169Slclee 
22105169Slclee 
22115169Slclee 		max_free = 0;
22125169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
22135169Slclee 
22145169Slclee 			/*
22155169Slclee 			 * check for free space before partition i
22165169Slclee 			 * where i varies from 0 to 3
22175169Slclee 			 *
22185169Slclee 			 * freespace after partition 3 is unusable
22195169Slclee 			 * because there are no free partitions
22205169Slclee 			 *
22215169Slclee 			 * freespace begins at the end of previous partition
22225169Slclee 			 * or cylinder 1
22235169Slclee 			 */
22245169Slclee 			if (i) {
22255169Slclee 				/* Not an empty table */
22265169Slclee 				first_free = lel(partition[i - 1]->relsect) +
22275169Slclee 				    lel(partition[i - 1]->numsect);
22285169Slclee 			} else {
22295169Slclee 				first_free = cyl_size;
22305169Slclee 			}
22315169Slclee 
22325169Slclee 			/*
22335169Slclee 			 * freespace ends before the current partition
22345169Slclee 			 * or the end of the disk (chs end)
22355169Slclee 			 */
22365169Slclee 			if (partition[i]->systid == UNUSED) {
22375169Slclee 				size_free = chs_capacity - first_free;
22385169Slclee 			} else {
22395169Slclee 				size_free =
22405169Slclee 				    lel(partition[i]->relsect) - first_free;
22415169Slclee 			}
22425169Slclee 
22435169Slclee 			/* save largest free space */
22445169Slclee 			if (max_free < size_free)
22455169Slclee 				max_free = size_free;
22465169Slclee 
22475169Slclee 			if ((cylen * cyl_size) <= size_free) {
22485169Slclee 				/* We found a place to use */
22495169Slclee 				break;
22505169Slclee 			}
22515169Slclee 			if (partition[i]->systid == UNUSED) {
22525169Slclee 				(void) printf(E_LINE);
22535169Slclee 				max_free /= (cyl_size);
22545169Slclee 				(void) fprintf(stderr, "fdisk: "
22555169Slclee 				    "Maximum percentage available is %d\n",
22565169Slclee 				    100 * max_free / Numcyl);
22575169Slclee 				return (-1);
22585169Slclee 			}
22590Sstevel@tonic-gate 		}
22605169Slclee 
22615169Slclee 		(void) printf(E_LINE);
22625169Slclee 		if (i >= FD_NUMPART) {
22635169Slclee 			(void) fprintf(stderr,
22645169Slclee 			    "fdisk: Partition table is full.\n");
22655169Slclee 			return (-1);
22665169Slclee 		}
22675169Slclee 
22685169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
22695169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
22705169Slclee 			return (i);
22715169Slclee 		}
22725169Slclee 		return (-1);
22735169Slclee 	} else {
22745169Slclee 
22755169Slclee 		/* Specifying size in cylinders */
2276251Slclee 		(void) printf(E_LINE);
22775169Slclee 		(void) printf(Q_LINE);
22785169Slclee 		(void) printf("Enter starting cylinder number: ");
22795169Slclee 		if ((cyl = getcyl()) == -1) {
22805169Slclee 			(void) printf(E_LINE);
22815169Slclee 			(void) printf("Invalid number; retry the operation.");
22825169Slclee 			return (-1);
22835169Slclee 		}
22845169Slclee 		if (cyl == 0) {
22855169Slclee 			(void) printf(E_LINE);
22865169Slclee 			(void) printf(
22875169Slclee 			    "New partition cannot start at cylinder 0.\n");
22885169Slclee 			return (-1);
22895169Slclee 		}
22905169Slclee 		if (cyl >= (unsigned int)Numcyl) {
22915169Slclee 			(void) printf(E_LINE);
22925169Slclee 			(void) printf(
22935169Slclee 			    "Cylinder %d is out of bounds, "
22945169Slclee 			    "the maximum is %d.\n",
22955169Slclee 			    cyl, Numcyl - 1);
22965169Slclee 			return (-1);
22975169Slclee 		}
22985169Slclee 		(void) printf(Q_LINE);
22995169Slclee 		(void) printf("Enter partition size in cylinders: ");
23005169Slclee 		if ((cylen = getcyl()) == -1) {
23015169Slclee 			(void) printf(E_LINE);
23025169Slclee 			(void) printf("Invalid number, retry the operation.");
23035169Slclee 			return (-1);
23045169Slclee 		}
23055169Slclee 
23065169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
23075169Slclee 			uint32_t	t_relsect, t_numsect;
23085169Slclee 
23095169Slclee 			if (partition[i]->systid == UNUSED)
23105169Slclee 				break;
23115169Slclee 			t_relsect = lel(partition[i]->relsect);
23125169Slclee 			t_numsect = lel(partition[i]->numsect);
23135169Slclee 
23145169Slclee 			if (cyl * cyl_size >= t_relsect &&
23155169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
23165169Slclee 				(void) printf(E_LINE);
23175169Slclee 				(void) printf(
23185169Slclee 				    "Cylinder %d is already allocated"
23195169Slclee 				    "\nretry the operation.",
23205169Slclee 				    cyl);
23215169Slclee 				return (-1);
23225169Slclee 			}
23235169Slclee 
23245169Slclee 			if (cyl * cyl_size < t_relsect &&
23255169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
23265169Slclee 				(void) printf(E_LINE);
23275169Slclee 				(void) printf(
23285169Slclee 				    "Maximum size for partition is %u cylinders"
23295169Slclee 				    "\nretry the operation.",
23305169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
23315169Slclee 				return (-1);
23325169Slclee 			}
23335169Slclee 		}
23345169Slclee 
23355169Slclee 		/* Verify partition doesn't exceed disk size */
23365169Slclee 		if (cyl + cylen > Numcyl) {
23375169Slclee 			(void) printf(E_LINE);
23385169Slclee 			(void) printf(
23395169Slclee 			    "Maximum size for partition is %d cylinders;"
23405169Slclee 			    "\nretry the operation.",
23415169Slclee 			    Numcyl - cyl);
23425169Slclee 			return (-1);
23435169Slclee 		}
23445169Slclee 
23455169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
23465169Slclee 		if ((tsystid == DOSOS12) &&
23475169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
23485169Slclee 			(void) printf(E_LINE);
23495169Slclee 			(void) printf(
23505169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
23515169Slclee 			    "\nretry the operation.",
23525169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
23535169Slclee 			return (-1);
23545169Slclee 		}
23555169Slclee 
2356251Slclee 		(void) printf(E_LINE);
23575169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
23585169Slclee 		    cyl * cyl_size, cylen * cyl_size);
23595169Slclee 		if (i < 0)
23605169Slclee 			return (-1);
23615169Slclee 
23625169Slclee 		if (verify_tbl() < 0) {
23635169Slclee 			(void) printf(E_LINE);
23645169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
23655169Slclee 			return (-1);
23665169Slclee 		}
23675169Slclee 
23685169Slclee 		return (i);
23690Sstevel@tonic-gate 	}
23700Sstevel@tonic-gate }
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate /*
23730Sstevel@tonic-gate  * dispmenu
23740Sstevel@tonic-gate  * Display command menu (interactive mode).
23750Sstevel@tonic-gate  */
2376251Slclee static void
2377251Slclee dispmenu(void)
23780Sstevel@tonic-gate {
2379251Slclee 	(void) printf(M_LINE);
2380251Slclee 	(void) printf(
2381251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
2382251Slclee 	    "   1. Create a partition\n"
2383251Slclee 	    "   2. Specify the active partition\n"
2384251Slclee 	    "   3. Delete a partition\n"
2385251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
2386251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
2387251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
23880Sstevel@tonic-gate }
23890Sstevel@tonic-gate 
23900Sstevel@tonic-gate /*
23910Sstevel@tonic-gate  * pchange
23920Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
23930Sstevel@tonic-gate  */
2394251Slclee static int
2395251Slclee pchange(void)
23960Sstevel@tonic-gate {
23970Sstevel@tonic-gate 	char s[80];
23980Sstevel@tonic-gate 	int i, j;
23990Sstevel@tonic-gate 
2400251Slclee 	for (;;) {
2401251Slclee 		(void) printf(Q_LINE);
24020Sstevel@tonic-gate 			{
2403251Slclee 			(void) printf(
2404251Slclee 			    "Specify the partition number to boot from"
24050Sstevel@tonic-gate 			    " (or specify 0 for none): ");
24060Sstevel@tonic-gate 			}
2407251Slclee 		(void) gets(s);
24080Sstevel@tonic-gate 		rm_blanks(s);
24090Sstevel@tonic-gate 		if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) {
2410251Slclee 			(void) printf(E_LINE);
2411251Slclee 			(void) printf(
2412251Slclee 			    "Invalid response, please specify a number"
24130Sstevel@tonic-gate 			    " between 0 and 4.\n");
24140Sstevel@tonic-gate 		} else {
24150Sstevel@tonic-gate 			break;
24160Sstevel@tonic-gate 		}
24170Sstevel@tonic-gate 	}
24180Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
24190Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
24200Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
24210Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
24220Sstevel@tonic-gate 				Table[i].bootid = 0;
24230Sstevel@tonic-gate 		}
2424251Slclee 		(void) printf(E_LINE);
2425251Slclee 			(void) printf(
2426251Slclee 			    "No partition is currently marked as active.");
24270Sstevel@tonic-gate 		return (0);
24280Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
24290Sstevel@tonic-gate 		i = s[0] - '1';
24300Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2431251Slclee 			(void) printf(E_LINE);
2432251Slclee 			(void) printf("Partition does not exist.");
24330Sstevel@tonic-gate 			return (-1);
24340Sstevel@tonic-gate 		}
24350Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
24360Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
24370Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
24380Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
2439251Slclee 			(void) printf(E_LINE);
2440251Slclee 			(void) printf(
2441251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
24420Sstevel@tonic-gate 			    "cannot be made active.\n");
2443251Slclee 			(void) printf("Select another partition.");
24440Sstevel@tonic-gate 			return (-1);
24450Sstevel@tonic-gate 		}
24460Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
24470Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
24480Sstevel@tonic-gate 			if (j != i)
24490Sstevel@tonic-gate 			Table[j].bootid = 0;
24500Sstevel@tonic-gate 		}
24510Sstevel@tonic-gate 	}
2452251Slclee 	(void) printf(E_LINE);
24530Sstevel@tonic-gate 		{
2454251Slclee 		(void) printf(
2455251Slclee 		    "Partition %d is now active. The system will start up"
2456251Slclee 		    " from this\n", i + 1);
2457251Slclee 		(void) printf("partition after the next reboot.");
24580Sstevel@tonic-gate 		}
24590Sstevel@tonic-gate 	return (1);
24600Sstevel@tonic-gate }
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate /*
24630Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
24640Sstevel@tonic-gate  */
2465251Slclee static int
2466251Slclee ppartid(void)
24670Sstevel@tonic-gate {
24680Sstevel@tonic-gate 	char	*p, s[80];
24690Sstevel@tonic-gate 	int	i;
24700Sstevel@tonic-gate 
24710Sstevel@tonic-gate 	for (;;) {
2472251Slclee 		(void) printf(Q_LINE);
2473251Slclee 		(void) printf("Specify the partition number to change"
24745169Slclee 		    " (or enter 0 to exit): ");
2475251Slclee 		if (!fgets(s, sizeof (s), stdin))
2476251Slclee 			return (1);
24770Sstevel@tonic-gate 		i = strtol(s, &p, 10);
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
2480251Slclee 			(void) printf(E_LINE);
2481251Slclee 			(void) printf(
2482251Slclee 			    "Invalid response, retry the operation.\n");
24830Sstevel@tonic-gate 			continue;
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 		if (i == 0) {
24870Sstevel@tonic-gate 			/* exit delete command */
2488251Slclee 			(void) printf(E_LINE); /* clear error message */
24890Sstevel@tonic-gate 			return (1);
24900Sstevel@tonic-gate 		}
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 		i -= 1;
24930Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
24940Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
24950Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
24960Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
24970Sstevel@tonic-gate 		} else {
2498251Slclee 			(void) printf(E_LINE);
2499251Slclee 			(void) printf(
2500251Slclee 			    "Partition %d is not a Solaris partition.",
25010Sstevel@tonic-gate 			    i + 1);
25020Sstevel@tonic-gate 			continue;
25030Sstevel@tonic-gate 		}
25040Sstevel@tonic-gate 
2505251Slclee 		(void) printf(E_LINE);
2506251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
25070Sstevel@tonic-gate 		return (1);
25080Sstevel@tonic-gate 	}
25090Sstevel@tonic-gate }
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate /*
25120Sstevel@tonic-gate  * pdelete
25130Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
25140Sstevel@tonic-gate  */
2515251Slclee static char
2516251Slclee pdelete(void)
25170Sstevel@tonic-gate {
25180Sstevel@tonic-gate 	char s[80];
25190Sstevel@tonic-gate 	int i, j;
25200Sstevel@tonic-gate 	char pactive;
25210Sstevel@tonic-gate 
2522251Slclee DEL1:	(void) printf(Q_LINE);
2523251Slclee 	(void) printf("Specify the partition number to delete"
25240Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
2525251Slclee 	(void) gets(s);
25260Sstevel@tonic-gate 	rm_blanks(s);
25270Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
2528251Slclee 		(void) printf(E_LINE);	/* clear error message */
25290Sstevel@tonic-gate 		return (1);
25300Sstevel@tonic-gate 	}
25310Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
25320Sstevel@tonic-gate 	if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) {
2533251Slclee 		(void) printf(E_LINE);
2534251Slclee 		(void) printf("Invalid response, retry the operation.\n");
25350Sstevel@tonic-gate 		goto DEL1;
25360Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
25370Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
25380Sstevel@tonic-gate 	}
25390Sstevel@tonic-gate 
25400Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
2541251Slclee 		(void) printf(E_LINE);
2542251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
25430Sstevel@tonic-gate 		return (-1);
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 
2546251Slclee 	(void) printf(Q_LINE);
2547251Slclee 	(void) printf("Are you sure you want to delete partition %d?"
2548251Slclee 	    " This will make all files and \n", i + 1);
2549251Slclee 	(void) printf("programs in this partition inaccessible (type"
25500Sstevel@tonic-gate 	    " \"y\" or \"n\"). ");
25510Sstevel@tonic-gate 
2552251Slclee 	(void) printf(E_LINE);
25530Sstevel@tonic-gate 	if (! yesno()) {
25540Sstevel@tonic-gate 		return (1);
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
25580Sstevel@tonic-gate 		pactive = 1;
25590Sstevel@tonic-gate 	} else {
25600Sstevel@tonic-gate 		pactive = 0;
25610Sstevel@tonic-gate 	}
25620Sstevel@tonic-gate 
25630Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
25645169Slclee 		Table[j] = Table[j + 1];
25650Sstevel@tonic-gate 	}
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate 	Table[j].systid = UNUSED;
25680Sstevel@tonic-gate 	Table[j].numsect = 0;
25690Sstevel@tonic-gate 	Table[j].relsect = 0;
25700Sstevel@tonic-gate 	Table[j].bootid = 0;
2571251Slclee 	(void) printf(E_LINE);
2572251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	if (pactive) {
25755169Slclee 		(void) printf(" This was the active partition.");
25760Sstevel@tonic-gate 	}
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	return (1);
25790Sstevel@tonic-gate }
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate /*
25820Sstevel@tonic-gate  * rm_blanks
25830Sstevel@tonic-gate  * Remove blanks from strings of user responses.
25840Sstevel@tonic-gate  */
2585251Slclee static void
2586251Slclee rm_blanks(char *s)
25870Sstevel@tonic-gate {
25880Sstevel@tonic-gate 	register int i, j;
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
25910Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
25920Sstevel@tonic-gate 			continue;
25930Sstevel@tonic-gate 		else
25940Sstevel@tonic-gate 			/* Found first non-blank character of the string */
25950Sstevel@tonic-gate 			break;
25960Sstevel@tonic-gate 	}
25970Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
25980Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
25990Sstevel@tonic-gate 			/* Reached end of string */
26000Sstevel@tonic-gate 			return;
26010Sstevel@tonic-gate 		}
26020Sstevel@tonic-gate 	}
26030Sstevel@tonic-gate }
26040Sstevel@tonic-gate 
26050Sstevel@tonic-gate /*
26060Sstevel@tonic-gate  * getcyl
26070Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
26080Sstevel@tonic-gate  * string to a decimal value.
26090Sstevel@tonic-gate  */
2610251Slclee static int
2611251Slclee getcyl(void)
26120Sstevel@tonic-gate {
26130Sstevel@tonic-gate int slen, i, j;
26140Sstevel@tonic-gate unsigned int cyl;
2615251Slclee 	(void) gets(s);
26160Sstevel@tonic-gate 	rm_blanks(s);
26170Sstevel@tonic-gate 	slen = strlen(s);
26180Sstevel@tonic-gate 	j = 1;
26190Sstevel@tonic-gate 	cyl = 0;
2620251Slclee 	for (i = slen - 1; i >= 0; i--) {
26210Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
26220Sstevel@tonic-gate 			return (-1);
26230Sstevel@tonic-gate 		}
2624251Slclee 		cyl += (j * (s[i] - '0'));
26250Sstevel@tonic-gate 		j *= 10;
26260Sstevel@tonic-gate 	}
26270Sstevel@tonic-gate 	return (cyl);
26280Sstevel@tonic-gate }
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate /*
26310Sstevel@tonic-gate  * disptbl
26320Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
26330Sstevel@tonic-gate  * of the disk used for each partition.
26340Sstevel@tonic-gate  */
2635251Slclee static void
2636251Slclee disptbl(void)
26370Sstevel@tonic-gate {
26380Sstevel@tonic-gate 	int i;
26390Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
26400Sstevel@tonic-gate 	char *stat, *type;
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
2643251Slclee 		(void) printf("WARNING: critical disk geometry information"
26445169Slclee 		    " missing!\n");
2645251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
26460Sstevel@tonic-gate 		exit(1);
26470Sstevel@tonic-gate 	}
26480Sstevel@tonic-gate 
2649251Slclee 	(void) printf(HOME);
2650251Slclee 	(void) printf(T_LINE);
2651251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
2652251Slclee 	(void) printf("             Cylinder size is %d (512 byte) blocks\n\n",
2653251Slclee 	    heads * sectors);
2654251Slclee 	(void) printf(
2655251Slclee 	    "                                               Cylinders\n");
2656251Slclee 	(void) printf(
2657251Slclee 	    "      Partition   Status    Type          Start   End   Length"
26580Sstevel@tonic-gate 	    "    %%\n");
2659251Slclee 	(void) printf(
2660251Slclee 	    "      =========   ======    ============  =====   ===   ======"
26610Sstevel@tonic-gate 	    "   ===");
26620Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
26630Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2664251Slclee 			(void) printf("\n");
2665251Slclee 			(void) printf(CLR_LIN);
26660Sstevel@tonic-gate 			continue;
26670Sstevel@tonic-gate 		}
26680Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
26695169Slclee 			stat = Actvstr;
26700Sstevel@tonic-gate 		else
26715169Slclee 			stat = NAstr;
26720Sstevel@tonic-gate 		switch (Table[i].systid) {
26730Sstevel@tonic-gate 		case UNIXOS:
26745169Slclee 			type = Ustr;
26755169Slclee 			break;
26760Sstevel@tonic-gate 		case SUNIXOS:
26775169Slclee 			type = SUstr;
26785169Slclee 			break;
26790Sstevel@tonic-gate 		case SUNIXOS2:
26805169Slclee 			type = SU2str;
26815169Slclee 			break;
26820Sstevel@tonic-gate 		case X86BOOT:
26835169Slclee 			type = X86str;
26845169Slclee 			break;
26850Sstevel@tonic-gate 		case DOSOS12:
26865169Slclee 			type = Dstr;
26875169Slclee 			break;
26880Sstevel@tonic-gate 		case DOSOS16:
26895169Slclee 			type = D16str;
26905169Slclee 			break;
26910Sstevel@tonic-gate 		case EXTDOS:
26925169Slclee 			type = EDstr;
26935169Slclee 			break;
26940Sstevel@tonic-gate 		case DOSDATA:
26955169Slclee 			type = DDstr;
26965169Slclee 			break;
26970Sstevel@tonic-gate 		case DOSHUGE:
26985169Slclee 			type = DBstr;
26995169Slclee 			break;
27000Sstevel@tonic-gate 		case PCIXOS:
27015169Slclee 			type = PCstr;
27025169Slclee 			break;
27030Sstevel@tonic-gate 		case DIAGPART:
27045169Slclee 			type = DIAGstr;
27055169Slclee 			break;
27060Sstevel@tonic-gate 		case FDISK_IFS:
27075169Slclee 			type = IFSstr;
27085169Slclee 			break;
27090Sstevel@tonic-gate 		case FDISK_AIXBOOT:
27105169Slclee 			type = AIXstr;
27115169Slclee 			break;
27120Sstevel@tonic-gate 		case FDISK_AIXDATA:
27135169Slclee 			type = AIXDstr;
27145169Slclee 			break;
27150Sstevel@tonic-gate 		case FDISK_OS2BOOT:
27165169Slclee 			type = OS2str;
27175169Slclee 			break;
27180Sstevel@tonic-gate 		case FDISK_WINDOWS:
27195169Slclee 			type = WINstr;
27205169Slclee 			break;
27210Sstevel@tonic-gate 		case FDISK_EXT_WIN:
27225169Slclee 			type = EWINstr;
27235169Slclee 			break;
27240Sstevel@tonic-gate 		case FDISK_FAT95:
27255169Slclee 			type = FAT95str;
27265169Slclee 			break;
27270Sstevel@tonic-gate 		case FDISK_EXTLBA:
27285169Slclee 			type = EXTLstr;
27295169Slclee 			break;
27300Sstevel@tonic-gate 		case FDISK_LINUX:
27315169Slclee 			type = LINUXstr;
27325169Slclee 			break;
27330Sstevel@tonic-gate 		case FDISK_CPM:
27345169Slclee 			type = CPMstr;
27355169Slclee 			break;
27360Sstevel@tonic-gate 		case FDISK_NOVELL3:
27375169Slclee 			type = NOVstr;
27385169Slclee 			break;
27390Sstevel@tonic-gate 		case FDISK_QNX4:
27405169Slclee 			type = QNXstr;
27415169Slclee 			break;
27420Sstevel@tonic-gate 		case FDISK_QNX42:
27435169Slclee 			type = QNX2str;
27445169Slclee 			break;
27450Sstevel@tonic-gate 		case FDISK_QNX43:
27465169Slclee 			type = QNX3str;
27475169Slclee 			break;
27480Sstevel@tonic-gate 		case FDISK_LINUXNAT:
27495169Slclee 			type = LINNATstr;
27505169Slclee 			break;
27510Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
27525169Slclee 			type = NTFSVOL1str;
27535169Slclee 			break;
27540Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
27555169Slclee 			type = NTFSVOL2str;
27565169Slclee 			break;
27570Sstevel@tonic-gate 		case FDISK_BSD:
27585169Slclee 			type = BSDstr;
27595169Slclee 			break;
27600Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
27615169Slclee 			type = NEXTSTEPstr;
27625169Slclee 			break;
27630Sstevel@tonic-gate 		case FDISK_BSDIFS:
27645169Slclee 			type = BSDIFSstr;
27655169Slclee 			break;
27660Sstevel@tonic-gate 		case FDISK_BSDISWAP:
27675169Slclee 			type = BSDISWAPstr;
27685169Slclee 			break;
27690Sstevel@tonic-gate 		case EFI_PMBR:
27705169Slclee 			type = EFIstr;
27715169Slclee 			break;
27720Sstevel@tonic-gate 		default:
27735169Slclee 			type = Ostr;
27745169Slclee 			break;
27750Sstevel@tonic-gate 		}
27765936Sbharding 		startcyl = lel(Table[i].relsect) /
27775936Sbharding 		    (unsigned long)(heads * sectors);
27785936Sbharding 		length = lel(Table[i].numsect) /
27795936Sbharding 		    (unsigned long)(heads * sectors);
27805936Sbharding 		if (lel(Table[i].numsect) % (unsigned long)(heads * sectors))
27810Sstevel@tonic-gate 			length++;
27820Sstevel@tonic-gate 		endcyl = startcyl + length - 1;
27830Sstevel@tonic-gate 		percent = length * 100 / Numcyl;
2784251Slclee 		if ((remainder = (length * 100 % Numcyl)) != 0) {
27850Sstevel@tonic-gate 			if ((remainder * 100 / Numcyl) > 50) {
27860Sstevel@tonic-gate 				/* round up */
27870Sstevel@tonic-gate 				percent++;
27880Sstevel@tonic-gate 			}
27890Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
27900Sstevel@tonic-gate 			/* rounded down */
27910Sstevel@tonic-gate 		}
27920Sstevel@tonic-gate 		if (percent > 100)
27930Sstevel@tonic-gate 			percent = 100;
2794251Slclee 		(void) printf(
2795251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
2796251Slclee 		    "    %3d",
2797251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
27980Sstevel@tonic-gate 	}
27990Sstevel@tonic-gate 	/* Print warning message if table is empty */
28000Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
2801251Slclee 		(void) printf(W_LINE);
2802251Slclee 		(void) printf("WARNING: no partitions are defined!");
28030Sstevel@tonic-gate 	} else {
28040Sstevel@tonic-gate 		/* Clear the warning line */
2805251Slclee 		(void) printf(W_LINE);
28060Sstevel@tonic-gate 	}
28070Sstevel@tonic-gate }
28080Sstevel@tonic-gate 
28090Sstevel@tonic-gate /*
28100Sstevel@tonic-gate  * print_Table
28110Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
28120Sstevel@tonic-gate  * the selected disk device.
28130Sstevel@tonic-gate  */
2814251Slclee static void
2815251Slclee print_Table(void)
2816251Slclee {
28170Sstevel@tonic-gate 	int i;
28180Sstevel@tonic-gate 
2819251Slclee 	(void) fprintf(stderr,
28200Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
28210Sstevel@tonic-gate 	    "   NUMSECT\n");
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2824251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
2825251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
2826251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
2827251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
28285169Slclee 		(void) fprintf(stderr, "%-8d ",
28295169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
28300Sstevel@tonic-gate 
2831251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
2832251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
28335169Slclee 		(void) fprintf(stderr, "%-8d ",
28345169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
2835251Slclee 		(void) fprintf(stderr, "%-9d ", lel(Table[i].relsect));
2836251Slclee 		(void) fprintf(stderr, "%-9d\n", lel(Table[i].numsect));
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate 	}
28390Sstevel@tonic-gate }
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate /*
28420Sstevel@tonic-gate  * copy_Table_to_Old_Table
28430Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
28440Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
28450Sstevel@tonic-gate  * ones compared when determining if Table has changed.
28460Sstevel@tonic-gate  */
2847251Slclee static void
2848251Slclee copy_Table_to_Old_Table(void)
28490Sstevel@tonic-gate {
28500Sstevel@tonic-gate 	int i;
28510Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
28525169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
28530Sstevel@tonic-gate 	}
28540Sstevel@tonic-gate }
28550Sstevel@tonic-gate 
28560Sstevel@tonic-gate /*
28570Sstevel@tonic-gate  * nulltbl
28580Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
28590Sstevel@tonic-gate  * fdisk table.
28600Sstevel@tonic-gate  */
2861251Slclee static void
2862251Slclee nulltbl(void)
28630Sstevel@tonic-gate {
28640Sstevel@tonic-gate 	int i;
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
28675169Slclee 		Table[i].systid = UNUSED;
28685169Slclee 		Table[i].numsect = lel(UNUSED);
28695169Slclee 		Table[i].relsect = lel(UNUSED);
28705169Slclee 		Table[i].bootid = 0;
28710Sstevel@tonic-gate 	}
28720Sstevel@tonic-gate }
28730Sstevel@tonic-gate 
28740Sstevel@tonic-gate /*
28750Sstevel@tonic-gate  * copy_Bootblk_to_Table
28760Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
28770Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
28780Sstevel@tonic-gate  */
2879251Slclee static void
2880251Slclee copy_Bootblk_to_Table(void)
28810Sstevel@tonic-gate {
28820Sstevel@tonic-gate 	int i, j;
28830Sstevel@tonic-gate 	char *bootptr;
28840Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
28850Sstevel@tonic-gate 
28860Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
2887251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
28880Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
28890Sstevel@tonic-gate 	if (les(Bootblk->signature) != MBB_MAGIC)  {
28900Sstevel@tonic-gate 		/* Signature is missing */
28910Sstevel@tonic-gate 		nulltbl();
2892251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
28930Sstevel@tonic-gate 		return;
28940Sstevel@tonic-gate 	}
28950Sstevel@tonic-gate 	/*
28960Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
28970Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
28980Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
28990Sstevel@tonic-gate 	 * those that are valid.
29000Sstevel@tonic-gate 	 */
29010Sstevel@tonic-gate 	j = 0;
29020Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
29030Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
29040Sstevel@tonic-gate 			/* Null entry */
29050Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29060Sstevel@tonic-gate 		} else {
2907251Slclee 			fill_ipart(bootptr, &Table[j]);
29080Sstevel@tonic-gate 			j++;
29090Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29100Sstevel@tonic-gate 		}
29110Sstevel@tonic-gate 	}
29120Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
29130Sstevel@tonic-gate 		Table[i].systid = UNUSED;
29140Sstevel@tonic-gate 		Table[i].numsect = lel(UNUSED);
29150Sstevel@tonic-gate 		Table[i].relsect = lel(UNUSED);
29160Sstevel@tonic-gate 		Table[i].bootid = 0;
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	}
29190Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
2920251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
29210Sstevel@tonic-gate 	copy_Table_to_Bootblk();
29220Sstevel@tonic-gate }
29230Sstevel@tonic-gate 
29240Sstevel@tonic-gate /*
29250Sstevel@tonic-gate  * fill_ipart
29260Sstevel@tonic-gate  * Initialize ipart structure values.
29270Sstevel@tonic-gate  */
2928251Slclee static void
29290Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
29300Sstevel@tonic-gate {
29310Sstevel@tonic-gate #ifdef sparc
29320Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
2933251Slclee 	partp->bootid	= getbyte(&bootptr);
2934251Slclee 	partp->beghead	= getbyte(&bootptr);
2935251Slclee 	partp->begsect	= getbyte(&bootptr);
2936251Slclee 	partp->begcyl	= getbyte(&bootptr);
2937251Slclee 	partp->systid	= getbyte(&bootptr);
2938251Slclee 	partp->endhead	= getbyte(&bootptr);
2939251Slclee 	partp->endsect	= getbyte(&bootptr);
2940251Slclee 	partp->endcyl	= getbyte(&bootptr);
2941251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
2942251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
29430Sstevel@tonic-gate #else
29440Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
29450Sstevel@tonic-gate #endif
29460Sstevel@tonic-gate }
29470Sstevel@tonic-gate 
29480Sstevel@tonic-gate /*
2949251Slclee  * getbyte, getlong
29500Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
29510Sstevel@tonic-gate  */
29520Sstevel@tonic-gate #ifdef sparc
2953251Slclee uchar_t
2954251Slclee getbyte(char **bp)
29550Sstevel@tonic-gate {
2956251Slclee 	uchar_t	b;
2957251Slclee 
2958251Slclee 	b = (uchar_t)**bp;
29590Sstevel@tonic-gate 	*bp = *bp + 1;
29600Sstevel@tonic-gate 	return (b);
29610Sstevel@tonic-gate }
29620Sstevel@tonic-gate 
2963251Slclee uint32_t
2964251Slclee getlong(char **bp)
29650Sstevel@tonic-gate {
2966251Slclee 	int32_t	b, bh, bl;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
29690Sstevel@tonic-gate 	*bp += 2;
29700Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
29710Sstevel@tonic-gate 	*bp += 2;
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate 	b = (bh << 16) | bl;
2974251Slclee 	return ((uint32_t)b);
29750Sstevel@tonic-gate }
29760Sstevel@tonic-gate #endif
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate /*
29790Sstevel@tonic-gate  * copy_Table_to_Bootblk
29800Sstevel@tonic-gate  * Copy the table into the 512 boot record. Note that the unused
29810Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
29820Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
29830Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
29840Sstevel@tonic-gate  */
2985251Slclee static void
2986251Slclee copy_Table_to_Bootblk(void)
29870Sstevel@tonic-gate {
29880Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
29910Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
29920Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
29930Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
29945169Slclee 		if (tbl_ptr->systid == UNUSED)
29955169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
29965169Slclee 		else
29975169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
29980Sstevel@tonic-gate 	}
29990Sstevel@tonic-gate 	Bootblk->signature = les(MBB_MAGIC);
30000Sstevel@tonic-gate }
30010Sstevel@tonic-gate 
30020Sstevel@tonic-gate /*
30030Sstevel@tonic-gate  * TableChanged
30040Sstevel@tonic-gate  * 	Check for any changes in the partition table.
30050Sstevel@tonic-gate  */
3006251Slclee static int
3007251Slclee TableChanged(void)
30080Sstevel@tonic-gate {
30090Sstevel@tonic-gate 	int i, changed;
30100Sstevel@tonic-gate 
30110Sstevel@tonic-gate 	changed = 0;
30120Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
30135169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
30145169Slclee 			/* Partition table changed, write back to disk */
30155169Slclee 			changed = 1;
30165169Slclee 		}
30170Sstevel@tonic-gate 	}
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 	return (changed);
30200Sstevel@tonic-gate }
30210Sstevel@tonic-gate 
30220Sstevel@tonic-gate /*
30230Sstevel@tonic-gate  * ffile_write
30240Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
30250Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
30260Sstevel@tonic-gate  */
3027251Slclee static void
3028251Slclee ffile_write(char *file)
30290Sstevel@tonic-gate {
30300Sstevel@tonic-gate 	register int	i;
30310Sstevel@tonic-gate 	FILE *fp;
30320Sstevel@tonic-gate 
30330Sstevel@tonic-gate 	/*
30340Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
30350Sstevel@tonic-gate 	 * Open file and write it.
30360Sstevel@tonic-gate 	 */
30370Sstevel@tonic-gate 	if (file != (char *)stdout) {
30385169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
30395169Slclee 			(void) fprintf(stderr,
30405169Slclee 			    "fdisk: Cannot open output file %s.\n",
30415169Slclee 			    file);
30425169Slclee 			exit(1);
30435169Slclee 		}
30440Sstevel@tonic-gate 	}
30450Sstevel@tonic-gate 	else
30465169Slclee 		fp = stdout;
30470Sstevel@tonic-gate 
30480Sstevel@tonic-gate 	/*
30490Sstevel@tonic-gate 	 * Write the fdisk table information
30500Sstevel@tonic-gate 	 */
3051251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
3052251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
3053251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3054251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
3055251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
3056251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
3057251Slclee 	(void) fprintf(fp, "*\n");
30580Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
30590Sstevel@tonic-gate 	if (v_flag) {
3060251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
3061251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3062251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
3063251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
3064251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
3065251Slclee 		(void) fprintf(fp, "*\n");
30660Sstevel@tonic-gate 	}
3067251Slclee 	(void) fprintf(fp, "* systid:\n");
3068251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
3069251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
3070251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
3071251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
3072251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
3073251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
3074251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
3075251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
3076251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
3077251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
3078251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
3079251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
3080251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
3081251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
3082251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
3083251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
3084251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
3085251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
3086251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
3087251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
3088251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
3089251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
3090251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
3091251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
3092251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
3093251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
3094251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
3095251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
3096251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
3097251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
3098251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
3099251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
3100251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
3101251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
3102251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
3103251Slclee 	(void) fprintf(fp, "*\n");
3104251Slclee 	(void) fprintf(fp,
31050Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
31060Sstevel@tonic-gate 	    "    Rsect    Numsect\n");
31070Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31080Sstevel@tonic-gate 		if (Table[i].systid != UNUSED)
3109251Slclee 			(void) fprintf(fp,
31100Sstevel@tonic-gate 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-8d"
31110Sstevel@tonic-gate 			    " %-8d\n",
31120Sstevel@tonic-gate 			    Table[i].systid,
31130Sstevel@tonic-gate 			    Table[i].bootid,
31140Sstevel@tonic-gate 			    Table[i].beghead,
31150Sstevel@tonic-gate 			    Table[i].begsect & 0x3f,
31160Sstevel@tonic-gate 			    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
31175169Slclee 			    0xc0) << 2)),
31180Sstevel@tonic-gate 			    Table[i].endhead,
31190Sstevel@tonic-gate 			    Table[i].endsect & 0x3f,
31200Sstevel@tonic-gate 			    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
31215169Slclee 			    0xc0) << 2)),
31220Sstevel@tonic-gate 			    lel(Table[i].relsect),
31230Sstevel@tonic-gate 			    lel(Table[i].numsect));
31240Sstevel@tonic-gate 	}
31250Sstevel@tonic-gate 	if (fp != stdout)
3126251Slclee 		(void) fclose(fp);
31270Sstevel@tonic-gate }
31280Sstevel@tonic-gate 
31290Sstevel@tonic-gate /*
31300Sstevel@tonic-gate  * fix_slice
31310Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
31320Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
31330Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
31340Sstevel@tonic-gate  */
3135251Slclee static void
3136251Slclee fix_slice(void)
31370Sstevel@tonic-gate {
31380Sstevel@tonic-gate 	int	i;
31390Sstevel@tonic-gate 	int	numsect;
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	if (io_image) {
3142251Slclee 		return;
31430Sstevel@tonic-gate 	}
31440Sstevel@tonic-gate 
31450Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31460Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
31470Sstevel@tonic-gate 			/*
31480Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
31490Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
31500Sstevel@tonic-gate 			 * the partition.
31510Sstevel@tonic-gate 			 */
31520Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
31530Sstevel@tonic-gate 			break;
31540Sstevel@tonic-gate 		}
31550Sstevel@tonic-gate 	}
31560Sstevel@tonic-gate 
31570Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
31580Sstevel@tonic-gate 		if (!io_nifdisk) {
31590Sstevel@tonic-gate 			(void) fprintf(stderr,
31600Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
31610Sstevel@tonic-gate 			    " checked.\n");
31620Sstevel@tonic-gate 		}
3163251Slclee 		return;
31640Sstevel@tonic-gate 	}
31650Sstevel@tonic-gate 
3166251Slclee 	if (readvtoc() != VTOC_OK) {
31670Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
31685169Slclee 	}
31695169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
31705169Slclee 		/* Special case for slice two (entire disk) */
31715169Slclee 		if (i == 2) {
31725169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
31735169Slclee 				(void) fprintf(stderr,
31745169Slclee 				    "slice %d starts at %ld, is not at"
31755169Slclee 				    " start of partition",
31765169Slclee 				    i, disk_vtoc.v_part[i].p_start);
31775169Slclee 				if (!io_nifdisk) {
31785169Slclee 					(void) printf(" adjust ?:");
31795169Slclee 					if (yesno())
31800Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
31815169Slclee 				} else {
31825169Slclee 					disk_vtoc.v_part[i].p_start = 0;
31835169Slclee 					(void) fprintf(stderr, " adjusted!\n");
31840Sstevel@tonic-gate 				}
31855169Slclee 
31865169Slclee 			}
31875169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
31885169Slclee 				(void) fprintf(stderr,
31895169Slclee 				    "slice %d size %ld does not cover"
31905169Slclee 				    " complete partition",
31915169Slclee 				    i, disk_vtoc.v_part[i].p_size);
31925169Slclee 				if (!io_nifdisk) {
31935169Slclee 					(void) printf(" adjust ?:");
31945169Slclee 					if (yesno())
31950Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
31960Sstevel@tonic-gate 						    numsect;
31975169Slclee 				} else {
31985169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
31995169Slclee 					(void) fprintf(stderr, " adjusted!\n");
32000Sstevel@tonic-gate 				}
32015169Slclee 			}
32025169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
32035169Slclee 				(void) fprintf(stderr,
32045169Slclee 				    "slice %d tag was %d should be %d",
32055169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
32065169Slclee 				    V_BACKUP);
32075169Slclee 				if (!io_nifdisk) {
3208251Slclee 					(void) printf(" fix ?:");
32095169Slclee 					if (yesno())
32100Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
32110Sstevel@tonic-gate 						    V_BACKUP;
32125169Slclee 				} else {
32130Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
32140Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
32150Sstevel@tonic-gate 				}
32165169Slclee 			}
32175169Slclee 			continue;
32185169Slclee 		}
32195169Slclee 		if (io_ADJT) {
32205169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
32215169Slclee 			    disk_vtoc.v_part[i].p_start +
32225169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
32235169Slclee 				(void) fprintf(stderr,
32245169Slclee 				    "slice %d (start %ld, end %ld)"
32255169Slclee 				    " is larger than the partition",
32265169Slclee 				    i, disk_vtoc.v_part[i].p_start,
32275169Slclee 				    disk_vtoc.v_part[i].p_start +
32285169Slclee 				    disk_vtoc.v_part[i].p_size);
32295169Slclee 				if (!io_nifdisk) {
32305169Slclee 					(void) printf(" remove ?:");
32315169Slclee 					if (yesno()) {
32320Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
32330Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
32340Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
32350Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
32360Sstevel@tonic-gate 					}
32370Sstevel@tonic-gate 				} else {
32385169Slclee 					disk_vtoc.v_part[i].p_size = 0;
32395169Slclee 					disk_vtoc.v_part[i].p_start = 0;
32405169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
32415169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
32420Sstevel@tonic-gate 					(void) fprintf(stderr,
32435169Slclee 					    " removed!\n");
32445169Slclee 				}
32455169Slclee 			}
32465169Slclee 			continue;
32475169Slclee 		}
32485169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
32495169Slclee 			(void) fprintf(stderr,
32505169Slclee 			    "slice %d (start %ld) is larger than the partition",
32515169Slclee 			    i, disk_vtoc.v_part[i].p_start);
32525169Slclee 			if (!io_nifdisk) {
32535169Slclee 				(void) printf(" remove ?:");
32545169Slclee 				if (yesno()) {
32555169Slclee 					disk_vtoc.v_part[i].p_size = 0;
32565169Slclee 					disk_vtoc.v_part[i].p_start = 0;
32575169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
32585169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
32590Sstevel@tonic-gate 				}
32605169Slclee 			} else {
32615169Slclee 				disk_vtoc.v_part[i].p_size = 0;
32625169Slclee 				disk_vtoc.v_part[i].p_start = 0;
32635169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
32645169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
32655169Slclee 				(void) fprintf(stderr,
32665169Slclee 				" removed!\n");
32675169Slclee 			}
32685169Slclee 		} else if (disk_vtoc.v_part[i].p_start
32695169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
32705169Slclee 			(void) fprintf(stderr,
32715169Slclee 			    "slice %d (end %ld) is larger"
32725169Slclee 			    " than the partition",
32735169Slclee 			    i,
32745169Slclee 			    disk_vtoc.v_part[i].p_start +
32755169Slclee 			    disk_vtoc.v_part[i].p_size);
32765169Slclee 			if (!io_nifdisk) {
32775169Slclee 				(void) printf(" adjust ?:");
32785169Slclee 				if (yesno()) {
32795169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
32805169Slclee 				}
32815169Slclee 			} else {
32825169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
32835169Slclee 				(void) fprintf(stderr, " adjusted!\n");
32840Sstevel@tonic-gate 			}
32850Sstevel@tonic-gate 		}
32860Sstevel@tonic-gate 	}
32870Sstevel@tonic-gate #if 1		/* bh for now */
32880Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
32890Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
32900Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
32910Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
32920Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
32930Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
32940Sstevel@tonic-gate #endif
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
32970Sstevel@tonic-gate 	if (!io_readonly)
3298251Slclee 		(void) writevtoc();
32990Sstevel@tonic-gate }
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate /*
33020Sstevel@tonic-gate  * yesno
33030Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
33040Sstevel@tonic-gate  */
33050Sstevel@tonic-gate 
3306251Slclee static int
3307251Slclee yesno(void)
33080Sstevel@tonic-gate {
33090Sstevel@tonic-gate 	char	s[80];
33100Sstevel@tonic-gate 
33110Sstevel@tonic-gate 	for (;;) {
3312251Slclee 		(void) gets(s);
33130Sstevel@tonic-gate 		rm_blanks(s);
33140Sstevel@tonic-gate 		if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) {
3315251Slclee 			(void) printf(E_LINE);
3316251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
33170Sstevel@tonic-gate 			continue;
33180Sstevel@tonic-gate 		}
33190Sstevel@tonic-gate 		if (s[0] == 'y')
33200Sstevel@tonic-gate 			return (1);
33210Sstevel@tonic-gate 		else
33220Sstevel@tonic-gate 			return (0);
33230Sstevel@tonic-gate 	}
33240Sstevel@tonic-gate }
33250Sstevel@tonic-gate 
33260Sstevel@tonic-gate /*
33270Sstevel@tonic-gate  * readvtoc
33280Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
33290Sstevel@tonic-gate  */
3330251Slclee static int
3331251Slclee readvtoc(void)
33320Sstevel@tonic-gate {
33330Sstevel@tonic-gate 	int	i;
33340Sstevel@tonic-gate 	int	retval = VTOC_OK;
33350Sstevel@tonic-gate 
33360Sstevel@tonic-gate 	if ((i = read_vtoc(Dev, &disk_vtoc)) < VTOC_OK) {
33370Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33380Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
33390Sstevel@tonic-gate 			vt_inval++;
33400Sstevel@tonic-gate 			retval = VTOC_INVAL;
33410Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33420Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
33435169Slclee 			    "GPT\n");
33440Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33450Sstevel@tonic-gate 		} else {
33460Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
33470Sstevel@tonic-gate 			retval = VTOC_RWERR;
33480Sstevel@tonic-gate 		}
33490Sstevel@tonic-gate 	}
33500Sstevel@tonic-gate 	return (retval);
33510Sstevel@tonic-gate }
33520Sstevel@tonic-gate 
33530Sstevel@tonic-gate /*
33540Sstevel@tonic-gate  * writevtoc
33550Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
33560Sstevel@tonic-gate  */
3357251Slclee static int
3358251Slclee writevtoc(void)
33590Sstevel@tonic-gate {
33600Sstevel@tonic-gate 	int	i;
33610Sstevel@tonic-gate 	int	retval = 0;
33620Sstevel@tonic-gate 
33630Sstevel@tonic-gate 	if ((i = write_vtoc(Dev, &disk_vtoc)) != 0) {
33640Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33650Sstevel@tonic-gate 			(void) fprintf(stderr,
33660Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
33670Sstevel@tonic-gate 			retval = VTOC_INVAL;
33680Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33690Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
33705169Slclee 			    "GPT\n");
33710Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33720Sstevel@tonic-gate 		} else {
33730Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
33740Sstevel@tonic-gate 			retval = VTOC_RWERR;
33750Sstevel@tonic-gate 		}
33760Sstevel@tonic-gate 	}
33770Sstevel@tonic-gate 	return (retval);
33780Sstevel@tonic-gate }
33790Sstevel@tonic-gate 
33800Sstevel@tonic-gate /*
33810Sstevel@tonic-gate  * efi_ioctl
33820Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
33830Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
33840Sstevel@tonic-gate  */
33850Sstevel@tonic-gate static int
33860Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
33870Sstevel@tonic-gate {
33880Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
33890Sstevel@tonic-gate 	int error;
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
33920Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
33930Sstevel@tonic-gate 
33940Sstevel@tonic-gate 	return (error);
33950Sstevel@tonic-gate }
33960Sstevel@tonic-gate 
33970Sstevel@tonic-gate /*
33980Sstevel@tonic-gate  * clear_efi
33990Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
34000Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
34010Sstevel@tonic-gate  */
3402251Slclee static int
3403251Slclee clear_efi(void)
34040Sstevel@tonic-gate {
34050Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
34060Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
34070Sstevel@tonic-gate 
34080Sstevel@tonic-gate 	/*
34090Sstevel@tonic-gate 	 * see if we can read the EFI label
34100Sstevel@tonic-gate 	 */
34110Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
34120Sstevel@tonic-gate 		return (VT_ERROR);
34130Sstevel@tonic-gate 	}
34140Sstevel@tonic-gate 
34150Sstevel@tonic-gate 	/*
34160Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
34170Sstevel@tonic-gate 	 */
34180Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
34190Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
34220Sstevel@tonic-gate 		return (VT_ERROR);
34230Sstevel@tonic-gate 	}
34240Sstevel@tonic-gate 
34250Sstevel@tonic-gate 	/*
34260Sstevel@tonic-gate 	 * clear the primary label
34270Sstevel@tonic-gate 	 */
34280Sstevel@tonic-gate 	if (io_debug) {
3429251Slclee 		(void) fprintf(stderr,
3430251Slclee 		    "\tClearing primary EFI label at block %lld\n",
3431251Slclee 		    dk_ioc.dki_lba);
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 
34340Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34350Sstevel@tonic-gate 		free(dk_ioc.dki_data);
34360Sstevel@tonic-gate 		switch (errno) {
34370Sstevel@tonic-gate 			case EIO:
34380Sstevel@tonic-gate 				return (VT_EIO);
34390Sstevel@tonic-gate 			case EINVAL:
34400Sstevel@tonic-gate 				return (VT_EINVAL);
34410Sstevel@tonic-gate 			default:
34420Sstevel@tonic-gate 				return (VT_ERROR);
34430Sstevel@tonic-gate 		}
34440Sstevel@tonic-gate 	}
34450Sstevel@tonic-gate 
34460Sstevel@tonic-gate 	/*
34470Sstevel@tonic-gate 	 * clear the backup partition table
34480Sstevel@tonic-gate 	 */
34490Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
34500Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
34510Sstevel@tonic-gate 	dk_ioc.dki_data++;
34520Sstevel@tonic-gate 	if (io_debug) {
3453251Slclee 		(void) fprintf(stderr,
3454251Slclee 		    "\tClearing backup partition table at block %lld\n",
3455251Slclee 		    dk_ioc.dki_lba);
34560Sstevel@tonic-gate 	}
34570Sstevel@tonic-gate 
34580Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34590Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
34605169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
34615169Slclee 		    errno);
34620Sstevel@tonic-gate 	}
34630Sstevel@tonic-gate 
34640Sstevel@tonic-gate 	/*
34650Sstevel@tonic-gate 	 * clear the backup label
34660Sstevel@tonic-gate 	 */
34670Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
34680Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
34690Sstevel@tonic-gate 	dk_ioc.dki_data--;
34700Sstevel@tonic-gate 	if (io_debug) {
3471251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
3472251Slclee 		    "%lld\n", dk_ioc.dki_lba);
34730Sstevel@tonic-gate 	}
34740Sstevel@tonic-gate 
34750Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
3476251Slclee 		(void) fprintf(stderr,
3477251Slclee 		    "\tUnable to clear backup EFI label at "
3478251Slclee 		    "block %llu; errno %d\n",
3479251Slclee 		    efi_vtoc->efi_last_lba,
3480251Slclee 		    errno);
34810Sstevel@tonic-gate 	}
34820Sstevel@tonic-gate 
34830Sstevel@tonic-gate 	free(dk_ioc.dki_data);
34840Sstevel@tonic-gate 	efi_free(efi_vtoc);
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 	return (0);
34870Sstevel@tonic-gate }
34880Sstevel@tonic-gate 
34890Sstevel@tonic-gate /*
34900Sstevel@tonic-gate  * clear_vtoc
34910Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
34920Sstevel@tonic-gate  *      device.
34930Sstevel@tonic-gate  */
3494251Slclee static void
34950Sstevel@tonic-gate clear_vtoc(int table, int part)
34960Sstevel@tonic-gate {
34970Sstevel@tonic-gate 	struct ipart *clr_table;
34980Sstevel@tonic-gate 	struct dk_label disk_label;
3499*6549Sbharding 	int pcyl, ncyl, count, bytes;
3500*6549Sbharding 	uint_t backup_block, solaris_offset;
3501*6549Sbharding 	off_t seek_byte;
35020Sstevel@tonic-gate 
35030Sstevel@tonic-gate #ifdef DEBUG
35040Sstevel@tonic-gate 	struct dk_label	read_label;
35050Sstevel@tonic-gate #endif /* DEBUG */
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 	if (table == OLD) {
35080Sstevel@tonic-gate 		clr_table = &Old_Table[part];
35090Sstevel@tonic-gate 	} else {
35100Sstevel@tonic-gate 		clr_table = &Table[part];
35110Sstevel@tonic-gate 	}
35120Sstevel@tonic-gate 
3513251Slclee 	(void) memset(&disk_label, 0, sizeof (struct dk_label));
35140Sstevel@tonic-gate 
3515*6549Sbharding 	seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz;
35160Sstevel@tonic-gate 
35170Sstevel@tonic-gate 	if (io_debug) {
3518*6549Sbharding 		(void) fprintf(stderr,
3519*6549Sbharding 		    "\tClearing primary VTOC at byte %llu (block %llu)\n",
3520*6549Sbharding 		    (uint64_t)seek_byte,
3521*6549Sbharding 		    (uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET));
35220Sstevel@tonic-gate 	}
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3525251Slclee 		(void) fprintf(stderr,
3526*6549Sbharding 		    "\tError seeking to primary label at byte %llu\n",
3527*6549Sbharding 		    (uint64_t)seek_byte);
3528251Slclee 		return;
35290Sstevel@tonic-gate 	}
35300Sstevel@tonic-gate 
35310Sstevel@tonic-gate 	bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3534251Slclee 		(void) fprintf(stderr,
3535251Slclee 		    "\tWarning: only %d bytes written to clear primary VTOC!\n",
3536251Slclee 		    bytes);
35370Sstevel@tonic-gate 	}
35380Sstevel@tonic-gate 
35390Sstevel@tonic-gate #ifdef DEBUG
35400Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3541251Slclee 		(void) fprintf(stderr,
3542*6549Sbharding 		    "DEBUG: Error seeking to primary label at byte %llu\n",
3543*6549Sbharding 		    (uint64_t)seek_byte);
3544251Slclee 		return;
35450Sstevel@tonic-gate 	} else {
3546*6549Sbharding 		(void) fprintf(stderr,
3547*6549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
3548*6549Sbharding 		    (uint64_t)seek_byte);
35490Sstevel@tonic-gate 	}
35500Sstevel@tonic-gate 
35510Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3554251Slclee 		(void) fprintf(stderr,
3555251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
35560Sstevel@tonic-gate 		    bytes);
35570Sstevel@tonic-gate 	}
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3560251Slclee 		(void) fprintf(stderr,
3561251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
35620Sstevel@tonic-gate 	} else {
3563251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
35640Sstevel@tonic-gate 		    "read_label\n");
35650Sstevel@tonic-gate 	}
35660Sstevel@tonic-gate #endif /* DEBUG */
35670Sstevel@tonic-gate 
35680Sstevel@tonic-gate 	/* Clear backup label */
35690Sstevel@tonic-gate 	pcyl = lel(clr_table->numsect) / (heads * sectors);
35700Sstevel@tonic-gate 	solaris_offset = lel(clr_table->relsect);
35710Sstevel@tonic-gate 	ncyl = pcyl - acyl;
35720Sstevel@tonic-gate 
35730Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
35740Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
3577*6549Sbharding 		seek_byte = (off_t)(solaris_offset + backup_block) * 512;
35780Sstevel@tonic-gate 
35790Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3580251Slclee 			(void) fprintf(stderr,
3581*6549Sbharding 			    "\tError seeking to backup label at byte %llu on "
3582*6549Sbharding 			    "%s.\n", (uint64_t)seek_byte, Dfltdev);
3583251Slclee 			return;
35840Sstevel@tonic-gate 		}
35850Sstevel@tonic-gate 
35860Sstevel@tonic-gate 		if (io_debug) {
3587251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
3588*6549Sbharding 			    " byte %llu (block %llu)\n",
3589*6549Sbharding 			    (uint64_t)seek_byte,
3590*6549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
35910Sstevel@tonic-gate 		}
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 		bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35940Sstevel@tonic-gate 
35950Sstevel@tonic-gate 		if (bytes != sizeof (struct dk_label)) {
3596251Slclee 			(void) fprintf(stderr,
3597251Slclee 			    "\t\tWarning: only %d bytes written to "
3598*6549Sbharding 			    "clear backup VTOC at block %llu!\n", bytes,
3599*6549Sbharding 			    (uint64_t)(solaris_offset + backup_block));
36000Sstevel@tonic-gate 		}
36010Sstevel@tonic-gate 
36020Sstevel@tonic-gate #ifdef DEBUG
36030Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3604251Slclee 		(void) fprintf(stderr,
3605*6549Sbharding 		    "DEBUG: Error seeking to backup label at byte %llu\n",
3606*6549Sbharding 		    (uint64_t)seek_byte);
3607251Slclee 		return;
36080Sstevel@tonic-gate 	} else {
3609*6549Sbharding 		(void) fprintf(stderr,
3610*6549Sbharding 		    "DEBUG: Successful lseek() to byte %llu\n",
3611*6549Sbharding 		    (uint64_t)seek_byte);
36120Sstevel@tonic-gate 	}
36130Sstevel@tonic-gate 
36140Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
36150Sstevel@tonic-gate 
36160Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3617251Slclee 		(void) fprintf(stderr,
3618251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
3619251Slclee 		    bytes);
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate 
36220Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3623251Slclee 		(void) fprintf(stderr,
3624251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
36250Sstevel@tonic-gate 	} else {
3626251Slclee 		(void) fprintf(stderr,
3627251Slclee 		    "DEBUG: Good compare of disk_label and backup "
36280Sstevel@tonic-gate 		    "read_label\n");
36290Sstevel@tonic-gate 	}
36300Sstevel@tonic-gate #endif /* DEBUG */
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 		backup_block += 2;
36330Sstevel@tonic-gate 	}
36340Sstevel@tonic-gate }
36350Sstevel@tonic-gate 
36360Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
36370Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
36380Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
36390Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
36400Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
36410Sstevel@tonic-gate 
36420Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
36430Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
36440Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
36450Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
36480Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
36510Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
36520Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36530Sstevel@tonic-gate 
36540Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
36550Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
36560Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36570Sstevel@tonic-gate 
36580Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
36590Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
36600Sstevel@tonic-gate 
36610Sstevel@tonic-gate /*
36620Sstevel@tonic-gate  *  lecture_and_query
36630Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
36640Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
36650Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
36660Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
36670Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
36680Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
36690Sstevel@tonic-gate  */
3670251Slclee static int
36710Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
36720Sstevel@tonic-gate {
36730Sstevel@tonic-gate 	if (io_nifdisk)
36740Sstevel@tonic-gate 		return (0);
36750Sstevel@tonic-gate 
3676251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
3677251Slclee 	(void) fprintf(stderr, "%s", warning);
3678251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
3679251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
36800Sstevel@tonic-gate 
36810Sstevel@tonic-gate 	return (yesno());
36820Sstevel@tonic-gate }
36830Sstevel@tonic-gate 
3684251Slclee static void
36850Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
36860Sstevel@tonic-gate {
36870Sstevel@tonic-gate 	struct vtoc v;
36880Sstevel@tonic-gate 	struct dk_geom d;
36890Sstevel@tonic-gate 	struct part_info pi;
36900Sstevel@tonic-gate 	long totsize;
36910Sstevel@tonic-gate 	int idx = -1;
36920Sstevel@tonic-gate 
36930Sstevel@tonic-gate 	/*
36940Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
36950Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
36960Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
36970Sstevel@tonic-gate 	 */
36980Sstevel@tonic-gate 	if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
36990Sstevel@tonic-gate 		if (pi.p_start != 0) {
37000Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
37010Sstevel@tonic-gate 			    devname)) {
37020Sstevel@tonic-gate 				(void) close(fd);
37030Sstevel@tonic-gate 				exit(1);
37040Sstevel@tonic-gate 			}
37050Sstevel@tonic-gate 		}
37060Sstevel@tonic-gate 	} else {
37070Sstevel@tonic-gate 		if ((idx = read_vtoc(fd, &v)) < 0) {
37080Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
37090Sstevel@tonic-gate 			    devname)) {
37100Sstevel@tonic-gate 				(void) close(fd);
37110Sstevel@tonic-gate 				exit(1);
37120Sstevel@tonic-gate 			}
37130Sstevel@tonic-gate 			return;
37140Sstevel@tonic-gate 		}
37150Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
37160Sstevel@tonic-gate 			perror(devname);
37170Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
37180Sstevel@tonic-gate 			    devname)) {
37190Sstevel@tonic-gate 				(void) close(fd);
37200Sstevel@tonic-gate 				exit(1);
37210Sstevel@tonic-gate 			}
37220Sstevel@tonic-gate 			return;
37230Sstevel@tonic-gate 		}
37240Sstevel@tonic-gate 		totsize = d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
37250Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
37260Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
37270Sstevel@tonic-gate 			    devname)) {
37280Sstevel@tonic-gate 				(void) close(fd);
37290Sstevel@tonic-gate 				exit(1);
37300Sstevel@tonic-gate 			}
37310Sstevel@tonic-gate 		}
37320Sstevel@tonic-gate 	}
37330Sstevel@tonic-gate }
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate /*
37370Sstevel@tonic-gate  * get_node
37380Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
37390Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
37400Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
37410Sstevel@tonic-gate  */
37420Sstevel@tonic-gate static char *
37430Sstevel@tonic-gate get_node(char *devname)
37440Sstevel@tonic-gate {
37450Sstevel@tonic-gate 	char *node;
37460Sstevel@tonic-gate 	struct stat statbuf;
37470Sstevel@tonic-gate 	size_t space;
37480Sstevel@tonic-gate 
37490Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
37500Sstevel@tonic-gate 	if (io_image)
37510Sstevel@tonic-gate 		return (devname);
37520Sstevel@tonic-gate 
37530Sstevel@tonic-gate 	node = devname;
37540Sstevel@tonic-gate 
37550Sstevel@tonic-gate 	/* Try the node as provided first */
37560Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
37570Sstevel@tonic-gate 		/*
37580Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
37590Sstevel@tonic-gate 		 * default path and try again.
37600Sstevel@tonic-gate 		 */
37610Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
3764251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
37650Sstevel@tonic-gate 			    "for device node.\n");
37660Sstevel@tonic-gate 			exit(1);
37670Sstevel@tonic-gate 		}
37680Sstevel@tonic-gate 
37690Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
37700Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
37710Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
37720Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
37730Sstevel@tonic-gate 
37740Sstevel@tonic-gate 		/* Try to stat it again */
37750Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
37760Sstevel@tonic-gate 			/* Failed all options, give up */
3777251Slclee 			(void) fprintf(stderr,
3778251Slclee 			    "fdisk: Cannot stat device %s.\n",
37790Sstevel@tonic-gate 			    devname);
37800Sstevel@tonic-gate 			exit(1);
37810Sstevel@tonic-gate 		}
37820Sstevel@tonic-gate 	}
37830Sstevel@tonic-gate 
37840Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
37850Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
3786251Slclee 		(void) fprintf(stderr,
3787251Slclee 		    "fdisk: %s must be a raw device.\n", node);
37880Sstevel@tonic-gate 		exit(1);
37890Sstevel@tonic-gate 	}
37900Sstevel@tonic-gate 
37910Sstevel@tonic-gate 	return (node);
37920Sstevel@tonic-gate }
3793