xref: /illumos-gate/usr/src/cmd/fdisk/fdisk.c (revision 13b70267)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51237e847Slclee  * Common Development and Distribution License (the "License").
61237e847Slclee  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
211237e847Slclee 
227c478bd9Sstevel@tonic-gate /*
23b8a1bc93SShidokht Yadegari  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
247e934d3aSAndy Fiddaman  * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1990, 1991 UNIX System Laboratories, Inc.	*/
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * PROGRAM: fdisk(1M)
367c478bd9Sstevel@tonic-gate  * This program reads the partition table on the specified device and
377c478bd9Sstevel@tonic-gate  * also reads the drive parameters. The user can perform various
387c478bd9Sstevel@tonic-gate  * operations from a supplied menu or from the command line. Diagnostic
397c478bd9Sstevel@tonic-gate  * options are also available.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <stdlib.h>
437c478bd9Sstevel@tonic-gate #include <string.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
50342440ecSPrasad Singamsetty #include <limits.h>
517c478bd9Sstevel@tonic-gate #include <sys/param.h>
527c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
537c478bd9Sstevel@tonic-gate #include <sys/efi_partition.h>
547c478bd9Sstevel@tonic-gate #include <sys/byteorder.h>
557c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <sys/dktp/fdisk.h>
587c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
597c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
60aa1b14e7SSheshadri Vasudevan #ifdef i386
61aa1b14e7SSheshadri Vasudevan #include <sys/tty.h>
62aa1b14e7SSheshadri Vasudevan #include <libfdisk.h>
63aa1b14e7SSheshadri Vasudevan #endif
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	CLR_SCR ""
667c478bd9Sstevel@tonic-gate #define	CLR_LIN ""
677c478bd9Sstevel@tonic-gate #define	HOME "" \
687c478bd9Sstevel@tonic-gate 	""
697c478bd9Sstevel@tonic-gate #define	Q_LINE ""
70aa1b14e7SSheshadri Vasudevan 
71aa1b14e7SSheshadri Vasudevan #ifdef i386
72aa1b14e7SSheshadri Vasudevan #define	W_LINE ""
73aa1b14e7SSheshadri Vasudevan #else
747c478bd9Sstevel@tonic-gate #define	W_LINE ""
75aa1b14e7SSheshadri Vasudevan #endif
76aa1b14e7SSheshadri Vasudevan 
777c478bd9Sstevel@tonic-gate #define	E_LINE ""
78aa1b14e7SSheshadri Vasudevan 
79aa1b14e7SSheshadri Vasudevan #ifdef i386
80aa1b14e7SSheshadri Vasudevan #define	M_LINE "" \
81aa1b14e7SSheshadri Vasudevan 	""
82aa1b14e7SSheshadri Vasudevan #else
837c478bd9Sstevel@tonic-gate #define	M_LINE "" \
847c478bd9Sstevel@tonic-gate 	""
85aa1b14e7SSheshadri Vasudevan #endif
86aa1b14e7SSheshadri Vasudevan 
877c478bd9Sstevel@tonic-gate #define	T_LINE ""
887c478bd9Sstevel@tonic-gate 
89*13b70267SToomas Soome #define	DEFAULT_PATH			"/dev/rdsk/"
90*13b70267SToomas Soome #define	DEFAULT_MASTER_BOOT_FILE	"/boot/pmbr"
917c478bd9Sstevel@tonic-gate 
92342440ecSPrasad Singamsetty #define	DK_MAX_2TB	UINT32_MAX	/* Max # of sectors in 2TB */
93342440ecSPrasad Singamsetty 
947c478bd9Sstevel@tonic-gate /* for clear_vtoc() */
957c478bd9Sstevel@tonic-gate #define	OLD		0
967c478bd9Sstevel@tonic-gate #define	NEW		1
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate /* readvtoc/writevtoc return codes */
997c478bd9Sstevel@tonic-gate #define	VTOC_OK		0	/* Good VTOC */
1007c478bd9Sstevel@tonic-gate #define	VTOC_INVAL	1	/* invalid VTOC */
1017c478bd9Sstevel@tonic-gate #define	VTOC_NOTSUP	2	/* operation not supported - EFI label */
1027c478bd9Sstevel@tonic-gate #define	VTOC_RWERR	3	/* couldn't read or write VTOC */
1037c478bd9Sstevel@tonic-gate 
104f85c7842SSuhasini Peddada /*
105f85c7842SSuhasini Peddada  * Support for fdisk(1M) on the SPARC platform
106f85c7842SSuhasini Peddada  *	In order to convert little endian values to big endian for SPARC,
107f85c7842SSuhasini Peddada  *	byte/short and long values must be swapped.
108f85c7842SSuhasini Peddada  *	These swapping macros will be used to access information in the
109f85c7842SSuhasini Peddada  *	mboot and ipart structures.
110f85c7842SSuhasini Peddada  */
111f85c7842SSuhasini Peddada 
112f85c7842SSuhasini Peddada #ifdef sparc
113f85c7842SSuhasini Peddada #define	les(val)	((((val)&0xFF)<<8)|(((val)>>8)&0xFF))
114f85c7842SSuhasini Peddada #define	lel(val)	(((unsigned)(les((val)&0x0000FFFF))<<16) | \
115f85c7842SSuhasini Peddada 			    (les((unsigned)((val)&0xffff0000)>>16)))
116f85c7842SSuhasini Peddada #else
117f85c7842SSuhasini Peddada #define	les(val)	(val)
118f85c7842SSuhasini Peddada #define	lel(val)	(val)
119f85c7842SSuhasini Peddada #endif
120f85c7842SSuhasini Peddada 
1217c478bd9Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16)
1229d5d1945Sbharding #define	VTOC_OFFSET	1
1237c478bd9Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8)
1247c478bd9Sstevel@tonic-gate #define	VTOC_OFFSET	0
1257c478bd9Sstevel@tonic-gate #else
1267c478bd9Sstevel@tonic-gate #error No VTOC format defined.
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate 
129aa1b14e7SSheshadri Vasudevan #ifdef i386
130aa1b14e7SSheshadri Vasudevan #define	FDISK_KB	(1024)
131aa1b14e7SSheshadri Vasudevan #define	FDISK_MB	(FDISK_KB * 1024)
132aa1b14e7SSheshadri Vasudevan #define	FDISK_GB	(FDISK_MB * 1024)
133aa1b14e7SSheshadri Vasudevan #define	TRUE	1
134aa1b14e7SSheshadri Vasudevan 
135aa1b14e7SSheshadri Vasudevan #define	FDISK_MAX_VALID_PART_ID	255
136aa1b14e7SSheshadri Vasudevan #define	FDISK_MAX_VALID_PART_NUM_DIGITS	2
137aa1b14e7SSheshadri Vasudevan #define	FDISK_MAX_VALID_PART_ID_DIGITS	3
138aa1b14e7SSheshadri Vasudevan 
139aa1b14e7SSheshadri Vasudevan /* Maximum number of digits for a valid partition size */
140aa1b14e7SSheshadri Vasudevan #define	FDISK_MAX_VALID_CYL_NUM_DIGITS	10
141aa1b14e7SSheshadri Vasudevan 
142aa1b14e7SSheshadri Vasudevan /* Minimum partition size in cylinders */
143aa1b14e7SSheshadri Vasudevan #define	FDISK_MIN_PART_SIZE	1
144aa1b14e7SSheshadri Vasudevan #endif
145aa1b14e7SSheshadri Vasudevan 
146bb16350dSlclee static char Usage[] = "Usage: fdisk\n"
1477c478bd9Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1487c478bd9Sstevel@tonic-gate "[ -b masterboot ]\n"
1497c478bd9Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n"
1507e934d3aSAndy Fiddaman "[ -E [slot:active] ]\n"
1517c478bd9Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n"
1527c478bd9Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n"
1537e934d3aSAndy Fiddaman "[ -w | r | d | n | I | B | g | G | R | t | T ] rdevice";
1547c478bd9Sstevel@tonic-gate 
155bb16350dSlclee static char Usage1[] = "    Partition options:\n"
1567c478bd9Sstevel@tonic-gate "	-A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1577c478bd9Sstevel@tonic-gate "		Create a partition with specific attributes:\n"
1587c478bd9Sstevel@tonic-gate "		id      = system id number (fdisk.h) for the partition type\n"
1597c478bd9Sstevel@tonic-gate "		act     = active partition flag (0 is off and 128 is on)\n"
1607c478bd9Sstevel@tonic-gate "		bhead   = beginning head for start of partition\n"
1617c478bd9Sstevel@tonic-gate "		bsect   = beginning sector for start of partition\n"
1627c478bd9Sstevel@tonic-gate "		bcyl    = beginning cylinder for start of partition\n"
1637c478bd9Sstevel@tonic-gate "		ehead   = ending head for end of partition\n"
1647c478bd9Sstevel@tonic-gate "		esect   = ending sector for end of partition\n"
1657c478bd9Sstevel@tonic-gate "		ecyl    = ending cylinder for end of partition\n"
1667c478bd9Sstevel@tonic-gate "		rsect   = sector number from start of disk for\n"
1677c478bd9Sstevel@tonic-gate "			  start of partition\n"
1687c478bd9Sstevel@tonic-gate "		numsect = partition size in sectors\n"
1697c478bd9Sstevel@tonic-gate "	-b master_boot\n"
1707c478bd9Sstevel@tonic-gate "		Use master_boot as the master boot file.\n"
1717c478bd9Sstevel@tonic-gate "	-B	Create one Solaris partition that uses the entire disk.\n"
1727c478bd9Sstevel@tonic-gate "	-D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n"
1737c478bd9Sstevel@tonic-gate "		Delete a partition. See attribute definitions for -A.\n"
1747e934d3aSAndy Fiddaman "	-E [slot:active]\n"
1757e934d3aSAndy Fiddaman "		Create an EFI partition that uses the entire disk.\n"
1767c478bd9Sstevel@tonic-gate "	-F fdisk_file\n"
1777c478bd9Sstevel@tonic-gate "		Use fdisk_file to initialize on-line fdisk table.\n"
1787c478bd9Sstevel@tonic-gate "	-I	Forego device checks. Generate a file image of what would go\n"
1797c478bd9Sstevel@tonic-gate "		on a disk using the geometry specified with the -S option.\n"
1807c478bd9Sstevel@tonic-gate "	-n	Do not run in interactive mode.\n"
1817c478bd9Sstevel@tonic-gate "	-R	Open the disk device as read-only.\n"
1827c478bd9Sstevel@tonic-gate "	-t	Check and adjust VTOC to be consistent with fdisk table.\n"
1837c478bd9Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be truncated.\n"
1847c478bd9Sstevel@tonic-gate "	-T	Check and adjust VTOC to be consistent with fdisk table.\n"
1857c478bd9Sstevel@tonic-gate "		VTOC slices exceeding the partition size will be removed.\n"
1867c478bd9Sstevel@tonic-gate "	-W fdisk_file\n"
1877c478bd9Sstevel@tonic-gate "		Write on-disk table to fdisk_file.\n"
1887c478bd9Sstevel@tonic-gate "	-W -	Write on-disk table to standard output.\n"
1897c478bd9Sstevel@tonic-gate "	-v	Display virtual geometry. Must be used with the -W option.\n"
1907c478bd9Sstevel@tonic-gate "    Diagnostic options:\n"
1917c478bd9Sstevel@tonic-gate "	-d	Activate debug information about progress.\n"
1927c478bd9Sstevel@tonic-gate "	-g	Write label geometry to standard output:\n"
1937c478bd9Sstevel@tonic-gate "		PCYL		number of physical cylinders\n"
1947c478bd9Sstevel@tonic-gate "		NCYL		number of usable cylinders\n"
1957c478bd9Sstevel@tonic-gate "		ACYL		number of alternate cylinders\n"
1967c478bd9Sstevel@tonic-gate "		BCYL		cylinder offset\n"
1977c478bd9Sstevel@tonic-gate "		NHEADS		number of heads\n"
1987c478bd9Sstevel@tonic-gate "		NSECTORS	number of sectors per track\n"
1997c478bd9Sstevel@tonic-gate "		SECTSIZ		size of a sector in bytes\n"
2007c478bd9Sstevel@tonic-gate "	-G	Write physical geometry to standard output (see -g).\n"
2017c478bd9Sstevel@tonic-gate "	-h	Issue this verbose help message.\n"
2027c478bd9Sstevel@tonic-gate "	-o offset\n"
2037c478bd9Sstevel@tonic-gate "		Block offset from start of disk (default 0). Ignored if\n"
2047c478bd9Sstevel@tonic-gate "		-P # specified.\n"
2057c478bd9Sstevel@tonic-gate "	-P fill_patt\n"
2067c478bd9Sstevel@tonic-gate "		Fill disk with pattern fill_patt. fill_patt can be decimal or\n"
2077c478bd9Sstevel@tonic-gate "		hexadecimal and is used as number for constant long word\n"
2087c478bd9Sstevel@tonic-gate "		pattern. If fill_patt is \"#\" then pattern of block #\n"
2097c478bd9Sstevel@tonic-gate "		for each block. Pattern is put in each block as long words\n"
2107c478bd9Sstevel@tonic-gate "		and fills each block (see -o and -s).\n"
2117c478bd9Sstevel@tonic-gate "	-r	Read from a disk to stdout (see -o and -s).\n"
2127c478bd9Sstevel@tonic-gate "	-s size	Number of blocks on which to perform operation (see -o).\n"
2137c478bd9Sstevel@tonic-gate "	-S geom_file\n"
2147c478bd9Sstevel@tonic-gate "		Use geom_file to set the label geometry (see -g).\n"
2157c478bd9Sstevel@tonic-gate "	-w	Write to a disk from stdin (see -o and -s).";
2167c478bd9Sstevel@tonic-gate 
217bb16350dSlclee static char Ostr[] = "Other OS";
218bb16350dSlclee static char Dstr[] = "DOS12";
219bb16350dSlclee static char D16str[] = "DOS16";
220bb16350dSlclee static char DDstr[] = "DOS-DATA";
221bb16350dSlclee static char EDstr[] = "EXT-DOS";
222bb16350dSlclee static char DBstr[] = "DOS-BIG";
223bb16350dSlclee static char PCstr[] = "PCIX";
224bb16350dSlclee static char Ustr[] = "UNIX System";
225bb16350dSlclee static char SUstr[] = "Solaris";
226bb16350dSlclee static char SU2str[] = "Solaris2";
227bb16350dSlclee static char X86str[] = "x86 Boot";
228bb16350dSlclee static char DIAGstr[] = "Diagnostic";
229bb16350dSlclee static char IFSstr[] = "IFS: NTFS";
230bb16350dSlclee static char AIXstr[] = "AIX Boot";
231bb16350dSlclee static char AIXDstr[] = "AIX Data";
232bb16350dSlclee static char OS2str[] = "OS/2 Boot";
233bb16350dSlclee static char WINstr[] = "Win95 FAT32";
234bb16350dSlclee static char EWINstr[] = "Ext Win95";
235bb16350dSlclee static char FAT95str[] = "FAT16 LBA";
236bb16350dSlclee static char EXTLstr[] = "EXT LBA";
237bb16350dSlclee static char LINUXstr[] = "Linux";
238bb16350dSlclee static char CPMstr[] = "CP/M";
239aa1b14e7SSheshadri Vasudevan static char NOV2str[] = "Netware 286";
240bb16350dSlclee static char NOVstr[] = "Netware 3.x+";
241bb16350dSlclee static char QNXstr[] = "QNX 4.x";
242bb16350dSlclee static char QNX2str[] = "QNX part 2";
243bb16350dSlclee static char QNX3str[] = "QNX part 3";
244bb16350dSlclee static char LINNATstr[] = "Linux native";
245b8a1bc93SShidokht Yadegari #ifdef i386
246e998e519SSheshadri Vasudevan static char LINSWAPstr[] = "Linux swap";
247b8a1bc93SShidokht Yadegari #endif
248bb16350dSlclee static char NTFSVOL1str[] = "NT volset 1";
249bb16350dSlclee static char NTFSVOL2str[] = "NT volset 2";
250bb16350dSlclee static char BSDstr[] = "BSD OS";
251bb16350dSlclee static char NEXTSTEPstr[] = "NeXTSTEP";
252bb16350dSlclee static char BSDIFSstr[] = "BSDI FS";
253bb16350dSlclee static char BSDISWAPstr[] = "BSDI swap";
254bb16350dSlclee static char Actvstr[] = "Active";
255bb16350dSlclee static char EFIstr[] = "EFI";
256bb16350dSlclee static char NAstr[] = "      ";
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate /* All the user options and flags */
259bb16350dSlclee static char *Dfltdev;			/* name of fixed disk drive */
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /* Diagnostic options */
262bb16350dSlclee static int	io_wrt = 0;		/* write stdin to disk (-w) */
263bb16350dSlclee static int	io_rd = 0;		/* read disk and write stdout (-r) */
264bb16350dSlclee static char	*io_fatt;		/* user supplied pattern (-P pattern) */
265bb16350dSlclee static int	io_patt = 0;		/* write pattern to disk (-P pattern) */
266bb16350dSlclee static int	io_lgeom = 0;		/* get label geometry (-g) */
267bb16350dSlclee static int	io_pgeom = 0;		/* get drive physical geometry (-G) */
268bb16350dSlclee static char	*io_sgeom = 0;		/* set label geometry (-S geom_file) */
269bb16350dSlclee static int	io_readonly = 0;	/* do not write to disk (-R) */
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */
2727c478bd9Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */
2739d5d1945Sbharding static off_t	io_offset = 0;		/* offset sector (-o offset) */
2749d5d1945Sbharding static off_t	io_size = 0;		/* size in sectors (-s size) */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /* Partition table flags */
277bb16350dSlclee static int	v_flag = 0;		/* virtual geometry-HBA flag (-v) */
278*13b70267SToomas Soome static int	stdo_flag = 0;		/* stdout flag (-W -) */
279bb16350dSlclee static int	io_fdisk = 0;		/* do fdisk operation */
280bb16350dSlclee static int	io_ifdisk = 0;		/* interactive partition */
281bb16350dSlclee static int	io_nifdisk = 0;		/* non-interactive partition (-n) */
282bb16350dSlclee 
283bb16350dSlclee static int	io_adjt = 0;		/* check/adjust VTOC (truncate (-t)) */
284bb16350dSlclee static int	io_ADJT = 0;		/* check/adjust VTOC (delete (-T)) */
285bb16350dSlclee static char	*io_ffdisk = 0;		/* input fdisk file name (-F file) */
286bb16350dSlclee static char	*io_Wfdisk = 0;		/* output fdisk file name (-W file) */
287bb16350dSlclee static char	*io_Afdisk = 0;		/* add entry to partition table (-A) */
288bb16350dSlclee static char	*io_Dfdisk = 0;		/* delete entry from part. table (-D) */
289bb16350dSlclee 
290bb16350dSlclee static char	*io_mboot = 0;		/* master boot record (-b boot_file) */
291bb16350dSlclee 
292bb16350dSlclee static struct mboot BootCod;		/* buffer for master boot record */
293bb16350dSlclee 
294bb16350dSlclee static int	io_wholedisk = 0;	/* use whole disk for Solaris (-B) */
295bb16350dSlclee static int	io_EFIdisk = 0;		/* use whole disk for EFI (-E) */
2967e934d3aSAndy Fiddaman static int	io_EFIslot = 0;		/* slot in which to place EFI entry */
2977e934d3aSAndy Fiddaman static int	io_EFIactive = 0;	/* mark EFI entry as active */
298bb16350dSlclee static int	io_debug = 0;		/* activate verbose mode (-d) */
299bb16350dSlclee static int	io_image = 0;		/* create image using geometry (-I) */
300bb16350dSlclee 
301bb16350dSlclee static struct mboot *Bootblk;		/* pointer to cut/paste sector zero */
302bb16350dSlclee static char	*Bootsect;		/* pointer to sector zero buffer */
303bb16350dSlclee static char	*Nullsect;
304342440ecSPrasad Singamsetty static struct extvtoc	disk_vtoc;	/* verify VTOC table */
305bb16350dSlclee static int	vt_inval = 0;
306bb16350dSlclee static int	no_virtgeom_ioctl = 0;	/* ioctl for virtual geometry failed */
307bb16350dSlclee static int	no_physgeom_ioctl = 0;	/* ioctl for physical geometry failed */
308bb16350dSlclee 
309bb16350dSlclee static struct ipart	Table[FD_NUMPART];
310bb16350dSlclee static struct ipart	Old_Table[FD_NUMPART];
3112f8ec719SBarry Harding static int		skip_verify[FD_NUMPART]; /* special case skip sz chk */
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /* Disk geometry information */
3141237e847Slclee static struct dk_minfo	minfo;
315bb16350dSlclee static struct dk_geom	disk_geom;
3167c478bd9Sstevel@tonic-gate 
317342440ecSPrasad Singamsetty static int Dev;			/* fd for open device */
318342440ecSPrasad Singamsetty 
3191237e847Slclee static diskaddr_t	dev_capacity;	/* number of blocks on device */
320342440ecSPrasad Singamsetty static diskaddr_t	chs_capacity;	/* Numcyl_usable * heads * sectors */
321342440ecSPrasad Singamsetty 
322342440ecSPrasad Singamsetty static int		Numcyl_usable;	/* Number of usable cylinders */
323342440ecSPrasad Singamsetty 					/*  used to limit fdisk to 2TB */
3241237e847Slclee 
3257c478bd9Sstevel@tonic-gate /* Physical geometry for the drive */
326bb16350dSlclee static int	Numcyl;			/* number of cylinders */
327bb16350dSlclee static int	heads;			/* number of heads */
328bb16350dSlclee static int	sectors;		/* number of sectors per track */
329bb16350dSlclee static int	acyl;			/* number of alternate sectors */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */
332bb16350dSlclee static int	hba_Numcyl;		/* number of cylinders */
333bb16350dSlclee static int	hba_heads;		/* number of heads */
334bb16350dSlclee static int	hba_sectors;		/* number of sectors per track */
3357c478bd9Sstevel@tonic-gate 
336bb16350dSlclee static int	sectsiz;		/* sector size */
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /* Load functions for fdisk table modification */
3397c478bd9Sstevel@tonic-gate #define	LOADFILE	0	/* load fdisk from file */
3407c478bd9Sstevel@tonic-gate #define	LOADDEL		1	/* delete an fdisk entry */
3417c478bd9Sstevel@tonic-gate #define	LOADADD		2	/* add an fdisk entry */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate #define	CBUFLEN 80
344bb16350dSlclee static char s[CBUFLEN];
345bb16350dSlclee 
346aa1b14e7SSheshadri Vasudevan #ifdef i386
347aa1b14e7SSheshadri Vasudevan /*
348aa1b14e7SSheshadri Vasudevan  * Complete list of all the 255 partition types. Some are unknown types
349aa1b14e7SSheshadri Vasudevan  * and some entries are known to be unused.
350aa1b14e7SSheshadri Vasudevan  *
351aa1b14e7SSheshadri Vasudevan  * Courtesy of http://www.win.tue.nl/~aeb/partitions/partition_types-1.html
352aa1b14e7SSheshadri Vasudevan  */
353aa1b14e7SSheshadri Vasudevan char *fdisk_part_types[] = {
354aa1b14e7SSheshadri Vasudevan 	"Empty",				/* 0 */
355aa1b14e7SSheshadri Vasudevan 	"FAT12",				/* 1 */
356aa1b14e7SSheshadri Vasudevan 	"XENIX /",				/* 2 */
357aa1b14e7SSheshadri Vasudevan 	"XENIX /usr",				/* 3 */
358aa1b14e7SSheshadri Vasudevan 	"FAT16 (Upto 32M)",			/* 4 */
359aa1b14e7SSheshadri Vasudevan 	"DOS Extended",				/* 5 */
360aa1b14e7SSheshadri Vasudevan 	"FAT16 (>32M, HUGEDOS)",		/* 6 */
361aa1b14e7SSheshadri Vasudevan 	"IFS: NTFS",				/* 7 */
362aa1b14e7SSheshadri Vasudevan 	"AIX Boot/QNX(qny)",			/* 8 */
363aa1b14e7SSheshadri Vasudevan 	"AIX Data/QNX(qnz)",			/* 9 */
364aa1b14e7SSheshadri Vasudevan 	"OS/2 Boot/Coherent swap",		/* 10 */
365aa1b14e7SSheshadri Vasudevan 	"WIN95 FAT32(Upto 2047GB)",		/* 11 */
366aa1b14e7SSheshadri Vasudevan 	"WIN95 FAT32(LBA)",			/* 12 */
367aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 13 */
368aa1b14e7SSheshadri Vasudevan 	"WIN95 FAT16(LBA)",			/* 14 */
369aa1b14e7SSheshadri Vasudevan 	"WIN95 Extended(LBA)",			/* 15 */
370aa1b14e7SSheshadri Vasudevan 	"OPUS",					/* 16 */
371aa1b14e7SSheshadri Vasudevan 	"Hidden FAT12",				/* 17 */
372aa1b14e7SSheshadri Vasudevan 	"Diagnostic",				/* 18 */
373aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 19 */
374aa1b14e7SSheshadri Vasudevan 	"Hidden FAT16(Upto 32M)",		/* 20 */
375aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 21 */
376aa1b14e7SSheshadri Vasudevan 	"Hidden FAT16(>=32M)",			/* 22 */
377aa1b14e7SSheshadri Vasudevan 	"Hidden IFS: HPFS",			/* 23 */
378aa1b14e7SSheshadri Vasudevan 	"AST SmartSleep Partition",		/* 24 */
379aa1b14e7SSheshadri Vasudevan 	"Unused/Willowtech Photon",		/* 25 */
380aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 26 */
381aa1b14e7SSheshadri Vasudevan 	"Hidden FAT32",				/* 27 */
382aa1b14e7SSheshadri Vasudevan 	"Hidden FAT32(LBA)",			/* 28 */
383aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 29 */
384aa1b14e7SSheshadri Vasudevan 	"Hidden FAT16(LBA)",			/* 30 */
385aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 31 */
386aa1b14e7SSheshadri Vasudevan 	"Unused/OSF1",				/* 32 */
387aa1b14e7SSheshadri Vasudevan 	"Reserved/FSo2(Oxygen FS)",		/* 33 */
388aa1b14e7SSheshadri Vasudevan 	"Unused/(Oxygen EXT)",			/* 34 */
389aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 35 */
390aa1b14e7SSheshadri Vasudevan 	"NEC DOS 3.x",				/* 36 */
391aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 37 */
392aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 38 */
393aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 39 */
394aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 40 */
395aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 41 */
396aa1b14e7SSheshadri Vasudevan 	"AtheOS File System",			/* 42 */
397aa1b14e7SSheshadri Vasudevan 	"SyllableSecure",			/* 43 */
398aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 44 */
399aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 45 */
400aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 46 */
401aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 47 */
402aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 48 */
403aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 49 */
404aa1b14e7SSheshadri Vasudevan 	"NOS",					/* 50 */
405aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 51 */
406aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 52 */
407aa1b14e7SSheshadri Vasudevan 	"JFS on OS/2",				/* 53 */
408aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 54 */
409aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 55 */
410aa1b14e7SSheshadri Vasudevan 	"THEOS 3.2 2GB",			/* 56 */
411aa1b14e7SSheshadri Vasudevan 	"Plan9/THEOS 4",			/* 57 */
412aa1b14e7SSheshadri Vasudevan 	"THEOS 4 4GB",				/* 58 */
413aa1b14e7SSheshadri Vasudevan 	"THEOS 4 Extended",			/* 59 */
414aa1b14e7SSheshadri Vasudevan 	"PartitionMagic Recovery",		/* 60 */
415aa1b14e7SSheshadri Vasudevan 	"Hidden NetWare",			/* 61 */
416aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 62 */
417aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 63 */
418aa1b14e7SSheshadri Vasudevan 	"Venix 80286",				/* 64 */
419aa1b14e7SSheshadri Vasudevan 	"MINIX/PPC PReP Boot",			/* 65 */
420aa1b14e7SSheshadri Vasudevan 	"Win2K Dynamic Disk/SFS(DOS)",		/* 66 */
421aa1b14e7SSheshadri Vasudevan 	"Linux+DRDOS shared",			/* 67 */
422aa1b14e7SSheshadri Vasudevan 	"GoBack partition",			/* 68 */
423aa1b14e7SSheshadri Vasudevan 	"Boot-US boot manager",			/* 69 */
424aa1b14e7SSheshadri Vasudevan 	"EUMEL/Elan",				/* 70 */
425aa1b14e7SSheshadri Vasudevan 	"EUMEL/Elan",				/* 71 */
426aa1b14e7SSheshadri Vasudevan 	"EUMEL/Elan",				/* 72 */
427aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 73 */
428aa1b14e7SSheshadri Vasudevan 	"ALFS/THIN FS for DOS",			/* 74 */
429aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 75 */
430aa1b14e7SSheshadri Vasudevan 	"Oberon partition",			/* 76 */
431aa1b14e7SSheshadri Vasudevan 	"QNX 4,x",				/* 77 */
432aa1b14e7SSheshadri Vasudevan 	"QNX 4,x 2nd Part",			/* 78 */
433aa1b14e7SSheshadri Vasudevan 	"QNX 4,x 3rd Part",			/* 79 */
434aa1b14e7SSheshadri Vasudevan 	"OnTrack DM R/O, Lynx RTOS",		/* 80 */
435aa1b14e7SSheshadri Vasudevan 	"OnTrack DM R/W, Novell",		/* 81 */
436aa1b14e7SSheshadri Vasudevan 	"CP/M",					/* 82 */
437aa1b14e7SSheshadri Vasudevan 	"Disk Manager 6.0 Aux3",		/* 83 */
438aa1b14e7SSheshadri Vasudevan 	"Disk Manager 6.0 DDO",			/* 84 */
439aa1b14e7SSheshadri Vasudevan 	"EZ-Drive",				/* 85 */
440aa1b14e7SSheshadri Vasudevan 	"Golden Bow VFeature/AT&T MS-DOS",	/* 86 */
441aa1b14e7SSheshadri Vasudevan 	"DrivePro",				/* 87 */
442aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 88 */
443aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 89 */
444aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 90 */
445aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 91 */
446aa1b14e7SSheshadri Vasudevan 	"Priam EDisk",				/* 92 */
447aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 93 */
448aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 94 */
449aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 95 */
450aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 96 */
451aa1b14e7SSheshadri Vasudevan 	"SpeedStor",				/* 97 */
452aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 98 */
453aa1b14e7SSheshadri Vasudevan 	"Unix SysV, Mach, GNU Hurd",		/* 99 */
454aa1b14e7SSheshadri Vasudevan 	"PC-ARMOUR, Netware 286",		/* 100 */
455aa1b14e7SSheshadri Vasudevan 	"Netware 386",				/* 101 */
456aa1b14e7SSheshadri Vasudevan 	"Netware SMS",				/* 102 */
457aa1b14e7SSheshadri Vasudevan 	"Novell",				/* 103 */
458aa1b14e7SSheshadri Vasudevan 	"Novell",				/* 104 */
459aa1b14e7SSheshadri Vasudevan 	"Netware NSS",				/* 105 */
460aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 106 */
461aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 107 */
462aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 108 */
463aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 109 */
464aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 110 */
465aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 111 */
466aa1b14e7SSheshadri Vasudevan 	"DiskSecure Multi-Boot",		/* 112 */
467aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 113 */
468aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 114 */
469aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 115 */
470aa1b14e7SSheshadri Vasudevan 	"Scramdisk partition",			/* 116 */
471aa1b14e7SSheshadri Vasudevan 	"IBM PC/IX",				/* 117 */
472aa1b14e7SSheshadri Vasudevan 	"Reserved",				/* 118 */
473aa1b14e7SSheshadri Vasudevan 	"M2FS/M2CS,Netware VNDI",		/* 119 */
474aa1b14e7SSheshadri Vasudevan 	"XOSL FS",				/* 120 */
475aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 121 */
476aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 122 */
477aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 123 */
478aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 124 */
479aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 125 */
480aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 126 */
481aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 127 */
482aa1b14e7SSheshadri Vasudevan 	"MINIX until 1.4a",			/* 128 */
483aa1b14e7SSheshadri Vasudevan 	"MINIX since 1.4b, early Linux",	/* 129 */
484aa1b14e7SSheshadri Vasudevan 	"Solaris/Linux swap",			/* 130 */
485aa1b14e7SSheshadri Vasudevan 	"Linux native",				/* 131 */
486aa1b14e7SSheshadri Vasudevan 	"OS/2 hidden,Win Hibernation",		/* 132 */
487aa1b14e7SSheshadri Vasudevan 	"Linux extended",			/* 133 */
488aa1b14e7SSheshadri Vasudevan 	"Old Linux RAID,NT FAT16 RAID",		/* 134 */
489aa1b14e7SSheshadri Vasudevan 	"NTFS volume set",			/* 135 */
490aa1b14e7SSheshadri Vasudevan 	"Linux plaintext part table",		/* 136 */
491aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 137 */
492aa1b14e7SSheshadri Vasudevan 	"Linux Kernel Partition",		/* 138 */
493aa1b14e7SSheshadri Vasudevan 	"Fault Tolerant FAT32 volume",		/* 139 */
494aa1b14e7SSheshadri Vasudevan 	"Fault Tolerant FAT32 volume",		/* 140 */
495aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden PDOS FAT12",		/* 141 */
496aa1b14e7SSheshadri Vasudevan 	"Linux LVM partition",			/* 142 */
497aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 143 */
498aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden PDOS FAT16",		/* 144 */
499aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden DOS EXT",		/* 145 */
500aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden FAT16 Large",	/* 146 */
501aa1b14e7SSheshadri Vasudevan 	"Hidden Linux native, Amoeba",		/* 147 */
502aa1b14e7SSheshadri Vasudevan 	"Amoeba Bad Block Table",		/* 148 */
503aa1b14e7SSheshadri Vasudevan 	"MIT EXOPC Native",			/* 149 */
504aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 150 */
505aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden PDOS FAT32",		/* 151 */
506aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden FAT32 LBA",		/* 152 */
507aa1b14e7SSheshadri Vasudevan 	"DCE376 logical drive",			/* 153 */
508aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden FAT16 LBA",		/* 154 */
509aa1b14e7SSheshadri Vasudevan 	"Free FDISK hidden DOS EXT",		/* 155 */
510aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 156 */
511aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 157 */
512aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 158 */
513aa1b14e7SSheshadri Vasudevan 	"BSD/OS",				/* 159 */
514aa1b14e7SSheshadri Vasudevan 	"Laptop hibernation",			/* 160 */
515aa1b14e7SSheshadri Vasudevan 	"Laptop hibernate,HP SpeedStor",	/* 161 */
516aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 162 */
517aa1b14e7SSheshadri Vasudevan 	"HP SpeedStor",				/* 163 */
518aa1b14e7SSheshadri Vasudevan 	"HP SpeedStor",				/* 164 */
519aa1b14e7SSheshadri Vasudevan 	"BSD/386,386BSD,NetBSD,FreeBSD",	/* 165 */
520aa1b14e7SSheshadri Vasudevan 	"OpenBSD,HP SpeedStor",			/* 166 */
521aa1b14e7SSheshadri Vasudevan 	"NeXTStep",				/* 167 */
522aa1b14e7SSheshadri Vasudevan 	"Mac OS-X",				/* 168 */
523aa1b14e7SSheshadri Vasudevan 	"NetBSD",				/* 169 */
524aa1b14e7SSheshadri Vasudevan 	"Olivetti FAT12 1.44MB Service",	/* 170 */
525aa1b14e7SSheshadri Vasudevan 	"Mac OS-X Boot",			/* 171 */
526aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 172 */
527aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 173 */
528aa1b14e7SSheshadri Vasudevan 	"ShagOS filesystem",			/* 174 */
529aa1b14e7SSheshadri Vasudevan 	"ShagOS swap",				/* 175 */
530aa1b14e7SSheshadri Vasudevan 	"BootStar Dummy",			/* 176 */
531aa1b14e7SSheshadri Vasudevan 	"HP SpeedStor",				/* 177 */
532aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 178 */
533aa1b14e7SSheshadri Vasudevan 	"HP SpeedStor",				/* 179 */
534aa1b14e7SSheshadri Vasudevan 	"HP SpeedStor",				/* 180 */
535aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 181 */
536aa1b14e7SSheshadri Vasudevan 	"Corrupted FAT16 NT Mirror Set",	/* 182 */
537aa1b14e7SSheshadri Vasudevan 	"Corrupted NTFS NT Mirror Set",		/* 183 */
538aa1b14e7SSheshadri Vasudevan 	"Old BSDI BSD/386 swap",		/* 184 */
539aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 185 */
540aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 186 */
541aa1b14e7SSheshadri Vasudevan 	"Boot Wizard hidden",			/* 187 */
542aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 188 */
543aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 189 */
544aa1b14e7SSheshadri Vasudevan 	"Solaris x86 boot",			/* 190 */
545aa1b14e7SSheshadri Vasudevan 	"Solaris2",				/* 191 */
546aa1b14e7SSheshadri Vasudevan 	"REAL/32 or Novell DOS secured",	/* 192 */
547aa1b14e7SSheshadri Vasudevan 	"DRDOS/secured(FAT12)",			/* 193 */
548aa1b14e7SSheshadri Vasudevan 	"Hidden Linux",				/* 194 */
549aa1b14e7SSheshadri Vasudevan 	"Hidden Linux swap",			/* 195 */
550aa1b14e7SSheshadri Vasudevan 	"DRDOS/secured(FAT16,< 32M)",		/* 196 */
551aa1b14e7SSheshadri Vasudevan 	"DRDOS/secured(Extended)",		/* 197 */
552aa1b14e7SSheshadri Vasudevan 	"NT corrupted FAT16 volume",		/* 198 */
553aa1b14e7SSheshadri Vasudevan 	"NT corrupted NTFS volume",		/* 199 */
554aa1b14e7SSheshadri Vasudevan 	"DRDOS8.0+",				/* 200 */
555aa1b14e7SSheshadri Vasudevan 	"DRDOS8.0+",				/* 201 */
556aa1b14e7SSheshadri Vasudevan 	"DRDOS8.0+",				/* 202 */
557aa1b14e7SSheshadri Vasudevan 	"DRDOS7.04+ secured FAT32(CHS)",	/* 203 */
558aa1b14e7SSheshadri Vasudevan 	"DRDOS7.04+ secured FAT32(LBA)",	/* 204 */
559aa1b14e7SSheshadri Vasudevan 	"CTOS Memdump",				/* 205 */
560aa1b14e7SSheshadri Vasudevan 	"DRDOS7.04+ FAT16X(LBA)",		/* 206 */
561aa1b14e7SSheshadri Vasudevan 	"DRDOS7.04+ secure EXT DOS(LBA)",	/* 207 */
562aa1b14e7SSheshadri Vasudevan 	"REAL/32 secure big, MDOS",		/* 208 */
563aa1b14e7SSheshadri Vasudevan 	"Old MDOS secure FAT12",		/* 209 */
564aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 210 */
565aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 211 */
566aa1b14e7SSheshadri Vasudevan 	"Old MDOS secure FAT16 <32M",		/* 212 */
567aa1b14e7SSheshadri Vasudevan 	"Old MDOS secure EXT",			/* 213 */
568aa1b14e7SSheshadri Vasudevan 	"Old MDOS secure FAT16 >=32M",		/* 214 */
569aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 215 */
570aa1b14e7SSheshadri Vasudevan 	"CP/M-86",				/* 216 */
571aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 217 */
572aa1b14e7SSheshadri Vasudevan 	"Non-FS Data",				/* 218 */
573aa1b14e7SSheshadri Vasudevan 	"CP/M,Concurrent DOS,CTOS",		/* 219 */
574aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 220 */
575aa1b14e7SSheshadri Vasudevan 	"Hidden CTOS memdump",			/* 221 */
576aa1b14e7SSheshadri Vasudevan 	"Dell PowerEdge utilities(FAT)",	/* 222 */
577aa1b14e7SSheshadri Vasudevan 	"DG/UX virtual disk manager",		/* 223 */
578aa1b14e7SSheshadri Vasudevan 	"ST AVFS(STMicroelectronics)",		/* 224 */
579aa1b14e7SSheshadri Vasudevan 	"SpeedStor 12-bit FAT EXT",		/* 225 */
580aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 226 */
581aa1b14e7SSheshadri Vasudevan 	"SpeedStor",				/* 227 */
582aa1b14e7SSheshadri Vasudevan 	"SpeedStor 16-bit FAT EXT",		/* 228 */
583aa1b14e7SSheshadri Vasudevan 	"Tandy MSDOS",				/* 229 */
584aa1b14e7SSheshadri Vasudevan 	"Storage Dimensions SpeedStor",		/* 230 */
585aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 231 */
586aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 232 */
587aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 233 */
588aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 234 */
589aa1b14e7SSheshadri Vasudevan 	"BeOS BFS",				/* 235 */
590aa1b14e7SSheshadri Vasudevan 	"SkyOS SkyFS",				/* 236 */
591aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 237 */
592aa1b14e7SSheshadri Vasudevan 	"EFI Header Indicator",			/* 238 */
593aa1b14e7SSheshadri Vasudevan 	"EFI Filesystem",			/* 239 */
594aa1b14e7SSheshadri Vasudevan 	"Linux/PA-RISC boot loader",		/* 240 */
595aa1b14e7SSheshadri Vasudevan 	"SpeedStor",				/* 241 */
596aa1b14e7SSheshadri Vasudevan 	"DOS 3.3+ secondary",			/* 242 */
597aa1b14e7SSheshadri Vasudevan 	"SpeedStor Reserved",			/* 243 */
598aa1b14e7SSheshadri Vasudevan 	"SpeedStor Large",			/* 244 */
599aa1b14e7SSheshadri Vasudevan 	"Prologue multi-volume",		/* 245 */
600aa1b14e7SSheshadri Vasudevan 	"SpeedStor",				/* 246 */
601aa1b14e7SSheshadri Vasudevan 	"Unused",				/* 247 */
602aa1b14e7SSheshadri Vasudevan 	"Unknown",				/* 248 */
603aa1b14e7SSheshadri Vasudevan 	"pCache",				/* 249 */
604aa1b14e7SSheshadri Vasudevan 	"Bochs",				/* 250 */
605aa1b14e7SSheshadri Vasudevan 	"VMware File System",			/* 251 */
606aa1b14e7SSheshadri Vasudevan 	"VMware swap",				/* 252 */
607aa1b14e7SSheshadri Vasudevan 	"Linux raid autodetect",		/* 253 */
608aa1b14e7SSheshadri Vasudevan 	"NT Disk Administrator hidden",		/* 254 */
609aa1b14e7SSheshadri Vasudevan 	"Xenix Bad Block Table"			/* 255 */
610aa1b14e7SSheshadri Vasudevan };
611aa1b14e7SSheshadri Vasudevan 
612aa1b14e7SSheshadri Vasudevan /* Allowed extended partition menu options */
613aa1b14e7SSheshadri Vasudevan static char ext_part_menu_opts[] = "adhipr";
614aa1b14e7SSheshadri Vasudevan 
615aa1b14e7SSheshadri Vasudevan /*
616aa1b14e7SSheshadri Vasudevan  * Structure holding all information about the extended partition
617aa1b14e7SSheshadri Vasudevan  * NOTE : As of now, there will be just one instance of ext_part_t, since most
618aa1b14e7SSheshadri Vasudevan  * known systems allow only one extended dos partition per disk.
619aa1b14e7SSheshadri Vasudevan  */
620aa1b14e7SSheshadri Vasudevan static ext_part_t *epp;
621aa1b14e7SSheshadri Vasudevan #endif
622aa1b14e7SSheshadri Vasudevan 
623bb16350dSlclee static void update_disk_and_exit(boolean_t table_changed);
624bb16350dSlclee int main(int argc, char *argv[]);
625bb16350dSlclee static int read_geom(char *sgeom);
626bb16350dSlclee static void dev_mboot_read(void);
6279d5d1945Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz);
628bb16350dSlclee static void mboot_read(void);
629bb16350dSlclee static void fill_patt(void);
630bb16350dSlclee static void abs_read(void);
631bb16350dSlclee static void abs_write(void);
632bb16350dSlclee static void load(int funct, char *file);
633bb16350dSlclee static void Set_Table_CHS_Values(int ti);
6345828e6d8SShidokht Yadegari static int nopartdefined();
635bb16350dSlclee static int insert_tbl(int id, int act,
636bb16350dSlclee     int bhead, int bsect, int bcyl,
637bb16350dSlclee     int ehead, int esect, int ecyl,
6385828e6d8SShidokht Yadegari     uint32_t rsect, uint32_t numsect, int startindex);
6392f8ec719SBarry Harding static int entry_from_old_table(int id, int act,
6402f8ec719SBarry Harding     int bhead, int bsect, int bcyl,
6412f8ec719SBarry Harding     int ehead, int esect, int ecyl,
6425828e6d8SShidokht Yadegari     uint32_t rsect, uint32_t numsect, int startindex);
643bb16350dSlclee static int verify_tbl(void);
644bb16350dSlclee static int pars_fdisk(char *line,
645bb16350dSlclee     int *id, int *act,
646bb16350dSlclee     int *bhead, int *bsect, int *bcyl,
647bb16350dSlclee     int *ehead, int *esect, int *ecyl,
648342440ecSPrasad Singamsetty     uint32_t *rsect, uint32_t *numsect);
649342440ecSPrasad Singamsetty static int validate_part(int id, uint32_t rsect, uint32_t numsect);
650bb16350dSlclee static void stage0(void);
651bb16350dSlclee static int pcreate(void);
652bb16350dSlclee static int specify(uchar_t tsystid);
653bb16350dSlclee static void dispmenu(void);
654bb16350dSlclee static int pchange(void);
655bb16350dSlclee static int ppartid(void);
656bb16350dSlclee static char pdelete(void);
657bb16350dSlclee static void rm_blanks(char *s);
658bb16350dSlclee static int getcyl(void);
659bb16350dSlclee static void disptbl(void);
660bb16350dSlclee static void print_Table(void);
661bb16350dSlclee static void copy_Table_to_Old_Table(void);
662bb16350dSlclee static void nulltbl(void);
663bb16350dSlclee static void copy_Bootblk_to_Table(void);
664bb16350dSlclee static void fill_ipart(char *bootptr, struct ipart *partp);
665bb16350dSlclee #ifdef sparc
666bb16350dSlclee uchar_t getbyte(char **bp);
667bb16350dSlclee uint32_t getlong(char **bp);
668bb16350dSlclee #endif
669bb16350dSlclee static void copy_Table_to_Bootblk(void);
670bb16350dSlclee static int TableChanged(void);
671bb16350dSlclee static void ffile_write(char *file);
672bb16350dSlclee static void fix_slice(void);
673bb16350dSlclee static int yesno(void);
674bb16350dSlclee static int readvtoc(void);
675bb16350dSlclee static int writevtoc(void);
676bb16350dSlclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc);
677bb16350dSlclee static int clear_efi(void);
678bb16350dSlclee static void clear_vtoc(int table, int part);
679bb16350dSlclee static int lecture_and_query(char *warning, char *devname);
680bb16350dSlclee static void sanity_check_provided_device(char *devname, int fd);
681bb16350dSlclee static char *get_node(char *devname);
6827e934d3aSAndy Fiddaman static int efi_create(void);
6837c478bd9Sstevel@tonic-gate 
684aa1b14e7SSheshadri Vasudevan #ifdef i386
685aa1b14e7SSheshadri Vasudevan static void id_to_name(uchar_t sysid, char *buffer);
686aa1b14e7SSheshadri Vasudevan static void ext_read_input(char *buf);
687aa1b14e7SSheshadri Vasudevan static int ext_read_options(char *buf);
688aa1b14e7SSheshadri Vasudevan static int ext_invalid_option(char ch);
689aa1b14e7SSheshadri Vasudevan static void ext_read_valid_part_num(int *pno);
690aa1b14e7SSheshadri Vasudevan static void ext_read_valid_part_id(uchar_t *partid);
691aa1b14e7SSheshadri Vasudevan static int ext_read_valid_partition_start(uint32_t *begsec);
692aa1b14e7SSheshadri Vasudevan static void ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec);
693aa1b14e7SSheshadri Vasudevan static void ext_part_menu();
694aa1b14e7SSheshadri Vasudevan static void add_logical_drive();
695aa1b14e7SSheshadri Vasudevan static void delete_logical_drive();
696aa1b14e7SSheshadri Vasudevan static void ext_print_help_menu();
697aa1b14e7SSheshadri Vasudevan static void ext_change_logical_drive_id();
698aa1b14e7SSheshadri Vasudevan static void ext_print_part_types();
699aa1b14e7SSheshadri Vasudevan static void ext_print_logical_drive_layout();
700aa1b14e7SSheshadri Vasudevan static void preach_and_continue();
701aa1b14e7SSheshadri Vasudevan #ifdef DEBUG
702aa1b14e7SSheshadri Vasudevan static void ext_print_logdrive_layout_debug();
703aa1b14e7SSheshadri Vasudevan #endif	/* DEBUG */
704aa1b14e7SSheshadri Vasudevan #endif	/* i386 */
705aa1b14e7SSheshadri Vasudevan 
706aa1b14e7SSheshadri Vasudevan /*
707aa1b14e7SSheshadri Vasudevan  * This function is called only during the non-interactive mode.
708aa1b14e7SSheshadri Vasudevan  * It is touchy and does not tolerate any errors. If there are
709aa1b14e7SSheshadri Vasudevan  * mounted logical drives, changes to the partition table
7107e934d3aSAndy Fiddaman  * are disallowed.
711aa1b14e7SSheshadri Vasudevan  */
7127c478bd9Sstevel@tonic-gate static void
update_disk_and_exit(boolean_t table_changed)7137c478bd9Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed)
7147c478bd9Sstevel@tonic-gate {
715aa1b14e7SSheshadri Vasudevan #ifdef i386
716aa1b14e7SSheshadri Vasudevan 	int rval;
717aa1b14e7SSheshadri Vasudevan #endif
7187c478bd9Sstevel@tonic-gate 	if (table_changed) {
7197c478bd9Sstevel@tonic-gate 		/*
7207c478bd9Sstevel@tonic-gate 		 * Copy the new table back to the sector buffer
7217c478bd9Sstevel@tonic-gate 		 * and write it to disk
7227c478bd9Sstevel@tonic-gate 		 */
7237c478bd9Sstevel@tonic-gate 		copy_Table_to_Bootblk();
7247c478bd9Sstevel@tonic-gate 		dev_mboot_write(0, Bootsect, sectsiz);
7257c478bd9Sstevel@tonic-gate 	}
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	/* If the VTOC table is wrong fix it (truncation only) */
7287c478bd9Sstevel@tonic-gate 	if (io_adjt)
7297c478bd9Sstevel@tonic-gate 		fix_slice();
7307c478bd9Sstevel@tonic-gate 
731aa1b14e7SSheshadri Vasudevan #ifdef i386
732aa1b14e7SSheshadri Vasudevan 	if (!io_readonly) {
733aa1b14e7SSheshadri Vasudevan 		rval = fdisk_commit_ext_part(epp);
734aa1b14e7SSheshadri Vasudevan 		switch (rval) {
735aa1b14e7SSheshadri Vasudevan 			case FDISK_SUCCESS:
736aa1b14e7SSheshadri Vasudevan 				/* Success */
737aa1b14e7SSheshadri Vasudevan 				break;
738aa1b14e7SSheshadri Vasudevan 			case FDISK_ENOEXTPART:
739aa1b14e7SSheshadri Vasudevan 				/* Nothing to do */
740aa1b14e7SSheshadri Vasudevan 				break;
741aa1b14e7SSheshadri Vasudevan 			default:
742b8a1bc93SShidokht Yadegari 				(void) fprintf(stderr, "Error in"
743aa1b14e7SSheshadri Vasudevan 				    " fdisk_commit_ext_part\n");
744aa1b14e7SSheshadri Vasudevan 				exit(rval);
745aa1b14e7SSheshadri Vasudevan 		}
746aa1b14e7SSheshadri Vasudevan 	}
747aa1b14e7SSheshadri Vasudevan 	libfdisk_fini(&epp);
748aa1b14e7SSheshadri Vasudevan #endif
7497c478bd9Sstevel@tonic-gate 	exit(0);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * main
7547c478bd9Sstevel@tonic-gate  * Process command-line options.
7557c478bd9Sstevel@tonic-gate  */
756bb16350dSlclee int
main(int argc,char * argv[])7577c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
7587c478bd9Sstevel@tonic-gate {
759bb16350dSlclee 	int c, i;
7607c478bd9Sstevel@tonic-gate 	extern	int optind;
7617c478bd9Sstevel@tonic-gate 	extern	char *optarg;
7627c478bd9Sstevel@tonic-gate 	int	errflg = 0;
7637c478bd9Sstevel@tonic-gate 	int	diag_cnt = 0;
7647c478bd9Sstevel@tonic-gate 	int openmode;
765aa1b14e7SSheshadri Vasudevan #ifdef i386
766