xref: /onnv-gate/usr/src/cmd/fdisk/fdisk.c (revision 6478:41e4f78423bf)
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)
1180Sstevel@tonic-gate #define	VTOC_OFFSET	512
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. */
246251Slclee static int	io_offset = 0;		/* offset sector (-o offset) */
247251Slclee static int	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);
316251Slclee static void dev_mboot_write(int 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':
4110Sstevel@tonic-gate 				io_offset = strtoul(optarg, 0, 0);
4120Sstevel@tonic-gate 				continue;
4130Sstevel@tonic-gate 			case 's':
4140Sstevel@tonic-gate 				io_size = strtoul(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
9650Sstevel@tonic-gate dev_mboot_write(int sect, char *buff, int bootsiz)
9660Sstevel@tonic-gate {
9670Sstevel@tonic-gate 	int 	new_pt, old_pt, error;
968*6478Sbharding 	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 
982*6478Sbharding 	/*
983*6478Sbharding 	 * If the new table has any Solaris partitions and the old
984*6478Sbharding 	 * table does not have an entry that describes it
985*6478Sbharding 	 * exactly then clear the old vtoc (if any).
986*6478Sbharding 	 */
9870Sstevel@tonic-gate 	for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) {
9880Sstevel@tonic-gate 
989*6478Sbharding 		/* 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 
994*6478Sbharding 		/* Does the old table have an exact entry for the new entry? */
995*6478Sbharding 		for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
996*6478Sbharding 
997*6478Sbharding 			/* We only care about old Solaris partitions. */
998*6478Sbharding 			if ((Old_Table[old_pt].systid == SUNIXOS) ||
999*6478Sbharding 			    (Old_Table[old_pt].systid == SUNIXOS2)) {
1000*6478Sbharding 
1001*6478Sbharding 				/* Is this old one the same as a new one? */
1002*6478Sbharding 				if ((Old_Table[old_pt].relsect ==
1003*6478Sbharding 				    Table[new_pt].relsect) &&
1004*6478Sbharding 				    (Old_Table[old_pt].numsect ==
1005*6478Sbharding 				    Table[new_pt].numsect))
1006*6478Sbharding 					break; /* Yes */
1007*6478Sbharding 			}
1008*6478Sbharding 		}
1009*6478Sbharding 
1010*6478Sbharding 		/* Did a solaris partition change location or size? */
1011*6478Sbharding 		if (old_pt >= FD_NUMPART) {
1012*6478Sbharding 			/* Yes clear old vtoc */
1013*6478Sbharding 			if (io_debug) {
1014*6478Sbharding 				(void) fprintf(stderr,
1015*6478Sbharding 				    "Clearing VTOC labels from NEW"
1016*6478Sbharding 				    " table\n");
1017*6478Sbharding 			}
1018*6478Sbharding 			clear_vtoc(NEW, new_pt);
1019*6478Sbharding 		}
1020*6478Sbharding 	}
1021*6478Sbharding 
1022*6478Sbharding 
1023*6478Sbharding 	/* see if the old table had EFI */
1024*6478Sbharding 	for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) {
1025*6478Sbharding 		if (Old_Table[old_pt].systid == EFI_PMBR) {
1026*6478Sbharding 			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;
11670Sstevel@tonic-gate 	int	io_fpatt = 0;
11680Sstevel@tonic-gate 	int	io_ipatt = 0;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 	if (strncmp(io_fatt, "#", 1) != 0) {
11710Sstevel@tonic-gate 		io_fpatt++;
11720Sstevel@tonic-gate 		io_ipatt = strtoul(io_fatt, 0, 0);
11730Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11740Sstevel@tonic-gate 		for (i = 0; i < sectsiz; i += 4, buff_ptr++)
11755169Slclee 			*buff_ptr = io_ipatt;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 
11780Sstevel@tonic-gate 	/*
11790Sstevel@tonic-gate 	 * Fill disk with pattern based on block number.
11800Sstevel@tonic-gate 	 * Write to the disk at absolute relative block io_offset
11810Sstevel@tonic-gate 	 * for io_size blocks.
11820Sstevel@tonic-gate 	 */
11830Sstevel@tonic-gate 	while (io_size--) {
11840Sstevel@tonic-gate 		buff_ptr = (int *)Bootsect;
11850Sstevel@tonic-gate 		if (!io_fpatt) {
11860Sstevel@tonic-gate 			for (i = 0; i < sectsiz; i += 4, buff_ptr++)
11870Sstevel@tonic-gate 				*buff_ptr = io_offset;
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 		/* Write the data to disk */
11900Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1191251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
11925169Slclee 			    Dfltdev);
11930Sstevel@tonic-gate 			exit(1);
11940Sstevel@tonic-gate 		}
11950Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1196251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
11975169Slclee 			    Dfltdev);
11980Sstevel@tonic-gate 			exit(1);
11990Sstevel@tonic-gate 		}
12000Sstevel@tonic-gate 	} /* while (--io_size); */
12010Sstevel@tonic-gate }
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate /*
12040Sstevel@tonic-gate  * abs_read
12050Sstevel@tonic-gate  * Read from the disk at absolute relative block io_offset for
12060Sstevel@tonic-gate  * io_size blocks. Write the data to standard ouput (-r).
12070Sstevel@tonic-gate  */
1208251Slclee static void
1209251Slclee abs_read(void)
1210251Slclee {
12110Sstevel@tonic-gate 	int c;
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 	while (io_size--) {
12140Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1215251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12160Sstevel@tonic-gate 			    Dfltdev);
12170Sstevel@tonic-gate 			exit(1);
12180Sstevel@tonic-gate 		}
12190Sstevel@tonic-gate 		if (read(Dev, Bootsect, sectsiz) != sectsiz) {
1220251Slclee 			(void) fprintf(stderr, "fdisk: Error reading %s.\n",
12210Sstevel@tonic-gate 			    Dfltdev);
12220Sstevel@tonic-gate 			exit(1);
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 		/* Write to standard ouptut */
1226251Slclee 		if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12270Sstevel@tonic-gate 			if (c >= 0) {
12280Sstevel@tonic-gate 				if (io_debug)
1229251Slclee 					(void) fprintf(stderr,
1230251Slclee 					    "fdisk: Output warning: %d of %d"
1231251Slclee 					    " characters written.\n",
1232251Slclee 					    c, sectsiz);
12330Sstevel@tonic-gate 				exit(2);
12340Sstevel@tonic-gate 			} else {
12350Sstevel@tonic-gate 				perror("write error on output file.");
12360Sstevel@tonic-gate 				exit(2);
12370Sstevel@tonic-gate 			}
12380Sstevel@tonic-gate 		} /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */
12390Sstevel@tonic-gate 			/* != sectsiz) */
12400Sstevel@tonic-gate 	} /* while (--io_size); */
12410Sstevel@tonic-gate 	exit(0);
12420Sstevel@tonic-gate }
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate /*
12450Sstevel@tonic-gate  * abs_write
12460Sstevel@tonic-gate  * Read the data from standard input. Write to the disk at
12470Sstevel@tonic-gate  * absolute relative block io_offset for io_size blocks (-w).
12480Sstevel@tonic-gate  */
1249251Slclee static void
1250251Slclee abs_write(void)
12510Sstevel@tonic-gate {
12520Sstevel@tonic-gate 	int c, i;
12530Sstevel@tonic-gate 
12540Sstevel@tonic-gate 	while (io_size--) {
12550Sstevel@tonic-gate 		int part_exit = 0;
12560Sstevel@tonic-gate 		/* Read from standard input */
12570Sstevel@tonic-gate 		if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) {
12580Sstevel@tonic-gate 			if (c >= 0) {
12590Sstevel@tonic-gate 				if (io_debug)
1260251Slclee 				(void) fprintf(stderr,
12610Sstevel@tonic-gate 				    "fdisk: WARNING: Incomplete read (%d of"
12620Sstevel@tonic-gate 				    " %d characters read) on input file.\n",
12635169Slclee 				    c, sectsiz);
12640Sstevel@tonic-gate 				/* Fill pattern to mark partial sector in buf */
12650Sstevel@tonic-gate 				for (i = c; i < sectsiz; ) {
12660Sstevel@tonic-gate 					Bootsect[i++] = 0x41;
12670Sstevel@tonic-gate 					Bootsect[i++] = 0x62;
12680Sstevel@tonic-gate 					Bootsect[i++] = 0x65;
12690Sstevel@tonic-gate 					Bootsect[i++] = 0;
12700Sstevel@tonic-gate 				}
12710Sstevel@tonic-gate 				part_exit++;
12720Sstevel@tonic-gate 			} else {
12730Sstevel@tonic-gate 				perror("read error on input file.");
12740Sstevel@tonic-gate 				exit(2);
12750Sstevel@tonic-gate 			}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		}
12780Sstevel@tonic-gate 		/* Write to disk drive */
12790Sstevel@tonic-gate 		if (lseek(Dev, sectsiz * io_offset++, SEEK_SET) == -1) {
1280251Slclee 			(void) fprintf(stderr, "fdisk: Error seeking on %s.\n",
12810Sstevel@tonic-gate 			    Dfltdev);
12820Sstevel@tonic-gate 			exit(1);
12830Sstevel@tonic-gate 		}
12840Sstevel@tonic-gate 		if (write(Dev, Bootsect, sectsiz) != sectsiz) {
1285251Slclee 			(void) fprintf(stderr, "fdisk: Error writing %s.\n",
12860Sstevel@tonic-gate 			    Dfltdev);
12870Sstevel@tonic-gate 			exit(1);
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		if (part_exit)
12900Sstevel@tonic-gate 		exit(0);
12910Sstevel@tonic-gate 	} /* while (--io_size); */
12920Sstevel@tonic-gate 	exit(1);
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate 
1295251Slclee 
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate  * load
12980Sstevel@tonic-gate  * Load will either read the fdisk table from a file or add or
12990Sstevel@tonic-gate  * delete an entry (-A, -D, -F).
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate 
1302251Slclee static void
1303251Slclee load(int funct, char *file)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	int	id;
13060Sstevel@tonic-gate 	int	act;
13070Sstevel@tonic-gate 	int	bhead;
13080Sstevel@tonic-gate 	int	bsect;
13090Sstevel@tonic-gate 	int	bcyl;
13100Sstevel@tonic-gate 	int	ehead;
13110Sstevel@tonic-gate 	int	esect;
13120Sstevel@tonic-gate 	int	ecyl;
13130Sstevel@tonic-gate 	int	rsect;
13140Sstevel@tonic-gate 	int	numsect;
13150Sstevel@tonic-gate 	char	line[256];
13160Sstevel@tonic-gate 	int	i = 0;
13170Sstevel@tonic-gate 	int	j;
13180Sstevel@tonic-gate 	FILE *fp;
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	switch (funct) {
13210Sstevel@tonic-gate 
13225169Slclee 	case LOADFILE:
13230Sstevel@tonic-gate 
13240Sstevel@tonic-gate 		/*
13250Sstevel@tonic-gate 		 * Zero out the table before loading it, which will
13260Sstevel@tonic-gate 		 * force it to be updated on disk later (-F
13270Sstevel@tonic-gate 		 * fdisk_file).
13280Sstevel@tonic-gate 		 */
13290Sstevel@tonic-gate 		nulltbl();
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 		/* Open the prototype file */
13320Sstevel@tonic-gate 		if ((fp = fopen(file, "r")) == NULL) {
13330Sstevel@tonic-gate 			(void) fprintf(stderr,
13340Sstevel@tonic-gate 			    "fdisk: Cannot open prototype partition file %s.\n",
13350Sstevel@tonic-gate 			    file);
13360Sstevel@tonic-gate 			exit(1);
13370Sstevel@tonic-gate 		}
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		/* Read a line from the file */
13400Sstevel@tonic-gate 		while (fgets(line, sizeof (line) - 1, fp)) {
13410Sstevel@tonic-gate 			if (pars_fdisk(line, &id, &act, &bhead, &bsect,
13420Sstevel@tonic-gate 			    &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) {
13430Sstevel@tonic-gate 				continue;
13440Sstevel@tonic-gate 			}
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 			/*
13470Sstevel@tonic-gate 			 * Validate the partition. It cannot start at sector
13480Sstevel@tonic-gate 			 * 0 unless it is UNUSED or already exists
13490Sstevel@tonic-gate 			 */
13500Sstevel@tonic-gate 			if (validate_part(id, rsect, numsect) < 0) {
13510Sstevel@tonic-gate 				(void) fprintf(stderr,
13520Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13530Sstevel@tonic-gate 				    line);
13540Sstevel@tonic-gate 				exit(1);
13550Sstevel@tonic-gate 			}
13560Sstevel@tonic-gate 			/*
13570Sstevel@tonic-gate 			 * Find an unused entry to use and put the entry
13580Sstevel@tonic-gate 			 * in table
13590Sstevel@tonic-gate 			 */
13600Sstevel@tonic-gate 			if (insert_tbl(id, act, bhead, bsect, bcyl, ehead,
13610Sstevel@tonic-gate 			    esect, ecyl, rsect, numsect) < 0) {
13620Sstevel@tonic-gate 				(void) fprintf(stderr,
13630Sstevel@tonic-gate 				    "fdisk: Error on entry \"%s\".\n",
13640Sstevel@tonic-gate 				    line);
13650Sstevel@tonic-gate 				exit(1);
13660Sstevel@tonic-gate 			}
13670Sstevel@tonic-gate 		} /* while (fgets(line, sizeof (line) - 1, fp)) */
13680Sstevel@tonic-gate 
13690Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1370251Slclee 			(void) fprintf(stderr,
13710Sstevel@tonic-gate 			    "fdisk: Cannot create partition table\n");
13720Sstevel@tonic-gate 			exit(1);
13730Sstevel@tonic-gate 		}
13740Sstevel@tonic-gate 
1375251Slclee 		(void) fclose(fp);
13760Sstevel@tonic-gate 		return;
13770Sstevel@tonic-gate 
13785169Slclee 	case LOADDEL:
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		/* Parse the user-supplied deletion line (-D) */
1381251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl,
1382251Slclee 		    &ehead, &esect, &ecyl, &rsect, &numsect)) {
1383251Slclee 			(void) fprintf(stderr,
1384251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1385251Slclee 			exit(1);
1386251Slclee 		}
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 		/* Find the exact entry in the table */
13890Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
13900Sstevel@tonic-gate 			if (Table[i].systid == id &&
13910Sstevel@tonic-gate 			    Table[i].bootid == act &&
13920Sstevel@tonic-gate 			    Table[i].beghead == bhead &&
13930Sstevel@tonic-gate 			    Table[i].begsect == ((bsect & 0x3f) |
13945169Slclee 			    (uchar_t)((bcyl>>2) & 0xc0)) &&
1395251Slclee 			    Table[i].begcyl == (uchar_t)(bcyl & 0xff) &&
13960Sstevel@tonic-gate 			    Table[i].endhead == ehead &&
13970Sstevel@tonic-gate 			    Table[i].endsect == ((esect & 0x3f) |
13985169Slclee 			    (uchar_t)((ecyl>>2) & 0xc0)) &&
1399251Slclee 			    Table[i].endcyl == (uchar_t)(ecyl & 0xff) &&
14000Sstevel@tonic-gate 			    Table[i].relsect == lel(rsect) &&
14010Sstevel@tonic-gate 			    Table[i].numsect == lel(numsect)) {
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 				/*
14040Sstevel@tonic-gate 				 * Found the entry. Now move rest of
14050Sstevel@tonic-gate 				 * entries up toward the top of the
14060Sstevel@tonic-gate 				 * table, leaving available entries at
14070Sstevel@tonic-gate 				 * the end of the fdisk table.
14080Sstevel@tonic-gate 				 */
1409251Slclee 				for (j = i; j < FD_NUMPART - 1; j++) {
1410251Slclee 					Table[j].systid = Table[j + 1].systid;
1411251Slclee 					Table[j].bootid = Table[j + 1].bootid;
1412251Slclee 					Table[j].beghead = Table[j + 1].beghead;
1413251Slclee 					Table[j].begsect = Table[j + 1].begsect;
1414251Slclee 					Table[j].begcyl = Table[j + 1].begcyl;
1415251Slclee 					Table[j].endhead = Table[j + 1].endhead;
1416251Slclee 					Table[j].endsect = Table[j + 1].endsect;
1417251Slclee 					Table[j].endcyl = Table[j + 1].endcyl;
1418251Slclee 					Table[j].relsect = Table[j + 1].relsect;
1419251Slclee 					Table[j].numsect = Table[j + 1].numsect;
14200Sstevel@tonic-gate 				}
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 				/*
14230Sstevel@tonic-gate 				 * Mark the last entry as unused in case
14240Sstevel@tonic-gate 				 * all table entries were in use prior
14250Sstevel@tonic-gate 				 * to the deletion.
14260Sstevel@tonic-gate 				 */
14270Sstevel@tonic-gate 
1428251Slclee 				Table[FD_NUMPART - 1].systid = UNUSED;
1429251Slclee 				Table[FD_NUMPART - 1].bootid = 0;
14300Sstevel@tonic-gate 				return;
14310Sstevel@tonic-gate 			}
14320Sstevel@tonic-gate 		}
1433251Slclee 		(void) fprintf(stderr,
14340Sstevel@tonic-gate 		    "fdisk: Entry does not match any existing partition:\n"
14350Sstevel@tonic-gate 		    "	\"%s\"\n",
14360Sstevel@tonic-gate 		    file);
14370Sstevel@tonic-gate 		exit(1);
1438251Slclee 		/* FALLTHRU */
14390Sstevel@tonic-gate 
14405169Slclee 	case LOADADD:
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 		/* Parse the user-supplied addition line (-A) */
1443251Slclee 		if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead,
1444251Slclee 		    &esect, &ecyl, &rsect, &numsect)) {
1445251Slclee 			(void) fprintf(stderr,
1446251Slclee 			    "fdisk: Syntax error \"%s\"\n", file);
1447251Slclee 			exit(1);
1448251Slclee 		}
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate 		/* Validate the partition. It cannot start at sector 0 */
14510Sstevel@tonic-gate 		if (rsect == 0) {
14520Sstevel@tonic-gate 			(void) fprintf(stderr,
14530Sstevel@tonic-gate 			    "fdisk: New partition cannot start at sector 0:\n"
14540Sstevel@tonic-gate 			    "   \"%s\".\n",
14550Sstevel@tonic-gate 			    file);
14560Sstevel@tonic-gate 			exit(1);
14570Sstevel@tonic-gate 		}
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate 		/*
14600Sstevel@tonic-gate 		 * if the user wishes to add an EFI partition, we need
14610Sstevel@tonic-gate 		 * more extensive validation.  rsect should be 1, and
14620Sstevel@tonic-gate 		 * numsect should equal the entire disk capacity - 1
14630Sstevel@tonic-gate 		 */
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 		if (id == EFI_PMBR) {
14660Sstevel@tonic-gate 			if (rsect != 1) {
14670Sstevel@tonic-gate 				(void) fprintf(stderr,
14680Sstevel@tonic-gate 				    "fdisk: EFI partitions must start at sector"
14690Sstevel@tonic-gate 				    " 1 (input rsect = %d)\n", rsect);
14700Sstevel@tonic-gate 				exit(1);
14710Sstevel@tonic-gate 			}
14720Sstevel@tonic-gate 
14735169Slclee 			if (numsect != dev_capacity - 1) {
14740Sstevel@tonic-gate 				(void) fprintf(stderr,
14750Sstevel@tonic-gate 				    "fdisk: EFI partitions must encompass the "
1476251Slclee 				    "entire disk\n"
14775169Slclee 				    "(input numsect: %d - avail: %llu)\n",
1478251Slclee 				    numsect,
14795169Slclee 				    dev_capacity - 1);
14800Sstevel@tonic-gate 				exit(1);
14810Sstevel@tonic-gate 			}
14820Sstevel@tonic-gate 		}
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate 		/* Find unused entry for use and put entry in table */
14850Sstevel@tonic-gate 		if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect,
14860Sstevel@tonic-gate 		    ecyl, rsect, numsect) < 0) {
14870Sstevel@tonic-gate 			(void) fprintf(stderr,
14880Sstevel@tonic-gate 			    "fdisk: Invalid entry could not be inserted:\n"
14890Sstevel@tonic-gate 			    "	\"%s\"\n",
14900Sstevel@tonic-gate 			    file);
14910Sstevel@tonic-gate 			exit(1);
14920Sstevel@tonic-gate 		}
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 		/* Make sure new entry does not overlap existing entry */
14950Sstevel@tonic-gate 		if (verify_tbl() < 0) {
1496251Slclee 			(void) fprintf(stderr,
1497251Slclee 			    "fdisk: Cannot create partition \"%s\"\n", file);
14980Sstevel@tonic-gate 			exit(1);
14990Sstevel@tonic-gate 		}
15000Sstevel@tonic-gate 	} /* switch funct */
15010Sstevel@tonic-gate }
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate /*
15040Sstevel@tonic-gate  * Set_Table_CHS_Values
15050Sstevel@tonic-gate  *
15060Sstevel@tonic-gate  * This will calculate the CHS values for beginning and ending CHS
15070Sstevel@tonic-gate  * for a single partition table entry (ti) based on the relsect
15080Sstevel@tonic-gate  * and numsect values contained in the partion table entry.
15090Sstevel@tonic-gate  *
15100Sstevel@tonic-gate  * hba_heads and hba_sectors contain the number of heads and sectors.
15110Sstevel@tonic-gate  *
15120Sstevel@tonic-gate  * If the number of cylinders exceeds the MAX_CYL,
15130Sstevel@tonic-gate  * then maximum values will be placed in the corresponding chs entry.
15140Sstevel@tonic-gate  */
15150Sstevel@tonic-gate static void
15160Sstevel@tonic-gate Set_Table_CHS_Values(int ti)
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate 	uint32_t	lba, cy, hd, sc;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	lba = (uint32_t)Table[ti].relsect;
15210Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15220Sstevel@tonic-gate 		/*
15230Sstevel@tonic-gate 		 * the lba address cannot be expressed in CHS value
15240Sstevel@tonic-gate 		 * so store the maximum CHS field values in the CHS fields.
15250Sstevel@tonic-gate 		 */
15260Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15270Sstevel@tonic-gate 		hd = MAX_HEAD;
15280Sstevel@tonic-gate 		sc = MAX_SECT;
15290Sstevel@tonic-gate 	} else {
15300Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15310Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15320Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 	Table[ti].begcyl = cy & 0xff;
1535251Slclee 	Table[ti].beghead = (uchar_t)hd;
1536251Slclee 	Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	/*
15390Sstevel@tonic-gate 	 * This code is identical to the code above
15400Sstevel@tonic-gate 	 * except that it works on ending CHS values
15410Sstevel@tonic-gate 	 */
15420Sstevel@tonic-gate 	lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1);
15430Sstevel@tonic-gate 	if (lba >= hba_heads * hba_sectors * MAX_CYL) {
15440Sstevel@tonic-gate 		cy = MAX_CYL + 1;
15450Sstevel@tonic-gate 		hd = MAX_HEAD;
15460Sstevel@tonic-gate 		sc = MAX_SECT;
15470Sstevel@tonic-gate 	} else {
15480Sstevel@tonic-gate 		cy = lba / hba_sectors / hba_heads;
15490Sstevel@tonic-gate 		hd = lba / hba_sectors % hba_heads;
15500Sstevel@tonic-gate 		sc = lba % hba_sectors + 1;
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 	Table[ti].endcyl = cy & 0xff;
1553251Slclee 	Table[ti].endhead = (uchar_t)hd;
1554251Slclee 	Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * insert_tbl
15590Sstevel@tonic-gate  * 	Insert entry into fdisk table. Check all user-supplied values
15600Sstevel@tonic-gate  *	for the entry, but not the validity relative to other table
15610Sstevel@tonic-gate  *	entries!
15620Sstevel@tonic-gate  */
1563251Slclee static int
1564251Slclee insert_tbl(
1565251Slclee     int id, int act,
1566251Slclee     int bhead, int bsect, int bcyl,
1567251Slclee     int ehead, int esect, int ecyl,
1568251Slclee     int rsect, int numsect)
15690Sstevel@tonic-gate {
15700Sstevel@tonic-gate 	int	i;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	/* validate partition size */
15735169Slclee 	if (rsect + numsect > dev_capacity) {
1574251Slclee 		(void) fprintf(stderr,
15750Sstevel@tonic-gate 		    "fdisk: Partition table exceeds the size of the disk.\n");
15760Sstevel@tonic-gate 		return (-1);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	/* find UNUSED partition table entry */
15800Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
15810Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
15820Sstevel@tonic-gate 			break;
15830Sstevel@tonic-gate 		}
15840Sstevel@tonic-gate 	}
15850Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
1586251Slclee 		(void) fprintf(stderr, "fdisk: Partition table is full.\n");
15870Sstevel@tonic-gate 		return (-1);
15880Sstevel@tonic-gate 	}
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate 
1591251Slclee 	Table[i].systid = (uchar_t)id;
1592251Slclee 	Table[i].bootid = (uchar_t)act;
15930Sstevel@tonic-gate 	Table[i].numsect = lel(numsect);
15940Sstevel@tonic-gate 	Table[i].relsect = lel(rsect);
15950Sstevel@tonic-gate 
15960Sstevel@tonic-gate 	/*
15970Sstevel@tonic-gate 	 * If we have been called with a valid geometry, use it
15980Sstevel@tonic-gate 	 * valid means non-zero values that fit in the BIOS fields
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	if (0 < bsect && bsect <= MAX_SECT &&
16010Sstevel@tonic-gate 	    0 <= bhead && bhead <= MAX_HEAD &&
16020Sstevel@tonic-gate 	    0 < esect && esect <= MAX_SECT &&
16030Sstevel@tonic-gate 	    0 <= ehead && ehead <= MAX_HEAD) {
16040Sstevel@tonic-gate 		if (bcyl > MAX_CYL)
16050Sstevel@tonic-gate 			bcyl = MAX_CYL + 1;
16060Sstevel@tonic-gate 		if (ecyl > MAX_CYL)
16070Sstevel@tonic-gate 			ecyl = MAX_CYL + 1;
16080Sstevel@tonic-gate 		Table[i].begcyl = bcyl & 0xff;
16090Sstevel@tonic-gate 		Table[i].endcyl = ecyl & 0xff;
1610251Slclee 		Table[i].beghead = (uchar_t)bhead;
1611251Slclee 		Table[i].endhead = (uchar_t)ehead;
1612251Slclee 		Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect);
16130Sstevel@tonic-gate 		Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect;
16140Sstevel@tonic-gate 	} else {
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		/*
16170Sstevel@tonic-gate 		 * The specified values are invalid,
16180Sstevel@tonic-gate 		 * so calculate the values based on hba_heads, hba_sectors
16190Sstevel@tonic-gate 		 */
16200Sstevel@tonic-gate 		Set_Table_CHS_Values(i);
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	/*
16240Sstevel@tonic-gate 	 * return partition index
16250Sstevel@tonic-gate 	 */
16260Sstevel@tonic-gate 	return (i);
16270Sstevel@tonic-gate }
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate /*
16300Sstevel@tonic-gate  * verify_tbl
16310Sstevel@tonic-gate  * Verify that no partition entries overlap or exceed the size of
16320Sstevel@tonic-gate  * the disk.
16330Sstevel@tonic-gate  */
1634251Slclee static int
1635251Slclee verify_tbl(void)
16360Sstevel@tonic-gate {
16370Sstevel@tonic-gate 	int	i, j, rsect, numsect;
16380Sstevel@tonic-gate 	int	noMoreParts = 0;
16390Sstevel@tonic-gate 	int	numParts = 0;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	/* Make sure new entry does not overlap an existing entry */
1642251Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
16430Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
16440Sstevel@tonic-gate 			numParts++;
16450Sstevel@tonic-gate 			/*
16460Sstevel@tonic-gate 			 * No valid partitions allowed after an UNUSED  or
16470Sstevel@tonic-gate 			 * EFI_PMBR part
16480Sstevel@tonic-gate 			 */
16490Sstevel@tonic-gate 			if (noMoreParts) {
16500Sstevel@tonic-gate 				return (-1);
16510Sstevel@tonic-gate 			}
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 			/*
16540Sstevel@tonic-gate 			 * EFI_PMBR partitions must be the only partition
16550Sstevel@tonic-gate 			 * and must be Table entry 0
16560Sstevel@tonic-gate 			 */
16570Sstevel@tonic-gate 			if (Table[i].systid == EFI_PMBR) {
16580Sstevel@tonic-gate 				if (i == 0) {
16590Sstevel@tonic-gate 					noMoreParts = 1;
16600Sstevel@tonic-gate 				} else {
16610Sstevel@tonic-gate 					return (-1);
16620Sstevel@tonic-gate 				}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 				if (Table[i].relsect != 1) {
1665251Slclee 					(void) fprintf(stderr, "ERROR: "
16660Sstevel@tonic-gate 					    "Invalid starting sector "
16670Sstevel@tonic-gate 					    "for EFI_PMBR partition:\n"
16680Sstevel@tonic-gate 					    "relsect %d "
16690Sstevel@tonic-gate 					    "(should be 1)\n",
16700Sstevel@tonic-gate 					    Table[i].relsect);
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 					return (-1);
16730Sstevel@tonic-gate 				}
16740Sstevel@tonic-gate 
16755169Slclee 				if (Table[i].numsect != dev_capacity - 1) {
1676251Slclee 					(void) fprintf(stderr, "ERROR: "
16770Sstevel@tonic-gate 					    "EFI_PMBR partition must "
16780Sstevel@tonic-gate 					    "encompass the entire "
1679251Slclee 					    "disk.\n numsect %d - "
16805169Slclee 					    "actual %llu\n",
16810Sstevel@tonic-gate 					    Table[i].numsect,
16825169Slclee 					    dev_capacity - 1);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 					return (-1);
16850Sstevel@tonic-gate 				}
16860Sstevel@tonic-gate 			}
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 			/* make sure the partition isn't larger than the disk */
16890Sstevel@tonic-gate 			rsect = lel(Table[i].relsect);
16900Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
16915169Slclee 			if ((rsect + numsect) > dev_capacity) {
16920Sstevel@tonic-gate 				return (-1);
16930Sstevel@tonic-gate 			}
16940Sstevel@tonic-gate 
1695251Slclee 			for (j = i + 1; j < FD_NUMPART; j++) {
16960Sstevel@tonic-gate 				if (Table[j].systid != UNUSED) {
16970Sstevel@tonic-gate 					int t_relsect = lel(Table[j].relsect);
16980Sstevel@tonic-gate 					int t_numsect = lel(Table[j].numsect);
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate 					if (noMoreParts) {
1701251Slclee 						(void) fprintf(stderr,
17020Sstevel@tonic-gate 						    "Cannot add partition to "
17030Sstevel@tonic-gate 						    "table; no more partitions "
17040Sstevel@tonic-gate 						    "allowed\n");
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 						if (io_debug) {
1707251Slclee 							(void) fprintf(stderr,
17080Sstevel@tonic-gate 							    "DEBUG: Current "
17090Sstevel@tonic-gate 							    "partition:\t"
17100Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1711251Slclee 							    "%d:%d:%d:%d:%d\n"
17120Sstevel@tonic-gate 							    "       Next "
17130Sstevel@tonic-gate 							    "partition:\t\t"
17140Sstevel@tonic-gate 							    "%d:%d:%d:%d:%d:"
1715251Slclee 							    "%d:%d:%d:%d:%d\n",
17160Sstevel@tonic-gate 							    Table[i].systid,
17170Sstevel@tonic-gate 							    Table[i].bootid,
17180Sstevel@tonic-gate 							    Table[i].begcyl,
17190Sstevel@tonic-gate 							    Table[i].beghead,
17200Sstevel@tonic-gate 							    Table[i].begsect,
17210Sstevel@tonic-gate 							    Table[i].endcyl,
17220Sstevel@tonic-gate 							    Table[i].endhead,
17230Sstevel@tonic-gate 							    Table[i].endsect,
17240Sstevel@tonic-gate 							    Table[i].relsect,
17250Sstevel@tonic-gate 							    Table[i].numsect,
17260Sstevel@tonic-gate 							    Table[j].systid,
17270Sstevel@tonic-gate 							    Table[j].bootid,
17280Sstevel@tonic-gate 							    Table[j].begcyl,
17290Sstevel@tonic-gate 							    Table[j].beghead,
17300Sstevel@tonic-gate 							    Table[j].begsect,
17310Sstevel@tonic-gate 							    Table[j].endcyl,
17320Sstevel@tonic-gate 							    Table[j].endhead,
17330Sstevel@tonic-gate 							    Table[j].endsect,
17340Sstevel@tonic-gate 							    Table[j].relsect,
17350Sstevel@tonic-gate 							    Table[j].numsect);
17360Sstevel@tonic-gate 						}
17370Sstevel@tonic-gate 
17380Sstevel@tonic-gate 						return (-1);
17390Sstevel@tonic-gate 					}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 					if ((rsect >=
17420Sstevel@tonic-gate 					    (t_relsect + t_numsect)) ||
1743251Slclee 					    ((rsect + numsect) <= t_relsect)) {
17440Sstevel@tonic-gate 						continue;
17450Sstevel@tonic-gate 					} else {
1746251Slclee 						(void) fprintf(stderr, "ERROR: "
17470Sstevel@tonic-gate 						    "current partition overlaps"
17480Sstevel@tonic-gate 						    " following partition\n");
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 						return (-1);
17510Sstevel@tonic-gate 					}
17520Sstevel@tonic-gate 				}
17530Sstevel@tonic-gate 			}
17540Sstevel@tonic-gate 		} else {
17550Sstevel@tonic-gate 			noMoreParts = 1;
17560Sstevel@tonic-gate 		}
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 	if (Table[i].systid != UNUSED) {
17590Sstevel@tonic-gate 		if (noMoreParts ||
17600Sstevel@tonic-gate 		    ((lel(Table[i].relsect) + lel(Table[i].numsect)) >
17615169Slclee 		    dev_capacity)) {
17620Sstevel@tonic-gate 			return (-1);
17630Sstevel@tonic-gate 		}
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate 	return (numParts);
17670Sstevel@tonic-gate }
17680Sstevel@tonic-gate 
17690Sstevel@tonic-gate /*
17700Sstevel@tonic-gate  * pars_fdisk
17710Sstevel@tonic-gate  * Parse user-supplied data to set up fdisk partitions
17720Sstevel@tonic-gate  * (-A, -D, -F).
17730Sstevel@tonic-gate  */
1774251Slclee static int
1775251Slclee pars_fdisk(
1776251Slclee     char *line,
1777251Slclee     int *id, int *act,
1778251Slclee     int *bhead, int *bsect, int *bcyl,
1779251Slclee     int *ehead, int *esect, int *ecyl,
1780251Slclee     int *rsect, int *numsect)
17810Sstevel@tonic-gate {
17820Sstevel@tonic-gate 	int	i;
17830Sstevel@tonic-gate 	if (line[0] == '\0' || line[0] == '\n' || line[0] == '*')
17845169Slclee 		return (1);
17850Sstevel@tonic-gate 	line[strlen(line)] = '\0';
17860Sstevel@tonic-gate 	for (i = 0; i < strlen(line); i++) {
17870Sstevel@tonic-gate 		if (line[i] == '\0') {
17880Sstevel@tonic-gate 			break;
17890Sstevel@tonic-gate 		} else if (line[i] == ':') {
17900Sstevel@tonic-gate 			line[i] = ' ';
17910Sstevel@tonic-gate 		}
17920Sstevel@tonic-gate 	}
1793251Slclee 	if (sscanf(line, "%d %d %d %d %d %d %d %d %d %d",
17940Sstevel@tonic-gate 	    id, act, bhead, bsect, bcyl, ehead, esect, ecyl,
17950Sstevel@tonic-gate 	    rsect, numsect) != 10) {
17960Sstevel@tonic-gate 		(void) fprintf(stderr, "Syntax error:\n	\"%s\".\n", line);
17970Sstevel@tonic-gate 		exit(1);
17980Sstevel@tonic-gate 	}
17990Sstevel@tonic-gate 	return (0);
18000Sstevel@tonic-gate }
18010Sstevel@tonic-gate 
18020Sstevel@tonic-gate /*
18030Sstevel@tonic-gate  * validate_part
18040Sstevel@tonic-gate  * Validate that a new partition does not start at sector 0. Only UNUSED
18050Sstevel@tonic-gate  * partitions and previously existing partitions are allowed to start at 0.
18060Sstevel@tonic-gate  */
1807251Slclee static int
1808251Slclee validate_part(int id, int rsect, int numsect)
18090Sstevel@tonic-gate {
18100Sstevel@tonic-gate 	int i;
18110Sstevel@tonic-gate 	if ((id != UNUSED) && (rsect == 0)) {
18120Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
18130Sstevel@tonic-gate 			if ((Old_Table[i].systid == id) &&
18140Sstevel@tonic-gate 			    (Old_Table[i].relsect == lel(rsect)) &&
18155169Slclee 			    (Old_Table[i].numsect == lel(numsect)))
18165169Slclee 				return (0);
18170Sstevel@tonic-gate 		}
1818251Slclee 		(void) fprintf(stderr,
1819251Slclee 		    "New partition cannot start at sector 0\n");
18200Sstevel@tonic-gate 		return (-1);
18210Sstevel@tonic-gate 	}
18220Sstevel@tonic-gate 	return (0);
18230Sstevel@tonic-gate }
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate /*
18260Sstevel@tonic-gate  * stage0
18270Sstevel@tonic-gate  * Print out interactive menu and process user input.
18280Sstevel@tonic-gate  */
1829251Slclee static void
1830251Slclee stage0(void)
18310Sstevel@tonic-gate {
1832251Slclee 	dispmenu();
1833251Slclee 	for (;;) {
1834251Slclee 		(void) printf(Q_LINE);
1835251Slclee 		(void) printf("Enter Selection: ");
1836251Slclee 		(void) gets(s);
18370Sstevel@tonic-gate 		rm_blanks(s);
18380Sstevel@tonic-gate 		while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) {
1839251Slclee 			(void) printf(E_LINE); /* Clear any previous error */
1840251Slclee 			(void) printf(
1841251Slclee 			    "Enter a one-digit number between 1 and 6.");
1842251Slclee 			(void) printf(Q_LINE);
1843251Slclee 			(void) printf("Enter Selection: ");
1844251Slclee 			(void) gets(s);
18450Sstevel@tonic-gate 			rm_blanks(s);
18460Sstevel@tonic-gate 		}
1847251Slclee 		(void) printf(E_LINE);
18480Sstevel@tonic-gate 		switch (s[0]) {
18490Sstevel@tonic-gate 			case '1':
18500Sstevel@tonic-gate 				if (pcreate() == -1)
18510Sstevel@tonic-gate 					return;
18520Sstevel@tonic-gate 				break;
18530Sstevel@tonic-gate 			case '2':
18540Sstevel@tonic-gate 				if (pchange() == -1)
18550Sstevel@tonic-gate 					return;
18560Sstevel@tonic-gate 				break;
18570Sstevel@tonic-gate 			case '3':
18580Sstevel@tonic-gate 				if (pdelete() == -1)
18590Sstevel@tonic-gate 					return;
18600Sstevel@tonic-gate 				break;
18610Sstevel@tonic-gate 			case '4':
18620Sstevel@tonic-gate 				if (ppartid() == -1)
18630Sstevel@tonic-gate 					return;
18640Sstevel@tonic-gate 				break;
18650Sstevel@tonic-gate 			case '5':
18660Sstevel@tonic-gate 				/* update disk partition table, if changed */
18670Sstevel@tonic-gate 				if (TableChanged() == 1) {
18680Sstevel@tonic-gate 					copy_Table_to_Bootblk();
18690Sstevel@tonic-gate 					dev_mboot_write(0, Bootsect, sectsiz);
18700Sstevel@tonic-gate 				}
18710Sstevel@tonic-gate 				/*
18720Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18730Sstevel@tonic-gate 				 * (truncate only)
18740Sstevel@tonic-gate 				 */
18750Sstevel@tonic-gate 				if (io_adjt) {
18760Sstevel@tonic-gate 					fix_slice();
18770Sstevel@tonic-gate 				}
1878251Slclee 				(void) close(Dev);
18790Sstevel@tonic-gate 				exit(0);
1880251Slclee 				/* FALLTHRU */
18810Sstevel@tonic-gate 			case '6':
18820Sstevel@tonic-gate 				/*
18830Sstevel@tonic-gate 				 * If the VTOC table is wrong fix it
18840Sstevel@tonic-gate 				 * (truncate only)
18850Sstevel@tonic-gate 				 */
18860Sstevel@tonic-gate 				if (io_adjt) {
18870Sstevel@tonic-gate 					fix_slice();
18880Sstevel@tonic-gate 				}
1889251Slclee 				(void) close(Dev);
18900Sstevel@tonic-gate 				exit(0);
1891251Slclee 				/* FALLTHRU */
18920Sstevel@tonic-gate 			default:
18930Sstevel@tonic-gate 				break;
18940Sstevel@tonic-gate 		}
18950Sstevel@tonic-gate 		copy_Table_to_Bootblk();
18960Sstevel@tonic-gate 		disptbl();
1897251Slclee 		dispmenu();
18980Sstevel@tonic-gate 	}
18990Sstevel@tonic-gate }
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate /*
19020Sstevel@tonic-gate  * pcreate
19030Sstevel@tonic-gate  * Create partition entry in the table (interactive mode).
19040Sstevel@tonic-gate  */
1905251Slclee static int
1906251Slclee pcreate(void)
19070Sstevel@tonic-gate {
1908251Slclee 	uchar_t tsystid = 'z';
19090Sstevel@tonic-gate 	int i, j;
19100Sstevel@tonic-gate 	int rsect = 1;
19110Sstevel@tonic-gate 	int retCode = 0;
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 	i = 0;
1914251Slclee 	for (;;) {
19150Sstevel@tonic-gate 		if (i == FD_NUMPART) {
1916251Slclee 			(void) printf(E_LINE);
1917251Slclee 			(void) printf(
1918251Slclee 			    "The partition table is full!\n"
1919251Slclee 			    "You must delete a partition before creating"
19200Sstevel@tonic-gate 			    " a new one.\n");
19210Sstevel@tonic-gate 			return (-1);
19220Sstevel@tonic-gate 		}
19230Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
19240Sstevel@tonic-gate 			break;
19250Sstevel@tonic-gate 		}
19260Sstevel@tonic-gate 		i++;
19270Sstevel@tonic-gate 	}
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	j = 0;
19300Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
19310Sstevel@tonic-gate 		if (Table[i].systid != UNUSED) {
19320Sstevel@tonic-gate 			j += lel(Table[i].numsect);
19330Sstevel@tonic-gate 		}
19345169Slclee 		if (j >= chs_capacity) {
1935251Slclee 			(void) printf(E_LINE);
1936251Slclee 			(void) printf("There is no more room on the disk for"
19370Sstevel@tonic-gate 			    " another partition.\n");
1938251Slclee 			(void) printf(
1939251Slclee 			    "You must delete a partition before creating"
19400Sstevel@tonic-gate 			    " a new one.\n");
19410Sstevel@tonic-gate 			return (-1);
19420Sstevel@tonic-gate 		}
19430Sstevel@tonic-gate 	}
19440Sstevel@tonic-gate 	while (tsystid == 'z') {
1945251Slclee 		(void) printf(Q_LINE);
1946251Slclee 		(void) printf(
1947251Slclee 		    "Select the partition type to create:\n"
1948251Slclee 		    "   1=SOLARIS2  2=UNIX        3=PCIXOS     4=Other\n"
1949251Slclee 		    "   5=DOS12     6=DOS16       7=DOSEXT     8=DOSBIG\n"
1950251Slclee 		    "   9=DOS16LBA  A=x86 Boot    B=Diagnostic C=FAT32\n"
1951251Slclee 		    "   D=FAT32LBA  E=DOSEXTLBA   F=EFI        0=Exit? ");
1952251Slclee 		(void) gets(s);
19530Sstevel@tonic-gate 		rm_blanks(s);
19540Sstevel@tonic-gate 		if (s[1] != 0) {
1955251Slclee 			(void) printf(E_LINE);
1956251Slclee 			(void) printf("Invalid selection, try again.");
19570Sstevel@tonic-gate 			continue;
19580Sstevel@tonic-gate 		}
19590Sstevel@tonic-gate 		switch (s[0]) {
19600Sstevel@tonic-gate 		case '0':		/* exit */
19615169Slclee 			(void) printf(E_LINE);
19625169Slclee 			return (-1);
19630Sstevel@tonic-gate 		case '1':		/* Solaris partition */
19645169Slclee 			tsystid = SUNIXOS2;
19655169Slclee 			break;
19660Sstevel@tonic-gate 		case '2':		/* UNIX partition */
19675169Slclee 			tsystid = UNIXOS;
19685169Slclee 			break;
19690Sstevel@tonic-gate 		case '3':		/* PCIXOS partition */
19705169Slclee 			tsystid = PCIXOS;
19715169Slclee 			break;
19720Sstevel@tonic-gate 		case '4':		/* OTHEROS System partition */
19735169Slclee 			tsystid = OTHEROS;
19745169Slclee 			break;
19750Sstevel@tonic-gate 		case '5':
19765169Slclee 			tsystid = DOSOS12; /* DOS 12 bit fat */
19775169Slclee 			break;
19780Sstevel@tonic-gate 		case '6':
19795169Slclee 			tsystid = DOSOS16; /* DOS 16 bit fat */
19805169Slclee 			break;
19810Sstevel@tonic-gate 		case '7':
19825169Slclee 			tsystid = EXTDOS;
19835169Slclee 			break;
19840Sstevel@tonic-gate 		case '8':
19855169Slclee 			tsystid = DOSHUGE;
19865169Slclee 			break;
19870Sstevel@tonic-gate 		case '9':
19885169Slclee 			tsystid = FDISK_FAT95;  /* FAT16, need extended int13 */
19895169Slclee 			break;
19900Sstevel@tonic-gate 		case 'a':		/* x86 Boot partition */
19910Sstevel@tonic-gate 		case 'A':
19925169Slclee 			tsystid = X86BOOT;
19935169Slclee 			break;
19940Sstevel@tonic-gate 		case 'b':		/* Diagnostic boot partition */
19950Sstevel@tonic-gate 		case 'B':
19965169Slclee 			tsystid = DIAGPART;
19975169Slclee 			break;
19980Sstevel@tonic-gate 		case 'c':		/* FAT32 */
19990Sstevel@tonic-gate 		case 'C':
20005169Slclee 			tsystid = FDISK_WINDOWS;
20015169Slclee 			break;
20020Sstevel@tonic-gate 		case 'd':		/* FAT32 and need extended int13 */
20030Sstevel@tonic-gate 		case 'D':
20045169Slclee 			tsystid = FDISK_EXT_WIN;
20055169Slclee 			break;
20060Sstevel@tonic-gate 		case 'e':	/* Extended partition, need extended int13 */
20070Sstevel@tonic-gate 		case 'E':
20085169Slclee 			tsystid = FDISK_EXTLBA;
20095169Slclee 			break;
20100Sstevel@tonic-gate 		case 'f':
20110Sstevel@tonic-gate 		case 'F':
20125169Slclee 			tsystid = EFI_PMBR;
20135169Slclee 			break;
20140Sstevel@tonic-gate 		default:
20155169Slclee 			(void) printf(E_LINE);
20165169Slclee 			(void) printf("Invalid selection, try again.");
20175169Slclee 			continue;
20180Sstevel@tonic-gate 		}
20190Sstevel@tonic-gate 	}
20200Sstevel@tonic-gate 
2021251Slclee 	(void) printf(E_LINE);
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate 	if (tsystid != EFI_PMBR) {
20240Sstevel@tonic-gate 		/* create the new partition */
20250Sstevel@tonic-gate 		i = specify(tsystid);
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 		if (i != -1) {
20280Sstevel@tonic-gate 			/* see if it should be the active partition */
2029251Slclee 			(void) printf(E_LINE);
2030251Slclee 			(void) printf(Q_LINE);
2031251Slclee 
2032251Slclee 			(void) printf(
2033251Slclee 			    "Should this become the active partition? If "
2034251Slclee 			    "yes, it  will be activated\n"
2035251Slclee 			    "each time the computer is reset or turned on.\n"
2036251Slclee 			    "Please type \"y\" or \"n\". ");
20370Sstevel@tonic-gate 
20380Sstevel@tonic-gate 			if (yesno()) {
2039251Slclee 				(void) printf(E_LINE);
20400Sstevel@tonic-gate 				for (j = 0; j < FD_NUMPART; j++) {
20410Sstevel@tonic-gate 					if (j == i) {
20420Sstevel@tonic-gate 						Table[j].bootid = ACTIVE;
2043251Slclee 						(void) printf(E_LINE);
2044251Slclee 						(void) printf(
2045251Slclee 						    "Partition %d is now "
20460Sstevel@tonic-gate 						    "the active partition.",
2047251Slclee 						    j + 1);
20480Sstevel@tonic-gate 					} else {
20490Sstevel@tonic-gate 						Table[j].bootid = 0;
20500Sstevel@tonic-gate 					}
20510Sstevel@tonic-gate 				}
20520Sstevel@tonic-gate 			} else {
20530Sstevel@tonic-gate 				Table[i].bootid = 0;
20540Sstevel@tonic-gate 			}
20550Sstevel@tonic-gate 
20560Sstevel@tonic-gate 			/* set up the return code */
20570Sstevel@tonic-gate 			i = 1;
20580Sstevel@tonic-gate 		}
20590Sstevel@tonic-gate 	} else {
20600Sstevel@tonic-gate 		/*
20610Sstevel@tonic-gate 		 * partitions of type EFI_PMBR must be the only partitions in
20620Sstevel@tonic-gate 		 * the table
20630Sstevel@tonic-gate 		 *
20640Sstevel@tonic-gate 		 * First, make sure there were no errors the table is
20650Sstevel@tonic-gate 		 * empty
20660Sstevel@tonic-gate 		 */
20670Sstevel@tonic-gate 		retCode = verify_tbl();
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 		if (retCode < 0) {
2070251Slclee 			(void) fprintf(stderr,
20710Sstevel@tonic-gate 			    "fdisk: Cannot create EFI partition table; \n"
20720Sstevel@tonic-gate 			    "current partition table is invalid.\n");
20730Sstevel@tonic-gate 			return (-1);
20740Sstevel@tonic-gate 		} else if (retCode > 0) {
2075251Slclee 			(void) printf(
2076251Slclee 			    "An EFI partition must be the only partition on "
2077251Slclee 			    "disk.  You may manually delete existing\n"
2078251Slclee 			    "partitions, or fdisk can do it.\n"
2079251Slclee 			    "Do you want fdisk to destroy existing "
2080251Slclee 			    "partitions?\n"
2081251Slclee 			    "Please type \"y\" or \"n\". ");
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate 			if (yesno()) {
20840Sstevel@tonic-gate 				nulltbl();
20850Sstevel@tonic-gate 			} else {
20860Sstevel@tonic-gate 				return (-1);
20870Sstevel@tonic-gate 			}
20880Sstevel@tonic-gate 		}
20890Sstevel@tonic-gate 
20900Sstevel@tonic-gate 		/* create the table entry - i should be 0 */
20910Sstevel@tonic-gate 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, rsect,
20925169Slclee 		    dev_capacity - rsect);
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 		if (i != 0) {
2095251Slclee 			(void) printf("Error creating EFI partition!!!\n");
20960Sstevel@tonic-gate 			i = -1;
20970Sstevel@tonic-gate 		} else {
20980Sstevel@tonic-gate 
20990Sstevel@tonic-gate 			/* EFI partitions are currently never active */
21000Sstevel@tonic-gate 			Table[i].bootid = 0;
21010Sstevel@tonic-gate 
21020Sstevel@tonic-gate 			/* set up the return code */
21030Sstevel@tonic-gate 			i = 1;
21040Sstevel@tonic-gate 		}
21050Sstevel@tonic-gate 	}
21060Sstevel@tonic-gate 
21070Sstevel@tonic-gate 	return (i);
21080Sstevel@tonic-gate }
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate /*
21110Sstevel@tonic-gate  * specify
21120Sstevel@tonic-gate  * Query the user to specify the size of the new partition in
21130Sstevel@tonic-gate  * terms of percentage of the disk or by specifying the starting
21140Sstevel@tonic-gate  * cylinder and length in cylinders.
21150Sstevel@tonic-gate  */
2116251Slclee static int
2117251Slclee specify(uchar_t tsystid)
21180Sstevel@tonic-gate {
21195169Slclee 	int	i, j, percent = -1;
21200Sstevel@tonic-gate 	int	cyl, cylen, first_free, size_free;
21215169Slclee 	int	max_free;
21225169Slclee 	int	cyl_size;
21230Sstevel@tonic-gate 	struct ipart *partition[FD_NUMPART];
21240Sstevel@tonic-gate 
21255169Slclee 	cyl_size = heads * sectors;
21265169Slclee 
21275169Slclee 	/*
21285169Slclee 	 * make a local copy of the partition table
21295169Slclee 	 * and sort it into relsect order
21305169Slclee 	 */
21315169Slclee 	for (i = 0; i < FD_NUMPART; i++)
21325169Slclee 		partition[i] = &Table[i];
21335169Slclee 
21345169Slclee 	for (i = 0; i < FD_NUMPART - 1; i++) {
21355169Slclee 		if (partition[i]->systid == UNUSED)
21365169Slclee 			break;
21375169Slclee 		for (j = i + 1; j < FD_NUMPART; j++) {
21385169Slclee 			if (partition[j]->systid == UNUSED)
21395169Slclee 				break;
21405169Slclee 			if (lel(partition[j]->relsect) <
21415169Slclee 			    lel(partition[i]->relsect)) {
21425169Slclee 				struct ipart *temp = partition[i];
21435169Slclee 				partition[i] = partition[j];
21445169Slclee 				partition[j] = temp;
21455169Slclee 			}
21465169Slclee 		}
21475169Slclee 	}
21485169Slclee 
2149251Slclee 	(void) printf(Q_LINE);
2150251Slclee 	(void) printf(
2151251Slclee 	    "Specify the percentage of disk to use for this partition\n"
2152251Slclee 	    "(or type \"c\" to specify the size in cylinders). ");
2153251Slclee 	(void) gets(s);
21540Sstevel@tonic-gate 	rm_blanks(s);
21550Sstevel@tonic-gate 	if (s[0] != 'c') {	/* Specify size in percentage of disk */
21565169Slclee 		i = 0;
21575169Slclee 		while (s[i] != '\0') {
21585169Slclee 			if (s[i] < '0' || s[i] > '9') {
21595169Slclee 				(void) printf(E_LINE);
21605169Slclee 				(void) printf("Invalid percentage value "
21615169Slclee 				    "specified; retry the operation.");
21625169Slclee 				return (-1);
21635169Slclee 			}
21645169Slclee 			i++;
21655169Slclee 			if (i > 3) {
21665169Slclee 				(void) printf(E_LINE);
21675169Slclee 				(void) printf("Invalid percentage value "
21685169Slclee 				    "specified; retry the operation.");
21695169Slclee 				return (-1);
21705169Slclee 			}
21715169Slclee 		}
21725169Slclee 		if ((percent = atoi(s)) > 100) {
21735169Slclee 			(void) printf(E_LINE);
21745169Slclee 			(void) printf(
21755169Slclee 			    "Percentage value is too large. The value must be"
21765169Slclee 			    " between 1 and 100;\nretry the operation.\n");
21775169Slclee 			return (-1);
21780Sstevel@tonic-gate 		}
21795169Slclee 		if (percent < 1) {
21805169Slclee 			(void) printf(E_LINE);
21815169Slclee 			(void) printf(
21825169Slclee 			    "Percentage value is too small. The value must be"
21835169Slclee 			    " between 1 and 100;\nretry the operation.\n");
21845169Slclee 			return (-1);
21855169Slclee 		}
21865169Slclee 
21875169Slclee 
21885169Slclee 		if (percent == 100)
21895169Slclee 			cylen = Numcyl - 1;
21905169Slclee 		else
21915169Slclee 			cylen = (Numcyl * percent) / 100;
21925169Slclee 
21935169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
21945169Slclee 		if ((tsystid == DOSOS12) &&
21955169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
21965169Slclee 			int n;
21975169Slclee 			n = MAXDOS * 100 / (int)(cyl_size) / Numcyl;
21985169Slclee 			(void) printf(E_LINE);
21995169Slclee 			(void) printf("Maximum size for a DOS partition "
22005169Slclee 			    "is %d%%; retry the operation.",
22015169Slclee 			    n <= 100 ? n : 100);
22025169Slclee 			return (-1);
22030Sstevel@tonic-gate 		}
22045169Slclee 
22055169Slclee 
22065169Slclee 		max_free = 0;
22075169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
22085169Slclee 
22095169Slclee 			/*
22105169Slclee 			 * check for free space before partition i
22115169Slclee 			 * where i varies from 0 to 3
22125169Slclee 			 *
22135169Slclee 			 * freespace after partition 3 is unusable
22145169Slclee 			 * because there are no free partitions
22155169Slclee 			 *
22165169Slclee 			 * freespace begins at the end of previous partition
22175169Slclee 			 * or cylinder 1
22185169Slclee 			 */
22195169Slclee 			if (i) {
22205169Slclee 				/* Not an empty table */
22215169Slclee 				first_free = lel(partition[i - 1]->relsect) +
22225169Slclee 				    lel(partition[i - 1]->numsect);
22235169Slclee 			} else {
22245169Slclee 				first_free = cyl_size;
22255169Slclee 			}
22265169Slclee 
22275169Slclee 			/*
22285169Slclee 			 * freespace ends before the current partition
22295169Slclee 			 * or the end of the disk (chs end)
22305169Slclee 			 */
22315169Slclee 			if (partition[i]->systid == UNUSED) {
22325169Slclee 				size_free = chs_capacity - first_free;
22335169Slclee 			} else {
22345169Slclee 				size_free =
22355169Slclee 				    lel(partition[i]->relsect) - first_free;
22365169Slclee 			}
22375169Slclee 
22385169Slclee 			/* save largest free space */
22395169Slclee 			if (max_free < size_free)
22405169Slclee 				max_free = size_free;
22415169Slclee 
22425169Slclee 			if ((cylen * cyl_size) <= size_free) {
22435169Slclee 				/* We found a place to use */
22445169Slclee 				break;
22455169Slclee 			}
22465169Slclee 			if (partition[i]->systid == UNUSED) {
22475169Slclee 				(void) printf(E_LINE);
22485169Slclee 				max_free /= (cyl_size);
22495169Slclee 				(void) fprintf(stderr, "fdisk: "
22505169Slclee 				    "Maximum percentage available is %d\n",
22515169Slclee 				    100 * max_free / Numcyl);
22525169Slclee 				return (-1);
22535169Slclee 			}
22540Sstevel@tonic-gate 		}
22555169Slclee 
22565169Slclee 		(void) printf(E_LINE);
22575169Slclee 		if (i >= FD_NUMPART) {
22585169Slclee 			(void) fprintf(stderr,
22595169Slclee 			    "fdisk: Partition table is full.\n");
22605169Slclee 			return (-1);
22615169Slclee 		}
22625169Slclee 
22635169Slclee 		if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
22645169Slclee 		    first_free, cylen * cyl_size)) >= 0)  {
22655169Slclee 			return (i);
22665169Slclee 		}
22675169Slclee 		return (-1);
22685169Slclee 	} else {
22695169Slclee 
22705169Slclee 		/* Specifying size in cylinders */
2271251Slclee 		(void) printf(E_LINE);
22725169Slclee 		(void) printf(Q_LINE);
22735169Slclee 		(void) printf("Enter starting cylinder number: ");
22745169Slclee 		if ((cyl = getcyl()) == -1) {
22755169Slclee 			(void) printf(E_LINE);
22765169Slclee 			(void) printf("Invalid number; retry the operation.");
22775169Slclee 			return (-1);
22785169Slclee 		}
22795169Slclee 		if (cyl == 0) {
22805169Slclee 			(void) printf(E_LINE);
22815169Slclee 			(void) printf(
22825169Slclee 			    "New partition cannot start at cylinder 0.\n");
22835169Slclee 			return (-1);
22845169Slclee 		}
22855169Slclee 		if (cyl >= (unsigned int)Numcyl) {
22865169Slclee 			(void) printf(E_LINE);
22875169Slclee 			(void) printf(
22885169Slclee 			    "Cylinder %d is out of bounds, "
22895169Slclee 			    "the maximum is %d.\n",
22905169Slclee 			    cyl, Numcyl - 1);
22915169Slclee 			return (-1);
22925169Slclee 		}
22935169Slclee 		(void) printf(Q_LINE);
22945169Slclee 		(void) printf("Enter partition size in cylinders: ");
22955169Slclee 		if ((cylen = getcyl()) == -1) {
22965169Slclee 			(void) printf(E_LINE);
22975169Slclee 			(void) printf("Invalid number, retry the operation.");
22985169Slclee 			return (-1);
22995169Slclee 		}
23005169Slclee 
23015169Slclee 		for (i = 0; i < FD_NUMPART; i++) {
23025169Slclee 			uint32_t	t_relsect, t_numsect;
23035169Slclee 
23045169Slclee 			if (partition[i]->systid == UNUSED)
23055169Slclee 				break;
23065169Slclee 			t_relsect = lel(partition[i]->relsect);
23075169Slclee 			t_numsect = lel(partition[i]->numsect);
23085169Slclee 
23095169Slclee 			if (cyl * cyl_size >= t_relsect &&
23105169Slclee 			    cyl * cyl_size < t_relsect + t_numsect) {
23115169Slclee 				(void) printf(E_LINE);
23125169Slclee 				(void) printf(
23135169Slclee 				    "Cylinder %d is already allocated"
23145169Slclee 				    "\nretry the operation.",
23155169Slclee 				    cyl);
23165169Slclee 				return (-1);
23175169Slclee 			}
23185169Slclee 
23195169Slclee 			if (cyl * cyl_size < t_relsect &&
23205169Slclee 			    (cyl + cylen - 1) * cyl_size > t_relsect) {
23215169Slclee 				(void) printf(E_LINE);
23225169Slclee 				(void) printf(
23235169Slclee 				    "Maximum size for partition is %u cylinders"
23245169Slclee 				    "\nretry the operation.",
23255169Slclee 				    (t_relsect - cyl * cyl_size) / cyl_size);
23265169Slclee 				return (-1);
23275169Slclee 			}
23285169Slclee 		}
23295169Slclee 
23305169Slclee 		/* Verify partition doesn't exceed disk size */
23315169Slclee 		if (cyl + cylen > Numcyl) {
23325169Slclee 			(void) printf(E_LINE);
23335169Slclee 			(void) printf(
23345169Slclee 			    "Maximum size for partition is %d cylinders;"
23355169Slclee 			    "\nretry the operation.",
23365169Slclee 			    Numcyl - cyl);
23375169Slclee 			return (-1);
23385169Slclee 		}
23395169Slclee 
23405169Slclee 		/* Verify DOS12 partition doesn't exceed max size of 32MB. */
23415169Slclee 		if ((tsystid == DOSOS12) &&
23425169Slclee 		    ((long)((long)cylen * cyl_size) > MAXDOS)) {
23435169Slclee 			(void) printf(E_LINE);
23445169Slclee 			(void) printf(
23455169Slclee 			    "Maximum size for a %s partition is %ld cylinders;"
23465169Slclee 			    "\nretry the operation.",
23475169Slclee 			    Dstr, MAXDOS / (int)(cyl_size));
23485169Slclee 			return (-1);
23495169Slclee 		}
23505169Slclee 
2351251Slclee 		(void) printf(E_LINE);
23525169Slclee 		i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0,
23535169Slclee 		    cyl * cyl_size, cylen * cyl_size);
23545169Slclee 		if (i < 0)
23555169Slclee 			return (-1);
23565169Slclee 
23575169Slclee 		if (verify_tbl() < 0) {
23585169Slclee 			(void) printf(E_LINE);
23595169Slclee 			(void) printf("fdisk: Cannot create partition table\n");
23605169Slclee 			return (-1);
23615169Slclee 		}
23625169Slclee 
23635169Slclee 		return (i);
23640Sstevel@tonic-gate 	}
23650Sstevel@tonic-gate }
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate /*
23680Sstevel@tonic-gate  * dispmenu
23690Sstevel@tonic-gate  * Display command menu (interactive mode).
23700Sstevel@tonic-gate  */
2371251Slclee static void
2372251Slclee dispmenu(void)
23730Sstevel@tonic-gate {
2374251Slclee 	(void) printf(M_LINE);
2375251Slclee 	(void) printf(
2376251Slclee 	    "SELECT ONE OF THE FOLLOWING:\n"
2377251Slclee 	    "   1. Create a partition\n"
2378251Slclee 	    "   2. Specify the active partition\n"
2379251Slclee 	    "   3. Delete a partition\n"
2380251Slclee 	    "   4. Change between Solaris and Solaris2 Partition IDs\n"
2381251Slclee 	    "   5. Exit (update disk configuration and exit)\n"
2382251Slclee 	    "   6. Cancel (exit without updating disk configuration)\n");
23830Sstevel@tonic-gate }
23840Sstevel@tonic-gate 
23850Sstevel@tonic-gate /*
23860Sstevel@tonic-gate  * pchange
23870Sstevel@tonic-gate  * Change the ACTIVE designation of a partition.
23880Sstevel@tonic-gate  */
2389251Slclee static int
2390251Slclee pchange(void)
23910Sstevel@tonic-gate {
23920Sstevel@tonic-gate 	char s[80];
23930Sstevel@tonic-gate 	int i, j;
23940Sstevel@tonic-gate 
2395251Slclee 	for (;;) {
2396251Slclee 		(void) printf(Q_LINE);
23970Sstevel@tonic-gate 			{
2398251Slclee 			(void) printf(
2399251Slclee 			    "Specify the partition number to boot from"
24000Sstevel@tonic-gate 			    " (or specify 0 for none): ");
24010Sstevel@tonic-gate 			}
2402251Slclee 		(void) gets(s);
24030Sstevel@tonic-gate 		rm_blanks(s);
24040Sstevel@tonic-gate 		if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) {
2405251Slclee 			(void) printf(E_LINE);
2406251Slclee 			(void) printf(
2407251Slclee 			    "Invalid response, please specify a number"
24080Sstevel@tonic-gate 			    " between 0 and 4.\n");
24090Sstevel@tonic-gate 		} else {
24100Sstevel@tonic-gate 			break;
24110Sstevel@tonic-gate 		}
24120Sstevel@tonic-gate 	}
24130Sstevel@tonic-gate 	if (s[0] == '0') {	/* No active partitions */
24140Sstevel@tonic-gate 		for (i = 0; i < FD_NUMPART; i++) {
24150Sstevel@tonic-gate 			if (Table[i].systid != UNUSED &&
24160Sstevel@tonic-gate 			    Table[i].bootid == ACTIVE)
24170Sstevel@tonic-gate 				Table[i].bootid = 0;
24180Sstevel@tonic-gate 		}
2419251Slclee 		(void) printf(E_LINE);
2420251Slclee 			(void) printf(
2421251Slclee 			    "No partition is currently marked as active.");
24220Sstevel@tonic-gate 		return (0);
24230Sstevel@tonic-gate 	} else {	/* User has selected a partition to be active */
24240Sstevel@tonic-gate 		i = s[0] - '1';
24250Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2426251Slclee 			(void) printf(E_LINE);
2427251Slclee 			(void) printf("Partition does not exist.");
24280Sstevel@tonic-gate 			return (-1);
24290Sstevel@tonic-gate 		}
24300Sstevel@tonic-gate 		/* a DOS-DATA or EXT-DOS partition cannot be active */
24310Sstevel@tonic-gate 		else if ((Table[i].systid == DOSDATA) ||
24320Sstevel@tonic-gate 		    (Table[i].systid == EXTDOS) ||
24330Sstevel@tonic-gate 		    (Table[i].systid == FDISK_EXTLBA)) {
2434251Slclee 			(void) printf(E_LINE);
2435251Slclee 			(void) printf(
2436251Slclee 			    "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions "
24370Sstevel@tonic-gate 			    "cannot be made active.\n");
2438251Slclee 			(void) printf("Select another partition.");
24390Sstevel@tonic-gate 			return (-1);
24400Sstevel@tonic-gate 		}
24410Sstevel@tonic-gate 		Table[i].bootid = ACTIVE;
24420Sstevel@tonic-gate 		for (j = 0; j < FD_NUMPART; j++) {
24430Sstevel@tonic-gate 			if (j != i)
24440Sstevel@tonic-gate 			Table[j].bootid = 0;
24450Sstevel@tonic-gate 		}
24460Sstevel@tonic-gate 	}
2447251Slclee 	(void) printf(E_LINE);
24480Sstevel@tonic-gate 		{
2449251Slclee 		(void) printf(
2450251Slclee 		    "Partition %d is now active. The system will start up"
2451251Slclee 		    " from this\n", i + 1);
2452251Slclee 		(void) printf("partition after the next reboot.");
24530Sstevel@tonic-gate 		}
24540Sstevel@tonic-gate 	return (1);
24550Sstevel@tonic-gate }
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate /*
24580Sstevel@tonic-gate  * Change between SOLARIS and SOLARIS2 partition id
24590Sstevel@tonic-gate  */
2460251Slclee static int
2461251Slclee ppartid(void)
24620Sstevel@tonic-gate {
24630Sstevel@tonic-gate 	char	*p, s[80];
24640Sstevel@tonic-gate 	int	i;
24650Sstevel@tonic-gate 
24660Sstevel@tonic-gate 	for (;;) {
2467251Slclee 		(void) printf(Q_LINE);
2468251Slclee 		(void) printf("Specify the partition number to change"
24695169Slclee 		    " (or enter 0 to exit): ");
2470251Slclee 		if (!fgets(s, sizeof (s), stdin))
2471251Slclee 			return (1);
24720Sstevel@tonic-gate 		i = strtol(s, &p, 10);
24730Sstevel@tonic-gate 
24740Sstevel@tonic-gate 		if (*p != '\n' || i < 0 || i > FD_NUMPART) {
2475251Slclee 			(void) printf(E_LINE);
2476251Slclee 			(void) printf(
2477251Slclee 			    "Invalid response, retry the operation.\n");
24780Sstevel@tonic-gate 			continue;
24790Sstevel@tonic-gate 		}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 		if (i == 0) {
24820Sstevel@tonic-gate 			/* exit delete command */
2483251Slclee 			(void) printf(E_LINE); /* clear error message */
24840Sstevel@tonic-gate 			return (1);
24850Sstevel@tonic-gate 		}
24860Sstevel@tonic-gate 
24870Sstevel@tonic-gate 		i -= 1;
24880Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS) {
24890Sstevel@tonic-gate 			Table[i].systid = SUNIXOS2;
24900Sstevel@tonic-gate 		} else if (Table[i].systid == SUNIXOS2) {
24910Sstevel@tonic-gate 			Table[i].systid = SUNIXOS;
24920Sstevel@tonic-gate 		} else {
2493251Slclee 			(void) printf(E_LINE);
2494251Slclee 			(void) printf(
2495251Slclee 			    "Partition %d is not a Solaris partition.",
24960Sstevel@tonic-gate 			    i + 1);
24970Sstevel@tonic-gate 			continue;
24980Sstevel@tonic-gate 		}
24990Sstevel@tonic-gate 
2500251Slclee 		(void) printf(E_LINE);
2501251Slclee 		(void) printf("Partition %d has been changed.", i + 1);
25020Sstevel@tonic-gate 		return (1);
25030Sstevel@tonic-gate 	}
25040Sstevel@tonic-gate }
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate /*
25070Sstevel@tonic-gate  * pdelete
25080Sstevel@tonic-gate  * Remove partition entry from the table (interactive mode).
25090Sstevel@tonic-gate  */
2510251Slclee static char
2511251Slclee pdelete(void)
25120Sstevel@tonic-gate {
25130Sstevel@tonic-gate 	char s[80];
25140Sstevel@tonic-gate 	int i, j;
25150Sstevel@tonic-gate 	char pactive;
25160Sstevel@tonic-gate 
2517251Slclee DEL1:	(void) printf(Q_LINE);
2518251Slclee 	(void) printf("Specify the partition number to delete"
25190Sstevel@tonic-gate 	    " (or enter 0 to exit): ");
2520251Slclee 	(void) gets(s);
25210Sstevel@tonic-gate 	rm_blanks(s);
25220Sstevel@tonic-gate 	if ((s[0] == '0')) {	/* exit delete command */
2523251Slclee 		(void) printf(E_LINE);	/* clear error message */
25240Sstevel@tonic-gate 		return (1);
25250Sstevel@tonic-gate 	}
25260Sstevel@tonic-gate 	/* Accept only a single digit between 1 and 4 */
25270Sstevel@tonic-gate 	if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) {
2528251Slclee 		(void) printf(E_LINE);
2529251Slclee 		(void) printf("Invalid response, retry the operation.\n");
25300Sstevel@tonic-gate 		goto DEL1;
25310Sstevel@tonic-gate 	} else {		/* Found a digit between 1 and 4 */
25320Sstevel@tonic-gate 		--i;	/* Structure begins with element 0 */
25330Sstevel@tonic-gate 	}
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	if (Table[i].systid == UNUSED) {
2536251Slclee 		(void) printf(E_LINE);
2537251Slclee 		(void) printf("Partition %d does not exist.", i + 1);
25380Sstevel@tonic-gate 		return (-1);
25390Sstevel@tonic-gate 	}
25400Sstevel@tonic-gate 
2541251Slclee 	(void) printf(Q_LINE);
2542251Slclee 	(void) printf("Are you sure you want to delete partition %d?"
2543251Slclee 	    " This will make all files and \n", i + 1);
2544251Slclee 	(void) printf("programs in this partition inaccessible (type"
25450Sstevel@tonic-gate 	    " \"y\" or \"n\"). ");
25460Sstevel@tonic-gate 
2547251Slclee 	(void) printf(E_LINE);
25480Sstevel@tonic-gate 	if (! yesno()) {
25490Sstevel@tonic-gate 		return (1);
25500Sstevel@tonic-gate 	}
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	if (Table[i].bootid == ACTIVE) {
25530Sstevel@tonic-gate 		pactive = 1;
25540Sstevel@tonic-gate 	} else {
25550Sstevel@tonic-gate 		pactive = 0;
25560Sstevel@tonic-gate 	}
25570Sstevel@tonic-gate 
25580Sstevel@tonic-gate 	for (j = i; j < FD_NUMPART - 1; j++) {
25595169Slclee 		Table[j] = Table[j + 1];
25600Sstevel@tonic-gate 	}
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	Table[j].systid = UNUSED;
25630Sstevel@tonic-gate 	Table[j].numsect = 0;
25640Sstevel@tonic-gate 	Table[j].relsect = 0;
25650Sstevel@tonic-gate 	Table[j].bootid = 0;
2566251Slclee 	(void) printf(E_LINE);
2567251Slclee 	(void) printf("Partition %d has been deleted.", i + 1);
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate 	if (pactive) {
25705169Slclee 		(void) printf(" This was the active partition.");
25710Sstevel@tonic-gate 	}
25720Sstevel@tonic-gate 
25730Sstevel@tonic-gate 	return (1);
25740Sstevel@tonic-gate }
25750Sstevel@tonic-gate 
25760Sstevel@tonic-gate /*
25770Sstevel@tonic-gate  * rm_blanks
25780Sstevel@tonic-gate  * Remove blanks from strings of user responses.
25790Sstevel@tonic-gate  */
2580251Slclee static void
2581251Slclee rm_blanks(char *s)
25820Sstevel@tonic-gate {
25830Sstevel@tonic-gate 	register int i, j;
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	for (i = 0; i < CBUFLEN; i++) {
25860Sstevel@tonic-gate 		if ((s[i] == ' ') || (s[i] == '\t'))
25870Sstevel@tonic-gate 			continue;
25880Sstevel@tonic-gate 		else
25890Sstevel@tonic-gate 			/* Found first non-blank character of the string */
25900Sstevel@tonic-gate 			break;
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 	for (j = 0; i < CBUFLEN; j++, i++) {
25930Sstevel@tonic-gate 		if ((s[j] = s[i]) == '\0') {
25940Sstevel@tonic-gate 			/* Reached end of string */
25950Sstevel@tonic-gate 			return;
25960Sstevel@tonic-gate 		}
25970Sstevel@tonic-gate 	}
25980Sstevel@tonic-gate }
25990Sstevel@tonic-gate 
26000Sstevel@tonic-gate /*
26010Sstevel@tonic-gate  * getcyl
26020Sstevel@tonic-gate  * Take the user-specified cylinder number and convert it from a
26030Sstevel@tonic-gate  * string to a decimal value.
26040Sstevel@tonic-gate  */
2605251Slclee static int
2606251Slclee getcyl(void)
26070Sstevel@tonic-gate {
26080Sstevel@tonic-gate int slen, i, j;
26090Sstevel@tonic-gate unsigned int cyl;
2610251Slclee 	(void) gets(s);
26110Sstevel@tonic-gate 	rm_blanks(s);
26120Sstevel@tonic-gate 	slen = strlen(s);
26130Sstevel@tonic-gate 	j = 1;
26140Sstevel@tonic-gate 	cyl = 0;
2615251Slclee 	for (i = slen - 1; i >= 0; i--) {
26160Sstevel@tonic-gate 		if (s[i] < '0' || s[i] > '9') {
26170Sstevel@tonic-gate 			return (-1);
26180Sstevel@tonic-gate 		}
2619251Slclee 		cyl += (j * (s[i] - '0'));
26200Sstevel@tonic-gate 		j *= 10;
26210Sstevel@tonic-gate 	}
26220Sstevel@tonic-gate 	return (cyl);
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate /*
26260Sstevel@tonic-gate  * disptbl
26270Sstevel@tonic-gate  * Display the current fdisk table; determine percentage
26280Sstevel@tonic-gate  * of the disk used for each partition.
26290Sstevel@tonic-gate  */
2630251Slclee static void
2631251Slclee disptbl(void)
26320Sstevel@tonic-gate {
26330Sstevel@tonic-gate 	int i;
26340Sstevel@tonic-gate 	unsigned int startcyl, endcyl, length, percent, remainder;
26350Sstevel@tonic-gate 	char *stat, *type;
26360Sstevel@tonic-gate 
26370Sstevel@tonic-gate 	if ((heads == 0) || (sectors == 0)) {
2638251Slclee 		(void) printf("WARNING: critical disk geometry information"
26395169Slclee 		    " missing!\n");
2640251Slclee 		(void) printf("\theads = %d, sectors = %d\n", heads, sectors);
26410Sstevel@tonic-gate 		exit(1);
26420Sstevel@tonic-gate 	}
26430Sstevel@tonic-gate 
2644251Slclee 	(void) printf(HOME);
2645251Slclee 	(void) printf(T_LINE);
2646251Slclee 	(void) printf("             Total disk size is %d cylinders\n", Numcyl);
2647251Slclee 	(void) printf("             Cylinder size is %d (512 byte) blocks\n\n",
2648251Slclee 	    heads * sectors);
2649251Slclee 	(void) printf(
2650251Slclee 	    "                                               Cylinders\n");
2651251Slclee 	(void) printf(
2652251Slclee 	    "      Partition   Status    Type          Start   End   Length"
26530Sstevel@tonic-gate 	    "    %%\n");
2654251Slclee 	(void) printf(
2655251Slclee 	    "      =========   ======    ============  =====   ===   ======"
26560Sstevel@tonic-gate 	    "   ===");
26570Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
26580Sstevel@tonic-gate 		if (Table[i].systid == UNUSED) {
2659251Slclee 			(void) printf("\n");
2660251Slclee 			(void) printf(CLR_LIN);
26610Sstevel@tonic-gate 			continue;
26620Sstevel@tonic-gate 		}
26630Sstevel@tonic-gate 		if (Table[i].bootid == ACTIVE)
26645169Slclee 			stat = Actvstr;
26650Sstevel@tonic-gate 		else
26665169Slclee 			stat = NAstr;
26670Sstevel@tonic-gate 		switch (Table[i].systid) {
26680Sstevel@tonic-gate 		case UNIXOS:
26695169Slclee 			type = Ustr;
26705169Slclee 			break;
26710Sstevel@tonic-gate 		case SUNIXOS:
26725169Slclee 			type = SUstr;
26735169Slclee 			break;
26740Sstevel@tonic-gate 		case SUNIXOS2:
26755169Slclee 			type = SU2str;
26765169Slclee 			break;
26770Sstevel@tonic-gate 		case X86BOOT:
26785169Slclee 			type = X86str;
26795169Slclee 			break;
26800Sstevel@tonic-gate 		case DOSOS12:
26815169Slclee 			type = Dstr;
26825169Slclee 			break;
26830Sstevel@tonic-gate 		case DOSOS16:
26845169Slclee 			type = D16str;
26855169Slclee 			break;
26860Sstevel@tonic-gate 		case EXTDOS:
26875169Slclee 			type = EDstr;
26885169Slclee 			break;
26890Sstevel@tonic-gate 		case DOSDATA:
26905169Slclee 			type = DDstr;
26915169Slclee 			break;
26920Sstevel@tonic-gate 		case DOSHUGE:
26935169Slclee 			type = DBstr;
26945169Slclee 			break;
26950Sstevel@tonic-gate 		case PCIXOS:
26965169Slclee 			type = PCstr;
26975169Slclee 			break;
26980Sstevel@tonic-gate 		case DIAGPART:
26995169Slclee 			type = DIAGstr;
27005169Slclee 			break;
27010Sstevel@tonic-gate 		case FDISK_IFS:
27025169Slclee 			type = IFSstr;
27035169Slclee 			break;
27040Sstevel@tonic-gate 		case FDISK_AIXBOOT:
27055169Slclee 			type = AIXstr;
27065169Slclee 			break;
27070Sstevel@tonic-gate 		case FDISK_AIXDATA:
27085169Slclee 			type = AIXDstr;
27095169Slclee 			break;
27100Sstevel@tonic-gate 		case FDISK_OS2BOOT:
27115169Slclee 			type = OS2str;
27125169Slclee 			break;
27130Sstevel@tonic-gate 		case FDISK_WINDOWS:
27145169Slclee 			type = WINstr;
27155169Slclee 			break;
27160Sstevel@tonic-gate 		case FDISK_EXT_WIN:
27175169Slclee 			type = EWINstr;
27185169Slclee 			break;
27190Sstevel@tonic-gate 		case FDISK_FAT95:
27205169Slclee 			type = FAT95str;
27215169Slclee 			break;
27220Sstevel@tonic-gate 		case FDISK_EXTLBA:
27235169Slclee 			type = EXTLstr;
27245169Slclee 			break;
27250Sstevel@tonic-gate 		case FDISK_LINUX:
27265169Slclee 			type = LINUXstr;
27275169Slclee 			break;
27280Sstevel@tonic-gate 		case FDISK_CPM:
27295169Slclee 			type = CPMstr;
27305169Slclee 			break;
27310Sstevel@tonic-gate 		case FDISK_NOVELL3:
27325169Slclee 			type = NOVstr;
27335169Slclee 			break;
27340Sstevel@tonic-gate 		case FDISK_QNX4:
27355169Slclee 			type = QNXstr;
27365169Slclee 			break;
27370Sstevel@tonic-gate 		case FDISK_QNX42:
27385169Slclee 			type = QNX2str;
27395169Slclee 			break;
27400Sstevel@tonic-gate 		case FDISK_QNX43:
27415169Slclee 			type = QNX3str;
27425169Slclee 			break;
27430Sstevel@tonic-gate 		case FDISK_LINUXNAT:
27445169Slclee 			type = LINNATstr;
27455169Slclee 			break;
27460Sstevel@tonic-gate 		case FDISK_NTFSVOL1:
27475169Slclee 			type = NTFSVOL1str;
27485169Slclee 			break;
27490Sstevel@tonic-gate 		case FDISK_NTFSVOL2:
27505169Slclee 			type = NTFSVOL2str;
27515169Slclee 			break;
27520Sstevel@tonic-gate 		case FDISK_BSD:
27535169Slclee 			type = BSDstr;
27545169Slclee 			break;
27550Sstevel@tonic-gate 		case FDISK_NEXTSTEP:
27565169Slclee 			type = NEXTSTEPstr;
27575169Slclee 			break;
27580Sstevel@tonic-gate 		case FDISK_BSDIFS:
27595169Slclee 			type = BSDIFSstr;
27605169Slclee 			break;
27610Sstevel@tonic-gate 		case FDISK_BSDISWAP:
27625169Slclee 			type = BSDISWAPstr;
27635169Slclee 			break;
27640Sstevel@tonic-gate 		case EFI_PMBR:
27655169Slclee 			type = EFIstr;
27665169Slclee 			break;
27670Sstevel@tonic-gate 		default:
27685169Slclee 			type = Ostr;
27695169Slclee 			break;
27700Sstevel@tonic-gate 		}
27715936Sbharding 		startcyl = lel(Table[i].relsect) /
27725936Sbharding 		    (unsigned long)(heads * sectors);
27735936Sbharding 		length = lel(Table[i].numsect) /
27745936Sbharding 		    (unsigned long)(heads * sectors);
27755936Sbharding 		if (lel(Table[i].numsect) % (unsigned long)(heads * sectors))
27760Sstevel@tonic-gate 			length++;
27770Sstevel@tonic-gate 		endcyl = startcyl + length - 1;
27780Sstevel@tonic-gate 		percent = length * 100 / Numcyl;
2779251Slclee 		if ((remainder = (length * 100 % Numcyl)) != 0) {
27800Sstevel@tonic-gate 			if ((remainder * 100 / Numcyl) > 50) {
27810Sstevel@tonic-gate 				/* round up */
27820Sstevel@tonic-gate 				percent++;
27830Sstevel@tonic-gate 			}
27840Sstevel@tonic-gate 			/* Else leave the percent as is since it's already */
27850Sstevel@tonic-gate 			/* rounded down */
27860Sstevel@tonic-gate 		}
27870Sstevel@tonic-gate 		if (percent > 100)
27880Sstevel@tonic-gate 			percent = 100;
2789251Slclee 		(void) printf(
2790251Slclee 		    "\n          %d       %s    %-12.12s   %4d  %4d    %4d"
2791251Slclee 		    "    %3d",
2792251Slclee 		    i + 1, stat, type, startcyl, endcyl, length, percent);
27930Sstevel@tonic-gate 	}
27940Sstevel@tonic-gate 	/* Print warning message if table is empty */
27950Sstevel@tonic-gate 	if (Table[0].systid == UNUSED) {
2796251Slclee 		(void) printf(W_LINE);
2797251Slclee 		(void) printf("WARNING: no partitions are defined!");
27980Sstevel@tonic-gate 	} else {
27990Sstevel@tonic-gate 		/* Clear the warning line */
2800251Slclee 		(void) printf(W_LINE);
28010Sstevel@tonic-gate 	}
28020Sstevel@tonic-gate }
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate /*
28050Sstevel@tonic-gate  * print_Table
28060Sstevel@tonic-gate  * Write the detailed fdisk table to standard error for
28070Sstevel@tonic-gate  * the selected disk device.
28080Sstevel@tonic-gate  */
2809251Slclee static void
2810251Slclee print_Table(void)
2811251Slclee {
28120Sstevel@tonic-gate 	int i;
28130Sstevel@tonic-gate 
2814251Slclee 	(void) fprintf(stderr,
28150Sstevel@tonic-gate 	    "  SYSID ACT BHEAD BSECT BEGCYL   EHEAD ESECT ENDCYL   RELSECT"
28160Sstevel@tonic-gate 	    "   NUMSECT\n");
28170Sstevel@tonic-gate 
28180Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
2819251Slclee 		(void) fprintf(stderr, "  %-5d ", Table[i].systid);
2820251Slclee 		(void) fprintf(stderr, "%-3d ", Table[i].bootid);
2821251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].beghead);
2822251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f);
28235169Slclee 		(void) fprintf(stderr, "%-8d ",
28245169Slclee 		    (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl);
28250Sstevel@tonic-gate 
2826251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endhead);
2827251Slclee 		(void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f);
28285169Slclee 		(void) fprintf(stderr, "%-8d ",
28295169Slclee 		    (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl);
2830251Slclee 		(void) fprintf(stderr, "%-9d ", lel(Table[i].relsect));
2831251Slclee 		(void) fprintf(stderr, "%-9d\n", lel(Table[i].numsect));
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	}
28340Sstevel@tonic-gate }
28350Sstevel@tonic-gate 
28360Sstevel@tonic-gate /*
28370Sstevel@tonic-gate  * copy_Table_to_Old_Table
28380Sstevel@tonic-gate  * Copy Table into Old_Table. The function only copies the systid,
28390Sstevel@tonic-gate  * numsect, relsect, and bootid values because they are the only
28400Sstevel@tonic-gate  * ones compared when determining if Table has changed.
28410Sstevel@tonic-gate  */
2842251Slclee static void
2843251Slclee copy_Table_to_Old_Table(void)
28440Sstevel@tonic-gate {
28450Sstevel@tonic-gate 	int i;
28460Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
28475169Slclee 		(void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0]));
28480Sstevel@tonic-gate 	}
28490Sstevel@tonic-gate }
28500Sstevel@tonic-gate 
28510Sstevel@tonic-gate /*
28520Sstevel@tonic-gate  * nulltbl
28530Sstevel@tonic-gate  * Zero out the systid, numsect, relsect, and bootid values in the
28540Sstevel@tonic-gate  * fdisk table.
28550Sstevel@tonic-gate  */
2856251Slclee static void
2857251Slclee nulltbl(void)
28580Sstevel@tonic-gate {
28590Sstevel@tonic-gate 	int i;
28600Sstevel@tonic-gate 
28610Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++)  {
28625169Slclee 		Table[i].systid = UNUSED;
28635169Slclee 		Table[i].numsect = lel(UNUSED);
28645169Slclee 		Table[i].relsect = lel(UNUSED);
28655169Slclee 		Table[i].bootid = 0;
28660Sstevel@tonic-gate 	}
28670Sstevel@tonic-gate }
28680Sstevel@tonic-gate 
28690Sstevel@tonic-gate /*
28700Sstevel@tonic-gate  * copy_Bootblk_to_Table
28710Sstevel@tonic-gate  * Copy the bytes from the boot record to an internal "Table".
28720Sstevel@tonic-gate  * All unused are padded with zeros starting at offset 446.
28730Sstevel@tonic-gate  */
2874251Slclee static void
2875251Slclee copy_Bootblk_to_Table(void)
28760Sstevel@tonic-gate {
28770Sstevel@tonic-gate 	int i, j;
28780Sstevel@tonic-gate 	char *bootptr;
28790Sstevel@tonic-gate 	struct ipart iparts[FD_NUMPART];
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	/* Get an aligned copy of the partition tables */
2882251Slclee 	(void) memcpy(iparts, Bootblk->parts, sizeof (iparts));
28830Sstevel@tonic-gate 	bootptr = (char *)iparts;	/* Points to start of partition table */
28840Sstevel@tonic-gate 	if (les(Bootblk->signature) != MBB_MAGIC)  {
28850Sstevel@tonic-gate 		/* Signature is missing */
28860Sstevel@tonic-gate 		nulltbl();
2887251Slclee 		(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
28880Sstevel@tonic-gate 		return;
28890Sstevel@tonic-gate 	}
28900Sstevel@tonic-gate 	/*
28910Sstevel@tonic-gate 	 * When the DOS fdisk command deletes a partition, it is not
28920Sstevel@tonic-gate 	 * recognized by the old algorithm.  The algorithm that
28930Sstevel@tonic-gate 	 * follows looks at each entry in the Bootrec and copies all
28940Sstevel@tonic-gate 	 * those that are valid.
28950Sstevel@tonic-gate 	 */
28960Sstevel@tonic-gate 	j = 0;
28970Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
28980Sstevel@tonic-gate 		if (iparts[i].systid == 0) {
28990Sstevel@tonic-gate 			/* Null entry */
29000Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29010Sstevel@tonic-gate 		} else {
2902251Slclee 			fill_ipart(bootptr, &Table[j]);
29030Sstevel@tonic-gate 			j++;
29040Sstevel@tonic-gate 			bootptr += sizeof (struct ipart);
29050Sstevel@tonic-gate 		}
29060Sstevel@tonic-gate 	}
29070Sstevel@tonic-gate 	for (i = j; i < FD_NUMPART; i++) {
29080Sstevel@tonic-gate 		Table[i].systid = UNUSED;
29090Sstevel@tonic-gate 		Table[i].numsect = lel(UNUSED);
29100Sstevel@tonic-gate 		Table[i].relsect = lel(UNUSED);
29110Sstevel@tonic-gate 		Table[i].bootid = 0;
29120Sstevel@tonic-gate 
29130Sstevel@tonic-gate 	}
29140Sstevel@tonic-gate 	/* For now, always replace the bootcode with ours */
2915251Slclee 	(void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ);
29160Sstevel@tonic-gate 	copy_Table_to_Bootblk();
29170Sstevel@tonic-gate }
29180Sstevel@tonic-gate 
29190Sstevel@tonic-gate /*
29200Sstevel@tonic-gate  * fill_ipart
29210Sstevel@tonic-gate  * Initialize ipart structure values.
29220Sstevel@tonic-gate  */
2923251Slclee static void
29240Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp)
29250Sstevel@tonic-gate {
29260Sstevel@tonic-gate #ifdef sparc
29270Sstevel@tonic-gate 	/* Packing struct ipart for Sparc */
2928251Slclee 	partp->bootid	= getbyte(&bootptr);
2929251Slclee 	partp->beghead	= getbyte(&bootptr);
2930251Slclee 	partp->begsect	= getbyte(&bootptr);
2931251Slclee 	partp->begcyl	= getbyte(&bootptr);
2932251Slclee 	partp->systid	= getbyte(&bootptr);
2933251Slclee 	partp->endhead	= getbyte(&bootptr);
2934251Slclee 	partp->endsect	= getbyte(&bootptr);
2935251Slclee 	partp->endcyl	= getbyte(&bootptr);
2936251Slclee 	partp->relsect	= (int32_t)getlong(&bootptr);
2937251Slclee 	partp->numsect	= (int32_t)getlong(&bootptr);
29380Sstevel@tonic-gate #else
29390Sstevel@tonic-gate 	*partp = *(struct ipart *)bootptr;
29400Sstevel@tonic-gate #endif
29410Sstevel@tonic-gate }
29420Sstevel@tonic-gate 
29430Sstevel@tonic-gate /*
2944251Slclee  * getbyte, getlong
29450Sstevel@tonic-gate  * 	Get a byte, a short, or a long (SPARC only).
29460Sstevel@tonic-gate  */
29470Sstevel@tonic-gate #ifdef sparc
2948251Slclee uchar_t
2949251Slclee getbyte(char **bp)
29500Sstevel@tonic-gate {
2951251Slclee 	uchar_t	b;
2952251Slclee 
2953251Slclee 	b = (uchar_t)**bp;
29540Sstevel@tonic-gate 	*bp = *bp + 1;
29550Sstevel@tonic-gate 	return (b);
29560Sstevel@tonic-gate }
29570Sstevel@tonic-gate 
2958251Slclee uint32_t
2959251Slclee getlong(char **bp)
29600Sstevel@tonic-gate {
2961251Slclee 	int32_t	b, bh, bl;
29620Sstevel@tonic-gate 
29630Sstevel@tonic-gate 	bh = ((**bp) << 8) | *(*bp + 1);
29640Sstevel@tonic-gate 	*bp += 2;
29650Sstevel@tonic-gate 	bl = ((**bp) << 8) | *(*bp + 1);
29660Sstevel@tonic-gate 	*bp += 2;
29670Sstevel@tonic-gate 
29680Sstevel@tonic-gate 	b = (bh << 16) | bl;
2969251Slclee 	return ((uint32_t)b);
29700Sstevel@tonic-gate }
29710Sstevel@tonic-gate #endif
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate /*
29740Sstevel@tonic-gate  * copy_Table_to_Bootblk
29750Sstevel@tonic-gate  * Copy the table into the 512 boot record. Note that the unused
29760Sstevel@tonic-gate  * entries will always be the last ones in the table and they are
29770Sstevel@tonic-gate  * marked with 100 in sysind. The the unused portion of the table
29780Sstevel@tonic-gate  * is padded with zeros in the bytes after the used entries.
29790Sstevel@tonic-gate  */
2980251Slclee static void
2981251Slclee copy_Table_to_Bootblk(void)
29820Sstevel@tonic-gate {
29830Sstevel@tonic-gate 	struct ipart *boot_ptr, *tbl_ptr;
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	boot_ptr = (struct ipart *)Bootblk->parts;
29860Sstevel@tonic-gate 	tbl_ptr = (struct ipart *)&Table[0].bootid;
29870Sstevel@tonic-gate 	for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid;
29880Sstevel@tonic-gate 	    tbl_ptr++, boot_ptr++) {
29895169Slclee 		if (tbl_ptr->systid == UNUSED)
29905169Slclee 			(void) memset(boot_ptr, 0, sizeof (struct ipart));
29915169Slclee 		else
29925169Slclee 			(void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart));
29930Sstevel@tonic-gate 	}
29940Sstevel@tonic-gate 	Bootblk->signature = les(MBB_MAGIC);
29950Sstevel@tonic-gate }
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate /*
29980Sstevel@tonic-gate  * TableChanged
29990Sstevel@tonic-gate  * 	Check for any changes in the partition table.
30000Sstevel@tonic-gate  */
3001251Slclee static int
3002251Slclee TableChanged(void)
30030Sstevel@tonic-gate {
30040Sstevel@tonic-gate 	int i, changed;
30050Sstevel@tonic-gate 
30060Sstevel@tonic-gate 	changed = 0;
30070Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
30085169Slclee 		if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) {
30095169Slclee 			/* Partition table changed, write back to disk */
30105169Slclee 			changed = 1;
30115169Slclee 		}
30120Sstevel@tonic-gate 	}
30130Sstevel@tonic-gate 
30140Sstevel@tonic-gate 	return (changed);
30150Sstevel@tonic-gate }
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate /*
30180Sstevel@tonic-gate  * ffile_write
30190Sstevel@tonic-gate  * 	Display contents of partition table to standard output or
30200Sstevel@tonic-gate  *	another file name without writing it to the disk (-W file).
30210Sstevel@tonic-gate  */
3022251Slclee static void
3023251Slclee ffile_write(char *file)
30240Sstevel@tonic-gate {
30250Sstevel@tonic-gate 	register int	i;
30260Sstevel@tonic-gate 	FILE *fp;
30270Sstevel@tonic-gate 
30280Sstevel@tonic-gate 	/*
30290Sstevel@tonic-gate 	 * If file isn't standard output, then it's a file name.
30300Sstevel@tonic-gate 	 * Open file and write it.
30310Sstevel@tonic-gate 	 */
30320Sstevel@tonic-gate 	if (file != (char *)stdout) {
30335169Slclee 		if ((fp = fopen(file, "w")) == NULL) {
30345169Slclee 			(void) fprintf(stderr,
30355169Slclee 			    "fdisk: Cannot open output file %s.\n",
30365169Slclee 			    file);
30375169Slclee 			exit(1);
30385169Slclee 		}
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 	else
30415169Slclee 		fp = stdout;
30420Sstevel@tonic-gate 
30430Sstevel@tonic-gate 	/*
30440Sstevel@tonic-gate 	 * Write the fdisk table information
30450Sstevel@tonic-gate 	 */
3046251Slclee 	(void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev);
3047251Slclee 	(void) fprintf(fp, "* Dimensions:\n");
3048251Slclee 	(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3049251Slclee 	(void) fprintf(fp, "*   %4d sectors/track\n", sectors);
3050251Slclee 	(void) fprintf(fp, "*   %4d tracks/cylinder\n", heads);
3051251Slclee 	(void) fprintf(fp, "*   %4d cylinders\n", Numcyl);
3052251Slclee 	(void) fprintf(fp, "*\n");
30530Sstevel@tonic-gate 	/* Write virtual (HBA) geometry, if required	*/
30540Sstevel@tonic-gate 	if (v_flag) {
3055251Slclee 		(void) fprintf(fp, "* HBA Dimensions:\n");
3056251Slclee 		(void) fprintf(fp, "*   %4d bytes/sector\n", sectsiz);
3057251Slclee 		(void) fprintf(fp, "*   %4d sectors/track\n", hba_sectors);
3058251Slclee 		(void) fprintf(fp, "*   %4d tracks/cylinder\n", hba_heads);
3059251Slclee 		(void) fprintf(fp, "*   %4d cylinders\n", hba_Numcyl);
3060251Slclee 		(void) fprintf(fp, "*\n");
30610Sstevel@tonic-gate 	}
3062251Slclee 	(void) fprintf(fp, "* systid:\n");
3063251Slclee 	(void) fprintf(fp, "*    1: DOSOS12\n");
3064251Slclee 	(void) fprintf(fp, "*    2: PCIXOS\n");
3065251Slclee 	(void) fprintf(fp, "*    4: DOSOS16\n");
3066251Slclee 	(void) fprintf(fp, "*    5: EXTDOS\n");
3067251Slclee 	(void) fprintf(fp, "*    6: DOSBIG\n");
3068251Slclee 	(void) fprintf(fp, "*    7: FDISK_IFS\n");
3069251Slclee 	(void) fprintf(fp, "*    8: FDISK_AIXBOOT\n");
3070251Slclee 	(void) fprintf(fp, "*    9: FDISK_AIXDATA\n");
3071251Slclee 	(void) fprintf(fp, "*   10: FDISK_0S2BOOT\n");
3072251Slclee 	(void) fprintf(fp, "*   11: FDISK_WINDOWS\n");
3073251Slclee 	(void) fprintf(fp, "*   12: FDISK_EXT_WIN\n");
3074251Slclee 	(void) fprintf(fp, "*   14: FDISK_FAT95\n");
3075251Slclee 	(void) fprintf(fp, "*   15: FDISK_EXTLBA\n");
3076251Slclee 	(void) fprintf(fp, "*   18: DIAGPART\n");
3077251Slclee 	(void) fprintf(fp, "*   65: FDISK_LINUX\n");
3078251Slclee 	(void) fprintf(fp, "*   82: FDISK_CPM\n");
3079251Slclee 	(void) fprintf(fp, "*   86: DOSDATA\n");
3080251Slclee 	(void) fprintf(fp, "*   98: OTHEROS\n");
3081251Slclee 	(void) fprintf(fp, "*   99: UNIXOS\n");
3082251Slclee 	(void) fprintf(fp, "*  101: FDISK_NOVELL3\n");
3083251Slclee 	(void) fprintf(fp, "*  119: FDISK_QNX4\n");
3084251Slclee 	(void) fprintf(fp, "*  120: FDISK_QNX42\n");
3085251Slclee 	(void) fprintf(fp, "*  121: FDISK_QNX43\n");
3086251Slclee 	(void) fprintf(fp, "*  130: SUNIXOS\n");
3087251Slclee 	(void) fprintf(fp, "*  131: FDISK_LINUXNAT\n");
3088251Slclee 	(void) fprintf(fp, "*  134: FDISK_NTFSVOL1\n");
3089251Slclee 	(void) fprintf(fp, "*  135: FDISK_NTFSVOL2\n");
3090251Slclee 	(void) fprintf(fp, "*  165: FDISK_BSD\n");
3091251Slclee 	(void) fprintf(fp, "*  167: FDISK_NEXTSTEP\n");
3092251Slclee 	(void) fprintf(fp, "*  183: FDISK_BSDIFS\n");
3093251Slclee 	(void) fprintf(fp, "*  184: FDISK_BSDISWAP\n");
3094251Slclee 	(void) fprintf(fp, "*  190: X86BOOT\n");
3095251Slclee 	(void) fprintf(fp, "*  191: SUNIXOS2\n");
3096251Slclee 	(void) fprintf(fp, "*  238: EFI_PMBR\n");
3097251Slclee 	(void) fprintf(fp, "*  239: EFI_FS\n");
3098251Slclee 	(void) fprintf(fp, "*\n");
3099251Slclee 	(void) fprintf(fp,
31000Sstevel@tonic-gate 	    "\n* Id    Act  Bhead  Bsect  Bcyl    Ehead  Esect  Ecyl"
31010Sstevel@tonic-gate 	    "    Rsect    Numsect\n");
31020Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31030Sstevel@tonic-gate 		if (Table[i].systid != UNUSED)
3104251Slclee 			(void) fprintf(fp,
31050Sstevel@tonic-gate 			    "  %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-8d"
31060Sstevel@tonic-gate 			    " %-8d\n",
31070Sstevel@tonic-gate 			    Table[i].systid,
31080Sstevel@tonic-gate 			    Table[i].bootid,
31090Sstevel@tonic-gate 			    Table[i].beghead,
31100Sstevel@tonic-gate 			    Table[i].begsect & 0x3f,
31110Sstevel@tonic-gate 			    ((Table[i].begcyl & 0xff) | ((Table[i].begsect &
31125169Slclee 			    0xc0) << 2)),
31130Sstevel@tonic-gate 			    Table[i].endhead,
31140Sstevel@tonic-gate 			    Table[i].endsect & 0x3f,
31150Sstevel@tonic-gate 			    ((Table[i].endcyl & 0xff) | ((Table[i].endsect &
31165169Slclee 			    0xc0) << 2)),
31170Sstevel@tonic-gate 			    lel(Table[i].relsect),
31180Sstevel@tonic-gate 			    lel(Table[i].numsect));
31190Sstevel@tonic-gate 	}
31200Sstevel@tonic-gate 	if (fp != stdout)
3121251Slclee 		(void) fclose(fp);
31220Sstevel@tonic-gate }
31230Sstevel@tonic-gate 
31240Sstevel@tonic-gate /*
31250Sstevel@tonic-gate  * fix_slice
31260Sstevel@tonic-gate  * 	Read the VTOC table on the Solaris partition and check that no
31270Sstevel@tonic-gate  *	slices exist that extend past the end of the Solaris partition.
31280Sstevel@tonic-gate  *	If no Solaris partition exists, nothing is done.
31290Sstevel@tonic-gate  */
3130251Slclee static void
3131251Slclee fix_slice(void)
31320Sstevel@tonic-gate {
31330Sstevel@tonic-gate 	int	i;
31340Sstevel@tonic-gate 	int	numsect;
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	if (io_image) {
3137251Slclee 		return;
31380Sstevel@tonic-gate 	}
31390Sstevel@tonic-gate 
31400Sstevel@tonic-gate 	for (i = 0; i < FD_NUMPART; i++) {
31410Sstevel@tonic-gate 		if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) {
31420Sstevel@tonic-gate 			/*
31430Sstevel@tonic-gate 			 * Only the size matters (not starting point), since
31440Sstevel@tonic-gate 			 * VTOC entries are relative to the start of
31450Sstevel@tonic-gate 			 * the partition.
31460Sstevel@tonic-gate 			 */
31470Sstevel@tonic-gate 			numsect = lel(Table[i].numsect);
31480Sstevel@tonic-gate 			break;
31490Sstevel@tonic-gate 		}
31500Sstevel@tonic-gate 	}
31510Sstevel@tonic-gate 
31520Sstevel@tonic-gate 	if (i >= FD_NUMPART) {
31530Sstevel@tonic-gate 		if (!io_nifdisk) {
31540Sstevel@tonic-gate 			(void) fprintf(stderr,
31550Sstevel@tonic-gate 			    "fdisk: No Solaris partition found - VTOC not"
31560Sstevel@tonic-gate 			    " checked.\n");
31570Sstevel@tonic-gate 		}
3158251Slclee 		return;
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 
3161251Slclee 	if (readvtoc() != VTOC_OK) {
31620Sstevel@tonic-gate 		exit(1);		/* Failed to read the VTOC */
31635169Slclee 	}
31645169Slclee 	for (i = 0; i < V_NUMPAR; i++) {
31655169Slclee 		/* Special case for slice two (entire disk) */
31665169Slclee 		if (i == 2) {
31675169Slclee 			if (disk_vtoc.v_part[i].p_start != 0) {
31685169Slclee 				(void) fprintf(stderr,
31695169Slclee 				    "slice %d starts at %ld, is not at"
31705169Slclee 				    " start of partition",
31715169Slclee 				    i, disk_vtoc.v_part[i].p_start);
31725169Slclee 				if (!io_nifdisk) {
31735169Slclee 					(void) printf(" adjust ?:");
31745169Slclee 					if (yesno())
31750Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
31765169Slclee 				} else {
31775169Slclee 					disk_vtoc.v_part[i].p_start = 0;
31785169Slclee 					(void) fprintf(stderr, " adjusted!\n");
31790Sstevel@tonic-gate 				}
31805169Slclee 
31815169Slclee 			}
31825169Slclee 			if (disk_vtoc.v_part[i].p_size != numsect) {
31835169Slclee 				(void) fprintf(stderr,
31845169Slclee 				    "slice %d size %ld does not cover"
31855169Slclee 				    " complete partition",
31865169Slclee 				    i, disk_vtoc.v_part[i].p_size);
31875169Slclee 				if (!io_nifdisk) {
31885169Slclee 					(void) printf(" adjust ?:");
31895169Slclee 					if (yesno())
31900Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size =
31910Sstevel@tonic-gate 						    numsect;
31925169Slclee 				} else {
31935169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
31945169Slclee 					(void) fprintf(stderr, " adjusted!\n");
31950Sstevel@tonic-gate 				}
31965169Slclee 			}
31975169Slclee 			if (disk_vtoc.v_part[i].p_tag != V_BACKUP) {
31985169Slclee 				(void) fprintf(stderr,
31995169Slclee 				    "slice %d tag was %d should be %d",
32005169Slclee 				    i, disk_vtoc.v_part[i].p_tag,
32015169Slclee 				    V_BACKUP);
32025169Slclee 				if (!io_nifdisk) {
3203251Slclee 					(void) printf(" fix ?:");
32045169Slclee 					if (yesno())
32050Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag =
32060Sstevel@tonic-gate 						    V_BACKUP;
32075169Slclee 				} else {
32080Sstevel@tonic-gate 					disk_vtoc.v_part[i].p_tag = V_BACKUP;
32090Sstevel@tonic-gate 					(void) fprintf(stderr, " fixed!\n");
32100Sstevel@tonic-gate 				}
32115169Slclee 			}
32125169Slclee 			continue;
32135169Slclee 		}
32145169Slclee 		if (io_ADJT) {
32155169Slclee 			if (disk_vtoc.v_part[i].p_start > numsect ||
32165169Slclee 			    disk_vtoc.v_part[i].p_start +
32175169Slclee 			    disk_vtoc.v_part[i].p_size > numsect) {
32185169Slclee 				(void) fprintf(stderr,
32195169Slclee 				    "slice %d (start %ld, end %ld)"
32205169Slclee 				    " is larger than the partition",
32215169Slclee 				    i, disk_vtoc.v_part[i].p_start,
32225169Slclee 				    disk_vtoc.v_part[i].p_start +
32235169Slclee 				    disk_vtoc.v_part[i].p_size);
32245169Slclee 				if (!io_nifdisk) {
32255169Slclee 					(void) printf(" remove ?:");
32265169Slclee 					if (yesno()) {
32270Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_size = 0;
32280Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_start = 0;
32290Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_tag = 0;
32300Sstevel@tonic-gate 						disk_vtoc.v_part[i].p_flag = 0;
32310Sstevel@tonic-gate 					}
32320Sstevel@tonic-gate 				} else {
32335169Slclee 					disk_vtoc.v_part[i].p_size = 0;
32345169Slclee 					disk_vtoc.v_part[i].p_start = 0;
32355169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
32365169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
32370Sstevel@tonic-gate 					(void) fprintf(stderr,
32385169Slclee 					    " removed!\n");
32395169Slclee 				}
32405169Slclee 			}
32415169Slclee 			continue;
32425169Slclee 		}
32435169Slclee 		if (disk_vtoc.v_part[i].p_start > numsect) {
32445169Slclee 			(void) fprintf(stderr,
32455169Slclee 			    "slice %d (start %ld) is larger than the partition",
32465169Slclee 			    i, disk_vtoc.v_part[i].p_start);
32475169Slclee 			if (!io_nifdisk) {
32485169Slclee 				(void) printf(" remove ?:");
32495169Slclee 				if (yesno()) {
32505169Slclee 					disk_vtoc.v_part[i].p_size = 0;
32515169Slclee 					disk_vtoc.v_part[i].p_start = 0;
32525169Slclee 					disk_vtoc.v_part[i].p_tag = 0;
32535169Slclee 					disk_vtoc.v_part[i].p_flag = 0;
32540Sstevel@tonic-gate 				}
32555169Slclee 			} else {
32565169Slclee 				disk_vtoc.v_part[i].p_size = 0;
32575169Slclee 				disk_vtoc.v_part[i].p_start = 0;
32585169Slclee 				disk_vtoc.v_part[i].p_tag = 0;
32595169Slclee 				disk_vtoc.v_part[i].p_flag = 0;
32605169Slclee 				(void) fprintf(stderr,
32615169Slclee 				" removed!\n");
32625169Slclee 			}
32635169Slclee 		} else if (disk_vtoc.v_part[i].p_start
32645169Slclee 		    + disk_vtoc.v_part[i].p_size > numsect) {
32655169Slclee 			(void) fprintf(stderr,
32665169Slclee 			    "slice %d (end %ld) is larger"
32675169Slclee 			    " than the partition",
32685169Slclee 			    i,
32695169Slclee 			    disk_vtoc.v_part[i].p_start +
32705169Slclee 			    disk_vtoc.v_part[i].p_size);
32715169Slclee 			if (!io_nifdisk) {
32725169Slclee 				(void) printf(" adjust ?:");
32735169Slclee 				if (yesno()) {
32745169Slclee 					disk_vtoc.v_part[i].p_size = numsect;
32755169Slclee 				}
32765169Slclee 			} else {
32775169Slclee 				disk_vtoc.v_part[i].p_size = numsect;
32785169Slclee 				(void) fprintf(stderr, " adjusted!\n");
32790Sstevel@tonic-gate 			}
32800Sstevel@tonic-gate 		}
32810Sstevel@tonic-gate 	}
32820Sstevel@tonic-gate #if 1		/* bh for now */
32830Sstevel@tonic-gate 	/* Make the VTOC look sane - ha ha */
32840Sstevel@tonic-gate 	disk_vtoc.v_version = V_VERSION;
32850Sstevel@tonic-gate 	disk_vtoc.v_sanity = VTOC_SANE;
32860Sstevel@tonic-gate 	disk_vtoc.v_nparts = V_NUMPAR;
32870Sstevel@tonic-gate 	if (disk_vtoc.v_sectorsz == 0)
32880Sstevel@tonic-gate 		disk_vtoc.v_sectorsz = NBPSCTR;
32890Sstevel@tonic-gate #endif
32900Sstevel@tonic-gate 
32910Sstevel@tonic-gate 	/* Write the VTOC back to the disk */
32920Sstevel@tonic-gate 	if (!io_readonly)
3293251Slclee 		(void) writevtoc();
32940Sstevel@tonic-gate }
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate /*
32970Sstevel@tonic-gate  * yesno
32980Sstevel@tonic-gate  * Get yes or no answer. Return 1 for yes and 0 for no.
32990Sstevel@tonic-gate  */
33000Sstevel@tonic-gate 
3301251Slclee static int
3302251Slclee yesno(void)
33030Sstevel@tonic-gate {
33040Sstevel@tonic-gate 	char	s[80];
33050Sstevel@tonic-gate 
33060Sstevel@tonic-gate 	for (;;) {
3307251Slclee 		(void) gets(s);
33080Sstevel@tonic-gate 		rm_blanks(s);
33090Sstevel@tonic-gate 		if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) {
3310251Slclee 			(void) printf(E_LINE);
3311251Slclee 			(void) printf("Please answer with \"y\" or \"n\": ");
33120Sstevel@tonic-gate 			continue;
33130Sstevel@tonic-gate 		}
33140Sstevel@tonic-gate 		if (s[0] == 'y')
33150Sstevel@tonic-gate 			return (1);
33160Sstevel@tonic-gate 		else
33170Sstevel@tonic-gate 			return (0);
33180Sstevel@tonic-gate 	}
33190Sstevel@tonic-gate }
33200Sstevel@tonic-gate 
33210Sstevel@tonic-gate /*
33220Sstevel@tonic-gate  * readvtoc
33230Sstevel@tonic-gate  * 	Read the VTOC from the Solaris partition of the device.
33240Sstevel@tonic-gate  */
3325251Slclee static int
3326251Slclee readvtoc(void)
33270Sstevel@tonic-gate {
33280Sstevel@tonic-gate 	int	i;
33290Sstevel@tonic-gate 	int	retval = VTOC_OK;
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 	if ((i = read_vtoc(Dev, &disk_vtoc)) < VTOC_OK) {
33320Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33330Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Invalid VTOC.\n");
33340Sstevel@tonic-gate 			vt_inval++;
33350Sstevel@tonic-gate 			retval = VTOC_INVAL;
33360Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33370Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
33385169Slclee 			    "GPT\n");
33390Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33400Sstevel@tonic-gate 		} else {
33410Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot read VTOC.\n");
33420Sstevel@tonic-gate 			retval = VTOC_RWERR;
33430Sstevel@tonic-gate 		}
33440Sstevel@tonic-gate 	}
33450Sstevel@tonic-gate 	return (retval);
33460Sstevel@tonic-gate }
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate /*
33490Sstevel@tonic-gate  * writevtoc
33500Sstevel@tonic-gate  * 	Write the VTOC to the Solaris partition on the device.
33510Sstevel@tonic-gate  */
3352251Slclee static int
3353251Slclee writevtoc(void)
33540Sstevel@tonic-gate {
33550Sstevel@tonic-gate 	int	i;
33560Sstevel@tonic-gate 	int	retval = 0;
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 	if ((i = write_vtoc(Dev, &disk_vtoc)) != 0) {
33590Sstevel@tonic-gate 		if (i == VT_EINVAL) {
33600Sstevel@tonic-gate 			(void) fprintf(stderr,
33610Sstevel@tonic-gate 			    "fdisk: Invalid entry exists in VTOC.\n");
33620Sstevel@tonic-gate 			retval = VTOC_INVAL;
33630Sstevel@tonic-gate 		} else if (i == VT_ENOTSUP) {
33640Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: partition may have EFI "
33655169Slclee 			    "GPT\n");
33660Sstevel@tonic-gate 			retval = VTOC_NOTSUP;
33670Sstevel@tonic-gate 		} else {
33680Sstevel@tonic-gate 			(void) fprintf(stderr, "fdisk: Cannot write VTOC.\n");
33690Sstevel@tonic-gate 			retval = VTOC_RWERR;
33700Sstevel@tonic-gate 		}
33710Sstevel@tonic-gate 	}
33720Sstevel@tonic-gate 	return (retval);
33730Sstevel@tonic-gate }
33740Sstevel@tonic-gate 
33750Sstevel@tonic-gate /*
33760Sstevel@tonic-gate  * efi_ioctl
33770Sstevel@tonic-gate  * issues DKIOCSETEFI IOCTL
33780Sstevel@tonic-gate  * (duplicate of private efi_ioctl() in rdwr_efi.c
33790Sstevel@tonic-gate  */
33800Sstevel@tonic-gate static int
33810Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc)
33820Sstevel@tonic-gate {
33830Sstevel@tonic-gate 	void *data = dk_ioc->dki_data;
33840Sstevel@tonic-gate 	int error;
33850Sstevel@tonic-gate 
33860Sstevel@tonic-gate 	dk_ioc->dki_data_64 = (uintptr_t)data;
33870Sstevel@tonic-gate 	error = ioctl(fd, cmd, (void *)dk_ioc);
33880Sstevel@tonic-gate 
33890Sstevel@tonic-gate 	return (error);
33900Sstevel@tonic-gate }
33910Sstevel@tonic-gate 
33920Sstevel@tonic-gate /*
33930Sstevel@tonic-gate  * clear_efi
33940Sstevel@tonic-gate  * Clear EFI labels from the EFI_PMBR partition on the device
33950Sstevel@tonic-gate  * This function is modeled on the libefi(3LIB) call efi_write()
33960Sstevel@tonic-gate  */
3397251Slclee static int
3398251Slclee clear_efi(void)
33990Sstevel@tonic-gate {
34000Sstevel@tonic-gate 	struct dk_gpt	*efi_vtoc;
34010Sstevel@tonic-gate 	dk_efi_t	dk_ioc;
34020Sstevel@tonic-gate 
34030Sstevel@tonic-gate 	/*
34040Sstevel@tonic-gate 	 * see if we can read the EFI label
34050Sstevel@tonic-gate 	 */
34060Sstevel@tonic-gate 	if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) {
34070Sstevel@tonic-gate 		return (VT_ERROR);
34080Sstevel@tonic-gate 	}
34090Sstevel@tonic-gate 
34100Sstevel@tonic-gate 	/*
34110Sstevel@tonic-gate 	 * set up the dk_ioc structure for writing
34120Sstevel@tonic-gate 	 */
34130Sstevel@tonic-gate 	dk_ioc.dki_lba = 1;
34140Sstevel@tonic-gate 	dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize;
34150Sstevel@tonic-gate 
34160Sstevel@tonic-gate 	if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) {
34170Sstevel@tonic-gate 		return (VT_ERROR);
34180Sstevel@tonic-gate 	}
34190Sstevel@tonic-gate 
34200Sstevel@tonic-gate 	/*
34210Sstevel@tonic-gate 	 * clear the primary label
34220Sstevel@tonic-gate 	 */
34230Sstevel@tonic-gate 	if (io_debug) {
3424251Slclee 		(void) fprintf(stderr,
3425251Slclee 		    "\tClearing primary EFI label at block %lld\n",
3426251Slclee 		    dk_ioc.dki_lba);
34270Sstevel@tonic-gate 	}
34280Sstevel@tonic-gate 
34290Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34300Sstevel@tonic-gate 		free(dk_ioc.dki_data);
34310Sstevel@tonic-gate 		switch (errno) {
34320Sstevel@tonic-gate 			case EIO:
34330Sstevel@tonic-gate 				return (VT_EIO);
34340Sstevel@tonic-gate 			case EINVAL:
34350Sstevel@tonic-gate 				return (VT_EINVAL);
34360Sstevel@tonic-gate 			default:
34370Sstevel@tonic-gate 				return (VT_ERROR);
34380Sstevel@tonic-gate 		}
34390Sstevel@tonic-gate 	}
34400Sstevel@tonic-gate 
34410Sstevel@tonic-gate 	/*
34420Sstevel@tonic-gate 	 * clear the backup partition table
34430Sstevel@tonic-gate 	 */
34440Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1;
34450Sstevel@tonic-gate 	dk_ioc.dki_length -= efi_vtoc->efi_lbasize;
34460Sstevel@tonic-gate 	dk_ioc.dki_data++;
34470Sstevel@tonic-gate 	if (io_debug) {
3448251Slclee 		(void) fprintf(stderr,
3449251Slclee 		    "\tClearing backup partition table at block %lld\n",
3450251Slclee 		    dk_ioc.dki_lba);
34510Sstevel@tonic-gate 	}
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
34540Sstevel@tonic-gate 		(void) fprintf(stderr, "\tUnable to clear backup EFI label at "
34555169Slclee 		    "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1,
34565169Slclee 		    errno);
34570Sstevel@tonic-gate 	}
34580Sstevel@tonic-gate 
34590Sstevel@tonic-gate 	/*
34600Sstevel@tonic-gate 	 * clear the backup label
34610Sstevel@tonic-gate 	 */
34620Sstevel@tonic-gate 	dk_ioc.dki_lba = efi_vtoc->efi_last_lba;
34630Sstevel@tonic-gate 	dk_ioc.dki_length = efi_vtoc->efi_lbasize;
34640Sstevel@tonic-gate 	dk_ioc.dki_data--;
34650Sstevel@tonic-gate 	if (io_debug) {
3466251Slclee 		(void) fprintf(stderr, "\tClearing backup label at block "
3467251Slclee 		    "%lld\n", dk_ioc.dki_lba);
34680Sstevel@tonic-gate 	}
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 	if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) {
3471251Slclee 		(void) fprintf(stderr,
3472251Slclee 		    "\tUnable to clear backup EFI label at "
3473251Slclee 		    "block %llu; errno %d\n",
3474251Slclee 		    efi_vtoc->efi_last_lba,
3475251Slclee 		    errno);
34760Sstevel@tonic-gate 	}
34770Sstevel@tonic-gate 
34780Sstevel@tonic-gate 	free(dk_ioc.dki_data);
34790Sstevel@tonic-gate 	efi_free(efi_vtoc);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 	return (0);
34820Sstevel@tonic-gate }
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate /*
34850Sstevel@tonic-gate  * clear_vtoc
34860Sstevel@tonic-gate  * 	Clear the VTOC from the current or previous Solaris partition on the
34870Sstevel@tonic-gate  *      device.
34880Sstevel@tonic-gate  */
3489251Slclee static void
34900Sstevel@tonic-gate clear_vtoc(int table, int part)
34910Sstevel@tonic-gate {
34920Sstevel@tonic-gate 	struct ipart *clr_table;
34930Sstevel@tonic-gate 	struct dk_label disk_label;
34940Sstevel@tonic-gate 	int pcyl, ncyl, backup_block, solaris_offset, count, bytes, seek_byte;
34950Sstevel@tonic-gate 
34960Sstevel@tonic-gate #ifdef DEBUG
34970Sstevel@tonic-gate 	struct dk_label	read_label;
34980Sstevel@tonic-gate #endif /* DEBUG */
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 	if (table == OLD) {
35010Sstevel@tonic-gate 		clr_table = &Old_Table[part];
35020Sstevel@tonic-gate 	} else {
35030Sstevel@tonic-gate 		clr_table = &Table[part];
35040Sstevel@tonic-gate 	}
35050Sstevel@tonic-gate 
3506251Slclee 	(void) memset(&disk_label, 0, sizeof (struct dk_label));
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	seek_byte = (lel(clr_table->relsect) * sectsiz) + VTOC_OFFSET;
35090Sstevel@tonic-gate 
35100Sstevel@tonic-gate 	if (io_debug) {
3511251Slclee 		(void) fprintf(stderr, "\tClearing primary VTOC at byte %d\n",
35120Sstevel@tonic-gate 		    seek_byte);
35130Sstevel@tonic-gate 	}
35140Sstevel@tonic-gate 
35150Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3516251Slclee 		(void) fprintf(stderr,
3517251Slclee 		    "\tError seeking to primary label at byte %d\n",
35180Sstevel@tonic-gate 		    seek_byte);
3519251Slclee 		return;
35200Sstevel@tonic-gate 	}
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate 	bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3525251Slclee 		(void) fprintf(stderr,
3526251Slclee 		    "\tWarning: only %d bytes written to clear primary VTOC!\n",
3527251Slclee 		    bytes);
35280Sstevel@tonic-gate 	}
35290Sstevel@tonic-gate 
35300Sstevel@tonic-gate #ifdef DEBUG
35310Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3532251Slclee 		(void) fprintf(stderr,
3533251Slclee 		    "DEBUG: Error seeking to primary label at byte %d\n",
3534251Slclee 		    seek_byte);
3535251Slclee 		return;
35360Sstevel@tonic-gate 	} else {
3537251Slclee 		(void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n",
35380Sstevel@tonic-gate 		    seek_byte);
35390Sstevel@tonic-gate 	}
35400Sstevel@tonic-gate 
35410Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3544251Slclee 		(void) fprintf(stderr,
3545251Slclee 		    "DEBUG: Warning: only %d bytes read of label\n",
35460Sstevel@tonic-gate 		    bytes);
35470Sstevel@tonic-gate 	}
35480Sstevel@tonic-gate 
35490Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3550251Slclee 		(void) fprintf(stderr,
3551251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
35520Sstevel@tonic-gate 	} else {
3553251Slclee 		(void) fprintf(stderr, "DEBUG Good compare of disk_label and "
35540Sstevel@tonic-gate 		    "read_label\n");
35550Sstevel@tonic-gate 	}
35560Sstevel@tonic-gate #endif /* DEBUG */
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	/* Clear backup label */
35590Sstevel@tonic-gate 	pcyl = lel(clr_table->numsect) / (heads * sectors);
35600Sstevel@tonic-gate 	solaris_offset = lel(clr_table->relsect);
35610Sstevel@tonic-gate 	ncyl = pcyl - acyl;
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 	backup_block = ((ncyl + acyl - 1) *
35640Sstevel@tonic-gate 	    (heads * sectors)) + ((heads - 1) * sectors) + 1;
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 	for (count = 1; count < 6; count++) {
35670Sstevel@tonic-gate 		seek_byte = (solaris_offset + backup_block) * 512;
35680Sstevel@tonic-gate 
35690Sstevel@tonic-gate 		if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3570251Slclee 			(void) fprintf(stderr,
35710Sstevel@tonic-gate 			    "\tError seeking to backup label at byte %d on "
35720Sstevel@tonic-gate 			    "%s.\n", seek_byte, Dfltdev);
3573251Slclee 			return;
35740Sstevel@tonic-gate 		}
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 		if (io_debug) {
3577251Slclee 			(void) fprintf(stderr, "\tClearing backup VTOC at"
35780Sstevel@tonic-gate 			    " byte %d (block %d)\n",
35790Sstevel@tonic-gate 			    (solaris_offset + backup_block) * 512,
35800Sstevel@tonic-gate 			    (solaris_offset + backup_block));
35810Sstevel@tonic-gate 		}
35820Sstevel@tonic-gate 
35830Sstevel@tonic-gate 		bytes = write(Dev, &disk_label, sizeof (struct dk_label));
35840Sstevel@tonic-gate 
35850Sstevel@tonic-gate 		if (bytes != sizeof (struct dk_label)) {
3586251Slclee 			(void) fprintf(stderr,
3587251Slclee 			    "\t\tWarning: only %d bytes written to "
35880Sstevel@tonic-gate 			    "clear backup VTOC at block %d!\n", bytes,
35890Sstevel@tonic-gate 			    (solaris_offset + backup_block));
35900Sstevel@tonic-gate 		}
35910Sstevel@tonic-gate 
35920Sstevel@tonic-gate #ifdef DEBUG
35930Sstevel@tonic-gate 	if (lseek(Dev, seek_byte, SEEK_SET) == -1) {
3594251Slclee 		(void) fprintf(stderr,
3595251Slclee 		    "DEBUG: Error seeking to backup label at byte %d\n",
3596251Slclee 		    seek_byte);
3597251Slclee 		return;
35980Sstevel@tonic-gate 	} else {
3599251Slclee 		(void) fprintf(stderr, "DEBUG: Successful lseek() to byte %d\n",
36000Sstevel@tonic-gate 		    seek_byte);
36010Sstevel@tonic-gate 	}
36020Sstevel@tonic-gate 
36030Sstevel@tonic-gate 	bytes = read(Dev, &read_label, sizeof (struct dk_label));
36040Sstevel@tonic-gate 
36050Sstevel@tonic-gate 	if (bytes != sizeof (struct dk_label)) {
3606251Slclee 		(void) fprintf(stderr,
3607251Slclee 		    "DEBUG: Warning: only %d bytes read of backup label\n",
3608251Slclee 		    bytes);
36090Sstevel@tonic-gate 	}
36100Sstevel@tonic-gate 
36110Sstevel@tonic-gate 	if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) {
3612251Slclee 		(void) fprintf(stderr,
3613251Slclee 		    "DEBUG: Warning: disk_label and read_label differ!!!\n");
36140Sstevel@tonic-gate 	} else {
3615251Slclee 		(void) fprintf(stderr,
3616251Slclee 		    "DEBUG: Good compare of disk_label and backup "
36170Sstevel@tonic-gate 		    "read_label\n");
36180Sstevel@tonic-gate 	}
36190Sstevel@tonic-gate #endif /* DEBUG */
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 		backup_block += 2;
36220Sstevel@tonic-gate 	}
36230Sstevel@tonic-gate }
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate #define	FDISK_STANDARD_LECTURE \
36260Sstevel@tonic-gate 	"Fdisk is normally used with the device that " \
36270Sstevel@tonic-gate 	"represents the entire fixed disk.\n" \
36280Sstevel@tonic-gate 	"(For example, /dev/rdsk/c0d0p0 on x86 or " \
36290Sstevel@tonic-gate 	"/dev/rdsk/c0t5d0s2 on sparc).\n"
36300Sstevel@tonic-gate 
36310Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_SECTOR_ZERO \
36320Sstevel@tonic-gate 	"The device does not appear to include absolute\n" \
36330Sstevel@tonic-gate 	"sector 0 of the PHYSICAL disk " \
36340Sstevel@tonic-gate 	"(the normal location for an fdisk table).\n"
36350Sstevel@tonic-gate 
36360Sstevel@tonic-gate #define	FDISK_LECTURE_NOT_FULL \
36370Sstevel@tonic-gate 	"The device does not appear to encompass the entire PHYSICAL disk.\n"
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate #define	FDISK_LECTURE_NO_VTOC \
36400Sstevel@tonic-gate 	"Unable to find a volume table of contents.\n" \
36410Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36420Sstevel@tonic-gate 
36430Sstevel@tonic-gate #define	FDISK_LECTURE_NO_GEOM \
36440Sstevel@tonic-gate 	"Unable to get geometry from device.\n" \
36450Sstevel@tonic-gate 	"Cannot verify the device encompasses the full PHYSICAL disk.\n"
36460Sstevel@tonic-gate 
36470Sstevel@tonic-gate #define	FDISK_SHALL_I_CONTINUE \
36480Sstevel@tonic-gate 	"Are you sure you want to continue? (y/n) "
36490Sstevel@tonic-gate 
36500Sstevel@tonic-gate /*
36510Sstevel@tonic-gate  *  lecture_and_query
36520Sstevel@tonic-gate  *	Called when a sanity check fails.  This routine gives a warning
36530Sstevel@tonic-gate  *	specific to the check that fails, followed by a generic lecture
36540Sstevel@tonic-gate  *	about the "right" device to supply as input.  Then, if appropriate,
36550Sstevel@tonic-gate  *	it will prompt the user on whether or not they want to continue.
36560Sstevel@tonic-gate  *	Inappropriate times for prompting are when the user has selected
36570Sstevel@tonic-gate  *	non-interactive mode or read-only mode.
36580Sstevel@tonic-gate  */
3659251Slclee static int
36600Sstevel@tonic-gate lecture_and_query(char *warning, char *devname)
36610Sstevel@tonic-gate {
36620Sstevel@tonic-gate 	if (io_nifdisk)
36630Sstevel@tonic-gate 		return (0);
36640Sstevel@tonic-gate 
3665251Slclee 	(void) fprintf(stderr, "WARNING: Device %s: \n", devname);
3666251Slclee 	(void) fprintf(stderr, "%s", warning);
3667251Slclee 	(void) fprintf(stderr, FDISK_STANDARD_LECTURE);
3668251Slclee 	(void) fprintf(stderr, FDISK_SHALL_I_CONTINUE);
36690Sstevel@tonic-gate 
36700Sstevel@tonic-gate 	return (yesno());
36710Sstevel@tonic-gate }
36720Sstevel@tonic-gate 
3673251Slclee static void
36740Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd)
36750Sstevel@tonic-gate {
36760Sstevel@tonic-gate 	struct vtoc v;
36770Sstevel@tonic-gate 	struct dk_geom d;
36780Sstevel@tonic-gate 	struct part_info pi;
36790Sstevel@tonic-gate 	long totsize;
36800Sstevel@tonic-gate 	int idx = -1;
36810Sstevel@tonic-gate 
36820Sstevel@tonic-gate 	/*
36830Sstevel@tonic-gate 	 *  First try the PARTINFO ioctl.  If it works, we will be able
36840Sstevel@tonic-gate 	 *  to tell if they've specified the full disk partition by checking
36850Sstevel@tonic-gate 	 *  to see if they've specified a partition that starts at sector 0.
36860Sstevel@tonic-gate 	 */
36870Sstevel@tonic-gate 	if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) {
36880Sstevel@tonic-gate 		if (pi.p_start != 0) {
36890Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO,
36900Sstevel@tonic-gate 			    devname)) {
36910Sstevel@tonic-gate 				(void) close(fd);
36920Sstevel@tonic-gate 				exit(1);
36930Sstevel@tonic-gate 			}
36940Sstevel@tonic-gate 		}
36950Sstevel@tonic-gate 	} else {
36960Sstevel@tonic-gate 		if ((idx = read_vtoc(fd, &v)) < 0) {
36970Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_VTOC,
36980Sstevel@tonic-gate 			    devname)) {
36990Sstevel@tonic-gate 				(void) close(fd);
37000Sstevel@tonic-gate 				exit(1);
37010Sstevel@tonic-gate 			}
37020Sstevel@tonic-gate 			return;
37030Sstevel@tonic-gate 		}
37040Sstevel@tonic-gate 		if (ioctl(fd, DKIOCGGEOM, &d) == -1) {
37050Sstevel@tonic-gate 			perror(devname);
37060Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NO_GEOM,
37070Sstevel@tonic-gate 			    devname)) {
37080Sstevel@tonic-gate 				(void) close(fd);
37090Sstevel@tonic-gate 				exit(1);
37100Sstevel@tonic-gate 			}
37110Sstevel@tonic-gate 			return;
37120Sstevel@tonic-gate 		}
37130Sstevel@tonic-gate 		totsize = d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect;
37140Sstevel@tonic-gate 		if (v.v_part[idx].p_size != totsize) {
37150Sstevel@tonic-gate 			if (!lecture_and_query(FDISK_LECTURE_NOT_FULL,
37160Sstevel@tonic-gate 			    devname)) {
37170Sstevel@tonic-gate 				(void) close(fd);
37180Sstevel@tonic-gate 				exit(1);
37190Sstevel@tonic-gate 			}
37200Sstevel@tonic-gate 		}
37210Sstevel@tonic-gate 	}
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 
37250Sstevel@tonic-gate /*
37260Sstevel@tonic-gate  * get_node
37270Sstevel@tonic-gate  * Called from main to construct the name of the device node to open.
37280Sstevel@tonic-gate  * Initially tries to stat the node exactly as provided, if that fails
37290Sstevel@tonic-gate  * we prepend the default path (/dev/rdsk/).
37300Sstevel@tonic-gate  */
37310Sstevel@tonic-gate static char *
37320Sstevel@tonic-gate get_node(char *devname)
37330Sstevel@tonic-gate {
37340Sstevel@tonic-gate 	char *node;
37350Sstevel@tonic-gate 	struct stat statbuf;
37360Sstevel@tonic-gate 	size_t space;
37370Sstevel@tonic-gate 
37380Sstevel@tonic-gate 	/* Don't do anything if we are skipping device checks */
37390Sstevel@tonic-gate 	if (io_image)
37400Sstevel@tonic-gate 		return (devname);
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate 	node = devname;
37430Sstevel@tonic-gate 
37440Sstevel@tonic-gate 	/* Try the node as provided first */
37450Sstevel@tonic-gate 	if (stat(node, (struct stat *)&statbuf) == -1) {
37460Sstevel@tonic-gate 		/*
37470Sstevel@tonic-gate 		 * Copy the passed in string to a new buffer, prepend the
37480Sstevel@tonic-gate 		 * default path and try again.
37490Sstevel@tonic-gate 		 */
37500Sstevel@tonic-gate 		space = strlen(DEFAULT_PATH) + strlen(devname) + 1;
37510Sstevel@tonic-gate 
37520Sstevel@tonic-gate 		if ((node = malloc(space)) == NULL) {
3753251Slclee 			(void) fprintf(stderr, "fdisk: Unable to obtain memory "
37540Sstevel@tonic-gate 			    "for device node.\n");
37550Sstevel@tonic-gate 			exit(1);
37560Sstevel@tonic-gate 		}
37570Sstevel@tonic-gate 
37580Sstevel@tonic-gate 		/* Copy over the default path and the provided node */
37590Sstevel@tonic-gate 		(void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH));
37600Sstevel@tonic-gate 		space -= strlen(DEFAULT_PATH);
37610Sstevel@tonic-gate 		(void) strlcpy(node + strlen(DEFAULT_PATH), devname, space);
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 		/* Try to stat it again */
37640Sstevel@tonic-gate 		if (stat(node, (struct stat *)&statbuf) == -1) {
37650Sstevel@tonic-gate 			/* Failed all options, give up */
3766251Slclee 			(void) fprintf(stderr,
3767251Slclee 			    "fdisk: Cannot stat device %s.\n",
37680Sstevel@tonic-gate 			    devname);
37690Sstevel@tonic-gate 			exit(1);
37700Sstevel@tonic-gate 		}
37710Sstevel@tonic-gate 	}
37720Sstevel@tonic-gate 
37730Sstevel@tonic-gate 	/* Make sure the device specified is the raw device */
37740Sstevel@tonic-gate 	if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
3775251Slclee 		(void) fprintf(stderr,
3776251Slclee 		    "fdisk: %s must be a raw device.\n", node);
37770Sstevel@tonic-gate 		exit(1);
37780Sstevel@tonic-gate 	}
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 	return (node);
37810Sstevel@tonic-gate }
3782