10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55169Slclee * Common Development and Distribution License (the "License"). 65169Slclee * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215169Slclee 220Sstevel@tonic-gate /* 238904SBarry.Harding@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 320Sstevel@tonic-gate /* All Rights Reserved */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 350Sstevel@tonic-gate * PROGRAM: fdisk(1M) 360Sstevel@tonic-gate * This program reads the partition table on the specified device and 370Sstevel@tonic-gate * also reads the drive parameters. The user can perform various 380Sstevel@tonic-gate * operations from a supplied menu or from the command line. Diagnostic 390Sstevel@tonic-gate * options are also available. 400Sstevel@tonic-gate */ 410Sstevel@tonic-gate #include <stdio.h> 420Sstevel@tonic-gate #include <stdlib.h> 430Sstevel@tonic-gate #include <string.h> 440Sstevel@tonic-gate #include <unistd.h> 450Sstevel@tonic-gate #include <errno.h> 460Sstevel@tonic-gate #include <fcntl.h> 470Sstevel@tonic-gate #include <ctype.h> 480Sstevel@tonic-gate #include <sys/stat.h> 490Sstevel@tonic-gate #include <sys/types.h> 507563SPrasad.Singamsetty@Sun.COM #include <limits.h> 510Sstevel@tonic-gate #include <sys/param.h> 520Sstevel@tonic-gate #include <sys/systeminfo.h> 530Sstevel@tonic-gate #include <sys/efi_partition.h> 540Sstevel@tonic-gate #include <sys/byteorder.h> 550Sstevel@tonic-gate #include <sys/systeminfo.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 580Sstevel@tonic-gate #include <sys/dkio.h> 590Sstevel@tonic-gate #include <sys/vtoc.h> 6010021SSheshadri.Vasudevan@Sun.COM #ifdef i386 6110021SSheshadri.Vasudevan@Sun.COM #include <sys/tty.h> 6210021SSheshadri.Vasudevan@Sun.COM #include <libfdisk.h> 6310021SSheshadri.Vasudevan@Sun.COM #endif 640Sstevel@tonic-gate 650Sstevel@tonic-gate #define CLR_SCR "[1;1H[0J" 660Sstevel@tonic-gate #define CLR_LIN "[0K" 670Sstevel@tonic-gate #define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \ 680Sstevel@tonic-gate "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H" 690Sstevel@tonic-gate #define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K" 7010021SSheshadri.Vasudevan@Sun.COM 7110021SSheshadri.Vasudevan@Sun.COM #ifdef i386 7210021SSheshadri.Vasudevan@Sun.COM #define W_LINE "[11;1H[0K" 7310021SSheshadri.Vasudevan@Sun.COM #else 740Sstevel@tonic-gate #define W_LINE "[12;1H[0K[11;1H[0K" 7510021SSheshadri.Vasudevan@Sun.COM #endif 7610021SSheshadri.Vasudevan@Sun.COM 770Sstevel@tonic-gate #define E_LINE "[24;1H[0K[23;1H[0K" 7810021SSheshadri.Vasudevan@Sun.COM 7910021SSheshadri.Vasudevan@Sun.COM #ifdef i386 8010021SSheshadri.Vasudevan@Sun.COM #define M_LINE "[12;1H[0K[13;1H[0K[14;1H[0K[15;1H[0K" \ 8110021SSheshadri.Vasudevan@Sun.COM "[16;1H[0K[17;1H[0K[18;1H[0K[19;1H[0K[12;1H" 8210021SSheshadri.Vasudevan@Sun.COM #else 830Sstevel@tonic-gate #define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \ 840Sstevel@tonic-gate "[0K[18;1H[0K[19;1H[0K[13;1H" 8510021SSheshadri.Vasudevan@Sun.COM #endif 8610021SSheshadri.Vasudevan@Sun.COM 870Sstevel@tonic-gate #define T_LINE "[1;1H[0K" 880Sstevel@tonic-gate 890Sstevel@tonic-gate #define DEFAULT_PATH "/dev/rdsk/" 900Sstevel@tonic-gate 918333SSuhasini.Peddada@Sun.COM /* XXX - should be in fdisk.h, used by sd as well */ 928333SSuhasini.Peddada@Sun.COM 938333SSuhasini.Peddada@Sun.COM /* 948333SSuhasini.Peddada@Sun.COM * the MAX values are the maximum usable values for BIOS chs values 958333SSuhasini.Peddada@Sun.COM * The MAX_CYL value of 1022 is the maximum usable value 968333SSuhasini.Peddada@Sun.COM * the value of 1023 is a fence value, 978333SSuhasini.Peddada@Sun.COM * indicating no CHS geometry exists for the corresponding LBA value. 988333SSuhasini.Peddada@Sun.COM * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1) 998333SSuhasini.Peddada@Sun.COM * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT) 1008333SSuhasini.Peddada@Sun.COM */ 1018333SSuhasini.Peddada@Sun.COM #define MAX_SECT (63) 1028333SSuhasini.Peddada@Sun.COM #define MAX_CYL (1022) 1038333SSuhasini.Peddada@Sun.COM #define MAX_HEAD (254) 1048333SSuhasini.Peddada@Sun.COM 1057563SPrasad.Singamsetty@Sun.COM #define DK_MAX_2TB UINT32_MAX /* Max # of sectors in 2TB */ 1067563SPrasad.Singamsetty@Sun.COM 1070Sstevel@tonic-gate /* for clear_vtoc() */ 1080Sstevel@tonic-gate #define OLD 0 1090Sstevel@tonic-gate #define NEW 1 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate /* readvtoc/writevtoc return codes */ 1120Sstevel@tonic-gate #define VTOC_OK 0 /* Good VTOC */ 1130Sstevel@tonic-gate #define VTOC_INVAL 1 /* invalid VTOC */ 1140Sstevel@tonic-gate #define VTOC_NOTSUP 2 /* operation not supported - EFI label */ 1150Sstevel@tonic-gate #define VTOC_RWERR 3 /* couldn't read or write VTOC */ 1160Sstevel@tonic-gate 1178333SSuhasini.Peddada@Sun.COM /* 1188333SSuhasini.Peddada@Sun.COM * Support for fdisk(1M) on the SPARC platform 1198333SSuhasini.Peddada@Sun.COM * In order to convert little endian values to big endian for SPARC, 1208333SSuhasini.Peddada@Sun.COM * byte/short and long values must be swapped. 1218333SSuhasini.Peddada@Sun.COM * These swapping macros will be used to access information in the 1228333SSuhasini.Peddada@Sun.COM * mboot and ipart structures. 1238333SSuhasini.Peddada@Sun.COM */ 1248333SSuhasini.Peddada@Sun.COM 1258333SSuhasini.Peddada@Sun.COM #ifdef sparc 1268333SSuhasini.Peddada@Sun.COM #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 1278333SSuhasini.Peddada@Sun.COM #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 1288333SSuhasini.Peddada@Sun.COM (les((unsigned)((val)&0xffff0000)>>16))) 1298333SSuhasini.Peddada@Sun.COM #else 1308333SSuhasini.Peddada@Sun.COM #define les(val) (val) 1318333SSuhasini.Peddada@Sun.COM #define lel(val) (val) 1328333SSuhasini.Peddada@Sun.COM #endif 1338333SSuhasini.Peddada@Sun.COM 1340Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1356549Sbharding #define VTOC_OFFSET 1 1360Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8) 1370Sstevel@tonic-gate #define VTOC_OFFSET 0 1380Sstevel@tonic-gate #else 1390Sstevel@tonic-gate #error No VTOC format defined. 1400Sstevel@tonic-gate #endif 1410Sstevel@tonic-gate 14210021SSheshadri.Vasudevan@Sun.COM #ifdef i386 14310021SSheshadri.Vasudevan@Sun.COM #define FDISK_KB (1024) 14410021SSheshadri.Vasudevan@Sun.COM #define FDISK_MB (FDISK_KB * 1024) 14510021SSheshadri.Vasudevan@Sun.COM #define FDISK_GB (FDISK_MB * 1024) 14610021SSheshadri.Vasudevan@Sun.COM #define TRUE 1 14710021SSheshadri.Vasudevan@Sun.COM 14810021SSheshadri.Vasudevan@Sun.COM #define FDISK_MAX_VALID_PART_ID 255 14910021SSheshadri.Vasudevan@Sun.COM #define FDISK_MAX_VALID_PART_NUM_DIGITS 2 15010021SSheshadri.Vasudevan@Sun.COM #define FDISK_MAX_VALID_PART_ID_DIGITS 3 15110021SSheshadri.Vasudevan@Sun.COM 15210021SSheshadri.Vasudevan@Sun.COM /* Maximum number of digits for a valid partition size */ 15310021SSheshadri.Vasudevan@Sun.COM #define FDISK_MAX_VALID_CYL_NUM_DIGITS 10 15410021SSheshadri.Vasudevan@Sun.COM 15510021SSheshadri.Vasudevan@Sun.COM /* Minimum partition size in cylinders */ 15610021SSheshadri.Vasudevan@Sun.COM #define FDISK_MIN_PART_SIZE 1 15710021SSheshadri.Vasudevan@Sun.COM #endif 15810021SSheshadri.Vasudevan@Sun.COM 159251Slclee static char Usage[] = "Usage: fdisk\n" 1600Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1610Sstevel@tonic-gate "[ -b masterboot ]\n" 1620Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1630Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n" 1640Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n" 1650Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice"; 1660Sstevel@tonic-gate 167251Slclee static char Usage1[] = " Partition options:\n" 1680Sstevel@tonic-gate " -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1690Sstevel@tonic-gate " Create a partition with specific attributes:\n" 1700Sstevel@tonic-gate " id = system id number (fdisk.h) for the partition type\n" 1710Sstevel@tonic-gate " act = active partition flag (0 is off and 128 is on)\n" 1720Sstevel@tonic-gate " bhead = beginning head for start of partition\n" 1730Sstevel@tonic-gate " bsect = beginning sector for start of partition\n" 1740Sstevel@tonic-gate " bcyl = beginning cylinder for start of partition\n" 1750Sstevel@tonic-gate " ehead = ending head for end of partition\n" 1760Sstevel@tonic-gate " esect = ending sector for end of partition\n" 1770Sstevel@tonic-gate " ecyl = ending cylinder for end of partition\n" 1780Sstevel@tonic-gate " rsect = sector number from start of disk for\n" 1790Sstevel@tonic-gate " start of partition\n" 1800Sstevel@tonic-gate " numsect = partition size in sectors\n" 1810Sstevel@tonic-gate " -b master_boot\n" 1820Sstevel@tonic-gate " Use master_boot as the master boot file.\n" 1830Sstevel@tonic-gate " -B Create one Solaris partition that uses the entire disk.\n" 1840Sstevel@tonic-gate " -E Create one EFI partition that uses the entire disk.\n" 1850Sstevel@tonic-gate " -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1860Sstevel@tonic-gate " Delete a partition. See attribute definitions for -A.\n" 1870Sstevel@tonic-gate " -F fdisk_file\n" 1880Sstevel@tonic-gate " Use fdisk_file to initialize on-line fdisk table.\n" 1890Sstevel@tonic-gate " -I Forego device checks. Generate a file image of what would go\n" 1900Sstevel@tonic-gate " on a disk using the geometry specified with the -S option.\n" 1910Sstevel@tonic-gate " -n Do not run in interactive mode.\n" 1920Sstevel@tonic-gate " -R Open the disk device as read-only.\n" 1930Sstevel@tonic-gate " -t Check and adjust VTOC to be consistent with fdisk table.\n" 1940Sstevel@tonic-gate " VTOC slices exceeding the partition size will be truncated.\n" 1950Sstevel@tonic-gate " -T Check and adjust VTOC to be consistent with fdisk table.\n" 1960Sstevel@tonic-gate " VTOC slices exceeding the partition size will be removed.\n" 1970Sstevel@tonic-gate " -W fdisk_file\n" 1980Sstevel@tonic-gate " Write on-disk table to fdisk_file.\n" 1990Sstevel@tonic-gate " -W - Write on-disk table to standard output.\n" 2000Sstevel@tonic-gate " -v Display virtual geometry. Must be used with the -W option.\n" 2010Sstevel@tonic-gate " Diagnostic options:\n" 2020Sstevel@tonic-gate " -d Activate debug information about progress.\n" 2030Sstevel@tonic-gate " -g Write label geometry to standard output:\n" 2040Sstevel@tonic-gate " PCYL number of physical cylinders\n" 2050Sstevel@tonic-gate " NCYL number of usable cylinders\n" 2060Sstevel@tonic-gate " ACYL number of alternate cylinders\n" 2070Sstevel@tonic-gate " BCYL cylinder offset\n" 2080Sstevel@tonic-gate " NHEADS number of heads\n" 2090Sstevel@tonic-gate " NSECTORS number of sectors per track\n" 2100Sstevel@tonic-gate " SECTSIZ size of a sector in bytes\n" 2110Sstevel@tonic-gate " -G Write physical geometry to standard output (see -g).\n" 2120Sstevel@tonic-gate " -h Issue this verbose help message.\n" 2130Sstevel@tonic-gate " -o offset\n" 2140Sstevel@tonic-gate " Block offset from start of disk (default 0). Ignored if\n" 2150Sstevel@tonic-gate " -P # specified.\n" 2160Sstevel@tonic-gate " -P fill_patt\n" 2170Sstevel@tonic-gate " Fill disk with pattern fill_patt. fill_patt can be decimal or\n" 2180Sstevel@tonic-gate " hexadecimal and is used as number for constant long word\n" 2190Sstevel@tonic-gate " pattern. If fill_patt is \"#\" then pattern of block #\n" 2200Sstevel@tonic-gate " for each block. Pattern is put in each block as long words\n" 2210Sstevel@tonic-gate " and fills each block (see -o and -s).\n" 2220Sstevel@tonic-gate " -r Read from a disk to stdout (see -o and -s).\n" 2230Sstevel@tonic-gate " -s size Number of blocks on which to perform operation (see -o).\n" 2240Sstevel@tonic-gate " -S geom_file\n" 2250Sstevel@tonic-gate " Use geom_file to set the label geometry (see -g).\n" 2260Sstevel@tonic-gate " -w Write to a disk from stdin (see -o and -s)."; 2270Sstevel@tonic-gate 228251Slclee static char Ostr[] = "Other OS"; 229251Slclee static char Dstr[] = "DOS12"; 230251Slclee static char D16str[] = "DOS16"; 231251Slclee static char DDstr[] = "DOS-DATA"; 232251Slclee static char EDstr[] = "EXT-DOS"; 233251Slclee static char DBstr[] = "DOS-BIG"; 234251Slclee static char PCstr[] = "PCIX"; 235251Slclee static char Ustr[] = "UNIX System"; 236251Slclee static char SUstr[] = "Solaris"; 237251Slclee static char SU2str[] = "Solaris2"; 238251Slclee static char X86str[] = "x86 Boot"; 239251Slclee static char DIAGstr[] = "Diagnostic"; 240251Slclee static char IFSstr[] = "IFS: NTFS"; 241251Slclee static char AIXstr[] = "AIX Boot"; 242251Slclee static char AIXDstr[] = "AIX Data"; 243251Slclee static char OS2str[] = "OS/2 Boot"; 244251Slclee static char WINstr[] = "Win95 FAT32"; 245251Slclee static char EWINstr[] = "Ext Win95"; 246251Slclee static char FAT95str[] = "FAT16 LBA"; 247251Slclee static char EXTLstr[] = "EXT LBA"; 248251Slclee static char LINUXstr[] = "Linux"; 249251Slclee static char CPMstr[] = "CP/M"; 25010021SSheshadri.Vasudevan@Sun.COM static char NOV2str[] = "Netware 286"; 251251Slclee static char NOVstr[] = "Netware 3.x+"; 252251Slclee static char QNXstr[] = "QNX 4.x"; 253251Slclee static char QNX2str[] = "QNX part 2"; 254251Slclee static char QNX3str[] = "QNX part 3"; 255251Slclee static char LINNATstr[] = "Linux native"; 25610682SSheshadri.Vasudevan@Sun.COM static char LINSWAPstr[] = "Linux swap"; 257251Slclee static char NTFSVOL1str[] = "NT volset 1"; 258251Slclee static char NTFSVOL2str[] = "NT volset 2"; 259251Slclee static char BSDstr[] = "BSD OS"; 260251Slclee static char NEXTSTEPstr[] = "NeXTSTEP"; 261251Slclee static char BSDIFSstr[] = "BSDI FS"; 262251Slclee static char BSDISWAPstr[] = "BSDI swap"; 263251Slclee static char Actvstr[] = "Active"; 264251Slclee static char EFIstr[] = "EFI"; 265251Slclee static char NAstr[] = " "; 2660Sstevel@tonic-gate 2670Sstevel@tonic-gate /* All the user options and flags */ 268251Slclee static char *Dfltdev; /* name of fixed disk drive */ 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate /* Diagnostic options */ 271251Slclee static int io_wrt = 0; /* write stdin to disk (-w) */ 272251Slclee static int io_rd = 0; /* read disk and write stdout (-r) */ 273251Slclee static char *io_fatt; /* user supplied pattern (-P pattern) */ 274251Slclee static int io_patt = 0; /* write pattern to disk (-P pattern) */ 275251Slclee static int io_lgeom = 0; /* get label geometry (-g) */ 276251Slclee static int io_pgeom = 0; /* get drive physical geometry (-G) */ 277251Slclee static char *io_sgeom = 0; /* set label geometry (-S geom_file) */ 278251Slclee static int io_readonly = 0; /* do not write to disk (-R) */ 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */ 2810Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */ 2826549Sbharding static off_t io_offset = 0; /* offset sector (-o offset) */ 2836549Sbharding static off_t io_size = 0; /* size in sectors (-s size) */ 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate /* Partition table flags */ 286251Slclee static int v_flag = 0; /* virtual geometry-HBA flag (-v) */ 287251Slclee static int stdo_flag = 0; /* stdout flag (-W -) */ 288251Slclee static int io_fdisk = 0; /* do fdisk operation */ 289251Slclee static int io_ifdisk = 0; /* interactive partition */ 290251Slclee static int io_nifdisk = 0; /* non-interactive partition (-n) */ 291251Slclee 292251Slclee static int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */ 293251Slclee static int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */ 294251Slclee static char *io_ffdisk = 0; /* input fdisk file name (-F file) */ 295251Slclee static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */ 296251Slclee static char *io_Afdisk = 0; /* add entry to partition table (-A) */ 297251Slclee static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */ 298251Slclee 299251Slclee static char *io_mboot = 0; /* master boot record (-b boot_file) */ 300251Slclee 301251Slclee static struct mboot BootCod; /* buffer for master boot record */ 302251Slclee 303251Slclee static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */ 304251Slclee static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */ 305251Slclee static int io_debug = 0; /* activate verbose mode (-d) */ 306251Slclee static int io_image = 0; /* create image using geometry (-I) */ 307251Slclee 308251Slclee static struct mboot *Bootblk; /* pointer to cut/paste sector zero */ 309251Slclee static char *Bootsect; /* pointer to sector zero buffer */ 310251Slclee static char *Nullsect; 3117563SPrasad.Singamsetty@Sun.COM static struct extvtoc disk_vtoc; /* verify VTOC table */ 312251Slclee static int vt_inval = 0; 313251Slclee static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */ 314251Slclee static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */ 315251Slclee 316251Slclee static struct ipart Table[FD_NUMPART]; 317251Slclee static struct ipart Old_Table[FD_NUMPART]; 3188904SBarry.Harding@Sun.COM static int skip_verify[FD_NUMPART]; /* special case skip sz chk */ 3190Sstevel@tonic-gate 3200Sstevel@tonic-gate /* Disk geometry information */ 3215169Slclee static struct dk_minfo minfo; 322251Slclee static struct dk_geom disk_geom; 323251Slclee 3247563SPrasad.Singamsetty@Sun.COM static int Dev; /* fd for open device */ 3257563SPrasad.Singamsetty@Sun.COM 3265169Slclee static diskaddr_t dev_capacity; /* number of blocks on device */ 3277563SPrasad.Singamsetty@Sun.COM static diskaddr_t chs_capacity; /* Numcyl_usable * heads * sectors */ 3287563SPrasad.Singamsetty@Sun.COM 3297563SPrasad.Singamsetty@Sun.COM static int Numcyl_usable; /* Number of usable cylinders */ 3307563SPrasad.Singamsetty@Sun.COM /* used to limit fdisk to 2TB */ 3317563SPrasad.Singamsetty@Sun.COM 3320Sstevel@tonic-gate /* Physical geometry for the drive */ 333251Slclee static int Numcyl; /* number of cylinders */ 334251Slclee static int heads; /* number of heads */ 335251Slclee static int sectors; /* number of sectors per track */ 336251Slclee static int acyl; /* number of alternate sectors */ 3370Sstevel@tonic-gate 3380Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */ 339251Slclee static int hba_Numcyl; /* number of cylinders */ 340251Slclee static int hba_heads; /* number of heads */ 341251Slclee static int hba_sectors; /* number of sectors per track */ 342251Slclee 343251Slclee static int sectsiz; /* sector size */ 3440Sstevel@tonic-gate 3450Sstevel@tonic-gate /* Load functions for fdisk table modification */ 3460Sstevel@tonic-gate #define LOADFILE 0 /* load fdisk from file */ 3470Sstevel@tonic-gate #define LOADDEL 1 /* delete an fdisk entry */ 3480Sstevel@tonic-gate #define LOADADD 2 /* add an fdisk entry */ 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate #define CBUFLEN 80 351251Slclee static char s[CBUFLEN]; 352251Slclee 35310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 35410021SSheshadri.Vasudevan@Sun.COM /* 35510021SSheshadri.Vasudevan@Sun.COM * Complete list of all the 255 partition types. Some are unknown types 35610021SSheshadri.Vasudevan@Sun.COM * and some entries are known to be unused. 35710021SSheshadri.Vasudevan@Sun.COM * 35810021SSheshadri.Vasudevan@Sun.COM * Courtesy of http://www.win.tue.nl/~aeb/partitions/partition_types-1.html 35910021SSheshadri.Vasudevan@Sun.COM */ 36010021SSheshadri.Vasudevan@Sun.COM char *fdisk_part_types[] = { 36110021SSheshadri.Vasudevan@Sun.COM "Empty", /* 0 */ 36210021SSheshadri.Vasudevan@Sun.COM "FAT12", /* 1 */ 36310021SSheshadri.Vasudevan@Sun.COM "XENIX /", /* 2 */ 36410021SSheshadri.Vasudevan@Sun.COM "XENIX /usr", /* 3 */ 36510021SSheshadri.Vasudevan@Sun.COM "FAT16 (Upto 32M)", /* 4 */ 36610021SSheshadri.Vasudevan@Sun.COM "DOS Extended", /* 5 */ 36710021SSheshadri.Vasudevan@Sun.COM "FAT16 (>32M, HUGEDOS)", /* 6 */ 36810021SSheshadri.Vasudevan@Sun.COM "IFS: NTFS", /* 7 */ 36910021SSheshadri.Vasudevan@Sun.COM "AIX Boot/QNX(qny)", /* 8 */ 37010021SSheshadri.Vasudevan@Sun.COM "AIX Data/QNX(qnz)", /* 9 */ 37110021SSheshadri.Vasudevan@Sun.COM "OS/2 Boot/Coherent swap", /* 10 */ 37210021SSheshadri.Vasudevan@Sun.COM "WIN95 FAT32(Upto 2047GB)", /* 11 */ 37310021SSheshadri.Vasudevan@Sun.COM "WIN95 FAT32(LBA)", /* 12 */ 37410021SSheshadri.Vasudevan@Sun.COM "Unused", /* 13 */ 37510021SSheshadri.Vasudevan@Sun.COM "WIN95 FAT16(LBA)", /* 14 */ 37610021SSheshadri.Vasudevan@Sun.COM "WIN95 Extended(LBA)", /* 15 */ 37710021SSheshadri.Vasudevan@Sun.COM "OPUS", /* 16 */ 37810021SSheshadri.Vasudevan@Sun.COM "Hidden FAT12", /* 17 */ 37910021SSheshadri.Vasudevan@Sun.COM "Diagnostic", /* 18 */ 38010021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 19 */ 38110021SSheshadri.Vasudevan@Sun.COM "Hidden FAT16(Upto 32M)", /* 20 */ 38210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 21 */ 38310021SSheshadri.Vasudevan@Sun.COM "Hidden FAT16(>=32M)", /* 22 */ 38410021SSheshadri.Vasudevan@Sun.COM "Hidden IFS: HPFS", /* 23 */ 38510021SSheshadri.Vasudevan@Sun.COM "AST SmartSleep Partition", /* 24 */ 38610021SSheshadri.Vasudevan@Sun.COM "Unused/Willowtech Photon", /* 25 */ 38710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 26 */ 38810021SSheshadri.Vasudevan@Sun.COM "Hidden FAT32", /* 27 */ 38910021SSheshadri.Vasudevan@Sun.COM "Hidden FAT32(LBA)", /* 28 */ 39010021SSheshadri.Vasudevan@Sun.COM "Unused", /* 29 */ 39110021SSheshadri.Vasudevan@Sun.COM "Hidden FAT16(LBA)", /* 30 */ 39210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 31 */ 39310021SSheshadri.Vasudevan@Sun.COM "Unused/OSF1", /* 32 */ 39410021SSheshadri.Vasudevan@Sun.COM "Reserved/FSo2(Oxygen FS)", /* 33 */ 39510021SSheshadri.Vasudevan@Sun.COM "Unused/(Oxygen EXT)", /* 34 */ 39610021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 35 */ 39710021SSheshadri.Vasudevan@Sun.COM "NEC DOS 3.x", /* 36 */ 39810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 37 */ 39910021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 38 */ 40010021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 39 */ 40110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 40 */ 40210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 41 */ 40310021SSheshadri.Vasudevan@Sun.COM "AtheOS File System", /* 42 */ 40410021SSheshadri.Vasudevan@Sun.COM "SyllableSecure", /* 43 */ 40510021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 44 */ 40610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 45 */ 40710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 46 */ 40810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 47 */ 40910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 48 */ 41010021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 49 */ 41110021SSheshadri.Vasudevan@Sun.COM "NOS", /* 50 */ 41210021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 51 */ 41310021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 52 */ 41410021SSheshadri.Vasudevan@Sun.COM "JFS on OS/2", /* 53 */ 41510021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 54 */ 41610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 55 */ 41710021SSheshadri.Vasudevan@Sun.COM "THEOS 3.2 2GB", /* 56 */ 41810021SSheshadri.Vasudevan@Sun.COM "Plan9/THEOS 4", /* 57 */ 41910021SSheshadri.Vasudevan@Sun.COM "THEOS 4 4GB", /* 58 */ 42010021SSheshadri.Vasudevan@Sun.COM "THEOS 4 Extended", /* 59 */ 42110021SSheshadri.Vasudevan@Sun.COM "PartitionMagic Recovery", /* 60 */ 42210021SSheshadri.Vasudevan@Sun.COM "Hidden NetWare", /* 61 */ 42310021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 62 */ 42410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 63 */ 42510021SSheshadri.Vasudevan@Sun.COM "Venix 80286", /* 64 */ 42610021SSheshadri.Vasudevan@Sun.COM "MINIX/PPC PReP Boot", /* 65 */ 42710021SSheshadri.Vasudevan@Sun.COM "Win2K Dynamic Disk/SFS(DOS)", /* 66 */ 42810021SSheshadri.Vasudevan@Sun.COM "Linux+DRDOS shared", /* 67 */ 42910021SSheshadri.Vasudevan@Sun.COM "GoBack partition", /* 68 */ 43010021SSheshadri.Vasudevan@Sun.COM "Boot-US boot manager", /* 69 */ 43110021SSheshadri.Vasudevan@Sun.COM "EUMEL/Elan", /* 70 */ 43210021SSheshadri.Vasudevan@Sun.COM "EUMEL/Elan", /* 71 */ 43310021SSheshadri.Vasudevan@Sun.COM "EUMEL/Elan", /* 72 */ 43410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 73 */ 43510021SSheshadri.Vasudevan@Sun.COM "ALFS/THIN FS for DOS", /* 74 */ 43610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 75 */ 43710021SSheshadri.Vasudevan@Sun.COM "Oberon partition", /* 76 */ 43810021SSheshadri.Vasudevan@Sun.COM "QNX 4,x", /* 77 */ 43910021SSheshadri.Vasudevan@Sun.COM "QNX 4,x 2nd Part", /* 78 */ 44010021SSheshadri.Vasudevan@Sun.COM "QNX 4,x 3rd Part", /* 79 */ 44110021SSheshadri.Vasudevan@Sun.COM "OnTrack DM R/O, Lynx RTOS", /* 80 */ 44210021SSheshadri.Vasudevan@Sun.COM "OnTrack DM R/W, Novell", /* 81 */ 44310021SSheshadri.Vasudevan@Sun.COM "CP/M", /* 82 */ 44410021SSheshadri.Vasudevan@Sun.COM "Disk Manager 6.0 Aux3", /* 83 */ 44510021SSheshadri.Vasudevan@Sun.COM "Disk Manager 6.0 DDO", /* 84 */ 44610021SSheshadri.Vasudevan@Sun.COM "EZ-Drive", /* 85 */ 44710021SSheshadri.Vasudevan@Sun.COM "Golden Bow VFeature/AT&T MS-DOS", /* 86 */ 44810021SSheshadri.Vasudevan@Sun.COM "DrivePro", /* 87 */ 44910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 88 */ 45010021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 89 */ 45110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 90 */ 45210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 91 */ 45310021SSheshadri.Vasudevan@Sun.COM "Priam EDisk", /* 92 */ 45410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 93 */ 45510021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 94 */ 45610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 95 */ 45710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 96 */ 45810021SSheshadri.Vasudevan@Sun.COM "SpeedStor", /* 97 */ 45910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 98 */ 46010021SSheshadri.Vasudevan@Sun.COM "Unix SysV, Mach, GNU Hurd", /* 99 */ 46110021SSheshadri.Vasudevan@Sun.COM "PC-ARMOUR, Netware 286", /* 100 */ 46210021SSheshadri.Vasudevan@Sun.COM "Netware 386", /* 101 */ 46310021SSheshadri.Vasudevan@Sun.COM "Netware SMS", /* 102 */ 46410021SSheshadri.Vasudevan@Sun.COM "Novell", /* 103 */ 46510021SSheshadri.Vasudevan@Sun.COM "Novell", /* 104 */ 46610021SSheshadri.Vasudevan@Sun.COM "Netware NSS", /* 105 */ 46710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 106 */ 46810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 107 */ 46910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 108 */ 47010021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 109 */ 47110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 110 */ 47210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 111 */ 47310021SSheshadri.Vasudevan@Sun.COM "DiskSecure Multi-Boot", /* 112 */ 47410021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 113 */ 47510021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 114 */ 47610021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 115 */ 47710021SSheshadri.Vasudevan@Sun.COM "Scramdisk partition", /* 116 */ 47810021SSheshadri.Vasudevan@Sun.COM "IBM PC/IX", /* 117 */ 47910021SSheshadri.Vasudevan@Sun.COM "Reserved", /* 118 */ 48010021SSheshadri.Vasudevan@Sun.COM "M2FS/M2CS,Netware VNDI", /* 119 */ 48110021SSheshadri.Vasudevan@Sun.COM "XOSL FS", /* 120 */ 48210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 121 */ 48310021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 122 */ 48410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 123 */ 48510021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 124 */ 48610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 125 */ 48710021SSheshadri.Vasudevan@Sun.COM "Unused", /* 126 */ 48810021SSheshadri.Vasudevan@Sun.COM "Unused", /* 127 */ 48910021SSheshadri.Vasudevan@Sun.COM "MINIX until 1.4a", /* 128 */ 49010021SSheshadri.Vasudevan@Sun.COM "MINIX since 1.4b, early Linux", /* 129 */ 49110021SSheshadri.Vasudevan@Sun.COM "Solaris/Linux swap", /* 130 */ 49210021SSheshadri.Vasudevan@Sun.COM "Linux native", /* 131 */ 49310021SSheshadri.Vasudevan@Sun.COM "OS/2 hidden,Win Hibernation", /* 132 */ 49410021SSheshadri.Vasudevan@Sun.COM "Linux extended", /* 133 */ 49510021SSheshadri.Vasudevan@Sun.COM "Old Linux RAID,NT FAT16 RAID", /* 134 */ 49610021SSheshadri.Vasudevan@Sun.COM "NTFS volume set", /* 135 */ 49710021SSheshadri.Vasudevan@Sun.COM "Linux plaintext part table", /* 136 */ 49810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 137 */ 49910021SSheshadri.Vasudevan@Sun.COM "Linux Kernel Partition", /* 138 */ 50010021SSheshadri.Vasudevan@Sun.COM "Fault Tolerant FAT32 volume", /* 139 */ 50110021SSheshadri.Vasudevan@Sun.COM "Fault Tolerant FAT32 volume", /* 140 */ 50210021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden PDOS FAT12", /* 141 */ 50310021SSheshadri.Vasudevan@Sun.COM "Linux LVM partition", /* 142 */ 50410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 143 */ 50510021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden PDOS FAT16", /* 144 */ 50610021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden DOS EXT", /* 145 */ 50710021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden FAT16 Large", /* 146 */ 50810021SSheshadri.Vasudevan@Sun.COM "Hidden Linux native, Amoeba", /* 147 */ 50910021SSheshadri.Vasudevan@Sun.COM "Amoeba Bad Block Table", /* 148 */ 51010021SSheshadri.Vasudevan@Sun.COM "MIT EXOPC Native", /* 149 */ 51110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 150 */ 51210021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden PDOS FAT32", /* 151 */ 51310021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden FAT32 LBA", /* 152 */ 51410021SSheshadri.Vasudevan@Sun.COM "DCE376 logical drive", /* 153 */ 51510021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden FAT16 LBA", /* 154 */ 51610021SSheshadri.Vasudevan@Sun.COM "Free FDISK hidden DOS EXT", /* 155 */ 51710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 156 */ 51810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 157 */ 51910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 158 */ 52010021SSheshadri.Vasudevan@Sun.COM "BSD/OS", /* 159 */ 52110021SSheshadri.Vasudevan@Sun.COM "Laptop hibernation", /* 160 */ 52210021SSheshadri.Vasudevan@Sun.COM "Laptop hibernate,HP SpeedStor", /* 161 */ 52310021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 162 */ 52410021SSheshadri.Vasudevan@Sun.COM "HP SpeedStor", /* 163 */ 52510021SSheshadri.Vasudevan@Sun.COM "HP SpeedStor", /* 164 */ 52610021SSheshadri.Vasudevan@Sun.COM "BSD/386,386BSD,NetBSD,FreeBSD", /* 165 */ 52710021SSheshadri.Vasudevan@Sun.COM "OpenBSD,HP SpeedStor", /* 166 */ 52810021SSheshadri.Vasudevan@Sun.COM "NeXTStep", /* 167 */ 52910021SSheshadri.Vasudevan@Sun.COM "Mac OS-X", /* 168 */ 53010021SSheshadri.Vasudevan@Sun.COM "NetBSD", /* 169 */ 53110021SSheshadri.Vasudevan@Sun.COM "Olivetti FAT12 1.44MB Service", /* 170 */ 53210021SSheshadri.Vasudevan@Sun.COM "Mac OS-X Boot", /* 171 */ 53310021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 172 */ 53410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 173 */ 53510021SSheshadri.Vasudevan@Sun.COM "ShagOS filesystem", /* 174 */ 53610021SSheshadri.Vasudevan@Sun.COM "ShagOS swap", /* 175 */ 53710021SSheshadri.Vasudevan@Sun.COM "BootStar Dummy", /* 176 */ 53810021SSheshadri.Vasudevan@Sun.COM "HP SpeedStor", /* 177 */ 53910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 178 */ 54010021SSheshadri.Vasudevan@Sun.COM "HP SpeedStor", /* 179 */ 54110021SSheshadri.Vasudevan@Sun.COM "HP SpeedStor", /* 180 */ 54210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 181 */ 54310021SSheshadri.Vasudevan@Sun.COM "Corrupted FAT16 NT Mirror Set", /* 182 */ 54410021SSheshadri.Vasudevan@Sun.COM "Corrupted NTFS NT Mirror Set", /* 183 */ 54510021SSheshadri.Vasudevan@Sun.COM "Old BSDI BSD/386 swap", /* 184 */ 54610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 185 */ 54710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 186 */ 54810021SSheshadri.Vasudevan@Sun.COM "Boot Wizard hidden", /* 187 */ 54910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 188 */ 55010021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 189 */ 55110021SSheshadri.Vasudevan@Sun.COM "Solaris x86 boot", /* 190 */ 55210021SSheshadri.Vasudevan@Sun.COM "Solaris2", /* 191 */ 55310021SSheshadri.Vasudevan@Sun.COM "REAL/32 or Novell DOS secured", /* 192 */ 55410021SSheshadri.Vasudevan@Sun.COM "DRDOS/secured(FAT12)", /* 193 */ 55510021SSheshadri.Vasudevan@Sun.COM "Hidden Linux", /* 194 */ 55610021SSheshadri.Vasudevan@Sun.COM "Hidden Linux swap", /* 195 */ 55710021SSheshadri.Vasudevan@Sun.COM "DRDOS/secured(FAT16,< 32M)", /* 196 */ 55810021SSheshadri.Vasudevan@Sun.COM "DRDOS/secured(Extended)", /* 197 */ 55910021SSheshadri.Vasudevan@Sun.COM "NT corrupted FAT16 volume", /* 198 */ 56010021SSheshadri.Vasudevan@Sun.COM "NT corrupted NTFS volume", /* 199 */ 56110021SSheshadri.Vasudevan@Sun.COM "DRDOS8.0+", /* 200 */ 56210021SSheshadri.Vasudevan@Sun.COM "DRDOS8.0+", /* 201 */ 56310021SSheshadri.Vasudevan@Sun.COM "DRDOS8.0+", /* 202 */ 56410021SSheshadri.Vasudevan@Sun.COM "DRDOS7.04+ secured FAT32(CHS)", /* 203 */ 56510021SSheshadri.Vasudevan@Sun.COM "DRDOS7.04+ secured FAT32(LBA)", /* 204 */ 56610021SSheshadri.Vasudevan@Sun.COM "CTOS Memdump", /* 205 */ 56710021SSheshadri.Vasudevan@Sun.COM "DRDOS7.04+ FAT16X(LBA)", /* 206 */ 56810021SSheshadri.Vasudevan@Sun.COM "DRDOS7.04+ secure EXT DOS(LBA)", /* 207 */ 56910021SSheshadri.Vasudevan@Sun.COM "REAL/32 secure big, MDOS", /* 208 */ 57010021SSheshadri.Vasudevan@Sun.COM "Old MDOS secure FAT12", /* 209 */ 57110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 210 */ 57210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 211 */ 57310021SSheshadri.Vasudevan@Sun.COM "Old MDOS secure FAT16 <32M", /* 212 */ 57410021SSheshadri.Vasudevan@Sun.COM "Old MDOS secure EXT", /* 213 */ 57510021SSheshadri.Vasudevan@Sun.COM "Old MDOS secure FAT16 >=32M", /* 214 */ 57610021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 215 */ 57710021SSheshadri.Vasudevan@Sun.COM "CP/M-86", /* 216 */ 57810021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 217 */ 57910021SSheshadri.Vasudevan@Sun.COM "Non-FS Data", /* 218 */ 58010021SSheshadri.Vasudevan@Sun.COM "CP/M,Concurrent DOS,CTOS", /* 219 */ 58110021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 220 */ 58210021SSheshadri.Vasudevan@Sun.COM "Hidden CTOS memdump", /* 221 */ 58310021SSheshadri.Vasudevan@Sun.COM "Dell PowerEdge utilities(FAT)", /* 222 */ 58410021SSheshadri.Vasudevan@Sun.COM "DG/UX virtual disk manager", /* 223 */ 58510021SSheshadri.Vasudevan@Sun.COM "ST AVFS(STMicroelectronics)", /* 224 */ 58610021SSheshadri.Vasudevan@Sun.COM "SpeedStor 12-bit FAT EXT", /* 225 */ 58710021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 226 */ 58810021SSheshadri.Vasudevan@Sun.COM "SpeedStor", /* 227 */ 58910021SSheshadri.Vasudevan@Sun.COM "SpeedStor 16-bit FAT EXT", /* 228 */ 59010021SSheshadri.Vasudevan@Sun.COM "Tandy MSDOS", /* 229 */ 59110021SSheshadri.Vasudevan@Sun.COM "Storage Dimensions SpeedStor", /* 230 */ 59210021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 231 */ 59310021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 232 */ 59410021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 233 */ 59510021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 234 */ 59610021SSheshadri.Vasudevan@Sun.COM "BeOS BFS", /* 235 */ 59710021SSheshadri.Vasudevan@Sun.COM "SkyOS SkyFS", /* 236 */ 59810021SSheshadri.Vasudevan@Sun.COM "Unused", /* 237 */ 59910021SSheshadri.Vasudevan@Sun.COM "EFI Header Indicator", /* 238 */ 60010021SSheshadri.Vasudevan@Sun.COM "EFI Filesystem", /* 239 */ 60110021SSheshadri.Vasudevan@Sun.COM "Linux/PA-RISC boot loader", /* 240 */ 60210021SSheshadri.Vasudevan@Sun.COM "SpeedStor", /* 241 */ 60310021SSheshadri.Vasudevan@Sun.COM "DOS 3.3+ secondary", /* 242 */ 60410021SSheshadri.Vasudevan@Sun.COM "SpeedStor Reserved", /* 243 */ 60510021SSheshadri.Vasudevan@Sun.COM "SpeedStor Large", /* 244 */ 60610021SSheshadri.Vasudevan@Sun.COM "Prologue multi-volume", /* 245 */ 60710021SSheshadri.Vasudevan@Sun.COM "SpeedStor", /* 246 */ 60810021SSheshadri.Vasudevan@Sun.COM "Unused", /* 247 */ 60910021SSheshadri.Vasudevan@Sun.COM "Unknown", /* 248 */ 61010021SSheshadri.Vasudevan@Sun.COM "pCache", /* 249 */ 61110021SSheshadri.Vasudevan@Sun.COM "Bochs", /* 250 */ 61210021SSheshadri.Vasudevan@Sun.COM "VMware File System", /* 251 */ 61310021SSheshadri.Vasudevan@Sun.COM "VMware swap", /* 252 */ 61410021SSheshadri.Vasudevan@Sun.COM "Linux raid autodetect", /* 253 */ 61510021SSheshadri.Vasudevan@Sun.COM "NT Disk Administrator hidden", /* 254 */ 61610021SSheshadri.Vasudevan@Sun.COM "Xenix Bad Block Table" /* 255 */ 61710021SSheshadri.Vasudevan@Sun.COM }; 61810021SSheshadri.Vasudevan@Sun.COM 61910021SSheshadri.Vasudevan@Sun.COM /* Allowed extended partition menu options */ 62010021SSheshadri.Vasudevan@Sun.COM static char ext_part_menu_opts[] = "adhipr"; 62110021SSheshadri.Vasudevan@Sun.COM 62210021SSheshadri.Vasudevan@Sun.COM /* 62310021SSheshadri.Vasudevan@Sun.COM * Structure holding all information about the extended partition 62410021SSheshadri.Vasudevan@Sun.COM * NOTE : As of now, there will be just one instance of ext_part_t, since most 62510021SSheshadri.Vasudevan@Sun.COM * known systems allow only one extended dos partition per disk. 62610021SSheshadri.Vasudevan@Sun.COM */ 62710021SSheshadri.Vasudevan@Sun.COM static ext_part_t *epp; 62810021SSheshadri.Vasudevan@Sun.COM #endif 62910021SSheshadri.Vasudevan@Sun.COM 630251Slclee static void update_disk_and_exit(boolean_t table_changed); 631251Slclee int main(int argc, char *argv[]); 632251Slclee static int read_geom(char *sgeom); 633251Slclee static void dev_mboot_read(void); 6346549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz); 635251Slclee static void mboot_read(void); 636251Slclee static void fill_patt(void); 637251Slclee static void abs_read(void); 638251Slclee static void abs_write(void); 639251Slclee static void load(int funct, char *file); 640251Slclee static void Set_Table_CHS_Values(int ti); 641*11382SShidokht.Yadegari@Sun.COM static int nopartdefined(); 642251Slclee static int insert_tbl(int id, int act, 643251Slclee int bhead, int bsect, int bcyl, 644251Slclee int ehead, int esect, int ecyl, 645*11382SShidokht.Yadegari@Sun.COM uint32_t rsect, uint32_t numsect, int startindex); 6468904SBarry.Harding@Sun.COM static int entry_from_old_table(int id, int act, 6478904SBarry.Harding@Sun.COM int bhead, int bsect, int bcyl, 6488904SBarry.Harding@Sun.COM int ehead, int esect, int ecyl, 649*11382SShidokht.Yadegari@Sun.COM uint32_t rsect, uint32_t numsect, int startindex); 650251Slclee static int verify_tbl(void); 651251Slclee static int pars_fdisk(char *line, 652251Slclee int *id, int *act, 653251Slclee int *bhead, int *bsect, int *bcyl, 654251Slclee int *ehead, int *esect, int *ecyl, 6557563SPrasad.Singamsetty@Sun.COM uint32_t *rsect, uint32_t *numsect); 6567563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect); 657251Slclee static void stage0(void); 658251Slclee static int pcreate(void); 659251Slclee static int specify(uchar_t tsystid); 660251Slclee static void dispmenu(void); 661251Slclee static int pchange(void); 662251Slclee static int ppartid(void); 663251Slclee static char pdelete(void); 664251Slclee static void rm_blanks(char *s); 665251Slclee static int getcyl(void); 666251Slclee static void disptbl(void); 667251Slclee static void print_Table(void); 668251Slclee static void copy_Table_to_Old_Table(void); 669251Slclee static void nulltbl(void); 670251Slclee static void copy_Bootblk_to_Table(void); 671251Slclee static void fill_ipart(char *bootptr, struct ipart *partp); 672251Slclee #ifdef sparc 673251Slclee uchar_t getbyte(char **bp); 674251Slclee uint32_t getlong(char **bp); 675251Slclee #endif 676251Slclee static void copy_Table_to_Bootblk(void); 677251Slclee static int TableChanged(void); 678251Slclee static void ffile_write(char *file); 679251Slclee static void fix_slice(void); 680251Slclee static int yesno(void); 681251Slclee static int readvtoc(void); 682251Slclee static int writevtoc(void); 683251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 684251Slclee static int clear_efi(void); 685251Slclee static void clear_vtoc(int table, int part); 686251Slclee static int lecture_and_query(char *warning, char *devname); 687251Slclee static void sanity_check_provided_device(char *devname, int fd); 688251Slclee static char *get_node(char *devname); 6890Sstevel@tonic-gate 69010021SSheshadri.Vasudevan@Sun.COM #ifdef i386 69110021SSheshadri.Vasudevan@Sun.COM static void id_to_name(uchar_t sysid, char *buffer); 69210021SSheshadri.Vasudevan@Sun.COM static void ext_read_input(char *buf); 69310021SSheshadri.Vasudevan@Sun.COM static int ext_read_options(char *buf); 69410021SSheshadri.Vasudevan@Sun.COM static int ext_invalid_option(char ch); 69510021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_num(int *pno); 69610021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_part_id(uchar_t *partid); 69710021SSheshadri.Vasudevan@Sun.COM static int ext_read_valid_partition_start(uint32_t *begsec); 69810021SSheshadri.Vasudevan@Sun.COM static void ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec); 69910021SSheshadri.Vasudevan@Sun.COM static void ext_part_menu(); 70010021SSheshadri.Vasudevan@Sun.COM static void add_logical_drive(); 70110021SSheshadri.Vasudevan@Sun.COM static void delete_logical_drive(); 70210021SSheshadri.Vasudevan@Sun.COM static void ext_print_help_menu(); 70310021SSheshadri.Vasudevan@Sun.COM static void ext_change_logical_drive_id(); 70410021SSheshadri.Vasudevan@Sun.COM static void ext_print_part_types(); 70510021SSheshadri.Vasudevan@Sun.COM static void ext_print_logical_drive_layout(); 70610021SSheshadri.Vasudevan@Sun.COM static void preach_and_continue(); 70710021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG 70810021SSheshadri.Vasudevan@Sun.COM static void ext_print_logdrive_layout_debug(); 70910021SSheshadri.Vasudevan@Sun.COM #endif /* DEBUG */ 71010021SSheshadri.Vasudevan@Sun.COM #endif /* i386 */ 71110021SSheshadri.Vasudevan@Sun.COM 71210021SSheshadri.Vasudevan@Sun.COM /* 71310021SSheshadri.Vasudevan@Sun.COM * This function is called only during the non-interactive mode. 71410021SSheshadri.Vasudevan@Sun.COM * It is touchy and does not tolerate any errors. If there are 71510021SSheshadri.Vasudevan@Sun.COM * mounted logical drives, changes to the partition table 71610021SSheshadri.Vasudevan@Sun.COM * is disallowed. 71710021SSheshadri.Vasudevan@Sun.COM */ 7180Sstevel@tonic-gate static void 7190Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed) 7200Sstevel@tonic-gate { 72110021SSheshadri.Vasudevan@Sun.COM #ifdef i386 72210021SSheshadri.Vasudevan@Sun.COM int rval; 72310021SSheshadri.Vasudevan@Sun.COM #endif 7240Sstevel@tonic-gate if (table_changed) { 7250Sstevel@tonic-gate /* 7260Sstevel@tonic-gate * Copy the new table back to the sector buffer 7270Sstevel@tonic-gate * and write it to disk 7280Sstevel@tonic-gate */ 7290Sstevel@tonic-gate copy_Table_to_Bootblk(); 7300Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 7310Sstevel@tonic-gate } 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* If the VTOC table is wrong fix it (truncation only) */ 7340Sstevel@tonic-gate if (io_adjt) 7350Sstevel@tonic-gate fix_slice(); 7360Sstevel@tonic-gate 73710021SSheshadri.Vasudevan@Sun.COM #ifdef i386 73810021SSheshadri.Vasudevan@Sun.COM if (!io_readonly) { 73910021SSheshadri.Vasudevan@Sun.COM rval = fdisk_commit_ext_part(epp); 74010021SSheshadri.Vasudevan@Sun.COM switch (rval) { 74110021SSheshadri.Vasudevan@Sun.COM case FDISK_SUCCESS: 74210021SSheshadri.Vasudevan@Sun.COM /* Success */ 74310021SSheshadri.Vasudevan@Sun.COM break; 74410021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOEXTPART: 74510021SSheshadri.Vasudevan@Sun.COM /* Nothing to do */ 74610021SSheshadri.Vasudevan@Sun.COM break; 74710021SSheshadri.Vasudevan@Sun.COM default: 74810021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Error in" 74910021SSheshadri.Vasudevan@Sun.COM " fdisk_commit_ext_part\n"); 75010021SSheshadri.Vasudevan@Sun.COM exit(rval); 75110021SSheshadri.Vasudevan@Sun.COM } 75210021SSheshadri.Vasudevan@Sun.COM } 75310021SSheshadri.Vasudevan@Sun.COM libfdisk_fini(&epp); 75410021SSheshadri.Vasudevan@Sun.COM #endif 7550Sstevel@tonic-gate exit(0); 7560Sstevel@tonic-gate } 7570Sstevel@tonic-gate 7580Sstevel@tonic-gate /* 7590Sstevel@tonic-gate * main 7600Sstevel@tonic-gate * Process command-line options. 7610Sstevel@tonic-gate */ 762251Slclee int 7630Sstevel@tonic-gate main(int argc, char *argv[]) 7640Sstevel@tonic-gate { 765251Slclee int c, i; 7660Sstevel@tonic-gate extern int optind; 7670Sstevel@tonic-gate extern char *optarg; 7680Sstevel@tonic-gate int errflg = 0; 7690Sstevel@tonic-gate int diag_cnt = 0; 7700Sstevel@tonic-gate int openmode; 77110021SSheshadri.Vasudevan@Sun.COM #ifdef i386 77210021SSheshadri.Vasudevan@Sun.COM int rval; 77310021SSheshadri.Vasudevan@Sun.COM int lf_op_flag = 0; 77410021SSheshadri.Vasudevan@Sun.COM #endif 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate setbuf(stderr, 0); /* so all output gets out on exit */ 7770Sstevel@tonic-gate setbuf(stdout, 0); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* Process the options. */ 7800Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE")) 7810Sstevel@tonic-gate != EOF) { 7820Sstevel@tonic-gate switch (c) { 7830Sstevel@tonic-gate 7840Sstevel@tonic-gate case 'o': 7856549Sbharding io_offset = (off_t)strtoull(optarg, 0, 0); 7860Sstevel@tonic-gate continue; 7870Sstevel@tonic-gate case 's': 7886549Sbharding io_size = (off_t)strtoull(optarg, 0, 0); 7890Sstevel@tonic-gate continue; 7900Sstevel@tonic-gate case 'P': 7910Sstevel@tonic-gate diag_cnt++; 7920Sstevel@tonic-gate io_patt++; 7930Sstevel@tonic-gate io_fatt = optarg; 7940Sstevel@tonic-gate continue; 7950Sstevel@tonic-gate case 'w': 7960Sstevel@tonic-gate diag_cnt++; 7970Sstevel@tonic-gate io_wrt++; 7980Sstevel@tonic-gate continue; 7990Sstevel@tonic-gate case 'r': 8000Sstevel@tonic-gate diag_cnt++; 8010Sstevel@tonic-gate io_rd++; 8020Sstevel@tonic-gate continue; 8030Sstevel@tonic-gate case 'd': 8040Sstevel@tonic-gate io_debug++; 8050Sstevel@tonic-gate continue; 8060Sstevel@tonic-gate case 'I': 8070Sstevel@tonic-gate io_image++; 8080Sstevel@tonic-gate continue; 8090Sstevel@tonic-gate case 'R': 8100Sstevel@tonic-gate io_readonly++; 8110Sstevel@tonic-gate continue; 8120Sstevel@tonic-gate case 'S': 8130Sstevel@tonic-gate diag_cnt++; 8140Sstevel@tonic-gate io_sgeom = optarg; 8150Sstevel@tonic-gate continue; 8160Sstevel@tonic-gate case 'T': 8170Sstevel@tonic-gate io_ADJT++; 818251Slclee /* FALLTHRU */ 8190Sstevel@tonic-gate case 't': 8200Sstevel@tonic-gate io_adjt++; 8210Sstevel@tonic-gate continue; 8220Sstevel@tonic-gate case 'B': 8230Sstevel@tonic-gate io_wholedisk++; 8240Sstevel@tonic-gate io_fdisk++; 8250Sstevel@tonic-gate continue; 8260Sstevel@tonic-gate case 'E': 8270Sstevel@tonic-gate io_EFIdisk++; 8280Sstevel@tonic-gate io_fdisk++; 8290Sstevel@tonic-gate continue; 8300Sstevel@tonic-gate case 'g': 8310Sstevel@tonic-gate diag_cnt++; 8320Sstevel@tonic-gate io_lgeom++; 8330Sstevel@tonic-gate continue; 8340Sstevel@tonic-gate case 'G': 8350Sstevel@tonic-gate diag_cnt++; 8360Sstevel@tonic-gate io_pgeom++; 8370Sstevel@tonic-gate continue; 8380Sstevel@tonic-gate case 'n': 8390Sstevel@tonic-gate io_nifdisk++; 8400Sstevel@tonic-gate io_fdisk++; 8410Sstevel@tonic-gate continue; 8420Sstevel@tonic-gate case 'F': 8430Sstevel@tonic-gate io_fdisk++; 8440Sstevel@tonic-gate io_ffdisk = optarg; 8450Sstevel@tonic-gate continue; 8460Sstevel@tonic-gate case 'b': 8470Sstevel@tonic-gate io_mboot = optarg; 8480Sstevel@tonic-gate continue; 8490Sstevel@tonic-gate case 'W': 8500Sstevel@tonic-gate /* 8510Sstevel@tonic-gate * If '-' is the -W argument, then write 8520Sstevel@tonic-gate * to standard output, otherwise write 8530Sstevel@tonic-gate * to the specified file. 8540Sstevel@tonic-gate */ 8550Sstevel@tonic-gate if (strncmp(optarg, "-", 1) == 0) 8560Sstevel@tonic-gate stdo_flag = 1; 8570Sstevel@tonic-gate else 8580Sstevel@tonic-gate io_Wfdisk = optarg; 8590Sstevel@tonic-gate io_fdisk++; 8600Sstevel@tonic-gate continue; 8610Sstevel@tonic-gate case 'A': 8620Sstevel@tonic-gate io_fdisk++; 8630Sstevel@tonic-gate io_Afdisk = optarg; 8640Sstevel@tonic-gate continue; 8650Sstevel@tonic-gate case 'D': 8660Sstevel@tonic-gate io_fdisk++; 8670Sstevel@tonic-gate io_Dfdisk = optarg; 8680Sstevel@tonic-gate continue; 8690Sstevel@tonic-gate case 'h': 870251Slclee (void) fprintf(stderr, "%s\n", Usage); 871251Slclee (void) fprintf(stderr, "%s\n", Usage1); 8720Sstevel@tonic-gate exit(0); 873251Slclee /* FALLTHRU */ 8740Sstevel@tonic-gate case 'v': 8750Sstevel@tonic-gate v_flag = 1; 8760Sstevel@tonic-gate continue; 8770Sstevel@tonic-gate case '?': 8780Sstevel@tonic-gate errflg++; 8790Sstevel@tonic-gate break; 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate break; 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate if (io_image && io_sgeom && diag_cnt == 1) { 8850Sstevel@tonic-gate diag_cnt = 0; 8860Sstevel@tonic-gate } 8870Sstevel@tonic-gate 8880Sstevel@tonic-gate /* User option checking */ 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate /* By default, run in interactive mode */ 8910Sstevel@tonic-gate if (!io_fdisk && !diag_cnt && !io_nifdisk) { 8920Sstevel@tonic-gate io_ifdisk++; 8930Sstevel@tonic-gate io_fdisk++; 8940Sstevel@tonic-gate } 8950Sstevel@tonic-gate if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) { 8960Sstevel@tonic-gate errflg++; 8970Sstevel@tonic-gate } 8980Sstevel@tonic-gate 8990Sstevel@tonic-gate /* Was any error detected? */ 9000Sstevel@tonic-gate if (errflg || argc == optind) { 901251Slclee (void) fprintf(stderr, "%s\n", Usage); 902251Slclee (void) fprintf(stderr, 9030Sstevel@tonic-gate "\nDetailed help is available with the -h option.\n"); 9040Sstevel@tonic-gate exit(2); 9050Sstevel@tonic-gate } 9060Sstevel@tonic-gate 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* Figure out the correct device node to open */ 9090Sstevel@tonic-gate Dfltdev = get_node(argv[optind]); 9100Sstevel@tonic-gate 9110Sstevel@tonic-gate if (io_readonly) 9120Sstevel@tonic-gate openmode = O_RDONLY; 9130Sstevel@tonic-gate else 9140Sstevel@tonic-gate openmode = O_RDWR|O_CREAT; 9150Sstevel@tonic-gate 9160Sstevel@tonic-gate if ((Dev = open(Dfltdev, openmode, 0666)) == -1) { 917251Slclee (void) fprintf(stderr, 918251Slclee "fdisk: Cannot open device %s.\n", 919251Slclee Dfltdev); 9200Sstevel@tonic-gate exit(1); 9210Sstevel@tonic-gate } 9225169Slclee /* 9235169Slclee * not all disk (or disklike) drivers support DKIOCGMEDIAINFO 9245169Slclee * in that case leave the minfo structure zeroed 9255169Slclee */ 9265169Slclee if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) { 9279889SLarry.Liu@Sun.COM (void) memset(&minfo, 0, sizeof (minfo)); 9285169Slclee } 9290Sstevel@tonic-gate 9300Sstevel@tonic-gate /* Get the disk geometry */ 9310Sstevel@tonic-gate if (!io_image) { 9320Sstevel@tonic-gate /* Get disk's HBA (virtual) geometry */ 9330Sstevel@tonic-gate errno = 0; 9340Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) { 9350Sstevel@tonic-gate 9360Sstevel@tonic-gate /* 9370Sstevel@tonic-gate * If ioctl isn't implemented on this platform, then 9380Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v), 9390Sstevel@tonic-gate * otherwise use the virtual geometry. 9400Sstevel@tonic-gate */ 9410Sstevel@tonic-gate 9420Sstevel@tonic-gate if (errno == ENOTTY) { 9430Sstevel@tonic-gate v_flag = 0; 9440Sstevel@tonic-gate no_virtgeom_ioctl = 1; 9450Sstevel@tonic-gate } else if (errno == EINVAL) { 9460Sstevel@tonic-gate /* 9470Sstevel@tonic-gate * This means that the ioctl exists, but 9480Sstevel@tonic-gate * is invalid for this disk, meaning the 9490Sstevel@tonic-gate * disk doesn't have an HBA geometry 9500Sstevel@tonic-gate * (like, say, it's larger than 8GB). 9510Sstevel@tonic-gate */ 9520Sstevel@tonic-gate v_flag = 0; 9530Sstevel@tonic-gate hba_Numcyl = hba_heads = hba_sectors = 0; 9540Sstevel@tonic-gate } else { 9550Sstevel@tonic-gate (void) fprintf(stderr, 9560Sstevel@tonic-gate "%s: Cannot get virtual disk geometry.\n", 9570Sstevel@tonic-gate argv[optind]); 9580Sstevel@tonic-gate exit(1); 9590Sstevel@tonic-gate } 9600Sstevel@tonic-gate } else { 9610Sstevel@tonic-gate /* save virtual geometry values obtained by ioctl */ 9620Sstevel@tonic-gate hba_Numcyl = disk_geom.dkg_ncyl; 9630Sstevel@tonic-gate hba_heads = disk_geom.dkg_nhead; 9640Sstevel@tonic-gate hba_sectors = disk_geom.dkg_nsect; 9650Sstevel@tonic-gate } 9660Sstevel@tonic-gate 9670Sstevel@tonic-gate errno = 0; 9680Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 9690Sstevel@tonic-gate if (errno == ENOTTY) { 9700Sstevel@tonic-gate no_physgeom_ioctl = 1; 9710Sstevel@tonic-gate } else { 9720Sstevel@tonic-gate (void) fprintf(stderr, 9730Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 9740Sstevel@tonic-gate argv[optind]); 9750Sstevel@tonic-gate exit(1); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * Call DKIOCGGEOM if the ioctls for physical and virtual 9810Sstevel@tonic-gate * geometry fail. Get both from this generic call. 9820Sstevel@tonic-gate */ 9830Sstevel@tonic-gate if (no_virtgeom_ioctl && no_physgeom_ioctl) { 9840Sstevel@tonic-gate errno = 0; 9850Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 9860Sstevel@tonic-gate (void) fprintf(stderr, 9870Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 9880Sstevel@tonic-gate argv[optind]); 9890Sstevel@tonic-gate exit(1); 9900Sstevel@tonic-gate } 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 9940Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 9950Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 9969889SLarry.Liu@Sun.COM 9979889SLarry.Liu@Sun.COM if (minfo.dki_lbsize != 0) 9989889SLarry.Liu@Sun.COM sectsiz = minfo.dki_lbsize; 9999889SLarry.Liu@Sun.COM else 10009889SLarry.Liu@Sun.COM sectsiz = 512; 10019889SLarry.Liu@Sun.COM 10020Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 10030Sstevel@tonic-gate 10040Sstevel@tonic-gate /* 10050Sstevel@tonic-gate * if hba geometry was not set by DKIOC_VIRTGEOM 10060Sstevel@tonic-gate * or we got an invalid hba geometry 10070Sstevel@tonic-gate * then set hba geometry based on max values 10080Sstevel@tonic-gate */ 10090Sstevel@tonic-gate if (no_virtgeom_ioctl || 1010251Slclee disk_geom.dkg_ncyl == 0 || 1011251Slclee disk_geom.dkg_nhead == 0 || 1012251Slclee disk_geom.dkg_nsect == 0 || 10130Sstevel@tonic-gate disk_geom.dkg_ncyl > MAX_CYL || 10140Sstevel@tonic-gate disk_geom.dkg_nhead > MAX_HEAD || 10150Sstevel@tonic-gate disk_geom.dkg_nsect > MAX_SECT) { 10160Sstevel@tonic-gate 10170Sstevel@tonic-gate /* 10180Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v) 10190Sstevel@tonic-gate */ 10200Sstevel@tonic-gate v_flag = 0; 10210Sstevel@tonic-gate hba_sectors = MAX_SECT; 10220Sstevel@tonic-gate hba_heads = MAX_HEAD + 1; 10230Sstevel@tonic-gate hba_Numcyl = (Numcyl * heads * sectors) / 10240Sstevel@tonic-gate (hba_sectors * hba_heads); 10250Sstevel@tonic-gate } 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate if (io_debug) { 1028251Slclee (void) fprintf(stderr, "Physical Geometry:\n"); 1029251Slclee (void) fprintf(stderr, 10300Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 10310Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 10320Sstevel@tonic-gate Numcyl, 10330Sstevel@tonic-gate heads, 10340Sstevel@tonic-gate sectors, 10350Sstevel@tonic-gate sectsiz, 1036251Slclee Numcyl * heads * sectors, 1037251Slclee (Numcyl * heads * sectors * sectsiz) / 1048576); 1038251Slclee (void) fprintf(stderr, "Virtual (HBA) Geometry:\n"); 1039251Slclee (void) fprintf(stderr, 10400Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 10410Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 10420Sstevel@tonic-gate hba_Numcyl, 10430Sstevel@tonic-gate hba_heads, 10440Sstevel@tonic-gate hba_sectors, 10450Sstevel@tonic-gate sectsiz, 1046251Slclee hba_Numcyl * hba_heads * hba_sectors, 1047251Slclee (hba_Numcyl * hba_heads * hba_sectors * sectsiz) / 1048251Slclee 1048576); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate } 10510Sstevel@tonic-gate 10520Sstevel@tonic-gate /* If user has requested a geometry report just do it and exit */ 10530Sstevel@tonic-gate if (io_lgeom) { 10540Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 10550Sstevel@tonic-gate (void) fprintf(stderr, 10560Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 10570Sstevel@tonic-gate argv[optind]); 10580Sstevel@tonic-gate exit(1); 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 10610Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 10620Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 10639889SLarry.Liu@Sun.COM if (minfo.dki_lbsize != 0) 10649889SLarry.Liu@Sun.COM sectsiz = minfo.dki_lbsize; 10659889SLarry.Liu@Sun.COM else 10669889SLarry.Liu@Sun.COM sectsiz = 512; 10679889SLarry.Liu@Sun.COM 10680Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 1069251Slclee (void) printf("* Label geometry for device %s\n", Dfltdev); 1070251Slclee (void) printf( 1071251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 10720Sstevel@tonic-gate " SECSIZ\n"); 1073251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 10740Sstevel@tonic-gate Numcyl, 10750Sstevel@tonic-gate disk_geom.dkg_ncyl, 10760Sstevel@tonic-gate disk_geom.dkg_acyl, 10770Sstevel@tonic-gate disk_geom.dkg_bcyl, 10780Sstevel@tonic-gate heads, 10790Sstevel@tonic-gate sectors, 10800Sstevel@tonic-gate sectsiz); 10810Sstevel@tonic-gate exit(0); 10820Sstevel@tonic-gate } else if (io_pgeom) { 10830Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 10840Sstevel@tonic-gate (void) fprintf(stderr, 10850Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 10860Sstevel@tonic-gate argv[optind]); 10870Sstevel@tonic-gate exit(1); 10880Sstevel@tonic-gate } 1089251Slclee (void) printf("* Physical geometry for device %s\n", Dfltdev); 1090251Slclee (void) printf( 1091251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 10920Sstevel@tonic-gate " SECSIZ\n"); 1093251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 10940Sstevel@tonic-gate disk_geom.dkg_pcyl, 10950Sstevel@tonic-gate disk_geom.dkg_ncyl, 10960Sstevel@tonic-gate disk_geom.dkg_acyl, 10970Sstevel@tonic-gate disk_geom.dkg_bcyl, 10980Sstevel@tonic-gate disk_geom.dkg_nhead, 10990Sstevel@tonic-gate disk_geom.dkg_nsect, 11000Sstevel@tonic-gate sectsiz); 11010Sstevel@tonic-gate exit(0); 11020Sstevel@tonic-gate } else if (io_sgeom) { 11030Sstevel@tonic-gate if (read_geom(io_sgeom)) { 11040Sstevel@tonic-gate exit(1); 11050Sstevel@tonic-gate } else if (!io_image) { 11060Sstevel@tonic-gate exit(0); 11070Sstevel@tonic-gate } 11080Sstevel@tonic-gate } 11090Sstevel@tonic-gate 11105169Slclee /* 11115169Slclee * some drivers may not support DKIOCGMEDIAINFO 11125169Slclee * in that case use CHS 11135169Slclee */ 11147563SPrasad.Singamsetty@Sun.COM chs_capacity = (diskaddr_t)Numcyl * heads * sectors; 11155169Slclee dev_capacity = chs_capacity; 11167563SPrasad.Singamsetty@Sun.COM Numcyl_usable = Numcyl; 11177563SPrasad.Singamsetty@Sun.COM 11187563SPrasad.Singamsetty@Sun.COM if (chs_capacity > DK_MAX_2TB) { 11197563SPrasad.Singamsetty@Sun.COM /* limit to 2TB */ 11207563SPrasad.Singamsetty@Sun.COM Numcyl_usable = DK_MAX_2TB / (heads * sectors); 11217563SPrasad.Singamsetty@Sun.COM chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors; 11227563SPrasad.Singamsetty@Sun.COM } 11237563SPrasad.Singamsetty@Sun.COM 11245169Slclee if (minfo.dki_capacity > 0) 11255169Slclee dev_capacity = minfo.dki_capacity; 11265169Slclee 11270Sstevel@tonic-gate /* Allocate memory to hold three complete sectors */ 11289889SLarry.Liu@Sun.COM Bootsect = (char *)calloc(3 * sectsiz, 1); 11290Sstevel@tonic-gate if (Bootsect == NULL) { 1130251Slclee (void) fprintf(stderr, 11310Sstevel@tonic-gate "fdisk: Unable to obtain enough buffer memory" 11320Sstevel@tonic-gate " (%d bytes).\n", 1133251Slclee 3 * sectsiz); 11340Sstevel@tonic-gate exit(1); 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate Nullsect = Bootsect + sectsiz; 11380Sstevel@tonic-gate /* Zero out the "NULL" sector */ 11390Sstevel@tonic-gate for (i = 0; i < sectsiz; i++) { 11400Sstevel@tonic-gate Nullsect[i] = 0; 11410Sstevel@tonic-gate } 11420Sstevel@tonic-gate 11430Sstevel@tonic-gate /* Find out what the user wants done */ 11440Sstevel@tonic-gate if (io_rd) { /* abs disk read */ 11450Sstevel@tonic-gate abs_read(); /* will not return */ 11460Sstevel@tonic-gate } else if (io_wrt && !io_readonly) { 11470Sstevel@tonic-gate abs_write(); /* will not return */ 11480Sstevel@tonic-gate } else if (io_patt && !io_readonly) { 11490Sstevel@tonic-gate fill_patt(); /* will not return */ 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate 11530Sstevel@tonic-gate /* This is the fdisk edit, the real reason for the program. */ 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate sanity_check_provided_device(Dfltdev, Dev); 11560Sstevel@tonic-gate 11570Sstevel@tonic-gate /* Get the new BOOT program in case we write a new fdisk table */ 11580Sstevel@tonic-gate mboot_read(); 11590Sstevel@tonic-gate 11600Sstevel@tonic-gate /* Read from disk master boot */ 11610Sstevel@tonic-gate dev_mboot_read(); 11620Sstevel@tonic-gate 11630Sstevel@tonic-gate /* 11640Sstevel@tonic-gate * Verify and copy the device's fdisk table. This will be used 11650Sstevel@tonic-gate * as the prototype mboot if the device's mboot looks invalid. 11660Sstevel@tonic-gate */ 11670Sstevel@tonic-gate Bootblk = (struct mboot *)Bootsect; 11680Sstevel@tonic-gate copy_Bootblk_to_Table(); 11690Sstevel@tonic-gate 11700Sstevel@tonic-gate /* save away a copy of Table in Old_Table for sensing changes */ 11710Sstevel@tonic-gate copy_Table_to_Old_Table(); 11720Sstevel@tonic-gate 117310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 117410021SSheshadri.Vasudevan@Sun.COM /* 117510021SSheshadri.Vasudevan@Sun.COM * Read extended partition only when the fdisk table is not 117610021SSheshadri.Vasudevan@Sun.COM * supplied from a file 117710021SSheshadri.Vasudevan@Sun.COM */ 117810021SSheshadri.Vasudevan@Sun.COM if (!io_ffdisk) { 117910021SSheshadri.Vasudevan@Sun.COM lf_op_flag |= FDISK_READ_DISK; 118010021SSheshadri.Vasudevan@Sun.COM } 118110021SSheshadri.Vasudevan@Sun.COM if ((rval = libfdisk_init(&epp, Dfltdev, &Table[0], lf_op_flag)) 118210021SSheshadri.Vasudevan@Sun.COM != FDISK_SUCCESS) { 118310021SSheshadri.Vasudevan@Sun.COM switch (rval) { 118410021SSheshadri.Vasudevan@Sun.COM /* 118511246SSharath.Srinivasan@Sun.COM * FDISK_EBADLOGDRIVE, FDISK_ENOLOGDRIVE and 118611246SSharath.Srinivasan@Sun.COM * FDISK_EBADMAGIC can be considered as 118711246SSharath.Srinivasan@Sun.COM * soft errors and hence we do not exit 118810021SSheshadri.Vasudevan@Sun.COM */ 118910021SSheshadri.Vasudevan@Sun.COM case FDISK_EBADLOGDRIVE: 119010021SSheshadri.Vasudevan@Sun.COM break; 119110021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOLOGDRIVE: 119210021SSheshadri.Vasudevan@Sun.COM break; 119311246SSharath.Srinivasan@Sun.COM case FDISK_EBADMAGIC: 119411246SSharath.Srinivasan@Sun.COM break; 119510021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOVGEOM: 119610021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Could not get virtual" 119710021SSheshadri.Vasudevan@Sun.COM " geometry for this device\n"); 119811246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 119910021SSheshadri.Vasudevan@Sun.COM exit(1); 120010021SSheshadri.Vasudevan@Sun.COM break; 120110021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOPGEOM: 120210021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Could not get physical" 120310021SSheshadri.Vasudevan@Sun.COM " geometry for this device\n"); 120411246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 120510021SSheshadri.Vasudevan@Sun.COM exit(1); 120610021SSheshadri.Vasudevan@Sun.COM break; 120710021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOLGEOM: 120810021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Could not get label" 120910021SSheshadri.Vasudevan@Sun.COM " geometry for this device\n"); 121011246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 121110021SSheshadri.Vasudevan@Sun.COM exit(1); 121210021SSheshadri.Vasudevan@Sun.COM break; 121310021SSheshadri.Vasudevan@Sun.COM default: 121410021SSheshadri.Vasudevan@Sun.COM perror("Failed to initialise libfdisk.\n"); 121511246SSharath.Srinivasan@Sun.COM libfdisk_fini(&epp); 121610021SSheshadri.Vasudevan@Sun.COM exit(1); 121710021SSheshadri.Vasudevan@Sun.COM break; 121810021SSheshadri.Vasudevan@Sun.COM } 121910021SSheshadri.Vasudevan@Sun.COM } 122010021SSheshadri.Vasudevan@Sun.COM #endif 122110021SSheshadri.Vasudevan@Sun.COM 12220Sstevel@tonic-gate /* Load fdisk table from specified file (-F fdisk_file) */ 12230Sstevel@tonic-gate if (io_ffdisk) { 12240Sstevel@tonic-gate /* Load and verify user-specified table parameters */ 12250Sstevel@tonic-gate load(LOADFILE, io_ffdisk); 12260Sstevel@tonic-gate } 12270Sstevel@tonic-gate 12280Sstevel@tonic-gate /* Does user want to delete or add an entry? */ 12290Sstevel@tonic-gate if (io_Dfdisk) { 12300Sstevel@tonic-gate load(LOADDEL, io_Dfdisk); 12310Sstevel@tonic-gate } 12320Sstevel@tonic-gate if (io_Afdisk) { 12330Sstevel@tonic-gate load(LOADADD, io_Afdisk); 12340Sstevel@tonic-gate } 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) { 12370Sstevel@tonic-gate /* Check if there is no fdisk table */ 1238*11382SShidokht.Yadegari@Sun.COM if (nopartdefined() || io_wholedisk || io_EFIdisk) { 12390Sstevel@tonic-gate if (io_ifdisk && !io_wholedisk && !io_EFIdisk) { 1240251Slclee (void) printf( 1241251Slclee "No fdisk table exists. The default" 1242251Slclee " partition for the disk is:\n\n" 1243251Slclee " a 100%% \"SOLARIS System\" " 1244251Slclee "partition\n\n" 1245251Slclee "Type \"y\" to accept the default " 12460Sstevel@tonic-gate "partition, otherwise type \"n\" to " 12470Sstevel@tonic-gate "edit the\n partition table.\n"); 12487563SPrasad.Singamsetty@Sun.COM 12497563SPrasad.Singamsetty@Sun.COM if (Numcyl > Numcyl_usable) 12507563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger" 12517563SPrasad.Singamsetty@Sun.COM " than 2TB. Solaris partition will" 12527563SPrasad.Singamsetty@Sun.COM " be limited to 2 TB.\n"); 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* Edit the partition table as directed */ 12560Sstevel@tonic-gate if (io_wholedisk ||(io_ifdisk && yesno())) { 12570Sstevel@tonic-gate 12580Sstevel@tonic-gate /* Default scenario */ 12590Sstevel@tonic-gate nulltbl(); 12600Sstevel@tonic-gate /* now set up UNIX System partition */ 12610Sstevel@tonic-gate Table[0].bootid = ACTIVE; 126210021SSheshadri.Vasudevan@Sun.COM Table[0].relsect = LE_32(heads * sectors); 12637563SPrasad.Singamsetty@Sun.COM 12647563SPrasad.Singamsetty@Sun.COM Table[0].numsect = 126510021SSheshadri.Vasudevan@Sun.COM LE_32((ulong_t)((Numcyl_usable - 1) * 12660Sstevel@tonic-gate heads * sectors)); 12677563SPrasad.Singamsetty@Sun.COM 12680Sstevel@tonic-gate Table[0].systid = SUNIXOS2; /* Solaris */ 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate /* calculate CHS values for table entry 0 */ 12710Sstevel@tonic-gate Set_Table_CHS_Values(0); 12720Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 12730Sstevel@tonic-gate } else if (io_EFIdisk) { 12740Sstevel@tonic-gate /* create an EFI partition for the whole disk */ 12750Sstevel@tonic-gate nulltbl(); 12760Sstevel@tonic-gate i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1, 12777563SPrasad.Singamsetty@Sun.COM (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB : 1278*11382SShidokht.Yadegari@Sun.COM (dev_capacity - 1), 0); 12790Sstevel@tonic-gate if (i != 0) { 1280251Slclee (void) fprintf(stderr, 1281251Slclee "Error creating EFI partition\n"); 12820Sstevel@tonic-gate exit(1); 12830Sstevel@tonic-gate } 12840Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 12850Sstevel@tonic-gate } 12860Sstevel@tonic-gate } 12870Sstevel@tonic-gate } 12880Sstevel@tonic-gate 12890Sstevel@tonic-gate /* Display complete fdisk table entries for debugging purposes */ 12900Sstevel@tonic-gate if (io_debug) { 1291251Slclee (void) fprintf(stderr, "Partition Table Entry Values:\n"); 12920Sstevel@tonic-gate print_Table(); 12930Sstevel@tonic-gate if (io_ifdisk) { 1294251Slclee (void) fprintf(stderr, "\n"); 1295251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 12969786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 12970Sstevel@tonic-gate } 12980Sstevel@tonic-gate } 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate /* Interactive fdisk mode */ 13010Sstevel@tonic-gate if (io_ifdisk) { 1302251Slclee (void) printf(CLR_SCR); 13030Sstevel@tonic-gate disptbl(); 1304251Slclee for (;;) { 1305251Slclee stage0(); 13060Sstevel@tonic-gate copy_Bootblk_to_Table(); 13070Sstevel@tonic-gate disptbl(); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* If user wants to write the table to a file, do it */ 13120Sstevel@tonic-gate if (io_Wfdisk) 13130Sstevel@tonic-gate ffile_write(io_Wfdisk); 13140Sstevel@tonic-gate else if (stdo_flag) 13150Sstevel@tonic-gate ffile_write((char *)stdout); 13160Sstevel@tonic-gate 13170Sstevel@tonic-gate update_disk_and_exit(TableChanged() == 1); 1318251Slclee return (0); 13190Sstevel@tonic-gate } 13200Sstevel@tonic-gate 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * read_geom 13230Sstevel@tonic-gate * Read geometry from specified file (-S). 13240Sstevel@tonic-gate */ 13250Sstevel@tonic-gate 1326251Slclee static int 1327251Slclee read_geom(char *sgeom) 13280Sstevel@tonic-gate { 13290Sstevel@tonic-gate char line[256]; 13300Sstevel@tonic-gate FILE *fp; 13310Sstevel@tonic-gate 13320Sstevel@tonic-gate /* open the prototype file */ 13330Sstevel@tonic-gate if ((fp = fopen(sgeom, "r")) == NULL) { 13340Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open file %s.\n", 13350Sstevel@tonic-gate io_sgeom); 13360Sstevel@tonic-gate return (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 (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 13420Sstevel@tonic-gate continue; 13430Sstevel@tonic-gate else { 13440Sstevel@tonic-gate line[strlen(line)] = '\0'; 1345251Slclee if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d", 13460Sstevel@tonic-gate &disk_geom.dkg_pcyl, 13470Sstevel@tonic-gate &disk_geom.dkg_ncyl, 13480Sstevel@tonic-gate &disk_geom.dkg_acyl, 13490Sstevel@tonic-gate &disk_geom.dkg_bcyl, 13500Sstevel@tonic-gate &disk_geom.dkg_nhead, 13510Sstevel@tonic-gate &disk_geom.dkg_nsect, 13520Sstevel@tonic-gate §siz) != 7) { 13530Sstevel@tonic-gate (void) fprintf(stderr, 13540Sstevel@tonic-gate "Syntax error:\n \"%s\".\n", 13550Sstevel@tonic-gate line); 13560Sstevel@tonic-gate return (1); 13570Sstevel@tonic-gate } 13580Sstevel@tonic-gate break; 13590Sstevel@tonic-gate } /* else */ 13600Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 13610Sstevel@tonic-gate 13620Sstevel@tonic-gate if (!io_image) { 13630Sstevel@tonic-gate if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) { 13640Sstevel@tonic-gate (void) fprintf(stderr, 13650Sstevel@tonic-gate "fdisk: Cannot set label geometry.\n"); 13660Sstevel@tonic-gate return (1); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate } else { 13690Sstevel@tonic-gate Numcyl = hba_Numcyl = disk_geom.dkg_ncyl; 13700Sstevel@tonic-gate heads = hba_heads = disk_geom.dkg_nhead; 13710Sstevel@tonic-gate sectors = hba_sectors = disk_geom.dkg_nsect; 13720Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 13730Sstevel@tonic-gate } 13740Sstevel@tonic-gate 1375251Slclee (void) fclose(fp); 13760Sstevel@tonic-gate return (0); 13770Sstevel@tonic-gate } 13780Sstevel@tonic-gate 13790Sstevel@tonic-gate /* 13800Sstevel@tonic-gate * dev_mboot_read 13810Sstevel@tonic-gate * Read the master boot sector from the device. 13820Sstevel@tonic-gate */ 1383251Slclee static void 1384251Slclee dev_mboot_read(void) 13850Sstevel@tonic-gate { 13860Sstevel@tonic-gate if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) { 13870Sstevel@tonic-gate perror("Error in ioctl DKIOCGMBOOT"); 13880Sstevel@tonic-gate } 13890Sstevel@tonic-gate if (errno == 0) 13900Sstevel@tonic-gate return; 13910Sstevel@tonic-gate if (lseek(Dev, 0, SEEK_SET) == -1) { 1392251Slclee (void) fprintf(stderr, 13930Sstevel@tonic-gate "fdisk: Error seeking to partition table on %s.\n", 13940Sstevel@tonic-gate Dfltdev); 13950Sstevel@tonic-gate if (!io_image) 13960Sstevel@tonic-gate exit(1); 13970Sstevel@tonic-gate } 13980Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1399251Slclee (void) fprintf(stderr, 14000Sstevel@tonic-gate "fdisk: Error reading partition table from %s.\n", 14010Sstevel@tonic-gate Dfltdev); 14020Sstevel@tonic-gate if (!io_image) 14030Sstevel@tonic-gate exit(1); 14040Sstevel@tonic-gate } 14050Sstevel@tonic-gate } 14060Sstevel@tonic-gate 14070Sstevel@tonic-gate /* 14080Sstevel@tonic-gate * dev_mboot_write 14090Sstevel@tonic-gate * Write the master boot sector to the device. 14100Sstevel@tonic-gate */ 1411251Slclee static void 14126549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz) 14130Sstevel@tonic-gate { 14140Sstevel@tonic-gate int new_pt, old_pt, error; 14156478Sbharding int clr_efi = -1; 14160Sstevel@tonic-gate 14170Sstevel@tonic-gate if (io_readonly) 1418251Slclee return; 14190Sstevel@tonic-gate 14200Sstevel@tonic-gate if (io_debug) { 1421251Slclee (void) fprintf(stderr, "About to write fdisk table:\n"); 14220Sstevel@tonic-gate print_Table(); 14230Sstevel@tonic-gate if (io_ifdisk) { 1424251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 14259786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 14260Sstevel@tonic-gate } 14270Sstevel@tonic-gate } 14280Sstevel@tonic-gate 14296478Sbharding /* 14306478Sbharding * If the new table has any Solaris partitions and the old 14316478Sbharding * table does not have an entry that describes it 14326478Sbharding * exactly then clear the old vtoc (if any). 14336478Sbharding */ 14340Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 14350Sstevel@tonic-gate 14366478Sbharding /* We only care about potential Solaris parts. */ 14370Sstevel@tonic-gate if (Table[new_pt].systid != SUNIXOS && 14380Sstevel@tonic-gate Table[new_pt].systid != SUNIXOS2) 14390Sstevel@tonic-gate continue; 14400Sstevel@tonic-gate 14416478Sbharding /* Does the old table have an exact entry for the new entry? */ 14426478Sbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 14436478Sbharding 14446478Sbharding /* We only care about old Solaris partitions. */ 14456478Sbharding if ((Old_Table[old_pt].systid == SUNIXOS) || 14466478Sbharding (Old_Table[old_pt].systid == SUNIXOS2)) { 14476478Sbharding 14486478Sbharding /* Is this old one the same as a new one? */ 14496478Sbharding if ((Old_Table[old_pt].relsect == 14506478Sbharding Table[new_pt].relsect) && 14516478Sbharding (Old_Table[old_pt].numsect == 14526478Sbharding Table[new_pt].numsect)) 14536478Sbharding break; /* Yes */ 14546478Sbharding } 14556478Sbharding } 14566478Sbharding 14576478Sbharding /* Did a solaris partition change location or size? */ 14586478Sbharding if (old_pt >= FD_NUMPART) { 14596478Sbharding /* Yes clear old vtoc */ 14606478Sbharding if (io_debug) { 14616478Sbharding (void) fprintf(stderr, 14626478Sbharding "Clearing VTOC labels from NEW" 14636478Sbharding " table\n"); 14646478Sbharding } 14656478Sbharding clear_vtoc(NEW, new_pt); 14666478Sbharding } 14676478Sbharding } 14686478Sbharding 14696478Sbharding 14706478Sbharding /* see if the old table had EFI */ 14716478Sbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 14726478Sbharding if (Old_Table[old_pt].systid == EFI_PMBR) { 14736478Sbharding clr_efi = old_pt; 14740Sstevel@tonic-gate } 14750Sstevel@tonic-gate } 14760Sstevel@tonic-gate 14770Sstevel@tonic-gate /* look to see if a EFI partition changed in relsect/numsect */ 14780Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 14790Sstevel@tonic-gate if (Table[new_pt].systid != EFI_PMBR) 14800Sstevel@tonic-gate continue; 14810Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 14825169Slclee if ((Old_Table[old_pt].systid == 14835169Slclee Table[new_pt].systid) && 14845169Slclee (Old_Table[old_pt].relsect == 14855169Slclee Table[new_pt].relsect) && 14865169Slclee (Old_Table[old_pt].numsect == 14875169Slclee Table[new_pt].numsect)) 14885169Slclee break; 14890Sstevel@tonic-gate } 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate /* 14920Sstevel@tonic-gate * if EFI partition changed, set the flag to clear 14930Sstevel@tonic-gate * the EFI GPT 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) { 14960Sstevel@tonic-gate clr_efi = 0; 14970Sstevel@tonic-gate } 14980Sstevel@tonic-gate break; 14990Sstevel@tonic-gate } 15000Sstevel@tonic-gate 15010Sstevel@tonic-gate /* clear labels if necessary */ 15020Sstevel@tonic-gate if (clr_efi >= 0) { 15030Sstevel@tonic-gate if (io_debug) { 1504251Slclee (void) fprintf(stderr, "Clearing EFI labels\n"); 15050Sstevel@tonic-gate } 15060Sstevel@tonic-gate if ((error = clear_efi()) != 0) { 15070Sstevel@tonic-gate if (io_debug) { 1508251Slclee (void) fprintf(stderr, 1509251Slclee "\tError %d clearing EFI labels" 15100Sstevel@tonic-gate " (probably no EFI labels exist)\n", 15110Sstevel@tonic-gate error); 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate } 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) { 1517251Slclee (void) fprintf(stderr, 15180Sstevel@tonic-gate "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n", 15190Sstevel@tonic-gate Dfltdev); 15200Sstevel@tonic-gate } 15210Sstevel@tonic-gate if (errno == 0) 15220Sstevel@tonic-gate return; 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate /* write to disk drive */ 15250Sstevel@tonic-gate if (lseek(Dev, sect, SEEK_SET) == -1) { 1526251Slclee (void) fprintf(stderr, 15270Sstevel@tonic-gate "fdisk: Error seeking to master boot record on %s.\n", 15280Sstevel@tonic-gate Dfltdev); 15290Sstevel@tonic-gate exit(1); 15300Sstevel@tonic-gate } 15310Sstevel@tonic-gate if (write(Dev, buff, bootsiz) != bootsiz) { 1532251Slclee (void) fprintf(stderr, 15330Sstevel@tonic-gate "fdisk: Error writing master boot record to %s.\n", 15340Sstevel@tonic-gate Dfltdev); 15350Sstevel@tonic-gate exit(1); 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate } 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate /* 15400Sstevel@tonic-gate * mboot_read 15410Sstevel@tonic-gate * Read the prototype boot records from the files. 15420Sstevel@tonic-gate */ 1543251Slclee static void 1544251Slclee mboot_read(void) 15450Sstevel@tonic-gate { 15460Sstevel@tonic-gate int mDev, i; 15470Sstevel@tonic-gate struct ipart *part; 15480Sstevel@tonic-gate 15490Sstevel@tonic-gate #if defined(i386) || defined(sparc) 15500Sstevel@tonic-gate /* 15510Sstevel@tonic-gate * If the master boot file hasn't been specified, use the 15520Sstevel@tonic-gate * implementation architecture name to generate the default one. 15530Sstevel@tonic-gate */ 15540Sstevel@tonic-gate if (io_mboot == (char *)0) { 15550Sstevel@tonic-gate /* 15560Sstevel@tonic-gate * Bug ID 1249035: 15570Sstevel@tonic-gate * The mboot file must be delivered on all platforms 15580Sstevel@tonic-gate * and installed in a non-platform-dependent 15590Sstevel@tonic-gate * directory; i.e., /usr/lib/fs/ufs. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate io_mboot = "/usr/lib/fs/ufs/mboot"; 15620Sstevel@tonic-gate } 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate /* First read in the master boot record */ 15650Sstevel@tonic-gate 15660Sstevel@tonic-gate /* Open the master boot proto file */ 15670Sstevel@tonic-gate if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) { 1568251Slclee (void) fprintf(stderr, 15690Sstevel@tonic-gate "fdisk: Cannot open master boot file %s.\n", 15700Sstevel@tonic-gate io_mboot); 15710Sstevel@tonic-gate exit(1); 15720Sstevel@tonic-gate } 15730Sstevel@tonic-gate 15740Sstevel@tonic-gate /* Read the master boot program */ 15750Sstevel@tonic-gate if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof 15760Sstevel@tonic-gate (struct mboot)) { 1577251Slclee (void) fprintf(stderr, 15780Sstevel@tonic-gate "fdisk: Cannot read master boot file %s.\n", 15790Sstevel@tonic-gate io_mboot); 15800Sstevel@tonic-gate exit(1); 15810Sstevel@tonic-gate } 15820Sstevel@tonic-gate 15830Sstevel@tonic-gate /* Is this really a master boot record? */ 158410021SSheshadri.Vasudevan@Sun.COM if (LE_16(BootCod.signature) != MBB_MAGIC) { 1585251Slclee (void) fprintf(stderr, 15860Sstevel@tonic-gate "fdisk: Invalid master boot file %s.\n", io_mboot); 1587251Slclee (void) fprintf(stderr, 1588251Slclee "Bad magic number: is %x, but should be %x.\n", 158910021SSheshadri.Vasudevan@Sun.COM LE_16(BootCod.signature), MBB_MAGIC); 15900Sstevel@tonic-gate exit(1); 15910Sstevel@tonic-gate } 15920Sstevel@tonic-gate 1593251Slclee (void) close(mDev); 15940Sstevel@tonic-gate #else 15950Sstevel@tonic-gate #error fdisk needs to be ported to new architecture 15960Sstevel@tonic-gate #endif 15970Sstevel@tonic-gate 15980Sstevel@tonic-gate /* Zero out the partitions part of this record */ 15990Sstevel@tonic-gate part = (struct ipart *)BootCod.parts; 16000Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++, part++) { 1601251Slclee (void) memset(part, 0, sizeof (struct ipart)); 16020Sstevel@tonic-gate } 16030Sstevel@tonic-gate 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate /* 16070Sstevel@tonic-gate * fill_patt 16080Sstevel@tonic-gate * Fill the disk with user/sector number pattern. 16090Sstevel@tonic-gate */ 1610251Slclee static void 1611251Slclee fill_patt(void) 16120Sstevel@tonic-gate { 1613251Slclee int *buff_ptr, i; 16146549Sbharding off_t *off_ptr; 16150Sstevel@tonic-gate int io_fpatt = 0; 16160Sstevel@tonic-gate int io_ipatt = 0; 16170Sstevel@tonic-gate 16180Sstevel@tonic-gate if (strncmp(io_fatt, "#", 1) != 0) { 16190Sstevel@tonic-gate io_fpatt++; 16200Sstevel@tonic-gate io_ipatt = strtoul(io_fatt, 0, 0); 16210Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 16220Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 16235169Slclee *buff_ptr = io_ipatt; 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate /* 16270Sstevel@tonic-gate * Fill disk with pattern based on block number. 16280Sstevel@tonic-gate * Write to the disk at absolute relative block io_offset 16290Sstevel@tonic-gate * for io_size blocks. 16300Sstevel@tonic-gate */ 16310Sstevel@tonic-gate while (io_size--) { 16326549Sbharding off_ptr = (off_t *)Bootsect; 16330Sstevel@tonic-gate if (!io_fpatt) { 16346549Sbharding for (i = 0; i < sectsiz; 16356549Sbharding i += sizeof (off_t), off_ptr++) 16366549Sbharding *off_ptr = io_offset; 16370Sstevel@tonic-gate } 16380Sstevel@tonic-gate /* Write the data to disk */ 16396549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 16406549Sbharding SEEK_SET) == -1) { 1641251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 16425169Slclee Dfltdev); 16430Sstevel@tonic-gate exit(1); 16440Sstevel@tonic-gate } 16450Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1646251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 16475169Slclee Dfltdev); 16480Sstevel@tonic-gate exit(1); 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate } /* while (--io_size); */ 16510Sstevel@tonic-gate } 16520Sstevel@tonic-gate 16530Sstevel@tonic-gate /* 16540Sstevel@tonic-gate * abs_read 16550Sstevel@tonic-gate * Read from the disk at absolute relative block io_offset for 16560Sstevel@tonic-gate * io_size blocks. Write the data to standard ouput (-r). 16570Sstevel@tonic-gate */ 1658251Slclee static void 1659251Slclee abs_read(void) 1660251Slclee { 16610Sstevel@tonic-gate int c; 16620Sstevel@tonic-gate 16630Sstevel@tonic-gate while (io_size--) { 16646549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 16656549Sbharding SEEK_SET) == -1) { 1666251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 16670Sstevel@tonic-gate Dfltdev); 16680Sstevel@tonic-gate exit(1); 16690Sstevel@tonic-gate } 16700Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1671251Slclee (void) fprintf(stderr, "fdisk: Error reading %s.\n", 16720Sstevel@tonic-gate Dfltdev); 16730Sstevel@tonic-gate exit(1); 16740Sstevel@tonic-gate } 16750Sstevel@tonic-gate 16760Sstevel@tonic-gate /* Write to standard ouptut */ 1677251Slclee if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) { 16780Sstevel@tonic-gate if (c >= 0) { 16790Sstevel@tonic-gate if (io_debug) 1680251Slclee (void) fprintf(stderr, 1681251Slclee "fdisk: Output warning: %d of %d" 1682251Slclee " characters written.\n", 1683251Slclee c, sectsiz); 16840Sstevel@tonic-gate exit(2); 16850Sstevel@tonic-gate } else { 16860Sstevel@tonic-gate perror("write error on output file."); 16870Sstevel@tonic-gate exit(2); 16880Sstevel@tonic-gate } 16890Sstevel@tonic-gate } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */ 16900Sstevel@tonic-gate /* != sectsiz) */ 16910Sstevel@tonic-gate } /* while (--io_size); */ 16920Sstevel@tonic-gate exit(0); 16930Sstevel@tonic-gate } 16940Sstevel@tonic-gate 16950Sstevel@tonic-gate /* 16960Sstevel@tonic-gate * abs_write 16970Sstevel@tonic-gate * Read the data from standard input. Write to the disk at 16980Sstevel@tonic-gate * absolute relative block io_offset for io_size blocks (-w). 16990Sstevel@tonic-gate */ 1700251Slclee static void 1701251Slclee abs_write(void) 17020Sstevel@tonic-gate { 17030Sstevel@tonic-gate int c, i; 17040Sstevel@tonic-gate 17050Sstevel@tonic-gate while (io_size--) { 17060Sstevel@tonic-gate int part_exit = 0; 17070Sstevel@tonic-gate /* Read from standard input */ 17080Sstevel@tonic-gate if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) { 17090Sstevel@tonic-gate if (c >= 0) { 17100Sstevel@tonic-gate if (io_debug) 1711251Slclee (void) fprintf(stderr, 17120Sstevel@tonic-gate "fdisk: WARNING: Incomplete read (%d of" 17130Sstevel@tonic-gate " %d characters read) on input file.\n", 17145169Slclee c, sectsiz); 17150Sstevel@tonic-gate /* Fill pattern to mark partial sector in buf */ 17160Sstevel@tonic-gate for (i = c; i < sectsiz; ) { 17170Sstevel@tonic-gate Bootsect[i++] = 0x41; 17180Sstevel@tonic-gate Bootsect[i++] = 0x62; 17190Sstevel@tonic-gate Bootsect[i++] = 0x65; 17200Sstevel@tonic-gate Bootsect[i++] = 0; 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate part_exit++; 17230Sstevel@tonic-gate } else { 17240Sstevel@tonic-gate perror("read error on input file."); 17250Sstevel@tonic-gate exit(2); 17260Sstevel@tonic-gate } 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate } 17290Sstevel@tonic-gate /* Write to disk drive */ 17306549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 17316549Sbharding SEEK_SET) == -1) { 1732251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 17330Sstevel@tonic-gate Dfltdev); 17340Sstevel@tonic-gate exit(1); 17350Sstevel@tonic-gate } 17360Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1737251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 17380Sstevel@tonic-gate Dfltdev); 17390Sstevel@tonic-gate exit(1); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate if (part_exit) 17420Sstevel@tonic-gate exit(0); 17430Sstevel@tonic-gate } /* while (--io_size); */ 17440Sstevel@tonic-gate exit(1); 17450Sstevel@tonic-gate } 17460Sstevel@tonic-gate 1747251Slclee 17480Sstevel@tonic-gate /* 17490Sstevel@tonic-gate * load 17500Sstevel@tonic-gate * Load will either read the fdisk table from a file or add or 17510Sstevel@tonic-gate * delete an entry (-A, -D, -F). 17520Sstevel@tonic-gate */ 17530Sstevel@tonic-gate 1754251Slclee static void 1755251Slclee load(int funct, char *file) 17560Sstevel@tonic-gate { 17570Sstevel@tonic-gate int id; 17580Sstevel@tonic-gate int act; 17590Sstevel@tonic-gate int bhead; 17600Sstevel@tonic-gate int bsect; 17610Sstevel@tonic-gate int bcyl; 17620Sstevel@tonic-gate int ehead; 17630Sstevel@tonic-gate int esect; 17640Sstevel@tonic-gate int ecyl; 17657563SPrasad.Singamsetty@Sun.COM uint32_t rsect; 17667563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 17670Sstevel@tonic-gate char line[256]; 17680Sstevel@tonic-gate int i = 0; 17690Sstevel@tonic-gate FILE *fp; 1770*11382SShidokht.Yadegari@Sun.COM int startindex = 0; 1771*11382SShidokht.Yadegari@Sun.COM int tmpindex = 0; 177210021SSheshadri.Vasudevan@Sun.COM #ifdef i386 177310021SSheshadri.Vasudevan@Sun.COM int ext_part_present = 0; 177410021SSheshadri.Vasudevan@Sun.COM uint32_t begsec, endsec, relsect; 177510021SSheshadri.Vasudevan@Sun.COM logical_drive_t *temp; 177610021SSheshadri.Vasudevan@Sun.COM int part_count = 0, ldcnt = 0; 177710021SSheshadri.Vasudevan@Sun.COM uint32_t ext_beg_sec, ext_end_sec; 177810021SSheshadri.Vasudevan@Sun.COM uint32_t old_ext_beg_sec = 0, old_ext_num_sec = 0; 177910021SSheshadri.Vasudevan@Sun.COM uint32_t new_ext_beg_sec = 0, new_ext_num_sec = 0; 178010021SSheshadri.Vasudevan@Sun.COM int ext_part_inited = 0; 178110021SSheshadri.Vasudevan@Sun.COM uchar_t systid; 178210021SSheshadri.Vasudevan@Sun.COM #endif 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate switch (funct) { 17850Sstevel@tonic-gate 17865169Slclee case LOADFILE: 17870Sstevel@tonic-gate 17880Sstevel@tonic-gate /* 17890Sstevel@tonic-gate * Zero out the table before loading it, which will 17900Sstevel@tonic-gate * force it to be updated on disk later (-F 17910Sstevel@tonic-gate * fdisk_file). 17920Sstevel@tonic-gate */ 17930Sstevel@tonic-gate nulltbl(); 17940Sstevel@tonic-gate 17950Sstevel@tonic-gate /* Open the prototype file */ 17960Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 17970Sstevel@tonic-gate (void) fprintf(stderr, 17980Sstevel@tonic-gate "fdisk: Cannot open prototype partition file %s.\n", 17990Sstevel@tonic-gate file); 18000Sstevel@tonic-gate exit(1); 18010Sstevel@tonic-gate } 18020Sstevel@tonic-gate 18030Sstevel@tonic-gate /* Read a line from the file */ 18040Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 18050Sstevel@tonic-gate if (pars_fdisk(line, &id, &act, &bhead, &bsect, 18060Sstevel@tonic-gate &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) { 18070Sstevel@tonic-gate continue; 18080Sstevel@tonic-gate } 180910021SSheshadri.Vasudevan@Sun.COM #ifdef i386 181010021SSheshadri.Vasudevan@Sun.COM part_count++; 181110021SSheshadri.Vasudevan@Sun.COM 181210021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended((uchar_t)id)) { 181310021SSheshadri.Vasudevan@Sun.COM if (ext_part_present) { 181410021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Extended partition" 181510021SSheshadri.Vasudevan@Sun.COM " already exists\n"); 181610021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "fdisk: Error on" 181710021SSheshadri.Vasudevan@Sun.COM " entry \"%s\".\n", line); 181810021SSheshadri.Vasudevan@Sun.COM exit(1); 181910021SSheshadri.Vasudevan@Sun.COM } 182010021SSheshadri.Vasudevan@Sun.COM ext_part_present = 1; 182110021SSheshadri.Vasudevan@Sun.COM /* 182210021SSheshadri.Vasudevan@Sun.COM * If the existing extended partition's start 182310021SSheshadri.Vasudevan@Sun.COM * and size matches the new one, do not 182410021SSheshadri.Vasudevan@Sun.COM * initialize the extended partition EBR 182510021SSheshadri.Vasudevan@Sun.COM * (Extended Boot Record) because there could 182610021SSheshadri.Vasudevan@Sun.COM * be existing logical drives. 182710021SSheshadri.Vasudevan@Sun.COM */ 182810021SSheshadri.Vasudevan@Sun.COM for (i = 0; i < FD_NUMPART; i++) { 182910021SSheshadri.Vasudevan@Sun.COM systid = Old_Table[i].systid; 183010021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(systid)) { 183110021SSheshadri.Vasudevan@Sun.COM old_ext_beg_sec = 183210021SSheshadri.Vasudevan@Sun.COM Old_Table[i].relsect; 183310021SSheshadri.Vasudevan@Sun.COM old_ext_num_sec = 183410021SSheshadri.Vasudevan@Sun.COM Old_Table[i].numsect; 183510021SSheshadri.Vasudevan@Sun.COM break; 183610021SSheshadri.Vasudevan@Sun.COM } 183710021SSheshadri.Vasudevan@Sun.COM } 183810021SSheshadri.Vasudevan@Sun.COM new_ext_beg_sec = rsect; 183910021SSheshadri.Vasudevan@Sun.COM new_ext_num_sec = numsect; 184010021SSheshadri.Vasudevan@Sun.COM if ((old_ext_beg_sec != new_ext_beg_sec) || 184110021SSheshadri.Vasudevan@Sun.COM (old_ext_num_sec != new_ext_num_sec)) { 184210021SSheshadri.Vasudevan@Sun.COM fdisk_init_ext_part(epp, 184310021SSheshadri.Vasudevan@Sun.COM new_ext_beg_sec, new_ext_num_sec); 184410021SSheshadri.Vasudevan@Sun.COM ext_part_inited = 1; 184510021SSheshadri.Vasudevan@Sun.COM } 184610021SSheshadri.Vasudevan@Sun.COM } 184710021SSheshadri.Vasudevan@Sun.COM 184810021SSheshadri.Vasudevan@Sun.COM if (part_count > FD_NUMPART) { 184910021SSheshadri.Vasudevan@Sun.COM /* This line should be logical drive info */ 185010021SSheshadri.Vasudevan@Sun.COM int offset = MAX_LOGDRIVE_OFFSET; 185110021SSheshadri.Vasudevan@Sun.COM if (!ext_part_present) { 185210021SSheshadri.Vasudevan@Sun.COM /* Erroneous input file */ 185310021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "More than 4 primary" 185410021SSheshadri.Vasudevan@Sun.COM " partitions found in input\n"); 185510021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Exiting...\n"); 185610021SSheshadri.Vasudevan@Sun.COM exit(1); 185710021SSheshadri.Vasudevan@Sun.COM } 185810021SSheshadri.Vasudevan@Sun.COM 185910021SSheshadri.Vasudevan@Sun.COM if (numsect == 0) { 186010021SSheshadri.Vasudevan@Sun.COM continue; 186110021SSheshadri.Vasudevan@Sun.COM } 186210021SSheshadri.Vasudevan@Sun.COM 186310021SSheshadri.Vasudevan@Sun.COM /* 186410021SSheshadri.Vasudevan@Sun.COM * If the start and size of the existing 186510021SSheshadri.Vasudevan@Sun.COM * extended partition matches the new one and 186610021SSheshadri.Vasudevan@Sun.COM * new logical drives are being defined via 186710021SSheshadri.Vasudevan@Sun.COM * the input file, initialize the EBR. 186810021SSheshadri.Vasudevan@Sun.COM */ 186910021SSheshadri.Vasudevan@Sun.COM if (!ext_part_inited) { 187010021SSheshadri.Vasudevan@Sun.COM fdisk_init_ext_part(epp, 187110021SSheshadri.Vasudevan@Sun.COM new_ext_beg_sec, new_ext_num_sec); 187210021SSheshadri.Vasudevan@Sun.COM ext_part_inited = 1; 187310021SSheshadri.Vasudevan@Sun.COM } 187410021SSheshadri.Vasudevan@Sun.COM 187510021SSheshadri.Vasudevan@Sun.COM begsec = rsect - offset; 187610021SSheshadri.Vasudevan@Sun.COM if ((ldcnt = 187710021SSheshadri.Vasudevan@Sun.COM fdisk_get_logical_drive_count(epp)) == 0) { 187810021SSheshadri.Vasudevan@Sun.COM /* Adding the first logical drive */ 187910021SSheshadri.Vasudevan@Sun.COM /* 188010021SSheshadri.Vasudevan@Sun.COM * Make sure that begsec doesnt wrap 188110021SSheshadri.Vasudevan@Sun.COM * around. This can happen if rsect is 188210021SSheshadri.Vasudevan@Sun.COM * less than offset. 188310021SSheshadri.Vasudevan@Sun.COM */ 188410021SSheshadri.Vasudevan@Sun.COM if (rsect < offset) { 188510021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Minimum of " 188610021SSheshadri.Vasudevan@Sun.COM "63 free sectors required " 188710021SSheshadri.Vasudevan@Sun.COM "before the beginning of " 188810021SSheshadri.Vasudevan@Sun.COM "a logical drive."); 188910021SSheshadri.Vasudevan@Sun.COM exit(1); 189010021SSheshadri.Vasudevan@Sun.COM } 189110021SSheshadri.Vasudevan@Sun.COM /* 189210021SSheshadri.Vasudevan@Sun.COM * Check if the first logical drive 189310021SSheshadri.Vasudevan@Sun.COM * is out of order. In that case, do 189410021SSheshadri.Vasudevan@Sun.COM * not subtract MAX_LOGDRIVE_OFFSET 189510021SSheshadri.Vasudevan@Sun.COM * from the given start of partition. 189610021SSheshadri.Vasudevan@Sun.COM */ 189710021SSheshadri.Vasudevan@Sun.COM if (begsec != new_ext_beg_sec) { 189810021SSheshadri.Vasudevan@Sun.COM begsec = rsect; 189910021SSheshadri.Vasudevan@Sun.COM offset = 0; 190010021SSheshadri.Vasudevan@Sun.COM } 190110021SSheshadri.Vasudevan@Sun.COM } 190210021SSheshadri.Vasudevan@Sun.COM if (ldcnt >= MAX_EXT_PARTS) { 190310021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "\nError : Number of " 190410021SSheshadri.Vasudevan@Sun.COM "logical drives exceeds limit of " 190510021SSheshadri.Vasudevan@Sun.COM "%d.\n", MAX_EXT_PARTS); 190610021SSheshadri.Vasudevan@Sun.COM exit(1); 190710021SSheshadri.Vasudevan@Sun.COM } 190810021SSheshadri.Vasudevan@Sun.COM 190910021SSheshadri.Vasudevan@Sun.COM if (id > FDISK_MAX_VALID_PART_ID) { 191010021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Invalid partition " 191110021SSheshadri.Vasudevan@Sun.COM "ID\n"); 191210021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "fdisk: Error on" 191310021SSheshadri.Vasudevan@Sun.COM " entry \"%s\".\n", line); 191410021SSheshadri.Vasudevan@Sun.COM exit(1); 191510021SSheshadri.Vasudevan@Sun.COM } 191610021SSheshadri.Vasudevan@Sun.COM 191710021SSheshadri.Vasudevan@Sun.COM endsec = rsect + numsect - 1; 191810021SSheshadri.Vasudevan@Sun.COM if (fdisk_validate_logical_drive(epp, 191910021SSheshadri.Vasudevan@Sun.COM begsec, offset, numsect) == 0) { 192010021SSheshadri.Vasudevan@Sun.COM if (id == EFI_PMBR) { 192110021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "EFI " 192210021SSheshadri.Vasudevan@Sun.COM "partitions not supported " 192310021SSheshadri.Vasudevan@Sun.COM "inside extended " 192410021SSheshadri.Vasudevan@Sun.COM "partition\n"); 192510021SSheshadri.Vasudevan@Sun.COM exit(1); 192610021SSheshadri.Vasudevan@Sun.COM } 192710021SSheshadri.Vasudevan@Sun.COM fdisk_add_logical_drive(epp, begsec, 192810021SSheshadri.Vasudevan@Sun.COM endsec, id); 192910021SSheshadri.Vasudevan@Sun.COM continue; 193010021SSheshadri.Vasudevan@Sun.COM } else { 193110021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "fdisk: Error on" 193210021SSheshadri.Vasudevan@Sun.COM " entry \"%s\".\n", line); 193310021SSheshadri.Vasudevan@Sun.COM exit(1); 193410021SSheshadri.Vasudevan@Sun.COM } 193510021SSheshadri.Vasudevan@Sun.COM } 193610021SSheshadri.Vasudevan@Sun.COM #endif 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate /* 19390Sstevel@tonic-gate * Validate the partition. It cannot start at sector 19400Sstevel@tonic-gate * 0 unless it is UNUSED or already exists 19410Sstevel@tonic-gate */ 19420Sstevel@tonic-gate if (validate_part(id, rsect, numsect) < 0) { 19430Sstevel@tonic-gate (void) fprintf(stderr, 19440Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 19450Sstevel@tonic-gate line); 19460Sstevel@tonic-gate exit(1); 19470Sstevel@tonic-gate } 19488904SBarry.Harding@Sun.COM 1949*11382SShidokht.Yadegari@Sun.COM if ((tmpindex = entry_from_old_table(id, act, bhead, 1950*11382SShidokht.Yadegari@Sun.COM bsect, bcyl, ehead, esect, ecyl, rsect, numsect, 1951*11382SShidokht.Yadegari@Sun.COM startindex)) != -1) { 19528904SBarry.Harding@Sun.COM /* 19538904SBarry.Harding@Sun.COM * If we got here it means we copied an 19548904SBarry.Harding@Sun.COM * unmodified entry. So there is no need 19558904SBarry.Harding@Sun.COM * to insert it in the table or do any 19568904SBarry.Harding@Sun.COM * checks against disk size. 19578904SBarry.Harding@Sun.COM * 19588904SBarry.Harding@Sun.COM * This is a work around on the following 19598904SBarry.Harding@Sun.COM * situation (for IDE disks, at least): 19608904SBarry.Harding@Sun.COM * Different operation systems calculate 19618904SBarry.Harding@Sun.COM * disk size different ways, of which there 19628904SBarry.Harding@Sun.COM * are two main ways. 19638904SBarry.Harding@Sun.COM * 19648904SBarry.Harding@Sun.COM * The first, rounds the disk size to modulo 19658904SBarry.Harding@Sun.COM * cylinder size (virtual made-up cylinder 19668904SBarry.Harding@Sun.COM * usually based on maximum number of heads 19678904SBarry.Harding@Sun.COM * and sectors in partition table fields). 19688904SBarry.Harding@Sun.COM * Our OS's (for IDE) and most other "Unix" 19698904SBarry.Harding@Sun.COM * type OS's do this. 19708904SBarry.Harding@Sun.COM * 19718904SBarry.Harding@Sun.COM * The second, uses every single block 19728904SBarry.Harding@Sun.COM * on the disk (to maximize available space). 19738904SBarry.Harding@Sun.COM * Since disk manufactures do not know about 19748904SBarry.Harding@Sun.COM * "virtual cylinders", there are some number 19758904SBarry.Harding@Sun.COM * of blocks that make up a partial cylinder 19768904SBarry.Harding@Sun.COM * at the end of the disk. 19778904SBarry.Harding@Sun.COM * 19788904SBarry.Harding@Sun.COM * The difference between these two methods 19798904SBarry.Harding@Sun.COM * is where the problem is. When one 19808904SBarry.Harding@Sun.COM * tries to install Solaris/OpenSolaris on 19818904SBarry.Harding@Sun.COM * a disk that has another OS using that 19828904SBarry.Harding@Sun.COM * "partial cylinder", install fails. It fails 19838904SBarry.Harding@Sun.COM * since fdisk thinks its asked to create a 19848904SBarry.Harding@Sun.COM * partition with the -F option that contains 19858904SBarry.Harding@Sun.COM * a partition that runs off the end of the 19868904SBarry.Harding@Sun.COM * disk. 19878904SBarry.Harding@Sun.COM */ 1988*11382SShidokht.Yadegari@Sun.COM startindex = tmpindex + 1; 19898904SBarry.Harding@Sun.COM continue; 19908904SBarry.Harding@Sun.COM } 19918904SBarry.Harding@Sun.COM 19920Sstevel@tonic-gate /* 19930Sstevel@tonic-gate * Find an unused entry to use and put the entry 19940Sstevel@tonic-gate * in table 19950Sstevel@tonic-gate */ 1996*11382SShidokht.Yadegari@Sun.COM if ((startindex = insert_tbl(id, act, bhead, bsect, 1997*11382SShidokht.Yadegari@Sun.COM bcyl, ehead, esect, ecyl, rsect, numsect, 1998*11382SShidokht.Yadegari@Sun.COM startindex)) < 0) { 19990Sstevel@tonic-gate (void) fprintf(stderr, 20000Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 20010Sstevel@tonic-gate line); 20020Sstevel@tonic-gate exit(1); 20030Sstevel@tonic-gate } 2004*11382SShidokht.Yadegari@Sun.COM startindex++; 20050Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 20060Sstevel@tonic-gate 20070Sstevel@tonic-gate if (verify_tbl() < 0) { 2008251Slclee (void) fprintf(stderr, 20090Sstevel@tonic-gate "fdisk: Cannot create partition table\n"); 20100Sstevel@tonic-gate exit(1); 20110Sstevel@tonic-gate } 20120Sstevel@tonic-gate 2013251Slclee (void) fclose(fp); 20140Sstevel@tonic-gate return; 20150Sstevel@tonic-gate 20165169Slclee case LOADDEL: 20170Sstevel@tonic-gate 20180Sstevel@tonic-gate /* Parse the user-supplied deletion line (-D) */ 2019251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, 2020251Slclee &ehead, &esect, &ecyl, &rsect, &numsect)) { 2021251Slclee (void) fprintf(stderr, 2022251Slclee "fdisk: Syntax error \"%s\"\n", file); 2023251Slclee exit(1); 2024251Slclee } 20250Sstevel@tonic-gate 20260Sstevel@tonic-gate /* Find the exact entry in the table */ 20270Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 20280Sstevel@tonic-gate if (Table[i].systid == id && 20290Sstevel@tonic-gate Table[i].bootid == act && 20300Sstevel@tonic-gate Table[i].beghead == bhead && 20310Sstevel@tonic-gate Table[i].begsect == ((bsect & 0x3f) | 20325169Slclee (uchar_t)((bcyl>>2) & 0xc0)) && 2033251Slclee Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 20340Sstevel@tonic-gate Table[i].endhead == ehead && 20350Sstevel@tonic-gate Table[i].endsect == ((esect & 0x3f) | 20365169Slclee (uchar_t)((ecyl>>2) & 0xc0)) && 2037251Slclee Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 203810021SSheshadri.Vasudevan@Sun.COM Table[i].relsect == LE_32(rsect) && 203910021SSheshadri.Vasudevan@Sun.COM Table[i].numsect == LE_32(numsect)) { 20400Sstevel@tonic-gate 2041*11382SShidokht.Yadegari@Sun.COM (void) memset(&Table[i], 0, 2042*11382SShidokht.Yadegari@Sun.COM sizeof (struct ipart)); 204310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 204410021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(id)) { 204510021SSheshadri.Vasudevan@Sun.COM fdisk_delete_ext_part(epp); 204610021SSheshadri.Vasudevan@Sun.COM } 204710021SSheshadri.Vasudevan@Sun.COM #endif 20480Sstevel@tonic-gate return; 20490Sstevel@tonic-gate } 20500Sstevel@tonic-gate } 205110021SSheshadri.Vasudevan@Sun.COM 205210021SSheshadri.Vasudevan@Sun.COM #ifdef i386 205310021SSheshadri.Vasudevan@Sun.COM ldcnt = FD_NUMPART + 1; 205410021SSheshadri.Vasudevan@Sun.COM for (temp = fdisk_get_ld_head(epp); temp != NULL; 205510021SSheshadri.Vasudevan@Sun.COM temp = temp->next) { 205610021SSheshadri.Vasudevan@Sun.COM relsect = temp->abs_secnum + temp->logdrive_offset; 205710021SSheshadri.Vasudevan@Sun.COM if (temp->parts[0].systid == id && 205810021SSheshadri.Vasudevan@Sun.COM temp->parts[0].bootid == act && 205910021SSheshadri.Vasudevan@Sun.COM temp->parts[0].beghead == bhead && 206010021SSheshadri.Vasudevan@Sun.COM temp->parts[0].begsect == ((bsect & 0x3f) | 206110021SSheshadri.Vasudevan@Sun.COM (uchar_t)((bcyl>>2) & 0xc0)) && 206210021SSheshadri.Vasudevan@Sun.COM temp->parts[0].begcyl == (uchar_t)(bcyl & 0xff) && 206310021SSheshadri.Vasudevan@Sun.COM temp->parts[0].endhead == ehead && 206410021SSheshadri.Vasudevan@Sun.COM temp->parts[0].endsect == ((esect & 0x3f) | 206510021SSheshadri.Vasudevan@Sun.COM (uchar_t)((ecyl>>2) & 0xc0)) && 206610021SSheshadri.Vasudevan@Sun.COM temp->parts[0].endcyl == (uchar_t)(ecyl & 0xff) && 206710021SSheshadri.Vasudevan@Sun.COM relsect == LE_32(rsect) && 206810021SSheshadri.Vasudevan@Sun.COM temp->parts[0].numsect == LE_32(numsect)) { 206910021SSheshadri.Vasudevan@Sun.COM fdisk_delete_logical_drive(epp, ldcnt); 207010021SSheshadri.Vasudevan@Sun.COM return; 207110021SSheshadri.Vasudevan@Sun.COM } 207210021SSheshadri.Vasudevan@Sun.COM ldcnt++; 207310021SSheshadri.Vasudevan@Sun.COM } 207410021SSheshadri.Vasudevan@Sun.COM #endif 207510021SSheshadri.Vasudevan@Sun.COM 2076251Slclee (void) fprintf(stderr, 20770Sstevel@tonic-gate "fdisk: Entry does not match any existing partition:\n" 20780Sstevel@tonic-gate " \"%s\"\n", 20790Sstevel@tonic-gate file); 20800Sstevel@tonic-gate exit(1); 20818333SSuhasini.Peddada@Sun.COM /* FALLTHRU */ 20820Sstevel@tonic-gate 20835169Slclee case LOADADD: 20840Sstevel@tonic-gate 20850Sstevel@tonic-gate /* Parse the user-supplied addition line (-A) */ 2086251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead, 2087251Slclee &esect, &ecyl, &rsect, &numsect)) { 2088251Slclee (void) fprintf(stderr, 2089251Slclee "fdisk: Syntax error \"%s\"\n", file); 2090251Slclee exit(1); 2091251Slclee } 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate /* Validate the partition. It cannot start at sector 0 */ 20940Sstevel@tonic-gate if (rsect == 0) { 20950Sstevel@tonic-gate (void) fprintf(stderr, 20960Sstevel@tonic-gate "fdisk: New partition cannot start at sector 0:\n" 20970Sstevel@tonic-gate " \"%s\".\n", 20980Sstevel@tonic-gate file); 20990Sstevel@tonic-gate exit(1); 21000Sstevel@tonic-gate } 21010Sstevel@tonic-gate 21020Sstevel@tonic-gate /* 21030Sstevel@tonic-gate * if the user wishes to add an EFI partition, we need 21040Sstevel@tonic-gate * more extensive validation. rsect should be 1, and 21050Sstevel@tonic-gate * numsect should equal the entire disk capacity - 1 21060Sstevel@tonic-gate */ 21070Sstevel@tonic-gate 21080Sstevel@tonic-gate if (id == EFI_PMBR) { 21090Sstevel@tonic-gate if (rsect != 1) { 21100Sstevel@tonic-gate (void) fprintf(stderr, 21110Sstevel@tonic-gate "fdisk: EFI partitions must start at sector" 21120Sstevel@tonic-gate " 1 (input rsect = %d)\n", rsect); 21130Sstevel@tonic-gate exit(1); 21140Sstevel@tonic-gate } 21150Sstevel@tonic-gate 21167563SPrasad.Singamsetty@Sun.COM 21177563SPrasad.Singamsetty@Sun.COM if (dev_capacity > DK_MAX_2TB) { 21187563SPrasad.Singamsetty@Sun.COM if (numsect != DK_MAX_2TB) { 21197563SPrasad.Singamsetty@Sun.COM (void) fprintf(stderr, 21207563SPrasad.Singamsetty@Sun.COM "fdisk: EFI partitions must " 21217563SPrasad.Singamsetty@Sun.COM "encompass the entire maximum 2 TB " 21227563SPrasad.Singamsetty@Sun.COM "(input numsect: %u - max: %llu)\n", 21237563SPrasad.Singamsetty@Sun.COM numsect, (diskaddr_t)DK_MAX_2TB); 21247563SPrasad.Singamsetty@Sun.COM exit(1); 21257563SPrasad.Singamsetty@Sun.COM } 21267563SPrasad.Singamsetty@Sun.COM } else if (numsect != dev_capacity - 1) { 21270Sstevel@tonic-gate (void) fprintf(stderr, 21280Sstevel@tonic-gate "fdisk: EFI partitions must encompass the " 2129251Slclee "entire disk\n" 21307563SPrasad.Singamsetty@Sun.COM "(input numsect: %u - avail: %llu)\n", 2131251Slclee numsect, 21325169Slclee dev_capacity - 1); 21330Sstevel@tonic-gate exit(1); 21340Sstevel@tonic-gate } 21350Sstevel@tonic-gate } 21360Sstevel@tonic-gate 213710021SSheshadri.Vasudevan@Sun.COM #ifdef i386 213810021SSheshadri.Vasudevan@Sun.COM if (id > FDISK_MAX_VALID_PART_ID) { 213910021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition ID\n"); 214010021SSheshadri.Vasudevan@Sun.COM exit(1); 214110021SSheshadri.Vasudevan@Sun.COM } 214210021SSheshadri.Vasudevan@Sun.COM 214310021SSheshadri.Vasudevan@Sun.COM if ((fdisk_ext_part_exists(epp)) && 214410021SSheshadri.Vasudevan@Sun.COM (fdisk_is_dos_extended(id))) { 214510021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, 214610021SSheshadri.Vasudevan@Sun.COM "Extended partition already exists.\n"); 214710021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, 214810021SSheshadri.Vasudevan@Sun.COM "fdisk: Invalid entry could not be " 214910021SSheshadri.Vasudevan@Sun.COM "inserted:\n \"%s\"\n", file); 215010021SSheshadri.Vasudevan@Sun.COM exit(1); 215110021SSheshadri.Vasudevan@Sun.COM } 215210021SSheshadri.Vasudevan@Sun.COM 215310021SSheshadri.Vasudevan@Sun.COM if (fdisk_ext_part_exists(epp) && 215410021SSheshadri.Vasudevan@Sun.COM (rsect >= (ext_beg_sec = fdisk_get_ext_beg_sec(epp))) && 215510021SSheshadri.Vasudevan@Sun.COM (rsect <= (ext_end_sec = fdisk_get_ext_end_sec(epp)))) { 215610021SSheshadri.Vasudevan@Sun.COM int offset = MAX_LOGDRIVE_OFFSET; 215710021SSheshadri.Vasudevan@Sun.COM 215810021SSheshadri.Vasudevan@Sun.COM /* 215910021SSheshadri.Vasudevan@Sun.COM * Make sure that begsec doesnt wrap around. 216010021SSheshadri.Vasudevan@Sun.COM * This can happen if rsect is less than offset 216110021SSheshadri.Vasudevan@Sun.COM */ 216210021SSheshadri.Vasudevan@Sun.COM if (rsect < offset) { 216310021SSheshadri.Vasudevan@Sun.COM return; 216410021SSheshadri.Vasudevan@Sun.COM } 216510021SSheshadri.Vasudevan@Sun.COM begsec = rsect - offset; 216610021SSheshadri.Vasudevan@Sun.COM if ((ldcnt = fdisk_get_logical_drive_count(epp)) == 0) { 216710021SSheshadri.Vasudevan@Sun.COM /* 216810021SSheshadri.Vasudevan@Sun.COM * Adding the first logical drive 216910021SSheshadri.Vasudevan@Sun.COM * Check if the first logical drive 217010021SSheshadri.Vasudevan@Sun.COM * is out of order. In that case, do 217110021SSheshadri.Vasudevan@Sun.COM * not subtract MAX_LOGDRIVE_OFFSET 217210021SSheshadri.Vasudevan@Sun.COM * from the given start of partition. 217310021SSheshadri.Vasudevan@Sun.COM */ 217410021SSheshadri.Vasudevan@Sun.COM if (begsec != ext_beg_sec) { 217510021SSheshadri.Vasudevan@Sun.COM begsec = rsect; 217610021SSheshadri.Vasudevan@Sun.COM offset = 0; 217710021SSheshadri.Vasudevan@Sun.COM } 217810021SSheshadri.Vasudevan@Sun.COM } 217910021SSheshadri.Vasudevan@Sun.COM 218010021SSheshadri.Vasudevan@Sun.COM if (ldcnt >= MAX_EXT_PARTS) { 218110021SSheshadri.Vasudevan@Sun.COM printf("\nNumber of logical drives exceeds " 218210021SSheshadri.Vasudevan@Sun.COM "limit of %d.\n", MAX_EXT_PARTS); 218310021SSheshadri.Vasudevan@Sun.COM printf("Failing further additions.\n"); 218410021SSheshadri.Vasudevan@Sun.COM exit(1); 218510021SSheshadri.Vasudevan@Sun.COM } 218610021SSheshadri.Vasudevan@Sun.COM 218710021SSheshadri.Vasudevan@Sun.COM if (numsect == 0) { 218810021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, 218910021SSheshadri.Vasudevan@Sun.COM "fdisk: Partition size cannot be zero:\n" 219010021SSheshadri.Vasudevan@Sun.COM " \"%s\".\n", 219110021SSheshadri.Vasudevan@Sun.COM file); 219210021SSheshadri.Vasudevan@Sun.COM exit(1); 219310021SSheshadri.Vasudevan@Sun.COM } 219410021SSheshadri.Vasudevan@Sun.COM endsec = rsect + numsect - 1; 219510021SSheshadri.Vasudevan@Sun.COM if (fdisk_validate_logical_drive(epp, begsec, 219610021SSheshadri.Vasudevan@Sun.COM offset, numsect) == 0) { 219710021SSheshadri.Vasudevan@Sun.COM /* Valid logical drive */ 219810021SSheshadri.Vasudevan@Sun.COM fdisk_add_logical_drive(epp, begsec, endsec, 219910021SSheshadri.Vasudevan@Sun.COM id); 220010021SSheshadri.Vasudevan@Sun.COM return; 220110682SSheshadri.Vasudevan@Sun.COM } else { 220210682SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, 220310682SSheshadri.Vasudevan@Sun.COM "fdisk: Invalid entry could not be " 220410682SSheshadri.Vasudevan@Sun.COM "inserted:\n \"%s\"\n", file); 220510682SSheshadri.Vasudevan@Sun.COM exit(1); 220610021SSheshadri.Vasudevan@Sun.COM } 220710021SSheshadri.Vasudevan@Sun.COM } 220810021SSheshadri.Vasudevan@Sun.COM #endif 220910021SSheshadri.Vasudevan@Sun.COM 22100Sstevel@tonic-gate /* Find unused entry for use and put entry in table */ 22110Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect, 2212*11382SShidokht.Yadegari@Sun.COM ecyl, rsect, numsect, 0) < 0) { 22130Sstevel@tonic-gate (void) fprintf(stderr, 22140Sstevel@tonic-gate "fdisk: Invalid entry could not be inserted:\n" 22150Sstevel@tonic-gate " \"%s\"\n", 22160Sstevel@tonic-gate file); 22170Sstevel@tonic-gate exit(1); 22180Sstevel@tonic-gate } 22190Sstevel@tonic-gate 22200Sstevel@tonic-gate /* Make sure new entry does not overlap existing entry */ 22210Sstevel@tonic-gate if (verify_tbl() < 0) { 2222251Slclee (void) fprintf(stderr, 2223251Slclee "fdisk: Cannot create partition \"%s\"\n", file); 22240Sstevel@tonic-gate exit(1); 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate } /* switch funct */ 22270Sstevel@tonic-gate } 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate /* 22300Sstevel@tonic-gate * Set_Table_CHS_Values 22310Sstevel@tonic-gate * 22320Sstevel@tonic-gate * This will calculate the CHS values for beginning and ending CHS 22330Sstevel@tonic-gate * for a single partition table entry (ti) based on the relsect 22340Sstevel@tonic-gate * and numsect values contained in the partion table entry. 22350Sstevel@tonic-gate * 22360Sstevel@tonic-gate * hba_heads and hba_sectors contain the number of heads and sectors. 22370Sstevel@tonic-gate * 22380Sstevel@tonic-gate * If the number of cylinders exceeds the MAX_CYL, 22390Sstevel@tonic-gate * then maximum values will be placed in the corresponding chs entry. 22400Sstevel@tonic-gate */ 22410Sstevel@tonic-gate static void 22420Sstevel@tonic-gate Set_Table_CHS_Values(int ti) 22430Sstevel@tonic-gate { 22440Sstevel@tonic-gate uint32_t lba, cy, hd, sc; 22450Sstevel@tonic-gate 22460Sstevel@tonic-gate lba = (uint32_t)Table[ti].relsect; 22470Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 22480Sstevel@tonic-gate /* 22490Sstevel@tonic-gate * the lba address cannot be expressed in CHS value 22500Sstevel@tonic-gate * so store the maximum CHS field values in the CHS fields. 22510Sstevel@tonic-gate */ 22520Sstevel@tonic-gate cy = MAX_CYL + 1; 22530Sstevel@tonic-gate hd = MAX_HEAD; 22540Sstevel@tonic-gate sc = MAX_SECT; 22550Sstevel@tonic-gate } else { 22560Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 22570Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 22580Sstevel@tonic-gate sc = lba % hba_sectors + 1; 22590Sstevel@tonic-gate } 22600Sstevel@tonic-gate Table[ti].begcyl = cy & 0xff; 2261251Slclee Table[ti].beghead = (uchar_t)hd; 2262251Slclee Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 22630Sstevel@tonic-gate 22640Sstevel@tonic-gate /* 22650Sstevel@tonic-gate * This code is identical to the code above 22660Sstevel@tonic-gate * except that it works on ending CHS values 22670Sstevel@tonic-gate */ 22680Sstevel@tonic-gate lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1); 22690Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 22700Sstevel@tonic-gate cy = MAX_CYL + 1; 22710Sstevel@tonic-gate hd = MAX_HEAD; 22720Sstevel@tonic-gate sc = MAX_SECT; 22730Sstevel@tonic-gate } else { 22740Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 22750Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 22760Sstevel@tonic-gate sc = lba % hba_sectors + 1; 22770Sstevel@tonic-gate } 22780Sstevel@tonic-gate Table[ti].endcyl = cy & 0xff; 2279251Slclee Table[ti].endhead = (uchar_t)hd; 2280251Slclee Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 22810Sstevel@tonic-gate } 22820Sstevel@tonic-gate 22830Sstevel@tonic-gate /* 22840Sstevel@tonic-gate * insert_tbl 22850Sstevel@tonic-gate * Insert entry into fdisk table. Check all user-supplied values 22860Sstevel@tonic-gate * for the entry, but not the validity relative to other table 22870Sstevel@tonic-gate * entries! 22880Sstevel@tonic-gate */ 2289251Slclee static int 2290251Slclee insert_tbl( 2291251Slclee int id, int act, 2292251Slclee int bhead, int bsect, int bcyl, 2293251Slclee int ehead, int esect, int ecyl, 2294*11382SShidokht.Yadegari@Sun.COM uint32_t rsect, uint32_t numsect, int startindex) 22950Sstevel@tonic-gate { 22960Sstevel@tonic-gate int i; 22970Sstevel@tonic-gate 22980Sstevel@tonic-gate /* validate partition size */ 22997563SPrasad.Singamsetty@Sun.COM if (((diskaddr_t)rsect + numsect) > dev_capacity) { 2300251Slclee (void) fprintf(stderr, 23010Sstevel@tonic-gate "fdisk: Partition table exceeds the size of the disk.\n"); 23020Sstevel@tonic-gate return (-1); 23030Sstevel@tonic-gate } 23040Sstevel@tonic-gate 23057563SPrasad.Singamsetty@Sun.COM 23060Sstevel@tonic-gate /* find UNUSED partition table entry */ 2307*11382SShidokht.Yadegari@Sun.COM for (i = startindex; i < FD_NUMPART; i++) { 23080Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 23090Sstevel@tonic-gate break; 23100Sstevel@tonic-gate } 23110Sstevel@tonic-gate } 23120Sstevel@tonic-gate if (i >= FD_NUMPART) { 2313251Slclee (void) fprintf(stderr, "fdisk: Partition table is full.\n"); 23140Sstevel@tonic-gate return (-1); 23150Sstevel@tonic-gate } 23160Sstevel@tonic-gate 23170Sstevel@tonic-gate 2318251Slclee Table[i].systid = (uchar_t)id; 2319251Slclee Table[i].bootid = (uchar_t)act; 232010021SSheshadri.Vasudevan@Sun.COM Table[i].numsect = LE_32(numsect); 232110021SSheshadri.Vasudevan@Sun.COM Table[i].relsect = LE_32(rsect); 23220Sstevel@tonic-gate 2323*11382SShidokht.Yadegari@Sun.COM if (id == UNUSED) { 2324*11382SShidokht.Yadegari@Sun.COM (void) memset(&Table[i], 0, sizeof (struct ipart)); 2325*11382SShidokht.Yadegari@Sun.COM } else if (0 < bsect && bsect <= MAX_SECT && 23260Sstevel@tonic-gate 0 <= bhead && bhead <= MAX_HEAD && 23270Sstevel@tonic-gate 0 < esect && esect <= MAX_SECT && 23280Sstevel@tonic-gate 0 <= ehead && ehead <= MAX_HEAD) { 2329*11382SShidokht.Yadegari@Sun.COM 2330*11382SShidokht.Yadegari@Sun.COM /* 2331*11382SShidokht.Yadegari@Sun.COM * If we have been called with a valid geometry, use it 2332*11382SShidokht.Yadegari@Sun.COM * valid means non-zero values that fit in the BIOS fields 2333*11382SShidokht.Yadegari@Sun.COM */ 23340Sstevel@tonic-gate if (bcyl > MAX_CYL) 23350Sstevel@tonic-gate bcyl = MAX_CYL + 1; 23360Sstevel@tonic-gate if (ecyl > MAX_CYL) 23370Sstevel@tonic-gate ecyl = MAX_CYL + 1; 23380Sstevel@tonic-gate Table[i].begcyl = bcyl & 0xff; 23390Sstevel@tonic-gate Table[i].endcyl = ecyl & 0xff; 2340251Slclee Table[i].beghead = (uchar_t)bhead; 2341251Slclee Table[i].endhead = (uchar_t)ehead; 2342251Slclee Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect); 23430Sstevel@tonic-gate Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect; 23440Sstevel@tonic-gate } else { 23450Sstevel@tonic-gate 23460Sstevel@tonic-gate /* 23470Sstevel@tonic-gate * The specified values are invalid, 23480Sstevel@tonic-gate * so calculate the values based on hba_heads, hba_sectors 23490Sstevel@tonic-gate */ 23500Sstevel@tonic-gate Set_Table_CHS_Values(i); 23510Sstevel@tonic-gate } 23520Sstevel@tonic-gate 23530Sstevel@tonic-gate /* 23540Sstevel@tonic-gate * return partition index 23550Sstevel@tonic-gate */ 23560Sstevel@tonic-gate return (i); 23570Sstevel@tonic-gate } 23580Sstevel@tonic-gate 23590Sstevel@tonic-gate /* 23608904SBarry.Harding@Sun.COM * entry_from_old_table 23618904SBarry.Harding@Sun.COM * If the specified entry is in the old table and is not a Solaris entry 23628904SBarry.Harding@Sun.COM * then insert same entry into new fdisk table. If we do this then 23638904SBarry.Harding@Sun.COM * all checks are skipped for that entry! 23648904SBarry.Harding@Sun.COM */ 23658904SBarry.Harding@Sun.COM static int 23668904SBarry.Harding@Sun.COM entry_from_old_table( 23678904SBarry.Harding@Sun.COM int id, int act, 23688904SBarry.Harding@Sun.COM int bhead, int bsect, int bcyl, 23698904SBarry.Harding@Sun.COM int ehead, int esect, int ecyl, 2370*11382SShidokht.Yadegari@Sun.COM uint32_t rsect, uint32_t numsect, int startindex) 23718904SBarry.Harding@Sun.COM { 23728904SBarry.Harding@Sun.COM uint32_t i, j; 23738904SBarry.Harding@Sun.COM 2374*11382SShidokht.Yadegari@Sun.COM if (id == SUNIXOS || id == SUNIXOS2 || id == UNUSED) 2375*11382SShidokht.Yadegari@Sun.COM return (-1); 237610015SBarry.Harding@Sun.COM for (i = 0; i < FD_NUMPART; i++) { 23778904SBarry.Harding@Sun.COM if (Old_Table[i].systid == id && 23788904SBarry.Harding@Sun.COM Old_Table[i].bootid == act && 23798904SBarry.Harding@Sun.COM Old_Table[i].beghead == bhead && 23808904SBarry.Harding@Sun.COM Old_Table[i].begsect == ((bsect & 0x3f) | 23818904SBarry.Harding@Sun.COM (uchar_t)((bcyl>>2) & 0xc0)) && 23828904SBarry.Harding@Sun.COM Old_Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 23838904SBarry.Harding@Sun.COM Old_Table[i].endhead == ehead && 23848904SBarry.Harding@Sun.COM Old_Table[i].endsect == ((esect & 0x3f) | 23858904SBarry.Harding@Sun.COM (uchar_t)((ecyl>>2) & 0xc0)) && 23868904SBarry.Harding@Sun.COM Old_Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 23878904SBarry.Harding@Sun.COM Old_Table[i].relsect == lel(rsect) && 23888904SBarry.Harding@Sun.COM Old_Table[i].numsect == lel(numsect)) { 23898904SBarry.Harding@Sun.COM /* find UNUSED partition table entry */ 2390*11382SShidokht.Yadegari@Sun.COM for (j = startindex; j < FD_NUMPART; j++) { 23918904SBarry.Harding@Sun.COM if (Table[j].systid == UNUSED) { 23928904SBarry.Harding@Sun.COM (void) memcpy(&Table[j], &Old_Table[i], 23938904SBarry.Harding@Sun.COM sizeof (Table[0])); 23948904SBarry.Harding@Sun.COM skip_verify[j] = 1; 2395*11382SShidokht.Yadegari@Sun.COM return (j); 23968904SBarry.Harding@Sun.COM 23978904SBarry.Harding@Sun.COM } 23988904SBarry.Harding@Sun.COM } 2399*11382SShidokht.Yadegari@Sun.COM return (-1); 24008904SBarry.Harding@Sun.COM } 24018904SBarry.Harding@Sun.COM 24028904SBarry.Harding@Sun.COM } 2403*11382SShidokht.Yadegari@Sun.COM return (-1); 24048904SBarry.Harding@Sun.COM } 24058904SBarry.Harding@Sun.COM 24068904SBarry.Harding@Sun.COM /* 24070Sstevel@tonic-gate * verify_tbl 24080Sstevel@tonic-gate * Verify that no partition entries overlap or exceed the size of 24090Sstevel@tonic-gate * the disk. 24100Sstevel@tonic-gate */ 2411251Slclee static int 2412251Slclee verify_tbl(void) 24130Sstevel@tonic-gate { 24147563SPrasad.Singamsetty@Sun.COM uint32_t i, j, rsect, numsect; 24150Sstevel@tonic-gate int noMoreParts = 0; 24160Sstevel@tonic-gate int numParts = 0; 24170Sstevel@tonic-gate 24180Sstevel@tonic-gate /* Make sure new entry does not overlap an existing entry */ 2419251Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 24200Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 24210Sstevel@tonic-gate numParts++; 24220Sstevel@tonic-gate /* 2423*11382SShidokht.Yadegari@Sun.COM * No valid partitions allowed after EFI_PMBR part 24240Sstevel@tonic-gate */ 24250Sstevel@tonic-gate if (noMoreParts) { 24260Sstevel@tonic-gate return (-1); 24270Sstevel@tonic-gate } 24280Sstevel@tonic-gate 24290Sstevel@tonic-gate if (Table[i].systid == EFI_PMBR) { 2430*11382SShidokht.Yadegari@Sun.COM /* 2431*11382SShidokht.Yadegari@Sun.COM * EFI_PMBR partition must be the only 2432*11382SShidokht.Yadegari@Sun.COM * partition 2433*11382SShidokht.Yadegari@Sun.COM */ 2434*11382SShidokht.Yadegari@Sun.COM noMoreParts = 1; 24350Sstevel@tonic-gate 24360Sstevel@tonic-gate if (Table[i].relsect != 1) { 2437251Slclee (void) fprintf(stderr, "ERROR: " 24380Sstevel@tonic-gate "Invalid starting sector " 24390Sstevel@tonic-gate "for EFI_PMBR partition:\n" 24400Sstevel@tonic-gate "relsect %d " 24410Sstevel@tonic-gate "(should be 1)\n", 24420Sstevel@tonic-gate Table[i].relsect); 24430Sstevel@tonic-gate 24440Sstevel@tonic-gate return (-1); 24450Sstevel@tonic-gate } 24460Sstevel@tonic-gate 2447*11382SShidokht.Yadegari@Sun.COM if (Table[i].numsect != 2448*11382SShidokht.Yadegari@Sun.COM ((dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB: 2449*11382SShidokht.Yadegari@Sun.COM (dev_capacity - 1))) { 2450*11382SShidokht.Yadegari@Sun.COM 2451251Slclee (void) fprintf(stderr, "ERROR: " 24520Sstevel@tonic-gate "EFI_PMBR partition must " 2453*11382SShidokht.Yadegari@Sun.COM "encompass the entire"); 2454*11382SShidokht.Yadegari@Sun.COM 2455*11382SShidokht.Yadegari@Sun.COM if (dev_capacity > DK_MAX_2TB) 2456*11382SShidokht.Yadegari@Sun.COM (void) fprintf(stderr, 2457*11382SShidokht.Yadegari@Sun.COM "maximum 2 TB.\n " 2458*11382SShidokht.Yadegari@Sun.COM "numsect %u - " 2459*11382SShidokht.Yadegari@Sun.COM "actual %llu\n", 2460*11382SShidokht.Yadegari@Sun.COM Table[i].numsect, 2461*11382SShidokht.Yadegari@Sun.COM (diskaddr_t)DK_MAX_2TB); 2462*11382SShidokht.Yadegari@Sun.COM 2463*11382SShidokht.Yadegari@Sun.COM else 2464*11382SShidokht.Yadegari@Sun.COM (void) fprintf(stderr, 2465*11382SShidokht.Yadegari@Sun.COM "disk.\n numsect %u - " 2466*11382SShidokht.Yadegari@Sun.COM "actual %llu\n", 2467*11382SShidokht.Yadegari@Sun.COM Table[i].numsect, 2468*11382SShidokht.Yadegari@Sun.COM dev_capacity - 1); 24690Sstevel@tonic-gate 24700Sstevel@tonic-gate return (-1); 24710Sstevel@tonic-gate } 24720Sstevel@tonic-gate } 24730Sstevel@tonic-gate 24740Sstevel@tonic-gate /* make sure the partition isn't larger than the disk */ 247510021SSheshadri.Vasudevan@Sun.COM rsect = LE_32(Table[i].relsect); 247610021SSheshadri.Vasudevan@Sun.COM numsect = LE_32(Table[i].numsect); 24777563SPrasad.Singamsetty@Sun.COM 24787563SPrasad.Singamsetty@Sun.COM if ((((diskaddr_t)rsect + numsect) > dev_capacity) || 24797563SPrasad.Singamsetty@Sun.COM (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) { 24808904SBarry.Harding@Sun.COM if (!skip_verify[i]) 24818904SBarry.Harding@Sun.COM return (-1); 24820Sstevel@tonic-gate } 24830Sstevel@tonic-gate 2484251Slclee for (j = i + 1; j < FD_NUMPART; j++) { 24850Sstevel@tonic-gate if (Table[j].systid != UNUSED) { 24867563SPrasad.Singamsetty@Sun.COM uint32_t t_relsect = 248710021SSheshadri.Vasudevan@Sun.COM LE_32(Table[j].relsect); 24887563SPrasad.Singamsetty@Sun.COM uint32_t t_numsect = 248910021SSheshadri.Vasudevan@Sun.COM LE_32(Table[j].numsect); 24900Sstevel@tonic-gate 24910Sstevel@tonic-gate if (noMoreParts) { 2492251Slclee (void) fprintf(stderr, 24930Sstevel@tonic-gate "Cannot add partition to " 24940Sstevel@tonic-gate "table; no more partitions " 24950Sstevel@tonic-gate "allowed\n"); 24960Sstevel@tonic-gate 24970Sstevel@tonic-gate if (io_debug) { 2498251Slclee (void) fprintf(stderr, 24990Sstevel@tonic-gate "DEBUG: Current " 25000Sstevel@tonic-gate "partition:\t" 25010Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 2502251Slclee "%d:%d:%d:%d:%d\n" 25030Sstevel@tonic-gate " Next " 25040Sstevel@tonic-gate "partition:\t\t" 25050Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 2506251Slclee "%d:%d:%d:%d:%d\n", 25070Sstevel@tonic-gate Table[i].systid, 25080Sstevel@tonic-gate Table[i].bootid, 25090Sstevel@tonic-gate Table[i].begcyl, 25100Sstevel@tonic-gate Table[i].beghead, 25110Sstevel@tonic-gate Table[i].begsect, 25120Sstevel@tonic-gate Table[i].endcyl, 25130Sstevel@tonic-gate Table[i].endhead, 25140Sstevel@tonic-gate Table[i].endsect, 25150Sstevel@tonic-gate Table[i].relsect, 25160Sstevel@tonic-gate Table[i].numsect, 25170Sstevel@tonic-gate Table[j].systid, 25180Sstevel@tonic-gate Table[j].bootid, 25190Sstevel@tonic-gate Table[j].begcyl, 25200Sstevel@tonic-gate Table[j].beghead, 25210Sstevel@tonic-gate Table[j].begsect, 25220Sstevel@tonic-gate Table[j].endcyl, 25230Sstevel@tonic-gate Table[j].endhead, 25240Sstevel@tonic-gate Table[j].endsect, 25250Sstevel@tonic-gate Table[j].relsect, 25260Sstevel@tonic-gate Table[j].numsect); 25270Sstevel@tonic-gate } 25280Sstevel@tonic-gate 25290Sstevel@tonic-gate return (-1); 25300Sstevel@tonic-gate } 25310Sstevel@tonic-gate if ((rsect >= 25320Sstevel@tonic-gate (t_relsect + t_numsect)) || 2533251Slclee ((rsect + numsect) <= t_relsect)) { 25340Sstevel@tonic-gate continue; 25350Sstevel@tonic-gate } else { 2536251Slclee (void) fprintf(stderr, "ERROR: " 25370Sstevel@tonic-gate "current partition overlaps" 25380Sstevel@tonic-gate " following partition\n"); 25390Sstevel@tonic-gate 25400Sstevel@tonic-gate return (-1); 25410Sstevel@tonic-gate } 25420Sstevel@tonic-gate } 25430Sstevel@tonic-gate } 25440Sstevel@tonic-gate } 25450Sstevel@tonic-gate } 25460Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 25478904SBarry.Harding@Sun.COM if (noMoreParts) 25488904SBarry.Harding@Sun.COM return (-1); 25498904SBarry.Harding@Sun.COM if (!skip_verify[i] && 25508904SBarry.Harding@Sun.COM ((((diskaddr_t)lel(Table[i].relsect) + 25518333SSuhasini.Peddada@Sun.COM lel(Table[i].numsect)) > dev_capacity) || 25528333SSuhasini.Peddada@Sun.COM (((diskaddr_t)lel(Table[i].relsect) + 25538904SBarry.Harding@Sun.COM lel(Table[i].numsect)) > DK_MAX_2TB))) { 25540Sstevel@tonic-gate return (-1); 25550Sstevel@tonic-gate } 25560Sstevel@tonic-gate } 25570Sstevel@tonic-gate 25580Sstevel@tonic-gate return (numParts); 25590Sstevel@tonic-gate } 25600Sstevel@tonic-gate 25610Sstevel@tonic-gate /* 25620Sstevel@tonic-gate * pars_fdisk 25630Sstevel@tonic-gate * Parse user-supplied data to set up fdisk partitions 25640Sstevel@tonic-gate * (-A, -D, -F). 25650Sstevel@tonic-gate */ 2566251Slclee static int 2567251Slclee pars_fdisk( 2568251Slclee char *line, 2569251Slclee int *id, int *act, 2570251Slclee int *bhead, int *bsect, int *bcyl, 2571251Slclee int *ehead, int *esect, int *ecyl, 25727563SPrasad.Singamsetty@Sun.COM uint32_t *rsect, uint32_t *numsect) 25730Sstevel@tonic-gate { 25740Sstevel@tonic-gate int i; 257510021SSheshadri.Vasudevan@Sun.COM int64_t test; 257610021SSheshadri.Vasudevan@Sun.COM char *tok, *p; 257710021SSheshadri.Vasudevan@Sun.COM char buf[256]; 257810021SSheshadri.Vasudevan@Sun.COM 25790Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 25805169Slclee return (1); 25810Sstevel@tonic-gate line[strlen(line)] = '\0'; 25820Sstevel@tonic-gate for (i = 0; i < strlen(line); i++) { 25830Sstevel@tonic-gate if (line[i] == '\0') { 25840Sstevel@tonic-gate break; 25850Sstevel@tonic-gate } else if (line[i] == ':') { 25860Sstevel@tonic-gate line[i] = ' '; 25870Sstevel@tonic-gate } 25880Sstevel@tonic-gate } 258910021SSheshadri.Vasudevan@Sun.COM strncpy(buf, line, 256); 259010021SSheshadri.Vasudevan@Sun.COM errno = 0; 259110021SSheshadri.Vasudevan@Sun.COM tok = strtok(buf, ": \t\n"); 259210021SSheshadri.Vasudevan@Sun.COM while (tok != NULL) { 259310021SSheshadri.Vasudevan@Sun.COM for (p = tok; *p != '\0'; p++) { 259410021SSheshadri.Vasudevan@Sun.COM if (!isdigit(*p)) { 259510021SSheshadri.Vasudevan@Sun.COM printf("Invalid input %s in line %s.\n", 259610021SSheshadri.Vasudevan@Sun.COM tok, line); 259710021SSheshadri.Vasudevan@Sun.COM exit(1); 259810021SSheshadri.Vasudevan@Sun.COM } 259910021SSheshadri.Vasudevan@Sun.COM } 260010021SSheshadri.Vasudevan@Sun.COM 260110021SSheshadri.Vasudevan@Sun.COM test = strtoll(tok, (char **)NULL, 10); 260210021SSheshadri.Vasudevan@Sun.COM if ((test < 0) || (test > 0xFFFFFFFF) || (errno != 0)) { 260310021SSheshadri.Vasudevan@Sun.COM printf("Invalid input %s in line %s.\n", tok, line); 260410021SSheshadri.Vasudevan@Sun.COM exit(1); 260510021SSheshadri.Vasudevan@Sun.COM } 260610021SSheshadri.Vasudevan@Sun.COM tok = strtok(NULL, ": \t\n"); 260710021SSheshadri.Vasudevan@Sun.COM } 26087563SPrasad.Singamsetty@Sun.COM if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u", 26090Sstevel@tonic-gate id, act, bhead, bsect, bcyl, ehead, esect, ecyl, 26100Sstevel@tonic-gate rsect, numsect) != 10) { 26110Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line); 26120Sstevel@tonic-gate exit(1); 26130Sstevel@tonic-gate } 26140Sstevel@tonic-gate return (0); 26150Sstevel@tonic-gate } 26160Sstevel@tonic-gate 26170Sstevel@tonic-gate /* 26180Sstevel@tonic-gate * validate_part 26190Sstevel@tonic-gate * Validate that a new partition does not start at sector 0. Only UNUSED 26200Sstevel@tonic-gate * partitions and previously existing partitions are allowed to start at 0. 26210Sstevel@tonic-gate */ 2622251Slclee static int 26237563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect) 26240Sstevel@tonic-gate { 26250Sstevel@tonic-gate int i; 26260Sstevel@tonic-gate if ((id != UNUSED) && (rsect == 0)) { 26270Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 26280Sstevel@tonic-gate if ((Old_Table[i].systid == id) && 262910021SSheshadri.Vasudevan@Sun.COM (Old_Table[i].relsect == LE_32(rsect)) && 263010021SSheshadri.Vasudevan@Sun.COM (Old_Table[i].numsect == LE_32(numsect))) 26315169Slclee return (0); 26320Sstevel@tonic-gate } 2633251Slclee (void) fprintf(stderr, 2634251Slclee "New partition cannot start at sector 0\n"); 26350Sstevel@tonic-gate return (-1); 26360Sstevel@tonic-gate } 263710021SSheshadri.Vasudevan@Sun.COM #ifdef i386 263810021SSheshadri.Vasudevan@Sun.COM if (id > FDISK_MAX_VALID_PART_ID) { 263910021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Invalid partition ID\n"); 264010021SSheshadri.Vasudevan@Sun.COM return (-1); 264110021SSheshadri.Vasudevan@Sun.COM } 264210021SSheshadri.Vasudevan@Sun.COM #endif 26430Sstevel@tonic-gate return (0); 26440Sstevel@tonic-gate } 26450Sstevel@tonic-gate 26460Sstevel@tonic-gate /* 26470Sstevel@tonic-gate * stage0 26480Sstevel@tonic-gate * Print out interactive menu and process user input. 26490Sstevel@tonic-gate */ 2650251Slclee static void 2651251Slclee stage0(void) 26520Sstevel@tonic-gate { 265310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 265410021SSheshadri.Vasudevan@Sun.COM int rval; 265510021SSheshadri.Vasudevan@Sun.COM #endif 2656251Slclee dispmenu(); 2657251Slclee for (;;) { 2658251Slclee (void) printf(Q_LINE); 2659251Slclee (void) printf("Enter Selection: "); 26609786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 26610Sstevel@tonic-gate rm_blanks(s); 266210021SSheshadri.Vasudevan@Sun.COM #ifdef i386 266310021SSheshadri.Vasudevan@Sun.COM while (!((s[0] > '0') && (s[0] < '8') && 266410021SSheshadri.Vasudevan@Sun.COM ((s[1] == '\0') || (s[1] == '\n')))) { 266510021SSheshadri.Vasudevan@Sun.COM #else 26669786SBarry.Harding@Sun.COM while (!((s[0] > '0') && (s[0] < '7') && 26679786SBarry.Harding@Sun.COM ((s[1] == '\0') || (s[1] == '\n')))) { 266810021SSheshadri.Vasudevan@Sun.COM #endif 2669251Slclee (void) printf(E_LINE); /* Clear any previous error */ 267010021SSheshadri.Vasudevan@Sun.COM #ifdef i386 267110021SSheshadri.Vasudevan@Sun.COM (void) printf( 267210021SSheshadri.Vasudevan@Sun.COM "Enter a one-digit number between 1 and 7."); 267310021SSheshadri.Vasudevan@Sun.COM #else 2674251Slclee (void) printf( 2675251Slclee "Enter a one-digit number between 1 and 6."); 267610021SSheshadri.Vasudevan@Sun.COM #endif 2677251Slclee (void) printf(Q_LINE); 2678251Slclee (void) printf("Enter Selection: "); 26799786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 26800Sstevel@tonic-gate rm_blanks(s); 26810Sstevel@tonic-gate } 2682251Slclee (void) printf(E_LINE); 26830Sstevel@tonic-gate switch (s[0]) { 26840Sstevel@tonic-gate case '1': 26850Sstevel@tonic-gate if (pcreate() == -1) 26860Sstevel@tonic-gate return; 26870Sstevel@tonic-gate break; 26880Sstevel@tonic-gate case '2': 26890Sstevel@tonic-gate if (pchange() == -1) 26900Sstevel@tonic-gate return; 26910Sstevel@tonic-gate break; 26920Sstevel@tonic-gate case '3': 26930Sstevel@tonic-gate if (pdelete() == -1) 26940Sstevel@tonic-gate return; 26950Sstevel@tonic-gate break; 26960Sstevel@tonic-gate case '4': 26970Sstevel@tonic-gate if (ppartid() == -1) 26980Sstevel@tonic-gate return; 26990Sstevel@tonic-gate break; 270010021SSheshadri.Vasudevan@Sun.COM #ifdef i386 270110021SSheshadri.Vasudevan@Sun.COM case '5': 270210021SSheshadri.Vasudevan@Sun.COM if (fdisk_ext_part_exists(epp)) { 270310021SSheshadri.Vasudevan@Sun.COM ext_part_menu(); 270410021SSheshadri.Vasudevan@Sun.COM } else { 270510021SSheshadri.Vasudevan@Sun.COM printf(Q_LINE); 270610021SSheshadri.Vasudevan@Sun.COM printf("\nNo extended partition found" 270710021SSheshadri.Vasudevan@Sun.COM "\n"); 270810021SSheshadri.Vasudevan@Sun.COM printf("Press enter to continue\n"); 270910021SSheshadri.Vasudevan@Sun.COM ext_read_input(s); 271010021SSheshadri.Vasudevan@Sun.COM } 271110021SSheshadri.Vasudevan@Sun.COM break; 271210021SSheshadri.Vasudevan@Sun.COM case '6': 271310021SSheshadri.Vasudevan@Sun.COM /* update disk partition table, if changed */ 271410021SSheshadri.Vasudevan@Sun.COM if (TableChanged() == 1) { 271510021SSheshadri.Vasudevan@Sun.COM copy_Table_to_Bootblk(); 271610021SSheshadri.Vasudevan@Sun.COM dev_mboot_write(0, Bootsect, sectsiz); 271710021SSheshadri.Vasudevan@Sun.COM } 271810021SSheshadri.Vasudevan@Sun.COM 271910021SSheshadri.Vasudevan@Sun.COM /* 272010021SSheshadri.Vasudevan@Sun.COM * If the VTOC table is wrong fix it 272110021SSheshadri.Vasudevan@Sun.COM * (truncate only) 272210021SSheshadri.Vasudevan@Sun.COM */ 272310021SSheshadri.Vasudevan@Sun.COM if (io_adjt) { 272410021SSheshadri.Vasudevan@Sun.COM fix_slice(); 272510021SSheshadri.Vasudevan@Sun.COM } 272610021SSheshadri.Vasudevan@Sun.COM if (!io_readonly) { 272710021SSheshadri.Vasudevan@Sun.COM rval = fdisk_commit_ext_part(epp); 272810021SSheshadri.Vasudevan@Sun.COM switch (rval) { 272910021SSheshadri.Vasudevan@Sun.COM case FDISK_SUCCESS: 273010021SSheshadri.Vasudevan@Sun.COM /* Success */ 273110021SSheshadri.Vasudevan@Sun.COM /* Fallthrough */ 273210021SSheshadri.Vasudevan@Sun.COM case FDISK_ENOEXTPART: 273310021SSheshadri.Vasudevan@Sun.COM /* Nothing to do */ 273410021SSheshadri.Vasudevan@Sun.COM break; 273510021SSheshadri.Vasudevan@Sun.COM case FDISK_EMOUNTED: 273610021SSheshadri.Vasudevan@Sun.COM printf(Q_LINE); 273710021SSheshadri.Vasudevan@Sun.COM preach_and_continue(); 273810021SSheshadri.Vasudevan@Sun.COM continue; 273910021SSheshadri.Vasudevan@Sun.COM default: 274010021SSheshadri.Vasudevan@Sun.COM perror("Commit failed"); 274110021SSheshadri.Vasudevan@Sun.COM exit(1); 274210021SSheshadri.Vasudevan@Sun.COM } 274310021SSheshadri.Vasudevan@Sun.COM libfdisk_fini(&epp); 274410021SSheshadri.Vasudevan@Sun.COM } 274510021SSheshadri.Vasudevan@Sun.COM (void) close(Dev); 274610021SSheshadri.Vasudevan@Sun.COM exit(0); 274710021SSheshadri.Vasudevan@Sun.COM #else 27480Sstevel@tonic-gate case '5': 27490Sstevel@tonic-gate /* update disk partition table, if changed */ 27500Sstevel@tonic-gate if (TableChanged() == 1) { 27510Sstevel@tonic-gate copy_Table_to_Bootblk(); 27520Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 27530Sstevel@tonic-gate } 27540Sstevel@tonic-gate /* 27550Sstevel@tonic-gate * If the VTOC table is wrong fix it 27560Sstevel@tonic-gate * (truncate only) 27570Sstevel@tonic-gate */ 27580Sstevel@tonic-gate if (io_adjt) { 27590Sstevel@tonic-gate fix_slice(); 27600Sstevel@tonic-gate } 2761251Slclee (void) close(Dev); 27620Sstevel@tonic-gate exit(0); 2763251Slclee /* FALLTHRU */ 276410021SSheshadri.Vasudevan@Sun.COM #endif 276510021SSheshadri.Vasudevan@Sun.COM #ifdef i386 276610021SSheshadri.Vasudevan@Sun.COM case '7': 276710021SSheshadri.Vasudevan@Sun.COM #else 27680Sstevel@tonic-gate case '6': 276910021SSheshadri.Vasudevan@Sun.COM #endif 27700Sstevel@tonic-gate /* 27710Sstevel@tonic-gate * If the VTOC table is wrong fix it 27720Sstevel@tonic-gate * (truncate only) 27730Sstevel@tonic-gate */ 27740Sstevel@tonic-gate if (io_adjt) { 27750Sstevel@tonic-gate fix_slice(); 27760Sstevel@tonic-gate } 2777251Slclee (void) close(Dev); 27780Sstevel@tonic-gate exit(0); 2779251Slclee /* FALLTHRU */ 27800Sstevel@tonic-gate default: 27810Sstevel@tonic-gate break; 27820Sstevel@tonic-gate } 27830Sstevel@tonic-gate copy_Table_to_Bootblk(); 27840Sstevel@tonic-gate disptbl(); 2785251Slclee dispmenu(); 27860Sstevel@tonic-gate } 27870Sstevel@tonic-gate } 27880Sstevel@tonic-gate 27890Sstevel@tonic-gate /* 27900Sstevel@tonic-gate * pcreate 27910Sstevel@tonic-gate * Create partition entry in the table (interactive mode). 27920Sstevel@tonic-gate */ 2793251Slclee static int 2794251Slclee pcreate(void) 27950Sstevel@tonic-gate { 2796251Slclee uchar_t tsystid = 'z'; 27970Sstevel@tonic-gate int i, j; 27987563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 27990Sstevel@tonic-gate int retCode = 0; 280010021SSheshadri.Vasudevan@Sun.COM #ifdef i386 280110021SSheshadri.Vasudevan@Sun.COM int ext_part_present = 0; 280210021SSheshadri.Vasudevan@Sun.COM #endif 28030Sstevel@tonic-gate 28040Sstevel@tonic-gate i = 0; 2805251Slclee for (;;) { 28060Sstevel@tonic-gate if (i == FD_NUMPART) { 2807251Slclee (void) printf(E_LINE); 2808251Slclee (void) printf( 2809251Slclee "The partition table is full!\n" 2810251Slclee "You must delete a partition before creating" 28110Sstevel@tonic-gate " a new one.\n"); 28120Sstevel@tonic-gate return (-1); 28130Sstevel@tonic-gate } 28140Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 28150Sstevel@tonic-gate break; 28160Sstevel@tonic-gate } 28170Sstevel@tonic-gate i++; 28180Sstevel@tonic-gate } 28190Sstevel@tonic-gate 28207563SPrasad.Singamsetty@Sun.COM numsect = 0; 28210Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 28220Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 282310021SSheshadri.Vasudevan@Sun.COM numsect += LE_32(Table[i].numsect); 28240Sstevel@tonic-gate } 282510021SSheshadri.Vasudevan@Sun.COM #ifdef i386 282610021SSheshadri.Vasudevan@Sun.COM /* Check if an extended partition already exists */ 282710021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(Table[i].systid)) { 282810021SSheshadri.Vasudevan@Sun.COM ext_part_present = 1; 282910021SSheshadri.Vasudevan@Sun.COM } 283010021SSheshadri.Vasudevan@Sun.COM #endif 28317563SPrasad.Singamsetty@Sun.COM if (numsect >= chs_capacity) { 2832251Slclee (void) printf(E_LINE); 2833251Slclee (void) printf("There is no more room on the disk for" 28340Sstevel@tonic-gate " another partition.\n"); 2835251Slclee (void) printf( 2836251Slclee "You must delete a partition before creating" 28370Sstevel@tonic-gate " a new one.\n"); 28380Sstevel@tonic-gate return (-1); 28390Sstevel@tonic-gate } 28400Sstevel@tonic-gate } 28410Sstevel@tonic-gate while (tsystid == 'z') { 28427563SPrasad.Singamsetty@Sun.COM 28437563SPrasad.Singamsetty@Sun.COM /* 28447563SPrasad.Singamsetty@Sun.COM * The question here is expanding to more than what is 28457563SPrasad.Singamsetty@Sun.COM * allocated for question lines (Q_LINE) which garbles 28467563SPrasad.Singamsetty@Sun.COM * at least warning line. Clearing warning line as workaround 28477563SPrasad.Singamsetty@Sun.COM * for now. 28487563SPrasad.Singamsetty@Sun.COM */ 28497563SPrasad.Singamsetty@Sun.COM 28507563SPrasad.Singamsetty@Sun.COM (void) printf(W_LINE); 2851251Slclee (void) printf(Q_LINE); 2852251Slclee (void) printf( 2853251Slclee "Select the partition type to create:\n" 2854251Slclee " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n" 2855251Slclee " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n" 2856251Slclee " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n" 2857251Slclee " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? "); 28589786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 28590Sstevel@tonic-gate rm_blanks(s); 28609786SBarry.Harding@Sun.COM if ((s[1] != '\0') && (s[1] != '\n')) { 2861251Slclee (void) printf(E_LINE); 2862251Slclee (void) printf("Invalid selection, try again."); 28630Sstevel@tonic-gate continue; 28640Sstevel@tonic-gate } 28650Sstevel@tonic-gate switch (s[0]) { 28660Sstevel@tonic-gate case '0': /* exit */ 28675169Slclee (void) printf(E_LINE); 28685169Slclee return (-1); 28690Sstevel@tonic-gate case '1': /* Solaris partition */ 28705169Slclee tsystid = SUNIXOS2; 28715169Slclee break; 28720Sstevel@tonic-gate case '2': /* UNIX partition */ 28735169Slclee tsystid = UNIXOS; 28745169Slclee break; 28750Sstevel@tonic-gate case '3': /* PCIXOS partition */ 28765169Slclee tsystid = PCIXOS; 28775169Slclee break; 28780Sstevel@tonic-gate case '4': /* OTHEROS System partition */ 28795169Slclee tsystid = OTHEROS; 28805169Slclee break; 28810Sstevel@tonic-gate case '5': 28825169Slclee tsystid = DOSOS12; /* DOS 12 bit fat */ 28835169Slclee break; 28840Sstevel@tonic-gate case '6': 28855169Slclee tsystid = DOSOS16; /* DOS 16 bit fat */ 28865169Slclee break; 28870Sstevel@tonic-gate case '7': 288810021SSheshadri.Vasudevan@Sun.COM #ifdef i386 288910021SSheshadri.Vasudevan@Sun.COM if (ext_part_present) { 289010021SSheshadri.Vasudevan@Sun.COM printf(Q_LINE); 289110021SSheshadri.Vasudevan@Sun.COM printf(E_LINE); 289210021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, 289310021SSheshadri.Vasudevan@Sun.COM "Extended partition already exists\n"); 289410021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Press enter to continue\n"); 289510021SSheshadri.Vasudevan@Sun.COM ext_read_input(s); 289610021SSheshadri.Vasudevan@Sun.COM continue; 289710021SSheshadri.Vasudevan@Sun.COM } 289810021SSheshadri.Vasudevan@Sun.COM #endif 28995169Slclee tsystid = EXTDOS; 29005169Slclee break; 29010Sstevel@tonic-gate case '8': 29025169Slclee tsystid = DOSHUGE; 29035169Slclee break; 29040Sstevel@tonic-gate case '9': 29055169Slclee tsystid = FDISK_FAT95; /* FAT16, need extended int13 */ 29065169Slclee break; 29070Sstevel@tonic-gate case 'a': /* x86 Boot partition */ 29080Sstevel@tonic-gate case 'A': 29095169Slclee tsystid = X86BOOT; 29105169Slclee break; 29110Sstevel@tonic-gate case 'b': /* Diagnostic boot partition */ 29120Sstevel@tonic-gate case 'B': 29135169Slclee tsystid = DIAGPART; 29145169Slclee break; 29150Sstevel@tonic-gate case 'c': /* FAT32 */ 29160Sstevel@tonic-gate case 'C': 29175169Slclee tsystid = FDISK_WINDOWS; 29185169Slclee break; 29190Sstevel@tonic-gate case 'd': /* FAT32 and need extended int13 */ 29200Sstevel@tonic-gate case 'D': 29215169Slclee tsystid = FDISK_EXT_WIN; 29225169Slclee break; 29230Sstevel@tonic-gate case 'e': /* Extended partition, need extended int13 */ 29240Sstevel@tonic-gate case 'E': 292510682SSheshadri.Vasudevan@Sun.COM #ifdef i386 292610682SSheshadri.Vasudevan@Sun.COM if (ext_part_present) { 292710682SSheshadri.Vasudevan@Sun.COM printf(Q_LINE); 292810682SSheshadri.Vasudevan@Sun.COM printf(E_LINE); 292910682SSheshadri.Vasudevan@Sun.COM fprintf(stderr, 293010682SSheshadri.Vasudevan@Sun.COM "Extended partition already exists\n"); 293110682SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Press enter to continue\n"); 293210682SSheshadri.Vasudevan@Sun.COM ext_read_input(s); 293310682SSheshadri.Vasudevan@Sun.COM continue; 293410682SSheshadri.Vasudevan@Sun.COM } 293510682SSheshadri.Vasudevan@Sun.COM #endif 29365169Slclee tsystid = FDISK_EXTLBA; 29375169Slclee break; 29380Sstevel@tonic-gate case 'f': 29390Sstevel@tonic-gate case 'F': 29405169Slclee tsystid = EFI_PMBR; 29415169Slclee break; 29420Sstevel@tonic-gate default: 29435169Slclee (void) printf(E_LINE); 29445169Slclee (void) printf("Invalid selection, try again."); 29455169Slclee continue; 29460Sstevel@tonic-gate } 29470Sstevel@tonic-gate } 29480Sstevel@tonic-gate 2949251Slclee (void) printf(E_LINE); 29500Sstevel@tonic-gate 29510Sstevel@tonic-gate if (tsystid != EFI_PMBR) { 29527563SPrasad.Singamsetty@Sun.COM (void) printf(W_LINE); 29537563SPrasad.Singamsetty@Sun.COM if ((dev_capacity > DK_MAX_2TB)) 29547563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger than 2 TB. " 29557563SPrasad.Singamsetty@Sun.COM "Upper limit is 2 TB for non-EFI partition ID\n"); 29567563SPrasad.Singamsetty@Sun.COM 29570Sstevel@tonic-gate /* create the new partition */ 29580Sstevel@tonic-gate i = specify(tsystid); 29590Sstevel@tonic-gate 29600Sstevel@tonic-gate if (i != -1) { 29610Sstevel@tonic-gate /* see if it should be the active partition */ 2962251Slclee (void) printf(E_LINE); 2963251Slclee (void) printf(Q_LINE); 2964251Slclee 2965251Slclee (void) printf( 2966251Slclee "Should this become the active partition? If " 2967251Slclee "yes, it will be activated\n" 2968251Slclee "each time the computer is reset or turned on.\n" 2969251Slclee "Please type \"y\" or \"n\". "); 29700Sstevel@tonic-gate 29710Sstevel@tonic-gate if (yesno()) { 2972251Slclee (void) printf(E_LINE); 29730Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 29740Sstevel@tonic-gate if (j == i) { 29750Sstevel@tonic-gate Table[j].bootid = ACTIVE; 2976251Slclee (void) printf(E_LINE); 2977251Slclee (void) printf( 2978251Slclee "Partition %d is now " 29790Sstevel@tonic-gate "the active partition.", 2980251Slclee j + 1); 29810Sstevel@tonic-gate } else { 29820Sstevel@tonic-gate Table[j].bootid = 0; 29830Sstevel@tonic-gate } 29840Sstevel@tonic-gate } 29850Sstevel@tonic-gate } else { 29860Sstevel@tonic-gate Table[i].bootid = 0; 29870Sstevel@tonic-gate } 29880Sstevel@tonic-gate 298910021SSheshadri.Vasudevan@Sun.COM #ifdef i386 299010021SSheshadri.Vasudevan@Sun.COM /* 299110021SSheshadri.Vasudevan@Sun.COM * If partition created is an extended partition, null 299210021SSheshadri.Vasudevan@Sun.COM * out the first sector of the first cylinder of the 299310021SSheshadri.Vasudevan@Sun.COM * extended partition 299410021SSheshadri.Vasudevan@Sun.COM */ 299510021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(Table[i].systid)) { 299610021SSheshadri.Vasudevan@Sun.COM fdisk_init_ext_part(epp, 299710021SSheshadri.Vasudevan@Sun.COM LE_32(Table[i].relsect), 299810021SSheshadri.Vasudevan@Sun.COM LE_32(Table[i].numsect)); 299910021SSheshadri.Vasudevan@Sun.COM } 300010021SSheshadri.Vasudevan@Sun.COM #endif 30010Sstevel@tonic-gate /* set up the return code */ 30020Sstevel@tonic-gate i = 1; 30030Sstevel@tonic-gate } 30040Sstevel@tonic-gate } else { 30050Sstevel@tonic-gate /* 30060Sstevel@tonic-gate * partitions of type EFI_PMBR must be the only partitions in 30070Sstevel@tonic-gate * the table 30080Sstevel@tonic-gate * 30090Sstevel@tonic-gate * First, make sure there were no errors the table is 30100Sstevel@tonic-gate * empty 30110Sstevel@tonic-gate */ 30120Sstevel@tonic-gate retCode = verify_tbl(); 30130Sstevel@tonic-gate 30140Sstevel@tonic-gate if (retCode < 0) { 3015251Slclee (void) fprintf(stderr, 30160Sstevel@tonic-gate "fdisk: Cannot create EFI partition table; \n" 30170Sstevel@tonic-gate "current partition table is invalid.\n"); 30180Sstevel@tonic-gate return (-1); 30190Sstevel@tonic-gate } else if (retCode > 0) { 3020251Slclee (void) printf( 3021251Slclee "An EFI partition must be the only partition on " 3022251Slclee "disk. You may manually delete existing\n" 3023251Slclee "partitions, or fdisk can do it.\n" 3024251Slclee "Do you want fdisk to destroy existing " 3025251Slclee "partitions?\n" 3026251Slclee "Please type \"y\" or \"n\". "); 30270Sstevel@tonic-gate 30280Sstevel@tonic-gate if (yesno()) { 30290Sstevel@tonic-gate nulltbl(); 30300Sstevel@tonic-gate } else { 30310Sstevel@tonic-gate return (-1); 30320Sstevel@tonic-gate } 30330Sstevel@tonic-gate } 30340Sstevel@tonic-gate 30350Sstevel@tonic-gate /* create the table entry - i should be 0 */ 30367563SPrasad.Singamsetty@Sun.COM i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1, 30377563SPrasad.Singamsetty@Sun.COM (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB: 3038*11382SShidokht.Yadegari@Sun.COM (dev_capacity - 1), 0); 30390Sstevel@tonic-gate 30400Sstevel@tonic-gate if (i != 0) { 3041251Slclee (void) printf("Error creating EFI partition!!!\n"); 30420Sstevel@tonic-gate i = -1; 30430Sstevel@tonic-gate } else { 30440Sstevel@tonic-gate 30450Sstevel@tonic-gate /* EFI partitions are currently never active */ 30460Sstevel@tonic-gate Table[i].bootid = 0; 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate /* set up the return code */ 30490Sstevel@tonic-gate i = 1; 30500Sstevel@tonic-gate } 30510Sstevel@tonic-gate } 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate return (i); 30540Sstevel@tonic-gate } 30550Sstevel@tonic-gate 30560Sstevel@tonic-gate /* 30570Sstevel@tonic-gate * specify 30580Sstevel@tonic-gate * Query the user to specify the size of the new partition in 30590Sstevel@tonic-gate * terms of percentage of the disk or by specifying the starting 30600Sstevel@tonic-gate * cylinder and length in cylinders. 30610Sstevel@tonic-gate */ 3062251Slclee static int 3063251Slclee specify(uchar_t tsystid) 30640Sstevel@tonic-gate { 30655169Slclee int i, j, percent = -1; 30667563SPrasad.Singamsetty@Sun.COM int cyl, cylen; 30677563SPrasad.Singamsetty@Sun.COM diskaddr_t first_free, size_free; 30687563SPrasad.Singamsetty@Sun.COM diskaddr_t max_free; 30695169Slclee int cyl_size; 30700Sstevel@tonic-gate struct ipart *partition[FD_NUMPART]; 3071*11382SShidokht.Yadegari@Sun.COM struct ipart localpart[FD_NUMPART]; 30720Sstevel@tonic-gate 30735169Slclee cyl_size = heads * sectors; 30745169Slclee 30755169Slclee /* 30765169Slclee * make a local copy of the partition table 30775169Slclee * and sort it into relsect order 30785169Slclee */ 3079*11382SShidokht.Yadegari@Sun.COM 3080*11382SShidokht.Yadegari@Sun.COM 3081*11382SShidokht.Yadegari@Sun.COM for (i = 0, j = 0; i < FD_NUMPART; i++) { 3082*11382SShidokht.Yadegari@Sun.COM if (Table[i].systid != UNUSED) { 3083*11382SShidokht.Yadegari@Sun.COM localpart[j] = Table[i]; 3084*11382SShidokht.Yadegari@Sun.COM j++; 3085*11382SShidokht.Yadegari@Sun.COM } 3086*11382SShidokht.Yadegari@Sun.COM } 3087*11382SShidokht.Yadegari@Sun.COM 3088*11382SShidokht.Yadegari@Sun.COM while (j < FD_NUMPART) { 3089*11382SShidokht.Yadegari@Sun.COM (void) memset(&localpart[j], 0, sizeof (struct ipart)); 3090*11382SShidokht.Yadegari@Sun.COM j++; 3091*11382SShidokht.Yadegari@Sun.COM } 3092*11382SShidokht.Yadegari@Sun.COM 30935169Slclee for (i = 0; i < FD_NUMPART; i++) 3094*11382SShidokht.Yadegari@Sun.COM partition[i] = &localpart[i]; 30955169Slclee 30965169Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 30975169Slclee if (partition[i]->systid == UNUSED) 30985169Slclee break; 30995169Slclee for (j = i + 1; j < FD_NUMPART; j++) { 31005169Slclee if (partition[j]->systid == UNUSED) 31015169Slclee break; 310210021SSheshadri.Vasudevan@Sun.COM if (LE_32(partition[j]->relsect) < 310310021SSheshadri.Vasudevan@Sun.COM LE_32(partition[i]->relsect)) { 31045169Slclee struct ipart *temp = partition[i]; 31055169Slclee partition[i] = partition[j]; 31065169Slclee partition[j] = temp; 31075169Slclee } 31085169Slclee } 31095169Slclee } 31105169Slclee 31117563SPrasad.Singamsetty@Sun.COM 3112251Slclee (void) printf(Q_LINE); 3113251Slclee (void) printf( 3114251Slclee "Specify the percentage of disk to use for this partition\n" 3115251Slclee "(or type \"c\" to specify the size in cylinders). "); 31169786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 31170Sstevel@tonic-gate rm_blanks(s); 31180Sstevel@tonic-gate if (s[0] != 'c') { /* Specify size in percentage of disk */ 31195169Slclee i = 0; 31209786SBarry.Harding@Sun.COM while ((s[i] != '\0') && (s[i] != '\n')) { 31215169Slclee if (s[i] < '0' || s[i] > '9') { 31225169Slclee (void) printf(E_LINE); 31235169Slclee (void) printf("Invalid percentage value " 31245169Slclee "specified; retry the operation."); 31255169Slclee return (-1); 31265169Slclee } 31275169Slclee i++; 31285169Slclee if (i > 3) { 31295169Slclee (void) printf(E_LINE); 31305169Slclee (void) printf("Invalid percentage value " 31315169Slclee "specified; retry the operation."); 31325169Slclee return (-1); 31335169Slclee } 31345169Slclee } 31355169Slclee if ((percent = atoi(s)) > 100) { 31365169Slclee (void) printf(E_LINE); 31375169Slclee (void) printf( 31385169Slclee "Percentage value is too large. The value must be" 31395169Slclee " between 1 and 100;\nretry the operation.\n"); 31405169Slclee return (-1); 31410Sstevel@tonic-gate } 31425169Slclee if (percent < 1) { 31435169Slclee (void) printf(E_LINE); 31445169Slclee (void) printf( 31455169Slclee "Percentage value is too small. The value must be" 31465169Slclee " between 1 and 100;\nretry the operation.\n"); 31475169Slclee return (-1); 31485169Slclee } 31495169Slclee 31505169Slclee if (percent == 100) 31517563SPrasad.Singamsetty@Sun.COM cylen = Numcyl_usable - 1; 31525169Slclee else 31537563SPrasad.Singamsetty@Sun.COM cylen = (Numcyl_usable * percent) / 100; 31545169Slclee 31555169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 31565169Slclee if ((tsystid == DOSOS12) && 31575169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 31585169Slclee int n; 31597563SPrasad.Singamsetty@Sun.COM n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable; 31605169Slclee (void) printf(E_LINE); 31615169Slclee (void) printf("Maximum size for a DOS partition " 31625169Slclee "is %d%%; retry the operation.", 31635169Slclee n <= 100 ? n : 100); 31645169Slclee return (-1); 31650Sstevel@tonic-gate } 31665169Slclee 31675169Slclee 31685169Slclee max_free = 0; 31695169Slclee for (i = 0; i < FD_NUMPART; i++) { 31705169Slclee 31715169Slclee /* 31725169Slclee * check for free space before partition i 31735169Slclee * where i varies from 0 to 3 31745169Slclee * 31755169Slclee * freespace after partition 3 is unusable 31765169Slclee * because there are no free partitions 31775169Slclee * 31785169Slclee * freespace begins at the end of previous partition 31795169Slclee * or cylinder 1 31805169Slclee */ 31815169Slclee if (i) { 31825169Slclee /* Not an empty table */ 318310021SSheshadri.Vasudevan@Sun.COM first_free = LE_32(partition[i - 1]->relsect) + 318410021SSheshadri.Vasudevan@Sun.COM LE_32(partition[i - 1]->numsect); 31855169Slclee } else { 31865169Slclee first_free = cyl_size; 31875169Slclee } 31885169Slclee 31895169Slclee /* 31905169Slclee * freespace ends before the current partition 31915169Slclee * or the end of the disk (chs end) 31925169Slclee */ 31935169Slclee if (partition[i]->systid == UNUSED) { 31945169Slclee size_free = chs_capacity - first_free; 31955169Slclee } else { 31968148SShidokht.Yadegari@Sun.COM /* 31978148SShidokht.Yadegari@Sun.COM * Partition might start before cylinder 1. 31988148SShidokht.Yadegari@Sun.COM * Make sure free space is not negative. 31998148SShidokht.Yadegari@Sun.COM */ 32005169Slclee size_free = 320110021SSheshadri.Vasudevan@Sun.COM (LE_32(partition[i]->relsect > first_free)) 320210021SSheshadri.Vasudevan@Sun.COM ? (LE_32(partition[i]->relsect) - 320310021SSheshadri.Vasudevan@Sun.COM first_free) : 0; 32045169Slclee } 32055169Slclee 32065169Slclee /* save largest free space */ 32075169Slclee if (max_free < size_free) 32085169Slclee max_free = size_free; 32095169Slclee 32107563SPrasad.Singamsetty@Sun.COM if (((uint64_t)cylen * cyl_size) <= size_free) { 32115169Slclee /* We found a place to use */ 32125169Slclee break; 32135169Slclee } 32145169Slclee if (partition[i]->systid == UNUSED) { 32155169Slclee (void) printf(E_LINE); 32165169Slclee max_free /= (cyl_size); 32175169Slclee (void) fprintf(stderr, "fdisk: " 32187563SPrasad.Singamsetty@Sun.COM "Maximum percentage available is %lld\n", 32197563SPrasad.Singamsetty@Sun.COM 100 * max_free / Numcyl_usable); 32205169Slclee return (-1); 32215169Slclee } 32220Sstevel@tonic-gate } 32235169Slclee 32245169Slclee (void) printf(E_LINE); 32255169Slclee if (i >= FD_NUMPART) { 32265169Slclee (void) fprintf(stderr, 32275169Slclee "fdisk: Partition table is full.\n"); 32285169Slclee return (-1); 32295169Slclee } 32305169Slclee 32315169Slclee if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 3232*11382SShidokht.Yadegari@Sun.COM first_free, cylen * cyl_size, 0)) >= 0) { 32335169Slclee return (i); 32345169Slclee } 32355169Slclee return (-1); 32365169Slclee } else { 32375169Slclee 32385169Slclee /* Specifying size in cylinders */ 3239251Slclee (void) printf(E_LINE); 32405169Slclee (void) printf(Q_LINE); 32415169Slclee (void) printf("Enter starting cylinder number: "); 32425169Slclee if ((cyl = getcyl()) == -1) { 32435169Slclee (void) printf(E_LINE); 32445169Slclee (void) printf("Invalid number; retry the operation."); 32455169Slclee return (-1); 32465169Slclee } 32475169Slclee if (cyl == 0) { 32485169Slclee (void) printf(E_LINE); 32495169Slclee (void) printf( 32505169Slclee "New partition cannot start at cylinder 0.\n"); 32515169Slclee return (-1); 32525169Slclee } 32537563SPrasad.Singamsetty@Sun.COM 32547563SPrasad.Singamsetty@Sun.COM 32557563SPrasad.Singamsetty@Sun.COM if (cyl >= Numcyl_usable) { 32565169Slclee (void) printf(E_LINE); 32575169Slclee (void) printf( 32585169Slclee "Cylinder %d is out of bounds, " 32595169Slclee "the maximum is %d.\n", 32607563SPrasad.Singamsetty@Sun.COM cyl, Numcyl_usable - 1); 32615169Slclee return (-1); 32625169Slclee } 32637563SPrasad.Singamsetty@Sun.COM 32645169Slclee (void) printf(Q_LINE); 32655169Slclee (void) printf("Enter partition size in cylinders: "); 32665169Slclee if ((cylen = getcyl()) == -1) { 32675169Slclee (void) printf(E_LINE); 32685169Slclee (void) printf("Invalid number, retry the operation."); 32695169Slclee return (-1); 32705169Slclee } 32715169Slclee 32725169Slclee for (i = 0; i < FD_NUMPART; i++) { 32735169Slclee uint32_t t_relsect, t_numsect; 32745169Slclee 32755169Slclee if (partition[i]->systid == UNUSED) 32765169Slclee break; 327710021SSheshadri.Vasudevan@Sun.COM t_relsect = LE_32(partition[i]->relsect); 327810021SSheshadri.Vasudevan@Sun.COM t_numsect = LE_32(partition[i]->numsect); 32795169Slclee 32805169Slclee if (cyl * cyl_size >= t_relsect && 32815169Slclee cyl * cyl_size < t_relsect + t_numsect) { 32825169Slclee (void) printf(E_LINE); 32835169Slclee (void) printf( 32845169Slclee "Cylinder %d is already allocated" 32855169Slclee "\nretry the operation.", 32865169Slclee cyl); 32875169Slclee return (-1); 32885169Slclee } 32895169Slclee 32905169Slclee if (cyl * cyl_size < t_relsect && 32915169Slclee (cyl + cylen - 1) * cyl_size > t_relsect) { 32925169Slclee (void) printf(E_LINE); 32935169Slclee (void) printf( 32945169Slclee "Maximum size for partition is %u cylinders" 32955169Slclee "\nretry the operation.", 32965169Slclee (t_relsect - cyl * cyl_size) / cyl_size); 32975169Slclee return (-1); 32985169Slclee } 32995169Slclee } 33005169Slclee 33017563SPrasad.Singamsetty@Sun.COM /* Verify partition doesn't exceed disk size or 2 TB */ 33027563SPrasad.Singamsetty@Sun.COM if (cyl + cylen > Numcyl_usable) { 33035169Slclee (void) printf(E_LINE); 33047563SPrasad.Singamsetty@Sun.COM if (Numcyl > Numcyl_usable) { 33057563SPrasad.Singamsetty@Sun.COM (void) printf( 33067563SPrasad.Singamsetty@Sun.COM "Maximum size for partition is %d " 33077563SPrasad.Singamsetty@Sun.COM "cylinders; \nretry the operation.", 33087563SPrasad.Singamsetty@Sun.COM Numcyl_usable - cyl); 33097563SPrasad.Singamsetty@Sun.COM } else { 33107563SPrasad.Singamsetty@Sun.COM (void) printf( 33117563SPrasad.Singamsetty@Sun.COM "Maximum size for partition is %d " 33127563SPrasad.Singamsetty@Sun.COM "cylinders; \nretry the operation.", 33137563SPrasad.Singamsetty@Sun.COM Numcyl_usable - cyl); 33147563SPrasad.Singamsetty@Sun.COM } 33155169Slclee return (-1); 33165169Slclee } 33175169Slclee 33185169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 33195169Slclee if ((tsystid == DOSOS12) && 33205169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 33215169Slclee (void) printf(E_LINE); 33225169Slclee (void) printf( 33235169Slclee "Maximum size for a %s partition is %ld cylinders;" 33245169Slclee "\nretry the operation.", 33255169Slclee Dstr, MAXDOS / (int)(cyl_size)); 33265169Slclee return (-1); 33275169Slclee } 33285169Slclee 3329251Slclee (void) printf(E_LINE); 33305169Slclee i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 3331*11382SShidokht.Yadegari@Sun.COM cyl * cyl_size, cylen * cyl_size, 0); 33325169Slclee if (i < 0) 33335169Slclee return (-1); 33345169Slclee 33355169Slclee if (verify_tbl() < 0) { 33365169Slclee (void) printf(E_LINE); 33375169Slclee (void) printf("fdisk: Cannot create partition table\n"); 33385169Slclee return (-1); 33395169Slclee } 33405169Slclee 33415169Slclee return (i); 33420Sstevel@tonic-gate } 33430Sstevel@tonic-gate } 33440Sstevel@tonic-gate 33450Sstevel@tonic-gate /* 33460Sstevel@tonic-gate * dispmenu 33470Sstevel@tonic-gate * Display command menu (interactive mode). 33480Sstevel@tonic-gate */ 3349251Slclee static void 3350251Slclee dispmenu(void) 33510Sstevel@tonic-gate { 3352251Slclee (void) printf(M_LINE); 335310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 335410021SSheshadri.Vasudevan@Sun.COM (void) printf( 335510021SSheshadri.Vasudevan@Sun.COM "SELECT ONE OF THE FOLLOWING:\n" 335610021SSheshadri.Vasudevan@Sun.COM " 1. Create a partition\n" 335710021SSheshadri.Vasudevan@Sun.COM " 2. Specify the active partition\n" 335810021SSheshadri.Vasudevan@Sun.COM " 3. Delete a partition\n" 335910021SSheshadri.Vasudevan@Sun.COM " 4. Change between Solaris and Solaris2 Partition IDs\n" 336010021SSheshadri.Vasudevan@Sun.COM " 5. Edit/View extended partitions\n" 336110021SSheshadri.Vasudevan@Sun.COM " 6. Exit (update disk configuration and exit)\n" 336210021SSheshadri.Vasudevan@Sun.COM " 7. Cancel (exit without updating disk configuration)\n"); 336310021SSheshadri.Vasudevan@Sun.COM #else 3364251Slclee (void) printf( 3365251Slclee "SELECT ONE OF THE FOLLOWING:\n" 3366251Slclee " 1. Create a partition\n" 3367251Slclee " 2. Specify the active partition\n" 3368251Slclee " 3. Delete a partition\n" 3369251Slclee " 4. Change between Solaris and Solaris2 Partition IDs\n" 3370251Slclee " 5. Exit (update disk configuration and exit)\n" 3371251Slclee " 6. Cancel (exit without updating disk configuration)\n"); 337210021SSheshadri.Vasudevan@Sun.COM #endif 33730Sstevel@tonic-gate } 33740Sstevel@tonic-gate 33750Sstevel@tonic-gate /* 33760Sstevel@tonic-gate * pchange 33770Sstevel@tonic-gate * Change the ACTIVE designation of a partition. 33780Sstevel@tonic-gate */ 3379251Slclee static int 3380251Slclee pchange(void) 33810Sstevel@tonic-gate { 33820Sstevel@tonic-gate char s[80]; 33830Sstevel@tonic-gate int i, j; 33840Sstevel@tonic-gate 3385251Slclee for (;;) { 3386251Slclee (void) printf(Q_LINE); 33870Sstevel@tonic-gate { 3388251Slclee (void) printf( 3389251Slclee "Specify the partition number to boot from" 33900Sstevel@tonic-gate " (or specify 0 for none): "); 33910Sstevel@tonic-gate } 33929786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 33930Sstevel@tonic-gate rm_blanks(s); 33949786SBarry.Harding@Sun.COM if (((s[1] != '\0') && (s[1] != '\n')) || 33959786SBarry.Harding@Sun.COM (s[0] < '0') || (s[0] > '4')) { 3396251Slclee (void) printf(E_LINE); 3397251Slclee (void) printf( 3398251Slclee "Invalid response, please specify a number" 33990Sstevel@tonic-gate " between 0 and 4.\n"); 34000Sstevel@tonic-gate } else { 34010Sstevel@tonic-gate break; 34020Sstevel@tonic-gate } 34030Sstevel@tonic-gate } 34040Sstevel@tonic-gate if (s[0] == '0') { /* No active partitions */ 34050Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 34060Sstevel@tonic-gate if (Table[i].systid != UNUSED && 34070Sstevel@tonic-gate Table[i].bootid == ACTIVE) 34080Sstevel@tonic-gate Table[i].bootid = 0; 34090Sstevel@tonic-gate } 3410251Slclee (void) printf(E_LINE); 3411251Slclee (void) printf( 3412251Slclee "No partition is currently marked as active."); 34130Sstevel@tonic-gate return (0); 34140Sstevel@tonic-gate } else { /* User has selected a partition to be active */ 3415*11382SShidokht.Yadegari@Sun.COM 34160Sstevel@tonic-gate i = s[0] - '1'; 3417*11382SShidokht.Yadegari@Sun.COM 34180Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 3419251Slclee (void) printf(E_LINE); 3420251Slclee (void) printf("Partition does not exist."); 34210Sstevel@tonic-gate return (-1); 34220Sstevel@tonic-gate } 34230Sstevel@tonic-gate /* a DOS-DATA or EXT-DOS partition cannot be active */ 34240Sstevel@tonic-gate else if ((Table[i].systid == DOSDATA) || 34250Sstevel@tonic-gate (Table[i].systid == EXTDOS) || 34260Sstevel@tonic-gate (Table[i].systid == FDISK_EXTLBA)) { 3427251Slclee (void) printf(E_LINE); 3428251Slclee (void) printf( 3429251Slclee "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions " 34300Sstevel@tonic-gate "cannot be made active.\n"); 3431251Slclee (void) printf("Select another partition."); 34320Sstevel@tonic-gate return (-1); 34330Sstevel@tonic-gate } 34340Sstevel@tonic-gate Table[i].bootid = ACTIVE; 34350Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 34360Sstevel@tonic-gate if (j != i) 34370Sstevel@tonic-gate Table[j].bootid = 0; 34380Sstevel@tonic-gate } 34390Sstevel@tonic-gate } 3440251Slclee (void) printf(E_LINE); 34410Sstevel@tonic-gate { 3442251Slclee (void) printf( 3443251Slclee "Partition %d is now active. The system will start up" 3444251Slclee " from this\n", i + 1); 3445251Slclee (void) printf("partition after the next reboot."); 34460Sstevel@tonic-gate } 34470Sstevel@tonic-gate return (1); 34480Sstevel@tonic-gate } 34490Sstevel@tonic-gate 34500Sstevel@tonic-gate /* 34510Sstevel@tonic-gate * Change between SOLARIS and SOLARIS2 partition id 34520Sstevel@tonic-gate */ 3453251Slclee static int 3454251Slclee ppartid(void) 34550Sstevel@tonic-gate { 34560Sstevel@tonic-gate char *p, s[80]; 34570Sstevel@tonic-gate int i; 34580Sstevel@tonic-gate 34590Sstevel@tonic-gate for (;;) { 3460251Slclee (void) printf(Q_LINE); 3461251Slclee (void) printf("Specify the partition number to change" 34625169Slclee " (or enter 0 to exit): "); 3463251Slclee if (!fgets(s, sizeof (s), stdin)) 3464251Slclee return (1); 34650Sstevel@tonic-gate i = strtol(s, &p, 10); 34660Sstevel@tonic-gate 34670Sstevel@tonic-gate if (*p != '\n' || i < 0 || i > FD_NUMPART) { 3468251Slclee (void) printf(E_LINE); 3469251Slclee (void) printf( 3470251Slclee "Invalid response, retry the operation.\n"); 34710Sstevel@tonic-gate continue; 34720Sstevel@tonic-gate } 34730Sstevel@tonic-gate 34740Sstevel@tonic-gate if (i == 0) { 34750Sstevel@tonic-gate /* exit delete command */ 3476251Slclee (void) printf(E_LINE); /* clear error message */ 34770Sstevel@tonic-gate return (1); 34780Sstevel@tonic-gate } 34790Sstevel@tonic-gate 34800Sstevel@tonic-gate i -= 1; 3481*11382SShidokht.Yadegari@Sun.COM 34820Sstevel@tonic-gate if (Table[i].systid == SUNIXOS) { 34830Sstevel@tonic-gate Table[i].systid = SUNIXOS2; 34840Sstevel@tonic-gate } else if (Table[i].systid == SUNIXOS2) { 34850Sstevel@tonic-gate Table[i].systid = SUNIXOS; 34860Sstevel@tonic-gate } else { 3487251Slclee (void) printf(E_LINE); 3488251Slclee (void) printf( 3489251Slclee "Partition %d is not a Solaris partition.", 34900Sstevel@tonic-gate i + 1); 34910Sstevel@tonic-gate continue; 34920Sstevel@tonic-gate } 34930Sstevel@tonic-gate 3494251Slclee (void) printf(E_LINE); 3495251Slclee (void) printf("Partition %d has been changed.", i + 1); 34960Sstevel@tonic-gate return (1); 34970Sstevel@tonic-gate } 34980Sstevel@tonic-gate } 34990Sstevel@tonic-gate 35000Sstevel@tonic-gate /* 35010Sstevel@tonic-gate * pdelete 35020Sstevel@tonic-gate * Remove partition entry from the table (interactive mode). 35030Sstevel@tonic-gate */ 3504251Slclee static char 3505251Slclee pdelete(void) 35060Sstevel@tonic-gate { 35070Sstevel@tonic-gate char s[80]; 3508*11382SShidokht.Yadegari@Sun.COM int i; 35090Sstevel@tonic-gate char pactive; 35100Sstevel@tonic-gate 3511251Slclee DEL1: (void) printf(Q_LINE); 3512251Slclee (void) printf("Specify the partition number to delete" 35130Sstevel@tonic-gate " (or enter 0 to exit): "); 35149786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 35150Sstevel@tonic-gate rm_blanks(s); 35160Sstevel@tonic-gate if ((s[0] == '0')) { /* exit delete command */ 3517251Slclee (void) printf(E_LINE); /* clear error message */ 35180Sstevel@tonic-gate return (1); 35190Sstevel@tonic-gate } 35200Sstevel@tonic-gate /* Accept only a single digit between 1 and 4 */ 35219786SBarry.Harding@Sun.COM if (((s[1] != '\0') && (s[1] != '\n')) || 35229786SBarry.Harding@Sun.COM (i = atoi(s)) < 1 || i > FD_NUMPART) { 3523251Slclee (void) printf(E_LINE); 3524251Slclee (void) printf("Invalid response, retry the operation.\n"); 35250Sstevel@tonic-gate goto DEL1; 35260Sstevel@tonic-gate } else { /* Found a digit between 1 and 4 */ 35270Sstevel@tonic-gate --i; /* Structure begins with element 0 */ 35280Sstevel@tonic-gate } 35290Sstevel@tonic-gate 35300Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 3531251Slclee (void) printf(E_LINE); 3532251Slclee (void) printf("Partition %d does not exist.", i + 1); 35330Sstevel@tonic-gate return (-1); 35340Sstevel@tonic-gate } 35350Sstevel@tonic-gate 353610021SSheshadri.Vasudevan@Sun.COM #ifdef i386 353710021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(Table[i].systid) && 353810021SSheshadri.Vasudevan@Sun.COM (Table[i].relsect == fdisk_get_ext_beg_sec(epp)) && 353910021SSheshadri.Vasudevan@Sun.COM fdisk_get_logical_drive_count(epp)) { 354010021SSheshadri.Vasudevan@Sun.COM (void) printf(Q_LINE); 354110021SSheshadri.Vasudevan@Sun.COM (void) printf("There are logical drives inside the" 354210021SSheshadri.Vasudevan@Sun.COM " extended partition\n"); 354310021SSheshadri.Vasudevan@Sun.COM (void) printf("Are you sure of proceeding with deletion ?" 354410021SSheshadri.Vasudevan@Sun.COM " (type \"y\" or \"n\") "); 354510021SSheshadri.Vasudevan@Sun.COM 354610021SSheshadri.Vasudevan@Sun.COM (void) printf(E_LINE); 354710021SSheshadri.Vasudevan@Sun.COM if (! yesno()) { 354810021SSheshadri.Vasudevan@Sun.COM return (1); 354910021SSheshadri.Vasudevan@Sun.COM } 355010021SSheshadri.Vasudevan@Sun.COM if (fdisk_mounted_logical_drives(epp) == FDISK_EMOUNTED) { 355110021SSheshadri.Vasudevan@Sun.COM (void) printf(Q_LINE); 355210021SSheshadri.Vasudevan@Sun.COM (void) printf("There are mounted logical drives. " 355310021SSheshadri.Vasudevan@Sun.COM "Committing changes now can cause data loss or " 355410021SSheshadri.Vasudevan@Sun.COM "corruption. Unmount all logical drives and then " 355510021SSheshadri.Vasudevan@Sun.COM "try committing the changes again.\n"); 355610021SSheshadri.Vasudevan@Sun.COM (void) printf("Press enter to continue.\n"); 355710021SSheshadri.Vasudevan@Sun.COM ext_read_input(s); 355810021SSheshadri.Vasudevan@Sun.COM return (1); 355910021SSheshadri.Vasudevan@Sun.COM } 356010021SSheshadri.Vasudevan@Sun.COM fdisk_delete_ext_part(epp); 356110021SSheshadri.Vasudevan@Sun.COM } else { 356210021SSheshadri.Vasudevan@Sun.COM #endif 356310021SSheshadri.Vasudevan@Sun.COM (void) printf(Q_LINE); 356410021SSheshadri.Vasudevan@Sun.COM (void) printf("Are you sure you want to delete partition %d?" 356510021SSheshadri.Vasudevan@Sun.COM " This will make all files and \n", i + 1); 356610021SSheshadri.Vasudevan@Sun.COM (void) printf("programs in this partition inaccessible (type" 356710021SSheshadri.Vasudevan@Sun.COM " \"y\" or \"n\"). "); 356810021SSheshadri.Vasudevan@Sun.COM 356910021SSheshadri.Vasudevan@Sun.COM (void) printf(E_LINE); 357010021SSheshadri.Vasudevan@Sun.COM if (! yesno()) { 357110021SSheshadri.Vasudevan@Sun.COM return (1); 357210021SSheshadri.Vasudevan@Sun.COM } 357310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 35740Sstevel@tonic-gate } 357510021SSheshadri.Vasudevan@Sun.COM #endif 35760Sstevel@tonic-gate 35770Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) { 35780Sstevel@tonic-gate pactive = 1; 35790Sstevel@tonic-gate } else { 35800Sstevel@tonic-gate pactive = 0; 35810Sstevel@tonic-gate } 35820Sstevel@tonic-gate 3583*11382SShidokht.Yadegari@Sun.COM (void) memset(&Table[i], 0, sizeof (struct ipart)); 3584*11382SShidokht.Yadegari@Sun.COM 3585251Slclee (void) printf(E_LINE); 3586251Slclee (void) printf("Partition %d has been deleted.", i + 1); 35870Sstevel@tonic-gate 35880Sstevel@tonic-gate if (pactive) { 35895169Slclee (void) printf(" This was the active partition."); 35900Sstevel@tonic-gate } 35910Sstevel@tonic-gate 35920Sstevel@tonic-gate return (1); 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate 35950Sstevel@tonic-gate /* 35960Sstevel@tonic-gate * rm_blanks 35970Sstevel@tonic-gate * Remove blanks from strings of user responses. 35980Sstevel@tonic-gate */ 3599251Slclee static void 3600251Slclee rm_blanks(char *s) 36010Sstevel@tonic-gate { 36020Sstevel@tonic-gate register int i, j; 36030Sstevel@tonic-gate 36040Sstevel@tonic-gate for (i = 0; i < CBUFLEN; i++) { 36050Sstevel@tonic-gate if ((s[i] == ' ') || (s[i] == '\t')) 36060Sstevel@tonic-gate continue; 36070Sstevel@tonic-gate else 36080Sstevel@tonic-gate /* Found first non-blank character of the string */ 36090Sstevel@tonic-gate break; 36100Sstevel@tonic-gate } 36110Sstevel@tonic-gate for (j = 0; i < CBUFLEN; j++, i++) { 36120Sstevel@tonic-gate if ((s[j] = s[i]) == '\0') { 36130Sstevel@tonic-gate /* Reached end of string */ 36140Sstevel@tonic-gate return; 36150Sstevel@tonic-gate } 36160Sstevel@tonic-gate } 36170Sstevel@tonic-gate } 36180Sstevel@tonic-gate 36190Sstevel@tonic-gate /* 36200Sstevel@tonic-gate * getcyl 36210Sstevel@tonic-gate * Take the user-specified cylinder number and convert it from a 36220Sstevel@tonic-gate * string to a decimal value. 36230Sstevel@tonic-gate */ 3624251Slclee static int 3625251Slclee getcyl(void) 36260Sstevel@tonic-gate { 36270Sstevel@tonic-gate int slen, i, j; 36280Sstevel@tonic-gate unsigned int cyl; 36299786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 36300Sstevel@tonic-gate rm_blanks(s); 36310Sstevel@tonic-gate slen = strlen(s); 36329786SBarry.Harding@Sun.COM if (s[slen - 1] == '\n') 36339786SBarry.Harding@Sun.COM slen--; 36340Sstevel@tonic-gate j = 1; 36350Sstevel@tonic-gate cyl = 0; 3636251Slclee for (i = slen - 1; i >= 0; i--) { 36370Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 36380Sstevel@tonic-gate return (-1); 36390Sstevel@tonic-gate } 3640251Slclee cyl += (j * (s[i] - '0')); 36410Sstevel@tonic-gate j *= 10; 36420Sstevel@tonic-gate } 36430Sstevel@tonic-gate return (cyl); 36440Sstevel@tonic-gate } 36450Sstevel@tonic-gate 36460Sstevel@tonic-gate /* 36470Sstevel@tonic-gate * disptbl 36480Sstevel@tonic-gate * Display the current fdisk table; determine percentage 36490Sstevel@tonic-gate * of the disk used for each partition. 36500Sstevel@tonic-gate */ 3651251Slclee static void 3652251Slclee disptbl(void) 36530Sstevel@tonic-gate { 36540Sstevel@tonic-gate int i; 36550Sstevel@tonic-gate unsigned int startcyl, endcyl, length, percent, remainder; 36560Sstevel@tonic-gate char *stat, *type; 36577563SPrasad.Singamsetty@Sun.COM int is_pmbr = 0; 36580Sstevel@tonic-gate 36590Sstevel@tonic-gate if ((heads == 0) || (sectors == 0)) { 3660251Slclee (void) printf("WARNING: critical disk geometry information" 36615169Slclee " missing!\n"); 3662251Slclee (void) printf("\theads = %d, sectors = %d\n", heads, sectors); 36630Sstevel@tonic-gate exit(1); 36640Sstevel@tonic-gate } 36650Sstevel@tonic-gate 3666251Slclee (void) printf(HOME); 3667251Slclee (void) printf(T_LINE); 3668251Slclee (void) printf(" Total disk size is %d cylinders\n", Numcyl); 36699889SLarry.Liu@Sun.COM (void) printf(" Cylinder size is %d (%d byte) blocks\n\n", 36709889SLarry.Liu@Sun.COM heads * sectors, sectsiz); 3671251Slclee (void) printf( 3672251Slclee " Cylinders\n"); 3673251Slclee (void) printf( 3674251Slclee " Partition Status Type Start End Length" 36750Sstevel@tonic-gate " %%\n"); 3676251Slclee (void) printf( 3677251Slclee " ========= ====== ============ ===== === ======" 36780Sstevel@tonic-gate " ==="); 3679*11382SShidokht.Yadegari@Sun.COM 3680*11382SShidokht.Yadegari@Sun.COM 36810Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3682*11382SShidokht.Yadegari@Sun.COM 36830Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 36840Sstevel@tonic-gate continue; 36850Sstevel@tonic-gate } 3686*11382SShidokht.Yadegari@Sun.COM 36870Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) 36885169Slclee stat = Actvstr; 36890Sstevel@tonic-gate else 36905169Slclee stat = NAstr; 36910Sstevel@tonic-gate switch (Table[i].systid) { 36920Sstevel@tonic-gate case UNIXOS: 36935169Slclee type = Ustr; 36945169Slclee break; 36950Sstevel@tonic-gate case SUNIXOS: 36965169Slclee type = SUstr; 369710682SSheshadri.Vasudevan@Sun.COM #ifdef i386 369810682SSheshadri.Vasudevan@Sun.COM if (fdisk_is_linux_swap(epp, Table[i].relsect, 369910682SSheshadri.Vasudevan@Sun.COM NULL) == 0) 370010682SSheshadri.Vasudevan@Sun.COM type = LINSWAPstr; 370110682SSheshadri.Vasudevan@Sun.COM #endif 37025169Slclee break; 37030Sstevel@tonic-gate case SUNIXOS2: 37045169Slclee type = SU2str; 37055169Slclee break; 37060Sstevel@tonic-gate case X86BOOT: 37075169Slclee type = X86str; 37085169Slclee break; 37090Sstevel@tonic-gate case DOSOS12: 37105169Slclee type = Dstr; 37115169Slclee break; 37120Sstevel@tonic-gate case DOSOS16: 37135169Slclee type = D16str; 37145169Slclee break; 37150Sstevel@tonic-gate case EXTDOS: 37165169Slclee type = EDstr; 37175169Slclee break; 37180Sstevel@tonic-gate case DOSDATA: 37195169Slclee type = DDstr; 37205169Slclee break; 37210Sstevel@tonic-gate case DOSHUGE: 37225169Slclee type = DBstr; 37235169Slclee break; 37240Sstevel@tonic-gate case PCIXOS: 37255169Slclee type = PCstr; 37265169Slclee break; 37270Sstevel@tonic-gate case DIAGPART: 37285169Slclee type = DIAGstr; 37295169Slclee break; 37300Sstevel@tonic-gate case FDISK_IFS: 37315169Slclee type = IFSstr; 37325169Slclee break; 37330Sstevel@tonic-gate case FDISK_AIXBOOT: 37345169Slclee type = AIXstr; 37355169Slclee break; 37360Sstevel@tonic-gate case FDISK_AIXDATA: 37375169Slclee type = AIXDstr; 37385169Slclee break; 37390Sstevel@tonic-gate case FDISK_OS2BOOT: 37405169Slclee type = OS2str; 37415169Slclee break; 37420Sstevel@tonic-gate case FDISK_WINDOWS: 37435169Slclee type = WINstr; 37445169Slclee break; 37450Sstevel@tonic-gate case FDISK_EXT_WIN: 37465169Slclee type = EWINstr; 37475169Slclee break; 37480Sstevel@tonic-gate case FDISK_FAT95: 37495169Slclee type = FAT95str; 37505169Slclee break; 37510Sstevel@tonic-gate case FDISK_EXTLBA: 37525169Slclee type = EXTLstr; 37535169Slclee break; 37540Sstevel@tonic-gate case FDISK_LINUX: 37555169Slclee type = LINUXstr; 37565169Slclee break; 37570Sstevel@tonic-gate case FDISK_CPM: 37585169Slclee type = CPMstr; 37595169Slclee break; 376010021SSheshadri.Vasudevan@Sun.COM case FDISK_NOVELL2: 376110021SSheshadri.Vasudevan@Sun.COM type = NOV2str; 376210021SSheshadri.Vasudevan@Sun.COM break; 37630Sstevel@tonic-gate case FDISK_NOVELL3: 37645169Slclee type = NOVstr; 37655169Slclee break; 37660Sstevel@tonic-gate case FDISK_QNX4: 37675169Slclee type = QNXstr; 37685169Slclee break; 37690Sstevel@tonic-gate case FDISK_QNX42: 37705169Slclee type = QNX2str; 37715169Slclee break; 37720Sstevel@tonic-gate case FDISK_QNX43: 37735169Slclee type = QNX3str; 37745169Slclee break; 37750Sstevel@tonic-gate case FDISK_LINUXNAT: 37765169Slclee type = LINNATstr; 37775169Slclee break; 37780Sstevel@tonic-gate case FDISK_NTFSVOL1: 37795169Slclee type = NTFSVOL1str; 37805169Slclee break; 37810Sstevel@tonic-gate case FDISK_NTFSVOL2: 37825169Slclee type = NTFSVOL2str; 37835169Slclee break; 37840Sstevel@tonic-gate case FDISK_BSD: 37855169Slclee type = BSDstr; 37865169Slclee break; 37870Sstevel@tonic-gate case FDISK_NEXTSTEP: 37885169Slclee type = NEXTSTEPstr; 37895169Slclee break; 37900Sstevel@tonic-gate case FDISK_BSDIFS: 37915169Slclee type = BSDIFSstr; 37925169Slclee break; 37930Sstevel@tonic-gate case FDISK_BSDISWAP: 37945169Slclee type = BSDISWAPstr; 37955169Slclee break; 37960Sstevel@tonic-gate case EFI_PMBR: 37975169Slclee type = EFIstr; 379810021SSheshadri.Vasudevan@Sun.COM if (LE_32(Table[i].numsect) == DK_MAX_2TB) 37997563SPrasad.Singamsetty@Sun.COM is_pmbr = 1; 38007563SPrasad.Singamsetty@Sun.COM 38015169Slclee break; 38020Sstevel@tonic-gate default: 38035169Slclee type = Ostr; 38045169Slclee break; 38050Sstevel@tonic-gate } 380610021SSheshadri.Vasudevan@Sun.COM startcyl = LE_32(Table[i].relsect) / 38075936Sbharding (unsigned long)(heads * sectors); 38087563SPrasad.Singamsetty@Sun.COM 380910021SSheshadri.Vasudevan@Sun.COM if (LE_32(Table[i].numsect) == DK_MAX_2TB) { 38107563SPrasad.Singamsetty@Sun.COM endcyl = Numcyl - 1; 38117563SPrasad.Singamsetty@Sun.COM length = endcyl - startcyl + 1; 38127563SPrasad.Singamsetty@Sun.COM } else { 381310021SSheshadri.Vasudevan@Sun.COM length = LE_32(Table[i].numsect) / 38147563SPrasad.Singamsetty@Sun.COM (unsigned long)(heads * sectors); 381510021SSheshadri.Vasudevan@Sun.COM if (LE_32(Table[i].numsect) % 38167563SPrasad.Singamsetty@Sun.COM (unsigned long)(heads * sectors)) 38177563SPrasad.Singamsetty@Sun.COM length++; 38187563SPrasad.Singamsetty@Sun.COM endcyl = startcyl + length - 1; 38197563SPrasad.Singamsetty@Sun.COM } 38207563SPrasad.Singamsetty@Sun.COM 38217563SPrasad.Singamsetty@Sun.COM percent = length * 100 / Numcyl_usable; 38227563SPrasad.Singamsetty@Sun.COM if ((remainder = (length * 100 % Numcyl_usable)) != 0) { 38237563SPrasad.Singamsetty@Sun.COM if ((remainder * 100 / Numcyl_usable) > 50) { 38240Sstevel@tonic-gate /* round up */ 38250Sstevel@tonic-gate percent++; 38260Sstevel@tonic-gate } 38270Sstevel@tonic-gate /* Else leave the percent as is since it's already */ 38280Sstevel@tonic-gate /* rounded down */ 38290Sstevel@tonic-gate } 38300Sstevel@tonic-gate if (percent > 100) 38310Sstevel@tonic-gate percent = 100; 3832251Slclee (void) printf( 3833251Slclee "\n %d %s %-12.12s %4d %4d %4d" 3834251Slclee " %3d", 3835251Slclee i + 1, stat, type, startcyl, endcyl, length, percent); 38360Sstevel@tonic-gate } 38377563SPrasad.Singamsetty@Sun.COM 38380Sstevel@tonic-gate /* Print warning message if table is empty */ 3839*11382SShidokht.Yadegari@Sun.COM if (nopartdefined()) { 3840251Slclee (void) printf(W_LINE); 3841251Slclee (void) printf("WARNING: no partitions are defined!"); 38420Sstevel@tonic-gate } else { 38430Sstevel@tonic-gate /* Clear the warning line */ 3844251Slclee (void) printf(W_LINE); 38457563SPrasad.Singamsetty@Sun.COM 38467563SPrasad.Singamsetty@Sun.COM /* Print warning if disk > 2TB and is not EFI PMBR */ 38477563SPrasad.Singamsetty@Sun.COM if (!is_pmbr && (dev_capacity > DK_MAX_2TB)) 38487563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger than 2 TB. " 38497563SPrasad.Singamsetty@Sun.COM "Upper limit is 2 TB for non-EFI partition ID\n"); 38500Sstevel@tonic-gate } 38510Sstevel@tonic-gate } 38520Sstevel@tonic-gate 38530Sstevel@tonic-gate /* 38540Sstevel@tonic-gate * print_Table 38550Sstevel@tonic-gate * Write the detailed fdisk table to standard error for 38560Sstevel@tonic-gate * the selected disk device. 38570Sstevel@tonic-gate */ 3858251Slclee static void 3859251Slclee print_Table(void) 3860251Slclee { 38610Sstevel@tonic-gate int i; 38620Sstevel@tonic-gate 3863251Slclee (void) fprintf(stderr, 38640Sstevel@tonic-gate " SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT" 38650Sstevel@tonic-gate " NUMSECT\n"); 38660Sstevel@tonic-gate 38670Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 3868251Slclee (void) fprintf(stderr, " %-5d ", Table[i].systid); 3869251Slclee (void) fprintf(stderr, "%-3d ", Table[i].bootid); 3870251Slclee (void) fprintf(stderr, "%-5d ", Table[i].beghead); 3871251Slclee (void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f); 38725169Slclee (void) fprintf(stderr, "%-8d ", 38735169Slclee (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl); 38740Sstevel@tonic-gate 3875251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endhead); 3876251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f); 38775169Slclee (void) fprintf(stderr, "%-8d ", 38785169Slclee (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl); 387910021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, "%-10u ", LE_32(Table[i].relsect)); 388010021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, "%-10u\n", LE_32(Table[i].numsect)); 38810Sstevel@tonic-gate 38820Sstevel@tonic-gate } 38830Sstevel@tonic-gate } 38840Sstevel@tonic-gate 38850Sstevel@tonic-gate /* 38860Sstevel@tonic-gate * copy_Table_to_Old_Table 38870Sstevel@tonic-gate * Copy Table into Old_Table. The function only copies the systid, 38880Sstevel@tonic-gate * numsect, relsect, and bootid values because they are the only 38890Sstevel@tonic-gate * ones compared when determining if Table has changed. 38900Sstevel@tonic-gate */ 3891251Slclee static void 3892251Slclee copy_Table_to_Old_Table(void) 38930Sstevel@tonic-gate { 38940Sstevel@tonic-gate int i; 38950Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 38965169Slclee (void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0])); 38970Sstevel@tonic-gate } 38980Sstevel@tonic-gate } 38990Sstevel@tonic-gate 39000Sstevel@tonic-gate /* 39010Sstevel@tonic-gate * nulltbl 39020Sstevel@tonic-gate * Zero out the systid, numsect, relsect, and bootid values in the 39030Sstevel@tonic-gate * fdisk table. 39040Sstevel@tonic-gate */ 3905251Slclee static void 3906251Slclee nulltbl(void) 39070Sstevel@tonic-gate { 39080Sstevel@tonic-gate int i; 39090Sstevel@tonic-gate 39100Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 39115169Slclee Table[i].systid = UNUSED; 391210021SSheshadri.Vasudevan@Sun.COM Table[i].numsect = LE_32(UNUSED); 391310021SSheshadri.Vasudevan@Sun.COM Table[i].relsect = LE_32(UNUSED); 39145169Slclee Table[i].bootid = 0; 39158904SBarry.Harding@Sun.COM skip_verify[i] = 0; 39160Sstevel@tonic-gate } 39170Sstevel@tonic-gate } 39180Sstevel@tonic-gate 39190Sstevel@tonic-gate /* 39200Sstevel@tonic-gate * copy_Bootblk_to_Table 39210Sstevel@tonic-gate * Copy the bytes from the boot record to an internal "Table". 39220Sstevel@tonic-gate * All unused are padded with zeros starting at offset 446. 39230Sstevel@tonic-gate */ 3924251Slclee static void 3925251Slclee copy_Bootblk_to_Table(void) 39260Sstevel@tonic-gate { 3927*11382SShidokht.Yadegari@Sun.COM int i; 39280Sstevel@tonic-gate char *bootptr; 39290Sstevel@tonic-gate struct ipart iparts[FD_NUMPART]; 39300Sstevel@tonic-gate 39310Sstevel@tonic-gate /* Get an aligned copy of the partition tables */ 3932251Slclee (void) memcpy(iparts, Bootblk->parts, sizeof (iparts)); 39330Sstevel@tonic-gate bootptr = (char *)iparts; /* Points to start of partition table */ 393410021SSheshadri.Vasudevan@Sun.COM if (LE_16(Bootblk->signature) != MBB_MAGIC) { 39350Sstevel@tonic-gate /* Signature is missing */ 39360Sstevel@tonic-gate nulltbl(); 3937251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 39380Sstevel@tonic-gate return; 39390Sstevel@tonic-gate } 39400Sstevel@tonic-gate /* 39410Sstevel@tonic-gate * When the DOS fdisk command deletes a partition, it is not 39420Sstevel@tonic-gate * recognized by the old algorithm. The algorithm that 39430Sstevel@tonic-gate * follows looks at each entry in the Bootrec and copies all 39440Sstevel@tonic-gate * those that are valid. 39450Sstevel@tonic-gate */ 39460Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 39470Sstevel@tonic-gate if (iparts[i].systid == 0) { 39480Sstevel@tonic-gate /* Null entry */ 3949*11382SShidokht.Yadegari@Sun.COM (void) memset(&Table[i], 0, sizeof (struct ipart)); 39500Sstevel@tonic-gate } else { 3951*11382SShidokht.Yadegari@Sun.COM fill_ipart(bootptr, &Table[i]); 39520Sstevel@tonic-gate } 3953*11382SShidokht.Yadegari@Sun.COM bootptr += sizeof (struct ipart); 39540Sstevel@tonic-gate } 3955*11382SShidokht.Yadegari@Sun.COM 39560Sstevel@tonic-gate /* For now, always replace the bootcode with ours */ 3957251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 39580Sstevel@tonic-gate copy_Table_to_Bootblk(); 39590Sstevel@tonic-gate } 39600Sstevel@tonic-gate 39610Sstevel@tonic-gate /* 39620Sstevel@tonic-gate * fill_ipart 39630Sstevel@tonic-gate * Initialize ipart structure values. 39640Sstevel@tonic-gate */ 3965251Slclee static void 39660Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp) 39670Sstevel@tonic-gate { 39680Sstevel@tonic-gate #ifdef sparc 39690Sstevel@tonic-gate /* Packing struct ipart for Sparc */ 3970251Slclee partp->bootid = getbyte(&bootptr); 3971251Slclee partp->beghead = getbyte(&bootptr); 3972251Slclee partp->begsect = getbyte(&bootptr); 3973251Slclee partp->begcyl = getbyte(&bootptr); 3974251Slclee partp->systid = getbyte(&bootptr); 3975251Slclee partp->endhead = getbyte(&bootptr); 3976251Slclee partp->endsect = getbyte(&bootptr); 3977251Slclee partp->endcyl = getbyte(&bootptr); 3978251Slclee partp->relsect = (int32_t)getlong(&bootptr); 3979251Slclee partp->numsect = (int32_t)getlong(&bootptr); 39800Sstevel@tonic-gate #else 39810Sstevel@tonic-gate *partp = *(struct ipart *)bootptr; 39820Sstevel@tonic-gate #endif 39830Sstevel@tonic-gate } 39840Sstevel@tonic-gate 39850Sstevel@tonic-gate /* 3986251Slclee * getbyte, getlong 39870Sstevel@tonic-gate * Get a byte, a short, or a long (SPARC only). 39880Sstevel@tonic-gate */ 39890Sstevel@tonic-gate #ifdef sparc 3990251Slclee uchar_t 3991251Slclee getbyte(char **bp) 39920Sstevel@tonic-gate { 3993251Slclee uchar_t b; 3994251Slclee 3995251Slclee b = (uchar_t)**bp; 39960Sstevel@tonic-gate *bp = *bp + 1; 39970Sstevel@tonic-gate return (b); 39980Sstevel@tonic-gate } 39990Sstevel@tonic-gate 4000251Slclee uint32_t 4001251Slclee getlong(char **bp) 40020Sstevel@tonic-gate { 4003251Slclee int32_t b, bh, bl; 40040Sstevel@tonic-gate 40050Sstevel@tonic-gate bh = ((**bp) << 8) | *(*bp + 1); 40060Sstevel@tonic-gate *bp += 2; 40070Sstevel@tonic-gate bl = ((**bp) << 8) | *(*bp + 1); 40080Sstevel@tonic-gate *bp += 2; 40090Sstevel@tonic-gate 40100Sstevel@tonic-gate b = (bh << 16) | bl; 4011251Slclee return ((uint32_t)b); 40120Sstevel@tonic-gate } 40130Sstevel@tonic-gate #endif 40140Sstevel@tonic-gate 40150Sstevel@tonic-gate /* 40160Sstevel@tonic-gate * copy_Table_to_Bootblk 40179889SLarry.Liu@Sun.COM * Copy the table into the boot record. Note that the unused 40180Sstevel@tonic-gate * entries will always be the last ones in the table and they are 40190Sstevel@tonic-gate * marked with 100 in sysind. The the unused portion of the table 40200Sstevel@tonic-gate * is padded with zeros in the bytes after the used entries. 40210Sstevel@tonic-gate */ 4022251Slclee static void 4023251Slclee copy_Table_to_Bootblk(void) 40240Sstevel@tonic-gate { 40250Sstevel@tonic-gate struct ipart *boot_ptr, *tbl_ptr; 40260Sstevel@tonic-gate 40270Sstevel@tonic-gate boot_ptr = (struct ipart *)Bootblk->parts; 40280Sstevel@tonic-gate tbl_ptr = (struct ipart *)&Table[0].bootid; 40290Sstevel@tonic-gate for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid; 40300Sstevel@tonic-gate tbl_ptr++, boot_ptr++) { 40315169Slclee if (tbl_ptr->systid == UNUSED) 40325169Slclee (void) memset(boot_ptr, 0, sizeof (struct ipart)); 40335169Slclee else 40345169Slclee (void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart)); 40350Sstevel@tonic-gate } 403610021SSheshadri.Vasudevan@Sun.COM Bootblk->signature = LE_16(MBB_MAGIC); 40370Sstevel@tonic-gate } 40380Sstevel@tonic-gate 40390Sstevel@tonic-gate /* 40400Sstevel@tonic-gate * TableChanged 40410Sstevel@tonic-gate * Check for any changes in the partition table. 40420Sstevel@tonic-gate */ 4043251Slclee static int 4044251Slclee TableChanged(void) 40450Sstevel@tonic-gate { 40460Sstevel@tonic-gate int i, changed; 40470Sstevel@tonic-gate 40480Sstevel@tonic-gate changed = 0; 40490Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 40505169Slclee if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) { 40515169Slclee /* Partition table changed, write back to disk */ 40525169Slclee changed = 1; 40535169Slclee } 40540Sstevel@tonic-gate } 40550Sstevel@tonic-gate 40560Sstevel@tonic-gate return (changed); 40570Sstevel@tonic-gate } 40580Sstevel@tonic-gate 40590Sstevel@tonic-gate /* 40600Sstevel@tonic-gate * ffile_write 40610Sstevel@tonic-gate * Display contents of partition table to standard output or 40620Sstevel@tonic-gate * another file name without writing it to the disk (-W file). 40630Sstevel@tonic-gate */ 4064251Slclee static void 4065251Slclee ffile_write(char *file) 40660Sstevel@tonic-gate { 40670Sstevel@tonic-gate register int i; 40680Sstevel@tonic-gate FILE *fp; 40690Sstevel@tonic-gate 40700Sstevel@tonic-gate /* 40710Sstevel@tonic-gate * If file isn't standard output, then it's a file name. 40720Sstevel@tonic-gate * Open file and write it. 40730Sstevel@tonic-gate */ 40740Sstevel@tonic-gate if (file != (char *)stdout) { 40755169Slclee if ((fp = fopen(file, "w")) == NULL) { 40765169Slclee (void) fprintf(stderr, 40775169Slclee "fdisk: Cannot open output file %s.\n", 40785169Slclee file); 40795169Slclee exit(1); 40805169Slclee } 40810Sstevel@tonic-gate } 40820Sstevel@tonic-gate else 40835169Slclee fp = stdout; 40840Sstevel@tonic-gate 40850Sstevel@tonic-gate /* 40860Sstevel@tonic-gate * Write the fdisk table information 40870Sstevel@tonic-gate */ 4088251Slclee (void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev); 4089251Slclee (void) fprintf(fp, "* Dimensions:\n"); 4090251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 4091251Slclee (void) fprintf(fp, "* %4d sectors/track\n", sectors); 4092251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", heads); 4093251Slclee (void) fprintf(fp, "* %4d cylinders\n", Numcyl); 4094251Slclee (void) fprintf(fp, "*\n"); 40950Sstevel@tonic-gate /* Write virtual (HBA) geometry, if required */ 40960Sstevel@tonic-gate if (v_flag) { 4097251Slclee (void) fprintf(fp, "* HBA Dimensions:\n"); 4098251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 4099251Slclee (void) fprintf(fp, "* %4d sectors/track\n", hba_sectors); 4100251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads); 4101251Slclee (void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl); 4102251Slclee (void) fprintf(fp, "*\n"); 41030Sstevel@tonic-gate } 4104251Slclee (void) fprintf(fp, "* systid:\n"); 4105251Slclee (void) fprintf(fp, "* 1: DOSOS12\n"); 4106251Slclee (void) fprintf(fp, "* 2: PCIXOS\n"); 4107251Slclee (void) fprintf(fp, "* 4: DOSOS16\n"); 4108251Slclee (void) fprintf(fp, "* 5: EXTDOS\n"); 4109251Slclee (void) fprintf(fp, "* 6: DOSBIG\n"); 4110251Slclee (void) fprintf(fp, "* 7: FDISK_IFS\n"); 4111251Slclee (void) fprintf(fp, "* 8: FDISK_AIXBOOT\n"); 4112251Slclee (void) fprintf(fp, "* 9: FDISK_AIXDATA\n"); 4113251Slclee (void) fprintf(fp, "* 10: FDISK_0S2BOOT\n"); 4114251Slclee (void) fprintf(fp, "* 11: FDISK_WINDOWS\n"); 4115251Slclee (void) fprintf(fp, "* 12: FDISK_EXT_WIN\n"); 4116251Slclee (void) fprintf(fp, "* 14: FDISK_FAT95\n"); 4117251Slclee (void) fprintf(fp, "* 15: FDISK_EXTLBA\n"); 4118251Slclee (void) fprintf(fp, "* 18: DIAGPART\n"); 4119251Slclee (void) fprintf(fp, "* 65: FDISK_LINUX\n"); 4120251Slclee (void) fprintf(fp, "* 82: FDISK_CPM\n"); 4121251Slclee (void) fprintf(fp, "* 86: DOSDATA\n"); 4122251Slclee (void) fprintf(fp, "* 98: OTHEROS\n"); 4123251Slclee (void) fprintf(fp, "* 99: UNIXOS\n"); 412410021SSheshadri.Vasudevan@Sun.COM (void) fprintf(fp, "* 100: FDISK_NOVELL2\n"); 4125251Slclee (void) fprintf(fp, "* 101: FDISK_NOVELL3\n"); 4126251Slclee (void) fprintf(fp, "* 119: FDISK_QNX4\n"); 4127251Slclee (void) fprintf(fp, "* 120: FDISK_QNX42\n"); 4128251Slclee (void) fprintf(fp, "* 121: FDISK_QNX43\n"); 4129251Slclee (void) fprintf(fp, "* 130: SUNIXOS\n"); 4130251Slclee (void) fprintf(fp, "* 131: FDISK_LINUXNAT\n"); 4131251Slclee (void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n"); 4132251Slclee (void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n"); 4133251Slclee (void) fprintf(fp, "* 165: FDISK_BSD\n"); 4134251Slclee (void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n"); 4135251Slclee (void) fprintf(fp, "* 183: FDISK_BSDIFS\n"); 4136251Slclee (void) fprintf(fp, "* 184: FDISK_BSDISWAP\n"); 4137251Slclee (void) fprintf(fp, "* 190: X86BOOT\n"); 4138251Slclee (void) fprintf(fp, "* 191: SUNIXOS2\n"); 4139251Slclee (void) fprintf(fp, "* 238: EFI_PMBR\n"); 4140251Slclee (void) fprintf(fp, "* 239: EFI_FS\n"); 4141251Slclee (void) fprintf(fp, "*\n"); 4142251Slclee (void) fprintf(fp, 41430Sstevel@tonic-gate "\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl" 41447563SPrasad.Singamsetty@Sun.COM " Rsect Numsect\n"); 41457563SPrasad.Singamsetty@Sun.COM 41460Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 414710021SSheshadri.Vasudevan@Sun.COM (void) fprintf(fp, 414810021SSheshadri.Vasudevan@Sun.COM " %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u" 414910021SSheshadri.Vasudevan@Sun.COM " %-10u\n", 415010021SSheshadri.Vasudevan@Sun.COM Table[i].systid, 415110021SSheshadri.Vasudevan@Sun.COM Table[i].bootid, 415210021SSheshadri.Vasudevan@Sun.COM Table[i].beghead, 415310021SSheshadri.Vasudevan@Sun.COM Table[i].begsect & 0x3f, 415410021SSheshadri.Vasudevan@Sun.COM ((Table[i].begcyl & 0xff) | ((Table[i].begsect & 415510021SSheshadri.Vasudevan@Sun.COM 0xc0) << 2)), 415610021SSheshadri.Vasudevan@Sun.COM Table[i].endhead, 415710021SSheshadri.Vasudevan@Sun.COM Table[i].endsect & 0x3f, 415810021SSheshadri.Vasudevan@Sun.COM ((Table[i].endcyl & 0xff) | ((Table[i].endsect & 415910021SSheshadri.Vasudevan@Sun.COM 0xc0) << 2)), 416010021SSheshadri.Vasudevan@Sun.COM LE_32(Table[i].relsect), 416110021SSheshadri.Vasudevan@Sun.COM LE_32(Table[i].numsect)); 416210021SSheshadri.Vasudevan@Sun.COM } 416310021SSheshadri.Vasudevan@Sun.COM #ifdef i386 416410021SSheshadri.Vasudevan@Sun.COM if (fdisk_ext_part_exists(epp)) { 416510021SSheshadri.Vasudevan@Sun.COM struct ipart ext_tab; 416610021SSheshadri.Vasudevan@Sun.COM logical_drive_t *temp; 416710021SSheshadri.Vasudevan@Sun.COM uint32_t rsect, numsect, tempsect = 0; 416810021SSheshadri.Vasudevan@Sun.COM for (temp = fdisk_get_ld_head(epp); temp != NULL; 416910021SSheshadri.Vasudevan@Sun.COM temp = temp->next) { 417010021SSheshadri.Vasudevan@Sun.COM ext_tab = temp->parts[0]; 417110021SSheshadri.Vasudevan@Sun.COM rsect = tempsect + LE_32(ext_tab.relsect) + 417210021SSheshadri.Vasudevan@Sun.COM fdisk_get_ext_beg_sec(epp); 417310021SSheshadri.Vasudevan@Sun.COM numsect = LE_32(ext_tab.numsect); 417410021SSheshadri.Vasudevan@Sun.COM tempsect = LE_32(temp->parts[1].relsect); 4175251Slclee (void) fprintf(fp, 417610021SSheshadri.Vasudevan@Sun.COM " %-5d %-4d %-6d %-6d %-7d %-6d %-6d " 417710021SSheshadri.Vasudevan@Sun.COM "%-7d %-8u %-8u\n", 417810021SSheshadri.Vasudevan@Sun.COM ext_tab.systid, 417910021SSheshadri.Vasudevan@Sun.COM ext_tab.bootid, 418010021SSheshadri.Vasudevan@Sun.COM ext_tab.beghead, 418110021SSheshadri.Vasudevan@Sun.COM ext_tab.begsect & 0x3f, 418210021SSheshadri.Vasudevan@Sun.COM ((ext_tab.begcyl & 0xff) | 418310021SSheshadri.Vasudevan@Sun.COM ((ext_tab.begsect & 0xc0) << 2)), 418410021SSheshadri.Vasudevan@Sun.COM ext_tab.endhead, 418510021SSheshadri.Vasudevan@Sun.COM ext_tab.endsect & 0x3f, 418610021SSheshadri.Vasudevan@Sun.COM ((ext_tab.endcyl & 0xff) | 418710021SSheshadri.Vasudevan@Sun.COM ((ext_tab.endsect & 0xc0) << 2)), 418810021SSheshadri.Vasudevan@Sun.COM rsect, 418910021SSheshadri.Vasudevan@Sun.COM numsect); 419010021SSheshadri.Vasudevan@Sun.COM } 41910Sstevel@tonic-gate } 419210021SSheshadri.Vasudevan@Sun.COM #endif 419310021SSheshadri.Vasudevan@Sun.COM 41940Sstevel@tonic-gate if (fp != stdout) 4195251Slclee (void) fclose(fp); 41960Sstevel@tonic-gate } 41970Sstevel@tonic-gate 41980Sstevel@tonic-gate /* 41990Sstevel@tonic-gate * fix_slice 42000Sstevel@tonic-gate * Read the VTOC table on the Solaris partition and check that no 42010Sstevel@tonic-gate * slices exist that extend past the end of the Solaris partition. 42020Sstevel@tonic-gate * If no Solaris partition exists, nothing is done. 42030Sstevel@tonic-gate */ 4204251Slclee static void 4205251Slclee fix_slice(void) 42060Sstevel@tonic-gate { 42070Sstevel@tonic-gate int i; 42087563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 42090Sstevel@tonic-gate 42100Sstevel@tonic-gate if (io_image) { 4211251Slclee return; 42120Sstevel@tonic-gate } 42130Sstevel@tonic-gate 42140Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 42150Sstevel@tonic-gate if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) { 42160Sstevel@tonic-gate /* 42170Sstevel@tonic-gate * Only the size matters (not starting point), since 42180Sstevel@tonic-gate * VTOC entries are relative to the start of 42190Sstevel@tonic-gate * the partition. 42200Sstevel@tonic-gate */ 422110021SSheshadri.Vasudevan@Sun.COM numsect = LE_32(Table[i].numsect); 42220Sstevel@tonic-gate break; 42230Sstevel@tonic-gate } 42240Sstevel@tonic-gate } 42250Sstevel@tonic-gate 42260Sstevel@tonic-gate if (i >= FD_NUMPART) { 42270Sstevel@tonic-gate if (!io_nifdisk) { 42280Sstevel@tonic-gate (void) fprintf(stderr, 42290Sstevel@tonic-gate "fdisk: No Solaris partition found - VTOC not" 42300Sstevel@tonic-gate " checked.\n"); 42310Sstevel@tonic-gate } 4232251Slclee return; 42330Sstevel@tonic-gate } 42340Sstevel@tonic-gate 4235251Slclee if (readvtoc() != VTOC_OK) { 42360Sstevel@tonic-gate exit(1); /* Failed to read the VTOC */ 42375169Slclee } 42385169Slclee for (i = 0; i < V_NUMPAR; i++) { 42395169Slclee /* Special case for slice two (entire disk) */ 42405169Slclee if (i == 2) { 42415169Slclee if (disk_vtoc.v_part[i].p_start != 0) { 42425169Slclee (void) fprintf(stderr, 42437563SPrasad.Singamsetty@Sun.COM "slice %d starts at %llu, is not at" 42445169Slclee " start of partition", 42455169Slclee i, disk_vtoc.v_part[i].p_start); 42465169Slclee if (!io_nifdisk) { 42475169Slclee (void) printf(" adjust ?:"); 42485169Slclee if (yesno()) 42490Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 42505169Slclee } else { 42515169Slclee disk_vtoc.v_part[i].p_start = 0; 42525169Slclee (void) fprintf(stderr, " adjusted!\n"); 42530Sstevel@tonic-gate } 42545169Slclee 42555169Slclee } 42565169Slclee if (disk_vtoc.v_part[i].p_size != numsect) { 42575169Slclee (void) fprintf(stderr, 42587563SPrasad.Singamsetty@Sun.COM "slice %d size %llu does not cover" 42595169Slclee " complete partition", 42605169Slclee i, disk_vtoc.v_part[i].p_size); 42615169Slclee if (!io_nifdisk) { 42625169Slclee (void) printf(" adjust ?:"); 42635169Slclee if (yesno()) 42640Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 42650Sstevel@tonic-gate numsect; 42665169Slclee } else { 42675169Slclee disk_vtoc.v_part[i].p_size = numsect; 42685169Slclee (void) fprintf(stderr, " adjusted!\n"); 42690Sstevel@tonic-gate } 42705169Slclee } 42715169Slclee if (disk_vtoc.v_part[i].p_tag != V_BACKUP) { 42725169Slclee (void) fprintf(stderr, 42735169Slclee "slice %d tag was %d should be %d", 42745169Slclee i, disk_vtoc.v_part[i].p_tag, 42755169Slclee V_BACKUP); 42765169Slclee if (!io_nifdisk) { 4277251Slclee (void) printf(" fix ?:"); 42785169Slclee if (yesno()) 42790Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 42800Sstevel@tonic-gate V_BACKUP; 42815169Slclee } else { 42820Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = V_BACKUP; 42830Sstevel@tonic-gate (void) fprintf(stderr, " fixed!\n"); 42840Sstevel@tonic-gate } 42855169Slclee } 42865169Slclee continue; 42875169Slclee } 42885169Slclee if (io_ADJT) { 42895169Slclee if (disk_vtoc.v_part[i].p_start > numsect || 42905169Slclee disk_vtoc.v_part[i].p_start + 42915169Slclee disk_vtoc.v_part[i].p_size > numsect) { 42925169Slclee (void) fprintf(stderr, 42937563SPrasad.Singamsetty@Sun.COM "slice %d (start %llu, end %llu)" 42945169Slclee " is larger than the partition", 42955169Slclee i, disk_vtoc.v_part[i].p_start, 42965169Slclee disk_vtoc.v_part[i].p_start + 42975169Slclee disk_vtoc.v_part[i].p_size); 42985169Slclee if (!io_nifdisk) { 42995169Slclee (void) printf(" remove ?:"); 43005169Slclee if (yesno()) { 43010Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 43020Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 43030Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 43040Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 43050Sstevel@tonic-gate } 43060Sstevel@tonic-gate } else { 43075169Slclee disk_vtoc.v_part[i].p_size = 0; 43085169Slclee disk_vtoc.v_part[i].p_start = 0; 43095169Slclee disk_vtoc.v_part[i].p_tag = 0; 43105169Slclee disk_vtoc.v_part[i].p_flag = 0; 43110Sstevel@tonic-gate (void) fprintf(stderr, 43125169Slclee " removed!\n"); 43135169Slclee } 43145169Slclee } 43155169Slclee continue; 43165169Slclee } 43175169Slclee if (disk_vtoc.v_part[i].p_start > numsect) { 43185169Slclee (void) fprintf(stderr, 43197563SPrasad.Singamsetty@Sun.COM "slice %d (start %llu) is larger than the " 43207563SPrasad.Singamsetty@Sun.COM "partition", i, disk_vtoc.v_part[i].p_start); 43215169Slclee if (!io_nifdisk) { 43225169Slclee (void) printf(" remove ?:"); 43235169Slclee if (yesno()) { 43245169Slclee disk_vtoc.v_part[i].p_size = 0; 43255169Slclee disk_vtoc.v_part[i].p_start = 0; 43265169Slclee disk_vtoc.v_part[i].p_tag = 0; 43275169Slclee disk_vtoc.v_part[i].p_flag = 0; 43280Sstevel@tonic-gate } 43295169Slclee } else { 43305169Slclee disk_vtoc.v_part[i].p_size = 0; 43315169Slclee disk_vtoc.v_part[i].p_start = 0; 43325169Slclee disk_vtoc.v_part[i].p_tag = 0; 43335169Slclee disk_vtoc.v_part[i].p_flag = 0; 43345169Slclee (void) fprintf(stderr, 43355169Slclee " removed!\n"); 43365169Slclee } 43375169Slclee } else if (disk_vtoc.v_part[i].p_start 43385169Slclee + disk_vtoc.v_part[i].p_size > numsect) { 43395169Slclee (void) fprintf(stderr, 43407563SPrasad.Singamsetty@Sun.COM "slice %d (end %llu) is larger" 43415169Slclee " than the partition", 43425169Slclee i, 43435169Slclee disk_vtoc.v_part[i].p_start + 43445169Slclee disk_vtoc.v_part[i].p_size); 43455169Slclee if (!io_nifdisk) { 43465169Slclee (void) printf(" adjust ?:"); 43475169Slclee if (yesno()) { 43485169Slclee disk_vtoc.v_part[i].p_size = numsect; 43495169Slclee } 43505169Slclee } else { 43515169Slclee disk_vtoc.v_part[i].p_size = numsect; 43525169Slclee (void) fprintf(stderr, " adjusted!\n"); 43530Sstevel@tonic-gate } 43540Sstevel@tonic-gate } 43550Sstevel@tonic-gate } 43560Sstevel@tonic-gate #if 1 /* bh for now */ 43570Sstevel@tonic-gate /* Make the VTOC look sane - ha ha */ 43580Sstevel@tonic-gate disk_vtoc.v_version = V_VERSION; 43590Sstevel@tonic-gate disk_vtoc.v_sanity = VTOC_SANE; 43600Sstevel@tonic-gate disk_vtoc.v_nparts = V_NUMPAR; 43610Sstevel@tonic-gate if (disk_vtoc.v_sectorsz == 0) 43620Sstevel@tonic-gate disk_vtoc.v_sectorsz = NBPSCTR; 43630Sstevel@tonic-gate #endif 43640Sstevel@tonic-gate 43650Sstevel@tonic-gate /* Write the VTOC back to the disk */ 43660Sstevel@tonic-gate if (!io_readonly) 4367251Slclee (void) writevtoc(); 43680Sstevel@tonic-gate } 43690Sstevel@tonic-gate 43700Sstevel@tonic-gate /* 43710Sstevel@tonic-gate * yesno 43720Sstevel@tonic-gate * Get yes or no answer. Return 1 for yes and 0 for no. 43730Sstevel@tonic-gate */ 43740Sstevel@tonic-gate 4375251Slclee static int 4376251Slclee yesno(void) 43770Sstevel@tonic-gate { 43780Sstevel@tonic-gate char s[80]; 43790Sstevel@tonic-gate 43800Sstevel@tonic-gate for (;;) { 43819786SBarry.Harding@Sun.COM (void) fgets(s, sizeof (s), stdin); 43820Sstevel@tonic-gate rm_blanks(s); 43839786SBarry.Harding@Sun.COM if (((s[1] != '\0') && (s[1] != '\n')) || 43849786SBarry.Harding@Sun.COM ((s[0] != 'y') && (s[0] != 'n'))) { 4385251Slclee (void) printf(E_LINE); 4386251Slclee (void) printf("Please answer with \"y\" or \"n\": "); 43870Sstevel@tonic-gate continue; 43880Sstevel@tonic-gate } 43890Sstevel@tonic-gate if (s[0] == 'y') 43900Sstevel@tonic-gate return (1); 43910Sstevel@tonic-gate else 43920Sstevel@tonic-gate return (0); 43930Sstevel@tonic-gate } 43940Sstevel@tonic-gate } 43950Sstevel@tonic-gate 43960Sstevel@tonic-gate /* 43970Sstevel@tonic-gate * readvtoc 43980Sstevel@tonic-gate * Read the VTOC from the Solaris partition of the device. 43990Sstevel@tonic-gate */ 4400251Slclee static int 4401251Slclee readvtoc(void) 44020Sstevel@tonic-gate { 44030Sstevel@tonic-gate int i; 44040Sstevel@tonic-gate int retval = VTOC_OK; 44050Sstevel@tonic-gate 44067563SPrasad.Singamsetty@Sun.COM if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) { 44070Sstevel@tonic-gate if (i == VT_EINVAL) { 44080Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Invalid VTOC.\n"); 44090Sstevel@tonic-gate vt_inval++; 44100Sstevel@tonic-gate retval = VTOC_INVAL; 44110Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 44120Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 44135169Slclee "GPT\n"); 44140Sstevel@tonic-gate retval = VTOC_NOTSUP; 44150Sstevel@tonic-gate } else { 44160Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot read VTOC.\n"); 44170Sstevel@tonic-gate retval = VTOC_RWERR; 44180Sstevel@tonic-gate } 44190Sstevel@tonic-gate } 44200Sstevel@tonic-gate return (retval); 44210Sstevel@tonic-gate } 44220Sstevel@tonic-gate 44230Sstevel@tonic-gate /* 44240Sstevel@tonic-gate * writevtoc 44250Sstevel@tonic-gate * Write the VTOC to the Solaris partition on the device. 44260Sstevel@tonic-gate */ 4427251Slclee static int 4428251Slclee writevtoc(void) 44290Sstevel@tonic-gate { 44300Sstevel@tonic-gate int i; 44310Sstevel@tonic-gate int retval = 0; 44320Sstevel@tonic-gate 44337563SPrasad.Singamsetty@Sun.COM if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) { 44340Sstevel@tonic-gate if (i == VT_EINVAL) { 44350Sstevel@tonic-gate (void) fprintf(stderr, 44360Sstevel@tonic-gate "fdisk: Invalid entry exists in VTOC.\n"); 44370Sstevel@tonic-gate retval = VTOC_INVAL; 44380Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 44390Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 44405169Slclee "GPT\n"); 44410Sstevel@tonic-gate retval = VTOC_NOTSUP; 44420Sstevel@tonic-gate } else { 44430Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot write VTOC.\n"); 44440Sstevel@tonic-gate retval = VTOC_RWERR; 44450Sstevel@tonic-gate } 44460Sstevel@tonic-gate } 44470Sstevel@tonic-gate return (retval); 44480Sstevel@tonic-gate } 44490Sstevel@tonic-gate 44500Sstevel@tonic-gate /* 44510Sstevel@tonic-gate * efi_ioctl 44520Sstevel@tonic-gate * issues DKIOCSETEFI IOCTL 44530Sstevel@tonic-gate * (duplicate of private efi_ioctl() in rdwr_efi.c 44540Sstevel@tonic-gate */ 44550Sstevel@tonic-gate static int 44560Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 44570Sstevel@tonic-gate { 44580Sstevel@tonic-gate void *data = dk_ioc->dki_data; 44590Sstevel@tonic-gate int error; 44600Sstevel@tonic-gate 44610Sstevel@tonic-gate dk_ioc->dki_data_64 = (uintptr_t)data; 44620Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc); 44630Sstevel@tonic-gate 44640Sstevel@tonic-gate return (error); 44650Sstevel@tonic-gate } 44660Sstevel@tonic-gate 44670Sstevel@tonic-gate /* 44680Sstevel@tonic-gate * clear_efi 44690Sstevel@tonic-gate * Clear EFI labels from the EFI_PMBR partition on the device 44700Sstevel@tonic-gate * This function is modeled on the libefi(3LIB) call efi_write() 44710Sstevel@tonic-gate */ 4472251Slclee static int 4473251Slclee clear_efi(void) 44740Sstevel@tonic-gate { 44750Sstevel@tonic-gate struct dk_gpt *efi_vtoc; 44760Sstevel@tonic-gate dk_efi_t dk_ioc; 44770Sstevel@tonic-gate 44780Sstevel@tonic-gate /* 44790Sstevel@tonic-gate * see if we can read the EFI label 44800Sstevel@tonic-gate */ 44810Sstevel@tonic-gate if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) { 44820Sstevel@tonic-gate return (VT_ERROR); 44830Sstevel@tonic-gate } 44840Sstevel@tonic-gate 44850Sstevel@tonic-gate /* 44860Sstevel@tonic-gate * set up the dk_ioc structure for writing 44870Sstevel@tonic-gate */ 44880Sstevel@tonic-gate dk_ioc.dki_lba = 1; 44890Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize; 44900Sstevel@tonic-gate 44910Sstevel@tonic-gate if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) { 44920Sstevel@tonic-gate return (VT_ERROR); 44930Sstevel@tonic-gate } 44940Sstevel@tonic-gate 44950Sstevel@tonic-gate /* 44960Sstevel@tonic-gate * clear the primary label 44970Sstevel@tonic-gate */ 44980Sstevel@tonic-gate if (io_debug) { 4499251Slclee (void) fprintf(stderr, 4500251Slclee "\tClearing primary EFI label at block %lld\n", 4501251Slclee dk_ioc.dki_lba); 45020Sstevel@tonic-gate } 45030Sstevel@tonic-gate 45040Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 45050Sstevel@tonic-gate free(dk_ioc.dki_data); 45060Sstevel@tonic-gate switch (errno) { 45070Sstevel@tonic-gate case EIO: 45080Sstevel@tonic-gate return (VT_EIO); 45090Sstevel@tonic-gate case EINVAL: 45100Sstevel@tonic-gate return (VT_EINVAL); 45110Sstevel@tonic-gate default: 45120Sstevel@tonic-gate return (VT_ERROR); 45130Sstevel@tonic-gate } 45140Sstevel@tonic-gate } 45150Sstevel@tonic-gate 45160Sstevel@tonic-gate /* 45170Sstevel@tonic-gate * clear the backup partition table 45180Sstevel@tonic-gate */ 45190Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1; 45200Sstevel@tonic-gate dk_ioc.dki_length -= efi_vtoc->efi_lbasize; 45219889SLarry.Liu@Sun.COM dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data + 45229889SLarry.Liu@Sun.COM efi_vtoc->efi_lbasize); 45230Sstevel@tonic-gate if (io_debug) { 4524251Slclee (void) fprintf(stderr, 4525251Slclee "\tClearing backup partition table at block %lld\n", 4526251Slclee dk_ioc.dki_lba); 45270Sstevel@tonic-gate } 45280Sstevel@tonic-gate 45290Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 45300Sstevel@tonic-gate (void) fprintf(stderr, "\tUnable to clear backup EFI label at " 45315169Slclee "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1, 45325169Slclee errno); 45330Sstevel@tonic-gate } 45340Sstevel@tonic-gate 45350Sstevel@tonic-gate /* 45360Sstevel@tonic-gate * clear the backup label 45370Sstevel@tonic-gate */ 45380Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_lba; 45390Sstevel@tonic-gate dk_ioc.dki_length = efi_vtoc->efi_lbasize; 45409889SLarry.Liu@Sun.COM dk_ioc.dki_data = (efi_gpt_t *)((char *)dk_ioc.dki_data - 45419889SLarry.Liu@Sun.COM efi_vtoc->efi_lbasize); 45420Sstevel@tonic-gate if (io_debug) { 4543251Slclee (void) fprintf(stderr, "\tClearing backup label at block " 4544251Slclee "%lld\n", dk_ioc.dki_lba); 45450Sstevel@tonic-gate } 45460Sstevel@tonic-gate 45470Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 4548251Slclee (void) fprintf(stderr, 4549251Slclee "\tUnable to clear backup EFI label at " 4550251Slclee "block %llu; errno %d\n", 4551251Slclee efi_vtoc->efi_last_lba, 4552251Slclee errno); 45530Sstevel@tonic-gate } 45540Sstevel@tonic-gate 45550Sstevel@tonic-gate free(dk_ioc.dki_data); 45560Sstevel@tonic-gate efi_free(efi_vtoc); 45570Sstevel@tonic-gate 45580Sstevel@tonic-gate return (0); 45590Sstevel@tonic-gate } 45600Sstevel@tonic-gate 45610Sstevel@tonic-gate /* 45620Sstevel@tonic-gate * clear_vtoc 45630Sstevel@tonic-gate * Clear the VTOC from the current or previous Solaris partition on the 45640Sstevel@tonic-gate * device. 45650Sstevel@tonic-gate */ 4566251Slclee static void 45670Sstevel@tonic-gate clear_vtoc(int table, int part) 45680Sstevel@tonic-gate { 45690Sstevel@tonic-gate struct ipart *clr_table; 45709889SLarry.Liu@Sun.COM char *disk_label; 45717563SPrasad.Singamsetty@Sun.COM uint32_t pcyl, ncyl, count; 45727563SPrasad.Singamsetty@Sun.COM diskaddr_t backup_block, solaris_offset; 45737563SPrasad.Singamsetty@Sun.COM ssize_t bytes; 45746549Sbharding off_t seek_byte; 45750Sstevel@tonic-gate 45760Sstevel@tonic-gate #ifdef DEBUG 45779889SLarry.Liu@Sun.COM char *read_label; 45780Sstevel@tonic-gate #endif /* DEBUG */ 45790Sstevel@tonic-gate 45800Sstevel@tonic-gate if (table == OLD) { 45810Sstevel@tonic-gate clr_table = &Old_Table[part]; 45820Sstevel@tonic-gate } else { 45830Sstevel@tonic-gate clr_table = &Table[part]; 45840Sstevel@tonic-gate } 45850Sstevel@tonic-gate 45869889SLarry.Liu@Sun.COM disk_label = (char *)calloc(sectsiz, 1); 45879889SLarry.Liu@Sun.COM if (disk_label == NULL) { 45889889SLarry.Liu@Sun.COM return; 45899889SLarry.Liu@Sun.COM } 45900Sstevel@tonic-gate 459110021SSheshadri.Vasudevan@Sun.COM seek_byte = (off_t)(LE_32(clr_table->relsect) + VTOC_OFFSET) * sectsiz; 45920Sstevel@tonic-gate 45930Sstevel@tonic-gate if (io_debug) { 45946549Sbharding (void) fprintf(stderr, 45956549Sbharding "\tClearing primary VTOC at byte %llu (block %llu)\n", 45966549Sbharding (uint64_t)seek_byte, 459710021SSheshadri.Vasudevan@Sun.COM (uint64_t)(LE_32(clr_table->relsect) + VTOC_OFFSET)); 45980Sstevel@tonic-gate } 45990Sstevel@tonic-gate 46000Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 4601251Slclee (void) fprintf(stderr, 46026549Sbharding "\tError seeking to primary label at byte %llu\n", 46036549Sbharding (uint64_t)seek_byte); 46049889SLarry.Liu@Sun.COM free(disk_label); 4605251Slclee return; 46060Sstevel@tonic-gate } 46070Sstevel@tonic-gate 46089889SLarry.Liu@Sun.COM bytes = write(Dev, disk_label, sectsiz); 46099889SLarry.Liu@Sun.COM 46109889SLarry.Liu@Sun.COM if (bytes != sectsiz) { 4611251Slclee (void) fprintf(stderr, 46127563SPrasad.Singamsetty@Sun.COM "\tWarning: only %d bytes written to clear primary" 46137563SPrasad.Singamsetty@Sun.COM " VTOC!\n", bytes); 46140Sstevel@tonic-gate } 46150Sstevel@tonic-gate 46160Sstevel@tonic-gate #ifdef DEBUG 46170Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 4618251Slclee (void) fprintf(stderr, 46196549Sbharding "DEBUG: Error seeking to primary label at byte %llu\n", 46206549Sbharding (uint64_t)seek_byte); 46219889SLarry.Liu@Sun.COM free(disk_label); 4622251Slclee return; 46230Sstevel@tonic-gate } else { 46246549Sbharding (void) fprintf(stderr, 46256549Sbharding "DEBUG: Successful lseek() to byte %llu\n", 46266549Sbharding (uint64_t)seek_byte); 46270Sstevel@tonic-gate } 46280Sstevel@tonic-gate 46299889SLarry.Liu@Sun.COM read_label = (char *)calloc(sectsiz, 1); 46309889SLarry.Liu@Sun.COM if (read_label == NULL) { 46319889SLarry.Liu@Sun.COM free(disk_label); 46329889SLarry.Liu@Sun.COM return; 46339889SLarry.Liu@Sun.COM } 46349889SLarry.Liu@Sun.COM 46359889SLarry.Liu@Sun.COM bytes = read(Dev, read_label, sectsiz); 46369889SLarry.Liu@Sun.COM 46379889SLarry.Liu@Sun.COM if (bytes != sectsiz) { 4638251Slclee (void) fprintf(stderr, 4639251Slclee "DEBUG: Warning: only %d bytes read of label\n", 46400Sstevel@tonic-gate bytes); 46410Sstevel@tonic-gate } 46420Sstevel@tonic-gate 46439889SLarry.Liu@Sun.COM if (memcmp(disk_label, read_label, sectsiz) != 0) { 4644251Slclee (void) fprintf(stderr, 4645251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 46460Sstevel@tonic-gate } else { 4647251Slclee (void) fprintf(stderr, "DEBUG Good compare of disk_label and " 46480Sstevel@tonic-gate "read_label\n"); 46490Sstevel@tonic-gate } 46500Sstevel@tonic-gate #endif /* DEBUG */ 46510Sstevel@tonic-gate 46520Sstevel@tonic-gate /* Clear backup label */ 465310021SSheshadri.Vasudevan@Sun.COM pcyl = LE_32(clr_table->numsect) / (heads * sectors); 465410021SSheshadri.Vasudevan@Sun.COM solaris_offset = LE_32(clr_table->relsect); 46550Sstevel@tonic-gate ncyl = pcyl - acyl; 46560Sstevel@tonic-gate 46570Sstevel@tonic-gate backup_block = ((ncyl + acyl - 1) * 46580Sstevel@tonic-gate (heads * sectors)) + ((heads - 1) * sectors) + 1; 46590Sstevel@tonic-gate 46600Sstevel@tonic-gate for (count = 1; count < 6; count++) { 46619889SLarry.Liu@Sun.COM seek_byte = (off_t)(solaris_offset + backup_block) * sectsiz; 46620Sstevel@tonic-gate 46630Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 4664251Slclee (void) fprintf(stderr, 46656549Sbharding "\tError seeking to backup label at byte %llu on " 46666549Sbharding "%s.\n", (uint64_t)seek_byte, Dfltdev); 46679889SLarry.Liu@Sun.COM free(disk_label); 46689889SLarry.Liu@Sun.COM #ifdef DEBUG 46699889SLarry.Liu@Sun.COM free(read_label); 46709889SLarry.Liu@Sun.COM #endif /* DEBUG */ 4671251Slclee return; 46720Sstevel@tonic-gate } 46730Sstevel@tonic-gate 46740Sstevel@tonic-gate if (io_debug) { 4675251Slclee (void) fprintf(stderr, "\tClearing backup VTOC at" 46766549Sbharding " byte %llu (block %llu)\n", 46776549Sbharding (uint64_t)seek_byte, 46786549Sbharding (uint64_t)(solaris_offset + backup_block)); 46790Sstevel@tonic-gate } 46800Sstevel@tonic-gate 46819889SLarry.Liu@Sun.COM bytes = write(Dev, disk_label, sectsiz); 46829889SLarry.Liu@Sun.COM 46839889SLarry.Liu@Sun.COM if (bytes != sectsiz) { 4684251Slclee (void) fprintf(stderr, 4685251Slclee "\t\tWarning: only %d bytes written to " 46866549Sbharding "clear backup VTOC at block %llu!\n", bytes, 46876549Sbharding (uint64_t)(solaris_offset + backup_block)); 46880Sstevel@tonic-gate } 46890Sstevel@tonic-gate 46900Sstevel@tonic-gate #ifdef DEBUG 46910Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 4692251Slclee (void) fprintf(stderr, 46936549Sbharding "DEBUG: Error seeking to backup label at byte %llu\n", 46946549Sbharding (uint64_t)seek_byte); 46959889SLarry.Liu@Sun.COM free(disk_label); 46969889SLarry.Liu@Sun.COM free(read_label); 4697251Slclee return; 46980Sstevel@tonic-gate } else { 46996549Sbharding (void) fprintf(stderr, 47006549Sbharding "DEBUG: Successful lseek() to byte %llu\n", 47016549Sbharding (uint64_t)seek_byte); 47020Sstevel@tonic-gate } 47030Sstevel@tonic-gate 47049889SLarry.Liu@Sun.COM bytes = read(Dev, read_label, sectsiz); 47059889SLarry.Liu@Sun.COM 47069889SLarry.Liu@Sun.COM if (bytes != sectsiz) { 4707251Slclee (void) fprintf(stderr, 4708251Slclee "DEBUG: Warning: only %d bytes read of backup label\n", 4709251Slclee bytes); 47100Sstevel@tonic-gate } 47110Sstevel@tonic-gate 47129889SLarry.Liu@Sun.COM if (memcmp(disk_label, read_label, sectsiz) != 0) { 4713251Slclee (void) fprintf(stderr, 4714251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 47150Sstevel@tonic-gate } else { 4716251Slclee (void) fprintf(stderr, 4717251Slclee "DEBUG: Good compare of disk_label and backup " 47180Sstevel@tonic-gate "read_label\n"); 47190Sstevel@tonic-gate } 47209889SLarry.Liu@Sun.COM 47210Sstevel@tonic-gate #endif /* DEBUG */ 47220Sstevel@tonic-gate 47230Sstevel@tonic-gate backup_block += 2; 47240Sstevel@tonic-gate } 47259889SLarry.Liu@Sun.COM 47269889SLarry.Liu@Sun.COM #ifdef DEBUG 47279889SLarry.Liu@Sun.COM free(read_label); 47289889SLarry.Liu@Sun.COM #endif /* DEBUG */ 47299889SLarry.Liu@Sun.COM free(disk_label); 47300Sstevel@tonic-gate } 47310Sstevel@tonic-gate 47320Sstevel@tonic-gate #define FDISK_STANDARD_LECTURE \ 47330Sstevel@tonic-gate "Fdisk is normally used with the device that " \ 47340Sstevel@tonic-gate "represents the entire fixed disk.\n" \ 47350Sstevel@tonic-gate "(For example, /dev/rdsk/c0d0p0 on x86 or " \ 47360Sstevel@tonic-gate "/dev/rdsk/c0t5d0s2 on sparc).\n" 47370Sstevel@tonic-gate 47380Sstevel@tonic-gate #define FDISK_LECTURE_NOT_SECTOR_ZERO \ 47390Sstevel@tonic-gate "The device does not appear to include absolute\n" \ 47400Sstevel@tonic-gate "sector 0 of the PHYSICAL disk " \ 47410Sstevel@tonic-gate "(the normal location for an fdisk table).\n" 47420Sstevel@tonic-gate 47430Sstevel@tonic-gate #define FDISK_LECTURE_NOT_FULL \ 47440Sstevel@tonic-gate "The device does not appear to encompass the entire PHYSICAL disk.\n" 47450Sstevel@tonic-gate 47460Sstevel@tonic-gate #define FDISK_LECTURE_NO_VTOC \ 47470Sstevel@tonic-gate "Unable to find a volume table of contents.\n" \ 47480Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 47490Sstevel@tonic-gate 47500Sstevel@tonic-gate #define FDISK_LECTURE_NO_GEOM \ 47510Sstevel@tonic-gate "Unable to get geometry from device.\n" \ 47520Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 47530Sstevel@tonic-gate 47540Sstevel@tonic-gate #define FDISK_SHALL_I_CONTINUE \ 47550Sstevel@tonic-gate "Are you sure you want to continue? (y/n) " 47560Sstevel@tonic-gate 47570Sstevel@tonic-gate /* 47580Sstevel@tonic-gate * lecture_and_query 47590Sstevel@tonic-gate * Called when a sanity check fails. This routine gives a warning 47600Sstevel@tonic-gate * specific to the check that fails, followed by a generic lecture 47610Sstevel@tonic-gate * about the "right" device to supply as input. Then, if appropriate, 47620Sstevel@tonic-gate * it will prompt the user on whether or not they want to continue. 47630Sstevel@tonic-gate * Inappropriate times for prompting are when the user has selected 47640Sstevel@tonic-gate * non-interactive mode or read-only mode. 47650Sstevel@tonic-gate */ 4766251Slclee static int 47670Sstevel@tonic-gate lecture_and_query(char *warning, char *devname) 47680Sstevel@tonic-gate { 47690Sstevel@tonic-gate if (io_nifdisk) 47700Sstevel@tonic-gate return (0); 47710Sstevel@tonic-gate 4772251Slclee (void) fprintf(stderr, "WARNING: Device %s: \n", devname); 4773251Slclee (void) fprintf(stderr, "%s", warning); 4774251Slclee (void) fprintf(stderr, FDISK_STANDARD_LECTURE); 4775251Slclee (void) fprintf(stderr, FDISK_SHALL_I_CONTINUE); 47760Sstevel@tonic-gate 47770Sstevel@tonic-gate return (yesno()); 47780Sstevel@tonic-gate } 47790Sstevel@tonic-gate 4780251Slclee static void 47810Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd) 47820Sstevel@tonic-gate { 47837563SPrasad.Singamsetty@Sun.COM struct extvtoc v; 47840Sstevel@tonic-gate struct dk_geom d; 47850Sstevel@tonic-gate struct part_info pi; 47867563SPrasad.Singamsetty@Sun.COM struct extpart_info extpi; 47877563SPrasad.Singamsetty@Sun.COM diskaddr_t totsize; 47880Sstevel@tonic-gate int idx = -1; 47890Sstevel@tonic-gate 47900Sstevel@tonic-gate /* 47910Sstevel@tonic-gate * First try the PARTINFO ioctl. If it works, we will be able 47920Sstevel@tonic-gate * to tell if they've specified the full disk partition by checking 47930Sstevel@tonic-gate * to see if they've specified a partition that starts at sector 0. 47940Sstevel@tonic-gate */ 47957563SPrasad.Singamsetty@Sun.COM if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) { 47967563SPrasad.Singamsetty@Sun.COM if (extpi.p_start != 0) { 47977563SPrasad.Singamsetty@Sun.COM if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 47987563SPrasad.Singamsetty@Sun.COM devname)) { 47997563SPrasad.Singamsetty@Sun.COM (void) close(fd); 48007563SPrasad.Singamsetty@Sun.COM exit(1); 48017563SPrasad.Singamsetty@Sun.COM } 48027563SPrasad.Singamsetty@Sun.COM } 48037563SPrasad.Singamsetty@Sun.COM } else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) { 48040Sstevel@tonic-gate if (pi.p_start != 0) { 48050Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 48060Sstevel@tonic-gate devname)) { 48070Sstevel@tonic-gate (void) close(fd); 48080Sstevel@tonic-gate exit(1); 48090Sstevel@tonic-gate } 48100Sstevel@tonic-gate } 48110Sstevel@tonic-gate } else { 48127563SPrasad.Singamsetty@Sun.COM if ((idx = read_extvtoc(fd, &v)) < 0) { 48130Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_VTOC, 48140Sstevel@tonic-gate devname)) { 48150Sstevel@tonic-gate (void) close(fd); 48160Sstevel@tonic-gate exit(1); 48170Sstevel@tonic-gate } 48180Sstevel@tonic-gate return; 48190Sstevel@tonic-gate } 48200Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &d) == -1) { 48210Sstevel@tonic-gate perror(devname); 48220Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_GEOM, 48230Sstevel@tonic-gate devname)) { 48240Sstevel@tonic-gate (void) close(fd); 48250Sstevel@tonic-gate exit(1); 48260Sstevel@tonic-gate } 48270Sstevel@tonic-gate return; 48280Sstevel@tonic-gate } 48297563SPrasad.Singamsetty@Sun.COM totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect; 48300Sstevel@tonic-gate if (v.v_part[idx].p_size != totsize) { 48310Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_FULL, 48320Sstevel@tonic-gate devname)) { 48330Sstevel@tonic-gate (void) close(fd); 48340Sstevel@tonic-gate exit(1); 48350Sstevel@tonic-gate } 48360Sstevel@tonic-gate } 48370Sstevel@tonic-gate } 48380Sstevel@tonic-gate } 48390Sstevel@tonic-gate 48400Sstevel@tonic-gate 48410Sstevel@tonic-gate /* 48420Sstevel@tonic-gate * get_node 48430Sstevel@tonic-gate * Called from main to construct the name of the device node to open. 48440Sstevel@tonic-gate * Initially tries to stat the node exactly as provided, if that fails 48450Sstevel@tonic-gate * we prepend the default path (/dev/rdsk/). 48460Sstevel@tonic-gate */ 48470Sstevel@tonic-gate static char * 48480Sstevel@tonic-gate get_node(char *devname) 48490Sstevel@tonic-gate { 48500Sstevel@tonic-gate char *node; 48510Sstevel@tonic-gate struct stat statbuf; 48520Sstevel@tonic-gate size_t space; 48530Sstevel@tonic-gate 48540Sstevel@tonic-gate /* Don't do anything if we are skipping device checks */ 48550Sstevel@tonic-gate if (io_image) 48560Sstevel@tonic-gate return (devname); 48570Sstevel@tonic-gate 48580Sstevel@tonic-gate node = devname; 48590Sstevel@tonic-gate 48600Sstevel@tonic-gate /* Try the node as provided first */ 48610Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 48620Sstevel@tonic-gate /* 48630Sstevel@tonic-gate * Copy the passed in string to a new buffer, prepend the 48640Sstevel@tonic-gate * default path and try again. 48650Sstevel@tonic-gate */ 48660Sstevel@tonic-gate space = strlen(DEFAULT_PATH) + strlen(devname) + 1; 48670Sstevel@tonic-gate 48680Sstevel@tonic-gate if ((node = malloc(space)) == NULL) { 4869251Slclee (void) fprintf(stderr, "fdisk: Unable to obtain memory " 48700Sstevel@tonic-gate "for device node.\n"); 48710Sstevel@tonic-gate exit(1); 48720Sstevel@tonic-gate } 48730Sstevel@tonic-gate 48740Sstevel@tonic-gate /* Copy over the default path and the provided node */ 48750Sstevel@tonic-gate (void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH)); 48760Sstevel@tonic-gate space -= strlen(DEFAULT_PATH); 48770Sstevel@tonic-gate (void) strlcpy(node + strlen(DEFAULT_PATH), devname, space); 48780Sstevel@tonic-gate 48790Sstevel@tonic-gate /* Try to stat it again */ 48800Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 48810Sstevel@tonic-gate /* Failed all options, give up */ 4882251Slclee (void) fprintf(stderr, 4883251Slclee "fdisk: Cannot stat device %s.\n", 48840Sstevel@tonic-gate devname); 48850Sstevel@tonic-gate exit(1); 48860Sstevel@tonic-gate } 48870Sstevel@tonic-gate } 48880Sstevel@tonic-gate 48890Sstevel@tonic-gate /* Make sure the device specified is the raw device */ 48900Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 4891251Slclee (void) fprintf(stderr, 4892251Slclee "fdisk: %s must be a raw device.\n", node); 48930Sstevel@tonic-gate exit(1); 48940Sstevel@tonic-gate } 48950Sstevel@tonic-gate 48960Sstevel@tonic-gate return (node); 48970Sstevel@tonic-gate } 489810021SSheshadri.Vasudevan@Sun.COM 489910021SSheshadri.Vasudevan@Sun.COM #ifdef i386 490010021SSheshadri.Vasudevan@Sun.COM static void 490110021SSheshadri.Vasudevan@Sun.COM preach_and_continue() 490210021SSheshadri.Vasudevan@Sun.COM { 490310021SSheshadri.Vasudevan@Sun.COM (void) fprintf(stderr, "There are mounted logical drives. Committing " 490410021SSheshadri.Vasudevan@Sun.COM "changes now can lead to inconsistancy in internal system state " 490510021SSheshadri.Vasudevan@Sun.COM "which can eventually cause data loss or corruption. Unmount all " 490610021SSheshadri.Vasudevan@Sun.COM "logical drives and try committing the changes again.\n"); 490710021SSheshadri.Vasudevan@Sun.COM ext_read_input(s); 490810021SSheshadri.Vasudevan@Sun.COM } 490910021SSheshadri.Vasudevan@Sun.COM 491010021SSheshadri.Vasudevan@Sun.COM /* 491110021SSheshadri.Vasudevan@Sun.COM * Convert a given partition ID to an descriptive string. 491210021SSheshadri.Vasudevan@Sun.COM * Just an index into the partition types table. 491310021SSheshadri.Vasudevan@Sun.COM */ 491410021SSheshadri.Vasudevan@Sun.COM void 491510021SSheshadri.Vasudevan@Sun.COM id_to_name(uchar_t sysid, char *buffer) 491610021SSheshadri.Vasudevan@Sun.COM { 491710021SSheshadri.Vasudevan@Sun.COM strcpy(buffer, fdisk_part_types[sysid]); 491810021SSheshadri.Vasudevan@Sun.COM } 491910021SSheshadri.Vasudevan@Sun.COM 492010021SSheshadri.Vasudevan@Sun.COM /* 492110021SSheshadri.Vasudevan@Sun.COM * Procedure to check the validity of the extended partition menu option 492210021SSheshadri.Vasudevan@Sun.COM * entered by the user 492310021SSheshadri.Vasudevan@Sun.COM */ 492410021SSheshadri.Vasudevan@Sun.COM static int 492510021SSheshadri.Vasudevan@Sun.COM ext_invalid_option(char ch) 492610021SSheshadri.Vasudevan@Sun.COM { 492710021SSheshadri.Vasudevan@Sun.COM char *p; 492810021SSheshadri.Vasudevan@Sun.COM 492910021SSheshadri.Vasudevan@Sun.COM p = strchr(ext_part_menu_opts, tolower(ch)); 493010021SSheshadri.Vasudevan@Sun.COM 493110021SSheshadri.Vasudevan@Sun.COM if (p == NULL) { 493210021SSheshadri.Vasudevan@Sun.COM return (1); 493310021SSheshadri.Vasudevan@Sun.COM } 493410021SSheshadri.Vasudevan@Sun.COM return (0); 493510021SSheshadri.Vasudevan@Sun.COM } 493610021SSheshadri.Vasudevan@Sun.COM 493710021SSheshadri.Vasudevan@Sun.COM /* 493810021SSheshadri.Vasudevan@Sun.COM * Read 16 bytes of the input (assuming that no valid user input spans more 493910021SSheshadri.Vasudevan@Sun.COM * than that). Flush the input stream, so that the next read does not reap 494010021SSheshadri.Vasudevan@Sun.COM * stale data from the previous input that was not processed. 494110021SSheshadri.Vasudevan@Sun.COM * Note that fgets also reads the trailing '\n' 494210021SSheshadri.Vasudevan@Sun.COM */ 494310021SSheshadri.Vasudevan@Sun.COM static void 494410021SSheshadri.Vasudevan@Sun.COM ext_read_input(char *buf) 494510021SSheshadri.Vasudevan@Sun.COM { 494610021SSheshadri.Vasudevan@Sun.COM fgets(buf, 16, stdin); 494710021SSheshadri.Vasudevan@Sun.COM fflush(stdin); 494810021SSheshadri.Vasudevan@Sun.COM } 494910021SSheshadri.Vasudevan@Sun.COM 495010021SSheshadri.Vasudevan@Sun.COM /* 495110021SSheshadri.Vasudevan@Sun.COM * Procedure to read and validate the user option at the extended partition menu 495210021SSheshadri.Vasudevan@Sun.COM */ 495310021SSheshadri.Vasudevan@Sun.COM static int 495410021SSheshadri.Vasudevan@Sun.COM ext_read_options(char *buf) 495510021SSheshadri.Vasudevan@Sun.COM { 495610021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 495710021SSheshadri.Vasudevan@Sun.COM if ((strlen(buf) != 2) || (ext_invalid_option(buf[0]))) { 495810021SSheshadri.Vasudevan@Sun.COM printf("\nUnknown Command\n"); 495910021SSheshadri.Vasudevan@Sun.COM return (-1); 496010021SSheshadri.Vasudevan@Sun.COM } 496110021SSheshadri.Vasudevan@Sun.COM return (0); 496210021SSheshadri.Vasudevan@Sun.COM } 496310021SSheshadri.Vasudevan@Sun.COM 496410021SSheshadri.Vasudevan@Sun.COM /* 496510021SSheshadri.Vasudevan@Sun.COM * Procedure to print the list of known partition types and their IDs 496610021SSheshadri.Vasudevan@Sun.COM */ 496710021SSheshadri.Vasudevan@Sun.COM static void 496810021SSheshadri.Vasudevan@Sun.COM ext_print_part_types() 496910021SSheshadri.Vasudevan@Sun.COM { 497010021SSheshadri.Vasudevan@Sun.COM int i, rowmax, rowcount = 1; 497110021SSheshadri.Vasudevan@Sun.COM struct winsize ws; 497210021SSheshadri.Vasudevan@Sun.COM char buf[80]; 497310021SSheshadri.Vasudevan@Sun.COM 497410021SSheshadri.Vasudevan@Sun.COM /* Get the current window dimensions */ 497510021SSheshadri.Vasudevan@Sun.COM if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) < 0) { 497610021SSheshadri.Vasudevan@Sun.COM perror("ioctl"); 497710021SSheshadri.Vasudevan@Sun.COM rowmax = 20; 497810021SSheshadri.Vasudevan@Sun.COM } else { 497910021SSheshadri.Vasudevan@Sun.COM /* 498010021SSheshadri.Vasudevan@Sun.COM * Accommodate the initial headings by reducing the number of 498110021SSheshadri.Vasudevan@Sun.COM * partition IDs being printed. 498210021SSheshadri.Vasudevan@Sun.COM */ 498310021SSheshadri.Vasudevan@Sun.COM rowmax = ws.ws_row - 5; 498410021SSheshadri.Vasudevan@Sun.COM } 498510021SSheshadri.Vasudevan@Sun.COM 498610021SSheshadri.Vasudevan@Sun.COM if (rowmax < 3) { 498710021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Window size too small." 498810021SSheshadri.Vasudevan@Sun.COM " Try resizing the window\n"); 498910021SSheshadri.Vasudevan@Sun.COM return; 499010021SSheshadri.Vasudevan@Sun.COM } 499110021SSheshadri.Vasudevan@Sun.COM 499210021SSheshadri.Vasudevan@Sun.COM printf("List of known partition types : \n"); 499310021SSheshadri.Vasudevan@Sun.COM printf("PartID Partition Type\n"); 499410021SSheshadri.Vasudevan@Sun.COM printf("====== ==============\n"); 499510021SSheshadri.Vasudevan@Sun.COM for (i = 0; i <= FDISK_MAX_VALID_PART_ID; i++) { 499610021SSheshadri.Vasudevan@Sun.COM printf("%-3d %s\n", i, fdisk_part_types[i]); 499710021SSheshadri.Vasudevan@Sun.COM rowcount++; 499810021SSheshadri.Vasudevan@Sun.COM if (rowcount == rowmax) { 499910021SSheshadri.Vasudevan@Sun.COM /* 500010021SSheshadri.Vasudevan@Sun.COM * After the initial screen, use all the rows for 500110021SSheshadri.Vasudevan@Sun.COM * printing the partition IDs, but one. 500210021SSheshadri.Vasudevan@Sun.COM */ 500310021SSheshadri.Vasudevan@Sun.COM rowmax = ws.ws_row - 1; 500410021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "\nPress enter to see next page or 'q'" 500510021SSheshadri.Vasudevan@Sun.COM " to quit : "); 500610021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 500710021SSheshadri.Vasudevan@Sun.COM if ((strlen(buf) == 2) && (tolower(buf[0]) == 'q')) { 500810021SSheshadri.Vasudevan@Sun.COM return; 500910021SSheshadri.Vasudevan@Sun.COM } 501010021SSheshadri.Vasudevan@Sun.COM rowcount = 1; 501110021SSheshadri.Vasudevan@Sun.COM } 501210021SSheshadri.Vasudevan@Sun.COM } 501310021SSheshadri.Vasudevan@Sun.COM } 501410021SSheshadri.Vasudevan@Sun.COM 501510021SSheshadri.Vasudevan@Sun.COM static void 501610021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(int *pno) 501710021SSheshadri.Vasudevan@Sun.COM { 501810021SSheshadri.Vasudevan@Sun.COM char buf[80]; 501910021SSheshadri.Vasudevan@Sun.COM int len, i; 502010021SSheshadri.Vasudevan@Sun.COM 502110021SSheshadri.Vasudevan@Sun.COM for (;;) { 502210021SSheshadri.Vasudevan@Sun.COM printf("Enter the partition number : "); 502310021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 502410021SSheshadri.Vasudevan@Sun.COM 502510021SSheshadri.Vasudevan@Sun.COM len = strlen(buf); 502610021SSheshadri.Vasudevan@Sun.COM 502710021SSheshadri.Vasudevan@Sun.COM /* Check length of the input */ 502810021SSheshadri.Vasudevan@Sun.COM if ((len < 2) || (len > (FDISK_MAX_VALID_PART_NUM_DIGITS+1))) { 502910021SSheshadri.Vasudevan@Sun.COM goto print_error_and_continue; 503010021SSheshadri.Vasudevan@Sun.COM } 503110021SSheshadri.Vasudevan@Sun.COM 503210021SSheshadri.Vasudevan@Sun.COM /* Check if there is a non-digit in the input */ 503310021SSheshadri.Vasudevan@Sun.COM for (i = 0; i < len-1; i++) { 503410021SSheshadri.Vasudevan@Sun.COM if (!isdigit(buf[i])) { 503510021SSheshadri.Vasudevan@Sun.COM goto print_error_and_continue; 503610021SSheshadri.Vasudevan@Sun.COM } 503710021SSheshadri.Vasudevan@Sun.COM } 503810021SSheshadri.Vasudevan@Sun.COM 503910021SSheshadri.Vasudevan@Sun.COM *pno = atoi(buf); 504010021SSheshadri.Vasudevan@Sun.COM 504110021SSheshadri.Vasudevan@Sun.COM if ((*pno <= FD_NUMPART) || 504210021SSheshadri.Vasudevan@Sun.COM *pno > (fdisk_get_logical_drive_count(epp) + FD_NUMPART)) { 504310021SSheshadri.Vasudevan@Sun.COM goto print_error_and_continue; 504410021SSheshadri.Vasudevan@Sun.COM } 504510021SSheshadri.Vasudevan@Sun.COM 504610021SSheshadri.Vasudevan@Sun.COM break; 504710021SSheshadri.Vasudevan@Sun.COM print_error_and_continue: 504810021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition number\n"); 504910021SSheshadri.Vasudevan@Sun.COM continue; 505010021SSheshadri.Vasudevan@Sun.COM } 505110021SSheshadri.Vasudevan@Sun.COM } 505210021SSheshadri.Vasudevan@Sun.COM 505310021SSheshadri.Vasudevan@Sun.COM static void 505410021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(uchar_t *partid) 505510021SSheshadri.Vasudevan@Sun.COM { 505610021SSheshadri.Vasudevan@Sun.COM char buf[80]; 505710021SSheshadri.Vasudevan@Sun.COM int len, i, id; 505810021SSheshadri.Vasudevan@Sun.COM 505910021SSheshadri.Vasudevan@Sun.COM for (;;) { 506010021SSheshadri.Vasudevan@Sun.COM printf("Enter the ID ( Type I for list of partition IDs ) : "); 506110021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 506210021SSheshadri.Vasudevan@Sun.COM len = strlen(buf); 506310021SSheshadri.Vasudevan@Sun.COM 506410021SSheshadri.Vasudevan@Sun.COM if ((len < 2) || (len > (FDISK_MAX_VALID_PART_ID_DIGITS + 1))) { 506510021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition ID\n"); 506610021SSheshadri.Vasudevan@Sun.COM continue; 506710021SSheshadri.Vasudevan@Sun.COM } 506810021SSheshadri.Vasudevan@Sun.COM 506910021SSheshadri.Vasudevan@Sun.COM if ((len == 2) && (toupper(buf[0]) == 'I')) { 507010021SSheshadri.Vasudevan@Sun.COM ext_print_part_types(); 507110021SSheshadri.Vasudevan@Sun.COM continue; 507210021SSheshadri.Vasudevan@Sun.COM } 507310021SSheshadri.Vasudevan@Sun.COM 507410021SSheshadri.Vasudevan@Sun.COM /* Check if there is a non-digit in the input */ 507510021SSheshadri.Vasudevan@Sun.COM for (i = 0; i < len-1; i++) { 507610021SSheshadri.Vasudevan@Sun.COM if (!isdigit(buf[i])) { 507710021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition ID\n"); 507810021SSheshadri.Vasudevan@Sun.COM break; 507910021SSheshadri.Vasudevan@Sun.COM } 508010021SSheshadri.Vasudevan@Sun.COM } 508110021SSheshadri.Vasudevan@Sun.COM 508210021SSheshadri.Vasudevan@Sun.COM if (i < len - 1) { 508310021SSheshadri.Vasudevan@Sun.COM continue; 508410021SSheshadri.Vasudevan@Sun.COM } 508510021SSheshadri.Vasudevan@Sun.COM 508610021SSheshadri.Vasudevan@Sun.COM /* Check if the (now) valid number is greater than the limit */ 508710021SSheshadri.Vasudevan@Sun.COM if ((id = atoi(buf)) > FDISK_MAX_VALID_PART_ID) { 508810021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition ID\n"); 508910021SSheshadri.Vasudevan@Sun.COM continue; 509010021SSheshadri.Vasudevan@Sun.COM } 509110021SSheshadri.Vasudevan@Sun.COM 509210021SSheshadri.Vasudevan@Sun.COM *partid = (uchar_t)id; 509310021SSheshadri.Vasudevan@Sun.COM 509410021SSheshadri.Vasudevan@Sun.COM /* Disallow multiple extended partitions */ 509510021SSheshadri.Vasudevan@Sun.COM if (fdisk_is_dos_extended(*partid)) { 509610021SSheshadri.Vasudevan@Sun.COM printf("Multiple extended partitions not allowed\n"); 509710021SSheshadri.Vasudevan@Sun.COM continue; 509810021SSheshadri.Vasudevan@Sun.COM } 509910021SSheshadri.Vasudevan@Sun.COM 510010021SSheshadri.Vasudevan@Sun.COM /* Disallow EFI partitions within extended partition */ 510110021SSheshadri.Vasudevan@Sun.COM if (*partid == EFI_PMBR) { 510210021SSheshadri.Vasudevan@Sun.COM printf("EFI partitions within an extended partition" 510310021SSheshadri.Vasudevan@Sun.COM " is not allowed\n"); 510410021SSheshadri.Vasudevan@Sun.COM continue; 510510021SSheshadri.Vasudevan@Sun.COM } 510610021SSheshadri.Vasudevan@Sun.COM 510710021SSheshadri.Vasudevan@Sun.COM return; /* Valid partition ID is in partid */ 510810021SSheshadri.Vasudevan@Sun.COM } 510910021SSheshadri.Vasudevan@Sun.COM } 511010021SSheshadri.Vasudevan@Sun.COM 511110021SSheshadri.Vasudevan@Sun.COM static void 511210021SSheshadri.Vasudevan@Sun.COM delete_logical_drive() 511310021SSheshadri.Vasudevan@Sun.COM { 511410021SSheshadri.Vasudevan@Sun.COM int pno; 511510021SSheshadri.Vasudevan@Sun.COM 511610021SSheshadri.Vasudevan@Sun.COM if (!fdisk_get_logical_drive_count(epp)) { 511710021SSheshadri.Vasudevan@Sun.COM printf("\nNo logical drives defined.\n"); 511810021SSheshadri.Vasudevan@Sun.COM return; 511910021SSheshadri.Vasudevan@Sun.COM } 512010021SSheshadri.Vasudevan@Sun.COM 512110021SSheshadri.Vasudevan@Sun.COM printf("\n"); 512210021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(&pno); 512310021SSheshadri.Vasudevan@Sun.COM fdisk_delete_logical_drive(epp, pno); 512410021SSheshadri.Vasudevan@Sun.COM printf("Partition %d deleted\n", pno); 512510021SSheshadri.Vasudevan@Sun.COM } 512610021SSheshadri.Vasudevan@Sun.COM 512710021SSheshadri.Vasudevan@Sun.COM static int 512810021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_start(uint32_t *begsec) 512910021SSheshadri.Vasudevan@Sun.COM { 513010021SSheshadri.Vasudevan@Sun.COM char buf[80]; 513110021SSheshadri.Vasudevan@Sun.COM int ret, len, i; 513210021SSheshadri.Vasudevan@Sun.COM uint32_t begcyl; 513310021SSheshadri.Vasudevan@Sun.COM uint32_t first_free_cyl; 513410021SSheshadri.Vasudevan@Sun.COM uint32_t first_free_sec; 513510021SSheshadri.Vasudevan@Sun.COM 513610021SSheshadri.Vasudevan@Sun.COM ret = fdisk_ext_find_first_free_sec(epp, &first_free_sec); 513710021SSheshadri.Vasudevan@Sun.COM if (ret != FDISK_SUCCESS) { 513810021SSheshadri.Vasudevan@Sun.COM return (ret); 513910021SSheshadri.Vasudevan@Sun.COM } 514010021SSheshadri.Vasudevan@Sun.COM 514110021SSheshadri.Vasudevan@Sun.COM first_free_cyl = FDISK_SECT_TO_CYL(epp, first_free_sec); 514210021SSheshadri.Vasudevan@Sun.COM for (;;) { 514310021SSheshadri.Vasudevan@Sun.COM printf("Enter the beginning cylinder (Default - %d) : ", 514410021SSheshadri.Vasudevan@Sun.COM first_free_cyl); 514510021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 514610021SSheshadri.Vasudevan@Sun.COM len = strlen(buf); 514710021SSheshadri.Vasudevan@Sun.COM if (len == 1) { /* User accepted the default value */ 514810021SSheshadri.Vasudevan@Sun.COM *begsec = first_free_sec; 514910021SSheshadri.Vasudevan@Sun.COM return (FDISK_SUCCESS); 515010021SSheshadri.Vasudevan@Sun.COM } 515110021SSheshadri.Vasudevan@Sun.COM 515210021SSheshadri.Vasudevan@Sun.COM if (len > (FDISK_MAX_VALID_CYL_NUM_DIGITS + 1)) { 515310021SSheshadri.Vasudevan@Sun.COM printf("Input too long\n"); 515410021SSheshadri.Vasudevan@Sun.COM printf("Invalid beginning cylinder number\n"); 515510021SSheshadri.Vasudevan@Sun.COM continue; 515610021SSheshadri.Vasudevan@Sun.COM } 515710021SSheshadri.Vasudevan@Sun.COM /* Check if there is a non-digit in the input */ 515810021SSheshadri.Vasudevan@Sun.COM for (i = 0; i < len - 1; i++) { 515910021SSheshadri.Vasudevan@Sun.COM if (!isdigit(buf[i])) { 516010021SSheshadri.Vasudevan@Sun.COM printf("Invalid beginning cylinder number\n"); 516110021SSheshadri.Vasudevan@Sun.COM break; 516210021SSheshadri.Vasudevan@Sun.COM } 516310021SSheshadri.Vasudevan@Sun.COM } 516410021SSheshadri.Vasudevan@Sun.COM if (i < len - 1) { 516510021SSheshadri.Vasudevan@Sun.COM continue; 516610021SSheshadri.Vasudevan@Sun.COM } 516710021SSheshadri.Vasudevan@Sun.COM 516810021SSheshadri.Vasudevan@Sun.COM begcyl = atoi(buf); 516910021SSheshadri.Vasudevan@Sun.COM ret = fdisk_ext_validate_part_start(epp, begcyl, begsec); 517010021SSheshadri.Vasudevan@Sun.COM switch (ret) { 517110021SSheshadri.Vasudevan@Sun.COM case FDISK_SUCCESS: 517210021SSheshadri.Vasudevan@Sun.COM /* 517310021SSheshadri.Vasudevan@Sun.COM * Success. 517410021SSheshadri.Vasudevan@Sun.COM * Valid beginning sector is in begsec 517510021SSheshadri.Vasudevan@Sun.COM */ 517610021SSheshadri.Vasudevan@Sun.COM break; 517710021SSheshadri.Vasudevan@Sun.COM 517810021SSheshadri.Vasudevan@Sun.COM case FDISK_EOVERLAP: 517910021SSheshadri.Vasudevan@Sun.COM printf("Partition boundary overlaps with "); 518010021SSheshadri.Vasudevan@Sun.COM printf("existing partitions\n"); 518110021SSheshadri.Vasudevan@Sun.COM printf("Invalid beginning cylinder number\n"); 518210021SSheshadri.Vasudevan@Sun.COM continue; 518310021SSheshadri.Vasudevan@Sun.COM 518410021SSheshadri.Vasudevan@Sun.COM case FDISK_EOOBOUND: 518510021SSheshadri.Vasudevan@Sun.COM printf("Cylinder boundary beyond the limits\n"); 518610021SSheshadri.Vasudevan@Sun.COM printf("Invalid beginning cylinder number\n"); 518710021SSheshadri.Vasudevan@Sun.COM continue; 518810021SSheshadri.Vasudevan@Sun.COM } 518910021SSheshadri.Vasudevan@Sun.COM return (FDISK_SUCCESS); 519010021SSheshadri.Vasudevan@Sun.COM } 519110021SSheshadri.Vasudevan@Sun.COM } 519210021SSheshadri.Vasudevan@Sun.COM 519310021SSheshadri.Vasudevan@Sun.COM /* 519410021SSheshadri.Vasudevan@Sun.COM * Algorithm : 519510021SSheshadri.Vasudevan@Sun.COM * 1. Check if the first character is a + 519610021SSheshadri.Vasudevan@Sun.COM * a) If yes, check if the last character is 'k', 'm' or 'g' 519710021SSheshadri.Vasudevan@Sun.COM * 2. If not, check if there are any non-digits 519810021SSheshadri.Vasudevan@Sun.COM * 3. Check for the length of the numeral string 519910021SSheshadri.Vasudevan@Sun.COM * 4. atoi the numeral string 520010021SSheshadri.Vasudevan@Sun.COM * 5. In case of data entered in KB, MB or GB, convert it to number of cylinders 520110021SSheshadri.Vasudevan@Sun.COM * a) Adjust size to be cylinder boundary aligned 520210021SSheshadri.Vasudevan@Sun.COM * 6. If size specifies is zero, flag error 520310021SSheshadri.Vasudevan@Sun.COM * 7. Check if the size is less than 1 cylinder 520410021SSheshadri.Vasudevan@Sun.COM * a) If yes, default the size FDISK_MIN_PART_SIZE 520510021SSheshadri.Vasudevan@Sun.COM * b) If no, Check if the size is within endcyl - begcyl 520610021SSheshadri.Vasudevan@Sun.COM */ 520710021SSheshadri.Vasudevan@Sun.COM static void 520810021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_size(uint32_t begsec, uint32_t *endsec) 520910021SSheshadri.Vasudevan@Sun.COM { 521010021SSheshadri.Vasudevan@Sun.COM char buf[80]; 521110021SSheshadri.Vasudevan@Sun.COM uint32_t tempcyl; 521210021SSheshadri.Vasudevan@Sun.COM uint32_t last_free_sec; 521310021SSheshadri.Vasudevan@Sun.COM uint32_t last_free_cyl; 521410021SSheshadri.Vasudevan@Sun.COM int i, len, ch, mbgb = 0, scale = FDISK_SECTS_PER_CYL(epp); 521510021SSheshadri.Vasudevan@Sun.COM uint64_t size = 0; 521610021SSheshadri.Vasudevan@Sun.COM int copy_len; 521710021SSheshadri.Vasudevan@Sun.COM char numbuf[FDISK_MAX_VALID_CYL_NUM_DIGITS + 1]; 521810021SSheshadri.Vasudevan@Sun.COM int sectsize = fdisk_get_disk_geom(epp, PHYSGEOM, SSIZE); 521910021SSheshadri.Vasudevan@Sun.COM uint32_t remdr, spc, poss_end; 522010021SSheshadri.Vasudevan@Sun.COM 522110021SSheshadri.Vasudevan@Sun.COM if (sectsize == EINVAL) { 522210021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Unsupported geometry statistics.\n"); 522310021SSheshadri.Vasudevan@Sun.COM exit(1); 522410021SSheshadri.Vasudevan@Sun.COM } 522510021SSheshadri.Vasudevan@Sun.COM 522610021SSheshadri.Vasudevan@Sun.COM last_free_sec = fdisk_ext_find_last_free_sec(epp, begsec); 522710021SSheshadri.Vasudevan@Sun.COM last_free_cyl = FDISK_SECT_TO_CYL(epp, last_free_sec); 522810021SSheshadri.Vasudevan@Sun.COM 522910021SSheshadri.Vasudevan@Sun.COM for (;;) { 523010021SSheshadri.Vasudevan@Sun.COM printf("Enter the size in cylinders (Default End Cylinder -"); 523110021SSheshadri.Vasudevan@Sun.COM printf(" %u)\n", last_free_cyl); 523210021SSheshadri.Vasudevan@Sun.COM printf("Type +<size>K, +<size>M or +<size>G to enter size in"); 523310021SSheshadri.Vasudevan@Sun.COM printf("KB, MB or GB : "); 523410021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 523510021SSheshadri.Vasudevan@Sun.COM len = strlen(buf); 523610021SSheshadri.Vasudevan@Sun.COM mbgb = 0; 523710021SSheshadri.Vasudevan@Sun.COM scale = FDISK_SECTS_PER_CYL(epp); 523810021SSheshadri.Vasudevan@Sun.COM 523910021SSheshadri.Vasudevan@Sun.COM if (len == 1) { /* User accepted the default value */ 524010021SSheshadri.Vasudevan@Sun.COM *endsec = last_free_sec; 524110021SSheshadri.Vasudevan@Sun.COM return; 524210021SSheshadri.Vasudevan@Sun.COM } 524310021SSheshadri.Vasudevan@Sun.COM 524410021SSheshadri.Vasudevan@Sun.COM copy_len = len - 1; 524510021SSheshadri.Vasudevan@Sun.COM 524610021SSheshadri.Vasudevan@Sun.COM if ((buf[0] == '+') && (isdigit(buf[1]))) { 524710021SSheshadri.Vasudevan@Sun.COM copy_len--; 524810021SSheshadri.Vasudevan@Sun.COM if ((ch = toupper(buf[len - 2])) == 'B') { 524910021SSheshadri.Vasudevan@Sun.COM ch = toupper(buf[len - 3]); 525010021SSheshadri.Vasudevan@Sun.COM copy_len--; 525110021SSheshadri.Vasudevan@Sun.COM } 525210021SSheshadri.Vasudevan@Sun.COM 525310021SSheshadri.Vasudevan@Sun.COM if (!((ch == 'K') || (ch == 'M') || (ch == 'G'))) { 525410021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition size\n"); 525510021SSheshadri.Vasudevan@Sun.COM continue; 525610021SSheshadri.Vasudevan@Sun.COM } 525710021SSheshadri.Vasudevan@Sun.COM 525810021SSheshadri.Vasudevan@Sun.COM copy_len--; 525910021SSheshadri.Vasudevan@Sun.COM mbgb = 1; 526010021SSheshadri.Vasudevan@Sun.COM scale = ((ch == 'K') ? FDISK_KB : 526110021SSheshadri.Vasudevan@Sun.COM ((ch == 'M') ? FDISK_MB : FDISK_GB)); 526210021SSheshadri.Vasudevan@Sun.COM } 526310021SSheshadri.Vasudevan@Sun.COM 526410021SSheshadri.Vasudevan@Sun.COM if (copy_len > FDISK_MAX_VALID_CYL_NUM_DIGITS) { 526510021SSheshadri.Vasudevan@Sun.COM printf("Input too long\n"); 526610021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition size\n"); 526710021SSheshadri.Vasudevan@Sun.COM continue; 526810021SSheshadri.Vasudevan@Sun.COM } 526910021SSheshadri.Vasudevan@Sun.COM 527010021SSheshadri.Vasudevan@Sun.COM strncpy(numbuf, &buf[mbgb], copy_len); 527110021SSheshadri.Vasudevan@Sun.COM numbuf[copy_len] = '\0'; 527210021SSheshadri.Vasudevan@Sun.COM 527310021SSheshadri.Vasudevan@Sun.COM for (i = mbgb; i < copy_len + mbgb; i++) { 527410021SSheshadri.Vasudevan@Sun.COM if (!isdigit(buf[i])) { 527510021SSheshadri.Vasudevan@Sun.COM break; 527610021SSheshadri.Vasudevan@Sun.COM } 527710021SSheshadri.Vasudevan@Sun.COM } 527810021SSheshadri.Vasudevan@Sun.COM 527910021SSheshadri.Vasudevan@Sun.COM if (i < copy_len + mbgb) { 528010021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition size\n"); 528110021SSheshadri.Vasudevan@Sun.COM continue; 528210021SSheshadri.Vasudevan@Sun.COM } 528310021SSheshadri.Vasudevan@Sun.COM 528410021SSheshadri.Vasudevan@Sun.COM size = (atoll(numbuf) * (scale)); 528510021SSheshadri.Vasudevan@Sun.COM 528610021SSheshadri.Vasudevan@Sun.COM if (size == 0) { 528710021SSheshadri.Vasudevan@Sun.COM printf("Zero size is invalid\n"); 528810021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition size\n"); 528910021SSheshadri.Vasudevan@Sun.COM continue; 529010021SSheshadri.Vasudevan@Sun.COM } 529110021SSheshadri.Vasudevan@Sun.COM 529210021SSheshadri.Vasudevan@Sun.COM if (mbgb) { 529310021SSheshadri.Vasudevan@Sun.COM size /= sectsize; 529410021SSheshadri.Vasudevan@Sun.COM } 529510021SSheshadri.Vasudevan@Sun.COM 529610021SSheshadri.Vasudevan@Sun.COM if (size > (last_free_sec - begsec + 1)) { 529710021SSheshadri.Vasudevan@Sun.COM printf("Cylinder boundary beyond the limits"); 529810021SSheshadri.Vasudevan@Sun.COM printf(" or overlaps with existing"); 529910021SSheshadri.Vasudevan@Sun.COM printf(" partitions\n"); 530010021SSheshadri.Vasudevan@Sun.COM printf("Invalid partition size\n"); 530110021SSheshadri.Vasudevan@Sun.COM continue; 530210021SSheshadri.Vasudevan@Sun.COM } 530310021SSheshadri.Vasudevan@Sun.COM 530410021SSheshadri.Vasudevan@Sun.COM /* 530510021SSheshadri.Vasudevan@Sun.COM * Adjust the ending sector such that there are no partial 530610021SSheshadri.Vasudevan@Sun.COM * cylinders allocated. But at the same time, make sure it 530710021SSheshadri.Vasudevan@Sun.COM * doesn't over shoot boundaries. 530810021SSheshadri.Vasudevan@Sun.COM */ 530910021SSheshadri.Vasudevan@Sun.COM spc = FDISK_SECTS_PER_CYL(epp); 531010021SSheshadri.Vasudevan@Sun.COM poss_end = begsec + size - 1; 531110021SSheshadri.Vasudevan@Sun.COM if (remdr = (poss_end % spc)) { 531210021SSheshadri.Vasudevan@Sun.COM poss_end += spc - remdr - 1; 531310021SSheshadri.Vasudevan@Sun.COM } 531410021SSheshadri.Vasudevan@Sun.COM *endsec = (poss_end > last_free_sec) ? last_free_sec : 531510021SSheshadri.Vasudevan@Sun.COM poss_end; 531610021SSheshadri.Vasudevan@Sun.COM 531710021SSheshadri.Vasudevan@Sun.COM return; 531810021SSheshadri.Vasudevan@Sun.COM } 531910021SSheshadri.Vasudevan@Sun.COM } 532010021SSheshadri.Vasudevan@Sun.COM 532110021SSheshadri.Vasudevan@Sun.COM /* 532210021SSheshadri.Vasudevan@Sun.COM * ALGORITHM: 532310021SSheshadri.Vasudevan@Sun.COM * 1. Get the starting and ending sectors/cylinder of the extended partition. 532410021SSheshadri.Vasudevan@Sun.COM * 2. Keep track of the first free sector/cylinder 532510021SSheshadri.Vasudevan@Sun.COM * 3. Allow the user to specify the beginning cylinder of the new partition 532610021SSheshadri.Vasudevan@Sun.COM * 4. Check for the validity of the entered data 532710021SSheshadri.Vasudevan@Sun.COM * a) If it is non-numeric 532810021SSheshadri.Vasudevan@Sun.COM * b) If it is beyond the extended partition limits 532910021SSheshadri.Vasudevan@Sun.COM * c) If it overlaps with the current logical drives 533010021SSheshadri.Vasudevan@Sun.COM * 5. Allow the user to specify the size in cylinders/ human readable form 533110021SSheshadri.Vasudevan@Sun.COM * 6. Check for the validity of the entered data 533210021SSheshadri.Vasudevan@Sun.COM * a) If it is non-numeric 533310021SSheshadri.Vasudevan@Sun.COM * b) If it is beyond the extended partition limits 533410021SSheshadri.Vasudevan@Sun.COM * c) If it overlaps with the current logical drives 533510021SSheshadri.Vasudevan@Sun.COM * d) If it is a number lesser than the starting cylinder 533610021SSheshadri.Vasudevan@Sun.COM * 7. Request partition ID for the new partition. 533710021SSheshadri.Vasudevan@Sun.COM * 8. Update the first free cylinder available 533810021SSheshadri.Vasudevan@Sun.COM * 9. Display Success message 533910021SSheshadri.Vasudevan@Sun.COM */ 534010021SSheshadri.Vasudevan@Sun.COM 534110021SSheshadri.Vasudevan@Sun.COM static void 534210021SSheshadri.Vasudevan@Sun.COM add_logical_drive() 534310021SSheshadri.Vasudevan@Sun.COM { 534410021SSheshadri.Vasudevan@Sun.COM uint32_t begsec, endsec; 534510021SSheshadri.Vasudevan@Sun.COM uchar_t partid; 534610021SSheshadri.Vasudevan@Sun.COM char buf[80]; 534710021SSheshadri.Vasudevan@Sun.COM int rval; 534810021SSheshadri.Vasudevan@Sun.COM 534910021SSheshadri.Vasudevan@Sun.COM if (fdisk_get_logical_drive_count(epp) >= MAX_EXT_PARTS) { 535010021SSheshadri.Vasudevan@Sun.COM printf("\nNumber of logical drives exceeds limit of %d.\n", 535110021SSheshadri.Vasudevan@Sun.COM MAX_EXT_PARTS); 535210021SSheshadri.Vasudevan@Sun.COM printf("Command did not succeed. Press enter to continue\n"); 535310021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 535410021SSheshadri.Vasudevan@Sun.COM return; 535510021SSheshadri.Vasudevan@Sun.COM } 535610021SSheshadri.Vasudevan@Sun.COM 535710021SSheshadri.Vasudevan@Sun.COM printf("\n"); 535810021SSheshadri.Vasudevan@Sun.COM rval = ext_read_valid_partition_start(&begsec); 535910021SSheshadri.Vasudevan@Sun.COM switch (rval) { 536010021SSheshadri.Vasudevan@Sun.COM case FDISK_SUCCESS: 536110021SSheshadri.Vasudevan@Sun.COM break; 536210021SSheshadri.Vasudevan@Sun.COM 536310021SSheshadri.Vasudevan@Sun.COM case FDISK_EOOBOUND: 536410021SSheshadri.Vasudevan@Sun.COM printf("\nNo space left in the extended partition\n"); 536510021SSheshadri.Vasudevan@Sun.COM printf("Press enter to continue\n"); 536610021SSheshadri.Vasudevan@Sun.COM ext_read_input(buf); 536710021SSheshadri.Vasudevan@Sun.COM return; 536810021SSheshadri.Vasudevan@Sun.COM } 536910021SSheshadri.Vasudevan@Sun.COM 537010021SSheshadri.Vasudevan@Sun.COM ext_read_valid_partition_size(begsec, &endsec); 537110021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(&partid); 537210021SSheshadri.Vasudevan@Sun.COM fdisk_add_logical_drive(epp, begsec, endsec, partid); 537310021SSheshadri.Vasudevan@Sun.COM 537410021SSheshadri.Vasudevan@Sun.COM printf("New partition with ID %d added\n", partid); 537510021SSheshadri.Vasudevan@Sun.COM } 537610021SSheshadri.Vasudevan@Sun.COM 537710021SSheshadri.Vasudevan@Sun.COM static void 537810021SSheshadri.Vasudevan@Sun.COM ext_change_logical_drive_id() 537910021SSheshadri.Vasudevan@Sun.COM { 538010021SSheshadri.Vasudevan@Sun.COM int pno; 538110021SSheshadri.Vasudevan@Sun.COM uchar_t partid; 538210021SSheshadri.Vasudevan@Sun.COM 538310021SSheshadri.Vasudevan@Sun.COM if (!fdisk_get_logical_drive_count(epp)) { 538410021SSheshadri.Vasudevan@Sun.COM printf("\nNo logical drives defined.\n"); 538510021SSheshadri.Vasudevan@Sun.COM return; 538610021SSheshadri.Vasudevan@Sun.COM } 538710021SSheshadri.Vasudevan@Sun.COM 538810021SSheshadri.Vasudevan@Sun.COM printf("\n"); 538910021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_num(&pno); 539010021SSheshadri.Vasudevan@Sun.COM ext_read_valid_part_id(&partid); 539110021SSheshadri.Vasudevan@Sun.COM fdisk_change_logical_drive_id(epp, pno, partid); 539210021SSheshadri.Vasudevan@Sun.COM 539310021SSheshadri.Vasudevan@Sun.COM printf("Partition ID of partition %d changed to %d\n", pno, partid); 539410021SSheshadri.Vasudevan@Sun.COM } 539510021SSheshadri.Vasudevan@Sun.COM 539610021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG 539710021SSheshadri.Vasudevan@Sun.COM static void 539810021SSheshadri.Vasudevan@Sun.COM ext_print_logdrive_layout_debug() 539910021SSheshadri.Vasudevan@Sun.COM { 540010021SSheshadri.Vasudevan@Sun.COM int pno; 540110021SSheshadri.Vasudevan@Sun.COM char namebuff[255]; 540210021SSheshadri.Vasudevan@Sun.COM logical_drive_t *head = fdisk_get_ld_head(epp); 540310021SSheshadri.Vasudevan@Sun.COM logical_drive_t *temp; 540410021SSheshadri.Vasudevan@Sun.COM 540510021SSheshadri.Vasudevan@Sun.COM if (!fdisk_get_logical_drive_count(epp)) { 540610021SSheshadri.Vasudevan@Sun.COM printf("\nNo logical drives defined.\n"); 540710021SSheshadri.Vasudevan@Sun.COM return; 540810021SSheshadri.Vasudevan@Sun.COM } 540910021SSheshadri.Vasudevan@Sun.COM 541010021SSheshadri.Vasudevan@Sun.COM printf("\n\n"); 541110021SSheshadri.Vasudevan@Sun.COM puts("# start block end block abs start abs end OSType"); 541210021SSheshadri.Vasudevan@Sun.COM for (temp = head, pno = 5; temp != NULL; temp = temp->next, pno++) { 541310021SSheshadri.Vasudevan@Sun.COM /* Print the logical drive details */ 541410021SSheshadri.Vasudevan@Sun.COM id_to_name(temp->parts[0].systid, namebuff); 541510021SSheshadri.Vasudevan@Sun.COM printf("%d: %.10u %.10u %.10u %.10u", 541610021SSheshadri.Vasudevan@Sun.COM pno, 541710021SSheshadri.Vasudevan@Sun.COM LE_32(temp->parts[0].relsect), 541810021SSheshadri.Vasudevan@Sun.COM LE_32(temp->parts[0].numsect), 541910021SSheshadri.Vasudevan@Sun.COM temp->abs_secnum, 542010021SSheshadri.Vasudevan@Sun.COM temp->abs_secnum + temp->numsect - 1 + 542110021SSheshadri.Vasudevan@Sun.COM MAX_LOGDRIVE_OFFSET); 542210021SSheshadri.Vasudevan@Sun.COM printf(" %s\n", namebuff); 542310021SSheshadri.Vasudevan@Sun.COM /* 542410021SSheshadri.Vasudevan@Sun.COM * Print the second entry in the EBR which is information 542510021SSheshadri.Vasudevan@Sun.COM * about the location and the size of the next extended 542610021SSheshadri.Vasudevan@Sun.COM * partition. 542710021SSheshadri.Vasudevan@Sun.COM */ 542810021SSheshadri.Vasudevan@Sun.COM id_to_name(temp->parts[1].systid, namebuff); 542910021SSheshadri.Vasudevan@Sun.COM printf("%d: %.10u %.10u %.10s %.10s", 543010021SSheshadri.Vasudevan@Sun.COM pno, 543110021SSheshadri.Vasudevan@Sun.COM LE_32(temp->parts[1].relsect), 543210021SSheshadri.Vasudevan@Sun.COM LE_32(temp->parts[1].numsect), 543310021SSheshadri.Vasudevan@Sun.COM " ", " "); 543410021SSheshadri.Vasudevan@Sun.COM printf(" %s\n", namebuff); 543510021SSheshadri.Vasudevan@Sun.COM } 543610021SSheshadri.Vasudevan@Sun.COM } 543710021SSheshadri.Vasudevan@Sun.COM #endif 543810021SSheshadri.Vasudevan@Sun.COM 543910021SSheshadri.Vasudevan@Sun.COM static void 544010021SSheshadri.Vasudevan@Sun.COM ext_print_logical_drive_layout() 544110021SSheshadri.Vasudevan@Sun.COM { 544210021SSheshadri.Vasudevan@Sun.COM int sysid; 544310021SSheshadri.Vasudevan@Sun.COM unsigned int startcyl, endcyl, length, percent, remainder; 544410021SSheshadri.Vasudevan@Sun.COM logical_drive_t *temp; 544510682SSheshadri.Vasudevan@Sun.COM uint32_t part_start; 544610021SSheshadri.Vasudevan@Sun.COM struct ipart *fpart; 544710021SSheshadri.Vasudevan@Sun.COM char namebuff[255]; 544810021SSheshadri.Vasudevan@Sun.COM int numcyl = fdisk_get_disk_geom(epp, PHYSGEOM, NCYL); 544910021SSheshadri.Vasudevan@Sun.COM int pno; 545010021SSheshadri.Vasudevan@Sun.COM 545110021SSheshadri.Vasudevan@Sun.COM if (numcyl == EINVAL) { 545210021SSheshadri.Vasudevan@Sun.COM fprintf(stderr, "Unsupported geometry statistics.\n"); 545310021SSheshadri.Vasudevan@Sun.COM exit(1); 545410021SSheshadri.Vasudevan@Sun.COM } 545510021SSheshadri.Vasudevan@Sun.COM 545610021SSheshadri.Vasudevan@Sun.COM if (!fdisk_get_logical_drive_count(epp)) { 545710021SSheshadri.Vasudevan@Sun.COM printf("\nNo logical drives defined.\n"); 545810021SSheshadri.Vasudevan@Sun.COM return; 545910021SSheshadri.Vasudevan@Sun.COM } 546010021SSheshadri.Vasudevan@Sun.COM 546110021SSheshadri.Vasudevan@Sun.COM printf("\n"); 546210021SSheshadri.Vasudevan@Sun.COM printf("Number of cylinders in disk : %u\n", numcyl); 546310021SSheshadri.Vasudevan@Sun.COM printf("Beginning cylinder of extended partition : %u\n", 546410021SSheshadri.Vasudevan@Sun.COM fdisk_get_ext_beg_cyl(epp)); 546510021SSheshadri.Vasudevan@Sun.COM printf("Ending cylinder of extended partition : %u\n", 546610021SSheshadri.Vasudevan@Sun.COM fdisk_get_ext_end_cyl(epp)); 546710021SSheshadri.Vasudevan@Sun.COM printf("\n"); 546810021SSheshadri.Vasudevan@Sun.COM printf("Part# StartCyl EndCyl Length %% " 546910021SSheshadri.Vasudevan@Sun.COM "Part ID (Type)\n"); 547010021SSheshadri.Vasudevan@Sun.COM printf("===== ======== ======== ======= ===" 547110021SSheshadri.Vasudevan@Sun.COM " ==============\n"); 547210021SSheshadri.Vasudevan@Sun.COM for (temp = fdisk_get_ld_head(epp), pno = 5; temp != NULL; 547310021SSheshadri.Vasudevan@Sun.COM temp = temp->next, pno++) { 547410021SSheshadri.Vasudevan@Sun.COM /* Print the logical drive details */ 547510021SSheshadri.Vasudevan@Sun.COM fpart = &temp->parts[0]; 547610021SSheshadri.Vasudevan@Sun.COM sysid = fpart->systid; 547710682SSheshadri.Vasudevan@Sun.COM /* 547810682SSheshadri.Vasudevan@Sun.COM * Check if partition id 0x82 is Solaris 547910682SSheshadri.Vasudevan@Sun.COM * or a Linux swap. Print the string 548010682SSheshadri.Vasudevan@Sun.COM * accordingly. 548110682SSheshadri.Vasudevan@Sun.COM */ 548210682SSheshadri.Vasudevan@Sun.COM if (sysid == SUNIXOS) { 548310682SSheshadri.Vasudevan@Sun.COM part_start = temp->abs_secnum + 548410682SSheshadri.Vasudevan@Sun.COM temp->logdrive_offset; 548510682SSheshadri.Vasudevan@Sun.COM if (fdisk_is_linux_swap(epp, part_start, 548610682SSheshadri.Vasudevan@Sun.COM NULL) == 0) 548710682SSheshadri.Vasudevan@Sun.COM strcpy(namebuff, LINSWAPstr); 548810682SSheshadri.Vasudevan@Sun.COM else 548910682SSheshadri.Vasudevan@Sun.COM strcpy(namebuff, SUstr); 549010682SSheshadri.Vasudevan@Sun.COM } else { 549110682SSheshadri.Vasudevan@Sun.COM id_to_name(sysid, namebuff); 549210682SSheshadri.Vasudevan@Sun.COM } 549310021SSheshadri.Vasudevan@Sun.COM startcyl = temp->begcyl; 549410021SSheshadri.Vasudevan@Sun.COM endcyl = temp->endcyl; 549510021SSheshadri.Vasudevan@Sun.COM if (startcyl == endcyl) { 549610021SSheshadri.Vasudevan@Sun.COM length = 1; 549710021SSheshadri.Vasudevan@Sun.COM } else { 549810021SSheshadri.Vasudevan@Sun.COM length = endcyl - startcyl + 1; 549910021SSheshadri.Vasudevan@Sun.COM } 550010021SSheshadri.Vasudevan@Sun.COM percent = length * 100 / numcyl; 550110021SSheshadri.Vasudevan@Sun.COM if ((remainder = (length * 100 % numcyl)) != 0) { 550210021SSheshadri.Vasudevan@Sun.COM if ((remainder * 100 / numcyl) > 50) { 550310021SSheshadri.Vasudevan@Sun.COM /* round up */ 550410021SSheshadri.Vasudevan@Sun.COM percent++; 550510021SSheshadri.Vasudevan@Sun.COM } 550610021SSheshadri.Vasudevan@Sun.COM /* Else leave the percent as is since it's already */ 550710021SSheshadri.Vasudevan@Sun.COM /* rounded down */ 550810021SSheshadri.Vasudevan@Sun.COM } 550910021SSheshadri.Vasudevan@Sun.COM if (percent > 100) { 551010021SSheshadri.Vasudevan@Sun.COM percent = 100; 551110021SSheshadri.Vasudevan@Sun.COM } 551210021SSheshadri.Vasudevan@Sun.COM printf("%-5d %-8u %-8u %-7u %-3d %-3d (%-.28s)\n", 551310021SSheshadri.Vasudevan@Sun.COM pno, startcyl, endcyl, length, percent, sysid, namebuff); 551410021SSheshadri.Vasudevan@Sun.COM } 551510021SSheshadri.Vasudevan@Sun.COM #ifdef DEBUG 551610021SSheshadri.Vasudevan@Sun.COM ext_print_logdrive_layout_debug(); 551710021SSheshadri.Vasudevan@Sun.COM #endif 551810021SSheshadri.Vasudevan@Sun.COM printf("\n"); 551910021SSheshadri.Vasudevan@Sun.COM } 552010021SSheshadri.Vasudevan@Sun.COM 552110021SSheshadri.Vasudevan@Sun.COM static void 552210021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu() 552310021SSheshadri.Vasudevan@Sun.COM { 552410021SSheshadri.Vasudevan@Sun.COM printf("\n"); 552510021SSheshadri.Vasudevan@Sun.COM printf("a Add a logical drive\n"); 552610021SSheshadri.Vasudevan@Sun.COM printf("d Delete a logical drive\n"); 552710021SSheshadri.Vasudevan@Sun.COM printf("h Print this help menu\n"); 552810021SSheshadri.Vasudevan@Sun.COM printf("i Change the id of the logical drive\n"); 552910021SSheshadri.Vasudevan@Sun.COM printf("p Print the logical drive layout\n"); 553010021SSheshadri.Vasudevan@Sun.COM printf("r Return to the main fdisk menu\n"); 553110021SSheshadri.Vasudevan@Sun.COM printf(" (To commit or cancel the changes)\n"); 553210021SSheshadri.Vasudevan@Sun.COM printf("\n"); 553310021SSheshadri.Vasudevan@Sun.COM } 553410021SSheshadri.Vasudevan@Sun.COM 553510021SSheshadri.Vasudevan@Sun.COM static void 553610021SSheshadri.Vasudevan@Sun.COM ext_part_menu() 553710021SSheshadri.Vasudevan@Sun.COM { 553810021SSheshadri.Vasudevan@Sun.COM char buf[80]; 553910021SSheshadri.Vasudevan@Sun.COM uchar_t *bbsigp; 554010021SSheshadri.Vasudevan@Sun.COM static int bbsig_disp_flag = 1; 554110021SSheshadri.Vasudevan@Sun.COM 554210021SSheshadri.Vasudevan@Sun.COM int i; 554310021SSheshadri.Vasudevan@Sun.COM 554410021SSheshadri.Vasudevan@Sun.COM printf(CLR_SCR); 554510021SSheshadri.Vasudevan@Sun.COM 554610021SSheshadri.Vasudevan@Sun.COM if (fdisk_corrupt_logical_drives(epp)) { 554710021SSheshadri.Vasudevan@Sun.COM printf("One or more logical drives seem to be corrupt.\n"); 554810021SSheshadri.Vasudevan@Sun.COM printf("Displaying only sane logical drives.\n"); 554910021SSheshadri.Vasudevan@Sun.COM } 555010021SSheshadri.Vasudevan@Sun.COM 555110021SSheshadri.Vasudevan@Sun.COM if (bbsig_disp_flag && fdisk_invalid_bb_sig(epp, &bbsigp)) { 555210021SSheshadri.Vasudevan@Sun.COM printf("The following logical drives have a wrong boot block" 555310021SSheshadri.Vasudevan@Sun.COM " signature :\n\n"); 555410021SSheshadri.Vasudevan@Sun.COM for (i = 0; bbsigp[i]; i++) { 555510021SSheshadri.Vasudevan@Sun.COM printf("%d ", bbsigp[i]); 555610021SSheshadri.Vasudevan@Sun.COM } 555710021SSheshadri.Vasudevan@Sun.COM printf("\n\n"); 555810021SSheshadri.Vasudevan@Sun.COM printf("They will be corrected when you choose to commit\n"); 555910021SSheshadri.Vasudevan@Sun.COM bbsig_disp_flag = 0; 556010021SSheshadri.Vasudevan@Sun.COM } 556110021SSheshadri.Vasudevan@Sun.COM 556210021SSheshadri.Vasudevan@Sun.COM printf("Extended partition menu\n"); 556310021SSheshadri.Vasudevan@Sun.COM 556410021SSheshadri.Vasudevan@Sun.COM for (;;) { 556510021SSheshadri.Vasudevan@Sun.COM printf("\nEnter Command (Type h for help) : "); 556610021SSheshadri.Vasudevan@Sun.COM if ((ext_read_options(buf)) < 0) { 556710021SSheshadri.Vasudevan@Sun.COM printf("\nCommand Options : \n"); 556810021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu(); 556910021SSheshadri.Vasudevan@Sun.COM continue; 557010021SSheshadri.Vasudevan@Sun.COM } 557110021SSheshadri.Vasudevan@Sun.COM switch (buf[0]) { 557210021SSheshadri.Vasudevan@Sun.COM case 'a': 557310021SSheshadri.Vasudevan@Sun.COM add_logical_drive(); 557410021SSheshadri.Vasudevan@Sun.COM break; 557510021SSheshadri.Vasudevan@Sun.COM case 'd': 557610021SSheshadri.Vasudevan@Sun.COM delete_logical_drive(); 557710021SSheshadri.Vasudevan@Sun.COM break; 557810021SSheshadri.Vasudevan@Sun.COM case 'h': 557910021SSheshadri.Vasudevan@Sun.COM ext_print_help_menu(); 558010021SSheshadri.Vasudevan@Sun.COM break; 558110021SSheshadri.Vasudevan@Sun.COM case 'i': 558210021SSheshadri.Vasudevan@Sun.COM ext_change_logical_drive_id(); 558310021SSheshadri.Vasudevan@Sun.COM break; 558410021SSheshadri.Vasudevan@Sun.COM case 'p': 558510021SSheshadri.Vasudevan@Sun.COM ext_print_logical_drive_layout(); 558610021SSheshadri.Vasudevan@Sun.COM break; 558710021SSheshadri.Vasudevan@Sun.COM case 'r': 558810021SSheshadri.Vasudevan@Sun.COM printf(CLR_SCR); 558910021SSheshadri.Vasudevan@Sun.COM return; 559010021SSheshadri.Vasudevan@Sun.COM default : /* NOTREACHED */ 559110021SSheshadri.Vasudevan@Sun.COM break; 559210021SSheshadri.Vasudevan@Sun.COM } 559310021SSheshadri.Vasudevan@Sun.COM } 559410021SSheshadri.Vasudevan@Sun.COM } 559510021SSheshadri.Vasudevan@Sun.COM #endif 5596*11382SShidokht.Yadegari@Sun.COM 5597*11382SShidokht.Yadegari@Sun.COM static int 5598*11382SShidokht.Yadegari@Sun.COM nopartdefined() 5599*11382SShidokht.Yadegari@Sun.COM { 5600*11382SShidokht.Yadegari@Sun.COM int i; 5601*11382SShidokht.Yadegari@Sun.COM 5602*11382SShidokht.Yadegari@Sun.COM for (i = 0; i < FD_NUMPART; i++) 5603*11382SShidokht.Yadegari@Sun.COM if (Table[i].systid != UNUSED) 5604*11382SShidokht.Yadegari@Sun.COM return (0); 5605*11382SShidokht.Yadegari@Sun.COM return (1); 5606*11382SShidokht.Yadegari@Sun.COM } 5607