10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 55169Slclee * Common Development and Distribution License (the "License"). 65169Slclee * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215169Slclee 220Sstevel@tonic-gate /* 235936Sbharding * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */ 280Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 310Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 320Sstevel@tonic-gate /* All Rights Reserved */ 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 350Sstevel@tonic-gate * PROGRAM: fdisk(1M) 360Sstevel@tonic-gate * This program reads the partition table on the specified device and 370Sstevel@tonic-gate * also reads the drive parameters. The user can perform various 380Sstevel@tonic-gate * operations from a supplied menu or from the command line. Diagnostic 390Sstevel@tonic-gate * options are also available. 400Sstevel@tonic-gate */ 410Sstevel@tonic-gate 420Sstevel@tonic-gate #include <stdio.h> 430Sstevel@tonic-gate #include <stdlib.h> 440Sstevel@tonic-gate #include <string.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <errno.h> 470Sstevel@tonic-gate #include <fcntl.h> 480Sstevel@tonic-gate #include <ctype.h> 490Sstevel@tonic-gate #include <sys/stat.h> 500Sstevel@tonic-gate #include <sys/types.h> 51*7563SPrasad.Singamsetty@Sun.COM #include <limits.h> 520Sstevel@tonic-gate #include <sys/param.h> 530Sstevel@tonic-gate #include <sys/systeminfo.h> 540Sstevel@tonic-gate #include <sys/efi_partition.h> 550Sstevel@tonic-gate #include <sys/byteorder.h> 560Sstevel@tonic-gate #include <sys/systeminfo.h> 570Sstevel@tonic-gate 580Sstevel@tonic-gate #include <sys/dktp/fdisk.h> 590Sstevel@tonic-gate #include <sys/dkio.h> 600Sstevel@tonic-gate #include <sys/vtoc.h> 610Sstevel@tonic-gate 620Sstevel@tonic-gate #define CLR_SCR "[1;1H[0J" 630Sstevel@tonic-gate #define CLR_LIN "[0K" 640Sstevel@tonic-gate #define HOME "[1;1H[0K[2;1H[0K[3;1H[0K[4;1H[0K[5;1H[0K" \ 650Sstevel@tonic-gate "[6;1H[0K[7;1H[0K[8;1H[0K[9;1H[0K[10;1H[0K[1;1H" 660Sstevel@tonic-gate #define Q_LINE "[22;1H[0K[21;1H[0K[20;1H[0K" 670Sstevel@tonic-gate #define W_LINE "[12;1H[0K[11;1H[0K" 680Sstevel@tonic-gate #define E_LINE "[24;1H[0K[23;1H[0K" 690Sstevel@tonic-gate #define M_LINE "[13;1H[0K[14;1H[0K[15;1H[0K[16;1H[0K[17;1H" \ 700Sstevel@tonic-gate "[0K[18;1H[0K[19;1H[0K[13;1H" 710Sstevel@tonic-gate #define T_LINE "[1;1H[0K" 720Sstevel@tonic-gate 730Sstevel@tonic-gate #define DEFAULT_PATH "/dev/rdsk/" 740Sstevel@tonic-gate 750Sstevel@tonic-gate /* XXX - should be in fdisk.h, used by sd as well */ 760Sstevel@tonic-gate 770Sstevel@tonic-gate /* 780Sstevel@tonic-gate * the MAX values are the maximum usable values for BIOS chs values 790Sstevel@tonic-gate * The MAX_CYL value of 1022 is the maximum usable value 800Sstevel@tonic-gate * the value of 1023 is a fence value, 810Sstevel@tonic-gate * indicating no CHS geometry exists for the corresponding LBA value. 820Sstevel@tonic-gate * HEAD range [ 0 .. MAX_HEAD ], so number of heads is (MAX_HEAD + 1) 830Sstevel@tonic-gate * SECT range [ 1 .. MAX_SECT ], so number of sectors is (MAX_SECT) 840Sstevel@tonic-gate */ 850Sstevel@tonic-gate #define MAX_SECT (63) 860Sstevel@tonic-gate #define MAX_CYL (1022) 870Sstevel@tonic-gate #define MAX_HEAD (254) 880Sstevel@tonic-gate 89*7563SPrasad.Singamsetty@Sun.COM #define DK_MAX_2TB UINT32_MAX /* Max # of sectors in 2TB */ 90*7563SPrasad.Singamsetty@Sun.COM 910Sstevel@tonic-gate /* for clear_vtoc() */ 920Sstevel@tonic-gate #define OLD 0 930Sstevel@tonic-gate #define NEW 1 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* readvtoc/writevtoc return codes */ 960Sstevel@tonic-gate #define VTOC_OK 0 /* Good VTOC */ 970Sstevel@tonic-gate #define VTOC_INVAL 1 /* invalid VTOC */ 980Sstevel@tonic-gate #define VTOC_NOTSUP 2 /* operation not supported - EFI label */ 990Sstevel@tonic-gate #define VTOC_RWERR 3 /* couldn't read or write VTOC */ 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate /* 1020Sstevel@tonic-gate * Support for fdisk(1M) on the SPARC platform 1030Sstevel@tonic-gate * In order to convert little endian values to big endian for SPARC, 1040Sstevel@tonic-gate * byte/short and long values must be swapped. 1050Sstevel@tonic-gate * These swapping macros will be used to access information in the 1060Sstevel@tonic-gate * mboot and ipart structures. 1070Sstevel@tonic-gate */ 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate #ifdef sparc 1100Sstevel@tonic-gate #define les(val) ((((val)&0xFF)<<8)|(((val)>>8)&0xFF)) 1110Sstevel@tonic-gate #define lel(val) (((unsigned)(les((val)&0x0000FFFF))<<16) | \ 1120Sstevel@tonic-gate (les((unsigned)((val)&0xffff0000)>>16))) 1130Sstevel@tonic-gate #else 1140Sstevel@tonic-gate #define les(val) (val) 1150Sstevel@tonic-gate #define lel(val) (val) 1160Sstevel@tonic-gate #endif 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate #if defined(_SUNOS_VTOC_16) 1196549Sbharding #define VTOC_OFFSET 1 1200Sstevel@tonic-gate #elif defined(_SUNOS_VTOC_8) 1210Sstevel@tonic-gate #define VTOC_OFFSET 0 1220Sstevel@tonic-gate #else 1230Sstevel@tonic-gate #error No VTOC format defined. 1240Sstevel@tonic-gate #endif 1250Sstevel@tonic-gate 126251Slclee static char Usage[] = "Usage: fdisk\n" 1270Sstevel@tonic-gate "[ -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1280Sstevel@tonic-gate "[ -b masterboot ]\n" 1290Sstevel@tonic-gate "[ -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect ]\n" 1300Sstevel@tonic-gate "[ -F fdisk_file ] [ -h ] [ -o offset ] [ -P fill_patt ] [ -s size ]\n" 1310Sstevel@tonic-gate "[ -S geom_file ] [ [ -v ] -W { creat_fdisk_file | - } ]\n" 1320Sstevel@tonic-gate "[ -w | r | d | n | I | B | E | g | G | R | t | T ] rdevice"; 1330Sstevel@tonic-gate 134251Slclee static char Usage1[] = " Partition options:\n" 1350Sstevel@tonic-gate " -A id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1360Sstevel@tonic-gate " Create a partition with specific attributes:\n" 1370Sstevel@tonic-gate " id = system id number (fdisk.h) for the partition type\n" 1380Sstevel@tonic-gate " act = active partition flag (0 is off and 128 is on)\n" 1390Sstevel@tonic-gate " bhead = beginning head for start of partition\n" 1400Sstevel@tonic-gate " bsect = beginning sector for start of partition\n" 1410Sstevel@tonic-gate " bcyl = beginning cylinder for start of partition\n" 1420Sstevel@tonic-gate " ehead = ending head for end of partition\n" 1430Sstevel@tonic-gate " esect = ending sector for end of partition\n" 1440Sstevel@tonic-gate " ecyl = ending cylinder for end of partition\n" 1450Sstevel@tonic-gate " rsect = sector number from start of disk for\n" 1460Sstevel@tonic-gate " start of partition\n" 1470Sstevel@tonic-gate " numsect = partition size in sectors\n" 1480Sstevel@tonic-gate " -b master_boot\n" 1490Sstevel@tonic-gate " Use master_boot as the master boot file.\n" 1500Sstevel@tonic-gate " -B Create one Solaris partition that uses the entire disk.\n" 1510Sstevel@tonic-gate " -E Create one EFI partition that uses the entire disk.\n" 1520Sstevel@tonic-gate " -D id:act:bhead:bsect:bcyl:ehead:esect:ecyl:rsect:numsect\n" 1530Sstevel@tonic-gate " Delete a partition. See attribute definitions for -A.\n" 1540Sstevel@tonic-gate " -F fdisk_file\n" 1550Sstevel@tonic-gate " Use fdisk_file to initialize on-line fdisk table.\n" 1560Sstevel@tonic-gate " -I Forego device checks. Generate a file image of what would go\n" 1570Sstevel@tonic-gate " on a disk using the geometry specified with the -S option.\n" 1580Sstevel@tonic-gate " -n Do not run in interactive mode.\n" 1590Sstevel@tonic-gate " -R Open the disk device as read-only.\n" 1600Sstevel@tonic-gate " -t Check and adjust VTOC to be consistent with fdisk table.\n" 1610Sstevel@tonic-gate " VTOC slices exceeding the partition size will be truncated.\n" 1620Sstevel@tonic-gate " -T Check and adjust VTOC to be consistent with fdisk table.\n" 1630Sstevel@tonic-gate " VTOC slices exceeding the partition size will be removed.\n" 1640Sstevel@tonic-gate " -W fdisk_file\n" 1650Sstevel@tonic-gate " Write on-disk table to fdisk_file.\n" 1660Sstevel@tonic-gate " -W - Write on-disk table to standard output.\n" 1670Sstevel@tonic-gate " -v Display virtual geometry. Must be used with the -W option.\n" 1680Sstevel@tonic-gate " Diagnostic options:\n" 1690Sstevel@tonic-gate " -d Activate debug information about progress.\n" 1700Sstevel@tonic-gate " -g Write label geometry to standard output:\n" 1710Sstevel@tonic-gate " PCYL number of physical cylinders\n" 1720Sstevel@tonic-gate " NCYL number of usable cylinders\n" 1730Sstevel@tonic-gate " ACYL number of alternate cylinders\n" 1740Sstevel@tonic-gate " BCYL cylinder offset\n" 1750Sstevel@tonic-gate " NHEADS number of heads\n" 1760Sstevel@tonic-gate " NSECTORS number of sectors per track\n" 1770Sstevel@tonic-gate " SECTSIZ size of a sector in bytes\n" 1780Sstevel@tonic-gate " -G Write physical geometry to standard output (see -g).\n" 1790Sstevel@tonic-gate " -h Issue this verbose help message.\n" 1800Sstevel@tonic-gate " -o offset\n" 1810Sstevel@tonic-gate " Block offset from start of disk (default 0). Ignored if\n" 1820Sstevel@tonic-gate " -P # specified.\n" 1830Sstevel@tonic-gate " -P fill_patt\n" 1840Sstevel@tonic-gate " Fill disk with pattern fill_patt. fill_patt can be decimal or\n" 1850Sstevel@tonic-gate " hexadecimal and is used as number for constant long word\n" 1860Sstevel@tonic-gate " pattern. If fill_patt is \"#\" then pattern of block #\n" 1870Sstevel@tonic-gate " for each block. Pattern is put in each block as long words\n" 1880Sstevel@tonic-gate " and fills each block (see -o and -s).\n" 1890Sstevel@tonic-gate " -r Read from a disk to stdout (see -o and -s).\n" 1900Sstevel@tonic-gate " -s size Number of blocks on which to perform operation (see -o).\n" 1910Sstevel@tonic-gate " -S geom_file\n" 1920Sstevel@tonic-gate " Use geom_file to set the label geometry (see -g).\n" 1930Sstevel@tonic-gate " -w Write to a disk from stdin (see -o and -s)."; 1940Sstevel@tonic-gate 195251Slclee static char Ostr[] = "Other OS"; 196251Slclee static char Dstr[] = "DOS12"; 197251Slclee static char D16str[] = "DOS16"; 198251Slclee static char DDstr[] = "DOS-DATA"; 199251Slclee static char EDstr[] = "EXT-DOS"; 200251Slclee static char DBstr[] = "DOS-BIG"; 201251Slclee static char PCstr[] = "PCIX"; 202251Slclee static char Ustr[] = "UNIX System"; 203251Slclee static char SUstr[] = "Solaris"; 204251Slclee static char SU2str[] = "Solaris2"; 205251Slclee static char X86str[] = "x86 Boot"; 206251Slclee static char DIAGstr[] = "Diagnostic"; 207251Slclee static char IFSstr[] = "IFS: NTFS"; 208251Slclee static char AIXstr[] = "AIX Boot"; 209251Slclee static char AIXDstr[] = "AIX Data"; 210251Slclee static char OS2str[] = "OS/2 Boot"; 211251Slclee static char WINstr[] = "Win95 FAT32"; 212251Slclee static char EWINstr[] = "Ext Win95"; 213251Slclee static char FAT95str[] = "FAT16 LBA"; 214251Slclee static char EXTLstr[] = "EXT LBA"; 215251Slclee static char LINUXstr[] = "Linux"; 216251Slclee static char CPMstr[] = "CP/M"; 217251Slclee static char NOVstr[] = "Netware 3.x+"; 218251Slclee static char QNXstr[] = "QNX 4.x"; 219251Slclee static char QNX2str[] = "QNX part 2"; 220251Slclee static char QNX3str[] = "QNX part 3"; 221251Slclee static char LINNATstr[] = "Linux native"; 222251Slclee static char NTFSVOL1str[] = "NT volset 1"; 223251Slclee static char NTFSVOL2str[] = "NT volset 2"; 224251Slclee static char BSDstr[] = "BSD OS"; 225251Slclee static char NEXTSTEPstr[] = "NeXTSTEP"; 226251Slclee static char BSDIFSstr[] = "BSDI FS"; 227251Slclee static char BSDISWAPstr[] = "BSDI swap"; 228251Slclee static char Actvstr[] = "Active"; 229251Slclee static char EFIstr[] = "EFI"; 230251Slclee static char NAstr[] = " "; 2310Sstevel@tonic-gate 2320Sstevel@tonic-gate /* All the user options and flags */ 233251Slclee static char *Dfltdev; /* name of fixed disk drive */ 2340Sstevel@tonic-gate 2350Sstevel@tonic-gate /* Diagnostic options */ 236251Slclee static int io_wrt = 0; /* write stdin to disk (-w) */ 237251Slclee static int io_rd = 0; /* read disk and write stdout (-r) */ 238251Slclee static char *io_fatt; /* user supplied pattern (-P pattern) */ 239251Slclee static int io_patt = 0; /* write pattern to disk (-P pattern) */ 240251Slclee static int io_lgeom = 0; /* get label geometry (-g) */ 241251Slclee static int io_pgeom = 0; /* get drive physical geometry (-G) */ 242251Slclee static char *io_sgeom = 0; /* set label geometry (-S geom_file) */ 243251Slclee static int io_readonly = 0; /* do not write to disk (-R) */ 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* The -o offset and -s size options specify the area of the disk on */ 2460Sstevel@tonic-gate /* which to perform the particular operation; i.e., -P, -r, or -w. */ 2476549Sbharding static off_t io_offset = 0; /* offset sector (-o offset) */ 2486549Sbharding static off_t io_size = 0; /* size in sectors (-s size) */ 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* Partition table flags */ 251251Slclee static int v_flag = 0; /* virtual geometry-HBA flag (-v) */ 252251Slclee static int stdo_flag = 0; /* stdout flag (-W -) */ 253251Slclee static int io_fdisk = 0; /* do fdisk operation */ 254251Slclee static int io_ifdisk = 0; /* interactive partition */ 255251Slclee static int io_nifdisk = 0; /* non-interactive partition (-n) */ 256251Slclee 257251Slclee static int io_adjt = 0; /* check/adjust VTOC (truncate (-t)) */ 258251Slclee static int io_ADJT = 0; /* check/adjust VTOC (delete (-T)) */ 259251Slclee static char *io_ffdisk = 0; /* input fdisk file name (-F file) */ 260251Slclee static char *io_Wfdisk = 0; /* output fdisk file name (-W file) */ 261251Slclee static char *io_Afdisk = 0; /* add entry to partition table (-A) */ 262251Slclee static char *io_Dfdisk = 0; /* delete entry from part. table (-D) */ 263251Slclee 264251Slclee static char *io_mboot = 0; /* master boot record (-b boot_file) */ 265251Slclee 266251Slclee static struct mboot BootCod; /* buffer for master boot record */ 267251Slclee 268251Slclee static int io_wholedisk = 0; /* use whole disk for Solaris (-B) */ 269251Slclee static int io_EFIdisk = 0; /* use whole disk for EFI (-E) */ 270251Slclee static int io_debug = 0; /* activate verbose mode (-d) */ 271251Slclee static int io_image = 0; /* create image using geometry (-I) */ 272251Slclee 273251Slclee static struct mboot *Bootblk; /* pointer to cut/paste sector zero */ 274251Slclee static char *Bootsect; /* pointer to sector zero buffer */ 275251Slclee static char *Nullsect; 276*7563SPrasad.Singamsetty@Sun.COM static struct extvtoc disk_vtoc; /* verify VTOC table */ 277251Slclee static int vt_inval = 0; 278251Slclee static int no_virtgeom_ioctl = 0; /* ioctl for virtual geometry failed */ 279251Slclee static int no_physgeom_ioctl = 0; /* ioctl for physical geometry failed */ 280251Slclee 281251Slclee static struct ipart Table[FD_NUMPART]; 282251Slclee static struct ipart Old_Table[FD_NUMPART]; 2830Sstevel@tonic-gate 2840Sstevel@tonic-gate /* Disk geometry information */ 2855169Slclee static struct dk_minfo minfo; 286251Slclee static struct dk_geom disk_geom; 287251Slclee 288*7563SPrasad.Singamsetty@Sun.COM static int Dev; /* fd for open device */ 289*7563SPrasad.Singamsetty@Sun.COM 2905169Slclee static diskaddr_t dev_capacity; /* number of blocks on device */ 291*7563SPrasad.Singamsetty@Sun.COM static diskaddr_t chs_capacity; /* Numcyl_usable * heads * sectors */ 292*7563SPrasad.Singamsetty@Sun.COM 293*7563SPrasad.Singamsetty@Sun.COM static int Numcyl_usable; /* Number of usable cylinders */ 294*7563SPrasad.Singamsetty@Sun.COM /* used to limit fdisk to 2TB */ 295*7563SPrasad.Singamsetty@Sun.COM 2960Sstevel@tonic-gate /* Physical geometry for the drive */ 297251Slclee static int Numcyl; /* number of cylinders */ 298251Slclee static int heads; /* number of heads */ 299251Slclee static int sectors; /* number of sectors per track */ 300251Slclee static int acyl; /* number of alternate sectors */ 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate /* HBA (virtual) geometry for the drive */ 303251Slclee static int hba_Numcyl; /* number of cylinders */ 304251Slclee static int hba_heads; /* number of heads */ 305251Slclee static int hba_sectors; /* number of sectors per track */ 306251Slclee 307251Slclee static int sectsiz; /* sector size */ 3080Sstevel@tonic-gate 3090Sstevel@tonic-gate /* Load functions for fdisk table modification */ 3100Sstevel@tonic-gate #define LOADFILE 0 /* load fdisk from file */ 3110Sstevel@tonic-gate #define LOADDEL 1 /* delete an fdisk entry */ 3120Sstevel@tonic-gate #define LOADADD 2 /* add an fdisk entry */ 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate #define CBUFLEN 80 315251Slclee static char s[CBUFLEN]; 316251Slclee 317251Slclee static void update_disk_and_exit(boolean_t table_changed); 318251Slclee int main(int argc, char *argv[]); 319251Slclee static int read_geom(char *sgeom); 320251Slclee static void dev_mboot_read(void); 3216549Sbharding static void dev_mboot_write(off_t sect, char *buff, int bootsiz); 322251Slclee static void mboot_read(void); 323251Slclee static void fill_patt(void); 324251Slclee static void abs_read(void); 325251Slclee static void abs_write(void); 326251Slclee static void load(int funct, char *file); 327251Slclee static void Set_Table_CHS_Values(int ti); 328251Slclee static int insert_tbl(int id, int act, 329251Slclee int bhead, int bsect, int bcyl, 330251Slclee int ehead, int esect, int ecyl, 331*7563SPrasad.Singamsetty@Sun.COM uint32_t rsect, uint32_t numsect); 332251Slclee static int verify_tbl(void); 333251Slclee static int pars_fdisk(char *line, 334251Slclee int *id, int *act, 335251Slclee int *bhead, int *bsect, int *bcyl, 336251Slclee int *ehead, int *esect, int *ecyl, 337*7563SPrasad.Singamsetty@Sun.COM uint32_t *rsect, uint32_t *numsect); 338*7563SPrasad.Singamsetty@Sun.COM static int validate_part(int id, uint32_t rsect, uint32_t numsect); 339251Slclee static void stage0(void); 340251Slclee static int pcreate(void); 341251Slclee static int specify(uchar_t tsystid); 342251Slclee static void dispmenu(void); 343251Slclee static int pchange(void); 344251Slclee static int ppartid(void); 345251Slclee static char pdelete(void); 346251Slclee static void rm_blanks(char *s); 347251Slclee static int getcyl(void); 348251Slclee static void disptbl(void); 349251Slclee static void print_Table(void); 350251Slclee static void copy_Table_to_Old_Table(void); 351251Slclee static void nulltbl(void); 352251Slclee static void copy_Bootblk_to_Table(void); 353251Slclee static void fill_ipart(char *bootptr, struct ipart *partp); 354251Slclee #ifdef sparc 355251Slclee uchar_t getbyte(char **bp); 356251Slclee uint32_t getlong(char **bp); 357251Slclee #endif 358251Slclee static void copy_Table_to_Bootblk(void); 359251Slclee static int TableChanged(void); 360251Slclee static void ffile_write(char *file); 361251Slclee static void fix_slice(void); 362251Slclee static int yesno(void); 363251Slclee static int readvtoc(void); 364251Slclee static int writevtoc(void); 365251Slclee static int efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc); 366251Slclee static int clear_efi(void); 367251Slclee static void clear_vtoc(int table, int part); 368251Slclee static int lecture_and_query(char *warning, char *devname); 369251Slclee static void sanity_check_provided_device(char *devname, int fd); 370251Slclee static char *get_node(char *devname); 3710Sstevel@tonic-gate 3720Sstevel@tonic-gate static void 3730Sstevel@tonic-gate update_disk_and_exit(boolean_t table_changed) 3740Sstevel@tonic-gate { 3750Sstevel@tonic-gate if (table_changed) { 3760Sstevel@tonic-gate /* 3770Sstevel@tonic-gate * Copy the new table back to the sector buffer 3780Sstevel@tonic-gate * and write it to disk 3790Sstevel@tonic-gate */ 3800Sstevel@tonic-gate copy_Table_to_Bootblk(); 3810Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 3820Sstevel@tonic-gate } 3830Sstevel@tonic-gate 3840Sstevel@tonic-gate /* If the VTOC table is wrong fix it (truncation only) */ 3850Sstevel@tonic-gate if (io_adjt) 3860Sstevel@tonic-gate fix_slice(); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate exit(0); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate /* 3940Sstevel@tonic-gate * main 3950Sstevel@tonic-gate * Process command-line options. 3960Sstevel@tonic-gate */ 397251Slclee int 3980Sstevel@tonic-gate main(int argc, char *argv[]) 3990Sstevel@tonic-gate { 400251Slclee int c, i; 4010Sstevel@tonic-gate extern int optind; 4020Sstevel@tonic-gate extern char *optarg; 4030Sstevel@tonic-gate int errflg = 0; 4040Sstevel@tonic-gate int diag_cnt = 0; 4050Sstevel@tonic-gate int openmode; 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate setbuf(stderr, 0); /* so all output gets out on exit */ 4080Sstevel@tonic-gate setbuf(stdout, 0); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate /* Process the options. */ 4110Sstevel@tonic-gate while ((c = getopt(argc, argv, "o:s:P:F:b:A:D:W:S:tTIhwvrndgGRBE")) 4120Sstevel@tonic-gate != EOF) { 4130Sstevel@tonic-gate switch (c) { 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate case 'o': 4166549Sbharding io_offset = (off_t)strtoull(optarg, 0, 0); 4170Sstevel@tonic-gate continue; 4180Sstevel@tonic-gate case 's': 4196549Sbharding io_size = (off_t)strtoull(optarg, 0, 0); 4200Sstevel@tonic-gate continue; 4210Sstevel@tonic-gate case 'P': 4220Sstevel@tonic-gate diag_cnt++; 4230Sstevel@tonic-gate io_patt++; 4240Sstevel@tonic-gate io_fatt = optarg; 4250Sstevel@tonic-gate continue; 4260Sstevel@tonic-gate case 'w': 4270Sstevel@tonic-gate diag_cnt++; 4280Sstevel@tonic-gate io_wrt++; 4290Sstevel@tonic-gate continue; 4300Sstevel@tonic-gate case 'r': 4310Sstevel@tonic-gate diag_cnt++; 4320Sstevel@tonic-gate io_rd++; 4330Sstevel@tonic-gate continue; 4340Sstevel@tonic-gate case 'd': 4350Sstevel@tonic-gate io_debug++; 4360Sstevel@tonic-gate continue; 4370Sstevel@tonic-gate case 'I': 4380Sstevel@tonic-gate io_image++; 4390Sstevel@tonic-gate continue; 4400Sstevel@tonic-gate case 'R': 4410Sstevel@tonic-gate io_readonly++; 4420Sstevel@tonic-gate continue; 4430Sstevel@tonic-gate case 'S': 4440Sstevel@tonic-gate diag_cnt++; 4450Sstevel@tonic-gate io_sgeom = optarg; 4460Sstevel@tonic-gate continue; 4470Sstevel@tonic-gate case 'T': 4480Sstevel@tonic-gate io_ADJT++; 449251Slclee /* FALLTHRU */ 4500Sstevel@tonic-gate case 't': 4510Sstevel@tonic-gate io_adjt++; 4520Sstevel@tonic-gate continue; 4530Sstevel@tonic-gate case 'B': 4540Sstevel@tonic-gate io_wholedisk++; 4550Sstevel@tonic-gate io_fdisk++; 4560Sstevel@tonic-gate continue; 4570Sstevel@tonic-gate case 'E': 4580Sstevel@tonic-gate io_EFIdisk++; 4590Sstevel@tonic-gate io_fdisk++; 4600Sstevel@tonic-gate continue; 4610Sstevel@tonic-gate case 'g': 4620Sstevel@tonic-gate diag_cnt++; 4630Sstevel@tonic-gate io_lgeom++; 4640Sstevel@tonic-gate continue; 4650Sstevel@tonic-gate case 'G': 4660Sstevel@tonic-gate diag_cnt++; 4670Sstevel@tonic-gate io_pgeom++; 4680Sstevel@tonic-gate continue; 4690Sstevel@tonic-gate case 'n': 4700Sstevel@tonic-gate io_nifdisk++; 4710Sstevel@tonic-gate io_fdisk++; 4720Sstevel@tonic-gate continue; 4730Sstevel@tonic-gate case 'F': 4740Sstevel@tonic-gate io_fdisk++; 4750Sstevel@tonic-gate io_ffdisk = optarg; 4760Sstevel@tonic-gate continue; 4770Sstevel@tonic-gate case 'b': 4780Sstevel@tonic-gate io_mboot = optarg; 4790Sstevel@tonic-gate continue; 4800Sstevel@tonic-gate case 'W': 4810Sstevel@tonic-gate /* 4820Sstevel@tonic-gate * If '-' is the -W argument, then write 4830Sstevel@tonic-gate * to standard output, otherwise write 4840Sstevel@tonic-gate * to the specified file. 4850Sstevel@tonic-gate */ 4860Sstevel@tonic-gate if (strncmp(optarg, "-", 1) == 0) 4870Sstevel@tonic-gate stdo_flag = 1; 4880Sstevel@tonic-gate else 4890Sstevel@tonic-gate io_Wfdisk = optarg; 4900Sstevel@tonic-gate io_fdisk++; 4910Sstevel@tonic-gate continue; 4920Sstevel@tonic-gate case 'A': 4930Sstevel@tonic-gate io_fdisk++; 4940Sstevel@tonic-gate io_Afdisk = optarg; 4950Sstevel@tonic-gate continue; 4960Sstevel@tonic-gate case 'D': 4970Sstevel@tonic-gate io_fdisk++; 4980Sstevel@tonic-gate io_Dfdisk = optarg; 4990Sstevel@tonic-gate continue; 5000Sstevel@tonic-gate case 'h': 501251Slclee (void) fprintf(stderr, "%s\n", Usage); 502251Slclee (void) fprintf(stderr, "%s\n", Usage1); 5030Sstevel@tonic-gate exit(0); 504251Slclee /* FALLTHRU */ 5050Sstevel@tonic-gate case 'v': 5060Sstevel@tonic-gate v_flag = 1; 5070Sstevel@tonic-gate continue; 5080Sstevel@tonic-gate case '?': 5090Sstevel@tonic-gate errflg++; 5100Sstevel@tonic-gate break; 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate break; 5130Sstevel@tonic-gate } 5140Sstevel@tonic-gate 5150Sstevel@tonic-gate if (io_image && io_sgeom && diag_cnt == 1) { 5160Sstevel@tonic-gate diag_cnt = 0; 5170Sstevel@tonic-gate } 5180Sstevel@tonic-gate 5190Sstevel@tonic-gate /* User option checking */ 5200Sstevel@tonic-gate 5210Sstevel@tonic-gate /* By default, run in interactive mode */ 5220Sstevel@tonic-gate if (!io_fdisk && !diag_cnt && !io_nifdisk) { 5230Sstevel@tonic-gate io_ifdisk++; 5240Sstevel@tonic-gate io_fdisk++; 5250Sstevel@tonic-gate } 5260Sstevel@tonic-gate if (((io_fdisk || io_adjt) && diag_cnt) || (diag_cnt > 1)) { 5270Sstevel@tonic-gate errflg++; 5280Sstevel@tonic-gate } 5290Sstevel@tonic-gate 5300Sstevel@tonic-gate /* Was any error detected? */ 5310Sstevel@tonic-gate if (errflg || argc == optind) { 532251Slclee (void) fprintf(stderr, "%s\n", Usage); 533251Slclee (void) fprintf(stderr, 5340Sstevel@tonic-gate "\nDetailed help is available with the -h option.\n"); 5350Sstevel@tonic-gate exit(2); 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* Figure out the correct device node to open */ 5400Sstevel@tonic-gate Dfltdev = get_node(argv[optind]); 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate if (io_readonly) 5430Sstevel@tonic-gate openmode = O_RDONLY; 5440Sstevel@tonic-gate else 5450Sstevel@tonic-gate openmode = O_RDWR|O_CREAT; 5460Sstevel@tonic-gate 5470Sstevel@tonic-gate if ((Dev = open(Dfltdev, openmode, 0666)) == -1) { 548251Slclee (void) fprintf(stderr, 549251Slclee "fdisk: Cannot open device %s.\n", 550251Slclee Dfltdev); 5510Sstevel@tonic-gate exit(1); 5520Sstevel@tonic-gate } 5535169Slclee /* 5545169Slclee * not all disk (or disklike) drivers support DKIOCGMEDIAINFO 5555169Slclee * in that case leave the minfo structure zeroed 5565169Slclee */ 5575169Slclee if (ioctl(Dev, DKIOCGMEDIAINFO, &minfo)) { 5585169Slclee memset(&minfo, 0, sizeof (minfo)); 5595169Slclee } 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate /* Get the disk geometry */ 5620Sstevel@tonic-gate if (!io_image) { 5630Sstevel@tonic-gate /* Get disk's HBA (virtual) geometry */ 5640Sstevel@tonic-gate errno = 0; 5650Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_VIRTGEOM, &disk_geom)) { 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate /* 5680Sstevel@tonic-gate * If ioctl isn't implemented on this platform, then 5690Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v), 5700Sstevel@tonic-gate * otherwise use the virtual geometry. 5710Sstevel@tonic-gate */ 5720Sstevel@tonic-gate 5730Sstevel@tonic-gate if (errno == ENOTTY) { 5740Sstevel@tonic-gate v_flag = 0; 5750Sstevel@tonic-gate no_virtgeom_ioctl = 1; 5760Sstevel@tonic-gate } else if (errno == EINVAL) { 5770Sstevel@tonic-gate /* 5780Sstevel@tonic-gate * This means that the ioctl exists, but 5790Sstevel@tonic-gate * is invalid for this disk, meaning the 5800Sstevel@tonic-gate * disk doesn't have an HBA geometry 5810Sstevel@tonic-gate * (like, say, it's larger than 8GB). 5820Sstevel@tonic-gate */ 5830Sstevel@tonic-gate v_flag = 0; 5840Sstevel@tonic-gate hba_Numcyl = hba_heads = hba_sectors = 0; 5850Sstevel@tonic-gate } else { 5860Sstevel@tonic-gate (void) fprintf(stderr, 5870Sstevel@tonic-gate "%s: Cannot get virtual disk geometry.\n", 5880Sstevel@tonic-gate argv[optind]); 5890Sstevel@tonic-gate exit(1); 5900Sstevel@tonic-gate } 5910Sstevel@tonic-gate } else { 5920Sstevel@tonic-gate /* save virtual geometry values obtained by ioctl */ 5930Sstevel@tonic-gate hba_Numcyl = disk_geom.dkg_ncyl; 5940Sstevel@tonic-gate hba_heads = disk_geom.dkg_nhead; 5950Sstevel@tonic-gate hba_sectors = disk_geom.dkg_nsect; 5960Sstevel@tonic-gate } 5970Sstevel@tonic-gate 5980Sstevel@tonic-gate errno = 0; 5990Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 6000Sstevel@tonic-gate if (errno == ENOTTY) { 6010Sstevel@tonic-gate no_physgeom_ioctl = 1; 6020Sstevel@tonic-gate } else { 6030Sstevel@tonic-gate (void) fprintf(stderr, 6040Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 6050Sstevel@tonic-gate argv[optind]); 6060Sstevel@tonic-gate exit(1); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate /* 6110Sstevel@tonic-gate * Call DKIOCGGEOM if the ioctls for physical and virtual 6120Sstevel@tonic-gate * geometry fail. Get both from this generic call. 6130Sstevel@tonic-gate */ 6140Sstevel@tonic-gate if (no_virtgeom_ioctl && no_physgeom_ioctl) { 6150Sstevel@tonic-gate errno = 0; 6160Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6170Sstevel@tonic-gate (void) fprintf(stderr, 6180Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6190Sstevel@tonic-gate argv[optind]); 6200Sstevel@tonic-gate exit(1); 6210Sstevel@tonic-gate } 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6250Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6260Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6270Sstevel@tonic-gate sectsiz = 512; 6280Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 6290Sstevel@tonic-gate 6300Sstevel@tonic-gate /* 6310Sstevel@tonic-gate * if hba geometry was not set by DKIOC_VIRTGEOM 6320Sstevel@tonic-gate * or we got an invalid hba geometry 6330Sstevel@tonic-gate * then set hba geometry based on max values 6340Sstevel@tonic-gate */ 6350Sstevel@tonic-gate if (no_virtgeom_ioctl || 636251Slclee disk_geom.dkg_ncyl == 0 || 637251Slclee disk_geom.dkg_nhead == 0 || 638251Slclee disk_geom.dkg_nsect == 0 || 6390Sstevel@tonic-gate disk_geom.dkg_ncyl > MAX_CYL || 6400Sstevel@tonic-gate disk_geom.dkg_nhead > MAX_HEAD || 6410Sstevel@tonic-gate disk_geom.dkg_nsect > MAX_SECT) { 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate /* 6440Sstevel@tonic-gate * turn off flag to print out virtual geometry (-v) 6450Sstevel@tonic-gate */ 6460Sstevel@tonic-gate v_flag = 0; 6470Sstevel@tonic-gate hba_sectors = MAX_SECT; 6480Sstevel@tonic-gate hba_heads = MAX_HEAD + 1; 6490Sstevel@tonic-gate hba_Numcyl = (Numcyl * heads * sectors) / 6500Sstevel@tonic-gate (hba_sectors * hba_heads); 6510Sstevel@tonic-gate } 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate if (io_debug) { 654251Slclee (void) fprintf(stderr, "Physical Geometry:\n"); 655251Slclee (void) fprintf(stderr, 6560Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6570Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6580Sstevel@tonic-gate Numcyl, 6590Sstevel@tonic-gate heads, 6600Sstevel@tonic-gate sectors, 6610Sstevel@tonic-gate sectsiz, 662251Slclee Numcyl * heads * sectors, 663251Slclee (Numcyl * heads * sectors * sectsiz) / 1048576); 664251Slclee (void) fprintf(stderr, "Virtual (HBA) Geometry:\n"); 665251Slclee (void) fprintf(stderr, 6660Sstevel@tonic-gate " cylinders[%d] heads[%d] sectors[%d]\n" 6670Sstevel@tonic-gate " sector size[%d] blocks[%d] mbytes[%d]\n", 6680Sstevel@tonic-gate hba_Numcyl, 6690Sstevel@tonic-gate hba_heads, 6700Sstevel@tonic-gate hba_sectors, 6710Sstevel@tonic-gate sectsiz, 672251Slclee hba_Numcyl * hba_heads * hba_sectors, 673251Slclee (hba_Numcyl * hba_heads * hba_sectors * sectsiz) / 674251Slclee 1048576); 6750Sstevel@tonic-gate } 6760Sstevel@tonic-gate } 6770Sstevel@tonic-gate 6780Sstevel@tonic-gate /* If user has requested a geometry report just do it and exit */ 6790Sstevel@tonic-gate if (io_lgeom) { 6800Sstevel@tonic-gate if (ioctl(Dev, DKIOCGGEOM, &disk_geom)) { 6810Sstevel@tonic-gate (void) fprintf(stderr, 6820Sstevel@tonic-gate "%s: Cannot get disk label geometry.\n", 6830Sstevel@tonic-gate argv[optind]); 6840Sstevel@tonic-gate exit(1); 6850Sstevel@tonic-gate } 6860Sstevel@tonic-gate Numcyl = disk_geom.dkg_ncyl; 6870Sstevel@tonic-gate heads = disk_geom.dkg_nhead; 6880Sstevel@tonic-gate sectors = disk_geom.dkg_nsect; 6890Sstevel@tonic-gate sectsiz = 512; 6900Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 691251Slclee (void) printf("* Label geometry for device %s\n", Dfltdev); 692251Slclee (void) printf( 693251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 6940Sstevel@tonic-gate " SECSIZ\n"); 695251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 6960Sstevel@tonic-gate Numcyl, 6970Sstevel@tonic-gate disk_geom.dkg_ncyl, 6980Sstevel@tonic-gate disk_geom.dkg_acyl, 6990Sstevel@tonic-gate disk_geom.dkg_bcyl, 7000Sstevel@tonic-gate heads, 7010Sstevel@tonic-gate sectors, 7020Sstevel@tonic-gate sectsiz); 7030Sstevel@tonic-gate exit(0); 7040Sstevel@tonic-gate } else if (io_pgeom) { 7050Sstevel@tonic-gate if (ioctl(Dev, DKIOCG_PHYGEOM, &disk_geom)) { 7060Sstevel@tonic-gate (void) fprintf(stderr, 7070Sstevel@tonic-gate "%s: Cannot get physical disk geometry.\n", 7080Sstevel@tonic-gate argv[optind]); 7090Sstevel@tonic-gate exit(1); 7100Sstevel@tonic-gate } 711251Slclee (void) printf("* Physical geometry for device %s\n", Dfltdev); 712251Slclee (void) printf( 713251Slclee "* PCYL NCYL ACYL BCYL NHEAD NSECT" 7140Sstevel@tonic-gate " SECSIZ\n"); 715251Slclee (void) printf(" %-8d %-8d %-8d %-8d %-5d %-5d %-6d\n", 7160Sstevel@tonic-gate disk_geom.dkg_pcyl, 7170Sstevel@tonic-gate disk_geom.dkg_ncyl, 7180Sstevel@tonic-gate disk_geom.dkg_acyl, 7190Sstevel@tonic-gate disk_geom.dkg_bcyl, 7200Sstevel@tonic-gate disk_geom.dkg_nhead, 7210Sstevel@tonic-gate disk_geom.dkg_nsect, 7220Sstevel@tonic-gate sectsiz); 7230Sstevel@tonic-gate exit(0); 7240Sstevel@tonic-gate } else if (io_sgeom) { 7250Sstevel@tonic-gate if (read_geom(io_sgeom)) { 7260Sstevel@tonic-gate exit(1); 7270Sstevel@tonic-gate } else if (!io_image) { 7280Sstevel@tonic-gate exit(0); 7290Sstevel@tonic-gate } 7300Sstevel@tonic-gate } 7310Sstevel@tonic-gate 7325169Slclee /* 7335169Slclee * some drivers may not support DKIOCGMEDIAINFO 7345169Slclee * in that case use CHS 7355169Slclee */ 736*7563SPrasad.Singamsetty@Sun.COM chs_capacity = (diskaddr_t)Numcyl * heads * sectors; 7375169Slclee dev_capacity = chs_capacity; 738*7563SPrasad.Singamsetty@Sun.COM Numcyl_usable = Numcyl; 739*7563SPrasad.Singamsetty@Sun.COM 740*7563SPrasad.Singamsetty@Sun.COM if (chs_capacity > DK_MAX_2TB) { 741*7563SPrasad.Singamsetty@Sun.COM /* limit to 2TB */ 742*7563SPrasad.Singamsetty@Sun.COM Numcyl_usable = DK_MAX_2TB / (heads * sectors); 743*7563SPrasad.Singamsetty@Sun.COM chs_capacity = (diskaddr_t)Numcyl_usable * heads * sectors; 744*7563SPrasad.Singamsetty@Sun.COM } 745*7563SPrasad.Singamsetty@Sun.COM 7465169Slclee if (minfo.dki_capacity > 0) 7475169Slclee dev_capacity = minfo.dki_capacity; 7485169Slclee 7490Sstevel@tonic-gate /* Allocate memory to hold three complete sectors */ 7500Sstevel@tonic-gate Bootsect = (char *)malloc(3 * sectsiz); 7510Sstevel@tonic-gate if (Bootsect == NULL) { 752251Slclee (void) fprintf(stderr, 7530Sstevel@tonic-gate "fdisk: Unable to obtain enough buffer memory" 7540Sstevel@tonic-gate " (%d bytes).\n", 755251Slclee 3 * sectsiz); 7560Sstevel@tonic-gate exit(1); 7570Sstevel@tonic-gate } 7580Sstevel@tonic-gate 7590Sstevel@tonic-gate Nullsect = Bootsect + sectsiz; 7600Sstevel@tonic-gate /* Zero out the "NULL" sector */ 7610Sstevel@tonic-gate for (i = 0; i < sectsiz; i++) { 7620Sstevel@tonic-gate Nullsect[i] = 0; 7630Sstevel@tonic-gate } 7640Sstevel@tonic-gate 7650Sstevel@tonic-gate /* Find out what the user wants done */ 7660Sstevel@tonic-gate if (io_rd) { /* abs disk read */ 7670Sstevel@tonic-gate abs_read(); /* will not return */ 7680Sstevel@tonic-gate } else if (io_wrt && !io_readonly) { 7690Sstevel@tonic-gate abs_write(); /* will not return */ 7700Sstevel@tonic-gate } else if (io_patt && !io_readonly) { 7710Sstevel@tonic-gate fill_patt(); /* will not return */ 7720Sstevel@tonic-gate } 7730Sstevel@tonic-gate 7740Sstevel@tonic-gate 7750Sstevel@tonic-gate /* This is the fdisk edit, the real reason for the program. */ 7760Sstevel@tonic-gate 7770Sstevel@tonic-gate sanity_check_provided_device(Dfltdev, Dev); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* Get the new BOOT program in case we write a new fdisk table */ 7800Sstevel@tonic-gate mboot_read(); 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate /* Read from disk master boot */ 7830Sstevel@tonic-gate dev_mboot_read(); 7840Sstevel@tonic-gate 7850Sstevel@tonic-gate /* 7860Sstevel@tonic-gate * Verify and copy the device's fdisk table. This will be used 7870Sstevel@tonic-gate * as the prototype mboot if the device's mboot looks invalid. 7880Sstevel@tonic-gate */ 7890Sstevel@tonic-gate Bootblk = (struct mboot *)Bootsect; 7900Sstevel@tonic-gate copy_Bootblk_to_Table(); 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate /* save away a copy of Table in Old_Table for sensing changes */ 7930Sstevel@tonic-gate copy_Table_to_Old_Table(); 7940Sstevel@tonic-gate 7950Sstevel@tonic-gate /* Load fdisk table from specified file (-F fdisk_file) */ 7960Sstevel@tonic-gate if (io_ffdisk) { 7970Sstevel@tonic-gate /* Load and verify user-specified table parameters */ 7980Sstevel@tonic-gate load(LOADFILE, io_ffdisk); 7990Sstevel@tonic-gate } 8000Sstevel@tonic-gate 8010Sstevel@tonic-gate /* Does user want to delete or add an entry? */ 8020Sstevel@tonic-gate if (io_Dfdisk) { 8030Sstevel@tonic-gate load(LOADDEL, io_Dfdisk); 8040Sstevel@tonic-gate } 8050Sstevel@tonic-gate if (io_Afdisk) { 8060Sstevel@tonic-gate load(LOADADD, io_Afdisk); 8070Sstevel@tonic-gate } 8080Sstevel@tonic-gate 8090Sstevel@tonic-gate if (!io_ffdisk && !io_Afdisk && !io_Dfdisk) { 8100Sstevel@tonic-gate /* Check if there is no fdisk table */ 8110Sstevel@tonic-gate if (Table[0].systid == UNUSED || io_wholedisk || io_EFIdisk) { 8120Sstevel@tonic-gate if (io_ifdisk && !io_wholedisk && !io_EFIdisk) { 813251Slclee (void) printf( 814251Slclee "No fdisk table exists. The default" 815251Slclee " partition for the disk is:\n\n" 816251Slclee " a 100%% \"SOLARIS System\" " 817251Slclee "partition\n\n" 818251Slclee "Type \"y\" to accept the default " 8190Sstevel@tonic-gate "partition, otherwise type \"n\" to " 8200Sstevel@tonic-gate "edit the\n partition table.\n"); 821*7563SPrasad.Singamsetty@Sun.COM 822*7563SPrasad.Singamsetty@Sun.COM if (Numcyl > Numcyl_usable) 823*7563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger" 824*7563SPrasad.Singamsetty@Sun.COM " than 2TB. Solaris partition will" 825*7563SPrasad.Singamsetty@Sun.COM " be limited to 2 TB.\n"); 8260Sstevel@tonic-gate } 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate /* Edit the partition table as directed */ 8290Sstevel@tonic-gate if (io_wholedisk ||(io_ifdisk && yesno())) { 8300Sstevel@tonic-gate 8310Sstevel@tonic-gate /* Default scenario */ 8320Sstevel@tonic-gate nulltbl(); 8330Sstevel@tonic-gate /* now set up UNIX System partition */ 8340Sstevel@tonic-gate Table[0].bootid = ACTIVE; 8350Sstevel@tonic-gate Table[0].relsect = lel(heads * sectors); 836*7563SPrasad.Singamsetty@Sun.COM 837*7563SPrasad.Singamsetty@Sun.COM Table[0].numsect = 838*7563SPrasad.Singamsetty@Sun.COM lel((ulong_t)((Numcyl_usable - 1) * 8390Sstevel@tonic-gate heads * sectors)); 840*7563SPrasad.Singamsetty@Sun.COM 8410Sstevel@tonic-gate Table[0].systid = SUNIXOS2; /* Solaris */ 8420Sstevel@tonic-gate 8430Sstevel@tonic-gate /* calculate CHS values for table entry 0 */ 8440Sstevel@tonic-gate Set_Table_CHS_Values(0); 8450Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8460Sstevel@tonic-gate } else if (io_EFIdisk) { 8470Sstevel@tonic-gate /* create an EFI partition for the whole disk */ 8480Sstevel@tonic-gate nulltbl(); 8490Sstevel@tonic-gate i = insert_tbl(EFI_PMBR, 0, 0, 0, 0, 0, 0, 0, 1, 850*7563SPrasad.Singamsetty@Sun.COM (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB : 851*7563SPrasad.Singamsetty@Sun.COM (dev_capacity - 1)); 8520Sstevel@tonic-gate if (i != 0) { 853251Slclee (void) fprintf(stderr, 854251Slclee "Error creating EFI partition\n"); 8550Sstevel@tonic-gate exit(1); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate update_disk_and_exit(B_TRUE); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate } 8600Sstevel@tonic-gate } 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate /* Display complete fdisk table entries for debugging purposes */ 8630Sstevel@tonic-gate if (io_debug) { 864251Slclee (void) fprintf(stderr, "Partition Table Entry Values:\n"); 8650Sstevel@tonic-gate print_Table(); 8660Sstevel@tonic-gate if (io_ifdisk) { 867251Slclee (void) fprintf(stderr, "\n"); 868251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 869251Slclee (void) gets(s); 8700Sstevel@tonic-gate } 8710Sstevel@tonic-gate } 8720Sstevel@tonic-gate 8730Sstevel@tonic-gate /* Interactive fdisk mode */ 8740Sstevel@tonic-gate if (io_ifdisk) { 875251Slclee (void) printf(CLR_SCR); 8760Sstevel@tonic-gate disptbl(); 877251Slclee for (;;) { 878251Slclee stage0(); 8790Sstevel@tonic-gate copy_Bootblk_to_Table(); 8800Sstevel@tonic-gate disptbl(); 8810Sstevel@tonic-gate } 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* If user wants to write the table to a file, do it */ 8850Sstevel@tonic-gate if (io_Wfdisk) 8860Sstevel@tonic-gate ffile_write(io_Wfdisk); 8870Sstevel@tonic-gate else if (stdo_flag) 8880Sstevel@tonic-gate ffile_write((char *)stdout); 8890Sstevel@tonic-gate 8900Sstevel@tonic-gate update_disk_and_exit(TableChanged() == 1); 891251Slclee return (0); 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * read_geom 8960Sstevel@tonic-gate * Read geometry from specified file (-S). 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate 899251Slclee static int 900251Slclee read_geom(char *sgeom) 9010Sstevel@tonic-gate { 9020Sstevel@tonic-gate char line[256]; 9030Sstevel@tonic-gate FILE *fp; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate /* open the prototype file */ 9060Sstevel@tonic-gate if ((fp = fopen(sgeom, "r")) == NULL) { 9070Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot open file %s.\n", 9080Sstevel@tonic-gate io_sgeom); 9090Sstevel@tonic-gate return (1); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* Read a line from the file */ 9130Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 9140Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 9150Sstevel@tonic-gate continue; 9160Sstevel@tonic-gate else { 9170Sstevel@tonic-gate line[strlen(line)] = '\0'; 918251Slclee if (sscanf(line, "%hu %hu %hu %hu %hu %hu %d", 9190Sstevel@tonic-gate &disk_geom.dkg_pcyl, 9200Sstevel@tonic-gate &disk_geom.dkg_ncyl, 9210Sstevel@tonic-gate &disk_geom.dkg_acyl, 9220Sstevel@tonic-gate &disk_geom.dkg_bcyl, 9230Sstevel@tonic-gate &disk_geom.dkg_nhead, 9240Sstevel@tonic-gate &disk_geom.dkg_nsect, 9250Sstevel@tonic-gate §siz) != 7) { 9260Sstevel@tonic-gate (void) fprintf(stderr, 9270Sstevel@tonic-gate "Syntax error:\n \"%s\".\n", 9280Sstevel@tonic-gate line); 9290Sstevel@tonic-gate return (1); 9300Sstevel@tonic-gate } 9310Sstevel@tonic-gate break; 9320Sstevel@tonic-gate } /* else */ 9330Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 9340Sstevel@tonic-gate 9350Sstevel@tonic-gate if (!io_image) { 9360Sstevel@tonic-gate if (ioctl(Dev, DKIOCSGEOM, &disk_geom)) { 9370Sstevel@tonic-gate (void) fprintf(stderr, 9380Sstevel@tonic-gate "fdisk: Cannot set label geometry.\n"); 9390Sstevel@tonic-gate return (1); 9400Sstevel@tonic-gate } 9410Sstevel@tonic-gate } else { 9420Sstevel@tonic-gate Numcyl = hba_Numcyl = disk_geom.dkg_ncyl; 9430Sstevel@tonic-gate heads = hba_heads = disk_geom.dkg_nhead; 9440Sstevel@tonic-gate sectors = hba_sectors = disk_geom.dkg_nsect; 9450Sstevel@tonic-gate acyl = disk_geom.dkg_acyl; 9460Sstevel@tonic-gate } 9470Sstevel@tonic-gate 948251Slclee (void) fclose(fp); 9490Sstevel@tonic-gate return (0); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate /* 9530Sstevel@tonic-gate * dev_mboot_read 9540Sstevel@tonic-gate * Read the master boot sector from the device. 9550Sstevel@tonic-gate */ 956251Slclee static void 957251Slclee dev_mboot_read(void) 9580Sstevel@tonic-gate { 9590Sstevel@tonic-gate if ((ioctl(Dev, DKIOCGMBOOT, Bootsect) < 0) && (errno != ENOTTY)) { 9600Sstevel@tonic-gate perror("Error in ioctl DKIOCGMBOOT"); 9610Sstevel@tonic-gate } 9620Sstevel@tonic-gate if (errno == 0) 9630Sstevel@tonic-gate return; 9640Sstevel@tonic-gate if (lseek(Dev, 0, SEEK_SET) == -1) { 965251Slclee (void) fprintf(stderr, 9660Sstevel@tonic-gate "fdisk: Error seeking to partition table on %s.\n", 9670Sstevel@tonic-gate Dfltdev); 9680Sstevel@tonic-gate if (!io_image) 9690Sstevel@tonic-gate exit(1); 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 972251Slclee (void) fprintf(stderr, 9730Sstevel@tonic-gate "fdisk: Error reading partition table from %s.\n", 9740Sstevel@tonic-gate Dfltdev); 9750Sstevel@tonic-gate if (!io_image) 9760Sstevel@tonic-gate exit(1); 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9800Sstevel@tonic-gate /* 9810Sstevel@tonic-gate * dev_mboot_write 9820Sstevel@tonic-gate * Write the master boot sector to the device. 9830Sstevel@tonic-gate */ 984251Slclee static void 9856549Sbharding dev_mboot_write(off_t sect, char *buff, int bootsiz) 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate int new_pt, old_pt, error; 9886478Sbharding int clr_efi = -1; 9890Sstevel@tonic-gate 9900Sstevel@tonic-gate if (io_readonly) 991251Slclee return; 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate if (io_debug) { 994251Slclee (void) fprintf(stderr, "About to write fdisk table:\n"); 9950Sstevel@tonic-gate print_Table(); 9960Sstevel@tonic-gate if (io_ifdisk) { 997251Slclee (void) fprintf(stderr, "Press Enter to continue.\n"); 998251Slclee (void) gets(s); 9990Sstevel@tonic-gate } 10000Sstevel@tonic-gate } 10010Sstevel@tonic-gate 10026478Sbharding /* 10036478Sbharding * If the new table has any Solaris partitions and the old 10046478Sbharding * table does not have an entry that describes it 10056478Sbharding * exactly then clear the old vtoc (if any). 10066478Sbharding */ 10070Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 10080Sstevel@tonic-gate 10096478Sbharding /* We only care about potential Solaris parts. */ 10100Sstevel@tonic-gate if (Table[new_pt].systid != SUNIXOS && 10110Sstevel@tonic-gate Table[new_pt].systid != SUNIXOS2) 10120Sstevel@tonic-gate continue; 10130Sstevel@tonic-gate 10146478Sbharding /* Does the old table have an exact entry for the new entry? */ 10156478Sbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 10166478Sbharding 10176478Sbharding /* We only care about old Solaris partitions. */ 10186478Sbharding if ((Old_Table[old_pt].systid == SUNIXOS) || 10196478Sbharding (Old_Table[old_pt].systid == SUNIXOS2)) { 10206478Sbharding 10216478Sbharding /* Is this old one the same as a new one? */ 10226478Sbharding if ((Old_Table[old_pt].relsect == 10236478Sbharding Table[new_pt].relsect) && 10246478Sbharding (Old_Table[old_pt].numsect == 10256478Sbharding Table[new_pt].numsect)) 10266478Sbharding break; /* Yes */ 10276478Sbharding } 10286478Sbharding } 10296478Sbharding 10306478Sbharding /* Did a solaris partition change location or size? */ 10316478Sbharding if (old_pt >= FD_NUMPART) { 10326478Sbharding /* Yes clear old vtoc */ 10336478Sbharding if (io_debug) { 10346478Sbharding (void) fprintf(stderr, 10356478Sbharding "Clearing VTOC labels from NEW" 10366478Sbharding " table\n"); 10376478Sbharding } 10386478Sbharding clear_vtoc(NEW, new_pt); 10396478Sbharding } 10406478Sbharding } 10416478Sbharding 10426478Sbharding 10436478Sbharding /* see if the old table had EFI */ 10446478Sbharding for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 10456478Sbharding if (Old_Table[old_pt].systid == EFI_PMBR) { 10466478Sbharding clr_efi = old_pt; 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate /* look to see if a EFI partition changed in relsect/numsect */ 10510Sstevel@tonic-gate for (new_pt = 0; new_pt < FD_NUMPART; new_pt++) { 10520Sstevel@tonic-gate if (Table[new_pt].systid != EFI_PMBR) 10530Sstevel@tonic-gate continue; 10540Sstevel@tonic-gate for (old_pt = 0; old_pt < FD_NUMPART; old_pt++) { 10555169Slclee if ((Old_Table[old_pt].systid == 10565169Slclee Table[new_pt].systid) && 10575169Slclee (Old_Table[old_pt].relsect == 10585169Slclee Table[new_pt].relsect) && 10595169Slclee (Old_Table[old_pt].numsect == 10605169Slclee Table[new_pt].numsect)) 10615169Slclee break; 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate /* 10650Sstevel@tonic-gate * if EFI partition changed, set the flag to clear 10660Sstevel@tonic-gate * the EFI GPT 10670Sstevel@tonic-gate */ 10680Sstevel@tonic-gate if (old_pt == FD_NUMPART && Table[new_pt].begcyl != 0) { 10690Sstevel@tonic-gate clr_efi = 0; 10700Sstevel@tonic-gate } 10710Sstevel@tonic-gate break; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate 10740Sstevel@tonic-gate /* clear labels if necessary */ 10750Sstevel@tonic-gate if (clr_efi >= 0) { 10760Sstevel@tonic-gate if (io_debug) { 1077251Slclee (void) fprintf(stderr, "Clearing EFI labels\n"); 10780Sstevel@tonic-gate } 10790Sstevel@tonic-gate if ((error = clear_efi()) != 0) { 10800Sstevel@tonic-gate if (io_debug) { 1081251Slclee (void) fprintf(stderr, 1082251Slclee "\tError %d clearing EFI labels" 10830Sstevel@tonic-gate " (probably no EFI labels exist)\n", 10840Sstevel@tonic-gate error); 10850Sstevel@tonic-gate } 10860Sstevel@tonic-gate } 10870Sstevel@tonic-gate } 10880Sstevel@tonic-gate 10890Sstevel@tonic-gate if ((ioctl(Dev, DKIOCSMBOOT, buff) == -1) && (errno != ENOTTY)) { 1090251Slclee (void) fprintf(stderr, 10910Sstevel@tonic-gate "fdisk: Error in ioctl DKIOCSMBOOT on %s.\n", 10920Sstevel@tonic-gate Dfltdev); 10930Sstevel@tonic-gate } 10940Sstevel@tonic-gate if (errno == 0) 10950Sstevel@tonic-gate return; 10960Sstevel@tonic-gate 10970Sstevel@tonic-gate /* write to disk drive */ 10980Sstevel@tonic-gate if (lseek(Dev, sect, SEEK_SET) == -1) { 1099251Slclee (void) fprintf(stderr, 11000Sstevel@tonic-gate "fdisk: Error seeking to master boot record on %s.\n", 11010Sstevel@tonic-gate Dfltdev); 11020Sstevel@tonic-gate exit(1); 11030Sstevel@tonic-gate } 11040Sstevel@tonic-gate if (write(Dev, buff, bootsiz) != bootsiz) { 1105251Slclee (void) fprintf(stderr, 11060Sstevel@tonic-gate "fdisk: Error writing master boot record to %s.\n", 11070Sstevel@tonic-gate Dfltdev); 11080Sstevel@tonic-gate exit(1); 11090Sstevel@tonic-gate } 11100Sstevel@tonic-gate } 11110Sstevel@tonic-gate 11120Sstevel@tonic-gate /* 11130Sstevel@tonic-gate * mboot_read 11140Sstevel@tonic-gate * Read the prototype boot records from the files. 11150Sstevel@tonic-gate */ 1116251Slclee static void 1117251Slclee mboot_read(void) 11180Sstevel@tonic-gate { 11190Sstevel@tonic-gate int mDev, i; 11200Sstevel@tonic-gate struct ipart *part; 11210Sstevel@tonic-gate 11220Sstevel@tonic-gate #if defined(i386) || defined(sparc) 11230Sstevel@tonic-gate /* 11240Sstevel@tonic-gate * If the master boot file hasn't been specified, use the 11250Sstevel@tonic-gate * implementation architecture name to generate the default one. 11260Sstevel@tonic-gate */ 11270Sstevel@tonic-gate if (io_mboot == (char *)0) { 11280Sstevel@tonic-gate /* 11290Sstevel@tonic-gate * Bug ID 1249035: 11300Sstevel@tonic-gate * The mboot file must be delivered on all platforms 11310Sstevel@tonic-gate * and installed in a non-platform-dependent 11320Sstevel@tonic-gate * directory; i.e., /usr/lib/fs/ufs. 11330Sstevel@tonic-gate */ 11340Sstevel@tonic-gate io_mboot = "/usr/lib/fs/ufs/mboot"; 11350Sstevel@tonic-gate } 11360Sstevel@tonic-gate 11370Sstevel@tonic-gate /* First read in the master boot record */ 11380Sstevel@tonic-gate 11390Sstevel@tonic-gate /* Open the master boot proto file */ 11400Sstevel@tonic-gate if ((mDev = open(io_mboot, O_RDONLY, 0666)) == -1) { 1141251Slclee (void) fprintf(stderr, 11420Sstevel@tonic-gate "fdisk: Cannot open master boot file %s.\n", 11430Sstevel@tonic-gate io_mboot); 11440Sstevel@tonic-gate exit(1); 11450Sstevel@tonic-gate } 11460Sstevel@tonic-gate 11470Sstevel@tonic-gate /* Read the master boot program */ 11480Sstevel@tonic-gate if (read(mDev, &BootCod, sizeof (struct mboot)) != sizeof 11490Sstevel@tonic-gate (struct mboot)) { 1150251Slclee (void) fprintf(stderr, 11510Sstevel@tonic-gate "fdisk: Cannot read master boot file %s.\n", 11520Sstevel@tonic-gate io_mboot); 11530Sstevel@tonic-gate exit(1); 11540Sstevel@tonic-gate } 11550Sstevel@tonic-gate 11560Sstevel@tonic-gate /* Is this really a master boot record? */ 11570Sstevel@tonic-gate if (les(BootCod.signature) != MBB_MAGIC) { 1158251Slclee (void) fprintf(stderr, 11590Sstevel@tonic-gate "fdisk: Invalid master boot file %s.\n", io_mboot); 1160251Slclee (void) fprintf(stderr, 1161251Slclee "Bad magic number: is %x, but should be %x.\n", 11620Sstevel@tonic-gate les(BootCod.signature), MBB_MAGIC); 11630Sstevel@tonic-gate exit(1); 11640Sstevel@tonic-gate } 11650Sstevel@tonic-gate 1166251Slclee (void) close(mDev); 11670Sstevel@tonic-gate #else 11680Sstevel@tonic-gate #error fdisk needs to be ported to new architecture 11690Sstevel@tonic-gate #endif 11700Sstevel@tonic-gate 11710Sstevel@tonic-gate /* Zero out the partitions part of this record */ 11720Sstevel@tonic-gate part = (struct ipart *)BootCod.parts; 11730Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++, part++) { 1174251Slclee (void) memset(part, 0, sizeof (struct ipart)); 11750Sstevel@tonic-gate } 11760Sstevel@tonic-gate 11770Sstevel@tonic-gate } 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate /* 11800Sstevel@tonic-gate * fill_patt 11810Sstevel@tonic-gate * Fill the disk with user/sector number pattern. 11820Sstevel@tonic-gate */ 1183251Slclee static void 1184251Slclee fill_patt(void) 11850Sstevel@tonic-gate { 1186251Slclee int *buff_ptr, i; 11876549Sbharding off_t *off_ptr; 11880Sstevel@tonic-gate int io_fpatt = 0; 11890Sstevel@tonic-gate int io_ipatt = 0; 11900Sstevel@tonic-gate 11910Sstevel@tonic-gate if (strncmp(io_fatt, "#", 1) != 0) { 11920Sstevel@tonic-gate io_fpatt++; 11930Sstevel@tonic-gate io_ipatt = strtoul(io_fatt, 0, 0); 11940Sstevel@tonic-gate buff_ptr = (int *)Bootsect; 11950Sstevel@tonic-gate for (i = 0; i < sectsiz; i += 4, buff_ptr++) 11965169Slclee *buff_ptr = io_ipatt; 11970Sstevel@tonic-gate } 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate /* 12000Sstevel@tonic-gate * Fill disk with pattern based on block number. 12010Sstevel@tonic-gate * Write to the disk at absolute relative block io_offset 12020Sstevel@tonic-gate * for io_size blocks. 12030Sstevel@tonic-gate */ 12040Sstevel@tonic-gate while (io_size--) { 12056549Sbharding off_ptr = (off_t *)Bootsect; 12060Sstevel@tonic-gate if (!io_fpatt) { 12076549Sbharding for (i = 0; i < sectsiz; 12086549Sbharding i += sizeof (off_t), off_ptr++) 12096549Sbharding *off_ptr = io_offset; 12100Sstevel@tonic-gate } 12110Sstevel@tonic-gate /* Write the data to disk */ 12126549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 12136549Sbharding SEEK_SET) == -1) { 1214251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12155169Slclee Dfltdev); 12160Sstevel@tonic-gate exit(1); 12170Sstevel@tonic-gate } 12180Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1219251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 12205169Slclee Dfltdev); 12210Sstevel@tonic-gate exit(1); 12220Sstevel@tonic-gate } 12230Sstevel@tonic-gate } /* while (--io_size); */ 12240Sstevel@tonic-gate } 12250Sstevel@tonic-gate 12260Sstevel@tonic-gate /* 12270Sstevel@tonic-gate * abs_read 12280Sstevel@tonic-gate * Read from the disk at absolute relative block io_offset for 12290Sstevel@tonic-gate * io_size blocks. Write the data to standard ouput (-r). 12300Sstevel@tonic-gate */ 1231251Slclee static void 1232251Slclee abs_read(void) 1233251Slclee { 12340Sstevel@tonic-gate int c; 12350Sstevel@tonic-gate 12360Sstevel@tonic-gate while (io_size--) { 12376549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 12386549Sbharding SEEK_SET) == -1) { 1239251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 12400Sstevel@tonic-gate Dfltdev); 12410Sstevel@tonic-gate exit(1); 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate if (read(Dev, Bootsect, sectsiz) != sectsiz) { 1244251Slclee (void) fprintf(stderr, "fdisk: Error reading %s.\n", 12450Sstevel@tonic-gate Dfltdev); 12460Sstevel@tonic-gate exit(1); 12470Sstevel@tonic-gate } 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate /* Write to standard ouptut */ 1250251Slclee if ((c = write(1, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12510Sstevel@tonic-gate if (c >= 0) { 12520Sstevel@tonic-gate if (io_debug) 1253251Slclee (void) fprintf(stderr, 1254251Slclee "fdisk: Output warning: %d of %d" 1255251Slclee " characters written.\n", 1256251Slclee c, sectsiz); 12570Sstevel@tonic-gate exit(2); 12580Sstevel@tonic-gate } else { 12590Sstevel@tonic-gate perror("write error on output file."); 12600Sstevel@tonic-gate exit(2); 12610Sstevel@tonic-gate } 12620Sstevel@tonic-gate } /* if ((c = write(1, Bootsect, (unsigned)sectsiz)) */ 12630Sstevel@tonic-gate /* != sectsiz) */ 12640Sstevel@tonic-gate } /* while (--io_size); */ 12650Sstevel@tonic-gate exit(0); 12660Sstevel@tonic-gate } 12670Sstevel@tonic-gate 12680Sstevel@tonic-gate /* 12690Sstevel@tonic-gate * abs_write 12700Sstevel@tonic-gate * Read the data from standard input. Write to the disk at 12710Sstevel@tonic-gate * absolute relative block io_offset for io_size blocks (-w). 12720Sstevel@tonic-gate */ 1273251Slclee static void 1274251Slclee abs_write(void) 12750Sstevel@tonic-gate { 12760Sstevel@tonic-gate int c, i; 12770Sstevel@tonic-gate 12780Sstevel@tonic-gate while (io_size--) { 12790Sstevel@tonic-gate int part_exit = 0; 12800Sstevel@tonic-gate /* Read from standard input */ 12810Sstevel@tonic-gate if ((c = read(0, Bootsect, (unsigned)sectsiz)) != sectsiz) { 12820Sstevel@tonic-gate if (c >= 0) { 12830Sstevel@tonic-gate if (io_debug) 1284251Slclee (void) fprintf(stderr, 12850Sstevel@tonic-gate "fdisk: WARNING: Incomplete read (%d of" 12860Sstevel@tonic-gate " %d characters read) on input file.\n", 12875169Slclee c, sectsiz); 12880Sstevel@tonic-gate /* Fill pattern to mark partial sector in buf */ 12890Sstevel@tonic-gate for (i = c; i < sectsiz; ) { 12900Sstevel@tonic-gate Bootsect[i++] = 0x41; 12910Sstevel@tonic-gate Bootsect[i++] = 0x62; 12920Sstevel@tonic-gate Bootsect[i++] = 0x65; 12930Sstevel@tonic-gate Bootsect[i++] = 0; 12940Sstevel@tonic-gate } 12950Sstevel@tonic-gate part_exit++; 12960Sstevel@tonic-gate } else { 12970Sstevel@tonic-gate perror("read error on input file."); 12980Sstevel@tonic-gate exit(2); 12990Sstevel@tonic-gate } 13000Sstevel@tonic-gate 13010Sstevel@tonic-gate } 13020Sstevel@tonic-gate /* Write to disk drive */ 13036549Sbharding if (lseek(Dev, (off_t)(sectsiz * io_offset++), 13046549Sbharding SEEK_SET) == -1) { 1305251Slclee (void) fprintf(stderr, "fdisk: Error seeking on %s.\n", 13060Sstevel@tonic-gate Dfltdev); 13070Sstevel@tonic-gate exit(1); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate if (write(Dev, Bootsect, sectsiz) != sectsiz) { 1310251Slclee (void) fprintf(stderr, "fdisk: Error writing %s.\n", 13110Sstevel@tonic-gate Dfltdev); 13120Sstevel@tonic-gate exit(1); 13130Sstevel@tonic-gate } 13140Sstevel@tonic-gate if (part_exit) 13150Sstevel@tonic-gate exit(0); 13160Sstevel@tonic-gate } /* while (--io_size); */ 13170Sstevel@tonic-gate exit(1); 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate 1320251Slclee 13210Sstevel@tonic-gate /* 13220Sstevel@tonic-gate * load 13230Sstevel@tonic-gate * Load will either read the fdisk table from a file or add or 13240Sstevel@tonic-gate * delete an entry (-A, -D, -F). 13250Sstevel@tonic-gate */ 13260Sstevel@tonic-gate 1327251Slclee static void 1328251Slclee load(int funct, char *file) 13290Sstevel@tonic-gate { 13300Sstevel@tonic-gate int id; 13310Sstevel@tonic-gate int act; 13320Sstevel@tonic-gate int bhead; 13330Sstevel@tonic-gate int bsect; 13340Sstevel@tonic-gate int bcyl; 13350Sstevel@tonic-gate int ehead; 13360Sstevel@tonic-gate int esect; 13370Sstevel@tonic-gate int ecyl; 1338*7563SPrasad.Singamsetty@Sun.COM uint32_t rsect; 1339*7563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 13400Sstevel@tonic-gate char line[256]; 13410Sstevel@tonic-gate int i = 0; 13420Sstevel@tonic-gate int j; 13430Sstevel@tonic-gate FILE *fp; 13440Sstevel@tonic-gate 13450Sstevel@tonic-gate switch (funct) { 13460Sstevel@tonic-gate 13475169Slclee case LOADFILE: 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate /* 13500Sstevel@tonic-gate * Zero out the table before loading it, which will 13510Sstevel@tonic-gate * force it to be updated on disk later (-F 13520Sstevel@tonic-gate * fdisk_file). 13530Sstevel@tonic-gate */ 13540Sstevel@tonic-gate nulltbl(); 13550Sstevel@tonic-gate 13560Sstevel@tonic-gate /* Open the prototype file */ 13570Sstevel@tonic-gate if ((fp = fopen(file, "r")) == NULL) { 13580Sstevel@tonic-gate (void) fprintf(stderr, 13590Sstevel@tonic-gate "fdisk: Cannot open prototype partition file %s.\n", 13600Sstevel@tonic-gate file); 13610Sstevel@tonic-gate exit(1); 13620Sstevel@tonic-gate } 13630Sstevel@tonic-gate 13640Sstevel@tonic-gate /* Read a line from the file */ 13650Sstevel@tonic-gate while (fgets(line, sizeof (line) - 1, fp)) { 13660Sstevel@tonic-gate if (pars_fdisk(line, &id, &act, &bhead, &bsect, 13670Sstevel@tonic-gate &bcyl, &ehead, &esect, &ecyl, &rsect, &numsect)) { 13680Sstevel@tonic-gate continue; 13690Sstevel@tonic-gate } 13700Sstevel@tonic-gate 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * Validate the partition. It cannot start at sector 13730Sstevel@tonic-gate * 0 unless it is UNUSED or already exists 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate if (validate_part(id, rsect, numsect) < 0) { 13760Sstevel@tonic-gate (void) fprintf(stderr, 13770Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13780Sstevel@tonic-gate line); 13790Sstevel@tonic-gate exit(1); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate /* 13820Sstevel@tonic-gate * Find an unused entry to use and put the entry 13830Sstevel@tonic-gate * in table 13840Sstevel@tonic-gate */ 13850Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, 13860Sstevel@tonic-gate esect, ecyl, rsect, numsect) < 0) { 13870Sstevel@tonic-gate (void) fprintf(stderr, 13880Sstevel@tonic-gate "fdisk: Error on entry \"%s\".\n", 13890Sstevel@tonic-gate line); 13900Sstevel@tonic-gate exit(1); 13910Sstevel@tonic-gate } 13920Sstevel@tonic-gate } /* while (fgets(line, sizeof (line) - 1, fp)) */ 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate if (verify_tbl() < 0) { 1395251Slclee (void) fprintf(stderr, 13960Sstevel@tonic-gate "fdisk: Cannot create partition table\n"); 13970Sstevel@tonic-gate exit(1); 13980Sstevel@tonic-gate } 13990Sstevel@tonic-gate 1400251Slclee (void) fclose(fp); 14010Sstevel@tonic-gate return; 14020Sstevel@tonic-gate 14035169Slclee case LOADDEL: 14040Sstevel@tonic-gate 14050Sstevel@tonic-gate /* Parse the user-supplied deletion line (-D) */ 1406251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, 1407251Slclee &ehead, &esect, &ecyl, &rsect, &numsect)) { 1408251Slclee (void) fprintf(stderr, 1409251Slclee "fdisk: Syntax error \"%s\"\n", file); 1410251Slclee exit(1); 1411251Slclee } 14120Sstevel@tonic-gate 14130Sstevel@tonic-gate /* Find the exact entry in the table */ 14140Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 14150Sstevel@tonic-gate if (Table[i].systid == id && 14160Sstevel@tonic-gate Table[i].bootid == act && 14170Sstevel@tonic-gate Table[i].beghead == bhead && 14180Sstevel@tonic-gate Table[i].begsect == ((bsect & 0x3f) | 14195169Slclee (uchar_t)((bcyl>>2) & 0xc0)) && 1420251Slclee Table[i].begcyl == (uchar_t)(bcyl & 0xff) && 14210Sstevel@tonic-gate Table[i].endhead == ehead && 14220Sstevel@tonic-gate Table[i].endsect == ((esect & 0x3f) | 14235169Slclee (uchar_t)((ecyl>>2) & 0xc0)) && 1424251Slclee Table[i].endcyl == (uchar_t)(ecyl & 0xff) && 14250Sstevel@tonic-gate Table[i].relsect == lel(rsect) && 14260Sstevel@tonic-gate Table[i].numsect == lel(numsect)) { 14270Sstevel@tonic-gate 14280Sstevel@tonic-gate /* 14290Sstevel@tonic-gate * Found the entry. Now move rest of 14300Sstevel@tonic-gate * entries up toward the top of the 14310Sstevel@tonic-gate * table, leaving available entries at 14320Sstevel@tonic-gate * the end of the fdisk table. 14330Sstevel@tonic-gate */ 1434251Slclee for (j = i; j < FD_NUMPART - 1; j++) { 1435251Slclee Table[j].systid = Table[j + 1].systid; 1436251Slclee Table[j].bootid = Table[j + 1].bootid; 1437251Slclee Table[j].beghead = Table[j + 1].beghead; 1438251Slclee Table[j].begsect = Table[j + 1].begsect; 1439251Slclee Table[j].begcyl = Table[j + 1].begcyl; 1440251Slclee Table[j].endhead = Table[j + 1].endhead; 1441251Slclee Table[j].endsect = Table[j + 1].endsect; 1442251Slclee Table[j].endcyl = Table[j + 1].endcyl; 1443251Slclee Table[j].relsect = Table[j + 1].relsect; 1444251Slclee Table[j].numsect = Table[j + 1].numsect; 14450Sstevel@tonic-gate } 14460Sstevel@tonic-gate 14470Sstevel@tonic-gate /* 14480Sstevel@tonic-gate * Mark the last entry as unused in case 14490Sstevel@tonic-gate * all table entries were in use prior 14500Sstevel@tonic-gate * to the deletion. 14510Sstevel@tonic-gate */ 14520Sstevel@tonic-gate 1453251Slclee Table[FD_NUMPART - 1].systid = UNUSED; 1454251Slclee Table[FD_NUMPART - 1].bootid = 0; 14550Sstevel@tonic-gate return; 14560Sstevel@tonic-gate } 14570Sstevel@tonic-gate } 1458251Slclee (void) fprintf(stderr, 14590Sstevel@tonic-gate "fdisk: Entry does not match any existing partition:\n" 14600Sstevel@tonic-gate " \"%s\"\n", 14610Sstevel@tonic-gate file); 14620Sstevel@tonic-gate exit(1); 1463251Slclee /* FALLTHRU */ 14640Sstevel@tonic-gate 14655169Slclee case LOADADD: 14660Sstevel@tonic-gate 14670Sstevel@tonic-gate /* Parse the user-supplied addition line (-A) */ 1468251Slclee if (pars_fdisk(file, &id, &act, &bhead, &bsect, &bcyl, &ehead, 1469251Slclee &esect, &ecyl, &rsect, &numsect)) { 1470251Slclee (void) fprintf(stderr, 1471251Slclee "fdisk: Syntax error \"%s\"\n", file); 1472251Slclee exit(1); 1473251Slclee } 14740Sstevel@tonic-gate 14750Sstevel@tonic-gate /* Validate the partition. It cannot start at sector 0 */ 14760Sstevel@tonic-gate if (rsect == 0) { 14770Sstevel@tonic-gate (void) fprintf(stderr, 14780Sstevel@tonic-gate "fdisk: New partition cannot start at sector 0:\n" 14790Sstevel@tonic-gate " \"%s\".\n", 14800Sstevel@tonic-gate file); 14810Sstevel@tonic-gate exit(1); 14820Sstevel@tonic-gate } 14830Sstevel@tonic-gate 14840Sstevel@tonic-gate /* 14850Sstevel@tonic-gate * if the user wishes to add an EFI partition, we need 14860Sstevel@tonic-gate * more extensive validation. rsect should be 1, and 14870Sstevel@tonic-gate * numsect should equal the entire disk capacity - 1 14880Sstevel@tonic-gate */ 14890Sstevel@tonic-gate 14900Sstevel@tonic-gate if (id == EFI_PMBR) { 14910Sstevel@tonic-gate if (rsect != 1) { 14920Sstevel@tonic-gate (void) fprintf(stderr, 14930Sstevel@tonic-gate "fdisk: EFI partitions must start at sector" 14940Sstevel@tonic-gate " 1 (input rsect = %d)\n", rsect); 14950Sstevel@tonic-gate exit(1); 14960Sstevel@tonic-gate } 14970Sstevel@tonic-gate 1498*7563SPrasad.Singamsetty@Sun.COM 1499*7563SPrasad.Singamsetty@Sun.COM if (dev_capacity > DK_MAX_2TB) { 1500*7563SPrasad.Singamsetty@Sun.COM if (numsect != DK_MAX_2TB) { 1501*7563SPrasad.Singamsetty@Sun.COM (void) fprintf(stderr, 1502*7563SPrasad.Singamsetty@Sun.COM "fdisk: EFI partitions must " 1503*7563SPrasad.Singamsetty@Sun.COM "encompass the entire maximum 2 TB " 1504*7563SPrasad.Singamsetty@Sun.COM "(input numsect: %u - max: %llu)\n", 1505*7563SPrasad.Singamsetty@Sun.COM numsect, (diskaddr_t)DK_MAX_2TB); 1506*7563SPrasad.Singamsetty@Sun.COM exit(1); 1507*7563SPrasad.Singamsetty@Sun.COM } 1508*7563SPrasad.Singamsetty@Sun.COM } else if (numsect != dev_capacity - 1) { 15090Sstevel@tonic-gate (void) fprintf(stderr, 15100Sstevel@tonic-gate "fdisk: EFI partitions must encompass the " 1511251Slclee "entire disk\n" 1512*7563SPrasad.Singamsetty@Sun.COM "(input numsect: %u - avail: %llu)\n", 1513251Slclee numsect, 15145169Slclee dev_capacity - 1); 15150Sstevel@tonic-gate exit(1); 15160Sstevel@tonic-gate } 15170Sstevel@tonic-gate } 15180Sstevel@tonic-gate 15190Sstevel@tonic-gate /* Find unused entry for use and put entry in table */ 15200Sstevel@tonic-gate if (insert_tbl(id, act, bhead, bsect, bcyl, ehead, esect, 15210Sstevel@tonic-gate ecyl, rsect, numsect) < 0) { 15220Sstevel@tonic-gate (void) fprintf(stderr, 15230Sstevel@tonic-gate "fdisk: Invalid entry could not be inserted:\n" 15240Sstevel@tonic-gate " \"%s\"\n", 15250Sstevel@tonic-gate file); 15260Sstevel@tonic-gate exit(1); 15270Sstevel@tonic-gate } 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate /* Make sure new entry does not overlap existing entry */ 15300Sstevel@tonic-gate if (verify_tbl() < 0) { 1531251Slclee (void) fprintf(stderr, 1532251Slclee "fdisk: Cannot create partition \"%s\"\n", file); 15330Sstevel@tonic-gate exit(1); 15340Sstevel@tonic-gate } 15350Sstevel@tonic-gate } /* switch funct */ 15360Sstevel@tonic-gate } 15370Sstevel@tonic-gate 15380Sstevel@tonic-gate /* 15390Sstevel@tonic-gate * Set_Table_CHS_Values 15400Sstevel@tonic-gate * 15410Sstevel@tonic-gate * This will calculate the CHS values for beginning and ending CHS 15420Sstevel@tonic-gate * for a single partition table entry (ti) based on the relsect 15430Sstevel@tonic-gate * and numsect values contained in the partion table entry. 15440Sstevel@tonic-gate * 15450Sstevel@tonic-gate * hba_heads and hba_sectors contain the number of heads and sectors. 15460Sstevel@tonic-gate * 15470Sstevel@tonic-gate * If the number of cylinders exceeds the MAX_CYL, 15480Sstevel@tonic-gate * then maximum values will be placed in the corresponding chs entry. 15490Sstevel@tonic-gate */ 15500Sstevel@tonic-gate static void 15510Sstevel@tonic-gate Set_Table_CHS_Values(int ti) 15520Sstevel@tonic-gate { 15530Sstevel@tonic-gate uint32_t lba, cy, hd, sc; 15540Sstevel@tonic-gate 15550Sstevel@tonic-gate lba = (uint32_t)Table[ti].relsect; 15560Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 15570Sstevel@tonic-gate /* 15580Sstevel@tonic-gate * the lba address cannot be expressed in CHS value 15590Sstevel@tonic-gate * so store the maximum CHS field values in the CHS fields. 15600Sstevel@tonic-gate */ 15610Sstevel@tonic-gate cy = MAX_CYL + 1; 15620Sstevel@tonic-gate hd = MAX_HEAD; 15630Sstevel@tonic-gate sc = MAX_SECT; 15640Sstevel@tonic-gate } else { 15650Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15660Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15670Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15680Sstevel@tonic-gate } 15690Sstevel@tonic-gate Table[ti].begcyl = cy & 0xff; 1570251Slclee Table[ti].beghead = (uchar_t)hd; 1571251Slclee Table[ti].begsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15720Sstevel@tonic-gate 15730Sstevel@tonic-gate /* 15740Sstevel@tonic-gate * This code is identical to the code above 15750Sstevel@tonic-gate * except that it works on ending CHS values 15760Sstevel@tonic-gate */ 15770Sstevel@tonic-gate lba = (uint32_t)(Table[ti].relsect + Table[ti].numsect - 1); 15780Sstevel@tonic-gate if (lba >= hba_heads * hba_sectors * MAX_CYL) { 15790Sstevel@tonic-gate cy = MAX_CYL + 1; 15800Sstevel@tonic-gate hd = MAX_HEAD; 15810Sstevel@tonic-gate sc = MAX_SECT; 15820Sstevel@tonic-gate } else { 15830Sstevel@tonic-gate cy = lba / hba_sectors / hba_heads; 15840Sstevel@tonic-gate hd = lba / hba_sectors % hba_heads; 15850Sstevel@tonic-gate sc = lba % hba_sectors + 1; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate Table[ti].endcyl = cy & 0xff; 1588251Slclee Table[ti].endhead = (uchar_t)hd; 1589251Slclee Table[ti].endsect = (uchar_t)(((cy >> 2) & 0xc0) | sc); 15900Sstevel@tonic-gate } 15910Sstevel@tonic-gate 15920Sstevel@tonic-gate /* 15930Sstevel@tonic-gate * insert_tbl 15940Sstevel@tonic-gate * Insert entry into fdisk table. Check all user-supplied values 15950Sstevel@tonic-gate * for the entry, but not the validity relative to other table 15960Sstevel@tonic-gate * entries! 15970Sstevel@tonic-gate */ 1598251Slclee static int 1599251Slclee insert_tbl( 1600251Slclee int id, int act, 1601251Slclee int bhead, int bsect, int bcyl, 1602251Slclee int ehead, int esect, int ecyl, 1603*7563SPrasad.Singamsetty@Sun.COM uint32_t rsect, uint32_t numsect) 16040Sstevel@tonic-gate { 16050Sstevel@tonic-gate int i; 16060Sstevel@tonic-gate 16070Sstevel@tonic-gate /* validate partition size */ 1608*7563SPrasad.Singamsetty@Sun.COM if (((diskaddr_t)rsect + numsect) > dev_capacity) { 1609251Slclee (void) fprintf(stderr, 16100Sstevel@tonic-gate "fdisk: Partition table exceeds the size of the disk.\n"); 16110Sstevel@tonic-gate return (-1); 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate 1614*7563SPrasad.Singamsetty@Sun.COM 16150Sstevel@tonic-gate /* find UNUSED partition table entry */ 16160Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 16170Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 16180Sstevel@tonic-gate break; 16190Sstevel@tonic-gate } 16200Sstevel@tonic-gate } 16210Sstevel@tonic-gate if (i >= FD_NUMPART) { 1622251Slclee (void) fprintf(stderr, "fdisk: Partition table is full.\n"); 16230Sstevel@tonic-gate return (-1); 16240Sstevel@tonic-gate } 16250Sstevel@tonic-gate 16260Sstevel@tonic-gate 1627251Slclee Table[i].systid = (uchar_t)id; 1628251Slclee Table[i].bootid = (uchar_t)act; 16290Sstevel@tonic-gate Table[i].numsect = lel(numsect); 16300Sstevel@tonic-gate Table[i].relsect = lel(rsect); 16310Sstevel@tonic-gate 16320Sstevel@tonic-gate /* 16330Sstevel@tonic-gate * If we have been called with a valid geometry, use it 16340Sstevel@tonic-gate * valid means non-zero values that fit in the BIOS fields 16350Sstevel@tonic-gate */ 16360Sstevel@tonic-gate if (0 < bsect && bsect <= MAX_SECT && 16370Sstevel@tonic-gate 0 <= bhead && bhead <= MAX_HEAD && 16380Sstevel@tonic-gate 0 < esect && esect <= MAX_SECT && 16390Sstevel@tonic-gate 0 <= ehead && ehead <= MAX_HEAD) { 16400Sstevel@tonic-gate if (bcyl > MAX_CYL) 16410Sstevel@tonic-gate bcyl = MAX_CYL + 1; 16420Sstevel@tonic-gate if (ecyl > MAX_CYL) 16430Sstevel@tonic-gate ecyl = MAX_CYL + 1; 16440Sstevel@tonic-gate Table[i].begcyl = bcyl & 0xff; 16450Sstevel@tonic-gate Table[i].endcyl = ecyl & 0xff; 1646251Slclee Table[i].beghead = (uchar_t)bhead; 1647251Slclee Table[i].endhead = (uchar_t)ehead; 1648251Slclee Table[i].begsect = (uchar_t)(((bcyl >> 2) & 0xc0) | bsect); 16490Sstevel@tonic-gate Table[i].endsect = ((ecyl >> 2) & 0xc0) | esect; 16500Sstevel@tonic-gate } else { 16510Sstevel@tonic-gate 16520Sstevel@tonic-gate /* 16530Sstevel@tonic-gate * The specified values are invalid, 16540Sstevel@tonic-gate * so calculate the values based on hba_heads, hba_sectors 16550Sstevel@tonic-gate */ 16560Sstevel@tonic-gate Set_Table_CHS_Values(i); 16570Sstevel@tonic-gate } 16580Sstevel@tonic-gate 16590Sstevel@tonic-gate /* 16600Sstevel@tonic-gate * return partition index 16610Sstevel@tonic-gate */ 16620Sstevel@tonic-gate return (i); 16630Sstevel@tonic-gate } 16640Sstevel@tonic-gate 16650Sstevel@tonic-gate /* 16660Sstevel@tonic-gate * verify_tbl 16670Sstevel@tonic-gate * Verify that no partition entries overlap or exceed the size of 16680Sstevel@tonic-gate * the disk. 16690Sstevel@tonic-gate */ 1670251Slclee static int 1671251Slclee verify_tbl(void) 16720Sstevel@tonic-gate { 1673*7563SPrasad.Singamsetty@Sun.COM uint32_t i, j, rsect, numsect; 16740Sstevel@tonic-gate int noMoreParts = 0; 16750Sstevel@tonic-gate int numParts = 0; 16760Sstevel@tonic-gate 16770Sstevel@tonic-gate /* Make sure new entry does not overlap an existing entry */ 1678251Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 16790Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 16800Sstevel@tonic-gate numParts++; 16810Sstevel@tonic-gate /* 16820Sstevel@tonic-gate * No valid partitions allowed after an UNUSED or 16830Sstevel@tonic-gate * EFI_PMBR part 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate if (noMoreParts) { 16860Sstevel@tonic-gate return (-1); 16870Sstevel@tonic-gate } 16880Sstevel@tonic-gate 16890Sstevel@tonic-gate /* 16900Sstevel@tonic-gate * EFI_PMBR partitions must be the only partition 16910Sstevel@tonic-gate * and must be Table entry 0 16920Sstevel@tonic-gate */ 16930Sstevel@tonic-gate if (Table[i].systid == EFI_PMBR) { 16940Sstevel@tonic-gate if (i == 0) { 16950Sstevel@tonic-gate noMoreParts = 1; 16960Sstevel@tonic-gate } else { 16970Sstevel@tonic-gate return (-1); 16980Sstevel@tonic-gate } 16990Sstevel@tonic-gate 17000Sstevel@tonic-gate if (Table[i].relsect != 1) { 1701251Slclee (void) fprintf(stderr, "ERROR: " 17020Sstevel@tonic-gate "Invalid starting sector " 17030Sstevel@tonic-gate "for EFI_PMBR partition:\n" 17040Sstevel@tonic-gate "relsect %d " 17050Sstevel@tonic-gate "(should be 1)\n", 17060Sstevel@tonic-gate Table[i].relsect); 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate return (-1); 17090Sstevel@tonic-gate } 17100Sstevel@tonic-gate 17115169Slclee if (Table[i].numsect != dev_capacity - 1) { 1712251Slclee (void) fprintf(stderr, "ERROR: " 17130Sstevel@tonic-gate "EFI_PMBR partition must " 17140Sstevel@tonic-gate "encompass the entire " 1715251Slclee "disk.\n numsect %d - " 17165169Slclee "actual %llu\n", 17170Sstevel@tonic-gate Table[i].numsect, 17185169Slclee dev_capacity - 1); 17190Sstevel@tonic-gate 17200Sstevel@tonic-gate return (-1); 17210Sstevel@tonic-gate } 17220Sstevel@tonic-gate } 17230Sstevel@tonic-gate 17240Sstevel@tonic-gate /* make sure the partition isn't larger than the disk */ 17250Sstevel@tonic-gate rsect = lel(Table[i].relsect); 17260Sstevel@tonic-gate numsect = lel(Table[i].numsect); 1727*7563SPrasad.Singamsetty@Sun.COM 1728*7563SPrasad.Singamsetty@Sun.COM if ((((diskaddr_t)rsect + numsect) > dev_capacity) || 1729*7563SPrasad.Singamsetty@Sun.COM (((diskaddr_t)rsect + numsect) > DK_MAX_2TB)) { 17300Sstevel@tonic-gate return (-1); 17310Sstevel@tonic-gate } 17320Sstevel@tonic-gate 1733251Slclee for (j = i + 1; j < FD_NUMPART; j++) { 17340Sstevel@tonic-gate if (Table[j].systid != UNUSED) { 1735*7563SPrasad.Singamsetty@Sun.COM uint32_t t_relsect = 1736*7563SPrasad.Singamsetty@Sun.COM lel(Table[j].relsect); 1737*7563SPrasad.Singamsetty@Sun.COM uint32_t t_numsect = 1738*7563SPrasad.Singamsetty@Sun.COM lel(Table[j].numsect); 17390Sstevel@tonic-gate 17400Sstevel@tonic-gate if (noMoreParts) { 1741251Slclee (void) fprintf(stderr, 17420Sstevel@tonic-gate "Cannot add partition to " 17430Sstevel@tonic-gate "table; no more partitions " 17440Sstevel@tonic-gate "allowed\n"); 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate if (io_debug) { 1747251Slclee (void) fprintf(stderr, 17480Sstevel@tonic-gate "DEBUG: Current " 17490Sstevel@tonic-gate "partition:\t" 17500Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1751251Slclee "%d:%d:%d:%d:%d\n" 17520Sstevel@tonic-gate " Next " 17530Sstevel@tonic-gate "partition:\t\t" 17540Sstevel@tonic-gate "%d:%d:%d:%d:%d:" 1755251Slclee "%d:%d:%d:%d:%d\n", 17560Sstevel@tonic-gate Table[i].systid, 17570Sstevel@tonic-gate Table[i].bootid, 17580Sstevel@tonic-gate Table[i].begcyl, 17590Sstevel@tonic-gate Table[i].beghead, 17600Sstevel@tonic-gate Table[i].begsect, 17610Sstevel@tonic-gate Table[i].endcyl, 17620Sstevel@tonic-gate Table[i].endhead, 17630Sstevel@tonic-gate Table[i].endsect, 17640Sstevel@tonic-gate Table[i].relsect, 17650Sstevel@tonic-gate Table[i].numsect, 17660Sstevel@tonic-gate Table[j].systid, 17670Sstevel@tonic-gate Table[j].bootid, 17680Sstevel@tonic-gate Table[j].begcyl, 17690Sstevel@tonic-gate Table[j].beghead, 17700Sstevel@tonic-gate Table[j].begsect, 17710Sstevel@tonic-gate Table[j].endcyl, 17720Sstevel@tonic-gate Table[j].endhead, 17730Sstevel@tonic-gate Table[j].endsect, 17740Sstevel@tonic-gate Table[j].relsect, 17750Sstevel@tonic-gate Table[j].numsect); 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate return (-1); 17790Sstevel@tonic-gate } 17800Sstevel@tonic-gate if ((rsect >= 17810Sstevel@tonic-gate (t_relsect + t_numsect)) || 1782251Slclee ((rsect + numsect) <= t_relsect)) { 17830Sstevel@tonic-gate continue; 17840Sstevel@tonic-gate } else { 1785251Slclee (void) fprintf(stderr, "ERROR: " 17860Sstevel@tonic-gate "current partition overlaps" 17870Sstevel@tonic-gate " following partition\n"); 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate return (-1); 17900Sstevel@tonic-gate } 17910Sstevel@tonic-gate } 17920Sstevel@tonic-gate } 17930Sstevel@tonic-gate } else { 17940Sstevel@tonic-gate noMoreParts = 1; 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate } 17970Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 17980Sstevel@tonic-gate if (noMoreParts || 1799*7563SPrasad.Singamsetty@Sun.COM (((diskaddr_t)lel(Table[i].relsect) + 1800*7563SPrasad.Singamsetty@Sun.COM lel(Table[i].numsect)) > dev_capacity) || 1801*7563SPrasad.Singamsetty@Sun.COM (((diskaddr_t)lel(Table[i].relsect) + 1802*7563SPrasad.Singamsetty@Sun.COM lel(Table[i].numsect)) > DK_MAX_2TB)) { 18030Sstevel@tonic-gate return (-1); 18040Sstevel@tonic-gate } 18050Sstevel@tonic-gate } 18060Sstevel@tonic-gate 18070Sstevel@tonic-gate return (numParts); 18080Sstevel@tonic-gate } 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * pars_fdisk 18120Sstevel@tonic-gate * Parse user-supplied data to set up fdisk partitions 18130Sstevel@tonic-gate * (-A, -D, -F). 18140Sstevel@tonic-gate */ 1815251Slclee static int 1816251Slclee pars_fdisk( 1817251Slclee char *line, 1818251Slclee int *id, int *act, 1819251Slclee int *bhead, int *bsect, int *bcyl, 1820251Slclee int *ehead, int *esect, int *ecyl, 1821*7563SPrasad.Singamsetty@Sun.COM uint32_t *rsect, uint32_t *numsect) 18220Sstevel@tonic-gate { 18230Sstevel@tonic-gate int i; 18240Sstevel@tonic-gate if (line[0] == '\0' || line[0] == '\n' || line[0] == '*') 18255169Slclee return (1); 18260Sstevel@tonic-gate line[strlen(line)] = '\0'; 18270Sstevel@tonic-gate for (i = 0; i < strlen(line); i++) { 18280Sstevel@tonic-gate if (line[i] == '\0') { 18290Sstevel@tonic-gate break; 18300Sstevel@tonic-gate } else if (line[i] == ':') { 18310Sstevel@tonic-gate line[i] = ' '; 18320Sstevel@tonic-gate } 18330Sstevel@tonic-gate } 1834*7563SPrasad.Singamsetty@Sun.COM if (sscanf(line, "%d %d %d %d %d %d %d %d %u %u", 18350Sstevel@tonic-gate id, act, bhead, bsect, bcyl, ehead, esect, ecyl, 18360Sstevel@tonic-gate rsect, numsect) != 10) { 18370Sstevel@tonic-gate (void) fprintf(stderr, "Syntax error:\n \"%s\".\n", line); 18380Sstevel@tonic-gate exit(1); 18390Sstevel@tonic-gate } 18400Sstevel@tonic-gate return (0); 18410Sstevel@tonic-gate } 18420Sstevel@tonic-gate 18430Sstevel@tonic-gate /* 18440Sstevel@tonic-gate * validate_part 18450Sstevel@tonic-gate * Validate that a new partition does not start at sector 0. Only UNUSED 18460Sstevel@tonic-gate * partitions and previously existing partitions are allowed to start at 0. 18470Sstevel@tonic-gate */ 1848251Slclee static int 1849*7563SPrasad.Singamsetty@Sun.COM validate_part(int id, uint32_t rsect, uint32_t numsect) 18500Sstevel@tonic-gate { 18510Sstevel@tonic-gate int i; 18520Sstevel@tonic-gate if ((id != UNUSED) && (rsect == 0)) { 18530Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 18540Sstevel@tonic-gate if ((Old_Table[i].systid == id) && 18550Sstevel@tonic-gate (Old_Table[i].relsect == lel(rsect)) && 18565169Slclee (Old_Table[i].numsect == lel(numsect))) 18575169Slclee return (0); 18580Sstevel@tonic-gate } 1859251Slclee (void) fprintf(stderr, 1860251Slclee "New partition cannot start at sector 0\n"); 18610Sstevel@tonic-gate return (-1); 18620Sstevel@tonic-gate } 18630Sstevel@tonic-gate return (0); 18640Sstevel@tonic-gate } 18650Sstevel@tonic-gate 18660Sstevel@tonic-gate /* 18670Sstevel@tonic-gate * stage0 18680Sstevel@tonic-gate * Print out interactive menu and process user input. 18690Sstevel@tonic-gate */ 1870251Slclee static void 1871251Slclee stage0(void) 18720Sstevel@tonic-gate { 1873251Slclee dispmenu(); 1874251Slclee for (;;) { 1875251Slclee (void) printf(Q_LINE); 1876251Slclee (void) printf("Enter Selection: "); 1877251Slclee (void) gets(s); 18780Sstevel@tonic-gate rm_blanks(s); 18790Sstevel@tonic-gate while (!((s[0] > '0') && (s[0] < '7') && (s[1] == 0))) { 1880251Slclee (void) printf(E_LINE); /* Clear any previous error */ 1881251Slclee (void) printf( 1882251Slclee "Enter a one-digit number between 1 and 6."); 1883251Slclee (void) printf(Q_LINE); 1884251Slclee (void) printf("Enter Selection: "); 1885251Slclee (void) gets(s); 18860Sstevel@tonic-gate rm_blanks(s); 18870Sstevel@tonic-gate } 1888251Slclee (void) printf(E_LINE); 18890Sstevel@tonic-gate switch (s[0]) { 18900Sstevel@tonic-gate case '1': 18910Sstevel@tonic-gate if (pcreate() == -1) 18920Sstevel@tonic-gate return; 18930Sstevel@tonic-gate break; 18940Sstevel@tonic-gate case '2': 18950Sstevel@tonic-gate if (pchange() == -1) 18960Sstevel@tonic-gate return; 18970Sstevel@tonic-gate break; 18980Sstevel@tonic-gate case '3': 18990Sstevel@tonic-gate if (pdelete() == -1) 19000Sstevel@tonic-gate return; 19010Sstevel@tonic-gate break; 19020Sstevel@tonic-gate case '4': 19030Sstevel@tonic-gate if (ppartid() == -1) 19040Sstevel@tonic-gate return; 19050Sstevel@tonic-gate break; 19060Sstevel@tonic-gate case '5': 19070Sstevel@tonic-gate /* update disk partition table, if changed */ 19080Sstevel@tonic-gate if (TableChanged() == 1) { 19090Sstevel@tonic-gate copy_Table_to_Bootblk(); 19100Sstevel@tonic-gate dev_mboot_write(0, Bootsect, sectsiz); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate /* 19130Sstevel@tonic-gate * If the VTOC table is wrong fix it 19140Sstevel@tonic-gate * (truncate only) 19150Sstevel@tonic-gate */ 19160Sstevel@tonic-gate if (io_adjt) { 19170Sstevel@tonic-gate fix_slice(); 19180Sstevel@tonic-gate } 1919251Slclee (void) close(Dev); 19200Sstevel@tonic-gate exit(0); 1921251Slclee /* FALLTHRU */ 19220Sstevel@tonic-gate case '6': 19230Sstevel@tonic-gate /* 19240Sstevel@tonic-gate * If the VTOC table is wrong fix it 19250Sstevel@tonic-gate * (truncate only) 19260Sstevel@tonic-gate */ 19270Sstevel@tonic-gate if (io_adjt) { 19280Sstevel@tonic-gate fix_slice(); 19290Sstevel@tonic-gate } 1930251Slclee (void) close(Dev); 19310Sstevel@tonic-gate exit(0); 1932251Slclee /* FALLTHRU */ 19330Sstevel@tonic-gate default: 19340Sstevel@tonic-gate break; 19350Sstevel@tonic-gate } 19360Sstevel@tonic-gate copy_Table_to_Bootblk(); 19370Sstevel@tonic-gate disptbl(); 1938251Slclee dispmenu(); 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate } 19410Sstevel@tonic-gate 19420Sstevel@tonic-gate /* 19430Sstevel@tonic-gate * pcreate 19440Sstevel@tonic-gate * Create partition entry in the table (interactive mode). 19450Sstevel@tonic-gate */ 1946251Slclee static int 1947251Slclee pcreate(void) 19480Sstevel@tonic-gate { 1949251Slclee uchar_t tsystid = 'z'; 19500Sstevel@tonic-gate int i, j; 1951*7563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 19520Sstevel@tonic-gate int retCode = 0; 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate i = 0; 1955251Slclee for (;;) { 19560Sstevel@tonic-gate if (i == FD_NUMPART) { 1957251Slclee (void) printf(E_LINE); 1958251Slclee (void) printf( 1959251Slclee "The partition table is full!\n" 1960251Slclee "You must delete a partition before creating" 19610Sstevel@tonic-gate " a new one.\n"); 19620Sstevel@tonic-gate return (-1); 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 19650Sstevel@tonic-gate break; 19660Sstevel@tonic-gate } 19670Sstevel@tonic-gate i++; 19680Sstevel@tonic-gate } 19690Sstevel@tonic-gate 1970*7563SPrasad.Singamsetty@Sun.COM numsect = 0; 19710Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 19720Sstevel@tonic-gate if (Table[i].systid != UNUSED) { 1973*7563SPrasad.Singamsetty@Sun.COM numsect += lel(Table[i].numsect); 19740Sstevel@tonic-gate } 1975*7563SPrasad.Singamsetty@Sun.COM if (numsect >= chs_capacity) { 1976251Slclee (void) printf(E_LINE); 1977251Slclee (void) printf("There is no more room on the disk for" 19780Sstevel@tonic-gate " another partition.\n"); 1979251Slclee (void) printf( 1980251Slclee "You must delete a partition before creating" 19810Sstevel@tonic-gate " a new one.\n"); 19820Sstevel@tonic-gate return (-1); 19830Sstevel@tonic-gate } 19840Sstevel@tonic-gate } 19850Sstevel@tonic-gate while (tsystid == 'z') { 1986*7563SPrasad.Singamsetty@Sun.COM 1987*7563SPrasad.Singamsetty@Sun.COM /* 1988*7563SPrasad.Singamsetty@Sun.COM * The question here is expanding to more than what is 1989*7563SPrasad.Singamsetty@Sun.COM * allocated for question lines (Q_LINE) which garbles 1990*7563SPrasad.Singamsetty@Sun.COM * at least warning line. Clearing warning line as workaround 1991*7563SPrasad.Singamsetty@Sun.COM * for now. 1992*7563SPrasad.Singamsetty@Sun.COM */ 1993*7563SPrasad.Singamsetty@Sun.COM 1994*7563SPrasad.Singamsetty@Sun.COM (void) printf(W_LINE); 1995251Slclee (void) printf(Q_LINE); 1996251Slclee (void) printf( 1997251Slclee "Select the partition type to create:\n" 1998251Slclee " 1=SOLARIS2 2=UNIX 3=PCIXOS 4=Other\n" 1999251Slclee " 5=DOS12 6=DOS16 7=DOSEXT 8=DOSBIG\n" 2000251Slclee " 9=DOS16LBA A=x86 Boot B=Diagnostic C=FAT32\n" 2001251Slclee " D=FAT32LBA E=DOSEXTLBA F=EFI 0=Exit? "); 2002251Slclee (void) gets(s); 20030Sstevel@tonic-gate rm_blanks(s); 20040Sstevel@tonic-gate if (s[1] != 0) { 2005251Slclee (void) printf(E_LINE); 2006251Slclee (void) printf("Invalid selection, try again."); 20070Sstevel@tonic-gate continue; 20080Sstevel@tonic-gate } 20090Sstevel@tonic-gate switch (s[0]) { 20100Sstevel@tonic-gate case '0': /* exit */ 20115169Slclee (void) printf(E_LINE); 20125169Slclee return (-1); 20130Sstevel@tonic-gate case '1': /* Solaris partition */ 20145169Slclee tsystid = SUNIXOS2; 20155169Slclee break; 20160Sstevel@tonic-gate case '2': /* UNIX partition */ 20175169Slclee tsystid = UNIXOS; 20185169Slclee break; 20190Sstevel@tonic-gate case '3': /* PCIXOS partition */ 20205169Slclee tsystid = PCIXOS; 20215169Slclee break; 20220Sstevel@tonic-gate case '4': /* OTHEROS System partition */ 20235169Slclee tsystid = OTHEROS; 20245169Slclee break; 20250Sstevel@tonic-gate case '5': 20265169Slclee tsystid = DOSOS12; /* DOS 12 bit fat */ 20275169Slclee break; 20280Sstevel@tonic-gate case '6': 20295169Slclee tsystid = DOSOS16; /* DOS 16 bit fat */ 20305169Slclee break; 20310Sstevel@tonic-gate case '7': 20325169Slclee tsystid = EXTDOS; 20335169Slclee break; 20340Sstevel@tonic-gate case '8': 20355169Slclee tsystid = DOSHUGE; 20365169Slclee break; 20370Sstevel@tonic-gate case '9': 20385169Slclee tsystid = FDISK_FAT95; /* FAT16, need extended int13 */ 20395169Slclee break; 20400Sstevel@tonic-gate case 'a': /* x86 Boot partition */ 20410Sstevel@tonic-gate case 'A': 20425169Slclee tsystid = X86BOOT; 20435169Slclee break; 20440Sstevel@tonic-gate case 'b': /* Diagnostic boot partition */ 20450Sstevel@tonic-gate case 'B': 20465169Slclee tsystid = DIAGPART; 20475169Slclee break; 20480Sstevel@tonic-gate case 'c': /* FAT32 */ 20490Sstevel@tonic-gate case 'C': 20505169Slclee tsystid = FDISK_WINDOWS; 20515169Slclee break; 20520Sstevel@tonic-gate case 'd': /* FAT32 and need extended int13 */ 20530Sstevel@tonic-gate case 'D': 20545169Slclee tsystid = FDISK_EXT_WIN; 20555169Slclee break; 20560Sstevel@tonic-gate case 'e': /* Extended partition, need extended int13 */ 20570Sstevel@tonic-gate case 'E': 20585169Slclee tsystid = FDISK_EXTLBA; 20595169Slclee break; 20600Sstevel@tonic-gate case 'f': 20610Sstevel@tonic-gate case 'F': 20625169Slclee tsystid = EFI_PMBR; 20635169Slclee break; 20640Sstevel@tonic-gate default: 20655169Slclee (void) printf(E_LINE); 20665169Slclee (void) printf("Invalid selection, try again."); 20675169Slclee continue; 20680Sstevel@tonic-gate } 20690Sstevel@tonic-gate } 20700Sstevel@tonic-gate 2071251Slclee (void) printf(E_LINE); 20720Sstevel@tonic-gate 20730Sstevel@tonic-gate if (tsystid != EFI_PMBR) { 2074*7563SPrasad.Singamsetty@Sun.COM (void) printf(W_LINE); 2075*7563SPrasad.Singamsetty@Sun.COM if ((dev_capacity > DK_MAX_2TB)) 2076*7563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger than 2 TB. " 2077*7563SPrasad.Singamsetty@Sun.COM "Upper limit is 2 TB for non-EFI partition ID\n"); 2078*7563SPrasad.Singamsetty@Sun.COM 20790Sstevel@tonic-gate /* create the new partition */ 20800Sstevel@tonic-gate i = specify(tsystid); 20810Sstevel@tonic-gate 20820Sstevel@tonic-gate if (i != -1) { 20830Sstevel@tonic-gate /* see if it should be the active partition */ 2084251Slclee (void) printf(E_LINE); 2085251Slclee (void) printf(Q_LINE); 2086251Slclee 2087251Slclee (void) printf( 2088251Slclee "Should this become the active partition? If " 2089251Slclee "yes, it will be activated\n" 2090251Slclee "each time the computer is reset or turned on.\n" 2091251Slclee "Please type \"y\" or \"n\". "); 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate if (yesno()) { 2094251Slclee (void) printf(E_LINE); 20950Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 20960Sstevel@tonic-gate if (j == i) { 20970Sstevel@tonic-gate Table[j].bootid = ACTIVE; 2098251Slclee (void) printf(E_LINE); 2099251Slclee (void) printf( 2100251Slclee "Partition %d is now " 21010Sstevel@tonic-gate "the active partition.", 2102251Slclee j + 1); 21030Sstevel@tonic-gate } else { 21040Sstevel@tonic-gate Table[j].bootid = 0; 21050Sstevel@tonic-gate } 21060Sstevel@tonic-gate } 21070Sstevel@tonic-gate } else { 21080Sstevel@tonic-gate Table[i].bootid = 0; 21090Sstevel@tonic-gate } 21100Sstevel@tonic-gate 21110Sstevel@tonic-gate /* set up the return code */ 21120Sstevel@tonic-gate i = 1; 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate } else { 21150Sstevel@tonic-gate /* 21160Sstevel@tonic-gate * partitions of type EFI_PMBR must be the only partitions in 21170Sstevel@tonic-gate * the table 21180Sstevel@tonic-gate * 21190Sstevel@tonic-gate * First, make sure there were no errors the table is 21200Sstevel@tonic-gate * empty 21210Sstevel@tonic-gate */ 21220Sstevel@tonic-gate retCode = verify_tbl(); 21230Sstevel@tonic-gate 21240Sstevel@tonic-gate if (retCode < 0) { 2125251Slclee (void) fprintf(stderr, 21260Sstevel@tonic-gate "fdisk: Cannot create EFI partition table; \n" 21270Sstevel@tonic-gate "current partition table is invalid.\n"); 21280Sstevel@tonic-gate return (-1); 21290Sstevel@tonic-gate } else if (retCode > 0) { 2130251Slclee (void) printf( 2131251Slclee "An EFI partition must be the only partition on " 2132251Slclee "disk. You may manually delete existing\n" 2133251Slclee "partitions, or fdisk can do it.\n" 2134251Slclee "Do you want fdisk to destroy existing " 2135251Slclee "partitions?\n" 2136251Slclee "Please type \"y\" or \"n\". "); 21370Sstevel@tonic-gate 21380Sstevel@tonic-gate if (yesno()) { 21390Sstevel@tonic-gate nulltbl(); 21400Sstevel@tonic-gate } else { 21410Sstevel@tonic-gate return (-1); 21420Sstevel@tonic-gate } 21430Sstevel@tonic-gate } 21440Sstevel@tonic-gate 21450Sstevel@tonic-gate /* create the table entry - i should be 0 */ 2146*7563SPrasad.Singamsetty@Sun.COM i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 1, 2147*7563SPrasad.Singamsetty@Sun.COM (dev_capacity > DK_MAX_2TB) ? DK_MAX_2TB: 2148*7563SPrasad.Singamsetty@Sun.COM (dev_capacity - 1)); 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate if (i != 0) { 2151251Slclee (void) printf("Error creating EFI partition!!!\n"); 21520Sstevel@tonic-gate i = -1; 21530Sstevel@tonic-gate } else { 21540Sstevel@tonic-gate 21550Sstevel@tonic-gate /* EFI partitions are currently never active */ 21560Sstevel@tonic-gate Table[i].bootid = 0; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate /* set up the return code */ 21590Sstevel@tonic-gate i = 1; 21600Sstevel@tonic-gate } 21610Sstevel@tonic-gate } 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate return (i); 21640Sstevel@tonic-gate } 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate /* 21670Sstevel@tonic-gate * specify 21680Sstevel@tonic-gate * Query the user to specify the size of the new partition in 21690Sstevel@tonic-gate * terms of percentage of the disk or by specifying the starting 21700Sstevel@tonic-gate * cylinder and length in cylinders. 21710Sstevel@tonic-gate */ 2172251Slclee static int 2173251Slclee specify(uchar_t tsystid) 21740Sstevel@tonic-gate { 21755169Slclee int i, j, percent = -1; 2176*7563SPrasad.Singamsetty@Sun.COM int cyl, cylen; 2177*7563SPrasad.Singamsetty@Sun.COM diskaddr_t first_free, size_free; 2178*7563SPrasad.Singamsetty@Sun.COM diskaddr_t max_free; 21795169Slclee int cyl_size; 21800Sstevel@tonic-gate struct ipart *partition[FD_NUMPART]; 21810Sstevel@tonic-gate 21825169Slclee cyl_size = heads * sectors; 21835169Slclee 21845169Slclee /* 21855169Slclee * make a local copy of the partition table 21865169Slclee * and sort it into relsect order 21875169Slclee */ 21885169Slclee for (i = 0; i < FD_NUMPART; i++) 21895169Slclee partition[i] = &Table[i]; 21905169Slclee 21915169Slclee for (i = 0; i < FD_NUMPART - 1; i++) { 21925169Slclee if (partition[i]->systid == UNUSED) 21935169Slclee break; 21945169Slclee for (j = i + 1; j < FD_NUMPART; j++) { 21955169Slclee if (partition[j]->systid == UNUSED) 21965169Slclee break; 21975169Slclee if (lel(partition[j]->relsect) < 21985169Slclee lel(partition[i]->relsect)) { 21995169Slclee struct ipart *temp = partition[i]; 22005169Slclee partition[i] = partition[j]; 22015169Slclee partition[j] = temp; 22025169Slclee } 22035169Slclee } 22045169Slclee } 22055169Slclee 2206*7563SPrasad.Singamsetty@Sun.COM 2207251Slclee (void) printf(Q_LINE); 2208251Slclee (void) printf( 2209251Slclee "Specify the percentage of disk to use for this partition\n" 2210251Slclee "(or type \"c\" to specify the size in cylinders). "); 2211251Slclee (void) gets(s); 22120Sstevel@tonic-gate rm_blanks(s); 22130Sstevel@tonic-gate if (s[0] != 'c') { /* Specify size in percentage of disk */ 22145169Slclee i = 0; 22155169Slclee while (s[i] != '\0') { 22165169Slclee if (s[i] < '0' || s[i] > '9') { 22175169Slclee (void) printf(E_LINE); 22185169Slclee (void) printf("Invalid percentage value " 22195169Slclee "specified; retry the operation."); 22205169Slclee return (-1); 22215169Slclee } 22225169Slclee i++; 22235169Slclee if (i > 3) { 22245169Slclee (void) printf(E_LINE); 22255169Slclee (void) printf("Invalid percentage value " 22265169Slclee "specified; retry the operation."); 22275169Slclee return (-1); 22285169Slclee } 22295169Slclee } 22305169Slclee if ((percent = atoi(s)) > 100) { 22315169Slclee (void) printf(E_LINE); 22325169Slclee (void) printf( 22335169Slclee "Percentage value is too large. The value must be" 22345169Slclee " between 1 and 100;\nretry the operation.\n"); 22355169Slclee return (-1); 22360Sstevel@tonic-gate } 22375169Slclee if (percent < 1) { 22385169Slclee (void) printf(E_LINE); 22395169Slclee (void) printf( 22405169Slclee "Percentage value is too small. The value must be" 22415169Slclee " between 1 and 100;\nretry the operation.\n"); 22425169Slclee return (-1); 22435169Slclee } 22445169Slclee 22455169Slclee if (percent == 100) 2246*7563SPrasad.Singamsetty@Sun.COM cylen = Numcyl_usable - 1; 22475169Slclee else 2248*7563SPrasad.Singamsetty@Sun.COM cylen = (Numcyl_usable * percent) / 100; 22495169Slclee 22505169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 22515169Slclee if ((tsystid == DOSOS12) && 22525169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 22535169Slclee int n; 2254*7563SPrasad.Singamsetty@Sun.COM n = MAXDOS * 100 / (int)(cyl_size) / Numcyl_usable; 22555169Slclee (void) printf(E_LINE); 22565169Slclee (void) printf("Maximum size for a DOS partition " 22575169Slclee "is %d%%; retry the operation.", 22585169Slclee n <= 100 ? n : 100); 22595169Slclee return (-1); 22600Sstevel@tonic-gate } 22615169Slclee 22625169Slclee 22635169Slclee max_free = 0; 22645169Slclee for (i = 0; i < FD_NUMPART; i++) { 22655169Slclee 22665169Slclee /* 22675169Slclee * check for free space before partition i 22685169Slclee * where i varies from 0 to 3 22695169Slclee * 22705169Slclee * freespace after partition 3 is unusable 22715169Slclee * because there are no free partitions 22725169Slclee * 22735169Slclee * freespace begins at the end of previous partition 22745169Slclee * or cylinder 1 22755169Slclee */ 22765169Slclee if (i) { 22775169Slclee /* Not an empty table */ 22785169Slclee first_free = lel(partition[i - 1]->relsect) + 22795169Slclee lel(partition[i - 1]->numsect); 22805169Slclee } else { 22815169Slclee first_free = cyl_size; 22825169Slclee } 22835169Slclee 22845169Slclee /* 22855169Slclee * freespace ends before the current partition 22865169Slclee * or the end of the disk (chs end) 22875169Slclee */ 22885169Slclee if (partition[i]->systid == UNUSED) { 22895169Slclee size_free = chs_capacity - first_free; 22905169Slclee } else { 22915169Slclee size_free = 22925169Slclee lel(partition[i]->relsect) - first_free; 22935169Slclee } 22945169Slclee 22955169Slclee /* save largest free space */ 22965169Slclee if (max_free < size_free) 22975169Slclee max_free = size_free; 22985169Slclee 2299*7563SPrasad.Singamsetty@Sun.COM if (((uint64_t)cylen * cyl_size) <= size_free) { 23005169Slclee /* We found a place to use */ 23015169Slclee break; 23025169Slclee } 23035169Slclee if (partition[i]->systid == UNUSED) { 23045169Slclee (void) printf(E_LINE); 23055169Slclee max_free /= (cyl_size); 23065169Slclee (void) fprintf(stderr, "fdisk: " 2307*7563SPrasad.Singamsetty@Sun.COM "Maximum percentage available is %lld\n", 2308*7563SPrasad.Singamsetty@Sun.COM 100 * max_free / Numcyl_usable); 23095169Slclee return (-1); 23105169Slclee } 23110Sstevel@tonic-gate } 23125169Slclee 23135169Slclee (void) printf(E_LINE); 23145169Slclee if (i >= FD_NUMPART) { 23155169Slclee (void) fprintf(stderr, 23165169Slclee "fdisk: Partition table is full.\n"); 23175169Slclee return (-1); 23185169Slclee } 23195169Slclee 23205169Slclee if ((i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 23215169Slclee first_free, cylen * cyl_size)) >= 0) { 23225169Slclee return (i); 23235169Slclee } 23245169Slclee return (-1); 23255169Slclee } else { 23265169Slclee 23275169Slclee /* Specifying size in cylinders */ 2328251Slclee (void) printf(E_LINE); 23295169Slclee (void) printf(Q_LINE); 23305169Slclee (void) printf("Enter starting cylinder number: "); 23315169Slclee if ((cyl = getcyl()) == -1) { 23325169Slclee (void) printf(E_LINE); 23335169Slclee (void) printf("Invalid number; retry the operation."); 23345169Slclee return (-1); 23355169Slclee } 23365169Slclee if (cyl == 0) { 23375169Slclee (void) printf(E_LINE); 23385169Slclee (void) printf( 23395169Slclee "New partition cannot start at cylinder 0.\n"); 23405169Slclee return (-1); 23415169Slclee } 2342*7563SPrasad.Singamsetty@Sun.COM 2343*7563SPrasad.Singamsetty@Sun.COM 2344*7563SPrasad.Singamsetty@Sun.COM if (cyl >= Numcyl_usable) { 23455169Slclee (void) printf(E_LINE); 23465169Slclee (void) printf( 23475169Slclee "Cylinder %d is out of bounds, " 23485169Slclee "the maximum is %d.\n", 2349*7563SPrasad.Singamsetty@Sun.COM cyl, Numcyl_usable - 1); 23505169Slclee return (-1); 23515169Slclee } 2352*7563SPrasad.Singamsetty@Sun.COM 23535169Slclee (void) printf(Q_LINE); 23545169Slclee (void) printf("Enter partition size in cylinders: "); 23555169Slclee if ((cylen = getcyl()) == -1) { 23565169Slclee (void) printf(E_LINE); 23575169Slclee (void) printf("Invalid number, retry the operation."); 23585169Slclee return (-1); 23595169Slclee } 23605169Slclee 23615169Slclee for (i = 0; i < FD_NUMPART; i++) { 23625169Slclee uint32_t t_relsect, t_numsect; 23635169Slclee 23645169Slclee if (partition[i]->systid == UNUSED) 23655169Slclee break; 23665169Slclee t_relsect = lel(partition[i]->relsect); 23675169Slclee t_numsect = lel(partition[i]->numsect); 23685169Slclee 23695169Slclee if (cyl * cyl_size >= t_relsect && 23705169Slclee cyl * cyl_size < t_relsect + t_numsect) { 23715169Slclee (void) printf(E_LINE); 23725169Slclee (void) printf( 23735169Slclee "Cylinder %d is already allocated" 23745169Slclee "\nretry the operation.", 23755169Slclee cyl); 23765169Slclee return (-1); 23775169Slclee } 23785169Slclee 23795169Slclee if (cyl * cyl_size < t_relsect && 23805169Slclee (cyl + cylen - 1) * cyl_size > t_relsect) { 23815169Slclee (void) printf(E_LINE); 23825169Slclee (void) printf( 23835169Slclee "Maximum size for partition is %u cylinders" 23845169Slclee "\nretry the operation.", 23855169Slclee (t_relsect - cyl * cyl_size) / cyl_size); 23865169Slclee return (-1); 23875169Slclee } 23885169Slclee } 23895169Slclee 2390*7563SPrasad.Singamsetty@Sun.COM /* Verify partition doesn't exceed disk size or 2 TB */ 2391*7563SPrasad.Singamsetty@Sun.COM if (cyl + cylen > Numcyl_usable) { 23925169Slclee (void) printf(E_LINE); 2393*7563SPrasad.Singamsetty@Sun.COM if (Numcyl > Numcyl_usable) { 2394*7563SPrasad.Singamsetty@Sun.COM (void) printf( 2395*7563SPrasad.Singamsetty@Sun.COM "Maximum size for partition is %d " 2396*7563SPrasad.Singamsetty@Sun.COM "cylinders; \nretry the operation.", 2397*7563SPrasad.Singamsetty@Sun.COM Numcyl_usable - cyl); 2398*7563SPrasad.Singamsetty@Sun.COM } else { 2399*7563SPrasad.Singamsetty@Sun.COM (void) printf( 2400*7563SPrasad.Singamsetty@Sun.COM "Maximum size for partition is %d " 2401*7563SPrasad.Singamsetty@Sun.COM "cylinders; \nretry the operation.", 2402*7563SPrasad.Singamsetty@Sun.COM Numcyl_usable - cyl); 2403*7563SPrasad.Singamsetty@Sun.COM } 24045169Slclee return (-1); 24055169Slclee } 24065169Slclee 24075169Slclee /* Verify DOS12 partition doesn't exceed max size of 32MB. */ 24085169Slclee if ((tsystid == DOSOS12) && 24095169Slclee ((long)((long)cylen * cyl_size) > MAXDOS)) { 24105169Slclee (void) printf(E_LINE); 24115169Slclee (void) printf( 24125169Slclee "Maximum size for a %s partition is %ld cylinders;" 24135169Slclee "\nretry the operation.", 24145169Slclee Dstr, MAXDOS / (int)(cyl_size)); 24155169Slclee return (-1); 24165169Slclee } 24175169Slclee 2418251Slclee (void) printf(E_LINE); 24195169Slclee i = insert_tbl(tsystid, 0, 0, 0, 0, 0, 0, 0, 24205169Slclee cyl * cyl_size, cylen * cyl_size); 24215169Slclee if (i < 0) 24225169Slclee return (-1); 24235169Slclee 24245169Slclee if (verify_tbl() < 0) { 24255169Slclee (void) printf(E_LINE); 24265169Slclee (void) printf("fdisk: Cannot create partition table\n"); 24275169Slclee return (-1); 24285169Slclee } 24295169Slclee 24305169Slclee return (i); 24310Sstevel@tonic-gate } 24320Sstevel@tonic-gate } 24330Sstevel@tonic-gate 24340Sstevel@tonic-gate /* 24350Sstevel@tonic-gate * dispmenu 24360Sstevel@tonic-gate * Display command menu (interactive mode). 24370Sstevel@tonic-gate */ 2438251Slclee static void 2439251Slclee dispmenu(void) 24400Sstevel@tonic-gate { 2441251Slclee (void) printf(M_LINE); 2442251Slclee (void) printf( 2443251Slclee "SELECT ONE OF THE FOLLOWING:\n" 2444251Slclee " 1. Create a partition\n" 2445251Slclee " 2. Specify the active partition\n" 2446251Slclee " 3. Delete a partition\n" 2447251Slclee " 4. Change between Solaris and Solaris2 Partition IDs\n" 2448251Slclee " 5. Exit (update disk configuration and exit)\n" 2449251Slclee " 6. Cancel (exit without updating disk configuration)\n"); 24500Sstevel@tonic-gate } 24510Sstevel@tonic-gate 24520Sstevel@tonic-gate /* 24530Sstevel@tonic-gate * pchange 24540Sstevel@tonic-gate * Change the ACTIVE designation of a partition. 24550Sstevel@tonic-gate */ 2456251Slclee static int 2457251Slclee pchange(void) 24580Sstevel@tonic-gate { 24590Sstevel@tonic-gate char s[80]; 24600Sstevel@tonic-gate int i, j; 24610Sstevel@tonic-gate 2462251Slclee for (;;) { 2463251Slclee (void) printf(Q_LINE); 24640Sstevel@tonic-gate { 2465251Slclee (void) printf( 2466251Slclee "Specify the partition number to boot from" 24670Sstevel@tonic-gate " (or specify 0 for none): "); 24680Sstevel@tonic-gate } 2469251Slclee (void) gets(s); 24700Sstevel@tonic-gate rm_blanks(s); 24710Sstevel@tonic-gate if ((s[1] != 0) || (s[0] < '0') || (s[0] > '4')) { 2472251Slclee (void) printf(E_LINE); 2473251Slclee (void) printf( 2474251Slclee "Invalid response, please specify a number" 24750Sstevel@tonic-gate " between 0 and 4.\n"); 24760Sstevel@tonic-gate } else { 24770Sstevel@tonic-gate break; 24780Sstevel@tonic-gate } 24790Sstevel@tonic-gate } 24800Sstevel@tonic-gate if (s[0] == '0') { /* No active partitions */ 24810Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 24820Sstevel@tonic-gate if (Table[i].systid != UNUSED && 24830Sstevel@tonic-gate Table[i].bootid == ACTIVE) 24840Sstevel@tonic-gate Table[i].bootid = 0; 24850Sstevel@tonic-gate } 2486251Slclee (void) printf(E_LINE); 2487251Slclee (void) printf( 2488251Slclee "No partition is currently marked as active."); 24890Sstevel@tonic-gate return (0); 24900Sstevel@tonic-gate } else { /* User has selected a partition to be active */ 24910Sstevel@tonic-gate i = s[0] - '1'; 24920Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2493251Slclee (void) printf(E_LINE); 2494251Slclee (void) printf("Partition does not exist."); 24950Sstevel@tonic-gate return (-1); 24960Sstevel@tonic-gate } 24970Sstevel@tonic-gate /* a DOS-DATA or EXT-DOS partition cannot be active */ 24980Sstevel@tonic-gate else if ((Table[i].systid == DOSDATA) || 24990Sstevel@tonic-gate (Table[i].systid == EXTDOS) || 25000Sstevel@tonic-gate (Table[i].systid == FDISK_EXTLBA)) { 2501251Slclee (void) printf(E_LINE); 2502251Slclee (void) printf( 2503251Slclee "DOS-DATA, EXT_DOS and EXT_DOS_LBA partitions " 25040Sstevel@tonic-gate "cannot be made active.\n"); 2505251Slclee (void) printf("Select another partition."); 25060Sstevel@tonic-gate return (-1); 25070Sstevel@tonic-gate } 25080Sstevel@tonic-gate Table[i].bootid = ACTIVE; 25090Sstevel@tonic-gate for (j = 0; j < FD_NUMPART; j++) { 25100Sstevel@tonic-gate if (j != i) 25110Sstevel@tonic-gate Table[j].bootid = 0; 25120Sstevel@tonic-gate } 25130Sstevel@tonic-gate } 2514251Slclee (void) printf(E_LINE); 25150Sstevel@tonic-gate { 2516251Slclee (void) printf( 2517251Slclee "Partition %d is now active. The system will start up" 2518251Slclee " from this\n", i + 1); 2519251Slclee (void) printf("partition after the next reboot."); 25200Sstevel@tonic-gate } 25210Sstevel@tonic-gate return (1); 25220Sstevel@tonic-gate } 25230Sstevel@tonic-gate 25240Sstevel@tonic-gate /* 25250Sstevel@tonic-gate * Change between SOLARIS and SOLARIS2 partition id 25260Sstevel@tonic-gate */ 2527251Slclee static int 2528251Slclee ppartid(void) 25290Sstevel@tonic-gate { 25300Sstevel@tonic-gate char *p, s[80]; 25310Sstevel@tonic-gate int i; 25320Sstevel@tonic-gate 25330Sstevel@tonic-gate for (;;) { 2534251Slclee (void) printf(Q_LINE); 2535251Slclee (void) printf("Specify the partition number to change" 25365169Slclee " (or enter 0 to exit): "); 2537251Slclee if (!fgets(s, sizeof (s), stdin)) 2538251Slclee return (1); 25390Sstevel@tonic-gate i = strtol(s, &p, 10); 25400Sstevel@tonic-gate 25410Sstevel@tonic-gate if (*p != '\n' || i < 0 || i > FD_NUMPART) { 2542251Slclee (void) printf(E_LINE); 2543251Slclee (void) printf( 2544251Slclee "Invalid response, retry the operation.\n"); 25450Sstevel@tonic-gate continue; 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate if (i == 0) { 25490Sstevel@tonic-gate /* exit delete command */ 2550251Slclee (void) printf(E_LINE); /* clear error message */ 25510Sstevel@tonic-gate return (1); 25520Sstevel@tonic-gate } 25530Sstevel@tonic-gate 25540Sstevel@tonic-gate i -= 1; 25550Sstevel@tonic-gate if (Table[i].systid == SUNIXOS) { 25560Sstevel@tonic-gate Table[i].systid = SUNIXOS2; 25570Sstevel@tonic-gate } else if (Table[i].systid == SUNIXOS2) { 25580Sstevel@tonic-gate Table[i].systid = SUNIXOS; 25590Sstevel@tonic-gate } else { 2560251Slclee (void) printf(E_LINE); 2561251Slclee (void) printf( 2562251Slclee "Partition %d is not a Solaris partition.", 25630Sstevel@tonic-gate i + 1); 25640Sstevel@tonic-gate continue; 25650Sstevel@tonic-gate } 25660Sstevel@tonic-gate 2567251Slclee (void) printf(E_LINE); 2568251Slclee (void) printf("Partition %d has been changed.", i + 1); 25690Sstevel@tonic-gate return (1); 25700Sstevel@tonic-gate } 25710Sstevel@tonic-gate } 25720Sstevel@tonic-gate 25730Sstevel@tonic-gate /* 25740Sstevel@tonic-gate * pdelete 25750Sstevel@tonic-gate * Remove partition entry from the table (interactive mode). 25760Sstevel@tonic-gate */ 2577251Slclee static char 2578251Slclee pdelete(void) 25790Sstevel@tonic-gate { 25800Sstevel@tonic-gate char s[80]; 25810Sstevel@tonic-gate int i, j; 25820Sstevel@tonic-gate char pactive; 25830Sstevel@tonic-gate 2584251Slclee DEL1: (void) printf(Q_LINE); 2585251Slclee (void) printf("Specify the partition number to delete" 25860Sstevel@tonic-gate " (or enter 0 to exit): "); 2587251Slclee (void) gets(s); 25880Sstevel@tonic-gate rm_blanks(s); 25890Sstevel@tonic-gate if ((s[0] == '0')) { /* exit delete command */ 2590251Slclee (void) printf(E_LINE); /* clear error message */ 25910Sstevel@tonic-gate return (1); 25920Sstevel@tonic-gate } 25930Sstevel@tonic-gate /* Accept only a single digit between 1 and 4 */ 25940Sstevel@tonic-gate if (s[1] != 0 || (i = atoi(s)) < 1 || i > FD_NUMPART) { 2595251Slclee (void) printf(E_LINE); 2596251Slclee (void) printf("Invalid response, retry the operation.\n"); 25970Sstevel@tonic-gate goto DEL1; 25980Sstevel@tonic-gate } else { /* Found a digit between 1 and 4 */ 25990Sstevel@tonic-gate --i; /* Structure begins with element 0 */ 26000Sstevel@tonic-gate } 26010Sstevel@tonic-gate 26020Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2603251Slclee (void) printf(E_LINE); 2604251Slclee (void) printf("Partition %d does not exist.", i + 1); 26050Sstevel@tonic-gate return (-1); 26060Sstevel@tonic-gate } 26070Sstevel@tonic-gate 2608251Slclee (void) printf(Q_LINE); 2609251Slclee (void) printf("Are you sure you want to delete partition %d?" 2610251Slclee " This will make all files and \n", i + 1); 2611251Slclee (void) printf("programs in this partition inaccessible (type" 26120Sstevel@tonic-gate " \"y\" or \"n\"). "); 26130Sstevel@tonic-gate 2614251Slclee (void) printf(E_LINE); 26150Sstevel@tonic-gate if (! yesno()) { 26160Sstevel@tonic-gate return (1); 26170Sstevel@tonic-gate } 26180Sstevel@tonic-gate 26190Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) { 26200Sstevel@tonic-gate pactive = 1; 26210Sstevel@tonic-gate } else { 26220Sstevel@tonic-gate pactive = 0; 26230Sstevel@tonic-gate } 26240Sstevel@tonic-gate 26250Sstevel@tonic-gate for (j = i; j < FD_NUMPART - 1; j++) { 26265169Slclee Table[j] = Table[j + 1]; 26270Sstevel@tonic-gate } 26280Sstevel@tonic-gate 26290Sstevel@tonic-gate Table[j].systid = UNUSED; 26300Sstevel@tonic-gate Table[j].numsect = 0; 26310Sstevel@tonic-gate Table[j].relsect = 0; 26320Sstevel@tonic-gate Table[j].bootid = 0; 2633251Slclee (void) printf(E_LINE); 2634251Slclee (void) printf("Partition %d has been deleted.", i + 1); 26350Sstevel@tonic-gate 26360Sstevel@tonic-gate if (pactive) { 26375169Slclee (void) printf(" This was the active partition."); 26380Sstevel@tonic-gate } 26390Sstevel@tonic-gate 26400Sstevel@tonic-gate return (1); 26410Sstevel@tonic-gate } 26420Sstevel@tonic-gate 26430Sstevel@tonic-gate /* 26440Sstevel@tonic-gate * rm_blanks 26450Sstevel@tonic-gate * Remove blanks from strings of user responses. 26460Sstevel@tonic-gate */ 2647251Slclee static void 2648251Slclee rm_blanks(char *s) 26490Sstevel@tonic-gate { 26500Sstevel@tonic-gate register int i, j; 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate for (i = 0; i < CBUFLEN; i++) { 26530Sstevel@tonic-gate if ((s[i] == ' ') || (s[i] == '\t')) 26540Sstevel@tonic-gate continue; 26550Sstevel@tonic-gate else 26560Sstevel@tonic-gate /* Found first non-blank character of the string */ 26570Sstevel@tonic-gate break; 26580Sstevel@tonic-gate } 26590Sstevel@tonic-gate for (j = 0; i < CBUFLEN; j++, i++) { 26600Sstevel@tonic-gate if ((s[j] = s[i]) == '\0') { 26610Sstevel@tonic-gate /* Reached end of string */ 26620Sstevel@tonic-gate return; 26630Sstevel@tonic-gate } 26640Sstevel@tonic-gate } 26650Sstevel@tonic-gate } 26660Sstevel@tonic-gate 26670Sstevel@tonic-gate /* 26680Sstevel@tonic-gate * getcyl 26690Sstevel@tonic-gate * Take the user-specified cylinder number and convert it from a 26700Sstevel@tonic-gate * string to a decimal value. 26710Sstevel@tonic-gate */ 2672251Slclee static int 2673251Slclee getcyl(void) 26740Sstevel@tonic-gate { 26750Sstevel@tonic-gate int slen, i, j; 26760Sstevel@tonic-gate unsigned int cyl; 2677251Slclee (void) gets(s); 26780Sstevel@tonic-gate rm_blanks(s); 26790Sstevel@tonic-gate slen = strlen(s); 26800Sstevel@tonic-gate j = 1; 26810Sstevel@tonic-gate cyl = 0; 2682251Slclee for (i = slen - 1; i >= 0; i--) { 26830Sstevel@tonic-gate if (s[i] < '0' || s[i] > '9') { 26840Sstevel@tonic-gate return (-1); 26850Sstevel@tonic-gate } 2686251Slclee cyl += (j * (s[i] - '0')); 26870Sstevel@tonic-gate j *= 10; 26880Sstevel@tonic-gate } 26890Sstevel@tonic-gate return (cyl); 26900Sstevel@tonic-gate } 26910Sstevel@tonic-gate 26920Sstevel@tonic-gate /* 26930Sstevel@tonic-gate * disptbl 26940Sstevel@tonic-gate * Display the current fdisk table; determine percentage 26950Sstevel@tonic-gate * of the disk used for each partition. 26960Sstevel@tonic-gate */ 2697251Slclee static void 2698251Slclee disptbl(void) 26990Sstevel@tonic-gate { 27000Sstevel@tonic-gate int i; 27010Sstevel@tonic-gate unsigned int startcyl, endcyl, length, percent, remainder; 27020Sstevel@tonic-gate char *stat, *type; 2703*7563SPrasad.Singamsetty@Sun.COM int is_pmbr = 0; 27040Sstevel@tonic-gate 27050Sstevel@tonic-gate if ((heads == 0) || (sectors == 0)) { 2706251Slclee (void) printf("WARNING: critical disk geometry information" 27075169Slclee " missing!\n"); 2708251Slclee (void) printf("\theads = %d, sectors = %d\n", heads, sectors); 27090Sstevel@tonic-gate exit(1); 27100Sstevel@tonic-gate } 27110Sstevel@tonic-gate 2712251Slclee (void) printf(HOME); 2713251Slclee (void) printf(T_LINE); 2714251Slclee (void) printf(" Total disk size is %d cylinders\n", Numcyl); 2715251Slclee (void) printf(" Cylinder size is %d (512 byte) blocks\n\n", 2716251Slclee heads * sectors); 2717251Slclee (void) printf( 2718251Slclee " Cylinders\n"); 2719251Slclee (void) printf( 2720251Slclee " Partition Status Type Start End Length" 27210Sstevel@tonic-gate " %%\n"); 2722251Slclee (void) printf( 2723251Slclee " ========= ====== ============ ===== === ======" 27240Sstevel@tonic-gate " ==="); 27250Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 27260Sstevel@tonic-gate if (Table[i].systid == UNUSED) { 2727251Slclee (void) printf("\n"); 2728251Slclee (void) printf(CLR_LIN); 27290Sstevel@tonic-gate continue; 27300Sstevel@tonic-gate } 27310Sstevel@tonic-gate if (Table[i].bootid == ACTIVE) 27325169Slclee stat = Actvstr; 27330Sstevel@tonic-gate else 27345169Slclee stat = NAstr; 27350Sstevel@tonic-gate switch (Table[i].systid) { 27360Sstevel@tonic-gate case UNIXOS: 27375169Slclee type = Ustr; 27385169Slclee break; 27390Sstevel@tonic-gate case SUNIXOS: 27405169Slclee type = SUstr; 27415169Slclee break; 27420Sstevel@tonic-gate case SUNIXOS2: 27435169Slclee type = SU2str; 27445169Slclee break; 27450Sstevel@tonic-gate case X86BOOT: 27465169Slclee type = X86str; 27475169Slclee break; 27480Sstevel@tonic-gate case DOSOS12: 27495169Slclee type = Dstr; 27505169Slclee break; 27510Sstevel@tonic-gate case DOSOS16: 27525169Slclee type = D16str; 27535169Slclee break; 27540Sstevel@tonic-gate case EXTDOS: 27555169Slclee type = EDstr; 27565169Slclee break; 27570Sstevel@tonic-gate case DOSDATA: 27585169Slclee type = DDstr; 27595169Slclee break; 27600Sstevel@tonic-gate case DOSHUGE: 27615169Slclee type = DBstr; 27625169Slclee break; 27630Sstevel@tonic-gate case PCIXOS: 27645169Slclee type = PCstr; 27655169Slclee break; 27660Sstevel@tonic-gate case DIAGPART: 27675169Slclee type = DIAGstr; 27685169Slclee break; 27690Sstevel@tonic-gate case FDISK_IFS: 27705169Slclee type = IFSstr; 27715169Slclee break; 27720Sstevel@tonic-gate case FDISK_AIXBOOT: 27735169Slclee type = AIXstr; 27745169Slclee break; 27750Sstevel@tonic-gate case FDISK_AIXDATA: 27765169Slclee type = AIXDstr; 27775169Slclee break; 27780Sstevel@tonic-gate case FDISK_OS2BOOT: 27795169Slclee type = OS2str; 27805169Slclee break; 27810Sstevel@tonic-gate case FDISK_WINDOWS: 27825169Slclee type = WINstr; 27835169Slclee break; 27840Sstevel@tonic-gate case FDISK_EXT_WIN: 27855169Slclee type = EWINstr; 27865169Slclee break; 27870Sstevel@tonic-gate case FDISK_FAT95: 27885169Slclee type = FAT95str; 27895169Slclee break; 27900Sstevel@tonic-gate case FDISK_EXTLBA: 27915169Slclee type = EXTLstr; 27925169Slclee break; 27930Sstevel@tonic-gate case FDISK_LINUX: 27945169Slclee type = LINUXstr; 27955169Slclee break; 27960Sstevel@tonic-gate case FDISK_CPM: 27975169Slclee type = CPMstr; 27985169Slclee break; 27990Sstevel@tonic-gate case FDISK_NOVELL3: 28005169Slclee type = NOVstr; 28015169Slclee break; 28020Sstevel@tonic-gate case FDISK_QNX4: 28035169Slclee type = QNXstr; 28045169Slclee break; 28050Sstevel@tonic-gate case FDISK_QNX42: 28065169Slclee type = QNX2str; 28075169Slclee break; 28080Sstevel@tonic-gate case FDISK_QNX43: 28095169Slclee type = QNX3str; 28105169Slclee break; 28110Sstevel@tonic-gate case FDISK_LINUXNAT: 28125169Slclee type = LINNATstr; 28135169Slclee break; 28140Sstevel@tonic-gate case FDISK_NTFSVOL1: 28155169Slclee type = NTFSVOL1str; 28165169Slclee break; 28170Sstevel@tonic-gate case FDISK_NTFSVOL2: 28185169Slclee type = NTFSVOL2str; 28195169Slclee break; 28200Sstevel@tonic-gate case FDISK_BSD: 28215169Slclee type = BSDstr; 28225169Slclee break; 28230Sstevel@tonic-gate case FDISK_NEXTSTEP: 28245169Slclee type = NEXTSTEPstr; 28255169Slclee break; 28260Sstevel@tonic-gate case FDISK_BSDIFS: 28275169Slclee type = BSDIFSstr; 28285169Slclee break; 28290Sstevel@tonic-gate case FDISK_BSDISWAP: 28305169Slclee type = BSDISWAPstr; 28315169Slclee break; 28320Sstevel@tonic-gate case EFI_PMBR: 28335169Slclee type = EFIstr; 2834*7563SPrasad.Singamsetty@Sun.COM if (lel(Table[i].numsect) == DK_MAX_2TB) 2835*7563SPrasad.Singamsetty@Sun.COM is_pmbr = 1; 2836*7563SPrasad.Singamsetty@Sun.COM 28375169Slclee break; 28380Sstevel@tonic-gate default: 28395169Slclee type = Ostr; 28405169Slclee break; 28410Sstevel@tonic-gate } 28425936Sbharding startcyl = lel(Table[i].relsect) / 28435936Sbharding (unsigned long)(heads * sectors); 2844*7563SPrasad.Singamsetty@Sun.COM 2845*7563SPrasad.Singamsetty@Sun.COM if (lel(Table[i].numsect) == DK_MAX_2TB) { 2846*7563SPrasad.Singamsetty@Sun.COM endcyl = Numcyl - 1; 2847*7563SPrasad.Singamsetty@Sun.COM length = endcyl - startcyl + 1; 2848*7563SPrasad.Singamsetty@Sun.COM } else { 2849*7563SPrasad.Singamsetty@Sun.COM length = lel(Table[i].numsect) / 2850*7563SPrasad.Singamsetty@Sun.COM (unsigned long)(heads * sectors); 2851*7563SPrasad.Singamsetty@Sun.COM if (lel(Table[i].numsect) % 2852*7563SPrasad.Singamsetty@Sun.COM (unsigned long)(heads * sectors)) 2853*7563SPrasad.Singamsetty@Sun.COM length++; 2854*7563SPrasad.Singamsetty@Sun.COM endcyl = startcyl + length - 1; 2855*7563SPrasad.Singamsetty@Sun.COM } 2856*7563SPrasad.Singamsetty@Sun.COM 2857*7563SPrasad.Singamsetty@Sun.COM percent = length * 100 / Numcyl_usable; 2858*7563SPrasad.Singamsetty@Sun.COM if ((remainder = (length * 100 % Numcyl_usable)) != 0) { 2859*7563SPrasad.Singamsetty@Sun.COM if ((remainder * 100 / Numcyl_usable) > 50) { 28600Sstevel@tonic-gate /* round up */ 28610Sstevel@tonic-gate percent++; 28620Sstevel@tonic-gate } 28630Sstevel@tonic-gate /* Else leave the percent as is since it's already */ 28640Sstevel@tonic-gate /* rounded down */ 28650Sstevel@tonic-gate } 28660Sstevel@tonic-gate if (percent > 100) 28670Sstevel@tonic-gate percent = 100; 2868251Slclee (void) printf( 2869251Slclee "\n %d %s %-12.12s %4d %4d %4d" 2870251Slclee " %3d", 2871251Slclee i + 1, stat, type, startcyl, endcyl, length, percent); 28720Sstevel@tonic-gate } 2873*7563SPrasad.Singamsetty@Sun.COM 28740Sstevel@tonic-gate /* Print warning message if table is empty */ 28750Sstevel@tonic-gate if (Table[0].systid == UNUSED) { 2876251Slclee (void) printf(W_LINE); 2877251Slclee (void) printf("WARNING: no partitions are defined!"); 28780Sstevel@tonic-gate } else { 28790Sstevel@tonic-gate /* Clear the warning line */ 2880251Slclee (void) printf(W_LINE); 2881*7563SPrasad.Singamsetty@Sun.COM 2882*7563SPrasad.Singamsetty@Sun.COM /* Print warning if disk > 2TB and is not EFI PMBR */ 2883*7563SPrasad.Singamsetty@Sun.COM if (!is_pmbr && (dev_capacity > DK_MAX_2TB)) 2884*7563SPrasad.Singamsetty@Sun.COM (void) printf("WARNING: Disk is larger than 2 TB. " 2885*7563SPrasad.Singamsetty@Sun.COM "Upper limit is 2 TB for non-EFI partition ID\n"); 28860Sstevel@tonic-gate } 28870Sstevel@tonic-gate } 28880Sstevel@tonic-gate 28890Sstevel@tonic-gate /* 28900Sstevel@tonic-gate * print_Table 28910Sstevel@tonic-gate * Write the detailed fdisk table to standard error for 28920Sstevel@tonic-gate * the selected disk device. 28930Sstevel@tonic-gate */ 2894251Slclee static void 2895251Slclee print_Table(void) 2896251Slclee { 28970Sstevel@tonic-gate int i; 28980Sstevel@tonic-gate 2899251Slclee (void) fprintf(stderr, 29000Sstevel@tonic-gate " SYSID ACT BHEAD BSECT BEGCYL EHEAD ESECT ENDCYL RELSECT" 29010Sstevel@tonic-gate " NUMSECT\n"); 29020Sstevel@tonic-gate 29030Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 2904251Slclee (void) fprintf(stderr, " %-5d ", Table[i].systid); 2905251Slclee (void) fprintf(stderr, "%-3d ", Table[i].bootid); 2906251Slclee (void) fprintf(stderr, "%-5d ", Table[i].beghead); 2907251Slclee (void) fprintf(stderr, "%-5d ", Table[i].begsect & 0x3f); 29085169Slclee (void) fprintf(stderr, "%-8d ", 29095169Slclee (((uint_t)Table[i].begsect & 0xc0) << 2) + Table[i].begcyl); 29100Sstevel@tonic-gate 2911251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endhead); 2912251Slclee (void) fprintf(stderr, "%-5d ", Table[i].endsect & 0x3f); 29135169Slclee (void) fprintf(stderr, "%-8d ", 29145169Slclee (((uint_t)Table[i].endsect & 0xc0) << 2) + Table[i].endcyl); 2915*7563SPrasad.Singamsetty@Sun.COM (void) fprintf(stderr, "%-10u ", lel(Table[i].relsect)); 2916*7563SPrasad.Singamsetty@Sun.COM (void) fprintf(stderr, "%-10u\n", lel(Table[i].numsect)); 29170Sstevel@tonic-gate 29180Sstevel@tonic-gate } 29190Sstevel@tonic-gate } 29200Sstevel@tonic-gate 29210Sstevel@tonic-gate /* 29220Sstevel@tonic-gate * copy_Table_to_Old_Table 29230Sstevel@tonic-gate * Copy Table into Old_Table. The function only copies the systid, 29240Sstevel@tonic-gate * numsect, relsect, and bootid values because they are the only 29250Sstevel@tonic-gate * ones compared when determining if Table has changed. 29260Sstevel@tonic-gate */ 2927251Slclee static void 2928251Slclee copy_Table_to_Old_Table(void) 29290Sstevel@tonic-gate { 29300Sstevel@tonic-gate int i; 29310Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 29325169Slclee (void) memcpy(&Old_Table[i], &Table[i], sizeof (Table[0])); 29330Sstevel@tonic-gate } 29340Sstevel@tonic-gate } 29350Sstevel@tonic-gate 29360Sstevel@tonic-gate /* 29370Sstevel@tonic-gate * nulltbl 29380Sstevel@tonic-gate * Zero out the systid, numsect, relsect, and bootid values in the 29390Sstevel@tonic-gate * fdisk table. 29400Sstevel@tonic-gate */ 2941251Slclee static void 2942251Slclee nulltbl(void) 29430Sstevel@tonic-gate { 29440Sstevel@tonic-gate int i; 29450Sstevel@tonic-gate 29460Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 29475169Slclee Table[i].systid = UNUSED; 29485169Slclee Table[i].numsect = lel(UNUSED); 29495169Slclee Table[i].relsect = lel(UNUSED); 29505169Slclee Table[i].bootid = 0; 29510Sstevel@tonic-gate } 29520Sstevel@tonic-gate } 29530Sstevel@tonic-gate 29540Sstevel@tonic-gate /* 29550Sstevel@tonic-gate * copy_Bootblk_to_Table 29560Sstevel@tonic-gate * Copy the bytes from the boot record to an internal "Table". 29570Sstevel@tonic-gate * All unused are padded with zeros starting at offset 446. 29580Sstevel@tonic-gate */ 2959251Slclee static void 2960251Slclee copy_Bootblk_to_Table(void) 29610Sstevel@tonic-gate { 29620Sstevel@tonic-gate int i, j; 29630Sstevel@tonic-gate char *bootptr; 29640Sstevel@tonic-gate struct ipart iparts[FD_NUMPART]; 29650Sstevel@tonic-gate 29660Sstevel@tonic-gate /* Get an aligned copy of the partition tables */ 2967251Slclee (void) memcpy(iparts, Bootblk->parts, sizeof (iparts)); 29680Sstevel@tonic-gate bootptr = (char *)iparts; /* Points to start of partition table */ 29690Sstevel@tonic-gate if (les(Bootblk->signature) != MBB_MAGIC) { 29700Sstevel@tonic-gate /* Signature is missing */ 29710Sstevel@tonic-gate nulltbl(); 2972251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 29730Sstevel@tonic-gate return; 29740Sstevel@tonic-gate } 29750Sstevel@tonic-gate /* 29760Sstevel@tonic-gate * When the DOS fdisk command deletes a partition, it is not 29770Sstevel@tonic-gate * recognized by the old algorithm. The algorithm that 29780Sstevel@tonic-gate * follows looks at each entry in the Bootrec and copies all 29790Sstevel@tonic-gate * those that are valid. 29800Sstevel@tonic-gate */ 29810Sstevel@tonic-gate j = 0; 29820Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 29830Sstevel@tonic-gate if (iparts[i].systid == 0) { 29840Sstevel@tonic-gate /* Null entry */ 29850Sstevel@tonic-gate bootptr += sizeof (struct ipart); 29860Sstevel@tonic-gate } else { 2987251Slclee fill_ipart(bootptr, &Table[j]); 29880Sstevel@tonic-gate j++; 29890Sstevel@tonic-gate bootptr += sizeof (struct ipart); 29900Sstevel@tonic-gate } 29910Sstevel@tonic-gate } 29920Sstevel@tonic-gate for (i = j; i < FD_NUMPART; i++) { 29930Sstevel@tonic-gate Table[i].systid = UNUSED; 29940Sstevel@tonic-gate Table[i].numsect = lel(UNUSED); 29950Sstevel@tonic-gate Table[i].relsect = lel(UNUSED); 29960Sstevel@tonic-gate Table[i].bootid = 0; 29970Sstevel@tonic-gate 29980Sstevel@tonic-gate } 29990Sstevel@tonic-gate /* For now, always replace the bootcode with ours */ 3000251Slclee (void) memcpy(Bootblk->bootinst, &BootCod, BOOTSZ); 30010Sstevel@tonic-gate copy_Table_to_Bootblk(); 30020Sstevel@tonic-gate } 30030Sstevel@tonic-gate 30040Sstevel@tonic-gate /* 30050Sstevel@tonic-gate * fill_ipart 30060Sstevel@tonic-gate * Initialize ipart structure values. 30070Sstevel@tonic-gate */ 3008251Slclee static void 30090Sstevel@tonic-gate fill_ipart(char *bootptr, struct ipart *partp) 30100Sstevel@tonic-gate { 30110Sstevel@tonic-gate #ifdef sparc 30120Sstevel@tonic-gate /* Packing struct ipart for Sparc */ 3013251Slclee partp->bootid = getbyte(&bootptr); 3014251Slclee partp->beghead = getbyte(&bootptr); 3015251Slclee partp->begsect = getbyte(&bootptr); 3016251Slclee partp->begcyl = getbyte(&bootptr); 3017251Slclee partp->systid = getbyte(&bootptr); 3018251Slclee partp->endhead = getbyte(&bootptr); 3019251Slclee partp->endsect = getbyte(&bootptr); 3020251Slclee partp->endcyl = getbyte(&bootptr); 3021251Slclee partp->relsect = (int32_t)getlong(&bootptr); 3022251Slclee partp->numsect = (int32_t)getlong(&bootptr); 30230Sstevel@tonic-gate #else 30240Sstevel@tonic-gate *partp = *(struct ipart *)bootptr; 30250Sstevel@tonic-gate #endif 30260Sstevel@tonic-gate } 30270Sstevel@tonic-gate 30280Sstevel@tonic-gate /* 3029251Slclee * getbyte, getlong 30300Sstevel@tonic-gate * Get a byte, a short, or a long (SPARC only). 30310Sstevel@tonic-gate */ 30320Sstevel@tonic-gate #ifdef sparc 3033251Slclee uchar_t 3034251Slclee getbyte(char **bp) 30350Sstevel@tonic-gate { 3036251Slclee uchar_t b; 3037251Slclee 3038251Slclee b = (uchar_t)**bp; 30390Sstevel@tonic-gate *bp = *bp + 1; 30400Sstevel@tonic-gate return (b); 30410Sstevel@tonic-gate } 30420Sstevel@tonic-gate 3043251Slclee uint32_t 3044251Slclee getlong(char **bp) 30450Sstevel@tonic-gate { 3046251Slclee int32_t b, bh, bl; 30470Sstevel@tonic-gate 30480Sstevel@tonic-gate bh = ((**bp) << 8) | *(*bp + 1); 30490Sstevel@tonic-gate *bp += 2; 30500Sstevel@tonic-gate bl = ((**bp) << 8) | *(*bp + 1); 30510Sstevel@tonic-gate *bp += 2; 30520Sstevel@tonic-gate 30530Sstevel@tonic-gate b = (bh << 16) | bl; 3054251Slclee return ((uint32_t)b); 30550Sstevel@tonic-gate } 30560Sstevel@tonic-gate #endif 30570Sstevel@tonic-gate 30580Sstevel@tonic-gate /* 30590Sstevel@tonic-gate * copy_Table_to_Bootblk 30600Sstevel@tonic-gate * Copy the table into the 512 boot record. Note that the unused 30610Sstevel@tonic-gate * entries will always be the last ones in the table and they are 30620Sstevel@tonic-gate * marked with 100 in sysind. The the unused portion of the table 30630Sstevel@tonic-gate * is padded with zeros in the bytes after the used entries. 30640Sstevel@tonic-gate */ 3065251Slclee static void 3066251Slclee copy_Table_to_Bootblk(void) 30670Sstevel@tonic-gate { 30680Sstevel@tonic-gate struct ipart *boot_ptr, *tbl_ptr; 30690Sstevel@tonic-gate 30700Sstevel@tonic-gate boot_ptr = (struct ipart *)Bootblk->parts; 30710Sstevel@tonic-gate tbl_ptr = (struct ipart *)&Table[0].bootid; 30720Sstevel@tonic-gate for (; tbl_ptr < (struct ipart *)&Table[FD_NUMPART].bootid; 30730Sstevel@tonic-gate tbl_ptr++, boot_ptr++) { 30745169Slclee if (tbl_ptr->systid == UNUSED) 30755169Slclee (void) memset(boot_ptr, 0, sizeof (struct ipart)); 30765169Slclee else 30775169Slclee (void) memcpy(boot_ptr, tbl_ptr, sizeof (struct ipart)); 30780Sstevel@tonic-gate } 30790Sstevel@tonic-gate Bootblk->signature = les(MBB_MAGIC); 30800Sstevel@tonic-gate } 30810Sstevel@tonic-gate 30820Sstevel@tonic-gate /* 30830Sstevel@tonic-gate * TableChanged 30840Sstevel@tonic-gate * Check for any changes in the partition table. 30850Sstevel@tonic-gate */ 3086251Slclee static int 3087251Slclee TableChanged(void) 30880Sstevel@tonic-gate { 30890Sstevel@tonic-gate int i, changed; 30900Sstevel@tonic-gate 30910Sstevel@tonic-gate changed = 0; 30920Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 30935169Slclee if (memcmp(&Old_Table[i], &Table[i], sizeof (Table[0])) != 0) { 30945169Slclee /* Partition table changed, write back to disk */ 30955169Slclee changed = 1; 30965169Slclee } 30970Sstevel@tonic-gate } 30980Sstevel@tonic-gate 30990Sstevel@tonic-gate return (changed); 31000Sstevel@tonic-gate } 31010Sstevel@tonic-gate 31020Sstevel@tonic-gate /* 31030Sstevel@tonic-gate * ffile_write 31040Sstevel@tonic-gate * Display contents of partition table to standard output or 31050Sstevel@tonic-gate * another file name without writing it to the disk (-W file). 31060Sstevel@tonic-gate */ 3107251Slclee static void 3108251Slclee ffile_write(char *file) 31090Sstevel@tonic-gate { 31100Sstevel@tonic-gate register int i; 31110Sstevel@tonic-gate FILE *fp; 31120Sstevel@tonic-gate 31130Sstevel@tonic-gate /* 31140Sstevel@tonic-gate * If file isn't standard output, then it's a file name. 31150Sstevel@tonic-gate * Open file and write it. 31160Sstevel@tonic-gate */ 31170Sstevel@tonic-gate if (file != (char *)stdout) { 31185169Slclee if ((fp = fopen(file, "w")) == NULL) { 31195169Slclee (void) fprintf(stderr, 31205169Slclee "fdisk: Cannot open output file %s.\n", 31215169Slclee file); 31225169Slclee exit(1); 31235169Slclee } 31240Sstevel@tonic-gate } 31250Sstevel@tonic-gate else 31265169Slclee fp = stdout; 31270Sstevel@tonic-gate 31280Sstevel@tonic-gate /* 31290Sstevel@tonic-gate * Write the fdisk table information 31300Sstevel@tonic-gate */ 3131251Slclee (void) fprintf(fp, "\n* %s default fdisk table\n", Dfltdev); 3132251Slclee (void) fprintf(fp, "* Dimensions:\n"); 3133251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3134251Slclee (void) fprintf(fp, "* %4d sectors/track\n", sectors); 3135251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", heads); 3136251Slclee (void) fprintf(fp, "* %4d cylinders\n", Numcyl); 3137251Slclee (void) fprintf(fp, "*\n"); 31380Sstevel@tonic-gate /* Write virtual (HBA) geometry, if required */ 31390Sstevel@tonic-gate if (v_flag) { 3140251Slclee (void) fprintf(fp, "* HBA Dimensions:\n"); 3141251Slclee (void) fprintf(fp, "* %4d bytes/sector\n", sectsiz); 3142251Slclee (void) fprintf(fp, "* %4d sectors/track\n", hba_sectors); 3143251Slclee (void) fprintf(fp, "* %4d tracks/cylinder\n", hba_heads); 3144251Slclee (void) fprintf(fp, "* %4d cylinders\n", hba_Numcyl); 3145251Slclee (void) fprintf(fp, "*\n"); 31460Sstevel@tonic-gate } 3147251Slclee (void) fprintf(fp, "* systid:\n"); 3148251Slclee (void) fprintf(fp, "* 1: DOSOS12\n"); 3149251Slclee (void) fprintf(fp, "* 2: PCIXOS\n"); 3150251Slclee (void) fprintf(fp, "* 4: DOSOS16\n"); 3151251Slclee (void) fprintf(fp, "* 5: EXTDOS\n"); 3152251Slclee (void) fprintf(fp, "* 6: DOSBIG\n"); 3153251Slclee (void) fprintf(fp, "* 7: FDISK_IFS\n"); 3154251Slclee (void) fprintf(fp, "* 8: FDISK_AIXBOOT\n"); 3155251Slclee (void) fprintf(fp, "* 9: FDISK_AIXDATA\n"); 3156251Slclee (void) fprintf(fp, "* 10: FDISK_0S2BOOT\n"); 3157251Slclee (void) fprintf(fp, "* 11: FDISK_WINDOWS\n"); 3158251Slclee (void) fprintf(fp, "* 12: FDISK_EXT_WIN\n"); 3159251Slclee (void) fprintf(fp, "* 14: FDISK_FAT95\n"); 3160251Slclee (void) fprintf(fp, "* 15: FDISK_EXTLBA\n"); 3161251Slclee (void) fprintf(fp, "* 18: DIAGPART\n"); 3162251Slclee (void) fprintf(fp, "* 65: FDISK_LINUX\n"); 3163251Slclee (void) fprintf(fp, "* 82: FDISK_CPM\n"); 3164251Slclee (void) fprintf(fp, "* 86: DOSDATA\n"); 3165251Slclee (void) fprintf(fp, "* 98: OTHEROS\n"); 3166251Slclee (void) fprintf(fp, "* 99: UNIXOS\n"); 3167251Slclee (void) fprintf(fp, "* 101: FDISK_NOVELL3\n"); 3168251Slclee (void) fprintf(fp, "* 119: FDISK_QNX4\n"); 3169251Slclee (void) fprintf(fp, "* 120: FDISK_QNX42\n"); 3170251Slclee (void) fprintf(fp, "* 121: FDISK_QNX43\n"); 3171251Slclee (void) fprintf(fp, "* 130: SUNIXOS\n"); 3172251Slclee (void) fprintf(fp, "* 131: FDISK_LINUXNAT\n"); 3173251Slclee (void) fprintf(fp, "* 134: FDISK_NTFSVOL1\n"); 3174251Slclee (void) fprintf(fp, "* 135: FDISK_NTFSVOL2\n"); 3175251Slclee (void) fprintf(fp, "* 165: FDISK_BSD\n"); 3176251Slclee (void) fprintf(fp, "* 167: FDISK_NEXTSTEP\n"); 3177251Slclee (void) fprintf(fp, "* 183: FDISK_BSDIFS\n"); 3178251Slclee (void) fprintf(fp, "* 184: FDISK_BSDISWAP\n"); 3179251Slclee (void) fprintf(fp, "* 190: X86BOOT\n"); 3180251Slclee (void) fprintf(fp, "* 191: SUNIXOS2\n"); 3181251Slclee (void) fprintf(fp, "* 238: EFI_PMBR\n"); 3182251Slclee (void) fprintf(fp, "* 239: EFI_FS\n"); 3183251Slclee (void) fprintf(fp, "*\n"); 3184251Slclee (void) fprintf(fp, 31850Sstevel@tonic-gate "\n* Id Act Bhead Bsect Bcyl Ehead Esect Ecyl" 3186*7563SPrasad.Singamsetty@Sun.COM " Rsect Numsect\n"); 3187*7563SPrasad.Singamsetty@Sun.COM 31880Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 31890Sstevel@tonic-gate if (Table[i].systid != UNUSED) 3190251Slclee (void) fprintf(fp, 3191*7563SPrasad.Singamsetty@Sun.COM " %-5d %-4d %-6d %-6d %-7d %-6d %-6d %-7d %-10u" 3192*7563SPrasad.Singamsetty@Sun.COM " %-10u\n", 31930Sstevel@tonic-gate Table[i].systid, 31940Sstevel@tonic-gate Table[i].bootid, 31950Sstevel@tonic-gate Table[i].beghead, 31960Sstevel@tonic-gate Table[i].begsect & 0x3f, 31970Sstevel@tonic-gate ((Table[i].begcyl & 0xff) | ((Table[i].begsect & 31985169Slclee 0xc0) << 2)), 31990Sstevel@tonic-gate Table[i].endhead, 32000Sstevel@tonic-gate Table[i].endsect & 0x3f, 32010Sstevel@tonic-gate ((Table[i].endcyl & 0xff) | ((Table[i].endsect & 32025169Slclee 0xc0) << 2)), 32030Sstevel@tonic-gate lel(Table[i].relsect), 32040Sstevel@tonic-gate lel(Table[i].numsect)); 32050Sstevel@tonic-gate } 32060Sstevel@tonic-gate if (fp != stdout) 3207251Slclee (void) fclose(fp); 32080Sstevel@tonic-gate } 32090Sstevel@tonic-gate 32100Sstevel@tonic-gate /* 32110Sstevel@tonic-gate * fix_slice 32120Sstevel@tonic-gate * Read the VTOC table on the Solaris partition and check that no 32130Sstevel@tonic-gate * slices exist that extend past the end of the Solaris partition. 32140Sstevel@tonic-gate * If no Solaris partition exists, nothing is done. 32150Sstevel@tonic-gate */ 3216251Slclee static void 3217251Slclee fix_slice(void) 32180Sstevel@tonic-gate { 32190Sstevel@tonic-gate int i; 3220*7563SPrasad.Singamsetty@Sun.COM uint32_t numsect; 32210Sstevel@tonic-gate 32220Sstevel@tonic-gate if (io_image) { 3223251Slclee return; 32240Sstevel@tonic-gate } 32250Sstevel@tonic-gate 32260Sstevel@tonic-gate for (i = 0; i < FD_NUMPART; i++) { 32270Sstevel@tonic-gate if (Table[i].systid == SUNIXOS || Table[i].systid == SUNIXOS2) { 32280Sstevel@tonic-gate /* 32290Sstevel@tonic-gate * Only the size matters (not starting point), since 32300Sstevel@tonic-gate * VTOC entries are relative to the start of 32310Sstevel@tonic-gate * the partition. 32320Sstevel@tonic-gate */ 32330Sstevel@tonic-gate numsect = lel(Table[i].numsect); 32340Sstevel@tonic-gate break; 32350Sstevel@tonic-gate } 32360Sstevel@tonic-gate } 32370Sstevel@tonic-gate 32380Sstevel@tonic-gate if (i >= FD_NUMPART) { 32390Sstevel@tonic-gate if (!io_nifdisk) { 32400Sstevel@tonic-gate (void) fprintf(stderr, 32410Sstevel@tonic-gate "fdisk: No Solaris partition found - VTOC not" 32420Sstevel@tonic-gate " checked.\n"); 32430Sstevel@tonic-gate } 3244251Slclee return; 32450Sstevel@tonic-gate } 32460Sstevel@tonic-gate 3247251Slclee if (readvtoc() != VTOC_OK) { 32480Sstevel@tonic-gate exit(1); /* Failed to read the VTOC */ 32495169Slclee } 32505169Slclee for (i = 0; i < V_NUMPAR; i++) { 32515169Slclee /* Special case for slice two (entire disk) */ 32525169Slclee if (i == 2) { 32535169Slclee if (disk_vtoc.v_part[i].p_start != 0) { 32545169Slclee (void) fprintf(stderr, 3255*7563SPrasad.Singamsetty@Sun.COM "slice %d starts at %llu, is not at" 32565169Slclee " start of partition", 32575169Slclee i, disk_vtoc.v_part[i].p_start); 32585169Slclee if (!io_nifdisk) { 32595169Slclee (void) printf(" adjust ?:"); 32605169Slclee if (yesno()) 32610Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 32625169Slclee } else { 32635169Slclee disk_vtoc.v_part[i].p_start = 0; 32645169Slclee (void) fprintf(stderr, " adjusted!\n"); 32650Sstevel@tonic-gate } 32665169Slclee 32675169Slclee } 32685169Slclee if (disk_vtoc.v_part[i].p_size != numsect) { 32695169Slclee (void) fprintf(stderr, 3270*7563SPrasad.Singamsetty@Sun.COM "slice %d size %llu does not cover" 32715169Slclee " complete partition", 32725169Slclee i, disk_vtoc.v_part[i].p_size); 32735169Slclee if (!io_nifdisk) { 32745169Slclee (void) printf(" adjust ?:"); 32755169Slclee if (yesno()) 32760Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 32770Sstevel@tonic-gate numsect; 32785169Slclee } else { 32795169Slclee disk_vtoc.v_part[i].p_size = numsect; 32805169Slclee (void) fprintf(stderr, " adjusted!\n"); 32810Sstevel@tonic-gate } 32825169Slclee } 32835169Slclee if (disk_vtoc.v_part[i].p_tag != V_BACKUP) { 32845169Slclee (void) fprintf(stderr, 32855169Slclee "slice %d tag was %d should be %d", 32865169Slclee i, disk_vtoc.v_part[i].p_tag, 32875169Slclee V_BACKUP); 32885169Slclee if (!io_nifdisk) { 3289251Slclee (void) printf(" fix ?:"); 32905169Slclee if (yesno()) 32910Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 32920Sstevel@tonic-gate V_BACKUP; 32935169Slclee } else { 32940Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = V_BACKUP; 32950Sstevel@tonic-gate (void) fprintf(stderr, " fixed!\n"); 32960Sstevel@tonic-gate } 32975169Slclee } 32985169Slclee continue; 32995169Slclee } 33005169Slclee if (io_ADJT) { 33015169Slclee if (disk_vtoc.v_part[i].p_start > numsect || 33025169Slclee disk_vtoc.v_part[i].p_start + 33035169Slclee disk_vtoc.v_part[i].p_size > numsect) { 33045169Slclee (void) fprintf(stderr, 3305*7563SPrasad.Singamsetty@Sun.COM "slice %d (start %llu, end %llu)" 33065169Slclee " is larger than the partition", 33075169Slclee i, disk_vtoc.v_part[i].p_start, 33085169Slclee disk_vtoc.v_part[i].p_start + 33095169Slclee disk_vtoc.v_part[i].p_size); 33105169Slclee if (!io_nifdisk) { 33115169Slclee (void) printf(" remove ?:"); 33125169Slclee if (yesno()) { 33130Sstevel@tonic-gate disk_vtoc.v_part[i].p_size = 0; 33140Sstevel@tonic-gate disk_vtoc.v_part[i].p_start = 0; 33150Sstevel@tonic-gate disk_vtoc.v_part[i].p_tag = 0; 33160Sstevel@tonic-gate disk_vtoc.v_part[i].p_flag = 0; 33170Sstevel@tonic-gate } 33180Sstevel@tonic-gate } else { 33195169Slclee disk_vtoc.v_part[i].p_size = 0; 33205169Slclee disk_vtoc.v_part[i].p_start = 0; 33215169Slclee disk_vtoc.v_part[i].p_tag = 0; 33225169Slclee disk_vtoc.v_part[i].p_flag = 0; 33230Sstevel@tonic-gate (void) fprintf(stderr, 33245169Slclee " removed!\n"); 33255169Slclee } 33265169Slclee } 33275169Slclee continue; 33285169Slclee } 33295169Slclee if (disk_vtoc.v_part[i].p_start > numsect) { 33305169Slclee (void) fprintf(stderr, 3331*7563SPrasad.Singamsetty@Sun.COM "slice %d (start %llu) is larger than the " 3332*7563SPrasad.Singamsetty@Sun.COM "partition", i, disk_vtoc.v_part[i].p_start); 33335169Slclee if (!io_nifdisk) { 33345169Slclee (void) printf(" remove ?:"); 33355169Slclee if (yesno()) { 33365169Slclee disk_vtoc.v_part[i].p_size = 0; 33375169Slclee disk_vtoc.v_part[i].p_start = 0; 33385169Slclee disk_vtoc.v_part[i].p_tag = 0; 33395169Slclee disk_vtoc.v_part[i].p_flag = 0; 33400Sstevel@tonic-gate } 33415169Slclee } else { 33425169Slclee disk_vtoc.v_part[i].p_size = 0; 33435169Slclee disk_vtoc.v_part[i].p_start = 0; 33445169Slclee disk_vtoc.v_part[i].p_tag = 0; 33455169Slclee disk_vtoc.v_part[i].p_flag = 0; 33465169Slclee (void) fprintf(stderr, 33475169Slclee " removed!\n"); 33485169Slclee } 33495169Slclee } else if (disk_vtoc.v_part[i].p_start 33505169Slclee + disk_vtoc.v_part[i].p_size > numsect) { 33515169Slclee (void) fprintf(stderr, 3352*7563SPrasad.Singamsetty@Sun.COM "slice %d (end %llu) is larger" 33535169Slclee " than the partition", 33545169Slclee i, 33555169Slclee disk_vtoc.v_part[i].p_start + 33565169Slclee disk_vtoc.v_part[i].p_size); 33575169Slclee if (!io_nifdisk) { 33585169Slclee (void) printf(" adjust ?:"); 33595169Slclee if (yesno()) { 33605169Slclee disk_vtoc.v_part[i].p_size = numsect; 33615169Slclee } 33625169Slclee } else { 33635169Slclee disk_vtoc.v_part[i].p_size = numsect; 33645169Slclee (void) fprintf(stderr, " adjusted!\n"); 33650Sstevel@tonic-gate } 33660Sstevel@tonic-gate } 33670Sstevel@tonic-gate } 33680Sstevel@tonic-gate #if 1 /* bh for now */ 33690Sstevel@tonic-gate /* Make the VTOC look sane - ha ha */ 33700Sstevel@tonic-gate disk_vtoc.v_version = V_VERSION; 33710Sstevel@tonic-gate disk_vtoc.v_sanity = VTOC_SANE; 33720Sstevel@tonic-gate disk_vtoc.v_nparts = V_NUMPAR; 33730Sstevel@tonic-gate if (disk_vtoc.v_sectorsz == 0) 33740Sstevel@tonic-gate disk_vtoc.v_sectorsz = NBPSCTR; 33750Sstevel@tonic-gate #endif 33760Sstevel@tonic-gate 33770Sstevel@tonic-gate /* Write the VTOC back to the disk */ 33780Sstevel@tonic-gate if (!io_readonly) 3379251Slclee (void) writevtoc(); 33800Sstevel@tonic-gate } 33810Sstevel@tonic-gate 33820Sstevel@tonic-gate /* 33830Sstevel@tonic-gate * yesno 33840Sstevel@tonic-gate * Get yes or no answer. Return 1 for yes and 0 for no. 33850Sstevel@tonic-gate */ 33860Sstevel@tonic-gate 3387251Slclee static int 3388251Slclee yesno(void) 33890Sstevel@tonic-gate { 33900Sstevel@tonic-gate char s[80]; 33910Sstevel@tonic-gate 33920Sstevel@tonic-gate for (;;) { 3393251Slclee (void) gets(s); 33940Sstevel@tonic-gate rm_blanks(s); 33950Sstevel@tonic-gate if ((s[1] != 0) || ((s[0] != 'y') && (s[0] != 'n'))) { 3396251Slclee (void) printf(E_LINE); 3397251Slclee (void) printf("Please answer with \"y\" or \"n\": "); 33980Sstevel@tonic-gate continue; 33990Sstevel@tonic-gate } 34000Sstevel@tonic-gate if (s[0] == 'y') 34010Sstevel@tonic-gate return (1); 34020Sstevel@tonic-gate else 34030Sstevel@tonic-gate return (0); 34040Sstevel@tonic-gate } 34050Sstevel@tonic-gate } 34060Sstevel@tonic-gate 34070Sstevel@tonic-gate /* 34080Sstevel@tonic-gate * readvtoc 34090Sstevel@tonic-gate * Read the VTOC from the Solaris partition of the device. 34100Sstevel@tonic-gate */ 3411251Slclee static int 3412251Slclee readvtoc(void) 34130Sstevel@tonic-gate { 34140Sstevel@tonic-gate int i; 34150Sstevel@tonic-gate int retval = VTOC_OK; 34160Sstevel@tonic-gate 3417*7563SPrasad.Singamsetty@Sun.COM if ((i = read_extvtoc(Dev, &disk_vtoc)) < VTOC_OK) { 34180Sstevel@tonic-gate if (i == VT_EINVAL) { 34190Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Invalid VTOC.\n"); 34200Sstevel@tonic-gate vt_inval++; 34210Sstevel@tonic-gate retval = VTOC_INVAL; 34220Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 34230Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 34245169Slclee "GPT\n"); 34250Sstevel@tonic-gate retval = VTOC_NOTSUP; 34260Sstevel@tonic-gate } else { 34270Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot read VTOC.\n"); 34280Sstevel@tonic-gate retval = VTOC_RWERR; 34290Sstevel@tonic-gate } 34300Sstevel@tonic-gate } 34310Sstevel@tonic-gate return (retval); 34320Sstevel@tonic-gate } 34330Sstevel@tonic-gate 34340Sstevel@tonic-gate /* 34350Sstevel@tonic-gate * writevtoc 34360Sstevel@tonic-gate * Write the VTOC to the Solaris partition on the device. 34370Sstevel@tonic-gate */ 3438251Slclee static int 3439251Slclee writevtoc(void) 34400Sstevel@tonic-gate { 34410Sstevel@tonic-gate int i; 34420Sstevel@tonic-gate int retval = 0; 34430Sstevel@tonic-gate 3444*7563SPrasad.Singamsetty@Sun.COM if ((i = write_extvtoc(Dev, &disk_vtoc)) != 0) { 34450Sstevel@tonic-gate if (i == VT_EINVAL) { 34460Sstevel@tonic-gate (void) fprintf(stderr, 34470Sstevel@tonic-gate "fdisk: Invalid entry exists in VTOC.\n"); 34480Sstevel@tonic-gate retval = VTOC_INVAL; 34490Sstevel@tonic-gate } else if (i == VT_ENOTSUP) { 34500Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: partition may have EFI " 34515169Slclee "GPT\n"); 34520Sstevel@tonic-gate retval = VTOC_NOTSUP; 34530Sstevel@tonic-gate } else { 34540Sstevel@tonic-gate (void) fprintf(stderr, "fdisk: Cannot write VTOC.\n"); 34550Sstevel@tonic-gate retval = VTOC_RWERR; 34560Sstevel@tonic-gate } 34570Sstevel@tonic-gate } 34580Sstevel@tonic-gate return (retval); 34590Sstevel@tonic-gate } 34600Sstevel@tonic-gate 34610Sstevel@tonic-gate /* 34620Sstevel@tonic-gate * efi_ioctl 34630Sstevel@tonic-gate * issues DKIOCSETEFI IOCTL 34640Sstevel@tonic-gate * (duplicate of private efi_ioctl() in rdwr_efi.c 34650Sstevel@tonic-gate */ 34660Sstevel@tonic-gate static int 34670Sstevel@tonic-gate efi_ioctl(int fd, int cmd, dk_efi_t *dk_ioc) 34680Sstevel@tonic-gate { 34690Sstevel@tonic-gate void *data = dk_ioc->dki_data; 34700Sstevel@tonic-gate int error; 34710Sstevel@tonic-gate 34720Sstevel@tonic-gate dk_ioc->dki_data_64 = (uintptr_t)data; 34730Sstevel@tonic-gate error = ioctl(fd, cmd, (void *)dk_ioc); 34740Sstevel@tonic-gate 34750Sstevel@tonic-gate return (error); 34760Sstevel@tonic-gate } 34770Sstevel@tonic-gate 34780Sstevel@tonic-gate /* 34790Sstevel@tonic-gate * clear_efi 34800Sstevel@tonic-gate * Clear EFI labels from the EFI_PMBR partition on the device 34810Sstevel@tonic-gate * This function is modeled on the libefi(3LIB) call efi_write() 34820Sstevel@tonic-gate */ 3483251Slclee static int 3484251Slclee clear_efi(void) 34850Sstevel@tonic-gate { 34860Sstevel@tonic-gate struct dk_gpt *efi_vtoc; 34870Sstevel@tonic-gate dk_efi_t dk_ioc; 34880Sstevel@tonic-gate 34890Sstevel@tonic-gate /* 34900Sstevel@tonic-gate * see if we can read the EFI label 34910Sstevel@tonic-gate */ 34920Sstevel@tonic-gate if (efi_alloc_and_read(Dev, &efi_vtoc) < 0) { 34930Sstevel@tonic-gate return (VT_ERROR); 34940Sstevel@tonic-gate } 34950Sstevel@tonic-gate 34960Sstevel@tonic-gate /* 34970Sstevel@tonic-gate * set up the dk_ioc structure for writing 34980Sstevel@tonic-gate */ 34990Sstevel@tonic-gate dk_ioc.dki_lba = 1; 35000Sstevel@tonic-gate dk_ioc.dki_length = EFI_MIN_ARRAY_SIZE + efi_vtoc->efi_lbasize; 35010Sstevel@tonic-gate 35020Sstevel@tonic-gate if ((dk_ioc.dki_data = calloc(dk_ioc.dki_length, 1)) == NULL) { 35030Sstevel@tonic-gate return (VT_ERROR); 35040Sstevel@tonic-gate } 35050Sstevel@tonic-gate 35060Sstevel@tonic-gate /* 35070Sstevel@tonic-gate * clear the primary label 35080Sstevel@tonic-gate */ 35090Sstevel@tonic-gate if (io_debug) { 3510251Slclee (void) fprintf(stderr, 3511251Slclee "\tClearing primary EFI label at block %lld\n", 3512251Slclee dk_ioc.dki_lba); 35130Sstevel@tonic-gate } 35140Sstevel@tonic-gate 35150Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 35160Sstevel@tonic-gate free(dk_ioc.dki_data); 35170Sstevel@tonic-gate switch (errno) { 35180Sstevel@tonic-gate case EIO: 35190Sstevel@tonic-gate return (VT_EIO); 35200Sstevel@tonic-gate case EINVAL: 35210Sstevel@tonic-gate return (VT_EINVAL); 35220Sstevel@tonic-gate default: 35230Sstevel@tonic-gate return (VT_ERROR); 35240Sstevel@tonic-gate } 35250Sstevel@tonic-gate } 35260Sstevel@tonic-gate 35270Sstevel@tonic-gate /* 35280Sstevel@tonic-gate * clear the backup partition table 35290Sstevel@tonic-gate */ 35300Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_u_lba + 1; 35310Sstevel@tonic-gate dk_ioc.dki_length -= efi_vtoc->efi_lbasize; 35320Sstevel@tonic-gate dk_ioc.dki_data++; 35330Sstevel@tonic-gate if (io_debug) { 3534251Slclee (void) fprintf(stderr, 3535251Slclee "\tClearing backup partition table at block %lld\n", 3536251Slclee dk_ioc.dki_lba); 35370Sstevel@tonic-gate } 35380Sstevel@tonic-gate 35390Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 35400Sstevel@tonic-gate (void) fprintf(stderr, "\tUnable to clear backup EFI label at " 35415169Slclee "block %llu; errno %d\n", efi_vtoc->efi_last_u_lba + 1, 35425169Slclee errno); 35430Sstevel@tonic-gate } 35440Sstevel@tonic-gate 35450Sstevel@tonic-gate /* 35460Sstevel@tonic-gate * clear the backup label 35470Sstevel@tonic-gate */ 35480Sstevel@tonic-gate dk_ioc.dki_lba = efi_vtoc->efi_last_lba; 35490Sstevel@tonic-gate dk_ioc.dki_length = efi_vtoc->efi_lbasize; 35500Sstevel@tonic-gate dk_ioc.dki_data--; 35510Sstevel@tonic-gate if (io_debug) { 3552251Slclee (void) fprintf(stderr, "\tClearing backup label at block " 3553251Slclee "%lld\n", dk_ioc.dki_lba); 35540Sstevel@tonic-gate } 35550Sstevel@tonic-gate 35560Sstevel@tonic-gate if (efi_ioctl(Dev, DKIOCSETEFI, &dk_ioc) == -1) { 3557251Slclee (void) fprintf(stderr, 3558251Slclee "\tUnable to clear backup EFI label at " 3559251Slclee "block %llu; errno %d\n", 3560251Slclee efi_vtoc->efi_last_lba, 3561251Slclee errno); 35620Sstevel@tonic-gate } 35630Sstevel@tonic-gate 35640Sstevel@tonic-gate free(dk_ioc.dki_data); 35650Sstevel@tonic-gate efi_free(efi_vtoc); 35660Sstevel@tonic-gate 35670Sstevel@tonic-gate return (0); 35680Sstevel@tonic-gate } 35690Sstevel@tonic-gate 35700Sstevel@tonic-gate /* 35710Sstevel@tonic-gate * clear_vtoc 35720Sstevel@tonic-gate * Clear the VTOC from the current or previous Solaris partition on the 35730Sstevel@tonic-gate * device. 35740Sstevel@tonic-gate */ 3575251Slclee static void 35760Sstevel@tonic-gate clear_vtoc(int table, int part) 35770Sstevel@tonic-gate { 35780Sstevel@tonic-gate struct ipart *clr_table; 35790Sstevel@tonic-gate struct dk_label disk_label; 3580*7563SPrasad.Singamsetty@Sun.COM uint32_t pcyl, ncyl, count; 3581*7563SPrasad.Singamsetty@Sun.COM diskaddr_t backup_block, solaris_offset; 3582*7563SPrasad.Singamsetty@Sun.COM ssize_t bytes; 35836549Sbharding off_t seek_byte; 35840Sstevel@tonic-gate 35850Sstevel@tonic-gate #ifdef DEBUG 35860Sstevel@tonic-gate struct dk_label read_label; 35870Sstevel@tonic-gate #endif /* DEBUG */ 35880Sstevel@tonic-gate 35890Sstevel@tonic-gate if (table == OLD) { 35900Sstevel@tonic-gate clr_table = &Old_Table[part]; 35910Sstevel@tonic-gate } else { 35920Sstevel@tonic-gate clr_table = &Table[part]; 35930Sstevel@tonic-gate } 35940Sstevel@tonic-gate 3595251Slclee (void) memset(&disk_label, 0, sizeof (struct dk_label)); 35960Sstevel@tonic-gate 35976549Sbharding seek_byte = (off_t)(lel(clr_table->relsect) + VTOC_OFFSET) * sectsiz; 35980Sstevel@tonic-gate 35990Sstevel@tonic-gate if (io_debug) { 36006549Sbharding (void) fprintf(stderr, 36016549Sbharding "\tClearing primary VTOC at byte %llu (block %llu)\n", 36026549Sbharding (uint64_t)seek_byte, 36036549Sbharding (uint64_t)(lel(clr_table->relsect) + VTOC_OFFSET)); 36040Sstevel@tonic-gate } 36050Sstevel@tonic-gate 36060Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3607251Slclee (void) fprintf(stderr, 36086549Sbharding "\tError seeking to primary label at byte %llu\n", 36096549Sbharding (uint64_t)seek_byte); 3610251Slclee return; 36110Sstevel@tonic-gate } 36120Sstevel@tonic-gate 36130Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 36140Sstevel@tonic-gate 36150Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3616251Slclee (void) fprintf(stderr, 3617*7563SPrasad.Singamsetty@Sun.COM "\tWarning: only %d bytes written to clear primary" 3618*7563SPrasad.Singamsetty@Sun.COM " VTOC!\n", bytes); 36190Sstevel@tonic-gate } 36200Sstevel@tonic-gate 36210Sstevel@tonic-gate #ifdef DEBUG 36220Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3623251Slclee (void) fprintf(stderr, 36246549Sbharding "DEBUG: Error seeking to primary label at byte %llu\n", 36256549Sbharding (uint64_t)seek_byte); 3626251Slclee return; 36270Sstevel@tonic-gate } else { 36286549Sbharding (void) fprintf(stderr, 36296549Sbharding "DEBUG: Successful lseek() to byte %llu\n", 36306549Sbharding (uint64_t)seek_byte); 36310Sstevel@tonic-gate } 36320Sstevel@tonic-gate 36330Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 36340Sstevel@tonic-gate 36350Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3636251Slclee (void) fprintf(stderr, 3637251Slclee "DEBUG: Warning: only %d bytes read of label\n", 36380Sstevel@tonic-gate bytes); 36390Sstevel@tonic-gate } 36400Sstevel@tonic-gate 36410Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3642251Slclee (void) fprintf(stderr, 3643251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 36440Sstevel@tonic-gate } else { 3645251Slclee (void) fprintf(stderr, "DEBUG Good compare of disk_label and " 36460Sstevel@tonic-gate "read_label\n"); 36470Sstevel@tonic-gate } 36480Sstevel@tonic-gate #endif /* DEBUG */ 36490Sstevel@tonic-gate 36500Sstevel@tonic-gate /* Clear backup label */ 36510Sstevel@tonic-gate pcyl = lel(clr_table->numsect) / (heads * sectors); 36520Sstevel@tonic-gate solaris_offset = lel(clr_table->relsect); 36530Sstevel@tonic-gate ncyl = pcyl - acyl; 36540Sstevel@tonic-gate 36550Sstevel@tonic-gate backup_block = ((ncyl + acyl - 1) * 36560Sstevel@tonic-gate (heads * sectors)) + ((heads - 1) * sectors) + 1; 36570Sstevel@tonic-gate 36580Sstevel@tonic-gate for (count = 1; count < 6; count++) { 36596549Sbharding seek_byte = (off_t)(solaris_offset + backup_block) * 512; 36600Sstevel@tonic-gate 36610Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3662251Slclee (void) fprintf(stderr, 36636549Sbharding "\tError seeking to backup label at byte %llu on " 36646549Sbharding "%s.\n", (uint64_t)seek_byte, Dfltdev); 3665251Slclee return; 36660Sstevel@tonic-gate } 36670Sstevel@tonic-gate 36680Sstevel@tonic-gate if (io_debug) { 3669251Slclee (void) fprintf(stderr, "\tClearing backup VTOC at" 36706549Sbharding " byte %llu (block %llu)\n", 36716549Sbharding (uint64_t)seek_byte, 36726549Sbharding (uint64_t)(solaris_offset + backup_block)); 36730Sstevel@tonic-gate } 36740Sstevel@tonic-gate 36750Sstevel@tonic-gate bytes = write(Dev, &disk_label, sizeof (struct dk_label)); 36760Sstevel@tonic-gate 36770Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3678251Slclee (void) fprintf(stderr, 3679251Slclee "\t\tWarning: only %d bytes written to " 36806549Sbharding "clear backup VTOC at block %llu!\n", bytes, 36816549Sbharding (uint64_t)(solaris_offset + backup_block)); 36820Sstevel@tonic-gate } 36830Sstevel@tonic-gate 36840Sstevel@tonic-gate #ifdef DEBUG 36850Sstevel@tonic-gate if (lseek(Dev, seek_byte, SEEK_SET) == -1) { 3686251Slclee (void) fprintf(stderr, 36876549Sbharding "DEBUG: Error seeking to backup label at byte %llu\n", 36886549Sbharding (uint64_t)seek_byte); 3689251Slclee return; 36900Sstevel@tonic-gate } else { 36916549Sbharding (void) fprintf(stderr, 36926549Sbharding "DEBUG: Successful lseek() to byte %llu\n", 36936549Sbharding (uint64_t)seek_byte); 36940Sstevel@tonic-gate } 36950Sstevel@tonic-gate 36960Sstevel@tonic-gate bytes = read(Dev, &read_label, sizeof (struct dk_label)); 36970Sstevel@tonic-gate 36980Sstevel@tonic-gate if (bytes != sizeof (struct dk_label)) { 3699251Slclee (void) fprintf(stderr, 3700251Slclee "DEBUG: Warning: only %d bytes read of backup label\n", 3701251Slclee bytes); 37020Sstevel@tonic-gate } 37030Sstevel@tonic-gate 37040Sstevel@tonic-gate if (memcmp(&disk_label, &read_label, sizeof (struct dk_label)) != 0) { 3705251Slclee (void) fprintf(stderr, 3706251Slclee "DEBUG: Warning: disk_label and read_label differ!!!\n"); 37070Sstevel@tonic-gate } else { 3708251Slclee (void) fprintf(stderr, 3709251Slclee "DEBUG: Good compare of disk_label and backup " 37100Sstevel@tonic-gate "read_label\n"); 37110Sstevel@tonic-gate } 37120Sstevel@tonic-gate #endif /* DEBUG */ 37130Sstevel@tonic-gate 37140Sstevel@tonic-gate backup_block += 2; 37150Sstevel@tonic-gate } 37160Sstevel@tonic-gate } 37170Sstevel@tonic-gate 37180Sstevel@tonic-gate #define FDISK_STANDARD_LECTURE \ 37190Sstevel@tonic-gate "Fdisk is normally used with the device that " \ 37200Sstevel@tonic-gate "represents the entire fixed disk.\n" \ 37210Sstevel@tonic-gate "(For example, /dev/rdsk/c0d0p0 on x86 or " \ 37220Sstevel@tonic-gate "/dev/rdsk/c0t5d0s2 on sparc).\n" 37230Sstevel@tonic-gate 37240Sstevel@tonic-gate #define FDISK_LECTURE_NOT_SECTOR_ZERO \ 37250Sstevel@tonic-gate "The device does not appear to include absolute\n" \ 37260Sstevel@tonic-gate "sector 0 of the PHYSICAL disk " \ 37270Sstevel@tonic-gate "(the normal location for an fdisk table).\n" 37280Sstevel@tonic-gate 37290Sstevel@tonic-gate #define FDISK_LECTURE_NOT_FULL \ 37300Sstevel@tonic-gate "The device does not appear to encompass the entire PHYSICAL disk.\n" 37310Sstevel@tonic-gate 37320Sstevel@tonic-gate #define FDISK_LECTURE_NO_VTOC \ 37330Sstevel@tonic-gate "Unable to find a volume table of contents.\n" \ 37340Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 37350Sstevel@tonic-gate 37360Sstevel@tonic-gate #define FDISK_LECTURE_NO_GEOM \ 37370Sstevel@tonic-gate "Unable to get geometry from device.\n" \ 37380Sstevel@tonic-gate "Cannot verify the device encompasses the full PHYSICAL disk.\n" 37390Sstevel@tonic-gate 37400Sstevel@tonic-gate #define FDISK_SHALL_I_CONTINUE \ 37410Sstevel@tonic-gate "Are you sure you want to continue? (y/n) " 37420Sstevel@tonic-gate 37430Sstevel@tonic-gate /* 37440Sstevel@tonic-gate * lecture_and_query 37450Sstevel@tonic-gate * Called when a sanity check fails. This routine gives a warning 37460Sstevel@tonic-gate * specific to the check that fails, followed by a generic lecture 37470Sstevel@tonic-gate * about the "right" device to supply as input. Then, if appropriate, 37480Sstevel@tonic-gate * it will prompt the user on whether or not they want to continue. 37490Sstevel@tonic-gate * Inappropriate times for prompting are when the user has selected 37500Sstevel@tonic-gate * non-interactive mode or read-only mode. 37510Sstevel@tonic-gate */ 3752251Slclee static int 37530Sstevel@tonic-gate lecture_and_query(char *warning, char *devname) 37540Sstevel@tonic-gate { 37550Sstevel@tonic-gate if (io_nifdisk) 37560Sstevel@tonic-gate return (0); 37570Sstevel@tonic-gate 3758251Slclee (void) fprintf(stderr, "WARNING: Device %s: \n", devname); 3759251Slclee (void) fprintf(stderr, "%s", warning); 3760251Slclee (void) fprintf(stderr, FDISK_STANDARD_LECTURE); 3761251Slclee (void) fprintf(stderr, FDISK_SHALL_I_CONTINUE); 37620Sstevel@tonic-gate 37630Sstevel@tonic-gate return (yesno()); 37640Sstevel@tonic-gate } 37650Sstevel@tonic-gate 3766251Slclee static void 37670Sstevel@tonic-gate sanity_check_provided_device(char *devname, int fd) 37680Sstevel@tonic-gate { 3769*7563SPrasad.Singamsetty@Sun.COM struct extvtoc v; 37700Sstevel@tonic-gate struct dk_geom d; 37710Sstevel@tonic-gate struct part_info pi; 3772*7563SPrasad.Singamsetty@Sun.COM struct extpart_info extpi; 3773*7563SPrasad.Singamsetty@Sun.COM diskaddr_t totsize; 37740Sstevel@tonic-gate int idx = -1; 37750Sstevel@tonic-gate 37760Sstevel@tonic-gate /* 37770Sstevel@tonic-gate * First try the PARTINFO ioctl. If it works, we will be able 37780Sstevel@tonic-gate * to tell if they've specified the full disk partition by checking 37790Sstevel@tonic-gate * to see if they've specified a partition that starts at sector 0. 37800Sstevel@tonic-gate */ 3781*7563SPrasad.Singamsetty@Sun.COM if (ioctl(fd, DKIOCEXTPARTINFO, &extpi) != -1) { 3782*7563SPrasad.Singamsetty@Sun.COM if (extpi.p_start != 0) { 3783*7563SPrasad.Singamsetty@Sun.COM if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 3784*7563SPrasad.Singamsetty@Sun.COM devname)) { 3785*7563SPrasad.Singamsetty@Sun.COM (void) close(fd); 3786*7563SPrasad.Singamsetty@Sun.COM exit(1); 3787*7563SPrasad.Singamsetty@Sun.COM } 3788*7563SPrasad.Singamsetty@Sun.COM } 3789*7563SPrasad.Singamsetty@Sun.COM } else if (ioctl(fd, DKIOCPARTINFO, &pi) != -1) { 37900Sstevel@tonic-gate if (pi.p_start != 0) { 37910Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_SECTOR_ZERO, 37920Sstevel@tonic-gate devname)) { 37930Sstevel@tonic-gate (void) close(fd); 37940Sstevel@tonic-gate exit(1); 37950Sstevel@tonic-gate } 37960Sstevel@tonic-gate } 37970Sstevel@tonic-gate } else { 3798*7563SPrasad.Singamsetty@Sun.COM if ((idx = read_extvtoc(fd, &v)) < 0) { 37990Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_VTOC, 38000Sstevel@tonic-gate devname)) { 38010Sstevel@tonic-gate (void) close(fd); 38020Sstevel@tonic-gate exit(1); 38030Sstevel@tonic-gate } 38040Sstevel@tonic-gate return; 38050Sstevel@tonic-gate } 38060Sstevel@tonic-gate if (ioctl(fd, DKIOCGGEOM, &d) == -1) { 38070Sstevel@tonic-gate perror(devname); 38080Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NO_GEOM, 38090Sstevel@tonic-gate devname)) { 38100Sstevel@tonic-gate (void) close(fd); 38110Sstevel@tonic-gate exit(1); 38120Sstevel@tonic-gate } 38130Sstevel@tonic-gate return; 38140Sstevel@tonic-gate } 3815*7563SPrasad.Singamsetty@Sun.COM totsize = (diskaddr_t)d.dkg_ncyl * d.dkg_nhead * d.dkg_nsect; 38160Sstevel@tonic-gate if (v.v_part[idx].p_size != totsize) { 38170Sstevel@tonic-gate if (!lecture_and_query(FDISK_LECTURE_NOT_FULL, 38180Sstevel@tonic-gate devname)) { 38190Sstevel@tonic-gate (void) close(fd); 38200Sstevel@tonic-gate exit(1); 38210Sstevel@tonic-gate } 38220Sstevel@tonic-gate } 38230Sstevel@tonic-gate } 38240Sstevel@tonic-gate } 38250Sstevel@tonic-gate 38260Sstevel@tonic-gate 38270Sstevel@tonic-gate /* 38280Sstevel@tonic-gate * get_node 38290Sstevel@tonic-gate * Called from main to construct the name of the device node to open. 38300Sstevel@tonic-gate * Initially tries to stat the node exactly as provided, if that fails 38310Sstevel@tonic-gate * we prepend the default path (/dev/rdsk/). 38320Sstevel@tonic-gate */ 38330Sstevel@tonic-gate static char * 38340Sstevel@tonic-gate get_node(char *devname) 38350Sstevel@tonic-gate { 38360Sstevel@tonic-gate char *node; 38370Sstevel@tonic-gate struct stat statbuf; 38380Sstevel@tonic-gate size_t space; 38390Sstevel@tonic-gate 38400Sstevel@tonic-gate /* Don't do anything if we are skipping device checks */ 38410Sstevel@tonic-gate if (io_image) 38420Sstevel@tonic-gate return (devname); 38430Sstevel@tonic-gate 38440Sstevel@tonic-gate node = devname; 38450Sstevel@tonic-gate 38460Sstevel@tonic-gate /* Try the node as provided first */ 38470Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 38480Sstevel@tonic-gate /* 38490Sstevel@tonic-gate * Copy the passed in string to a new buffer, prepend the 38500Sstevel@tonic-gate * default path and try again. 38510Sstevel@tonic-gate */ 38520Sstevel@tonic-gate space = strlen(DEFAULT_PATH) + strlen(devname) + 1; 38530Sstevel@tonic-gate 38540Sstevel@tonic-gate if ((node = malloc(space)) == NULL) { 3855251Slclee (void) fprintf(stderr, "fdisk: Unable to obtain memory " 38560Sstevel@tonic-gate "for device node.\n"); 38570Sstevel@tonic-gate exit(1); 38580Sstevel@tonic-gate } 38590Sstevel@tonic-gate 38600Sstevel@tonic-gate /* Copy over the default path and the provided node */ 38610Sstevel@tonic-gate (void) strncpy(node, DEFAULT_PATH, strlen(DEFAULT_PATH)); 38620Sstevel@tonic-gate space -= strlen(DEFAULT_PATH); 38630Sstevel@tonic-gate (void) strlcpy(node + strlen(DEFAULT_PATH), devname, space); 38640Sstevel@tonic-gate 38650Sstevel@tonic-gate /* Try to stat it again */ 38660Sstevel@tonic-gate if (stat(node, (struct stat *)&statbuf) == -1) { 38670Sstevel@tonic-gate /* Failed all options, give up */ 3868251Slclee (void) fprintf(stderr, 3869251Slclee "fdisk: Cannot stat device %s.\n", 38700Sstevel@tonic-gate devname); 38710Sstevel@tonic-gate exit(1); 38720Sstevel@tonic-gate } 38730Sstevel@tonic-gate } 38740Sstevel@tonic-gate 38750Sstevel@tonic-gate /* Make sure the device specified is the raw device */ 38760Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFCHR) { 3877251Slclee (void) fprintf(stderr, 3878251Slclee "fdisk: %s must be a raw device.\n", node); 38790Sstevel@tonic-gate exit(1); 38800Sstevel@tonic-gate } 38810Sstevel@tonic-gate 38820Sstevel@tonic-gate return (node); 38830Sstevel@tonic-gate } 3884